id
int64
1
36.7k
label
int64
0
1
bug_url
stringlengths
91
134
bug_function
stringlengths
13
72.7k
functions
stringlengths
17
79.2k
1
0
https://github.com/libav/libav/blob/54bc15d5ebfd07fd468743ba29f709ea19e840b9/libswscale/output.c/#L1339
static void yuv2gbrp_full_X_c(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize, const int16_t *chrFilter, const int16_t **chrUSrc, const int16_t **chrVSrc, int chrFilterSize, const int16_t **alpSrc, uint8_t **dest, int dstW, int y) { const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->dstFormat); int i; int hasAlpha = (desc->flags & AV_PIX_FMT_FLAG_ALPHA) && alpSrc; uint16_t **dest16 = (uint16_t**)dest; int SH = 22 + 7 - desc->comp[0].depth_minus1; for (i = 0; i < dstW; i++) { int j; int Y = 1 << 9; int U = (1 << 9) - (128 << 19); int V = (1 << 9) - (128 << 19); int R, G, B, A; for (j = 0; j < lumFilterSize; j++) Y += lumSrc[j][i] * lumFilter[j]; for (j = 0; j < chrFilterSize; j++) { U += chrUSrc[j][i] * chrFilter[j]; V += chrVSrc[j][i] * chrFilter[j]; } Y >>= 10; U >>= 10; V >>= 10; if (hasAlpha) { A = 1 << 18; for (j = 0; j < lumFilterSize; j++) A += alpSrc[j][i] * lumFilter[j]; A >>= 19; if (A & 0x100) A = av_clip_uint8(A); } Y -= c->yuv2rgb_y_offset; Y *= c->yuv2rgb_y_coeff; Y += 1 << 21; R = Y + V * c->yuv2rgb_v2r_coeff; G = Y + V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff; B = Y + U * c->yuv2rgb_u2b_coeff; if ((R | G | B) & 0xC0000000) { R = av_clip_uintp2(R, 30); G = av_clip_uintp2(G, 30); B = av_clip_uintp2(B, 30); } if (SH != 22) { dest16[0][i] = G >> SH; dest16[1][i] = B >> SH; dest16[2][i] = R >> SH; if (hasAlpha) dest16[3][i] = A; } else { dest[0][i] = G >> 22; dest[1][i] = B >> 22; dest[2][i] = R >> 22; if (hasAlpha) dest[3][i] = A; } } if (SH != 22 && (!isBE(c->dstFormat)) != (!HAVE_BIGENDIAN)) { for (i = 0; i < dstW; i++) { dest16[0][i] = av_bswap16(dest16[0][i]); dest16[1][i] = av_bswap16(dest16[1][i]); dest16[2][i] = av_bswap16(dest16[2][i]); if (hasAlpha) dest16[3][i] = av_bswap16(dest16[3][i]); } } }
['static void\nyuv2gbrp_full_X_c(SwsContext *c, const int16_t *lumFilter,\n const int16_t **lumSrc, int lumFilterSize,\n const int16_t *chrFilter, const int16_t **chrUSrc,\n const int16_t **chrVSrc, int chrFilterSize,\n const int16_t **alpSrc, uint8_t **dest,\n int dstW, int y)\n{\n const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->dstFormat);\n int i;\n int hasAlpha = (desc->flags & AV_PIX_FMT_FLAG_ALPHA) && alpSrc;\n uint16_t **dest16 = (uint16_t**)dest;\n int SH = 22 + 7 - desc->comp[0].depth_minus1;\n for (i = 0; i < dstW; i++) {\n int j;\n int Y = 1 << 9;\n int U = (1 << 9) - (128 << 19);\n int V = (1 << 9) - (128 << 19);\n int R, G, B, A;\n for (j = 0; j < lumFilterSize; j++)\n Y += lumSrc[j][i] * lumFilter[j];\n for (j = 0; j < chrFilterSize; j++) {\n U += chrUSrc[j][i] * chrFilter[j];\n V += chrVSrc[j][i] * chrFilter[j];\n }\n Y >>= 10;\n U >>= 10;\n V >>= 10;\n if (hasAlpha) {\n A = 1 << 18;\n for (j = 0; j < lumFilterSize; j++)\n A += alpSrc[j][i] * lumFilter[j];\n A >>= 19;\n if (A & 0x100)\n A = av_clip_uint8(A);\n }\n Y -= c->yuv2rgb_y_offset;\n Y *= c->yuv2rgb_y_coeff;\n Y += 1 << 21;\n R = Y + V * c->yuv2rgb_v2r_coeff;\n G = Y + V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;\n B = Y + U * c->yuv2rgb_u2b_coeff;\n if ((R | G | B) & 0xC0000000) {\n R = av_clip_uintp2(R, 30);\n G = av_clip_uintp2(G, 30);\n B = av_clip_uintp2(B, 30);\n }\n if (SH != 22) {\n dest16[0][i] = G >> SH;\n dest16[1][i] = B >> SH;\n dest16[2][i] = R >> SH;\n if (hasAlpha)\n dest16[3][i] = A;\n } else {\n dest[0][i] = G >> 22;\n dest[1][i] = B >> 22;\n dest[2][i] = R >> 22;\n if (hasAlpha)\n dest[3][i] = A;\n }\n }\n if (SH != 22 && (!isBE(c->dstFormat)) != (!HAVE_BIGENDIAN)) {\n for (i = 0; i < dstW; i++) {\n dest16[0][i] = av_bswap16(dest16[0][i]);\n dest16[1][i] = av_bswap16(dest16[1][i]);\n dest16[2][i] = av_bswap16(dest16[2][i]);\n if (hasAlpha)\n dest16[3][i] = av_bswap16(dest16[3][i]);\n }\n }\n}']
2
0
https://github.com/openssl/openssl/blob/04485c5bc0dc7f49940e6d91b27cdcc7b83a8ab5/crypto/bn/bn_ctx.c/#L355
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['int ec_GFp_simple_points_make_affine(const EC_GROUP *group, size_t num, EC_POINT *points[], BN_CTX *ctx)\n\t{\n\tBN_CTX *new_ctx = NULL;\n\tBIGNUM *tmp0, *tmp1;\n\tsize_t pow2 = 0;\n\tBIGNUM **heap = NULL;\n\tsize_t i;\n\tint ret = 0;\n\tif (num == 0)\n\t\treturn 1;\n\tif (ctx == NULL)\n\t\t{\n\t\tctx = new_ctx = BN_CTX_new();\n\t\tif (ctx == NULL)\n\t\t\treturn 0;\n\t\t}\n\tBN_CTX_start(ctx);\n\ttmp0 = BN_CTX_get(ctx);\n\ttmp1 = BN_CTX_get(ctx);\n\tif (tmp0 == NULL || tmp1 == NULL) goto err;\n\tpow2 = 1;\n\twhile (num > pow2)\n\t\tpow2 <<= 1;\n\tpow2 <<= 1;\n\theap = OPENSSL_malloc(pow2 * sizeof heap[0]);\n\tif (heap == NULL) goto err;\n\theap[0] = NULL;\n\tfor (i = pow2/2 - 1; i > 0; i--)\n\t\theap[i] = NULL;\n\tfor (i = 0; i < num; i++)\n\t\theap[pow2/2 + i] = &points[i]->Z;\n\tfor (i = pow2/2 + num; i < pow2; i++)\n\t\theap[i] = NULL;\n\tfor (i = pow2/2 - 1; i > 0; i--)\n\t\t{\n\t\theap[i] = BN_new();\n\t\tif (heap[i] == NULL) goto err;\n\t\tif (heap[2*i] != NULL)\n\t\t\t{\n\t\t\tif ((heap[2*i + 1] == NULL) || BN_is_zero(heap[2*i + 1]))\n\t\t\t\t{\n\t\t\t\tif (!BN_copy(heap[i], heap[2*i])) goto err;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (BN_is_zero(heap[2*i]))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_copy(heap[i], heap[2*i + 1])) goto err;\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tif (!group->meth->field_mul(group, heap[i],\n\t\t\t\t\t\theap[2*i], heap[2*i + 1], ctx)) goto err;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\tif (!BN_is_zero(heap[1]))\n\t\t{\n\t\tif (!BN_mod_inverse(heap[1], heap[1], &group->field, ctx))\n\t\t\t{\n\t\t\tECerr(EC_F_EC_GFP_SIMPLE_POINTS_MAKE_AFFINE, ERR_R_BN_LIB);\n\t\t\tgoto err;\n\t\t\t}\n\t\t}\n\tif (group->meth->field_encode != 0)\n\t\t{\n\t\tif (!group->meth->field_encode(group, heap[1], heap[1], ctx)) goto err;\n\t\tif (!group->meth->field_encode(group, heap[1], heap[1], ctx)) goto err;\n\t\t}\n\tfor (i = 2; i < pow2/2 + num; i += 2)\n\t\t{\n\t\tif ((heap[i + 1] != NULL) && !BN_is_zero(heap[i + 1]))\n\t\t\t{\n\t\t\tif (!group->meth->field_mul(group, tmp0, heap[i/2], heap[i + 1], ctx)) goto err;\n\t\t\tif (!group->meth->field_mul(group, tmp1, heap[i/2], heap[i], ctx)) goto err;\n\t\t\tif (!BN_copy(heap[i], tmp0)) goto err;\n\t\t\tif (!BN_copy(heap[i + 1], tmp1)) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (!BN_copy(heap[i], heap[i/2])) goto err;\n\t\t\t}\n\t\t}\n\tfor (i = 0; i < num; i++)\n\t\t{\n\t\tEC_POINT *p = points[i];\n\t\tif (!BN_is_zero(&p->Z))\n\t\t\t{\n\t\t\tif (!group->meth->field_sqr(group, tmp1, &p->Z, ctx)) goto err;\n\t\t\tif (!group->meth->field_mul(group, &p->X, &p->X, tmp1, ctx)) goto err;\n\t\t\tif (!group->meth->field_mul(group, tmp1, tmp1, &p->Z, ctx)) goto err;\n\t\t\tif (!group->meth->field_mul(group, &p->Y, &p->Y, tmp1, ctx)) goto err;\n\t\t\tif (group->meth->field_set_to_one != 0)\n\t\t\t\t{\n\t\t\t\tif (!group->meth->field_set_to_one(group, &p->Z, ctx)) goto err;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (!BN_one(&p->Z)) goto err;\n\t\t\t\t}\n\t\t\tp->Z_is_one = 1;\n\t\t\t}\n\t\t}\n\tret = 1;\n err:\n\tBN_CTX_end(ctx);\n\tif (new_ctx != NULL)\n\t\tBN_CTX_free(new_ctx);\n\tif (heap != NULL)\n\t\t{\n\t\tfor (i = pow2/2 - 1; i > 0; i--)\n\t\t\t{\n\t\t\tif (heap[i] != NULL)\n\t\t\t\tBN_clear_free(heap[i]);\n\t\t\t}\n\t\tOPENSSL_free(heap);\n\t\t}\n\treturn ret;\n\t}', 'void BN_CTX_start(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_start", ctx);\n\tif(ctx->err_stack || ctx->too_many)\n\t\tctx->err_stack++;\n\telse if(!BN_STACK_push(&ctx->stack, ctx->used))\n\t\t{\n\t\tBNerr(BN_F_BN_CTX_START,BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n\t\tctx->err_stack++;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'BIGNUM *BN_mod_inverse(BIGNUM *in,\n\tconst BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n\t{\n\tBIGNUM *rv;\n\tint noinv;\n\trv = int_bn_mod_inverse(in, a, n, ctx, &noinv);\n\tif (noinv)\n\t\tBNerr(BN_F_BN_MOD_INVERSE,BN_R_NO_INVERSE);\n\treturn rv;\n\t}', 'BIGNUM *int_bn_mod_inverse(BIGNUM *in,\n\tconst BIGNUM *a, const BIGNUM *n, BN_CTX *ctx, int *pnoinv)\n\t{\n\tBIGNUM *A,*B,*X,*Y,*M,*D,*T,*R=NULL;\n\tBIGNUM *ret=NULL;\n\tint sign;\n\tif (pnoinv)\n\t\t*pnoinv = 0;\n\tif ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0))\n\t\t{\n\t\treturn BN_mod_inverse_no_branch(in, a, n, ctx);\n\t\t}\n\tbn_check_top(a);\n\tbn_check_top(n);\n\tBN_CTX_start(ctx);\n\tA = BN_CTX_get(ctx);\n\tB = BN_CTX_get(ctx);\n\tX = BN_CTX_get(ctx);\n\tD = BN_CTX_get(ctx);\n\tM = BN_CTX_get(ctx);\n\tY = BN_CTX_get(ctx);\n\tT = BN_CTX_get(ctx);\n\tif (T == NULL) goto err;\n\tif (in == NULL)\n\t\tR=BN_new();\n\telse\n\t\tR=in;\n\tif (R == NULL) goto err;\n\tBN_one(X);\n\tBN_zero(Y);\n\tif (BN_copy(B,a) == NULL) goto err;\n\tif (BN_copy(A,n) == NULL) goto err;\n\tA->neg = 0;\n\tif (B->neg || (BN_ucmp(B, A) >= 0))\n\t\t{\n\t\tif (!BN_nnmod(B, B, A, ctx)) goto err;\n\t\t}\n\tsign = -1;\n\tif (BN_is_odd(n) && (BN_num_bits(n) <= (BN_BITS <= 32 ? 450 : 2048)))\n\t\t{\n\t\tint shift;\n\t\twhile (!BN_is_zero(B))\n\t\t\t{\n\t\t\tshift = 0;\n\t\t\twhile (!BN_is_bit_set(B, shift))\n\t\t\t\t{\n\t\t\t\tshift++;\n\t\t\t\tif (BN_is_odd(X))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_uadd(X, X, n)) goto err;\n\t\t\t\t\t}\n\t\t\t\tif (!BN_rshift1(X, X)) goto err;\n\t\t\t\t}\n\t\t\tif (shift > 0)\n\t\t\t\t{\n\t\t\t\tif (!BN_rshift(B, B, shift)) goto err;\n\t\t\t\t}\n\t\t\tshift = 0;\n\t\t\twhile (!BN_is_bit_set(A, shift))\n\t\t\t\t{\n\t\t\t\tshift++;\n\t\t\t\tif (BN_is_odd(Y))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_uadd(Y, Y, n)) goto err;\n\t\t\t\t\t}\n\t\t\t\tif (!BN_rshift1(Y, Y)) goto err;\n\t\t\t\t}\n\t\t\tif (shift > 0)\n\t\t\t\t{\n\t\t\t\tif (!BN_rshift(A, A, shift)) goto err;\n\t\t\t\t}\n\t\t\tif (BN_ucmp(B, A) >= 0)\n\t\t\t\t{\n\t\t\t\tif (!BN_uadd(X, X, Y)) goto err;\n\t\t\t\tif (!BN_usub(B, B, A)) goto err;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (!BN_uadd(Y, Y, X)) goto err;\n\t\t\t\tif (!BN_usub(A, A, B)) goto err;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\twhile (!BN_is_zero(B))\n\t\t\t{\n\t\t\tBIGNUM *tmp;\n\t\t\tif (BN_num_bits(A) == BN_num_bits(B))\n\t\t\t\t{\n\t\t\t\tif (!BN_one(D)) goto err;\n\t\t\t\tif (!BN_sub(M,A,B)) goto err;\n\t\t\t\t}\n\t\t\telse if (BN_num_bits(A) == BN_num_bits(B) + 1)\n\t\t\t\t{\n\t\t\t\tif (!BN_lshift1(T,B)) goto err;\n\t\t\t\tif (BN_ucmp(A,T) < 0)\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_one(D)) goto err;\n\t\t\t\t\tif (!BN_sub(M,A,B)) goto err;\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_sub(M,A,T)) goto err;\n\t\t\t\t\tif (!BN_add(D,T,B)) goto err;\n\t\t\t\t\tif (BN_ucmp(A,D) < 0)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tif (!BN_set_word(D,2)) goto err;\n\t\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t\t{\n\t\t\t\t\t\tif (!BN_set_word(D,3)) goto err;\n\t\t\t\t\t\tif (!BN_sub(M,M,B)) goto err;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (!BN_div(D,M,A,B,ctx)) goto err;\n\t\t\t\t}\n\t\t\ttmp=A;\n\t\t\tA=B;\n\t\t\tB=M;\n\t\t\tif (BN_is_one(D))\n\t\t\t\t{\n\t\t\t\tif (!BN_add(tmp,X,Y)) goto err;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (BN_is_word(D,2))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_lshift1(tmp,X)) goto err;\n\t\t\t\t\t}\n\t\t\t\telse if (BN_is_word(D,4))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_lshift(tmp,X,2)) goto err;\n\t\t\t\t\t}\n\t\t\t\telse if (D->top == 1)\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_copy(tmp,X)) goto err;\n\t\t\t\t\tif (!BN_mul_word(tmp,D->d[0])) goto err;\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_mul(tmp,D,X,ctx)) goto err;\n\t\t\t\t\t}\n\t\t\t\tif (!BN_add(tmp,tmp,Y)) goto err;\n\t\t\t\t}\n\t\t\tM=Y;\n\t\t\tY=X;\n\t\t\tX=tmp;\n\t\t\tsign = -sign;\n\t\t\t}\n\t\t}\n\tif (sign < 0)\n\t\t{\n\t\tif (!BN_sub(Y,n,Y)) goto err;\n\t\t}\n\tif (BN_is_one(A))\n\t\t{\n\t\tif (!Y->neg && BN_ucmp(Y,n) < 0)\n\t\t\t{\n\t\t\tif (!BN_copy(R,Y)) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (!BN_nnmod(R,Y,n,ctx)) goto err;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tif (pnoinv)\n\t\t\t*pnoinv = 1;\n\t\tgoto err;\n\t\t}\n\tret=R;\nerr:\n\tif ((ret == NULL) && (in == NULL)) BN_free(R);\n\tBN_CTX_end(ctx);\n\tbn_check_top(ret);\n\treturn(ret);\n\t}', 'static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,\n\tconst BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n\t{\n\tBIGNUM *A,*B,*X,*Y,*M,*D,*T,*R=NULL;\n\tBIGNUM local_A, local_B;\n\tBIGNUM *pA, *pB;\n\tBIGNUM *ret=NULL;\n\tint sign;\n\tbn_check_top(a);\n\tbn_check_top(n);\n\tBN_CTX_start(ctx);\n\tA = BN_CTX_get(ctx);\n\tB = BN_CTX_get(ctx);\n\tX = BN_CTX_get(ctx);\n\tD = BN_CTX_get(ctx);\n\tM = BN_CTX_get(ctx);\n\tY = BN_CTX_get(ctx);\n\tT = BN_CTX_get(ctx);\n\tif (T == NULL) goto err;\n\tif (in == NULL)\n\t\tR=BN_new();\n\telse\n\t\tR=in;\n\tif (R == NULL) goto err;\n\tBN_one(X);\n\tBN_zero(Y);\n\tif (BN_copy(B,a) == NULL) goto err;\n\tif (BN_copy(A,n) == NULL) goto err;\n\tA->neg = 0;\n\tif (B->neg || (BN_ucmp(B, A) >= 0))\n\t\t{\n\t\tpB = &local_B;\n\t\tBN_with_flags(pB, B, BN_FLG_CONSTTIME);\n\t\tif (!BN_nnmod(B, pB, A, ctx)) goto err;\n\t\t}\n\tsign = -1;\n\twhile (!BN_is_zero(B))\n\t\t{\n\t\tBIGNUM *tmp;\n\t\tpA = &local_A;\n\t\tBN_with_flags(pA, A, BN_FLG_CONSTTIME);\n\t\tif (!BN_div(D,M,pA,B,ctx)) goto err;\n\t\ttmp=A;\n\t\tA=B;\n\t\tB=M;\n\t\tif (!BN_mul(tmp,D,X,ctx)) goto err;\n\t\tif (!BN_add(tmp,tmp,Y)) goto err;\n\t\tM=Y;\n\t\tY=X;\n\t\tX=tmp;\n\t\tsign = -sign;\n\t\t}\n\tif (sign < 0)\n\t\t{\n\t\tif (!BN_sub(Y,n,Y)) goto err;\n\t\t}\n\tif (BN_is_one(A))\n\t\t{\n\t\tif (!Y->neg && BN_ucmp(Y,n) < 0)\n\t\t\t{\n\t\t\tif (!BN_copy(R,Y)) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (!BN_nnmod(R,Y,n,ctx)) goto err;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_INVERSE_NO_BRANCH,BN_R_NO_INVERSE);\n\t\tgoto err;\n\t\t}\n\tret=R;\nerr:\n\tif ((ret == NULL) && (in == NULL)) BN_free(R);\n\tBN_CTX_end(ctx);\n\tbn_check_top(ret);\n\treturn(ret);\n\t}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n\t{\n\tif (!(BN_mod(r,m,d,ctx)))\n\t\treturn 0;\n\tif (!r->neg)\n\t\treturn 1;\n\treturn (d->neg ? BN_sub : BN_add)(r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n\t BN_CTX *ctx)\n\t{\n\tint norm_shift,i,loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tint num_n,div_n;\n\tint no_branch=0;\n\tif (num->top > 0 && num->d[num->top - 1] == 0)\n\t\t{\n\t\tBNerr(BN_F_BN_DIV,BN_R_NOT_INITIALIZED);\n\t\treturn 0;\n\t\t}\n\tbn_check_top(num);\n\tif ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0))\n\t\t{\n\t\tno_branch=1;\n\t\t}\n\tbn_check_top(dv);\n\tbn_check_top(rm);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\n\t\t}\n\tif (!no_branch && BN_ucmp(num,divisor) < 0)\n\t\t{\n\t\tif (rm != NULL)\n\t\t\t{ if (BN_copy(rm,num) == NULL) return(0); }\n\t\tif (dv != NULL) BN_zero(dv);\n\t\treturn(1);\n\t\t}\n\tBN_CTX_start(ctx);\n\ttmp=BN_CTX_get(ctx);\n\tsnum=BN_CTX_get(ctx);\n\tsdiv=BN_CTX_get(ctx);\n\tif (dv == NULL)\n\t\tres=BN_CTX_get(ctx);\n\telse\tres=dv;\n\tif (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)\n\t\tgoto err;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tif (!(BN_lshift(sdiv,divisor,norm_shift))) goto err;\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tif (!(BN_lshift(snum,num,norm_shift))) goto err;\n\tsnum->neg=0;\n\tif (no_branch)\n\t\t{\n\t\tif (snum->top <= sdiv->top+1)\n\t\t\t{\n\t\t\tif (bn_wexpand(snum, sdiv->top + 2) == NULL) goto err;\n\t\t\tfor (i = snum->top; i < sdiv->top + 2; i++) snum->d[i] = 0;\n\t\t\tsnum->top = sdiv->top + 2;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (bn_wexpand(snum, snum->top + 1) == NULL) goto err;\n\t\t\tsnum->d[snum->top] = 0;\n\t\t\tsnum->top ++;\n\t\t\t}\n\t\t}\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\twnum.neg = 0;\n\twnum.d = &(snum->d[loop]);\n\twnum.top = div_n;\n\twnum.dmax = snum->dmax - loop;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop-no_branch;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (!no_branch)\n\t\t{\n\t\tif (BN_ucmp(&wnum,sdiv) >= 0)\n\t\t\t{\n\t\t\tbn_clear_top2max(&wnum);\n\t\t\tbn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n\t\t\t*resp=1;\n\t\t\t}\n\t\telse\n\t\t\tres->top--;\n\t\t}\n\tif (res->top == 0)\n\t\tres->neg = 0;\n\telse\n\t\tresp--;\n\tfor (i=0; i<loop-1; i++, wnump--, resp--)\n\t\t{\n\t\tBN_ULONG q,l0;\n#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n\t\tBN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG);\n\t\tq=bn_div_3_words(wnump,d1,d0);\n#else\n\t\tBN_ULONG n0,n1,rem=0;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\t{\n#ifdef BN_LLONG\n\t\t\tBN_ULLONG t2;\n#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n\t\t\tq=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);\n#else\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tt2 -= d1;\n\t\t\t\t}\n#else\n\t\t\tBN_ULONG t2l,t2h;\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#if defined(BN_UMULT_LOHI)\n\t\t\tBN_UMULT_LOHI(t2l,t2h,d1,q);\n#elif defined(BN_UMULT_HIGH)\n\t\t\tt2l = d1 * q;\n\t\t\tt2h = BN_UMULT_HIGH(d1,q);\n#else\n\t\t\t{\n\t\t\tBN_ULONG ql, qh;\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n\t\t\t}\n#endif\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif ((t2h < rem) ||\n\t\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t\t}\n#endif\n\t\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\twnum.d--;\n\t\tif (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1))\n\t\t\t{\n\t\t\tq--;\n\t\t\tif (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n\t\t\t\t(*wnump)++;\n\t\t\t}\n\t\t*resp = q;\n\t\t}\n\tbn_correct_top(snum);\n\tif (rm != NULL)\n\t\t{\n\t\tint neg = num->neg;\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\tif (!BN_is_zero(rm))\n\t\t\trm->neg = neg;\n\t\tbn_check_top(rm);\n\t\t}\n\tif (no_branch)\tbn_correct_top(res);\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'void BN_CTX_end(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_end", ctx);\n\tif(ctx->err_stack)\n\t\tctx->err_stack--;\n\telse\n\t\t{\n\t\tunsigned int fp = BN_STACK_pop(&ctx->stack);\n\t\tif(fp < ctx->used)\n\t\t\tBN_POOL_release(&ctx->pool, ctx->used - fp);\n\t\tctx->used = fp;\n\t\tctx->too_many = 0;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n\t{\n\treturn st->indexes[--(st->depth)];\n\t}']
3
0
https://github.com/openssl/openssl/blob/fff684168c7923aa85e6b4381d71d933396e32b0/crypto/rsa/rsa_sp800_56b_gen.c/#L125
int rsa_fips186_4_gen_prob_primes(RSA *rsa, BIGNUM *p1, BIGNUM *p2, BIGNUM *Xpout, const BIGNUM *Xp, const BIGNUM *Xp1, const BIGNUM *Xp2, BIGNUM *q1, BIGNUM *q2, BIGNUM *Xqout, const BIGNUM *Xq, const BIGNUM *Xq1, const BIGNUM *Xq2, int nbits, const BIGNUM *e, BN_CTX *ctx, BN_GENCB *cb) { int ret = 0, ok; BIGNUM *Xpo = NULL, *Xqo = NULL, *tmp = NULL; if (nbits < RSA_FIPS1864_MIN_KEYGEN_KEYSIZE) { RSAerr(RSA_F_RSA_FIPS186_4_GEN_PROB_PRIMES, RSA_R_INVALID_KEY_LENGTH); return 0; } if (!rsa_check_public_exponent(e)) { RSAerr(RSA_F_RSA_FIPS186_4_GEN_PROB_PRIMES, RSA_R_PUB_EXPONENT_OUT_OF_RANGE); goto err; } BN_CTX_start(ctx); tmp = BN_CTX_get(ctx); Xpo = (Xpout != NULL) ? Xpout : BN_CTX_get(ctx); Xqo = (Xqout != NULL) ? Xqout : BN_CTX_get(ctx); if (tmp == NULL || Xpo == NULL || Xqo == NULL) goto err; if (rsa->p == NULL) rsa->p = BN_secure_new(); if (rsa->q == NULL) rsa->q = BN_secure_new(); if (rsa->p == NULL || rsa->q == NULL) goto err; if (!bn_rsa_fips186_4_gen_prob_primes(rsa->p, Xpo, p1, p2, Xp, Xp1, Xp2, nbits, e, ctx, cb)) goto err; for(;;) { if (!bn_rsa_fips186_4_gen_prob_primes(rsa->q, Xqo, q1, q2, Xq, Xq1, Xq2, nbits, e, ctx, cb)) goto err; ok = rsa_check_pminusq_diff(tmp, Xpo, Xqo, nbits); if (ok < 0) goto err; if (ok == 0) continue; ok = rsa_check_pminusq_diff(tmp, rsa->p, rsa->q, nbits); if (ok < 0) goto err; if (ok == 0) continue; break; } ret = 1; err: if (Xpo != Xpout) BN_clear(Xpo); if (Xqo != Xqout) BN_clear(Xqo); BN_clear(tmp); BN_CTX_end(ctx); return ret; }
['int rsa_fips186_4_gen_prob_primes(RSA *rsa, BIGNUM *p1, BIGNUM *p2,\n BIGNUM *Xpout, const BIGNUM *Xp,\n const BIGNUM *Xp1, const BIGNUM *Xp2,\n BIGNUM *q1, BIGNUM *q2, BIGNUM *Xqout,\n const BIGNUM *Xq, const BIGNUM *Xq1,\n const BIGNUM *Xq2, int nbits,\n const BIGNUM *e, BN_CTX *ctx, BN_GENCB *cb)\n{\n int ret = 0, ok;\n BIGNUM *Xpo = NULL, *Xqo = NULL, *tmp = NULL;\n if (nbits < RSA_FIPS1864_MIN_KEYGEN_KEYSIZE) {\n RSAerr(RSA_F_RSA_FIPS186_4_GEN_PROB_PRIMES, RSA_R_INVALID_KEY_LENGTH);\n return 0;\n }\n if (!rsa_check_public_exponent(e)) {\n RSAerr(RSA_F_RSA_FIPS186_4_GEN_PROB_PRIMES,\n RSA_R_PUB_EXPONENT_OUT_OF_RANGE);\n goto err;\n }\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n Xpo = (Xpout != NULL) ? Xpout : BN_CTX_get(ctx);\n Xqo = (Xqout != NULL) ? Xqout : BN_CTX_get(ctx);\n if (tmp == NULL || Xpo == NULL || Xqo == NULL)\n goto err;\n if (rsa->p == NULL)\n rsa->p = BN_secure_new();\n if (rsa->q == NULL)\n rsa->q = BN_secure_new();\n if (rsa->p == NULL || rsa->q == NULL)\n goto err;\n if (!bn_rsa_fips186_4_gen_prob_primes(rsa->p, Xpo, p1, p2, Xp, Xp1, Xp2,\n nbits, e, ctx, cb))\n goto err;\n for(;;) {\n if (!bn_rsa_fips186_4_gen_prob_primes(rsa->q, Xqo, q1, q2, Xq, Xq1,\n Xq2, nbits, e, ctx, cb))\n goto err;\n ok = rsa_check_pminusq_diff(tmp, Xpo, Xqo, nbits);\n if (ok < 0)\n goto err;\n if (ok == 0)\n continue;\n ok = rsa_check_pminusq_diff(tmp, rsa->p, rsa->q, nbits);\n if (ok < 0)\n goto err;\n if (ok == 0)\n continue;\n break;\n }\n ret = 1;\nerr:\n if (Xpo != Xpout)\n BN_clear(Xpo);\n if (Xqo != Xqout)\n BN_clear(Xqo);\n BN_clear(tmp);\n BN_CTX_end(ctx);\n return ret;\n}', 'int rsa_check_public_exponent(const BIGNUM *e)\n{\n int bitlen = BN_num_bits(e);\n return (BN_is_odd(e) && bitlen > 16 && bitlen < 257);\n}', 'int BN_num_bits(const BIGNUM *a)\n{\n int i = a->top - 1;\n bn_check_top(a);\n if (BN_is_zero(a))\n return 0;\n return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));\n}', 'int BN_is_zero(const BIGNUM *a)\n{\n return a->top == 0;\n}', 'int BN_num_bits_word(BN_ULONG l)\n{\n BN_ULONG x, mask;\n int bits = (l != 0);\n#if BN_BITS2 > 32\n x = l >> 32;\n mask = (0 - x) & BN_MASK2;\n mask = (0 - (mask >> (BN_BITS2 - 1)));\n bits += 32 & mask;\n l ^= (x ^ l) & mask;\n#endif\n x = l >> 16;\n mask = (0 - x) & BN_MASK2;\n mask = (0 - (mask >> (BN_BITS2 - 1)));\n bits += 16 & mask;\n l ^= (x ^ l) & mask;\n x = l >> 8;\n mask = (0 - x) & BN_MASK2;\n mask = (0 - (mask >> (BN_BITS2 - 1)));\n bits += 8 & mask;\n l ^= (x ^ l) & mask;\n x = l >> 4;\n mask = (0 - x) & BN_MASK2;\n mask = (0 - (mask >> (BN_BITS2 - 1)));\n bits += 4 & mask;\n l ^= (x ^ l) & mask;\n x = l >> 2;\n mask = (0 - x) & BN_MASK2;\n mask = (0 - (mask >> (BN_BITS2 - 1)));\n bits += 2 & mask;\n l ^= (x ^ l) & mask;\n x = l >> 1;\n mask = (0 - x) & BN_MASK2;\n mask = (0 - (mask >> (BN_BITS2 - 1)));\n bits += 1 & mask;\n return bits;\n}', 'int BN_is_odd(const BIGNUM *a)\n{\n return (a->top > 0) && (a->d[0] & 1);\n}', 'void BN_clear(BIGNUM *a)\n{\n bn_check_top(a);\n if (a->d != NULL)\n OPENSSL_cleanse(a->d, sizeof(*a->d) * a->dmax);\n a->neg = 0;\n a->top = 0;\n a->flags &= ~BN_FLG_FIXED_TOP;\n}']
4
0
https://github.com/openssl/openssl/blob/d91f45688c2d0bfcc5b3b57fb20cc80b010eef0b/ssl/s3_enc.c/#L518
int ssl3_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p, size_t len, size_t *secret_size) { static const unsigned char *salt[3] = { #ifndef CHARSET_EBCDIC (const unsigned char *)"A", (const unsigned char *)"BB", (const unsigned char *)"CCC", #else (const unsigned char *)"\x41", (const unsigned char *)"\x42\x42", (const unsigned char *)"\x43\x43\x43", #endif }; unsigned char buf[EVP_MAX_MD_SIZE]; EVP_MD_CTX *ctx = EVP_MD_CTX_new(); int i, ret = 1; unsigned int n; size_t ret_secret_size = 0; if (ctx == NULL) { SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_GENERATE_MASTER_SECRET, ERR_R_MALLOC_FAILURE); return 0; } for (i = 0; i < 3; i++) { if (EVP_DigestInit_ex(ctx, s->ctx->sha1, NULL) <= 0 || EVP_DigestUpdate(ctx, salt[i], strlen((const char *)salt[i])) <= 0 || EVP_DigestUpdate(ctx, p, len) <= 0 || EVP_DigestUpdate(ctx, &(s->s3->client_random[0]), SSL3_RANDOM_SIZE) <= 0 || EVP_DigestUpdate(ctx, &(s->s3->server_random[0]), SSL3_RANDOM_SIZE) <= 0 || EVP_DigestFinal_ex(ctx, buf, &n) <= 0 || EVP_DigestInit_ex(ctx, s->ctx->md5, NULL) <= 0 || EVP_DigestUpdate(ctx, p, len) <= 0 || EVP_DigestUpdate(ctx, buf, n) <= 0 || EVP_DigestFinal_ex(ctx, out, &n) <= 0) { SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_GENERATE_MASTER_SECRET, ERR_R_INTERNAL_ERROR); ret = 0; break; } out += n; ret_secret_size += n; } EVP_MD_CTX_free(ctx); OPENSSL_cleanse(buf, sizeof(buf)); if (ret) *secret_size = ret_secret_size; return ret; }
['int ssl3_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,\n size_t len, size_t *secret_size)\n{\n static const unsigned char *salt[3] = {\n#ifndef CHARSET_EBCDIC\n (const unsigned char *)"A",\n (const unsigned char *)"BB",\n (const unsigned char *)"CCC",\n#else\n (const unsigned char *)"\\x41",\n (const unsigned char *)"\\x42\\x42",\n (const unsigned char *)"\\x43\\x43\\x43",\n#endif\n };\n unsigned char buf[EVP_MAX_MD_SIZE];\n EVP_MD_CTX *ctx = EVP_MD_CTX_new();\n int i, ret = 1;\n unsigned int n;\n size_t ret_secret_size = 0;\n if (ctx == NULL) {\n SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_GENERATE_MASTER_SECRET,\n ERR_R_MALLOC_FAILURE);\n return 0;\n }\n for (i = 0; i < 3; i++) {\n if (EVP_DigestInit_ex(ctx, s->ctx->sha1, NULL) <= 0\n || EVP_DigestUpdate(ctx, salt[i],\n strlen((const char *)salt[i])) <= 0\n || EVP_DigestUpdate(ctx, p, len) <= 0\n || EVP_DigestUpdate(ctx, &(s->s3->client_random[0]),\n SSL3_RANDOM_SIZE) <= 0\n || EVP_DigestUpdate(ctx, &(s->s3->server_random[0]),\n SSL3_RANDOM_SIZE) <= 0\n || EVP_DigestFinal_ex(ctx, buf, &n) <= 0\n || EVP_DigestInit_ex(ctx, s->ctx->md5, NULL) <= 0\n || EVP_DigestUpdate(ctx, p, len) <= 0\n || EVP_DigestUpdate(ctx, buf, n) <= 0\n || EVP_DigestFinal_ex(ctx, out, &n) <= 0) {\n SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n SSL_F_SSL3_GENERATE_MASTER_SECRET, ERR_R_INTERNAL_ERROR);\n ret = 0;\n break;\n }\n out += n;\n ret_secret_size += n;\n }\n EVP_MD_CTX_free(ctx);\n OPENSSL_cleanse(buf, sizeof(buf));\n if (ret)\n *secret_size = ret_secret_size;\n return ret;\n}', 'EVP_MD_CTX *EVP_MD_CTX_new(void)\n{\n return OPENSSL_zalloc(sizeof(EVP_MD_CTX));\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n INCREMENT(malloc_count);\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)(file); (void)(line);\n ret = malloc(num);\n#endif\n return ret;\n}', 'void ossl_statem_fatal(SSL *s, int al, int func, int reason, const char *file,\n int line)\n{\n}', 'void EVP_MD_CTX_free(EVP_MD_CTX *ctx)\n{\n EVP_MD_CTX_reset(ctx);\n OPENSSL_free(ctx);\n}', 'void CRYPTO_free(void *str, const char *file, int line)\n{\n INCREMENT(free_count);\n if (free_impl != NULL && free_impl != &CRYPTO_free) {\n free_impl(str, file, line);\n return;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_free(str, 0, file, line);\n free(str);\n CRYPTO_mem_debug_free(str, 1, file, line);\n } else {\n free(str);\n }\n#else\n free(str);\n#endif\n}']
5
0
https://github.com/openssl/openssl/blob/c21869fb07ea02ffd46e817caeb47d85a85ee8ef/crypto/pem/pvkfmt.c/#L760
static EVP_PKEY *do_PVK_body(const unsigned char **in, unsigned int saltlen, unsigned int keylen, pem_password_cb *cb, void *u) { EVP_PKEY *ret = NULL; const unsigned char *p = *in; unsigned int magic; unsigned char *enctmp = NULL, *q; EVP_CIPHER_CTX cctx; EVP_CIPHER_CTX_init(&cctx); if (saltlen) { char psbuf[PEM_BUFSIZE]; unsigned char keybuf[20]; int enctmplen, inlen; if (cb) inlen=cb(psbuf,PEM_BUFSIZE,0,u); else inlen=PEM_def_callback(psbuf,PEM_BUFSIZE,0,u); if (inlen <= 0) { PEMerr(PEM_F_DO_PVK_BODY,PEM_R_BAD_PASSWORD_READ); return NULL; } enctmp = OPENSSL_malloc(keylen + 8); if (!enctmp) { PEMerr(PEM_F_DO_PVK_BODY, ERR_R_MALLOC_FAILURE); return NULL; } if (!derive_pvk_key(keybuf, p, saltlen, (unsigned char *)psbuf, inlen)) return NULL; p += saltlen; memcpy(enctmp, p, 8); p += 8; inlen = keylen - 8; q = enctmp + 8; if (!EVP_DecryptInit_ex(&cctx, EVP_rc4(), NULL, keybuf, NULL)) goto err; if (!EVP_DecryptUpdate(&cctx, q, &enctmplen, p, inlen)) goto err; if (!EVP_DecryptFinal_ex(&cctx, q + enctmplen, &enctmplen)) goto err; magic = read_ledword((const unsigned char **)&q); if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) { q = enctmp + 8; memset(keybuf + 5, 0, 11); if (!EVP_DecryptInit_ex(&cctx, EVP_rc4(), NULL, keybuf, NULL)) goto err; OPENSSL_cleanse(keybuf, 20); if (!EVP_DecryptUpdate(&cctx, q, &enctmplen, p, inlen)) goto err; if (!EVP_DecryptFinal_ex(&cctx, q + enctmplen, &enctmplen)) goto err; magic = read_ledword((const unsigned char **)&q); if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) { PEMerr(PEM_F_DO_PVK_BODY, PEM_R_BAD_DECRYPT); goto err; } } else OPENSSL_cleanse(keybuf, 20); p = enctmp; } ret = b2i_PrivateKey(&p, keylen); err: EVP_CIPHER_CTX_cleanup(&cctx); if (enctmp && saltlen) OPENSSL_free(enctmp); return ret; }
['EVP_PKEY *b2i_PVK_bio(BIO *in, pem_password_cb *cb, void *u)\n\t{\n\tunsigned char pvk_hdr[24], *buf = NULL;\n\tconst unsigned char *p;\n\tint buflen;\n\tEVP_PKEY *ret = NULL;\n\tunsigned int saltlen, keylen;\n\tif (BIO_read(in, pvk_hdr, 24) != 24)\n\t\t{\n\t\tPEMerr(PEM_F_B2I_PVK_BIO, PEM_R_PVK_DATA_TOO_SHORT);\n\t\treturn NULL;\n\t\t}\n\tp = pvk_hdr;\n\tif (!do_PVK_header(&p, 24, 0, &saltlen, &keylen))\n\t\treturn 0;\n\tbuflen = (int) keylen + saltlen;\n\tbuf = OPENSSL_malloc(buflen);\n\tif (!buf)\n\t\t{\n\t\tPEMerr(PEM_F_B2I_PVK_BIO, ERR_R_MALLOC_FAILURE);\n\t\treturn 0;\n\t\t}\n\tp = buf;\n\tif (BIO_read(in, buf, buflen) != buflen)\n\t\t{\n\t\tPEMerr(PEM_F_B2I_PVK_BIO, PEM_R_PVK_DATA_TOO_SHORT);\n\t\tgoto err;\n\t\t}\n\tret = do_PVK_body(&p, saltlen, keylen, cb, u);\n\terr:\n\tif (buf)\n\t\t{\n\t\tOPENSSL_cleanse(buf, buflen);\n\t\tOPENSSL_free(buf);\n\t\t}\n\treturn ret;\n\t}', 'static int do_PVK_header(const unsigned char **in, unsigned int length,\n\t\tint skip_magic,\n\t \tunsigned int *psaltlen, unsigned int *pkeylen)\n\t{\n\tconst unsigned char *p = *in;\n\tunsigned int pvk_magic, keytype, is_encrypted;\n\tif (skip_magic)\n\t\t{\n\t\tif (length < 20)\n\t\t\t{\n\t\t\tPEMerr(PEM_F_DO_PVK_HEADER, PEM_R_PVK_TOO_SHORT);\n\t\t\treturn 0;\n\t\t\t}\n\t\tlength -= 20;\n\t\t}\n\telse\n\t\t{\n\t\tif (length < 24)\n\t\t\t{\n\t\t\tPEMerr(PEM_F_DO_PVK_HEADER, PEM_R_PVK_TOO_SHORT);\n\t\t\treturn 0;\n\t\t\t}\n\t\tlength -= 24;\n\t\tpvk_magic = read_ledword(&p);\n\t\tif (pvk_magic != MS_PVKMAGIC)\n\t\t\t{\n\t\t\tPEMerr(PEM_F_DO_PVK_HEADER, PEM_R_BAD_MAGIC_NUMBER);\n\t\t\treturn 0;\n\t\t\t}\n\t\t}\n\tp += 4;\n\tkeytype = read_ledword(&p);\n\tis_encrypted = read_ledword(&p);\n\t*psaltlen = read_ledword(&p);\n\t*pkeylen = read_ledword(&p);\n\tif (is_encrypted && !*psaltlen)\n\t\t{\n\t\tPEMerr(PEM_F_DO_PVK_HEADER, PEM_R_INCONSISTENT_HEADER);\n\t\treturn 0;\n\t\t}\n\t*in = p;\n\treturn 1;\n\t}', 'static EVP_PKEY *do_PVK_body(const unsigned char **in,\n\t\tunsigned int saltlen, unsigned int keylen,\n\t\tpem_password_cb *cb, void *u)\n\t{\n\tEVP_PKEY *ret = NULL;\n\tconst unsigned char *p = *in;\n\tunsigned int magic;\n\tunsigned char *enctmp = NULL, *q;\n\tEVP_CIPHER_CTX cctx;\n\tEVP_CIPHER_CTX_init(&cctx);\n\tif (saltlen)\n\t\t{\n\t\tchar psbuf[PEM_BUFSIZE];\n\t\tunsigned char keybuf[20];\n\t\tint enctmplen, inlen;\n\t\tif (cb)\n\t\t\tinlen=cb(psbuf,PEM_BUFSIZE,0,u);\n\t\telse\n\t\t\tinlen=PEM_def_callback(psbuf,PEM_BUFSIZE,0,u);\n\t\tif (inlen <= 0)\n\t\t\t{\n\t\t\tPEMerr(PEM_F_DO_PVK_BODY,PEM_R_BAD_PASSWORD_READ);\n\t\t\treturn NULL;\n\t\t\t}\n\t\tenctmp = OPENSSL_malloc(keylen + 8);\n\t\tif (!enctmp)\n\t\t\t{\n\t\t\tPEMerr(PEM_F_DO_PVK_BODY, ERR_R_MALLOC_FAILURE);\n\t\t\treturn NULL;\n\t\t\t}\n\t\tif (!derive_pvk_key(keybuf, p, saltlen,\n\t\t\t (unsigned char *)psbuf, inlen))\n\t\t\treturn NULL;\n\t\tp += saltlen;\n\t\tmemcpy(enctmp, p, 8);\n\t\tp += 8;\n\t\tinlen = keylen - 8;\n\t\tq = enctmp + 8;\n\t\tif (!EVP_DecryptInit_ex(&cctx, EVP_rc4(), NULL, keybuf, NULL))\n\t\t\tgoto err;\n\t\tif (!EVP_DecryptUpdate(&cctx, q, &enctmplen, p, inlen))\n\t\t\tgoto err;\n\t\tif (!EVP_DecryptFinal_ex(&cctx, q + enctmplen, &enctmplen))\n\t\t\tgoto err;\n\t\tmagic = read_ledword((const unsigned char **)&q);\n\t\tif (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC)\n\t\t\t{\n\t\t\tq = enctmp + 8;\n\t\t\tmemset(keybuf + 5, 0, 11);\n\t\t\tif (!EVP_DecryptInit_ex(&cctx, EVP_rc4(), NULL, keybuf,\n\t\t\t\t\t\t\t\tNULL))\n\t\t\t\tgoto err;\n\t\t\tOPENSSL_cleanse(keybuf, 20);\n\t\t\tif (!EVP_DecryptUpdate(&cctx, q, &enctmplen, p, inlen))\n\t\t\t\tgoto err;\n\t\t\tif (!EVP_DecryptFinal_ex(&cctx, q + enctmplen,\n\t\t\t\t\t\t\t\t&enctmplen))\n\t\t\t\tgoto err;\n\t\t\tmagic = read_ledword((const unsigned char **)&q);\n\t\t\tif (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC)\n\t\t\t\t{\n\t\t\t\tPEMerr(PEM_F_DO_PVK_BODY, PEM_R_BAD_DECRYPT);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\t}\n\t\telse\n\t\t\tOPENSSL_cleanse(keybuf, 20);\n\t\tp = enctmp;\n\t\t}\n\tret = b2i_PrivateKey(&p, keylen);\n\terr:\n\tEVP_CIPHER_CTX_cleanup(&cctx);\n\tif (enctmp && saltlen)\n\t\tOPENSSL_free(enctmp);\n\treturn ret;\n\t}']
6
0
https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/h264pred.c/#L359
static void pred4x4_horizontal_up_rv40_c(uint8_t *src, uint8_t *topright, int stride){ LOAD_LEFT_EDGE LOAD_DOWN_LEFT_EDGE LOAD_TOP_EDGE LOAD_TOP_RIGHT_EDGE src[0+0*stride]=(t1 + 2*t2 + t3 + 2*l0 + 2*l1 + 4)>>3; src[1+0*stride]=(t2 + 2*t3 + t4 + l0 + 2*l1 + l2 + 4)>>3; src[2+0*stride]= src[0+1*stride]=(t3 + 2*t4 + t5 + 2*l1 + 2*l2 + 4)>>3; src[3+0*stride]= src[1+1*stride]=(t4 + 2*t5 + t6 + l1 + 2*l2 + l3 + 4)>>3; src[2+1*stride]= src[0+2*stride]=(t5 + 2*t6 + t7 + 2*l2 + 2*l3 + 4)>>3; src[3+1*stride]= src[1+2*stride]=(t6 + 3*t7 + l2 + 3*l3 + 4)>>3; src[3+2*stride]= src[1+3*stride]=(l3 + 2*l4 + l5 + 2)>>2; src[0+3*stride]= src[2+2*stride]=(t6 + t7 + l3 + l4 + 2)>>2; src[2+3*stride]=(l4 + l5 + 1)>>1; src[3+3*stride]=(l4 + 2*l5 + l6 + 2)>>2; }
['static void pred4x4_horizontal_up_rv40_c(uint8_t *src, uint8_t *topright, int stride){\n LOAD_LEFT_EDGE\n LOAD_DOWN_LEFT_EDGE\n LOAD_TOP_EDGE\n LOAD_TOP_RIGHT_EDGE\n src[0+0*stride]=(t1 + 2*t2 + t3 + 2*l0 + 2*l1 + 4)>>3;\n src[1+0*stride]=(t2 + 2*t3 + t4 + l0 + 2*l1 + l2 + 4)>>3;\n src[2+0*stride]=\n src[0+1*stride]=(t3 + 2*t4 + t5 + 2*l1 + 2*l2 + 4)>>3;\n src[3+0*stride]=\n src[1+1*stride]=(t4 + 2*t5 + t6 + l1 + 2*l2 + l3 + 4)>>3;\n src[2+1*stride]=\n src[0+2*stride]=(t5 + 2*t6 + t7 + 2*l2 + 2*l3 + 4)>>3;\n src[3+1*stride]=\n src[1+2*stride]=(t6 + 3*t7 + l2 + 3*l3 + 4)>>3;\n src[3+2*stride]=\n src[1+3*stride]=(l3 + 2*l4 + l5 + 2)>>2;\n src[0+3*stride]=\n src[2+2*stride]=(t6 + t7 + l3 + l4 + 2)>>2;\n src[2+3*stride]=(l4 + l5 + 1)>>1;\n src[3+3*stride]=(l4 + 2*l5 + l6 + 2)>>2;\n}']
7
0
https://github.com/libav/libav/blob/8d44cd2cd82e27e6b051fe0606dece3b0bec0bcd/libavformat/spdifenc.c/#L352
static int spdif_header_aac(AVFormatContext *s, AVPacket *pkt) { IEC61937Context *ctx = s->priv_data; AACADTSHeaderInfo hdr; GetBitContext gbc; int ret; init_get_bits(&gbc, pkt->data, AAC_ADTS_HEADER_SIZE * 8); ret = ff_aac_parse_header(&gbc, &hdr); if (ret < 0) { av_log(s, AV_LOG_ERROR, "Wrong AAC file format\n"); return AVERROR_INVALIDDATA; } ctx->pkt_offset = hdr.samples << 2; switch (hdr.num_aac_frames) { case 1: ctx->data_type = IEC61937_MPEG2_AAC; break; case 2: ctx->data_type = IEC61937_MPEG2_AAC_LSF_2048; break; case 4: ctx->data_type = IEC61937_MPEG2_AAC_LSF_4096; break; default: av_log(s, AV_LOG_ERROR, "%i samples in AAC frame not supported\n", hdr.samples); return AVERROR(EINVAL); } return 0; }
['static int spdif_header_aac(AVFormatContext *s, AVPacket *pkt)\n{\n IEC61937Context *ctx = s->priv_data;\n AACADTSHeaderInfo hdr;\n GetBitContext gbc;\n int ret;\n init_get_bits(&gbc, pkt->data, AAC_ADTS_HEADER_SIZE * 8);\n ret = ff_aac_parse_header(&gbc, &hdr);\n if (ret < 0) {\n av_log(s, AV_LOG_ERROR, "Wrong AAC file format\\n");\n return AVERROR_INVALIDDATA;\n }\n ctx->pkt_offset = hdr.samples << 2;\n switch (hdr.num_aac_frames) {\n case 1:\n ctx->data_type = IEC61937_MPEG2_AAC;\n break;\n case 2:\n ctx->data_type = IEC61937_MPEG2_AAC_LSF_2048;\n break;\n case 4:\n ctx->data_type = IEC61937_MPEG2_AAC_LSF_4096;\n break;\n default:\n av_log(s, AV_LOG_ERROR, "%i samples in AAC frame not supported\\n",\n hdr.samples);\n return AVERROR(EINVAL);\n }\n return 0;\n}', 'static inline void init_get_bits(GetBitContext *s,\n const uint8_t *buffer, int bit_size)\n{\n int buffer_size = (bit_size+7)>>3;\n if (buffer_size < 0 || bit_size < 0) {\n buffer_size = bit_size = 0;\n buffer = NULL;\n }\n s->buffer = buffer;\n s->size_in_bits = bit_size;\n s->buffer_end = buffer + buffer_size;\n#ifdef ALT_BITSTREAM_READER\n s->index = 0;\n#elif defined A32_BITSTREAM_READER\n s->buffer_ptr = (uint32_t*)((intptr_t)buffer & ~3);\n s->bit_count = 32 + 8*((intptr_t)buffer & 3);\n skip_bits_long(s, 0);\n#endif\n}', 'int ff_aac_parse_header(GetBitContext *gbc, AACADTSHeaderInfo *hdr)\n{\n int size, rdb, ch, sr;\n int aot, crc_abs;\n if(get_bits(gbc, 12) != 0xfff)\n return AAC_AC3_PARSE_ERROR_SYNC;\n skip_bits1(gbc);\n skip_bits(gbc, 2);\n crc_abs = get_bits1(gbc);\n aot = get_bits(gbc, 2);\n sr = get_bits(gbc, 4);\n if(!ff_mpeg4audio_sample_rates[sr])\n return AAC_AC3_PARSE_ERROR_SAMPLE_RATE;\n skip_bits1(gbc);\n ch = get_bits(gbc, 3);\n skip_bits1(gbc);\n skip_bits1(gbc);\n skip_bits1(gbc);\n skip_bits1(gbc);\n size = get_bits(gbc, 13);\n if(size < AAC_ADTS_HEADER_SIZE)\n return AAC_AC3_PARSE_ERROR_FRAME_SIZE;\n skip_bits(gbc, 11);\n rdb = get_bits(gbc, 2);\n hdr->object_type = aot + 1;\n hdr->chan_config = ch;\n hdr->crc_absent = crc_abs;\n hdr->num_aac_frames = rdb + 1;\n hdr->sampling_index = sr;\n hdr->sample_rate = ff_mpeg4audio_sample_rates[sr];\n hdr->samples = (rdb + 1) * 1024;\n hdr->bit_rate = size * 8 * hdr->sample_rate / hdr->samples;\n return size;\n}', 'static inline unsigned int get_bits(GetBitContext *s, int n){\n register int tmp;\n OPEN_READER(re, s);\n UPDATE_CACHE(re, s);\n tmp = SHOW_UBITS(re, s, n);\n LAST_SKIP_BITS(re, s, n);\n CLOSE_READER(re, s);\n return tmp;\n}', 'static av_always_inline av_const uint32_t av_bswap32(uint32_t x)\n{\n x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);\n x= (x>>16) | (x<<16);\n return x;\n}', '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 inline unsigned int get_bits1(GetBitContext *s){\n#ifdef ALT_BITSTREAM_READER\n unsigned int index = s->index;\n uint8_t result = s->buffer[index>>3];\n#ifdef ALT_BITSTREAM_READER_LE\n result >>= index & 7;\n result &= 1;\n#else\n result <<= index & 7;\n result >>= 8 - 1;\n#endif\n index++;\n s->index = index;\n return result;\n#else\n return get_bits(s, 1);\n#endif\n}']
8
1
https://github.com/libav/libav/blob/63e8d9760f23a4edf81e9ae58c4f6d3baa6ff4dd/libavcodec/mpegaudiodec.c/#L681
static void apply_window_mp3_c(MPA_INT *synth_buf, MPA_INT *window, int *dither_state, OUT_INT *samples, int incr) { register const MPA_INT *w, *w2, *p; int j; OUT_INT *samples2; #if CONFIG_FLOAT float sum, sum2; #elif FRAC_BITS <= 15 int sum, sum2; #else int64_t sum, sum2; #endif memcpy(synth_buf + 512, synth_buf, 32 * sizeof(*synth_buf)); samples2 = samples + 31 * incr; w = window; w2 = window + 31; sum = *dither_state; p = synth_buf + 16; SUM8(MACS, sum, w, p); p = synth_buf + 48; SUM8(MLSS, sum, w + 32, p); *samples = round_sample(&sum); samples += incr; w++; for(j=1;j<16;j++) { sum2 = 0; p = synth_buf + 16 + j; SUM8P2(sum, MACS, sum2, MLSS, w, w2, p); p = synth_buf + 48 - j; SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p); *samples = round_sample(&sum); samples += incr; sum += sum2; *samples2 = round_sample(&sum); samples2 -= incr; w++; w2--; } p = synth_buf + 32; SUM8(MLSS, sum, w + 32, p); *samples = round_sample(&sum); *dither_state= sum; }
['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 INTFLOAT sb_samples[SBLIMIT])\n{\n register MPA_INT *synth_buf;\n int offset;\n#if FRAC_BITS <= 15\n int32_t tmp[32];\n int j;\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 apply_window_mp3_c(synth_buf, window, dither_state, samples, incr);\n offset = (offset - 32) & 511;\n *synth_buf_offset = offset;\n}', 'static void apply_window_mp3_c(MPA_INT *synth_buf, MPA_INT *window,\n int *dither_state, OUT_INT *samples, int incr)\n{\n register const MPA_INT *w, *w2, *p;\n int j;\n OUT_INT *samples2;\n#if CONFIG_FLOAT\n float sum, sum2;\n#elif FRAC_BITS <= 15\n int sum, sum2;\n#else\n int64_t sum, sum2;\n#endif\n memcpy(synth_buf + 512, synth_buf, 32 * sizeof(*synth_buf));\n samples2 = samples + 31 * incr;\n w = window;\n w2 = window + 31;\n sum = *dither_state;\n p = synth_buf + 16;\n SUM8(MACS, sum, w, p);\n p = synth_buf + 48;\n SUM8(MLSS, sum, w + 32, p);\n *samples = round_sample(&sum);\n samples += incr;\n w++;\n for(j=1;j<16;j++) {\n sum2 = 0;\n p = synth_buf + 16 + j;\n SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);\n p = synth_buf + 48 - j;\n SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);\n *samples = round_sample(&sum);\n samples += incr;\n sum += sum2;\n *samples2 = round_sample(&sum);\n samples2 -= incr;\n w++;\n w2--;\n }\n p = synth_buf + 32;\n SUM8(MLSS, sum, w + 32, p);\n *samples = round_sample(&sum);\n *dither_state= sum;\n}']
9
0
https://github.com/libav/libav/blob/a92be9b856bd11b081041c43c25d442028fe9a63/libavcodec/smacker.c/#L610
static int smka_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt) { SmackerAudioContext *s = avctx->priv_data; const uint8_t *buf = avpkt->data; int buf_size = avpkt->size; GetBitContext gb; HuffContext h[4] = { { 0 } }; VLC vlc[4] = { { 0 } }; int16_t *samples; uint8_t *samples8; int val; int i, res, ret; int unp_size; int bits, stereo; int pred[2] = {0, 0}; if (buf_size <= 4) { av_log(avctx, AV_LOG_ERROR, "packet is too small\n"); return AVERROR(EINVAL); } unp_size = AV_RL32(buf); init_get_bits(&gb, buf + 4, (buf_size - 4) * 8); if(!get_bits1(&gb)){ av_log(avctx, AV_LOG_INFO, "Sound: no data\n"); *got_frame_ptr = 0; return 1; } stereo = get_bits1(&gb); bits = get_bits1(&gb); if (stereo ^ (avctx->channels != 1)) { av_log(avctx, AV_LOG_ERROR, "channels mismatch\n"); return AVERROR(EINVAL); } if (bits && avctx->sample_fmt == AV_SAMPLE_FMT_U8) { av_log(avctx, AV_LOG_ERROR, "sample format mismatch\n"); return AVERROR(EINVAL); } s->frame.nb_samples = unp_size / (avctx->channels * (bits + 1)); if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) { av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); return ret; } samples = (int16_t *)s->frame.data[0]; samples8 = s->frame.data[0]; for(i = 0; i < (1 << (bits + stereo)); i++) { h[i].length = 256; h[i].maxlength = 0; h[i].current = 0; h[i].bits = av_mallocz(256 * 4); h[i].lengths = av_mallocz(256 * sizeof(int)); h[i].values = av_mallocz(256 * sizeof(int)); skip_bits1(&gb); smacker_decode_tree(&gb, &h[i], 0, 0); skip_bits1(&gb); if(h[i].current > 1) { res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length, h[i].lengths, sizeof(int), sizeof(int), h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE); if(res < 0) { av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\n"); return -1; } } } if(bits) { for(i = stereo; i >= 0; i--) pred[i] = av_bswap16(get_bits(&gb, 16)); for(i = 0; i <= stereo; i++) *samples++ = pred[i]; for(; i < unp_size / 2; i++) { if(i & stereo) { if(vlc[2].table) res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3); else res = 0; val = h[2].values[res]; if(vlc[3].table) res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3); else res = 0; val |= h[3].values[res] << 8; pred[1] += sign_extend(val, 16); *samples++ = av_clip_int16(pred[1]); } else { if(vlc[0].table) res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3); else res = 0; val = h[0].values[res]; if(vlc[1].table) res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3); else res = 0; val |= h[1].values[res] << 8; pred[0] += sign_extend(val, 16); *samples++ = av_clip_int16(pred[0]); } } } else { for(i = stereo; i >= 0; i--) pred[i] = get_bits(&gb, 8); for(i = 0; i <= stereo; i++) *samples8++ = pred[i]; for(; i < unp_size; i++) { if(i & stereo){ if(vlc[1].table) res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3); else res = 0; pred[1] += sign_extend(h[1].values[res], 8); *samples8++ = av_clip_uint8(pred[1]); } else { if(vlc[0].table) res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3); else res = 0; pred[0] += sign_extend(h[0].values[res], 8); *samples8++ = av_clip_uint8(pred[0]); } } } for(i = 0; i < 4; i++) { if(vlc[i].table) ff_free_vlc(&vlc[i]); av_free(h[i].bits); av_free(h[i].lengths); av_free(h[i].values); } *got_frame_ptr = 1; *(AVFrame *)data = s->frame; return buf_size; }
['static int smka_decode_frame(AVCodecContext *avctx, void *data,\n int *got_frame_ptr, AVPacket *avpkt)\n{\n SmackerAudioContext *s = avctx->priv_data;\n const uint8_t *buf = avpkt->data;\n int buf_size = avpkt->size;\n GetBitContext gb;\n HuffContext h[4] = { { 0 } };\n VLC vlc[4] = { { 0 } };\n int16_t *samples;\n uint8_t *samples8;\n int val;\n int i, res, ret;\n int unp_size;\n int bits, stereo;\n int pred[2] = {0, 0};\n if (buf_size <= 4) {\n av_log(avctx, AV_LOG_ERROR, "packet is too small\\n");\n return AVERROR(EINVAL);\n }\n unp_size = AV_RL32(buf);\n init_get_bits(&gb, buf + 4, (buf_size - 4) * 8);\n if(!get_bits1(&gb)){\n av_log(avctx, AV_LOG_INFO, "Sound: no data\\n");\n *got_frame_ptr = 0;\n return 1;\n }\n stereo = get_bits1(&gb);\n bits = get_bits1(&gb);\n if (stereo ^ (avctx->channels != 1)) {\n av_log(avctx, AV_LOG_ERROR, "channels mismatch\\n");\n return AVERROR(EINVAL);\n }\n if (bits && avctx->sample_fmt == AV_SAMPLE_FMT_U8) {\n av_log(avctx, AV_LOG_ERROR, "sample format mismatch\\n");\n return AVERROR(EINVAL);\n }\n s->frame.nb_samples = unp_size / (avctx->channels * (bits + 1));\n if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {\n av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n return ret;\n }\n samples = (int16_t *)s->frame.data[0];\n samples8 = s->frame.data[0];\n for(i = 0; i < (1 << (bits + stereo)); i++) {\n h[i].length = 256;\n h[i].maxlength = 0;\n h[i].current = 0;\n h[i].bits = av_mallocz(256 * 4);\n h[i].lengths = av_mallocz(256 * sizeof(int));\n h[i].values = av_mallocz(256 * sizeof(int));\n skip_bits1(&gb);\n smacker_decode_tree(&gb, &h[i], 0, 0);\n skip_bits1(&gb);\n if(h[i].current > 1) {\n res = init_vlc(&vlc[i], SMKTREE_BITS, h[i].length,\n h[i].lengths, sizeof(int), sizeof(int),\n h[i].bits, sizeof(uint32_t), sizeof(uint32_t), INIT_VLC_LE);\n if(res < 0) {\n av_log(avctx, AV_LOG_ERROR, "Cannot build VLC table\\n");\n return -1;\n }\n }\n }\n if(bits) {\n for(i = stereo; i >= 0; i--)\n pred[i] = av_bswap16(get_bits(&gb, 16));\n for(i = 0; i <= stereo; i++)\n *samples++ = pred[i];\n for(; i < unp_size / 2; i++) {\n if(i & stereo) {\n if(vlc[2].table)\n res = get_vlc2(&gb, vlc[2].table, SMKTREE_BITS, 3);\n else\n res = 0;\n val = h[2].values[res];\n if(vlc[3].table)\n res = get_vlc2(&gb, vlc[3].table, SMKTREE_BITS, 3);\n else\n res = 0;\n val |= h[3].values[res] << 8;\n pred[1] += sign_extend(val, 16);\n *samples++ = av_clip_int16(pred[1]);\n } else {\n if(vlc[0].table)\n res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);\n else\n res = 0;\n val = h[0].values[res];\n if(vlc[1].table)\n res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);\n else\n res = 0;\n val |= h[1].values[res] << 8;\n pred[0] += sign_extend(val, 16);\n *samples++ = av_clip_int16(pred[0]);\n }\n }\n } else {\n for(i = stereo; i >= 0; i--)\n pred[i] = get_bits(&gb, 8);\n for(i = 0; i <= stereo; i++)\n *samples8++ = pred[i];\n for(; i < unp_size; i++) {\n if(i & stereo){\n if(vlc[1].table)\n res = get_vlc2(&gb, vlc[1].table, SMKTREE_BITS, 3);\n else\n res = 0;\n pred[1] += sign_extend(h[1].values[res], 8);\n *samples8++ = av_clip_uint8(pred[1]);\n } else {\n if(vlc[0].table)\n res = get_vlc2(&gb, vlc[0].table, SMKTREE_BITS, 3);\n else\n res = 0;\n pred[0] += sign_extend(h[0].values[res], 8);\n *samples8++ = av_clip_uint8(pred[0]);\n }\n }\n }\n for(i = 0; i < 4; i++) {\n if(vlc[i].table)\n ff_free_vlc(&vlc[i]);\n av_free(h[i].bits);\n av_free(h[i].lengths);\n av_free(h[i].values);\n }\n *got_frame_ptr = 1;\n *(AVFrame *)data = s->frame;\n return buf_size;\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}']
10
0
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/test/exptest.c/#L177
static int test_exp_mod_zero() { BIGNUM *a = NULL, *p = NULL, *m = NULL; BIGNUM *r = NULL; BN_ULONG one_word = 1; BN_CTX *ctx = BN_CTX_new(); int ret = 1, failed = 0; m = BN_new(); if (!m) goto err; BN_one(m); a = BN_new(); if (!a) goto err; BN_one(a); p = BN_new(); if (!p) goto err; BN_zero(p); r = BN_new(); if (!r) goto err; if (!BN_rand(a, 1024, 0, 0)) goto err; if (!BN_mod_exp(r, a, p, m, ctx)) goto err; if (!a_is_zero_mod_one("BN_mod_exp", r, a)) failed = 1; if (!BN_mod_exp_recp(r, a, p, m, ctx)) goto err; if (!a_is_zero_mod_one("BN_mod_exp_recp", r, a)) failed = 1; if (!BN_mod_exp_simple(r, a, p, m, ctx)) goto err; if (!a_is_zero_mod_one("BN_mod_exp_simple", r, a)) failed = 1; if (!BN_mod_exp_mont(r, a, p, m, ctx, NULL)) goto err; if (!a_is_zero_mod_one("BN_mod_exp_mont", r, a)) failed = 1; if (!BN_mod_exp_mont_consttime(r, a, p, m, ctx, NULL)) { goto err; } if (!a_is_zero_mod_one("BN_mod_exp_mont_consttime", r, a)) failed = 1; if (!BN_mod_exp_mont_word(r, one_word, p, m, ctx, NULL)) goto err; if (!BN_is_zero(r)) { fprintf(stderr, "BN_mod_exp_mont_word failed:\n"); fprintf(stderr, "1 ** 0 mod 1 = r (should be 0)\n"); fprintf(stderr, "r = "); BN_print_fp(stderr, r); fprintf(stderr, "\n"); return 0; } ret = failed; err: BN_free(r); BN_free(a); BN_free(p); BN_free(m); BN_CTX_free(ctx); return ret; }
['static int test_exp_mod_zero()\n{\n BIGNUM *a = NULL, *p = NULL, *m = NULL;\n BIGNUM *r = NULL;\n BN_ULONG one_word = 1;\n BN_CTX *ctx = BN_CTX_new();\n int ret = 1, failed = 0;\n m = BN_new();\n if (!m)\n goto err;\n BN_one(m);\n a = BN_new();\n if (!a)\n goto err;\n BN_one(a);\n p = BN_new();\n if (!p)\n goto err;\n BN_zero(p);\n r = BN_new();\n if (!r)\n goto err;\n if (!BN_rand(a, 1024, 0, 0))\n goto err;\n if (!BN_mod_exp(r, a, p, m, ctx))\n goto err;\n if (!a_is_zero_mod_one("BN_mod_exp", r, a))\n failed = 1;\n if (!BN_mod_exp_recp(r, a, p, m, ctx))\n goto err;\n if (!a_is_zero_mod_one("BN_mod_exp_recp", r, a))\n failed = 1;\n if (!BN_mod_exp_simple(r, a, p, m, ctx))\n goto err;\n if (!a_is_zero_mod_one("BN_mod_exp_simple", r, a))\n failed = 1;\n if (!BN_mod_exp_mont(r, a, p, m, ctx, NULL))\n goto err;\n if (!a_is_zero_mod_one("BN_mod_exp_mont", r, a))\n failed = 1;\n if (!BN_mod_exp_mont_consttime(r, a, p, m, ctx, NULL)) {\n goto err;\n }\n if (!a_is_zero_mod_one("BN_mod_exp_mont_consttime", r, a))\n failed = 1;\n if (!BN_mod_exp_mont_word(r, one_word, p, m, ctx, NULL))\n goto err;\n if (!BN_is_zero(r)) {\n fprintf(stderr, "BN_mod_exp_mont_word failed:\\n");\n fprintf(stderr, "1 ** 0 mod 1 = r (should be 0)\\n");\n fprintf(stderr, "r = ");\n BN_print_fp(stderr, r);\n fprintf(stderr, "\\n");\n return 0;\n }\n ret = failed;\n err:\n BN_free(r);\n BN_free(a);\n BN_free(p);\n BN_free(m);\n BN_CTX_free(ctx);\n return ret;\n}', 'BN_CTX *BN_CTX_new(void)\n{\n BN_CTX *ret;\n if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {\n BNerr(BN_F_BN_CTX_NEW, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n BN_POOL_init(&ret->pool);\n BN_STACK_init(&ret->stack);\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#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}', 'static void BN_POOL_init(BN_POOL *p)\n{\n p->head = p->current = p->tail = NULL;\n p->used = p->size = 0;\n}', 'static void BN_STACK_init(BN_STACK *st)\n{\n st->indexes = NULL;\n st->depth = st->size = 0;\n}', 'BIGNUM *BN_new(void)\n{\n BIGNUM *ret;\n if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {\n BNerr(BN_F_BN_NEW, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n ret->flags = BN_FLG_MALLOCED;\n bn_check_top(ret);\n return (ret);\n}', '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}', 'void RAND_add(const void *buf, int num, double entropy)\n{\n const RAND_METHOD *meth = RAND_get_rand_method();\n if (meth && meth->add)\n meth->add(buf, num, entropy);\n}', 'const RAND_METHOD *RAND_get_rand_method(void)\n{\n if (!default_RAND_meth) {\n#ifndef OPENSSL_NO_ENGINE\n ENGINE *e = ENGINE_get_default_RAND();\n if (e) {\n default_RAND_meth = ENGINE_get_RAND(e);\n if (!default_RAND_meth) {\n ENGINE_finish(e);\n e = NULL;\n }\n }\n if (e)\n funct_ref = e;\n else\n#endif\n default_RAND_meth = RAND_OpenSSL();\n }\n return default_RAND_meth;\n}', 'ENGINE *ENGINE_get_default_RAND(void)\n{\n return engine_table_select(&rand_table, dummy_nid);\n}', 'ENGINE *engine_table_select(ENGINE_TABLE **table, int nid)\n#else\nENGINE *engine_table_select_tmp(ENGINE_TABLE **table, int nid, const char *f,\n int l)\n#endif\n{\n ENGINE *ret = NULL;\n ENGINE_PILE tmplate, *fnd = NULL;\n int initres, loop = 0;\n if (!(*table)) {\n#ifdef ENGINE_TABLE_DEBUG\n fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, nothing "\n "registered!\\n", f, l, nid);\n#endif\n return NULL;\n }\n ERR_set_mark();\n CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);\n if (!int_table_check(table, 0))\n goto end;\n tmplate.nid = nid;\n fnd = lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate);\n if (!fnd)\n goto end;\n if (fnd->funct && engine_unlocked_init(fnd->funct)) {\n#ifdef ENGINE_TABLE_DEBUG\n fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, using "\n "ENGINE \'%s\' cached\\n", f, l, nid, fnd->funct->id);\n#endif\n ret = fnd->funct;\n goto end;\n }\n if (fnd->uptodate) {\n ret = fnd->funct;\n goto end;\n }\n trynext:\n ret = sk_ENGINE_value(fnd->sk, loop++);\n if (!ret) {\n#ifdef ENGINE_TABLE_DEBUG\n fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, no "\n "registered implementations would initialise\\n", f, l, nid);\n#endif\n goto end;\n }\n if ((ret->funct_ref > 0) || !(table_flags & ENGINE_TABLE_FLAG_NOINIT))\n initres = engine_unlocked_init(ret);\n else\n initres = 0;\n if (initres) {\n if ((fnd->funct != ret) && engine_unlocked_init(ret)) {\n if (fnd->funct)\n engine_unlocked_finish(fnd->funct, 0);\n fnd->funct = ret;\n#ifdef ENGINE_TABLE_DEBUG\n fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, "\n "setting default to \'%s\'\\n", f, l, nid, ret->id);\n#endif\n }\n#ifdef ENGINE_TABLE_DEBUG\n fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, using "\n "newly initialised \'%s\'\\n", f, l, nid, ret->id);\n#endif\n goto end;\n }\n goto trynext;\n end:\n if (fnd)\n fnd->uptodate = 1;\n#ifdef ENGINE_TABLE_DEBUG\n if (ret)\n fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, caching "\n "ENGINE \'%s\'\\n", f, l, nid, ret->id);\n else\n fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, caching "\n "\'no matching ENGINE\'\\n", f, l, nid);\n#endif\n CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);\n ERR_pop_to_mark();\n return ret;\n}', 'RAND_METHOD *RAND_OpenSSL(void)\n{\n return (&rand_meth);\n}', 'int RAND_bytes(unsigned char *buf, int num)\n{\n const RAND_METHOD *meth = RAND_get_rand_method();\n if (meth && meth->bytes)\n return meth->bytes(buf, num);\n return (-1);\n}', 'void CRYPTO_clear_free(void *str, size_t num)\n{\n if (str == NULL)\n return;\n if (num)\n OPENSSL_cleanse(str, num);\n CRYPTO_free(str);\n}', 'void CRYPTO_free(void *str)\n{\n#ifdef CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_free(str, 0);\n free(str);\n CRYPTO_mem_debug_free(str, 1);\n } else {\n free(str);\n }\n#else\n free(str);\n#endif\n}', 'void BN_free(BIGNUM *a)\n{\n if (a == NULL)\n return;\n bn_check_top(a);\n if (!BN_get_flags(a, BN_FLG_STATIC_DATA))\n bn_free_d(a);\n if (a->flags & BN_FLG_MALLOCED)\n OPENSSL_free(a);\n else {\n#if OPENSSL_API_COMPAT < 0x00908000L\n a->flags |= BN_FLG_FREE;\n#endif\n a->d = NULL;\n }\n}', 'int BN_get_flags(const BIGNUM *b, int n)\n{\n return b->flags & n;\n}']
11
0
https://github.com/libav/libav/blob/ab492ca2ab105aeb24d955f3f03756bdb3139ee1/libavcodec/mpeg12.c/#L198
static inline int mpeg1_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n) { int level, i, j, run; RLTable *rl = &ff_rl_mpeg1; uint8_t * const scantable = s->intra_scantable.permutated; const uint16_t *quant_matrix = s->inter_matrix; const int qscale = s->qscale; { OPEN_READER(re, &s->gb); i = -1; UPDATE_CACHE(re, &s->gb); if (((int32_t)GET_CACHE(re, &s->gb)) < 0) { level = (3 * qscale * quant_matrix[0]) >> 5; level = (level - 1) | 1; if (GET_CACHE(re, &s->gb) & 0x40000000) level = -level; block[0] = level; i++; SKIP_BITS(re, &s->gb, 2); if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF) goto end; } for (;;) { GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0); if (level != 0) { i += run; j = scantable[i]; level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5; level = (level - 1) | 1; level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1); } else { run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6); UPDATE_CACHE(re, &s->gb); level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8); if (level == -128) { level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8); } else if (level == 0) { level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8); } i += run; j = scantable[i]; if (level < 0) { level = -level; level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5; level = (level - 1) | 1; level = -level; } else { level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5; level = (level - 1) | 1; } } if (i > 63) { av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y); return -1; } block[j] = level; if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF) break; UPDATE_CACHE(re, &s->gb); } end: LAST_SKIP_BITS(re, &s->gb, 2); CLOSE_READER(re, &s->gb); } s->block_last_index[n] = i; return 0; }
['static inline int mpeg1_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)\n{\n int level, i, j, run;\n RLTable *rl = &ff_rl_mpeg1;\n uint8_t * const scantable = s->intra_scantable.permutated;\n const uint16_t *quant_matrix = s->inter_matrix;\n const int qscale = s->qscale;\n {\n OPEN_READER(re, &s->gb);\n i = -1;\n UPDATE_CACHE(re, &s->gb);\n if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {\n level = (3 * qscale * quant_matrix[0]) >> 5;\n level = (level - 1) | 1;\n if (GET_CACHE(re, &s->gb) & 0x40000000)\n level = -level;\n block[0] = level;\n i++;\n SKIP_BITS(re, &s->gb, 2);\n if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)\n goto end;\n }\n for (;;) {\n GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);\n if (level != 0) {\n i += run;\n j = scantable[i];\n level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;\n level = (level - 1) | 1;\n level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);\n SKIP_BITS(re, &s->gb, 1);\n } else {\n run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);\n UPDATE_CACHE(re, &s->gb);\n level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);\n if (level == -128) {\n level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);\n } else if (level == 0) {\n level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);\n }\n i += run;\n j = scantable[i];\n if (level < 0) {\n level = -level;\n level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;\n level = (level - 1) | 1;\n level = -level;\n } else {\n level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;\n level = (level - 1) | 1;\n }\n }\n if (i > 63) {\n av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\\n", s->mb_x, s->mb_y);\n return -1;\n }\n block[j] = level;\n if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)\n break;\n UPDATE_CACHE(re, &s->gb);\n }\nend:\n LAST_SKIP_BITS(re, &s->gb, 2);\n CLOSE_READER(re, &s->gb);\n }\n s->block_last_index[n] = i;\n return 0;\n}']
12
0
https://github.com/openssl/openssl/blob/a21285b3636a8356f01027416b0cd43b016f58ca/crypto/bn/bn_lib.c/#L291
BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b) { bn_check_top(b); if (a == b) return a; if (bn_wexpand(a, b->top) == NULL) return NULL; if (b->top > 0) memcpy(a->d, b->d, sizeof(b->d[0]) * b->top); a->neg = b->neg; a->top = b->top; a->flags |= b->flags & BN_FLG_FIXED_TOP; bn_check_top(a); return a; }
['int dsa_builtin_paramgen2(DSA *ret, size_t L, size_t N,\n const EVP_MD *evpmd, const unsigned char *seed_in,\n size_t seed_len, int idx, unsigned char *seed_out,\n int *counter_ret, unsigned long *h_ret,\n BN_GENCB *cb)\n{\n int ok = -1;\n unsigned char *seed = NULL, *seed_tmp = NULL;\n unsigned char md[EVP_MAX_MD_SIZE];\n int mdsize;\n BIGNUM *r0, *W, *X, *c, *test;\n BIGNUM *g = NULL, *q = NULL, *p = NULL;\n BN_MONT_CTX *mont = NULL;\n int i, k, n = 0, m = 0, qsize = N >> 3;\n int counter = 0;\n int r = 0;\n BN_CTX *ctx = NULL;\n EVP_MD_CTX *mctx = EVP_MD_CTX_new();\n unsigned int h = 2;\n if (mctx == NULL)\n goto err;\n if (evpmd == NULL) {\n if (N == 160)\n evpmd = EVP_sha1();\n else if (N == 224)\n evpmd = EVP_sha224();\n else\n evpmd = EVP_sha256();\n }\n mdsize = EVP_MD_size(evpmd);\n if (!ret->p || !ret->q || idx >= 0) {\n if (seed_len == 0)\n seed_len = mdsize;\n seed = OPENSSL_malloc(seed_len);\n if (seed_out)\n seed_tmp = seed_out;\n else\n seed_tmp = OPENSSL_malloc(seed_len);\n if (seed == NULL || seed_tmp == NULL)\n goto err;\n if (seed_in)\n memcpy(seed, seed_in, seed_len);\n }\n if ((ctx = BN_CTX_new()) == NULL)\n goto err;\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n BN_CTX_start(ctx);\n r0 = BN_CTX_get(ctx);\n g = BN_CTX_get(ctx);\n W = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n c = BN_CTX_get(ctx);\n test = BN_CTX_get(ctx);\n if (test == NULL)\n goto err;\n if (ret->p && ret->q) {\n p = ret->p;\n q = ret->q;\n if (idx >= 0)\n memcpy(seed_tmp, seed, seed_len);\n goto g_only;\n } else {\n p = BN_CTX_get(ctx);\n q = BN_CTX_get(ctx);\n if (q == NULL)\n goto err;\n }\n if (!BN_lshift(test, BN_value_one(), L - 1))\n goto err;\n for (;;) {\n for (;;) {\n unsigned char *pmd;\n if (!BN_GENCB_call(cb, 0, m++))\n goto err;\n if (!seed_in) {\n if (RAND_bytes(seed, seed_len) <= 0)\n goto err;\n }\n if (!EVP_Digest(seed, seed_len, md, NULL, evpmd, NULL))\n goto err;\n if (mdsize > qsize)\n pmd = md + mdsize - qsize;\n else\n pmd = md;\n if (mdsize < qsize)\n memset(md + mdsize, 0, qsize - mdsize);\n pmd[0] |= 0x80;\n pmd[qsize - 1] |= 0x01;\n if (!BN_bin2bn(pmd, qsize, q))\n goto err;\n r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx,\n seed_in ? 1 : 0, cb);\n if (r > 0)\n break;\n if (r != 0)\n goto err;\n if (seed_in) {\n ok = 0;\n DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN2, DSA_R_Q_NOT_PRIME);\n goto err;\n }\n }\n if (seed_out)\n memcpy(seed_out, seed, seed_len);\n if (!BN_GENCB_call(cb, 2, 0))\n goto err;\n if (!BN_GENCB_call(cb, 3, 0))\n goto err;\n counter = 0;\n n = (L - 1) / (mdsize << 3);\n for (;;) {\n if ((counter != 0) && !BN_GENCB_call(cb, 0, counter))\n goto err;\n BN_zero(W);\n for (k = 0; k <= n; k++) {\n for (i = seed_len - 1; i >= 0; i--) {\n seed[i]++;\n if (seed[i] != 0)\n break;\n }\n if (!EVP_Digest(seed, seed_len, md, NULL, evpmd, NULL))\n goto err;\n if (!BN_bin2bn(md, mdsize, r0))\n goto err;\n if (!BN_lshift(r0, r0, (mdsize << 3) * k))\n goto err;\n if (!BN_add(W, W, r0))\n goto err;\n }\n if (!BN_mask_bits(W, L - 1))\n goto err;\n if (!BN_copy(X, W))\n goto err;\n if (!BN_add(X, X, test))\n goto err;\n if (!BN_lshift1(r0, q))\n goto err;\n if (!BN_mod(c, X, r0, ctx))\n goto err;\n if (!BN_sub(r0, c, BN_value_one()))\n goto err;\n if (!BN_sub(p, X, r0))\n goto err;\n if (BN_cmp(p, test) >= 0) {\n r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb);\n if (r > 0)\n goto end;\n if (r != 0)\n goto err;\n }\n counter++;\n if (counter >= (int)(4 * L))\n break;\n }\n if (seed_in) {\n ok = 0;\n DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN2, DSA_R_INVALID_PARAMETERS);\n goto err;\n }\n }\n end:\n if (!BN_GENCB_call(cb, 2, 1))\n goto err;\n g_only:\n if (!BN_sub(test, p, BN_value_one()))\n goto err;\n if (!BN_div(r0, NULL, test, q, ctx))\n goto err;\n if (idx < 0) {\n if (!BN_set_word(test, h))\n goto err;\n } else\n h = 1;\n if (!BN_MONT_CTX_set(mont, p, ctx))\n goto err;\n for (;;) {\n static const unsigned char ggen[4] = { 0x67, 0x67, 0x65, 0x6e };\n if (idx >= 0) {\n md[0] = idx & 0xff;\n md[1] = (h >> 8) & 0xff;\n md[2] = h & 0xff;\n if (!EVP_DigestInit_ex(mctx, evpmd, NULL))\n goto err;\n if (!EVP_DigestUpdate(mctx, seed_tmp, seed_len))\n goto err;\n if (!EVP_DigestUpdate(mctx, ggen, sizeof(ggen)))\n goto err;\n if (!EVP_DigestUpdate(mctx, md, 3))\n goto err;\n if (!EVP_DigestFinal_ex(mctx, md, NULL))\n goto err;\n if (!BN_bin2bn(md, mdsize, test))\n goto err;\n }\n if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont))\n goto err;\n if (!BN_is_one(g))\n break;\n if (idx < 0 && !BN_add(test, test, BN_value_one()))\n goto err;\n h++;\n if (idx >= 0 && h > 0xffff)\n goto err;\n }\n if (!BN_GENCB_call(cb, 3, 1))\n goto err;\n ok = 1;\n err:\n if (ok == 1) {\n if (p != ret->p) {\n BN_free(ret->p);\n ret->p = BN_dup(p);\n }\n if (q != ret->q) {\n BN_free(ret->q);\n ret->q = BN_dup(q);\n }\n BN_free(ret->g);\n ret->g = BN_dup(g);\n if (ret->p == NULL || ret->q == NULL || ret->g == NULL) {\n ok = -1;\n goto err;\n }\n if (counter_ret != NULL)\n *counter_ret = counter;\n if (h_ret != NULL)\n *h_ret = h;\n }\n OPENSSL_free(seed);\n if (seed_out != seed_tmp)\n OPENSSL_free(seed_tmp);\n if (ctx)\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n BN_MONT_CTX_free(mont);\n EVP_MD_CTX_free(mctx);\n return ok;\n}', 'int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l;\n bn_check_top(r);\n bn_check_top(a);\n if (n < 0) {\n BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n nw = n / BN_BITS2;\n if (bn_wexpand(r, a->top + nw + 1) == NULL)\n return 0;\n r->neg = a->neg;\n lb = n % BN_BITS2;\n rb = BN_BITS2 - lb;\n f = a->d;\n t = r->d;\n t[a->top + nw] = 0;\n if (lb == 0)\n for (i = a->top - 1; i >= 0; i--)\n t[nw + i] = f[i];\n else\n for (i = a->top - 1; i >= 0; i--) {\n l = f[i];\n t[nw + i + 1] |= (l >> rb) & BN_MASK2;\n t[nw + i] = (l << lb) & BN_MASK2;\n }\n memset(t, 0, sizeof(*t) * nw);\n r->top = a->top + nw + 1;\n bn_correct_top(r);\n bn_check_top(r);\n return 1;\n}', 'int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)\n{\n int i, ret = 0;\n BIGNUM *Ri, *R;\n if (BN_is_zero(mod))\n return 0;\n BN_CTX_start(ctx);\n if ((Ri = BN_CTX_get(ctx)) == NULL)\n goto err;\n R = &(mont->RR);\n if (!BN_copy(&(mont->N), mod))\n goto err;\n if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)\n BN_set_flags(&(mont->N), BN_FLG_CONSTTIME);\n mont->N.neg = 0;\n#ifdef MONT_WORD\n {\n BIGNUM tmod;\n BN_ULONG buf[2];\n bn_init(&tmod);\n tmod.d = buf;\n tmod.dmax = 2;\n tmod.neg = 0;\n if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)\n BN_set_flags(&tmod, BN_FLG_CONSTTIME);\n mont->ri = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2;\n# if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)\n BN_zero(R);\n if (!(BN_set_bit(R, 2 * BN_BITS2)))\n goto err;\n tmod.top = 0;\n if ((buf[0] = mod->d[0]))\n tmod.top = 1;\n if ((buf[1] = mod->top > 1 ? mod->d[1] : 0))\n tmod.top = 2;\n if (BN_is_one(&tmod))\n BN_zero(Ri);\n else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, 2 * BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (bn_expand(Ri, (int)sizeof(BN_ULONG) * 2) == NULL)\n goto err;\n Ri->neg = 0;\n Ri->d[0] = BN_MASK2;\n Ri->d[1] = BN_MASK2;\n Ri->top = 2;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;\n# else\n BN_zero(R);\n if (!(BN_set_bit(R, BN_BITS2)))\n goto err;\n buf[0] = mod->d[0];\n buf[1] = 0;\n tmod.top = buf[0] != 0 ? 1 : 0;\n if (BN_is_one(&tmod))\n BN_zero(Ri);\n else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (!BN_set_word(Ri, BN_MASK2))\n goto err;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = 0;\n# endif\n }\n#else\n {\n mont->ri = BN_num_bits(&mont->N);\n BN_zero(R);\n if (!BN_set_bit(R, mont->ri))\n goto err;\n if ((BN_mod_inverse(Ri, R, &mont->N, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, mont->ri))\n goto err;\n if (!BN_sub_word(Ri, 1))\n goto err;\n if (!BN_div(&(mont->Ni), NULL, Ri, &mont->N, ctx))\n goto err;\n }\n#endif\n BN_zero(&(mont->RR));\n if (!BN_set_bit(&(mont->RR), mont->ri * 2))\n goto err;\n if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx))\n goto err;\n for (i = mont->RR.top, ret = mont->N.top; i < ret; i++)\n mont->RR.d[i] = 0;\n mont->RR.top = ret;\n mont->RR.flags |= BN_FLG_FIXED_TOP;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'BIGNUM *BN_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}']
13
0
https://github.com/openssl/openssl/blob/2864df8f9d3264e19b49a246e272fb513f4c1be3/crypto/bn/bn_ctx.c/#L270
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['static int field_tests_default(int n)\n{\n BN_CTX *ctx = NULL;\n EC_GROUP *group = NULL;\n int nid = curves[n].nid;\n int ret = 0;\n TEST_info("Testing curve %s\\n", OBJ_nid2sn(nid));\n if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))\n || !TEST_ptr(ctx = BN_CTX_new())\n || !group_field_tests(group, ctx))\n goto err;\n ret = 1;\n err:\n if (group != NULL)\n EC_GROUP_free(group);\n if (ctx != NULL)\n BN_CTX_free(ctx);\n return ret;\n}', 'static int group_field_tests(const EC_GROUP *group, BN_CTX *ctx)\n{\n BIGNUM *a = NULL, *b = NULL, *c = NULL;\n int ret = 0;\n if (group->meth->field_inv == NULL || group->meth->field_mul == NULL)\n return 1;\n BN_CTX_start(ctx);\n a = BN_CTX_get(ctx);\n b = BN_CTX_get(ctx);\n if (!TEST_ptr(c = BN_CTX_get(ctx))\n || !TEST_true(group->meth->field_inv(group, b, BN_value_one(), ctx))\n || !TEST_true(BN_is_one(b))\n || !TEST_true(BN_pseudo_rand(a, BN_num_bits(group->field) - 1,\n BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))\n || !TEST_true(group->meth->field_inv(group, b, a, ctx))\n || (group->meth->field_encode &&\n !TEST_true(group->meth->field_encode(group, a, a, ctx)))\n || (group->meth->field_encode &&\n !TEST_true(group->meth->field_encode(group, b, b, ctx)))\n || !TEST_true(group->meth->field_mul(group, c, a, b, ctx))\n || (group->meth->field_decode &&\n !TEST_true(group->meth->field_decode(group, c, c, ctx)))\n || !TEST_true(BN_is_one(c)))\n goto err;\n BN_zero(a);\n if (!TEST_false(group->meth->field_inv(group, b, a, ctx))\n || !TEST_true(ERR_GET_LIB(ERR_peek_last_error()) == ERR_LIB_EC)\n || !TEST_true(ERR_GET_REASON(ERR_peek_last_error()) ==\n EC_R_CANNOT_INVERT)\n || !TEST_false(group->meth->field_inv(group, b, group->field, ctx))\n || !TEST_true(ERR_GET_LIB(ERR_peek_last_error()) == ERR_LIB_EC)\n || !TEST_true(ERR_GET_REASON(ERR_peek_last_error()) ==\n EC_R_CANNOT_INVERT))\n goto err;\n ERR_clear_error();\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 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}']
14
0
https://github.com/nginx/nginx/blob/e5b2d3c6b2a132bbbbac0249566f0da7ff12bc39/src/http/ngx_http_core_module.c/#L2169
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 { r->args.len = 0; r->args.data = NULL; } 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->main->count++; ngx_http_handler(r); return NGX_DONE; }
['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_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 }\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 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 r->args.len = 0;\n r->args.data = NULL;\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->main->count++;\n ngx_http_handler(r);\n return NGX_DONE;\n}']
15
0
https://github.com/openssl/openssl/blob/1fac96e4d6484a517f2ebe99b72016726391723c/crypto/bn/bn_lib.c/#L447
BIGNUM *bn_expand2(BIGNUM *b, int words) { BN_ULONG *A,*a; const BN_ULONG *B; int i; bn_check_top(b); if (words > b->max) { bn_check_top(b); if (BN_get_flags(b,BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND2,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return(NULL); } a=A=(BN_ULONG *)Malloc(sizeof(BN_ULONG)*(words+1)); if (A == NULL) { BNerr(BN_F_BN_EXPAND2,ERR_R_MALLOC_FAILURE); return(NULL); } #if 1 B=b->d; if (B != NULL) { #if 0 for (i=b->top&(~7); i>0; i-=8) { A[0]=B[0]; A[1]=B[1]; A[2]=B[2]; A[3]=B[3]; A[4]=B[4]; A[5]=B[5]; A[6]=B[6]; A[7]=B[7]; A+=8; B+=8; } switch (b->top&7) { case 7: A[6]=B[6]; case 6: A[5]=B[5]; case 5: A[4]=B[4]; case 4: A[3]=B[3]; case 3: A[2]=B[2]; case 2: A[1]=B[1]; case 1: A[0]=B[0]; case 0: ; } #else for (i=b->top>>2; i>0; i--,A+=4,B+=4) { BN_ULONG a0,a1,a2,a3; a0=B[0]; a1=B[1]; a2=B[2]; a3=B[3]; A[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3; } switch (b->top&3) { case 3: A[2]=B[2]; case 2: A[1]=B[1]; case 1: A[0]=B[0]; case 0: ; } #endif Free(b->d); } b->d=a; b->max=words; A= &(b->d[b->top]); for (i=(b->max - b->top)>>3; i>0; i--,A+=8) { A[0]=0; A[1]=0; A[2]=0; A[3]=0; A[4]=0; A[5]=0; A[6]=0; A[7]=0; } for (i=(b->max - b->top)&7; i>0; i--,A++) A[0]=0; #else memset(A,0,sizeof(BN_ULONG)*(words+1)); memcpy(A,b->d,sizeof(b->d[0])*b->top); b->d=a; b->max=words; #endif } return(b); }
['static int RSA_eay_private_decrypt(int flen, unsigned char *from,\n\t unsigned char *to, RSA *rsa, int padding)\n\t{\n\tBIGNUM f,ret;\n\tint j,num=0,r= -1;\n\tunsigned char *p;\n\tunsigned char *buf=NULL;\n\tBN_CTX *ctx=NULL;\n\tBN_init(&f);\n\tBN_init(&ret);\n\tctx=BN_CTX_new();\n\tif (ctx == NULL) goto err;\n\tnum=BN_num_bytes(rsa->n);\n\tif ((buf=(unsigned char *)Malloc(num)) == NULL)\n\t\t{\n\t\tRSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,ERR_R_MALLOC_FAILURE);\n\t\tgoto err;\n\t\t}\n\tif (flen > num)\n\t\t{\n\t\tRSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN);\n\t\tgoto err;\n\t\t}\n\tif (BN_bin2bn(from,(int)flen,&f) == NULL) goto err;\n\tif ((rsa->flags & RSA_FLAG_BLINDING) && (rsa->blinding == NULL))\n\t\tRSA_blinding_on(rsa,ctx);\n\tif (rsa->flags & RSA_FLAG_BLINDING)\n\t\tif (!BN_BLINDING_convert(&f,rsa->blinding,ctx)) goto err;\n\tif (\t(rsa->p != NULL) &&\n\t\t(rsa->q != NULL) &&\n\t\t(rsa->dmp1 != NULL) &&\n\t\t(rsa->dmq1 != NULL) &&\n\t\t(rsa->iqmp != NULL))\n\t\t{ if (!rsa->meth->rsa_mod_exp(&ret,&f,rsa)) goto err; }\n\telse\n\t\t{\n\t\tif (!rsa->meth->bn_mod_exp(&ret,&f,rsa->d,rsa->n,ctx,NULL))\n\t\t\tgoto err;\n\t\t}\n\tif (rsa->flags & RSA_FLAG_BLINDING)\n\t\tif (!BN_BLINDING_invert(&ret,rsa->blinding,ctx)) goto err;\n\tp=buf;\n\tj=BN_bn2bin(&ret,p);\n\tswitch (padding)\n\t\t{\n\tcase RSA_PKCS1_PADDING:\n\t\tr=RSA_padding_check_PKCS1_type_2(to,num,buf,j,num);\n\t\tbreak;\n#ifndef NO_SHA\n case RSA_PKCS1_OAEP_PADDING:\n\t r=RSA_padding_check_PKCS1_OAEP(to,num,buf,j,num,NULL,0);\n break;\n#endif\n \tcase RSA_SSLV23_PADDING:\n\t\tr=RSA_padding_check_SSLv23(to,num,buf,j,num);\n\t\tbreak;\n\tcase RSA_NO_PADDING:\n\t\tr=RSA_padding_check_none(to,num,buf,j,num);\n\t\tbreak;\n\tdefault:\n\t\tRSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE);\n\t\tgoto err;\n\t\t}\n\tif (r < 0)\n\t\tRSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_PADDING_CHECK_FAILED);\nerr:\n\tif (ctx != NULL) BN_CTX_free(ctx);\n\tBN_clear_free(&f);\n\tBN_clear_free(&ret);\n\tif (buf != NULL)\n\t\t{\n\t\tmemset(buf,0,num);\n\t\tFree(buf);\n\t\t}\n\treturn(r);\n\t}', 'BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)\n\t{\n\tunsigned int i,m;\n\tunsigned int n;\n\tBN_ULONG l;\n\tif (ret == NULL) ret=BN_new();\n\tif (ret == NULL) return(NULL);\n\tl=0;\n\tn=len;\n\tif (n == 0)\n\t\t{\n\t\tret->top=0;\n\t\treturn(ret);\n\t\t}\n\tif (bn_expand(ret,(int)(n+2)*8) == NULL)\n\t\treturn(NULL);\n\ti=((n-1)/BN_BYTES)+1;\n\tm=((n-1)%(BN_BYTES));\n\tret->top=i;\n\twhile (n-- > 0)\n\t\t{\n\t\tl=(l<<8L)| *(s++);\n\t\tif (m-- == 0)\n\t\t\t{\n\t\t\tret->d[--i]=l;\n\t\t\tl=0;\n\t\t\tm=BN_BYTES-1;\n\t\t\t}\n\t\t}\n\tbn_fix_top(ret);\n\treturn(ret);\n\t}', 'int BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx)\n\t{\n\tbn_check_top(n);\n\tif ((b->A == NULL) || (b->Ai == NULL))\n\t\t{\n\t\tBNerr(BN_F_BN_BLINDING_CONVERT,BN_R_NOT_INITIALIZED);\n\t\treturn(0);\n\t\t}\n\treturn(BN_mod_mul(n,n,b->A,b->mod,ctx));\n\t}', 'int BN_mod_mul(BIGNUM *ret, BIGNUM *a, BIGNUM *b, BIGNUM *m, BN_CTX *ctx)\n\t{\n\tBIGNUM *t;\n\tint r=0;\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tbn_check_top(m);\n\tt= &(ctx->bn[ctx->tos++]);\n\tif (a == b)\n\t\t{ if (!BN_sqr(t,a,ctx)) goto err; }\n\telse\n\t\t{ if (!BN_mul(t,a,b,ctx)) goto err; }\n\tif (!BN_mod(ret,t,m,ctx)) goto err;\n\tr=1;\nerr:\n\tctx->tos--;\n\treturn(r);\n\t}', 'int BN_mod(BIGNUM *rem, BIGNUM *m, BIGNUM *d, BN_CTX *ctx)\n\t{\n#if 0\n\tint i,nm,nd;\n\tBIGNUM *dv;\n\tif (BN_ucmp(m,d) < 0)\n\t\treturn((BN_copy(rem,m) == NULL)?0:1);\n\tdv= &(ctx->bn[ctx->tos]);\n\tif (!BN_copy(rem,m)) return(0);\n\tnm=BN_num_bits(rem);\n\tnd=BN_num_bits(d);\n\tif (!BN_lshift(dv,d,nm-nd)) return(0);\n\tfor (i=nm-nd; i>=0; i--)\n\t\t{\n\t\tif (BN_cmp(rem,dv) >= 0)\n\t\t\t{\n\t\t\tif (!BN_sub(rem,rem,dv)) return(0);\n\t\t\t}\n\t\tif (!BN_rshift1(dv,dv)) return(0);\n\t\t}\n\treturn(1);\n#else\n\treturn(BN_div(NULL,rem,m,d,ctx));\n#endif\n\t}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, BIGNUM *num, BIGNUM *divisor,\n\t BN_CTX *ctx)\n\t{\n\tint norm_shift,i,j,loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tint num_n,div_n;\n\tbn_check_top(num);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\n\t\t}\n\tif (BN_ucmp(num,divisor) < 0)\n\t\t{\n\t\tif (rm != NULL)\n\t\t\t{ if (BN_copy(rm,num) == NULL) return(0); }\n\t\tif (dv != NULL) BN_zero(dv);\n\t\treturn(1);\n\t\t}\n\ttmp= &(ctx->bn[ctx->tos]);\n\ttmp->neg=0;\n\tsnum= &(ctx->bn[ctx->tos+1]);\n\tsdiv= &(ctx->bn[ctx->tos+2]);\n\tif (dv == NULL)\n\t\tres= &(ctx->bn[ctx->tos+3]);\n\telse\tres=dv;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tBN_lshift(sdiv,divisor,norm_shift);\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tBN_lshift(snum,num,norm_shift);\n\tsnum->neg=0;\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\tBN_init(&wnum);\n\twnum.d=\t &(snum->d[loop]);\n\twnum.top= div_n;\n\twnum.max= snum->max+1;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (BN_ucmp(&wnum,sdiv) >= 0)\n\t\t{\n\t\tif (!BN_usub(&wnum,&wnum,sdiv)) goto err;\n\t\t*resp=1;\n\t\tres->d[res->top-1]=1;\n\t\t}\n\telse\n\t\tres->top--;\n\tresp--;\n\tfor (i=0; i<loop-1; i++)\n\t\t{\n\t\tBN_ULONG q,n0,n1;\n\t\tBN_ULONG l0;\n\t\twnum.d--; wnum.top++;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\tq=bn_div_words(n0,n1,d0);\n\t\t{\n#ifdef BN_LLONG\n\t\tBN_ULLONG t1,t2,rem;\n\t\tt1=((BN_ULLONG)n0<<BN_BITS2)|n1;\n\t\tfor (;;)\n\t\t\t{\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\trem=t1-(BN_ULLONG)q*d0;\n\t\t\tif ((rem>>BN_BITS2) ||\n\t\t\t\t(t2 <= ((BN_ULLONG)(rem<<BN_BITS2)+wnump[-2])))\n\t\t\t\tbreak;\n\t\t\tq--;\n\t\t\t}\n#else\n\t\tBN_ULONG t1l,t1h,t2l,t2h,t3l,t3h,ql,qh,t3t;\n\t\tt1h=n0;\n\t\tt1l=n1;\n\t\tfor (;;)\n\t\t\t{\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n\t\t\tt3t=LBITS(d0); t3h=HBITS(d0);\n\t\t\tmul64(t3t,t3h,ql,qh);\n\t\t\tt3l=(t1l-t3t)&BN_MASK2;\n\t\t\tif (t3l > t1l) t3h++;\n\t\t\tt3h=(t1h-t3h)&BN_MASK2;\n\t\t\tif (t3h) break;\n\t\t\tif (t2h < t3l) break;\n\t\t\tif ((t2h == t3l) && (t2l <= wnump[-2])) break;\n\t\t\tq--;\n\t\t\t}\n#endif\n\t\t}\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\tfor (j=div_n+1; j>0; j--)\n\t\t\tif (tmp->d[j-1]) break;\n\t\ttmp->top=j;\n\t\tj=wnum.top;\n\t\tBN_sub(&wnum,&wnum,tmp);\n\t\tsnum->top=snum->top+wnum.top-j;\n\t\tif (wnum.neg)\n\t\t\t{\n\t\t\tq--;\n\t\t\tj=wnum.top;\n\t\t\tBN_add(&wnum,&wnum,sdiv);\n\t\t\tsnum->top+=wnum.top-j;\n\t\t\t}\n\t\t*(resp--)=q;\n\t\twnump--;\n\t\t}\n\tif (rm != NULL)\n\t\t{\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\trm->neg=num->neg;\n\t\t}\n\treturn(1);\nerr:\n\treturn(0);\n\t}', 'BIGNUM *BN_copy(BIGNUM *a, BIGNUM *b)\n\t{\n\tint i;\n\tBN_ULONG *A;\n\tconst BN_ULONG *B;\n\tbn_check_top(b);\n\tif (a == b) return(a);\n\tif (bn_wexpand(a,b->top) == NULL) return(NULL);\n#if 1\n\tA=a->d;\n\tB=b->d;\n\tfor (i=b->top>>2; i>0; i--,A+=4,B+=4)\n\t\t{\n\t\tBN_ULONG a0,a1,a2,a3;\n\t\ta0=B[0]; a1=B[1]; a2=B[2]; a3=B[3];\n\t\tA[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3;\n\t\t}\n\tswitch (b->top&3)\n\t\t{\n\t\tcase 3: A[2]=B[2];\n\t\tcase 2: A[1]=B[1];\n\t\tcase 1: A[0]=B[0];\n\t\tcase 0: ;\n\t\t}\n#else\n\tmemcpy(a->d,b->d,sizeof(b->d[0])*b->top);\n#endif\n\ta->top=b->top;\n\tif ((a->top == 0) && (a->d != NULL))\n\t\ta->d[0]=0;\n\ta->neg=b->neg;\n\treturn(a);\n\t}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n\t{\n\tBN_ULONG *A,*a;\n\tconst BN_ULONG *B;\n\tint i;\n\tbn_check_top(b);\n\tif (words > b->max)\n\t\t{\n\t\tbn_check_top(b);\n\t\tif (BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_EXPAND2,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n\t\t\treturn(NULL);\n\t\t\t}\n\t\ta=A=(BN_ULONG *)Malloc(sizeof(BN_ULONG)*(words+1));\n\t\tif (A == NULL)\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_EXPAND2,ERR_R_MALLOC_FAILURE);\n\t\t\treturn(NULL);\n\t\t\t}\n#if 1\n\t\tB=b->d;\n\t\tif (B != NULL)\n\t\t\t{\n#if 0\n\t\t\tfor (i=b->top&(~7); i>0; i-=8)\n\t\t\t\t{\n\t\t\t\tA[0]=B[0]; A[1]=B[1]; A[2]=B[2]; A[3]=B[3];\n\t\t\t\tA[4]=B[4]; A[5]=B[5]; A[6]=B[6]; A[7]=B[7];\n\t\t\t\tA+=8;\n\t\t\t\tB+=8;\n\t\t\t\t}\n\t\t\tswitch (b->top&7)\n\t\t\t\t{\n\t\t\tcase 7:\n\t\t\t\tA[6]=B[6];\n\t\t\tcase 6:\n\t\t\t\tA[5]=B[5];\n\t\t\tcase 5:\n\t\t\t\tA[4]=B[4];\n\t\t\tcase 4:\n\t\t\t\tA[3]=B[3];\n\t\t\tcase 3:\n\t\t\t\tA[2]=B[2];\n\t\t\tcase 2:\n\t\t\t\tA[1]=B[1];\n\t\t\tcase 1:\n\t\t\t\tA[0]=B[0];\n\t\t\tcase 0:\n\t\t\t\t;\n\t\t\t\t}\n#else\n\t\t\tfor (i=b->top>>2; i>0; i--,A+=4,B+=4)\n\t\t\t\t{\n\t\t\t\tBN_ULONG a0,a1,a2,a3;\n\t\t\t\ta0=B[0]; a1=B[1]; a2=B[2]; a3=B[3];\n\t\t\t\tA[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3;\n\t\t\t\t}\n\t\t\tswitch (b->top&3)\n\t\t\t\t{\n\t\t\t\tcase 3:\tA[2]=B[2];\n\t\t\t\tcase 2:\tA[1]=B[1];\n\t\t\t\tcase 1:\tA[0]=B[0];\n\t\t\t\tcase 0:\t;\n\t\t\t\t}\n#endif\n\t\t\tFree(b->d);\n\t\t\t}\n\t\tb->d=a;\n\t\tb->max=words;\n\t\tA= &(b->d[b->top]);\n\t\tfor (i=(b->max - b->top)>>3; i>0; i--,A+=8)\n\t\t\t{\n\t\t\tA[0]=0; A[1]=0; A[2]=0; A[3]=0;\n\t\t\tA[4]=0; A[5]=0; A[6]=0; A[7]=0;\n\t\t\t}\n\t\tfor (i=(b->max - b->top)&7; i>0; i--,A++)\n\t\t\tA[0]=0;\n#else\n\t\t\tmemset(A,0,sizeof(BN_ULONG)*(words+1));\n\t\t\tmemcpy(A,b->d,sizeof(b->d[0])*b->top);\n\t\t\tb->d=a;\n\t\t\tb->max=words;\n#endif\n\t\t}\n\treturn(b);\n\t}']
16
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 ec_GF2m_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,\n const EC_POINT *b, BN_CTX *ctx)\n{\n BN_CTX *new_ctx = NULL;\n BIGNUM *x0, *y0, *x1, *y1, *x2, *y2, *s, *t;\n int ret = 0;\n if (EC_POINT_is_at_infinity(group, a)) {\n if (!EC_POINT_copy(r, b))\n return 0;\n return 1;\n }\n if (EC_POINT_is_at_infinity(group, b)) {\n if (!EC_POINT_copy(r, a))\n return 0;\n return 1;\n }\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n BN_CTX_start(ctx);\n x0 = BN_CTX_get(ctx);\n y0 = BN_CTX_get(ctx);\n x1 = BN_CTX_get(ctx);\n y1 = BN_CTX_get(ctx);\n x2 = BN_CTX_get(ctx);\n y2 = BN_CTX_get(ctx);\n s = BN_CTX_get(ctx);\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (a->Z_is_one) {\n if (!BN_copy(x0, a->X))\n goto err;\n if (!BN_copy(y0, a->Y))\n goto err;\n } else {\n if (!EC_POINT_get_affine_coordinates(group, a, x0, y0, ctx))\n goto err;\n }\n if (b->Z_is_one) {\n if (!BN_copy(x1, b->X))\n goto err;\n if (!BN_copy(y1, b->Y))\n goto err;\n } else {\n if (!EC_POINT_get_affine_coordinates(group, b, x1, y1, ctx))\n goto err;\n }\n if (BN_GF2m_cmp(x0, x1)) {\n if (!BN_GF2m_add(t, x0, x1))\n goto err;\n if (!BN_GF2m_add(s, y0, y1))\n goto err;\n if (!group->meth->field_div(group, s, s, t, ctx))\n goto err;\n if (!group->meth->field_sqr(group, x2, s, ctx))\n goto err;\n if (!BN_GF2m_add(x2, x2, group->a))\n goto err;\n if (!BN_GF2m_add(x2, x2, s))\n goto err;\n if (!BN_GF2m_add(x2, x2, t))\n goto err;\n } else {\n if (BN_GF2m_cmp(y0, y1) || BN_is_zero(x1)) {\n if (!EC_POINT_set_to_infinity(group, r))\n goto err;\n ret = 1;\n goto err;\n }\n if (!group->meth->field_div(group, s, y1, x1, ctx))\n goto err;\n if (!BN_GF2m_add(s, s, x1))\n goto err;\n if (!group->meth->field_sqr(group, x2, s, ctx))\n goto err;\n if (!BN_GF2m_add(x2, x2, s))\n goto err;\n if (!BN_GF2m_add(x2, x2, group->a))\n goto err;\n }\n if (!BN_GF2m_add(y2, x1, x2))\n goto err;\n if (!group->meth->field_mul(group, y2, y2, s, ctx))\n goto err;\n if (!BN_GF2m_add(y2, y2, x2))\n goto err;\n if (!BN_GF2m_add(y2, y2, y1))\n goto err;\n if (!EC_POINT_set_affine_coordinates(group, r, x2, y2, ctx))\n goto err;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG("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}']
17
0
https://github.com/libav/libav/blob/fb0c9d41d685abb58575c5482ca33b8cd457c5ec/libavcodec/h264.c/#L218
void ff_h264_draw_horiz_band(H264Context *h, int y, int height) { AVCodecContext *avctx = h->avctx; Picture *cur = &h->cur_pic; Picture *last = h->ref_list[0][0].f.data[0] ? &h->ref_list[0][0] : NULL; const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt); int vshift = desc->log2_chroma_h; const int field_pic = h->picture_structure != PICT_FRAME; if (field_pic) { height <<= 1; y <<= 1; } height = FFMIN(height, avctx->height - y); if (field_pic && h->first_field && !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) return; if (avctx->draw_horiz_band) { AVFrame *src; int offset[AV_NUM_DATA_POINTERS]; int i; if (cur->f.pict_type == AV_PICTURE_TYPE_B || h->low_delay || (avctx->slice_flags & SLICE_FLAG_CODED_ORDER)) src = &cur->f; else if (last) src = &last->f; else return; offset[0] = y * src->linesize[0]; offset[1] = offset[2] = (y >> vshift) * src->linesize[1]; for (i = 3; i < AV_NUM_DATA_POINTERS; i++) offset[i] = 0; emms_c(); avctx->draw_horiz_band(avctx, src, offset, y, h->picture_structure, height); } }
['void ff_h264_draw_horiz_band(H264Context *h, int y, int height)\n{\n AVCodecContext *avctx = h->avctx;\n Picture *cur = &h->cur_pic;\n Picture *last = h->ref_list[0][0].f.data[0] ? &h->ref_list[0][0] : NULL;\n const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);\n int vshift = desc->log2_chroma_h;\n const int field_pic = h->picture_structure != PICT_FRAME;\n if (field_pic) {\n height <<= 1;\n y <<= 1;\n }\n height = FFMIN(height, avctx->height - y);\n if (field_pic && h->first_field && !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD))\n return;\n if (avctx->draw_horiz_band) {\n AVFrame *src;\n int offset[AV_NUM_DATA_POINTERS];\n int i;\n if (cur->f.pict_type == AV_PICTURE_TYPE_B || h->low_delay ||\n (avctx->slice_flags & SLICE_FLAG_CODED_ORDER))\n src = &cur->f;\n else if (last)\n src = &last->f;\n else\n return;\n offset[0] = y * src->linesize[0];\n offset[1] =\n offset[2] = (y >> vshift) * src->linesize[1];\n for (i = 3; i < AV_NUM_DATA_POINTERS; i++)\n offset[i] = 0;\n emms_c();\n avctx->draw_horiz_band(avctx, src, offset,\n y, h->picture_structure, height);\n }\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}']
18
0
https://github.com/openssl/openssl/blob/d40a1b865fddc3d67f8c06ff1f1466fad331c8f7/crypto/asn1/asn1_lib.c/#L390
int ASN1_STRING_set(ASN1_STRING *str, const void *_data, size_t len) { unsigned char *c; const char *data=_data; if (len < 0) { if (data == NULL) return(0); else len=strlen(data); } if ((str->length < len) || (str->data == NULL)) { c=str->data; if (c == NULL) str->data=OPENSSL_malloc(len+1); else str->data=OPENSSL_realloc(c,len+1); if (str->data == NULL) { ASN1err(ASN1_F_ASN1_STRING_SET,ERR_R_MALLOC_FAILURE); str->data=c; return(0); } } str->length=len; if (data != NULL) { memcpy(str->data,data,len); str->data[len]='\0'; } return(1); }
['static ASN1_TYPE *asn1_str2type(const char *str, int format, int utype)\n\t{\n\tASN1_TYPE *atmp = NULL;\n\tCONF_VALUE vtmp;\n\tunsigned char *rdata;\n\tlong rdlen;\n\tint no_unused = 1;\n\tif (!(atmp = ASN1_TYPE_new()))\n\t\t{\n\t\tASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);\n\t\treturn NULL;\n\t\t}\n\tif (!str)\n\t\tstr = "";\n\tswitch(utype)\n\t\t{\n\t\tcase V_ASN1_NULL:\n\t\tif (str && *str)\n\t\t\t{\n\t\t\tASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_NULL_VALUE);\n\t\t\tgoto bad_form;\n\t\t\t}\n\t\tbreak;\n\t\tcase V_ASN1_BOOLEAN:\n\t\tif (format != ASN1_GEN_FORMAT_ASCII)\n\t\t\t{\n\t\t\tASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_NOT_ASCII_FORMAT);\n\t\t\tgoto bad_form;\n\t\t\t}\n\t\tvtmp.name = NULL;\n\t\tvtmp.section = NULL;\n\t\tvtmp.value = (char *)str;\n\t\tif (!X509V3_get_value_bool(&vtmp, &atmp->value.boolean))\n\t\t\t{\n\t\t\tASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_BOOLEAN);\n\t\t\tgoto bad_str;\n\t\t\t}\n\t\tbreak;\n\t\tcase V_ASN1_INTEGER:\n\t\tcase V_ASN1_ENUMERATED:\n\t\tif (format != ASN1_GEN_FORMAT_ASCII)\n\t\t\t{\n\t\t\tASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_INTEGER_NOT_ASCII_FORMAT);\n\t\t\tgoto bad_form;\n\t\t\t}\n\t\tif (!(atmp->value.integer = s2i_ASN1_INTEGER(NULL, (char *)str)))\n\t\t\t{\n\t\t\tASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_INTEGER);\n\t\t\tgoto bad_str;\n\t\t\t}\n\t\tbreak;\n\t\tcase V_ASN1_OBJECT:\n\t\tif (format != ASN1_GEN_FORMAT_ASCII)\n\t\t\t{\n\t\t\tASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_OBJECT_NOT_ASCII_FORMAT);\n\t\t\tgoto bad_form;\n\t\t\t}\n\t\tif (!(atmp->value.object = OBJ_txt2obj(str, 0)))\n\t\t\t{\n\t\t\tASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_OBJECT);\n\t\t\tgoto bad_str;\n\t\t\t}\n\t\tbreak;\n\t\tcase V_ASN1_UTCTIME:\n\t\tcase V_ASN1_GENERALIZEDTIME:\n\t\tif (format != ASN1_GEN_FORMAT_ASCII)\n\t\t\t{\n\t\t\tASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_TIME_NOT_ASCII_FORMAT);\n\t\t\tgoto bad_form;\n\t\t\t}\n\t\tif (!(atmp->value.asn1_string = ASN1_STRING_new()))\n\t\t\t{\n\t\t\tASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);\n\t\t\tgoto bad_str;\n\t\t\t}\n\t\tif (!ASN1_STRING_set(atmp->value.asn1_string, str, -1))\n\t\t\t{\n\t\t\tASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);\n\t\t\tgoto bad_str;\n\t\t\t}\n\t\tatmp->value.asn1_string->type = utype;\n\t\tif (!ASN1_TIME_check(atmp->value.asn1_string))\n\t\t\t{\n\t\t\tASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_TIME_VALUE);\n\t\t\tgoto bad_str;\n\t\t\t}\n\t\tbreak;\n\t\tcase V_ASN1_BMPSTRING:\n\t\tcase V_ASN1_PRINTABLESTRING:\n\t\tcase V_ASN1_IA5STRING:\n\t\tcase V_ASN1_T61STRING:\n\t\tcase V_ASN1_UTF8STRING:\n\t\tcase V_ASN1_VISIBLESTRING:\n\t\tcase V_ASN1_UNIVERSALSTRING:\n\t\tcase V_ASN1_GENERALSTRING:\n\t\tcase V_ASN1_NUMERICSTRING:\n\t\tif (format == ASN1_GEN_FORMAT_ASCII)\n\t\t\tformat = MBSTRING_ASC;\n\t\telse if (format == ASN1_GEN_FORMAT_UTF8)\n\t\t\tformat = MBSTRING_UTF8;\n\t\telse\n\t\t\t{\n\t\t\tASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_FORMAT);\n\t\t\tgoto bad_form;\n\t\t\t}\n\t\tif (ASN1_mbstring_copy(&atmp->value.asn1_string, (unsigned char *)str,\n\t\t\t\t\t\t-1, format, ASN1_tag2bit(utype)) <= 0)\n\t\t\t{\n\t\t\tASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);\n\t\t\tgoto bad_str;\n\t\t\t}\n\t\tbreak;\n\t\tcase V_ASN1_BIT_STRING:\n\t\tcase V_ASN1_OCTET_STRING:\n\t\tif (!(atmp->value.asn1_string = ASN1_STRING_new()))\n\t\t\t{\n\t\t\tASN1err(ASN1_F_ASN1_STR2TYPE, ERR_R_MALLOC_FAILURE);\n\t\t\tgoto bad_form;\n\t\t\t}\n\t\tif (format == ASN1_GEN_FORMAT_HEX)\n\t\t\t{\n\t\t\tif (!(rdata = string_to_hex((char *)str, &rdlen)))\n\t\t\t\t{\n\t\t\t\tASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_HEX);\n\t\t\t\tgoto bad_str;\n\t\t\t\t}\n\t\t\tatmp->value.asn1_string->data = rdata;\n\t\t\tatmp->value.asn1_string->length = rdlen;\n\t\t\tatmp->value.asn1_string->type = utype;\n\t\t\t}\n\t\telse if (format == ASN1_GEN_FORMAT_ASCII)\n\t\t\tASN1_STRING_set(atmp->value.asn1_string, str, -1);\n\t\telse if ((format == ASN1_GEN_FORMAT_BITLIST) && (utype == V_ASN1_BIT_STRING))\n\t\t\t{\n\t\t\tif (!CONF_parse_list(str, \',\', 1, bitstr_cb, atmp->value.bit_string))\n\t\t\t\t{\n\t\t\t\tASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_LIST_ERROR);\n\t\t\t\tgoto bad_str;\n\t\t\t\t}\n\t\t\tno_unused = 0;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_ILLEGAL_BITSTRING_FORMAT);\n\t\t\tgoto bad_form;\n\t\t\t}\n\t\tif ((utype == V_ASN1_BIT_STRING) && no_unused)\n\t\t\t{\n\t\t\tatmp->value.asn1_string->flags\n\t\t\t\t&= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07);\n \t\tatmp->value.asn1_string->flags\n\t\t\t\t|= ASN1_STRING_FLAG_BITS_LEFT;\n\t\t\t}\n\t\tbreak;\n\t\tdefault:\n\t\tASN1err(ASN1_F_ASN1_STR2TYPE, ASN1_R_UNSUPPORTED_TYPE);\n\t\tgoto bad_str;\n\t\tbreak;\n\t\t}\n\tatmp->type = utype;\n\treturn atmp;\n\tbad_str:\n\tERR_add_error_data(2, "string=", str);\n\tbad_form:\n\tASN1_TYPE_free(atmp);\n\treturn NULL;\n\t}', "int ASN1_STRING_set(ASN1_STRING *str, const void *_data, size_t len)\n\t{\n\tunsigned char *c;\n\tconst char *data=_data;\n\tif (len < 0)\n\t\t{\n\t\tif (data == NULL)\n\t\t\treturn(0);\n\t\telse\n\t\t\tlen=strlen(data);\n\t\t}\n\tif ((str->length < len) || (str->data == NULL))\n\t\t{\n\t\tc=str->data;\n\t\tif (c == NULL)\n\t\t\tstr->data=OPENSSL_malloc(len+1);\n\t\telse\n\t\t\tstr->data=OPENSSL_realloc(c,len+1);\n\t\tif (str->data == NULL)\n\t\t\t{\n\t\t\tASN1err(ASN1_F_ASN1_STRING_SET,ERR_R_MALLOC_FAILURE);\n\t\t\tstr->data=c;\n\t\t\treturn(0);\n\t\t\t}\n\t\t}\n\tstr->length=len;\n\tif (data != NULL)\n\t\t{\n\t\tmemcpy(str->data,data,len);\n\t\tstr->data[len]='\\0';\n\t\t}\n\treturn(1);\n\t}"]
19
0
https://github.com/libav/libav/blob/d9cf5f516974c64e01846ca685301014b38cf224/libavformat/mpegts.c/#L678
static int read_sl_header(PESContext *pes, SLConfigDescr *sl, const uint8_t *buf, int buf_size) { GetBitContext gb; int au_start_flag = 0, au_end_flag = 0, ocr_flag = 0, idle_flag = 0; int padding_flag = 0, padding_bits = 0, inst_bitrate_flag = 0; int dts_flag = -1, cts_flag = -1; int64_t dts = AV_NOPTS_VALUE, cts = AV_NOPTS_VALUE; init_get_bits(&gb, buf, buf_size*8); if (sl->use_au_start) au_start_flag = get_bits1(&gb); if (sl->use_au_end) au_end_flag = get_bits1(&gb); if (!sl->use_au_start && !sl->use_au_end) au_start_flag = au_end_flag = 1; if (sl->ocr_len > 0) ocr_flag = get_bits1(&gb); if (sl->use_idle) idle_flag = get_bits1(&gb); if (sl->use_padding) padding_flag = get_bits1(&gb); if (padding_flag) padding_bits = get_bits(&gb, 3); if (!idle_flag && (!padding_flag || padding_bits != 0)) { if (sl->packet_seq_num_len) skip_bits_long(&gb, sl->packet_seq_num_len); if (sl->degr_prior_len) if (get_bits1(&gb)) skip_bits(&gb, sl->degr_prior_len); if (ocr_flag) skip_bits_long(&gb, sl->ocr_len); if (au_start_flag) { if (sl->use_rand_acc_pt) get_bits1(&gb); if (sl->au_seq_num_len > 0) skip_bits_long(&gb, sl->au_seq_num_len); if (sl->use_timestamps) { dts_flag = get_bits1(&gb); cts_flag = get_bits1(&gb); } } if (sl->inst_bitrate_len) inst_bitrate_flag = get_bits1(&gb); if (dts_flag == 1) dts = get_bits64(&gb, sl->timestamp_len); if (cts_flag == 1) cts = get_bits64(&gb, sl->timestamp_len); if (sl->au_len > 0) skip_bits_long(&gb, sl->au_len); if (inst_bitrate_flag) skip_bits_long(&gb, sl->inst_bitrate_len); } if (dts != AV_NOPTS_VALUE) pes->dts = dts; if (cts != AV_NOPTS_VALUE) pes->pts = cts; if (sl->timestamp_len && sl->timestamp_res) avpriv_set_pts_info(pes->st, sl->timestamp_len, 1, sl->timestamp_res); return (get_bits_count(&gb) + 7) >> 3; }
['static int read_sl_header(PESContext *pes, SLConfigDescr *sl, const uint8_t *buf, int buf_size)\n{\n GetBitContext gb;\n int au_start_flag = 0, au_end_flag = 0, ocr_flag = 0, idle_flag = 0;\n int padding_flag = 0, padding_bits = 0, inst_bitrate_flag = 0;\n int dts_flag = -1, cts_flag = -1;\n int64_t dts = AV_NOPTS_VALUE, cts = AV_NOPTS_VALUE;\n init_get_bits(&gb, buf, buf_size*8);\n if (sl->use_au_start)\n au_start_flag = get_bits1(&gb);\n if (sl->use_au_end)\n au_end_flag = get_bits1(&gb);\n if (!sl->use_au_start && !sl->use_au_end)\n au_start_flag = au_end_flag = 1;\n if (sl->ocr_len > 0)\n ocr_flag = get_bits1(&gb);\n if (sl->use_idle)\n idle_flag = get_bits1(&gb);\n if (sl->use_padding)\n padding_flag = get_bits1(&gb);\n if (padding_flag)\n padding_bits = get_bits(&gb, 3);\n if (!idle_flag && (!padding_flag || padding_bits != 0)) {\n if (sl->packet_seq_num_len)\n skip_bits_long(&gb, sl->packet_seq_num_len);\n if (sl->degr_prior_len)\n if (get_bits1(&gb))\n skip_bits(&gb, sl->degr_prior_len);\n if (ocr_flag)\n skip_bits_long(&gb, sl->ocr_len);\n if (au_start_flag) {\n if (sl->use_rand_acc_pt)\n get_bits1(&gb);\n if (sl->au_seq_num_len > 0)\n skip_bits_long(&gb, sl->au_seq_num_len);\n if (sl->use_timestamps) {\n dts_flag = get_bits1(&gb);\n cts_flag = get_bits1(&gb);\n }\n }\n if (sl->inst_bitrate_len)\n inst_bitrate_flag = get_bits1(&gb);\n if (dts_flag == 1)\n dts = get_bits64(&gb, sl->timestamp_len);\n if (cts_flag == 1)\n cts = get_bits64(&gb, sl->timestamp_len);\n if (sl->au_len > 0)\n skip_bits_long(&gb, sl->au_len);\n if (inst_bitrate_flag)\n skip_bits_long(&gb, sl->inst_bitrate_len);\n }\n if (dts != AV_NOPTS_VALUE)\n pes->dts = dts;\n if (cts != AV_NOPTS_VALUE)\n pes->pts = cts;\n if (sl->timestamp_len && sl->timestamp_res)\n avpriv_set_pts_info(pes->st, sl->timestamp_len, 1, sl->timestamp_res);\n return (get_bits_count(&gb) + 7) >> 3;\n}', 'static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,\n int bit_size)\n{\n int buffer_size;\n int ret = 0;\n if (bit_size > INT_MAX - 7 || bit_size <= 0) {\n buffer_size = bit_size = 0;\n buffer = NULL;\n ret = AVERROR_INVALIDDATA;\n }\n buffer_size = (bit_size + 7) >> 3;\n s->buffer = buffer;\n s->size_in_bits = bit_size;\n#if !UNCHECKED_BITSTREAM_READER\n s->size_in_bits_plus8 = bit_size + 8;\n#endif\n s->buffer_end = buffer + buffer_size;\n s->index = 0;\n return ret;\n}', 'static inline unsigned int get_bits1(GetBitContext *s)\n{\n unsigned int index = s->index;\n uint8_t result = s->buffer[index>>3];\n#ifdef BITSTREAM_READER_LE\n result >>= index & 7;\n result &= 1;\n#else\n result <<= index & 7;\n result >>= 8 - 1;\n#endif\n#if !UNCHECKED_BITSTREAM_READER\n if (s->index < s->size_in_bits_plus8)\n#endif\n index++;\n s->index = index;\n return result;\n}']
20
0
https://github.com/openssl/openssl/blob/2864df8f9d3264e19b49a246e272fb513f4c1be3/crypto/bn/bn_ctx.c/#L342
static void BN_POOL_release(BN_POOL *p, unsigned int num) { unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE; p->used -= num; while (num--) { bn_check_top(p->current->vals + offset); if (offset == 0) { offset = BN_CTX_POOL_SIZE - 1; p->current = p->current->prev; } else offset--; } }
['int BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)\n{\n BIGNUM *b = NULL;\n int ret = 0;\n BN_CTX_start(ctx);\n if ((b = BN_CTX_get(ctx)) == NULL)\n goto err;\n do {\n if (!BN_priv_rand(b, BN_num_bits(p) - 1,\n BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))\n goto err;\n } while (BN_is_zero(b));\n if (!BN_GF2m_mod_mul(r, a, b, p, ctx))\n goto err;\n if (!BN_GF2m_mod_inv_vartime(r, r, p, ctx))\n goto err;\n if (!BN_GF2m_mod_mul(r, r, b, p, ctx))\n goto err;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_GF2m_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n const BIGNUM *p, BN_CTX *ctx)\n{\n int ret = 0;\n const int max = BN_num_bits(p) + 1;\n int *arr = NULL;\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(p);\n if ((arr = OPENSSL_malloc(sizeof(*arr) * max)) == NULL)\n goto err;\n ret = BN_GF2m_poly2arr(p, arr, max);\n if (!ret || ret > max) {\n BNerr(BN_F_BN_GF2M_MOD_MUL, BN_R_INVALID_LENGTH);\n goto err;\n }\n ret = BN_GF2m_mod_mul_arr(r, a, b, arr, ctx);\n bn_check_top(r);\n err:\n OPENSSL_free(arr);\n return ret;\n}', 'static int BN_GF2m_mod_inv_vartime(BIGNUM *r, const BIGNUM *a,\n const BIGNUM *p, BN_CTX *ctx)\n{\n BIGNUM *b, *c = NULL, *u = NULL, *v = NULL, *tmp;\n int ret = 0;\n bn_check_top(a);\n bn_check_top(p);\n BN_CTX_start(ctx);\n b = BN_CTX_get(ctx);\n c = BN_CTX_get(ctx);\n u = BN_CTX_get(ctx);\n v = BN_CTX_get(ctx);\n if (v == NULL)\n goto err;\n if (!BN_GF2m_mod(u, a, p))\n goto err;\n if (BN_is_zero(u))\n goto err;\n if (!BN_copy(v, p))\n goto err;\n# if 0\n if (!BN_one(b))\n goto err;\n while (1) {\n while (!BN_is_odd(u)) {\n if (BN_is_zero(u))\n goto err;\n if (!BN_rshift1(u, u))\n goto err;\n if (BN_is_odd(b)) {\n if (!BN_GF2m_add(b, b, p))\n goto err;\n }\n if (!BN_rshift1(b, b))\n goto err;\n }\n if (BN_abs_is_word(u, 1))\n break;\n if (BN_num_bits(u) < BN_num_bits(v)) {\n tmp = u;\n u = v;\n v = tmp;\n tmp = b;\n b = c;\n c = tmp;\n }\n if (!BN_GF2m_add(u, u, v))\n goto err;\n if (!BN_GF2m_add(b, b, c))\n goto err;\n }\n# else\n {\n int i;\n int ubits = BN_num_bits(u);\n int vbits = BN_num_bits(v);\n int top = p->top;\n BN_ULONG *udp, *bdp, *vdp, *cdp;\n if (!bn_wexpand(u, top))\n goto err;\n udp = u->d;\n for (i = u->top; i < top; i++)\n udp[i] = 0;\n u->top = top;\n if (!bn_wexpand(b, top))\n goto err;\n bdp = b->d;\n bdp[0] = 1;\n for (i = 1; i < top; i++)\n bdp[i] = 0;\n b->top = top;\n if (!bn_wexpand(c, top))\n goto err;\n cdp = c->d;\n for (i = 0; i < top; i++)\n cdp[i] = 0;\n c->top = top;\n vdp = v->d;\n while (1) {\n while (ubits && !(udp[0] & 1)) {\n BN_ULONG u0, u1, b0, b1, mask;\n u0 = udp[0];\n b0 = bdp[0];\n mask = (BN_ULONG)0 - (b0 & 1);\n b0 ^= p->d[0] & mask;\n for (i = 0; i < top - 1; i++) {\n u1 = udp[i + 1];\n udp[i] = ((u0 >> 1) | (u1 << (BN_BITS2 - 1))) & BN_MASK2;\n u0 = u1;\n b1 = bdp[i + 1] ^ (p->d[i + 1] & mask);\n bdp[i] = ((b0 >> 1) | (b1 << (BN_BITS2 - 1))) & BN_MASK2;\n b0 = b1;\n }\n udp[i] = u0 >> 1;\n bdp[i] = b0 >> 1;\n ubits--;\n }\n if (ubits <= BN_BITS2) {\n if (udp[0] == 0)\n goto err;\n if (udp[0] == 1)\n break;\n }\n if (ubits < vbits) {\n i = ubits;\n ubits = vbits;\n vbits = i;\n tmp = u;\n u = v;\n v = tmp;\n tmp = b;\n b = c;\n c = tmp;\n udp = vdp;\n vdp = v->d;\n bdp = cdp;\n cdp = c->d;\n }\n for (i = 0; i < top; i++) {\n udp[i] ^= vdp[i];\n bdp[i] ^= cdp[i];\n }\n if (ubits == vbits) {\n BN_ULONG ul;\n int utop = (ubits - 1) / BN_BITS2;\n while ((ul = udp[utop]) == 0 && utop)\n utop--;\n ubits = utop * BN_BITS2 + BN_num_bits_word(ul);\n }\n }\n bn_correct_top(b);\n }\n# endif\n if (!BN_copy(r, b))\n goto err;\n bn_check_top(r);\n ret = 1;\n err:\n# ifdef BN_DEBUG\n bn_correct_top(c);\n bn_correct_top(u);\n bn_correct_top(v);\n# endif\n BN_CTX_end(ctx);\n return ret;\n}', '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}', '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 void BN_POOL_release(BN_POOL *p, unsigned int num)\n{\n unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE;\n p->used -= num;\n while (num--) {\n bn_check_top(p->current->vals + offset);\n if (offset == 0) {\n offset = BN_CTX_POOL_SIZE - 1;\n p->current = p->current->prev;\n } else\n offset--;\n }\n}']
21
0
https://github.com/openssl/openssl/blob/924e5eda2c82d737cc5a1b9c37918aa6e34825da/apps/speed.c/#L2819
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) { if (pipe(fd) == -1) { fprintf(stderr, "pipe failure\n"); exit(1); } fflush(stdout); fflush(stderr); if(fork()) { close(fd[1]); fds[n]=fd[0]; } else { close(fd[0]); close(1); if (dup(fd[1]) == -1) { fprintf(stderr, "dup failed\n"); exit(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; } #ifndef OPENSSL_NO_DSA 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; } #endif #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\tif (pipe(fd) == -1)\n\t\t\t{\n\t\t\tfprintf(stderr, "pipe failure\\n");\n\t\t\texit(1);\n\t\t\t}\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\tif (dup(fd[1]) == -1)\n\t\t\t\t{\n\t\t\t\tfprintf(stderr, "dup failed\\n");\n\t\t\t\texit(1);\n\t\t\t\t}\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#ifndef OPENSSL_NO_DSA\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#endif\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}']
22
0
https://github.com/openssl/openssl/blob/1586365835e8eb950e804a4f1e62cff9563061bb/crypto/x509v3/v3_purp.c/#L121
int X509_check_purpose(X509 *x, int id, int ca) { int idx; const X509_PURPOSE *pt; if(!(x->ex_flags & EXFLAG_SET)) { CRYPTO_w_lock(CRYPTO_LOCK_X509); x509v3_cache_extensions(x); CRYPTO_w_unlock(CRYPTO_LOCK_X509); } if(id == -1) return 1; idx = X509_PURPOSE_get_by_id(id); if(idx == -1) return -1; pt = X509_PURPOSE_get0(idx); return pt->check_purpose(pt, x, ca); }
['int X509_check_purpose(X509 *x, int id, int ca)\n{\n\tint idx;\n\tconst X509_PURPOSE *pt;\n\tif(!(x->ex_flags & EXFLAG_SET)) {\n\t\tCRYPTO_w_lock(CRYPTO_LOCK_X509);\n\t\tx509v3_cache_extensions(x);\n\t\tCRYPTO_w_unlock(CRYPTO_LOCK_X509);\n\t}\n\tif(id == -1) return 1;\n\tidx = X509_PURPOSE_get_by_id(id);\n\tif(idx == -1) return -1;\n\tpt = X509_PURPOSE_get0(idx);\n\treturn pt->check_purpose(pt, x, ca);\n}', 'void CRYPTO_lock(int mode, int type, const char *file, int line)\n\t{\n#ifdef LOCK_DEBUG\n\t\t{\n\t\tchar *rw_text,*operation_text;\n\t\tif (mode & CRYPTO_LOCK)\n\t\t\toperation_text="lock ";\n\t\telse if (mode & CRYPTO_UNLOCK)\n\t\t\toperation_text="unlock";\n\t\telse\n\t\t\toperation_text="ERROR ";\n\t\tif (mode & CRYPTO_READ)\n\t\t\trw_text="r";\n\t\telse if (mode & CRYPTO_WRITE)\n\t\t\trw_text="w";\n\t\telse\n\t\t\trw_text="ERROR";\n\t\tfprintf(stderr,"lock:%08lx:(%s)%s %-18s %s:%d\\n",\n\t\t\tCRYPTO_thread_id(), rw_text, operation_text,\n\t\t\tCRYPTO_get_lock_name(type), file, line);\n\t\t}\n#endif\n\tif (type < 0)\n\t\t{\n\t\tstruct CRYPTO_dynlock_value *pointer\n\t\t\t= CRYPTO_get_dynlock_value(type);\n\t\tif (pointer && dynlock_lock_callback)\n\t\t\t{\n\t\t\tdynlock_lock_callback(mode, pointer, file, line);\n\t\t\t}\n\t\tCRYPTO_destroy_dynlockid(type);\n\t\t}\n\telse\n\t\tif (locking_callback != NULL)\n\t\t\tlocking_callback(mode,type,file,line);\n\t}', 'static void x509v3_cache_extensions(X509 *x)\n{\n\tBASIC_CONSTRAINTS *bs;\n\tASN1_BIT_STRING *usage;\n\tASN1_BIT_STRING *ns;\n\tEXTENDED_KEY_USAGE *extusage;\n\tX509_EXTENSION *ex;\n\tint i;\n\tif(x->ex_flags & EXFLAG_SET) return;\n#ifndef OPENSSL_NO_SHA\n\tX509_digest(x, EVP_sha1(), x->sha1_hash, NULL);\n#endif\n\tif(!X509_NAME_cmp(X509_get_subject_name(x), X509_get_issuer_name(x)))\n\t\t\t x->ex_flags |= EXFLAG_SS;\n\tif(!X509_get_version(x)) x->ex_flags |= EXFLAG_V1;\n\tif((bs=X509_get_ext_d2i(x, NID_basic_constraints, NULL, NULL))) {\n\t\tif(bs->ca) x->ex_flags |= EXFLAG_CA;\n\t\tif(bs->pathlen) {\n\t\t\tif((bs->pathlen->type == V_ASN1_NEG_INTEGER)\n\t\t\t\t\t\t|| !bs->ca) {\n\t\t\t\tx->ex_flags |= EXFLAG_INVALID;\n\t\t\t\tx->ex_pathlen = 0;\n\t\t\t} else x->ex_pathlen = ASN1_INTEGER_get(bs->pathlen);\n\t\t} else x->ex_pathlen = -1;\n\t\tBASIC_CONSTRAINTS_free(bs);\n\t\tx->ex_flags |= EXFLAG_BCONS;\n\t}\n\tif((usage=X509_get_ext_d2i(x, NID_key_usage, NULL, NULL))) {\n\t\tif(usage->length > 0) {\n\t\t\tx->ex_kusage = usage->data[0];\n\t\t\tif(usage->length > 1)\n\t\t\t\tx->ex_kusage |= usage->data[1] << 8;\n\t\t} else x->ex_kusage = 0;\n\t\tx->ex_flags |= EXFLAG_KUSAGE;\n\t\tASN1_BIT_STRING_free(usage);\n\t}\n\tx->ex_xkusage = 0;\n\tif((extusage=X509_get_ext_d2i(x, NID_ext_key_usage, NULL, NULL))) {\n\t\tx->ex_flags |= EXFLAG_XKUSAGE;\n\t\tfor(i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) {\n\t\t\tswitch(OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage,i))) {\n\t\t\t\tcase NID_server_auth:\n\t\t\t\tx->ex_xkusage |= XKU_SSL_SERVER;\n\t\t\t\tbreak;\n\t\t\t\tcase NID_client_auth:\n\t\t\t\tx->ex_xkusage |= XKU_SSL_CLIENT;\n\t\t\t\tbreak;\n\t\t\t\tcase NID_email_protect:\n\t\t\t\tx->ex_xkusage |= XKU_SMIME;\n\t\t\t\tbreak;\n\t\t\t\tcase NID_code_sign:\n\t\t\t\tx->ex_xkusage |= XKU_CODE_SIGN;\n\t\t\t\tbreak;\n\t\t\t\tcase NID_ms_sgc:\n\t\t\t\tcase NID_ns_sgc:\n\t\t\t\tx->ex_xkusage |= XKU_SGC;\n\t\t\t\tbreak;\n\t\t\t\tcase NID_OCSP_sign:\n\t\t\t\tx->ex_xkusage |= XKU_OCSP_SIGN;\n\t\t\t\tbreak;\n\t\t\t\tcase NID_time_stamp:\n\t\t\t\tx->ex_xkusage |= XKU_TIMESTAMP;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tsk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free);\n\t}\n\tif((ns=X509_get_ext_d2i(x, NID_netscape_cert_type, NULL, NULL))) {\n\t\tif(ns->length > 0) x->ex_nscert = ns->data[0];\n\t\telse x->ex_nscert = 0;\n\t\tx->ex_flags |= EXFLAG_NSCERT;\n\t\tASN1_BIT_STRING_free(ns);\n\t}\n\tx->skid =X509_get_ext_d2i(x, NID_subject_key_identifier, NULL, NULL);\n\tx->akid =X509_get_ext_d2i(x, NID_authority_key_identifier, NULL, NULL);\n\tfor (i = 0; i < X509_get_ext_count(x); i++)\n\t\t{\n\t\tex = X509_get_ext(x, i);\n\t\tif (!X509_EXTENSION_get_critical(ex))\n\t\t\tcontinue;\n\t\tif (!X509_supported_extension(ex))\n\t\t\t{\n\t\t\tx->ex_flags |= EXFLAG_CRITICAL;\n\t\t\tbreak;\n\t\t\t}\n\t\t}\n\tx->ex_flags |= EXFLAG_SET;\n}', 'int X509_PURPOSE_get_by_id(int purpose)\n{\n\tX509_PURPOSE tmp;\n\tint idx;\n\tif((purpose >= X509_PURPOSE_MIN) && (purpose <= X509_PURPOSE_MAX))\n\t\treturn purpose - X509_PURPOSE_MIN;\n\ttmp.purpose = purpose;\n\tif(!xptable) return -1;\n\tidx = sk_X509_PURPOSE_find(xptable, &tmp);\n\tif(idx == -1) return -1;\n\treturn idx + X509_PURPOSE_COUNT;\n}', 'X509_PURPOSE * X509_PURPOSE_get0(int idx)\n{\n\tif(idx < 0) return NULL;\n\tif(idx < X509_PURPOSE_COUNT) return xstandard + idx;\n\treturn sk_X509_PURPOSE_value(xptable, idx - X509_PURPOSE_COUNT);\n}']
23
0
https://github.com/openssl/openssl/blob/1a50eedf2a1fbb1e0e009ad616d8be678e4c6340/crypto/bn/bn_ctx.c/#L276
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['static int ecp_nistz256_points_mul(const EC_GROUP *group,\n EC_POINT *r,\n const BIGNUM *scalar,\n size_t num,\n const EC_POINT *points[],\n const BIGNUM *scalars[], BN_CTX *ctx)\n{\n int i = 0, ret = 0, no_precomp_for_generator = 0, p_is_infinity = 0;\n unsigned char p_str[33] = { 0 };\n const PRECOMP256_ROW *preComputedTable = NULL;\n const NISTZ256_PRE_COMP *pre_comp = NULL;\n const EC_POINT *generator = NULL;\n const BIGNUM **new_scalars = NULL;\n const EC_POINT **new_points = NULL;\n unsigned int idx = 0;\n const unsigned int window_size = 7;\n const unsigned int mask = (1 << (window_size + 1)) - 1;\n unsigned int wvalue;\n ALIGN32 union {\n P256_POINT p;\n P256_POINT_AFFINE a;\n } t, p;\n BIGNUM *tmp_scalar;\n if ((num + 1) == 0 || (num + 1) > OPENSSL_MALLOC_MAX_NELEMS(void *)) {\n ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n BN_CTX_start(ctx);\n if (scalar) {\n generator = EC_GROUP_get0_generator(group);\n if (generator == NULL) {\n ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, EC_R_UNDEFINED_GENERATOR);\n goto err;\n }\n pre_comp = group->pre_comp.nistz256;\n if (pre_comp) {\n EC_POINT *pre_comp_generator = EC_POINT_new(group);\n if (pre_comp_generator == NULL)\n goto err;\n if (!ecp_nistz256_set_from_affine(pre_comp_generator,\n group, pre_comp->precomp[0],\n ctx)) {\n EC_POINT_free(pre_comp_generator);\n goto err;\n }\n if (0 == EC_POINT_cmp(group, generator, pre_comp_generator, ctx))\n preComputedTable = (const PRECOMP256_ROW *)pre_comp->precomp;\n EC_POINT_free(pre_comp_generator);\n }\n if (preComputedTable == NULL && ecp_nistz256_is_affine_G(generator)) {\n preComputedTable = ecp_nistz256_precomputed;\n }\n if (preComputedTable) {\n if ((BN_num_bits(scalar) > 256)\n || BN_is_negative(scalar)) {\n if ((tmp_scalar = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (!BN_nnmod(tmp_scalar, scalar, group->order, ctx)) {\n ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, ERR_R_BN_LIB);\n goto err;\n }\n scalar = tmp_scalar;\n }\n for (i = 0; i < bn_get_top(scalar) * BN_BYTES; i += BN_BYTES) {\n BN_ULONG d = bn_get_words(scalar)[i / BN_BYTES];\n p_str[i + 0] = (unsigned char)d;\n p_str[i + 1] = (unsigned char)(d >> 8);\n p_str[i + 2] = (unsigned char)(d >> 16);\n p_str[i + 3] = (unsigned char)(d >>= 24);\n if (BN_BYTES == 8) {\n d >>= 8;\n p_str[i + 4] = (unsigned char)d;\n p_str[i + 5] = (unsigned char)(d >> 8);\n p_str[i + 6] = (unsigned char)(d >> 16);\n p_str[i + 7] = (unsigned char)(d >> 24);\n }\n }\n for (; i < 33; i++)\n p_str[i] = 0;\n#if defined(ECP_NISTZ256_AVX2)\n if (ecp_nistz_avx2_eligible()) {\n ecp_nistz256_avx2_mul_g(&p.p, p_str, preComputedTable);\n } else\n#endif\n {\n BN_ULONG infty;\n wvalue = (p_str[0] << 1) & mask;\n idx += window_size;\n wvalue = _booth_recode_w7(wvalue);\n ecp_nistz256_gather_w7(&p.a, preComputedTable[0],\n wvalue >> 1);\n ecp_nistz256_neg(p.p.Z, p.p.Y);\n copy_conditional(p.p.Y, p.p.Z, wvalue & 1);\n infty = (p.p.X[0] | p.p.X[1] | p.p.X[2] | p.p.X[3] |\n p.p.Y[0] | p.p.Y[1] | p.p.Y[2] | p.p.Y[3]);\n if (P256_LIMBS == 8)\n infty |= (p.p.X[4] | p.p.X[5] | p.p.X[6] | p.p.X[7] |\n p.p.Y[4] | p.p.Y[5] | p.p.Y[6] | p.p.Y[7]);\n infty = 0 - is_zero(infty);\n infty = ~infty;\n p.p.Z[0] = ONE[0] & infty;\n p.p.Z[1] = ONE[1] & infty;\n p.p.Z[2] = ONE[2] & infty;\n p.p.Z[3] = ONE[3] & infty;\n if (P256_LIMBS == 8) {\n p.p.Z[4] = ONE[4] & infty;\n p.p.Z[5] = ONE[5] & infty;\n p.p.Z[6] = ONE[6] & infty;\n p.p.Z[7] = ONE[7] & infty;\n }\n for (i = 1; i < 37; i++) {\n unsigned int off = (idx - 1) / 8;\n wvalue = p_str[off] | p_str[off + 1] << 8;\n wvalue = (wvalue >> ((idx - 1) % 8)) & mask;\n idx += window_size;\n wvalue = _booth_recode_w7(wvalue);\n ecp_nistz256_gather_w7(&t.a,\n preComputedTable[i], wvalue >> 1);\n ecp_nistz256_neg(t.p.Z, t.a.Y);\n copy_conditional(t.a.Y, t.p.Z, wvalue & 1);\n ecp_nistz256_point_add_affine(&p.p, &p.p, &t.a);\n }\n }\n } else {\n p_is_infinity = 1;\n no_precomp_for_generator = 1;\n }\n } else\n p_is_infinity = 1;\n if (no_precomp_for_generator) {\n new_scalars = OPENSSL_malloc((num + 1) * sizeof(BIGNUM *));\n if (new_scalars == NULL) {\n ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n new_points = OPENSSL_malloc((num + 1) * sizeof(EC_POINT *));\n if (new_points == NULL) {\n ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n memcpy(new_scalars, scalars, num * sizeof(BIGNUM *));\n new_scalars[num] = scalar;\n memcpy(new_points, points, num * sizeof(EC_POINT *));\n new_points[num] = generator;\n scalars = new_scalars;\n points = new_points;\n num++;\n }\n if (num) {\n P256_POINT *out = &t.p;\n if (p_is_infinity)\n out = &p.p;\n if (!ecp_nistz256_windowed_mul(group, out, scalars, points, num, ctx))\n goto err;\n if (!p_is_infinity)\n ecp_nistz256_point_add(&p.p, &p.p, out);\n }\n if (!bn_set_words(r->X, p.p.X, P256_LIMBS) ||\n !bn_set_words(r->Y, p.p.Y, P256_LIMBS) ||\n !bn_set_words(r->Z, p.p.Z, P256_LIMBS)) {\n goto err;\n }\n r->Z_is_one = is_one(r->Z) & 1;\n ret = 1;\nerr:\n if (ctx)\n BN_CTX_end(ctx);\n OPENSSL_free(new_points);\n OPENSSL_free(new_scalars);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_start", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG_EXIT(ctx);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
24
0
https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/h264pred.c/#L357
static void pred4x4_horizontal_up_rv40_c(uint8_t *src, uint8_t *topright, int stride){ LOAD_LEFT_EDGE LOAD_DOWN_LEFT_EDGE LOAD_TOP_EDGE LOAD_TOP_RIGHT_EDGE src[0+0*stride]=(t1 + 2*t2 + t3 + 2*l0 + 2*l1 + 4)>>3; src[1+0*stride]=(t2 + 2*t3 + t4 + l0 + 2*l1 + l2 + 4)>>3; src[2+0*stride]= src[0+1*stride]=(t3 + 2*t4 + t5 + 2*l1 + 2*l2 + 4)>>3; src[3+0*stride]= src[1+1*stride]=(t4 + 2*t5 + t6 + l1 + 2*l2 + l3 + 4)>>3; src[2+1*stride]= src[0+2*stride]=(t5 + 2*t6 + t7 + 2*l2 + 2*l3 + 4)>>3; src[3+1*stride]= src[1+2*stride]=(t6 + 3*t7 + l2 + 3*l3 + 4)>>3; src[3+2*stride]= src[1+3*stride]=(l3 + 2*l4 + l5 + 2)>>2; src[0+3*stride]= src[2+2*stride]=(t6 + t7 + l3 + l4 + 2)>>2; src[2+3*stride]=(l4 + l5 + 1)>>1; src[3+3*stride]=(l4 + 2*l5 + l6 + 2)>>2; }
['static void pred4x4_horizontal_up_rv40_c(uint8_t *src, uint8_t *topright, int stride){\n LOAD_LEFT_EDGE\n LOAD_DOWN_LEFT_EDGE\n LOAD_TOP_EDGE\n LOAD_TOP_RIGHT_EDGE\n src[0+0*stride]=(t1 + 2*t2 + t3 + 2*l0 + 2*l1 + 4)>>3;\n src[1+0*stride]=(t2 + 2*t3 + t4 + l0 + 2*l1 + l2 + 4)>>3;\n src[2+0*stride]=\n src[0+1*stride]=(t3 + 2*t4 + t5 + 2*l1 + 2*l2 + 4)>>3;\n src[3+0*stride]=\n src[1+1*stride]=(t4 + 2*t5 + t6 + l1 + 2*l2 + l3 + 4)>>3;\n src[2+1*stride]=\n src[0+2*stride]=(t5 + 2*t6 + t7 + 2*l2 + 2*l3 + 4)>>3;\n src[3+1*stride]=\n src[1+2*stride]=(t6 + 3*t7 + l2 + 3*l3 + 4)>>3;\n src[3+2*stride]=\n src[1+3*stride]=(l3 + 2*l4 + l5 + 2)>>2;\n src[0+3*stride]=\n src[2+2*stride]=(t6 + t7 + l3 + l4 + 2)>>2;\n src[2+3*stride]=(l4 + l5 + 1)>>1;\n src[3+3*stride]=(l4 + 2*l5 + l6 + 2)>>2;\n}']
25
0
https://github.com/openssl/openssl/blob/5d1c09de1f2736e1d4b1877206d08455ec75f558/crypto/bn/bn_sqr.c/#L114
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); }
['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_rshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, j, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l, tmp;\n bn_check_top(r);\n bn_check_top(a);\n if (n < 0) {\n BNerr(BN_F_BN_RSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n nw = n / BN_BITS2;\n rb = n % BN_BITS2;\n lb = BN_BITS2 - rb;\n if (nw >= a->top || a->top == 0) {\n BN_zero(r);\n return 1;\n }\n i = (BN_num_bits(a) - n + (BN_BITS2 - 1)) / BN_BITS2;\n if (r != a) {\n if (bn_wexpand(r, i) == NULL)\n return 0;\n r->neg = a->neg;\n } else {\n if (n == 0)\n return 1;\n }\n f = &(a->d[nw]);\n t = r->d;\n j = a->top - nw;\n r->top = i;\n if (rb == 0) {\n for (i = j; i != 0; i--)\n *(t++) = *(f++);\n } else {\n l = *(f++);\n for (i = j - 1; i != 0; i--) {\n tmp = (l >> rb) & BN_MASK2;\n l = *(f++);\n *(t++) = (tmp | (l << lb)) & BN_MASK2;\n }\n if ((l = (l >> rb) & BN_MASK2))\n *(t) = l;\n }\n if (!r->top)\n r->neg = 0;\n bn_check_top(r);\n return 1;\n}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n BN_CTX *ctx)\n{\n int ret;\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n if (BN_is_odd(m)) {\n# ifdef MONT_EXP_WORD\n if (a->top == 1 && !a->neg\n && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(a, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(m, BN_FLG_CONSTTIME) == 0)) {\n BN_ULONG A = a->d[0];\n ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);\n } else\n# endif\n ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);\n } else\n#endif\n#ifdef RECP_MUL_MOD\n {\n ret = BN_mod_exp_recp(r, a, p, m, ctx);\n }\n#else\n {\n ret = BN_mod_exp_simple(r, a, p, m, ctx);\n }\n#endif\n bn_check_top(r);\n return ret;\n}', 'int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n int start = 1;\n BIGNUM *d, *r;\n const BIGNUM *aa;\n BIGNUM *val[TABLE_SIZE];\n BN_MONT_CTX *mont = NULL;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(a, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont);\n }\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_abs_is_word(m, 1)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n d = BN_CTX_get(ctx);\n r = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (val[0] == NULL)\n goto err;\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n if (a->neg || BN_ucmp(a, m) >= 0) {\n if (!BN_nnmod(val[0], a, m, ctx))\n goto err;\n aa = val[0];\n } else\n aa = a;\n if (BN_is_zero(aa)) {\n BN_zero(rr);\n ret = 1;\n goto err;\n }\n if (!bn_to_mont_fixed_top(val[0], aa, mont, ctx))\n goto err;\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!bn_mul_mont_fixed_top(d, val[0], val[0], mont, ctx))\n goto err;\n j = 1 << (window - 1);\n for (i = 1; i < j; i++) {\n if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n !bn_mul_mont_fixed_top(val[i], val[i - 1], d, mont, ctx))\n goto err;\n }\n }\n start = 1;\n wvalue = 0;\n wstart = bits - 1;\n wend = 0;\n#if 1\n j = m->top;\n if (m->d[j - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n if (bn_wexpand(r, j) == NULL)\n goto err;\n r->d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < j; i++)\n r->d[i] = (~m->d[i]) & BN_MASK2;\n r->top = j;\n r->flags |= BN_FLG_FIXED_TOP;\n } else\n#endif\n if (!bn_to_mont_fixed_top(r, BN_value_one(), mont, ctx))\n goto err;\n for (;;) {\n if (BN_is_bit_set(p, wstart) == 0) {\n if (!start) {\n if (!bn_mul_mont_fixed_top(r, r, r, mont, ctx))\n goto err;\n }\n if (wstart == 0)\n break;\n wstart--;\n continue;\n }\n j = wstart;\n wvalue = 1;\n wend = 0;\n for (i = 1; i < window; i++) {\n if (wstart - i < 0)\n break;\n if (BN_is_bit_set(p, wstart - i)) {\n wvalue <<= (i - wend);\n wvalue |= 1;\n wend = i;\n }\n }\n j = wend + 1;\n if (!start)\n for (i = 0; i < j; i++) {\n if (!bn_mul_mont_fixed_top(r, r, r, mont, ctx))\n goto err;\n }\n if (!bn_mul_mont_fixed_top(r, r, val[wvalue >> 1], mont, ctx))\n goto err;\n wstart -= wend + 1;\n wvalue = 0;\n start = 0;\n if (wstart < 0)\n break;\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n j = mont->N.top;\n val[0]->d[0] = 1;\n for (i = 1; i < j; i++)\n val[0]->d[i] = 0;\n val[0]->top = j;\n if (!BN_mod_mul_montgomery(rr, r, val[0], mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, r, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n BN_CTX_end(ctx);\n bn_check_top(rr);\n return ret;\n}', 'int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx,\n BN_MONT_CTX *in_mont)\n{\n int i, bits, ret = 0, window, wvalue, wmask, window0;\n int top;\n BN_MONT_CTX *mont = NULL;\n int numPowers;\n unsigned char *powerbufFree = NULL;\n int powerbufLen = 0;\n unsigned char *powerbuf = NULL;\n BIGNUM tmp, am;\n#if defined(SPARC_T4_MONT)\n unsigned int t4 = 0;\n#endif\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT_CONSTTIME, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n top = m->top;\n bits = p->top * BN_BITS2;\n if (bits == 0) {\n if (BN_abs_is_word(m, 1)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n#ifdef RSAZ_ENABLED\n if (!a->neg) {\n if ((16 == a->top) && (16 == p->top) && (BN_num_bits(m) == 1024)\n && rsaz_avx2_eligible()) {\n if (NULL == bn_wexpand(rr, 16))\n goto err;\n RSAZ_1024_mod_exp_avx2(rr->d, a->d, p->d, m->d, mont->RR.d,\n mont->n0[0]);\n rr->top = 16;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n } else if ((8 == a->top) && (8 == p->top) && (BN_num_bits(m) == 512)) {\n if (NULL == bn_wexpand(rr, 8))\n goto err;\n RSAZ_512_mod_exp(rr->d, a->d, p->d, m->d, mont->n0[0], mont->RR.d);\n rr->top = 8;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n }\n }\n#endif\n window = BN_window_bits_for_ctime_exponent_size(bits);\n#if defined(SPARC_T4_MONT)\n if (window >= 5 && (top & 15) == 0 && top <= 64 &&\n (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) ==\n (CFR_MONTMUL | CFR_MONTSQR) && (t4 = OPENSSL_sparcv9cap_P[0]))\n window = 5;\n else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window >= 5) {\n window = 5;\n powerbufLen += top * sizeof(mont->N.d[0]);\n }\n#endif\n (void)0;\n numPowers = 1 << window;\n powerbufLen += sizeof(m->d[0]) * (top * numPowers +\n ((2 * top) >\n numPowers ? (2 * top) : numPowers));\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree =\n alloca(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH);\n else\n#endif\n if ((powerbufFree =\n OPENSSL_malloc(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH))\n == NULL)\n goto err;\n powerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);\n memset(powerbuf, 0, powerbufLen);\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree = NULL;\n#endif\n tmp.d = (BN_ULONG *)(powerbuf + sizeof(m->d[0]) * top * numPowers);\n am.d = tmp.d + top;\n tmp.top = am.top = 0;\n tmp.dmax = am.dmax = top;\n tmp.neg = am.neg = 0;\n tmp.flags = am.flags = BN_FLG_STATIC_DATA;\n#if 1\n if (m->d[top - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n tmp.d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < top; i++)\n tmp.d[i] = (~m->d[i]) & BN_MASK2;\n tmp.top = top;\n } else\n#endif\n if (!bn_to_mont_fixed_top(&tmp, BN_value_one(), mont, ctx))\n goto err;\n if (a->neg || BN_ucmp(a, m) >= 0) {\n if (!BN_nnmod(&am, a, m, ctx))\n goto err;\n if (!bn_to_mont_fixed_top(&am, &am, mont, ctx))\n goto err;\n } else if (!bn_to_mont_fixed_top(&am, a, mont, ctx))\n goto err;\n#if defined(SPARC_T4_MONT)\n if (t4) {\n typedef int (*bn_pwr5_mont_f) (BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_8(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_16(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_24(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_32(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n static const bn_pwr5_mont_f pwr5_funcs[4] = {\n bn_pwr5_mont_t4_8, bn_pwr5_mont_t4_16,\n bn_pwr5_mont_t4_24, bn_pwr5_mont_t4_32\n };\n bn_pwr5_mont_f pwr5_worker = pwr5_funcs[top / 16 - 1];\n typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap, const void *bp,\n const BN_ULONG *np, const BN_ULONG *n0);\n int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n static const bn_mul_mont_f mul_funcs[4] = {\n bn_mul_mont_t4_8, bn_mul_mont_t4_16,\n bn_mul_mont_t4_24, bn_mul_mont_t4_32\n };\n bn_mul_mont_f mul_worker = mul_funcs[top / 16 - 1];\n void bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_gather5_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_flip_n_scatter5_t4(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5_t4(BN_ULONG *out, size_t num,\n void *table, size_t power);\n void bn_flip_t4(BN_ULONG *dst, BN_ULONG *src, size_t num);\n BN_ULONG *np = mont->N.d, *n0 = mont->n0;\n int stride = 5 * (6 - (top / 16 - 1));\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 0);\n bn_flip_n_scatter5_t4(am.d, top, powerbuf, 1);\n if (!(*mul_worker) (tmp.d, am.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, am.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, am.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 2);\n for (i = 3; i < 32; i++) {\n if (!(*mul_worker) (tmp.d, tmp.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, tmp.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, tmp.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, i);\n }\n np = alloca(top * sizeof(BN_ULONG));\n top /= 2;\n bn_flip_t4(np, mont->N.d, top);\n window0 = (bits - 1) % 5 + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n bn_gather5_t4(tmp.d, top, powerbuf, wvalue);\n while (bits > 0) {\n if (bits < stride)\n stride = bits;\n bits -= stride;\n wvalue = bn_get_bits(p, bits);\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n bits += stride - 5;\n wvalue >>= stride - 5;\n wvalue &= 31;\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5_t4(tmp.d, tmp.d, powerbuf, np, n0, top,\n wvalue);\n }\n bn_flip_t4(tmp.d, tmp.d, top);\n top *= 2;\n tmp.top = top;\n bn_correct_top(&tmp);\n OPENSSL_cleanse(np, top * sizeof(BN_ULONG));\n } else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window == 5 && top > 1) {\n void bn_mul_mont_gather5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_scatter5(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5(BN_ULONG *out, size_t num, void *table, size_t power);\n void bn_power5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n int bn_get_bits5(const BN_ULONG *ap, int off);\n int bn_from_montgomery(BN_ULONG *rp, const BN_ULONG *ap,\n const BN_ULONG *not_used, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n BN_ULONG *n0 = mont->n0, *np;\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n for (np = am.d + top, i = 0; i < top; i++)\n np[i] = mont->N.d[i];\n bn_scatter5(tmp.d, top, powerbuf, 0);\n bn_scatter5(am.d, am.top, powerbuf, 1);\n bn_mul_mont(tmp.d, am.d, am.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2);\n# if 0\n for (i = 3; i < 32; i++) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# else\n for (i = 4; i < 32; i *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n for (i = 3; i < 8; i += 2) {\n int j;\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n for (j = 2 * i; j < 32; j *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, j);\n }\n }\n for (; i < 16; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2 * i);\n }\n for (; i < 32; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# endif\n window0 = (bits - 1) % 5 + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n bn_gather5(tmp.d, top, powerbuf, wvalue);\n if (top & 7) {\n while (bits > 0) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5(tmp.d, tmp.d, powerbuf, np, n0, top,\n bn_get_bits5(p->d, bits -= 5));\n }\n } else {\n while (bits > 0) {\n bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top,\n bn_get_bits5(p->d, bits -= 5));\n }\n }\n ret = bn_from_montgomery(tmp.d, tmp.d, NULL, np, n0, top);\n tmp.top = top;\n bn_correct_top(&tmp);\n if (ret) {\n if (!BN_copy(rr, &tmp))\n ret = 0;\n goto err;\n }\n } else\n#endif\n {\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 0, window))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, window))\n goto err;\n if (window > 1) {\n if (!bn_mul_mont_fixed_top(&tmp, &am, &am, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 2,\n window))\n goto err;\n for (i = 3; i < numPowers; i++) {\n if (!bn_mul_mont_fixed_top(&tmp, &am, &tmp, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, i,\n window))\n goto err;\n }\n }\n window0 = (bits - 1) % window + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&tmp, top, powerbuf, wvalue,\n window))\n goto err;\n wmask = (1 << window) - 1;\n while (bits > 0) {\n for (i = 0; i < window; i++)\n if (!bn_mul_mont_fixed_top(&tmp, &tmp, &tmp, mont, ctx))\n goto err;\n bits -= window;\n wvalue = bn_get_bits(p, bits) & wmask;\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&am, top, powerbuf, wvalue,\n window))\n goto err;\n if (!bn_mul_mont_fixed_top(&tmp, &tmp, &am, mont, ctx))\n goto err;\n }\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n am.d[0] = 1;\n for (i = 1; i < top; i++)\n am.d[i] = 0;\n if (!BN_mod_mul_montgomery(rr, &tmp, &am, mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, &tmp, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n if (powerbuf != NULL) {\n OPENSSL_cleanse(powerbuf, powerbufLen);\n OPENSSL_free(powerbufFree);\n }\n BN_CTX_end(ctx);\n return ret;\n}', 'int bn_to_mont_fixed_top(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,\n BN_CTX *ctx)\n{\n return bn_mul_mont_fixed_top(r, a, &(mont->RR), mont, ctx);\n}', 'int bn_mul_mont_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n BN_MONT_CTX *mont, BN_CTX *ctx)\n{\n BIGNUM *tmp;\n int ret = 0;\n int num = mont->N.top;\n#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\n if (num > 1 && a->top == num && b->top == num) {\n if (bn_wexpand(r, num) == NULL)\n return 0;\n if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {\n r->neg = a->neg ^ b->neg;\n r->top = num;\n r->flags |= BN_FLG_FIXED_TOP;\n return 1;\n }\n }\n#endif\n if ((a->top + b->top) > 2 * num)\n return 0;\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n if (tmp == NULL)\n goto err;\n bn_check_top(tmp);\n if (a == b) {\n if (!BN_sqr(tmp, a, ctx))\n goto err;\n } else {\n if (!BN_mul(tmp, a, b, ctx))\n goto err;\n }\n#ifdef MONT_WORD\n if (!bn_from_montgomery_word(r, tmp, mont))\n goto err;\n#else\n if (!BN_from_montgomery(r, tmp, mont, ctx))\n goto err;\n#endif\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int max, al;\n int ret = 0;\n BIGNUM *tmp, *rr;\n bn_check_top(a);\n al = a->top;\n if (al <= 0) {\n r->top = 0;\n r->neg = 0;\n return 1;\n }\n BN_CTX_start(ctx);\n rr = (a != r) ? r : BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (rr == NULL || tmp == NULL)\n goto err;\n max = 2 * al;\n if (bn_wexpand(rr, max) == NULL)\n goto err;\n if (al == 4) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[8];\n bn_sqr_normal(rr->d, a->d, 4, t);\n#else\n bn_sqr_comba4(rr->d, a->d);\n#endif\n } else if (al == 8) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[16];\n bn_sqr_normal(rr->d, a->d, 8, t);\n#else\n bn_sqr_comba8(rr->d, a->d);\n#endif\n } else {\n#if defined(BN_RECURSION)\n if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {\n BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];\n bn_sqr_normal(rr->d, a->d, al, t);\n } else {\n int j, k;\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n if (al == j) {\n if (bn_wexpand(tmp, k * 2) == NULL)\n goto err;\n bn_sqr_recursive(rr->d, a->d, al, tmp->d);\n } else {\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n }\n }\n#else\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n#endif\n }\n rr->neg = 0;\n rr->top = max;\n bn_correct_top(rr);\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(rr);\n bn_check_top(tmp);\n BN_CTX_end(ctx);\n return ret;\n}', 'void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)\n{\n int i, j, max;\n const BN_ULONG *ap;\n BN_ULONG *rp;\n max = n * 2;\n ap = a;\n rp = r;\n rp[0] = rp[max - 1] = 0;\n rp++;\n j = n;\n if (--j > 0) {\n ap++;\n rp[j] = bn_mul_words(rp, ap, j, ap[-1]);\n rp += 2;\n }\n for (i = n - 2; i > 0; i--) {\n j--;\n ap++;\n rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);\n rp += 2;\n }\n bn_add_words(r, r, r, max);\n bn_sqr_words(tmp, a, n);\n bn_add_words(r, r, tmp, max);\n}']
26
0
https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/h264pred.c/#L214
static void pred4x4_down_left_rv40_notop_c(uint8_t *src, uint8_t *topright, int stride){ LOAD_LEFT_EDGE LOAD_DOWN_LEFT_EDGE src[0+0*stride]=(l0 + l2 + 2*l1 + 2)>>2; src[1+0*stride]= src[0+1*stride]=(l1 + l3 + 2*l2 + 2)>>2; src[2+0*stride]= src[1+1*stride]= src[0+2*stride]=(l2 + l4 + 2*l3 + 2)>>2; src[3+0*stride]= src[2+1*stride]= src[1+2*stride]= src[0+3*stride]=(l3 + l5 + 2*l4 + 2)>>2; src[3+1*stride]= src[2+2*stride]= src[1+3*stride]=(l4 + l6 + 2*l5 + 2)>>2; src[3+2*stride]= src[2+3*stride]=(l5 + l7 + 2*l6 + 2)>>2; src[3+3*stride]=(l6 + l7 + 1)>>1; }
['static void pred4x4_down_left_rv40_notop_c(uint8_t *src, uint8_t *topright, int stride){\n LOAD_LEFT_EDGE\n LOAD_DOWN_LEFT_EDGE\n src[0+0*stride]=(l0 + l2 + 2*l1 + 2)>>2;\n src[1+0*stride]=\n src[0+1*stride]=(l1 + l3 + 2*l2 + 2)>>2;\n src[2+0*stride]=\n src[1+1*stride]=\n src[0+2*stride]=(l2 + l4 + 2*l3 + 2)>>2;\n src[3+0*stride]=\n src[2+1*stride]=\n src[1+2*stride]=\n src[0+3*stride]=(l3 + l5 + 2*l4 + 2)>>2;\n src[3+1*stride]=\n src[2+2*stride]=\n src[1+3*stride]=(l4 + l6 + 2*l5 + 2)>>2;\n src[3+2*stride]=\n src[2+3*stride]=(l5 + l7 + 2*l6 + 2)>>2;\n src[3+3*stride]=(l6 + l7 + 1)>>1;\n}']
27
0
https://github.com/openssl/openssl/blob/305b68f1a2b6d4d0aa07a6ab47ac372f067a40bb/crypto/bn/bn_lib.c/#L231
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *a = NULL; if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return NULL; } if (BN_get_flags(b, BN_FLG_SECURE)) a = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = OPENSSL_zalloc(words * sizeof(*a)); if (a == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return NULL; } assert(b->top <= words); if (b->top > 0) memcpy(a, b->d, sizeof(*a) * b->top); return a; }
['int BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x,\n const BIGNUM *p, BN_CTX *ctx)\n{\n BIGNUM *xinv = NULL;\n int ret = 0;\n bn_check_top(y);\n bn_check_top(x);\n bn_check_top(p);\n BN_CTX_start(ctx);\n xinv = BN_CTX_get(ctx);\n if (xinv == NULL)\n goto err;\n if (!BN_GF2m_mod_inv(xinv, x, p, ctx))\n goto err;\n if (!BN_GF2m_mod_mul(r, y, xinv, p, ctx))\n goto err;\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n a->flags &= ~BN_FLG_FIXED_TOP;\n bn_check_top(a);\n return 1;\n}', 'int BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)\n{\n BIGNUM *b = NULL;\n int ret = 0;\n BN_CTX_start(ctx);\n if ((b = BN_CTX_get(ctx)) == NULL)\n goto err;\n do {\n if (!BN_priv_rand(b, BN_num_bits(p) - 1,\n BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))\n goto err;\n } while (BN_is_zero(b));\n if (!BN_GF2m_mod_mul(r, a, b, p, ctx))\n goto err;\n if (!BN_GF2m_mod_inv_vartime(r, r, p, ctx))\n goto err;\n if (!BN_GF2m_mod_mul(r, r, b, p, ctx))\n goto err;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_GF2m_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n const BIGNUM *p, BN_CTX *ctx)\n{\n int ret = 0;\n const int max = BN_num_bits(p) + 1;\n int *arr = NULL;\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(p);\n if ((arr = OPENSSL_malloc(sizeof(*arr) * max)) == NULL)\n goto err;\n ret = BN_GF2m_poly2arr(p, arr, max);\n if (!ret || ret > max) {\n BNerr(BN_F_BN_GF2M_MOD_MUL, BN_R_INVALID_LENGTH);\n goto err;\n }\n ret = BN_GF2m_mod_mul_arr(r, a, b, arr, ctx);\n bn_check_top(r);\n err:\n OPENSSL_free(arr);\n return ret;\n}', 'int BN_GF2m_mod_mul_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n const int p[], BN_CTX *ctx)\n{\n int zlen, i, j, k, ret = 0;\n BIGNUM *s;\n BN_ULONG x1, x0, y1, y0, zz[4];\n bn_check_top(a);\n bn_check_top(b);\n if (a == b) {\n return BN_GF2m_mod_sqr_arr(r, a, p, ctx);\n }\n BN_CTX_start(ctx);\n if ((s = BN_CTX_get(ctx)) == NULL)\n goto err;\n zlen = a->top + b->top + 4;\n if (!bn_wexpand(s, zlen))\n goto err;\n s->top = zlen;\n for (i = 0; i < zlen; i++)\n s->d[i] = 0;\n for (j = 0; j < b->top; j += 2) {\n y0 = b->d[j];\n y1 = ((j + 1) == b->top) ? 0 : b->d[j + 1];\n for (i = 0; i < a->top; i += 2) {\n x0 = a->d[i];\n x1 = ((i + 1) == a->top) ? 0 : a->d[i + 1];\n bn_GF2m_mul_2x2(zz, x1, x0, y1, y0);\n for (k = 0; k < 4; k++)\n s->d[i + j + k] ^= zz[k];\n }\n }\n bn_correct_top(s);\n if (BN_GF2m_mod_arr(r, s, p))\n ret = 1;\n bn_check_top(r);\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_GF2m_mod_arr(BIGNUM *r, const BIGNUM *a, const int p[])\n{\n int j, k;\n int n, dN, d0, d1;\n BN_ULONG zz, *z;\n bn_check_top(a);\n if (!p[0]) {\n BN_zero(r);\n return 1;\n }\n if (a != r) {\n if (!bn_wexpand(r, a->top))\n return 0;\n for (j = 0; j < a->top; j++) {\n r->d[j] = a->d[j];\n }\n r->top = a->top;\n }\n z = r->d;\n dN = p[0] / BN_BITS2;\n for (j = r->top - 1; j > dN;) {\n zz = z[j];\n if (z[j] == 0) {\n j--;\n continue;\n }\n z[j] = 0;\n for (k = 1; p[k] != 0; k++) {\n n = p[0] - p[k];\n d0 = n % BN_BITS2;\n d1 = BN_BITS2 - d0;\n n /= BN_BITS2;\n z[j - n] ^= (zz >> d0);\n if (d0)\n z[j - n - 1] ^= (zz << d1);\n }\n n = dN;\n d0 = p[0] % BN_BITS2;\n d1 = BN_BITS2 - d0;\n z[j - n] ^= (zz >> d0);\n if (d0)\n z[j - n - 1] ^= (zz << d1);\n }\n while (j == dN) {\n d0 = p[0] % BN_BITS2;\n zz = z[dN] >> d0;\n if (zz == 0)\n break;\n d1 = BN_BITS2 - d0;\n if (d0)\n z[dN] = (z[dN] << d1) >> d1;\n else\n z[dN] = 0;\n z[0] ^= zz;\n for (k = 1; p[k] != 0; k++) {\n BN_ULONG tmp_ulong;\n n = p[k] / BN_BITS2;\n d0 = p[k] % BN_BITS2;\n d1 = BN_BITS2 - d0;\n z[n] ^= (zz << d0);\n if (d0 && (tmp_ulong = zz >> d1))\n z[n + 1] ^= tmp_ulong;\n }\n }\n bn_correct_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 if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}']
28
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 DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)\n{\n BIGNUM *kinv = NULL;\n BIGNUM *m, *blind, *blindm, *tmp;\n BN_CTX *ctx = NULL;\n int reason = ERR_R_BN_LIB;\n DSA_SIG *ret = NULL;\n int rv = 0;\n if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) {\n reason = DSA_R_MISSING_PARAMETERS;\n goto err;\n }\n ret = DSA_SIG_new();\n if (ret == NULL)\n goto err;\n ret->r = BN_new();\n ret->s = BN_new();\n if (ret->r == NULL || ret->s == NULL)\n goto err;\n ctx = BN_CTX_new();\n if (ctx == NULL)\n goto err;\n m = BN_CTX_get(ctx);\n blind = BN_CTX_get(ctx);\n blindm = BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (tmp == NULL)\n goto err;\n redo:\n if (!dsa_sign_setup(dsa, ctx, &kinv, &ret->r, dgst, dlen))\n goto err;\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 do {\n if (!BN_priv_rand(blind, BN_num_bits(dsa->q) - 1,\n BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))\n goto err;\n } while (BN_is_zero(blind));\n BN_set_flags(blind, BN_FLG_CONSTTIME);\n BN_set_flags(blindm, BN_FLG_CONSTTIME);\n BN_set_flags(tmp, BN_FLG_CONSTTIME);\n if (!BN_mod_mul(tmp, blind, dsa->priv_key, dsa->q, ctx))\n goto err;\n if (!BN_mod_mul(tmp, tmp, ret->r, dsa->q, ctx))\n goto err;\n if (!BN_mod_mul(blindm, blind, m, dsa->q, ctx))\n goto err;\n if (!BN_mod_add_quick(ret->s, tmp, blindm, dsa->q))\n goto err;\n if (!BN_mod_mul(ret->s, ret->s, kinv, dsa->q, ctx))\n goto err;\n if (BN_mod_inverse(blind, blind, dsa->q, ctx) == NULL)\n goto err;\n if (!BN_mod_mul(ret->s, ret->s, blind, dsa->q, ctx))\n goto err;\n if (BN_is_zero(ret->r) || BN_is_zero(ret->s))\n goto redo;\n rv = 1;\n err:\n if (rv == 0) {\n DSAerr(DSA_F_DSA_DO_SIGN, reason);\n DSA_SIG_free(ret);\n ret = NULL;\n }\n BN_CTX_free(ctx);\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, *kinv = NULL, *r = *rp;\n BIGNUM *l;\n int ret = 0;\n int q_bits, q_words;\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 l = BN_new();\n if (k == NULL || l == NULL)\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 q_bits = BN_num_bits(dsa->q);\n q_words = bn_get_top(dsa->q);\n if (!bn_wexpand(k, q_words + 2)\n || !bn_wexpand(l, q_words + 2))\n goto err;\n do {\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 if (!BN_priv_rand_range(k, dsa->q))\n goto err;\n } while (BN_is_zero(k));\n BN_set_flags(k, BN_FLG_CONSTTIME);\n BN_set_flags(l, BN_FLG_CONSTTIME);\n if (dsa->flags & DSA_FLAG_CACHE_MONT_P) {\n if (!BN_MONT_CTX_set_locked(&dsa->method_mont_p,\n dsa->lock, dsa->p, ctx))\n goto err;\n }\n if (!BN_add(l, k, dsa->q)\n || !BN_add(k, l, dsa->q))\n goto err;\n BN_consttime_swap(BN_is_bit_set(l, q_bits), k, l, q_words + 2);\n if ((dsa)->meth->bn_mod_exp != NULL) {\n if (!dsa->meth->bn_mod_exp(dsa, r, dsa->g, k, dsa->p, ctx,\n dsa->method_mont_p))\n goto err;\n } else {\n if (!BN_mod_exp_mont(r, dsa->g, k, dsa->p, ctx, dsa->method_mont_p))\n goto err;\n }\n if (!BN_mod(r, r, dsa->q, ctx))\n goto err;\n if ((kinv = dsa_mod_inverse_fermat(k, dsa->q, ctx)) == NULL)\n goto err;\n BN_clear_free(*kinvp);\n *kinvp = kinv;\n kinv = NULL;\n ret = 1;\n err:\n if (!ret)\n DSAerr(DSA_F_DSA_SIGN_SETUP, ERR_R_BN_LIB);\n if (ctx != ctx_in)\n BN_CTX_free(ctx);\n BN_clear_free(k);\n BN_clear_free(l);\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("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_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}', '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}']
29
0
https://github.com/libav/libav/blob/4cd19f6e7851ee6afb08eb346c82d5574fa2b699/libavcodec/mpc8.c/#L47
static inline int mpc8_dec_base(GetBitContext *gb, int k, int n) { int code = get_bits(gb, mpc8_cnk_len[k-1][n-1] - 1); if (code >= mpc8_cnk_lost[k-1][n-1]) code = ((code << 1) | get_bits1(gb)) - mpc8_cnk_lost[k-1][n-1]; return code; }
['static int mpc8_decode_frame(AVCodecContext * avctx,\n void *data, int *data_size,\n const uint8_t * buf, int buf_size)\n{\n MPCContext *c = avctx->priv_data;\n GetBitContext gb2, *gb = &gb2;\n int i, j, k, ch, cnt, res, t;\n Band *bands = c->bands;\n int off;\n int maxband, keyframe;\n int last[2];\n keyframe = c->cur_frame == 0;\n if(keyframe){\n memset(c->Q, 0, sizeof(c->Q));\n c->last_bits_used = 0;\n }\n init_get_bits(gb, buf, buf_size * 8);\n skip_bits(gb, c->last_bits_used & 7);\n if(keyframe)\n maxband = mpc8_get_mod_golomb(gb, c->maxbands + 1);\n else{\n maxband = c->last_max_band + get_vlc2(gb, band_vlc.table, MPC8_BANDS_BITS, 2);\n if(maxband > 32) maxband -= 33;\n }\n c->last_max_band = maxband;\n if(maxband){\n last[0] = last[1] = 0;\n for(i = maxband - 1; i >= 0; i--){\n for(ch = 0; ch < 2; ch++){\n last[ch] = get_vlc2(gb, res_vlc[last[ch] > 2].table, MPC8_RES_BITS, 2) + last[ch];\n if(last[ch] > 15) last[ch] -= 17;\n bands[i].res[ch] = last[ch];\n }\n }\n if(c->MSS){\n int mask;\n cnt = 0;\n for(i = 0; i < maxband; i++)\n if(bands[i].res[0] || bands[i].res[1])\n cnt++;\n t = mpc8_get_mod_golomb(gb, cnt);\n mask = mpc8_get_mask(gb, cnt, t);\n for(i = maxband - 1; i >= 0; i--)\n if(bands[i].res[0] || bands[i].res[1]){\n bands[i].msf = mask & 1;\n mask >>= 1;\n }\n }\n }\n for(i = maxband; i < c->maxbands; i++)\n bands[i].res[0] = bands[i].res[1] = 0;\n if(keyframe){\n for(i = 0; i < 32; i++)\n c->oldDSCF[0][i] = c->oldDSCF[1][i] = 1;\n }\n for(i = 0; i < maxband; i++){\n if(bands[i].res[0] || bands[i].res[1]){\n cnt = !!bands[i].res[0] + !!bands[i].res[1] - 1;\n if(cnt >= 0){\n t = get_vlc2(gb, scfi_vlc[cnt].table, scfi_vlc[cnt].bits, 1);\n if(bands[i].res[0]) bands[i].scfi[0] = t >> (2 * cnt);\n if(bands[i].res[1]) bands[i].scfi[1] = t & 3;\n }\n }\n }\n for(i = 0; i < maxband; i++){\n for(ch = 0; ch < 2; ch++){\n if(!bands[i].res[ch]) continue;\n if(c->oldDSCF[ch][i]){\n bands[i].scf_idx[ch][0] = get_bits(gb, 7) - 6;\n c->oldDSCF[ch][i] = 0;\n }else{\n t = get_vlc2(gb, dscf_vlc[1].table, MPC8_DSCF1_BITS, 2);\n if(t == 64)\n t += get_bits(gb, 6);\n bands[i].scf_idx[ch][0] = ((bands[i].scf_idx[ch][2] + t - 25) & 0x7F) - 6;\n }\n for(j = 0; j < 2; j++){\n if((bands[i].scfi[ch] << j) & 2)\n bands[i].scf_idx[ch][j + 1] = bands[i].scf_idx[ch][j];\n else{\n t = get_vlc2(gb, dscf_vlc[0].table, MPC8_DSCF0_BITS, 2);\n if(t == 31)\n t = 64 + get_bits(gb, 6);\n bands[i].scf_idx[ch][j + 1] = ((bands[i].scf_idx[ch][j] + t - 25) & 0x7F) - 6;\n }\n }\n }\n }\n for(i = 0, off = 0; i < maxband; i++, off += SAMPLES_PER_BAND){\n for(ch = 0; ch < 2; ch++){\n res = bands[i].res[ch];\n switch(res){\n case -1:\n for(j = 0; j < SAMPLES_PER_BAND; j++)\n c->Q[ch][off + j] = (av_random(&c->rnd) & 0x3FC) - 510;\n break;\n case 0:\n break;\n case 1:\n for(j = 0; j < SAMPLES_PER_BAND; j += SAMPLES_PER_BAND / 2){\n cnt = get_vlc2(gb, q1_vlc.table, MPC8_Q1_BITS, 2);\n t = mpc8_get_mask(gb, 18, cnt);\n for(k = 0; k < SAMPLES_PER_BAND / 2; k++, t <<= 1)\n c->Q[ch][off + j + k] = (t & 0x20000) ? (get_bits1(gb) << 1) - 1 : 0;\n }\n break;\n case 2:\n cnt = 6;\n for(j = 0; j < SAMPLES_PER_BAND; j += 3){\n t = get_vlc2(gb, q2_vlc[cnt > 3].table, MPC8_Q2_BITS, 2);\n c->Q[ch][off + j + 0] = mpc8_idx50[t];\n c->Q[ch][off + j + 1] = mpc8_idx51[t];\n c->Q[ch][off + j + 2] = mpc8_idx52[t];\n cnt = (cnt >> 1) + mpc8_huffq2[t];\n }\n break;\n case 3:\n case 4:\n for(j = 0; j < SAMPLES_PER_BAND; j += 2){\n t = get_vlc2(gb, q3_vlc[res - 3].table, MPC8_Q3_BITS, 2) + q3_offsets[res - 3];\n c->Q[ch][off + j + 1] = t >> 4;\n c->Q[ch][off + j + 0] = (t & 8) ? (t & 0xF) - 16 : (t & 0xF);\n }\n break;\n case 5:\n case 6:\n case 7:\n case 8:\n cnt = 2 * mpc8_thres[res];\n for(j = 0; j < SAMPLES_PER_BAND; j++){\n t = get_vlc2(gb, quant_vlc[res - 5][cnt > mpc8_thres[res]].table, quant_vlc[res - 5][cnt > mpc8_thres[res]].bits, 2) + quant_offsets[res - 5];\n c->Q[ch][off + j] = t;\n cnt = (cnt >> 1) + FFABS(c->Q[ch][off + j]);\n }\n break;\n default:\n for(j = 0; j < SAMPLES_PER_BAND; j++){\n c->Q[ch][off + j] = get_vlc2(gb, q9up_vlc.table, MPC8_Q9UP_BITS, 2);\n if(res != 9){\n c->Q[ch][off + j] <<= res - 9;\n c->Q[ch][off + j] |= get_bits(gb, res - 9);\n }\n c->Q[ch][off + j] -= (1 << (res - 2)) - 1;\n }\n }\n }\n }\n ff_mpc_dequantize_and_synth(c, maxband, data);\n c->cur_frame++;\n c->last_bits_used = get_bits_count(gb);\n if(c->cur_frame >= c->frames)\n c->cur_frame = 0;\n *data_size = MPC_FRAME_SIZE * 4;\n return c->cur_frame ? c->last_bits_used >> 3 : buf_size;\n}', 'static int mpc8_get_mask(GetBitContext *gb, int size, int t)\n{\n int mask = 0;\n if(t && t != size)\n mask = mpc8_dec_enum(gb, FFMIN(t, size - t), size);\n if((t << 1) > size) mask = ~mask;\n return mask;\n}', 'static inline int mpc8_dec_enum(GetBitContext *gb, int k, int n)\n{\n int bits = 0;\n const uint32_t * C = mpc8_cnk[k-1];\n int code = mpc8_dec_base(gb, k, n);\n do {\n n--;\n if (code >= C[n]) {\n bits |= 1 << n;\n code -= C[n];\n C -= 32;\n k--;\n }\n } while(k > 0);\n return bits;\n}', 'static inline int mpc8_dec_base(GetBitContext *gb, int k, int n)\n{\n int code = get_bits(gb, mpc8_cnk_len[k-1][n-1] - 1);\n if (code >= mpc8_cnk_lost[k-1][n-1])\n code = ((code << 1) | get_bits1(gb)) - mpc8_cnk_lost[k-1][n-1];\n return code;\n}']
30
0
https://github.com/openssl/openssl/blob/8e826a339f8cda20a4311fa88a1de782972cf40d/crypto/bn/bn_shift.c/#L112
int BN_lshift(BIGNUM *r, const BIGNUM *a, int n) { int i, nw, lb, rb; BN_ULONG *t, *f; BN_ULONG l; bn_check_top(r); bn_check_top(a); if (n < 0) { BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT); return 0; } nw = n / BN_BITS2; if (bn_wexpand(r, a->top + nw + 1) == NULL) return (0); r->neg = a->neg; lb = n % BN_BITS2; rb = BN_BITS2 - lb; f = a->d; t = r->d; t[a->top + nw] = 0; if (lb == 0) for (i = a->top - 1; i >= 0; i--) t[nw + i] = f[i]; else for (i = a->top - 1; i >= 0; i--) { l = f[i]; t[nw + i + 1] |= (l >> rb) & BN_MASK2; t[nw + i] = (l << lb) & BN_MASK2; } memset(t, 0, sizeof(*t) * nw); r->top = a->top + nw + 1; bn_correct_top(r); bn_check_top(r); return 1; }
['static int srp_Verify_N_and_g(const BIGNUM *N, const BIGNUM *g)\n{\n BN_CTX *bn_ctx = BN_CTX_new();\n BIGNUM *p = BN_new();\n BIGNUM *r = BN_new();\n int ret =\n g != NULL && N != NULL && bn_ctx != NULL && BN_is_odd(N) &&\n BN_is_prime_ex(N, SRP_NUMBER_ITERATIONS_FOR_PRIME, bn_ctx, NULL) == 1 &&\n p != NULL && BN_rshift1(p, N) &&\n BN_is_prime_ex(p, SRP_NUMBER_ITERATIONS_FOR_PRIME, bn_ctx, NULL) == 1 &&\n r != NULL &&\n BN_mod_exp(r, g, p, N, bn_ctx) &&\n BN_add_word(r, 1) && BN_cmp(r, N) == 0;\n BN_free(r);\n BN_free(p);\n BN_CTX_free(bn_ctx);\n return ret;\n}', 'int BN_is_odd(const BIGNUM *a)\n{\n return (a->top > 0) && (a->d[0] & 1);\n}', 'int BN_is_prime_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,\n BN_GENCB *cb)\n{\n return BN_is_prime_fasttest_ex(a, checks, ctx_passed, 0, cb);\n}', 'int BN_is_prime_fasttest_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,\n int do_trial_division, BN_GENCB *cb)\n{\n int i, j, ret = -1;\n int k;\n BN_CTX *ctx = NULL;\n BIGNUM *A1, *A1_odd, *check;\n BN_MONT_CTX *mont = NULL;\n if (BN_cmp(a, BN_value_one()) <= 0)\n return 0;\n if (checks == BN_prime_checks)\n checks = BN_prime_checks_for_size(BN_num_bits(a));\n if (!BN_is_odd(a))\n return BN_is_word(a, 2);\n if (do_trial_division) {\n for (i = 1; i < NUMPRIMES; i++) {\n BN_ULONG mod = BN_mod_word(a, primes[i]);\n if (mod == (BN_ULONG)-1)\n goto err;\n if (mod == 0)\n return BN_is_word(a, primes[i]);\n }\n if (!BN_GENCB_call(cb, 1, -1))\n goto err;\n }\n if (ctx_passed != NULL)\n ctx = ctx_passed;\n else if ((ctx = BN_CTX_new()) == NULL)\n goto err;\n BN_CTX_start(ctx);\n A1 = BN_CTX_get(ctx);\n A1_odd = BN_CTX_get(ctx);\n check = BN_CTX_get(ctx);\n if (check == NULL)\n goto err;\n if (!BN_copy(A1, a))\n goto err;\n if (!BN_sub_word(A1, 1))\n goto err;\n if (BN_is_zero(A1)) {\n ret = 0;\n goto err;\n }\n k = 1;\n while (!BN_is_bit_set(A1, k))\n k++;\n if (!BN_rshift(A1_odd, A1, k))\n goto err;\n mont = BN_MONT_CTX_new();\n if (mont == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, a, ctx))\n goto err;\n for (i = 0; i < checks; i++) {\n if (!BN_priv_rand_range(check, A1))\n goto err;\n if (!BN_add_word(check, 1))\n goto err;\n j = witness(check, a, A1, A1_odd, k, ctx, mont);\n if (j == -1)\n goto err;\n if (j) {\n ret = 0;\n goto err;\n }\n if (!BN_GENCB_call(cb, 1, i))\n goto err;\n }\n ret = 1;\n err:\n if (ctx != NULL) {\n BN_CTX_end(ctx);\n if (ctx_passed == NULL)\n BN_CTX_free(ctx);\n }\n BN_MONT_CTX_free(mont);\n return (ret);\n}', 'int BN_cmp(const BIGNUM *a, const BIGNUM *b)\n{\n int i;\n int gt, lt;\n BN_ULONG t1, t2;\n if ((a == NULL) || (b == NULL)) {\n if (a != NULL)\n return (-1);\n else if (b != NULL)\n return 1;\n else\n return (0);\n }\n bn_check_top(a);\n bn_check_top(b);\n if (a->neg != b->neg) {\n if (a->neg)\n return (-1);\n else\n return 1;\n }\n if (a->neg == 0) {\n gt = 1;\n lt = -1;\n } else {\n gt = -1;\n lt = 1;\n }\n if (a->top > b->top)\n return (gt);\n if (a->top < b->top)\n return (lt);\n for (i = a->top - 1; i >= 0; i--) {\n t1 = a->d[i];\n t2 = b->d[i];\n if (t1 > t2)\n return (gt);\n if (t1 < t2)\n return (lt);\n }\n return (0);\n}', 'int BN_rshift1(BIGNUM *r, const BIGNUM *a)\n{\n BN_ULONG *ap, *rp, t, c;\n int i, j;\n bn_check_top(r);\n bn_check_top(a);\n if (BN_is_zero(a)) {\n BN_zero(r);\n return 1;\n }\n i = a->top;\n ap = a->d;\n j = i - (ap[i - 1] == 1);\n if (a != r) {\n if (bn_wexpand(r, j) == NULL)\n return (0);\n r->neg = a->neg;\n }\n rp = r->d;\n t = ap[--i];\n c = (t & 1) ? BN_TBIT : 0;\n if (t >>= 1)\n rp[i] = t;\n while (i > 0) {\n t = ap[--i];\n rp[i] = ((t >> 1) & BN_MASK2) | c;\n c = (t & 1) ? BN_TBIT : 0;\n }\n r->top = j;\n if (!r->top)\n r->neg = 0;\n bn_check_top(r);\n return 1;\n}', 'int BN_is_zero(const BIGNUM *a)\n{\n return a->top == 0;\n}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n BN_CTX *ctx)\n{\n int ret;\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n if (BN_is_odd(m)) {\n# ifdef MONT_EXP_WORD\n if (a->top == 1 && !a->neg\n && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(a, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(m, BN_FLG_CONSTTIME) == 0)) {\n BN_ULONG A = a->d[0];\n ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);\n } else\n# endif\n ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);\n } else\n#endif\n#ifdef RECP_MUL_MOD\n {\n ret = BN_mod_exp_recp(r, a, p, m, ctx);\n }\n#else\n {\n ret = BN_mod_exp_simple(r, a, p, m, ctx);\n }\n#endif\n bn_check_top(r);\n return (ret);\n}', 'int BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n BN_MONT_CTX *mont = NULL;\n int b, bits, ret = 0;\n int r_is_one;\n BN_ULONG w, next_w;\n BIGNUM *r, *t;\n BIGNUM *swap_tmp;\n#define BN_MOD_MUL_WORD(r, w, m) \\\n (BN_mul_word(r, (w)) && \\\n ( \\\n (BN_mod(t, r, m, ctx) && (swap_tmp = r, r = t, t = swap_tmp, 1))))\n#define BN_TO_MONTGOMERY_WORD(r, w, mont) \\\n (BN_set_word(r, (w)) && BN_to_montgomery(r, r, (mont), ctx))\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n BNerr(BN_F_BN_MOD_EXP_MONT_WORD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT_WORD, BN_R_CALLED_WITH_EVEN_MODULUS);\n return (0);\n }\n if (m->top == 1)\n a %= m->d[0];\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_is_one(m)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n if (a == 0) {\n BN_zero(rr);\n ret = 1;\n return ret;\n }\n BN_CTX_start(ctx);\n r = BN_CTX_get(ctx);\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n r_is_one = 1;\n w = a;\n for (b = bits - 2; b >= 0; b--) {\n next_w = w * w;\n if ((next_w / w) != w) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n next_w = 1;\n }\n w = next_w;\n if (!r_is_one) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (BN_is_bit_set(p, b)) {\n next_w = w * a;\n if ((next_w / a) != w) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n next_w = a;\n }\n w = next_w;\n }\n }\n if (w != 1) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n }\n if (r_is_one) {\n if (!BN_one(rr))\n goto err;\n } else {\n if (!BN_from_montgomery(rr, r, mont, ctx))\n goto err;\n }\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n BN_CTX_end(ctx);\n bn_check_top(rr);\n return (ret);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return (0);\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return (0);\n }\n if (dv != NULL)\n BN_zero(dv);\n return 1;\n }\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n resp++;\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n resp--;\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return (0);\n}', 'int BN_num_bits(const BIGNUM *a)\n{\n int i = a->top - 1;\n bn_check_top(a);\n if (BN_is_zero(a))\n return 0;\n return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));\n}', 'int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l;\n bn_check_top(r);\n bn_check_top(a);\n if (n < 0) {\n BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n nw = n / BN_BITS2;\n if (bn_wexpand(r, a->top + nw + 1) == NULL)\n return (0);\n r->neg = a->neg;\n lb = n % BN_BITS2;\n rb = BN_BITS2 - lb;\n f = a->d;\n t = r->d;\n t[a->top + nw] = 0;\n if (lb == 0)\n for (i = a->top - 1; i >= 0; i--)\n t[nw + i] = f[i];\n else\n for (i = a->top - 1; i >= 0; i--) {\n l = f[i];\n t[nw + i + 1] |= (l >> rb) & BN_MASK2;\n t[nw + i] = (l << lb) & BN_MASK2;\n }\n memset(t, 0, sizeof(*t) * nw);\n r->top = a->top + nw + 1;\n bn_correct_top(r);\n bn_check_top(r);\n return 1;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']
31
0
https://github.com/libav/libav/blob/1e8b9738fa70e20967ddb542d2f9d5552fc51ec6/libavcodec/h264.c/#L3764
static av_always_inline void fill_filter_caches_inter(H264Context *h, int mb_type, int top_xy, int left_xy[LEFT_MBS], int top_type, int left_type[LEFT_MBS], int mb_xy, int list) { int b_stride = h->b_stride; int16_t(*mv_dst)[2] = &h->mv_cache[list][scan8[0]]; int8_t *ref_cache = &h->ref_cache[list][scan8[0]]; if (IS_INTER(mb_type) || IS_DIRECT(mb_type)) { if (USES_LIST(top_type, list)) { const int b_xy = h->mb2b_xy[top_xy] + 3 * b_stride; const int b8_xy = 4 * top_xy + 2; int (*ref2frm)[64] = h->ref2frm[h->slice_table[top_xy] & (MAX_SLICES - 1)][0] + (MB_MBAFF(h) ? 20 : 2); AV_COPY128(mv_dst - 1 * 8, h->cur_pic.motion_val[list][b_xy + 0]); ref_cache[0 - 1 * 8] = ref_cache[1 - 1 * 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 0]]; ref_cache[2 - 1 * 8] = ref_cache[3 - 1 * 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 1]]; } else { AV_ZERO128(mv_dst - 1 * 8); AV_WN32A(&ref_cache[0 - 1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u); } if (!IS_INTERLACED(mb_type ^ left_type[LTOP])) { if (USES_LIST(left_type[LTOP], list)) { const int b_xy = h->mb2b_xy[left_xy[LTOP]] + 3; const int b8_xy = 4 * left_xy[LTOP] + 1; int (*ref2frm)[64] = h->ref2frm[h->slice_table[left_xy[LTOP]] & (MAX_SLICES - 1)][0] + (MB_MBAFF(h) ? 20 : 2); AV_COPY32(mv_dst - 1 + 0, h->cur_pic.motion_val[list][b_xy + b_stride * 0]); AV_COPY32(mv_dst - 1 + 8, h->cur_pic.motion_val[list][b_xy + b_stride * 1]); AV_COPY32(mv_dst - 1 + 16, h->cur_pic.motion_val[list][b_xy + b_stride * 2]); AV_COPY32(mv_dst - 1 + 24, h->cur_pic.motion_val[list][b_xy + b_stride * 3]); ref_cache[-1 + 0] = ref_cache[-1 + 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 2 * 0]]; ref_cache[-1 + 16] = ref_cache[-1 + 24] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 2 * 1]]; } else { AV_ZERO32(mv_dst - 1 + 0); AV_ZERO32(mv_dst - 1 + 8); AV_ZERO32(mv_dst - 1 + 16); AV_ZERO32(mv_dst - 1 + 24); ref_cache[-1 + 0] = ref_cache[-1 + 8] = ref_cache[-1 + 16] = ref_cache[-1 + 24] = LIST_NOT_USED; } } } if (!USES_LIST(mb_type, list)) { fill_rectangle(mv_dst, 4, 4, 8, pack16to32(0, 0), 4); AV_WN32A(&ref_cache[0 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u); AV_WN32A(&ref_cache[1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u); AV_WN32A(&ref_cache[2 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u); AV_WN32A(&ref_cache[3 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u); return; } { int8_t *ref = &h->cur_pic.ref_index[list][4 * mb_xy]; int (*ref2frm)[64] = h->ref2frm[h->slice_num & (MAX_SLICES - 1)][0] + (MB_MBAFF(h) ? 20 : 2); uint32_t ref01 = (pack16to32(ref2frm[list][ref[0]], ref2frm[list][ref[1]]) & 0x00FF00FF) * 0x0101; uint32_t ref23 = (pack16to32(ref2frm[list][ref[2]], ref2frm[list][ref[3]]) & 0x00FF00FF) * 0x0101; AV_WN32A(&ref_cache[0 * 8], ref01); AV_WN32A(&ref_cache[1 * 8], ref01); AV_WN32A(&ref_cache[2 * 8], ref23); AV_WN32A(&ref_cache[3 * 8], ref23); } { int16_t(*mv_src)[2] = &h->cur_pic.motion_val[list][4 * h->mb_x + 4 * h->mb_y * b_stride]; AV_COPY128(mv_dst + 8 * 0, mv_src + 0 * b_stride); AV_COPY128(mv_dst + 8 * 1, mv_src + 1 * b_stride); AV_COPY128(mv_dst + 8 * 2, mv_src + 2 * b_stride); AV_COPY128(mv_dst + 8 * 3, mv_src + 3 * b_stride); } }
['static av_always_inline void fill_filter_caches_inter(H264Context *h,\n int mb_type, int top_xy,\n int left_xy[LEFT_MBS],\n int top_type,\n int left_type[LEFT_MBS],\n int mb_xy, int list)\n{\n int b_stride = h->b_stride;\n int16_t(*mv_dst)[2] = &h->mv_cache[list][scan8[0]];\n int8_t *ref_cache = &h->ref_cache[list][scan8[0]];\n if (IS_INTER(mb_type) || IS_DIRECT(mb_type)) {\n if (USES_LIST(top_type, list)) {\n const int b_xy = h->mb2b_xy[top_xy] + 3 * b_stride;\n const int b8_xy = 4 * top_xy + 2;\n int (*ref2frm)[64] = h->ref2frm[h->slice_table[top_xy] & (MAX_SLICES - 1)][0] + (MB_MBAFF(h) ? 20 : 2);\n AV_COPY128(mv_dst - 1 * 8, h->cur_pic.motion_val[list][b_xy + 0]);\n ref_cache[0 - 1 * 8] =\n ref_cache[1 - 1 * 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 0]];\n ref_cache[2 - 1 * 8] =\n ref_cache[3 - 1 * 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 1]];\n } else {\n AV_ZERO128(mv_dst - 1 * 8);\n AV_WN32A(&ref_cache[0 - 1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);\n }\n if (!IS_INTERLACED(mb_type ^ left_type[LTOP])) {\n if (USES_LIST(left_type[LTOP], list)) {\n const int b_xy = h->mb2b_xy[left_xy[LTOP]] + 3;\n const int b8_xy = 4 * left_xy[LTOP] + 1;\n int (*ref2frm)[64] = h->ref2frm[h->slice_table[left_xy[LTOP]] & (MAX_SLICES - 1)][0] + (MB_MBAFF(h) ? 20 : 2);\n AV_COPY32(mv_dst - 1 + 0, h->cur_pic.motion_val[list][b_xy + b_stride * 0]);\n AV_COPY32(mv_dst - 1 + 8, h->cur_pic.motion_val[list][b_xy + b_stride * 1]);\n AV_COPY32(mv_dst - 1 + 16, h->cur_pic.motion_val[list][b_xy + b_stride * 2]);\n AV_COPY32(mv_dst - 1 + 24, h->cur_pic.motion_val[list][b_xy + b_stride * 3]);\n ref_cache[-1 + 0] =\n ref_cache[-1 + 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 2 * 0]];\n ref_cache[-1 + 16] =\n ref_cache[-1 + 24] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 2 * 1]];\n } else {\n AV_ZERO32(mv_dst - 1 + 0);\n AV_ZERO32(mv_dst - 1 + 8);\n AV_ZERO32(mv_dst - 1 + 16);\n AV_ZERO32(mv_dst - 1 + 24);\n ref_cache[-1 + 0] =\n ref_cache[-1 + 8] =\n ref_cache[-1 + 16] =\n ref_cache[-1 + 24] = LIST_NOT_USED;\n }\n }\n }\n if (!USES_LIST(mb_type, list)) {\n fill_rectangle(mv_dst, 4, 4, 8, pack16to32(0, 0), 4);\n AV_WN32A(&ref_cache[0 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);\n AV_WN32A(&ref_cache[1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);\n AV_WN32A(&ref_cache[2 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);\n AV_WN32A(&ref_cache[3 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);\n return;\n }\n {\n int8_t *ref = &h->cur_pic.ref_index[list][4 * mb_xy];\n int (*ref2frm)[64] = h->ref2frm[h->slice_num & (MAX_SLICES - 1)][0] + (MB_MBAFF(h) ? 20 : 2);\n uint32_t ref01 = (pack16to32(ref2frm[list][ref[0]], ref2frm[list][ref[1]]) & 0x00FF00FF) * 0x0101;\n uint32_t ref23 = (pack16to32(ref2frm[list][ref[2]], ref2frm[list][ref[3]]) & 0x00FF00FF) * 0x0101;\n AV_WN32A(&ref_cache[0 * 8], ref01);\n AV_WN32A(&ref_cache[1 * 8], ref01);\n AV_WN32A(&ref_cache[2 * 8], ref23);\n AV_WN32A(&ref_cache[3 * 8], ref23);\n }\n {\n int16_t(*mv_src)[2] = &h->cur_pic.motion_val[list][4 * h->mb_x + 4 * h->mb_y * b_stride];\n AV_COPY128(mv_dst + 8 * 0, mv_src + 0 * b_stride);\n AV_COPY128(mv_dst + 8 * 1, mv_src + 1 * b_stride);\n AV_COPY128(mv_dst + 8 * 2, mv_src + 2 * b_stride);\n AV_COPY128(mv_dst + 8 * 3, mv_src + 3 * b_stride);\n }\n}']
32
0
https://github.com/openssl/openssl/blob/5dfc369ffcdc4722482c818e6ba6cf6e704c2cb5/crypto/asn1/a_bytes.c/#L161
int i2d_ASN1_bytes(ASN1_STRING *a, unsigned char **pp, int tag, int xclass) { int ret,r,constructed; unsigned char *p; if (a == NULL) return(0); if (tag == V_ASN1_BIT_STRING) return(i2d_ASN1_BIT_STRING(a,pp)); ret=a->length; r=ASN1_object_size(0,ret,tag); if (pp == NULL) return(r); p= *pp; if ((tag == V_ASN1_SEQUENCE) || (tag == V_ASN1_SET)) constructed=1; else constructed=0; ASN1_put_object(&p,constructed,ret,tag,xclass); memcpy(p,a->data,a->length); p+=a->length; *pp= p; return(r); }
['PKCS12 *PKCS12_create(char *pass, char *name, EVP_PKEY *pkey, X509 *cert,\n\t STACK *ca, int nid_key, int nid_cert, int iter, int mac_iter,\n\t int keytype)\n{\n\tPKCS12 *p12;\n\tSTACK *bags, *safes;\n\tPKCS12_SAFEBAG *bag;\n\tPKCS8_PRIV_KEY_INFO *p8;\n\tPKCS7 *authsafe;\n\tX509 *tcert;\n\tint i;\n\tunsigned char keyid[EVP_MAX_MD_SIZE];\n\tunsigned int keyidlen;\n\tif(!nid_cert) nid_cert = NID_pbe_WithSHA1And40BitRC2_CBC;\n\tif(!nid_key) nid_key = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;\n\tif(!iter) iter = 1000;\n\tif(!mac_iter) mac_iter = 1;\n\tif(!pkey || !cert) {\n\t\tPKCS12err(PKCS12_F_PKCS12_CREATE,PKCS12_R_INVALID_NULL_ARGUMENT);\n\t\treturn NULL;\n\t}\n\tif(!(bags = sk_new (NULL))) {\n\t\tPKCS12err(PKCS12_F_PKCS12_CREATE,ERR_R_MALLOC_FAILURE);\n\t\treturn NULL;\n\t}\n\tif(!(bag = M_PKCS12_x5092certbag(cert))) return NULL;\n\tif(name && !PKCS12_add_friendlyname(bag, name, -1)) return NULL;\n\tX509_digest(cert, EVP_sha1(), keyid, &keyidlen);\n\tif(!PKCS12_add_localkeyid(bag, keyid, keyidlen)) return NULL;\n\tif(!sk_push(bags, (char *)bag)) {\n\t\tPKCS12err(PKCS12_F_PKCS12_CREATE,ERR_R_MALLOC_FAILURE);\n\t\treturn NULL;\n\t}\n\tif(ca) {\n\t\tfor(i = 0; i < sk_num(ca); i++) {\n\t\t\ttcert = (X509 *)sk_value(ca, i);\n\t\t\tif(!(bag = M_PKCS12_x5092certbag(tcert))) return NULL;\n\t\t\tif(!sk_push(bags, (char *)bag)) {\n\t\t\t\tPKCS12err(PKCS12_F_PKCS12_CREATE,ERR_R_MALLOC_FAILURE);\n\t\t\t\treturn NULL;\n\t\t\t}\n\t\t}\n\t}\n\tauthsafe = PKCS12_pack_p7encdata (nid_cert, pass, -1, NULL, 0,\n\t\t\t\t\t iter, bags);\n\tsk_pop_free(bags, PKCS12_SAFEBAG_free);\n\tif (!authsafe) return NULL;\n\tif(!(safes = sk_new (NULL)) || !sk_push(safes, (char *)authsafe)) {\n\t\tPKCS12err(PKCS12_F_PKCS12_CREATE,ERR_R_MALLOC_FAILURE);\n\t\treturn NULL;\n\t}\n\tif(!(p8 = EVP_PKEY2PKCS8 (pkey))) return NULL;\n\tif(keytype && !PKCS8_add_keyusage(p8, keytype)) return NULL;\n\tbag = PKCS12_MAKE_SHKEYBAG (nid_key, pass, -1, NULL, 0, iter, p8);\n\tif(!bag) return NULL;\n\tPKCS8_PRIV_KEY_INFO_free(p8);\n if (name && !PKCS12_add_friendlyname (bag, name, -1)) return NULL;\n\tif(!PKCS12_add_localkeyid (bag, keyid, keyidlen)) return NULL;\n\tif(!(bags = sk_new(NULL)) || !sk_push (bags, (char *)bag)) {\n\t\tPKCS12err(PKCS12_F_PKCS12_CREATE,ERR_R_MALLOC_FAILURE);\n\t\treturn NULL;\n\t}\n\tif(!(authsafe = PKCS12_pack_p7data (bags))) return NULL;\n\tsk_pop_free(bags, PKCS12_SAFEBAG_free);\n\tif(!sk_push(safes, (char *)authsafe)) {\n\t\tPKCS12err(PKCS12_F_PKCS12_CREATE,ERR_R_MALLOC_FAILURE);\n\t\treturn NULL;\n\t}\n\tif(!(p12 = PKCS12_init (NID_pkcs7_data))) return NULL;\n\tif(!M_PKCS12_pack_authsafes (p12, safes)) return NULL;\n\tsk_pop_free(safes, PKCS7_free);\n\tif(!PKCS12_set_mac (p12, pass, -1, NULL, 0, mac_iter, NULL))\n\t return NULL;\n\treturn p12;\n}', 'PKCS7 *PKCS12_pack_p7encdata (int pbe_nid, const char *pass, int passlen,\n\t unsigned char *salt, int saltlen, int iter, STACK *bags)\n{\n\tPKCS7 *p7;\n\tX509_ALGOR *pbe;\n\tif (!(p7 = PKCS7_new())) {\n\t\tPKCS12err(PKCS12_F_PKCS12_PACK_P7ENCDATA, ERR_R_MALLOC_FAILURE);\n\t\treturn NULL;\n\t}\n\tp7->type = OBJ_nid2obj(NID_pkcs7_encrypted);\n\tif (!(p7->d.encrypted = PKCS7_ENCRYPT_new ())) {\n\t\tPKCS12err(PKCS12_F_PKCS12_PACK_P7ENCDATA, ERR_R_MALLOC_FAILURE);\n\t\treturn NULL;\n\t}\n\tASN1_INTEGER_set (p7->d.encrypted->version, 0);\n\tp7->d.encrypted->enc_data->content_type = OBJ_nid2obj(NID_pkcs7_data);\n\tif (!(pbe = PKCS5_pbe_set (pbe_nid, iter, salt, saltlen))) {\n\t\tPKCS12err(PKCS12_F_PKCS12_PACK_P7ENCDATA, ERR_R_MALLOC_FAILURE);\n\t\treturn NULL;\n\t}\n\tX509_ALGOR_free(p7->d.encrypted->enc_data->algorithm);\n\tp7->d.encrypted->enc_data->algorithm = pbe;\n\tASN1_OCTET_STRING_free(p7->d.encrypted->enc_data->enc_data);\n\tif (!(p7->d.encrypted->enc_data->enc_data =\n\tPKCS12_i2d_encrypt (pbe, i2d_PKCS12_SAFEBAG, pass, passlen,\n\t\t\t\t (char *)bags, 1))) {\n\t\tPKCS12err(PKCS12_F_PKCS12_PACK_P7ENCDATA, PKCS12_R_ENCRYPT_ERROR);\n\t\treturn NULL;\n\t}\n\treturn p7;\n}', 'X509_ALGOR *PKCS5_pbe_set(int alg, int iter, unsigned char *salt,\n\t int saltlen)\n{\n\tunsigned char *pdata, *ptmp;\n\tint plen;\n\tPBEPARAM *pbe;\n\tASN1_OBJECT *al;\n\tX509_ALGOR *algor;\n\tASN1_TYPE *astype;\n\tif (!(pbe = PBEPARAM_new ())) {\n\t\tASN1err(ASN1_F_ASN1_PBE_SET,ERR_R_MALLOC_FAILURE);\n\t\treturn NULL;\n\t}\n\tASN1_INTEGER_set (pbe->iter, iter);\n\tif (!saltlen) saltlen = PKCS5_SALT_LEN;\n\tif (!(pbe->salt->data = Malloc (saltlen))) {\n\t\tASN1err(ASN1_F_ASN1_PBE_SET,ERR_R_MALLOC_FAILURE);\n\t\treturn NULL;\n\t}\n\tpbe->salt->length = saltlen;\n\tif (salt) memcpy (pbe->salt->data, salt, saltlen);\n\telse RAND_bytes (pbe->salt->data, saltlen);\n\tif (!(plen = i2d_PBEPARAM (pbe, NULL))) {\n\t\tASN1err(ASN1_F_ASN1_PBE_SET,ASN1_R_ENCODE_ERROR);\n\t\treturn NULL;\n\t}\n\tif (!(pdata = Malloc (plen))) {\n\t\tASN1err(ASN1_F_ASN1_PBE_SET,ERR_R_MALLOC_FAILURE);\n\t\treturn NULL;\n\t}\n\tptmp = pdata;\n\ti2d_PBEPARAM (pbe, &ptmp);\n\tPBEPARAM_free (pbe);\n\tif (!(astype = ASN1_TYPE_new())) {\n\t\tASN1err(ASN1_F_ASN1_PBE_SET,ERR_R_MALLOC_FAILURE);\n\t\treturn NULL;\n\t}\n\tastype->type = V_ASN1_SEQUENCE;\n\tif (!(astype->value.sequence=ASN1_STRING_new())) {\n\t\tASN1err(ASN1_F_ASN1_PBE_SET,ERR_R_MALLOC_FAILURE);\n\t\treturn NULL;\n\t}\n\tASN1_STRING_set (astype->value.sequence, pdata, plen);\n\tFree (pdata);\n\tal = OBJ_nid2obj(alg);\n\tif (!(algor = X509_ALGOR_new())) {\n\t\tASN1err(ASN1_F_ASN1_PBE_SET,ERR_R_MALLOC_FAILURE);\n\t\treturn NULL;\n\t}\n\tASN1_OBJECT_free(algor->algorithm);\n\talgor->algorithm = al;\n\talgor->parameter = astype;\n\treturn (algor);\n}', 'int i2d_PBEPARAM(PBEPARAM *a, unsigned char **pp)\n{\n\tM_ASN1_I2D_vars(a);\n\tM_ASN1_I2D_len (a->salt, i2d_ASN1_OCTET_STRING);\n\tM_ASN1_I2D_len (a->iter, i2d_ASN1_INTEGER);\n\tM_ASN1_I2D_seq_total ();\n\tM_ASN1_I2D_put (a->salt, i2d_ASN1_OCTET_STRING);\n\tM_ASN1_I2D_put (a->iter, i2d_ASN1_INTEGER);\n\tM_ASN1_I2D_finish();\n}', 'int i2d_ASN1_OCTET_STRING(ASN1_OCTET_STRING *a, unsigned char **pp)\n\t{\n\treturn(i2d_ASN1_bytes((ASN1_STRING *)a,pp,\n\t\tV_ASN1_OCTET_STRING,V_ASN1_UNIVERSAL));\n\t}', 'int i2d_ASN1_bytes(ASN1_STRING *a, unsigned char **pp, int tag, int xclass)\n\t{\n\tint ret,r,constructed;\n\tunsigned char *p;\n\tif (a == NULL) return(0);\n\tif (tag == V_ASN1_BIT_STRING)\n\t\treturn(i2d_ASN1_BIT_STRING(a,pp));\n\tret=a->length;\n\tr=ASN1_object_size(0,ret,tag);\n\tif (pp == NULL) return(r);\n\tp= *pp;\n\tif ((tag == V_ASN1_SEQUENCE) || (tag == V_ASN1_SET))\n\t\tconstructed=1;\n\telse\n\t\tconstructed=0;\n\tASN1_put_object(&p,constructed,ret,tag,xclass);\n\tmemcpy(p,a->data,a->length);\n\tp+=a->length;\n\t*pp= p;\n\treturn(r);\n\t}', 'void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,\n\t int xclass)\n\t{\n\tunsigned char *p= *pp;\n\tint i;\n\ti=(constructed)?V_ASN1_CONSTRUCTED:0;\n\ti|=(xclass&V_ASN1_PRIVATE);\n\tif (tag < 31)\n\t\t*(p++)=i|(tag&V_ASN1_PRIMATIVE_TAG);\n\telse\n\t\t{\n\t\t*(p++)=i|V_ASN1_PRIMATIVE_TAG;\n\t\twhile (tag > 0x7f)\n\t\t\t{\n\t\t\t*(p++)=(tag&0x7f)|0x80;\n\t\t\ttag>>=7;\n\t\t\t}\n\t\t*(p++)=(tag&0x7f);\n\t\t}\n\tif ((constructed == 2) && (length == 0))\n\t\t*(p++)=0x80;\n\telse\n\t\tasn1_put_length(&p,length);\n\t*pp=p;\n\t}']
33
0
https://github.com/openssl/openssl/blob/b66411f6cda6970c01283ddde6d8063c57b3b7d9/crypto/bn/bn_print.c/#L213
int BN_dec2bn(BIGNUM **bn, const char *a) { BIGNUM *ret = NULL; BN_ULONG l = 0; int neg = 0, i, j; int num; if ((a == NULL) || (*a == '\0')) return (0); if (*a == '-') { neg = 1; a++; } for (i = 0; i <= (INT_MAX/4) && isdigit((unsigned char)a[i]); i++) continue; if (i == 0 || i > INT_MAX/4) goto err; num = i + neg; if (bn == NULL) return (num); if (*bn == NULL) { if ((ret = BN_new()) == NULL) return (0); } else { ret = *bn; BN_zero(ret); } if (bn_expand(ret, i * 4) == NULL) goto err; j = BN_DEC_NUM - (i % BN_DEC_NUM); if (j == BN_DEC_NUM) j = 0; l = 0; while (--i >= 0) { l *= 10; l += *a - '0'; a++; if (++j == BN_DEC_NUM) { if (!BN_mul_word(ret, BN_DEC_CONV) || !BN_add_word(ret, l)) goto err; l = 0; j = 0; } } 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 x9_62_tests()\n{\n int ret = 0;\n if (!change_rand())\n goto x962_err;\n if (!TEST_true(x9_62_test_internal(NID_X9_62_prime192v1,\n "3342403536405981729393488334694600415596881826869351677613",\n "5735822328888155254683894997897571951568553642892029982342")))\n goto x962_err;\n if (!TEST_true(x9_62_test_internal(NID_X9_62_prime239v1,\n "3086361431751678114926225473006680188549593787585317781474"\n "62058306432176",\n "3238135532097973577080787768312505059318910517550078427819"\n "78505179448783")))\n goto x962_err;\n# ifndef OPENSSL_NO_EC2M\n if (!TEST_true(x9_62_test_internal(NID_X9_62_c2tnb191v1,\n "87194383164871543355722284926904419997237591535066528048",\n "308992691965804947361541664549085895292153777025772063598")))\n goto x962_err;\n if (!TEST_true(x9_62_test_internal(NID_X9_62_c2tnb239v1,\n "2159633321041961198501834003903461262881815148684178964245"\n "5876922391552",\n "1970303740007316867383349976549972270528498040721988191026"\n "49413465737174")))\n goto x962_err;\n# endif\n ret = 1;\n x962_err:\n if (!TEST_true(restore_rand()))\n ret = 0;\n return ret;\n}', 'static int x9_62_test_internal(int nid, const char *r_in, const char *s_in)\n{\n int ret = 0;\n const char message[] = "abc";\n unsigned char digest[20];\n unsigned int dgst_len = 0;\n EVP_MD_CTX *md_ctx;\n EC_KEY *key = NULL;\n ECDSA_SIG *signature = NULL;\n BIGNUM *r = NULL, *s = NULL;\n BIGNUM *kinv = NULL, *rp = NULL;\n const BIGNUM *sig_r, *sig_s;\n if (!TEST_ptr(md_ctx = EVP_MD_CTX_new()))\n goto x962_int_err;\n if (!TEST_true(EVP_DigestInit(md_ctx, EVP_sha1()))\n || !TEST_true(EVP_DigestUpdate(md_ctx, (const void *)message, 3))\n || !TEST_true(EVP_DigestFinal(md_ctx, digest, &dgst_len)))\n goto x962_int_err;\n TEST_info("testing %s", OBJ_nid2sn(nid));\n if (!TEST_ptr(key = EC_KEY_new_by_curve_name(nid)))\n goto x962_int_err;\n use_fake = 1;\n if (!TEST_true(EC_KEY_generate_key(key)))\n goto x962_int_err;\n use_fake = 1;\n if (!TEST_true(ECDSA_sign_setup(key, NULL, &kinv, &rp)))\n goto x962_int_err;\n if (!TEST_ptr(signature = ECDSA_do_sign_ex(digest, 20, kinv, rp, key)))\n goto x962_int_err;\n if (!TEST_ptr(r = BN_new()) || !TEST_ptr(s = BN_new()))\n goto x962_int_err;\n if (!TEST_true(BN_dec2bn(&r, r_in)) || !TEST_true(BN_dec2bn(&s, s_in)))\n goto x962_int_err;\n ECDSA_SIG_get0(signature, &sig_r, &sig_s);\n if (!TEST_int_eq(BN_cmp(sig_r, r), 0)\n || !TEST_int_eq(BN_cmp(sig_s, s), 0))\n goto x962_int_err;\n if (!TEST_int_eq(ECDSA_do_verify(digest, 20, signature, key), 1))\n goto x962_int_err;\n ret = 1;\n x962_int_err:\n EC_KEY_free(key);\n ECDSA_SIG_free(signature);\n BN_free(r);\n BN_free(s);\n EVP_MD_CTX_free(md_ctx);\n BN_clear_free(kinv);\n BN_clear_free(rp);\n return ret;\n}', "int BN_dec2bn(BIGNUM **bn, const char *a)\n{\n BIGNUM *ret = NULL;\n BN_ULONG l = 0;\n int neg = 0, i, j;\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) && isdigit((unsigned char)a[i]); i++)\n continue;\n if (i == 0 || i > INT_MAX/4)\n goto err;\n num = i + neg;\n if (bn == NULL)\n return (num);\n if (*bn == NULL) {\n if ((ret = BN_new()) == NULL)\n return (0);\n } else {\n ret = *bn;\n BN_zero(ret);\n }\n if (bn_expand(ret, i * 4) == NULL)\n goto err;\n j = BN_DEC_NUM - (i % BN_DEC_NUM);\n if (j == BN_DEC_NUM)\n j = 0;\n l = 0;\n while (--i >= 0) {\n l *= 10;\n l += *a - '0';\n a++;\n if (++j == BN_DEC_NUM) {\n if (!BN_mul_word(ret, BN_DEC_CONV)\n || !BN_add_word(ret, l))\n goto err;\n l = 0;\n j = 0;\n }\n }\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}"]
34
0
https://github.com/openssl/openssl/blob/c10d1bc81cb047cbd53f8cc430632b6a4a70252d/test/evp_test.c/#L1685
static int encode_test_run(struct evp_test *t) { struct encode_data *edata = t->data; unsigned char *encode_out = NULL, *decode_out = NULL; int output_len, chunk_len; const char *err = "INTERNAL_ERROR"; EVP_ENCODE_CTX *decode_ctx = EVP_ENCODE_CTX_new(); if (decode_ctx == NULL) goto err; if (edata->encoding == BASE64_CANONICAL_ENCODING) { EVP_ENCODE_CTX *encode_ctx = EVP_ENCODE_CTX_new(); if (encode_ctx == NULL) goto err; encode_out = OPENSSL_malloc(EVP_ENCODE_LENGTH(edata->input_len)); if (encode_out == NULL) goto err; EVP_EncodeInit(encode_ctx); EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len, edata->input, edata->input_len); output_len = chunk_len; EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len); output_len += chunk_len; EVP_ENCODE_CTX_free(encode_ctx); if (check_var_length_output(t, edata->output, edata->output_len, encode_out, output_len)) { err = "BAD_ENCODING"; goto err; } } decode_out = OPENSSL_malloc(EVP_DECODE_LENGTH(edata->output_len)); if (decode_out == NULL) goto err; EVP_DecodeInit(decode_ctx); if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, edata->output, edata->output_len) < 0) { err = "DECODE_ERROR"; goto err; } output_len = chunk_len; if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) { err = "DECODE_ERROR"; goto err; } output_len += chunk_len; if (edata->encoding != BASE64_INVALID_ENCODING && check_var_length_output(t, edata->input, edata->input_len, decode_out, output_len)) { err = "BAD_DECODING"; goto err; } err = NULL; err: t->err = err; OPENSSL_free(encode_out); OPENSSL_free(decode_out); EVP_ENCODE_CTX_free(decode_ctx); return 1; }
['static int encode_test_run(struct evp_test *t)\n{\n struct encode_data *edata = t->data;\n unsigned char *encode_out = NULL, *decode_out = NULL;\n int output_len, chunk_len;\n const char *err = "INTERNAL_ERROR";\n EVP_ENCODE_CTX *decode_ctx = EVP_ENCODE_CTX_new();\n if (decode_ctx == NULL)\n goto err;\n if (edata->encoding == BASE64_CANONICAL_ENCODING) {\n EVP_ENCODE_CTX *encode_ctx = EVP_ENCODE_CTX_new();\n if (encode_ctx == NULL)\n goto err;\n encode_out = OPENSSL_malloc(EVP_ENCODE_LENGTH(edata->input_len));\n if (encode_out == NULL)\n goto err;\n EVP_EncodeInit(encode_ctx);\n EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,\n edata->input, edata->input_len);\n output_len = chunk_len;\n EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);\n output_len += chunk_len;\n EVP_ENCODE_CTX_free(encode_ctx);\n if (check_var_length_output(t, edata->output, edata->output_len,\n encode_out, output_len)) {\n err = "BAD_ENCODING";\n goto err;\n }\n }\n decode_out = OPENSSL_malloc(EVP_DECODE_LENGTH(edata->output_len));\n if (decode_out == NULL)\n goto err;\n EVP_DecodeInit(decode_ctx);\n if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, edata->output,\n edata->output_len) < 0) {\n err = "DECODE_ERROR";\n goto err;\n }\n output_len = chunk_len;\n if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {\n err = "DECODE_ERROR";\n goto err;\n }\n output_len += chunk_len;\n if (edata->encoding != BASE64_INVALID_ENCODING &&\n check_var_length_output(t, edata->input, edata->input_len,\n decode_out, output_len)) {\n err = "BAD_DECODING";\n goto err;\n }\n err = NULL;\n err:\n t->err = err;\n OPENSSL_free(encode_out);\n OPENSSL_free(decode_out);\n EVP_ENCODE_CTX_free(decode_ctx);\n return 1;\n}', 'EVP_ENCODE_CTX *EVP_ENCODE_CTX_new(void)\n{\n return (EVP_ENCODE_CTX *)OPENSSL_zalloc(sizeof(EVP_ENCODE_CTX));\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (num <= 0)\n return NULL;\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)file;\n (void)line;\n ret = malloc(num);\n#endif\n#ifndef OPENSSL_CPUID_OBJ\n if (ret && (num > 2048)) {\n extern unsigned char cleanse_ctr;\n ((unsigned char *)ret)[0] = cleanse_ctr;\n }\n#endif\n return ret;\n}']
35
0
https://github.com/openssl/openssl/blob/f9df0a7775f483c175cda5832360cccd1db6943a/crypto/bn/bn_ctx.c/#L342
static void BN_POOL_release(BN_POOL *p, unsigned int num) { unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE; p->used -= num; while (num--) { bn_check_top(p->current->vals + offset); if (offset == 0) { offset = BN_CTX_POOL_SIZE - 1; p->current = p->current->prev; } else offset--; } }
['int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,\n BN_CTX *ctx)\n{\n BIGNUM *t;\n int ret = 0;\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(m);\n BN_CTX_start(ctx);\n if ((t = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (a == b) {\n if (!BN_sqr(t, a, ctx))\n goto err;\n } else {\n if (!BN_mul(t, a, b, ctx))\n goto err;\n }\n if (!BN_nnmod(r, t, m, ctx))\n goto err;\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return (ret);\n}', 'int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int max, al;\n int ret = 0;\n BIGNUM *tmp, *rr;\n bn_check_top(a);\n al = a->top;\n if (al <= 0) {\n r->top = 0;\n r->neg = 0;\n return 1;\n }\n BN_CTX_start(ctx);\n rr = (a != r) ? r : BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (rr == NULL || tmp == NULL)\n goto err;\n max = 2 * al;\n if (bn_wexpand(rr, max) == NULL)\n goto err;\n if (al == 4) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[8];\n bn_sqr_normal(rr->d, a->d, 4, t);\n#else\n bn_sqr_comba4(rr->d, a->d);\n#endif\n } else if (al == 8) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[16];\n bn_sqr_normal(rr->d, a->d, 8, t);\n#else\n bn_sqr_comba8(rr->d, a->d);\n#endif\n } else {\n#if defined(BN_RECURSION)\n if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {\n BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];\n bn_sqr_normal(rr->d, a->d, al, t);\n } else {\n int j, k;\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n if (al == j) {\n if (bn_wexpand(tmp, k * 2) == NULL)\n goto err;\n bn_sqr_recursive(rr->d, a->d, al, tmp->d);\n } else {\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n }\n }\n#else\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n#endif\n }\n rr->neg = 0;\n if (a->d[al - 1] == (a->d[al - 1] & BN_MASK2l))\n rr->top = max - 1;\n else\n rr->top = max;\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(rr);\n bn_check_top(tmp);\n BN_CTX_end(ctx);\n return (ret);\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return (0);\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return (0);\n }\n if (dv != NULL)\n BN_zero(dv);\n return 1;\n }\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n resp++;\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n resp--;\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return (0);\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'static void BN_POOL_release(BN_POOL *p, unsigned int num)\n{\n unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE;\n p->used -= num;\n while (num--) {\n bn_check_top(p->current->vals + offset);\n if (offset == 0) {\n offset = BN_CTX_POOL_SIZE - 1;\n p->current = p->current->prev;\n } else\n offset--;\n }\n}']
36
0
https://github.com/openssl/openssl/blob/9b02dc97e4963969da69675a871dbe80e6d31cda/include/internal/constant_time_locl.h/#L159
static ossl_inline unsigned int constant_time_is_zero(unsigned int a) { return constant_time_msb(~a & (a - 1)); }
['static int test_is_zero_8(unsigned int a)\n{\n if (a == 0 && !TEST_uint_eq(constant_time_is_zero_8(a), CONSTTIME_TRUE_8))\n return 0;\n if (a != 0 && !TEST_uint_eq(constant_time_is_zero_8(a), CONSTTIME_FALSE_8))\n return 0;\n return 1;\n}', 'static ossl_inline unsigned char constant_time_is_zero_8(unsigned int a)\n{\n return (unsigned char)constant_time_is_zero(a);\n}', 'static ossl_inline unsigned int constant_time_is_zero(unsigned int a)\n{\n return constant_time_msb(~a & (a - 1));\n}']
37
0
https://github.com/openssl/openssl/blob/f586d97191ad9821faea026df68aceaba45d1800/crypto/bn/bn_ctx.c/#L442
static void BN_POOL_release(BN_POOL *p, unsigned int num) { unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE; p->used -= num; while(num--) { bn_check_top(p->current->vals + offset); if(!offset) { offset = BN_CTX_POOL_SIZE - 1; p->current = p->current->prev; } else offset--; } }
['BN_BLINDING *RSA_setup_blinding(RSA *rsa, BN_CTX *in_ctx)\n{\n\tBIGNUM local_n;\n\tBIGNUM *e,*n;\n\tBN_CTX *ctx;\n\tBN_BLINDING *ret = NULL;\n\tif (in_ctx == NULL)\n\t\t{\n\t\tif ((ctx = BN_CTX_new()) == NULL) return 0;\n\t\t}\n\telse\n\t\tctx = in_ctx;\n\tBN_CTX_start(ctx);\n\te = BN_CTX_get(ctx);\n\tif (e == NULL)\n\t\t{\n\t\tRSAerr(RSA_F_RSA_SETUP_BLINDING, ERR_R_MALLOC_FAILURE);\n\t\tgoto err;\n\t\t}\n\tif (rsa->e == NULL)\n\t\t{\n\t\te = rsa_get_public_exp(rsa->d, rsa->p, rsa->q, ctx);\n\t\tif (e == NULL)\n\t\t\t{\n\t\t\tRSAerr(RSA_F_RSA_SETUP_BLINDING, RSA_R_NO_PUBLIC_EXPONENT);\n\t\t\tgoto err;\n\t\t\t}\n\t\t}\n\telse\n\t\te = rsa->e;\n\tif ((RAND_status() == 0) && rsa->d != NULL && rsa->d->d != NULL)\n\t\t{\n\t\tRAND_add(rsa->d->d, rsa->d->dmax * sizeof rsa->d->d[0], 0.0);\n\t\t}\n\tif (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))\n\t\t{\n\t\tn = &local_n;\n\t\tBN_with_flags(n, rsa->n, BN_FLG_CONSTTIME);\n\t\t}\n\telse\n\t\tn = rsa->n;\n\tret = BN_BLINDING_create_param(NULL, e, n, ctx,\n\t\t\trsa->meth->bn_mod_exp, rsa->_method_mod_n);\n\tif (ret == NULL)\n\t\t{\n\t\tRSAerr(RSA_F_RSA_SETUP_BLINDING, ERR_R_BN_LIB);\n\t\tgoto err;\n\t\t}\n\tCRYPTO_THREADID_current(BN_BLINDING_thread_id(ret));\nerr:\n\tBN_CTX_end(ctx);\n\tif (in_ctx == NULL)\n\t\tBN_CTX_free(ctx);\n\tif(rsa->e == NULL)\n\t\tBN_free(e);\n\treturn ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n\t{\n\tBIGNUM *ret;\n\tCTXDBG_ENTRY("BN_CTX_get", ctx);\n\tif(ctx->err_stack || ctx->too_many) return NULL;\n\tif((ret = BN_POOL_get(&ctx->pool)) == NULL)\n\t\t{\n\t\tctx->too_many = 1;\n\t\tBNerr(BN_F_BN_CTX_GET,BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n\t\treturn NULL;\n\t\t}\n\tBN_zero(ret);\n\tctx->used++;\n\tCTXDBG_RET(ctx, ret);\n\treturn ret;\n\t}', 'static BIGNUM *rsa_get_public_exp(const BIGNUM *d, const BIGNUM *p,\n\tconst BIGNUM *q, BN_CTX *ctx)\n{\n\tBIGNUM *ret = NULL, *r0, *r1, *r2;\n\tif (d == NULL || p == NULL || q == NULL)\n\t\treturn NULL;\n\tBN_CTX_start(ctx);\n\tr0 = BN_CTX_get(ctx);\n\tr1 = BN_CTX_get(ctx);\n\tr2 = BN_CTX_get(ctx);\n\tif (r2 == NULL)\n\t\tgoto err;\n\tif (!BN_sub(r1, p, BN_value_one())) goto err;\n\tif (!BN_sub(r2, q, BN_value_one())) goto err;\n\tif (!BN_mul(r0, r1, r2, ctx)) goto err;\n\tret = BN_mod_inverse(NULL, d, r0, ctx);\nerr:\n\tBN_CTX_end(ctx);\n\treturn ret;\n}', 'int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n\t{\n\tint ret=0;\n\tint top,al,bl;\n\tBIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\tint i;\n#endif\n#ifdef BN_RECURSION\n\tBIGNUM *t=NULL;\n\tint j=0,k;\n#endif\n#ifdef BN_COUNT\n\tfprintf(stderr,"BN_mul %d * %d\\n",a->top,b->top);\n#endif\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tbn_check_top(r);\n\tal=a->top;\n\tbl=b->top;\n\tif ((al == 0) || (bl == 0))\n\t\t{\n\t\tBN_zero(r);\n\t\treturn(1);\n\t\t}\n\ttop=al+bl;\n\tBN_CTX_start(ctx);\n\tif ((r == a) || (r == b))\n\t\t{\n\t\tif ((rr = BN_CTX_get(ctx)) == NULL) goto err;\n\t\t}\n\telse\n\t\trr = r;\n\trr->neg=a->neg^b->neg;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\ti = al-bl;\n#endif\n#ifdef BN_MUL_COMBA\n\tif (i == 0)\n\t\t{\n# if 0\n\t\tif (al == 4)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,8) == NULL) goto err;\n\t\t\trr->top=8;\n\t\t\tbn_mul_comba4(rr->d,a->d,b->d);\n\t\t\tgoto end;\n\t\t\t}\n# endif\n\t\tif (al == 8)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,16) == NULL) goto err;\n\t\t\trr->top=16;\n\t\t\tbn_mul_comba8(rr->d,a->d,b->d);\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n#endif\n#ifdef BN_RECURSION\n\tif ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL))\n\t\t{\n\t\tif (i >= -1 && i <= 1)\n\t\t\t{\n\t\t\tif (i >= 0)\n\t\t\t\t{\n\t\t\t\tj = BN_num_bits_word((BN_ULONG)al);\n\t\t\t\t}\n\t\t\tif (i == -1)\n\t\t\t\t{\n\t\t\t\tj = BN_num_bits_word((BN_ULONG)bl);\n\t\t\t\t}\n\t\t\tj = 1<<(j-1);\n\t\t\tassert(j <= al || j <= bl);\n\t\t\tk = j+j;\n\t\t\tt = BN_CTX_get(ctx);\n\t\t\tif (t == NULL)\n\t\t\t\tgoto err;\n\t\t\tif (al > j || bl > j)\n\t\t\t\t{\n\t\t\t\tif (bn_wexpand(t,k*4) == NULL) goto err;\n\t\t\t\tif (bn_wexpand(rr,k*4) == NULL) goto err;\n\t\t\t\tbn_mul_part_recursive(rr->d,a->d,b->d,\n\t\t\t\t\tj,al-j,bl-j,t->d);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (bn_wexpand(t,k*2) == NULL) goto err;\n\t\t\t\tif (bn_wexpand(rr,k*2) == NULL) goto err;\n\t\t\t\tbn_mul_recursive(rr->d,a->d,b->d,\n\t\t\t\t\tj,al-j,bl-j,t->d);\n\t\t\t\t}\n\t\t\trr->top=top;\n\t\t\tgoto end;\n\t\t\t}\n#if 0\n\t\tif (i == 1 && !BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tBIGNUM *tmp_bn = (BIGNUM *)b;\n\t\t\tif (bn_wexpand(tmp_bn,al) == NULL) goto err;\n\t\t\ttmp_bn->d[bl]=0;\n\t\t\tbl++;\n\t\t\ti--;\n\t\t\t}\n\t\telse if (i == -1 && !BN_get_flags(a,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tBIGNUM *tmp_bn = (BIGNUM *)a;\n\t\t\tif (bn_wexpand(tmp_bn,bl) == NULL) goto err;\n\t\t\ttmp_bn->d[al]=0;\n\t\t\tal++;\n\t\t\ti++;\n\t\t\t}\n\t\tif (i == 0)\n\t\t\t{\n\t\t\tj=BN_num_bits_word((BN_ULONG)al);\n\t\t\tj=1<<(j-1);\n\t\t\tk=j+j;\n\t\t\tt = BN_CTX_get(ctx);\n\t\t\tif (al == j)\n\t\t\t\t{\n\t\t\t\tif (bn_wexpand(t,k*2) == NULL) goto err;\n\t\t\t\tif (bn_wexpand(rr,k*2) == NULL) goto err;\n\t\t\t\tbn_mul_recursive(rr->d,a->d,b->d,al,t->d);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (bn_wexpand(t,k*4) == NULL) goto err;\n\t\t\t\tif (bn_wexpand(rr,k*4) == NULL) goto err;\n\t\t\t\tbn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d);\n\t\t\t\t}\n\t\t\trr->top=top;\n\t\t\tgoto end;\n\t\t\t}\n#endif\n\t\t}\n#endif\n\tif (bn_wexpand(rr,top) == NULL) goto err;\n\trr->top=top;\n\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\nend:\n#endif\n\tbn_correct_top(rr);\n\tif (r != rr) BN_copy(r,rr);\n\tret=1;\nerr:\n\tbn_check_top(r);\n\tBN_CTX_end(ctx);\n\treturn(ret);\n\t}', 'void BN_CTX_end(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_end", ctx);\n\tif(ctx->err_stack)\n\t\tctx->err_stack--;\n\telse\n\t\t{\n\t\tunsigned int fp = BN_STACK_pop(&ctx->stack);\n\t\tif(fp < ctx->used)\n\t\t\tBN_POOL_release(&ctx->pool, ctx->used - fp);\n\t\tctx->used = fp;\n\t\tctx->too_many = 0;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'static void BN_POOL_release(BN_POOL *p, unsigned int num)\n\t{\n\tunsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE;\n\tp->used -= num;\n\twhile(num--)\n\t\t{\n\t\tbn_check_top(p->current->vals + offset);\n\t\tif(!offset)\n\t\t\t{\n\t\t\toffset = BN_CTX_POOL_SIZE - 1;\n\t\t\tp->current = p->current->prev;\n\t\t\t}\n\t\telse\n\t\t\toffset--;\n\t\t}\n\t}']
38
0
https://github.com/openssl/openssl/blob/0ea155fc1c4e6ba3655f435164ea3f884883df29/crypto/init.c/#L370
int ossl_init_thread_start(uint64_t opts) { struct thread_local_inits_st *locals; if (!OPENSSL_init_crypto(0, NULL)) return 0; locals = ossl_init_get_thread_local(1); if (locals == NULL) return 0; if (opts & OPENSSL_INIT_THREAD_ASYNC) { #ifdef OPENSSL_INIT_DEBUG fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: " "marking thread for async\n"); #endif locals->async = 1; } if (opts & OPENSSL_INIT_THREAD_ERR_STATE) { #ifdef OPENSSL_INIT_DEBUG fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: " "marking thread for err_state\n"); #endif locals->err_state = 1; } return 1; }
['int ossl_init_thread_start(uint64_t opts)\n{\n struct thread_local_inits_st *locals;\n if (!OPENSSL_init_crypto(0, NULL))\n return 0;\n locals = ossl_init_get_thread_local(1);\n if (locals == NULL)\n return 0;\n if (opts & OPENSSL_INIT_THREAD_ASYNC) {\n#ifdef OPENSSL_INIT_DEBUG\n fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "\n "marking thread for async\\n");\n#endif\n locals->async = 1;\n }\n if (opts & OPENSSL_INIT_THREAD_ERR_STATE) {\n#ifdef OPENSSL_INIT_DEBUG\n fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "\n "marking thread for err_state\\n");\n#endif\n locals->err_state = 1;\n }\n return 1;\n}', 'static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)\n{\n struct thread_local_inits_st *local =\n CRYPTO_THREAD_get_local(&threadstopkey);\n if (local == NULL && alloc) {\n local = OPENSSL_zalloc(sizeof *local);\n if (local != NULL && !CRYPTO_THREAD_set_local(&threadstopkey, local)) {\n OPENSSL_free(local);\n return NULL;\n }\n }\n if (!alloc) {\n CRYPTO_THREAD_set_local(&threadstopkey, NULL);\n }\n return local;\n}', 'void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)\n{\n return pthread_getspecific(*key);\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n osslargused(file); osslargused(line);\n ret = malloc(num);\n#endif\n return ret;\n}', 'int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)\n{\n if (pthread_setspecific(*key, val) != 0)\n return 0;\n return 1;\n}']
39
1
https://github.com/libav/libav/blob/e754dfc0bba4f81fe797f240fca94fea5dfd925e/libavcodec/ac3enc.c/#L2553
static av_cold void set_bandwidth(AC3EncodeContext *s) { int blk, ch; int av_uninit(cpl_start); if (s->cutoff) { int fbw_coeffs; fbw_coeffs = s->cutoff * 2 * AC3_MAX_COEFS / s->sample_rate; s->bandwidth_code = av_clip((fbw_coeffs - 73) / 3, 0, 60); } else { s->bandwidth_code = ac3_bandwidth_tab[s->fbw_channels-1][s->bit_alloc.sr_code][s->frame_size_code/2]; } for (ch = 1; ch <= s->fbw_channels; ch++) { s->start_freq[ch] = 0; for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) s->blocks[blk].end_freq[ch] = s->bandwidth_code * 3 + 73; } if (s->lfe_on) { s->start_freq[s->lfe_channel] = 0; for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) s->blocks[blk].end_freq[ch] = 7; } if (s->cpl_enabled) { if (s->options.cpl_start >= 0) { cpl_start = s->options.cpl_start; } else { cpl_start = ac3_coupling_start_tab[s->channel_mode-2][s->bit_alloc.sr_code][s->frame_size_code/2]; if (cpl_start < 0) s->cpl_enabled = 0; } } if (s->cpl_enabled) { int i, cpl_start_band, cpl_end_band; uint8_t *cpl_band_sizes = s->cpl_band_sizes; cpl_end_band = s->bandwidth_code / 4 + 3; cpl_start_band = av_clip(cpl_start, 0, FFMIN(cpl_end_band-1, 15)); s->num_cpl_subbands = cpl_end_band - cpl_start_band; s->num_cpl_bands = 1; *cpl_band_sizes = 12; for (i = cpl_start_band + 1; i < cpl_end_band; i++) { if (ff_eac3_default_cpl_band_struct[i]) { *cpl_band_sizes += 12; } else { s->num_cpl_bands++; cpl_band_sizes++; *cpl_band_sizes = 12; } } s->start_freq[CPL_CH] = cpl_start_band * 12 + 37; s->cpl_end_freq = cpl_end_band * 12 + 37; for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) s->blocks[blk].end_freq[CPL_CH] = s->cpl_end_freq; } }
['static av_cold int ac3_encode_init(AVCodecContext *avctx)\n{\n AC3EncodeContext *s = avctx->priv_data;\n int ret, frame_size_58;\n s->eac3 = avctx->codec_id == CODEC_ID_EAC3;\n avctx->frame_size = AC3_FRAME_SIZE;\n ff_ac3_common_init();\n ret = validate_options(avctx, s);\n if (ret)\n return ret;\n s->bitstream_mode = avctx->audio_service_type;\n if (s->bitstream_mode == AV_AUDIO_SERVICE_TYPE_KARAOKE)\n s->bitstream_mode = 0x7;\n s->bits_written = 0;\n s->samples_written = 0;\n frame_size_58 = (( s->frame_size >> 2) + ( s->frame_size >> 4)) << 1;\n s->crc_inv[0] = pow_poly((CRC16_POLY >> 1), (8 * frame_size_58) - 16, CRC16_POLY);\n if (s->bit_alloc.sr_code == 1) {\n frame_size_58 = (((s->frame_size+2) >> 2) + ((s->frame_size+2) >> 4)) << 1;\n s->crc_inv[1] = pow_poly((CRC16_POLY >> 1), (8 * frame_size_58) - 16, CRC16_POLY);\n }\n if (CONFIG_EAC3_ENCODER && s->eac3)\n s->output_frame_header = ff_eac3_output_frame_header;\n else\n s->output_frame_header = ac3_output_frame_header;\n set_bandwidth(s);\n exponent_init(s);\n bit_alloc_init(s);\n FF_ALLOCZ_OR_GOTO(avctx, s->mdct, sizeof(AC3MDCTContext), init_fail);\n ret = mdct_init(avctx, s->mdct, 9);\n if (ret)\n goto init_fail;\n ret = allocate_buffers(avctx);\n if (ret)\n goto init_fail;\n avctx->coded_frame= avcodec_alloc_frame();\n dsputil_init(&s->dsp, avctx);\n ff_ac3dsp_init(&s->ac3dsp, avctx->flags & CODEC_FLAG_BITEXACT);\n dprint_options(avctx);\n return 0;\ninit_fail:\n ac3_encode_close(avctx);\n return ret;\n}', 'static av_cold int validate_options(AVCodecContext *avctx, AC3EncodeContext *s)\n{\n int i, ret, max_sr;\n if (!avctx->channel_layout) {\n av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The "\n "encoder will guess the layout, but it "\n "might be incorrect.\\n");\n }\n ret = set_channel_info(s, avctx->channels, &avctx->channel_layout);\n if (ret) {\n av_log(avctx, AV_LOG_ERROR, "invalid channel layout\\n");\n return ret;\n }\n max_sr = s->eac3 ? 2 : 8;\n for (i = 0; i <= max_sr; i++) {\n if ((ff_ac3_sample_rate_tab[i % 3] >> (i / 3)) == avctx->sample_rate)\n break;\n }\n if (i > max_sr) {\n av_log(avctx, AV_LOG_ERROR, "invalid sample rate\\n");\n return AVERROR(EINVAL);\n }\n s->sample_rate = avctx->sample_rate;\n s->bit_alloc.sr_shift = i / 3;\n s->bit_alloc.sr_code = i % 3;\n s->bitstream_id = s->eac3 ? 16 : 8 + s->bit_alloc.sr_shift;\n if (s->eac3) {\n int max_br, min_br, wpf, min_br_dist, min_br_code;\n max_br = 2048 * s->sample_rate / AC3_FRAME_SIZE * 16;\n min_br = ((s->sample_rate + (AC3_FRAME_SIZE-1)) / AC3_FRAME_SIZE) * 16;\n if (avctx->bit_rate < min_br || avctx->bit_rate > max_br) {\n av_log(avctx, AV_LOG_ERROR, "invalid bit rate. must be %d to %d "\n "for this sample rate\\n", min_br, max_br);\n return AVERROR(EINVAL);\n }\n wpf = (avctx->bit_rate / 16) * AC3_FRAME_SIZE / s->sample_rate;\n av_assert1(wpf > 0 && wpf <= 2048);\n min_br_code = -1;\n min_br_dist = INT_MAX;\n for (i = 0; i < 19; i++) {\n int br_dist = abs(ff_ac3_bitrate_tab[i] * 1000 - avctx->bit_rate);\n if (br_dist < min_br_dist) {\n min_br_dist = br_dist;\n min_br_code = i;\n }\n }\n s->frame_size_code = min_br_code << 1;\n while (wpf > 1 && wpf * s->sample_rate / AC3_FRAME_SIZE * 16 > avctx->bit_rate)\n wpf--;\n s->frame_size_min = 2 * wpf;\n } else {\n for (i = 0; i < 19; i++) {\n if ((ff_ac3_bitrate_tab[i] >> s->bit_alloc.sr_shift)*1000 == avctx->bit_rate)\n break;\n }\n if (i == 19) {\n av_log(avctx, AV_LOG_ERROR, "invalid bit rate\\n");\n return AVERROR(EINVAL);\n }\n s->frame_size_code = i << 1;\n s->frame_size_min = 2 * ff_ac3_frame_size_tab[s->frame_size_code][s->bit_alloc.sr_code];\n }\n s->bit_rate = avctx->bit_rate;\n s->frame_size = s->frame_size_min;\n if (avctx->cutoff < 0) {\n av_log(avctx, AV_LOG_ERROR, "invalid cutoff frequency\\n");\n return AVERROR(EINVAL);\n }\n s->cutoff = avctx->cutoff;\n if (s->cutoff > (s->sample_rate >> 1))\n s->cutoff = s->sample_rate >> 1;\n if ((avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_KARAOKE &&\n avctx->channels == 1) ||\n ((avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_COMMENTARY ||\n avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_EMERGENCY ||\n avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_VOICE_OVER)\n && avctx->channels > 1)) {\n av_log(avctx, AV_LOG_ERROR, "invalid audio service type for the "\n "specified number of channels\\n");\n return AVERROR(EINVAL);\n }\n if (!s->eac3) {\n ret = validate_metadata(avctx);\n if (ret)\n return ret;\n }\n s->rematrixing_enabled = s->options.stereo_rematrixing &&\n (s->channel_mode == AC3_CHMODE_STEREO);\n s->cpl_enabled = s->options.channel_coupling &&\n s->channel_mode >= AC3_CHMODE_STEREO &&\n CONFIG_AC3ENC_FLOAT;\n return 0;\n}', 'static av_cold int set_channel_info(AC3EncodeContext *s, int channels,\n int64_t *channel_layout)\n{\n int ch_layout;\n if (channels < 1 || channels > AC3_MAX_CHANNELS)\n return AVERROR(EINVAL);\n if ((uint64_t)*channel_layout > 0x7FF)\n return AVERROR(EINVAL);\n ch_layout = *channel_layout;\n if (!ch_layout)\n ch_layout = avcodec_guess_channel_layout(channels, CODEC_ID_AC3, NULL);\n s->lfe_on = !!(ch_layout & AV_CH_LOW_FREQUENCY);\n s->channels = channels;\n s->fbw_channels = channels - s->lfe_on;\n s->lfe_channel = s->lfe_on ? s->fbw_channels + 1 : -1;\n if (s->lfe_on)\n ch_layout -= AV_CH_LOW_FREQUENCY;\n switch (ch_layout) {\n case AV_CH_LAYOUT_MONO: s->channel_mode = AC3_CHMODE_MONO; break;\n case AV_CH_LAYOUT_STEREO: s->channel_mode = AC3_CHMODE_STEREO; break;\n case AV_CH_LAYOUT_SURROUND: s->channel_mode = AC3_CHMODE_3F; break;\n case AV_CH_LAYOUT_2_1: s->channel_mode = AC3_CHMODE_2F1R; break;\n case AV_CH_LAYOUT_4POINT0: s->channel_mode = AC3_CHMODE_3F1R; break;\n case AV_CH_LAYOUT_QUAD:\n case AV_CH_LAYOUT_2_2: s->channel_mode = AC3_CHMODE_2F2R; break;\n case AV_CH_LAYOUT_5POINT0:\n case AV_CH_LAYOUT_5POINT0_BACK: s->channel_mode = AC3_CHMODE_3F2R; break;\n default:\n return AVERROR(EINVAL);\n }\n s->has_center = (s->channel_mode & 0x01) && s->channel_mode != AC3_CHMODE_MONO;\n s->has_surround = s->channel_mode & 0x04;\n s->channel_map = ff_ac3_enc_channel_map[s->channel_mode][s->lfe_on];\n *channel_layout = ch_layout;\n if (s->lfe_on)\n *channel_layout |= AV_CH_LOW_FREQUENCY;\n return 0;\n}', 'static av_cold void set_bandwidth(AC3EncodeContext *s)\n{\n int blk, ch;\n int av_uninit(cpl_start);\n if (s->cutoff) {\n int fbw_coeffs;\n fbw_coeffs = s->cutoff * 2 * AC3_MAX_COEFS / s->sample_rate;\n s->bandwidth_code = av_clip((fbw_coeffs - 73) / 3, 0, 60);\n } else {\n s->bandwidth_code = ac3_bandwidth_tab[s->fbw_channels-1][s->bit_alloc.sr_code][s->frame_size_code/2];\n }\n for (ch = 1; ch <= s->fbw_channels; ch++) {\n s->start_freq[ch] = 0;\n for (blk = 0; blk < AC3_MAX_BLOCKS; blk++)\n s->blocks[blk].end_freq[ch] = s->bandwidth_code * 3 + 73;\n }\n if (s->lfe_on) {\n s->start_freq[s->lfe_channel] = 0;\n for (blk = 0; blk < AC3_MAX_BLOCKS; blk++)\n s->blocks[blk].end_freq[ch] = 7;\n }\n if (s->cpl_enabled) {\n if (s->options.cpl_start >= 0) {\n cpl_start = s->options.cpl_start;\n } else {\n cpl_start = ac3_coupling_start_tab[s->channel_mode-2][s->bit_alloc.sr_code][s->frame_size_code/2];\n if (cpl_start < 0)\n s->cpl_enabled = 0;\n }\n }\n if (s->cpl_enabled) {\n int i, cpl_start_band, cpl_end_band;\n uint8_t *cpl_band_sizes = s->cpl_band_sizes;\n cpl_end_band = s->bandwidth_code / 4 + 3;\n cpl_start_band = av_clip(cpl_start, 0, FFMIN(cpl_end_band-1, 15));\n s->num_cpl_subbands = cpl_end_band - cpl_start_band;\n s->num_cpl_bands = 1;\n *cpl_band_sizes = 12;\n for (i = cpl_start_band + 1; i < cpl_end_band; i++) {\n if (ff_eac3_default_cpl_band_struct[i]) {\n *cpl_band_sizes += 12;\n } else {\n s->num_cpl_bands++;\n cpl_band_sizes++;\n *cpl_band_sizes = 12;\n }\n }\n s->start_freq[CPL_CH] = cpl_start_band * 12 + 37;\n s->cpl_end_freq = cpl_end_band * 12 + 37;\n for (blk = 0; blk < AC3_MAX_BLOCKS; blk++)\n s->blocks[blk].end_freq[CPL_CH] = s->cpl_end_freq;\n }\n}']
40
0
https://github.com/libav/libav/blob/3b2fbe67bd63b00331db2a9b213f6d420418a312/libavcodec/opus_celt.c/#L791
static void celt_decode_allocation(CeltContext *s, OpusRangeCoder *rc) { int cap[CELT_MAX_BANDS]; int boost[CELT_MAX_BANDS]; int threshold[CELT_MAX_BANDS]; int bits1[CELT_MAX_BANDS]; int bits2[CELT_MAX_BANDS]; int trim_offset[CELT_MAX_BANDS]; int skip_startband = s->startband; int dynalloc = 6; int alloctrim = 5; int extrabits = 0; int skip_bit = 0; int intensitystereo_bit = 0; int dualstereo_bit = 0; int remaining, bandbits; int low, high, total, done; int totalbits; int consumed; int i, j; consumed = opus_rc_tell(rc); s->spread = CELT_SPREAD_NORMAL; if (consumed + 4 <= s->framebits) s->spread = opus_rc_getsymbol(rc, celt_model_spread); for (i = 0; i < CELT_MAX_BANDS; i++) { cap[i] = (celt_static_caps[s->duration][s->coded_channels - 1][i] + 64) * celt_freq_range[i] << (s->coded_channels - 1) << s->duration >> 2; } totalbits = s->framebits << 3; consumed = opus_rc_tell_frac(rc); for (i = s->startband; i < s->endband; i++) { int quanta, band_dynalloc; boost[i] = 0; quanta = celt_freq_range[i] << (s->coded_channels - 1) << s->duration; quanta = FFMIN(quanta << 3, FFMAX(6 << 3, quanta)); band_dynalloc = dynalloc; while (consumed + (band_dynalloc<<3) < totalbits && boost[i] < cap[i]) { int add = opus_rc_p2model(rc, band_dynalloc); consumed = opus_rc_tell_frac(rc); if (!add) break; boost[i] += quanta; totalbits -= quanta; band_dynalloc = 1; } if (boost[i]) dynalloc = FFMAX(2, dynalloc - 1); } if (consumed + (6 << 3) <= totalbits) alloctrim = opus_rc_getsymbol(rc, celt_model_alloc_trim); totalbits = (s->framebits << 3) - opus_rc_tell_frac(rc) - 1; s->anticollapse_bit = 0; if (s->blocks > 1 && s->duration >= 2 && totalbits >= ((s->duration + 2) << 3)) s->anticollapse_bit = 1 << 3; totalbits -= s->anticollapse_bit; if (totalbits >= 1 << 3) skip_bit = 1 << 3; totalbits -= skip_bit; if (s->coded_channels == 2) { intensitystereo_bit = celt_log2_frac[s->endband - s->startband]; if (intensitystereo_bit <= totalbits) { totalbits -= intensitystereo_bit; if (totalbits >= 1 << 3) { dualstereo_bit = 1 << 3; totalbits -= 1 << 3; } } else intensitystereo_bit = 0; } for (i = s->startband; i < s->endband; i++) { int trim = alloctrim - 5 - s->duration; int band = celt_freq_range[i] * (s->endband - i - 1); int duration = s->duration + 3; int scale = duration + s->coded_channels - 1; threshold[i] = FFMAX(3 * celt_freq_range[i] << duration >> 4, s->coded_channels << 3); trim_offset[i] = trim * (band << scale) >> 6; if (celt_freq_range[i] << s->duration == 1) trim_offset[i] -= s->coded_channels << 3; } low = 1; high = CELT_VECTORS - 1; while (low <= high) { int center = (low + high) >> 1; done = total = 0; for (i = s->endband - 1; i >= s->startband; i--) { bandbits = celt_freq_range[i] * celt_static_alloc[center][i] << (s->coded_channels - 1) << s->duration >> 2; if (bandbits) bandbits = FFMAX(0, bandbits + trim_offset[i]); bandbits += boost[i]; if (bandbits >= threshold[i] || done) { done = 1; total += FFMIN(bandbits, cap[i]); } else if (bandbits >= s->coded_channels << 3) total += s->coded_channels << 3; } if (total > totalbits) high = center - 1; else low = center + 1; } high = low--; for (i = s->startband; i < s->endband; i++) { bits1[i] = celt_freq_range[i] * celt_static_alloc[low][i] << (s->coded_channels - 1) << s->duration >> 2; bits2[i] = high >= CELT_VECTORS ? cap[i] : celt_freq_range[i] * celt_static_alloc[high][i] << (s->coded_channels - 1) << s->duration >> 2; if (bits1[i]) bits1[i] = FFMAX(0, bits1[i] + trim_offset[i]); if (bits2[i]) bits2[i] = FFMAX(0, bits2[i] + trim_offset[i]); if (low) bits1[i] += boost[i]; bits2[i] += boost[i]; if (boost[i]) skip_startband = i; bits2[i] = FFMAX(0, bits2[i] - bits1[i]); } low = 0; high = 1 << CELT_ALLOC_STEPS; for (i = 0; i < CELT_ALLOC_STEPS; i++) { int center = (low + high) >> 1; done = total = 0; for (j = s->endband - 1; j >= s->startband; j--) { bandbits = bits1[j] + (center * bits2[j] >> CELT_ALLOC_STEPS); if (bandbits >= threshold[j] || done) { done = 1; total += FFMIN(bandbits, cap[j]); } else if (bandbits >= s->coded_channels << 3) total += s->coded_channels << 3; } if (total > totalbits) high = center; else low = center; } done = total = 0; for (i = s->endband - 1; i >= s->startband; i--) { bandbits = bits1[i] + (low * bits2[i] >> CELT_ALLOC_STEPS); if (bandbits >= threshold[i] || done) done = 1; else bandbits = (bandbits >= s->coded_channels << 3) ? s->coded_channels << 3 : 0; bandbits = FFMIN(bandbits, cap[i]); s->pulses[i] = bandbits; total += bandbits; } for (s->codedbands = s->endband; ; s->codedbands--) { int allocation; j = s->codedbands - 1; if (j == skip_startband) { totalbits += skip_bit; break; } remaining = totalbits - total; bandbits = remaining / (celt_freq_bands[j+1] - celt_freq_bands[s->startband]); remaining -= bandbits * (celt_freq_bands[j+1] - celt_freq_bands[s->startband]); allocation = s->pulses[j] + bandbits * celt_freq_range[j] + FFMAX(0, remaining - (celt_freq_bands[j] - celt_freq_bands[s->startband])); if (allocation >= FFMAX(threshold[j], (s->coded_channels + 1) <<3 )) { if (opus_rc_p2model(rc, 1)) break; total += 1 << 3; allocation -= 1 << 3; } total -= s->pulses[j]; if (intensitystereo_bit) { total -= intensitystereo_bit; intensitystereo_bit = celt_log2_frac[j - s->startband]; total += intensitystereo_bit; } total += s->pulses[j] = (allocation >= s->coded_channels << 3) ? s->coded_channels << 3 : 0; } s->intensitystereo = 0; s->dualstereo = 0; if (intensitystereo_bit) s->intensitystereo = s->startband + opus_rc_unimodel(rc, s->codedbands + 1 - s->startband); if (s->intensitystereo <= s->startband) totalbits += dualstereo_bit; else if (dualstereo_bit) s->dualstereo = opus_rc_p2model(rc, 1); remaining = totalbits - total; bandbits = remaining / (celt_freq_bands[s->codedbands] - celt_freq_bands[s->startband]); remaining -= bandbits * (celt_freq_bands[s->codedbands] - celt_freq_bands[s->startband]); for (i = s->startband; i < s->codedbands; i++) { int bits = FFMIN(remaining, celt_freq_range[i]); s->pulses[i] += bits + bandbits * celt_freq_range[i]; remaining -= bits; } for (i = s->startband; i < s->codedbands; i++) { int N = celt_freq_range[i] << s->duration; int prev_extra = extrabits; s->pulses[i] += extrabits; if (N > 1) { int dof; int temp; int offset; int fine_bits, max_bits; extrabits = FFMAX(0, s->pulses[i] - cap[i]); s->pulses[i] -= extrabits; dof = N * s->coded_channels + (s->coded_channels == 2 && N > 2 && !s->dualstereo && i < s->intensitystereo); temp = dof * (celt_log_freq_range[i] + (s->duration<<3)); offset = (temp >> 1) - dof * CELT_FINE_OFFSET; if (N == 2) offset += dof<<1; if (s->pulses[i] + offset < 2 * (dof << 3)) offset += temp >> 2; else if (s->pulses[i] + offset < 3 * (dof << 3)) offset += temp >> 3; fine_bits = (s->pulses[i] + offset + (dof << 2)) / (dof << 3); max_bits = FFMIN((s->pulses[i]>>3) >> (s->coded_channels - 1), CELT_MAX_FINE_BITS); max_bits = FFMAX(max_bits, 0); s->fine_bits[i] = av_clip(fine_bits, 0, max_bits); s->fine_priority[i] = (s->fine_bits[i] * (dof<<3) >= s->pulses[i] + offset); s->pulses[i] -= s->fine_bits[i] << (s->coded_channels - 1) << 3; } else { extrabits = FFMAX(0, s->pulses[i] - (s->coded_channels << 3)); s->pulses[i] -= extrabits; s->fine_bits[i] = 0; s->fine_priority[i] = 1; } if (extrabits > 0) { int fineextra = FFMIN(extrabits >> (s->coded_channels + 2), CELT_MAX_FINE_BITS - s->fine_bits[i]); s->fine_bits[i] += fineextra; fineextra <<= s->coded_channels + 2; s->fine_priority[i] = (fineextra >= extrabits - prev_extra); extrabits -= fineextra; } } s->remaining = extrabits; for (; i < s->endband; i++) { s->fine_bits[i] = s->pulses[i] >> (s->coded_channels - 1) >> 3; s->pulses[i] = 0; s->fine_priority[i] = s->fine_bits[i] < 1; } }
['static void celt_decode_allocation(CeltContext *s, OpusRangeCoder *rc)\n{\n int cap[CELT_MAX_BANDS];\n int boost[CELT_MAX_BANDS];\n int threshold[CELT_MAX_BANDS];\n int bits1[CELT_MAX_BANDS];\n int bits2[CELT_MAX_BANDS];\n int trim_offset[CELT_MAX_BANDS];\n int skip_startband = s->startband;\n int dynalloc = 6;\n int alloctrim = 5;\n int extrabits = 0;\n int skip_bit = 0;\n int intensitystereo_bit = 0;\n int dualstereo_bit = 0;\n int remaining, bandbits;\n int low, high, total, done;\n int totalbits;\n int consumed;\n int i, j;\n consumed = opus_rc_tell(rc);\n s->spread = CELT_SPREAD_NORMAL;\n if (consumed + 4 <= s->framebits)\n s->spread = opus_rc_getsymbol(rc, celt_model_spread);\n for (i = 0; i < CELT_MAX_BANDS; i++) {\n cap[i] = (celt_static_caps[s->duration][s->coded_channels - 1][i] + 64)\n * celt_freq_range[i] << (s->coded_channels - 1) << s->duration >> 2;\n }\n totalbits = s->framebits << 3;\n consumed = opus_rc_tell_frac(rc);\n for (i = s->startband; i < s->endband; i++) {\n int quanta, band_dynalloc;\n boost[i] = 0;\n quanta = celt_freq_range[i] << (s->coded_channels - 1) << s->duration;\n quanta = FFMIN(quanta << 3, FFMAX(6 << 3, quanta));\n band_dynalloc = dynalloc;\n while (consumed + (band_dynalloc<<3) < totalbits && boost[i] < cap[i]) {\n int add = opus_rc_p2model(rc, band_dynalloc);\n consumed = opus_rc_tell_frac(rc);\n if (!add)\n break;\n boost[i] += quanta;\n totalbits -= quanta;\n band_dynalloc = 1;\n }\n if (boost[i])\n dynalloc = FFMAX(2, dynalloc - 1);\n }\n if (consumed + (6 << 3) <= totalbits)\n alloctrim = opus_rc_getsymbol(rc, celt_model_alloc_trim);\n totalbits = (s->framebits << 3) - opus_rc_tell_frac(rc) - 1;\n s->anticollapse_bit = 0;\n if (s->blocks > 1 && s->duration >= 2 &&\n totalbits >= ((s->duration + 2) << 3))\n s->anticollapse_bit = 1 << 3;\n totalbits -= s->anticollapse_bit;\n if (totalbits >= 1 << 3)\n skip_bit = 1 << 3;\n totalbits -= skip_bit;\n if (s->coded_channels == 2) {\n intensitystereo_bit = celt_log2_frac[s->endband - s->startband];\n if (intensitystereo_bit <= totalbits) {\n totalbits -= intensitystereo_bit;\n if (totalbits >= 1 << 3) {\n dualstereo_bit = 1 << 3;\n totalbits -= 1 << 3;\n }\n } else\n intensitystereo_bit = 0;\n }\n for (i = s->startband; i < s->endband; i++) {\n int trim = alloctrim - 5 - s->duration;\n int band = celt_freq_range[i] * (s->endband - i - 1);\n int duration = s->duration + 3;\n int scale = duration + s->coded_channels - 1;\n threshold[i] = FFMAX(3 * celt_freq_range[i] << duration >> 4,\n s->coded_channels << 3);\n trim_offset[i] = trim * (band << scale) >> 6;\n if (celt_freq_range[i] << s->duration == 1)\n trim_offset[i] -= s->coded_channels << 3;\n }\n low = 1;\n high = CELT_VECTORS - 1;\n while (low <= high) {\n int center = (low + high) >> 1;\n done = total = 0;\n for (i = s->endband - 1; i >= s->startband; i--) {\n bandbits = celt_freq_range[i] * celt_static_alloc[center][i]\n << (s->coded_channels - 1) << s->duration >> 2;\n if (bandbits)\n bandbits = FFMAX(0, bandbits + trim_offset[i]);\n bandbits += boost[i];\n if (bandbits >= threshold[i] || done) {\n done = 1;\n total += FFMIN(bandbits, cap[i]);\n } else if (bandbits >= s->coded_channels << 3)\n total += s->coded_channels << 3;\n }\n if (total > totalbits)\n high = center - 1;\n else\n low = center + 1;\n }\n high = low--;\n for (i = s->startband; i < s->endband; i++) {\n bits1[i] = celt_freq_range[i] * celt_static_alloc[low][i]\n << (s->coded_channels - 1) << s->duration >> 2;\n bits2[i] = high >= CELT_VECTORS ? cap[i] :\n celt_freq_range[i] * celt_static_alloc[high][i]\n << (s->coded_channels - 1) << s->duration >> 2;\n if (bits1[i])\n bits1[i] = FFMAX(0, bits1[i] + trim_offset[i]);\n if (bits2[i])\n bits2[i] = FFMAX(0, bits2[i] + trim_offset[i]);\n if (low)\n bits1[i] += boost[i];\n bits2[i] += boost[i];\n if (boost[i])\n skip_startband = i;\n bits2[i] = FFMAX(0, bits2[i] - bits1[i]);\n }\n low = 0;\n high = 1 << CELT_ALLOC_STEPS;\n for (i = 0; i < CELT_ALLOC_STEPS; i++) {\n int center = (low + high) >> 1;\n done = total = 0;\n for (j = s->endband - 1; j >= s->startband; j--) {\n bandbits = bits1[j] + (center * bits2[j] >> CELT_ALLOC_STEPS);\n if (bandbits >= threshold[j] || done) {\n done = 1;\n total += FFMIN(bandbits, cap[j]);\n } else if (bandbits >= s->coded_channels << 3)\n total += s->coded_channels << 3;\n }\n if (total > totalbits)\n high = center;\n else\n low = center;\n }\n done = total = 0;\n for (i = s->endband - 1; i >= s->startband; i--) {\n bandbits = bits1[i] + (low * bits2[i] >> CELT_ALLOC_STEPS);\n if (bandbits >= threshold[i] || done)\n done = 1;\n else\n bandbits = (bandbits >= s->coded_channels << 3) ?\n s->coded_channels << 3 : 0;\n bandbits = FFMIN(bandbits, cap[i]);\n s->pulses[i] = bandbits;\n total += bandbits;\n }\n for (s->codedbands = s->endband; ; s->codedbands--) {\n int allocation;\n j = s->codedbands - 1;\n if (j == skip_startband) {\n totalbits += skip_bit;\n break;\n }\n remaining = totalbits - total;\n bandbits = remaining / (celt_freq_bands[j+1] - celt_freq_bands[s->startband]);\n remaining -= bandbits * (celt_freq_bands[j+1] - celt_freq_bands[s->startband]);\n allocation = s->pulses[j] + bandbits * celt_freq_range[j]\n + FFMAX(0, remaining - (celt_freq_bands[j] - celt_freq_bands[s->startband]));\n if (allocation >= FFMAX(threshold[j], (s->coded_channels + 1) <<3 )) {\n if (opus_rc_p2model(rc, 1))\n break;\n total += 1 << 3;\n allocation -= 1 << 3;\n }\n total -= s->pulses[j];\n if (intensitystereo_bit) {\n total -= intensitystereo_bit;\n intensitystereo_bit = celt_log2_frac[j - s->startband];\n total += intensitystereo_bit;\n }\n total += s->pulses[j] = (allocation >= s->coded_channels << 3) ?\n s->coded_channels << 3 : 0;\n }\n s->intensitystereo = 0;\n s->dualstereo = 0;\n if (intensitystereo_bit)\n s->intensitystereo = s->startband +\n opus_rc_unimodel(rc, s->codedbands + 1 - s->startband);\n if (s->intensitystereo <= s->startband)\n totalbits += dualstereo_bit;\n else if (dualstereo_bit)\n s->dualstereo = opus_rc_p2model(rc, 1);\n remaining = totalbits - total;\n bandbits = remaining / (celt_freq_bands[s->codedbands] - celt_freq_bands[s->startband]);\n remaining -= bandbits * (celt_freq_bands[s->codedbands] - celt_freq_bands[s->startband]);\n for (i = s->startband; i < s->codedbands; i++) {\n int bits = FFMIN(remaining, celt_freq_range[i]);\n s->pulses[i] += bits + bandbits * celt_freq_range[i];\n remaining -= bits;\n }\n for (i = s->startband; i < s->codedbands; i++) {\n int N = celt_freq_range[i] << s->duration;\n int prev_extra = extrabits;\n s->pulses[i] += extrabits;\n if (N > 1) {\n int dof;\n int temp;\n int offset;\n int fine_bits, max_bits;\n extrabits = FFMAX(0, s->pulses[i] - cap[i]);\n s->pulses[i] -= extrabits;\n dof = N * s->coded_channels\n + (s->coded_channels == 2 && N > 2 && !s->dualstereo && i < s->intensitystereo);\n temp = dof * (celt_log_freq_range[i] + (s->duration<<3));\n offset = (temp >> 1) - dof * CELT_FINE_OFFSET;\n if (N == 2)\n offset += dof<<1;\n if (s->pulses[i] + offset < 2 * (dof << 3))\n offset += temp >> 2;\n else if (s->pulses[i] + offset < 3 * (dof << 3))\n offset += temp >> 3;\n fine_bits = (s->pulses[i] + offset + (dof << 2)) / (dof << 3);\n max_bits = FFMIN((s->pulses[i]>>3) >> (s->coded_channels - 1),\n CELT_MAX_FINE_BITS);\n max_bits = FFMAX(max_bits, 0);\n s->fine_bits[i] = av_clip(fine_bits, 0, max_bits);\n s->fine_priority[i] = (s->fine_bits[i] * (dof<<3) >= s->pulses[i] + offset);\n s->pulses[i] -= s->fine_bits[i] << (s->coded_channels - 1) << 3;\n } else {\n extrabits = FFMAX(0, s->pulses[i] - (s->coded_channels << 3));\n s->pulses[i] -= extrabits;\n s->fine_bits[i] = 0;\n s->fine_priority[i] = 1;\n }\n if (extrabits > 0) {\n int fineextra = FFMIN(extrabits >> (s->coded_channels + 2),\n CELT_MAX_FINE_BITS - s->fine_bits[i]);\n s->fine_bits[i] += fineextra;\n fineextra <<= s->coded_channels + 2;\n s->fine_priority[i] = (fineextra >= extrabits - prev_extra);\n extrabits -= fineextra;\n }\n }\n s->remaining = extrabits;\n for (; i < s->endband; i++) {\n s->fine_bits[i] = s->pulses[i] >> (s->coded_channels - 1) >> 3;\n s->pulses[i] = 0;\n s->fine_priority[i] = s->fine_bits[i] < 1;\n }\n}']
41
0
https://github.com/openssl/openssl/blob/4b8515baa6edef1a771f9e4e3fbc0395b4a629e8/crypto/bn/bn_lib.c/#L271
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *a = NULL; bn_check_top(b); if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return (NULL); } if (BN_get_flags(b, BN_FLG_SECURE)) a = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = OPENSSL_zalloc(words * sizeof(*a)); if (a == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return (NULL); } assert(b->top <= words); if (b->top > 0) memcpy(a, b->d, sizeof(*a) * b->top); return a; }
['int BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m,\n BN_RECP_CTX *recp, BN_CTX *ctx)\n{\n int i, j, ret = 0;\n BIGNUM *a, *b, *d, *r;\n BN_CTX_start(ctx);\n d = (dv != NULL) ? dv : BN_CTX_get(ctx);\n r = (rem != NULL) ? rem : BN_CTX_get(ctx);\n a = BN_CTX_get(ctx);\n b = BN_CTX_get(ctx);\n if (b == NULL)\n goto err;\n if (BN_ucmp(m, &(recp->N)) < 0) {\n BN_zero(d);\n if (!BN_copy(r, m)) {\n BN_CTX_end(ctx);\n return 0;\n }\n BN_CTX_end(ctx);\n return (1);\n }\n i = BN_num_bits(m);\n j = recp->num_bits << 1;\n if (j > i)\n i = j;\n if (i != recp->shift)\n recp->shift = BN_reciprocal(&(recp->Nr), &(recp->N), i, ctx);\n if (recp->shift == -1)\n goto err;\n if (!BN_rshift(a, m, recp->num_bits))\n goto err;\n if (!BN_mul(b, a, &(recp->Nr), ctx))\n goto err;\n if (!BN_rshift(d, b, i - recp->num_bits))\n goto err;\n d->neg = 0;\n if (!BN_mul(b, &(recp->N), d, ctx))\n goto err;\n if (!BN_usub(r, m, b))\n goto err;\n r->neg = 0;\n j = 0;\n while (BN_ucmp(r, &(recp->N)) >= 0) {\n if (j++ > 2) {\n BNerr(BN_F_BN_DIV_RECP, BN_R_BAD_RECIPROCAL);\n goto err;\n }\n if (!BN_usub(r, r, &(recp->N)))\n goto err;\n if (!BN_add_word(d, 1))\n goto err;\n }\n r->neg = BN_is_zero(r) ? 0 : m->neg;\n d->neg = m->neg ^ recp->N.neg;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n bn_check_top(dv);\n bn_check_top(rem);\n return (ret);\n}', '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_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}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return (NULL);\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n osslargused(file); osslargused(line);\n ret = malloc(num);\n#endif\n return ret;\n}']
42
0
https://github.com/openssl/openssl/blob/31f528b15de7bc2afbbdc592973a3a50da9b1629/engines/ccgost/gost_pmeth.c/#L85
static int pkey_gost_ctrl (EVP_PKEY_CTX *ctx, int type, int p1, void *p2) { struct gost_pmeth_data *pctx = (struct gost_pmeth_data*)EVP_PKEY_CTX_get_data(ctx); switch (type) { case EVP_PKEY_CTRL_MD: { if (EVP_MD_type((const EVP_MD *)p2) != NID_id_GostR3411_94) { GOSTerr(GOST_F_PKEY_GOST_CTRL, GOST_R_INVALID_DIGEST_TYPE); return 0; } pctx->md = (EVP_MD *)p2; return 1; } break; case EVP_PKEY_CTRL_PKCS7_ENCRYPT: case EVP_PKEY_CTRL_PKCS7_DECRYPT: case EVP_PKEY_CTRL_PKCS7_SIGN: return 1; case EVP_PKEY_CTRL_GOST_PARAMSET: pctx->sign_param_nid = (int)p1; return 1; case EVP_PKEY_CTRL_SET_IV: pctx->shared_ukm=OPENSSL_malloc((int)p1); memcpy(pctx->shared_ukm,p2,(int) p1); return 1; } return -2; }
['static int pkey_gost_ctrl (EVP_PKEY_CTX *ctx, int type, int p1, void *p2)\n\t{\n\tstruct gost_pmeth_data *pctx = (struct gost_pmeth_data*)EVP_PKEY_CTX_get_data(ctx);\n\tswitch (type)\n\t\t{\n\t\tcase EVP_PKEY_CTRL_MD:\n\t\t{\n\t\tif (EVP_MD_type((const EVP_MD *)p2) != NID_id_GostR3411_94)\n\t\t\t{\n\t\t\tGOSTerr(GOST_F_PKEY_GOST_CTRL, GOST_R_INVALID_DIGEST_TYPE);\n\t\t\treturn 0;\n\t\t\t}\n\t\tpctx->md = (EVP_MD *)p2;\n\t\treturn 1;\n\t\t}\n\t\tbreak;\n\t\tcase EVP_PKEY_CTRL_PKCS7_ENCRYPT:\n\t\tcase EVP_PKEY_CTRL_PKCS7_DECRYPT:\n\t\tcase EVP_PKEY_CTRL_PKCS7_SIGN:\n\t\t\treturn 1;\n\t\tcase EVP_PKEY_CTRL_GOST_PARAMSET:\n\t\t\tpctx->sign_param_nid = (int)p1;\n\t\t\treturn 1;\n\t\tcase EVP_PKEY_CTRL_SET_IV:\n\t\t\tpctx->shared_ukm=OPENSSL_malloc((int)p1);\n\t\t\tmemcpy(pctx->shared_ukm,p2,(int) p1);\n\t\t\treturn 1;\n\t\t}\n\treturn -2;\n\t}', 'void *EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx)\n\t{\n\treturn ctx->data;\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}']
43
0
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/bn/bn_ctx.c/#L328
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['int ec_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}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_start", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG_EXIT(ctx);\n}', 'BIGNUM *BN_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n{\n BIGNUM *rv;\n int noinv;\n rv = int_bn_mod_inverse(in, a, n, ctx, &noinv);\n if (noinv)\n BNerr(BN_F_BN_MOD_INVERSE, BN_R_NO_INVERSE);\n return rv;\n}', 'BIGNUM *int_bn_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,\n int *pnoinv)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM *ret = NULL;\n int sign;\n if (pnoinv)\n *pnoinv = 0;\n if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {\n return BN_mod_inverse_no_branch(in, a, n, ctx);\n }\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n if (!BN_nnmod(B, B, A, ctx))\n goto err;\n }\n sign = -1;\n if (BN_is_odd(n) && (BN_num_bits(n) <= (BN_BITS <= 32 ? 450 : 2048))) {\n int shift;\n while (!BN_is_zero(B)) {\n shift = 0;\n while (!BN_is_bit_set(B, shift)) {\n shift++;\n if (BN_is_odd(X)) {\n if (!BN_uadd(X, X, n))\n goto err;\n }\n if (!BN_rshift1(X, X))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(B, B, shift))\n goto err;\n }\n shift = 0;\n while (!BN_is_bit_set(A, shift)) {\n shift++;\n if (BN_is_odd(Y)) {\n if (!BN_uadd(Y, Y, n))\n goto err;\n }\n if (!BN_rshift1(Y, Y))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(A, A, shift))\n goto err;\n }\n if (BN_ucmp(B, A) >= 0) {\n if (!BN_uadd(X, X, Y))\n goto err;\n if (!BN_usub(B, B, A))\n goto err;\n } else {\n if (!BN_uadd(Y, Y, X))\n goto err;\n if (!BN_usub(A, A, B))\n goto err;\n }\n }\n } else {\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n if (BN_num_bits(A) == BN_num_bits(B)) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else if (BN_num_bits(A) == BN_num_bits(B) + 1) {\n if (!BN_lshift1(T, B))\n goto err;\n if (BN_ucmp(A, T) < 0) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else {\n if (!BN_sub(M, A, T))\n goto err;\n if (!BN_add(D, T, B))\n goto err;\n if (BN_ucmp(A, D) < 0) {\n if (!BN_set_word(D, 2))\n goto err;\n } else {\n if (!BN_set_word(D, 3))\n goto err;\n if (!BN_sub(M, M, B))\n goto err;\n }\n }\n } else {\n if (!BN_div(D, M, A, B, ctx))\n goto err;\n }\n tmp = A;\n A = B;\n B = M;\n if (BN_is_one(D)) {\n if (!BN_add(tmp, X, Y))\n goto err;\n } else {\n if (BN_is_word(D, 2)) {\n if (!BN_lshift1(tmp, X))\n goto err;\n } else if (BN_is_word(D, 4)) {\n if (!BN_lshift(tmp, X, 2))\n goto err;\n } else if (D->top == 1) {\n if (!BN_copy(tmp, X))\n goto err;\n if (!BN_mul_word(tmp, D->d[0]))\n goto err;\n } else {\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n }\n if (!BN_add(tmp, tmp, Y))\n goto err;\n }\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n if (pnoinv)\n *pnoinv = 1;\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return (ret);\n}', 'static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n,\n BN_CTX *ctx)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM *ret = NULL;\n int sign;\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n {\n BIGNUM local_B;\n bn_init(&local_B);\n BN_with_flags(&local_B, B, BN_FLG_CONSTTIME);\n if (!BN_nnmod(B, &local_B, A, ctx))\n goto err;\n }\n }\n sign = -1;\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n {\n BIGNUM local_A;\n bn_init(&local_A);\n BN_with_flags(&local_A, A, BN_FLG_CONSTTIME);\n if (!BN_div(D, M, &local_A, B, ctx))\n goto err;\n }\n tmp = A;\n A = B;\n B = M;\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n if (!BN_add(tmp, tmp, Y))\n goto err;\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n BNerr(BN_F_BN_MOD_INVERSE_NO_BRANCH, BN_R_NO_INVERSE);\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return (ret);\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return (0);\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return (0);\n }\n if (dv != NULL)\n BN_zero(dv);\n return (1);\n }\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (dv == NULL)\n res = BN_CTX_get(ctx);\n else\n res = dv;\n if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n res->neg = (num->neg ^ divisor->neg);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--, resp--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# ifdef BN_DEBUG_LEVITTE\n fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n", n0, n1, d0, q);\n# endif\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifdef BN_DEBUG_LEVITTE\n fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n", n0, n1, d0, q);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return (1);\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return (0);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
44
0
https://github.com/openssl/openssl/blob/9639515871c73722de3fff04d3c50d54aa6b1477/crypto/asn1/x_name.c/#L257
void X509_NAME_ENTRY_free(X509_NAME_ENTRY *a) { if (a == NULL) return; ASN1_OBJECT_free(a->object); ASN1_BIT_STRING_free(a->value); Free(a); }
['X509_NAME *d2i_X509_NAME(X509_NAME **a, unsigned char **pp, long length)\n\t{\n\tint set=0,i;\n\tint idx=0;\n\tunsigned char *orig;\n\tM_ASN1_D2I_vars(a,X509_NAME *,X509_NAME_new);\n\torig= *pp;\n\tif (sk_X509_NAME_ENTRY_num(ret->entries) > 0)\n\t\t{\n\t\twhile (sk_X509_NAME_ENTRY_num(ret->entries) > 0)\n\t\t\tX509_NAME_ENTRY_free(\n\t\t\t\t sk_X509_NAME_ENTRY_pop(ret->entries));\n\t\t}\n\tM_ASN1_D2I_Init();\n\tM_ASN1_D2I_start_sequence();\n\tfor (;;)\n\t\t{\n\t\tif (M_ASN1_D2I_end_sequence()) break;\n\t\tM_ASN1_D2I_get_set_type(X509_NAME_ENTRY,ret->entries,\n\t\t\t\t\td2i_X509_NAME_ENTRY,\n\t\t\t\t\tX509_NAME_ENTRY_free);\n\t\tfor (; idx < sk_X509_NAME_ENTRY_num(ret->entries); idx++)\n\t\t\t{\n\t\t\tsk_X509_NAME_ENTRY_value(ret->entries,idx)->set=set;\n\t\t\t}\n\t\tset++;\n\t\t}\n\ti=(int)(c.p-orig);\n\tif (!BUF_MEM_grow(ret->bytes,i)) goto err;\n\tmemcpy(ret->bytes->data,orig,i);\n\tret->bytes->length=i;\n\tret->modified=0;\n\tM_ASN1_D2I_Finish(a,X509_NAME_free,ASN1_F_D2I_X509_NAME);\n\t}', 'void X509_NAME_ENTRY_free(X509_NAME_ENTRY *a)\n\t{\n\tif (a == NULL) return;\n\tASN1_OBJECT_free(a->object);\n\tASN1_BIT_STRING_free(a->value);\n\tFree(a);\n\t}']
45
0
https://github.com/openssl/openssl/blob/732d31beeeb2e2e9f44d05da8387cfeca06b91b8/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\t{\n\tBIGNUM *kinv=NULL,*r=NULL,*s=NULL;\n\tBIGNUM m;\n\tBIGNUM xr;\n\tBN_CTX *ctx=NULL;\n\tint reason=ERR_R_BN_LIB;\n\tDSA_SIG *ret=NULL;\n\tBN_init(&m);\n\tBN_init(&xr);\n\tif (!dsa->p || !dsa->q || !dsa->g)\n\t\t{\n\t\treason=DSA_R_MISSING_PARAMETERS;\n\t\tgoto err;\n\t\t}\n\ts=BN_new();\n\tif (s == NULL) goto err;\n\tif (dlen > SHA256_DIGEST_LENGTH)\n\t\t{\n\t\treason=DSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE;\n\t\tgoto err;\n\t\t}\n\tctx=BN_CTX_new();\n\tif (ctx == NULL) goto err;\n\tif ((dsa->kinv == NULL) || (dsa->r == NULL))\n\t\t{\n\t\tif (!DSA_sign_setup(dsa,ctx,&kinv,&r)) goto err;\n\t\t}\n\telse\n\t\t{\n\t\tkinv=dsa->kinv;\n\t\tdsa->kinv=NULL;\n\t\tr=dsa->r;\n\t\tdsa->r=NULL;\n\t\t}\n\tif (dlen > BN_num_bytes(dsa->q))\n\t\tdlen = BN_num_bytes(dsa->q);\n\tif (BN_bin2bn(dgst,dlen,&m) == NULL)\n\t\tgoto err;\n\tif (!BN_mod_mul(&xr,dsa->priv_key,r,dsa->q,ctx)) goto err;\n\tif (!BN_add(s, &xr, &m)) goto err;\n\tif (BN_cmp(s,dsa->q) > 0)\n\t\tif (!BN_sub(s,s,dsa->q)) goto err;\n\tif (!BN_mod_mul(s,s,kinv,dsa->q,ctx)) goto err;\n\tret=DSA_SIG_new();\n\tif (ret == NULL) goto err;\n\tret->r = r;\n\tret->s = s;\nerr:\n\tif (!ret)\n\t\t{\n\t\tDSAerr(DSA_F_DSA_DO_SIGN,reason);\n\t\tBN_free(r);\n\t\tBN_free(s);\n\t\t}\n\tif (ctx != NULL) BN_CTX_free(ctx);\n\tBN_clear_free(&m);\n\tBN_clear_free(&xr);\n\tif (kinv != NULL)\n\t BN_clear_free(kinv);\n\treturn(ret);\n\t}', 'int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,\n\tBN_CTX *ctx)\n\t{\n\tBIGNUM *t;\n\tint ret=0;\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tbn_check_top(m);\n\tBN_CTX_start(ctx);\n\tif ((t = BN_CTX_get(ctx)) == NULL) goto err;\n\tif (a == b)\n\t\t{ if (!BN_sqr(t,a,ctx)) goto err; }\n\telse\n\t\t{ if (!BN_mul(t,a,b,ctx)) goto err; }\n\tif (!BN_nnmod(r,t,m,ctx)) goto err;\n\tbn_check_top(r);\n\tret=1;\nerr:\n\tBN_CTX_end(ctx);\n\treturn(ret);\n\t}', 'void BN_CTX_start(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_start", ctx);\n\tif(ctx->err_stack || ctx->too_many)\n\t\tctx->err_stack++;\n\telse if(!BN_STACK_push(&ctx->stack, ctx->used))\n\t\t{\n\t\tBNerr(BN_F_BN_CTX_START,BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n\t\tctx->err_stack++;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'void BN_CTX_end(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_end", ctx);\n\tif(ctx->err_stack)\n\t\tctx->err_stack--;\n\telse\n\t\t{\n\t\tunsigned int fp = BN_STACK_pop(&ctx->stack);\n\t\tif(fp < ctx->used)\n\t\t\tBN_POOL_release(&ctx->pool, ctx->used - fp);\n\t\tctx->used = fp;\n\t\tctx->too_many = 0;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n\t{\n\tint max,al;\n\tint ret = 0;\n\tBIGNUM *tmp,*rr;\n#ifdef BN_COUNT\n\tfprintf(stderr,"BN_sqr %d * %d\\n",a->top,a->top);\n#endif\n\tbn_check_top(a);\n\tal=a->top;\n\tif (al <= 0)\n\t\t{\n\t\tr->top=0;\n\t\treturn 1;\n\t\t}\n\tBN_CTX_start(ctx);\n\trr=(a != r) ? r : BN_CTX_get(ctx);\n\ttmp=BN_CTX_get(ctx);\n\tif (!rr || !tmp) goto err;\n\tmax = 2 * al;\n\tif (bn_wexpand(rr,max) == NULL) goto err;\n\tif (al == 4)\n\t\t{\n#ifndef BN_SQR_COMBA\n\t\tBN_ULONG t[8];\n\t\tbn_sqr_normal(rr->d,a->d,4,t);\n#else\n\t\tbn_sqr_comba4(rr->d,a->d);\n#endif\n\t\t}\n\telse if (al == 8)\n\t\t{\n#ifndef BN_SQR_COMBA\n\t\tBN_ULONG t[16];\n\t\tbn_sqr_normal(rr->d,a->d,8,t);\n#else\n\t\tbn_sqr_comba8(rr->d,a->d);\n#endif\n\t\t}\n\telse\n\t\t{\n#if defined(BN_RECURSION)\n\t\tif (al < BN_SQR_RECURSIVE_SIZE_NORMAL)\n\t\t\t{\n\t\t\tBN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL*2];\n\t\t\tbn_sqr_normal(rr->d,a->d,al,t);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tint j,k;\n\t\t\tj=BN_num_bits_word((BN_ULONG)al);\n\t\t\tj=1<<(j-1);\n\t\t\tk=j+j;\n\t\t\tif (al == j)\n\t\t\t\t{\n\t\t\t\tif (bn_wexpand(tmp,k*2) == NULL) goto err;\n\t\t\t\tbn_sqr_recursive(rr->d,a->d,al,tmp->d);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (bn_wexpand(tmp,max) == NULL) goto err;\n\t\t\t\tbn_sqr_normal(rr->d,a->d,al,tmp->d);\n\t\t\t\t}\n\t\t\t}\n#else\n\t\tif (bn_wexpand(tmp,max) == NULL) goto err;\n\t\tbn_sqr_normal(rr->d,a->d,al,tmp->d);\n#endif\n\t\t}\n\trr->neg=0;\n\tif(a->d[al - 1] == (a->d[al - 1] & BN_MASK2l))\n\t\trr->top = max - 1;\n\telse\n\t\trr->top = max;\n\tif (rr != r) BN_copy(r,rr);\n\tret = 1;\n err:\n\tbn_check_top(rr);\n\tbn_check_top(tmp);\n\tBN_CTX_end(ctx);\n\treturn(ret);\n\t}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n\t{\n\treturn st->indexes[--(st->depth)];\n\t}']
46
0
https://github.com/openssl/openssl/blob/ee6b68ce4c67870f9323d2a380eb949f447c56ee/crypto/o_str.c/#L216
char *OPENSSL_buf2hexstr(const unsigned char *buffer, long len) { static const char hexdig[] = "0123456789ABCDEF"; char *tmp, *q; const unsigned char *p; int i; if (len == 0) { return OPENSSL_zalloc(1); } if ((tmp = OPENSSL_malloc(len * 3)) == NULL) { CRYPTOerr(CRYPTO_F_OPENSSL_BUF2HEXSTR, ERR_R_MALLOC_FAILURE); return NULL; } q = tmp; for (i = 0, p = buffer; i < len; i++, p++) { *q++ = hexdig[(*p >> 4) & 0xf]; *q++ = hexdig[*p & 0xf]; *q++ = ':'; } q[-1] = 0; #ifdef CHARSET_EBCDIC ebcdic2ascii(tmp, tmp, q - tmp - 1); #endif return tmp; }
['char *OPENSSL_buf2hexstr(const unsigned char *buffer, long len)\n{\n static const char hexdig[] = "0123456789ABCDEF";\n char *tmp, *q;\n const unsigned char *p;\n int i;\n if (len == 0)\n {\n return OPENSSL_zalloc(1);\n }\n if ((tmp = OPENSSL_malloc(len * 3)) == NULL) {\n CRYPTOerr(CRYPTO_F_OPENSSL_BUF2HEXSTR, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n q = tmp;\n for (i = 0, p = buffer; i < len; i++, p++) {\n *q++ = hexdig[(*p >> 4) & 0xf];\n *q++ = hexdig[*p & 0xf];\n *q++ = \':\';\n }\n q[-1] = 0;\n#ifdef CHARSET_EBCDIC\n ebcdic2ascii(tmp, tmp, q - tmp - 1);\n#endif\n return tmp;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n osslargused(file); osslargused(line);\n ret = malloc(num);\n#endif\n return ret;\n}']
47
0
https://github.com/openssl/openssl/blob/ea32151f7b9353f8906188d007c6893704ac17bb/crypto/ts/ts_rsp_verify.c/#L380
static int int_ts_RESP_verify_token(TS_VERIFY_CTX *ctx, PKCS7 *token, TS_TST_INFO *tst_info) { X509 *signer = NULL; GENERAL_NAME *tsa_name = tst_info->tsa; X509_ALGOR *md_alg = NULL; unsigned char *imprint = NULL; unsigned imprint_len = 0; int ret = 0; if ((ctx->flags & TS_VFY_SIGNATURE) && !TS_RESP_verify_signature(token, ctx->certs, ctx->store, &signer)) goto err; if ((ctx->flags & TS_VFY_VERSION) && TS_TST_INFO_get_version(tst_info) != 1) { TSerr(TS_F_INT_TS_RESP_VERIFY_TOKEN, TS_R_UNSUPPORTED_VERSION); goto err; } if ((ctx->flags & TS_VFY_POLICY) && !ts_check_policy(ctx->policy, tst_info)) goto err; if ((ctx->flags & TS_VFY_IMPRINT) && !ts_check_imprints(ctx->md_alg, ctx->imprint, ctx->imprint_len, tst_info)) goto err; if ((ctx->flags & TS_VFY_DATA) && (!ts_compute_imprint(ctx->data, tst_info, &md_alg, &imprint, &imprint_len) || !ts_check_imprints(md_alg, imprint, imprint_len, tst_info))) goto err; if ((ctx->flags & TS_VFY_NONCE) && !ts_check_nonces(ctx->nonce, tst_info)) goto err; if ((ctx->flags & TS_VFY_SIGNER) && tsa_name && !ts_check_signer_name(tsa_name, signer)) { TSerr(TS_F_INT_TS_RESP_VERIFY_TOKEN, TS_R_TSA_NAME_MISMATCH); goto err; } if ((ctx->flags & TS_VFY_TSA_NAME) && !ts_check_signer_name(ctx->tsa_name, signer)) { TSerr(TS_F_INT_TS_RESP_VERIFY_TOKEN, TS_R_TSA_UNTRUSTED); goto err; } ret = 1; err: X509_free(signer); X509_ALGOR_free(md_alg); OPENSSL_free(imprint); return ret; }
['static int int_ts_RESP_verify_token(TS_VERIFY_CTX *ctx,\n PKCS7 *token, TS_TST_INFO *tst_info)\n{\n X509 *signer = NULL;\n GENERAL_NAME *tsa_name = tst_info->tsa;\n X509_ALGOR *md_alg = NULL;\n unsigned char *imprint = NULL;\n unsigned imprint_len = 0;\n int ret = 0;\n if ((ctx->flags & TS_VFY_SIGNATURE)\n && !TS_RESP_verify_signature(token, ctx->certs, ctx->store, &signer))\n goto err;\n if ((ctx->flags & TS_VFY_VERSION)\n && TS_TST_INFO_get_version(tst_info) != 1) {\n TSerr(TS_F_INT_TS_RESP_VERIFY_TOKEN, TS_R_UNSUPPORTED_VERSION);\n goto err;\n }\n if ((ctx->flags & TS_VFY_POLICY)\n && !ts_check_policy(ctx->policy, tst_info))\n goto err;\n if ((ctx->flags & TS_VFY_IMPRINT)\n && !ts_check_imprints(ctx->md_alg, ctx->imprint, ctx->imprint_len,\n tst_info))\n goto err;\n if ((ctx->flags & TS_VFY_DATA)\n && (!ts_compute_imprint(ctx->data, tst_info,\n &md_alg, &imprint, &imprint_len)\n || !ts_check_imprints(md_alg, imprint, imprint_len, tst_info)))\n goto err;\n if ((ctx->flags & TS_VFY_NONCE)\n && !ts_check_nonces(ctx->nonce, tst_info))\n goto err;\n if ((ctx->flags & TS_VFY_SIGNER)\n && tsa_name && !ts_check_signer_name(tsa_name, signer)) {\n TSerr(TS_F_INT_TS_RESP_VERIFY_TOKEN, TS_R_TSA_NAME_MISMATCH);\n goto err;\n }\n if ((ctx->flags & TS_VFY_TSA_NAME)\n && !ts_check_signer_name(ctx->tsa_name, signer)) {\n TSerr(TS_F_INT_TS_RESP_VERIFY_TOKEN, TS_R_TSA_UNTRUSTED);\n goto err;\n }\n ret = 1;\n err:\n X509_free(signer);\n X509_ALGOR_free(md_alg);\n OPENSSL_free(imprint);\n return ret;\n}', 'static int ts_check_policy(ASN1_OBJECT *req_oid, TS_TST_INFO *tst_info)\n{\n ASN1_OBJECT *resp_oid = tst_info->policy_id;\n if (OBJ_cmp(req_oid, resp_oid) != 0) {\n TSerr(TS_F_TS_CHECK_POLICY, TS_R_POLICY_MISMATCH);\n return 0;\n }\n return 1;\n}', 'int OBJ_cmp(const ASN1_OBJECT *a, const ASN1_OBJECT *b)\n{\n int ret;\n ret = (a->length - b->length);\n if (ret)\n return (ret);\n return (memcmp(a->data, b->data, a->length));\n}', 'static int ts_check_signer_name(GENERAL_NAME *tsa_name, X509 *signer)\n{\n STACK_OF(GENERAL_NAME) *gen_names = NULL;\n int idx = -1;\n int found = 0;\n if (tsa_name->type == GEN_DIRNAME\n && X509_name_cmp(tsa_name->d.dirn, X509_get_subject_name(signer)) == 0)\n return 1;\n gen_names = X509_get_ext_d2i(signer, NID_subject_alt_name, NULL, &idx);\n while (gen_names != NULL) {\n found = ts_find_name(gen_names, tsa_name) >= 0;\n if (found)\n break;\n GENERAL_NAMES_free(gen_names);\n gen_names = X509_get_ext_d2i(signer, NID_subject_alt_name, NULL, &idx);\n }\n GENERAL_NAMES_free(gen_names);\n return found;\n}', 'void *X509_get_ext_d2i(X509 *x, int nid, int *crit, int *idx)\n{\n return X509V3_get_d2i(x->cert_info.extensions, nid, crit, idx);\n}']
48
0
https://github.com/openssl/openssl/blob/3ad4af89cf7380aa94d1995e05e713d59e1c469a/crypto/idea/i_skey.c/#L127
void IDEA_set_decrypt_key(IDEA_KEY_SCHEDULE *ek, IDEA_KEY_SCHEDULE *dk) { int r; register IDEA_INT *fp, *tp, t; tp = &(dk->data[0][0]); fp = &(ek->data[8][0]); for (r = 0; r < 9; r++) { *(tp++) = inverse(fp[0]); *(tp++) = ((int)(0x10000L - fp[2]) & 0xffff); *(tp++) = ((int)(0x10000L - fp[1]) & 0xffff); *(tp++) = inverse(fp[3]); if (r == 8) break; fp -= 6; *(tp++) = fp[4]; *(tp++) = fp[5]; } tp = &(dk->data[0][0]); t = tp[1]; tp[1] = tp[2]; tp[2] = t; t = tp[49]; tp[49] = tp[50]; tp[50] = t; }
['void IDEA_set_decrypt_key(IDEA_KEY_SCHEDULE *ek, IDEA_KEY_SCHEDULE *dk)\n{\n int r;\n register IDEA_INT *fp, *tp, t;\n tp = &(dk->data[0][0]);\n fp = &(ek->data[8][0]);\n for (r = 0; r < 9; r++) {\n *(tp++) = inverse(fp[0]);\n *(tp++) = ((int)(0x10000L - fp[2]) & 0xffff);\n *(tp++) = ((int)(0x10000L - fp[1]) & 0xffff);\n *(tp++) = inverse(fp[3]);\n if (r == 8)\n break;\n fp -= 6;\n *(tp++) = fp[4];\n *(tp++) = fp[5];\n }\n tp = &(dk->data[0][0]);\n t = tp[1];\n tp[1] = tp[2];\n tp[2] = t;\n t = tp[49];\n tp[49] = tp[50];\n tp[50] = t;\n}']
49
0
https://github.com/openssl/openssl/blob/9b02dc97e4963969da69675a871dbe80e6d31cda/crypto/bn/bn_shift.c/#L112
int BN_lshift(BIGNUM *r, const BIGNUM *a, int n) { int i, nw, lb, rb; BN_ULONG *t, *f; BN_ULONG l; bn_check_top(r); bn_check_top(a); if (n < 0) { BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT); return 0; } nw = n / BN_BITS2; if (bn_wexpand(r, a->top + nw + 1) == NULL) return 0; r->neg = a->neg; lb = n % BN_BITS2; rb = BN_BITS2 - lb; f = a->d; t = r->d; t[a->top + nw] = 0; if (lb == 0) for (i = a->top - 1; i >= 0; i--) t[nw + i] = f[i]; else for (i = a->top - 1; i >= 0; i--) { l = f[i]; t[nw + i + 1] |= (l >> rb) & BN_MASK2; t[nw + i] = (l << lb) & BN_MASK2; } memset(t, 0, sizeof(*t) * nw); r->top = a->top + nw + 1; bn_correct_top(r); bn_check_top(r); return 1; }
['static int run_srp_kat(void)\n{\n int ret = 0;\n BIGNUM *s = NULL;\n BIGNUM *v = NULL;\n BIGNUM *a = NULL;\n BIGNUM *b = NULL;\n BIGNUM *u = NULL;\n BIGNUM *x = NULL;\n BIGNUM *Apub = NULL;\n BIGNUM *Bpub = NULL;\n BIGNUM *Kclient = NULL;\n BIGNUM *Kserver = NULL;\n const SRP_gN *GN;\n if (!TEST_ptr(GN = SRP_get_default_gN("1024")))\n goto err;\n BN_hex2bn(&s, "BEB25379D1A8581EB5A727673A2441EE");\n if (!TEST_true(SRP_create_verifier_BN("alice", "password123", &s, &v, GN->N,\n GN->g)))\n goto err;\n TEST_info("checking v");\n if (!TEST_true(check_bn("v", v,\n "7E273DE8696FFC4F4E337D05B4B375BEB0DDE1569E8FA00A9886D812"\n "9BADA1F1822223CA1A605B530E379BA4729FDC59F105B4787E5186F5"\n "C671085A1447B52A48CF1970B4FB6F8400BBF4CEBFBB168152E08AB5"\n "EA53D15C1AFF87B2B9DA6E04E058AD51CC72BFC9033B564E26480D78"\n "E955A5E29E7AB245DB2BE315E2099AFB")))\n goto err;\n TEST_note(" okay");\n BN_hex2bn(&b, "E487CB59D31AC550471E81F00F6928E01DDA08E974A004F49E61F5D1"\n "05284D20");\n Bpub = SRP_Calc_B(b, GN->N, GN->g, v);\n if (!TEST_true(SRP_Verify_B_mod_N(Bpub, GN->N)))\n goto err;\n TEST_info("checking B");\n if (!TEST_true(check_bn("B", Bpub,\n "BD0C61512C692C0CB6D041FA01BB152D4916A1E77AF46AE105393011"\n "BAF38964DC46A0670DD125B95A981652236F99D9B681CBF87837EC99"\n "6C6DA04453728610D0C6DDB58B318885D7D82C7F8DEB75CE7BD4FBAA"\n "37089E6F9C6059F388838E7A00030B331EB76840910440B1B27AAEAE"\n "EB4012B7D7665238A8E3FB004B117B58")))\n goto err;\n TEST_note(" okay");\n BN_hex2bn(&a, "60975527035CF2AD1989806F0407210BC81EDC04E2762A56AFD529DD"\n "DA2D4393");\n Apub = SRP_Calc_A(a, GN->N, GN->g);\n if (!TEST_true(SRP_Verify_A_mod_N(Apub, GN->N)))\n goto err;\n TEST_info("checking A");\n if (!TEST_true(check_bn("A", Apub,\n "61D5E490F6F1B79547B0704C436F523DD0E560F0C64115BB72557EC4"\n "4352E8903211C04692272D8B2D1A5358A2CF1B6E0BFCF99F921530EC"\n "8E39356179EAE45E42BA92AEACED825171E1E8B9AF6D9C03E1327F44"\n "BE087EF06530E69F66615261EEF54073CA11CF5858F0EDFDFE15EFEA"\n "B349EF5D76988A3672FAC47B0769447B")))\n goto err;\n TEST_note(" okay");\n u = SRP_Calc_u(Apub, Bpub, GN->N);\n if (!TEST_true(check_bn("u", u,\n "CE38B9593487DA98554ED47D70A7AE5F462EF019")))\n goto err;\n x = SRP_Calc_x(s, "alice", "password123");\n Kclient = SRP_Calc_client_key(GN->N, Bpub, GN->g, x, a, u);\n TEST_info("checking client\'s key");\n if (!TEST_true(check_bn("Client\'s key", Kclient,\n "B0DC82BABCF30674AE450C0287745E7990A3381F63B387AAF271A10D"\n "233861E359B48220F7C4693C9AE12B0A6F67809F0876E2D013800D6C"\n "41BB59B6D5979B5C00A172B4A2A5903A0BDCAF8A709585EB2AFAFA8F"\n "3499B200210DCC1F10EB33943CD67FC88A2F39A4BE5BEC4EC0A3212D"\n "C346D7E474B29EDE8A469FFECA686E5A")))\n goto err;\n TEST_note(" okay");\n Kserver = SRP_Calc_server_key(Apub, v, u, b, GN->N);\n TEST_info("checking server\'s key");\n if (!TEST_true(check_bn("Server\'s key", Kserver,\n "B0DC82BABCF30674AE450C0287745E7990A3381F63B387AAF271A10D"\n "233861E359B48220F7C4693C9AE12B0A6F67809F0876E2D013800D6C"\n "41BB59B6D5979B5C00A172B4A2A5903A0BDCAF8A709585EB2AFAFA8F"\n "3499B200210DCC1F10EB33943CD67FC88A2F39A4BE5BEC4EC0A3212D"\n "C346D7E474B29EDE8A469FFECA686E5A")))\n goto err;\n TEST_note(" okay");\n ret = 1;\nerr:\n BN_clear_free(Kclient);\n BN_clear_free(Kserver);\n BN_clear_free(x);\n BN_free(u);\n BN_free(Apub);\n BN_clear_free(a);\n BN_free(Bpub);\n BN_clear_free(b);\n BN_free(s);\n BN_clear_free(v);\n return ret;\n}', 'int SRP_create_verifier_BN(const char *user, const char *pass, BIGNUM **salt,\n BIGNUM **verifier, const BIGNUM *N,\n const BIGNUM *g)\n{\n int result = 0;\n BIGNUM *x = NULL;\n BN_CTX *bn_ctx = BN_CTX_new();\n unsigned char tmp2[MAX_LEN];\n BIGNUM *salttmp = NULL;\n if ((user == NULL) ||\n (pass == NULL) ||\n (salt == NULL) ||\n (verifier == NULL) || (N == NULL) || (g == NULL) || (bn_ctx == NULL))\n goto err;\n if (*salt == NULL) {\n if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)\n goto err;\n salttmp = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);\n } else {\n salttmp = *salt;\n }\n x = SRP_Calc_x(salttmp, user, pass);\n *verifier = BN_new();\n if (*verifier == NULL)\n goto err;\n if (!BN_mod_exp(*verifier, g, x, N, bn_ctx)) {\n BN_clear_free(*verifier);\n goto err;\n }\n result = 1;\n *salt = salttmp;\n err:\n if (salt != NULL && *salt != salttmp)\n BN_clear_free(salttmp);\n BN_clear_free(x);\n BN_CTX_free(bn_ctx);\n return result;\n}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n BN_CTX *ctx)\n{\n int ret;\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n if (BN_is_odd(m)) {\n# ifdef MONT_EXP_WORD\n if (a->top == 1 && !a->neg\n && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(a, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(m, BN_FLG_CONSTTIME) == 0)) {\n BN_ULONG A = a->d[0];\n ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);\n } else\n# endif\n ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);\n } else\n#endif\n#ifdef RECP_MUL_MOD\n {\n ret = BN_mod_exp_recp(r, a, p, m, ctx);\n }\n#else\n {\n ret = BN_mod_exp_simple(r, a, p, m, ctx);\n }\n#endif\n bn_check_top(r);\n return ret;\n}', 'int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n int start = 1;\n BIGNUM *d, *r;\n const BIGNUM *aa;\n BIGNUM *val[TABLE_SIZE];\n BN_MONT_CTX *mont = NULL;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(a, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont);\n }\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_is_one(m)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n d = BN_CTX_get(ctx);\n r = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (val[0] == NULL)\n goto err;\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n if (a->neg || BN_ucmp(a, m) >= 0) {\n if (!BN_nnmod(val[0], a, m, ctx))\n goto err;\n aa = val[0];\n } else\n aa = a;\n if (BN_is_zero(aa)) {\n BN_zero(rr);\n ret = 1;\n goto err;\n }\n if (!BN_to_montgomery(val[0], aa, mont, ctx))\n goto err;\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!BN_mod_mul_montgomery(d, val[0], val[0], mont, ctx))\n goto err;\n j = 1 << (window - 1);\n for (i = 1; i < j; i++) {\n if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul_montgomery(val[i], val[i - 1], d, mont, ctx))\n goto err;\n }\n }\n start = 1;\n wvalue = 0;\n wstart = bits - 1;\n wend = 0;\n#if 1\n j = m->top;\n if (m->d[j - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n if (bn_wexpand(r, j) == NULL)\n goto err;\n r->d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < j; i++)\n r->d[i] = (~m->d[i]) & BN_MASK2;\n r->top = j;\n bn_correct_top(r);\n } else\n#endif\n if (!BN_to_montgomery(r, BN_value_one(), mont, ctx))\n goto err;\n for (;;) {\n if (BN_is_bit_set(p, wstart) == 0) {\n if (!start) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (wstart == 0)\n break;\n wstart--;\n continue;\n }\n j = wstart;\n wvalue = 1;\n wend = 0;\n for (i = 1; i < window; i++) {\n if (wstart - i < 0)\n break;\n if (BN_is_bit_set(p, wstart - i)) {\n wvalue <<= (i - wend);\n wvalue |= 1;\n wend = i;\n }\n }\n j = wend + 1;\n if (!start)\n for (i = 0; i < j; i++) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (!BN_mod_mul_montgomery(r, r, val[wvalue >> 1], mont, ctx))\n goto err;\n wstart -= wend + 1;\n wvalue = 0;\n start = 0;\n if (wstart < 0)\n break;\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n j = mont->N.top;\n val[0]->d[0] = 1;\n for (i = 1; i < j; i++)\n val[0]->d[i] = 0;\n val[0]->top = j;\n if (!BN_mod_mul_montgomery(rr, r, val[0], mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, r, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n BN_CTX_end(ctx);\n bn_check_top(rr);\n return ret;\n}', 'int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx,\n BN_MONT_CTX *in_mont)\n{\n int i, bits, ret = 0, window, wvalue;\n int top;\n BN_MONT_CTX *mont = NULL;\n int numPowers;\n unsigned char *powerbufFree = NULL;\n int powerbufLen = 0;\n unsigned char *powerbuf = NULL;\n BIGNUM tmp, am;\n#if defined(SPARC_T4_MONT)\n unsigned int t4 = 0;\n#endif\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT_CONSTTIME, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n top = m->top;\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_is_one(m)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n#ifdef RSAZ_ENABLED\n if ((16 == a->top) && (16 == p->top) && (BN_num_bits(m) == 1024)\n && rsaz_avx2_eligible()) {\n if (NULL == bn_wexpand(rr, 16))\n goto err;\n RSAZ_1024_mod_exp_avx2(rr->d, a->d, p->d, m->d, mont->RR.d,\n mont->n0[0]);\n rr->top = 16;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n } else if ((8 == a->top) && (8 == p->top) && (BN_num_bits(m) == 512)) {\n if (NULL == bn_wexpand(rr, 8))\n goto err;\n RSAZ_512_mod_exp(rr->d, a->d, p->d, m->d, mont->n0[0], mont->RR.d);\n rr->top = 8;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n }\n#endif\n window = BN_window_bits_for_ctime_exponent_size(bits);\n#if defined(SPARC_T4_MONT)\n if (window >= 5 && (top & 15) == 0 && top <= 64 &&\n (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) ==\n (CFR_MONTMUL | CFR_MONTSQR) && (t4 = OPENSSL_sparcv9cap_P[0]))\n window = 5;\n else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window >= 5) {\n window = 5;\n powerbufLen += top * sizeof(mont->N.d[0]);\n }\n#endif\n (void)0;\n numPowers = 1 << window;\n powerbufLen += sizeof(m->d[0]) * (top * numPowers +\n ((2 * top) >\n numPowers ? (2 * top) : numPowers));\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree =\n alloca(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH);\n else\n#endif\n if ((powerbufFree =\n OPENSSL_malloc(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH))\n == NULL)\n goto err;\n powerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);\n memset(powerbuf, 0, powerbufLen);\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree = NULL;\n#endif\n tmp.d = (BN_ULONG *)(powerbuf + sizeof(m->d[0]) * top * numPowers);\n am.d = tmp.d + top;\n tmp.top = am.top = 0;\n tmp.dmax = am.dmax = top;\n tmp.neg = am.neg = 0;\n tmp.flags = am.flags = BN_FLG_STATIC_DATA;\n#if 1\n if (m->d[top - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n tmp.d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < top; i++)\n tmp.d[i] = (~m->d[i]) & BN_MASK2;\n tmp.top = top;\n } else\n#endif\n if (!BN_to_montgomery(&tmp, BN_value_one(), mont, ctx))\n goto err;\n if (a->neg || BN_ucmp(a, m) >= 0) {\n if (!BN_mod(&am, a, m, ctx))\n goto err;\n if (!BN_to_montgomery(&am, &am, mont, ctx))\n goto err;\n } else if (!BN_to_montgomery(&am, a, mont, ctx))\n goto err;\n#if defined(SPARC_T4_MONT)\n if (t4) {\n typedef int (*bn_pwr5_mont_f) (BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_8(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_16(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_24(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_32(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n static const bn_pwr5_mont_f pwr5_funcs[4] = {\n bn_pwr5_mont_t4_8, bn_pwr5_mont_t4_16,\n bn_pwr5_mont_t4_24, bn_pwr5_mont_t4_32\n };\n bn_pwr5_mont_f pwr5_worker = pwr5_funcs[top / 16 - 1];\n typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap, const void *bp,\n const BN_ULONG *np, const BN_ULONG *n0);\n int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n static const bn_mul_mont_f mul_funcs[4] = {\n bn_mul_mont_t4_8, bn_mul_mont_t4_16,\n bn_mul_mont_t4_24, bn_mul_mont_t4_32\n };\n bn_mul_mont_f mul_worker = mul_funcs[top / 16 - 1];\n void bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_gather5_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_flip_n_scatter5_t4(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5_t4(BN_ULONG *out, size_t num,\n void *table, size_t power);\n void bn_flip_t4(BN_ULONG *dst, BN_ULONG *src, size_t num);\n BN_ULONG *np = mont->N.d, *n0 = mont->n0;\n int stride = 5 * (6 - (top / 16 - 1));\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 0);\n bn_flip_n_scatter5_t4(am.d, top, powerbuf, 1);\n if (!(*mul_worker) (tmp.d, am.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, am.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, am.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 2);\n for (i = 3; i < 32; i++) {\n if (!(*mul_worker) (tmp.d, tmp.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, tmp.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, tmp.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, i);\n }\n np = alloca(top * sizeof(BN_ULONG));\n top /= 2;\n bn_flip_t4(np, mont->N.d, top);\n bits--;\n for (wvalue = 0, i = bits % 5; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_gather5_t4(tmp.d, top, powerbuf, wvalue);\n while (bits >= 0) {\n if (bits < stride)\n stride = bits + 1;\n bits -= stride;\n wvalue = bn_get_bits(p, bits + 1);\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n bits += stride - 5;\n wvalue >>= stride - 5;\n wvalue &= 31;\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5_t4(tmp.d, tmp.d, powerbuf, np, n0, top,\n wvalue);\n }\n bn_flip_t4(tmp.d, tmp.d, top);\n top *= 2;\n tmp.top = top;\n bn_correct_top(&tmp);\n OPENSSL_cleanse(np, top * sizeof(BN_ULONG));\n } else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window == 5 && top > 1) {\n void bn_mul_mont_gather5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_scatter5(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5(BN_ULONG *out, size_t num, void *table, size_t power);\n void bn_power5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n int bn_get_bits5(const BN_ULONG *ap, int off);\n int bn_from_montgomery(BN_ULONG *rp, const BN_ULONG *ap,\n const BN_ULONG *not_used, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n BN_ULONG *n0 = mont->n0, *np;\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n for (np = am.d + top, i = 0; i < top; i++)\n np[i] = mont->N.d[i];\n bn_scatter5(tmp.d, top, powerbuf, 0);\n bn_scatter5(am.d, am.top, powerbuf, 1);\n bn_mul_mont(tmp.d, am.d, am.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2);\n# if 0\n for (i = 3; i < 32; i++) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# else\n for (i = 4; i < 32; i *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n for (i = 3; i < 8; i += 2) {\n int j;\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n for (j = 2 * i; j < 32; j *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, j);\n }\n }\n for (; i < 16; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2 * i);\n }\n for (; i < 32; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# endif\n bits--;\n for (wvalue = 0, i = bits % 5; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_gather5(tmp.d, top, powerbuf, wvalue);\n if (top & 7)\n while (bits >= 0) {\n for (wvalue = 0, i = 0; i < 5; i++, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5(tmp.d, tmp.d, powerbuf, np, n0, top,\n wvalue);\n } else {\n while (bits >= 0) {\n wvalue = bn_get_bits5(p->d, bits - 4);\n bits -= 5;\n bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top, wvalue);\n }\n }\n ret = bn_from_montgomery(tmp.d, tmp.d, NULL, np, n0, top);\n tmp.top = top;\n bn_correct_top(&tmp);\n if (ret) {\n if (!BN_copy(rr, &tmp))\n ret = 0;\n goto err;\n }\n } else\n#endif\n {\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 0, window))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, window))\n goto err;\n if (window > 1) {\n if (!BN_mod_mul_montgomery(&tmp, &am, &am, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 2,\n window))\n goto err;\n for (i = 3; i < numPowers; i++) {\n if (!BN_mod_mul_montgomery(&tmp, &am, &tmp, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, i,\n window))\n goto err;\n }\n }\n bits--;\n for (wvalue = 0, i = bits % window; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&tmp, top, powerbuf, wvalue,\n window))\n goto err;\n while (bits >= 0) {\n wvalue = 0;\n for (i = 0; i < window; i++, bits--) {\n if (!BN_mod_mul_montgomery(&tmp, &tmp, &tmp, mont, ctx))\n goto err;\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n }\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&am, top, powerbuf, wvalue,\n window))\n goto err;\n if (!BN_mod_mul_montgomery(&tmp, &tmp, &am, mont, ctx))\n goto err;\n }\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n am.d[0] = 1;\n for (i = 1; i < top; i++)\n am.d[i] = 0;\n if (!BN_mod_mul_montgomery(rr, &tmp, &am, mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, &tmp, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n if (powerbuf != NULL) {\n OPENSSL_cleanse(powerbuf, powerbufLen);\n OPENSSL_free(powerbufFree);\n }\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return 0;\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return 0;\n }\n if (dv != NULL)\n BN_zero(dv);\n return 1;\n }\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n resp++;\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n resp--;\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return 0;\n}', 'int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l;\n bn_check_top(r);\n bn_check_top(a);\n if (n < 0) {\n BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n nw = n / BN_BITS2;\n if (bn_wexpand(r, a->top + nw + 1) == NULL)\n return 0;\n r->neg = a->neg;\n lb = n % BN_BITS2;\n rb = BN_BITS2 - lb;\n f = a->d;\n t = r->d;\n t[a->top + nw] = 0;\n if (lb == 0)\n for (i = a->top - 1; i >= 0; i--)\n t[nw + i] = f[i];\n else\n for (i = a->top - 1; i >= 0; i--) {\n l = f[i];\n t[nw + i + 1] |= (l >> rb) & BN_MASK2;\n t[nw + i] = (l << lb) & BN_MASK2;\n }\n memset(t, 0, sizeof(*t) * nw);\n r->top = a->top + nw + 1;\n bn_correct_top(r);\n bn_check_top(r);\n return 1;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']
50
0
https://github.com/openssl/openssl/blob/9f519addc09b2005fa8c6cde36e3267de02577bb/crypto/err/err.c/#L569
static unsigned long get_error_values(int inc, int top, const char **file, int *line, const char **data, int *flags) { int i = 0; ERR_STATE *es; unsigned long ret; es = ERR_get_state(); if (inc && top) { if (file) *file = ""; if (line) *line = 0; if (data) *data = ""; if (flags) *flags = 0; return ERR_R_INTERNAL_ERROR; } if (es->bottom == es->top) return 0; if (top) i = es->top; else i = (es->bottom + 1) % ERR_NUM_ERRORS; ret = es->err_buffer[i]; if (inc) { es->bottom = i; es->err_buffer[i] = 0; } if ((file != NULL) && (line != NULL)) { if (es->err_file[i] == NULL) { *file = "NA"; if (line != NULL) *line = 0; } else { *file = es->err_file[i]; if (line != NULL) *line = es->err_line[i]; } } if (data == NULL) { if (inc) { err_clear_data(es, i); } } else { if (es->err_data[i] == NULL) { *data = ""; if (flags != NULL) *flags = 0; } else { *data = es->err_data[i]; if (flags != NULL) *flags = es->err_data_flags[i]; } } return ret; }
['static unsigned long get_error_values(int inc, int top, const char **file,\n int *line, const char **data,\n int *flags)\n{\n int i = 0;\n ERR_STATE *es;\n unsigned long ret;\n es = ERR_get_state();\n if (inc && top) {\n if (file)\n *file = "";\n if (line)\n *line = 0;\n if (data)\n *data = "";\n if (flags)\n *flags = 0;\n return ERR_R_INTERNAL_ERROR;\n }\n if (es->bottom == es->top)\n return 0;\n if (top)\n i = es->top;\n else\n i = (es->bottom + 1) % ERR_NUM_ERRORS;\n ret = es->err_buffer[i];\n if (inc) {\n es->bottom = i;\n es->err_buffer[i] = 0;\n }\n if ((file != NULL) && (line != NULL)) {\n if (es->err_file[i] == NULL) {\n *file = "NA";\n if (line != NULL)\n *line = 0;\n } else {\n *file = es->err_file[i];\n if (line != NULL)\n *line = es->err_line[i];\n }\n }\n if (data == NULL) {\n if (inc) {\n err_clear_data(es, i);\n }\n } else {\n if (es->err_data[i] == NULL) {\n *data = "";\n if (flags != NULL)\n *flags = 0;\n } else {\n *data = es->err_data[i];\n if (flags != NULL)\n *flags = es->err_data_flags[i];\n }\n }\n return ret;\n}', 'ERR_STATE *ERR_get_state(void)\n{\n ERR_STATE *state = NULL;\n CRYPTO_THREAD_run_once(&err_init, err_do_init);\n state = CRYPTO_THREAD_get_local(&err_thread_local);\n if (state == NULL) {\n state = OPENSSL_zalloc(sizeof(*state));\n if (state == NULL)\n return NULL;\n if (!CRYPTO_THREAD_set_local(&err_thread_local, state)) {\n ERR_STATE_free(state);\n return NULL;\n }\n OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);\n ossl_init_thread_start(OPENSSL_INIT_THREAD_ERR_STATE);\n }\n return state;\n}', 'int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))\n{\n if (pthread_once(once, init) != 0)\n return 0;\n return 1;\n}', 'void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)\n{\n return pthread_getspecific(*key);\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num <= 0)\n return NULL;\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n osslargused(file); osslargused(line);\n ret = malloc(num);\n#endif\n return ret;\n}', 'int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)\n{\n if (pthread_setspecific(*key, val) != 0)\n return 0;\n return 1;\n}']
51
0
https://github.com/libav/libav/blob/28c1115a915e4e198bfb6bd39909b2d1327c1454/avconv.c/#L1321
static void do_video_stats(AVFormatContext *os, OutputStream *ost, int frame_size) { AVCodecContext *enc; int frame_number; double ti1, bitrate, avg_bitrate; if (!vstats_file) { vstats_file = fopen(vstats_filename, "w"); if (!vstats_file) { perror("fopen"); exit_program(1); } } enc = ost->st->codec; if (enc->codec_type == AVMEDIA_TYPE_VIDEO) { frame_number = ost->frame_number; fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number, enc->coded_frame->quality/(float)FF_QP2LAMBDA); if (enc->flags&CODEC_FLAG_PSNR) fprintf(vstats_file, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0]/(enc->width*enc->height*255.0*255.0))); fprintf(vstats_file,"f_size= %6d ", frame_size); ti1 = ost->sync_opts * av_q2d(enc->time_base); if (ti1 < 0.01) ti1 = 0.01; bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0; avg_bitrate = (double)(video_size * 8) / ti1 / 1000.0; fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ", (double)video_size / 1024, ti1, bitrate, avg_bitrate); fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(enc->coded_frame->pict_type)); } }
['static void do_video_stats(AVFormatContext *os, OutputStream *ost,\n int frame_size)\n{\n AVCodecContext *enc;\n int frame_number;\n double ti1, bitrate, avg_bitrate;\n if (!vstats_file) {\n vstats_file = fopen(vstats_filename, "w");\n if (!vstats_file) {\n perror("fopen");\n exit_program(1);\n }\n }\n enc = ost->st->codec;\n if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {\n frame_number = ost->frame_number;\n fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number, enc->coded_frame->quality/(float)FF_QP2LAMBDA);\n if (enc->flags&CODEC_FLAG_PSNR)\n fprintf(vstats_file, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0]/(enc->width*enc->height*255.0*255.0)));\n fprintf(vstats_file,"f_size= %6d ", frame_size);\n ti1 = ost->sync_opts * av_q2d(enc->time_base);\n if (ti1 < 0.01)\n ti1 = 0.01;\n bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;\n avg_bitrate = (double)(video_size * 8) / ti1 / 1000.0;\n fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",\n (double)video_size / 1024, ti1, bitrate, avg_bitrate);\n fprintf(vstats_file, "type= %c\\n", av_get_picture_type_char(enc->coded_frame->pict_type));\n }\n}']
52
0
https://github.com/libav/libav/blob/f5a2c9816e0b58edf2a87297be8d648631fc3432/libavformat/rtpdec.c/#L361
static int rtp_parse_mp4_au(RTPDemuxContext *s, const uint8_t *buf) { int au_headers_length, au_header_size, i; GetBitContext getbitcontext; rtp_payload_data_t *infos; infos = s->rtp_payload_data; if (infos == NULL) return -1; au_headers_length = AV_RB16(buf); if (au_headers_length > RTP_MAX_PACKET_LENGTH) return -1; infos->au_headers_length_bytes = (au_headers_length + 7) / 8; buf += 2; init_get_bits(&getbitcontext, buf, infos->au_headers_length_bytes * 8); au_header_size = infos->sizelength + infos->indexlength; if (au_header_size <= 0 || (au_headers_length % au_header_size != 0)) return -1; infos->nb_au_headers = au_headers_length / au_header_size; infos->au_headers = av_malloc(sizeof(struct AUHeaders) * infos->nb_au_headers); infos->au_headers[0].size = 0; infos->au_headers[0].index = 0; for (i = 0; i < infos->nb_au_headers; ++i) { infos->au_headers[0].size += get_bits_long(&getbitcontext, infos->sizelength); infos->au_headers[0].index = get_bits_long(&getbitcontext, infos->indexlength); } infos->nb_au_headers = 1; return 0; }
['static int rtp_parse_mp4_au(RTPDemuxContext *s, const uint8_t *buf)\n{\n int au_headers_length, au_header_size, i;\n GetBitContext getbitcontext;\n rtp_payload_data_t *infos;\n infos = s->rtp_payload_data;\n if (infos == NULL)\n return -1;\n au_headers_length = AV_RB16(buf);\n if (au_headers_length > RTP_MAX_PACKET_LENGTH)\n return -1;\n infos->au_headers_length_bytes = (au_headers_length + 7) / 8;\n buf += 2;\n init_get_bits(&getbitcontext, buf, infos->au_headers_length_bytes * 8);\n au_header_size = infos->sizelength + infos->indexlength;\n if (au_header_size <= 0 || (au_headers_length % au_header_size != 0))\n return -1;\n infos->nb_au_headers = au_headers_length / au_header_size;\n infos->au_headers = av_malloc(sizeof(struct AUHeaders) * infos->nb_au_headers);\n infos->au_headers[0].size = 0;\n infos->au_headers[0].index = 0;\n for (i = 0; i < infos->nb_au_headers; ++i) {\n infos->au_headers[0].size += get_bits_long(&getbitcontext, infos->sizelength);\n infos->au_headers[0].index = get_bits_long(&getbitcontext, infos->indexlength);\n }\n infos->nb_au_headers = 1;\n return 0;\n}', 'static inline void init_get_bits(GetBitContext *s,\n const uint8_t *buffer, int bit_size)\n{\n int buffer_size= (bit_size+7)>>3;\n if(buffer_size < 0 || bit_size < 0) {\n buffer_size = bit_size = 0;\n buffer = NULL;\n }\n s->buffer= buffer;\n s->size_in_bits= bit_size;\n s->buffer_end= buffer + buffer_size;\n#ifdef ALT_BITSTREAM_READER\n s->index=0;\n#elif defined LIBMPEG2_BITSTREAM_READER\n s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));\n s->bit_count = 16 + 8*((intptr_t)buffer&1);\n skip_bits_long(s, 0);\n#elif defined A32_BITSTREAM_READER\n s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));\n s->bit_count = 32 + 8*((intptr_t)buffer&3);\n skip_bits_long(s, 0);\n#endif\n}', 'void *av_malloc(unsigned int size)\n{\n void *ptr;\n#ifdef CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-16) )\n return NULL;\n#ifdef CONFIG_MEMALIGN_HACK\n ptr = malloc(size+16);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&15) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif defined (HAVE_MEMALIGN)\n ptr = memalign(16,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}', '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}', '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}']
53
0
https://github.com/openssl/openssl/blob/92eb4c551d7433ba1e74e77001dab2e256f8a870/crypto/x509/x509_lu.c/#L666
int X509_STORE_CTX_get1_issuer(X509 **issuer, X509_STORE_CTX *ctx, X509 *x) { X509_NAME *xn; X509_OBJECT obj, *pobj; int i, ok, idx, ret; xn=X509_get_issuer_name(x); ok=X509_STORE_get_by_subject(ctx,X509_LU_X509,xn,&obj); if (ok != X509_LU_X509) { if (ok == X509_LU_RETRY) { X509_OBJECT_free_contents(&obj); X509err(X509_F_X509_STORE_CTX_GET1_ISSUER,X509_R_SHOULD_RETRY); return -1; } else if (ok != X509_LU_FAIL) { X509_OBJECT_free_contents(&obj); return -1; } return 0; } if (ctx->check_issued(ctx, x, obj.data.x509)) { *issuer = obj.data.x509; return 1; } X509_OBJECT_free_contents(&obj); ret = 0; CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE); idx = X509_OBJECT_idx_by_subject(ctx->ctx->objs, X509_LU_X509, xn); if (idx != -1) { for (i = idx; i < sk_X509_OBJECT_num(ctx->ctx->objs); i++) { pobj = sk_X509_OBJECT_value(ctx->ctx->objs, i); if (pobj->type != X509_LU_X509) break; if (X509_NAME_cmp(xn, X509_get_subject_name(pobj->data.x509))) break; if (ctx->check_issued(ctx, x, pobj->data.x509)) { *issuer = pobj->data.x509; X509_OBJECT_up_ref_count(pobj); ret = 1; break; } } } CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE); return ret; }
['int X509_STORE_CTX_get1_issuer(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)\n\t{\n\tX509_NAME *xn;\n\tX509_OBJECT obj, *pobj;\n\tint i, ok, idx, ret;\n\txn=X509_get_issuer_name(x);\n\tok=X509_STORE_get_by_subject(ctx,X509_LU_X509,xn,&obj);\n\tif (ok != X509_LU_X509)\n\t\t{\n\t\tif (ok == X509_LU_RETRY)\n\t\t\t{\n\t\t\tX509_OBJECT_free_contents(&obj);\n\t\t\tX509err(X509_F_X509_STORE_CTX_GET1_ISSUER,X509_R_SHOULD_RETRY);\n\t\t\treturn -1;\n\t\t\t}\n\t\telse if (ok != X509_LU_FAIL)\n\t\t\t{\n\t\t\tX509_OBJECT_free_contents(&obj);\n\t\t\treturn -1;\n\t\t\t}\n\t\treturn 0;\n\t\t}\n\tif (ctx->check_issued(ctx, x, obj.data.x509))\n\t\t{\n\t\t*issuer = obj.data.x509;\n\t\treturn 1;\n\t\t}\n\tX509_OBJECT_free_contents(&obj);\n\tret = 0;\n\tCRYPTO_w_lock(CRYPTO_LOCK_X509_STORE);\n\tidx = X509_OBJECT_idx_by_subject(ctx->ctx->objs, X509_LU_X509, xn);\n\tif (idx != -1)\n\t\t{\n\t\tfor (i = idx; i < sk_X509_OBJECT_num(ctx->ctx->objs); i++)\n\t\t\t{\n\t\t\tpobj = sk_X509_OBJECT_value(ctx->ctx->objs, i);\n\t\t\tif (pobj->type != X509_LU_X509)\n\t\t\t\tbreak;\n\t\t\tif (X509_NAME_cmp(xn, X509_get_subject_name(pobj->data.x509)))\n\t\t\t\tbreak;\n\t\t\tif (ctx->check_issued(ctx, x, pobj->data.x509))\n\t\t\t\t{\n\t\t\t\t*issuer = pobj->data.x509;\n\t\t\t\tX509_OBJECT_up_ref_count(pobj);\n\t\t\t\tret = 1;\n\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\tCRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);\n\treturn ret;\n\t}', 'X509_NAME *X509_get_issuer_name(X509 *a)\n\t{\n\treturn(a->cert_info->issuer);\n\t}', 'void X509_OBJECT_free_contents(X509_OBJECT *a)\n\t{\n\tswitch (a->type)\n\t\t{\n\tcase X509_LU_X509:\n\t\tX509_free(a->data.x509);\n\t\tbreak;\n\tcase X509_LU_CRL:\n\t\tX509_CRL_free(a->data.crl);\n\t\tbreak;\n\t\t}\n\t}', 'void CRYPTO_lock(int mode, int type, const char *file, int line)\n\t{\n#ifdef LOCK_DEBUG\n\t\t{\n\t\tCRYPTO_THREADID id;\n\t\tchar *rw_text,*operation_text;\n\t\tif (mode & CRYPTO_LOCK)\n\t\t\toperation_text="lock ";\n\t\telse if (mode & CRYPTO_UNLOCK)\n\t\t\toperation_text="unlock";\n\t\telse\n\t\t\toperation_text="ERROR ";\n\t\tif (mode & CRYPTO_READ)\n\t\t\trw_text="r";\n\t\telse if (mode & CRYPTO_WRITE)\n\t\t\trw_text="w";\n\t\telse\n\t\t\trw_text="ERROR";\n\t\tCRYPTO_THREADID_current(&id);\n\t\tfprintf(stderr,"lock:%08lx:(%s)%s %-18s %s:%d\\n",\n\t\t\tCRYPTO_THREADID_hash(&id), rw_text, operation_text,\n\t\t\tCRYPTO_get_lock_name(type), file, line);\n\t\t}\n#endif\n\tif (type < 0)\n\t\t{\n\t\tif (dynlock_lock_callback != NULL)\n\t\t\t{\n\t\t\tstruct CRYPTO_dynlock_value *pointer\n\t\t\t\t= CRYPTO_get_dynlock_value(type);\n\t\t\tOPENSSL_assert(pointer != NULL);\n\t\t\tdynlock_lock_callback(mode, pointer, file, line);\n\t\t\tCRYPTO_destroy_dynlockid(type);\n\t\t\t}\n\t\t}\n\telse\n\t\tif (locking_callback != NULL)\n\t\t\tlocking_callback(mode,type,file,line);\n\t}', 'int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, int type,\n\t X509_NAME *name)\n\t{\n\treturn x509_object_idx_cnt(h, type, name, NULL);\n\t}', 'int sk_num(const _STACK *st)\n{\n\tif(st == NULL) return -1;\n\treturn st->num;\n}', 'void *sk_value(const _STACK *st, int i)\n{\n\tif(!st || (i < 0) || (i >= st->num)) return NULL;\n\treturn st->data[i];\n}']
54
0
https://github.com/openssl/openssl/blob/a6a66e4511eec0f4ecc2943117a42b3723eb2222/crypto/bn/bn_ctx.c/#L300
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['BIGNUM *SRP_Calc_server_key(const BIGNUM *A, const BIGNUM *v, const BIGNUM *u,\n const BIGNUM *b, const BIGNUM *N)\n{\n BIGNUM *tmp = NULL, *S = NULL;\n BN_CTX *bn_ctx;\n if (u == NULL || A == NULL || v == NULL || b == NULL || N == NULL)\n return NULL;\n if ((bn_ctx = BN_CTX_new()) == NULL || (tmp = BN_new()) == NULL)\n goto err;\n if (!BN_mod_exp(tmp, v, u, N, bn_ctx))\n goto err;\n if (!BN_mod_mul(tmp, A, tmp, N, bn_ctx))\n goto err;\n S = BN_new();\n if (S != NULL && !BN_mod_exp(S, tmp, b, N, bn_ctx)) {\n BN_free(S);\n S = NULL;\n }\n err:\n BN_CTX_free(bn_ctx);\n BN_clear_free(tmp);\n return S;\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_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("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(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 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}']
55
0
https://github.com/openssl/openssl/blob/ea32151f7b9353f8906188d007c6893704ac17bb/crypto/rsa/rsa_lib.c/#L121
RSA *RSA_new_method(ENGINE *engine) { RSA *ret = OPENSSL_zalloc(sizeof(*ret)); if (ret == NULL) { RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE); return NULL; } ret->references = 1; ret->lock = CRYPTO_THREAD_lock_new(); if (ret->lock == NULL) { RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE); OPENSSL_free(ret); return NULL; } ret->meth = RSA_get_default_method(); #ifndef OPENSSL_NO_ENGINE ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW; if (engine) { if (!ENGINE_init(engine)) { RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB); goto err; } ret->engine = engine; } else ret->engine = ENGINE_get_default_RSA(); if (ret->engine) { ret->meth = ENGINE_get_RSA(ret->engine); if (ret->meth == NULL) { RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB); goto err; } } #endif ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW; if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) { goto err; } if ((ret->meth->init != NULL) && !ret->meth->init(ret)) { RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_INIT_FAIL); goto err; } return ret; err: RSA_free(ret); return NULL; }
['RSA *RSA_new_method(ENGINE *engine)\n{\n RSA *ret = OPENSSL_zalloc(sizeof(*ret));\n if (ret == NULL) {\n RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n ret->references = 1;\n ret->lock = CRYPTO_THREAD_lock_new();\n if (ret->lock == NULL) {\n RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);\n OPENSSL_free(ret);\n return NULL;\n }\n ret->meth = RSA_get_default_method();\n#ifndef OPENSSL_NO_ENGINE\n ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;\n if (engine) {\n if (!ENGINE_init(engine)) {\n RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);\n goto err;\n }\n ret->engine = engine;\n } else\n ret->engine = ENGINE_get_default_RSA();\n if (ret->engine) {\n ret->meth = ENGINE_get_RSA(ret->engine);\n if (ret->meth == NULL) {\n RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);\n goto err;\n }\n }\n#endif\n ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;\n if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) {\n goto err;\n }\n if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {\n RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_INIT_FAIL);\n goto err;\n }\n return ret;\nerr:\n RSA_free(ret);\n return NULL;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (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}', 'const RSA_METHOD *RSA_get_default_method(void)\n{\n if (default_RSA_meth == NULL) {\n#ifdef RSA_NULL\n default_RSA_meth = RSA_null_method();\n#else\n default_RSA_meth = RSA_PKCS1_OpenSSL();\n#endif\n }\n return default_RSA_meth;\n}', 'ENGINE *ENGINE_get_default_RSA(void)\n{\n return engine_table_select(&rsa_table, dummy_nid);\n}', 'ENGINE *engine_table_select(ENGINE_TABLE **table, int nid)\n#else\nENGINE *engine_table_select_tmp(ENGINE_TABLE **table, int nid, const char *f,\n int l)\n#endif\n{\n ENGINE *ret = NULL;\n ENGINE_PILE tmplate, *fnd = NULL;\n int initres, loop = 0;\n if (!(*table)) {\n#ifdef ENGINE_TABLE_DEBUG\n fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, nothing "\n "registered!\\n", f, l, nid);\n#endif\n return NULL;\n }\n ERR_set_mark();\n CRYPTO_THREAD_write_lock(global_engine_lock);\n if (!int_table_check(table, 0))\n goto end;\n tmplate.nid = nid;\n fnd = lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate);\n if (!fnd)\n goto end;\n if (fnd->funct && engine_unlocked_init(fnd->funct)) {\n#ifdef ENGINE_TABLE_DEBUG\n fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, using "\n "ENGINE \'%s\' cached\\n", f, l, nid, fnd->funct->id);\n#endif\n ret = fnd->funct;\n goto end;\n }\n if (fnd->uptodate) {\n ret = fnd->funct;\n goto end;\n }\n trynext:\n ret = sk_ENGINE_value(fnd->sk, loop++);\n if (!ret) {\n#ifdef ENGINE_TABLE_DEBUG\n fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, no "\n "registered implementations would initialise\\n", f, l, nid);\n#endif\n goto end;\n }\n if ((ret->funct_ref > 0) || !(table_flags & ENGINE_TABLE_FLAG_NOINIT))\n initres = engine_unlocked_init(ret);\n else\n initres = 0;\n if (initres) {\n if ((fnd->funct != ret) && engine_unlocked_init(ret)) {\n if (fnd->funct)\n engine_unlocked_finish(fnd->funct, 0);\n fnd->funct = ret;\n#ifdef ENGINE_TABLE_DEBUG\n fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, "\n "setting default to \'%s\'\\n", f, l, nid, ret->id);\n#endif\n }\n#ifdef ENGINE_TABLE_DEBUG\n fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, using "\n "newly initialised \'%s\'\\n", f, l, nid, ret->id);\n#endif\n goto end;\n }\n goto trynext;\n end:\n if (fnd)\n fnd->uptodate = 1;\n#ifdef ENGINE_TABLE_DEBUG\n if (ret)\n fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, caching "\n "ENGINE \'%s\'\\n", f, l, nid, ret->id);\n else\n fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, caching "\n "\'no matching ENGINE\'\\n", f, l, nid);\n#endif\n CRYPTO_THREAD_unlock(global_engine_lock);\n ERR_pop_to_mark();\n return ret;\n}', 'int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)\n{\n int mx, i;\n void *ptr;\n EX_CALLBACK **storage = NULL;\n EX_CALLBACK *stack[10];\n EX_CALLBACKS *ip = get_and_lock(class_index);\n if (ip == NULL)\n return 0;\n ad->sk = NULL;\n mx = sk_EX_CALLBACK_num(ip->meth);\n if (mx > 0) {\n if (mx < (int)OSSL_NELEM(stack))\n storage = stack;\n else\n storage = OPENSSL_malloc(sizeof(*storage) * mx);\n if (storage != NULL)\n for (i = 0; i < mx; i++)\n storage[i] = sk_EX_CALLBACK_value(ip->meth, i);\n }\n CRYPTO_THREAD_unlock(ex_data_lock);\n if (mx > 0 && storage == NULL) {\n CRYPTOerr(CRYPTO_F_CRYPTO_NEW_EX_DATA, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n for (i = 0; i < mx; i++) {\n if (storage[i] && storage[i]->new_func) {\n ptr = CRYPTO_get_ex_data(ad, i);\n storage[i]->new_func(obj, ptr, ad, i,\n storage[i]->argl, storage[i]->argp);\n }\n }\n if (storage != stack)\n OPENSSL_free(storage);\n return 1;\n}', 'static EX_CALLBACKS *get_and_lock(int class_index)\n{\n EX_CALLBACKS *ip;\n if (class_index < 0 || class_index >= CRYPTO_EX_INDEX__COUNT) {\n CRYPTOerr(CRYPTO_F_GET_AND_LOCK, ERR_R_PASSED_INVALID_ARGUMENT);\n return NULL;\n }\n CRYPTO_THREAD_run_once(&ex_data_init, do_ex_data_init);\n if (ex_data_lock == NULL) {\n return NULL;\n }\n ip = &ex_data[class_index];\n CRYPTO_THREAD_write_lock(ex_data_lock);\n return ip;\n}', 'int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))\n{\n if (pthread_once(once, init) != 0)\n return 0;\n return 1;\n}']
56
0
https://github.com/libav/libav/blob/2f99117f6ff24ce5be2abb9e014cb8b86c2aa0e0/libavcodec/h264_cabac.c/#L1270
void ff_h264_init_cabac_states(const H264Context *h, H264SliceContext *sl) { int i; const int8_t (*tab)[2]; const int slice_qp = av_clip(sl->qscale - 6*(h->ps.sps->bit_depth_luma-8), 0, 51); if (sl->slice_type_nos == AV_PICTURE_TYPE_I) tab = cabac_context_init_I; else tab = cabac_context_init_PB[sl->cabac_init_idc]; for( i= 0; i < 1024; i++ ) { int pre = 2*(((tab[i][0] * slice_qp) >>4 ) + tab[i][1]) - 127; pre^= pre>>31; if(pre > 124) pre= 124 + (pre&1); sl->cabac_state[i] = pre; } }
['static int h264_decode_frame(AVCodecContext *avctx, void *data,\n int *got_frame, AVPacket *avpkt)\n{\n const uint8_t *buf = avpkt->data;\n int buf_size = avpkt->size;\n H264Context *h = avctx->priv_data;\n AVFrame *pict = data;\n int buf_index = 0;\n int ret;\n const uint8_t *new_extradata;\n int new_extradata_size;\n h->flags = avctx->flags;\n h->setup_finished = 0;\n h->nb_slice_ctx_queued = 0;\nout:\n if (buf_size == 0) {\n H264Picture *out;\n int i, out_idx;\n h->cur_pic_ptr = NULL;\n out = h->delayed_pic[0];\n out_idx = 0;\n for (i = 1;\n h->delayed_pic[i] &&\n !h->delayed_pic[i]->f->key_frame &&\n !h->delayed_pic[i]->mmco_reset;\n i++)\n if (h->delayed_pic[i]->poc < out->poc) {\n out = h->delayed_pic[i];\n out_idx = i;\n }\n for (i = out_idx; h->delayed_pic[i]; i++)\n h->delayed_pic[i] = h->delayed_pic[i + 1];\n if (out) {\n ret = output_frame(h, pict, out->f);\n if (ret < 0)\n return ret;\n *got_frame = 1;\n }\n return buf_index;\n }\n new_extradata_size = 0;\n new_extradata = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA,\n &new_extradata_size);\n if (new_extradata_size > 0 && new_extradata) {\n ret = ff_h264_decode_extradata(new_extradata, new_extradata_size,\n &h->ps, &h->is_avc, &h->nal_length_size,\n avctx->err_recognition, avctx);\n if (ret < 0)\n return ret;\n }\n buf_index = decode_nal_units(h, buf, buf_size);\n if (buf_index < 0)\n return AVERROR_INVALIDDATA;\n if (!h->cur_pic_ptr && h->nal_unit_type == H264_NAL_END_SEQUENCE) {\n buf_size = 0;\n goto out;\n }\n if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS) && !h->cur_pic_ptr) {\n if (avctx->skip_frame >= AVDISCARD_NONREF)\n return 0;\n av_log(avctx, AV_LOG_ERROR, "no frame!\\n");\n return AVERROR_INVALIDDATA;\n }\n if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS) ||\n (h->mb_y >= h->mb_height && h->mb_height)) {\n ff_h264_field_end(h, &h->slice_ctx[0], 0);\n *got_frame = 0;\n if (h->output_frame->buf[0]) {\n ret = output_frame(h, pict, h->output_frame) ;\n av_frame_unref(h->output_frame);\n if (ret < 0)\n return ret;\n *got_frame = 1;\n }\n }\n assert(pict->buf[0] || !*got_frame);\n return get_consumed_bytes(buf_index, buf_size);\n}', 'static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size)\n{\n AVCodecContext *const avctx = h->avctx;\n int nals_needed = 0;\n int i, ret = 0;\n if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS)) {\n h->current_slice = 0;\n if (!h->first_field)\n h->cur_pic_ptr = NULL;\n ff_h264_sei_uninit(&h->sei);\n }\n ret = ff_h2645_packet_split(&h->pkt, buf, buf_size, avctx, h->is_avc,\n h->nal_length_size, avctx->codec_id);\n if (ret < 0) {\n av_log(avctx, AV_LOG_ERROR,\n "Error splitting the input into NAL units.\\n");\n if (h->is_avc && !(avctx->err_recognition & AV_EF_EXPLODE)) {\n int err = ff_h2645_packet_split(&h->pkt, buf, buf_size, avctx, 0, 0,\n avctx->codec_id);\n if (err >= 0) {\n av_log(avctx, AV_LOG_WARNING,\n "The stream seems to contain AVCC extradata with Annex B "\n "formatted data, which is invalid.");\n h->is_avc = 0;\n ret = 0;\n }\n }\n if (ret < 0)\n return ret;\n }\n if (avctx->active_thread_type & FF_THREAD_FRAME)\n nals_needed = get_last_needed_nal(h);\n for (i = 0; i < h->pkt.nb_nals; i++) {\n H2645NAL *nal = &h->pkt.nals[i];\n int max_slice_ctx, err;\n if (avctx->skip_frame >= AVDISCARD_NONREF &&\n nal->ref_idc == 0 && nal->type != H264_NAL_SEI)\n continue;\n h->nal_ref_idc = nal->ref_idc;\n h->nal_unit_type = nal->type;\n err = 0;\n switch (nal->type) {\n case H264_NAL_IDR_SLICE:\n idr(h);\n case H264_NAL_SLICE:\n if ((err = ff_h264_queue_decode_slice(h, nal)))\n break;\n if (avctx->active_thread_type & FF_THREAD_FRAME && !h->avctx->hwaccel &&\n i >= nals_needed && !h->setup_finished && h->cur_pic_ptr) {\n ff_thread_finish_setup(avctx);\n h->setup_finished = 1;\n }\n max_slice_ctx = avctx->hwaccel ? 1 : h->nb_slice_ctx;\n if (h->nb_slice_ctx_queued == max_slice_ctx) {\n if (avctx->hwaccel) {\n ret = avctx->hwaccel->decode_slice(avctx, nal->raw_data, nal->raw_size);\n h->nb_slice_ctx_queued = 0;\n } else\n ret = ff_h264_execute_decode_slices(h);\n if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))\n goto end;\n }\n break;\n case H264_NAL_DPA:\n case H264_NAL_DPB:\n case H264_NAL_DPC:\n avpriv_request_sample(avctx, "data partitioning");\n ret = AVERROR(ENOSYS);\n goto end;\n break;\n case H264_NAL_SEI:\n ret = ff_h264_sei_decode(&h->sei, &nal->gb, &h->ps, avctx);\n if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))\n goto end;\n break;\n case H264_NAL_SPS:\n ret = ff_h264_decode_seq_parameter_set(&nal->gb, avctx, &h->ps);\n if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))\n goto end;\n break;\n case H264_NAL_PPS:\n ret = ff_h264_decode_picture_parameter_set(&nal->gb, avctx, &h->ps,\n nal->size_bits);\n if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))\n goto end;\n break;\n case H264_NAL_AUD:\n case H264_NAL_END_SEQUENCE:\n case H264_NAL_END_STREAM:\n case H264_NAL_FILLER_DATA:\n case H264_NAL_SPS_EXT:\n case H264_NAL_AUXILIARY_SLICE:\n break;\n default:\n av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\\n",\n nal->type, nal->size_bits);\n }\n if (err < 0) {\n av_log(h->avctx, AV_LOG_ERROR, "decode_slice_header error\\n");\n }\n }\n ret = ff_h264_execute_decode_slices(h);\n if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))\n goto end;\n ret = 0;\nend:\n if (h->cur_pic_ptr && !h->droppable) {\n ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,\n h->picture_structure == PICT_BOTTOM_FIELD);\n }\n return (ret < 0) ? ret : buf_size;\n}', 'int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length,\n void *logctx, int is_nalff, int nal_length_size,\n enum AVCodecID codec_id)\n{\n int consumed, ret = 0;\n const uint8_t *next_avc = buf + (is_nalff ? 0 : length);\n pkt->nb_nals = 0;\n while (length >= 4) {\n H2645NAL *nal;\n int extract_length = 0;\n int skip_trailing_zeros = 1;\n if (buf == next_avc) {\n int i;\n for (i = 0; i < nal_length_size; i++)\n extract_length = (extract_length << 8) | buf[i];\n if (extract_length > length) {\n av_log(logctx, AV_LOG_ERROR,\n "Invalid NAL unit size (%d > %d).\\n",\n extract_length, length);\n return AVERROR_INVALIDDATA;\n }\n buf += nal_length_size;\n length -= nal_length_size;\n next_avc = buf + extract_length;\n } else {\n int buf_index = find_next_start_code(buf, next_avc);\n buf += buf_index;\n length -= buf_index;\n if (buf == next_avc)\n continue;\n if (length > 0) {\n extract_length = length;\n } else if (pkt->nb_nals == 0) {\n av_log(logctx, AV_LOG_ERROR, "No NAL unit found\\n");\n return AVERROR_INVALIDDATA;\n } else {\n break;\n }\n }\n if (pkt->nals_allocated < pkt->nb_nals + 1) {\n int new_size = pkt->nals_allocated + 1;\n H2645NAL *tmp = av_realloc_array(pkt->nals, new_size, sizeof(*tmp));\n if (!tmp)\n return AVERROR(ENOMEM);\n pkt->nals = tmp;\n memset(pkt->nals + pkt->nals_allocated, 0,\n (new_size - pkt->nals_allocated) * sizeof(*tmp));\n pkt->nals_allocated = new_size;\n }\n nal = &pkt->nals[pkt->nb_nals++];\n consumed = ff_h2645_extract_rbsp(buf, extract_length, nal);\n if (consumed < 0)\n return consumed;\n if (consumed < length - 3 &&\n buf[consumed] == 0x00 && buf[consumed + 1] == 0x00 &&\n buf[consumed + 2] == 0x01 && buf[consumed + 3] == 0xE0)\n skip_trailing_zeros = 0;\n nal->size_bits = get_bit_length(nal, skip_trailing_zeros);\n ret = init_get_bits(&nal->gb, nal->data, nal->size_bits);\n if (ret < 0)\n return ret;\n if (codec_id == AV_CODEC_ID_HEVC)\n ret = hevc_parse_nal_header(nal, logctx);\n else\n ret = h264_parse_nal_header(nal, logctx);\n if (ret <= 0) {\n if (ret < 0) {\n av_log(logctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\\n",\n nal->type);\n }\n pkt->nb_nals--;\n }\n buf += consumed;\n length -= consumed;\n }\n return 0;\n}', 'static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,\n int bit_size)\n{\n int buffer_size;\n int ret = 0;\n if (bit_size > INT_MAX - 7 || bit_size < 0 || !buffer) {\n 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_h264_execute_decode_slices(H264Context *h)\n{\n AVCodecContext *const avctx = h->avctx;\n H264SliceContext *sl;\n int context_count = h->nb_slice_ctx_queued;\n int ret = 0;\n int i, j;\n if (h->avctx->hwaccel || context_count < 1)\n return 0;\n if (context_count == 1) {\n h->slice_ctx[0].next_slice_idx = h->mb_width * h->mb_height;\n h->postpone_filter = 0;\n ret = decode_slice(avctx, &h->slice_ctx[0]);\n h->mb_y = h->slice_ctx[0].mb_y;\n if (ret < 0)\n goto finish;\n } else {\n for (i = 0; i < context_count; i++) {\n int next_slice_idx = h->mb_width * h->mb_height;\n int slice_idx;\n sl = &h->slice_ctx[i];\n sl->er.error_count = 0;\n slice_idx = sl->mb_y * h->mb_width + sl->mb_x;\n for (j = 0; j < context_count; j++) {\n H264SliceContext *sl2 = &h->slice_ctx[j];\n int slice_idx2 = sl2->mb_y * h->mb_width + sl2->mb_x;\n if (i == j || slice_idx2 < slice_idx)\n continue;\n next_slice_idx = FFMIN(next_slice_idx, slice_idx2);\n }\n sl->next_slice_idx = next_slice_idx;\n }\n avctx->execute(avctx, decode_slice, h->slice_ctx,\n NULL, context_count, sizeof(h->slice_ctx[0]));\n sl = &h->slice_ctx[context_count - 1];\n h->mb_y = sl->mb_y;\n for (i = 1; i < context_count; i++)\n h->slice_ctx[0].er.error_count += h->slice_ctx[i].er.error_count;\n if (h->postpone_filter) {\n h->postpone_filter = 0;\n for (i = 0; i < context_count; i++) {\n int y_end, x_end;\n sl = &h->slice_ctx[i];\n y_end = FFMIN(sl->mb_y + 1, h->mb_height);\n x_end = (sl->mb_y >= h->mb_height) ? h->mb_width : sl->mb_x;\n for (j = sl->resync_mb_y; j < y_end; j += 1 + FIELD_OR_MBAFF_PICTURE(h)) {\n sl->mb_y = j;\n loop_filter(h, sl, j > sl->resync_mb_y ? 0 : sl->resync_mb_x,\n j == y_end - 1 ? x_end : h->mb_width);\n }\n }\n }\n }\nfinish:\n h->nb_slice_ctx_queued = 0;\n return ret;\n}', 'static int decode_slice(struct AVCodecContext *avctx, void *arg)\n{\n H264SliceContext *sl = arg;\n const H264Context *h = sl->h264;\n int lf_x_start = sl->mb_x;\n int orig_deblock = sl->deblocking_filter;\n int ret;\n sl->linesize = h->cur_pic_ptr->f->linesize[0];\n sl->uvlinesize = h->cur_pic_ptr->f->linesize[1];\n ret = alloc_scratch_buffers(sl, sl->linesize);\n if (ret < 0)\n return ret;\n sl->mb_skip_run = -1;\n if (h->postpone_filter)\n sl->deblocking_filter = 0;\n sl->is_complex = FRAME_MBAFF(h) || h->picture_structure != PICT_FRAME ||\n (CONFIG_GRAY && (h->flags & AV_CODEC_FLAG_GRAY));\n if (h->ps.pps->cabac) {\n align_get_bits(&sl->gb);\n ff_init_cabac_decoder(&sl->cabac,\n sl->gb.buffer + get_bits_count(&sl->gb) / 8,\n (get_bits_left(&sl->gb) + 7) / 8);\n ff_h264_init_cabac_states(h, sl);\n for (;;) {\n int ret, eos;\n if (sl->mb_x + sl->mb_y * h->mb_width >= sl->next_slice_idx) {\n av_log(h->avctx, AV_LOG_ERROR, "Slice overlaps with next at %d\\n",\n sl->next_slice_idx);\n return AVERROR_INVALIDDATA;\n }\n ret = ff_h264_decode_mb_cabac(h, sl);\n if (ret >= 0)\n ff_h264_hl_decode_mb(h, sl);\n if (ret >= 0 && FRAME_MBAFF(h)) {\n sl->mb_y++;\n ret = ff_h264_decode_mb_cabac(h, sl);\n if (ret >= 0)\n ff_h264_hl_decode_mb(h, sl);\n sl->mb_y--;\n }\n eos = get_cabac_terminate(&sl->cabac);\n if ((h->workaround_bugs & FF_BUG_TRUNCATED) &&\n sl->cabac.bytestream > sl->cabac.bytestream_end + 2) {\n er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x - 1,\n sl->mb_y, ER_MB_END);\n if (sl->mb_x >= lf_x_start)\n loop_filter(h, sl, lf_x_start, sl->mb_x + 1);\n goto finish;\n }\n if (ret < 0 || sl->cabac.bytestream > sl->cabac.bytestream_end + 2) {\n av_log(h->avctx, AV_LOG_ERROR,\n "error while decoding MB %d %d, bytestream %td\\n",\n sl->mb_x, sl->mb_y,\n sl->cabac.bytestream_end - sl->cabac.bytestream);\n er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,\n sl->mb_y, ER_MB_ERROR);\n return AVERROR_INVALIDDATA;\n }\n if (++sl->mb_x >= h->mb_width) {\n loop_filter(h, sl, lf_x_start, sl->mb_x);\n sl->mb_x = lf_x_start = 0;\n decode_finish_row(h, sl);\n ++sl->mb_y;\n if (FIELD_OR_MBAFF_PICTURE(h)) {\n ++sl->mb_y;\n if (FRAME_MBAFF(h) && sl->mb_y < h->mb_height)\n predict_field_decoding_flag(h, sl);\n }\n }\n if (eos || sl->mb_y >= h->mb_height) {\n ff_tlog(h->avctx, "slice end %d %d\\n",\n get_bits_count(&sl->gb), sl->gb.size_in_bits);\n er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x - 1,\n sl->mb_y, ER_MB_END);\n if (sl->mb_x > lf_x_start)\n loop_filter(h, sl, lf_x_start, sl->mb_x);\n goto finish;\n }\n }\n } else {\n for (;;) {\n int ret;\n if (sl->mb_x + sl->mb_y * h->mb_width >= sl->next_slice_idx) {\n av_log(h->avctx, AV_LOG_ERROR, "Slice overlaps with next at %d\\n",\n sl->next_slice_idx);\n return AVERROR_INVALIDDATA;\n }\n ret = ff_h264_decode_mb_cavlc(h, sl);\n if (ret >= 0)\n ff_h264_hl_decode_mb(h, sl);\n if (ret >= 0 && FRAME_MBAFF(h)) {\n sl->mb_y++;\n ret = ff_h264_decode_mb_cavlc(h, sl);\n if (ret >= 0)\n ff_h264_hl_decode_mb(h, sl);\n sl->mb_y--;\n }\n if (ret < 0) {\n av_log(h->avctx, AV_LOG_ERROR,\n "error while decoding MB %d %d\\n", sl->mb_x, sl->mb_y);\n er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,\n sl->mb_y, ER_MB_ERROR);\n return ret;\n }\n if (++sl->mb_x >= h->mb_width) {\n loop_filter(h, sl, lf_x_start, sl->mb_x);\n sl->mb_x = lf_x_start = 0;\n decode_finish_row(h, sl);\n ++sl->mb_y;\n if (FIELD_OR_MBAFF_PICTURE(h)) {\n ++sl->mb_y;\n if (FRAME_MBAFF(h) && sl->mb_y < h->mb_height)\n predict_field_decoding_flag(h, sl);\n }\n if (sl->mb_y >= h->mb_height) {\n ff_tlog(h->avctx, "slice end %d %d\\n",\n get_bits_count(&sl->gb), sl->gb.size_in_bits);\n if (get_bits_left(&sl->gb) == 0) {\n er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y,\n sl->mb_x - 1, sl->mb_y, ER_MB_END);\n goto finish;\n } else {\n er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y,\n sl->mb_x - 1, sl->mb_y, ER_MB_END);\n return AVERROR_INVALIDDATA;\n }\n }\n }\n if (get_bits_left(&sl->gb) <= 0 && sl->mb_skip_run <= 0) {\n ff_tlog(h->avctx, "slice end %d %d\\n",\n get_bits_count(&sl->gb), sl->gb.size_in_bits);\n if (get_bits_left(&sl->gb) == 0) {\n er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y,\n sl->mb_x - 1, sl->mb_y, ER_MB_END);\n if (sl->mb_x > lf_x_start)\n loop_filter(h, sl, lf_x_start, sl->mb_x);\n goto finish;\n } else {\n er_add_slice(sl, sl->resync_mb_x, sl->resync_mb_y, sl->mb_x,\n sl->mb_y, ER_MB_ERROR);\n return AVERROR_INVALIDDATA;\n }\n }\n }\n }\nfinish:\n sl->deblocking_filter = orig_deblock;\n return 0;\n}', 'void ff_h264_init_cabac_states(const H264Context *h, H264SliceContext *sl)\n{\n int i;\n const int8_t (*tab)[2];\n const int slice_qp = av_clip(sl->qscale - 6*(h->ps.sps->bit_depth_luma-8), 0, 51);\n if (sl->slice_type_nos == AV_PICTURE_TYPE_I) tab = cabac_context_init_I;\n else tab = cabac_context_init_PB[sl->cabac_init_idc];\n for( i= 0; i < 1024; i++ ) {\n int pre = 2*(((tab[i][0] * slice_qp) >>4 ) + tab[i][1]) - 127;\n pre^= pre>>31;\n if(pre > 124)\n pre= 124 + (pre&1);\n sl->cabac_state[i] = pre;\n }\n}']
57
0
https://gitlab.com/libtiff/libtiff/blob/163627448aa8d2893582f2546dd85706586e6243/libtiff/tif_swab.c/#L113
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++; } }
['static TIFF* openSrcImage (char **imageSpec)\n{\n\tTIFF *tif;\n\tchar *fn = *imageSpec;\n\t*imageSpec = strchr (fn, comma);\n\tif (*imageSpec) {\n\t\t**imageSpec = \'\\0\';\n\t\ttif = TIFFOpen (fn, "r");\n\t\tif (!(*imageSpec)[1]) {*imageSpec = NULL; return tif;}\n\t\tif (tif) {\n\t\t\t**imageSpec = comma;\n\t\t\tif (!nextSrcImage(tif, imageSpec)) {\n\t\t\t\tTIFFClose (tif);\n\t\t\t\ttif = NULL;\n\t\t\t}\n\t\t}\n\t}else\n\t\ttif = TIFFOpen (fn, "r");\n\treturn tif;\n}', 'static int nextSrcImage (TIFF *tif, char **imageSpec)\n{\n\tif (**imageSpec == comma) {\n\t\tchar *start = *imageSpec + 1;\n\t\ttdir_t nextImage = (tdir_t)strtol(start, imageSpec, 0);\n\t\tif (start == *imageSpec) nextImage = TIFFCurrentDirectory (tif);\n\t\tif (**imageSpec)\n\t\t{\n\t\t\tif (**imageSpec == comma) {\n\t\t\t\tif ((*imageSpec)[1] == \'\\0\') *imageSpec = NULL;\n\t\t\t}else{\n\t\t\t\tfprintf (stderr,\n\t\t\t\t "Expected a %c separated image # list after %s\\n",\n\t\t\t\t comma, TIFFFileName (tif));\n\t\t\t\texit (-4);\n\t\t\t}\n\t\t}\n\t\tif (TIFFSetDirectory (tif, nextImage)) return 1;\n\t\tfprintf (stderr, "%s%c%d not found!\\n",\n\t\t TIFFFileName(tif), comma, (int) nextImage);\n\t}\n\treturn 0;\n}', 'void\nTIFFClose(TIFF* tif)\n{\n\tTIFFCloseProc closeproc = tif->tif_closeproc;\n\tthandle_t fd = tif->tif_clientdata;\n\tTIFFCleanup(tif);\n\t(void) (*closeproc)(fd);\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 *link = tif->tif_clientinfo;\n\t\ttif->tif_clientinfo = link->next;\n\t\t_TIFFfree( link->name );\n\t\t_TIFFfree( link );\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))\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))\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\t\t\t\t\tif (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_STRIPOFFSETS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripoffset))\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_TILEOFFSETS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripoffset))\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,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,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,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,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\tassert(0);\n\t\t\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\t\tfor (m=0; m<(uint32)(tif->tif_dir.td_customValueCount); m++)\n\t\t{\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,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].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,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].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,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].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,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].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,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].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,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].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,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].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,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].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,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].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,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].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,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].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,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].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,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].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,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].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,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].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,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].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)&(~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\tassert(na<ndir);\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=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 uint64* ma;\n uint32 mb;\n uint32* p;\n uint32* q;\n int o;\n if (dir==NULL)\n {\n (*ndir)++;\n return(1);\n }\n if( tif->tif_flags&TIFF_BIGTIFF )\n return TIFFWriteDirectoryTagCheckedLong8Array(tif,ndir,dir,\n tag,count,value);\n p = _TIFFmalloc(count*sizeof(uint32));\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 Classic TIFF file.");\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 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}']
58
0
https://github.com/openssl/openssl/blob/270a4bba49849de7f928f4fab186205abd132411/apps/pkcs12.c/#L765
static int get_cert_chain(X509 *cert, X509_STORE *store, STACK_OF(X509) **chain) { X509_STORE_CTX *store_ctx = NULL; STACK_OF(X509) *chn = NULL; int i = 0; store_ctx = X509_STORE_CTX_new(); if (store_ctx == NULL) { i = X509_V_ERR_UNSPECIFIED; goto end; } if (!X509_STORE_CTX_init(store_ctx, store, cert, NULL)) { i = X509_V_ERR_UNSPECIFIED; goto end; } if (X509_verify_cert(store_ctx) > 0) chn = X509_STORE_CTX_get1_chain(store_ctx); else if ((i = X509_STORE_CTX_get_error(store_ctx)) == 0) i = X509_V_ERR_UNSPECIFIED; end: X509_STORE_CTX_free(store_ctx); *chain = chn; return i; }
['static int get_cert_chain(X509 *cert, X509_STORE *store,\n STACK_OF(X509) **chain)\n{\n X509_STORE_CTX *store_ctx = NULL;\n STACK_OF(X509) *chn = NULL;\n int i = 0;\n store_ctx = X509_STORE_CTX_new();\n if (store_ctx == NULL) {\n i = X509_V_ERR_UNSPECIFIED;\n goto end;\n }\n if (!X509_STORE_CTX_init(store_ctx, store, cert, NULL)) {\n i = X509_V_ERR_UNSPECIFIED;\n goto end;\n }\n if (X509_verify_cert(store_ctx) > 0)\n chn = X509_STORE_CTX_get1_chain(store_ctx);\n else if ((i = X509_STORE_CTX_get_error(store_ctx)) == 0)\n i = X509_V_ERR_UNSPECIFIED;\nend:\n X509_STORE_CTX_free(store_ctx);\n *chain = chn;\n return i;\n}', 'X509_STORE_CTX *X509_STORE_CTX_new(void)\n{\n X509_STORE_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));\n if (ctx == NULL) {\n X509err(X509_F_X509_STORE_CTX_NEW, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n return ctx;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)(file); (void)(line);\n ret = malloc(num);\n#endif\n return ret;\n}', 'void X509_STORE_CTX_free(X509_STORE_CTX *ctx)\n{\n if (ctx == NULL)\n return;\n X509_STORE_CTX_cleanup(ctx);\n OPENSSL_free(ctx);\n}', 'void CRYPTO_free(void *str, const char *file, int line)\n{\n if (free_impl != NULL && free_impl != &CRYPTO_free) {\n free_impl(str, file, line);\n return;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_free(str, 0, file, line);\n free(str);\n CRYPTO_mem_debug_free(str, 1, file, line);\n } else {\n free(str);\n }\n#else\n free(str);\n#endif\n}']
59
0
https://github.com/openssl/openssl/blob/313fce7b61ecaf5879cf84b256bdd0964134836e/crypto/bn/bn_ctx.c/#L353
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, DSA_SIG *sig,\n\t\t\t DSA *dsa)\n\t{\n\tBN_CTX *ctx;\n\tBIGNUM u1,u2,t1;\n\tBN_MONT_CTX *mont=NULL;\n\tint ret = -1, i;\n\tif (!dsa->p || !dsa->q || !dsa->g)\n\t\t{\n\t\tDSAerr(DSA_F_DSA_DO_VERIFY,DSA_R_MISSING_PARAMETERS);\n\t\treturn -1;\n\t\t}\n\ti = BN_num_bits(dsa->q);\n\tif (i != 160 && i != 224 && i != 256)\n\t\t{\n\t\tDSAerr(DSA_F_DSA_DO_VERIFY,DSA_R_BAD_Q_VALUE);\n\t\treturn -1;\n\t\t}\n\tif (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS)\n\t\t{\n\t\tDSAerr(DSA_F_DSA_DO_VERIFY,DSA_R_MODULUS_TOO_LARGE);\n\t\treturn -1;\n\t\t}\n\tif (dgst_len > SHA256_DIGEST_LENGTH)\n\t\t{\n\t\tDSAerr(DSA_F_DSA_DO_VERIFY,DSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);\n\t\treturn -1;\n\t\t}\n\tBN_init(&u1);\n\tBN_init(&u2);\n\tBN_init(&t1);\n\tif ((ctx=BN_CTX_new()) == NULL) goto err;\n\tif (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||\n\t BN_ucmp(sig->r, dsa->q) >= 0)\n\t\t{\n\t\tret = 0;\n\t\tgoto err;\n\t\t}\n\tif (BN_is_zero(sig->s) || BN_is_negative(sig->s) ||\n\t BN_ucmp(sig->s, dsa->q) >= 0)\n\t\t{\n\t\tret = 0;\n\t\tgoto err;\n\t\t}\n\tif ((BN_mod_inverse(&u2,sig->s,dsa->q,ctx)) == NULL) goto err;\n\tif (dgst_len > (i >> 3))\n\t\tdgst_len = (i >> 3);\n\tif (BN_bin2bn(dgst,dgst_len,&u1) == NULL) goto err;\n\tif (!BN_mod_mul(&u1,&u1,&u2,dsa->q,ctx)) goto err;\n\tif (!BN_mod_mul(&u2,sig->r,&u2,dsa->q,ctx)) goto err;\n\tif (dsa->flags & DSA_FLAG_CACHE_MONT_P)\n\t\t{\n\t\tmont = BN_MONT_CTX_set_locked(&dsa->method_mont_p,\n\t\t\t\t\tCRYPTO_LOCK_DSA, dsa->p, ctx);\n\t\tif (!mont)\n\t\t\tgoto err;\n\t\t}\n\tDSA_MOD_EXP(goto err, dsa, &t1, dsa->g, &u1, dsa->pub_key, &u2, dsa->p, ctx, mont);\n\tif (!BN_mod(&u1,&t1,dsa->q,ctx)) goto err;\n\tret=(BN_ucmp(&u1, sig->r) == 0);\n\terr:\n\tif (ret != 1) DSAerr(DSA_F_DSA_DO_VERIFY,ERR_R_BN_LIB);\n\tif (ctx != NULL) BN_CTX_free(ctx);\n\tBN_free(&u1);\n\tBN_free(&u2);\n\tBN_free(&t1);\n\treturn(ret);\n\t}', 'BIGNUM *BN_mod_inverse(BIGNUM *in,\n\tconst BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n\t{\n\tBIGNUM *A,*B,*X,*Y,*M,*D,*T,*R=NULL;\n\tBIGNUM *ret=NULL;\n\tint sign;\n\tif ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0))\n\t\t{\n\t\treturn BN_mod_inverse_no_branch(in, a, n, ctx);\n\t\t}\n\tbn_check_top(a);\n\tbn_check_top(n);\n\tBN_CTX_start(ctx);\n\tA = BN_CTX_get(ctx);\n\tB = BN_CTX_get(ctx);\n\tX = BN_CTX_get(ctx);\n\tD = BN_CTX_get(ctx);\n\tM = BN_CTX_get(ctx);\n\tY = BN_CTX_get(ctx);\n\tT = BN_CTX_get(ctx);\n\tif (T == NULL) goto err;\n\tif (in == NULL)\n\t\tR=BN_new();\n\telse\n\t\tR=in;\n\tif (R == NULL) goto err;\n\tBN_one(X);\n\tBN_zero(Y);\n\tif (BN_copy(B,a) == NULL) goto err;\n\tif (BN_copy(A,n) == NULL) goto err;\n\tA->neg = 0;\n\tif (B->neg || (BN_ucmp(B, A) >= 0))\n\t\t{\n\t\tif (!BN_nnmod(B, B, A, ctx)) goto err;\n\t\t}\n\tsign = -1;\n\tif (BN_is_odd(n) && (BN_num_bits(n) <= (BN_BITS <= 32 ? 450 : 2048)))\n\t\t{\n\t\tint shift;\n\t\twhile (!BN_is_zero(B))\n\t\t\t{\n\t\t\tshift = 0;\n\t\t\twhile (!BN_is_bit_set(B, shift))\n\t\t\t\t{\n\t\t\t\tshift++;\n\t\t\t\tif (BN_is_odd(X))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_uadd(X, X, n)) goto err;\n\t\t\t\t\t}\n\t\t\t\tif (!BN_rshift1(X, X)) goto err;\n\t\t\t\t}\n\t\t\tif (shift > 0)\n\t\t\t\t{\n\t\t\t\tif (!BN_rshift(B, B, shift)) goto err;\n\t\t\t\t}\n\t\t\tshift = 0;\n\t\t\twhile (!BN_is_bit_set(A, shift))\n\t\t\t\t{\n\t\t\t\tshift++;\n\t\t\t\tif (BN_is_odd(Y))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_uadd(Y, Y, n)) goto err;\n\t\t\t\t\t}\n\t\t\t\tif (!BN_rshift1(Y, Y)) goto err;\n\t\t\t\t}\n\t\t\tif (shift > 0)\n\t\t\t\t{\n\t\t\t\tif (!BN_rshift(A, A, shift)) goto err;\n\t\t\t\t}\n\t\t\tif (BN_ucmp(B, A) >= 0)\n\t\t\t\t{\n\t\t\t\tif (!BN_uadd(X, X, Y)) goto err;\n\t\t\t\tif (!BN_usub(B, B, A)) goto err;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (!BN_uadd(Y, Y, X)) goto err;\n\t\t\t\tif (!BN_usub(A, A, B)) goto err;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\twhile (!BN_is_zero(B))\n\t\t\t{\n\t\t\tBIGNUM *tmp;\n\t\t\tif (BN_num_bits(A) == BN_num_bits(B))\n\t\t\t\t{\n\t\t\t\tif (!BN_one(D)) goto err;\n\t\t\t\tif (!BN_sub(M,A,B)) goto err;\n\t\t\t\t}\n\t\t\telse if (BN_num_bits(A) == BN_num_bits(B) + 1)\n\t\t\t\t{\n\t\t\t\tif (!BN_lshift1(T,B)) goto err;\n\t\t\t\tif (BN_ucmp(A,T) < 0)\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_one(D)) goto err;\n\t\t\t\t\tif (!BN_sub(M,A,B)) goto err;\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_sub(M,A,T)) goto err;\n\t\t\t\t\tif (!BN_add(D,T,B)) goto err;\n\t\t\t\t\tif (BN_ucmp(A,D) < 0)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tif (!BN_set_word(D,2)) goto err;\n\t\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t\t{\n\t\t\t\t\t\tif (!BN_set_word(D,3)) goto err;\n\t\t\t\t\t\tif (!BN_sub(M,M,B)) goto err;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (!BN_div(D,M,A,B,ctx)) goto err;\n\t\t\t\t}\n\t\t\ttmp=A;\n\t\t\tA=B;\n\t\t\tB=M;\n\t\t\tif (BN_is_one(D))\n\t\t\t\t{\n\t\t\t\tif (!BN_add(tmp,X,Y)) goto err;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (BN_is_word(D,2))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_lshift1(tmp,X)) goto err;\n\t\t\t\t\t}\n\t\t\t\telse if (BN_is_word(D,4))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_lshift(tmp,X,2)) goto err;\n\t\t\t\t\t}\n\t\t\t\telse if (D->top == 1)\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_copy(tmp,X)) goto err;\n\t\t\t\t\tif (!BN_mul_word(tmp,D->d[0])) goto err;\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_mul(tmp,D,X,ctx)) goto err;\n\t\t\t\t\t}\n\t\t\t\tif (!BN_add(tmp,tmp,Y)) goto err;\n\t\t\t\t}\n\t\t\tM=Y;\n\t\t\tY=X;\n\t\t\tX=tmp;\n\t\t\tsign = -sign;\n\t\t\t}\n\t\t}\n\tif (sign < 0)\n\t\t{\n\t\tif (!BN_sub(Y,n,Y)) goto err;\n\t\t}\n\tif (BN_is_one(A))\n\t\t{\n\t\tif (!Y->neg && BN_ucmp(Y,n) < 0)\n\t\t\t{\n\t\t\tif (!BN_copy(R,Y)) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (!BN_nnmod(R,Y,n,ctx)) goto err;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_INVERSE,BN_R_NO_INVERSE);\n\t\tgoto err;\n\t\t}\n\tret=R;\nerr:\n\tif ((ret == NULL) && (in == NULL)) BN_free(R);\n\tBN_CTX_end(ctx);\n\tbn_check_top(ret);\n\treturn(ret);\n\t}', 'void BN_CTX_start(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_start", ctx);\n\tif(ctx->err_stack || ctx->too_many)\n\t\tctx->err_stack++;\n\telse if(!BN_STACK_push(&ctx->stack, ctx->used))\n\t\t{\n\t\tBNerr(BN_F_BN_CTX_START,BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n\t\tctx->err_stack++;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'void BN_CTX_end(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_end", ctx);\n\tif(ctx->err_stack)\n\t\tctx->err_stack--;\n\telse\n\t\t{\n\t\tunsigned int fp = BN_STACK_pop(&ctx->stack);\n\t\tif(fp < ctx->used)\n\t\t\tBN_POOL_release(&ctx->pool, ctx->used - fp);\n\t\tctx->used = fp;\n\t\tctx->too_many = 0;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,\n\tBN_CTX *ctx)\n\t{\n\tBIGNUM *t;\n\tint ret=0;\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tbn_check_top(m);\n\tBN_CTX_start(ctx);\n\tif ((t = BN_CTX_get(ctx)) == NULL) goto err;\n\tif (a == b)\n\t\t{ if (!BN_sqr(t,a,ctx)) goto err; }\n\telse\n\t\t{ if (!BN_mul(t,a,b,ctx)) goto err; }\n\tif (!BN_nnmod(r,t,m,ctx)) goto err;\n\tbn_check_top(r);\n\tret=1;\nerr:\n\tBN_CTX_end(ctx);\n\treturn(ret);\n\t}', 'int BN_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;\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\tval1[0] = BN_CTX_get(ctx);\n\tval2[0] = BN_CTX_get(ctx);\n\tif(!d || !r || !val1[0] || !val2[0]) goto err;\n\tif (in_mont != NULL)\n\t\tmont=in_mont;\n\telse\n\t\t{\n\t\tif ((mont=BN_MONT_CTX_new()) == NULL) goto err;\n\t\tif (!BN_MONT_CTX_set(mont,m,ctx)) goto err;\n\t\t}\n\twindow1 = BN_window_bits_for_exponent_size(bits1);\n\twindow2 = BN_window_bits_for_exponent_size(bits2);\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\tBN_zero(rr);\n\t\tret = 1;\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\tif(((val1[i] = BN_CTX_get(ctx)) == NULL) ||\n\t\t\t\t\t!BN_mod_mul_montgomery(val1[i],val1[i-1],\n\t\t\t\t\t\td,mont,ctx))\n\t\t\t\tgoto err;\n\t\t\t}\n\t\t}\n\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\tBN_zero(rr);\n\t\tret = 1;\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\tif(((val2[i] = BN_CTX_get(ctx)) == NULL) ||\n\t\t\t\t\t!BN_mod_mul_montgomery(val2[i],val2[i-1],\n\t\t\t\t\t\td,mont,ctx))\n\t\t\t\tgoto err;\n\t\t\t}\n\t\t}\n\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\tbn_check_top(rr);\n\treturn(ret);\n\t}', 'int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)\n\t{\n\tint ret = 0;\n\tBIGNUM *Ri,*R;\n\tBN_CTX_start(ctx);\n\tif((Ri = BN_CTX_get(ctx)) == NULL) goto err;\n\tR= &(mont->RR);\n\tif (!BN_copy(&(mont->N),mod)) goto err;\n\tmont->N.neg = 0;\n#ifdef MONT_WORD\n\t\t{\n\t\tBIGNUM tmod;\n\t\tBN_ULONG buf[2];\n\t\ttmod.d=buf;\n\t\ttmod.dmax=2;\n\t\ttmod.neg=0;\n\t\tmont->ri=(BN_num_bits(mod)+(BN_BITS2-1))/BN_BITS2*BN_BITS2;\n#if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)\n\t\tBN_zero(R);\n\t\tif (!(BN_set_bit(R,2*BN_BITS2))) goto err;\n\t\t\t\t\t\t\t\ttmod.top=0;\n\t\tif ((buf[0] = mod->d[0]))\t\t\ttmod.top=1;\n\t\tif ((buf[1] = mod->top>1 ? mod->d[1] : 0))\ttmod.top=2;\n\t\tif ((BN_mod_inverse(Ri,R,&tmod,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tif (!BN_lshift(Ri,Ri,2*BN_BITS2)) goto err;\n\t\tif (!BN_is_zero(Ri))\n\t\t\t{\n\t\t\tif (!BN_sub_word(Ri,1)) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (bn_expand(Ri,(int)sizeof(BN_ULONG)*2) == NULL)\n\t\t\t\tgoto err;\n\t\t\tRi->neg=0;\n\t\t\tRi->d[0]=BN_MASK2;\n\t\t\tRi->d[1]=BN_MASK2;\n\t\t\tRi->top=2;\n\t\t\t}\n\t\tif (!BN_div(Ri,NULL,Ri,&tmod,ctx)) goto err;\n\t\tmont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n\t\tmont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;\n#else\n\t\tBN_zero(R);\n\t\tif (!(BN_set_bit(R,BN_BITS2))) goto err;\n\t\tbuf[0]=mod->d[0];\n\t\tbuf[1]=0;\n\t\ttmod.top = buf[0] != 0 ? 1 : 0;\n\t\tif ((BN_mod_inverse(Ri,R,&tmod,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tif (!BN_lshift(Ri,Ri,BN_BITS2)) goto err;\n\t\tif (!BN_is_zero(Ri))\n\t\t\t{\n\t\t\tif (!BN_sub_word(Ri,1)) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (!BN_set_word(Ri,BN_MASK2)) goto err;\n\t\t\t}\n\t\tif (!BN_div(Ri,NULL,Ri,&tmod,ctx)) goto err;\n\t\tmont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n\t\tmont->n0[1] = 0;\n#endif\n\t\t}\n#else\n\t\t{\n\t\tmont->ri=BN_num_bits(&mont->N);\n\t\tBN_zero(R);\n\t\tif (!BN_set_bit(R,mont->ri)) goto err;\n\t\tif ((BN_mod_inverse(Ri,R,&mont->N,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tif (!BN_lshift(Ri,Ri,mont->ri)) goto err;\n\t\tif (!BN_sub_word(Ri,1)) goto err;\n\t\tif (!BN_div(&(mont->Ni),NULL,Ri,&mont->N,ctx)) goto err;\n\t\t}\n#endif\n\tBN_zero(&(mont->RR));\n\tif (!BN_set_bit(&(mont->RR),mont->ri*2)) goto err;\n\tif (!BN_mod(&(mont->RR),&(mont->RR),&(mont->N),ctx)) goto err;\n\tret = 1;\nerr:\n\tBN_CTX_end(ctx);\n\treturn ret;\n\t}', 'BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,\n\tconst BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n\t{\n\tBIGNUM *A,*B,*X,*Y,*M,*D,*T,*R=NULL;\n\tBIGNUM local_A, local_B;\n\tBIGNUM *pA, *pB;\n\tBIGNUM *ret=NULL;\n\tint sign;\n\tbn_check_top(a);\n\tbn_check_top(n);\n\tBN_CTX_start(ctx);\n\tA = BN_CTX_get(ctx);\n\tB = BN_CTX_get(ctx);\n\tX = BN_CTX_get(ctx);\n\tD = BN_CTX_get(ctx);\n\tM = BN_CTX_get(ctx);\n\tY = BN_CTX_get(ctx);\n\tT = BN_CTX_get(ctx);\n\tif (T == NULL) goto err;\n\tif (in == NULL)\n\t\tR=BN_new();\n\telse\n\t\tR=in;\n\tif (R == NULL) goto err;\n\tBN_one(X);\n\tBN_zero(Y);\n\tif (BN_copy(B,a) == NULL) goto err;\n\tif (BN_copy(A,n) == NULL) goto err;\n\tA->neg = 0;\n\tif (B->neg || (BN_ucmp(B, A) >= 0))\n\t\t{\n\t\tpB = &local_B;\n\t\tBN_with_flags(pB, B, BN_FLG_CONSTTIME);\n\t\tif (!BN_nnmod(B, pB, A, ctx)) goto err;\n\t\t}\n\tsign = -1;\n\twhile (!BN_is_zero(B))\n\t\t{\n\t\tBIGNUM *tmp;\n\t\tpA = &local_A;\n\t\tBN_with_flags(pA, A, BN_FLG_CONSTTIME);\n\t\tif (!BN_div(D,M,pA,B,ctx)) goto err;\n\t\ttmp=A;\n\t\tA=B;\n\t\tB=M;\n\t\tif (!BN_mul(tmp,D,X,ctx)) goto err;\n\t\tif (!BN_add(tmp,tmp,Y)) goto err;\n\t\tM=Y;\n\t\tY=X;\n\t\tX=tmp;\n\t\tsign = -sign;\n\t\t}\n\tif (sign < 0)\n\t\t{\n\t\tif (!BN_sub(Y,n,Y)) goto err;\n\t\t}\n\tif (BN_is_one(A))\n\t\t{\n\t\tif (!Y->neg && BN_ucmp(Y,n) < 0)\n\t\t\t{\n\t\t\tif (!BN_copy(R,Y)) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (!BN_nnmod(R,Y,n,ctx)) goto err;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_INVERSE,BN_R_NO_INVERSE);\n\t\tgoto err;\n\t\t}\n\tret=R;\nerr:\n\tif ((ret == NULL) && (in == NULL)) BN_free(R);\n\tBN_CTX_end(ctx);\n\tbn_check_top(ret);\n\treturn(ret);\n\t}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n\t{\n\tif (!(BN_mod(r,m,d,ctx)))\n\t\treturn 0;\n\tif (!r->neg)\n\t\treturn 1;\n\treturn (d->neg ? BN_sub : BN_add)(r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n\t BN_CTX *ctx)\n\t{\n\tint norm_shift,i,loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tint num_n,div_n;\n\tif ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0))\n\t\t{\n\t\treturn BN_div_no_branch(dv, rm, num, divisor, ctx);\n\t\t}\n\tbn_check_top(dv);\n\tbn_check_top(rm);\n\tbn_check_top(num);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\n\t\t}\n\tif (BN_ucmp(num,divisor) < 0)\n\t\t{\n\t\tif (rm != NULL)\n\t\t\t{ if (BN_copy(rm,num) == NULL) return(0); }\n\t\tif (dv != NULL) BN_zero(dv);\n\t\treturn(1);\n\t\t}\n\tBN_CTX_start(ctx);\n\ttmp=BN_CTX_get(ctx);\n\tsnum=BN_CTX_get(ctx);\n\tsdiv=BN_CTX_get(ctx);\n\tif (dv == NULL)\n\t\tres=BN_CTX_get(ctx);\n\telse\tres=dv;\n\tif (sdiv == NULL || res == NULL) goto err;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tif (!(BN_lshift(sdiv,divisor,norm_shift))) goto err;\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tif (!(BN_lshift(snum,num,norm_shift))) goto err;\n\tsnum->neg=0;\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\twnum.neg = 0;\n\twnum.d = &(snum->d[loop]);\n\twnum.top = div_n;\n\twnum.dmax = snum->dmax - loop;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (BN_ucmp(&wnum,sdiv) >= 0)\n\t\t{\n\t\tbn_clear_top2max(&wnum);\n\t\tbn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n\t\t*resp=1;\n\t\t}\n\telse\n\t\tres->top--;\n\tif (res->top == 0)\n\t\tres->neg = 0;\n\telse\n\t\tresp--;\n\tfor (i=0; i<loop-1; i++, wnump--, resp--)\n\t\t{\n\t\tBN_ULONG q,l0;\n#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n\t\tBN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG);\n\t\tq=bn_div_3_words(wnump,d1,d0);\n#else\n\t\tBN_ULONG n0,n1,rem=0;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\t{\n#ifdef BN_LLONG\n\t\t\tBN_ULLONG t2;\n#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n\t\t\tq=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);\n#else\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tt2 -= d1;\n\t\t\t\t}\n#else\n\t\t\tBN_ULONG t2l,t2h,ql,qh;\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#if defined(BN_UMULT_LOHI)\n\t\t\tBN_UMULT_LOHI(t2l,t2h,d1,q);\n#elif defined(BN_UMULT_HIGH)\n\t\t\tt2l = d1 * q;\n\t\t\tt2h = BN_UMULT_HIGH(d1,q);\n#else\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n#endif\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif ((t2h < rem) ||\n\t\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t\t}\n#endif\n\t\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\twnum.d--;\n\t\tif (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1))\n\t\t\t{\n\t\t\tq--;\n\t\t\tif (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n\t\t\t\t(*wnump)++;\n\t\t\t}\n\t\t*resp = q;\n\t\t}\n\tbn_correct_top(snum);\n\tif (rm != NULL)\n\t\t{\n\t\tint neg = num->neg;\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\tif (!BN_is_zero(rm))\n\t\t\trm->neg = neg;\n\t\tbn_check_top(rm);\n\t\t}\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'int BN_div_no_branch(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,\n\tconst BIGNUM *divisor, BN_CTX *ctx)\n\t{\n\tint norm_shift,i,loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tint num_n,div_n;\n\tbn_check_top(dv);\n\tbn_check_top(rm);\n\tbn_check_top(num);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\n\t\t}\n\tBN_CTX_start(ctx);\n\ttmp=BN_CTX_get(ctx);\n\tsnum=BN_CTX_get(ctx);\n\tsdiv=BN_CTX_get(ctx);\n\tif (dv == NULL)\n\t\tres=BN_CTX_get(ctx);\n\telse\tres=dv;\n\tif (sdiv == NULL || res == NULL) goto err;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tif (!(BN_lshift(sdiv,divisor,norm_shift))) goto err;\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tif (!(BN_lshift(snum,num,norm_shift))) goto err;\n\tsnum->neg=0;\n\tif (snum->top <= sdiv->top+1)\n\t\t{\n\t\tif (bn_wexpand(snum, sdiv->top + 2) == NULL) goto err;\n\t\tfor (i = snum->top; i < sdiv->top + 2; i++) snum->d[i] = 0;\n\t\tsnum->top = sdiv->top + 2;\n\t\t}\n\telse\n\t\t{\n\t\tif (bn_wexpand(snum, snum->top + 1) == NULL) goto err;\n\t\tsnum->d[snum->top] = 0;\n\t\tsnum->top ++;\n\t\t}\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\twnum.neg = 0;\n\twnum.d = &(snum->d[loop]);\n\twnum.top = div_n;\n\twnum.dmax = snum->dmax - loop;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop-1;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (res->top == 0)\n\t\tres->neg = 0;\n\telse\n\t\tresp--;\n\tfor (i=0; i<loop-1; i++, wnump--, resp--)\n\t\t{\n\t\tBN_ULONG q,l0;\n#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n\t\tBN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG);\n\t\tq=bn_div_3_words(wnump,d1,d0);\n#else\n\t\tBN_ULONG n0,n1,rem=0;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\t{\n#ifdef BN_LLONG\n\t\t\tBN_ULLONG t2;\n#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n\t\t\tq=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);\n#else\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tt2 -= d1;\n\t\t\t\t}\n#else\n\t\t\tBN_ULONG t2l,t2h,ql,qh;\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#if defined(BN_UMULT_LOHI)\n\t\t\tBN_UMULT_LOHI(t2l,t2h,d1,q);\n#elif defined(BN_UMULT_HIGH)\n\t\t\tt2l = d1 * q;\n\t\t\tt2h = BN_UMULT_HIGH(d1,q);\n#else\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n#endif\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif ((t2h < rem) ||\n\t\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t\t}\n#endif\n\t\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\twnum.d--;\n\t\tif (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1))\n\t\t\t{\n\t\t\tq--;\n\t\t\tif (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n\t\t\t\t(*wnump)++;\n\t\t\t}\n\t\t*resp = q;\n\t\t}\n\tbn_correct_top(snum);\n\tif (rm != NULL)\n\t\t{\n\t\tint neg = num->neg;\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\tif (!BN_is_zero(rm))\n\t\t\trm->neg = neg;\n\t\tbn_check_top(rm);\n\t\t}\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n\t{\n\treturn st->indexes[--(st->depth)];\n\t}']
60
0
https://github.com/openssl/openssl/blob/54d00677f305375eee65a0c9edb5f0980c5f020f/crypto/bn/bn_lib.c/#L365
int BN_set_word(BIGNUM *a, BN_ULONG w) { bn_check_top(a); if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL) return 0; a->neg = 0; a->d[0] = w; a->top = (w ? 1 : 0); a->flags &= ~BN_FLG_FIXED_TOP; bn_check_top(a); return 1; }
['static int rsa_ossl_public_decrypt(int flen, const unsigned char *from,\n unsigned char *to, RSA *rsa, int padding)\n{\n BIGNUM *f, *ret;\n int i, num = 0, r = -1;\n unsigned char *buf = NULL;\n BN_CTX *ctx = NULL;\n if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {\n RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_MODULUS_TOO_LARGE);\n return -1;\n }\n if (BN_ucmp(rsa->n, rsa->e) <= 0) {\n RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);\n return -1;\n }\n if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {\n if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {\n RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);\n return -1;\n }\n }\n if ((ctx = BN_CTX_new()) == NULL)\n goto err;\n BN_CTX_start(ctx);\n f = BN_CTX_get(ctx);\n ret = BN_CTX_get(ctx);\n num = BN_num_bytes(rsa->n);\n buf = OPENSSL_malloc(num);\n if (ret == NULL || buf == NULL) {\n RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (flen > num) {\n RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_DATA_GREATER_THAN_MOD_LEN);\n goto err;\n }\n if (BN_bin2bn(from, flen, f) == NULL)\n goto err;\n if (BN_ucmp(f, rsa->n) >= 0) {\n RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT,\n RSA_R_DATA_TOO_LARGE_FOR_MODULUS);\n goto err;\n }\n if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)\n if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,\n rsa->n, ctx))\n goto err;\n if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,\n rsa->_method_mod_n))\n goto err;\n if ((padding == RSA_X931_PADDING) && ((bn_get_words(ret)[0] & 0xf) != 12))\n if (!BN_sub(ret, rsa->n, ret))\n goto err;\n i = BN_bn2binpad(ret, buf, num);\n switch (padding) {\n case RSA_PKCS1_PADDING:\n r = RSA_padding_check_PKCS1_type_1(to, num, buf, i, num);\n break;\n case RSA_X931_PADDING:\n r = RSA_padding_check_X931(to, num, buf, i, num);\n break;\n case RSA_NO_PADDING:\n memcpy(to, buf, (r = i));\n break;\n default:\n RSAerr(RSA_F_RSA_OSSL_PUBLIC_DECRYPT, RSA_R_UNKNOWN_PADDING_TYPE);\n goto err;\n }\n if (r < 0)\n RSAerr(RSA_F_RSA_OSSL_PUBLIC_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}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ret->flags &= (~BN_FLG_CONSTTIME);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n a->flags &= ~BN_FLG_FIXED_TOP;\n bn_check_top(a);\n return 1;\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int ret, r_neg, cmp_res;\n bn_check_top(a);\n bn_check_top(b);\n if (a->neg != b->neg) {\n r_neg = a->neg;\n ret = BN_uadd(r, a, b);\n } else {\n cmp_res = BN_ucmp(a, b);\n if (cmp_res > 0) {\n r_neg = a->neg;\n ret = BN_usub(r, a, b);\n } else if (cmp_res < 0) {\n r_neg = !b->neg;\n ret = BN_usub(r, b, a);\n } else {\n r_neg = 0;\n BN_zero(r);\n ret = 1;\n }\n }\n r->neg = r_neg;\n bn_check_top(r);\n return ret;\n}']
61
0
https://github.com/openssl/openssl/blob/5dfc369ffcdc4722482c818e6ba6cf6e704c2cb5/crypto/asn1/asn1_lib.c/#L222
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; }
['unsigned char *ASN1_seq_pack(STACK *safes, int (*i2d)(), unsigned char **buf,\n\t int *len)\n{\n\tint safelen;\n\tunsigned char *safe, *p;\n\tif (!(safelen = i2d_ASN1_SET(safes, NULL, i2d, V_ASN1_SEQUENCE,\n\t\t\t\t\t V_ASN1_UNIVERSAL, IS_SEQUENCE))) {\n\t\tASN1err(ASN1_F_ASN1_SEQ_PACK,ASN1_R_ENCODE_ERROR);\n\t\treturn NULL;\n\t}\n\tif (!(safe = Malloc (safelen))) {\n\t\tASN1err(ASN1_F_ASN1_SEQ_PACK,ERR_R_MALLOC_FAILURE);\n\t\treturn NULL;\n\t}\n\tp = safe;\n\ti2d_ASN1_SET(safes, &p, i2d, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL,\n\t\t\t\t\t\t\t\t IS_SEQUENCE);\n\tif (len) *len = safelen;\n\tif (buf) *buf = safe;\n\treturn safe;\n}', 'int i2d_ASN1_SET(STACK *a, unsigned char **pp, int (*func)(), int ex_tag,\n\t int ex_class, int is_set)\n\t{\n\tint ret=0,r;\n\tint i;\n\tunsigned char *p;\n unsigned char *pStart, *pTempMem;\n MYBLOB *rgSetBlob;\n int totSize;\n\tif (a == NULL) return(0);\n\tfor (i=sk_num(a)-1; i>=0; i--)\n\t\tret+=func(sk_value(a,i),NULL);\n\tr=ASN1_object_size(1,ret,ex_tag);\n\tif (pp == NULL) return(r);\n\tp= *pp;\n\tASN1_put_object(&p,1,ret,ex_tag,ex_class);\n\tif(!is_set || (sk_num(a) < 2))\n\t\t{\n\t\tfor (i=0; i<sk_num(a); i++)\n \tfunc(sk_value(a,i),&p);\n\t\t*pp=p;\n\t\treturn(r);\n\t\t}\n pStart = p;\n rgSetBlob = (MYBLOB *)Malloc( sk_num(a) * sizeof(MYBLOB));\n for (i=0; i<sk_num(a); i++)\n\t {\n rgSetBlob[i].pbData = p;\n func(sk_value(a,i),&p);\n rgSetBlob[i].cbData = p - rgSetBlob[i].pbData;\n\t\t}\n *pp=p;\n totSize = p - pStart;\n qsort( rgSetBlob, sk_num(a), sizeof(MYBLOB), SetBlobCmp);\n pTempMem = Malloc(totSize);\n p = pTempMem;\n for(i=0; i<sk_num(a); ++i)\n\t\t{\n memcpy(p, rgSetBlob[i].pbData, rgSetBlob[i].cbData);\n p += rgSetBlob[i].cbData;\n\t\t}\n memcpy(pStart, pTempMem, totSize);\n Free(pTempMem);\n Free(rgSetBlob);\n return(r);\n }', 'void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,\n\t int xclass)\n\t{\n\tunsigned char *p= *pp;\n\tint i;\n\ti=(constructed)?V_ASN1_CONSTRUCTED:0;\n\ti|=(xclass&V_ASN1_PRIVATE);\n\tif (tag < 31)\n\t\t*(p++)=i|(tag&V_ASN1_PRIMATIVE_TAG);\n\telse\n\t\t{\n\t\t*(p++)=i|V_ASN1_PRIMATIVE_TAG;\n\t\twhile (tag > 0x7f)\n\t\t\t{\n\t\t\t*(p++)=(tag&0x7f)|0x80;\n\t\t\ttag>>=7;\n\t\t\t}\n\t\t*(p++)=(tag&0x7f);\n\t\t}\n\tif ((constructed == 2) && (length == 0))\n\t\t*(p++)=0x80;\n\telse\n\t\tasn1_put_length(&p,length);\n\t*pp=p;\n\t}', '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}']
62
0
https://github.com/openssl/openssl/blob/4bf4bc784f12bcdc3a3e772f85f6d33f5eccdab3/crypto/engine/hw_4758_cca.c/#L574
static int cca_rsa_priv_dec(int flen, const unsigned char *from, unsigned char *to, RSA *rsa,int padding) { long returnCode; long reasonCode; long lflen = flen; long exitDataLength = 0; unsigned char exitData[8]; long ruleArrayLength = 1; unsigned char ruleArray[8] = "PKCS-1.2"; long dataStructureLength = 0; unsigned char dataStructure[8]; long outputLength = RSA_size(rsa); long keyTokenLength; unsigned char* keyToken = (unsigned char*)RSA_get_ex_data(rsa, hndidx); keyTokenLength = *(long*)keyToken; keyToken+=sizeof(long); pkaDecrypt(&returnCode, &reasonCode, &exitDataLength, exitData, &ruleArrayLength, ruleArray, &lflen, (unsigned char*)from, &dataStructureLength, dataStructure, &keyTokenLength, keyToken, &outputLength, to); return (returnCode | reasonCode) ? 0 : 1; }
['static int cca_rsa_priv_dec(int flen, const unsigned char *from,\n\t\t\tunsigned char *to, RSA *rsa,int padding)\n\t{\n\tlong returnCode;\n\tlong reasonCode;\n\tlong lflen = flen;\n\tlong exitDataLength = 0;\n\tunsigned char exitData[8];\n\tlong ruleArrayLength = 1;\n\tunsigned char ruleArray[8] = "PKCS-1.2";\n\tlong dataStructureLength = 0;\n\tunsigned char dataStructure[8];\n\tlong outputLength = RSA_size(rsa);\n\tlong keyTokenLength;\n\tunsigned char* keyToken = (unsigned char*)RSA_get_ex_data(rsa, hndidx);\n\tkeyTokenLength = *(long*)keyToken;\n\tkeyToken+=sizeof(long);\n\tpkaDecrypt(&returnCode, &reasonCode, &exitDataLength, exitData,\n\t\t&ruleArrayLength, ruleArray, &lflen, (unsigned char*)from,\n\t\t&dataStructureLength, dataStructure, &keyTokenLength,\n\t\tkeyToken, &outputLength, to);\n\treturn (returnCode | reasonCode) ? 0 : 1;\n\t}', 'int RSA_size(const RSA *r)\n\t{\n\treturn(BN_num_bytes(r->n));\n\t}', 'int BN_num_bits(const BIGNUM *a)\n\t{\n\tBN_ULONG l;\n\tint i;\n\tbn_check_top(a);\n\tif (a->top == 0) return(0);\n\tl=a->d[a->top-1];\n\tassert(l != 0);\n\ti=(a->top-1)*BN_BITS2;\n\treturn(i+BN_num_bits_word(l));\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}']
63
0
https://github.com/openssl/openssl/blob/f9df0a7775f483c175cda5832360cccd1db6943a/crypto/bn/bn_lib.c/#L404
int BN_set_word(BIGNUM *a, BN_ULONG w) { bn_check_top(a); if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL) return (0); a->neg = 0; a->d[0] = w; a->top = (w ? 1 : 0); bn_check_top(a); return 1; }
['BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)\n{\n BIGNUM *ret = in;\n int err = 1;\n int r;\n BIGNUM *A, *b, *q, *t, *x, *y;\n int e, i, j;\n if (!BN_is_odd(p) || BN_abs_is_word(p, 1)) {\n if (BN_abs_is_word(p, 2)) {\n if (ret == NULL)\n ret = BN_new();\n if (ret == NULL)\n goto end;\n if (!BN_set_word(ret, BN_is_bit_set(a, 0))) {\n if (ret != in)\n BN_free(ret);\n return NULL;\n }\n bn_check_top(ret);\n return ret;\n }\n BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);\n return (NULL);\n }\n if (BN_is_zero(a) || BN_is_one(a)) {\n if (ret == NULL)\n ret = BN_new();\n if (ret == NULL)\n goto end;\n if (!BN_set_word(ret, BN_is_one(a))) {\n if (ret != in)\n BN_free(ret);\n return NULL;\n }\n bn_check_top(ret);\n return ret;\n }\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n b = BN_CTX_get(ctx);\n q = BN_CTX_get(ctx);\n t = BN_CTX_get(ctx);\n x = BN_CTX_get(ctx);\n y = BN_CTX_get(ctx);\n if (y == NULL)\n goto end;\n if (ret == NULL)\n ret = BN_new();\n if (ret == NULL)\n goto end;\n if (!BN_nnmod(A, a, p, ctx))\n goto end;\n e = 1;\n while (!BN_is_bit_set(p, e))\n e++;\n if (e == 1) {\n if (!BN_rshift(q, p, 2))\n goto end;\n q->neg = 0;\n if (!BN_add_word(q, 1))\n goto end;\n if (!BN_mod_exp(ret, A, q, p, ctx))\n goto end;\n err = 0;\n goto vrfy;\n }\n if (e == 2) {\n if (!BN_mod_lshift1_quick(t, A, p))\n goto end;\n if (!BN_rshift(q, p, 3))\n goto end;\n q->neg = 0;\n if (!BN_mod_exp(b, t, q, p, ctx))\n goto end;\n if (!BN_mod_sqr(y, b, p, ctx))\n goto end;\n if (!BN_mod_mul(t, t, y, p, ctx))\n goto end;\n if (!BN_sub_word(t, 1))\n goto end;\n if (!BN_mod_mul(x, A, b, p, ctx))\n goto end;\n if (!BN_mod_mul(x, x, t, p, ctx))\n goto end;\n if (!BN_copy(ret, x))\n goto end;\n err = 0;\n goto vrfy;\n }\n if (!BN_copy(q, p))\n goto end;\n q->neg = 0;\n i = 2;\n do {\n if (i < 22) {\n if (!BN_set_word(y, i))\n goto end;\n } else {\n if (!BN_rand(y, BN_num_bits(p), 0, 0))\n goto end;\n if (BN_ucmp(y, p) >= 0) {\n if (!(p->neg ? BN_add : BN_sub) (y, y, p))\n goto end;\n }\n if (BN_is_zero(y))\n if (!BN_set_word(y, i))\n goto end;\n }\n r = BN_kronecker(y, q, ctx);\n if (r < -1)\n goto end;\n if (r == 0) {\n BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);\n goto end;\n }\n }\n while (r == 1 && ++i < 82);\n if (r != -1) {\n BNerr(BN_F_BN_MOD_SQRT, BN_R_TOO_MANY_ITERATIONS);\n goto end;\n }\n if (!BN_rshift(q, q, e))\n goto end;\n if (!BN_mod_exp(y, y, q, p, ctx))\n goto end;\n if (BN_is_one(y)) {\n BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);\n goto end;\n }\n if (!BN_rshift1(t, q))\n goto end;\n if (BN_is_zero(t)) {\n if (!BN_nnmod(t, A, p, ctx))\n goto end;\n if (BN_is_zero(t)) {\n BN_zero(ret);\n err = 0;\n goto end;\n } else if (!BN_one(x))\n goto end;\n } else {\n if (!BN_mod_exp(x, A, t, p, ctx))\n goto end;\n if (BN_is_zero(x)) {\n BN_zero(ret);\n err = 0;\n goto end;\n }\n }\n if (!BN_mod_sqr(b, x, p, ctx))\n goto end;\n if (!BN_mod_mul(b, b, A, p, ctx))\n goto end;\n if (!BN_mod_mul(x, x, A, p, ctx))\n goto end;\n while (1) {\n if (BN_is_one(b)) {\n if (!BN_copy(ret, x))\n goto end;\n err = 0;\n goto vrfy;\n }\n i = 1;\n if (!BN_mod_sqr(t, b, p, ctx))\n goto end;\n while (!BN_is_one(t)) {\n i++;\n if (i == e) {\n BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE);\n goto end;\n }\n if (!BN_mod_mul(t, t, t, p, ctx))\n goto end;\n }\n if (!BN_copy(t, y))\n goto end;\n for (j = e - i - 1; j > 0; j--) {\n if (!BN_mod_sqr(t, t, p, ctx))\n goto end;\n }\n if (!BN_mod_mul(y, t, t, p, ctx))\n goto end;\n if (!BN_mod_mul(x, x, t, p, ctx))\n goto end;\n if (!BN_mod_mul(b, b, y, p, ctx))\n goto end;\n e = i;\n }\n vrfy:\n if (!err) {\n if (!BN_mod_sqr(x, ret, p, ctx))\n err = 1;\n if (!err && 0 != BN_cmp(x, A)) {\n BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE);\n err = 1;\n }\n }\n end:\n if (err) {\n if (ret != in)\n BN_clear_free(ret);\n ret = NULL;\n }\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return 1;\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'int BN_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}']
64
0
https://github.com/openssl/openssl/blob/02cba628daa7fea959c561531a8a984756bdf41c/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; } } }
['void SSL_free(SSL *s)\n{\n int i;\n if (s == NULL)\n return;\n CRYPTO_DOWN_REF(&s->references, &i, s->lock);\n REF_PRINT_COUNT("SSL", s);\n if (i > 0)\n return;\n REF_ASSERT_ISNT(i < 0);\n X509_VERIFY_PARAM_free(s->param);\n dane_final(&s->dane);\n CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);\n ssl_free_wbio_buffer(s);\n BIO_free_all(s->wbio);\n BIO_free_all(s->rbio);\n BUF_MEM_free(s->init_buf);\n sk_SSL_CIPHER_free(s->cipher_list);\n sk_SSL_CIPHER_free(s->cipher_list_by_id);\n if (s->session != NULL) {\n ssl_clear_bad_session(s);\n SSL_SESSION_free(s->session);\n }\n clear_ciphers(s);\n ssl_cert_free(s->cert);\n OPENSSL_free(s->ext.hostname);\n SSL_CTX_free(s->session_ctx);\n#ifndef OPENSSL_NO_EC\n OPENSSL_free(s->ext.ecpointformats);\n OPENSSL_free(s->ext.supportedgroups);\n#endif\n sk_X509_EXTENSION_pop_free(s->ext.ocsp.exts, X509_EXTENSION_free);\n#ifndef OPENSSL_NO_OCSP\n sk_OCSP_RESPID_pop_free(s->ext.ocsp.ids, OCSP_RESPID_free);\n#endif\n#ifndef OPENSSL_NO_CT\n SCT_LIST_free(s->scts);\n OPENSSL_free(s->ext.scts);\n#endif\n OPENSSL_free(s->ext.ocsp.resp);\n OPENSSL_free(s->ext.alpn);\n sk_X509_NAME_pop_free(s->client_CA, X509_NAME_free);\n sk_X509_pop_free(s->verified_chain, X509_free);\n if (s->method != NULL)\n s->method->ssl_free(s);\n RECORD_LAYER_release(&s->rlayer);\n SSL_CTX_free(s->ctx);\n ASYNC_WAIT_CTX_free(s->waitctx);\n#if !defined(OPENSSL_NO_NEXTPROTONEG)\n OPENSSL_free(s->ext.npn);\n#endif\n#ifndef OPENSSL_NO_SRTP\n sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);\n#endif\n CRYPTO_THREAD_lock_free(s->lock);\n OPENSSL_free(s);\n}', '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 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->ext.ecpointformats);\n OPENSSL_free(a->ext.supportedgroups);\n#endif\n OPENSSL_free(a->ext.alpn);\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}']
65
0
https://github.com/openssl/openssl/blob/b59e1bed7da7933d4c6af750fe3f0300b57874fe/apps/speed.c/#L2306
static int do_multi(int multi) { int n; int fd[2]; int *fds; static char sep[] = ":"; fds = malloc(sizeof(*fds) * multi); for (n = 0; n < multi; ++n) { if (pipe(fd) == -1) { BIO_printf(bio_err, "pipe failure\n"); exit(1); } fflush(stdout); (void)BIO_flush(bio_err); if (fork()) { close(fd[1]); fds[n] = fd[0]; } else { close(fd[0]); close(1); if (dup(fd[1]) == -1) { BIO_printf(bio_err, "dup failed\n"); exit(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] != '+') { BIO_printf(bio_err, "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) == 0) { 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) == 0) { 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; } # ifndef OPENSSL_NO_DSA else if (strncmp(buf, "+F3:", 4) == 0) { 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; } # endif # ifndef OPENSSL_NO_EC else if (strncmp(buf, "+F4:", 4) == 0) { 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_EC else if (strncmp(buf, "+F5:", 4) == 0) { 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) == 0) { ; } else BIO_printf(bio_err, "Unknown type '%s' from child %d\n", buf, n); } fclose(f); } free(fds); return 1; }
['static int do_multi(int multi)\n{\n int n;\n int fd[2];\n int *fds;\n static char sep[] = ":";\n fds = malloc(sizeof(*fds) * multi);\n for (n = 0; n < multi; ++n) {\n if (pipe(fd) == -1) {\n BIO_printf(bio_err, "pipe failure\\n");\n exit(1);\n }\n fflush(stdout);\n (void)BIO_flush(bio_err);\n if (fork()) {\n close(fd[1]);\n fds[n] = fd[0];\n } else {\n close(fd[0]);\n close(1);\n if (dup(fd[1]) == -1) {\n BIO_printf(bio_err, "dup failed\\n");\n exit(1);\n }\n close(fd[1]);\n mr = 1;\n usertime = 0;\n free(fds);\n return 0;\n }\n printf("Forked child %d\\n", n);\n }\n for (n = 0; n < multi; ++n) {\n FILE *f;\n char buf[1024];\n char *p;\n f = fdopen(fds[n], "r");\n while (fgets(buf, sizeof buf, f)) {\n p = strchr(buf, \'\\n\');\n if (p)\n *p = \'\\0\';\n if (buf[0] != \'+\') {\n BIO_printf(bio_err, "Don\'t understand line \'%s\' from child %d\\n",\n buf, n);\n continue;\n }\n printf("Got: %s from %d\\n", buf, n);\n if (strncmp(buf, "+F:", 3) == 0) {\n int alg;\n int j;\n p = buf + 3;\n alg = atoi(sstrsep(&p, sep));\n sstrsep(&p, sep);\n for (j = 0; j < SIZE_NUM; ++j)\n results[alg][j] += atof(sstrsep(&p, sep));\n } else if (strncmp(buf, "+F2:", 4) == 0) {\n int k;\n double d;\n p = buf + 4;\n k = atoi(sstrsep(&p, sep));\n sstrsep(&p, sep);\n d = atof(sstrsep(&p, sep));\n if (n)\n rsa_results[k][0] = 1 / (1 / rsa_results[k][0] + 1 / d);\n else\n rsa_results[k][0] = d;\n d = atof(sstrsep(&p, sep));\n if (n)\n rsa_results[k][1] = 1 / (1 / rsa_results[k][1] + 1 / d);\n else\n rsa_results[k][1] = d;\n }\n# ifndef OPENSSL_NO_DSA\n else if (strncmp(buf, "+F3:", 4) == 0) {\n int k;\n double d;\n p = buf + 4;\n k = atoi(sstrsep(&p, sep));\n sstrsep(&p, sep);\n d = atof(sstrsep(&p, sep));\n if (n)\n dsa_results[k][0] = 1 / (1 / dsa_results[k][0] + 1 / d);\n else\n dsa_results[k][0] = d;\n d = atof(sstrsep(&p, sep));\n if (n)\n dsa_results[k][1] = 1 / (1 / dsa_results[k][1] + 1 / d);\n else\n dsa_results[k][1] = d;\n }\n# endif\n# ifndef OPENSSL_NO_EC\n else if (strncmp(buf, "+F4:", 4) == 0) {\n int k;\n double d;\n p = buf + 4;\n k = atoi(sstrsep(&p, sep));\n sstrsep(&p, sep);\n d = atof(sstrsep(&p, sep));\n if (n)\n ecdsa_results[k][0] =\n 1 / (1 / ecdsa_results[k][0] + 1 / d);\n else\n ecdsa_results[k][0] = d;\n d = atof(sstrsep(&p, sep));\n if (n)\n ecdsa_results[k][1] =\n 1 / (1 / ecdsa_results[k][1] + 1 / d);\n else\n ecdsa_results[k][1] = d;\n }\n# endif\n# ifndef OPENSSL_NO_EC\n else if (strncmp(buf, "+F5:", 4) == 0) {\n int k;\n double d;\n p = buf + 4;\n k = atoi(sstrsep(&p, sep));\n sstrsep(&p, sep);\n d = atof(sstrsep(&p, sep));\n if (n)\n ecdh_results[k][0] = 1 / (1 / ecdh_results[k][0] + 1 / d);\n else\n ecdh_results[k][0] = d;\n }\n# endif\n else if (strncmp(buf, "+H:", 3) == 0) {\n ;\n } else\n BIO_printf(bio_err, "Unknown type \'%s\' from child %d\\n", buf, n);\n }\n fclose(f);\n }\n free(fds);\n return 1;\n}', 'long BIO_ctrl(BIO *b, int cmd, long larg, void *parg)\n{\n long ret;\n long (*cb) (BIO *, int, const char *, int, long, long);\n if (b == NULL)\n return (0);\n if ((b->method == NULL) || (b->method->ctrl == NULL)) {\n BIOerr(BIO_F_BIO_CTRL, BIO_R_UNSUPPORTED_METHOD);\n return (-2);\n }\n cb = b->callback;\n if ((cb != NULL) &&\n ((ret = cb(b, BIO_CB_CTRL, parg, cmd, larg, 1L)) <= 0))\n return (ret);\n ret = b->method->ctrl(b, cmd, larg, parg);\n if (cb != NULL)\n ret = cb(b, BIO_CB_CTRL | BIO_CB_RETURN, parg, cmd, larg, ret);\n return (ret);\n}']
66
0
https://github.com/openssl/openssl/blob/8fa6a40be2935ca109a28cc43d28cd27051ada01/apps/speed.c/#L2580
static int do_multi(int multi) { int n; int fd[2]; int *fds; static char sep[]=":"; fds=malloc(multi*sizeof *fds); for(n=0 ; n < multi ; ++n) { pipe(fd); if(fork()) { close(fd[1]); fds[n]=fd[0]; } else { close(fd[0]); close(1); dup(fd[1]); close(fd[1]); mr=1; usertime=0; return 0; } printf("Forked child %d\n",n); } for(n=0 ; n < multi ; ++n) { FILE *f; char buf[1024]; char *p; f=fdopen(fds[n],"r"); while(fgets(buf,sizeof buf,f)) { p=strchr(buf,'\n'); if(p) *p='\0'; if(buf[0] != '+') { fprintf(stderr,"Don't understand line '%s' from child %d\n", buf,n); continue; } printf("Got: %s from %d\n",buf,n); if(!strncmp(buf,"+F:",3)) { int alg; int j; p=buf+3; alg=atoi(sstrsep(&p,sep)); sstrsep(&p,sep); for(j=0 ; j < SIZE_NUM ; ++j) results[alg][j]+=atof(sstrsep(&p,sep)); } else if(!strncmp(buf,"+F2:",4)) { int k; double d; p=buf+4; k=atoi(sstrsep(&p,sep)); sstrsep(&p,sep); d=atof(sstrsep(&p,sep)); if(n) rsa_results[k][0]=1/(1/rsa_results[k][0]+1/d); else rsa_results[k][0]=d; d=atof(sstrsep(&p,sep)); if(n) rsa_results[k][1]=1/(1/rsa_results[k][1]+1/d); else rsa_results[k][1]=d; } else if(!strncmp(buf,"+F2:",4)) { int k; double d; p=buf+4; k=atoi(sstrsep(&p,sep)); sstrsep(&p,sep); d=atof(sstrsep(&p,sep)); if(n) rsa_results[k][0]=1/(1/rsa_results[k][0]+1/d); else rsa_results[k][0]=d; d=atof(sstrsep(&p,sep)); if(n) rsa_results[k][1]=1/(1/rsa_results[k][1]+1/d); else rsa_results[k][1]=d; } else if(!strncmp(buf,"+F3:",4)) { int k; double d; p=buf+4; k=atoi(sstrsep(&p,sep)); sstrsep(&p,sep); d=atof(sstrsep(&p,sep)); if(n) dsa_results[k][0]=1/(1/dsa_results[k][0]+1/d); else dsa_results[k][0]=d; d=atof(sstrsep(&p,sep)); if(n) dsa_results[k][1]=1/(1/dsa_results[k][1]+1/d); else dsa_results[k][1]=d; } #ifndef OPENSSL_NO_ECDSA else if(!strncmp(buf,"+F4:",4)) { int k; double d; p=buf+4; k=atoi(sstrsep(&p,sep)); sstrsep(&p,sep); d=atof(sstrsep(&p,sep)); if(n) ecdsa_results[k][0]=1/(1/ecdsa_results[k][0]+1/d); else ecdsa_results[k][0]=d; d=atof(sstrsep(&p,sep)); if(n) ecdsa_results[k][1]=1/(1/ecdsa_results[k][1]+1/d); else ecdsa_results[k][1]=d; } #endif #ifndef OPENSSL_NO_ECDH else if(!strncmp(buf,"+F5:",4)) { int k; double d; p=buf+4; k=atoi(sstrsep(&p,sep)); sstrsep(&p,sep); d=atof(sstrsep(&p,sep)); if(n) ecdh_results[k][0]=1/(1/ecdh_results[k][0]+1/d); else ecdh_results[k][0]=d; } #endif else if(!strncmp(buf,"+H:",3)) { } else fprintf(stderr,"Unknown type '%s' from child %d\n",buf,n); } } return 1; }
['static int do_multi(int multi)\n\t{\n\tint n;\n\tint fd[2];\n\tint *fds;\n\tstatic char sep[]=":";\n\tfds=malloc(multi*sizeof *fds);\n\tfor(n=0 ; n < multi ; ++n)\n\t\t{\n\t\tpipe(fd);\n\t\tif(fork())\n\t\t\t{\n\t\t\tclose(fd[1]);\n\t\t\tfds[n]=fd[0];\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tclose(fd[0]);\n\t\t\tclose(1);\n\t\t\tdup(fd[1]);\n\t\t\tclose(fd[1]);\n\t\t\tmr=1;\n\t\t\tusertime=0;\n\t\t\treturn 0;\n\t\t\t}\n\t\tprintf("Forked child %d\\n",n);\n\t\t}\n\tfor(n=0 ; n < multi ; ++n)\n\t\t{\n\t\tFILE *f;\n\t\tchar buf[1024];\n\t\tchar *p;\n\t\tf=fdopen(fds[n],"r");\n\t\twhile(fgets(buf,sizeof buf,f))\n\t\t\t{\n\t\t\tp=strchr(buf,\'\\n\');\n\t\t\tif(p)\n\t\t\t\t*p=\'\\0\';\n\t\t\tif(buf[0] != \'+\')\n\t\t\t\t{\n\t\t\t\tfprintf(stderr,"Don\'t understand line \'%s\' from child %d\\n",\n\t\t\t\t\t\tbuf,n);\n\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\tprintf("Got: %s from %d\\n",buf,n);\n\t\t\tif(!strncmp(buf,"+F:",3))\n\t\t\t\t{\n\t\t\t\tint alg;\n\t\t\t\tint j;\n\t\t\t\tp=buf+3;\n\t\t\t\talg=atoi(sstrsep(&p,sep));\n\t\t\t\tsstrsep(&p,sep);\n\t\t\t\tfor(j=0 ; j < SIZE_NUM ; ++j)\n\t\t\t\t\tresults[alg][j]+=atof(sstrsep(&p,sep));\n\t\t\t\t}\n\t\t\telse if(!strncmp(buf,"+F2:",4))\n\t\t\t\t{\n\t\t\t\tint k;\n\t\t\t\tdouble d;\n\t\t\t\tp=buf+4;\n\t\t\t\tk=atoi(sstrsep(&p,sep));\n\t\t\t\tsstrsep(&p,sep);\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\trsa_results[k][0]=1/(1/rsa_results[k][0]+1/d);\n\t\t\t\telse\n\t\t\t\t\trsa_results[k][0]=d;\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\trsa_results[k][1]=1/(1/rsa_results[k][1]+1/d);\n\t\t\t\telse\n\t\t\t\t\trsa_results[k][1]=d;\n\t\t\t\t}\n\t\t\telse if(!strncmp(buf,"+F2:",4))\n\t\t\t\t{\n\t\t\t\tint k;\n\t\t\t\tdouble d;\n\t\t\t\tp=buf+4;\n\t\t\t\tk=atoi(sstrsep(&p,sep));\n\t\t\t\tsstrsep(&p,sep);\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\trsa_results[k][0]=1/(1/rsa_results[k][0]+1/d);\n\t\t\t\telse\n\t\t\t\t\trsa_results[k][0]=d;\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\trsa_results[k][1]=1/(1/rsa_results[k][1]+1/d);\n\t\t\t\telse\n\t\t\t\t\trsa_results[k][1]=d;\n\t\t\t\t}\n\t\t\telse if(!strncmp(buf,"+F3:",4))\n\t\t\t\t{\n\t\t\t\tint k;\n\t\t\t\tdouble d;\n\t\t\t\tp=buf+4;\n\t\t\t\tk=atoi(sstrsep(&p,sep));\n\t\t\t\tsstrsep(&p,sep);\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\tdsa_results[k][0]=1/(1/dsa_results[k][0]+1/d);\n\t\t\t\telse\n\t\t\t\t\tdsa_results[k][0]=d;\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\tdsa_results[k][1]=1/(1/dsa_results[k][1]+1/d);\n\t\t\t\telse\n\t\t\t\t\tdsa_results[k][1]=d;\n\t\t\t\t}\n#ifndef OPENSSL_NO_ECDSA\n\t\t\telse if(!strncmp(buf,"+F4:",4))\n\t\t\t\t{\n\t\t\t\tint k;\n\t\t\t\tdouble d;\n\t\t\t\tp=buf+4;\n\t\t\t\tk=atoi(sstrsep(&p,sep));\n\t\t\t\tsstrsep(&p,sep);\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\tecdsa_results[k][0]=1/(1/ecdsa_results[k][0]+1/d);\n\t\t\t\telse\n\t\t\t\t\tecdsa_results[k][0]=d;\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\tecdsa_results[k][1]=1/(1/ecdsa_results[k][1]+1/d);\n\t\t\t\telse\n\t\t\t\t\tecdsa_results[k][1]=d;\n\t\t\t\t}\n#endif\n#ifndef OPENSSL_NO_ECDH\n\t\t\telse if(!strncmp(buf,"+F5:",4))\n\t\t\t\t{\n\t\t\t\tint k;\n\t\t\t\tdouble d;\n\t\t\t\tp=buf+4;\n\t\t\t\tk=atoi(sstrsep(&p,sep));\n\t\t\t\tsstrsep(&p,sep);\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\tecdh_results[k][0]=1/(1/ecdh_results[k][0]+1/d);\n\t\t\t\telse\n\t\t\t\t\tecdh_results[k][0]=d;\n\t\t\t\t}\n#endif\n\t\t\telse if(!strncmp(buf,"+H:",3))\n\t\t\t\t{\n\t\t\t\t}\n\t\t\telse\n\t\t\t\tfprintf(stderr,"Unknown type \'%s\' from child %d\\n",buf,n);\n\t\t\t}\n\t\t}\n\treturn 1;\n\t}']
67
0
https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/ffmpeg.c/#L3465
static void opt_inter_matrix(const char *arg) { inter_matrix = av_mallocz(sizeof(uint16_t) * 64); parse_matrix_coeffs(inter_matrix, arg); }
['static void opt_inter_matrix(const char *arg)\n{\n inter_matrix = av_mallocz(sizeof(uint16_t) * 64);\n parse_matrix_coeffs(inter_matrix, arg);\n}', 'void *av_mallocz(unsigned int size)\n{\n void *ptr = av_malloc(size);\n if (ptr)\n memset(ptr, 0, size);\n return ptr;\n}', 'void *av_malloc(unsigned int size)\n{\n void *ptr;\n#ifdef CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-16) )\n return NULL;\n#ifdef CONFIG_MEMALIGN_HACK\n ptr = malloc(size+16);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&15) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif defined (HAVE_MEMALIGN)\n ptr = memalign(16,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}', 'static void parse_matrix_coeffs(uint16_t *dest, const char *str)\n{\n int i;\n const char *p = str;\n for(i = 0;; i++) {\n dest[i] = atoi(p);\n if(i == 63)\n break;\n p = strchr(p, \',\');\n if(!p) {\n fprintf(stderr, "Syntax error in matrix \\"%s\\" at coeff %d\\n", str, i);\n exit(1);\n }\n p++;\n }\n}']
68
0
https://github.com/libav/libav/blob/60d4c6ff76467d4d8f55c1cc61ab6c618e8ea2f3/libavformat/mpegts.c/#L749
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,\n 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 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}']
69
0
https://github.com/openssl/openssl/blob/d636aa7109aceeb870df83c4978b271824617530/ssl/d1_pkt.c/#L658
int dtls1_get_record(SSL *s) { int ssl_major,ssl_minor; int i,n; SSL3_RECORD *rr; SSL_SESSION *sess; unsigned char *p; unsigned short version; DTLS1_BITMAP *bitmap; unsigned int is_next_epoch; rr= &(s->s3->rrec); sess=s->session; if ( ! dtls1_process_buffered_records(s)) return 0; if (dtls1_get_processed_record(s)) return 1; again: if ( (s->rstate != SSL_ST_READ_BODY) || (s->packet_length < DTLS1_RT_HEADER_LENGTH)) { n=ssl3_read_n(s, DTLS1_RT_HEADER_LENGTH, s->s3->rbuf.len, 0); if (n <= 0) return(n); if (s->packet_length != DTLS1_RT_HEADER_LENGTH) { s->packet_length = 0; goto again; } s->rstate=SSL_ST_READ_BODY; p=s->packet; rr->type= *(p++); ssl_major= *(p++); ssl_minor= *(p++); version=(ssl_major<<8)|ssl_minor; n2s(p,rr->epoch); memcpy(&(s->s3->read_sequence[2]), p, 6); p+=6; n2s(p,rr->length); if (!s->first_packet) { if (version != s->version) { rr->length = 0; s->packet_length = 0; goto again; } } if ((version & 0xff00) != (s->version & 0xff00)) { rr->length = 0; s->packet_length = 0; goto again; } if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) { rr->length = 0; s->packet_length = 0; goto again; } } if (rr->length > s->packet_length-DTLS1_RT_HEADER_LENGTH) { i=rr->length; n=ssl3_read_n(s,i,i,1); if (n <= 0) return(n); if ( n != i) { rr->length = 0; s->packet_length = 0; goto again; } } s->rstate=SSL_ST_READ_HEADER; bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch); if ( bitmap == NULL) { rr->length = 0; s->packet_length = 0; goto again; } if (!(s->d1->listen && rr->type == SSL3_RT_HANDSHAKE && *p == SSL3_MT_CLIENT_HELLO) && !dtls1_record_replay_check(s, bitmap)) { rr->length = 0; s->packet_length=0; goto again; } if (rr->length == 0) goto again; if (is_next_epoch) { dtls1_record_bitmap_update(s, bitmap); dtls1_buffer_record(s, &(s->d1->unprocessed_rcds), rr->seq_num); rr->length = 0; s->packet_length = 0; goto again; } if ( ! dtls1_process_record(s)) return(0); dtls1_clear_timeouts(s); return(1); }
['int dtls1_get_record(SSL *s)\n\t{\n\tint ssl_major,ssl_minor;\n\tint i,n;\n\tSSL3_RECORD *rr;\n\tSSL_SESSION *sess;\n\tunsigned char *p;\n\tunsigned short version;\n\tDTLS1_BITMAP *bitmap;\n\tunsigned int is_next_epoch;\n\trr= &(s->s3->rrec);\n\tsess=s->session;\n\tif ( ! dtls1_process_buffered_records(s))\n return 0;\n\tif (dtls1_get_processed_record(s))\n\t\treturn 1;\nagain:\n\tif (\t(s->rstate != SSL_ST_READ_BODY) ||\n\t\t(s->packet_length < DTLS1_RT_HEADER_LENGTH))\n\t\t{\n\t\tn=ssl3_read_n(s, DTLS1_RT_HEADER_LENGTH, s->s3->rbuf.len, 0);\n\t\tif (n <= 0) return(n);\n\t\tif (s->packet_length != DTLS1_RT_HEADER_LENGTH)\n\t\t\t{\n\t\t\ts->packet_length = 0;\n\t\t\tgoto again;\n\t\t\t}\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->epoch);\n\t\tmemcpy(&(s->s3->read_sequence[2]), p, 6);\n\t\tp+=6;\n\t\tn2s(p,rr->length);\n\t\tif (!s->first_packet)\n\t\t\t{\n\t\t\tif (version != s->version)\n\t\t\t\t{\n\t\t\t\trr->length = 0;\n\t\t\t\ts->packet_length = 0;\n\t\t\t\tgoto again;\n\t\t\t\t}\n\t\t\t}\n\t\tif ((version & 0xff00) != (s->version & 0xff00))\n\t\t\t{\n\t\t\trr->length = 0;\n\t\t\ts->packet_length = 0;\n\t\t\tgoto again;\n\t\t\t}\n\t\tif (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH)\n\t\t\t{\n\t\t\trr->length = 0;\n\t\t\ts->packet_length = 0;\n\t\t\tgoto again;\n\t\t\t}\n\t\t}\n\tif (rr->length > s->packet_length-DTLS1_RT_HEADER_LENGTH)\n\t\t{\n\t\ti=rr->length;\n\t\tn=ssl3_read_n(s,i,i,1);\n\t\tif (n <= 0) return(n);\n\t\tif ( n != i)\n\t\t\t{\n\t\t\trr->length = 0;\n\t\t\ts->packet_length = 0;\n\t\t\tgoto again;\n\t\t\t}\n\t\t}\n\ts->rstate=SSL_ST_READ_HEADER;\n\tbitmap = dtls1_get_bitmap(s, rr, &is_next_epoch);\n\tif ( bitmap == NULL)\n\t\t{\n\t\trr->length = 0;\n\t\ts->packet_length = 0;\n\t\tgoto again;\n\t\t}\n\tif (!(s->d1->listen && rr->type == SSL3_RT_HANDSHAKE &&\n\t\t*p == SSL3_MT_CLIENT_HELLO) &&\n\t\t!dtls1_record_replay_check(s, bitmap))\n\t\t{\n\t\trr->length = 0;\n\t\ts->packet_length=0;\n\t\tgoto again;\n\t\t}\n\tif (rr->length == 0) goto again;\n\tif (is_next_epoch)\n\t\t{\n\t\tdtls1_record_bitmap_update(s, bitmap);\n\t\tdtls1_buffer_record(s, &(s->d1->unprocessed_rcds), rr->seq_num);\n\t\trr->length = 0;\n\t\ts->packet_length = 0;\n\t\tgoto again;\n\t\t}\n\tif ( ! dtls1_process_record(s))\n\t\treturn(0);\n\tdtls1_clear_timeouts(s);\n\treturn(1);\n\t}']
70
0
https://github.com/libav/libav/blob/6cecd63005b29a1dc3a5104e6ac85fd112705122/libavcodec/interplayvideo.c/#L441
static int ipvideo_decode_block_opcode_0xA(IpvideoContext *s) { int x, y; unsigned char P[4]; int flags = 0; CHECK_STREAM_PTR(24); if (s->stream_ptr[0] <= s->stream_ptr[1]) { CHECK_STREAM_PTR(32); for (y = 0; y < 16; y++) { if (!(y & 3)) { memcpy(P, s->stream_ptr, 4); s->stream_ptr += 4; flags = bytestream_get_le32(&s->stream_ptr); } for (x = 0; x < 4; x++, flags >>= 2) *s->pixel_ptr++ = P[flags & 0x03]; s->pixel_ptr += s->stride - 4; if (y == 7) s->pixel_ptr -= 8 * s->stride - 4; } } else { int vert = s->stream_ptr[12] <= s->stream_ptr[13]; uint64_t flags = 0; for (y = 0; y < 16; y++) { if (!(y & 7)) { memcpy(P, s->stream_ptr, 4); s->stream_ptr += 4; flags = bytestream_get_le64(&s->stream_ptr); } for (x = 0; x < 4; x++, flags >>= 2) *s->pixel_ptr++ = P[flags & 0x03]; if (vert) { s->pixel_ptr += s->stride - 4; if (y == 7) s->pixel_ptr -= 8 * s->stride - 4; } else if (y & 1) s->pixel_ptr += s->line_inc; } } return 0; }
['static int ipvideo_decode_block_opcode_0xA(IpvideoContext *s)\n{\n int x, y;\n unsigned char P[4];\n int flags = 0;\n CHECK_STREAM_PTR(24);\n if (s->stream_ptr[0] <= s->stream_ptr[1]) {\n CHECK_STREAM_PTR(32);\n for (y = 0; y < 16; y++) {\n if (!(y & 3)) {\n memcpy(P, s->stream_ptr, 4);\n s->stream_ptr += 4;\n flags = bytestream_get_le32(&s->stream_ptr);\n }\n for (x = 0; x < 4; x++, flags >>= 2)\n *s->pixel_ptr++ = P[flags & 0x03];\n s->pixel_ptr += s->stride - 4;\n if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;\n }\n } else {\n int vert = s->stream_ptr[12] <= s->stream_ptr[13];\n uint64_t flags = 0;\n for (y = 0; y < 16; y++) {\n if (!(y & 7)) {\n memcpy(P, s->stream_ptr, 4);\n s->stream_ptr += 4;\n flags = bytestream_get_le64(&s->stream_ptr);\n }\n for (x = 0; x < 4; x++, flags >>= 2)\n *s->pixel_ptr++ = P[flags & 0x03];\n if (vert) {\n s->pixel_ptr += s->stride - 4;\n if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;\n } else if (y & 1) s->pixel_ptr += s->line_inc;\n }\n }\n return 0;\n}']
71
0
https://github.com/libav/libav/blob/ffb0af7f17eb0da86e9b140e86a1404d3c6c9e79/libavcodec/mpeg4videodec.c/#L1117
static inline int mpeg4_decode_block(MpegEncContext *s, int16_t *block, int n, int coded, int intra, int rvlc) { int level, i, last, run, qmul, qadd, dc_pred_dir; RLTable *rl; RL_VLC_ELEM *rl_vlc; const uint8_t *scan_table; if (intra) { if (s->use_intra_dc_vlc) { if (s->partitioned_frame) { level = s->dc_val[0][s->block_index[n]]; if (n < 4) level = FASTDIV((level + (s->y_dc_scale >> 1)), s->y_dc_scale); else level = FASTDIV((level + (s->c_dc_scale >> 1)), s->c_dc_scale); dc_pred_dir = (s->pred_dir_table[s->mb_x + s->mb_y * s->mb_stride] << n) & 32; } else { level = mpeg4_decode_dc(s, n, &dc_pred_dir); if (level < 0) return -1; } block[0] = level; i = 0; } else { i = -1; ff_mpeg4_pred_dc(s, n, 0, &dc_pred_dir, 0); } if (!coded) goto not_coded; if (rvlc) { rl = &ff_rvlc_rl_intra; rl_vlc = ff_rvlc_rl_intra.rl_vlc[0]; } else { rl = &ff_mpeg4_rl_intra; rl_vlc = ff_mpeg4_rl_intra.rl_vlc[0]; } if (s->ac_pred) { if (dc_pred_dir == 0) scan_table = s->intra_v_scantable.permutated; else scan_table = s->intra_h_scantable.permutated; } else { scan_table = s->intra_scantable.permutated; } qmul = 1; qadd = 0; } else { i = -1; if (!coded) { s->block_last_index[n] = i; return 0; } if (rvlc) rl = &ff_rvlc_rl_inter; else rl = &ff_h263_rl_inter; scan_table = s->intra_scantable.permutated; if (s->mpeg_quant) { qmul = 1; qadd = 0; if (rvlc) rl_vlc = ff_rvlc_rl_inter.rl_vlc[0]; else rl_vlc = ff_h263_rl_inter.rl_vlc[0]; } else { qmul = s->qscale << 1; qadd = (s->qscale - 1) | 1; if (rvlc) rl_vlc = ff_rvlc_rl_inter.rl_vlc[s->qscale]; else rl_vlc = ff_h263_rl_inter.rl_vlc[s->qscale]; } } { OPEN_READER(re, &s->gb); for (;;) { UPDATE_CACHE(re, &s->gb); GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0); if (level == 0) { if (rvlc) { if (SHOW_UBITS(re, &s->gb, 1) == 0) { av_log(s->avctx, AV_LOG_ERROR, "1. marker bit missing in rvlc esc\n"); return -1; } SKIP_CACHE(re, &s->gb, 1); last = SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1); run = SHOW_UBITS(re, &s->gb, 6); SKIP_COUNTER(re, &s->gb, 1 + 1 + 6); UPDATE_CACHE(re, &s->gb); if (SHOW_UBITS(re, &s->gb, 1) == 0) { av_log(s->avctx, AV_LOG_ERROR, "2. marker bit missing in rvlc esc\n"); return -1; } SKIP_CACHE(re, &s->gb, 1); level = SHOW_UBITS(re, &s->gb, 11); SKIP_CACHE(re, &s->gb, 11); if (SHOW_UBITS(re, &s->gb, 5) != 0x10) { av_log(s->avctx, AV_LOG_ERROR, "reverse esc missing\n"); return -1; } SKIP_CACHE(re, &s->gb, 5); level = level * qmul + qadd; level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); SKIP_COUNTER(re, &s->gb, 1 + 11 + 5 + 1); i += run + 1; if (last) i += 192; } else { int cache; cache = GET_CACHE(re, &s->gb); if (IS_3IV1) cache ^= 0xC0000000; if (cache & 0x80000000) { if (cache & 0x40000000) { SKIP_CACHE(re, &s->gb, 2); last = SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1); run = SHOW_UBITS(re, &s->gb, 6); SKIP_COUNTER(re, &s->gb, 2 + 1 + 6); UPDATE_CACHE(re, &s->gb); if (IS_3IV1) { level = SHOW_SBITS(re, &s->gb, 12); LAST_SKIP_BITS(re, &s->gb, 12); } else { if (SHOW_UBITS(re, &s->gb, 1) == 0) { av_log(s->avctx, AV_LOG_ERROR, "1. marker bit missing in 3. esc\n"); return -1; } SKIP_CACHE(re, &s->gb, 1); level = SHOW_SBITS(re, &s->gb, 12); SKIP_CACHE(re, &s->gb, 12); if (SHOW_UBITS(re, &s->gb, 1) == 0) { av_log(s->avctx, AV_LOG_ERROR, "2. marker bit missing in 3. esc\n"); return -1; } SKIP_COUNTER(re, &s->gb, 1 + 12 + 1); } if (level > 0) level = level * qmul + qadd; else level = level * qmul - qadd; if ((unsigned)(level + 2048) > 4095) { if (s->err_recognition & AV_EF_BITSTREAM) { if (level > 2560 || level < -2560) { av_log(s->avctx, AV_LOG_ERROR, "|level| overflow in 3. esc, qp=%d\n", s->qscale); return -1; } } level = level < 0 ? -2048 : 2047; } i += run + 1; if (last) i += 192; } else { SKIP_BITS(re, &s->gb, 2); GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1); i += run + rl->max_run[run >> 7][level / qmul] + 1; level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); LAST_SKIP_BITS(re, &s->gb, 1); } } else { SKIP_BITS(re, &s->gb, 1); GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1); i += run; level = level + rl->max_level[run >> 7][(run - 1) & 63] * qmul; level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); LAST_SKIP_BITS(re, &s->gb, 1); } } } else { i += run; level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); LAST_SKIP_BITS(re, &s->gb, 1); } if (i > 62) { i -= 192; if (i & (~63)) { av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y); return -1; } block[scan_table[i]] = level; break; } block[scan_table[i]] = level; } CLOSE_READER(re, &s->gb); } not_coded: if (intra) { if (!s->use_intra_dc_vlc) { block[0] = ff_mpeg4_pred_dc(s, n, block[0], &dc_pred_dir, 0); i -= i >> 31; } ff_mpeg4_pred_ac(s, block, n, dc_pred_dir); if (s->ac_pred) i = 63; } s->block_last_index[n] = i; return 0; }
['static inline int mpeg4_decode_block(MpegEncContext *s, int16_t *block,\n int n, int coded, int intra, int rvlc)\n{\n int level, i, last, run, qmul, qadd, dc_pred_dir;\n RLTable *rl;\n RL_VLC_ELEM *rl_vlc;\n const uint8_t *scan_table;\n if (intra) {\n if (s->use_intra_dc_vlc) {\n if (s->partitioned_frame) {\n level = s->dc_val[0][s->block_index[n]];\n if (n < 4)\n level = FASTDIV((level + (s->y_dc_scale >> 1)), s->y_dc_scale);\n else\n level = FASTDIV((level + (s->c_dc_scale >> 1)), s->c_dc_scale);\n dc_pred_dir = (s->pred_dir_table[s->mb_x + s->mb_y * s->mb_stride] << n) & 32;\n } else {\n level = mpeg4_decode_dc(s, n, &dc_pred_dir);\n if (level < 0)\n return -1;\n }\n block[0] = level;\n i = 0;\n } else {\n i = -1;\n ff_mpeg4_pred_dc(s, n, 0, &dc_pred_dir, 0);\n }\n if (!coded)\n goto not_coded;\n if (rvlc) {\n rl = &ff_rvlc_rl_intra;\n rl_vlc = ff_rvlc_rl_intra.rl_vlc[0];\n } else {\n rl = &ff_mpeg4_rl_intra;\n rl_vlc = ff_mpeg4_rl_intra.rl_vlc[0];\n }\n if (s->ac_pred) {\n if (dc_pred_dir == 0)\n scan_table = s->intra_v_scantable.permutated;\n else\n scan_table = s->intra_h_scantable.permutated;\n } else {\n scan_table = s->intra_scantable.permutated;\n }\n qmul = 1;\n qadd = 0;\n } else {\n i = -1;\n if (!coded) {\n s->block_last_index[n] = i;\n return 0;\n }\n if (rvlc)\n rl = &ff_rvlc_rl_inter;\n else\n rl = &ff_h263_rl_inter;\n scan_table = s->intra_scantable.permutated;\n if (s->mpeg_quant) {\n qmul = 1;\n qadd = 0;\n if (rvlc)\n rl_vlc = ff_rvlc_rl_inter.rl_vlc[0];\n else\n rl_vlc = ff_h263_rl_inter.rl_vlc[0];\n } else {\n qmul = s->qscale << 1;\n qadd = (s->qscale - 1) | 1;\n if (rvlc)\n rl_vlc = ff_rvlc_rl_inter.rl_vlc[s->qscale];\n else\n rl_vlc = ff_h263_rl_inter.rl_vlc[s->qscale];\n }\n }\n {\n OPEN_READER(re, &s->gb);\n for (;;) {\n UPDATE_CACHE(re, &s->gb);\n GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);\n if (level == 0) {\n if (rvlc) {\n if (SHOW_UBITS(re, &s->gb, 1) == 0) {\n av_log(s->avctx, AV_LOG_ERROR,\n "1. marker bit missing in rvlc esc\\n");\n return -1;\n }\n SKIP_CACHE(re, &s->gb, 1);\n last = SHOW_UBITS(re, &s->gb, 1);\n SKIP_CACHE(re, &s->gb, 1);\n run = SHOW_UBITS(re, &s->gb, 6);\n SKIP_COUNTER(re, &s->gb, 1 + 1 + 6);\n UPDATE_CACHE(re, &s->gb);\n if (SHOW_UBITS(re, &s->gb, 1) == 0) {\n av_log(s->avctx, AV_LOG_ERROR,\n "2. marker bit missing in rvlc esc\\n");\n return -1;\n }\n SKIP_CACHE(re, &s->gb, 1);\n level = SHOW_UBITS(re, &s->gb, 11);\n SKIP_CACHE(re, &s->gb, 11);\n if (SHOW_UBITS(re, &s->gb, 5) != 0x10) {\n av_log(s->avctx, AV_LOG_ERROR, "reverse esc missing\\n");\n return -1;\n }\n SKIP_CACHE(re, &s->gb, 5);\n level = level * qmul + qadd;\n level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);\n SKIP_COUNTER(re, &s->gb, 1 + 11 + 5 + 1);\n i += run + 1;\n if (last)\n i += 192;\n } else {\n int cache;\n cache = GET_CACHE(re, &s->gb);\n if (IS_3IV1)\n cache ^= 0xC0000000;\n if (cache & 0x80000000) {\n if (cache & 0x40000000) {\n SKIP_CACHE(re, &s->gb, 2);\n last = SHOW_UBITS(re, &s->gb, 1);\n SKIP_CACHE(re, &s->gb, 1);\n run = SHOW_UBITS(re, &s->gb, 6);\n SKIP_COUNTER(re, &s->gb, 2 + 1 + 6);\n UPDATE_CACHE(re, &s->gb);\n if (IS_3IV1) {\n level = SHOW_SBITS(re, &s->gb, 12);\n LAST_SKIP_BITS(re, &s->gb, 12);\n } else {\n if (SHOW_UBITS(re, &s->gb, 1) == 0) {\n av_log(s->avctx, AV_LOG_ERROR,\n "1. marker bit missing in 3. esc\\n");\n return -1;\n }\n SKIP_CACHE(re, &s->gb, 1);\n level = SHOW_SBITS(re, &s->gb, 12);\n SKIP_CACHE(re, &s->gb, 12);\n if (SHOW_UBITS(re, &s->gb, 1) == 0) {\n av_log(s->avctx, AV_LOG_ERROR,\n "2. marker bit missing in 3. esc\\n");\n return -1;\n }\n SKIP_COUNTER(re, &s->gb, 1 + 12 + 1);\n }\n if (level > 0)\n level = level * qmul + qadd;\n else\n level = level * qmul - qadd;\n if ((unsigned)(level + 2048) > 4095) {\n if (s->err_recognition & AV_EF_BITSTREAM) {\n if (level > 2560 || level < -2560) {\n av_log(s->avctx, AV_LOG_ERROR,\n "|level| overflow in 3. esc, qp=%d\\n",\n s->qscale);\n return -1;\n }\n }\n level = level < 0 ? -2048 : 2047;\n }\n i += run + 1;\n if (last)\n i += 192;\n } else {\n SKIP_BITS(re, &s->gb, 2);\n GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);\n i += run + rl->max_run[run >> 7][level / qmul] + 1;\n level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);\n LAST_SKIP_BITS(re, &s->gb, 1);\n }\n } else {\n SKIP_BITS(re, &s->gb, 1);\n GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);\n i += run;\n level = level + rl->max_level[run >> 7][(run - 1) & 63] * qmul;\n level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);\n LAST_SKIP_BITS(re, &s->gb, 1);\n }\n }\n } else {\n i += run;\n level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);\n LAST_SKIP_BITS(re, &s->gb, 1);\n }\n if (i > 62) {\n i -= 192;\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 -1;\n }\n block[scan_table[i]] = level;\n break;\n }\n block[scan_table[i]] = level;\n }\n CLOSE_READER(re, &s->gb);\n }\nnot_coded:\n if (intra) {\n if (!s->use_intra_dc_vlc) {\n block[0] = ff_mpeg4_pred_dc(s, n, block[0], &dc_pred_dir, 0);\n i -= i >> 31;\n }\n ff_mpeg4_pred_ac(s, block, n, dc_pred_dir);\n if (s->ac_pred)\n i = 63;\n }\n s->block_last_index[n] = i;\n return 0;\n}']
72
0
https://github.com/openssl/openssl/blob/305b68f1a2b6d4d0aa07a6ab47ac372f067a40bb/crypto/bn/bn_lib.c/#L364
int BN_set_word(BIGNUM *a, BN_ULONG w) { bn_check_top(a); if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL) return 0; a->neg = 0; a->d[0] = w; a->top = (w ? 1 : 0); a->flags &= ~BN_FLG_FIXED_TOP; bn_check_top(a); return 1; }
['static int dh_builtin_genparams(DH *ret, int prime_len, int generator,\n BN_GENCB *cb)\n{\n BIGNUM *t1, *t2;\n int g, ok = -1;\n BN_CTX *ctx = NULL;\n ctx = BN_CTX_new();\n if (ctx == NULL)\n goto err;\n BN_CTX_start(ctx);\n t1 = BN_CTX_get(ctx);\n t2 = BN_CTX_get(ctx);\n if (t2 == NULL)\n goto err;\n if (!ret->p && ((ret->p = BN_new()) == NULL))\n goto err;\n if (!ret->g && ((ret->g = BN_new()) == NULL))\n goto err;\n if (generator <= 1) {\n DHerr(DH_F_DH_BUILTIN_GENPARAMS, DH_R_BAD_GENERATOR);\n goto err;\n }\n if (generator == DH_GENERATOR_2) {\n if (!BN_set_word(t1, 24))\n goto err;\n if (!BN_set_word(t2, 11))\n goto err;\n g = 2;\n } else if (generator == DH_GENERATOR_5) {\n if (!BN_set_word(t1, 10))\n goto err;\n if (!BN_set_word(t2, 3))\n goto err;\n g = 5;\n } else {\n if (!BN_set_word(t1, 2))\n goto err;\n if (!BN_set_word(t2, 1))\n goto err;\n g = generator;\n }\n if (!BN_generate_prime_ex(ret->p, prime_len, 1, t1, t2, cb))\n goto err;\n if (!BN_GENCB_call(cb, 3, 0))\n goto err;\n if (!BN_set_word(ret->g, g))\n goto err;\n ok = 1;\n err:\n if (ok == -1) {\n DHerr(DH_F_DH_BUILTIN_GENPARAMS, ERR_R_BN_LIB);\n ok = 0;\n }\n if (ctx != NULL) {\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n }\n return ok;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n a->flags &= ~BN_FLG_FIXED_TOP;\n bn_check_top(a);\n return 1;\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}']
73
0
https://github.com/openssl/openssl/blob/1fac96e4d6484a517f2ebe99b72016726391723c/crypto/bn/bn_lib.c/#L470
BIGNUM *bn_expand2(BIGNUM *b, int words) { BN_ULONG *A,*a; const BN_ULONG *B; int i; bn_check_top(b); if (words > b->max) { bn_check_top(b); if (BN_get_flags(b,BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND2,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return(NULL); } a=A=(BN_ULONG *)Malloc(sizeof(BN_ULONG)*(words+1)); if (A == NULL) { BNerr(BN_F_BN_EXPAND2,ERR_R_MALLOC_FAILURE); return(NULL); } #if 1 B=b->d; if (B != NULL) { #if 0 for (i=b->top&(~7); i>0; i-=8) { A[0]=B[0]; A[1]=B[1]; A[2]=B[2]; A[3]=B[3]; A[4]=B[4]; A[5]=B[5]; A[6]=B[6]; A[7]=B[7]; A+=8; B+=8; } switch (b->top&7) { case 7: A[6]=B[6]; case 6: A[5]=B[5]; case 5: A[4]=B[4]; case 4: A[3]=B[3]; case 3: A[2]=B[2]; case 2: A[1]=B[1]; case 1: A[0]=B[0]; case 0: ; } #else for (i=b->top>>2; i>0; i--,A+=4,B+=4) { BN_ULONG a0,a1,a2,a3; a0=B[0]; a1=B[1]; a2=B[2]; a3=B[3]; A[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3; } switch (b->top&3) { case 3: A[2]=B[2]; case 2: A[1]=B[1]; case 1: A[0]=B[0]; case 0: ; } #endif Free(b->d); } b->d=a; b->max=words; A= &(b->d[b->top]); for (i=(b->max - b->top)>>3; i>0; i--,A+=8) { A[0]=0; A[1]=0; A[2]=0; A[3]=0; A[4]=0; A[5]=0; A[6]=0; A[7]=0; } for (i=(b->max - b->top)&7; i>0; i--,A++) A[0]=0; #else memset(A,0,sizeof(BN_ULONG)*(words+1)); memcpy(A,b->d,sizeof(b->d[0])*b->top); b->d=a; b->max=words; #endif } return(b); }
['int 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#else\n\t\tw=0;\n#endif\n\t\ta->d[i]=(BN_ULONG)w&BN_MASK2;\n\t\tif (a->d[i] != 0) a->top=i+1;\n\t\t}\n\treturn(1);\n\t}', 'int BN_set_bit(BIGNUM *a, int n)\n\t{\n\tint i,j,k;\n\ti=n/BN_BITS2;\n\tj=n%BN_BITS2;\n\tif (a->top <= i)\n\t\t{\n\t\tif (bn_wexpand(a,i+1) == NULL) return(0);\n\t\tfor(k=a->top; k<i+1; k++)\n\t\t\ta->d[k]=0;\n\t\ta->top=i+1;\n\t\t}\n\ta->d[i]|=(((BN_ULONG)1)<<j);\n\treturn(1);\n\t}', 'BIGNUM *BN_mod_inverse(BIGNUM *in, BIGNUM *a, BIGNUM *n, BN_CTX *ctx)\n\t{\n\tBIGNUM *A,*B,*X,*Y,*M,*D,*R;\n\tBIGNUM *T,*ret=NULL;\n\tint sign;\n\tbn_check_top(a);\n\tbn_check_top(n);\n\tA= &(ctx->bn[ctx->tos]);\n\tB= &(ctx->bn[ctx->tos+1]);\n\tX= &(ctx->bn[ctx->tos+2]);\n\tD= &(ctx->bn[ctx->tos+3]);\n\tM= &(ctx->bn[ctx->tos+4]);\n\tY= &(ctx->bn[ctx->tos+5]);\n\tctx->tos+=6;\n\tif (in == NULL)\n\t\tR=BN_new();\n\telse\n\t\tR=in;\n\tif (R == NULL) goto err;\n\tBN_zero(X);\n\tBN_one(Y);\n\tif (BN_copy(A,a) == NULL) goto err;\n\tif (BN_copy(B,n) == NULL) goto err;\n\tsign=1;\n\twhile (!BN_is_zero(B))\n\t\t{\n\t\tif (!BN_div(D,M,A,B,ctx)) goto err;\n\t\tT=A;\n\t\tA=B;\n\t\tB=M;\n\t\tif (!BN_mul(T,D,X,ctx)) goto err;\n\t\tif (!BN_add(T,T,Y)) goto err;\n\t\tM=Y;\n\t\tY=X;\n\t\tX=T;\n\t\tsign= -sign;\n\t\t}\n\tif (sign < 0)\n\t\t{\n\t\tif (!BN_sub(Y,n,Y)) goto err;\n\t\t}\n\tif (BN_is_one(A))\n\t\t{ if (!BN_mod(R,Y,n,ctx)) goto err; }\n\telse\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_INVERSE,BN_R_NO_INVERSE);\n\t\tgoto err;\n\t\t}\n\tret=R;\nerr:\n\tif ((ret == NULL) && (in == NULL)) BN_free(R);\n\tctx->tos-=6;\n\treturn(ret);\n\t}', 'int BN_mod(BIGNUM *rem, BIGNUM *m, BIGNUM *d, BN_CTX *ctx)\n\t{\n#if 0\n\tint i,nm,nd;\n\tBIGNUM *dv;\n\tif (BN_ucmp(m,d) < 0)\n\t\treturn((BN_copy(rem,m) == NULL)?0:1);\n\tdv= &(ctx->bn[ctx->tos]);\n\tif (!BN_copy(rem,m)) return(0);\n\tnm=BN_num_bits(rem);\n\tnd=BN_num_bits(d);\n\tif (!BN_lshift(dv,d,nm-nd)) return(0);\n\tfor (i=nm-nd; i>=0; i--)\n\t\t{\n\t\tif (BN_cmp(rem,dv) >= 0)\n\t\t\t{\n\t\t\tif (!BN_sub(rem,rem,dv)) return(0);\n\t\t\t}\n\t\tif (!BN_rshift1(dv,dv)) return(0);\n\t\t}\n\treturn(1);\n#else\n\treturn(BN_div(NULL,rem,m,d,ctx));\n#endif\n\t}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, BIGNUM *num, BIGNUM *divisor,\n\t BN_CTX *ctx)\n\t{\n\tint norm_shift,i,j,loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tint num_n,div_n;\n\tbn_check_top(num);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\n\t\t}\n\tif (BN_ucmp(num,divisor) < 0)\n\t\t{\n\t\tif (rm != NULL)\n\t\t\t{ if (BN_copy(rm,num) == NULL) return(0); }\n\t\tif (dv != NULL) BN_zero(dv);\n\t\treturn(1);\n\t\t}\n\ttmp= &(ctx->bn[ctx->tos]);\n\ttmp->neg=0;\n\tsnum= &(ctx->bn[ctx->tos+1]);\n\tsdiv= &(ctx->bn[ctx->tos+2]);\n\tif (dv == NULL)\n\t\tres= &(ctx->bn[ctx->tos+3]);\n\telse\tres=dv;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tBN_lshift(sdiv,divisor,norm_shift);\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tBN_lshift(snum,num,norm_shift);\n\tsnum->neg=0;\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\tBN_init(&wnum);\n\twnum.d=\t &(snum->d[loop]);\n\twnum.top= div_n;\n\twnum.max= snum->max+1;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (BN_ucmp(&wnum,sdiv) >= 0)\n\t\t{\n\t\tif (!BN_usub(&wnum,&wnum,sdiv)) goto err;\n\t\t*resp=1;\n\t\tres->d[res->top-1]=1;\n\t\t}\n\telse\n\t\tres->top--;\n\tresp--;\n\tfor (i=0; i<loop-1; i++)\n\t\t{\n\t\tBN_ULONG q,n0,n1;\n\t\tBN_ULONG l0;\n\t\twnum.d--; wnum.top++;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\tq=bn_div_words(n0,n1,d0);\n\t\t{\n#ifdef BN_LLONG\n\t\tBN_ULLONG t1,t2,rem;\n\t\tt1=((BN_ULLONG)n0<<BN_BITS2)|n1;\n\t\tfor (;;)\n\t\t\t{\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\trem=t1-(BN_ULLONG)q*d0;\n\t\t\tif ((rem>>BN_BITS2) ||\n\t\t\t\t(t2 <= ((BN_ULLONG)(rem<<BN_BITS2)+wnump[-2])))\n\t\t\t\tbreak;\n\t\t\tq--;\n\t\t\t}\n#else\n\t\tBN_ULONG t1l,t1h,t2l,t2h,t3l,t3h,ql,qh,t3t;\n\t\tt1h=n0;\n\t\tt1l=n1;\n\t\tfor (;;)\n\t\t\t{\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n\t\t\tt3t=LBITS(d0); t3h=HBITS(d0);\n\t\t\tmul64(t3t,t3h,ql,qh);\n\t\t\tt3l=(t1l-t3t)&BN_MASK2;\n\t\t\tif (t3l > t1l) t3h++;\n\t\t\tt3h=(t1h-t3h)&BN_MASK2;\n\t\t\tif (t3h) break;\n\t\t\tif (t2h < t3l) break;\n\t\t\tif ((t2h == t3l) && (t2l <= wnump[-2])) break;\n\t\t\tq--;\n\t\t\t}\n#endif\n\t\t}\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\tfor (j=div_n+1; j>0; j--)\n\t\t\tif (tmp->d[j-1]) break;\n\t\ttmp->top=j;\n\t\tj=wnum.top;\n\t\tBN_sub(&wnum,&wnum,tmp);\n\t\tsnum->top=snum->top+wnum.top-j;\n\t\tif (wnum.neg)\n\t\t\t{\n\t\t\tq--;\n\t\t\tj=wnum.top;\n\t\t\tBN_add(&wnum,&wnum,sdiv);\n\t\t\tsnum->top+=wnum.top-j;\n\t\t\t}\n\t\t*(resp--)=q;\n\t\twnump--;\n\t\t}\n\tif (rm != NULL)\n\t\t{\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\trm->neg=num->neg;\n\t\t}\n\treturn(1);\nerr:\n\treturn(0);\n\t}', 'BIGNUM *BN_copy(BIGNUM *a, BIGNUM *b)\n\t{\n\tint i;\n\tBN_ULONG *A;\n\tconst BN_ULONG *B;\n\tbn_check_top(b);\n\tif (a == b) return(a);\n\tif (bn_wexpand(a,b->top) == NULL) return(NULL);\n#if 1\n\tA=a->d;\n\tB=b->d;\n\tfor (i=b->top>>2; i>0; i--,A+=4,B+=4)\n\t\t{\n\t\tBN_ULONG a0,a1,a2,a3;\n\t\ta0=B[0]; a1=B[1]; a2=B[2]; a3=B[3];\n\t\tA[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3;\n\t\t}\n\tswitch (b->top&3)\n\t\t{\n\t\tcase 3: A[2]=B[2];\n\t\tcase 2: A[1]=B[1];\n\t\tcase 1: A[0]=B[0];\n\t\tcase 0: ;\n\t\t}\n#else\n\tmemcpy(a->d,b->d,sizeof(b->d[0])*b->top);\n#endif\n\ta->top=b->top;\n\tif ((a->top == 0) && (a->d != NULL))\n\t\ta->d[0]=0;\n\ta->neg=b->neg;\n\treturn(a);\n\t}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n\t{\n\tBN_ULONG *A,*a;\n\tconst BN_ULONG *B;\n\tint i;\n\tbn_check_top(b);\n\tif (words > b->max)\n\t\t{\n\t\tbn_check_top(b);\n\t\tif (BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_EXPAND2,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n\t\t\treturn(NULL);\n\t\t\t}\n\t\ta=A=(BN_ULONG *)Malloc(sizeof(BN_ULONG)*(words+1));\n\t\tif (A == NULL)\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_EXPAND2,ERR_R_MALLOC_FAILURE);\n\t\t\treturn(NULL);\n\t\t\t}\n#if 1\n\t\tB=b->d;\n\t\tif (B != NULL)\n\t\t\t{\n#if 0\n\t\t\tfor (i=b->top&(~7); i>0; i-=8)\n\t\t\t\t{\n\t\t\t\tA[0]=B[0]; A[1]=B[1]; A[2]=B[2]; A[3]=B[3];\n\t\t\t\tA[4]=B[4]; A[5]=B[5]; A[6]=B[6]; A[7]=B[7];\n\t\t\t\tA+=8;\n\t\t\t\tB+=8;\n\t\t\t\t}\n\t\t\tswitch (b->top&7)\n\t\t\t\t{\n\t\t\tcase 7:\n\t\t\t\tA[6]=B[6];\n\t\t\tcase 6:\n\t\t\t\tA[5]=B[5];\n\t\t\tcase 5:\n\t\t\t\tA[4]=B[4];\n\t\t\tcase 4:\n\t\t\t\tA[3]=B[3];\n\t\t\tcase 3:\n\t\t\t\tA[2]=B[2];\n\t\t\tcase 2:\n\t\t\t\tA[1]=B[1];\n\t\t\tcase 1:\n\t\t\t\tA[0]=B[0];\n\t\t\tcase 0:\n\t\t\t\t;\n\t\t\t\t}\n#else\n\t\t\tfor (i=b->top>>2; i>0; i--,A+=4,B+=4)\n\t\t\t\t{\n\t\t\t\tBN_ULONG a0,a1,a2,a3;\n\t\t\t\ta0=B[0]; a1=B[1]; a2=B[2]; a3=B[3];\n\t\t\t\tA[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3;\n\t\t\t\t}\n\t\t\tswitch (b->top&3)\n\t\t\t\t{\n\t\t\t\tcase 3:\tA[2]=B[2];\n\t\t\t\tcase 2:\tA[1]=B[1];\n\t\t\t\tcase 1:\tA[0]=B[0];\n\t\t\t\tcase 0:\t;\n\t\t\t\t}\n#endif\n\t\t\tFree(b->d);\n\t\t\t}\n\t\tb->d=a;\n\t\tb->max=words;\n\t\tA= &(b->d[b->top]);\n\t\tfor (i=(b->max - b->top)>>3; i>0; i--,A+=8)\n\t\t\t{\n\t\t\tA[0]=0; A[1]=0; A[2]=0; A[3]=0;\n\t\t\tA[4]=0; A[5]=0; A[6]=0; A[7]=0;\n\t\t\t}\n\t\tfor (i=(b->max - b->top)&7; i>0; i--,A++)\n\t\t\tA[0]=0;\n#else\n\t\t\tmemset(A,0,sizeof(BN_ULONG)*(words+1));\n\t\t\tmemcpy(A,b->d,sizeof(b->d[0])*b->top);\n\t\t\tb->d=a;\n\t\t\tb->max=words;\n#endif\n\t\t}\n\treturn(b);\n\t}']
74
0
https://github.com/libav/libav/blob/3b2fbe67bd63b00331db2a9b213f6d420418a312/libavcodec/opus_silk.c/#L968
static void silk_lsf2lpc(const int16_t nlsf[16], float lpcf[16], int order) { int i, k; int32_t lsp[16]; int32_t p[9], q[9]; int32_t lpc32[16]; int16_t lpc[16]; for (k = 0; k < order; k++) { int index = nlsf[k] >> 8; int offset = nlsf[k] & 255; int k2 = (order == 10) ? silk_lsf_ordering_nbmb[k] : silk_lsf_ordering_wb[k]; lsp[k2] = silk_cosine[index] * 256; lsp[k2] += (silk_cosine[index + 1] - silk_cosine[index]) * offset; lsp[k2] = (lsp[k2] + 4) >> 3; } silk_lsp2poly(lsp , p, order >> 1); silk_lsp2poly(lsp + 1, q, order >> 1); for (k = 0; k < order>>1; k++) { lpc32[k] = -p[k + 1] - p[k] - q[k + 1] + q[k]; lpc32[order-k-1] = -p[k + 1] - p[k] + q[k + 1] - q[k]; } for (i = 0; i < 10; i++) { int j; unsigned int maxabs = 0; for (j = 0, k = 0; j < order; j++) { unsigned int x = FFABS(lpc32[k]); if (x > maxabs) { maxabs = x; k = j; } } maxabs = (maxabs + 16) >> 5; if (maxabs > 32767) { unsigned int chirp, chirp_base; maxabs = FFMIN(maxabs, 163838); chirp_base = chirp = 65470 - ((maxabs - 32767) << 14) / ((maxabs * (k+1)) >> 2); for (k = 0; k < order; k++) { lpc32[k] = ROUND_MULL(lpc32[k], chirp, 16); chirp = (chirp_base * chirp + 32768) >> 16; } } else break; } if (i == 10) { for (k = 0; k < order; k++) { int x = (lpc32[k] + 16) >> 5; lpc[k] = av_clip_int16(x); lpc32[k] = lpc[k] << 5; } } else { for (k = 0; k < order; k++) lpc[k] = (lpc32[k] + 16) >> 5; } for (i = 1; i <= 16 && !silk_is_lpc_stable(lpc, order); i++) { unsigned int chirp, chirp_base; chirp_base = chirp = 65536 - (1 << i); for (k = 0; k < order; k++) { lpc32[k] = ROUND_MULL(lpc32[k], chirp, 16); lpc[k] = (lpc32[k] + 16) >> 5; chirp = (chirp_base * chirp + 32768) >> 16; } } for (i = 0; i < order; i++) lpcf[i] = lpc[i] / 4096.0f; }
['static void silk_lsf2lpc(const int16_t nlsf[16], float lpcf[16], int order)\n{\n int i, k;\n int32_t lsp[16];\n int32_t p[9], q[9];\n int32_t lpc32[16];\n int16_t lpc[16];\n for (k = 0; k < order; k++) {\n int index = nlsf[k] >> 8;\n int offset = nlsf[k] & 255;\n int k2 = (order == 10) ? silk_lsf_ordering_nbmb[k] : silk_lsf_ordering_wb[k];\n lsp[k2] = silk_cosine[index] * 256;\n lsp[k2] += (silk_cosine[index + 1] - silk_cosine[index]) * offset;\n lsp[k2] = (lsp[k2] + 4) >> 3;\n }\n silk_lsp2poly(lsp , p, order >> 1);\n silk_lsp2poly(lsp + 1, q, order >> 1);\n for (k = 0; k < order>>1; k++) {\n lpc32[k] = -p[k + 1] - p[k] - q[k + 1] + q[k];\n lpc32[order-k-1] = -p[k + 1] - p[k] + q[k + 1] - q[k];\n }\n for (i = 0; i < 10; i++) {\n int j;\n unsigned int maxabs = 0;\n for (j = 0, k = 0; j < order; j++) {\n unsigned int x = FFABS(lpc32[k]);\n if (x > maxabs) {\n maxabs = x;\n k = j;\n }\n }\n maxabs = (maxabs + 16) >> 5;\n if (maxabs > 32767) {\n unsigned int chirp, chirp_base;\n maxabs = FFMIN(maxabs, 163838);\n chirp_base = chirp = 65470 - ((maxabs - 32767) << 14) / ((maxabs * (k+1)) >> 2);\n for (k = 0; k < order; k++) {\n lpc32[k] = ROUND_MULL(lpc32[k], chirp, 16);\n chirp = (chirp_base * chirp + 32768) >> 16;\n }\n } else break;\n }\n if (i == 10) {\n for (k = 0; k < order; k++) {\n int x = (lpc32[k] + 16) >> 5;\n lpc[k] = av_clip_int16(x);\n lpc32[k] = lpc[k] << 5;\n }\n } else {\n for (k = 0; k < order; k++)\n lpc[k] = (lpc32[k] + 16) >> 5;\n }\n for (i = 1; i <= 16 && !silk_is_lpc_stable(lpc, order); i++) {\n unsigned int chirp, chirp_base;\n chirp_base = chirp = 65536 - (1 << i);\n for (k = 0; k < order; k++) {\n lpc32[k] = ROUND_MULL(lpc32[k], chirp, 16);\n lpc[k] = (lpc32[k] + 16) >> 5;\n chirp = (chirp_base * chirp + 32768) >> 16;\n }\n }\n for (i = 0; i < order; i++)\n lpcf[i] = lpc[i] / 4096.0f;\n}']
75
0
https://github.com/openssl/openssl/blob/4ace4ccda2934d2628c3d63d41e79abe041621a7/crypto/rand/rand_lib.c/#L846
int RAND_pseudo_bytes(unsigned char *buf, int num) { const RAND_METHOD *meth = RAND_get_rand_method(); if (meth->pseudorand != NULL) return meth->pseudorand(buf, num); return -1; }
['int RAND_pseudo_bytes(unsigned char *buf, int num)\n{\n const RAND_METHOD *meth = RAND_get_rand_method();\n if (meth->pseudorand != NULL)\n return meth->pseudorand(buf, num);\n return -1;\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}']
76
0
https://github.com/openssl/openssl/blob/d40a1b865fddc3d67f8c06ff1f1466fad331c8f7/crypto/bn/bn_lib.c/#L250
int BN_num_bits(const BIGNUM *a) { int i = a->top - 1; bn_check_top(a); if (BN_is_zero(a)) return 0; return ((i*BN_BITS2) + BN_num_bits_word(a->d[i])); }
['static int cswift_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)\n\t{\n\tint to_return = 0;\n\tconst RSA_METHOD * def_rsa_method;\n\tif(!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp)\n\t\t{\n\t\tCSWIFTerr(CSWIFT_F_CSWIFT_RSA_MOD_EXP,CSWIFT_R_MISSING_KEY_COMPONENTS);\n\t\tgoto err;\n\t\t}\n\tif(BN_num_bytes(rsa->p) > 128 ||\n\t\tBN_num_bytes(rsa->q) > 128 ||\n\t\tBN_num_bytes(rsa->dmp1) > 128 ||\n\t\tBN_num_bytes(rsa->dmq1) > 128 ||\n\t\tBN_num_bytes(rsa->iqmp) > 128)\n\t{\n#ifdef RSA_NULL\n\t\tdef_rsa_method=RSA_null_method();\n#else\n#if 0\n\t\tdef_rsa_method=RSA_PKCS1_RSAref();\n#else\n\t\tdef_rsa_method=RSA_PKCS1_SSLeay();\n#endif\n#endif\n\t\tif(def_rsa_method)\n\t\t\treturn def_rsa_method->rsa_mod_exp(r0, I, rsa, ctx);\n\t}\n\tto_return = cswift_mod_exp_crt(r0, I, rsa->p, rsa->q, rsa->dmp1,\n\t\trsa->dmq1, rsa->iqmp, ctx);\nerr:\n\treturn to_return;\n\t}', 'int BN_num_bits(const BIGNUM *a)\n\t{\n\tint i = a->top - 1;\n\tbn_check_top(a);\n\tif (BN_is_zero(a)) return 0;\n\treturn ((i*BN_BITS2) + BN_num_bits_word(a->d[i]));\n\t}', 'static int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,\n\t\t\tconst BIGNUM *q, const BIGNUM *dmp1,\n\t\t\tconst BIGNUM *dmq1, const BIGNUM *iqmp, BN_CTX *ctx)\n\t{\n\tSW_STATUS sw_status;\n\tSW_LARGENUMBER arg, res;\n\tSW_PARAM sw_param;\n\tSW_CONTEXT_HANDLE hac;\n\tBIGNUM *result = NULL;\n\tBIGNUM *argument = NULL;\n\tint to_return = 0;\n\tint acquired = 0;\n\tsw_param.up.crt.p.value = NULL;\n\tsw_param.up.crt.q.value = NULL;\n\tsw_param.up.crt.dmp1.value = NULL;\n\tsw_param.up.crt.dmq1.value = NULL;\n\tsw_param.up.crt.iqmp.value = NULL;\n\tif(!get_context(&hac))\n\t\t{\n\t\tCSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_UNIT_FAILURE);\n\t\tgoto err;\n\t\t}\n\tacquired = 1;\n\targument = BN_new();\n\tresult = BN_new();\n\tif(!result || !argument)\n\t\t{\n\t\tCSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_CTX_FULL);\n\t\tgoto err;\n\t\t}\n\tsw_param.type = SW_ALG_CRT;\n\tif(!cswift_bn_32copy(&sw_param.up.crt.p, p))\n\t{\n\t\tCSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL);\n\t\tgoto err;\n\t}\n\tif(!cswift_bn_32copy(&sw_param.up.crt.q, q))\n\t{\n\t\tCSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL);\n\t\tgoto err;\n\t}\n\tif(!cswift_bn_32copy(&sw_param.up.crt.dmp1, dmp1))\n\t{\n\t\tCSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL);\n\t\tgoto err;\n\t}\n\tif(!cswift_bn_32copy(&sw_param.up.crt.dmq1, dmq1))\n\t{\n\t\tCSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL);\n\t\tgoto err;\n\t}\n\tif(!cswift_bn_32copy(&sw_param.up.crt.iqmp, iqmp))\n\t{\n\t\tCSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL);\n\t\tgoto err;\n\t}\n\tif(\t!bn_wexpand(argument, a->top) ||\n\t\t\t!bn_wexpand(result, p->top + q->top))\n\t\t{\n\t\tCSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL);\n\t\tgoto err;\n\t\t}\n\tsw_status = p_CSwift_AttachKeyParam(hac, &sw_param);\n\tswitch(sw_status)\n\t\t{\n\tcase SW_OK:\n\t\tbreak;\n\tcase SW_ERR_INPUT_SIZE:\n\t\tCSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BAD_KEY_SIZE);\n\t\tgoto err;\n\tdefault:\n\t\t{\n\t\tchar tmpbuf[DECIMAL_SIZE(sw_status)+1];\n\t\tCSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_REQUEST_FAILED);\n\t\tsprintf(tmpbuf, "%ld", sw_status);\n\t\tERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);\n\t\t}\n\t\tgoto err;\n\t\t}\n\targ.nbytes = BN_bn2bin(a, (unsigned char *)argument->d);\n\targ.value = (unsigned char *)argument->d;\n\tres.nbytes = 2 * BN_num_bytes(p);\n\tmemset(result->d, 0, res.nbytes);\n\tres.value = (unsigned char *)result->d;\n\tif((sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_MODEXP_CRT, &arg, 1,\n\t\t&res, 1)) != SW_OK)\n\t\t{\n\t\tchar tmpbuf[DECIMAL_SIZE(sw_status)+1];\n\t\tCSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_REQUEST_FAILED);\n\t\tsprintf(tmpbuf, "%ld", sw_status);\n\t\tERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);\n\t\tgoto err;\n\t\t}\n\tBN_bin2bn((unsigned char *)result->d, res.nbytes, r);\n\tto_return = 1;\nerr:\n\tif(sw_param.up.crt.p.value)\n\t\tOPENSSL_free(sw_param.up.crt.p.value);\n\tif(sw_param.up.crt.q.value)\n\t\tOPENSSL_free(sw_param.up.crt.q.value);\n\tif(sw_param.up.crt.dmp1.value)\n\t\tOPENSSL_free(sw_param.up.crt.dmp1.value);\n\tif(sw_param.up.crt.dmq1.value)\n\t\tOPENSSL_free(sw_param.up.crt.dmq1.value);\n\tif(sw_param.up.crt.iqmp.value)\n\t\tOPENSSL_free(sw_param.up.crt.iqmp.value);\n\tif(result)\n\t\tBN_free(result);\n\tif(argument)\n\t\tBN_free(argument);\n\tif(acquired)\n\t\trelease_context(hac);\n\treturn to_return;\n\t}', 'int cswift_bn_32copy(SW_LARGENUMBER * out, const BIGNUM * in)\n{\n\tint mod;\n\tint numbytes = BN_num_bytes(in);\n\tmod = 0;\n\twhile( ((out->nbytes = (numbytes+mod)) % 32) )\n\t{\n\t\tmod++;\n\t}\n\tout->value = (unsigned char*)OPENSSL_malloc(out->nbytes);\n\tif(!out->value)\n\t{\n\t\treturn 0;\n\t}\n\tBN_bn2bin(in, &out->value[mod]);\n\tif(mod)\n\t\tmemset(out->value, 0, mod);\n\treturn 1;\n}']
77
0
https://github.com/openssl/openssl/blob/f006217bb628d05a2d5b866ff252bd94e3477e1f/test/evp_test.c/#L1565
static int encode_test_init(struct evp_test *t, const char *encoding) { struct encode_data *edata = OPENSSL_zalloc(sizeof(*edata)); if (strcmp(encoding, "canonical") == 0) { edata->encoding = BASE64_CANONICAL_ENCODING; } else if (strcmp(encoding, "valid") == 0) { edata->encoding = BASE64_VALID_ENCODING; } else if (strcmp(encoding, "invalid") == 0) { edata->encoding = BASE64_INVALID_ENCODING; t->expected_err = OPENSSL_strdup("DECODE_ERROR"); if (t->expected_err == NULL) return 0; } else { fprintf(stderr, "Bad encoding: %s. Should be one of " "{canonical, valid, invalid}\n", encoding); return 0; } t->data = edata; return 1; }
['static int encode_test_init(struct evp_test *t, const char *encoding)\n{\n struct encode_data *edata = OPENSSL_zalloc(sizeof(*edata));\n if (strcmp(encoding, "canonical") == 0) {\n edata->encoding = BASE64_CANONICAL_ENCODING;\n } else if (strcmp(encoding, "valid") == 0) {\n edata->encoding = BASE64_VALID_ENCODING;\n } else if (strcmp(encoding, "invalid") == 0) {\n edata->encoding = BASE64_INVALID_ENCODING;\n t->expected_err = OPENSSL_strdup("DECODE_ERROR");\n if (t->expected_err == NULL)\n return 0;\n } else {\n fprintf(stderr, "Bad encoding: %s. Should be one of "\n "{canonical, valid, invalid}\\n", encoding);\n return 0;\n }\n t->data = edata;\n return 1;\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}']
78
0
https://github.com/openssl/openssl/blob/e3713c365c2657236439fea00822a43aa396d112/test/evp_extra_test.c/#L372
static int test_EVP_DigestSignInit(void) { int ret = 0; EVP_PKEY *pkey = NULL; unsigned char *sig = NULL; size_t sig_len = 0; EVP_MD_CTX *md_ctx, *md_ctx_verify = NULL; if (!TEST_ptr(md_ctx = EVP_MD_CTX_new()) || !TEST_ptr(md_ctx_verify = EVP_MD_CTX_new()) || !TEST_ptr(pkey = load_example_rsa_key())) goto out; if (!TEST_true(EVP_DigestSignInit(md_ctx, NULL, EVP_sha256(), NULL, pkey)) || !TEST_true(EVP_DigestSignUpdate(md_ctx, kMsg, sizeof(kMsg)))) goto out; if (!TEST_true(EVP_DigestSignFinal(md_ctx, NULL, &sig_len)) || !TEST_size_t_eq(sig_len, (size_t)EVP_PKEY_size(pkey))) goto out; if (!TEST_ptr(sig = OPENSSL_malloc(sig_len)) || !TEST_true(EVP_DigestSignFinal(md_ctx, sig, &sig_len))) goto out; if (!TEST_true(EVP_DigestVerifyInit(md_ctx_verify, NULL, EVP_sha256(), NULL, pkey)) || !TEST_true(EVP_DigestVerifyUpdate(md_ctx_verify, kMsg, sizeof(kMsg))) || !TEST_true(EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len))) goto out; ret = 1; out: EVP_MD_CTX_free(md_ctx); EVP_MD_CTX_free(md_ctx_verify); EVP_PKEY_free(pkey); OPENSSL_free(sig); return ret; }
['static int test_EVP_DigestSignInit(void)\n{\n int ret = 0;\n EVP_PKEY *pkey = NULL;\n unsigned char *sig = NULL;\n size_t sig_len = 0;\n EVP_MD_CTX *md_ctx, *md_ctx_verify = NULL;\n if (!TEST_ptr(md_ctx = EVP_MD_CTX_new())\n || !TEST_ptr(md_ctx_verify = EVP_MD_CTX_new())\n || !TEST_ptr(pkey = load_example_rsa_key()))\n goto out;\n if (!TEST_true(EVP_DigestSignInit(md_ctx, NULL, EVP_sha256(), NULL, pkey))\n || !TEST_true(EVP_DigestSignUpdate(md_ctx, kMsg, sizeof(kMsg))))\n goto out;\n if (!TEST_true(EVP_DigestSignFinal(md_ctx, NULL, &sig_len))\n || !TEST_size_t_eq(sig_len, (size_t)EVP_PKEY_size(pkey)))\n goto out;\n if (!TEST_ptr(sig = OPENSSL_malloc(sig_len))\n || !TEST_true(EVP_DigestSignFinal(md_ctx, sig, &sig_len)))\n goto out;\n if (!TEST_true(EVP_DigestVerifyInit(md_ctx_verify, NULL, EVP_sha256(),\n NULL, pkey))\n || !TEST_true(EVP_DigestVerifyUpdate(md_ctx_verify,\n kMsg, sizeof(kMsg)))\n || !TEST_true(EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len)))\n goto out;\n ret = 1;\n out:\n EVP_MD_CTX_free(md_ctx);\n EVP_MD_CTX_free(md_ctx_verify);\n EVP_PKEY_free(pkey);\n OPENSSL_free(sig);\n return ret;\n}', 'EVP_MD_CTX *EVP_MD_CTX_new(void)\n{\n return OPENSSL_zalloc(sizeof(EVP_MD_CTX));\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n INCREMENT(malloc_count);\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)(file); (void)(line);\n ret = malloc(num);\n#endif\n return ret;\n}', 'int test_ptr(const char *file, int line, const char *s, const void *p)\n{\n if (p != NULL)\n return 1;\n test_fail_message(NULL, file, line, "ptr", s, "NULL", "!=", "%p", p);\n return 0;\n}', 'void EVP_MD_CTX_free(EVP_MD_CTX *ctx)\n{\n EVP_MD_CTX_reset(ctx);\n OPENSSL_free(ctx);\n}', 'void CRYPTO_free(void *str, const char *file, int line)\n{\n INCREMENT(free_count);\n if (free_impl != NULL && free_impl != &CRYPTO_free) {\n free_impl(str, file, line);\n return;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_free(str, 0, file, line);\n free(str);\n CRYPTO_mem_debug_free(str, 1, file, line);\n } else {\n free(str);\n }\n#else\n free(str);\n#endif\n}']
79
0
https://github.com/openssl/openssl/blob/ed371b8cbac0d0349667558c061c1ae380cf75eb/crypto/bn/bn_lib.c/#L232
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *a = NULL; if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return NULL; } if (BN_get_flags(b, BN_FLG_SECURE)) a = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = OPENSSL_zalloc(words * sizeof(*a)); if (a == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return NULL; } assert(b->top <= words); if (b->top > 0) memcpy(a, b->d, sizeof(*a) * b->top); return a; }
['int BN_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m)\n{\n if (r != a) {\n if (BN_copy(r, a) == NULL)\n return 0;\n }\n while (n > 0) {\n int max_shift;\n max_shift = BN_num_bits(m) - BN_num_bits(r);\n if (max_shift < 0) {\n BNerr(BN_F_BN_MOD_LSHIFT_QUICK, BN_R_INPUT_NOT_REDUCED);\n return 0;\n }\n if (max_shift > n)\n max_shift = n;\n if (max_shift) {\n if (!BN_lshift(r, r, max_shift))\n return 0;\n n -= max_shift;\n } else {\n if (!BN_lshift1(r, r))\n return 0;\n --n;\n }\n if (BN_cmp(r, m) >= 0) {\n if (!BN_sub(r, r, m))\n return 0;\n }\n }\n bn_check_top(r);\n return 1;\n}', 'int BN_num_bits(const BIGNUM *a)\n{\n int i = a->top - 1;\n bn_check_top(a);\n if (BN_is_zero(a))\n return 0;\n return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));\n}', 'int BN_is_zero(const BIGNUM *a)\n{\n return a->top == 0;\n}', 'int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int ret;\n if (n < 0) {\n BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n ret = bn_lshift_fixed_top(r, a, n);\n bn_correct_top(r);\n bn_check_top(r);\n return ret;\n}', 'int BN_cmp(const BIGNUM *a, const BIGNUM *b)\n{\n int i;\n int gt, lt;\n BN_ULONG t1, t2;\n if ((a == NULL) || (b == NULL)) {\n if (a != NULL)\n return -1;\n else if (b != NULL)\n return 1;\n else\n return 0;\n }\n bn_check_top(a);\n bn_check_top(b);\n if (a->neg != b->neg) {\n if (a->neg)\n return -1;\n else\n return 1;\n }\n if (a->neg == 0) {\n gt = 1;\n lt = -1;\n } else {\n gt = -1;\n lt = 1;\n }\n if (a->top > b->top)\n return gt;\n if (a->top < b->top)\n return lt;\n for (i = a->top - 1; i >= 0; i--) {\n t1 = a->d[i];\n t2 = b->d[i];\n if (t1 > t2)\n return gt;\n if (t1 < t2)\n return lt;\n }\n return 0;\n}', 'int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int ret, r_neg, cmp_res;\n bn_check_top(a);\n bn_check_top(b);\n if (a->neg != b->neg) {\n r_neg = a->neg;\n ret = BN_uadd(r, a, b);\n } else {\n cmp_res = BN_ucmp(a, b);\n if (cmp_res > 0) {\n r_neg = a->neg;\n ret = BN_usub(r, a, b);\n } else if (cmp_res < 0) {\n r_neg = !b->neg;\n ret = BN_usub(r, b, a);\n } else {\n r_neg = 0;\n BN_zero(r);\n ret = 1;\n }\n }\n r->neg = r_neg;\n bn_check_top(r);\n return ret;\n}', 'int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int max, min, dif;\n BN_ULONG t1, t2, borrow, *rp;\n const BN_ULONG *ap, *bp;\n bn_check_top(a);\n bn_check_top(b);\n max = a->top;\n min = b->top;\n dif = max - min;\n if (dif < 0) {\n BNerr(BN_F_BN_USUB, BN_R_ARG2_LT_ARG3);\n return 0;\n }\n if (bn_wexpand(r, max) == NULL)\n return 0;\n ap = a->d;\n bp = b->d;\n rp = r->d;\n borrow = bn_sub_words(rp, ap, bp, min);\n ap += min;\n rp += min;\n while (dif) {\n dif--;\n t1 = *(ap++);\n t2 = (t1 - borrow) & BN_MASK2;\n *(rp++) = t2;\n borrow &= (t1 == 0);\n }\n while (max && *--rp == 0)\n max--;\n r->top = max;\n r->neg = 0;\n bn_pollute(r);\n return 1;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n INCREMENT(malloc_count);\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n if (allow_customize) {\n allow_customize = 0;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)(file); (void)(line);\n ret = malloc(num);\n#endif\n return ret;\n}']
80
0
https://github.com/libav/libav/blob/a893655bdaa726a82424367b6456d195be12ebbc/libavfilter/formats.c/#L264
void ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref) { FORMATS_REF(f, ref); }
['void ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)\n{\n FORMATS_REF(f, ref);\n}', 'void *av_realloc(void *ptr, size_t size)\n{\n#if CONFIG_MEMALIGN_HACK\n int diff;\n#endif\n if (size > (INT_MAX - 16))\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n if (!ptr)\n return av_malloc(size);\n diff = ((char *)ptr)[-1];\n return (char *)realloc((char *)ptr - diff, size + diff) + diff;\n#elif HAVE_ALIGNED_MALLOC\n return _aligned_realloc(ptr, size, 32);\n#else\n return realloc(ptr, size);\n#endif\n}']
81
0
https://github.com/openssl/openssl/blob/924e5eda2c82d737cc5a1b9c37918aa6e34825da/ssl/ssltest.c/#L462
static int verify_alpn(SSL *client, SSL *server) { const unsigned char *client_proto, *server_proto; unsigned int client_proto_len = 0, server_proto_len = 0; SSL_get0_alpn_selected(client, &client_proto, &client_proto_len); SSL_get0_alpn_selected(server, &server_proto, &server_proto_len); if (alpn_selected != NULL) { OPENSSL_free(alpn_selected); alpn_selected = NULL; } if (client_proto_len != server_proto_len || memcmp(client_proto, server_proto, client_proto_len) != 0) { BIO_printf(bio_stdout, "ALPN selected protocols differ!\n"); goto err; } if (client_proto_len > 0 && alpn_expected == NULL) { BIO_printf(bio_stdout, "ALPN unexpectedly negotiated\n"); goto err; } if (alpn_expected != NULL && (client_proto_len != strlen(alpn_expected) || memcmp(client_proto, alpn_expected, client_proto_len) != 0)) { BIO_printf(bio_stdout, "ALPN selected protocols not equal to expected protocol: %s\n", alpn_expected); goto err; } return 0; err: BIO_printf(bio_stdout, "ALPN results: client: '"); BIO_write(bio_stdout, client_proto, client_proto_len); BIO_printf(bio_stdout, "', server: '"); BIO_write(bio_stdout, server_proto, server_proto_len); BIO_printf(bio_stdout, "'\n"); BIO_printf(bio_stdout, "ALPN configured: client: '%s', server: '%s'\n", alpn_client, alpn_server); return -1; }
['static int verify_alpn(SSL *client, SSL *server)\n\t{\n\tconst unsigned char *client_proto, *server_proto;\n\tunsigned int client_proto_len = 0, server_proto_len = 0;\n\tSSL_get0_alpn_selected(client, &client_proto, &client_proto_len);\n\tSSL_get0_alpn_selected(server, &server_proto, &server_proto_len);\n\tif (alpn_selected != NULL)\n\t\t{\n\t\tOPENSSL_free(alpn_selected);\n\t\talpn_selected = NULL;\n\t\t}\n\tif (client_proto_len != server_proto_len ||\n\t memcmp(client_proto, server_proto, client_proto_len) != 0)\n\t\t{\n\t\tBIO_printf(bio_stdout, "ALPN selected protocols differ!\\n");\n\t\tgoto err;\n\t\t}\n\tif (client_proto_len > 0 && alpn_expected == NULL)\n\t\t{\n\t\tBIO_printf(bio_stdout, "ALPN unexpectedly negotiated\\n");\n\t\tgoto err;\n\t\t}\n\tif (alpn_expected != NULL &&\n\t (client_proto_len != strlen(alpn_expected) ||\n\t memcmp(client_proto, alpn_expected, client_proto_len) != 0))\n\t\t{\n\t\tBIO_printf(bio_stdout, "ALPN selected protocols not equal to expected protocol: %s\\n", alpn_expected);\n\t\tgoto err;\n\t\t}\n\treturn 0;\nerr:\n\tBIO_printf(bio_stdout, "ALPN results: client: \'");\n\tBIO_write(bio_stdout, client_proto, client_proto_len);\n\tBIO_printf(bio_stdout, "\', server: \'");\n\tBIO_write(bio_stdout, server_proto, server_proto_len);\n\tBIO_printf(bio_stdout, "\'\\n");\n\tBIO_printf(bio_stdout, "ALPN configured: client: \'%s\', server: \'%s\'\\n", alpn_client, alpn_server);\n\treturn -1;\n\t}', 'void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,\n\t\t\t unsigned *len)\n\t{\n\t*data = NULL;\n\tif (ssl->s3)\n\t\t*data = ssl->s3->alpn_selected;\n\tif (*data == NULL)\n\t\t*len = 0;\n\telse\n\t\t*len = ssl->s3->alpn_selected_len;\n\t}', 'void CRYPTO_free(void *str)\n\t{\n\tif (free_debug_func != NULL)\n\t\tfree_debug_func(str, 0);\n#ifdef LEVITTE_DEBUG_MEM\n\tfprintf(stderr, "LEVITTE_DEBUG_MEM: < 0x%p\\n", str);\n#endif\n\tfree_func(str);\n\tif (free_debug_func != NULL)\n\t\tfree_debug_func(NULL, 1);\n\t}']
82
0
https://github.com/openssl/openssl/blob/ff64702b3d83d4c77756e0fd7b624e2165dbbdf0/crypto/packet.c/#L52
int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes) { if (!ossl_assert(pkt->subs != NULL && len != 0)) return 0; if (pkt->maxsize - pkt->written < len) return 0; if (pkt->buf != NULL && (pkt->buf->length - pkt->written < len)) { size_t newlen; size_t reflen; reflen = (len > pkt->buf->length) ? len : pkt->buf->length; if (reflen > SIZE_MAX / 2) { newlen = SIZE_MAX; } else { newlen = reflen * 2; if (newlen < DEFAULT_BUF_SIZE) newlen = DEFAULT_BUF_SIZE; } if (BUF_MEM_grow(pkt->buf, newlen) == 0) return 0; } if (allocbytes != NULL) *allocbytes = WPACKET_get_curr(pkt); return 1; }
['EXT_RETURN tls_construct_ctos_sct(SSL *s, WPACKET *pkt, unsigned int context,\n X509 *x, size_t chainidx)\n{\n if (s->ct_validation_callback == NULL)\n return EXT_RETURN_NOT_SENT;\n if (x != NULL)\n return EXT_RETURN_NOT_SENT;\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signed_certificate_timestamp)\n || !WPACKET_put_bytes_u16(pkt, 0)) {\n SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_SCT,\n ERR_R_INTERNAL_ERROR);\n return EXT_RETURN_FAIL;\n }\n return EXT_RETURN_SENT;\n}', 'int WPACKET_put_bytes__(WPACKET *pkt, unsigned int val, size_t size)\n{\n unsigned char *data;\n if (!ossl_assert(size <= sizeof(unsigned int))\n || !WPACKET_allocate_bytes(pkt, size, &data)\n || !put_value(data, val, size))\n return 0;\n return 1;\n}', 'int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)\n{\n if (!WPACKET_reserve_bytes(pkt, len, allocbytes))\n return 0;\n pkt->written += len;\n pkt->curr += len;\n return 1;\n}', 'int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)\n{\n if (!ossl_assert(pkt->subs != NULL && len != 0))\n return 0;\n if (pkt->maxsize - pkt->written < len)\n return 0;\n if (pkt->buf != NULL && (pkt->buf->length - pkt->written < len)) {\n size_t newlen;\n size_t reflen;\n reflen = (len > pkt->buf->length) ? len : pkt->buf->length;\n if (reflen > SIZE_MAX / 2) {\n newlen = SIZE_MAX;\n } else {\n newlen = reflen * 2;\n if (newlen < DEFAULT_BUF_SIZE)\n newlen = DEFAULT_BUF_SIZE;\n }\n if (BUF_MEM_grow(pkt->buf, newlen) == 0)\n return 0;\n }\n if (allocbytes != NULL)\n *allocbytes = WPACKET_get_curr(pkt);\n return 1;\n}']
83
0
https://github.com/libav/libav/blob/c5254755c0154dcc7bb1191a84e6e7cf0106343b/libavcodec/mpegvideo_enc.c/#L1074
static int estimate_best_b_count(MpegEncContext *s) { AVCodec *codec = avcodec_find_encoder(s->avctx->codec_id); AVCodecContext *c = avcodec_alloc_context3(NULL); AVFrame input[FF_MAX_B_FRAMES + 2]; const int scale = s->avctx->brd_scale; int i, j, out_size, p_lambda, b_lambda, lambda2; int outbuf_size = s->width * s->height; uint8_t *outbuf = av_malloc(outbuf_size); int64_t best_rd = INT64_MAX; int best_b_count = -1; assert(scale >= 0 && scale <= 3); p_lambda = s->last_lambda_for[AV_PICTURE_TYPE_P]; b_lambda = s->last_lambda_for[AV_PICTURE_TYPE_B]; if (!b_lambda) b_lambda = p_lambda; lambda2 = (b_lambda * b_lambda + (1 << FF_LAMBDA_SHIFT) / 2) >> FF_LAMBDA_SHIFT; c->width = s->width >> scale; c->height = s->height >> scale; c->flags = CODEC_FLAG_QSCALE | CODEC_FLAG_PSNR | CODEC_FLAG_INPUT_PRESERVED ; c->flags |= s->avctx->flags & CODEC_FLAG_QPEL; c->mb_decision = s->avctx->mb_decision; c->me_cmp = s->avctx->me_cmp; c->mb_cmp = s->avctx->mb_cmp; c->me_sub_cmp = s->avctx->me_sub_cmp; c->pix_fmt = PIX_FMT_YUV420P; c->time_base = s->avctx->time_base; c->max_b_frames = s->max_b_frames; if (avcodec_open2(c, codec, NULL) < 0) return -1; for (i = 0; i < s->max_b_frames + 2; i++) { int ysize = c->width * c->height; int csize = (c->width / 2) * (c->height / 2); Picture pre_input, *pre_input_ptr = i ? s->input_picture[i - 1] : s->next_picture_ptr; avcodec_get_frame_defaults(&input[i]); input[i].data[0] = av_malloc(ysize + 2 * csize); input[i].data[1] = input[i].data[0] + ysize; input[i].data[2] = input[i].data[1] + csize; input[i].linesize[0] = c->width; input[i].linesize[1] = input[i].linesize[2] = c->width / 2; if (pre_input_ptr && (!i || s->input_picture[i - 1])) { pre_input = *pre_input_ptr; if (pre_input.f.type != FF_BUFFER_TYPE_SHARED && i) { pre_input.f.data[0] += INPLACE_OFFSET; pre_input.f.data[1] += INPLACE_OFFSET; pre_input.f.data[2] += INPLACE_OFFSET; } s->dsp.shrink[scale](input[i].data[0], input[i].linesize[0], pre_input.f.data[0], pre_input.f.linesize[0], c->width, c->height); s->dsp.shrink[scale](input[i].data[1], input[i].linesize[1], pre_input.f.data[1], pre_input.f.linesize[1], c->width >> 1, c->height >> 1); s->dsp.shrink[scale](input[i].data[2], input[i].linesize[2], pre_input.f.data[2], pre_input.f.linesize[2], c->width >> 1, c->height >> 1); } } for (j = 0; j < s->max_b_frames + 1; j++) { int64_t rd = 0; if (!s->input_picture[j]) break; c->error[0] = c->error[1] = c->error[2] = 0; input[0].pict_type = AV_PICTURE_TYPE_I; input[0].quality = 1 * FF_QP2LAMBDA; out_size = avcodec_encode_video(c, outbuf, outbuf_size, &input[0]); for (i = 0; i < s->max_b_frames + 1; i++) { int is_p = i % (j + 1) == j || i == s->max_b_frames; input[i + 1].pict_type = is_p ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B; input[i + 1].quality = is_p ? p_lambda : b_lambda; out_size = avcodec_encode_video(c, outbuf, outbuf_size, &input[i + 1]); rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3); } while (out_size) { out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL); rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3); } rd += c->error[0] + c->error[1] + c->error[2]; if (rd < best_rd) { best_rd = rd; best_b_count = j; } } av_freep(&outbuf); avcodec_close(c); av_freep(&c); for (i = 0; i < s->max_b_frames + 2; i++) { av_freep(&input[i].data[0]); } return best_b_count; }
['static int estimate_best_b_count(MpegEncContext *s)\n{\n AVCodec *codec = avcodec_find_encoder(s->avctx->codec_id);\n AVCodecContext *c = avcodec_alloc_context3(NULL);\n AVFrame input[FF_MAX_B_FRAMES + 2];\n const int scale = s->avctx->brd_scale;\n int i, j, out_size, p_lambda, b_lambda, lambda2;\n int outbuf_size = s->width * s->height;\n uint8_t *outbuf = av_malloc(outbuf_size);\n int64_t best_rd = INT64_MAX;\n int best_b_count = -1;\n assert(scale >= 0 && scale <= 3);\n p_lambda = s->last_lambda_for[AV_PICTURE_TYPE_P];\n b_lambda = s->last_lambda_for[AV_PICTURE_TYPE_B];\n if (!b_lambda)\n b_lambda = p_lambda;\n lambda2 = (b_lambda * b_lambda + (1 << FF_LAMBDA_SHIFT) / 2) >>\n FF_LAMBDA_SHIFT;\n c->width = s->width >> scale;\n c->height = s->height >> scale;\n c->flags = CODEC_FLAG_QSCALE | CODEC_FLAG_PSNR |\n CODEC_FLAG_INPUT_PRESERVED ;\n c->flags |= s->avctx->flags & CODEC_FLAG_QPEL;\n c->mb_decision = s->avctx->mb_decision;\n c->me_cmp = s->avctx->me_cmp;\n c->mb_cmp = s->avctx->mb_cmp;\n c->me_sub_cmp = s->avctx->me_sub_cmp;\n c->pix_fmt = PIX_FMT_YUV420P;\n c->time_base = s->avctx->time_base;\n c->max_b_frames = s->max_b_frames;\n if (avcodec_open2(c, codec, NULL) < 0)\n return -1;\n for (i = 0; i < s->max_b_frames + 2; i++) {\n int ysize = c->width * c->height;\n int csize = (c->width / 2) * (c->height / 2);\n Picture pre_input, *pre_input_ptr = i ? s->input_picture[i - 1] :\n s->next_picture_ptr;\n avcodec_get_frame_defaults(&input[i]);\n input[i].data[0] = av_malloc(ysize + 2 * csize);\n input[i].data[1] = input[i].data[0] + ysize;\n input[i].data[2] = input[i].data[1] + csize;\n input[i].linesize[0] = c->width;\n input[i].linesize[1] =\n input[i].linesize[2] = c->width / 2;\n if (pre_input_ptr && (!i || s->input_picture[i - 1])) {\n pre_input = *pre_input_ptr;\n if (pre_input.f.type != FF_BUFFER_TYPE_SHARED && i) {\n pre_input.f.data[0] += INPLACE_OFFSET;\n pre_input.f.data[1] += INPLACE_OFFSET;\n pre_input.f.data[2] += INPLACE_OFFSET;\n }\n s->dsp.shrink[scale](input[i].data[0], input[i].linesize[0],\n pre_input.f.data[0], pre_input.f.linesize[0],\n c->width, c->height);\n s->dsp.shrink[scale](input[i].data[1], input[i].linesize[1],\n pre_input.f.data[1], pre_input.f.linesize[1],\n c->width >> 1, c->height >> 1);\n s->dsp.shrink[scale](input[i].data[2], input[i].linesize[2],\n pre_input.f.data[2], pre_input.f.linesize[2],\n c->width >> 1, c->height >> 1);\n }\n }\n for (j = 0; j < s->max_b_frames + 1; j++) {\n int64_t rd = 0;\n if (!s->input_picture[j])\n break;\n c->error[0] = c->error[1] = c->error[2] = 0;\n input[0].pict_type = AV_PICTURE_TYPE_I;\n input[0].quality = 1 * FF_QP2LAMBDA;\n out_size = avcodec_encode_video(c, outbuf,\n outbuf_size, &input[0]);\n for (i = 0; i < s->max_b_frames + 1; i++) {\n int is_p = i % (j + 1) == j || i == s->max_b_frames;\n input[i + 1].pict_type = is_p ?\n AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B;\n input[i + 1].quality = is_p ? p_lambda : b_lambda;\n out_size = avcodec_encode_video(c, outbuf, outbuf_size,\n &input[i + 1]);\n rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);\n }\n while (out_size) {\n out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);\n rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);\n }\n rd += c->error[0] + c->error[1] + c->error[2];\n if (rd < best_rd) {\n best_rd = rd;\n best_b_count = j;\n }\n }\n av_freep(&outbuf);\n avcodec_close(c);\n av_freep(&c);\n for (i = 0; i < s->max_b_frames + 2; i++) {\n av_freep(&input[i].data[0]);\n }\n return best_b_count;\n}', 'AVCodecContext *avcodec_alloc_context3(AVCodec *codec){\n AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));\n if(avctx==NULL) return NULL;\n if(avcodec_get_context_defaults3(avctx, codec) < 0){\n av_free(avctx);\n return NULL;\n }\n return avctx;\n}', 'void *av_malloc(size_t size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-32) )\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size+32);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&31) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr,32,size))\n ptr = NULL;\n#elif HAVE_MEMALIGN\n ptr = memalign(32,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}']
84
0
https://github.com/openssl/openssl/blob/f006217bb628d05a2d5b866ff252bd94e3477e1f/crypto/mem.c/#L244
void CRYPTO_free(void *str) { #ifndef OPENSSL_NO_CRYPTO_MDEBUG if (call_malloc_debug) { CRYPTO_mem_debug_free(str, 0); free(str); CRYPTO_mem_debug_free(str, 1); } else { free(str); } #else free(str); #endif }
['unsigned long X509_NAME_hash_old(X509_NAME *x)\n{\n EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();\n unsigned long ret = 0;\n unsigned char md[16];\n if (md_ctx == NULL)\n return ret;\n i2d_X509_NAME(x, NULL);\n EVP_MD_CTX_set_flags(md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);\n if (EVP_DigestInit_ex(md_ctx, EVP_md5(), NULL)\n && EVP_DigestUpdate(md_ctx, x->bytes->data, x->bytes->length)\n && EVP_DigestFinal_ex(md_ctx, md, NULL))\n ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |\n ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)\n ) & 0xffffffffL;\n EVP_MD_CTX_free(md_ctx);\n return (ret);\n}', 'int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)\n{\n EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);\n#ifndef OPENSSL_NO_ENGINE\n if (ctx->engine && ctx->digest && (!type ||\n (type\n && (type->type ==\n ctx->digest->type))))\n goto skip_to_init;\n if (type) {\n if (ctx->engine)\n ENGINE_finish(ctx->engine);\n if (impl) {\n if (!ENGINE_init(impl)) {\n EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);\n return 0;\n }\n } else\n impl = ENGINE_get_digest_engine(type->type);\n if (impl) {\n const EVP_MD *d = ENGINE_get_digest(impl, type->type);\n if (!d) {\n EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);\n ENGINE_finish(impl);\n return 0;\n }\n type = d;\n ctx->engine = impl;\n } else\n ctx->engine = NULL;\n } else {\n if (!ctx->digest) {\n EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_NO_DIGEST_SET);\n return 0;\n }\n type = ctx->digest;\n }\n#endif\n if (ctx->digest != type) {\n if (ctx->digest && ctx->digest->ctx_size)\n OPENSSL_free(ctx->md_data);\n ctx->digest = type;\n if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {\n ctx->update = type->update;\n ctx->md_data = OPENSSL_malloc(type->ctx_size);\n if (ctx->md_data == NULL) {\n EVPerr(EVP_F_EVP_DIGESTINIT_EX, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n }\n }\n#ifndef OPENSSL_NO_ENGINE\n skip_to_init:\n#endif\n if (ctx->pctx) {\n int r;\n r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,\n EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);\n if (r <= 0 && (r != -2))\n return 0;\n }\n if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT)\n return 1;\n return ctx->digest->init(ctx);\n}', 'void CRYPTO_free(void *str)\n{\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_free(str, 0);\n free(str);\n CRYPTO_mem_debug_free(str, 1);\n } else {\n free(str);\n }\n#else\n free(str);\n#endif\n}']
85
0
https://github.com/libav/libav/blob/688417399c69aadd4c287bdb0dec82ef8799011c/libavcodec/hevcdsp_template.c/#L907
PUT_HEVC_QPEL_HV(3, 1)
['QPEL(12)', 'PUT_HEVC_QPEL_HV(3, 1)']
86
0
https://github.com/openssl/openssl/blob/15db6a40d3569789329d3f6f84e47e0e0e8f9caa/ssl/packet_locl.h/#L83
static inline void packet_forward(PACKET *pkt, size_t len) { pkt->curr += len; pkt->remaining -= len; }
['int dtls1_listen(SSL *s, struct sockaddr *client)\n{\n int next, n, ret = 0, clearpkt = 0;\n unsigned char cookie[DTLS1_COOKIE_LENGTH];\n unsigned char seq[SEQ_NUM_SIZE];\n unsigned char *data, *p, *buf;\n unsigned long reclen, fragoff, fraglen, msglen;\n unsigned int rectype, versmajor, msgseq, msgtype, clientvers, cookielen;\n BIO *rbio, *wbio;\n BUF_MEM *bufm;\n struct sockaddr_storage tmpclient;\n PACKET pkt, msgpkt, msgpayload, session, cookiepkt;\n if (!SSL_clear(s))\n return -1;\n ERR_clear_error();\n rbio = SSL_get_rbio(s);\n wbio = SSL_get_wbio(s);\n if(!rbio || !wbio) {\n SSLerr(SSL_F_DTLS1_LISTEN, SSL_R_BIO_NOT_SET);\n return -1;\n }\n BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_PEEK_MODE, 1, NULL);\n if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00)) {\n SSLerr(SSL_F_DTLS1_LISTEN, SSL_R_UNSUPPORTED_SSL_VERSION);\n return -1;\n }\n if (s->init_buf == NULL) {\n if ((bufm = BUF_MEM_new()) == NULL) {\n SSLerr(SSL_F_DTLS1_LISTEN, ERR_R_MALLOC_FAILURE);\n return -1;\n }\n if (!BUF_MEM_grow(bufm, SSL3_RT_MAX_PLAIN_LENGTH)) {\n BUF_MEM_free(bufm);\n SSLerr(SSL_F_DTLS1_LISTEN, ERR_R_MALLOC_FAILURE);\n return -1;\n }\n s->init_buf = bufm;\n }\n buf = (unsigned char *)s->init_buf->data;\n do {\n clear_sys_error();\n n = BIO_read(rbio, buf, SSL3_RT_MAX_PLAIN_LENGTH);\n if (n <= 0) {\n if(BIO_should_retry(rbio)) {\n goto end;\n }\n return -1;\n }\n clearpkt = 1;\n if (!PACKET_buf_init(&pkt, buf, n)) {\n SSLerr(SSL_F_DTLS1_LISTEN, ERR_R_INTERNAL_ERROR);\n return -1;\n }\n if (n < DTLS1_RT_HEADER_LENGTH) {\n SSLerr(SSL_F_DTLS1_LISTEN, SSL_R_RECORD_TOO_SMALL);\n goto end;\n }\n if (s->msg_callback)\n s->msg_callback(0, 0, SSL3_RT_HEADER, buf,\n DTLS1_RT_HEADER_LENGTH, s, s->msg_callback_arg);\n if (!PACKET_get_1(&pkt, &rectype)\n || !PACKET_get_1(&pkt, &versmajor)) {\n SSLerr(SSL_F_DTLS1_LISTEN, SSL_R_LENGTH_MISMATCH);\n goto end;\n }\n if (rectype != SSL3_RT_HANDSHAKE) {\n SSLerr(SSL_F_DTLS1_LISTEN, SSL_R_UNEXPECTED_MESSAGE);\n goto end;\n }\n if (versmajor != DTLS1_VERSION_MAJOR) {\n SSLerr(SSL_F_DTLS1_LISTEN, SSL_R_BAD_PROTOCOL_VERSION_NUMBER);\n goto end;\n }\n if (!PACKET_forward(&pkt, 1)\n || !PACKET_copy_bytes(&pkt, seq, SEQ_NUM_SIZE)\n || !PACKET_get_length_prefixed_2(&pkt, &msgpkt)\n || PACKET_remaining(&pkt) != 0) {\n SSLerr(SSL_F_DTLS1_LISTEN, SSL_R_LENGTH_MISMATCH);\n goto end;\n }\n if (seq[0] != 0 || seq[1] != 0) {\n SSLerr(SSL_F_DTLS1_LISTEN, SSL_R_UNEXPECTED_MESSAGE);\n goto end;\n }\n data = PACKET_data(&msgpkt);\n if (!PACKET_get_1(&msgpkt, &msgtype)\n || !PACKET_get_net_3(&msgpkt, &msglen)\n || !PACKET_get_net_2(&msgpkt, &msgseq)\n || !PACKET_get_net_3(&msgpkt, &fragoff)\n || !PACKET_get_net_3(&msgpkt, &fraglen)\n || !PACKET_get_sub_packet(&msgpkt, &msgpayload, msglen)\n || PACKET_remaining(&msgpkt) != 0) {\n SSLerr(SSL_F_DTLS1_LISTEN, SSL_R_LENGTH_MISMATCH);\n goto end;\n }\n if (msgtype != SSL3_MT_CLIENT_HELLO) {\n SSLerr(SSL_F_DTLS1_LISTEN, SSL_R_UNEXPECTED_MESSAGE);\n goto end;\n }\n if(msgseq > 2) {\n SSLerr(SSL_F_DTLS1_LISTEN, SSL_R_INVALID_SEQUENCE_NUMBER);\n goto end;\n }\n if (fragoff != 0 || fraglen != msglen) {\n SSLerr(SSL_F_DTLS1_LISTEN, SSL_R_FRAGMENTED_CLIENT_HELLO);\n goto end;\n }\n if (s->msg_callback)\n s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, data,\n msglen + DTLS1_HM_HEADER_LENGTH, s,\n s->msg_callback_arg);\n if (!PACKET_get_net_2(&msgpayload, &clientvers)) {\n SSLerr(SSL_F_DTLS1_LISTEN, SSL_R_LENGTH_MISMATCH);\n goto end;\n }\n if ((clientvers > (unsigned int)s->method->version &&\n s->method->version != DTLS_ANY_VERSION)) {\n SSLerr(SSL_F_DTLS1_LISTEN, SSL_R_WRONG_VERSION_NUMBER);\n goto end;\n }\n if (!PACKET_forward(&msgpayload, SSL3_RANDOM_SIZE)\n || !PACKET_get_length_prefixed_1(&msgpayload, &session)\n || !PACKET_get_length_prefixed_1(&msgpayload, &cookiepkt)) {\n SSLerr(SSL_F_DTLS1_LISTEN, SSL_R_LENGTH_MISMATCH);\n goto end;\n }\n if (PACKET_remaining(&cookiepkt) == 0) {\n next = LISTEN_SEND_VERIFY_REQUEST;\n } else {\n if (s->ctx->app_verify_cookie_cb == NULL) {\n SSLerr(SSL_F_DTLS1_LISTEN, SSL_R_NO_VERIFY_COOKIE_CALLBACK);\n return -1;\n }\n if (s->ctx->app_verify_cookie_cb(s, PACKET_data(&cookiepkt),\n PACKET_remaining(&cookiepkt)) ==\n 0) {\n next = LISTEN_SEND_VERIFY_REQUEST;\n } else {\n next = LISTEN_SUCCESS;\n }\n }\n if (next == LISTEN_SEND_VERIFY_REQUEST) {\n BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_PEEK_MODE, 0, NULL);\n BIO_read(rbio, buf, SSL3_RT_MAX_PLAIN_LENGTH);\n BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_PEEK_MODE, 1, NULL);\n if (s->ctx->app_gen_cookie_cb == NULL ||\n s->ctx->app_gen_cookie_cb(s, cookie, &cookielen) == 0 ||\n cookielen > 255) {\n SSLerr(SSL_F_DTLS1_LISTEN, SSL_R_COOKIE_GEN_CALLBACK_FAILURE);\n return -1;\n }\n p = &buf[DTLS1_RT_HEADER_LENGTH];\n msglen = dtls1_raw_hello_verify_request(p + DTLS1_HM_HEADER_LENGTH,\n cookie, cookielen);\n *p++ = DTLS1_MT_HELLO_VERIFY_REQUEST;\n l2n3(msglen, p);\n s2n(0, p);\n l2n3(0, p);\n l2n3(msglen, p);\n reclen = msglen + DTLS1_HM_HEADER_LENGTH;\n p = buf;\n *(p++) = SSL3_RT_HANDSHAKE;\n if (s->method->version == DTLS_ANY_VERSION) {\n *(p++) = DTLS1_VERSION >> 8;\n *(p++) = DTLS1_VERSION & 0xff;\n } else {\n *(p++) = s->version >> 8;\n *(p++) = s->version & 0xff;\n }\n memcpy(p, seq, SEQ_NUM_SIZE);\n p += SEQ_NUM_SIZE;\n s2n(reclen, p);\n reclen += DTLS1_RT_HEADER_LENGTH;\n if (s->msg_callback)\n s->msg_callback(1, 0, SSL3_RT_HEADER, buf,\n DTLS1_RT_HEADER_LENGTH, s, s->msg_callback_arg);\n if(BIO_dgram_get_peer(rbio, &tmpclient) <= 0\n || BIO_dgram_set_peer(wbio, &tmpclient) <= 0) {\n SSLerr(SSL_F_DTLS1_LISTEN, ERR_R_INTERNAL_ERROR);\n goto end;\n }\n if (BIO_write(wbio, buf, reclen) < (int)reclen) {\n if(BIO_should_retry(wbio)) {\n goto end;\n }\n return -1;\n }\n if (BIO_flush(wbio) <= 0) {\n if(BIO_should_retry(wbio)) {\n goto end;\n }\n return -1;\n }\n }\n } while (next != LISTEN_SUCCESS);\n s->d1->handshake_read_seq = 1;\n s->d1->handshake_write_seq = 1;\n s->d1->next_handshake_write_seq = 1;\n DTLS_RECORD_LAYER_set_write_sequence(&s->rlayer, seq);\n SSL_set_options(s, SSL_OP_COOKIE_EXCHANGE);\n s->state = SSL_ST_ACCEPT;\n if(BIO_dgram_get_peer(rbio, client) <= 0) {\n SSLerr(SSL_F_DTLS1_LISTEN, ERR_R_INTERNAL_ERROR);\n return -1;\n }\n ret = 1;\n clearpkt = 0;\nend:\n BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_PEEK_MODE, 0, NULL);\n if (clearpkt) {\n BIO_read(rbio, buf, SSL3_RT_MAX_PLAIN_LENGTH);\n }\n return ret;\n}', '__owur static inline int PACKET_buf_init(PACKET *pkt, unsigned char *buf,\n size_t len)\n{\n if (len > (size_t)(SIZE_MAX / 2))\n return 0;\n pkt->curr = buf;\n pkt->remaining = len;\n return 1;\n}', '__owur static inline int PACKET_get_1(PACKET *pkt, unsigned int *data)\n{\n if (!PACKET_peek_1(pkt, data))\n return 0;\n packet_forward(pkt, 1);\n return 1;\n}', '__owur static inline int PACKET_peek_1(const PACKET *pkt, unsigned int *data)\n{\n if (!PACKET_remaining(pkt))\n return 0;\n *data = *pkt->curr;\n return 1;\n}', '__owur static inline int PACKET_forward(PACKET *pkt, size_t len)\n{\n if (PACKET_remaining(pkt) < len)\n return 0;\n packet_forward(pkt, len);\n return 1;\n}', '__owur static inline int PACKET_copy_bytes(PACKET *pkt, unsigned char *data,\n size_t len)\n{\n if (!PACKET_peek_copy_bytes(pkt, data, len))\n return 0;\n packet_forward(pkt, len);\n return 1;\n}', 'static inline void packet_forward(PACKET *pkt, size_t len)\n{\n pkt->curr += len;\n pkt->remaining -= len;\n}']
87
0
https://github.com/nginx/nginx/blob/40a366c5a8927ad152eec2ab5e65c1a1f70354e4/src/core/ngx_hash.c/#L962
ngx_int_t ngx_hash_add_key(ngx_hash_keys_arrays_t *ha, ngx_str_t *key, void *value, ngx_uint_t flags) { size_t len; u_char *p; ngx_str_t *name; ngx_uint_t i, k, n, skip, last; ngx_array_t *keys, *hwc; ngx_hash_key_t *hk; last = key->len; if (flags & NGX_HASH_WILDCARD_KEY) { n = 0; for (i = 0; i < key->len; i++) { if (key->data[i] == '*') { if (++n > 1) { return NGX_DECLINED; } } if (key->data[i] == '.' && key->data[i + 1] == '.') { return NGX_DECLINED; } } if (key->len > 1 && key->data[0] == '.') { skip = 1; goto wildcard; } if (key->len > 2) { if (key->data[0] == '*' && key->data[1] == '.') { skip = 2; goto wildcard; } if (key->data[i - 2] == '.' && key->data[i - 1] == '*') { skip = 0; last -= 2; goto wildcard; } } if (n) { return NGX_DECLINED; } } k = 0; for (i = 0; i < last; i++) { if (!(flags & NGX_HASH_READONLY_KEY)) { key->data[i] = ngx_tolower(key->data[i]); } k = ngx_hash(k, key->data[i]); } k %= ha->hsize; name = ha->keys_hash[k].elts; if (name) { for (i = 0; i < ha->keys_hash[k].nelts; i++) { if (last != name[i].len) { continue; } if (ngx_strncmp(key->data, name[i].data, last) == 0) { return NGX_BUSY; } } } else { if (ngx_array_init(&ha->keys_hash[k], ha->temp_pool, 4, sizeof(ngx_str_t)) != NGX_OK) { return NGX_ERROR; } } name = ngx_array_push(&ha->keys_hash[k]); if (name == NULL) { return NGX_ERROR; } *name = *key; hk = ngx_array_push(&ha->keys); if (hk == NULL) { return NGX_ERROR; } hk->key = *key; hk->key_hash = ngx_hash_key(key->data, last); hk->value = value; return NGX_OK; wildcard: k = ngx_hash_strlow(&key->data[skip], &key->data[skip], last - skip); k %= ha->hsize; if (skip == 1) { name = ha->keys_hash[k].elts; if (name) { len = last - skip; for (i = 0; i < ha->keys_hash[k].nelts; i++) { if (len != name[i].len) { continue; } if (ngx_strncmp(&key->data[1], name[i].data, len) == 0) { return NGX_BUSY; } } } else { if (ngx_array_init(&ha->keys_hash[k], ha->temp_pool, 4, sizeof(ngx_str_t)) != NGX_OK) { return NGX_ERROR; } } name = ngx_array_push(&ha->keys_hash[k]); if (name == NULL) { return NGX_ERROR; } name->len = last - 1; name->data = ngx_pnalloc(ha->temp_pool, name->len); if (name->data == NULL) { return NGX_ERROR; } ngx_memcpy(name->data, &key->data[1], name->len); } if (skip) { p = ngx_pnalloc(ha->temp_pool, last); if (p == NULL) { return NGX_ERROR; } len = 0; n = 0; for (i = last - 1; i; i--) { if (key->data[i] == '.') { ngx_memcpy(&p[n], &key->data[i + 1], len); n += len; p[n++] = '.'; len = 0; continue; } len++; } if (len) { ngx_memcpy(&p[n], &key->data[1], len); n += len; } p[n] = '\0'; hwc = &ha->dns_wc_head; keys = &ha->dns_wc_head_hash[k]; } else { last++; p = ngx_pnalloc(ha->temp_pool, last); if (p == NULL) { return NGX_ERROR; } ngx_cpystrn(p, key->data, last); hwc = &ha->dns_wc_tail; keys = &ha->dns_wc_tail_hash[k]; } name = keys->elts; if (name) { len = last - skip; for (i = 0; i < keys->nelts; i++) { if (len != name[i].len) { continue; } if (ngx_strncmp(key->data + skip, name[i].data, len) == 0) { return NGX_BUSY; } } } else { if (ngx_array_init(keys, ha->temp_pool, 4, sizeof(ngx_str_t)) != NGX_OK) { return NGX_ERROR; } } name = ngx_array_push(keys); if (name == NULL) { return NGX_ERROR; } name->len = last - skip; name->data = ngx_pnalloc(ha->temp_pool, name->len); if (name->data == NULL) { return NGX_ERROR; } ngx_memcpy(name->data, key->data + skip, name->len); hk = ngx_array_push(hwc); if (hk == NULL) { return NGX_ERROR; } hk->key.len = last - 1; hk->key.data = p; hk->key_hash = 0; hk->value = value; return NGX_OK; }
['static ngx_int_t\nngx_http_userid_add_variables(ngx_conf_t *cf)\n{\n ngx_int_t n;\n ngx_http_variable_t *var;\n var = ngx_http_add_variable(cf, &ngx_http_userid_got, 0);\n if (var == NULL) {\n return NGX_ERROR;\n }\n var->get_handler = ngx_http_userid_got_variable;\n var = ngx_http_add_variable(cf, &ngx_http_userid_set, 0);\n if (var == NULL) {\n return NGX_ERROR;\n }\n var->get_handler = ngx_http_userid_set_variable;\n var = ngx_http_add_variable(cf, &ngx_http_userid_reset,\n NGX_HTTP_VAR_CHANGEABLE);\n if (var == NULL) {\n return NGX_ERROR;\n }\n var->get_handler = ngx_http_userid_reset_variable;\n n = ngx_http_get_variable_index(cf, &ngx_http_userid_reset);\n if (n == NGX_ERROR) {\n return NGX_ERROR;\n }\n ngx_http_userid_reset_index = n;\n return NGX_OK;\n}', 'ngx_http_variable_t *\nngx_http_add_variable(ngx_conf_t *cf, ngx_str_t *name, ngx_uint_t flags)\n{\n ngx_int_t rc;\n ngx_uint_t i;\n ngx_hash_key_t *key;\n ngx_http_variable_t *v;\n ngx_http_core_main_conf_t *cmcf;\n cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);\n key = cmcf->variables_keys->keys.elts;\n for (i = 0; i < cmcf->variables_keys->keys.nelts; i++) {\n if (name->len != key[i].key.len\n || ngx_strncasecmp(name->data, key[i].key.data, name->len) != 0)\n {\n continue;\n }\n v = key[i].value;\n if (!(v->flags & NGX_HTTP_VAR_CHANGEABLE)) {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "the duplicate \\"%V\\" variable", name);\n return NULL;\n }\n return v;\n }\n v = ngx_palloc(cf->pool, sizeof(ngx_http_variable_t));\n if (v == NULL) {\n return NULL;\n }\n v->name.len = name->len;\n v->name.data = ngx_pnalloc(cf->pool, name->len);\n if (v->name.data == NULL) {\n return NULL;\n }\n ngx_strlow(v->name.data, name->data, name->len);\n v->set_handler = NULL;\n v->get_handler = NULL;\n v->data = 0;\n v->flags = flags;\n v->index = 0;\n rc = ngx_hash_add_key(cmcf->variables_keys, &v->name, v, 0);\n if (rc == NGX_ERROR) {\n return NULL;\n }\n if (rc == NGX_BUSY) {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "conflicting variable name \\"%V\\"", name);\n return NULL;\n }\n return v;\n}', "ngx_int_t\nngx_hash_add_key(ngx_hash_keys_arrays_t *ha, ngx_str_t *key, void *value,\n ngx_uint_t flags)\n{\n size_t len;\n u_char *p;\n ngx_str_t *name;\n ngx_uint_t i, k, n, skip, last;\n ngx_array_t *keys, *hwc;\n ngx_hash_key_t *hk;\n last = key->len;\n if (flags & NGX_HASH_WILDCARD_KEY) {\n n = 0;\n for (i = 0; i < key->len; i++) {\n if (key->data[i] == '*') {\n if (++n > 1) {\n return NGX_DECLINED;\n }\n }\n if (key->data[i] == '.' && key->data[i + 1] == '.') {\n return NGX_DECLINED;\n }\n }\n if (key->len > 1 && key->data[0] == '.') {\n skip = 1;\n goto wildcard;\n }\n if (key->len > 2) {\n if (key->data[0] == '*' && key->data[1] == '.') {\n skip = 2;\n goto wildcard;\n }\n if (key->data[i - 2] == '.' && key->data[i - 1] == '*') {\n skip = 0;\n last -= 2;\n goto wildcard;\n }\n }\n if (n) {\n return NGX_DECLINED;\n }\n }\n k = 0;\n for (i = 0; i < last; i++) {\n if (!(flags & NGX_HASH_READONLY_KEY)) {\n key->data[i] = ngx_tolower(key->data[i]);\n }\n k = ngx_hash(k, key->data[i]);\n }\n k %= ha->hsize;\n name = ha->keys_hash[k].elts;\n if (name) {\n for (i = 0; i < ha->keys_hash[k].nelts; i++) {\n if (last != name[i].len) {\n continue;\n }\n if (ngx_strncmp(key->data, name[i].data, last) == 0) {\n return NGX_BUSY;\n }\n }\n } else {\n if (ngx_array_init(&ha->keys_hash[k], ha->temp_pool, 4,\n sizeof(ngx_str_t))\n != NGX_OK)\n {\n return NGX_ERROR;\n }\n }\n name = ngx_array_push(&ha->keys_hash[k]);\n if (name == NULL) {\n return NGX_ERROR;\n }\n *name = *key;\n hk = ngx_array_push(&ha->keys);\n if (hk == NULL) {\n return NGX_ERROR;\n }\n hk->key = *key;\n hk->key_hash = ngx_hash_key(key->data, last);\n hk->value = value;\n return NGX_OK;\nwildcard:\n k = ngx_hash_strlow(&key->data[skip], &key->data[skip], last - skip);\n k %= ha->hsize;\n if (skip == 1) {\n name = ha->keys_hash[k].elts;\n if (name) {\n len = last - skip;\n for (i = 0; i < ha->keys_hash[k].nelts; i++) {\n if (len != name[i].len) {\n continue;\n }\n if (ngx_strncmp(&key->data[1], name[i].data, len) == 0) {\n return NGX_BUSY;\n }\n }\n } else {\n if (ngx_array_init(&ha->keys_hash[k], ha->temp_pool, 4,\n sizeof(ngx_str_t))\n != NGX_OK)\n {\n return NGX_ERROR;\n }\n }\n name = ngx_array_push(&ha->keys_hash[k]);\n if (name == NULL) {\n return NGX_ERROR;\n }\n name->len = last - 1;\n name->data = ngx_pnalloc(ha->temp_pool, name->len);\n if (name->data == NULL) {\n return NGX_ERROR;\n }\n ngx_memcpy(name->data, &key->data[1], name->len);\n }\n if (skip) {\n p = ngx_pnalloc(ha->temp_pool, last);\n if (p == NULL) {\n return NGX_ERROR;\n }\n len = 0;\n n = 0;\n for (i = last - 1; i; i--) {\n if (key->data[i] == '.') {\n ngx_memcpy(&p[n], &key->data[i + 1], len);\n n += len;\n p[n++] = '.';\n len = 0;\n continue;\n }\n len++;\n }\n if (len) {\n ngx_memcpy(&p[n], &key->data[1], len);\n n += len;\n }\n p[n] = '\\0';\n hwc = &ha->dns_wc_head;\n keys = &ha->dns_wc_head_hash[k];\n } else {\n last++;\n p = ngx_pnalloc(ha->temp_pool, last);\n if (p == NULL) {\n return NGX_ERROR;\n }\n ngx_cpystrn(p, key->data, last);\n hwc = &ha->dns_wc_tail;\n keys = &ha->dns_wc_tail_hash[k];\n }\n name = keys->elts;\n if (name) {\n len = last - skip;\n for (i = 0; i < keys->nelts; i++) {\n if (len != name[i].len) {\n continue;\n }\n if (ngx_strncmp(key->data + skip, name[i].data, len) == 0) {\n return NGX_BUSY;\n }\n }\n } else {\n if (ngx_array_init(keys, ha->temp_pool, 4, sizeof(ngx_str_t)) != NGX_OK)\n {\n return NGX_ERROR;\n }\n }\n name = ngx_array_push(keys);\n if (name == NULL) {\n return NGX_ERROR;\n }\n name->len = last - skip;\n name->data = ngx_pnalloc(ha->temp_pool, name->len);\n if (name->data == NULL) {\n return NGX_ERROR;\n }\n ngx_memcpy(name->data, key->data + skip, name->len);\n hk = ngx_array_push(hwc);\n if (hk == NULL) {\n return NGX_ERROR;\n }\n hk->key.len = last - 1;\n hk->key.data = p;\n hk->key_hash = 0;\n hk->value = value;\n return NGX_OK;\n}"]
88
0
https://gitlab.com/libtiff/libtiff/blob/5b06ac3f2851cf84ec425f1a0c3ddcf954e625aa/tools/tiffcrop.c/#L7506
static int createCroppedImage(struct image_data *image, struct crop_mask *crop, unsigned char **read_buff_ptr, unsigned char **crop_buff_ptr) { tsize_t cropsize; unsigned char *read_buff = NULL; unsigned char *crop_buff = NULL; unsigned char *new_buff = NULL; static tsize_t prev_cropsize = 0; read_buff = *read_buff_ptr; crop_buff = read_buff; *crop_buff_ptr = read_buff; crop->combined_width = image->width; crop->combined_length = image->length; cropsize = crop->bufftotal; crop_buff = *crop_buff_ptr; if (!crop_buff) { crop_buff = (unsigned char *)_TIFFmalloc(cropsize); *crop_buff_ptr = crop_buff; _TIFFmemset(crop_buff, 0, cropsize); prev_cropsize = cropsize; } else { if (prev_cropsize < cropsize) { new_buff = _TIFFrealloc(crop_buff, cropsize); if (!new_buff) { free (crop_buff); crop_buff = (unsigned char *)_TIFFmalloc(cropsize); } else crop_buff = new_buff; _TIFFmemset(crop_buff, 0, cropsize); } } if (!crop_buff) { TIFFError("createCroppedImage", "Unable to allocate/reallocate crop buffer"); return (-1); } *crop_buff_ptr = crop_buff; if (crop->crop_mode & CROP_INVERT) { switch (crop->photometric) { case PHOTOMETRIC_MINISWHITE: case PHOTOMETRIC_MINISBLACK: image->photometric = crop->photometric; break; case INVERT_DATA_ONLY: case INVERT_DATA_AND_TAG: if (invertImage(image->photometric, image->spp, image->bps, crop->combined_width, crop->combined_length, crop_buff)) { TIFFError("createCroppedImage", "Failed to invert colorspace for image or cropped selection"); return (-1); } if (crop->photometric == INVERT_DATA_AND_TAG) { switch (image->photometric) { case PHOTOMETRIC_MINISWHITE: image->photometric = PHOTOMETRIC_MINISBLACK; break; case PHOTOMETRIC_MINISBLACK: image->photometric = PHOTOMETRIC_MINISWHITE; break; default: break; } } break; default: break; } } if (crop->crop_mode & CROP_MIRROR) { if (mirrorImage(image->spp, image->bps, crop->mirror, crop->combined_width, crop->combined_length, crop_buff)) { TIFFError("createCroppedImage", "Failed to mirror image or cropped selection %s", (crop->rotation == MIRROR_HORIZ) ? "horizontally" : "vertically"); return (-1); } } if (crop->crop_mode & CROP_ROTATE) { if (rotateImage(crop->rotation, image, &crop->combined_width, &crop->combined_length, crop_buff_ptr)) { TIFFError("createCroppedImage", "Failed to rotate image or cropped selection by %d degrees", crop->rotation); return (-1); } } if (crop_buff == read_buff) *read_buff_ptr = NULL; return (0); }
['static int\ncreateCroppedImage(struct image_data *image, struct crop_mask *crop,\n unsigned char **read_buff_ptr, unsigned char **crop_buff_ptr)\n {\n tsize_t cropsize;\n unsigned char *read_buff = NULL;\n unsigned char *crop_buff = NULL;\n unsigned char *new_buff = NULL;\n static tsize_t prev_cropsize = 0;\n read_buff = *read_buff_ptr;\n crop_buff = read_buff;\n *crop_buff_ptr = read_buff;\n crop->combined_width = image->width;\n crop->combined_length = image->length;\n cropsize = crop->bufftotal;\n crop_buff = *crop_buff_ptr;\n if (!crop_buff)\n {\n crop_buff = (unsigned char *)_TIFFmalloc(cropsize);\n *crop_buff_ptr = crop_buff;\n _TIFFmemset(crop_buff, 0, cropsize);\n prev_cropsize = cropsize;\n }\n else\n {\n if (prev_cropsize < cropsize)\n {\n new_buff = _TIFFrealloc(crop_buff, cropsize);\n if (!new_buff)\n {\n\tfree (crop_buff);\n crop_buff = (unsigned char *)_TIFFmalloc(cropsize);\n }\n else\n crop_buff = new_buff;\n _TIFFmemset(crop_buff, 0, cropsize);\n }\n }\n if (!crop_buff)\n {\n TIFFError("createCroppedImage", "Unable to allocate/reallocate crop buffer");\n return (-1);\n }\n *crop_buff_ptr = crop_buff;\n if (crop->crop_mode & CROP_INVERT)\n {\n switch (crop->photometric)\n {\n case PHOTOMETRIC_MINISWHITE:\n case PHOTOMETRIC_MINISBLACK:\n\t image->photometric = crop->photometric;\n\t break;\n case INVERT_DATA_ONLY:\n case INVERT_DATA_AND_TAG:\n if (invertImage(image->photometric, image->spp, image->bps,\n crop->combined_width, crop->combined_length, crop_buff))\n {\n TIFFError("createCroppedImage",\n "Failed to invert colorspace for image or cropped selection");\n return (-1);\n }\n if (crop->photometric == INVERT_DATA_AND_TAG)\n {\n switch (image->photometric)\n {\n case PHOTOMETRIC_MINISWHITE:\n \t image->photometric = PHOTOMETRIC_MINISBLACK;\n\t break;\n case PHOTOMETRIC_MINISBLACK:\n \t image->photometric = PHOTOMETRIC_MINISWHITE;\n\t break;\n default:\n\t break;\n\t }\n\t }\n break;\n default: break;\n }\n }\n if (crop->crop_mode & CROP_MIRROR)\n {\n if (mirrorImage(image->spp, image->bps, crop->mirror,\n crop->combined_width, crop->combined_length, crop_buff))\n {\n TIFFError("createCroppedImage", "Failed to mirror image or cropped selection %s",\n\t (crop->rotation == MIRROR_HORIZ) ? "horizontally" : "vertically");\n return (-1);\n }\n }\n if (crop->crop_mode & CROP_ROTATE)\n {\n if (rotateImage(crop->rotation, image, &crop->combined_width,\n &crop->combined_length, crop_buff_ptr))\n {\n TIFFError("createCroppedImage",\n "Failed to rotate image or cropped selection by %d degrees", crop->rotation);\n return (-1);\n }\n }\n if (crop_buff == read_buff)\n *read_buff_ptr = NULL;\n return (0);\n }', 'void*\n_TIFFrealloc(void* p, tmsize_t s)\n{\n\treturn (realloc(p, (size_t) s));\n}', 'void*\n_TIFFmalloc(tmsize_t s)\n{\n if (s == 0)\n return ((void *) NULL);\n\treturn (malloc((size_t) s));\n}', 'void\n_TIFFmemset(void* p, int v, tmsize_t c)\n{\n\tmemset(p, v, (size_t) c);\n}']
89
0
https://github.com/nginx/nginx/blob/8ce8f6667f3f14c004148138c0aec3dff79c350b/src/core/ngx_open_file_cache.c/#L338
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_wrapper(name, of, &fi) == NGX_FILE_ERROR) { 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, 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, 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 (NGX_HAVE_OPENAT) && of->disable_symlinks == file->disable_symlinks #endif )) { 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; #if (NGX_HAVE_OPENAT) of->failed = file->disable_symlinks ? ngx_openat_file_n : ngx_open_file_n; #else of->failed = ngx_open_file_n; #endif } 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, 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 " \"%V\" failed", name); } goto add_event; } ngx_rbtree_delete(&cache->rbtree, &file->node); cache->current--; file->close = 1; goto create; } rc = ngx_open_and_stat_file(name, 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 (NGX_HAVE_OPENAT) file->disable_symlinks = of->disable_symlinks; #endif 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 " \"%V\" failed", name); } } return NGX_ERROR; }
['static ngx_int_t\nngx_http_index_handler(ngx_http_request_t *r)\n{\n u_char *p, *name;\n size_t len, root, reserve, allocated;\n ngx_int_t rc;\n ngx_str_t path, uri;\n ngx_uint_t i, dir_tested;\n ngx_http_index_t *index;\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_index_loc_conf_t *ilcf;\n ngx_http_script_len_code_pt lcode;\n if (r->uri.data[r->uri.len - 1] != \'/\') {\n return NGX_DECLINED;\n }\n if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD|NGX_HTTP_POST))) {\n return NGX_DECLINED;\n }\n ilcf = ngx_http_get_module_loc_conf(r, ngx_http_index_module);\n clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);\n allocated = 0;\n root = 0;\n dir_tested = 0;\n name = NULL;\n path.data = NULL;\n index = ilcf->indices->elts;\n for (i = 0; i < ilcf->indices->nelts; i++) {\n if (index[i].lengths == NULL) {\n if (index[i].name.data[0] == \'/\') {\n return ngx_http_internal_redirect(r, &index[i].name, &r->args);\n }\n reserve = ilcf->max_index_len;\n len = index[i].name.len;\n } else {\n ngx_memzero(&e, sizeof(ngx_http_script_engine_t));\n e.ip = index[i].lengths->elts;\n e.request = r;\n e.flushed = 1;\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 reserve = len + 16;\n }\n if (reserve > allocated) {\n name = ngx_http_map_uri_to_path(r, &path, &root, reserve);\n if (name == NULL) {\n return NGX_ERROR;\n }\n allocated = path.data + path.len - name;\n }\n if (index[i].values == NULL) {\n ngx_memcpy(name, index[i].name.data, index[i].name.len);\n path.len = (name + index[i].name.len - 1) - path.data;\n } else {\n e.ip = index[i].values->elts;\n e.pos = name;\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 if (*name == \'/\') {\n uri.len = len - 1;\n uri.data = name;\n return ngx_http_internal_redirect(r, &uri, &r->args);\n }\n path.len = e.pos - path.data;\n *e.pos = \'\\0\';\n }\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "open index \\"%V\\"", &path);\n ngx_memzero(&of, sizeof(ngx_open_file_info_t));\n of.read_ahead = clcf->read_ahead;\n of.directio = clcf->directio;\n of.valid = clcf->open_file_cache_valid;\n of.min_uses = clcf->open_file_cache_min_uses;\n of.test_only = 1;\n of.errors = clcf->open_file_cache_errors;\n of.events = clcf->open_file_cache_events;\n#if (NGX_HAVE_OPENAT)\n of.disable_symlinks = clcf->disable_symlinks;\n#endif\n if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)\n != NGX_OK)\n {\n ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, of.err,\n "%s \\"%s\\" failed", of.failed, path.data);\n if (of.err == 0) {\n return NGX_HTTP_INTERNAL_SERVER_ERROR;\n }\n#if (NGX_HAVE_OPENAT)\n if (of.err == NGX_EMLINK\n || of.err == NGX_ELOOP)\n {\n return NGX_HTTP_FORBIDDEN;\n }\n#endif\n if (of.err == NGX_ENOTDIR\n || of.err == NGX_ENAMETOOLONG\n || of.err == NGX_EACCES)\n {\n return ngx_http_index_error(r, clcf, path.data, of.err);\n }\n if (!dir_tested) {\n rc = ngx_http_index_test_dir(r, clcf, path.data, name - 1);\n if (rc != NGX_OK) {\n return rc;\n }\n dir_tested = 1;\n }\n if (of.err == NGX_ENOENT) {\n continue;\n }\n ngx_log_error(NGX_LOG_CRIT, r->connection->log, of.err,\n "%s \\"%s\\" failed", of.failed, path.data);\n return NGX_HTTP_INTERNAL_SERVER_ERROR;\n }\n uri.len = r->uri.len + len - 1;\n if (!clcf->alias) {\n uri.data = path.data + root;\n } else {\n uri.data = ngx_pnalloc(r->pool, uri.len);\n if (uri.data == NULL) {\n return NGX_HTTP_INTERNAL_SERVER_ERROR;\n }\n p = ngx_copy(uri.data, r->uri.data, r->uri.len);\n ngx_memcpy(p, name, len - 1);\n }\n return ngx_http_internal_redirect(r, &uri, &r->args);\n }\n return NGX_DECLINED;\n}', 'ngx_int_t\nngx_open_cached_file(ngx_open_file_cache_t *cache, ngx_str_t *name,\n ngx_open_file_info_t *of, ngx_pool_t *pool)\n{\n time_t now;\n uint32_t hash;\n ngx_int_t rc;\n ngx_file_info_t fi;\n ngx_pool_cleanup_t *cln;\n ngx_cached_open_file_t *file;\n ngx_pool_cleanup_file_t *clnf;\n ngx_open_file_cache_cleanup_t *ofcln;\n of->fd = NGX_INVALID_FILE;\n of->err = 0;\n if (cache == NULL) {\n if (of->test_only) {\n if (ngx_file_info_wrapper(name, of, &fi) == NGX_FILE_ERROR) {\n return NGX_ERROR;\n }\n of->uniq = ngx_file_uniq(&fi);\n of->mtime = ngx_file_mtime(&fi);\n of->size = ngx_file_size(&fi);\n of->fs_size = ngx_file_fs_size(&fi);\n of->is_dir = ngx_is_dir(&fi);\n of->is_file = ngx_is_file(&fi);\n of->is_link = ngx_is_link(&fi);\n of->is_exec = ngx_is_exec(&fi);\n return NGX_OK;\n }\n cln = ngx_pool_cleanup_add(pool, sizeof(ngx_pool_cleanup_file_t));\n if (cln == NULL) {\n return NGX_ERROR;\n }\n rc = ngx_open_and_stat_file(name, of, pool->log);\n if (rc == NGX_OK && !of->is_dir) {\n cln->handler = ngx_pool_cleanup_file;\n clnf = cln->data;\n clnf->fd = of->fd;\n clnf->name = name->data;\n clnf->log = pool->log;\n }\n return rc;\n }\n cln = ngx_pool_cleanup_add(pool, sizeof(ngx_open_file_cache_cleanup_t));\n if (cln == NULL) {\n return NGX_ERROR;\n }\n now = ngx_time();\n hash = ngx_crc32_long(name->data, name->len);\n file = ngx_open_file_lookup(cache, name, hash);\n if (file) {\n file->uses++;\n ngx_queue_remove(&file->queue);\n if (file->fd == NGX_INVALID_FILE && file->err == 0 && !file->is_dir) {\n rc = ngx_open_and_stat_file(name, of, pool->log);\n if (rc != NGX_OK && (of->err == 0 || !of->errors)) {\n goto failed;\n }\n goto add_event;\n }\n if (file->use_event\n || (file->event == NULL\n && (of->uniq == 0 || of->uniq == file->uniq)\n && now - file->created < of->valid\n#if (NGX_HAVE_OPENAT)\n && of->disable_symlinks == file->disable_symlinks\n#endif\n ))\n {\n if (file->err == 0) {\n of->fd = file->fd;\n of->uniq = file->uniq;\n of->mtime = file->mtime;\n of->size = file->size;\n of->is_dir = file->is_dir;\n of->is_file = file->is_file;\n of->is_link = file->is_link;\n of->is_exec = file->is_exec;\n of->is_directio = file->is_directio;\n if (!file->is_dir) {\n file->count++;\n ngx_open_file_add_event(cache, file, of, pool->log);\n }\n } else {\n of->err = file->err;\n#if (NGX_HAVE_OPENAT)\n of->failed = file->disable_symlinks ? ngx_openat_file_n\n : ngx_open_file_n;\n#else\n of->failed = ngx_open_file_n;\n#endif\n }\n goto found;\n }\n ngx_log_debug4(NGX_LOG_DEBUG_CORE, pool->log, 0,\n "retest open file: %s, fd:%d, c:%d, e:%d",\n file->name, file->fd, file->count, file->err);\n if (file->is_dir) {\n of->test_dir = 1;\n }\n of->fd = file->fd;\n of->uniq = file->uniq;\n rc = ngx_open_and_stat_file(name, of, pool->log);\n if (rc != NGX_OK && (of->err == 0 || !of->errors)) {\n goto failed;\n }\n if (of->is_dir) {\n if (file->is_dir || file->err) {\n goto update;\n }\n } else if (of->err == 0) {\n if (file->is_dir || file->err) {\n goto add_event;\n }\n if (of->uniq == file->uniq) {\n if (file->event) {\n file->use_event = 1;\n }\n of->is_directio = file->is_directio;\n goto update;\n }\n } else {\n if (file->err || file->is_dir) {\n goto update;\n }\n }\n if (file->count == 0) {\n ngx_open_file_del_event(file);\n if (ngx_close_file(file->fd) == NGX_FILE_ERROR) {\n ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno,\n ngx_close_file_n " \\"%V\\" failed", name);\n }\n goto add_event;\n }\n ngx_rbtree_delete(&cache->rbtree, &file->node);\n cache->current--;\n file->close = 1;\n goto create;\n }\n rc = ngx_open_and_stat_file(name, of, pool->log);\n if (rc != NGX_OK && (of->err == 0 || !of->errors)) {\n goto failed;\n }\ncreate:\n if (cache->current >= cache->max) {\n ngx_expire_old_cached_files(cache, 0, pool->log);\n }\n file = ngx_alloc(sizeof(ngx_cached_open_file_t), pool->log);\n if (file == NULL) {\n goto failed;\n }\n file->name = ngx_alloc(name->len + 1, pool->log);\n if (file->name == NULL) {\n ngx_free(file);\n file = NULL;\n goto failed;\n }\n ngx_cpystrn(file->name, name->data, name->len + 1);\n file->node.key = hash;\n ngx_rbtree_insert(&cache->rbtree, &file->node);\n cache->current++;\n file->uses = 1;\n file->count = 0;\n file->use_event = 0;\n file->event = NULL;\nadd_event:\n ngx_open_file_add_event(cache, file, of, pool->log);\nupdate:\n file->fd = of->fd;\n file->err = of->err;\n#if (NGX_HAVE_OPENAT)\n file->disable_symlinks = of->disable_symlinks;\n#endif\n if (of->err == 0) {\n file->uniq = of->uniq;\n file->mtime = of->mtime;\n file->size = of->size;\n file->close = 0;\n file->is_dir = of->is_dir;\n file->is_file = of->is_file;\n file->is_link = of->is_link;\n file->is_exec = of->is_exec;\n file->is_directio = of->is_directio;\n if (!of->is_dir) {\n file->count++;\n }\n }\n file->created = now;\nfound:\n file->accessed = now;\n ngx_queue_insert_head(&cache->expire_queue, &file->queue);\n ngx_log_debug5(NGX_LOG_DEBUG_CORE, pool->log, 0,\n "cached open file: %s, fd:%d, c:%d, e:%d, u:%d",\n file->name, file->fd, file->count, file->err, file->uses);\n if (of->err == 0) {\n if (!of->is_dir) {\n cln->handler = ngx_open_file_cleanup;\n ofcln = cln->data;\n ofcln->cache = cache;\n ofcln->file = file;\n ofcln->min_uses = of->min_uses;\n ofcln->log = pool->log;\n }\n return NGX_OK;\n }\n return NGX_ERROR;\nfailed:\n if (file) {\n ngx_rbtree_delete(&cache->rbtree, &file->node);\n cache->current--;\n if (file->count == 0) {\n if (file->fd != NGX_INVALID_FILE) {\n if (ngx_close_file(file->fd) == NGX_FILE_ERROR) {\n ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno,\n ngx_close_file_n " \\"%s\\" failed",\n file->name);\n }\n }\n ngx_free(file->name);\n ngx_free(file);\n } else {\n file->close = 1;\n }\n }\n if (of->fd != NGX_INVALID_FILE) {\n if (ngx_close_file(of->fd) == NGX_FILE_ERROR) {\n ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno,\n ngx_close_file_n " \\"%V\\" failed", name);\n }\n }\n return NGX_ERROR;\n}']
90
0
https://github.com/openssl/openssl/blob/a8140a42f5ee9e4e1423b5b6b319dc4657659f6f/crypto/bn/bn_lib.c/#L232
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *a = NULL; if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return NULL; } if (BN_get_flags(b, BN_FLG_SECURE)) a = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = OPENSSL_zalloc(words * sizeof(*a)); if (a == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return NULL; } assert(b->top <= words); if (b->top > 0) memcpy(a, b->d, sizeof(*a) * b->top); return a; }
['static int bnrand_range(BNRAND_FLAG flag, BIGNUM *r, const BIGNUM *range)\n{\n int n;\n int count = 100;\n if (range->neg || BN_is_zero(range)) {\n BNerr(BN_F_BNRAND_RANGE, BN_R_INVALID_RANGE);\n return 0;\n }\n n = BN_num_bits(range);\n if (n == 1)\n BN_zero(r);\n else if (!BN_is_bit_set(range, n - 2) && !BN_is_bit_set(range, n - 3)) {\n do {\n if (!bnrand(flag, r, n + 1, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))\n return 0;\n if (BN_cmp(r, range) >= 0) {\n if (!BN_sub(r, r, range))\n return 0;\n if (BN_cmp(r, range) >= 0)\n if (!BN_sub(r, r, range))\n return 0;\n }\n if (!--count) {\n BNerr(BN_F_BNRAND_RANGE, BN_R_TOO_MANY_ITERATIONS);\n return 0;\n }\n }\n while (BN_cmp(r, range) >= 0);\n } else {\n do {\n if (!bnrand(flag, r, n, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))\n return 0;\n if (!--count) {\n BNerr(BN_F_BNRAND_RANGE, BN_R_TOO_MANY_ITERATIONS);\n return 0;\n }\n }\n while (BN_cmp(r, range) >= 0);\n }\n bn_check_top(r);\n return 1;\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#if defined(FIPS_MODE)\n BNerr(BN_F_BNRAND, ERR_R_INTERNAL_ERROR);\n goto err;\n#else\n b = flag == NORMAL ? RAND_bytes(buf, bytes) : RAND_priv_bytes(buf, bytes);\n#endif\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 !defined(FIPS_MODE)\n if (RAND_bytes(&c, 1) <= 0)\n goto err;\n#endif\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_cmp(const BIGNUM *a, const BIGNUM *b)\n{\n int i;\n int gt, lt;\n BN_ULONG t1, t2;\n if ((a == NULL) || (b == NULL)) {\n if (a != NULL)\n return -1;\n else if (b != NULL)\n return 1;\n else\n return 0;\n }\n bn_check_top(a);\n bn_check_top(b);\n if (a->neg != b->neg) {\n if (a->neg)\n return -1;\n else\n return 1;\n }\n if (a->neg == 0) {\n gt = 1;\n lt = -1;\n } else {\n gt = -1;\n lt = 1;\n }\n if (a->top > b->top)\n return gt;\n if (a->top < b->top)\n return lt;\n for (i = a->top - 1; i >= 0; i--) {\n t1 = a->d[i];\n t2 = b->d[i];\n if (t1 > t2)\n return gt;\n if (t1 < t2)\n return lt;\n }\n return 0;\n}', 'int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int ret, r_neg, cmp_res;\n bn_check_top(a);\n bn_check_top(b);\n if (a->neg != b->neg) {\n r_neg = a->neg;\n ret = BN_uadd(r, a, b);\n } else {\n cmp_res = BN_ucmp(a, b);\n if (cmp_res > 0) {\n r_neg = a->neg;\n ret = BN_usub(r, a, b);\n } else if (cmp_res < 0) {\n r_neg = !b->neg;\n ret = BN_usub(r, b, a);\n } else {\n r_neg = 0;\n BN_zero(r);\n ret = 1;\n }\n }\n r->neg = r_neg;\n bn_check_top(r);\n return ret;\n}', 'int BN_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 if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n INCREMENT(malloc_count);\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n if (allow_customize) {\n allow_customize = 0;\n }\n#if !defined(OPENSSL_NO_CRYPTO_MDEBUG) && !defined(FIPS_MODE)\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)(file); (void)(line);\n ret = malloc(num);\n#endif\n return ret;\n}']
91
0
https://github.com/libav/libav/blob/2934cd9dbf390962ec008182f9eddd4296cf5527/libavcodec/vp8.c/#L1242
static av_always_inline void vp8_mc(VP8Context *s, int luma, uint8_t *dst, uint8_t *src, const VP56mv *mv, int x_off, int y_off, int block_w, int block_h, int width, int height, int linesize, vp8_mc_func mc_func[3][3]) { if (AV_RN32A(mv)) { static const uint8_t idx[3][8] = { { 0, 1, 2, 1, 2, 1, 2, 1 }, { 0, 3, 5, 3, 5, 3, 5, 3 }, { 0, 2, 3, 2, 3, 2, 3, 2 }, }; int mx = (mv->x << luma)&7, mx_idx = idx[0][mx]; int my = (mv->y << luma)&7, my_idx = idx[0][my]; x_off += mv->x >> (3 - luma); y_off += mv->y >> (3 - luma); src += y_off * linesize + x_off; if (x_off < mx_idx || x_off >= width - block_w - idx[2][mx] || y_off < my_idx || y_off >= height - block_h - idx[2][my]) { ff_emulated_edge_mc(s->edge_emu_buffer, src - my_idx * linesize - mx_idx, linesize, block_w + idx[1][mx], block_h + idx[1][my], x_off - mx_idx, y_off - my_idx, width, height); src = s->edge_emu_buffer + mx_idx + linesize * my_idx; } mc_func[my_idx][mx_idx](dst, linesize, src, linesize, block_h, mx, my); } else mc_func[0][0](dst, linesize, src + y_off * linesize + x_off, linesize, block_h, 0, 0); }
['static av_always_inline\nvoid vp8_mc_part(VP8Context *s, uint8_t *dst[3],\n AVFrame *ref_frame, int x_off, int y_off,\n int bx_off, int by_off,\n int block_w, int block_h,\n int width, int height, VP56mv *mv)\n{\n VP56mv uvmv = *mv;\n vp8_mc(s, 1, dst[0] + by_off * s->linesize + bx_off,\n ref_frame->data[0], mv, x_off + bx_off, y_off + by_off,\n block_w, block_h, width, height, s->linesize,\n s->put_pixels_tab[block_w == 8]);\n if (s->profile == 3) {\n uvmv.x &= ~7;\n uvmv.y &= ~7;\n }\n x_off >>= 1; y_off >>= 1;\n bx_off >>= 1; by_off >>= 1;\n width >>= 1; height >>= 1;\n block_w >>= 1; block_h >>= 1;\n vp8_mc(s, 0, dst[1] + by_off * s->uvlinesize + bx_off,\n ref_frame->data[1], &uvmv, x_off + bx_off, y_off + by_off,\n block_w, block_h, width, height, s->uvlinesize,\n s->put_pixels_tab[1 + (block_w == 4)]);\n vp8_mc(s, 0, dst[2] + by_off * s->uvlinesize + bx_off,\n ref_frame->data[2], &uvmv, x_off + bx_off, y_off + by_off,\n block_w, block_h, width, height, s->uvlinesize,\n s->put_pixels_tab[1 + (block_w == 4)]);\n}', 'static av_always_inline\nvoid vp8_mc(VP8Context *s, int luma,\n uint8_t *dst, uint8_t *src, const VP56mv *mv,\n int x_off, int y_off, int block_w, int block_h,\n int width, int height, int linesize,\n vp8_mc_func mc_func[3][3])\n{\n if (AV_RN32A(mv)) {\n static const uint8_t idx[3][8] = {\n { 0, 1, 2, 1, 2, 1, 2, 1 },\n { 0, 3, 5, 3, 5, 3, 5, 3 },\n { 0, 2, 3, 2, 3, 2, 3, 2 },\n };\n int mx = (mv->x << luma)&7, mx_idx = idx[0][mx];\n int my = (mv->y << luma)&7, my_idx = idx[0][my];\n x_off += mv->x >> (3 - luma);\n y_off += mv->y >> (3 - luma);\n src += y_off * linesize + x_off;\n if (x_off < mx_idx || x_off >= width - block_w - idx[2][mx] ||\n y_off < my_idx || y_off >= height - block_h - idx[2][my]) {\n ff_emulated_edge_mc(s->edge_emu_buffer, src - my_idx * linesize - mx_idx, linesize,\n block_w + idx[1][mx], block_h + idx[1][my],\n x_off - mx_idx, y_off - my_idx, width, height);\n src = s->edge_emu_buffer + mx_idx + linesize * my_idx;\n }\n mc_func[my_idx][mx_idx](dst, linesize, src, linesize, block_h, mx, my);\n } else\n mc_func[0][0](dst, linesize, src + y_off * linesize + x_off, linesize, block_h, 0, 0);\n}']
92
0
https://github.com/openssl/openssl/blob/71b1ceffc4c795f5db21861dd1016fbe23a53a53/crypto/evp/evp_enc.c/#L292
int is_partially_overlapping(const void *ptr1, const void *ptr2, int len) { PTRDIFF_T diff = (PTRDIFF_T)ptr1-(PTRDIFF_T)ptr2; int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) | (diff > (0 - (PTRDIFF_T)len))); return overlapped; }
['static int kek_unwrap_key(unsigned char *out, size_t *outlen,\n const unsigned char *in, size_t inlen,\n EVP_CIPHER_CTX *ctx)\n{\n size_t blocklen = EVP_CIPHER_CTX_block_size(ctx);\n unsigned char *tmp;\n int outl, rv = 0;\n if (inlen < 2 * blocklen) {\n return 0;\n }\n if (inlen % blocklen) {\n return 0;\n }\n if ((tmp = OPENSSL_malloc(inlen)) == NULL) {\n CMSerr(CMS_F_KEK_UNWRAP_KEY, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n if (!EVP_DecryptUpdate(ctx, tmp + inlen - 2 * blocklen, &outl,\n in + inlen - 2 * blocklen, blocklen * 2)\n || !EVP_DecryptUpdate(ctx, tmp, &outl,\n tmp + inlen - blocklen, blocklen)\n || !EVP_DecryptUpdate(ctx, tmp, &outl, in, inlen - blocklen)\n || !EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, NULL)\n || !EVP_DecryptUpdate(ctx, tmp, &outl, tmp, inlen))\n goto err;\n if (((tmp[1] ^ tmp[4]) & (tmp[2] ^ tmp[5]) & (tmp[3] ^ tmp[6])) != 0xff) {\n goto err;\n }\n if (inlen < (size_t)(tmp[0] - 4)) {\n goto err;\n }\n *outlen = (size_t)tmp[0];\n memcpy(out, tmp + 4, *outlen);\n rv = 1;\n err:\n OPENSSL_clear_free(tmp, inlen);\n return rv;\n}', 'int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,\n const unsigned char *in, int inl)\n{\n int fix_len, cmpl = inl;\n unsigned int b;\n if (ctx->encrypt) {\n EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_INVALID_OPERATION);\n return 0;\n }\n b = ctx->cipher->block_size;\n if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))\n cmpl = (cmpl + 7) / 8;\n if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {\n if (b == 1 && is_partially_overlapping(out, in, cmpl)) {\n EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);\n return 0;\n }\n fix_len = ctx->cipher->do_cipher(ctx, out, in, inl);\n if (fix_len < 0) {\n *outl = 0;\n return 0;\n } else\n *outl = fix_len;\n return 1;\n }\n if (inl <= 0) {\n *outl = 0;\n return inl == 0;\n }\n if (ctx->flags & EVP_CIPH_NO_PADDING)\n return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);\n OPENSSL_assert(b <= sizeof(ctx->final));\n if (ctx->final_used) {\n if (((PTRDIFF_T)out == (PTRDIFF_T)in)\n || is_partially_overlapping(out, in, b)) {\n EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);\n return 0;\n }\n memcpy(out, ctx->final, b);\n out += b;\n fix_len = 1;\n } else\n fix_len = 0;\n if (!evp_EncryptDecryptUpdate(ctx, out, outl, in, inl))\n return 0;\n if (b > 1 && !ctx->buf_len) {\n *outl -= b;\n ctx->final_used = 1;\n memcpy(ctx->final, &out[*outl], b);\n } else\n ctx->final_used = 0;\n if (fix_len)\n *outl += b;\n return 1;\n}', 'int is_partially_overlapping(const void *ptr1, const void *ptr2, int len)\n{\n PTRDIFF_T diff = (PTRDIFF_T)ptr1-(PTRDIFF_T)ptr2;\n int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) |\n (diff > (0 - (PTRDIFF_T)len)));\n return overlapped;\n}']
93
0
https://github.com/libav/libav/blob/8f935b9271052be8f97d655081b94b68b6c23bfb/libavformat/mpeg.c/#L313
static int mpegps_read_pes_header(AVFormatContext *s, int64_t *ppos, int *pstart_code, int64_t *ppts, int64_t *pdts) { MpegDemuxContext *m = s->priv_data; int len, size, startcode, c, flags, header_len; int pes_ext, ext2_len, id_ext, skip; int64_t pts, dts; int64_t last_sync= url_ftell(s->pb); error_redo: url_fseek(s->pb, last_sync, SEEK_SET); redo: m->header_state = 0xff; size = MAX_SYNC_SIZE; startcode = find_next_start_code(s->pb, &size, &m->header_state); last_sync = url_ftell(s->pb); if (startcode < 0){ if(url_feof(s->pb)) return AVERROR_EOF; return AVERROR(EAGAIN); } if (startcode == PACK_START_CODE) goto redo; if (startcode == SYSTEM_HEADER_START_CODE) goto redo; if (startcode == PADDING_STREAM) { url_fskip(s->pb, avio_rb16(s->pb)); goto redo; } if (startcode == PRIVATE_STREAM_2) { len = avio_rb16(s->pb); if (!m->sofdec) { while (len-- >= 6) { if (avio_r8(s->pb) == 'S') { uint8_t buf[5]; avio_read(s->pb, buf, sizeof(buf)); m->sofdec = !memcmp(buf, "ofdec", 5); len -= sizeof(buf); break; } } m->sofdec -= !m->sofdec; } url_fskip(s->pb, len); goto redo; } if (startcode == PROGRAM_STREAM_MAP) { mpegps_psm_parse(m, s->pb); goto redo; } if (!((startcode >= 0x1c0 && startcode <= 0x1df) || (startcode >= 0x1e0 && startcode <= 0x1ef) || (startcode == 0x1bd) || (startcode == 0x1fd))) goto redo; if (ppos) { *ppos = url_ftell(s->pb) - 4; } len = avio_rb16(s->pb); pts = dts = AV_NOPTS_VALUE; for(;;) { if (len < 1) goto error_redo; c = avio_r8(s->pb); len--; if (c != 0xff) break; } if ((c & 0xc0) == 0x40) { avio_r8(s->pb); c = avio_r8(s->pb); len -= 2; } if ((c & 0xe0) == 0x20) { dts = pts = get_pts(s->pb, c); len -= 4; if (c & 0x10){ dts = get_pts(s->pb, -1); len -= 5; } } else if ((c & 0xc0) == 0x80) { #if 0 if ((c & 0x30) != 0) { goto redo; } #endif flags = avio_r8(s->pb); header_len = avio_r8(s->pb); len -= 2; if (header_len > len) goto error_redo; len -= header_len; if (flags & 0x80) { dts = pts = get_pts(s->pb, -1); header_len -= 5; if (flags & 0x40) { dts = get_pts(s->pb, -1); header_len -= 5; } } if (flags & 0x3f && header_len == 0){ flags &= 0xC0; av_log(s, AV_LOG_WARNING, "Further flags set but no bytes left\n"); } if (flags & 0x01) { pes_ext = avio_r8(s->pb); header_len--; skip = (pes_ext >> 4) & 0xb; skip += skip & 0x9; if (pes_ext & 0x40 || skip > header_len){ av_log(s, AV_LOG_WARNING, "pes_ext %X is invalid\n", pes_ext); pes_ext=skip=0; } url_fskip(s->pb, skip); header_len -= skip; if (pes_ext & 0x01) { ext2_len = avio_r8(s->pb); header_len--; if ((ext2_len & 0x7f) > 0) { id_ext = avio_r8(s->pb); if ((id_ext & 0x80) == 0) startcode = ((startcode & 0xff) << 8) | id_ext; header_len--; } } } if(header_len < 0) goto error_redo; url_fskip(s->pb, header_len); } else if( c!= 0xf ) goto redo; if (startcode == PRIVATE_STREAM_1 && !m->psm_es_type[startcode & 0xff]) { startcode = avio_r8(s->pb); len--; if (startcode >= 0x80 && startcode <= 0xcf) { avio_r8(s->pb); avio_r8(s->pb); avio_r8(s->pb); len -= 3; if (startcode >= 0xb0 && startcode <= 0xbf) { avio_r8(s->pb); len--; } } } if(len<0) goto error_redo; if(dts != AV_NOPTS_VALUE && ppos){ int i; for(i=0; i<s->nb_streams; i++){ if(startcode == s->streams[i]->id && !url_is_streamed(s->pb) ) { ff_reduce_index(s, i); av_add_index_entry(s->streams[i], *ppos, dts, 0, 0, AVINDEX_KEYFRAME ); } } } *pstart_code = startcode; *ppts = pts; *pdts = dts; return len; }
['static int mpegps_read_pes_header(AVFormatContext *s,\n int64_t *ppos, int *pstart_code,\n int64_t *ppts, int64_t *pdts)\n{\n MpegDemuxContext *m = s->priv_data;\n int len, size, startcode, c, flags, header_len;\n int pes_ext, ext2_len, id_ext, skip;\n int64_t pts, dts;\n int64_t last_sync= url_ftell(s->pb);\n error_redo:\n url_fseek(s->pb, last_sync, SEEK_SET);\n redo:\n m->header_state = 0xff;\n size = MAX_SYNC_SIZE;\n startcode = find_next_start_code(s->pb, &size, &m->header_state);\n last_sync = url_ftell(s->pb);\n if (startcode < 0){\n if(url_feof(s->pb))\n return AVERROR_EOF;\n return AVERROR(EAGAIN);\n }\n if (startcode == PACK_START_CODE)\n goto redo;\n if (startcode == SYSTEM_HEADER_START_CODE)\n goto redo;\n if (startcode == PADDING_STREAM) {\n url_fskip(s->pb, avio_rb16(s->pb));\n goto redo;\n }\n if (startcode == PRIVATE_STREAM_2) {\n len = avio_rb16(s->pb);\n if (!m->sofdec) {\n while (len-- >= 6) {\n if (avio_r8(s->pb) == \'S\') {\n uint8_t buf[5];\n avio_read(s->pb, buf, sizeof(buf));\n m->sofdec = !memcmp(buf, "ofdec", 5);\n len -= sizeof(buf);\n break;\n }\n }\n m->sofdec -= !m->sofdec;\n }\n url_fskip(s->pb, len);\n goto redo;\n }\n if (startcode == PROGRAM_STREAM_MAP) {\n mpegps_psm_parse(m, s->pb);\n goto redo;\n }\n if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||\n (startcode >= 0x1e0 && startcode <= 0x1ef) ||\n (startcode == 0x1bd) || (startcode == 0x1fd)))\n goto redo;\n if (ppos) {\n *ppos = url_ftell(s->pb) - 4;\n }\n len = avio_rb16(s->pb);\n pts =\n dts = AV_NOPTS_VALUE;\n for(;;) {\n if (len < 1)\n goto error_redo;\n c = avio_r8(s->pb);\n len--;\n if (c != 0xff)\n break;\n }\n if ((c & 0xc0) == 0x40) {\n avio_r8(s->pb);\n c = avio_r8(s->pb);\n len -= 2;\n }\n if ((c & 0xe0) == 0x20) {\n dts = pts = get_pts(s->pb, c);\n len -= 4;\n if (c & 0x10){\n dts = get_pts(s->pb, -1);\n len -= 5;\n }\n } else if ((c & 0xc0) == 0x80) {\n#if 0\n if ((c & 0x30) != 0) {\n goto redo;\n }\n#endif\n flags = avio_r8(s->pb);\n header_len = avio_r8(s->pb);\n len -= 2;\n if (header_len > len)\n goto error_redo;\n len -= header_len;\n if (flags & 0x80) {\n dts = pts = get_pts(s->pb, -1);\n header_len -= 5;\n if (flags & 0x40) {\n dts = get_pts(s->pb, -1);\n header_len -= 5;\n }\n }\n if (flags & 0x3f && header_len == 0){\n flags &= 0xC0;\n av_log(s, AV_LOG_WARNING, "Further flags set but no bytes left\\n");\n }\n if (flags & 0x01) {\n pes_ext = avio_r8(s->pb);\n header_len--;\n skip = (pes_ext >> 4) & 0xb;\n skip += skip & 0x9;\n if (pes_ext & 0x40 || skip > header_len){\n av_log(s, AV_LOG_WARNING, "pes_ext %X is invalid\\n", pes_ext);\n pes_ext=skip=0;\n }\n url_fskip(s->pb, skip);\n header_len -= skip;\n if (pes_ext & 0x01) {\n ext2_len = avio_r8(s->pb);\n header_len--;\n if ((ext2_len & 0x7f) > 0) {\n id_ext = avio_r8(s->pb);\n if ((id_ext & 0x80) == 0)\n startcode = ((startcode & 0xff) << 8) | id_ext;\n header_len--;\n }\n }\n }\n if(header_len < 0)\n goto error_redo;\n url_fskip(s->pb, header_len);\n }\n else if( c!= 0xf )\n goto redo;\n if (startcode == PRIVATE_STREAM_1 && !m->psm_es_type[startcode & 0xff]) {\n startcode = avio_r8(s->pb);\n len--;\n if (startcode >= 0x80 && startcode <= 0xcf) {\n avio_r8(s->pb);\n avio_r8(s->pb);\n avio_r8(s->pb);\n len -= 3;\n if (startcode >= 0xb0 && startcode <= 0xbf) {\n avio_r8(s->pb);\n len--;\n }\n }\n }\n if(len<0)\n goto error_redo;\n if(dts != AV_NOPTS_VALUE && ppos){\n int i;\n for(i=0; i<s->nb_streams; i++){\n if(startcode == s->streams[i]->id &&\n !url_is_streamed(s->pb) ) {\n ff_reduce_index(s, i);\n av_add_index_entry(s->streams[i], *ppos, dts, 0, 0, AVINDEX_KEYFRAME );\n }\n }\n }\n *pstart_code = startcode;\n *ppts = pts;\n *pdts = dts;\n return len;\n}']
94
0
https://github.com/libav/libav/blob/4426540f0c3ee516662f79d0a6ab5b95503b6611/avconv.c/#L1473
static enum AVPixelFormat get_format(AVCodecContext *s, const enum AVPixelFormat *pix_fmts) { InputStream *ist = s->opaque; const enum AVPixelFormat *p; int ret; for (p = pix_fmts; *p != -1; p++) { const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(*p); const HWAccel *hwaccel; if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) break; hwaccel = get_hwaccel(*p); if (!hwaccel || (ist->active_hwaccel_id && ist->active_hwaccel_id != hwaccel->id) || (ist->hwaccel_id != HWACCEL_AUTO && ist->hwaccel_id != hwaccel->id)) continue; ret = hwaccel->init(s); if (ret < 0) { if (ist->hwaccel_id == hwaccel->id) { av_log(NULL, AV_LOG_FATAL, "%s hwaccel requested for input stream #%d:%d, " "but cannot be initialized.\n", hwaccel->name, ist->file_index, ist->st->index); return AV_PIX_FMT_NONE; } continue; } ist->active_hwaccel_id = hwaccel->id; ist->hwaccel_pix_fmt = *p; break; } return *p; }
['static enum AVPixelFormat get_format(AVCodecContext *s, const enum AVPixelFormat *pix_fmts)\n{\n InputStream *ist = s->opaque;\n const enum AVPixelFormat *p;\n int ret;\n for (p = pix_fmts; *p != -1; p++) {\n const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(*p);\n const HWAccel *hwaccel;\n if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))\n break;\n hwaccel = get_hwaccel(*p);\n if (!hwaccel ||\n (ist->active_hwaccel_id && ist->active_hwaccel_id != hwaccel->id) ||\n (ist->hwaccel_id != HWACCEL_AUTO && ist->hwaccel_id != hwaccel->id))\n continue;\n ret = hwaccel->init(s);\n if (ret < 0) {\n if (ist->hwaccel_id == hwaccel->id) {\n av_log(NULL, AV_LOG_FATAL,\n "%s hwaccel requested for input stream #%d:%d, "\n "but cannot be initialized.\\n", hwaccel->name,\n ist->file_index, ist->st->index);\n return AV_PIX_FMT_NONE;\n }\n continue;\n }\n ist->active_hwaccel_id = hwaccel->id;\n ist->hwaccel_pix_fmt = *p;\n break;\n }\n return *p;\n}', 'const AVPixFmtDescriptor *av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)\n{\n if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB)\n return NULL;\n return &av_pix_fmt_descriptors[pix_fmt];\n}']
95
0
https://github.com/libav/libav/blob/cb668476ab1343d27e03edc0b32f57ca7a187471/libavcodec/motion_est.c/#L136
static av_always_inline int cmp_direct_inline(MpegEncContext *s, const int x, const int y, const int subx, const int suby, const int size, const int h, int ref_index, int src_index, me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, int qpel){ MotionEstContext * const c= &s->me; const int stride= c->stride; const int hx= subx + (x<<(1+qpel)); const int hy= suby + (y<<(1+qpel)); uint8_t * const * const ref= c->ref[ref_index]; uint8_t * const * const src= c->src[src_index]; int d; assert(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1)); if(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1)){ const int time_pp= s->pp_time; const int time_pb= s->pb_time; const int mask= 2*qpel+1; if(s->mv_type==MV_TYPE_8X8){ int i; for(i=0; i<4; i++){ int fx = c->direct_basis_mv[i][0] + hx; int fy = c->direct_basis_mv[i][1] + hy; int bx = hx ? fx - c->co_located_mv[i][0] : c->co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(qpel+4)); int by = hy ? fy - c->co_located_mv[i][1] : c->co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(qpel+4)); int fxy= (fx&mask) + ((fy&mask)<<(qpel+1)); int bxy= (bx&mask) + ((by&mask)<<(qpel+1)); uint8_t *dst= c->temp + 8*(i&1) + 8*stride*(i>>1); if(qpel){ c->qpel_put[1][fxy](dst, ref[0] + (fx>>2) + (fy>>2)*stride, stride); c->qpel_avg[1][bxy](dst, ref[8] + (bx>>2) + (by>>2)*stride, stride); }else{ c->hpel_put[1][fxy](dst, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 8); c->hpel_avg[1][bxy](dst, ref[8] + (bx>>1) + (by>>1)*stride, stride, 8); } } }else{ int fx = c->direct_basis_mv[0][0] + hx; int fy = c->direct_basis_mv[0][1] + hy; int bx = hx ? fx - c->co_located_mv[0][0] : (c->co_located_mv[0][0]*(time_pb - time_pp)/time_pp); int by = hy ? fy - c->co_located_mv[0][1] : (c->co_located_mv[0][1]*(time_pb - time_pp)/time_pp); int fxy= (fx&mask) + ((fy&mask)<<(qpel+1)); int bxy= (bx&mask) + ((by&mask)<<(qpel+1)); if(qpel){ c->qpel_put[1][fxy](c->temp , ref[0] + (fx>>2) + (fy>>2)*stride , stride); c->qpel_put[1][fxy](c->temp + 8 , ref[0] + (fx>>2) + (fy>>2)*stride + 8 , stride); c->qpel_put[1][fxy](c->temp + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride + 8*stride, stride); c->qpel_put[1][fxy](c->temp + 8 + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride + 8 + 8*stride, stride); c->qpel_avg[1][bxy](c->temp , ref[8] + (bx>>2) + (by>>2)*stride , stride); c->qpel_avg[1][bxy](c->temp + 8 , ref[8] + (bx>>2) + (by>>2)*stride + 8 , stride); c->qpel_avg[1][bxy](c->temp + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride + 8*stride, stride); c->qpel_avg[1][bxy](c->temp + 8 + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride + 8 + 8*stride, stride); }else{ assert((fx>>1) + 16*s->mb_x >= -16); assert((fy>>1) + 16*s->mb_y >= -16); assert((fx>>1) + 16*s->mb_x <= s->width); assert((fy>>1) + 16*s->mb_y <= s->height); assert((bx>>1) + 16*s->mb_x >= -16); assert((by>>1) + 16*s->mb_y >= -16); assert((bx>>1) + 16*s->mb_x <= s->width); assert((by>>1) + 16*s->mb_y <= s->height); c->hpel_put[0][fxy](c->temp, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 16); c->hpel_avg[0][bxy](c->temp, ref[8] + (bx>>1) + (by>>1)*stride, stride, 16); } } d = cmp_func(s, c->temp, src[0], stride, 16); }else d= 256*256*256*32; return d; }
['void ff_estimate_b_frame_motion(MpegEncContext * s,\n int mb_x, int mb_y)\n{\n MotionEstContext * const c= &s->me;\n const int penalty_factor= c->mb_penalty_factor;\n int fmin, bmin, dmin, fbmin, bimin, fimin;\n int type=0;\n const int xy = mb_y*s->mb_stride + mb_x;\n init_ref(c, s->new_picture.f.data, s->last_picture.f.data,\n s->next_picture.f.data, 16 * mb_x, 16 * mb_y, 2);\n get_limits(s, 16*mb_x, 16*mb_y);\n c->skip=0;\n if (s->codec_id == CODEC_ID_MPEG4 && s->next_picture.f.mbskip_table[xy]) {\n int score= direct_search(s, mb_x, mb_y);\n score= ((unsigned)(score*score + 128*256))>>16;\n c->mc_mb_var_sum_temp += score;\n s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score;\n s->mb_type[mb_y*s->mb_stride + mb_x]= CANDIDATE_MB_TYPE_DIRECT0;\n return;\n }\n if(c->avctx->me_threshold){\n int vard= check_input_motion(s, mb_x, mb_y, 0);\n if((vard+128)>>8 < c->avctx->me_threshold){\n s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;\n c->mc_mb_var_sum_temp += (vard+128)>>8;\n return;\n }\n if((vard+128)>>8 < c->avctx->mb_threshold){\n type= s->mb_type[mb_y*s->mb_stride + mb_x];\n if(type == CANDIDATE_MB_TYPE_DIRECT){\n direct_search(s, mb_x, mb_y);\n }\n if(type == CANDIDATE_MB_TYPE_FORWARD || type == CANDIDATE_MB_TYPE_BIDIR){\n c->skip=0;\n ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code);\n }\n if(type == CANDIDATE_MB_TYPE_BACKWARD || type == CANDIDATE_MB_TYPE_BIDIR){\n c->skip=0;\n ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code);\n }\n if(type == CANDIDATE_MB_TYPE_FORWARD_I || type == CANDIDATE_MB_TYPE_BIDIR_I){\n c->skip=0;\n c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;\n interlaced_search(s, 0,\n s->b_field_mv_table[0], s->b_field_select_table[0],\n s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 1);\n }\n if(type == CANDIDATE_MB_TYPE_BACKWARD_I || type == CANDIDATE_MB_TYPE_BIDIR_I){\n c->skip=0;\n c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_MV;\n interlaced_search(s, 2,\n s->b_field_mv_table[1], s->b_field_select_table[1],\n s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 1);\n }\n return;\n }\n }\n if (s->codec_id == CODEC_ID_MPEG4)\n dmin= direct_search(s, mb_x, mb_y);\n else\n dmin= INT_MAX;\n c->skip=0;\n fmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code) + 3*penalty_factor;\n c->skip=0;\n bmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code) + 2*penalty_factor;\n c->skip=0;\n fbmin= bidir_refine(s, mb_x, mb_y) + penalty_factor;\n if(s->flags & CODEC_FLAG_INTERLACED_ME){\n c->skip=0;\n c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;\n fimin= interlaced_search(s, 0,\n s->b_field_mv_table[0], s->b_field_select_table[0],\n s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 0);\n c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_MV;\n bimin= interlaced_search(s, 2,\n s->b_field_mv_table[1], s->b_field_select_table[1],\n s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 0);\n }else\n fimin= bimin= INT_MAX;\n {\n int score= fmin;\n type = CANDIDATE_MB_TYPE_FORWARD;\n if (dmin <= score){\n score = dmin;\n type = CANDIDATE_MB_TYPE_DIRECT;\n }\n if(bmin<score){\n score=bmin;\n type= CANDIDATE_MB_TYPE_BACKWARD;\n }\n if(fbmin<score){\n score=fbmin;\n type= CANDIDATE_MB_TYPE_BIDIR;\n }\n if(fimin<score){\n score=fimin;\n type= CANDIDATE_MB_TYPE_FORWARD_I;\n }\n if(bimin<score){\n score=bimin;\n type= CANDIDATE_MB_TYPE_BACKWARD_I;\n }\n score= ((unsigned)(score*score + 128*256))>>16;\n c->mc_mb_var_sum_temp += score;\n s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score;\n }\n if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){\n type= CANDIDATE_MB_TYPE_FORWARD | CANDIDATE_MB_TYPE_BACKWARD | CANDIDATE_MB_TYPE_BIDIR | CANDIDATE_MB_TYPE_DIRECT;\n if(fimin < INT_MAX)\n type |= CANDIDATE_MB_TYPE_FORWARD_I;\n if(bimin < INT_MAX)\n type |= CANDIDATE_MB_TYPE_BACKWARD_I;\n if(fimin < INT_MAX && bimin < INT_MAX){\n type |= CANDIDATE_MB_TYPE_BIDIR_I;\n }\n if(dmin>256*256*16) type&= ~CANDIDATE_MB_TYPE_DIRECT;\n if(s->codec_id == CODEC_ID_MPEG4 && type&CANDIDATE_MB_TYPE_DIRECT && s->flags&CODEC_FLAG_MV0 && *(uint32_t*)s->b_direct_mv_table[xy])\n type |= CANDIDATE_MB_TYPE_DIRECT0;\n }\n s->mb_type[mb_y*s->mb_stride + mb_x]= type;\n}', 'static int ff_estimate_motion_b(MpegEncContext * s,\n int mb_x, int mb_y, int16_t (*mv_table)[2], int ref_index, int f_code)\n{\n MotionEstContext * const c= &s->me;\n int mx, my, dmin;\n int P[10][2];\n const int shift= 1+s->quarter_sample;\n const int mot_stride = s->mb_stride;\n const int mot_xy = mb_y*mot_stride + mb_x;\n uint8_t * const mv_penalty= c->mv_penalty[f_code] + MAX_MV;\n int mv_scale;\n c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);\n c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);\n c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);\n c->current_mv_penalty= mv_penalty;\n get_limits(s, 16*mb_x, 16*mb_y);\n switch(s->me_method) {\n case ME_ZERO:\n default:\n no_motion_search(s, &mx, &my);\n dmin = 0;\n mx-= mb_x*16;\n my-= mb_y*16;\n break;\n case ME_X1:\n case ME_EPZS:\n {\n P_LEFT[0] = mv_table[mot_xy - 1][0];\n P_LEFT[1] = mv_table[mot_xy - 1][1];\n if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);\n if (!s->first_slice_line) {\n P_TOP[0] = mv_table[mot_xy - mot_stride ][0];\n P_TOP[1] = mv_table[mot_xy - mot_stride ][1];\n P_TOPRIGHT[0] = mv_table[mot_xy - mot_stride + 1 ][0];\n P_TOPRIGHT[1] = mv_table[mot_xy - mot_stride + 1 ][1];\n if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1]= (c->ymax<<shift);\n if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);\n if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);\n P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);\n P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);\n }\n c->pred_x= P_LEFT[0];\n c->pred_y= P_LEFT[1];\n }\n if(mv_table == s->b_forw_mv_table){\n mv_scale= (s->pb_time<<16) / (s->pp_time<<shift);\n }else{\n mv_scale= ((s->pb_time - s->pp_time)<<16) / (s->pp_time<<shift);\n }\n dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, ref_index, s->p_mv_table, mv_scale, 0, 16);\n break;\n }\n dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, ref_index, 0, 16);\n if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)\n dmin= ff_get_mb_score(s, mx, my, 0, ref_index, 0, 16, 1);\n mv_table[mot_xy][0]= mx;\n mv_table[mot_xy][1]= my;\n return dmin;\n}', 'inline int ff_epzs_motion_search(MpegEncContext * s, int *mx_ptr, int *my_ptr,\n int P[10][2], int src_index, int ref_index, int16_t (*last_mv)[2],\n int ref_mv_scale, int size, int h)\n{\n MotionEstContext * const c= &s->me;\n if(c->flags==0 && h==16 && size==0){\n return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, 0, 0, 16);\n }else{\n return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, c->flags, size, h);\n }\n}', 'static av_always_inline int epzs_motion_search_internal(MpegEncContext * s, int *mx_ptr, int *my_ptr,\n int P[10][2], int src_index, int ref_index, int16_t (*last_mv)[2],\n int ref_mv_scale, int flags, int size, int h)\n{\n MotionEstContext * const c= &s->me;\n int best[2]={0, 0};\n int d;\n int dmin;\n unsigned map_generation;\n int penalty_factor;\n const int ref_mv_stride= s->mb_stride;\n const int ref_mv_xy= s->mb_x + s->mb_y*ref_mv_stride;\n me_cmp_func cmpf, chroma_cmpf;\n LOAD_COMMON\n LOAD_COMMON2\n if(c->pre_pass){\n penalty_factor= c->pre_penalty_factor;\n cmpf= s->dsp.me_pre_cmp[size];\n chroma_cmpf= s->dsp.me_pre_cmp[size+1];\n }else{\n penalty_factor= c->penalty_factor;\n cmpf= s->dsp.me_cmp[size];\n chroma_cmpf= s->dsp.me_cmp[size+1];\n }\n map_generation= update_map_generation(c);\n assert(cmpf);\n dmin= cmp(s, 0, 0, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);\n map[0]= map_generation;\n score_map[0]= dmin;\n if((s->pict_type == AV_PICTURE_TYPE_B && !(c->flags & FLAG_DIRECT)) || s->flags&CODEC_FLAG_MV0)\n dmin += (mv_penalty[pred_x] + mv_penalty[pred_y])*penalty_factor;\n if (s->first_slice_line) {\n CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)\n CHECK_CLIPPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,\n (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)\n }else{\n if(dmin<((h*h*s->avctx->mv0_threshold)>>8)\n && ( P_LEFT[0] |P_LEFT[1]\n |P_TOP[0] |P_TOP[1]\n |P_TOPRIGHT[0]|P_TOPRIGHT[1])==0){\n *mx_ptr= 0;\n *my_ptr= 0;\n c->skip=1;\n return dmin;\n }\n CHECK_MV( P_MEDIAN[0] >>shift , P_MEDIAN[1] >>shift)\n CHECK_CLIPPED_MV((P_MEDIAN[0]>>shift) , (P_MEDIAN[1]>>shift)-1)\n CHECK_CLIPPED_MV((P_MEDIAN[0]>>shift) , (P_MEDIAN[1]>>shift)+1)\n CHECK_CLIPPED_MV((P_MEDIAN[0]>>shift)-1, (P_MEDIAN[1]>>shift) )\n CHECK_CLIPPED_MV((P_MEDIAN[0]>>shift)+1, (P_MEDIAN[1]>>shift) )\n CHECK_CLIPPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,\n (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)\n CHECK_MV(P_LEFT[0] >>shift, P_LEFT[1] >>shift)\n CHECK_MV(P_TOP[0] >>shift, P_TOP[1] >>shift)\n CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)\n }\n if(dmin>h*h*4){\n if(c->pre_pass){\n CHECK_CLIPPED_MV((last_mv[ref_mv_xy-1][0]*ref_mv_scale + (1<<15))>>16,\n (last_mv[ref_mv_xy-1][1]*ref_mv_scale + (1<<15))>>16)\n if(!s->first_slice_line)\n CHECK_CLIPPED_MV((last_mv[ref_mv_xy-ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,\n (last_mv[ref_mv_xy-ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)\n }else{\n CHECK_CLIPPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16,\n (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)\n if(s->mb_y+1<s->end_mb_y)\n CHECK_CLIPPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,\n (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)\n }\n }\n if(c->avctx->last_predictor_count){\n const int count= c->avctx->last_predictor_count;\n const int xstart= FFMAX(0, s->mb_x - count);\n const int ystart= FFMAX(0, s->mb_y - count);\n const int xend= FFMIN(s->mb_width , s->mb_x + count + 1);\n const int yend= FFMIN(s->mb_height, s->mb_y + count + 1);\n int mb_y;\n for(mb_y=ystart; mb_y<yend; mb_y++){\n int mb_x;\n for(mb_x=xstart; mb_x<xend; mb_x++){\n const int xy= mb_x + 1 + (mb_y + 1)*ref_mv_stride;\n int mx= (last_mv[xy][0]*ref_mv_scale + (1<<15))>>16;\n int my= (last_mv[xy][1]*ref_mv_scale + (1<<15))>>16;\n if(mx>xmax || mx<xmin || my>ymax || my<ymin) continue;\n CHECK_MV(mx,my)\n }\n }\n }\n dmin= diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);\n *mx_ptr= best[0];\n *my_ptr= best[1];\n return dmin;\n}', 'static av_always_inline int diamond_search(MpegEncContext * s, int *best, int dmin,\n int src_index, int ref_index, int const penalty_factor,\n int size, int h, int flags){\n MotionEstContext * const c= &s->me;\n if(c->dia_size==-1)\n return funny_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);\n else if(c->dia_size<-1)\n return sab_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);\n else if(c->dia_size<2)\n return small_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);\n else if(c->dia_size>1024)\n return full_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);\n else if(c->dia_size>768)\n return umh_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);\n else if(c->dia_size>512)\n return hex_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags, c->dia_size&0xFF);\n else if(c->dia_size>256)\n return l2s_dia_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);\n else\n return var_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);\n}', 'static int l2s_dia_search(MpegEncContext * s, int *best, int dmin,\n int src_index, int ref_index, int const penalty_factor,\n int size, int h, int flags)\n{\n MotionEstContext * const c= &s->me;\n me_cmp_func cmpf, chroma_cmpf;\n LOAD_COMMON\n LOAD_COMMON2\n unsigned map_generation = c->map_generation;\n int x,y,i,d;\n int dia_size= c->dia_size&0xFF;\n const int dec= dia_size & (dia_size-1);\n static const int hex[8][2]={{-2, 0}, {-1,-1}, { 0,-2}, { 1,-1},\n { 2, 0}, { 1, 1}, { 0, 2}, {-1, 1}};\n cmpf= s->dsp.me_cmp[size];\n chroma_cmpf= s->dsp.me_cmp[size+1];\n for(; dia_size; dia_size= dec ? dia_size-1 : dia_size>>1){\n do{\n x= best[0];\n y= best[1];\n for(i=0; i<8; i++){\n CHECK_CLIPPED_MV(x+hex[i][0]*dia_size, y+hex[i][1]*dia_size);\n }\n }while(best[0] != x || best[1] != y);\n }\n x= best[0];\n y= best[1];\n CHECK_CLIPPED_MV(x+1, y);\n CHECK_CLIPPED_MV(x, y+1);\n CHECK_CLIPPED_MV(x-1, y);\n CHECK_CLIPPED_MV(x, y-1);\n return dmin;\n}', 'static av_always_inline int cmp(MpegEncContext *s, const int x, const int y, const int subx, const int suby,\n const int size, const int h, int ref_index, int src_index,\n me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){\n if(av_builtin_constant_p(flags) && av_builtin_constant_p(h) && av_builtin_constant_p(size)\n && av_builtin_constant_p(subx) && av_builtin_constant_p(suby)\n && flags==0 && h==16 && size==0 && subx==0 && suby==0){\n return cmp_simple(s,x,y,ref_index,src_index, cmp_func, chroma_cmp_func);\n }else if(av_builtin_constant_p(subx) && av_builtin_constant_p(suby)\n && subx==0 && suby==0){\n return cmp_fpel_internal(s,x,y,size,h,ref_index,src_index, cmp_func, chroma_cmp_func,flags);\n }else{\n return cmp_internal(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags);\n }\n}', 'static int cmp_fpel_internal(MpegEncContext *s, const int x, const int y,\n const int size, const int h, int ref_index, int src_index,\n me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){\n if(flags&FLAG_DIRECT){\n return cmp_direct_inline(s,x,y,0,0,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags&FLAG_QPEL);\n }else{\n return cmp_inline(s,x,y,0,0,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 0, flags&FLAG_CHROMA);\n }\n}', 'static av_always_inline int cmp_direct_inline(MpegEncContext *s, const int x, const int y, const int subx, const int suby,\n const int size, const int h, int ref_index, int src_index,\n me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, int qpel){\n MotionEstContext * const c= &s->me;\n const int stride= c->stride;\n const int hx= subx + (x<<(1+qpel));\n const int hy= suby + (y<<(1+qpel));\n uint8_t * const * const ref= c->ref[ref_index];\n uint8_t * const * const src= c->src[src_index];\n int d;\n assert(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1));\n if(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1)){\n const int time_pp= s->pp_time;\n const int time_pb= s->pb_time;\n const int mask= 2*qpel+1;\n if(s->mv_type==MV_TYPE_8X8){\n int i;\n for(i=0; i<4; i++){\n int fx = c->direct_basis_mv[i][0] + hx;\n int fy = c->direct_basis_mv[i][1] + hy;\n int bx = hx ? fx - c->co_located_mv[i][0] : c->co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(qpel+4));\n int by = hy ? fy - c->co_located_mv[i][1] : c->co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(qpel+4));\n int fxy= (fx&mask) + ((fy&mask)<<(qpel+1));\n int bxy= (bx&mask) + ((by&mask)<<(qpel+1));\n uint8_t *dst= c->temp + 8*(i&1) + 8*stride*(i>>1);\n if(qpel){\n c->qpel_put[1][fxy](dst, ref[0] + (fx>>2) + (fy>>2)*stride, stride);\n c->qpel_avg[1][bxy](dst, ref[8] + (bx>>2) + (by>>2)*stride, stride);\n }else{\n c->hpel_put[1][fxy](dst, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 8);\n c->hpel_avg[1][bxy](dst, ref[8] + (bx>>1) + (by>>1)*stride, stride, 8);\n }\n }\n }else{\n int fx = c->direct_basis_mv[0][0] + hx;\n int fy = c->direct_basis_mv[0][1] + hy;\n int bx = hx ? fx - c->co_located_mv[0][0] : (c->co_located_mv[0][0]*(time_pb - time_pp)/time_pp);\n int by = hy ? fy - c->co_located_mv[0][1] : (c->co_located_mv[0][1]*(time_pb - time_pp)/time_pp);\n int fxy= (fx&mask) + ((fy&mask)<<(qpel+1));\n int bxy= (bx&mask) + ((by&mask)<<(qpel+1));\n if(qpel){\n c->qpel_put[1][fxy](c->temp , ref[0] + (fx>>2) + (fy>>2)*stride , stride);\n c->qpel_put[1][fxy](c->temp + 8 , ref[0] + (fx>>2) + (fy>>2)*stride + 8 , stride);\n c->qpel_put[1][fxy](c->temp + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride + 8*stride, stride);\n c->qpel_put[1][fxy](c->temp + 8 + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride + 8 + 8*stride, stride);\n c->qpel_avg[1][bxy](c->temp , ref[8] + (bx>>2) + (by>>2)*stride , stride);\n c->qpel_avg[1][bxy](c->temp + 8 , ref[8] + (bx>>2) + (by>>2)*stride + 8 , stride);\n c->qpel_avg[1][bxy](c->temp + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride + 8*stride, stride);\n c->qpel_avg[1][bxy](c->temp + 8 + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride + 8 + 8*stride, stride);\n }else{\n assert((fx>>1) + 16*s->mb_x >= -16);\n assert((fy>>1) + 16*s->mb_y >= -16);\n assert((fx>>1) + 16*s->mb_x <= s->width);\n assert((fy>>1) + 16*s->mb_y <= s->height);\n assert((bx>>1) + 16*s->mb_x >= -16);\n assert((by>>1) + 16*s->mb_y >= -16);\n assert((bx>>1) + 16*s->mb_x <= s->width);\n assert((by>>1) + 16*s->mb_y <= s->height);\n c->hpel_put[0][fxy](c->temp, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 16);\n c->hpel_avg[0][bxy](c->temp, ref[8] + (bx>>1) + (by>>1)*stride, stride, 16);\n }\n }\n d = cmp_func(s, c->temp, src[0], stride, 16);\n }else\n d= 256*256*256*32;\n return d;\n}']
96
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 decode_scale_factors(WMAProDecodeCtx* s)\n{\n int i;\n for (i = 0; i < s->channels_for_cur_subframe; i++) {\n int c = s->channel_indexes_for_cur_subframe[i];\n int* sf;\n int* sf_end;\n s->channel[c].scale_factors = s->channel[c].saved_scale_factors[!s->channel[c].scale_factor_idx];\n sf_end = s->channel[c].scale_factors + s->num_bands;\n if (s->channel[c].reuse_sf) {\n const int8_t* sf_offsets = s->sf_offsets[s->table_idx][s->channel[c].table_idx];\n int b;\n for (b = 0; b < s->num_bands; b++)\n s->channel[c].scale_factors[b] =\n s->channel[c].saved_scale_factors[s->channel[c].scale_factor_idx][*sf_offsets++];\n }\n if (!s->channel[c].cur_subframe || bitstream_read_bit(&s->bc)) {\n if (!s->channel[c].reuse_sf) {\n int val;\n s->channel[c].scale_factor_step = bitstream_read(&s->bc, 2) + 1;\n val = 45 / s->channel[c].scale_factor_step;\n for (sf = s->channel[c].scale_factors; sf < sf_end; sf++) {\n val += bitstream_read_vlc(&s->bc, sf_vlc.table, SCALEVLCBITS, SCALEMAXDEPTH) - 60;\n *sf = val;\n }\n } else {\n int i;\n for (i = 0; i < s->num_bands; i++) {\n int idx;\n int skip;\n int val;\n int sign;\n idx = bitstream_read_vlc(&s->bc, sf_rl_vlc.table, VLCBITS, SCALERLMAXDEPTH);\n if (!idx) {\n uint32_t code = bitstream_read(&s->bc, 14);\n val = code >> 6;\n sign = (code & 1) - 1;\n skip = (code & 0x3f) >> 1;\n } else if (idx == 1) {\n break;\n } else {\n skip = scale_rl_run[idx];\n val = scale_rl_level[idx];\n sign = bitstream_read_bit(&s->bc)-1;\n }\n i += skip;\n if (i >= s->num_bands) {\n av_log(s->avctx, AV_LOG_ERROR,\n "invalid scale factor coding\\n");\n return AVERROR_INVALIDDATA;\n }\n s->channel[c].scale_factors[i] += (val ^ sign) - sign;\n }\n }\n s->channel[c].scale_factor_idx = !s->channel[c].scale_factor_idx;\n s->channel[c].table_idx = s->table_idx;\n s->channel[c].reuse_sf = 1;\n }\n s->channel[c].max_scale_factor = s->channel[c].scale_factors[0];\n for (sf = s->channel[c].scale_factors + 1; sf < sf_end; sf++) {\n s->channel[c].max_scale_factor =\n FFMAX(s->channel[c].max_scale_factor, *sf);\n }\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}']
97
0
https://github.com/libav/libav/blob/0266988ccd15436eaf5f7bb6f9509e6bfd5ce589/libavcodec/hqx.c/#L185
static void hqx_idct_put(uint16_t *dst, ptrdiff_t stride, int16_t *block, const uint8_t *quant) { int i, j; hqx_idct(block, quant); for (i = 0; i < 8; i++) { for (j = 0; j < 8; j++) { int v = av_clip(block[j + i * 8] + 0x800, 0, 0xFFF); dst[j] = (v << 4) | (v >> 8); } dst += stride >> 1; } }
['static int hqx_decode_422a(HQXContext *ctx, AVFrame *pic,\n GetBitContext *gb, int x, int y)\n{\n const int *quants;\n int flag = 0;\n int last_dc;\n int i, ret;\n int cbp;\n cbp = get_vlc2(gb, ctx->cbp_vlc.table, ctx->cbp_vlc.bits, 1);\n for (i = 0; i < 12; i++)\n memset(ctx->block[i], 0, sizeof(**ctx->block) * 64);\n for (i = 0; i < 12; i++)\n ctx->block[i][0] = -0x800;\n if (cbp) {\n if (ctx->interlaced)\n flag = get_bits1(gb);\n quants = hqx_quants[get_bits(gb, 4)];\n cbp |= cbp << 4;\n if (cbp & 0x3)\n cbp |= 0x500;\n if (cbp & 0xC)\n cbp |= 0xA00;\n for (i = 0; i < 12; i++) {\n if (i == 0 || i == 4 || i == 8 || i == 10)\n last_dc = 0;\n if (cbp & (1 << i)) {\n int vlc_index = ctx->dcb - 9;\n ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants,\n ctx->dcb, ctx->block[i], &last_dc);\n if (ret < 0)\n return ret;\n }\n }\n }\n put_blocks(pic, 3, x, y, flag, ctx->block[ 0], ctx->block[ 2], hqx_quant_luma);\n put_blocks(pic, 3, x + 8, y, flag, ctx->block[ 1], ctx->block[ 3], hqx_quant_luma);\n put_blocks(pic, 0, x, y, flag, ctx->block[ 4], ctx->block[ 6], hqx_quant_luma);\n put_blocks(pic, 0, x + 8, y, flag, ctx->block[ 5], ctx->block[ 7], hqx_quant_luma);\n put_blocks(pic, 2, x >> 1, y, flag, ctx->block[ 8], ctx->block[ 9], hqx_quant_chroma);\n put_blocks(pic, 1, x >> 1, y, flag, ctx->block[10], ctx->block[11], hqx_quant_chroma);\n return 0;\n}', 'static inline void put_blocks(AVFrame *pic, int plane,\n int x, int y, int ilace,\n int16_t *block0, int16_t *block1,\n const uint8_t *quant)\n{\n int fields = ilace ? 2 : 1;\n int lsize = pic->linesize[plane];\n uint8_t *p = pic->data[plane] + x * 2;\n hqx_idct_put((uint16_t *)(p + y * lsize), lsize * fields, block0, quant);\n hqx_idct_put((uint16_t *)(p + (y + (ilace ? 1 : 8)) * lsize),\n lsize * fields, block1, quant);\n}', 'static void hqx_idct_put(uint16_t *dst, ptrdiff_t stride,\n int16_t *block, const uint8_t *quant)\n{\n int i, j;\n hqx_idct(block, quant);\n for (i = 0; i < 8; i++) {\n for (j = 0; j < 8; j++) {\n int v = av_clip(block[j + i * 8] + 0x800, 0, 0xFFF);\n dst[j] = (v << 4) | (v >> 8);\n }\n dst += stride >> 1;\n }\n}']
98
0
https://github.com/openssl/openssl/blob/985de8634000df9b33b8ac4519fa10a99e43b314/crypto/x509/x509name.c/#L85
int X509_NAME_get_text_by_OBJ(X509_NAME *name, ASN1_OBJECT *obj, char *buf, int len) { int i; ASN1_STRING *data; i=X509_NAME_get_index_by_OBJ(name,obj,-1); if (i < 0) return(-1); data=X509_NAME_ENTRY_get_data(X509_NAME_get_entry(name,i)); i=(data->length > (len-1))?(len-1):data->length; if (buf == NULL) return(data->length); memcpy(buf,data->data,i); buf[i]='\0'; return(i); }
["int X509_NAME_get_text_by_OBJ(X509_NAME *name, ASN1_OBJECT *obj, char *buf,\n\t int len)\n\t{\n\tint i;\n\tASN1_STRING *data;\n\ti=X509_NAME_get_index_by_OBJ(name,obj,-1);\n\tif (i < 0) return(-1);\n\tdata=X509_NAME_ENTRY_get_data(X509_NAME_get_entry(name,i));\n\ti=(data->length > (len-1))?(len-1):data->length;\n\tif (buf == NULL) return(data->length);\n\tmemcpy(buf,data->data,i);\n\tbuf[i]='\\0';\n\treturn(i);\n\t}", 'X509_NAME_ENTRY *X509_NAME_get_entry(X509_NAME *name, int loc)\n\t{\n\tif(name == NULL || sk_X509_NAME_ENTRY_num(name->entries) <= loc\n\t || loc < 0)\n\t\treturn(NULL);\n\telse\n\t\treturn(sk_X509_NAME_ENTRY_value(name->entries,loc));\n\t}', 'int sk_num(const _STACK *st)\n{\n\tif(st == NULL) return -1;\n\treturn st->num;\n}', 'void *sk_value(const _STACK *st, int i)\n{\n\tif(!st || (i < 0) || (i >= st->num)) return NULL;\n\treturn st->data[i];\n}', 'ASN1_STRING *X509_NAME_ENTRY_get_data(X509_NAME_ENTRY *ne)\n\t{\n\tif (ne == NULL) return(NULL);\n\treturn(ne->value);\n\t}']
99
0
https://github.com/openssl/openssl/blob/9c46f4b9cd4912b61cb546c48b678488d7f26ed6/crypto/lhash/lhash.c/#L273
static void doall_util_fn(_LHASH *lh, int use_arg, LHASH_DOALL_FN_TYPE func, LHASH_DOALL_ARG_FN_TYPE func_arg, void *arg) { int i; LHASH_NODE *a, *n; 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; } } }
['int MAIN(int argc, char **argv)\n{\n int ret = 1, i;\n int verbose = 0, Verbose = 0;\n int use_supported = 0;\n#ifndef OPENSSL_NO_SSL_TRACE\n int stdname = 0;\n#endif\n const char **pp;\n const char *p;\n int badops = 0;\n SSL_CTX *ctx = NULL;\n SSL *ssl = NULL;\n char *ciphers = NULL;\n const SSL_METHOD *meth = NULL;\n STACK_OF(SSL_CIPHER) *sk = NULL;\n char buf[512];\n BIO *STDout = NULL;\n meth = SSLv23_server_method();\n apps_startup();\n if (bio_err == NULL)\n bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);\n STDout = BIO_new_fp(stdout, BIO_NOCLOSE);\n#ifdef OPENSSL_SYS_VMS\n {\n BIO *tmpbio = BIO_new(BIO_f_linebuffer());\n STDout = BIO_push(tmpbio, STDout);\n }\n#endif\n if (!load_config(bio_err, NULL))\n goto end;\n argc--;\n argv++;\n while (argc >= 1) {\n if (strcmp(*argv, "-v") == 0)\n verbose = 1;\n else if (strcmp(*argv, "-V") == 0)\n verbose = Verbose = 1;\n else if (strcmp(*argv, "-s") == 0)\n use_supported = 1;\n#ifndef OPENSSL_NO_SSL_TRACE\n else if (strcmp(*argv, "-stdname") == 0)\n stdname = verbose = 1;\n#endif\n#ifndef OPENSSL_NO_SSL3\n else if (strcmp(*argv, "-ssl3") == 0)\n meth = SSLv3_client_method();\n#endif\n#ifndef OPENSSL_NO_TLS1\n else if (strcmp(*argv, "-tls1") == 0)\n meth = TLSv1_client_method();\n#endif\n else if ((strncmp(*argv, "-h", 2) == 0) || (strcmp(*argv, "-?") == 0)) {\n badops = 1;\n break;\n } else {\n ciphers = *argv;\n }\n argc--;\n argv++;\n }\n if (badops) {\n for (pp = ciphers_usage; (*pp != NULL); pp++)\n BIO_printf(bio_err, "%s", *pp);\n goto end;\n }\n OpenSSL_add_ssl_algorithms();\n ctx = SSL_CTX_new(meth);\n if (ctx == NULL)\n goto err;\n if (ciphers != NULL) {\n if (!SSL_CTX_set_cipher_list(ctx, ciphers)) {\n BIO_printf(bio_err, "Error in cipher list\\n");\n goto err;\n }\n }\n ssl = SSL_new(ctx);\n if (ssl == NULL)\n goto err;\n if (use_supported)\n sk = SSL_get1_supported_ciphers(ssl);\n else\n sk = SSL_get_ciphers(ssl);\n if (!verbose) {\n for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {\n SSL_CIPHER *c = sk_SSL_CIPHER_value(sk, i);\n p = SSL_CIPHER_get_name(c);\n if (p == NULL)\n break;\n if (i != 0)\n BIO_printf(STDout, ":");\n BIO_printf(STDout, "%s", p);\n }\n BIO_printf(STDout, "\\n");\n } else {\n for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {\n SSL_CIPHER *c;\n c = sk_SSL_CIPHER_value(sk, i);\n if (Verbose) {\n unsigned long id = SSL_CIPHER_get_id(c);\n int id0 = (int)(id >> 24);\n int id1 = (int)((id >> 16) & 0xffL);\n int id2 = (int)((id >> 8) & 0xffL);\n int id3 = (int)(id & 0xffL);\n if ((id & 0xff000000L) == 0x03000000L) {\n BIO_printf(STDout, " 0x%02X,0x%02X - ", id2,\n id3);\n } else {\n BIO_printf(STDout, "0x%02X,0x%02X,0x%02X,0x%02X - ", id0,\n id1, id2, id3);\n }\n }\n#ifndef OPENSSL_NO_SSL_TRACE\n if (stdname) {\n const char *nm = SSL_CIPHER_standard_name(c);\n if (nm == NULL)\n nm = "UNKNOWN";\n BIO_printf(STDout, "%s - ", nm);\n }\n#endif\n BIO_puts(STDout, SSL_CIPHER_description(c, buf, sizeof buf));\n }\n }\n ret = 0;\n if (0) {\n err:\n SSL_load_error_strings();\n ERR_print_errors(bio_err);\n }\n end:\n if (use_supported && sk)\n sk_SSL_CIPHER_free(sk);\n if (ctx != NULL)\n SSL_CTX_free(ctx);\n if (ssl != NULL)\n SSL_free(ssl);\n if (STDout != NULL)\n BIO_free_all(STDout);\n apps_shutdown();\n OPENSSL_EXIT(ret);\n}', 'SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)\n{\n SSL_CTX *ret = NULL;\n if (meth == NULL) {\n SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_NULL_SSL_METHOD_PASSED);\n return (NULL);\n }\n if (FIPS_mode() && (meth->version < TLS1_VERSION)) {\n SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);\n return NULL;\n }\n if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {\n SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);\n goto err;\n }\n ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));\n if (ret == NULL)\n goto err;\n memset(ret, 0, sizeof(SSL_CTX));\n ret->method = meth;\n ret->cert_store = NULL;\n ret->session_cache_mode = SSL_SESS_CACHE_SERVER;\n ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;\n ret->session_cache_head = NULL;\n ret->session_cache_tail = NULL;\n ret->session_timeout = meth->get_timeout();\n ret->new_session_cb = 0;\n ret->remove_session_cb = 0;\n ret->get_session_cb = 0;\n ret->generate_session_id = 0;\n memset((char *)&ret->stats, 0, sizeof(ret->stats));\n ret->references = 1;\n ret->quiet_shutdown = 0;\n ret->info_callback = NULL;\n ret->app_verify_callback = 0;\n ret->app_verify_arg = NULL;\n ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;\n ret->read_ahead = 0;\n ret->msg_callback = 0;\n ret->msg_callback_arg = NULL;\n ret->verify_mode = SSL_VERIFY_NONE;\n#if 0\n ret->verify_depth = -1;\n#endif\n ret->sid_ctx_length = 0;\n ret->default_verify_callback = NULL;\n if ((ret->cert = ssl_cert_new()) == NULL)\n goto err;\n ret->default_passwd_callback = 0;\n ret->default_passwd_callback_userdata = NULL;\n ret->client_cert_cb = 0;\n ret->app_gen_cookie_cb = 0;\n ret->app_verify_cookie_cb = 0;\n ret->sessions = lh_SSL_SESSION_new();\n if (ret->sessions == NULL)\n goto err;\n ret->cert_store = X509_STORE_new();\n if (ret->cert_store == NULL)\n goto err;\n ssl_create_cipher_list(ret->method,\n &ret->cipher_list, &ret->cipher_list_by_id,\n SSL_DEFAULT_CIPHER_LIST, ret->cert);\n if (ret->cipher_list == NULL || sk_SSL_CIPHER_num(ret->cipher_list) <= 0) {\n SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_LIBRARY_HAS_NO_CIPHERS);\n goto err2;\n }\n ret->param = X509_VERIFY_PARAM_new();\n if (!ret->param)\n goto err;\n if ((ret->md5 = EVP_get_digestbyname("ssl3-md5")) == NULL) {\n SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES);\n goto err2;\n }\n if ((ret->sha1 = EVP_get_digestbyname("ssl3-sha1")) == NULL) {\n SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES);\n goto err2;\n }\n if ((ret->client_CA = sk_X509_NAME_new_null()) == NULL)\n goto err;\n CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);\n ret->extra_certs = NULL;\n if (!(meth->ssl3_enc->enc_flags & SSL_ENC_FLAG_DTLS))\n ret->comp_methods = SSL_COMP_get_compression_methods();\n ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;\n#ifndef OPENSSL_NO_TLSEXT\n ret->tlsext_servername_callback = 0;\n ret->tlsext_servername_arg = NULL;\n if ((RAND_pseudo_bytes(ret->tlsext_tick_key_name, 16) <= 0)\n || (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0)\n || (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0))\n ret->options |= SSL_OP_NO_TICKET;\n ret->tlsext_status_cb = 0;\n ret->tlsext_status_arg = NULL;\n# ifndef OPENSSL_NO_NEXTPROTONEG\n ret->next_protos_advertised_cb = 0;\n ret->next_proto_select_cb = 0;\n# endif\n#endif\n#ifndef OPENSSL_NO_PSK\n ret->psk_identity_hint = NULL;\n ret->psk_client_callback = NULL;\n ret->psk_server_callback = NULL;\n#endif\n#ifndef OPENSSL_NO_SRP\n SSL_CTX_SRP_CTX_init(ret);\n#endif\n#ifndef OPENSSL_NO_BUF_FREELISTS\n ret->freelist_max_len = SSL_MAX_BUF_FREELIST_LEN_DEFAULT;\n ret->rbuf_freelist = OPENSSL_malloc(sizeof(SSL3_BUF_FREELIST));\n if (!ret->rbuf_freelist)\n goto err;\n ret->rbuf_freelist->chunklen = 0;\n ret->rbuf_freelist->len = 0;\n ret->rbuf_freelist->head = NULL;\n ret->wbuf_freelist = OPENSSL_malloc(sizeof(SSL3_BUF_FREELIST));\n if (!ret->wbuf_freelist) {\n OPENSSL_free(ret->rbuf_freelist);\n goto err;\n }\n ret->wbuf_freelist->chunklen = 0;\n ret->wbuf_freelist->len = 0;\n ret->wbuf_freelist->head = NULL;\n#endif\n#ifndef OPENSSL_NO_ENGINE\n ret->client_cert_engine = NULL;\n# ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO\n# define eng_strx(x) #x\n# define eng_str(x) eng_strx(x)\n {\n ENGINE *eng;\n eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));\n if (!eng) {\n ERR_clear_error();\n ENGINE_load_builtin_engines();\n eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));\n }\n if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng))\n ERR_clear_error();\n }\n# endif\n#endif\n ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;\n return (ret);\n err:\n SSLerr(SSL_F_SSL_CTX_NEW, ERR_R_MALLOC_FAILURE);\n err2:\n if (ret != NULL)\n SSL_CTX_free(ret);\n return (NULL);\n}', '_LHASH *lh_new(LHASH_HASH_FN_TYPE h, LHASH_COMP_FN_TYPE c)\n{\n _LHASH *ret;\n int i;\n if ((ret = OPENSSL_malloc(sizeof(_LHASH))) == NULL)\n goto err0;\n if ((ret->b = OPENSSL_malloc(sizeof(LHASH_NODE *) * MIN_NODES)) == NULL)\n goto err1;\n for (i = 0; i < MIN_NODES; i++)\n ret->b[i] = NULL;\n ret->comp = ((c == NULL) ? (LHASH_COMP_FN_TYPE)strcmp : c);\n ret->hash = ((h == NULL) ? (LHASH_HASH_FN_TYPE)lh_strhash : h);\n ret->num_nodes = MIN_NODES / 2;\n ret->num_alloc_nodes = MIN_NODES;\n ret->p = 0;\n ret->pmax = MIN_NODES / 2;\n ret->up_load = UP_LOAD;\n ret->down_load = DOWN_LOAD;\n ret->num_items = 0;\n ret->num_expands = 0;\n ret->num_expand_reallocs = 0;\n ret->num_contracts = 0;\n ret->num_contract_reallocs = 0;\n ret->num_hash_calls = 0;\n ret->num_comp_calls = 0;\n ret->num_insert = 0;\n ret->num_replace = 0;\n ret->num_delete = 0;\n ret->num_no_delete = 0;\n ret->num_retrieve = 0;\n ret->num_retrieve_miss = 0;\n ret->num_hash_comps = 0;\n ret->error = 0;\n return (ret);\n err1:\n OPENSSL_free(ret);\n err0:\n return (NULL);\n}', 'void SSL_CTX_free(SSL_CTX *a)\n{\n int i;\n if (a == NULL)\n return;\n i = CRYPTO_add(&a->references, -1, CRYPTO_LOCK_SSL_CTX);\n#ifdef REF_PRINT\n REF_PRINT("SSL_CTX", a);\n#endif\n if (i > 0)\n return;\n#ifdef REF_CHECK\n if (i < 0) {\n fprintf(stderr, "SSL_CTX_free, bad reference count\\n");\n abort();\n }\n#endif\n if (a->param)\n X509_VERIFY_PARAM_free(a->param);\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 if (a->sessions != NULL)\n lh_SSL_SESSION_free(a->sessions);\n if (a->cert_store != NULL)\n X509_STORE_free(a->cert_store);\n if (a->cipher_list != NULL)\n sk_SSL_CIPHER_free(a->cipher_list);\n if (a->cipher_list_by_id != NULL)\n sk_SSL_CIPHER_free(a->cipher_list_by_id);\n if (a->cert != NULL)\n ssl_cert_free(a->cert);\n if (a->client_CA != NULL)\n sk_X509_NAME_pop_free(a->client_CA, X509_NAME_free);\n if (a->extra_certs != NULL)\n sk_X509_pop_free(a->extra_certs, X509_free);\n#if 0\n if (a->comp_methods != NULL)\n sk_SSL_COMP_pop_free(a->comp_methods, SSL_COMP_free);\n#else\n a->comp_methods = NULL;\n#endif\n#ifndef OPENSSL_NO_SRTP\n if (a->srtp_profiles)\n sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);\n#endif\n#ifndef OPENSSL_NO_PSK\n if (a->psk_identity_hint)\n OPENSSL_free(a->psk_identity_hint);\n#endif\n#ifndef OPENSSL_NO_SRP\n SSL_CTX_SRP_CTX_free(a);\n#endif\n#ifndef OPENSSL_NO_ENGINE\n if (a->client_cert_engine)\n ENGINE_finish(a->client_cert_engine);\n#endif\n#ifndef OPENSSL_NO_BUF_FREELISTS\n if (a->wbuf_freelist)\n ssl_buf_freelist_free(a->wbuf_freelist);\n if (a->rbuf_freelist)\n ssl_buf_freelist_free(a->rbuf_freelist);\n#endif\n#ifndef OPENSSL_NO_TLSEXT\n# ifndef OPENSSL_NO_EC\n if (a->tlsext_ecpointformatlist)\n OPENSSL_free(a->tlsext_ecpointformatlist);\n if (a->tlsext_ellipticcurvelist)\n OPENSSL_free(a->tlsext_ellipticcurvelist);\n# endif\n if (a->alpn_client_proto_list != NULL)\n OPENSSL_free(a->alpn_client_proto_list);\n#endif\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_w_lock(CRYPTO_LOCK_SSL_CTX);\n i = CHECKED_LHASH_OF(SSL_SESSION, tp.cache)->down_load;\n CHECKED_LHASH_OF(SSL_SESSION, tp.cache)->down_load = 0;\n lh_SSL_SESSION_doall_arg(tp.cache, LHASH_DOALL_ARG_FN(timeout),\n TIMEOUT_PARAM, &tp);\n CHECKED_LHASH_OF(SSL_SESSION, tp.cache)->down_load = i;\n CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n}', 'void lh_doall_arg(_LHASH *lh, LHASH_DOALL_ARG_FN_TYPE func, void *arg)\n{\n doall_util_fn(lh, 1, (LHASH_DOALL_FN_TYPE)0, func, arg);\n}', 'static void doall_util_fn(_LHASH *lh, int use_arg, LHASH_DOALL_FN_TYPE func,\n LHASH_DOALL_ARG_FN_TYPE func_arg, void *arg)\n{\n int i;\n LHASH_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}']
100
0
https://github.com/libav/libav/blob/2f99117f6ff24ce5be2abb9e014cb8b86c2aa0e0/libavcodec/bitstream.h/#L237
static inline void skip_remaining(BitstreamContext *bc, unsigned n) { #ifdef BITSTREAM_READER_LE bc->bits >>= n; #else bc->bits <<= n; #endif bc->bits_left -= n; }
['static int tscc2_decode_mb(TSCC2Context *c, int *q, int vlc_set,\n uint8_t *dst, int stride, int plane)\n{\n BitstreamContext *bc = &c->bc;\n int prev_dc, dc, nc, ac, bpos, val;\n int i, j, k, l;\n if (bitstream_read_bit(bc)) {\n if (bitstream_read_bit(bc)) {\n val = bitstream_read(bc, 8);\n for (i = 0; i < 8; i++, dst += stride)\n memset(dst, val, 16);\n } else {\n if (bitstream_bits_left(bc) < 16 * 8 * 8)\n return AVERROR_INVALIDDATA;\n for (i = 0; i < 8; i++) {\n for (j = 0; j < 16; j++)\n dst[j] = bitstream_read(bc, 8);\n dst += stride;\n }\n }\n return 0;\n }\n prev_dc = 0;\n for (j = 0; j < 2; j++) {\n for (k = 0; k < 4; k++) {\n if (!(j | k)) {\n dc = bitstream_read(bc, 8);\n } else {\n dc = bitstream_read_vlc(bc, c->dc_vlc.table, 9, 2);\n if (dc == -1)\n return AVERROR_INVALIDDATA;\n if (dc == 0x100)\n dc = bitstream_read(bc, 8);\n }\n dc = (dc + prev_dc) & 0xFF;\n prev_dc = dc;\n c->block[0] = dc;\n nc = bitstream_read_vlc(bc, c->nc_vlc[vlc_set].table, 9, 1);\n if (nc == -1)\n return AVERROR_INVALIDDATA;\n bpos = 1;\n memset(c->block + 1, 0, 15 * sizeof(*c->block));\n for (l = 0; l < nc; l++) {\n ac = bitstream_read_vlc(bc, c->ac_vlc[vlc_set].table, 9, 2);\n if (ac == -1)\n return AVERROR_INVALIDDATA;\n if (ac == 0x1000)\n ac = bitstream_read(bc, 12);\n bpos += ac & 0xF;\n if (bpos >= 16)\n return AVERROR_INVALIDDATA;\n val = sign_extend(ac >> 4, 8);\n c->block[ff_zigzag_scan[bpos++]] = val;\n }\n tscc2_idct4_put(c->block, q, dst + k * 4, stride);\n }\n dst += 4 * stride;\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 int bitstream_read_vlc(BitstreamContext *bc, VLC_TYPE (*table)[2],\n int bits, int max_depth)\n{\n int nb_bits;\n unsigned idx = bitstream_peek(bc, bits);\n int code = table[idx][0];\n int n = table[idx][1];\n if (max_depth > 1 && n < 0) {\n skip_remaining(bc, bits);\n code = set_idx(bc, code, &n, &nb_bits, table);\n if (max_depth > 2 && n < 0) {\n skip_remaining(bc, nb_bits);\n code = set_idx(bc, code, &n, &nb_bits, table);\n }\n }\n skip_remaining(bc, n);\n return code;\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}']