id
int64 1
36.7k
| label
int64 0
1
| bug_url
stringlengths 91
134
| bug_function
stringlengths 13
72.7k
| functions
stringlengths 17
79.2k
|
---|---|---|---|---|
35,401 | 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)];
} | ['int BN_mod_exp2_mont(BIGNUM *rr, const BIGNUM *a1, const BIGNUM *p1,\n const BIGNUM *a2, const BIGNUM *p2, const BIGNUM *m,\n BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n int i, j, bits, b, bits1, bits2, ret =\n 0, wpos1, wpos2, window1, window2, wvalue1, wvalue2;\n int r_is_one = 1;\n BIGNUM *d, *r;\n const BIGNUM *a_mod_m;\n BIGNUM *val1[TABLE_SIZE], *val2[TABLE_SIZE];\n BN_MONT_CTX *mont = NULL;\n bn_check_top(a1);\n bn_check_top(p1);\n bn_check_top(a2);\n bn_check_top(p2);\n bn_check_top(m);\n if (!(m->d[0] & 1)) {\n BNerr(BN_F_BN_MOD_EXP2_MONT, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n bits1 = BN_num_bits(p1);\n bits2 = BN_num_bits(p2);\n if ((bits1 == 0) && (bits2 == 0)) {\n ret = BN_one(rr);\n return ret;\n }\n bits = (bits1 > bits2) ? bits1 : bits2;\n BN_CTX_start(ctx);\n d = BN_CTX_get(ctx);\n r = BN_CTX_get(ctx);\n val1[0] = BN_CTX_get(ctx);\n val2[0] = BN_CTX_get(ctx);\n if (val2[0] == NULL)\n goto err;\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n window1 = BN_window_bits_for_exponent_size(bits1);\n window2 = BN_window_bits_for_exponent_size(bits2);\n if (a1->neg || BN_ucmp(a1, m) >= 0) {\n if (!BN_mod(val1[0], a1, m, ctx))\n goto err;\n a_mod_m = val1[0];\n } else\n a_mod_m = a1;\n if (BN_is_zero(a_mod_m)) {\n BN_zero(rr);\n ret = 1;\n goto err;\n }\n if (!BN_to_montgomery(val1[0], a_mod_m, mont, ctx))\n goto err;\n if (window1 > 1) {\n if (!BN_mod_mul_montgomery(d, val1[0], val1[0], mont, ctx))\n goto err;\n j = 1 << (window1 - 1);\n for (i = 1; i < j; i++) {\n if (((val1[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul_montgomery(val1[i], val1[i - 1], d, mont, ctx))\n goto err;\n }\n }\n if (a2->neg || BN_ucmp(a2, m) >= 0) {\n if (!BN_mod(val2[0], a2, m, ctx))\n goto err;\n a_mod_m = val2[0];\n } else\n a_mod_m = a2;\n if (BN_is_zero(a_mod_m)) {\n BN_zero(rr);\n ret = 1;\n goto err;\n }\n if (!BN_to_montgomery(val2[0], a_mod_m, mont, ctx))\n goto err;\n if (window2 > 1) {\n if (!BN_mod_mul_montgomery(d, val2[0], val2[0], mont, ctx))\n goto err;\n j = 1 << (window2 - 1);\n for (i = 1; i < j; i++) {\n if (((val2[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul_montgomery(val2[i], val2[i - 1], d, mont, ctx))\n goto err;\n }\n }\n r_is_one = 1;\n wvalue1 = 0;\n wvalue2 = 0;\n wpos1 = 0;\n wpos2 = 0;\n if (!BN_to_montgomery(r, BN_value_one(), mont, ctx))\n goto err;\n for (b = bits - 1; b >= 0; b--) {\n if (!r_is_one) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (!wvalue1)\n if (BN_is_bit_set(p1, b)) {\n i = b - window1 + 1;\n while (!BN_is_bit_set(p1, i))\n i++;\n wpos1 = i;\n wvalue1 = 1;\n for (i = b - 1; i >= wpos1; i--) {\n wvalue1 <<= 1;\n if (BN_is_bit_set(p1, i))\n wvalue1++;\n }\n }\n if (!wvalue2)\n if (BN_is_bit_set(p2, b)) {\n i = b - window2 + 1;\n while (!BN_is_bit_set(p2, i))\n i++;\n wpos2 = i;\n wvalue2 = 1;\n for (i = b - 1; i >= wpos2; i--) {\n wvalue2 <<= 1;\n if (BN_is_bit_set(p2, i))\n wvalue2++;\n }\n }\n if (wvalue1 && b == wpos1) {\n if (!BN_mod_mul_montgomery(r, r, val1[wvalue1 >> 1], mont, ctx))\n goto err;\n wvalue1 = 0;\n r_is_one = 0;\n }\n if (wvalue2 && b == wpos2) {\n if (!BN_mod_mul_montgomery(r, r, val2[wvalue2 >> 1], mont, ctx))\n goto err;\n wvalue2 = 0;\n r_is_one = 0;\n }\n }\n if (!BN_from_montgomery(rr, r, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n BN_CTX_end(ctx);\n bn_check_top(rr);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG("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}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG("ENTER 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("LEAVE BN_CTX_get()", ctx);\n return ret;\n}', 'int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,\n BN_CTX *ctx)\n{\n return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);\n}', 'int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n BN_MONT_CTX *mont, BN_CTX *ctx)\n{\n int ret = bn_mul_mont_fixed_top(r, a, b, mont, ctx);\n bn_correct_top(r);\n bn_check_top(r);\n return ret;\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_sqr_fixed_top(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int max, al;\n int ret = 0;\n BIGNUM *tmp, *rr;\n bn_check_top(a);\n al = a->top;\n if (al <= 0) {\n r->top = 0;\n r->neg = 0;\n return 1;\n }\n BN_CTX_start(ctx);\n rr = (a != r) ? r : BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (rr == NULL || tmp == NULL)\n goto err;\n max = 2 * al;\n if (bn_wexpand(rr, max) == NULL)\n goto err;\n if (al == 4) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[8];\n bn_sqr_normal(rr->d, a->d, 4, t);\n#else\n bn_sqr_comba4(rr->d, a->d);\n#endif\n } else if (al == 8) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[16];\n bn_sqr_normal(rr->d, a->d, 8, t);\n#else\n bn_sqr_comba8(rr->d, a->d);\n#endif\n } else {\n#if defined(BN_RECURSION)\n if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {\n BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];\n bn_sqr_normal(rr->d, a->d, al, t);\n } else {\n int j, k;\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n if (al == j) {\n if (bn_wexpand(tmp, k * 2) == NULL)\n goto err;\n bn_sqr_recursive(rr->d, a->d, al, tmp->d);\n } else {\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n }\n }\n#else\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n#endif\n }\n rr->neg = 0;\n rr->top = max;\n rr->flags |= BN_FLG_FIXED_TOP;\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(rr);\n bn_check_top(tmp);\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_end()", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG("LEAVE BN_CTX_end()", ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}'] |
35,402 | 0 | https://github.com/openssl/openssl/blob/55442b8a5b719f54578083fae0fcc814b599cd84/crypto/bn/bn_lib.c/#L921 | void bn_correct_top(BIGNUM *a)
{
BN_ULONG *ftl;
int tmp_top = a->top;
if (tmp_top > 0) {
for (ftl = &(a->d[tmp_top]); tmp_top > 0; tmp_top--) {
ftl--;
if (*ftl != 0)
break;
}
a->top = tmp_top;
}
if (a->top == 0)
a->neg = 0;
bn_pollute(a);
} | ['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 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(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 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 || !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_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)\n{\n if (group->order == NULL)\n return 0;\n if (!BN_copy(order, group->order))\n return 0;\n return !BN_is_zero(order);\n}', 'int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = 0;\n int top, al, bl;\n BIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n int i;\n#endif\n#ifdef BN_RECURSION\n BIGNUM *t = NULL;\n int j = 0, k;\n#endif\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(r);\n al = a->top;\n bl = b->top;\n if ((al == 0) || (bl == 0)) {\n BN_zero(r);\n return 1;\n }\n top = al + bl;\n BN_CTX_start(ctx);\n if ((r == a) || (r == b)) {\n if ((rr = BN_CTX_get(ctx)) == NULL)\n goto err;\n } else\n rr = r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n i = al - bl;\n#endif\n#ifdef BN_MUL_COMBA\n if (i == 0) {\n# if 0\n if (al == 4) {\n if (bn_wexpand(rr, 8) == NULL)\n goto err;\n rr->top = 8;\n bn_mul_comba4(rr->d, a->d, b->d);\n goto end;\n }\n# endif\n if (al == 8) {\n if (bn_wexpand(rr, 16) == NULL)\n goto err;\n rr->top = 16;\n bn_mul_comba8(rr->d, a->d, b->d);\n goto end;\n }\n }\n#endif\n#ifdef BN_RECURSION\n if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {\n if (i >= -1 && i <= 1) {\n if (i >= 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n }\n if (i == -1) {\n j = BN_num_bits_word((BN_ULONG)bl);\n }\n j = 1 << (j - 1);\n assert(j <= al || j <= bl);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (al > j || bl > j) {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d,\n j, al - j, bl - j, t->d);\n } else {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);\n }\n rr->top = top;\n goto end;\n }\n }\n#endif\n if (bn_wexpand(rr, top) == NULL)\n goto err;\n rr->top = top;\n bn_mul_normal(rr->d, a->d, al, b->d, bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n end:\n#endif\n rr->neg = a->neg ^ b->neg;\n bn_correct_top(rr);\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(r);\n BN_CTX_end(ctx);\n return ret;\n}', 'void bn_correct_top(BIGNUM *a)\n{\n BN_ULONG *ftl;\n int tmp_top = a->top;\n if (tmp_top > 0) {\n for (ftl = &(a->d[tmp_top]); tmp_top > 0; tmp_top--) {\n ftl--;\n if (*ftl != 0)\n break;\n }\n a->top = tmp_top;\n }\n if (a->top == 0)\n a->neg = 0;\n bn_pollute(a);\n}'] |
35,403 | 0 | https://github.com/openssl/openssl/blob/183733f882056ea3e6fe95e665b85fcc6a45dcb4/crypto/x509/x509_att.c/#L323 | int X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE *attr, int attrtype,
const void *data, int len)
{
ASN1_TYPE *ttmp;
ASN1_STRING *stmp = NULL;
int atype = 0;
if (!attr)
return 0;
if (attrtype & MBSTRING_FLAG) {
stmp = ASN1_STRING_set_by_NID(NULL, data, len, attrtype,
OBJ_obj2nid(attr->object));
if (!stmp) {
X509err(X509_F_X509_ATTRIBUTE_SET1_DATA, ERR_R_ASN1_LIB);
return 0;
}
atype = stmp->type;
} else if (len != -1) {
if ((stmp = ASN1_STRING_type_new(attrtype)) == NULL)
goto err;
if (!ASN1_STRING_set(stmp, data, len))
goto err;
atype = attrtype;
}
if (attrtype == 0)
return 1;
if ((ttmp = ASN1_TYPE_new()) == NULL)
goto err;
if ((len == -1) && !(attrtype & MBSTRING_FLAG)) {
if (!ASN1_TYPE_set1(ttmp, attrtype, data))
goto err;
} else
ASN1_TYPE_set(ttmp, atype, stmp);
if (!sk_ASN1_TYPE_push(attr->set, ttmp))
goto err;
return 1;
err:
X509err(X509_F_X509_ATTRIBUTE_SET1_DATA, ERR_R_MALLOC_FAILURE);
return 0;
} | ['int X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE *attr, int attrtype,\n const void *data, int len)\n{\n ASN1_TYPE *ttmp;\n ASN1_STRING *stmp = NULL;\n int atype = 0;\n if (!attr)\n return 0;\n if (attrtype & MBSTRING_FLAG) {\n stmp = ASN1_STRING_set_by_NID(NULL, data, len, attrtype,\n OBJ_obj2nid(attr->object));\n if (!stmp) {\n X509err(X509_F_X509_ATTRIBUTE_SET1_DATA, ERR_R_ASN1_LIB);\n return 0;\n }\n atype = stmp->type;\n } else if (len != -1) {\n if ((stmp = ASN1_STRING_type_new(attrtype)) == NULL)\n goto err;\n if (!ASN1_STRING_set(stmp, data, len))\n goto err;\n atype = attrtype;\n }\n if (attrtype == 0)\n return 1;\n if ((ttmp = ASN1_TYPE_new()) == NULL)\n goto err;\n if ((len == -1) && !(attrtype & MBSTRING_FLAG)) {\n if (!ASN1_TYPE_set1(ttmp, attrtype, data))\n goto err;\n } else\n ASN1_TYPE_set(ttmp, atype, stmp);\n if (!sk_ASN1_TYPE_push(attr->set, ttmp))\n goto err;\n return 1;\n err:\n X509err(X509_F_X509_ATTRIBUTE_SET1_DATA, ERR_R_MALLOC_FAILURE);\n return 0;\n}', 'ASN1_STRING *ASN1_STRING_type_new(int type)\n{\n ASN1_STRING *ret;\n ret = OPENSSL_zalloc(sizeof(*ret));\n if (ret == NULL) {\n ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n ret->type = type;\n return (ret);\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (num <= 0)\n return NULL;\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)file;\n (void)line;\n ret = malloc(num);\n#endif\n#ifndef OPENSSL_CPUID_OBJ\n if (ret && (num > 2048)) {\n extern unsigned char cleanse_ctr;\n ((unsigned char *)ret)[0] = cleanse_ctr;\n }\n#endif\n return ret;\n}', "int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len)\n{\n unsigned char *c;\n const char *data = _data;\n if (len < 0) {\n if (data == NULL)\n return (0);\n else\n len = strlen(data);\n }\n if ((str->length < len) || (str->data == NULL)) {\n c = str->data;\n str->data = OPENSSL_realloc(c, len + 1);\n if (str->data == NULL) {\n ASN1err(ASN1_F_ASN1_STRING_SET, ERR_R_MALLOC_FAILURE);\n str->data = c;\n return (0);\n }\n }\n str->length = len;\n if (data != NULL) {\n memcpy(str->data, data, len);\n str->data[len] = '\\0';\n }\n return (1);\n}", 'void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)\n{\n if (str == NULL)\n return CRYPTO_malloc(num, file, line);\n if (num == 0) {\n CRYPTO_free(str);\n return NULL;\n }\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n void *ret;\n CRYPTO_mem_debug_realloc(str, NULL, num, 0, file, line);\n ret = realloc(str, num);\n CRYPTO_mem_debug_realloc(str, ret, num, 1, file, line);\n return ret;\n }\n#else\n (void)file;\n (void)line;\n#endif\n return realloc(str, num);\n}'] |
35,404 | 0 | https://github.com/libav/libav/blob/e5b0fc170f85b00f7dd0ac514918fb5c95253d39/libavcodec/bitstream.h/#L139 | static inline uint64_t get_val(BitstreamContext *bc, unsigned n)
{
#ifdef BITSTREAM_READER_LE
uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);
bc->bits >>= n;
#else
uint64_t ret = bc->bits >> (64 - n);
bc->bits <<= n;
#endif
bc->bits_left -= n;
return ret;
} | ['static inline void decode_block_intra(MadContext *s, int16_t * block)\n{\n int level, i, j, run;\n RLTable *rl = &ff_rl_mpeg1;\n const uint8_t *scantable = s->scantable.permutated;\n int16_t *quant_matrix = s->quant_matrix;\n block[0] = (128 + bitstream_read_signed(&s->bc, 8)) * quant_matrix[0];\n i = 0;\n {\n for (;;) {\n BITSTREAM_RL_VLC(level, run, &s->bc, rl->rl_vlc[0], TEX_VLC_BITS, 2);\n if (level == 127) {\n break;\n } else if (level != 0) {\n i += run;\n if (i > 63) {\n av_log(s->avctx, AV_LOG_ERROR,\n "ac-tex damaged at %d %d\\n", s->mb_x, s->mb_y);\n return;\n }\n j = scantable[i];\n level = (level*quant_matrix[j]) >> 4;\n level = (level-1)|1;\n level = bitstream_apply_sign(&s->bc, level);\n } else {\n level = bitstream_read_signed(&s->bc, 10);\n run = bitstream_read(&s->bc, 6) + 1;\n i += run;\n if (i > 63) {\n av_log(s->avctx, AV_LOG_ERROR,\n "ac-tex damaged at %d %d\\n", s->mb_x, s->mb_y);\n return;\n }\n j = scantable[i];\n if (level < 0) {\n level = -level;\n level = (level*quant_matrix[j]) >> 4;\n level = (level-1)|1;\n level = -level;\n } else {\n level = (level*quant_matrix[j]) >> 4;\n level = (level-1)|1;\n }\n }\n block[j] = level;\n }\n }\n}', 'static inline int32_t bitstream_read_signed(BitstreamContext *bc, unsigned n)\n{\n return sign_extend(bitstream_read(bc, n), 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}'] |
35,405 | 0 | https://github.com/libav/libav/blob/47399ccdfd93d337c96c76fbf591f0e3637131ef/libavcodec/bitstream.h/#L236 | static inline void skip_remaining(BitstreamContext *bc, unsigned n)
{
#ifdef BITSTREAM_READER_LE
bc->bits >>= n;
#else
bc->bits <<= n;
#endif
bc->bits_left -= n;
} | ['static int wma_decode_superframe(AVCodecContext *avctx, void *data,\n int *got_frame_ptr, AVPacket *avpkt)\n{\n AVFrame *frame = data;\n const uint8_t *buf = avpkt->data;\n int buf_size = avpkt->size;\n WMACodecContext *s = avctx->priv_data;\n int nb_frames, bit_offset, i, pos, len, ret;\n uint8_t *q;\n float **samples;\n int samples_offset;\n ff_tlog(avctx, "***decode_superframe:\\n");\n if (buf_size == 0) {\n s->last_superframe_len = 0;\n return 0;\n }\n if (buf_size < avctx->block_align) {\n av_log(avctx, AV_LOG_ERROR,\n "Input packet size too small (%d < %d)\\n",\n buf_size, avctx->block_align);\n return AVERROR_INVALIDDATA;\n }\n buf_size = avctx->block_align;\n bitstream_init8(&s->bc, buf, buf_size);\n if (s->use_bit_reservoir) {\n bitstream_skip(&s->bc, 4);\n nb_frames = bitstream_read(&s->bc, 4) - (s->last_superframe_len <= 0);\n } else\n nb_frames = 1;\n frame->nb_samples = nb_frames * s->frame_len;\n if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {\n av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n return ret;\n }\n samples = (float **) frame->extended_data;\n samples_offset = 0;\n if (s->use_bit_reservoir) {\n bit_offset = bitstream_read(&s->bc, s->byte_offset_bits + 3);\n if (bit_offset > bitstream_bits_left(&s->bc)) {\n av_log(avctx, AV_LOG_ERROR,\n "Invalid last frame bit offset %d > buf size %d (%d)\\n",\n bit_offset, bitstream_bits_left(&s->bc), buf_size);\n goto fail;\n }\n if (s->last_superframe_len > 0) {\n if ((s->last_superframe_len + ((bit_offset + 7) >> 3)) >\n MAX_CODED_SUPERFRAME_SIZE)\n goto fail;\n q = s->last_superframe + s->last_superframe_len;\n len = bit_offset;\n while (len > 7) {\n *q++ = bitstream_read(&s->bc, 8);\n len -= 8;\n }\n if (len > 0)\n *q++ = bitstream_read(&s->bc, len) << (8 - len);\n memset(q, 0, AV_INPUT_BUFFER_PADDING_SIZE);\n bitstream_init(&s->bc, s->last_superframe,\n s->last_superframe_len * 8 + bit_offset);\n if (s->last_bitoffset > 0)\n bitstream_skip(&s->bc, s->last_bitoffset);\n if (wma_decode_frame(s, samples, samples_offset) < 0)\n goto fail;\n samples_offset += s->frame_len;\n nb_frames--;\n }\n pos = bit_offset + 4 + 4 + s->byte_offset_bits + 3;\n if (pos >= MAX_CODED_SUPERFRAME_SIZE * 8 || pos > buf_size * 8)\n return AVERROR_INVALIDDATA;\n bitstream_init8(&s->bc, buf + (pos >> 3), buf_size - (pos >> 3));\n len = pos & 7;\n if (len > 0)\n bitstream_skip(&s->bc, len);\n s->reset_block_lengths = 1;\n for (i = 0; i < nb_frames; i++) {\n if (wma_decode_frame(s, samples, samples_offset) < 0)\n goto fail;\n samples_offset += s->frame_len;\n }\n pos = bitstream_tell(&s->bc) +\n ((bit_offset + 4 + 4 + s->byte_offset_bits + 3) & ~7);\n s->last_bitoffset = pos & 7;\n pos >>= 3;\n len = buf_size - pos;\n if (len > MAX_CODED_SUPERFRAME_SIZE || len < 0) {\n av_log(s->avctx, AV_LOG_ERROR, "len %d invalid\\n", len);\n goto fail;\n }\n s->last_superframe_len = len;\n memcpy(s->last_superframe, buf + pos, len);\n } else {\n if (wma_decode_frame(s, samples, samples_offset) < 0)\n goto fail;\n samples_offset += s->frame_len;\n }\n ff_dlog(s->avctx, "%d %d %d %d outbytes:%td eaten:%d\\n",\n s->frame_len_bits, s->block_len_bits, s->frame_len, s->block_len,\n (int8_t *) samples - (int8_t *) data, avctx->block_align);\n *got_frame_ptr = 1;\n return avctx->block_align;\nfail:\n s->last_superframe_len = 0;\n return -1;\n}', 'static inline int bitstream_init8(BitstreamContext *bc, const uint8_t *buffer,\n unsigned byte_size)\n{\n if (byte_size > INT_MAX / 8)\n return AVERROR_INVALIDDATA;\n return bitstream_init(bc, buffer, byte_size * 8);\n}', 'static inline void bitstream_skip(BitstreamContext *bc, unsigned n)\n{\n if (n < bc->bits_left)\n skip_remaining(bc, n);\n else {\n n -= bc->bits_left;\n bc->bits = 0;\n bc->bits_left = 0;\n if (n >= 64) {\n unsigned skip = n / 8;\n n -= skip * 8;\n bc->ptr += skip;\n }\n refill_64(bc);\n if (n)\n skip_remaining(bc, n);\n }\n}', 'static inline void skip_remaining(BitstreamContext *bc, unsigned n)\n{\n#ifdef BITSTREAM_READER_LE\n bc->bits >>= n;\n#else\n bc->bits <<= n;\n#endif\n bc->bits_left -= n;\n}'] |
35,406 | 0 | https://github.com/libav/libav/blob/25b6837f7cacd691b19cbc12b9dad1ce84a318a1/libavcodec/vp8dsp.c/#L346 | NORMAL_LIMIT(8) | ['NORMAL_LIMIT(8)'] |
35,407 | 0 | https://github.com/libav/libav/blob/a6783b8961a0c68b0b76a35f03538778e67c3ec9/libavformat/oggparseflac.c/#L65 | static int
flac_header (AVFormatContext * s, int idx)
{
struct ogg *ogg = s->priv_data;
struct ogg_stream *os = ogg->streams + idx;
AVStream *st = s->streams[idx];
GetBitContext gb;
FLACStreaminfo si;
int mdt;
if (os->buf[os->pstart] == 0xff)
return 0;
init_get_bits(&gb, os->buf + os->pstart, os->psize*8);
skip_bits1(&gb);
mdt = get_bits(&gb, 7);
if (mdt == OGG_FLAC_METADATA_TYPE_STREAMINFO) {
uint8_t *streaminfo_start = os->buf + os->pstart + 5 + 4 + 4 + 4;
skip_bits_long(&gb, 4*8);
if(get_bits(&gb, 8) != 1)
return -1;
skip_bits_long(&gb, 8 + 16);
skip_bits_long(&gb, 4*8);
if (get_bits_long(&gb, 32) != FLAC_STREAMINFO_SIZE)
return -1;
ff_flac_parse_streaminfo(st->codec, &si, streaminfo_start);
st->codec->codec_type = CODEC_TYPE_AUDIO;
st->codec->codec_id = CODEC_ID_FLAC;
st->codec->extradata =
av_malloc(FLAC_STREAMINFO_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
memcpy(st->codec->extradata, streaminfo_start, FLAC_STREAMINFO_SIZE);
st->codec->extradata_size = FLAC_STREAMINFO_SIZE;
st->time_base.num = 1;
st->time_base.den = st->codec->sample_rate;
} else if (mdt == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
vorbis_comment (s, os->buf + os->pstart + 4, os->psize - 4);
}
return 1;
} | ['static int\nflac_header (AVFormatContext * s, int idx)\n{\n struct ogg *ogg = s->priv_data;\n struct ogg_stream *os = ogg->streams + idx;\n AVStream *st = s->streams[idx];\n GetBitContext gb;\n FLACStreaminfo si;\n int mdt;\n if (os->buf[os->pstart] == 0xff)\n return 0;\n init_get_bits(&gb, os->buf + os->pstart, os->psize*8);\n skip_bits1(&gb);\n mdt = get_bits(&gb, 7);\n if (mdt == OGG_FLAC_METADATA_TYPE_STREAMINFO) {\n uint8_t *streaminfo_start = os->buf + os->pstart + 5 + 4 + 4 + 4;\n skip_bits_long(&gb, 4*8);\n if(get_bits(&gb, 8) != 1)\n return -1;\n skip_bits_long(&gb, 8 + 16);\n skip_bits_long(&gb, 4*8);\n if (get_bits_long(&gb, 32) != FLAC_STREAMINFO_SIZE)\n return -1;\n ff_flac_parse_streaminfo(st->codec, &si, streaminfo_start);\n st->codec->codec_type = CODEC_TYPE_AUDIO;\n st->codec->codec_id = CODEC_ID_FLAC;\n st->codec->extradata =\n av_malloc(FLAC_STREAMINFO_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);\n memcpy(st->codec->extradata, streaminfo_start, FLAC_STREAMINFO_SIZE);\n st->codec->extradata_size = FLAC_STREAMINFO_SIZE;\n st->time_base.num = 1;\n st->time_base.den = st->codec->sample_rate;\n } else if (mdt == FLAC_METADATA_TYPE_VORBIS_COMMENT) {\n vorbis_comment (s, os->buf + os->pstart + 4, os->psize - 4);\n }\n return 1;\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 void skip_bits1(GetBitContext *s){\n skip_bits(s, 1);\n}', 'static inline void skip_bits(GetBitContext *s, int n){\n OPEN_READER(re, s)\n UPDATE_CACHE(re, s)\n LAST_SKIP_BITS(re, s, n)\n CLOSE_READER(re, s)\n}', 'static av_always_inline av_const uint32_t bswap_32(uint32_t x)\n{\n x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);\n x= (x>>16) | (x<<16);\n return x;\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 inline void skip_bits_long(GetBitContext *s, int n){\n s->index += n;\n}', 'static inline unsigned int get_bits_long(GetBitContext *s, int n){\n if(n<=17) return get_bits(s, n);\n else{\n#ifdef ALT_BITSTREAM_READER_LE\n int ret= get_bits(s, 16);\n return ret | (get_bits(s, n-16) << 16);\n#else\n int ret= get_bits(s, 16) << (n-16);\n return ret | get_bits(s, n-16);\n#endif\n }\n}', 'void *av_malloc(unsigned int size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-16) )\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size+16);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&15) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr,16,size))\n ptr = NULL;\n#elif HAVE_MEMALIGN\n ptr = memalign(16,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}'] |
35,408 | 0 | https://github.com/openssl/openssl/blob/aa951ef3d745aa0c32b984fd9be2cc21382b97f6/crypto/bn/bn_ctx.c/#L273 | static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
} | ['static int dsa_do_verify(const unsigned char *dgst, int dgst_len,\n DSA_SIG *sig, DSA *dsa)\n{\n BN_CTX *ctx;\n BIGNUM *u1, *u2, *t1;\n BN_MONT_CTX *mont = NULL;\n const BIGNUM *r, *s;\n int ret = -1, i;\n if (!dsa->p || !dsa->q || !dsa->g) {\n DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_MISSING_PARAMETERS);\n return -1;\n }\n i = BN_num_bits(dsa->q);\n if (i != 160 && i != 224 && i != 256) {\n DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_BAD_Q_VALUE);\n return -1;\n }\n if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) {\n DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_MODULUS_TOO_LARGE);\n return -1;\n }\n u1 = BN_new();\n u2 = BN_new();\n t1 = BN_new();\n ctx = BN_CTX_new();\n if (u1 == NULL || u2 == NULL || t1 == NULL || ctx == NULL)\n goto err;\n DSA_SIG_get0(sig, &r, &s);\n if (BN_is_zero(r) || BN_is_negative(r) ||\n BN_ucmp(r, dsa->q) >= 0) {\n ret = 0;\n goto err;\n }\n if (BN_is_zero(s) || BN_is_negative(s) ||\n BN_ucmp(s, dsa->q) >= 0) {\n ret = 0;\n goto err;\n }\n if ((BN_mod_inverse(u2, s, dsa->q, ctx)) == NULL)\n goto err;\n if (dgst_len > (i >> 3))\n dgst_len = (i >> 3);\n if (BN_bin2bn(dgst, dgst_len, u1) == NULL)\n goto err;\n if (!BN_mod_mul(u1, u1, u2, dsa->q, ctx))\n goto err;\n if (!BN_mod_mul(u2, r, u2, dsa->q, ctx))\n goto err;\n if (dsa->flags & DSA_FLAG_CACHE_MONT_P) {\n mont = BN_MONT_CTX_set_locked(&dsa->method_mont_p,\n dsa->lock, dsa->p, ctx);\n if (!mont)\n goto err;\n }\n if (dsa->meth->dsa_mod_exp != NULL) {\n if (!dsa->meth->dsa_mod_exp(dsa, t1, dsa->g, u1, dsa->pub_key, u2,\n dsa->p, ctx, mont))\n goto err;\n } else {\n if (!BN_mod_exp2_mont(t1, dsa->g, u1, dsa->pub_key, u2, dsa->p, ctx,\n mont))\n goto err;\n }\n if (!BN_mod(u1, t1, dsa->q, ctx))\n goto err;\n ret = (BN_ucmp(u1, r) == 0);\n err:\n if (ret < 0)\n DSAerr(DSA_F_DSA_DO_VERIFY, ERR_R_BN_LIB);\n BN_CTX_free(ctx);\n BN_free(u1);\n BN_free(u2);\n BN_free(t1);\n return (ret);\n}', 'BIGNUM *BN_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n{\n BIGNUM *rv;\n int noinv;\n rv = int_bn_mod_inverse(in, a, n, ctx, &noinv);\n if (noinv)\n BNerr(BN_F_BN_MOD_INVERSE, BN_R_NO_INVERSE);\n return rv;\n}', 'BIGNUM *int_bn_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,\n int *pnoinv)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM *ret = NULL;\n int sign;\n if (pnoinv)\n *pnoinv = 0;\n if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {\n return BN_mod_inverse_no_branch(in, a, n, ctx);\n }\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n if (!BN_nnmod(B, B, A, ctx))\n goto err;\n }\n sign = -1;\n if (BN_is_odd(n) && (BN_num_bits(n) <= 2048)) {\n int shift;\n while (!BN_is_zero(B)) {\n shift = 0;\n while (!BN_is_bit_set(B, shift)) {\n shift++;\n if (BN_is_odd(X)) {\n if (!BN_uadd(X, X, n))\n goto err;\n }\n if (!BN_rshift1(X, X))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(B, B, shift))\n goto err;\n }\n shift = 0;\n while (!BN_is_bit_set(A, shift)) {\n shift++;\n if (BN_is_odd(Y)) {\n if (!BN_uadd(Y, Y, n))\n goto err;\n }\n if (!BN_rshift1(Y, Y))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(A, A, shift))\n goto err;\n }\n if (BN_ucmp(B, A) >= 0) {\n if (!BN_uadd(X, X, Y))\n goto err;\n if (!BN_usub(B, B, A))\n goto err;\n } else {\n if (!BN_uadd(Y, Y, X))\n goto err;\n if (!BN_usub(A, A, B))\n goto err;\n }\n }\n } else {\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n if (BN_num_bits(A) == BN_num_bits(B)) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else if (BN_num_bits(A) == BN_num_bits(B) + 1) {\n if (!BN_lshift1(T, B))\n goto err;\n if (BN_ucmp(A, T) < 0) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else {\n if (!BN_sub(M, A, T))\n goto err;\n if (!BN_add(D, T, B))\n goto err;\n if (BN_ucmp(A, D) < 0) {\n if (!BN_set_word(D, 2))\n goto err;\n } else {\n if (!BN_set_word(D, 3))\n goto err;\n if (!BN_sub(M, M, B))\n goto err;\n }\n }\n } else {\n if (!BN_div(D, M, A, B, ctx))\n goto err;\n }\n tmp = A;\n A = B;\n B = M;\n if (BN_is_one(D)) {\n if (!BN_add(tmp, X, Y))\n goto err;\n } else {\n if (BN_is_word(D, 2)) {\n if (!BN_lshift1(tmp, X))\n goto err;\n } else if (BN_is_word(D, 4)) {\n if (!BN_lshift(tmp, X, 2))\n goto err;\n } else if (D->top == 1) {\n if (!BN_copy(tmp, X))\n goto err;\n if (!BN_mul_word(tmp, D->d[0]))\n goto err;\n } else {\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n }\n if (!BN_add(tmp, tmp, Y))\n goto err;\n }\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n if (pnoinv)\n *pnoinv = 1;\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return (ret);\n}', '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_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,\n BN_CTX *ctx)\n{\n BIGNUM *t;\n int ret = 0;\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(m);\n BN_CTX_start(ctx);\n if ((t = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (a == b) {\n if (!BN_sqr(t, a, ctx))\n goto err;\n } else {\n if (!BN_mul(t, a, b, ctx))\n goto err;\n }\n if (!BN_nnmod(r, t, m, ctx))\n goto err;\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return (ret);\n}', 'int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int max, al;\n int ret = 0;\n BIGNUM *tmp, *rr;\n bn_check_top(a);\n al = a->top;\n if (al <= 0) {\n r->top = 0;\n r->neg = 0;\n return 1;\n }\n BN_CTX_start(ctx);\n rr = (a != r) ? r : BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (!rr || !tmp)\n goto err;\n max = 2 * al;\n if (bn_wexpand(rr, max) == NULL)\n goto err;\n if (al == 4) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[8];\n bn_sqr_normal(rr->d, a->d, 4, t);\n#else\n bn_sqr_comba4(rr->d, a->d);\n#endif\n } else if (al == 8) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[16];\n bn_sqr_normal(rr->d, a->d, 8, t);\n#else\n bn_sqr_comba8(rr->d, a->d);\n#endif\n } else {\n#if defined(BN_RECURSION)\n if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {\n BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];\n bn_sqr_normal(rr->d, a->d, al, t);\n } else {\n int j, k;\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n if (al == j) {\n if (bn_wexpand(tmp, k * 2) == NULL)\n goto err;\n bn_sqr_recursive(rr->d, a->d, al, tmp->d);\n } else {\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n }\n }\n#else\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n#endif\n }\n rr->neg = 0;\n if (a->d[al - 1] == (a->d[al - 1] & BN_MASK2l))\n rr->top = max - 1;\n else\n rr->top = max;\n if (rr != r)\n BN_copy(r, rr);\n ret = 1;\n err:\n bn_check_top(rr);\n bn_check_top(tmp);\n BN_CTX_end(ctx);\n return (ret);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}'] |
35,409 | 0 | https://github.com/libav/libav/blob/47399ccdfd93d337c96c76fbf591f0e3637131ef/libavcodec/bitstream.h/#L236 | static inline void skip_remaining(BitstreamContext *bc, unsigned n)
{
#ifdef BITSTREAM_READER_LE
bc->bits >>= n;
#else
bc->bits <<= n;
#endif
bc->bits_left -= n;
} | ['static int h261_resync(H261Context *h)\n{\n MpegEncContext *const s = &h->s;\n int left, ret;\n if (h->gob_start_code_skipped) {\n ret = h261_decode_gob_header(h);\n if (ret >= 0)\n return 0;\n } else {\n if (bitstream_peek(&s->bc, 15) == 0) {\n ret = h261_decode_gob_header(h);\n if (ret >= 0)\n return 0;\n }\n s->bc = s->last_resync_bc;\n bitstream_align(&s->bc);\n left = bitstream_bits_left(&s->bc);\n for (; left > 15 + 1 + 4 + 5; left -= 8) {\n if (bitstream_peek(&s->bc, 15) == 0) {\n BitstreamContext bak = s->bc;\n ret = h261_decode_gob_header(h);\n if (ret >= 0)\n return 0;\n s->bc = bak;\n }\n bitstream_skip(&s->bc, 8);\n }\n }\n return -1;\n}', 'static inline const uint8_t *bitstream_align(BitstreamContext *bc)\n{\n unsigned n = -bitstream_tell(bc) & 7;\n if (n)\n bitstream_skip(bc, n);\n return bc->buffer + (bitstream_tell(bc) >> 3);\n}', 'static inline unsigned bitstream_peek(BitstreamContext *bc, unsigned n)\n{\n if (n > bc->bits_left)\n refill_32(bc);\n return show_val(bc, n);\n}', 'static int h261_decode_gob_header(H261Context *h)\n{\n unsigned int val;\n MpegEncContext *const s = &h->s;\n if (!h->gob_start_code_skipped) {\n val = bitstream_peek(&s->bc, 15);\n if (val)\n return -1;\n bitstream_skip(&s->bc, 16);\n }\n h->gob_start_code_skipped = 0;\n h->gob_number = bitstream_read(&s->bc, 4);\n s->qscale = bitstream_read(&s->bc, 5);\n if (s->mb_height == 18) {\n if ((h->gob_number <= 0) || (h->gob_number > 12))\n return -1;\n } else {\n if ((h->gob_number != 1) && (h->gob_number != 3) &&\n (h->gob_number != 5))\n return -1;\n }\n while (bitstream_read_bit(&s->bc) != 0)\n bitstream_skip(&s->bc, 8);\n if (s->qscale == 0) {\n av_log(s->avctx, AV_LOG_ERROR, "qscale has forbidden 0 value\\n");\n if (s->avctx->err_recognition & AV_EF_BITSTREAM)\n return -1;\n }\n h->current_mba = 0;\n h->mba_diff = 0;\n return 0;\n}', 'static inline void bitstream_skip(BitstreamContext *bc, unsigned n)\n{\n if (n < bc->bits_left)\n skip_remaining(bc, n);\n else {\n n -= bc->bits_left;\n bc->bits = 0;\n bc->bits_left = 0;\n if (n >= 64) {\n unsigned skip = n / 8;\n n -= skip * 8;\n bc->ptr += skip;\n }\n refill_64(bc);\n if (n)\n skip_remaining(bc, n);\n }\n}', 'static inline void skip_remaining(BitstreamContext *bc, unsigned n)\n{\n#ifdef BITSTREAM_READER_LE\n bc->bits >>= n;\n#else\n bc->bits <<= n;\n#endif\n bc->bits_left -= n;\n}'] |
35,410 | 0 | https://github.com/openssl/openssl/blob/33af4421f2ae5e4d0da3a121f51820f4b49a724c/engines/e_4758cca.c/#L640 | static int cca_rsa_verify(int type, const unsigned char *m, unsigned int m_len,
unsigned char *sigbuf, unsigned int siglen, const RSA *rsa)
{
long returnCode;
long reasonCode;
long lsiglen = siglen;
long exitDataLength = 0;
unsigned char exitData[8];
long ruleArrayLength = 1;
unsigned char ruleArray[8] = "PKCS-1.1";
long keyTokenLength;
unsigned char* keyToken = (unsigned char*)RSA_get_ex_data(rsa, hndidx);
long length = SSL_SIG_LEN;
long keyLength ;
unsigned char *hashBuffer = NULL;
X509_SIG sig;
ASN1_TYPE parameter;
X509_ALGOR algorithm;
ASN1_OCTET_STRING digest;
keyTokenLength = *(long*)keyToken;
keyToken+=sizeof(long);
if (type == NID_md5 || type == NID_sha1)
{
sig.algor = &algorithm;
algorithm.algorithm = OBJ_nid2obj(type);
if (!algorithm.algorithm)
{
CCA4758err(CCA4758_F_CCA_RSA_VERIFY,
CCA4758_R_UNKNOWN_ALGORITHM_TYPE);
return 0;
}
if (!algorithm.algorithm->length)
{
CCA4758err(CCA4758_F_CCA_RSA_VERIFY,
CCA4758_R_ASN1_OID_UNKNOWN_FOR_MD);
return 0;
}
parameter.type = V_ASN1_NULL;
parameter.value.ptr = NULL;
algorithm.parameter = ¶meter;
sig.digest = &digest;
sig.digest->data = (unsigned char*)m;
sig.digest->length = m_len;
length = i2d_X509_SIG(&sig, NULL);
}
keyLength = RSA_size(rsa);
if (length - RSA_PKCS1_PADDING > keyLength)
{
CCA4758err(CCA4758_F_CCA_RSA_VERIFY,
CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
return 0;
}
switch (type)
{
case NID_md5_sha1 :
if (m_len != SSL_SIG_LEN)
{
CCA4758err(CCA4758_F_CCA_RSA_VERIFY,
CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
return 0;
}
hashBuffer = (unsigned char *)m;
length = m_len;
break;
case NID_md5 :
{
unsigned char *ptr;
ptr = hashBuffer = OPENSSL_malloc(
(unsigned int)keyLength+1);
if (!hashBuffer)
{
CCA4758err(CCA4758_F_CCA_RSA_VERIFY,
ERR_R_MALLOC_FAILURE);
return 0;
}
i2d_X509_SIG(&sig, &ptr);
}
break;
case NID_sha1 :
{
unsigned char *ptr;
ptr = hashBuffer = OPENSSL_malloc(
(unsigned int)keyLength+1);
if (!hashBuffer)
{
CCA4758err(CCA4758_F_CCA_RSA_VERIFY,
ERR_R_MALLOC_FAILURE);
return 0;
}
i2d_X509_SIG(&sig, &ptr);
}
break;
default:
return 0;
}
digitalSignatureVerify(&returnCode, &reasonCode, &exitDataLength,
exitData, &ruleArrayLength, ruleArray, &keyTokenLength,
keyToken, &length, hashBuffer, &lsiglen, sigbuf);
if (type == NID_sha1 || type == NID_md5)
{
OPENSSL_cleanse(hashBuffer, keyLength+1);
OPENSSL_free(hashBuffer);
}
return ((returnCode || reasonCode) ? 0 : 1);
} | ['static int cca_rsa_verify(int type, const unsigned char *m, unsigned int m_len,\n\t\tunsigned char *sigbuf, unsigned int siglen, const RSA *rsa)\n\t{\n\tlong returnCode;\n\tlong reasonCode;\n\tlong lsiglen = siglen;\n\tlong exitDataLength = 0;\n\tunsigned char exitData[8];\n\tlong ruleArrayLength = 1;\n\tunsigned char ruleArray[8] = "PKCS-1.1";\n\tlong keyTokenLength;\n\tunsigned char* keyToken = (unsigned char*)RSA_get_ex_data(rsa, hndidx);\n\tlong length = SSL_SIG_LEN;\n\tlong keyLength ;\n\tunsigned char *hashBuffer = NULL;\n\tX509_SIG sig;\n\tASN1_TYPE parameter;\n\tX509_ALGOR algorithm;\n\tASN1_OCTET_STRING digest;\n\tkeyTokenLength = *(long*)keyToken;\n\tkeyToken+=sizeof(long);\n\tif (type == NID_md5 || type == NID_sha1)\n\t\t{\n\t\tsig.algor = &algorithm;\n\t\talgorithm.algorithm = OBJ_nid2obj(type);\n\t\tif (!algorithm.algorithm)\n\t\t\t{\n\t\t\tCCA4758err(CCA4758_F_CCA_RSA_VERIFY,\n\t\t\t\tCCA4758_R_UNKNOWN_ALGORITHM_TYPE);\n\t\t\treturn 0;\n\t\t\t}\n\t\tif (!algorithm.algorithm->length)\n\t\t\t{\n\t\t\tCCA4758err(CCA4758_F_CCA_RSA_VERIFY,\n\t\t\t\tCCA4758_R_ASN1_OID_UNKNOWN_FOR_MD);\n\t\t\treturn 0;\n\t\t\t}\n\t\tparameter.type = V_ASN1_NULL;\n\t\tparameter.value.ptr = NULL;\n\t\talgorithm.parameter = ¶meter;\n\t\tsig.digest = &digest;\n\t\tsig.digest->data = (unsigned char*)m;\n\t\tsig.digest->length = m_len;\n\t\tlength = i2d_X509_SIG(&sig, NULL);\n\t\t}\n\tkeyLength = RSA_size(rsa);\n\tif (length - RSA_PKCS1_PADDING > keyLength)\n\t\t{\n\t\tCCA4758err(CCA4758_F_CCA_RSA_VERIFY,\n\t\t\tCCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);\n\t\treturn 0;\n\t\t}\n\tswitch (type)\n\t\t{\n\t\tcase NID_md5_sha1 :\n\t\t\tif (m_len != SSL_SIG_LEN)\n\t\t\t\t{\n\t\t\t\tCCA4758err(CCA4758_F_CCA_RSA_VERIFY,\n\t\t\t\tCCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);\n\t\t\t\treturn 0;\n\t\t\t\t}\n\t\t\thashBuffer = (unsigned char *)m;\n\t\t\tlength = m_len;\n\t\t\tbreak;\n\t\tcase NID_md5 :\n\t\t\t{\n\t\t\tunsigned char *ptr;\n\t\t\tptr = hashBuffer = OPENSSL_malloc(\n\t\t\t\t\t(unsigned int)keyLength+1);\n\t\t\tif (!hashBuffer)\n\t\t\t\t{\n\t\t\t\tCCA4758err(CCA4758_F_CCA_RSA_VERIFY,\n\t\t\t\t\t\tERR_R_MALLOC_FAILURE);\n\t\t\t\treturn 0;\n\t\t\t\t}\n\t\t\ti2d_X509_SIG(&sig, &ptr);\n\t\t\t}\n\t\t\tbreak;\n\t\tcase NID_sha1 :\n\t\t\t{\n\t\t\tunsigned char *ptr;\n\t\t\tptr = hashBuffer = OPENSSL_malloc(\n\t\t\t\t\t(unsigned int)keyLength+1);\n\t\t\tif (!hashBuffer)\n\t\t\t\t{\n\t\t\t\tCCA4758err(CCA4758_F_CCA_RSA_VERIFY,\n\t\t\t\t\t\tERR_R_MALLOC_FAILURE);\n\t\t\t\treturn 0;\n\t\t\t\t}\n\t\t\ti2d_X509_SIG(&sig, &ptr);\n\t\t\t}\n\t\t\tbreak;\n\t\tdefault:\n\t\t\treturn 0;\n\t\t}\n\tdigitalSignatureVerify(&returnCode, &reasonCode, &exitDataLength,\n\t\texitData, &ruleArrayLength, ruleArray, &keyTokenLength,\n\t\tkeyToken, &length, hashBuffer, &lsiglen, sigbuf);\n\tif (type == NID_sha1 || type == NID_md5)\n\t\t{\n\t\tOPENSSL_cleanse(hashBuffer, keyLength+1);\n\t\tOPENSSL_free(hashBuffer);\n\t\t}\n\treturn ((returnCode || reasonCode) ? 0 : 1);\n\t}', 'void *RSA_get_ex_data(const RSA *r, int idx)\n\t{\n\treturn(CRYPTO_get_ex_data(&r->ex_data,idx));\n\t}', 'void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)\n\t{\n\tif (ad->sk == NULL)\n\t\treturn(0);\n\telse if (idx >= sk_num(ad->sk))\n\t\treturn(0);\n\telse\n\t\treturn(sk_value(ad->sk,idx));\n\t}', 'int sk_num(const STACK *st)\n{\n\tif(st == NULL) return -1;\n\treturn st->num;\n}', 'char *sk_value(const STACK *st, int i)\n{\n\tif(!st || (i < 0) || (i >= st->num)) return NULL;\n\treturn st->data[i];\n}'] |
35,411 | 0 | https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/rsa/rsa_ssl.c/#L132 | int RSA_padding_check_SSLv23(unsigned char *to, int tlen,
const unsigned char *from, int flen, int num)
{
int i, j, k;
const unsigned char *p;
p = from;
if (flen < 10) {
RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, RSA_R_DATA_TOO_SMALL);
return (-1);
}
if ((num != (flen + 1)) || (*(p++) != 02)) {
RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, RSA_R_BLOCK_TYPE_IS_NOT_02);
return (-1);
}
j = flen - 1;
for (i = 0; i < j; i++)
if (*(p++) == 0)
break;
if ((i == j) || (i < 8)) {
RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23,
RSA_R_NULL_BEFORE_BLOCK_MISSING);
return (-1);
}
for (k = -9; k < -1; k++) {
if (p[k] != 0x03)
break;
}
if (k == -1) {
RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, RSA_R_SSLV3_ROLLBACK_ATTACK);
return (-1);
}
i++;
j -= i;
if (j > tlen) {
RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, RSA_R_DATA_TOO_LARGE);
return (-1);
}
memcpy(to, p, (unsigned int)j);
return (j);
} | ['static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,\n unsigned char *to, RSA *rsa, int padding)\n{\n BIGNUM *f, *ret;\n int j, num = 0, r = -1;\n unsigned char *p;\n unsigned char *buf = NULL;\n BN_CTX *ctx = NULL;\n int local_blinding = 0;\n BIGNUM *unblind = NULL;\n BN_BLINDING *blinding = NULL;\n if ((ctx = BN_CTX_new()) == NULL)\n goto err;\n BN_CTX_start(ctx);\n f = BN_CTX_get(ctx);\n ret = BN_CTX_get(ctx);\n num = BN_num_bytes(rsa->n);\n buf = OPENSSL_malloc(num);\n if (f == NULL || ret == NULL || buf == NULL) {\n RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (flen > num) {\n RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT,\n RSA_R_DATA_GREATER_THAN_MOD_LEN);\n goto err;\n }\n if (BN_bin2bn(from, (int)flen, f) == NULL)\n goto err;\n if (BN_ucmp(f, rsa->n) >= 0) {\n RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT,\n RSA_R_DATA_TOO_LARGE_FOR_MODULUS);\n goto err;\n }\n if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {\n blinding = rsa_get_blinding(rsa, &local_blinding, ctx);\n if (blinding == NULL) {\n RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n }\n if (blinding != NULL) {\n if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) {\n RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (!rsa_blinding_convert(blinding, f, unblind, ctx))\n goto err;\n }\n if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||\n ((rsa->p != NULL) &&\n (rsa->q != NULL) &&\n (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {\n if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))\n goto err;\n } else {\n BIGNUM *d = NULL, *local_d = NULL;\n if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {\n local_d = d = BN_new();\n if (d == NULL) {\n RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);\n } else {\n d = rsa->d;\n }\n if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)\n if (!BN_MONT_CTX_set_locked\n (&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx)) {\n BN_free(local_d);\n goto err;\n }\n if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,\n rsa->_method_mod_n)) {\n BN_free(local_d);\n goto err;\n }\n BN_free(local_d);\n }\n if (blinding)\n if (!rsa_blinding_invert(blinding, ret, unblind, ctx))\n goto err;\n p = buf;\n j = BN_bn2bin(ret, p);\n switch (padding) {\n case RSA_PKCS1_PADDING:\n r = RSA_padding_check_PKCS1_type_2(to, num, buf, j, num);\n break;\n case RSA_PKCS1_OAEP_PADDING:\n r = RSA_padding_check_PKCS1_OAEP(to, num, buf, j, num, NULL, 0);\n break;\n case RSA_SSLV23_PADDING:\n r = RSA_padding_check_SSLv23(to, num, buf, j, num);\n break;\n case RSA_NO_PADDING:\n r = RSA_padding_check_none(to, num, buf, j, num);\n break;\n default:\n RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, RSA_R_UNKNOWN_PADDING_TYPE);\n goto err;\n }\n if (r < 0)\n RSAerr(RSA_F_RSA_OSSL_PRIVATE_DECRYPT, RSA_R_PADDING_CHECK_FAILED);\n err:\n if (ctx != NULL)\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n OPENSSL_clear_free(buf, num);\n return (r);\n}', 'int BN_num_bits(const BIGNUM *a)\n{\n int i = a->top - 1;\n bn_check_top(a);\n if (BN_is_zero(a))\n return 0;\n return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (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}', 'int RSA_padding_check_SSLv23(unsigned char *to, int tlen,\n const unsigned char *from, int flen, int num)\n{\n int i, j, k;\n const unsigned char *p;\n p = from;\n if (flen < 10) {\n RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, RSA_R_DATA_TOO_SMALL);\n return (-1);\n }\n if ((num != (flen + 1)) || (*(p++) != 02)) {\n RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, RSA_R_BLOCK_TYPE_IS_NOT_02);\n return (-1);\n }\n j = flen - 1;\n for (i = 0; i < j; i++)\n if (*(p++) == 0)\n break;\n if ((i == j) || (i < 8)) {\n RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23,\n RSA_R_NULL_BEFORE_BLOCK_MISSING);\n return (-1);\n }\n for (k = -9; k < -1; k++) {\n if (p[k] != 0x03)\n break;\n }\n if (k == -1) {\n RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, RSA_R_SSLV3_ROLLBACK_ATTACK);\n return (-1);\n }\n i++;\n j -= i;\n if (j > tlen) {\n RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, RSA_R_DATA_TOO_LARGE);\n return (-1);\n }\n memcpy(to, p, (unsigned int)j);\n return (j);\n}'] |
35,412 | 0 | https://github.com/libav/libav/blob/5d7870dc76624e42a747a3c7c6f206c8ed9e9b2e/ffmpeg.c/#L4088 | static void opt_vstats (void)
{
char filename[40];
time_t today2 = time(NULL);
struct tm *today = localtime(&today2);
snprintf(filename, sizeof(filename), "vstats_%02d%02d%02d.log", today->tm_hour, today->tm_min,
today->tm_sec);
opt_vstats_file(filename);
} | ['static void opt_vstats (void)\n{\n char filename[40];\n time_t today2 = time(NULL);\n struct tm *today = localtime(&today2);\n snprintf(filename, sizeof(filename), "vstats_%02d%02d%02d.log", today->tm_hour, today->tm_min,\n today->tm_sec);\n opt_vstats_file(filename);\n}'] |
35,413 | 0 | https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/h264pred.c/#L318 | static void pred4x4_vertical_left_rv40_c(uint8_t *src, uint8_t *topright, int stride){
LOAD_LEFT_EDGE
LOAD_DOWN_LEFT_EDGE
pred4x4_vertical_left_rv40(src, topright, stride, l0, l1, l2, l3, l4);
} | ['static void pred4x4_vertical_left_rv40_c(uint8_t *src, uint8_t *topright, int stride){\n LOAD_LEFT_EDGE\n LOAD_DOWN_LEFT_EDGE\n pred4x4_vertical_left_rv40(src, topright, stride, l0, l1, l2, l3, l4);\n}'] |
35,414 | 0 | https://github.com/libav/libav/blob/640d5f1c801061844394813c78ea449e5826f6e5/libavcodec/nellymoser.c/#L221 | void ff_nelly_get_sample_bits(const float *buf, int *bits)
{
int i, j;
short sbuf[128];
int bitsum = 0, last_bitsum, small_bitsum, big_bitsum;
short shift, shift_saved;
int max, sum, last_off, tmp;
int big_off, small_off;
int off;
max = 0;
for (i = 0; i < NELLY_FILL_LEN; i++) {
max = FFMAX(max, buf[i]);
}
shift = -16;
shift += headroom(&max);
sum = 0;
for (i = 0; i < NELLY_FILL_LEN; i++) {
sbuf[i] = signed_shift(buf[i], shift);
sbuf[i] = (3*sbuf[i])>>2;
sum += sbuf[i];
}
shift += 11;
shift_saved = shift;
sum -= NELLY_DETAIL_BITS << shift;
shift += headroom(&sum);
small_off = (NELLY_BASE_OFF * (sum>>16)) >> 15;
shift = shift_saved - (NELLY_BASE_SHIFT+shift-31);
small_off = signed_shift(small_off, shift);
bitsum = sum_bits(sbuf, shift_saved, small_off);
if (bitsum != NELLY_DETAIL_BITS) {
off = bitsum - NELLY_DETAIL_BITS;
for(shift=0; FFABS(off) <= 16383; shift++)
off *= 2;
off = (off * NELLY_BASE_OFF) >> 15;
shift = shift_saved-(NELLY_BASE_SHIFT+shift-15);
off = signed_shift(off, shift);
for (j = 1; j < 20; j++) {
last_off = small_off;
small_off += off;
last_bitsum = bitsum;
bitsum = sum_bits(sbuf, shift_saved, small_off);
if ((bitsum-NELLY_DETAIL_BITS) * (last_bitsum-NELLY_DETAIL_BITS) <= 0)
break;
}
if (bitsum > NELLY_DETAIL_BITS) {
big_off = small_off;
small_off = last_off;
big_bitsum=bitsum;
small_bitsum=last_bitsum;
} else {
big_off = last_off;
big_bitsum=last_bitsum;
small_bitsum=bitsum;
}
while (bitsum != NELLY_DETAIL_BITS && j <= 19) {
off = (big_off+small_off)>>1;
bitsum = sum_bits(sbuf, shift_saved, off);
if (bitsum > NELLY_DETAIL_BITS) {
big_off=off;
big_bitsum=bitsum;
} else {
small_off = off;
small_bitsum=bitsum;
}
j++;
}
if (abs(big_bitsum-NELLY_DETAIL_BITS) >=
abs(small_bitsum-NELLY_DETAIL_BITS)) {
bitsum = small_bitsum;
} else {
small_off = big_off;
bitsum = big_bitsum;
}
}
for (i = 0; i < NELLY_FILL_LEN; i++) {
tmp = sbuf[i]-small_off;
tmp = ((tmp>>(shift_saved-1))+1)>>1;
bits[i] = av_clip(tmp, 0, NELLY_BIT_CAP);
}
if (bitsum > NELLY_DETAIL_BITS) {
tmp = i = 0;
while (tmp < NELLY_DETAIL_BITS) {
tmp += bits[i];
i++;
}
bits[i-1] -= tmp - NELLY_DETAIL_BITS;
for(; i < NELLY_FILL_LEN; i++)
bits[i] = 0;
}
} | ['static void nelly_decode_block(NellyMoserDecodeContext *s,\n const unsigned char block[NELLY_BLOCK_LEN],\n float audio[NELLY_SAMPLES])\n{\n int i,j;\n float buf[NELLY_FILL_LEN], pows[NELLY_FILL_LEN];\n float *aptr, *bptr, *pptr, val, pval;\n int bits[NELLY_BUF_LEN];\n unsigned char v;\n init_get_bits(&s->gb, block, NELLY_BLOCK_LEN * 8);\n bptr = buf;\n pptr = pows;\n val = ff_nelly_init_table[get_bits(&s->gb, 6)];\n for (i=0 ; i<NELLY_BANDS ; i++) {\n if (i > 0)\n val += ff_nelly_delta_table[get_bits(&s->gb, 5)];\n pval = -pow(2, val/2048) * s->scale_bias;\n for (j = 0; j < ff_nelly_band_sizes_table[i]; j++) {\n *bptr++ = val;\n *pptr++ = pval;\n }\n }\n ff_nelly_get_sample_bits(buf, bits);\n for (i = 0; i < 2; i++) {\n aptr = audio + i * NELLY_BUF_LEN;\n init_get_bits(&s->gb, block, NELLY_BLOCK_LEN * 8);\n skip_bits_long(&s->gb, NELLY_HEADER_BITS + i*NELLY_DETAIL_BITS);\n for (j = 0; j < NELLY_FILL_LEN; j++) {\n if (bits[j] <= 0) {\n aptr[j] = M_SQRT1_2*pows[j];\n if (av_lfg_get(&s->random_state) & 1)\n aptr[j] *= -1.0;\n } else {\n v = get_bits(&s->gb, bits[j]);\n aptr[j] = ff_nelly_dequantization_table[(1<<bits[j])-1+v]*pows[j];\n }\n }\n memset(&aptr[NELLY_FILL_LEN], 0,\n (NELLY_BUF_LEN - NELLY_FILL_LEN) * sizeof(float));\n s->imdct_ctx.imdct_calc(&s->imdct_ctx, s->imdct_out, aptr);\n s->dsp.vector_fmul_reverse(s->state, s->state, ff_sine_128, NELLY_BUF_LEN);\n s->dsp.vector_fmul_add(aptr, s->imdct_out, ff_sine_128, s->state, NELLY_BUF_LEN);\n memcpy(s->state, s->imdct_out + NELLY_BUF_LEN, sizeof(float)*NELLY_BUF_LEN);\n }\n}', 'void ff_nelly_get_sample_bits(const float *buf, int *bits)\n{\n int i, j;\n short sbuf[128];\n int bitsum = 0, last_bitsum, small_bitsum, big_bitsum;\n short shift, shift_saved;\n int max, sum, last_off, tmp;\n int big_off, small_off;\n int off;\n max = 0;\n for (i = 0; i < NELLY_FILL_LEN; i++) {\n max = FFMAX(max, buf[i]);\n }\n shift = -16;\n shift += headroom(&max);\n sum = 0;\n for (i = 0; i < NELLY_FILL_LEN; i++) {\n sbuf[i] = signed_shift(buf[i], shift);\n sbuf[i] = (3*sbuf[i])>>2;\n sum += sbuf[i];\n }\n shift += 11;\n shift_saved = shift;\n sum -= NELLY_DETAIL_BITS << shift;\n shift += headroom(&sum);\n small_off = (NELLY_BASE_OFF * (sum>>16)) >> 15;\n shift = shift_saved - (NELLY_BASE_SHIFT+shift-31);\n small_off = signed_shift(small_off, shift);\n bitsum = sum_bits(sbuf, shift_saved, small_off);\n if (bitsum != NELLY_DETAIL_BITS) {\n off = bitsum - NELLY_DETAIL_BITS;\n for(shift=0; FFABS(off) <= 16383; shift++)\n off *= 2;\n off = (off * NELLY_BASE_OFF) >> 15;\n shift = shift_saved-(NELLY_BASE_SHIFT+shift-15);\n off = signed_shift(off, shift);\n for (j = 1; j < 20; j++) {\n last_off = small_off;\n small_off += off;\n last_bitsum = bitsum;\n bitsum = sum_bits(sbuf, shift_saved, small_off);\n if ((bitsum-NELLY_DETAIL_BITS) * (last_bitsum-NELLY_DETAIL_BITS) <= 0)\n break;\n }\n if (bitsum > NELLY_DETAIL_BITS) {\n big_off = small_off;\n small_off = last_off;\n big_bitsum=bitsum;\n small_bitsum=last_bitsum;\n } else {\n big_off = last_off;\n big_bitsum=last_bitsum;\n small_bitsum=bitsum;\n }\n while (bitsum != NELLY_DETAIL_BITS && j <= 19) {\n off = (big_off+small_off)>>1;\n bitsum = sum_bits(sbuf, shift_saved, off);\n if (bitsum > NELLY_DETAIL_BITS) {\n big_off=off;\n big_bitsum=bitsum;\n } else {\n small_off = off;\n small_bitsum=bitsum;\n }\n j++;\n }\n if (abs(big_bitsum-NELLY_DETAIL_BITS) >=\n abs(small_bitsum-NELLY_DETAIL_BITS)) {\n bitsum = small_bitsum;\n } else {\n small_off = big_off;\n bitsum = big_bitsum;\n }\n }\n for (i = 0; i < NELLY_FILL_LEN; i++) {\n tmp = sbuf[i]-small_off;\n tmp = ((tmp>>(shift_saved-1))+1)>>1;\n bits[i] = av_clip(tmp, 0, NELLY_BIT_CAP);\n }\n if (bitsum > NELLY_DETAIL_BITS) {\n tmp = i = 0;\n while (tmp < NELLY_DETAIL_BITS) {\n tmp += bits[i];\n i++;\n }\n bits[i-1] -= tmp - NELLY_DETAIL_BITS;\n for(; i < NELLY_FILL_LEN; i++)\n bits[i] = 0;\n }\n}'] |
35,415 | 0 | https://github.com/openssl/openssl/blob/f61c5ca6ca183bf0a51651857e3efb02a98889ad/test/handshake_helper.c/#L112 | static int select_server_ctx(SSL *s, void *arg, int ignore)
{
const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
HANDSHAKE_EX_DATA *ex_data =
(HANDSHAKE_EX_DATA*)(SSL_get_ex_data(s, ex_data_idx));
if (servername == NULL) {
ex_data->servername = SSL_TEST_SERVERNAME_SERVER1;
return SSL_TLSEXT_ERR_NOACK;
}
if (strcmp(servername, "server2") == 0) {
SSL_CTX *new_ctx = (SSL_CTX*)arg;
SSL_set_SSL_CTX(s, new_ctx);
SSL_clear_options(s, 0xFFFFFFFFL);
SSL_set_options(s, SSL_CTX_get_options(new_ctx));
ex_data->servername = SSL_TEST_SERVERNAME_SERVER2;
return SSL_TLSEXT_ERR_OK;
} else if (strcmp(servername, "server1") == 0) {
ex_data->servername = SSL_TEST_SERVERNAME_SERVER1;
return SSL_TLSEXT_ERR_OK;
} else if (ignore) {
ex_data->servername = SSL_TEST_SERVERNAME_SERVER1;
return SSL_TLSEXT_ERR_NOACK;
} else {
return SSL_TLSEXT_ERR_ALERT_FATAL;
}
} | ['static int select_server_ctx(SSL *s, void *arg, int ignore)\n{\n const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);\n HANDSHAKE_EX_DATA *ex_data =\n (HANDSHAKE_EX_DATA*)(SSL_get_ex_data(s, ex_data_idx));\n if (servername == NULL) {\n ex_data->servername = SSL_TEST_SERVERNAME_SERVER1;\n return SSL_TLSEXT_ERR_NOACK;\n }\n if (strcmp(servername, "server2") == 0) {\n SSL_CTX *new_ctx = (SSL_CTX*)arg;\n SSL_set_SSL_CTX(s, new_ctx);\n SSL_clear_options(s, 0xFFFFFFFFL);\n SSL_set_options(s, SSL_CTX_get_options(new_ctx));\n ex_data->servername = SSL_TEST_SERVERNAME_SERVER2;\n return SSL_TLSEXT_ERR_OK;\n } else if (strcmp(servername, "server1") == 0) {\n ex_data->servername = SSL_TEST_SERVERNAME_SERVER1;\n return SSL_TLSEXT_ERR_OK;\n } else if (ignore) {\n ex_data->servername = SSL_TEST_SERVERNAME_SERVER1;\n return SSL_TLSEXT_ERR_NOACK;\n } else {\n return SSL_TLSEXT_ERR_ALERT_FATAL;\n }\n}', 'const char *SSL_get_servername(const SSL *s, const int type)\n{\n if (type != TLSEXT_NAMETYPE_host_name)\n return NULL;\n return s->session && !s->ext.hostname ?\n s->session->ext.hostname : s->ext.hostname;\n}', 'void *SSL_get_ex_data(const SSL *s, int idx)\n{\n return (CRYPTO_get_ex_data(&s->ex_data, idx));\n}', 'void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)\n{\n if (ad->sk == NULL || idx >= sk_void_num(ad->sk))\n return NULL;\n return sk_void_value(ad->sk, idx);\n}', 'SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx)\n{\n CERT *new_cert;\n if (ssl->ctx == ctx)\n return ssl->ctx;\n if (ctx == NULL)\n ctx = ssl->initial_ctx;\n new_cert = ssl_cert_dup(ctx->cert);\n if (new_cert == NULL) {\n return NULL;\n }\n ssl_cert_free(ssl->cert);\n ssl->cert = new_cert;\n OPENSSL_assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));\n if ((ssl->ctx != NULL) &&\n (ssl->sid_ctx_length == ssl->ctx->sid_ctx_length) &&\n (memcmp(ssl->sid_ctx, ssl->ctx->sid_ctx, ssl->sid_ctx_length) == 0)) {\n ssl->sid_ctx_length = ctx->sid_ctx_length;\n memcpy(&ssl->sid_ctx, &ctx->sid_ctx, sizeof(ssl->sid_ctx));\n }\n SSL_CTX_up_ref(ctx);\n SSL_CTX_free(ssl->ctx);\n ssl->ctx = ctx;\n return ssl->ctx;\n}', 'unsigned long SSL_clear_options(SSL *s, unsigned long op)\n{\n return s->options &= ~op;\n}', 'unsigned long SSL_CTX_get_options(const SSL_CTX *ctx)\n{\n return ctx->options;\n}', 'unsigned long SSL_set_options(SSL *s, unsigned long op)\n{\n return s->options |= op;\n}'] |
35,416 | 0 | https://github.com/openssl/openssl/blob/8d9fb8c8dbdaad8c7e6009c96618b17aac9662b9/ssl/ssl_lib.c/#L635 | SSL *SSL_new(SSL_CTX *ctx)
{
SSL *s;
if (ctx == NULL) {
SSLerr(SSL_F_SSL_NEW, SSL_R_NULL_SSL_CTX);
return (NULL);
}
if (ctx->method == NULL) {
SSLerr(SSL_F_SSL_NEW, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
return (NULL);
}
s = OPENSSL_zalloc(sizeof(*s));
if (s == NULL)
goto err;
s->lock = CRYPTO_THREAD_lock_new();
if (s->lock == NULL) {
SSLerr(SSL_F_SSL_NEW, ERR_R_MALLOC_FAILURE);
OPENSSL_free(s);
return NULL;
}
RECORD_LAYER_init(&s->rlayer, s);
s->options = ctx->options;
s->min_proto_version = ctx->min_proto_version;
s->max_proto_version = ctx->max_proto_version;
s->mode = ctx->mode;
s->max_cert_list = ctx->max_cert_list;
s->references = 1;
s->cert = ssl_cert_dup(ctx->cert);
if (s->cert == NULL)
goto err;
RECORD_LAYER_set_read_ahead(&s->rlayer, ctx->read_ahead);
s->msg_callback = ctx->msg_callback;
s->msg_callback_arg = ctx->msg_callback_arg;
s->verify_mode = ctx->verify_mode;
s->not_resumable_session_cb = ctx->not_resumable_session_cb;
s->sid_ctx_length = ctx->sid_ctx_length;
OPENSSL_assert(s->sid_ctx_length <= sizeof s->sid_ctx);
memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
s->verify_callback = ctx->default_verify_callback;
s->generate_session_id = ctx->generate_session_id;
s->param = X509_VERIFY_PARAM_new();
if (s->param == NULL)
goto err;
X509_VERIFY_PARAM_inherit(s->param, ctx->param);
s->quiet_shutdown = ctx->quiet_shutdown;
s->max_send_fragment = ctx->max_send_fragment;
s->split_send_fragment = ctx->split_send_fragment;
s->max_pipelines = ctx->max_pipelines;
if (s->max_pipelines > 1)
RECORD_LAYER_set_read_ahead(&s->rlayer, 1);
if (ctx->default_read_buf_len > 0)
SSL_set_default_read_buffer_len(s, ctx->default_read_buf_len);
SSL_CTX_up_ref(ctx);
s->ctx = ctx;
s->tlsext_debug_cb = 0;
s->tlsext_debug_arg = NULL;
s->tlsext_ticket_expected = 0;
s->tlsext_status_type = -1;
s->tlsext_status_expected = 0;
s->tlsext_ocsp_ids = NULL;
s->tlsext_ocsp_exts = NULL;
s->tlsext_ocsp_resp = NULL;
s->tlsext_ocsp_resplen = -1;
SSL_CTX_up_ref(ctx);
s->initial_ctx = ctx;
# ifndef OPENSSL_NO_EC
if (ctx->tlsext_ecpointformatlist) {
s->tlsext_ecpointformatlist =
OPENSSL_memdup(ctx->tlsext_ecpointformatlist,
ctx->tlsext_ecpointformatlist_length);
if (!s->tlsext_ecpointformatlist)
goto err;
s->tlsext_ecpointformatlist_length =
ctx->tlsext_ecpointformatlist_length;
}
if (ctx->tlsext_ellipticcurvelist) {
s->tlsext_ellipticcurvelist =
OPENSSL_memdup(ctx->tlsext_ellipticcurvelist,
ctx->tlsext_ellipticcurvelist_length);
if (!s->tlsext_ellipticcurvelist)
goto err;
s->tlsext_ellipticcurvelist_length =
ctx->tlsext_ellipticcurvelist_length;
}
# endif
# ifndef OPENSSL_NO_NEXTPROTONEG
s->next_proto_negotiated = NULL;
# endif
if (s->ctx->alpn_client_proto_list) {
s->alpn_client_proto_list =
OPENSSL_malloc(s->ctx->alpn_client_proto_list_len);
if (s->alpn_client_proto_list == NULL)
goto err;
memcpy(s->alpn_client_proto_list, s->ctx->alpn_client_proto_list,
s->ctx->alpn_client_proto_list_len);
s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
}
s->verified_chain = NULL;
s->verify_result = X509_V_OK;
s->default_passwd_callback = ctx->default_passwd_callback;
s->default_passwd_callback_userdata = ctx->default_passwd_callback_userdata;
s->method = ctx->method;
if (!s->method->ssl_new(s))
goto err;
s->server = (ctx->method->ssl_accept == ssl_undefined_function) ? 0 : 1;
if (!SSL_clear(s))
goto err;
CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
#ifndef OPENSSL_NO_PSK
s->psk_client_callback = ctx->psk_client_callback;
s->psk_server_callback = ctx->psk_server_callback;
#endif
s->job = NULL;
#ifndef OPENSSL_NO_CT
if (!SSL_set_ct_validation_callback(s, ctx->ct_validation_callback,
ctx->ct_validation_callback_arg))
goto err;
#endif
return s;
err:
SSL_free(s);
SSLerr(SSL_F_SSL_NEW, ERR_R_MALLOC_FAILURE);
return NULL;
} | ['SSL *SSL_new(SSL_CTX *ctx)\n{\n SSL *s;\n if (ctx == NULL) {\n SSLerr(SSL_F_SSL_NEW, SSL_R_NULL_SSL_CTX);\n return (NULL);\n }\n if (ctx->method == NULL) {\n SSLerr(SSL_F_SSL_NEW, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);\n return (NULL);\n }\n s = OPENSSL_zalloc(sizeof(*s));\n if (s == NULL)\n goto err;\n s->lock = CRYPTO_THREAD_lock_new();\n if (s->lock == NULL) {\n SSLerr(SSL_F_SSL_NEW, ERR_R_MALLOC_FAILURE);\n OPENSSL_free(s);\n return NULL;\n }\n RECORD_LAYER_init(&s->rlayer, s);\n s->options = ctx->options;\n s->min_proto_version = ctx->min_proto_version;\n s->max_proto_version = ctx->max_proto_version;\n s->mode = ctx->mode;\n s->max_cert_list = ctx->max_cert_list;\n s->references = 1;\n s->cert = ssl_cert_dup(ctx->cert);\n if (s->cert == NULL)\n goto err;\n RECORD_LAYER_set_read_ahead(&s->rlayer, ctx->read_ahead);\n s->msg_callback = ctx->msg_callback;\n s->msg_callback_arg = ctx->msg_callback_arg;\n s->verify_mode = ctx->verify_mode;\n s->not_resumable_session_cb = ctx->not_resumable_session_cb;\n s->sid_ctx_length = ctx->sid_ctx_length;\n OPENSSL_assert(s->sid_ctx_length <= sizeof s->sid_ctx);\n memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));\n s->verify_callback = ctx->default_verify_callback;\n s->generate_session_id = ctx->generate_session_id;\n s->param = X509_VERIFY_PARAM_new();\n if (s->param == NULL)\n goto err;\n X509_VERIFY_PARAM_inherit(s->param, ctx->param);\n s->quiet_shutdown = ctx->quiet_shutdown;\n s->max_send_fragment = ctx->max_send_fragment;\n s->split_send_fragment = ctx->split_send_fragment;\n s->max_pipelines = ctx->max_pipelines;\n if (s->max_pipelines > 1)\n RECORD_LAYER_set_read_ahead(&s->rlayer, 1);\n if (ctx->default_read_buf_len > 0)\n SSL_set_default_read_buffer_len(s, ctx->default_read_buf_len);\n SSL_CTX_up_ref(ctx);\n s->ctx = ctx;\n s->tlsext_debug_cb = 0;\n s->tlsext_debug_arg = NULL;\n s->tlsext_ticket_expected = 0;\n s->tlsext_status_type = -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 SSL_CTX_up_ref(ctx);\n s->initial_ctx = ctx;\n# ifndef OPENSSL_NO_EC\n if (ctx->tlsext_ecpointformatlist) {\n s->tlsext_ecpointformatlist =\n OPENSSL_memdup(ctx->tlsext_ecpointformatlist,\n ctx->tlsext_ecpointformatlist_length);\n if (!s->tlsext_ecpointformatlist)\n goto err;\n s->tlsext_ecpointformatlist_length =\n ctx->tlsext_ecpointformatlist_length;\n }\n if (ctx->tlsext_ellipticcurvelist) {\n s->tlsext_ellipticcurvelist =\n OPENSSL_memdup(ctx->tlsext_ellipticcurvelist,\n ctx->tlsext_ellipticcurvelist_length);\n if (!s->tlsext_ellipticcurvelist)\n goto err;\n s->tlsext_ellipticcurvelist_length =\n ctx->tlsext_ellipticcurvelist_length;\n }\n# endif\n# ifndef OPENSSL_NO_NEXTPROTONEG\n s->next_proto_negotiated = NULL;\n# endif\n if (s->ctx->alpn_client_proto_list) {\n s->alpn_client_proto_list =\n OPENSSL_malloc(s->ctx->alpn_client_proto_list_len);\n if (s->alpn_client_proto_list == NULL)\n goto err;\n memcpy(s->alpn_client_proto_list, s->ctx->alpn_client_proto_list,\n s->ctx->alpn_client_proto_list_len);\n s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;\n }\n s->verified_chain = NULL;\n s->verify_result = X509_V_OK;\n s->default_passwd_callback = ctx->default_passwd_callback;\n s->default_passwd_callback_userdata = ctx->default_passwd_callback_userdata;\n s->method = ctx->method;\n if (!s->method->ssl_new(s))\n goto err;\n s->server = (ctx->method->ssl_accept == ssl_undefined_function) ? 0 : 1;\n if (!SSL_clear(s))\n goto err;\n 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 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 *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num <= 0)\n return NULL;\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n osslargused(file); osslargused(line);\n ret = malloc(num);\n#endif\n return ret;\n}', 'CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)\n{\n CRYPTO_RWLOCK *lock = OPENSSL_zalloc(sizeof(pthread_rwlock_t));\n if (lock == NULL)\n return NULL;\n if (pthread_rwlock_init(lock, NULL) != 0) {\n OPENSSL_free(lock);\n return NULL;\n }\n return lock;\n}'] |
35,417 | 0 | https://github.com/openssl/openssl/blob/f9df0a7775f483c175cda5832360cccd1db6943a/crypto/bn/bn_sqr.c/#L120 | void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)
{
int i, j, max;
const BN_ULONG *ap;
BN_ULONG *rp;
max = n * 2;
ap = a;
rp = r;
rp[0] = rp[max - 1] = 0;
rp++;
j = n;
if (--j > 0) {
ap++;
rp[j] = bn_mul_words(rp, ap, j, ap[-1]);
rp += 2;
}
for (i = n - 2; i > 0; i--) {
j--;
ap++;
rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);
rp += 2;
}
bn_add_words(r, r, r, max);
bn_sqr_words(tmp, a, n);
bn_add_words(r, r, tmp, max);
} | ['static int test_expmodzero(void)\n{\n BIGNUM *a = NULL, *r = NULL, *zero = NULL;\n int st = 0;\n if (!TEST_ptr(zero = BN_new())\n || !TEST_ptr(a = BN_new())\n || !TEST_ptr(r = BN_new()))\n goto err;\n BN_zero(zero);\n if (!TEST_true(BN_mod_exp(r, a, zero, BN_value_one(), NULL))\n || !TEST_BN_eq_zero(r)\n || !TEST_true(BN_mod_exp_mont(r, a, zero, BN_value_one(),\n NULL, NULL))\n || !TEST_BN_eq_zero(r)\n || !TEST_true(BN_mod_exp_mont_consttime(r, a, zero,\n BN_value_one(),\n NULL, NULL))\n || !TEST_BN_eq_zero(r)\n || !TEST_true(BN_mod_exp_mont_word(r, 42, zero,\n BN_value_one(), NULL, NULL))\n || !TEST_BN_eq_zero(r))\n goto err;\n st = 1;\nerr:\n BN_free(zero);\n BN_free(a);\n BN_free(r);\n return st;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return 1;\n}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n BN_CTX *ctx)\n{\n int ret;\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n if (BN_is_odd(m)) {\n# ifdef MONT_EXP_WORD\n if (a->top == 1 && !a->neg\n && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)) {\n BN_ULONG A = a->d[0];\n ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);\n } else\n# endif\n ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);\n } else\n#endif\n#ifdef RECP_MUL_MOD\n {\n ret = BN_mod_exp_recp(r, a, p, m, ctx);\n }\n#else\n {\n ret = BN_mod_exp_simple(r, a, p, m, ctx);\n }\n#endif\n bn_check_top(r);\n return (ret);\n}', 'int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n int start = 1;\n BIGNUM *d, *r;\n const BIGNUM *aa;\n BIGNUM *val[TABLE_SIZE];\n BN_MONT_CTX *mont = NULL;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0) {\n return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont);\n }\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT, BN_R_CALLED_WITH_EVEN_MODULUS);\n return (0);\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_is_one(m)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n d = BN_CTX_get(ctx);\n r = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (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_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,\n BN_CTX *ctx)\n{\n return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);\n}', 'int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n BN_MONT_CTX *mont, BN_CTX *ctx)\n{\n BIGNUM *tmp;\n int ret = 0;\n#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\n int num = mont->N.top;\n if (num > 1 && a->top == num && b->top == num) {\n if (bn_wexpand(r, num) == NULL)\n return (0);\n if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {\n r->neg = a->neg ^ b->neg;\n r->top = num;\n bn_correct_top(r);\n return 1;\n }\n }\n#endif\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n if (tmp == NULL)\n goto err;\n bn_check_top(tmp);\n if (a == b) {\n if (!BN_sqr(tmp, a, ctx))\n goto err;\n } else {\n if (!BN_mul(tmp, a, b, ctx))\n goto err;\n }\n#ifdef MONT_WORD\n if (!BN_from_montgomery_word(r, tmp, mont))\n goto err;\n#else\n if (!BN_from_montgomery(r, tmp, mont, ctx))\n goto err;\n#endif\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return (ret);\n}', 'int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int max, al;\n int ret = 0;\n BIGNUM *tmp, *rr;\n bn_check_top(a);\n al = a->top;\n if (al <= 0) {\n r->top = 0;\n r->neg = 0;\n return 1;\n }\n BN_CTX_start(ctx);\n rr = (a != r) ? r : BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (rr == NULL || tmp == NULL)\n goto err;\n max = 2 * al;\n if (bn_wexpand(rr, max) == NULL)\n goto err;\n if (al == 4) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[8];\n bn_sqr_normal(rr->d, a->d, 4, t);\n#else\n bn_sqr_comba4(rr->d, a->d);\n#endif\n } else if (al == 8) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[16];\n bn_sqr_normal(rr->d, a->d, 8, t);\n#else\n bn_sqr_comba8(rr->d, a->d);\n#endif\n } else {\n#if defined(BN_RECURSION)\n if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {\n BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];\n bn_sqr_normal(rr->d, a->d, al, t);\n } else {\n int j, k;\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n if (al == j) {\n if (bn_wexpand(tmp, k * 2) == NULL)\n goto err;\n bn_sqr_recursive(rr->d, a->d, al, tmp->d);\n } else {\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n }\n }\n#else\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n#endif\n }\n rr->neg = 0;\n if (a->d[al - 1] == (a->d[al - 1] & BN_MASK2l))\n rr->top = max - 1;\n else\n rr->top = max;\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(rr);\n bn_check_top(tmp);\n BN_CTX_end(ctx);\n return (ret);\n}', 'void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)\n{\n int i, j, max;\n const BN_ULONG *ap;\n BN_ULONG *rp;\n max = n * 2;\n ap = a;\n rp = r;\n rp[0] = rp[max - 1] = 0;\n rp++;\n j = n;\n if (--j > 0) {\n ap++;\n rp[j] = bn_mul_words(rp, ap, j, ap[-1]);\n rp += 2;\n }\n for (i = n - 2; i > 0; i--) {\n j--;\n ap++;\n rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);\n rp += 2;\n }\n bn_add_words(r, r, r, max);\n bn_sqr_words(tmp, a, n);\n bn_add_words(r, r, tmp, max);\n}'] |
35,418 | 0 | https://github.com/openssl/openssl/blob/55442b8a5b719f54578083fae0fcc814b599cd84/crypto/bn/bn_lib.c/#L233 | 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\nint SM2_sig_verify(const EC_KEY *key, const ECDSA_SIG *sig, const BIGNUM *e)\n{\n int ret = 0;\n const EC_GROUP *group = EC_KEY_get0_group(key);\n const BIGNUM *order = EC_GROUP_get0_order(group);\n BN_CTX *ctx = NULL;\n EC_POINT *pt = NULL;\n BIGNUM *t = NULL;\n BIGNUM *x1 = NULL;\n const BIGNUM *r = NULL;\n const BIGNUM *s = NULL;\n ctx = BN_CTX_new();\n if (ctx == NULL)\n goto done;\n pt = EC_POINT_new(group);\n if (pt == NULL)\n goto done;\n BN_CTX_start(ctx);\n t = BN_CTX_get(ctx);\n x1 = BN_CTX_get(ctx);\n if (x1 == NULL)\n goto done;\n ECDSA_SIG_get0(sig, &r, &s);\n if (BN_cmp(r, BN_value_one()) < 0)\n goto done;\n if (BN_cmp(s, BN_value_one()) < 0)\n goto done;\n if (BN_cmp(order, r) <= 0)\n goto done;\n if (BN_cmp(order, s) <= 0)\n goto done;\n if (BN_mod_add(t, r, s, order, ctx) == 0)\n goto done;\n if (BN_is_zero(t) == 1)\n goto done;\n if (EC_POINT_mul(group, pt, s, EC_KEY_get0_public_key(key), t, ctx) == 0)\n goto done;\n if (EC_POINT_get_affine_coordinates_GFp(group, pt, x1, NULL, ctx) == 0)\n goto done;\n if (BN_mod_add(t, e, x1, order, ctx) == 0)\n goto done;\n if (BN_cmp(r, t) == 0)\n ret = 1;\n done:\n EC_POINT_free(pt);\n BN_CTX_free(ctx);\n return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return 1;\n}', 'int BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,\n BN_CTX *ctx)\n{\n if (!BN_add(r, a, b))\n return 0;\n return BN_nnmod(r, r, m, ctx);\n}', 'int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int ret, r_neg, cmp_res;\n bn_check_top(a);\n bn_check_top(b);\n if (a->neg == b->neg) {\n r_neg = a->neg;\n ret = BN_uadd(r, a, b);\n } else {\n cmp_res = BN_ucmp(a, b);\n if (cmp_res > 0) {\n r_neg = a->neg;\n ret = BN_usub(r, a, b);\n } else if (cmp_res < 0) {\n r_neg = b->neg;\n ret = BN_usub(r, b, a);\n } else {\n r_neg = 0;\n BN_zero(r);\n ret = 1;\n }\n }\n r->neg = r_neg;\n bn_check_top(r);\n return ret;\n}', 'int BN_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 bn_check_top(a);\n bn_check_top(b);\n if (a->top < b->top) {\n const BIGNUM *tmp;\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 while (dif) {\n dif--;\n t1 = *(ap++);\n t2 = (t1 + carry) & BN_MASK2;\n *(rp++) = t2;\n carry &= (t2 == 0);\n }\n *rp = carry;\n r->top += carry;\n r->neg = 0;\n bn_check_top(r);\n return 1;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}'] |
35,419 | 0 | https://github.com/libav/libav/blob/d5cc1ed723cfbbf71ea005ce1a2e2f5b55a9f631/ffmpeg.c/#L3574 | static void new_subtitle_stream(AVFormatContext *oc)
{
AVStream *st;
AVCodecContext *subtitle_enc;
st = av_new_stream(oc, oc->nb_streams);
if (!st) {
fprintf(stderr, "Could not alloc stream\n");
av_exit(1);
}
avcodec_get_context_defaults2(st->codec, AVMEDIA_TYPE_SUBTITLE);
bitstream_filters[nb_output_files][oc->nb_streams - 1]= subtitle_bitstream_filters;
subtitle_bitstream_filters= NULL;
subtitle_enc = st->codec;
subtitle_enc->codec_type = AVMEDIA_TYPE_SUBTITLE;
if(subtitle_codec_tag)
subtitle_enc->codec_tag= subtitle_codec_tag;
if (subtitle_stream_copy) {
st->stream_copy = 1;
} else {
set_context_opts(avcodec_opts[AVMEDIA_TYPE_SUBTITLE], subtitle_enc, AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_ENCODING_PARAM);
subtitle_enc->codec_id = find_codec_or_die(subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, 1,
subtitle_enc->strict_std_compliance);
output_codecs[nb_ocodecs] = avcodec_find_encoder_by_name(subtitle_codec_name);
}
nb_ocodecs++;
if (subtitle_language) {
av_metadata_set2(&st->metadata, "language", subtitle_language, 0);
av_freep(&subtitle_language);
}
subtitle_disable = 0;
av_freep(&subtitle_codec_name);
subtitle_stream_copy = 0;
} | ['static void new_subtitle_stream(AVFormatContext *oc)\n{\n AVStream *st;\n AVCodecContext *subtitle_enc;\n st = av_new_stream(oc, oc->nb_streams);\n if (!st) {\n fprintf(stderr, "Could not alloc stream\\n");\n av_exit(1);\n }\n avcodec_get_context_defaults2(st->codec, AVMEDIA_TYPE_SUBTITLE);\n bitstream_filters[nb_output_files][oc->nb_streams - 1]= subtitle_bitstream_filters;\n subtitle_bitstream_filters= NULL;\n subtitle_enc = st->codec;\n subtitle_enc->codec_type = AVMEDIA_TYPE_SUBTITLE;\n if(subtitle_codec_tag)\n subtitle_enc->codec_tag= subtitle_codec_tag;\n if (subtitle_stream_copy) {\n st->stream_copy = 1;\n } else {\n set_context_opts(avcodec_opts[AVMEDIA_TYPE_SUBTITLE], subtitle_enc, AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_ENCODING_PARAM);\n subtitle_enc->codec_id = find_codec_or_die(subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, 1,\n subtitle_enc->strict_std_compliance);\n output_codecs[nb_ocodecs] = avcodec_find_encoder_by_name(subtitle_codec_name);\n }\n nb_ocodecs++;\n if (subtitle_language) {\n av_metadata_set2(&st->metadata, "language", subtitle_language, 0);\n av_freep(&subtitle_language);\n }\n subtitle_disable = 0;\n av_freep(&subtitle_codec_name);\n subtitle_stream_copy = 0;\n}', 'AVStream *av_new_stream(AVFormatContext *s, int id)\n{\n AVStream *st;\n int i;\n if (s->nb_streams >= MAX_STREAMS){\n av_log(s, AV_LOG_ERROR, "Too many streams\\n");\n return NULL;\n }\n st = av_mallocz(sizeof(AVStream));\n if (!st)\n return NULL;\n st->codec= avcodec_alloc_context();\n if (s->iformat) {\n st->codec->bit_rate = 0;\n }\n st->index = s->nb_streams;\n st->id = id;\n st->start_time = AV_NOPTS_VALUE;\n st->duration = AV_NOPTS_VALUE;\n st->cur_dts = 0;\n st->first_dts = AV_NOPTS_VALUE;\n st->probe_packets = MAX_PROBE_PACKETS;\n av_set_pts_info(st, 33, 1, 90000);\n st->last_IP_pts = AV_NOPTS_VALUE;\n for(i=0; i<MAX_REORDER_DELAY+1; i++)\n st->pts_buffer[i]= AV_NOPTS_VALUE;\n st->reference_dts = AV_NOPTS_VALUE;\n st->sample_aspect_ratio = (AVRational){0,1};\n s->streams[s->nb_streams++] = st;\n return st;\n}'] |
35,420 | 0 | https://github.com/openssl/openssl/blob/ed371b8cbac0d0349667558c061c1ae380cf75eb/crypto/bn/bn_lib.c/#L950 | void bn_correct_top(BIGNUM *a)
{
BN_ULONG *ftl;
int tmp_top = a->top;
if (tmp_top > 0) {
for (ftl = &(a->d[tmp_top]); tmp_top > 0; tmp_top--) {
ftl--;
if (*ftl != 0)
break;
}
a->top = tmp_top;
}
if (a->top == 0)
a->neg = 0;
a->flags &= ~BN_FLG_FIXED_TOP;
bn_pollute(a);
} | ['static int test_mod(void)\n{\n BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;\n int st = 0, i;\n if (!TEST_ptr(a = BN_new())\n || !TEST_ptr(b = BN_new())\n || !TEST_ptr(c = BN_new())\n || !TEST_ptr(d = BN_new())\n || !TEST_ptr(e = BN_new()))\n goto err;\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 BN_set_negative(a, rand_neg());\n BN_set_negative(b, rand_neg());\n BN_mod(c, a, b, ctx);\n BN_div(d, e, a, b, ctx);\n BN_sub(e, e, c);\n if (!TEST_BN_eq_zero(e))\n goto err;\n }\n st = 1;\nerr:\n BN_free(a);\n BN_free(b);\n BN_free(c);\n BN_free(d);\n BN_free(e);\n return st;\n}', 'int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom)\n{\n return bnrand(TESTING, rnd, bits, top, bottom);\n}', 'static int bnrand(BNRAND_FLAG flag, BIGNUM *rnd, int bits, int top, int bottom)\n{\n unsigned char *buf = NULL;\n int b, ret = 0, bit, bytes, mask;\n if (bits == 0) {\n if (top != BN_RAND_TOP_ANY || bottom != BN_RAND_BOTTOM_ANY)\n goto toosmall;\n BN_zero(rnd);\n return 1;\n }\n if (bits < 0 || (bits == 1 && top > 0))\n goto toosmall;\n bytes = (bits + 7) / 8;\n bit = (bits - 1) % 8;\n mask = 0xff << (bit + 1);\n buf = OPENSSL_malloc(bytes);\n if (buf == NULL) {\n BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n b = flag == NORMAL ? RAND_bytes(buf, bytes) : RAND_priv_bytes(buf, bytes);\n if (b <= 0)\n goto err;\n if (flag == TESTING) {\n int i;\n unsigned char c;\n for (i = 0; i < bytes; i++) {\n if (RAND_bytes(&c, 1) <= 0)\n goto err;\n if (c >= 128 && i > 0)\n buf[i] = buf[i - 1];\n else if (c < 42)\n buf[i] = 0;\n else if (c < 84)\n buf[i] = 255;\n }\n }\n if (top >= 0) {\n if (top) {\n if (bit == 0) {\n buf[0] = 1;\n buf[1] |= 0x80;\n } else {\n buf[0] |= (3 << (bit - 1));\n }\n } else {\n buf[0] |= (1 << bit);\n }\n }\n buf[0] &= ~mask;\n if (bottom)\n buf[bytes - 1] |= 1;\n if (!BN_bin2bn(buf, bytes, rnd))\n goto err;\n ret = 1;\n err:\n OPENSSL_clear_free(buf, bytes);\n bn_check_top(rnd);\n return ret;\ntoosmall:\n BNerr(BN_F_BNRAND, BN_R_BITS_TOO_SMALL);\n return 0;\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int ret;\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return 0;\n }\n if (divisor->d[divisor->top - 1] == 0) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n ret = bn_div_fixed_top(dv, rm, num, divisor, ctx);\n if (ret) {\n if (dv != NULL)\n bn_correct_top(dv);\n if (rm != NULL)\n bn_correct_top(rm);\n }\n return ret;\n}', 'int bn_div_fixed_top(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,\n const BIGNUM *divisor, BN_CTX *ctx)\n{\n int norm_shift, i, j, loop;\n BIGNUM *tmp, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnum, *wnumtop;\n BN_ULONG d0, d1;\n int num_n, div_n;\n assert(divisor->top > 0 && divisor->d[divisor->top - 1] != 0);\n bn_check_top(num);\n bn_check_top(divisor);\n bn_check_top(dv);\n bn_check_top(rm);\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n if (!BN_copy(sdiv, divisor))\n goto err;\n norm_shift = bn_left_align(sdiv);\n sdiv->neg = 0;\n if (!(bn_lshift_fixed_top(snum, num, norm_shift)))\n goto err;\n div_n = sdiv->top;\n num_n = snum->top;\n if (num_n <= div_n) {\n if (bn_wexpand(snum, div_n + 1) == NULL)\n goto err;\n memset(&(snum->d[num_n]), 0, (div_n - num_n + 1) * sizeof(BN_ULONG));\n snum->top = num_n = div_n + 1;\n }\n loop = num_n - div_n;\n wnum = &(snum->d[loop]);\n wnumtop = &(snum->d[num_n - 1]);\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n if (!bn_wexpand(res, loop))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop;\n res->flags |= BN_FLG_FIXED_TOP;\n resp = &(res->d[loop]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n for (i = 0; i < loop; i++, wnumtop--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W)\n q = bn_div_3_words(wnumtop, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnumtop[0];\n n1 = wnumtop[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n BN_ULONG n2 = (wnumtop == wnum) ? 0 : wnumtop[-2];\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | n2))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= n2)))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum--;\n l0 = bn_sub_words(wnum, wnum, tmp->d, div_n + 1);\n q -= l0;\n for (l0 = 0 - l0, j = 0; j < div_n; j++)\n tmp->d[j] = sdiv->d[j] & l0;\n l0 = bn_add_words(wnum, wnum, tmp->d, div_n);\n (*wnumtop) += l0;\n assert((*wnumtop) == 0);\n *--resp = q;\n }\n snum->neg = num->neg;\n snum->top = div_n;\n snum->flags |= BN_FLG_FIXED_TOP;\n if (rm != NULL)\n bn_rshift_fixed_top(rm, snum, norm_shift);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return 0;\n}', 'void bn_correct_top(BIGNUM *a)\n{\n BN_ULONG *ftl;\n int tmp_top = a->top;\n if (tmp_top > 0) {\n for (ftl = &(a->d[tmp_top]); tmp_top > 0; tmp_top--) {\n ftl--;\n if (*ftl != 0)\n break;\n }\n a->top = tmp_top;\n }\n if (a->top == 0)\n a->neg = 0;\n a->flags &= ~BN_FLG_FIXED_TOP;\n bn_pollute(a);\n}'] |
35,421 | 0 | https://github.com/openssl/openssl/blob/681acb311bb7c68c9310d2e96bf2cf0e35443a22/ssl/ssl_lib.c/#L645 | SSL *SSL_new(SSL_CTX *ctx)
{
SSL *s;
if (ctx == NULL) {
SSLerr(SSL_F_SSL_NEW, SSL_R_NULL_SSL_CTX);
return (NULL);
}
if (ctx->method == NULL) {
SSLerr(SSL_F_SSL_NEW, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
return (NULL);
}
s = OPENSSL_zalloc(sizeof(*s));
if (s == NULL)
goto err;
s->lock = CRYPTO_THREAD_lock_new();
if (s->lock == NULL)
goto err;
if (RAND_get_rand_method() == RAND_OpenSSL()) {
s->drbg = RAND_DRBG_new(NID_aes_128_ctr, RAND_DRBG_FLAG_CTR_USE_DF,
RAND_DRBG_get0_global());
if (s->drbg == NULL
|| RAND_DRBG_instantiate(s->drbg, NULL, 0) == 0) {
CRYPTO_THREAD_lock_free(s->lock);
goto err;
}
}
RECORD_LAYER_init(&s->rlayer, s);
s->options = ctx->options;
s->dane.flags = ctx->dane.flags;
s->min_proto_version = ctx->min_proto_version;
s->max_proto_version = ctx->max_proto_version;
s->mode = ctx->mode;
s->max_cert_list = ctx->max_cert_list;
s->references = 1;
s->max_early_data = ctx->max_early_data;
s->cert = ssl_cert_dup(ctx->cert);
if (s->cert == NULL)
goto err;
RECORD_LAYER_set_read_ahead(&s->rlayer, ctx->read_ahead);
s->msg_callback = ctx->msg_callback;
s->msg_callback_arg = ctx->msg_callback_arg;
s->verify_mode = ctx->verify_mode;
s->not_resumable_session_cb = ctx->not_resumable_session_cb;
s->record_padding_cb = ctx->record_padding_cb;
s->record_padding_arg = ctx->record_padding_arg;
s->block_padding = ctx->block_padding;
s->sid_ctx_length = ctx->sid_ctx_length;
if (!ossl_assert(s->sid_ctx_length <= sizeof s->sid_ctx))
goto err;
memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
s->verify_callback = ctx->default_verify_callback;
s->generate_session_id = ctx->generate_session_id;
s->param = X509_VERIFY_PARAM_new();
if (s->param == NULL)
goto err;
X509_VERIFY_PARAM_inherit(s->param, ctx->param);
s->quiet_shutdown = ctx->quiet_shutdown;
s->max_send_fragment = ctx->max_send_fragment;
s->split_send_fragment = ctx->split_send_fragment;
s->max_pipelines = ctx->max_pipelines;
if (s->max_pipelines > 1)
RECORD_LAYER_set_read_ahead(&s->rlayer, 1);
if (ctx->default_read_buf_len > 0)
SSL_set_default_read_buffer_len(s, ctx->default_read_buf_len);
SSL_CTX_up_ref(ctx);
s->ctx = ctx;
s->ext.debug_cb = 0;
s->ext.debug_arg = NULL;
s->ext.ticket_expected = 0;
s->ext.status_type = ctx->ext.status_type;
s->ext.status_expected = 0;
s->ext.ocsp.ids = NULL;
s->ext.ocsp.exts = NULL;
s->ext.ocsp.resp = NULL;
s->ext.ocsp.resp_len = 0;
SSL_CTX_up_ref(ctx);
s->session_ctx = ctx;
#ifndef OPENSSL_NO_EC
if (ctx->ext.ecpointformats) {
s->ext.ecpointformats =
OPENSSL_memdup(ctx->ext.ecpointformats,
ctx->ext.ecpointformats_len);
if (!s->ext.ecpointformats)
goto err;
s->ext.ecpointformats_len =
ctx->ext.ecpointformats_len;
}
if (ctx->ext.supportedgroups) {
s->ext.supportedgroups =
OPENSSL_memdup(ctx->ext.supportedgroups,
ctx->ext.supportedgroups_len
* sizeof(*ctx->ext.supportedgroups));
if (!s->ext.supportedgroups)
goto err;
s->ext.supportedgroups_len = ctx->ext.supportedgroups_len;
}
#endif
#ifndef OPENSSL_NO_NEXTPROTONEG
s->ext.npn = NULL;
#endif
if (s->ctx->ext.alpn) {
s->ext.alpn = OPENSSL_malloc(s->ctx->ext.alpn_len);
if (s->ext.alpn == NULL)
goto err;
memcpy(s->ext.alpn, s->ctx->ext.alpn, s->ctx->ext.alpn_len);
s->ext.alpn_len = s->ctx->ext.alpn_len;
}
s->verified_chain = NULL;
s->verify_result = X509_V_OK;
s->default_passwd_callback = ctx->default_passwd_callback;
s->default_passwd_callback_userdata = ctx->default_passwd_callback_userdata;
s->method = ctx->method;
s->key_update = SSL_KEY_UPDATE_NONE;
if (!s->method->ssl_new(s))
goto err;
s->server = (ctx->method->ssl_accept == ssl_undefined_function) ? 0 : 1;
if (!SSL_clear(s))
goto err;
if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data))
goto err;
#ifndef OPENSSL_NO_PSK
s->psk_client_callback = ctx->psk_client_callback;
s->psk_server_callback = ctx->psk_server_callback;
#endif
s->psk_find_session_cb = ctx->psk_find_session_cb;
s->psk_use_session_cb = ctx->psk_use_session_cb;
s->job = NULL;
#ifndef OPENSSL_NO_CT
if (!SSL_set_ct_validation_callback(s, ctx->ct_validation_callback,
ctx->ct_validation_callback_arg))
goto err;
#endif
return s;
err:
SSL_free(s);
SSLerr(SSL_F_SSL_NEW, ERR_R_MALLOC_FAILURE);
return NULL;
} | ['SSL *SSL_new(SSL_CTX *ctx)\n{\n SSL *s;\n if (ctx == NULL) {\n SSLerr(SSL_F_SSL_NEW, SSL_R_NULL_SSL_CTX);\n return (NULL);\n }\n if (ctx->method == NULL) {\n SSLerr(SSL_F_SSL_NEW, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);\n return (NULL);\n }\n s = OPENSSL_zalloc(sizeof(*s));\n if (s == NULL)\n goto err;\n s->lock = CRYPTO_THREAD_lock_new();\n if (s->lock == NULL)\n goto err;\n if (RAND_get_rand_method() == RAND_OpenSSL()) {\n s->drbg = RAND_DRBG_new(NID_aes_128_ctr, RAND_DRBG_FLAG_CTR_USE_DF,\n RAND_DRBG_get0_global());\n if (s->drbg == NULL\n || RAND_DRBG_instantiate(s->drbg, NULL, 0) == 0) {\n CRYPTO_THREAD_lock_free(s->lock);\n goto err;\n }\n }\n RECORD_LAYER_init(&s->rlayer, s);\n s->options = ctx->options;\n s->dane.flags = ctx->dane.flags;\n s->min_proto_version = ctx->min_proto_version;\n s->max_proto_version = ctx->max_proto_version;\n s->mode = ctx->mode;\n s->max_cert_list = ctx->max_cert_list;\n s->references = 1;\n s->max_early_data = ctx->max_early_data;\n s->cert = ssl_cert_dup(ctx->cert);\n if (s->cert == NULL)\n goto err;\n RECORD_LAYER_set_read_ahead(&s->rlayer, ctx->read_ahead);\n s->msg_callback = ctx->msg_callback;\n s->msg_callback_arg = ctx->msg_callback_arg;\n s->verify_mode = ctx->verify_mode;\n s->not_resumable_session_cb = ctx->not_resumable_session_cb;\n s->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->max_send_fragment = ctx->max_send_fragment;\n s->split_send_fragment = ctx->split_send_fragment;\n s->max_pipelines = ctx->max_pipelines;\n if (s->max_pipelines > 1)\n RECORD_LAYER_set_read_ahead(&s->rlayer, 1);\n if (ctx->default_read_buf_len > 0)\n SSL_set_default_read_buffer_len(s, ctx->default_read_buf_len);\n SSL_CTX_up_ref(ctx);\n s->ctx = ctx;\n s->ext.debug_cb = 0;\n s->ext.debug_arg = NULL;\n s->ext.ticket_expected = 0;\n s->ext.status_type = ctx->ext.status_type;\n s->ext.status_expected = 0;\n s->ext.ocsp.ids = NULL;\n s->ext.ocsp.exts = NULL;\n s->ext.ocsp.resp = NULL;\n s->ext.ocsp.resp_len = 0;\n SSL_CTX_up_ref(ctx);\n s->session_ctx = ctx;\n#ifndef OPENSSL_NO_EC\n if (ctx->ext.ecpointformats) {\n s->ext.ecpointformats =\n OPENSSL_memdup(ctx->ext.ecpointformats,\n ctx->ext.ecpointformats_len);\n if (!s->ext.ecpointformats)\n goto err;\n s->ext.ecpointformats_len =\n ctx->ext.ecpointformats_len;\n }\n if (ctx->ext.supportedgroups) {\n s->ext.supportedgroups =\n OPENSSL_memdup(ctx->ext.supportedgroups,\n ctx->ext.supportedgroups_len\n * sizeof(*ctx->ext.supportedgroups));\n if (!s->ext.supportedgroups)\n goto err;\n s->ext.supportedgroups_len = ctx->ext.supportedgroups_len;\n }\n#endif\n#ifndef OPENSSL_NO_NEXTPROTONEG\n s->ext.npn = NULL;\n#endif\n if (s->ctx->ext.alpn) {\n s->ext.alpn = OPENSSL_malloc(s->ctx->ext.alpn_len);\n if (s->ext.alpn == NULL)\n goto err;\n memcpy(s->ext.alpn, s->ctx->ext.alpn, s->ctx->ext.alpn_len);\n s->ext.alpn_len = s->ctx->ext.alpn_len;\n }\n s->verified_chain = NULL;\n s->verify_result = X509_V_OK;\n s->default_passwd_callback = ctx->default_passwd_callback;\n s->default_passwd_callback_userdata = ctx->default_passwd_callback_userdata;\n s->method = ctx->method;\n s->key_update = SSL_KEY_UPDATE_NONE;\n if (!s->method->ssl_new(s))\n goto err;\n s->server = (ctx->method->ssl_accept == ssl_undefined_function) ? 0 : 1;\n if (!SSL_clear(s))\n goto err;\n if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data))\n goto err;\n#ifndef OPENSSL_NO_PSK\n s->psk_client_callback = ctx->psk_client_callback;\n s->psk_server_callback = ctx->psk_server_callback;\n#endif\n s->psk_find_session_cb = ctx->psk_find_session_cb;\n s->psk_use_session_cb = ctx->psk_use_session_cb;\n s->job = NULL;\n#ifndef OPENSSL_NO_CT\n if (!SSL_set_ct_validation_callback(s, ctx->ct_validation_callback,\n ctx->ct_validation_callback_arg))\n goto err;\n#endif\n return s;\n err:\n SSL_free(s);\n SSLerr(SSL_F_SSL_NEW, ERR_R_MALLOC_FAILURE);\n return NULL;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)(file); (void)(line);\n ret = malloc(num);\n#endif\n return ret;\n}', 'CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)\n{\n# ifdef USE_RWLOCK\n CRYPTO_RWLOCK *lock = OPENSSL_zalloc(sizeof(pthread_rwlock_t));\n if (lock == NULL)\n return NULL;\n if (pthread_rwlock_init(lock, NULL) != 0) {\n OPENSSL_free(lock);\n return NULL;\n }\n# else\n pthread_mutexattr_t attr;\n CRYPTO_RWLOCK *lock = OPENSSL_zalloc(sizeof(pthread_mutex_t));\n if (lock == NULL)\n return NULL;\n pthread_mutexattr_init(&attr);\n pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);\n if (pthread_mutex_init(lock, &attr) != 0) {\n pthread_mutexattr_destroy(&attr);\n OPENSSL_free(lock);\n return NULL;\n }\n pthread_mutexattr_destroy(&attr);\n# endif\n return lock;\n}', 'const RAND_METHOD *RAND_get_rand_method(void)\n{\n const RAND_METHOD *tmp_meth = NULL;\n if (!RUN_ONCE(&rand_init, do_rand_init))\n return NULL;\n CRYPTO_THREAD_write_lock(rand_meth_lock);\n if (default_RAND_meth == NULL) {\n#ifndef OPENSSL_NO_ENGINE\n ENGINE *e;\n if ((e = ENGINE_get_default_RAND()) != NULL\n && (tmp_meth = ENGINE_get_RAND(e)) != NULL) {\n funct_ref = e;\n default_RAND_meth = tmp_meth;\n } else {\n ENGINE_finish(e);\n default_RAND_meth = &rand_meth;\n }\n#else\n default_RAND_meth = &rand_meth;\n#endif\n }\n tmp_meth = default_RAND_meth;\n CRYPTO_THREAD_unlock(rand_meth_lock);\n return tmp_meth;\n}', 'int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))\n{\n if (pthread_once(once, init) != 0)\n return 0;\n return 1;\n}', 'RAND_METHOD *RAND_OpenSSL(void)\n{\n return &rand_meth;\n}'] |
35,422 | 0 | https://github.com/openssl/openssl/blob/d40a1b865fddc3d67f8c06ff1f1466fad331c8f7/crypto/bn/bn_lib.c/#L250 | int BN_num_bits(const BIGNUM *a)
{
int i = a->top - 1;
bn_check_top(a);
if (BN_is_zero(a)) return 0;
return ((i*BN_BITS2) + BN_num_bits_word(a->d[i]));
} | ['int dsa_builtin_paramgen(DSA *ret, size_t bits, size_t qbits,\n\tconst EVP_MD *evpmd, unsigned char *seed_in, size_t seed_len,\n\tint *counter_ret, unsigned long *h_ret, BN_GENCB *cb)\n\t{\n\tint ok=0;\n\tunsigned char seed[SHA256_DIGEST_LENGTH];\n\tunsigned char md[SHA256_DIGEST_LENGTH];\n\tunsigned char buf[SHA256_DIGEST_LENGTH],buf2[SHA256_DIGEST_LENGTH];\n\tBIGNUM *r0,*W,*X,*c,*test;\n\tBIGNUM *g=NULL,*q=NULL,*p=NULL;\n\tBN_MONT_CTX *mont=NULL;\n\tint i, k,n=0,b,m=0, qsize = qbits >> 3;\n\tint counter=0;\n\tint r=0;\n\tBN_CTX *ctx=NULL;\n\tunsigned int h=2;\n\tif (qsize != SHA_DIGEST_LENGTH && qsize != SHA224_DIGEST_LENGTH &&\n\t qsize != SHA256_DIGEST_LENGTH)\n\t\treturn 0;\n\tif (evpmd == NULL)\n\t\tevpmd = EVP_sha1();\n\tif (bits < 512)\n\t\tbits = 512;\n\tbits = (bits+63)/64*64;\n\tif (seed_len && (seed_len < (size_t)qsize))\n\t\tseed_in = NULL;\n\tif (seed_len > (size_t)qsize)\n\t\tseed_len = qsize;\n\tif (seed_in != NULL)\n\t\t{\n\t\tmemcpy(seed, seed_in, seed_len);\n\t\tseed_in = NULL;\n\t\t}\n\tif ((ctx=BN_CTX_new()) == NULL)\n\t\tgoto err;\n\tif ((mont=BN_MONT_CTX_new()) == NULL)\n\t\tgoto err;\n\tBN_CTX_start(ctx);\n\tr0 = BN_CTX_get(ctx);\n\tg = BN_CTX_get(ctx);\n\tW = BN_CTX_get(ctx);\n\tq = BN_CTX_get(ctx);\n\tX = BN_CTX_get(ctx);\n\tc = BN_CTX_get(ctx);\n\tp = BN_CTX_get(ctx);\n\ttest = BN_CTX_get(ctx);\n\tif (!BN_lshift(test,BN_value_one(),bits-1))\n\t\tgoto err;\n\tfor (;;)\n\t\t{\n\t\tfor (;;)\n\t\t\t{\n\t\t\tint seed_is_random;\n\t\t\tif(!BN_GENCB_call(cb, 0, m++))\n\t\t\t\tgoto err;\n\t\t\tif (!seed_len)\n\t\t\t\t{\n\t\t\t\tRAND_pseudo_bytes(seed, qsize);\n\t\t\t\tseed_is_random = 1;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tseed_is_random = 0;\n\t\t\t\tseed_len=0;\n\t\t\t\t}\n\t\t\tmemcpy(buf , seed, qsize);\n\t\t\tmemcpy(buf2, seed, qsize);\n\t\t\tfor (i = qsize-1; i >= 0; i--)\n\t\t\t\t{\n\t\t\t\tbuf[i]++;\n\t\t\t\tif (buf[i] != 0)\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\tEVP_Digest(seed, qsize, md, NULL, evpmd, NULL);\n\t\t\tEVP_Digest(buf, qsize, buf2, NULL, evpmd, NULL);\n\t\t\tfor (i = 0; i < qsize; i++)\n\t\t\t\tmd[i]^=buf2[i];\n\t\t\tmd[0] |= 0x80;\n\t\t\tmd[qsize-1] |= 0x01;\n\t\t\tif (!BN_bin2bn(md, qsize, q))\n\t\t\t\tgoto err;\n\t\t\tr = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx,\n\t\t\t\t\tseed_is_random, cb);\n\t\t\tif (r > 0)\n\t\t\t\tbreak;\n\t\t\tif (r != 0)\n\t\t\t\tgoto err;\n\t\t\t}\n\t\tif(!BN_GENCB_call(cb, 2, 0)) goto err;\n\t\tif(!BN_GENCB_call(cb, 3, 0)) goto err;\n\t\tcounter=0;\n\t\tn=(bits-1)/160;\n\t\tb=(bits-1)-n*160;\n\t\tfor (;;)\n\t\t\t{\n\t\t\tif ((counter != 0) && !BN_GENCB_call(cb, 0, counter))\n\t\t\t\tgoto err;\n\t\t\tBN_zero(W);\n\t\t\tfor (k=0; k<=n; k++)\n\t\t\t\t{\n\t\t\t\tfor (i = qsize-1; i >= 0; i--)\n\t\t\t\t\t{\n\t\t\t\t\tbuf[i]++;\n\t\t\t\t\tif (buf[i] != 0)\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\tEVP_Digest(buf, qsize, md ,NULL, evpmd, NULL);\n\t\t\t\tif (!BN_bin2bn(md, qsize, r0))\n\t\t\t\t\tgoto err;\n\t\t\t\tif (!BN_lshift(r0,r0,(qsize << 3)*k)) goto err;\n\t\t\t\tif (!BN_add(W,W,r0)) goto err;\n\t\t\t\t}\n\t\t\tif (!BN_mask_bits(W,bits-1)) goto err;\n\t\t\tif (!BN_copy(X,W)) goto err;\n\t\t\tif (!BN_add(X,X,test)) goto err;\n\t\t\tif (!BN_lshift1(r0,q)) goto err;\n\t\t\tif (!BN_mod(c,X,r0,ctx)) goto err;\n\t\t\tif (!BN_sub(r0,c,BN_value_one())) goto err;\n\t\t\tif (!BN_sub(p,X,r0)) goto err;\n\t\t\tif (BN_cmp(p,test) >= 0)\n\t\t\t\t{\n\t\t\t\tr = BN_is_prime_fasttest_ex(p, DSS_prime_checks,\n\t\t\t\t\t\tctx, 1, cb);\n\t\t\t\tif (r > 0)\n\t\t\t\t\t\tgoto end;\n\t\t\t\tif (r != 0)\n\t\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tcounter++;\n\t\t\tif (counter >= 4096) break;\n\t\t\t}\n\t\t}\nend:\n\tif(!BN_GENCB_call(cb, 2, 1))\n\t\tgoto err;\n\tif (!BN_sub(test,p,BN_value_one())) goto err;\n\tif (!BN_div(r0,NULL,test,q,ctx)) goto err;\n\tif (!BN_set_word(test,h)) goto err;\n\tif (!BN_MONT_CTX_set(mont,p,ctx)) goto err;\n\tfor (;;)\n\t\t{\n\t\tif (!BN_mod_exp_mont(g,test,r0,p,ctx,mont)) goto err;\n\t\tif (!BN_is_one(g)) break;\n\t\tif (!BN_add(test,test,BN_value_one())) goto err;\n\t\th++;\n\t\t}\n\tif(!BN_GENCB_call(cb, 3, 1))\n\t\tgoto err;\n\tok=1;\nerr:\n\tif (ok)\n\t\t{\n\t\tif(ret->p) BN_free(ret->p);\n\t\tif(ret->q) BN_free(ret->q);\n\t\tif(ret->g) BN_free(ret->g);\n\t\tret->p=BN_dup(p);\n\t\tret->q=BN_dup(q);\n\t\tret->g=BN_dup(g);\n\t\tif (ret->p == NULL || ret->q == NULL || ret->g == NULL)\n\t\t\t{\n\t\t\tok=0;\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (seed_in != NULL) memcpy(seed_in,seed, qsize);\n\t\tif (counter_ret != NULL) *counter_ret=counter;\n\t\tif (h_ret != NULL) *h_ret=h;\n\t\t}\n\tif(ctx)\n\t\t{\n\t\tBN_CTX_end(ctx);\n\t\tBN_CTX_free(ctx);\n\t\t}\n\tif (mont != NULL) BN_MONT_CTX_free(mont);\n\treturn ok;\n\t}', 'BIGNUM *BN_bin2bn(const unsigned char *s, size_t len, BIGNUM *ret)\n\t{\n\tunsigned int i,m;\n\tunsigned int n;\n\tBN_ULONG l;\n\tBIGNUM *bn = NULL;\n\tif (ret == NULL)\n\t\tret = bn = BN_new();\n\tif (ret == NULL) return(NULL);\n\tbn_check_top(ret);\n\tl=0;\n\tn=len;\n\tif (n == 0)\n\t\t{\n\t\tret->top=0;\n\t\treturn(ret);\n\t\t}\n\ti=((n-1)/BN_BYTES)+1;\n\tm=((n-1)%(BN_BYTES));\n\tif (bn_wexpand(ret, i) == NULL)\n\t\t{\n\t\tif (bn) BN_free(bn);\n\t\treturn NULL;\n\t\t}\n\tret->top=i;\n\tret->neg=0;\n\twhile (n--)\n\t\t{\n\t\tl=(l<<8L)| *(s++);\n\t\tif (m-- == 0)\n\t\t\t{\n\t\t\tret->d[--i]=l;\n\t\t\tl=0;\n\t\t\tm=BN_BYTES-1;\n\t\t\t}\n\t\t}\n\tbn_correct_top(ret);\n\treturn(ret);\n\t}', 'int BN_is_prime_fasttest_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,\n\t\tint do_trial_division, BN_GENCB *cb)\n\t{\n\tint i, j, ret = -1;\n\tint k;\n\tBN_CTX *ctx = NULL;\n\tBIGNUM *A1, *A1_odd, *check;\n\tBN_MONT_CTX *mont = NULL;\n\tconst BIGNUM *A = NULL;\n\tif (BN_cmp(a, BN_value_one()) <= 0)\n\t\treturn 0;\n\tif (checks == BN_prime_checks)\n\t\tchecks = BN_prime_checks_for_size(BN_num_bits(a));\n\tif (!BN_is_odd(a))\n\t\treturn BN_is_word(a, 2);\n\tif (do_trial_division)\n\t\t{\n\t\tfor (i = 1; i < NUMPRIMES; i++)\n\t\t\tif (BN_mod_word(a, primes[i]) == 0)\n\t\t\t\treturn 0;\n\t\tif(!BN_GENCB_call(cb, 1, -1))\n\t\t\tgoto err;\n\t\t}\n\tif (ctx_passed != NULL)\n\t\tctx = ctx_passed;\n\telse\n\t\tif ((ctx=BN_CTX_new()) == NULL)\n\t\t\tgoto err;\n\tBN_CTX_start(ctx);\n\tif (a->neg)\n\t\t{\n\t\tBIGNUM *t;\n\t\tif ((t = BN_CTX_get(ctx)) == NULL) goto err;\n\t\tBN_copy(t, a);\n\t\tt->neg = 0;\n\t\tA = t;\n\t\t}\n\telse\n\t\tA = a;\n\tA1 = BN_CTX_get(ctx);\n\tA1_odd = BN_CTX_get(ctx);\n\tcheck = BN_CTX_get(ctx);\n\tif (check == NULL) goto err;\n\tif (!BN_copy(A1, A))\n\t\tgoto err;\n\tif (!BN_sub_word(A1, 1))\n\t\tgoto err;\n\tif (BN_is_zero(A1))\n\t\t{\n\t\tret = 0;\n\t\tgoto err;\n\t\t}\n\tk = 1;\n\twhile (!BN_is_bit_set(A1, k))\n\t\tk++;\n\tif (!BN_rshift(A1_odd, A1, k))\n\t\tgoto err;\n\tmont = BN_MONT_CTX_new();\n\tif (mont == NULL)\n\t\tgoto err;\n\tif (!BN_MONT_CTX_set(mont, A, ctx))\n\t\tgoto err;\n\tfor (i = 0; i < checks; i++)\n\t\t{\n\t\tif (!BN_pseudo_rand_range(check, A1))\n\t\t\tgoto err;\n\t\tif (!BN_add_word(check, 1))\n\t\t\tgoto err;\n\t\tj = witness(check, A, A1, A1_odd, k, ctx, mont);\n\t\tif (j == -1) goto err;\n\t\tif (j)\n\t\t\t{\n\t\t\tret=0;\n\t\t\tgoto err;\n\t\t\t}\n\t\tif(!BN_GENCB_call(cb, 1, i))\n\t\t\tgoto err;\n\t\t}\n\tret=1;\nerr:\n\tif (ctx != NULL)\n\t\t{\n\t\tBN_CTX_end(ctx);\n\t\tif (ctx_passed == NULL)\n\t\t\tBN_CTX_free(ctx);\n\t\t}\n\tif (mont != NULL)\n\t\tBN_MONT_CTX_free(mont);\n\treturn(ret);\n\t}', 'int BN_cmp(const BIGNUM *a, const BIGNUM *b)\n\t{\n\tint i;\n\tint gt,lt;\n\tBN_ULONG t1,t2;\n\tif ((a == NULL) || (b == NULL))\n\t\t{\n\t\tif (a != NULL)\n\t\t\treturn(-1);\n\t\telse if (b != NULL)\n\t\t\treturn(1);\n\t\telse\n\t\t\treturn(0);\n\t\t}\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tif (a->neg != b->neg)\n\t\t{\n\t\tif (a->neg)\n\t\t\treturn(-1);\n\t\telse\treturn(1);\n\t\t}\n\tif (a->neg == 0)\n\t\t{ gt=1; lt= -1; }\n\telse\t{ gt= -1; lt=1; }\n\tif (a->top > b->top) return(gt);\n\tif (a->top < b->top) return(lt);\n\tfor (i=a->top-1; i>=0; i--)\n\t\t{\n\t\tt1=a->d[i];\n\t\tt2=b->d[i];\n\t\tif (t1 > t2) return(gt);\n\t\tif (t1 < t2) return(lt);\n\t\t}\n\treturn(0);\n\t}', 'int BN_num_bits(const BIGNUM *a)\n\t{\n\tint i = a->top - 1;\n\tbn_check_top(a);\n\tif (BN_is_zero(a)) return 0;\n\treturn ((i*BN_BITS2) + BN_num_bits_word(a->d[i]));\n\t}'] |
35,423 | 0 | https://github.com/openssl/openssl/blob/1fac96e4d6484a517f2ebe99b72016726391723c/crypto/des/des_enc.c/#L144 | void des_encrypt(DES_LONG *data, des_key_schedule ks, int enc)
{
register DES_LONG l,r,t,u;
#ifdef DES_PTR
register const unsigned char *des_SP=(const unsigned char *)des_SPtrans;
#endif
#ifndef DES_UNROLL
register int i;
#endif
register DES_LONG *s;
r=data[0];
l=data[1];
IP(r,l);
r=ROTATE(r,29)&0xffffffffL;
l=ROTATE(l,29)&0xffffffffL;
s=(DES_LONG *)ks;
if (enc)
{
#ifdef DES_UNROLL
D_ENCRYPT(l,r, 0);
D_ENCRYPT(r,l, 2);
D_ENCRYPT(l,r, 4);
D_ENCRYPT(r,l, 6);
D_ENCRYPT(l,r, 8);
D_ENCRYPT(r,l,10);
D_ENCRYPT(l,r,12);
D_ENCRYPT(r,l,14);
D_ENCRYPT(l,r,16);
D_ENCRYPT(r,l,18);
D_ENCRYPT(l,r,20);
D_ENCRYPT(r,l,22);
D_ENCRYPT(l,r,24);
D_ENCRYPT(r,l,26);
D_ENCRYPT(l,r,28);
D_ENCRYPT(r,l,30);
#else
for (i=0; i<32; i+=8)
{
D_ENCRYPT(l,r,i+0);
D_ENCRYPT(r,l,i+2);
D_ENCRYPT(l,r,i+4);
D_ENCRYPT(r,l,i+6);
}
#endif
}
else
{
#ifdef DES_UNROLL
D_ENCRYPT(l,r,30);
D_ENCRYPT(r,l,28);
D_ENCRYPT(l,r,26);
D_ENCRYPT(r,l,24);
D_ENCRYPT(l,r,22);
D_ENCRYPT(r,l,20);
D_ENCRYPT(l,r,18);
D_ENCRYPT(r,l,16);
D_ENCRYPT(l,r,14);
D_ENCRYPT(r,l,12);
D_ENCRYPT(l,r,10);
D_ENCRYPT(r,l, 8);
D_ENCRYPT(l,r, 6);
D_ENCRYPT(r,l, 4);
D_ENCRYPT(l,r, 2);
D_ENCRYPT(r,l, 0);
#else
for (i=30; i>0; i-=8)
{
D_ENCRYPT(l,r,i-0);
D_ENCRYPT(r,l,i-2);
D_ENCRYPT(l,r,i-4);
D_ENCRYPT(r,l,i-6);
}
#endif
}
l=ROTATE(l,3)&0xffffffffL;
r=ROTATE(r,3)&0xffffffffL;
FP(r,l);
data[0]=l;
data[1]=r;
l=r=t=u=0;
} | ['int _des_crypt(char *buf, int len, struct desparams *desp)\n\t{\n\tdes_key_schedule ks;\n\tint enc;\n\tdes_set_key(&desp->des_key,ks);\n\tenc=(desp->des_dir == ENCRYPT)?DES_ENCRYPT:DES_DECRYPT;\n\tif (desp->des_mode == CBC)\n\t\tdes_ecb_encrypt((const_des_cblock *)desp->UDES.UDES_buf,\n\t\t\t\t(des_cblock *)desp->UDES.UDES_buf,ks,\n\t\t\t\tenc);\n\telse\n\t\t{\n\t\tdes_ncbc_encrypt(desp->UDES.UDES_buf,desp->UDES.UDES_buf,\n\t\t\t\tlen,ks,&desp->des_ivec,enc);\n#ifdef undef\n\t\ta=(char *)&(desp->UDES.UDES_buf[len-8]);\n\t\tb=(char *)&(desp->des_ivec[0]);\n\t\t*(a++)= *(b++); *(a++)= *(b++);\n\t\t*(a++)= *(b++); *(a++)= *(b++);\n\t\t*(a++)= *(b++); *(a++)= *(b++);\n\t\t*(a++)= *(b++); *(a++)= *(b++);\n#endif\n\t\t}\n\treturn(1);\n\t}', 'void des_ecb_encrypt(const_des_cblock *input, des_cblock *output,\n\t des_key_schedule ks,\n\t int enc)\n\t{\n\tregister DES_LONG l;\n\tDES_LONG ll[2];\n\tconst unsigned char *in = &(*input)[0];\n\tunsigned char *out = &(*output)[0];\n\tc2l(in,l); ll[0]=l;\n\tc2l(in,l); ll[1]=l;\n\tdes_encrypt(ll,ks,enc);\n\tl=ll[0]; l2c(l,out);\n\tl=ll[1]; l2c(l,out);\n\tl=ll[0]=ll[1]=0;\n\t}', 'void des_encrypt(DES_LONG *data, des_key_schedule ks, int enc)\n\t{\n\tregister DES_LONG l,r,t,u;\n#ifdef DES_PTR\n\tregister const unsigned char *des_SP=(const unsigned char *)des_SPtrans;\n#endif\n#ifndef DES_UNROLL\n\tregister int i;\n#endif\n\tregister DES_LONG *s;\n\tr=data[0];\n\tl=data[1];\n\tIP(r,l);\n\tr=ROTATE(r,29)&0xffffffffL;\n\tl=ROTATE(l,29)&0xffffffffL;\n\ts=(DES_LONG *)ks;\n\tif (enc)\n\t\t{\n#ifdef DES_UNROLL\n\t\tD_ENCRYPT(l,r, 0);\n\t\tD_ENCRYPT(r,l, 2);\n\t\tD_ENCRYPT(l,r, 4);\n\t\tD_ENCRYPT(r,l, 6);\n\t\tD_ENCRYPT(l,r, 8);\n\t\tD_ENCRYPT(r,l,10);\n\t\tD_ENCRYPT(l,r,12);\n\t\tD_ENCRYPT(r,l,14);\n\t\tD_ENCRYPT(l,r,16);\n\t\tD_ENCRYPT(r,l,18);\n\t\tD_ENCRYPT(l,r,20);\n\t\tD_ENCRYPT(r,l,22);\n\t\tD_ENCRYPT(l,r,24);\n\t\tD_ENCRYPT(r,l,26);\n\t\tD_ENCRYPT(l,r,28);\n\t\tD_ENCRYPT(r,l,30);\n#else\n\t\tfor (i=0; i<32; i+=8)\n\t\t\t{\n\t\t\tD_ENCRYPT(l,r,i+0);\n\t\t\tD_ENCRYPT(r,l,i+2);\n\t\t\tD_ENCRYPT(l,r,i+4);\n\t\t\tD_ENCRYPT(r,l,i+6);\n\t\t\t}\n#endif\n\t\t}\n\telse\n\t\t{\n#ifdef DES_UNROLL\n\t\tD_ENCRYPT(l,r,30);\n\t\tD_ENCRYPT(r,l,28);\n\t\tD_ENCRYPT(l,r,26);\n\t\tD_ENCRYPT(r,l,24);\n\t\tD_ENCRYPT(l,r,22);\n\t\tD_ENCRYPT(r,l,20);\n\t\tD_ENCRYPT(l,r,18);\n\t\tD_ENCRYPT(r,l,16);\n\t\tD_ENCRYPT(l,r,14);\n\t\tD_ENCRYPT(r,l,12);\n\t\tD_ENCRYPT(l,r,10);\n\t\tD_ENCRYPT(r,l, 8);\n\t\tD_ENCRYPT(l,r, 6);\n\t\tD_ENCRYPT(r,l, 4);\n\t\tD_ENCRYPT(l,r, 2);\n\t\tD_ENCRYPT(r,l, 0);\n#else\n\t\tfor (i=30; i>0; i-=8)\n\t\t\t{\n\t\t\tD_ENCRYPT(l,r,i-0);\n\t\t\tD_ENCRYPT(r,l,i-2);\n\t\t\tD_ENCRYPT(l,r,i-4);\n\t\t\tD_ENCRYPT(r,l,i-6);\n\t\t\t}\n#endif\n\t\t}\n\tl=ROTATE(l,3)&0xffffffffL;\n\tr=ROTATE(r,3)&0xffffffffL;\n\tFP(r,l);\n\tdata[0]=l;\n\tdata[1]=r;\n\tl=r=t=u=0;\n\t}'] |
35,424 | 0 | https://github.com/openssl/openssl/blob/ee6b68ce4c67870f9323d2a380eb949f447c56ee/test/testutil/tests.c/#L303 | static char *print_mem_maybe_null(const void *s, size_t n,
char outbuf[MEM_BUFFER_SIZE])
{
size_t i;
const unsigned char *p = (const unsigned char *)s;
char *out = outbuf;
int pad = 2 * n >= MEM_BUFFER_SIZE;
if (s == NULL)
return strcpy(outbuf, "(NULL)");
if (pad) {
if ((out = OPENSSL_malloc(2 * n + 1)) == NULL) {
out = outbuf;
n = (MEM_BUFFER_SIZE - 4) / 2;
} else {
pad = 0;
}
}
for (i = 0; i < 2 * n; ) {
const unsigned char c = *p++;
out[i++] = "0123456789abcdef"[c >> 4];
out[i++] = "0123456789abcdef"[c & 15];
}
if (pad) {
out[i++] = '.';
out[i++] = '.';
out[i++] = '.';
}
out[i] = '\0';
return out;
} | ['static int cfb_test(int bits, unsigned char *cfb_cipher)\n{\n DES_key_schedule ks;\n DES_set_key_checked(&cfb_key, &ks);\n memcpy(cfb_tmp, cfb_iv, sizeof(cfb_iv));\n DES_cfb_encrypt(plain, cfb_buf1, bits, sizeof(plain), &ks, &cfb_tmp,\n DES_ENCRYPT);\n if (!TEST_mem_eq(cfb_cipher, sizeof(plain), cfb_buf1, sizeof(plain)))\n return 0;\n memcpy(cfb_tmp, cfb_iv, sizeof(cfb_iv));\n DES_cfb_encrypt(cfb_buf1, cfb_buf2, bits, sizeof(plain), &ks, &cfb_tmp,\n DES_DECRYPT);\n return TEST_mem_eq(plain, sizeof(plain), cfb_buf2, sizeof(plain));\n}', 'int test_mem_eq(const char *file, int line, const char *st1, const char *st2,\n const void *s1, size_t n1, const void *s2, size_t n2)\n{\n char b1[MEM_BUFFER_SIZE], b2[MEM_BUFFER_SIZE];\n if (s1 == NULL && s2 == NULL)\n return 1;\n if (n1 != n2 || s1 == NULL || s2 == NULL || memcmp(s1, s2, n1) != 0) {\n char *m1 = print_mem_maybe_null(s1, n1, b1);\n char *m2 = print_mem_maybe_null(s2, n2, b2);\n test_fail_message(NULL, file, line, "memory",\n "%s %s [%zu] == %s %s [%zu]",\n st1, m1, n1, st2, m2, n2);\n if (m1 != b1)\n OPENSSL_free(m1);\n if (m2 != b2)\n OPENSSL_free(m2);\n return 0;\n }\n return 1;\n}', 'static char *print_mem_maybe_null(const void *s, size_t n,\n char outbuf[MEM_BUFFER_SIZE])\n{\n size_t i;\n const unsigned char *p = (const unsigned char *)s;\n char *out = outbuf;\n int pad = 2 * n >= MEM_BUFFER_SIZE;\n if (s == NULL)\n return strcpy(outbuf, "(NULL)");\n if (pad) {\n if ((out = OPENSSL_malloc(2 * n + 1)) == NULL) {\n out = outbuf;\n n = (MEM_BUFFER_SIZE - 4) / 2;\n } else {\n pad = 0;\n }\n }\n for (i = 0; i < 2 * n; ) {\n const unsigned char c = *p++;\n out[i++] = "0123456789abcdef"[c >> 4];\n out[i++] = "0123456789abcdef"[c & 15];\n }\n if (pad) {\n out[i++] = \'.\';\n out[i++] = \'.\';\n out[i++] = \'.\';\n }\n out[i] = \'\\0\';\n return out;\n}'] |
35,425 | 0 | https://github.com/openssl/openssl/blob/47bbaa5b607f592009ed40f5678fde21c10a873c/crypto/bn/bn_lib.c/#L680 | int BN_set_bit(BIGNUM *a, int n)
{
int i, j, k;
if (n < 0)
return 0;
i = n / BN_BITS2;
j = n % BN_BITS2;
if (a->top <= i) {
if (bn_wexpand(a, i + 1) == NULL)
return (0);
for (k = a->top; k < i + 1; k++)
a->d[k] = 0;
a->top = i + 1;
}
a->d[i] |= (((BN_ULONG)1) << j);
bn_check_top(a);
return (1);
} | ['EC_KEY *d2i_ECPrivateKey(EC_KEY **a, const unsigned char **in, long len)\n{\n EC_KEY *ret = NULL;\n EC_PRIVATEKEY *priv_key = NULL;\n if ((priv_key = d2i_EC_PRIVATEKEY(NULL, in, len)) == NULL) {\n ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);\n return NULL;\n }\n if (a == NULL || *a == NULL) {\n if ((ret = EC_KEY_new()) == NULL) {\n ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n } else\n ret = *a;\n if (priv_key->parameters) {\n EC_GROUP_clear_free(ret->group);\n ret->group = ec_asn1_pkparameters2group(priv_key->parameters);\n }\n if (ret->group == NULL) {\n ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);\n goto err;\n }\n ret->version = priv_key->version;\n if (priv_key->privateKey) {\n if (ret->priv_key == NULL)\n ret->priv_key = BN_secure_new();\n ret->priv_key = BN_bin2bn(ASN1_STRING_data(priv_key->privateKey),\n ASN1_STRING_length(priv_key->privateKey),\n ret->priv_key);\n if (ret->priv_key == NULL) {\n ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_BN_LIB);\n goto err;\n }\n } else {\n ECerr(EC_F_D2I_ECPRIVATEKEY, EC_R_MISSING_PRIVATE_KEY);\n goto err;\n }\n EC_POINT_clear_free(ret->pub_key);\n ret->pub_key = EC_POINT_new(ret->group);\n if (ret->pub_key == NULL) {\n ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);\n goto err;\n }\n if (priv_key->publicKey) {\n const unsigned char *pub_oct;\n int pub_oct_len;\n pub_oct = ASN1_STRING_data(priv_key->publicKey);\n pub_oct_len = ASN1_STRING_length(priv_key->publicKey);\n if (pub_oct_len <= 0) {\n ECerr(EC_F_D2I_ECPRIVATEKEY, EC_R_BUFFER_TOO_SMALL);\n goto err;\n }\n ret->conv_form = (point_conversion_form_t) (pub_oct[0] & ~0x01);\n if (!EC_POINT_oct2point(ret->group, ret->pub_key,\n pub_oct, (size_t)(pub_oct_len), NULL)) {\n ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);\n goto err;\n }\n } else {\n if (!EC_POINT_mul\n (ret->group, ret->pub_key, ret->priv_key, NULL, NULL, NULL)) {\n ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);\n goto err;\n }\n ret->enc_flag |= EC_PKEY_NO_PUBKEY;\n }\n if (a)\n *a = ret;\n EC_PRIVATEKEY_free(priv_key);\n return (ret);\n err:\n if (a == NULL || *a != ret)\n EC_KEY_free(ret);\n EC_PRIVATEKEY_free(priv_key);\n return NULL;\n}', 'EC_GROUP *ec_asn1_pkparameters2group(const ECPKPARAMETERS *params)\n{\n EC_GROUP *ret = NULL;\n int tmp = 0;\n if (params == NULL) {\n ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, EC_R_MISSING_PARAMETERS);\n return NULL;\n }\n if (params->type == 0) {\n tmp = OBJ_obj2nid(params->value.named_curve);\n if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) {\n ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP,\n EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);\n return NULL;\n }\n EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE);\n } else if (params->type == 1) {\n ret = ec_asn1_parameters2group(params->value.parameters);\n if (!ret) {\n ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, ERR_R_EC_LIB);\n return NULL;\n }\n EC_GROUP_set_asn1_flag(ret, 0x0);\n } else if (params->type == 2) {\n return NULL;\n } else {\n ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, EC_R_ASN1_ERROR);\n return NULL;\n }\n return ret;\n}', 'static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *params)\n{\n int ok = 0, tmp;\n EC_GROUP *ret = NULL;\n BIGNUM *p = NULL, *a = NULL, *b = NULL;\n EC_POINT *point = NULL;\n long field_bits;\n if (!params->fieldID || !params->fieldID->fieldType ||\n !params->fieldID->p.ptr) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);\n goto err;\n }\n if (!params->curve || !params->curve->a ||\n !params->curve->a->data || !params->curve->b ||\n !params->curve->b->data) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);\n goto err;\n }\n a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL);\n if (a == NULL) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);\n goto err;\n }\n b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);\n if (b == NULL) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);\n goto err;\n }\n tmp = OBJ_obj2nid(params->fieldID->fieldType);\n if (tmp == NID_X9_62_characteristic_two_field)\n#ifdef OPENSSL_NO_EC2M\n {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_GF2M_NOT_SUPPORTED);\n goto err;\n }\n#else\n {\n X9_62_CHARACTERISTIC_TWO *char_two;\n char_two = params->fieldID->p.char_two;\n field_bits = char_two->m;\n if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_FIELD_TOO_LARGE);\n goto err;\n }\n if ((p = BN_new()) == NULL) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n tmp = OBJ_obj2nid(char_two->type);\n if (tmp == NID_X9_62_tpBasis) {\n long tmp_long;\n if (!char_two->p.tpBasis) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);\n goto err;\n }\n tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis);\n if (!(char_two->m > tmp_long && tmp_long > 0)) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP,\n EC_R_INVALID_TRINOMIAL_BASIS);\n goto err;\n }\n if (!BN_set_bit(p, (int)char_two->m))\n goto err;\n if (!BN_set_bit(p, (int)tmp_long))\n goto err;\n if (!BN_set_bit(p, 0))\n goto err;\n } else if (tmp == NID_X9_62_ppBasis) {\n X9_62_PENTANOMIAL *penta;\n penta = char_two->p.ppBasis;\n if (!penta) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);\n goto err;\n }\n if (!\n (char_two->m > penta->k3 && penta->k3 > penta->k2\n && penta->k2 > penta->k1 && penta->k1 > 0)) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP,\n EC_R_INVALID_PENTANOMIAL_BASIS);\n goto err;\n }\n if (!BN_set_bit(p, (int)char_two->m))\n goto err;\n if (!BN_set_bit(p, (int)penta->k1))\n goto err;\n if (!BN_set_bit(p, (int)penta->k2))\n goto err;\n if (!BN_set_bit(p, (int)penta->k3))\n goto err;\n if (!BN_set_bit(p, 0))\n goto err;\n } else if (tmp == NID_X9_62_onBasis) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_NOT_IMPLEMENTED);\n goto err;\n } else {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);\n goto err;\n }\n ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL);\n }\n#endif\n else if (tmp == NID_X9_62_prime_field) {\n if (!params->fieldID->p.prime) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);\n goto err;\n }\n p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL);\n if (p == NULL) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);\n goto err;\n }\n if (BN_is_negative(p) || BN_is_zero(p)) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_FIELD);\n goto err;\n }\n field_bits = BN_num_bits(p);\n if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_FIELD_TOO_LARGE);\n goto err;\n }\n ret = EC_GROUP_new_curve_GFp(p, a, b, NULL);\n } else {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_FIELD);\n goto err;\n }\n if (ret == NULL) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);\n goto err;\n }\n if (params->curve->seed != NULL) {\n OPENSSL_free(ret->seed);\n if ((ret->seed = OPENSSL_malloc(params->curve->seed->length)) == NULL) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n memcpy(ret->seed, params->curve->seed->data,\n params->curve->seed->length);\n ret->seed_len = params->curve->seed->length;\n }\n if (!params->order || !params->base || !params->base->data) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);\n goto err;\n }\n if ((point = EC_POINT_new(ret)) == NULL)\n goto err;\n EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t)\n (params->base->data[0] & ~0x01));\n if (!EC_POINT_oct2point(ret, point, params->base->data,\n params->base->length, NULL)) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);\n goto err;\n }\n if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);\n goto err;\n }\n if (BN_is_negative(a) || BN_is_zero(a)) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_GROUP_ORDER);\n goto err;\n }\n if (BN_num_bits(a) > (int)field_bits + 1) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_GROUP_ORDER);\n goto err;\n }\n if (params->cofactor == NULL) {\n BN_free(b);\n b = NULL;\n } else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);\n goto err;\n }\n if (!EC_GROUP_set_generator(ret, point, a, b)) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);\n goto err;\n }\n ok = 1;\n err:\n if (!ok) {\n EC_GROUP_clear_free(ret);\n ret = NULL;\n }\n BN_free(p);\n BN_free(a);\n BN_free(b);\n EC_POINT_free(point);\n return (ret);\n}', 'int BN_set_bit(BIGNUM *a, int n)\n{\n int i, j, k;\n if (n < 0)\n return 0;\n i = n / BN_BITS2;\n j = n % BN_BITS2;\n if (a->top <= i) {\n if (bn_wexpand(a, i + 1) == NULL)\n return (0);\n for (k = a->top; k < i + 1; k++)\n a->d[k] = 0;\n a->top = i + 1;\n }\n a->d[i] |= (((BN_ULONG)1) << j);\n bn_check_top(a);\n return (1);\n}'] |
35,426 | 0 | https://github.com/openssl/openssl/blob/ee3a6c646ff8ea6b9ada5a58f4a0e7c9b7be944b/crypto/bn/bn_mont.c/#L207 | static int BN_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont)
{
BIGNUM *n;
BN_ULONG *ap, *np, *rp, n0, v, carry;
int nl, max, i;
n = &(mont->N);
nl = n->top;
if (nl == 0) {
ret->top = 0;
return (1);
}
max = (2 * nl);
if (bn_wexpand(r, max) == NULL)
return (0);
r->neg ^= n->neg;
np = n->d;
rp = r->d;
i = max - r->top;
if (i)
memset(&rp[r->top], 0, sizeof(*rp) * i);
r->top = max;
n0 = mont->n0[0];
for (carry = 0, i = 0; i < nl; i++, rp++) {
v = bn_mul_add_words(rp, np, nl, (rp[0] * n0) & BN_MASK2);
v = (v + carry + rp[nl]) & BN_MASK2;
carry |= (v != rp[nl]);
carry &= (v <= rp[nl]);
rp[nl] = v;
}
if (bn_wexpand(ret, nl) == NULL)
return (0);
ret->top = nl;
ret->neg = r->neg;
rp = ret->d;
ap = &(r->d[nl]);
# define BRANCH_FREE 1
# if BRANCH_FREE
{
BN_ULONG *nrp;
size_t m;
v = bn_sub_words(rp, ap, np, nl) - carry;
m = (0 - (size_t)v);
nrp =
(BN_ULONG *)(((PTR_SIZE_INT) rp & ~m) | ((PTR_SIZE_INT) ap & m));
for (i = 0, nl -= 4; i < nl; i += 4) {
BN_ULONG t1, t2, t3, t4;
t1 = nrp[i + 0];
t2 = nrp[i + 1];
t3 = nrp[i + 2];
ap[i + 0] = 0;
t4 = nrp[i + 3];
ap[i + 1] = 0;
rp[i + 0] = t1;
ap[i + 2] = 0;
rp[i + 1] = t2;
ap[i + 3] = 0;
rp[i + 2] = t3;
rp[i + 3] = t4;
}
for (nl += 4; i < nl; i++)
rp[i] = nrp[i], ap[i] = 0;
}
# else
if (bn_sub_words(rp, ap, np, nl) - carry)
memcpy(rp, ap, nl * sizeof(BN_ULONG));
# endif
bn_correct_top(r);
bn_correct_top(ret);
bn_check_top(ret);
return (1);
} | ['int ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p,\n const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n BN_CTX *new_ctx = NULL;\n BN_MONT_CTX *mont = NULL;\n BIGNUM *one = NULL;\n int ret = 0;\n BN_MONT_CTX_free(group->field_data1);\n group->field_data1 = NULL;\n BN_free(group->field_data2);\n group->field_data2 = NULL;\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n mont = BN_MONT_CTX_new();\n if (mont == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, p, ctx)) {\n ECerr(EC_F_EC_GFP_MONT_GROUP_SET_CURVE, ERR_R_BN_LIB);\n goto err;\n }\n one = BN_new();\n if (one == NULL)\n goto err;\n if (!BN_to_montgomery(one, BN_value_one(), mont, ctx))\n goto err;\n group->field_data1 = mont;\n mont = NULL;\n group->field_data2 = one;\n one = NULL;\n ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);\n if (!ret) {\n BN_MONT_CTX_free(group->field_data1);\n group->field_data1 = NULL;\n BN_free(group->field_data2);\n group->field_data2 = NULL;\n }\n err:\n BN_CTX_free(new_ctx);\n BN_MONT_CTX_free(mont);\n return ret;\n}', 'int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)\n{\n int ret = 0;\n BIGNUM *Ri, *R;\n if (BN_is_zero(mod))\n return 0;\n BN_CTX_start(ctx);\n if ((Ri = BN_CTX_get(ctx)) == NULL)\n goto err;\n R = &(mont->RR);\n if (!BN_copy(&(mont->N), mod))\n goto err;\n mont->N.neg = 0;\n#ifdef MONT_WORD\n {\n BIGNUM tmod;\n BN_ULONG buf[2];\n bn_init(&tmod);\n tmod.d = buf;\n tmod.dmax = 2;\n tmod.neg = 0;\n 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}', 'int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,\n BN_CTX *ctx)\n{\n return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);\n}', 'int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n BN_MONT_CTX *mont, BN_CTX *ctx)\n{\n BIGNUM *tmp;\n int ret = 0;\n#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\n int num = mont->N.top;\n if (num > 1 && a->top == num && b->top == num) {\n if (bn_wexpand(r, num) == NULL)\n return (0);\n if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {\n r->neg = a->neg ^ b->neg;\n r->top = num;\n bn_correct_top(r);\n return (1);\n }\n }\n#endif\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n if (tmp == NULL)\n goto err;\n bn_check_top(tmp);\n if (a == b) {\n if (!BN_sqr(tmp, a, ctx))\n goto err;\n } else {\n if (!BN_mul(tmp, a, b, ctx))\n goto err;\n }\n#ifdef MONT_WORD\n if (!BN_from_montgomery_word(r, tmp, mont))\n goto err;\n#else\n if (!BN_from_montgomery(r, tmp, mont, ctx))\n goto err;\n#endif\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return (ret);\n}', 'static int BN_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont)\n{\n BIGNUM *n;\n BN_ULONG *ap, *np, *rp, n0, v, carry;\n int nl, max, i;\n n = &(mont->N);\n nl = n->top;\n if (nl == 0) {\n ret->top = 0;\n return (1);\n }\n max = (2 * nl);\n if (bn_wexpand(r, max) == NULL)\n return (0);\n r->neg ^= n->neg;\n np = n->d;\n rp = r->d;\n i = max - r->top;\n if (i)\n memset(&rp[r->top], 0, sizeof(*rp) * i);\n r->top = max;\n n0 = mont->n0[0];\n for (carry = 0, i = 0; i < nl; i++, rp++) {\n v = bn_mul_add_words(rp, np, nl, (rp[0] * n0) & BN_MASK2);\n v = (v + carry + rp[nl]) & BN_MASK2;\n carry |= (v != rp[nl]);\n carry &= (v <= rp[nl]);\n rp[nl] = v;\n }\n if (bn_wexpand(ret, nl) == NULL)\n return (0);\n ret->top = nl;\n ret->neg = r->neg;\n rp = ret->d;\n ap = &(r->d[nl]);\n# define BRANCH_FREE 1\n# if BRANCH_FREE\n {\n BN_ULONG *nrp;\n size_t m;\n v = bn_sub_words(rp, ap, np, nl) - carry;\n m = (0 - (size_t)v);\n nrp =\n (BN_ULONG *)(((PTR_SIZE_INT) rp & ~m) | ((PTR_SIZE_INT) ap & m));\n for (i = 0, nl -= 4; i < nl; i += 4) {\n BN_ULONG t1, t2, t3, t4;\n t1 = nrp[i + 0];\n t2 = nrp[i + 1];\n t3 = nrp[i + 2];\n ap[i + 0] = 0;\n t4 = nrp[i + 3];\n ap[i + 1] = 0;\n rp[i + 0] = t1;\n ap[i + 2] = 0;\n rp[i + 1] = t2;\n ap[i + 3] = 0;\n rp[i + 2] = t3;\n rp[i + 3] = t4;\n }\n for (nl += 4; i < nl; i++)\n rp[i] = nrp[i], ap[i] = 0;\n }\n# else\n if (bn_sub_words(rp, ap, np, nl) - carry)\n memcpy(rp, ap, nl * sizeof(BN_ULONG));\n# endif\n bn_correct_top(r);\n bn_correct_top(ret);\n bn_check_top(ret);\n return (1);\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}'] |
35,427 | 0 | https://github.com/openssl/openssl/blob/8ae173bb57819a23717fd3c8e7c51cb62f4268d0/crypto/bn/bn_ctx.c/#L300 | static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
} | ['int rsa_check_crt_components(const RSA *rsa, BN_CTX *ctx)\n{\n int ret = 0;\n BIGNUM *r = NULL, *p1 = NULL, *q1 = NULL;\n if (rsa->dmp1 == NULL || rsa->dmq1 == NULL || rsa->iqmp == NULL) {\n if (rsa->dmp1 != NULL || rsa->dmq1 != NULL || rsa->iqmp != NULL)\n return 0;\n return 1;\n }\n BN_CTX_start(ctx);\n r = BN_CTX_get(ctx);\n p1 = BN_CTX_get(ctx);\n q1 = BN_CTX_get(ctx);\n ret = (q1 != NULL)\n && (BN_copy(p1, rsa->p) != NULL)\n && BN_sub_word(p1, 1)\n && (BN_copy(q1, rsa->q) != NULL)\n && BN_sub_word(q1, 1)\n && (BN_cmp(rsa->dmp1, BN_value_one()) > 0)\n && (BN_cmp(rsa->dmp1, p1) < 0)\n && (BN_cmp(rsa->dmq1, BN_value_one()) > 0)\n && (BN_cmp(rsa->dmq1, q1) < 0)\n && (BN_cmp(rsa->iqmp, BN_value_one()) > 0)\n && (BN_cmp(rsa->iqmp, rsa->p) < 0)\n && BN_mod_mul(r, rsa->dmp1, rsa->e, p1, ctx)\n && BN_is_one(r)\n && BN_mod_mul(r, rsa->dmq1, rsa->e, q1, ctx)\n && BN_is_one(r)\n && BN_mod_mul(r, rsa->iqmp, rsa->q, rsa->p, ctx)\n && BN_is_one(r);\n BN_clear(p1);\n BN_clear(q1);\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_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}', '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_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int ret = bn_sqr_fixed_top(r, a, ctx);\n bn_correct_top(r);\n bn_check_top(r);\n return ret;\n}', 'int bn_sqr_fixed_top(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int max, al;\n int ret = 0;\n BIGNUM *tmp, *rr;\n bn_check_top(a);\n al = a->top;\n if (al <= 0) {\n r->top = 0;\n r->neg = 0;\n return 1;\n }\n BN_CTX_start(ctx);\n rr = (a != r) ? r : BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (rr == NULL || tmp == NULL)\n goto err;\n max = 2 * al;\n if (bn_wexpand(rr, max) == NULL)\n goto err;\n if (al == 4) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[8];\n bn_sqr_normal(rr->d, a->d, 4, t);\n#else\n bn_sqr_comba4(rr->d, a->d);\n#endif\n } else if (al == 8) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[16];\n bn_sqr_normal(rr->d, a->d, 8, t);\n#else\n bn_sqr_comba8(rr->d, a->d);\n#endif\n } else {\n#if defined(BN_RECURSION)\n if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {\n BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];\n bn_sqr_normal(rr->d, a->d, al, t);\n } else {\n int j, k;\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n if (al == j) {\n if (bn_wexpand(tmp, k * 2) == NULL)\n goto err;\n bn_sqr_recursive(rr->d, a->d, al, tmp->d);\n } else {\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n }\n }\n#else\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n#endif\n }\n rr->neg = 0;\n rr->top = max;\n rr->flags |= BN_FLG_FIXED_TOP;\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(rr);\n bn_check_top(tmp);\n BN_CTX_end(ctx);\n return ret;\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}'] |
35,428 | 0 | https://github.com/openssl/openssl/blob/a8140a42f5ee9e4e1423b5b6b319dc4657659f6f/crypto/bn/bn_conv.c/#L141 | int BN_hex2bn(BIGNUM **bn, const char *a)
{
BIGNUM *ret = NULL;
BN_ULONG l = 0;
int neg = 0, h, m, i, j, k, c;
int num;
if (a == NULL || *a == '\0')
return 0;
if (*a == '-') {
neg = 1;
a++;
}
for (i = 0; i <= INT_MAX / 4 && ossl_isxdigit(a[i]); i++)
continue;
if (i == 0 || i > INT_MAX / 4)
goto err;
num = i + neg;
if (bn == NULL)
return num;
if (*bn == NULL) {
if ((ret = BN_new()) == NULL)
return 0;
} else {
ret = *bn;
BN_zero(ret);
}
if (bn_expand(ret, i * 4) == NULL)
goto err;
j = i;
m = 0;
h = 0;
while (j > 0) {
m = (BN_BYTES * 2 <= j) ? BN_BYTES * 2 : j;
l = 0;
for (;;) {
c = a[j - m];
k = OPENSSL_hexchar2int(c);
if (k < 0)
k = 0;
l = (l << 4) | k;
if (--m <= 0) {
ret->d[h++] = l;
break;
}
}
j -= BN_BYTES * 2;
}
ret->top = h;
bn_correct_top(ret);
*bn = ret;
bn_check_top(ret);
if (ret->top != 0)
ret->neg = neg;
return num;
err:
if (*bn == NULL)
BN_free(ret);
return 0;
} | ['static int test_hex2bn(void)\n{\n BIGNUM *bn = NULL;\n int st = 0;\n if (!TEST_int_eq(parseBN(&bn, "0"), 1)\n || !TEST_BN_eq_zero(bn)\n || !TEST_BN_ge_zero(bn)\n || !TEST_BN_even(bn))\n goto err;\n BN_free(bn);\n bn = NULL;\n if (!TEST_int_eq(parseBN(&bn, "256"), 3)\n || !TEST_BN_eq_word(bn, 0x256)\n || !TEST_BN_ge_zero(bn)\n || !TEST_BN_gt_zero(bn)\n || !TEST_BN_ne_zero(bn)\n || !TEST_BN_even(bn))\n goto err;\n BN_free(bn);\n bn = NULL;\n if (!TEST_int_eq(parseBN(&bn, "-42"), 3)\n || !TEST_BN_abs_eq_word(bn, 0x42)\n || !TEST_BN_lt_zero(bn)\n || !TEST_BN_le_zero(bn)\n || !TEST_BN_ne_zero(bn)\n || !TEST_BN_even(bn))\n goto err;\n BN_free(bn);\n bn = NULL;\n if (!TEST_int_eq(parseBN(&bn, "cb"), 2)\n || !TEST_BN_eq_word(bn, 0xCB)\n || !TEST_BN_ge_zero(bn)\n || !TEST_BN_gt_zero(bn)\n || !TEST_BN_ne_zero(bn)\n || !TEST_BN_odd(bn))\n goto err;\n BN_free(bn);\n bn = NULL;\n if (!TEST_int_eq(parseBN(&bn, "-0"), 2)\n || !TEST_BN_eq_zero(bn)\n || !TEST_BN_ge_zero(bn)\n || !TEST_BN_le_zero(bn)\n || !TEST_BN_even(bn))\n goto err;\n BN_free(bn);\n bn = NULL;\n if (!TEST_int_eq(parseBN(&bn, "abctrailing garbage is ignored"), 3)\n || !TEST_BN_eq_word(bn, 0xabc)\n || !TEST_BN_ge_zero(bn)\n || !TEST_BN_gt_zero(bn)\n || !TEST_BN_ne_zero(bn)\n || !TEST_BN_even(bn))\n goto err;\n st = 1;\n err:\n BN_free(bn);\n return st;\n}', 'static int parseBN(BIGNUM **out, const char *in)\n{\n *out = NULL;\n return BN_hex2bn(out, in);\n}', "int BN_hex2bn(BIGNUM **bn, const char *a)\n{\n BIGNUM *ret = NULL;\n BN_ULONG l = 0;\n int neg = 0, h, m, i, j, k, c;\n int num;\n if (a == NULL || *a == '\\0')\n return 0;\n if (*a == '-') {\n neg = 1;\n a++;\n }\n for (i = 0; i <= INT_MAX / 4 && ossl_isxdigit(a[i]); i++)\n continue;\n if (i == 0 || i > INT_MAX / 4)\n goto err;\n num = i + neg;\n if (bn == NULL)\n return num;\n if (*bn == NULL) {\n if ((ret = BN_new()) == NULL)\n return 0;\n } else {\n ret = *bn;\n BN_zero(ret);\n }\n if (bn_expand(ret, i * 4) == NULL)\n goto err;\n j = i;\n m = 0;\n h = 0;\n while (j > 0) {\n m = (BN_BYTES * 2 <= j) ? BN_BYTES * 2 : j;\n l = 0;\n for (;;) {\n c = a[j - m];\n k = OPENSSL_hexchar2int(c);\n if (k < 0)\n k = 0;\n l = (l << 4) | k;\n if (--m <= 0) {\n ret->d[h++] = l;\n break;\n }\n }\n j -= BN_BYTES * 2;\n }\n ret->top = h;\n bn_correct_top(ret);\n *bn = ret;\n bn_check_top(ret);\n if (ret->top != 0)\n ret->neg = neg;\n return num;\n err:\n if (*bn == NULL)\n BN_free(ret);\n return 0;\n}"] |
35,429 | 0 | https://gitlab.com/libtiff/libtiff/blob/9c243a11a35d118d00ef8947d244ac55c4145640/tools/fax2tiff.c/#L387 | int
copyFaxFile(TIFF* tifin, TIFF* tifout)
{
uint32 row;
uint32 linesize = TIFFhowmany8(xsize);
uint16 badrun;
int ok;
tifin->tif_rawdatasize = (tmsize_t)TIFFGetFileSize(tifin);
if (tifin->tif_rawdatasize == 0) {
TIFFError(tifin->tif_name, "Empty input file");
return (0);
}
tifin->tif_rawdata = _TIFFmalloc(tifin->tif_rawdatasize);
if (tifin->tif_rawdata == NULL) {
TIFFError(tifin->tif_name, "Not enough memory");
return (0);
}
if (!ReadOK(tifin, tifin->tif_rawdata, tifin->tif_rawdatasize)) {
TIFFError(tifin->tif_name, "Read error at scanline 0");
return (0);
}
tifin->tif_rawcp = tifin->tif_rawdata;
tifin->tif_rawcc = tifin->tif_rawdatasize;
(*tifin->tif_setupdecode)(tifin);
(*tifin->tif_predecode)(tifin, (tsample_t) 0);
tifin->tif_row = 0;
badfaxlines = 0;
badfaxrun = 0;
_TIFFmemset(refbuf, 0, linesize);
row = 0;
badrun = 0;
while (tifin->tif_rawcc > 0) {
ok = (*tifin->tif_decoderow)(tifin, (tdata_t) rowbuf,
linesize, 0);
if (!ok) {
badfaxlines++;
badrun++;
_TIFFmemcpy(rowbuf, refbuf, linesize);
} else {
if (badrun > badfaxrun)
badfaxrun = badrun;
badrun = 0;
_TIFFmemcpy(refbuf, rowbuf, linesize);
}
tifin->tif_row++;
if (TIFFWriteScanline(tifout, rowbuf, row, 0) < 0) {
fprintf(stderr, "%s: Write error at row %ld.\n",
tifout->tif_name, (long) row);
break;
}
row++;
if (stretch) {
if (TIFFWriteScanline(tifout, rowbuf, row, 0) < 0) {
fprintf(stderr, "%s: Write error at row %ld.\n",
tifout->tif_name, (long) row);
break;
}
row++;
}
}
if (badrun > badfaxrun)
badfaxrun = badrun;
_TIFFfree(tifin->tif_rawdata);
return (row);
} | ['int\ncopyFaxFile(TIFF* tifin, TIFF* tifout)\n{\n\tuint32 row;\n\tuint32 linesize = TIFFhowmany8(xsize);\n\tuint16 badrun;\n\tint ok;\n\ttifin->tif_rawdatasize = (tmsize_t)TIFFGetFileSize(tifin);\n\tif (tifin->tif_rawdatasize == 0) {\n\t\tTIFFError(tifin->tif_name, "Empty input file");\n\t\treturn (0);\n\t}\n\ttifin->tif_rawdata = _TIFFmalloc(tifin->tif_rawdatasize);\n\tif (tifin->tif_rawdata == NULL) {\n\t\tTIFFError(tifin->tif_name, "Not enough memory");\n\t\treturn (0);\n\t}\n\tif (!ReadOK(tifin, tifin->tif_rawdata, tifin->tif_rawdatasize)) {\n\t\tTIFFError(tifin->tif_name, "Read error at scanline 0");\n\t\treturn (0);\n\t}\n\ttifin->tif_rawcp = tifin->tif_rawdata;\n\ttifin->tif_rawcc = tifin->tif_rawdatasize;\n\t(*tifin->tif_setupdecode)(tifin);\n\t(*tifin->tif_predecode)(tifin, (tsample_t) 0);\n\ttifin->tif_row = 0;\n\tbadfaxlines = 0;\n\tbadfaxrun = 0;\n\t_TIFFmemset(refbuf, 0, linesize);\n\trow = 0;\n\tbadrun = 0;\n\twhile (tifin->tif_rawcc > 0) {\n\t\tok = (*tifin->tif_decoderow)(tifin, (tdata_t) rowbuf,\n\t\t\t\t\t linesize, 0);\n\t\tif (!ok) {\n\t\t\tbadfaxlines++;\n\t\t\tbadrun++;\n\t\t\t_TIFFmemcpy(rowbuf, refbuf, linesize);\n\t\t} else {\n\t\t\tif (badrun > badfaxrun)\n\t\t\t\tbadfaxrun = badrun;\n\t\t\tbadrun = 0;\n\t\t\t_TIFFmemcpy(refbuf, rowbuf, linesize);\n\t\t}\n\t\ttifin->tif_row++;\n\t\tif (TIFFWriteScanline(tifout, rowbuf, row, 0) < 0) {\n\t\t\tfprintf(stderr, "%s: Write error at row %ld.\\n",\n\t\t\t tifout->tif_name, (long) row);\n\t\t\tbreak;\n\t\t}\n\t\trow++;\n\t\tif (stretch) {\n\t\t\tif (TIFFWriteScanline(tifout, rowbuf, row, 0) < 0) {\n\t\t\t\tfprintf(stderr, "%s: Write error at row %ld.\\n",\n\t\t\t\t tifout->tif_name, (long) row);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\trow++;\n\t\t}\n\t}\n\tif (badrun > badfaxrun)\n\t\tbadfaxrun = badrun;\n\t_TIFFfree(tifin->tif_rawdata);\n\treturn (row);\n}', 'void*\n_TIFFmalloc(tmsize_t s)\n{\n if (s == 0)\n return ((void *) NULL);\n\treturn (malloc((size_t) s));\n}'] |
35,430 | 0 | https://github.com/libav/libav/blob/10397215aa5682494138c300866f03bb9b75f062/libavformat/rmdec.c/#L820 | int
ff_rm_retrieve_cache (AVFormatContext *s, AVIOContext *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, AVIOContext *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(size_t size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-32) )\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size+32);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&31) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr,32,size))\n ptr = NULL;\n#elif HAVE_MEMALIGN\n ptr = memalign(32,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}', 'void av_init_packet(AVPacket *pkt)\n{\n pkt->pts = AV_NOPTS_VALUE;\n pkt->dts = AV_NOPTS_VALUE;\n pkt->pos = -1;\n pkt->duration = 0;\n pkt->convergence_duration = 0;\n pkt->flags = 0;\n pkt->stream_index = 0;\n pkt->destruct= NULL;\n pkt->side_data = NULL;\n pkt->side_data_elems = 0;\n}'] |
35,431 | 0 | https://github.com/openssl/openssl/blob/0bde1089f895718db2fe2637fda4a0c2ed6df904/crypto/lhash/lhash.c/#L356 | static void contract(LHASH *lh)
{
LHASH_NODE **n,*n1,*np;
np=lh->b[lh->p+lh->pmax-1];
lh->b[lh->p+lh->pmax-1]=NULL;
if (lh->p == 0)
{
n=(LHASH_NODE **)Realloc(lh->b,
(unsigned int)(sizeof(LHASH_NODE *)*lh->pmax));
if (n == NULL)
{
lh->error++;
return;
}
lh->num_contract_reallocs++;
lh->num_alloc_nodes/=2;
lh->pmax/=2;
lh->p=lh->pmax-1;
lh->b=n;
}
else
lh->p--;
lh->num_nodes--;
lh->num_contracts++;
n1=lh->b[(int)lh->p];
if (n1 == NULL)
lh->b[(int)lh->p]=np;
else
{
while (n1->next != NULL)
n1=n1->next;
n1->next=np;
}
} | ['int ssl3_connect(SSL *s)\n\t{\n\tBUF_MEM *buf;\n\tunsigned long Time=time(NULL),l;\n\tlong num1;\n\tvoid (*cb)()=NULL;\n\tint ret= -1;\n\tint new_state,state,skip=0;;\n\tRAND_add(&Time,sizeof(Time),0);\n\tERR_clear_error();\n\tclear_sys_error();\n\tif (s->info_callback != NULL)\n\t\tcb=s->info_callback;\n\telse if (s->ctx->info_callback != NULL)\n\t\tcb=s->ctx->info_callback;\n\tif (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);\n\ts->in_handshake++;\n\tfor (;;)\n\t\t{\n\t\tstate=s->state;\n\t\tswitch(s->state)\n\t\t\t{\n\t\tcase SSL_ST_RENEGOTIATE:\n\t\t\ts->new_session=1;\n\t\t\ts->state=SSL_ST_CONNECT;\n\t\t\ts->ctx->stats.sess_connect_renegotiate++;\n\t\tcase SSL_ST_BEFORE:\n\t\tcase SSL_ST_CONNECT:\n\t\tcase SSL_ST_BEFORE|SSL_ST_CONNECT:\n\t\tcase SSL_ST_OK|SSL_ST_CONNECT:\n\t\t\ts->server=0;\n\t\t\tif (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);\n\t\t\tif ((s->version & 0xff00 ) != 0x0300)\n\t\t\t\tabort();\n\t\t\ts->type=SSL_ST_CONNECT;\n\t\t\tif (s->init_buf == NULL)\n\t\t\t\t{\n\t\t\t\tif ((buf=BUF_MEM_new()) == NULL)\n\t\t\t\t\t{\n\t\t\t\t\tret= -1;\n\t\t\t\t\tgoto end;\n\t\t\t\t\t}\n\t\t\t\tif (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))\n\t\t\t\t\t{\n\t\t\t\t\tret= -1;\n\t\t\t\t\tgoto end;\n\t\t\t\t\t}\n\t\t\t\ts->init_buf=buf;\n\t\t\t\t}\n\t\t\tif (!ssl3_setup_buffers(s)) { ret= -1; goto end; }\n\t\t\tif (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }\n\t\t\tssl3_init_finished_mac(s);\n\t\t\ts->state=SSL3_ST_CW_CLNT_HELLO_A;\n\t\t\ts->ctx->stats.sess_connect++;\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_CW_CLNT_HELLO_A:\n\t\tcase SSL3_ST_CW_CLNT_HELLO_B:\n\t\t\ts->shutdown=0;\n\t\t\tret=ssl3_client_hello(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->state=SSL3_ST_CR_SRVR_HELLO_A;\n\t\t\ts->init_num=0;\n\t\t\tif (s->bbio != s->wbio)\n\t\t\t\ts->wbio=BIO_push(s->bbio,s->wbio);\n\t\t\tbreak;\n\t\tcase SSL3_ST_CR_SRVR_HELLO_A:\n\t\tcase SSL3_ST_CR_SRVR_HELLO_B:\n\t\t\tret=ssl3_get_server_hello(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\tif (s->hit)\n\t\t\t\ts->state=SSL3_ST_CR_FINISHED_A;\n\t\t\telse\n\t\t\t\ts->state=SSL3_ST_CR_CERT_A;\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_CR_CERT_A:\n\t\tcase SSL3_ST_CR_CERT_B:\n\t\t\tif (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))\n\t\t\t\t{\n\t\t\t\tret=ssl3_get_server_certificate(s);\n\t\t\t\tif (ret <= 0) goto end;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\tskip=1;\n\t\t\ts->state=SSL3_ST_CR_KEY_EXCH_A;\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_CR_KEY_EXCH_A:\n\t\tcase SSL3_ST_CR_KEY_EXCH_B:\n\t\t\tret=ssl3_get_key_exchange(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->state=SSL3_ST_CR_CERT_REQ_A;\n\t\t\ts->init_num=0;\n\t\t\tif (!ssl3_check_cert_and_algorithm(s))\n\t\t\t\t{\n\t\t\t\tret= -1;\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\tbreak;\n\t\tcase SSL3_ST_CR_CERT_REQ_A:\n\t\tcase SSL3_ST_CR_CERT_REQ_B:\n\t\t\tret=ssl3_get_certificate_request(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->state=SSL3_ST_CR_SRVR_DONE_A;\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_CR_SRVR_DONE_A:\n\t\tcase SSL3_ST_CR_SRVR_DONE_B:\n\t\t\tret=ssl3_get_server_done(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\tif (s->s3->tmp.cert_req)\n\t\t\t\ts->state=SSL3_ST_CW_CERT_A;\n\t\t\telse\n\t\t\t\ts->state=SSL3_ST_CW_KEY_EXCH_A;\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_CW_CERT_A:\n\t\tcase SSL3_ST_CW_CERT_B:\n\t\tcase SSL3_ST_CW_CERT_C:\n\t\tcase SSL3_ST_CW_CERT_D:\n\t\t\tret=ssl3_send_client_certificate(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->state=SSL3_ST_CW_KEY_EXCH_A;\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_CW_KEY_EXCH_A:\n\t\tcase SSL3_ST_CW_KEY_EXCH_B:\n\t\t\tret=ssl3_send_client_key_exchange(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\tl=s->s3->tmp.new_cipher->algorithms;\n\t\t\tif (s->s3->tmp.cert_req == 1)\n\t\t\t\t{\n\t\t\t\ts->state=SSL3_ST_CW_CERT_VRFY_A;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\ts->state=SSL3_ST_CW_CHANGE_A;\n\t\t\t\ts->s3->change_cipher_spec=0;\n\t\t\t\t}\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_CW_CERT_VRFY_A:\n\t\tcase SSL3_ST_CW_CERT_VRFY_B:\n\t\t\tret=ssl3_send_client_verify(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->state=SSL3_ST_CW_CHANGE_A;\n\t\t\ts->init_num=0;\n\t\t\ts->s3->change_cipher_spec=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_CW_CHANGE_A:\n\t\tcase SSL3_ST_CW_CHANGE_B:\n\t\t\tret=ssl3_send_change_cipher_spec(s,\n\t\t\t\tSSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->state=SSL3_ST_CW_FINISHED_A;\n\t\t\ts->init_num=0;\n\t\t\ts->session->cipher=s->s3->tmp.new_cipher;\n\t\t\tif (s->s3->tmp.new_compression == NULL)\n\t\t\t\ts->session->compress_meth=0;\n\t\t\telse\n\t\t\t\ts->session->compress_meth=\n\t\t\t\t\ts->s3->tmp.new_compression->id;\n\t\t\tif (!s->method->ssl3_enc->setup_key_block(s))\n\t\t\t\t{\n\t\t\t\tret= -1;\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\tif (!s->method->ssl3_enc->change_cipher_state(s,\n\t\t\t\tSSL3_CHANGE_CIPHER_CLIENT_WRITE))\n\t\t\t\t{\n\t\t\t\tret= -1;\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\tbreak;\n\t\tcase SSL3_ST_CW_FINISHED_A:\n\t\tcase SSL3_ST_CW_FINISHED_B:\n\t\t\tret=ssl3_send_finished(s,\n\t\t\t\tSSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,\n\t\t\t\ts->method->ssl3_enc->client_finished_label,\n\t\t\t\ts->method->ssl3_enc->client_finished_label_len);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->state=SSL3_ST_CW_FLUSH;\n\t\t\ts->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;\n\t\t\tif (s->hit)\n\t\t\t\t{\n\t\t\t\ts->s3->tmp.next_state=SSL_ST_OK;\n\t\t\t\tif (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)\n\t\t\t\t\t{\n\t\t\t\t\ts->state=SSL_ST_OK;\n\t\t\t\t\ts->s3->flags|=SSL3_FLAGS_POP_BUFFER;\n\t\t\t\t\ts->s3->delay_buf_pop_ret=0;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\ts->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;\n\t\t\t\t}\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_CR_FINISHED_A:\n\t\tcase SSL3_ST_CR_FINISHED_B:\n\t\t\tret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,\n\t\t\t\tSSL3_ST_CR_FINISHED_B);\n\t\t\tif (ret <= 0) goto end;\n\t\t\tif (s->hit)\n\t\t\t\ts->state=SSL3_ST_CW_CHANGE_A;\n\t\t\telse\n\t\t\t\ts->state=SSL_ST_OK;\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_CW_FLUSH:\n\t\t\tnum1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);\n\t\t\tif (num1 > 0)\n\t\t\t\t{\n\t\t\t\ts->rwstate=SSL_WRITING;\n\t\t\t\tnum1=BIO_flush(s->wbio);\n\t\t\t\tif (num1 <= 0) { ret= -1; goto end; }\n\t\t\t\ts->rwstate=SSL_NOTHING;\n\t\t\t\t}\n\t\t\ts->state=s->s3->tmp.next_state;\n\t\t\tbreak;\n\t\tcase SSL_ST_OK:\n\t\t\tssl3_cleanup_key_block(s);\n\t\t\tif (s->init_buf != NULL)\n\t\t\t\t{\n\t\t\t\tBUF_MEM_free(s->init_buf);\n\t\t\t\ts->init_buf=NULL;\n\t\t\t\t}\n\t\t\tif (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))\n\t\t\t\tssl_free_wbio_buffer(s);\n\t\t\ts->init_num=0;\n\t\t\ts->new_session=0;\n\t\t\tssl_update_cache(s,SSL_SESS_CACHE_CLIENT);\n\t\t\tif (s->hit) s->ctx->stats.sess_hit++;\n\t\t\tret=1;\n\t\t\ts->handshake_func=ssl3_connect;\n\t\t\ts->ctx->stats.sess_connect_good++;\n\t\t\tif (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);\n\t\t\tgoto end;\n\t\tdefault:\n\t\t\tSSLerr(SSL_F_SSL3_CONNECT,SSL_R_UNKNOWN_STATE);\n\t\t\tret= -1;\n\t\t\tgoto end;\n\t\t\t}\n\t\tif (!s->s3->tmp.reuse_message && !skip)\n\t\t\t{\n\t\t\tif (s->debug)\n\t\t\t\t{\n\t\t\t\tif ((ret=BIO_flush(s->wbio)) <= 0)\n\t\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\tif ((cb != NULL) && (s->state != state))\n\t\t\t\t{\n\t\t\t\tnew_state=s->state;\n\t\t\t\ts->state=state;\n\t\t\t\tcb(s,SSL_CB_CONNECT_LOOP,1);\n\t\t\t\ts->state=new_state;\n\t\t\t\t}\n\t\t\t}\n\t\tskip=0;\n\t\t}\nend:\n\tif (cb != NULL)\n\t\tcb(s,SSL_CB_CONNECT_EXIT,ret);\n\ts->in_handshake--;\n\treturn(ret);\n\t}', 'static int ssl3_get_key_exchange(SSL *s)\n\t{\n#ifndef NO_RSA\n\tunsigned char *q,md_buf[EVP_MAX_MD_SIZE*2];\n#endif\n\tEVP_MD_CTX md_ctx;\n\tunsigned char *param,*p;\n\tint al,i,j,param_len,ok;\n\tlong n,alg;\n\tEVP_PKEY *pkey=NULL;\n#ifndef NO_RSA\n\tRSA *rsa=NULL;\n#endif\n#ifndef NO_DH\n\tDH *dh=NULL;\n#endif\n\tn=ssl3_get_message(s,\n\t\tSSL3_ST_CR_KEY_EXCH_A,\n\t\tSSL3_ST_CR_KEY_EXCH_B,\n\t\t-1,\n\t\t1024*8,\n\t\t&ok);\n\tif (!ok) return((int)n);\n\tif (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)\n\t\t{\n\t\ts->s3->tmp.reuse_message=1;\n\t\treturn(1);\n\t\t}\n\tparam=p=(unsigned char *)s->init_buf->data;\n\tif (s->session->sess_cert != NULL)\n\t\t{\n#ifndef NO_RSA\n\t\tif (s->session->sess_cert->peer_rsa_tmp != NULL)\n\t\t\t{\n\t\t\tRSA_free(s->session->sess_cert->peer_rsa_tmp);\n\t\t\ts->session->sess_cert->peer_rsa_tmp=NULL;\n\t\t\t}\n#endif\n#ifndef NO_DH\n\t\tif (s->session->sess_cert->peer_dh_tmp)\n\t\t\t{\n\t\t\tDH_free(s->session->sess_cert->peer_dh_tmp);\n\t\t\ts->session->sess_cert->peer_dh_tmp=NULL;\n\t\t\t}\n#endif\n\t\t}\n\telse\n\t\t{\n\t\ts->session->sess_cert=ssl_sess_cert_new();\n\t\t}\n\tparam_len=0;\n\talg=s->s3->tmp.new_cipher->algorithms;\n#ifndef NO_RSA\n\tif (alg & SSL_kRSA)\n\t\t{\n\t\tif ((rsa=RSA_new()) == NULL)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);\n\t\t\tgoto err;\n\t\t\t}\n\t\tn2s(p,i);\n\t\tparam_len=i+2;\n\t\tif (param_len > n)\n\t\t\t{\n\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_MODULUS_LENGTH);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tif (!(rsa->n=BN_bin2bn(p,i,rsa->n)))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);\n\t\t\tgoto err;\n\t\t\t}\n\t\tp+=i;\n\t\tn2s(p,i);\n\t\tparam_len+=i+2;\n\t\tif (param_len > n)\n\t\t\t{\n\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_E_LENGTH);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tif (!(rsa->e=BN_bin2bn(p,i,rsa->e)))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);\n\t\t\tgoto err;\n\t\t\t}\n\t\tp+=i;\n\t\tn-=param_len;\n\t\tif (alg & SSL_aRSA)\n\t\t\tpkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);\n\t\telse\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);\n\t\t\tgoto err;\n\t\t\t}\n\t\ts->session->sess_cert->peer_rsa_tmp=rsa;\n\t\trsa=NULL;\n\t\t}\n\telse\n#endif\n#ifndef NO_DH\n\t\tif (alg & SSL_kEDH)\n\t\t{\n\t\tif ((dh=DH_new()) == NULL)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_DH_LIB);\n\t\t\tgoto err;\n\t\t\t}\n\t\tn2s(p,i);\n\t\tparam_len=i+2;\n\t\tif (param_len > n)\n\t\t\t{\n\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_P_LENGTH);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tif (!(dh->p=BN_bin2bn(p,i,NULL)))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);\n\t\t\tgoto err;\n\t\t\t}\n\t\tp+=i;\n\t\tn2s(p,i);\n\t\tparam_len+=i+2;\n\t\tif (param_len > n)\n\t\t\t{\n\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_G_LENGTH);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tif (!(dh->g=BN_bin2bn(p,i,NULL)))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);\n\t\t\tgoto err;\n\t\t\t}\n\t\tp+=i;\n\t\tn2s(p,i);\n\t\tparam_len+=i+2;\n\t\tif (param_len > n)\n\t\t\t{\n\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_PUB_KEY_LENGTH);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tif (!(dh->pub_key=BN_bin2bn(p,i,NULL)))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);\n\t\t\tgoto err;\n\t\t\t}\n\t\tp+=i;\n\t\tn-=param_len;\n#ifndef NO_RSA\n\t\tif (alg & SSL_aRSA)\n\t\t\tpkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);\n\t\telse\n#endif\n#ifndef NO_DSA\n\t\tif (alg & SSL_aDSS)\n\t\t\tpkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509);\n#endif\n\t\ts->session->sess_cert->peer_dh_tmp=dh;\n\t\tdh=NULL;\n\t\t}\n\telse if ((alg & SSL_kDHr) || (alg & SSL_kDHd))\n\t\t{\n\t\tal=SSL_AD_ILLEGAL_PARAMETER;\n\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);\n\t\tgoto f_err;\n\t\t}\n#endif\n\tif (alg & SSL_aFZA)\n\t\t{\n\t\tal=SSL_AD_HANDSHAKE_FAILURE;\n\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);\n\t\tgoto f_err;\n\t\t}\n\tif (pkey != NULL)\n\t\t{\n\t\tn2s(p,i);\n\t\tn-=2;\n\t\tj=EVP_PKEY_size(pkey);\n\t\tif ((i != n) || (n > j) || (n <= 0))\n\t\t\t{\n\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE_LENGTH);\n\t\t\tgoto f_err;\n\t\t\t}\n#ifndef NO_RSA\n\t\tif (pkey->type == EVP_PKEY_RSA)\n\t\t\t{\n\t\t\tint num;\n\t\t\tj=0;\n\t\t\tq=md_buf;\n\t\t\tfor (num=2; num > 0; num--)\n\t\t\t\t{\n\t\t\t\tEVP_DigestInit(&md_ctx,(num == 2)\n\t\t\t\t\t?s->ctx->md5:s->ctx->sha1);\n\t\t\t\tEVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);\n\t\t\t\tEVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);\n\t\t\t\tEVP_DigestUpdate(&md_ctx,param,param_len);\n\t\t\t\tEVP_DigestFinal(&md_ctx,q,(unsigned int *)&i);\n\t\t\t\tq+=i;\n\t\t\t\tj+=i;\n\t\t\t\t}\n\t\t\ti=RSA_verify(NID_md5_sha1, md_buf, j, p, n,\n\t\t\t\t\t\t\t\tpkey->pkey.rsa);\n\t\t\tif (i < 0)\n\t\t\t\t{\n\t\t\t\tal=SSL_AD_DECRYPT_ERROR;\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\tif (i == 0)\n\t\t\t\t{\n\t\t\t\tal=SSL_AD_DECRYPT_ERROR;\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\t}\n\t\telse\n#endif\n#ifndef NO_DSA\n\t\t\tif (pkey->type == EVP_PKEY_DSA)\n\t\t\t{\n\t\t\tEVP_VerifyInit(&md_ctx,EVP_dss1());\n\t\t\tEVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);\n\t\t\tEVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);\n\t\t\tEVP_VerifyUpdate(&md_ctx,param,param_len);\n\t\t\tif (!EVP_VerifyFinal(&md_ctx,p,(int)n,pkey))\n\t\t\t\t{\n\t\t\t\tal=SSL_AD_DECRYPT_ERROR;\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\t}\n\t\telse\n#endif\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);\n\t\t\tgoto err;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tif (!(alg & SSL_aNULL))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (n != 0)\n\t\t\t{\n\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_EXTRA_DATA_IN_MESSAGE);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\t}\n\tEVP_PKEY_free(pkey);\n\treturn(1);\nf_err:\n\tssl3_send_alert(s,SSL3_AL_FATAL,al);\nerr:\n\tEVP_PKEY_free(pkey);\n#ifndef NO_RSA\n\tif (rsa != NULL)\n\t\tRSA_free(rsa);\n#endif\n#ifndef NO_DH\n\tif (dh != NULL)\n\t\tDH_free(dh);\n#endif\n\treturn(-1);\n\t}', 'long ssl3_get_message(SSL *s, int st1, int stn, int mt, long max, int *ok)\n\t{\n\tunsigned char *p;\n\tunsigned long l;\n\tlong n;\n\tint i,al;\n\tif (s->s3->tmp.reuse_message)\n\t\t{\n\t\ts->s3->tmp.reuse_message=0;\n\t\tif ((mt >= 0) && (s->s3->tmp.message_type != mt))\n\t\t\t{\n\t\t\tal=SSL_AD_UNEXPECTED_MESSAGE;\n\t\t\tSSLerr(SSL_F_SSL3_GET_MESSAGE,SSL_R_UNEXPECTED_MESSAGE);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\t*ok=1;\n\t\treturn((int)s->s3->tmp.message_size);\n\t\t}\n\tp=(unsigned char *)s->init_buf->data;\n\tif (s->state == st1)\n\t\t{\n\t\ti=ssl3_read_bytes(s,SSL3_RT_HANDSHAKE,&p[s->init_num],\n\t\t\t\t 4-s->init_num);\n\t\tif (i < (4-s->init_num))\n\t\t\t{\n\t\t\t*ok=0;\n\t\t\treturn(ssl3_part_read(s,i));\n\t\t\t}\n\t\tif ((mt >= 0) && (*p != mt))\n\t\t\t{\n\t\t\tal=SSL_AD_UNEXPECTED_MESSAGE;\n\t\t\tSSLerr(SSL_F_SSL3_GET_MESSAGE,SSL_R_UNEXPECTED_MESSAGE);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tif((mt < 0) && (*p == SSL3_MT_CLIENT_HELLO) &&\n\t\t\t\t\t(st1 == SSL3_ST_SR_CERT_A) &&\n\t\t\t\t\t(stn == SSL3_ST_SR_CERT_B))\n\t\t\t{\n\t\t\tssl3_init_finished_mac(s);\n\t\t\tssl3_finish_mac(s, p + s->init_num, i);\n\t\t\t}\n\t\ts->s3->tmp.message_type= *(p++);\n\t\tn2l3(p,l);\n\t\tif (l > (unsigned long)max)\n\t\t\t{\n\t\t\tal=SSL_AD_ILLEGAL_PARAMETER;\n\t\t\tSSLerr(SSL_F_SSL3_GET_MESSAGE,SSL_R_EXCESSIVE_MESSAGE_SIZE);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tif (l && !BUF_MEM_grow(s->init_buf,(int)l))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_MESSAGE,ERR_R_BUF_LIB);\n\t\t\tgoto err;\n\t\t\t}\n\t\ts->s3->tmp.message_size=l;\n\t\ts->state=stn;\n\t\ts->init_num=0;\n\t\t}\n\tp=(unsigned char *)s->init_buf->data;\n\tn=s->s3->tmp.message_size;\n\tif (n > 0)\n\t\t{\n\t\ti=ssl3_read_bytes(s,SSL3_RT_HANDSHAKE,&p[s->init_num],n);\n\t\tif (i != (int)n)\n\t\t\t{\n\t\t\t*ok=0;\n\t\t\treturn(ssl3_part_read(s,i));\n\t\t\t}\n\t\t}\n\t*ok=1;\n\treturn(n);\nf_err:\n\tssl3_send_alert(s,SSL3_AL_FATAL,al);\nerr:\n\t*ok=0;\n\treturn(-1);\n\t}', '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_C_IS_EXPORT(s->s3->tmp.new_cipher) && !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_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\t}\n\t\telse\n#endif\n#ifndef NO_DH\n\t\t\tif (algs & (SSL_kEDH|SSL_kDHr|SSL_kDHd))\n\t\t\t {\n\t\t\t if (dh == NULL\n\t\t\t\t|| DH_size(dh) > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\t}\n\t\telse\n#endif\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\t}\n\treturn(1);\nf_err:\n\tssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);\nerr:\n\treturn(0);\n\t}', 'void ssl3_send_alert(SSL *s, int level, int desc)\n\t{\n\tdesc=s->method->ssl3_enc->alert_value(desc);\n\tif (desc < 0) return;\n\tif ((level == 2) && (s->session != NULL))\n\t\tSSL_CTX_remove_session(s->ctx,s->session);\n\ts->s3->alert_dispatch=1;\n\ts->s3->send_alert[0]=level;\n\ts->s3->send_alert[1]=desc;\n\tif (s->s3->wbuf.left == 0)\n\t\tssl3_dispatch_alert(s);\n\t}', 'int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)\n{\n\treturn remove_session_lock(ctx, c, 1);\n}', 'static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)\n\t{\n\tSSL_SESSION *r;\n\tint ret=0;\n\tif ((c != NULL) && (c->session_id_length != 0))\n\t\t{\n\t\tif(lck) CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);\n\t\tr=(SSL_SESSION *)lh_delete(ctx->sessions,c);\n\t\tif (r != NULL)\n\t\t\t{\n\t\t\tret=1;\n\t\t\tSSL_SESSION_list_remove(ctx,c);\n\t\t\t}\n\t\tif(lck) CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n\t\tif (ret)\n\t\t\t{\n\t\t\tr->not_resumable=1;\n\t\t\tif (ctx->remove_session_cb != NULL)\n\t\t\t\tctx->remove_session_cb(ctx,r);\n\t\t\tSSL_SESSION_free(r);\n\t\t\t}\n\t\t}\n\telse\n\t\tret=0;\n\treturn(ret);\n\t}', 'void *lh_delete(LHASH *lh, void *data)\n\t{\n\tunsigned long hash;\n\tLHASH_NODE *nn,**rn;\n\tvoid *ret;\n\tlh->error=0;\n\trn=getrn(lh,data,&hash);\n\tif (*rn == NULL)\n\t\t{\n\t\tlh->num_no_delete++;\n\t\treturn(NULL);\n\t\t}\n\telse\n\t\t{\n\t\tnn= *rn;\n\t\t*rn=nn->next;\n\t\tret=nn->data;\n\t\tFree(nn);\n\t\tlh->num_delete++;\n\t\t}\n\tlh->num_items--;\n\tif ((lh->num_nodes > MIN_NODES) &&\n\t\t(lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes)))\n\t\tcontract(lh);\n\treturn(ret);\n\t}', 'static void contract(LHASH *lh)\n\t{\n\tLHASH_NODE **n,*n1,*np;\n\tnp=lh->b[lh->p+lh->pmax-1];\n\tlh->b[lh->p+lh->pmax-1]=NULL;\n\tif (lh->p == 0)\n\t\t{\n\t\tn=(LHASH_NODE **)Realloc(lh->b,\n\t\t\t(unsigned int)(sizeof(LHASH_NODE *)*lh->pmax));\n\t\tif (n == NULL)\n\t\t\t{\n\t\t\tlh->error++;\n\t\t\treturn;\n\t\t\t}\n\t\tlh->num_contract_reallocs++;\n\t\tlh->num_alloc_nodes/=2;\n\t\tlh->pmax/=2;\n\t\tlh->p=lh->pmax-1;\n\t\tlh->b=n;\n\t\t}\n\telse\n\t\tlh->p--;\n\tlh->num_nodes--;\n\tlh->num_contracts++;\n\tn1=lh->b[(int)lh->p];\n\tif (n1 == NULL)\n\t\tlh->b[(int)lh->p]=np;\n\telse\n\t\t{\n\t\twhile (n1->next != NULL)\n\t\t\tn1=n1->next;\n\t\tn1->next=np;\n\t\t}\n\t}'] |
35,432 | 0 | https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/bn/bn_lib.c/#L450 | 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);
} | ['char *i2s_ASN1_INTEGER(X509V3_EXT_METHOD *method, ASN1_INTEGER *a)\n{\n BIGNUM *bntmp = NULL;\n char *strtmp = NULL;\n if (!a)\n return NULL;\n if ((bntmp = ASN1_INTEGER_to_BN(a, NULL)) == NULL\n || (strtmp = BN_bn2dec(bntmp)) == NULL)\n X509V3err(X509V3_F_I2S_ASN1_INTEGER, ERR_R_MALLOC_FAILURE);\n BN_free(bntmp);\n return strtmp;\n}', 'BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn)\n{\n return asn1_string_to_bn(ai, bn, V_ASN1_INTEGER);\n}', 'static BIGNUM *asn1_string_to_bn(const ASN1_INTEGER *ai, BIGNUM *bn,\n int itype)\n{\n BIGNUM *ret;\n if ((ai->type & ~V_ASN1_NEG) != itype) {\n ASN1err(ASN1_F_ASN1_STRING_TO_BN, ASN1_R_WRONG_INTEGER_TYPE);\n return NULL;\n }\n ret = BN_bin2bn(ai->data, ai->length, bn);\n if (ret == 0) {\n ASN1err(ASN1_F_ASN1_STRING_TO_BN, ASN1_R_BN_LIB);\n return NULL;\n }\n if (ai->type & V_ASN1_NEG)\n BN_set_negative(ret, 1);\n return ret;\n}', "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 i = BN_num_bits(a) * 3;\n num = (i / 10 + i / 1000 + 1) + 1;\n bn_data = OPENSSL_malloc((num / BN_DEC_NUM + 1) * 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 i = 0;\n while (!BN_is_zero(t)) {\n *lp = BN_div_word(t, BN_DEC_CONV);\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}'] |
35,433 | 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)];
} | ['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}', 'static int bn_x931_derive_pi(BIGNUM *pi, const BIGNUM *Xpi, BN_CTX *ctx,\n BN_GENCB *cb)\n{\n int i = 0, is_prime;\n if (!BN_copy(pi, Xpi))\n return 0;\n if (!BN_is_odd(pi) && !BN_add_word(pi, 1))\n return 0;\n for (;;) {\n i++;\n BN_GENCB_call(cb, 0, i);\n is_prime = BN_is_prime_fasttest_ex(pi, 27, ctx, 1, cb);\n if (is_prime < 0)\n return 0;\n if (is_prime)\n break;\n if (!BN_add_word(pi, 2))\n return 0;\n }\n BN_GENCB_call(cb, 2, i);\n return 1;\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}'] |
35,434 | 0 | https://github.com/libav/libav/blob/f73467192558cadff476c98c73767ec04e7212c3/libavformat/rtpdec_asf.c/#L115 | int ff_wms_parse_sdp_a_line(AVFormatContext *s, const char *p)
{
int ret = 0;
if (av_strstart(p, "pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,", &p)) {
ByteIOContext pb;
RTSPState *rt = s->priv_data;
int len = strlen(p) * 6 / 8;
char *buf = av_mallocz(len);
av_base64_decode(buf, p, len);
if (rtp_asf_fix_header(buf, len) < 0)
av_log(s, AV_LOG_ERROR,
"Failed to fix invalid RTSP-MS/ASF min_pktsize\n");
init_packetizer(&pb, buf, len);
if (rt->asf_ctx) {
av_close_input_stream(rt->asf_ctx);
rt->asf_ctx = NULL;
}
ret = av_open_input_stream(&rt->asf_ctx, &pb, "", &asf_demuxer, NULL);
if (ret < 0)
return ret;
av_metadata_copy(&s->metadata, rt->asf_ctx->metadata, 0);
rt->asf_pb_pos = url_ftell(&pb);
av_free(buf);
rt->asf_ctx->pb = NULL;
}
return ret;
} | ['int ff_wms_parse_sdp_a_line(AVFormatContext *s, const char *p)\n{\n int ret = 0;\n if (av_strstart(p, "pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,", &p)) {\n ByteIOContext pb;\n RTSPState *rt = s->priv_data;\n int len = strlen(p) * 6 / 8;\n char *buf = av_mallocz(len);\n av_base64_decode(buf, p, len);\n if (rtp_asf_fix_header(buf, len) < 0)\n av_log(s, AV_LOG_ERROR,\n "Failed to fix invalid RTSP-MS/ASF min_pktsize\\n");\n init_packetizer(&pb, buf, len);\n if (rt->asf_ctx) {\n av_close_input_stream(rt->asf_ctx);\n rt->asf_ctx = NULL;\n }\n ret = av_open_input_stream(&rt->asf_ctx, &pb, "", &asf_demuxer, NULL);\n if (ret < 0)\n return ret;\n av_metadata_copy(&s->metadata, rt->asf_ctx->metadata, 0);\n rt->asf_pb_pos = url_ftell(&pb);\n av_free(buf);\n rt->asf_ctx->pb = NULL;\n }\n return ret;\n}', 'void *av_mallocz(FF_INTERNAL_MEM_TYPE size)\n{\n void *ptr = av_malloc(size);\n if (ptr)\n memset(ptr, 0, size);\n return ptr;\n}', 'void *av_malloc(FF_INTERNAL_MEM_TYPE size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-16) )\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size+16);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&15) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr,16,size))\n ptr = NULL;\n#elif HAVE_MEMALIGN\n ptr = memalign(16,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}', 'static void init_packetizer(ByteIOContext *pb, uint8_t *buf, int len)\n{\n init_put_byte(pb, buf, len, 0, NULL, packetizer_read, NULL, NULL);\n pb->pos = len;\n pb->buf_end = buf + len;\n}', 'int init_put_byte(ByteIOContext *s,\n unsigned char *buffer,\n int buffer_size,\n int write_flag,\n void *opaque,\n int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),\n int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),\n int64_t (*seek)(void *opaque, int64_t offset, int whence))\n{\n s->buffer = buffer;\n s->buffer_size = buffer_size;\n s->buf_ptr = buffer;\n s->opaque = opaque;\n url_resetbuf(s, write_flag ? URL_WRONLY : URL_RDONLY);\n s->write_packet = write_packet;\n s->read_packet = read_packet;\n s->seek = seek;\n s->pos = 0;\n s->must_flush = 0;\n s->eof_reached = 0;\n s->error = 0;\n s->is_streamed = 0;\n s->max_packet_size = 0;\n s->update_checksum= NULL;\n if(!read_packet && !write_flag){\n s->pos = buffer_size;\n s->buf_end = s->buffer + buffer_size;\n }\n s->read_pause = NULL;\n s->read_seek = NULL;\n return 0;\n}', 'int url_resetbuf(ByteIOContext *s, int flags)\n#else\nstatic int url_resetbuf(ByteIOContext *s, int flags)\n#endif\n{\n#if FF_API_URL_RESETBUF\n if (flags & URL_RDWR)\n return AVERROR(EINVAL);\n#else\n assert(flags == URL_WRONLY || flags == URL_RDONLY);\n#endif\n if (flags & URL_WRONLY) {\n s->buf_end = s->buffer + s->buffer_size;\n s->write_flag = 1;\n } else {\n s->buf_end = s->buffer;\n s->write_flag = 0;\n }\n return 0;\n}'] |
35,435 | 0 | https://github.com/openssl/openssl/blob/e825109236f6795fbe24c0c6a489ef89ca05a906/crypto/lhash/lhash.c/#L187 | static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,
OPENSSL_LH_DOALL_FUNC func,
OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)
{
int i;
OPENSSL_LH_NODE *a, *n;
if (lh == NULL)
return;
for (i = lh->num_nodes - 1; i >= 0; i--) {
a = lh->b[i];
while (a != NULL) {
n = a->next;
if (use_arg)
func_arg(a->data, arg);
else
func(a->data);
a = n;
}
}
} | ['static int test_tls13_psk(int idx)\n{\n SSL_CTX *sctx = NULL, *cctx = NULL;\n SSL *serverssl = NULL, *clientssl = NULL;\n const SSL_CIPHER *cipher = NULL;\n const unsigned char key[] = {\n 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,\n 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,\n 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,\n 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f\n };\n int testresult = 0;\n if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),\n TLS1_VERSION, TLS_MAX_VERSION,\n &sctx, &cctx, idx == 3 ? NULL : cert,\n idx == 3 ? NULL : privkey)))\n goto end;\n if (idx != 3) {\n if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,\n "TLS_AES_128_GCM_SHA256")))\n goto end;\n }\n if (idx == 0 || idx == 1) {\n SSL_CTX_set_psk_use_session_callback(cctx, use_session_cb);\n SSL_CTX_set_psk_find_session_callback(sctx, find_session_cb);\n }\n#ifndef OPENSSL_NO_PSK\n if (idx >= 1) {\n SSL_CTX_set_psk_client_callback(cctx, psk_client_cb);\n SSL_CTX_set_psk_server_callback(sctx, psk_server_cb);\n }\n#endif\n srvid = pskid;\n use_session_cb_cnt = 0;\n find_session_cb_cnt = 0;\n psk_client_cb_cnt = 0;\n psk_server_cb_cnt = 0;\n if (idx != 3) {\n if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,\n NULL, NULL))\n || !TEST_true(create_ssl_connection(serverssl, clientssl,\n SSL_ERROR_NONE))\n || !TEST_false(SSL_session_reused(clientssl))\n || !TEST_false(SSL_session_reused(serverssl)))\n goto end;\n if (idx == 0 || idx == 1) {\n if (!TEST_true(use_session_cb_cnt == 1)\n || !TEST_true(find_session_cb_cnt == 0)\n || !TEST_true(psk_client_cb_cnt == idx)\n || !TEST_true(psk_server_cb_cnt == 0))\n goto end;\n } else {\n if (!TEST_true(use_session_cb_cnt == 0)\n || !TEST_true(find_session_cb_cnt == 0)\n || !TEST_true(psk_client_cb_cnt == 1)\n || !TEST_true(psk_server_cb_cnt == 0))\n goto end;\n }\n shutdown_ssl_connection(serverssl, clientssl);\n serverssl = clientssl = NULL;\n use_session_cb_cnt = psk_client_cb_cnt = 0;\n }\n if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,\n NULL, NULL)))\n goto end;\n cipher = SSL_CIPHER_find(clientssl, TLS13_AES_128_GCM_SHA256_BYTES);\n clientpsk = SSL_SESSION_new();\n if (!TEST_ptr(clientpsk)\n || !TEST_ptr(cipher)\n || !TEST_true(SSL_SESSION_set1_master_key(clientpsk, key,\n sizeof(key)))\n || !TEST_true(SSL_SESSION_set_cipher(clientpsk, cipher))\n || !TEST_true(SSL_SESSION_set_protocol_version(clientpsk,\n TLS1_3_VERSION))\n || !TEST_true(SSL_SESSION_up_ref(clientpsk)))\n goto end;\n serverpsk = clientpsk;\n if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))\n || !TEST_true(SSL_session_reused(clientssl))\n || !TEST_true(SSL_session_reused(serverssl)))\n goto end;\n if (idx == 0 || idx == 1) {\n if (!TEST_true(use_session_cb_cnt == 1)\n || !TEST_true(find_session_cb_cnt == 1)\n || !TEST_true(psk_client_cb_cnt == 0)\n || !TEST_true(psk_server_cb_cnt == 0))\n goto end;\n } else {\n if (!TEST_true(use_session_cb_cnt == 0)\n || !TEST_true(find_session_cb_cnt == 0)\n || !TEST_true(psk_client_cb_cnt == 1)\n || !TEST_true(psk_server_cb_cnt == 1))\n goto end;\n }\n shutdown_ssl_connection(serverssl, clientssl);\n serverssl = clientssl = NULL;\n use_session_cb_cnt = find_session_cb_cnt = 0;\n psk_client_cb_cnt = psk_server_cb_cnt = 0;\n if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,\n NULL, NULL)))\n goto end;\n if (!TEST_true(SSL_set1_groups_list(serverssl, "P-256")))\n goto end;\n if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))\n || !TEST_true(SSL_session_reused(clientssl))\n || !TEST_true(SSL_session_reused(serverssl)))\n goto end;\n if (idx == 0 || idx == 1) {\n if (!TEST_true(use_session_cb_cnt == 2)\n || !TEST_true(find_session_cb_cnt == 2)\n || !TEST_true(psk_client_cb_cnt == 0)\n || !TEST_true(psk_server_cb_cnt == 0))\n goto end;\n } else {\n if (!TEST_true(use_session_cb_cnt == 0)\n || !TEST_true(find_session_cb_cnt == 0)\n || !TEST_true(psk_client_cb_cnt == 2)\n || !TEST_true(psk_server_cb_cnt == 2))\n goto end;\n }\n shutdown_ssl_connection(serverssl, clientssl);\n serverssl = clientssl = NULL;\n use_session_cb_cnt = find_session_cb_cnt = 0;\n psk_client_cb_cnt = psk_server_cb_cnt = 0;\n if (idx != 3) {\n srvid = "Dummy Identity";\n if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,\n NULL, NULL))\n || !TEST_true(create_ssl_connection(serverssl, clientssl,\n SSL_ERROR_NONE))\n || !TEST_false(SSL_session_reused(clientssl))\n || !TEST_false(SSL_session_reused(serverssl)))\n goto end;\n if (idx == 0 || idx == 1) {\n if (!TEST_true(use_session_cb_cnt == 1)\n || !TEST_true(find_session_cb_cnt == 1)\n || !TEST_true(psk_client_cb_cnt == 0)\n || !TEST_true(psk_server_cb_cnt == idx))\n goto end;\n } else {\n if (!TEST_true(use_session_cb_cnt == 0)\n || !TEST_true(find_session_cb_cnt == 0)\n || !TEST_true(psk_client_cb_cnt == 1)\n || !TEST_true(psk_server_cb_cnt == 1))\n goto end;\n }\n shutdown_ssl_connection(serverssl, clientssl);\n serverssl = clientssl = NULL;\n }\n testresult = 1;\n end:\n SSL_SESSION_free(clientpsk);\n SSL_SESSION_free(serverpsk);\n clientpsk = serverpsk = NULL;\n SSL_free(serverssl);\n SSL_free(clientssl);\n SSL_CTX_free(sctx);\n SSL_CTX_free(cctx);\n return testresult;\n}', 'int create_ssl_objects(SSL_CTX *serverctx, SSL_CTX *clientctx, SSL **sssl,\n SSL **cssl, BIO *s_to_c_fbio, BIO *c_to_s_fbio)\n{\n SSL *serverssl = NULL, *clientssl = NULL;\n BIO *s_to_c_bio = NULL, *c_to_s_bio = NULL;\n if (*sssl != NULL)\n serverssl = *sssl;\n else if (!TEST_ptr(serverssl = SSL_new(serverctx)))\n goto error;\n if (*cssl != NULL)\n clientssl = *cssl;\n else if (!TEST_ptr(clientssl = SSL_new(clientctx)))\n goto error;\n if (SSL_is_dtls(clientssl)) {\n if (!TEST_ptr(s_to_c_bio = BIO_new(bio_s_mempacket_test()))\n || !TEST_ptr(c_to_s_bio = BIO_new(bio_s_mempacket_test())))\n goto error;\n } else {\n if (!TEST_ptr(s_to_c_bio = BIO_new(BIO_s_mem()))\n || !TEST_ptr(c_to_s_bio = BIO_new(BIO_s_mem())))\n goto error;\n }\n if (s_to_c_fbio != NULL\n && !TEST_ptr(s_to_c_bio = BIO_push(s_to_c_fbio, s_to_c_bio)))\n goto error;\n if (c_to_s_fbio != NULL\n && !TEST_ptr(c_to_s_bio = BIO_push(c_to_s_fbio, c_to_s_bio)))\n goto error;\n BIO_set_mem_eof_return(s_to_c_bio, -1);\n BIO_set_mem_eof_return(c_to_s_bio, -1);\n SSL_set_bio(serverssl, c_to_s_bio, s_to_c_bio);\n BIO_up_ref(s_to_c_bio);\n BIO_up_ref(c_to_s_bio);\n SSL_set_bio(clientssl, s_to_c_bio, c_to_s_bio);\n *sssl = serverssl;\n *cssl = clientssl;\n return 1;\n error:\n SSL_free(serverssl);\n SSL_free(clientssl);\n BIO_free(s_to_c_bio);\n BIO_free(c_to_s_bio);\n BIO_free(s_to_c_fbio);\n BIO_free(c_to_s_fbio);\n return 0;\n}', 'SSL *SSL_new(SSL_CTX *ctx)\n{\n SSL *s;\n if (ctx == NULL) {\n SSLerr(SSL_F_SSL_NEW, SSL_R_NULL_SSL_CTX);\n return NULL;\n }\n if (ctx->method == NULL) {\n SSLerr(SSL_F_SSL_NEW, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);\n return NULL;\n }\n s = OPENSSL_zalloc(sizeof(*s));\n if (s == NULL)\n goto err;\n s->references = 1;\n s->lock = CRYPTO_THREAD_lock_new();\n if (s->lock == NULL) {\n OPENSSL_free(s);\n s = NULL;\n goto err;\n }\n RECORD_LAYER_init(&s->rlayer, s);\n s->options = ctx->options;\n s->dane.flags = ctx->dane.flags;\n s->min_proto_version = ctx->min_proto_version;\n s->max_proto_version = ctx->max_proto_version;\n s->mode = ctx->mode;\n s->max_cert_list = ctx->max_cert_list;\n s->max_early_data = ctx->max_early_data;\n s->tls13_ciphersuites = sk_SSL_CIPHER_dup(ctx->tls13_ciphersuites);\n if (s->tls13_ciphersuites == NULL)\n goto err;\n s->cert = ssl_cert_dup(ctx->cert);\n if (s->cert == NULL)\n goto err;\n RECORD_LAYER_set_read_ahead(&s->rlayer, ctx->read_ahead);\n s->msg_callback = ctx->msg_callback;\n s->msg_callback_arg = ctx->msg_callback_arg;\n s->verify_mode = ctx->verify_mode;\n s->not_resumable_session_cb = ctx->not_resumable_session_cb;\n s->record_padding_cb = ctx->record_padding_cb;\n s->record_padding_arg = ctx->record_padding_arg;\n s->block_padding = ctx->block_padding;\n s->sid_ctx_length = ctx->sid_ctx_length;\n if (!ossl_assert(s->sid_ctx_length <= sizeof(s->sid_ctx)))\n goto err;\n memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));\n s->verify_callback = ctx->default_verify_callback;\n s->generate_session_id = ctx->generate_session_id;\n s->param = X509_VERIFY_PARAM_new();\n if (s->param == NULL)\n goto err;\n X509_VERIFY_PARAM_inherit(s->param, ctx->param);\n s->quiet_shutdown = ctx->quiet_shutdown;\n s->ext.max_fragment_len_mode = ctx->ext.max_fragment_len_mode;\n s->max_send_fragment = ctx->max_send_fragment;\n s->split_send_fragment = ctx->split_send_fragment;\n s->max_pipelines = ctx->max_pipelines;\n if (s->max_pipelines > 1)\n RECORD_LAYER_set_read_ahead(&s->rlayer, 1);\n if (ctx->default_read_buf_len > 0)\n SSL_set_default_read_buffer_len(s, ctx->default_read_buf_len);\n SSL_CTX_up_ref(ctx);\n s->ctx = ctx;\n s->ext.debug_cb = 0;\n s->ext.debug_arg = NULL;\n s->ext.ticket_expected = 0;\n s->ext.status_type = ctx->ext.status_type;\n s->ext.status_expected = 0;\n s->ext.ocsp.ids = NULL;\n s->ext.ocsp.exts = NULL;\n s->ext.ocsp.resp = NULL;\n s->ext.ocsp.resp_len = 0;\n SSL_CTX_up_ref(ctx);\n s->session_ctx = ctx;\n#ifndef OPENSSL_NO_EC\n if (ctx->ext.ecpointformats) {\n s->ext.ecpointformats =\n OPENSSL_memdup(ctx->ext.ecpointformats,\n ctx->ext.ecpointformats_len);\n if (!s->ext.ecpointformats)\n goto err;\n s->ext.ecpointformats_len =\n ctx->ext.ecpointformats_len;\n }\n if (ctx->ext.supportedgroups) {\n s->ext.supportedgroups =\n OPENSSL_memdup(ctx->ext.supportedgroups,\n ctx->ext.supportedgroups_len\n * sizeof(*ctx->ext.supportedgroups));\n if (!s->ext.supportedgroups)\n goto err;\n s->ext.supportedgroups_len = ctx->ext.supportedgroups_len;\n }\n#endif\n#ifndef OPENSSL_NO_NEXTPROTONEG\n s->ext.npn = NULL;\n#endif\n if (s->ctx->ext.alpn) {\n s->ext.alpn = OPENSSL_malloc(s->ctx->ext.alpn_len);\n if (s->ext.alpn == NULL)\n goto err;\n memcpy(s->ext.alpn, s->ctx->ext.alpn, s->ctx->ext.alpn_len);\n s->ext.alpn_len = s->ctx->ext.alpn_len;\n }\n s->verified_chain = NULL;\n s->verify_result = X509_V_OK;\n s->default_passwd_callback = ctx->default_passwd_callback;\n s->default_passwd_callback_userdata = ctx->default_passwd_callback_userdata;\n s->method = ctx->method;\n s->key_update = SSL_KEY_UPDATE_NONE;\n if (!s->method->ssl_new(s))\n goto err;\n s->server = (ctx->method->ssl_accept == ssl_undefined_function) ? 0 : 1;\n if (!SSL_clear(s))\n goto err;\n if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data))\n goto err;\n#ifndef OPENSSL_NO_PSK\n s->psk_client_callback = ctx->psk_client_callback;\n s->psk_server_callback = ctx->psk_server_callback;\n#endif\n s->psk_find_session_cb = ctx->psk_find_session_cb;\n s->psk_use_session_cb = ctx->psk_use_session_cb;\n s->job = NULL;\n#ifndef OPENSSL_NO_CT\n if (!SSL_set_ct_validation_callback(s, ctx->ct_validation_callback,\n ctx->ct_validation_callback_arg))\n goto err;\n#endif\n return s;\n err:\n SSL_free(s);\n SSLerr(SSL_F_SSL_NEW, ERR_R_MALLOC_FAILURE);\n return NULL;\n}', 'void SSL_free(SSL *s)\n{\n int i;\n if (s == NULL)\n return;\n CRYPTO_DOWN_REF(&s->references, &i, s->lock);\n REF_PRINT_COUNT("SSL", s);\n if (i > 0)\n return;\n REF_ASSERT_ISNT(i < 0);\n X509_VERIFY_PARAM_free(s->param);\n dane_final(&s->dane);\n CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);\n ssl_free_wbio_buffer(s);\n BIO_free_all(s->wbio);\n BIO_free_all(s->rbio);\n BUF_MEM_free(s->init_buf);\n sk_SSL_CIPHER_free(s->cipher_list);\n sk_SSL_CIPHER_free(s->cipher_list_by_id);\n sk_SSL_CIPHER_free(s->tls13_ciphersuites);\n if (s->session != NULL) {\n ssl_clear_bad_session(s);\n SSL_SESSION_free(s->session);\n }\n SSL_SESSION_free(s->psksession);\n OPENSSL_free(s->psksession_id);\n clear_ciphers(s);\n ssl_cert_free(s->cert);\n OPENSSL_free(s->ext.hostname);\n SSL_CTX_free(s->session_ctx);\n#ifndef OPENSSL_NO_EC\n OPENSSL_free(s->ext.ecpointformats);\n OPENSSL_free(s->ext.supportedgroups);\n#endif\n sk_X509_EXTENSION_pop_free(s->ext.ocsp.exts, X509_EXTENSION_free);\n#ifndef OPENSSL_NO_OCSP\n sk_OCSP_RESPID_pop_free(s->ext.ocsp.ids, OCSP_RESPID_free);\n#endif\n#ifndef OPENSSL_NO_CT\n SCT_LIST_free(s->scts);\n OPENSSL_free(s->ext.scts);\n#endif\n OPENSSL_free(s->ext.ocsp.resp);\n OPENSSL_free(s->ext.alpn);\n OPENSSL_free(s->ext.tls13_cookie);\n OPENSSL_free(s->clienthello);\n OPENSSL_free(s->pha_context);\n EVP_MD_CTX_free(s->pha_dgst);\n sk_X509_NAME_pop_free(s->ca_names, X509_NAME_free);\n sk_X509_pop_free(s->verified_chain, X509_free);\n if (s->method != NULL)\n s->method->ssl_free(s);\n RECORD_LAYER_release(&s->rlayer);\n SSL_CTX_free(s->ctx);\n ASYNC_WAIT_CTX_free(s->waitctx);\n#if !defined(OPENSSL_NO_NEXTPROTONEG)\n OPENSSL_free(s->ext.npn);\n#endif\n#ifndef OPENSSL_NO_SRTP\n sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);\n#endif\n 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_pop_free(a->extra_certs, X509_free);\n a->comp_methods = NULL;\n#ifndef OPENSSL_NO_SRTP\n sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);\n#endif\n#ifndef OPENSSL_NO_SRP\n SSL_CTX_SRP_CTX_free(a);\n#endif\n#ifndef OPENSSL_NO_ENGINE\n ENGINE_finish(a->client_cert_engine);\n#endif\n#ifndef OPENSSL_NO_EC\n OPENSSL_free(a->ext.ecpointformats);\n OPENSSL_free(a->ext.supportedgroups);\n#endif\n OPENSSL_free(a->ext.alpn);\n OPENSSL_secure_free(a->ext.secure);\n CRYPTO_THREAD_lock_free(a->lock);\n OPENSSL_free(a);\n}', 'void SSL_CTX_flush_sessions(SSL_CTX *s, long t)\n{\n unsigned long i;\n TIMEOUT_PARAM tp;\n tp.ctx = s;\n tp.cache = s->sessions;\n if (tp.cache == NULL)\n return;\n tp.time = t;\n CRYPTO_THREAD_write_lock(s->lock);\n i = lh_SSL_SESSION_get_down_load(s->sessions);\n lh_SSL_SESSION_set_down_load(s->sessions, 0);\n lh_SSL_SESSION_doall_TIMEOUT_PARAM(tp.cache, timeout_cb, &tp);\n lh_SSL_SESSION_set_down_load(s->sessions, i);\n CRYPTO_THREAD_unlock(s->lock);\n}', 'IMPLEMENT_LHASH_DOALL_ARG(SSL_SESSION, TIMEOUT_PARAM)', 'void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)\n{\n doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);\n}', 'static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,\n OPENSSL_LH_DOALL_FUNC func,\n OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)\n{\n int i;\n OPENSSL_LH_NODE *a, *n;\n if (lh == NULL)\n return;\n for (i = lh->num_nodes - 1; i >= 0; i--) {\n a = lh->b[i];\n while (a != NULL) {\n n = a->next;\n if (use_arg)\n func_arg(a->data, arg);\n else\n func(a->data);\n a = n;\n }\n }\n}'] |
35,436 | 0 | https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/bn/bn_lib.c/#L352 | static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
BN_ULONG *A, *a = NULL;
const BN_ULONG *B;
int i;
bn_check_top(b);
if (words > (INT_MAX / (4 * BN_BITS2))) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
return NULL;
}
if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
return (NULL);
}
if (BN_get_flags(b,BN_FLG_SECURE))
a = A = OPENSSL_secure_malloc(words * sizeof(*a));
else
a = A = OPENSSL_malloc(words * sizeof(*a));
if (A == NULL) {
BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
return (NULL);
}
#ifdef PURIFY
memset(a, 0, sizeof(*a) * words);
#endif
#if 1
B = b->d;
if (B != NULL) {
for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {
BN_ULONG a0, a1, a2, a3;
a0 = B[0];
a1 = B[1];
a2 = B[2];
a3 = B[3];
A[0] = a0;
A[1] = a1;
A[2] = a2;
A[3] = a3;
}
switch (b->top & 3) {
case 3:
A[2] = B[2];
case 2:
A[1] = B[1];
case 1:
A[0] = B[0];
case 0:
;
}
}
#else
memset(A, 0, sizeof(*A) * words);
memcpy(A, b->d, sizeof(b->d[0]) * b->top);
#endif
return (a);
} | ['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, 0, 1))\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 if ((BN_mod_word(p, (BN_ULONG)primes[i]) == 0) ||\n (BN_mod_word(q, (BN_ULONG)primes[i]) == 0)) {\n if (!BN_add(p, p, padd))\n goto err;\n if (!BN_add(q, q, qadd))\n goto err;\n goto loop;\n }\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n bn_check_top(p);\n return (ret);\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return (1);\n}', 'int BN_rand(BIGNUM *rnd, int bits, int top, int bottom)\n{\n return bnrand(0, rnd, bits, top, bottom);\n}', 'static int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)\n{\n unsigned char *buf = NULL;\n int ret = 0, bit, bytes, mask;\n time_t tim;\n if (bits < 0 || (bits == 1 && top > 0)) {\n BNerr(BN_F_BNRAND, BN_R_BITS_TOO_SMALL);\n return 0;\n }\n if (bits == 0) {\n BN_zero(rnd);\n return 1;\n }\n bytes = (bits + 7) / 8;\n bit = (bits - 1) % 8;\n mask = 0xff << (bit + 1);\n buf = OPENSSL_malloc(bytes);\n if (buf == NULL) {\n BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n time(&tim);\n RAND_add(&tim, sizeof(tim), 0.0);\n if (pseudorand) {\n if (RAND_bytes(buf, bytes) <= 0)\n goto err;\n } else {\n if (RAND_bytes(buf, bytes) <= 0)\n goto err;\n }\n if (pseudorand == 2) {\n int i;\n unsigned char c;\n for (i = 0; i < bytes; i++) {\n if (RAND_bytes(&c, 1) <= 0)\n goto err;\n if (c >= 128 && i > 0)\n buf[i] = buf[i - 1];\n else if (c < 42)\n buf[i] = 0;\n else if (c < 84)\n buf[i] = 255;\n }\n }\n if (top >= 0) {\n if (top) {\n if (bit == 0) {\n buf[0] = 1;\n buf[1] |= 0x80;\n } else {\n buf[0] |= (3 << (bit - 1));\n }\n } else {\n buf[0] |= (1 << bit);\n }\n }\n buf[0] &= ~mask;\n if (bottom)\n buf[bytes - 1] |= 1;\n if (!BN_bin2bn(buf, bytes, rnd))\n goto err;\n ret = 1;\n err:\n OPENSSL_clear_free(buf, bytes);\n bn_check_top(rnd);\n return (ret);\n}', 'BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)\n{\n unsigned int i, m;\n unsigned int n;\n BN_ULONG l;\n BIGNUM *bn = NULL;\n if (ret == NULL)\n ret = bn = BN_new();\n if (ret == NULL)\n return (NULL);\n bn_check_top(ret);\n for ( ; len > 0 && *s == 0; s++, len--)\n continue;\n n = len;\n if (n == 0) {\n ret->top = 0;\n return (ret);\n }\n i = ((n - 1) / BN_BYTES) + 1;\n m = ((n - 1) % (BN_BYTES));\n if (bn_wexpand(ret, (int)i) == NULL) {\n BN_free(bn);\n return NULL;\n }\n ret->top = i;\n ret->neg = 0;\n l = 0;\n while (n--) {\n l = (l << 8L) | *(s++);\n if (m-- == 0) {\n ret->d[--i] = l;\n l = 0;\n m = BN_BYTES - 1;\n }\n }\n bn_correct_top(ret);\n return (ret);\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *A, *a = NULL;\n const BN_ULONG *B;\n int i;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return (NULL);\n }\n if (BN_get_flags(b,BN_FLG_SECURE))\n a = A = OPENSSL_secure_malloc(words * sizeof(*a));\n else\n a = A = OPENSSL_malloc(words * sizeof(*a));\n if (A == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n#ifdef PURIFY\n memset(a, 0, sizeof(*a) * words);\n#endif\n#if 1\n B = b->d;\n if (B != NULL) {\n for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {\n BN_ULONG a0, a1, a2, a3;\n a0 = B[0];\n a1 = B[1];\n a2 = B[2];\n a3 = B[3];\n A[0] = a0;\n A[1] = a1;\n A[2] = a2;\n A[3] = a3;\n }\n switch (b->top & 3) {\n case 3:\n A[2] = B[2];\n case 2:\n A[1] = B[1];\n case 1:\n A[0] = B[0];\n case 0:\n ;\n }\n }\n#else\n memset(A, 0, sizeof(*A) * words);\n memcpy(A, b->d, sizeof(b->d[0]) * b->top);\n#endif\n return (a);\n}'] |
35,437 | 0 | https://github.com/libav/libav/blob/23f4c5acc438366d84cacf49e33b0bcd72f04937/libavformat/utils.c/#L2698 | void avformat_close_input(AVFormatContext **ps)
{
AVFormatContext *s = *ps;
AVIOContext *pb = s->pb;
if ((s->iformat && s->iformat->flags & AVFMT_NOFILE) ||
(s->flags & AVFMT_FLAG_CUSTOM_IO))
pb = NULL;
flush_packet_queue(s);
if (s->iformat) {
if (s->iformat->read_close)
s->iformat->read_close(s);
}
avformat_free_context(s);
*ps = NULL;
avio_close(pb);
} | ['void avformat_close_input(AVFormatContext **ps)\n{\n AVFormatContext *s = *ps;\n AVIOContext *pb = s->pb;\n if ((s->iformat && s->iformat->flags & AVFMT_NOFILE) ||\n (s->flags & AVFMT_FLAG_CUSTOM_IO))\n pb = NULL;\n flush_packet_queue(s);\n if (s->iformat) {\n if (s->iformat->read_close)\n s->iformat->read_close(s);\n }\n avformat_free_context(s);\n *ps = NULL;\n avio_close(pb);\n}', 'int avio_close(AVIOContext *s)\n{\n URLContext *h;\n if (!s)\n return 0;\n avio_flush(s);\n h = s->opaque;\n av_freep(&s->buffer);\n av_free(s);\n return ffurl_close(h);\n}', 'void avio_flush(AVIOContext *s)\n{\n flush_buffer(s);\n s->must_flush = 0;\n}', 'static void flush_buffer(AVIOContext *s)\n{\n if (s->buf_ptr > s->buffer) {\n if (s->write_packet && !s->error) {\n int ret = s->write_packet(s->opaque, s->buffer,\n s->buf_ptr - s->buffer);\n if (ret < 0) {\n s->error = ret;\n }\n }\n if (s->update_checksum) {\n s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,\n s->buf_ptr - s->checksum_ptr);\n s->checksum_ptr = s->buffer;\n }\n s->pos += s->buf_ptr - s->buffer;\n }\n s->buf_ptr = s->buffer;\n}'] |
35,438 | 0 | https://github.com/libav/libav/blob/45ee556d51ef04d79d52bf6b0b7f28a4d231cb0c/libavcodec/flac_parser.c/#L97 | static int frame_header_is_valid(AVCodecContext *avctx, const uint8_t *buf,
FLACFrameInfo *fi)
{
GetBitContext gb;
init_get_bits(&gb, buf, MAX_FRAME_HEADER_SIZE * 8);
return !ff_flac_decode_frame_header(avctx, &gb, fi, 127);
} | ['static int frame_header_is_valid(AVCodecContext *avctx, const uint8_t *buf,\n FLACFrameInfo *fi)\n{\n GetBitContext gb;\n init_get_bits(&gb, buf, MAX_FRAME_HEADER_SIZE * 8);\n return !ff_flac_decode_frame_header(avctx, &gb, fi, 127);\n}', 'static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,\n int bit_size)\n{\n int buffer_size;\n int ret = 0;\n if (bit_size > INT_MAX - 7 || bit_size < 0 || !buffer) {\n buffer_size = bit_size = 0;\n buffer = NULL;\n ret = AVERROR_INVALIDDATA;\n }\n buffer_size = (bit_size + 7) >> 3;\n s->buffer = buffer;\n s->size_in_bits = bit_size;\n#if !UNCHECKED_BITSTREAM_READER\n s->size_in_bits_plus8 = bit_size + 8;\n#endif\n s->buffer_end = buffer + buffer_size;\n s->index = 0;\n return ret;\n}', 'int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,\n FLACFrameInfo *fi, int log_level_offset)\n{\n int bs_code, sr_code, bps_code;\n if ((get_bits(gb, 15) & 0x7FFF) != 0x7FFC) {\n av_log(avctx, AV_LOG_ERROR + log_level_offset, "invalid sync code\\n");\n return AVERROR_INVALIDDATA;\n }\n fi->is_var_size = get_bits1(gb);\n bs_code = get_bits(gb, 4);\n sr_code = get_bits(gb, 4);\n fi->ch_mode = get_bits(gb, 4);\n if (fi->ch_mode < FLAC_MAX_CHANNELS) {\n fi->channels = fi->ch_mode + 1;\n fi->ch_mode = FLAC_CHMODE_INDEPENDENT;\n } else if (fi->ch_mode < FLAC_MAX_CHANNELS + FLAC_CHMODE_MID_SIDE) {\n fi->channels = 2;\n fi->ch_mode -= FLAC_MAX_CHANNELS - 1;\n } else {\n av_log(avctx, AV_LOG_ERROR + log_level_offset,\n "invalid channel mode: %d\\n", fi->ch_mode);\n return AVERROR_INVALIDDATA;\n }\n bps_code = get_bits(gb, 3);\n if (bps_code == 3 || bps_code == 7) {\n av_log(avctx, AV_LOG_ERROR + log_level_offset,\n "invalid sample size code (%d)\\n",\n bps_code);\n return AVERROR_INVALIDDATA;\n }\n fi->bps = sample_size_table[bps_code];\n if (get_bits1(gb)) {\n av_log(avctx, AV_LOG_ERROR + log_level_offset,\n "broken stream, invalid padding\\n");\n return AVERROR_INVALIDDATA;\n }\n fi->frame_or_sample_num = get_utf8(gb);\n if (fi->frame_or_sample_num < 0) {\n av_log(avctx, AV_LOG_ERROR + log_level_offset,\n "sample/frame number invalid; utf8 fscked\\n");\n return AVERROR_INVALIDDATA;\n }\n if (bs_code == 0) {\n av_log(avctx, AV_LOG_ERROR + log_level_offset,\n "reserved blocksize code: 0\\n");\n return AVERROR_INVALIDDATA;\n } else if (bs_code == 6) {\n fi->blocksize = get_bits(gb, 8) + 1;\n } else if (bs_code == 7) {\n fi->blocksize = get_bits(gb, 16) + 1;\n } else {\n fi->blocksize = ff_flac_blocksize_table[bs_code];\n }\n if (sr_code < 12) {\n fi->samplerate = ff_flac_sample_rate_table[sr_code];\n } else if (sr_code == 12) {\n fi->samplerate = get_bits(gb, 8) * 1000;\n } else if (sr_code == 13) {\n fi->samplerate = get_bits(gb, 16);\n } else if (sr_code == 14) {\n fi->samplerate = get_bits(gb, 16) * 10;\n } else {\n av_log(avctx, AV_LOG_ERROR + log_level_offset,\n "illegal sample rate code %d\\n",\n sr_code);\n return AVERROR_INVALIDDATA;\n }\n skip_bits(gb, 8);\n if (av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, gb->buffer,\n get_bits_count(gb)/8)) {\n av_log(avctx, AV_LOG_ERROR + log_level_offset,\n "header crc mismatch\\n");\n return AVERROR_INVALIDDATA;\n }\n return 0;\n}', 'static inline unsigned int get_bits1(GetBitContext *s)\n{\n unsigned int index = s->index;\n uint8_t result = s->buffer[index >> 3];\n#ifdef BITSTREAM_READER_LE\n result >>= index & 7;\n result &= 1;\n#else\n result <<= index & 7;\n result >>= 8 - 1;\n#endif\n#if !UNCHECKED_BITSTREAM_READER\n if (s->index < s->size_in_bits_plus8)\n#endif\n index++;\n s->index = index;\n return result;\n}'] |
35,439 | 0 | https://github.com/libav/libav/blob/f924d52975ec5bbae41d26f79be2373a1b12046b/libavutil/base64.c/#L55 | int av_base64_decode(uint8_t *out, const char *in, int out_size)
{
int i;
unsigned v = 0;
uint8_t *dst = out;
for (i = 0; in[i] && in[i] != '='; i++) {
unsigned int index= in[i]-43;
if (index>=FF_ARRAY_ELEMS(map2) || map2[index] == 0xff)
return -1;
v = (v << 6) + map2[index];
if (i & 3) {
if (dst - out < out_size) {
*dst++ = v >> (6 - 2 * (i & 3));
}
}
}
return dst - out;
} | ['static int srtp_open(URLContext *h, const char *uri, int flags)\n{\n SRTPProtoContext *s = h->priv_data;\n char hostname[256], buf[1024], path[1024];\n int rtp_port, ret;\n if (s->out_suite && s->out_params)\n if ((ret = ff_srtp_set_crypto(&s->srtp_out, s->out_suite, s->out_params)) < 0)\n goto fail;\n if (s->in_suite && s->in_params)\n if ((ret = ff_srtp_set_crypto(&s->srtp_in, s->in_suite, s->in_params)) < 0)\n goto fail;\n av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,\n path, sizeof(path), uri);\n ff_url_join(buf, sizeof(buf), "rtp", NULL, hostname, rtp_port, "%s", path);\n if ((ret = ffurl_open(&s->rtp_hd, buf, flags, &h->interrupt_callback, NULL)) < 0)\n goto fail;\n h->max_packet_size = FFMIN(s->rtp_hd->max_packet_size,\n sizeof(s->encryptbuf)) - 14;\n h->is_streamed = 1;\n return 0;\nfail:\n srtp_close(h);\n return ret;\n}', 'int ff_srtp_set_crypto(struct SRTPContext *s, const char *suite,\n const char *params)\n{\n uint8_t buf[30];\n ff_srtp_free(s);\n if (!strcmp(suite, "AES_CM_128_HMAC_SHA1_80")) {\n s->hmac_size = 10;\n } else if (!strcmp(suite, "AES_CM_128_HMAC_SHA1_32")) {\n s->hmac_size = 4;\n } else {\n av_log(NULL, AV_LOG_WARNING, "SRTP Crypto suite %s not supported\\n",\n suite);\n return AVERROR(EINVAL);\n }\n if (av_base64_decode(buf, params, sizeof(buf)) != sizeof(buf)) {\n av_log(NULL, AV_LOG_WARNING, "Incorrect amount of SRTP params\\n");\n return AVERROR(EINVAL);\n }\n s->aes = av_aes_alloc();\n s->hmac = av_hmac_alloc(AV_HMAC_SHA1);\n if (!s->aes || !s->hmac)\n return AVERROR(ENOMEM);\n memcpy(s->master_key, buf, 16);\n memcpy(s->master_salt, buf + 16, 14);\n av_aes_init(s->aes, s->master_key, 128, 0);\n derive_key(s->aes, s->master_salt, 0x00, s->rtp_key, sizeof(s->rtp_key));\n derive_key(s->aes, s->master_salt, 0x02, s->rtp_salt, sizeof(s->rtp_salt));\n derive_key(s->aes, s->master_salt, 0x01, s->rtp_auth, sizeof(s->rtp_auth));\n derive_key(s->aes, s->master_salt, 0x03, s->rtcp_key, sizeof(s->rtcp_key));\n derive_key(s->aes, s->master_salt, 0x05, s->rtcp_salt, sizeof(s->rtcp_salt));\n derive_key(s->aes, s->master_salt, 0x04, s->rtcp_auth, sizeof(s->rtcp_auth));\n return 0;\n}', "int av_base64_decode(uint8_t *out, const char *in, int out_size)\n{\n int i;\n unsigned v = 0;\n uint8_t *dst = out;\n for (i = 0; in[i] && in[i] != '='; i++) {\n unsigned int index= in[i]-43;\n if (index>=FF_ARRAY_ELEMS(map2) || map2[index] == 0xff)\n return -1;\n v = (v << 6) + map2[index];\n if (i & 3) {\n if (dst - out < out_size) {\n *dst++ = v >> (6 - 2 * (i & 3));\n }\n }\n }\n return dst - out;\n}"] |
35,440 | 0 | https://github.com/openssl/openssl/blob/8ae173bb57819a23717fd3c8e7c51cb62f4268d0/crypto/bn/bn_ctx.c/#L300 | static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
} | ['static int sm2_sig_verify(const EC_KEY *key, const ECDSA_SIG *sig,\n const BIGNUM *e)\n{\n int ret = 0;\n const EC_GROUP *group = EC_KEY_get0_group(key);\n const BIGNUM *order = EC_GROUP_get0_order(group);\n BN_CTX *ctx = NULL;\n EC_POINT *pt = NULL;\n BIGNUM *t = NULL;\n BIGNUM *x1 = NULL;\n const BIGNUM *r = NULL;\n const BIGNUM *s = NULL;\n ctx = BN_CTX_new();\n pt = EC_POINT_new(group);\n if (ctx == NULL || pt == NULL) {\n SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_MALLOC_FAILURE);\n goto done;\n }\n BN_CTX_start(ctx);\n t = BN_CTX_get(ctx);\n x1 = BN_CTX_get(ctx);\n if (x1 == NULL) {\n SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_MALLOC_FAILURE);\n goto done;\n }\n ECDSA_SIG_get0(sig, &r, &s);\n if (BN_cmp(r, BN_value_one()) < 0\n || BN_cmp(s, BN_value_one()) < 0\n || BN_cmp(order, r) <= 0\n || BN_cmp(order, s) <= 0) {\n SM2err(SM2_F_SM2_SIG_VERIFY, SM2_R_BAD_SIGNATURE);\n goto done;\n }\n if (!BN_mod_add(t, r, s, order, ctx)) {\n SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_BN_LIB);\n goto done;\n }\n if (BN_is_zero(t)) {\n SM2err(SM2_F_SM2_SIG_VERIFY, SM2_R_BAD_SIGNATURE);\n goto done;\n }\n if (!EC_POINT_mul(group, pt, s, EC_KEY_get0_public_key(key), t, ctx)\n || !EC_POINT_get_affine_coordinates(group, pt, x1, NULL, ctx)) {\n SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_EC_LIB);\n goto done;\n }\n if (!BN_mod_add(t, e, x1, order, ctx)) {\n SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_BN_LIB);\n goto done;\n }\n if (BN_cmp(r, t) == 0)\n ret = 1;\n done:\n EC_POINT_free(pt);\n BN_CTX_free(ctx);\n return ret;\n}', 'int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,\n const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)\n{\n const EC_POINT *points[1];\n const BIGNUM *scalars[1];\n points[0] = point;\n scalars[0] = p_scalar;\n return EC_POINTs_mul(group, r, g_scalar,\n (point != NULL\n && p_scalar != NULL), points, scalars, ctx);\n}', 'int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,\n size_t num, const EC_POINT *points[],\n const BIGNUM *scalars[], BN_CTX *ctx)\n{\n int ret = 0;\n size_t i = 0;\n BN_CTX *new_ctx = NULL;\n if ((scalar == NULL) && (num == 0)) {\n return EC_POINT_set_to_infinity(group, r);\n }\n if (!ec_point_is_compat(r, group)) {\n ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);\n return 0;\n }\n for (i = 0; i < num; i++) {\n if (!ec_point_is_compat(points[i], group)) {\n ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);\n return 0;\n }\n }\n if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL) {\n ECerr(EC_F_EC_POINTS_MUL, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n if (group->meth->mul != NULL)\n ret = group->meth->mul(group, r, scalar, num, points, scalars, ctx);\n else\n ret = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'int BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,\n BN_CTX *ctx)\n{\n if (!BN_add(r, a, b))\n return 0;\n return BN_nnmod(r, r, m, ctx);\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int ret;\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return 0;\n }\n if (divisor->d[divisor->top - 1] == 0) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n ret = bn_div_fixed_top(dv, rm, num, divisor, ctx);\n if (ret) {\n if (dv != NULL)\n bn_correct_top(dv);\n if (rm != NULL)\n bn_correct_top(rm);\n }\n return ret;\n}', 'int bn_div_fixed_top(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,\n const BIGNUM *divisor, BN_CTX *ctx)\n{\n int norm_shift, i, j, loop;\n BIGNUM *tmp, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnum, *wnumtop;\n BN_ULONG d0, d1;\n int num_n, div_n;\n assert(divisor->top > 0 && divisor->d[divisor->top - 1] != 0);\n bn_check_top(num);\n bn_check_top(divisor);\n bn_check_top(dv);\n bn_check_top(rm);\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n if (!BN_copy(sdiv, divisor))\n goto err;\n norm_shift = bn_left_align(sdiv);\n sdiv->neg = 0;\n if (!(bn_lshift_fixed_top(snum, num, norm_shift)))\n goto err;\n div_n = sdiv->top;\n num_n = snum->top;\n if (num_n <= div_n) {\n if (bn_wexpand(snum, div_n + 1) == NULL)\n goto err;\n memset(&(snum->d[num_n]), 0, (div_n - num_n + 1) * sizeof(BN_ULONG));\n snum->top = num_n = div_n + 1;\n }\n loop = num_n - div_n;\n wnum = &(snum->d[loop]);\n wnumtop = &(snum->d[num_n - 1]);\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n if (!bn_wexpand(res, loop))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop;\n res->flags |= BN_FLG_FIXED_TOP;\n resp = &(res->d[loop]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n for (i = 0; i < loop; i++, wnumtop--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W)\n q = bn_div_3_words(wnumtop, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnumtop[0];\n n1 = wnumtop[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n BN_ULONG n2 = (wnumtop == wnum) ? 0 : wnumtop[-2];\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | n2))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= n2)))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum--;\n l0 = bn_sub_words(wnum, wnum, tmp->d, div_n + 1);\n q -= l0;\n for (l0 = 0 - l0, j = 0; j < div_n; j++)\n tmp->d[j] = sdiv->d[j] & l0;\n l0 = bn_add_words(wnum, wnum, tmp->d, div_n);\n (*wnumtop) += l0;\n assert((*wnumtop) == 0);\n *--resp = q;\n }\n snum->neg = num->neg;\n snum->top = div_n;\n snum->flags |= BN_FLG_FIXED_TOP;\n if (rm != NULL)\n bn_rshift_fixed_top(rm, snum, norm_shift);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return 0;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_start()", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG("LEAVE BN_CTX_start()", ctx);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n if (ctx == NULL)\n return;\n CTXDBG("ENTER BN_CTX_end()", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG("LEAVE BN_CTX_end()", ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}'] |
35,441 | 0 | https://github.com/openssl/openssl/blob/305b68f1a2b6d4d0aa07a6ab47ac372f067a40bb/crypto/bn/bn_lib.c/#L290 | BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
{
bn_check_top(b);
if (a == b)
return a;
if (bn_wexpand(a, b->top) == NULL)
return NULL;
if (b->top > 0)
memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);
a->neg = b->neg;
a->top = b->top;
a->flags |= b->flags & BN_FLG_FIXED_TOP;
bn_check_top(a);
return a;
} | ['static int file_exp(STANZA *s)\n{\n BIGNUM *a = NULL, *e = NULL, *exp = NULL, *ret = NULL;\n int st = 0;\n if (!TEST_ptr(a = getBN(s, "A"))\n || !TEST_ptr(e = getBN(s, "E"))\n || !TEST_ptr(exp = getBN(s, "Exp"))\n || !TEST_ptr(ret = BN_new()))\n goto err;\n if (!TEST_true(BN_exp(ret, a, e, ctx))\n || !equalBN("A ^ E", exp, ret))\n goto err;\n st = 1;\nerr:\n BN_free(a);\n BN_free(e);\n BN_free(exp);\n BN_free(ret);\n return st;\n}', 'static BIGNUM *getBN(STANZA *s, const char *attribute)\n{\n const char *hex;\n BIGNUM *ret = NULL;\n if ((hex = findattr(s, attribute)) == NULL) {\n TEST_error("%s:%d: Can\'t find %s", s->test_file, s->start, attribute);\n return NULL;\n }\n if (parseBN(&ret, hex) != (int)strlen(hex)) {\n TEST_error("Could not decode \'%s\'", hex);\n return NULL;\n }\n return ret;\n}', 'static int parseBN(BIGNUM **out, const char *in)\n{\n *out = NULL;\n return BN_hex2bn(out, in);\n}', "int BN_hex2bn(BIGNUM **bn, const char *a)\n{\n BIGNUM *ret = NULL;\n BN_ULONG l = 0;\n int neg = 0, h, m, i, j, k, c;\n int num;\n if (a == NULL || *a == '\\0')\n return 0;\n if (*a == '-') {\n neg = 1;\n a++;\n }\n for (i = 0; i <= INT_MAX / 4 && ossl_isxdigit(a[i]); i++)\n continue;\n if (i == 0 || i > INT_MAX / 4)\n goto err;\n num = i + neg;\n if (bn == NULL)\n return num;\n if (*bn == NULL) {\n if ((ret = BN_new()) == NULL)\n return 0;\n } else {\n ret = *bn;\n BN_zero(ret);\n }\n if (bn_expand(ret, i * 4) == NULL)\n goto err;\n j = i;\n m = 0;\n h = 0;\n while (j > 0) {\n m = (BN_BYTES * 2 <= j) ? BN_BYTES * 2 : j;\n l = 0;\n for (;;) {\n c = a[j - m];\n k = OPENSSL_hexchar2int(c);\n if (k < 0)\n k = 0;\n l = (l << 4) | k;\n if (--m <= 0) {\n ret->d[h++] = l;\n break;\n }\n }\n j -= BN_BYTES * 2;\n }\n ret->top = h;\n bn_correct_top(ret);\n *bn = ret;\n bn_check_top(ret);\n if (ret->top != 0)\n ret->neg = neg;\n return num;\n err:\n if (*bn == NULL)\n BN_free(ret);\n return 0;\n}", 'int BN_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 || BN_get_flags(a, BN_FLG_CONSTTIME) != 0) {\n BNerr(BN_F_BN_EXP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n BN_CTX_start(ctx);\n rr = ((r == a) || (r == p)) ? BN_CTX_get(ctx) : 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 && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n bn_check_top(r);\n return ret;\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n bn_check_top(b);\n if (a == b)\n return a;\n if (bn_wexpand(a, b->top) == NULL)\n return NULL;\n if (b->top > 0)\n memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n a->neg = b->neg;\n a->top = b->top;\n a->flags |= b->flags & BN_FLG_FIXED_TOP;\n bn_check_top(a);\n return a;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}'] |
35,442 | 0 | https://github.com/libav/libav/blob/dedfa00107dbbb319d0e33faac683557b86d1007/libavcodec/parser.c/#L195 | int av_parser_change(AVCodecParserContext *s,
AVCodecContext *avctx,
uint8_t **poutbuf, int *poutbuf_size,
const uint8_t *buf, int buf_size, int keyframe){
if(s && s->parser->split){
if((avctx->flags & CODEC_FLAG_GLOBAL_HEADER) || (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)){
int i= s->parser->split(avctx, buf, buf_size);
buf += i;
buf_size -= i;
}
}
*poutbuf= (uint8_t *) buf;
*poutbuf_size= buf_size;
if(avctx->extradata){
if( (keyframe && (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER))
){
int size= buf_size + avctx->extradata_size;
*poutbuf_size= size;
*poutbuf= av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
memcpy((*poutbuf) + avctx->extradata_size, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
return 1;
}
}
return 0;
} | ['int av_parser_change(AVCodecParserContext *s,\n AVCodecContext *avctx,\n uint8_t **poutbuf, int *poutbuf_size,\n const uint8_t *buf, int buf_size, int keyframe){\n if(s && s->parser->split){\n if((avctx->flags & CODEC_FLAG_GLOBAL_HEADER) || (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)){\n int i= s->parser->split(avctx, buf, buf_size);\n buf += i;\n buf_size -= i;\n }\n }\n *poutbuf= (uint8_t *) buf;\n *poutbuf_size= buf_size;\n if(avctx->extradata){\n if( (keyframe && (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER))\n ){\n int size= buf_size + avctx->extradata_size;\n *poutbuf_size= size;\n *poutbuf= av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);\n memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);\n memcpy((*poutbuf) + avctx->extradata_size, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);\n return 1;\n }\n }\n return 0;\n}', 'void *av_malloc(size_t size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if (size > (INT_MAX - 32) || !size)\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size + 32);\n if (!ptr)\n return ptr;\n diff = ((-(long)ptr - 1) & 31) + 1;\n ptr = (char *)ptr + diff;\n ((char *)ptr)[-1] = diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr, 32, size))\n ptr = NULL;\n#elif HAVE_ALIGNED_MALLOC\n ptr = _aligned_malloc(size, 32);\n#elif HAVE_MEMALIGN\n ptr = memalign(32, size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}'] |
35,443 | 0 | https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/bn/bn_shift.c/#L159 | int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)
{
int i, nw, lb, rb;
BN_ULONG *t, *f;
BN_ULONG l;
bn_check_top(r);
bn_check_top(a);
if (n < 0) {
BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);
return 0;
}
r->neg = a->neg;
nw = n / BN_BITS2;
if (bn_wexpand(r, a->top + nw + 1) == NULL)
return (0);
lb = n % BN_BITS2;
rb = BN_BITS2 - lb;
f = a->d;
t = r->d;
t[a->top + nw] = 0;
if (lb == 0)
for (i = a->top - 1; i >= 0; i--)
t[nw + i] = f[i];
else
for (i = a->top - 1; i >= 0; i--) {
l = f[i];
t[nw + i + 1] |= (l >> rb) & BN_MASK2;
t[nw + i] = (l << lb) & BN_MASK2;
}
memset(t, 0, sizeof(*t) * nw);
r->top = a->top + nw + 1;
bn_correct_top(r);
bn_check_top(r);
return (1);
} | ['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)\n p1 = BN_CTX_get(ctx);\n if (!p2)\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 (!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 && BN_is_prime_fasttest_ex(p, 50, ctx, 1, cb))\n break;\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}', '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_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,\n BN_CTX *ctx)\n{\n if (!BN_sub(r, a, b))\n return 0;\n return BN_nnmod(r, r, m, 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 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_num_bits(const BIGNUM *a)\n{\n int i = a->top - 1;\n bn_check_top(a);\n if (BN_is_zero(a))\n return 0;\n return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));\n}', 'int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l;\n bn_check_top(r);\n bn_check_top(a);\n if (n < 0) {\n BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n r->neg = a->neg;\n nw = n / BN_BITS2;\n if (bn_wexpand(r, a->top + nw + 1) == NULL)\n return (0);\n lb = n % BN_BITS2;\n rb = BN_BITS2 - lb;\n f = a->d;\n t = r->d;\n t[a->top + nw] = 0;\n if (lb == 0)\n for (i = a->top - 1; i >= 0; i--)\n t[nw + i] = f[i];\n else\n for (i = a->top - 1; i >= 0; i--) {\n l = f[i];\n t[nw + i + 1] |= (l >> rb) & BN_MASK2;\n t[nw + i] = (l << lb) & BN_MASK2;\n }\n memset(t, 0, sizeof(*t) * nw);\n r->top = a->top + nw + 1;\n bn_correct_top(r);\n bn_check_top(r);\n return (1);\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}'] |
35,444 | 0 | https://github.com/libav/libav/blob/dad7a9c7c0ae8ebc56f2e3a24e6fa4da5c2cd491/libavcodec/bitstream.h/#L139 | static inline uint64_t get_val(BitstreamContext *bc, unsigned n)
{
#ifdef BITSTREAM_READER_LE
uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);
bc->bits >>= n;
#else
uint64_t ret = bc->bits >> (64 - n);
bc->bits <<= n;
#endif
bc->bits_left -= n;
return ret;
} | ['static int h261_decode_gob_header(H261Context *h)\n{\n unsigned int val;\n MpegEncContext *const s = &h->s;\n if (!h->gob_start_code_skipped) {\n val = bitstream_peek(&s->bc, 15);\n if (val)\n return -1;\n bitstream_skip(&s->bc, 16);\n }\n h->gob_start_code_skipped = 0;\n h->gob_number = bitstream_read(&s->bc, 4);\n s->qscale = bitstream_read(&s->bc, 5);\n if (s->mb_height == 18) {\n if ((h->gob_number <= 0) || (h->gob_number > 12))\n return -1;\n } else {\n if ((h->gob_number != 1) && (h->gob_number != 3) &&\n (h->gob_number != 5))\n return -1;\n }\n while (bitstream_read_bit(&s->bc) != 0)\n bitstream_skip(&s->bc, 8);\n if (s->qscale == 0) {\n av_log(s->avctx, AV_LOG_ERROR, "qscale has forbidden 0 value\\n");\n if (s->avctx->err_recognition & AV_EF_BITSTREAM)\n return -1;\n }\n h->current_mba = 0;\n h->mba_diff = 0;\n return 0;\n}', 'static inline uint32_t bitstream_read(BitstreamContext *bc, unsigned n)\n{\n if (!n)\n return 0;\n if (n > bc->bits_left) {\n refill_32(bc);\n if (bc->bits_left < 32)\n bc->bits_left = n;\n }\n return get_val(bc, n);\n}', 'static inline uint64_t get_val(BitstreamContext *bc, unsigned n)\n{\n#ifdef BITSTREAM_READER_LE\n uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);\n bc->bits >>= n;\n#else\n uint64_t ret = bc->bits >> (64 - n);\n bc->bits <<= n;\n#endif\n bc->bits_left -= n;\n return ret;\n}'] |
35,445 | 0 | https://github.com/openssl/openssl/blob/02ab618c97eb5c383153f1835017533efc2f7422/crypto/asn1/asn1_lib.c/#L212 | static void asn1_put_length(unsigned char **pp, int length)
{
unsigned char *p= *pp;
int i,l;
if (length <= 127)
*(p++)=(unsigned char)length;
else
{
l=length;
for (i=0; l > 0; i++)
l>>=8;
*(p++)=i|0x80;
l=i;
while (i-- > 0)
{
p[i]=length&0xff;
length>>=8;
}
p+=l;
}
*pp=p;
} | ['static int request_certificate(SSL *s)\n\t{\n\tunsigned char *p,*p2,*buf2;\n\tunsigned char *ccd;\n\tint i,j,ctype,ret= -1;\n\tX509 *x509=NULL;\n\tSTACK_OF(X509) *sk=NULL;\n\tccd=s->s2->tmp.ccl;\n\tif (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_A)\n\t\t{\n\t\tp=(unsigned char *)s->init_buf->data;\n\t\t*(p++)=SSL2_MT_REQUEST_CERTIFICATE;\n\t\t*(p++)=SSL2_AT_MD5_WITH_RSA_ENCRYPTION;\n\t\tRAND_bytes(ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH);\n\t\tmemcpy(p,ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH);\n\t\ts->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_B;\n\t\ts->init_num=SSL2_MIN_CERT_CHALLENGE_LENGTH+2;\n\t\ts->init_off=0;\n\t\t}\n\tif (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_B)\n\t\t{\n\t\ti=ssl2_do_write(s);\n\t\tif (i <= 0)\n\t\t\t{\n\t\t\tret=i;\n\t\t\tgoto end;\n\t\t\t}\n\t\ts->init_num=0;\n\t\ts->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_C;\n\t\t}\n\tif (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_C)\n\t\t{\n\t\tp=(unsigned char *)s->init_buf->data;\n\t\ti=ssl2_read(s,(char *)&(p[s->init_num]),6-s->init_num);\n\t\tif (i < 3)\n\t\t\t{\n\t\t\tret=ssl2_part_read(s,SSL_F_REQUEST_CERTIFICATE,i);\n\t\t\tgoto end;\n\t\t\t}\n\t\tif ((*p == SSL2_MT_ERROR) && (i >= 3))\n\t\t\t{\n\t\t\tn2s(p,i);\n\t\t\tif (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)\n\t\t\t\t{\n\t\t\t\tssl2_return_error(s,SSL2_PE_BAD_CERTIFICATE);\n\t\t\t\tSSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\tret=1;\n\t\t\tgoto end;\n\t\t\t}\n\t\tif ((*(p++) != SSL2_MT_CLIENT_CERTIFICATE) || (i < 6))\n\t\t\t{\n\t\t\tssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);\n\t\t\tSSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_SHORT_READ);\n\t\t\tgoto end;\n\t\t\t}\n\t\tctype= *(p++);\n\t\tif (ctype != SSL2_AT_MD5_WITH_RSA_ENCRYPTION)\n\t\t\t{\n\t\t\tssl2_return_error(s,SSL2_PE_UNSUPPORTED_CERTIFICATE_TYPE);\n\t\t\tSSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_BAD_RESPONSE_ARGUMENT);\n\t\t\tgoto end;\n\t\t\t}\n\t\tn2s(p,i); s->s2->tmp.clen=i;\n\t\tn2s(p,i); s->s2->tmp.rlen=i;\n\t\ts->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_D;\n\t\ts->init_num=0;\n\t\t}\n\tp=(unsigned char *)s->init_buf->data;\n\tj=s->s2->tmp.clen+s->s2->tmp.rlen-s->init_num;\n\ti=ssl2_read(s,(char *)&(p[s->init_num]),j);\n\tif (i < j)\n\t\t{\n\t\tret=ssl2_part_read(s,SSL_F_REQUEST_CERTIFICATE,i);\n\t\tgoto end;\n\t\t}\n\tx509=(X509 *)d2i_X509(NULL,&p,(long)s->s2->tmp.clen);\n\tif (x509 == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_REQUEST_CERTIFICATE,ERR_R_X509_LIB);\n\t\tgoto msg_end;\n\t\t}\n\tif (((sk=sk_X509_new_null()) == NULL) || (!sk_X509_push(sk,x509)))\n\t\t{\n\t\tSSLerr(SSL_F_REQUEST_CERTIFICATE,ERR_R_MALLOC_FAILURE);\n\t\tgoto msg_end;\n\t\t}\n\ti=ssl_verify_cert_chain(s,sk);\n\tif (i)\n\t\t{\n\t\tEVP_MD_CTX ctx;\n\t\tEVP_PKEY *pkey=NULL;\n\t\tEVP_VerifyInit(&ctx,s->ctx->rsa_md5);\n\t\tEVP_VerifyUpdate(&ctx,s->s2->key_material,\n\t\t\t(unsigned int)s->s2->key_material_length);\n\t\tEVP_VerifyUpdate(&ctx,ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH);\n\t\ti=i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509,NULL);\n\t\tbuf2=(unsigned char *)Malloc((unsigned int)i);\n\t\tif (buf2 == NULL)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_REQUEST_CERTIFICATE,ERR_R_MALLOC_FAILURE);\n\t\t\tgoto msg_end;\n\t\t\t}\n\t\tp2=buf2;\n\t\ti=i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509,&p2);\n\t\tEVP_VerifyUpdate(&ctx,buf2,(unsigned int)i);\n\t\tFree(buf2);\n\t\tpkey=X509_get_pubkey(x509);\n\t\tif (pkey == NULL) goto end;\n\t\ti=EVP_VerifyFinal(&ctx,p,s->s2->tmp.rlen,pkey);\n\t\tEVP_PKEY_free(pkey);\n\t\tmemset(&ctx,0,sizeof(ctx));\n\t\tif (i)\n\t\t\t{\n\t\t\tif (s->session->peer != NULL)\n\t\t\t\tX509_free(s->session->peer);\n\t\t\ts->session->peer=x509;\n\t\t\tCRYPTO_add(&x509->references,1,CRYPTO_LOCK_X509);\n\t\t\tret=1;\n\t\t\tgoto end;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tSSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_BAD_CHECKSUM);\n\t\t\tgoto msg_end;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\nmsg_end:\n\t\tssl2_return_error(s,SSL2_PE_BAD_CERTIFICATE);\n\t\t}\nend:\n\tsk_X509_free(sk);\n\tX509_free(x509);\n\treturn(ret);\n\t}', 'X509 *d2i_X509(X509 **a, unsigned char **pp, long length)\n\t{\n\tM_ASN1_D2I_vars(a,X509 *,X509_new);\n\tM_ASN1_D2I_Init();\n\tM_ASN1_D2I_start_sequence();\n\tM_ASN1_D2I_get(ret->cert_info,d2i_X509_CINF);\n\tM_ASN1_D2I_get(ret->sig_alg,d2i_X509_ALGOR);\n\tM_ASN1_D2I_get(ret->signature,d2i_ASN1_BIT_STRING);\n\tif (ret->name != NULL) Free(ret->name);\n\tret->name=X509_NAME_oneline(ret->cert_info->subject,NULL,0);\n\tM_ASN1_D2I_Finish(a,X509_free,ASN1_F_D2I_X509);\n\t}', 'IMPLEMENT_STACK_OF(X509)', 'int sk_push(STACK *st, char *data)\n\t{\n\treturn(sk_insert(st,data,st->num));\n\t}', 'int sk_insert(STACK *st, char *data, int loc)\n\t{\n\tchar **s;\n\tif(st == NULL) return 0;\n\tif (st->num_alloc <= st->num+1)\n\t\t{\n\t\ts=(char **)Realloc((char *)st->data,\n\t\t\t(unsigned int)sizeof(char *)*st->num_alloc*2);\n\t\tif (s == NULL)\n\t\t\treturn(0);\n\t\tst->data=s;\n\t\tst->num_alloc*=2;\n\t\t}\n\tif ((loc >= (int)st->num) || (loc < 0))\n\t\tst->data[st->num]=data;\n\telse\n\t\t{\n\t\tint i;\n\t\tchar **f,**t;\n\t\tf=(char **)st->data;\n\t\tt=(char **)&(st->data[1]);\n\t\tfor (i=st->num; i>=loc; i--)\n\t\t\tt[i]=f[i];\n#ifdef undef\n\t\tmemmove( (char *)&(st->data[loc+1]),\n\t\t\t(char *)&(st->data[loc]),\n\t\t\tsizeof(char *)*(st->num-loc));\n#endif\n\t\tst->data[loc]=data;\n\t\t}\n\tst->num++;\n\tst->sorted=0;\n\treturn(st->num);\n\t}', 'int i2d_X509(X509 *a, unsigned char **pp)\n\t{\n\tM_ASN1_I2D_vars(a);\n\tM_ASN1_I2D_len(a->cert_info,\ti2d_X509_CINF);\n\tM_ASN1_I2D_len(a->sig_alg,\ti2d_X509_ALGOR);\n\tM_ASN1_I2D_len(a->signature,\ti2d_ASN1_BIT_STRING);\n\tM_ASN1_I2D_seq_total();\n\tM_ASN1_I2D_put(a->cert_info,\ti2d_X509_CINF);\n\tM_ASN1_I2D_put(a->sig_alg,\ti2d_X509_ALGOR);\n\tM_ASN1_I2D_put(a->signature,\ti2d_ASN1_BIT_STRING);\n\tM_ASN1_I2D_finish();\n\t}', 'void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,\n\t int xclass)\n\t{\n\tunsigned char *p= *pp;\n\tint i;\n\ti=(constructed)?V_ASN1_CONSTRUCTED:0;\n\ti|=(xclass&V_ASN1_PRIVATE);\n\tif (tag < 31)\n\t\t*(p++)=i|(tag&V_ASN1_PRIMITIVE_TAG);\n\telse\n\t\t{\n\t\t*(p++)=i|V_ASN1_PRIMITIVE_TAG;\n\t\twhile (tag > 0x7f)\n\t\t\t{\n\t\t\t*(p++)=(tag&0x7f)|0x80;\n\t\t\ttag>>=7;\n\t\t\t}\n\t\t*(p++)=(tag&0x7f);\n\t\t}\n\tif ((constructed == 2) && (length == 0))\n\t\t*(p++)=0x80;\n\telse\n\t\tasn1_put_length(&p,length);\n\t*pp=p;\n\t}', 'int i2d_X509_CINF(X509_CINF *a, unsigned char **pp)\n\t{\n\tint v1=0,v2=0;\n\tM_ASN1_I2D_vars(a);\n\tM_ASN1_I2D_len_EXP_opt(a->version,i2d_ASN1_INTEGER,0,v1);\n\tM_ASN1_I2D_len(a->serialNumber,\t\ti2d_ASN1_INTEGER);\n\tM_ASN1_I2D_len(a->signature,\t\ti2d_X509_ALGOR);\n\tM_ASN1_I2D_len(a->issuer,\t\ti2d_X509_NAME);\n\tM_ASN1_I2D_len(a->validity,\t\ti2d_X509_VAL);\n\tM_ASN1_I2D_len(a->subject,\t\ti2d_X509_NAME);\n\tM_ASN1_I2D_len(a->key,\t\t\ti2d_X509_PUBKEY);\n\tM_ASN1_I2D_len_IMP_opt(a->issuerUID,\ti2d_ASN1_BIT_STRING);\n\tM_ASN1_I2D_len_IMP_opt(a->subjectUID,\ti2d_ASN1_BIT_STRING);\n\tM_ASN1_I2D_len_EXP_SEQUENCE_opt_type(X509_EXTENSION,a->extensions,\n\t\t\t\t\t i2d_X509_EXTENSION,3,\n\t\t\t\t\t V_ASN1_SEQUENCE,v2);\n\tM_ASN1_I2D_seq_total();\n\tM_ASN1_I2D_put_EXP_opt(a->version,i2d_ASN1_INTEGER,0,v1);\n\tM_ASN1_I2D_put(a->serialNumber,\t\ti2d_ASN1_INTEGER);\n\tM_ASN1_I2D_put(a->signature,\t\ti2d_X509_ALGOR);\n\tM_ASN1_I2D_put(a->issuer,\t\ti2d_X509_NAME);\n\tM_ASN1_I2D_put(a->validity,\t\ti2d_X509_VAL);\n\tM_ASN1_I2D_put(a->subject,\t\ti2d_X509_NAME);\n\tM_ASN1_I2D_put(a->key,\t\t\ti2d_X509_PUBKEY);\n\tM_ASN1_I2D_put_IMP_opt(a->issuerUID,\ti2d_ASN1_BIT_STRING,1);\n\tM_ASN1_I2D_put_IMP_opt(a->subjectUID,\ti2d_ASN1_BIT_STRING,2);\n\tM_ASN1_I2D_put_EXP_SEQUENCE_opt_type(X509_EXTENSION,a->extensions,\n\t\t\t\t\t i2d_X509_EXTENSION,3,\n\t\t\t\t\t V_ASN1_SEQUENCE,v2);\n\tM_ASN1_I2D_finish();\n\t}', 'int i2d_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp)\n\t{\n\tint pad=0,ret,r,i,t;\n\tunsigned char *p,*n,pb=0;\n\tif ((a == NULL) || (a->data == NULL)) return(0);\n\tt=a->type;\n\tif (a->length == 0)\n\t\tret=1;\n\telse\n\t\t{\n\t\tret=a->length;\n\t\ti=a->data[0];\n\t\tif ((t == V_ASN1_INTEGER) && (i > 127)) {\n\t\t\tpad=1;\n\t\t\tpb=0;\n\t\t} else if(t == V_ASN1_NEG_INTEGER) {\n\t\t\tif(i>128) {\n\t\t\t\tpad=1;\n\t\t\t\tpb=0xFF;\n\t\t\t} else if(i == 128) {\n\t\t\t\tfor(i = 1; i < a->length; i++) if(a->data[i]) {\n\t\t\t\t\t\tpad=1;\n\t\t\t\t\t\tpb=0xFF;\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tret+=pad;\n\t\t}\n\tr=ASN1_object_size(0,ret,V_ASN1_INTEGER);\n\tif (pp == NULL) return(r);\n\tp= *pp;\n\tASN1_put_object(&p,0,ret,V_ASN1_INTEGER,V_ASN1_UNIVERSAL);\n\tif (pad) *(p++)=pb;\n\tif (a->length == 0) *(p++)=0;\n\telse if (t == V_ASN1_INTEGER) memcpy(p,a->data,(unsigned int)a->length);\n\telse {\n\t\tn=a->data + a->length - 1;\n\t\tp += a->length - 1;\n\t\ti = a->length;\n\t\twhile(!*n) {\n\t\t\t*(p--) = 0;\n\t\t\tn--;\n\t\t\ti--;\n\t\t}\n\t\t*(p--) = ((*(n--)) ^ 0xff) + 1;\n\t\ti--;\n\t\tfor(;i > 0; i--) *(p--) = *(n--) ^ 0xff;\n\t}\n\t*pp+=r;\n\treturn(r);\n\t}', 'static void asn1_put_length(unsigned char **pp, int length)\n\t{\n\tunsigned char *p= *pp;\n\tint i,l;\n\tif (length <= 127)\n\t\t*(p++)=(unsigned char)length;\n\telse\n\t\t{\n\t\tl=length;\n\t\tfor (i=0; l > 0; i++)\n\t\t\tl>>=8;\n\t\t*(p++)=i|0x80;\n\t\tl=i;\n\t\twhile (i-- > 0)\n\t\t\t{\n\t\t\tp[i]=length&0xff;\n\t\t\tlength>>=8;\n\t\t\t}\n\t\tp+=l;\n\t\t}\n\t*pp=p;\n\t}'] |
35,446 | 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)];
} | ['static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)\n{\n BIGNUM *kinv = NULL, *r = NULL, *s = NULL;\n BIGNUM *m;\n BIGNUM *xr;\n BN_CTX *ctx = NULL;\n int reason = ERR_R_BN_LIB;\n DSA_SIG *ret = NULL;\n int noredo = 0;\n m = BN_new();\n xr = BN_new();\n if (!m || !xr)\n goto err;\n if (!dsa->p || !dsa->q || !dsa->g) {\n reason = DSA_R_MISSING_PARAMETERS;\n goto err;\n }\n s = BN_new();\n if (s == NULL)\n goto err;\n ctx = BN_CTX_new();\n if (ctx == NULL)\n goto err;\n redo:\n if ((dsa->kinv == NULL) || (dsa->r == NULL)) {\n if (!dsa_sign_setup(dsa, ctx, &kinv, &r, dgst, dlen))\n goto err;\n } else {\n kinv = dsa->kinv;\n dsa->kinv = NULL;\n r = dsa->r;\n dsa->r = NULL;\n noredo = 1;\n }\n if (dlen > BN_num_bytes(dsa->q))\n dlen = BN_num_bytes(dsa->q);\n if (BN_bin2bn(dgst, dlen, m) == NULL)\n goto err;\n if (!BN_mod_mul(xr, dsa->priv_key, r, dsa->q, ctx))\n goto err;\n if (!BN_add(s, xr, m))\n goto err;\n if (BN_cmp(s, dsa->q) > 0)\n if (!BN_sub(s, s, dsa->q))\n goto err;\n if (!BN_mod_mul(s, s, kinv, dsa->q, ctx))\n goto err;\n ret = DSA_SIG_new();\n if (ret == NULL)\n goto err;\n if (BN_is_zero(r) || BN_is_zero(s)) {\n if (noredo) {\n reason = DSA_R_NEED_NEW_SETUP_VALUES;\n goto err;\n }\n goto redo;\n }\n ret->r = r;\n ret->s = s;\n err:\n if (!ret) {\n DSAerr(DSA_F_DSA_DO_SIGN, reason);\n BN_free(r);\n BN_free(s);\n }\n if (ctx != NULL)\n BN_CTX_free(ctx);\n BN_clear_free(m);\n BN_clear_free(xr);\n if (kinv != NULL)\n BN_clear_free(kinv);\n return (ret);\n}', 'static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in,\n BIGNUM **kinvp, BIGNUM **rp,\n const unsigned char *dgst, int dlen)\n{\n BN_CTX *ctx = NULL;\n BIGNUM *k, *kq, *K, *kinv = NULL, *r = NULL;\n int ret = 0;\n if (!dsa->p || !dsa->q || !dsa->g) {\n DSAerr(DSA_F_DSA_SIGN_SETUP, DSA_R_MISSING_PARAMETERS);\n return 0;\n }\n k = BN_new();\n kq = BN_new();\n if (!k || !kq)\n goto err;\n if (ctx_in == NULL) {\n if ((ctx = BN_CTX_new()) == NULL)\n goto err;\n } else\n ctx = ctx_in;\n if ((r = BN_new()) == NULL)\n goto err;\n do {\n#ifndef OPENSSL_NO_SHA512\n if (dgst != NULL) {\n if (!BN_generate_dsa_nonce(k, dsa->q, dsa->priv_key, dgst,\n dlen, ctx))\n goto err;\n } else\n#endif\n if (!BN_rand_range(k, dsa->q))\n goto err;\n } while (BN_is_zero(k));\n if ((dsa->flags & DSA_FLAG_NO_EXP_CONSTTIME) == 0) {\n BN_set_flags(k, BN_FLG_CONSTTIME);\n }\n if (dsa->flags & DSA_FLAG_CACHE_MONT_P) {\n if (!BN_MONT_CTX_set_locked(&dsa->method_mont_p,\n CRYPTO_LOCK_DSA, dsa->p, ctx))\n goto err;\n }\n if ((dsa->flags & DSA_FLAG_NO_EXP_CONSTTIME) == 0) {\n if (!BN_copy(kq, k))\n goto err;\n if (!BN_add(kq, kq, dsa->q))\n goto err;\n if (BN_num_bits(kq) <= BN_num_bits(dsa->q)) {\n if (!BN_add(kq, kq, dsa->q))\n goto err;\n }\n K = kq;\n } else {\n K = k;\n }\n DSA_BN_MOD_EXP(goto err, dsa, r, dsa->g, K, dsa->p, ctx,\n dsa->method_mont_p);\n if (!BN_mod(r, r, dsa->q, ctx))\n goto err;\n if ((kinv = BN_mod_inverse(NULL, k, dsa->q, ctx)) == NULL)\n goto err;\n if (*kinvp != NULL)\n BN_clear_free(*kinvp);\n *kinvp = kinv;\n kinv = NULL;\n if (*rp != NULL)\n BN_clear_free(*rp);\n *rp = r;\n ret = 1;\n err:\n if (!ret) {\n DSAerr(DSA_F_DSA_SIGN_SETUP, ERR_R_BN_LIB);\n if (r != NULL)\n BN_clear_free(r);\n }\n if (ctx_in == NULL)\n BN_CTX_free(ctx);\n BN_clear_free(k);\n BN_clear_free(kq);\n return (ret);\n}', 'int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,\n BN_CTX *ctx)\n{\n BIGNUM *t;\n int ret = 0;\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(m);\n BN_CTX_start(ctx);\n if ((t = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (a == b) {\n if (!BN_sqr(t, a, ctx))\n goto err;\n } else {\n if (!BN_mul(t, a, b, ctx))\n goto err;\n }\n if (!BN_nnmod(r, t, m, ctx))\n goto err;\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return (ret);\n}', '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_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}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}'] |
35,447 | 0 | https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/crypto/lhash/lhash.c/#L370 | static void contract(LHASH *lh)
{
LHASH_NODE **n,*n1,*np;
np=lh->b[lh->p+lh->pmax-1];
lh->b[lh->p+lh->pmax-1]=NULL;
if (lh->p == 0)
{
n=(LHASH_NODE **)Realloc((char *)lh->b,
(unsigned int)(sizeof(LHASH_NODE *)*lh->pmax));
if (n == NULL)
{
lh->error++;
return;
}
lh->num_contract_reallocs++;
lh->num_alloc_nodes/=2;
lh->pmax/=2;
lh->p=lh->pmax-1;
lh->b=n;
}
else
lh->p--;
lh->num_nodes--;
lh->num_contracts++;
n1=lh->b[(int)lh->p];
if (n1 == NULL)
lh->b[(int)lh->p]=np;
else
{
while (n1->next != NULL)
n1=n1->next;
n1->next=np;
}
} | ['int ssl3_read_bytes(SSL *s, int type, char *buf, int len)\n\t{\n\tint al,i,j,n,ret;\n\tSSL3_RECORD *rr;\n\tvoid (*cb)()=NULL;\n\tBIO *bio;\n\tif (s->s3->rbuf.buf == NULL)\n\t\tif (!ssl3_setup_buffers(s))\n\t\t\treturn(-1);\n\tif (!s->in_handshake && SSL_in_init(s))\n\t\t{\n\t\ti=s->handshake_func(s);\n\t\tif (i < 0) return(i);\n\t\tif (i == 0)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE);\n\t\t\treturn(-1);\n\t\t\t}\n\t\t}\nstart:\n\ts->rwstate=SSL_NOTHING;\n\trr= &(s->s3->rrec);\n\tif ((rr->length == 0) || (s->rstate == SSL_ST_READ_BODY))\n\t\t{\n\t\tret=ssl3_get_record(s);\n\t\tif (ret <= 0) return(ret);\n\t\t}\n\tif (s->s3->change_cipher_spec && (rr->type != SSL3_RT_HANDSHAKE))\n\t\t{\n\t\tal=SSL_AD_UNEXPECTED_MESSAGE;\n\t\tSSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_DATA_BETWEEN_CCS_AND_FINISHED);\n\t\tgoto err;\n\t\t}\n\tif (s->shutdown & SSL_RECEIVED_SHUTDOWN)\n\t\t{\n\t\trr->length=0;\n\t\ts->rwstate=SSL_NOTHING;\n\t\treturn(0);\n\t\t}\n\tif ((rr->type == SSL3_RT_HANDSHAKE) && (rr->length == 4) &&\n\t\t(rr->data[0] == SSL3_MT_CLIENT_REQUEST) &&\n\t\t(s->session != NULL) && (s->session->cipher != NULL))\n\t\t{\n\t\tif ((rr->data[1] != 0) || (rr->data[2] != 0) ||\n\t\t\t(rr->data[3] != 0))\n\t\t\t{\n\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\tSSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_BAD_CLIENT_REQUEST);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (SSL_is_init_finished(s) &&\n\t\t\t!(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS) &&\n\t\t\t!s->s3->renegotiate)\n\t\t\t{\n\t\t\tssl3_renegotiate(s);\n\t\t\tif (ssl3_renegotiate_check(s))\n\t\t\t\t{\n\t\t\t\tn=s->handshake_func(s);\n\t\t\t\tif (n < 0) return(n);\n\t\t\t\tif (n == 0)\n\t\t\t\t\t{\n\t\t\t\t\tSSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE);\n\t\t\t\t\treturn(-1);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\trr->length=0;\n \tgoto start;\n\t\t}\n\tif ((rr->type != type) || (s->shutdown & SSL_SENT_SHUTDOWN))\n\t\t{\n\t\tif (rr->type == SSL3_RT_ALERT)\n\t\t\t{\n\t\t\tif ((rr->length != 2) || (rr->off != 0))\n\t\t\t\t{\n\t\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\t\tSSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_BAD_ALERT_RECORD);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\ti=rr->data[0];\n\t\t\tn=rr->data[1];\n\t\t\trr->length=0;\n\t\t\tif (s->info_callback != NULL)\n\t\t\t\tcb=s->info_callback;\n\t\t\telse if (s->ctx->info_callback != NULL)\n\t\t\t\tcb=s->ctx->info_callback;\n\t\t\tif (cb != NULL)\n\t\t\t\t{\n\t\t\t\tj=(i<<8)|n;\n\t\t\t\tcb(s,SSL_CB_READ_ALERT,j);\n\t\t\t\t}\n\t\t\tif (i == 1)\n\t\t\t\t{\n\t\t\t\ts->s3->warn_alert=n;\n\t\t\t\tif (n == SSL_AD_CLOSE_NOTIFY)\n\t\t\t\t\t{\n\t\t\t\t\ts->shutdown|=SSL_RECEIVED_SHUTDOWN;\n\t\t\t\t\treturn(0);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\telse if (i == 2)\n\t\t\t\t{\n\t\t\t\tchar tmp[16];\n\t\t\t\ts->rwstate=SSL_NOTHING;\n\t\t\t\ts->s3->fatal_alert=n;\n\t\t\t\tSSLerr(SSL_F_SSL3_READ_BYTES,\n\t\t\t\t\tSSL_AD_REASON_OFFSET+n);\n\t\t\t\tsprintf(tmp,"%d",n);\n\t\t\t\tERR_add_error_data(2,"SSL alert number ",tmp);\n\t\t\t\ts->shutdown|=SSL_RECEIVED_SHUTDOWN;\n\t\t\t\tSSL_CTX_remove_session(s->ctx,s->session);\n\t\t\t\treturn(0);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tal=SSL_AD_ILLEGAL_PARAMETER;\n\t\t\t\tSSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_UNKNOWN_ALERT_TYPE);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\trr->length=0;\n\t\t\tgoto start;\n\t\t\t}\n\t\tif (s->shutdown & SSL_SENT_SHUTDOWN)\n\t\t\t{\n\t\t\ts->rwstate=SSL_NOTHING;\n\t\t\trr->length=0;\n\t\t\treturn(0);\n\t\t\t}\n\t\tif (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC)\n\t\t\t{\n\t\t\tif (\t(rr->length != 1) || (rr->off != 0) ||\n\t\t\t\t(rr->data[0] != SSL3_MT_CCS))\n\t\t\t\t{\n\t\t\t\ti=SSL_AD_ILLEGAL_PARAMETER;\n\t\t\t\tSSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_BAD_CHANGE_CIPHER_SPEC);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\trr->length=0;\n\t\t\ts->s3->change_cipher_spec=1;\n\t\t\tif (!do_change_cipher_spec(s))\n\t\t\t\tgoto err;\n\t\t\telse\n\t\t\t\tgoto start;\n\t\t\t}\n\t\tif ((rr->type == SSL3_RT_HANDSHAKE) &&\n\t\t\t!s->in_handshake)\n\t\t\t{\n\t\t\tif (((s->state&SSL_ST_MASK) == SSL_ST_OK) &&\n\t\t\t\t!(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS))\n\t\t\t\t{\n\t\t\t\ts->state=SSL_ST_BEFORE|(s->server)\n\t\t\t\t\t\t?SSL_ST_ACCEPT\n\t\t\t\t\t\t:SSL_ST_CONNECT;\n\t\t\t\ts->new_session=1;\n\t\t\t\t}\n\t\t\tn=s->handshake_func(s);\n\t\t\tif (n < 0) return(n);\n\t\t\tif (n == 0)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE);\n\t\t\t\treturn(-1);\n\t\t\t\t}\n\t\t\ts->rwstate=SSL_READING;\n\t\t\tbio=SSL_get_rbio(s);\n\t\t\tBIO_clear_retry_flags(bio);\n\t\t\tBIO_set_retry_read(bio);\n\t\t\treturn(-1);\n\t\t\t}\n\t\tswitch (rr->type)\n\t\t\t{\n\t\tdefault:\n#ifndef NO_TLS\n\t\t\tif (s->version == TLS1_VERSION)\n\t\t\t\t{\n\t\t\t\tgoto start;\n\t\t\t\t}\n#endif\n\t\tcase SSL3_RT_CHANGE_CIPHER_SPEC:\n\t\tcase SSL3_RT_ALERT:\n\t\tcase SSL3_RT_HANDSHAKE:\n\t\t\tal=SSL_AD_UNEXPECTED_MESSAGE;\n\t\t\tSSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_UNEXPECTED_RECORD);\n\t\t\tgoto f_err;\n\t\tcase SSL3_RT_APPLICATION_DATA:\n\t\t\tif (s->s3->in_read_app_data &&\n\t\t\t\t(s->s3->total_renegotiations != 0) &&\n\t\t\t\t((\n\t\t\t\t (s->state & SSL_ST_CONNECT) &&\n\t\t\t\t (s->state >= SSL3_ST_CW_CLNT_HELLO_A) &&\n\t\t\t\t (s->state <= SSL3_ST_CR_SRVR_HELLO_A)\n\t\t\t\t ) || (\n\t\t\t\t (s->state & SSL_ST_ACCEPT) &&\n\t\t\t\t (s->state <= SSL3_ST_SW_HELLO_REQ_A) &&\n\t\t\t\t (s->state >= SSL3_ST_SR_CLNT_HELLO_A)\n\t\t\t\t )\n\t\t\t\t))\n\t\t\t\t{\n\t\t\t\ts->s3->in_read_app_data=0;\n\t\t\t\treturn(-1);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tal=SSL_AD_UNEXPECTED_MESSAGE;\n\t\t\t\tSSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_UNEXPECTED_RECORD);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\tif (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) &&\n\t\t(s->enc_read_ctx == NULL))\n\t\t{\n\t\tal=SSL_AD_UNEXPECTED_MESSAGE;\n\t\tSSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_APP_DATA_IN_HANDSHAKE);\n\t\tgoto f_err;\n\t\t}\n\tif (len <= 0) return(len);\n\tif ((unsigned int)len > rr->length)\n\t\tn=rr->length;\n\telse\n\t\tn=len;\n\tmemcpy(buf,&(rr->data[rr->off]),(unsigned int)n);\n\trr->length-=n;\n\trr->off+=n;\n\tif (rr->length <= 0)\n\t\t{\n\t\ts->rstate=SSL_ST_READ_HEADER;\n\t\trr->off=0;\n\t\t}\n\tif (type == SSL3_RT_HANDSHAKE)\n\t\tssl3_finish_mac(s,buf,n);\n\treturn(n);\nf_err:\n\tssl3_send_alert(s,SSL3_AL_FATAL,al);\nerr:\n\treturn(-1);\n\t}', 'static int ssl3_get_record(SSL *s)\n\t{\n\tint ssl_major,ssl_minor,al;\n\tint n,i,ret= -1;\n\tSSL3_BUFFER *rb;\n\tSSL3_RECORD *rr;\n\tSSL_SESSION *sess;\n\tunsigned char *p;\n\tunsigned char md[EVP_MAX_MD_SIZE];\n\tshort version;\n\tunsigned int mac_size;\n\tint clear=0,extra;\n\trr= &(s->s3->rrec);\n\trb= &(s->s3->rbuf);\n\tsess=s->session;\n\tif (s->options & SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER)\n\t\textra=SSL3_RT_MAX_EXTRA;\n\telse\n\t\textra=0;\nagain:\n\tif (\t(s->rstate != SSL_ST_READ_BODY) ||\n\t\t(s->packet_length < SSL3_RT_HEADER_LENGTH))\n\t\t{\n\t\tn=ssl3_read_n(s,SSL3_RT_HEADER_LENGTH,\n\t\t\tSSL3_RT_MAX_PACKET_SIZE,0);\n\t\tif (n <= 0) return(n);\n\t\ts->rstate=SSL_ST_READ_BODY;\n\t\tp=s->packet;\n\t\trr->type= *(p++);\n\t\tssl_major= *(p++);\n\t\tssl_minor= *(p++);\n\t\tversion=(ssl_major<<8)|ssl_minor;\n\t\tn2s(p,rr->length);\n\t\tif (s->first_packet)\n\t\t\t{\n\t\t\ts->first_packet=0;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (version != s->version)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_WRONG_VERSION_NUMBER);\n\t\t\t\ts->version=version;\n\t\t\t\tal=SSL_AD_PROTOCOL_VERSION;\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\t}\n\t\tif ((version>>8) != SSL3_VERSION_MAJOR)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_WRONG_VERSION_NUMBER);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (rr->length >\n\t\t\t(unsigned int)SSL3_RT_MAX_ENCRYPTED_LENGTH+extra)\n\t\t\t{\n\t\t\tal=SSL_AD_RECORD_OVERFLOW;\n\t\t\tSSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_PACKET_LENGTH_TOO_LONG);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\ts->rstate=SSL_ST_READ_BODY;\n\t\t}\n\tif (s->rstate == SSL_ST_READ_BODY)\n\t\t{\n\t\tif (rr->length > (s->packet_length-SSL3_RT_HEADER_LENGTH))\n\t\t\t{\n\t\t\ti=rr->length;\n\t\t\tn=ssl3_read_n(s,i,i,1);\n\t\t\tif (n <= 0) return(n);\n\t\t\t}\n\t\ts->rstate=SSL_ST_READ_HEADER;\n\t\t}\n\trr->input= &(s->packet[SSL3_RT_HEADER_LENGTH]);\n\ts->rstate=SSL_ST_READ_HEADER;\n\tif (rr->length > (unsigned int)SSL3_RT_MAX_ENCRYPTED_LENGTH+extra)\n\t\t{\n\t\tal=SSL_AD_RECORD_OVERFLOW;\n\t\tSSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_ENCRYPTED_LENGTH_TOO_LONG);\n\t\tgoto f_err;\n\t\t}\n\trr->data=rr->input;\n\tif (!s->method->ssl3_enc->enc(s,0))\n\t\t{\n\t\tal=SSL_AD_DECRYPT_ERROR;\n\t\tgoto f_err;\n\t\t}\n#ifdef TLS_DEBUG\nprintf("dec %d\\n",rr->length);\n{ unsigned int z; for (z=0; z<rr->length; z++) printf("%02X%c",rr->data[z],((z+1)%16)?\' \':\'\\n\'); }\nprintf("\\n");\n#endif\n\tif (\t(sess == NULL) ||\n\t\t(s->enc_read_ctx == NULL) ||\n\t\t(s->read_hash == NULL))\n\t\tclear=1;\n\tif (!clear)\n\t\t{\n\t\tmac_size=EVP_MD_size(s->read_hash);\n\t\tif (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH+extra+mac_size)\n\t\t\t{\n\t\t\tal=SSL_AD_RECORD_OVERFLOW;\n\t\t\tSSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_PRE_MAC_LENGTH_TOO_LONG);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tif (rr->length < mac_size)\n\t\t\t{\n\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\tSSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_LENGTH_TOO_SHORT);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\trr->length-=mac_size;\n\t\ti=s->method->ssl3_enc->mac(s,md,0);\n\t\tif (memcmp(md,&(rr->data[rr->length]),mac_size) != 0)\n\t\t\t{\n\t\t\tal=SSL_AD_BAD_RECORD_MAC;\n\t\t\tSSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_BAD_MAC_DECODE);\n\t\t\tret= -1;\n\t\t\tgoto f_err;\n\t\t\t}\n\t\t}\n\tif (s->expand != NULL)\n\t\t{\n\t\tif (rr->length >\n\t\t\t(unsigned int)SSL3_RT_MAX_COMPRESSED_LENGTH+extra)\n\t\t\t{\n\t\t\tal=SSL_AD_RECORD_OVERFLOW;\n\t\t\tSSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_COMPRESSED_LENGTH_TOO_LONG);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tif (!do_uncompress(s))\n\t\t\t{\n\t\t\tal=SSL_AD_DECOMPRESSION_FAILURE;\n\t\t\tSSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_BAD_DECOMPRESSION);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\t}\n\tif (rr->length > (unsigned int)SSL3_RT_MAX_PLAIN_LENGTH+extra)\n\t\t{\n\t\tal=SSL_AD_RECORD_OVERFLOW;\n\t\tSSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_DATA_LENGTH_TOO_LONG);\n\t\tgoto f_err;\n\t\t}\n\trr->off=0;\n\ts->packet_length=0;\n\tif (rr->length == 0) goto again;\n\treturn(1);\nf_err:\n\tssl3_send_alert(s,SSL3_AL_FATAL,al);\nerr:\n\treturn(ret);\n\t}', 'void ssl3_send_alert(SSL *s, int level, int desc)\n\t{\n\tdesc=s->method->ssl3_enc->alert_value(desc);\n\tif (desc < 0) return;\n\tif ((level == 2) && (s->session != NULL))\n\t\tSSL_CTX_remove_session(s->ctx,s->session);\n\ts->s3->alert_dispatch=1;\n\ts->s3->send_alert[0]=level;\n\ts->s3->send_alert[1]=desc;\n\tif (s->s3->wbuf.left == 0)\n\t\tssl3_dispatch_alert(s);\n\t}', 'int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)\n\t{\n\tSSL_SESSION *r;\n\tint ret=0;\n\tif ((c != NULL) && (c->session_id_length != 0))\n\t\t{\n\t\tCRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);\n\t\tr=(SSL_SESSION *)lh_delete(ctx->sessions,(char *)c);\n\t\tif (r != NULL)\n\t\t\t{\n\t\t\tret=1;\n\t\t\tSSL_SESSION_list_remove(ctx,c);\n\t\t\t}\n\t\tCRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n\t\tif (ret)\n\t\t\t{\n\t\t\tr->not_resumable=1;\n\t\t\tif (ctx->remove_session_cb != NULL)\n\t\t\t\tctx->remove_session_cb(ctx,r);\n\t\t\tSSL_SESSION_free(r);\n\t\t\t}\n\t\t}\n\telse\n\t\tret=0;\n\treturn(ret);\n\t}', 'char *lh_delete(LHASH *lh, char *data)\n\t{\n\tunsigned long hash;\n\tLHASH_NODE *nn,**rn;\n\tchar *ret;\n\tlh->error=0;\n\trn=getrn(lh,data,&hash);\n\tif (*rn == NULL)\n\t\t{\n\t\tlh->num_no_delete++;\n\t\treturn(NULL);\n\t\t}\n\telse\n\t\t{\n\t\tnn= *rn;\n\t\t*rn=nn->next;\n\t\tret=nn->data;\n\t\tFree((char *)nn);\n\t\tlh->num_delete++;\n\t\t}\n\tlh->num_items--;\n\tif ((lh->num_nodes > MIN_NODES) &&\n\t\t(lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes)))\n\t\tcontract(lh);\n\treturn(ret);\n\t}', 'static void contract(LHASH *lh)\n\t{\n\tLHASH_NODE **n,*n1,*np;\n\tnp=lh->b[lh->p+lh->pmax-1];\n\tlh->b[lh->p+lh->pmax-1]=NULL;\n\tif (lh->p == 0)\n\t\t{\n\t\tn=(LHASH_NODE **)Realloc((char *)lh->b,\n\t\t\t(unsigned int)(sizeof(LHASH_NODE *)*lh->pmax));\n\t\tif (n == NULL)\n\t\t\t{\n\t\t\tlh->error++;\n\t\t\treturn;\n\t\t\t}\n\t\tlh->num_contract_reallocs++;\n\t\tlh->num_alloc_nodes/=2;\n\t\tlh->pmax/=2;\n\t\tlh->p=lh->pmax-1;\n\t\tlh->b=n;\n\t\t}\n\telse\n\t\tlh->p--;\n\tlh->num_nodes--;\n\tlh->num_contracts++;\n\tn1=lh->b[(int)lh->p];\n\tif (n1 == NULL)\n\t\tlh->b[(int)lh->p]=np;\n\telse\n\t\t{\n\t\twhile (n1->next != NULL)\n\t\t\tn1=n1->next;\n\t\tn1->next=np;\n\t\t}\n\t}'] |
35,448 | 0 | https://gitlab.com/libtiff/libtiff/blob/6c63f17749ad8fc2e1e94e056b18cbc557ab59f1/libtiff/tif_strip.c/#L65 | uint32
TIFFNumberOfStrips(TIFF* tif)
{
TIFFDirectory *td = &tif->tif_dir;
uint32 nstrips;
nstrips = (td->td_rowsperstrip == (uint32) -1 ? 1 :
TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip));
if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
nstrips = _TIFFMultiply32(tif, nstrips, (uint32)td->td_samplesperpixel,
"TIFFNumberOfStrips");
return (nstrips);
} | ['tsize_t t2p_write_pdf(T2P* t2p, TIFF* input, TIFF* output){\n\ttsize_t written=0;\n\tttile_t i2=0;\n\ttsize_t streamlen=0;\n\tuint16 i=0;\n\tt2p_read_tiff_init(t2p, input);\n\tif(t2p->t2p_error!=T2P_ERR_OK){return(0);}\n\tt2p->pdf_xrefoffsets= (uint32*) _TIFFmalloc(TIFFSafeMultiply(tmsize_t,t2p->pdf_xrefcount,sizeof(uint32)) );\n\tif(t2p->pdf_xrefoffsets==NULL){\n\t\tTIFFError(\n\t\t\tTIFF2PDF_MODULE,\n\t\t\t"Can\'t allocate %u bytes of memory for t2p_write_pdf",\n\t\t\t(unsigned int) (t2p->pdf_xrefcount * sizeof(uint32)) );\n\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\treturn(written);\n\t}\n\tt2p->pdf_xrefcount=0;\n\tt2p->pdf_catalog=1;\n\tt2p->pdf_info=2;\n\tt2p->pdf_pages=3;\n\twritten += t2p_write_pdf_header(t2p, output);\n\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\tt2p->pdf_catalog=t2p->pdf_xrefcount;\n\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\twritten += t2p_write_pdf_catalog(t2p, output);\n\twritten += t2p_write_pdf_obj_end(output);\n\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\tt2p->pdf_info=t2p->pdf_xrefcount;\n\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\twritten += t2p_write_pdf_info(t2p, input, output);\n\twritten += t2p_write_pdf_obj_end(output);\n\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\tt2p->pdf_pages=t2p->pdf_xrefcount;\n\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\twritten += t2p_write_pdf_pages(t2p, output);\n\twritten += t2p_write_pdf_obj_end(output);\n\tfor(t2p->pdf_page=0;t2p->pdf_page<t2p->tiff_pagecount;t2p->pdf_page++){\n\t\tt2p_read_tiff_data(t2p, input);\n\t\tif(t2p->t2p_error!=T2P_ERR_OK){return(0);}\n\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\twritten += t2p_write_pdf_page(t2p->pdf_xrefcount, t2p, output);\n\t\twritten += t2p_write_pdf_obj_end(output);\n\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\twritten += t2p_write_pdf_stream_dict_start(output);\n\t\twritten += t2p_write_pdf_stream_dict(0, t2p->pdf_xrefcount+1, output);\n\t\twritten += t2p_write_pdf_stream_dict_end(output);\n\t\twritten += t2p_write_pdf_stream_start(output);\n\t\tstreamlen=written;\n\t\twritten += t2p_write_pdf_page_content_stream(t2p, output);\n\t\tstreamlen=written-streamlen;\n\t\twritten += t2p_write_pdf_stream_end(output);\n\t\twritten += t2p_write_pdf_obj_end(output);\n\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\twritten += t2p_write_pdf_stream_length(streamlen, output);\n\t\twritten += t2p_write_pdf_obj_end(output);\n\t\tif(t2p->tiff_transferfunctioncount != 0){\n\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\twritten += t2p_write_pdf_transfer(t2p, output);\n\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t\tfor(i=0; i < t2p->tiff_transferfunctioncount; i++){\n\t\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\t\twritten += t2p_write_pdf_stream_dict_start(output);\n\t\t\t\twritten += t2p_write_pdf_transfer_dict(t2p, output, i);\n\t\t\t\twritten += t2p_write_pdf_stream_dict_end(output);\n\t\t\t\twritten += t2p_write_pdf_stream_start(output);\n\t\t\t\twritten += t2p_write_pdf_transfer_stream(t2p, output, i);\n\t\t\t\twritten += t2p_write_pdf_stream_end(output);\n\t\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t\t}\n\t\t}\n\t\tif( (t2p->pdf_colorspace & T2P_CS_PALETTE) != 0){\n\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\tt2p->pdf_palettecs=t2p->pdf_xrefcount;\n\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\twritten += t2p_write_pdf_stream_dict_start(output);\n\t\t\twritten += t2p_write_pdf_stream_dict(t2p->pdf_palettesize, 0, output);\n\t\t\twritten += t2p_write_pdf_stream_dict_end(output);\n\t\t\twritten += t2p_write_pdf_stream_start(output);\n\t\t\twritten += t2p_write_pdf_xobject_palettecs_stream(t2p, output);\n\t\t\twritten += t2p_write_pdf_stream_end(output);\n\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t}\n\t\tif( (t2p->pdf_colorspace & T2P_CS_ICCBASED) != 0){\n\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\tt2p->pdf_icccs=t2p->pdf_xrefcount;\n\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\twritten += t2p_write_pdf_stream_dict_start(output);\n\t\t\twritten += t2p_write_pdf_xobject_icccs_dict(t2p, output);\n\t\t\twritten += t2p_write_pdf_stream_dict_end(output);\n\t\t\twritten += t2p_write_pdf_stream_start(output);\n\t\t\twritten += t2p_write_pdf_xobject_icccs_stream(t2p, output);\n\t\t\twritten += t2p_write_pdf_stream_end(output);\n\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t}\n\t\tif(t2p->tiff_tiles[t2p->pdf_page].tiles_tilecount !=0){\n\t\t\tfor(i2=0;i2<t2p->tiff_tiles[t2p->pdf_page].tiles_tilecount;i2++){\n\t\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\t\twritten += t2p_write_pdf_stream_dict_start(output);\n\t\t\t\twritten += t2p_write_pdf_xobject_stream_dict(\n\t\t\t\t\ti2+1,\n\t\t\t\t\tt2p,\n\t\t\t\t\toutput);\n\t\t\t\twritten += t2p_write_pdf_stream_dict_end(output);\n\t\t\t\twritten += t2p_write_pdf_stream_start(output);\n\t\t\t\tstreamlen=written;\n\t\t\t\tt2p_read_tiff_size_tile(t2p, input, i2);\n\t\t\t\twritten += t2p_readwrite_pdf_image_tile(t2p, input, output, i2);\n\t\t\t\tt2p_write_advance_directory(t2p, output);\n\t\t\t\tif(t2p->t2p_error!=T2P_ERR_OK){return(0);}\n\t\t\t\tstreamlen=written-streamlen;\n\t\t\t\twritten += t2p_write_pdf_stream_end(output);\n\t\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\t\twritten += t2p_write_pdf_stream_length(streamlen, output);\n\t\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t\t}\n\t\t} else {\n\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\twritten += t2p_write_pdf_stream_dict_start(output);\n\t\t\twritten += t2p_write_pdf_xobject_stream_dict(\n\t\t\t\t0,\n\t\t\t\tt2p,\n\t\t\t\toutput);\n\t\t\twritten += t2p_write_pdf_stream_dict_end(output);\n\t\t\twritten += t2p_write_pdf_stream_start(output);\n\t\t\tstreamlen=written;\n\t\t\tt2p_read_tiff_size(t2p, input);\n\t\t\tif (t2p->tiff_maxdatasize && (t2p->tiff_datasize > t2p->tiff_maxdatasize)) {\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Allocation of " TIFF_UINT64_FORMAT " bytes is forbidden. Limit is " TIFF_UINT64_FORMAT ". Use -m option to change limit",\n\t\t\t\t\t(uint64)t2p->tiff_datasize, (uint64)t2p->tiff_maxdatasize);\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn (0);\n\t\t\t}\n\t\t\twritten += t2p_readwrite_pdf_image(t2p, input, output);\n\t\t\tt2p_write_advance_directory(t2p, output);\n\t\t\tif(t2p->t2p_error!=T2P_ERR_OK){return(0);}\n\t\t\tstreamlen=written-streamlen;\n\t\t\twritten += t2p_write_pdf_stream_end(output);\n\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\twritten += t2p_write_pdf_stream_length(streamlen, output);\n\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t}\n\t}\n\tt2p->pdf_startxref = written;\n\twritten += t2p_write_pdf_xreftable(t2p, output);\n\twritten += t2p_write_pdf_trailer(t2p, output);\n\tt2p_disable(output);\n\treturn(written);\n}', 'void t2p_read_tiff_data(T2P* t2p, TIFF* input){\n\tint i=0;\n\tuint16* r = NULL;\n\tuint16* g = NULL;\n\tuint16* b = NULL;\n\tuint16* a = NULL;\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(r == NULL || g == NULL || b == NULL){\n\t\t\t\tTIFFError(\n\t\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t\t"Error getting 3 components from color map");\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(r == NULL || g == NULL || b == NULL || a == NULL){\n\t\t\t\tTIFFError(\n\t\t\t\t\tTIFF2PDF_MODULE,\n\t\t\t\t\t"Error getting 4 components from color map");\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 && t2p->tiff_planar!=PLANARCONFIG_SEPARATE){\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] != (uint16*) NULL) &&\n (t2p->tiff_transferfunction[2] != (uint16*) NULL)\n ) {\n\t\t\tt2p->tiff_transferfunctioncount=3;\n\t\t} else {\n\t\t\tt2p->tiff_transferfunctioncount=1;\n\t\t}\n\t} else {\n\t\tt2p->tiff_transferfunctioncount=0;\n\t}\n\tif(TIFFGetField(input, TIFFTAG_WHITEPOINT, &xfloatp)!=0){\n\t\tt2p->tiff_whitechromaticities[0]=xfloatp[0];\n\t\tt2p->tiff_whitechromaticities[1]=xfloatp[1];\n\t\tif(t2p->pdf_colorspace & T2P_CS_GRAY){\n\t\t\tt2p->pdf_colorspace |= T2P_CS_CALGRAY;\n\t\t}\n\t\tif(t2p->pdf_colorspace & T2P_CS_RGB){\n\t\t\tt2p->pdf_colorspace |= T2P_CS_CALRGB;\n\t\t}\n\t}\n\tif(TIFFGetField(input, TIFFTAG_PRIMARYCHROMATICITIES, &xfloatp)!=0){\n\t\tt2p->tiff_primarychromaticities[0]=xfloatp[0];\n\t\tt2p->tiff_primarychromaticities[1]=xfloatp[1];\n\t\tt2p->tiff_primarychromaticities[2]=xfloatp[2];\n\t\tt2p->tiff_primarychromaticities[3]=xfloatp[3];\n\t\tt2p->tiff_primarychromaticities[4]=xfloatp[4];\n\t\tt2p->tiff_primarychromaticities[5]=xfloatp[5];\n\t\tif(t2p->pdf_colorspace & T2P_CS_RGB){\n\t\t\tt2p->pdf_colorspace |= T2P_CS_CALRGB;\n\t\t}\n\t}\n\tif(t2p->pdf_colorspace & T2P_CS_LAB){\n\t\tif(TIFFGetField(input, TIFFTAG_WHITEPOINT, &xfloatp) != 0){\n\t\t\tt2p->tiff_whitechromaticities[0]=xfloatp[0];\n\t\t\tt2p->tiff_whitechromaticities[1]=xfloatp[1];\n\t\t} else {\n\t\t\tt2p->tiff_whitechromaticities[0]=0.3457F;\n\t\t\tt2p->tiff_whitechromaticities[1]=0.3585F;\n\t\t}\n\t}\n\tif(TIFFGetField(input,\n\t\tTIFFTAG_ICCPROFILE,\n\t\t&(t2p->tiff_iccprofilelength),\n\t\t&(t2p->tiff_iccprofile))!=0){\n\t\tt2p->pdf_colorspace |= T2P_CS_ICCBASED;\n\t} else {\n\t\tt2p->tiff_iccprofilelength=0;\n\t\tt2p->tiff_iccprofile=NULL;\n\t}\n#ifdef CCITT_SUPPORT\n\tif( t2p->tiff_bitspersample==1 &&\n\t\tt2p->tiff_samplesperpixel==1){\n\t\tt2p->pdf_compression = T2P_COMPRESS_G4;\n\t}\n#endif\n\treturn;\n}', 'int\nTIFFSetDirectory(TIFF* tif, uint16 dirn)\n{\n\tuint64 nextdir;\n\tuint16 n;\n\tif (!(tif->tif_flags&TIFF_BIGTIFF))\n\t\tnextdir = tif->tif_header.classic.tiff_diroff;\n\telse\n\t\tnextdir = tif->tif_header.big.tiff_diroff;\n\tfor (n = dirn; n > 0 && nextdir != 0; n--)\n\t\tif (!TIFFAdvanceDirectory(tif, &nextdir, NULL))\n\t\t\treturn (0);\n\ttif->tif_nextdiroff = nextdir;\n\ttif->tif_curdir = (dirn - n) - 1;\n\ttif->tif_dirnumber = 0;\n\treturn (TIFFReadDirectory(tif));\n}', 'void t2p_read_tiff_size(T2P* t2p, TIFF* input){\n\tuint64* sbc=NULL;\n#if defined(JPEG_SUPPORT) || defined (OJPEG_SUPPORT)\n\tunsigned char* jpt=NULL;\n\ttstrip_t i=0;\n\ttstrip_t stripcount=0;\n#endif\n uint64 k = 0;\n\tif(t2p->pdf_transcode == T2P_TRANSCODE_RAW){\n#ifdef CCITT_SUPPORT\n\t\tif(t2p->pdf_compression == T2P_COMPRESS_G4 ){\n\t\t\tTIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc);\n if (sbc[0] != (uint64)(tmsize_t)sbc[0]) {\n TIFFError(TIFF2PDF_MODULE, "Integer overflow");\n t2p->t2p_error = T2P_ERR_ERROR;\n }\n\t\t\tt2p->tiff_datasize=(tmsize_t)sbc[0];\n\t\t\treturn;\n\t\t}\n#endif\n#ifdef ZIP_SUPPORT\n\t\tif(t2p->pdf_compression == T2P_COMPRESS_ZIP){\n\t\t\tTIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc);\n if (sbc[0] != (uint64)(tmsize_t)sbc[0]) {\n TIFFError(TIFF2PDF_MODULE, "Integer overflow");\n t2p->t2p_error = T2P_ERR_ERROR;\n }\n\t\t\tt2p->tiff_datasize=(tmsize_t)sbc[0];\n\t\t\treturn;\n\t\t}\n#endif\n#ifdef OJPEG_SUPPORT\n\t\tif(t2p->tiff_compression == COMPRESSION_OJPEG){\n\t\t\tif(!TIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc)){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Input file %s missing field: TIFFTAG_STRIPBYTECOUNTS",\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tstripcount=TIFFNumberOfStrips(input);\n\t\t\tfor(i=0;i<stripcount;i++){\n\t\t\t\tk = checkAdd64(k, sbc[i], t2p);\n\t\t\t}\n\t\t\tif(TIFFGetField(input, TIFFTAG_JPEGIFOFFSET, &(t2p->tiff_dataoffset))){\n\t\t\t\tif(t2p->tiff_dataoffset != 0){\n\t\t\t\t\tif(TIFFGetField(input, TIFFTAG_JPEGIFBYTECOUNT, &(t2p->tiff_datasize))!=0){\n\t\t\t\t\t\tif((uint64)t2p->tiff_datasize < k) {\n\t\t\t\t\t\t\tTIFFWarning(TIFF2PDF_MODULE,\n\t\t\t\t\t\t\t\t"Input file %s has short JPEG interchange file byte count",\n\t\t\t\t\t\t\t\tTIFFFileName(input));\n\t\t\t\t\t\t\tt2p->pdf_ojpegiflength=t2p->tiff_datasize;\n\t\t\t\t\t\t\tk = checkAdd64(k, t2p->tiff_datasize, t2p);\n\t\t\t\t\t\t\tk = checkAdd64(k, 6, t2p);\n\t\t\t\t\t\t\tk = checkAdd64(k, stripcount, t2p);\n\t\t\t\t\t\t\tk = checkAdd64(k, stripcount, t2p);\n\t\t\t\t\t\t\tt2p->tiff_datasize = (tsize_t) k;\n\t\t\t\t\t\t\tif ((uint64) t2p->tiff_datasize != k) {\n\t\t\t\t\t\t\t\tTIFFError(TIFF2PDF_MODULE, "Integer overflow");\n\t\t\t\t\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}else {\n\t\t\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t\t\t"Input file %s missing field: TIFFTAG_JPEGIFBYTECOUNT",\n\t\t\t\t\t\t\tTIFFFileName(input));\n\t\t\t\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tk = checkAdd64(k, stripcount, t2p);\n\t\t\tk = checkAdd64(k, stripcount, t2p);\n\t\t\tk = checkAdd64(k, 2048, t2p);\n\t\t\tt2p->tiff_datasize = (tsize_t) k;\n\t\t\tif ((uint64) t2p->tiff_datasize != k) {\n\t\t\t\tTIFFError(TIFF2PDF_MODULE, "Integer overflow");\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t}\n\t\t\treturn;\n\t\t}\n#endif\n#ifdef JPEG_SUPPORT\n\t\tif(t2p->tiff_compression == COMPRESSION_JPEG) {\n\t\t\tuint32 count = 0;\n\t\t\tif(TIFFGetField(input, TIFFTAG_JPEGTABLES, &count, &jpt) != 0 ){\n\t\t\t\tif(count > 4){\n\t\t\t\t\tk += count;\n\t\t\t\t\tk -= 2;\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tk = 2;\n\t\t\t}\n\t\t\tstripcount=TIFFNumberOfStrips(input);\n\t\t\tif(!TIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc)){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Input file %s missing field: TIFFTAG_STRIPBYTECOUNTS",\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tfor(i=0;i<stripcount;i++){\n\t\t\t\tk = checkAdd64(k, sbc[i], t2p);\n\t\t\t\tk -=2;\n\t\t\t\tk +=2;\n\t\t\t}\n\t\t\tk = checkAdd64(k, 2, t2p);\n\t\t\tk = checkAdd64(k, 6, t2p);\n\t\t\tt2p->tiff_datasize = (tsize_t) k;\n\t\t\tif ((uint64) t2p->tiff_datasize != k) {\n\t\t\t\tTIFFError(TIFF2PDF_MODULE, "Integer overflow");\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t}\n\t\t\treturn;\n\t\t}\n#endif\n\t\t(void) 0;\n\t}\n\tk = checkMultiply64(TIFFScanlineSize(input), t2p->tiff_length, t2p);\n\tif(t2p->tiff_planar==PLANARCONFIG_SEPARATE){\n\t\tk = checkMultiply64(k, t2p->tiff_samplesperpixel, t2p);\n\t}\n\tif (k == 0) {\n\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t}\n\tt2p->tiff_datasize = (tsize_t) k;\n\tif ((uint64) t2p->tiff_datasize != k) {\n\t\tTIFFError(TIFF2PDF_MODULE, "Integer overflow");\n\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t}\n\treturn;\n}', 'tsize_t t2p_readwrite_pdf_image(T2P* t2p, TIFF* input, TIFF* output){\n\ttsize_t written=0;\n\tunsigned char* buffer=NULL;\n\tunsigned char* samplebuffer=NULL;\n\ttsize_t bufferoffset=0;\n\ttsize_t samplebufferoffset=0;\n\ttsize_t read=0;\n\ttstrip_t i=0;\n\ttstrip_t j=0;\n\ttstrip_t stripcount=0;\n\ttsize_t stripsize=0;\n\ttsize_t sepstripcount=0;\n\ttsize_t sepstripsize=0;\n#ifdef OJPEG_SUPPORT\n\ttoff_t inputoffset=0;\n\tuint16 h_samp=1;\n\tuint16 v_samp=1;\n\tuint16 ri=1;\n\tuint32 rows=0;\n#endif\n#ifdef JPEG_SUPPORT\n\tunsigned char* jpt;\n\tfloat* xfloatp;\n\tuint64* sbc;\n\tunsigned char* stripbuffer;\n\ttsize_t striplength=0;\n\tuint32 max_striplength=0;\n#endif\n\tif (t2p->t2p_error != T2P_ERR_OK)\n\t\treturn(0);\n\tif(t2p->pdf_transcode == T2P_TRANSCODE_RAW){\n#ifdef CCITT_SUPPORT\n\t\tif(t2p->pdf_compression == T2P_COMPRESS_G4){\n\t\t\tbuffer = (unsigned char*)\n\t\t\t\t_TIFFmalloc(t2p->tiff_datasize);\n\t\t\tif (buffer == NULL) {\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n "Can\'t allocate %lu bytes of memory for "\n "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\tTIFFReadRawStrip(input, 0, (tdata_t) buffer,\n\t\t\t\t\t t2p->tiff_datasize);\n\t\t\tif (t2p->tiff_fillorder==FILLORDER_LSB2MSB){\n\t\t\t\t\tTIFFReverseBits(buffer,\n\t\t\t\t\t\t\tt2p->tiff_datasize);\n\t\t\t}\n\t\t\tt2pWriteFile(output, (tdata_t) buffer,\n\t\t\t\t t2p->tiff_datasize);\n\t\t\t_TIFFfree(buffer);\n\t\t\treturn(t2p->tiff_datasize);\n\t\t}\n#endif\n#ifdef ZIP_SUPPORT\n\t\tif (t2p->pdf_compression == T2P_COMPRESS_ZIP) {\n\t\t\tbuffer = (unsigned char*)\n\t\t\t\t_TIFFmalloc(t2p->tiff_datasize);\n\t\t\tif(buffer == NULL){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t"Can\'t allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s",\n\t\t\t\t\t(unsigned long) t2p->tiff_datasize,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn(0);\n\t\t\t}\n memset(buffer, 0, t2p->tiff_datasize);\n\t\t\tTIFFReadRawStrip(input, 0, (tdata_t) buffer,\n\t\t\t\t\t t2p->tiff_datasize);\n\t\t\tif (t2p->tiff_fillorder==FILLORDER_LSB2MSB) {\n\t\t\t\t\tTIFFReverseBits(buffer,\n\t\t\t\t\t\t\tt2p->tiff_datasize);\n\t\t\t}\n\t\t\tt2pWriteFile(output, (tdata_t) buffer,\n\t\t\t\t t2p->tiff_datasize);\n\t\t\t_TIFFfree(buffer);\n\t\t\treturn(t2p->tiff_datasize);\n\t\t}\n#endif\n#ifdef OJPEG_SUPPORT\n\t\tif(t2p->tiff_compression == COMPRESSION_OJPEG) {\n\t\t\tif(t2p->tiff_dataoffset != 0) {\n\t\t\t\tbuffer = (unsigned char*)\n\t\t\t\t\t_TIFFmalloc(t2p->tiff_datasize);\n\t\t\t\tif(buffer == NULL) {\n\t\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t"Can\'t allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s",\n\t\t\t\t\t\t(unsigned long) t2p->tiff_datasize,\n\t\t\t\t\t\tTIFFFileName(input));\n\t\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\t\treturn(0);\n\t\t\t\t}\n memset(buffer, 0, t2p->tiff_datasize);\n\t\t\t\tif(t2p->pdf_ojpegiflength==0){\n\t\t\t\t\tinputoffset=t2pSeekFile(input, 0,\n\t\t\t\t\t\t\t\t SEEK_CUR);\n\t\t\t\t\tt2pSeekFile(input,\n\t\t\t\t\t\t t2p->tiff_dataoffset,\n\t\t\t\t\t\t SEEK_SET);\n\t\t\t\t\tt2pReadFile(input, (tdata_t) buffer,\n\t\t\t\t\t\t t2p->tiff_datasize);\n\t\t\t\t\tt2pSeekFile(input, inputoffset,\n\t\t\t\t\t\t SEEK_SET);\n\t\t\t\t\tt2pWriteFile(output, (tdata_t) buffer,\n\t\t\t\t\t\t t2p->tiff_datasize);\n\t\t\t\t\t_TIFFfree(buffer);\n\t\t\t\t\treturn(t2p->tiff_datasize);\n\t\t\t\t} else {\n\t\t\t\t\tinputoffset=t2pSeekFile(input, 0,\n\t\t\t\t\t\t\t\t SEEK_CUR);\n\t\t\t\t\tt2pSeekFile(input,\n\t\t\t\t\t\t t2p->tiff_dataoffset,\n\t\t\t\t\t\t SEEK_SET);\n\t\t\t\t\tbufferoffset = t2pReadFile(input,\n\t\t\t\t\t\t(tdata_t) buffer,\n\t\t\t\t\t\tt2p->pdf_ojpegiflength);\n\t\t\t\t\tt2p->pdf_ojpegiflength = 0;\n\t\t\t\t\tt2pSeekFile(input, inputoffset,\n\t\t\t\t\t\t SEEK_SET);\n\t\t\t\t\tTIFFGetField(input,\n\t\t\t\t\t\t TIFFTAG_YCBCRSUBSAMPLING,\n\t\t\t\t\t\t &h_samp, &v_samp);\n\t\t\t\t\tbuffer[bufferoffset++]= 0xff;\n\t\t\t\t\tbuffer[bufferoffset++]= 0xdd;\n\t\t\t\t\tbuffer[bufferoffset++]= 0x00;\n\t\t\t\t\tbuffer[bufferoffset++]= 0x04;\n\t\t\t\t\th_samp*=8;\n\t\t\t\t\tv_samp*=8;\n\t\t\t\t\tri=(t2p->tiff_width+h_samp-1) / h_samp;\n\t\t\t\t\tTIFFGetField(input,\n\t\t\t\t\t\t TIFFTAG_ROWSPERSTRIP,\n\t\t\t\t\t\t &rows);\n\t\t\t\t\tri*=(rows+v_samp-1)/v_samp;\n\t\t\t\t\tbuffer[bufferoffset++]= (ri>>8) & 0xff;\n\t\t\t\t\tbuffer[bufferoffset++]= ri & 0xff;\n\t\t\t\t\tstripcount=TIFFNumberOfStrips(input);\n\t\t\t\t\tfor(i=0;i<stripcount;i++){\n\t\t\t\t\t\tif(i != 0 ){\n\t\t\t\t\t\t\tbuffer[bufferoffset++]=0xff;\n\t\t\t\t\t\t\tbuffer[bufferoffset++]=(0xd0 | ((i-1)%8));\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbufferoffset+=TIFFReadRawStrip(input,\n\t\t\t\t\t\t\ti,\n\t\t\t\t\t\t\t(tdata_t) &(((unsigned char*)buffer)[bufferoffset]),\n\t\t\t\t\t\t\t-1);\n\t\t\t\t\t}\n\t\t\t\t\tt2pWriteFile(output, (tdata_t) buffer, bufferoffset);\n\t\t\t\t\t_TIFFfree(buffer);\n\t\t\t\t\treturn(bufferoffset);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tif(! t2p->pdf_ojpegdata){\n\t\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t"No support for OJPEG image %s with bad tables",\n\t\t\t\t\t\tTIFFFileName(input));\n\t\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\t\treturn(0);\n\t\t\t\t}\n\t\t\t\tbuffer = (unsigned char*)\n\t\t\t\t\t_TIFFmalloc(t2p->tiff_datasize);\n\t\t\t\tif(buffer==NULL){\n\t\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t"Can\'t allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s",\n\t\t\t\t\t\t(unsigned long) t2p->tiff_datasize,\n\t\t\t\t\t\tTIFFFileName(input));\n\t\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\t\treturn(0);\n\t\t\t\t}\n memset(buffer, 0, t2p->tiff_datasize);\n\t\t\t\t_TIFFmemcpy(buffer, t2p->pdf_ojpegdata, t2p->pdf_ojpegdatalength);\n\t\t\t\tbufferoffset=t2p->pdf_ojpegdatalength;\n\t\t\t\tstripcount=TIFFNumberOfStrips(input);\n\t\t\t\tfor(i=0;i<stripcount;i++){\n\t\t\t\t\tif(i != 0){\n\t\t\t\t\t\tbuffer[bufferoffset++]=0xff;\n\t\t\t\t\t\tbuffer[bufferoffset++]=(0xd0 | ((i-1)%8));\n\t\t\t\t\t}\n\t\t\t\t\tbufferoffset+=TIFFReadRawStrip(input,\n\t\t\t\t\t\ti,\n\t\t\t\t\t\t(tdata_t) &(((unsigned char*)buffer)[bufferoffset]),\n\t\t\t\t\t\t-1);\n\t\t\t\t}\n\t\t\t\tif( ! ( (buffer[bufferoffset-1]==0xd9) && (buffer[bufferoffset-2]==0xff) ) ){\n\t\t\t\t\t\tbuffer[bufferoffset++]=0xff;\n\t\t\t\t\t\tbuffer[bufferoffset++]=0xd9;\n\t\t\t\t}\n\t\t\t\tt2pWriteFile(output, (tdata_t) buffer, bufferoffset);\n\t\t\t\t_TIFFfree(buffer);\n\t\t\t\treturn(bufferoffset);\n#if 0\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t"No support for OJPEG image %s with no JPEG File Interchange offset",\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn(0);\n#endif\n\t\t\t}\n\t\t}\n#endif\n#ifdef JPEG_SUPPORT\n\t\tif(t2p->tiff_compression == COMPRESSION_JPEG) {\n\t\t\tuint32 count = 0;\n\t\t\tbuffer = (unsigned char*)\n\t\t\t\t_TIFFmalloc(t2p->tiff_datasize);\n\t\t\tif(buffer==NULL){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t"Can\'t allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s",\n\t\t\t\t\t(unsigned long) t2p->tiff_datasize,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn(0);\n\t\t\t}\n memset(buffer, 0, t2p->tiff_datasize);\n\t\t\tif (TIFFGetField(input, TIFFTAG_JPEGTABLES, &count, &jpt) != 0) {\n\t\t\t\tif(count > 4) {\n\t\t\t\t\t_TIFFmemcpy(buffer, jpt, count);\n\t\t\t\t\tbufferoffset += count - 2;\n\t\t\t\t}\n\t\t\t}\n\t\t\tstripcount=TIFFNumberOfStrips(input);\n\t\t\tTIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc);\n\t\t\tfor(i=0;i<stripcount;i++){\n\t\t\t\tif(sbc[i]>max_striplength) max_striplength=sbc[i];\n\t\t\t}\n\t\t\tstripbuffer = (unsigned char*)\n\t\t\t\t_TIFFmalloc(max_striplength);\n\t\t\tif(stripbuffer==NULL){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t"Can\'t allocate %u bytes of memory for t2p_readwrite_pdf_image, %s",\n\t\t\t\t\tmax_striplength,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\t_TIFFfree(buffer);\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn(0);\n\t\t\t}\n\t\t\tfor(i=0;i<stripcount;i++){\n\t\t\t\tstriplength=TIFFReadRawStrip(input, i, (tdata_t) stripbuffer, -1);\n\t\t\t\tif(!t2p_process_jpeg_strip(\n\t\t\t\t\tstripbuffer,\n\t\t\t\t\t&striplength,\n\t\t\t\t\tbuffer,\n t2p->tiff_datasize,\n\t\t\t\t\t&bufferoffset,\n\t\t\t\t\ti,\n\t\t\t\t\tt2p->tiff_length)){\n\t\t\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t"Can\'t process JPEG data in input file %s",\n\t\t\t\t\t\t\tTIFFFileName(input));\n\t\t\t\t\t\t_TIFFfree(samplebuffer);\n\t\t\t\t\t\t_TIFFfree(buffer);\n\t\t\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\t\t\treturn(0);\n\t\t\t\t}\n\t\t\t}\n\t\t\tbuffer[bufferoffset++]=0xff;\n\t\t\tbuffer[bufferoffset++]=0xd9;\n\t\t\tt2pWriteFile(output, (tdata_t) buffer, bufferoffset);\n\t\t\t_TIFFfree(stripbuffer);\n\t\t\t_TIFFfree(buffer);\n\t\t\treturn(bufferoffset);\n\t\t}\n#endif\n\t\t(void)0;\n\t}\n\tif(t2p->pdf_sample==T2P_SAMPLE_NOTHING){\n\t\tbuffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize);\n\t\tif(buffer==NULL){\n\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t"Can\'t allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s",\n\t\t\t\t(unsigned long) t2p->tiff_datasize,\n\t\t\t\tTIFFFileName(input));\n\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\treturn(0);\n\t\t}\n memset(buffer, 0, t2p->tiff_datasize);\n\t\tstripsize=TIFFStripSize(input);\n\t\tstripcount=TIFFNumberOfStrips(input);\n\t\tfor(i=0;i<stripcount;i++){\n\t\t\tread =\n\t\t\t\tTIFFReadEncodedStrip(input,\n\t\t\t\ti,\n\t\t\t\t(tdata_t) &buffer[bufferoffset],\n\t\t\t\tTIFFmin(stripsize, t2p->tiff_datasize - bufferoffset));\n\t\t\tif(read==-1){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Error on decoding strip %u of %s",\n\t\t\t\t\ti,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\t_TIFFfree(buffer);\n\t\t\t\tt2p->t2p_error=T2P_ERR_ERROR;\n\t\t\t\treturn(0);\n\t\t\t}\n\t\t\tbufferoffset+=read;\n\t\t}\n\t} else {\n\t\tif(t2p->pdf_sample & T2P_SAMPLE_PLANAR_SEPARATE_TO_CONTIG){\n\t\t\tsepstripsize=TIFFStripSize(input);\n\t\t\tsepstripcount=TIFFNumberOfStrips(input);\n\t\t\tstripsize=sepstripsize*t2p->tiff_samplesperpixel;\n\t\t\tstripcount=sepstripcount/t2p->tiff_samplesperpixel;\n\t\t\tbuffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize);\n\t\t\tif(buffer==NULL){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t"Can\'t allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s",\n\t\t\t\t\t(unsigned long) t2p->tiff_datasize,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn(0);\n\t\t\t}\n memset(buffer, 0, t2p->tiff_datasize);\n\t\t\tsamplebuffer = (unsigned char*) _TIFFmalloc(stripsize);\n\t\t\tif(samplebuffer==NULL){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t"Can\'t allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s",\n\t\t\t\t\t(unsigned long) t2p->tiff_datasize,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n _TIFFfree(buffer);\n\t\t\t\treturn(0);\n\t\t\t}\n\t\t\tfor(i=0;i<stripcount;i++){\n\t\t\t\tsamplebufferoffset=0;\n\t\t\t\tfor(j=0;j<t2p->tiff_samplesperpixel;j++){\n\t\t\t\t\tread =\n\t\t\t\t\t\tTIFFReadEncodedStrip(input,\n\t\t\t\t\t\t\ti + j*stripcount,\n\t\t\t\t\t\t\t(tdata_t) &(samplebuffer[samplebufferoffset]),\n\t\t\t\t\t\t\tTIFFmin(sepstripsize, stripsize - samplebufferoffset));\n\t\t\t\t\tif(read==-1){\n\t\t\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Error on decoding strip %u of %s",\n\t\t\t\t\t\t\ti + j*stripcount,\n\t\t\t\t\t\t\tTIFFFileName(input));\n\t\t\t\t\t\t\t_TIFFfree(buffer);\n\t\t\t\t\t\tt2p->t2p_error=T2P_ERR_ERROR;\n\t\t\t\t\t\treturn(0);\n\t\t\t\t\t}\n\t\t\t\t\tsamplebufferoffset+=read;\n\t\t\t\t}\n\t\t\t\tt2p_sample_planar_separate_to_contig(\n\t\t\t\t\tt2p,\n\t\t\t\t\t&(buffer[bufferoffset]),\n\t\t\t\t\tsamplebuffer,\n\t\t\t\t\tsamplebufferoffset);\n\t\t\t\tbufferoffset+=samplebufferoffset;\n\t\t\t}\n\t\t\t_TIFFfree(samplebuffer);\n\t\t\tgoto dataready;\n\t\t}\n\t\tbuffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize);\n\t\tif(buffer==NULL){\n\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t"Can\'t allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s",\n\t\t\t\t(unsigned long) t2p->tiff_datasize,\n\t\t\t\tTIFFFileName(input));\n\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\treturn(0);\n\t\t}\n memset(buffer, 0, t2p->tiff_datasize);\n\t\tstripsize=TIFFStripSize(input);\n\t\tstripcount=TIFFNumberOfStrips(input);\n\t\tfor(i=0;i<stripcount;i++){\n\t\t\tread =\n\t\t\t\tTIFFReadEncodedStrip(input,\n\t\t\t\ti,\n\t\t\t\t(tdata_t) &buffer[bufferoffset],\n\t\t\t\tTIFFmin(stripsize, t2p->tiff_datasize - bufferoffset));\n\t\t\tif(read==-1){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Error on decoding strip %u of %s",\n\t\t\t\t\ti,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\t_TIFFfree(samplebuffer);\n\t\t\t\t_TIFFfree(buffer);\n\t\t\t\tt2p->t2p_error=T2P_ERR_ERROR;\n\t\t\t\treturn(0);\n\t\t\t}\n\t\t\tbufferoffset+=read;\n\t\t}\n\t\tif(t2p->pdf_sample & T2P_SAMPLE_REALIZE_PALETTE){\n\t\t\tsamplebuffer=(unsigned char*)_TIFFrealloc(\n\t\t\t\t(tdata_t) buffer,\n\t\t\t\tt2p->tiff_datasize * t2p->tiff_samplesperpixel);\n\t\t\tif(samplebuffer==NULL){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t"Can\'t allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s",\n\t\t\t\t\t(unsigned long) t2p->tiff_datasize,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\t_TIFFfree(buffer);\n\t\t\t\treturn(0);\n\t\t\t} else {\n\t\t\t\tbuffer=samplebuffer;\n\t\t\t\tt2p->tiff_datasize *= t2p->tiff_samplesperpixel;\n\t\t\t}\n\t\t\tt2p_sample_realize_palette(t2p, buffer);\n\t\t}\n\t\tif(t2p->pdf_sample & T2P_SAMPLE_RGBA_TO_RGB){\n\t\t\tt2p->tiff_datasize=t2p_sample_rgba_to_rgb(\n\t\t\t\t(tdata_t)buffer,\n\t\t\t\tt2p->tiff_width*t2p->tiff_length);\n\t\t}\n\t\tif(t2p->pdf_sample & T2P_SAMPLE_RGBAA_TO_RGB){\n\t\t\tt2p->tiff_datasize=t2p_sample_rgbaa_to_rgb(\n\t\t\t\t(tdata_t)buffer,\n\t\t\t\tt2p->tiff_width*t2p->tiff_length);\n\t\t}\n\t\tif(t2p->pdf_sample & T2P_SAMPLE_YCBCR_TO_RGB){\n\t\t\tsamplebuffer=(unsigned char*)_TIFFrealloc(\n\t\t\t\t(tdata_t)buffer,\n\t\t\t\tt2p->tiff_width*t2p->tiff_length*4);\n\t\t\tif(samplebuffer==NULL){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t"Can\'t allocate %lu bytes of memory for t2p_readwrite_pdf_image, %s",\n\t\t\t\t\t(unsigned long) t2p->tiff_datasize,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\t_TIFFfree(buffer);\n\t\t\t\treturn(0);\n\t\t\t} else {\n\t\t\t\tbuffer=samplebuffer;\n\t\t\t}\n\t\t\tif(!TIFFReadRGBAImageOriented(\n\t\t\t\tinput,\n\t\t\t\tt2p->tiff_width,\n\t\t\t\tt2p->tiff_length,\n\t\t\t\t(uint32*)buffer,\n\t\t\t\tORIENTATION_TOPLEFT,\n\t\t\t\t0)){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t"Can\'t use TIFFReadRGBAImageOriented to extract RGB image from %s",\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn(0);\n\t\t\t}\n\t\t\tt2p->tiff_datasize=t2p_sample_abgr_to_rgb(\n\t\t\t\t(tdata_t) buffer,\n\t\t\t\tt2p->tiff_width*t2p->tiff_length);\n\t\t}\n\t\tif(t2p->pdf_sample & T2P_SAMPLE_LAB_SIGNED_TO_UNSIGNED){\n\t\t\tt2p->tiff_datasize=t2p_sample_lab_signed_to_unsigned(\n\t\t\t\t(tdata_t)buffer,\n\t\t\t\tt2p->tiff_width*t2p->tiff_length);\n\t\t}\n\t}\ndataready:\n\tt2p_disable(output);\n\tTIFFSetField(output, TIFFTAG_PHOTOMETRIC, t2p->tiff_photometric);\n\tTIFFSetField(output, TIFFTAG_BITSPERSAMPLE, t2p->tiff_bitspersample);\n\tTIFFSetField(output, TIFFTAG_SAMPLESPERPIXEL, t2p->tiff_samplesperpixel);\n\tTIFFSetField(output, TIFFTAG_IMAGEWIDTH, t2p->tiff_width);\n\tTIFFSetField(output, TIFFTAG_IMAGELENGTH, t2p->tiff_length);\n\tTIFFSetField(output, TIFFTAG_ROWSPERSTRIP, t2p->tiff_length);\n\tTIFFSetField(output, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);\n\tTIFFSetField(output, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);\n\tswitch(t2p->pdf_compression){\n\tcase T2P_COMPRESS_NONE:\n\t\tTIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_NONE);\n\t\tbreak;\n#ifdef CCITT_SUPPORT\n\tcase T2P_COMPRESS_G4:\n\t\tTIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4);\n\t\tbreak;\n#endif\n#ifdef JPEG_SUPPORT\n\tcase T2P_COMPRESS_JPEG:\n\t\tif(t2p->tiff_photometric==PHOTOMETRIC_YCBCR) {\n\t\t\tuint16 hor = 0, ver = 0;\n\t\t\tif (TIFFGetField(input, TIFFTAG_YCBCRSUBSAMPLING, &hor, &ver) !=0 ) {\n\t\t\t\tif(hor != 0 && ver != 0){\n\t\t\t\t\tTIFFSetField(output, TIFFTAG_YCBCRSUBSAMPLING, hor, ver);\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(TIFFGetField(input, TIFFTAG_REFERENCEBLACKWHITE, &xfloatp)!=0){\n\t\t\t\tTIFFSetField(output, TIFFTAG_REFERENCEBLACKWHITE, xfloatp);\n\t\t\t}\n\t\t}\n\t\tif(TIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_JPEG)==0){\n\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t"Unable to use JPEG compression for input %s and output %s",\n\t\t\t\tTIFFFileName(input),\n\t\t\t\tTIFFFileName(output));\n\t\t\t_TIFFfree(buffer);\n\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\treturn(0);\n\t\t}\n\t\tTIFFSetField(output, TIFFTAG_JPEGTABLESMODE, 0);\n\t\tif(t2p->pdf_colorspace & (T2P_CS_RGB | T2P_CS_LAB)){\n\t\t\tTIFFSetField(output, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR);\n\t\t\tif(t2p->tiff_photometric != PHOTOMETRIC_YCBCR){\n\t\t\t\tTIFFSetField(output, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);\n\t\t\t} else {\n\t\t\t\tTIFFSetField(output, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RAW);\n\t\t\t}\n\t\t}\n\t\tif(t2p->pdf_colorspace & T2P_CS_GRAY){\n\t\t\t(void)0;\n\t\t}\n\t\tif(t2p->pdf_colorspace & T2P_CS_CMYK){\n\t\t\t(void)0;\n\t\t}\n\t\tif(t2p->pdf_defaultcompressionquality != 0){\n\t\t\tTIFFSetField(output,\n\t\t\t\tTIFFTAG_JPEGQUALITY,\n\t\t\t\tt2p->pdf_defaultcompressionquality);\n\t\t}\n\t\tbreak;\n#endif\n#ifdef ZIP_SUPPORT\n\tcase T2P_COMPRESS_ZIP:\n\t\tTIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE);\n\t\tif(t2p->pdf_defaultcompressionquality%100 != 0){\n\t\t\tTIFFSetField(output,\n\t\t\t\tTIFFTAG_PREDICTOR,\n\t\t\t\tt2p->pdf_defaultcompressionquality % 100);\n\t\t}\n\t\tif(t2p->pdf_defaultcompressionquality/100 != 0){\n\t\t\tTIFFSetField(output,\n\t\t\t\tTIFFTAG_ZIPQUALITY,\n\t\t\t\t(t2p->pdf_defaultcompressionquality / 100));\n\t\t}\n\t\tbreak;\n#endif\n\tdefault:\n\t\tbreak;\n\t}\n\tt2p_enable(output);\n\tt2p->outputwritten = 0;\n#ifdef JPEG_SUPPORT\n\tif(t2p->pdf_compression == T2P_COMPRESS_JPEG\n\t && t2p->tiff_photometric == PHOTOMETRIC_YCBCR){\n\t\tbufferoffset = TIFFWriteEncodedStrip(output, (tstrip_t)0,\n\t\t\t\t\t\t buffer,\n\t\t\t\t\t\t stripsize * stripcount);\n\t} else\n#endif\n {\n\t\tbufferoffset = TIFFWriteEncodedStrip(output, (tstrip_t)0,\n\t\t\t\t\t\t buffer,\n\t\t\t\t\t\t t2p->tiff_datasize);\n\t}\n\tif (buffer != NULL) {\n\t\t_TIFFfree(buffer);\n\t\tbuffer=NULL;\n\t}\n\tif (bufferoffset == (tsize_t)-1) {\n\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t "Error writing encoded strip to output PDF %s",\n\t\t\t TIFFFileName(output));\n\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\treturn(0);\n\t}\n\twritten = t2p->outputwritten;\n\treturn(written);\n}', 'uint32\nTIFFNumberOfStrips(TIFF* tif)\n{\n\tTIFFDirectory *td = &tif->tif_dir;\n\tuint32 nstrips;\n\tnstrips = (td->td_rowsperstrip == (uint32) -1 ? 1 :\n\t TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip));\n\tif (td->td_planarconfig == PLANARCONFIG_SEPARATE)\n\t\tnstrips = _TIFFMultiply32(tif, nstrips, (uint32)td->td_samplesperpixel,\n\t\t "TIFFNumberOfStrips");\n\treturn (nstrips);\n}'] |
35,449 | 0 | https://gitlab.com/libtiff/libtiff/blob/06337fdcfd19f2e5dbe99209540dbe34315f29eb/libtiff/tif_swab.c/#L111 | void
TIFFSwabArrayOfLong(register uint32* lp, tmsize_t n)
{
register unsigned char *cp;
register unsigned char t;
assert(sizeof(uint32)==4);
while (n-- > 0) {
cp = (unsigned char *)lp;
t = cp[3]; cp[3] = cp[0]; cp[0] = t;
t = cp[2]; cp[2] = cp[1]; cp[1] = t;
lp++;
}
} | ['TIFF*\nTIFFClientOpen(\n\tconst char* name, const char* mode,\n\tthandle_t clientdata,\n\tTIFFReadWriteProc readproc,\n\tTIFFReadWriteProc writeproc,\n\tTIFFSeekProc seekproc,\n\tTIFFCloseProc closeproc,\n\tTIFFSizeProc sizeproc,\n\tTIFFMapFileProc mapproc,\n\tTIFFUnmapFileProc unmapproc\n)\n{\n\tstatic const char module[] = "TIFFClientOpen";\n\tTIFF *tif;\n\tint m;\n\tconst char* cp;\n\tassert(sizeof(uint8)==1);\n\tassert(sizeof(int8)==1);\n\tassert(sizeof(uint16)==2);\n\tassert(sizeof(int16)==2);\n\tassert(sizeof(uint32)==4);\n\tassert(sizeof(int32)==4);\n\tassert(sizeof(uint64)==8);\n\tassert(sizeof(int64)==8);\n\tassert(sizeof(tmsize_t)==sizeof(void*));\n\t{\n\t\tunion{\n\t\t\tuint8 a8[2];\n\t\t\tuint16 a16;\n\t\t} n;\n\t\tn.a8[0]=1;\n\t\tn.a8[1]=0;\n\t\t#ifdef WORDS_BIGENDIAN\n\t\tassert(n.a16==256);\n\t\t#else\n\t\tassert(n.a16==1);\n\t\t#endif\n\t}\n\tm = _TIFFgetMode(mode, module);\n\tif (m == -1)\n\t\tgoto bad2;\n\ttif = (TIFF *)_TIFFmalloc((tmsize_t)(sizeof (TIFF) + strlen(name) + 1));\n\tif (tif == NULL) {\n\t\tTIFFErrorExt(clientdata, module, "%s: Out of memory (TIFF structure)", name);\n\t\tgoto bad2;\n\t}\n\t_TIFFmemset(tif, 0, sizeof (*tif));\n\ttif->tif_name = (char *)tif + sizeof (TIFF);\n\tstrcpy(tif->tif_name, name);\n\ttif->tif_mode = m &~ (O_CREAT|O_TRUNC);\n\ttif->tif_curdir = (uint16) -1;\n\ttif->tif_curoff = 0;\n\ttif->tif_curstrip = (uint32) -1;\n\ttif->tif_row = (uint32) -1;\n\ttif->tif_clientdata = clientdata;\n\tif (!readproc || !writeproc || !seekproc || !closeproc || !sizeproc) {\n\t\tTIFFErrorExt(clientdata, module,\n\t\t "One of the client procedures is NULL pointer.");\n\t\tgoto bad2;\n\t}\n\ttif->tif_readproc = readproc;\n\ttif->tif_writeproc = writeproc;\n\ttif->tif_seekproc = seekproc;\n\ttif->tif_closeproc = closeproc;\n\ttif->tif_sizeproc = sizeproc;\n\tif (mapproc)\n\t\ttif->tif_mapproc = mapproc;\n\telse\n\t\ttif->tif_mapproc = _tiffDummyMapProc;\n\tif (unmapproc)\n\t\ttif->tif_unmapproc = unmapproc;\n\telse\n\t\ttif->tif_unmapproc = _tiffDummyUnmapProc;\n\t_TIFFSetDefaultCompressionState(tif);\n\ttif->tif_flags = FILLORDER_MSB2LSB;\n\tif (m == O_RDONLY )\n\t\ttif->tif_flags |= TIFF_MAPPED;\n\t#ifdef STRIPCHOP_DEFAULT\n\tif (m == O_RDONLY || m == O_RDWR)\n\t\ttif->tif_flags |= STRIPCHOP_DEFAULT;\n\t#endif\n\tfor (cp = mode; *cp; cp++)\n\t\tswitch (*cp) {\n\t\t\tcase \'b\':\n\t\t\t\t#ifndef WORDS_BIGENDIAN\n\t\t\t\tif (m&O_CREAT)\n\t\t\t\t\ttif->tif_flags |= TIFF_SWAB;\n\t\t\t\t#endif\n\t\t\t\tbreak;\n\t\t\tcase \'l\':\n\t\t\t\t#ifdef WORDS_BIGENDIAN\n\t\t\t\tif ((m&O_CREAT))\n\t\t\t\t\ttif->tif_flags |= TIFF_SWAB;\n\t\t\t\t#endif\n\t\t\t\tbreak;\n\t\t\tcase \'B\':\n\t\t\t\ttif->tif_flags = (tif->tif_flags &~ TIFF_FILLORDER) |\n\t\t\t\t FILLORDER_MSB2LSB;\n\t\t\t\tbreak;\n\t\t\tcase \'L\':\n\t\t\t\ttif->tif_flags = (tif->tif_flags &~ TIFF_FILLORDER) |\n\t\t\t\t FILLORDER_LSB2MSB;\n\t\t\t\tbreak;\n\t\t\tcase \'H\':\n\t\t\t\ttif->tif_flags = (tif->tif_flags &~ TIFF_FILLORDER) |\n\t\t\t\t HOST_FILLORDER;\n\t\t\t\tbreak;\n\t\t\tcase \'M\':\n\t\t\t\tif (m == O_RDONLY)\n\t\t\t\t\ttif->tif_flags |= TIFF_MAPPED;\n\t\t\t\tbreak;\n\t\t\tcase \'m\':\n\t\t\t\tif (m == O_RDONLY)\n\t\t\t\t\ttif->tif_flags &= ~TIFF_MAPPED;\n\t\t\t\tbreak;\n\t\t\tcase \'C\':\n\t\t\t\tif (m == O_RDONLY)\n\t\t\t\t\ttif->tif_flags |= TIFF_STRIPCHOP;\n\t\t\t\tbreak;\n\t\t\tcase \'c\':\n\t\t\t\tif (m == O_RDONLY)\n\t\t\t\t\ttif->tif_flags &= ~TIFF_STRIPCHOP;\n\t\t\t\tbreak;\n\t\t\tcase \'h\':\n\t\t\t\ttif->tif_flags |= TIFF_HEADERONLY;\n\t\t\t\tbreak;\n\t\t\tcase \'8\':\n\t\t\t\tif (m&O_CREAT)\n\t\t\t\t\ttif->tif_flags |= TIFF_BIGTIFF;\n\t\t\t\tbreak;\n\t\t\tcase \'D\':\n\t\t\t tif->tif_flags |= TIFF_DEFERSTRILELOAD;\n\t\t\t\tbreak;\n\t\t\tcase \'O\':\n\t\t\t\tif( m == O_RDONLY )\n\t\t\t\t\ttif->tif_flags |= (TIFF_LAZYSTRILELOAD | TIFF_DEFERSTRILELOAD);\n\t\t\t\tbreak;\n\t\t}\n#ifdef DEFER_STRILE_LOAD\n tif->tif_flags |= TIFF_DEFERSTRILELOAD;\n#endif\n\tif ((m & O_TRUNC) ||\n\t !ReadOK(tif, &tif->tif_header, sizeof (TIFFHeaderClassic))) {\n\t\tif (tif->tif_mode == O_RDONLY) {\n\t\t\tTIFFErrorExt(tif->tif_clientdata, name,\n\t\t\t "Cannot read TIFF header");\n\t\t\tgoto bad;\n\t\t}\n\t\t#ifdef WORDS_BIGENDIAN\n\t\ttif->tif_header.common.tiff_magic = (tif->tif_flags & TIFF_SWAB)\n\t\t ? TIFF_LITTLEENDIAN : TIFF_BIGENDIAN;\n\t\t#else\n\t\ttif->tif_header.common.tiff_magic = (tif->tif_flags & TIFF_SWAB)\n\t\t ? TIFF_BIGENDIAN : TIFF_LITTLEENDIAN;\n\t\t#endif\n\t\tif (!(tif->tif_flags&TIFF_BIGTIFF))\n\t\t{\n\t\t\ttif->tif_header.common.tiff_version = TIFF_VERSION_CLASSIC;\n\t\t\ttif->tif_header.classic.tiff_diroff = 0;\n\t\t\tif (tif->tif_flags & TIFF_SWAB)\n\t\t\t\tTIFFSwabShort(&tif->tif_header.common.tiff_version);\n\t\t\ttif->tif_header_size = sizeof(TIFFHeaderClassic);\n\t\t}\n\t\telse\n\t\t{\n\t\t\ttif->tif_header.common.tiff_version = TIFF_VERSION_BIG;\n\t\t\ttif->tif_header.big.tiff_offsetsize = 8;\n\t\t\ttif->tif_header.big.tiff_unused = 0;\n\t\t\ttif->tif_header.big.tiff_diroff = 0;\n\t\t\tif (tif->tif_flags & TIFF_SWAB)\n\t\t\t{\n\t\t\t\tTIFFSwabShort(&tif->tif_header.common.tiff_version);\n\t\t\t\tTIFFSwabShort(&tif->tif_header.big.tiff_offsetsize);\n\t\t\t}\n\t\t\ttif->tif_header_size = sizeof (TIFFHeaderBig);\n\t\t}\n\t\tTIFFSeekFile( tif, 0, SEEK_SET );\n\t\tif (!WriteOK(tif, &tif->tif_header, (tmsize_t)(tif->tif_header_size))) {\n\t\t\tTIFFErrorExt(tif->tif_clientdata, name,\n\t\t\t "Error writing TIFF header");\n\t\t\tgoto bad;\n\t\t}\n\t\tif (tif->tif_header.common.tiff_magic == TIFF_BIGENDIAN) {\n\t\t\t#ifndef WORDS_BIGENDIAN\n\t\t\ttif->tif_flags |= TIFF_SWAB;\n\t\t\t#endif\n\t\t} else {\n\t\t\t#ifdef WORDS_BIGENDIAN\n\t\t\ttif->tif_flags |= TIFF_SWAB;\n\t\t\t#endif\n\t\t}\n\t\tif (!TIFFDefaultDirectory(tif))\n\t\t\tgoto bad;\n\t\ttif->tif_diroff = 0;\n\t\ttif->tif_dirlist = NULL;\n\t\ttif->tif_dirlistsize = 0;\n\t\ttif->tif_dirnumber = 0;\n\t\treturn (tif);\n\t}\n\tif (tif->tif_header.common.tiff_magic != TIFF_BIGENDIAN &&\n\t tif->tif_header.common.tiff_magic != TIFF_LITTLEENDIAN\n\t #if MDI_SUPPORT\n\t &&\n\t #if HOST_BIGENDIAN\n\t tif->tif_header.common.tiff_magic != MDI_BIGENDIAN\n\t #else\n\t tif->tif_header.common.tiff_magic != MDI_LITTLEENDIAN\n\t #endif\n\t ) {\n\t\tTIFFErrorExt(tif->tif_clientdata, name,\n\t\t "Not a TIFF or MDI file, bad magic number %d (0x%x)",\n\t #else\n\t ) {\n\t\tTIFFErrorExt(tif->tif_clientdata, name,\n\t\t "Not a TIFF file, bad magic number %d (0x%x)",\n\t #endif\n\t\t tif->tif_header.common.tiff_magic,\n\t\t tif->tif_header.common.tiff_magic);\n\t\tgoto bad;\n\t}\n\tif (tif->tif_header.common.tiff_magic == TIFF_BIGENDIAN) {\n\t\t#ifndef WORDS_BIGENDIAN\n\t\ttif->tif_flags |= TIFF_SWAB;\n\t\t#endif\n\t} else {\n\t\t#ifdef WORDS_BIGENDIAN\n\t\ttif->tif_flags |= TIFF_SWAB;\n\t\t#endif\n\t}\n\tif (tif->tif_flags & TIFF_SWAB)\n\t\tTIFFSwabShort(&tif->tif_header.common.tiff_version);\n\tif ((tif->tif_header.common.tiff_version != TIFF_VERSION_CLASSIC)&&\n\t (tif->tif_header.common.tiff_version != TIFF_VERSION_BIG)) {\n\t\tTIFFErrorExt(tif->tif_clientdata, name,\n\t\t "Not a TIFF file, bad version number %d (0x%x)",\n\t\t tif->tif_header.common.tiff_version,\n\t\t tif->tif_header.common.tiff_version);\n\t\tgoto bad;\n\t}\n\tif (tif->tif_header.common.tiff_version == TIFF_VERSION_CLASSIC)\n\t{\n\t\tif (tif->tif_flags & TIFF_SWAB)\n\t\t\tTIFFSwabLong(&tif->tif_header.classic.tiff_diroff);\n\t\ttif->tif_header_size = sizeof(TIFFHeaderClassic);\n\t}\n\telse\n\t{\n\t\tif (!ReadOK(tif, ((uint8*)(&tif->tif_header) + sizeof(TIFFHeaderClassic)), (sizeof(TIFFHeaderBig)-sizeof(TIFFHeaderClassic))))\n\t\t{\n\t\t\tTIFFErrorExt(tif->tif_clientdata, name,\n\t\t\t "Cannot read TIFF header");\n\t\t\tgoto bad;\n\t\t}\n\t\tif (tif->tif_flags & TIFF_SWAB)\n\t\t{\n\t\t\tTIFFSwabShort(&tif->tif_header.big.tiff_offsetsize);\n\t\t\tTIFFSwabLong8(&tif->tif_header.big.tiff_diroff);\n\t\t}\n\t\tif (tif->tif_header.big.tiff_offsetsize != 8)\n\t\t{\n\t\t\tTIFFErrorExt(tif->tif_clientdata, name,\n\t\t\t "Not a TIFF file, bad BigTIFF offsetsize %d (0x%x)",\n\t\t\t tif->tif_header.big.tiff_offsetsize,\n\t\t\t tif->tif_header.big.tiff_offsetsize);\n\t\t\tgoto bad;\n\t\t}\n\t\tif (tif->tif_header.big.tiff_unused != 0)\n\t\t{\n\t\t\tTIFFErrorExt(tif->tif_clientdata, name,\n\t\t\t "Not a TIFF file, bad BigTIFF unused %d (0x%x)",\n\t\t\t tif->tif_header.big.tiff_unused,\n\t\t\t tif->tif_header.big.tiff_unused);\n\t\t\tgoto bad;\n\t\t}\n\t\ttif->tif_header_size = sizeof(TIFFHeaderBig);\n\t\ttif->tif_flags |= TIFF_BIGTIFF;\n\t}\n\ttif->tif_flags |= TIFF_MYBUFFER;\n\ttif->tif_rawcp = tif->tif_rawdata = 0;\n\ttif->tif_rawdatasize = 0;\n tif->tif_rawdataoff = 0;\n tif->tif_rawdataloaded = 0;\n\tswitch (mode[0]) {\n\t\tcase \'r\':\n\t\t\tif (!(tif->tif_flags&TIFF_BIGTIFF))\n\t\t\t\ttif->tif_nextdiroff = tif->tif_header.classic.tiff_diroff;\n\t\t\telse\n\t\t\t\ttif->tif_nextdiroff = tif->tif_header.big.tiff_diroff;\n\t\t\tif (tif->tif_flags & TIFF_MAPPED)\n\t\t\t{\n\t\t\t\ttoff_t n;\n\t\t\t\tif (TIFFMapFileContents(tif,(void**)(&tif->tif_base),&n))\n\t\t\t\t{\n\t\t\t\t\ttif->tif_size=(tmsize_t)n;\n\t\t\t\t\tassert((toff_t)tif->tif_size==n);\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\ttif->tif_flags &= ~TIFF_MAPPED;\n\t\t\t}\n\t\t\tif (tif->tif_flags & TIFF_HEADERONLY)\n\t\t\t\treturn (tif);\n\t\t\tif (TIFFReadDirectory(tif)) {\n\t\t\t\ttif->tif_rawcc = (tmsize_t)-1;\n\t\t\t\ttif->tif_flags |= TIFF_BUFFERSETUP;\n\t\t\t\treturn (tif);\n\t\t\t}\n\t\t\tbreak;\n\t\tcase \'a\':\n\t\t\tif (!TIFFDefaultDirectory(tif))\n\t\t\t\tgoto bad;\n\t\t\treturn (tif);\n\t}\nbad:\n\ttif->tif_mode = O_RDONLY;\n TIFFCleanup(tif);\nbad2:\n\treturn ((TIFF*)0);\n}', 'void\nTIFFCleanup(TIFF* tif)\n{\n\tif (tif->tif_mode != O_RDONLY)\n\t\tTIFFFlush(tif);\n\t(*tif->tif_cleanup)(tif);\n\tTIFFFreeDirectory(tif);\n\tif (tif->tif_dirlist)\n\t\t_TIFFfree(tif->tif_dirlist);\n\twhile( tif->tif_clientinfo )\n\t{\n\t\tTIFFClientInfoLink *psLink = tif->tif_clientinfo;\n\t\ttif->tif_clientinfo = psLink->next;\n\t\t_TIFFfree( psLink->name );\n\t\t_TIFFfree( psLink );\n\t}\n\tif (tif->tif_rawdata && (tif->tif_flags&TIFF_MYBUFFER))\n\t\t_TIFFfree(tif->tif_rawdata);\n\tif (isMapped(tif))\n\t\tTIFFUnmapFileContents(tif, tif->tif_base, (toff_t)tif->tif_size);\n\tif (tif->tif_fields && tif->tif_nfields > 0) {\n\t\tuint32 i;\n\t\tfor (i = 0; i < tif->tif_nfields; i++) {\n\t\t\tTIFFField *fld = tif->tif_fields[i];\n\t\t\tif (fld->field_bit == FIELD_CUSTOM &&\n\t\t\t strncmp("Tag ", fld->field_name, 4) == 0) {\n\t\t\t\t_TIFFfree(fld->field_name);\n\t\t\t\t_TIFFfree(fld);\n\t\t\t}\n\t\t}\n\t\t_TIFFfree(tif->tif_fields);\n\t}\n if (tif->tif_nfieldscompat > 0) {\n uint32 i;\n for (i = 0; i < tif->tif_nfieldscompat; i++) {\n if (tif->tif_fieldscompat[i].allocated_size)\n _TIFFfree(tif->tif_fieldscompat[i].fields);\n }\n _TIFFfree(tif->tif_fieldscompat);\n }\n\t_TIFFfree(tif);\n}', 'int\nTIFFFlush(TIFF* tif)\n{\n if( tif->tif_mode == O_RDONLY )\n return 1;\n if (!TIFFFlushData(tif))\n return (0);\n if( (tif->tif_flags & TIFF_DIRTYSTRIP)\n && !(tif->tif_flags & TIFF_DIRTYDIRECT)\n && tif->tif_mode == O_RDWR )\n {\n uint64 *offsets=NULL, *sizes=NULL;\n if( TIFFIsTiled(tif) )\n {\n if( TIFFGetField( tif, TIFFTAG_TILEOFFSETS, &offsets )\n && TIFFGetField( tif, TIFFTAG_TILEBYTECOUNTS, &sizes )\n && _TIFFRewriteField( tif, TIFFTAG_TILEOFFSETS, TIFF_LONG8,\n tif->tif_dir.td_nstrips, offsets )\n && _TIFFRewriteField( tif, TIFFTAG_TILEBYTECOUNTS, TIFF_LONG8,\n tif->tif_dir.td_nstrips, sizes ) )\n {\n tif->tif_flags &= ~TIFF_DIRTYSTRIP;\n tif->tif_flags &= ~TIFF_BEENWRITING;\n return 1;\n }\n }\n else\n {\n if( TIFFGetField( tif, TIFFTAG_STRIPOFFSETS, &offsets )\n && TIFFGetField( tif, TIFFTAG_STRIPBYTECOUNTS, &sizes )\n && _TIFFRewriteField( tif, TIFFTAG_STRIPOFFSETS, TIFF_LONG8,\n tif->tif_dir.td_nstrips, offsets )\n && _TIFFRewriteField( tif, TIFFTAG_STRIPBYTECOUNTS, TIFF_LONG8,\n tif->tif_dir.td_nstrips, sizes ) )\n {\n tif->tif_flags &= ~TIFF_DIRTYSTRIP;\n tif->tif_flags &= ~TIFF_BEENWRITING;\n return 1;\n }\n }\n }\n if ((tif->tif_flags & (TIFF_DIRTYDIRECT|TIFF_DIRTYSTRIP))\n && !TIFFRewriteDirectory(tif))\n return (0);\n return (1);\n}', 'int\nTIFFFlushData(TIFF* tif)\n{\n\tif ((tif->tif_flags & TIFF_BEENWRITING) == 0)\n\t\treturn (1);\n\tif (tif->tif_flags & TIFF_POSTENCODE) {\n\t\ttif->tif_flags &= ~TIFF_POSTENCODE;\n\t\tif (!(*tif->tif_postencode)(tif))\n\t\t\treturn (0);\n\t}\n\treturn (TIFFFlushData1(tif));\n}', 'int\nTIFFRewriteDirectory( TIFF *tif )\n{\n\tstatic const char module[] = "TIFFRewriteDirectory";\n\tif( tif->tif_diroff == 0 )\n\t\treturn TIFFWriteDirectory( tif );\n\tif (!(tif->tif_flags&TIFF_BIGTIFF))\n\t{\n\t\tif (tif->tif_header.classic.tiff_diroff == tif->tif_diroff)\n\t\t{\n\t\t\ttif->tif_header.classic.tiff_diroff = 0;\n\t\t\ttif->tif_diroff = 0;\n\t\t\tTIFFSeekFile(tif,4,SEEK_SET);\n\t\t\tif (!WriteOK(tif, &(tif->tif_header.classic.tiff_diroff),4))\n\t\t\t{\n\t\t\t\tTIFFErrorExt(tif->tif_clientdata, tif->tif_name,\n\t\t\t\t "Error updating TIFF header");\n\t\t\t\treturn (0);\n\t\t\t}\n\t\t}\n\t\telse\n\t\t{\n\t\t\tuint32 nextdir;\n\t\t\tnextdir = tif->tif_header.classic.tiff_diroff;\n\t\t\twhile(1) {\n\t\t\t\tuint16 dircount;\n\t\t\t\tuint32 nextnextdir;\n\t\t\t\tif (!SeekOK(tif, nextdir) ||\n\t\t\t\t !ReadOK(tif, &dircount, 2)) {\n\t\t\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t\t\t "Error fetching directory count");\n\t\t\t\t\treturn (0);\n\t\t\t\t}\n\t\t\t\tif (tif->tif_flags & TIFF_SWAB)\n\t\t\t\t\tTIFFSwabShort(&dircount);\n\t\t\t\t(void) TIFFSeekFile(tif,\n\t\t\t\t nextdir+2+dircount*12, SEEK_SET);\n\t\t\t\tif (!ReadOK(tif, &nextnextdir, 4)) {\n\t\t\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t\t\t "Error fetching directory link");\n\t\t\t\t\treturn (0);\n\t\t\t\t}\n\t\t\t\tif (tif->tif_flags & TIFF_SWAB)\n\t\t\t\t\tTIFFSwabLong(&nextnextdir);\n\t\t\t\tif (nextnextdir==tif->tif_diroff)\n\t\t\t\t{\n\t\t\t\t\tuint32 m;\n\t\t\t\t\tm=0;\n\t\t\t\t\t(void) TIFFSeekFile(tif,\n\t\t\t\t\t nextdir+2+dircount*12, SEEK_SET);\n\t\t\t\t\tif (!WriteOK(tif, &m, 4)) {\n\t\t\t\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t\t\t\t "Error writing directory link");\n\t\t\t\t\t\treturn (0);\n\t\t\t\t\t}\n\t\t\t\t\ttif->tif_diroff=0;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tnextdir=nextnextdir;\n\t\t\t}\n\t\t}\n\t}\n\telse\n\t{\n\t\tif (tif->tif_header.big.tiff_diroff == tif->tif_diroff)\n\t\t{\n\t\t\ttif->tif_header.big.tiff_diroff = 0;\n\t\t\ttif->tif_diroff = 0;\n\t\t\tTIFFSeekFile(tif,8,SEEK_SET);\n\t\t\tif (!WriteOK(tif, &(tif->tif_header.big.tiff_diroff),8))\n\t\t\t{\n\t\t\t\tTIFFErrorExt(tif->tif_clientdata, tif->tif_name,\n\t\t\t\t "Error updating TIFF header");\n\t\t\t\treturn (0);\n\t\t\t}\n\t\t}\n\t\telse\n\t\t{\n\t\t\tuint64 nextdir;\n\t\t\tnextdir = tif->tif_header.big.tiff_diroff;\n\t\t\twhile(1) {\n\t\t\t\tuint64 dircount64;\n\t\t\t\tuint16 dircount;\n\t\t\t\tuint64 nextnextdir;\n\t\t\t\tif (!SeekOK(tif, nextdir) ||\n\t\t\t\t !ReadOK(tif, &dircount64, 8)) {\n\t\t\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t\t\t "Error fetching directory count");\n\t\t\t\t\treturn (0);\n\t\t\t\t}\n\t\t\t\tif (tif->tif_flags & TIFF_SWAB)\n\t\t\t\t\tTIFFSwabLong8(&dircount64);\n\t\t\t\tif (dircount64>0xFFFF)\n\t\t\t\t{\n\t\t\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t\t\t "Sanity check on tag count failed, likely corrupt TIFF");\n\t\t\t\t\treturn (0);\n\t\t\t\t}\n\t\t\t\tdircount=(uint16)dircount64;\n\t\t\t\t(void) TIFFSeekFile(tif,\n\t\t\t\t nextdir+8+dircount*20, SEEK_SET);\n\t\t\t\tif (!ReadOK(tif, &nextnextdir, 8)) {\n\t\t\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t\t\t "Error fetching directory link");\n\t\t\t\t\treturn (0);\n\t\t\t\t}\n\t\t\t\tif (tif->tif_flags & TIFF_SWAB)\n\t\t\t\t\tTIFFSwabLong8(&nextnextdir);\n\t\t\t\tif (nextnextdir==tif->tif_diroff)\n\t\t\t\t{\n\t\t\t\t\tuint64 m;\n\t\t\t\t\tm=0;\n\t\t\t\t\t(void) TIFFSeekFile(tif,\n\t\t\t\t\t nextdir+8+dircount*20, SEEK_SET);\n\t\t\t\t\tif (!WriteOK(tif, &m, 8)) {\n\t\t\t\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t\t\t\t "Error writing directory link");\n\t\t\t\t\t\treturn (0);\n\t\t\t\t\t}\n\t\t\t\t\ttif->tif_diroff=0;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tnextdir=nextnextdir;\n\t\t\t}\n\t\t}\n\t}\n\treturn TIFFWriteDirectory( tif );\n}', 'int\nTIFFWriteDirectory(TIFF* tif)\n{\n\treturn TIFFWriteDirectorySec(tif,TRUE,TRUE,NULL);\n}', 'static int\nTIFFWriteDirectorySec(TIFF* tif, int isimage, int imagedone, uint64* pdiroff)\n{\n\tstatic const char module[] = "TIFFWriteDirectorySec";\n\tuint32 ndir;\n\tTIFFDirEntry* dir;\n\tuint32 dirsize;\n\tvoid* dirmem;\n\tuint32 m;\n\tif (tif->tif_mode == O_RDONLY)\n\t\treturn (1);\n _TIFFFillStriles( tif );\n\tif (imagedone)\n\t{\n\t\tif (tif->tif_flags & TIFF_POSTENCODE)\n\t\t{\n\t\t\ttif->tif_flags &= ~TIFF_POSTENCODE;\n\t\t\tif (!(*tif->tif_postencode)(tif))\n\t\t\t{\n\t\t\t\tTIFFErrorExt(tif->tif_clientdata,module,\n\t\t\t\t "Error post-encoding before directory write");\n\t\t\t\treturn (0);\n\t\t\t}\n\t\t}\n\t\t(*tif->tif_close)(tif);\n\t\tif (tif->tif_rawcc > 0\n\t\t && (tif->tif_flags & TIFF_BEENWRITING) != 0 )\n\t\t{\n\t\t if( !TIFFFlushData1(tif) )\n {\n\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t "Error flushing data before directory write");\n\t\t\treturn (0);\n }\n\t\t}\n\t\tif ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)\n\t\t{\n\t\t\t_TIFFfree(tif->tif_rawdata);\n\t\t\ttif->tif_rawdata = NULL;\n\t\t\ttif->tif_rawcc = 0;\n\t\t\ttif->tif_rawdatasize = 0;\n tif->tif_rawdataoff = 0;\n tif->tif_rawdataloaded = 0;\n\t\t}\n\t\ttif->tif_flags &= ~(TIFF_BEENWRITING|TIFF_BUFFERSETUP);\n\t}\n\tdir=NULL;\n\tdirmem=NULL;\n\tdirsize=0;\n\twhile (1)\n\t{\n\t\tndir=0;\n\t\tif (isimage)\n\t\t{\n\t\t\tif (TIFFFieldSet(tif,FIELD_IMAGEDIMENSIONS))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_IMAGEWIDTH,tif->tif_dir.td_imagewidth))\n\t\t\t\t\tgoto bad;\n\t\t\t\tif (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_IMAGELENGTH,tif->tif_dir.td_imagelength))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_TILEDIMENSIONS))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_TILEWIDTH,tif->tif_dir.td_tilewidth))\n\t\t\t\t\tgoto bad;\n\t\t\t\tif (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_TILELENGTH,tif->tif_dir.td_tilelength))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_RESOLUTION))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_XRESOLUTION,tif->tif_dir.td_xresolution))\n\t\t\t\t\tgoto bad;\n\t\t\t\tif (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_YRESOLUTION,tif->tif_dir.td_yresolution))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_POSITION))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_XPOSITION,tif->tif_dir.td_xposition))\n\t\t\t\t\tgoto bad;\n\t\t\t\tif (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_YPOSITION,tif->tif_dir.td_yposition))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_SUBFILETYPE))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,TIFFTAG_SUBFILETYPE,tif->tif_dir.td_subfiletype))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_BITSPERSAMPLE,tif->tif_dir.td_bitspersample))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_COMPRESSION))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_COMPRESSION,tif->tif_dir.td_compression))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_PHOTOMETRIC))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_PHOTOMETRIC,tif->tif_dir.td_photometric))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_THRESHHOLDING))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_THRESHHOLDING,tif->tif_dir.td_threshholding))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_FILLORDER))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_FILLORDER,tif->tif_dir.td_fillorder))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_ORIENTATION))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_ORIENTATION,tif->tif_dir.td_orientation))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_SAMPLESPERPIXEL))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_SAMPLESPERPIXEL,tif->tif_dir.td_samplesperpixel))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_ROWSPERSTRIP))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_ROWSPERSTRIP,tif->tif_dir.td_rowsperstrip))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_MINSAMPLEVALUE))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_MINSAMPLEVALUE,tif->tif_dir.td_minsamplevalue))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_MAXSAMPLEVALUE))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_MAXSAMPLEVALUE,tif->tif_dir.td_maxsamplevalue))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_PLANARCONFIG))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_PLANARCONFIG,tif->tif_dir.td_planarconfig))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_RESOLUTIONUNIT))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_RESOLUTIONUNIT,tif->tif_dir.td_resolutionunit))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_PAGENUMBER))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_PAGENUMBER,2,&tif->tif_dir.td_pagenumber[0]))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_STRIPBYTECOUNTS))\n\t\t\t{\n\t\t\t\tif (!isTiled(tif))\n\t\t\t\t{\n\t\t\t\t\tif (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_STRIPBYTECOUNTS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripbytecount_p))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tif (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_TILEBYTECOUNTS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripbytecount_p))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_STRIPOFFSETS))\n\t\t\t{\n\t\t\t\tif (!isTiled(tif))\n\t\t\t\t{\n if (tif->tif_dir.td_stripoffset_p != NULL &&\n !TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_STRIPOFFSETS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripoffset_p))\n goto bad;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tif (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_TILEOFFSETS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripoffset_p))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_COLORMAP))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagColormap(tif,&ndir,dir))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_EXTRASAMPLES))\n\t\t\t{\n\t\t\t\tif (tif->tif_dir.td_extrasamples)\n\t\t\t\t{\n\t\t\t\t\tuint16 na;\n\t\t\t\t\tuint16* nb;\n\t\t\t\t\tTIFFGetFieldDefaulted(tif,TIFFTAG_EXTRASAMPLES,&na,&nb);\n\t\t\t\t\tif (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_EXTRASAMPLES,na,nb))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_SAMPLEFORMAT))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_SAMPLEFORMAT,tif->tif_dir.td_sampleformat))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_SMINSAMPLEVALUE))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagSampleformatArray(tif,&ndir,dir,TIFFTAG_SMINSAMPLEVALUE,tif->tif_dir.td_samplesperpixel,tif->tif_dir.td_sminsamplevalue))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_SMAXSAMPLEVALUE))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagSampleformatArray(tif,&ndir,dir,TIFFTAG_SMAXSAMPLEVALUE,tif->tif_dir.td_samplesperpixel,tif->tif_dir.td_smaxsamplevalue))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_IMAGEDEPTH))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,TIFFTAG_IMAGEDEPTH,tif->tif_dir.td_imagedepth))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_TILEDEPTH))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,TIFFTAG_TILEDEPTH,tif->tif_dir.td_tiledepth))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_HALFTONEHINTS))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_HALFTONEHINTS,2,&tif->tif_dir.td_halftonehints[0]))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_YCBCRSUBSAMPLING))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_YCBCRSUBSAMPLING,2,&tif->tif_dir.td_ycbcrsubsampling[0]))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_YCBCRPOSITIONING))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_YCBCRPOSITIONING,tif->tif_dir.td_ycbcrpositioning))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_REFBLACKWHITE))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagRationalArray(tif,&ndir,dir,TIFFTAG_REFERENCEBLACKWHITE,6,tif->tif_dir.td_refblackwhite))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_TRANSFERFUNCTION))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagTransferfunction(tif,&ndir,dir))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_INKNAMES))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagAscii(tif,&ndir,dir,TIFFTAG_INKNAMES,tif->tif_dir.td_inknameslen,tif->tif_dir.td_inknames))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_SUBIFD))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagSubifd(tif,&ndir,dir))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\t{\n\t\t\t\tuint32 n;\n\t\t\t\tfor (n=0; n<tif->tif_nfields; n++) {\n\t\t\t\t\tconst TIFFField* o;\n\t\t\t\t\to = tif->tif_fields[n];\n\t\t\t\t\tif ((o->field_bit>=FIELD_CODEC)&&(TIFFFieldSet(tif,o->field_bit)))\n\t\t\t\t\t{\n\t\t\t\t\t\tswitch (o->get_field_type)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tcase TIFF_SETGET_ASCII:\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tuint32 pa;\n\t\t\t\t\t\t\t\t\tchar* pb;\n\t\t\t\t\t\t\t\t\tassert(o->field_type==TIFF_ASCII);\n\t\t\t\t\t\t\t\t\tassert(o->field_readcount==TIFF_VARIABLE);\n\t\t\t\t\t\t\t\t\tassert(o->field_passcount==0);\n\t\t\t\t\t\t\t\t\tTIFFGetField(tif,o->field_tag,&pb);\n\t\t\t\t\t\t\t\t\tpa=(uint32)(strlen(pb));\n\t\t\t\t\t\t\t\t\tif (!TIFFWriteDirectoryTagAscii(tif,&ndir,dir,(uint16)o->field_tag,pa,pb))\n\t\t\t\t\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\tcase TIFF_SETGET_UINT16:\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tuint16 p;\n\t\t\t\t\t\t\t\t\tassert(o->field_type==TIFF_SHORT);\n\t\t\t\t\t\t\t\t\tassert(o->field_readcount==1);\n\t\t\t\t\t\t\t\t\tassert(o->field_passcount==0);\n\t\t\t\t\t\t\t\t\tTIFFGetField(tif,o->field_tag,&p);\n\t\t\t\t\t\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,(uint16)o->field_tag,p))\n\t\t\t\t\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\tcase TIFF_SETGET_UINT32:\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tuint32 p;\n\t\t\t\t\t\t\t\t\tassert(o->field_type==TIFF_LONG);\n\t\t\t\t\t\t\t\t\tassert(o->field_readcount==1);\n\t\t\t\t\t\t\t\t\tassert(o->field_passcount==0);\n\t\t\t\t\t\t\t\t\tTIFFGetField(tif,o->field_tag,&p);\n\t\t\t\t\t\t\t\t\tif (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,(uint16)o->field_tag,p))\n\t\t\t\t\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\tcase TIFF_SETGET_C32_UINT8:\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tuint32 pa;\n\t\t\t\t\t\t\t\t\tvoid* pb;\n\t\t\t\t\t\t\t\t\tassert(o->field_type==TIFF_UNDEFINED);\n\t\t\t\t\t\t\t\t\tassert(o->field_readcount==TIFF_VARIABLE2);\n\t\t\t\t\t\t\t\t\tassert(o->field_passcount==1);\n\t\t\t\t\t\t\t\t\tTIFFGetField(tif,o->field_tag,&pa,&pb);\n\t\t\t\t\t\t\t\t\tif (!TIFFWriteDirectoryTagUndefinedArray(tif,&ndir,dir,(uint16)o->field_tag,pa,pb))\n\t\t\t\t\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\tdefault:\n\t\t\t\t\t\t\t\tTIFFErrorExt(tif->tif_clientdata,module,\n\t\t\t\t\t\t\t\t "Cannot write tag %d (%s)",\n\t\t\t\t\t\t\t\t TIFFFieldTag(o),\n o->field_name ? o->field_name : "unknown");\n\t\t\t\t\t\t\t\tgoto bad;\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\t\tfor (m=0; m<(uint32)(tif->tif_dir.td_customValueCount); m++)\n\t\t{\n uint16 tag = (uint16)tif->tif_dir.td_customValues[m].info->field_tag;\n uint32 count = tif->tif_dir.td_customValues[m].count;\n\t\t\tswitch (tif->tif_dir.td_customValues[m].info->field_type)\n\t\t\t{\n\t\t\t\tcase TIFF_ASCII:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagAscii(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_UNDEFINED:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagUndefinedArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_BYTE:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagByteArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_SBYTE:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagSbyteArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_SHORT:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_SSHORT:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagSshortArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_LONG:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagLongArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_SLONG:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagSlongArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_LONG8:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagLong8Array(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_SLONG8:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagSlong8Array(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_RATIONAL:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagRationalArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_SRATIONAL:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagSrationalArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_FLOAT:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagFloatArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_DOUBLE:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagDoubleArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_IFD:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagIfdArray(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_IFD8:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagIfdIfd8Array(tif,&ndir,dir,tag,count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tdefault:\n\t\t\t\t\tassert(0);\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif (dir!=NULL)\n\t\t\tbreak;\n\t\tdir=_TIFFmalloc(ndir*sizeof(TIFFDirEntry));\n\t\tif (dir==NULL)\n\t\t{\n\t\t\tTIFFErrorExt(tif->tif_clientdata,module,"Out of memory");\n\t\t\tgoto bad;\n\t\t}\n\t\tif (isimage)\n\t\t{\n\t\t\tif ((tif->tif_diroff==0)&&(!TIFFLinkDirectory(tif)))\n\t\t\t\tgoto bad;\n\t\t}\n\t\telse\n\t\t\ttif->tif_diroff=(TIFFSeekFile(tif,0,SEEK_END)+1)&(~((toff_t)1));\n\t\tif (pdiroff!=NULL)\n\t\t\t*pdiroff=tif->tif_diroff;\n\t\tif (!(tif->tif_flags&TIFF_BIGTIFF))\n\t\t\tdirsize=2+ndir*12+4;\n\t\telse\n\t\t\tdirsize=8+ndir*20+8;\n\t\ttif->tif_dataoff=tif->tif_diroff+dirsize;\n\t\tif (!(tif->tif_flags&TIFF_BIGTIFF))\n\t\t\ttif->tif_dataoff=(uint32)tif->tif_dataoff;\n\t\tif ((tif->tif_dataoff<tif->tif_diroff)||(tif->tif_dataoff<(uint64)dirsize))\n\t\t{\n\t\t\tTIFFErrorExt(tif->tif_clientdata,module,"Maximum TIFF file size exceeded");\n\t\t\tgoto bad;\n\t\t}\n\t\tif (tif->tif_dataoff&1)\n\t\t\ttif->tif_dataoff++;\n\t\tif (isimage)\n\t\t\ttif->tif_curdir++;\n\t}\n\tif (isimage)\n\t{\n\t\tif (TIFFFieldSet(tif,FIELD_SUBIFD)&&(tif->tif_subifdoff==0))\n\t\t{\n\t\t\tuint32 na;\n\t\t\tTIFFDirEntry* nb;\n\t\t\tfor (na=0, nb=dir; ; na++, nb++)\n\t\t\t{\n\t\t\t\tif( na == ndir )\n {\n TIFFErrorExt(tif->tif_clientdata,module,\n "Cannot find SubIFD tag");\n goto bad;\n }\n\t\t\t\tif (nb->tdir_tag==TIFFTAG_SUBIFD)\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t\tif (!(tif->tif_flags&TIFF_BIGTIFF))\n\t\t\t\ttif->tif_subifdoff=tif->tif_diroff+2+na*12+8;\n\t\t\telse\n\t\t\t\ttif->tif_subifdoff=tif->tif_diroff+8+na*20+12;\n\t\t}\n\t}\n\tdirmem=_TIFFmalloc(dirsize);\n\tif (dirmem==NULL)\n\t{\n\t\tTIFFErrorExt(tif->tif_clientdata,module,"Out of memory");\n\t\tgoto bad;\n\t}\n\tif (!(tif->tif_flags&TIFF_BIGTIFF))\n\t{\n\t\tuint8* n;\n\t\tuint32 nTmp;\n\t\tTIFFDirEntry* o;\n\t\tn=dirmem;\n\t\t*(uint16*)n=(uint16)ndir;\n\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\tTIFFSwabShort((uint16*)n);\n\t\tn+=2;\n\t\to=dir;\n\t\tfor (m=0; m<ndir; m++)\n\t\t{\n\t\t\t*(uint16*)n=o->tdir_tag;\n\t\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\t\tTIFFSwabShort((uint16*)n);\n\t\t\tn+=2;\n\t\t\t*(uint16*)n=o->tdir_type;\n\t\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\t\tTIFFSwabShort((uint16*)n);\n\t\t\tn+=2;\n\t\t\tnTmp = (uint32)o->tdir_count;\n\t\t\t_TIFFmemcpy(n,&nTmp,4);\n\t\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\t\tTIFFSwabLong((uint32*)n);\n\t\t\tn+=4;\n\t\t\t_TIFFmemcpy(n,&o->tdir_offset,4);\n\t\t\tn+=4;\n\t\t\to++;\n\t\t}\n\t\tnTmp = (uint32)tif->tif_nextdiroff;\n\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\tTIFFSwabLong(&nTmp);\n\t\t_TIFFmemcpy(n,&nTmp,4);\n\t}\n\telse\n\t{\n\t\tuint8* n;\n\t\tTIFFDirEntry* o;\n\t\tn=dirmem;\n\t\t*(uint64*)n=ndir;\n\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\tTIFFSwabLong8((uint64*)n);\n\t\tn+=8;\n\t\to=dir;\n\t\tfor (m=0; m<ndir; m++)\n\t\t{\n\t\t\t*(uint16*)n=o->tdir_tag;\n\t\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\t\tTIFFSwabShort((uint16*)n);\n\t\t\tn+=2;\n\t\t\t*(uint16*)n=o->tdir_type;\n\t\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\t\tTIFFSwabShort((uint16*)n);\n\t\t\tn+=2;\n\t\t\t_TIFFmemcpy(n,&o->tdir_count,8);\n\t\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\t\tTIFFSwabLong8((uint64*)n);\n\t\t\tn+=8;\n\t\t\t_TIFFmemcpy(n,&o->tdir_offset,8);\n\t\t\tn+=8;\n\t\t\to++;\n\t\t}\n\t\t_TIFFmemcpy(n,&tif->tif_nextdiroff,8);\n\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\tTIFFSwabLong8((uint64*)n);\n\t}\n\t_TIFFfree(dir);\n\tdir=NULL;\n\tif (!SeekOK(tif,tif->tif_diroff))\n\t{\n\t\tTIFFErrorExt(tif->tif_clientdata,module,"IO error writing directory");\n\t\tgoto bad;\n\t}\n\tif (!WriteOK(tif,dirmem,(tmsize_t)dirsize))\n\t{\n\t\tTIFFErrorExt(tif->tif_clientdata,module,"IO error writing directory");\n\t\tgoto bad;\n\t}\n\t_TIFFfree(dirmem);\n\tif (imagedone)\n\t{\n\t\tTIFFFreeDirectory(tif);\n\t\ttif->tif_flags &= ~TIFF_DIRTYDIRECT;\n\t\ttif->tif_flags &= ~TIFF_DIRTYSTRIP;\n\t\t(*tif->tif_cleanup)(tif);\n\t\tTIFFCreateDirectory(tif);\n\t}\n\treturn(1);\nbad:\n\tif (dir!=NULL)\n\t\t_TIFFfree(dir);\n\tif (dirmem!=NULL)\n\t\t_TIFFfree(dirmem);\n\treturn(0);\n}', 'static int\nTIFFWriteDirectoryTagLongLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)\n{\n static const char module[] = "TIFFWriteDirectoryTagLongLong8Array";\n int o;\n int write_aslong4;\n if (dir==NULL)\n {\n (*ndir)++;\n return(1);\n }\n if( tif->tif_flags&TIFF_BIGTIFF )\n {\n int write_aslong8 = 1;\n if( count > 1 && tag == TIFFTAG_STRIPBYTECOUNTS )\n {\n write_aslong8 = WriteAsLong8(tif, TIFFStripSize64(tif));\n }\n else if( count > 1 && tag == TIFFTAG_TILEBYTECOUNTS )\n {\n write_aslong8 = WriteAsLong8(tif, TIFFTileSize64(tif));\n }\n if( write_aslong8 )\n {\n return TIFFWriteDirectoryTagCheckedLong8Array(tif,ndir,dir,\n tag,count,value);\n }\n }\n write_aslong4 = 1;\n if( count > 1 && tag == TIFFTAG_STRIPBYTECOUNTS )\n {\n write_aslong4 = WriteAsLong4(tif, TIFFStripSize64(tif));\n }\n else if( count > 1 && tag == TIFFTAG_TILEBYTECOUNTS )\n {\n write_aslong4 = WriteAsLong4(tif, TIFFTileSize64(tif));\n }\n if( write_aslong4 )\n {\n uint32* p = _TIFFmalloc(count*sizeof(uint32));\n uint32* q;\n uint64* ma;\n uint32 mb;\n if (p==NULL)\n {\n TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");\n return(0);\n }\n for (q=p, ma=value, mb=0; mb<count; ma++, mb++, q++)\n {\n if (*ma>0xFFFFFFFF)\n {\n TIFFErrorExt(tif->tif_clientdata,module,\n "Attempt to write value larger than 0xFFFFFFFF in LONG array.");\n _TIFFfree(p);\n return(0);\n }\n *q= (uint32)(*ma);\n }\n o=TIFFWriteDirectoryTagCheckedLongArray(tif,ndir,dir,tag,count,p);\n _TIFFfree(p);\n }\n else\n {\n uint16* p = _TIFFmalloc(count*sizeof(uint16));\n uint16* q;\n uint64* ma;\n uint32 mb;\n if (p==NULL)\n {\n TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");\n return(0);\n }\n for (q=p, ma=value, mb=0; mb<count; ma++, mb++, q++)\n {\n if (*ma>0xFFFF)\n {\n TIFFErrorExt(tif->tif_clientdata,module,\n "Attempt to write value larger than 0xFFFF in SHORT array.");\n _TIFFfree(p);\n return(0);\n }\n *q= (uint16)(*ma);\n }\n o=TIFFWriteDirectoryTagCheckedShortArray(tif,ndir,dir,tag,count,p);\n _TIFFfree(p);\n }\n return(o);\n}', 'void*\n_TIFFmalloc(tmsize_t s)\n{\n if (s == 0)\n return ((void *) NULL);\n\treturn (malloc((size_t) s));\n}', 'static int\nTIFFWriteDirectoryTagCheckedLongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value)\n{\n\tassert(count<0x40000000);\n\tassert(sizeof(uint32)==4);\n\tif (tif->tif_flags&TIFF_SWAB)\n\t\tTIFFSwabArrayOfLong(value,count);\n\treturn(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_LONG,count,count*4,value));\n}', 'void\nTIFFSwabArrayOfLong(register uint32* lp, tmsize_t n)\n{\n\tregister unsigned char *cp;\n\tregister unsigned char t;\n\tassert(sizeof(uint32)==4);\n\twhile (n-- > 0) {\n\t\tcp = (unsigned char *)lp;\n\t\tt = cp[3]; cp[3] = cp[0]; cp[0] = t;\n\t\tt = cp[2]; cp[2] = cp[1]; cp[1] = t;\n\t\tlp++;\n\t}\n}'] |
35,450 | 0 | https://github.com/libav/libav/blob/78f318be59a8e6174f21c2d7c3403ef325c73011/libavformat/oggparseflac.c/#L65 | static int
flac_header (AVFormatContext * s, int idx)
{
struct ogg *ogg = s->priv_data;
struct ogg_stream *os = ogg->streams + idx;
AVStream *st = s->streams[idx];
GetBitContext gb;
FLACStreaminfo si;
int mdt;
if (os->buf[os->pstart] == 0xff)
return 0;
init_get_bits(&gb, os->buf + os->pstart, os->psize*8);
skip_bits1(&gb);
mdt = get_bits(&gb, 7);
if (mdt == OGG_FLAC_METADATA_TYPE_STREAMINFO) {
uint8_t *streaminfo_start = os->buf + os->pstart + 5 + 4 + 4 + 4;
skip_bits_long(&gb, 4*8);
if(get_bits(&gb, 8) != 1)
return -1;
skip_bits_long(&gb, 8 + 16);
skip_bits_long(&gb, 4*8);
if (get_bits_long(&gb, 32) != FLAC_STREAMINFO_SIZE)
return -1;
ff_flac_parse_streaminfo(st->codec, &si, streaminfo_start);
st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
st->codec->codec_id = CODEC_ID_FLAC;
st->codec->extradata =
av_malloc(FLAC_STREAMINFO_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
memcpy(st->codec->extradata, streaminfo_start, FLAC_STREAMINFO_SIZE);
st->codec->extradata_size = FLAC_STREAMINFO_SIZE;
st->time_base.num = 1;
st->time_base.den = st->codec->sample_rate;
} else if (mdt == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
ff_vorbis_comment (s, &st->metadata, os->buf + os->pstart + 4, os->psize - 4);
}
return 1;
} | ['static int\nflac_header (AVFormatContext * s, int idx)\n{\n struct ogg *ogg = s->priv_data;\n struct ogg_stream *os = ogg->streams + idx;\n AVStream *st = s->streams[idx];\n GetBitContext gb;\n FLACStreaminfo si;\n int mdt;\n if (os->buf[os->pstart] == 0xff)\n return 0;\n init_get_bits(&gb, os->buf + os->pstart, os->psize*8);\n skip_bits1(&gb);\n mdt = get_bits(&gb, 7);\n if (mdt == OGG_FLAC_METADATA_TYPE_STREAMINFO) {\n uint8_t *streaminfo_start = os->buf + os->pstart + 5 + 4 + 4 + 4;\n skip_bits_long(&gb, 4*8);\n if(get_bits(&gb, 8) != 1)\n return -1;\n skip_bits_long(&gb, 8 + 16);\n skip_bits_long(&gb, 4*8);\n if (get_bits_long(&gb, 32) != FLAC_STREAMINFO_SIZE)\n return -1;\n ff_flac_parse_streaminfo(st->codec, &si, streaminfo_start);\n st->codec->codec_type = AVMEDIA_TYPE_AUDIO;\n st->codec->codec_id = CODEC_ID_FLAC;\n st->codec->extradata =\n av_malloc(FLAC_STREAMINFO_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);\n memcpy(st->codec->extradata, streaminfo_start, FLAC_STREAMINFO_SIZE);\n st->codec->extradata_size = FLAC_STREAMINFO_SIZE;\n st->time_base.num = 1;\n st->time_base.den = st->codec->sample_rate;\n } else if (mdt == FLAC_METADATA_TYPE_VORBIS_COMMENT) {\n ff_vorbis_comment (s, &st->metadata, os->buf + os->pstart + 4, os->psize - 4);\n }\n return 1;\n}', 'static inline void init_get_bits(GetBitContext *s,\n const uint8_t *buffer, int bit_size)\n{\n int buffer_size = (bit_size+7)>>3;\n if (buffer_size < 0 || bit_size < 0) {\n buffer_size = bit_size = 0;\n buffer = NULL;\n }\n s->buffer = buffer;\n s->size_in_bits = bit_size;\n s->buffer_end = buffer + buffer_size;\n#ifdef ALT_BITSTREAM_READER\n s->index = 0;\n#elif defined A32_BITSTREAM_READER\n s->buffer_ptr = (uint32_t*)((intptr_t)buffer & ~3);\n s->bit_count = 32 + 8*((intptr_t)buffer & 3);\n skip_bits_long(s, 0);\n#endif\n}', 'static inline void skip_bits1(GetBitContext *s){\n skip_bits(s, 1);\n}', 'static inline void skip_bits(GetBitContext *s, int n){\n OPEN_READER(re, s);\n UPDATE_CACHE(re, s);\n LAST_SKIP_BITS(re, s, n);\n CLOSE_READER(re, s);\n}', 'static av_always_inline av_const uint32_t av_bswap32(uint32_t x)\n{\n x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);\n x= (x>>16) | (x<<16);\n return x;\n}', 'static inline 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 inline void skip_bits_long(GetBitContext *s, int n){\n s->index += n;\n}', 'static inline unsigned int get_bits_long(GetBitContext *s, int n){\n if (n <= MIN_CACHE_BITS) return get_bits(s, n);\n else {\n#ifdef ALT_BITSTREAM_READER_LE\n int ret = get_bits(s, 16);\n return ret | (get_bits(s, n-16) << 16);\n#else\n int ret = get_bits(s, 16) << (n-16);\n return ret | get_bits(s, n-16);\n#endif\n }\n}', 'void *av_malloc(FF_INTERNAL_MEM_TYPE size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-16) )\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size+16);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&15) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr,16,size))\n ptr = NULL;\n#elif HAVE_MEMALIGN\n ptr = memalign(16,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}'] |
35,451 | 0 | https://github.com/libav/libav/blob/9f54e461fecec7a97ec1b97ae4468135ea770609/libavformat/movenc.c/#L901 | static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track)
{
MOVStts *ctts_entries;
uint32_t entries = 0;
uint32_t atom_size;
int i;
ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries));
ctts_entries[0].count = 1;
ctts_entries[0].duration = track->cluster[0].cts;
for (i=1; i<track->entry; i++) {
if (track->cluster[i].cts == ctts_entries[entries].duration) {
ctts_entries[entries].count++;
} else {
entries++;
ctts_entries[entries].duration = track->cluster[i].cts;
ctts_entries[entries].count = 1;
}
}
entries++;
atom_size = 16 + (entries * 8);
avio_wb32(pb, atom_size);
ffio_wfourcc(pb, "ctts");
avio_wb32(pb, 0);
avio_wb32(pb, entries);
for (i=0; i<entries; i++) {
avio_wb32(pb, ctts_entries[i].count);
avio_wb32(pb, ctts_entries[i].duration);
}
av_free(ctts_entries);
return atom_size;
} | ['static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track)\n{\n MOVStts *ctts_entries;\n uint32_t entries = 0;\n uint32_t atom_size;\n int i;\n ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries));\n ctts_entries[0].count = 1;\n ctts_entries[0].duration = track->cluster[0].cts;\n for (i=1; i<track->entry; i++) {\n if (track->cluster[i].cts == ctts_entries[entries].duration) {\n ctts_entries[entries].count++;\n } else {\n entries++;\n ctts_entries[entries].duration = track->cluster[i].cts;\n ctts_entries[entries].count = 1;\n }\n }\n entries++;\n atom_size = 16 + (entries * 8);\n avio_wb32(pb, atom_size);\n ffio_wfourcc(pb, "ctts");\n avio_wb32(pb, 0);\n avio_wb32(pb, entries);\n for (i=0; i<entries; i++) {\n avio_wb32(pb, ctts_entries[i].count);\n avio_wb32(pb, ctts_entries[i].duration);\n }\n av_free(ctts_entries);\n return atom_size;\n}', 'void *av_malloc(size_t size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-32) )\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size+32);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&31) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr,32,size))\n ptr = NULL;\n#elif HAVE_MEMALIGN\n ptr = memalign(32,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}'] |
35,452 | 0 | https://github.com/libav/libav/blob/df84d7d9bdf6b8e6896c711880f130b72738c828/libavcodec/mpegaudiodec.c/#L911 | 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}'] |
35,453 | 0 | https://github.com/libav/libav/blob/4860abb116674c7be31e825db05cdcfd30f3aff2/libavcodec/flac.c/#L309 | static int decode_subframe_fixed(FLACContext *s, int channel, int pred_order)
{
const int blocksize = s->blocksize;
int32_t *decoded = s->decoded[channel];
int a, b, c, d, i;
for (i = 0; i < pred_order; i++)
{
decoded[i] = get_sbits(&s->gb, s->curr_bps);
}
if (decode_residuals(s, channel, pred_order) < 0)
return -1;
if(pred_order > 0)
a = decoded[pred_order-1];
if(pred_order > 1)
b = a - decoded[pred_order-2];
if(pred_order > 2)
c = b - decoded[pred_order-2] + decoded[pred_order-3];
if(pred_order > 3)
d = c - decoded[pred_order-2] + 2*decoded[pred_order-3] - decoded[pred_order-4];
switch(pred_order)
{
case 0:
break;
case 1:
for (i = pred_order; i < blocksize; i++)
decoded[i] = a += decoded[i];
break;
case 2:
for (i = pred_order; i < blocksize; i++)
decoded[i] = a += b += decoded[i];
break;
case 3:
for (i = pred_order; i < blocksize; i++)
decoded[i] = a += b += c += decoded[i];
break;
case 4:
for (i = pred_order; i < blocksize; i++)
decoded[i] = a += b += c += d += decoded[i];
break;
default:
av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order);
return -1;
}
return 0;
} | ['static int decode_subframe_fixed(FLACContext *s, int channel, int pred_order)\n{\n const int blocksize = s->blocksize;\n int32_t *decoded = s->decoded[channel];\n int a, b, c, d, i;\n for (i = 0; i < pred_order; i++)\n {\n decoded[i] = get_sbits(&s->gb, s->curr_bps);\n }\n if (decode_residuals(s, channel, pred_order) < 0)\n return -1;\n if(pred_order > 0)\n a = decoded[pred_order-1];\n if(pred_order > 1)\n b = a - decoded[pred_order-2];\n if(pred_order > 2)\n c = b - decoded[pred_order-2] + decoded[pred_order-3];\n if(pred_order > 3)\n d = c - decoded[pred_order-2] + 2*decoded[pred_order-3] - decoded[pred_order-4];\n switch(pred_order)\n {\n case 0:\n break;\n case 1:\n for (i = pred_order; i < blocksize; i++)\n decoded[i] = a += decoded[i];\n break;\n case 2:\n for (i = pred_order; i < blocksize; i++)\n decoded[i] = a += b += decoded[i];\n break;\n case 3:\n for (i = pred_order; i < blocksize; i++)\n decoded[i] = a += b += c += decoded[i];\n break;\n case 4:\n for (i = pred_order; i < blocksize; i++)\n decoded[i] = a += b += c += d += decoded[i];\n break;\n default:\n av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\\n", pred_order);\n return -1;\n }\n return 0;\n}'] |
35,454 | 0 | https://github.com/libav/libav/blob/6ea220cbeec8863e2006a03b73bed52db2b13ee7/avtools/avconv.c/#L1653 | 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;
}
if (ist->hw_frames_ctx) {
s->hw_frames_ctx = av_buffer_ref(ist->hw_frames_ctx);
if (!s->hw_frames_ctx)
return AV_PIX_FMT_NONE;
}
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 if (ist->hw_frames_ctx) {\n s->hw_frames_ctx = av_buffer_ref(ist->hw_frames_ctx);\n if (!s->hw_frames_ctx)\n return AV_PIX_FMT_NONE;\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}'] |
35,455 | 0 | https://github.com/openssl/openssl/blob/9f519addc09b2005fa8c6cde36e3267de02577bb/ssl/record/ssl3_record.c/#L1176 | int ssl3_cbc_remove_padding(SSL3_RECORD *rec,
unsigned block_size, unsigned mac_size)
{
unsigned padding_length, good;
const unsigned overhead = 1 + mac_size;
if (overhead > rec->length)
return 0;
padding_length = rec->data[rec->length - 1];
good = constant_time_ge(rec->length, padding_length + overhead);
good &= constant_time_ge(block_size, padding_length + 1);
rec->length -= good & (padding_length + 1);
return constant_time_select_int(good, 1, -1);
} | ['int ssl3_enc(SSL *s, SSL3_RECORD *inrecs, unsigned int n_recs, int send)\n{\n SSL3_RECORD *rec;\n EVP_CIPHER_CTX *ds;\n unsigned long l;\n int bs, i, mac_size = 0;\n const EVP_CIPHER *enc;\n rec = inrecs;\n if (n_recs != 1)\n return 0;\n if (send) {\n ds = s->enc_write_ctx;\n if (s->enc_write_ctx == NULL)\n enc = NULL;\n else\n enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx);\n } else {\n ds = s->enc_read_ctx;\n if (s->enc_read_ctx == NULL)\n enc = NULL;\n else\n enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);\n }\n if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {\n memmove(rec->data, rec->input, rec->length);\n rec->input = rec->data;\n } else {\n l = rec->length;\n bs = EVP_CIPHER_CTX_block_size(ds);\n if ((bs != 1) && send) {\n i = bs - ((int)l % bs);\n l += i;\n memset(&rec->input[rec->length], 0, i);\n rec->length += i;\n rec->input[l - 1] = (i - 1);\n }\n if (!send) {\n if (l == 0 || l % bs != 0)\n return 0;\n }\n if (EVP_Cipher(ds, rec->data, rec->input, l) < 1)\n return -1;\n if (EVP_MD_CTX_md(s->read_hash) != NULL)\n mac_size = EVP_MD_CTX_size(s->read_hash);\n if ((bs != 1) && !send)\n return ssl3_cbc_remove_padding(rec, bs, mac_size);\n }\n return (1);\n}', 'int ssl3_cbc_remove_padding(SSL3_RECORD *rec,\n unsigned block_size, unsigned mac_size)\n{\n unsigned padding_length, good;\n const unsigned overhead = 1 + mac_size;\n if (overhead > rec->length)\n return 0;\n padding_length = rec->data[rec->length - 1];\n good = constant_time_ge(rec->length, padding_length + overhead);\n good &= constant_time_ge(block_size, padding_length + 1);\n rec->length -= good & (padding_length + 1);\n return constant_time_select_int(good, 1, -1);\n}'] |
35,456 | 0 | https://github.com/libav/libav/blob/54bc15d5ebfd07fd468743ba29f709ea19e840b9/libswscale/utils.c/#L193 | 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}'] |
35,457 | 0 | https://github.com/openssl/openssl/blob/0a52d38b31ee56479c80509716c3bd46b764190a/crypto/bn/bn_mul.c/#L1013 | int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
{
int ret=0;
int top,al,bl;
BIGNUM *rr;
#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
int i;
#endif
#ifdef BN_RECURSION
BIGNUM *t;
int j,k;
#endif
#ifdef BN_COUNT
fprintf(stderr,"BN_mul %d * %d\n",a->top,b->top);
#endif
bn_check_top(a);
bn_check_top(b);
bn_check_top(r);
al=a->top;
bl=b->top;
if ((al == 0) || (bl == 0))
{
BN_zero(r);
return(1);
}
top=al+bl;
BN_CTX_start(ctx);
if ((r == a) || (r == b))
{
if ((rr = BN_CTX_get(ctx)) == NULL) goto err;
}
else
rr = r;
rr->neg=a->neg^b->neg;
#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
i = al-bl;
#endif
#ifdef BN_MUL_COMBA
if (i == 0)
{
# if 0
if (al == 4)
{
if (bn_wexpand(rr,8) == NULL) goto err;
rr->top=8;
bn_mul_comba4(rr->d,a->d,b->d);
goto end;
}
# endif
if (al == 8)
{
if (bn_wexpand(rr,16) == NULL) goto err;
rr->top=16;
bn_mul_comba8(rr->d,a->d,b->d);
goto end;
}
}
#endif
#ifdef BN_RECURSION
if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL))
{
if (i >= -1 && i <= 1)
{
int sav_j =0;
if (i >= 0)
{
j = BN_num_bits_word((BN_ULONG)al);
}
if (i == -1)
{
j = BN_num_bits_word((BN_ULONG)bl);
}
sav_j = j;
j = 1<<(j-1);
assert(j <= al || j <= bl);
k = j+j;
t = BN_CTX_get(ctx);
if (al > j || bl > j)
{
bn_wexpand(t,k*4);
bn_wexpand(rr,k*4);
bn_mul_part_recursive(rr->d,a->d,b->d,
j,al-j,bl-j,t->d);
}
else
{
bn_wexpand(t,k*2);
bn_wexpand(rr,k*2);
bn_mul_recursive(rr->d,a->d,b->d,
j,al-j,bl-j,t->d);
}
rr->top=top;
goto end;
}
#if 0
if (i == 1 && !BN_get_flags(b,BN_FLG_STATIC_DATA))
{
BIGNUM *tmp_bn = (BIGNUM *)b;
bn_wexpand(tmp_bn,al);
tmp_bn->d[bl]=0;
bl++;
i--;
}
else if (i == -1 && !BN_get_flags(a,BN_FLG_STATIC_DATA))
{
BIGNUM *tmp_bn = (BIGNUM *)a;
bn_wexpand(tmp_bn,bl);
tmp_bn->d[al]=0;
al++;
i++;
}
if (i == 0)
{
j=BN_num_bits_word((BN_ULONG)al);
j=1<<(j-1);
k=j+j;
t = BN_CTX_get(ctx);
if (al == j)
{
bn_wexpand(t,k*2);
bn_wexpand(rr,k*2);
bn_mul_recursive(rr->d,a->d,b->d,al,t->d);
}
else
{
bn_wexpand(t,k*4);
bn_wexpand(rr,k*4);
bn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d);
}
rr->top=top;
goto end;
}
#endif
}
#endif
if (bn_wexpand(rr,top) == NULL) goto err;
rr->top=top;
bn_mul_normal(rr->d,a->d,al,b->d,bl);
#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
end:
#endif
bn_fix_top(rr);
if (r != rr) BN_copy(r,rr);
ret=1;
err:
BN_CTX_end(ctx);
return(ret);
} | ['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;\n\tint j,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\tint sav_j =0;\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\tsav_j = j;\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 (al > j || bl > j)\n\t\t\t\t{\n\t\t\t\tbn_wexpand(t,k*4);\n\t\t\t\tbn_wexpand(rr,k*4);\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\tbn_wexpand(t,k*2);\n\t\t\t\tbn_wexpand(rr,k*2);\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\tbn_wexpand(tmp_bn,al);\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\tbn_wexpand(tmp_bn,bl);\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\tbn_wexpand(t,k*2);\n\t\t\t\tbn_wexpand(rr,k*2);\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\tbn_wexpand(t,k*4);\n\t\t\t\tbn_wexpand(rr,k*4);\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_fix_top(rr);\n\tif (r != rr) BN_copy(r,rr);\n\tret=1;\nerr:\n\tBN_CTX_end(ctx);\n\treturn(ret);\n\t}'] |
35,458 | 0 | https://github.com/apache/httpd/blob/8b2ec33ac5d314be345814db08e194ffeda6beb0/modules/proxy/mod_proxy_ajp.c/#L252 | static int ap_proxy_ajp_request(apr_pool_t *p, request_rec *r,
proxy_conn_rec *conn,
conn_rec *origin,
proxy_dir_conf *conf,
apr_uri_t *uri,
char *url, char *server_portstr)
{
apr_status_t status;
int result;
apr_bucket *e;
apr_bucket_brigade *input_brigade;
apr_bucket_brigade *output_brigade;
ajp_msg_t *msg;
apr_size_t bufsiz = 0;
char *buff;
char *send_body_chunk_buff;
apr_uint16_t size;
apr_byte_t conn_reuse = 0;
const char *tenc;
int havebody = 1;
int output_failed = 0;
int backend_failed = 0;
apr_off_t bb_len;
int data_sent = 0;
int request_ended = 0;
int headers_sent = 0;
int rv = 0;
apr_int32_t conn_poll_fd;
apr_pollfd_t *conn_poll;
proxy_server_conf *psf =
ap_get_module_config(r->server->module_config, &proxy_module);
apr_size_t maxsize = AJP_MSG_BUFFER_SZ;
int send_body = 0;
apr_off_t content_length = 0;
int original_status = r->status;
const char *original_status_line = r->status_line;
if (psf->io_buffer_size_set)
maxsize = psf->io_buffer_size;
if (maxsize > AJP_MAX_BUFFER_SZ)
maxsize = AJP_MAX_BUFFER_SZ;
else if (maxsize < AJP_MSG_BUFFER_SZ)
maxsize = AJP_MSG_BUFFER_SZ;
maxsize = APR_ALIGN(maxsize, 1024);
status = ajp_send_header(conn->sock, r, maxsize, uri);
if (status != APR_SUCCESS) {
conn->close++;
ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(00868)
"request failed to %pI (%s)",
conn->worker->cp->addr,
conn->worker->s->hostname);
if (status == AJP_EOVERFLOW)
return HTTP_BAD_REQUEST;
else if (status == AJP_EBAD_METHOD) {
return HTTP_NOT_IMPLEMENTED;
} else {
if (is_idempotent(r) == METHOD_IDEMPOTENT) {
return HTTP_SERVICE_UNAVAILABLE;
}
return HTTP_INTERNAL_SERVER_ERROR;
}
}
bufsiz = maxsize;
status = ajp_alloc_data_msg(r->pool, &buff, &bufsiz, &msg);
if (status != APR_SUCCESS) {
conn->close++;
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00869)
"ajp_alloc_data_msg failed");
return HTTP_INTERNAL_SERVER_ERROR;
}
input_brigade = apr_brigade_create(p, r->connection->bucket_alloc);
tenc = apr_table_get(r->headers_in, "Transfer-Encoding");
if (tenc && (strcasecmp(tenc, "chunked") == 0)) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00870) "request is chunked");
} else {
content_length = get_content_length(r);
status = ap_get_brigade(r->input_filters, input_brigade,
AP_MODE_READBYTES, APR_BLOCK_READ,
maxsize - AJP_HEADER_SZ);
if (status != APR_SUCCESS) {
conn->close++;
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00871)
"ap_get_brigade failed");
apr_brigade_destroy(input_brigade);
return HTTP_BAD_REQUEST;
}
if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00872) "APR_BUCKET_IS_EOS");
}
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00873)
"data to read (max %" APR_SIZE_T_FMT
" at %" APR_SIZE_T_FMT ")", bufsiz, msg->pos);
status = apr_brigade_flatten(input_brigade, buff, &bufsiz);
if (status != APR_SUCCESS) {
conn->close++;
apr_brigade_destroy(input_brigade);
ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(00874)
"apr_brigade_flatten");
return HTTP_INTERNAL_SERVER_ERROR;
}
apr_brigade_cleanup(input_brigade);
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00875)
"got %" APR_SIZE_T_FMT " bytes of data", bufsiz);
if (bufsiz > 0) {
status = ajp_send_data_msg(conn->sock, msg, bufsiz);
ajp_msg_log(r, msg, "First ajp_send_data_msg: ajp_ilink_send packet dump");
if (status != APR_SUCCESS) {
conn->close++;
apr_brigade_destroy(input_brigade);
ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(00876)
"send failed to %pI (%s)",
conn->worker->cp->addr,
conn->worker->s->hostname);
return HTTP_INTERNAL_SERVER_ERROR;
}
conn->worker->s->transferred += bufsiz;
send_body = 1;
}
else if (content_length > 0) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(00877)
"read zero bytes, expecting"
" %" APR_OFF_T_FMT " bytes",
content_length);
conn->close++;
return HTTP_BAD_REQUEST;
}
}
conn->data = NULL;
status = ajp_read_header(conn->sock, r, maxsize,
(ajp_msg_t **)&(conn->data));
if (status != APR_SUCCESS) {
conn->close++;
apr_brigade_destroy(input_brigade);
ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(00878)
"read response failed from %pI (%s)",
conn->worker->cp->addr,
conn->worker->s->hostname);
if (APR_STATUS_IS_TIMEUP(status) && conn->worker->s->ping_timeout_set) {
return HTTP_GATEWAY_TIME_OUT;
}
if (!send_body && (is_idempotent(r) == METHOD_IDEMPOTENT)) {
return HTTP_SERVICE_UNAVAILABLE;
}
return HTTP_INTERNAL_SERVER_ERROR;
}
result = ajp_parse_type(r, conn->data);
output_brigade = apr_brigade_create(p, r->connection->bucket_alloc);
conn_poll = apr_pcalloc(p, sizeof(apr_pollfd_t));
conn_poll->reqevents = APR_POLLIN;
conn_poll->desc_type = APR_POLL_SOCKET;
conn_poll->desc.s = conn->sock;
bufsiz = maxsize;
for (;;) {
switch (result) {
case CMD_AJP13_GET_BODY_CHUNK:
if (havebody) {
if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
bufsiz = 0;
havebody = 0;
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, status, r, APLOGNO(00879)
"APR_BUCKET_IS_EOS");
} else {
status = ap_get_brigade(r->input_filters, input_brigade,
AP_MODE_READBYTES,
APR_BLOCK_READ,
maxsize - AJP_HEADER_SZ);
if (status != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, status, r, APLOGNO(00880)
"ap_get_brigade failed");
output_failed = 1;
break;
}
bufsiz = maxsize;
status = apr_brigade_flatten(input_brigade, buff,
&bufsiz);
apr_brigade_cleanup(input_brigade);
if (status != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, status, r, APLOGNO(00881)
"apr_brigade_flatten failed");
output_failed = 1;
break;
}
}
ajp_msg_reset(msg);
status = ajp_send_data_msg(conn->sock, msg, bufsiz);
ajp_msg_log(r, msg, "ajp_send_data_msg after CMD_AJP13_GET_BODY_CHUNK: ajp_ilink_send packet dump");
if (status != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, status, r, APLOGNO(00882)
"ajp_send_data_msg failed");
backend_failed = 1;
break;
}
conn->worker->s->transferred += bufsiz;
} else {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00883)
"ap_proxy_ajp_request error read after end");
backend_failed = 1;
}
break;
case CMD_AJP13_SEND_HEADERS:
if (headers_sent) {
backend_failed = 1;
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00884)
"Backend sent headers twice.");
break;
}
status = ajp_parse_header(r, conf, conn->data);
if (status != APR_SUCCESS) {
backend_failed = 1;
}
else if ((r->status == 401) && conf->error_override) {
const char *buf;
const char *wa = "WWW-Authenticate";
if ((buf = apr_table_get(r->headers_out, wa))) {
apr_table_set(r->err_headers_out, wa, buf);
} else {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00885)
"ap_proxy_ajp_request: origin server "
"sent 401 without WWW-Authenticate header");
}
}
headers_sent = 1;
break;
case CMD_AJP13_SEND_BODY_CHUNK:
status = ajp_parse_data(r, conn->data, &size, &send_body_chunk_buff);
if (status == APR_SUCCESS) {
if (!conf->error_override || !ap_is_HTTP_ERROR(r->status)) {
if (size == 0) {
if (headers_sent) {
e = apr_bucket_flush_create(r->connection->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(output_brigade, e);
}
else {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00886)
"Ignoring flush message "
"received before headers");
}
}
else {
apr_status_t rv;
if (conf->error_override && !ap_is_HTTP_ERROR(r->status)
&& ap_is_HTTP_ERROR(original_status)) {
r->status = original_status;
r->status_line = original_status_line;
}
e = apr_bucket_transient_create(send_body_chunk_buff, size,
r->connection->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(output_brigade, e);
if ((conn->worker->s->flush_packets == flush_on) ||
((conn->worker->s->flush_packets == flush_auto) &&
((rv = apr_poll(conn_poll, 1, &conn_poll_fd,
conn->worker->s->flush_wait))
!= APR_SUCCESS) &&
APR_STATUS_IS_TIMEUP(rv))) {
e = apr_bucket_flush_create(r->connection->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(output_brigade, e);
}
apr_brigade_length(output_brigade, 0, &bb_len);
if (bb_len != -1)
conn->worker->s->read += bb_len;
}
if (headers_sent) {
if (ap_pass_brigade(r->output_filters,
output_brigade) != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00887)
"error processing body.%s",
r->connection->aborted ?
" Client aborted connection." : "");
output_failed = 1;
}
data_sent = 1;
apr_brigade_cleanup(output_brigade);
}
}
}
else {
backend_failed = 1;
}
break;
case CMD_AJP13_END_RESPONSE:
status = ajp_parse_reuse(r, conn->data, &conn_reuse);
if (status != APR_SUCCESS) {
backend_failed = 1;
}
if (!conf->error_override || !ap_is_HTTP_ERROR(r->status)) {
e = apr_bucket_eos_create(r->connection->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(output_brigade, e);
if (ap_pass_brigade(r->output_filters,
output_brigade) != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00888)
"error processing end");
output_failed = 1;
}
data_sent = 1;
}
request_ended = 1;
break;
default:
backend_failed = 1;
break;
}
if (r->connection->aborted) {
conn->close++;
output_failed = 0;
result = CMD_AJP13_END_RESPONSE;
request_ended = 1;
}
if ((result == CMD_AJP13_END_RESPONSE) || backend_failed
|| output_failed)
break;
status = ajp_read_header(conn->sock, r, maxsize,
(ajp_msg_t **)&(conn->data));
if (status != APR_SUCCESS) {
backend_failed = 1;
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, status, r, APLOGNO(00889)
"ajp_read_header failed");
break;
}
result = ajp_parse_type(r, conn->data);
}
apr_brigade_destroy(input_brigade);
apr_brigade_cleanup(output_brigade);
if (backend_failed || output_failed) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00890)
"Processing of request failed backend: %i, "
"output: %i", backend_failed, output_failed);
conn->close++;
if (data_sent) {
rv = DONE;
}
}
else if (!request_ended) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00891)
"Processing of request didn't terminate cleanly");
conn->close++;
backend_failed = 1;
if (data_sent) {
rv = DONE;
}
}
else if (!conn_reuse) {
conn->close++;
}
else {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00892)
"got response from %pI (%s)",
conn->worker->cp->addr,
conn->worker->s->hostname);
if (conf->error_override && ap_is_HTTP_ERROR(r->status)) {
rv = r->status;
r->status = HTTP_OK;
}
else {
rv = OK;
}
}
if (backend_failed) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(00893)
"dialog to %pI (%s) failed",
conn->worker->cp->addr,
conn->worker->s->hostname);
if (data_sent) {
ap_proxy_backend_broke(r, output_brigade);
} else if (!send_body && (is_idempotent(r) == METHOD_IDEMPOTENT)) {
rv = HTTP_SERVICE_UNAVAILABLE;
} else {
rv = HTTP_INTERNAL_SERVER_ERROR;
}
}
if (data_sent && !r->eos_sent && APR_BRIGADE_EMPTY(output_brigade)) {
e = apr_bucket_eos_create(r->connection->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(output_brigade, e);
}
if (!APR_BRIGADE_EMPTY(output_brigade))
ap_pass_brigade(r->output_filters, output_brigade);
apr_brigade_destroy(output_brigade);
if (apr_table_get(r->subprocess_env, "proxy-nokeepalive")) {
conn->close++;
}
return rv;
} | ['static int ap_proxy_ajp_request(apr_pool_t *p, request_rec *r,\n proxy_conn_rec *conn,\n conn_rec *origin,\n proxy_dir_conf *conf,\n apr_uri_t *uri,\n char *url, char *server_portstr)\n{\n apr_status_t status;\n int result;\n apr_bucket *e;\n apr_bucket_brigade *input_brigade;\n apr_bucket_brigade *output_brigade;\n ajp_msg_t *msg;\n apr_size_t bufsiz = 0;\n char *buff;\n char *send_body_chunk_buff;\n apr_uint16_t size;\n apr_byte_t conn_reuse = 0;\n const char *tenc;\n int havebody = 1;\n int output_failed = 0;\n int backend_failed = 0;\n apr_off_t bb_len;\n int data_sent = 0;\n int request_ended = 0;\n int headers_sent = 0;\n int rv = 0;\n apr_int32_t conn_poll_fd;\n apr_pollfd_t *conn_poll;\n proxy_server_conf *psf =\n ap_get_module_config(r->server->module_config, &proxy_module);\n apr_size_t maxsize = AJP_MSG_BUFFER_SZ;\n int send_body = 0;\n apr_off_t content_length = 0;\n int original_status = r->status;\n const char *original_status_line = r->status_line;\n if (psf->io_buffer_size_set)\n maxsize = psf->io_buffer_size;\n if (maxsize > AJP_MAX_BUFFER_SZ)\n maxsize = AJP_MAX_BUFFER_SZ;\n else if (maxsize < AJP_MSG_BUFFER_SZ)\n maxsize = AJP_MSG_BUFFER_SZ;\n maxsize = APR_ALIGN(maxsize, 1024);\n status = ajp_send_header(conn->sock, r, maxsize, uri);\n if (status != APR_SUCCESS) {\n conn->close++;\n ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(00868)\n "request failed to %pI (%s)",\n conn->worker->cp->addr,\n conn->worker->s->hostname);\n if (status == AJP_EOVERFLOW)\n return HTTP_BAD_REQUEST;\n else if (status == AJP_EBAD_METHOD) {\n return HTTP_NOT_IMPLEMENTED;\n } else {\n if (is_idempotent(r) == METHOD_IDEMPOTENT) {\n return HTTP_SERVICE_UNAVAILABLE;\n }\n return HTTP_INTERNAL_SERVER_ERROR;\n }\n }\n bufsiz = maxsize;\n status = ajp_alloc_data_msg(r->pool, &buff, &bufsiz, &msg);\n if (status != APR_SUCCESS) {\n conn->close++;\n ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00869)\n "ajp_alloc_data_msg failed");\n return HTTP_INTERNAL_SERVER_ERROR;\n }\n input_brigade = apr_brigade_create(p, r->connection->bucket_alloc);\n tenc = apr_table_get(r->headers_in, "Transfer-Encoding");\n if (tenc && (strcasecmp(tenc, "chunked") == 0)) {\n ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00870) "request is chunked");\n } else {\n content_length = get_content_length(r);\n status = ap_get_brigade(r->input_filters, input_brigade,\n AP_MODE_READBYTES, APR_BLOCK_READ,\n maxsize - AJP_HEADER_SZ);\n if (status != APR_SUCCESS) {\n conn->close++;\n ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00871)\n "ap_get_brigade failed");\n apr_brigade_destroy(input_brigade);\n return HTTP_BAD_REQUEST;\n }\n if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {\n ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00872) "APR_BUCKET_IS_EOS");\n }\n ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00873)\n "data to read (max %" APR_SIZE_T_FMT\n " at %" APR_SIZE_T_FMT ")", bufsiz, msg->pos);\n status = apr_brigade_flatten(input_brigade, buff, &bufsiz);\n if (status != APR_SUCCESS) {\n conn->close++;\n apr_brigade_destroy(input_brigade);\n ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(00874)\n "apr_brigade_flatten");\n return HTTP_INTERNAL_SERVER_ERROR;\n }\n apr_brigade_cleanup(input_brigade);\n ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00875)\n "got %" APR_SIZE_T_FMT " bytes of data", bufsiz);\n if (bufsiz > 0) {\n status = ajp_send_data_msg(conn->sock, msg, bufsiz);\n ajp_msg_log(r, msg, "First ajp_send_data_msg: ajp_ilink_send packet dump");\n if (status != APR_SUCCESS) {\n conn->close++;\n apr_brigade_destroy(input_brigade);\n ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(00876)\n "send failed to %pI (%s)",\n conn->worker->cp->addr,\n conn->worker->s->hostname);\n return HTTP_INTERNAL_SERVER_ERROR;\n }\n conn->worker->s->transferred += bufsiz;\n send_body = 1;\n }\n else if (content_length > 0) {\n ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(00877)\n "read zero bytes, expecting"\n " %" APR_OFF_T_FMT " bytes",\n content_length);\n conn->close++;\n return HTTP_BAD_REQUEST;\n }\n }\n conn->data = NULL;\n status = ajp_read_header(conn->sock, r, maxsize,\n (ajp_msg_t **)&(conn->data));\n if (status != APR_SUCCESS) {\n conn->close++;\n apr_brigade_destroy(input_brigade);\n ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(00878)\n "read response failed from %pI (%s)",\n conn->worker->cp->addr,\n conn->worker->s->hostname);\n if (APR_STATUS_IS_TIMEUP(status) && conn->worker->s->ping_timeout_set) {\n return HTTP_GATEWAY_TIME_OUT;\n }\n if (!send_body && (is_idempotent(r) == METHOD_IDEMPOTENT)) {\n return HTTP_SERVICE_UNAVAILABLE;\n }\n return HTTP_INTERNAL_SERVER_ERROR;\n }\n result = ajp_parse_type(r, conn->data);\n output_brigade = apr_brigade_create(p, r->connection->bucket_alloc);\n conn_poll = apr_pcalloc(p, sizeof(apr_pollfd_t));\n conn_poll->reqevents = APR_POLLIN;\n conn_poll->desc_type = APR_POLL_SOCKET;\n conn_poll->desc.s = conn->sock;\n bufsiz = maxsize;\n for (;;) {\n switch (result) {\n case CMD_AJP13_GET_BODY_CHUNK:\n if (havebody) {\n if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {\n bufsiz = 0;\n havebody = 0;\n ap_log_rerror(APLOG_MARK, APLOG_DEBUG, status, r, APLOGNO(00879)\n "APR_BUCKET_IS_EOS");\n } else {\n status = ap_get_brigade(r->input_filters, input_brigade,\n AP_MODE_READBYTES,\n APR_BLOCK_READ,\n maxsize - AJP_HEADER_SZ);\n if (status != APR_SUCCESS) {\n ap_log_rerror(APLOG_MARK, APLOG_DEBUG, status, r, APLOGNO(00880)\n "ap_get_brigade failed");\n output_failed = 1;\n break;\n }\n bufsiz = maxsize;\n status = apr_brigade_flatten(input_brigade, buff,\n &bufsiz);\n apr_brigade_cleanup(input_brigade);\n if (status != APR_SUCCESS) {\n ap_log_rerror(APLOG_MARK, APLOG_DEBUG, status, r, APLOGNO(00881)\n "apr_brigade_flatten failed");\n output_failed = 1;\n break;\n }\n }\n ajp_msg_reset(msg);\n status = ajp_send_data_msg(conn->sock, msg, bufsiz);\n ajp_msg_log(r, msg, "ajp_send_data_msg after CMD_AJP13_GET_BODY_CHUNK: ajp_ilink_send packet dump");\n if (status != APR_SUCCESS) {\n ap_log_rerror(APLOG_MARK, APLOG_DEBUG, status, r, APLOGNO(00882)\n "ajp_send_data_msg failed");\n backend_failed = 1;\n break;\n }\n conn->worker->s->transferred += bufsiz;\n } else {\n ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00883)\n "ap_proxy_ajp_request error read after end");\n backend_failed = 1;\n }\n break;\n case CMD_AJP13_SEND_HEADERS:\n if (headers_sent) {\n backend_failed = 1;\n ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00884)\n "Backend sent headers twice.");\n break;\n }\n status = ajp_parse_header(r, conf, conn->data);\n if (status != APR_SUCCESS) {\n backend_failed = 1;\n }\n else if ((r->status == 401) && conf->error_override) {\n const char *buf;\n const char *wa = "WWW-Authenticate";\n if ((buf = apr_table_get(r->headers_out, wa))) {\n apr_table_set(r->err_headers_out, wa, buf);\n } else {\n ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00885)\n "ap_proxy_ajp_request: origin server "\n "sent 401 without WWW-Authenticate header");\n }\n }\n headers_sent = 1;\n break;\n case CMD_AJP13_SEND_BODY_CHUNK:\n status = ajp_parse_data(r, conn->data, &size, &send_body_chunk_buff);\n if (status == APR_SUCCESS) {\n if (!conf->error_override || !ap_is_HTTP_ERROR(r->status)) {\n if (size == 0) {\n if (headers_sent) {\n e = apr_bucket_flush_create(r->connection->bucket_alloc);\n APR_BRIGADE_INSERT_TAIL(output_brigade, e);\n }\n else {\n ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00886)\n "Ignoring flush message "\n "received before headers");\n }\n }\n else {\n apr_status_t rv;\n if (conf->error_override && !ap_is_HTTP_ERROR(r->status)\n && ap_is_HTTP_ERROR(original_status)) {\n r->status = original_status;\n r->status_line = original_status_line;\n }\n e = apr_bucket_transient_create(send_body_chunk_buff, size,\n r->connection->bucket_alloc);\n APR_BRIGADE_INSERT_TAIL(output_brigade, e);\n if ((conn->worker->s->flush_packets == flush_on) ||\n ((conn->worker->s->flush_packets == flush_auto) &&\n ((rv = apr_poll(conn_poll, 1, &conn_poll_fd,\n conn->worker->s->flush_wait))\n != APR_SUCCESS) &&\n APR_STATUS_IS_TIMEUP(rv))) {\n e = apr_bucket_flush_create(r->connection->bucket_alloc);\n APR_BRIGADE_INSERT_TAIL(output_brigade, e);\n }\n apr_brigade_length(output_brigade, 0, &bb_len);\n if (bb_len != -1)\n conn->worker->s->read += bb_len;\n }\n if (headers_sent) {\n if (ap_pass_brigade(r->output_filters,\n output_brigade) != APR_SUCCESS) {\n ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00887)\n "error processing body.%s",\n r->connection->aborted ?\n " Client aborted connection." : "");\n output_failed = 1;\n }\n data_sent = 1;\n apr_brigade_cleanup(output_brigade);\n }\n }\n }\n else {\n backend_failed = 1;\n }\n break;\n case CMD_AJP13_END_RESPONSE:\n status = ajp_parse_reuse(r, conn->data, &conn_reuse);\n if (status != APR_SUCCESS) {\n backend_failed = 1;\n }\n if (!conf->error_override || !ap_is_HTTP_ERROR(r->status)) {\n e = apr_bucket_eos_create(r->connection->bucket_alloc);\n APR_BRIGADE_INSERT_TAIL(output_brigade, e);\n if (ap_pass_brigade(r->output_filters,\n output_brigade) != APR_SUCCESS) {\n ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00888)\n "error processing end");\n output_failed = 1;\n }\n data_sent = 1;\n }\n request_ended = 1;\n break;\n default:\n backend_failed = 1;\n break;\n }\n if (r->connection->aborted) {\n conn->close++;\n output_failed = 0;\n result = CMD_AJP13_END_RESPONSE;\n request_ended = 1;\n }\n if ((result == CMD_AJP13_END_RESPONSE) || backend_failed\n || output_failed)\n break;\n status = ajp_read_header(conn->sock, r, maxsize,\n (ajp_msg_t **)&(conn->data));\n if (status != APR_SUCCESS) {\n backend_failed = 1;\n ap_log_rerror(APLOG_MARK, APLOG_DEBUG, status, r, APLOGNO(00889)\n "ajp_read_header failed");\n break;\n }\n result = ajp_parse_type(r, conn->data);\n }\n apr_brigade_destroy(input_brigade);\n apr_brigade_cleanup(output_brigade);\n if (backend_failed || output_failed) {\n ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00890)\n "Processing of request failed backend: %i, "\n "output: %i", backend_failed, output_failed);\n conn->close++;\n if (data_sent) {\n rv = DONE;\n }\n }\n else if (!request_ended) {\n ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00891)\n "Processing of request didn\'t terminate cleanly");\n conn->close++;\n backend_failed = 1;\n if (data_sent) {\n rv = DONE;\n }\n }\n else if (!conn_reuse) {\n conn->close++;\n }\n else {\n ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00892)\n "got response from %pI (%s)",\n conn->worker->cp->addr,\n conn->worker->s->hostname);\n if (conf->error_override && ap_is_HTTP_ERROR(r->status)) {\n rv = r->status;\n r->status = HTTP_OK;\n }\n else {\n rv = OK;\n }\n }\n if (backend_failed) {\n ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(00893)\n "dialog to %pI (%s) failed",\n conn->worker->cp->addr,\n conn->worker->s->hostname);\n if (data_sent) {\n ap_proxy_backend_broke(r, output_brigade);\n } else if (!send_body && (is_idempotent(r) == METHOD_IDEMPOTENT)) {\n rv = HTTP_SERVICE_UNAVAILABLE;\n } else {\n rv = HTTP_INTERNAL_SERVER_ERROR;\n }\n }\n if (data_sent && !r->eos_sent && APR_BRIGADE_EMPTY(output_brigade)) {\n e = apr_bucket_eos_create(r->connection->bucket_alloc);\n APR_BRIGADE_INSERT_TAIL(output_brigade, e);\n }\n if (!APR_BRIGADE_EMPTY(output_brigade))\n ap_pass_brigade(r->output_filters, output_brigade);\n apr_brigade_destroy(output_brigade);\n if (apr_table_get(r->subprocess_env, "proxy-nokeepalive")) {\n conn->close++;\n }\n return rv;\n}'] |
35,459 | 0 | https://github.com/openssl/openssl/blob/f006217bb628d05a2d5b866ff252bd94e3477e1f/crypto/bn/bn_lib.c/#L351 | static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
BN_ULONG *A, *a = NULL;
const BN_ULONG *B;
int i;
bn_check_top(b);
if (words > (INT_MAX / (4 * BN_BITS2))) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
return NULL;
}
if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
return (NULL);
}
if (BN_get_flags(b,BN_FLG_SECURE))
a = A = OPENSSL_secure_malloc(words * sizeof(*a));
else
a = A = OPENSSL_malloc(words * sizeof(*a));
if (A == NULL) {
BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
return (NULL);
}
#ifdef PURIFY
memset(a, 0, sizeof(*a) * words);
#endif
#if 1
B = b->d;
if (B != NULL) {
for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {
BN_ULONG a0, a1, a2, a3;
a0 = B[0];
a1 = B[1];
a2 = B[2];
a3 = B[3];
A[0] = a0;
A[1] = a1;
A[2] = a2;
A[3] = a3;
}
switch (b->top & 3) {
case 3:
A[2] = B[2];
case 2:
A[1] = B[1];
case 1:
A[0] = B[0];
case 0:
;
}
}
#else
memset(A, 0, sizeof(*A) * words);
memcpy(A, b->d, sizeof(b->d[0]) * b->top);
#endif
return (a);
} | ['static DH *get_dh1024()\n{\n static unsigned char dh1024_p[] = {\n 0xF8, 0x81, 0x89, 0x7D, 0x14, 0x24, 0xC5, 0xD1, 0xE6, 0xF7, 0xBF,\n 0x3A,\n 0xE4, 0x90, 0xF4, 0xFC, 0x73, 0xFB, 0x34, 0xB5, 0xFA, 0x4C, 0x56,\n 0xA2,\n 0xEA, 0xA7, 0xE9, 0xC0, 0xC0, 0xCE, 0x89, 0xE1, 0xFA, 0x63, 0x3F,\n 0xB0,\n 0x6B, 0x32, 0x66, 0xF1, 0xD1, 0x7B, 0xB0, 0x00, 0x8F, 0xCA, 0x87,\n 0xC2,\n 0xAE, 0x98, 0x89, 0x26, 0x17, 0xC2, 0x05, 0xD2, 0xEC, 0x08, 0xD0,\n 0x8C,\n 0xFF, 0x17, 0x52, 0x8C, 0xC5, 0x07, 0x93, 0x03, 0xB1, 0xF6, 0x2F,\n 0xB8,\n 0x1C, 0x52, 0x47, 0x27, 0x1B, 0xDB, 0xD1, 0x8D, 0x9D, 0x69, 0x1D,\n 0x52,\n 0x4B, 0x32, 0x81, 0xAA, 0x7F, 0x00, 0xC8, 0xDC, 0xE6, 0xD9, 0xCC,\n 0xC1,\n 0x11, 0x2D, 0x37, 0x34, 0x6C, 0xEA, 0x02, 0x97, 0x4B, 0x0E, 0xBB,\n 0xB1,\n 0x71, 0x33, 0x09, 0x15, 0xFD, 0xDD, 0x23, 0x87, 0x07, 0x5E, 0x89,\n 0xAB,\n 0x6B, 0x7C, 0x5F, 0xEC, 0xA6, 0x24, 0xDC, 0x53,\n };\n static unsigned char dh1024_g[] = {\n 0x02,\n };\n DH *dh;\n if ((dh = DH_new()) == NULL)\n return (NULL);\n dh->p = BN_bin2bn(dh1024_p, sizeof(dh1024_p), NULL);\n dh->g = BN_bin2bn(dh1024_g, sizeof(dh1024_g), NULL);\n if ((dh->p == NULL) || (dh->g == NULL)) {\n DH_free(dh);\n return (NULL);\n }\n return (dh);\n}', 'BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)\n{\n unsigned int i, m;\n unsigned int n;\n BN_ULONG l;\n BIGNUM *bn = NULL;\n if (ret == NULL)\n ret = bn = BN_new();\n if (ret == NULL)\n return (NULL);\n bn_check_top(ret);\n for ( ; len > 0 && *s == 0; s++, len--)\n continue;\n n = len;\n if (n == 0) {\n ret->top = 0;\n return (ret);\n }\n i = ((n - 1) / BN_BYTES) + 1;\n m = ((n - 1) % (BN_BYTES));\n if (bn_wexpand(ret, (int)i) == NULL) {\n BN_free(bn);\n return NULL;\n }\n ret->top = i;\n ret->neg = 0;\n l = 0;\n while (n--) {\n l = (l << 8L) | *(s++);\n if (m-- == 0) {\n ret->d[--i] = l;\n l = 0;\n m = BN_BYTES - 1;\n }\n }\n bn_correct_top(ret);\n return (ret);\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *A, *a = NULL;\n const BN_ULONG *B;\n int i;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return (NULL);\n }\n if (BN_get_flags(b,BN_FLG_SECURE))\n a = A = OPENSSL_secure_malloc(words * sizeof(*a));\n else\n a = A = OPENSSL_malloc(words * sizeof(*a));\n if (A == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n#ifdef PURIFY\n memset(a, 0, sizeof(*a) * words);\n#endif\n#if 1\n B = b->d;\n if (B != NULL) {\n for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {\n BN_ULONG a0, a1, a2, a3;\n a0 = B[0];\n a1 = B[1];\n a2 = B[2];\n a3 = B[3];\n A[0] = a0;\n A[1] = a1;\n A[2] = a2;\n A[3] = a3;\n }\n switch (b->top & 3) {\n case 3:\n A[2] = B[2];\n case 2:\n A[1] = B[1];\n case 1:\n A[0] = B[0];\n case 0:\n ;\n }\n }\n#else\n memset(A, 0, sizeof(*A) * words);\n memcpy(A, b->d, sizeof(b->d[0]) * b->top);\n#endif\n return (a);\n}'] |
35,460 | 0 | https://github.com/openssl/openssl/blob/ea32151f7b9353f8906188d007c6893704ac17bb/crypto/lhash/lhash.c/#L164 | static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,
OPENSSL_LH_DOALL_FUNC func,
OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)
{
int i;
OPENSSL_LH_NODE *a, *n;
if (lh == NULL)
return;
for (i = lh->num_nodes - 1; i >= 0; i--) {
a = lh->b[i];
while (a != NULL) {
n = a->next;
if (use_arg)
func_arg(a->data, arg);
else
func(a->data);
a = n;
}
}
} | ['static int ssl_servername_cb(SSL *s, int *ad, void *arg)\n{\n tlsextctx *p = (tlsextctx *) arg;\n const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);\n if (servername && p->biodebug)\n BIO_printf(p->biodebug, "Hostname in TLS extension: \\"%s\\"\\n",\n servername);\n if (!p->servername)\n return SSL_TLSEXT_ERR_NOACK;\n if (servername) {\n if (strcasecmp(servername, p->servername))\n return p->extension_error;\n if (ctx2) {\n BIO_printf(p->biodebug, "Switching server context.\\n");\n SSL_set_SSL_CTX(s, ctx2);\n }\n }\n return SSL_TLSEXT_ERR_OK;\n}', 'SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx)\n{\n CERT *new_cert;\n if (ssl->ctx == ctx)\n return ssl->ctx;\n if (ctx == NULL)\n ctx = ssl->initial_ctx;\n new_cert = ssl_cert_dup(ctx->cert);\n if (new_cert == NULL) {\n return NULL;\n }\n ssl_cert_free(ssl->cert);\n ssl->cert = new_cert;\n OPENSSL_assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));\n if ((ssl->ctx != NULL) &&\n (ssl->sid_ctx_length == ssl->ctx->sid_ctx_length) &&\n (memcmp(ssl->sid_ctx, ssl->ctx->sid_ctx, ssl->sid_ctx_length) == 0)) {\n ssl->sid_ctx_length = ctx->sid_ctx_length;\n memcpy(&ssl->sid_ctx, &ctx->sid_ctx, sizeof(ssl->sid_ctx));\n }\n SSL_CTX_up_ref(ctx);\n SSL_CTX_free(ssl->ctx);\n ssl->ctx = ctx;\n return ssl->ctx;\n}', 'void SSL_CTX_free(SSL_CTX *a)\n{\n int i;\n if (a == NULL)\n return;\n CRYPTO_atomic_add(&a->references, -1, &i, a->lock);\n REF_PRINT_COUNT("SSL_CTX", a);\n if (i > 0)\n return;\n REF_ASSERT_ISNT(i < 0);\n X509_VERIFY_PARAM_free(a->param);\n dane_ctx_final(&a->dane);\n if (a->sessions != NULL)\n SSL_CTX_flush_sessions(a, 0);\n CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);\n lh_SSL_SESSION_free(a->sessions);\n X509_STORE_free(a->cert_store);\n#ifndef OPENSSL_NO_CT\n CTLOG_STORE_free(a->ctlog_store);\n#endif\n sk_SSL_CIPHER_free(a->cipher_list);\n sk_SSL_CIPHER_free(a->cipher_list_by_id);\n ssl_cert_free(a->cert);\n sk_X509_NAME_pop_free(a->client_CA, X509_NAME_free);\n sk_X509_pop_free(a->extra_certs, X509_free);\n a->comp_methods = NULL;\n#ifndef OPENSSL_NO_SRTP\n sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);\n#endif\n#ifndef OPENSSL_NO_SRP\n SSL_CTX_SRP_CTX_free(a);\n#endif\n#ifndef OPENSSL_NO_ENGINE\n ENGINE_finish(a->client_cert_engine);\n#endif\n#ifndef OPENSSL_NO_EC\n OPENSSL_free(a->tlsext_ecpointformatlist);\n OPENSSL_free(a->tlsext_ellipticcurvelist);\n#endif\n OPENSSL_free(a->alpn_client_proto_list);\n CRYPTO_THREAD_lock_free(a->lock);\n OPENSSL_free(a);\n}', 'void SSL_CTX_flush_sessions(SSL_CTX *s, long t)\n{\n unsigned long i;\n TIMEOUT_PARAM tp;\n tp.ctx = s;\n tp.cache = s->sessions;\n if (tp.cache == NULL)\n return;\n tp.time = t;\n CRYPTO_THREAD_write_lock(s->lock);\n i = lh_SSL_SESSION_get_down_load(s->sessions);\n lh_SSL_SESSION_set_down_load(s->sessions, 0);\n lh_SSL_SESSION_doall_TIMEOUT_PARAM(tp.cache, timeout_cb, &tp);\n lh_SSL_SESSION_set_down_load(s->sessions, i);\n CRYPTO_THREAD_unlock(s->lock);\n}', 'IMPLEMENT_LHASH_DOALL_ARG(SSL_SESSION, TIMEOUT_PARAM)', 'void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)\n{\n doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);\n}', 'static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,\n OPENSSL_LH_DOALL_FUNC func,\n OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)\n{\n int i;\n OPENSSL_LH_NODE *a, *n;\n if (lh == NULL)\n return;\n for (i = lh->num_nodes - 1; i >= 0; i--) {\n a = lh->b[i];\n while (a != NULL) {\n n = a->next;\n if (use_arg)\n func_arg(a->data, arg);\n else\n func(a->data);\n a = n;\n }\n }\n}'] |
35,461 | 0 | https://github.com/openssl/openssl/blob/14c6d27d63795ead1b70d97e3303731b433c0db8/crypto/bn/bn_ctx.c/#L111 | void BN_CTX_start(BN_CTX *ctx)
{
if (ctx->depth < BN_CTX_NUM_POS)
ctx->pos[ctx->depth] = ctx->tos;
ctx->depth++;
} | ['static int RSA_eay_mod_exp(BIGNUM *r0, BIGNUM *I, RSA *rsa)\n\t{\n\tconst RSA_METHOD *meth;\n\tBIGNUM r1,m1;\n\tint ret=0;\n\tBN_CTX *ctx;\n\tmeth = ENGINE_get_RSA(rsa->engine);\n\tif ((ctx=BN_CTX_new()) == NULL) goto err;\n\tBN_init(&m1);\n\tBN_init(&r1);\n\tif (rsa->flags & RSA_FLAG_CACHE_PRIVATE)\n\t\t{\n\t\tif (rsa->_method_mod_p == NULL)\n\t\t\t{\n\t\t\tif ((rsa->_method_mod_p=BN_MONT_CTX_new()) != NULL)\n\t\t\t\tif (!BN_MONT_CTX_set(rsa->_method_mod_p,rsa->p,\n\t\t\t\t\t\t ctx))\n\t\t\t\t\tgoto err;\n\t\t\t}\n\t\tif (rsa->_method_mod_q == NULL)\n\t\t\t{\n\t\t\tif ((rsa->_method_mod_q=BN_MONT_CTX_new()) != NULL)\n\t\t\t\tif (!BN_MONT_CTX_set(rsa->_method_mod_q,rsa->q,\n\t\t\t\t\t\t ctx))\n\t\t\t\t\tgoto err;\n\t\t\t}\n\t\t}\n\tif (!BN_mod(&r1,I,rsa->q,ctx)) goto err;\n\tif (!meth->bn_mod_exp(&m1,&r1,rsa->dmq1,rsa->q,ctx,\n\t\trsa->_method_mod_q)) goto err;\n\tif (!BN_mod(&r1,I,rsa->p,ctx)) goto err;\n\tif (!meth->bn_mod_exp(r0,&r1,rsa->dmp1,rsa->p,ctx,\n\t\trsa->_method_mod_p)) goto err;\n\tif (!BN_sub(r0,r0,&m1)) goto err;\n\tif (r0->neg)\n\t\tif (!BN_add(r0,r0,rsa->p)) goto err;\n\tif (!BN_mul(&r1,r0,rsa->iqmp,ctx)) goto err;\n\tif (!BN_mod(r0,&r1,rsa->p,ctx)) goto err;\n\tif (r0->neg)\n\t\tif (!BN_add(r0,r0,rsa->p)) goto err;\n\tif (!BN_mul(&r1,r0,rsa->q,ctx)) goto err;\n\tif (!BN_add(r0,&r1,&m1)) goto err;\n\tret=1;\nerr:\n\tBN_clear_free(&m1);\n\tBN_clear_free(&r1);\n\tBN_CTX_free(ctx);\n\treturn(ret);\n\t}', 'BN_CTX *BN_CTX_new(void)\n\t{\n\tBN_CTX *ret;\n\tret=(BN_CTX *)OPENSSL_malloc(sizeof(BN_CTX));\n\tif (ret == NULL)\n\t\t{\n\t\tBNerr(BN_F_BN_CTX_NEW,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tBN_CTX_init(ret);\n\tret->flags=BN_FLG_MALLOCED;\n\treturn(ret);\n\t}', 'void BN_CTX_init(BN_CTX *ctx)\n\t{\n\tint i;\n\tctx->tos = 0;\n\tctx->flags = 0;\n\tctx->depth = 0;\n\tctx->too_many = 0;\n\tfor (i = 0; i < BN_CTX_NUM; i++)\n\t\tBN_init(&(ctx->bn[i]));\n\t}', 'int BN_mod(BIGNUM *rem, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n\t{\n#if 0\n\tint i,nm,nd;\n\tBIGNUM *dv;\n\tif (BN_ucmp(m,d) < 0)\n\t\treturn((BN_copy(rem,m) == NULL)?0:1);\n\tBN_CTX_start(ctx);\n\tdv=BN_CTX_get(ctx);\n\tif (!BN_copy(rem,m)) goto err;\n\tnm=BN_num_bits(rem);\n\tnd=BN_num_bits(d);\n\tif (!BN_lshift(dv,d,nm-nd)) goto err;\n\tfor (i=nm-nd; i>=0; i--)\n\t\t{\n\t\tif (BN_cmp(rem,dv) >= 0)\n\t\t\t{\n\t\t\tif (!BN_sub(rem,rem,dv)) goto err;\n\t\t\t}\n\t\tif (!BN_rshift1(dv,dv)) goto err;\n\t\t}\n\tBN_CTX_end(ctx);\n\treturn(1);\n err:\n\tBN_CTX_end(ctx);\n\treturn(0);\n#else\n\treturn(BN_div(NULL,rem,m,d,ctx));\n#endif\n\t}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n\t BN_CTX *ctx)\n\t{\n\tint norm_shift,i,j,loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tint num_n,div_n;\n\tbn_check_top(num);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\n\t\t}\n\tif (BN_ucmp(num,divisor) < 0)\n\t\t{\n\t\tif (rm != NULL)\n\t\t\t{ if (BN_copy(rm,num) == NULL) return(0); }\n\t\tif (dv != NULL) BN_zero(dv);\n\t\treturn(1);\n\t\t}\n\tBN_CTX_start(ctx);\n\ttmp=BN_CTX_get(ctx);\n\ttmp->neg=0;\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 (res == NULL) goto err;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tBN_lshift(sdiv,divisor,norm_shift);\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tBN_lshift(snum,num,norm_shift);\n\tsnum->neg=0;\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\tBN_init(&wnum);\n\twnum.d=\t &(snum->d[loop]);\n\twnum.top= div_n;\n\twnum.dmax= snum->dmax+1;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (BN_ucmp(&wnum,sdiv) >= 0)\n\t\t{\n\t\tif (!BN_usub(&wnum,&wnum,sdiv)) goto err;\n\t\t*resp=1;\n\t\tres->d[res->top-1]=1;\n\t\t}\n\telse\n\t\tres->top--;\n\tresp--;\n\tfor (i=0; i<loop-1; i++)\n\t\t{\n\t\tBN_ULONG q,l0;\n#ifdef BN_DIV3W\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#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#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#ifdef 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\twnum.d--; wnum.top++;\n\t\ttmp->d[div_n]=l0;\n\t\tfor (j=div_n+1; j>0; j--)\n\t\t\tif (tmp->d[j-1]) break;\n\t\ttmp->top=j;\n\t\tj=wnum.top;\n\t\tBN_sub(&wnum,&wnum,tmp);\n\t\tsnum->top=snum->top+wnum.top-j;\n\t\tif (wnum.neg)\n\t\t\t{\n\t\t\tq--;\n\t\t\tj=wnum.top;\n\t\t\tBN_add(&wnum,&wnum,sdiv);\n\t\t\tsnum->top+=wnum.top-j;\n\t\t\t}\n\t\t*(resp--)=q;\n\t\twnump--;\n\t\t}\n\tif (rm != NULL)\n\t\t{\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\trm->neg=num->neg;\n\t\t}\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)\n\t{\n\tint top,al,bl;\n\tBIGNUM *rr;\n\tint ret = 0;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\tint i;\n#endif\n#ifdef BN_RECURSION\n\tBIGNUM *t;\n\tint j,k;\n#endif\n#ifdef BN_COUNT\n\tprintf("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 && !BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tbn_wexpand(b,al);\n\t\t\tb->d[bl]=0;\n\t\t\tbl++;\n\t\t\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\tbn_wexpand(a,bl);\n\t\t\ta->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\tbn_wexpand(t,k*2);\n\t\t\t\tbn_wexpand(rr,k*2);\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\tbn_wexpand(a,k);\n\t\t\t\tbn_wexpand(b,k);\n\t\t\t\tbn_wexpand(t,k*4);\n\t\t\t\tbn_wexpand(rr,k*4);\n\t\t\t\tfor (i=a->top; i<k; i++)\n\t\t\t\t\ta->d[i]=0;\n\t\t\t\tfor (i=b->top; i<k; i++)\n\t\t\t\t\tb->d[i]=0;\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\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_fix_top(rr);\n\tif (r != rr) BN_copy(r,rr);\n\tret=1;\nerr:\n\tBN_CTX_end(ctx);\n\treturn(ret);\n\t}', 'void BN_CTX_start(BN_CTX *ctx)\n\t{\n\tif (ctx->depth < BN_CTX_NUM_POS)\n\t\tctx->pos[ctx->depth] = ctx->tos;\n\tctx->depth++;\n\t}'] |
35,462 | 0 | https://github.com/nginx/nginx/blob/bcd78e22e97d4c870b5104d0c540caaa972176ed/src/http/ngx_http_core_module.c/#L2281 | ngx_int_t
ngx_http_internal_redirect(ngx_http_request_t *r,
ngx_str_t *uri, ngx_str_t *args)
{
ngx_http_core_srv_conf_t *cscf;
r->uri_changes--;
if (r->uri_changes == 0) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"rewrite or internal redirection cycle "
"while internal redirect to \"%V\"", uri);
r->main->count++;
ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
return NGX_DONE;
}
r->uri = *uri;
if (args) {
r->args = *args;
} else {
ngx_str_null(&r->args);
}
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"internal redirect: \"%V?%V\"", uri, &r->args);
ngx_http_set_exten(r);
ngx_memzero(r->ctx, sizeof(void *) * ngx_http_max_module);
cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
r->loc_conf = cscf->ctx->loc_conf;
ngx_http_update_location_config(r);
#if (NGX_HTTP_CACHE)
r->cache = NULL;
#endif
r->internal = 1;
r->add_uri_to_alias = 0;
r->main->count++;
ngx_http_handler(r);
return NGX_DONE;
} | ['static void\nngx_http_upstream_init_request(ngx_http_request_t *r)\n{\n ngx_str_t *host;\n ngx_uint_t i;\n ngx_resolver_ctx_t *ctx, temp;\n ngx_http_cleanup_t *cln;\n ngx_http_upstream_t *u;\n ngx_http_core_loc_conf_t *clcf;\n ngx_http_upstream_srv_conf_t *uscf, **uscfp;\n ngx_http_upstream_main_conf_t *umcf;\n if (r->aio) {\n return;\n }\n u = r->upstream;\n#if (NGX_HTTP_CACHE)\n if (u->conf->cache) {\n ngx_int_t rc;\n rc = ngx_http_upstream_cache(r, u);\n if (rc == NGX_BUSY) {\n r->write_event_handler = ngx_http_upstream_init_request;\n return;\n }\n r->write_event_handler = ngx_http_request_empty_handler;\n if (rc == NGX_DONE) {\n return;\n }\n if (rc != NGX_DECLINED) {\n ngx_http_finalize_request(r, rc);\n return;\n }\n }\n#endif\n u->store = (u->conf->store || u->conf->store_lengths);\n if (!u->store && !r->post_action && !u->conf->ignore_client_abort) {\n r->read_event_handler = ngx_http_upstream_rd_check_broken_connection;\n r->write_event_handler = ngx_http_upstream_wr_check_broken_connection;\n }\n if (r->request_body) {\n u->request_bufs = r->request_body->bufs;\n }\n if (u->create_request(r) != NGX_OK) {\n ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n u->peer.local = u->conf->local;\n clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);\n u->output.alignment = clcf->directio_alignment;\n u->output.pool = r->pool;\n u->output.bufs.num = 1;\n u->output.bufs.size = clcf->client_body_buffer_size;\n u->output.output_filter = ngx_chain_writer;\n u->output.filter_ctx = &u->writer;\n u->writer.pool = r->pool;\n if (r->upstream_states == NULL) {\n r->upstream_states = ngx_array_create(r->pool, 1,\n sizeof(ngx_http_upstream_state_t));\n if (r->upstream_states == NULL) {\n ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n } else {\n u->state = ngx_array_push(r->upstream_states);\n if (u->state == NULL) {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n ngx_memzero(u->state, sizeof(ngx_http_upstream_state_t));\n }\n cln = ngx_http_cleanup_add(r, 0);\n if (cln == NULL) {\n ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n cln->handler = ngx_http_upstream_cleanup;\n cln->data = r;\n u->cleanup = &cln->handler;\n if (u->resolved == NULL) {\n uscf = u->conf->upstream;\n } else {\n if (u->resolved->sockaddr) {\n if (ngx_http_upstream_create_round_robin_peer(r, u->resolved)\n != NGX_OK)\n {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n ngx_http_upstream_connect(r, u);\n return;\n }\n host = &u->resolved->host;\n umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);\n uscfp = umcf->upstreams.elts;\n for (i = 0; i < umcf->upstreams.nelts; i++) {\n uscf = uscfp[i];\n if (uscf->host.len == host->len\n && ((uscf->port == 0 && u->resolved->no_port)\n || uscf->port == u->resolved->port)\n && ngx_memcmp(uscf->host.data, host->data, host->len) == 0)\n {\n goto found;\n }\n }\n temp.name = *host;\n ctx = ngx_resolve_start(clcf->resolver, &temp);\n if (ctx == NULL) {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n if (ctx == NGX_NO_RESOLVER) {\n ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,\n "no resolver defined to resolve %V", host);\n ngx_http_upstream_finalize_request(r, u, NGX_HTTP_BAD_GATEWAY);\n return;\n }\n ctx->name = *host;\n ctx->type = NGX_RESOLVE_A;\n ctx->handler = ngx_http_upstream_resolve_handler;\n ctx->data = r;\n ctx->timeout = clcf->resolver_timeout;\n u->resolved->ctx = ctx;\n if (ngx_resolve_name(ctx) != NGX_OK) {\n u->resolved->ctx = NULL;\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n return;\n }\nfound:\n if (uscf->peer.init(r, uscf) != NGX_OK) {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n ngx_http_upstream_connect(r, u);\n}', 'static void\nngx_http_upstream_connect(ngx_http_request_t *r, ngx_http_upstream_t *u)\n{\n ngx_int_t rc;\n ngx_time_t *tp;\n ngx_connection_t *c;\n r->connection->log->action = "connecting to upstream";\n r->connection->single_connection = 0;\n if (u->state && u->state->response_sec) {\n tp = ngx_timeofday();\n u->state->response_sec = tp->sec - u->state->response_sec;\n u->state->response_msec = tp->msec - u->state->response_msec;\n }\n u->state = ngx_array_push(r->upstream_states);\n if (u->state == NULL) {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n ngx_memzero(u->state, sizeof(ngx_http_upstream_state_t));\n tp = ngx_timeofday();\n u->state->response_sec = tp->sec;\n u->state->response_msec = tp->msec;\n rc = ngx_event_connect_peer(&u->peer);\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "http upstream connect: %i", rc);\n if (rc == NGX_ERROR) {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n u->state->peer = u->peer.name;\n if (rc == NGX_BUSY) {\n ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "no live upstreams");\n ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_NOLIVE);\n return;\n }\n if (rc == NGX_DECLINED) {\n ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);\n return;\n }\n c = u->peer.connection;\n c->data = r;\n c->write->handler = ngx_http_upstream_handler;\n c->read->handler = ngx_http_upstream_handler;\n u->write_event_handler = ngx_http_upstream_send_request_handler;\n u->read_event_handler = ngx_http_upstream_process_header;\n c->sendfile &= r->connection->sendfile;\n u->output.sendfile = c->sendfile;\n c->pool = r->pool;\n c->log = r->connection->log;\n c->read->log = c->log;\n c->write->log = c->log;\n u->writer.out = NULL;\n u->writer.last = &u->writer.out;\n u->writer.connection = c;\n u->writer.limit = 0;\n if (u->request_sent) {\n if (ngx_http_upstream_reinit(r, u) != NGX_OK) {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n }\n if (r->request_body\n && r->request_body->buf\n && r->request_body->temp_file\n && r == r->main)\n {\n u->output.free = ngx_alloc_chain_link(r->pool);\n if (u->output.free == NULL) {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n u->output.free->buf = r->request_body->buf;\n u->output.free->next = NULL;\n u->output.allocated = 1;\n r->request_body->buf->pos = r->request_body->buf->start;\n r->request_body->buf->last = r->request_body->buf->start;\n r->request_body->buf->tag = u->output.tag;\n }\n u->request_sent = 0;\n if (rc == NGX_AGAIN) {\n ngx_add_timer(c->write, u->conf->connect_timeout);\n return;\n }\n#if (NGX_HTTP_SSL)\n if (u->ssl && c->ssl == NULL) {\n ngx_http_upstream_ssl_init_connection(r, u, c);\n return;\n }\n#endif\n ngx_http_upstream_send_request(r, u);\n}', 'static void\nngx_http_upstream_send_request(ngx_http_request_t *r, ngx_http_upstream_t *u)\n{\n ngx_int_t rc;\n ngx_connection_t *c;\n c = u->peer.connection;\n ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,\n "http upstream send request");\n if (!u->request_sent && ngx_http_upstream_test_connect(c) != NGX_OK) {\n ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);\n return;\n }\n c->log->action = "sending request to upstream";\n rc = ngx_output_chain(&u->output, u->request_sent ? NULL : u->request_bufs);\n u->request_sent = 1;\n if (rc == NGX_ERROR) {\n ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);\n return;\n }\n if (c->write->timer_set) {\n ngx_del_timer(c->write);\n }\n if (rc == NGX_AGAIN) {\n ngx_add_timer(c->write, u->conf->send_timeout);\n if (ngx_handle_write_event(c->write, u->conf->send_lowat) != NGX_OK) {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n return;\n }\n if (c->tcp_nopush == NGX_TCP_NOPUSH_SET) {\n if (ngx_tcp_push(c->fd) == NGX_ERROR) {\n ngx_log_error(NGX_LOG_CRIT, c->log, ngx_socket_errno,\n ngx_tcp_push_n " failed");\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n c->tcp_nopush = NGX_TCP_NOPUSH_UNSET;\n }\n ngx_add_timer(c->read, u->conf->read_timeout);\n#if 1\n if (c->read->ready) {\n ngx_http_upstream_process_header(r, u);\n return;\n }\n#endif\n u->write_event_handler = ngx_http_upstream_dummy_handler;\n if (ngx_handle_write_event(c->write, 0) != NGX_OK) {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n}', 'static void\nngx_http_upstream_process_header(ngx_http_request_t *r, ngx_http_upstream_t *u)\n{\n ssize_t n;\n ngx_int_t rc;\n ngx_connection_t *c;\n c = u->peer.connection;\n ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,\n "http upstream process header");\n c->log->action = "reading response header from upstream";\n if (c->read->timedout) {\n ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_TIMEOUT);\n return;\n }\n if (!u->request_sent && ngx_http_upstream_test_connect(c) != NGX_OK) {\n ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);\n return;\n }\n if (u->buffer.start == NULL) {\n u->buffer.start = ngx_palloc(r->pool, u->conf->buffer_size);\n if (u->buffer.start == NULL) {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n u->buffer.pos = u->buffer.start;\n u->buffer.last = u->buffer.start;\n u->buffer.end = u->buffer.start + u->conf->buffer_size;\n u->buffer.temporary = 1;\n u->buffer.tag = u->output.tag;\n if (ngx_list_init(&u->headers_in.headers, r->pool, 8,\n sizeof(ngx_table_elt_t))\n != NGX_OK)\n {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n#if (NGX_HTTP_CACHE)\n if (r->cache) {\n u->buffer.pos += r->cache->header_start;\n u->buffer.last = u->buffer.pos;\n }\n#endif\n }\n for ( ;; ) {\n n = c->recv(c, u->buffer.last, u->buffer.end - u->buffer.last);\n if (n == NGX_AGAIN) {\n#if 0\n ngx_add_timer(rev, u->read_timeout);\n#endif\n if (ngx_handle_read_event(c->read, 0) != NGX_OK) {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n return;\n }\n if (n == 0) {\n ngx_log_error(NGX_LOG_ERR, c->log, 0,\n "upstream prematurely closed connection");\n }\n if (n == NGX_ERROR || n == 0) {\n ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);\n return;\n }\n u->buffer.last += n;\n#if 0\n u->valid_header_in = 0;\n u->peer.cached = 0;\n#endif\n rc = u->process_header(r);\n if (rc == NGX_AGAIN) {\n if (u->buffer.pos == u->buffer.end) {\n ngx_log_error(NGX_LOG_ERR, c->log, 0,\n "upstream sent too big header");\n ngx_http_upstream_next(r, u,\n NGX_HTTP_UPSTREAM_FT_INVALID_HEADER);\n return;\n }\n continue;\n }\n break;\n }\n if (rc == NGX_HTTP_UPSTREAM_INVALID_HEADER) {\n ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_INVALID_HEADER);\n return;\n }\n if (rc == NGX_ERROR) {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n if (u->headers_in.status_n > NGX_HTTP_SPECIAL_RESPONSE) {\n if (r->subrequest_in_memory) {\n u->buffer.last = u->buffer.pos;\n }\n if (ngx_http_upstream_test_next(r, u) == NGX_OK) {\n return;\n }\n if (ngx_http_upstream_intercept_errors(r, u) == NGX_OK) {\n return;\n }\n }\n if (ngx_http_upstream_process_headers(r, u) != NGX_OK) {\n return;\n }\n if (!r->subrequest_in_memory) {\n ngx_http_upstream_send_response(r, u);\n return;\n }\n if (u->input_filter == NULL) {\n u->input_filter_init = ngx_http_upstream_non_buffered_filter_init;\n u->input_filter = ngx_http_upstream_non_buffered_filter;\n u->input_filter_ctx = r;\n }\n if (u->input_filter_init(u->input_filter_ctx) == NGX_ERROR) {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n n = u->buffer.last - u->buffer.pos;\n if (n) {\n u->buffer.last -= n;\n u->state->response_length += n;\n if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) {\n ngx_http_upstream_finalize_request(r, u, NGX_ERROR);\n return;\n }\n if (u->length == 0) {\n ngx_http_upstream_finalize_request(r, u, 0);\n return;\n }\n }\n u->read_event_handler = ngx_http_upstream_process_body_in_memory;\n ngx_http_upstream_process_body_in_memory(r, u);\n}', 'static ngx_int_t\nngx_http_upstream_test_next(ngx_http_request_t *r, ngx_http_upstream_t *u)\n{\n ngx_uint_t status;\n ngx_http_upstream_next_t *un;\n status = u->headers_in.status_n;\n for (un = ngx_http_upstream_next_errors; un->status; un++) {\n if (status != un->status) {\n continue;\n }\n if (u->peer.tries > 1 && (u->conf->next_upstream & un->mask)) {\n ngx_http_upstream_next(r, u, un->mask);\n return NGX_OK;\n }\n#if (NGX_HTTP_CACHE)\n if (u->cache_status == NGX_HTTP_CACHE_EXPIRED\n && (u->conf->cache_use_stale & un->mask))\n {\n ngx_int_t rc;\n rc = u->reinit_request(r);\n if (rc == NGX_OK) {\n u->cache_status = NGX_HTTP_CACHE_STALE;\n rc = ngx_http_upstream_cache_send(r, u);\n }\n ngx_http_upstream_finalize_request(r, u, rc);\n return NGX_OK;\n }\n#endif\n }\n return NGX_DECLINED;\n}', 'static void\nngx_http_upstream_next(ngx_http_request_t *r, ngx_http_upstream_t *u,\n ngx_uint_t ft_type)\n{\n ngx_uint_t status, state;\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "http next upstream, %xi", ft_type);\n#if 0\n ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock);\n#endif\n if (ft_type == NGX_HTTP_UPSTREAM_FT_HTTP_404) {\n state = NGX_PEER_NEXT;\n } else {\n state = NGX_PEER_FAILED;\n }\n if (ft_type != NGX_HTTP_UPSTREAM_FT_NOLIVE) {\n u->peer.free(&u->peer, u->peer.data, state);\n }\n if (ft_type == NGX_HTTP_UPSTREAM_FT_TIMEOUT) {\n ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_ETIMEDOUT,\n "upstream timed out");\n }\n if (u->peer.cached && ft_type == NGX_HTTP_UPSTREAM_FT_ERROR) {\n status = 0;\n } else {\n switch(ft_type) {\n case NGX_HTTP_UPSTREAM_FT_TIMEOUT:\n status = NGX_HTTP_GATEWAY_TIME_OUT;\n break;\n case NGX_HTTP_UPSTREAM_FT_HTTP_500:\n status = NGX_HTTP_INTERNAL_SERVER_ERROR;\n break;\n case NGX_HTTP_UPSTREAM_FT_HTTP_404:\n status = NGX_HTTP_NOT_FOUND;\n break;\n default:\n status = NGX_HTTP_BAD_GATEWAY;\n }\n }\n if (r->connection->error) {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_CLIENT_CLOSED_REQUEST);\n return;\n }\n if (status) {\n u->state->status = status;\n if (u->peer.tries == 0 || !(u->conf->next_upstream & ft_type)) {\n#if (NGX_HTTP_CACHE)\n if (u->cache_status == NGX_HTTP_CACHE_EXPIRED\n && (u->conf->cache_use_stale & ft_type))\n {\n ngx_int_t rc;\n rc = u->reinit_request(r);\n if (rc == NGX_OK) {\n u->cache_status = NGX_HTTP_CACHE_STALE;\n rc = ngx_http_upstream_cache_send(r, u);\n }\n ngx_http_upstream_finalize_request(r, u, rc);\n return;\n }\n#endif\n ngx_http_upstream_finalize_request(r, u, status);\n return;\n }\n }\n if (u->peer.connection) {\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "close http upstream connection: %d",\n u->peer.connection->fd);\n#if (NGX_HTTP_SSL)\n if (u->peer.connection->ssl) {\n u->peer.connection->ssl->no_wait_shutdown = 1;\n u->peer.connection->ssl->no_send_shutdown = 1;\n (void) ngx_ssl_shutdown(u->peer.connection);\n }\n#endif\n ngx_close_connection(u->peer.connection);\n }\n#if 0\n if (u->conf->busy_lock && !u->busy_locked) {\n ngx_http_upstream_busy_lock(p);\n return;\n }\n#endif\n ngx_http_upstream_connect(r, u);\n}', 'static void\nngx_http_upstream_finalize_request(ngx_http_request_t *r,\n ngx_http_upstream_t *u, ngx_int_t rc)\n{\n ngx_time_t *tp;\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "finalize http upstream request: %i", rc);\n if (u->cleanup) {\n *u->cleanup = NULL;\n u->cleanup = NULL;\n }\n if (u->resolved && u->resolved->ctx) {\n ngx_resolve_name_done(u->resolved->ctx);\n u->resolved->ctx = NULL;\n }\n if (u->state && u->state->response_sec) {\n tp = ngx_timeofday();\n u->state->response_sec = tp->sec - u->state->response_sec;\n u->state->response_msec = tp->msec - u->state->response_msec;\n if (u->pipe) {\n u->state->response_length = u->pipe->read_length;\n }\n }\n u->finalize_request(r, rc);\n if (u->peer.free) {\n u->peer.free(&u->peer, u->peer.data, 0);\n }\n if (u->peer.connection) {\n#if (NGX_HTTP_SSL)\n if (u->peer.connection->ssl) {\n u->peer.connection->ssl->no_wait_shutdown = 1;\n (void) ngx_ssl_shutdown(u->peer.connection);\n }\n#endif\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "close http upstream connection: %d",\n u->peer.connection->fd);\n ngx_close_connection(u->peer.connection);\n }\n u->peer.connection = NULL;\n if (u->pipe && u->pipe->temp_file) {\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "http upstream temp fd: %d",\n u->pipe->temp_file->file.fd);\n }\n#if (NGX_HTTP_CACHE)\n if (u->cacheable && r->cache) {\n time_t valid;\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "http upstream cache fd: %d",\n r->cache->file.fd);\n if (rc == NGX_HTTP_BAD_GATEWAY || rc == NGX_HTTP_GATEWAY_TIME_OUT) {\n valid = ngx_http_file_cache_valid(u->conf->cache_valid, rc);\n if (valid) {\n r->cache->valid_sec = ngx_time() + valid;\n r->cache->error = rc;\n }\n }\n ngx_http_file_cache_free(r, u->pipe->temp_file);\n }\n#endif\n if (u->header_sent\n && (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE))\n {\n rc = 0;\n }\n if (rc == NGX_DECLINED) {\n return;\n }\n r->connection->log->action = "sending to client";\n if (rc == 0) {\n rc = ngx_http_send_special(r, NGX_HTTP_LAST);\n }\n ngx_http_finalize_request(r, rc);\n}', 'void\nngx_http_finalize_request(ngx_http_request_t *r, ngx_int_t rc)\n{\n ngx_connection_t *c;\n ngx_http_request_t *pr;\n ngx_http_core_loc_conf_t *clcf;\n c = r->connection;\n ngx_log_debug5(NGX_LOG_DEBUG_HTTP, c->log, 0,\n "http finalize request: %d, \\"%V?%V\\" a:%d, c:%d",\n rc, &r->uri, &r->args, r == c->data, r->main->count);\n if (rc == NGX_DONE) {\n ngx_http_finalize_connection(r);\n return;\n }\n if (rc == NGX_OK && r->filter_finalize) {\n c->error = 1;\n return;\n }\n if (rc == NGX_DECLINED) {\n r->content_handler = NULL;\n r->write_event_handler = ngx_http_core_run_phases;\n ngx_http_core_run_phases(r);\n return;\n }\n if (r != r->main && r->post_subrequest) {\n rc = r->post_subrequest->handler(r, r->post_subrequest->data, rc);\n }\n if (rc == NGX_ERROR\n || rc == NGX_HTTP_REQUEST_TIME_OUT\n || rc == NGX_HTTP_CLIENT_CLOSED_REQUEST\n || c->error)\n {\n if (ngx_http_post_action(r) == NGX_OK) {\n return;\n }\n if (r->main->blocked) {\n r->write_event_handler = ngx_http_request_finalizer;\n }\n ngx_http_terminate_request(r, rc);\n return;\n }\n if (rc >= NGX_HTTP_SPECIAL_RESPONSE\n || rc == NGX_HTTP_CREATED\n || rc == NGX_HTTP_NO_CONTENT)\n {\n if (rc == NGX_HTTP_CLOSE) {\n ngx_http_terminate_request(r, rc);\n return;\n }\n if (r == r->main) {\n if (c->read->timer_set) {\n ngx_del_timer(c->read);\n }\n if (c->write->timer_set) {\n ngx_del_timer(c->write);\n }\n }\n c->read->handler = ngx_http_request_handler;\n c->write->handler = ngx_http_request_handler;\n ngx_http_finalize_request(r, ngx_http_special_response_handler(r, rc));\n return;\n }\n if (r != r->main) {\n if (r->buffered || r->postponed) {\n if (ngx_http_set_write_handler(r) != NGX_OK) {\n ngx_http_terminate_request(r, 0);\n }\n return;\n }\n#if (NGX_DEBUG)\n if (r != c->data) {\n ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,\n "http finalize non-active request: \\"%V?%V\\"",\n &r->uri, &r->args);\n }\n#endif\n pr = r->parent;\n if (r == c->data) {\n r->main->count--;\n if (!r->logged) {\n clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);\n if (clcf->log_subrequest) {\n ngx_http_log_request(r);\n }\n r->logged = 1;\n } else {\n ngx_log_error(NGX_LOG_ALERT, c->log, 0,\n "subrequest: \\"%V?%V\\" logged again",\n &r->uri, &r->args);\n }\n r->done = 1;\n if (pr->postponed && pr->postponed->request == r) {\n pr->postponed = pr->postponed->next;\n }\n c->data = pr;\n } else {\n r->write_event_handler = ngx_http_request_finalizer;\n if (r->waited) {\n r->done = 1;\n }\n }\n if (ngx_http_post_request(pr, NULL) != NGX_OK) {\n r->main->count++;\n ngx_http_terminate_request(r, 0);\n return;\n }\n ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,\n "http wake parent request: \\"%V?%V\\"",\n &pr->uri, &pr->args);\n return;\n }\n if (r->buffered || c->buffered || r->postponed || r->blocked) {\n if (ngx_http_set_write_handler(r) != NGX_OK) {\n ngx_http_terminate_request(r, 0);\n }\n return;\n }\n if (r != c->data) {\n ngx_log_error(NGX_LOG_ALERT, c->log, 0,\n "http finalize non-active request: \\"%V?%V\\"",\n &r->uri, &r->args);\n return;\n }\n r->done = 1;\n r->write_event_handler = ngx_http_request_empty_handler;\n if (!r->post_action) {\n r->request_complete = 1;\n }\n if (ngx_http_post_action(r) == NGX_OK) {\n return;\n }\n if (c->read->timer_set) {\n ngx_del_timer(c->read);\n }\n if (c->write->timer_set) {\n c->write->delayed = 0;\n ngx_del_timer(c->write);\n }\n if (c->read->eof) {\n ngx_http_close_request(r, 0);\n return;\n }\n ngx_http_finalize_connection(r);\n}', 'static ngx_int_t\nngx_http_post_action(ngx_http_request_t *r)\n{\n ngx_http_core_loc_conf_t *clcf;\n clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);\n if (clcf->post_action.data == NULL) {\n return NGX_DECLINED;\n }\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "post action: \\"%V\\"", &clcf->post_action);\n r->main->count--;\n r->http_version = NGX_HTTP_VERSION_9;\n r->header_only = 1;\n r->post_action = 1;\n r->read_event_handler = ngx_http_block_reading;\n if (clcf->post_action.data[0] == \'/\') {\n ngx_http_internal_redirect(r, &clcf->post_action, NULL);\n } else {\n ngx_http_named_location(r, &clcf->post_action);\n }\n return NGX_OK;\n}', 'ngx_int_t\nngx_http_internal_redirect(ngx_http_request_t *r,\n ngx_str_t *uri, ngx_str_t *args)\n{\n ngx_http_core_srv_conf_t *cscf;\n r->uri_changes--;\n if (r->uri_changes == 0) {\n ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,\n "rewrite or internal redirection cycle "\n "while internal redirect to \\"%V\\"", uri);\n r->main->count++;\n ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);\n return NGX_DONE;\n }\n r->uri = *uri;\n if (args) {\n r->args = *args;\n } else {\n ngx_str_null(&r->args);\n }\n ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "internal redirect: \\"%V?%V\\"", uri, &r->args);\n ngx_http_set_exten(r);\n ngx_memzero(r->ctx, sizeof(void *) * ngx_http_max_module);\n cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);\n r->loc_conf = cscf->ctx->loc_conf;\n ngx_http_update_location_config(r);\n#if (NGX_HTTP_CACHE)\n r->cache = NULL;\n#endif\n r->internal = 1;\n r->add_uri_to_alias = 0;\n r->main->count++;\n ngx_http_handler(r);\n return NGX_DONE;\n}'] |
35,463 | 0 | https://github.com/libav/libav/blob/493f54ada083b4d6c8f14f02607224fe258c211c/ffmpeg.c/#L3415 | static void opt_output_file(const char *filename)
{
AVFormatContext *oc;
int use_video, use_audio, use_subtitle;
int input_has_video, input_has_audio, input_has_subtitle;
AVFormatParameters params, *ap = ¶ms;
AVOutputFormat *file_oformat;
if (!strcmp(filename, "-"))
filename = "pipe:";
oc = avformat_alloc_context();
if (!oc) {
print_error(filename, AVERROR(ENOMEM));
av_exit(1);
}
if (last_asked_format) {
file_oformat = av_guess_format(last_asked_format, NULL, NULL);
if (!file_oformat) {
fprintf(stderr, "Requested output format '%s' is not a suitable output format\n", last_asked_format);
av_exit(1);
}
last_asked_format = NULL;
} else {
file_oformat = av_guess_format(NULL, filename, NULL);
if (!file_oformat) {
fprintf(stderr, "Unable to find a suitable output format for '%s'\n",
filename);
av_exit(1);
}
}
oc->oformat = file_oformat;
av_strlcpy(oc->filename, filename, sizeof(oc->filename));
if (!strcmp(file_oformat->name, "ffm") &&
av_strstart(filename, "http:", NULL)) {
int err = read_ffserver_streams(oc, filename);
if (err < 0) {
print_error(filename, err);
av_exit(1);
}
} else {
use_video = file_oformat->video_codec != CODEC_ID_NONE || video_stream_copy || video_codec_name;
use_audio = file_oformat->audio_codec != CODEC_ID_NONE || audio_stream_copy || audio_codec_name;
use_subtitle = file_oformat->subtitle_codec != CODEC_ID_NONE || subtitle_stream_copy || subtitle_codec_name;
if (nb_input_files > 0) {
check_audio_video_sub_inputs(&input_has_video, &input_has_audio,
&input_has_subtitle);
if (!input_has_video)
use_video = 0;
if (!input_has_audio)
use_audio = 0;
if (!input_has_subtitle)
use_subtitle = 0;
}
if (audio_disable) {
use_audio = 0;
}
if (video_disable) {
use_video = 0;
}
if (subtitle_disable) {
use_subtitle = 0;
}
if (use_video) {
new_video_stream(oc);
}
if (use_audio) {
new_audio_stream(oc);
}
if (use_subtitle) {
new_subtitle_stream(oc);
}
oc->timestamp = rec_timestamp;
for(; metadata_count>0; metadata_count--){
av_metadata_set(&oc->metadata, metadata[metadata_count-1].key,
metadata[metadata_count-1].value);
}
av_metadata_conv(oc, oc->oformat->metadata_conv, NULL);
}
output_files[nb_output_files++] = oc;
if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
if (!av_filename_number_test(oc->filename)) {
print_error(oc->filename, AVERROR_NUMEXPECTED);
av_exit(1);
}
}
if (!(oc->oformat->flags & AVFMT_NOFILE)) {
if (!file_overwrite &&
(strchr(filename, ':') == NULL ||
filename[1] == ':' ||
av_strstart(filename, "file:", NULL))) {
if (url_exist(filename)) {
if (!using_stdin) {
fprintf(stderr,"File '%s' already exists. Overwrite ? [y/N] ", filename);
fflush(stderr);
if (!read_yesno()) {
fprintf(stderr, "Not overwriting - exiting\n");
av_exit(1);
}
}
else {
fprintf(stderr,"File '%s' already exists. Exiting.\n", filename);
av_exit(1);
}
}
}
if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
fprintf(stderr, "Could not open '%s'\n", filename);
av_exit(1);
}
}
memset(ap, 0, sizeof(*ap));
if (av_set_parameters(oc, ap) < 0) {
fprintf(stderr, "%s: Invalid encoding parameters\n",
oc->filename);
av_exit(1);
}
oc->preload= (int)(mux_preload*AV_TIME_BASE);
oc->max_delay= (int)(mux_max_delay*AV_TIME_BASE);
oc->loop_output = loop_output;
oc->flags |= AVFMT_FLAG_NONBLOCK;
set_context_opts(oc, avformat_opts, AV_OPT_FLAG_ENCODING_PARAM);
} | ['static void opt_output_file(const char *filename)\n{\n AVFormatContext *oc;\n int use_video, use_audio, use_subtitle;\n int input_has_video, input_has_audio, input_has_subtitle;\n AVFormatParameters params, *ap = ¶ms;\n AVOutputFormat *file_oformat;\n if (!strcmp(filename, "-"))\n filename = "pipe:";\n oc = avformat_alloc_context();\n if (!oc) {\n print_error(filename, AVERROR(ENOMEM));\n av_exit(1);\n }\n if (last_asked_format) {\n file_oformat = av_guess_format(last_asked_format, NULL, NULL);\n if (!file_oformat) {\n fprintf(stderr, "Requested output format \'%s\' is not a suitable output format\\n", last_asked_format);\n av_exit(1);\n }\n last_asked_format = NULL;\n } else {\n file_oformat = av_guess_format(NULL, filename, NULL);\n if (!file_oformat) {\n fprintf(stderr, "Unable to find a suitable output format for \'%s\'\\n",\n filename);\n av_exit(1);\n }\n }\n oc->oformat = file_oformat;\n av_strlcpy(oc->filename, filename, sizeof(oc->filename));\n if (!strcmp(file_oformat->name, "ffm") &&\n av_strstart(filename, "http:", NULL)) {\n int err = read_ffserver_streams(oc, filename);\n if (err < 0) {\n print_error(filename, err);\n av_exit(1);\n }\n } else {\n use_video = file_oformat->video_codec != CODEC_ID_NONE || video_stream_copy || video_codec_name;\n use_audio = file_oformat->audio_codec != CODEC_ID_NONE || audio_stream_copy || audio_codec_name;\n use_subtitle = file_oformat->subtitle_codec != CODEC_ID_NONE || subtitle_stream_copy || subtitle_codec_name;\n if (nb_input_files > 0) {\n check_audio_video_sub_inputs(&input_has_video, &input_has_audio,\n &input_has_subtitle);\n if (!input_has_video)\n use_video = 0;\n if (!input_has_audio)\n use_audio = 0;\n if (!input_has_subtitle)\n use_subtitle = 0;\n }\n if (audio_disable) {\n use_audio = 0;\n }\n if (video_disable) {\n use_video = 0;\n }\n if (subtitle_disable) {\n use_subtitle = 0;\n }\n if (use_video) {\n new_video_stream(oc);\n }\n if (use_audio) {\n new_audio_stream(oc);\n }\n if (use_subtitle) {\n new_subtitle_stream(oc);\n }\n oc->timestamp = rec_timestamp;\n for(; metadata_count>0; metadata_count--){\n av_metadata_set(&oc->metadata, metadata[metadata_count-1].key,\n metadata[metadata_count-1].value);\n }\n av_metadata_conv(oc, oc->oformat->metadata_conv, NULL);\n }\n output_files[nb_output_files++] = oc;\n if (oc->oformat->flags & AVFMT_NEEDNUMBER) {\n if (!av_filename_number_test(oc->filename)) {\n print_error(oc->filename, AVERROR_NUMEXPECTED);\n av_exit(1);\n }\n }\n if (!(oc->oformat->flags & AVFMT_NOFILE)) {\n if (!file_overwrite &&\n (strchr(filename, \':\') == NULL ||\n filename[1] == \':\' ||\n av_strstart(filename, "file:", NULL))) {\n if (url_exist(filename)) {\n if (!using_stdin) {\n fprintf(stderr,"File \'%s\' already exists. Overwrite ? [y/N] ", filename);\n fflush(stderr);\n if (!read_yesno()) {\n fprintf(stderr, "Not overwriting - exiting\\n");\n av_exit(1);\n }\n }\n else {\n fprintf(stderr,"File \'%s\' already exists. Exiting.\\n", filename);\n av_exit(1);\n }\n }\n }\n if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {\n fprintf(stderr, "Could not open \'%s\'\\n", filename);\n av_exit(1);\n }\n }\n memset(ap, 0, sizeof(*ap));\n if (av_set_parameters(oc, ap) < 0) {\n fprintf(stderr, "%s: Invalid encoding parameters\\n",\n oc->filename);\n av_exit(1);\n }\n oc->preload= (int)(mux_preload*AV_TIME_BASE);\n oc->max_delay= (int)(mux_max_delay*AV_TIME_BASE);\n oc->loop_output = loop_output;\n oc->flags |= AVFMT_FLAG_NONBLOCK;\n set_context_opts(oc, avformat_opts, AV_OPT_FLAG_ENCODING_PARAM);\n}', 'AVFormatContext *avformat_alloc_context(void)\n{\n AVFormatContext *ic;\n ic = av_malloc(sizeof(AVFormatContext));\n if (!ic) return ic;\n avformat_get_context_defaults(ic);\n ic->av_class = &av_format_context_class;\n return ic;\n}', 'void *av_malloc(unsigned int size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-16) )\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size+16);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&15) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr,16,size))\n ptr = NULL;\n#elif HAVE_MEMALIGN\n ptr = memalign(16,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}', 'size_t av_strlcpy(char *dst, const char *src, size_t size)\n{\n size_t len = 0;\n while (++len < size && *src)\n *dst++ = *src++;\n if (len <= size)\n *dst = 0;\n return len + strlen(src) - 1;\n}'] |
35,464 | 0 | https://github.com/openssl/openssl/blob/e02c519cd32a55e6ad39a0cfbeeda775f9115f28/crypto/bn/bn_ctx.c/#L276 | static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
} | ['int ecdh_simple_compute_key(unsigned char **pout, size_t *poutlen,\n const EC_POINT *pub_key, const EC_KEY *ecdh)\n{\n BN_CTX *ctx;\n EC_POINT *tmp = NULL;\n BIGNUM *x = NULL;\n const BIGNUM *priv_key;\n const EC_GROUP *group;\n int ret = 0;\n size_t buflen, len;\n unsigned char *buf = NULL;\n if ((ctx = BN_CTX_new()) == NULL)\n goto err;\n BN_CTX_start(ctx);\n x = BN_CTX_get(ctx);\n if (x == NULL) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n priv_key = EC_KEY_get0_private_key(ecdh);\n if (priv_key == NULL) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_NO_PRIVATE_VALUE);\n goto err;\n }\n group = EC_KEY_get0_group(ecdh);\n if (EC_KEY_get_flags(ecdh) & EC_FLAG_COFACTOR_ECDH) {\n if (!EC_GROUP_get_cofactor(group, x, NULL) ||\n !BN_mul(x, x, priv_key, ctx)) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n priv_key = x;\n }\n if ((tmp = EC_POINT_new(group)) == NULL) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (!EC_POINT_mul(group, tmp, NULL, pub_key, priv_key, ctx)) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_POINT_ARITHMETIC_FAILURE);\n goto err;\n }\n if (!EC_POINT_get_affine_coordinates(group, tmp, x, NULL, ctx)) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_POINT_ARITHMETIC_FAILURE);\n goto err;\n }\n buflen = (EC_GROUP_get_degree(group) + 7) / 8;\n len = BN_num_bytes(x);\n if (len > buflen) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if ((buf = OPENSSL_malloc(buflen)) == NULL) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n memset(buf, 0, buflen - len);\n if (len != (size_t)BN_bn2bin(x, buf + buflen - len)) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_BN_LIB);\n goto err;\n }\n *pout = buf;\n *poutlen = buflen;\n buf = NULL;\n ret = 1;\n err:\n EC_POINT_free(tmp);\n if (ctx)\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n OPENSSL_free(buf);\n return ret;\n}', '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_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = bn_mul_fixed_top(r, a, b, ctx);\n bn_correct_top(r);\n bn_check_top(r);\n return ret;\n}', 'int bn_mul_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = 0;\n int top, al, bl;\n BIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n int i;\n#endif\n#ifdef BN_RECURSION\n BIGNUM *t = NULL;\n int j = 0, k;\n#endif\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(r);\n al = a->top;\n bl = b->top;\n if ((al == 0) || (bl == 0)) {\n BN_zero(r);\n return 1;\n }\n top = al + bl;\n BN_CTX_start(ctx);\n if ((r == a) || (r == b)) {\n if ((rr = BN_CTX_get(ctx)) == NULL)\n goto err;\n } else\n rr = r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n i = al - bl;\n#endif\n#ifdef BN_MUL_COMBA\n if (i == 0) {\n# if 0\n if (al == 4) {\n if (bn_wexpand(rr, 8) == NULL)\n goto err;\n rr->top = 8;\n bn_mul_comba4(rr->d, a->d, b->d);\n goto end;\n }\n# endif\n if (al == 8) {\n if (bn_wexpand(rr, 16) == NULL)\n goto err;\n rr->top = 16;\n bn_mul_comba8(rr->d, a->d, b->d);\n goto end;\n }\n }\n#endif\n#ifdef BN_RECURSION\n if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {\n if (i >= -1 && i <= 1) {\n if (i >= 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n }\n if (i == -1) {\n j = BN_num_bits_word((BN_ULONG)bl);\n }\n j = 1 << (j - 1);\n assert(j <= al || j <= bl);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (al > j || bl > j) {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d,\n j, al - j, bl - j, t->d);\n } else {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);\n }\n rr->top = top;\n goto end;\n }\n }\n#endif\n if (bn_wexpand(rr, top) == NULL)\n goto err;\n rr->top = top;\n bn_mul_normal(rr->d, a->d, al, b->d, bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n end:\n#endif\n rr->neg = a->neg ^ b->neg;\n rr->flags |= BN_FLG_FIXED_TOP;\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(r);\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}'] |
35,465 | 0 | https://github.com/openssl/openssl/blob/8b0d4242404f9e5da26e7594fa0864b2df4601af/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);
a->top = b->top;
a->neg = b->neg;
bn_check_top(a);
return a;
} | ['make_dh(1024_160)', '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 a->top = b->top;\n a->neg = b->neg;\n bn_check_top(a);\n return a;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}'] |
35,466 | 0 | https://gitlab.com/libtiff/libtiff/blob/848ff19ce2ccd8a6eacf2939c2532b3929a1cf55/tools/tiff2pdf.c/#L1902 | void t2p_read_tiff_size(T2P* t2p, TIFF* input){
uint64* sbc=NULL;
#if defined(JPEG_SUPPORT) || defined (OJPEG_SUPPORT)
unsigned char* jpt=NULL;
tstrip_t i=0;
tstrip_t stripcount=0;
#endif
uint64 k = 0;
if(t2p->pdf_transcode == T2P_TRANSCODE_RAW){
#ifdef CCITT_SUPPORT
if(t2p->pdf_compression == T2P_COMPRESS_G4 ){
TIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc);
t2p->tiff_datasize=(tmsize_t)sbc[0];
return;
}
#endif
#ifdef ZIP_SUPPORT
if(t2p->pdf_compression == T2P_COMPRESS_ZIP){
TIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc);
t2p->tiff_datasize=(tmsize_t)sbc[0];
return;
}
#endif
#ifdef OJPEG_SUPPORT
if(t2p->tiff_compression == COMPRESSION_OJPEG){
if(!TIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc)){
TIFFError(TIFF2PDF_MODULE,
"Input file %s missing field: TIFFTAG_STRIPBYTECOUNTS",
TIFFFileName(input));
t2p->t2p_error = T2P_ERR_ERROR;
return;
}
stripcount=TIFFNumberOfStrips(input);
for(i=0;i<stripcount;i++){
k = checkAdd64(k, sbc[i], t2p);
}
if(TIFFGetField(input, TIFFTAG_JPEGIFOFFSET, &(t2p->tiff_dataoffset))){
if(t2p->tiff_dataoffset != 0){
if(TIFFGetField(input, TIFFTAG_JPEGIFBYTECOUNT, &(t2p->tiff_datasize))!=0){
if((uint64)t2p->tiff_datasize < k) {
TIFFWarning(TIFF2PDF_MODULE,
"Input file %s has short JPEG interchange file byte count",
TIFFFileName(input));
t2p->pdf_ojpegiflength=t2p->tiff_datasize;
k = checkAdd64(k, t2p->tiff_datasize, t2p);
k = checkAdd64(k, 6, t2p);
k = checkAdd64(k, stripcount, t2p);
k = checkAdd64(k, stripcount, t2p);
t2p->tiff_datasize = (tsize_t) k;
if ((uint64) t2p->tiff_datasize != k) {
TIFFError(TIFF2PDF_MODULE, "Integer overflow");
t2p->t2p_error = T2P_ERR_ERROR;
}
return;
}
return;
}else {
TIFFError(TIFF2PDF_MODULE,
"Input file %s missing field: TIFFTAG_JPEGIFBYTECOUNT",
TIFFFileName(input));
t2p->t2p_error = T2P_ERR_ERROR;
return;
}
}
}
k = checkAdd64(k, stripcount, t2p);
k = checkAdd64(k, stripcount, t2p);
k = checkAdd64(k, 2048, t2p);
t2p->tiff_datasize = (tsize_t) k;
if ((uint64) t2p->tiff_datasize != k) {
TIFFError(TIFF2PDF_MODULE, "Integer overflow");
t2p->t2p_error = T2P_ERR_ERROR;
}
return;
}
#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 += count;
k -= 2;
}
} else {
k = 2;
}
stripcount=TIFFNumberOfStrips(input);
if(!TIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc)){
TIFFError(TIFF2PDF_MODULE,
"Input file %s missing field: TIFFTAG_STRIPBYTECOUNTS",
TIFFFileName(input));
t2p->t2p_error = T2P_ERR_ERROR;
return;
}
for(i=0;i<stripcount;i++){
k = checkAdd64(k, sbc[i], t2p);
k -=2;
k +=2;
}
k = checkAdd64(k, 2, t2p);
k = checkAdd64(k, 6, t2p);
t2p->tiff_datasize = (tsize_t) k;
if ((uint64) t2p->tiff_datasize != k) {
TIFFError(TIFF2PDF_MODULE, "Integer overflow");
t2p->t2p_error = T2P_ERR_ERROR;
}
return;
}
#endif
(void) 0;
}
k = checkMultiply64(TIFFScanlineSize(input), t2p->tiff_length, t2p);
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(T2P* t2p, TIFF* input){\n\tuint64* sbc=NULL;\n#if defined(JPEG_SUPPORT) || defined (OJPEG_SUPPORT)\n\tunsigned char* jpt=NULL;\n\ttstrip_t i=0;\n\ttstrip_t stripcount=0;\n#endif\n uint64 k = 0;\n\tif(t2p->pdf_transcode == T2P_TRANSCODE_RAW){\n#ifdef CCITT_SUPPORT\n\t\tif(t2p->pdf_compression == T2P_COMPRESS_G4 ){\n\t\t\tTIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc);\n\t\t\tt2p->tiff_datasize=(tmsize_t)sbc[0];\n\t\t\treturn;\n\t\t}\n#endif\n#ifdef ZIP_SUPPORT\n\t\tif(t2p->pdf_compression == T2P_COMPRESS_ZIP){\n\t\t\tTIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc);\n\t\t\tt2p->tiff_datasize=(tmsize_t)sbc[0];\n\t\t\treturn;\n\t\t}\n#endif\n#ifdef OJPEG_SUPPORT\n\t\tif(t2p->tiff_compression == COMPRESSION_OJPEG){\n\t\t\tif(!TIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc)){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Input file %s missing field: TIFFTAG_STRIPBYTECOUNTS",\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tstripcount=TIFFNumberOfStrips(input);\n\t\t\tfor(i=0;i<stripcount;i++){\n\t\t\t\tk = checkAdd64(k, sbc[i], t2p);\n\t\t\t}\n\t\t\tif(TIFFGetField(input, TIFFTAG_JPEGIFOFFSET, &(t2p->tiff_dataoffset))){\n\t\t\t\tif(t2p->tiff_dataoffset != 0){\n\t\t\t\t\tif(TIFFGetField(input, TIFFTAG_JPEGIFBYTECOUNT, &(t2p->tiff_datasize))!=0){\n\t\t\t\t\t\tif((uint64)t2p->tiff_datasize < k) {\n\t\t\t\t\t\t\tTIFFWarning(TIFF2PDF_MODULE,\n\t\t\t\t\t\t\t\t"Input file %s has short JPEG interchange file byte count",\n\t\t\t\t\t\t\t\tTIFFFileName(input));\n\t\t\t\t\t\t\tt2p->pdf_ojpegiflength=t2p->tiff_datasize;\n\t\t\t\t\t\t\tk = checkAdd64(k, t2p->tiff_datasize, t2p);\n\t\t\t\t\t\t\tk = checkAdd64(k, 6, t2p);\n\t\t\t\t\t\t\tk = checkAdd64(k, stripcount, t2p);\n\t\t\t\t\t\t\tk = checkAdd64(k, stripcount, t2p);\n\t\t\t\t\t\t\tt2p->tiff_datasize = (tsize_t) k;\n\t\t\t\t\t\t\tif ((uint64) t2p->tiff_datasize != k) {\n\t\t\t\t\t\t\t\tTIFFError(TIFF2PDF_MODULE, "Integer overflow");\n\t\t\t\t\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}else {\n\t\t\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t\t\t"Input file %s missing field: TIFFTAG_JPEGIFBYTECOUNT",\n\t\t\t\t\t\t\tTIFFFileName(input));\n\t\t\t\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tk = checkAdd64(k, stripcount, t2p);\n\t\t\tk = checkAdd64(k, stripcount, t2p);\n\t\t\tk = checkAdd64(k, 2048, t2p);\n\t\t\tt2p->tiff_datasize = (tsize_t) k;\n\t\t\tif ((uint64) t2p->tiff_datasize != k) {\n\t\t\t\tTIFFError(TIFF2PDF_MODULE, "Integer overflow");\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t}\n\t\t\treturn;\n\t\t}\n#endif\n#ifdef JPEG_SUPPORT\n\t\tif(t2p->tiff_compression == COMPRESSION_JPEG) {\n\t\t\tuint32 count = 0;\n\t\t\tif(TIFFGetField(input, TIFFTAG_JPEGTABLES, &count, &jpt) != 0 ){\n\t\t\t\tif(count > 4){\n\t\t\t\t\tk += count;\n\t\t\t\t\tk -= 2;\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tk = 2;\n\t\t\t}\n\t\t\tstripcount=TIFFNumberOfStrips(input);\n\t\t\tif(!TIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc)){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Input file %s missing field: TIFFTAG_STRIPBYTECOUNTS",\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tfor(i=0;i<stripcount;i++){\n\t\t\t\tk = checkAdd64(k, sbc[i], t2p);\n\t\t\t\tk -=2;\n\t\t\t\tk +=2;\n\t\t\t}\n\t\t\tk = checkAdd64(k, 2, t2p);\n\t\t\tk = checkAdd64(k, 6, t2p);\n\t\t\tt2p->tiff_datasize = (tsize_t) k;\n\t\t\tif ((uint64) t2p->tiff_datasize != k) {\n\t\t\t\tTIFFError(TIFF2PDF_MODULE, "Integer overflow");\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t}\n\t\t\treturn;\n\t\t}\n#endif\n\t\t(void) 0;\n\t}\n\tk = checkMultiply64(TIFFScanlineSize(input), t2p->tiff_length, t2p);\n\tif(t2p->tiff_planar==PLANARCONFIG_SEPARATE){\n\t\tk = checkMultiply64(k, t2p->tiff_samplesperpixel, t2p);\n\t}\n\tif (k == 0) {\n\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t}\n\tt2p->tiff_datasize = (tsize_t) k;\n\tif ((uint64) t2p->tiff_datasize != k) {\n\t\tTIFFError(TIFF2PDF_MODULE, "Integer overflow");\n\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t}\n\treturn;\n}', '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}'] |
35,467 | 0 | https://github.com/libav/libav/blob/01fdfa51aca9086e04bd354fe3f103a49352c085/avconv_opt.c/#L1902 | static int opt_vstats(void *optctx, const char *opt, const char *arg)
{
char filename[40];
time_t today2 = time(NULL);
struct tm *today = localtime(&today2);
snprintf(filename, sizeof(filename), "vstats_%02d%02d%02d.log", today->tm_hour, today->tm_min,
today->tm_sec);
return opt_vstats_file(NULL, opt, filename);
} | ['static int opt_vstats(void *optctx, const char *opt, const char *arg)\n{\n char filename[40];\n time_t today2 = time(NULL);\n struct tm *today = localtime(&today2);\n snprintf(filename, sizeof(filename), "vstats_%02d%02d%02d.log", today->tm_hour, today->tm_min,\n today->tm_sec);\n return opt_vstats_file(NULL, opt, filename);\n}'] |
35,468 | 0 | https://github.com/openssl/openssl/blob/ed371b8cbac0d0349667558c061c1ae380cf75eb/crypto/bn/bn_lib.c/#L291 | BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
{
bn_check_top(b);
if (a == b)
return a;
if (bn_wexpand(a, b->top) == NULL)
return NULL;
if (b->top > 0)
memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);
a->neg = b->neg;
a->top = b->top;
a->flags |= b->flags & BN_FLG_FIXED_TOP;
bn_check_top(a);
return a;
} | ['BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)\n{\n BIGNUM *ret = in;\n int err = 1;\n int r;\n BIGNUM *A, *b, *q, *t, *x, *y;\n int e, i, j;\n if (!BN_is_odd(p) || BN_abs_is_word(p, 1)) {\n if (BN_abs_is_word(p, 2)) {\n if (ret == NULL)\n ret = BN_new();\n if (ret == NULL)\n goto end;\n if (!BN_set_word(ret, BN_is_bit_set(a, 0))) {\n if (ret != in)\n BN_free(ret);\n return NULL;\n }\n bn_check_top(ret);\n return ret;\n }\n BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);\n return NULL;\n }\n if (BN_is_zero(a) || BN_is_one(a)) {\n if (ret == NULL)\n ret = BN_new();\n if (ret == NULL)\n goto end;\n if (!BN_set_word(ret, BN_is_one(a))) {\n if (ret != in)\n BN_free(ret);\n return NULL;\n }\n bn_check_top(ret);\n return ret;\n }\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n b = BN_CTX_get(ctx);\n q = BN_CTX_get(ctx);\n t = BN_CTX_get(ctx);\n x = BN_CTX_get(ctx);\n y = BN_CTX_get(ctx);\n if (y == NULL)\n goto end;\n if (ret == NULL)\n ret = BN_new();\n if (ret == NULL)\n goto end;\n if (!BN_nnmod(A, a, p, ctx))\n goto end;\n e = 1;\n while (!BN_is_bit_set(p, e))\n e++;\n if (e == 1) {\n if (!BN_rshift(q, p, 2))\n goto end;\n q->neg = 0;\n if (!BN_add_word(q, 1))\n goto end;\n if (!BN_mod_exp(ret, A, q, p, ctx))\n goto end;\n err = 0;\n goto vrfy;\n }\n if (e == 2) {\n if (!BN_mod_lshift1_quick(t, A, p))\n goto end;\n if (!BN_rshift(q, p, 3))\n goto end;\n q->neg = 0;\n if (!BN_mod_exp(b, t, q, p, ctx))\n goto end;\n if (!BN_mod_sqr(y, b, p, ctx))\n goto end;\n if (!BN_mod_mul(t, t, y, p, ctx))\n goto end;\n if (!BN_sub_word(t, 1))\n goto end;\n if (!BN_mod_mul(x, A, b, p, ctx))\n goto end;\n if (!BN_mod_mul(x, x, t, p, ctx))\n goto end;\n if (!BN_copy(ret, x))\n goto end;\n err = 0;\n goto vrfy;\n }\n if (!BN_copy(q, p))\n goto end;\n q->neg = 0;\n i = 2;\n do {\n if (i < 22) {\n if (!BN_set_word(y, i))\n goto end;\n } else {\n if (!BN_priv_rand(y, BN_num_bits(p), 0, 0))\n goto end;\n if (BN_ucmp(y, p) >= 0) {\n if (!(p->neg ? BN_add : BN_sub) (y, y, p))\n goto end;\n }\n if (BN_is_zero(y))\n if (!BN_set_word(y, i))\n goto end;\n }\n r = BN_kronecker(y, q, ctx);\n if (r < -1)\n goto end;\n if (r == 0) {\n BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);\n goto end;\n }\n }\n while (r == 1 && ++i < 82);\n if (r != -1) {\n BNerr(BN_F_BN_MOD_SQRT, BN_R_TOO_MANY_ITERATIONS);\n goto end;\n }\n if (!BN_rshift(q, q, e))\n goto end;\n if (!BN_mod_exp(y, y, q, p, ctx))\n goto end;\n if (BN_is_one(y)) {\n BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);\n goto end;\n }\n if (!BN_rshift1(t, q))\n goto end;\n if (BN_is_zero(t)) {\n if (!BN_nnmod(t, A, p, ctx))\n goto end;\n if (BN_is_zero(t)) {\n BN_zero(ret);\n err = 0;\n goto end;\n } else if (!BN_one(x))\n goto end;\n } else {\n if (!BN_mod_exp(x, A, t, p, ctx))\n goto end;\n if (BN_is_zero(x)) {\n BN_zero(ret);\n err = 0;\n goto end;\n }\n }\n if (!BN_mod_sqr(b, x, p, ctx))\n goto end;\n if (!BN_mod_mul(b, b, A, p, ctx))\n goto end;\n if (!BN_mod_mul(x, x, A, p, ctx))\n goto end;\n while (1) {\n if (BN_is_one(b)) {\n if (!BN_copy(ret, x))\n goto end;\n err = 0;\n goto vrfy;\n }\n i = 1;\n if (!BN_mod_sqr(t, b, p, ctx))\n goto end;\n while (!BN_is_one(t)) {\n i++;\n if (i == e) {\n BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE);\n goto end;\n }\n if (!BN_mod_mul(t, t, t, p, ctx))\n goto end;\n }\n if (!BN_copy(t, y))\n goto end;\n for (j = e - i - 1; j > 0; j--) {\n if (!BN_mod_sqr(t, t, p, ctx))\n goto end;\n }\n if (!BN_mod_mul(y, t, t, p, ctx))\n goto end;\n if (!BN_mod_mul(x, x, t, p, ctx))\n goto end;\n if (!BN_mod_mul(b, b, y, p, ctx))\n goto end;\n e = i;\n }\n vrfy:\n if (!err) {\n if (!BN_mod_sqr(x, ret, p, ctx))\n err = 1;\n if (!err && 0 != BN_cmp(x, A)) {\n BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE);\n err = 1;\n }\n }\n end:\n if (err) {\n if (ret != in)\n BN_clear_free(ret);\n ret = NULL;\n }\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return ret;\n}', 'int BN_is_odd(const BIGNUM *a)\n{\n return (a->top > 0) && (a->d[0] & 1);\n}', 'int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w)\n{\n return ((a->top == 1) && (a->d[0] == w)) || ((w == 0) && (a->top == 0));\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int ret;\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return 0;\n }\n if (divisor->d[divisor->top - 1] == 0) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n ret = bn_div_fixed_top(dv, rm, num, divisor, ctx);\n if (ret) {\n if (dv != NULL)\n bn_correct_top(dv);\n if (rm != NULL)\n bn_correct_top(rm);\n }\n return ret;\n}', 'int BN_is_zero(const BIGNUM *a)\n{\n return a->top == 0;\n}', 'int BN_is_bit_set(const BIGNUM *a, int n)\n{\n int i, j;\n bn_check_top(a);\n if (n < 0)\n return 0;\n i = n / BN_BITS2;\n j = n % BN_BITS2;\n if (a->top <= i)\n return 0;\n return (int)(((a->d[i]) >> j) & ((BN_ULONG)1));\n}', 'int BN_mod_lshift1_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *m)\n{\n if (!BN_lshift1(r, a))\n return 0;\n bn_check_top(r);\n if (BN_cmp(r, m) >= 0)\n return BN_sub(r, r, m);\n return 1;\n}', 'int BN_rshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, j, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l, tmp;\n bn_check_top(r);\n bn_check_top(a);\n if (n < 0) {\n BNerr(BN_F_BN_RSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n nw = n / BN_BITS2;\n rb = n % BN_BITS2;\n lb = BN_BITS2 - rb;\n if (nw >= a->top || a->top == 0) {\n BN_zero(r);\n return 1;\n }\n i = (BN_num_bits(a) - n + (BN_BITS2 - 1)) / BN_BITS2;\n if (r != a) {\n if (bn_wexpand(r, i) == NULL)\n return 0;\n r->neg = a->neg;\n } else {\n if (n == 0)\n return 1;\n }\n f = &(a->d[nw]);\n t = r->d;\n j = a->top - nw;\n r->top = i;\n if (rb == 0) {\n for (i = j; i != 0; i--)\n *(t++) = *(f++);\n } else {\n l = *(f++);\n for (i = j - 1; i != 0; i--) {\n tmp = (l >> rb) & BN_MASK2;\n l = *(f++);\n *(t++) = (tmp | (l << lb)) & BN_MASK2;\n }\n if ((l = (l >> rb) & BN_MASK2))\n *(t) = l;\n }\n if (!r->top)\n r->neg = 0;\n bn_check_top(r);\n return 1;\n}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n BN_CTX *ctx)\n{\n int ret;\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n if (BN_is_odd(m)) {\n# ifdef MONT_EXP_WORD\n if (a->top == 1 && !a->neg\n && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(a, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(m, BN_FLG_CONSTTIME) == 0)) {\n BN_ULONG A = a->d[0];\n ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);\n } else\n# endif\n ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);\n } else\n#endif\n#ifdef RECP_MUL_MOD\n {\n ret = BN_mod_exp_recp(r, a, p, m, ctx);\n }\n#else\n {\n ret = BN_mod_exp_simple(r, a, p, m, ctx);\n }\n#endif\n bn_check_top(r);\n return ret;\n}', 'int BN_mod_exp_mont_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_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx)\n{\n if (!BN_sqr(r, a, ctx))\n return 0;\n return BN_mod(r, r, m, ctx);\n}', 'int bn_div_fixed_top(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,\n const BIGNUM *divisor, BN_CTX *ctx)\n{\n int norm_shift, i, j, loop;\n BIGNUM *tmp, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnum, *wnumtop;\n BN_ULONG d0, d1;\n int num_n, div_n;\n assert(divisor->top > 0 && divisor->d[divisor->top - 1] != 0);\n bn_check_top(num);\n bn_check_top(divisor);\n bn_check_top(dv);\n bn_check_top(rm);\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n if (!BN_copy(sdiv, divisor))\n goto err;\n norm_shift = bn_left_align(sdiv);\n sdiv->neg = 0;\n if (!(bn_lshift_fixed_top(snum, num, norm_shift)))\n goto err;\n div_n = sdiv->top;\n num_n = snum->top;\n if (num_n <= div_n) {\n if (bn_wexpand(snum, div_n + 1) == NULL)\n goto err;\n memset(&(snum->d[num_n]), 0, (div_n - num_n + 1) * sizeof(BN_ULONG));\n snum->top = num_n = div_n + 1;\n }\n loop = num_n - div_n;\n wnum = &(snum->d[loop]);\n wnumtop = &(snum->d[num_n - 1]);\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n if (!bn_wexpand(res, loop))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop;\n res->flags |= BN_FLG_FIXED_TOP;\n resp = &(res->d[loop]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n for (i = 0; i < loop; i++, wnumtop--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W)\n q = bn_div_3_words(wnumtop, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnumtop[0];\n n1 = wnumtop[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n BN_ULONG n2 = (wnumtop == wnum) ? 0 : wnumtop[-2];\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | n2))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= n2)))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum--;\n l0 = bn_sub_words(wnum, wnum, tmp->d, div_n + 1);\n q -= l0;\n for (l0 = 0 - l0, j = 0; j < div_n; j++)\n tmp->d[j] = sdiv->d[j] & l0;\n l0 = bn_add_words(wnum, wnum, tmp->d, div_n);\n (*wnumtop) += l0;\n assert((*wnumtop) == 0);\n *--resp = q;\n }\n snum->neg = num->neg;\n snum->top = div_n;\n snum->flags |= BN_FLG_FIXED_TOP;\n if (rm != NULL)\n bn_rshift_fixed_top(rm, snum, norm_shift);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return 0;\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n bn_check_top(b);\n if (a == b)\n return a;\n if (bn_wexpand(a, b->top) == NULL)\n return NULL;\n if (b->top > 0)\n memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n a->neg = b->neg;\n a->top = b->top;\n a->flags |= b->flags & BN_FLG_FIXED_TOP;\n bn_check_top(a);\n return a;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}'] |
35,469 | 0 | https://github.com/libav/libav/blob/fc417db3f162d5269c0d22f8e467da4afa67c20a/libavfilter/formats.c/#L285 | void avfilter_formats_unref(AVFilterFormats **ref)
{
FORMATS_UNREF(ref, formats);
} | ['static int pick_formats(AVFilterGraph *graph)\n{\n int i, j, ret;\n for (i = 0; i < graph->filter_count; i++) {\n AVFilterContext *filter = graph->filters[i];\n for (j = 0; j < filter->input_count; j++)\n if ((ret = pick_format(filter->inputs[j])) < 0)\n return ret;\n for (j = 0; j < filter->output_count; j++)\n if ((ret = pick_format(filter->outputs[j])) < 0)\n return ret;\n }\n return 0;\n}', 'static int pick_format(AVFilterLink *link)\n{\n if (!link || !link->in_formats)\n return 0;\n link->in_formats->format_count = 1;\n link->format = link->in_formats->formats[0];\n if (link->type == AVMEDIA_TYPE_AUDIO) {\n if (!link->in_samplerates->format_count) {\n av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"\n " the link between filters %s and %s.\\n", link->src->name,\n link->dst->name);\n return AVERROR(EINVAL);\n }\n link->in_samplerates->format_count = 1;\n link->sample_rate = link->in_samplerates->formats[0];\n if (!link->in_channel_layouts->nb_channel_layouts) {\n av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"\n "the link between filters %s and %s.\\n", link->src->name,\n link->dst->name);\n return AVERROR(EINVAL);\n }\n link->in_channel_layouts->nb_channel_layouts = 1;\n link->channel_layout = link->in_channel_layouts->channel_layouts[0];\n }\n avfilter_formats_unref(&link->in_formats);\n avfilter_formats_unref(&link->out_formats);\n avfilter_formats_unref(&link->in_samplerates);\n avfilter_formats_unref(&link->out_samplerates);\n ff_channel_layouts_unref(&link->in_channel_layouts);\n ff_channel_layouts_unref(&link->out_channel_layouts);\n return 0;\n}', 'void avfilter_formats_unref(AVFilterFormats **ref)\n{\n FORMATS_UNREF(ref, formats);\n}'] |
35,470 | 0 | https://github.com/openssl/openssl/blob/5dfc369ffcdc4722482c818e6ba6cf6e704c2cb5/crypto/sha/sha1dgst.c/#L148 | void SHA1_Update(SHA_CTX *c, const register unsigned char *data,
unsigned long len)
{
register SHA_LONG *p;
int ew,ec,sw,sc;
SHA_LONG l;
if (len == 0) return;
l=(c->Nl+(len<<3))&0xffffffffL;
if (l < c->Nl)
c->Nh++;
c->Nh+=(len>>29);
c->Nl=l;
if (c->num != 0)
{
p=c->data;
sw=c->num>>2;
sc=c->num&0x03;
if ((c->num+len) >= SHA_CBLOCK)
{
l= p[sw];
M_p_c2nl(data,l,sc);
p[sw++]=l;
for (; sw<SHA_LBLOCK; sw++)
{
M_c2nl(data,l);
p[sw]=l;
}
len-=(SHA_CBLOCK-c->num);
sha1_block(c,p,64);
c->num=0;
}
else
{
c->num+=(int)len;
if ((sc+len) < 4)
{
l= p[sw];
M_p_c2nl_p(data,l,sc,len);
p[sw]=l;
}
else
{
ew=(c->num>>2);
ec=(c->num&0x03);
l= p[sw];
M_p_c2nl(data,l,sc);
p[sw++]=l;
for (; sw < ew; sw++)
{ M_c2nl(data,l); p[sw]=l; }
if (ec)
{
M_c2nl_p(data,l,ec);
p[sw]=l;
}
}
return;
}
}
#if 1
#if defined(B_ENDIAN) || defined(SHA1_ASM)
if ((((unsigned long)data)%sizeof(SHA_LONG)) == 0)
{
sw=len/SHA_CBLOCK;
if (sw)
{
sw*=SHA_CBLOCK;
sha1_block(c,(SHA_LONG *)data,sw);
data+=sw;
len-=sw;
}
}
#endif
#endif
p=c->data;
while (len >= SHA_CBLOCK)
{
#if defined(B_ENDIAN) || defined(L_ENDIAN)
if (p != (SHA_LONG *)data)
memcpy(p,data,SHA_CBLOCK);
data+=SHA_CBLOCK;
# ifdef L_ENDIAN
# ifndef SHA1_ASM
for (sw=(SHA_LBLOCK/4); sw; sw--)
{
Endian_Reverse32(p[0]);
Endian_Reverse32(p[1]);
Endian_Reverse32(p[2]);
Endian_Reverse32(p[3]);
p+=4;
}
p=c->data;
# endif
# endif
#else
for (sw=(SHA_BLOCK/4); sw; sw--)
{
M_c2nl(data,l); *(p++)=l;
M_c2nl(data,l); *(p++)=l;
M_c2nl(data,l); *(p++)=l;
M_c2nl(data,l); *(p++)=l;
}
p=c->data;
#endif
sha1_block(c,p,64);
len-=SHA_CBLOCK;
}
ec=(int)len;
c->num=ec;
ew=(ec>>2);
ec&=0x03;
for (sw=0; sw < ew; sw++)
{ M_c2nl(data,l); p[sw]=l; }
M_c2nl_p(data,l,ec);
p[sw]=l;
} | ['static void ssleay_rand_seed(const void *buf, int num)\n\t{\n\tint i,j,k,st_idx,st_num;\n\tMD_CTX m;\n#ifdef NORAND\n\treturn;\n#endif\n\tCRYPTO_w_lock(CRYPTO_LOCK_RAND);\n\tst_idx=state_index;\n\tst_num=state_num;\n\tstate_index=(state_index+num);\n\tif (state_index >= STATE_SIZE)\n\t\t{\n\t\tstate_index%=STATE_SIZE;\n\t\tstate_num=STATE_SIZE;\n\t\t}\n\telse if (state_num < STATE_SIZE)\n\t\t{\n\t\tif (state_index > state_num)\n\t\t\tstate_num=state_index;\n\t\t}\n\tCRYPTO_w_unlock(CRYPTO_LOCK_RAND);\n\tfor (i=0; i<num; i+=MD_DIGEST_LENGTH)\n\t\t{\n\t\tj=(num-i);\n\t\tj=(j > MD_DIGEST_LENGTH)?MD_DIGEST_LENGTH:j;\n\t\tMD_Init(&m);\n\t\tMD_Update(&m,md,MD_DIGEST_LENGTH);\n\t\tk=(st_idx+j)-STATE_SIZE;\n\t\tif (k > 0)\n\t\t\t{\n\t\t\tMD_Update(&m,&(state[st_idx]),j-k);\n\t\t\tMD_Update(&m,&(state[0]),k);\n\t\t\t}\n\t\telse\n\t\t\tMD_Update(&m,&(state[st_idx]),j);\n\t\tMD_Update(&m,buf,j);\n\t\tMD_Update(&m,(unsigned char *)&(md_count[0]),sizeof(md_count));\n\t\tMD_Final(md,&m);\n\t\tmd_count[1]++;\n\t\tbuf=(const char *)buf + j;\n\t\tfor (k=0; k<j; k++)\n\t\t\t{\n\t\t\tstate[st_idx++]^=md[k];\n\t\t\tif (st_idx >= STATE_SIZE)\n\t\t\t\t{\n\t\t\t\tst_idx=0;\n\t\t\t\tst_num=STATE_SIZE;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\tmemset((char *)&m,0,sizeof(m));\n\t}', 'void SHA1_Update(SHA_CTX *c, const register unsigned char *data,\n\t unsigned long len)\n\t{\n\tregister SHA_LONG *p;\n\tint ew,ec,sw,sc;\n\tSHA_LONG l;\n\tif (len == 0) return;\n\tl=(c->Nl+(len<<3))&0xffffffffL;\n\tif (l < c->Nl)\n\t\tc->Nh++;\n\tc->Nh+=(len>>29);\n\tc->Nl=l;\n\tif (c->num != 0)\n\t\t{\n\t\tp=c->data;\n\t\tsw=c->num>>2;\n\t\tsc=c->num&0x03;\n\t\tif ((c->num+len) >= SHA_CBLOCK)\n\t\t\t{\n\t\t\tl= p[sw];\n\t\t\tM_p_c2nl(data,l,sc);\n\t\t\tp[sw++]=l;\n\t\t\tfor (; sw<SHA_LBLOCK; sw++)\n\t\t\t\t{\n\t\t\t\tM_c2nl(data,l);\n\t\t\t\tp[sw]=l;\n\t\t\t\t}\n\t\t\tlen-=(SHA_CBLOCK-c->num);\n\t\t\tsha1_block(c,p,64);\n\t\t\tc->num=0;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tc->num+=(int)len;\n\t\t\tif ((sc+len) < 4)\n\t\t\t\t{\n\t\t\t\tl= p[sw];\n\t\t\t\tM_p_c2nl_p(data,l,sc,len);\n\t\t\t\tp[sw]=l;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tew=(c->num>>2);\n\t\t\t\tec=(c->num&0x03);\n\t\t\t\tl= p[sw];\n\t\t\t\tM_p_c2nl(data,l,sc);\n\t\t\t\tp[sw++]=l;\n\t\t\t\tfor (; sw < ew; sw++)\n\t\t\t\t\t{ M_c2nl(data,l); p[sw]=l; }\n\t\t\t\tif (ec)\n\t\t\t\t\t{\n\t\t\t\t\tM_c2nl_p(data,l,ec);\n\t\t\t\t\tp[sw]=l;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\treturn;\n\t\t\t}\n\t\t}\n#if 1\n#if defined(B_ENDIAN) || defined(SHA1_ASM)\n\tif ((((unsigned long)data)%sizeof(SHA_LONG)) == 0)\n\t\t{\n\t\tsw=len/SHA_CBLOCK;\n\t\tif (sw)\n\t\t\t{\n\t\t\tsw*=SHA_CBLOCK;\n\t\t\tsha1_block(c,(SHA_LONG *)data,sw);\n\t\t\tdata+=sw;\n\t\t\tlen-=sw;\n\t\t\t}\n\t\t}\n#endif\n#endif\n\tp=c->data;\n\twhile (len >= SHA_CBLOCK)\n\t\t{\n#if defined(B_ENDIAN) || defined(L_ENDIAN)\n\t\tif (p != (SHA_LONG *)data)\n\t\t\tmemcpy(p,data,SHA_CBLOCK);\n\t\tdata+=SHA_CBLOCK;\n# ifdef L_ENDIAN\n# ifndef SHA1_ASM\n\t\tfor (sw=(SHA_LBLOCK/4); sw; sw--)\n\t\t\t{\n\t\t\tEndian_Reverse32(p[0]);\n\t\t\tEndian_Reverse32(p[1]);\n\t\t\tEndian_Reverse32(p[2]);\n\t\t\tEndian_Reverse32(p[3]);\n\t\t\tp+=4;\n\t\t\t}\n\t\tp=c->data;\n# endif\n# endif\n#else\n\t\tfor (sw=(SHA_BLOCK/4); sw; sw--)\n\t\t\t{\n\t\t\tM_c2nl(data,l); *(p++)=l;\n\t\t\tM_c2nl(data,l); *(p++)=l;\n\t\t\tM_c2nl(data,l); *(p++)=l;\n\t\t\tM_c2nl(data,l); *(p++)=l;\n\t\t\t}\n\t\tp=c->data;\n#endif\n\t\tsha1_block(c,p,64);\n\t\tlen-=SHA_CBLOCK;\n\t\t}\n\tec=(int)len;\n\tc->num=ec;\n\tew=(ec>>2);\n\tec&=0x03;\n\tfor (sw=0; sw < ew; sw++)\n\t\t{ M_c2nl(data,l); p[sw]=l; }\n\tM_c2nl_p(data,l,ec);\n\tp[sw]=l;\n\t}'] |
35,471 | 0 | https://github.com/openssl/openssl/blob/a974e64aaaa8a6f99f55a68d28c07c04ecea2f50/crypto/pkcs7/pk7_doit.c/#L1097 | 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;
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{\n STACK_OF(PKCS7_RECIP_INFO) *rsk;\n PKCS7_RECIP_INFO *ri;\n int i;\n i = OBJ_obj2nid(p7->type);\n if (i != NID_pkcs7_signedAndEnveloped)\n return NULL;\n if (p7->d.signed_and_enveloped == NULL)\n return NULL;\n rsk = p7->d.signed_and_enveloped->recipientinfo;\n if (rsk == NULL)\n return NULL;\n if (sk_PKCS7_RECIP_INFO_num(rsk) <= idx)\n return (NULL);\n ri = sk_PKCS7_RECIP_INFO_value(rsk, idx);\n return (ri->issuer_and_serial);\n}', 'int OBJ_obj2nid(const ASN1_OBJECT *a)\n{\n const unsigned int *op;\n ADDED_OBJ ad, *adp;\n if (a == NULL)\n return (NID_undef);\n if (a->nid != 0)\n return (a->nid);\n if (a->length == 0)\n return NID_undef;\n if (added != NULL) {\n ad.type = ADDED_DATA;\n ad.obj = (ASN1_OBJECT *)a;\n adp = lh_ADDED_OBJ_retrieve(added, &ad);\n if (adp != NULL)\n return (adp->obj->nid);\n }\n op = OBJ_bsearch_obj(&a, obj_objs, NUM_OBJ);\n if (op == NULL)\n return (NID_undef);\n return (nid_objs[*op].nid);\n}', 'void *lh_retrieve(_LHASH *lh, const void *data)\n{\n unsigned long hash;\n LHASH_NODE **rn;\n void *ret;\n lh->error = 0;\n rn = getrn(lh, data, &hash);\n if (*rn == NULL) {\n lh->num_retrieve_miss++;\n return (NULL);\n } else {\n ret = (*rn)->data;\n lh->num_retrieve++;\n }\n return (ret);\n}', 'int sk_num(const _STACK *st)\n{\n if (st == NULL)\n return -1;\n return st->num;\n}', 'void *sk_value(const _STACK *st, int i)\n{\n if (!st || (i < 0) || (i >= st->num))\n return NULL;\n return st->data[i];\n}'] |
35,472 | 0 | https://github.com/openssl/openssl/blob/305b68f1a2b6d4d0aa07a6ab47ac372f067a40bb/crypto/bn/bn_ctx.c/#L276 | static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
} | ['int BN_mod_exp2_mont(BIGNUM *rr, const BIGNUM *a1, const BIGNUM *p1,\n const BIGNUM *a2, const BIGNUM *p2, const BIGNUM *m,\n BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n int i, j, bits, b, bits1, bits2, ret =\n 0, wpos1, wpos2, window1, window2, wvalue1, wvalue2;\n int r_is_one = 1;\n BIGNUM *d, *r;\n const BIGNUM *a_mod_m;\n BIGNUM *val1[TABLE_SIZE], *val2[TABLE_SIZE];\n BN_MONT_CTX *mont = NULL;\n bn_check_top(a1);\n bn_check_top(p1);\n bn_check_top(a2);\n bn_check_top(p2);\n bn_check_top(m);\n if (!(m->d[0] & 1)) {\n BNerr(BN_F_BN_MOD_EXP2_MONT, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n bits1 = BN_num_bits(p1);\n bits2 = BN_num_bits(p2);\n if ((bits1 == 0) && (bits2 == 0)) {\n ret = BN_one(rr);\n return ret;\n }\n bits = (bits1 > bits2) ? bits1 : bits2;\n BN_CTX_start(ctx);\n d = BN_CTX_get(ctx);\n r = BN_CTX_get(ctx);\n val1[0] = BN_CTX_get(ctx);\n val2[0] = BN_CTX_get(ctx);\n if (val2[0] == NULL)\n goto err;\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n window1 = BN_window_bits_for_exponent_size(bits1);\n window2 = BN_window_bits_for_exponent_size(bits2);\n if (a1->neg || BN_ucmp(a1, m) >= 0) {\n if (!BN_mod(val1[0], a1, m, ctx))\n goto err;\n a_mod_m = val1[0];\n } else\n a_mod_m = a1;\n if (BN_is_zero(a_mod_m)) {\n BN_zero(rr);\n ret = 1;\n goto err;\n }\n if (!BN_to_montgomery(val1[0], a_mod_m, mont, ctx))\n goto err;\n if (window1 > 1) {\n if (!BN_mod_mul_montgomery(d, val1[0], val1[0], mont, ctx))\n goto err;\n j = 1 << (window1 - 1);\n for (i = 1; i < j; i++) {\n if (((val1[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul_montgomery(val1[i], val1[i - 1], d, mont, ctx))\n goto err;\n }\n }\n if (a2->neg || BN_ucmp(a2, m) >= 0) {\n if (!BN_mod(val2[0], a2, m, ctx))\n goto err;\n a_mod_m = val2[0];\n } else\n a_mod_m = a2;\n if (BN_is_zero(a_mod_m)) {\n BN_zero(rr);\n ret = 1;\n goto err;\n }\n if (!BN_to_montgomery(val2[0], a_mod_m, mont, ctx))\n goto err;\n if (window2 > 1) {\n if (!BN_mod_mul_montgomery(d, val2[0], val2[0], mont, ctx))\n goto err;\n j = 1 << (window2 - 1);\n for (i = 1; i < j; i++) {\n if (((val2[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul_montgomery(val2[i], val2[i - 1], d, mont, ctx))\n goto err;\n }\n }\n r_is_one = 1;\n wvalue1 = 0;\n wvalue2 = 0;\n wpos1 = 0;\n wpos2 = 0;\n if (!BN_to_montgomery(r, BN_value_one(), mont, ctx))\n goto err;\n for (b = bits - 1; b >= 0; b--) {\n if (!r_is_one) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (!wvalue1)\n if (BN_is_bit_set(p1, b)) {\n i = b - window1 + 1;\n while (!BN_is_bit_set(p1, i))\n i++;\n wpos1 = i;\n wvalue1 = 1;\n for (i = b - 1; i >= wpos1; i--) {\n wvalue1 <<= 1;\n if (BN_is_bit_set(p1, i))\n wvalue1++;\n }\n }\n if (!wvalue2)\n if (BN_is_bit_set(p2, b)) {\n i = b - window2 + 1;\n while (!BN_is_bit_set(p2, i))\n i++;\n wpos2 = i;\n wvalue2 = 1;\n for (i = b - 1; i >= wpos2; i--) {\n wvalue2 <<= 1;\n if (BN_is_bit_set(p2, i))\n wvalue2++;\n }\n }\n if (wvalue1 && b == wpos1) {\n if (!BN_mod_mul_montgomery(r, r, val1[wvalue1 >> 1], mont, ctx))\n goto err;\n wvalue1 = 0;\n r_is_one = 0;\n }\n if (wvalue2 && b == wpos2) {\n if (!BN_mod_mul_montgomery(r, r, val2[wvalue2 >> 1], mont, ctx))\n goto err;\n wvalue2 = 0;\n r_is_one = 0;\n }\n }\n if (!BN_from_montgomery(rr, r, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n BN_CTX_end(ctx);\n bn_check_top(rr);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_start", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG_EXIT(ctx);\n}', 'int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,\n BN_CTX *ctx)\n{\n return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);\n}', 'int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n BN_MONT_CTX *mont, BN_CTX *ctx)\n{\n BIGNUM *tmp;\n int ret = 0;\n 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 bn_correct_top(r);\n return 1;\n }\n }\n#endif\n if ((a->top + b->top) > 2 * num)\n return 0;\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n if (tmp == NULL)\n goto err;\n bn_check_top(tmp);\n if (a == b) {\n if (!BN_sqr(tmp, a, ctx))\n goto err;\n } else {\n if (!BN_mul(tmp, a, b, ctx))\n goto err;\n }\n#ifdef MONT_WORD\n if (!BN_from_montgomery_word(r, tmp, mont))\n goto err;\n#else\n if (!BN_from_montgomery(r, tmp, mont, ctx))\n goto err;\n#endif\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = 0;\n int top, al, bl;\n BIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n int i;\n#endif\n#ifdef BN_RECURSION\n BIGNUM *t = NULL;\n int j = 0, k;\n#endif\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(r);\n al = a->top;\n bl = b->top;\n if ((al == 0) || (bl == 0)) {\n BN_zero(r);\n return 1;\n }\n top = al + bl;\n BN_CTX_start(ctx);\n if ((r == a) || (r == b)) {\n if ((rr = BN_CTX_get(ctx)) == NULL)\n goto err;\n } else\n rr = r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n i = al - bl;\n#endif\n#ifdef BN_MUL_COMBA\n if (i == 0) {\n# if 0\n if (al == 4) {\n if (bn_wexpand(rr, 8) == NULL)\n goto err;\n rr->top = 8;\n bn_mul_comba4(rr->d, a->d, b->d);\n goto end;\n }\n# endif\n if (al == 8) {\n if (bn_wexpand(rr, 16) == NULL)\n goto err;\n rr->top = 16;\n bn_mul_comba8(rr->d, a->d, b->d);\n goto end;\n }\n }\n#endif\n#ifdef BN_RECURSION\n if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {\n if (i >= -1 && i <= 1) {\n if (i >= 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n }\n if (i == -1) {\n j = BN_num_bits_word((BN_ULONG)bl);\n }\n j = 1 << (j - 1);\n assert(j <= al || j <= bl);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (al > j || bl > j) {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d,\n j, al - j, bl - j, t->d);\n } else {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);\n }\n rr->top = top;\n goto end;\n }\n }\n#endif\n if (bn_wexpand(rr, top) == NULL)\n goto err;\n rr->top = top;\n bn_mul_normal(rr->d, a->d, al, b->d, bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n end:\n#endif\n rr->neg = a->neg ^ b->neg;\n bn_correct_top(rr);\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(r);\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}'] |
35,473 | 0 | https://github.com/openssl/openssl/blob/f826bf779854ab9375f89a74d77f0142bf6944ae/apps/speed.c/#L2625 | static int do_multi(int multi)
{
int n;
int fd[2];
int *fds;
static char sep[]=":";
fds=malloc(multi*sizeof *fds);
for(n=0 ; n < multi ; ++n)
{
pipe(fd);
fflush(stdout);
fflush(stderr);
if(fork())
{
close(fd[1]);
fds[n]=fd[0];
}
else
{
close(fd[0]);
close(1);
dup(fd[1]);
close(fd[1]);
mr=1;
usertime=0;
free(fds);
return 0;
}
printf("Forked child %d\n",n);
}
for(n=0 ; n < multi ; ++n)
{
FILE *f;
char buf[1024];
char *p;
f=fdopen(fds[n],"r");
while(fgets(buf,sizeof buf,f))
{
p=strchr(buf,'\n');
if(p)
*p='\0';
if(buf[0] != '+')
{
fprintf(stderr,"Don't understand line '%s' from child %d\n",
buf,n);
continue;
}
printf("Got: %s from %d\n",buf,n);
if(!strncmp(buf,"+F:",3))
{
int alg;
int j;
p=buf+3;
alg=atoi(sstrsep(&p,sep));
sstrsep(&p,sep);
for(j=0 ; j < SIZE_NUM ; ++j)
results[alg][j]+=atof(sstrsep(&p,sep));
}
else if(!strncmp(buf,"+F2:",4))
{
int k;
double d;
p=buf+4;
k=atoi(sstrsep(&p,sep));
sstrsep(&p,sep);
d=atof(sstrsep(&p,sep));
if(n)
rsa_results[k][0]=1/(1/rsa_results[k][0]+1/d);
else
rsa_results[k][0]=d;
d=atof(sstrsep(&p,sep));
if(n)
rsa_results[k][1]=1/(1/rsa_results[k][1]+1/d);
else
rsa_results[k][1]=d;
}
else if(!strncmp(buf,"+F2:",4))
{
int k;
double d;
p=buf+4;
k=atoi(sstrsep(&p,sep));
sstrsep(&p,sep);
d=atof(sstrsep(&p,sep));
if(n)
rsa_results[k][0]=1/(1/rsa_results[k][0]+1/d);
else
rsa_results[k][0]=d;
d=atof(sstrsep(&p,sep));
if(n)
rsa_results[k][1]=1/(1/rsa_results[k][1]+1/d);
else
rsa_results[k][1]=d;
}
else if(!strncmp(buf,"+F3:",4))
{
int k;
double d;
p=buf+4;
k=atoi(sstrsep(&p,sep));
sstrsep(&p,sep);
d=atof(sstrsep(&p,sep));
if(n)
dsa_results[k][0]=1/(1/dsa_results[k][0]+1/d);
else
dsa_results[k][0]=d;
d=atof(sstrsep(&p,sep));
if(n)
dsa_results[k][1]=1/(1/dsa_results[k][1]+1/d);
else
dsa_results[k][1]=d;
}
#ifndef OPENSSL_NO_ECDSA
else if(!strncmp(buf,"+F4:",4))
{
int k;
double d;
p=buf+4;
k=atoi(sstrsep(&p,sep));
sstrsep(&p,sep);
d=atof(sstrsep(&p,sep));
if(n)
ecdsa_results[k][0]=1/(1/ecdsa_results[k][0]+1/d);
else
ecdsa_results[k][0]=d;
d=atof(sstrsep(&p,sep));
if(n)
ecdsa_results[k][1]=1/(1/ecdsa_results[k][1]+1/d);
else
ecdsa_results[k][1]=d;
}
#endif
#ifndef OPENSSL_NO_ECDH
else if(!strncmp(buf,"+F5:",4))
{
int k;
double d;
p=buf+4;
k=atoi(sstrsep(&p,sep));
sstrsep(&p,sep);
d=atof(sstrsep(&p,sep));
if(n)
ecdh_results[k][0]=1/(1/ecdh_results[k][0]+1/d);
else
ecdh_results[k][0]=d;
}
#endif
else if(!strncmp(buf,"+H:",3))
{
}
else
fprintf(stderr,"Unknown type '%s' from child %d\n",buf,n);
}
fclose(f);
}
free(fds);
return 1;
} | ['static int do_multi(int multi)\n\t{\n\tint n;\n\tint fd[2];\n\tint *fds;\n\tstatic char sep[]=":";\n\tfds=malloc(multi*sizeof *fds);\n\tfor(n=0 ; n < multi ; ++n)\n\t\t{\n\t\tpipe(fd);\n\t\tfflush(stdout);\n\t\tfflush(stderr);\n\t\tif(fork())\n\t\t\t{\n\t\t\tclose(fd[1]);\n\t\t\tfds[n]=fd[0];\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tclose(fd[0]);\n\t\t\tclose(1);\n\t\t\tdup(fd[1]);\n\t\t\tclose(fd[1]);\n\t\t\tmr=1;\n\t\t\tusertime=0;\n\t\t\tfree(fds);\n\t\t\treturn 0;\n\t\t\t}\n\t\tprintf("Forked child %d\\n",n);\n\t\t}\n\tfor(n=0 ; n < multi ; ++n)\n\t\t{\n\t\tFILE *f;\n\t\tchar buf[1024];\n\t\tchar *p;\n\t\tf=fdopen(fds[n],"r");\n\t\twhile(fgets(buf,sizeof buf,f))\n\t\t\t{\n\t\t\tp=strchr(buf,\'\\n\');\n\t\t\tif(p)\n\t\t\t\t*p=\'\\0\';\n\t\t\tif(buf[0] != \'+\')\n\t\t\t\t{\n\t\t\t\tfprintf(stderr,"Don\'t understand line \'%s\' from child %d\\n",\n\t\t\t\t\t\tbuf,n);\n\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\tprintf("Got: %s from %d\\n",buf,n);\n\t\t\tif(!strncmp(buf,"+F:",3))\n\t\t\t\t{\n\t\t\t\tint alg;\n\t\t\t\tint j;\n\t\t\t\tp=buf+3;\n\t\t\t\talg=atoi(sstrsep(&p,sep));\n\t\t\t\tsstrsep(&p,sep);\n\t\t\t\tfor(j=0 ; j < SIZE_NUM ; ++j)\n\t\t\t\t\tresults[alg][j]+=atof(sstrsep(&p,sep));\n\t\t\t\t}\n\t\t\telse if(!strncmp(buf,"+F2:",4))\n\t\t\t\t{\n\t\t\t\tint k;\n\t\t\t\tdouble d;\n\t\t\t\tp=buf+4;\n\t\t\t\tk=atoi(sstrsep(&p,sep));\n\t\t\t\tsstrsep(&p,sep);\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\trsa_results[k][0]=1/(1/rsa_results[k][0]+1/d);\n\t\t\t\telse\n\t\t\t\t\trsa_results[k][0]=d;\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\trsa_results[k][1]=1/(1/rsa_results[k][1]+1/d);\n\t\t\t\telse\n\t\t\t\t\trsa_results[k][1]=d;\n\t\t\t\t}\n\t\t\telse if(!strncmp(buf,"+F2:",4))\n\t\t\t\t{\n\t\t\t\tint k;\n\t\t\t\tdouble d;\n\t\t\t\tp=buf+4;\n\t\t\t\tk=atoi(sstrsep(&p,sep));\n\t\t\t\tsstrsep(&p,sep);\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\trsa_results[k][0]=1/(1/rsa_results[k][0]+1/d);\n\t\t\t\telse\n\t\t\t\t\trsa_results[k][0]=d;\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\trsa_results[k][1]=1/(1/rsa_results[k][1]+1/d);\n\t\t\t\telse\n\t\t\t\t\trsa_results[k][1]=d;\n\t\t\t\t}\n\t\t\telse if(!strncmp(buf,"+F3:",4))\n\t\t\t\t{\n\t\t\t\tint k;\n\t\t\t\tdouble d;\n\t\t\t\tp=buf+4;\n\t\t\t\tk=atoi(sstrsep(&p,sep));\n\t\t\t\tsstrsep(&p,sep);\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\tdsa_results[k][0]=1/(1/dsa_results[k][0]+1/d);\n\t\t\t\telse\n\t\t\t\t\tdsa_results[k][0]=d;\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\tdsa_results[k][1]=1/(1/dsa_results[k][1]+1/d);\n\t\t\t\telse\n\t\t\t\t\tdsa_results[k][1]=d;\n\t\t\t\t}\n#ifndef OPENSSL_NO_ECDSA\n\t\t\telse if(!strncmp(buf,"+F4:",4))\n\t\t\t\t{\n\t\t\t\tint k;\n\t\t\t\tdouble d;\n\t\t\t\tp=buf+4;\n\t\t\t\tk=atoi(sstrsep(&p,sep));\n\t\t\t\tsstrsep(&p,sep);\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\tecdsa_results[k][0]=1/(1/ecdsa_results[k][0]+1/d);\n\t\t\t\telse\n\t\t\t\t\tecdsa_results[k][0]=d;\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\tecdsa_results[k][1]=1/(1/ecdsa_results[k][1]+1/d);\n\t\t\t\telse\n\t\t\t\t\tecdsa_results[k][1]=d;\n\t\t\t\t}\n#endif\n#ifndef OPENSSL_NO_ECDH\n\t\t\telse if(!strncmp(buf,"+F5:",4))\n\t\t\t\t{\n\t\t\t\tint k;\n\t\t\t\tdouble d;\n\t\t\t\tp=buf+4;\n\t\t\t\tk=atoi(sstrsep(&p,sep));\n\t\t\t\tsstrsep(&p,sep);\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\tecdh_results[k][0]=1/(1/ecdh_results[k][0]+1/d);\n\t\t\t\telse\n\t\t\t\t\tecdh_results[k][0]=d;\n\t\t\t\t}\n#endif\n\t\t\telse if(!strncmp(buf,"+H:",3))\n\t\t\t\t{\n\t\t\t\t}\n\t\t\telse\n\t\t\t\tfprintf(stderr,"Unknown type \'%s\' from child %d\\n",buf,n);\n\t\t\t}\n\t\tfclose(f);\n\t\t}\n\tfree(fds);\n\treturn 1;\n\t}'] |
35,474 | 0 | https://github.com/openssl/openssl/blob/02cba628daa7fea959c561531a8a984756bdf41c/crypto/lhash/lhash.c/#L123 | void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
{
unsigned long hash;
OPENSSL_LH_NODE *nn, **rn;
void *ret;
lh->error = 0;
rn = getrn(lh, data, &hash);
if (*rn == NULL) {
lh->num_no_delete++;
return (NULL);
} else {
nn = *rn;
*rn = nn->next;
ret = nn->data;
OPENSSL_free(nn);
lh->num_delete++;
}
lh->num_items--;
if ((lh->num_nodes > MIN_NODES) &&
(lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
contract(lh);
return (ret);
} | ['static int state_machine(SSL *s, int server)\n{\n BUF_MEM *buf = NULL;\n unsigned long Time = (unsigned long)time(NULL);\n void (*cb) (const SSL *ssl, int type, int val) = NULL;\n OSSL_STATEM *st = &s->statem;\n int ret = -1;\n int ssret;\n if (st->state == MSG_FLOW_ERROR) {\n return -1;\n }\n RAND_add(&Time, sizeof(Time), 0);\n ERR_clear_error();\n clear_sys_error();\n cb = get_callback(s);\n st->in_handshake++;\n if (!SSL_in_init(s) || SSL_in_before(s)) {\n if (!SSL_clear(s))\n return -1;\n }\n#ifndef OPENSSL_NO_SCTP\n if (SSL_IS_DTLS(s)) {\n BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,\n st->in_handshake, NULL);\n }\n#endif\n if (st->state == MSG_FLOW_UNINITED\n || st->state == MSG_FLOW_FINISHED) {\n if (st->state == MSG_FLOW_UNINITED) {\n st->hand_state = TLS_ST_BEFORE;\n st->request_state = TLS_ST_BEFORE;\n }\n s->server = server;\n if (cb != NULL)\n cb(s, SSL_CB_HANDSHAKE_START, 1);\n if (SSL_IS_DTLS(s)) {\n if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00) &&\n (server || (s->version & 0xff00) != (DTLS1_BAD_VER & 0xff00))) {\n SSLerr(SSL_F_STATE_MACHINE, ERR_R_INTERNAL_ERROR);\n goto end;\n }\n } else {\n if ((s->version >> 8) != SSL3_VERSION_MAJOR) {\n SSLerr(SSL_F_STATE_MACHINE, ERR_R_INTERNAL_ERROR);\n goto end;\n }\n }\n if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {\n SSLerr(SSL_F_STATE_MACHINE, SSL_R_VERSION_TOO_LOW);\n goto end;\n }\n if (s->init_buf == NULL) {\n if ((buf = BUF_MEM_new()) == NULL) {\n goto end;\n }\n if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {\n goto end;\n }\n s->init_buf = buf;\n buf = NULL;\n }\n if (!ssl3_setup_buffers(s)) {\n goto end;\n }\n s->init_num = 0;\n s->s3->change_cipher_spec = 0;\n#ifndef OPENSSL_NO_SCTP\n if (!SSL_IS_DTLS(s) || !BIO_dgram_is_sctp(SSL_get_wbio(s)))\n#endif\n if (!ssl_init_wbio_buffer(s)) {\n goto end;\n }\n if (SSL_IS_FIRST_HANDSHAKE(s) || s->renegotiate) {\n if (!tls_setup_handshake(s)) {\n ossl_statem_set_error(s);\n goto end;\n }\n if (SSL_IS_FIRST_HANDSHAKE(s))\n st->read_state_first_init = 1;\n }\n st->state = MSG_FLOW_WRITING;\n init_write_state_machine(s);\n }\n while (st->state != MSG_FLOW_FINISHED) {\n if (st->state == MSG_FLOW_READING) {\n ssret = read_state_machine(s);\n if (ssret == SUB_STATE_FINISHED) {\n st->state = MSG_FLOW_WRITING;\n init_write_state_machine(s);\n } else {\n goto end;\n }\n } else if (st->state == MSG_FLOW_WRITING) {\n ssret = write_state_machine(s);\n if (ssret == SUB_STATE_FINISHED) {\n st->state = MSG_FLOW_READING;\n init_read_state_machine(s);\n } else if (ssret == SUB_STATE_END_HANDSHAKE) {\n st->state = MSG_FLOW_FINISHED;\n } else {\n goto end;\n }\n } else {\n ossl_statem_set_error(s);\n goto end;\n }\n }\n ret = 1;\n end:\n st->in_handshake--;\n#ifndef OPENSSL_NO_SCTP\n if (SSL_IS_DTLS(s)) {\n BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,\n st->in_handshake, NULL);\n }\n#endif\n BUF_MEM_free(buf);\n if (cb != NULL) {\n if (server)\n cb(s, SSL_CB_ACCEPT_EXIT, ret);\n else\n cb(s, SSL_CB_CONNECT_EXIT, ret);\n }\n return ret;\n}', 'int tls_setup_handshake(SSL *s)\n{\n if (!ssl3_init_finished_mac(s))\n return 0;\n if (s->server) {\n if (SSL_IS_FIRST_HANDSHAKE(s)) {\n s->ctx->stats.sess_accept++;\n } else if (!s->s3->send_connection_binding &&\n !(s->options &\n SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {\n SSLerr(SSL_F_TLS_SETUP_HANDSHAKE,\n SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);\n ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);\n return 0;\n } else {\n s->ctx->stats.sess_accept_renegotiate++;\n s->s3->tmp.cert_request = 0;\n }\n } else {\n if (SSL_IS_FIRST_HANDSHAKE(s))\n s->ctx->stats.sess_connect++;\n else\n s->ctx->stats.sess_connect_renegotiate++;\n memset(s->s3->client_random, 0, sizeof(s->s3->client_random));\n s->hit = 0;\n s->s3->tmp.cert_req = 0;\n if (SSL_IS_DTLS(s))\n s->statem.use_timer = 1;\n }\n return 1;\n}', 'int ssl3_send_alert(SSL *s, int level, int desc)\n{\n desc = s->method->ssl3_enc->alert_value(desc);\n if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION)\n desc = SSL_AD_HANDSHAKE_FAILURE;\n if (desc < 0)\n return -1;\n if ((level == SSL3_AL_FATAL) && (s->session != NULL))\n SSL_CTX_remove_session(s->session_ctx, s->session);\n s->s3->alert_dispatch = 1;\n s->s3->send_alert[0] = level;\n s->s3->send_alert[1] = desc;\n if (!RECORD_LAYER_write_pending(&s->rlayer)) {\n return s->method->ssl_dispatch_alert(s);\n }\n return -1;\n}', 'int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)\n{\n return remove_session_lock(ctx, c, 1);\n}', 'static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)\n{\n SSL_SESSION *r;\n int ret = 0;\n if ((c != NULL) && (c->session_id_length != 0)) {\n if (lck)\n CRYPTO_THREAD_write_lock(ctx->lock);\n if ((r = lh_SSL_SESSION_retrieve(ctx->sessions, c)) == c) {\n ret = 1;\n r = lh_SSL_SESSION_delete(ctx->sessions, c);\n SSL_SESSION_list_remove(ctx, c);\n }\n c->not_resumable = 1;\n if (lck)\n CRYPTO_THREAD_unlock(ctx->lock);\n if (ret)\n SSL_SESSION_free(r);\n if (ctx->remove_session_cb != NULL)\n ctx->remove_session_cb(ctx, c);\n } else\n ret = 0;\n return (ret);\n}', 'DEFINE_LHASH_OF(SSL_SESSION)', 'void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)\n{\n unsigned long hash;\n OPENSSL_LH_NODE *nn, **rn;\n void *ret;\n lh->error = 0;\n rn = getrn(lh, data, &hash);\n if (*rn == NULL) {\n lh->num_no_delete++;\n return (NULL);\n } else {\n nn = *rn;\n *rn = nn->next;\n ret = nn->data;\n OPENSSL_free(nn);\n lh->num_delete++;\n }\n lh->num_items--;\n if ((lh->num_nodes > MIN_NODES) &&\n (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))\n contract(lh);\n return (ret);\n}'] |
35,475 | 0 | https://github.com/libav/libav/blob/f97cb4515626228620d7317191c4c32f14eb1a1b/libavcodec/error_resilience.c/#L497 | 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, mot_step, mot_stride;
set_mv_strides(s, &mot_step, &mot_stride);
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.f.mb_type[mb_xy]))
f = MV_FROZEN;
if (!(error & ER_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.f.mb_type[mb_xy]))
continue;
if (!(s->error_status_table[mb_xy] & ER_MV_ERROR))
continue;
s->mv_dir = s->last_picture.f.data[0] ? MV_DIR_FORWARD
: MV_DIR_BACKWARD;
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, 0);
}
}
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 ref[8] = { 0 };
int pred_count = 0;
int j;
int best_score = 256 * 256 * 256 * 64;
int best_pred = 0;
const int mot_index = (mb_x + mb_y * mot_stride) * mot_step;
int prev_x, prev_y, prev_ref;
if ((mb_x ^ mb_y ^ pass) & 1)
continue;
if (fixed[mb_xy] == MV_FROZEN)
continue;
assert(!IS_INTRA(s->current_picture.f.mb_type[mb_xy]));
assert(s->last_picture_ptr && s->last_picture_ptr->f.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.f.motion_val[0][mot_index - mot_step][0];
mv_predictor[pred_count][1] =
s->current_picture.f.motion_val[0][mot_index - mot_step][1];
ref[pred_count] =
s->current_picture.f.ref_index[0][4 * (mb_xy - 1)];
pred_count++;
}
if (mb_x + 1 < mb_width && fixed[mb_xy + 1]) {
mv_predictor[pred_count][0] =
s->current_picture.f.motion_val[0][mot_index + mot_step][0];
mv_predictor[pred_count][1] =
s->current_picture.f.motion_val[0][mot_index + mot_step][1];
ref[pred_count] =
s->current_picture.f.ref_index[0][4 * (mb_xy + 1)];
pred_count++;
}
if (mb_y > 0 && fixed[mb_xy - mb_stride]) {
mv_predictor[pred_count][0] =
s->current_picture.f.motion_val[0][mot_index - mot_stride * mot_step][0];
mv_predictor[pred_count][1] =
s->current_picture.f.motion_val[0][mot_index - mot_stride * mot_step][1];
ref[pred_count] =
s->current_picture.f.ref_index[0][4 * (mb_xy - s->mb_stride)];
pred_count++;
}
if (mb_y + 1<mb_height && fixed[mb_xy + mb_stride]) {
mv_predictor[pred_count][0] =
s->current_picture.f.motion_val[0][mot_index + mot_stride * mot_step][0];
mv_predictor[pred_count][1] =
s->current_picture.f.motion_val[0][mot_index + mot_stride * mot_step][1];
ref[pred_count] =
s->current_picture.f.ref_index[0][4 * (mb_xy + s->mb_stride)];
pred_count++;
}
if (pred_count == 0)
continue;
if (pred_count > 1) {
int sum_x = 0, sum_y = 0, sum_r = 0;
int max_x, max_y, min_x, min_y, max_r, min_r;
for (j = 0; j < pred_count; j++) {
sum_x += mv_predictor[j][0];
sum_y += mv_predictor[j][1];
sum_r += ref[j];
if (j && ref[j] != ref[j - 1])
goto skip_mean_and_median;
}
mv_predictor[pred_count][0] = sum_x / j;
mv_predictor[pred_count][1] = sum_y / j;
ref[pred_count] = sum_r / j;
if (pred_count >= 3) {
min_y = min_x = min_r = 99999;
max_y = max_x = max_r = -99999;
} else {
min_x = min_y = max_x = max_y = min_r = max_r = 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]);
max_r = FFMAX(max_r, ref[j]);
min_x = FFMIN(min_x, mv_predictor[j][0]);
min_y = FFMIN(min_y, mv_predictor[j][1]);
min_r = FFMIN(min_r, ref[j]);
}
mv_predictor[pred_count + 1][0] = sum_x - max_x - min_x;
mv_predictor[pred_count + 1][1] = sum_y - max_y - min_y;
ref[pred_count + 1] = sum_r - max_r - min_r;
if (pred_count == 4) {
mv_predictor[pred_count + 1][0] /= 2;
mv_predictor[pred_count + 1][1] /= 2;
ref[pred_count + 1] /= 2;
}
pred_count += 2;
}
skip_mean_and_median:
pred_count++;
if (!fixed[mb_xy]) {
if (s->avctx->codec_id == CODEC_ID_H264) {
} else {
ff_thread_await_progress((AVFrame *) s->last_picture_ptr,
mb_y, 0);
}
if (!s->last_picture.f.motion_val[0] ||
!s->last_picture.f.ref_index[0])
goto skip_last_mv;
prev_x = s->last_picture.f.motion_val[0][mot_index][0];
prev_y = s->last_picture.f.motion_val[0][mot_index][1];
prev_ref = s->last_picture.f.ref_index[0][4 * mb_xy];
} else {
prev_x = s->current_picture.f.motion_val[0][mot_index][0];
prev_y = s->current_picture.f.motion_val[0][mot_index][1];
prev_ref = s->current_picture.f.ref_index[0][4 * mb_xy];
}
mv_predictor[pred_count][0] = prev_x;
mv_predictor[pred_count][1] = prev_y;
ref[pred_count] = prev_ref;
pred_count++;
skip_last_mv:
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.f.data[0] +
mb_x * 16 + mb_y * 16 * s->linesize;
s->current_picture.f.motion_val[0][mot_index][0] =
s->mv[0][0][0] = mv_predictor[j][0];
s->current_picture.f.motion_val[0][mot_index][1] =
s->mv[0][0][1] = mv_predictor[j][1];
if (ref[j] < 0)
continue;
decode_mb(s, ref[j]);
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->mv[0][0][0] = mv_predictor[best_pred][0];
s->mv[0][0][1] = mv_predictor[best_pred][1];
for (i = 0; i < mot_step; i++)
for (j = 0; j < mot_step; j++) {
s->current_picture.f.motion_val[0][mot_index + i + j * mot_stride][0] = s->mv[0][0][0];
s->current_picture.f.motion_val[0][mot_index + i + j * mot_stride][1] = s->mv[0][0][1];
}
decode_mb(s, ref[best_pred]);
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{\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, mot_step, mot_stride;\n set_mv_strides(s, &mot_step, &mot_stride);\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.f.mb_type[mb_xy]))\n f = MV_FROZEN;\n if (!(error & ER_MV_ERROR))\n 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)) ||\n 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.f.mb_type[mb_xy]))\n continue;\n if (!(s->error_status_table[mb_xy] & ER_MV_ERROR))\n continue;\n s->mv_dir = s->last_picture.f.data[0] ? MV_DIR_FORWARD\n : MV_DIR_BACKWARD;\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, 0);\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;\n int 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 ref[8] = { 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_index = (mb_x + mb_y * mot_stride) * mot_step;\n int prev_x, prev_y, prev_ref;\n if ((mb_x ^ mb_y ^ pass) & 1)\n continue;\n if (fixed[mb_xy] == MV_FROZEN)\n continue;\n assert(!IS_INTRA(s->current_picture.f.mb_type[mb_xy]));\n assert(s->last_picture_ptr && s->last_picture_ptr->f.data[0]);\n j = 0;\n if (mb_x > 0 && fixed[mb_xy - 1] == MV_FROZEN)\n j = 1;\n if (mb_x + 1 < mb_width && fixed[mb_xy + 1] == MV_FROZEN)\n j = 1;\n if (mb_y > 0 && fixed[mb_xy - mb_stride] == MV_FROZEN)\n j = 1;\n if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride] == MV_FROZEN)\n j = 1;\n if (j == 0)\n continue;\n j = 0;\n if (mb_x > 0 && fixed[mb_xy - 1 ] == MV_CHANGED)\n j = 1;\n if (mb_x + 1 < mb_width && fixed[mb_xy + 1 ] == MV_CHANGED)\n j = 1;\n if (mb_y > 0 && fixed[mb_xy - mb_stride] == MV_CHANGED)\n j = 1;\n if (mb_y + 1 < mb_height && fixed[mb_xy + mb_stride] == MV_CHANGED)\n j = 1;\n if (j == 0 && pass > 1)\n continue;\n none_left = 0;\n if (mb_x > 0 && fixed[mb_xy - 1]) {\n mv_predictor[pred_count][0] =\n s->current_picture.f.motion_val[0][mot_index - mot_step][0];\n mv_predictor[pred_count][1] =\n s->current_picture.f.motion_val[0][mot_index - mot_step][1];\n ref[pred_count] =\n s->current_picture.f.ref_index[0][4 * (mb_xy - 1)];\n pred_count++;\n }\n if (mb_x + 1 < mb_width && fixed[mb_xy + 1]) {\n mv_predictor[pred_count][0] =\n s->current_picture.f.motion_val[0][mot_index + mot_step][0];\n mv_predictor[pred_count][1] =\n s->current_picture.f.motion_val[0][mot_index + mot_step][1];\n ref[pred_count] =\n s->current_picture.f.ref_index[0][4 * (mb_xy + 1)];\n pred_count++;\n }\n if (mb_y > 0 && fixed[mb_xy - mb_stride]) {\n mv_predictor[pred_count][0] =\n s->current_picture.f.motion_val[0][mot_index - mot_stride * mot_step][0];\n mv_predictor[pred_count][1] =\n s->current_picture.f.motion_val[0][mot_index - mot_stride * mot_step][1];\n ref[pred_count] =\n s->current_picture.f.ref_index[0][4 * (mb_xy - s->mb_stride)];\n pred_count++;\n }\n if (mb_y + 1<mb_height && fixed[mb_xy + mb_stride]) {\n mv_predictor[pred_count][0] =\n s->current_picture.f.motion_val[0][mot_index + mot_stride * mot_step][0];\n mv_predictor[pred_count][1] =\n s->current_picture.f.motion_val[0][mot_index + mot_stride * mot_step][1];\n ref[pred_count] =\n s->current_picture.f.ref_index[0][4 * (mb_xy + s->mb_stride)];\n pred_count++;\n }\n if (pred_count == 0)\n continue;\n if (pred_count > 1) {\n int sum_x = 0, sum_y = 0, sum_r = 0;\n int max_x, max_y, min_x, min_y, max_r, min_r;\n for (j = 0; j < pred_count; j++) {\n sum_x += mv_predictor[j][0];\n sum_y += mv_predictor[j][1];\n sum_r += ref[j];\n if (j && ref[j] != ref[j - 1])\n goto skip_mean_and_median;\n }\n mv_predictor[pred_count][0] = sum_x / j;\n mv_predictor[pred_count][1] = sum_y / j;\n ref[pred_count] = sum_r / j;\n if (pred_count >= 3) {\n min_y = min_x = min_r = 99999;\n max_y = max_x = max_r = -99999;\n } else {\n min_x = min_y = max_x = max_y = min_r = max_r = 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 max_r = FFMAX(max_r, ref[j]);\n min_x = FFMIN(min_x, mv_predictor[j][0]);\n min_y = FFMIN(min_y, mv_predictor[j][1]);\n min_r = FFMIN(min_r, ref[j]);\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 ref[pred_count + 1] = sum_r - max_r - min_r;\n if (pred_count == 4) {\n mv_predictor[pred_count + 1][0] /= 2;\n mv_predictor[pred_count + 1][1] /= 2;\n ref[pred_count + 1] /= 2;\n }\n pred_count += 2;\n }\nskip_mean_and_median:\n pred_count++;\n if (!fixed[mb_xy]) {\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 if (!s->last_picture.f.motion_val[0] ||\n !s->last_picture.f.ref_index[0])\n goto skip_last_mv;\n prev_x = s->last_picture.f.motion_val[0][mot_index][0];\n prev_y = s->last_picture.f.motion_val[0][mot_index][1];\n prev_ref = s->last_picture.f.ref_index[0][4 * mb_xy];\n } else {\n prev_x = s->current_picture.f.motion_val[0][mot_index][0];\n prev_y = s->current_picture.f.motion_val[0][mot_index][1];\n prev_ref = s->current_picture.f.ref_index[0][4 * mb_xy];\n }\n mv_predictor[pred_count][0] = prev_x;\n mv_predictor[pred_count][1] = prev_y;\n ref[pred_count] = prev_ref;\n pred_count++;\nskip_last_mv:\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.f.data[0] +\n mb_x * 16 + mb_y * 16 * s->linesize;\n s->current_picture.f.motion_val[0][mot_index][0] =\n s->mv[0][0][0] = mv_predictor[j][0];\n s->current_picture.f.motion_val[0][mot_index][1] =\n s->mv[0][0][1] = mv_predictor[j][1];\n if (ref[j] < 0)\n continue;\n decode_mb(s, ref[j]);\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] -\n 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] -\n 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] -\n src[k + s->linesize * 16]);\n }\n if (score <= best_score) {\n best_score = score;\n best_pred = j;\n }\n }\n score_sum += best_score;\n s->mv[0][0][0] = mv_predictor[best_pred][0];\n s->mv[0][0][1] = mv_predictor[best_pred][1];\n for (i = 0; i < mot_step; i++)\n for (j = 0; j < mot_step; j++) {\n s->current_picture.f.motion_val[0][mot_index + i + j * mot_stride][0] = s->mv[0][0][0];\n s->current_picture.f.motion_val[0][mot_index + i + j * mot_stride][1] = s->mv[0][0][1];\n }\n decode_mb(s, ref[best_pred]);\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}'] |
35,476 | 0 | https://github.com/openssl/openssl/blob/6bc62a620e715f7580651ca932eab052aa527886/crypto/bn/bn_ctx.c/#L268 | static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
} | ['static int test_gf2m_modsqrt(void)\n{\n BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;\n BIGNUM *e = NULL, *f = NULL;\n int i, j, st = 0;\n if (!TEST_ptr(a = BN_new())\n || !TEST_ptr(b[0] = BN_new())\n || !TEST_ptr(b[1] = BN_new())\n || !TEST_ptr(c = BN_new())\n || !TEST_ptr(d = BN_new())\n || !TEST_ptr(e = BN_new())\n || !TEST_ptr(f = BN_new()))\n goto err;\n BN_GF2m_arr2poly(p0, b[0]);\n BN_GF2m_arr2poly(p1, b[1]);\n for (i = 0; i < NUM0; i++) {\n BN_bntest_rand(a, 512, 0, 0);\n for (j = 0; j < 2; j++) {\n BN_GF2m_mod(c, a, b[j]);\n BN_GF2m_mod_sqrt(d, a, b[j], ctx);\n BN_GF2m_mod_sqr(e, d, b[j], ctx);\n BN_GF2m_add(f, c, e);\n if (!TEST_BN_eq_zero(f))\n goto err;\n }\n }\n st = 1;\n err:\n BN_free(a);\n BN_free(b[0]);\n BN_free(b[1]);\n BN_free(c);\n BN_free(d);\n BN_free(e);\n BN_free(f);\n return st;\n}', 'int BN_GF2m_mod_sqrt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)\n{\n int ret = 0;\n const int max = BN_num_bits(p) + 1;\n int *arr = NULL;\n bn_check_top(a);\n bn_check_top(p);\n if ((arr = OPENSSL_malloc(sizeof(*arr) * max)) == NULL)\n goto err;\n ret = BN_GF2m_poly2arr(p, arr, max);\n if (!ret || ret > max) {\n BNerr(BN_F_BN_GF2M_MOD_SQRT, BN_R_INVALID_LENGTH);\n goto err;\n }\n ret = BN_GF2m_mod_sqrt_arr(r, a, arr, ctx);\n bn_check_top(r);\n err:\n OPENSSL_free(arr);\n return ret;\n}', 'int BN_GF2m_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)\n{\n int ret = 0;\n const int max = BN_num_bits(p) + 1;\n int *arr = NULL;\n bn_check_top(a);\n bn_check_top(p);\n if ((arr = OPENSSL_malloc(sizeof(*arr) * max)) == NULL)\n goto err;\n ret = BN_GF2m_poly2arr(p, arr, max);\n if (!ret || ret > max) {\n BNerr(BN_F_BN_GF2M_MOD_SQR, BN_R_INVALID_LENGTH);\n goto err;\n }\n ret = BN_GF2m_mod_sqr_arr(r, a, arr, ctx);\n bn_check_top(r);\n err:\n OPENSSL_free(arr);\n return ret;\n}', 'int BN_GF2m_mod_sqr_arr(BIGNUM *r, const BIGNUM *a, const int p[],\n BN_CTX *ctx)\n{\n int i, ret = 0;\n BIGNUM *s;\n bn_check_top(a);\n BN_CTX_start(ctx);\n if ((s = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (!bn_wexpand(s, 2 * a->top))\n goto err;\n for (i = a->top - 1; i >= 0; i--) {\n s->d[2 * i + 1] = SQR1(a->d[i]);\n s->d[2 * i] = SQR0(a->d[i]);\n }\n s->top = 2 * a->top;\n bn_correct_top(s);\n if (!BN_GF2m_mod_arr(r, s, p))\n goto err;\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_start()", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG("LEAVE BN_CTX_start()", ctx);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_end()", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG("LEAVE BN_CTX_end()", ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}'] |
35,477 | 0 | https://github.com/libav/libav/blob/124c21d79f2124d028890022e98ea853a834a964/libavcodec/mjpegdec.c/#L801 | static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int Ah, int Al){
int i, mb_x, mb_y;
uint8_t* data[MAX_COMPONENTS];
int linesize[MAX_COMPONENTS];
for(i=0; i < nb_components; i++) {
int c = s->comp_index[i];
data[c] = s->picture.data[c];
linesize[c]=s->linesize[c];
s->coefs_finished[c] |= 1;
if(s->avctx->codec->id==CODEC_ID_AMV) {
assert(!(s->avctx->flags & CODEC_FLAG_EMU_EDGE));
data[c] += (linesize[c] * (s->v_scount[i] * (8 * s->mb_height -((s->height/s->v_max)&7)) - 1 ));
linesize[c] *= -1;
}
}
for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
if (s->restart_interval && !s->restart_count)
s->restart_count = s->restart_interval;
for(i=0;i<nb_components;i++) {
uint8_t *ptr;
int n, h, v, x, y, c, j;
n = s->nb_blocks[i];
c = s->comp_index[i];
h = s->h_scount[i];
v = s->v_scount[i];
x = 0;
y = 0;
for(j=0;j<n;j++) {
ptr = data[c] +
(((linesize[c] * (v * mb_y + y) * 8) +
(h * mb_x + x) * 8) >> s->avctx->lowres);
if(s->interlaced && s->bottom_field)
ptr += linesize[c] >> 1;
if(!s->progressive) {
s->dsp.clear_block(s->block);
if(decode_block(s, s->block, i,
s->dc_index[i], s->ac_index[i],
s->quant_matrixes[ s->quant_index[c] ]) < 0) {
av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x);
return -1;
}
s->dsp.idct_put(ptr, linesize[c], s->block);
} else {
int block_idx = s->block_stride[c] * (v * mb_y + y) + (h * mb_x + x);
DCTELEM *block = s->blocks[c][block_idx];
if(Ah)
block[0] += get_bits1(&s->gb) * s->quant_matrixes[ s->quant_index[c] ][0] << Al;
else if(decode_dc_progressive(s, block, i, s->dc_index[i], s->quant_matrixes[ s->quant_index[c] ], Al) < 0) {
av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x);
return -1;
}
}
if (++x == h) {
x = 0;
y++;
}
}
}
if (s->restart_interval && (s->restart_interval < 1350) &&
!--s->restart_count) {
align_get_bits(&s->gb);
skip_bits(&s->gb, 16);
for (i=0; i<nb_components; i++)
s->last_dc[i] = 1024;
}
}
}
return 0;
} | ['static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int Ah, int Al){\n int i, mb_x, mb_y;\n uint8_t* data[MAX_COMPONENTS];\n int linesize[MAX_COMPONENTS];\n for(i=0; i < nb_components; i++) {\n int c = s->comp_index[i];\n data[c] = s->picture.data[c];\n linesize[c]=s->linesize[c];\n s->coefs_finished[c] |= 1;\n if(s->avctx->codec->id==CODEC_ID_AMV) {\n assert(!(s->avctx->flags & CODEC_FLAG_EMU_EDGE));\n data[c] += (linesize[c] * (s->v_scount[i] * (8 * s->mb_height -((s->height/s->v_max)&7)) - 1 ));\n linesize[c] *= -1;\n }\n }\n for(mb_y = 0; mb_y < s->mb_height; mb_y++) {\n for(mb_x = 0; mb_x < s->mb_width; mb_x++) {\n if (s->restart_interval && !s->restart_count)\n s->restart_count = s->restart_interval;\n for(i=0;i<nb_components;i++) {\n uint8_t *ptr;\n int n, h, v, x, y, c, j;\n n = s->nb_blocks[i];\n c = s->comp_index[i];\n h = s->h_scount[i];\n v = s->v_scount[i];\n x = 0;\n y = 0;\n for(j=0;j<n;j++) {\n ptr = data[c] +\n (((linesize[c] * (v * mb_y + y) * 8) +\n (h * mb_x + x) * 8) >> s->avctx->lowres);\n if(s->interlaced && s->bottom_field)\n ptr += linesize[c] >> 1;\n if(!s->progressive) {\n s->dsp.clear_block(s->block);\n if(decode_block(s, s->block, i,\n s->dc_index[i], s->ac_index[i],\n s->quant_matrixes[ s->quant_index[c] ]) < 0) {\n av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\\n", mb_y, mb_x);\n return -1;\n }\n s->dsp.idct_put(ptr, linesize[c], s->block);\n } else {\n int block_idx = s->block_stride[c] * (v * mb_y + y) + (h * mb_x + x);\n DCTELEM *block = s->blocks[c][block_idx];\n if(Ah)\n block[0] += get_bits1(&s->gb) * s->quant_matrixes[ s->quant_index[c] ][0] << Al;\n else if(decode_dc_progressive(s, block, i, s->dc_index[i], s->quant_matrixes[ s->quant_index[c] ], Al) < 0) {\n av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\\n", mb_y, mb_x);\n return -1;\n }\n }\n if (++x == h) {\n x = 0;\n y++;\n }\n }\n }\n if (s->restart_interval && (s->restart_interval < 1350) &&\n !--s->restart_count) {\n align_get_bits(&s->gb);\n skip_bits(&s->gb, 16);\n for (i=0; i<nb_components; i++)\n s->last_dc[i] = 1024;\n }\n }\n }\n return 0;\n}'] |
35,478 | 0 | https://github.com/nginx/nginx/blob/8d3ef1a3b320f786d06170d5831555aa3910de64/src/core/ngx_open_file_cache.c/#L322 | ngx_int_t
ngx_open_cached_file(ngx_open_file_cache_t *cache, ngx_str_t *name,
ngx_open_file_info_t *of, ngx_pool_t *pool)
{
time_t now;
uint32_t hash;
ngx_int_t rc;
ngx_file_info_t fi;
ngx_pool_cleanup_t *cln;
ngx_cached_open_file_t *file;
ngx_pool_cleanup_file_t *clnf;
ngx_open_file_cache_cleanup_t *ofcln;
of->fd = NGX_INVALID_FILE;
of->err = 0;
if (cache == NULL) {
if (of->test_only) {
if (ngx_file_info(name->data, &fi) == NGX_FILE_ERROR) {
of->err = ngx_errno;
of->failed = ngx_file_info_n;
return NGX_ERROR;
}
of->uniq = ngx_file_uniq(&fi);
of->mtime = ngx_file_mtime(&fi);
of->size = ngx_file_size(&fi);
of->fs_size = ngx_file_fs_size(&fi);
of->is_dir = ngx_is_dir(&fi);
of->is_file = ngx_is_file(&fi);
of->is_link = ngx_is_link(&fi);
of->is_exec = ngx_is_exec(&fi);
return NGX_OK;
}
cln = ngx_pool_cleanup_add(pool, sizeof(ngx_pool_cleanup_file_t));
if (cln == NULL) {
return NGX_ERROR;
}
rc = ngx_open_and_stat_file(name->data, of, pool->log);
if (rc == NGX_OK && !of->is_dir) {
cln->handler = ngx_pool_cleanup_file;
clnf = cln->data;
clnf->fd = of->fd;
clnf->name = name->data;
clnf->log = pool->log;
}
return rc;
}
cln = ngx_pool_cleanup_add(pool, sizeof(ngx_open_file_cache_cleanup_t));
if (cln == NULL) {
return NGX_ERROR;
}
now = ngx_time();
hash = ngx_crc32_long(name->data, name->len);
file = ngx_open_file_lookup(cache, name, hash);
if (file) {
file->uses++;
ngx_queue_remove(&file->queue);
if (file->fd == NGX_INVALID_FILE && file->err == 0 && !file->is_dir) {
rc = ngx_open_and_stat_file(name->data, of, pool->log);
if (rc != NGX_OK && (of->err == 0 || !of->errors)) {
goto failed;
}
goto add_event;
}
if (file->use_event
|| (file->event == NULL
&& (of->uniq == 0 || of->uniq == file->uniq)
&& now - file->created < of->valid))
{
if (file->err == 0) {
of->fd = file->fd;
of->uniq = file->uniq;
of->mtime = file->mtime;
of->size = file->size;
of->is_dir = file->is_dir;
of->is_file = file->is_file;
of->is_link = file->is_link;
of->is_exec = file->is_exec;
of->is_directio = file->is_directio;
if (!file->is_dir) {
file->count++;
ngx_open_file_add_event(cache, file, of, pool->log);
}
} else {
of->err = file->err;
of->failed = ngx_open_file_n;
}
goto found;
}
ngx_log_debug4(NGX_LOG_DEBUG_CORE, pool->log, 0,
"retest open file: %s, fd:%d, c:%d, e:%d",
file->name, file->fd, file->count, file->err);
if (file->is_dir) {
of->test_dir = 1;
}
of->fd = file->fd;
of->uniq = file->uniq;
rc = ngx_open_and_stat_file(name->data, of, pool->log);
if (rc != NGX_OK && (of->err == 0 || !of->errors)) {
goto failed;
}
if (of->is_dir) {
if (file->is_dir || file->err) {
goto update;
}
} else if (of->err == 0) {
if (file->is_dir || file->err) {
goto add_event;
}
if (of->uniq == file->uniq) {
if (file->event) {
file->use_event = 1;
}
of->is_directio = file->is_directio;
goto update;
}
} else {
if (file->err || file->is_dir) {
goto update;
}
}
if (file->count == 0) {
ngx_open_file_del_event(file);
if (ngx_close_file(file->fd) == NGX_FILE_ERROR) {
ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno,
ngx_close_file_n " \"%s\" failed",
name->data);
}
goto add_event;
}
ngx_rbtree_delete(&cache->rbtree, &file->node);
cache->current--;
file->close = 1;
goto create;
}
rc = ngx_open_and_stat_file(name->data, of, pool->log);
if (rc != NGX_OK && (of->err == 0 || !of->errors)) {
goto failed;
}
create:
if (cache->current >= cache->max) {
ngx_expire_old_cached_files(cache, 0, pool->log);
}
file = ngx_alloc(sizeof(ngx_cached_open_file_t), pool->log);
if (file == NULL) {
goto failed;
}
file->name = ngx_alloc(name->len + 1, pool->log);
if (file->name == NULL) {
ngx_free(file);
file = NULL;
goto failed;
}
ngx_cpystrn(file->name, name->data, name->len + 1);
file->node.key = hash;
ngx_rbtree_insert(&cache->rbtree, &file->node);
cache->current++;
file->uses = 1;
file->count = 0;
file->use_event = 0;
file->event = NULL;
add_event:
ngx_open_file_add_event(cache, file, of, pool->log);
update:
file->fd = of->fd;
file->err = of->err;
if (of->err == 0) {
file->uniq = of->uniq;
file->mtime = of->mtime;
file->size = of->size;
file->close = 0;
file->is_dir = of->is_dir;
file->is_file = of->is_file;
file->is_link = of->is_link;
file->is_exec = of->is_exec;
file->is_directio = of->is_directio;
if (!of->is_dir) {
file->count++;
}
}
file->created = now;
found:
file->accessed = now;
ngx_queue_insert_head(&cache->expire_queue, &file->queue);
ngx_log_debug5(NGX_LOG_DEBUG_CORE, pool->log, 0,
"cached open file: %s, fd:%d, c:%d, e:%d, u:%d",
file->name, file->fd, file->count, file->err, file->uses);
if (of->err == 0) {
if (!of->is_dir) {
cln->handler = ngx_open_file_cleanup;
ofcln = cln->data;
ofcln->cache = cache;
ofcln->file = file;
ofcln->min_uses = of->min_uses;
ofcln->log = pool->log;
}
return NGX_OK;
}
return NGX_ERROR;
failed:
if (file) {
ngx_rbtree_delete(&cache->rbtree, &file->node);
cache->current--;
if (file->count == 0) {
if (file->fd != NGX_INVALID_FILE) {
if (ngx_close_file(file->fd) == NGX_FILE_ERROR) {
ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno,
ngx_close_file_n " \"%s\" failed",
file->name);
}
}
ngx_free(file->name);
ngx_free(file);
} else {
file->close = 1;
}
}
if (of->fd != NGX_INVALID_FILE) {
if (ngx_close_file(of->fd) == NGX_FILE_ERROR) {
ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno,
ngx_close_file_n " \"%s\" failed", name->data);
}
}
return NGX_ERROR;
} | ['ngx_int_t\nngx_http_core_try_files_phase(ngx_http_request_t *r,\n ngx_http_phase_handler_t *ph)\n{\n size_t len, root, alias, reserve, allocated;\n u_char *p, *name;\n ngx_str_t path, args;\n ngx_uint_t test_dir;\n ngx_http_try_file_t *tf;\n ngx_open_file_info_t of;\n ngx_http_script_code_pt code;\n ngx_http_script_engine_t e;\n ngx_http_core_loc_conf_t *clcf;\n ngx_http_script_len_code_pt lcode;\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "try files phase: %ui", r->phase_handler);\n clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);\n if (clcf->try_files == NULL) {\n r->phase_handler++;\n return NGX_AGAIN;\n }\n allocated = 0;\n root = 0;\n name = NULL;\n path.data = NULL;\n tf = clcf->try_files;\n alias = clcf->alias;\n for ( ;; ) {\n if (tf->lengths) {\n ngx_memzero(&e, sizeof(ngx_http_script_engine_t));\n e.ip = tf->lengths->elts;\n e.request = r;\n len = 1;\n while (*(uintptr_t *) e.ip) {\n lcode = *(ngx_http_script_len_code_pt *) e.ip;\n len += lcode(&e);\n }\n } else {\n len = tf->name.len;\n }\n reserve = ngx_abs((ssize_t) (len - r->uri.len)) + alias + 16;\n if (reserve > allocated) {\n if (ngx_http_map_uri_to_path(r, &path, &root, reserve) == NULL) {\n ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);\n return NGX_OK;\n }\n name = path.data + root;\n allocated = path.len - root - (r->uri.len - alias);\n }\n if (tf->values == NULL) {\n ngx_memcpy(name, tf->name.data, tf->name.len);\n path.len = (name + tf->name.len - 1) - path.data;\n } else {\n e.ip = tf->values->elts;\n e.pos = name;\n e.flushed = 1;\n while (*(uintptr_t *) e.ip) {\n code = *(ngx_http_script_code_pt *) e.ip;\n code((ngx_http_script_engine_t *) &e);\n }\n path.len = e.pos - path.data;\n *e.pos = \'\\0\';\n if (alias && ngx_strncmp(name, clcf->name.data, alias) == 0) {\n ngx_memmove(name, name + alias, len - alias);\n path.len -= alias;\n }\n }\n test_dir = tf->test_dir;\n tf++;\n ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "trying to use %s: \\"%s\\" \\"%s\\"",\n test_dir ? "dir" : "file", name, path.data);\n if (tf->lengths == NULL && tf->name.len == 0) {\n if (tf->code) {\n ngx_http_finalize_request(r, tf->code);\n return NGX_OK;\n }\n path.len -= root;\n path.data += root;\n if (path.data[0] == \'@\') {\n (void) ngx_http_named_location(r, &path);\n } else {\n ngx_http_split_args(r, &path, &args);\n (void) ngx_http_internal_redirect(r, &path, &args);\n }\n ngx_http_finalize_request(r, NGX_DONE);\n return NGX_OK;\n }\n ngx_memzero(&of, sizeof(ngx_open_file_info_t));\n of.read_ahead = clcf->read_ahead;\n of.directio = clcf->directio;\n of.valid = clcf->open_file_cache_valid;\n of.min_uses = clcf->open_file_cache_min_uses;\n of.test_only = 1;\n of.errors = clcf->open_file_cache_errors;\n of.events = clcf->open_file_cache_events;\n if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)\n != NGX_OK)\n {\n if (of.err != NGX_ENOENT\n && of.err != NGX_ENOTDIR\n && of.err != NGX_ENAMETOOLONG)\n {\n ngx_log_error(NGX_LOG_CRIT, r->connection->log, of.err,\n "%s \\"%s\\" failed", of.failed, path.data);\n }\n continue;\n }\n if (of.is_dir && !test_dir) {\n continue;\n }\n path.len -= root;\n path.data += root;\n if (!alias) {\n r->uri = path;\n#if (NGX_PCRE)\n } else if (clcf->regex) {\n if (!test_dir) {\n r->uri = path;\n r->add_uri_to_alias = 1;\n }\n#endif\n } else {\n r->uri.len = alias + path.len;\n r->uri.data = ngx_pnalloc(r->pool, r->uri.len);\n if (r->uri.data == NULL) {\n ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);\n return NGX_OK;\n }\n p = ngx_copy(r->uri.data, clcf->name.data, alias);\n ngx_memcpy(p, name, path.len);\n }\n ngx_http_set_exten(r);\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "try file uri: \\"%V\\"", &r->uri);\n r->phase_handler++;\n return NGX_AGAIN;\n }\n}', 'ngx_int_t\nngx_open_cached_file(ngx_open_file_cache_t *cache, ngx_str_t *name,\n ngx_open_file_info_t *of, ngx_pool_t *pool)\n{\n time_t now;\n uint32_t hash;\n ngx_int_t rc;\n ngx_file_info_t fi;\n ngx_pool_cleanup_t *cln;\n ngx_cached_open_file_t *file;\n ngx_pool_cleanup_file_t *clnf;\n ngx_open_file_cache_cleanup_t *ofcln;\n of->fd = NGX_INVALID_FILE;\n of->err = 0;\n if (cache == NULL) {\n if (of->test_only) {\n if (ngx_file_info(name->data, &fi) == NGX_FILE_ERROR) {\n of->err = ngx_errno;\n of->failed = ngx_file_info_n;\n return NGX_ERROR;\n }\n of->uniq = ngx_file_uniq(&fi);\n of->mtime = ngx_file_mtime(&fi);\n of->size = ngx_file_size(&fi);\n of->fs_size = ngx_file_fs_size(&fi);\n of->is_dir = ngx_is_dir(&fi);\n of->is_file = ngx_is_file(&fi);\n of->is_link = ngx_is_link(&fi);\n of->is_exec = ngx_is_exec(&fi);\n return NGX_OK;\n }\n cln = ngx_pool_cleanup_add(pool, sizeof(ngx_pool_cleanup_file_t));\n if (cln == NULL) {\n return NGX_ERROR;\n }\n rc = ngx_open_and_stat_file(name->data, of, pool->log);\n if (rc == NGX_OK && !of->is_dir) {\n cln->handler = ngx_pool_cleanup_file;\n clnf = cln->data;\n clnf->fd = of->fd;\n clnf->name = name->data;\n clnf->log = pool->log;\n }\n return rc;\n }\n cln = ngx_pool_cleanup_add(pool, sizeof(ngx_open_file_cache_cleanup_t));\n if (cln == NULL) {\n return NGX_ERROR;\n }\n now = ngx_time();\n hash = ngx_crc32_long(name->data, name->len);\n file = ngx_open_file_lookup(cache, name, hash);\n if (file) {\n file->uses++;\n ngx_queue_remove(&file->queue);\n if (file->fd == NGX_INVALID_FILE && file->err == 0 && !file->is_dir) {\n rc = ngx_open_and_stat_file(name->data, of, pool->log);\n if (rc != NGX_OK && (of->err == 0 || !of->errors)) {\n goto failed;\n }\n goto add_event;\n }\n if (file->use_event\n || (file->event == NULL\n && (of->uniq == 0 || of->uniq == file->uniq)\n && now - file->created < of->valid))\n {\n if (file->err == 0) {\n of->fd = file->fd;\n of->uniq = file->uniq;\n of->mtime = file->mtime;\n of->size = file->size;\n of->is_dir = file->is_dir;\n of->is_file = file->is_file;\n of->is_link = file->is_link;\n of->is_exec = file->is_exec;\n of->is_directio = file->is_directio;\n if (!file->is_dir) {\n file->count++;\n ngx_open_file_add_event(cache, file, of, pool->log);\n }\n } else {\n of->err = file->err;\n of->failed = ngx_open_file_n;\n }\n goto found;\n }\n ngx_log_debug4(NGX_LOG_DEBUG_CORE, pool->log, 0,\n "retest open file: %s, fd:%d, c:%d, e:%d",\n file->name, file->fd, file->count, file->err);\n if (file->is_dir) {\n of->test_dir = 1;\n }\n of->fd = file->fd;\n of->uniq = file->uniq;\n rc = ngx_open_and_stat_file(name->data, of, pool->log);\n if (rc != NGX_OK && (of->err == 0 || !of->errors)) {\n goto failed;\n }\n if (of->is_dir) {\n if (file->is_dir || file->err) {\n goto update;\n }\n } else if (of->err == 0) {\n if (file->is_dir || file->err) {\n goto add_event;\n }\n if (of->uniq == file->uniq) {\n if (file->event) {\n file->use_event = 1;\n }\n of->is_directio = file->is_directio;\n goto update;\n }\n } else {\n if (file->err || file->is_dir) {\n goto update;\n }\n }\n if (file->count == 0) {\n ngx_open_file_del_event(file);\n if (ngx_close_file(file->fd) == NGX_FILE_ERROR) {\n ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno,\n ngx_close_file_n " \\"%s\\" failed",\n name->data);\n }\n goto add_event;\n }\n ngx_rbtree_delete(&cache->rbtree, &file->node);\n cache->current--;\n file->close = 1;\n goto create;\n }\n rc = ngx_open_and_stat_file(name->data, of, pool->log);\n if (rc != NGX_OK && (of->err == 0 || !of->errors)) {\n goto failed;\n }\ncreate:\n if (cache->current >= cache->max) {\n ngx_expire_old_cached_files(cache, 0, pool->log);\n }\n file = ngx_alloc(sizeof(ngx_cached_open_file_t), pool->log);\n if (file == NULL) {\n goto failed;\n }\n file->name = ngx_alloc(name->len + 1, pool->log);\n if (file->name == NULL) {\n ngx_free(file);\n file = NULL;\n goto failed;\n }\n ngx_cpystrn(file->name, name->data, name->len + 1);\n file->node.key = hash;\n ngx_rbtree_insert(&cache->rbtree, &file->node);\n cache->current++;\n file->uses = 1;\n file->count = 0;\n file->use_event = 0;\n file->event = NULL;\nadd_event:\n ngx_open_file_add_event(cache, file, of, pool->log);\nupdate:\n file->fd = of->fd;\n file->err = of->err;\n if (of->err == 0) {\n file->uniq = of->uniq;\n file->mtime = of->mtime;\n file->size = of->size;\n file->close = 0;\n file->is_dir = of->is_dir;\n file->is_file = of->is_file;\n file->is_link = of->is_link;\n file->is_exec = of->is_exec;\n file->is_directio = of->is_directio;\n if (!of->is_dir) {\n file->count++;\n }\n }\n file->created = now;\nfound:\n file->accessed = now;\n ngx_queue_insert_head(&cache->expire_queue, &file->queue);\n ngx_log_debug5(NGX_LOG_DEBUG_CORE, pool->log, 0,\n "cached open file: %s, fd:%d, c:%d, e:%d, u:%d",\n file->name, file->fd, file->count, file->err, file->uses);\n if (of->err == 0) {\n if (!of->is_dir) {\n cln->handler = ngx_open_file_cleanup;\n ofcln = cln->data;\n ofcln->cache = cache;\n ofcln->file = file;\n ofcln->min_uses = of->min_uses;\n ofcln->log = pool->log;\n }\n return NGX_OK;\n }\n return NGX_ERROR;\nfailed:\n if (file) {\n ngx_rbtree_delete(&cache->rbtree, &file->node);\n cache->current--;\n if (file->count == 0) {\n if (file->fd != NGX_INVALID_FILE) {\n if (ngx_close_file(file->fd) == NGX_FILE_ERROR) {\n ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno,\n ngx_close_file_n " \\"%s\\" failed",\n file->name);\n }\n }\n ngx_free(file->name);\n ngx_free(file);\n } else {\n file->close = 1;\n }\n }\n if (of->fd != NGX_INVALID_FILE) {\n if (ngx_close_file(of->fd) == NGX_FILE_ERROR) {\n ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno,\n ngx_close_file_n " \\"%s\\" failed", name->data);\n }\n }\n return NGX_ERROR;\n}'] |
35,479 | 0 | https://github.com/libav/libav/blob/4ab26cb4cc9af2ab2199105aa273aa23e1f27911/libavformat/flvdec.c/#L778 | static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
{
FLVContext *flv = s->priv_data;
int ret, i, type, size, flags, is_audio;
int64_t next, pos;
int64_t dts, pts = AV_NOPTS_VALUE;
int sample_rate = 0, channels = 0;
AVStream *st = NULL;
for(;;avio_skip(s->pb, 4)){
pos = avio_tell(s->pb);
type = avio_r8(s->pb);
size = avio_rb24(s->pb);
dts = avio_rb24(s->pb);
dts |= avio_r8(s->pb) << 24;
av_dlog(s, "type:%d, size:%d, dts:%"PRId64"\n", type, size, dts);
if (s->pb->eof_reached)
return AVERROR_EOF;
avio_skip(s->pb, 3);
flags = 0;
if (flv->validate_next < flv->validate_count) {
int64_t validate_pos = flv->validate_index[flv->validate_next].pos;
if (pos == validate_pos) {
if (FFABS(dts - flv->validate_index[flv->validate_next].dts) <=
VALIDATE_INDEX_TS_THRESH) {
flv->validate_next++;
} else {
clear_index_entries(s, validate_pos);
flv->validate_count = 0;
}
} else if (pos > validate_pos) {
clear_index_entries(s, validate_pos);
flv->validate_count = 0;
}
}
if(size == 0)
continue;
next= size + avio_tell(s->pb);
if (type == FLV_TAG_TYPE_AUDIO) {
is_audio=1;
flags = avio_r8(s->pb);
size--;
} else if (type == FLV_TAG_TYPE_VIDEO) {
is_audio=0;
flags = avio_r8(s->pb);
size--;
if ((flags & 0xf0) == 0x50)
goto skip;
} else {
if (type == FLV_TAG_TYPE_META && size > 13+1+4)
if (flv_read_metabody(s, next) > 0) {
return flv_data_packet(s, pkt, dts, next);
}
else
av_log(s, AV_LOG_DEBUG, "skipping flv packet: type %d, size %d, flags %d\n", type, size, flags);
skip:
avio_seek(s->pb, next, SEEK_SET);
continue;
}
if (!size)
continue;
for(i=0;i<s->nb_streams;i++) {
st = s->streams[i];
if (is_audio && st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
if (flv_same_audio_codec(st->codec, flags)) {
break;
}
} else
if (!is_audio && st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
if (flv_same_video_codec(st->codec, flags)) {
break;
}
}
}
if(i == s->nb_streams){
st = create_stream(s,
is_audio ? AVMEDIA_TYPE_AUDIO : AVMEDIA_TYPE_VIDEO);
s->ctx_flags &= ~AVFMTCTX_NOHEADER;
}
av_dlog(s, "%d %X %d \n", is_audio, flags, st->discard);
if( (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || is_audio))
||(st->discard >= AVDISCARD_BIDIR && ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && !is_audio))
|| st->discard >= AVDISCARD_ALL
){
avio_seek(s->pb, next, SEEK_SET);
continue;
}
if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY)
av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME);
break;
}
if(s->pb->seekable && (!s->duration || s->duration==AV_NOPTS_VALUE)){
int size;
const int64_t pos= avio_tell(s->pb);
const int64_t fsize= avio_size(s->pb);
avio_seek(s->pb, fsize-4, SEEK_SET);
size= avio_rb32(s->pb);
avio_seek(s->pb, fsize-3-size, SEEK_SET);
if(size == avio_rb24(s->pb) + 11){
uint32_t ts = avio_rb24(s->pb);
ts |= avio_r8(s->pb) << 24;
s->duration = ts * (int64_t)AV_TIME_BASE / 1000;
}
avio_seek(s->pb, pos, SEEK_SET);
}
if(is_audio){
int bits_per_coded_sample;
channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
sample_rate = (44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >> FLV_AUDIO_SAMPLERATE_OFFSET) >> 3);
bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
if(!st->codec->channels || !st->codec->sample_rate || !st->codec->bits_per_coded_sample) {
st->codec->channels = channels;
st->codec->sample_rate = sample_rate;
st->codec->bits_per_coded_sample = bits_per_coded_sample;
}
if(!st->codec->codec_id){
flv_set_audio_codec(s, st, st->codec, flags & FLV_AUDIO_CODECID_MASK);
flv->last_sample_rate = sample_rate = st->codec->sample_rate;
flv->last_channels = channels = st->codec->channels;
} else {
AVCodecContext ctx;
ctx.sample_rate = sample_rate;
flv_set_audio_codec(s, st, &ctx, flags & FLV_AUDIO_CODECID_MASK);
sample_rate = ctx.sample_rate;
}
}else{
size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK);
}
if (st->codec->codec_id == AV_CODEC_ID_AAC ||
st->codec->codec_id == AV_CODEC_ID_H264) {
int type = avio_r8(s->pb);
size--;
if (st->codec->codec_id == AV_CODEC_ID_H264) {
int32_t cts = (avio_rb24(s->pb)+0xff800000)^0xff800000;
pts = dts + cts;
if (cts < 0) {
flv->wrong_dts = 1;
av_log(s, AV_LOG_WARNING, "negative cts, previous timestamps might be wrong\n");
}
if (flv->wrong_dts)
dts = AV_NOPTS_VALUE;
}
if (type == 0) {
if (st->codec->extradata) {
if ((ret = flv_queue_extradata(flv, s->pb, is_audio, size)) < 0)
return ret;
ret = AVERROR(EAGAIN);
goto leave;
}
if ((ret = flv_get_extradata(s, st, size)) < 0)
return ret;
if (st->codec->codec_id == AV_CODEC_ID_AAC) {
MPEG4AudioConfig cfg;
avpriv_mpeg4audio_get_config(&cfg, st->codec->extradata,
st->codec->extradata_size * 8, 1);
st->codec->channels = cfg.channels;
if (cfg.ext_sample_rate)
st->codec->sample_rate = cfg.ext_sample_rate;
else
st->codec->sample_rate = cfg.sample_rate;
av_dlog(s, "mp4a config channels %d sample rate %d\n",
st->codec->channels, st->codec->sample_rate);
}
ret = AVERROR(EAGAIN);
goto leave;
}
}
if (!size) {
ret = AVERROR(EAGAIN);
goto leave;
}
ret= av_get_packet(s->pb, pkt, size);
if (ret < 0) {
return AVERROR(EIO);
}
pkt->size = ret;
pkt->dts = dts;
pkt->pts = pts == AV_NOPTS_VALUE ? dts : pts;
pkt->stream_index = st->index;
if (flv->new_extradata[is_audio]) {
uint8_t *side = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
flv->new_extradata_size[is_audio]);
if (side) {
memcpy(side, flv->new_extradata[is_audio],
flv->new_extradata_size[is_audio]);
av_freep(&flv->new_extradata[is_audio]);
flv->new_extradata_size[is_audio] = 0;
}
}
if (is_audio && (sample_rate != flv->last_sample_rate ||
channels != flv->last_channels)) {
flv->last_sample_rate = sample_rate;
flv->last_channels = channels;
ff_add_param_change(pkt, channels, 0, sample_rate, 0, 0);
}
if (is_audio || ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY))
pkt->flags |= AV_PKT_FLAG_KEY;
leave:
avio_skip(s->pb, 4);
return ret;
} | ['static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)\n{\n FLVContext *flv = s->priv_data;\n int ret, i, type, size, flags, is_audio;\n int64_t next, pos;\n int64_t dts, pts = AV_NOPTS_VALUE;\n int sample_rate = 0, channels = 0;\n AVStream *st = NULL;\n for(;;avio_skip(s->pb, 4)){\n pos = avio_tell(s->pb);\n type = avio_r8(s->pb);\n size = avio_rb24(s->pb);\n dts = avio_rb24(s->pb);\n dts |= avio_r8(s->pb) << 24;\n av_dlog(s, "type:%d, size:%d, dts:%"PRId64"\\n", type, size, dts);\n if (s->pb->eof_reached)\n return AVERROR_EOF;\n avio_skip(s->pb, 3);\n flags = 0;\n if (flv->validate_next < flv->validate_count) {\n int64_t validate_pos = flv->validate_index[flv->validate_next].pos;\n if (pos == validate_pos) {\n if (FFABS(dts - flv->validate_index[flv->validate_next].dts) <=\n VALIDATE_INDEX_TS_THRESH) {\n flv->validate_next++;\n } else {\n clear_index_entries(s, validate_pos);\n flv->validate_count = 0;\n }\n } else if (pos > validate_pos) {\n clear_index_entries(s, validate_pos);\n flv->validate_count = 0;\n }\n }\n if(size == 0)\n continue;\n next= size + avio_tell(s->pb);\n if (type == FLV_TAG_TYPE_AUDIO) {\n is_audio=1;\n flags = avio_r8(s->pb);\n size--;\n } else if (type == FLV_TAG_TYPE_VIDEO) {\n is_audio=0;\n flags = avio_r8(s->pb);\n size--;\n if ((flags & 0xf0) == 0x50)\n goto skip;\n } else {\n if (type == FLV_TAG_TYPE_META && size > 13+1+4)\n if (flv_read_metabody(s, next) > 0) {\n return flv_data_packet(s, pkt, dts, next);\n }\n else\n av_log(s, AV_LOG_DEBUG, "skipping flv packet: type %d, size %d, flags %d\\n", type, size, flags);\n skip:\n avio_seek(s->pb, next, SEEK_SET);\n continue;\n }\n if (!size)\n continue;\n for(i=0;i<s->nb_streams;i++) {\n st = s->streams[i];\n if (is_audio && st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {\n if (flv_same_audio_codec(st->codec, flags)) {\n break;\n }\n } else\n if (!is_audio && st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {\n if (flv_same_video_codec(st->codec, flags)) {\n break;\n }\n }\n }\n if(i == s->nb_streams){\n st = create_stream(s,\n is_audio ? AVMEDIA_TYPE_AUDIO : AVMEDIA_TYPE_VIDEO);\n s->ctx_flags &= ~AVFMTCTX_NOHEADER;\n }\n av_dlog(s, "%d %X %d \\n", is_audio, flags, st->discard);\n if( (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || is_audio))\n ||(st->discard >= AVDISCARD_BIDIR && ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && !is_audio))\n || st->discard >= AVDISCARD_ALL\n ){\n avio_seek(s->pb, next, SEEK_SET);\n continue;\n }\n if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY)\n av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME);\n break;\n }\n if(s->pb->seekable && (!s->duration || s->duration==AV_NOPTS_VALUE)){\n int size;\n const int64_t pos= avio_tell(s->pb);\n const int64_t fsize= avio_size(s->pb);\n avio_seek(s->pb, fsize-4, SEEK_SET);\n size= avio_rb32(s->pb);\n avio_seek(s->pb, fsize-3-size, SEEK_SET);\n if(size == avio_rb24(s->pb) + 11){\n uint32_t ts = avio_rb24(s->pb);\n ts |= avio_r8(s->pb) << 24;\n s->duration = ts * (int64_t)AV_TIME_BASE / 1000;\n }\n avio_seek(s->pb, pos, SEEK_SET);\n }\n if(is_audio){\n int bits_per_coded_sample;\n channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;\n sample_rate = (44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >> FLV_AUDIO_SAMPLERATE_OFFSET) >> 3);\n bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;\n if(!st->codec->channels || !st->codec->sample_rate || !st->codec->bits_per_coded_sample) {\n st->codec->channels = channels;\n st->codec->sample_rate = sample_rate;\n st->codec->bits_per_coded_sample = bits_per_coded_sample;\n }\n if(!st->codec->codec_id){\n flv_set_audio_codec(s, st, st->codec, flags & FLV_AUDIO_CODECID_MASK);\n flv->last_sample_rate = sample_rate = st->codec->sample_rate;\n flv->last_channels = channels = st->codec->channels;\n } else {\n AVCodecContext ctx;\n ctx.sample_rate = sample_rate;\n flv_set_audio_codec(s, st, &ctx, flags & FLV_AUDIO_CODECID_MASK);\n sample_rate = ctx.sample_rate;\n }\n }else{\n size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK);\n }\n if (st->codec->codec_id == AV_CODEC_ID_AAC ||\n st->codec->codec_id == AV_CODEC_ID_H264) {\n int type = avio_r8(s->pb);\n size--;\n if (st->codec->codec_id == AV_CODEC_ID_H264) {\n int32_t cts = (avio_rb24(s->pb)+0xff800000)^0xff800000;\n pts = dts + cts;\n if (cts < 0) {\n flv->wrong_dts = 1;\n av_log(s, AV_LOG_WARNING, "negative cts, previous timestamps might be wrong\\n");\n }\n if (flv->wrong_dts)\n dts = AV_NOPTS_VALUE;\n }\n if (type == 0) {\n if (st->codec->extradata) {\n if ((ret = flv_queue_extradata(flv, s->pb, is_audio, size)) < 0)\n return ret;\n ret = AVERROR(EAGAIN);\n goto leave;\n }\n if ((ret = flv_get_extradata(s, st, size)) < 0)\n return ret;\n if (st->codec->codec_id == AV_CODEC_ID_AAC) {\n MPEG4AudioConfig cfg;\n avpriv_mpeg4audio_get_config(&cfg, st->codec->extradata,\n st->codec->extradata_size * 8, 1);\n st->codec->channels = cfg.channels;\n if (cfg.ext_sample_rate)\n st->codec->sample_rate = cfg.ext_sample_rate;\n else\n st->codec->sample_rate = cfg.sample_rate;\n av_dlog(s, "mp4a config channels %d sample rate %d\\n",\n st->codec->channels, st->codec->sample_rate);\n }\n ret = AVERROR(EAGAIN);\n goto leave;\n }\n }\n if (!size) {\n ret = AVERROR(EAGAIN);\n goto leave;\n }\n ret= av_get_packet(s->pb, pkt, size);\n if (ret < 0) {\n return AVERROR(EIO);\n }\n pkt->size = ret;\n pkt->dts = dts;\n pkt->pts = pts == AV_NOPTS_VALUE ? dts : pts;\n pkt->stream_index = st->index;\n if (flv->new_extradata[is_audio]) {\n uint8_t *side = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,\n flv->new_extradata_size[is_audio]);\n if (side) {\n memcpy(side, flv->new_extradata[is_audio],\n flv->new_extradata_size[is_audio]);\n av_freep(&flv->new_extradata[is_audio]);\n flv->new_extradata_size[is_audio] = 0;\n }\n }\n if (is_audio && (sample_rate != flv->last_sample_rate ||\n channels != flv->last_channels)) {\n flv->last_sample_rate = sample_rate;\n flv->last_channels = channels;\n ff_add_param_change(pkt, channels, 0, sample_rate, 0, 0);\n }\n if (is_audio || ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY))\n pkt->flags |= AV_PKT_FLAG_KEY;\nleave:\n avio_skip(s->pb, 4);\n return ret;\n}'] |
35,480 | 0 | https://github.com/libav/libav/blob/e5b0fc170f85b00f7dd0ac514918fb5c95253d39/libavcodec/bitstream.h/#L237 | static inline void skip_remaining(BitstreamContext *bc, unsigned n)
{
#ifdef BITSTREAM_READER_LE
bc->bits >>= n;
#else
bc->bits <<= n;
#endif
bc->bits_left -= n;
} | ['static void tgq_decode_block(TgqContext *s, int16_t block[64], BitstreamContext *bc)\n{\n uint8_t *perm = s->scantable.permutated;\n int i, j, value;\n block[0] = bitstream_read_signed(bc, 8) * s->qtable[0];\n for (i = 1; i < 64;) {\n switch (bitstream_peek(bc, 3)) {\n case 4:\n block[perm[i++]] = 0;\n case 0:\n block[perm[i++]] = 0;\n bitstream_skip(bc, 3);\n break;\n case 5:\n case 1:\n bitstream_skip(bc, 2);\n value = bitstream_read(bc, 6);\n for (j = 0; j < value; j++)\n block[perm[i++]] = 0;\n break;\n case 6:\n bitstream_skip(bc, 3);\n block[perm[i]] = -s->qtable[perm[i]];\n i++;\n break;\n case 2:\n bitstream_skip(bc, 3);\n block[perm[i]] = s->qtable[perm[i]];\n i++;\n break;\n case 7:\n case 3:\n bitstream_skip(bc, 2);\n if (bitstream_peek(bc, 6) == 0x3F) {\n bitstream_skip(bc, 6);\n block[perm[i]] = bitstream_read_signed(bc, 8) * s->qtable[perm[i]];\n } else {\n block[perm[i]] = bitstream_read_signed(bc, 6) * s->qtable[perm[i]];\n }\n i++;\n break;\n }\n }\n block[0] += 128 << 4;\n}', 'static inline void bitstream_skip(BitstreamContext *bc, unsigned n)\n{\n if (n <= bc->bits_left)\n skip_remaining(bc, n);\n else {\n n -= bc->bits_left;\n skip_remaining(bc, bc->bits_left);\n if (n >= 64) {\n unsigned skip = n / 8;\n n -= skip * 8;\n bc->ptr += skip;\n }\n refill_64(bc);\n if (n)\n skip_remaining(bc, n);\n }\n}', 'static inline void skip_remaining(BitstreamContext *bc, unsigned n)\n{\n#ifdef BITSTREAM_READER_LE\n bc->bits >>= n;\n#else\n bc->bits <<= n;\n#endif\n bc->bits_left -= n;\n}', 'static inline unsigned bitstream_peek(BitstreamContext *bc, unsigned n)\n{\n if (n > bc->bits_left)\n refill_32(bc);\n return show_val(bc, n);\n}'] |
35,481 | 0 | https://github.com/openssl/openssl/blob/ea32151f7b9353f8906188d007c6893704ac17bb/crypto/lhash/lhash.c/#L123 | void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
{
unsigned long hash;
OPENSSL_LH_NODE *nn, **rn;
void *ret;
lh->error = 0;
rn = getrn(lh, data, &hash);
if (*rn == NULL) {
lh->num_no_delete++;
return (NULL);
} else {
nn = *rn;
*rn = nn->next;
ret = nn->data;
OPENSSL_free(nn);
lh->num_delete++;
}
lh->num_items--;
if ((lh->num_nodes > MIN_NODES) &&
(lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
contract(lh);
return (ret);
} | ['int OBJ_NAME_remove(const char *name, int type)\n{\n OBJ_NAME on, *ret;\n if (names_lh == NULL)\n return (0);\n type &= ~OBJ_NAME_ALIAS;\n on.name = name;\n on.type = type;\n ret = lh_OBJ_NAME_delete(names_lh, &on);\n if (ret != NULL) {\n if ((name_funcs_stack != NULL)\n && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) {\n sk_NAME_FUNCS_value(name_funcs_stack,\n ret->type)->free_func(ret->name, ret->type,\n ret->data);\n }\n OPENSSL_free(ret);\n return (1);\n } else\n return (0);\n}', 'void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)\n{\n unsigned long hash;\n OPENSSL_LH_NODE *nn, **rn;\n void *ret;\n lh->error = 0;\n rn = getrn(lh, data, &hash);\n if (*rn == NULL) {\n lh->num_no_delete++;\n return (NULL);\n } else {\n nn = *rn;\n *rn = nn->next;\n ret = nn->data;\n OPENSSL_free(nn);\n lh->num_delete++;\n }\n lh->num_items--;\n if ((lh->num_nodes > MIN_NODES) &&\n (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))\n contract(lh);\n return (ret);\n}'] |
35,482 | 0 | https://github.com/libav/libav/blob/f77f640b3035d357a6c6ffcea243c7ea0d8ebc67/libavformat/mpegts.c/#L686 | 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 void init_get_bits(GetBitContext *s, const uint8_t *buffer,\n int bit_size)\n{\n int buffer_size = (bit_size+7)>>3;\n if (buffer_size < 0 || bit_size < 0) {\n buffer_size = bit_size = 0;\n buffer = NULL;\n }\n s->buffer = buffer;\n s->size_in_bits = bit_size;\n#if !UNCHECKED_BITSTREAM_READER\n s->size_in_bits_plus8 = bit_size + 8;\n#endif\n s->buffer_end = buffer + buffer_size;\n s->index = 0;\n}', 'static inline unsigned int get_bits1(GetBitContext *s)\n{\n unsigned int index = s->index;\n uint8_t result = s->buffer[index>>3];\n#ifdef BITSTREAM_READER_LE\n result >>= index & 7;\n result &= 1;\n#else\n result <<= index & 7;\n result >>= 8 - 1;\n#endif\n#if !UNCHECKED_BITSTREAM_READER\n if (s->index < s->size_in_bits_plus8)\n#endif\n index++;\n s->index = index;\n return result;\n}'] |
35,483 | 0 | https://github.com/openssl/openssl/blob/f006217bb628d05a2d5b866ff252bd94e3477e1f/crypto/x509/x509_obj.c/#L96 | char *X509_NAME_oneline(X509_NAME *a, char *buf, int len)
{
X509_NAME_ENTRY *ne;
int i;
int n, lold, l, l1, l2, num, j, type;
const char *s;
char *p;
unsigned char *q;
BUF_MEM *b = NULL;
static const char hex[17] = "0123456789ABCDEF";
int gs_doit[4];
char tmp_buf[80];
#ifdef CHARSET_EBCDIC
char ebcdic_buf[1024];
#endif
if (buf == NULL) {
if ((b = BUF_MEM_new()) == NULL)
goto err;
if (!BUF_MEM_grow(b, 200))
goto err;
b->data[0] = '\0';
len = 200;
}
if (a == NULL) {
if (b) {
buf = b->data;
OPENSSL_free(b);
}
strncpy(buf, "NO X509_NAME", len);
buf[len - 1] = '\0';
return buf;
}
len--;
l = 0;
for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
ne = sk_X509_NAME_ENTRY_value(a->entries, i);
n = OBJ_obj2nid(ne->object);
if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
i2t_ASN1_OBJECT(tmp_buf, sizeof(tmp_buf), ne->object);
s = tmp_buf;
}
l1 = strlen(s);
type = ne->value->type;
num = ne->value->length;
q = ne->value->data;
#ifdef CHARSET_EBCDIC
if (type == V_ASN1_GENERALSTRING ||
type == V_ASN1_VISIBLESTRING ||
type == V_ASN1_PRINTABLESTRING ||
type == V_ASN1_TELETEXSTRING ||
type == V_ASN1_VISIBLESTRING || type == V_ASN1_IA5STRING) {
ascii2ebcdic(ebcdic_buf, q, (num > sizeof ebcdic_buf)
? sizeof ebcdic_buf : num);
q = ebcdic_buf;
}
#endif
if ((type == V_ASN1_GENERALSTRING) && ((num % 4) == 0)) {
gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 0;
for (j = 0; j < num; j++)
if (q[j] != 0)
gs_doit[j & 3] = 1;
if (gs_doit[0] | gs_doit[1] | gs_doit[2])
gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1;
else {
gs_doit[0] = gs_doit[1] = gs_doit[2] = 0;
gs_doit[3] = 1;
}
} else
gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1;
for (l2 = j = 0; j < num; j++) {
if (!gs_doit[j & 3])
continue;
l2++;
#ifndef CHARSET_EBCDIC
if ((q[j] < ' ') || (q[j] > '~'))
l2 += 3;
#else
if ((os_toascii[q[j]] < os_toascii[' ']) ||
(os_toascii[q[j]] > os_toascii['~']))
l2 += 3;
#endif
}
lold = l;
l += 1 + l1 + 1 + l2;
if (b != NULL) {
if (!BUF_MEM_grow(b, l + 1))
goto err;
p = &(b->data[lold]);
} else if (l > len) {
break;
} else
p = &(buf[lold]);
*(p++) = '/';
memcpy(p, s, (unsigned int)l1);
p += l1;
*(p++) = '=';
#ifndef CHARSET_EBCDIC
q = ne->value->data;
#endif
for (j = 0; j < num; j++) {
if (!gs_doit[j & 3])
continue;
#ifndef CHARSET_EBCDIC
n = q[j];
if ((n < ' ') || (n > '~')) {
*(p++) = '\\';
*(p++) = 'x';
*(p++) = hex[(n >> 4) & 0x0f];
*(p++) = hex[n & 0x0f];
} else
*(p++) = n;
#else
n = os_toascii[q[j]];
if ((n < os_toascii[' ']) || (n > os_toascii['~'])) {
*(p++) = '\\';
*(p++) = 'x';
*(p++) = hex[(n >> 4) & 0x0f];
*(p++) = hex[n & 0x0f];
} else
*(p++) = q[j];
#endif
}
*p = '\0';
}
if (b != NULL) {
p = b->data;
OPENSSL_free(b);
} else
p = buf;
if (i == 0)
*p = '\0';
return (p);
err:
X509err(X509_F_X509_NAME_ONELINE, ERR_R_MALLOC_FAILURE);
BUF_MEM_free(b);
return (NULL);
} | ['int X509_NAME_print(BIO *bp, X509_NAME *name, int obase)\n{\n char *s, *c, *b;\n int l, i;\n l = 80 - 2 - obase;\n b = X509_NAME_oneline(name, NULL, 0);\n if (!b)\n return 0;\n if (!*b) {\n OPENSSL_free(b);\n return 1;\n }\n s = b + 1;\n c = s;\n for (;;) {\n#ifndef CHARSET_EBCDIC\n if (((*s == \'/\') &&\n ((s[1] >= \'A\') && (s[1] <= \'Z\') && ((s[2] == \'=\') ||\n ((s[2] >= \'A\')\n && (s[2] <= \'Z\')\n && (s[3] == \'=\'))\n ))) || (*s == \'\\0\'))\n#else\n if (((*s == \'/\') &&\n (isupper(s[1]) && ((s[2] == \'=\') ||\n (isupper(s[2]) && (s[3] == \'=\'))\n ))) || (*s == \'\\0\'))\n#endif\n {\n i = s - c;\n if (BIO_write(bp, c, i) != i)\n goto err;\n c = s + 1;\n if (*s != \'\\0\') {\n if (BIO_write(bp, ", ", 2) != 2)\n goto err;\n }\n l--;\n }\n if (*s == \'\\0\')\n break;\n s++;\n l--;\n }\n OPENSSL_free(b);\n return 1;\n err:\n X509err(X509_F_X509_NAME_PRINT, ERR_R_BUF_LIB);\n OPENSSL_free(b);\n return 0;\n}', 'char *X509_NAME_oneline(X509_NAME *a, char *buf, int len)\n{\n X509_NAME_ENTRY *ne;\n int i;\n int n, lold, l, l1, l2, num, j, type;\n const char *s;\n char *p;\n unsigned char *q;\n BUF_MEM *b = NULL;\n static const char hex[17] = "0123456789ABCDEF";\n int gs_doit[4];\n char tmp_buf[80];\n#ifdef CHARSET_EBCDIC\n char ebcdic_buf[1024];\n#endif\n if (buf == NULL) {\n if ((b = BUF_MEM_new()) == NULL)\n goto err;\n if (!BUF_MEM_grow(b, 200))\n goto err;\n b->data[0] = \'\\0\';\n len = 200;\n }\n if (a == NULL) {\n if (b) {\n buf = b->data;\n OPENSSL_free(b);\n }\n strncpy(buf, "NO X509_NAME", len);\n buf[len - 1] = \'\\0\';\n return buf;\n }\n len--;\n l = 0;\n for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {\n ne = sk_X509_NAME_ENTRY_value(a->entries, i);\n n = OBJ_obj2nid(ne->object);\n if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {\n i2t_ASN1_OBJECT(tmp_buf, sizeof(tmp_buf), ne->object);\n s = tmp_buf;\n }\n l1 = strlen(s);\n type = ne->value->type;\n num = ne->value->length;\n q = ne->value->data;\n#ifdef CHARSET_EBCDIC\n if (type == V_ASN1_GENERALSTRING ||\n type == V_ASN1_VISIBLESTRING ||\n type == V_ASN1_PRINTABLESTRING ||\n type == V_ASN1_TELETEXSTRING ||\n type == V_ASN1_VISIBLESTRING || type == V_ASN1_IA5STRING) {\n ascii2ebcdic(ebcdic_buf, q, (num > sizeof ebcdic_buf)\n ? sizeof ebcdic_buf : num);\n q = ebcdic_buf;\n }\n#endif\n if ((type == V_ASN1_GENERALSTRING) && ((num % 4) == 0)) {\n gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 0;\n for (j = 0; j < num; j++)\n if (q[j] != 0)\n gs_doit[j & 3] = 1;\n if (gs_doit[0] | gs_doit[1] | gs_doit[2])\n gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1;\n else {\n gs_doit[0] = gs_doit[1] = gs_doit[2] = 0;\n gs_doit[3] = 1;\n }\n } else\n gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1;\n for (l2 = j = 0; j < num; j++) {\n if (!gs_doit[j & 3])\n continue;\n l2++;\n#ifndef CHARSET_EBCDIC\n if ((q[j] < \' \') || (q[j] > \'~\'))\n l2 += 3;\n#else\n if ((os_toascii[q[j]] < os_toascii[\' \']) ||\n (os_toascii[q[j]] > os_toascii[\'~\']))\n l2 += 3;\n#endif\n }\n lold = l;\n l += 1 + l1 + 1 + l2;\n if (b != NULL) {\n if (!BUF_MEM_grow(b, l + 1))\n goto err;\n p = &(b->data[lold]);\n } else if (l > len) {\n break;\n } else\n p = &(buf[lold]);\n *(p++) = \'/\';\n memcpy(p, s, (unsigned int)l1);\n p += l1;\n *(p++) = \'=\';\n#ifndef CHARSET_EBCDIC\n q = ne->value->data;\n#endif\n for (j = 0; j < num; j++) {\n if (!gs_doit[j & 3])\n continue;\n#ifndef CHARSET_EBCDIC\n n = q[j];\n if ((n < \' \') || (n > \'~\')) {\n *(p++) = \'\\\\\';\n *(p++) = \'x\';\n *(p++) = hex[(n >> 4) & 0x0f];\n *(p++) = hex[n & 0x0f];\n } else\n *(p++) = n;\n#else\n n = os_toascii[q[j]];\n if ((n < os_toascii[\' \']) || (n > os_toascii[\'~\'])) {\n *(p++) = \'\\\\\';\n *(p++) = \'x\';\n *(p++) = hex[(n >> 4) & 0x0f];\n *(p++) = hex[n & 0x0f];\n } else\n *(p++) = q[j];\n#endif\n }\n *p = \'\\0\';\n }\n if (b != NULL) {\n p = b->data;\n OPENSSL_free(b);\n } else\n p = buf;\n if (i == 0)\n *p = \'\\0\';\n return (p);\n err:\n X509err(X509_F_X509_NAME_ONELINE, ERR_R_MALLOC_FAILURE);\n BUF_MEM_free(b);\n return (NULL);\n}'] |
35,484 | 0 | https://github.com/libav/libav/blob/fd7f59639c43f0ab6b83ad2c1ceccafc553d7845/libavcodec/sonic.c/#L422 | static void modified_levinson_durbin(int *window, int window_entries,
int *out, int out_entries, int channels, int *tap_quant)
{
int i;
int *state = av_mallocz(4* window_entries);
memcpy(state, window, 4* window_entries);
for (i = 0; i < out_entries; i++)
{
int step = (i+1)*channels, k, j;
double xx = 0.0, xy = 0.0;
#if 1
int *x_ptr = &(window[step]), *state_ptr = &(state[0]);
j = window_entries - step;
for (;j>=0;j--,x_ptr++,state_ptr++)
{
double x_value = *x_ptr, state_value = *state_ptr;
xx += state_value*state_value;
xy += x_value*state_value;
}
#else
for (j = 0; j <= (window_entries - step); j++);
{
double stepval = window[step+j], stateval = window[j];
xx += stateval*stateval;
xy += stepval*stateval;
}
#endif
if (xx == 0.0)
k = 0;
else
k = (int)(floor(-xy/xx * (double)LATTICE_FACTOR / (double)(tap_quant[i]) + 0.5));
if (k > (LATTICE_FACTOR/tap_quant[i]))
k = LATTICE_FACTOR/tap_quant[i];
if (-k > (LATTICE_FACTOR/tap_quant[i]))
k = -(LATTICE_FACTOR/tap_quant[i]);
out[i] = k;
k *= tap_quant[i];
#if 1
x_ptr = &(window[step]);
state_ptr = &(state[0]);
j = window_entries - step;
for (;j>=0;j--,x_ptr++,state_ptr++)
{
int x_value = *x_ptr, state_value = *state_ptr;
*x_ptr = x_value + shift_down(k*state_value,LATTICE_SHIFT);
*state_ptr = state_value + shift_down(k*x_value, LATTICE_SHIFT);
}
#else
for (j=0; j <= (window_entries - step); j++)
{
int stepval = window[step+j], stateval=state[j];
window[step+j] += shift_down(k * stateval, LATTICE_SHIFT);
state[j] += shift_down(k * stepval, LATTICE_SHIFT);
}
#endif
}
av_free(state);
} | ['static void modified_levinson_durbin(int *window, int window_entries,\n int *out, int out_entries, int channels, int *tap_quant)\n{\n int i;\n int *state = av_mallocz(4* window_entries);\n memcpy(state, window, 4* window_entries);\n for (i = 0; i < out_entries; i++)\n {\n int step = (i+1)*channels, k, j;\n double xx = 0.0, xy = 0.0;\n#if 1\n int *x_ptr = &(window[step]), *state_ptr = &(state[0]);\n j = window_entries - step;\n for (;j>=0;j--,x_ptr++,state_ptr++)\n {\n double x_value = *x_ptr, state_value = *state_ptr;\n xx += state_value*state_value;\n xy += x_value*state_value;\n }\n#else\n for (j = 0; j <= (window_entries - step); j++);\n {\n double stepval = window[step+j], stateval = window[j];\n xx += stateval*stateval;\n xy += stepval*stateval;\n }\n#endif\n if (xx == 0.0)\n k = 0;\n else\n k = (int)(floor(-xy/xx * (double)LATTICE_FACTOR / (double)(tap_quant[i]) + 0.5));\n if (k > (LATTICE_FACTOR/tap_quant[i]))\n k = LATTICE_FACTOR/tap_quant[i];\n if (-k > (LATTICE_FACTOR/tap_quant[i]))\n k = -(LATTICE_FACTOR/tap_quant[i]);\n out[i] = k;\n k *= tap_quant[i];\n#if 1\n x_ptr = &(window[step]);\n state_ptr = &(state[0]);\n j = window_entries - step;\n for (;j>=0;j--,x_ptr++,state_ptr++)\n {\n int x_value = *x_ptr, state_value = *state_ptr;\n *x_ptr = x_value + shift_down(k*state_value,LATTICE_SHIFT);\n *state_ptr = state_value + shift_down(k*x_value, LATTICE_SHIFT);\n }\n#else\n for (j=0; j <= (window_entries - step); j++)\n {\n int stepval = window[step+j], stateval=state[j];\n window[step+j] += shift_down(k * stateval, LATTICE_SHIFT);\n state[j] += shift_down(k * stepval, LATTICE_SHIFT);\n }\n#endif\n }\n av_free(state);\n}', 'void *av_mallocz(unsigned int size)\n{\n void *ptr = av_malloc(size);\n if (ptr)\n memset(ptr, 0, size);\n return ptr;\n}', 'void *av_malloc(unsigned int size)\n{\n void *ptr = NULL;\n#ifdef CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-16) )\n return NULL;\n#ifdef CONFIG_MEMALIGN_HACK\n ptr = malloc(size+16);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&15) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif defined (HAVE_POSIX_MEMALIGN)\n posix_memalign(&ptr,16,size);\n#elif defined (HAVE_MEMALIGN)\n ptr = memalign(16,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}'] |
35,485 | 0 | https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/bn/bn_lib.c/#L896 | int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w)
{
return ((a->top == 1) && (a->d[0] == w)) || ((w == 0) && (a->top == 0));
} | ['int BN_GF2m_mod_sqrt_arr(BIGNUM *r, const BIGNUM *a, const int p[],\n BN_CTX *ctx)\n{\n int ret = 0;\n BIGNUM *u;\n bn_check_top(a);\n if (!p[0]) {\n BN_zero(r);\n return 1;\n }\n BN_CTX_start(ctx);\n if ((u = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (!BN_set_bit(u, p[0] - 1))\n goto err;\n ret = BN_GF2m_mod_exp_arr(r, a, u, p, ctx);\n bn_check_top(r);\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return (1);\n}', 'int BN_GF2m_mod_exp_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n const int p[], BN_CTX *ctx)\n{\n int ret = 0, i, n;\n BIGNUM *u;\n bn_check_top(a);\n bn_check_top(b);\n if (BN_is_zero(b))\n return (BN_one(r));\n if (BN_abs_is_word(b, 1))\n return (BN_copy(r, a) != NULL);\n BN_CTX_start(ctx);\n if ((u = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (!BN_GF2m_mod_arr(u, a, p))\n goto err;\n n = BN_num_bits(b) - 1;\n for (i = n - 1; i >= 0; i--) {\n if (!BN_GF2m_mod_sqr_arr(u, u, p, ctx))\n goto err;\n if (BN_is_bit_set(b, i)) {\n if (!BN_GF2m_mod_mul_arr(u, u, a, p, ctx))\n goto err;\n }\n }\n if (!BN_copy(r, u))\n goto err;\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\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}'] |
35,486 | 0 | https://github.com/openssl/openssl/blob/6bc62a620e715f7580651ca932eab052aa527886/crypto/bn/bn_ctx.c/#L268 | static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
} | ['ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,\n const BIGNUM *in_kinv, const BIGNUM *in_r,\n EC_KEY *eckey)\n{\n int ok = 0, i;\n BIGNUM *kinv = NULL, *s, *m = NULL;\n const BIGNUM *order, *ckinv;\n BN_CTX *ctx = NULL;\n const EC_GROUP *group;\n ECDSA_SIG *ret;\n const BIGNUM *priv_key;\n group = EC_KEY_get0_group(eckey);\n priv_key = EC_KEY_get0_private_key(eckey);\n if (group == NULL || priv_key == NULL) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_PASSED_NULL_PARAMETER);\n return NULL;\n }\n if (!EC_KEY_can_sign(eckey)) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);\n return NULL;\n }\n ret = ECDSA_SIG_new();\n if (ret == NULL) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n ret->r = BN_new();\n ret->s = BN_new();\n if (ret->r == NULL || ret->s == NULL) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n s = ret->s;\n if ((ctx = BN_CTX_new()) == NULL\n || (m = BN_new()) == NULL) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n order = EC_GROUP_get0_order(group);\n i = BN_num_bits(order);\n if (8 * dgst_len > i)\n dgst_len = (i + 7) / 8;\n if (!BN_bin2bn(dgst, dgst_len, m)) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);\n goto err;\n }\n do {\n if (in_kinv == NULL || in_r == NULL) {\n if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, dgst, dgst_len)) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_ECDSA_LIB);\n goto err;\n }\n ckinv = kinv;\n } else {\n ckinv = in_kinv;\n if (BN_copy(ret->r, in_r) == NULL) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n }\n if (!bn_to_mont_fixed_top(s, ret->r, group->mont_data, ctx)\n || !bn_mul_mont_fixed_top(s, s, priv_key, group->mont_data, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if (!bn_mod_add_fixed_top(s, s, m, order)) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if (!bn_to_mont_fixed_top(s, s, group->mont_data, ctx)\n || !BN_mod_mul_montgomery(s, s, ckinv, group->mont_data, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if (BN_is_zero(s)) {\n if (in_kinv != NULL && in_r != NULL) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_NEED_NEW_SETUP_VALUES);\n goto err;\n }\n } else {\n break;\n }\n } while (1);\n ok = 1;\n err:\n if (!ok) {\n ECDSA_SIG_free(ret);\n ret = NULL;\n }\n BN_CTX_free(ctx);\n BN_clear_free(m);\n BN_clear_free(kinv);\n return ret;\n}', 'static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in,\n BIGNUM **kinvp, BIGNUM **rp,\n const unsigned char *dgst, int dlen)\n{\n BN_CTX *ctx = NULL;\n BIGNUM *k = NULL, *r = NULL, *X = NULL;\n const BIGNUM *order;\n EC_POINT *tmp_point = NULL;\n const EC_GROUP *group;\n int ret = 0;\n int order_bits;\n if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);\n return 0;\n }\n if (!EC_KEY_can_sign(eckey)) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);\n return 0;\n }\n if ((ctx = ctx_in) == NULL) {\n if ((ctx = BN_CTX_new()) == NULL) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n }\n k = BN_new();\n r = BN_new();\n X = BN_new();\n if (k == NULL || r == NULL || X == NULL) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if ((tmp_point = EC_POINT_new(group)) == NULL) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);\n goto err;\n }\n order = EC_GROUP_get0_order(group);\n order_bits = BN_num_bits(order);\n if (!BN_set_bit(k, order_bits)\n || !BN_set_bit(r, order_bits)\n || !BN_set_bit(X, order_bits))\n goto err;\n do {\n do {\n if (dgst != NULL) {\n if (!BN_generate_dsa_nonce(k, order,\n EC_KEY_get0_private_key(eckey),\n dgst, dlen, ctx)) {\n ECerr(EC_F_ECDSA_SIGN_SETUP,\n EC_R_RANDOM_NUMBER_GENERATION_FAILED);\n goto err;\n }\n } else {\n if (!BN_priv_rand_range(k, order)) {\n ECerr(EC_F_ECDSA_SIGN_SETUP,\n EC_R_RANDOM_NUMBER_GENERATION_FAILED);\n goto err;\n }\n }\n } while (BN_is_zero(k));\n if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);\n goto err;\n }\n if (!EC_POINT_get_affine_coordinates(group, tmp_point, X, NULL, ctx)) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);\n goto err;\n }\n if (!BN_nnmod(r, X, order, ctx)) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);\n goto err;\n }\n } while (BN_is_zero(r));\n if (!ec_group_do_inverse_ord(group, k, k, ctx)) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);\n goto err;\n }\n BN_clear_free(*rp);\n BN_clear_free(*kinvp);\n *rp = r;\n *kinvp = k;\n ret = 1;\n err:\n if (!ret) {\n BN_clear_free(k);\n BN_clear_free(r);\n }\n if (ctx != ctx_in)\n BN_CTX_free(ctx);\n EC_POINT_free(tmp_point);\n BN_clear_free(X);\n return ret;\n}', 'int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n BN_MONT_CTX *mont, BN_CTX *ctx)\n{\n int ret = bn_mul_mont_fixed_top(r, a, b, mont, ctx);\n bn_correct_top(r);\n bn_check_top(r);\n return ret;\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}', '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_mul_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = 0;\n int top, al, bl;\n BIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n int i;\n#endif\n#ifdef BN_RECURSION\n BIGNUM *t = NULL;\n int j = 0, k;\n#endif\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(r);\n al = a->top;\n bl = b->top;\n if ((al == 0) || (bl == 0)) {\n BN_zero(r);\n return 1;\n }\n top = al + bl;\n BN_CTX_start(ctx);\n if ((r == a) || (r == b)) {\n if ((rr = BN_CTX_get(ctx)) == NULL)\n goto err;\n } else\n rr = r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n i = al - bl;\n#endif\n#ifdef BN_MUL_COMBA\n if (i == 0) {\n# if 0\n if (al == 4) {\n if (bn_wexpand(rr, 8) == NULL)\n goto err;\n rr->top = 8;\n bn_mul_comba4(rr->d, a->d, b->d);\n goto end;\n }\n# endif\n if (al == 8) {\n if (bn_wexpand(rr, 16) == NULL)\n goto err;\n rr->top = 16;\n bn_mul_comba8(rr->d, a->d, b->d);\n goto end;\n }\n }\n#endif\n#ifdef BN_RECURSION\n if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {\n if (i >= -1 && i <= 1) {\n if (i >= 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n }\n if (i == -1) {\n j = BN_num_bits_word((BN_ULONG)bl);\n }\n j = 1 << (j - 1);\n assert(j <= al || j <= bl);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (al > j || bl > j) {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d,\n j, al - j, bl - j, t->d);\n } else {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);\n }\n rr->top = top;\n goto end;\n }\n }\n#endif\n if (bn_wexpand(rr, top) == NULL)\n goto err;\n rr->top = top;\n bn_mul_normal(rr->d, a->d, al, b->d, bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n end:\n#endif\n rr->neg = a->neg ^ b->neg;\n rr->flags |= BN_FLG_FIXED_TOP;\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(r);\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_end()", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG("LEAVE BN_CTX_end()", ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}'] |
35,487 | 0 | https://github.com/openssl/openssl/blob/0a52d38b31ee56479c80509716c3bd46b764190a/crypto/bn/bn_asm.c/#L396 | 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);
for (;;)
{
t1=a[0]; t2=b[0];
r[0]=(t1-t2-c)&BN_MASK2;
if (t1 != t2) c=(t1 < t2);
if (--n <= 0) break;
t1=a[1]; t2=b[1];
r[1]=(t1-t2-c)&BN_MASK2;
if (t1 != t2) c=(t1 < t2);
if (--n <= 0) break;
t1=a[2]; t2=b[2];
r[2]=(t1-t2-c)&BN_MASK2;
if (t1 != t2) c=(t1 < t2);
if (--n <= 0) break;
t1=a[3]; t2=b[3];
r[3]=(t1-t2-c)&BN_MASK2;
if (t1 != t2) c=(t1 < t2);
if (--n <= 0) break;
a+=4;
b+=4;
r+=4;
}
return(c);
} | ['int BN_mod_exp2_mont(BIGNUM *rr, const BIGNUM *a1, const BIGNUM *p1,\n\tconst BIGNUM *a2, const BIGNUM *p2, const BIGNUM *m,\n\tBN_CTX *ctx, BN_MONT_CTX *in_mont)\n\t{\n\tint i,j,bits,b,bits1,bits2,ret=0,wpos1,wpos2,window1,window2,wvalue1,wvalue2;\n\tint r_is_one=1,ts1=0,ts2=0;\n\tBIGNUM *d,*r;\n\tconst BIGNUM *a_mod_m;\n\tBIGNUM val1[TABLE_SIZE], val2[TABLE_SIZE];\n\tBN_MONT_CTX *mont=NULL;\n\tbn_check_top(a1);\n\tbn_check_top(p1);\n\tbn_check_top(a2);\n\tbn_check_top(p2);\n\tbn_check_top(m);\n\tif (!(m->d[0] & 1))\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_EXP2_MONT,BN_R_CALLED_WITH_EVEN_MODULUS);\n\t\treturn(0);\n\t\t}\n\tbits1=BN_num_bits(p1);\n\tbits2=BN_num_bits(p2);\n\tif ((bits1 == 0) && (bits2 == 0))\n\t\t{\n\t\tret = BN_one(rr);\n\t\treturn ret;\n\t\t}\n\tbits=(bits1 > bits2)?bits1:bits2;\n\tBN_CTX_start(ctx);\n\td = BN_CTX_get(ctx);\n\tr = BN_CTX_get(ctx);\n\tif (d == NULL || 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\twindow1 = BN_window_bits_for_exponent_size(bits1);\n\twindow2 = BN_window_bits_for_exponent_size(bits2);\n\tBN_init(&val1[0]);\n\tts1=1;\n\tif (a1->neg || BN_ucmp(a1,m) >= 0)\n\t\t{\n\t\tif (!BN_mod(&(val1[0]),a1,m,ctx))\n\t\t\tgoto err;\n\t\ta_mod_m = &(val1[0]);\n\t\t}\n\telse\n\t\ta_mod_m = a1;\n\tif (BN_is_zero(a_mod_m))\n\t\t{\n\t\tret = BN_zero(rr);\n\t\tgoto err;\n\t\t}\n\tif (!BN_to_montgomery(&(val1[0]),a_mod_m,mont,ctx)) goto err;\n\tif (window1 > 1)\n\t\t{\n\t\tif (!BN_mod_mul_montgomery(d,&(val1[0]),&(val1[0]),mont,ctx)) goto err;\n\t\tj=1<<(window1-1);\n\t\tfor (i=1; i<j; i++)\n\t\t\t{\n\t\t\tBN_init(&(val1[i]));\n\t\t\tif (!BN_mod_mul_montgomery(&(val1[i]),&(val1[i-1]),d,mont,ctx))\n\t\t\t\tgoto err;\n\t\t\t}\n\t\tts1=i;\n\t\t}\n\tBN_init(&val2[0]);\n\tts2=1;\n\tif (a2->neg || BN_ucmp(a2,m) >= 0)\n\t\t{\n\t\tif (!BN_mod(&(val2[0]),a2,m,ctx))\n\t\t\tgoto err;\n\t\ta_mod_m = &(val2[0]);\n\t\t}\n\telse\n\t\ta_mod_m = a2;\n\tif (BN_is_zero(a_mod_m))\n\t\t{\n\t\tret = BN_zero(rr);\n\t\tgoto err;\n\t\t}\n\tif (!BN_to_montgomery(&(val2[0]),a_mod_m,mont,ctx)) goto err;\n\tif (window2 > 1)\n\t\t{\n\t\tif (!BN_mod_mul_montgomery(d,&(val2[0]),&(val2[0]),mont,ctx)) goto err;\n\t\tj=1<<(window2-1);\n\t\tfor (i=1; i<j; i++)\n\t\t\t{\n\t\t\tBN_init(&(val2[i]));\n\t\t\tif (!BN_mod_mul_montgomery(&(val2[i]),&(val2[i-1]),d,mont,ctx))\n\t\t\t\tgoto err;\n\t\t\t}\n\t\tts2=i;\n\t\t}\n\tr_is_one=1;\n\twvalue1=0;\n\twvalue2=0;\n\twpos1=0;\n\twpos2=0;\n\tif (!BN_to_montgomery(r,BN_value_one(),mont,ctx)) goto err;\n\tfor (b=bits-1; b>=0; b--)\n\t\t{\n\t\tif (!r_is_one)\n\t\t\t{\n\t\t\tif (!BN_mod_mul_montgomery(r,r,r,mont,ctx))\n\t\t\t\tgoto err;\n\t\t\t}\n\t\tif (!wvalue1)\n\t\t\tif (BN_is_bit_set(p1, b))\n\t\t\t\t{\n\t\t\t\ti = b-window1+1;\n\t\t\t\twhile (!BN_is_bit_set(p1, i))\n\t\t\t\t\ti++;\n\t\t\t\twpos1 = i;\n\t\t\t\twvalue1 = 1;\n\t\t\t\tfor (i = b-1; i >= wpos1; i--)\n\t\t\t\t\t{\n\t\t\t\t\twvalue1 <<= 1;\n\t\t\t\t\tif (BN_is_bit_set(p1, i))\n\t\t\t\t\t\twvalue1++;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\tif (!wvalue2)\n\t\t\tif (BN_is_bit_set(p2, b))\n\t\t\t\t{\n\t\t\t\ti = b-window2+1;\n\t\t\t\twhile (!BN_is_bit_set(p2, i))\n\t\t\t\t\ti++;\n\t\t\t\twpos2 = i;\n\t\t\t\twvalue2 = 1;\n\t\t\t\tfor (i = b-1; i >= wpos2; i--)\n\t\t\t\t\t{\n\t\t\t\t\twvalue2 <<= 1;\n\t\t\t\t\tif (BN_is_bit_set(p2, i))\n\t\t\t\t\t\twvalue2++;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\tif (wvalue1 && b == wpos1)\n\t\t\t{\n\t\t\tif (!BN_mod_mul_montgomery(r,r,&(val1[wvalue1>>1]),mont,ctx))\n\t\t\t\tgoto err;\n\t\t\twvalue1 = 0;\n\t\t\tr_is_one = 0;\n\t\t\t}\n\t\tif (wvalue2 && b == wpos2)\n\t\t\t{\n\t\t\tif (!BN_mod_mul_montgomery(r,r,&(val2[wvalue2>>1]),mont,ctx))\n\t\t\t\tgoto err;\n\t\t\twvalue2 = 0;\n\t\t\tr_is_one = 0;\n\t\t\t}\n\t\t}\n\tBN_from_montgomery(rr,r,mont,ctx);\n\tret=1;\nerr:\n\tif ((in_mont == NULL) && (mont != NULL)) BN_MONT_CTX_free(mont);\n\tBN_CTX_end(ctx);\n\tfor (i=0; i<ts1; i++)\n\t\tBN_clear_free(&(val1[i]));\n\tfor (i=0; i<ts2; i++)\n\t\tBN_clear_free(&(val2[i]));\n\treturn(ret);\n\t}', 'BIGNUM *BN_value_one(void)\n\t{\n\tstatic BN_ULONG data_one=1L;\n\tstatic BIGNUM const_one={&data_one,1,1,0};\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\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\tif (!BN_from_montgomery(r,tmp,mont,ctx)) goto err;\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;\n\tint j,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\tint sav_j =0;\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\tsav_j = j;\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 (al > j || bl > j)\n\t\t\t\t{\n\t\t\t\tbn_wexpand(t,k*4);\n\t\t\t\tbn_wexpand(rr,k*4);\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\tbn_wexpand(t,k*2);\n\t\t\t\tbn_wexpand(rr,k*2);\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\tbn_wexpand(tmp_bn,al);\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\tbn_wexpand(tmp_bn,bl);\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\tbn_wexpand(t,k*2);\n\t\t\t\tbn_wexpand(rr,k*2);\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\tbn_wexpand(t,k*4);\n\t\t\t\tbn_wexpand(rr,k*4);\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_fix_top(rr);\n\tif (r != rr) BN_copy(r,rr);\n\tret=1;\nerr:\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\tunsigned int c1,c2,neg,zero;\n\tBN_ULONG ln,lo,*p;\n# ifdef BN_COUNT\n\tfprintf(stderr," bn_mul_part_recursive (%d+%d) * (%d+%d)\\n",\n\t\ttna, n, tnb, n);\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\tzero=neg=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\t\tzero=1;\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\t\tzero=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\t\tzero=1;\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 < 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\tfor (;;)\n\t\t{\n\t\tt1=a[0]; t2=b[0];\n\t\tr[0]=(t1-t2-c)&BN_MASK2;\n\t\tif (t1 != t2) c=(t1 < t2);\n\t\tif (--n <= 0) break;\n\t\tt1=a[1]; t2=b[1];\n\t\tr[1]=(t1-t2-c)&BN_MASK2;\n\t\tif (t1 != t2) c=(t1 < t2);\n\t\tif (--n <= 0) break;\n\t\tt1=a[2]; t2=b[2];\n\t\tr[2]=(t1-t2-c)&BN_MASK2;\n\t\tif (t1 != t2) c=(t1 < t2);\n\t\tif (--n <= 0) break;\n\t\tt1=a[3]; t2=b[3];\n\t\tr[3]=(t1-t2-c)&BN_MASK2;\n\t\tif (t1 != t2) c=(t1 < t2);\n\t\tif (--n <= 0) break;\n\t\ta+=4;\n\t\tb+=4;\n\t\tr+=4;\n\t\t}\n\treturn(c);\n\t}'] |
35,488 | 0 | https://github.com/openssl/openssl/blob/9c46f4b9cd4912b61cb546c48b678488d7f26ed6/crypto/lhash/lhash.c/#L233 | void *lh_delete(_LHASH *lh, const void *data)
{
unsigned long hash;
LHASH_NODE *nn, **rn;
void *ret;
lh->error = 0;
rn = getrn(lh, data, &hash);
if (*rn == NULL) {
lh->num_no_delete++;
return (NULL);
} else {
nn = *rn;
*rn = nn->next;
ret = nn->data;
OPENSSL_free(nn);
lh->num_delete++;
}
lh->num_items--;
if ((lh->num_nodes > MIN_NODES) &&
(lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
contract(lh);
return (ret);
} | ['int dtls1_read_bytes(SSL *s, int type, unsigned char *buf, 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 (s->s3->rbuf.buf == NULL)\n if (!ssl3_setup_buffers(s))\n return (-1);\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->s3->rrec);\n if (s->state == SSL_ST_OK && rr->length == 0) {\n pitem *item;\n item = pqueue_pop(s->d1->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 ((rr->length == 0) || (s->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 rr->length = 0;\n goto start;\n }\n if (s->s3->change_cipher_spec\n && (rr->type != SSL3_RT_HANDSHAKE)) {\n if (dtls1_buffer_record(s, &(s->d1->buffered_app_data), rr->seq_num) <\n 0) {\n SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR);\n return -1;\n }\n rr->length = 0;\n goto start;\n }\n if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {\n rr->length = 0;\n s->rwstate = SSL_NOTHING;\n return (0);\n }\n if (type == rr->type) {\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 (len <= 0)\n return (len);\n if ((unsigned int)len > rr->length)\n n = rr->length;\n else\n n = (unsigned int)len;\n memcpy(buf, &(rr->data[rr->off]), n);\n if (!peek) {\n rr->length -= n;\n rr->off += n;\n if (rr->length == 0) {\n s->rstate = SSL_ST_READ_HEADER;\n rr->off = 0;\n }\n }\n#ifndef OPENSSL_NO_SCTP\n if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&\n rr->type == 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 (rr->type == SSL3_RT_HANDSHAKE) {\n dest_maxlen = sizeof s->d1->handshake_fragment;\n dest = s->d1->handshake_fragment;\n dest_len = &s->d1->handshake_fragment_len;\n } else if (rr->type == SSL3_RT_ALERT) {\n dest_maxlen = sizeof(s->d1->alert_fragment);\n dest = s->d1->alert_fragment;\n dest_len = &s->d1->alert_fragment_len;\n }\n#ifndef OPENSSL_NO_HEARTBEATS\n else if (rr->type == TLS1_RT_HEARTBEAT) {\n dtls1_process_heartbeat(s);\n rr->length = 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 (rr->type != SSL3_RT_CHANGE_CIPHER_SPEC) {\n if (rr->type == 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 (rr->length < dest_maxlen) {\n#ifdef DTLS1_AD_MISSING_HANDSHAKE_MESSAGE\n FIX ME\n#endif\n s->rstate = SSL_ST_READ_HEADER;\n rr->length = 0;\n goto start;\n }\n for (k = 0; k < dest_maxlen; k++) {\n dest[k] = rr->data[rr->off++];\n rr->length--;\n }\n *dest_len = dest_maxlen;\n }\n }\n if ((!s->server) &&\n (s->d1->handshake_fragment_len >= DTLS1_HM_HEADER_LENGTH) &&\n (s->d1->handshake_fragment[0] == SSL3_MT_HELLO_REQUEST) &&\n (s->session != NULL) && (s->session->cipher != NULL)) {\n s->d1->handshake_fragment_len = 0;\n if ((s->d1->handshake_fragment[1] != 0) ||\n (s->d1->handshake_fragment[2] != 0) ||\n (s->d1->handshake_fragment[3] != 0)) {\n al = SSL_AD_DECODE_ERROR;\n SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_BAD_HELLO_REQUEST);\n goto err;\n }\n if (s->msg_callback)\n s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,\n s->d1->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 (s->s3->rbuf.left == 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->d1->alert_fragment_len >= DTLS1_AL_HEADER_LENGTH) {\n int alert_level = s->d1->alert_fragment[0];\n int alert_descr = s->d1->alert_fragment[1];\n s->d1->alert_fragment_len = 0;\n if (s->msg_callback)\n s->msg_callback(0, s->version, SSL3_RT_ALERT,\n s->d1->alert_fragment, 2, s, 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 == 1) {\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->d1->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 == 2) {\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 rr->length = 0;\n return (0);\n }\n if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) {\n struct ccs_header_st ccs_hdr;\n unsigned int ccs_hdr_len = DTLS1_CCS_HEADER_LENGTH;\n dtls1_get_ccs_header(rr->data, &ccs_hdr);\n if (s->version == DTLS1_BAD_VER)\n ccs_hdr_len = 3;\n if ((rr->length != ccs_hdr_len) ||\n (rr->off != 0) || (rr->data[0] != SSL3_MT_CCS)) {\n i = SSL_AD_ILLEGAL_PARAMETER;\n SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_BAD_CHANGE_CIPHER_SPEC);\n goto err;\n }\n rr->length = 0;\n if (s->msg_callback)\n s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC,\n rr->data, 1, s, s->msg_callback_arg);\n if (!s->d1->change_cipher_spec_ok) {\n goto start;\n }\n s->d1->change_cipher_spec_ok = 0;\n s->s3->change_cipher_spec = 1;\n if (!ssl3_do_change_cipher_spec(s))\n goto err;\n dtls1_reset_seq_numbers(s, SSL3_CC_READ);\n if (s->version == DTLS1_BAD_VER)\n s->d1->handshake_read_seq++;\n#ifndef OPENSSL_NO_SCTP\n BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD, 1, NULL);\n#endif\n goto start;\n }\n if ((s->d1->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 (rr->epoch != s->d1->r_epoch) {\n rr->length = 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 rr->length = 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#if 0\n s->state = SSL_ST_BEFORE | (s->server)\n ? SSL_ST_ACCEPT : SSL_ST_CONNECT;\n#else\n s->state = s->server ? SSL_ST_ACCEPT : SSL_ST_CONNECT;\n#endif\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 (s->s3->rbuf.left == 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 (rr->type) {\n default:\n#ifndef OPENSSL_NO_TLS\n if (s->version == TLS1_VERSION) {\n rr->length = 0;\n goto start;\n }\n#endif\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 err:\n return (-1);\n}', 'int ssl3_send_alert(SSL *s, int level, int desc)\n{\n desc = s->method->ssl3_enc->alert_value(desc);\n if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION)\n desc = SSL_AD_HANDSHAKE_FAILURE;\n if (desc < 0)\n return -1;\n if ((level == SSL3_AL_FATAL) && (s->session != NULL))\n SSL_CTX_remove_session(s->ctx, s->session);\n s->s3->alert_dispatch = 1;\n s->s3->send_alert[0] = level;\n s->s3->send_alert[1] = desc;\n if (s->s3->wbuf.left == 0)\n return s->method->ssl_dispatch_alert(s);\n return -1;\n}', 'int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)\n{\n return remove_session_lock(ctx, c, 1);\n}', 'static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)\n{\n SSL_SESSION *r;\n int ret = 0;\n if ((c != NULL) && (c->session_id_length != 0)) {\n if (lck)\n CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);\n if ((r = lh_SSL_SESSION_retrieve(ctx->sessions, c)) == c) {\n ret = 1;\n r = lh_SSL_SESSION_delete(ctx->sessions, c);\n SSL_SESSION_list_remove(ctx, c);\n }\n if (lck)\n CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n if (ret) {\n r->not_resumable = 1;\n if (ctx->remove_session_cb != NULL)\n ctx->remove_session_cb(ctx, r);\n SSL_SESSION_free(r);\n }\n } else\n ret = 0;\n return (ret);\n}', 'void *lh_delete(_LHASH *lh, const void *data)\n{\n unsigned long hash;\n LHASH_NODE *nn, **rn;\n void *ret;\n lh->error = 0;\n rn = getrn(lh, data, &hash);\n if (*rn == NULL) {\n lh->num_no_delete++;\n return (NULL);\n } else {\n nn = *rn;\n *rn = nn->next;\n ret = nn->data;\n OPENSSL_free(nn);\n lh->num_delete++;\n }\n lh->num_items--;\n if ((lh->num_nodes > MIN_NODES) &&\n (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))\n contract(lh);\n return (ret);\n}'] |
35,489 | 0 | https://github.com/libav/libav/blob/e4e30256f87f177decf59b59e923d05ef64147df/libavcodec/ra144enc.c/#L217 | static int adaptive_cb_search(const int16_t *adapt_cb, float *work,
const float *coefs, float *data)
{
int i, best_vect;
float score, gain, best_score, best_gain;
float exc[BLOCKSIZE];
gain = best_score = 0;
for (i = BLOCKSIZE / 2; i <= BUFFERSIZE; i++) {
create_adapt_vect(exc, adapt_cb, i);
get_match_score(work, coefs, exc, NULL, NULL, data, &score, &gain);
if (score > best_score) {
best_score = score;
best_vect = i;
best_gain = gain;
}
}
if (!best_score)
return 0;
create_adapt_vect(exc, adapt_cb, best_vect);
ff_celp_lp_synthesis_filterf(work, coefs, exc, BLOCKSIZE, LPC_ORDER);
for (i = 0; i < BLOCKSIZE; i++)
data[i] -= best_gain * work[i];
return best_vect - BLOCKSIZE / 2 + 1;
} | ['static int adaptive_cb_search(const int16_t *adapt_cb, float *work,\n const float *coefs, float *data)\n{\n int i, best_vect;\n float score, gain, best_score, best_gain;\n float exc[BLOCKSIZE];\n gain = best_score = 0;\n for (i = BLOCKSIZE / 2; i <= BUFFERSIZE; i++) {\n create_adapt_vect(exc, adapt_cb, i);\n get_match_score(work, coefs, exc, NULL, NULL, data, &score, &gain);\n if (score > best_score) {\n best_score = score;\n best_vect = i;\n best_gain = gain;\n }\n }\n if (!best_score)\n return 0;\n create_adapt_vect(exc, adapt_cb, best_vect);\n ff_celp_lp_synthesis_filterf(work, coefs, exc, BLOCKSIZE, LPC_ORDER);\n for (i = 0; i < BLOCKSIZE; i++)\n data[i] -= best_gain * work[i];\n return best_vect - BLOCKSIZE / 2 + 1;\n}'] |
35,490 | 0 | https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/crypto/bn/bn_mul.c/#L728 | void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb)
{
BN_ULONG *rr;
#ifdef BN_COUNT
printf(" bn_mul_normal %d * %d\n",na,nb);
#endif
if (na < nb)
{
int itmp;
BN_ULONG *ltmp;
itmp=na; na=nb; nb=itmp;
ltmp=a; a=b; b=ltmp;
}
rr= &(r[na]);
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 DSA_is_prime(BIGNUM *w, void (*callback)(), char *cb_arg)\n\t{\n\tint ok= -1,j,i,n;\n\tBN_CTX *ctx=NULL,*ctx2=NULL;\n\tBIGNUM *w_1,*b,*m,*z,*tmp,*mont_1;\n\tint a;\n\tBN_MONT_CTX *mont=NULL;\n\tif (!BN_is_bit_set(w,0)) return(0);\n\tif ((ctx=BN_CTX_new()) == NULL) goto err;\n\tif ((ctx2=BN_CTX_new()) == NULL) goto err;\n\tif ((mont=BN_MONT_CTX_new()) == NULL) goto err;\n\tm= &(ctx2->bn[2]);\n\tb= &(ctx2->bn[3]);\n\tz= &(ctx2->bn[4]);\n\tw_1= &(ctx2->bn[5]);\n\ttmp= &(ctx2->bn[6]);\n\tmont_1= &(ctx2->bn[7]);\n\tn=50;\n\tif (!BN_sub(w_1,w,BN_value_one())) goto err;\n\tfor (a=1; !BN_is_bit_set(w_1,a); a++)\n\t\t;\n\tif (!BN_rshift(m,w_1,a)) goto err;\n\tBN_MONT_CTX_set(mont,w,ctx);\n\tBN_to_montgomery(mont_1,BN_value_one(),mont,ctx);\n\tBN_to_montgomery(w_1,w_1,mont,ctx);\n\tfor (i=1; i < n; i++)\n\t\t{\n\t\tBN_rand(b,BN_num_bits(w)-2 ,0,0);\n\t\tj=0;\n\t\tif (!BN_mod_exp_mont(z,b,m,w,ctx,mont)) goto err;\n\t\tif (!BN_to_montgomery(z,z,mont,ctx)) goto err;\n\t\tfor (;;)\n\t\t\t{\n\t\t\tif (((j == 0) && (BN_cmp(z,mont_1) == 0)) ||\n\t\t\t\t(BN_cmp(z,w_1) == 0))\n\t\t\t\tbreak;\n\t\t\tif ((j > 0) && (BN_cmp(z,mont_1) == 0))\n\t\t\t\t{\n\t\t\t\tok=0;\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tj++;\n\t\t\tif (j >= a)\n\t\t\t\t{\n\t\t\t\tok=0;\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif (!BN_mod_mul_montgomery(z,z,z,mont,ctx)) goto err;\n\t\t\tif (callback != NULL) callback(1,j,cb_arg);\n\t\t\t}\n\t\t}\n\tok=1;\nerr:\n\tif (ok == -1) DSAerr(DSA_F_DSA_IS_PRIME,ERR_R_BN_LIB);\n\tBN_CTX_free(ctx);\n\tBN_CTX_free(ctx2);\n\treturn(ok);\n\t}', 'int BN_MONT_CTX_set(BN_MONT_CTX *mont, BIGNUM *mod, BN_CTX *ctx)\n\t{\n\tBIGNUM Ri,*R;\n\tBN_init(&Ri);\n\tR= &(mont->RR);\n\tBN_copy(&(mont->N),mod);\n#ifdef BN_RECURSION_MONT\n\tif (mont->N.top < BN_MONT_CTX_SET_SIZE_WORD)\n#endif\n\t\t{\n\t\tBIGNUM tmod;\n\t\tBN_ULONG buf[2];\n\t\tmont->use_word=1;\n\t\tmont->ri=(BN_num_bits(mod)+(BN_BITS2-1))/BN_BITS2*BN_BITS2;\n\t\tBN_zero(R);\n\t\tBN_set_bit(R,BN_BITS2);\n\t\tbuf[0]=mod->d[0];\n\t\tbuf[1]=0;\n\t\ttmod.d=buf;\n\t\ttmod.top=1;\n\t\ttmod.max=mod->max;\n\t\ttmod.neg=mod->neg;\n\t\tif ((BN_mod_inverse(&Ri,R,&tmod,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tBN_lshift(&Ri,&Ri,BN_BITS2);\n\t\tif (!BN_is_zero(&Ri))\n\t\t\t{\n#if 1\n\t\t\tBN_sub_word(&Ri,1);\n#else\n\t\t\tBN_usub(&Ri,&Ri,BN_value_one());\n#endif\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tBN_set_word(&Ri,BN_MASK2);\n\t\t\t}\n\t\tBN_div(&Ri,NULL,&Ri,&tmod,ctx);\n\t\tmont->n0=Ri.d[0];\n\t\tBN_free(&Ri);\n\t\t}\n#ifdef BN_RECURSION_MONT\n\telse\n\t\t{\n\t\tmont->use_word=0;\n\t\tmont->ri=(BN_num_bits(mod)+(BN_BITS2-1))/BN_BITS2*BN_BITS2;\n#if 1\n\t\tBN_zero(R);\n\t\tBN_set_bit(R,mont->ri);\n#else\n\t\tBN_lshift(R,BN_value_one(),mont->ri);\n#endif\n\t\tif ((BN_mod_inverse(&Ri,R,mod,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tBN_lshift(&Ri,&Ri,mont->ri);\n#if 1\n\t\tBN_sub_word(&Ri,1);\n#else\n\t\tBN_usub(&Ri,&Ri,BN_value_one());\n#endif\n\t\tBN_div(&(mont->Ni),NULL,&Ri,mod,ctx);\n\t\tBN_free(&Ri);\n\t\t}\n#endif\n#if 1\n\tBN_zero(&(mont->RR));\n\tBN_set_bit(&(mont->RR),mont->ri*2);\n#else\n\tBN_lshift(mont->RR,BN_value_one(),mont->ri*2);\n#endif\n\tBN_mod(&(mont->RR),&(mont->RR),&(mont->N),ctx);\n\treturn(1);\nerr:\n\treturn(0);\n\t}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n\t{\n\tint i,n;\n\tif (bn_expand(a,sizeof(BN_ULONG)*8) == NULL) return(0);\n\tn=sizeof(BN_ULONG)/BN_BYTES;\n\ta->neg=0;\n\ta->top=0;\n\ta->d[0]=(BN_ULONG)w&BN_MASK2;\n\tif (a->d[0] != 0) a->top=1;\n\tfor (i=1; i<n; i++)\n\t\t{\n#ifndef SIXTY_FOUR_BIT\n\t\tw>>=BN_BITS4;\n\t\tw>>=BN_BITS4;\n#endif\n\t\ta->d[i]=(BN_ULONG)w&BN_MASK2;\n\t\tif (a->d[i] != 0) a->top=i+1;\n\t\t}\n\treturn(1);\n\t}', 'int BN_set_bit(BIGNUM *a, int n)\n\t{\n\tint i,j,k;\n\ti=n/BN_BITS2;\n\tj=n%BN_BITS2;\n\tif (a->top <= i)\n\t\t{\n\t\tif (bn_wexpand(a,i+1) == NULL) return(0);\n\t\tfor(k=a->top; k<i+1; k++)\n\t\t\ta->d[k]=0;\n\t\ta->top=i+1;\n\t\t}\n\ta->d[i]|=(1L<<j);\n\treturn(1);\n\t}', 'int BN_mod_mul_montgomery(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_MONT_CTX *mont,\n\t BN_CTX *ctx)\n\t{\n\tBIGNUM *tmp,*tmp2;\n tmp= &(ctx->bn[ctx->tos]);\n tmp2= &(ctx->bn[ctx->tos]);\n\tctx->tos+=2;\n\tbn_check_top(tmp);\n\tbn_check_top(tmp2);\n\tif (a == b)\n\t\t{\n#if 0\n\t\tbn_wexpand(tmp,a->top*2);\n\t\tbn_wexpand(tmp2,a->top*4);\n\t\tbn_sqr_recursive(tmp->d,a->d,a->top,tmp2->d);\n\t\ttmp->top=a->top*2;\n\t\tif (tmp->d[tmp->top-1] == 0)\n\t\t\ttmp->top--;\n#else\n\t\tif (!BN_sqr(tmp,a,ctx)) goto err;\n#endif\n\t\t}\n\telse\n\t\t{\n\t\tif (!BN_mul(tmp,a,b,ctx)) goto err;\n\t\t}\n\tif (!BN_from_montgomery(r,tmp,mont,ctx)) goto err;\n\tctx->tos-=2;\n\treturn(1);\nerr:\n\treturn(0);\n\t}', 'int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)\n\t{\n\tint top,al,bl;\n\tBIGNUM *rr;\n#ifdef BN_RECURSION\n\tBIGNUM *t;\n\tint i,j,k;\n#endif\n#ifdef BN_COUNT\nprintf("BN_mul %d * %d\\n",a->top,b->top);\n#endif\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tbn_check_top(r);\n\tal=a->top;\n\tbl=b->top;\n\tr->neg=a->neg^b->neg;\n\tif ((al == 0) || (bl == 0))\n\t\t{\n\t\tBN_zero(r);\n\t\treturn(1);\n\t\t}\n\ttop=al+bl;\n\tif ((r == a) || (r == b))\n\t\trr= &(ctx->bn[ctx->tos+1]);\n\telse\n\t\trr=r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\tif (al == bl)\n\t\t{\n# ifdef BN_MUL_COMBA\n if (al == 8)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,16) == NULL) return(0);\n\t\t\tr->top=16;\n\t\t\tbn_mul_comba8(rr->d,a->d,b->d);\n\t\t\tgoto end;\n\t\t\t}\n\t\telse\n# endif\n#ifdef BN_RECURSION\n\t\tif (al < BN_MULL_SIZE_NORMAL)\n#endif\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,top) == NULL) return(0);\n\t\t\trr->top=top;\n\t\t\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n\t\t\tgoto end;\n\t\t\t}\n# ifdef BN_RECURSION\n\t\tgoto symetric;\n# endif\n\t\t}\n#endif\n#ifdef BN_RECURSION\n\telse if ((al < BN_MULL_SIZE_NORMAL) || (bl < BN_MULL_SIZE_NORMAL))\n\t\t{\n\t\tif (bn_wexpand(rr,top) == NULL) return(0);\n\t\trr->top=top;\n\t\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n\t\tgoto end;\n\t\t}\n\telse\n\t\t{\n\t\ti=(al-bl);\n\t\tif ((i == 1) && !BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tbn_wexpand(b,al);\n\t\t\tb->d[bl]=0;\n\t\t\tbl++;\n\t\t\tgoto symetric;\n\t\t\t}\n\t\telse if ((i == -1) && !BN_get_flags(a,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tbn_wexpand(a,bl);\n\t\t\ta->d[al]=0;\n\t\t\tal++;\n\t\t\tgoto symetric;\n\t\t\t}\n\t\t}\n#endif\n\tif (bn_wexpand(rr,top) == NULL) return(0);\n\trr->top=top;\n\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n#ifdef BN_RECURSION\n\tif (0)\n\t\t{\nsymetric:\n\t\tj=BN_num_bits_word((BN_ULONG)al);\n\t\tj=1<<(j-1);\n\t\tk=j+j;\n\t\tt= &(ctx->bn[ctx->tos]);\n\t\tif (al == j)\n\t\t\t{\n\t\t\tbn_wexpand(t,k*2);\n\t\t\tbn_wexpand(rr,k*2);\n\t\t\tbn_mul_recursive(rr->d,a->d,b->d,al,t->d);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tbn_wexpand(a,k);\n\t\t\tbn_wexpand(b,k);\n\t\t\tbn_wexpand(t,k*4);\n\t\t\tbn_wexpand(rr,k*4);\n\t\t\tfor (i=a->top; i<k; i++)\n\t\t\t\ta->d[i]=0;\n\t\t\tfor (i=b->top; i<k; i++)\n\t\t\t\tb->d[i]=0;\n\t\t\tbn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d);\n\t\t\t}\n\t\trr->top=top;\n\t\t}\n#endif\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\nend:\n#endif\n\tbn_fix_top(rr);\n\tif (r != rr) BN_copy(r,rr);\n\treturn(1);\n\t}', 'void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int tn,\n\t int n, BN_ULONG *t)\n\t{\n\tint i,j,n2=n*2;\n\tunsigned int c1;\n\tBN_ULONG ln,lo,*p;\n#ifdef BN_COUNT\nprintf(" bn_mul_part_recursive %d * %d\\n",tn+n,tn+n);\n#endif\n\tif (n < 8)\n\t\t{\n\t\ti=tn+n;\n\t\tbn_mul_normal(r,a,i,b,i);\n\t\treturn;\n\t\t}\n\tbn_sub_words(t, a, &(a[n]),n);\n\tbn_sub_words(&(t[n]),b, &(b[n]),n);\n if (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]),tn,&(b[n]),tn);\n\t\tmemset(&(r[n2+tn*2]),0,sizeof(BN_ULONG)*(n2-tn*2));\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,p);\n\t\tbn_mul_recursive(r,a,b,n,p);\n\t\ti=n/2;\n\t\tj=tn-i;\n\t\tif (j == 0)\n\t\t\t{\n\t\t\tbn_mul_recursive(&(r[n2]),&(a[n]),&(b[n]),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\tj,i,p);\n\t\t\t\tmemset(&(r[n2+tn*2]),0,\n\t\t\t\t\tsizeof(BN_ULONG)*(n2-tn*2));\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 (tn < BN_MUL_RECURSIVE_SIZE_NORMAL)\n\t\t\t\t{\n\t\t\t\tbn_mul_normal(&(r[n2]),&(a[n]),tn,&(b[n]),tn);\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 < tn)\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\ttn-i,i,p);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\telse if (i == tn)\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,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\tc1-=(int)(bn_sub_words(&(t[n2]),t,&(t[n2]),n2));\n\tc1+=(int)(bn_add_words(&(r[n]),&(r[n]),&(t[n2]),n2));\n\tif (c1)\n\t\t{\n\t\tp= &(r[n+n2]);\n\t\tlo= *p;\n\t\tln=(lo+c1)&BN_MASK2;\n\t\t*p=ln;\n\t\tif (ln < c1)\n\t\t\t{\n\t\t\tdo\t{\n\t\t\t\tp++;\n\t\t\t\tlo= *p;\n\t\t\t\tln=(lo+1)&BN_MASK2;\n\t\t\t\t*p=ln;\n\t\t\t\t} while (ln == 0);\n\t\t\t}\n\t\t}\n\t}', 'void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb)\n\t{\n\tBN_ULONG *rr;\n#ifdef BN_COUNT\nprintf(" bn_mul_normal %d * %d\\n",na,nb);\n#endif\n\tif (na < nb)\n\t\t{\n\t\tint itmp;\n\t\tBN_ULONG *ltmp;\n\t\titmp=na; na=nb; nb=itmp;\n\t\tltmp=a; a=b; b=ltmp;\n\t\t}\n\trr= &(r[na]);\n\trr[0]=bn_mul_words(r,a,na,b[0]);\n\tfor (;;)\n\t\t{\n\t\tif (--nb <= 0) return;\n\t\trr[1]=bn_mul_add_words(&(r[1]),a,na,b[1]);\n\t\tif (--nb <= 0) return;\n\t\trr[2]=bn_mul_add_words(&(r[2]),a,na,b[2]);\n\t\tif (--nb <= 0) return;\n\t\trr[3]=bn_mul_add_words(&(r[3]),a,na,b[3]);\n\t\tif (--nb <= 0) return;\n\t\trr[4]=bn_mul_add_words(&(r[4]),a,na,b[4]);\n\t\trr+=4;\n\t\tr+=4;\n\t\tb+=4;\n\t\t}\n\t}'] |
35,491 | 0 | https://github.com/openssl/openssl/blob/1fac96e4d6484a517f2ebe99b72016726391723c/crypto/des/des_enc.c/#L144 | void des_encrypt(DES_LONG *data, des_key_schedule ks, int enc)
{
register DES_LONG l,r,t,u;
#ifdef DES_PTR
register const unsigned char *des_SP=(const unsigned char *)des_SPtrans;
#endif
#ifndef DES_UNROLL
register int i;
#endif
register DES_LONG *s;
r=data[0];
l=data[1];
IP(r,l);
r=ROTATE(r,29)&0xffffffffL;
l=ROTATE(l,29)&0xffffffffL;
s=(DES_LONG *)ks;
if (enc)
{
#ifdef DES_UNROLL
D_ENCRYPT(l,r, 0);
D_ENCRYPT(r,l, 2);
D_ENCRYPT(l,r, 4);
D_ENCRYPT(r,l, 6);
D_ENCRYPT(l,r, 8);
D_ENCRYPT(r,l,10);
D_ENCRYPT(l,r,12);
D_ENCRYPT(r,l,14);
D_ENCRYPT(l,r,16);
D_ENCRYPT(r,l,18);
D_ENCRYPT(l,r,20);
D_ENCRYPT(r,l,22);
D_ENCRYPT(l,r,24);
D_ENCRYPT(r,l,26);
D_ENCRYPT(l,r,28);
D_ENCRYPT(r,l,30);
#else
for (i=0; i<32; i+=8)
{
D_ENCRYPT(l,r,i+0);
D_ENCRYPT(r,l,i+2);
D_ENCRYPT(l,r,i+4);
D_ENCRYPT(r,l,i+6);
}
#endif
}
else
{
#ifdef DES_UNROLL
D_ENCRYPT(l,r,30);
D_ENCRYPT(r,l,28);
D_ENCRYPT(l,r,26);
D_ENCRYPT(r,l,24);
D_ENCRYPT(l,r,22);
D_ENCRYPT(r,l,20);
D_ENCRYPT(l,r,18);
D_ENCRYPT(r,l,16);
D_ENCRYPT(l,r,14);
D_ENCRYPT(r,l,12);
D_ENCRYPT(l,r,10);
D_ENCRYPT(r,l, 8);
D_ENCRYPT(l,r, 6);
D_ENCRYPT(r,l, 4);
D_ENCRYPT(l,r, 2);
D_ENCRYPT(r,l, 0);
#else
for (i=30; i>0; i-=8)
{
D_ENCRYPT(l,r,i-0);
D_ENCRYPT(r,l,i-2);
D_ENCRYPT(l,r,i-4);
D_ENCRYPT(r,l,i-6);
}
#endif
}
l=ROTATE(l,3)&0xffffffffL;
r=ROTATE(r,3)&0xffffffffL;
FP(r,l);
data[0]=l;
data[1]=r;
l=r=t=u=0;
} | ['int _des_crypt(char *buf, int len, struct desparams *desp)\n\t{\n\tdes_key_schedule ks;\n\tint enc;\n\tdes_set_key(&desp->des_key,ks);\n\tenc=(desp->des_dir == ENCRYPT)?DES_ENCRYPT:DES_DECRYPT;\n\tif (desp->des_mode == CBC)\n\t\tdes_ecb_encrypt((const_des_cblock *)desp->UDES.UDES_buf,\n\t\t\t\t(des_cblock *)desp->UDES.UDES_buf,ks,\n\t\t\t\tenc);\n\telse\n\t\t{\n\t\tdes_ncbc_encrypt(desp->UDES.UDES_buf,desp->UDES.UDES_buf,\n\t\t\t\tlen,ks,&desp->des_ivec,enc);\n#ifdef undef\n\t\ta=(char *)&(desp->UDES.UDES_buf[len-8]);\n\t\tb=(char *)&(desp->des_ivec[0]);\n\t\t*(a++)= *(b++); *(a++)= *(b++);\n\t\t*(a++)= *(b++); *(a++)= *(b++);\n\t\t*(a++)= *(b++); *(a++)= *(b++);\n\t\t*(a++)= *(b++); *(a++)= *(b++);\n#endif\n\t\t}\n\treturn(1);\n\t}', 'void des_ncbc_encrypt(const unsigned char *in, unsigned char *out, long length,\n\t des_key_schedule schedule, des_cblock *ivec, int enc)\n\t{\n\tregister DES_LONG tin0,tin1;\n\tregister DES_LONG tout0,tout1,xor0,xor1;\n\tregister long l=length;\n\tDES_LONG tin[2];\n\tunsigned char *iv;\n\tiv = &(*ivec)[0];\n\tif (enc)\n\t\t{\n\t\tc2l(iv,tout0);\n\t\tc2l(iv,tout1);\n\t\tfor (l-=8; l>=0; l-=8)\n\t\t\t{\n\t\t\tc2l(in,tin0);\n\t\t\tc2l(in,tin1);\n\t\t\ttin0^=tout0; tin[0]=tin0;\n\t\t\ttin1^=tout1; tin[1]=tin1;\n\t\t\tdes_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);\n\t\t\ttout0=tin[0]; l2c(tout0,out);\n\t\t\ttout1=tin[1]; l2c(tout1,out);\n\t\t\t}\n\t\tif (l != -8)\n\t\t\t{\n\t\t\tc2ln(in,tin0,tin1,l+8);\n\t\t\ttin0^=tout0; tin[0]=tin0;\n\t\t\ttin1^=tout1; tin[1]=tin1;\n\t\t\tdes_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);\n\t\t\ttout0=tin[0]; l2c(tout0,out);\n\t\t\ttout1=tin[1]; l2c(tout1,out);\n\t\t\t}\n\t\tiv = &(*ivec)[0];\n\t\tl2c(tout0,iv);\n\t\tl2c(tout1,iv);\n\t\t}\n\telse\n\t\t{\n\t\tc2l(iv,xor0);\n\t\tc2l(iv,xor1);\n\t\tfor (l-=8; l>=0; l-=8)\n\t\t\t{\n\t\t\tc2l(in,tin0); tin[0]=tin0;\n\t\t\tc2l(in,tin1); tin[1]=tin1;\n\t\t\tdes_encrypt((DES_LONG *)tin,schedule,DES_DECRYPT);\n\t\t\ttout0=tin[0]^xor0;\n\t\t\ttout1=tin[1]^xor1;\n\t\t\tl2c(tout0,out);\n\t\t\tl2c(tout1,out);\n\t\t\txor0=tin0;\n\t\t\txor1=tin1;\n\t\t\t}\n\t\tif (l != -8)\n\t\t\t{\n\t\t\tc2l(in,tin0); tin[0]=tin0;\n\t\t\tc2l(in,tin1); tin[1]=tin1;\n\t\t\tdes_encrypt((DES_LONG *)tin,schedule,DES_DECRYPT);\n\t\t\ttout0=tin[0]^xor0;\n\t\t\ttout1=tin[1]^xor1;\n\t\t\tl2cn(tout0,tout1,out,l+8);\n\t\t\txor0=tin0;\n\t\t\txor1=tin1;\n\t\t\t}\n\t\tiv = &(*ivec)[0];\n\t\tl2c(xor0,iv);\n\t\tl2c(xor1,iv);\n\t\t}\n\ttin0=tin1=tout0=tout1=xor0=xor1=0;\n\ttin[0]=tin[1]=0;\n\t}', 'void des_encrypt(DES_LONG *data, des_key_schedule ks, int enc)\n\t{\n\tregister DES_LONG l,r,t,u;\n#ifdef DES_PTR\n\tregister const unsigned char *des_SP=(const unsigned char *)des_SPtrans;\n#endif\n#ifndef DES_UNROLL\n\tregister int i;\n#endif\n\tregister DES_LONG *s;\n\tr=data[0];\n\tl=data[1];\n\tIP(r,l);\n\tr=ROTATE(r,29)&0xffffffffL;\n\tl=ROTATE(l,29)&0xffffffffL;\n\ts=(DES_LONG *)ks;\n\tif (enc)\n\t\t{\n#ifdef DES_UNROLL\n\t\tD_ENCRYPT(l,r, 0);\n\t\tD_ENCRYPT(r,l, 2);\n\t\tD_ENCRYPT(l,r, 4);\n\t\tD_ENCRYPT(r,l, 6);\n\t\tD_ENCRYPT(l,r, 8);\n\t\tD_ENCRYPT(r,l,10);\n\t\tD_ENCRYPT(l,r,12);\n\t\tD_ENCRYPT(r,l,14);\n\t\tD_ENCRYPT(l,r,16);\n\t\tD_ENCRYPT(r,l,18);\n\t\tD_ENCRYPT(l,r,20);\n\t\tD_ENCRYPT(r,l,22);\n\t\tD_ENCRYPT(l,r,24);\n\t\tD_ENCRYPT(r,l,26);\n\t\tD_ENCRYPT(l,r,28);\n\t\tD_ENCRYPT(r,l,30);\n#else\n\t\tfor (i=0; i<32; i+=8)\n\t\t\t{\n\t\t\tD_ENCRYPT(l,r,i+0);\n\t\t\tD_ENCRYPT(r,l,i+2);\n\t\t\tD_ENCRYPT(l,r,i+4);\n\t\t\tD_ENCRYPT(r,l,i+6);\n\t\t\t}\n#endif\n\t\t}\n\telse\n\t\t{\n#ifdef DES_UNROLL\n\t\tD_ENCRYPT(l,r,30);\n\t\tD_ENCRYPT(r,l,28);\n\t\tD_ENCRYPT(l,r,26);\n\t\tD_ENCRYPT(r,l,24);\n\t\tD_ENCRYPT(l,r,22);\n\t\tD_ENCRYPT(r,l,20);\n\t\tD_ENCRYPT(l,r,18);\n\t\tD_ENCRYPT(r,l,16);\n\t\tD_ENCRYPT(l,r,14);\n\t\tD_ENCRYPT(r,l,12);\n\t\tD_ENCRYPT(l,r,10);\n\t\tD_ENCRYPT(r,l, 8);\n\t\tD_ENCRYPT(l,r, 6);\n\t\tD_ENCRYPT(r,l, 4);\n\t\tD_ENCRYPT(l,r, 2);\n\t\tD_ENCRYPT(r,l, 0);\n#else\n\t\tfor (i=30; i>0; i-=8)\n\t\t\t{\n\t\t\tD_ENCRYPT(l,r,i-0);\n\t\t\tD_ENCRYPT(r,l,i-2);\n\t\t\tD_ENCRYPT(l,r,i-4);\n\t\t\tD_ENCRYPT(r,l,i-6);\n\t\t\t}\n#endif\n\t\t}\n\tl=ROTATE(l,3)&0xffffffffL;\n\tr=ROTATE(r,3)&0xffffffffL;\n\tFP(r,l);\n\tdata[0]=l;\n\tdata[1]=r;\n\tl=r=t=u=0;\n\t}'] |
35,492 | 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}'] |
35,493 | 0 | https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/bn/bn_lib.c/#L352 | static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
BN_ULONG *A, *a = NULL;
const BN_ULONG *B;
int i;
bn_check_top(b);
if (words > (INT_MAX / (4 * BN_BITS2))) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
return NULL;
}
if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
return (NULL);
}
if (BN_get_flags(b,BN_FLG_SECURE))
a = A = OPENSSL_secure_malloc(words * sizeof(*a));
else
a = A = OPENSSL_malloc(words * sizeof(*a));
if (A == NULL) {
BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
return (NULL);
}
#ifdef PURIFY
memset(a, 0, sizeof(*a) * words);
#endif
#if 1
B = b->d;
if (B != NULL) {
for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {
BN_ULONG a0, a1, a2, a3;
a0 = B[0];
a1 = B[1];
a2 = B[2];
a3 = B[3];
A[0] = a0;
A[1] = a1;
A[2] = a2;
A[3] = a3;
}
switch (b->top & 3) {
case 3:
A[2] = B[2];
case 2:
A[1] = B[1];
case 1:
A[0] = B[0];
case 0:
;
}
}
#else
memset(A, 0, sizeof(*A) * words);
memcpy(A, b->d, sizeof(b->d[0]) * b->top);
#endif
return (a);
} | ['int bn_probable_prime_dh_coprime(BIGNUM *rnd, int bits, BN_CTX *ctx)\n{\n int i;\n BIGNUM *offset_index;\n BIGNUM *offset_count;\n int ret = 0;\n OPENSSL_assert(bits > prime_multiplier_bits);\n BN_CTX_start(ctx);\n if ((offset_index = BN_CTX_get(ctx)) == NULL)\n goto err;\n if ((offset_count = BN_CTX_get(ctx)) == NULL)\n goto err;\n BN_add_word(offset_count, prime_offset_count);\n loop:\n if (!BN_rand(rnd, bits - prime_multiplier_bits, 0, 1))\n goto err;\n if (BN_is_bit_set(rnd, bits))\n goto loop;\n if (!BN_rand_range(offset_index, offset_count))\n goto err;\n BN_mul_word(rnd, prime_multiplier);\n BN_add_word(rnd, prime_offsets[BN_get_word(offset_index)]);\n for (i = first_prime_index; i < NUMPRIMES; i++) {\n if (BN_mod_word(rnd, (BN_ULONG)primes[i]) <= 1) {\n goto loop;\n }\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n bn_check_top(rnd);\n return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return (1);\n}', 'int BN_add_word(BIGNUM *a, BN_ULONG w)\n{\n BN_ULONG l;\n int i;\n bn_check_top(a);\n w &= BN_MASK2;\n if (!w)\n return 1;\n if (BN_is_zero(a))\n return BN_set_word(a, w);\n if (a->neg) {\n a->neg = 0;\n i = BN_sub_word(a, w);\n if (!BN_is_zero(a))\n a->neg = !(a->neg);\n return (i);\n }\n for (i = 0; w != 0 && i < a->top; i++) {\n a->d[i] = l = (a->d[i] + w) & BN_MASK2;\n w = (w > l) ? 1 : 0;\n }\n if (w && i == a->top) {\n if (bn_wexpand(a, a->top + 1) == NULL)\n return 0;\n a->top++;\n a->d[i] = w;\n }\n bn_check_top(a);\n return (1);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *A, *a = NULL;\n const BN_ULONG *B;\n int i;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return (NULL);\n }\n if (BN_get_flags(b,BN_FLG_SECURE))\n a = A = OPENSSL_secure_malloc(words * sizeof(*a));\n else\n a = A = OPENSSL_malloc(words * sizeof(*a));\n if (A == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n#ifdef PURIFY\n memset(a, 0, sizeof(*a) * words);\n#endif\n#if 1\n B = b->d;\n if (B != NULL) {\n for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {\n BN_ULONG a0, a1, a2, a3;\n a0 = B[0];\n a1 = B[1];\n a2 = B[2];\n a3 = B[3];\n A[0] = a0;\n A[1] = a1;\n A[2] = a2;\n A[3] = a3;\n }\n switch (b->top & 3) {\n case 3:\n A[2] = B[2];\n case 2:\n A[1] = B[1];\n case 1:\n A[0] = B[0];\n case 0:\n ;\n }\n }\n#else\n memset(A, 0, sizeof(*A) * words);\n memcpy(A, b->d, sizeof(b->d[0]) * b->top);\n#endif\n return (a);\n}'] |
35,494 | 0 | https://github.com/openssl/openssl/blob/9c4fe782607d8542c5f55ef1b5c687fef1da5d75/crypto/bn/bn_lib.c/#L615 | BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
{
unsigned int i,m;
unsigned int n;
BN_ULONG l;
BIGNUM *bn = NULL;
if (ret == NULL)
ret = bn = BN_new();
if (ret == NULL) return(NULL);
bn_check_top(ret);
l=0;
n=len;
if (n == 0)
{
ret->top=0;
return(ret);
}
i=((n-1)/BN_BYTES)+1;
m=((n-1)%(BN_BYTES));
if (bn_wexpand(ret, (int)i) == NULL)
{
if (bn) BN_free(bn);
return NULL;
}
ret->top=i;
ret->neg=0;
while (n--)
{
l=(l<<8L)| *(s++);
if (m-- == 0)
{
ret->d[--i]=l;
l=0;
m=BN_BYTES-1;
}
}
bn_correct_top(ret);
return(ret);
} | ['static EVP_PKEY *b2i_rsa(const unsigned char **in, unsigned int length,\n\t\t\t\t\t\tunsigned int bitlen, int ispub)\n\t{\n\tconst unsigned char *p = *in;\n\tEVP_PKEY *ret = NULL;\n\tRSA *rsa = NULL;\n\tunsigned int nbyte, hnbyte;\n\tnbyte = (bitlen + 7) >> 3;\n\thnbyte = (bitlen + 15) >> 4;\n\trsa = RSA_new();\n\tret = EVP_PKEY_new();\n\tif (!rsa || !ret)\n\t\tgoto memerr;\n\trsa->e = BN_new();\n\tif (!rsa->e)\n\t\tgoto memerr;\n\tif (!BN_set_word(rsa->e, read_ledword(&p)))\n\t\tgoto memerr;\n\tif (!read_lebn(&p, nbyte, &rsa->n))\n\t\tgoto memerr;\n\tif (!ispub)\n\t\t{\n\t\tif (!read_lebn(&p, hnbyte, &rsa->p))\n\t\t\tgoto memerr;\n\t\tif (!read_lebn(&p, hnbyte, &rsa->q))\n\t\t\tgoto memerr;\n\t\tif (!read_lebn(&p, hnbyte, &rsa->dmp1))\n\t\t\tgoto memerr;\n\t\tif (!read_lebn(&p, hnbyte, &rsa->dmq1))\n\t\t\tgoto memerr;\n\t\tif (!read_lebn(&p, hnbyte, &rsa->iqmp))\n\t\t\tgoto memerr;\n\t\tif (!read_lebn(&p, nbyte, &rsa->d))\n\t\t\tgoto memerr;\n\t\t}\n\tEVP_PKEY_set1_RSA(ret, rsa);\n\tRSA_free(rsa);\n\t*in = p;\n\treturn ret;\n\tmemerr:\n\tPEMerr(PEM_F_B2I_RSA, ERR_R_MALLOC_FAILURE);\n\tif (rsa)\n\t\tRSA_free(rsa);\n\tif (ret)\n\t\tEVP_PKEY_free(ret);\n\treturn NULL;\n\t}', 'static int read_lebn(const unsigned char **in, unsigned int nbyte, BIGNUM **r)\n\t{\n\tconst unsigned char *p;\n\tunsigned char *tmpbuf, *q;\n\tunsigned int i;\n\tp = *in + nbyte - 1;\n\ttmpbuf = OPENSSL_malloc(nbyte);\n\tif (!tmpbuf)\n\t\treturn 0;\n\tq = tmpbuf;\n\tfor (i = 0; i < nbyte; i++)\n\t\t*q++ = *p--;\n\t*r = BN_bin2bn(tmpbuf, nbyte, NULL);\n\tOPENSSL_free(tmpbuf);\n\tif (*r)\n\t\t{\n\t\t*in += nbyte;\n\t\treturn 1;\n\t\t}\n\telse\n\t\treturn 0;\n\t}', 'BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)\n\t{\n\tunsigned int i,m;\n\tunsigned int n;\n\tBN_ULONG l;\n\tBIGNUM *bn = NULL;\n\tif (ret == NULL)\n\t\tret = bn = BN_new();\n\tif (ret == NULL) return(NULL);\n\tbn_check_top(ret);\n\tl=0;\n\tn=len;\n\tif (n == 0)\n\t\t{\n\t\tret->top=0;\n\t\treturn(ret);\n\t\t}\n\ti=((n-1)/BN_BYTES)+1;\n\tm=((n-1)%(BN_BYTES));\n\tif (bn_wexpand(ret, (int)i) == NULL)\n\t\t{\n\t\tif (bn) BN_free(bn);\n\t\treturn NULL;\n\t\t}\n\tret->top=i;\n\tret->neg=0;\n\twhile (n--)\n\t\t{\n\t\tl=(l<<8L)| *(s++);\n\t\tif (m-- == 0)\n\t\t\t{\n\t\t\tret->d[--i]=l;\n\t\t\tl=0;\n\t\t\tm=BN_BYTES-1;\n\t\t\t}\n\t\t}\n\tbn_correct_top(ret);\n\treturn(ret);\n\t}'] |
35,495 | 1 | https://github.com/apache/httpd/blob/8469fc3f69cf297ceff280de3559a660f621cb5a/server/protocol.c/#L400 | AP_DECLARE(apr_status_t) ap_rgetline_core(char **s, apr_size_t n,
apr_size_t *read, request_rec *r,
int flags, apr_bucket_brigade *bb)
{
apr_status_t rv;
apr_bucket *e;
apr_size_t bytes_handled = 0, current_alloc = 0;
char *pos, *last_char = *s;
int do_alloc = (*s == NULL), saw_eos = 0;
int fold = flags & AP_GETLINE_FOLD;
int crlf = flags & AP_GETLINE_CRLF;
if (last_char)
*last_char = '\0';
for (;;) {
apr_brigade_cleanup(bb);
rv = ap_get_brigade(r->proto_input_filters, bb, AP_MODE_GETLINE,
APR_BLOCK_READ, 0);
if (rv != APR_SUCCESS) {
return rv;
}
if (APR_BRIGADE_EMPTY(bb)) {
return APR_EGENERAL;
}
for (e = APR_BRIGADE_FIRST(bb);
e != APR_BRIGADE_SENTINEL(bb);
e = APR_BUCKET_NEXT(e))
{
const char *str;
apr_size_t len;
if (APR_BUCKET_IS_EOS(e)) {
saw_eos = 1;
break;
}
rv = apr_bucket_read(e, &str, &len, APR_BLOCK_READ);
if (rv != APR_SUCCESS) {
return rv;
}
if (len == 0) {
continue;
}
if (n < bytes_handled + len) {
*read = bytes_handled;
if (*s) {
if (bytes_handled > 0) {
(*s)[bytes_handled-1] = '\0';
}
else {
(*s)[0] = '\0';
}
}
return APR_ENOSPC;
}
if (do_alloc) {
if (!*s) {
current_alloc = len;
*s = apr_palloc(r->pool, current_alloc);
}
else if (bytes_handled + len > current_alloc) {
apr_size_t new_size = current_alloc * 2;
char *new_buffer;
if (bytes_handled + len > new_size) {
new_size = (bytes_handled + len) * 2;
}
new_buffer = apr_palloc(r->pool, new_size);
memcpy(new_buffer, *s, bytes_handled);
current_alloc = new_size;
*s = new_buffer;
}
}
pos = *s + bytes_handled;
memcpy(pos, str, len);
last_char = pos + len - 1;
bytes_handled += len;
}
if (last_char && (*last_char == APR_ASCII_LF)) {
break;
}
}
if (crlf && (last_char <= *s || last_char[-1] != APR_ASCII_CR)) {
*last_char = '\0';
bytes_handled = last_char - *s;
*read = bytes_handled;
return APR_EINVAL;
}
if (last_char > *s && last_char[-1] == APR_ASCII_CR) {
last_char--;
}
*last_char = '\0';
bytes_handled = last_char - *s;
if (fold && bytes_handled && !saw_eos) {
for (;;) {
const char *str;
apr_size_t len;
char c;
apr_brigade_cleanup(bb);
rv = ap_get_brigade(r->proto_input_filters, bb, AP_MODE_SPECULATIVE,
APR_BLOCK_READ, 1);
if (rv != APR_SUCCESS) {
return rv;
}
if (APR_BRIGADE_EMPTY(bb)) {
break;
}
e = APR_BRIGADE_FIRST(bb);
if (APR_BUCKET_IS_EOS(e)) {
break;
}
rv = apr_bucket_read(e, &str, &len, APR_BLOCK_READ);
if (rv != APR_SUCCESS) {
apr_brigade_cleanup(bb);
return rv;
}
c = *str;
if (c == APR_ASCII_BLANK || c == APR_ASCII_TAB) {
if (bytes_handled >= n) {
*read = n;
(*s)[n-1] = '\0';
return APR_ENOSPC;
}
else {
apr_size_t next_size, next_len;
char *tmp;
if (do_alloc) {
tmp = NULL;
}
else {
tmp = last_char;
}
next_size = n - bytes_handled;
rv = ap_rgetline_core(&tmp, next_size,
&next_len, r, 0, bb);
if (rv != APR_SUCCESS) {
return rv;
}
if (do_alloc && next_len > 0) {
char *new_buffer;
apr_size_t new_size = bytes_handled + next_len + 1;
new_buffer = apr_palloc(r->pool, new_size);
memcpy(new_buffer, *s, bytes_handled);
memcpy(new_buffer + bytes_handled, tmp, next_len + 1);
*s = new_buffer;
}
last_char += next_len;
bytes_handled += next_len;
}
}
else {
break;
}
}
}
*read = bytes_handled;
if (strlen(*s) < bytes_handled) {
return APR_EINVAL;
}
return APR_SUCCESS;
} | ['static apr_status_t read_chunked_trailers(http_ctx_t *ctx, ap_filter_t *f,\n apr_bucket_brigade *b, int merge)\n{\n int rv;\n apr_bucket *e;\n request_rec *r = f->r;\n apr_table_t *saved_headers_in = r->headers_in;\n int saved_status = r->status;\n r->status = HTTP_OK;\n r->headers_in = r->trailers_in;\n apr_table_clear(r->headers_in);\n ap_get_mime_headers(r);\n if(r->status == HTTP_OK) {\n r->status = saved_status;\n e = apr_bucket_eos_create(f->c->bucket_alloc);\n APR_BRIGADE_INSERT_TAIL(b, e);\n ctx->eos_sent = 1;\n rv = APR_SUCCESS;\n }\n else {\n const char *error_notes = apr_table_get(r->notes,\n "error-notes");\n ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(02656)\n "Error while reading HTTP trailer: %i%s%s",\n r->status, error_notes ? ": " : "",\n error_notes ? error_notes : "");\n rv = APR_EINVAL;\n }\n if(!merge) {\n r->headers_in = saved_headers_in;\n }\n else {\n r->headers_in = apr_table_overlay(r->pool, saved_headers_in,\n r->trailers_in);\n }\n return rv;\n}', 'AP_DECLARE(void) ap_get_mime_headers(request_rec *r)\n{\n apr_bucket_brigade *tmp_bb;\n tmp_bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);\n ap_get_mime_headers_core(r, tmp_bb);\n apr_brigade_destroy(tmp_bb);\n}', 'AP_DECLARE(void) ap_get_mime_headers_core(request_rec *r, apr_bucket_brigade *bb)\n{\n char *last_field = NULL;\n apr_size_t last_len = 0;\n apr_size_t alloc_len = 0;\n char *field;\n char *value;\n apr_size_t len;\n int fields_read = 0;\n char *tmp_field;\n core_server_config *conf = ap_get_core_module_config(r->server->module_config);\n int strict = (conf->http_conformance != AP_HTTP_CONFORMANCE_UNSAFE);\n while(1) {\n apr_status_t rv;\n field = NULL;\n rv = ap_rgetline(&field, r->server->limit_req_fieldsize + 2,\n &len, r, strict ? AP_GETLINE_CRLF : 0, bb);\n if (rv != APR_SUCCESS) {\n if (APR_STATUS_IS_TIMEUP(rv)) {\n r->status = HTTP_REQUEST_TIME_OUT;\n }\n else {\n r->status = HTTP_BAD_REQUEST;\n }\n if (rv == APR_ENOSPC) {\n apr_table_setn(r->notes, "error-notes",\n "Size of a request header field "\n "exceeds server limit.");\n ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00561)\n "Request header exceeds LimitRequestFieldSize%s"\n "%.*s",\n (field && *field) ? ": " : "",\n (field) ? field_name_len(field) : 0,\n (field) ? field : "");\n }\n return;\n }\n while (len > 1 && (field[len-1] == \'\\t\' || field[len-1] == \' \')) {\n field[--len] = \'\\0\';\n }\n if (*field == \'\\t\' || *field == \' \') {\n apr_size_t fold_len;\n if (last_field == NULL) {\n r->status = HTTP_BAD_REQUEST;\n ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03442)\n "Line folding encountered before first"\n " header line");\n return;\n }\n if (field[1] == \'\\0\') {\n r->status = HTTP_BAD_REQUEST;\n ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03443)\n "Empty folded line encountered");\n return;\n }\n while (field[1] == \'\\t\' || field[1] == \' \') {\n ++field; --len;\n }\n fold_len = last_len + len + 1;\n if (fold_len >= (apr_size_t)(r->server->limit_req_fieldsize)) {\n r->status = HTTP_BAD_REQUEST;\n apr_table_setn(r->notes, "error-notes",\n "Size of a request header field "\n "exceeds server limit.");\n ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00562)\n "Request header exceeds LimitRequestFieldSize "\n "after folding: %.*s",\n field_name_len(last_field), last_field);\n return;\n }\n if (fold_len > alloc_len) {\n char *fold_buf;\n alloc_len += alloc_len;\n if (fold_len > alloc_len) {\n alloc_len = fold_len;\n }\n fold_buf = (char *)apr_palloc(r->pool, alloc_len);\n memcpy(fold_buf, last_field, last_len);\n last_field = fold_buf;\n }\n memcpy(last_field + last_len, field, len +1);\n last_field[last_len] = \' \';\n last_len += len;\n continue;\n }\n else if (last_field != NULL) {\n if (r->server->limit_req_fields\n && (++fields_read > r->server->limit_req_fields)) {\n r->status = HTTP_BAD_REQUEST;\n apr_table_setn(r->notes, "error-notes",\n "The number of request header fields "\n "exceeds this server\'s limit.");\n ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00563)\n "Number of request headers exceeds "\n "LimitRequestFields");\n return;\n }\n if (!strict)\n {\n if (!(value = strchr(last_field, \':\'))) {\n r->status = HTTP_BAD_REQUEST;\n ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00564)\n "Request header field is missing \':\' "\n "separator: %.*s", (int)LOG_NAME_MAX_LEN,\n last_field);\n return;\n }\n if (value == last_field) {\n r->status = HTTP_BAD_REQUEST;\n ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03453)\n "Request header field name was empty");\n return;\n }\n *value++ = \'\\0\';\n if (strpbrk(last_field, "\\t\\n\\v\\f\\r ")) {\n r->status = HTTP_BAD_REQUEST;\n ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03452)\n "Request header field name presented"\n " invalid whitespace");\n return;\n }\n while (*value == \' \' || *value == \'\\t\') {\n ++value;\n }\n if (strpbrk(value, "\\n\\v\\f\\r")) {\n r->status = HTTP_BAD_REQUEST;\n ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03451)\n "Request header field value presented"\n " bad whitespace");\n return;\n }\n }\n else\n {\n value = (char *)ap_scan_http_token(last_field);\n if ((value == last_field) || *value != \':\') {\n r->status = HTTP_BAD_REQUEST;\n ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02426)\n "Request header field name is malformed: "\n "%.*s", (int)LOG_NAME_MAX_LEN, last_field);\n return;\n }\n *value++ = \'\\0\';\n while (*value == \' \' || *value == \'\\t\') {\n ++value;\n }\n tmp_field = (char *)ap_scan_http_field_content(value);\n if (*tmp_field != \'\\0\') {\n r->status = HTTP_BAD_REQUEST;\n ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02427)\n "Request header value is malformed: "\n "%.*s", (int)LOG_NAME_MAX_LEN, value);\n return;\n }\n }\n apr_table_addn(r->headers_in, last_field, value);\n }\n if (len == 0) {\n break;\n }\n alloc_len = 0;\n last_field = field;\n last_len = len;\n }\n apr_table_compress(r->headers_in, APR_OVERLAP_TABLES_MERGE);\n apr_table_do(table_do_fn_check_lengths, r, r->headers_in, NULL);\n}', "AP_DECLARE(apr_status_t) ap_rgetline_core(char **s, apr_size_t n,\n apr_size_t *read, request_rec *r,\n int flags, apr_bucket_brigade *bb)\n{\n apr_status_t rv;\n apr_bucket *e;\n apr_size_t bytes_handled = 0, current_alloc = 0;\n char *pos, *last_char = *s;\n int do_alloc = (*s == NULL), saw_eos = 0;\n int fold = flags & AP_GETLINE_FOLD;\n int crlf = flags & AP_GETLINE_CRLF;\n if (last_char)\n *last_char = '\\0';\n for (;;) {\n apr_brigade_cleanup(bb);\n rv = ap_get_brigade(r->proto_input_filters, bb, AP_MODE_GETLINE,\n APR_BLOCK_READ, 0);\n if (rv != APR_SUCCESS) {\n return rv;\n }\n if (APR_BRIGADE_EMPTY(bb)) {\n return APR_EGENERAL;\n }\n for (e = APR_BRIGADE_FIRST(bb);\n e != APR_BRIGADE_SENTINEL(bb);\n e = APR_BUCKET_NEXT(e))\n {\n const char *str;\n apr_size_t len;\n if (APR_BUCKET_IS_EOS(e)) {\n saw_eos = 1;\n break;\n }\n rv = apr_bucket_read(e, &str, &len, APR_BLOCK_READ);\n if (rv != APR_SUCCESS) {\n return rv;\n }\n if (len == 0) {\n continue;\n }\n if (n < bytes_handled + len) {\n *read = bytes_handled;\n if (*s) {\n if (bytes_handled > 0) {\n (*s)[bytes_handled-1] = '\\0';\n }\n else {\n (*s)[0] = '\\0';\n }\n }\n return APR_ENOSPC;\n }\n if (do_alloc) {\n if (!*s) {\n current_alloc = len;\n *s = apr_palloc(r->pool, current_alloc);\n }\n else if (bytes_handled + len > current_alloc) {\n apr_size_t new_size = current_alloc * 2;\n char *new_buffer;\n if (bytes_handled + len > new_size) {\n new_size = (bytes_handled + len) * 2;\n }\n new_buffer = apr_palloc(r->pool, new_size);\n memcpy(new_buffer, *s, bytes_handled);\n current_alloc = new_size;\n *s = new_buffer;\n }\n }\n pos = *s + bytes_handled;\n memcpy(pos, str, len);\n last_char = pos + len - 1;\n bytes_handled += len;\n }\n if (last_char && (*last_char == APR_ASCII_LF)) {\n break;\n }\n }\n if (crlf && (last_char <= *s || last_char[-1] != APR_ASCII_CR)) {\n *last_char = '\\0';\n bytes_handled = last_char - *s;\n *read = bytes_handled;\n return APR_EINVAL;\n }\n if (last_char > *s && last_char[-1] == APR_ASCII_CR) {\n last_char--;\n }\n *last_char = '\\0';\n bytes_handled = last_char - *s;\n if (fold && bytes_handled && !saw_eos) {\n for (;;) {\n const char *str;\n apr_size_t len;\n char c;\n apr_brigade_cleanup(bb);\n rv = ap_get_brigade(r->proto_input_filters, bb, AP_MODE_SPECULATIVE,\n APR_BLOCK_READ, 1);\n if (rv != APR_SUCCESS) {\n return rv;\n }\n if (APR_BRIGADE_EMPTY(bb)) {\n break;\n }\n e = APR_BRIGADE_FIRST(bb);\n if (APR_BUCKET_IS_EOS(e)) {\n break;\n }\n rv = apr_bucket_read(e, &str, &len, APR_BLOCK_READ);\n if (rv != APR_SUCCESS) {\n apr_brigade_cleanup(bb);\n return rv;\n }\n c = *str;\n if (c == APR_ASCII_BLANK || c == APR_ASCII_TAB) {\n if (bytes_handled >= n) {\n *read = n;\n (*s)[n-1] = '\\0';\n return APR_ENOSPC;\n }\n else {\n apr_size_t next_size, next_len;\n char *tmp;\n if (do_alloc) {\n tmp = NULL;\n }\n else {\n tmp = last_char;\n }\n next_size = n - bytes_handled;\n rv = ap_rgetline_core(&tmp, next_size,\n &next_len, r, 0, bb);\n if (rv != APR_SUCCESS) {\n return rv;\n }\n if (do_alloc && next_len > 0) {\n char *new_buffer;\n apr_size_t new_size = bytes_handled + next_len + 1;\n new_buffer = apr_palloc(r->pool, new_size);\n memcpy(new_buffer, *s, bytes_handled);\n memcpy(new_buffer + bytes_handled, tmp, next_len + 1);\n *s = new_buffer;\n }\n last_char += next_len;\n bytes_handled += next_len;\n }\n }\n else {\n break;\n }\n }\n }\n *read = bytes_handled;\n if (strlen(*s) < bytes_handled) {\n return APR_EINVAL;\n }\n return APR_SUCCESS;\n}"] |
35,496 | 0 | https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/motion_est_template.c/#L724 | static int l2s_dia_search(MpegEncContext * s, int *best, int dmin,
int src_index, int ref_index, int const penalty_factor,
int size, int h, int flags)
{
MotionEstContext * const c= &s->me;
me_cmp_func cmpf, chroma_cmpf;
LOAD_COMMON
LOAD_COMMON2
int map_generation= c->map_generation;
int x,y,i,d;
int dia_size= c->dia_size&0xFF;
const int dec= dia_size & (dia_size-1);
static const int hex[8][2]={{-2, 0}, {-1,-1}, { 0,-2}, { 1,-1},
{ 2, 0}, { 1, 1}, { 0, 2}, {-1, 1}};
cmpf= s->dsp.me_cmp[size];
chroma_cmpf= s->dsp.me_cmp[size+1];
for(; dia_size; dia_size= dec ? dia_size-1 : dia_size>>1){
do{
x= best[0];
y= best[1];
for(i=0; i<8; i++){
CHECK_CLIPPED_MV(x+hex[i][0]*dia_size, y+hex[i][1]*dia_size);
}
}while(best[0] != x || best[1] != y);
}
x= best[0];
y= best[1];
CHECK_CLIPPED_MV(x+1, y);
CHECK_CLIPPED_MV(x, y+1);
CHECK_CLIPPED_MV(x-1, y);
CHECK_CLIPPED_MV(x, y-1);
return dmin;
} | ['static int l2s_dia_search(MpegEncContext * s, int *best, int dmin,\n int src_index, int ref_index, int const penalty_factor,\n int size, int h, int flags)\n{\n MotionEstContext * const c= &s->me;\n me_cmp_func cmpf, chroma_cmpf;\n LOAD_COMMON\n LOAD_COMMON2\n int map_generation= c->map_generation;\n int x,y,i,d;\n int dia_size= c->dia_size&0xFF;\n const int dec= dia_size & (dia_size-1);\n static const int hex[8][2]={{-2, 0}, {-1,-1}, { 0,-2}, { 1,-1},\n { 2, 0}, { 1, 1}, { 0, 2}, {-1, 1}};\n cmpf= s->dsp.me_cmp[size];\n chroma_cmpf= s->dsp.me_cmp[size+1];\n for(; dia_size; dia_size= dec ? dia_size-1 : dia_size>>1){\n do{\n x= best[0];\n y= best[1];\n for(i=0; i<8; i++){\n CHECK_CLIPPED_MV(x+hex[i][0]*dia_size, y+hex[i][1]*dia_size);\n }\n }while(best[0] != x || best[1] != y);\n }\n x= best[0];\n y= best[1];\n CHECK_CLIPPED_MV(x+1, y);\n CHECK_CLIPPED_MV(x, y+1);\n CHECK_CLIPPED_MV(x-1, y);\n CHECK_CLIPPED_MV(x, y-1);\n return dmin;\n}'] |
35,497 | 0 | https://github.com/libav/libav/blob/8a49d2bcbe7573bb4b765728b2578fac0d19763f/libavformat/mpegts.c/#L687 | 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 || !buffer) {\n buffer_size = bit_size = 0;\n buffer = NULL;\n ret = AVERROR_INVALIDDATA;\n }\n buffer_size = (bit_size + 7) >> 3;\n s->buffer = buffer;\n s->size_in_bits = bit_size;\n#if !UNCHECKED_BITSTREAM_READER\n s->size_in_bits_plus8 = bit_size + 8;\n#endif\n s->buffer_end = buffer + buffer_size;\n s->index = 0;\n return ret;\n}', 'static inline unsigned int get_bits1(GetBitContext *s)\n{\n unsigned int index = s->index;\n uint8_t result = s->buffer[index >> 3];\n#ifdef BITSTREAM_READER_LE\n result >>= index & 7;\n result &= 1;\n#else\n result <<= index & 7;\n result >>= 8 - 1;\n#endif\n#if !UNCHECKED_BITSTREAM_READER\n if (s->index < s->size_in_bits_plus8)\n#endif\n index++;\n s->index = index;\n return result;\n}'] |
35,498 | 0 | https://github.com/openssl/openssl/blob/a5fcd09e7552dedf87d5a1ff5d07a0397bc057cb/crypto/asn1/p7_s_e.c/#L124 | PKCS7_SIGN_ENVELOPE *PKCS7_SIGN_ENVELOPE_new(void)
{
PKCS7_SIGN_ENVELOPE *ret=NULL;
ASN1_CTX c;
M_ASN1_New_Malloc(ret,PKCS7_SIGN_ENVELOPE);
M_ASN1_New(ret->version,M_ASN1_INTEGER_new);
M_ASN1_New(ret->recipientinfo,sk_PKCS7_RECIP_INFO_new_null);
M_ASN1_New(ret->md_algs,sk_X509_ALGOR_new_null);
M_ASN1_New(ret->enc_data,PKCS7_ENC_CONTENT_new);
ret->cert=NULL;
ret->crl=NULL;
M_ASN1_New(ret->signer_info,sk_PKCS7_SIGNER_INFO_new_null);
return(ret);
M_ASN1_New_Error(ASN1_F_PKCS7_SIGN_ENVELOPE_NEW);
} | ['PKCS7_SIGN_ENVELOPE *PKCS7_SIGN_ENVELOPE_new(void)\n\t{\n\tPKCS7_SIGN_ENVELOPE *ret=NULL;\n\tASN1_CTX c;\n\tM_ASN1_New_Malloc(ret,PKCS7_SIGN_ENVELOPE);\n\tM_ASN1_New(ret->version,M_ASN1_INTEGER_new);\n\tM_ASN1_New(ret->recipientinfo,sk_PKCS7_RECIP_INFO_new_null);\n\tM_ASN1_New(ret->md_algs,sk_X509_ALGOR_new_null);\n\tM_ASN1_New(ret->enc_data,PKCS7_ENC_CONTENT_new);\n\tret->cert=NULL;\n\tret->crl=NULL;\n\tM_ASN1_New(ret->signer_info,sk_PKCS7_SIGNER_INFO_new_null);\n\treturn(ret);\n\tM_ASN1_New_Error(ASN1_F_PKCS7_SIGN_ENVELOPE_NEW);\n\t}', 'ASN1_STRING *ASN1_STRING_type_new(int type)\n\t{\n\tASN1_STRING *ret;\n\tret=(ASN1_STRING *)Malloc(sizeof(ASN1_STRING));\n\tif (ret == NULL)\n\t\t{\n\t\tASN1err(ASN1_F_ASN1_STRING_TYPE_NEW,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tret->length=0;\n\tret->type=type;\n\tret->data=NULL;\n\tret->flags=0;\n\treturn(ret);\n\t}', 'IMPLEMENT_STACK_OF(PKCS7_RECIP_INFO)', 'STACK *sk_new(int (*c)())\n\t{\n\tSTACK *ret;\n\tint i;\n\tif ((ret=(STACK *)Malloc(sizeof(STACK))) == NULL)\n\t\tgoto err0;\n\tif ((ret->data=(char **)Malloc(sizeof(char *)*MIN_NODES)) == NULL)\n\t\tgoto err1;\n\tfor (i=0; i<MIN_NODES; i++)\n\t\tret->data[i]=NULL;\n\tret->comp=c;\n\tret->num_alloc=MIN_NODES;\n\tret->num=0;\n\tret->sorted=0;\n\treturn(ret);\nerr1:\n\tFree((char *)ret);\nerr0:\n\treturn(NULL);\n\t}', 'IMPLEMENT_STACK_OF(X509_ALGOR)'] |
35,499 | 0 | https://github.com/openssl/openssl/blob/985de8634000df9b33b8ac4519fa10a99e43b314/apps/x509.c/#L1116 | static int x509_certify(X509_STORE *ctx, char *CAfile, const EVP_MD *digest,
X509 *x, X509 *xca, EVP_PKEY *pkey, char *serialfile, int create,
int days, int clrext, CONF *conf, char *section, ASN1_INTEGER *sno)
{
int ret=0;
ASN1_INTEGER *bs=NULL;
X509_STORE_CTX xsc;
EVP_PKEY *upkey;
upkey = X509_get_pubkey(xca);
EVP_PKEY_copy_parameters(upkey,pkey);
EVP_PKEY_free(upkey);
if(!X509_STORE_CTX_init(&xsc,ctx,x,NULL))
{
BIO_printf(bio_err,"Error initialising X509 store\n");
goto end;
}
if (sno) bs = sno;
else if (!(bs = x509_load_serial(CAfile, serialfile, create)))
goto end;
X509_STORE_CTX_set_cert(&xsc,x);
if (!reqfile && !X509_verify_cert(&xsc))
goto end;
if (!X509_check_private_key(xca,pkey))
{
BIO_printf(bio_err,"CA certificate and CA private key do not match\n");
goto end;
}
if (!X509_set_issuer_name(x,X509_get_subject_name(xca))) goto end;
if (!X509_set_serialNumber(x,bs)) goto end;
if (X509_gmtime_adj(X509_get_notBefore(x),0L) == NULL)
goto end;
if (X509_gmtime_adj(X509_get_notAfter(x),(long)60*60*24*days) == NULL)
goto end;
if (clrext)
{
while (X509_get_ext_count(x) > 0) X509_delete_ext(x, 0);
}
if (conf)
{
X509V3_CTX ctx2;
X509_set_version(x,2);
X509V3_set_ctx(&ctx2, xca, x, NULL, NULL, 0);
X509V3_set_nconf(&ctx2, conf);
if (!X509V3_EXT_add_nconf(conf, &ctx2, section, x)) goto end;
}
if (!X509_sign(x,pkey,digest)) goto end;
ret=1;
end:
X509_STORE_CTX_cleanup(&xsc);
if (!ret)
ERR_print_errors(bio_err);
if (!sno) ASN1_INTEGER_free(bs);
return ret;
} | ['static int x509_certify(X509_STORE *ctx, char *CAfile, const EVP_MD *digest,\n\t X509 *x, X509 *xca, EVP_PKEY *pkey, char *serialfile, int create,\n\t int days, int clrext, CONF *conf, char *section, ASN1_INTEGER *sno)\n\t{\n\tint ret=0;\n\tASN1_INTEGER *bs=NULL;\n\tX509_STORE_CTX xsc;\n\tEVP_PKEY *upkey;\n\tupkey = X509_get_pubkey(xca);\n\tEVP_PKEY_copy_parameters(upkey,pkey);\n\tEVP_PKEY_free(upkey);\n\tif(!X509_STORE_CTX_init(&xsc,ctx,x,NULL))\n\t\t{\n\t\tBIO_printf(bio_err,"Error initialising X509 store\\n");\n\t\tgoto end;\n\t\t}\n\tif (sno) bs = sno;\n\telse if (!(bs = x509_load_serial(CAfile, serialfile, create)))\n\t\tgoto end;\n\tX509_STORE_CTX_set_cert(&xsc,x);\n\tif (!reqfile && !X509_verify_cert(&xsc))\n\t\tgoto end;\n\tif (!X509_check_private_key(xca,pkey))\n\t\t{\n\t\tBIO_printf(bio_err,"CA certificate and CA private key do not match\\n");\n\t\tgoto end;\n\t\t}\n\tif (!X509_set_issuer_name(x,X509_get_subject_name(xca))) goto end;\n\tif (!X509_set_serialNumber(x,bs)) goto end;\n\tif (X509_gmtime_adj(X509_get_notBefore(x),0L) == NULL)\n\t\tgoto end;\n\tif (X509_gmtime_adj(X509_get_notAfter(x),(long)60*60*24*days) == NULL)\n\t\tgoto end;\n\tif (clrext)\n\t\t{\n\t\twhile (X509_get_ext_count(x) > 0) X509_delete_ext(x, 0);\n\t\t}\n\tif (conf)\n\t\t{\n\t\tX509V3_CTX ctx2;\n\t\tX509_set_version(x,2);\n X509V3_set_ctx(&ctx2, xca, x, NULL, NULL, 0);\n X509V3_set_nconf(&ctx2, conf);\n if (!X509V3_EXT_add_nconf(conf, &ctx2, section, x)) goto end;\n\t\t}\n\tif (!X509_sign(x,pkey,digest)) goto end;\n\tret=1;\nend:\n\tX509_STORE_CTX_cleanup(&xsc);\n\tif (!ret)\n\t\tERR_print_errors(bio_err);\n\tif (!sno) ASN1_INTEGER_free(bs);\n\treturn ret;\n\t}', 'EVP_PKEY *X509_get_pubkey(X509 *x)\n\t{\n\tif ((x == NULL) || (x->cert_info == NULL))\n\t\treturn(NULL);\n\treturn(X509_PUBKEY_get(x->cert_info->key));\n\t}', 'EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key)\n\t{\n\tEVP_PKEY *ret=NULL;\n\tif (key == NULL) goto error;\n\tif (key->pkey != NULL)\n\t\t{\n\t\tCRYPTO_add(&key->pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);\n\t\treturn key->pkey;\n\t\t}\n\tif (key->public_key == NULL) goto error;\n\tif ((ret = EVP_PKEY_new()) == NULL)\n\t\t{\n\t\tX509err(X509_F_X509_PUBKEY_GET, ERR_R_MALLOC_FAILURE);\n\t\tgoto error;\n\t\t}\n\tif (!EVP_PKEY_set_type(ret, OBJ_obj2nid(key->algor->algorithm)))\n\t\t{\n\t\tX509err(X509_F_X509_PUBKEY_GET,X509_R_UNSUPPORTED_ALGORITHM);\n\t\tgoto error;\n\t\t}\n\tif (ret->ameth->pub_decode)\n\t\t{\n\t\tif (!ret->ameth->pub_decode(ret, key))\n\t\t\t{\n\t\t\tX509err(X509_F_X509_PUBKEY_GET,\n\t\t\t\t\t\tX509_R_PUBLIC_KEY_DECODE_ERROR);\n\t\t\tgoto error;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tX509err(X509_F_X509_PUBKEY_GET, X509_R_METHOD_NOT_SUPPORTED);\n\t\tgoto error;\n\t\t}\n\tkey->pkey = ret;\n\tCRYPTO_add(&ret->references, 1, CRYPTO_LOCK_EVP_PKEY);\n\treturn ret;\n\terror:\n\tif (ret != NULL)\n\t\tEVP_PKEY_free(ret);\n\treturn(NULL);\n\t}', 'EVP_PKEY *EVP_PKEY_new(void)\n\t{\n\tEVP_PKEY *ret;\n\tret=(EVP_PKEY *)OPENSSL_malloc(sizeof(EVP_PKEY));\n\tif (ret == NULL)\n\t\t{\n\t\tEVPerr(EVP_F_EVP_PKEY_NEW,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tret->type=EVP_PKEY_NONE;\n\tret->save_type=EVP_PKEY_NONE;\n\tret->references=1;\n\tret->ameth=NULL;\n\tret->engine=NULL;\n\tret->pkey.ptr=NULL;\n\tret->attributes=NULL;\n\tret->save_parameters=1;\n\treturn(ret);\n\t}', 'void *CRYPTO_malloc(int num, const char *file, int line)\n\t{\n\tvoid *ret = NULL;\n\tif (num <= 0) return NULL;\n\tallow_customize = 0;\n\tif (malloc_debug_func != NULL)\n\t\t{\n\t\tallow_customize_debug = 0;\n\t\tmalloc_debug_func(NULL, num, file, line, 0);\n\t\t}\n\tret = malloc_ex_func(num,file,line);\n#ifdef LEVITTE_DEBUG_MEM\n\tfprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\\n", ret, num);\n#endif\n\tif (malloc_debug_func != NULL)\n\t\tmalloc_debug_func(ret, num, file, line, 1);\n#ifndef OPENSSL_CPUID_OBJ\n if(ret && (num > 2048))\n\t{\textern unsigned char cleanse_ctr;\n ((unsigned char *)ret)[0] = cleanse_ctr;\n\t}\n#endif\n\treturn ret;\n\t}', 'void ERR_put_error(int lib, int func, int reason, const char *file,\n\t int line)\n\t{\n\tERR_STATE *es;\n#ifdef _OSD_POSIX\n\tif (strncmp(file,"*POSIX(", sizeof("*POSIX(")-1) == 0) {\n\t\tchar *end;\n\t\tfile += sizeof("*POSIX(")-1;\n\t\tend = &file[strlen(file)-1];\n\t\tif (*end == \')\')\n\t\t\t*end = \'\\0\';\n\t\tif ((end = strrchr(file, \'/\')) != NULL)\n\t\t\tfile = &end[1];\n\t}\n#endif\n\tes=ERR_get_state();\n\tes->top=(es->top+1)%ERR_NUM_ERRORS;\n\tif (es->top == es->bottom)\n\t\tes->bottom=(es->bottom+1)%ERR_NUM_ERRORS;\n\tes->err_flags[es->top]=0;\n\tes->err_buffer[es->top]=ERR_PACK(lib,func,reason);\n\tes->err_file[es->top]=file;\n\tes->err_line[es->top]=line;\n\terr_clear_data(es,es->top);\n\t}', 'int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)\n\t{\n\tif (to->type != from->type)\n\t\t{\n\t\tEVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS,EVP_R_DIFFERENT_KEY_TYPES);\n\t\tgoto err;\n\t\t}\n\tif (EVP_PKEY_missing_parameters(from))\n\t\t{\n\t\tEVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS,EVP_R_MISSING_PARAMETERS);\n\t\tgoto err;\n\t\t}\n\tif (from->ameth && from->ameth->param_copy)\n\t\treturn from->ameth->param_copy(to, from);\nerr:\n\treturn 0;\n\t}'] |
35,500 | 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 unpack_bitstream(G723_1_Context *p, const uint8_t *buf,\n int buf_size)\n{\n BitstreamContext bc;\n int ad_cb_len;\n int temp, info_bits, i;\n bitstream_init(&bc, buf, buf_size * 8);\n info_bits = bitstream_read(&bc, 2);\n if (info_bits == 3) {\n p->cur_frame_type = UNTRANSMITTED_FRAME;\n return 0;\n }\n p->lsp_index[2] = bitstream_read(&bc, 8);\n p->lsp_index[1] = bitstream_read(&bc, 8);\n p->lsp_index[0] = bitstream_read(&bc, 8);\n if (info_bits == 2) {\n p->cur_frame_type = SID_FRAME;\n p->subframe[0].amp_index = bitstream_read(&bc, 6);\n return 0;\n }\n p->cur_rate = info_bits ? RATE_5300 : RATE_6300;\n p->cur_frame_type = ACTIVE_FRAME;\n p->pitch_lag[0] = bitstream_read(&bc, 7);\n if (p->pitch_lag[0] > 123)\n return -1;\n p->pitch_lag[0] += PITCH_MIN;\n p->subframe[1].ad_cb_lag = bitstream_read(&bc, 2);\n p->pitch_lag[1] = bitstream_read(&bc, 7);\n if (p->pitch_lag[1] > 123)\n return -1;\n p->pitch_lag[1] += PITCH_MIN;\n p->subframe[3].ad_cb_lag = bitstream_read(&bc, 2);\n p->subframe[0].ad_cb_lag = 1;\n p->subframe[2].ad_cb_lag = 1;\n for (i = 0; i < SUBFRAMES; i++) {\n temp = bitstream_read(&bc, 12);\n ad_cb_len = 170;\n p->subframe[i].dirac_train = 0;\n if (p->cur_rate == RATE_6300 && p->pitch_lag[i >> 1] < SUBFRAME_LEN - 2) {\n p->subframe[i].dirac_train = temp >> 11;\n temp &= 0x7FF;\n ad_cb_len = 85;\n }\n p->subframe[i].ad_cb_gain = FASTDIV(temp, GAIN_LEVELS);\n if (p->subframe[i].ad_cb_gain < ad_cb_len) {\n p->subframe[i].amp_index = temp - p->subframe[i].ad_cb_gain *\n GAIN_LEVELS;\n } else {\n return -1;\n }\n }\n p->subframe[0].grid_index = bitstream_read(&bc, 1);\n p->subframe[1].grid_index = bitstream_read(&bc, 1);\n p->subframe[2].grid_index = bitstream_read(&bc, 1);\n p->subframe[3].grid_index = bitstream_read(&bc, 1);\n if (p->cur_rate == RATE_6300) {\n bitstream_skip(&bc, 1);\n temp = bitstream_read(&bc, 13);\n p->subframe[0].pulse_pos = temp / 810;\n temp -= p->subframe[0].pulse_pos * 810;\n p->subframe[1].pulse_pos = FASTDIV(temp, 90);\n temp -= p->subframe[1].pulse_pos * 90;\n p->subframe[2].pulse_pos = FASTDIV(temp, 9);\n p->subframe[3].pulse_pos = temp - p->subframe[2].pulse_pos * 9;\n p->subframe[0].pulse_pos = (p->subframe[0].pulse_pos << 16) +\n bitstream_read(&bc, 16);\n p->subframe[1].pulse_pos = (p->subframe[1].pulse_pos << 14) +\n bitstream_read(&bc, 14);\n p->subframe[2].pulse_pos = (p->subframe[2].pulse_pos << 16) +\n bitstream_read(&bc, 16);\n p->subframe[3].pulse_pos = (p->subframe[3].pulse_pos << 14) +\n bitstream_read(&bc, 14);\n p->subframe[0].pulse_sign = bitstream_read(&bc, 6);\n p->subframe[1].pulse_sign = bitstream_read(&bc, 5);\n p->subframe[2].pulse_sign = bitstream_read(&bc, 6);\n p->subframe[3].pulse_sign = bitstream_read(&bc, 5);\n } else {\n p->subframe[0].pulse_pos = bitstream_read(&bc, 12);\n p->subframe[1].pulse_pos = bitstream_read(&bc, 12);\n p->subframe[2].pulse_pos = bitstream_read(&bc, 12);\n p->subframe[3].pulse_pos = bitstream_read(&bc, 12);\n p->subframe[0].pulse_sign = bitstream_read(&bc, 4);\n p->subframe[1].pulse_sign = bitstream_read(&bc, 4);\n p->subframe[2].pulse_sign = bitstream_read(&bc, 4);\n p->subframe[3].pulse_sign = bitstream_read(&bc, 4);\n }\n return 0;\n}', 'static inline uint32_t bitstream_read(BitstreamContext *bc, unsigned n)\n{\n if (!n)\n return 0;\n if (n > bc->bits_left) {\n refill_32(bc);\n if (bc->bits_left < 32)\n bc->bits_left = n;\n }\n return get_val(bc, n);\n}', 'static inline uint64_t get_val(BitstreamContext *bc, unsigned n)\n{\n#ifdef BITSTREAM_READER_LE\n uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);\n bc->bits >>= n;\n#else\n uint64_t ret = bc->bits >> (64 - n);\n bc->bits <<= n;\n#endif\n bc->bits_left -= n;\n return ret;\n}'] |