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
36,401
0
https://github.com/openssl/openssl/blob/260a16f33682a819414fcba6161708a5e6bdff50/crypto/lhash/lhash.c/#L200
static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg, OPENSSL_LH_DOALL_FUNC func, OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg) { int i; OPENSSL_LH_NODE *a, *n; if (lh == NULL) return; for (i = lh->num_nodes - 1; i >= 0; i--) { a = lh->b[i]; while (a != NULL) { n = a->next; if (use_arg) func_arg(a->data, arg); else func(a->data); a = n; } } }
['static int test_early_data_replay_int(int idx, int usecb, int confopt)\n{\n SSL_CTX *cctx = NULL, *sctx = NULL;\n SSL *clientssl = NULL, *serverssl = NULL;\n int testresult = 0;\n SSL_SESSION *sess = NULL;\n size_t readbytes, written;\n unsigned char buf[20];\n allow_ed_cb_called = 0;\n if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),\n TLS1_VERSION, 0, &sctx,\n &cctx, cert, privkey)))\n return 0;\n if (usecb > 0) {\n if (confopt == 0) {\n SSL_CTX_set_options(sctx, SSL_OP_NO_ANTI_REPLAY);\n } else {\n SSL_CONF_CTX *confctx = SSL_CONF_CTX_new();\n if (!TEST_ptr(confctx))\n goto end;\n SSL_CONF_CTX_set_flags(confctx, SSL_CONF_FLAG_FILE\n | SSL_CONF_FLAG_SERVER);\n SSL_CONF_CTX_set_ssl_ctx(confctx, sctx);\n if (!TEST_int_eq(SSL_CONF_cmd(confctx, "Options", "-AntiReplay"),\n 2)) {\n SSL_CONF_CTX_free(confctx);\n goto end;\n }\n SSL_CONF_CTX_free(confctx);\n }\n SSL_CTX_set_allow_early_data_cb(sctx, allow_early_data_cb, &usecb);\n }\n if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,\n &serverssl, &sess, idx)))\n goto end;\n if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))\n || !TEST_true(SSL_session_reused(clientssl)))\n goto end;\n SSL_shutdown(clientssl);\n SSL_shutdown(serverssl);\n SSL_free(serverssl);\n SSL_free(clientssl);\n serverssl = clientssl = NULL;\n if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,\n &clientssl, NULL, NULL))\n || !TEST_true(SSL_set_session(clientssl, sess)))\n goto end;\n if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),\n &written))\n || !TEST_size_t_eq(written, strlen(MSG1)))\n goto end;\n if (usecb <= 1) {\n if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),\n &readbytes),\n SSL_READ_EARLY_DATA_FINISH)\n || !TEST_int_eq(SSL_get_early_data_status(serverssl),\n SSL_EARLY_DATA_REJECTED))\n goto end;\n } else {\n if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),\n &readbytes),\n SSL_READ_EARLY_DATA_SUCCESS)\n || !TEST_mem_eq(MSG1, strlen(MSG1), buf, readbytes)\n || !TEST_int_gt(SSL_connect(clientssl), 0)\n || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),\n &readbytes),\n SSL_READ_EARLY_DATA_FINISH)\n || !TEST_int_eq(SSL_get_early_data_status(serverssl),\n SSL_EARLY_DATA_ACCEPTED))\n goto end;\n }\n if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))\n || !TEST_int_eq(SSL_session_reused(clientssl), (usecb > 0) ? 1 : 0)\n || !TEST_int_eq(allow_ed_cb_called, usecb > 0 ? 1 : 0))\n goto end;\n testresult = 1;\n end:\n SSL_SESSION_free(sess);\n SSL_SESSION_free(clientpsk);\n SSL_SESSION_free(serverpsk);\n clientpsk = serverpsk = NULL;\n SSL_free(serverssl);\n SSL_free(clientssl);\n SSL_CTX_free(sctx);\n SSL_CTX_free(cctx);\n return testresult;\n}', 'static int setupearly_data_test(SSL_CTX **cctx, SSL_CTX **sctx, SSL **clientssl,\n SSL **serverssl, SSL_SESSION **sess, int idx)\n{\n if (*sctx == NULL\n && !TEST_true(create_ssl_ctx_pair(TLS_server_method(),\n TLS_client_method(),\n TLS1_VERSION, 0,\n sctx, cctx, cert, privkey)))\n return 0;\n if (!TEST_true(SSL_CTX_set_max_early_data(*sctx, SSL3_RT_MAX_PLAIN_LENGTH)))\n return 0;\n if (idx == 1) {\n SSL_CTX_set_read_ahead(*cctx, 1);\n SSL_CTX_set_read_ahead(*sctx, 1);\n } else if (idx == 2) {\n SSL_CTX_set_psk_use_session_callback(*cctx, use_session_cb);\n SSL_CTX_set_psk_find_session_callback(*sctx, find_session_cb);\n use_session_cb_cnt = 0;\n find_session_cb_cnt = 0;\n srvid = pskid;\n }\n if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl, clientssl,\n NULL, NULL)))\n return 0;\n if (idx == 1\n && !TEST_true(SSL_set_tlsext_host_name(*clientssl, "localhost")))\n return 0;\n if (idx == 2) {\n clientpsk = create_a_psk(*clientssl);\n if (!TEST_ptr(clientpsk)\n || !TEST_true(SSL_SESSION_set_max_early_data(clientpsk,\n 0x100))\n || !TEST_true(SSL_SESSION_up_ref(clientpsk))) {\n SSL_SESSION_free(clientpsk);\n clientpsk = NULL;\n return 0;\n }\n serverpsk = clientpsk;\n if (sess != NULL) {\n if (!TEST_true(SSL_SESSION_up_ref(clientpsk))) {\n SSL_SESSION_free(clientpsk);\n SSL_SESSION_free(serverpsk);\n clientpsk = serverpsk = NULL;\n return 0;\n }\n *sess = clientpsk;\n }\n return 1;\n }\n if (sess == NULL)\n return 1;\n if (!TEST_true(create_ssl_connection(*serverssl, *clientssl,\n SSL_ERROR_NONE)))\n return 0;\n *sess = SSL_get1_session(*clientssl);\n SSL_shutdown(*clientssl);\n SSL_shutdown(*serverssl);\n SSL_free(*serverssl);\n SSL_free(*clientssl);\n *serverssl = *clientssl = NULL;\n if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl,\n clientssl, NULL, NULL))\n || !TEST_true(SSL_set_session(*clientssl, *sess)))\n return 0;\n return 1;\n}', 'void SSL_free(SSL *s)\n{\n int i;\n if (s == NULL)\n return;\n CRYPTO_DOWN_REF(&s->references, &i, s->lock);\n REF_PRINT_COUNT("SSL", s);\n if (i > 0)\n return;\n REF_ASSERT_ISNT(i < 0);\n X509_VERIFY_PARAM_free(s->param);\n dane_final(&s->dane);\n CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);\n RECORD_LAYER_release(&s->rlayer);\n ssl_free_wbio_buffer(s);\n BIO_free_all(s->wbio);\n s->wbio = NULL;\n BIO_free_all(s->rbio);\n s->rbio = NULL;\n BUF_MEM_free(s->init_buf);\n sk_SSL_CIPHER_free(s->cipher_list);\n sk_SSL_CIPHER_free(s->cipher_list_by_id);\n sk_SSL_CIPHER_free(s->tls13_ciphersuites);\n if (s->session != NULL) {\n ssl_clear_bad_session(s);\n SSL_SESSION_free(s->session);\n }\n SSL_SESSION_free(s->psksession);\n OPENSSL_free(s->psksession_id);\n clear_ciphers(s);\n ssl_cert_free(s->cert);\n OPENSSL_free(s->ext.hostname);\n SSL_CTX_free(s->session_ctx);\n#ifndef OPENSSL_NO_EC\n OPENSSL_free(s->ext.ecpointformats);\n OPENSSL_free(s->ext.supportedgroups);\n#endif\n sk_X509_EXTENSION_pop_free(s->ext.ocsp.exts, X509_EXTENSION_free);\n#ifndef OPENSSL_NO_OCSP\n sk_OCSP_RESPID_pop_free(s->ext.ocsp.ids, OCSP_RESPID_free);\n#endif\n#ifndef OPENSSL_NO_CT\n SCT_LIST_free(s->scts);\n OPENSSL_free(s->ext.scts);\n#endif\n OPENSSL_free(s->ext.ocsp.resp);\n OPENSSL_free(s->ext.alpn);\n OPENSSL_free(s->ext.tls13_cookie);\n OPENSSL_free(s->clienthello);\n OPENSSL_free(s->pha_context);\n EVP_MD_CTX_free(s->pha_dgst);\n sk_X509_NAME_pop_free(s->ca_names, X509_NAME_free);\n sk_X509_NAME_pop_free(s->client_ca_names, X509_NAME_free);\n sk_X509_pop_free(s->verified_chain, X509_free);\n if (s->method != NULL)\n s->method->ssl_free(s);\n SSL_CTX_free(s->ctx);\n ASYNC_WAIT_CTX_free(s->waitctx);\n#if !defined(OPENSSL_NO_NEXTPROTONEG)\n OPENSSL_free(s->ext.npn);\n#endif\n#ifndef OPENSSL_NO_SRTP\n sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);\n#endif\n CRYPTO_THREAD_lock_free(s->lock);\n OPENSSL_free(s);\n}', 'void SSL_CTX_free(SSL_CTX *a)\n{\n int i;\n if (a == NULL)\n return;\n CRYPTO_DOWN_REF(&a->references, &i, a->lock);\n REF_PRINT_COUNT("SSL_CTX", a);\n if (i > 0)\n return;\n REF_ASSERT_ISNT(i < 0);\n X509_VERIFY_PARAM_free(a->param);\n dane_ctx_final(&a->dane);\n if (a->sessions != NULL)\n SSL_CTX_flush_sessions(a, 0);\n CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);\n lh_SSL_SESSION_free(a->sessions);\n X509_STORE_free(a->cert_store);\n#ifndef OPENSSL_NO_CT\n CTLOG_STORE_free(a->ctlog_store);\n#endif\n sk_SSL_CIPHER_free(a->cipher_list);\n sk_SSL_CIPHER_free(a->cipher_list_by_id);\n sk_SSL_CIPHER_free(a->tls13_ciphersuites);\n ssl_cert_free(a->cert);\n sk_X509_NAME_pop_free(a->ca_names, X509_NAME_free);\n sk_X509_NAME_pop_free(a->client_ca_names, X509_NAME_free);\n sk_X509_pop_free(a->extra_certs, X509_free);\n a->comp_methods = NULL;\n#ifndef OPENSSL_NO_SRTP\n sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);\n#endif\n#ifndef OPENSSL_NO_SRP\n SSL_CTX_SRP_CTX_free(a);\n#endif\n#ifndef OPENSSL_NO_ENGINE\n ENGINE_finish(a->client_cert_engine);\n#endif\n#ifndef OPENSSL_NO_EC\n OPENSSL_free(a->ext.ecpointformats);\n OPENSSL_free(a->ext.supportedgroups);\n#endif\n OPENSSL_free(a->ext.alpn);\n OPENSSL_secure_free(a->ext.secure);\n CRYPTO_THREAD_lock_free(a->lock);\n OPENSSL_free(a);\n}', 'void SSL_CTX_flush_sessions(SSL_CTX *s, long t)\n{\n unsigned long i;\n TIMEOUT_PARAM tp;\n tp.ctx = s;\n tp.cache = s->sessions;\n if (tp.cache == NULL)\n return;\n tp.time = t;\n CRYPTO_THREAD_write_lock(s->lock);\n i = lh_SSL_SESSION_get_down_load(s->sessions);\n lh_SSL_SESSION_set_down_load(s->sessions, 0);\n lh_SSL_SESSION_doall_TIMEOUT_PARAM(tp.cache, timeout_cb, &tp);\n lh_SSL_SESSION_set_down_load(s->sessions, i);\n CRYPTO_THREAD_unlock(s->lock);\n}', 'IMPLEMENT_LHASH_DOALL_ARG(SSL_SESSION, TIMEOUT_PARAM)', 'void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)\n{\n doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);\n}', 'static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,\n OPENSSL_LH_DOALL_FUNC func,\n OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)\n{\n int i;\n OPENSSL_LH_NODE *a, *n;\n if (lh == NULL)\n return;\n for (i = lh->num_nodes - 1; i >= 0; i--) {\n a = lh->b[i];\n while (a != NULL) {\n n = a->next;\n if (use_arg)\n func_arg(a->data, arg);\n else\n func(a->data);\n a = n;\n }\n }\n}']
36,402
0
https://github.com/openssl/openssl/blob/1a50eedf2a1fbb1e0e009ad616d8be678e4c6340/crypto/ec/ec_mult.c/#L624
int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar, size_t num, const EC_POINT *points[], const BIGNUM *scalars[], BN_CTX *ctx) { const EC_POINT *generator = NULL; EC_POINT *tmp = NULL; size_t totalnum; size_t blocksize = 0, numblocks = 0; size_t pre_points_per_block = 0; size_t i, j; int k; int r_is_inverted = 0; int r_is_at_infinity = 1; size_t *wsize = NULL; signed char **wNAF = NULL; size_t *wNAF_len = NULL; size_t max_len = 0; size_t num_val; EC_POINT **val = NULL; EC_POINT **v; EC_POINT ***val_sub = NULL; const EC_PRE_COMP *pre_comp = NULL; int num_scalar = 0; int ret = 0; if (!BN_is_zero(group->order) && !BN_is_zero(group->cofactor)) { if ((scalar != NULL) && (num == 0)) { return ec_scalar_mul_ladder(group, r, scalar, NULL, ctx); } if ((scalar == NULL) && (num == 1)) { return ec_scalar_mul_ladder(group, r, scalars[0], points[0], ctx); } } if (scalar != NULL) { generator = EC_GROUP_get0_generator(group); if (generator == NULL) { ECerr(EC_F_EC_WNAF_MUL, EC_R_UNDEFINED_GENERATOR); goto err; } pre_comp = group->pre_comp.ec; if (pre_comp && pre_comp->numblocks && (EC_POINT_cmp(group, generator, pre_comp->points[0], ctx) == 0)) { blocksize = pre_comp->blocksize; numblocks = (BN_num_bits(scalar) / blocksize) + 1; if (numblocks > pre_comp->numblocks) numblocks = pre_comp->numblocks; pre_points_per_block = (size_t)1 << (pre_comp->w - 1); if (pre_comp->num != (pre_comp->numblocks * pre_points_per_block)) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR); goto err; } } else { pre_comp = NULL; numblocks = 1; num_scalar = 1; } } totalnum = num + numblocks; wsize = OPENSSL_malloc(totalnum * sizeof(wsize[0])); wNAF_len = OPENSSL_malloc(totalnum * sizeof(wNAF_len[0])); wNAF = OPENSSL_malloc((totalnum + 1) * sizeof(wNAF[0])); val_sub = OPENSSL_malloc(totalnum * sizeof(val_sub[0])); if (wNAF != NULL) wNAF[0] = NULL; if (wsize == NULL || wNAF_len == NULL || wNAF == NULL || val_sub == NULL) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE); goto err; } num_val = 0; for (i = 0; i < num + num_scalar; i++) { size_t bits; bits = i < num ? BN_num_bits(scalars[i]) : BN_num_bits(scalar); wsize[i] = EC_window_bits_for_scalar_size(bits); num_val += (size_t)1 << (wsize[i] - 1); wNAF[i + 1] = NULL; wNAF[i] = bn_compute_wNAF((i < num ? scalars[i] : scalar), wsize[i], &wNAF_len[i]); if (wNAF[i] == NULL) goto err; if (wNAF_len[i] > max_len) max_len = wNAF_len[i]; } if (numblocks) { if (pre_comp == NULL) { if (num_scalar != 1) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR); goto err; } } else { signed char *tmp_wNAF = NULL; size_t tmp_len = 0; if (num_scalar != 0) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR); goto err; } wsize[num] = pre_comp->w; tmp_wNAF = bn_compute_wNAF(scalar, wsize[num], &tmp_len); if (!tmp_wNAF) goto err; if (tmp_len <= max_len) { numblocks = 1; totalnum = num + 1; wNAF[num] = tmp_wNAF; wNAF[num + 1] = NULL; wNAF_len[num] = tmp_len; val_sub[num] = pre_comp->points; } else { signed char *pp; EC_POINT **tmp_points; if (tmp_len < numblocks * blocksize) { numblocks = (tmp_len + blocksize - 1) / blocksize; if (numblocks > pre_comp->numblocks) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR); OPENSSL_free(tmp_wNAF); goto err; } totalnum = num + numblocks; } pp = tmp_wNAF; tmp_points = pre_comp->points; for (i = num; i < totalnum; i++) { if (i < totalnum - 1) { wNAF_len[i] = blocksize; if (tmp_len < blocksize) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR); OPENSSL_free(tmp_wNAF); goto err; } tmp_len -= blocksize; } else wNAF_len[i] = tmp_len; wNAF[i + 1] = NULL; wNAF[i] = OPENSSL_malloc(wNAF_len[i]); if (wNAF[i] == NULL) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE); OPENSSL_free(tmp_wNAF); goto err; } memcpy(wNAF[i], pp, wNAF_len[i]); if (wNAF_len[i] > max_len) max_len = wNAF_len[i]; if (*tmp_points == NULL) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR); OPENSSL_free(tmp_wNAF); goto err; } val_sub[i] = tmp_points; tmp_points += pre_points_per_block; pp += blocksize; } OPENSSL_free(tmp_wNAF); } } } val = OPENSSL_malloc((num_val + 1) * sizeof(val[0])); if (val == NULL) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE); goto err; } val[num_val] = NULL; v = val; for (i = 0; i < num + num_scalar; i++) { val_sub[i] = v; for (j = 0; j < ((size_t)1 << (wsize[i] - 1)); j++) { *v = EC_POINT_new(group); if (*v == NULL) goto err; v++; } } if (!(v == val + num_val)) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR); goto err; } if ((tmp = EC_POINT_new(group)) == NULL) goto err; for (i = 0; i < num + num_scalar; i++) { if (i < num) { if (!EC_POINT_copy(val_sub[i][0], points[i])) goto err; } else { if (!EC_POINT_copy(val_sub[i][0], generator)) goto err; } if (wsize[i] > 1) { if (!EC_POINT_dbl(group, tmp, val_sub[i][0], ctx)) goto err; for (j = 1; j < ((size_t)1 << (wsize[i] - 1)); j++) { if (!EC_POINT_add (group, val_sub[i][j], val_sub[i][j - 1], tmp, ctx)) goto err; } } } if (!EC_POINTs_make_affine(group, num_val, val, ctx)) goto err; r_is_at_infinity = 1; for (k = max_len - 1; k >= 0; k--) { if (!r_is_at_infinity) { if (!EC_POINT_dbl(group, r, r, ctx)) goto err; } for (i = 0; i < totalnum; i++) { if (wNAF_len[i] > (size_t)k) { int digit = wNAF[i][k]; int is_neg; if (digit) { is_neg = digit < 0; if (is_neg) digit = -digit; if (is_neg != r_is_inverted) { if (!r_is_at_infinity) { if (!EC_POINT_invert(group, r, ctx)) goto err; } r_is_inverted = !r_is_inverted; } if (r_is_at_infinity) { if (!EC_POINT_copy(r, val_sub[i][digit >> 1])) goto err; r_is_at_infinity = 0; } else { if (!EC_POINT_add (group, r, r, val_sub[i][digit >> 1], ctx)) goto err; } } } } } if (r_is_at_infinity) { if (!EC_POINT_set_to_infinity(group, r)) goto err; } else { if (r_is_inverted) if (!EC_POINT_invert(group, r, ctx)) goto err; } ret = 1; err: EC_POINT_free(tmp); OPENSSL_free(wsize); OPENSSL_free(wNAF_len); if (wNAF != NULL) { signed char **w; for (w = wNAF; *w != NULL; w++) OPENSSL_free(*w); OPENSSL_free(wNAF); } if (val != NULL) { for (v = val; *v != NULL; v++) EC_POINT_clear_free(*v); OPENSSL_free(val); } OPENSSL_free(val_sub); return ret; }
['static int group_order_tests(EC_GROUP *group)\n{\n BIGNUM *n1 = NULL, *n2 = NULL, *order = NULL;\n EC_POINT *P = NULL, *Q = NULL, *R = NULL, *S = NULL;\n const EC_POINT *G = NULL;\n BN_CTX *ctx = NULL;\n int i = 0, r = 0;\n if (!TEST_ptr(n1 = BN_new())\n || !TEST_ptr(n2 = BN_new())\n || !TEST_ptr(order = BN_new())\n || !TEST_ptr(ctx = BN_CTX_new())\n || !TEST_ptr(G = EC_GROUP_get0_generator(group))\n || !TEST_ptr(P = EC_POINT_new(group))\n || !TEST_ptr(Q = EC_POINT_new(group))\n || !TEST_ptr(R = EC_POINT_new(group))\n || !TEST_ptr(S = EC_POINT_new(group)))\n goto err;\n if (!TEST_true(EC_GROUP_get_order(group, order, ctx))\n || !TEST_true(EC_POINT_mul(group, Q, order, NULL, NULL, ctx))\n || !TEST_true(EC_POINT_is_at_infinity(group, Q))\n || !TEST_true(EC_GROUP_precompute_mult(group, ctx))\n || !TEST_true(EC_POINT_mul(group, Q, order, NULL, NULL, ctx))\n || !TEST_true(EC_POINT_is_at_infinity(group, Q))\n || !TEST_true(EC_POINT_copy(P, G))\n || !TEST_true(BN_one(n1))\n || !TEST_true(EC_POINT_mul(group, Q, n1, NULL, NULL, ctx))\n || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx))\n || !TEST_true(BN_sub(n1, order, n1))\n || !TEST_true(EC_POINT_mul(group, Q, n1, NULL, NULL, ctx))\n || !TEST_true(EC_POINT_invert(group, Q, ctx))\n || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx)))\n goto err;\n for (i = 1; i <= 2; i++) {\n const BIGNUM *scalars[6];\n const EC_POINT *points[6];\n if (!TEST_true(BN_set_word(n1, i))\n || !TEST_true(EC_POINT_mul(group, P, n1, NULL, NULL, ctx))\n || (i == 1 && !TEST_int_eq(0, EC_POINT_cmp(group, P, G, ctx)))\n || !TEST_true(BN_one(n1))\n || !TEST_true(BN_sub(n1, n1, order))\n || !TEST_true(EC_POINT_mul(group, Q, NULL, P, n1, ctx))\n || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx))\n || !TEST_true(BN_add(n2, order, BN_value_one()))\n || !TEST_true(EC_POINT_mul(group, Q, NULL, P, n2, ctx))\n || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx))\n || !TEST_true(BN_mul(n2, n1, n2, ctx))\n || !TEST_true(EC_POINT_mul(group, Q, NULL, P, n2, ctx))\n || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx)))\n goto err;\n BN_set_negative(n2, 0);\n if (!TEST_true(EC_POINT_mul(group, Q, NULL, P, n2, ctx))\n || !TEST_true(EC_POINT_add(group, Q, Q, P, ctx))\n || !TEST_true(EC_POINT_is_at_infinity(group, Q))\n || !TEST_false(EC_POINT_is_at_infinity(group, P)))\n goto err;\n scalars[0] = scalars[1] = BN_value_one();\n points[0] = points[1] = P;\n if (!TEST_true(EC_POINTs_mul(group, R, NULL, 2, points, scalars, ctx))\n || !TEST_true(EC_POINT_dbl(group, S, points[0], ctx))\n || !TEST_int_eq(0, EC_POINT_cmp(group, R, S, ctx)))\n goto err;\n scalars[0] = n1;\n points[0] = Q;\n scalars[1] = n2;\n points[1] = P;\n scalars[2] = n1;\n points[2] = Q;\n scalars[3] = n2;\n points[3] = Q;\n scalars[4] = n1;\n points[4] = P;\n scalars[5] = n2;\n points[5] = Q;\n if (!TEST_true(EC_POINTs_mul(group, P, NULL, 6, points, scalars, ctx))\n || !TEST_true(EC_POINT_is_at_infinity(group, P)))\n goto err;\n }\n r = 1;\nerr:\n if (r == 0 && i != 0)\n TEST_info(i == 1 ? "allowing precomputation" :\n "without precomputation");\n EC_POINT_free(P);\n EC_POINT_free(Q);\n EC_POINT_free(R);\n EC_POINT_free(S);\n BN_free(n1);\n BN_free(n2);\n BN_free(order);\n BN_CTX_free(ctx);\n return r;\n}', 'int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)\n{\n if (group->meth->mul == 0)\n return ec_wNAF_precompute_mult(group, ctx);\n if (group->meth->precompute_mult != 0)\n return group->meth->precompute_mult(group, ctx);\n else\n return 1;\n}', 'int ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *ctx)\n{\n const EC_POINT *generator;\n EC_POINT *tmp_point = NULL, *base = NULL, **var;\n BN_CTX *new_ctx = NULL;\n const BIGNUM *order;\n size_t i, bits, w, pre_points_per_block, blocksize, numblocks, num;\n EC_POINT **points = NULL;\n EC_PRE_COMP *pre_comp;\n int ret = 0;\n EC_pre_comp_free(group);\n if ((pre_comp = ec_pre_comp_new(group)) == NULL)\n return 0;\n generator = EC_GROUP_get0_generator(group);\n if (generator == NULL) {\n ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, EC_R_UNDEFINED_GENERATOR);\n goto err;\n }\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n goto err;\n }\n BN_CTX_start(ctx);\n order = EC_GROUP_get0_order(group);\n if (order == NULL)\n goto err;\n if (BN_is_zero(order)) {\n ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, EC_R_UNKNOWN_ORDER);\n goto err;\n }\n bits = BN_num_bits(order);\n blocksize = 8;\n w = 4;\n if (EC_window_bits_for_scalar_size(bits) > w) {\n w = EC_window_bits_for_scalar_size(bits);\n }\n numblocks = (bits + blocksize - 1) / blocksize;\n pre_points_per_block = (size_t)1 << (w - 1);\n num = pre_points_per_block * numblocks;\n points = OPENSSL_malloc(sizeof(*points) * (num + 1));\n if (points == NULL) {\n ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n var = points;\n var[num] = NULL;\n for (i = 0; i < num; i++) {\n if ((var[i] = EC_POINT_new(group)) == NULL) {\n ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n }\n if ((tmp_point = EC_POINT_new(group)) == NULL\n || (base = EC_POINT_new(group)) == NULL) {\n ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (!EC_POINT_copy(base, generator))\n goto err;\n for (i = 0; i < numblocks; i++) {\n size_t j;\n if (!EC_POINT_dbl(group, tmp_point, base, ctx))\n goto err;\n if (!EC_POINT_copy(*var++, base))\n goto err;\n for (j = 1; j < pre_points_per_block; j++, var++) {\n if (!EC_POINT_add(group, *var, tmp_point, *(var - 1), ctx))\n goto err;\n }\n if (i < numblocks - 1) {\n size_t k;\n if (blocksize <= 2) {\n ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if (!EC_POINT_dbl(group, base, tmp_point, ctx))\n goto err;\n for (k = 2; k < blocksize; k++) {\n if (!EC_POINT_dbl(group, base, base, ctx))\n goto err;\n }\n }\n }\n if (!EC_POINTs_make_affine(group, num, points, ctx))\n goto err;\n pre_comp->group = group;\n pre_comp->blocksize = blocksize;\n pre_comp->numblocks = numblocks;\n pre_comp->w = w;\n pre_comp->points = points;\n points = NULL;\n pre_comp->num = num;\n SETPRECOMP(group, ec, pre_comp);\n pre_comp = NULL;\n ret = 1;\n err:\n if (ctx != NULL)\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n EC_ec_pre_comp_free(pre_comp);\n if (points) {\n EC_POINT **p;\n for (p = points; *p != NULL; p++)\n EC_POINT_free(*p);\n OPENSSL_free(points);\n }\n EC_POINT_free(tmp_point);\n EC_POINT_free(base);\n return ret;\n}', 'int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,\n const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)\n{\n const EC_POINT *points[1];\n const BIGNUM *scalars[1];\n points[0] = point;\n scalars[0] = p_scalar;\n return EC_POINTs_mul(group, r, g_scalar,\n (point != NULL\n && p_scalar != NULL), points, scalars, ctx);\n}', 'int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,\n size_t num, const EC_POINT *points[],\n const BIGNUM *scalars[], BN_CTX *ctx)\n{\n int ret = 0;\n size_t i = 0;\n BN_CTX *new_ctx = NULL;\n if ((scalar == NULL) && (num == 0)) {\n return EC_POINT_set_to_infinity(group, r);\n }\n if (!ec_point_is_compat(r, group)) {\n ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);\n return 0;\n }\n for (i = 0; i < num; i++) {\n if (!ec_point_is_compat(points[i], group)) {\n ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);\n return 0;\n }\n }\n if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL) {\n ECerr(EC_F_EC_POINTS_MUL, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n if (group->meth->mul != NULL)\n ret = group->meth->mul(group, r, scalar, num, points, scalars, ctx);\n else\n ret = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,\n size_t num, const EC_POINT *points[], const BIGNUM *scalars[],\n BN_CTX *ctx)\n{\n const EC_POINT *generator = NULL;\n EC_POINT *tmp = NULL;\n size_t totalnum;\n size_t blocksize = 0, numblocks = 0;\n size_t pre_points_per_block = 0;\n size_t i, j;\n int k;\n int r_is_inverted = 0;\n int r_is_at_infinity = 1;\n size_t *wsize = NULL;\n signed char **wNAF = NULL;\n size_t *wNAF_len = NULL;\n size_t max_len = 0;\n size_t num_val;\n EC_POINT **val = NULL;\n EC_POINT **v;\n EC_POINT ***val_sub = NULL;\n const EC_PRE_COMP *pre_comp = NULL;\n int num_scalar = 0;\n int ret = 0;\n if (!BN_is_zero(group->order) && !BN_is_zero(group->cofactor)) {\n if ((scalar != NULL) && (num == 0)) {\n return ec_scalar_mul_ladder(group, r, scalar, NULL, ctx);\n }\n if ((scalar == NULL) && (num == 1)) {\n return ec_scalar_mul_ladder(group, r, scalars[0], points[0], ctx);\n }\n }\n if (scalar != NULL) {\n generator = EC_GROUP_get0_generator(group);\n if (generator == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, EC_R_UNDEFINED_GENERATOR);\n goto err;\n }\n pre_comp = group->pre_comp.ec;\n if (pre_comp && pre_comp->numblocks\n && (EC_POINT_cmp(group, generator, pre_comp->points[0], ctx) ==\n 0)) {\n blocksize = pre_comp->blocksize;\n numblocks = (BN_num_bits(scalar) / blocksize) + 1;\n if (numblocks > pre_comp->numblocks)\n numblocks = pre_comp->numblocks;\n pre_points_per_block = (size_t)1 << (pre_comp->w - 1);\n if (pre_comp->num != (pre_comp->numblocks * pre_points_per_block)) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n } else {\n pre_comp = NULL;\n numblocks = 1;\n num_scalar = 1;\n }\n }\n totalnum = num + numblocks;\n wsize = OPENSSL_malloc(totalnum * sizeof(wsize[0]));\n wNAF_len = OPENSSL_malloc(totalnum * sizeof(wNAF_len[0]));\n wNAF = OPENSSL_malloc((totalnum + 1) * sizeof(wNAF[0]));\n val_sub = OPENSSL_malloc(totalnum * sizeof(val_sub[0]));\n if (wNAF != NULL)\n wNAF[0] = NULL;\n if (wsize == NULL || wNAF_len == NULL || wNAF == NULL || val_sub == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n num_val = 0;\n for (i = 0; i < num + num_scalar; i++) {\n size_t bits;\n bits = i < num ? BN_num_bits(scalars[i]) : BN_num_bits(scalar);\n wsize[i] = EC_window_bits_for_scalar_size(bits);\n num_val += (size_t)1 << (wsize[i] - 1);\n wNAF[i + 1] = NULL;\n wNAF[i] =\n bn_compute_wNAF((i < num ? scalars[i] : scalar), wsize[i],\n &wNAF_len[i]);\n if (wNAF[i] == NULL)\n goto err;\n if (wNAF_len[i] > max_len)\n max_len = wNAF_len[i];\n }\n if (numblocks) {\n if (pre_comp == NULL) {\n if (num_scalar != 1) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n } else {\n signed char *tmp_wNAF = NULL;\n size_t tmp_len = 0;\n if (num_scalar != 0) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n wsize[num] = pre_comp->w;\n tmp_wNAF = bn_compute_wNAF(scalar, wsize[num], &tmp_len);\n if (!tmp_wNAF)\n goto err;\n if (tmp_len <= max_len) {\n numblocks = 1;\n totalnum = num + 1;\n wNAF[num] = tmp_wNAF;\n wNAF[num + 1] = NULL;\n wNAF_len[num] = tmp_len;\n val_sub[num] = pre_comp->points;\n } else {\n signed char *pp;\n EC_POINT **tmp_points;\n if (tmp_len < numblocks * blocksize) {\n numblocks = (tmp_len + blocksize - 1) / blocksize;\n if (numblocks > pre_comp->numblocks) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n OPENSSL_free(tmp_wNAF);\n goto err;\n }\n totalnum = num + numblocks;\n }\n pp = tmp_wNAF;\n tmp_points = pre_comp->points;\n for (i = num; i < totalnum; i++) {\n if (i < totalnum - 1) {\n wNAF_len[i] = blocksize;\n if (tmp_len < blocksize) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n OPENSSL_free(tmp_wNAF);\n goto err;\n }\n tmp_len -= blocksize;\n } else\n wNAF_len[i] = tmp_len;\n wNAF[i + 1] = NULL;\n wNAF[i] = OPENSSL_malloc(wNAF_len[i]);\n if (wNAF[i] == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);\n OPENSSL_free(tmp_wNAF);\n goto err;\n }\n memcpy(wNAF[i], pp, wNAF_len[i]);\n if (wNAF_len[i] > max_len)\n max_len = wNAF_len[i];\n if (*tmp_points == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n OPENSSL_free(tmp_wNAF);\n goto err;\n }\n val_sub[i] = tmp_points;\n tmp_points += pre_points_per_block;\n pp += blocksize;\n }\n OPENSSL_free(tmp_wNAF);\n }\n }\n }\n val = OPENSSL_malloc((num_val + 1) * sizeof(val[0]));\n if (val == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n val[num_val] = NULL;\n v = val;\n for (i = 0; i < num + num_scalar; i++) {\n val_sub[i] = v;\n for (j = 0; j < ((size_t)1 << (wsize[i] - 1)); j++) {\n *v = EC_POINT_new(group);\n if (*v == NULL)\n goto err;\n v++;\n }\n }\n if (!(v == val + num_val)) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if ((tmp = EC_POINT_new(group)) == NULL)\n goto err;\n for (i = 0; i < num + num_scalar; i++) {\n if (i < num) {\n if (!EC_POINT_copy(val_sub[i][0], points[i]))\n goto err;\n } else {\n if (!EC_POINT_copy(val_sub[i][0], generator))\n goto err;\n }\n if (wsize[i] > 1) {\n if (!EC_POINT_dbl(group, tmp, val_sub[i][0], ctx))\n goto err;\n for (j = 1; j < ((size_t)1 << (wsize[i] - 1)); j++) {\n if (!EC_POINT_add\n (group, val_sub[i][j], val_sub[i][j - 1], tmp, ctx))\n goto err;\n }\n }\n }\n if (!EC_POINTs_make_affine(group, num_val, val, ctx))\n goto err;\n r_is_at_infinity = 1;\n for (k = max_len - 1; k >= 0; k--) {\n if (!r_is_at_infinity) {\n if (!EC_POINT_dbl(group, r, r, ctx))\n goto err;\n }\n for (i = 0; i < totalnum; i++) {\n if (wNAF_len[i] > (size_t)k) {\n int digit = wNAF[i][k];\n int is_neg;\n if (digit) {\n is_neg = digit < 0;\n if (is_neg)\n digit = -digit;\n if (is_neg != r_is_inverted) {\n if (!r_is_at_infinity) {\n if (!EC_POINT_invert(group, r, ctx))\n goto err;\n }\n r_is_inverted = !r_is_inverted;\n }\n if (r_is_at_infinity) {\n if (!EC_POINT_copy(r, val_sub[i][digit >> 1]))\n goto err;\n r_is_at_infinity = 0;\n } else {\n if (!EC_POINT_add\n (group, r, r, val_sub[i][digit >> 1], ctx))\n goto err;\n }\n }\n }\n }\n }\n if (r_is_at_infinity) {\n if (!EC_POINT_set_to_infinity(group, r))\n goto err;\n } else {\n if (r_is_inverted)\n if (!EC_POINT_invert(group, r, ctx))\n goto err;\n }\n ret = 1;\n err:\n EC_POINT_free(tmp);\n OPENSSL_free(wsize);\n OPENSSL_free(wNAF_len);\n if (wNAF != NULL) {\n signed char **w;\n for (w = wNAF; *w != NULL; w++)\n OPENSSL_free(*w);\n OPENSSL_free(wNAF);\n }\n if (val != NULL) {\n for (v = val; *v != NULL; v++)\n EC_POINT_clear_free(*v);\n OPENSSL_free(val);\n }\n OPENSSL_free(val_sub);\n return ret;\n}']
36,403
0
https://github.com/libav/libav/blob/834259528b6cf593bf9544e3183b84b9b7880641/libavcodec/golomb.h/#L282
static inline int get_ur_golomb(GetBitContext *gb, int k, int limit, int esc_len) { unsigned int buf; int log; OPEN_READER(re, gb); UPDATE_CACHE(re, gb); buf = GET_CACHE(re, gb); log = av_log2(buf); if (log > 31 - limit) { buf >>= log - k; buf += (30 - log) << k; LAST_SKIP_BITS(re, gb, 32 + k - log); CLOSE_READER(re, gb); return buf; } else { LAST_SKIP_BITS(re, gb, limit); UPDATE_CACHE(re, gb); buf = SHOW_UBITS(re, gb, esc_len); LAST_SKIP_BITS(re, gb, esc_len); CLOSE_READER(re, gb); return buf + limit - 1; } }
['static inline int get_ur_golomb(GetBitContext *gb, int k, int limit,\n int esc_len)\n{\n unsigned int buf;\n int log;\n OPEN_READER(re, gb);\n UPDATE_CACHE(re, gb);\n buf = GET_CACHE(re, gb);\n log = av_log2(buf);\n if (log > 31 - limit) {\n buf >>= log - k;\n buf += (30 - log) << k;\n LAST_SKIP_BITS(re, gb, 32 + k - log);\n CLOSE_READER(re, gb);\n return buf;\n } else {\n LAST_SKIP_BITS(re, gb, limit);\n UPDATE_CACHE(re, gb);\n buf = SHOW_UBITS(re, gb, esc_len);\n LAST_SKIP_BITS(re, gb, esc_len);\n CLOSE_READER(re, gb);\n return buf + limit - 1;\n }\n}']
36,404
0
https://github.com/openssl/openssl/blob/305b68f1a2b6d4d0aa07a6ab47ac372f067a40bb/crypto/bn/bn_lib.c/#L290
BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b) { bn_check_top(b); if (a == b) return a; if (bn_wexpand(a, b->top) == NULL) return NULL; if (b->top > 0) memcpy(a->d, b->d, sizeof(b->d[0]) * b->top); a->neg = b->neg; a->top = b->top; a->flags |= b->flags & BN_FLG_FIXED_TOP; bn_check_top(a); return a; }
['make_dh(1024_160)', 'BIGNUM *BN_dup(const BIGNUM *a)\n{\n BIGNUM *t;\n if (a == NULL)\n return NULL;\n bn_check_top(a);\n t = BN_get_flags(a, BN_FLG_SECURE) ? BN_secure_new() : BN_new();\n if (t == NULL)\n return NULL;\n if (!BN_copy(t, a)) {\n BN_free(t);\n return NULL;\n }\n bn_check_top(t);\n return t;\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n bn_check_top(b);\n if (a == b)\n return a;\n if (bn_wexpand(a, b->top) == NULL)\n return NULL;\n if (b->top > 0)\n memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n a->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}']
36,405
0
https://github.com/openssl/openssl/blob/23dd0c9f8dc6f7edf4b872d13e5644dfbbee585b/crypto/evp/m_sigver.c/#L157
int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig, size_t siglen) { unsigned char md[EVP_MAX_MD_SIZE]; int r = 0; unsigned int mdlen = 0; int vctx = 0; if (ctx->pctx->pmeth->verifyctx) vctx = 1; else vctx = 0; if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) { if (vctx) { r = ctx->pctx->pmeth->verifyctx(ctx->pctx, sig, siglen, ctx); } else r = EVP_DigestFinal_ex(ctx, md, &mdlen); } else { EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new(); if (tmp_ctx == NULL || !EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) return -1; if (vctx) { r = tmp_ctx->pctx->pmeth->verifyctx(tmp_ctx->pctx, sig, siglen, tmp_ctx); } else r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen); EVP_MD_CTX_free(tmp_ctx); } if (vctx || !r) return r; return EVP_PKEY_verify(ctx->pctx, sig, siglen, md, mdlen); }
['int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig,\n size_t siglen)\n{\n unsigned char md[EVP_MAX_MD_SIZE];\n int r = 0;\n unsigned int mdlen = 0;\n int vctx = 0;\n if (ctx->pctx->pmeth->verifyctx)\n vctx = 1;\n else\n vctx = 0;\n if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {\n if (vctx) {\n r = ctx->pctx->pmeth->verifyctx(ctx->pctx, sig, siglen, ctx);\n } else\n r = EVP_DigestFinal_ex(ctx, md, &mdlen);\n } else {\n EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();\n if (tmp_ctx == NULL || !EVP_MD_CTX_copy_ex(tmp_ctx, ctx))\n return -1;\n if (vctx) {\n r = tmp_ctx->pctx->pmeth->verifyctx(tmp_ctx->pctx,\n sig, siglen, tmp_ctx);\n } else\n r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);\n EVP_MD_CTX_free(tmp_ctx);\n }\n if (vctx || !r)\n return r;\n return EVP_PKEY_verify(ctx->pctx, sig, siglen, md, mdlen);\n}', 'EVP_MD_CTX *EVP_MD_CTX_new(void)\n{\n return OPENSSL_zalloc(sizeof(EVP_MD_CTX));\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num <= 0)\n return NULL;\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n osslargused(file); osslargused(line);\n ret = malloc(num);\n#endif\n return ret;\n}', 'int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)\n{\n unsigned char *tmp_buf;\n if ((in == NULL) || (in->digest == NULL)) {\n EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_INPUT_NOT_INITIALIZED);\n return 0;\n }\n#ifndef OPENSSL_NO_ENGINE\n if (in->engine && !ENGINE_init(in->engine)) {\n EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_ENGINE_LIB);\n return 0;\n }\n#endif\n if (out->digest == in->digest) {\n tmp_buf = out->md_data;\n EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);\n } else\n tmp_buf = NULL;\n EVP_MD_CTX_reset(out);\n memcpy(out, in, sizeof(*out));\n out->md_data = NULL;\n out->pctx = NULL;\n if (in->md_data && out->digest->ctx_size) {\n if (tmp_buf)\n out->md_data = tmp_buf;\n else {\n out->md_data = OPENSSL_malloc(out->digest->ctx_size);\n if (out->md_data == NULL) {\n EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n }\n memcpy(out->md_data, in->md_data, out->digest->ctx_size);\n }\n out->update = in->update;\n if (in->pctx) {\n out->pctx = EVP_PKEY_CTX_dup(in->pctx);\n if (!out->pctx) {\n EVP_MD_CTX_reset(out);\n return 0;\n }\n }\n if (out->digest->copy)\n return out->digest->copy(out, in);\n return 1;\n}', 'int ENGINE_init(ENGINE *e)\n{\n int ret;\n if (e == NULL) {\n ENGINEerr(ENGINE_F_ENGINE_INIT, ERR_R_PASSED_NULL_PARAMETER);\n return 0;\n }\n if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {\n ENGINEerr(ENGINE_F_ENGINE_INIT, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n CRYPTO_THREAD_write_lock(global_engine_lock);\n ret = engine_unlocked_init(e);\n CRYPTO_THREAD_unlock(global_engine_lock);\n return ret;\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}']
36,406
0
https://github.com/openssl/openssl/blob/1fac96e4d6484a517f2ebe99b72016726391723c/crypto/bn/bn_lib.c/#L448
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 DSA_is_prime(BIGNUM *w, void (*callback)(), char *cb_arg)\n\t{\n\tint ok= -1,j,i,n;\n\tBN_CTX *ctx=NULL,*ctx2=NULL;\n\tBIGNUM *w_1,*b,*m,*z,*tmp,*mont_1;\n\tint a;\n\tBN_MONT_CTX *mont=NULL;\n\tif (!BN_is_bit_set(w,0)) return(0);\n\tif ((ctx=BN_CTX_new()) == NULL) goto err;\n\tif ((ctx2=BN_CTX_new()) == NULL) goto err;\n\tif ((mont=BN_MONT_CTX_new()) == NULL) goto err;\n\tm= &(ctx2->bn[2]);\n\tb= &(ctx2->bn[3]);\n\tz= &(ctx2->bn[4]);\n\tw_1= &(ctx2->bn[5]);\n\ttmp= &(ctx2->bn[6]);\n\tmont_1= &(ctx2->bn[7]);\n\tn=50;\n\tif (!BN_sub(w_1,w,BN_value_one())) goto err;\n\tfor (a=1; !BN_is_bit_set(w_1,a); a++)\n\t\t;\n\tif (!BN_rshift(m,w_1,a)) goto err;\n\tBN_MONT_CTX_set(mont,w,ctx);\n\tBN_to_montgomery(mont_1,BN_value_one(),mont,ctx);\n\tBN_to_montgomery(w_1,w_1,mont,ctx);\n\tfor (i=1; i < n; i++)\n\t\t{\n\t\tBN_rand(b,BN_num_bits(w)-2 ,0,0);\n\t\tj=0;\n\t\tif (!BN_mod_exp_mont(z,b,m,w,ctx,mont)) goto err;\n\t\tif (!BN_to_montgomery(z,z,mont,ctx)) goto err;\n\t\tfor (;;)\n\t\t\t{\n\t\t\tif (((j == 0) && (BN_cmp(z,mont_1) == 0)) ||\n\t\t\t\t(BN_cmp(z,w_1) == 0))\n\t\t\t\tbreak;\n\t\t\tif ((j > 0) && (BN_cmp(z,mont_1) == 0))\n\t\t\t\t{\n\t\t\t\tok=0;\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tj++;\n\t\t\tif (j >= a)\n\t\t\t\t{\n\t\t\t\tok=0;\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif (!BN_mod_mul_montgomery(z,z,z,mont,ctx)) goto err;\n\t\t\tif (callback != NULL) callback(1,j,cb_arg);\n\t\t\t}\n\t\t}\n\tok=1;\nerr:\n\tif (ok == -1) DSAerr(DSA_F_DSA_IS_PRIME,ERR_R_BN_LIB);\n\tBN_CTX_free(ctx);\n\tBN_CTX_free(ctx2);\n\treturn(ok);\n\t}', 'BIGNUM *BN_value_one(void)\n\t{\n\tstatic BN_ULONG data_one=1L;\n\tstatic BIGNUM const_one={&data_one,1,1,0};\n\treturn(&const_one);\n\t}', 'int BN_mod_mul_montgomery(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_MONT_CTX *mont,\n\t BN_CTX *ctx)\n\t{\n\tBIGNUM *tmp,*tmp2;\n tmp= &(ctx->bn[ctx->tos]);\n tmp2= &(ctx->bn[ctx->tos]);\n\tctx->tos+=2;\n\tbn_check_top(tmp);\n\tbn_check_top(tmp2);\n\tif (a == b)\n\t\t{\n#if 0\n\t\tbn_wexpand(tmp,a->top*2);\n\t\tbn_wexpand(tmp2,a->top*4);\n\t\tbn_sqr_recursive(tmp->d,a->d,a->top,tmp2->d);\n\t\ttmp->top=a->top*2;\n\t\tif (tmp->d[tmp->top-1] == 0)\n\t\t\ttmp->top--;\n#else\n\t\tif (!BN_sqr(tmp,a,ctx)) goto err;\n#endif\n\t\t}\n\telse\n\t\t{\n\t\tif (!BN_mul(tmp,a,b,ctx)) goto err;\n\t\t}\n\tif (!BN_from_montgomery(r,tmp,mont,ctx)) goto err;\n\tctx->tos-=2;\n\treturn(1);\nerr:\n\treturn(0);\n\t}', 'int BN_sqr(BIGNUM *r, BIGNUM *a, BN_CTX *ctx)\n\t{\n\tint max,al;\n\tBIGNUM *tmp,*rr;\n#ifdef BN_COUNT\nprintf("BN_sqr %d * %d\\n",a->top,a->top);\n#endif\n\tbn_check_top(a);\n\ttmp= &(ctx->bn[ctx->tos]);\n\trr=(a != r)?r: (&ctx->bn[ctx->tos+1]);\n\tal=a->top;\n\tif (al <= 0)\n\t\t{\n\t\tr->top=0;\n\t\treturn(1);\n\t\t}\n\tmax=(al+al);\n\tif (bn_wexpand(rr,max+1) == NULL) return(0);\n\tr->neg=0;\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(a,k*2) == NULL) return(0);\n\t\t\t\tif (bn_wexpand(tmp,k*2) == NULL) return(0);\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) return(0);\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) return(0);\n\t\tbn_sqr_normal(rr->d,a->d,al,tmp->d);\n#endif\n\t\t}\n\trr->top=max;\n\tif ((max > 0) && (rr->d[max-1] == 0)) rr->top--;\n\tif (rr != r) BN_copy(r,rr);\n\treturn(1);\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}']
36,407
0
https://github.com/openssl/openssl/blob/55525742f4c2bf416013fc3a75ec642775d97f80/crypto/bn/bn_ctx.c/#L353
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['int 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}', '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}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n\t{\n\tif (!(BN_mod(r,m,d,ctx)))\n\t\treturn 0;\n\tif (!r->neg)\n\t\treturn 1;\n\treturn (d->neg ? BN_sub : BN_add)(r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n\t BN_CTX *ctx)\n\t{\n\tint norm_shift,i,loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tint num_n,div_n;\n\tif ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0))\n\t\t{\n\t\treturn BN_div_no_branch(dv, rm, num, divisor, ctx);\n\t\t}\n\tbn_check_top(dv);\n\tbn_check_top(rm);\n\tbn_check_top(num);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\n\t\t}\n\tif (BN_ucmp(num,divisor) < 0)\n\t\t{\n\t\tif (rm != NULL)\n\t\t\t{ if (BN_copy(rm,num) == NULL) return(0); }\n\t\tif (dv != NULL) BN_zero(dv);\n\t\treturn(1);\n\t\t}\n\tBN_CTX_start(ctx);\n\ttmp=BN_CTX_get(ctx);\n\tsnum=BN_CTX_get(ctx);\n\tsdiv=BN_CTX_get(ctx);\n\tif (dv == NULL)\n\t\tres=BN_CTX_get(ctx);\n\telse\tres=dv;\n\tif (sdiv == NULL || res == NULL) goto err;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tif (!(BN_lshift(sdiv,divisor,norm_shift))) goto err;\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tif (!(BN_lshift(snum,num,norm_shift))) goto err;\n\tsnum->neg=0;\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\twnum.neg = 0;\n\twnum.d = &(snum->d[loop]);\n\twnum.top = div_n;\n\twnum.dmax = snum->dmax - loop;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (BN_ucmp(&wnum,sdiv) >= 0)\n\t\t{\n\t\tbn_clear_top2max(&wnum);\n\t\tbn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n\t\t*resp=1;\n\t\t}\n\telse\n\t\tres->top--;\n\tif (res->top == 0)\n\t\tres->neg = 0;\n\telse\n\t\tresp--;\n\tfor (i=0; i<loop-1; i++, wnump--, resp--)\n\t\t{\n\t\tBN_ULONG q,l0;\n#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n\t\tBN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG);\n\t\tq=bn_div_3_words(wnump,d1,d0);\n#else\n\t\tBN_ULONG n0,n1,rem=0;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\t{\n#ifdef BN_LLONG\n\t\t\tBN_ULLONG t2;\n#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n\t\t\tq=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);\n#else\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tt2 -= d1;\n\t\t\t\t}\n#else\n\t\t\tBN_ULONG t2l,t2h,ql,qh;\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#if defined(BN_UMULT_LOHI)\n\t\t\tBN_UMULT_LOHI(t2l,t2h,d1,q);\n#elif defined(BN_UMULT_HIGH)\n\t\t\tt2l = d1 * q;\n\t\t\tt2h = BN_UMULT_HIGH(d1,q);\n#else\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n#endif\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif ((t2h < rem) ||\n\t\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t\t}\n#endif\n\t\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\twnum.d--;\n\t\tif (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1))\n\t\t\t{\n\t\t\tq--;\n\t\t\tif (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n\t\t\t\t(*wnump)++;\n\t\t\t}\n\t\t*resp = q;\n\t\t}\n\tbn_correct_top(snum);\n\tif (rm != NULL)\n\t\t{\n\t\tint neg = num->neg;\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\tif (!BN_is_zero(rm))\n\t\t\trm->neg = neg;\n\t\tbn_check_top(rm);\n\t\t}\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'static int BN_div_no_branch(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,\n\tconst BIGNUM *divisor, BN_CTX *ctx)\n\t{\n\tint norm_shift,i,loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tint num_n,div_n;\n\tbn_check_top(dv);\n\tbn_check_top(rm);\n\tbn_check_top(num);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV_NO_BRANCH,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\n\t\t}\n\tBN_CTX_start(ctx);\n\ttmp=BN_CTX_get(ctx);\n\tsnum=BN_CTX_get(ctx);\n\tsdiv=BN_CTX_get(ctx);\n\tif (dv == NULL)\n\t\tres=BN_CTX_get(ctx);\n\telse\tres=dv;\n\tif (sdiv == NULL || res == NULL) goto err;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tif (!(BN_lshift(sdiv,divisor,norm_shift))) goto err;\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tif (!(BN_lshift(snum,num,norm_shift))) goto err;\n\tsnum->neg=0;\n\tif (snum->top <= sdiv->top+1)\n\t\t{\n\t\tif (bn_wexpand(snum, sdiv->top + 2) == NULL) goto err;\n\t\tfor (i = snum->top; i < sdiv->top + 2; i++) snum->d[i] = 0;\n\t\tsnum->top = sdiv->top + 2;\n\t\t}\n\telse\n\t\t{\n\t\tif (bn_wexpand(snum, snum->top + 1) == NULL) goto err;\n\t\tsnum->d[snum->top] = 0;\n\t\tsnum->top ++;\n\t\t}\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\twnum.neg = 0;\n\twnum.d = &(snum->d[loop]);\n\twnum.top = div_n;\n\twnum.dmax = snum->dmax - loop;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop-1;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (res->top == 0)\n\t\tres->neg = 0;\n\telse\n\t\tresp--;\n\tfor (i=0; i<loop-1; i++, wnump--, resp--)\n\t\t{\n\t\tBN_ULONG q,l0;\n#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n\t\tBN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG);\n\t\tq=bn_div_3_words(wnump,d1,d0);\n#else\n\t\tBN_ULONG n0,n1,rem=0;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\t{\n#ifdef BN_LLONG\n\t\t\tBN_ULLONG t2;\n#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n\t\t\tq=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);\n#else\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tt2 -= d1;\n\t\t\t\t}\n#else\n\t\t\tBN_ULONG t2l,t2h,ql,qh;\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#if defined(BN_UMULT_LOHI)\n\t\t\tBN_UMULT_LOHI(t2l,t2h,d1,q);\n#elif defined(BN_UMULT_HIGH)\n\t\t\tt2l = d1 * q;\n\t\t\tt2h = BN_UMULT_HIGH(d1,q);\n#else\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n#endif\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif ((t2h < rem) ||\n\t\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t\t}\n#endif\n\t\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\twnum.d--;\n\t\tif (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1))\n\t\t\t{\n\t\t\tq--;\n\t\t\tif (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n\t\t\t\t(*wnump)++;\n\t\t\t}\n\t\t*resp = q;\n\t\t}\n\tbn_correct_top(snum);\n\tif (rm != NULL)\n\t\t{\n\t\tint neg = num->neg;\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\tif (!BN_is_zero(rm))\n\t\t\trm->neg = neg;\n\t\tbn_check_top(rm);\n\t\t}\n\tbn_correct_top(res);\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'void BN_CTX_end(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_end", ctx);\n\tif(ctx->err_stack)\n\t\tctx->err_stack--;\n\telse\n\t\t{\n\t\tunsigned int fp = BN_STACK_pop(&ctx->stack);\n\t\tif(fp < ctx->used)\n\t\t\tBN_POOL_release(&ctx->pool, ctx->used - fp);\n\t\tctx->used = fp;\n\t\tctx->too_many = 0;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n\t{\n\treturn st->indexes[--(st->depth)];\n\t}']
36,408
0
https://github.com/openssl/openssl/blob/8b0d4242404f9e5da26e7594fa0864b2df4601af/crypto/bn/bn_lib.c/#L271
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *a = NULL; bn_check_top(b); if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return (NULL); } if (BN_get_flags(b, BN_FLG_SECURE)) a = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = OPENSSL_zalloc(words * sizeof(*a)); if (a == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return (NULL); } assert(b->top <= words); if (b->top > 0) memcpy(a, b->d, sizeof(*a) * b->top); return a; }
['int ec_GFp_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,\n const EC_POINT *b, BN_CTX *ctx)\n{\n int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *,\n const BIGNUM *, BN_CTX *);\n int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);\n const BIGNUM *p;\n BN_CTX *new_ctx = NULL;\n BIGNUM *n0, *n1, *n2, *n3, *n4, *n5, *n6;\n int ret = 0;\n if (a == b)\n return EC_POINT_dbl(group, r, a, ctx);\n if (EC_POINT_is_at_infinity(group, a))\n return EC_POINT_copy(r, b);\n if (EC_POINT_is_at_infinity(group, b))\n return EC_POINT_copy(r, a);\n field_mul = group->meth->field_mul;\n field_sqr = group->meth->field_sqr;\n p = group->field;\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n BN_CTX_start(ctx);\n n0 = BN_CTX_get(ctx);\n n1 = BN_CTX_get(ctx);\n n2 = BN_CTX_get(ctx);\n n3 = BN_CTX_get(ctx);\n n4 = BN_CTX_get(ctx);\n n5 = BN_CTX_get(ctx);\n n6 = BN_CTX_get(ctx);\n if (n6 == NULL)\n goto end;\n if (b->Z_is_one) {\n if (!BN_copy(n1, a->X))\n goto end;\n if (!BN_copy(n2, a->Y))\n goto end;\n } else {\n if (!field_sqr(group, n0, b->Z, ctx))\n goto end;\n if (!field_mul(group, n1, a->X, n0, ctx))\n goto end;\n if (!field_mul(group, n0, n0, b->Z, ctx))\n goto end;\n if (!field_mul(group, n2, a->Y, n0, ctx))\n goto end;\n }\n if (a->Z_is_one) {\n if (!BN_copy(n3, b->X))\n goto end;\n if (!BN_copy(n4, b->Y))\n goto end;\n } else {\n if (!field_sqr(group, n0, a->Z, ctx))\n goto end;\n if (!field_mul(group, n3, b->X, n0, ctx))\n goto end;\n if (!field_mul(group, n0, n0, a->Z, ctx))\n goto end;\n if (!field_mul(group, n4, b->Y, n0, ctx))\n goto end;\n }\n if (!BN_mod_sub_quick(n5, n1, n3, p))\n goto end;\n if (!BN_mod_sub_quick(n6, n2, n4, p))\n goto end;\n if (BN_is_zero(n5)) {\n if (BN_is_zero(n6)) {\n BN_CTX_end(ctx);\n ret = EC_POINT_dbl(group, r, a, ctx);\n ctx = NULL;\n goto end;\n } else {\n BN_zero(r->Z);\n r->Z_is_one = 0;\n ret = 1;\n goto end;\n }\n }\n if (!BN_mod_add_quick(n1, n1, n3, p))\n goto end;\n if (!BN_mod_add_quick(n2, n2, n4, p))\n goto end;\n if (a->Z_is_one && b->Z_is_one) {\n if (!BN_copy(r->Z, n5))\n goto end;\n } else {\n if (a->Z_is_one) {\n if (!BN_copy(n0, b->Z))\n goto end;\n } else if (b->Z_is_one) {\n if (!BN_copy(n0, a->Z))\n goto end;\n } else {\n if (!field_mul(group, n0, a->Z, b->Z, ctx))\n goto end;\n }\n if (!field_mul(group, r->Z, n0, n5, ctx))\n goto end;\n }\n r->Z_is_one = 0;\n if (!field_sqr(group, n0, n6, ctx))\n goto end;\n if (!field_sqr(group, n4, n5, ctx))\n goto end;\n if (!field_mul(group, n3, n1, n4, ctx))\n goto end;\n if (!BN_mod_sub_quick(r->X, n0, n3, p))\n goto end;\n if (!BN_mod_lshift1_quick(n0, r->X, p))\n goto end;\n if (!BN_mod_sub_quick(n0, n3, n0, p))\n goto end;\n if (!field_mul(group, n0, n0, n6, ctx))\n goto end;\n if (!field_mul(group, n5, n4, n5, ctx))\n goto end;\n if (!field_mul(group, n1, n2, n5, ctx))\n goto end;\n if (!BN_mod_sub_quick(n0, n0, n1, p))\n goto end;\n if (BN_is_odd(n0))\n if (!BN_add(n0, n0, p))\n goto end;\n if (!BN_rshift1(r->Y, n0))\n goto end;\n ret = 1;\n end:\n if (ctx)\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n bn_check_top(b);\n if (a == b)\n return a;\n if (bn_wexpand(a, b->top) == NULL)\n return NULL;\n if (b->top > 0)\n memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n a->top = b->top;\n a->neg = b->neg;\n bn_check_top(a);\n return a;\n}', 'int BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n const BIGNUM *m)\n{\n if (!BN_sub(r, a, b))\n return 0;\n if (r->neg)\n return BN_add(r, r, m);\n return 1;\n}', 'int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int max;\n int add = 0, neg = 0;\n bn_check_top(a);\n bn_check_top(b);\n if (a->neg) {\n if (b->neg) {\n const BIGNUM *tmp;\n tmp = a;\n a = b;\n b = tmp;\n } else {\n add = 1;\n neg = 1;\n }\n } else {\n if (b->neg) {\n add = 1;\n neg = 0;\n }\n }\n if (add) {\n if (!BN_uadd(r, a, b))\n return 0;\n r->neg = neg;\n return 1;\n }\n max = (a->top > b->top) ? a->top : b->top;\n if (bn_wexpand(r, max) == NULL)\n return 0;\n if (BN_ucmp(a, b) < 0) {\n if (!BN_usub(r, b, a))\n return 0;\n r->neg = 1;\n } else {\n if (!BN_usub(r, a, b))\n return 0;\n r->neg = 0;\n }\n bn_check_top(r);\n return 1;\n}', 'int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int max, min, dif;\n const BN_ULONG *ap, *bp;\n BN_ULONG *rp, carry, t1, t2;\n bn_check_top(a);\n bn_check_top(b);\n if (a->top < b->top) {\n const BIGNUM *tmp;\n tmp = a;\n a = b;\n b = tmp;\n }\n max = a->top;\n min = b->top;\n dif = max - min;\n if (bn_wexpand(r, max + 1) == NULL)\n return 0;\n r->top = max;\n ap = a->d;\n bp = b->d;\n rp = r->d;\n carry = bn_add_words(rp, ap, bp, min);\n rp += min;\n ap += min;\n while (dif) {\n dif--;\n t1 = *(ap++);\n t2 = (t1 + carry) & BN_MASK2;\n *(rp++) = t2;\n carry &= (t2 == 0);\n }\n *rp = carry;\n r->top += carry;\n r->neg = 0;\n bn_check_top(r);\n return 1;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return (NULL);\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}']
36,409
0
https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/crypto/lhash/lhash.c/#L296
void lh_doall_arg(LHASH *lh, void (*func)(), char *arg) { int i; LHASH_NODE *a,*n; for (i=lh->num_nodes-1; i>=0; i--) { a=lh->b[i]; while (a != NULL) { n=a->next; func(a->data,arg); a=n; } } }
['int MAIN(int argc, char **argv)\n\t{\n#ifndef NO_DSA\n\tDSA *dsa_params=NULL;\n#endif\n\tint ex=1,x509=0,days=30;\n\tX509 *x509ss=NULL;\n\tX509_REQ *req=NULL;\n\tEVP_PKEY *pkey=NULL;\n\tint i,badops=0,newreq=0,newkey= -1,pkey_type=0;\n\tBIO *in=NULL,*out=NULL;\n\tint informat,outformat,verify=0,noout=0,text=0,keyform=FORMAT_PEM;\n\tint nodes=0,kludge=0;\n\tchar *infile,*outfile,*prog,*keyfile=NULL,*template=NULL,*keyout=NULL;\n\tchar *extensions = NULL;\n\tEVP_CIPHER *cipher=NULL;\n\tint modulus=0;\n\tchar *p;\n\tconst EVP_MD *md_alg=NULL,*digest=EVP_md5();\n#ifndef MONOLITH\n\tMS_STATIC char config_name[256];\n#endif\n#ifndef NO_DES\n\tcipher=EVP_des_ede3_cbc();\n#endif\n\tapps_startup();\n\tif (bio_err == NULL)\n\t\tif ((bio_err=BIO_new(BIO_s_file())) != NULL)\n\t\t\tBIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);\n\tinfile=NULL;\n\toutfile=NULL;\n\tinformat=FORMAT_PEM;\n\toutformat=FORMAT_PEM;\n\tprog=argv[0];\n\targc--;\n\targv++;\n\twhile (argc >= 1)\n\t\t{\n\t\tif \t(strcmp(*argv,"-inform") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tinformat=str2fmt(*(++argv));\n\t\t\t}\n\t\telse if (strcmp(*argv,"-outform") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\toutformat=str2fmt(*(++argv));\n\t\t\t}\n\t\telse if (strcmp(*argv,"-key") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tkeyfile= *(++argv);\n\t\t\t}\n\t\telse if (strcmp(*argv,"-new") == 0)\n\t\t\t{\n\t\t\tpkey_type=TYPE_RSA;\n\t\t\tnewreq=1;\n\t\t\t}\n\t\telse if (strcmp(*argv,"-config") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\ttemplate= *(++argv);\n\t\t\t}\n\t\telse if (strcmp(*argv,"-keyform") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tkeyform=str2fmt(*(++argv));\n\t\t\t}\n\t\telse if (strcmp(*argv,"-in") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tinfile= *(++argv);\n\t\t\t}\n\t\telse if (strcmp(*argv,"-out") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\toutfile= *(++argv);\n\t\t\t}\n\t\telse if (strcmp(*argv,"-keyout") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tkeyout= *(++argv);\n\t\t\t}\n\t\telse if (strcmp(*argv,"-newkey") == 0)\n\t\t\t{\n\t\t\tint is_numeric;\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tp= *(++argv);\n\t\t\tis_numeric = p[0] >= \'0\' && p[0] <= \'9\';\n\t\t\tif (strncmp("rsa:",p,4) == 0 || is_numeric)\n\t\t\t\t{\n\t\t\t\tpkey_type=TYPE_RSA;\n\t\t\t\tif(!is_numeric)\n\t\t\t\t p+=4;\n\t\t\t\tnewkey= atoi(p);\n\t\t\t\t}\n\t\t\telse\n#ifndef NO_DSA\n\t\t\t\tif (strncmp("dsa:",p,4) == 0)\n\t\t\t\t{\n\t\t\t\tX509 *xtmp=NULL;\n\t\t\t\tEVP_PKEY *dtmp;\n\t\t\t\tpkey_type=TYPE_DSA;\n\t\t\t\tp+=4;\n\t\t\t\tif ((in=BIO_new_file(p,"r")) == NULL)\n\t\t\t\t\t{\n\t\t\t\t\tperror(p);\n\t\t\t\t\tgoto end;\n\t\t\t\t\t}\n\t\t\t\tif ((dsa_params=PEM_read_bio_DSAparams(in,NULL,NULL)) == NULL)\n\t\t\t\t\t{\n\t\t\t\t\tERR_clear_error();\n\t\t\t\t\tBIO_reset(in);\n\t\t\t\t\tif ((xtmp=PEM_read_bio_X509(in,NULL,NULL)) == NULL)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tBIO_printf(bio_err,"unable to load DSA parameters from file\\n");\n\t\t\t\t\t\tgoto end;\n\t\t\t\t\t\t}\n\t\t\t\t\tdtmp=X509_get_pubkey(xtmp);\n\t\t\t\t\tif (dtmp->type == EVP_PKEY_DSA)\n\t\t\t\t\t\tdsa_params=DSAparams_dup(dtmp->pkey.dsa);\n\t\t\t\t\tEVP_PKEY_free(dtmp);\n\t\t\t\t\tX509_free(xtmp);\n\t\t\t\t\tif (dsa_params == NULL)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tBIO_printf(bio_err,"Certificate does not contain DSA parameters\\n");\n\t\t\t\t\t\tgoto end;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\tBIO_free(in);\n\t\t\t\tnewkey=BN_num_bits(dsa_params->p);\n\t\t\t\tin=NULL;\n\t\t\t\t}\n\t\t\telse\n#endif\n#ifndef NO_DH\n\t\t\t\tif (strncmp("dh:",p,4) == 0)\n\t\t\t\t{\n\t\t\t\tpkey_type=TYPE_DH;\n\t\t\t\tp+=3;\n\t\t\t\t}\n\t\t\telse\n#endif\n\t\t\t\tpkey_type=TYPE_RSA;\n\t\t\tnewreq=1;\n\t\t\t}\n\t\telse if (strcmp(*argv,"-modulus") == 0)\n\t\t\tmodulus=1;\n\t\telse if (strcmp(*argv,"-verify") == 0)\n\t\t\tverify=1;\n\t\telse if (strcmp(*argv,"-nodes") == 0)\n\t\t\tnodes=1;\n\t\telse if (strcmp(*argv,"-noout") == 0)\n\t\t\tnoout=1;\n\t\telse if (strcmp(*argv,"-text") == 0)\n\t\t\ttext=1;\n\t\telse if (strcmp(*argv,"-x509") == 0)\n\t\t\tx509=1;\n\t\telse if (strcmp(*argv,"-asn1-kludge") == 0)\n\t\t\tkludge=1;\n\t\telse if (strcmp(*argv,"-no-asn1-kludge") == 0)\n\t\t\tkludge=0;\n\t\telse if (strcmp(*argv,"-days") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tdays= atoi(*(++argv));\n\t\t\tif (days == 0) days=30;\n\t\t\t}\n\t\telse if ((md_alg=EVP_get_digestbyname(&((*argv)[1]))) != NULL)\n\t\t\t{\n\t\t\tdigest=md_alg;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"unknown option %s\\n",*argv);\n\t\t\tbadops=1;\n\t\t\tbreak;\n\t\t\t}\n\t\targc--;\n\t\targv++;\n\t\t}\n\tif (badops)\n\t\t{\nbad:\n\t\tBIO_printf(bio_err,"%s [options] <infile >outfile\\n",prog);\n\t\tBIO_printf(bio_err,"where options are\\n");\n\t\tBIO_printf(bio_err," -inform arg input format - one of DER TXT PEM\\n");\n\t\tBIO_printf(bio_err," -outform arg output format - one of DER TXT PEM\\n");\n\t\tBIO_printf(bio_err," -in arg input file\\n");\n\t\tBIO_printf(bio_err," -out arg output file\\n");\n\t\tBIO_printf(bio_err," -text text form of request\\n");\n\t\tBIO_printf(bio_err," -noout do not output REQ\\n");\n\t\tBIO_printf(bio_err," -verify verify signature on REQ\\n");\n\t\tBIO_printf(bio_err," -modulus RSA modulus\\n");\n\t\tBIO_printf(bio_err," -nodes don\'t encrypt the output key\\n");\n\t\tBIO_printf(bio_err," -key file\tuse the private key contained in file\\n");\n\t\tBIO_printf(bio_err," -keyform arg key file format\\n");\n\t\tBIO_printf(bio_err," -keyout arg file to send the key to\\n");\n\t\tBIO_printf(bio_err," -newkey rsa:bits generate a new RSA key of \'bits\' in size\\n");\n\t\tBIO_printf(bio_err," -newkey dsa:file generate a new DSA key, parameters taken from CA in \'file\'\\n");\n\t\tBIO_printf(bio_err," -[digest] Digest to sign with (md5, sha1, md2, mdc2)\\n");\n\t\tBIO_printf(bio_err," -config file request template file.\\n");\n\t\tBIO_printf(bio_err," -new new request.\\n");\n\t\tBIO_printf(bio_err," -x509 output a x509 structure instead of a cert. req.\\n");\n\t\tBIO_printf(bio_err," -days number of days a x509 generated by -x509 is valid for.\\n");\n\t\tBIO_printf(bio_err," -asn1-kludge Output the \'request\' in a format that is wrong but some CA\'s\\n");\n\t\tBIO_printf(bio_err," have been reported as requiring\\n");\n\t\tBIO_printf(bio_err," [ It is now always turned on but can be turned off with -no-asn1-kludge ]\\n");\n\t\tgoto end;\n\t\t}\n\tERR_load_crypto_strings();\n\tX509V3_add_standard_extensions();\n#ifndef MONOLITH\n\tp=getenv("OPENSSL_CONF");\n\tif (p == NULL)\n\t\tp=getenv("SSLEAY_CONF");\n\tif (p == NULL)\n\t\t{\n\t\tstrcpy(config_name,X509_get_default_cert_area());\n\t\tstrcat(config_name,"/lib/");\n\t\tstrcat(config_name,OPENSSL_CONF);\n\t\tp=config_name;\n\t\t}\n default_config_file=p;\n\tconfig=CONF_load(config,p,NULL);\n#endif\n\tif (template != NULL)\n\t\t{\n\t\tlong errline;\n\t\tBIO_printf(bio_err,"Using configuration from %s\\n",template);\n\t\treq_conf=CONF_load(NULL,template,&errline);\n\t\tif (req_conf == NULL)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"error on line %ld of %s\\n",errline,template);\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\treq_conf=config;\n\t\tBIO_printf(bio_err,"Using configuration from %s\\n",\n\t\t\tdefault_config_file);\n\t\tif (req_conf == NULL)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"Unable to load config info\\n");\n\t\t\t}\n\t\t}\n\tif (req_conf != NULL)\n\t\t{\n\t\tp=CONF_get_string(req_conf,NULL,"oid_file");\n\t\tif (p != NULL)\n\t\t\t{\n\t\t\tBIO *oid_bio;\n\t\t\toid_bio=BIO_new_file(p,"r");\n\t\t\tif (oid_bio == NULL)\n\t\t\t\t{\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tOBJ_create_objects(oid_bio);\n\t\t\t\tBIO_free(oid_bio);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif(!add_oid_section(req_conf)) goto end;\n\tif ((md_alg == NULL) &&\n\t\t((p=CONF_get_string(req_conf,SECTION,"default_md")) != NULL))\n\t\t{\n\t\tif ((md_alg=EVP_get_digestbyname(p)) != NULL)\n\t\t\tdigest=md_alg;\n\t\t}\n\textensions = CONF_get_string(req_conf, SECTION, V3_EXTENSIONS);\n\tif(extensions) {\n\t\tX509V3_CTX ctx;\n\t\tX509V3_set_ctx_test(&ctx);\n\t\tX509V3_set_conf_lhash(&ctx, req_conf);\n\t\tif(!X509V3_EXT_add_conf(req_conf, &ctx, extensions, NULL)) {\n\t\t\tBIO_printf(bio_err,\n\t\t\t "Error Loading extension section %s\\n", extensions);\n\t\t\tgoto end;\n\t\t}\n\t}\n\tin=BIO_new(BIO_s_file());\n\tout=BIO_new(BIO_s_file());\n\tif ((in == NULL) || (out == NULL))\n\t\tgoto end;\n\tif (keyfile != NULL)\n\t\t{\n\t\tif (BIO_read_filename(in,keyfile) <= 0)\n\t\t\t{\n\t\t\tperror(keyfile);\n\t\t\tgoto end;\n\t\t\t}\n\t\tif (keyform == FORMAT_PEM)\n\t\t\tpkey=PEM_read_bio_PrivateKey(in,NULL,NULL);\n\t\telse\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"bad input format specified for X509 request\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\tif (pkey == NULL)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"unable to load Private key\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n\tif (newreq && (pkey == NULL))\n\t\t{\n\t\tchar *randfile;\n\t\tchar buffer[200];\n\t\tif ((randfile=CONF_get_string(req_conf,SECTION,"RANDFILE")) == NULL)\n\t\t\trandfile=RAND_file_name(buffer,200);\n#ifdef WINDOWS\n\t\tBIO_printf(bio_err,"Loading \'screen\' into random state -");\n\t\tBIO_flush(bio_err);\n\t\tRAND_screen();\n\t\tBIO_printf(bio_err," done\\n");\n#endif\n\t\tif ((randfile == NULL) || !RAND_load_file(randfile,1024L*1024L))\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"unable to load \'random state\'\\n");\n\t\t\tBIO_printf(bio_err,"What this means is that the random number generator has not been seeded\\n");\n\t\t\tBIO_printf(bio_err,"with much random data.\\n");\n\t\t\tBIO_printf(bio_err,"Consider setting the RANDFILE environment variable to point at a file that\\n");\n\t\t\tBIO_printf(bio_err,"\'random\' data can be kept in.\\n");\n\t\t\t}\n\t\tif (newkey <= 0)\n\t\t\t{\n\t\t\tnewkey=(int)CONF_get_number(req_conf,SECTION,BITS);\n\t\t\tif (newkey <= 0)\n\t\t\t\tnewkey=DEFAULT_KEY_LENGTH;\n\t\t\t}\n\t\tif (newkey < MIN_KEY_LENGTH)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"private key length is too short,\\n");\n\t\t\tBIO_printf(bio_err,"it needs to be at least %d bits, not %d\\n",MIN_KEY_LENGTH,newkey);\n\t\t\tgoto end;\n\t\t\t}\n\t\tBIO_printf(bio_err,"Generating a %d bit %s private key\\n",\n\t\t\tnewkey,(pkey_type == TYPE_RSA)?"RSA":"DSA");\n\t\tif ((pkey=EVP_PKEY_new()) == NULL) goto end;\n#ifndef NO_RSA\n\t\tif (pkey_type == TYPE_RSA)\n\t\t\t{\n\t\t\tif (!EVP_PKEY_assign_RSA(pkey,\n\t\t\t\tRSA_generate_key(newkey,0x10001,\n\t\t\t\t\treq_cb,(char *)bio_err)))\n\t\t\t\tgoto end;\n\t\t\t}\n\t\telse\n#endif\n#ifndef NO_DSA\n\t\t\tif (pkey_type == TYPE_DSA)\n\t\t\t{\n\t\t\tif (!DSA_generate_key(dsa_params)) goto end;\n\t\t\tif (!EVP_PKEY_assign_DSA(pkey,dsa_params)) goto end;\n\t\t\tdsa_params=NULL;\n\t\t\t}\n#endif\n\t\tif ((randfile == NULL) || (RAND_write_file(randfile) == 0))\n\t\t\tBIO_printf(bio_err,"unable to write \'random state\'\\n");\n\t\tif (pkey == NULL) goto end;\n\t\tif (keyout == NULL)\n\t\t\tkeyout=CONF_get_string(req_conf,SECTION,KEYFILE);\n\t\tif (keyout == NULL)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"writing new private key to stdout\\n");\n\t\t\tBIO_set_fp(out,stdout,BIO_NOCLOSE);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"writing new private key to \'%s\'\\n",keyout);\n\t\t\tif (BIO_write_filename(out,keyout) <= 0)\n\t\t\t\t{\n\t\t\t\tperror(keyout);\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\t}\n\t\tp=CONF_get_string(req_conf,SECTION,"encrypt_rsa_key");\n\t\tif (p == NULL)\n\t\t\tp=CONF_get_string(req_conf,SECTION,"encrypt_key");\n\t\tif ((p != NULL) && (strcmp(p,"no") == 0))\n\t\t\tcipher=NULL;\n\t\tif (nodes) cipher=NULL;\n\t\ti=0;\nloop:\n\t\tif (!PEM_write_bio_PrivateKey(out,pkey,cipher,\n\t\t\tNULL,0,NULL))\n\t\t\t{\n\t\t\tif ((ERR_GET_REASON(ERR_peek_error()) ==\n\t\t\t\tPEM_R_PROBLEMS_GETTING_PASSWORD) && (i < 3))\n\t\t\t\t{\n\t\t\t\tERR_clear_error();\n\t\t\t\ti++;\n\t\t\t\tgoto loop;\n\t\t\t\t}\n\t\t\tgoto end;\n\t\t\t}\n\t\tBIO_printf(bio_err,"-----\\n");\n\t\t}\n\tif (!newreq)\n\t\t{\n\t\tkludge= -1;\n\t\tif (infile == NULL)\n\t\t\tBIO_set_fp(in,stdin,BIO_NOCLOSE);\n\t\telse\n\t\t\t{\n\t\t\tif (BIO_read_filename(in,infile) <= 0)\n\t\t\t\t{\n\t\t\t\tperror(infile);\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\t}\n\t\tif\t(informat == FORMAT_ASN1)\n\t\t\treq=d2i_X509_REQ_bio(in,NULL);\n\t\telse if (informat == FORMAT_PEM)\n\t\t\treq=PEM_read_bio_X509_REQ(in,NULL,NULL);\n\t\telse\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"bad input format specified for X509 request\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\tif (req == NULL)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"unable to load X509 request\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n\tif (newreq || x509)\n\t\t{\n#ifndef NO_DSA\n\t\tif (pkey->type == EVP_PKEY_DSA)\n\t\t\tdigest=EVP_dss1();\n#endif\n\t\tif (pkey == NULL)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"you need to specify a private key\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\tif (req == NULL)\n\t\t\t{\n\t\t\treq=X509_REQ_new();\n\t\t\tif (req == NULL)\n\t\t\t\t{\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\ti=make_REQ(req,pkey,!x509);\n\t\t\tif (kludge >= 0)\n\t\t\t\treq->req_info->req_kludge=kludge;\n\t\t\tif (!i)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err,"problems making Certificate Request\\n");\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\t}\n\t\tif (x509)\n\t\t\t{\n\t\t\tEVP_PKEY *tmppkey;\n\t\t\tX509V3_CTX ext_ctx;\n\t\t\tif ((x509ss=X509_new()) == NULL) goto end;\n\t\t\tif(!X509_set_version(x509ss, 2)) goto end;\n\t\t\tASN1_INTEGER_set(X509_get_serialNumber(x509ss),0L);\n\t\t\tX509_set_issuer_name(x509ss,\n\t\t\t\tX509_REQ_get_subject_name(req));\n\t\t\tX509_gmtime_adj(X509_get_notBefore(x509ss),0);\n\t\t\tX509_gmtime_adj(X509_get_notAfter(x509ss),\n\t\t\t\t(long)60*60*24*days);\n\t\t\tX509_set_subject_name(x509ss,\n\t\t\t\tX509_REQ_get_subject_name(req));\n\t\t\ttmppkey = X509_REQ_get_pubkey(req);\n\t\t\tX509_set_pubkey(x509ss,tmppkey);\n\t\t\tEVP_PKEY_free(tmppkey);\n\t\t\tX509V3_set_ctx(&ext_ctx, x509ss, x509ss, NULL, NULL, 0);\n\t\t\tX509V3_set_conf_lhash(&ext_ctx, req_conf);\n\t\t\tif(extensions && !X509V3_EXT_add_conf(req_conf,\n\t\t\t\t \t&ext_ctx, extensions, x509ss))\n\t\t\t {\n\t\t\t BIO_printf(bio_err,\n\t\t\t\t "Error Loading extension section %s\\n",\n\t\t\t\t extensions);\n\t\t\t goto end;\n\t\t\t }\n\t\t\tif (!(i=X509_sign(x509ss,pkey,digest)))\n\t\t\t\tgoto end;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (!(i=X509_REQ_sign(req,pkey,digest)))\n\t\t\t\tgoto end;\n\t\t\t}\n\t\t}\n\tif (verify && !x509)\n\t\t{\n\t\tint tmp=0;\n\t\tif (pkey == NULL)\n\t\t\t{\n\t\t\tpkey=X509_REQ_get_pubkey(req);\n\t\t\ttmp=1;\n\t\t\tif (pkey == NULL) goto end;\n\t\t\t}\n\t\ti=X509_REQ_verify(req,pkey);\n\t\tif (tmp) {\n\t\t\tEVP_PKEY_free(pkey);\n\t\t\tpkey=NULL;\n\t\t}\n\t\tif (i < 0)\n\t\t\t{\n\t\t\tgoto end;\n\t\t\t}\n\t\telse if (i == 0)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"verify failure\\n");\n\t\t\t}\n\t\telse\n\t\t\tBIO_printf(bio_err,"verify OK\\n");\n\t\t}\n\tif (noout && !text && !modulus)\n\t\t{\n\t\tex=0;\n\t\tgoto end;\n\t\t}\n\tif (outfile == NULL)\n\t\tBIO_set_fp(out,stdout,BIO_NOCLOSE);\n\telse\n\t\t{\n\t\tif ((keyout != NULL) && (strcmp(outfile,keyout) == 0))\n\t\t\ti=(int)BIO_append_filename(out,outfile);\n\t\telse\n\t\t\ti=(int)BIO_write_filename(out,outfile);\n\t\tif (!i)\n\t\t\t{\n\t\t\tperror(outfile);\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n\tif (text)\n\t\t{\n\t\tif (x509)\n\t\t\tX509_print(out,x509ss);\n\t\telse\n\t\t\tX509_REQ_print(out,req);\n\t\t}\n\tif (modulus)\n\t\t{\n\t\tEVP_PKEY *pubkey;\n\t\tif (x509)\n\t\t\tpubkey=X509_get_pubkey(x509ss);\n\t\telse\n\t\t\tpubkey=X509_REQ_get_pubkey(req);\n\t\tif (pubkey == NULL)\n\t\t\t{\n\t\t\tfprintf(stdout,"Modulus=unavailable\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\tfprintf(stdout,"Modulus=");\n#ifndef NO_RSA\n\t\tif (pubkey->type == EVP_PKEY_RSA)\n\t\t\tBN_print(out,pubkey->pkey.rsa->n);\n\t\telse\n#endif\n\t\t\tfprintf(stdout,"Wrong Algorithm type");\n\t\tfprintf(stdout,"\\n");\n\t\t}\n\tif (!noout && !x509)\n\t\t{\n\t\tif \t(outformat == FORMAT_ASN1)\n\t\t\ti=i2d_X509_REQ_bio(out,req);\n\t\telse if (outformat == FORMAT_PEM)\n\t\t\ti=PEM_write_bio_X509_REQ(out,req);\n\t\telse\t{\n\t\t\tBIO_printf(bio_err,"bad output format specified for outfile\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\tif (!i)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"unable to write X509 request\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n\tif (!noout && x509 && (x509ss != NULL))\n\t\t{\n\t\tif \t(outformat == FORMAT_ASN1)\n\t\t\ti=i2d_X509_bio(out,x509ss);\n\t\telse if (outformat == FORMAT_PEM)\n\t\t\ti=PEM_write_bio_X509(out,x509ss);\n\t\telse\t{\n\t\t\tBIO_printf(bio_err,"bad output format specified for outfile\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\tif (!i)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"unable to write X509 certificate\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n\tex=0;\nend:\n\tif (ex)\n\t\t{\n\t\tERR_print_errors(bio_err);\n\t\t}\n\tif ((req_conf != NULL) && (req_conf != config)) CONF_free(req_conf);\n\tBIO_free(in);\n\tBIO_free(out);\n\tEVP_PKEY_free(pkey);\n\tX509_REQ_free(req);\n\tX509_free(x509ss);\n\tX509V3_EXT_cleanup();\n\tOBJ_cleanup();\n#ifndef NO_DSA\n\tif (dsa_params != NULL) DSA_free(dsa_params);\n#endif\n\tEXIT(ex);\n\t}', 'LHASH *CONF_load(LHASH *h, char *file, long *line)\n\t{\n\tLHASH *ret=NULL;\n\tFILE *in=NULL;\n#define BUFSIZE\t512\n\tchar btmp[16];\n\tint bufnum=0,i,ii;\n\tBUF_MEM *buff=NULL;\n\tchar *s,*p,*end;\n\tint again,n;\n\tlong eline=0;\n\tCONF_VALUE *v=NULL,*vv,*tv;\n\tCONF_VALUE *sv=NULL;\n\tchar *section=NULL,*buf;\n\tSTACK *section_sk=NULL,*ts;\n\tchar *start,*psection,*pname;\n\tif ((buff=BUF_MEM_new()) == NULL)\n\t\t{\n\t\tCONFerr(CONF_F_CONF_LOAD,ERR_R_BUF_LIB);\n\t\tgoto err;\n\t\t}\n\tin=fopen(file,"rb");\n\tif (in == NULL)\n\t\t{\n\t\tSYSerr(SYS_F_FOPEN,get_last_sys_error());\n\t\tERR_set_error_data(BUF_strdup(file),\n\t\t\tERR_TXT_MALLOCED|ERR_TXT_STRING);\n\t\tCONFerr(CONF_F_CONF_LOAD,ERR_R_SYS_LIB);\n\t\tgoto err;\n\t\t}\n\tsection=(char *)Malloc(10);\n\tif (section == NULL)\n\t\t{\n\t\tCONFerr(CONF_F_CONF_LOAD,ERR_R_MALLOC_FAILURE);\n\t\tgoto err;\n\t\t}\n\tstrcpy(section,"default");\n\tif (h == NULL)\n\t\t{\n\t\tif ((ret=lh_new(hash,cmp)) == NULL)\n\t\t\t{\n\t\t\tCONFerr(CONF_F_CONF_LOAD,ERR_R_MALLOC_FAILURE);\n\t\t\tgoto err;\n\t\t\t}\n\t\t}\n\telse\n\t\tret=h;\n\tsv=new_section(ret,section);\n\tif (sv == NULL)\n\t\t{\n\t\tCONFerr(CONF_F_CONF_LOAD,CONF_R_UNABLE_TO_CREATE_NEW_SECTION);\n\t\tgoto err;\n\t\t}\n\tsection_sk=(STACK *)sv->value;\n\tbufnum=0;\n\tfor (;;)\n\t\t{\n\t\tagain=0;\n\t\tif (!BUF_MEM_grow(buff,bufnum+BUFSIZE))\n\t\t\t{\n\t\t\tCONFerr(CONF_F_CONF_LOAD,ERR_R_BUF_LIB);\n\t\t\tgoto err;\n\t\t\t}\n\t\tp= &(buff->data[bufnum]);\n\t\t*p=\'\\0\';\n\t\tfgets(p,BUFSIZE-1,in);\n\t\tp[BUFSIZE-1]=\'\\0\';\n\t\tii=i=strlen(p);\n\t\tif (i == 0) break;\n\t\twhile (i > 0)\n\t\t\t{\n\t\t\tif ((p[i-1] != \'\\r\') && (p[i-1] != \'\\n\'))\n\t\t\t\tbreak;\n\t\t\telse\n\t\t\t\ti--;\n\t\t\t}\n\t\tif (i == ii)\n\t\t\tagain=1;\n\t\telse\n\t\t\t{\n\t\t\tp[i]=\'\\0\';\n\t\t\teline++;\n\t\t\t}\n\t\tbufnum+=i;\n\t\tv=NULL;\n\t\tif (bufnum >= 1)\n\t\t\t{\n\t\t\tp= &(buff->data[bufnum-1]);\n\t\t\tif (\tIS_ESC(p[0]) &&\n\t\t\t\t((bufnum <= 1) || !IS_ESC(p[-1])))\n\t\t\t\t{\n\t\t\t\tbufnum--;\n\t\t\t\tagain=1;\n\t\t\t\t}\n\t\t\t}\n\t\tif (again) continue;\n\t\tbufnum=0;\n\t\tbuf=buff->data;\n\t\tclear_comments(buf);\n\t\tn=strlen(buf);\n\t\ts=eat_ws(buf);\n\t\tif (IS_EOF(*s)) continue;\n\t\tif (*s == \'[\')\n\t\t\t{\n\t\t\tchar *ss;\n\t\t\ts++;\n\t\t\tstart=eat_ws(s);\n\t\t\tss=start;\nagain:\n\t\t\tend=eat_alpha_numeric(ss);\n\t\t\tp=eat_ws(end);\n\t\t\tif (*p != \']\')\n\t\t\t\t{\n\t\t\t\tif (*p != \'\\0\')\n\t\t\t\t\t{\n\t\t\t\t\tss=p;\n\t\t\t\t\tgoto again;\n\t\t\t\t\t}\n\t\t\t\tCONFerr(CONF_F_CONF_LOAD,CONF_R_MISSING_CLOSE_SQUARE_BRACKET);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\t*end=\'\\0\';\n\t\t\tif (!str_copy(ret,NULL,&section,start)) goto err;\n\t\t\tif ((sv=get_section(ret,section)) == NULL)\n\t\t\t\tsv=new_section(ret,section);\n\t\t\tif (sv == NULL)\n\t\t\t\t{\n\t\t\t\tCONFerr(CONF_F_CONF_LOAD,CONF_R_UNABLE_TO_CREATE_NEW_SECTION);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tsection_sk=(STACK *)sv->value;\n\t\t\tcontinue;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tpname=s;\n\t\t\tpsection=NULL;\n\t\t\tend=eat_alpha_numeric(s);\n\t\t\tif ((end[0] == \':\') && (end[1] == \':\'))\n\t\t\t\t{\n\t\t\t\t*end=\'\\0\';\n\t\t\t\tend+=2;\n\t\t\t\tpsection=pname;\n\t\t\t\tpname=end;\n\t\t\t\tend=eat_alpha_numeric(end);\n\t\t\t\t}\n\t\t\tp=eat_ws(end);\n\t\t\tif (*p != \'=\')\n\t\t\t\t{\n\t\t\t\tCONFerr(CONF_F_CONF_LOAD,CONF_R_MISSING_EQUAL_SIGN);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\t*end=\'\\0\';\n\t\t\tp++;\n\t\t\tstart=eat_ws(p);\n\t\t\twhile (!IS_EOF(*p))\n\t\t\t\tp++;\n\t\t\tp--;\n\t\t\twhile ((p != start) && (IS_WS(*p)))\n\t\t\t\tp--;\n\t\t\tp++;\n\t\t\t*p=\'\\0\';\n\t\t\tif ((v=(CONF_VALUE *)Malloc(sizeof(CONF_VALUE))) == NULL)\n\t\t\t\t{\n\t\t\t\tCONFerr(CONF_F_CONF_LOAD,ERR_R_MALLOC_FAILURE);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif (psection == NULL) psection=section;\n\t\t\tv->name=(char *)Malloc(strlen(pname)+1);\n\t\t\tv->value=NULL;\n\t\t\tif (v->name == NULL)\n\t\t\t\t{\n\t\t\t\tCONFerr(CONF_F_CONF_LOAD,ERR_R_MALLOC_FAILURE);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tstrcpy(v->name,pname);\n\t\t\tif (!str_copy(ret,psection,&(v->value),start)) goto err;\n\t\t\tif (strcmp(psection,section) != 0)\n\t\t\t\t{\n\t\t\t\tif ((tv=get_section(ret,psection))\n\t\t\t\t\t== NULL)\n\t\t\t\t\ttv=new_section(ret,psection);\n\t\t\t\tif (tv == NULL)\n\t\t\t\t\t{\n\t\t\t\t\tCONFerr(CONF_F_CONF_LOAD,CONF_R_UNABLE_TO_CREATE_NEW_SECTION);\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t\tts=(STACK *)tv->value;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\ttv=sv;\n\t\t\t\tts=section_sk;\n\t\t\t\t}\n\t\t\tv->section=tv->section;\n\t\t\tif (!sk_push(ts,(char *)v))\n\t\t\t\t{\n\t\t\t\tCONFerr(CONF_F_CONF_LOAD,ERR_R_MALLOC_FAILURE);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tvv=(CONF_VALUE *)lh_insert(ret,(char *)v);\n\t\t\tif (vv != NULL)\n\t\t\t\t{\n\t\t\t\tsk_delete_ptr(ts,(char *)vv);\n\t\t\t\tFree(vv->name);\n\t\t\t\tFree(vv->value);\n\t\t\t\tFree(vv);\n\t\t\t\t}\n\t\t\tv=NULL;\n\t\t\t}\n\t\t}\n\tif (buff != NULL) BUF_MEM_free(buff);\n\tif (section != NULL) Free(section);\n\tif (in != NULL) fclose(in);\n\treturn(ret);\nerr:\n\tif (buff != NULL) BUF_MEM_free(buff);\n\tif (section != NULL) Free(section);\n\tif (line != NULL) *line=eline;\n\tsprintf(btmp,"%ld",eline);\n\tERR_add_error_data(2,"line ",btmp);\n\tif (in != NULL) fclose(in);\n\tif ((h != ret) && (ret != NULL)) CONF_free(ret);\n\tif (v != NULL)\n\t\t{\n\t\tif (v->name != NULL) Free(v->name);\n\t\tif (v->value != NULL) Free(v->value);\n\t\tif (v != NULL) Free(v);\n\t\t}\n\treturn(NULL);\n\t}', 'void CONF_free(LHASH *conf)\n\t{\n\tif (conf == NULL) return;\n\tconf->down_load=0;\n\tlh_doall_arg(conf,(void (*)())value_free_hash,(char *)conf);\n\tlh_doall_arg(conf,(void (*)())value_free_stack,(char *)conf);\n\tlh_free(conf);\n\t}', 'void lh_doall_arg(LHASH *lh, void (*func)(), char *arg)\n\t{\n\tint i;\n\tLHASH_NODE *a,*n;\n\tfor (i=lh->num_nodes-1; i>=0; i--)\n\t\t{\n\t\ta=lh->b[i];\n\t\twhile (a != NULL)\n\t\t\t{\n\t\t\tn=a->next;\n\t\t\tfunc(a->data,arg);\n\t\t\ta=n;\n\t\t\t}\n\t\t}\n\t}']
36,410
0
https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/crypto/bn/bn_lib.c/#L377
BIGNUM *bn_expand2(BIGNUM *b, int words) { BN_ULONG *A,*B,*a; int i,j; bn_check_top(b); if (words > b->max) { bn_check_top(b); if (BN_get_flags(b,BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND2,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return(NULL); } a=A=(BN_ULONG *)Malloc(sizeof(BN_ULONG)*(words+1)); if (A == NULL) { BNerr(BN_F_BN_EXPAND2,ERR_R_MALLOC_FAILURE); return(NULL); } memset(A,0x5c,sizeof(BN_ULONG)*(words+1)); #if 1 B=b->d; if (B != NULL) { for (i=b->top&(~7); i>0; i-=8) { A[0]=B[0]; A[1]=B[1]; A[2]=B[2]; A[3]=B[3]; A[4]=B[4]; A[5]=B[5]; A[6]=B[6]; A[7]=B[7]; A+=8; B+=8; } switch (b->top&7) { case 7: A[6]=B[6]; case 6: A[5]=B[5]; case 5: A[4]=B[4]; case 4: A[3]=B[3]; case 3: A[2]=B[2]; case 2: A[1]=B[1]; case 1: A[0]=B[0]; case 0: ; } Free(b->d); } b->d=a; b->max=words; B= &(b->d[b->top]); j=(b->max - b->top) & ~7; for (i=0; i<j; i+=8) { B[0]=0; B[1]=0; B[2]=0; B[3]=0; B[4]=0; B[5]=0; B[6]=0; B[7]=0; B+=8; } j=(b->max - b->top) & 7; for (i=0; i<j; i++) { B[0]=0; B++; } #else memcpy(a->d,b->d,sizeof(b->d[0])*b->top); #endif } return(b); }
['int test_div(BIO *bp, BN_CTX *ctx)\n\t{\n\tBIGNUM a,b,c,d;\n\tint i;\n\tint j;\n\tBN_init(&a);\n\tBN_init(&b);\n\tBN_init(&c);\n\tBN_init(&d);\n\tBN_rand(&a,400,0,0);\n\tfor (i=0; i<100; i++)\n\t\t{\n\t\tBN_rand(&b,50+i,0,0);\n\t\ta.neg=rand_neg();\n\t\tb.neg=rand_neg();\n\t\tif (bp == NULL)\n\t\t\tfor (j=0; j<100; j++)\n\t\t\t\tBN_div(&d,&c,&a,&b,ctx);\n\t\tBN_div(&d,&c,&a,&b,ctx);\n\t\tif (bp != NULL)\n\t\t\t{\n\t\t\tif (!results)\n\t\t\t\t{\n\t\t\t\tBN_print(bp,&a);\n\t\t\t\tBIO_puts(bp," / ");\n\t\t\t\tBN_print(bp,&b);\n\t\t\t\tBIO_puts(bp," - ");\n\t\t\t\t}\n\t\t\tBN_print(bp,&d);\n\t\t\tBIO_puts(bp,"\\n");\n\t\t\tif (!results)\n\t\t\t\t{\n\t\t\t\tBN_print(bp,&a);\n\t\t\t\tBIO_puts(bp," % ");\n\t\t\t\tBN_print(bp,&b);\n\t\t\t\tBIO_puts(bp," - ");\n\t\t\t\t}\n\t\t\tBN_print(bp,&c);\n\t\t\tBIO_puts(bp,"\\n");\n\t\t\t}\n\t\t}\n\tBN_free(&a);\n\tBN_free(&b);\n\tBN_free(&c);\n\tBN_free(&d);\n\treturn(1);\n\t}', 'int BN_rand(BIGNUM *rnd, int bits, int top, int bottom)\n\t{\n\tunsigned char *buf=NULL;\n\tint ret=0,bit,bytes,mask;\n\ttime_t tim;\n\tbytes=(bits+7)/8;\n\tbit=(bits-1)%8;\n\tmask=0xff<<bit;\n\tbuf=(unsigned char *)Malloc(bytes);\n\tif (buf == NULL)\n\t\t{\n\t\tBNerr(BN_F_BN_RAND,ERR_R_MALLOC_FAILURE);\n\t\tgoto err;\n\t\t}\n\ttime(&tim);\n\tRAND_seed(&tim,sizeof(tim));\n\tRAND_bytes(buf,(int)bytes);\n\tif (top)\n\t\t{\n\t\tif (bit == 0)\n\t\t\t{\n\t\t\tbuf[0]=1;\n\t\t\tbuf[1]|=0x80;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tbuf[0]|=(3<<(bit-1));\n\t\t\tbuf[0]&= ~(mask<<1);\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tbuf[0]|=(1<<bit);\n\t\tbuf[0]&= ~(mask<<1);\n\t\t}\n\tif (bottom)\n\t\tbuf[bytes-1]|=1;\n\tif (!BN_bin2bn(buf,bytes,rnd)) goto err;\n\tret=1;\nerr:\n\tif (buf != NULL)\n\t\t{\n\t\tmemset(buf,0,bytes);\n\t\tFree(buf);\n\t\t}\n\treturn(ret);\n\t}', 'BIGNUM *BN_bin2bn(unsigned char *s, int len, BIGNUM *ret)\n\t{\n\tunsigned int i,m;\n\tunsigned int n;\n\tBN_ULONG l;\n\tif (ret == NULL) ret=BN_new();\n\tif (ret == NULL) return(NULL);\n\tl=0;\n\tn=len;\n\tif (n == 0)\n\t\t{\n\t\tret->top=0;\n\t\treturn(ret);\n\t\t}\n\tif (bn_expand(ret,(int)(n+2)*8) == NULL)\n\t\treturn(NULL);\n\ti=((n-1)/BN_BYTES)+1;\n\tm=((n-1)%(BN_BYTES));\n\tret->top=i;\n\twhile (n-- > 0)\n\t\t{\n\t\tl=(l<<8L)| *(s++);\n\t\tif (m-- == 0)\n\t\t\t{\n\t\t\tret->d[--i]=l;\n\t\t\tl=0;\n\t\t\tm=BN_BYTES-1;\n\t\t\t}\n\t\t}\n\tbn_fix_top(ret);\n\treturn(ret);\n\t}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n\t{\n\tBN_ULONG *A,*B,*a;\n\tint i,j;\n\tbn_check_top(b);\n\tif (words > b->max)\n\t\t{\n\t\tbn_check_top(b);\n\t\tif (BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_EXPAND2,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n\t\t\treturn(NULL);\n\t\t\t}\n\t\ta=A=(BN_ULONG *)Malloc(sizeof(BN_ULONG)*(words+1));\n\t\tif (A == NULL)\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_EXPAND2,ERR_R_MALLOC_FAILURE);\n\t\t\treturn(NULL);\n\t\t\t}\nmemset(A,0x5c,sizeof(BN_ULONG)*(words+1));\n#if 1\n\t\tB=b->d;\n\t\tif (B != NULL)\n\t\t\t{\n\t\t\tfor (i=b->top&(~7); i>0; i-=8)\n\t\t\t\t{\n\t\t\t\tA[0]=B[0]; A[1]=B[1]; A[2]=B[2]; A[3]=B[3];\n\t\t\t\tA[4]=B[4]; A[5]=B[5]; A[6]=B[6]; A[7]=B[7];\n\t\t\t\tA+=8;\n\t\t\t\tB+=8;\n\t\t\t\t}\n\t\t\tswitch (b->top&7)\n\t\t\t\t{\n\t\t\tcase 7:\n\t\t\t\tA[6]=B[6];\n\t\t\tcase 6:\n\t\t\t\tA[5]=B[5];\n\t\t\tcase 5:\n\t\t\t\tA[4]=B[4];\n\t\t\tcase 4:\n\t\t\t\tA[3]=B[3];\n\t\t\tcase 3:\n\t\t\t\tA[2]=B[2];\n\t\t\tcase 2:\n\t\t\t\tA[1]=B[1];\n\t\t\tcase 1:\n\t\t\t\tA[0]=B[0];\n\t\t\tcase 0:\n\t\t\t\t;\n\t\t\t\t}\n\t\t\tFree(b->d);\n\t\t\t}\n\t\tb->d=a;\n\t\tb->max=words;\n\t\tB= &(b->d[b->top]);\n\t\tj=(b->max - b->top) & ~7;\n\t\tfor (i=0; i<j; i+=8)\n\t\t\t{\n\t\t\tB[0]=0; B[1]=0; B[2]=0; B[3]=0;\n\t\t\tB[4]=0; B[5]=0; B[6]=0; B[7]=0;\n\t\t\tB+=8;\n\t\t\t}\n\t\tj=(b->max - b->top) & 7;\n\t\tfor (i=0; i<j; i++)\n\t\t\t{\n\t\t\tB[0]=0;\n\t\t\tB++;\n\t\t\t}\n#else\n\t\t\tmemcpy(a->d,b->d,sizeof(b->d[0])*b->top);\n#endif\n\t\t}\n\treturn(b);\n\t}']
36,411
0
https://github.com/openssl/openssl/blob/f006217bb628d05a2d5b866ff252bd94e3477e1f/crypto/bn/bn_lib.c/#L351
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *A, *a = NULL; const BN_ULONG *B; int i; bn_check_top(b); if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return (NULL); } if (BN_get_flags(b,BN_FLG_SECURE)) a = A = OPENSSL_secure_malloc(words * sizeof(*a)); else a = A = OPENSSL_malloc(words * sizeof(*a)); if (A == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return (NULL); } #ifdef PURIFY memset(a, 0, sizeof(*a) * words); #endif #if 1 B = b->d; if (B != NULL) { for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) { BN_ULONG a0, a1, a2, a3; a0 = B[0]; a1 = B[1]; a2 = B[2]; a3 = B[3]; A[0] = a0; A[1] = a1; A[2] = a2; A[3] = a3; } switch (b->top & 3) { case 3: A[2] = B[2]; case 2: A[1] = B[1]; case 1: A[0] = B[0]; case 0: ; } } #else memset(A, 0, sizeof(*A) * words); memcpy(A, b->d, sizeof(b->d[0]) * b->top); #endif return (a); }
['int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group,\n EC_POINT *point,\n const BIGNUM *x_, int y_bit,\n BN_CTX *ctx)\n{\n BN_CTX *new_ctx = NULL;\n BIGNUM *tmp1, *tmp2, *x, *y;\n int ret = 0;\n ERR_clear_error();\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n y_bit = (y_bit != 0);\n BN_CTX_start(ctx);\n tmp1 = BN_CTX_get(ctx);\n tmp2 = BN_CTX_get(ctx);\n x = BN_CTX_get(ctx);\n y = BN_CTX_get(ctx);\n if (y == NULL)\n goto err;\n if (!BN_nnmod(x, x_, group->field, ctx))\n goto err;\n if (group->meth->field_decode == 0) {\n if (!group->meth->field_sqr(group, tmp2, x_, ctx))\n goto err;\n if (!group->meth->field_mul(group, tmp1, tmp2, x_, ctx))\n goto err;\n } else {\n if (!BN_mod_sqr(tmp2, x_, group->field, ctx))\n goto err;\n if (!BN_mod_mul(tmp1, tmp2, x_, group->field, ctx))\n goto err;\n }\n if (group->a_is_minus3) {\n if (!BN_mod_lshift1_quick(tmp2, x, group->field))\n goto err;\n if (!BN_mod_add_quick(tmp2, tmp2, x, group->field))\n goto err;\n if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n } else {\n if (group->meth->field_decode) {\n if (!group->meth->field_decode(group, tmp2, group->a, ctx))\n goto err;\n if (!BN_mod_mul(tmp2, tmp2, x, group->field, ctx))\n goto err;\n } else {\n if (!group->meth->field_mul(group, tmp2, group->a, x, ctx))\n goto err;\n }\n if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n }\n if (group->meth->field_decode) {\n if (!group->meth->field_decode(group, tmp2, group->b, ctx))\n goto err;\n if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n } else {\n if (!BN_mod_add_quick(tmp1, tmp1, group->b, group->field))\n goto err;\n }\n if (!BN_mod_sqrt(y, tmp1, group->field, ctx)) {\n unsigned long err = ERR_peek_last_error();\n if (ERR_GET_LIB(err) == ERR_LIB_BN\n && ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) {\n ERR_clear_error();\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSED_POINT);\n } else\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n ERR_R_BN_LIB);\n goto err;\n }\n if (y_bit != BN_is_odd(y)) {\n if (BN_is_zero(y)) {\n int kron;\n kron = BN_kronecker(x, group->field, ctx);\n if (kron == -2)\n goto err;\n if (kron == 1)\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSION_BIT);\n else\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSED_POINT);\n goto err;\n }\n if (!BN_usub(y, group->field, y))\n goto err;\n }\n if (y_bit != BN_is_odd(y)) {\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx))\n goto err;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return (1);\n}', 'int BN_mod_lshift1_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *m)\n{\n if (!BN_lshift1(r, a))\n return 0;\n bn_check_top(r);\n if (BN_cmp(r, m) >= 0)\n return BN_sub(r, r, m);\n return 1;\n}', 'int BN_lshift1(BIGNUM *r, const BIGNUM *a)\n{\n register BN_ULONG *ap, *rp, t, c;\n int i;\n bn_check_top(r);\n bn_check_top(a);\n if (r != a) {\n r->neg = a->neg;\n if (bn_wexpand(r, a->top + 1) == NULL)\n return (0);\n r->top = a->top;\n } else {\n if (bn_wexpand(r, a->top + 1) == NULL)\n return (0);\n }\n ap = a->d;\n rp = r->d;\n c = 0;\n for (i = 0; i < a->top; i++) {\n t = *(ap++);\n *(rp++) = ((t << 1) | c) & BN_MASK2;\n c = (t & BN_TBIT) ? 1 : 0;\n }\n if (c) {\n *rp = 1;\n r->top++;\n }\n bn_check_top(r);\n return (1);\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *A, *a = NULL;\n const BN_ULONG *B;\n int i;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return (NULL);\n }\n if (BN_get_flags(b,BN_FLG_SECURE))\n a = A = OPENSSL_secure_malloc(words * sizeof(*a));\n else\n a = A = OPENSSL_malloc(words * sizeof(*a));\n if (A == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n#ifdef PURIFY\n memset(a, 0, sizeof(*a) * words);\n#endif\n#if 1\n B = b->d;\n if (B != NULL) {\n for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {\n BN_ULONG a0, a1, a2, a3;\n a0 = B[0];\n a1 = B[1];\n a2 = B[2];\n a3 = B[3];\n A[0] = a0;\n A[1] = a1;\n A[2] = a2;\n A[3] = a3;\n }\n switch (b->top & 3) {\n case 3:\n A[2] = B[2];\n case 2:\n A[1] = B[1];\n case 1:\n A[0] = B[0];\n case 0:\n ;\n }\n }\n#else\n memset(A, 0, sizeof(*A) * words);\n memcpy(A, b->d, sizeof(b->d[0]) * b->top);\n#endif\n return (a);\n}']
36,412
0
https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/crypto/lhash/lhash.c/#L370
static void contract(LHASH *lh) { LHASH_NODE **n,*n1,*np; np=lh->b[lh->p+lh->pmax-1]; lh->b[lh->p+lh->pmax-1]=NULL; if (lh->p == 0) { n=(LHASH_NODE **)Realloc((char *)lh->b, (unsigned int)(sizeof(LHASH_NODE *)*lh->pmax)); if (n == NULL) { lh->error++; return; } lh->num_contract_reallocs++; lh->num_alloc_nodes/=2; lh->pmax/=2; lh->p=lh->pmax-1; lh->b=n; } else lh->p--; lh->num_nodes--; lh->num_contracts++; n1=lh->b[(int)lh->p]; if (n1 == NULL) lh->b[(int)lh->p]=np; else { while (n1->next != NULL) n1=n1->next; n1->next=np; } }
['SSL *SSL_new(SSL_CTX *ctx)\n\t{\n\tSSL *s;\n\tif (ctx == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_NEW,SSL_R_NULL_SSL_CTX);\n\t\treturn(NULL);\n\t\t}\n\tif (ctx->method == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_NEW,SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);\n\t\treturn(NULL);\n\t\t}\n\ts=(SSL *)Malloc(sizeof(SSL));\n\tif (s == NULL) goto err;\n\tmemset(s,0,sizeof(SSL));\n\tif (ctx->default_cert != NULL)\n\t\t{\n\t\tCRYPTO_add(&ctx->default_cert->references,1,\n\t\t\t CRYPTO_LOCK_SSL_CERT);\n\t\ts->cert=ctx->default_cert;\n\t\t}\n\telse\n\t\ts->cert=NULL;\n\ts->verify_mode=ctx->verify_mode;\n\ts->verify_callback=ctx->default_verify_callback;\n\tCRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);\n\ts->ctx=ctx;\n\ts->verify_result=X509_V_OK;\n\ts->method=ctx->method;\n\tif (!s->method->ssl_new(s))\n\t\t{\n\t\tSSL_CTX_free(ctx);\n\t\tFree(s);\n\t\tgoto err;\n\t\t}\n\ts->quiet_shutdown=ctx->quiet_shutdown;\n\ts->references=1;\n\ts->server=(ctx->method->ssl_accept == ssl_undefined_function)?0:1;\n\ts->options=ctx->options;\n\tSSL_clear(s);\n\tCRYPTO_new_ex_data(ssl_meth,(char *)s,&s->ex_data);\n\treturn(s);\nerr:\n\tSSLerr(SSL_F_SSL_NEW,ERR_R_MALLOC_FAILURE);\n\treturn(NULL);\n\t}', 'void SSL_free(SSL *s)\n\t{\n\tint i;\n\tif(s == NULL)\n\t return;\n\ti=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL);\n#ifdef REF_PRINT\n\tREF_PRINT("SSL",s);\n#endif\n\tif (i > 0) return;\n#ifdef REF_CHECK\n\tif (i < 0)\n\t\t{\n\t\tfprintf(stderr,"SSL_free, bad reference count\\n");\n\t\tabort();\n\t\t}\n#endif\n\tCRYPTO_free_ex_data(ssl_meth,(char *)s,&s->ex_data);\n\tif (s->bbio != NULL)\n\t\t{\n\t\tif (s->bbio == s->wbio)\n\t\t\t{\n\t\t\ts->wbio=BIO_pop(s->wbio);\n\t\t\t}\n\t\tBIO_free(s->bbio);\n\t\ts->bbio=NULL;\n\t\t}\n\tif (s->rbio != NULL)\n\t\tBIO_free_all(s->rbio);\n\tif ((s->wbio != NULL) && (s->wbio != s->rbio))\n\t\tBIO_free_all(s->wbio);\n\tif (s->init_buf != NULL) BUF_MEM_free(s->init_buf);\n\tif (s->cipher_list != NULL) sk_SSL_CIPHER_free(s->cipher_list);\n\tif (s->cipher_list_by_id != NULL) sk_SSL_CIPHER_free(s->cipher_list_by_id);\n\tif (s->session != NULL)\n\t\t{\n\t\tssl_clear_bad_session(s);\n\t\tSSL_SESSION_free(s->session);\n\t\t}\n\tssl_clear_cipher_ctx(s);\n\tif (s->cert != NULL) ssl_cert_free(s->cert);\n\tif (s->ctx) SSL_CTX_free(s->ctx);\n\tif (s->client_CA != NULL)\n\t\tsk_X509_NAME_pop_free(s->client_CA,X509_NAME_free);\n\tif (s->method != NULL) s->method->ssl_free(s);\n\tFree((char *)s);\n\t}', 'int ssl_clear_bad_session(SSL *s)\n\t{\n\tif (\t(s->session != NULL) &&\n\t\t!(s->shutdown & SSL_SENT_SHUTDOWN) &&\n\t\t!(SSL_in_init(s) || SSL_in_before(s)))\n\t\t{\n\t\tSSL_CTX_remove_session(s->ctx,s->session);\n\t\treturn(1);\n\t\t}\n\telse\n\t\treturn(0);\n\t}', 'int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)\n\t{\n\tSSL_SESSION *r;\n\tint ret=0;\n\tif ((c != NULL) && (c->session_id_length != 0))\n\t\t{\n\t\tCRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);\n\t\tr=(SSL_SESSION *)lh_delete(ctx->sessions,(char *)c);\n\t\tif (r != NULL)\n\t\t\t{\n\t\t\tret=1;\n\t\t\tSSL_SESSION_list_remove(ctx,c);\n\t\t\t}\n\t\tCRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n\t\tif (ret)\n\t\t\t{\n\t\t\tr->not_resumable=1;\n\t\t\tif (ctx->remove_session_cb != NULL)\n\t\t\t\tctx->remove_session_cb(ctx,r);\n\t\t\tSSL_SESSION_free(r);\n\t\t\t}\n\t\t}\n\telse\n\t\tret=0;\n\treturn(ret);\n\t}', 'char *lh_delete(LHASH *lh, char *data)\n\t{\n\tunsigned long hash;\n\tLHASH_NODE *nn,**rn;\n\tchar *ret;\n\tlh->error=0;\n\trn=getrn(lh,data,&hash);\n\tif (*rn == NULL)\n\t\t{\n\t\tlh->num_no_delete++;\n\t\treturn(NULL);\n\t\t}\n\telse\n\t\t{\n\t\tnn= *rn;\n\t\t*rn=nn->next;\n\t\tret=nn->data;\n\t\tFree((char *)nn);\n\t\tlh->num_delete++;\n\t\t}\n\tlh->num_items--;\n\tif ((lh->num_nodes > MIN_NODES) &&\n\t\t(lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes)))\n\t\tcontract(lh);\n\treturn(ret);\n\t}', 'static void contract(LHASH *lh)\n\t{\n\tLHASH_NODE **n,*n1,*np;\n\tnp=lh->b[lh->p+lh->pmax-1];\n\tlh->b[lh->p+lh->pmax-1]=NULL;\n\tif (lh->p == 0)\n\t\t{\n\t\tn=(LHASH_NODE **)Realloc((char *)lh->b,\n\t\t\t(unsigned int)(sizeof(LHASH_NODE *)*lh->pmax));\n\t\tif (n == NULL)\n\t\t\t{\n\t\t\tlh->error++;\n\t\t\treturn;\n\t\t\t}\n\t\tlh->num_contract_reallocs++;\n\t\tlh->num_alloc_nodes/=2;\n\t\tlh->pmax/=2;\n\t\tlh->p=lh->pmax-1;\n\t\tlh->b=n;\n\t\t}\n\telse\n\t\tlh->p--;\n\tlh->num_nodes--;\n\tlh->num_contracts++;\n\tn1=lh->b[(int)lh->p];\n\tif (n1 == NULL)\n\t\tlh->b[(int)lh->p]=np;\n\telse\n\t\t{\n\t\twhile (n1->next != NULL)\n\t\t\tn1=n1->next;\n\t\tn1->next=np;\n\t\t}\n\t}']
36,413
0
https://github.com/openssl/openssl/blob/a8140a42f5ee9e4e1423b5b6b319dc4657659f6f/crypto/bn/bn_conv.c/#L171
int BN_hex2bn(BIGNUM **bn, const char *a) { BIGNUM *ret = NULL; BN_ULONG l = 0; int neg = 0, h, m, i, j, k, c; int num; if (a == NULL || *a == '\0') return 0; if (*a == '-') { neg = 1; a++; } for (i = 0; i <= INT_MAX / 4 && ossl_isxdigit(a[i]); i++) continue; if (i == 0 || i > INT_MAX / 4) goto err; num = i + neg; if (bn == NULL) return num; if (*bn == NULL) { if ((ret = BN_new()) == NULL) return 0; } else { ret = *bn; BN_zero(ret); } if (bn_expand(ret, i * 4) == NULL) goto err; j = i; m = 0; h = 0; while (j > 0) { m = (BN_BYTES * 2 <= j) ? BN_BYTES * 2 : j; l = 0; for (;;) { c = a[j - m]; k = OPENSSL_hexchar2int(c); if (k < 0) k = 0; l = (l << 4) | k; if (--m <= 0) { ret->d[h++] = l; break; } } j -= BN_BYTES * 2; } ret->top = h; bn_correct_top(ret); *bn = ret; bn_check_top(ret); if (ret->top != 0) ret->neg = neg; return num; err: if (*bn == NULL) BN_free(ret); return 0; }
['static int file_modexp(STANZA *s)\n{\n BIGNUM *a = NULL, *e = NULL, *m = NULL, *mod_exp = NULL, *ret = NULL;\n BIGNUM *b = NULL, *c = NULL, *d = NULL;\n int st = 0;\n if (!TEST_ptr(a = getBN(s, "A"))\n || !TEST_ptr(e = getBN(s, "E"))\n || !TEST_ptr(m = getBN(s, "M"))\n || !TEST_ptr(mod_exp = getBN(s, "ModExp"))\n || !TEST_ptr(ret = BN_new())\n || !TEST_ptr(d = BN_new()))\n goto err;\n if (!TEST_true(BN_mod_exp(ret, a, e, m, ctx))\n || !equalBN("A ^ E (mod M)", mod_exp, ret))\n goto err;\n if (BN_is_odd(m)) {\n if (!TEST_true(BN_mod_exp_mont(ret, a, e, m, ctx, NULL))\n || !equalBN("A ^ E (mod M) (mont)", mod_exp, ret)\n || !TEST_true(BN_mod_exp_mont_consttime(ret, a, e, m,\n ctx, NULL))\n || !equalBN("A ^ E (mod M) (mont const", mod_exp, ret))\n goto err;\n }\n BN_hex2bn(&a, "050505050505");\n BN_hex2bn(&b, "02");\n BN_hex2bn(&c,\n "4141414141414141414141274141414141414141414141414141414141414141"\n "4141414141414141414141414141414141414141414141414141414141414141"\n "4141414141414141414141800000000000000000000000000000000000000000"\n "0000000000000000000000000000000000000000000000000000000000000000"\n "0000000000000000000000000000000000000000000000000000000000000000"\n "0000000000000000000000000000000000000000000000000000000001");\n if (!TEST_true(BN_mod_exp(d, a, b, c, ctx))\n || !TEST_true(BN_mul(e, a, a, ctx))\n || !TEST_BN_eq(d, e))\n goto err;\n st = 1;\n err:\n BN_free(a);\n BN_free(b);\n BN_free(c);\n BN_free(d);\n BN_free(e);\n BN_free(m);\n BN_free(mod_exp);\n BN_free(ret);\n return st;\n}', "int BN_hex2bn(BIGNUM **bn, const char *a)\n{\n BIGNUM *ret = NULL;\n BN_ULONG l = 0;\n int neg = 0, h, m, i, j, k, c;\n int num;\n if (a == NULL || *a == '\\0')\n return 0;\n if (*a == '-') {\n neg = 1;\n a++;\n }\n for (i = 0; i <= INT_MAX / 4 && ossl_isxdigit(a[i]); i++)\n continue;\n if (i == 0 || i > INT_MAX / 4)\n goto err;\n num = i + neg;\n if (bn == NULL)\n return num;\n if (*bn == NULL) {\n if ((ret = BN_new()) == NULL)\n return 0;\n } else {\n ret = *bn;\n BN_zero(ret);\n }\n if (bn_expand(ret, i * 4) == NULL)\n goto err;\n j = i;\n m = 0;\n h = 0;\n while (j > 0) {\n m = (BN_BYTES * 2 <= j) ? BN_BYTES * 2 : j;\n l = 0;\n for (;;) {\n c = a[j - m];\n k = OPENSSL_hexchar2int(c);\n if (k < 0)\n k = 0;\n l = (l << 4) | k;\n if (--m <= 0) {\n ret->d[h++] = l;\n break;\n }\n }\n j -= BN_BYTES * 2;\n }\n ret->top = h;\n bn_correct_top(ret);\n *bn = ret;\n bn_check_top(ret);\n if (ret->top != 0)\n ret->neg = neg;\n return num;\n err:\n if (*bn == NULL)\n BN_free(ret);\n return 0;\n}"]
36,414
0
https://github.com/openssl/openssl/blob/f9df0a7775f483c175cda5832360cccd1db6943a/crypto/bn/bn_ctx.c/#L273
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx)\n{\n if (!BN_sqr(r, a, ctx))\n return 0;\n return BN_mod(r, r, m, ctx);\n}', 'int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int max, al;\n int ret = 0;\n BIGNUM *tmp, *rr;\n bn_check_top(a);\n al = a->top;\n if (al <= 0) {\n r->top = 0;\n r->neg = 0;\n return 1;\n }\n BN_CTX_start(ctx);\n rr = (a != r) ? r : BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (rr == NULL || tmp == NULL)\n goto err;\n max = 2 * al;\n if (bn_wexpand(rr, max) == NULL)\n goto err;\n if (al == 4) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[8];\n bn_sqr_normal(rr->d, a->d, 4, t);\n#else\n bn_sqr_comba4(rr->d, a->d);\n#endif\n } else if (al == 8) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[16];\n bn_sqr_normal(rr->d, a->d, 8, t);\n#else\n bn_sqr_comba8(rr->d, a->d);\n#endif\n } else {\n#if defined(BN_RECURSION)\n if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {\n BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];\n bn_sqr_normal(rr->d, a->d, al, t);\n } else {\n int j, k;\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n if (al == j) {\n if (bn_wexpand(tmp, k * 2) == NULL)\n goto err;\n bn_sqr_recursive(rr->d, a->d, al, tmp->d);\n } else {\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n }\n }\n#else\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n#endif\n }\n rr->neg = 0;\n if (a->d[al - 1] == (a->d[al - 1] & BN_MASK2l))\n rr->top = max - 1;\n else\n rr->top = max;\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(rr);\n bn_check_top(tmp);\n BN_CTX_end(ctx);\n return (ret);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return (0);\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return (0);\n }\n if (dv != NULL)\n BN_zero(dv);\n return 1;\n }\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n resp++;\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n resp--;\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return (0);\n}', 'void BN_CTX_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}']
36,415
0
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/ec/ec_mult.c/#L410
int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar, size_t num, const EC_POINT *points[], const BIGNUM *scalars[], BN_CTX *ctx) { BN_CTX *new_ctx = NULL; const EC_POINT *generator = NULL; EC_POINT *tmp = NULL; size_t totalnum; size_t blocksize = 0, numblocks = 0; size_t pre_points_per_block = 0; size_t i, j; int k; int r_is_inverted = 0; int r_is_at_infinity = 1; size_t *wsize = NULL; signed char **wNAF = NULL; size_t *wNAF_len = NULL; size_t max_len = 0; size_t num_val; EC_POINT **val = NULL; EC_POINT **v; EC_POINT ***val_sub = NULL; const EC_PRE_COMP *pre_comp = NULL; int num_scalar = 0; int ret = 0; if (group->meth != r->meth) { ECerr(EC_F_EC_WNAF_MUL, EC_R_INCOMPATIBLE_OBJECTS); return 0; } if ((scalar == NULL) && (num == 0)) { return EC_POINT_set_to_infinity(group, r); } for (i = 0; i < num; i++) { if (group->meth != points[i]->meth) { ECerr(EC_F_EC_WNAF_MUL, EC_R_INCOMPATIBLE_OBJECTS); return 0; } } if (ctx == NULL) { ctx = new_ctx = BN_CTX_new(); if (ctx == NULL) goto err; } if (scalar != NULL) { generator = EC_GROUP_get0_generator(group); if (generator == NULL) { ECerr(EC_F_EC_WNAF_MUL, EC_R_UNDEFINED_GENERATOR); goto err; } pre_comp = EC_EX_DATA_get_data(group->extra_data, ec_pre_comp_dup, ec_pre_comp_free, ec_pre_comp_clear_free); if (pre_comp && pre_comp->numblocks && (EC_POINT_cmp(group, generator, pre_comp->points[0], ctx) == 0)) { blocksize = pre_comp->blocksize; numblocks = (BN_num_bits(scalar) / blocksize) + 1; if (numblocks > pre_comp->numblocks) numblocks = pre_comp->numblocks; pre_points_per_block = (size_t)1 << (pre_comp->w - 1); if (pre_comp->num != (pre_comp->numblocks * pre_points_per_block)) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR); goto err; } } else { pre_comp = NULL; numblocks = 1; num_scalar = 1; } } totalnum = num + numblocks; wsize = OPENSSL_malloc(totalnum * sizeof wsize[0]); wNAF_len = OPENSSL_malloc(totalnum * sizeof wNAF_len[0]); wNAF = OPENSSL_malloc((totalnum + 1) * sizeof wNAF[0]); val_sub = OPENSSL_malloc(totalnum * sizeof val_sub[0]); if (wNAF != NULL) wNAF[0] = NULL; if (wsize == NULL || wNAF_len == NULL || wNAF == NULL || val_sub == NULL) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE); goto err; } num_val = 0; for (i = 0; i < num + num_scalar; i++) { size_t bits; bits = i < num ? BN_num_bits(scalars[i]) : BN_num_bits(scalar); wsize[i] = EC_window_bits_for_scalar_size(bits); num_val += (size_t)1 << (wsize[i] - 1); wNAF[i + 1] = NULL; wNAF[i] = bn_compute_wNAF((i < num ? scalars[i] : scalar), wsize[i], &wNAF_len[i]); if (wNAF[i] == NULL) goto err; if (wNAF_len[i] > max_len) max_len = wNAF_len[i]; } if (numblocks) { if (pre_comp == NULL) { if (num_scalar != 1) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR); goto err; } } else { signed char *tmp_wNAF = NULL; size_t tmp_len = 0; if (num_scalar != 0) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR); goto err; } wsize[num] = pre_comp->w; tmp_wNAF = bn_compute_wNAF(scalar, wsize[num], &tmp_len); if (!tmp_wNAF) goto err; if (tmp_len <= max_len) { numblocks = 1; totalnum = num + 1; wNAF[num] = tmp_wNAF; wNAF[num + 1] = NULL; wNAF_len[num] = tmp_len; if (tmp_len > max_len) max_len = tmp_len; val_sub[num] = pre_comp->points; } else { signed char *pp; EC_POINT **tmp_points; if (tmp_len < numblocks * blocksize) { numblocks = (tmp_len + blocksize - 1) / blocksize; if (numblocks > pre_comp->numblocks) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR); goto err; } totalnum = num + numblocks; } pp = tmp_wNAF; tmp_points = pre_comp->points; for (i = num; i < totalnum; i++) { if (i < totalnum - 1) { wNAF_len[i] = blocksize; if (tmp_len < blocksize) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR); goto err; } tmp_len -= blocksize; } else wNAF_len[i] = tmp_len; wNAF[i + 1] = NULL; wNAF[i] = OPENSSL_malloc(wNAF_len[i]); if (wNAF[i] == NULL) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE); OPENSSL_free(tmp_wNAF); goto err; } memcpy(wNAF[i], pp, wNAF_len[i]); if (wNAF_len[i] > max_len) max_len = wNAF_len[i]; if (*tmp_points == NULL) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR); OPENSSL_free(tmp_wNAF); goto err; } val_sub[i] = tmp_points; tmp_points += pre_points_per_block; pp += blocksize; } OPENSSL_free(tmp_wNAF); } } } val = OPENSSL_malloc((num_val + 1) * sizeof val[0]); if (val == NULL) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE); goto err; } val[num_val] = NULL; v = val; for (i = 0; i < num + num_scalar; i++) { val_sub[i] = v; for (j = 0; j < ((size_t)1 << (wsize[i] - 1)); j++) { *v = EC_POINT_new(group); if (*v == NULL) goto err; v++; } } if (!(v == val + num_val)) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR); goto err; } if ((tmp = EC_POINT_new(group)) == NULL) goto err; for (i = 0; i < num + num_scalar; i++) { if (i < num) { if (!EC_POINT_copy(val_sub[i][0], points[i])) goto err; } else { if (!EC_POINT_copy(val_sub[i][0], generator)) goto err; } if (wsize[i] > 1) { if (!EC_POINT_dbl(group, tmp, val_sub[i][0], ctx)) goto err; for (j = 1; j < ((size_t)1 << (wsize[i] - 1)); j++) { if (!EC_POINT_add (group, val_sub[i][j], val_sub[i][j - 1], tmp, ctx)) goto err; } } } if (!EC_POINTs_make_affine(group, num_val, val, ctx)) goto err; r_is_at_infinity = 1; for (k = max_len - 1; k >= 0; k--) { if (!r_is_at_infinity) { if (!EC_POINT_dbl(group, r, r, ctx)) goto err; } for (i = 0; i < totalnum; i++) { if (wNAF_len[i] > (size_t)k) { int digit = wNAF[i][k]; int is_neg; if (digit) { is_neg = digit < 0; if (is_neg) digit = -digit; if (is_neg != r_is_inverted) { if (!r_is_at_infinity) { if (!EC_POINT_invert(group, r, ctx)) goto err; } r_is_inverted = !r_is_inverted; } if (r_is_at_infinity) { if (!EC_POINT_copy(r, val_sub[i][digit >> 1])) goto err; r_is_at_infinity = 0; } else { if (!EC_POINT_add (group, r, r, val_sub[i][digit >> 1], ctx)) goto err; } } } } } if (r_is_at_infinity) { if (!EC_POINT_set_to_infinity(group, r)) goto err; } else { if (r_is_inverted) if (!EC_POINT_invert(group, r, ctx)) goto err; } ret = 1; err: BN_CTX_free(new_ctx); EC_POINT_free(tmp); OPENSSL_free(wsize); OPENSSL_free(wNAF_len); if (wNAF != NULL) { signed char **w; for (w = wNAF; *w != NULL; w++) OPENSSL_free(*w); OPENSSL_free(wNAF); } if (val != NULL) { for (v = val; *v != NULL; v++) EC_POINT_clear_free(*v); OPENSSL_free(val); } OPENSSL_free(val_sub); return ret; }
['int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,\n size_t num, const EC_POINT *points[], const BIGNUM *scalars[],\n BN_CTX *ctx)\n{\n BN_CTX *new_ctx = NULL;\n const EC_POINT *generator = NULL;\n EC_POINT *tmp = NULL;\n size_t totalnum;\n size_t blocksize = 0, numblocks = 0;\n size_t pre_points_per_block = 0;\n size_t i, j;\n int k;\n int r_is_inverted = 0;\n int r_is_at_infinity = 1;\n size_t *wsize = NULL;\n signed char **wNAF = NULL;\n size_t *wNAF_len = NULL;\n size_t max_len = 0;\n size_t num_val;\n EC_POINT **val = NULL;\n EC_POINT **v;\n EC_POINT ***val_sub = NULL;\n const EC_PRE_COMP *pre_comp = NULL;\n int num_scalar = 0;\n int ret = 0;\n if (group->meth != r->meth) {\n ECerr(EC_F_EC_WNAF_MUL, EC_R_INCOMPATIBLE_OBJECTS);\n return 0;\n }\n if ((scalar == NULL) && (num == 0)) {\n return EC_POINT_set_to_infinity(group, r);\n }\n for (i = 0; i < num; i++) {\n if (group->meth != points[i]->meth) {\n ECerr(EC_F_EC_WNAF_MUL, EC_R_INCOMPATIBLE_OBJECTS);\n return 0;\n }\n }\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n goto err;\n }\n if (scalar != NULL) {\n generator = EC_GROUP_get0_generator(group);\n if (generator == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, EC_R_UNDEFINED_GENERATOR);\n goto err;\n }\n pre_comp =\n EC_EX_DATA_get_data(group->extra_data, ec_pre_comp_dup,\n ec_pre_comp_free, ec_pre_comp_clear_free);\n if (pre_comp && pre_comp->numblocks\n && (EC_POINT_cmp(group, generator, pre_comp->points[0], ctx) ==\n 0)) {\n blocksize = pre_comp->blocksize;\n numblocks = (BN_num_bits(scalar) / blocksize) + 1;\n if (numblocks > pre_comp->numblocks)\n numblocks = pre_comp->numblocks;\n pre_points_per_block = (size_t)1 << (pre_comp->w - 1);\n if (pre_comp->num != (pre_comp->numblocks * pre_points_per_block)) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n } else {\n pre_comp = NULL;\n numblocks = 1;\n num_scalar = 1;\n }\n }\n totalnum = num + numblocks;\n wsize = OPENSSL_malloc(totalnum * sizeof wsize[0]);\n wNAF_len = OPENSSL_malloc(totalnum * sizeof wNAF_len[0]);\n wNAF = OPENSSL_malloc((totalnum + 1) * sizeof wNAF[0]);\n val_sub = OPENSSL_malloc(totalnum * sizeof val_sub[0]);\n if (wNAF != NULL)\n wNAF[0] = NULL;\n if (wsize == NULL || wNAF_len == NULL || wNAF == NULL || val_sub == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n num_val = 0;\n for (i = 0; i < num + num_scalar; i++) {\n size_t bits;\n bits = i < num ? BN_num_bits(scalars[i]) : BN_num_bits(scalar);\n wsize[i] = EC_window_bits_for_scalar_size(bits);\n num_val += (size_t)1 << (wsize[i] - 1);\n wNAF[i + 1] = NULL;\n wNAF[i] =\n bn_compute_wNAF((i < num ? scalars[i] : scalar), wsize[i],\n &wNAF_len[i]);\n if (wNAF[i] == NULL)\n goto err;\n if (wNAF_len[i] > max_len)\n max_len = wNAF_len[i];\n }\n if (numblocks) {\n if (pre_comp == NULL) {\n if (num_scalar != 1) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n } else {\n signed char *tmp_wNAF = NULL;\n size_t tmp_len = 0;\n if (num_scalar != 0) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n wsize[num] = pre_comp->w;\n tmp_wNAF = bn_compute_wNAF(scalar, wsize[num], &tmp_len);\n if (!tmp_wNAF)\n goto err;\n if (tmp_len <= max_len) {\n numblocks = 1;\n totalnum = num + 1;\n wNAF[num] = tmp_wNAF;\n wNAF[num + 1] = NULL;\n wNAF_len[num] = tmp_len;\n if (tmp_len > max_len)\n max_len = tmp_len;\n val_sub[num] = pre_comp->points;\n } else {\n signed char *pp;\n EC_POINT **tmp_points;\n if (tmp_len < numblocks * blocksize) {\n numblocks = (tmp_len + blocksize - 1) / blocksize;\n if (numblocks > pre_comp->numblocks) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n totalnum = num + numblocks;\n }\n pp = tmp_wNAF;\n tmp_points = pre_comp->points;\n for (i = num; i < totalnum; i++) {\n if (i < totalnum - 1) {\n wNAF_len[i] = blocksize;\n if (tmp_len < blocksize) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n tmp_len -= blocksize;\n } else\n wNAF_len[i] = tmp_len;\n wNAF[i + 1] = NULL;\n wNAF[i] = OPENSSL_malloc(wNAF_len[i]);\n if (wNAF[i] == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);\n OPENSSL_free(tmp_wNAF);\n goto err;\n }\n memcpy(wNAF[i], pp, wNAF_len[i]);\n if (wNAF_len[i] > max_len)\n max_len = wNAF_len[i];\n if (*tmp_points == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n OPENSSL_free(tmp_wNAF);\n goto err;\n }\n val_sub[i] = tmp_points;\n tmp_points += pre_points_per_block;\n pp += blocksize;\n }\n OPENSSL_free(tmp_wNAF);\n }\n }\n }\n val = OPENSSL_malloc((num_val + 1) * sizeof val[0]);\n if (val == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n val[num_val] = NULL;\n v = val;\n for (i = 0; i < num + num_scalar; i++) {\n val_sub[i] = v;\n for (j = 0; j < ((size_t)1 << (wsize[i] - 1)); j++) {\n *v = EC_POINT_new(group);\n if (*v == NULL)\n goto err;\n v++;\n }\n }\n if (!(v == val + num_val)) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if ((tmp = EC_POINT_new(group)) == NULL)\n goto err;\n for (i = 0; i < num + num_scalar; i++) {\n if (i < num) {\n if (!EC_POINT_copy(val_sub[i][0], points[i]))\n goto err;\n } else {\n if (!EC_POINT_copy(val_sub[i][0], generator))\n goto err;\n }\n if (wsize[i] > 1) {\n if (!EC_POINT_dbl(group, tmp, val_sub[i][0], ctx))\n goto err;\n for (j = 1; j < ((size_t)1 << (wsize[i] - 1)); j++) {\n if (!EC_POINT_add\n (group, val_sub[i][j], val_sub[i][j - 1], tmp, ctx))\n goto err;\n }\n }\n }\n if (!EC_POINTs_make_affine(group, num_val, val, ctx))\n goto err;\n r_is_at_infinity = 1;\n for (k = max_len - 1; k >= 0; k--) {\n if (!r_is_at_infinity) {\n if (!EC_POINT_dbl(group, r, r, ctx))\n goto err;\n }\n for (i = 0; i < totalnum; i++) {\n if (wNAF_len[i] > (size_t)k) {\n int digit = wNAF[i][k];\n int is_neg;\n if (digit) {\n is_neg = digit < 0;\n if (is_neg)\n digit = -digit;\n if (is_neg != r_is_inverted) {\n if (!r_is_at_infinity) {\n if (!EC_POINT_invert(group, r, ctx))\n goto err;\n }\n r_is_inverted = !r_is_inverted;\n }\n if (r_is_at_infinity) {\n if (!EC_POINT_copy(r, val_sub[i][digit >> 1]))\n goto err;\n r_is_at_infinity = 0;\n } else {\n if (!EC_POINT_add\n (group, r, r, val_sub[i][digit >> 1], ctx))\n goto err;\n }\n }\n }\n }\n }\n if (r_is_at_infinity) {\n if (!EC_POINT_set_to_infinity(group, r))\n goto err;\n } else {\n if (r_is_inverted)\n if (!EC_POINT_invert(group, r, ctx))\n goto err;\n }\n ret = 1;\n err:\n BN_CTX_free(new_ctx);\n EC_POINT_free(tmp);\n OPENSSL_free(wsize);\n OPENSSL_free(wNAF_len);\n if (wNAF != NULL) {\n signed char **w;\n for (w = wNAF; *w != NULL; w++)\n OPENSSL_free(*w);\n OPENSSL_free(wNAF);\n }\n if (val != NULL) {\n for (v = val; *v != NULL; v++)\n EC_POINT_clear_free(*v);\n OPENSSL_free(val);\n }\n OPENSSL_free(val_sub);\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}']
36,416
0
https://github.com/openssl/openssl/blob/18e1e302452e6dea4500b6f981cee7e151294dea/crypto/bn/bn_ctx.c/#L268
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['int ec_GFp_nist_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,\n BN_CTX *ctx)\n{\n int ret = 0;\n BN_CTX *ctx_new = NULL;\n if (!group || !r || !a) {\n ECerr(EC_F_EC_GFP_NIST_FIELD_SQR, EC_R_PASSED_NULL_PARAMETER);\n goto err;\n }\n if (!ctx)\n if ((ctx_new = ctx = BN_CTX_new()) == NULL)\n goto err;\n if (!BN_sqr(r, a, ctx))\n goto err;\n if (!group->field_mod_func(r, r, group->field, ctx))\n goto err;\n ret = 1;\n err:\n BN_CTX_free(ctx_new);\n return ret;\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_start(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_start()", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG("LEAVE BN_CTX_start()", ctx);\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG("ENTER BN_CTX_get()", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ret->flags &= (~BN_FLG_CONSTTIME);\n ctx->used++;\n CTXDBG("LEAVE BN_CTX_get()", ctx);\n return ret;\n}', '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}']
36,417
0
https://github.com/libav/libav/blob/452a398fd6bdca3f301c5c8af3bc241bc16a777e/libavcodec/dnxhdenc.c/#L187
static int dnxhd_init_rc(DNXHDEncContext *ctx) { CHECKED_ALLOCZ(ctx->mb_rc, 8160*ctx->m.avctx->qmax*sizeof(RCEntry)); if (ctx->m.avctx->mb_decision != FF_MB_DECISION_RD) CHECKED_ALLOCZ(ctx->mb_cmp, ctx->m.mb_num*sizeof(RCCMPEntry)); ctx->frame_bits = (ctx->cid_table->coding_unit_size - 640 - 4) * 8; ctx->qscale = 1; ctx->lambda = 2<<LAMBDA_FRAC_BITS; return 0; fail: return -1; }
['static int dnxhd_encode_init(AVCodecContext *avctx)\n{\n DNXHDEncContext *ctx = avctx->priv_data;\n int i, index;\n ctx->cid = ff_dnxhd_find_cid(avctx);\n if (!ctx->cid || avctx->pix_fmt != PIX_FMT_YUV422P) {\n av_log(avctx, AV_LOG_ERROR, "video parameters incompatible with DNxHD\\n");\n return -1;\n }\n av_log(avctx, AV_LOG_DEBUG, "cid %d\\n", ctx->cid);\n index = ff_dnxhd_get_cid_table(ctx->cid);\n ctx->cid_table = &ff_dnxhd_cid_table[index];\n ctx->m.avctx = avctx;\n ctx->m.mb_intra = 1;\n ctx->m.h263_aic = 1;\n dsputil_init(&ctx->m.dsp, avctx);\n ff_dct_common_init(&ctx->m);\n if (!ctx->m.dct_quantize)\n ctx->m.dct_quantize = dct_quantize_c;\n ctx->m.mb_height = (avctx->height + 15) / 16;\n ctx->m.mb_width = (avctx->width + 15) / 16;\n if (avctx->flags & CODEC_FLAG_INTERLACED_DCT) {\n ctx->interlaced = 1;\n ctx->m.mb_height /= 2;\n }\n ctx->m.mb_num = ctx->m.mb_height * ctx->m.mb_width;\n if (avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS)\n ctx->m.intra_quant_bias = avctx->intra_quant_bias;\n if (dnxhd_init_qmat(ctx, ctx->m.intra_quant_bias, 0) < 0)\n return -1;\n if (dnxhd_init_vlc(ctx) < 0)\n return -1;\n if (dnxhd_init_rc(ctx) < 0)\n return -1;\n CHECKED_ALLOCZ(ctx->slice_size, ctx->m.mb_height*sizeof(uint32_t));\n CHECKED_ALLOCZ(ctx->mb_bits, ctx->m.mb_num *sizeof(uint16_t));\n CHECKED_ALLOCZ(ctx->mb_qscale, ctx->m.mb_num *sizeof(uint8_t));\n ctx->frame.key_frame = 1;\n ctx->frame.pict_type = FF_I_TYPE;\n ctx->m.avctx->coded_frame = &ctx->frame;\n if (avctx->thread_count > MAX_THREADS || (avctx->thread_count > ctx->m.mb_height)) {\n av_log(avctx, AV_LOG_ERROR, "too many threads\\n");\n return -1;\n }\n ctx->thread[0] = ctx;\n for (i = 1; i < avctx->thread_count; i++) {\n ctx->thread[i] = av_malloc(sizeof(DNXHDEncContext));\n memcpy(ctx->thread[i], ctx, sizeof(DNXHDEncContext));\n }\n for (i = 0; i < avctx->thread_count; i++) {\n ctx->thread[i]->m.start_mb_y = (ctx->m.mb_height*(i ) + avctx->thread_count/2) / avctx->thread_count;\n ctx->thread[i]->m.end_mb_y = (ctx->m.mb_height*(i+1) + avctx->thread_count/2) / avctx->thread_count;\n }\n return 0;\n fail:\n return -1;\n}', 'void dsputil_init(DSPContext* c, AVCodecContext *avctx)\n{\n int i;\n ff_check_alignment();\n#ifdef CONFIG_ENCODERS\n if(avctx->dct_algo==FF_DCT_FASTINT) {\n c->fdct = fdct_ifast;\n c->fdct248 = fdct_ifast248;\n }\n else if(avctx->dct_algo==FF_DCT_FAAN) {\n c->fdct = ff_faandct;\n c->fdct248 = ff_faandct248;\n }\n else {\n c->fdct = ff_jpeg_fdct_islow;\n c->fdct248 = ff_fdct248_islow;\n }\n#endif\n if(avctx->lowres==1){\n if(avctx->idct_algo==FF_IDCT_INT || avctx->idct_algo==FF_IDCT_AUTO || !ENABLE_H264_DECODER){\n c->idct_put= ff_jref_idct4_put;\n c->idct_add= ff_jref_idct4_add;\n }else{\n c->idct_put= ff_h264_lowres_idct_put_c;\n c->idct_add= ff_h264_lowres_idct_add_c;\n }\n c->idct = j_rev_dct4;\n c->idct_permutation_type= FF_NO_IDCT_PERM;\n }else if(avctx->lowres==2){\n c->idct_put= ff_jref_idct2_put;\n c->idct_add= ff_jref_idct2_add;\n c->idct = j_rev_dct2;\n c->idct_permutation_type= FF_NO_IDCT_PERM;\n }else if(avctx->lowres==3){\n c->idct_put= ff_jref_idct1_put;\n c->idct_add= ff_jref_idct1_add;\n c->idct = j_rev_dct1;\n c->idct_permutation_type= FF_NO_IDCT_PERM;\n }else{\n if(avctx->idct_algo==FF_IDCT_INT){\n c->idct_put= ff_jref_idct_put;\n c->idct_add= ff_jref_idct_add;\n c->idct = j_rev_dct;\n c->idct_permutation_type= FF_LIBMPEG2_IDCT_PERM;\n }else if((ENABLE_VP3_DECODER || ENABLE_VP5_DECODER || ENABLE_VP6_DECODER || ENABLE_THEORA_DECODER ) &&\n avctx->idct_algo==FF_IDCT_VP3){\n c->idct_put= ff_vp3_idct_put_c;\n c->idct_add= ff_vp3_idct_add_c;\n c->idct = ff_vp3_idct_c;\n c->idct_permutation_type= FF_NO_IDCT_PERM;\n }else if(avctx->idct_algo==FF_IDCT_WMV2){\n c->idct_put= ff_wmv2_idct_put_c;\n c->idct_add= ff_wmv2_idct_add_c;\n c->idct = ff_wmv2_idct_c;\n c->idct_permutation_type= FF_NO_IDCT_PERM;\n }else if(avctx->idct_algo==FF_IDCT_FAAN){\n c->idct_put= ff_faanidct_put;\n c->idct_add= ff_faanidct_add;\n c->idct = ff_faanidct;\n c->idct_permutation_type= FF_NO_IDCT_PERM;\n }else{\n c->idct_put= ff_simple_idct_put;\n c->idct_add= ff_simple_idct_add;\n c->idct = ff_simple_idct;\n c->idct_permutation_type= FF_NO_IDCT_PERM;\n }\n }\n if (ENABLE_H264_DECODER) {\n c->h264_idct_add= ff_h264_idct_add_c;\n c->h264_idct8_add= ff_h264_idct8_add_c;\n c->h264_idct_dc_add= ff_h264_idct_dc_add_c;\n c->h264_idct8_dc_add= ff_h264_idct8_dc_add_c;\n }\n c->get_pixels = get_pixels_c;\n c->diff_pixels = diff_pixels_c;\n c->put_pixels_clamped = put_pixels_clamped_c;\n c->put_signed_pixels_clamped = put_signed_pixels_clamped_c;\n c->add_pixels_clamped = add_pixels_clamped_c;\n c->add_pixels8 = add_pixels8_c;\n c->add_pixels4 = add_pixels4_c;\n c->sum_abs_dctelem = sum_abs_dctelem_c;\n c->gmc1 = gmc1_c;\n c->gmc = ff_gmc_c;\n c->clear_blocks = clear_blocks_c;\n c->pix_sum = pix_sum_c;\n c->pix_norm1 = pix_norm1_c;\n c->pix_abs[0][0] = pix_abs16_c;\n c->pix_abs[0][1] = pix_abs16_x2_c;\n c->pix_abs[0][2] = pix_abs16_y2_c;\n c->pix_abs[0][3] = pix_abs16_xy2_c;\n c->pix_abs[1][0] = pix_abs8_c;\n c->pix_abs[1][1] = pix_abs8_x2_c;\n c->pix_abs[1][2] = pix_abs8_y2_c;\n c->pix_abs[1][3] = pix_abs8_xy2_c;\n#define dspfunc(PFX, IDX, NUM) \\\n c->PFX ## _pixels_tab[IDX][0] = PFX ## _pixels ## NUM ## _c; \\\n c->PFX ## _pixels_tab[IDX][1] = PFX ## _pixels ## NUM ## _x2_c; \\\n c->PFX ## _pixels_tab[IDX][2] = PFX ## _pixels ## NUM ## _y2_c; \\\n c->PFX ## _pixels_tab[IDX][3] = PFX ## _pixels ## NUM ## _xy2_c\n dspfunc(put, 0, 16);\n dspfunc(put_no_rnd, 0, 16);\n dspfunc(put, 1, 8);\n dspfunc(put_no_rnd, 1, 8);\n dspfunc(put, 2, 4);\n dspfunc(put, 3, 2);\n dspfunc(avg, 0, 16);\n dspfunc(avg_no_rnd, 0, 16);\n dspfunc(avg, 1, 8);\n dspfunc(avg_no_rnd, 1, 8);\n dspfunc(avg, 2, 4);\n dspfunc(avg, 3, 2);\n#undef dspfunc\n c->put_no_rnd_pixels_l2[0]= put_no_rnd_pixels16_l2_c;\n c->put_no_rnd_pixels_l2[1]= put_no_rnd_pixels8_l2_c;\n c->put_tpel_pixels_tab[ 0] = put_tpel_pixels_mc00_c;\n c->put_tpel_pixels_tab[ 1] = put_tpel_pixels_mc10_c;\n c->put_tpel_pixels_tab[ 2] = put_tpel_pixels_mc20_c;\n c->put_tpel_pixels_tab[ 4] = put_tpel_pixels_mc01_c;\n c->put_tpel_pixels_tab[ 5] = put_tpel_pixels_mc11_c;\n c->put_tpel_pixels_tab[ 6] = put_tpel_pixels_mc21_c;\n c->put_tpel_pixels_tab[ 8] = put_tpel_pixels_mc02_c;\n c->put_tpel_pixels_tab[ 9] = put_tpel_pixels_mc12_c;\n c->put_tpel_pixels_tab[10] = put_tpel_pixels_mc22_c;\n c->avg_tpel_pixels_tab[ 0] = avg_tpel_pixels_mc00_c;\n c->avg_tpel_pixels_tab[ 1] = avg_tpel_pixels_mc10_c;\n c->avg_tpel_pixels_tab[ 2] = avg_tpel_pixels_mc20_c;\n c->avg_tpel_pixels_tab[ 4] = avg_tpel_pixels_mc01_c;\n c->avg_tpel_pixels_tab[ 5] = avg_tpel_pixels_mc11_c;\n c->avg_tpel_pixels_tab[ 6] = avg_tpel_pixels_mc21_c;\n c->avg_tpel_pixels_tab[ 8] = avg_tpel_pixels_mc02_c;\n c->avg_tpel_pixels_tab[ 9] = avg_tpel_pixels_mc12_c;\n c->avg_tpel_pixels_tab[10] = avg_tpel_pixels_mc22_c;\n#define dspfunc(PFX, IDX, NUM) \\\n c->PFX ## _pixels_tab[IDX][ 0] = PFX ## NUM ## _mc00_c; \\\n c->PFX ## _pixels_tab[IDX][ 1] = PFX ## NUM ## _mc10_c; \\\n c->PFX ## _pixels_tab[IDX][ 2] = PFX ## NUM ## _mc20_c; \\\n c->PFX ## _pixels_tab[IDX][ 3] = PFX ## NUM ## _mc30_c; \\\n c->PFX ## _pixels_tab[IDX][ 4] = PFX ## NUM ## _mc01_c; \\\n c->PFX ## _pixels_tab[IDX][ 5] = PFX ## NUM ## _mc11_c; \\\n c->PFX ## _pixels_tab[IDX][ 6] = PFX ## NUM ## _mc21_c; \\\n c->PFX ## _pixels_tab[IDX][ 7] = PFX ## NUM ## _mc31_c; \\\n c->PFX ## _pixels_tab[IDX][ 8] = PFX ## NUM ## _mc02_c; \\\n c->PFX ## _pixels_tab[IDX][ 9] = PFX ## NUM ## _mc12_c; \\\n c->PFX ## _pixels_tab[IDX][10] = PFX ## NUM ## _mc22_c; \\\n c->PFX ## _pixels_tab[IDX][11] = PFX ## NUM ## _mc32_c; \\\n c->PFX ## _pixels_tab[IDX][12] = PFX ## NUM ## _mc03_c; \\\n c->PFX ## _pixels_tab[IDX][13] = PFX ## NUM ## _mc13_c; \\\n c->PFX ## _pixels_tab[IDX][14] = PFX ## NUM ## _mc23_c; \\\n c->PFX ## _pixels_tab[IDX][15] = PFX ## NUM ## _mc33_c\n dspfunc(put_qpel, 0, 16);\n dspfunc(put_no_rnd_qpel, 0, 16);\n dspfunc(avg_qpel, 0, 16);\n dspfunc(put_qpel, 1, 8);\n dspfunc(put_no_rnd_qpel, 1, 8);\n dspfunc(avg_qpel, 1, 8);\n dspfunc(put_h264_qpel, 0, 16);\n dspfunc(put_h264_qpel, 1, 8);\n dspfunc(put_h264_qpel, 2, 4);\n dspfunc(put_h264_qpel, 3, 2);\n dspfunc(avg_h264_qpel, 0, 16);\n dspfunc(avg_h264_qpel, 1, 8);\n dspfunc(avg_h264_qpel, 2, 4);\n#undef dspfunc\n c->put_h264_chroma_pixels_tab[0]= put_h264_chroma_mc8_c;\n c->put_h264_chroma_pixels_tab[1]= put_h264_chroma_mc4_c;\n c->put_h264_chroma_pixels_tab[2]= put_h264_chroma_mc2_c;\n c->avg_h264_chroma_pixels_tab[0]= avg_h264_chroma_mc8_c;\n c->avg_h264_chroma_pixels_tab[1]= avg_h264_chroma_mc4_c;\n c->avg_h264_chroma_pixels_tab[2]= avg_h264_chroma_mc2_c;\n c->put_no_rnd_h264_chroma_pixels_tab[0]= put_no_rnd_h264_chroma_mc8_c;\n c->weight_h264_pixels_tab[0]= weight_h264_pixels16x16_c;\n c->weight_h264_pixels_tab[1]= weight_h264_pixels16x8_c;\n c->weight_h264_pixels_tab[2]= weight_h264_pixels8x16_c;\n c->weight_h264_pixels_tab[3]= weight_h264_pixels8x8_c;\n c->weight_h264_pixels_tab[4]= weight_h264_pixels8x4_c;\n c->weight_h264_pixels_tab[5]= weight_h264_pixels4x8_c;\n c->weight_h264_pixels_tab[6]= weight_h264_pixels4x4_c;\n c->weight_h264_pixels_tab[7]= weight_h264_pixels4x2_c;\n c->weight_h264_pixels_tab[8]= weight_h264_pixels2x4_c;\n c->weight_h264_pixels_tab[9]= weight_h264_pixels2x2_c;\n c->biweight_h264_pixels_tab[0]= biweight_h264_pixels16x16_c;\n c->biweight_h264_pixels_tab[1]= biweight_h264_pixels16x8_c;\n c->biweight_h264_pixels_tab[2]= biweight_h264_pixels8x16_c;\n c->biweight_h264_pixels_tab[3]= biweight_h264_pixels8x8_c;\n c->biweight_h264_pixels_tab[4]= biweight_h264_pixels8x4_c;\n c->biweight_h264_pixels_tab[5]= biweight_h264_pixels4x8_c;\n c->biweight_h264_pixels_tab[6]= biweight_h264_pixels4x4_c;\n c->biweight_h264_pixels_tab[7]= biweight_h264_pixels4x2_c;\n c->biweight_h264_pixels_tab[8]= biweight_h264_pixels2x4_c;\n c->biweight_h264_pixels_tab[9]= biweight_h264_pixels2x2_c;\n c->draw_edges = draw_edges_c;\n#ifdef CONFIG_CAVS_DECODER\n ff_cavsdsp_init(c,avctx);\n#endif\n#if defined(CONFIG_VC1_DECODER) || defined(CONFIG_WMV3_DECODER)\n ff_vc1dsp_init(c,avctx);\n#endif\n#if defined(CONFIG_WMV2_DECODER) || defined(CONFIG_VC1_DECODER) || defined(CONFIG_WMV3_DECODER)\n ff_intrax8dsp_init(c,avctx);\n#endif\n#if defined(CONFIG_H264_ENCODER)\n ff_h264dspenc_init(c,avctx);\n#endif\n c->put_mspel_pixels_tab[0]= put_mspel8_mc00_c;\n c->put_mspel_pixels_tab[1]= put_mspel8_mc10_c;\n c->put_mspel_pixels_tab[2]= put_mspel8_mc20_c;\n c->put_mspel_pixels_tab[3]= put_mspel8_mc30_c;\n c->put_mspel_pixels_tab[4]= put_mspel8_mc02_c;\n c->put_mspel_pixels_tab[5]= put_mspel8_mc12_c;\n c->put_mspel_pixels_tab[6]= put_mspel8_mc22_c;\n c->put_mspel_pixels_tab[7]= put_mspel8_mc32_c;\n#define SET_CMP_FUNC(name) \\\n c->name[0]= name ## 16_c;\\\n c->name[1]= name ## 8x8_c;\n SET_CMP_FUNC(hadamard8_diff)\n c->hadamard8_diff[4]= hadamard8_intra16_c;\n SET_CMP_FUNC(dct_sad)\n SET_CMP_FUNC(dct_max)\n#ifdef CONFIG_GPL\n SET_CMP_FUNC(dct264_sad)\n#endif\n c->sad[0]= pix_abs16_c;\n c->sad[1]= pix_abs8_c;\n c->sse[0]= sse16_c;\n c->sse[1]= sse8_c;\n c->sse[2]= sse4_c;\n SET_CMP_FUNC(quant_psnr)\n SET_CMP_FUNC(rd)\n SET_CMP_FUNC(bit)\n c->vsad[0]= vsad16_c;\n c->vsad[4]= vsad_intra16_c;\n c->vsse[0]= vsse16_c;\n c->vsse[4]= vsse_intra16_c;\n c->nsse[0]= nsse16_c;\n c->nsse[1]= nsse8_c;\n#ifdef CONFIG_SNOW_ENCODER\n c->w53[0]= w53_16_c;\n c->w53[1]= w53_8_c;\n c->w97[0]= w97_16_c;\n c->w97[1]= w97_8_c;\n#endif\n c->ssd_int8_vs_int16 = ssd_int8_vs_int16_c;\n c->add_bytes= add_bytes_c;\n c->add_bytes_l2= add_bytes_l2_c;\n c->diff_bytes= diff_bytes_c;\n c->sub_hfyu_median_prediction= sub_hfyu_median_prediction_c;\n c->bswap_buf= bswap_buf;\n#ifdef CONFIG_PNG_DECODER\n c->add_png_paeth_prediction= ff_add_png_paeth_prediction;\n#endif\n c->h264_v_loop_filter_luma= h264_v_loop_filter_luma_c;\n c->h264_h_loop_filter_luma= h264_h_loop_filter_luma_c;\n c->h264_v_loop_filter_chroma= h264_v_loop_filter_chroma_c;\n c->h264_h_loop_filter_chroma= h264_h_loop_filter_chroma_c;\n c->h264_v_loop_filter_chroma_intra= h264_v_loop_filter_chroma_intra_c;\n c->h264_h_loop_filter_chroma_intra= h264_h_loop_filter_chroma_intra_c;\n c->h264_loop_filter_strength= NULL;\n if (ENABLE_ANY_H263) {\n c->h263_h_loop_filter= h263_h_loop_filter_c;\n c->h263_v_loop_filter= h263_v_loop_filter_c;\n }\n c->h261_loop_filter= h261_loop_filter_c;\n c->try_8x8basis= try_8x8basis_c;\n c->add_8x8basis= add_8x8basis_c;\n#ifdef CONFIG_SNOW_DECODER\n c->vertical_compose97i = ff_snow_vertical_compose97i;\n c->horizontal_compose97i = ff_snow_horizontal_compose97i;\n c->inner_add_yblock = ff_snow_inner_add_yblock;\n#endif\n#ifdef CONFIG_VORBIS_DECODER\n c->vorbis_inverse_coupling = vorbis_inverse_coupling;\n#endif\n#ifdef CONFIG_FLAC_ENCODER\n c->flac_compute_autocorr = ff_flac_compute_autocorr;\n#endif\n c->vector_fmul = vector_fmul_c;\n c->vector_fmul_reverse = vector_fmul_reverse_c;\n c->vector_fmul_add_add = ff_vector_fmul_add_add_c;\n c->vector_fmul_window = ff_vector_fmul_window_c;\n c->float_to_int16 = ff_float_to_int16_c;\n c->float_to_int16_interleave = ff_float_to_int16_interleave_c;\n c->add_int16 = add_int16_c;\n c->sub_int16 = sub_int16_c;\n c->scalarproduct_int16 = scalarproduct_int16_c;\n c->shrink[0]= ff_img_copy_plane;\n c->shrink[1]= ff_shrink22;\n c->shrink[2]= ff_shrink44;\n c->shrink[3]= ff_shrink88;\n c->prefetch= just_return;\n memset(c->put_2tap_qpel_pixels_tab, 0, sizeof(c->put_2tap_qpel_pixels_tab));\n memset(c->avg_2tap_qpel_pixels_tab, 0, sizeof(c->avg_2tap_qpel_pixels_tab));\n if (ENABLE_MMX) dsputil_init_mmx (c, avctx);\n if (ENABLE_ARMV4L) dsputil_init_armv4l(c, avctx);\n if (ENABLE_MLIB) dsputil_init_mlib (c, avctx);\n if (ENABLE_VIS) dsputil_init_vis (c, avctx);\n if (ENABLE_ALPHA) dsputil_init_alpha (c, avctx);\n if (ENABLE_POWERPC) dsputil_init_ppc (c, avctx);\n if (ENABLE_MMI) dsputil_init_mmi (c, avctx);\n if (ENABLE_SH4) dsputil_init_sh4 (c, avctx);\n if (ENABLE_BFIN) dsputil_init_bfin (c, avctx);\n for(i=0; i<64; i++){\n if(!c->put_2tap_qpel_pixels_tab[0][i])\n c->put_2tap_qpel_pixels_tab[0][i]= c->put_h264_qpel_pixels_tab[0][i];\n if(!c->avg_2tap_qpel_pixels_tab[0][i])\n c->avg_2tap_qpel_pixels_tab[0][i]= c->avg_h264_qpel_pixels_tab[0][i];\n }\n switch(c->idct_permutation_type){\n case FF_NO_IDCT_PERM:\n for(i=0; i<64; i++)\n c->idct_permutation[i]= i;\n break;\n case FF_LIBMPEG2_IDCT_PERM:\n for(i=0; i<64; i++)\n c->idct_permutation[i]= (i & 0x38) | ((i & 6) >> 1) | ((i & 1) << 2);\n break;\n case FF_SIMPLE_IDCT_PERM:\n for(i=0; i<64; i++)\n c->idct_permutation[i]= simple_mmx_permutation[i];\n break;\n case FF_TRANSPOSE_IDCT_PERM:\n for(i=0; i<64; i++)\n c->idct_permutation[i]= ((i&7)<<3) | (i>>3);\n break;\n case FF_PARTTRANS_IDCT_PERM:\n for(i=0; i<64; i++)\n c->idct_permutation[i]= (i&0x24) | ((i&3)<<3) | ((i>>3)&3);\n break;\n case FF_SSE2_IDCT_PERM:\n for(i=0; i<64; i++)\n c->idct_permutation[i]= (i&0x38) | idct_sse2_row_perm[i&7];\n break;\n default:\n av_log(avctx, AV_LOG_ERROR, "Internal error, IDCT permutation not set\\n");\n }\n}', 'int ff_dct_common_init(MpegEncContext *s)\n{\n s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;\n s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;\n s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;\n s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;\n s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;\n if(s->flags & CODEC_FLAG_BITEXACT)\n s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;\n s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;\n#if defined(HAVE_MMX)\n MPV_common_init_mmx(s);\n#elif defined(ARCH_ALPHA)\n MPV_common_init_axp(s);\n#elif defined(CONFIG_MLIB)\n MPV_common_init_mlib(s);\n#elif defined(HAVE_MMI)\n MPV_common_init_mmi(s);\n#elif defined(ARCH_ARMV4L)\n MPV_common_init_armv4l(s);\n#elif defined(HAVE_ALTIVEC)\n MPV_common_init_altivec(s);\n#elif defined(ARCH_BFIN)\n MPV_common_init_bfin(s);\n#endif\n if(s->alternate_scan){\n ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);\n ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);\n }else{\n ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);\n ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);\n }\n ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);\n ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);\n return 0;\n}', 'void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable){\n int i;\n int end;\n st->scantable= src_scantable;\n for(i=0; i<64; i++){\n int j;\n j = src_scantable[i];\n st->permutated[i] = permutation[j];\n#ifdef ARCH_POWERPC\n st->inverse[j] = i;\n#endif\n }\n end=-1;\n for(i=0; i<64; i++){\n int j;\n j = st->permutated[i];\n if(j>end) end=j;\n st->raster_end[i]= end;\n }\n}', 'static int dnxhd_init_qmat(DNXHDEncContext *ctx, int lbias, int cbias)\n{\n uint16_t weight_matrix[64] = {1,};\n int qscale, i;\n CHECKED_ALLOCZ(ctx->qmatrix_l, (ctx->m.avctx->qmax+1) * 64 * sizeof(int));\n CHECKED_ALLOCZ(ctx->qmatrix_c, (ctx->m.avctx->qmax+1) * 64 * sizeof(int));\n CHECKED_ALLOCZ(ctx->qmatrix_l16, (ctx->m.avctx->qmax+1) * 64 * 2 * sizeof(uint16_t));\n CHECKED_ALLOCZ(ctx->qmatrix_c16, (ctx->m.avctx->qmax+1) * 64 * 2 * sizeof(uint16_t));\n for (i = 1; i < 64; i++) {\n int j = ctx->m.dsp.idct_permutation[ff_zigzag_direct[i]];\n weight_matrix[j] = ctx->cid_table->luma_weight[i];\n }\n ff_convert_matrix(&ctx->m.dsp, ctx->qmatrix_l, ctx->qmatrix_l16, weight_matrix,\n ctx->m.intra_quant_bias, 1, ctx->m.avctx->qmax, 1);\n for (i = 1; i < 64; i++) {\n int j = ctx->m.dsp.idct_permutation[ff_zigzag_direct[i]];\n weight_matrix[j] = ctx->cid_table->chroma_weight[i];\n }\n ff_convert_matrix(&ctx->m.dsp, ctx->qmatrix_c, ctx->qmatrix_c16, weight_matrix,\n ctx->m.intra_quant_bias, 1, ctx->m.avctx->qmax, 1);\n for (qscale = 1; qscale <= ctx->m.avctx->qmax; qscale++) {\n for (i = 0; i < 64; i++) {\n ctx->qmatrix_l [qscale] [i] <<= 2; ctx->qmatrix_c [qscale] [i] <<= 2;\n ctx->qmatrix_l16[qscale][0][i] <<= 2; ctx->qmatrix_l16[qscale][1][i] <<= 2;\n ctx->qmatrix_c16[qscale][0][i] <<= 2; ctx->qmatrix_c16[qscale][1][i] <<= 2;\n }\n }\n return 0;\n fail:\n return -1;\n}', 'static int dnxhd_init_vlc(DNXHDEncContext *ctx)\n{\n int i, j, level, run;\n int max_level = 1<<(ctx->cid_table->bit_depth+2);\n CHECKED_ALLOCZ(ctx->vlc_codes, max_level*4*sizeof(*ctx->vlc_codes));\n CHECKED_ALLOCZ(ctx->vlc_bits, max_level*4*sizeof(*ctx->vlc_bits));\n CHECKED_ALLOCZ(ctx->run_codes, 63*2);\n CHECKED_ALLOCZ(ctx->run_bits, 63);\n ctx->vlc_codes += max_level*2;\n ctx->vlc_bits += max_level*2;\n for (level = -max_level; level < max_level; level++) {\n for (run = 0; run < 2; run++) {\n int index = (level<<1)|run;\n int sign, offset = 0, alevel = level;\n MASK_ABS(sign, alevel);\n if (alevel > 64) {\n offset = (alevel-1)>>6;\n alevel -= offset<<6;\n }\n for (j = 0; j < 257; j++) {\n if (ctx->cid_table->ac_level[j] == alevel &&\n (!offset || (ctx->cid_table->ac_index_flag[j] && offset)) &&\n (!run || (ctx->cid_table->ac_run_flag [j] && run))) {\n assert(!ctx->vlc_codes[index]);\n if (alevel) {\n ctx->vlc_codes[index] = (ctx->cid_table->ac_codes[j]<<1)|(sign&1);\n ctx->vlc_bits [index] = ctx->cid_table->ac_bits[j]+1;\n } else {\n ctx->vlc_codes[index] = ctx->cid_table->ac_codes[j];\n ctx->vlc_bits [index] = ctx->cid_table->ac_bits [j];\n }\n break;\n }\n }\n assert(!alevel || j < 257);\n if (offset) {\n ctx->vlc_codes[index] = (ctx->vlc_codes[index]<<ctx->cid_table->index_bits)|offset;\n ctx->vlc_bits [index]+= ctx->cid_table->index_bits;\n }\n }\n }\n for (i = 0; i < 62; i++) {\n int run = ctx->cid_table->run[i];\n assert(run < 63);\n ctx->run_codes[run] = ctx->cid_table->run_codes[i];\n ctx->run_bits [run] = ctx->cid_table->run_bits[i];\n }\n return 0;\n fail:\n return -1;\n}', 'static int dnxhd_init_rc(DNXHDEncContext *ctx)\n{\n CHECKED_ALLOCZ(ctx->mb_rc, 8160*ctx->m.avctx->qmax*sizeof(RCEntry));\n if (ctx->m.avctx->mb_decision != FF_MB_DECISION_RD)\n CHECKED_ALLOCZ(ctx->mb_cmp, ctx->m.mb_num*sizeof(RCCMPEntry));\n ctx->frame_bits = (ctx->cid_table->coding_unit_size - 640 - 4) * 8;\n ctx->qscale = 1;\n ctx->lambda = 2<<LAMBDA_FRAC_BITS;\n return 0;\n fail:\n return -1;\n}']
36,418
0
https://github.com/openssl/openssl/blob/4b8515baa6edef1a771f9e4e3fbc0395b4a629e8/crypto/bn/bn_ctx.c/#L273
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['static int test_badmod()\n{\n BIGNUM *a = NULL, *b = NULL, *zero = NULL;\n BN_MONT_CTX *mont = NULL;\n int st = 0;\n if (!TEST_ptr(a = BN_new())\n || !TEST_ptr(b = BN_new())\n || !TEST_ptr(zero = BN_new())\n || !TEST_ptr(mont = BN_MONT_CTX_new()))\n goto err;\n BN_zero(zero);\n if (!TEST_false(BN_div(a, b, BN_value_one(), zero, ctx)))\n goto err;\n ERR_clear_error();\n if (!TEST_false(BN_mod_mul(a, BN_value_one(), BN_value_one(), zero, ctx)))\n goto err;\n ERR_clear_error();\n if (!TEST_false(BN_mod_exp(a, BN_value_one(), BN_value_one(), zero, ctx)))\n goto err;\n ERR_clear_error();\n if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),\n zero, ctx, NULL)))\n goto err;\n ERR_clear_error();\n if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),\n zero, ctx, NULL)))\n goto err;\n ERR_clear_error();\n if (!TEST_false(BN_MONT_CTX_set(mont, zero, ctx)))\n goto err;\n ERR_clear_error();\n if (!TEST_true(BN_set_word(b, 16)))\n goto err;\n if (!TEST_false(BN_MONT_CTX_set(mont, b, ctx)))\n goto err;\n ERR_clear_error();\n if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),\n b, ctx, NULL)))\n goto err;\n ERR_clear_error();\n if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),\n b, ctx, NULL)))\n goto err;\n ERR_clear_error();\n st = 1;\nerr:\n BN_free(a);\n BN_free(b);\n BN_free(zero);\n BN_MONT_CTX_free(mont);\n return st;\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return (0);\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return (0);\n }\n if (dv != NULL)\n BN_zero(dv);\n return (1);\n }\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n resp++;\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n resp--;\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return (1);\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return (0);\n}', 'void BN_CTX_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}']
36,419
0
https://github.com/openssl/openssl/blob/0247086d9a0713e18a0f16949039a40fdb63ff7e/ssl/t1_lib.c/#L2160
DH *ssl_get_auto_dh(SSL *s) { int dh_secbits = 80; if (s->cert->dh_tmp_auto == 2) return DH_get_1024_160(); if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) { if (s->s3->tmp.new_cipher->strength_bits == 256) dh_secbits = 128; else dh_secbits = 80; } else { CERT_PKEY *cpk = ssl_get_server_send_pkey(s); dh_secbits = EVP_PKEY_security_bits(cpk->privatekey); } if (dh_secbits >= 128) { DH *dhp = DH_new(); BIGNUM *p, *g; if (dhp == NULL) return NULL; g = BN_new(); if (g != NULL) BN_set_word(g, 2); if (dh_secbits >= 192) p = BN_get_rfc3526_prime_8192(NULL); else p = BN_get_rfc3526_prime_3072(NULL); if (p == NULL || g == NULL || !DH_set0_pqg(dhp, p, NULL, g)) { DH_free(dhp); BN_free(p); BN_free(g); return NULL; } return dhp; } if (dh_secbits >= 112) return DH_get_2048_224(); return DH_get_1024_160(); }
['DH *ssl_get_auto_dh(SSL *s)\n{\n int dh_secbits = 80;\n if (s->cert->dh_tmp_auto == 2)\n return DH_get_1024_160();\n if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) {\n if (s->s3->tmp.new_cipher->strength_bits == 256)\n dh_secbits = 128;\n else\n dh_secbits = 80;\n } else {\n CERT_PKEY *cpk = ssl_get_server_send_pkey(s);\n dh_secbits = EVP_PKEY_security_bits(cpk->privatekey);\n }\n if (dh_secbits >= 128) {\n DH *dhp = DH_new();\n BIGNUM *p, *g;\n if (dhp == NULL)\n return NULL;\n g = BN_new();\n if (g != NULL)\n BN_set_word(g, 2);\n if (dh_secbits >= 192)\n p = BN_get_rfc3526_prime_8192(NULL);\n else\n p = BN_get_rfc3526_prime_3072(NULL);\n if (p == NULL || g == NULL || !DH_set0_pqg(dhp, p, NULL, g)) {\n DH_free(dhp);\n BN_free(p);\n BN_free(g);\n return NULL;\n }\n return dhp;\n }\n if (dh_secbits >= 112)\n return DH_get_2048_224();\n return DH_get_1024_160();\n}', 'CERT_PKEY *ssl_get_server_send_pkey(SSL *s)\n{\n CERT *c;\n int i;\n c = s->cert;\n if (!s->s3 || !s->s3->tmp.new_cipher)\n return NULL;\n ssl_set_masks(s);\n i = ssl_get_server_cert_index(s);\n if (i < 0)\n return NULL;\n return &c->pkeys[i];\n}', 'static int ssl_get_server_cert_index(const SSL *s)\n{\n int idx;\n idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);\n if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509)\n idx = SSL_PKEY_RSA_SIGN;\n if (idx == SSL_PKEY_GOST_EC) {\n if (s->cert->pkeys[SSL_PKEY_GOST12_512].x509)\n idx = SSL_PKEY_GOST12_512;\n else if (s->cert->pkeys[SSL_PKEY_GOST12_256].x509)\n idx = SSL_PKEY_GOST12_256;\n else if (s->cert->pkeys[SSL_PKEY_GOST01].x509)\n idx = SSL_PKEY_GOST01;\n else\n idx = -1;\n }\n if (idx == -1)\n SSLerr(SSL_F_SSL_GET_SERVER_CERT_INDEX, ERR_R_INTERNAL_ERROR);\n return idx;\n}', 'int ssl_cipher_get_cert_index(const SSL_CIPHER *c)\n{\n uint32_t alg_a;\n alg_a = c->algorithm_auth;\n if (alg_a & SSL_aECDSA)\n return SSL_PKEY_ECC;\n else if (alg_a & SSL_aDSS)\n return SSL_PKEY_DSA_SIGN;\n else if (alg_a & SSL_aRSA)\n return SSL_PKEY_RSA_ENC;\n else if (alg_a & SSL_aGOST12)\n return SSL_PKEY_GOST_EC;\n else if (alg_a & SSL_aGOST01)\n return SSL_PKEY_GOST01;\n return -1;\n}']
36,420
0
https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/crypto/des/des_enc.c/#L115
void des_encrypt(DES_LONG *data, des_key_schedule ks, int enc) { register DES_LONG l,r,t,u; #ifdef DES_PTR register const unsigned char *des_SP=(const unsigned char *)des_SPtrans; #endif #ifndef DES_UNROLL register int i; #endif register DES_LONG *s; r=data[0]; l=data[1]; IP(r,l); r=ROTATE(r,29)&0xffffffffL; l=ROTATE(l,29)&0xffffffffL; s=(DES_LONG *)ks; if (enc) { #ifdef DES_UNROLL D_ENCRYPT(l,r, 0); D_ENCRYPT(r,l, 2); D_ENCRYPT(l,r, 4); D_ENCRYPT(r,l, 6); D_ENCRYPT(l,r, 8); D_ENCRYPT(r,l,10); D_ENCRYPT(l,r,12); D_ENCRYPT(r,l,14); D_ENCRYPT(l,r,16); D_ENCRYPT(r,l,18); D_ENCRYPT(l,r,20); D_ENCRYPT(r,l,22); D_ENCRYPT(l,r,24); D_ENCRYPT(r,l,26); D_ENCRYPT(l,r,28); D_ENCRYPT(r,l,30); #else for (i=0; i<32; i+=8) { D_ENCRYPT(l,r,i+0); D_ENCRYPT(r,l,i+2); D_ENCRYPT(l,r,i+4); D_ENCRYPT(r,l,i+6); } #endif } else { #ifdef DES_UNROLL D_ENCRYPT(l,r,30); D_ENCRYPT(r,l,28); D_ENCRYPT(l,r,26); D_ENCRYPT(r,l,24); D_ENCRYPT(l,r,22); D_ENCRYPT(r,l,20); D_ENCRYPT(l,r,18); D_ENCRYPT(r,l,16); D_ENCRYPT(l,r,14); D_ENCRYPT(r,l,12); D_ENCRYPT(l,r,10); D_ENCRYPT(r,l, 8); D_ENCRYPT(l,r, 6); D_ENCRYPT(r,l, 4); D_ENCRYPT(l,r, 2); D_ENCRYPT(r,l, 0); #else for (i=30; i>0; i-=8) { D_ENCRYPT(l,r,i-0); D_ENCRYPT(r,l,i-2); D_ENCRYPT(l,r,i-4); D_ENCRYPT(r,l,i-6); } #endif } l=ROTATE(l,3)&0xffffffffL; r=ROTATE(r,3)&0xffffffffL; FP(r,l); data[0]=l; data[1]=r; l=r=t=u=0; }
['int _des_crypt(char *buf, int len, struct desparams *desp)\n\t{\n\tdes_key_schedule ks;\n\tint enc;\n\tdes_set_key(desp->des_key,ks);\n\tenc=(desp->des_dir == ENCRYPT)?DES_ENCRYPT:DES_DECRYPT;\n\tif (desp->des_mode == CBC)\n\t\tdes_ecb_encrypt(desp->UDES.UDES_buf,desp->UDES.UDES_buf,ks,\n\t\t\t\tenc);\n\telse\n\t\t{\n\t\tdes_ncbc_encrypt(desp->UDES.UDES_buf,desp->UDES.UDES_buf,\n\t\t\t\tlen,ks,desp->des_ivec,enc);\n#ifdef undef\n\t\ta=(char *)&(desp->UDES.UDES_buf[len-8]);\n\t\tb=(char *)&(desp->des_ivec[0]);\n\t\t*(a++)= *(b++); *(a++)= *(b++);\n\t\t*(a++)= *(b++); *(a++)= *(b++);\n\t\t*(a++)= *(b++); *(a++)= *(b++);\n\t\t*(a++)= *(b++); *(a++)= *(b++);\n#endif\n\t\t}\n\treturn(1);\n\t}', 'void des_ecb_encrypt(const des_cblock in, des_cblock out, des_key_schedule ks,\n\t int enc)\n\t{\n\tregister DES_LONG l;\n\tDES_LONG ll[2];\n\tc2l(in,l); ll[0]=l;\n\tc2l(in,l); ll[1]=l;\n\tdes_encrypt(ll,ks,enc);\n\tl=ll[0]; l2c(l,out);\n\tl=ll[1]; l2c(l,out);\n\tl=ll[0]=ll[1]=0;\n\t}', 'void des_encrypt(DES_LONG *data, des_key_schedule ks, int enc)\n\t{\n\tregister DES_LONG l,r,t,u;\n#ifdef DES_PTR\n\tregister const unsigned char *des_SP=(const unsigned char *)des_SPtrans;\n#endif\n#ifndef DES_UNROLL\n\tregister int i;\n#endif\n\tregister DES_LONG *s;\n\tr=data[0];\n\tl=data[1];\n\tIP(r,l);\n\tr=ROTATE(r,29)&0xffffffffL;\n\tl=ROTATE(l,29)&0xffffffffL;\n\ts=(DES_LONG *)ks;\n\tif (enc)\n\t\t{\n#ifdef DES_UNROLL\n\t\tD_ENCRYPT(l,r, 0);\n\t\tD_ENCRYPT(r,l, 2);\n\t\tD_ENCRYPT(l,r, 4);\n\t\tD_ENCRYPT(r,l, 6);\n\t\tD_ENCRYPT(l,r, 8);\n\t\tD_ENCRYPT(r,l,10);\n\t\tD_ENCRYPT(l,r,12);\n\t\tD_ENCRYPT(r,l,14);\n\t\tD_ENCRYPT(l,r,16);\n\t\tD_ENCRYPT(r,l,18);\n\t\tD_ENCRYPT(l,r,20);\n\t\tD_ENCRYPT(r,l,22);\n\t\tD_ENCRYPT(l,r,24);\n\t\tD_ENCRYPT(r,l,26);\n\t\tD_ENCRYPT(l,r,28);\n\t\tD_ENCRYPT(r,l,30);\n#else\n\t\tfor (i=0; i<32; i+=8)\n\t\t\t{\n\t\t\tD_ENCRYPT(l,r,i+0);\n\t\t\tD_ENCRYPT(r,l,i+2);\n\t\t\tD_ENCRYPT(l,r,i+4);\n\t\t\tD_ENCRYPT(r,l,i+6);\n\t\t\t}\n#endif\n\t\t}\n\telse\n\t\t{\n#ifdef DES_UNROLL\n\t\tD_ENCRYPT(l,r,30);\n\t\tD_ENCRYPT(r,l,28);\n\t\tD_ENCRYPT(l,r,26);\n\t\tD_ENCRYPT(r,l,24);\n\t\tD_ENCRYPT(l,r,22);\n\t\tD_ENCRYPT(r,l,20);\n\t\tD_ENCRYPT(l,r,18);\n\t\tD_ENCRYPT(r,l,16);\n\t\tD_ENCRYPT(l,r,14);\n\t\tD_ENCRYPT(r,l,12);\n\t\tD_ENCRYPT(l,r,10);\n\t\tD_ENCRYPT(r,l, 8);\n\t\tD_ENCRYPT(l,r, 6);\n\t\tD_ENCRYPT(r,l, 4);\n\t\tD_ENCRYPT(l,r, 2);\n\t\tD_ENCRYPT(r,l, 0);\n#else\n\t\tfor (i=30; i>0; i-=8)\n\t\t\t{\n\t\t\tD_ENCRYPT(l,r,i-0);\n\t\t\tD_ENCRYPT(r,l,i-2);\n\t\t\tD_ENCRYPT(l,r,i-4);\n\t\t\tD_ENCRYPT(r,l,i-6);\n\t\t\t}\n#endif\n\t\t}\n\tl=ROTATE(l,3)&0xffffffffL;\n\tr=ROTATE(r,3)&0xffffffffL;\n\tFP(r,l);\n\tdata[0]=l;\n\tdata[1]=r;\n\tl=r=t=u=0;\n\t}']
36,421
0
https://github.com/openssl/openssl/blob/09c4b4e0b7c795daa99aeca27f83fcac007db33d/apps/ca.c/#L2957
int make_revoked(X509_REVOKED *rev, char *str) { char *tmp = NULL; char *rtime_str, *reason_str = NULL, *arg_str = NULL, *p; int reason_code = -1; int i, ret = 0; ASN1_OBJECT *hold = NULL; ASN1_GENERALIZEDTIME *comp_time = NULL; ASN1_ENUMERATED *rtmp = NULL; tmp = BUF_strdup(str); p = strchr(tmp, ','); rtime_str = tmp; if (p) { *p = '\0'; p++; reason_str = p; p = strchr(p, ','); if (p) { *p = '\0'; arg_str = p + 1; } } if (rev && !ASN1_UTCTIME_set_string(rev->revocationDate, rtime_str)) { BIO_printf(bio_err, "invalid revocation date %s\n", rtime_str); goto err; } if (reason_str) { for (i = 0; i < NUM_REASONS; i++) { if(!strcasecmp(reason_str, crl_reasons[i])) { reason_code = i; break; } } if (reason_code == OCSP_REVOKED_STATUS_NOSTATUS) { BIO_printf(bio_err, "invalid reason code %s\n", reason_str); goto err; } if (reason_code == 7) reason_code = OCSP_REVOKED_STATUS_REMOVEFROMCRL; else if (reason_code == 8) { if (!arg_str) { BIO_printf(bio_err, "missing hold instruction\n"); goto err; } reason_code = OCSP_REVOKED_STATUS_CERTIFICATEHOLD; hold = OBJ_txt2obj(arg_str, 0); if (!hold) { BIO_printf(bio_err, "invalid object identifier %s\n", arg_str); goto err; } } else if ((reason_code == 9) || (reason_code == 10)) { if (!arg_str) { BIO_printf(bio_err, "missing compromised time\n"); goto err; } comp_time = ASN1_GENERALIZEDTIME_new(); if (!ASN1_GENERALIZEDTIME_set_string(comp_time, arg_str)) { BIO_printf(bio_err, "invalid compromised time %s\n", arg_str); goto err; } if (reason_code == 9) reason_code = OCSP_REVOKED_STATUS_KEYCOMPROMISE; else reason_code = OCSP_REVOKED_STATUS_CACOMPROMISE; } } if (rev && (reason_code != OCSP_REVOKED_STATUS_NOSTATUS)) { rtmp = ASN1_ENUMERATED_new(); if (!rtmp || !ASN1_ENUMERATED_set(rtmp, reason_code)) goto err; if (!X509_REVOKED_add1_ext_i2d(rev, NID_crl_reason, rtmp, 0, 0)) goto err; } if (rev && comp_time) { if (!X509_REVOKED_add1_ext_i2d(rev, NID_invalidity_date, comp_time, 0, 0)) goto err; } if (rev && hold) { if (!X509_REVOKED_add1_ext_i2d(rev, NID_hold_instruction_code, hold, 0, 0)) goto err; } if (reason_code != OCSP_REVOKED_STATUS_NOSTATUS) ret = 2; else ret = 1; err: if (tmp) OPENSSL_free(tmp); ASN1_OBJECT_free(hold); ASN1_GENERALIZEDTIME_free(comp_time); ASN1_ENUMERATED_free(rtmp); return ret; }
['int make_revoked(X509_REVOKED *rev, char *str)\n\t{\n\tchar *tmp = NULL;\n\tchar *rtime_str, *reason_str = NULL, *arg_str = NULL, *p;\n\tint reason_code = -1;\n\tint i, ret = 0;\n\tASN1_OBJECT *hold = NULL;\n\tASN1_GENERALIZEDTIME *comp_time = NULL;\n\tASN1_ENUMERATED *rtmp = NULL;\n\ttmp = BUF_strdup(str);\n\tp = strchr(tmp, \',\');\n\trtime_str = tmp;\n\tif (p)\n\t\t{\n\t\t*p = \'\\0\';\n\t\tp++;\n\t\treason_str = p;\n\t\tp = strchr(p, \',\');\n\t\tif (p)\n\t\t\t{\n\t\t\t*p = \'\\0\';\n\t\t\targ_str = p + 1;\n\t\t\t}\n\t\t}\n\tif (rev && !ASN1_UTCTIME_set_string(rev->revocationDate, rtime_str))\n\t\t{\n\t\tBIO_printf(bio_err, "invalid revocation date %s\\n", rtime_str);\n\t\tgoto err;\n\t\t}\n\tif (reason_str)\n\t\t{\n\t\tfor (i = 0; i < NUM_REASONS; i++)\n\t\t\t{\n\t\t\tif(!strcasecmp(reason_str, crl_reasons[i]))\n\t\t\t\t{\n\t\t\t\treason_code = i;\n\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\tif (reason_code == OCSP_REVOKED_STATUS_NOSTATUS)\n\t\t\t{\n\t\t\tBIO_printf(bio_err, "invalid reason code %s\\n", reason_str);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (reason_code == 7)\n\t\t\treason_code = OCSP_REVOKED_STATUS_REMOVEFROMCRL;\n\t\telse if (reason_code == 8)\n\t\t\t{\n\t\t\tif (!arg_str)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err, "missing hold instruction\\n");\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\treason_code = OCSP_REVOKED_STATUS_CERTIFICATEHOLD;\n\t\t\thold = OBJ_txt2obj(arg_str, 0);\n\t\t\tif (!hold)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err, "invalid object identifier %s\\n", arg_str);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\t}\n\t\telse if ((reason_code == 9) || (reason_code == 10))\n\t\t\t{\n\t\t\tif (!arg_str)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err, "missing compromised time\\n");\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tcomp_time = ASN1_GENERALIZEDTIME_new();\n\t\t\tif (!ASN1_GENERALIZEDTIME_set_string(comp_time, arg_str))\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err, "invalid compromised time %s\\n", arg_str);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif (reason_code == 9)\n\t\t\t\treason_code = OCSP_REVOKED_STATUS_KEYCOMPROMISE;\n\t\t\telse\n\t\t\t\treason_code = OCSP_REVOKED_STATUS_CACOMPROMISE;\n\t\t\t}\n\t\t}\n\tif (rev && (reason_code != OCSP_REVOKED_STATUS_NOSTATUS))\n\t\t{\n\t\trtmp = ASN1_ENUMERATED_new();\n\t\tif (!rtmp || !ASN1_ENUMERATED_set(rtmp, reason_code))\n\t\t\tgoto err;\n\t\tif (!X509_REVOKED_add1_ext_i2d(rev, NID_crl_reason, rtmp, 0, 0))\n\t\t\tgoto err;\n\t\t}\n\tif (rev && comp_time)\n\t\t{\n\t\tif (!X509_REVOKED_add1_ext_i2d(rev, NID_invalidity_date, comp_time, 0, 0))\n\t\t\tgoto err;\n\t\t}\n\tif (rev && hold)\n\t\t{\n\t\tif (!X509_REVOKED_add1_ext_i2d(rev, NID_hold_instruction_code, hold, 0, 0))\n\t\t\tgoto err;\n\t\t}\n\tif (reason_code != OCSP_REVOKED_STATUS_NOSTATUS)\n\t\tret = 2;\n\telse ret = 1;\n\terr:\n\tif (tmp) OPENSSL_free(tmp);\n\tASN1_OBJECT_free(hold);\n\tASN1_GENERALIZEDTIME_free(comp_time);\n\tASN1_ENUMERATED_free(rtmp);\n\treturn ret;\n\t}', 'char *BUF_strdup(const char *str)\n\t{\n\tchar *ret;\n\tint n;\n\tif (str == NULL) return(NULL);\n\tn=strlen(str);\n\tret=OPENSSL_malloc(n+1);\n\tif (ret == NULL)\n\t\t{\n\t\tBUFerr(BUF_F_BUF_STRDUP,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tmemcpy(ret,str,n+1);\n\treturn(ret);\n\t}', 'void *CRYPTO_malloc(int num, const char *file, int line)\n\t{\n\tvoid *ret = NULL;\n\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\treturn ret;\n\t}', 'void ERR_put_error(int lib, int func, int reason, const char *file,\n\t int line)\n\t{\n\tERR_STATE *es;\n#ifdef _OSD_POSIX\n\tif (strncmp(file,"*POSIX(", sizeof("*POSIX(")-1) == 0) {\n\t\tchar *end;\n\t\tfile += sizeof("*POSIX(")-1;\n\t\tend = &file[strlen(file)-1];\n\t\tif (*end == \')\')\n\t\t\t*end = \'\\0\';\n\t\tif ((end = strrchr(file, \'/\')) != NULL)\n\t\t\tfile = &end[1];\n\t}\n#endif\n\tes=ERR_get_state();\n\tes->top=(es->top+1)%ERR_NUM_ERRORS;\n\tif (es->top == es->bottom)\n\t\tes->bottom=(es->bottom+1)%ERR_NUM_ERRORS;\n\tes->err_buffer[es->top]=ERR_PACK(lib,func,reason);\n\tes->err_file[es->top]=file;\n\tes->err_line[es->top]=line;\n\terr_clear_data(es,es->top);\n\t}', 'void CRYPTO_free(void *str)\n\t{\n\tif (free_debug_func != NULL)\n\t\tfree_debug_func(str, 0);\n#ifdef LEVITTE_DEBUG_MEM\n\tfprintf(stderr, "LEVITTE_DEBUG_MEM: < 0x%p\\n", str);\n#endif\n\tfree_func(str);\n\tif (free_debug_func != NULL)\n\t\tfree_debug_func(NULL, 1);\n\t}']
36,422
0
https://github.com/openssl/openssl/blob/305b68f1a2b6d4d0aa07a6ab47ac372f067a40bb/crypto/bn/bn_lib.c/#L231
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *a = NULL; if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return NULL; } if (BN_get_flags(b, BN_FLG_SECURE)) a = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = OPENSSL_zalloc(words * sizeof(*a)); if (a == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return NULL; } assert(b->top <= words); if (b->top > 0) memcpy(a, b->d, sizeof(*a) * b->top); return a; }
['static int 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_prime_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,\n BN_GENCB *cb)\n{\n return BN_is_prime_fasttest_ex(a, checks, ctx_passed, 0, cb);\n}', 'int BN_is_prime_fasttest_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,\n int do_trial_division, BN_GENCB *cb)\n{\n int i, j, ret = -1;\n int k;\n BN_CTX *ctx = NULL;\n BIGNUM *A1, *A1_odd, *A3, *check;\n BN_MONT_CTX *mont = NULL;\n if (BN_is_word(a, 2) || BN_is_word(a, 3))\n return 1;\n if (!BN_is_odd(a) || BN_cmp(a, BN_value_one()) <= 0)\n return 0;\n if (checks == BN_prime_checks)\n checks = BN_prime_checks_for_size(BN_num_bits(a));\n if (do_trial_division) {\n for (i = 1; i < NUMPRIMES; i++) {\n BN_ULONG mod = BN_mod_word(a, primes[i]);\n if (mod == (BN_ULONG)-1)\n goto err;\n if (mod == 0)\n return BN_is_word(a, primes[i]);\n }\n if (!BN_GENCB_call(cb, 1, -1))\n goto err;\n }\n if (ctx_passed != NULL)\n ctx = ctx_passed;\n else if ((ctx = BN_CTX_new()) == NULL)\n goto err;\n BN_CTX_start(ctx);\n A1 = BN_CTX_get(ctx);\n A3 = BN_CTX_get(ctx);\n A1_odd = BN_CTX_get(ctx);\n check = BN_CTX_get(ctx);\n if (check == NULL)\n goto err;\n if (!BN_copy(A1, a) || !BN_sub_word(A1, 1))\n goto err;\n if (!BN_copy(A3, a) || !BN_sub_word(A3, 3))\n goto err;\n k = 1;\n while (!BN_is_bit_set(A1, k))\n k++;\n if (!BN_rshift(A1_odd, A1, k))\n goto err;\n mont = BN_MONT_CTX_new();\n if (mont == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, a, ctx))\n goto err;\n for (i = 0; i < checks; i++) {\n if (!BN_priv_rand_range(check, A3) || !BN_add_word(check, 2))\n goto err;\n j = witness(check, a, A1, A1_odd, k, ctx, mont);\n if (j == -1)\n goto err;\n if (j) {\n ret = 0;\n goto err;\n }\n if (!BN_GENCB_call(cb, 1, i))\n goto err;\n }\n ret = 1;\n err:\n if (ctx != NULL) {\n BN_CTX_end(ctx);\n if (ctx_passed == NULL)\n BN_CTX_free(ctx);\n }\n BN_MONT_CTX_free(mont);\n return ret;\n}', 'int BN_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 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 ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return 0;\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return 0;\n }\n if (dv != NULL)\n BN_zero(dv);\n return 1;\n }\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.flags = BN_FLG_STATIC_DATA;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n resp++;\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n resp--;\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return 0;\n}', 'int BN_rshift1(BIGNUM *r, const BIGNUM *a)\n{\n BN_ULONG *ap, *rp, t, c;\n int i, j;\n bn_check_top(r);\n bn_check_top(a);\n if (BN_is_zero(a)) {\n BN_zero(r);\n return 1;\n }\n i = a->top;\n ap = a->d;\n j = i - (ap[i - 1] == 1);\n if (a != r) {\n if (bn_wexpand(r, j) == NULL)\n return 0;\n r->neg = a->neg;\n }\n rp = r->d;\n t = ap[--i];\n c = (t & 1) ? BN_TBIT : 0;\n if (t >>= 1)\n rp[i] = t;\n while (i > 0) {\n t = ap[--i];\n rp[i] = ((t >> 1) & BN_MASK2) | c;\n c = (t & 1) ? BN_TBIT : 0;\n }\n r->top = j;\n if (!r->top)\n r->neg = 0;\n bn_check_top(r);\n return 1;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n a->flags &= ~BN_FLG_FIXED_TOP;\n bn_check_top(a);\n return 1;\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n INCREMENT(malloc_count);\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n if (allow_customize) {\n allow_customize = 0;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)(file); (void)(line);\n ret = malloc(num);\n#endif\n return ret;\n}']
36,423
0
https://github.com/openssl/openssl/blob/3b1fb1a0226e29c9d7c79ff7fbde21ef9cac4deb/crypto/x509/x509_vfy.c/#L554
static int check_chain_extensions(X509_STORE_CTX *ctx) { #ifdef OPENSSL_NO_CHAIN_VERIFY return 1; #else int i, ok=0, must_be_ca, plen = 0; X509 *x; int (*cb)(int xok,X509_STORE_CTX *xctx); int proxy_path_length = 0; int purpose; int allow_proxy_certs; cb=ctx->verify_cb; must_be_ca = -1; if (ctx->parent) { allow_proxy_certs = 0; purpose = X509_PURPOSE_CRL_SIGN; } else { allow_proxy_certs = !!(ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS); if (getenv("OPENSSL_ALLOW_PROXY_CERTS")) allow_proxy_certs = 1; purpose = ctx->param->purpose; } for (i = 0; i < ctx->last_untrusted; i++) { int ret; x = sk_X509_value(ctx->chain, i); if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL) && (x->ex_flags & EXFLAG_CRITICAL)) { ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION; ctx->error_depth = i; ctx->current_cert = x; ok=cb(0,ctx); if (!ok) goto end; } if (!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY)) { ctx->error = X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED; ctx->error_depth = i; ctx->current_cert = x; ok=cb(0,ctx); if (!ok) goto end; } ret = X509_check_ca(x); switch(must_be_ca) { case -1: if ((ctx->param->flags & X509_V_FLAG_X509_STRICT) && (ret != 1) && (ret != 0)) { ret = 0; ctx->error = X509_V_ERR_INVALID_CA; } else ret = 1; break; case 0: if (ret != 0) { ret = 0; ctx->error = X509_V_ERR_INVALID_NON_CA; } else ret = 1; break; default: if ((ret == 0) || ((ctx->param->flags & X509_V_FLAG_X509_STRICT) && (ret != 1))) { ret = 0; ctx->error = X509_V_ERR_INVALID_CA; } else ret = 1; break; } if (ret == 0) { ctx->error_depth = i; ctx->current_cert = x; ok=cb(0,ctx); if (!ok) goto end; } if (ctx->param->purpose > 0) { ret = X509_check_purpose(x, purpose, must_be_ca > 0); if ((ret == 0) || ((ctx->param->flags & X509_V_FLAG_X509_STRICT) && (ret != 1))) { ctx->error = X509_V_ERR_INVALID_PURPOSE; ctx->error_depth = i; ctx->current_cert = x; ok=cb(0,ctx); if (!ok) goto end; } } if ((i > 1) && !(x->ex_flags & EXFLAG_SI) && (x->ex_pathlen != -1) && (plen > (x->ex_pathlen + proxy_path_length + 1))) { ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED; ctx->error_depth = i; ctx->current_cert = x; ok=cb(0,ctx); if (!ok) goto end; } if (!(x->ex_flags & EXFLAG_SI)) plen++; if (x->ex_flags & EXFLAG_PROXY) { if (x->ex_pcpathlen != -1 && i > x->ex_pcpathlen) { ctx->error = X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED; ctx->error_depth = i; ctx->current_cert = x; ok=cb(0,ctx); if (!ok) goto end; } proxy_path_length++; must_be_ca = 0; } else must_be_ca = 1; } ok = 1; end: return ok; #endif }
['static int check_chain_extensions(X509_STORE_CTX *ctx)\n{\n#ifdef OPENSSL_NO_CHAIN_VERIFY\n\treturn 1;\n#else\n\tint i, ok=0, must_be_ca, plen = 0;\n\tX509 *x;\n\tint (*cb)(int xok,X509_STORE_CTX *xctx);\n\tint proxy_path_length = 0;\n\tint purpose;\n\tint allow_proxy_certs;\n\tcb=ctx->verify_cb;\n\tmust_be_ca = -1;\n\tif (ctx->parent)\n\t\t{\n\t\tallow_proxy_certs = 0;\n\t\tpurpose = X509_PURPOSE_CRL_SIGN;\n\t\t}\n\telse\n\t\t{\n\t\tallow_proxy_certs =\n\t\t\t!!(ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS);\n\t\tif (getenv("OPENSSL_ALLOW_PROXY_CERTS"))\n\t\t\tallow_proxy_certs = 1;\n\t\tpurpose = ctx->param->purpose;\n\t\t}\n\tfor (i = 0; i < ctx->last_untrusted; i++)\n\t\t{\n\t\tint ret;\n\t\tx = sk_X509_value(ctx->chain, i);\n\t\tif (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)\n\t\t\t&& (x->ex_flags & EXFLAG_CRITICAL))\n\t\t\t{\n\t\t\tctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION;\n\t\t\tctx->error_depth = i;\n\t\t\tctx->current_cert = x;\n\t\t\tok=cb(0,ctx);\n\t\t\tif (!ok) goto end;\n\t\t\t}\n\t\tif (!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY))\n\t\t\t{\n\t\t\tctx->error = X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED;\n\t\t\tctx->error_depth = i;\n\t\t\tctx->current_cert = x;\n\t\t\tok=cb(0,ctx);\n\t\t\tif (!ok) goto end;\n\t\t\t}\n\t\tret = X509_check_ca(x);\n\t\tswitch(must_be_ca)\n\t\t\t{\n\t\tcase -1:\n\t\t\tif ((ctx->param->flags & X509_V_FLAG_X509_STRICT)\n\t\t\t\t&& (ret != 1) && (ret != 0))\n\t\t\t\t{\n\t\t\t\tret = 0;\n\t\t\t\tctx->error = X509_V_ERR_INVALID_CA;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\tret = 1;\n\t\t\tbreak;\n\t\tcase 0:\n\t\t\tif (ret != 0)\n\t\t\t\t{\n\t\t\t\tret = 0;\n\t\t\t\tctx->error = X509_V_ERR_INVALID_NON_CA;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\tret = 1;\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tif ((ret == 0)\n\t\t\t\t|| ((ctx->param->flags & X509_V_FLAG_X509_STRICT)\n\t\t\t\t\t&& (ret != 1)))\n\t\t\t\t{\n\t\t\t\tret = 0;\n\t\t\t\tctx->error = X509_V_ERR_INVALID_CA;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\tret = 1;\n\t\t\tbreak;\n\t\t\t}\n\t\tif (ret == 0)\n\t\t\t{\n\t\t\tctx->error_depth = i;\n\t\t\tctx->current_cert = x;\n\t\t\tok=cb(0,ctx);\n\t\t\tif (!ok) goto end;\n\t\t\t}\n\t\tif (ctx->param->purpose > 0)\n\t\t\t{\n\t\t\tret = X509_check_purpose(x, purpose, must_be_ca > 0);\n\t\t\tif ((ret == 0)\n\t\t\t\t|| ((ctx->param->flags & X509_V_FLAG_X509_STRICT)\n\t\t\t\t\t&& (ret != 1)))\n\t\t\t\t{\n\t\t\t\tctx->error = X509_V_ERR_INVALID_PURPOSE;\n\t\t\t\tctx->error_depth = i;\n\t\t\t\tctx->current_cert = x;\n\t\t\t\tok=cb(0,ctx);\n\t\t\t\tif (!ok) goto end;\n\t\t\t\t}\n\t\t\t}\n\t\tif ((i > 1) && !(x->ex_flags & EXFLAG_SI)\n\t\t\t && (x->ex_pathlen != -1)\n\t\t\t && (plen > (x->ex_pathlen + proxy_path_length + 1)))\n\t\t\t{\n\t\t\tctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED;\n\t\t\tctx->error_depth = i;\n\t\t\tctx->current_cert = x;\n\t\t\tok=cb(0,ctx);\n\t\t\tif (!ok) goto end;\n\t\t\t}\n\t\tif (!(x->ex_flags & EXFLAG_SI))\n\t\t\tplen++;\n\t\tif (x->ex_flags & EXFLAG_PROXY)\n\t\t\t{\n\t\t\tif (x->ex_pcpathlen != -1 && i > x->ex_pcpathlen)\n\t\t\t\t{\n\t\t\t\tctx->error =\n\t\t\t\t\tX509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED;\n\t\t\t\tctx->error_depth = i;\n\t\t\t\tctx->current_cert = x;\n\t\t\t\tok=cb(0,ctx);\n\t\t\t\tif (!ok) goto end;\n\t\t\t\t}\n\t\t\tproxy_path_length++;\n\t\t\tmust_be_ca = 0;\n\t\t\t}\n\t\telse\n\t\t\tmust_be_ca = 1;\n\t\t}\n\tok = 1;\n end:\n\treturn ok;\n#endif\n}', '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}', 'int X509_check_ca(X509 *x)\n{\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\treturn check_ca(x);\n}']
36,424
0
https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/crypto/md5/md5_dgst.c/#L133
void MD5_Update(MD5_CTX *c, const void *_data, unsigned long len) { register const unsigned char *data=_data; register ULONG *p; int sw,sc; ULONG l; if (len == 0) return; l=(c->Nl+(len<<3))&0xffffffffL; if (l < c->Nl) c->Nh++; c->Nh+=(len>>29); c->Nl=l; if (c->num != 0) { p=c->data; sw=c->num>>2; sc=c->num&0x03; if ((c->num+len) >= MD5_CBLOCK) { l= p[sw]; p_c2l(data,l,sc); p[sw++]=l; for (; sw<MD5_LBLOCK; sw++) { c2l(data,l); p[sw]=l; } len-=(MD5_CBLOCK-c->num); md5_block(c,p,64); c->num=0; } else { int ew,ec; c->num+=(int)len; if ((sc+len) < 4) { l= p[sw]; p_c2l_p(data,l,sc,len); p[sw]=l; } else { ew=(c->num>>2); ec=(c->num&0x03); l= p[sw]; p_c2l(data,l,sc); p[sw++]=l; for (; sw < ew; sw++) { c2l(data,l); p[sw]=l; } if (ec) { c2l_p(data,l,ec); p[sw]=l; } } return; } } #ifdef L_ENDIAN if ((((unsigned long)data)%sizeof(ULONG)) == 0) { sw=(int)len/MD5_CBLOCK; if (sw > 0) { sw*=MD5_CBLOCK; md5_block(c,(ULONG *)data,sw); data+=sw; len-=sw; } } #endif p=c->data; while (len >= MD5_CBLOCK) { #if defined(L_ENDIAN) || defined(B_ENDIAN) if (p != (unsigned long *)data) memcpy(p,data,MD5_CBLOCK); data+=MD5_CBLOCK; #ifdef B_ENDIAN for (sw=(MD5_LBLOCK/4); sw; sw--) { Endian_Reverse32(p[0]); Endian_Reverse32(p[1]); Endian_Reverse32(p[2]); Endian_Reverse32(p[3]); p+=4; } #endif #else for (sw=(MD5_LBLOCK/4); sw; sw--) { c2l(data,l); *(p++)=l; c2l(data,l); *(p++)=l; c2l(data,l); *(p++)=l; c2l(data,l); *(p++)=l; } #endif p=c->data; md5_block(c,p,64); len-=MD5_CBLOCK; } sc=(int)len; c->num=sc; if (sc) { sw=sc>>2; #ifdef L_ENDIAN p[sw]=0; memcpy(p,data,sc); #else sc&=0x03; for ( ; sw; sw--) { c2l(data,l); *(p++)=l; } c2l_p(data,l,sc); *p=l; #endif } }
['int ssl2_accept(SSL *s)\n\t{\n\tunsigned long l=time(NULL);\n\tBUF_MEM *buf=NULL;\n\tint ret= -1;\n\tlong num1;\n\tvoid (*cb)()=NULL;\n\tint new_state,state;\n\tRAND_seed(&l,sizeof(l));\n\tERR_clear_error();\n\tclear_sys_error();\n\tif (s->info_callback != NULL)\n\t\tcb=s->info_callback;\n\telse if (s->ctx->info_callback != NULL)\n\t\tcb=s->ctx->info_callback;\n\tif (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);\n\ts->in_handshake++;\n\tif (((s->session == NULL) || (s->session->cert == NULL)) &&\n\t\t(s->cert == NULL))\n\t\t{\n\t\tSSLerr(SSL_F_SSL2_ACCEPT,SSL_R_NO_CERTIFICATE_SET);\n\t\treturn(-1);\n\t\t}\n\tclear_sys_error();\n\tfor (;;)\n\t\t{\n\t\tstate=s->state;\n\t\tswitch (s->state)\n\t\t\t{\n\t\tcase SSL_ST_BEFORE:\n\t\tcase SSL_ST_ACCEPT:\n\t\tcase SSL_ST_BEFORE|SSL_ST_ACCEPT:\n\t\tcase SSL_ST_OK|SSL_ST_ACCEPT:\n\t\t\ts->server=1;\n\t\t\tif (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);\n\t\t\ts->version=SSL2_VERSION;\n\t\t\ts->type=SSL_ST_ACCEPT;\n\t\t\tbuf=s->init_buf;\n\t\t\tif ((buf == NULL) && ((buf=BUF_MEM_new()) == NULL))\n\t\t\t\t{ ret= -1; goto end; }\n\t\t\tif (!BUF_MEM_grow(buf,(int)\n\t\t\t\tSSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER))\n\t\t\t\t{ ret= -1; goto end; }\n\t\t\ts->init_buf=buf;\n\t\t\ts->init_num=0;\n\t\t\ts->ctx->stats.sess_accept++;\n\t\t\ts->handshake_func=ssl2_accept;\n\t\t\ts->state=SSL2_ST_GET_CLIENT_HELLO_A;\n\t\t\tBREAK;\n\t\tcase SSL2_ST_GET_CLIENT_HELLO_A:\n\t\tcase SSL2_ST_GET_CLIENT_HELLO_B:\n\t\tcase SSL2_ST_GET_CLIENT_HELLO_C:\n\t\t\ts->shutdown=0;\n\t\t\tret=get_client_hello(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->init_num=0;\n\t\t\ts->state=SSL2_ST_SEND_SERVER_HELLO_A;\n\t\t\tBREAK;\n\t\tcase SSL2_ST_SEND_SERVER_HELLO_A:\n\t\tcase SSL2_ST_SEND_SERVER_HELLO_B:\n\t\t\tret=server_hello(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->init_num=0;\n\t\t\tif (!s->hit)\n\t\t\t\t{\n\t\t\t\ts->state=SSL2_ST_GET_CLIENT_MASTER_KEY_A;\n\t\t\t\tBREAK;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\ts->state=SSL2_ST_SERVER_START_ENCRYPTION;\n\t\t\t\tBREAK;\n\t\t\t\t}\n\t\tcase SSL2_ST_GET_CLIENT_MASTER_KEY_A:\n\t\tcase SSL2_ST_GET_CLIENT_MASTER_KEY_B:\n\t\t\tret=get_client_master_key(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->init_num=0;\n\t\t\ts->state=SSL2_ST_SERVER_START_ENCRYPTION;\n\t\t\tBREAK;\n\t\tcase SSL2_ST_SERVER_START_ENCRYPTION:\n\t\t\tif (!ssl2_enc_init(s,0))\n\t\t\t\t{ ret= -1; goto end; }\n\t\t\ts->s2->clear_text=0;\n\t\t\ts->state=SSL2_ST_SEND_SERVER_VERIFY_A;\n\t\t\tBREAK;\n\t\tcase SSL2_ST_SEND_SERVER_VERIFY_A:\n\t\tcase SSL2_ST_SEND_SERVER_VERIFY_B:\n\t\t\tret=server_verify(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->init_num=0;\n\t\t\tif (s->hit)\n\t\t\t\t{\n\t\t\t\ts->state=SSL2_ST_SEND_SERVER_VERIFY_C;\n\t\t\t\tBREAK;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\ts->state=SSL2_ST_GET_CLIENT_FINISHED_A;\n\t\t\t\tbreak;\n\t\t\t\t}\n \t\tcase SSL2_ST_SEND_SERVER_VERIFY_C:\n \t\t\tnum1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);\n \t\t\tif (num1 != 0)\n \t\t\t\t{\n\t\t\t\ts->rwstate=SSL_WRITING;\n \t\t\t\tnum1=BIO_flush(s->wbio);\n \t\t\t\tif (num1 <= 0) { ret= -1; goto end; }\n\t\t\t\ts->rwstate=SSL_NOTHING;\n\t\t\t\t}\n \t\t\ts->wbio=BIO_pop(s->wbio);\n \t\t\ts->state=SSL2_ST_GET_CLIENT_FINISHED_A;\n \t\t\tBREAK;\n\t\tcase SSL2_ST_GET_CLIENT_FINISHED_A:\n\t\tcase SSL2_ST_GET_CLIENT_FINISHED_B:\n\t\t\tret=get_client_finished(s);\n\t\t\tif (ret <= 0)\n\t\t\t\tgoto end;\n\t\t\ts->init_num=0;\n\t\t\ts->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_A;\n\t\t\tBREAK;\n\t\tcase SSL2_ST_SEND_REQUEST_CERTIFICATE_A:\n\t\tcase SSL2_ST_SEND_REQUEST_CERTIFICATE_B:\n\t\tcase SSL2_ST_SEND_REQUEST_CERTIFICATE_C:\n\t\tcase SSL2_ST_SEND_REQUEST_CERTIFICATE_D:\n\t\t\tif (!(s->verify_mode & SSL_VERIFY_PEER) ||\n\t\t\t\t((s->session->peer != NULL) &&\n\t\t\t\t(s->verify_mode & SSL_VERIFY_CLIENT_ONCE)))\n\t\t\t\t{\n\t\t\t\ts->state=SSL2_ST_SEND_SERVER_FINISHED_A;\n\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tret=request_certificate(s);\n\t\t\t\tif (ret <= 0) goto end;\n\t\t\t\ts->init_num=0;\n\t\t\t\ts->state=SSL2_ST_SEND_SERVER_FINISHED_A;\n\t\t\t\t}\n\t\t\tBREAK;\n\t\tcase SSL2_ST_SEND_SERVER_FINISHED_A:\n\t\tcase SSL2_ST_SEND_SERVER_FINISHED_B:\n\t\t\tret=server_finish(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->init_num=0;\n\t\t\ts->state=SSL_ST_OK;\n\t\t\tbreak;\n\t\tcase SSL_ST_OK:\n\t\t\tBUF_MEM_free(s->init_buf);\n\t\t\tssl_free_wbio_buffer(s);\n\t\t\ts->init_buf=NULL;\n\t\t\ts->init_num=0;\n\t\t\tssl_update_cache(s,SSL_SESS_CACHE_SERVER);\n\t\t\ts->ctx->stats.sess_accept_good++;\n\t\t\tret=1;\n\t\t\tif (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);\n\t\t\tgoto end;\n\t\tdefault:\n\t\t\tSSLerr(SSL_F_SSL2_ACCEPT,SSL_R_UNKNOWN_STATE);\n\t\t\tret= -1;\n\t\t\tgoto end;\n\t\t\t}\n\t\tif ((cb != NULL) && (s->state != state))\n\t\t\t{\n\t\t\tnew_state=s->state;\n\t\t\ts->state=state;\n\t\t\tcb(s,SSL_CB_ACCEPT_LOOP,1);\n\t\t\ts->state=new_state;\n\t\t\t}\n\t\t}\nend:\n\ts->in_handshake--;\n\tif (cb != NULL)\n\t\tcb(s,SSL_CB_ACCEPT_EXIT,ret);\n\treturn(ret);\n\t}', 'int ssl2_enc_init(SSL *s, int client)\n\t{\n\tEVP_CIPHER_CTX *rs,*ws;\n\tconst EVP_CIPHER *c;\n\tconst EVP_MD *md;\n\tint num;\n\tif (!ssl_cipher_get_evp(s->session,&c,&md,NULL))\n\t\t{\n\t\tssl2_return_error(s,SSL2_PE_NO_CIPHER);\n\t\tSSLerr(SSL_F_SSL2_ENC_INIT,SSL_R_PROBLEMS_MAPPING_CIPHER_FUNCTIONS);\n\t\treturn(0);\n\t\t}\n\ts->read_hash=md;\n\ts->write_hash=md;\n\tif ((s->enc_read_ctx == NULL) &&\n\t\t((s->enc_read_ctx=(EVP_CIPHER_CTX *)\n\t\tMalloc(sizeof(EVP_CIPHER_CTX))) == NULL))\n\t\tgoto err;\n\tif ((s->enc_write_ctx == NULL) &&\n\t\t((s->enc_write_ctx=(EVP_CIPHER_CTX *)\n\t\tMalloc(sizeof(EVP_CIPHER_CTX))) == NULL))\n\t\tgoto err;\n\trs= s->enc_read_ctx;\n\tws= s->enc_write_ctx;\n\tEVP_CIPHER_CTX_init(rs);\n\tEVP_CIPHER_CTX_init(ws);\n\tnum=c->key_len;\n\ts->s2->key_material_length=num*2;\n\tssl2_generate_key_material(s);\n\tEVP_EncryptInit(ws,c,&(s->s2->key_material[(client)?num:0]),\n\t\ts->session->key_arg);\n\tEVP_DecryptInit(rs,c,&(s->s2->key_material[(client)?0:num]),\n\t\ts->session->key_arg);\n\ts->s2->read_key= &(s->s2->key_material[(client)?0:num]);\n\ts->s2->write_key= &(s->s2->key_material[(client)?num:0]);\n\treturn(1);\nerr:\n\tSSLerr(SSL_F_SSL2_ENC_INIT,ERR_R_MALLOC_FAILURE);\n\treturn(0);\n\t}', "void ssl2_generate_key_material(SSL *s)\n\t{\n\tunsigned int i;\n\tMD5_CTX ctx;\n\tunsigned char *km;\n\tunsigned char c='0';\n\tkm=s->s2->key_material;\n\tfor (i=0; i<s->s2->key_material_length; i+=MD5_DIGEST_LENGTH)\n\t\t{\n\t\tMD5_Init(&ctx);\n\t\tMD5_Update(&ctx,s->session->master_key,s->session->master_key_length);\n\t\tMD5_Update(&ctx,(unsigned char *)&c,1);\n\t\tc++;\n\t\tMD5_Update(&ctx,s->s2->challenge,s->s2->challenge_length);\n\t\tMD5_Update(&ctx,s->s2->conn_id,s->s2->conn_id_length);\n\t\tMD5_Final(km,&ctx);\n\t\tkm+=MD5_DIGEST_LENGTH;\n\t\t}\n\t}", 'void MD5_Update(MD5_CTX *c, const void *_data, unsigned long len)\n\t{\n\tregister const unsigned char *data=_data;\n\tregister ULONG *p;\n\tint sw,sc;\n\tULONG l;\n\tif (len == 0) return;\n\tl=(c->Nl+(len<<3))&0xffffffffL;\n\tif (l < c->Nl)\n\t\tc->Nh++;\n\tc->Nh+=(len>>29);\n\tc->Nl=l;\n\tif (c->num != 0)\n\t\t{\n\t\tp=c->data;\n\t\tsw=c->num>>2;\n\t\tsc=c->num&0x03;\n\t\tif ((c->num+len) >= MD5_CBLOCK)\n\t\t\t{\n\t\t\tl= p[sw];\n\t\t\tp_c2l(data,l,sc);\n\t\t\tp[sw++]=l;\n\t\t\tfor (; sw<MD5_LBLOCK; sw++)\n\t\t\t\t{\n\t\t\t\tc2l(data,l);\n\t\t\t\tp[sw]=l;\n\t\t\t\t}\n\t\t\tlen-=(MD5_CBLOCK-c->num);\n\t\t\tmd5_block(c,p,64);\n\t\t\tc->num=0;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tint ew,ec;\n\t\t\tc->num+=(int)len;\n\t\t\tif ((sc+len) < 4)\n\t\t\t\t{\n\t\t\t\tl= p[sw];\n\t\t\t\tp_c2l_p(data,l,sc,len);\n\t\t\t\tp[sw]=l;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tew=(c->num>>2);\n\t\t\t\tec=(c->num&0x03);\n\t\t\t\tl= p[sw];\n\t\t\t\tp_c2l(data,l,sc);\n\t\t\t\tp[sw++]=l;\n\t\t\t\tfor (; sw < ew; sw++)\n\t\t\t\t\t{ c2l(data,l); p[sw]=l; }\n\t\t\t\tif (ec)\n\t\t\t\t\t{\n\t\t\t\t\tc2l_p(data,l,ec);\n\t\t\t\t\tp[sw]=l;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\treturn;\n\t\t\t}\n\t\t}\n#ifdef L_ENDIAN\n\tif ((((unsigned long)data)%sizeof(ULONG)) == 0)\n\t\t{\n\t\tsw=(int)len/MD5_CBLOCK;\n\t\tif (sw > 0)\n\t\t\t{\n\t\t\tsw*=MD5_CBLOCK;\n\t\t\tmd5_block(c,(ULONG *)data,sw);\n\t\t\tdata+=sw;\n\t\t\tlen-=sw;\n\t\t\t}\n\t\t}\n#endif\n\tp=c->data;\n\twhile (len >= MD5_CBLOCK)\n\t\t{\n#if defined(L_ENDIAN) || defined(B_ENDIAN)\n\t\tif (p != (unsigned long *)data)\n\t\t\tmemcpy(p,data,MD5_CBLOCK);\n\t\tdata+=MD5_CBLOCK;\n#ifdef B_ENDIAN\n\t\tfor (sw=(MD5_LBLOCK/4); sw; sw--)\n\t\t\t{\n\t\t\tEndian_Reverse32(p[0]);\n\t\t\tEndian_Reverse32(p[1]);\n\t\t\tEndian_Reverse32(p[2]);\n\t\t\tEndian_Reverse32(p[3]);\n\t\t\tp+=4;\n\t\t\t}\n#endif\n#else\n\t\tfor (sw=(MD5_LBLOCK/4); sw; sw--)\n\t\t\t{\n\t\t\tc2l(data,l); *(p++)=l;\n\t\t\tc2l(data,l); *(p++)=l;\n\t\t\tc2l(data,l); *(p++)=l;\n\t\t\tc2l(data,l); *(p++)=l;\n\t\t\t}\n#endif\n\t\tp=c->data;\n\t\tmd5_block(c,p,64);\n\t\tlen-=MD5_CBLOCK;\n\t\t}\n\tsc=(int)len;\n\tc->num=sc;\n\tif (sc)\n\t\t{\n\t\tsw=sc>>2;\n#ifdef L_ENDIAN\n\t\tp[sw]=0;\n\t\tmemcpy(p,data,sc);\n#else\n\t\tsc&=0x03;\n\t\tfor ( ; sw; sw--)\n\t\t\t{ c2l(data,l); *(p++)=l; }\n\t\tc2l_p(data,l,sc);\n\t\t*p=l;\n#endif\n\t\t}\n\t}']
36,425
0
https://github.com/libav/libav/blob/d5cc1ed723cfbbf71ea005ce1a2e2f5b55a9f631/libavformat/rtsp.c/#L696
static int rtsp_open_transport_ctx(AVFormatContext *s, RTSPStream *rtsp_st) { RTSPState *rt = s->priv_data; AVStream *st = NULL; if (rtsp_st->stream_index >= 0) st = s->streams[rtsp_st->stream_index]; if (!st) s->ctx_flags |= AVFMTCTX_NOHEADER; if (s->oformat) { rtsp_st->transport_priv = rtsp_rtp_mux_open(s, st, rtsp_st->rtp_handle); rtsp_st->rtp_handle = NULL; } else if (rt->transport == RTSP_TRANSPORT_RDT) rtsp_st->transport_priv = ff_rdt_parse_open(s, st->index, rtsp_st->dynamic_protocol_context, rtsp_st->dynamic_handler); else rtsp_st->transport_priv = rtp_parse_open(s, st, rtsp_st->rtp_handle, rtsp_st->sdp_payload_type, &rtsp_st->rtp_payload_data); if (!rtsp_st->transport_priv) { return AVERROR(ENOMEM); } else if (rt->transport != RTSP_TRANSPORT_RDT) { if (rtsp_st->dynamic_handler) { rtp_parse_set_dynamic_protocol(rtsp_st->transport_priv, rtsp_st->dynamic_protocol_context, rtsp_st->dynamic_handler); } } return 0; }
['static int rtsp_open_transport_ctx(AVFormatContext *s, RTSPStream *rtsp_st)\n{\n RTSPState *rt = s->priv_data;\n AVStream *st = NULL;\n if (rtsp_st->stream_index >= 0)\n st = s->streams[rtsp_st->stream_index];\n if (!st)\n s->ctx_flags |= AVFMTCTX_NOHEADER;\n if (s->oformat) {\n rtsp_st->transport_priv = rtsp_rtp_mux_open(s, st, rtsp_st->rtp_handle);\n rtsp_st->rtp_handle = NULL;\n } else if (rt->transport == RTSP_TRANSPORT_RDT)\n rtsp_st->transport_priv = ff_rdt_parse_open(s, st->index,\n rtsp_st->dynamic_protocol_context,\n rtsp_st->dynamic_handler);\n else\n rtsp_st->transport_priv = rtp_parse_open(s, st, rtsp_st->rtp_handle,\n rtsp_st->sdp_payload_type,\n &rtsp_st->rtp_payload_data);\n if (!rtsp_st->transport_priv) {\n return AVERROR(ENOMEM);\n } else if (rt->transport != RTSP_TRANSPORT_RDT) {\n if (rtsp_st->dynamic_handler) {\n rtp_parse_set_dynamic_protocol(rtsp_st->transport_priv,\n rtsp_st->dynamic_protocol_context,\n rtsp_st->dynamic_handler);\n }\n }\n return 0;\n}']
36,426
0
https://github.com/libav/libav/blob/df84d7d9bdf6b8e6896c711880f130b72738c828/libavcodec/mpegaudiodec.c/#L887
void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset, MPA_INT *window, int *dither_state, OUT_INT *samples, int incr, int32_t sb_samples[SBLIMIT]) { register MPA_INT *synth_buf; register const MPA_INT *w, *w2, *p; int j, offset; OUT_INT *samples2; #if FRAC_BITS <= 15 int32_t tmp[32]; int sum, sum2; #else int64_t sum, sum2; #endif offset = *synth_buf_offset; synth_buf = synth_buf_ptr + offset; #if FRAC_BITS <= 15 dct32(tmp, sb_samples); for(j=0;j<32;j++) { synth_buf[j] = av_clip_int16(tmp[j]); } #else dct32(synth_buf, sb_samples); #endif memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT)); samples2 = samples + 31 * incr; w = window; w2 = window + 31; sum = *dither_state; p = synth_buf + 16; SUM8(MACS, sum, w, p); p = synth_buf + 48; SUM8(MLSS, sum, w + 32, p); *samples = round_sample(&sum); samples += incr; w++; for(j=1;j<16;j++) { sum2 = 0; p = synth_buf + 16 + j; SUM8P2(sum, MACS, sum2, MLSS, w, w2, p); p = synth_buf + 48 - j; SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p); *samples = round_sample(&sum); samples += incr; sum += sum2; *samples2 = round_sample(&sum); samples2 -= incr; w++; w2--; } p = synth_buf + 32; SUM8(MLSS, sum, w + 32, p); *samples = round_sample(&sum); *dither_state= sum; offset = (offset - 32) & 511; *synth_buf_offset = offset; }
['static void mpc_synth(MPCContext *c, int16_t *out)\n{\n int dither_state = 0;\n int i, ch;\n OUT_INT samples[MPA_MAX_CHANNELS * MPA_FRAME_SIZE], *samples_ptr;\n for(ch = 0; ch < 2; ch++){\n samples_ptr = samples + ch;\n for(i = 0; i < SAMPLES_PER_BAND; i++) {\n ff_mpa_synth_filter(c->synth_buf[ch], &(c->synth_buf_offset[ch]),\n ff_mpa_synth_window, &dither_state,\n samples_ptr, 2,\n c->sb_samples[ch][i]);\n samples_ptr += 64;\n }\n }\n for(i = 0; i < MPC_FRAME_SIZE*2; i++)\n *out++=samples[i];\n}', 'void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,\n MPA_INT *window, int *dither_state,\n OUT_INT *samples, int incr,\n int32_t sb_samples[SBLIMIT])\n{\n register MPA_INT *synth_buf;\n register const MPA_INT *w, *w2, *p;\n int j, offset;\n OUT_INT *samples2;\n#if FRAC_BITS <= 15\n int32_t tmp[32];\n int sum, sum2;\n#else\n int64_t sum, sum2;\n#endif\n offset = *synth_buf_offset;\n synth_buf = synth_buf_ptr + offset;\n#if FRAC_BITS <= 15\n dct32(tmp, sb_samples);\n for(j=0;j<32;j++) {\n synth_buf[j] = av_clip_int16(tmp[j]);\n }\n#else\n dct32(synth_buf, sb_samples);\n#endif\n memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));\n samples2 = samples + 31 * incr;\n w = window;\n w2 = window + 31;\n sum = *dither_state;\n p = synth_buf + 16;\n SUM8(MACS, sum, w, p);\n p = synth_buf + 48;\n SUM8(MLSS, sum, w + 32, p);\n *samples = round_sample(&sum);\n samples += incr;\n w++;\n for(j=1;j<16;j++) {\n sum2 = 0;\n p = synth_buf + 16 + j;\n SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);\n p = synth_buf + 48 - j;\n SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);\n *samples = round_sample(&sum);\n samples += incr;\n sum += sum2;\n *samples2 = round_sample(&sum);\n samples2 -= incr;\n w++;\n w2--;\n }\n p = synth_buf + 32;\n SUM8(MLSS, sum, w + 32, p);\n *samples = round_sample(&sum);\n *dither_state= sum;\n offset = (offset - 32) & 511;\n *synth_buf_offset = offset;\n}']
36,427
0
https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/crypto/bn/bn_lib.c/#L377
BIGNUM *bn_expand2(BIGNUM *b, int words) { BN_ULONG *A,*B,*a; int i,j; bn_check_top(b); if (words > b->max) { bn_check_top(b); if (BN_get_flags(b,BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND2,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return(NULL); } a=A=(BN_ULONG *)Malloc(sizeof(BN_ULONG)*(words+1)); if (A == NULL) { BNerr(BN_F_BN_EXPAND2,ERR_R_MALLOC_FAILURE); return(NULL); } memset(A,0x5c,sizeof(BN_ULONG)*(words+1)); #if 1 B=b->d; if (B != NULL) { for (i=b->top&(~7); i>0; i-=8) { A[0]=B[0]; A[1]=B[1]; A[2]=B[2]; A[3]=B[3]; A[4]=B[4]; A[5]=B[5]; A[6]=B[6]; A[7]=B[7]; A+=8; B+=8; } switch (b->top&7) { case 7: A[6]=B[6]; case 6: A[5]=B[5]; case 5: A[4]=B[4]; case 4: A[3]=B[3]; case 3: A[2]=B[2]; case 2: A[1]=B[1]; case 1: A[0]=B[0]; case 0: ; } Free(b->d); } b->d=a; b->max=words; B= &(b->d[b->top]); j=(b->max - b->top) & ~7; for (i=0; i<j; i+=8) { B[0]=0; B[1]=0; B[2]=0; B[3]=0; B[4]=0; B[5]=0; B[6]=0; B[7]=0; B+=8; } j=(b->max - b->top) & 7; for (i=0; i<j; i++) { B[0]=0; B++; } #else memcpy(a->d,b->d,sizeof(b->d[0])*b->top); #endif } return(b); }
['int test_rshift(BIO *bp)\n\t{\n\tBIGNUM *a,*b,*c;\n\tint i;\n\ta=BN_new();\n\tb=BN_new();\n\tc=BN_new();\n\tBN_one(c);\n\tBN_rand(a,200,0,0);\n\ta->neg=rand_neg();\n\tfor (i=0; i<70; i++)\n\t\t{\n\t\tBN_rshift(b,a,i+1);\n\t\tBN_add(c,c,c);\n\t\tif (bp != NULL)\n\t\t\t{\n\t\t\tif (!results)\n\t\t\t\t{\n\t\t\t\tBN_print(bp,a);\n\t\t\t\tBIO_puts(bp," / ");\n\t\t\t\tBN_print(bp,c);\n\t\t\t\tBIO_puts(bp," - ");\n\t\t\t\t}\n\t\t\tBN_print(bp,b);\n\t\t\tBIO_puts(bp,"\\n");\n\t\t\t}\n\t\t}\n\tBN_free(a);\n\tBN_free(b);\n\tBN_free(c);\n\treturn(1);\n\t}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n\t{\n\tint i,n;\n\tif (bn_expand(a,sizeof(BN_ULONG)*8) == NULL) return(0);\n\tn=sizeof(BN_ULONG)/BN_BYTES;\n\ta->neg=0;\n\ta->top=0;\n\ta->d[0]=(BN_ULONG)w&BN_MASK2;\n\tif (a->d[0] != 0) a->top=1;\n\tfor (i=1; i<n; i++)\n\t\t{\n#ifndef SIXTY_FOUR_BIT\n\t\tw>>=BN_BITS4;\n\t\tw>>=BN_BITS4;\n#endif\n\t\ta->d[i]=(BN_ULONG)w&BN_MASK2;\n\t\tif (a->d[i] != 0) a->top=i+1;\n\t\t}\n\treturn(1);\n\t}', 'int BN_rand(BIGNUM *rnd, int bits, int top, int bottom)\n\t{\n\tunsigned char *buf=NULL;\n\tint ret=0,bit,bytes,mask;\n\ttime_t tim;\n\tbytes=(bits+7)/8;\n\tbit=(bits-1)%8;\n\tmask=0xff<<bit;\n\tbuf=(unsigned char *)Malloc(bytes);\n\tif (buf == NULL)\n\t\t{\n\t\tBNerr(BN_F_BN_RAND,ERR_R_MALLOC_FAILURE);\n\t\tgoto err;\n\t\t}\n\ttime(&tim);\n\tRAND_seed(&tim,sizeof(tim));\n\tRAND_bytes(buf,(int)bytes);\n\tif (top)\n\t\t{\n\t\tif (bit == 0)\n\t\t\t{\n\t\t\tbuf[0]=1;\n\t\t\tbuf[1]|=0x80;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tbuf[0]|=(3<<(bit-1));\n\t\t\tbuf[0]&= ~(mask<<1);\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tbuf[0]|=(1<<bit);\n\t\tbuf[0]&= ~(mask<<1);\n\t\t}\n\tif (bottom)\n\t\tbuf[bytes-1]|=1;\n\tif (!BN_bin2bn(buf,bytes,rnd)) goto err;\n\tret=1;\nerr:\n\tif (buf != NULL)\n\t\t{\n\t\tmemset(buf,0,bytes);\n\t\tFree(buf);\n\t\t}\n\treturn(ret);\n\t}', 'int BN_rshift(BIGNUM *r, BIGNUM *a, int n)\n\t{\n\tint i,j,nw,lb,rb;\n\tBN_ULONG *t,*f;\n\tBN_ULONG l,tmp;\n\tnw=n/BN_BITS2;\n\trb=n%BN_BITS2;\n\tlb=BN_BITS2-rb;\n\tif (nw > a->top)\n\t\t{\n\t\tBN_zero(r);\n\t\treturn(1);\n\t\t}\n\tif (r != a)\n\t\t{\n\t\tr->neg=a->neg;\n\t\tif (bn_wexpand(r,a->top-nw+1) == NULL) return(0);\n\t\t}\n\tf= &(a->d[nw]);\n\tt=r->d;\n\tj=a->top-nw;\n\tr->top=j;\n\tif (rb == 0)\n\t\t{\n\t\tfor (i=j+1; i > 0; i--)\n\t\t\t*(t++)= *(f++);\n\t\t}\n\telse\n\t\t{\n\t\tl= *(f++);\n\t\tfor (i=1; i<j; i++)\n\t\t\t{\n\t\t\ttmp =(l>>rb)&BN_MASK2;\n\t\t\tl= *(f++);\n\t\t\t*(t++) =(tmp|(l<<lb))&BN_MASK2;\n\t\t\t}\n\t\t*(t++) =(l>>rb)&BN_MASK2;\n\t\t}\n\t*t=0;\n\tbn_fix_top(r);\n\treturn(1);\n\t}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n\t{\n\tBN_ULONG *A,*B,*a;\n\tint i,j;\n\tbn_check_top(b);\n\tif (words > b->max)\n\t\t{\n\t\tbn_check_top(b);\n\t\tif (BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_EXPAND2,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n\t\t\treturn(NULL);\n\t\t\t}\n\t\ta=A=(BN_ULONG *)Malloc(sizeof(BN_ULONG)*(words+1));\n\t\tif (A == NULL)\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_EXPAND2,ERR_R_MALLOC_FAILURE);\n\t\t\treturn(NULL);\n\t\t\t}\nmemset(A,0x5c,sizeof(BN_ULONG)*(words+1));\n#if 1\n\t\tB=b->d;\n\t\tif (B != NULL)\n\t\t\t{\n\t\t\tfor (i=b->top&(~7); i>0; i-=8)\n\t\t\t\t{\n\t\t\t\tA[0]=B[0]; A[1]=B[1]; A[2]=B[2]; A[3]=B[3];\n\t\t\t\tA[4]=B[4]; A[5]=B[5]; A[6]=B[6]; A[7]=B[7];\n\t\t\t\tA+=8;\n\t\t\t\tB+=8;\n\t\t\t\t}\n\t\t\tswitch (b->top&7)\n\t\t\t\t{\n\t\t\tcase 7:\n\t\t\t\tA[6]=B[6];\n\t\t\tcase 6:\n\t\t\t\tA[5]=B[5];\n\t\t\tcase 5:\n\t\t\t\tA[4]=B[4];\n\t\t\tcase 4:\n\t\t\t\tA[3]=B[3];\n\t\t\tcase 3:\n\t\t\t\tA[2]=B[2];\n\t\t\tcase 2:\n\t\t\t\tA[1]=B[1];\n\t\t\tcase 1:\n\t\t\t\tA[0]=B[0];\n\t\t\tcase 0:\n\t\t\t\t;\n\t\t\t\t}\n\t\t\tFree(b->d);\n\t\t\t}\n\t\tb->d=a;\n\t\tb->max=words;\n\t\tB= &(b->d[b->top]);\n\t\tj=(b->max - b->top) & ~7;\n\t\tfor (i=0; i<j; i+=8)\n\t\t\t{\n\t\t\tB[0]=0; B[1]=0; B[2]=0; B[3]=0;\n\t\t\tB[4]=0; B[5]=0; B[6]=0; B[7]=0;\n\t\t\tB+=8;\n\t\t\t}\n\t\tj=(b->max - b->top) & 7;\n\t\tfor (i=0; i<j; i++)\n\t\t\t{\n\t\t\tB[0]=0;\n\t\t\tB++;\n\t\t\t}\n#else\n\t\t\tmemcpy(a->d,b->d,sizeof(b->d[0])*b->top);\n#endif\n\t\t}\n\treturn(b);\n\t}']
36,428
0
https://github.com/openssl/openssl/blob/47ddf355b46eae8c846e411f44531e928e04adf5/apps/speed.c/#L981
int MAIN(int argc, char **argv) { ENGINE *e; unsigned char *buf=NULL,*buf2=NULL; int mret=1; #define ALGOR_NUM 16 #define SIZE_NUM 5 #define RSA_NUM 4 #define DSA_NUM 3 long count,rsa_count,save_count=0; int i,j,k; #ifndef OPENSSL_NO_RSA unsigned rsa_num; #endif #ifndef OPENSSL_NO_MD2 unsigned char md2[MD2_DIGEST_LENGTH]; #endif #ifndef OPENSSL_NO_MDC2 unsigned char mdc2[MDC2_DIGEST_LENGTH]; #endif #ifndef OPENSSL_NO_MD4 unsigned char md4[MD4_DIGEST_LENGTH]; #endif #ifndef OPENSSL_NO_MD5 unsigned char md5[MD5_DIGEST_LENGTH]; unsigned char hmac[MD5_DIGEST_LENGTH]; #endif #ifndef OPENSSL_NO_SHA unsigned char sha[SHA_DIGEST_LENGTH]; #endif #ifndef OPENSSL_NO_RIPEMD unsigned char rmd160[RIPEMD160_DIGEST_LENGTH]; #endif #ifndef OPENSSL_NO_RC4 RC4_KEY rc4_ks; #endif #ifndef OPENSSL_NO_RC5 RC5_32_KEY rc5_ks; #endif #ifndef OPENSSL_NO_RC2 RC2_KEY rc2_ks; #endif #ifndef OPENSSL_NO_IDEA IDEA_KEY_SCHEDULE idea_ks; #endif #ifndef OPENSSL_NO_BF BF_KEY bf_ks; #endif #ifndef OPENSSL_NO_CAST CAST_KEY cast_ks; #endif static unsigned char key16[16]= {0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0, 0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12}; unsigned char iv[8]; #ifndef OPENSSL_NO_DES des_cblock *buf_as_des_cblock = NULL; static des_cblock key ={0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0}; static des_cblock key2={0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12}; static des_cblock key3={0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12,0x34}; des_key_schedule sch,sch2,sch3; #endif #define D_MD2 0 #define D_MDC2 1 #define D_MD4 2 #define D_MD5 3 #define D_HMAC 4 #define D_SHA1 5 #define D_RMD160 6 #define D_RC4 7 #define D_CBC_DES 8 #define D_EDE3_DES 9 #define D_CBC_IDEA 10 #define D_CBC_RC2 11 #define D_CBC_RC5 12 #define D_CBC_BF 13 #define D_CBC_CAST 14 #define D_EVP 15 double d,results[ALGOR_NUM][SIZE_NUM]; static int lengths[SIZE_NUM]={8,64,256,1024,8*1024}; long c[ALGOR_NUM][SIZE_NUM]; static const char *names[ALGOR_NUM]={ "md2","mdc2","md4","md5","hmac(md5)","sha1","rmd160","rc4", "des cbc","des ede3","idea cbc", "rc2 cbc","rc5-32/12 cbc","blowfish cbc","cast cbc"}; #define R_DSA_512 0 #define R_DSA_1024 1 #define R_DSA_2048 2 #define R_RSA_512 0 #define R_RSA_1024 1 #define R_RSA_2048 2 #define R_RSA_4096 3 #ifndef OPENSSL_NO_RSA RSA *rsa_key[RSA_NUM]; long rsa_c[RSA_NUM][2]; double rsa_results[RSA_NUM][2]; static unsigned int rsa_bits[RSA_NUM]={512,1024,2048,4096}; static unsigned char *rsa_data[RSA_NUM]= {test512,test1024,test2048,test4096}; static int rsa_data_length[RSA_NUM]={ sizeof(test512),sizeof(test1024), sizeof(test2048),sizeof(test4096)}; #endif #ifndef OPENSSL_NO_DSA DSA *dsa_key[DSA_NUM]; long dsa_c[DSA_NUM][2]; double dsa_results[DSA_NUM][2]; static unsigned int dsa_bits[DSA_NUM]={512,1024,2048}; #endif int rsa_doit[RSA_NUM]; int dsa_doit[DSA_NUM]; int doit[ALGOR_NUM]; int pr_header=0; int usertime=1; const EVP_CIPHER *evp=NULL; #ifndef TIMES usertime=-1; #endif apps_startup(); memset(results, 0, sizeof(results)); #ifndef OPENSSL_NO_DSA memset(dsa_key,0,sizeof(dsa_key)); #endif if (bio_err == NULL) if ((bio_err=BIO_new(BIO_s_file())) != NULL) BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT); #ifndef OPENSSL_NO_RSA memset(rsa_key,0,sizeof(rsa_key)); for (i=0; i<RSA_NUM; i++) rsa_key[i]=NULL; #endif if ((buf=(unsigned char *)OPENSSL_malloc((int)BUFSIZE)) == NULL) { BIO_printf(bio_err,"out of memory\n"); goto end; } #ifndef OPENSSL_NO_DES buf_as_des_cblock = (des_cblock *)buf; #endif if ((buf2=(unsigned char *)OPENSSL_malloc((int)BUFSIZE)) == NULL) { BIO_printf(bio_err,"out of memory\n"); goto end; } memset(c,0,sizeof(c)); memset(iv,0,sizeof(iv)); for (i=0; i<ALGOR_NUM; i++) doit[i]=0; for (i=0; i<RSA_NUM; i++) rsa_doit[i]=0; for (i=0; i<DSA_NUM; i++) dsa_doit[i]=0; j=0; argc--; argv++; while (argc) { if ((argc > 0) && (strcmp(*argv,"-elapsed") == 0)) usertime = 0; else if ((argc > 0) && (strcmp(*argv,"-evp") == 0)) { argc--; argv++; if(argc == 0) { BIO_printf(bio_err,"no EVP given\n"); goto end; } evp=EVP_get_cipherbyname(*argv); if(!evp) { BIO_printf(bio_err,"%s is an unknown cipher\n",*argv); goto end; } doit[D_EVP]=1; } else if ((argc > 0) && (strcmp(*argv,"-engine") == 0)) { argc--; argv++; if(argc == 0) { BIO_printf(bio_err,"no engine given\n"); goto end; } if((e = ENGINE_by_id(*argv)) == NULL) { BIO_printf(bio_err,"invalid engine \"%s\"\n", *argv); goto end; } if(!ENGINE_set_default(e, ENGINE_METHOD_ALL)) { BIO_printf(bio_err,"can't use that engine\n"); goto end; } BIO_printf(bio_err,"engine \"%s\" set.\n", *argv); ENGINE_free(e); j--; } else #ifndef OPENSSL_NO_MD2 if (strcmp(*argv,"md2") == 0) doit[D_MD2]=1; else #endif #ifndef OPENSSL_NO_MDC2 if (strcmp(*argv,"mdc2") == 0) doit[D_MDC2]=1; else #endif #ifndef OPENSSL_NO_MD4 if (strcmp(*argv,"md4") == 0) doit[D_MD4]=1; else #endif #ifndef OPENSSL_NO_MD5 if (strcmp(*argv,"md5") == 0) doit[D_MD5]=1; else #endif #ifndef OPENSSL_NO_MD5 if (strcmp(*argv,"hmac") == 0) doit[D_HMAC]=1; else #endif #ifndef OPENSSL_NO_SHA if (strcmp(*argv,"sha1") == 0) doit[D_SHA1]=1; else if (strcmp(*argv,"sha") == 0) doit[D_SHA1]=1; else #endif #ifndef OPENSSL_NO_RIPEMD if (strcmp(*argv,"ripemd") == 0) doit[D_RMD160]=1; else if (strcmp(*argv,"rmd160") == 0) doit[D_RMD160]=1; else if (strcmp(*argv,"ripemd160") == 0) doit[D_RMD160]=1; else #endif #ifndef OPENSSL_NO_RC4 if (strcmp(*argv,"rc4") == 0) doit[D_RC4]=1; else #endif #ifndef OPENSSL_NO_DES if (strcmp(*argv,"des-cbc") == 0) doit[D_CBC_DES]=1; else if (strcmp(*argv,"des-ede3") == 0) doit[D_EDE3_DES]=1; else #endif #ifndef OPENSSL_NO_RSA #if 0 if (strcmp(*argv,"rsaref") == 0) { RSA_set_default_openssl_method(RSA_PKCS1_RSAref()); j--; } else #endif #ifndef RSA_NULL if (strcmp(*argv,"openssl") == 0) { RSA_set_default_openssl_method(RSA_PKCS1_SSLeay()); j--; } else #endif #endif if (strcmp(*argv,"dsa512") == 0) dsa_doit[R_DSA_512]=2; else if (strcmp(*argv,"dsa1024") == 0) dsa_doit[R_DSA_1024]=2; else if (strcmp(*argv,"dsa2048") == 0) dsa_doit[R_DSA_2048]=2; else if (strcmp(*argv,"rsa512") == 0) rsa_doit[R_RSA_512]=2; else if (strcmp(*argv,"rsa1024") == 0) rsa_doit[R_RSA_1024]=2; else if (strcmp(*argv,"rsa2048") == 0) rsa_doit[R_RSA_2048]=2; else if (strcmp(*argv,"rsa4096") == 0) rsa_doit[R_RSA_4096]=2; else #ifndef OPENSSL_NO_RC2 if (strcmp(*argv,"rc2-cbc") == 0) doit[D_CBC_RC2]=1; else if (strcmp(*argv,"rc2") == 0) doit[D_CBC_RC2]=1; else #endif #ifndef OPENSSL_NO_RC5 if (strcmp(*argv,"rc5-cbc") == 0) doit[D_CBC_RC5]=1; else if (strcmp(*argv,"rc5") == 0) doit[D_CBC_RC5]=1; else #endif #ifndef OPENSSL_NO_IDEA if (strcmp(*argv,"idea-cbc") == 0) doit[D_CBC_IDEA]=1; else if (strcmp(*argv,"idea") == 0) doit[D_CBC_IDEA]=1; else #endif #ifndef OPENSSL_NO_BF if (strcmp(*argv,"bf-cbc") == 0) doit[D_CBC_BF]=1; else if (strcmp(*argv,"blowfish") == 0) doit[D_CBC_BF]=1; else if (strcmp(*argv,"bf") == 0) doit[D_CBC_BF]=1; else #endif #ifndef OPENSSL_NO_CAST if (strcmp(*argv,"cast-cbc") == 0) doit[D_CBC_CAST]=1; else if (strcmp(*argv,"cast") == 0) doit[D_CBC_CAST]=1; else if (strcmp(*argv,"cast5") == 0) doit[D_CBC_CAST]=1; else #endif #ifndef OPENSSL_NO_DES if (strcmp(*argv,"des") == 0) { doit[D_CBC_DES]=1; doit[D_EDE3_DES]=1; } else #endif #ifndef OPENSSL_NO_RSA if (strcmp(*argv,"rsa") == 0) { rsa_doit[R_RSA_512]=1; rsa_doit[R_RSA_1024]=1; rsa_doit[R_RSA_2048]=1; rsa_doit[R_RSA_4096]=1; } else #endif #ifndef OPENSSL_NO_DSA if (strcmp(*argv,"dsa") == 0) { dsa_doit[R_DSA_512]=1; dsa_doit[R_DSA_1024]=1; } else #endif { BIO_printf(bio_err,"Error: bad option or value\n"); BIO_printf(bio_err,"\n"); BIO_printf(bio_err,"Available values:\n"); #ifndef OPENSSL_NO_MD2 BIO_printf(bio_err,"md2 "); #endif #ifndef OPENSSL_NO_MDC2 BIO_printf(bio_err,"mdc2 "); #endif #ifndef OPENSSL_NO_MD4 BIO_printf(bio_err,"md4 "); #endif #ifndef OPENSSL_NO_MD5 BIO_printf(bio_err,"md5 "); #ifndef OPENSSL_NO_HMAC BIO_printf(bio_err,"hmac "); #endif #endif #ifndef OPENSSL_NO_SHA1 BIO_printf(bio_err,"sha1 "); #endif #ifndef OPENSSL_NO_RIPEMD160 BIO_printf(bio_err,"rmd160"); #endif #if !defined(OPENSSL_NO_MD2) || !defined(OPENSSL_NO_MDC2) || \ !defined(OPENSSL_NO_MD4) || !defined(OPENSSL_NO_MD5) || \ !defined(OPENSSL_NO_SHA1) || !defined(OPENSSL_NO_RIPEMD160) BIO_printf(bio_err,"\n"); #endif #ifndef OPENSSL_NO_IDEA BIO_printf(bio_err,"idea-cbc "); #endif #ifndef OPENSSL_NO_RC2 BIO_printf(bio_err,"rc2-cbc "); #endif #ifndef OPENSSL_NO_RC5 BIO_printf(bio_err,"rc5-cbc "); #endif #ifndef OPENSSL_NO_BF BIO_printf(bio_err,"bf-cbc"); #endif #if !defined(OPENSSL_NO_IDEA) || !defined(OPENSSL_NO_RC2) || \ !defined(OPENSSL_NO_BF) || !defined(OPENSSL_NO_RC5) BIO_printf(bio_err,"\n"); #endif BIO_printf(bio_err,"des-cbc des-ede3 "); #ifndef OPENSSL_NO_RC4 BIO_printf(bio_err,"rc4"); #endif BIO_printf(bio_err,"\n"); #ifndef OPENSSL_NO_RSA BIO_printf(bio_err,"rsa512 rsa1024 rsa2048 rsa4096\n"); #endif #ifndef OPENSSL_NO_DSA BIO_printf(bio_err,"dsa512 dsa1024 dsa2048\n"); #endif #ifndef OPENSSL_NO_IDEA BIO_printf(bio_err,"idea "); #endif #ifndef OPENSSL_NO_RC2 BIO_printf(bio_err,"rc2 "); #endif #ifndef OPENSSL_NO_DES BIO_printf(bio_err,"des "); #endif #ifndef OPENSSL_NO_RSA BIO_printf(bio_err,"rsa "); #endif #ifndef OPENSSL_NO_BF BIO_printf(bio_err,"blowfish"); #endif #if !defined(OPENSSL_NO_IDEA) || !defined(OPENSSL_NO_RC2) || \ !defined(OPENSSL_NO_DES) || !defined(OPENSSL_NO_RSA) || \ !defined(OPENSSL_NO_BF) BIO_printf(bio_err,"\n"); #endif BIO_printf(bio_err,"\n"); BIO_printf(bio_err,"Available options:\n"); #ifdef TIMES BIO_printf(bio_err,"-elapsed measure time in real time instead of CPU user time.\n"); #endif BIO_printf(bio_err,"-engine e use engine e, possibly a hardware device.\n"); goto end; } argc--; argv++; j++; } if (j == 0) { for (i=0; i<ALGOR_NUM; i++) { if (i != D_EVP) doit[i]=1; } for (i=0; i<RSA_NUM; i++) rsa_doit[i]=1; for (i=0; i<DSA_NUM; i++) dsa_doit[i]=1; } for (i=0; i<ALGOR_NUM; i++) if (doit[i]) pr_header++; if (usertime == 0) BIO_printf(bio_err,"You have chosen to measure elapsed time instead of user CPU time.\n"); if (usertime <= 0) { BIO_printf(bio_err,"To get the most accurate results, try to run this\n"); BIO_printf(bio_err,"program when this computer is idle.\n"); } #ifndef OPENSSL_NO_RSA for (i=0; i<RSA_NUM; i++) { const unsigned char *p; p=rsa_data[i]; rsa_key[i]=d2i_RSAPrivateKey(NULL,&p,rsa_data_length[i]); if (rsa_key[i] == NULL) { BIO_printf(bio_err,"internal error loading RSA key number %d\n",i); goto end; } #if 0 else { BIO_printf(bio_err,"Loaded RSA key, %d bit modulus and e= 0x",BN_num_bits(rsa_key[i]->n)); BN_print(bio_err,rsa_key[i]->e); BIO_printf(bio_err,"\n"); } #endif } #endif #ifndef OPENSSL_NO_DSA dsa_key[0]=get_dsa512(); dsa_key[1]=get_dsa1024(); dsa_key[2]=get_dsa2048(); #endif #ifndef OPENSSL_NO_DES des_set_key_unchecked(&key,sch); des_set_key_unchecked(&key2,sch2); des_set_key_unchecked(&key3,sch3); #endif #ifndef OPENSSL_NO_IDEA idea_set_encrypt_key(key16,&idea_ks); #endif #ifndef OPENSSL_NO_RC4 RC4_set_key(&rc4_ks,16,key16); #endif #ifndef OPENSSL_NO_RC2 RC2_set_key(&rc2_ks,16,key16,128); #endif #ifndef OPENSSL_NO_RC5 RC5_32_set_key(&rc5_ks,16,key16,12); #endif #ifndef OPENSSL_NO_BF BF_set_key(&bf_ks,16,key16); #endif #ifndef OPENSSL_NO_CAST CAST_set_key(&cast_ks,16,key16); #endif #ifndef OPENSSL_NO_RSA memset(rsa_c,0,sizeof(rsa_c)); #endif #ifndef SIGALRM #ifndef OPENSSL_NO_DES BIO_printf(bio_err,"First we calculate the approximate speed ...\n"); count=10; do { long i; count*=2; Time_F(START,usertime); for (i=count; i; i--) des_ecb_encrypt(buf_as_des_cblock,buf_as_des_cblock, &(sch[0]),DES_ENCRYPT); d=Time_F(STOP,usertime); } while (d <3); save_count=count; c[D_MD2][0]=count/10; c[D_MDC2][0]=count/10; c[D_MD4][0]=count; c[D_MD5][0]=count; c[D_HMAC][0]=count; c[D_SHA1][0]=count; c[D_RMD160][0]=count; c[D_RC4][0]=count*5; c[D_CBC_DES][0]=count; c[D_EDE3_DES][0]=count/3; c[D_CBC_IDEA][0]=count; c[D_CBC_RC2][0]=count; c[D_CBC_RC5][0]=count; c[D_CBC_BF][0]=count; c[D_CBC_CAST][0]=count; for (i=1; i<SIZE_NUM; i++) { c[D_MD2][i]=c[D_MD2][0]*4*lengths[0]/lengths[i]; c[D_MDC2][i]=c[D_MDC2][0]*4*lengths[0]/lengths[i]; c[D_MD4][i]=c[D_MD4][0]*4*lengths[0]/lengths[i]; c[D_MD5][i]=c[D_MD5][0]*4*lengths[0]/lengths[i]; c[D_HMAC][i]=c[D_HMAC][0]*4*lengths[0]/lengths[i]; c[D_SHA1][i]=c[D_SHA1][0]*4*lengths[0]/lengths[i]; c[D_RMD160][i]=c[D_RMD160][0]*4*lengths[0]/lengths[i]; } for (i=1; i<SIZE_NUM; i++) { long l0,l1; l0=(long)lengths[i-1]; l1=(long)lengths[i]; c[D_RC4][i]=c[D_RC4][i-1]*l0/l1; c[D_CBC_DES][i]=c[D_CBC_DES][i-1]*l0/l1; c[D_EDE3_DES][i]=c[D_EDE3_DES][i-1]*l0/l1; c[D_CBC_IDEA][i]=c[D_CBC_IDEA][i-1]*l0/l1; c[D_CBC_RC2][i]=c[D_CBC_RC2][i-1]*l0/l1; c[D_CBC_RC5][i]=c[D_CBC_RC5][i-1]*l0/l1; c[D_CBC_BF][i]=c[D_CBC_BF][i-1]*l0/l1; c[D_CBC_CAST][i]=c[D_CBC_CAST][i-1]*l0/l1; } #ifndef OPENSSL_NO_RSA rsa_c[R_RSA_512][0]=count/2000; rsa_c[R_RSA_512][1]=count/400; for (i=1; i<RSA_NUM; i++) { rsa_c[i][0]=rsa_c[i-1][0]/8; rsa_c[i][1]=rsa_c[i-1][1]/4; if ((rsa_doit[i] <= 1) && (rsa_c[i][0] == 0)) rsa_doit[i]=0; else { if (rsa_c[i][0] == 0) { rsa_c[i][0]=1; rsa_c[i][1]=20; } } } #endif dsa_c[R_DSA_512][0]=count/1000; dsa_c[R_DSA_512][1]=count/1000/2; for (i=1; i<DSA_NUM; i++) { dsa_c[i][0]=dsa_c[i-1][0]/4; dsa_c[i][1]=dsa_c[i-1][1]/4; if ((dsa_doit[i] <= 1) && (dsa_c[i][0] == 0)) dsa_doit[i]=0; else { if (dsa_c[i] == 0) { dsa_c[i][0]=1; dsa_c[i][1]=1; } } } #define COND(d) (count < (d)) #define COUNT(d) (d) #else # error "You cannot disable DES on systems without SIGALRM." #endif #else #define COND(c) (run) #define COUNT(d) (count) signal(SIGALRM,sig_done); #endif #ifndef OPENSSL_NO_MD2 if (doit[D_MD2]) { for (j=0; j<SIZE_NUM; j++) { print_message(names[D_MD2],c[D_MD2][j],lengths[j]); Time_F(START,usertime); for (count=0,run=1; COND(c[D_MD2][j]); count++) MD2(buf,(unsigned long)lengths[j],&(md2[0])); d=Time_F(STOP,usertime); BIO_printf(bio_err,"%ld %s's in %.2fs\n", count,names[D_MD2],d); results[D_MD2][j]=((double)count)/d*lengths[j]; } } #endif #ifndef OPENSSL_NO_MDC2 if (doit[D_MDC2]) { for (j=0; j<SIZE_NUM; j++) { print_message(names[D_MDC2],c[D_MDC2][j],lengths[j]); Time_F(START,usertime); for (count=0,run=1; COND(c[D_MDC2][j]); count++) MDC2(buf,(unsigned long)lengths[j],&(mdc2[0])); d=Time_F(STOP,usertime); BIO_printf(bio_err,"%ld %s's in %.2fs\n", count,names[D_MDC2],d); results[D_MDC2][j]=((double)count)/d*lengths[j]; } } #endif #ifndef OPENSSL_NO_MD4 if (doit[D_MD4]) { for (j=0; j<SIZE_NUM; j++) { print_message(names[D_MD4],c[D_MD4][j],lengths[j]); Time_F(START,usertime); for (count=0,run=1; COND(c[D_MD4][j]); count++) MD4(&(buf[0]),(unsigned long)lengths[j],&(md4[0])); d=Time_F(STOP,usertime); BIO_printf(bio_err,"%ld %s's in %.2fs\n", count,names[D_MD4],d); results[D_MD4][j]=((double)count)/d*lengths[j]; } } #endif #ifndef OPENSSL_NO_MD5 if (doit[D_MD5]) { for (j=0; j<SIZE_NUM; j++) { print_message(names[D_MD5],c[D_MD5][j],lengths[j]); Time_F(START,usertime); for (count=0,run=1; COND(c[D_MD5][j]); count++) MD5(&(buf[0]),(unsigned long)lengths[j],&(md5[0])); d=Time_F(STOP,usertime); BIO_printf(bio_err,"%ld %s's in %.2fs\n", count,names[D_MD5],d); results[D_MD5][j]=((double)count)/d*lengths[j]; } } #endif #if !defined(OPENSSL_NO_MD5) && !defined(OPENSSL_NO_HMAC) if (doit[D_HMAC]) { HMAC_CTX hctx; HMAC_Init(&hctx,(unsigned char *)"This is a key...", 16,EVP_md5()); for (j=0; j<SIZE_NUM; j++) { print_message(names[D_HMAC],c[D_HMAC][j],lengths[j]); Time_F(START,usertime); for (count=0,run=1; COND(c[D_HMAC][j]); count++) { HMAC_Init(&hctx,NULL,0,NULL); HMAC_Update(&hctx,buf,lengths[j]); HMAC_Final(&hctx,&(hmac[0]),NULL); } d=Time_F(STOP,usertime); BIO_printf(bio_err,"%ld %s's in %.2fs\n", count,names[D_HMAC],d); results[D_HMAC][j]=((double)count)/d*lengths[j]; } } #endif #ifndef OPENSSL_NO_SHA if (doit[D_SHA1]) { for (j=0; j<SIZE_NUM; j++) { print_message(names[D_SHA1],c[D_SHA1][j],lengths[j]); Time_F(START,usertime); for (count=0,run=1; COND(c[D_SHA1][j]); count++) SHA1(buf,(unsigned long)lengths[j],&(sha[0])); d=Time_F(STOP,usertime); BIO_printf(bio_err,"%ld %s's in %.2fs\n", count,names[D_SHA1],d); results[D_SHA1][j]=((double)count)/d*lengths[j]; } } #endif #ifndef OPENSSL_NO_RIPEMD if (doit[D_RMD160]) { for (j=0; j<SIZE_NUM; j++) { print_message(names[D_RMD160],c[D_RMD160][j],lengths[j]); Time_F(START,usertime); for (count=0,run=1; COND(c[D_RMD160][j]); count++) RIPEMD160(buf,(unsigned long)lengths[j],&(rmd160[0])); d=Time_F(STOP,usertime); BIO_printf(bio_err,"%ld %s's in %.2fs\n", count,names[D_RMD160],d); results[D_RMD160][j]=((double)count)/d*lengths[j]; } } #endif #ifndef OPENSSL_NO_RC4 if (doit[D_RC4]) { for (j=0; j<SIZE_NUM; j++) { print_message(names[D_RC4],c[D_RC4][j],lengths[j]); Time_F(START,usertime); for (count=0,run=1; COND(c[D_RC4][j]); count++) RC4(&rc4_ks,(unsigned int)lengths[j], buf,buf); d=Time_F(STOP,usertime); BIO_printf(bio_err,"%ld %s's in %.2fs\n", count,names[D_RC4],d); results[D_RC4][j]=((double)count)/d*lengths[j]; } } #endif #ifndef OPENSSL_NO_DES if (doit[D_CBC_DES]) { for (j=0; j<SIZE_NUM; j++) { print_message(names[D_CBC_DES],c[D_CBC_DES][j],lengths[j]); Time_F(START,usertime); for (count=0,run=1; COND(c[D_CBC_DES][j]); count++) des_ncbc_encrypt(buf,buf,lengths[j],sch, &iv,DES_ENCRYPT); d=Time_F(STOP,usertime); BIO_printf(bio_err,"%ld %s's in %.2fs\n", count,names[D_CBC_DES],d); results[D_CBC_DES][j]=((double)count)/d*lengths[j]; } } if (doit[D_EDE3_DES]) { for (j=0; j<SIZE_NUM; j++) { print_message(names[D_EDE3_DES],c[D_EDE3_DES][j],lengths[j]); Time_F(START,usertime); for (count=0,run=1; COND(c[D_EDE3_DES][j]); count++) des_ede3_cbc_encrypt(buf,buf,lengths[j], sch,sch2,sch3, &iv,DES_ENCRYPT); d=Time_F(STOP,usertime); BIO_printf(bio_err,"%ld %s's in %.2fs\n", count,names[D_EDE3_DES],d); results[D_EDE3_DES][j]=((double)count)/d*lengths[j]; } } #endif #ifndef OPENSSL_NO_IDEA if (doit[D_CBC_IDEA]) { for (j=0; j<SIZE_NUM; j++) { print_message(names[D_CBC_IDEA],c[D_CBC_IDEA][j],lengths[j]); Time_F(START,usertime); for (count=0,run=1; COND(c[D_CBC_IDEA][j]); count++) idea_cbc_encrypt(buf,buf, (unsigned long)lengths[j],&idea_ks, iv,IDEA_ENCRYPT); d=Time_F(STOP,usertime); BIO_printf(bio_err,"%ld %s's in %.2fs\n", count,names[D_CBC_IDEA],d); results[D_CBC_IDEA][j]=((double)count)/d*lengths[j]; } } #endif #ifndef OPENSSL_NO_RC2 if (doit[D_CBC_RC2]) { for (j=0; j<SIZE_NUM; j++) { print_message(names[D_CBC_RC2],c[D_CBC_RC2][j],lengths[j]); Time_F(START,usertime); for (count=0,run=1; COND(c[D_CBC_RC2][j]); count++) RC2_cbc_encrypt(buf,buf, (unsigned long)lengths[j],&rc2_ks, iv,RC2_ENCRYPT); d=Time_F(STOP,usertime); BIO_printf(bio_err,"%ld %s's in %.2fs\n", count,names[D_CBC_RC2],d); results[D_CBC_RC2][j]=((double)count)/d*lengths[j]; } } #endif #ifndef OPENSSL_NO_RC5 if (doit[D_CBC_RC5]) { for (j=0; j<SIZE_NUM; j++) { print_message(names[D_CBC_RC5],c[D_CBC_RC5][j],lengths[j]); Time_F(START,usertime); for (count=0,run=1; COND(c[D_CBC_RC5][j]); count++) RC5_32_cbc_encrypt(buf,buf, (unsigned long)lengths[j],&rc5_ks, iv,RC5_ENCRYPT); d=Time_F(STOP,usertime); BIO_printf(bio_err,"%ld %s's in %.2fs\n", count,names[D_CBC_RC5],d); results[D_CBC_RC5][j]=((double)count)/d*lengths[j]; } } #endif #ifndef OPENSSL_NO_BF if (doit[D_CBC_BF]) { for (j=0; j<SIZE_NUM; j++) { print_message(names[D_CBC_BF],c[D_CBC_BF][j],lengths[j]); Time_F(START,usertime); for (count=0,run=1; COND(c[D_CBC_BF][j]); count++) BF_cbc_encrypt(buf,buf, (unsigned long)lengths[j],&bf_ks, iv,BF_ENCRYPT); d=Time_F(STOP,usertime); BIO_printf(bio_err,"%ld %s's in %.2fs\n", count,names[D_CBC_BF],d); results[D_CBC_BF][j]=((double)count)/d*lengths[j]; } } #endif #ifndef OPENSSL_NO_CAST if (doit[D_CBC_CAST]) { for (j=0; j<SIZE_NUM; j++) { print_message(names[D_CBC_CAST],c[D_CBC_CAST][j],lengths[j]); Time_F(START,usertime); for (count=0,run=1; COND(c[D_CBC_CAST][j]); count++) CAST_cbc_encrypt(buf,buf, (unsigned long)lengths[j],&cast_ks, iv,CAST_ENCRYPT); d=Time_F(STOP,usertime); BIO_printf(bio_err,"%ld %s's in %.2fs\n", count,names[D_CBC_CAST],d); results[D_CBC_CAST][j]=((double)count)/d*lengths[j]; } } #endif if (doit[D_EVP]) { for (j=0; j<SIZE_NUM; j++) { EVP_CIPHER_CTX ctx; int outl; names[D_EVP]=OBJ_nid2ln(evp->nid); print_message(names[D_EVP],save_count, lengths[j]); EVP_EncryptInit(&ctx,evp,key16,iv); Time_F(START,usertime); for (count=0,run=1; COND(save_count*4*lengths[0]/lengths[j]); count++) EVP_EncryptUpdate(&ctx,buf,&outl,buf,lengths[j]); EVP_EncryptFinal(&ctx,buf,&outl); d=Time_F(STOP,usertime); BIO_printf(bio_err,"%ld %s's in %.2fs\n", count,names[D_EVP],d); results[D_EVP][j]=((double)count)/d*lengths[j]; } } RAND_pseudo_bytes(buf,36); #ifndef OPENSSL_NO_RSA for (j=0; j<RSA_NUM; j++) { int ret; if (!rsa_doit[j]) continue; ret=RSA_sign(NID_md5_sha1, buf,36, buf2, &rsa_num, rsa_key[j]); if (ret == 0) { BIO_printf(bio_err,"RSA sign failure. No RSA sign will be done.\n"); ERR_print_errors(bio_err); rsa_count=1; } else { pkey_print_message("private","rsa", rsa_c[j][0],rsa_bits[j], RSA_SECONDS); Time_F(START,usertime); for (count=0,run=1; COND(rsa_c[j][0]); count++) { ret=RSA_sign(NID_md5_sha1, buf,36, buf2, &rsa_num, rsa_key[j]); if (ret == 0) { BIO_printf(bio_err, "RSA sign failure\n"); ERR_print_errors(bio_err); count=1; break; } } d=Time_F(STOP,usertime); BIO_printf(bio_err, "%ld %d bit private RSA's in %.2fs\n", count,rsa_bits[j],d); rsa_results[j][0]=d/(double)count; rsa_count=count; } #if 1 ret=RSA_verify(NID_md5_sha1, buf,36, buf2, rsa_num, rsa_key[j]); if (ret <= 0) { BIO_printf(bio_err,"RSA verify failure. No RSA verify will be done.\n"); ERR_print_errors(bio_err); dsa_doit[j] = 0; } else { pkey_print_message("public","rsa", rsa_c[j][1],rsa_bits[j], RSA_SECONDS); Time_F(START,usertime); for (count=0,run=1; COND(rsa_c[j][1]); count++) { ret=RSA_verify(NID_md5_sha1, buf,36, buf2, rsa_num, rsa_key[j]); if (ret == 0) { BIO_printf(bio_err, "RSA verify failure\n"); ERR_print_errors(bio_err); count=1; break; } } d=Time_F(STOP,usertime); BIO_printf(bio_err, "%ld %d bit public RSA's in %.2fs\n", count,rsa_bits[j],d); rsa_results[j][1]=d/(double)count; } #endif if (rsa_count <= 1) { for (j++; j<RSA_NUM; j++) rsa_doit[j]=0; } } #endif RAND_pseudo_bytes(buf,20); #ifndef OPENSSL_NO_DSA if (RAND_status() != 1) { RAND_seed(rnd_seed, sizeof rnd_seed); rnd_fake = 1; } for (j=0; j<DSA_NUM; j++) { unsigned int kk; int ret; if (!dsa_doit[j]) continue; DSA_generate_key(dsa_key[j]); ret=DSA_sign(EVP_PKEY_DSA,buf,20,buf2, &kk,dsa_key[j]); if (ret == 0) { BIO_printf(bio_err,"DSA sign failure. No DSA sign will be done.\n"); ERR_print_errors(bio_err); rsa_count=1; } else { pkey_print_message("sign","dsa", dsa_c[j][0],dsa_bits[j], DSA_SECONDS); Time_F(START,usertime); for (count=0,run=1; COND(dsa_c[j][0]); count++) { ret=DSA_sign(EVP_PKEY_DSA,buf,20,buf2, &kk,dsa_key[j]); if (ret == 0) { BIO_printf(bio_err, "DSA sign failure\n"); ERR_print_errors(bio_err); count=1; break; } } d=Time_F(STOP,usertime); BIO_printf(bio_err,"%ld %d bit DSA signs in %.2fs\n", count,dsa_bits[j],d); dsa_results[j][0]=d/(double)count; rsa_count=count; } ret=DSA_verify(EVP_PKEY_DSA,buf,20,buf2, kk,dsa_key[j]); if (ret <= 0) { BIO_printf(bio_err,"DSA verify failure. No DSA verify will be done.\n"); ERR_print_errors(bio_err); dsa_doit[j] = 0; } else { pkey_print_message("verify","dsa", dsa_c[j][1],dsa_bits[j], DSA_SECONDS); Time_F(START,usertime); for (count=0,run=1; COND(dsa_c[j][1]); count++) { ret=DSA_verify(EVP_PKEY_DSA,buf,20,buf2, kk,dsa_key[j]); if (ret <= 0) { BIO_printf(bio_err, "DSA verify failure\n"); ERR_print_errors(bio_err); count=1; break; } } d=Time_F(STOP,usertime); BIO_printf(bio_err,"%ld %d bit DSA verify in %.2fs\n", count,dsa_bits[j],d); dsa_results[j][1]=d/(double)count; } if (rsa_count <= 1) { for (j++; j<DSA_NUM; j++) dsa_doit[j]=0; } } if (rnd_fake) RAND_cleanup(); #endif fprintf(stdout,"%s\n",SSLeay_version(SSLEAY_VERSION)); fprintf(stdout,"%s\n",SSLeay_version(SSLEAY_BUILT_ON)); printf("options:"); printf("%s ",BN_options()); #ifndef OPENSSL_NO_MD2 printf("%s ",MD2_options()); #endif #ifndef OPENSSL_NO_RC4 printf("%s ",RC4_options()); #endif #ifndef OPENSSL_NO_DES printf("%s ",des_options()); #endif #ifndef OPENSSL_NO_IDEA printf("%s ",idea_options()); #endif #ifndef OPENSSL_NO_BF printf("%s ",BF_options()); #endif fprintf(stdout,"\n%s\n",SSLeay_version(SSLEAY_CFLAGS)); if (pr_header) { fprintf(stdout,"The 'numbers' are in 1000s of bytes per second processed.\n"); fprintf(stdout,"type "); for (j=0; j<SIZE_NUM; j++) fprintf(stdout,"%7d bytes",lengths[j]); fprintf(stdout,"\n"); } for (k=0; k<ALGOR_NUM; k++) { if (!doit[k]) continue; fprintf(stdout,"%-13s",names[k]); for (j=0; j<SIZE_NUM; j++) { if (results[k][j] > 10000) fprintf(stdout," %11.2fk",results[k][j]/1e3); else fprintf(stdout," %11.2f ",results[k][j]); } fprintf(stdout,"\n"); } #ifndef OPENSSL_NO_RSA j=1; for (k=0; k<RSA_NUM; k++) { if (!rsa_doit[k]) continue; if (j) { printf("%18ssign verify sign/s verify/s\n"," "); j=0; } fprintf(stdout,"rsa %4u bits %8.4fs %8.4fs %8.1f %8.1f", rsa_bits[k],rsa_results[k][0],rsa_results[k][1], 1.0/rsa_results[k][0],1.0/rsa_results[k][1]); fprintf(stdout,"\n"); } #endif #ifndef OPENSSL_NO_DSA j=1; for (k=0; k<DSA_NUM; k++) { if (!dsa_doit[k]) continue; if (j) { printf("%18ssign verify sign/s verify/s\n"," "); j=0; } fprintf(stdout,"dsa %4u bits %8.4fs %8.4fs %8.1f %8.1f", dsa_bits[k],dsa_results[k][0],dsa_results[k][1], 1.0/dsa_results[k][0],1.0/dsa_results[k][1]); fprintf(stdout,"\n"); } #endif mret=0; end: ERR_print_errors(bio_err); if (buf != NULL) OPENSSL_free(buf); if (buf2 != NULL) OPENSSL_free(buf2); #ifndef OPENSSL_NO_RSA for (i=0; i<RSA_NUM; i++) if (rsa_key[i] != NULL) RSA_free(rsa_key[i]); #endif #ifndef OPENSSL_NO_DSA for (i=0; i<DSA_NUM; i++) if (dsa_key[i] != NULL) DSA_free(dsa_key[i]); #endif EXIT(mret); }
['int MAIN(int argc, char **argv)\n\t{\n\tENGINE *e;\n\tunsigned char *buf=NULL,*buf2=NULL;\n\tint mret=1;\n#define ALGOR_NUM\t16\n#define SIZE_NUM\t5\n#define RSA_NUM\t\t4\n#define DSA_NUM\t\t3\n\tlong count,rsa_count,save_count=0;\n\tint i,j,k;\n#ifndef OPENSSL_NO_RSA\n\tunsigned rsa_num;\n#endif\n#ifndef OPENSSL_NO_MD2\n\tunsigned char md2[MD2_DIGEST_LENGTH];\n#endif\n#ifndef OPENSSL_NO_MDC2\n\tunsigned char mdc2[MDC2_DIGEST_LENGTH];\n#endif\n#ifndef OPENSSL_NO_MD4\n\tunsigned char md4[MD4_DIGEST_LENGTH];\n#endif\n#ifndef OPENSSL_NO_MD5\n\tunsigned char md5[MD5_DIGEST_LENGTH];\n\tunsigned char hmac[MD5_DIGEST_LENGTH];\n#endif\n#ifndef OPENSSL_NO_SHA\n\tunsigned char sha[SHA_DIGEST_LENGTH];\n#endif\n#ifndef OPENSSL_NO_RIPEMD\n\tunsigned char rmd160[RIPEMD160_DIGEST_LENGTH];\n#endif\n#ifndef OPENSSL_NO_RC4\n\tRC4_KEY rc4_ks;\n#endif\n#ifndef OPENSSL_NO_RC5\n\tRC5_32_KEY rc5_ks;\n#endif\n#ifndef OPENSSL_NO_RC2\n\tRC2_KEY rc2_ks;\n#endif\n#ifndef OPENSSL_NO_IDEA\n\tIDEA_KEY_SCHEDULE idea_ks;\n#endif\n#ifndef OPENSSL_NO_BF\n\tBF_KEY bf_ks;\n#endif\n#ifndef OPENSSL_NO_CAST\n\tCAST_KEY cast_ks;\n#endif\n\tstatic unsigned char key16[16]=\n\t\t{0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,\n\t\t 0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12};\n\tunsigned char iv[8];\n#ifndef OPENSSL_NO_DES\n\tdes_cblock *buf_as_des_cblock = NULL;\n\tstatic des_cblock key ={0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0};\n\tstatic des_cblock key2={0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12};\n\tstatic des_cblock key3={0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12,0x34};\n\tdes_key_schedule sch,sch2,sch3;\n#endif\n#define\tD_MD2\t\t0\n#define\tD_MDC2\t\t1\n#define\tD_MD4\t\t2\n#define\tD_MD5\t\t3\n#define\tD_HMAC\t\t4\n#define\tD_SHA1\t\t5\n#define D_RMD160\t6\n#define\tD_RC4\t\t7\n#define\tD_CBC_DES\t8\n#define\tD_EDE3_DES\t9\n#define\tD_CBC_IDEA\t10\n#define\tD_CBC_RC2\t11\n#define\tD_CBC_RC5\t12\n#define\tD_CBC_BF\t13\n#define\tD_CBC_CAST\t14\n#define D_EVP\t\t15\n\tdouble d,results[ALGOR_NUM][SIZE_NUM];\n\tstatic int lengths[SIZE_NUM]={8,64,256,1024,8*1024};\n\tlong c[ALGOR_NUM][SIZE_NUM];\n\tstatic const char *names[ALGOR_NUM]={\n\t\t"md2","mdc2","md4","md5","hmac(md5)","sha1","rmd160","rc4",\n\t\t"des cbc","des ede3","idea cbc",\n\t\t"rc2 cbc","rc5-32/12 cbc","blowfish cbc","cast cbc"};\n#define\tR_DSA_512\t0\n#define\tR_DSA_1024\t1\n#define\tR_DSA_2048\t2\n#define\tR_RSA_512\t0\n#define\tR_RSA_1024\t1\n#define\tR_RSA_2048\t2\n#define\tR_RSA_4096\t3\n#ifndef OPENSSL_NO_RSA\n\tRSA *rsa_key[RSA_NUM];\n\tlong rsa_c[RSA_NUM][2];\n\tdouble rsa_results[RSA_NUM][2];\n\tstatic unsigned int rsa_bits[RSA_NUM]={512,1024,2048,4096};\n\tstatic unsigned char *rsa_data[RSA_NUM]=\n\t\t{test512,test1024,test2048,test4096};\n\tstatic int rsa_data_length[RSA_NUM]={\n\t\tsizeof(test512),sizeof(test1024),\n\t\tsizeof(test2048),sizeof(test4096)};\n#endif\n#ifndef OPENSSL_NO_DSA\n\tDSA *dsa_key[DSA_NUM];\n\tlong dsa_c[DSA_NUM][2];\n\tdouble dsa_results[DSA_NUM][2];\n\tstatic unsigned int dsa_bits[DSA_NUM]={512,1024,2048};\n#endif\n\tint rsa_doit[RSA_NUM];\n\tint dsa_doit[DSA_NUM];\n\tint doit[ALGOR_NUM];\n\tint pr_header=0;\n\tint usertime=1;\n\tconst EVP_CIPHER *evp=NULL;\n#ifndef TIMES\n\tusertime=-1;\n#endif\n\tapps_startup();\n\tmemset(results, 0, sizeof(results));\n#ifndef OPENSSL_NO_DSA\n\tmemset(dsa_key,0,sizeof(dsa_key));\n#endif\n\tif (bio_err == NULL)\n\t\tif ((bio_err=BIO_new(BIO_s_file())) != NULL)\n\t\t\tBIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);\n#ifndef OPENSSL_NO_RSA\n\tmemset(rsa_key,0,sizeof(rsa_key));\n\tfor (i=0; i<RSA_NUM; i++)\n\t\trsa_key[i]=NULL;\n#endif\n\tif ((buf=(unsigned char *)OPENSSL_malloc((int)BUFSIZE)) == NULL)\n\t\t{\n\t\tBIO_printf(bio_err,"out of memory\\n");\n\t\tgoto end;\n\t\t}\n#ifndef OPENSSL_NO_DES\n\tbuf_as_des_cblock = (des_cblock *)buf;\n#endif\n\tif ((buf2=(unsigned char *)OPENSSL_malloc((int)BUFSIZE)) == NULL)\n\t\t{\n\t\tBIO_printf(bio_err,"out of memory\\n");\n\t\tgoto end;\n\t\t}\n\tmemset(c,0,sizeof(c));\n\tmemset(iv,0,sizeof(iv));\n\tfor (i=0; i<ALGOR_NUM; i++)\n\t\tdoit[i]=0;\n\tfor (i=0; i<RSA_NUM; i++)\n\t\trsa_doit[i]=0;\n\tfor (i=0; i<DSA_NUM; i++)\n\t\tdsa_doit[i]=0;\n\tj=0;\n\targc--;\n\targv++;\n\twhile (argc)\n\t\t{\n\t\tif\t((argc > 0) && (strcmp(*argv,"-elapsed") == 0))\n\t\t\tusertime = 0;\n\t\telse if\t((argc > 0) && (strcmp(*argv,"-evp") == 0))\n\t\t\t{\n\t\t\targc--;\n\t\t\targv++;\n\t\t\tif(argc == 0)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err,"no EVP given\\n");\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\tevp=EVP_get_cipherbyname(*argv);\n\t\t\tif(!evp)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err,"%s is an unknown cipher\\n",*argv);\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\tdoit[D_EVP]=1;\n\t\t\t}\n\t\telse\n\t\tif\t((argc > 0) && (strcmp(*argv,"-engine") == 0))\n\t\t\t{\n\t\t\targc--;\n\t\t\targv++;\n\t\t\tif(argc == 0)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err,"no engine given\\n");\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\tif((e = ENGINE_by_id(*argv)) == NULL)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err,"invalid engine \\"%s\\"\\n",\n\t\t\t\t\t*argv);\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\tif(!ENGINE_set_default(e, ENGINE_METHOD_ALL))\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err,"can\'t use that engine\\n");\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\tBIO_printf(bio_err,"engine \\"%s\\" set.\\n", *argv);\n\t\t\tENGINE_free(e);\n\t\t\tj--;\n\t\t\t}\n\t\telse\n#ifndef OPENSSL_NO_MD2\n\t\tif\t(strcmp(*argv,"md2") == 0) doit[D_MD2]=1;\n\t\telse\n#endif\n#ifndef OPENSSL_NO_MDC2\n\t\t\tif (strcmp(*argv,"mdc2") == 0) doit[D_MDC2]=1;\n\t\telse\n#endif\n#ifndef OPENSSL_NO_MD4\n\t\t\tif (strcmp(*argv,"md4") == 0) doit[D_MD4]=1;\n\t\telse\n#endif\n#ifndef OPENSSL_NO_MD5\n\t\t\tif (strcmp(*argv,"md5") == 0) doit[D_MD5]=1;\n\t\telse\n#endif\n#ifndef OPENSSL_NO_MD5\n\t\t\tif (strcmp(*argv,"hmac") == 0) doit[D_HMAC]=1;\n\t\telse\n#endif\n#ifndef OPENSSL_NO_SHA\n\t\t\tif (strcmp(*argv,"sha1") == 0) doit[D_SHA1]=1;\n\t\telse\n\t\t\tif (strcmp(*argv,"sha") == 0) doit[D_SHA1]=1;\n\t\telse\n#endif\n#ifndef OPENSSL_NO_RIPEMD\n\t\t\tif (strcmp(*argv,"ripemd") == 0) doit[D_RMD160]=1;\n\t\telse\n\t\t\tif (strcmp(*argv,"rmd160") == 0) doit[D_RMD160]=1;\n\t\telse\n\t\t\tif (strcmp(*argv,"ripemd160") == 0) doit[D_RMD160]=1;\n\t\telse\n#endif\n#ifndef OPENSSL_NO_RC4\n\t\t\tif (strcmp(*argv,"rc4") == 0) doit[D_RC4]=1;\n\t\telse\n#endif\n#ifndef OPENSSL_NO_DES\n\t\t\tif (strcmp(*argv,"des-cbc") == 0) doit[D_CBC_DES]=1;\n\t\telse\tif (strcmp(*argv,"des-ede3") == 0) doit[D_EDE3_DES]=1;\n\t\telse\n#endif\n#ifndef OPENSSL_NO_RSA\n#if 0\n\t\t\tif (strcmp(*argv,"rsaref") == 0)\n\t\t\t{\n\t\t\tRSA_set_default_openssl_method(RSA_PKCS1_RSAref());\n\t\t\tj--;\n\t\t\t}\n\t\telse\n#endif\n#ifndef RSA_NULL\n\t\t\tif (strcmp(*argv,"openssl") == 0)\n\t\t\t{\n\t\t\tRSA_set_default_openssl_method(RSA_PKCS1_SSLeay());\n\t\t\tj--;\n\t\t\t}\n\t\telse\n#endif\n#endif\n\t\t if (strcmp(*argv,"dsa512") == 0) dsa_doit[R_DSA_512]=2;\n\t\telse if (strcmp(*argv,"dsa1024") == 0) dsa_doit[R_DSA_1024]=2;\n\t\telse if (strcmp(*argv,"dsa2048") == 0) dsa_doit[R_DSA_2048]=2;\n\t\telse if (strcmp(*argv,"rsa512") == 0) rsa_doit[R_RSA_512]=2;\n\t\telse if (strcmp(*argv,"rsa1024") == 0) rsa_doit[R_RSA_1024]=2;\n\t\telse if (strcmp(*argv,"rsa2048") == 0) rsa_doit[R_RSA_2048]=2;\n\t\telse if (strcmp(*argv,"rsa4096") == 0) rsa_doit[R_RSA_4096]=2;\n\t\telse\n#ifndef OPENSSL_NO_RC2\n\t\t if (strcmp(*argv,"rc2-cbc") == 0) doit[D_CBC_RC2]=1;\n\t\telse if (strcmp(*argv,"rc2") == 0) doit[D_CBC_RC2]=1;\n\t\telse\n#endif\n#ifndef OPENSSL_NO_RC5\n\t\t if (strcmp(*argv,"rc5-cbc") == 0) doit[D_CBC_RC5]=1;\n\t\telse if (strcmp(*argv,"rc5") == 0) doit[D_CBC_RC5]=1;\n\t\telse\n#endif\n#ifndef OPENSSL_NO_IDEA\n\t\t if (strcmp(*argv,"idea-cbc") == 0) doit[D_CBC_IDEA]=1;\n\t\telse if (strcmp(*argv,"idea") == 0) doit[D_CBC_IDEA]=1;\n\t\telse\n#endif\n#ifndef OPENSSL_NO_BF\n\t\t if (strcmp(*argv,"bf-cbc") == 0) doit[D_CBC_BF]=1;\n\t\telse if (strcmp(*argv,"blowfish") == 0) doit[D_CBC_BF]=1;\n\t\telse if (strcmp(*argv,"bf") == 0) doit[D_CBC_BF]=1;\n\t\telse\n#endif\n#ifndef OPENSSL_NO_CAST\n\t\t if (strcmp(*argv,"cast-cbc") == 0) doit[D_CBC_CAST]=1;\n\t\telse if (strcmp(*argv,"cast") == 0) doit[D_CBC_CAST]=1;\n\t\telse if (strcmp(*argv,"cast5") == 0) doit[D_CBC_CAST]=1;\n\t\telse\n#endif\n#ifndef OPENSSL_NO_DES\n\t\t\tif (strcmp(*argv,"des") == 0)\n\t\t\t{\n\t\t\tdoit[D_CBC_DES]=1;\n\t\t\tdoit[D_EDE3_DES]=1;\n\t\t\t}\n\t\telse\n#endif\n#ifndef OPENSSL_NO_RSA\n\t\t\tif (strcmp(*argv,"rsa") == 0)\n\t\t\t{\n\t\t\trsa_doit[R_RSA_512]=1;\n\t\t\trsa_doit[R_RSA_1024]=1;\n\t\t\trsa_doit[R_RSA_2048]=1;\n\t\t\trsa_doit[R_RSA_4096]=1;\n\t\t\t}\n\t\telse\n#endif\n#ifndef OPENSSL_NO_DSA\n\t\t\tif (strcmp(*argv,"dsa") == 0)\n\t\t\t{\n\t\t\tdsa_doit[R_DSA_512]=1;\n\t\t\tdsa_doit[R_DSA_1024]=1;\n\t\t\t}\n\t\telse\n#endif\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"Error: bad option or value\\n");\n\t\t\tBIO_printf(bio_err,"\\n");\n\t\t\tBIO_printf(bio_err,"Available values:\\n");\n#ifndef OPENSSL_NO_MD2\n\t\t\tBIO_printf(bio_err,"md2 ");\n#endif\n#ifndef OPENSSL_NO_MDC2\n\t\t\tBIO_printf(bio_err,"mdc2 ");\n#endif\n#ifndef OPENSSL_NO_MD4\n\t\t\tBIO_printf(bio_err,"md4 ");\n#endif\n#ifndef OPENSSL_NO_MD5\n\t\t\tBIO_printf(bio_err,"md5 ");\n#ifndef OPENSSL_NO_HMAC\n\t\t\tBIO_printf(bio_err,"hmac ");\n#endif\n#endif\n#ifndef OPENSSL_NO_SHA1\n\t\t\tBIO_printf(bio_err,"sha1 ");\n#endif\n#ifndef OPENSSL_NO_RIPEMD160\n\t\t\tBIO_printf(bio_err,"rmd160");\n#endif\n#if !defined(OPENSSL_NO_MD2) || !defined(OPENSSL_NO_MDC2) || \\\n !defined(OPENSSL_NO_MD4) || !defined(OPENSSL_NO_MD5) || \\\n !defined(OPENSSL_NO_SHA1) || !defined(OPENSSL_NO_RIPEMD160)\n\t\t\tBIO_printf(bio_err,"\\n");\n#endif\n#ifndef OPENSSL_NO_IDEA\n\t\t\tBIO_printf(bio_err,"idea-cbc ");\n#endif\n#ifndef OPENSSL_NO_RC2\n\t\t\tBIO_printf(bio_err,"rc2-cbc ");\n#endif\n#ifndef OPENSSL_NO_RC5\n\t\t\tBIO_printf(bio_err,"rc5-cbc ");\n#endif\n#ifndef OPENSSL_NO_BF\n\t\t\tBIO_printf(bio_err,"bf-cbc");\n#endif\n#if !defined(OPENSSL_NO_IDEA) || !defined(OPENSSL_NO_RC2) || \\\n !defined(OPENSSL_NO_BF) || !defined(OPENSSL_NO_RC5)\n\t\t\tBIO_printf(bio_err,"\\n");\n#endif\n\t\t\tBIO_printf(bio_err,"des-cbc des-ede3 ");\n#ifndef OPENSSL_NO_RC4\n\t\t\tBIO_printf(bio_err,"rc4");\n#endif\n\t\t\tBIO_printf(bio_err,"\\n");\n#ifndef OPENSSL_NO_RSA\n\t\t\tBIO_printf(bio_err,"rsa512 rsa1024 rsa2048 rsa4096\\n");\n#endif\n#ifndef OPENSSL_NO_DSA\n\t\t\tBIO_printf(bio_err,"dsa512 dsa1024 dsa2048\\n");\n#endif\n#ifndef OPENSSL_NO_IDEA\n\t\t\tBIO_printf(bio_err,"idea ");\n#endif\n#ifndef OPENSSL_NO_RC2\n\t\t\tBIO_printf(bio_err,"rc2 ");\n#endif\n#ifndef OPENSSL_NO_DES\n\t\t\tBIO_printf(bio_err,"des ");\n#endif\n#ifndef OPENSSL_NO_RSA\n\t\t\tBIO_printf(bio_err,"rsa ");\n#endif\n#ifndef OPENSSL_NO_BF\n\t\t\tBIO_printf(bio_err,"blowfish");\n#endif\n#if !defined(OPENSSL_NO_IDEA) || !defined(OPENSSL_NO_RC2) || \\\n !defined(OPENSSL_NO_DES) || !defined(OPENSSL_NO_RSA) || \\\n !defined(OPENSSL_NO_BF)\n\t\t\tBIO_printf(bio_err,"\\n");\n#endif\n\t\t\tBIO_printf(bio_err,"\\n");\n\t\t\tBIO_printf(bio_err,"Available options:\\n");\n#ifdef TIMES\n\t\t\tBIO_printf(bio_err,"-elapsed measure time in real time instead of CPU user time.\\n");\n#endif\n\t\t\tBIO_printf(bio_err,"-engine e use engine e, possibly a hardware device.\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\targc--;\n\t\targv++;\n\t\tj++;\n\t\t}\n\tif (j == 0)\n\t\t{\n\t\tfor (i=0; i<ALGOR_NUM; i++)\n\t\t\t{\n\t\t\tif (i != D_EVP)\n\t\t\t\tdoit[i]=1;\n\t\t\t}\n\t\tfor (i=0; i<RSA_NUM; i++)\n\t\t\trsa_doit[i]=1;\n\t\tfor (i=0; i<DSA_NUM; i++)\n\t\t\tdsa_doit[i]=1;\n\t\t}\n\tfor (i=0; i<ALGOR_NUM; i++)\n\t\tif (doit[i]) pr_header++;\n\tif (usertime == 0)\n\t\tBIO_printf(bio_err,"You have chosen to measure elapsed time instead of user CPU time.\\n");\n\tif (usertime <= 0)\n\t\t{\n\t\tBIO_printf(bio_err,"To get the most accurate results, try to run this\\n");\n\t\tBIO_printf(bio_err,"program when this computer is idle.\\n");\n\t\t}\n#ifndef OPENSSL_NO_RSA\n\tfor (i=0; i<RSA_NUM; i++)\n\t\t{\n\t\tconst unsigned char *p;\n\t\tp=rsa_data[i];\n\t\trsa_key[i]=d2i_RSAPrivateKey(NULL,&p,rsa_data_length[i]);\n\t\tif (rsa_key[i] == NULL)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"internal error loading RSA key number %d\\n",i);\n\t\t\tgoto end;\n\t\t\t}\n#if 0\n\t\telse\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"Loaded RSA key, %d bit modulus and e= 0x",BN_num_bits(rsa_key[i]->n));\n\t\t\tBN_print(bio_err,rsa_key[i]->e);\n\t\t\tBIO_printf(bio_err,"\\n");\n\t\t\t}\n#endif\n\t\t}\n#endif\n#ifndef OPENSSL_NO_DSA\n\tdsa_key[0]=get_dsa512();\n\tdsa_key[1]=get_dsa1024();\n\tdsa_key[2]=get_dsa2048();\n#endif\n#ifndef OPENSSL_NO_DES\n\tdes_set_key_unchecked(&key,sch);\n\tdes_set_key_unchecked(&key2,sch2);\n\tdes_set_key_unchecked(&key3,sch3);\n#endif\n#ifndef OPENSSL_NO_IDEA\n\tidea_set_encrypt_key(key16,&idea_ks);\n#endif\n#ifndef OPENSSL_NO_RC4\n\tRC4_set_key(&rc4_ks,16,key16);\n#endif\n#ifndef OPENSSL_NO_RC2\n\tRC2_set_key(&rc2_ks,16,key16,128);\n#endif\n#ifndef OPENSSL_NO_RC5\n\tRC5_32_set_key(&rc5_ks,16,key16,12);\n#endif\n#ifndef OPENSSL_NO_BF\n\tBF_set_key(&bf_ks,16,key16);\n#endif\n#ifndef OPENSSL_NO_CAST\n\tCAST_set_key(&cast_ks,16,key16);\n#endif\n#ifndef OPENSSL_NO_RSA\n\tmemset(rsa_c,0,sizeof(rsa_c));\n#endif\n#ifndef SIGALRM\n#ifndef OPENSSL_NO_DES\n\tBIO_printf(bio_err,"First we calculate the approximate speed ...\\n");\n\tcount=10;\n\tdo\t{\n\t\tlong i;\n\t\tcount*=2;\n\t\tTime_F(START,usertime);\n\t\tfor (i=count; i; i--)\n\t\t\tdes_ecb_encrypt(buf_as_des_cblock,buf_as_des_cblock,\n\t\t\t\t&(sch[0]),DES_ENCRYPT);\n\t\td=Time_F(STOP,usertime);\n\t\t} while (d <3);\n\tsave_count=count;\n\tc[D_MD2][0]=count/10;\n\tc[D_MDC2][0]=count/10;\n\tc[D_MD4][0]=count;\n\tc[D_MD5][0]=count;\n\tc[D_HMAC][0]=count;\n\tc[D_SHA1][0]=count;\n\tc[D_RMD160][0]=count;\n\tc[D_RC4][0]=count*5;\n\tc[D_CBC_DES][0]=count;\n\tc[D_EDE3_DES][0]=count/3;\n\tc[D_CBC_IDEA][0]=count;\n\tc[D_CBC_RC2][0]=count;\n\tc[D_CBC_RC5][0]=count;\n\tc[D_CBC_BF][0]=count;\n\tc[D_CBC_CAST][0]=count;\n\tfor (i=1; i<SIZE_NUM; i++)\n\t\t{\n\t\tc[D_MD2][i]=c[D_MD2][0]*4*lengths[0]/lengths[i];\n\t\tc[D_MDC2][i]=c[D_MDC2][0]*4*lengths[0]/lengths[i];\n\t\tc[D_MD4][i]=c[D_MD4][0]*4*lengths[0]/lengths[i];\n\t\tc[D_MD5][i]=c[D_MD5][0]*4*lengths[0]/lengths[i];\n\t\tc[D_HMAC][i]=c[D_HMAC][0]*4*lengths[0]/lengths[i];\n\t\tc[D_SHA1][i]=c[D_SHA1][0]*4*lengths[0]/lengths[i];\n\t\tc[D_RMD160][i]=c[D_RMD160][0]*4*lengths[0]/lengths[i];\n\t\t}\n\tfor (i=1; i<SIZE_NUM; i++)\n\t\t{\n\t\tlong l0,l1;\n\t\tl0=(long)lengths[i-1];\n\t\tl1=(long)lengths[i];\n\t\tc[D_RC4][i]=c[D_RC4][i-1]*l0/l1;\n\t\tc[D_CBC_DES][i]=c[D_CBC_DES][i-1]*l0/l1;\n\t\tc[D_EDE3_DES][i]=c[D_EDE3_DES][i-1]*l0/l1;\n\t\tc[D_CBC_IDEA][i]=c[D_CBC_IDEA][i-1]*l0/l1;\n\t\tc[D_CBC_RC2][i]=c[D_CBC_RC2][i-1]*l0/l1;\n\t\tc[D_CBC_RC5][i]=c[D_CBC_RC5][i-1]*l0/l1;\n\t\tc[D_CBC_BF][i]=c[D_CBC_BF][i-1]*l0/l1;\n\t\tc[D_CBC_CAST][i]=c[D_CBC_CAST][i-1]*l0/l1;\n\t\t}\n#ifndef OPENSSL_NO_RSA\n\trsa_c[R_RSA_512][0]=count/2000;\n\trsa_c[R_RSA_512][1]=count/400;\n\tfor (i=1; i<RSA_NUM; i++)\n\t\t{\n\t\trsa_c[i][0]=rsa_c[i-1][0]/8;\n\t\trsa_c[i][1]=rsa_c[i-1][1]/4;\n\t\tif ((rsa_doit[i] <= 1) && (rsa_c[i][0] == 0))\n\t\t\trsa_doit[i]=0;\n\t\telse\n\t\t\t{\n\t\t\tif (rsa_c[i][0] == 0)\n\t\t\t\t{\n\t\t\t\trsa_c[i][0]=1;\n\t\t\t\trsa_c[i][1]=20;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n#endif\n\tdsa_c[R_DSA_512][0]=count/1000;\n\tdsa_c[R_DSA_512][1]=count/1000/2;\n\tfor (i=1; i<DSA_NUM; i++)\n\t\t{\n\t\tdsa_c[i][0]=dsa_c[i-1][0]/4;\n\t\tdsa_c[i][1]=dsa_c[i-1][1]/4;\n\t\tif ((dsa_doit[i] <= 1) && (dsa_c[i][0] == 0))\n\t\t\tdsa_doit[i]=0;\n\t\telse\n\t\t\t{\n\t\t\tif (dsa_c[i] == 0)\n\t\t\t\t{\n\t\t\t\tdsa_c[i][0]=1;\n\t\t\t\tdsa_c[i][1]=1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n#define COND(d)\t(count < (d))\n#define COUNT(d) (d)\n#else\n# error "You cannot disable DES on systems without SIGALRM."\n#endif\n#else\n#define COND(c)\t(run)\n#define COUNT(d) (count)\n\tsignal(SIGALRM,sig_done);\n#endif\n#ifndef OPENSSL_NO_MD2\n\tif (doit[D_MD2])\n\t\t{\n\t\tfor (j=0; j<SIZE_NUM; j++)\n\t\t\t{\n\t\t\tprint_message(names[D_MD2],c[D_MD2][j],lengths[j]);\n\t\t\tTime_F(START,usertime);\n\t\t\tfor (count=0,run=1; COND(c[D_MD2][j]); count++)\n\t\t\t\tMD2(buf,(unsigned long)lengths[j],&(md2[0]));\n\t\t\td=Time_F(STOP,usertime);\n\t\t\tBIO_printf(bio_err,"%ld %s\'s in %.2fs\\n",\n\t\t\t\tcount,names[D_MD2],d);\n\t\t\tresults[D_MD2][j]=((double)count)/d*lengths[j];\n\t\t\t}\n\t\t}\n#endif\n#ifndef OPENSSL_NO_MDC2\n\tif (doit[D_MDC2])\n\t\t{\n\t\tfor (j=0; j<SIZE_NUM; j++)\n\t\t\t{\n\t\t\tprint_message(names[D_MDC2],c[D_MDC2][j],lengths[j]);\n\t\t\tTime_F(START,usertime);\n\t\t\tfor (count=0,run=1; COND(c[D_MDC2][j]); count++)\n\t\t\t\tMDC2(buf,(unsigned long)lengths[j],&(mdc2[0]));\n\t\t\td=Time_F(STOP,usertime);\n\t\t\tBIO_printf(bio_err,"%ld %s\'s in %.2fs\\n",\n\t\t\t\tcount,names[D_MDC2],d);\n\t\t\tresults[D_MDC2][j]=((double)count)/d*lengths[j];\n\t\t\t}\n\t\t}\n#endif\n#ifndef OPENSSL_NO_MD4\n\tif (doit[D_MD4])\n\t\t{\n\t\tfor (j=0; j<SIZE_NUM; j++)\n\t\t\t{\n\t\t\tprint_message(names[D_MD4],c[D_MD4][j],lengths[j]);\n\t\t\tTime_F(START,usertime);\n\t\t\tfor (count=0,run=1; COND(c[D_MD4][j]); count++)\n\t\t\t\tMD4(&(buf[0]),(unsigned long)lengths[j],&(md4[0]));\n\t\t\td=Time_F(STOP,usertime);\n\t\t\tBIO_printf(bio_err,"%ld %s\'s in %.2fs\\n",\n\t\t\t\tcount,names[D_MD4],d);\n\t\t\tresults[D_MD4][j]=((double)count)/d*lengths[j];\n\t\t\t}\n\t\t}\n#endif\n#ifndef OPENSSL_NO_MD5\n\tif (doit[D_MD5])\n\t\t{\n\t\tfor (j=0; j<SIZE_NUM; j++)\n\t\t\t{\n\t\t\tprint_message(names[D_MD5],c[D_MD5][j],lengths[j]);\n\t\t\tTime_F(START,usertime);\n\t\t\tfor (count=0,run=1; COND(c[D_MD5][j]); count++)\n\t\t\t\tMD5(&(buf[0]),(unsigned long)lengths[j],&(md5[0]));\n\t\t\td=Time_F(STOP,usertime);\n\t\t\tBIO_printf(bio_err,"%ld %s\'s in %.2fs\\n",\n\t\t\t\tcount,names[D_MD5],d);\n\t\t\tresults[D_MD5][j]=((double)count)/d*lengths[j];\n\t\t\t}\n\t\t}\n#endif\n#if !defined(OPENSSL_NO_MD5) && !defined(OPENSSL_NO_HMAC)\n\tif (doit[D_HMAC])\n\t\t{\n\t\tHMAC_CTX hctx;\n\t\tHMAC_Init(&hctx,(unsigned char *)"This is a key...",\n\t\t\t16,EVP_md5());\n\t\tfor (j=0; j<SIZE_NUM; j++)\n\t\t\t{\n\t\t\tprint_message(names[D_HMAC],c[D_HMAC][j],lengths[j]);\n\t\t\tTime_F(START,usertime);\n\t\t\tfor (count=0,run=1; COND(c[D_HMAC][j]); count++)\n\t\t\t\t{\n\t\t\t\tHMAC_Init(&hctx,NULL,0,NULL);\n HMAC_Update(&hctx,buf,lengths[j]);\n HMAC_Final(&hctx,&(hmac[0]),NULL);\n\t\t\t\t}\n\t\t\td=Time_F(STOP,usertime);\n\t\t\tBIO_printf(bio_err,"%ld %s\'s in %.2fs\\n",\n\t\t\t\tcount,names[D_HMAC],d);\n\t\t\tresults[D_HMAC][j]=((double)count)/d*lengths[j];\n\t\t\t}\n\t\t}\n#endif\n#ifndef OPENSSL_NO_SHA\n\tif (doit[D_SHA1])\n\t\t{\n\t\tfor (j=0; j<SIZE_NUM; j++)\n\t\t\t{\n\t\t\tprint_message(names[D_SHA1],c[D_SHA1][j],lengths[j]);\n\t\t\tTime_F(START,usertime);\n\t\t\tfor (count=0,run=1; COND(c[D_SHA1][j]); count++)\n\t\t\t\tSHA1(buf,(unsigned long)lengths[j],&(sha[0]));\n\t\t\td=Time_F(STOP,usertime);\n\t\t\tBIO_printf(bio_err,"%ld %s\'s in %.2fs\\n",\n\t\t\t\tcount,names[D_SHA1],d);\n\t\t\tresults[D_SHA1][j]=((double)count)/d*lengths[j];\n\t\t\t}\n\t\t}\n#endif\n#ifndef OPENSSL_NO_RIPEMD\n\tif (doit[D_RMD160])\n\t\t{\n\t\tfor (j=0; j<SIZE_NUM; j++)\n\t\t\t{\n\t\t\tprint_message(names[D_RMD160],c[D_RMD160][j],lengths[j]);\n\t\t\tTime_F(START,usertime);\n\t\t\tfor (count=0,run=1; COND(c[D_RMD160][j]); count++)\n\t\t\t\tRIPEMD160(buf,(unsigned long)lengths[j],&(rmd160[0]));\n\t\t\td=Time_F(STOP,usertime);\n\t\t\tBIO_printf(bio_err,"%ld %s\'s in %.2fs\\n",\n\t\t\t\tcount,names[D_RMD160],d);\n\t\t\tresults[D_RMD160][j]=((double)count)/d*lengths[j];\n\t\t\t}\n\t\t}\n#endif\n#ifndef OPENSSL_NO_RC4\n\tif (doit[D_RC4])\n\t\t{\n\t\tfor (j=0; j<SIZE_NUM; j++)\n\t\t\t{\n\t\t\tprint_message(names[D_RC4],c[D_RC4][j],lengths[j]);\n\t\t\tTime_F(START,usertime);\n\t\t\tfor (count=0,run=1; COND(c[D_RC4][j]); count++)\n\t\t\t\tRC4(&rc4_ks,(unsigned int)lengths[j],\n\t\t\t\t\tbuf,buf);\n\t\t\td=Time_F(STOP,usertime);\n\t\t\tBIO_printf(bio_err,"%ld %s\'s in %.2fs\\n",\n\t\t\t\tcount,names[D_RC4],d);\n\t\t\tresults[D_RC4][j]=((double)count)/d*lengths[j];\n\t\t\t}\n\t\t}\n#endif\n#ifndef OPENSSL_NO_DES\n\tif (doit[D_CBC_DES])\n\t\t{\n\t\tfor (j=0; j<SIZE_NUM; j++)\n\t\t\t{\n\t\t\tprint_message(names[D_CBC_DES],c[D_CBC_DES][j],lengths[j]);\n\t\t\tTime_F(START,usertime);\n\t\t\tfor (count=0,run=1; COND(c[D_CBC_DES][j]); count++)\n\t\t\t\tdes_ncbc_encrypt(buf,buf,lengths[j],sch,\n\t\t\t\t\t\t &iv,DES_ENCRYPT);\n\t\t\td=Time_F(STOP,usertime);\n\t\t\tBIO_printf(bio_err,"%ld %s\'s in %.2fs\\n",\n\t\t\t\tcount,names[D_CBC_DES],d);\n\t\t\tresults[D_CBC_DES][j]=((double)count)/d*lengths[j];\n\t\t\t}\n\t\t}\n\tif (doit[D_EDE3_DES])\n\t\t{\n\t\tfor (j=0; j<SIZE_NUM; j++)\n\t\t\t{\n\t\t\tprint_message(names[D_EDE3_DES],c[D_EDE3_DES][j],lengths[j]);\n\t\t\tTime_F(START,usertime);\n\t\t\tfor (count=0,run=1; COND(c[D_EDE3_DES][j]); count++)\n\t\t\t\tdes_ede3_cbc_encrypt(buf,buf,lengths[j],\n\t\t\t\t\t\t sch,sch2,sch3,\n\t\t\t\t\t\t &iv,DES_ENCRYPT);\n\t\t\td=Time_F(STOP,usertime);\n\t\t\tBIO_printf(bio_err,"%ld %s\'s in %.2fs\\n",\n\t\t\t\tcount,names[D_EDE3_DES],d);\n\t\t\tresults[D_EDE3_DES][j]=((double)count)/d*lengths[j];\n\t\t\t}\n\t\t}\n#endif\n#ifndef OPENSSL_NO_IDEA\n\tif (doit[D_CBC_IDEA])\n\t\t{\n\t\tfor (j=0; j<SIZE_NUM; j++)\n\t\t\t{\n\t\t\tprint_message(names[D_CBC_IDEA],c[D_CBC_IDEA][j],lengths[j]);\n\t\t\tTime_F(START,usertime);\n\t\t\tfor (count=0,run=1; COND(c[D_CBC_IDEA][j]); count++)\n\t\t\t\tidea_cbc_encrypt(buf,buf,\n\t\t\t\t\t(unsigned long)lengths[j],&idea_ks,\n\t\t\t\t\tiv,IDEA_ENCRYPT);\n\t\t\td=Time_F(STOP,usertime);\n\t\t\tBIO_printf(bio_err,"%ld %s\'s in %.2fs\\n",\n\t\t\t\tcount,names[D_CBC_IDEA],d);\n\t\t\tresults[D_CBC_IDEA][j]=((double)count)/d*lengths[j];\n\t\t\t}\n\t\t}\n#endif\n#ifndef OPENSSL_NO_RC2\n\tif (doit[D_CBC_RC2])\n\t\t{\n\t\tfor (j=0; j<SIZE_NUM; j++)\n\t\t\t{\n\t\t\tprint_message(names[D_CBC_RC2],c[D_CBC_RC2][j],lengths[j]);\n\t\t\tTime_F(START,usertime);\n\t\t\tfor (count=0,run=1; COND(c[D_CBC_RC2][j]); count++)\n\t\t\t\tRC2_cbc_encrypt(buf,buf,\n\t\t\t\t\t(unsigned long)lengths[j],&rc2_ks,\n\t\t\t\t\tiv,RC2_ENCRYPT);\n\t\t\td=Time_F(STOP,usertime);\n\t\t\tBIO_printf(bio_err,"%ld %s\'s in %.2fs\\n",\n\t\t\t\tcount,names[D_CBC_RC2],d);\n\t\t\tresults[D_CBC_RC2][j]=((double)count)/d*lengths[j];\n\t\t\t}\n\t\t}\n#endif\n#ifndef OPENSSL_NO_RC5\n\tif (doit[D_CBC_RC5])\n\t\t{\n\t\tfor (j=0; j<SIZE_NUM; j++)\n\t\t\t{\n\t\t\tprint_message(names[D_CBC_RC5],c[D_CBC_RC5][j],lengths[j]);\n\t\t\tTime_F(START,usertime);\n\t\t\tfor (count=0,run=1; COND(c[D_CBC_RC5][j]); count++)\n\t\t\t\tRC5_32_cbc_encrypt(buf,buf,\n\t\t\t\t\t(unsigned long)lengths[j],&rc5_ks,\n\t\t\t\t\tiv,RC5_ENCRYPT);\n\t\t\td=Time_F(STOP,usertime);\n\t\t\tBIO_printf(bio_err,"%ld %s\'s in %.2fs\\n",\n\t\t\t\tcount,names[D_CBC_RC5],d);\n\t\t\tresults[D_CBC_RC5][j]=((double)count)/d*lengths[j];\n\t\t\t}\n\t\t}\n#endif\n#ifndef OPENSSL_NO_BF\n\tif (doit[D_CBC_BF])\n\t\t{\n\t\tfor (j=0; j<SIZE_NUM; j++)\n\t\t\t{\n\t\t\tprint_message(names[D_CBC_BF],c[D_CBC_BF][j],lengths[j]);\n\t\t\tTime_F(START,usertime);\n\t\t\tfor (count=0,run=1; COND(c[D_CBC_BF][j]); count++)\n\t\t\t\tBF_cbc_encrypt(buf,buf,\n\t\t\t\t\t(unsigned long)lengths[j],&bf_ks,\n\t\t\t\t\tiv,BF_ENCRYPT);\n\t\t\td=Time_F(STOP,usertime);\n\t\t\tBIO_printf(bio_err,"%ld %s\'s in %.2fs\\n",\n\t\t\t\tcount,names[D_CBC_BF],d);\n\t\t\tresults[D_CBC_BF][j]=((double)count)/d*lengths[j];\n\t\t\t}\n\t\t}\n#endif\n#ifndef OPENSSL_NO_CAST\n\tif (doit[D_CBC_CAST])\n\t\t{\n\t\tfor (j=0; j<SIZE_NUM; j++)\n\t\t\t{\n\t\t\tprint_message(names[D_CBC_CAST],c[D_CBC_CAST][j],lengths[j]);\n\t\t\tTime_F(START,usertime);\n\t\t\tfor (count=0,run=1; COND(c[D_CBC_CAST][j]); count++)\n\t\t\t\tCAST_cbc_encrypt(buf,buf,\n\t\t\t\t\t(unsigned long)lengths[j],&cast_ks,\n\t\t\t\t\tiv,CAST_ENCRYPT);\n\t\t\td=Time_F(STOP,usertime);\n\t\t\tBIO_printf(bio_err,"%ld %s\'s in %.2fs\\n",\n\t\t\t\tcount,names[D_CBC_CAST],d);\n\t\t\tresults[D_CBC_CAST][j]=((double)count)/d*lengths[j];\n\t\t\t}\n\t\t}\n#endif\n\tif (doit[D_EVP])\n\t\t{\n\t\tfor (j=0; j<SIZE_NUM; j++)\n\t\t\t{\n\t\t\tEVP_CIPHER_CTX ctx;\n\t\t\tint outl;\n\t\t\tnames[D_EVP]=OBJ_nid2ln(evp->nid);\n\t\t\tprint_message(names[D_EVP],save_count,\n\t\t\t\t\t\t lengths[j]);\n\t\t\tEVP_EncryptInit(&ctx,evp,key16,iv);\n\t\t\tTime_F(START,usertime);\n\t\t\tfor (count=0,run=1; COND(save_count*4*lengths[0]/lengths[j]); count++)\n\t\t\t EVP_EncryptUpdate(&ctx,buf,&outl,buf,lengths[j]);\n\t\t\tEVP_EncryptFinal(&ctx,buf,&outl);\n\t\t\td=Time_F(STOP,usertime);\n\t\t\tBIO_printf(bio_err,"%ld %s\'s in %.2fs\\n",\n\t\t\t\t\t count,names[D_EVP],d);\n\t\t\tresults[D_EVP][j]=((double)count)/d*lengths[j];\n\t\t\t}\n\t\t}\n\tRAND_pseudo_bytes(buf,36);\n#ifndef OPENSSL_NO_RSA\n\tfor (j=0; j<RSA_NUM; j++)\n\t\t{\n\t\tint ret;\n\t\tif (!rsa_doit[j]) continue;\n\t\tret=RSA_sign(NID_md5_sha1, buf,36, buf2, &rsa_num, rsa_key[j]);\n\t\tif (ret == 0)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"RSA sign failure. No RSA sign will be done.\\n");\n\t\t\tERR_print_errors(bio_err);\n\t\t\trsa_count=1;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tpkey_print_message("private","rsa",\n\t\t\t\trsa_c[j][0],rsa_bits[j],\n\t\t\t\tRSA_SECONDS);\n\t\t\tTime_F(START,usertime);\n\t\t\tfor (count=0,run=1; COND(rsa_c[j][0]); count++)\n\t\t\t\t{\n\t\t\t\tret=RSA_sign(NID_md5_sha1, buf,36, buf2,\n\t\t\t\t\t&rsa_num, rsa_key[j]);\n\t\t\t\tif (ret == 0)\n\t\t\t\t\t{\n\t\t\t\t\tBIO_printf(bio_err,\n\t\t\t\t\t\t"RSA sign failure\\n");\n\t\t\t\t\tERR_print_errors(bio_err);\n\t\t\t\t\tcount=1;\n\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\td=Time_F(STOP,usertime);\n\t\t\tBIO_printf(bio_err,\n\t\t\t\t"%ld %d bit private RSA\'s in %.2fs\\n",\n\t\t\t\tcount,rsa_bits[j],d);\n\t\t\trsa_results[j][0]=d/(double)count;\n\t\t\trsa_count=count;\n\t\t\t}\n#if 1\n\t\tret=RSA_verify(NID_md5_sha1, buf,36, buf2, rsa_num, rsa_key[j]);\n\t\tif (ret <= 0)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"RSA verify failure. No RSA verify will be done.\\n");\n\t\t\tERR_print_errors(bio_err);\n\t\t\tdsa_doit[j] = 0;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tpkey_print_message("public","rsa",\n\t\t\t\trsa_c[j][1],rsa_bits[j],\n\t\t\t\tRSA_SECONDS);\n\t\t\tTime_F(START,usertime);\n\t\t\tfor (count=0,run=1; COND(rsa_c[j][1]); count++)\n\t\t\t\t{\n\t\t\t\tret=RSA_verify(NID_md5_sha1, buf,36, buf2,\n\t\t\t\t\trsa_num, rsa_key[j]);\n\t\t\t\tif (ret == 0)\n\t\t\t\t\t{\n\t\t\t\t\tBIO_printf(bio_err,\n\t\t\t\t\t\t"RSA verify failure\\n");\n\t\t\t\t\tERR_print_errors(bio_err);\n\t\t\t\t\tcount=1;\n\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\td=Time_F(STOP,usertime);\n\t\t\tBIO_printf(bio_err,\n\t\t\t\t"%ld %d bit public RSA\'s in %.2fs\\n",\n\t\t\t\tcount,rsa_bits[j],d);\n\t\t\trsa_results[j][1]=d/(double)count;\n\t\t\t}\n#endif\n\t\tif (rsa_count <= 1)\n\t\t\t{\n\t\t\tfor (j++; j<RSA_NUM; j++)\n\t\t\t\trsa_doit[j]=0;\n\t\t\t}\n\t\t}\n#endif\n\tRAND_pseudo_bytes(buf,20);\n#ifndef OPENSSL_NO_DSA\n\tif (RAND_status() != 1)\n\t\t{\n\t\tRAND_seed(rnd_seed, sizeof rnd_seed);\n\t\trnd_fake = 1;\n\t\t}\n\tfor (j=0; j<DSA_NUM; j++)\n\t\t{\n\t\tunsigned int kk;\n\t\tint ret;\n\t\tif (!dsa_doit[j]) continue;\n\t\tDSA_generate_key(dsa_key[j]);\n\t\tret=DSA_sign(EVP_PKEY_DSA,buf,20,buf2,\n\t\t\t&kk,dsa_key[j]);\n\t\tif (ret == 0)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"DSA sign failure. No DSA sign will be done.\\n");\n\t\t\tERR_print_errors(bio_err);\n\t\t\trsa_count=1;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tpkey_print_message("sign","dsa",\n\t\t\t\tdsa_c[j][0],dsa_bits[j],\n\t\t\t\tDSA_SECONDS);\n\t\t\tTime_F(START,usertime);\n\t\t\tfor (count=0,run=1; COND(dsa_c[j][0]); count++)\n\t\t\t\t{\n\t\t\t\tret=DSA_sign(EVP_PKEY_DSA,buf,20,buf2,\n\t\t\t\t\t&kk,dsa_key[j]);\n\t\t\t\tif (ret == 0)\n\t\t\t\t\t{\n\t\t\t\t\tBIO_printf(bio_err,\n\t\t\t\t\t\t"DSA sign failure\\n");\n\t\t\t\t\tERR_print_errors(bio_err);\n\t\t\t\t\tcount=1;\n\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\td=Time_F(STOP,usertime);\n\t\t\tBIO_printf(bio_err,"%ld %d bit DSA signs in %.2fs\\n",\n\t\t\t\tcount,dsa_bits[j],d);\n\t\t\tdsa_results[j][0]=d/(double)count;\n\t\t\trsa_count=count;\n\t\t\t}\n\t\tret=DSA_verify(EVP_PKEY_DSA,buf,20,buf2,\n\t\t\tkk,dsa_key[j]);\n\t\tif (ret <= 0)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"DSA verify failure. No DSA verify will be done.\\n");\n\t\t\tERR_print_errors(bio_err);\n\t\t\tdsa_doit[j] = 0;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tpkey_print_message("verify","dsa",\n\t\t\t\tdsa_c[j][1],dsa_bits[j],\n\t\t\t\tDSA_SECONDS);\n\t\t\tTime_F(START,usertime);\n\t\t\tfor (count=0,run=1; COND(dsa_c[j][1]); count++)\n\t\t\t\t{\n\t\t\t\tret=DSA_verify(EVP_PKEY_DSA,buf,20,buf2,\n\t\t\t\t\tkk,dsa_key[j]);\n\t\t\t\tif (ret <= 0)\n\t\t\t\t\t{\n\t\t\t\t\tBIO_printf(bio_err,\n\t\t\t\t\t\t"DSA verify failure\\n");\n\t\t\t\t\tERR_print_errors(bio_err);\n\t\t\t\t\tcount=1;\n\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\td=Time_F(STOP,usertime);\n\t\t\tBIO_printf(bio_err,"%ld %d bit DSA verify in %.2fs\\n",\n\t\t\t\tcount,dsa_bits[j],d);\n\t\t\tdsa_results[j][1]=d/(double)count;\n\t\t\t}\n\t\tif (rsa_count <= 1)\n\t\t\t{\n\t\t\tfor (j++; j<DSA_NUM; j++)\n\t\t\t\tdsa_doit[j]=0;\n\t\t\t}\n\t\t}\n\tif (rnd_fake) RAND_cleanup();\n#endif\n\tfprintf(stdout,"%s\\n",SSLeay_version(SSLEAY_VERSION));\n fprintf(stdout,"%s\\n",SSLeay_version(SSLEAY_BUILT_ON));\n\tprintf("options:");\n\tprintf("%s ",BN_options());\n#ifndef OPENSSL_NO_MD2\n\tprintf("%s ",MD2_options());\n#endif\n#ifndef OPENSSL_NO_RC4\n\tprintf("%s ",RC4_options());\n#endif\n#ifndef OPENSSL_NO_DES\n\tprintf("%s ",des_options());\n#endif\n#ifndef OPENSSL_NO_IDEA\n\tprintf("%s ",idea_options());\n#endif\n#ifndef OPENSSL_NO_BF\n\tprintf("%s ",BF_options());\n#endif\n\tfprintf(stdout,"\\n%s\\n",SSLeay_version(SSLEAY_CFLAGS));\n\tif (pr_header)\n\t\t{\n\t\tfprintf(stdout,"The \'numbers\' are in 1000s of bytes per second processed.\\n");\n\t\tfprintf(stdout,"type ");\n\t\tfor (j=0; j<SIZE_NUM; j++)\n\t\t\tfprintf(stdout,"%7d bytes",lengths[j]);\n\t\tfprintf(stdout,"\\n");\n\t\t}\n\tfor (k=0; k<ALGOR_NUM; k++)\n\t\t{\n\t\tif (!doit[k]) continue;\n\t\tfprintf(stdout,"%-13s",names[k]);\n\t\tfor (j=0; j<SIZE_NUM; j++)\n\t\t\t{\n\t\t\tif (results[k][j] > 10000)\n\t\t\t\tfprintf(stdout," %11.2fk",results[k][j]/1e3);\n\t\t\telse\n\t\t\t\tfprintf(stdout," %11.2f ",results[k][j]);\n\t\t\t}\n\t\tfprintf(stdout,"\\n");\n\t\t}\n#ifndef OPENSSL_NO_RSA\n\tj=1;\n\tfor (k=0; k<RSA_NUM; k++)\n\t\t{\n\t\tif (!rsa_doit[k]) continue;\n\t\tif (j)\n\t\t\t{\n\t\t\tprintf("%18ssign verify sign/s verify/s\\n"," ");\n\t\t\tj=0;\n\t\t\t}\n\t\tfprintf(stdout,"rsa %4u bits %8.4fs %8.4fs %8.1f %8.1f",\n\t\t\trsa_bits[k],rsa_results[k][0],rsa_results[k][1],\n\t\t\t1.0/rsa_results[k][0],1.0/rsa_results[k][1]);\n\t\tfprintf(stdout,"\\n");\n\t\t}\n#endif\n#ifndef OPENSSL_NO_DSA\n\tj=1;\n\tfor (k=0; k<DSA_NUM; k++)\n\t\t{\n\t\tif (!dsa_doit[k]) continue;\n\t\tif (j)\t{\n\t\t\tprintf("%18ssign verify sign/s verify/s\\n"," ");\n\t\t\tj=0;\n\t\t\t}\n\t\tfprintf(stdout,"dsa %4u bits %8.4fs %8.4fs %8.1f %8.1f",\n\t\t\tdsa_bits[k],dsa_results[k][0],dsa_results[k][1],\n\t\t\t1.0/dsa_results[k][0],1.0/dsa_results[k][1]);\n\t\tfprintf(stdout,"\\n");\n\t\t}\n#endif\n\tmret=0;\nend:\n\tERR_print_errors(bio_err);\n\tif (buf != NULL) OPENSSL_free(buf);\n\tif (buf2 != NULL) OPENSSL_free(buf2);\n#ifndef OPENSSL_NO_RSA\n\tfor (i=0; i<RSA_NUM; i++)\n\t\tif (rsa_key[i] != NULL)\n\t\t\tRSA_free(rsa_key[i]);\n#endif\n#ifndef OPENSSL_NO_DSA\n\tfor (i=0; i<DSA_NUM; i++)\n\t\tif (dsa_key[i] != NULL)\n\t\t\tDSA_free(dsa_key[i]);\n#endif\n\tEXIT(mret);\n\t}']
36,429
0
https://github.com/libav/libav/blob/a082ac412520cd5d812bd57e5ccdad2af557125b/libavcodec/proresenc.c/#L414
static int estimate_dcs(int *error, DCTELEM *blocks, int blocks_per_slice, int scale) { int i; int codebook = 3, code, dc, prev_dc, delta, sign, new_sign; int bits; prev_dc = (blocks[0] - 0x4000) / scale; bits = estimate_vlc(FIRST_DC_CB, MAKE_CODE(prev_dc)); codebook = 3; blocks += 64; *error += FFABS(blocks[0] - 0x4000) % scale; for (i = 1; i < blocks_per_slice; i++, blocks += 64) { dc = (blocks[0] - 0x4000) / scale; *error += FFABS(blocks[0] - 0x4000) % scale; delta = dc - prev_dc; new_sign = GET_SIGN(delta); delta = (delta ^ sign) - sign; code = MAKE_CODE(delta); bits += estimate_vlc(ff_prores_dc_codebook[codebook], code); codebook = (code + (code & 1)) >> 1; codebook = FFMIN(codebook, 3); sign = new_sign; prev_dc = dc; } return bits; }
['static int find_slice_quant(AVCodecContext *avctx, const AVFrame *pic,\n int trellis_node, int x, int y, int mbs_per_slice)\n{\n ProresContext *ctx = avctx->priv_data;\n int i, q, pq, xp, yp;\n const uint16_t *src;\n int slice_width_factor = av_log2(mbs_per_slice);\n int num_cblocks[MAX_PLANES], pwidth;\n int plane_factor[MAX_PLANES], is_chroma[MAX_PLANES];\n const int min_quant = ctx->profile_info->min_quant;\n const int max_quant = ctx->profile_info->max_quant;\n int error, bits, bits_limit;\n int mbs, prev, cur, new_score;\n int slice_bits[TRELLIS_WIDTH], slice_score[TRELLIS_WIDTH];\n mbs = x + mbs_per_slice;\n for (i = 0; i < ctx->num_planes; i++) {\n is_chroma[i] = (i == 1 || i == 2);\n plane_factor[i] = slice_width_factor + 2;\n if (is_chroma[i])\n plane_factor[i] += ctx->chroma_factor - 3;\n if (!is_chroma[i] || ctx->chroma_factor == CFACTOR_Y444) {\n xp = x << 4;\n yp = y << 4;\n num_cblocks[i] = 4;\n pwidth = avctx->width;\n } else {\n xp = x << 3;\n yp = y << 4;\n num_cblocks[i] = 2;\n pwidth = avctx->width >> 1;\n }\n src = (const uint16_t*)(pic->data[i] + yp * pic->linesize[i]) + xp;\n get_slice_data(ctx, src, pic->linesize[i], xp, yp,\n pwidth, avctx->height, ctx->blocks[i],\n mbs_per_slice, num_cblocks[i]);\n }\n for (q = min_quant; q <= max_quant; q++) {\n ctx->nodes[trellis_node + q].prev_node = -1;\n ctx->nodes[trellis_node + q].quant = q;\n }\n for (q = min_quant; q <= max_quant; q++) {\n bits = 0;\n error = 0;\n for (i = 0; i < ctx->num_planes; i++) {\n bits += estimate_slice_plane(ctx, &error, i,\n src, pic->linesize[i],\n mbs_per_slice,\n num_cblocks[i], plane_factor[i],\n ctx->quants[q]);\n }\n if (bits > 65000 * 8) {\n error = SCORE_LIMIT;\n break;\n }\n slice_bits[q] = bits;\n slice_score[q] = error;\n }\n bits_limit = mbs * ctx->bits_per_mb;\n for (pq = min_quant; pq <= max_quant; pq++) {\n prev = trellis_node - TRELLIS_WIDTH + pq;\n for (q = min_quant; q <= max_quant; q++) {\n cur = trellis_node + q;\n bits = ctx->nodes[prev].bits + slice_bits[q];\n error = slice_score[q];\n if (bits > bits_limit)\n error = SCORE_LIMIT;\n if (ctx->nodes[prev].score < SCORE_LIMIT && error < SCORE_LIMIT)\n new_score = ctx->nodes[prev].score + error;\n else\n new_score = SCORE_LIMIT;\n if (ctx->nodes[cur].prev_node == -1 ||\n ctx->nodes[cur].score >= new_score) {\n ctx->nodes[cur].bits = bits;\n ctx->nodes[cur].score = new_score;\n ctx->nodes[cur].prev_node = prev;\n }\n }\n }\n error = ctx->nodes[trellis_node + min_quant].score;\n pq = trellis_node + min_quant;\n for (q = min_quant + 1; q <= max_quant; q++) {\n if (ctx->nodes[trellis_node + q].score <= error) {\n error = ctx->nodes[trellis_node + q].score;\n pq = trellis_node + q;\n }\n }\n return pq;\n}', 'static int estimate_slice_plane(ProresContext *ctx, int *error, int plane,\n const uint16_t *src, int linesize,\n int mbs_per_slice,\n int blocks_per_mb, int plane_size_factor,\n const int16_t *qmat)\n{\n int blocks_per_slice;\n int bits;\n blocks_per_slice = mbs_per_slice * blocks_per_mb;\n bits = estimate_dcs(error, ctx->blocks[plane], blocks_per_slice, qmat[0]);\n bits += estimate_acs(error, ctx->blocks[plane], blocks_per_slice,\n plane_size_factor, ctx->scantable.permutated, qmat);\n return FFALIGN(bits, 8);\n}', 'static int estimate_dcs(int *error, DCTELEM *blocks, int blocks_per_slice,\n int scale)\n{\n int i;\n int codebook = 3, code, dc, prev_dc, delta, sign, new_sign;\n int bits;\n prev_dc = (blocks[0] - 0x4000) / scale;\n bits = estimate_vlc(FIRST_DC_CB, MAKE_CODE(prev_dc));\n codebook = 3;\n blocks += 64;\n *error += FFABS(blocks[0] - 0x4000) % scale;\n for (i = 1; i < blocks_per_slice; i++, blocks += 64) {\n dc = (blocks[0] - 0x4000) / scale;\n *error += FFABS(blocks[0] - 0x4000) % scale;\n delta = dc - prev_dc;\n new_sign = GET_SIGN(delta);\n delta = (delta ^ sign) - sign;\n code = MAKE_CODE(delta);\n bits += estimate_vlc(ff_prores_dc_codebook[codebook], code);\n codebook = (code + (code & 1)) >> 1;\n codebook = FFMIN(codebook, 3);\n sign = new_sign;\n prev_dc = dc;\n }\n return bits;\n}']
36,430
0
https://github.com/libav/libav/blob/92c5052db971a2a3dfb7c3fc4317b55f401d7811/ffmpeg.c/#L3691
static void opt_streamid(const char *opt, const char *arg) { int idx; char *p; char idx_str[16]; strncpy(idx_str, arg, sizeof(idx_str)); idx_str[sizeof(idx_str)-1] = '\0'; p = strchr(idx_str, ':'); if (!p) { fprintf(stderr, "Invalid value '%s' for option '%s', required syntax is 'index:value'\n", arg, opt); ffmpeg_exit(1); } *p++ = '\0'; idx = parse_number_or_die(opt, idx_str, OPT_INT, 0, MAX_STREAMS-1); streamid_map = grow_array(streamid_map, sizeof(*streamid_map), &nb_streamid_map, idx+1); streamid_map[idx] = parse_number_or_die(opt, p, OPT_INT, 0, INT_MAX); }
['static void opt_streamid(const char *opt, const char *arg)\n{\n int idx;\n char *p;\n char idx_str[16];\n strncpy(idx_str, arg, sizeof(idx_str));\n idx_str[sizeof(idx_str)-1] = \'\\0\';\n p = strchr(idx_str, \':\');\n if (!p) {\n fprintf(stderr,\n "Invalid value \'%s\' for option \'%s\', required syntax is \'index:value\'\\n",\n arg, opt);\n ffmpeg_exit(1);\n }\n *p++ = \'\\0\';\n idx = parse_number_or_die(opt, idx_str, OPT_INT, 0, MAX_STREAMS-1);\n streamid_map = grow_array(streamid_map, sizeof(*streamid_map), &nb_streamid_map, idx+1);\n streamid_map[idx] = parse_number_or_die(opt, p, OPT_INT, 0, INT_MAX);\n}', 'static void *grow_array(void *array, int elem_size, int *size, int new_size)\n{\n if (new_size >= INT_MAX / elem_size) {\n fprintf(stderr, "Array too big.\\n");\n ffmpeg_exit(1);\n }\n if (*size < new_size) {\n uint8_t *tmp = av_realloc(array, new_size*elem_size);\n if (!tmp) {\n fprintf(stderr, "Could not alloc buffer.\\n");\n ffmpeg_exit(1);\n }\n memset(tmp + *size*elem_size, 0, (new_size-*size) * elem_size);\n *size = new_size;\n return tmp;\n }\n return array;\n}', 'void *av_realloc(void *ptr, unsigned int size)\n{\n#if CONFIG_MEMALIGN_HACK\n int diff;\n#endif\n if(size > (INT_MAX-16) )\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n if(!ptr) return av_malloc(size);\n diff= ((char*)ptr)[-1];\n return (char*)realloc((char*)ptr - diff, size + diff) + diff;\n#else\n return realloc(ptr, size);\n#endif\n}']
36,431
0
https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/apps/s_socket.c/#L484
int host_ip(char *str, unsigned char ip[4]) { unsigned int in[4]; int i; if (sscanf(str,"%d.%d.%d.%d",&(in[0]),&(in[1]),&(in[2]),&(in[3])) == 4) { for (i=0; i<4; i++) if (in[i] > 255) { BIO_printf(bio_err,"invalid IP address\n"); goto err; } ip[0]=in[0]; ip[1]=in[1]; ip[2]=in[2]; ip[3]=in[3]; } else { struct hostent *he; if (!sock_init()) return(0); he=GetHostByName(str); if (he == NULL) { BIO_printf(bio_err,"gethostbyname failure\n"); goto err; } if ((short)he->h_addrtype != AF_INET) { BIO_printf(bio_err,"gethostbyname addr is not AF_INET\n"); return(0); } ip[0]=he->h_addr_list[0][0]; ip[1]=he->h_addr_list[0][1]; ip[2]=he->h_addr_list[0][2]; ip[3]=he->h_addr_list[0][3]; } return(1); err: return(0); }
['int host_ip(char *str, unsigned char ip[4])\n\t{\n\tunsigned int in[4];\n\tint i;\n\tif (sscanf(str,"%d.%d.%d.%d",&(in[0]),&(in[1]),&(in[2]),&(in[3])) == 4)\n\t\t{\n\t\tfor (i=0; i<4; i++)\n\t\t\tif (in[i] > 255)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err,"invalid IP address\\n");\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\tip[0]=in[0];\n\t\tip[1]=in[1];\n\t\tip[2]=in[2];\n\t\tip[3]=in[3];\n\t\t}\n\telse\n\t\t{\n\t\tstruct hostent *he;\n\t\tif (!sock_init()) return(0);\n\t\the=GetHostByName(str);\n\t\tif (he == NULL)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"gethostbyname failure\\n");\n\t\t\tgoto err;\n\t\t\t}\n\t\tif ((short)he->h_addrtype != AF_INET)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"gethostbyname addr is not AF_INET\\n");\n\t\t\treturn(0);\n\t\t\t}\n\t\tip[0]=he->h_addr_list[0][0];\n\t\tip[1]=he->h_addr_list[0][1];\n\t\tip[2]=he->h_addr_list[0][2];\n\t\tip[3]=he->h_addr_list[0][3];\n\t\t}\n\treturn(1);\nerr:\n\treturn(0);\n\t}']
36,432
0
https://github.com/apache/httpd/blob/89d29d4f8c87bb44767597b667a3fbaab478934c/modules/proxy/mod_proxy_fcgi.c/#L474
static apr_status_t dispatch(proxy_conn_rec *conn, proxy_dir_conf *conf, request_rec *r, apr_pool_t *setaside_pool, apr_uint16_t request_id, const char **err) { apr_bucket_brigade *ib, *ob; int seen_end_of_headers = 0, done = 0; apr_status_t rv = APR_SUCCESS; int script_error_status = HTTP_OK; conn_rec *c = r->connection; struct iovec vec[2]; ap_fcgi_header header; unsigned char farray[AP_FCGI_HEADER_LEN]; apr_pollfd_t pfd; int header_state = HDR_STATE_READING_HEADERS; char stack_iobuf[AP_IOBUFSIZE]; apr_size_t iobuf_size = AP_IOBUFSIZE; char *iobuf = stack_iobuf; *err = NULL; if (conn->worker->s->io_buffer_size_set) { iobuf_size = conn->worker->s->io_buffer_size; iobuf = apr_palloc(r->pool, iobuf_size); } pfd.desc_type = APR_POLL_SOCKET; pfd.desc.s = conn->sock; pfd.p = r->pool; pfd.reqevents = APR_POLLIN | APR_POLLOUT; ib = apr_brigade_create(r->pool, c->bucket_alloc); ob = apr_brigade_create(r->pool, c->bucket_alloc); while (! done) { apr_interval_time_t timeout; apr_size_t len; int n; apr_socket_timeout_get(conn->sock, &timeout); rv = apr_poll(&pfd, 1, &n, timeout); if (rv != APR_SUCCESS) { if (APR_STATUS_IS_EINTR(rv)) { continue; } *err = "polling"; break; } if (pfd.rtnevents & APR_POLLOUT) { apr_size_t to_send, writebuflen; int last_stdin = 0; char *iobuf_cursor; rv = ap_get_brigade(r->input_filters, ib, AP_MODE_READBYTES, APR_BLOCK_READ, iobuf_size); if (rv != APR_SUCCESS) { *err = "reading input brigade"; break; } if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(ib))) { last_stdin = 1; } writebuflen = iobuf_size; rv = apr_brigade_flatten(ib, iobuf, &writebuflen); apr_brigade_cleanup(ib); if (rv != APR_SUCCESS) { *err = "flattening brigade"; break; } to_send = writebuflen; iobuf_cursor = iobuf; while (to_send > 0) { int nvec = 0; apr_size_t write_this_time; write_this_time = to_send < AP_FCGI_MAX_CONTENT_LEN ? to_send : AP_FCGI_MAX_CONTENT_LEN; ap_fcgi_fill_in_header(&header, AP_FCGI_STDIN, request_id, (apr_uint16_t)write_this_time, 0); ap_fcgi_header_to_array(&header, farray); vec[nvec].iov_base = (void *)farray; vec[nvec].iov_len = sizeof(farray); ++nvec; if (writebuflen) { vec[nvec].iov_base = iobuf_cursor; vec[nvec].iov_len = write_this_time; ++nvec; } rv = send_data(conn, vec, nvec, &len); if (rv != APR_SUCCESS) { *err = "sending stdin"; break; } to_send -= write_this_time; iobuf_cursor += write_this_time; } if (last_stdin) { pfd.reqevents = APR_POLLIN; if (writebuflen) { ap_fcgi_fill_in_header(&header, AP_FCGI_STDIN, request_id, 0, 0); ap_fcgi_header_to_array(&header, farray); vec[0].iov_base = (void *)farray; vec[0].iov_len = sizeof(farray); rv = send_data(conn, vec, 1, &len); if (rv != APR_SUCCESS) { *err = "sending empty stdin"; break; } } } } if (pfd.rtnevents & APR_POLLIN) { apr_size_t readbuflen; apr_uint16_t clen, rid; apr_bucket *b; unsigned char plen; unsigned char type, version; rv = get_data_full(conn, (char *) farray, AP_FCGI_HEADER_LEN); if (rv != APR_SUCCESS) { ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01067) "Failed to read FastCGI header"); break; } #ifdef FCGI_DUMP_HEADERS ap_log_rdata(APLOG_MARK, APLOG_DEBUG, r, "FastCGI header", farray, AP_FCGI_HEADER_LEN, 0); #endif ap_fcgi_header_fields_from_array(&version, &type, &rid, &clen, &plen, farray); if (version != AP_FCGI_VERSION_1) { ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01068) "Got bogus version %d", (int)version); rv = APR_EINVAL; break; } if (rid != request_id) { ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01069) "Got bogus rid %d, expected %d", rid, request_id); rv = APR_EINVAL; break; } recv_again: if (clen > iobuf_size) { readbuflen = iobuf_size; } else { readbuflen = clen; } if (readbuflen != 0) { rv = get_data(conn, iobuf, &readbuflen); if (rv != APR_SUCCESS) { *err = "reading response body"; break; } } switch (type) { case AP_FCGI_STDOUT: if (clen != 0) { b = apr_bucket_transient_create(iobuf, readbuflen, c->bucket_alloc); APR_BRIGADE_INSERT_TAIL(ob, b); if (! seen_end_of_headers) { int st = handle_headers(r, &header_state, iobuf); if (st == 1) { int status; seen_end_of_headers = 1; status = ap_scan_script_header_err_brigade_ex(r, ob, NULL, APLOG_MODULE_INDEX); if (status != OK) { apr_bucket *tmp_b; apr_brigade_cleanup(ob); tmp_b = apr_bucket_eos_create(c->bucket_alloc); APR_BRIGADE_INSERT_TAIL(ob, tmp_b); r->status = status; ap_pass_brigade(r->output_filters, ob); ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01070) "Error parsing script headers"); rv = APR_EINVAL; break; } if (conf->error_override && ap_is_HTTP_ERROR(r->status)) { script_error_status = r->status; r->status = HTTP_OK; } if (script_error_status == HTTP_OK && !APR_BRIGADE_EMPTY(ob)) { rv = ap_pass_brigade(r->output_filters, ob); if (rv != APR_SUCCESS) { *err = "passing brigade to output filters"; break; } } apr_brigade_cleanup(ob); apr_pool_clear(setaside_pool); } else { apr_bucket_setaside(b, setaside_pool); } } else { if (script_error_status == HTTP_OK) { rv = ap_pass_brigade(r->output_filters, ob); if (rv != APR_SUCCESS) { *err = "passing brigade to output filters"; break; } } apr_brigade_cleanup(ob); } if (clen > readbuflen) { clen -= readbuflen; goto recv_again; } } else { if (script_error_status == HTTP_OK) { b = apr_bucket_eos_create(c->bucket_alloc); APR_BRIGADE_INSERT_TAIL(ob, b); rv = ap_pass_brigade(r->output_filters, ob); if (rv != APR_SUCCESS) { *err = "passing brigade to output filters"; break; } } } break; case AP_FCGI_STDERR: if (clen) { ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01071) "Got error '%s'", iobuf); } if (clen > readbuflen) { clen -= readbuflen; goto recv_again; } break; case AP_FCGI_END_REQUEST: done = 1; break; default: ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01072) "Got bogus record %d", type); break; } if (plen) { rv = get_data_full(conn, iobuf, plen); if (rv != APR_SUCCESS) { ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(02537) "Error occurred reading padding"); break; } } } } apr_brigade_destroy(ib); apr_brigade_destroy(ob); if (script_error_status != HTTP_OK) { ap_die(script_error_status, r); } return rv; }
['static apr_status_t dispatch(proxy_conn_rec *conn, proxy_dir_conf *conf,\n request_rec *r, apr_pool_t *setaside_pool,\n apr_uint16_t request_id,\n const char **err)\n{\n apr_bucket_brigade *ib, *ob;\n int seen_end_of_headers = 0, done = 0;\n apr_status_t rv = APR_SUCCESS;\n int script_error_status = HTTP_OK;\n conn_rec *c = r->connection;\n struct iovec vec[2];\n ap_fcgi_header header;\n unsigned char farray[AP_FCGI_HEADER_LEN];\n apr_pollfd_t pfd;\n int header_state = HDR_STATE_READING_HEADERS;\n char stack_iobuf[AP_IOBUFSIZE];\n apr_size_t iobuf_size = AP_IOBUFSIZE;\n char *iobuf = stack_iobuf;\n *err = NULL;\n if (conn->worker->s->io_buffer_size_set) {\n iobuf_size = conn->worker->s->io_buffer_size;\n iobuf = apr_palloc(r->pool, iobuf_size);\n }\n pfd.desc_type = APR_POLL_SOCKET;\n pfd.desc.s = conn->sock;\n pfd.p = r->pool;\n pfd.reqevents = APR_POLLIN | APR_POLLOUT;\n ib = apr_brigade_create(r->pool, c->bucket_alloc);\n ob = apr_brigade_create(r->pool, c->bucket_alloc);\n while (! done) {\n apr_interval_time_t timeout;\n apr_size_t len;\n int n;\n apr_socket_timeout_get(conn->sock, &timeout);\n rv = apr_poll(&pfd, 1, &n, timeout);\n if (rv != APR_SUCCESS) {\n if (APR_STATUS_IS_EINTR(rv)) {\n continue;\n }\n *err = "polling";\n break;\n }\n if (pfd.rtnevents & APR_POLLOUT) {\n apr_size_t to_send, writebuflen;\n int last_stdin = 0;\n char *iobuf_cursor;\n rv = ap_get_brigade(r->input_filters, ib,\n AP_MODE_READBYTES, APR_BLOCK_READ,\n iobuf_size);\n if (rv != APR_SUCCESS) {\n *err = "reading input brigade";\n break;\n }\n if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(ib))) {\n last_stdin = 1;\n }\n writebuflen = iobuf_size;\n rv = apr_brigade_flatten(ib, iobuf, &writebuflen);\n apr_brigade_cleanup(ib);\n if (rv != APR_SUCCESS) {\n *err = "flattening brigade";\n break;\n }\n to_send = writebuflen;\n iobuf_cursor = iobuf;\n while (to_send > 0) {\n int nvec = 0;\n apr_size_t write_this_time;\n write_this_time =\n to_send < AP_FCGI_MAX_CONTENT_LEN ? to_send : AP_FCGI_MAX_CONTENT_LEN;\n ap_fcgi_fill_in_header(&header, AP_FCGI_STDIN, request_id,\n (apr_uint16_t)write_this_time, 0);\n ap_fcgi_header_to_array(&header, farray);\n vec[nvec].iov_base = (void *)farray;\n vec[nvec].iov_len = sizeof(farray);\n ++nvec;\n if (writebuflen) {\n vec[nvec].iov_base = iobuf_cursor;\n vec[nvec].iov_len = write_this_time;\n ++nvec;\n }\n rv = send_data(conn, vec, nvec, &len);\n if (rv != APR_SUCCESS) {\n *err = "sending stdin";\n break;\n }\n to_send -= write_this_time;\n iobuf_cursor += write_this_time;\n }\n if (last_stdin) {\n pfd.reqevents = APR_POLLIN;\n if (writebuflen) {\n ap_fcgi_fill_in_header(&header, AP_FCGI_STDIN, request_id,\n 0, 0);\n ap_fcgi_header_to_array(&header, farray);\n vec[0].iov_base = (void *)farray;\n vec[0].iov_len = sizeof(farray);\n rv = send_data(conn, vec, 1, &len);\n if (rv != APR_SUCCESS) {\n *err = "sending empty stdin";\n break;\n }\n }\n }\n }\n if (pfd.rtnevents & APR_POLLIN) {\n apr_size_t readbuflen;\n apr_uint16_t clen, rid;\n apr_bucket *b;\n unsigned char plen;\n unsigned char type, version;\n rv = get_data_full(conn, (char *) farray, AP_FCGI_HEADER_LEN);\n if (rv != APR_SUCCESS) {\n ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01067)\n "Failed to read FastCGI header");\n break;\n }\n#ifdef FCGI_DUMP_HEADERS\n ap_log_rdata(APLOG_MARK, APLOG_DEBUG, r, "FastCGI header",\n farray, AP_FCGI_HEADER_LEN, 0);\n#endif\n ap_fcgi_header_fields_from_array(&version, &type, &rid,\n &clen, &plen, farray);\n if (version != AP_FCGI_VERSION_1) {\n ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01068)\n "Got bogus version %d", (int)version);\n rv = APR_EINVAL;\n break;\n }\n if (rid != request_id) {\n ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01069)\n "Got bogus rid %d, expected %d",\n rid, request_id);\n rv = APR_EINVAL;\n break;\n }\nrecv_again:\n if (clen > iobuf_size) {\n readbuflen = iobuf_size;\n } else {\n readbuflen = clen;\n }\n if (readbuflen != 0) {\n rv = get_data(conn, iobuf, &readbuflen);\n if (rv != APR_SUCCESS) {\n *err = "reading response body";\n break;\n }\n }\n switch (type) {\n case AP_FCGI_STDOUT:\n if (clen != 0) {\n b = apr_bucket_transient_create(iobuf,\n readbuflen,\n c->bucket_alloc);\n APR_BRIGADE_INSERT_TAIL(ob, b);\n if (! seen_end_of_headers) {\n int st = handle_headers(r, &header_state, iobuf);\n if (st == 1) {\n int status;\n seen_end_of_headers = 1;\n status = ap_scan_script_header_err_brigade_ex(r, ob,\n NULL, APLOG_MODULE_INDEX);\n if (status != OK) {\n apr_bucket *tmp_b;\n apr_brigade_cleanup(ob);\n tmp_b = apr_bucket_eos_create(c->bucket_alloc);\n APR_BRIGADE_INSERT_TAIL(ob, tmp_b);\n r->status = status;\n ap_pass_brigade(r->output_filters, ob);\n ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01070)\n "Error parsing script headers");\n rv = APR_EINVAL;\n break;\n }\n if (conf->error_override &&\n ap_is_HTTP_ERROR(r->status)) {\n script_error_status = r->status;\n r->status = HTTP_OK;\n }\n if (script_error_status == HTTP_OK\n && !APR_BRIGADE_EMPTY(ob)) {\n rv = ap_pass_brigade(r->output_filters, ob);\n if (rv != APR_SUCCESS) {\n *err = "passing brigade to output filters";\n break;\n }\n }\n apr_brigade_cleanup(ob);\n apr_pool_clear(setaside_pool);\n }\n else {\n apr_bucket_setaside(b, setaside_pool);\n }\n } else {\n if (script_error_status == HTTP_OK) {\n rv = ap_pass_brigade(r->output_filters, ob);\n if (rv != APR_SUCCESS) {\n *err = "passing brigade to output filters";\n break;\n }\n }\n apr_brigade_cleanup(ob);\n }\n if (clen > readbuflen) {\n clen -= readbuflen;\n goto recv_again;\n }\n } else {\n if (script_error_status == HTTP_OK) {\n b = apr_bucket_eos_create(c->bucket_alloc);\n APR_BRIGADE_INSERT_TAIL(ob, b);\n rv = ap_pass_brigade(r->output_filters, ob);\n if (rv != APR_SUCCESS) {\n *err = "passing brigade to output filters";\n break;\n }\n }\n }\n break;\n case AP_FCGI_STDERR:\n if (clen) {\n ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01071)\n "Got error \'%s\'", iobuf);\n }\n if (clen > readbuflen) {\n clen -= readbuflen;\n goto recv_again;\n }\n break;\n case AP_FCGI_END_REQUEST:\n done = 1;\n break;\n default:\n ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01072)\n "Got bogus record %d", type);\n break;\n }\n if (plen) {\n rv = get_data_full(conn, iobuf, plen);\n if (rv != APR_SUCCESS) {\n ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,\n APLOGNO(02537) "Error occurred reading padding");\n break;\n }\n }\n }\n }\n apr_brigade_destroy(ib);\n apr_brigade_destroy(ob);\n if (script_error_status != HTTP_OK) {\n ap_die(script_error_status, r);\n }\n return rv;\n}']
36,433
0
https://github.com/libav/libav/blob/32a15a2441c4bfc83b2934f617d64d196492bdde/libavformat/utils.c/#L2469
void av_close_input_stream(AVFormatContext *s) { int i; AVStream *st; if (s->iformat->read_close) s->iformat->read_close(s); for(i=0;i<s->nb_streams;i++) { st = s->streams[i]; if (st->parser) { av_parser_close(st->parser); av_free_packet(&st->cur_pkt); } av_metadata_free(&st->metadata); av_free(st->index_entries); av_free(st->codec->extradata); av_free(st->codec); #if LAVF_API_OLD_METADATA av_free(st->filename); #endif av_free(st->priv_data); av_free(st); } for(i=s->nb_programs-1; i>=0; i--) { #if LAVF_API_OLD_METADATA av_freep(&s->programs[i]->provider_name); av_freep(&s->programs[i]->name); #endif av_metadata_free(&s->programs[i]->metadata); av_freep(&s->programs[i]->stream_index); av_freep(&s->programs[i]); } av_freep(&s->programs); flush_packet_queue(s); av_freep(&s->priv_data); while(s->nb_chapters--) { #if LAVF_API_OLD_METADATA av_free(s->chapters[s->nb_chapters]->title); #endif av_metadata_free(&s->chapters[s->nb_chapters]->metadata); av_free(s->chapters[s->nb_chapters]); } av_freep(&s->chapters); av_metadata_free(&s->metadata); av_free(s); }
['static int read_ffserver_streams(AVFormatContext *s, const char *filename)\n{\n int i, err;\n AVFormatContext *ic;\n int nopts = 0;\n err = av_open_input_file(&ic, filename, NULL, FFM_PACKET_SIZE, NULL);\n if (err < 0)\n return err;\n s->nb_streams = ic->nb_streams;\n for(i=0;i<ic->nb_streams;i++) {\n AVStream *st;\n AVCodec *codec;\n st = av_mallocz(sizeof(AVStream));\n memcpy(st, ic->streams[i], sizeof(AVStream));\n st->codec = avcodec_alloc_context();\n if (!st->codec) {\n print_error(filename, AVERROR(ENOMEM));\n ffmpeg_exit(1);\n }\n avcodec_copy_context(st->codec, ic->streams[i]->codec);\n s->streams[i] = st;\n codec = avcodec_find_encoder(st->codec->codec_id);\n if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {\n if (audio_stream_copy) {\n st->stream_copy = 1;\n } else\n choose_sample_fmt(st, codec);\n } else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {\n if (video_stream_copy) {\n st->stream_copy = 1;\n } else\n choose_pixel_fmt(st, codec);\n }\n if(!st->codec->thread_count)\n st->codec->thread_count = 1;\n if(st->codec->thread_count>1)\n avcodec_thread_init(st->codec, st->codec->thread_count);\n if(st->codec->flags & CODEC_FLAG_BITEXACT)\n nopts = 1;\n }\n if (!nopts)\n s->timestamp = av_gettime();\n av_close_input_file(ic);\n return 0;\n}', 'void av_close_input_file(AVFormatContext *s)\n{\n ByteIOContext *pb = s->iformat->flags & AVFMT_NOFILE ? NULL : s->pb;\n av_close_input_stream(s);\n if (pb)\n url_fclose(pb);\n}', 'void av_close_input_stream(AVFormatContext *s)\n{\n int i;\n AVStream *st;\n if (s->iformat->read_close)\n s->iformat->read_close(s);\n for(i=0;i<s->nb_streams;i++) {\n st = s->streams[i];\n if (st->parser) {\n av_parser_close(st->parser);\n av_free_packet(&st->cur_pkt);\n }\n av_metadata_free(&st->metadata);\n av_free(st->index_entries);\n av_free(st->codec->extradata);\n av_free(st->codec);\n#if LAVF_API_OLD_METADATA\n av_free(st->filename);\n#endif\n av_free(st->priv_data);\n av_free(st);\n }\n for(i=s->nb_programs-1; i>=0; i--) {\n#if LAVF_API_OLD_METADATA\n av_freep(&s->programs[i]->provider_name);\n av_freep(&s->programs[i]->name);\n#endif\n av_metadata_free(&s->programs[i]->metadata);\n av_freep(&s->programs[i]->stream_index);\n av_freep(&s->programs[i]);\n }\n av_freep(&s->programs);\n flush_packet_queue(s);\n av_freep(&s->priv_data);\n while(s->nb_chapters--) {\n#if LAVF_API_OLD_METADATA\n av_free(s->chapters[s->nb_chapters]->title);\n#endif\n av_metadata_free(&s->chapters[s->nb_chapters]->metadata);\n av_free(s->chapters[s->nb_chapters]);\n }\n av_freep(&s->chapters);\n av_metadata_free(&s->metadata);\n av_free(s);\n}']
36,434
0
https://github.com/openssl/openssl/blob/ed371b8cbac0d0349667558c061c1ae380cf75eb/crypto/bn/bn_ctx.c/#L276
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group,\n EC_POINT *point,\n const BIGNUM *x_, int y_bit,\n BN_CTX *ctx)\n{\n BN_CTX *new_ctx = NULL;\n BIGNUM *tmp1, *tmp2, *x, *y;\n int ret = 0;\n ERR_clear_error();\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n y_bit = (y_bit != 0);\n BN_CTX_start(ctx);\n tmp1 = BN_CTX_get(ctx);\n tmp2 = BN_CTX_get(ctx);\n x = BN_CTX_get(ctx);\n y = BN_CTX_get(ctx);\n if (y == NULL)\n goto err;\n if (!BN_nnmod(x, x_, group->field, ctx))\n goto err;\n if (group->meth->field_decode == 0) {\n if (!group->meth->field_sqr(group, tmp2, x_, ctx))\n goto err;\n if (!group->meth->field_mul(group, tmp1, tmp2, x_, ctx))\n goto err;\n } else {\n if (!BN_mod_sqr(tmp2, x_, group->field, ctx))\n goto err;\n if (!BN_mod_mul(tmp1, tmp2, x_, group->field, ctx))\n goto err;\n }\n if (group->a_is_minus3) {\n if (!BN_mod_lshift1_quick(tmp2, x, group->field))\n goto err;\n if (!BN_mod_add_quick(tmp2, tmp2, x, group->field))\n goto err;\n if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n } else {\n if (group->meth->field_decode) {\n if (!group->meth->field_decode(group, tmp2, group->a, ctx))\n goto err;\n if (!BN_mod_mul(tmp2, tmp2, x, group->field, ctx))\n goto err;\n } else {\n if (!group->meth->field_mul(group, tmp2, group->a, x, ctx))\n goto err;\n }\n if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n }\n if (group->meth->field_decode) {\n if (!group->meth->field_decode(group, tmp2, group->b, ctx))\n goto err;\n if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n } else {\n if (!BN_mod_add_quick(tmp1, tmp1, group->b, group->field))\n goto err;\n }\n if (!BN_mod_sqrt(y, tmp1, group->field, ctx)) {\n unsigned long err = ERR_peek_last_error();\n if (ERR_GET_LIB(err) == ERR_LIB_BN\n && ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) {\n ERR_clear_error();\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSED_POINT);\n } else\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n ERR_R_BN_LIB);\n goto err;\n }\n if (y_bit != BN_is_odd(y)) {\n if (BN_is_zero(y)) {\n int kron;\n kron = BN_kronecker(x, group->field, ctx);\n if (kron == -2)\n goto err;\n if (kron == 1)\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSION_BIT);\n else\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSED_POINT);\n goto err;\n }\n if (!BN_usub(y, group->field, y))\n goto err;\n }\n if (y_bit != BN_is_odd(y)) {\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx))\n goto err;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_start", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG_EXIT(ctx);\n}', 'int BN_mod_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 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_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}']
36,435
0
https://github.com/libav/libav/blob/0e7fa0bc3ba8eaea3eb623aa269806d2eca3a2c2/libavformat/mxfenc.c/#L1382
static uint64_t mxf_parse_timestamp(time_t timestamp) { struct tm *time = gmtime(&timestamp); return (uint64_t)(time->tm_year+1900) << 48 | (uint64_t)(time->tm_mon+1) << 40 | (uint64_t) time->tm_mday << 32 | time->tm_hour << 24 | time->tm_min << 16 | time->tm_sec << 8; }
['static uint64_t mxf_parse_timestamp(time_t timestamp)\n{\n struct tm *time = gmtime(&timestamp);\n return (uint64_t)(time->tm_year+1900) << 48 |\n (uint64_t)(time->tm_mon+1) << 40 |\n (uint64_t) time->tm_mday << 32 |\n time->tm_hour << 24 |\n time->tm_min << 16 |\n time->tm_sec << 8;\n}']
36,436
0
https://github.com/openssl/openssl/blob/6fc1748ec65c94c195d02b59556434e36a5f7651/crypto/lhash/lhash.c/#L222
static int expand(OPENSSL_LHASH *lh) { OPENSSL_LH_NODE **n, **n1, **n2, *np; unsigned int p, i, j; unsigned long hash, nni; lh->num_nodes++; lh->num_expands++; p = (int)lh->p++; n1 = &(lh->b[p]); n2 = &(lh->b[p + (int)lh->pmax]); *n2 = NULL; nni = lh->num_alloc_nodes; for (np = *n1; np != NULL;) { hash = np->hash; if ((hash % nni) != p) { *n1 = (*n1)->next; np->next = *n2; *n2 = np; } else n1 = &((*n1)->next); np = *n1; } if ((lh->p) >= lh->pmax) { j = (int)lh->num_alloc_nodes * 2; n = OPENSSL_realloc(lh->b, (int)(sizeof(OPENSSL_LH_NODE *) * j)); if (n == NULL) { lh->error++; lh->p = 0; return 0; } for (i = (int)lh->num_alloc_nodes; i < j; i++) n[i] = NULL; lh->pmax = lh->num_alloc_nodes; lh->num_alloc_nodes = j; lh->num_expand_reallocs++; lh->p = 0; lh->b = n; } return 1; }
['STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file)\n{\n BIO *in = BIO_new(BIO_s_file());\n X509 *x = NULL;\n X509_NAME *xn = NULL;\n STACK_OF(X509_NAME) *ret = NULL;\n LHASH_OF(X509_NAME) *name_hash = lh_X509_NAME_new(xname_hash, xname_cmp);\n if ((name_hash == NULL) || (in == NULL)) {\n SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (!BIO_read_filename(in, file))\n goto err;\n for (;;) {\n if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)\n break;\n if (ret == NULL) {\n ret = sk_X509_NAME_new_null();\n if (ret == NULL) {\n SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n }\n if ((xn = X509_get_subject_name(x)) == NULL)\n goto err;\n xn = X509_NAME_dup(xn);\n if (xn == NULL)\n goto err;\n if (lh_X509_NAME_retrieve(name_hash, xn) != NULL) {\n X509_NAME_free(xn);\n xn = NULL;\n } else {\n lh_X509_NAME_insert(name_hash, xn);\n if (!sk_X509_NAME_push(ret, xn))\n goto err;\n }\n }\n goto done;\n err:\n X509_NAME_free(xn);\n sk_X509_NAME_pop_free(ret, X509_NAME_free);\n ret = NULL;\n done:\n BIO_free(in);\n X509_free(x);\n lh_X509_NAME_free(name_hash);\n if (ret != NULL)\n ERR_clear_error();\n return (ret);\n}', 'DEFINE_LHASH_OF(X509_NAME)', 'OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c)\n{\n OPENSSL_LHASH *ret;\n if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)\n goto err0;\n if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)\n goto err1;\n ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c);\n ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h);\n ret->num_nodes = MIN_NODES / 2;\n ret->num_alloc_nodes = MIN_NODES;\n ret->pmax = MIN_NODES / 2;\n ret->up_load = UP_LOAD;\n ret->down_load = DOWN_LOAD;\n return (ret);\n err1:\n OPENSSL_free(ret);\n err0:\n return (NULL);\n}', 'void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)\n{\n unsigned long hash;\n OPENSSL_LH_NODE *nn, **rn;\n void *ret;\n lh->error = 0;\n if ((lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) && !expand(lh))\n return NULL;\n rn = getrn(lh, data, &hash);\n if (*rn == NULL) {\n if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {\n lh->error++;\n return (NULL);\n }\n nn->data = data;\n nn->next = NULL;\n nn->hash = hash;\n *rn = nn;\n ret = NULL;\n lh->num_insert++;\n lh->num_items++;\n } else {\n ret = (*rn)->data;\n (*rn)->data = data;\n lh->num_replace++;\n }\n return (ret);\n}', 'static int expand(OPENSSL_LHASH *lh)\n{\n OPENSSL_LH_NODE **n, **n1, **n2, *np;\n unsigned int p, i, j;\n unsigned long hash, nni;\n lh->num_nodes++;\n lh->num_expands++;\n p = (int)lh->p++;\n n1 = &(lh->b[p]);\n n2 = &(lh->b[p + (int)lh->pmax]);\n *n2 = NULL;\n nni = lh->num_alloc_nodes;\n for (np = *n1; np != NULL;) {\n hash = np->hash;\n if ((hash % nni) != p) {\n *n1 = (*n1)->next;\n np->next = *n2;\n *n2 = np;\n } else\n n1 = &((*n1)->next);\n np = *n1;\n }\n if ((lh->p) >= lh->pmax) {\n j = (int)lh->num_alloc_nodes * 2;\n n = OPENSSL_realloc(lh->b, (int)(sizeof(OPENSSL_LH_NODE *) * j));\n if (n == NULL) {\n lh->error++;\n lh->p = 0;\n return 0;\n }\n for (i = (int)lh->num_alloc_nodes; i < j; i++)\n n[i] = NULL;\n lh->pmax = lh->num_alloc_nodes;\n lh->num_alloc_nodes = j;\n lh->num_expand_reallocs++;\n lh->p = 0;\n lh->b = n;\n }\n return 1;\n}', 'void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)\n{\n if (realloc_impl != NULL && realloc_impl != &CRYPTO_realloc)\n return realloc_impl(str, num, file, line);\n if (str == NULL)\n return CRYPTO_malloc(num, file, line);\n if (num == 0) {\n CRYPTO_free(str, file, line);\n return NULL;\n }\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n void *ret;\n CRYPTO_mem_debug_realloc(str, NULL, num, 0, file, line);\n ret = realloc(str, num);\n CRYPTO_mem_debug_realloc(str, ret, num, 1, file, line);\n return ret;\n }\n#else\n osslargused(file); osslargused(line);\n#endif\n return realloc(str, num);\n}']
36,437
0
https://github.com/libav/libav/blob/80bdf7e0b7975956c42f17589cb21a5531f179ef/libavcodec/vc1dec.c/#L1599
static inline void vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y, int mv1, int r_x, int r_y, uint8_t* is_intra, int pred_flag, int dir) { MpegEncContext *s = &v->s; int xy, wrap, off = 0; int16_t *A, *B, *C; int px, py; int sum; int mixedmv_pic, num_samefield = 0, num_oppfield = 0; int opposit, f; int16_t samefield_pred[2], oppfield_pred[2]; int16_t samefield_predA[2], oppfield_predA[2]; int16_t samefield_predB[2], oppfield_predB[2]; int16_t samefield_predC[2], oppfield_predC[2]; int16_t *predA, *predC; int a_valid, b_valid, c_valid; int hybridmv_thresh, y_bias = 0; if (v->mv_mode == MV_PMODE_MIXED_MV || ((v->mv_mode == MV_PMODE_INTENSITY_COMP) && (v->mv_mode2 == MV_PMODE_MIXED_MV))) mixedmv_pic = 1; else mixedmv_pic = 0; dmv_x <<= 1 - s->quarter_sample; dmv_y <<= 1 - s->quarter_sample; wrap = s->b8_stride; xy = s->block_index[n]; if(s->mb_intra){ s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = 0; s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = 0; s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = 0; s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0; if(mv1) { s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][0] = 0; s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][1] = 0; s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][0] = 0; s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][1] = 0; s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0; s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0; v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0; s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][0] = 0; s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][1] = 0; s->current_picture.f.motion_val[1][xy + wrap][0] = 0; s->current_picture.f.motion_val[1][xy + wrap + v->blocks_off][1] = 0; s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0; s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0; } return; } C = s->current_picture.f.motion_val[dir][xy - 1 + v->blocks_off]; A = s->current_picture.f.motion_val[dir][xy - wrap + v->blocks_off]; if(mv1) { if (v->field_mode && mixedmv_pic) off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2; else off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2; } else { switch (n) { case 0: off = (s->mb_x > 0) ? -1 : 1; break; case 1: off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1; break; case 2: off = 1; break; case 3: off = -1; } } B = s->current_picture.f.motion_val[dir][xy - wrap + off + v->blocks_off]; a_valid = !s->first_slice_line || (n==2 || n==3); b_valid = a_valid && (s->mb_width > 1); c_valid = s->mb_x || (n==1 || n==3); if (v->field_mode) { a_valid = a_valid && !is_intra[xy - wrap]; b_valid = b_valid && !is_intra[xy - wrap + off]; c_valid = c_valid && !is_intra[xy - 1]; } if (a_valid) { f = v->mv_f[dir][xy - wrap + v->blocks_off]; num_oppfield += f; num_samefield += 1 - f; if (f) { oppfield_predA[0] = A[0]; oppfield_predA[1] = A[1]; samefield_predA[0] = scaleforsame(v, 0, A[0], 0, dir); samefield_predA[1] = scaleforsame(v, n, A[1], 1, dir); } else { samefield_predA[0] = A[0]; samefield_predA[1] = A[1]; if (v->numref) oppfield_predA[0] = scaleforopp(v, A[0], 0, dir); if (v->numref) oppfield_predA[1] = scaleforopp(v, A[1], 1, dir); } } else { samefield_predA[0] = samefield_predA[1] = 0; oppfield_predA[0] = oppfield_predA[1] = 0; } if (c_valid) { f = v->mv_f[dir][xy - 1 + v->blocks_off]; num_oppfield += f; num_samefield += 1 - f; if (f) { oppfield_predC[0] = C[0]; oppfield_predC[1] = C[1]; samefield_predC[0] = scaleforsame(v, 0, C[0], 0, dir); samefield_predC[1] = scaleforsame(v, n, C[1], 1, dir); } else { samefield_predC[0] = C[0]; samefield_predC[1] = C[1]; if (v->numref) oppfield_predC[0] = scaleforopp(v, C[0], 0, dir); if (v->numref) oppfield_predC[1] = scaleforopp(v, C[1], 1, dir); } } else { samefield_predC[0] = samefield_predC[1] = 0; oppfield_predC[0] = oppfield_predC[1] = 0; } if (b_valid) { f = v->mv_f[dir][xy - wrap + off + v->blocks_off]; num_oppfield += f; num_samefield += 1 - f; if (f) { oppfield_predB[0] = B[0]; oppfield_predB[1] = B[1]; samefield_predB[0] = scaleforsame(v, 0, B[0], 0, dir); samefield_predB[1] = scaleforsame(v, n, B[1], 1, dir); } else { samefield_predB[0] = B[0]; samefield_predB[1] = B[1]; if (v->numref) oppfield_predB[0] = scaleforopp(v, B[0], 0, dir); if (v->numref) oppfield_predB[1] = scaleforopp(v, B[1], 1, dir); } } else { samefield_predB[0] = samefield_predB[1] = 0; oppfield_predB[0] = oppfield_predB[1] = 0; } if (a_valid) { samefield_pred[0] = samefield_predA[0]; samefield_pred[1] = samefield_predA[1]; oppfield_pred[0] = oppfield_predA[0]; oppfield_pred[1] = oppfield_predA[1]; } else if (c_valid) { samefield_pred[0] = samefield_predC[0]; samefield_pred[1] = samefield_predC[1]; oppfield_pred[0] = oppfield_predC[0]; oppfield_pred[1] = oppfield_predC[1]; } else if (b_valid) { samefield_pred[0] = samefield_predB[0]; samefield_pred[1] = samefield_predB[1]; oppfield_pred[0] = oppfield_predB[0]; oppfield_pred[1] = oppfield_predB[1]; } else { samefield_pred[0] = samefield_pred[1] = 0; oppfield_pred[0] = oppfield_pred[1] = 0; } if (num_samefield + num_oppfield > 1) { samefield_pred[0] = mid_pred(samefield_predA[0], samefield_predB[0], samefield_predC[0]); samefield_pred[1] = mid_pred(samefield_predA[1], samefield_predB[1], samefield_predC[1]); if (v->numref) oppfield_pred[0] = mid_pred(oppfield_predA[0], oppfield_predB[0], oppfield_predC[0]); if (v->numref) oppfield_pred[1] = mid_pred(oppfield_predA[1], oppfield_predB[1], oppfield_predC[1]); } if (v->field_mode) { if (num_samefield <= num_oppfield) opposit = 1 - pred_flag; else opposit = pred_flag; } else opposit = 0; if (opposit) { px = oppfield_pred[0]; py = oppfield_pred[1]; predA = oppfield_predA; predC = oppfield_predC; v->mv_f[dir][xy + v->blocks_off] = f = 1; v->ref_field_type[dir] = !v->cur_field_type; } else { px = samefield_pred[0]; py = samefield_pred[1]; predA = samefield_predA; predC = samefield_predC; v->mv_f[dir][xy + v->blocks_off] = f = 0; v->ref_field_type[dir] = v->cur_field_type; } if (!v->field_mode) { int qx, qy, X, Y; qx = (s->mb_x << 6) + ((n==1 || n==3) ? 32 : 0); qy = (s->mb_y << 6) + ((n==2 || n==3) ? 32 : 0); X = (s->mb_width << 6) - 4; Y = (s->mb_height << 6) - 4; if(mv1) { if(qx + px < -60) px = -60 - qx; if(qy + py < -60) py = -60 - qy; } else { if(qx + px < -28) px = -28 - qx; if(qy + py < -28) py = -28 - qy; } if(qx + px > X) px = X - qx; if(qy + py > Y) py = Y - qy; } if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) { if (v->field_mode && !s->quarter_sample) hybridmv_thresh = 16; else hybridmv_thresh = 32; if (a_valid && c_valid) { if (is_intra[xy - wrap]) sum = FFABS(px) + FFABS(py); else sum = FFABS(px - predA[0]) + FFABS(py - predA[1]); if (sum > hybridmv_thresh) { if (get_bits1(&s->gb)) { px = predA[0]; py = predA[1]; } else { px = predC[0]; py = predC[1]; } } else { if (is_intra[xy - 1]) sum = FFABS(px) + FFABS(py); else sum = FFABS(px - predC[0]) + FFABS(py - predC[1]); if (sum > hybridmv_thresh) { if(get_bits1(&s->gb)) { px = predA[0]; py = predA[1]; } else { px = predC[0]; py = predC[1]; } } } } } if (v->field_mode && !s->quarter_sample) { r_x <<= 1; r_y <<= 1; } if (v->field_mode && v->numref) r_y >>= 1; if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0) y_bias = 1; s->mv[dir][n][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x; s->mv[dir][n][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1] = ((py + dmv_y + r_y - y_bias) & ((r_y << 1) - 1)) - r_y + y_bias; if(mv1) { s->current_picture.f.motion_val[dir][xy + 1 + v->blocks_off][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0]; s->current_picture.f.motion_val[dir][xy + 1 + v->blocks_off][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1]; s->current_picture.f.motion_val[dir][xy + wrap + v->blocks_off][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0]; s->current_picture.f.motion_val[dir][xy + wrap + v->blocks_off][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1]; s->current_picture.f.motion_val[dir][xy + wrap + 1 + v->blocks_off][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0]; s->current_picture.f.motion_val[dir][xy + wrap + 1 + v->blocks_off][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1]; v->mv_f[dir][xy + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off]; v->mv_f[dir][xy + wrap + v->blocks_off] = v->mv_f[dir][xy + wrap + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off]; } }
['static inline void vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y, int mv1, int r_x, int r_y, uint8_t* is_intra, int pred_flag, int dir)\n{\n MpegEncContext *s = &v->s;\n int xy, wrap, off = 0;\n int16_t *A, *B, *C;\n int px, py;\n int sum;\n int mixedmv_pic, num_samefield = 0, num_oppfield = 0;\n int opposit, f;\n int16_t samefield_pred[2], oppfield_pred[2];\n int16_t samefield_predA[2], oppfield_predA[2];\n int16_t samefield_predB[2], oppfield_predB[2];\n int16_t samefield_predC[2], oppfield_predC[2];\n int16_t *predA, *predC;\n int a_valid, b_valid, c_valid;\n int hybridmv_thresh, y_bias = 0;\n if (v->mv_mode == MV_PMODE_MIXED_MV ||\n ((v->mv_mode == MV_PMODE_INTENSITY_COMP) && (v->mv_mode2 == MV_PMODE_MIXED_MV))) mixedmv_pic = 1;\n else mixedmv_pic = 0;\n dmv_x <<= 1 - s->quarter_sample;\n dmv_y <<= 1 - s->quarter_sample;\n wrap = s->b8_stride;\n xy = s->block_index[n];\n if(s->mb_intra){\n s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = 0;\n s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = 0;\n s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = 0;\n s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0;\n if(mv1) {\n s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][0] = 0;\n s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][1] = 0;\n s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][0] = 0;\n s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][1] = 0;\n s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;\n s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;\n v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;\n s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][0] = 0;\n s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][1] = 0;\n s->current_picture.f.motion_val[1][xy + wrap][0] = 0;\n s->current_picture.f.motion_val[1][xy + wrap + v->blocks_off][1] = 0;\n s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;\n s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;\n }\n return;\n }\n C = s->current_picture.f.motion_val[dir][xy - 1 + v->blocks_off];\n A = s->current_picture.f.motion_val[dir][xy - wrap + v->blocks_off];\n if(mv1) {\n if (v->field_mode && mixedmv_pic)\n off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;\n else\n off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;\n } else {\n switch (n) {\n case 0:\n off = (s->mb_x > 0) ? -1 : 1;\n break;\n case 1:\n off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;\n break;\n case 2:\n off = 1;\n break;\n case 3:\n off = -1;\n }\n }\n B = s->current_picture.f.motion_val[dir][xy - wrap + off + v->blocks_off];\n a_valid = !s->first_slice_line || (n==2 || n==3);\n b_valid = a_valid && (s->mb_width > 1);\n c_valid = s->mb_x || (n==1 || n==3);\n if (v->field_mode) {\n a_valid = a_valid && !is_intra[xy - wrap];\n b_valid = b_valid && !is_intra[xy - wrap + off];\n c_valid = c_valid && !is_intra[xy - 1];\n }\n if (a_valid) {\n f = v->mv_f[dir][xy - wrap + v->blocks_off];\n num_oppfield += f;\n num_samefield += 1 - f;\n if (f) {\n oppfield_predA[0] = A[0];\n oppfield_predA[1] = A[1];\n samefield_predA[0] = scaleforsame(v, 0, A[0], 0, dir);\n samefield_predA[1] = scaleforsame(v, n, A[1], 1, dir);\n } else {\n samefield_predA[0] = A[0];\n samefield_predA[1] = A[1];\n if (v->numref)\n oppfield_predA[0] = scaleforopp(v, A[0], 0, dir);\n if (v->numref)\n oppfield_predA[1] = scaleforopp(v, A[1], 1, dir);\n }\n } else {\n samefield_predA[0] = samefield_predA[1] = 0;\n oppfield_predA[0] = oppfield_predA[1] = 0;\n }\n if (c_valid) {\n f = v->mv_f[dir][xy - 1 + v->blocks_off];\n num_oppfield += f;\n num_samefield += 1 - f;\n if (f) {\n oppfield_predC[0] = C[0];\n oppfield_predC[1] = C[1];\n samefield_predC[0] = scaleforsame(v, 0, C[0], 0, dir);\n samefield_predC[1] = scaleforsame(v, n, C[1], 1, dir);\n } else {\n samefield_predC[0] = C[0];\n samefield_predC[1] = C[1];\n if (v->numref)\n oppfield_predC[0] = scaleforopp(v, C[0], 0, dir);\n if (v->numref)\n oppfield_predC[1] = scaleforopp(v, C[1], 1, dir);\n }\n } else {\n samefield_predC[0] = samefield_predC[1] = 0;\n oppfield_predC[0] = oppfield_predC[1] = 0;\n }\n if (b_valid) {\n f = v->mv_f[dir][xy - wrap + off + v->blocks_off];\n num_oppfield += f;\n num_samefield += 1 - f;\n if (f) {\n oppfield_predB[0] = B[0];\n oppfield_predB[1] = B[1];\n samefield_predB[0] = scaleforsame(v, 0, B[0], 0, dir);\n samefield_predB[1] = scaleforsame(v, n, B[1], 1, dir);\n } else {\n samefield_predB[0] = B[0];\n samefield_predB[1] = B[1];\n if (v->numref)\n oppfield_predB[0] = scaleforopp(v, B[0], 0, dir);\n if (v->numref)\n oppfield_predB[1] = scaleforopp(v, B[1], 1, dir);\n }\n } else {\n samefield_predB[0] = samefield_predB[1] = 0;\n oppfield_predB[0] = oppfield_predB[1] = 0;\n }\n if (a_valid) {\n samefield_pred[0] = samefield_predA[0];\n samefield_pred[1] = samefield_predA[1];\n oppfield_pred[0] = oppfield_predA[0];\n oppfield_pred[1] = oppfield_predA[1];\n } else if (c_valid) {\n samefield_pred[0] = samefield_predC[0];\n samefield_pred[1] = samefield_predC[1];\n oppfield_pred[0] = oppfield_predC[0];\n oppfield_pred[1] = oppfield_predC[1];\n } else if (b_valid) {\n samefield_pred[0] = samefield_predB[0];\n samefield_pred[1] = samefield_predB[1];\n oppfield_pred[0] = oppfield_predB[0];\n oppfield_pred[1] = oppfield_predB[1];\n } else {\n samefield_pred[0] = samefield_pred[1] = 0;\n oppfield_pred[0] = oppfield_pred[1] = 0;\n }\n if (num_samefield + num_oppfield > 1) {\n samefield_pred[0] = mid_pred(samefield_predA[0], samefield_predB[0], samefield_predC[0]);\n samefield_pred[1] = mid_pred(samefield_predA[1], samefield_predB[1], samefield_predC[1]);\n if (v->numref)\n oppfield_pred[0] = mid_pred(oppfield_predA[0], oppfield_predB[0], oppfield_predC[0]);\n if (v->numref)\n oppfield_pred[1] = mid_pred(oppfield_predA[1], oppfield_predB[1], oppfield_predC[1]);\n }\n if (v->field_mode) {\n if (num_samefield <= num_oppfield)\n opposit = 1 - pred_flag;\n else\n opposit = pred_flag;\n } else\n opposit = 0;\n if (opposit) {\n px = oppfield_pred[0];\n py = oppfield_pred[1];\n predA = oppfield_predA;\n predC = oppfield_predC;\n v->mv_f[dir][xy + v->blocks_off] = f = 1;\n v->ref_field_type[dir] = !v->cur_field_type;\n } else {\n px = samefield_pred[0];\n py = samefield_pred[1];\n predA = samefield_predA;\n predC = samefield_predC;\n v->mv_f[dir][xy + v->blocks_off] = f = 0;\n v->ref_field_type[dir] = v->cur_field_type;\n }\n if (!v->field_mode) {\n int qx, qy, X, Y;\n qx = (s->mb_x << 6) + ((n==1 || n==3) ? 32 : 0);\n qy = (s->mb_y << 6) + ((n==2 || n==3) ? 32 : 0);\n X = (s->mb_width << 6) - 4;\n Y = (s->mb_height << 6) - 4;\n if(mv1) {\n if(qx + px < -60) px = -60 - qx;\n if(qy + py < -60) py = -60 - qy;\n } else {\n if(qx + px < -28) px = -28 - qx;\n if(qy + py < -28) py = -28 - qy;\n }\n if(qx + px > X) px = X - qx;\n if(qy + py > Y) py = Y - qy;\n }\n if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {\n if (v->field_mode && !s->quarter_sample)\n hybridmv_thresh = 16;\n else\n hybridmv_thresh = 32;\n if (a_valid && c_valid) {\n if (is_intra[xy - wrap])\n sum = FFABS(px) + FFABS(py);\n else\n sum = FFABS(px - predA[0]) + FFABS(py - predA[1]);\n if (sum > hybridmv_thresh) {\n if (get_bits1(&s->gb)) {\n px = predA[0];\n py = predA[1];\n } else {\n px = predC[0];\n py = predC[1];\n }\n } else {\n if (is_intra[xy - 1])\n sum = FFABS(px) + FFABS(py);\n else\n sum = FFABS(px - predC[0]) + FFABS(py - predC[1]);\n if (sum > hybridmv_thresh) {\n if(get_bits1(&s->gb)) {\n px = predA[0];\n py = predA[1];\n } else {\n px = predC[0];\n py = predC[1];\n }\n }\n }\n }\n }\n if (v->field_mode && !s->quarter_sample) {\n r_x <<= 1;\n r_y <<= 1;\n }\n if (v->field_mode && v->numref)\n r_y >>= 1;\n if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)\n y_bias = 1;\n s->mv[dir][n][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;\n s->mv[dir][n][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1] = ((py + dmv_y + r_y - y_bias) & ((r_y << 1) - 1)) - r_y + y_bias;\n if(mv1) {\n s->current_picture.f.motion_val[dir][xy + 1 + v->blocks_off][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0];\n s->current_picture.f.motion_val[dir][xy + 1 + v->blocks_off][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1];\n s->current_picture.f.motion_val[dir][xy + wrap + v->blocks_off][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0];\n s->current_picture.f.motion_val[dir][xy + wrap + v->blocks_off][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1];\n s->current_picture.f.motion_val[dir][xy + wrap + 1 + v->blocks_off][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0];\n s->current_picture.f.motion_val[dir][xy + wrap + 1 + v->blocks_off][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1];\n v->mv_f[dir][xy + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];\n v->mv_f[dir][xy + wrap + v->blocks_off] = v->mv_f[dir][xy + wrap + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];\n }\n}']
36,438
0
https://github.com/openssl/openssl/blob/ed371b8cbac0d0349667558c061c1ae380cf75eb/crypto/bn/bn_ctx.c/#L276
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['static int test_kronecker(void)\n{\n BIGNUM *a = NULL, *b = NULL, *r = NULL, *t = NULL;\n int i, legendre, kronecker, st = 0;\n if (!TEST_ptr(a = BN_new())\n || !TEST_ptr(b = BN_new())\n || !TEST_ptr(r = BN_new())\n || !TEST_ptr(t = BN_new()))\n goto err;\n if (!TEST_true(BN_generate_prime_ex(b, 512, 0, NULL, NULL, NULL)))\n goto err;\n BN_set_negative(b, rand_neg());\n for (i = 0; i < NUM0; i++) {\n if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))\n goto err;\n BN_set_negative(a, rand_neg());\n if (!TEST_true(BN_copy(t, b)))\n goto err;\n BN_set_negative(t, 0);\n if (!TEST_true(BN_sub_word(t, 1)))\n goto err;\n if (!TEST_true(BN_rshift1(t, t)))\n goto err;\n BN_set_negative(b, 0);\n if (!TEST_true(BN_mod_exp_recp(r, a, t, b, ctx)))\n goto err;\n BN_set_negative(b, 1);\n if (BN_is_word(r, 1))\n legendre = 1;\n else if (BN_is_zero(r))\n legendre = 0;\n else {\n if (!TEST_true(BN_add_word(r, 1)))\n goto err;\n if (!TEST_int_eq(BN_ucmp(r, b), 0)) {\n TEST_info("Legendre symbol computation failed");\n goto err;\n }\n legendre = -1;\n }\n if (!TEST_int_ge(kronecker = BN_kronecker(a, b, ctx), -1))\n goto err;\n if (BN_is_negative(a) && BN_is_negative(b))\n kronecker = -kronecker;\n if (!TEST_int_eq(legendre, kronecker))\n goto err;\n }\n st = 1;\n err:\n BN_free(a);\n BN_free(b);\n BN_free(r);\n BN_free(t);\n return st;\n}', 'int BN_kronecker(const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int i;\n int ret = -2;\n int err = 0;\n BIGNUM *A, *B, *tmp;\n static const int tab[8] = { 0, 1, 0, -1, 0, -1, 0, 1 };\n bn_check_top(a);\n bn_check_top(b);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n if (B == NULL)\n goto end;\n err = !BN_copy(A, a);\n if (err)\n goto end;\n err = !BN_copy(B, b);\n if (err)\n goto end;\n if (BN_is_zero(B)) {\n ret = BN_abs_is_word(A, 1);\n goto end;\n }\n if (!BN_is_odd(A) && !BN_is_odd(B)) {\n ret = 0;\n goto end;\n }\n i = 0;\n while (!BN_is_bit_set(B, i))\n i++;\n err = !BN_rshift(B, B, i);\n if (err)\n goto end;\n if (i & 1) {\n ret = tab[BN_lsw(A) & 7];\n } else {\n ret = 1;\n }\n if (B->neg) {\n B->neg = 0;\n if (A->neg)\n ret = -ret;\n }\n while (1) {\n if (BN_is_zero(A)) {\n ret = BN_is_one(B) ? ret : 0;\n goto end;\n }\n i = 0;\n while (!BN_is_bit_set(A, i))\n i++;\n err = !BN_rshift(A, A, i);\n if (err)\n goto end;\n if (i & 1) {\n ret = ret * tab[BN_lsw(B) & 7];\n }\n if ((A->neg ? ~BN_lsw(A) : BN_lsw(A)) & BN_lsw(B) & 2)\n ret = -ret;\n err = !BN_nnmod(B, B, A, ctx);\n if (err)\n goto end;\n tmp = A;\n A = B;\n B = tmp;\n tmp->neg = 0;\n }\n end:\n BN_CTX_end(ctx);\n if (err)\n return -2;\n else\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_start", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG_EXIT(ctx);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx)\n{\n int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n int start = 1;\n BIGNUM *aa;\n BIGNUM *val[TABLE_SIZE];\n BN_RECP_CTX recp;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(a, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n BNerr(BN_F_BN_MOD_EXP_RECP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_abs_is_word(m, 1)) {\n ret = 1;\n BN_zero(r);\n } else {\n ret = BN_one(r);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n aa = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (val[0] == NULL)\n goto err;\n BN_RECP_CTX_init(&recp);\n if (m->neg) {\n if (!BN_copy(aa, m))\n goto err;\n aa->neg = 0;\n if (BN_RECP_CTX_set(&recp, aa, ctx) <= 0)\n goto err;\n } else {\n if (BN_RECP_CTX_set(&recp, m, ctx) <= 0)\n goto err;\n }\n if (!BN_nnmod(val[0], a, m, ctx))\n goto err;\n if (BN_is_zero(val[0])) {\n BN_zero(r);\n ret = 1;\n goto err;\n }\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!BN_mod_mul_reciprocal(aa, val[0], val[0], &recp, ctx))\n goto err;\n j = 1 << (window - 1);\n for (i = 1; i < j; i++) {\n if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul_reciprocal(val[i], val[i - 1], aa, &recp, ctx))\n goto err;\n }\n }\n start = 1;\n wvalue = 0;\n wstart = bits - 1;\n wend = 0;\n if (!BN_one(r))\n goto err;\n for (;;) {\n if (BN_is_bit_set(p, wstart) == 0) {\n if (!start)\n if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))\n goto err;\n if (wstart == 0)\n break;\n wstart--;\n continue;\n }\n j = wstart;\n wvalue = 1;\n wend = 0;\n for (i = 1; i < window; i++) {\n if (wstart - i < 0)\n break;\n if (BN_is_bit_set(p, wstart - i)) {\n wvalue <<= (i - wend);\n wvalue |= 1;\n wend = i;\n }\n }\n j = wend + 1;\n if (!start)\n for (i = 0; i < j; i++) {\n if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))\n goto err;\n }\n if (!BN_mod_mul_reciprocal(r, r, val[wvalue >> 1], &recp, ctx))\n goto err;\n wstart -= wend + 1;\n wvalue = 0;\n start = 0;\n if (wstart < 0)\n break;\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_RECP_CTX_free(&recp);\n bn_check_top(r);\n return ret;\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int ret;\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return 0;\n }\n if (divisor->d[divisor->top - 1] == 0) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n ret = bn_div_fixed_top(dv, rm, num, divisor, ctx);\n if (ret) {\n if (dv != NULL)\n bn_correct_top(dv);\n if (rm != NULL)\n bn_correct_top(rm);\n }\n return ret;\n}', 'int bn_div_fixed_top(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,\n const BIGNUM *divisor, BN_CTX *ctx)\n{\n int norm_shift, i, j, loop;\n BIGNUM *tmp, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnum, *wnumtop;\n BN_ULONG d0, d1;\n int num_n, div_n;\n assert(divisor->top > 0 && divisor->d[divisor->top - 1] != 0);\n bn_check_top(num);\n bn_check_top(divisor);\n bn_check_top(dv);\n bn_check_top(rm);\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n if (!BN_copy(sdiv, divisor))\n goto err;\n norm_shift = bn_left_align(sdiv);\n sdiv->neg = 0;\n if (!(bn_lshift_fixed_top(snum, num, norm_shift)))\n goto err;\n div_n = sdiv->top;\n num_n = snum->top;\n if (num_n <= div_n) {\n if (bn_wexpand(snum, div_n + 1) == NULL)\n goto err;\n memset(&(snum->d[num_n]), 0, (div_n - num_n + 1) * sizeof(BN_ULONG));\n snum->top = num_n = div_n + 1;\n }\n loop = num_n - div_n;\n wnum = &(snum->d[loop]);\n wnumtop = &(snum->d[num_n - 1]);\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n if (!bn_wexpand(res, loop))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop;\n res->flags |= BN_FLG_FIXED_TOP;\n resp = &(res->d[loop]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n for (i = 0; i < loop; i++, wnumtop--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W)\n q = bn_div_3_words(wnumtop, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnumtop[0];\n n1 = wnumtop[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n BN_ULONG n2 = (wnumtop == wnum) ? 0 : wnumtop[-2];\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | n2))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= n2)))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum--;\n l0 = bn_sub_words(wnum, wnum, tmp->d, div_n + 1);\n q -= l0;\n for (l0 = 0 - l0, j = 0; j < div_n; j++)\n tmp->d[j] = sdiv->d[j] & l0;\n l0 = bn_add_words(wnum, wnum, tmp->d, div_n);\n (*wnumtop) += l0;\n assert((*wnumtop) == 0);\n *--resp = q;\n }\n snum->neg = num->neg;\n snum->top = div_n;\n snum->flags |= BN_FLG_FIXED_TOP;\n if (rm != NULL)\n bn_rshift_fixed_top(rm, snum, norm_shift);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return 0;\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
36,439
0
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/bn/bn_lib.c/#L536
int BN_set_word(BIGNUM *a, BN_ULONG w) { bn_check_top(a); if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL) return (0); a->neg = 0; a->d[0] = w; a->top = (w ? 1 : 0); bn_check_top(a); return (1); }
['int test_add(BIO *bp)\n{\n BIGNUM *a, *b, *c;\n int i;\n a = BN_new();\n b = BN_new();\n c = BN_new();\n BN_bntest_rand(a, 512, 0, 0);\n for (i = 0; i < num0; i++) {\n BN_bntest_rand(b, 450 + i, 0, 0);\n a->neg = rand_neg();\n b->neg = rand_neg();\n BN_add(c, a, b);\n if (bp != NULL) {\n if (!results) {\n BN_print(bp, a);\n BIO_puts(bp, " + ");\n BN_print(bp, b);\n BIO_puts(bp, " - ");\n }\n BN_print(bp, c);\n BIO_puts(bp, "\\n");\n }\n a->neg = !a->neg;\n b->neg = !b->neg;\n BN_add(c, c, b);\n BN_add(c, c, a);\n if (!BN_is_zero(c)) {\n fprintf(stderr, "Add test failed!\\n");\n return 0;\n }\n }\n BN_free(a);\n BN_free(b);\n BN_free(c);\n return (1);\n}', 'int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom)\n{\n return bnrand(2, 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}', '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}']
36,440
0
https://github.com/openssl/openssl/blob/6882a964460cfbe350219236fb6aa141eb4f1c97/crypto/asn1/asn1_par.c/#L383
const char *ASN1_tag2str(int tag) { const static char *tag2str[] = { "EOC", "BOOLEAN", "INTEGER", "BIT STRING", "OCTET STRING", "NULL", "OBJECT", "OBJECT DESCRIPTOR", "EXTERNAL", "REAL", "ENUMERATED", "<ASN1 11>", "UTF8STRING", "<ASN1 13>", "<ASN1 14>", "<ASN1 15>", "SEQUENCE", "SET", "NUMERICSTRING", "PRINTABLESTRING", "T61STRING", "VIDEOTEXSTRING", "IA5STRING", "UTCTIME" "GENERALIZEDTIME", "GRAPHICSTRING", "VISIBLESTRING", "GENERALSTRING", "UNIVERSALSTRING", "<ASN1 29>", "BMPSTRING" }; if((tag == V_ASN1_NEG_INTEGER) || (tag == V_ASN1_NEG_ENUMERATED)) tag &= ~0x100; if(tag < 0 || tag > 30) return "(unknown)"; return tag2str[tag]; }
['static int asn1_parse2(BIO *bp, unsigned char **pp, long length, int offset,\n\t int depth, int indent)\n\t{\n\tunsigned char *p,*ep,*tot,*op,*opp;\n\tlong len;\n\tint tag,xclass,ret=0;\n\tint nl,hl,j,r;\n\tASN1_OBJECT *o=NULL;\n\tASN1_OCTET_STRING *os=NULL;\n\tp= *pp;\n\ttot=p+length;\n\top=p-1;\n\twhile ((p < tot) && (op < p))\n\t\t{\n\t\top=p;\n\t\tj=ASN1_get_object(&p,&len,&tag,&xclass,length);\n#ifdef LINT\n\t\tj=j;\n#endif\n\t\tif (j & 0x80)\n\t\t\t{\n\t\t\tif (BIO_write(bp,"Error in encoding\\n",18) <= 0)\n\t\t\t\tgoto end;\n\t\t\tret=0;\n\t\t\tgoto end;\n\t\t\t}\n\t\thl=(p-op);\n\t\tlength-=hl;\n\t\tif (BIO_printf(bp,"%5ld:",(long)offset+(long)(op- *pp))\n\t\t\t<= 0) goto end;\n\t\tif (j != (V_ASN1_CONSTRUCTED | 1))\n\t\t\t{\n\t\t\tif (BIO_printf(bp,"d=%-2d hl=%ld l=%4ld ",\n\t\t\t\tdepth,(long)hl,len) <= 0)\n\t\t\t\tgoto end;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (BIO_printf(bp,"d=%-2d hl=%ld l=inf ",\n\t\t\t\tdepth,(long)hl) <= 0)\n\t\t\t\tgoto end;\n\t\t\t}\n\t\tif (!asn1_print_info(bp,tag,xclass,j,(indent)?depth:0))\n\t\t\tgoto end;\n\t\tif (j & V_ASN1_CONSTRUCTED)\n\t\t\t{\n\t\t\tep=p+len;\n\t\t\tif (BIO_write(bp,"\\n",1) <= 0) goto end;\n\t\t\tif (len > length)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bp,\n\t\t\t\t\t"length is greater than %ld\\n",length);\n\t\t\t\tret=0;\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\tif ((j == 0x21) && (len == 0))\n\t\t\t\t{\n\t\t\t\tfor (;;)\n\t\t\t\t\t{\n\t\t\t\t\tr=asn1_parse2(bp,&p,(long)(tot-p),\n\t\t\t\t\t\toffset+(p - *pp),depth+1,\n\t\t\t\t\t\tindent);\n\t\t\t\t\tif (r == 0) { ret=0; goto end; }\n\t\t\t\t\tif ((r == 2) || (p >= tot)) break;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\telse\n\t\t\t\twhile (p < ep)\n\t\t\t\t\t{\n\t\t\t\t\tr=asn1_parse2(bp,&p,(long)len,\n\t\t\t\t\t\toffset+(p - *pp),depth+1,\n\t\t\t\t\t\tindent);\n\t\t\t\t\tif (r == 0) { ret=0; goto end; }\n\t\t\t\t\t}\n\t\t\t}\n\t\telse if (xclass != 0)\n\t\t\t{\n\t\t\tp+=len;\n\t\t\tif (BIO_write(bp,"\\n",1) <= 0) goto end;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tnl=0;\n\t\t\tif (\t(tag == V_ASN1_PRINTABLESTRING) ||\n\t\t\t\t(tag == V_ASN1_T61STRING) ||\n\t\t\t\t(tag == V_ASN1_IA5STRING) ||\n\t\t\t\t(tag == V_ASN1_VISIBLESTRING) ||\n\t\t\t\t(tag == V_ASN1_UTCTIME) ||\n\t\t\t\t(tag == V_ASN1_GENERALIZEDTIME))\n\t\t\t\t{\n\t\t\t\tif (BIO_write(bp,":",1) <= 0) goto end;\n\t\t\t\tif ((len > 0) &&\n\t\t\t\t\tBIO_write(bp,(char *)p,(int)len)\n\t\t\t\t\t!= (int)len)\n\t\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\telse if (tag == V_ASN1_OBJECT)\n\t\t\t\t{\n\t\t\t\topp=op;\n\t\t\t\tif (d2i_ASN1_OBJECT(&o,&opp,len+hl) != NULL)\n\t\t\t\t\t{\n\t\t\t\t\tif (BIO_write(bp,":",1) <= 0) goto end;\n\t\t\t\t\ti2a_ASN1_OBJECT(bp,o);\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tif (BIO_write(bp,":BAD OBJECT",11) <= 0)\n\t\t\t\t\t\tgoto end;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\telse if (tag == V_ASN1_BOOLEAN)\n\t\t\t\t{\n\t\t\t\tint ii;\n\t\t\t\topp=op;\n\t\t\t\tii=d2i_ASN1_BOOLEAN(NULL,&opp,len+hl);\n\t\t\t\tif (ii < 0)\n\t\t\t\t\t{\n\t\t\t\t\tif (BIO_write(bp,"Bad boolean\\n",12))\n\t\t\t\t\t\tgoto end;\n\t\t\t\t\t}\n\t\t\t\tBIO_printf(bp,":%d",ii);\n\t\t\t\t}\n\t\t\telse if (tag == V_ASN1_BMPSTRING)\n\t\t\t\t{\n\t\t\t\t}\n\t\t\telse if (tag == V_ASN1_OCTET_STRING)\n\t\t\t\t{\n\t\t\t\tint i,printable=1;\n\t\t\t\topp=op;\n\t\t\t\tos=d2i_ASN1_OCTET_STRING(NULL,&opp,len+hl);\n\t\t\t\tif (os != NULL)\n\t\t\t\t\t{\n\t\t\t\t\topp=os->data;\n\t\t\t\t\tfor (i=0; i<os->length; i++)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tif ((\t(opp[i] < \' \') &&\n\t\t\t\t\t\t\t(opp[i] != \'\\n\') &&\n\t\t\t\t\t\t\t(opp[i] != \'\\r\') &&\n\t\t\t\t\t\t\t(opp[i] != \'\\t\')) ||\n\t\t\t\t\t\t\t(opp[i] > \'~\'))\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\tprintable=0;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\tif (printable && (os->length > 0))\n\t\t\t\t\t\t{\n\t\t\t\t\t\tif (BIO_write(bp,":",1) <= 0)\n\t\t\t\t\t\t\tgoto end;\n\t\t\t\t\t\tif (BIO_write(bp,(char *)opp,\n\t\t\t\t\t\t\tos->length) <= 0)\n\t\t\t\t\t\t\tgoto end;\n\t\t\t\t\t\t}\n\t\t\t\t\tASN1_OCTET_STRING_free(os);\n\t\t\t\t\tos=NULL;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\telse if (tag == V_ASN1_INTEGER)\n\t\t\t\t{\n\t\t\t\tASN1_INTEGER *bs;\n\t\t\t\tint i;\n\t\t\t\topp=op;\n\t\t\t\tbs=d2i_ASN1_INTEGER(NULL,&opp,len+hl);\n\t\t\t\tif (bs != NULL)\n\t\t\t\t\t{\n\t\t\t\t\tif (BIO_write(bp,":",1) <= 0) goto end;\n\t\t\t\t\tif (bs->type == V_ASN1_NEG_INTEGER)\n\t\t\t\t\t\tif (BIO_write(bp,"-",1) <= 0)\n\t\t\t\t\t\t\tgoto end;\n\t\t\t\t\tfor (i=0; i<bs->length; i++)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tif (BIO_printf(bp,"%02X",\n\t\t\t\t\t\t\tbs->data[i]) <= 0)\n\t\t\t\t\t\t\tgoto end;\n\t\t\t\t\t\t}\n\t\t\t\t\tif (bs->length == 0)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tif (BIO_write(bp,"00",2) <= 0)\n\t\t\t\t\t\t\tgoto end;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tif (BIO_write(bp,"BAD INTEGER",11) <= 0)\n\t\t\t\t\t\tgoto end;\n\t\t\t\t\t}\n\t\t\t\tASN1_INTEGER_free(bs);\n\t\t\t\t}\n\t\t\telse if (tag == V_ASN1_ENUMERATED)\n\t\t\t\t{\n\t\t\t\tASN1_ENUMERATED *bs;\n\t\t\t\tint i;\n\t\t\t\topp=op;\n\t\t\t\tbs=d2i_ASN1_ENUMERATED(NULL,&opp,len+hl);\n\t\t\t\tif (bs != NULL)\n\t\t\t\t\t{\n\t\t\t\t\tif (BIO_write(bp,":",1) <= 0) goto end;\n\t\t\t\t\tif (bs->type == V_ASN1_NEG_ENUMERATED)\n\t\t\t\t\t\tif (BIO_write(bp,"-",1) <= 0)\n\t\t\t\t\t\t\tgoto end;\n\t\t\t\t\tfor (i=0; i<bs->length; i++)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tif (BIO_printf(bp,"%02X",\n\t\t\t\t\t\t\tbs->data[i]) <= 0)\n\t\t\t\t\t\t\tgoto end;\n\t\t\t\t\t\t}\n\t\t\t\t\tif (bs->length == 0)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tif (BIO_write(bp,"00",2) <= 0)\n\t\t\t\t\t\t\tgoto end;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tif (BIO_write(bp,"BAD ENUMERATED",11) <= 0)\n\t\t\t\t\t\tgoto end;\n\t\t\t\t\t}\n\t\t\t\tASN1_ENUMERATED_free(bs);\n\t\t\t\t}\n\t\t\tif (!nl)\n\t\t\t\t{\n\t\t\t\tif (BIO_write(bp,"\\n",1) <= 0) goto end;\n\t\t\t\t}\n\t\t\tp+=len;\n\t\t\tif ((tag == V_ASN1_EOC) && (xclass == 0))\n\t\t\t\t{\n\t\t\t\tret=2;\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\t}\n\t\tlength-=len;\n\t\t}\n\tret=1;\nend:\n\tif (o != NULL) ASN1_OBJECT_free(o);\n\tif (os != NULL) ASN1_OCTET_STRING_free(os);\n\t*pp=p;\n\treturn(ret);\n\t}', 'int ASN1_get_object(unsigned char **pp, long *plength, int *ptag, int *pclass,\n\t long omax)\n\t{\n\tint i,ret;\n\tlong l;\n\tunsigned char *p= *pp;\n\tint tag,xclass,inf;\n\tlong max=omax;\n\tif (!max) goto err;\n\tret=(*p&V_ASN1_CONSTRUCTED);\n\txclass=(*p&V_ASN1_PRIVATE);\n\ti= *p&V_ASN1_PRIMITIVE_TAG;\n\tif (i == V_ASN1_PRIMITIVE_TAG)\n\t\t{\n\t\tp++;\n\t\tif (--max == 0) goto err;\n\t\tl=0;\n\t\twhile (*p&0x80)\n\t\t\t{\n\t\t\tl<<=7L;\n\t\t\tl|= *(p++)&0x7f;\n\t\t\tif (--max == 0) goto err;\n\t\t\t}\n\t\tl<<=7L;\n\t\tl|= *(p++)&0x7f;\n\t\ttag=(int)l;\n\t\t}\n\telse\n\t\t{\n\t\ttag=i;\n\t\tp++;\n\t\tif (--max == 0) goto err;\n\t\t}\n\t*ptag=tag;\n\t*pclass=xclass;\n\tif (!asn1_get_length(&p,&inf,plength,(int)max)) goto err;\n#if 0\n\tfprintf(stderr,"p=%d + *plength=%ld > omax=%ld + *pp=%d (%d > %d)\\n",\n\t\t(int)p,*plength,omax,(int)*pp,(int)(p+ *plength),\n\t\t(int)(omax+ *pp));\n#endif\n#if 0\n\tif ((p+ *plength) > (omax+ *pp))\n\t\t{\n\t\tASN1err(ASN1_F_ASN1_GET_OBJECT,ASN1_R_TOO_LONG);\n\t\tret|=0x80;\n\t\t}\n#endif\n\t*pp=p;\n\treturn(ret|inf);\nerr:\n\tASN1err(ASN1_F_ASN1_GET_OBJECT,ASN1_R_HEADER_TOO_LONG);\n\treturn(0x80);\n\t}', 'static int asn1_print_info(BIO *bp, int tag, int xclass, int constructed,\n\t int indent)\n\t{\n\tstatic const char fmt[]="%-18s";\n\tstatic const char fmt2[]="%2d %-15s";\n\tchar str[128];\n\tconst char *p,*p2=NULL;\n\tif (constructed & V_ASN1_CONSTRUCTED)\n\t\tp="cons: ";\n\telse\n\t\tp="prim: ";\n\tif (BIO_write(bp,p,6) < 6) goto err;\n\tif (indent)\n\t\t{\n\t\tif (indent > 128) indent=128;\n\t\tmemset(str,\' \',indent);\n\t\tif (BIO_write(bp,str,indent) < indent) goto err;\n\t\t}\n\tp=str;\n\tif ((xclass & V_ASN1_PRIVATE) == V_ASN1_PRIVATE)\n\t\tsprintf(str,"priv [ %d ] ",tag);\n\telse if ((xclass & V_ASN1_CONTEXT_SPECIFIC) == V_ASN1_CONTEXT_SPECIFIC)\n\t\tsprintf(str,"cont [ %d ]",tag);\n\telse if ((xclass & V_ASN1_APPLICATION) == V_ASN1_APPLICATION)\n\t\tsprintf(str,"appl [ %d ]",tag);\n\telse p = ASN1_tag2str(tag);\n\tif (p2 != NULL)\n\t\t{\n\t\tif (BIO_printf(bp,fmt2,tag,p2) <= 0) goto err;\n\t\t}\n\telse\n\t\t{\n\t\tif (BIO_printf(bp,fmt,p) <= 0) goto err;\n\t\t}\n\treturn(1);\nerr:\n\treturn(0);\n\t}', 'const char *ASN1_tag2str(int tag)\n{\n\tconst static char *tag2str[] = {\n\t "EOC", "BOOLEAN", "INTEGER", "BIT STRING", "OCTET STRING",\n\t "NULL", "OBJECT", "OBJECT DESCRIPTOR", "EXTERNAL", "REAL",\n\t "ENUMERATED", "<ASN1 11>", "UTF8STRING", "<ASN1 13>",\n\t"<ASN1 14>", "<ASN1 15>", "SEQUENCE", "SET",\n\t"NUMERICSTRING", "PRINTABLESTRING", "T61STRING",\n\t"VIDEOTEXSTRING", "IA5STRING", "UTCTIME" "GENERALIZEDTIME",\n\t"GRAPHICSTRING", "VISIBLESTRING", "GENERALSTRING",\n\t"UNIVERSALSTRING", "<ASN1 29>", "BMPSTRING"\n\t};\n\tif((tag == V_ASN1_NEG_INTEGER) || (tag == V_ASN1_NEG_ENUMERATED))\n\t\t\t\t\t\t\ttag &= ~0x100;\n\tif(tag < 0 || tag > 30) return "(unknown)";\n\treturn tag2str[tag];\n}']
36,441
0
https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/h264pred.c/#L337
static void pred4x4_horizontal_up_c(uint8_t *src, uint8_t *topright, int stride){ LOAD_LEFT_EDGE src[0+0*stride]=(l0 + l1 + 1)>>1; src[1+0*stride]=(l0 + 2*l1 + l2 + 2)>>2; src[2+0*stride]= src[0+1*stride]=(l1 + l2 + 1)>>1; src[3+0*stride]= src[1+1*stride]=(l1 + 2*l2 + l3 + 2)>>2; src[2+1*stride]= src[0+2*stride]=(l2 + l3 + 1)>>1; src[3+1*stride]= src[1+2*stride]=(l2 + 2*l3 + l3 + 2)>>2; src[3+2*stride]= src[1+3*stride]= src[0+3*stride]= src[2+2*stride]= src[2+3*stride]= src[3+3*stride]=l3; }
['static void pred4x4_horizontal_up_c(uint8_t *src, uint8_t *topright, int stride){\n LOAD_LEFT_EDGE\n src[0+0*stride]=(l0 + l1 + 1)>>1;\n src[1+0*stride]=(l0 + 2*l1 + l2 + 2)>>2;\n src[2+0*stride]=\n src[0+1*stride]=(l1 + l2 + 1)>>1;\n src[3+0*stride]=\n src[1+1*stride]=(l1 + 2*l2 + l3 + 2)>>2;\n src[2+1*stride]=\n src[0+2*stride]=(l2 + l3 + 1)>>1;\n src[3+1*stride]=\n src[1+2*stride]=(l2 + 2*l3 + l3 + 2)>>2;\n src[3+2*stride]=\n src[1+3*stride]=\n src[0+3*stride]=\n src[2+2*stride]=\n src[2+3*stride]=\n src[3+3*stride]=l3;\n}']
36,442
0
https://github.com/openssl/openssl/blob/8de2910b5cbfa6169b54d1267abf654da773f00a/crypto/rsa/rsa_eay.c/#L300
static int RSA_eay_private_decrypt(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding) { const RSA_METHOD *meth; BIGNUM f,ret; int j,num=0,r= -1; unsigned char *p; unsigned char *buf=NULL; BN_CTX *ctx=NULL; meth = ENGINE_get_RSA(rsa->engine); BN_init(&f); BN_init(&ret); ctx=BN_CTX_new(); if (ctx == NULL) goto err; num=BN_num_bytes(rsa->n); if ((buf=(unsigned char *)OPENSSL_malloc(num)) == NULL) { RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,ERR_R_MALLOC_FAILURE); goto err; } if (flen > num) { RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN); goto err; } if (BN_bin2bn(from,(int)flen,&f) == NULL) goto err; if ((rsa->flags & RSA_FLAG_BLINDING) && (rsa->blinding == NULL)) RSA_blinding_on(rsa,ctx); if (rsa->flags & RSA_FLAG_BLINDING) if (!BN_BLINDING_convert(&f,rsa->blinding,ctx)) goto err; if ( (rsa->flags & RSA_FLAG_EXT_PKEY) || ((rsa->p != NULL) && (rsa->q != NULL) && (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL)) ) { if (!meth->rsa_mod_exp(&ret,&f,rsa)) goto err; } else { if (!meth->bn_mod_exp(&ret,&f,rsa->d,rsa->n,ctx,NULL)) goto err; } if (rsa->flags & RSA_FLAG_BLINDING) if (!BN_BLINDING_invert(&ret,rsa->blinding,ctx)) goto err; p=buf; j=BN_bn2bin(&ret,p); switch (padding) { case RSA_PKCS1_PADDING: r=RSA_padding_check_PKCS1_type_2(to,num,buf,j,num); break; #ifndef NO_SHA case RSA_PKCS1_OAEP_PADDING: r=RSA_padding_check_PKCS1_OAEP(to,num,buf,j,num,NULL,0); break; #endif case RSA_SSLV23_PADDING: r=RSA_padding_check_SSLv23(to,num,buf,j,num); break; case RSA_NO_PADDING: r=RSA_padding_check_none(to,num,buf,j,num); break; default: RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE); goto err; } if (r < 0) RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_PADDING_CHECK_FAILED); err: if (ctx != NULL) BN_CTX_free(ctx); BN_clear_free(&f); BN_clear_free(&ret); if (buf != NULL) { memset(buf,0,num); OPENSSL_free(buf); } return(r); }
['static int RSA_eay_private_decrypt(int flen, const unsigned char *from,\n\t unsigned char *to, RSA *rsa, int padding)\n\t{\n\tconst RSA_METHOD *meth;\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\tmeth = ENGINE_get_RSA(rsa->engine);\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 *)OPENSSL_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 ( (rsa->flags & RSA_FLAG_EXT_PKEY) ||\n\t\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 (!meth->rsa_mod_exp(&ret,&f,rsa)) goto err; }\n\telse\n\t\t{\n\t\tif (!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\tOPENSSL_free(buf);\n\t\t}\n\treturn(r);\n\t}', 'const RSA_METHOD *ENGINE_get_RSA(ENGINE *e)\n\t{\n\tif(e == NULL)\n\t\t{\n\t\tENGINEerr(ENGINE_F_ENGINE_GET_RSA,\n\t\t\tERR_R_PASSED_NULL_PARAMETER);\n\t\treturn NULL;\n\t\t}\n\treturn e->rsa_meth;\n\t}', 'void ERR_put_error(int lib, int func, int reason, const char *file,\n\t int line)\n\t{\n\tERR_STATE *es;\n#ifdef _OSD_POSIX\n\tif (strncmp(file,"*POSIX(", sizeof("*POSIX(")-1) == 0) {\n\t\tchar *end;\n\t\tfile += sizeof("*POSIX(")-1;\n\t\tend = &file[strlen(file)-1];\n\t\tif (*end == \')\')\n\t\t\t*end = \'\\0\';\n\t\tif ((end = strrchr(file, \'/\')) != NULL)\n\t\t\tfile = &end[1];\n\t}\n#endif\n\tes=ERR_get_state();\n\tes->top=(es->top+1)%ERR_NUM_ERRORS;\n\tif (es->top == es->bottom)\n\t\tes->bottom=(es->bottom+1)%ERR_NUM_ERRORS;\n\tes->err_buffer[es->top]=ERR_PACK(lib,func,reason);\n\tes->err_file[es->top]=file;\n\tes->err_line[es->top]=line;\n\terr_clear_data(es,es->top);\n\t}', 'void BN_init(BIGNUM *a)\n\t{\n\tmemset(a,0,sizeof(BIGNUM));\n\t}', 'BN_CTX *BN_CTX_new(void)\n\t{\n\tBN_CTX *ret;\n\tret=(BN_CTX *)OPENSSL_malloc(sizeof(BN_CTX));\n\tif (ret == NULL)\n\t\t{\n\t\tBNerr(BN_F_BN_CTX_NEW,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tBN_CTX_init(ret);\n\tret->flags=BN_FLG_MALLOCED;\n\treturn(ret);\n\t}', 'void *CRYPTO_malloc(int num, const char *file, int line)\n\t{\n\tvoid *ret = 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_func(num);\n#ifdef LEVITTE_DEBUG\n\tfprintf(stderr, "LEVITTE_DEBUG: > 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\treturn ret;\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}']
36,443
0
https://github.com/openssl/openssl/blob/00fe865dbec8fb626a63ed9f5b0be4073597c7a8/crypto/lhash/lhash.c/#L278
static void doall_util_fn(LHASH *lh, int use_arg, LHASH_DOALL_FN_TYPE func, LHASH_DOALL_ARG_FN_TYPE func_arg, void *arg) { int i; LHASH_NODE *a,*n; for (i=lh->num_nodes-1; i>=0; i--) { a=lh->b[i]; while (a != NULL) { n=a->next; if(use_arg) func_arg(a->data,arg); else func(a->data); a=n; } } }
['void SSL_free(SSL *s)\n\t{\n\tint i;\n\tif(s == NULL)\n\t return;\n\ti=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL);\n#ifdef REF_PRINT\n\tREF_PRINT("SSL",s);\n#endif\n\tif (i > 0) return;\n#ifdef REF_CHECK\n\tif (i < 0)\n\t\t{\n\t\tfprintf(stderr,"SSL_free, bad reference count\\n");\n\t\tabort();\n\t\t}\n#endif\n\tif (s->param)\n\t\tX509_VERIFY_PARAM_free(s->param);\n\tCRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);\n\tif (s->bbio != NULL)\n\t\t{\n\t\tif (s->bbio == s->wbio)\n\t\t\t{\n\t\t\ts->wbio=BIO_pop(s->wbio);\n\t\t\t}\n\t\tBIO_free(s->bbio);\n\t\ts->bbio=NULL;\n\t\t}\n\tif (s->rbio != NULL)\n\t\tBIO_free_all(s->rbio);\n\tif ((s->wbio != NULL) && (s->wbio != s->rbio))\n\t\tBIO_free_all(s->wbio);\n\tif (s->init_buf != NULL) BUF_MEM_free(s->init_buf);\n\tif (s->cipher_list != NULL) sk_SSL_CIPHER_free(s->cipher_list);\n\tif (s->cipher_list_by_id != NULL) sk_SSL_CIPHER_free(s->cipher_list_by_id);\n\tif (s->session != NULL)\n\t\t{\n\t\tssl_clear_bad_session(s);\n\t\tSSL_SESSION_free(s->session);\n\t\t}\n\tssl_clear_cipher_ctx(s);\n\tif (s->cert != NULL) ssl_cert_free(s->cert);\n\tif (s->ctx) SSL_CTX_free(s->ctx);\n#ifndef OPENSSL_NO_TLSEXT\n\tif (s->initial_ctx) SSL_CTX_free(s->initial_ctx);\n#endif\n\tif (s->client_CA != NULL)\n\t\tsk_X509_NAME_pop_free(s->client_CA,X509_NAME_free);\n\tif (s->method != NULL) s->method->ssl_free(s);\n#ifndef\tOPENSSL_NO_KRB5\n\tif (s->kssl_ctx != NULL)\n\t\tkssl_ctx_free(s->kssl_ctx);\n#endif\n\tOPENSSL_free(s);\n\t}', 'void SSL_CTX_free(SSL_CTX *a)\n\t{\n\tint i;\n\tif (a == NULL) return;\n\ti=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX);\n#ifdef REF_PRINT\n\tREF_PRINT("SSL_CTX",a);\n#endif\n\tif (i > 0) return;\n#ifdef REF_CHECK\n\tif (i < 0)\n\t\t{\n\t\tfprintf(stderr,"SSL_CTX_free, bad reference count\\n");\n\t\tabort();\n\t\t}\n#endif\n\tif (a->param)\n\t\tX509_VERIFY_PARAM_free(a->param);\n\tif (a->sessions != NULL)\n\t\tSSL_CTX_flush_sessions(a,0);\n\tCRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);\n\tif (a->sessions != NULL)\n\t\tlh_free(a->sessions);\n\tif (a->cert_store != NULL)\n\t\tX509_STORE_free(a->cert_store);\n\tif (a->cipher_list != NULL)\n\t\tsk_SSL_CIPHER_free(a->cipher_list);\n\tif (a->cipher_list_by_id != NULL)\n\t\tsk_SSL_CIPHER_free(a->cipher_list_by_id);\n\tif (a->cert != NULL)\n\t\tssl_cert_free(a->cert);\n\tif (a->client_CA != NULL)\n\t\tsk_X509_NAME_pop_free(a->client_CA,X509_NAME_free);\n\tif (a->extra_certs != NULL)\n\t\tsk_X509_pop_free(a->extra_certs,X509_free);\n#if 0\n\tif (a->comp_methods != NULL)\n\t\tsk_SSL_COMP_pop_free(a->comp_methods,SSL_COMP_free);\n#else\n\ta->comp_methods = NULL;\n#endif\n\tOPENSSL_free(a);\n\t}', 'void SSL_CTX_flush_sessions(SSL_CTX *s, long t)\n\t{\n\tunsigned long i;\n\tTIMEOUT_PARAM tp;\n\ttp.ctx=s;\n\ttp.cache=s->sessions;\n\tif (tp.cache == NULL) return;\n\ttp.time=t;\n\tCRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);\n\ti=tp.cache->down_load;\n\ttp.cache->down_load=0;\n\tlh_doall_arg(tp.cache, LHASH_DOALL_ARG_FN(timeout), &tp);\n\ttp.cache->down_load=i;\n\tCRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n\t}', 'void lh_doall_arg(LHASH *lh, LHASH_DOALL_ARG_FN_TYPE func, void *arg)\n\t{\n\tdoall_util_fn(lh, 1, (LHASH_DOALL_FN_TYPE)0, func, arg);\n\t}', 'static void doall_util_fn(LHASH *lh, int use_arg, LHASH_DOALL_FN_TYPE func,\n\t\t\t LHASH_DOALL_ARG_FN_TYPE func_arg, void *arg)\n\t{\n\tint i;\n\tLHASH_NODE *a,*n;\n\tfor (i=lh->num_nodes-1; i>=0; i--)\n\t\t{\n\t\ta=lh->b[i];\n\t\twhile (a != NULL)\n\t\t\t{\n\t\t\tn=a->next;\n\t\t\tif(use_arg)\n\t\t\t\tfunc_arg(a->data,arg);\n\t\t\telse\n\t\t\t\tfunc(a->data);\n\t\t\ta=n;\n\t\t\t}\n\t\t}\n\t}']
36,444
0
https://github.com/openssl/openssl/blob/8fa6a40be2935ca109a28cc43d28cd27051ada01/crypto/evp/evp_pbe.c/#L129
int EVP_PBE_alg_add(int nid, const EVP_CIPHER *cipher, const EVP_MD *md, EVP_PBE_KEYGEN *keygen) { EVP_PBE_CTL *pbe_tmp; if (!pbe_algs) pbe_algs = sk_new(pbe_cmp); if (!(pbe_tmp = (EVP_PBE_CTL*) OPENSSL_malloc (sizeof(EVP_PBE_CTL)))) { EVPerr(EVP_F_EVP_PBE_ALG_ADD,ERR_R_MALLOC_FAILURE); return 0; } pbe_tmp->pbe_nid = nid; pbe_tmp->cipher = cipher; pbe_tmp->md = md; pbe_tmp->keygen = keygen; sk_push (pbe_algs, (char *)pbe_tmp); return 1; }
['int EVP_PBE_alg_add(int nid, const EVP_CIPHER *cipher, const EVP_MD *md,\n\t EVP_PBE_KEYGEN *keygen)\n{\n\tEVP_PBE_CTL *pbe_tmp;\n\tif (!pbe_algs) pbe_algs = sk_new(pbe_cmp);\n\tif (!(pbe_tmp = (EVP_PBE_CTL*) OPENSSL_malloc (sizeof(EVP_PBE_CTL)))) {\n\t\tEVPerr(EVP_F_EVP_PBE_ALG_ADD,ERR_R_MALLOC_FAILURE);\n\t\treturn 0;\n\t}\n\tpbe_tmp->pbe_nid = nid;\n\tpbe_tmp->cipher = cipher;\n\tpbe_tmp->md = md;\n\tpbe_tmp->keygen = keygen;\n\tsk_push (pbe_algs, (char *)pbe_tmp);\n\treturn 1;\n}', 'STACK *sk_new(int (*c)(const char * const *, const char * const *))\n\t{\n\tSTACK *ret;\n\tint i;\n\tif ((ret=(STACK *)OPENSSL_malloc(sizeof(STACK))) == NULL)\n\t\tgoto err;\n\tif ((ret->data=(char **)OPENSSL_malloc(sizeof(char *)*MIN_NODES)) == NULL)\n\t\tgoto err;\n\tfor (i=0; i<MIN_NODES; i++)\n\t\tret->data[i]=NULL;\n\tret->comp=c;\n\tret->num_alloc=MIN_NODES;\n\tret->num=0;\n\tret->sorted=0;\n\treturn(ret);\nerr:\n\tif(ret)\n\t\tOPENSSL_free(ret);\n\treturn(NULL);\n\t}', 'void *CRYPTO_malloc(int num, const char *file, int line)\n\t{\n\tvoid *ret = NULL;\n\textern unsigned char cleanse_ctr;\n\tif (num <= 0) return NULL;\n\tallow_customize = 0;\n\tif (malloc_debug_func != NULL)\n\t\t{\n\t\tallow_customize_debug = 0;\n\t\tmalloc_debug_func(NULL, num, file, line, 0);\n\t\t}\n\tret = malloc_ex_func(num,file,line);\n#ifdef LEVITTE_DEBUG_MEM\n\tfprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\\n", ret, num);\n#endif\n\tif (malloc_debug_func != NULL)\n\t\tmalloc_debug_func(ret, num, file, line, 1);\n if(ret && (num > 2048))\n ((unsigned char *)ret)[0] = cleanse_ctr;\n\treturn ret;\n\t}', 'int sk_push(STACK *st, char *data)\n\t{\n\treturn(sk_insert(st,data,st->num));\n\t}']
36,445
0
https://github.com/nginx/nginx/blob/70f7141074896fb1ff3e5fc08407ea0f64f2076b/src/core/ngx_sha1.c/#L246
static const u_char * ngx_sha1_body(ngx_sha1_t *ctx, const u_char *data, size_t size) { uint32_t a, b, c, d, e, temp; uint32_t saved_a, saved_b, saved_c, saved_d, saved_e; uint32_t words[80]; ngx_uint_t i; const u_char *p; p = data; a = ctx->a; b = ctx->b; c = ctx->c; d = ctx->d; e = ctx->e; do { saved_a = a; saved_b = b; saved_c = c; saved_d = d; saved_e = e; for (i = 0; i < 16; i++) { words[i] = GET(i); } for (i = 16; i < 80; i++) { words[i] = ROTATE(1, words[i - 3] ^ words[i - 8] ^ words[i - 14] ^ words[i - 16]); } STEP(F1, a, b, c, d, e, words[0], 0x5a827999); STEP(F1, a, b, c, d, e, words[1], 0x5a827999); STEP(F1, a, b, c, d, e, words[2], 0x5a827999); STEP(F1, a, b, c, d, e, words[3], 0x5a827999); STEP(F1, a, b, c, d, e, words[4], 0x5a827999); STEP(F1, a, b, c, d, e, words[5], 0x5a827999); STEP(F1, a, b, c, d, e, words[6], 0x5a827999); STEP(F1, a, b, c, d, e, words[7], 0x5a827999); STEP(F1, a, b, c, d, e, words[8], 0x5a827999); STEP(F1, a, b, c, d, e, words[9], 0x5a827999); STEP(F1, a, b, c, d, e, words[10], 0x5a827999); STEP(F1, a, b, c, d, e, words[11], 0x5a827999); STEP(F1, a, b, c, d, e, words[12], 0x5a827999); STEP(F1, a, b, c, d, e, words[13], 0x5a827999); STEP(F1, a, b, c, d, e, words[14], 0x5a827999); STEP(F1, a, b, c, d, e, words[15], 0x5a827999); STEP(F1, a, b, c, d, e, words[16], 0x5a827999); STEP(F1, a, b, c, d, e, words[17], 0x5a827999); STEP(F1, a, b, c, d, e, words[18], 0x5a827999); STEP(F1, a, b, c, d, e, words[19], 0x5a827999); STEP(F2, a, b, c, d, e, words[20], 0x6ed9eba1); STEP(F2, a, b, c, d, e, words[21], 0x6ed9eba1); STEP(F2, a, b, c, d, e, words[22], 0x6ed9eba1); STEP(F2, a, b, c, d, e, words[23], 0x6ed9eba1); STEP(F2, a, b, c, d, e, words[24], 0x6ed9eba1); STEP(F2, a, b, c, d, e, words[25], 0x6ed9eba1); STEP(F2, a, b, c, d, e, words[26], 0x6ed9eba1); STEP(F2, a, b, c, d, e, words[27], 0x6ed9eba1); STEP(F2, a, b, c, d, e, words[28], 0x6ed9eba1); STEP(F2, a, b, c, d, e, words[29], 0x6ed9eba1); STEP(F2, a, b, c, d, e, words[30], 0x6ed9eba1); STEP(F2, a, b, c, d, e, words[31], 0x6ed9eba1); STEP(F2, a, b, c, d, e, words[32], 0x6ed9eba1); STEP(F2, a, b, c, d, e, words[33], 0x6ed9eba1); STEP(F2, a, b, c, d, e, words[34], 0x6ed9eba1); STEP(F2, a, b, c, d, e, words[35], 0x6ed9eba1); STEP(F2, a, b, c, d, e, words[36], 0x6ed9eba1); STEP(F2, a, b, c, d, e, words[37], 0x6ed9eba1); STEP(F2, a, b, c, d, e, words[38], 0x6ed9eba1); STEP(F2, a, b, c, d, e, words[39], 0x6ed9eba1); STEP(F3, a, b, c, d, e, words[40], 0x8f1bbcdc); STEP(F3, a, b, c, d, e, words[41], 0x8f1bbcdc); STEP(F3, a, b, c, d, e, words[42], 0x8f1bbcdc); STEP(F3, a, b, c, d, e, words[43], 0x8f1bbcdc); STEP(F3, a, b, c, d, e, words[44], 0x8f1bbcdc); STEP(F3, a, b, c, d, e, words[45], 0x8f1bbcdc); STEP(F3, a, b, c, d, e, words[46], 0x8f1bbcdc); STEP(F3, a, b, c, d, e, words[47], 0x8f1bbcdc); STEP(F3, a, b, c, d, e, words[48], 0x8f1bbcdc); STEP(F3, a, b, c, d, e, words[49], 0x8f1bbcdc); STEP(F3, a, b, c, d, e, words[50], 0x8f1bbcdc); STEP(F3, a, b, c, d, e, words[51], 0x8f1bbcdc); STEP(F3, a, b, c, d, e, words[52], 0x8f1bbcdc); STEP(F3, a, b, c, d, e, words[53], 0x8f1bbcdc); STEP(F3, a, b, c, d, e, words[54], 0x8f1bbcdc); STEP(F3, a, b, c, d, e, words[55], 0x8f1bbcdc); STEP(F3, a, b, c, d, e, words[56], 0x8f1bbcdc); STEP(F3, a, b, c, d, e, words[57], 0x8f1bbcdc); STEP(F3, a, b, c, d, e, words[58], 0x8f1bbcdc); STEP(F3, a, b, c, d, e, words[59], 0x8f1bbcdc); STEP(F2, a, b, c, d, e, words[60], 0xca62c1d6); STEP(F2, a, b, c, d, e, words[61], 0xca62c1d6); STEP(F2, a, b, c, d, e, words[62], 0xca62c1d6); STEP(F2, a, b, c, d, e, words[63], 0xca62c1d6); STEP(F2, a, b, c, d, e, words[64], 0xca62c1d6); STEP(F2, a, b, c, d, e, words[65], 0xca62c1d6); STEP(F2, a, b, c, d, e, words[66], 0xca62c1d6); STEP(F2, a, b, c, d, e, words[67], 0xca62c1d6); STEP(F2, a, b, c, d, e, words[68], 0xca62c1d6); STEP(F2, a, b, c, d, e, words[69], 0xca62c1d6); STEP(F2, a, b, c, d, e, words[70], 0xca62c1d6); STEP(F2, a, b, c, d, e, words[71], 0xca62c1d6); STEP(F2, a, b, c, d, e, words[72], 0xca62c1d6); STEP(F2, a, b, c, d, e, words[73], 0xca62c1d6); STEP(F2, a, b, c, d, e, words[74], 0xca62c1d6); STEP(F2, a, b, c, d, e, words[75], 0xca62c1d6); STEP(F2, a, b, c, d, e, words[76], 0xca62c1d6); STEP(F2, a, b, c, d, e, words[77], 0xca62c1d6); STEP(F2, a, b, c, d, e, words[78], 0xca62c1d6); STEP(F2, a, b, c, d, e, words[79], 0xca62c1d6); a += saved_a; b += saved_b; c += saved_c; d += saved_d; e += saved_e; p += 64; } while (size -= 64); ctx->a = a; ctx->b = b; ctx->c = c; ctx->d = d; ctx->e = e; return p; }
['static const u_char *\nngx_sha1_body(ngx_sha1_t *ctx, const u_char *data, size_t size)\n{\n uint32_t a, b, c, d, e, temp;\n uint32_t saved_a, saved_b, saved_c, saved_d, saved_e;\n uint32_t words[80];\n ngx_uint_t i;\n const u_char *p;\n p = data;\n a = ctx->a;\n b = ctx->b;\n c = ctx->c;\n d = ctx->d;\n e = ctx->e;\n do {\n saved_a = a;\n saved_b = b;\n saved_c = c;\n saved_d = d;\n saved_e = e;\n for (i = 0; i < 16; i++) {\n words[i] = GET(i);\n }\n for (i = 16; i < 80; i++) {\n words[i] = ROTATE(1, words[i - 3] ^ words[i - 8] ^ words[i - 14]\n ^ words[i - 16]);\n }\n STEP(F1, a, b, c, d, e, words[0], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[1], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[2], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[3], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[4], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[5], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[6], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[7], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[8], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[9], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[10], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[11], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[12], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[13], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[14], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[15], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[16], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[17], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[18], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[19], 0x5a827999);\n STEP(F2, a, b, c, d, e, words[20], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[21], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[22], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[23], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[24], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[25], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[26], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[27], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[28], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[29], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[30], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[31], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[32], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[33], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[34], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[35], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[36], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[37], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[38], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[39], 0x6ed9eba1);\n STEP(F3, a, b, c, d, e, words[40], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[41], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[42], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[43], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[44], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[45], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[46], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[47], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[48], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[49], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[50], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[51], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[52], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[53], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[54], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[55], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[56], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[57], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[58], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[59], 0x8f1bbcdc);\n STEP(F2, a, b, c, d, e, words[60], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[61], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[62], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[63], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[64], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[65], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[66], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[67], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[68], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[69], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[70], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[71], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[72], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[73], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[74], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[75], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[76], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[77], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[78], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[79], 0xca62c1d6);\n a += saved_a;\n b += saved_b;\n c += saved_c;\n d += saved_d;\n e += saved_e;\n p += 64;\n } while (size -= 64);\n ctx->a = a;\n ctx->b = b;\n ctx->c = c;\n ctx->d = d;\n ctx->e = e;\n return p;\n}']
36,446
0
https://github.com/openssl/openssl/blob/b90506e995d44dee0ef4dd0324b56b59154256c2/ssl/packet.c/#L46
int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes) { assert(pkt->subs != NULL && len != 0); if (pkt->subs == NULL || len == 0) return 0; if (pkt->maxsize - pkt->written < len) return 0; if (pkt->buf->length - pkt->written < len) { size_t newlen; size_t reflen; reflen = (len > pkt->buf->length) ? len : pkt->buf->length; if (reflen > SIZE_MAX / 2) { newlen = SIZE_MAX; } else { newlen = reflen * 2; if (newlen < DEFAULT_BUF_SIZE) newlen = DEFAULT_BUF_SIZE; } if (BUF_MEM_grow(pkt->buf, newlen) == 0) return 0; } *allocbytes = (unsigned char *)pkt->buf->data + pkt->curr; return 1; }
['int tls_construct_next_proto(SSL *s, WPACKET *pkt)\n{\n size_t len, padding_len;\n unsigned char *padding = NULL;\n len = s->next_proto_negotiated_len;\n padding_len = 32 - ((len + 2) % 32);\n if (!WPACKET_sub_memcpy_u8(pkt, s->next_proto_negotiated, len)\n || !WPACKET_sub_allocate_bytes_u8(pkt, padding_len, &padding)) {\n SSLerr(SSL_F_TLS_CONSTRUCT_NEXT_PROTO, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n memset(padding, 0, padding_len);\n return 1;\n err:\n ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);\n return 0;\n}', 'int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,\n size_t lenbytes)\n{\n if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)\n || !WPACKET_memcpy(pkt, src, len)\n || !WPACKET_close(pkt))\n return 0;\n return 1;\n}', 'int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)\n{\n WPACKET_SUB *sub;\n unsigned char *lenchars;\n assert(pkt->subs != NULL);\n if (pkt->subs == NULL)\n return 0;\n sub = OPENSSL_zalloc(sizeof(*sub));\n if (sub == NULL)\n return 0;\n sub->parent = pkt->subs;\n pkt->subs = sub;\n sub->pwritten = pkt->written + lenbytes;\n sub->lenbytes = lenbytes;\n if (lenbytes == 0) {\n sub->packet_len = 0;\n return 1;\n }\n if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))\n return 0;\n sub->packet_len = lenchars - (unsigned char *)pkt->buf->data;\n return 1;\n}', 'int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)\n{\n unsigned char *dest;\n if (len == 0)\n return 1;\n if (!WPACKET_allocate_bytes(pkt, len, &dest))\n return 0;\n memcpy(dest, src, len);\n return 1;\n}', 'int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)\n{\n if (!WPACKET_reserve_bytes(pkt, len, allocbytes))\n return 0;\n pkt->written += len;\n pkt->curr += len;\n return 1;\n}', 'int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)\n{\n assert(pkt->subs != NULL && len != 0);\n if (pkt->subs == NULL || len == 0)\n return 0;\n if (pkt->maxsize - pkt->written < len)\n return 0;\n if (pkt->buf->length - pkt->written < len) {\n size_t newlen;\n size_t reflen;\n reflen = (len > pkt->buf->length) ? len : pkt->buf->length;\n if (reflen > SIZE_MAX / 2) {\n newlen = SIZE_MAX;\n } else {\n newlen = reflen * 2;\n if (newlen < DEFAULT_BUF_SIZE)\n newlen = DEFAULT_BUF_SIZE;\n }\n if (BUF_MEM_grow(pkt->buf, newlen) == 0)\n return 0;\n }\n *allocbytes = (unsigned char *)pkt->buf->data + pkt->curr;\n return 1;\n}']
36,447
0
https://github.com/openssl/openssl/blob/555cbb328ee2eaa9356cd23e2194c1600653c500/ssl/packet.c/#L49
int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes) { if (!ossl_assert(pkt->subs != NULL && len != 0)) return 0; if (pkt->maxsize - pkt->written < len) return 0; if (pkt->staticbuf == NULL && (pkt->buf->length - pkt->written < len)) { size_t newlen; size_t reflen; reflen = (len > pkt->buf->length) ? len : pkt->buf->length; if (reflen > SIZE_MAX / 2) { newlen = SIZE_MAX; } else { newlen = reflen * 2; if (newlen < DEFAULT_BUF_SIZE) newlen = DEFAULT_BUF_SIZE; } if (BUF_MEM_grow(pkt->buf, newlen) == 0) return 0; } if (allocbytes != NULL) *allocbytes = WPACKET_get_curr(pkt); return 1; }
['int tls_construct_server_certificate(SSL *s, WPACKET *pkt)\n{\n CERT_PKEY *cpk = s->s3.tmp.cert;\n if (cpk == NULL) {\n SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n if (SSL_IS_TLS13(s) && !WPACKET_put_bytes_u8(pkt, 0)) {\n SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n if (!ssl3_output_cert_chain(s, pkt, cpk)) {\n return 0;\n }\n return 1;\n}', 'unsigned long ssl3_output_cert_chain(SSL *s, WPACKET *pkt, CERT_PKEY *cpk)\n{\n if (!WPACKET_start_sub_packet_u24(pkt)) {\n SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_OUTPUT_CERT_CHAIN,\n ERR_R_INTERNAL_ERROR);\n return 0;\n }\n if (!ssl_add_cert_chain(s, pkt, cpk))\n return 0;\n if (!WPACKET_close(pkt)) {\n SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_OUTPUT_CERT_CHAIN,\n ERR_R_INTERNAL_ERROR);\n return 0;\n }\n return 1;\n}', 'int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)\n{\n WPACKET_SUB *sub;\n unsigned char *lenchars;\n if (!ossl_assert(pkt->subs != NULL))\n return 0;\n if ((sub = OPENSSL_zalloc(sizeof(*sub))) == NULL) {\n SSLerr(SSL_F_WPACKET_START_SUB_PACKET_LEN__, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n sub->parent = pkt->subs;\n pkt->subs = sub;\n sub->pwritten = pkt->written + lenbytes;\n sub->lenbytes = lenbytes;\n if (lenbytes == 0) {\n sub->packet_len = 0;\n return 1;\n }\n if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))\n return 0;\n sub->packet_len = lenchars - GETBUF(pkt);\n return 1;\n}', 'int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)\n{\n if (!WPACKET_reserve_bytes(pkt, len, allocbytes))\n return 0;\n pkt->written += len;\n pkt->curr += len;\n return 1;\n}', 'int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)\n{\n if (!ossl_assert(pkt->subs != NULL && len != 0))\n return 0;\n if (pkt->maxsize - pkt->written < len)\n return 0;\n if (pkt->staticbuf == NULL && (pkt->buf->length - pkt->written < len)) {\n size_t newlen;\n size_t reflen;\n reflen = (len > pkt->buf->length) ? len : pkt->buf->length;\n if (reflen > SIZE_MAX / 2) {\n newlen = SIZE_MAX;\n } else {\n newlen = reflen * 2;\n if (newlen < DEFAULT_BUF_SIZE)\n newlen = DEFAULT_BUF_SIZE;\n }\n if (BUF_MEM_grow(pkt->buf, newlen) == 0)\n return 0;\n }\n if (allocbytes != NULL)\n *allocbytes = WPACKET_get_curr(pkt);\n return 1;\n}']
36,448
0
https://github.com/openssl/openssl/blob/11af1a2758baefab1157755c508d7b7490155bab/crypto/sha/sha1dgst.c/#L148
void SHA1_Update(SHA_CTX *c, register const unsigned char *data, unsigned long len) { register SHA_LONG *p; int ew,ec,sw,sc; SHA_LONG l; if (len == 0) return; l=(c->Nl+(len<<3))&0xffffffffL; if (l < c->Nl) c->Nh++; c->Nh+=(len>>29); c->Nl=l; if (c->num != 0) { p=c->data; sw=c->num>>2; sc=c->num&0x03; if ((c->num+len) >= SHA_CBLOCK) { l= p[sw]; M_p_c2nl(data,l,sc); p[sw++]=l; for (; sw<SHA_LBLOCK; sw++) { M_c2nl(data,l); p[sw]=l; } len-=(SHA_CBLOCK-c->num); sha1_block(c,p,1); c->num=0; } else { c->num+=(int)len; if ((sc+len) < 4) { l= p[sw]; M_p_c2nl_p(data,l,sc,len); p[sw]=l; } else { ew=(c->num>>2); ec=(c->num&0x03); l= p[sw]; M_p_c2nl(data,l,sc); p[sw++]=l; for (; sw < ew; sw++) { M_c2nl(data,l); p[sw]=l; } if (ec) { M_c2nl_p(data,l,ec); p[sw]=l; } } return; } } #if SHA_LONG_LOG2==2 #if defined(B_ENDIAN) || defined(SHA1_ASM) if ((((unsigned long)data)%sizeof(SHA_LONG)) == 0) { sw=len/SHA_CBLOCK; if (sw) { sha1_block(c,(SHA_LONG *)data,sw); sw*=SHA_CBLOCK; data+=sw; len-=sw; } } #endif #endif p=c->data; while (len >= SHA_CBLOCK) { #if SHA_LONG_LOG2==2 #if defined(B_ENDIAN) || defined(SHA1_ASM) #define SHA_NO_TAIL_CODE if (p != (SHA_LONG *)data) memcpy(p,data,SHA_CBLOCK); data+=SHA_CBLOCK; sha1_block(c,p=c->data,1); len-=SHA_CBLOCK; #elif defined(L_ENDIAN) #define BE_COPY(dst,src,i) { \ l = ((SHA_LONG *)src)[i]; \ Endian_Reverse32(l); \ dst[i] = l; \ } if ((((unsigned long)data)%sizeof(SHA_LONG)) == 0) { for (sw=(SHA_LBLOCK/4); sw; sw--) { BE_COPY(p,data,0); BE_COPY(p,data,1); BE_COPY(p,data,2); BE_COPY(p,data,3); p+=4; data += 4*sizeof(SHA_LONG); } sha1_block(c,p=c->data,1); len-=SHA_CBLOCK; continue; } #endif #endif #ifndef SHA_NO_TAIL_CODE p=c->data; for (sw=(SHA_LBLOCK/4); sw; sw--) { M_c2nl(data,l); p[0]=l; M_c2nl(data,l); p[1]=l; M_c2nl(data,l); p[2]=l; M_c2nl(data,l); p[3]=l; p+=4; } p=c->data; sha1_block(c,p,1); len-=SHA_CBLOCK; #endif } ec=(int)len; c->num=ec; ew=(ec>>2); ec&=0x03; for (sw=0; sw < ew; sw++) { M_c2nl(data,l); p[sw]=l; } M_c2nl_p(data,l,ec); p[sw]=l; }
['static void ssleay_rand_bytes(unsigned char *buf, int num)\n\t{\n\tint i,j,k,st_num,st_idx;\n\tMD_CTX m;\n\tstatic int init=1;\n\tunsigned long l;\n#ifdef DEVRANDOM\n\tFILE *fh;\n#endif\n#ifdef PREDICT\n\t{\n\tstatic unsigned char val=0;\n\tfor (i=0; i<num; i++)\n\t\tbuf[i]=val++;\n\treturn;\n\t}\n#endif\n\tCRYPTO_w_lock(CRYPTO_LOCK_RAND);\n\tif (init)\n\t\t{\n\t\tCRYPTO_w_unlock(CRYPTO_LOCK_RAND);\n\t\tRAND_seed(&m,sizeof(m));\n#ifndef MSDOS\n\t\tl=getpid();\n\t\tRAND_seed(&l,sizeof(l));\n\t\tl=getuid();\n\t\tRAND_seed(&l,sizeof(l));\n#endif\n\t\tl=time(NULL);\n\t\tRAND_seed(&l,sizeof(l));\n\t\tif ((fh = fopen(DEVRANDOM, "r")) != NULL)\n\t\t\t{\n\t\t\tunsigned char tmpbuf[32];\n\t\t\tfread((unsigned char *)tmpbuf,1,32,fh);\n\t\t\tfclose(fh);\n\t\t\tRAND_seed(tmpbuf,32);\n\t\t\tmemset(tmpbuf,0,32);\n\t\t\t}\n#ifdef PURIFY\n\t\tmemset(state,0,STATE_SIZE);\n\t\tmemset(md,0,MD_DIGEST_LENGTH);\n#endif\n\t\tCRYPTO_w_lock(CRYPTO_LOCK_RAND);\n\t\tinit=0;\n\t\t}\n\tst_idx=state_index;\n\tst_num=state_num;\n\tstate_index+=num;\n\tif (state_index > state_num)\n\t\tstate_index=(state_index%state_num);\n\tCRYPTO_w_unlock(CRYPTO_LOCK_RAND);\n\twhile (num > 0)\n\t\t{\n\t\tj=(num >= MD_DIGEST_LENGTH/2)?MD_DIGEST_LENGTH/2:num;\n\t\tnum-=j;\n\t\tMD_Init(&m);\n\t\tMD_Update(&m,&(md[MD_DIGEST_LENGTH/2]),MD_DIGEST_LENGTH/2);\n\t\tMD_Update(&m,(unsigned char *)&(md_count[0]),sizeof(md_count));\n#ifndef PURIFY\n\t\tMD_Update(&m,buf,j);\n#endif\n\t\tk=(st_idx+j)-st_num;\n\t\tif (k > 0)\n\t\t\t{\n\t\t\tMD_Update(&m,&(state[st_idx]),j-k);\n\t\t\tMD_Update(&m,&(state[0]),k);\n\t\t\t}\n\t\telse\n\t\t\tMD_Update(&m,&(state[st_idx]),j);\n\t\tMD_Final(md,&m);\n\t\tfor (i=0; i<j; i++)\n\t\t\t{\n\t\t\tif (st_idx >= st_num)\n\t\t\t\tst_idx=0;\n\t\t\tstate[st_idx++]^=md[i];\n\t\t\t*(buf++)=md[i+MD_DIGEST_LENGTH/2];\n\t\t\t}\n\t\t}\n\tMD_Init(&m);\n\tMD_Update(&m,(unsigned char *)&(md_count[0]),sizeof(md_count));\n\tmd_count[0]++;\n\tMD_Update(&m,md,MD_DIGEST_LENGTH);\n\tMD_Final(md,&m);\n\tmemset(&m,0,sizeof(m));\n\t}', 'void SHA1_Init(SHA_CTX *c)\n\t{\n\tc->h0=INIT_DATA_h0;\n\tc->h1=INIT_DATA_h1;\n\tc->h2=INIT_DATA_h2;\n\tc->h3=INIT_DATA_h3;\n\tc->h4=INIT_DATA_h4;\n\tc->Nl=0;\n\tc->Nh=0;\n\tc->num=0;\n\t}', 'void SHA1_Update(SHA_CTX *c, register const unsigned char *data,\n\t unsigned long len)\n\t{\n\tregister SHA_LONG *p;\n\tint ew,ec,sw,sc;\n\tSHA_LONG l;\n\tif (len == 0) return;\n\tl=(c->Nl+(len<<3))&0xffffffffL;\n\tif (l < c->Nl)\n\t\tc->Nh++;\n\tc->Nh+=(len>>29);\n\tc->Nl=l;\n\tif (c->num != 0)\n\t\t{\n\t\tp=c->data;\n\t\tsw=c->num>>2;\n\t\tsc=c->num&0x03;\n\t\tif ((c->num+len) >= SHA_CBLOCK)\n\t\t\t{\n\t\t\tl= p[sw];\n\t\t\tM_p_c2nl(data,l,sc);\n\t\t\tp[sw++]=l;\n\t\t\tfor (; sw<SHA_LBLOCK; sw++)\n\t\t\t\t{\n\t\t\t\tM_c2nl(data,l);\n\t\t\t\tp[sw]=l;\n\t\t\t\t}\n\t\t\tlen-=(SHA_CBLOCK-c->num);\n\t\t\tsha1_block(c,p,1);\n\t\t\tc->num=0;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tc->num+=(int)len;\n\t\t\tif ((sc+len) < 4)\n\t\t\t\t{\n\t\t\t\tl= p[sw];\n\t\t\t\tM_p_c2nl_p(data,l,sc,len);\n\t\t\t\tp[sw]=l;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tew=(c->num>>2);\n\t\t\t\tec=(c->num&0x03);\n\t\t\t\tl= p[sw];\n\t\t\t\tM_p_c2nl(data,l,sc);\n\t\t\t\tp[sw++]=l;\n\t\t\t\tfor (; sw < ew; sw++)\n\t\t\t\t\t{ M_c2nl(data,l); p[sw]=l; }\n\t\t\t\tif (ec)\n\t\t\t\t\t{\n\t\t\t\t\tM_c2nl_p(data,l,ec);\n\t\t\t\t\tp[sw]=l;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\treturn;\n\t\t\t}\n\t\t}\n#if SHA_LONG_LOG2==2\n#if defined(B_ENDIAN) || defined(SHA1_ASM)\n\tif ((((unsigned long)data)%sizeof(SHA_LONG)) == 0)\n\t\t{\n\t\tsw=len/SHA_CBLOCK;\n\t\tif (sw)\n\t\t\t{\n\t\t\tsha1_block(c,(SHA_LONG *)data,sw);\n\t\t\tsw*=SHA_CBLOCK;\n\t\t\tdata+=sw;\n\t\t\tlen-=sw;\n\t\t\t}\n\t\t}\n#endif\n#endif\n\tp=c->data;\n\twhile (len >= SHA_CBLOCK)\n\t\t{\n#if SHA_LONG_LOG2==2\n#if defined(B_ENDIAN) || defined(SHA1_ASM)\n#define SHA_NO_TAIL_CODE\n\t\tif (p != (SHA_LONG *)data)\n\t\t\tmemcpy(p,data,SHA_CBLOCK);\n\t\tdata+=SHA_CBLOCK;\n\t\tsha1_block(c,p=c->data,1);\n\t\tlen-=SHA_CBLOCK;\n#elif defined(L_ENDIAN)\n#define BE_COPY(dst,src,i)\t{\t\t\t\t\\\n\t\t\t\tl = ((SHA_LONG *)src)[i];\t\\\n\t\t\t\tEndian_Reverse32(l);\t\t\\\n\t\t\t\tdst[i] = l;\t\t\t\\\n\t\t\t\t}\n\t\tif ((((unsigned long)data)%sizeof(SHA_LONG)) == 0)\n\t\t\t{\n\t\t\tfor (sw=(SHA_LBLOCK/4); sw; sw--)\n\t\t\t\t{\n\t\t\t\tBE_COPY(p,data,0);\n\t\t\t\tBE_COPY(p,data,1);\n\t\t\t\tBE_COPY(p,data,2);\n\t\t\t\tBE_COPY(p,data,3);\n\t\t\t\tp+=4;\n\t\t\t\tdata += 4*sizeof(SHA_LONG);\n\t\t\t\t}\n\t\t\tsha1_block(c,p=c->data,1);\n\t\t\tlen-=SHA_CBLOCK;\n\t\t\tcontinue;\n\t\t\t}\n#endif\n#endif\n#ifndef SHA_NO_TAIL_CODE\n\t\tp=c->data;\n\t\tfor (sw=(SHA_LBLOCK/4); sw; sw--)\n\t\t\t{\n\t\t\tM_c2nl(data,l); p[0]=l;\n\t\t\tM_c2nl(data,l); p[1]=l;\n\t\t\tM_c2nl(data,l); p[2]=l;\n\t\t\tM_c2nl(data,l); p[3]=l;\n\t\t\tp+=4;\n\t\t\t}\n\t\tp=c->data;\n\t\tsha1_block(c,p,1);\n\t\tlen-=SHA_CBLOCK;\n#endif\n\t\t}\n\tec=(int)len;\n\tc->num=ec;\n\tew=(ec>>2);\n\tec&=0x03;\n\tfor (sw=0; sw < ew; sw++)\n\t\t{ M_c2nl(data,l); p[sw]=l; }\n\tM_c2nl_p(data,l,ec);\n\tp[sw]=l;\n\t}']
36,449
0
https://github.com/libav/libav/blob/6aa4e88106a554cef1d2294bb0a18b8f843032ef/libavformat/rtsp.c/#L1898
static int sdp_read_header(AVFormatContext *s) { RTSPState *rt = s->priv_data; RTSPStream *rtsp_st; int size, i, err; char *content; char url[1024]; if (!ff_network_init()) return AVERROR(EIO); if (s->max_delay < 0) s->max_delay = DEFAULT_REORDERING_DELAY; content = av_malloc(SDP_MAX_SIZE); size = avio_read(s->pb, content, SDP_MAX_SIZE - 1); if (size <= 0) { av_free(content); return AVERROR_INVALIDDATA; } content[size] ='\0'; err = ff_sdp_parse(s, content); av_free(content); if (err) goto fail; for (i = 0; i < rt->nb_rtsp_streams; i++) { char namebuf[50]; rtsp_st = rt->rtsp_streams[i]; getnameinfo((struct sockaddr*) &rtsp_st->sdp_ip, sizeof(rtsp_st->sdp_ip), namebuf, sizeof(namebuf), NULL, 0, NI_NUMERICHOST); ff_url_join(url, sizeof(url), "rtp", NULL, namebuf, rtsp_st->sdp_port, "?localport=%d&ttl=%d&connect=%d", rtsp_st->sdp_port, rtsp_st->sdp_ttl, rt->rtsp_flags & RTSP_FLAG_FILTER_SRC ? 1 : 0); if (ffurl_open(&rtsp_st->rtp_handle, url, AVIO_FLAG_READ_WRITE, &s->interrupt_callback, NULL) < 0) { err = AVERROR_INVALIDDATA; goto fail; } if ((err = rtsp_open_transport_ctx(s, rtsp_st))) goto fail; } return 0; fail: ff_rtsp_close_streams(s); ff_network_close(); return err; }
['static int sdp_read_header(AVFormatContext *s)\n{\n RTSPState *rt = s->priv_data;\n RTSPStream *rtsp_st;\n int size, i, err;\n char *content;\n char url[1024];\n if (!ff_network_init())\n return AVERROR(EIO);\n if (s->max_delay < 0)\n s->max_delay = DEFAULT_REORDERING_DELAY;\n content = av_malloc(SDP_MAX_SIZE);\n size = avio_read(s->pb, content, SDP_MAX_SIZE - 1);\n if (size <= 0) {\n av_free(content);\n return AVERROR_INVALIDDATA;\n }\n content[size] =\'\\0\';\n err = ff_sdp_parse(s, content);\n av_free(content);\n if (err) goto fail;\n for (i = 0; i < rt->nb_rtsp_streams; i++) {\n char namebuf[50];\n rtsp_st = rt->rtsp_streams[i];\n getnameinfo((struct sockaddr*) &rtsp_st->sdp_ip, sizeof(rtsp_st->sdp_ip),\n namebuf, sizeof(namebuf), NULL, 0, NI_NUMERICHOST);\n ff_url_join(url, sizeof(url), "rtp", NULL,\n namebuf, rtsp_st->sdp_port,\n "?localport=%d&ttl=%d&connect=%d", rtsp_st->sdp_port,\n rtsp_st->sdp_ttl,\n rt->rtsp_flags & RTSP_FLAG_FILTER_SRC ? 1 : 0);\n if (ffurl_open(&rtsp_st->rtp_handle, url, AVIO_FLAG_READ_WRITE,\n &s->interrupt_callback, NULL) < 0) {\n err = AVERROR_INVALIDDATA;\n goto fail;\n }\n if ((err = rtsp_open_transport_ctx(s, rtsp_st)))\n goto fail;\n }\n return 0;\nfail:\n ff_rtsp_close_streams(s);\n ff_network_close();\n return err;\n}', 'int ff_network_init(void)\n{\n#if HAVE_WINSOCK2_H\n WSADATA wsaData;\n#endif\n if (!ff_network_inited_globally)\n av_log(NULL, AV_LOG_WARNING, "Using network protocols without global "\n "network initialization. Please use "\n "avformat_network_init(), this will "\n "become mandatory later.\\n");\n#if HAVE_WINSOCK2_H\n if (WSAStartup(MAKEWORD(1,1), &wsaData))\n return 0;\n#endif\n return 1;\n}', 'void *av_malloc(size_t size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n assert(size);\n if (size > (INT_MAX-32) || !size)\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size+32);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&31) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr,32,size))\n ptr = NULL;\n#elif HAVE_ALIGNED_MALLOC\n ptr = _aligned_malloc(size, 32);\n#elif HAVE_MEMALIGN\n ptr = memalign(32,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}']
36,450
0
https://github.com/libav/libav/blob/47399ccdfd93d337c96c76fbf591f0e3637131ef/libavcodec/bitstream.h/#L236
static inline void skip_remaining(BitstreamContext *bc, unsigned n) { #ifdef BITSTREAM_READER_LE bc->bits >>= n; #else bc->bits <<= n; #endif bc->bits_left -= n; }
['static int svq1_decode_frame_header(AVCodecContext *avctx, AVFrame *frame)\n{\n SVQ1Context *s = avctx->priv_data;\n BitstreamContext *bc = &s->bc;\n int frame_size_code;\n bitstream_skip(bc, 8);\n s->nonref = 0;\n switch (bitstream_read(bc, 2)) {\n case 0:\n frame->pict_type = AV_PICTURE_TYPE_I;\n break;\n case 2:\n s->nonref = 1;\n case 1:\n frame->pict_type = AV_PICTURE_TYPE_P;\n break;\n default:\n av_log(avctx, AV_LOG_ERROR, "Invalid frame type.\\n");\n return AVERROR_INVALIDDATA;\n }\n if (frame->pict_type == AV_PICTURE_TYPE_I) {\n if (s->frame_code == 0x50 || s->frame_code == 0x60) {\n int csum = bitstream_read(bc, 16);\n csum = ff_svq1_packet_checksum(bc->buffer, bc->size_in_bits >> 3,\n csum);\n ff_dlog(avctx, "%s checksum (%02x) for packet data\\n",\n (csum == 0) ? "correct" : "incorrect", csum);\n }\n if ((s->frame_code ^ 0x10) >= 0x50) {\n uint8_t msg[256];\n svq1_parse_string(bc, msg);\n av_log(avctx, AV_LOG_INFO,\n "embedded message: \\"%s\\"\\n", (char *)msg);\n }\n bitstream_skip(bc, 2);\n bitstream_skip(bc, 2);\n bitstream_skip(bc, 1);\n frame_size_code = bitstream_read(bc, 3);\n if (frame_size_code == 7) {\n s->width = bitstream_read(bc, 12);\n s->height = bitstream_read(bc, 12);\n if (!s->width || !s->height)\n return AVERROR_INVALIDDATA;\n } else {\n s->width = ff_svq1_frame_size_table[frame_size_code][0];\n s->height = ff_svq1_frame_size_table[frame_size_code][1];\n }\n }\n if (bitstream_read_bit(bc) == 1) {\n bitstream_skip(bc, 1);\n bitstream_skip(bc, 1);\n if (bitstream_read(bc, 2) != 0)\n return AVERROR_INVALIDDATA;\n }\n if (bitstream_read_bit(bc) == 1) {\n bitstream_skip(bc, 1);\n bitstream_skip(bc, 4);\n bitstream_skip(bc, 1);\n bitstream_skip(bc, 2);\n while (bitstream_read_bit(bc) == 1)\n bitstream_skip(bc, 8);\n }\n return 0;\n}', 'static inline void bitstream_skip(BitstreamContext *bc, unsigned n)\n{\n if (n < bc->bits_left)\n skip_remaining(bc, n);\n else {\n n -= bc->bits_left;\n bc->bits = 0;\n bc->bits_left = 0;\n if (n >= 64) {\n unsigned skip = n / 8;\n n -= skip * 8;\n bc->ptr += skip;\n }\n refill_64(bc);\n if (n)\n skip_remaining(bc, n);\n }\n}', 'static inline void refill_64(BitstreamContext *bc)\n{\n if (bc->ptr >= bc->buffer_end)\n return;\n#ifdef BITSTREAM_READER_LE\n bc->bits = AV_RL64(bc->ptr);\n#else\n bc->bits = AV_RB64(bc->ptr);\n#endif\n bc->ptr += 8;\n bc->bits_left = 64;\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}']
36,451
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 void\nngx_http_upstream_process_header(ngx_http_request_t *r, ngx_http_upstream_t *u)\n{\n ssize_t n;\n ngx_int_t rc;\n ngx_connection_t *c;\n c = u->peer.connection;\n ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,\n "http upstream process header");\n c->log->action = "reading response header from upstream";\n if (c->read->timedout) {\n ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_TIMEOUT);\n return;\n }\n if (!u->request_sent && ngx_http_upstream_test_connect(c) != NGX_OK) {\n ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);\n return;\n }\n if (u->buffer.start == NULL) {\n u->buffer.start = ngx_palloc(r->pool, u->conf->buffer_size);\n if (u->buffer.start == NULL) {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n u->buffer.pos = u->buffer.start;\n u->buffer.last = u->buffer.start;\n u->buffer.end = u->buffer.start + u->conf->buffer_size;\n u->buffer.temporary = 1;\n u->buffer.tag = u->output.tag;\n if (ngx_list_init(&u->headers_in.headers, r->pool, 8,\n sizeof(ngx_table_elt_t))\n != NGX_OK)\n {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n#if (NGX_HTTP_CACHE)\n if (r->cache) {\n u->buffer.pos += r->cache->header_start;\n u->buffer.last = u->buffer.pos;\n }\n#endif\n }\n for ( ;; ) {\n n = c->recv(c, u->buffer.last, u->buffer.end - u->buffer.last);\n if (n == NGX_AGAIN) {\n#if 0\n ngx_add_timer(rev, u->read_timeout);\n#endif\n if (ngx_handle_read_event(c->read, 0) != NGX_OK) {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n return;\n }\n if (n == 0) {\n ngx_log_error(NGX_LOG_ERR, c->log, 0,\n "upstream prematurely closed connection");\n }\n if (n == NGX_ERROR || n == 0) {\n ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);\n return;\n }\n u->buffer.last += n;\n#if 0\n u->valid_header_in = 0;\n u->peer.cached = 0;\n#endif\n rc = u->process_header(r);\n if (rc == NGX_AGAIN) {\n if (u->buffer.pos == u->buffer.end) {\n ngx_log_error(NGX_LOG_ERR, c->log, 0,\n "upstream sent too big header");\n ngx_http_upstream_next(r, u,\n NGX_HTTP_UPSTREAM_FT_INVALID_HEADER);\n return;\n }\n continue;\n }\n break;\n }\n if (rc == NGX_HTTP_UPSTREAM_INVALID_HEADER) {\n ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_INVALID_HEADER);\n return;\n }\n if (rc == NGX_ERROR) {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n if (u->headers_in.status_n >= NGX_HTTP_BAD_REQUEST) {\n if (r->subrequest_in_memory) {\n u->buffer.last = u->buffer.pos;\n }\n if (ngx_http_upstream_test_next(r, u) == NGX_OK) {\n return;\n }\n if (ngx_http_upstream_intercept_errors(r, u) == NGX_OK) {\n return;\n }\n }\n if (ngx_http_upstream_process_headers(r, u) != NGX_OK) {\n return;\n }\n if (!r->subrequest_in_memory) {\n ngx_http_upstream_send_response(r, u);\n return;\n }\n if (u->input_filter == NULL) {\n u->input_filter_init = ngx_http_upstream_non_buffered_filter_init;\n u->input_filter = ngx_http_upstream_non_buffered_filter;\n u->input_filter_ctx = r;\n }\n if (u->input_filter_init(u->input_filter_ctx) == NGX_ERROR) {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n n = u->buffer.last - u->buffer.pos;\n if (n) {\n u->buffer.last -= n;\n u->state->response_length += n;\n if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) {\n ngx_http_upstream_finalize_request(r, u, NGX_ERROR);\n return;\n }\n if (u->length == 0) {\n ngx_http_upstream_finalize_request(r, u, 0);\n return;\n }\n }\n u->read_event_handler = ngx_http_upstream_process_body_in_memory;\n ngx_http_upstream_process_body_in_memory(r, u);\n}', 'static ngx_int_t\nngx_http_upstream_process_headers(ngx_http_request_t *r, ngx_http_upstream_t *u)\n{\n ngx_str_t *uri, args;\n ngx_uint_t i, flags;\n ngx_list_part_t *part;\n ngx_table_elt_t *h;\n ngx_http_upstream_header_t *hh;\n ngx_http_upstream_main_conf_t *umcf;\n umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);\n if (u->headers_in.x_accel_redirect\n && !(u->conf->ignore_headers & NGX_HTTP_UPSTREAM_IGN_XA_REDIRECT))\n {\n ngx_http_upstream_finalize_request(r, u, NGX_DECLINED);\n part = &u->headers_in.headers.part;\n h = part->elts;\n for (i = 0; ; i++) {\n if (i >= part->nelts) {\n if (part->next == NULL) {\n break;\n }\n part = part->next;\n h = part->elts;\n i = 0;\n }\n hh = ngx_hash_find(&umcf->headers_in_hash, h[i].hash,\n h[i].lowcase_key, h[i].key.len);\n if (hh && hh->redirect) {\n if (hh->copy_handler(r, &h[i], hh->conf) != NGX_OK) {\n ngx_http_finalize_request(r,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return NGX_DONE;\n }\n }\n }\n uri = &u->headers_in.x_accel_redirect->value;\n args.len = 0;\n args.data = NULL;\n flags = 0;\n if (ngx_http_parse_unsafe_uri(r, uri, &args, &flags) != NGX_OK) {\n ngx_http_finalize_request(r, NGX_HTTP_NOT_FOUND);\n return NGX_DONE;\n }\n if (flags & NGX_HTTP_ZERO_IN_URI) {\n r->zero_in_uri = 1;\n }\n if (r->method != NGX_HTTP_HEAD) {\n r->method = NGX_HTTP_GET;\n }\n r->valid_unparsed_uri = 0;\n ngx_http_internal_redirect(r, uri, &args);\n ngx_http_finalize_request(r, NGX_DONE);\n return NGX_DONE;\n }\n part = &u->headers_in.headers.part;\n h = part->elts;\n for (i = 0; ; i++) {\n if (i >= part->nelts) {\n if (part->next == NULL) {\n break;\n }\n part = part->next;\n h = part->elts;\n i = 0;\n }\n if (ngx_hash_find(&u->conf->hide_headers_hash, h[i].hash,\n h[i].lowcase_key, h[i].key.len))\n {\n continue;\n }\n hh = ngx_hash_find(&umcf->headers_in_hash, h[i].hash,\n h[i].lowcase_key, h[i].key.len);\n if (hh) {\n if (hh->copy_handler(r, &h[i], hh->conf) != NGX_OK) {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return NGX_DONE;\n }\n continue;\n }\n if (ngx_http_upstream_copy_header_line(r, &h[i], 0) != NGX_OK) {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return NGX_DONE;\n }\n }\n if (r->headers_out.server && r->headers_out.server->value.data == NULL) {\n r->headers_out.server->hash = 0;\n }\n if (r->headers_out.date && r->headers_out.date->value.data == NULL) {\n r->headers_out.date->hash = 0;\n }\n r->headers_out.status = u->headers_in.status_n;\n r->headers_out.status_line = u->headers_in.status_line;\n u->headers_in.content_length_n = r->headers_out.content_length_n;\n if (r->headers_out.content_length_n != -1) {\n u->length = (size_t) r->headers_out.content_length_n;\n } else {\n u->length = NGX_MAX_SIZE_T_VALUE;\n }\n return NGX_OK;\n}', 'static void\nngx_http_upstream_send_response(ngx_http_request_t *r, ngx_http_upstream_t *u)\n{\n int tcp_nodelay;\n ssize_t n;\n ngx_int_t rc;\n ngx_event_pipe_t *p;\n ngx_connection_t *c;\n ngx_http_core_loc_conf_t *clcf;\n rc = ngx_http_send_header(r);\n if (rc == NGX_ERROR || rc > NGX_OK || r->post_action) {\n ngx_http_upstream_finalize_request(r, u, rc);\n return;\n }\n c = r->connection;\n if (r->header_only) {\n if (u->cacheable || u->store) {\n if (ngx_shutdown_socket(c->fd, NGX_WRITE_SHUTDOWN) == -1) {\n ngx_connection_error(c, ngx_socket_errno,\n ngx_shutdown_socket_n " failed");\n }\n r->read_event_handler = ngx_http_request_empty_handler;\n r->write_event_handler = ngx_http_request_empty_handler;\n c->error = 1;\n } else {\n ngx_http_upstream_finalize_request(r, u, rc);\n return;\n }\n }\n u->header_sent = 1;\n if (r->request_body && r->request_body->temp_file) {\n ngx_pool_run_cleanup_file(r->pool, r->request_body->temp_file->file.fd);\n r->request_body->temp_file->file.fd = NGX_INVALID_FILE;\n }\n clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);\n if (!u->buffering) {\n if (u->input_filter == NULL) {\n u->input_filter_init = ngx_http_upstream_non_buffered_filter_init;\n u->input_filter = ngx_http_upstream_non_buffered_filter;\n u->input_filter_ctx = r;\n }\n u->read_event_handler = ngx_http_upstream_process_non_buffered_upstream;\n r->write_event_handler =\n ngx_http_upstream_process_non_buffered_downstream;\n r->limit_rate = 0;\n if (u->input_filter_init(u->input_filter_ctx) == NGX_ERROR) {\n ngx_http_upstream_finalize_request(r, u, 0);\n return;\n }\n if (clcf->tcp_nodelay && c->tcp_nodelay == NGX_TCP_NODELAY_UNSET) {\n ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "tcp_nodelay");\n tcp_nodelay = 1;\n if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY,\n (const void *) &tcp_nodelay, sizeof(int)) == -1)\n {\n ngx_connection_error(c, ngx_socket_errno,\n "setsockopt(TCP_NODELAY) failed");\n ngx_http_upstream_finalize_request(r, u, 0);\n return;\n }\n c->tcp_nodelay = NGX_TCP_NODELAY_SET;\n }\n n = u->buffer.last - u->buffer.pos;\n if (n) {\n u->buffer.last = u->buffer.pos;\n u->state->response_length += n;\n if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) {\n ngx_http_upstream_finalize_request(r, u, 0);\n return;\n }\n ngx_http_upstream_process_non_buffered_downstream(r);\n } else {\n u->buffer.pos = u->buffer.start;\n u->buffer.last = u->buffer.start;\n if (ngx_http_send_special(r, NGX_HTTP_FLUSH) == NGX_ERROR) {\n ngx_http_upstream_finalize_request(r, u, 0);\n return;\n }\n if (u->peer.connection->read->ready) {\n ngx_http_upstream_process_non_buffered_upstream(r, u);\n }\n }\n return;\n }\n#if (NGX_HTTP_CACHE)\n if (r->cache && r->cache->file.fd != NGX_INVALID_FILE) {\n ngx_pool_run_cleanup_file(r->pool, r->cache->file.fd);\n r->cache->file.fd = NGX_INVALID_FILE;\n }\n if (u->cacheable) {\n time_t now, valid;\n now = ngx_time();\n valid = r->cache->valid_sec;\n if (valid == 0) {\n valid = ngx_http_file_cache_valid(u->conf->cache_valid,\n u->headers_in.status_n);\n if (valid) {\n r->cache->valid_sec = now + valid;\n }\n }\n if (valid) {\n r->cache->last_modified = r->headers_out.last_modified_time;\n r->cache->date = now;\n r->cache->body_start = (u_short) (u->buffer.pos - u->buffer.start);\n if (r->headers_out.content_length_n != -1) {\n r->cache->length = r->cache->body_start\n + r->headers_out.content_length_n;\n }\n ngx_http_file_cache_set_header(r, u->buffer.start);\n } else {\n u->cacheable = 0;\n r->headers_out.last_modified_time = -1;\n }\n }\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,\n "http cacheable: %d", u->cacheable);\n if (u->cacheable == 0 && r->cache) {\n ngx_http_file_cache_free(r, u->pipe->temp_file);\n }\n#endif\n p = u->pipe;\n p->output_filter = (ngx_event_pipe_output_filter_pt) ngx_http_output_filter;\n p->output_ctx = r;\n p->tag = u->output.tag;\n p->bufs = u->conf->bufs;\n p->busy_size = u->conf->busy_buffers_size;\n p->upstream = u->peer.connection;\n p->downstream = c;\n p->pool = r->pool;\n p->log = c->log;\n p->cacheable = u->cacheable || u->store;\n p->temp_file = ngx_pcalloc(r->pool, sizeof(ngx_temp_file_t));\n if (p->temp_file == NULL) {\n ngx_http_upstream_finalize_request(r, u, 0);\n return;\n }\n p->temp_file->file.fd = NGX_INVALID_FILE;\n p->temp_file->file.log = c->log;\n p->temp_file->path = u->conf->temp_path;\n p->temp_file->pool = r->pool;\n if (p->cacheable) {\n p->temp_file->persistent = 1;\n } else {\n p->temp_file->log_level = NGX_LOG_WARN;\n p->temp_file->warn = "an upstream response is buffered "\n "to a temporary file";\n }\n p->max_temp_file_size = u->conf->max_temp_file_size;\n p->temp_file_write_size = u->conf->temp_file_write_size;\n p->preread_bufs = ngx_alloc_chain_link(r->pool);\n if (p->preread_bufs == NULL) {\n ngx_http_upstream_finalize_request(r, u, 0);\n return;\n }\n p->preread_bufs->buf = &u->buffer;\n p->preread_bufs->next = NULL;\n u->buffer.recycled = 1;\n p->preread_size = u->buffer.last - u->buffer.pos;\n if (u->cacheable) {\n p->buf_to_file = ngx_calloc_buf(r->pool);\n if (p->buf_to_file == NULL) {\n ngx_http_upstream_finalize_request(r, u, 0);\n return;\n }\n p->buf_to_file->pos = u->buffer.start;\n p->buf_to_file->last = u->buffer.pos;\n p->buf_to_file->temporary = 1;\n }\n if (ngx_event_flags & NGX_USE_AIO_EVENT) {\n p->single_buf = 1;\n }\n p->free_bufs = 1;\n u->buffer.last = u->buffer.pos;\n if (u->conf->cyclic_temp_file) {\n p->cyclic_temp_file = 1;\n c->sendfile = 0;\n } else {\n p->cyclic_temp_file = 0;\n }\n p->read_timeout = u->conf->read_timeout;\n p->send_timeout = clcf->send_timeout;\n p->send_lowat = clcf->send_lowat;\n u->read_event_handler = ngx_http_upstream_process_upstream;\n r->write_event_handler = ngx_http_upstream_process_downstream;\n ngx_http_upstream_process_upstream(r, u);\n}', 'static void\nngx_http_upstream_finalize_request(ngx_http_request_t *r,\n ngx_http_upstream_t *u, ngx_int_t rc)\n{\n ngx_time_t *tp;\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "finalize http upstream request: %i", rc);\n if (u->cleanup) {\n *u->cleanup = NULL;\n }\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}']
36,452
0
https://github.com/openssl/openssl/blob/8fa6a40be2935ca109a28cc43d28cd27051ada01/crypto/lhash/lhash.c/#L365
static void contract(LHASH *lh) { LHASH_NODE **n,*n1,*np; np=lh->b[lh->p+lh->pmax-1]; lh->b[lh->p+lh->pmax-1]=NULL; if (lh->p == 0) { n=(LHASH_NODE **)OPENSSL_realloc(lh->b, (unsigned int)(sizeof(LHASH_NODE *)*lh->pmax)); if (n == NULL) { lh->error++; return; } lh->num_contract_reallocs++; lh->num_alloc_nodes/=2; lh->pmax/=2; lh->p=lh->pmax-1; lh->b=n; } else lh->p--; lh->num_nodes--; lh->num_contracts++; n1=lh->b[(int)lh->p]; if (n1 == NULL) lh->b[(int)lh->p]=np; else { while (n1->next != NULL) n1=n1->next; n1->next=np; } }
['int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c)\n\t{\n\tint ret=0;\n\tSSL_SESSION *s;\n\tCRYPTO_add(&c->references,1,CRYPTO_LOCK_SSL_SESSION);\n\tCRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);\n\ts=(SSL_SESSION *)lh_insert(ctx->sessions,c);\n\tif (s != NULL && s != c)\n\t\t{\n\t\tSSL_SESSION_list_remove(ctx,s);\n\t\tSSL_SESSION_free(s);\n\t\ts = NULL;\n\t\t}\n\tif (s == NULL)\n\t\tSSL_SESSION_list_add(ctx,c);\n\tif (s != NULL)\n\t\t{\n\t\tSSL_SESSION_free(s);\n\t\tret=0;\n\t\t}\n\telse\n\t\t{\n\t\tret=1;\n\t\tif (SSL_CTX_sess_get_cache_size(ctx) > 0)\n\t\t\t{\n\t\t\twhile (SSL_CTX_sess_number(ctx) >\n\t\t\t\tSSL_CTX_sess_get_cache_size(ctx))\n\t\t\t\t{\n\t\t\t\tif (!remove_session_lock(ctx,\n\t\t\t\t\tctx->session_cache_tail, 0))\n\t\t\t\t\tbreak;\n\t\t\t\telse\n\t\t\t\t\tctx->stats.sess_cache_full++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\tCRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n\treturn(ret);\n\t}', 'void *lh_insert(LHASH *lh, void *data)\n\t{\n\tunsigned long hash;\n\tLHASH_NODE *nn,**rn;\n\tvoid *ret;\n\tlh->error=0;\n\tif (lh->up_load <= (lh->num_items*LH_LOAD_MULT/lh->num_nodes))\n\t\texpand(lh);\n\trn=getrn(lh,data,&hash);\n\tif (*rn == NULL)\n\t\t{\n\t\tif ((nn=(LHASH_NODE *)OPENSSL_malloc(sizeof(LHASH_NODE))) == NULL)\n\t\t\t{\n\t\t\tlh->error++;\n\t\t\treturn(NULL);\n\t\t\t}\n\t\tnn->data=data;\n\t\tnn->next=NULL;\n#ifndef OPENSSL_NO_HASH_COMP\n\t\tnn->hash=hash;\n#endif\n\t\t*rn=nn;\n\t\tret=NULL;\n\t\tlh->num_insert++;\n\t\tlh->num_items++;\n\t\t}\n\telse\n\t\t{\n\t\tret= (*rn)->data;\n\t\t(*rn)->data=data;\n\t\tlh->num_replace++;\n\t\t}\n\treturn(ret);\n\t}', 'static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)\n\t{\n\tSSL_SESSION *r;\n\tint ret=0;\n\tif ((c != NULL) && (c->session_id_length != 0))\n\t\t{\n\t\tif(lck) CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);\n\t\tif ((r = (SSL_SESSION *)lh_retrieve(ctx->sessions,c)) == c)\n\t\t\t{\n\t\t\tret=1;\n\t\t\tr=(SSL_SESSION *)lh_delete(ctx->sessions,c);\n\t\t\tSSL_SESSION_list_remove(ctx,c);\n\t\t\t}\n\t\tif(lck) CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n\t\tif (ret)\n\t\t\t{\n\t\t\tr->not_resumable=1;\n\t\t\tif (ctx->remove_session_cb != NULL)\n\t\t\t\tctx->remove_session_cb(ctx,r);\n\t\t\tSSL_SESSION_free(r);\n\t\t\t}\n\t\t}\n\telse\n\t\tret=0;\n\treturn(ret);\n\t}', 'void *lh_delete(LHASH *lh, const void *data)\n\t{\n\tunsigned long hash;\n\tLHASH_NODE *nn,**rn;\n\tvoid *ret;\n\tlh->error=0;\n\trn=getrn(lh,data,&hash);\n\tif (*rn == NULL)\n\t\t{\n\t\tlh->num_no_delete++;\n\t\treturn(NULL);\n\t\t}\n\telse\n\t\t{\n\t\tnn= *rn;\n\t\t*rn=nn->next;\n\t\tret=nn->data;\n\t\tOPENSSL_free(nn);\n\t\tlh->num_delete++;\n\t\t}\n\tlh->num_items--;\n\tif ((lh->num_nodes > MIN_NODES) &&\n\t\t(lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes)))\n\t\tcontract(lh);\n\treturn(ret);\n\t}', 'static void contract(LHASH *lh)\n\t{\n\tLHASH_NODE **n,*n1,*np;\n\tnp=lh->b[lh->p+lh->pmax-1];\n\tlh->b[lh->p+lh->pmax-1]=NULL;\n\tif (lh->p == 0)\n\t\t{\n\t\tn=(LHASH_NODE **)OPENSSL_realloc(lh->b,\n\t\t\t(unsigned int)(sizeof(LHASH_NODE *)*lh->pmax));\n\t\tif (n == NULL)\n\t\t\t{\n\t\t\tlh->error++;\n\t\t\treturn;\n\t\t\t}\n\t\tlh->num_contract_reallocs++;\n\t\tlh->num_alloc_nodes/=2;\n\t\tlh->pmax/=2;\n\t\tlh->p=lh->pmax-1;\n\t\tlh->b=n;\n\t\t}\n\telse\n\t\tlh->p--;\n\tlh->num_nodes--;\n\tlh->num_contracts++;\n\tn1=lh->b[(int)lh->p];\n\tif (n1 == NULL)\n\t\tlh->b[(int)lh->p]=np;\n\telse\n\t\t{\n\t\twhile (n1->next != NULL)\n\t\t\tn1=n1->next;\n\t\tn1->next=np;\n\t\t}\n\t}']
36,453
0
https://github.com/libav/libav/blob/c9b10cc4dbb67a94c29359fde79fb882d71fef6f/libavcodec/bmp.c/#L167
static int bmp_decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt) { const uint8_t *buf = avpkt->data; int buf_size = avpkt->size; BMPContext *s = avctx->priv_data; AVFrame *picture = data; AVFrame *p = &s->picture; unsigned int fsize, hsize; int width, height; unsigned int depth; BiCompression comp; unsigned int ihsize; int i, j, n, linesize; uint32_t rgb[3]; uint8_t *ptr; int dsize; const uint8_t *buf0 = buf; GetByteContext gb; if(buf_size < 14){ av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size); return -1; } if(bytestream_get_byte(&buf) != 'B' || bytestream_get_byte(&buf) != 'M') { av_log(avctx, AV_LOG_ERROR, "bad magic number\n"); return -1; } fsize = bytestream_get_le32(&buf); if(buf_size < fsize){ av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d), trying to decode anyway\n", buf_size, fsize); fsize = buf_size; } buf += 2; buf += 2; hsize = bytestream_get_le32(&buf); ihsize = bytestream_get_le32(&buf); if(ihsize + 14 > hsize){ av_log(avctx, AV_LOG_ERROR, "invalid header size %d\n", hsize); return -1; } if(fsize == 14 || fsize == ihsize + 14) fsize = buf_size - 2; if(fsize <= hsize){ av_log(avctx, AV_LOG_ERROR, "declared file size is less than header size (%d < %d)\n", fsize, hsize); return -1; } switch(ihsize){ case 40: case 64: case 108: case 124: width = bytestream_get_le32(&buf); height = bytestream_get_le32(&buf); break; case 12: width = bytestream_get_le16(&buf); height = bytestream_get_le16(&buf); break; default: av_log(avctx, AV_LOG_ERROR, "unsupported BMP file, patch welcome\n"); return -1; } if(bytestream_get_le16(&buf) != 1){ av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n"); return -1; } depth = bytestream_get_le16(&buf); if(ihsize == 40) comp = bytestream_get_le32(&buf); else comp = BMP_RGB; if(comp != BMP_RGB && comp != BMP_BITFIELDS && comp != BMP_RLE4 && comp != BMP_RLE8){ av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp); return -1; } if(comp == BMP_BITFIELDS){ buf += 20; rgb[0] = bytestream_get_le32(&buf); rgb[1] = bytestream_get_le32(&buf); rgb[2] = bytestream_get_le32(&buf); } avctx->width = width; avctx->height = height > 0? height: -height; avctx->pix_fmt = AV_PIX_FMT_NONE; switch(depth){ case 32: if(comp == BMP_BITFIELDS){ rgb[0] = (rgb[0] >> 15) & 3; rgb[1] = (rgb[1] >> 15) & 3; rgb[2] = (rgb[2] >> 15) & 3; if(rgb[0] + rgb[1] + rgb[2] != 3 || rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]){ break; } } else { rgb[0] = 2; rgb[1] = 1; rgb[2] = 0; } avctx->pix_fmt = AV_PIX_FMT_BGR24; break; case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24; break; case 16: if(comp == BMP_RGB) avctx->pix_fmt = AV_PIX_FMT_RGB555; else if (comp == BMP_BITFIELDS) { if (rgb[0] == 0xF800 && rgb[1] == 0x07E0 && rgb[2] == 0x001F) avctx->pix_fmt = AV_PIX_FMT_RGB565; else if (rgb[0] == 0x7C00 && rgb[1] == 0x03E0 && rgb[2] == 0x001F) avctx->pix_fmt = AV_PIX_FMT_RGB555; else if (rgb[0] == 0x0F00 && rgb[1] == 0x00F0 && rgb[2] == 0x000F) avctx->pix_fmt = AV_PIX_FMT_RGB444; else { av_log(avctx, AV_LOG_ERROR, "Unknown bitfields %0X %0X %0X\n", rgb[0], rgb[1], rgb[2]); return AVERROR(EINVAL); } } break; case 8: if(hsize - ihsize - 14 > 0) avctx->pix_fmt = AV_PIX_FMT_PAL8; else avctx->pix_fmt = AV_PIX_FMT_GRAY8; break; case 1: case 4: if(hsize - ihsize - 14 > 0){ avctx->pix_fmt = AV_PIX_FMT_PAL8; }else{ av_log(avctx, AV_LOG_ERROR, "Unknown palette for %d-colour BMP\n", 1<<depth); return -1; } break; default: av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth); return -1; } if(avctx->pix_fmt == AV_PIX_FMT_NONE){ av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n"); return -1; } if(p->data[0]) avctx->release_buffer(avctx, p); p->reference = 0; if(avctx->get_buffer(avctx, p) < 0){ av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); return -1; } p->pict_type = AV_PICTURE_TYPE_I; p->key_frame = 1; buf = buf0 + hsize; dsize = buf_size - hsize; n = ((avctx->width * depth) / 8 + 3) & ~3; if(n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8){ av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n", dsize, n * avctx->height); return -1; } if(comp == BMP_RLE4 || comp == BMP_RLE8) memset(p->data[0], 0, avctx->height * p->linesize[0]); if(height > 0){ ptr = p->data[0] + (avctx->height - 1) * p->linesize[0]; linesize = -p->linesize[0]; } else { ptr = p->data[0]; linesize = p->linesize[0]; } if(avctx->pix_fmt == AV_PIX_FMT_PAL8){ int colors = 1 << depth; memset(p->data[1], 0, 1024); if(ihsize >= 36){ int t; buf = buf0 + 46; t = bytestream_get_le32(&buf); if(t < 0 || t > (1 << depth)){ av_log(avctx, AV_LOG_ERROR, "Incorrect number of colors - %X for bitdepth %d\n", t, depth); }else if(t){ colors = t; } } buf = buf0 + 14 + ihsize; if((hsize-ihsize-14) < (colors << 2)){ for(i = 0; i < colors; i++) ((uint32_t*)p->data[1])[i] = bytestream_get_le24(&buf); }else{ for(i = 0; i < colors; i++) ((uint32_t*)p->data[1])[i] = bytestream_get_le32(&buf); } buf = buf0 + hsize; } if(comp == BMP_RLE4 || comp == BMP_RLE8){ if(height < 0){ p->data[0] += p->linesize[0] * (avctx->height - 1); p->linesize[0] = -p->linesize[0]; } bytestream2_init(&gb, buf, dsize); ff_msrle_decode(avctx, (AVPicture*)p, depth, &gb); if(height < 0){ p->data[0] += p->linesize[0] * (avctx->height - 1); p->linesize[0] = -p->linesize[0]; } }else{ switch(depth){ case 1: for (i = 0; i < avctx->height; i++) { int j; for (j = 0; j < n; j++) { ptr[j*8+0] = buf[j] >> 7; ptr[j*8+1] = (buf[j] >> 6) & 1; ptr[j*8+2] = (buf[j] >> 5) & 1; ptr[j*8+3] = (buf[j] >> 4) & 1; ptr[j*8+4] = (buf[j] >> 3) & 1; ptr[j*8+5] = (buf[j] >> 2) & 1; ptr[j*8+6] = (buf[j] >> 1) & 1; ptr[j*8+7] = buf[j] & 1; } buf += n; ptr += linesize; } break; case 8: case 24: for(i = 0; i < avctx->height; i++){ memcpy(ptr, buf, n); buf += n; ptr += linesize; } break; case 4: for(i = 0; i < avctx->height; i++){ int j; for(j = 0; j < n; j++){ ptr[j*2+0] = (buf[j] >> 4) & 0xF; ptr[j*2+1] = buf[j] & 0xF; } buf += n; ptr += linesize; } break; case 16: for(i = 0; i < avctx->height; i++){ const uint16_t *src = (const uint16_t *) buf; uint16_t *dst = (uint16_t *) ptr; for(j = 0; j < avctx->width; j++) *dst++ = av_le2ne16(*src++); buf += n; ptr += linesize; } break; case 32: for(i = 0; i < avctx->height; i++){ const uint8_t *src = buf; uint8_t *dst = ptr; for(j = 0; j < avctx->width; j++){ dst[0] = src[rgb[2]]; dst[1] = src[rgb[1]]; dst[2] = src[rgb[0]]; dst += 3; src += 4; } buf += n; ptr += linesize; } break; default: av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n"); return -1; } } *picture = s->picture; *data_size = sizeof(AVPicture); return buf_size; }
['static int bmp_decode_frame(AVCodecContext *avctx,\n void *data, int *data_size,\n AVPacket *avpkt)\n{\n const uint8_t *buf = avpkt->data;\n int buf_size = avpkt->size;\n BMPContext *s = avctx->priv_data;\n AVFrame *picture = data;\n AVFrame *p = &s->picture;\n unsigned int fsize, hsize;\n int width, height;\n unsigned int depth;\n BiCompression comp;\n unsigned int ihsize;\n int i, j, n, linesize;\n uint32_t rgb[3];\n uint8_t *ptr;\n int dsize;\n const uint8_t *buf0 = buf;\n GetByteContext gb;\n if(buf_size < 14){\n av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\\n", buf_size);\n return -1;\n }\n if(bytestream_get_byte(&buf) != \'B\' ||\n bytestream_get_byte(&buf) != \'M\') {\n av_log(avctx, AV_LOG_ERROR, "bad magic number\\n");\n return -1;\n }\n fsize = bytestream_get_le32(&buf);\n if(buf_size < fsize){\n av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d), trying to decode anyway\\n",\n buf_size, fsize);\n fsize = buf_size;\n }\n buf += 2;\n buf += 2;\n hsize = bytestream_get_le32(&buf);\n ihsize = bytestream_get_le32(&buf);\n if(ihsize + 14 > hsize){\n av_log(avctx, AV_LOG_ERROR, "invalid header size %d\\n", hsize);\n return -1;\n }\n if(fsize == 14 || fsize == ihsize + 14)\n fsize = buf_size - 2;\n if(fsize <= hsize){\n av_log(avctx, AV_LOG_ERROR, "declared file size is less than header size (%d < %d)\\n",\n fsize, hsize);\n return -1;\n }\n switch(ihsize){\n case 40:\n case 64:\n case 108:\n case 124:\n width = bytestream_get_le32(&buf);\n height = bytestream_get_le32(&buf);\n break;\n case 12:\n width = bytestream_get_le16(&buf);\n height = bytestream_get_le16(&buf);\n break;\n default:\n av_log(avctx, AV_LOG_ERROR, "unsupported BMP file, patch welcome\\n");\n return -1;\n }\n if(bytestream_get_le16(&buf) != 1){\n av_log(avctx, AV_LOG_ERROR, "invalid BMP header\\n");\n return -1;\n }\n depth = bytestream_get_le16(&buf);\n if(ihsize == 40)\n comp = bytestream_get_le32(&buf);\n else\n comp = BMP_RGB;\n if(comp != BMP_RGB && comp != BMP_BITFIELDS && comp != BMP_RLE4 && comp != BMP_RLE8){\n av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\\n", comp);\n return -1;\n }\n if(comp == BMP_BITFIELDS){\n buf += 20;\n rgb[0] = bytestream_get_le32(&buf);\n rgb[1] = bytestream_get_le32(&buf);\n rgb[2] = bytestream_get_le32(&buf);\n }\n avctx->width = width;\n avctx->height = height > 0? height: -height;\n avctx->pix_fmt = AV_PIX_FMT_NONE;\n switch(depth){\n case 32:\n if(comp == BMP_BITFIELDS){\n rgb[0] = (rgb[0] >> 15) & 3;\n rgb[1] = (rgb[1] >> 15) & 3;\n rgb[2] = (rgb[2] >> 15) & 3;\n if(rgb[0] + rgb[1] + rgb[2] != 3 ||\n rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]){\n break;\n }\n } else {\n rgb[0] = 2;\n rgb[1] = 1;\n rgb[2] = 0;\n }\n avctx->pix_fmt = AV_PIX_FMT_BGR24;\n break;\n case 24:\n avctx->pix_fmt = AV_PIX_FMT_BGR24;\n break;\n case 16:\n if(comp == BMP_RGB)\n avctx->pix_fmt = AV_PIX_FMT_RGB555;\n else if (comp == BMP_BITFIELDS) {\n if (rgb[0] == 0xF800 && rgb[1] == 0x07E0 && rgb[2] == 0x001F)\n avctx->pix_fmt = AV_PIX_FMT_RGB565;\n else if (rgb[0] == 0x7C00 && rgb[1] == 0x03E0 && rgb[2] == 0x001F)\n avctx->pix_fmt = AV_PIX_FMT_RGB555;\n else if (rgb[0] == 0x0F00 && rgb[1] == 0x00F0 && rgb[2] == 0x000F)\n avctx->pix_fmt = AV_PIX_FMT_RGB444;\n else {\n av_log(avctx, AV_LOG_ERROR, "Unknown bitfields %0X %0X %0X\\n", rgb[0], rgb[1], rgb[2]);\n return AVERROR(EINVAL);\n }\n }\n break;\n case 8:\n if(hsize - ihsize - 14 > 0)\n avctx->pix_fmt = AV_PIX_FMT_PAL8;\n else\n avctx->pix_fmt = AV_PIX_FMT_GRAY8;\n break;\n case 1:\n case 4:\n if(hsize - ihsize - 14 > 0){\n avctx->pix_fmt = AV_PIX_FMT_PAL8;\n }else{\n av_log(avctx, AV_LOG_ERROR, "Unknown palette for %d-colour BMP\\n", 1<<depth);\n return -1;\n }\n break;\n default:\n av_log(avctx, AV_LOG_ERROR, "depth %d not supported\\n", depth);\n return -1;\n }\n if(avctx->pix_fmt == AV_PIX_FMT_NONE){\n av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\\n");\n return -1;\n }\n if(p->data[0])\n avctx->release_buffer(avctx, p);\n p->reference = 0;\n if(avctx->get_buffer(avctx, p) < 0){\n av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n return -1;\n }\n p->pict_type = AV_PICTURE_TYPE_I;\n p->key_frame = 1;\n buf = buf0 + hsize;\n dsize = buf_size - hsize;\n n = ((avctx->width * depth) / 8 + 3) & ~3;\n if(n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8){\n av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\\n",\n dsize, n * avctx->height);\n return -1;\n }\n if(comp == BMP_RLE4 || comp == BMP_RLE8)\n memset(p->data[0], 0, avctx->height * p->linesize[0]);\n if(height > 0){\n ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];\n linesize = -p->linesize[0];\n } else {\n ptr = p->data[0];\n linesize = p->linesize[0];\n }\n if(avctx->pix_fmt == AV_PIX_FMT_PAL8){\n int colors = 1 << depth;\n memset(p->data[1], 0, 1024);\n if(ihsize >= 36){\n int t;\n buf = buf0 + 46;\n t = bytestream_get_le32(&buf);\n if(t < 0 || t > (1 << depth)){\n av_log(avctx, AV_LOG_ERROR, "Incorrect number of colors - %X for bitdepth %d\\n", t, depth);\n }else if(t){\n colors = t;\n }\n }\n buf = buf0 + 14 + ihsize;\n if((hsize-ihsize-14) < (colors << 2)){\n for(i = 0; i < colors; i++)\n ((uint32_t*)p->data[1])[i] = bytestream_get_le24(&buf);\n }else{\n for(i = 0; i < colors; i++)\n ((uint32_t*)p->data[1])[i] = bytestream_get_le32(&buf);\n }\n buf = buf0 + hsize;\n }\n if(comp == BMP_RLE4 || comp == BMP_RLE8){\n if(height < 0){\n p->data[0] += p->linesize[0] * (avctx->height - 1);\n p->linesize[0] = -p->linesize[0];\n }\n bytestream2_init(&gb, buf, dsize);\n ff_msrle_decode(avctx, (AVPicture*)p, depth, &gb);\n if(height < 0){\n p->data[0] += p->linesize[0] * (avctx->height - 1);\n p->linesize[0] = -p->linesize[0];\n }\n }else{\n switch(depth){\n case 1:\n for (i = 0; i < avctx->height; i++) {\n int j;\n for (j = 0; j < n; j++) {\n ptr[j*8+0] = buf[j] >> 7;\n ptr[j*8+1] = (buf[j] >> 6) & 1;\n ptr[j*8+2] = (buf[j] >> 5) & 1;\n ptr[j*8+3] = (buf[j] >> 4) & 1;\n ptr[j*8+4] = (buf[j] >> 3) & 1;\n ptr[j*8+5] = (buf[j] >> 2) & 1;\n ptr[j*8+6] = (buf[j] >> 1) & 1;\n ptr[j*8+7] = buf[j] & 1;\n }\n buf += n;\n ptr += linesize;\n }\n break;\n case 8:\n case 24:\n for(i = 0; i < avctx->height; i++){\n memcpy(ptr, buf, n);\n buf += n;\n ptr += linesize;\n }\n break;\n case 4:\n for(i = 0; i < avctx->height; i++){\n int j;\n for(j = 0; j < n; j++){\n ptr[j*2+0] = (buf[j] >> 4) & 0xF;\n ptr[j*2+1] = buf[j] & 0xF;\n }\n buf += n;\n ptr += linesize;\n }\n break;\n case 16:\n for(i = 0; i < avctx->height; i++){\n const uint16_t *src = (const uint16_t *) buf;\n uint16_t *dst = (uint16_t *) ptr;\n for(j = 0; j < avctx->width; j++)\n *dst++ = av_le2ne16(*src++);\n buf += n;\n ptr += linesize;\n }\n break;\n case 32:\n for(i = 0; i < avctx->height; i++){\n const uint8_t *src = buf;\n uint8_t *dst = ptr;\n for(j = 0; j < avctx->width; j++){\n dst[0] = src[rgb[2]];\n dst[1] = src[rgb[1]];\n dst[2] = src[rgb[0]];\n dst += 3;\n src += 4;\n }\n buf += n;\n ptr += linesize;\n }\n break;\n default:\n av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\\n");\n return -1;\n }\n }\n *picture = s->picture;\n *data_size = sizeof(AVPicture);\n return buf_size;\n}']
36,454
0
https://github.com/openssl/openssl/blob/c849c6d9d3bf806fecfe0c16eaa55d361979ff7f/include/internal/constant_time_locl.h/#L168
static ossl_inline unsigned int constant_time_is_zero(unsigned int a) { return constant_time_msb(~a & (a - 1)); }
['static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx,\n unsigned char *out, size_t *outlen,\n const unsigned char *in, size_t inlen)\n{\n int ret;\n RSA_PKEY_CTX *rctx = ctx->data;\n if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {\n int i;\n if (!setup_tbuf(rctx, ctx))\n return -1;\n ret = RSA_private_decrypt(inlen, in, rctx->tbuf,\n ctx->pkey->pkey.rsa, RSA_NO_PADDING);\n if (ret <= 0)\n return ret;\n for (i = 0; i < ret; i++) {\n if (rctx->tbuf[i])\n break;\n }\n ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, ret, rctx->tbuf + i,\n ret - i, ret,\n rctx->oaep_label,\n rctx->oaep_labellen,\n rctx->md, rctx->mgf1md);\n } else\n ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa,\n rctx->pad_mode);\n if (ret < 0)\n return ret;\n *outlen = ret;\n return 1;\n}', 'static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk)\n{\n if (ctx->tbuf)\n return 1;\n ctx->tbuf = OPENSSL_malloc(EVP_PKEY_size(pk->pkey));\n if (ctx->tbuf == NULL)\n return 0;\n return 1;\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 if (allow_customize)\n allow_customize = 0;\n if (malloc_debug_func != NULL) {\n if (allow_customize_debug)\n allow_customize_debug = 0;\n malloc_debug_func(NULL, num, file, line, 0);\n }\n ret = malloc_ex_func(num, file, line);\n if (malloc_debug_func != NULL)\n malloc_debug_func(ret, num, file, line, 1);\n#ifndef OPENSSL_CPUID_OBJ\n if (ret && (num > 2048)) {\n extern unsigned char cleanse_ctr;\n ((unsigned char *)ret)[0] = cleanse_ctr;\n }\n#endif\n return ret;\n}', 'int RSA_padding_check_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,\n const unsigned char *from, int flen,\n int num, const unsigned char *param,\n int plen, const EVP_MD *md,\n const EVP_MD *mgf1md)\n{\n int i, dblen, mlen = -1, one_index = 0, msg_index;\n unsigned int good, found_one_byte;\n const unsigned char *maskedseed, *maskeddb;\n unsigned char *db = NULL, *em = NULL, seed[EVP_MAX_MD_SIZE],\n phash[EVP_MAX_MD_SIZE];\n int mdlen;\n if (md == NULL)\n md = EVP_sha1();\n if (mgf1md == NULL)\n mgf1md = md;\n mdlen = EVP_MD_size(md);\n if (tlen <= 0 || flen <= 0)\n return -1;\n if (num < flen || num < 2 * mdlen + 2)\n goto decoding_err;\n dblen = num - mdlen - 1;\n db = OPENSSL_malloc(dblen);\n em = OPENSSL_malloc(num);\n if (db == NULL || em == NULL) {\n RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1, ERR_R_MALLOC_FAILURE);\n goto cleanup;\n }\n memset(em, 0, num);\n memcpy(em + num - flen, from, flen);\n good = constant_time_is_zero(em[0]);\n maskedseed = em + 1;\n maskeddb = em + 1 + mdlen;\n if (PKCS1_MGF1(seed, mdlen, maskeddb, dblen, mgf1md))\n goto cleanup;\n for (i = 0; i < mdlen; i++)\n seed[i] ^= maskedseed[i];\n if (PKCS1_MGF1(db, dblen, seed, mdlen, mgf1md))\n goto cleanup;\n for (i = 0; i < dblen; i++)\n db[i] ^= maskeddb[i];\n if (!EVP_Digest((void *)param, plen, phash, NULL, md, NULL))\n goto cleanup;\n good &= constant_time_is_zero(CRYPTO_memcmp(db, phash, mdlen));\n found_one_byte = 0;\n for (i = mdlen; i < dblen; i++) {\n unsigned int equals1 = constant_time_eq(db[i], 1);\n unsigned int equals0 = constant_time_is_zero(db[i]);\n one_index = constant_time_select_int(~found_one_byte & equals1,\n i, one_index);\n found_one_byte |= equals1;\n good &= (found_one_byte | equals0);\n }\n good &= found_one_byte;\n if (!good)\n goto decoding_err;\n msg_index = one_index + 1;\n mlen = dblen - msg_index;\n if (tlen < mlen) {\n RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1, RSA_R_DATA_TOO_LARGE);\n mlen = -1;\n } else {\n memcpy(to, db + msg_index, mlen);\n goto cleanup;\n }\n decoding_err:\n RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1,\n RSA_R_OAEP_DECODING_ERROR);\n cleanup:\n OPENSSL_free(db);\n OPENSSL_free(em);\n return mlen;\n}', 'static ossl_inline unsigned int constant_time_is_zero(unsigned int a)\n{\n return constant_time_msb(~a & (a - 1));\n}']
36,455
0
https://github.com/libav/libav/blob/ab492ca2ab105aeb24d955f3f03756bdb3139ee1/libavcodec/dnxhddec.c/#L202
static av_always_inline void dnxhd_decode_dct_block(DNXHDContext *ctx, DCTELEM *block, int n, int qscale, int index_bits, int level_bias, int level_shift) { int i, j, index1, index2, len; int level, component, sign; const uint8_t *weight_matrix; OPEN_READER(bs, &ctx->gb); if (n&2) { component = 1 + (n&1); weight_matrix = ctx->cid_table->chroma_weight; } else { component = 0; weight_matrix = ctx->cid_table->luma_weight; } UPDATE_CACHE(bs, &ctx->gb); GET_VLC(len, bs, &ctx->gb, ctx->dc_vlc.table, DNXHD_DC_VLC_BITS, 1); if (len) { level = GET_CACHE(bs, &ctx->gb); LAST_SKIP_BITS(bs, &ctx->gb, len); sign = ~level >> 31; level = (NEG_USR32(sign ^ level, len) ^ sign) - sign; ctx->last_dc[component] += level; } block[0] = ctx->last_dc[component]; for (i = 1; ; i++) { UPDATE_CACHE(bs, &ctx->gb); GET_VLC(index1, bs, &ctx->gb, ctx->ac_vlc.table, DNXHD_VLC_BITS, 2); level = ctx->cid_table->ac_level[index1]; if (!level) { break; } sign = SHOW_SBITS(bs, &ctx->gb, 1); SKIP_BITS(bs, &ctx->gb, 1); if (ctx->cid_table->ac_index_flag[index1]) { level += SHOW_UBITS(bs, &ctx->gb, index_bits) << 6; SKIP_BITS(bs, &ctx->gb, index_bits); } if (ctx->cid_table->ac_run_flag[index1]) { UPDATE_CACHE(bs, &ctx->gb); GET_VLC(index2, bs, &ctx->gb, ctx->run_vlc.table, DNXHD_VLC_BITS, 2); i += ctx->cid_table->run[index2]; } if (i > 63) { av_log(ctx->avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\n", n, i); break; } j = ctx->scantable.permutated[i]; level = (2*level+1) * qscale * weight_matrix[i]; if (level_bias < 32 || weight_matrix[i] != level_bias) level += level_bias; level >>= level_shift; block[j] = (level^sign) - sign; } CLOSE_READER(bs, &ctx->gb); }
['static av_always_inline void dnxhd_decode_dct_block(DNXHDContext *ctx,\n DCTELEM *block, int n,\n int qscale,\n int index_bits,\n int level_bias,\n int level_shift)\n{\n int i, j, index1, index2, len;\n int level, component, sign;\n const uint8_t *weight_matrix;\n OPEN_READER(bs, &ctx->gb);\n if (n&2) {\n component = 1 + (n&1);\n weight_matrix = ctx->cid_table->chroma_weight;\n } else {\n component = 0;\n weight_matrix = ctx->cid_table->luma_weight;\n }\n UPDATE_CACHE(bs, &ctx->gb);\n GET_VLC(len, bs, &ctx->gb, ctx->dc_vlc.table, DNXHD_DC_VLC_BITS, 1);\n if (len) {\n level = GET_CACHE(bs, &ctx->gb);\n LAST_SKIP_BITS(bs, &ctx->gb, len);\n sign = ~level >> 31;\n level = (NEG_USR32(sign ^ level, len) ^ sign) - sign;\n ctx->last_dc[component] += level;\n }\n block[0] = ctx->last_dc[component];\n for (i = 1; ; i++) {\n UPDATE_CACHE(bs, &ctx->gb);\n GET_VLC(index1, bs, &ctx->gb, ctx->ac_vlc.table,\n DNXHD_VLC_BITS, 2);\n level = ctx->cid_table->ac_level[index1];\n if (!level) {\n break;\n }\n sign = SHOW_SBITS(bs, &ctx->gb, 1);\n SKIP_BITS(bs, &ctx->gb, 1);\n if (ctx->cid_table->ac_index_flag[index1]) {\n level += SHOW_UBITS(bs, &ctx->gb, index_bits) << 6;\n SKIP_BITS(bs, &ctx->gb, index_bits);\n }\n if (ctx->cid_table->ac_run_flag[index1]) {\n UPDATE_CACHE(bs, &ctx->gb);\n GET_VLC(index2, bs, &ctx->gb, ctx->run_vlc.table,\n DNXHD_VLC_BITS, 2);\n i += ctx->cid_table->run[index2];\n }\n if (i > 63) {\n av_log(ctx->avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\\n", n, i);\n break;\n }\n j = ctx->scantable.permutated[i];\n level = (2*level+1) * qscale * weight_matrix[i];\n if (level_bias < 32 || weight_matrix[i] != level_bias)\n level += level_bias;\n level >>= level_shift;\n block[j] = (level^sign) - sign;\n }\n CLOSE_READER(bs, &ctx->gb);\n}']
36,456
0
https://github.com/openssl/openssl/blob/b11327929294cf825e4759d97af6f174bd6b081c/test/ectest.c/#L1184
static int group_field_test(void) { int r = 1; BIGNUM *secp521r1_field = NULL; BIGNUM *sect163r2_field = NULL; EC_GROUP *secp521r1_group = NULL; EC_GROUP *sect163r2_group = NULL; BN_hex2bn(&secp521r1_field, "01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" "FFFF"); BN_hex2bn(&sect163r2_field, "08000000000000000000000000000000" "00000000C9"); secp521r1_group = EC_GROUP_new_by_curve_name(NID_secp521r1); if (BN_cmp(secp521r1_field, EC_GROUP_get0_field(secp521r1_group))) r = 0; # ifndef OPENSSL_NO_EC2M sect163r2_group = EC_GROUP_new_by_curve_name(NID_sect163r2); if (BN_cmp(sect163r2_field, EC_GROUP_get0_field(sect163r2_group))) r = 0; # endif EC_GROUP_free(secp521r1_group); EC_GROUP_free(sect163r2_group); BN_free(secp521r1_field); BN_free(sect163r2_field); return r; }
['static int group_field_test(void)\n{\n int r = 1;\n BIGNUM *secp521r1_field = NULL;\n BIGNUM *sect163r2_field = NULL;\n EC_GROUP *secp521r1_group = NULL;\n EC_GROUP *sect163r2_group = NULL;\n BN_hex2bn(&secp521r1_field,\n "01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFF");\n BN_hex2bn(&sect163r2_field,\n "08000000000000000000000000000000"\n "00000000C9");\n secp521r1_group = EC_GROUP_new_by_curve_name(NID_secp521r1);\n if (BN_cmp(secp521r1_field, EC_GROUP_get0_field(secp521r1_group)))\n r = 0;\n # ifndef OPENSSL_NO_EC2M\n sect163r2_group = EC_GROUP_new_by_curve_name(NID_sect163r2);\n if (BN_cmp(sect163r2_field, EC_GROUP_get0_field(sect163r2_group)))\n r = 0;\n # endif\n EC_GROUP_free(secp521r1_group);\n EC_GROUP_free(sect163r2_group);\n BN_free(secp521r1_field);\n BN_free(sect163r2_field);\n return r;\n}', 'EC_GROUP *EC_GROUP_new_by_curve_name(int nid)\n{\n size_t i;\n EC_GROUP *ret = NULL;\n if (nid <= 0)\n return NULL;\n#ifdef FIPS_MODE\n if (EC_curve_nid2nist(nid) == NULL)\n return NULL;\n#endif\n for (i = 0; i < curve_list_length; i++)\n if (curve_list[i].nid == nid) {\n ret = ec_group_new_from_data(curve_list[i]);\n break;\n }\n if (ret == NULL) {\n ECerr(EC_F_EC_GROUP_NEW_BY_CURVE_NAME, EC_R_UNKNOWN_GROUP);\n return NULL;\n }\n return ret;\n}', 'void ERR_put_error(int lib, int func, int reason, const char *file, int line)\n{\n c_put_error(lib, func, reason, file, line);\n ERR_add_error_data(1, "(in the FIPS module)");\n}', 'const BIGNUM *EC_GROUP_get0_field(const EC_GROUP *group)\n{\n return group->field;\n}']
36,457
0
https://github.com/openssl/openssl/blob/5dfc369ffcdc4722482c818e6ba6cf6e704c2cb5/crypto/idea/i_skey.c/#L87
void idea_set_encrypt_key(unsigned char *key, IDEA_KEY_SCHEDULE *ks) { int i; register IDEA_INT *kt,*kf,r0,r1,r2; kt= &(ks->data[0][0]); n2s(key,kt[0]); n2s(key,kt[1]); n2s(key,kt[2]); n2s(key,kt[3]); n2s(key,kt[4]); n2s(key,kt[5]); n2s(key,kt[6]); n2s(key,kt[7]); kf=kt; kt+=8; for (i=0; i<6; i++) { r2= kf[1]; r1= kf[2]; *(kt++)= ((r2<<9) | (r1>>7))&0xffff; r0= kf[3]; *(kt++)= ((r1<<9) | (r0>>7))&0xffff; r1= kf[4]; *(kt++)= ((r0<<9) | (r1>>7))&0xffff; r0= kf[5]; *(kt++)= ((r1<<9) | (r0>>7))&0xffff; r1= kf[6]; *(kt++)= ((r0<<9) | (r1>>7))&0xffff; r0= kf[7]; *(kt++)= ((r1<<9) | (r0>>7))&0xffff; r1= kf[0]; if (i >= 5) break; *(kt++)= ((r0<<9) | (r1>>7))&0xffff; *(kt++)= ((r1<<9) | (r2>>7))&0xffff; kf+=8; } }
['void idea_set_encrypt_key(unsigned char *key, IDEA_KEY_SCHEDULE *ks)\n\t{\n\tint i;\n\tregister IDEA_INT *kt,*kf,r0,r1,r2;\n\tkt= &(ks->data[0][0]);\n\tn2s(key,kt[0]); n2s(key,kt[1]); n2s(key,kt[2]); n2s(key,kt[3]);\n\tn2s(key,kt[4]); n2s(key,kt[5]); n2s(key,kt[6]); n2s(key,kt[7]);\n\tkf=kt;\n\tkt+=8;\n\tfor (i=0; i<6; i++)\n\t\t{\n\t\tr2= kf[1];\n\t\tr1= kf[2];\n\t\t*(kt++)= ((r2<<9) | (r1>>7))&0xffff;\n\t\tr0= kf[3];\n\t\t*(kt++)= ((r1<<9) | (r0>>7))&0xffff;\n\t\tr1= kf[4];\n\t\t*(kt++)= ((r0<<9) | (r1>>7))&0xffff;\n\t\tr0= kf[5];\n\t\t*(kt++)= ((r1<<9) | (r0>>7))&0xffff;\n\t\tr1= kf[6];\n\t\t*(kt++)= ((r0<<9) | (r1>>7))&0xffff;\n\t\tr0= kf[7];\n\t\t*(kt++)= ((r1<<9) | (r0>>7))&0xffff;\n\t\tr1= kf[0];\n\t\tif (i >= 5) break;\n\t\t*(kt++)= ((r0<<9) | (r1>>7))&0xffff;\n\t\t*(kt++)= ((r1<<9) | (r2>>7))&0xffff;\n\t\tkf+=8;\n\t\t}\n\t}']
36,458
0
https://github.com/openssl/openssl/blob/562fd0d883053f6f62d97439d65cda03a3a1e25d/crypto/lhash/lhash.c/#L240
void *lh_delete(_LHASH *lh, const void *data) { unsigned long hash; LHASH_NODE *nn,**rn; void *ret; lh->error=0; rn=getrn(lh,data,&hash); if (*rn == NULL) { lh->num_no_delete++; return(NULL); } else { nn= *rn; *rn=nn->next; ret=nn->data; OPENSSL_free(nn); lh->num_delete++; } lh->num_items--; if ((lh->num_nodes > MIN_NODES) && (lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes))) contract(lh); return(ret); }
['int dtls1_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek)\n\t{\n\tint al,i,j,ret;\n\tunsigned int n;\n\tSSL3_RECORD *rr;\n\tvoid (*cb)(const SSL *ssl,int type2,int val)=NULL;\n\tif (s->s3->rbuf.buf == NULL)\n\t\tif (!ssl3_setup_buffers(s))\n\t\t\treturn(-1);\n\tif ((type && (type != SSL3_RT_APPLICATION_DATA) &&\n\t\t(type != SSL3_RT_HANDSHAKE) && type) ||\n\t (peek && (type != SSL3_RT_APPLICATION_DATA)))\n\t\t{\n\t\tSSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR);\n\t\treturn -1;\n\t\t}\n\tif ( (ret = have_handshake_fragment(s, type, buf, len, peek)))\n\t\treturn ret;\n#ifndef OPENSSL_NO_SCTP\n\tif ((!s->in_handshake && SSL_in_init(s)) ||\n\t (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&\n\t (s->state == DTLS1_SCTP_ST_SR_READ_SOCK || s->state == DTLS1_SCTP_ST_CR_READ_SOCK) &&\n\t s->s3->in_read_app_data != 2))\n#else\n\tif (!s->in_handshake && SSL_in_init(s))\n#endif\n\t\t{\n\t\ti=s->handshake_func(s);\n\t\tif (i < 0) return(i);\n\t\tif (i == 0)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE);\n\t\t\treturn(-1);\n\t\t\t}\n\t\t}\nstart:\n\ts->rwstate=SSL_NOTHING;\n\trr = &(s->s3->rrec);\n\tif (s->state == SSL_ST_OK && rr->length == 0)\n\t\t{\n\t\tpitem *item;\n\t\titem = pqueue_pop(s->d1->buffered_app_data.q);\n\t\tif (item)\n\t\t\t{\n#ifndef OPENSSL_NO_SCTP\n\t\t\tif (BIO_dgram_is_sctp(SSL_get_rbio(s)))\n\t\t\t\t{\n\t\t\t\tDTLS1_RECORD_DATA *rdata = (DTLS1_RECORD_DATA *) item->data;\n\t\t\t\tBIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SCTP_SET_RCVINFO, sizeof(rdata->recordinfo), &rdata->recordinfo);\n\t\t\t\t}\n#endif\n\t\t\tdtls1_copy_record(s, item);\n\t\t\tOPENSSL_free(item->data);\n\t\t\tpitem_free(item);\n\t\t\t}\n\t\t}\n\tif (dtls1_handle_timeout(s) > 0)\n\t\tgoto start;\n\tif ((rr->length == 0) || (s->rstate == SSL_ST_READ_BODY))\n\t\t{\n\t\tret=dtls1_get_record(s);\n\t\tif (ret <= 0)\n\t\t\t{\n\t\t\tret = dtls1_read_failed(s, ret);\n\t\t\tif (ret <= 0)\n\t\t\t\treturn(ret);\n\t\t\telse\n\t\t\t\tgoto start;\n\t\t\t}\n\t\t}\n\tif (s->d1->listen && rr->type != SSL3_RT_HANDSHAKE)\n\t\t{\n\t\trr->length = 0;\n\t\tgoto start;\n\t\t}\n\tif (s->s3->change_cipher_spec\n\t\t&& (rr->type != SSL3_RT_HANDSHAKE))\n\t\t{\n\t\tdtls1_buffer_record(s, &(s->d1->buffered_app_data), rr->seq_num);\n\t\trr->length = 0;\n\t\tgoto start;\n\t\t}\n\tif (s->shutdown & SSL_RECEIVED_SHUTDOWN)\n\t\t{\n\t\trr->length=0;\n\t\ts->rwstate=SSL_NOTHING;\n\t\treturn(0);\n\t\t}\n\tif (type == rr->type)\n\t\t{\n\t\tif (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) &&\n\t\t\t(s->enc_read_ctx == NULL))\n\t\t\t{\n\t\t\tal=SSL_AD_UNEXPECTED_MESSAGE;\n\t\t\tSSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_APP_DATA_IN_HANDSHAKE);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tif (len <= 0) return(len);\n\t\tif ((unsigned int)len > rr->length)\n\t\t\tn = rr->length;\n\t\telse\n\t\t\tn = (unsigned int)len;\n\t\tmemcpy(buf,&(rr->data[rr->off]),n);\n\t\tif (!peek)\n\t\t\t{\n\t\t\trr->length-=n;\n\t\t\trr->off+=n;\n\t\t\tif (rr->length == 0)\n\t\t\t\t{\n\t\t\t\ts->rstate=SSL_ST_READ_HEADER;\n\t\t\t\trr->off=0;\n\t\t\t\t}\n\t\t\t}\n#ifndef OPENSSL_NO_SCTP\n\t\t\tif (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&\n\t\t\t rr->type == SSL3_RT_APPLICATION_DATA &&\n\t\t\t (s->state == DTLS1_SCTP_ST_SR_READ_SOCK || s->state == DTLS1_SCTP_ST_CR_READ_SOCK))\n\t\t\t\t{\n\t\t\t\ts->rwstate=SSL_READING;\n\t\t\t\tBIO_clear_retry_flags(SSL_get_rbio(s));\n\t\t\t\tBIO_set_retry_read(SSL_get_rbio(s));\n\t\t\t\t}\n\t\t\tif (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&\n\t\t\t s->d1->shutdown_received && !BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s)))\n\t\t\t\t{\n\t\t\t\ts->shutdown |= SSL_RECEIVED_SHUTDOWN;\n\t\t\t\treturn(0);\n\t\t\t\t}\n#endif\n\t\treturn(n);\n\t\t}\n\t\t{\n\t\tunsigned int k, dest_maxlen = 0;\n\t\tunsigned char *dest = NULL;\n\t\tunsigned int *dest_len = NULL;\n\t\tif (rr->type == SSL3_RT_HANDSHAKE)\n\t\t\t{\n\t\t\tdest_maxlen = sizeof s->d1->handshake_fragment;\n\t\t\tdest = s->d1->handshake_fragment;\n\t\t\tdest_len = &s->d1->handshake_fragment_len;\n\t\t\t}\n\t\telse if (rr->type == SSL3_RT_ALERT)\n\t\t\t{\n\t\t\tdest_maxlen = sizeof(s->d1->alert_fragment);\n\t\t\tdest = s->d1->alert_fragment;\n\t\t\tdest_len = &s->d1->alert_fragment_len;\n\t\t\t}\n#ifndef OPENSSL_NO_HEARTBEATS\n\t\telse if (rr->type == TLS1_RT_HEARTBEAT)\n\t\t\t{\n\t\t\tdtls1_process_heartbeat(s);\n\t\t\trr->length = 0;\n\t\t\ts->rwstate=SSL_READING;\n\t\t\tBIO_clear_retry_flags(SSL_get_rbio(s));\n\t\t\tBIO_set_retry_read(SSL_get_rbio(s));\n\t\t\treturn(-1);\n\t\t\t}\n#endif\n\t\telse if (rr->type != SSL3_RT_CHANGE_CIPHER_SPEC)\n\t\t\t{\n\t\t\tif (rr->type == SSL3_RT_APPLICATION_DATA)\n\t\t\t\t{\n\t\t\t\tBIO *bio;\n\t\t\t\ts->s3->in_read_app_data=2;\n\t\t\t\tbio=SSL_get_rbio(s);\n\t\t\t\ts->rwstate=SSL_READING;\n\t\t\t\tBIO_clear_retry_flags(bio);\n\t\t\t\tBIO_set_retry_read(bio);\n\t\t\t\treturn(-1);\n\t\t\t\t}\n\t\t\tal=SSL_AD_UNEXPECTED_MESSAGE;\n\t\t\tSSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_UNEXPECTED_RECORD);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tif (dest_maxlen > 0)\n\t\t\t{\n\t\t\tif ( rr->length < dest_maxlen)\n\t\t\t\t{\n#ifdef DTLS1_AD_MISSING_HANDSHAKE_MESSAGE\n\t\t\t\tFIX ME\n#endif\n\t\t\t\ts->rstate=SSL_ST_READ_HEADER;\n\t\t\t\trr->length = 0;\n\t\t\t\tgoto start;\n\t\t\t\t}\n\t\t\tfor ( k = 0; k < dest_maxlen; k++)\n\t\t\t\t{\n\t\t\t\tdest[k] = rr->data[rr->off++];\n\t\t\t\trr->length--;\n\t\t\t\t}\n\t\t\t*dest_len = dest_maxlen;\n\t\t\t}\n\t\t}\n\tif ((!s->server) &&\n\t\t(s->d1->handshake_fragment_len >= DTLS1_HM_HEADER_LENGTH) &&\n\t\t(s->d1->handshake_fragment[0] == SSL3_MT_HELLO_REQUEST) &&\n\t\t(s->session != NULL) && (s->session->cipher != NULL))\n\t\t{\n\t\ts->d1->handshake_fragment_len = 0;\n\t\tif ((s->d1->handshake_fragment[1] != 0) ||\n\t\t\t(s->d1->handshake_fragment[2] != 0) ||\n\t\t\t(s->d1->handshake_fragment[3] != 0))\n\t\t\t{\n\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\tSSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_BAD_HELLO_REQUEST);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (s->msg_callback)\n\t\t\ts->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,\n\t\t\t\ts->d1->handshake_fragment, 4, s, s->msg_callback_arg);\n\t\tif (SSL_is_init_finished(s) &&\n\t\t\t!(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS) &&\n\t\t\t!s->s3->renegotiate)\n\t\t\t{\n\t\t\ts->d1->handshake_read_seq++;\n\t\t\ts->new_session = 1;\n\t\t\tssl3_renegotiate(s);\n\t\t\tif (ssl3_renegotiate_check(s))\n\t\t\t\t{\n\t\t\t\ti=s->handshake_func(s);\n\t\t\t\tif (i < 0) return(i);\n\t\t\t\tif (i == 0)\n\t\t\t\t\t{\n\t\t\t\t\tSSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE);\n\t\t\t\t\treturn(-1);\n\t\t\t\t\t}\n\t\t\t\tif (!(s->mode & SSL_MODE_AUTO_RETRY))\n\t\t\t\t\t{\n\t\t\t\t\tif (s->s3->rbuf.left == 0)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tBIO *bio;\n\t\t\t\t\t\ts->rwstate=SSL_READING;\n\t\t\t\t\t\tbio=SSL_get_rbio(s);\n\t\t\t\t\t\tBIO_clear_retry_flags(bio);\n\t\t\t\t\t\tBIO_set_retry_read(bio);\n\t\t\t\t\t\treturn(-1);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\tgoto start;\n\t\t}\n\tif (s->d1->alert_fragment_len >= DTLS1_AL_HEADER_LENGTH)\n\t\t{\n\t\tint alert_level = s->d1->alert_fragment[0];\n\t\tint alert_descr = s->d1->alert_fragment[1];\n\t\ts->d1->alert_fragment_len = 0;\n\t\tif (s->msg_callback)\n\t\t\ts->msg_callback(0, s->version, SSL3_RT_ALERT,\n\t\t\t\ts->d1->alert_fragment, 2, s, s->msg_callback_arg);\n\t\tif (s->info_callback != NULL)\n\t\t\tcb=s->info_callback;\n\t\telse if (s->ctx->info_callback != NULL)\n\t\t\tcb=s->ctx->info_callback;\n\t\tif (cb != NULL)\n\t\t\t{\n\t\t\tj = (alert_level << 8) | alert_descr;\n\t\t\tcb(s, SSL_CB_READ_ALERT, j);\n\t\t\t}\n\t\tif (alert_level == 1)\n\t\t\t{\n\t\t\ts->s3->warn_alert = alert_descr;\n\t\t\tif (alert_descr == SSL_AD_CLOSE_NOTIFY)\n\t\t\t\t{\n#ifndef OPENSSL_NO_SCTP\n\t\t\t\tif (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&\n\t\t\t\t\tBIO_dgram_sctp_msg_waiting(SSL_get_rbio(s)))\n\t\t\t\t\t{\n\t\t\t\t\ts->d1->shutdown_received = 1;\n\t\t\t\t\ts->rwstate=SSL_READING;\n\t\t\t\t\tBIO_clear_retry_flags(SSL_get_rbio(s));\n\t\t\t\t\tBIO_set_retry_read(SSL_get_rbio(s));\n\t\t\t\t\treturn -1;\n\t\t\t\t\t}\n#endif\n\t\t\t\ts->shutdown |= SSL_RECEIVED_SHUTDOWN;\n\t\t\t\treturn(0);\n\t\t\t\t}\n#if 0\n\t\t\tif (alert_descr == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE)\n\t\t\t\t{\n\t\t\t\tunsigned short seq;\n\t\t\t\tunsigned int frag_off;\n\t\t\t\tunsigned char *p = &(s->d1->alert_fragment[2]);\n\t\t\t\tn2s(p, seq);\n\t\t\t\tn2l3(p, frag_off);\n\t\t\t\tdtls1_retransmit_message(s,\n\t\t\t\t\t\t\t\t\t\t dtls1_get_queue_priority(frag->msg_header.seq, 0),\n\t\t\t\t\t\t\t\t\t\t frag_off, &found);\n\t\t\t\tif ( ! found && SSL_in_init(s))\n\t\t\t\t\t{\n\t\t\t\t\tssl3_send_alert(s,SSL3_AL_WARNING,\n\t\t\t\t\t\tDTLS1_AD_MISSING_HANDSHAKE_MESSAGE);\n\t\t\t\t\t}\n\t\t\t\t}\n#endif\n\t\t\t}\n\t\telse if (alert_level == 2)\n\t\t\t{\n\t\t\tchar tmp[16];\n\t\t\ts->rwstate=SSL_NOTHING;\n\t\t\ts->s3->fatal_alert = alert_descr;\n\t\t\tSSLerr(SSL_F_DTLS1_READ_BYTES, SSL_AD_REASON_OFFSET + alert_descr);\n\t\t\tBIO_snprintf(tmp,sizeof tmp,"%d",alert_descr);\n\t\t\tERR_add_error_data(2,"SSL alert number ",tmp);\n\t\t\ts->shutdown|=SSL_RECEIVED_SHUTDOWN;\n\t\t\tSSL_CTX_remove_session(s->ctx,s->session);\n\t\t\treturn(0);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tal=SSL_AD_ILLEGAL_PARAMETER;\n\t\t\tSSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_UNKNOWN_ALERT_TYPE);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tgoto start;\n\t\t}\n\tif (s->shutdown & SSL_SENT_SHUTDOWN)\n\t\t{\n\t\ts->rwstate=SSL_NOTHING;\n\t\trr->length=0;\n\t\treturn(0);\n\t\t}\n\tif (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC)\n\t\t{\n\t\tstruct ccs_header_st ccs_hdr;\n\t\tunsigned int ccs_hdr_len = DTLS1_CCS_HEADER_LENGTH;\n\t\tdtls1_get_ccs_header(rr->data, &ccs_hdr);\n\t\tif (s->version == DTLS1_BAD_VER)\n\t\t\tccs_hdr_len = 3;\n\t\tif (\t(rr->length != ccs_hdr_len) ||\n\t\t\t(rr->off != 0) || (rr->data[0] != SSL3_MT_CCS))\n\t\t\t{\n\t\t\ti=SSL_AD_ILLEGAL_PARAMETER;\n\t\t\tSSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_BAD_CHANGE_CIPHER_SPEC);\n\t\t\tgoto err;\n\t\t\t}\n\t\trr->length=0;\n\t\tif (s->msg_callback)\n\t\t\ts->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC,\n\t\t\t\trr->data, 1, s, s->msg_callback_arg);\n\t\tif (!s->d1->change_cipher_spec_ok)\n\t\t\t{\n\t\t\tgoto start;\n\t\t\t}\n\t\ts->d1->change_cipher_spec_ok = 0;\n\t\ts->s3->change_cipher_spec=1;\n\t\tif (!ssl3_do_change_cipher_spec(s))\n\t\t\tgoto err;\n\t\tdtls1_reset_seq_numbers(s, SSL3_CC_READ);\n\t\tif (s->version == DTLS1_BAD_VER)\n\t\t\ts->d1->handshake_read_seq++;\n#ifndef OPENSSL_NO_SCTP\n\t\tBIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD, 1, NULL);\n#endif\n\t\tgoto start;\n\t\t}\n\tif ((s->d1->handshake_fragment_len >= DTLS1_HM_HEADER_LENGTH) &&\n\t\t!s->in_handshake)\n\t\t{\n\t\tstruct hm_header_st msg_hdr;\n\t\tdtls1_get_message_header(rr->data, &msg_hdr);\n\t\tif( rr->epoch != s->d1->r_epoch)\n\t\t\t{\n\t\t\trr->length = 0;\n\t\t\tgoto start;\n\t\t\t}\n\t\tif (msg_hdr.type == SSL3_MT_FINISHED)\n\t\t\t{\n\t\t\tif (dtls1_check_timeout_num(s) < 0)\n\t\t\t\treturn -1;\n\t\t\tdtls1_retransmit_buffered_messages(s);\n\t\t\trr->length = 0;\n\t\t\tgoto start;\n\t\t\t}\n\t\tif (((s->state&SSL_ST_MASK) == SSL_ST_OK) &&\n\t\t\t!(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS))\n\t\t\t{\n#if 0\n\t\t\ts->state=SSL_ST_BEFORE|(s->server)\n\t\t\t\t?SSL_ST_ACCEPT\n\t\t\t\t:SSL_ST_CONNECT;\n#else\n\t\t\ts->state = s->server ? SSL_ST_ACCEPT : SSL_ST_CONNECT;\n#endif\n\t\t\ts->renegotiate=1;\n\t\t\ts->new_session=1;\n\t\t\t}\n\t\ti=s->handshake_func(s);\n\t\tif (i < 0) return(i);\n\t\tif (i == 0)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE);\n\t\t\treturn(-1);\n\t\t\t}\n\t\tif (!(s->mode & SSL_MODE_AUTO_RETRY))\n\t\t\t{\n\t\t\tif (s->s3->rbuf.left == 0)\n\t\t\t\t{\n\t\t\t\tBIO *bio;\n\t\t\t\ts->rwstate=SSL_READING;\n\t\t\t\tbio=SSL_get_rbio(s);\n\t\t\t\tBIO_clear_retry_flags(bio);\n\t\t\t\tBIO_set_retry_read(bio);\n\t\t\t\treturn(-1);\n\t\t\t\t}\n\t\t\t}\n\t\tgoto start;\n\t\t}\n\tswitch (rr->type)\n\t\t{\n\tdefault:\n#ifndef OPENSSL_NO_TLS\n\t\tif (s->version == TLS1_VERSION)\n\t\t\t{\n\t\t\trr->length = 0;\n\t\t\tgoto start;\n\t\t\t}\n#endif\n\t\tal=SSL_AD_UNEXPECTED_MESSAGE;\n\t\tSSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_UNEXPECTED_RECORD);\n\t\tgoto f_err;\n\tcase SSL3_RT_CHANGE_CIPHER_SPEC:\n\tcase SSL3_RT_ALERT:\n\tcase SSL3_RT_HANDSHAKE:\n\t\tal=SSL_AD_UNEXPECTED_MESSAGE;\n\t\tSSLerr(SSL_F_DTLS1_READ_BYTES,ERR_R_INTERNAL_ERROR);\n\t\tgoto f_err;\n\tcase SSL3_RT_APPLICATION_DATA:\n\t\tif (s->s3->in_read_app_data &&\n\t\t\t(s->s3->total_renegotiations != 0) &&\n\t\t\t((\n\t\t\t\t(s->state & SSL_ST_CONNECT) &&\n\t\t\t\t(s->state >= SSL3_ST_CW_CLNT_HELLO_A) &&\n\t\t\t\t(s->state <= SSL3_ST_CR_SRVR_HELLO_A)\n\t\t\t\t) || (\n\t\t\t\t\t(s->state & SSL_ST_ACCEPT) &&\n\t\t\t\t\t(s->state <= SSL3_ST_SW_HELLO_REQ_A) &&\n\t\t\t\t\t(s->state >= SSL3_ST_SR_CLNT_HELLO_A)\n\t\t\t\t\t)\n\t\t\t\t))\n\t\t\t{\n\t\t\ts->s3->in_read_app_data=2;\n\t\t\treturn(-1);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tal=SSL_AD_UNEXPECTED_MESSAGE;\n\t\t\tSSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_UNEXPECTED_RECORD);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\t}\nf_err:\n\tssl3_send_alert(s,SSL3_AL_FATAL,al);\nerr:\n\treturn(-1);\n\t}', 'int ssl3_send_alert(SSL *s, int level, int desc)\n\t{\n\tdesc=s->method->ssl3_enc->alert_value(desc);\n\tif (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION)\n\t\tdesc = SSL_AD_HANDSHAKE_FAILURE;\n\tif (desc < 0) return -1;\n\tif ((level == SSL3_AL_FATAL) && (s->session != NULL))\n\t\tSSL_CTX_remove_session(s->ctx,s->session);\n\ts->s3->alert_dispatch=1;\n\ts->s3->send_alert[0]=level;\n\ts->s3->send_alert[1]=desc;\n\tif (s->s3->wbuf.left == 0)\n\t\treturn s->method->ssl_dispatch_alert(s);\n\treturn -1;\n\t}', 'int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)\n{\n\treturn remove_session_lock(ctx, c, 1);\n}', 'static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)\n\t{\n\tSSL_SESSION *r;\n\tint ret=0;\n\tif ((c != NULL) && (c->session_id_length != 0))\n\t\t{\n\t\tif(lck) CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);\n\t\tif ((r = lh_SSL_SESSION_retrieve(ctx->sessions,c)) == c)\n\t\t\t{\n\t\t\tret=1;\n\t\t\tr=lh_SSL_SESSION_delete(ctx->sessions,c);\n\t\t\tSSL_SESSION_list_remove(ctx,c);\n\t\t\t}\n\t\tif(lck) CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n\t\tif (ret)\n\t\t\t{\n\t\t\tr->not_resumable=1;\n\t\t\tif (ctx->remove_session_cb != NULL)\n\t\t\t\tctx->remove_session_cb(ctx,r);\n\t\t\tSSL_SESSION_free(r);\n\t\t\t}\n\t\t}\n\telse\n\t\tret=0;\n\treturn(ret);\n\t}', 'void *lh_delete(_LHASH *lh, const void *data)\n\t{\n\tunsigned long hash;\n\tLHASH_NODE *nn,**rn;\n\tvoid *ret;\n\tlh->error=0;\n\trn=getrn(lh,data,&hash);\n\tif (*rn == NULL)\n\t\t{\n\t\tlh->num_no_delete++;\n\t\treturn(NULL);\n\t\t}\n\telse\n\t\t{\n\t\tnn= *rn;\n\t\t*rn=nn->next;\n\t\tret=nn->data;\n\t\tOPENSSL_free(nn);\n\t\tlh->num_delete++;\n\t\t}\n\tlh->num_items--;\n\tif ((lh->num_nodes > MIN_NODES) &&\n\t\t(lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes)))\n\t\tcontract(lh);\n\treturn(ret);\n\t}']
36,459
0
https://github.com/openssl/openssl/blob/305b68f1a2b6d4d0aa07a6ab47ac372f067a40bb/crypto/bn/bn_lib.c/#L231
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *a = NULL; if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return NULL; } if (BN_get_flags(b, BN_FLG_SECURE)) a = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = OPENSSL_zalloc(words * sizeof(*a)); if (a == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return NULL; } assert(b->top <= words); if (b->top > 0) memcpy(a, b->d, sizeof(*a) * b->top); return a; }
['static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)\n{\n BIGNUM *r1, *m1, *vrfy, *r2, *m[RSA_MAX_PRIME_NUM - 2];\n int ret = 0, i, ex_primes = 0;\n RSA_PRIME_INFO *pinfo;\n BN_CTX_start(ctx);\n r1 = BN_CTX_get(ctx);\n r2 = BN_CTX_get(ctx);\n m1 = BN_CTX_get(ctx);\n vrfy = BN_CTX_get(ctx);\n if (vrfy == NULL)\n goto err;\n if (rsa->version == RSA_ASN1_VERSION_MULTI\n && ((ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) <= 0\n || ex_primes > RSA_MAX_PRIME_NUM - 2))\n goto err;\n {\n BIGNUM *p = BN_new(), *q = BN_new();\n if (p == NULL || q == NULL) {\n BN_free(p);\n BN_free(q);\n goto err;\n }\n BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);\n BN_with_flags(q, rsa->q, BN_FLG_CONSTTIME);\n if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {\n if (!BN_MONT_CTX_set_locked\n (&rsa->_method_mod_p, rsa->lock, p, ctx)\n || !BN_MONT_CTX_set_locked(&rsa->_method_mod_q,\n rsa->lock, q, ctx)) {\n BN_free(p);\n BN_free(q);\n goto err;\n }\n if (ex_primes > 0) {\n BIGNUM *r = BN_new();\n if (r == NULL) {\n BN_free(p);\n BN_free(q);\n goto err;\n }\n for (i = 0; i < ex_primes; i++) {\n pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);\n BN_with_flags(r, pinfo->r, BN_FLG_CONSTTIME);\n if (!BN_MONT_CTX_set_locked(&pinfo->m, rsa->lock, r, ctx)) {\n BN_free(p);\n BN_free(q);\n BN_free(r);\n goto err;\n }\n }\n BN_free(r);\n }\n }\n BN_free(p);\n BN_free(q);\n }\n if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)\n if (!BN_MONT_CTX_set_locked\n (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx))\n goto err;\n {\n BIGNUM *c = BN_new();\n if (c == NULL)\n goto err;\n BN_with_flags(c, I, BN_FLG_CONSTTIME);\n if (!BN_mod(r1, c, rsa->q, ctx)) {\n BN_free(c);\n goto err;\n }\n {\n BIGNUM *dmq1 = BN_new();\n if (dmq1 == NULL) {\n BN_free(c);\n goto err;\n }\n BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);\n if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx,\n rsa->_method_mod_q)) {\n BN_free(c);\n BN_free(dmq1);\n goto err;\n }\n BN_free(dmq1);\n }\n if (!BN_mod(r1, c, rsa->p, ctx)) {\n BN_free(c);\n goto err;\n }\n BN_free(c);\n }\n {\n BIGNUM *dmp1 = BN_new();\n if (dmp1 == NULL)\n goto err;\n BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);\n if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx,\n rsa->_method_mod_p)) {\n BN_free(dmp1);\n goto err;\n }\n BN_free(dmp1);\n }\n if (ex_primes > 0) {\n BIGNUM *di = BN_new(), *cc = BN_new();\n if (cc == NULL || di == NULL) {\n BN_free(cc);\n BN_free(di);\n goto err;\n }\n for (i = 0; i < ex_primes; i++) {\n if ((m[i] = BN_CTX_get(ctx)) == NULL) {\n BN_free(cc);\n BN_free(di);\n goto err;\n }\n pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);\n BN_with_flags(cc, I, BN_FLG_CONSTTIME);\n BN_with_flags(di, pinfo->d, BN_FLG_CONSTTIME);\n if (!BN_mod(r1, cc, pinfo->r, ctx)) {\n BN_free(cc);\n BN_free(di);\n goto err;\n }\n if (!rsa->meth->bn_mod_exp(m[i], r1, di, pinfo->r, ctx, pinfo->m)) {\n BN_free(cc);\n BN_free(di);\n goto err;\n }\n }\n BN_free(cc);\n BN_free(di);\n }\n if (!BN_sub(r0, r0, m1))\n goto err;\n if (BN_is_negative(r0))\n if (!BN_add(r0, r0, rsa->p))\n goto err;\n if (!BN_mul(r1, r0, rsa->iqmp, ctx))\n goto err;\n {\n BIGNUM *pr1 = BN_new();\n if (pr1 == NULL)\n goto err;\n BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);\n if (!BN_mod(r0, pr1, rsa->p, ctx)) {\n BN_free(pr1);\n goto err;\n }\n BN_free(pr1);\n }\n if (BN_is_negative(r0))\n if (!BN_add(r0, r0, rsa->p))\n goto err;\n if (!BN_mul(r1, r0, rsa->q, ctx))\n goto err;\n if (!BN_add(r0, r1, m1))\n goto err;\n if (ex_primes > 0) {\n BIGNUM *pr2 = BN_new();\n if (pr2 == NULL)\n goto err;\n for (i = 0; i < ex_primes; i++) {\n pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);\n if (!BN_sub(r1, m[i], r0)) {\n BN_free(pr2);\n goto err;\n }\n if (!BN_mul(r2, r1, pinfo->t, ctx)) {\n BN_free(pr2);\n goto err;\n }\n BN_with_flags(pr2, r2, BN_FLG_CONSTTIME);\n if (!BN_mod(r1, pr2, pinfo->r, ctx)) {\n BN_free(pr2);\n goto err;\n }\n if (BN_is_negative(r1))\n if (!BN_add(r1, r1, pinfo->r)) {\n BN_free(pr2);\n goto err;\n }\n if (!BN_mul(r1, r1, pinfo->pp, ctx)) {\n BN_free(pr2);\n goto err;\n }\n if (!BN_add(r0, r0, r1)) {\n BN_free(pr2);\n goto err;\n }\n }\n BN_free(pr2);\n }\n if (rsa->e && rsa->n) {\n if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,\n rsa->_method_mod_n))\n goto err;\n if (!BN_sub(vrfy, vrfy, I))\n goto err;\n if (!BN_mod(vrfy, vrfy, rsa->n, ctx))\n goto err;\n if (BN_is_negative(vrfy))\n if (!BN_add(vrfy, vrfy, rsa->n))\n goto err;\n if (!BN_is_zero(vrfy)) {\n BIGNUM *d = BN_new();\n if (d == NULL)\n goto err;\n BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);\n if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx,\n rsa->_method_mod_n)) {\n BN_free(d);\n goto err;\n }\n BN_free(d);\n }\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return 0;\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return 0;\n }\n if (dv != NULL)\n BN_zero(dv);\n return 1;\n }\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.flags = BN_FLG_STATIC_DATA;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n resp++;\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n resp--;\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return 0;\n}', 'int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int ret, r_neg, cmp_res;\n bn_check_top(a);\n bn_check_top(b);\n if (a->neg == b->neg) {\n r_neg = a->neg;\n ret = BN_uadd(r, a, b);\n } else {\n cmp_res = BN_ucmp(a, b);\n if (cmp_res > 0) {\n r_neg = a->neg;\n ret = BN_usub(r, a, b);\n } else if (cmp_res < 0) {\n r_neg = b->neg;\n ret = BN_usub(r, b, a);\n } else {\n r_neg = 0;\n BN_zero(r);\n ret = 1;\n }\n }\n r->neg = r_neg;\n bn_check_top(r);\n return ret;\n}', 'int BN_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}']
36,460
0
https://github.com/openssl/openssl/blob/d4b009d5f88875ac0e3ac0b2b9689ed16a4c88dc/crypto/lhash/lhash.c/#L209
void *lh_delete(_LHASH *lh, const void *data) { unsigned long hash; LHASH_NODE *nn, **rn; void *ret; lh->error = 0; rn = getrn(lh, data, &hash); if (*rn == NULL) { lh->num_no_delete++; return (NULL); } else { nn = *rn; *rn = nn->next; ret = nn->data; OPENSSL_free(nn); lh->num_delete++; } lh->num_items--; if ((lh->num_nodes > MIN_NODES) && (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes))) contract(lh); return (ret); }
['int dtls1_process_buffered_records(SSL *s)\n{\n pitem *item;\n item = pqueue_peek(s->rlayer.d->unprocessed_rcds.q);\n if (item) {\n if (s->rlayer.d->unprocessed_rcds.epoch != s->rlayer.d->r_epoch)\n return (1);\n while (pqueue_peek(s->rlayer.d->unprocessed_rcds.q)) {\n dtls1_get_unprocessed_record(s);\n if (!dtls1_process_record(s))\n return (0);\n if (dtls1_buffer_record(s, &(s->rlayer.d->processed_rcds),\n SSL3_RECORD_get_seq_num(&s->rlayer.rrec)) < 0)\n return -1;\n }\n }\n s->rlayer.d->processed_rcds.epoch = s->rlayer.d->r_epoch;\n s->rlayer.d->unprocessed_rcds.epoch = s->rlayer.d->r_epoch + 1;\n return (1);\n}', 'int dtls1_process_record(SSL *s)\n{\n int i, al;\n int enc_err;\n SSL_SESSION *sess;\n SSL3_RECORD *rr;\n unsigned int mac_size;\n unsigned char md[EVP_MAX_MD_SIZE];\n rr = RECORD_LAYER_get_rrec(&s->rlayer);\n sess = s->session;\n rr->input = &(RECORD_LAYER_get_packet(&s->rlayer)[DTLS1_RT_HEADER_LENGTH]);\n if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {\n al = SSL_AD_RECORD_OVERFLOW;\n SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);\n goto f_err;\n }\n rr->data = rr->input;\n rr->orig_len = rr->length;\n enc_err = s->method->ssl3_enc->enc(s, 0);\n if (enc_err == 0) {\n rr->length = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto err;\n }\n#ifdef TLS_DEBUG\n printf("dec %d\\n", rr->length);\n {\n unsigned int z;\n for (z = 0; z < rr->length; z++)\n printf("%02X%c", rr->data[z], ((z + 1) % 16) ? \' \' : \'\\n\');\n }\n printf("\\n");\n#endif\n if ((sess != NULL) &&\n (s->enc_read_ctx != NULL) && (EVP_MD_CTX_md(s->read_hash) != NULL)) {\n unsigned char *mac = NULL;\n unsigned char mac_tmp[EVP_MAX_MD_SIZE];\n mac_size = EVP_MD_CTX_size(s->read_hash);\n OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);\n if (rr->orig_len < mac_size ||\n (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE &&\n rr->orig_len < mac_size + 1)) {\n al = SSL_AD_DECODE_ERROR;\n SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_LENGTH_TOO_SHORT);\n goto f_err;\n }\n if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) {\n mac = mac_tmp;\n ssl3_cbc_copy_mac(mac_tmp, rr, mac_size);\n rr->length -= mac_size;\n } else {\n rr->length -= mac_size;\n mac = &rr->data[rr->length];\n }\n i = s->method->ssl3_enc->mac(s, md, 0 );\n if (i < 0 || mac == NULL\n || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0)\n enc_err = -1;\n if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)\n enc_err = -1;\n }\n if (enc_err < 0) {\n rr->length = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto err;\n }\n if (s->expand != NULL) {\n if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {\n al = SSL_AD_RECORD_OVERFLOW;\n SSLerr(SSL_F_DTLS1_PROCESS_RECORD,\n SSL_R_COMPRESSED_LENGTH_TOO_LONG);\n goto f_err;\n }\n if (!ssl3_do_uncompress(s)) {\n al = SSL_AD_DECOMPRESSION_FAILURE;\n SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_BAD_DECOMPRESSION);\n goto f_err;\n }\n }\n if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH) {\n al = SSL_AD_RECORD_OVERFLOW;\n SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_DATA_LENGTH_TOO_LONG);\n goto f_err;\n }\n rr->off = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n return (1);\n f_err:\n ssl3_send_alert(s, SSL3_AL_FATAL, al);\n err:\n return (0);\n}', 'int ssl3_send_alert(SSL *s, int level, int desc)\n{\n desc = s->method->ssl3_enc->alert_value(desc);\n if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION)\n desc = SSL_AD_HANDSHAKE_FAILURE;\n if (desc < 0)\n return -1;\n if ((level == SSL3_AL_FATAL) && (s->session != NULL))\n SSL_CTX_remove_session(s->ctx, s->session);\n s->s3->alert_dispatch = 1;\n s->s3->send_alert[0] = level;\n s->s3->send_alert[1] = desc;\n if (!RECORD_LAYER_write_pending(&s->rlayer)) {\n return s->method->ssl_dispatch_alert(s);\n }\n return -1;\n}', 'int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)\n{\n return remove_session_lock(ctx, c, 1);\n}', 'static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)\n{\n SSL_SESSION *r;\n int ret = 0;\n if ((c != NULL) && (c->session_id_length != 0)) {\n if (lck)\n CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);\n if ((r = lh_SSL_SESSION_retrieve(ctx->sessions, c)) == c) {\n ret = 1;\n r = lh_SSL_SESSION_delete(ctx->sessions, c);\n SSL_SESSION_list_remove(ctx, c);\n }\n if (lck)\n CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n if (ret) {\n r->not_resumable = 1;\n if (ctx->remove_session_cb != NULL)\n ctx->remove_session_cb(ctx, r);\n SSL_SESSION_free(r);\n }\n } else\n ret = 0;\n return (ret);\n}', 'DEFINE_LHASH_OF(SSL_SESSION)', 'void *lh_delete(_LHASH *lh, const void *data)\n{\n unsigned long hash;\n LHASH_NODE *nn, **rn;\n void *ret;\n lh->error = 0;\n rn = getrn(lh, data, &hash);\n if (*rn == NULL) {\n lh->num_no_delete++;\n return (NULL);\n } else {\n nn = *rn;\n *rn = nn->next;\n ret = nn->data;\n OPENSSL_free(nn);\n lh->num_delete++;\n }\n lh->num_items--;\n if ((lh->num_nodes > MIN_NODES) &&\n (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))\n contract(lh);\n return (ret);\n}']
36,461
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 BIGNUM *rsa_get_public_exp(const BIGNUM *d, const BIGNUM *p,\n const BIGNUM *q, BN_CTX *ctx)\n{\n BIGNUM *ret = NULL, *r0, *r1, *r2;\n if (d == NULL || p == NULL || q == NULL)\n return NULL;\n BN_CTX_start(ctx);\n r0 = BN_CTX_get(ctx);\n r1 = BN_CTX_get(ctx);\n r2 = BN_CTX_get(ctx);\n if (r2 == NULL)\n goto err;\n if (!BN_sub(r1, p, BN_value_one()))\n goto err;\n if (!BN_sub(r2, q, BN_value_one()))\n goto err;\n if (!BN_mul(r0, r1, r2, ctx))\n goto err;\n ret = BN_mod_inverse(NULL, d, r0, ctx);\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}', 'BIGNUM *BN_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n{\n BIGNUM *rv;\n int noinv;\n rv = int_bn_mod_inverse(in, a, n, ctx, &noinv);\n if (noinv)\n BNerr(BN_F_BN_MOD_INVERSE, BN_R_NO_INVERSE);\n return rv;\n}', 'BIGNUM *int_bn_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,\n int *pnoinv)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM *ret = NULL;\n int sign;\n if (BN_abs_is_word(n, 1) || BN_is_zero(n)) {\n if (pnoinv != NULL)\n *pnoinv = 1;\n return NULL;\n }\n if (pnoinv != NULL)\n *pnoinv = 0;\n if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {\n return BN_mod_inverse_no_branch(in, a, n, ctx);\n }\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n if (!BN_nnmod(B, B, A, ctx))\n goto err;\n }\n sign = -1;\n if (BN_is_odd(n) && (BN_num_bits(n) <= 2048)) {\n int shift;\n while (!BN_is_zero(B)) {\n shift = 0;\n while (!BN_is_bit_set(B, shift)) {\n shift++;\n if (BN_is_odd(X)) {\n if (!BN_uadd(X, X, n))\n goto err;\n }\n if (!BN_rshift1(X, X))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(B, B, shift))\n goto err;\n }\n shift = 0;\n while (!BN_is_bit_set(A, shift)) {\n shift++;\n if (BN_is_odd(Y)) {\n if (!BN_uadd(Y, Y, n))\n goto err;\n }\n if (!BN_rshift1(Y, Y))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(A, A, shift))\n goto err;\n }\n if (BN_ucmp(B, A) >= 0) {\n if (!BN_uadd(X, X, Y))\n goto err;\n if (!BN_usub(B, B, A))\n goto err;\n } else {\n if (!BN_uadd(Y, Y, X))\n goto err;\n if (!BN_usub(A, A, B))\n goto err;\n }\n }\n } else {\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n if (BN_num_bits(A) == BN_num_bits(B)) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else if (BN_num_bits(A) == BN_num_bits(B) + 1) {\n if (!BN_lshift1(T, B))\n goto err;\n if (BN_ucmp(A, T) < 0) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else {\n if (!BN_sub(M, A, T))\n goto err;\n if (!BN_add(D, T, B))\n goto err;\n if (BN_ucmp(A, D) < 0) {\n if (!BN_set_word(D, 2))\n goto err;\n } else {\n if (!BN_set_word(D, 3))\n goto err;\n if (!BN_sub(M, M, B))\n goto err;\n }\n }\n } else {\n if (!BN_div(D, M, A, B, ctx))\n goto err;\n }\n tmp = A;\n A = B;\n B = M;\n if (BN_is_one(D)) {\n if (!BN_add(tmp, X, Y))\n goto err;\n } else {\n if (BN_is_word(D, 2)) {\n if (!BN_lshift1(tmp, X))\n goto err;\n } else if (BN_is_word(D, 4)) {\n if (!BN_lshift(tmp, X, 2))\n goto err;\n } else if (D->top == 1) {\n if (!BN_copy(tmp, X))\n goto err;\n if (!BN_mul_word(tmp, D->d[0]))\n goto err;\n } else {\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n }\n if (!BN_add(tmp, tmp, Y))\n goto err;\n }\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n if (pnoinv)\n *pnoinv = 1;\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return ret;\n}', 'static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n,\n BN_CTX *ctx)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM *ret = NULL;\n int sign;\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n {\n BIGNUM local_B;\n bn_init(&local_B);\n BN_with_flags(&local_B, B, BN_FLG_CONSTTIME);\n if (!BN_nnmod(B, &local_B, A, ctx))\n goto err;\n }\n }\n sign = -1;\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n {\n BIGNUM local_A;\n bn_init(&local_A);\n BN_with_flags(&local_A, A, BN_FLG_CONSTTIME);\n if (!BN_div(D, M, &local_A, B, ctx))\n goto err;\n }\n tmp = A;\n A = B;\n B = M;\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n if (!BN_add(tmp, tmp, Y))\n goto err;\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n BNerr(BN_F_BN_MOD_INVERSE_NO_BRANCH, BN_R_NO_INVERSE);\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return ret;\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int ret;\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return 0;\n }\n if (divisor->d[divisor->top - 1] == 0) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n ret = bn_div_fixed_top(dv, rm, num, divisor, ctx);\n if (ret) {\n if (dv != NULL)\n bn_correct_top(dv);\n if (rm != NULL)\n bn_correct_top(rm);\n }\n return ret;\n}', 'int bn_div_fixed_top(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,\n const BIGNUM *divisor, BN_CTX *ctx)\n{\n int norm_shift, i, j, loop;\n BIGNUM *tmp, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnum, *wnumtop;\n BN_ULONG d0, d1;\n int num_n, div_n;\n assert(divisor->top > 0 && divisor->d[divisor->top - 1] != 0);\n bn_check_top(num);\n bn_check_top(divisor);\n bn_check_top(dv);\n bn_check_top(rm);\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n if (!BN_copy(sdiv, divisor))\n goto err;\n norm_shift = bn_left_align(sdiv);\n sdiv->neg = 0;\n if (!(bn_lshift_fixed_top(snum, num, norm_shift)))\n goto err;\n div_n = sdiv->top;\n num_n = snum->top;\n if (num_n <= div_n) {\n if (bn_wexpand(snum, div_n + 1) == NULL)\n goto err;\n memset(&(snum->d[num_n]), 0, (div_n - num_n + 1) * sizeof(BN_ULONG));\n snum->top = num_n = div_n + 1;\n }\n loop = num_n - div_n;\n wnum = &(snum->d[loop]);\n wnumtop = &(snum->d[num_n - 1]);\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n if (!bn_wexpand(res, loop))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop;\n res->flags |= BN_FLG_FIXED_TOP;\n resp = &(res->d[loop]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n for (i = 0; i < loop; i++, wnumtop--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W)\n q = bn_div_3_words(wnumtop, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnumtop[0];\n n1 = wnumtop[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n BN_ULONG n2 = (wnumtop == wnum) ? 0 : wnumtop[-2];\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | n2))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= n2)))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum--;\n l0 = bn_sub_words(wnum, wnum, tmp->d, div_n + 1);\n q -= l0;\n for (l0 = 0 - l0, j = 0; j < div_n; j++)\n tmp->d[j] = sdiv->d[j] & l0;\n l0 = bn_add_words(wnum, wnum, tmp->d, div_n);\n (*wnumtop) += l0;\n assert((*wnumtop) == 0);\n *--resp = q;\n }\n snum->neg = num->neg;\n snum->top = div_n;\n snum->flags |= BN_FLG_FIXED_TOP;\n if (rm != NULL)\n bn_rshift_fixed_top(rm, snum, norm_shift);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return 0;\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n 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}']
36,462
0
https://github.com/openssl/openssl/blob/92eb4c551d7433ba1e74e77001dab2e256f8a870/crypto/bn/bn_ctx.c/#L355
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)\n\t{\n\tBN_CTX *ctx=NULL;\n\tBN_MONT_CTX *mont=NULL;\n\tBIGNUM *tmp;\n\tint ret= -1;\n int check_result;\n\tif (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS)\n\t\t{\n\t\tDHerr(DH_F_COMPUTE_KEY,DH_R_MODULUS_TOO_LARGE);\n\t\tgoto err;\n\t\t}\n#ifdef OPENSSL_FIPS\n\tif (FIPS_mode() && (BN_num_bits(dh->p) < OPENSSL_DH_FIPS_MIN_MODULUS_BITS))\n\t\t{\n\t\tDHerr(DH_F_COMPUTE_KEY, DH_R_KEY_SIZE_TOO_SMALL);\n\t\tgoto err;\n\t\t}\n#endif\n\tctx = BN_CTX_new();\n\tif (ctx == NULL) goto err;\n\tBN_CTX_start(ctx);\n\ttmp = BN_CTX_get(ctx);\n\tif (dh->priv_key == NULL)\n\t\t{\n\t\tDHerr(DH_F_COMPUTE_KEY,DH_R_NO_PRIVATE_VALUE);\n\t\tgoto err;\n\t\t}\n\tif (dh->flags & DH_FLAG_CACHE_MONT_P)\n\t\t{\n\t\tmont = BN_MONT_CTX_set_locked(&dh->method_mont_p,\n\t\t\t\tCRYPTO_LOCK_DH, dh->p, ctx);\n\t\tif ((dh->flags & DH_FLAG_NO_EXP_CONSTTIME) == 0)\n\t\t\t{\n\t\t\tBN_set_flags(dh->priv_key, BN_FLG_CONSTTIME);\n\t\t\t}\n\t\tif (!mont)\n\t\t\tgoto err;\n\t\t}\n if (!DH_check_pub_key(dh, pub_key, &check_result) || check_result)\n\t\t{\n\t\tDHerr(DH_F_COMPUTE_KEY,DH_R_INVALID_PUBKEY);\n\t\tgoto err;\n\t\t}\n\tif (!dh->meth->bn_mod_exp(dh, tmp, pub_key, dh->priv_key,dh->p,ctx,mont))\n\t\t{\n\t\tDHerr(DH_F_COMPUTE_KEY,ERR_R_BN_LIB);\n\t\tgoto err;\n\t\t}\n\tret=BN_bn2bin(tmp,key);\nerr:\n\tif (ctx != NULL)\n\t\t{\n\t\tBN_CTX_end(ctx);\n\t\tBN_CTX_free(ctx);\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}', 'BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, int lock,\n\t\t\t\t\tconst BIGNUM *mod, BN_CTX *ctx)\n\t{\n\tint got_write_lock = 0;\n\tBN_MONT_CTX *ret;\n\tCRYPTO_r_lock(lock);\n\tif (!*pmont)\n\t\t{\n\t\tCRYPTO_r_unlock(lock);\n\t\tCRYPTO_w_lock(lock);\n\t\tgot_write_lock = 1;\n\t\tif (!*pmont)\n\t\t\t{\n\t\t\tret = BN_MONT_CTX_new();\n\t\t\tif (ret && !BN_MONT_CTX_set(ret, mod, ctx))\n\t\t\t\tBN_MONT_CTX_free(ret);\n\t\t\telse\n\t\t\t\t*pmont = ret;\n\t\t\t}\n\t\t}\n\tret = *pmont;\n\tif (got_write_lock)\n\t\tCRYPTO_w_unlock(lock);\n\telse\n\t\tCRYPTO_r_unlock(lock);\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\tBN_init(&tmod);\n\t\ttmod.d=buf;\n\t\ttmod.dmax=2;\n\t\ttmod.neg=0;\n\t\tmont->ri=(BN_num_bits(mod)+(BN_BITS2-1))/BN_BITS2*BN_BITS2;\n#if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)\n\t\tBN_zero(R);\n\t\tif (!(BN_set_bit(R,2*BN_BITS2))) goto err;\n\t\t\t\t\t\t\t\ttmod.top=0;\n\t\tif ((buf[0] = mod->d[0]))\t\t\ttmod.top=1;\n\t\tif ((buf[1] = mod->top>1 ? mod->d[1] : 0))\ttmod.top=2;\n\t\tif ((BN_mod_inverse(Ri,R,&tmod,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tif (!BN_lshift(Ri,Ri,2*BN_BITS2)) goto err;\n\t\tif (!BN_is_zero(Ri))\n\t\t\t{\n\t\t\tif (!BN_sub_word(Ri,1)) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (bn_expand(Ri,(int)sizeof(BN_ULONG)*2) == NULL)\n\t\t\t\tgoto err;\n\t\t\tRi->neg=0;\n\t\t\tRi->d[0]=BN_MASK2;\n\t\t\tRi->d[1]=BN_MASK2;\n\t\t\tRi->top=2;\n\t\t\t}\n\t\tif (!BN_div(Ri,NULL,Ri,&tmod,ctx)) goto err;\n\t\tmont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n\t\tmont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;\n#else\n\t\tBN_zero(R);\n\t\tif (!(BN_set_bit(R,BN_BITS2))) goto err;\n\t\tbuf[0]=mod->d[0];\n\t\tbuf[1]=0;\n\t\ttmod.top = buf[0] != 0 ? 1 : 0;\n\t\tif ((BN_mod_inverse(Ri,R,&tmod,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tif (!BN_lshift(Ri,Ri,BN_BITS2)) goto err;\n\t\tif (!BN_is_zero(Ri))\n\t\t\t{\n\t\t\tif (!BN_sub_word(Ri,1)) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (!BN_set_word(Ri,BN_MASK2)) goto err;\n\t\t\t}\n\t\tif (!BN_div(Ri,NULL,Ri,&tmod,ctx)) goto err;\n\t\tmont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n\t\tmont->n0[1] = 0;\n#endif\n\t\t}\n#else\n\t\t{\n\t\tmont->ri=BN_num_bits(&mont->N);\n\t\tBN_zero(R);\n\t\tif (!BN_set_bit(R,mont->ri)) goto err;\n\t\tif ((BN_mod_inverse(Ri,R,&mont->N,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tif (!BN_lshift(Ri,Ri,mont->ri)) goto err;\n\t\tif (!BN_sub_word(Ri,1)) goto err;\n\t\tif (!BN_div(&(mont->Ni),NULL,Ri,&mont->N,ctx)) goto err;\n\t\t}\n#endif\n\tBN_zero(&(mont->RR));\n\tif (!BN_set_bit(&(mont->RR),mont->ri*2)) goto err;\n\tif (!BN_mod(&(mont->RR),&(mont->RR),&(mont->N),ctx)) goto err;\n\tret = 1;\nerr:\n\tBN_CTX_end(ctx);\n\treturn ret;\n\t}', 'BIGNUM *BN_mod_inverse(BIGNUM *in,\n\tconst BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n\t{\n\tBIGNUM *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\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\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(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 || 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\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;\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\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'static int BN_div_no_branch(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,\n\tconst BIGNUM *divisor, BN_CTX *ctx)\n\t{\n\tint norm_shift,i,loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tint num_n,div_n;\n\tbn_check_top(dv);\n\tbn_check_top(rm);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV_NO_BRANCH,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\n\t\t}\n\tBN_CTX_start(ctx);\n\ttmp=BN_CTX_get(ctx);\n\tsnum=BN_CTX_get(ctx);\n\tsdiv=BN_CTX_get(ctx);\n\tif (dv == NULL)\n\t\tres=BN_CTX_get(ctx);\n\telse\tres=dv;\n\tif (sdiv == NULL || res == NULL) goto err;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tif (!(BN_lshift(sdiv,divisor,norm_shift))) goto err;\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tif (!(BN_lshift(snum,num,norm_shift))) goto err;\n\tsnum->neg=0;\n\tif (snum->top <= sdiv->top+1)\n\t\t{\n\t\tif (bn_wexpand(snum, sdiv->top + 2) == NULL) goto err;\n\t\tfor (i = snum->top; i < sdiv->top + 2; i++) snum->d[i] = 0;\n\t\tsnum->top = sdiv->top + 2;\n\t\t}\n\telse\n\t\t{\n\t\tif (bn_wexpand(snum, snum->top + 1) == NULL) goto err;\n\t\tsnum->d[snum->top] = 0;\n\t\tsnum->top ++;\n\t\t}\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\twnum.neg = 0;\n\twnum.d = &(snum->d[loop]);\n\twnum.top = div_n;\n\twnum.dmax = snum->dmax - loop;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop-1;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (res->top == 0)\n\t\tres->neg = 0;\n\telse\n\t\tresp--;\n\tfor (i=0; i<loop-1; i++, wnump--, resp--)\n\t\t{\n\t\tBN_ULONG q,l0;\n#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n\t\tBN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG);\n\t\tq=bn_div_3_words(wnump,d1,d0);\n#else\n\t\tBN_ULONG n0,n1,rem=0;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\t{\n#ifdef BN_LLONG\n\t\t\tBN_ULLONG t2;\n#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n\t\t\tq=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);\n#else\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tt2 -= d1;\n\t\t\t\t}\n#else\n\t\t\tBN_ULONG t2l,t2h;\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\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}']
36,463
0
https://github.com/openssl/openssl/blob/e02c519cd32a55e6ad39a0cfbeeda775f9115f28/crypto/bn/bn_lib.c/#L232
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *a = NULL; if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return NULL; } if (BN_get_flags(b, BN_FLG_SECURE)) a = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = OPENSSL_zalloc(words * sizeof(*a)); if (a == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return NULL; } assert(b->top <= words); if (b->top > 0) memcpy(a, b->d, sizeof(*a) * b->top); return a; }
['int ecdh_simple_compute_key(unsigned char **pout, size_t *poutlen,\n const EC_POINT *pub_key, const EC_KEY *ecdh)\n{\n BN_CTX *ctx;\n EC_POINT *tmp = NULL;\n BIGNUM *x = NULL;\n const BIGNUM *priv_key;\n const EC_GROUP *group;\n int ret = 0;\n size_t buflen, len;\n unsigned char *buf = NULL;\n if ((ctx = BN_CTX_new()) == NULL)\n goto err;\n BN_CTX_start(ctx);\n x = BN_CTX_get(ctx);\n if (x == NULL) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n priv_key = EC_KEY_get0_private_key(ecdh);\n if (priv_key == NULL) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_NO_PRIVATE_VALUE);\n goto err;\n }\n group = EC_KEY_get0_group(ecdh);\n if (EC_KEY_get_flags(ecdh) & EC_FLAG_COFACTOR_ECDH) {\n if (!EC_GROUP_get_cofactor(group, x, NULL) ||\n !BN_mul(x, x, priv_key, ctx)) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n priv_key = x;\n }\n if ((tmp = EC_POINT_new(group)) == NULL) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (!EC_POINT_mul(group, tmp, NULL, pub_key, priv_key, ctx)) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_POINT_ARITHMETIC_FAILURE);\n goto err;\n }\n if (!EC_POINT_get_affine_coordinates(group, tmp, x, NULL, ctx)) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_POINT_ARITHMETIC_FAILURE);\n goto err;\n }\n buflen = (EC_GROUP_get_degree(group) + 7) / 8;\n len = BN_num_bytes(x);\n if (len > buflen) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if ((buf = OPENSSL_malloc(buflen)) == NULL) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n memset(buf, 0, buflen - len);\n if (len != (size_t)BN_bn2bin(x, buf + buflen - len)) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_BN_LIB);\n goto err;\n }\n *pout = buf;\n *poutlen = buflen;\n buf = NULL;\n ret = 1;\n err:\n EC_POINT_free(tmp);\n if (ctx)\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n OPENSSL_free(buf);\n return ret;\n}', 'int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,\n BN_CTX *ctx)\n{\n if (group->cofactor == NULL)\n return 0;\n if (!BN_copy(cofactor, group->cofactor))\n return 0;\n return !BN_is_zero(group->cofactor);\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}', '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}']
36,464
0
https://github.com/libav/libav/blob/1d0feb5d1ac04d187b335f0e8d411c9f40b3a885/libavcodec/mpegvideo.c/#L828
static int init_context_frame(MpegEncContext *s) { int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y; s->mb_width = (s->width + 15) / 16; s->mb_stride = s->mb_width + 1; s->b8_stride = s->mb_width * 2 + 1; s->b4_stride = s->mb_width * 4 + 1; mb_array_size = s->mb_height * s->mb_stride; mv_table_size = (s->mb_height + 2) * s->mb_stride + 1; s->h_edge_pos = s->mb_width * 16; s->v_edge_pos = s->mb_height * 16; s->mb_num = s->mb_width * s->mb_height; s->block_wrap[0] = s->block_wrap[1] = s->block_wrap[2] = s->block_wrap[3] = s->b8_stride; s->block_wrap[4] = s->block_wrap[5] = s->mb_stride; y_size = s->b8_stride * (2 * s->mb_height + 1); c_size = s->mb_stride * (s->mb_height + 1); yc_size = y_size + 2 * c_size; FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int), fail); for (y = 0; y < s->mb_height; y++) for (x = 0; x < s->mb_width; x++) s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride; s->mb_index2xy[s->mb_height * s->mb_width] = (s->mb_height - 1) * s->mb_stride + s->mb_width; if (s->encoding) { FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail); FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail); FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail); FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail); FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail); FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail); s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1; s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1; s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1; s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base + s->mb_stride + 1; s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base + s->mb_stride + 1; s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1; FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size * sizeof(uint16_t), fail); FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size * sizeof(int), fail); FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab, mb_array_size * sizeof(float), fail); FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab, mb_array_size * sizeof(float), fail); } if (s->codec_id == AV_CODEC_ID_MPEG4 || (s->flags & CODEC_FLAG_INTERLACED_ME)) { for (i = 0; i < 2; i++) { int j, k; for (j = 0; j < 2; j++) { for (k = 0; k < 2; k++) { FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_mv_table_base[i][j][k], mv_table_size * 2 * sizeof(int16_t), fail); s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] + s->mb_stride + 1; } FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j], mb_array_size * 2 * sizeof(uint8_t), fail); FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j], mv_table_size * 2 * sizeof(int16_t), fail); s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j] + s->mb_stride + 1; } FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i], mb_array_size * 2 * sizeof(uint8_t), fail); } } if (s->out_format == FMT_H263) { FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail); s->coded_block = s->coded_block_base + s->b8_stride + 1; FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table, mb_array_size * sizeof(uint8_t), fail); FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table, mb_array_size * sizeof(uint8_t), fail); } if (s->h263_pred || s->h263_plus || !s->encoding) { FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base, yc_size * sizeof(int16_t), fail); s->dc_val[0] = s->dc_val_base + s->b8_stride + 1; s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1; s->dc_val[2] = s->dc_val[1] + c_size; for (i = 0; i < yc_size; i++) s->dc_val_base[i] = 1024; } FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail); memset(s->mbintra_table, 1, mb_array_size); FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail); if ((s->avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) || s->avctx->debug_mv) { s->visualization_buffer[0] = av_malloc((s->mb_width * 16 + 2 * EDGE_WIDTH) * s->mb_height * 16 + 2 * EDGE_WIDTH); s->visualization_buffer[1] = av_malloc((s->mb_width * 16 + 2 * EDGE_WIDTH) * s->mb_height * 16 + 2 * EDGE_WIDTH); s->visualization_buffer[2] = av_malloc((s->mb_width * 16 + 2 * EDGE_WIDTH) * s->mb_height * 16 + 2 * EDGE_WIDTH); } return init_er(s); fail: return AVERROR(ENOMEM); }
['static int init_context_frame(MpegEncContext *s)\n{\n int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;\n s->mb_width = (s->width + 15) / 16;\n s->mb_stride = s->mb_width + 1;\n s->b8_stride = s->mb_width * 2 + 1;\n s->b4_stride = s->mb_width * 4 + 1;\n mb_array_size = s->mb_height * s->mb_stride;\n mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;\n s->h_edge_pos = s->mb_width * 16;\n s->v_edge_pos = s->mb_height * 16;\n s->mb_num = s->mb_width * s->mb_height;\n s->block_wrap[0] =\n s->block_wrap[1] =\n s->block_wrap[2] =\n s->block_wrap[3] = s->b8_stride;\n s->block_wrap[4] =\n s->block_wrap[5] = s->mb_stride;\n y_size = s->b8_stride * (2 * s->mb_height + 1);\n c_size = s->mb_stride * (s->mb_height + 1);\n yc_size = y_size + 2 * c_size;\n FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int),\n fail);\n for (y = 0; y < s->mb_height; y++)\n for (x = 0; x < s->mb_width; x++)\n s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;\n s->mb_index2xy[s->mb_height * s->mb_width] =\n (s->mb_height - 1) * s->mb_stride + s->mb_width;\n if (s->encoding) {\n FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base,\n mv_table_size * 2 * sizeof(int16_t), fail);\n FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base,\n mv_table_size * 2 * sizeof(int16_t), fail);\n FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base,\n mv_table_size * 2 * sizeof(int16_t), fail);\n FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base,\n mv_table_size * 2 * sizeof(int16_t), fail);\n FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base,\n mv_table_size * 2 * sizeof(int16_t), fail);\n FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base,\n mv_table_size * 2 * sizeof(int16_t), fail);\n s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;\n s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;\n s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;\n s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base +\n s->mb_stride + 1;\n s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base +\n s->mb_stride + 1;\n s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;\n FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size *\n sizeof(uint16_t), fail);\n FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size *\n sizeof(int), fail);\n FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab,\n mb_array_size * sizeof(float), fail);\n FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab,\n mb_array_size * sizeof(float), fail);\n }\n if (s->codec_id == AV_CODEC_ID_MPEG4 ||\n (s->flags & CODEC_FLAG_INTERLACED_ME)) {\n for (i = 0; i < 2; i++) {\n int j, k;\n for (j = 0; j < 2; j++) {\n for (k = 0; k < 2; k++) {\n FF_ALLOCZ_OR_GOTO(s->avctx,\n s->b_field_mv_table_base[i][j][k],\n mv_table_size * 2 * sizeof(int16_t),\n fail);\n s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +\n s->mb_stride + 1;\n }\n FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j],\n mb_array_size * 2 * sizeof(uint8_t), fail);\n FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j],\n mv_table_size * 2 * sizeof(int16_t), fail);\n s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j]\n + s->mb_stride + 1;\n }\n FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i],\n mb_array_size * 2 * sizeof(uint8_t), fail);\n }\n }\n if (s->out_format == FMT_H263) {\n FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail);\n s->coded_block = s->coded_block_base + s->b8_stride + 1;\n FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table,\n mb_array_size * sizeof(uint8_t), fail);\n FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table,\n mb_array_size * sizeof(uint8_t), fail);\n }\n if (s->h263_pred || s->h263_plus || !s->encoding) {\n FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base,\n yc_size * sizeof(int16_t), fail);\n s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;\n s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;\n s->dc_val[2] = s->dc_val[1] + c_size;\n for (i = 0; i < yc_size; i++)\n s->dc_val_base[i] = 1024;\n }\n FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);\n memset(s->mbintra_table, 1, mb_array_size);\n FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);\n if ((s->avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) ||\n s->avctx->debug_mv) {\n s->visualization_buffer[0] = av_malloc((s->mb_width * 16 +\n 2 * EDGE_WIDTH) * s->mb_height * 16 + 2 * EDGE_WIDTH);\n s->visualization_buffer[1] = av_malloc((s->mb_width * 16 +\n 2 * EDGE_WIDTH) * s->mb_height * 16 + 2 * EDGE_WIDTH);\n s->visualization_buffer[2] = av_malloc((s->mb_width * 16 +\n 2 * EDGE_WIDTH) * s->mb_height * 16 + 2 * EDGE_WIDTH);\n }\n return init_er(s);\nfail:\n return AVERROR(ENOMEM);\n}', 'void *av_mallocz(size_t size)\n{\n void *ptr = av_malloc(size);\n if (ptr)\n memset(ptr, 0, size);\n return ptr;\n}', 'void *av_malloc(size_t size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if (size > (INT_MAX - 32) || !size)\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size + 32);\n if (!ptr)\n return ptr;\n diff = ((-(long)ptr - 1) & 31) + 1;\n ptr = (char *)ptr + diff;\n ((char *)ptr)[-1] = diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr, 32, size))\n ptr = NULL;\n#elif HAVE_ALIGNED_MALLOC\n ptr = _aligned_malloc(size, 32);\n#elif HAVE_MEMALIGN\n ptr = memalign(32, size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}']
36,465
0
https://github.com/openssl/openssl/blob/305b68f1a2b6d4d0aa07a6ab47ac372f067a40bb/crypto/bn/bn_lib.c/#L231
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *a = NULL; if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return NULL; } if (BN_get_flags(b, BN_FLG_SECURE)) a = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = OPENSSL_zalloc(words * sizeof(*a)); if (a == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return NULL; } assert(b->top <= words); if (b->top > 0) memcpy(a, b->d, sizeof(*a) * b->top); return a; }
['int ec_GFp_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,\n const EC_POINT *b, BN_CTX *ctx)\n{\n int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *,\n const BIGNUM *, BN_CTX *);\n int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);\n const BIGNUM *p;\n BN_CTX *new_ctx = NULL;\n BIGNUM *n0, *n1, *n2, *n3, *n4, *n5, *n6;\n int ret = 0;\n if (a == b)\n return EC_POINT_dbl(group, r, a, ctx);\n if (EC_POINT_is_at_infinity(group, a))\n return EC_POINT_copy(r, b);\n if (EC_POINT_is_at_infinity(group, b))\n return EC_POINT_copy(r, a);\n field_mul = group->meth->field_mul;\n field_sqr = group->meth->field_sqr;\n p = group->field;\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n BN_CTX_start(ctx);\n n0 = BN_CTX_get(ctx);\n n1 = BN_CTX_get(ctx);\n n2 = BN_CTX_get(ctx);\n n3 = BN_CTX_get(ctx);\n n4 = BN_CTX_get(ctx);\n n5 = BN_CTX_get(ctx);\n n6 = BN_CTX_get(ctx);\n if (n6 == NULL)\n goto end;\n if (b->Z_is_one) {\n if (!BN_copy(n1, a->X))\n goto end;\n if (!BN_copy(n2, a->Y))\n goto end;\n } else {\n if (!field_sqr(group, n0, b->Z, ctx))\n goto end;\n if (!field_mul(group, n1, a->X, n0, ctx))\n goto end;\n if (!field_mul(group, n0, n0, b->Z, ctx))\n goto end;\n if (!field_mul(group, n2, a->Y, n0, ctx))\n goto end;\n }\n if (a->Z_is_one) {\n if (!BN_copy(n3, b->X))\n goto end;\n if (!BN_copy(n4, b->Y))\n goto end;\n } else {\n if (!field_sqr(group, n0, a->Z, ctx))\n goto end;\n if (!field_mul(group, n3, b->X, n0, ctx))\n goto end;\n if (!field_mul(group, n0, n0, a->Z, ctx))\n goto end;\n if (!field_mul(group, n4, b->Y, n0, ctx))\n goto end;\n }\n if (!BN_mod_sub_quick(n5, n1, n3, p))\n goto end;\n if (!BN_mod_sub_quick(n6, n2, n4, p))\n goto end;\n if (BN_is_zero(n5)) {\n if (BN_is_zero(n6)) {\n BN_CTX_end(ctx);\n ret = EC_POINT_dbl(group, r, a, ctx);\n ctx = NULL;\n goto end;\n } else {\n BN_zero(r->Z);\n r->Z_is_one = 0;\n ret = 1;\n goto end;\n }\n }\n if (!BN_mod_add_quick(n1, n1, n3, p))\n goto end;\n if (!BN_mod_add_quick(n2, n2, n4, p))\n goto end;\n if (a->Z_is_one && b->Z_is_one) {\n if (!BN_copy(r->Z, n5))\n goto end;\n } else {\n if (a->Z_is_one) {\n if (!BN_copy(n0, b->Z))\n goto end;\n } else if (b->Z_is_one) {\n if (!BN_copy(n0, a->Z))\n goto end;\n } else {\n if (!field_mul(group, n0, a->Z, b->Z, ctx))\n goto end;\n }\n if (!field_mul(group, r->Z, n0, n5, ctx))\n goto end;\n }\n r->Z_is_one = 0;\n if (!field_sqr(group, n0, n6, ctx))\n goto end;\n if (!field_sqr(group, n4, n5, ctx))\n goto end;\n if (!field_mul(group, n3, n1, n4, ctx))\n goto end;\n if (!BN_mod_sub_quick(r->X, n0, n3, p))\n goto end;\n if (!BN_mod_lshift1_quick(n0, r->X, p))\n goto end;\n if (!BN_mod_sub_quick(n0, n3, n0, p))\n goto end;\n if (!field_mul(group, n0, n0, n6, ctx))\n goto end;\n if (!field_mul(group, n5, n4, n5, ctx))\n goto end;\n if (!field_mul(group, n1, n2, n5, ctx))\n goto end;\n if (!BN_mod_sub_quick(n0, n0, n1, p))\n goto end;\n if (BN_is_odd(n0))\n if (!BN_add(n0, n0, p))\n goto end;\n if (!BN_rshift1(r->Y, n0))\n goto end;\n ret = 1;\n end:\n if (ctx)\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'BIGNUM *BN_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}', 'int BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n const BIGNUM *m)\n{\n if (!BN_sub(r, a, b))\n return 0;\n if (r->neg)\n return BN_add(r, r, m);\n return 1;\n}', 'int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int ret, r_neg, cmp_res;\n bn_check_top(a);\n bn_check_top(b);\n if (a->neg != b->neg) {\n r_neg = a->neg;\n ret = BN_uadd(r, a, b);\n } else {\n cmp_res = BN_ucmp(a, b);\n if (cmp_res > 0) {\n r_neg = a->neg;\n ret = BN_usub(r, a, b);\n } else if (cmp_res < 0) {\n r_neg = !b->neg;\n ret = BN_usub(r, b, a);\n } else {\n r_neg = 0;\n BN_zero(r);\n ret = 1;\n }\n }\n r->neg = r_neg;\n bn_check_top(r);\n return ret;\n}', '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}']
36,466
0
https://gitlab.com/libtiff/libtiff/blob/d85a64b6d6379c9f9b8de6ff3a26e380b0fc3e18/libtiff/tif_dirread.c/#L4337
static int EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount) { static const char module[] = "EstimateStripByteCounts"; TIFFDirEntry *dp; TIFFDirectory *td = &tif->tif_dir; uint32 strip; _TIFFFillStriles( tif ); if (td->td_stripbytecount) _TIFFfree(td->td_stripbytecount); td->td_stripbytecount = (uint64*) _TIFFCheckMalloc(tif, td->td_nstrips, sizeof (uint64), "for \"StripByteCounts\" array"); if( td->td_stripbytecount == NULL ) return -1; if (td->td_compression != COMPRESSION_NONE) { uint64 space; uint64 filesize; uint16 n; filesize = TIFFGetFileSize(tif); if (!(tif->tif_flags&TIFF_BIGTIFF)) space=sizeof(TIFFHeaderClassic)+2+dircount*12+4; else space=sizeof(TIFFHeaderBig)+8+dircount*20+8; for (dp = dir, n = dircount; n > 0; n--, dp++) { uint32 typewidth = TIFFDataWidth((TIFFDataType) dp->tdir_type); uint64 datasize; typewidth = TIFFDataWidth((TIFFDataType) dp->tdir_type); if (typewidth == 0) { TIFFErrorExt(tif->tif_clientdata, module, "Cannot determine size of unknown tag type %d", dp->tdir_type); return -1; } datasize=(uint64)typewidth*dp->tdir_count; if (!(tif->tif_flags&TIFF_BIGTIFF)) { if (datasize<=4) datasize=0; } else { if (datasize<=8) datasize=0; } space+=datasize; } space = filesize - space; if (td->td_planarconfig == PLANARCONFIG_SEPARATE) space /= td->td_samplesperpixel; for (strip = 0; strip < td->td_nstrips; strip++) td->td_stripbytecount[strip] = space; strip--; if (td->td_stripoffset[strip]+td->td_stripbytecount[strip] > filesize) td->td_stripbytecount[strip] = filesize - td->td_stripoffset[strip]; } else if (isTiled(tif)) { uint64 bytespertile = TIFFTileSize64(tif); for (strip = 0; strip < td->td_nstrips; strip++) td->td_stripbytecount[strip] = bytespertile; } else { uint64 rowbytes = TIFFScanlineSize64(tif); uint32 rowsperstrip = td->td_imagelength/td->td_stripsperimage; for (strip = 0; strip < td->td_nstrips; strip++) td->td_stripbytecount[strip] = rowbytes * rowsperstrip; } TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS); if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP)) td->td_rowsperstrip = td->td_imagelength; return 1; }
['int\nTIFFReadDirectory(TIFF* tif)\n{\n\tstatic const char module[] = "TIFFReadDirectory";\n\tTIFFDirEntry* dir;\n\tuint16 dircount;\n\tTIFFDirEntry* dp;\n\tuint16 di;\n\tconst TIFFField* fip;\n\tuint32 fii=FAILED_FII;\n toff_t nextdiroff;\n\ttif->tif_diroff=tif->tif_nextdiroff;\n\tif (!TIFFCheckDirOffset(tif,tif->tif_nextdiroff))\n\t\treturn 0;\n\t(*tif->tif_cleanup)(tif);\n\ttif->tif_curdir++;\n nextdiroff = tif->tif_nextdiroff;\n\tdircount=TIFFFetchDirectory(tif,nextdiroff,&dir,&tif->tif_nextdiroff);\n\tif (!dircount)\n\t{\n\t\tTIFFErrorExt(tif->tif_clientdata,module,\n\t\t "Failed to read directory at offset " TIFF_UINT64_FORMAT,nextdiroff);\n\t\treturn 0;\n\t}\n\tTIFFReadDirectoryCheckOrder(tif,dir,dircount);\n\t{\n\t\tTIFFDirEntry* ma;\n\t\tuint16 mb;\n\t\tfor (ma=dir, mb=0; mb<dircount; ma++, mb++)\n\t\t{\n\t\t\tTIFFDirEntry* na;\n\t\t\tuint16 nb;\n\t\t\tfor (na=ma+1, nb=mb+1; nb<dircount; na++, nb++)\n\t\t\t{\n\t\t\t\tif (ma->tdir_tag==na->tdir_tag)\n\t\t\t\t\tna->tdir_tag=IGNORE;\n\t\t\t}\n\t\t}\n\t}\n\ttif->tif_flags &= ~TIFF_BEENWRITING;\n\ttif->tif_flags &= ~TIFF_BUF4WRITE;\n\tTIFFFreeDirectory(tif);\n\tTIFFDefaultDirectory(tif);\n\tTIFFSetField(tif,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);\n\tdp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_SAMPLESPERPIXEL);\n\tif (dp)\n\t{\n\t\tif (!TIFFFetchNormalTag(tif,dp,0))\n\t\t\tgoto bad;\n\t\tdp->tdir_tag=IGNORE;\n\t}\n\tdp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_COMPRESSION);\n\tif (dp)\n\t{\n\t\tuint16 value;\n\t\tenum TIFFReadDirEntryErr err;\n\t\terr=TIFFReadDirEntryShort(tif,dp,&value);\n\t\tif (err==TIFFReadDirEntryErrCount)\n\t\t\terr=TIFFReadDirEntryPersampleShort(tif,dp,&value);\n\t\tif (err!=TIFFReadDirEntryErrOk)\n\t\t{\n\t\t\tTIFFReadDirEntryOutputErr(tif,err,module,"Compression",0);\n\t\t\tgoto bad;\n\t\t}\n\t\tif (!TIFFSetField(tif,TIFFTAG_COMPRESSION,value))\n\t\t\tgoto bad;\n\t\tdp->tdir_tag=IGNORE;\n\t}\n\telse\n\t{\n\t\tif (!TIFFSetField(tif,TIFFTAG_COMPRESSION,COMPRESSION_NONE))\n\t\t\tgoto bad;\n\t}\n\tfor (di=0, dp=dir; di<dircount; di++, dp++)\n\t{\n\t\tif (dp->tdir_tag!=IGNORE)\n\t\t{\n\t\t\tTIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);\n\t\t\tif (fii == FAILED_FII)\n\t\t\t{\n\t\t\t\tTIFFWarningExt(tif->tif_clientdata, module,\n\t\t\t\t "Unknown field with tag %d (0x%x) encountered",\n\t\t\t\t dp->tdir_tag,dp->tdir_tag);\n\t\t\t\tif (!_TIFFMergeFields(tif,\n\t\t\t\t\t_TIFFCreateAnonField(tif,\n\t\t\t\t\t\tdp->tdir_tag,\n\t\t\t\t\t\t(TIFFDataType) dp->tdir_type),\n\t\t\t\t\t1)) {\n\t\t\t\t\tTIFFWarningExt(tif->tif_clientdata,\n\t\t\t\t\t module,\n\t\t\t\t\t "Registering anonymous field with tag %d (0x%x) failed",\n\t\t\t\t\t dp->tdir_tag,\n\t\t\t\t\t dp->tdir_tag);\n\t\t\t\t\tdp->tdir_tag=IGNORE;\n\t\t\t\t} else {\n\t\t\t\t\tTIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);\n\t\t\t\t\tassert(fii != FAILED_FII);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (dp->tdir_tag!=IGNORE)\n\t\t{\n\t\t\tfip=tif->tif_fields[fii];\n\t\t\tif (fip->field_bit==FIELD_IGNORE)\n\t\t\t\tdp->tdir_tag=IGNORE;\n\t\t\telse\n\t\t\t{\n\t\t\t\tswitch (dp->tdir_tag)\n\t\t\t\t{\n\t\t\t\t\tcase TIFFTAG_STRIPOFFSETS:\n\t\t\t\t\tcase TIFFTAG_STRIPBYTECOUNTS:\n\t\t\t\t\tcase TIFFTAG_TILEOFFSETS:\n\t\t\t\t\tcase TIFFTAG_TILEBYTECOUNTS:\n\t\t\t\t\t\tTIFFSetFieldBit(tif,fip->field_bit);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase TIFFTAG_IMAGEWIDTH:\n\t\t\t\t\tcase TIFFTAG_IMAGELENGTH:\n\t\t\t\t\tcase TIFFTAG_IMAGEDEPTH:\n\t\t\t\t\tcase TIFFTAG_TILELENGTH:\n\t\t\t\t\tcase TIFFTAG_TILEWIDTH:\n\t\t\t\t\tcase TIFFTAG_TILEDEPTH:\n\t\t\t\t\tcase TIFFTAG_PLANARCONFIG:\n\t\t\t\t\tcase TIFFTAG_ROWSPERSTRIP:\n\t\t\t\t\tcase TIFFTAG_EXTRASAMPLES:\n\t\t\t\t\t\tif (!TIFFFetchNormalTag(tif,dp,0))\n\t\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t\tdp->tdir_tag=IGNORE;\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tif ((tif->tif_dir.td_compression==COMPRESSION_OJPEG)&&\n\t (tif->tif_dir.td_planarconfig==PLANARCONFIG_SEPARATE))\n\t{\n if (!_TIFFFillStriles(tif))\n goto bad;\n\t\tdp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_STRIPOFFSETS);\n\t\tif ((dp!=0)&&(dp->tdir_count==1))\n\t\t{\n\t\t\tdp=TIFFReadDirectoryFindEntry(tif,dir,dircount,\n\t\t\t TIFFTAG_STRIPBYTECOUNTS);\n\t\t\tif ((dp!=0)&&(dp->tdir_count==1))\n\t\t\t{\n\t\t\t\ttif->tif_dir.td_planarconfig=PLANARCONFIG_CONTIG;\n\t\t\t\tTIFFWarningExt(tif->tif_clientdata,module,\n\t\t\t\t "Planarconfig tag value assumed incorrect, "\n\t\t\t\t "assuming data is contig instead of chunky");\n\t\t\t}\n\t\t}\n\t}\n\tif (!TIFFFieldSet(tif,FIELD_IMAGEDIMENSIONS))\n\t{\n\t\tMissingRequired(tif,"ImageLength");\n\t\tgoto bad;\n\t}\n\tif (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) {\n\t\ttif->tif_dir.td_nstrips = TIFFNumberOfStrips(tif);\n\t\ttif->tif_dir.td_tilewidth = tif->tif_dir.td_imagewidth;\n\t\ttif->tif_dir.td_tilelength = tif->tif_dir.td_rowsperstrip;\n\t\ttif->tif_dir.td_tiledepth = tif->tif_dir.td_imagedepth;\n\t\ttif->tif_flags &= ~TIFF_ISTILED;\n\t} else {\n\t\ttif->tif_dir.td_nstrips = TIFFNumberOfTiles(tif);\n\t\ttif->tif_flags |= TIFF_ISTILED;\n\t}\n\tif (!tif->tif_dir.td_nstrips) {\n\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t "Cannot handle zero number of %s",\n\t\t isTiled(tif) ? "tiles" : "strips");\n\t\tgoto bad;\n\t}\n\ttif->tif_dir.td_stripsperimage = tif->tif_dir.td_nstrips;\n\tif (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE)\n\t\ttif->tif_dir.td_stripsperimage /= tif->tif_dir.td_samplesperpixel;\n\tif (!TIFFFieldSet(tif, FIELD_STRIPOFFSETS)) {\n\t\tif ((tif->tif_dir.td_compression==COMPRESSION_OJPEG) &&\n\t\t (isTiled(tif)==0) &&\n\t\t (tif->tif_dir.td_nstrips==1)) {\n\t\t\tTIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);\n\t\t} else {\n\t\t\tMissingRequired(tif,\n\t\t\t\tisTiled(tif) ? "TileOffsets" : "StripOffsets");\n\t\t\tgoto bad;\n\t\t}\n\t}\n\tfor (di=0, dp=dir; di<dircount; di++, dp++)\n\t{\n\t\tswitch (dp->tdir_tag)\n\t\t{\n\t\t\tcase IGNORE:\n\t\t\t\tbreak;\n\t\t\tcase TIFFTAG_MINSAMPLEVALUE:\n\t\t\tcase TIFFTAG_MAXSAMPLEVALUE:\n\t\t\tcase TIFFTAG_BITSPERSAMPLE:\n\t\t\tcase TIFFTAG_DATATYPE:\n\t\t\tcase TIFFTAG_SAMPLEFORMAT:\n\t\t\t\t{\n\t\t\t\t\tuint16 value;\n\t\t\t\t\tenum TIFFReadDirEntryErr err;\n\t\t\t\t\terr=TIFFReadDirEntryShort(tif,dp,&value);\n\t\t\t\t\tif (err==TIFFReadDirEntryErrCount)\n\t\t\t\t\t\terr=TIFFReadDirEntryPersampleShort(tif,dp,&value);\n\t\t\t\t\tif (err!=TIFFReadDirEntryErrOk)\n\t\t\t\t\t{\n\t\t\t\t\t\tfip = TIFFFieldWithTag(tif,dp->tdir_tag);\n\t\t\t\t\t\tTIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t}\n\t\t\t\t\tif (!TIFFSetField(tif,dp->tdir_tag,value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase TIFFTAG_SMINSAMPLEVALUE:\n\t\t\tcase TIFFTAG_SMAXSAMPLEVALUE:\n\t\t\t\t{\n\t\t\t\t\tdouble *data;\n\t\t\t\t\tenum TIFFReadDirEntryErr err;\n\t\t\t\t\tuint32 saved_flags;\n\t\t\t\t\tint m;\n\t\t\t\t\tif (dp->tdir_count != (uint64)tif->tif_dir.td_samplesperpixel)\n\t\t\t\t\t\terr = TIFFReadDirEntryErrCount;\n\t\t\t\t\telse\n\t\t\t\t\t\terr = TIFFReadDirEntryDoubleArray(tif, dp, &data);\n\t\t\t\t\tif (err!=TIFFReadDirEntryErrOk)\n\t\t\t\t\t{\n\t\t\t\t\t\tfip = TIFFFieldWithTag(tif,dp->tdir_tag);\n\t\t\t\t\t\tTIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t}\n\t\t\t\t\tsaved_flags = tif->tif_flags;\n\t\t\t\t\ttif->tif_flags |= TIFF_PERSAMPLE;\n\t\t\t\t\tm = TIFFSetField(tif,dp->tdir_tag,data);\n\t\t\t\t\ttif->tif_flags = saved_flags;\n\t\t\t\t\t_TIFFfree(data);\n\t\t\t\t\tif (!m)\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase TIFFTAG_STRIPOFFSETS:\n\t\t\tcase TIFFTAG_TILEOFFSETS:\n#if defined(DEFER_STRILE_LOAD)\n _TIFFmemcpy( &(tif->tif_dir.td_stripoffset_entry),\n dp, sizeof(TIFFDirEntry) );\n#else\n\t\t\t\tif (!TIFFFetchStripThing(tif,dp,tif->tif_dir.td_nstrips,&tif->tif_dir.td_stripoffset))\n\t\t\t\t\tgoto bad;\n#endif\n\t\t\t\tbreak;\n\t\t\tcase TIFFTAG_STRIPBYTECOUNTS:\n\t\t\tcase TIFFTAG_TILEBYTECOUNTS:\n#if defined(DEFER_STRILE_LOAD)\n _TIFFmemcpy( &(tif->tif_dir.td_stripbytecount_entry),\n dp, sizeof(TIFFDirEntry) );\n#else\n\t\t\t\tif (!TIFFFetchStripThing(tif,dp,tif->tif_dir.td_nstrips,&tif->tif_dir.td_stripbytecount))\n\t\t\t\t\tgoto bad;\n#endif\n\t\t\t\tbreak;\n\t\t\tcase TIFFTAG_COLORMAP:\n\t\t\tcase TIFFTAG_TRANSFERFUNCTION:\n\t\t\t\t{\n\t\t\t\t\tenum TIFFReadDirEntryErr err;\n\t\t\t\t\tuint32 countpersample;\n\t\t\t\t\tuint32 countrequired;\n\t\t\t\t\tuint32 incrementpersample;\n\t\t\t\t\tuint16* value=NULL;\n\t\t\t\t\tcountpersample=(1L<<tif->tif_dir.td_bitspersample);\n\t\t\t\t\tif ((dp->tdir_tag==TIFFTAG_TRANSFERFUNCTION)&&(dp->tdir_count==(uint64)countpersample))\n\t\t\t\t\t{\n\t\t\t\t\t\tcountrequired=countpersample;\n\t\t\t\t\t\tincrementpersample=0;\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tcountrequired=3*countpersample;\n\t\t\t\t\t\tincrementpersample=countpersample;\n\t\t\t\t\t}\n\t\t\t\t\tif (dp->tdir_count!=(uint64)countrequired)\n\t\t\t\t\t\terr=TIFFReadDirEntryErrCount;\n\t\t\t\t\telse\n\t\t\t\t\t\terr=TIFFReadDirEntryShortArray(tif,dp,&value);\n\t\t\t\t\tif (err!=TIFFReadDirEntryErrOk)\n {\n\t\t\t\t\t\tfip = TIFFFieldWithTag(tif,dp->tdir_tag);\n\t\t\t\t\t\tTIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",1);\n }\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tTIFFSetField(tif,dp->tdir_tag,value,value+incrementpersample,value+2*incrementpersample);\n\t\t\t\t\t\t_TIFFfree(value);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase TIFFTAG_OSUBFILETYPE:\n\t\t\t\t{\n\t\t\t\t\tuint16 valueo;\n\t\t\t\t\tuint32 value;\n\t\t\t\t\tif (TIFFReadDirEntryShort(tif,dp,&valueo)==TIFFReadDirEntryErrOk)\n\t\t\t\t\t{\n\t\t\t\t\t\tswitch (valueo)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tcase OFILETYPE_REDUCEDIMAGE: value=FILETYPE_REDUCEDIMAGE; break;\n\t\t\t\t\t\t\tcase OFILETYPE_PAGE: value=FILETYPE_PAGE; break;\n\t\t\t\t\t\t\tdefault: value=0; break;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (value!=0)\n\t\t\t\t\t\t\tTIFFSetField(tif,TIFFTAG_SUBFILETYPE,value);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\t(void) TIFFFetchNormalTag(tif, dp, TRUE);\n\t\t\t\tbreak;\n\t\t}\n\t}\n\tif (tif->tif_dir.td_compression==COMPRESSION_OJPEG)\n\t{\n\t\tif (!TIFFFieldSet(tif,FIELD_PHOTOMETRIC))\n\t\t{\n\t\t\tTIFFWarningExt(tif->tif_clientdata, module,\n\t\t\t "Photometric tag is missing, assuming data is YCbCr");\n\t\t\tif (!TIFFSetField(tif,TIFFTAG_PHOTOMETRIC,PHOTOMETRIC_YCBCR))\n\t\t\t\tgoto bad;\n\t\t}\n\t\telse if (tif->tif_dir.td_photometric==PHOTOMETRIC_RGB)\n\t\t{\n\t\t\ttif->tif_dir.td_photometric=PHOTOMETRIC_YCBCR;\n\t\t\tTIFFWarningExt(tif->tif_clientdata, module,\n\t\t\t "Photometric tag value assumed incorrect, "\n\t\t\t "assuming data is YCbCr instead of RGB");\n\t\t}\n\t\tif (!TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))\n\t\t{\n\t\t\tTIFFWarningExt(tif->tif_clientdata,module,\n\t\t\t "BitsPerSample tag is missing, assuming 8 bits per sample");\n\t\t\tif (!TIFFSetField(tif,TIFFTAG_BITSPERSAMPLE,8))\n\t\t\t\tgoto bad;\n\t\t}\n\t\tif (!TIFFFieldSet(tif,FIELD_SAMPLESPERPIXEL))\n\t\t{\n\t\t\tif (tif->tif_dir.td_photometric==PHOTOMETRIC_RGB)\n\t\t\t{\n\t\t\t\tTIFFWarningExt(tif->tif_clientdata,module,\n\t\t\t\t "SamplesPerPixel tag is missing, "\n\t\t\t\t "assuming correct SamplesPerPixel value is 3");\n\t\t\t\tif (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,3))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (tif->tif_dir.td_photometric==PHOTOMETRIC_YCBCR)\n\t\t\t{\n\t\t\t\tTIFFWarningExt(tif->tif_clientdata,module,\n\t\t\t\t "SamplesPerPixel tag is missing, "\n\t\t\t\t "applying correct SamplesPerPixel value of 3");\n\t\t\t\tif (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,3))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\telse if ((tif->tif_dir.td_photometric==PHOTOMETRIC_MINISWHITE)\n\t\t\t\t || (tif->tif_dir.td_photometric==PHOTOMETRIC_MINISBLACK))\n\t\t\t{\n\t\t\t\tif (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,1))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t}\n\t}\n\tif (tif->tif_dir.td_photometric == PHOTOMETRIC_PALETTE &&\n\t !TIFFFieldSet(tif, FIELD_COLORMAP)) {\n\t\tif ( tif->tif_dir.td_bitspersample>=8 && tif->tif_dir.td_samplesperpixel==3)\n\t\t\ttif->tif_dir.td_photometric = PHOTOMETRIC_RGB;\n\t\telse if (tif->tif_dir.td_bitspersample>=8)\n\t\t\ttif->tif_dir.td_photometric = PHOTOMETRIC_MINISBLACK;\n\t\telse {\n\t\t\tMissingRequired(tif, "Colormap");\n\t\t\tgoto bad;\n\t\t}\n\t}\n\tif (tif->tif_dir.td_compression!=COMPRESSION_OJPEG)\n\t{\n\t\tif (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS)) {\n\t\t\tif ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&\n\t\t\t tif->tif_dir.td_nstrips > 1) ||\n\t\t\t (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE &&\n\t\t\t tif->tif_dir.td_nstrips != (uint32)tif->tif_dir.td_samplesperpixel)) {\n\t\t\t MissingRequired(tif, "StripByteCounts");\n\t\t\t goto bad;\n\t\t\t}\n\t\t\tTIFFWarningExt(tif->tif_clientdata, module,\n\t\t\t\t"TIFF directory is missing required "\n\t\t\t\t"\\"StripByteCounts\\" field, calculating from imagelength");\n\t\t\tif (EstimateStripByteCounts(tif, dir, dircount) < 0)\n\t\t\t goto bad;\n\t\t#define\tBYTECOUNTLOOKSBAD \\\n\t\t ( (tif->tif_dir.td_stripbytecount[0] == 0 && tif->tif_dir.td_stripoffset[0] != 0) || \\\n\t\t (tif->tif_dir.td_compression == COMPRESSION_NONE && \\\n\t\t tif->tif_dir.td_stripbytecount[0] > TIFFGetFileSize(tif) - tif->tif_dir.td_stripoffset[0]) || \\\n\t\t (tif->tif_mode == O_RDONLY && \\\n\t\t tif->tif_dir.td_compression == COMPRESSION_NONE && \\\n\t\t tif->tif_dir.td_stripbytecount[0] < TIFFScanlineSize64(tif) * tif->tif_dir.td_imagelength) )\n\t\t} else if (tif->tif_dir.td_nstrips == 1\n && _TIFFFillStriles(tif)\n\t\t\t && tif->tif_dir.td_stripoffset[0] != 0\n\t\t\t && BYTECOUNTLOOKSBAD) {\n\t\t\tTIFFWarningExt(tif->tif_clientdata, module,\n\t\t\t "Bogus \\"StripByteCounts\\" field, ignoring and calculating from imagelength");\n\t\t\tif(EstimateStripByteCounts(tif, dir, dircount) < 0)\n\t\t\t goto bad;\n#if !defined(DEFER_STRILE_LOAD)\n\t\t} else if (tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG\n\t\t\t && tif->tif_dir.td_nstrips > 2\n\t\t\t && tif->tif_dir.td_compression == COMPRESSION_NONE\n\t\t\t && tif->tif_dir.td_stripbytecount[0] != tif->tif_dir.td_stripbytecount[1]\n\t\t\t && tif->tif_dir.td_stripbytecount[0] != 0\n\t\t\t && tif->tif_dir.td_stripbytecount[1] != 0 ) {\n\t\t\tTIFFWarningExt(tif->tif_clientdata, module,\n\t\t\t "Wrong \\"StripByteCounts\\" field, ignoring and calculating from imagelength");\n\t\t\tif (EstimateStripByteCounts(tif, dir, dircount) < 0)\n\t\t\t goto bad;\n#endif\n\t\t}\n\t}\n\tif (dir)\n\t{\n\t\t_TIFFfree(dir);\n\t\tdir=NULL;\n\t}\n\tif (!TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE))\n\t{\n\t\tif (tif->tif_dir.td_bitspersample>=16)\n\t\t\ttif->tif_dir.td_maxsamplevalue=0xFFFF;\n\t\telse\n\t\t\ttif->tif_dir.td_maxsamplevalue = (uint16)((1L<<tif->tif_dir.td_bitspersample)-1);\n\t}\n#if !defined(DEFER_STRILE_LOAD)\n\tif (tif->tif_dir.td_nstrips > 1) {\n\t\tuint32 strip;\n\t\ttif->tif_dir.td_stripbytecountsorted = 1;\n\t\tfor (strip = 1; strip < tif->tif_dir.td_nstrips; strip++) {\n\t\t\tif (tif->tif_dir.td_stripoffset[strip - 1] >\n\t\t\t tif->tif_dir.td_stripoffset[strip]) {\n\t\t\t\ttif->tif_dir.td_stripbytecountsorted = 0;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n#endif\n\t(*tif->tif_fixuptags)(tif);\n\tif ((tif->tif_dir.td_planarconfig==PLANARCONFIG_CONTIG)&&\n\t (tif->tif_dir.td_nstrips==1)&&\n\t (tif->tif_dir.td_compression==COMPRESSION_NONE)&&\n\t ((tif->tif_flags&(TIFF_STRIPCHOP|TIFF_ISTILED))==TIFF_STRIPCHOP))\n {\n if ( !_TIFFFillStriles(tif) || !tif->tif_dir.td_stripbytecount )\n return 0;\n\t\tChopUpSingleUncompressedStrip(tif);\n }\n\ttif->tif_flags &= ~TIFF_DIRTYDIRECT;\n\ttif->tif_flags &= ~TIFF_DIRTYSTRIP;\n\ttif->tif_row = (uint32) -1;\n\ttif->tif_curstrip = (uint32) -1;\n\ttif->tif_col = (uint32) -1;\n\ttif->tif_curtile = (uint32) -1;\n\ttif->tif_tilesize = (tmsize_t) -1;\n\ttif->tif_scanlinesize = TIFFScanlineSize(tif);\n\tif (!tif->tif_scanlinesize) {\n\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t "Cannot handle zero scanline size");\n\t\treturn (0);\n\t}\n\tif (isTiled(tif)) {\n\t\ttif->tif_tilesize = TIFFTileSize(tif);\n\t\tif (!tif->tif_tilesize) {\n\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t "Cannot handle zero tile size");\n\t\t\treturn (0);\n\t\t}\n\t} else {\n\t\tif (!TIFFStripSize(tif)) {\n\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t "Cannot handle zero strip size");\n\t\t\treturn (0);\n\t\t}\n\t}\n\treturn (1);\nbad:\n\tif (dir)\n\t\t_TIFFfree(dir);\n\treturn (0);\n}', 'static int\nEstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)\n{\n\tstatic const char module[] = "EstimateStripByteCounts";\n\tTIFFDirEntry *dp;\n\tTIFFDirectory *td = &tif->tif_dir;\n\tuint32 strip;\n _TIFFFillStriles( tif );\n\tif (td->td_stripbytecount)\n\t\t_TIFFfree(td->td_stripbytecount);\n\ttd->td_stripbytecount = (uint64*)\n\t _TIFFCheckMalloc(tif, td->td_nstrips, sizeof (uint64),\n\t\t"for \\"StripByteCounts\\" array");\n if( td->td_stripbytecount == NULL )\n return -1;\n\tif (td->td_compression != COMPRESSION_NONE) {\n\t\tuint64 space;\n\t\tuint64 filesize;\n\t\tuint16 n;\n\t\tfilesize = TIFFGetFileSize(tif);\n\t\tif (!(tif->tif_flags&TIFF_BIGTIFF))\n\t\t\tspace=sizeof(TIFFHeaderClassic)+2+dircount*12+4;\n\t\telse\n\t\t\tspace=sizeof(TIFFHeaderBig)+8+dircount*20+8;\n\t\tfor (dp = dir, n = dircount; n > 0; n--, dp++)\n\t\t{\n\t\t\tuint32 typewidth = TIFFDataWidth((TIFFDataType) dp->tdir_type);\n\t\t\tuint64 datasize;\n\t\t\ttypewidth = TIFFDataWidth((TIFFDataType) dp->tdir_type);\n\t\t\tif (typewidth == 0) {\n\t\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t\t "Cannot determine size of unknown tag type %d",\n\t\t\t\t dp->tdir_type);\n\t\t\t\treturn -1;\n\t\t\t}\n\t\t\tdatasize=(uint64)typewidth*dp->tdir_count;\n\t\t\tif (!(tif->tif_flags&TIFF_BIGTIFF))\n\t\t\t{\n\t\t\t\tif (datasize<=4)\n\t\t\t\t\tdatasize=0;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tif (datasize<=8)\n\t\t\t\t\tdatasize=0;\n\t\t\t}\n\t\t\tspace+=datasize;\n\t\t}\n\t\tspace = filesize - space;\n\t\tif (td->td_planarconfig == PLANARCONFIG_SEPARATE)\n\t\t\tspace /= td->td_samplesperpixel;\n\t\tfor (strip = 0; strip < td->td_nstrips; strip++)\n\t\t\ttd->td_stripbytecount[strip] = space;\n\t\tstrip--;\n\t\tif (td->td_stripoffset[strip]+td->td_stripbytecount[strip] > filesize)\n\t\t\ttd->td_stripbytecount[strip] = filesize - td->td_stripoffset[strip];\n\t} else if (isTiled(tif)) {\n\t\tuint64 bytespertile = TIFFTileSize64(tif);\n\t\tfor (strip = 0; strip < td->td_nstrips; strip++)\n\t\t td->td_stripbytecount[strip] = bytespertile;\n\t} else {\n\t\tuint64 rowbytes = TIFFScanlineSize64(tif);\n\t\tuint32 rowsperstrip = td->td_imagelength/td->td_stripsperimage;\n\t\tfor (strip = 0; strip < td->td_nstrips; strip++)\n\t\t\ttd->td_stripbytecount[strip] = rowbytes * rowsperstrip;\n\t}\n\tTIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);\n\tif (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP))\n\t\ttd->td_rowsperstrip = td->td_imagelength;\n\treturn 1;\n}']
36,467
0
https://github.com/openssl/openssl/blob/9b02dc97e4963969da69675a871dbe80e6d31cda/crypto/bn/bn_lib.c/#L260
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *a = NULL; bn_check_top(b); if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return NULL; } if (BN_get_flags(b, BN_FLG_SECURE)) a = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = OPENSSL_zalloc(words * sizeof(*a)); if (a == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return NULL; } assert(b->top <= words); if (b->top > 0) memcpy(a, b->d, sizeof(*a) * b->top); return a; }
['int BN_GF2m_mod_div_arr(BIGNUM *r, const BIGNUM *yy, const BIGNUM *xx,\n const int p[], BN_CTX *ctx)\n{\n BIGNUM *field;\n int ret = 0;\n bn_check_top(yy);\n bn_check_top(xx);\n BN_CTX_start(ctx);\n if ((field = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (!BN_GF2m_arr2poly(p, field))\n goto err;\n ret = BN_GF2m_mod_div(r, yy, xx, field, ctx);\n bn_check_top(r);\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return 1;\n}', 'int BN_GF2m_arr2poly(const int p[], BIGNUM *a)\n{\n int i;\n bn_check_top(a);\n BN_zero(a);\n for (i = 0; p[i] != -1; i++) {\n if (BN_set_bit(a, p[i]) == 0)\n return 0;\n }\n bn_check_top(a);\n return 1;\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n 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}']
36,468
0
https://github.com/libav/libav/blob/a80af87eea88a674375637ccfb54b41526f774a4/libavcodec/dvbsubdec.c/#L943
static void dvbsub_parse_clut_segment(AVCodecContext *avctx, const uint8_t *buf, int buf_size) { DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data; const uint8_t *buf_end = buf + buf_size; int clut_id; DVBSubCLUT *clut; int entry_id, depth , full_range; int y, cr, cb, alpha; int r, g, b, r_add, g_add, b_add; #ifdef DEBUG_PACKET_CONTENTS int i; av_log(avctx, AV_LOG_INFO, "DVB clut packet:\n"); for (i=0; i < buf_size; i++) { av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]); if (i % 16 == 15) av_log(avctx, AV_LOG_INFO, "\n"); } if (i % 16) av_log(avctx, AV_LOG_INFO, "\n"); #endif clut_id = *buf++; buf += 1; clut = get_clut(ctx, clut_id); if (!clut) { clut = av_malloc(sizeof(DVBSubCLUT)); memcpy(clut, &default_clut, sizeof(DVBSubCLUT)); clut->id = clut_id; clut->next = ctx->clut_list; ctx->clut_list = clut; } while (buf + 4 < buf_end) { entry_id = *buf++; depth = (*buf) & 0xe0; if (depth == 0) { av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf); return; } full_range = (*buf++) & 1; if (full_range) { y = *buf++; cr = *buf++; cb = *buf++; alpha = *buf++; } else { y = buf[0] & 0xfc; cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4; cb = (buf[1] << 2) & 0xf0; alpha = (buf[1] << 6) & 0xc0; buf += 2; } if (y == 0) alpha = 0xff; YUV_TO_RGB1_CCIR(cb, cr); YUV_TO_RGB2_CCIR(r, g, b, y); dprintf(avctx, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha); if (depth & 0x80) clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha); if (depth & 0x40) clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha); if (depth & 0x20) clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha); } }
['static void dvbsub_parse_clut_segment(AVCodecContext *avctx,\n const uint8_t *buf, int buf_size)\n{\n DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;\n const uint8_t *buf_end = buf + buf_size;\n int clut_id;\n DVBSubCLUT *clut;\n int entry_id, depth , full_range;\n int y, cr, cb, alpha;\n int r, g, b, r_add, g_add, b_add;\n#ifdef DEBUG_PACKET_CONTENTS\n int i;\n av_log(avctx, AV_LOG_INFO, "DVB clut packet:\\n");\n for (i=0; i < buf_size; i++) {\n av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);\n if (i % 16 == 15)\n av_log(avctx, AV_LOG_INFO, "\\n");\n }\n if (i % 16)\n av_log(avctx, AV_LOG_INFO, "\\n");\n#endif\n clut_id = *buf++;\n buf += 1;\n clut = get_clut(ctx, clut_id);\n if (!clut) {\n clut = av_malloc(sizeof(DVBSubCLUT));\n memcpy(clut, &default_clut, sizeof(DVBSubCLUT));\n clut->id = clut_id;\n clut->next = ctx->clut_list;\n ctx->clut_list = clut;\n }\n while (buf + 4 < buf_end) {\n entry_id = *buf++;\n depth = (*buf) & 0xe0;\n if (depth == 0) {\n av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\\n", *buf);\n return;\n }\n full_range = (*buf++) & 1;\n if (full_range) {\n y = *buf++;\n cr = *buf++;\n cb = *buf++;\n alpha = *buf++;\n } else {\n y = buf[0] & 0xfc;\n cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;\n cb = (buf[1] << 2) & 0xf0;\n alpha = (buf[1] << 6) & 0xc0;\n buf += 2;\n }\n if (y == 0)\n alpha = 0xff;\n YUV_TO_RGB1_CCIR(cb, cr);\n YUV_TO_RGB2_CCIR(r, g, b, y);\n dprintf(avctx, "clut %d := (%d,%d,%d,%d)\\n", entry_id, r, g, b, alpha);\n if (depth & 0x80)\n clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);\n if (depth & 0x40)\n clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);\n if (depth & 0x20)\n clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);\n }\n}', 'static DVBSubCLUT* get_clut(DVBSubContext *ctx, int clut_id)\n{\n DVBSubCLUT *ptr = ctx->clut_list;\n while (ptr && ptr->id != clut_id) {\n ptr = ptr->next;\n }\n return ptr;\n}', 'void *av_malloc(unsigned int size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-16) )\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size+16);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&15) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr,16,size))\n ptr = NULL;\n#elif HAVE_MEMALIGN\n ptr = memalign(16,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}']
36,469
0
https://github.com/openssl/openssl/blob/9c46f4b9cd4912b61cb546c48b678488d7f26ed6/crypto/evp/evp_test.c/#L138
static void test_ctx_replace(EVP_CIPHER_CTX **pctx) { EVP_CIPHER_CTX *ctx_copy; ctx_copy = EVP_CIPHER_CTX_new(); EVP_CIPHER_CTX_copy(ctx_copy, *pctx); EVP_CIPHER_CTX_free(*pctx); *pctx = ctx_copy; }
['static void test_ctx_replace(EVP_CIPHER_CTX **pctx)\n{\n EVP_CIPHER_CTX *ctx_copy;\n ctx_copy = EVP_CIPHER_CTX_new();\n EVP_CIPHER_CTX_copy(ctx_copy, *pctx);\n EVP_CIPHER_CTX_free(*pctx);\n *pctx = ctx_copy;\n}', 'EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void)\n{\n EVP_CIPHER_CTX *ctx = OPENSSL_malloc(sizeof *ctx);\n if (ctx)\n EVP_CIPHER_CTX_init(ctx);\n return ctx;\n}', 'void *CRYPTO_malloc(int num, const char *file, int line)\n{\n void *ret = NULL;\n if (num <= 0)\n return NULL;\n if (allow_customize)\n allow_customize = 0;\n if (malloc_debug_func != NULL) {\n if (allow_customize_debug)\n allow_customize_debug = 0;\n malloc_debug_func(NULL, num, file, line, 0);\n }\n ret = malloc_ex_func(num, file, line);\n#ifdef LEVITTE_DEBUG_MEM\n fprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\\n", ret, num);\n#endif\n if (malloc_debug_func != NULL)\n malloc_debug_func(ret, num, file, line, 1);\n#ifndef OPENSSL_CPUID_OBJ\n if (ret && (num > 2048)) {\n extern unsigned char cleanse_ctr;\n ((unsigned char *)ret)[0] = cleanse_ctr;\n }\n#endif\n return ret;\n}']
36,470
0
https://github.com/libav/libav/blob/831018b0bbe26a603802a9022472f714a59293be/libavutil/mem.c/#L139
int av_reallocp(void *ptr, size_t size) { void *val; if (!size) { av_freep(ptr); return 0; } memcpy(&val, ptr, sizeof(val)); val = av_realloc(val, size); if (!val) { av_freep(ptr); return AVERROR(ENOMEM); } memcpy(ptr, &val, sizeof(val)); return 0; }
['int av_reallocp(void *ptr, size_t size)\n{\n void *val;\n if (!size) {\n av_freep(ptr);\n return 0;\n }\n memcpy(&val, ptr, sizeof(val));\n val = av_realloc(val, size);\n if (!val) {\n av_freep(ptr);\n return AVERROR(ENOMEM);\n }\n memcpy(ptr, &val, sizeof(val));\n return 0;\n}', 'void *av_realloc(void *ptr, size_t size)\n{\n if (size > (INT_MAX - 16))\n return NULL;\n#if HAVE_ALIGNED_MALLOC\n return _aligned_realloc(ptr, size, 32);\n#else\n return realloc(ptr, size);\n#endif\n}']
36,471
0
https://github.com/openssl/openssl/blob/7bf7333d688264f6d389c1c3c87c127f484b2efa/apps/pkeyutl.c/#L330
int MAIN(int argc, char **argv) { BIO *in = NULL, *out = NULL; char *infile = NULL, *outfile = NULL, *sigfile = NULL; ENGINE *e = NULL; int pkey_op = EVP_PKEY_OP_SIGN, key_type = KEY_PRIVKEY; int keyform = FORMAT_PEM, peerform = FORMAT_PEM; char badarg = 0, rev = 0; char hexdump = 0, asn1parse = 0; EVP_PKEY_CTX *ctx = NULL; char *passargin = NULL; int keysize = -1; unsigned char *buf_in = NULL, *buf_out = NULL, *sig = NULL; int buf_inlen, buf_outlen, siglen = -1; int ret = 1, rv = -1; argc--; argv++; if(!bio_err) bio_err = BIO_new_fp(stderr, BIO_NOCLOSE); if (!load_config(bio_err, NULL)) goto end; ERR_load_crypto_strings(); OpenSSL_add_all_algorithms(); while(argc >= 1) { if (!strcmp(*argv,"-in")) { if (--argc < 1) badarg = 1; infile= *(++argv); } else if (!strcmp(*argv,"-out")) { if (--argc < 1) badarg = 1; outfile= *(++argv); } else if (!strcmp(*argv,"-sigfile")) { if (--argc < 1) badarg = 1; sigfile= *(++argv); } else if(!strcmp(*argv, "-inkey")) { if (--argc < 1) badarg = 1; else { ctx = init_ctx(&keysize, *(++argv), keyform, key_type, passargin, pkey_op, e); if (!ctx) { BIO_puts(bio_err, "Error initializing context\n"); ERR_print_errors(bio_err); badarg = 1; } } } else if (!strcmp(*argv,"-peerkey")) { if (--argc < 1) badarg = 1; else if (!setup_peer(bio_err, ctx, peerform, *(++argv))) badarg = 1; } else if (!strcmp(*argv,"-passin")) { if (--argc < 1) badarg = 1; passargin= *(++argv); } else if (strcmp(*argv,"-peerform") == 0) { if (--argc < 1) badarg = 1; peerform=str2fmt(*(++argv)); } else if (strcmp(*argv,"-keyform") == 0) { if (--argc < 1) badarg = 1; keyform=str2fmt(*(++argv)); } #ifndef OPENSSL_NO_ENGINE else if(!strcmp(*argv, "-engine")) { if (--argc < 1) badarg = 1; else e = setup_engine(bio_err, *(++argv), 0); } #endif else if(!strcmp(*argv, "-pubin")) key_type = KEY_PUBKEY; else if(!strcmp(*argv, "-certin")) key_type = KEY_CERT; else if(!strcmp(*argv, "-asn1parse")) asn1parse = 1; else if(!strcmp(*argv, "-hexdump")) hexdump = 1; else if(!strcmp(*argv, "-sign")) pkey_op = EVP_PKEY_OP_SIGN; else if(!strcmp(*argv, "-verify")) pkey_op = EVP_PKEY_OP_VERIFY; else if(!strcmp(*argv, "-verifyrecover")) pkey_op = EVP_PKEY_OP_VERIFYRECOVER; else if(!strcmp(*argv, "-rev")) rev = 1; else if(!strcmp(*argv, "-encrypt")) pkey_op = EVP_PKEY_OP_ENCRYPT; else if(!strcmp(*argv, "-decrypt")) pkey_op = EVP_PKEY_OP_DECRYPT; else if(!strcmp(*argv, "-derive")) pkey_op = EVP_PKEY_OP_DERIVE; else if (strcmp(*argv,"-pkeyopt") == 0) { if (--argc < 1) badarg = 1; else if (!ctx) { BIO_puts(bio_err, "-pkeyopt command before -inkey\n"); badarg = 1; } else if (pkey_ctrl_string(ctx, *(++argv)) <= 0) { BIO_puts(bio_err, "parameter setting error\n"); ERR_print_errors(bio_err); goto end; } } else badarg = 1; if(badarg) { usage(); goto end; } argc--; argv++; } if (!ctx) { usage(); goto end; } if (sigfile && (pkey_op != EVP_PKEY_OP_VERIFY)) { BIO_puts(bio_err, "Signature file specified for non verify\n"); goto end; } if (!sigfile && (pkey_op == EVP_PKEY_OP_VERIFY)) { BIO_puts(bio_err, "No signature file specified for verify\n"); goto end; } app_RAND_load_file(NULL, bio_err, 0); if (pkey_op != EVP_PKEY_OP_DERIVE) { if(infile) { if(!(in = BIO_new_file(infile, "rb"))) { BIO_puts(bio_err, "Error Opening Input File\n"); ERR_print_errors(bio_err); goto end; } } else in = BIO_new_fp(stdin, BIO_NOCLOSE); } if(outfile) { if(!(out = BIO_new_file(outfile, "wb"))) { BIO_printf(bio_err, "Error Creating Output File\n"); ERR_print_errors(bio_err); goto end; } } else { out = BIO_new_fp(stdout, BIO_NOCLOSE); #ifdef OPENSSL_SYS_VMS { BIO *tmpbio = BIO_new(BIO_f_linebuffer()); out = BIO_push(tmpbio, out); } #endif } if (sigfile) { BIO *sigbio = BIO_new_file(sigfile, "rb"); if (!sigbio) { BIO_printf(bio_err, "Can't open signature file %s\n", sigfile); goto end; } siglen = bio_to_mem(&sig, keysize * 10, sigbio); BIO_free(sigbio); if (siglen <= 0) { BIO_printf(bio_err, "Error reading signature data\n"); goto end; } } if (in) { buf_inlen = bio_to_mem(&buf_in, keysize * 10, in); if(buf_inlen <= 0) { BIO_printf(bio_err, "Error reading input Data\n"); exit(1); } if(rev) { int i; unsigned char ctmp; for(i = 0; i < buf_inlen/2; i++) { ctmp = buf_in[i]; buf_in[i] = buf_in[buf_inlen - 1 - i]; buf_in[buf_inlen - 1 - i] = ctmp; } } } if(pkey_op == EVP_PKEY_OP_VERIFY) { rv = EVP_PKEY_verify(ctx, sig, siglen, buf_in, buf_inlen); if (rv == 0) BIO_puts(out, "Signature Verification Failure\n"); else if (rv == 1) BIO_puts(out, "Signature Verified Successfully\n"); if (rv >= 0) goto end; } else { rv = do_keyop(ctx, pkey_op, NULL, &buf_outlen, buf_in, buf_inlen); if (rv > 0) { buf_out = OPENSSL_malloc(buf_outlen); if (!buf_out) rv = -1; else rv = do_keyop(ctx, pkey_op, buf_out, &buf_outlen, buf_in, buf_inlen); } } if(rv <= 0) { BIO_printf(bio_err, "Public Key operation error\n"); ERR_print_errors(bio_err); goto end; } ret = 0; if(asn1parse) { if(!ASN1_parse_dump(out, buf_out, buf_outlen, 1, -1)) ERR_print_errors(bio_err); } else if(hexdump) BIO_dump(out, (char *)buf_out, buf_outlen); else BIO_write(out, buf_out, buf_outlen); end: if (ctx) EVP_PKEY_CTX_free(ctx); BIO_free(in); BIO_free_all(out); if (buf_in) OPENSSL_free(buf_in); if (buf_out) OPENSSL_free(buf_out); if (sig) OPENSSL_free(sig); return ret; }
['int MAIN(int argc, char **argv)\n{\n\tBIO *in = NULL, *out = NULL;\n\tchar *infile = NULL, *outfile = NULL, *sigfile = NULL;\n\tENGINE *e = NULL;\n\tint pkey_op = EVP_PKEY_OP_SIGN, key_type = KEY_PRIVKEY;\n\tint keyform = FORMAT_PEM, peerform = FORMAT_PEM;\n\tchar badarg = 0, rev = 0;\n\tchar hexdump = 0, asn1parse = 0;\n\tEVP_PKEY_CTX *ctx = NULL;\n\tchar *passargin = NULL;\n\tint keysize = -1;\n\tunsigned char *buf_in = NULL, *buf_out = NULL, *sig = NULL;\n\tint buf_inlen, buf_outlen, siglen = -1;\n\tint ret = 1, rv = -1;\n\targc--;\n\targv++;\n\tif(!bio_err) bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);\n\tif (!load_config(bio_err, NULL))\n\t\tgoto end;\n\tERR_load_crypto_strings();\n\tOpenSSL_add_all_algorithms();\n\twhile(argc >= 1)\n\t\t{\n\t\tif (!strcmp(*argv,"-in"))\n\t\t\t{\n\t\t\tif (--argc < 1) badarg = 1;\n infile= *(++argv);\n\t\t\t}\n\t\telse if (!strcmp(*argv,"-out"))\n\t\t\t{\n\t\t\tif (--argc < 1) badarg = 1;\n\t\t\toutfile= *(++argv);\n\t\t\t}\n\t\telse if (!strcmp(*argv,"-sigfile"))\n\t\t\t{\n\t\t\tif (--argc < 1) badarg = 1;\n\t\t\tsigfile= *(++argv);\n\t\t\t}\n\t\telse if(!strcmp(*argv, "-inkey"))\n\t\t\t{\n\t\t\tif (--argc < 1)\n\t\t\t\tbadarg = 1;\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tctx = init_ctx(&keysize,\n\t\t\t\t\t\t*(++argv), keyform, key_type,\n\t\t\t\t\t\tpassargin, pkey_op, e);\n\t\t\t\tif (!ctx)\n\t\t\t\t\t{\n\t\t\t\t\tBIO_puts(bio_err,\n\t\t\t\t\t\t"Error initializing context\\n");\n\t\t\t\t\tERR_print_errors(bio_err);\n\t\t\t\t\tbadarg = 1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\telse if (!strcmp(*argv,"-peerkey"))\n\t\t\t{\n\t\t\tif (--argc < 1)\n\t\t\t\tbadarg = 1;\n\t\t\telse if (!setup_peer(bio_err, ctx, peerform, *(++argv)))\n\t\t\t\tbadarg = 1;\n\t\t\t}\n\t\telse if (!strcmp(*argv,"-passin"))\n\t\t\t{\n\t\t\tif (--argc < 1) badarg = 1;\n\t\t\tpassargin= *(++argv);\n\t\t\t}\n\t\telse if (strcmp(*argv,"-peerform") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) badarg = 1;\n\t\t\tpeerform=str2fmt(*(++argv));\n\t\t\t}\n\t\telse if (strcmp(*argv,"-keyform") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) badarg = 1;\n\t\t\tkeyform=str2fmt(*(++argv));\n\t\t\t}\n#ifndef OPENSSL_NO_ENGINE\n\t\telse if(!strcmp(*argv, "-engine"))\n\t\t\t{\n\t\t\tif (--argc < 1)\n\t\t\t\tbadarg = 1;\n\t\t\telse\n\t\t\t\te = setup_engine(bio_err, *(++argv), 0);\n\t\t\t}\n#endif\n\t\telse if(!strcmp(*argv, "-pubin"))\n\t\t\tkey_type = KEY_PUBKEY;\n\t\telse if(!strcmp(*argv, "-certin"))\n\t\t\tkey_type = KEY_CERT;\n\t\telse if(!strcmp(*argv, "-asn1parse"))\n\t\t\tasn1parse = 1;\n\t\telse if(!strcmp(*argv, "-hexdump"))\n\t\t\thexdump = 1;\n\t\telse if(!strcmp(*argv, "-sign"))\n\t\t\tpkey_op = EVP_PKEY_OP_SIGN;\n\t\telse if(!strcmp(*argv, "-verify"))\n\t\t\tpkey_op = EVP_PKEY_OP_VERIFY;\n\t\telse if(!strcmp(*argv, "-verifyrecover"))\n\t\t\tpkey_op = EVP_PKEY_OP_VERIFYRECOVER;\n\t\telse if(!strcmp(*argv, "-rev"))\n\t\t\trev = 1;\n\t\telse if(!strcmp(*argv, "-encrypt"))\n\t\t\tpkey_op = EVP_PKEY_OP_ENCRYPT;\n\t\telse if(!strcmp(*argv, "-decrypt"))\n\t\t\tpkey_op = EVP_PKEY_OP_DECRYPT;\n\t\telse if(!strcmp(*argv, "-derive"))\n\t\t\tpkey_op = EVP_PKEY_OP_DERIVE;\n\t\telse if (strcmp(*argv,"-pkeyopt") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1)\n\t\t\t\tbadarg = 1;\n\t\t\telse if (!ctx)\n\t\t\t\t{\n\t\t\t\tBIO_puts(bio_err,\n\t\t\t\t\t"-pkeyopt command before -inkey\\n");\n\t\t\t\tbadarg = 1;\n\t\t\t\t}\n\t\t\telse if (pkey_ctrl_string(ctx, *(++argv)) <= 0)\n\t\t\t\t{\n\t\t\t\tBIO_puts(bio_err, "parameter setting error\\n");\n\t\t\t\tERR_print_errors(bio_err);\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\t}\n\t\telse badarg = 1;\n\t\tif(badarg)\n\t\t\t{\n\t\t\tusage();\n\t\t\tgoto end;\n\t\t\t}\n\t\targc--;\n\t\targv++;\n\t\t}\n\tif (!ctx)\n\t\t{\n\t\tusage();\n\t\tgoto end;\n\t\t}\n\tif (sigfile && (pkey_op != EVP_PKEY_OP_VERIFY))\n\t\t{\n\t\tBIO_puts(bio_err, "Signature file specified for non verify\\n");\n\t\tgoto end;\n\t\t}\n\tif (!sigfile && (pkey_op == EVP_PKEY_OP_VERIFY))\n\t\t{\n\t\tBIO_puts(bio_err, "No signature file specified for verify\\n");\n\t\tgoto end;\n\t\t}\n\tapp_RAND_load_file(NULL, bio_err, 0);\n\tif (pkey_op != EVP_PKEY_OP_DERIVE)\n\t\t{\n\t\tif(infile)\n\t\t\t{\n\t\t\tif(!(in = BIO_new_file(infile, "rb")))\n\t\t\t\t{\n\t\t\t\tBIO_puts(bio_err,\n\t\t\t\t\t"Error Opening Input File\\n");\n\t\t\t\tERR_print_errors(bio_err);\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\t}\n\t\telse\n\t\t\tin = BIO_new_fp(stdin, BIO_NOCLOSE);\n\t\t}\n\tif(outfile)\n\t\t{\n\t\tif(!(out = BIO_new_file(outfile, "wb")))\n\t\t\t{\n\t\t\tBIO_printf(bio_err, "Error Creating Output File\\n");\n\t\t\tERR_print_errors(bio_err);\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tout = BIO_new_fp(stdout, BIO_NOCLOSE);\n#ifdef OPENSSL_SYS_VMS\n\t\t{\n\t\t BIO *tmpbio = BIO_new(BIO_f_linebuffer());\n\t\t out = BIO_push(tmpbio, out);\n\t\t}\n#endif\n\t}\n\tif (sigfile)\n\t\t{\n\t\tBIO *sigbio = BIO_new_file(sigfile, "rb");\n\t\tif (!sigbio)\n\t\t\t{\n\t\t\tBIO_printf(bio_err, "Can\'t open signature file %s\\n",\n\t\t\t\t\t\t\t\tsigfile);\n\t\t\tgoto end;\n\t\t\t}\n\t\tsiglen = bio_to_mem(&sig, keysize * 10, sigbio);\n\t\tBIO_free(sigbio);\n\t\tif (siglen <= 0)\n\t\t\t{\n\t\t\tBIO_printf(bio_err, "Error reading signature data\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n\tif (in)\n\t\t{\n\t\tbuf_inlen = bio_to_mem(&buf_in, keysize * 10, in);\n\t\tif(buf_inlen <= 0)\n\t\t\t{\n\t\t\tBIO_printf(bio_err, "Error reading input Data\\n");\n\t\t\texit(1);\n\t\t\t}\n\t\tif(rev)\n\t\t\t{\n\t\t\tint i;\n\t\t\tunsigned char ctmp;\n\t\t\tfor(i = 0; i < buf_inlen/2; i++)\n\t\t\t\t{\n\t\t\t\tctmp = buf_in[i];\n\t\t\t\tbuf_in[i] = buf_in[buf_inlen - 1 - i];\n\t\t\t\tbuf_in[buf_inlen - 1 - i] = ctmp;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\tif(pkey_op == EVP_PKEY_OP_VERIFY)\n\t\t{\n\t\trv = EVP_PKEY_verify(ctx, sig, siglen, buf_in, buf_inlen);\n\t\tif (rv == 0)\n\t\t\tBIO_puts(out, "Signature Verification Failure\\n");\n\t\telse if (rv == 1)\n\t\t\tBIO_puts(out, "Signature Verified Successfully\\n");\n\t\tif (rv >= 0)\n\t\t\tgoto end;\n\t\t}\n\telse\n\t\t{\n\t\trv = do_keyop(ctx, pkey_op, NULL, &buf_outlen,\n\t\t\t\t\t\t\tbuf_in, buf_inlen);\n\t\tif (rv > 0)\n\t\t\t{\n\t\t\tbuf_out = OPENSSL_malloc(buf_outlen);\n\t\t\tif (!buf_out)\n\t\t\t\trv = -1;\n\t\t\telse\n\t\t\t\trv = do_keyop(ctx, pkey_op,\n\t\t\t\t\t\tbuf_out, &buf_outlen,\n\t\t\t\t\t\tbuf_in, buf_inlen);\n\t\t\t}\n\t\t}\n\tif(rv <= 0)\n\t\t{\n\t\tBIO_printf(bio_err, "Public Key operation error\\n");\n\t\tERR_print_errors(bio_err);\n\t\tgoto end;\n\t\t}\n\tret = 0;\n\tif(asn1parse)\n\t\t{\n\t\tif(!ASN1_parse_dump(out, buf_out, buf_outlen, 1, -1))\n\t\t\tERR_print_errors(bio_err);\n\t\t}\n\telse if(hexdump)\n\t\tBIO_dump(out, (char *)buf_out, buf_outlen);\n\telse\n\t\tBIO_write(out, buf_out, buf_outlen);\n\tend:\n\tif (ctx)\n\t\tEVP_PKEY_CTX_free(ctx);\n\tBIO_free(in);\n\tBIO_free_all(out);\n\tif (buf_in)\n\t\tOPENSSL_free(buf_in);\n\tif (buf_out)\n\t\tOPENSSL_free(buf_out);\n\tif (sig)\n\t\tOPENSSL_free(sig);\n\treturn ret;\n}']
36,472
0
https://github.com/openssl/openssl/blob/0ca6d7c6b1e73d17ca67b7ffd8435bde43bf50af/apps/ca.c/#L1511
static int do_body(X509 **xret, EVP_PKEY *pkey, X509 *x509, const EVP_MD *dgst, STACK_OF(CONF_VALUE) *policy, TXT_DB *db, BIGNUM *serial, char *startdate, char *enddate, int days, int batch, int verbose, X509_REQ *req, char *ext_sect, LHASH *lconf) { X509_NAME *name=NULL,*CAname=NULL,*subject=NULL; ASN1_UTCTIME *tm,*tmptm; ASN1_STRING *str,*str2; ASN1_OBJECT *obj; X509 *ret=NULL; X509_CINF *ci; X509_NAME_ENTRY *ne; X509_NAME_ENTRY *tne,*push; EVP_PKEY *pktmp; int ok= -1,i,j,last,nid; char *p; CONF_VALUE *cv; char *row[DB_NUMBER],**rrow,**irow=NULL; char buf[25],*pbuf; tmptm=ASN1_UTCTIME_new(); if (tmptm == NULL) { BIO_printf(bio_err,"malloc error\n"); return(0); } for (i=0; i<DB_NUMBER; i++) row[i]=NULL; BIO_printf(bio_err,"The Subjects Distinguished Name is as follows\n"); name=X509_REQ_get_subject_name(req); for (i=0; i<X509_NAME_entry_count(name); i++) { ne=(X509_NAME_ENTRY *)X509_NAME_get_entry(name,i); obj=X509_NAME_ENTRY_get_object(ne); j=i2a_ASN1_OBJECT(bio_err,obj); str=X509_NAME_ENTRY_get_data(ne); pbuf=buf; for (j=22-j; j>0; j--) *(pbuf++)=' '; *(pbuf++)=':'; *(pbuf++)='\0'; BIO_puts(bio_err,buf); if (msie_hack) { nid=OBJ_obj2nid(ne->object); if (str->type == V_ASN1_UNIVERSALSTRING) ASN1_UNIVERSALSTRING_to_string(str); if ((str->type == V_ASN1_IA5STRING) && (nid != NID_pkcs9_emailAddress)) str->type=V_ASN1_T61STRING; if ((nid == NID_pkcs9_emailAddress) && (str->type == V_ASN1_PRINTABLESTRING)) str->type=V_ASN1_IA5STRING; } if (str->type == V_ASN1_PRINTABLESTRING) BIO_printf(bio_err,"PRINTABLE:'"); else if (str->type == V_ASN1_T61STRING) BIO_printf(bio_err,"T61STRING:'"); else if (str->type == V_ASN1_IA5STRING) BIO_printf(bio_err,"IA5STRING:'"); else if (str->type == V_ASN1_UNIVERSALSTRING) BIO_printf(bio_err,"UNIVERSALSTRING:'"); else BIO_printf(bio_err,"ASN.1 %2d:'",str->type); if ((OBJ_obj2nid(obj) == NID_pkcs9_emailAddress) && (str->type != V_ASN1_IA5STRING)) { BIO_printf(bio_err,"\nemailAddress type needs to be of type IA5STRING\n"); goto err; } j=ASN1_PRINTABLE_type(str->data,str->length); if ( ((j == V_ASN1_T61STRING) && (str->type != V_ASN1_T61STRING)) || ((j == V_ASN1_IA5STRING) && (str->type == V_ASN1_PRINTABLESTRING))) { BIO_printf(bio_err,"\nThe string contains characters that are illegal for the ASN.1 type\n"); goto err; } p=(char *)str->data; for (j=str->length; j>0; j--) { if ((*p >= ' ') && (*p <= '~')) BIO_printf(bio_err,"%c",*p); else if (*p & 0x80) BIO_printf(bio_err,"\\0x%02X",*p); else if ((unsigned char)*p == 0xf7) BIO_printf(bio_err,"^?"); else BIO_printf(bio_err,"^%c",*p+'@'); p++; } BIO_printf(bio_err,"'\n"); } if ((subject=X509_NAME_new()) == NULL) { BIO_printf(bio_err,"Malloc failure\n"); goto err; } CAname=X509_NAME_dup(x509->cert_info->subject); if (CAname == NULL) goto err; str=str2=NULL; for (i=0; i<sk_CONF_VALUE_num(policy); i++) { cv=sk_CONF_VALUE_value(policy,i); if ((j=OBJ_txt2nid(cv->name)) == NID_undef) { BIO_printf(bio_err,"%s:unknown object type in 'policy' configuration\n",cv->name); goto err; } obj=OBJ_nid2obj(j); last= -1; for (;;) { j=X509_NAME_get_index_by_OBJ(name,obj,last); if (j < 0) { if (last != -1) break; tne=NULL; } else { tne=X509_NAME_get_entry(name,j); } last=j; push=NULL; if (strcmp(cv->value,"optional") == 0) { if (tne != NULL) push=tne; } else if (strcmp(cv->value,"supplied") == 0) { if (tne == NULL) { BIO_printf(bio_err,"The %s field needed to be supplied and was missing\n",cv->name); goto err; } else push=tne; } else if (strcmp(cv->value,"match") == 0) { int last2; if (tne == NULL) { BIO_printf(bio_err,"The mandatory %s field was missing\n",cv->name); goto err; } last2= -1; again2: j=X509_NAME_get_index_by_OBJ(CAname,obj,last2); if ((j < 0) && (last2 == -1)) { BIO_printf(bio_err,"The %s field does not exist in the CA certificate,\nthe 'policy' is misconfigured\n",cv->name); goto err; } if (j >= 0) { push=X509_NAME_get_entry(CAname,j); str=X509_NAME_ENTRY_get_data(tne); str2=X509_NAME_ENTRY_get_data(push); last2=j; if (ASN1_STRING_cmp(str,str2) != 0) goto again2; } if (j < 0) { BIO_printf(bio_err,"The %s field needed to be the same in the\nCA certificate (%s) and the request (%s)\n",cv->name,((str == NULL)?"NULL":(char *)str->data),((str2 == NULL)?"NULL":(char *)str2->data)); goto err; } } else { BIO_printf(bio_err,"%s:invalid type in 'policy' configuration\n",cv->value); goto err; } if (push != NULL) { if (!X509_NAME_add_entry(subject,push, X509_NAME_entry_count(subject),0)) { if (push != NULL) X509_NAME_ENTRY_free(push); BIO_printf(bio_err,"Malloc failure\n"); goto err; } } if (j < 0) break; } } if (preserve) { X509_NAME_free(subject); subject=X509_NAME_dup(X509_REQ_get_subject_name(req)); if (subject == NULL) goto err; } if (verbose) BIO_printf(bio_err,"The subject name apears to be ok, checking data base for clashes\n"); row[DB_name]=X509_NAME_oneline(subject,NULL,0); row[DB_serial]=BN_bn2hex(serial); if ((row[DB_name] == NULL) || (row[DB_serial] == NULL)) { BIO_printf(bio_err,"Malloc failure\n"); goto err; } rrow=TXT_DB_get_by_index(db,DB_name,row); if (rrow != NULL) { BIO_printf(bio_err,"ERROR:There is already a certificate for %s\n", row[DB_name]); } else { rrow=TXT_DB_get_by_index(db,DB_serial,row); if (rrow != NULL) { BIO_printf(bio_err,"ERROR:Serial number %s has already been issued,\n", row[DB_serial]); BIO_printf(bio_err," check the database/serial_file for corruption\n"); } } if (rrow != NULL) { BIO_printf(bio_err, "The matching entry has the following details\n"); if (rrow[DB_type][0] == 'E') p="Expired"; else if (rrow[DB_type][0] == 'R') p="Revoked"; else if (rrow[DB_type][0] == 'V') p="Valid"; else p="\ninvalid type, Data base error\n"; BIO_printf(bio_err,"Type :%s\n",p);; if (rrow[DB_type][0] == 'R') { p=rrow[DB_exp_date]; if (p == NULL) p="undef"; BIO_printf(bio_err,"Was revoked on:%s\n",p); } p=rrow[DB_exp_date]; if (p == NULL) p="undef"; BIO_printf(bio_err,"Expires on :%s\n",p); p=rrow[DB_serial]; if (p == NULL) p="undef"; BIO_printf(bio_err,"Serial Number :%s\n",p); p=rrow[DB_file]; if (p == NULL) p="undef"; BIO_printf(bio_err,"File name :%s\n",p); p=rrow[DB_name]; if (p == NULL) p="undef"; BIO_printf(bio_err,"Subject Name :%s\n",p); ok= -1; goto err; } if (verbose) BIO_printf(bio_err,"Everything appears to be ok, creating and signing the certificate\n"); if ((ret=X509_new()) == NULL) goto err; ci=ret->cert_info; #ifdef X509_V3 if (!X509_set_version(x509,2)) goto err; #endif if (BN_to_ASN1_INTEGER(serial,ci->serialNumber) == NULL) goto err; if (!X509_set_issuer_name(ret,X509_get_subject_name(x509))) goto err; BIO_printf(bio_err,"Certificate is to be certified until "); if (strcmp(startdate,"today") == 0) X509_gmtime_adj(X509_get_notBefore(ret),0); else ASN1_UTCTIME_set_string(X509_get_notBefore(ret),startdate); if (enddate == NULL) X509_gmtime_adj(X509_get_notAfter(ret),(long)60*60*24*days); else ASN1_UTCTIME_set_string(X509_get_notAfter(ret),enddate); ASN1_UTCTIME_print(bio_err,X509_get_notAfter(ret)); if(days) BIO_printf(bio_err," (%d days)",days); BIO_printf(bio_err, "\n"); if (!X509_set_subject_name(ret,subject)) goto err; pktmp=X509_REQ_get_pubkey(req); i = X509_set_pubkey(ret,pktmp); EVP_PKEY_free(pktmp); if (!i) goto err; if (ext_sect) { X509V3_CTX ctx; if (ci->version == NULL) if ((ci->version=ASN1_INTEGER_new()) == NULL) goto err; ASN1_INTEGER_set(ci->version,2); if (ci->extensions != NULL) sk_X509_EXTENSION_pop_free(ci->extensions, X509_EXTENSION_free); ci->extensions = NULL; X509V3_set_ctx(&ctx, x509, ret, req, NULL, 0); X509V3_set_conf_lhash(&ctx, lconf); if(!X509V3_EXT_add_conf(lconf, &ctx, ext_sect, ret)) goto err; } if (!batch) { BIO_printf(bio_err,"Sign the certificate? [y/n]:"); (void)BIO_flush(bio_err); buf[0]='\0'; fgets(buf,sizeof(buf)-1,stdin); if (!((buf[0] == 'y') || (buf[0] == 'Y'))) { BIO_printf(bio_err,"CERTIFICATE WILL NOT BE CERTIFIED\n"); ok=0; goto err; } } #ifndef NO_DSA if (pkey->type == EVP_PKEY_DSA) dgst=EVP_dss1(); pktmp=X509_get_pubkey(ret); if (EVP_PKEY_missing_parameters(pktmp) && !EVP_PKEY_missing_parameters(pkey)) EVP_PKEY_copy_parameters(pktmp,pkey); EVP_PKEY_free(pktmp); #endif if (!X509_sign(ret,pkey,dgst)) goto err; row[DB_type]=(char *)Malloc(2); tm=X509_get_notAfter(ret); row[DB_exp_date]=(char *)Malloc(tm->length+1); memcpy(row[DB_exp_date],tm->data,tm->length); row[DB_exp_date][tm->length]='\0'; row[DB_rev_date]=NULL; row[DB_file]=(char *)Malloc(8); if ((row[DB_type] == NULL) || (row[DB_exp_date] == NULL) || (row[DB_file] == NULL)) { BIO_printf(bio_err,"Malloc failure\n"); goto err; } strcpy(row[DB_file],"unknown"); row[DB_type][0]='V'; row[DB_type][1]='\0'; if ((irow=(char **)Malloc(sizeof(char *)*(DB_NUMBER+1))) == NULL) { BIO_printf(bio_err,"Malloc failure\n"); goto err; } for (i=0; i<DB_NUMBER; i++) { irow[i]=row[i]; row[i]=NULL; } irow[DB_NUMBER]=NULL; if (!TXT_DB_insert(db,irow)) { BIO_printf(bio_err,"failed to update database\n"); BIO_printf(bio_err,"TXT_DB error number %ld\n",db->error); goto err; } ok=1; err: for (i=0; i<DB_NUMBER; i++) if (row[i] != NULL) Free(row[i]); if (CAname != NULL) X509_NAME_free(CAname); if (subject != NULL) X509_NAME_free(subject); if (ok <= 0) { if (ret != NULL) X509_free(ret); ret=NULL; } else *xret=ret; return(ok); }
['static int do_body(X509 **xret, EVP_PKEY *pkey, X509 *x509, const EVP_MD *dgst,\n\t STACK_OF(CONF_VALUE) *policy, TXT_DB *db, BIGNUM *serial,\n\t char *startdate, char *enddate, int days, int batch, int verbose,\n\t X509_REQ *req, char *ext_sect, LHASH *lconf)\n\t{\n\tX509_NAME *name=NULL,*CAname=NULL,*subject=NULL;\n\tASN1_UTCTIME *tm,*tmptm;\n\tASN1_STRING *str,*str2;\n\tASN1_OBJECT *obj;\n\tX509 *ret=NULL;\n\tX509_CINF *ci;\n\tX509_NAME_ENTRY *ne;\n\tX509_NAME_ENTRY *tne,*push;\n\tEVP_PKEY *pktmp;\n\tint ok= -1,i,j,last,nid;\n\tchar *p;\n\tCONF_VALUE *cv;\n\tchar *row[DB_NUMBER],**rrow,**irow=NULL;\n\tchar buf[25],*pbuf;\n\ttmptm=ASN1_UTCTIME_new();\n\tif (tmptm == NULL)\n\t\t{\n\t\tBIO_printf(bio_err,"malloc error\\n");\n\t\treturn(0);\n\t\t}\n\tfor (i=0; i<DB_NUMBER; i++)\n\t\trow[i]=NULL;\n\tBIO_printf(bio_err,"The Subjects Distinguished Name is as follows\\n");\n\tname=X509_REQ_get_subject_name(req);\n\tfor (i=0; i<X509_NAME_entry_count(name); i++)\n\t\t{\n\t\tne=(X509_NAME_ENTRY *)X509_NAME_get_entry(name,i);\n\t\tobj=X509_NAME_ENTRY_get_object(ne);\n\t\tj=i2a_ASN1_OBJECT(bio_err,obj);\n\t\tstr=X509_NAME_ENTRY_get_data(ne);\n\t\tpbuf=buf;\n\t\tfor (j=22-j; j>0; j--)\n\t\t\t*(pbuf++)=\' \';\n\t\t*(pbuf++)=\':\';\n\t\t*(pbuf++)=\'\\0\';\n\t\tBIO_puts(bio_err,buf);\n\t\tif (msie_hack)\n\t\t\t{\n\t\t\tnid=OBJ_obj2nid(ne->object);\n\t\t\tif (str->type == V_ASN1_UNIVERSALSTRING)\n\t\t\t\tASN1_UNIVERSALSTRING_to_string(str);\n\t\t\tif ((str->type == V_ASN1_IA5STRING) &&\n\t\t\t\t(nid != NID_pkcs9_emailAddress))\n\t\t\t\tstr->type=V_ASN1_T61STRING;\n\t\t\tif ((nid == NID_pkcs9_emailAddress) &&\n\t\t\t\t(str->type == V_ASN1_PRINTABLESTRING))\n\t\t\t\tstr->type=V_ASN1_IA5STRING;\n\t\t\t}\n\t\tif (str->type == V_ASN1_PRINTABLESTRING)\n\t\t\tBIO_printf(bio_err,"PRINTABLE:\'");\n\t\telse if (str->type == V_ASN1_T61STRING)\n\t\t\tBIO_printf(bio_err,"T61STRING:\'");\n\t\telse if (str->type == V_ASN1_IA5STRING)\n\t\t\tBIO_printf(bio_err,"IA5STRING:\'");\n\t\telse if (str->type == V_ASN1_UNIVERSALSTRING)\n\t\t\tBIO_printf(bio_err,"UNIVERSALSTRING:\'");\n\t\telse\n\t\t\tBIO_printf(bio_err,"ASN.1 %2d:\'",str->type);\n\t\tif ((OBJ_obj2nid(obj) == NID_pkcs9_emailAddress) &&\n\t\t\t(str->type != V_ASN1_IA5STRING))\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"\\nemailAddress type needs to be of type IA5STRING\\n");\n\t\t\tgoto err;\n\t\t\t}\n\t\tj=ASN1_PRINTABLE_type(str->data,str->length);\n\t\tif (\t((j == V_ASN1_T61STRING) &&\n\t\t\t (str->type != V_ASN1_T61STRING)) ||\n\t\t\t((j == V_ASN1_IA5STRING) &&\n\t\t\t (str->type == V_ASN1_PRINTABLESTRING)))\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"\\nThe string contains characters that are illegal for the ASN.1 type\\n");\n\t\t\tgoto err;\n\t\t\t}\n\t\tp=(char *)str->data;\n\t\tfor (j=str->length; j>0; j--)\n\t\t\t{\n\t\t\tif ((*p >= \' \') && (*p <= \'~\'))\n\t\t\t\tBIO_printf(bio_err,"%c",*p);\n\t\t\telse if (*p & 0x80)\n\t\t\t\tBIO_printf(bio_err,"\\\\0x%02X",*p);\n\t\t\telse if ((unsigned char)*p == 0xf7)\n\t\t\t\tBIO_printf(bio_err,"^?");\n\t\t\telse\tBIO_printf(bio_err,"^%c",*p+\'@\');\n\t\t\tp++;\n\t\t\t}\n\t\tBIO_printf(bio_err,"\'\\n");\n\t\t}\n\tif ((subject=X509_NAME_new()) == NULL)\n\t\t{\n\t\tBIO_printf(bio_err,"Malloc failure\\n");\n\t\tgoto err;\n\t\t}\n\tCAname=X509_NAME_dup(x509->cert_info->subject);\n\tif (CAname == NULL) goto err;\n\tstr=str2=NULL;\n\tfor (i=0; i<sk_CONF_VALUE_num(policy); i++)\n\t\t{\n\t\tcv=sk_CONF_VALUE_value(policy,i);\n\t\tif ((j=OBJ_txt2nid(cv->name)) == NID_undef)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"%s:unknown object type in \'policy\' configuration\\n",cv->name);\n\t\t\tgoto err;\n\t\t\t}\n\t\tobj=OBJ_nid2obj(j);\n\t\tlast= -1;\n\t\tfor (;;)\n\t\t\t{\n\t\t\tj=X509_NAME_get_index_by_OBJ(name,obj,last);\n\t\t\tif (j < 0)\n\t\t\t\t{\n\t\t\t\tif (last != -1) break;\n\t\t\t\ttne=NULL;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\ttne=X509_NAME_get_entry(name,j);\n\t\t\t\t}\n\t\t\tlast=j;\n\t\t\tpush=NULL;\n\t\t\tif (strcmp(cv->value,"optional") == 0)\n\t\t\t\t{\n\t\t\t\tif (tne != NULL)\n\t\t\t\t\tpush=tne;\n\t\t\t\t}\n\t\t\telse if (strcmp(cv->value,"supplied") == 0)\n\t\t\t\t{\n\t\t\t\tif (tne == NULL)\n\t\t\t\t\t{\n\t\t\t\t\tBIO_printf(bio_err,"The %s field needed to be supplied and was missing\\n",cv->name);\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\tpush=tne;\n\t\t\t\t}\n\t\t\telse if (strcmp(cv->value,"match") == 0)\n\t\t\t\t{\n\t\t\t\tint last2;\n\t\t\t\tif (tne == NULL)\n\t\t\t\t\t{\n\t\t\t\t\tBIO_printf(bio_err,"The mandatory %s field was missing\\n",cv->name);\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t\tlast2= -1;\nagain2:\n\t\t\t\tj=X509_NAME_get_index_by_OBJ(CAname,obj,last2);\n\t\t\t\tif ((j < 0) && (last2 == -1))\n\t\t\t\t\t{\n\t\t\t\t\tBIO_printf(bio_err,"The %s field does not exist in the CA certificate,\\nthe \'policy\' is misconfigured\\n",cv->name);\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t\tif (j >= 0)\n\t\t\t\t\t{\n\t\t\t\t\tpush=X509_NAME_get_entry(CAname,j);\n\t\t\t\t\tstr=X509_NAME_ENTRY_get_data(tne);\n\t\t\t\t\tstr2=X509_NAME_ENTRY_get_data(push);\n\t\t\t\t\tlast2=j;\n\t\t\t\t\tif (ASN1_STRING_cmp(str,str2) != 0)\n\t\t\t\t\t\tgoto again2;\n\t\t\t\t\t}\n\t\t\t\tif (j < 0)\n\t\t\t\t\t{\n\t\t\t\t\tBIO_printf(bio_err,"The %s field needed to be the same in the\\nCA certificate (%s) and the request (%s)\\n",cv->name,((str == NULL)?"NULL":(char *)str->data),((str2 == NULL)?"NULL":(char *)str2->data));\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err,"%s:invalid type in \'policy\' configuration\\n",cv->value);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif (push != NULL)\n\t\t\t\t{\n\t\t\t\tif (!X509_NAME_add_entry(subject,push,\n\t\t\t\t\tX509_NAME_entry_count(subject),0))\n\t\t\t\t\t{\n\t\t\t\t\tif (push != NULL)\n\t\t\t\t\t\tX509_NAME_ENTRY_free(push);\n\t\t\t\t\tBIO_printf(bio_err,"Malloc failure\\n");\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\tif (j < 0) break;\n\t\t\t}\n\t\t}\n\tif (preserve)\n\t\t{\n\t\tX509_NAME_free(subject);\n\t\tsubject=X509_NAME_dup(X509_REQ_get_subject_name(req));\n\t\tif (subject == NULL) goto err;\n\t\t}\n\tif (verbose)\n\t\tBIO_printf(bio_err,"The subject name apears to be ok, checking data base for clashes\\n");\n\trow[DB_name]=X509_NAME_oneline(subject,NULL,0);\n\trow[DB_serial]=BN_bn2hex(serial);\n\tif ((row[DB_name] == NULL) || (row[DB_serial] == NULL))\n\t\t{\n\t\tBIO_printf(bio_err,"Malloc failure\\n");\n\t\tgoto err;\n\t\t}\n\trrow=TXT_DB_get_by_index(db,DB_name,row);\n\tif (rrow != NULL)\n\t\t{\n\t\tBIO_printf(bio_err,"ERROR:There is already a certificate for %s\\n",\n\t\t\trow[DB_name]);\n\t\t}\n\telse\n\t\t{\n\t\trrow=TXT_DB_get_by_index(db,DB_serial,row);\n\t\tif (rrow != NULL)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"ERROR:Serial number %s has already been issued,\\n",\n\t\t\t\trow[DB_serial]);\n\t\t\tBIO_printf(bio_err," check the database/serial_file for corruption\\n");\n\t\t\t}\n\t\t}\n\tif (rrow != NULL)\n\t\t{\n\t\tBIO_printf(bio_err,\n\t\t\t"The matching entry has the following details\\n");\n\t\tif (rrow[DB_type][0] == \'E\')\n\t\t\tp="Expired";\n\t\telse if (rrow[DB_type][0] == \'R\')\n\t\t\tp="Revoked";\n\t\telse if (rrow[DB_type][0] == \'V\')\n\t\t\tp="Valid";\n\t\telse\n\t\t\tp="\\ninvalid type, Data base error\\n";\n\t\tBIO_printf(bio_err,"Type\t :%s\\n",p);;\n\t\tif (rrow[DB_type][0] == \'R\')\n\t\t\t{\n\t\t\tp=rrow[DB_exp_date]; if (p == NULL) p="undef";\n\t\t\tBIO_printf(bio_err,"Was revoked on:%s\\n",p);\n\t\t\t}\n\t\tp=rrow[DB_exp_date]; if (p == NULL) p="undef";\n\t\tBIO_printf(bio_err,"Expires on :%s\\n",p);\n\t\tp=rrow[DB_serial]; if (p == NULL) p="undef";\n\t\tBIO_printf(bio_err,"Serial Number :%s\\n",p);\n\t\tp=rrow[DB_file]; if (p == NULL) p="undef";\n\t\tBIO_printf(bio_err,"File name :%s\\n",p);\n\t\tp=rrow[DB_name]; if (p == NULL) p="undef";\n\t\tBIO_printf(bio_err,"Subject Name :%s\\n",p);\n\t\tok= -1;\n\t\tgoto err;\n\t\t}\n\tif (verbose)\n\t\tBIO_printf(bio_err,"Everything appears to be ok, creating and signing the certificate\\n");\n\tif ((ret=X509_new()) == NULL) goto err;\n\tci=ret->cert_info;\n#ifdef X509_V3\n\tif (!X509_set_version(x509,2)) goto err;\n#endif\n\tif (BN_to_ASN1_INTEGER(serial,ci->serialNumber) == NULL)\n\t\tgoto err;\n\tif (!X509_set_issuer_name(ret,X509_get_subject_name(x509)))\n\t\tgoto err;\n\tBIO_printf(bio_err,"Certificate is to be certified until ");\n\tif (strcmp(startdate,"today") == 0)\n\t\tX509_gmtime_adj(X509_get_notBefore(ret),0);\n\telse ASN1_UTCTIME_set_string(X509_get_notBefore(ret),startdate);\n\tif (enddate == NULL)\n\t\tX509_gmtime_adj(X509_get_notAfter(ret),(long)60*60*24*days);\n\telse ASN1_UTCTIME_set_string(X509_get_notAfter(ret),enddate);\n\tASN1_UTCTIME_print(bio_err,X509_get_notAfter(ret));\n\tif(days) BIO_printf(bio_err," (%d days)",days);\n\tBIO_printf(bio_err, "\\n");\n\tif (!X509_set_subject_name(ret,subject)) goto err;\n\tpktmp=X509_REQ_get_pubkey(req);\n\ti = X509_set_pubkey(ret,pktmp);\n\tEVP_PKEY_free(pktmp);\n\tif (!i) goto err;\n\tif (ext_sect)\n\t\t{\n\t\tX509V3_CTX ctx;\n\t\tif (ci->version == NULL)\n\t\t\tif ((ci->version=ASN1_INTEGER_new()) == NULL)\n\t\t\t\tgoto err;\n\t\tASN1_INTEGER_set(ci->version,2);\n\t\tif (ci->extensions != NULL)\n\t\t\tsk_X509_EXTENSION_pop_free(ci->extensions,\n\t\t\t\t\t\t X509_EXTENSION_free);\n\t\tci->extensions = NULL;\n\t\tX509V3_set_ctx(&ctx, x509, ret, req, NULL, 0);\n\t\tX509V3_set_conf_lhash(&ctx, lconf);\n\t\tif(!X509V3_EXT_add_conf(lconf, &ctx, ext_sect, ret)) goto err;\n\t\t}\n\tif (!batch)\n\t\t{\n\t\tBIO_printf(bio_err,"Sign the certificate? [y/n]:");\n\t\t(void)BIO_flush(bio_err);\n\t\tbuf[0]=\'\\0\';\n\t\tfgets(buf,sizeof(buf)-1,stdin);\n\t\tif (!((buf[0] == \'y\') || (buf[0] == \'Y\')))\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"CERTIFICATE WILL NOT BE CERTIFIED\\n");\n\t\t\tok=0;\n\t\t\tgoto err;\n\t\t\t}\n\t\t}\n#ifndef NO_DSA\n\tif (pkey->type == EVP_PKEY_DSA) dgst=EVP_dss1();\n\tpktmp=X509_get_pubkey(ret);\n\tif (EVP_PKEY_missing_parameters(pktmp) &&\n\t\t!EVP_PKEY_missing_parameters(pkey))\n\t\tEVP_PKEY_copy_parameters(pktmp,pkey);\n\tEVP_PKEY_free(pktmp);\n#endif\n\tif (!X509_sign(ret,pkey,dgst))\n\t\tgoto err;\n\trow[DB_type]=(char *)Malloc(2);\n\ttm=X509_get_notAfter(ret);\n\trow[DB_exp_date]=(char *)Malloc(tm->length+1);\n\tmemcpy(row[DB_exp_date],tm->data,tm->length);\n\trow[DB_exp_date][tm->length]=\'\\0\';\n\trow[DB_rev_date]=NULL;\n\trow[DB_file]=(char *)Malloc(8);\n\tif ((row[DB_type] == NULL) || (row[DB_exp_date] == NULL) ||\n\t\t(row[DB_file] == NULL))\n\t\t{\n\t\tBIO_printf(bio_err,"Malloc failure\\n");\n\t\tgoto err;\n\t\t}\n\tstrcpy(row[DB_file],"unknown");\n\trow[DB_type][0]=\'V\';\n\trow[DB_type][1]=\'\\0\';\n\tif ((irow=(char **)Malloc(sizeof(char *)*(DB_NUMBER+1))) == NULL)\n\t\t{\n\t\tBIO_printf(bio_err,"Malloc failure\\n");\n\t\tgoto err;\n\t\t}\n\tfor (i=0; i<DB_NUMBER; i++)\n\t\t{\n\t\tirow[i]=row[i];\n\t\trow[i]=NULL;\n\t\t}\n\tirow[DB_NUMBER]=NULL;\n\tif (!TXT_DB_insert(db,irow))\n\t\t{\n\t\tBIO_printf(bio_err,"failed to update database\\n");\n\t\tBIO_printf(bio_err,"TXT_DB error number %ld\\n",db->error);\n\t\tgoto err;\n\t\t}\n\tok=1;\nerr:\n\tfor (i=0; i<DB_NUMBER; i++)\n\t\tif (row[i] != NULL) Free(row[i]);\n\tif (CAname != NULL)\n\t\tX509_NAME_free(CAname);\n\tif (subject != NULL)\n\t\tX509_NAME_free(subject);\n\tif (ok <= 0)\n\t\t{\n\t\tif (ret != NULL) X509_free(ret);\n\t\tret=NULL;\n\t\t}\n\telse\n\t\t*xret=ret;\n\treturn(ok);\n\t}', 'ASN1_STRING *ASN1_STRING_type_new(int type)\n\t{\n\tASN1_STRING *ret;\n\tret=(ASN1_STRING *)Malloc(sizeof(ASN1_STRING));\n\tif (ret == NULL)\n\t\t{\n\t\tASN1err(ASN1_F_ASN1_STRING_TYPE_NEW,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tret->length=0;\n\tret->type=type;\n\tret->data=NULL;\n\tret->flags=0;\n\treturn(ret);\n\t}']
36,473
0
https://github.com/openssl/openssl/blob/2d5d70b15559f9813054ddb11b30b816daf62ebe/crypto/asn1/a_int.c/#L309
static size_t asn1_put_uint64(unsigned char *b, uint64_t r) { if (r >= 0x100) { unsigned char *p; uint64_t rtmp = r; size_t i = 0; while (rtmp) { rtmp >>= 8; i++; } p = b + i - 1; do { *p-- = r & 0xFF; r >>= 8; } while (p >= b); return i; } b[0] = (unsigned char)r; return 1; }
['static int asn1_string_set_uint64(ASN1_STRING *a, uint64_t r, int itype)\n{\n unsigned char tbuf[sizeof(r)];\n size_t l;\n a->type = itype;\n l = asn1_put_uint64(tbuf, r);\n if (l == 0)\n return 0;\n return ASN1_STRING_set(a, tbuf, l);\n}', 'static size_t asn1_put_uint64(unsigned char *b, uint64_t r)\n{\n if (r >= 0x100) {\n unsigned char *p;\n uint64_t rtmp = r;\n size_t i = 0;\n while (rtmp) {\n rtmp >>= 8;\n i++;\n }\n p = b + i - 1;\n do {\n *p-- = r & 0xFF;\n r >>= 8;\n } while (p >= b);\n return i;\n }\n b[0] = (unsigned char)r;\n return 1;\n}']
36,474
0
https://github.com/openssl/openssl/blob/e3713c365c2657236439fea00822a43aa396d112/crypto/bn/bn_lib.c/#L260
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *a = NULL; bn_check_top(b); if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return (NULL); } if (BN_get_flags(b, BN_FLG_SECURE)) a = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = OPENSSL_zalloc(words * sizeof(*a)); if (a == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return (NULL); } assert(b->top <= words); if (b->top > 0) memcpy(a, b->d, sizeof(*a) * b->top); return a; }
['static int ASIdentifierChoice_is_canonical(ASIdentifierChoice *choice)\n{\n ASN1_INTEGER *a_max_plus_one = NULL;\n BIGNUM *bn = NULL;\n int i, ret = 0;\n if (choice == NULL || choice->type == ASIdentifierChoice_inherit)\n return 1;\n if (choice->type != ASIdentifierChoice_asIdsOrRanges ||\n sk_ASIdOrRange_num(choice->u.asIdsOrRanges) == 0)\n return 0;\n for (i = 0; i < sk_ASIdOrRange_num(choice->u.asIdsOrRanges) - 1; i++) {\n ASIdOrRange *a = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i);\n ASIdOrRange *b = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i + 1);\n ASN1_INTEGER *a_min = NULL, *a_max = NULL, *b_min = NULL, *b_max =\n NULL;\n if (!extract_min_max(a, &a_min, &a_max)\n || !extract_min_max(b, &b_min, &b_max))\n goto done;\n if (ASN1_INTEGER_cmp(a_min, b_min) >= 0 ||\n ASN1_INTEGER_cmp(a_min, a_max) > 0 ||\n ASN1_INTEGER_cmp(b_min, b_max) > 0)\n goto done;\n if ((bn == NULL && (bn = BN_new()) == NULL) ||\n ASN1_INTEGER_to_BN(a_max, bn) == NULL ||\n !BN_add_word(bn, 1) ||\n (a_max_plus_one =\n BN_to_ASN1_INTEGER(bn, a_max_plus_one)) == NULL) {\n X509V3err(X509V3_F_ASIDENTIFIERCHOICE_IS_CANONICAL,\n ERR_R_MALLOC_FAILURE);\n goto done;\n }\n if (ASN1_INTEGER_cmp(a_max_plus_one, b_min) >= 0)\n goto done;\n }\n i = sk_ASIdOrRange_num(choice->u.asIdsOrRanges) - 1;\n {\n ASIdOrRange *a = sk_ASIdOrRange_value(choice->u.asIdsOrRanges, i);\n ASN1_INTEGER *a_min, *a_max;\n if (a != NULL && a->type == ASIdOrRange_range) {\n if (!extract_min_max(a, &a_min, &a_max)\n || ASN1_INTEGER_cmp(a_min, a_max) > 0)\n goto done;\n }\n }\n ret = 1;\n done:\n ASN1_INTEGER_free(a_max_plus_one);\n BN_free(bn);\n return ret;\n}', 'BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn)\n{\n return asn1_string_to_bn(ai, bn, V_ASN1_INTEGER);\n}', 'static BIGNUM *asn1_string_to_bn(const ASN1_INTEGER *ai, BIGNUM *bn,\n int itype)\n{\n BIGNUM *ret;\n if ((ai->type & ~V_ASN1_NEG) != itype) {\n ASN1err(ASN1_F_ASN1_STRING_TO_BN, ASN1_R_WRONG_INTEGER_TYPE);\n return NULL;\n }\n ret = BN_bin2bn(ai->data, ai->length, bn);\n if (ret == NULL) {\n ASN1err(ASN1_F_ASN1_STRING_TO_BN, ASN1_R_BN_LIB);\n return NULL;\n }\n if (ai->type & V_ASN1_NEG)\n BN_set_negative(ret, 1);\n return ret;\n}', 'int BN_add_word(BIGNUM *a, BN_ULONG w)\n{\n BN_ULONG l;\n int i;\n bn_check_top(a);\n w &= BN_MASK2;\n if (!w)\n return 1;\n if (BN_is_zero(a))\n return BN_set_word(a, w);\n if (a->neg) {\n a->neg = 0;\n i = BN_sub_word(a, w);\n if (!BN_is_zero(a))\n a->neg = !(a->neg);\n return (i);\n }\n for (i = 0; w != 0 && i < a->top; i++) {\n a->d[i] = l = (a->d[i] + w) & BN_MASK2;\n w = (w > l) ? 1 : 0;\n }\n if (w && i == a->top) {\n if (bn_wexpand(a, a->top + 1) == NULL)\n return 0;\n a->top++;\n a->d[i] = w;\n }\n bn_check_top(a);\n return 1;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return (NULL);\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}']
36,475
0
https://github.com/libav/libav/blob/26301caaa1aec5d71b564bff452147d6183370bf/libavcodec/mpegvideo_enc.c/#L1114
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; 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 = AV_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 = encode_frame(c, &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 = encode_frame(c, &input[i + 1]); rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3); } while (out_size) { out_size = encode_frame(c, 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; } } 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 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 = AV_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 = encode_frame(c, &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 = encode_frame(c, &input[i + 1]);\n rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);\n }\n while (out_size) {\n out_size = encode_frame(c, 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 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}', 'AVCodec *avcodec_find_encoder(enum AVCodecID id)\n{\n return find_encdec(id, 1);\n}', 'AVCodecContext *avcodec_alloc_context3(const AVCodec *codec)\n{\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) || !size)\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size + 32);\n if (!ptr)\n return ptr;\n diff = ((-(long)ptr - 1) & 31) + 1;\n ptr = (char *)ptr + diff;\n ((char *)ptr)[-1] = diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr, 32, size))\n ptr = NULL;\n#elif HAVE_ALIGNED_MALLOC\n ptr = _aligned_malloc(size, 32);\n#elif HAVE_MEMALIGN\n ptr = memalign(32, size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}']
36,476
0
https://github.com/libav/libav/blob/4d4d7cf9d539a053f531f662a972b23d335738eb/libavcodec/fmvc.c/#L177
static int decode_type2(GetByteContext *gb, PutByteContext *pb) { unsigned repeat = 0, first = 1, opcode; int i, len, pos; while (bytestream2_get_bytes_left(gb) > 0) { GetByteContext gbc; while (bytestream2_get_bytes_left(gb) > 0) { if (first) { first = 0; if (bytestream2_peek_byte(gb) > 17) { len = bytestream2_get_byte(gb) - 17; if (len < 4) { do { bytestream2_put_byte(pb, bytestream2_get_byte(gb)); --len; } while (len); opcode = bytestream2_peek_byte(gb); continue; } else { do { bytestream2_put_byte(pb, bytestream2_get_byte(gb)); --len; } while (len); opcode = bytestream2_peek_byte(gb); if (opcode < 0x10) { bytestream2_skip(gb, 1); pos = - (opcode >> 2) - 4 * bytestream2_get_byte(gb) - 2049; bytestream2_init(&gbc, pb->buffer_start, pb->buffer_end - pb->buffer_start); bytestream2_seek(&gbc, bytestream2_tell_p(pb) + pos, SEEK_SET); bytestream2_put_byte(pb, bytestream2_get_byte(&gbc)); bytestream2_put_byte(pb, bytestream2_get_byte(&gbc)); bytestream2_put_byte(pb, bytestream2_get_byte(&gbc)); len = opcode & 3; if (!len) { repeat = 1; } else { do { bytestream2_put_byte(pb, bytestream2_get_byte(gb)); --len; } while (len); opcode = bytestream2_peek_byte(gb); } continue; } } repeat = 0; } repeat = 1; } if (repeat) { repeat = 0; opcode = bytestream2_peek_byte(gb); if (opcode < 0x10) { bytestream2_skip(gb, 1); if (!opcode) { if (!bytestream2_peek_byte(gb)) { do { bytestream2_skip(gb, 1); opcode += 255; } while (!bytestream2_peek_byte(gb) && bytestream2_get_bytes_left(gb) > 0); } opcode += bytestream2_get_byte(gb) + 15; } bytestream2_put_le32(pb, bytestream2_get_le32(gb)); for (i = opcode - 1; i > 0; --i) bytestream2_put_byte(pb, bytestream2_get_byte(gb)); opcode = bytestream2_peek_byte(gb); if (opcode < 0x10) { bytestream2_skip(gb, 1); pos = - (opcode >> 2) - 4 * bytestream2_get_byte(gb) - 2049; bytestream2_init(&gbc, pb->buffer_start, pb->buffer_end - pb->buffer_start); bytestream2_seek(&gbc, bytestream2_tell_p(pb) + pos, SEEK_SET); bytestream2_put_byte(pb, bytestream2_get_byte(&gbc)); bytestream2_put_byte(pb, bytestream2_get_byte(&gbc)); bytestream2_put_byte(pb, bytestream2_get_byte(&gbc)); len = opcode & 3; if (!len) { repeat = 1; } else { do { bytestream2_put_byte(pb, bytestream2_get_byte(gb)); --len; } while (len); opcode = bytestream2_peek_byte(gb); } continue; } } } if (opcode >= 0x40) { bytestream2_skip(gb, 1); pos = - ((opcode >> 2) & 7) - 1 - 8 * bytestream2_get_byte(gb); len = (opcode >> 5) - 1; bytestream2_init(&gbc, pb->buffer_start, pb->buffer_end - pb->buffer_start); bytestream2_seek(&gbc, bytestream2_tell_p(pb) + pos, SEEK_SET); bytestream2_put_byte(pb, bytestream2_get_byte(&gbc)); bytestream2_put_byte(pb, bytestream2_get_byte(&gbc)); do { bytestream2_put_byte(pb, bytestream2_get_byte(&gbc)); --len; } while (len); len = opcode & 3; if (!len) { repeat = 1; } else { do { bytestream2_put_byte(pb, bytestream2_get_byte(gb)); --len; } while (len); opcode = bytestream2_peek_byte(gb); } continue; } else if (opcode < 0x20) { break; } len = opcode & 0x1F; bytestream2_skip(gb, 1); if (!len) { if (!bytestream2_peek_byte(gb)) { do { bytestream2_skip(gb, 1); len += 255; } while (!bytestream2_peek_byte(gb) && bytestream2_get_bytes_left(gb) > 0); } len += bytestream2_get_byte(gb) + 31; } i = bytestream2_get_le16(gb); pos = - (i >> 2) - 1; bytestream2_init(&gbc, pb->buffer_start, pb->buffer_end - pb->buffer_start); bytestream2_seek(&gbc, bytestream2_tell_p(pb) + pos, SEEK_SET); if (len < 6 || bytestream2_tell_p(pb) - bytestream2_tell(&gbc) < 4) { bytestream2_put_byte(pb, bytestream2_get_byte(&gbc)); bytestream2_put_byte(pb, bytestream2_get_byte(&gbc)); do { bytestream2_put_byte(pb, bytestream2_get_byte(&gbc)); --len; } while (len); } else { bytestream2_put_le32(pb, bytestream2_get_le32(&gbc)); for (len = len - 2; len; --len) bytestream2_put_byte(pb, bytestream2_get_byte(&gbc)); } len = i & 3; if (!len) { repeat = 1; } else { do { bytestream2_put_byte(pb, bytestream2_get_byte(gb)); --len; } while (len); opcode = bytestream2_peek_byte(gb); } } bytestream2_skip(gb, 1); if (opcode < 0x10) { pos = -(opcode >> 2) - 1 - 4 * bytestream2_get_byte(gb); bytestream2_init(&gbc, pb->buffer_start, pb->buffer_end - pb->buffer_start); bytestream2_seek(&gbc, bytestream2_tell_p(pb) + pos, SEEK_SET); bytestream2_put_byte(pb, bytestream2_get_byte(&gbc)); bytestream2_put_byte(pb, bytestream2_get_byte(&gbc)); len = opcode & 3; if (!len) { repeat = 1; } else { do { bytestream2_put_byte(pb, bytestream2_get_byte(gb)); --len; } while (len); opcode = bytestream2_peek_byte(gb); } continue; } len = opcode & 7; if (!len) { if (!bytestream2_peek_byte(gb)) { do { bytestream2_skip(gb, 1); len += 255; } while (!bytestream2_peek_byte(gb) && bytestream2_get_bytes_left(gb) > 0); } len += bytestream2_get_byte(gb) + 7; } i = bytestream2_get_le16(gb); pos = bytestream2_tell_p(pb) - 2048 * (opcode & 8); pos = pos - (i >> 2); if (pos == bytestream2_tell_p(pb)) break; pos = pos - 0x4000; bytestream2_init(&gbc, pb->buffer_start, pb->buffer_end - pb->buffer_start); bytestream2_seek(&gbc, pos, SEEK_SET); if (len < 6 || bytestream2_tell_p(pb) - bytestream2_tell(&gbc) < 4) { bytestream2_put_byte(pb, bytestream2_get_byte(&gbc)); bytestream2_put_byte(pb, bytestream2_get_byte(&gbc)); do { bytestream2_put_byte(pb, bytestream2_get_byte(&gbc)); --len; } while (len); } else { bytestream2_put_le32(pb, bytestream2_get_le32(&gbc)); for (len = len - 2; len; --len) bytestream2_put_byte(pb, bytestream2_get_byte(&gbc)); } len = i & 3; if (!len) { repeat = 1; } else { do { bytestream2_put_byte(pb, bytestream2_get_byte(gb)); --len; } while (len); opcode = bytestream2_peek_byte(gb); } } return 0; }
['static int decode_type2(GetByteContext *gb, PutByteContext *pb)\n{\n unsigned repeat = 0, first = 1, opcode;\n int i, len, pos;\n while (bytestream2_get_bytes_left(gb) > 0) {\n GetByteContext gbc;\n while (bytestream2_get_bytes_left(gb) > 0) {\n if (first) {\n first = 0;\n if (bytestream2_peek_byte(gb) > 17) {\n len = bytestream2_get_byte(gb) - 17;\n if (len < 4) {\n do {\n bytestream2_put_byte(pb, bytestream2_get_byte(gb));\n --len;\n } while (len);\n opcode = bytestream2_peek_byte(gb);\n continue;\n } else {\n do {\n bytestream2_put_byte(pb, bytestream2_get_byte(gb));\n --len;\n } while (len);\n opcode = bytestream2_peek_byte(gb);\n if (opcode < 0x10) {\n bytestream2_skip(gb, 1);\n pos = - (opcode >> 2) - 4 * bytestream2_get_byte(gb) - 2049;\n bytestream2_init(&gbc, pb->buffer_start, pb->buffer_end - pb->buffer_start);\n bytestream2_seek(&gbc, bytestream2_tell_p(pb) + pos, SEEK_SET);\n bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));\n bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));\n bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));\n len = opcode & 3;\n if (!len) {\n repeat = 1;\n } else {\n do {\n bytestream2_put_byte(pb, bytestream2_get_byte(gb));\n --len;\n } while (len);\n opcode = bytestream2_peek_byte(gb);\n }\n continue;\n }\n }\n repeat = 0;\n }\n repeat = 1;\n }\n if (repeat) {\n repeat = 0;\n opcode = bytestream2_peek_byte(gb);\n if (opcode < 0x10) {\n bytestream2_skip(gb, 1);\n if (!opcode) {\n if (!bytestream2_peek_byte(gb)) {\n do {\n bytestream2_skip(gb, 1);\n opcode += 255;\n } while (!bytestream2_peek_byte(gb) && bytestream2_get_bytes_left(gb) > 0);\n }\n opcode += bytestream2_get_byte(gb) + 15;\n }\n bytestream2_put_le32(pb, bytestream2_get_le32(gb));\n for (i = opcode - 1; i > 0; --i)\n bytestream2_put_byte(pb, bytestream2_get_byte(gb));\n opcode = bytestream2_peek_byte(gb);\n if (opcode < 0x10) {\n bytestream2_skip(gb, 1);\n pos = - (opcode >> 2) - 4 * bytestream2_get_byte(gb) - 2049;\n bytestream2_init(&gbc, pb->buffer_start, pb->buffer_end - pb->buffer_start);\n bytestream2_seek(&gbc, bytestream2_tell_p(pb) + pos, SEEK_SET);\n bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));\n bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));\n bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));\n len = opcode & 3;\n if (!len) {\n repeat = 1;\n } else {\n do {\n bytestream2_put_byte(pb, bytestream2_get_byte(gb));\n --len;\n } while (len);\n opcode = bytestream2_peek_byte(gb);\n }\n continue;\n }\n }\n }\n if (opcode >= 0x40) {\n bytestream2_skip(gb, 1);\n pos = - ((opcode >> 2) & 7) - 1 - 8 * bytestream2_get_byte(gb);\n len = (opcode >> 5) - 1;\n bytestream2_init(&gbc, pb->buffer_start, pb->buffer_end - pb->buffer_start);\n bytestream2_seek(&gbc, bytestream2_tell_p(pb) + pos, SEEK_SET);\n bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));\n bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));\n do {\n bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));\n --len;\n } while (len);\n len = opcode & 3;\n if (!len) {\n repeat = 1;\n } else {\n do {\n bytestream2_put_byte(pb, bytestream2_get_byte(gb));\n --len;\n } while (len);\n opcode = bytestream2_peek_byte(gb);\n }\n continue;\n } else if (opcode < 0x20) {\n break;\n }\n len = opcode & 0x1F;\n bytestream2_skip(gb, 1);\n if (!len) {\n if (!bytestream2_peek_byte(gb)) {\n do {\n bytestream2_skip(gb, 1);\n len += 255;\n } while (!bytestream2_peek_byte(gb) && bytestream2_get_bytes_left(gb) > 0);\n }\n len += bytestream2_get_byte(gb) + 31;\n }\n i = bytestream2_get_le16(gb);\n pos = - (i >> 2) - 1;\n bytestream2_init(&gbc, pb->buffer_start, pb->buffer_end - pb->buffer_start);\n bytestream2_seek(&gbc, bytestream2_tell_p(pb) + pos, SEEK_SET);\n if (len < 6 || bytestream2_tell_p(pb) - bytestream2_tell(&gbc) < 4) {\n bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));\n bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));\n do {\n bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));\n --len;\n } while (len);\n } else {\n bytestream2_put_le32(pb, bytestream2_get_le32(&gbc));\n for (len = len - 2; len; --len)\n bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));\n }\n len = i & 3;\n if (!len) {\n repeat = 1;\n } else {\n do {\n bytestream2_put_byte(pb, bytestream2_get_byte(gb));\n --len;\n } while (len);\n opcode = bytestream2_peek_byte(gb);\n }\n }\n bytestream2_skip(gb, 1);\n if (opcode < 0x10) {\n pos = -(opcode >> 2) - 1 - 4 * bytestream2_get_byte(gb);\n bytestream2_init(&gbc, pb->buffer_start, pb->buffer_end - pb->buffer_start);\n bytestream2_seek(&gbc, bytestream2_tell_p(pb) + pos, SEEK_SET);\n bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));\n bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));\n len = opcode & 3;\n if (!len) {\n repeat = 1;\n } else {\n do {\n bytestream2_put_byte(pb, bytestream2_get_byte(gb));\n --len;\n } while (len);\n opcode = bytestream2_peek_byte(gb);\n }\n continue;\n }\n len = opcode & 7;\n if (!len) {\n if (!bytestream2_peek_byte(gb)) {\n do {\n bytestream2_skip(gb, 1);\n len += 255;\n } while (!bytestream2_peek_byte(gb) && bytestream2_get_bytes_left(gb) > 0);\n }\n len += bytestream2_get_byte(gb) + 7;\n }\n i = bytestream2_get_le16(gb);\n pos = bytestream2_tell_p(pb) - 2048 * (opcode & 8);\n pos = pos - (i >> 2);\n if (pos == bytestream2_tell_p(pb))\n break;\n pos = pos - 0x4000;\n bytestream2_init(&gbc, pb->buffer_start, pb->buffer_end - pb->buffer_start);\n bytestream2_seek(&gbc, pos, SEEK_SET);\n if (len < 6 || bytestream2_tell_p(pb) - bytestream2_tell(&gbc) < 4) {\n bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));\n bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));\n do {\n bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));\n --len;\n } while (len);\n } else {\n bytestream2_put_le32(pb, bytestream2_get_le32(&gbc));\n for (len = len - 2; len; --len)\n bytestream2_put_byte(pb, bytestream2_get_byte(&gbc));\n }\n len = i & 3;\n if (!len) {\n repeat = 1;\n } else {\n do {\n bytestream2_put_byte(pb, bytestream2_get_byte(gb));\n --len;\n } while (len);\n opcode = bytestream2_peek_byte(gb);\n }\n }\n return 0;\n}']
36,477
0
https://github.com/openssl/openssl/blob/8b0d4242404f9e5da26e7594fa0864b2df4601af/crypto/lhash/lhash.c/#L123
void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data) { unsigned long hash; OPENSSL_LH_NODE *nn, **rn; void *ret; lh->error = 0; rn = getrn(lh, data, &hash); if (*rn == NULL) { lh->num_no_delete++; return (NULL); } else { nn = *rn; *rn = nn->next; ret = nn->data; OPENSSL_free(nn); lh->num_delete++; } lh->num_items--; if ((lh->num_nodes > MIN_NODES) && (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes))) contract(lh); return (ret); }
['static int init_ssl_connection(SSL *con)\n{\n int i;\n long verify_err;\n int retry = 0;\n#ifndef OPENSSL_NO_DTLS\n if (dtlslisten) {\n BIO_ADDR *client = NULL;\n if ((client = BIO_ADDR_new()) == NULL) {\n BIO_printf(bio_err, "ERROR - memory\\n");\n return 0;\n }\n i = DTLSv1_listen(con, client);\n if (i > 0) {\n BIO *wbio;\n int fd = -1;\n wbio = SSL_get_wbio(con);\n if (wbio) {\n BIO_get_fd(wbio, &fd);\n }\n if (!wbio || BIO_connect(fd, client, 0) == 0) {\n BIO_printf(bio_err, "ERROR - unable to connect\\n");\n BIO_ADDR_free(client);\n return 0;\n }\n BIO_ADDR_free(client);\n dtlslisten = 0;\n i = SSL_accept(con);\n } else {\n BIO_ADDR_free(client);\n }\n } else\n#endif\n do {\n i = SSL_accept(con);\n if (i <= 0)\n retry = BIO_sock_should_retry(i);\n#ifdef CERT_CB_TEST_RETRY\n {\n while (i <= 0\n && SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP\n && SSL_get_state(con) == TLS_ST_SR_CLNT_HELLO) {\n BIO_printf(bio_err,\n "LOOKUP from certificate callback during accept\\n");\n i = SSL_accept(con);\n if (i <= 0)\n retry = BIO_sock_should_retry(i);\n }\n }\n#endif\n#ifndef OPENSSL_NO_SRP\n while (i <= 0\n && SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP) {\n BIO_printf(bio_s_out, "LOOKUP during accept %s\\n",\n srp_callback_parm.login);\n SRP_user_pwd_free(srp_callback_parm.user);\n srp_callback_parm.user =\n SRP_VBASE_get1_by_user(srp_callback_parm.vb,\n srp_callback_parm.login);\n if (srp_callback_parm.user)\n BIO_printf(bio_s_out, "LOOKUP done %s\\n",\n srp_callback_parm.user->info);\n else\n BIO_printf(bio_s_out, "LOOKUP not successful\\n");\n i = SSL_accept(con);\n if (i <= 0)\n retry = BIO_sock_should_retry(i);\n }\n#endif\n } while (i < 0 && SSL_waiting_for_async(con));\n if (i <= 0) {\n if ((dtlslisten && i == 0)\n || (!dtlslisten && retry)) {\n BIO_printf(bio_s_out, "DELAY\\n");\n return (1);\n }\n BIO_printf(bio_err, "ERROR\\n");\n verify_err = SSL_get_verify_result(con);\n if (verify_err != X509_V_OK) {\n BIO_printf(bio_err, "verify error:%s\\n",\n X509_verify_cert_error_string(verify_err));\n }\n ERR_print_errors(bio_err);\n return (0);\n }\n print_connection_info(con);\n return 1;\n}', 'int DTLSv1_listen(SSL *s, BIO_ADDR *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 const unsigned char *data;\n unsigned char *buf;\n size_t fragoff, fraglen, msglen;\n unsigned int rectype, versmajor, msgseq, msgtype, clientvers, cookielen;\n BIO *rbio, *wbio;\n BUF_MEM *bufm;\n BIO_ADDR *tmpclient = NULL;\n PACKET pkt, msgpkt, msgpayload, session, cookiepkt;\n if (s->handshake_func == NULL) {\n SSL_set_accept_state(s);\n }\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_DTLSV1_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_DTLSV1_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_DTLSV1_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_DTLSV1_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_DTLSV1_LISTEN, ERR_R_INTERNAL_ERROR);\n return -1;\n }\n if (n < DTLS1_RT_HEADER_LENGTH) {\n SSLerr(SSL_F_DTLSV1_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_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH);\n goto end;\n }\n if (rectype != SSL3_RT_HANDSHAKE) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_UNEXPECTED_MESSAGE);\n goto end;\n }\n if (versmajor != DTLS1_VERSION_MAJOR) {\n SSLerr(SSL_F_DTLSV1_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 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH);\n goto end;\n }\n if (seq[0] != 0 || seq[1] != 0) {\n SSLerr(SSL_F_DTLSV1_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_len(&msgpkt, &msglen)\n || !PACKET_get_net_2(&msgpkt, &msgseq)\n || !PACKET_get_net_3_len(&msgpkt, &fragoff)\n || !PACKET_get_net_3_len(&msgpkt, &fraglen)\n || !PACKET_get_sub_packet(&msgpkt, &msgpayload, fraglen)\n || PACKET_remaining(&msgpkt) != 0) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH);\n goto end;\n }\n if (msgtype != SSL3_MT_CLIENT_HELLO) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_UNEXPECTED_MESSAGE);\n goto end;\n }\n if (msgseq > 2) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_INVALID_SEQUENCE_NUMBER);\n goto end;\n }\n if (fragoff != 0 || fraglen > msglen) {\n SSLerr(SSL_F_DTLSV1_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 fraglen + DTLS1_HM_HEADER_LENGTH, s,\n s->msg_callback_arg);\n if (!PACKET_get_net_2(&msgpayload, &clientvers)) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH);\n goto end;\n }\n if (DTLS_VERSION_LT(clientvers, (unsigned int)s->method->version) &&\n s->method->version != DTLS_ANY_VERSION) {\n SSLerr(SSL_F_DTLSV1_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_DTLSV1_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_DTLSV1_LISTEN, SSL_R_NO_VERIFY_COOKIE_CALLBACK);\n return -1;\n }\n if (s->ctx->app_verify_cookie_cb(s, PACKET_data(&cookiepkt),\n (unsigned int)PACKET_remaining(&cookiepkt)) == 0) {\n next = LISTEN_SEND_VERIFY_REQUEST;\n } else {\n next = LISTEN_SUCCESS;\n }\n }\n if (next == LISTEN_SEND_VERIFY_REQUEST) {\n WPACKET wpkt;\n unsigned int version;\n size_t wreclen;\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_DTLSV1_LISTEN, SSL_R_COOKIE_GEN_CALLBACK_FAILURE);\n return -1;\n }\n version = (s->method->version == DTLS_ANY_VERSION) ? DTLS1_VERSION\n : s->version;\n if (!WPACKET_init(&wpkt, s->init_buf)\n || !WPACKET_put_bytes_u8(&wpkt, SSL3_RT_HANDSHAKE)\n || !WPACKET_put_bytes_u16(&wpkt, version)\n || !WPACKET_memcpy(&wpkt, seq, SEQ_NUM_SIZE)\n || !WPACKET_start_sub_packet_u16(&wpkt)\n || !WPACKET_put_bytes_u8(&wpkt,\n DTLS1_MT_HELLO_VERIFY_REQUEST)\n || !WPACKET_put_bytes_u24(&wpkt, 0)\n || !WPACKET_put_bytes_u16(&wpkt, 0)\n || !WPACKET_put_bytes_u24(&wpkt, 0)\n || !WPACKET_start_sub_packet_u24(&wpkt)\n || !dtls_raw_hello_verify_request(&wpkt, cookie, cookielen)\n || !WPACKET_close(&wpkt)\n || !WPACKET_close(&wpkt)\n || !WPACKET_get_total_written(&wpkt, &wreclen)\n || !WPACKET_finish(&wpkt)) {\n SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_INTERNAL_ERROR);\n WPACKET_cleanup(&wpkt);\n return -1;\n }\n memcpy(&buf[DTLS1_RT_HEADER_LENGTH + 1],\n &buf[DTLS1_RT_HEADER_LENGTH + DTLS1_HM_HEADER_LENGTH - 3],\n 3);\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 ((tmpclient = BIO_ADDR_new()) == NULL) {\n SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_MALLOC_FAILURE);\n goto end;\n }\n if (BIO_dgram_get_peer(rbio, tmpclient) > 0) {\n (void)BIO_dgram_set_peer(wbio, tmpclient);\n }\n BIO_ADDR_free(tmpclient);\n tmpclient = NULL;\n if (BIO_write(wbio, buf, wreclen) < (int)wreclen) {\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 ossl_statem_set_hello_verify_done(s);\n if (BIO_dgram_get_peer(rbio, client) <= 0)\n BIO_ADDR_clear(client);\n ret = 1;\n clearpkt = 0;\n end:\n BIO_ADDR_free(tmpclient);\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}', 'int SSL_clear(SSL *s)\n{\n if (s->method == NULL) {\n SSLerr(SSL_F_SSL_CLEAR, SSL_R_NO_METHOD_SPECIFIED);\n return (0);\n }\n if (ssl_clear_bad_session(s)) {\n SSL_SESSION_free(s->session);\n s->session = NULL;\n }\n s->error = 0;\n s->hit = 0;\n s->shutdown = 0;\n if (s->renegotiate) {\n SSLerr(SSL_F_SSL_CLEAR, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n ossl_statem_clear(s);\n s->version = s->method->version;\n s->client_version = s->version;\n s->rwstate = SSL_NOTHING;\n BUF_MEM_free(s->init_buf);\n s->init_buf = NULL;\n clear_ciphers(s);\n s->first_packet = 0;\n s->key_update = SSL_KEY_UPDATE_NONE;\n s->dane.mdpth = -1;\n s->dane.pdpth = -1;\n X509_free(s->dane.mcert);\n s->dane.mcert = NULL;\n s->dane.mtlsa = NULL;\n X509_VERIFY_PARAM_move_peername(s->param, NULL);\n if (!ossl_statem_get_in_handshake(s) && (s->session == NULL)\n && (s->method != s->ctx->method)) {\n s->method->ssl_free(s);\n s->method = s->ctx->method;\n if (!s->method->ssl_new(s))\n return (0);\n } else\n s->method->ssl_clear(s);\n RECORD_LAYER_clear(&s->rlayer);\n return (1);\n}', 'int SSL_accept(SSL *s)\n{\n if (s->handshake_func == NULL) {\n SSL_set_accept_state(s);\n }\n return SSL_do_handshake(s);\n}', 'int SSL_do_handshake(SSL *s)\n{\n int ret = 1;\n if (s->handshake_func == NULL) {\n SSLerr(SSL_F_SSL_DO_HANDSHAKE, SSL_R_CONNECTION_TYPE_NOT_SET);\n return -1;\n }\n if (s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY) {\n int edfin;\n edfin = ssl_write_early_finish(s);\n if (edfin <= 0)\n return edfin;\n }\n ossl_statem_check_finish_init(s, -1);\n s->method->ssl_renegotiate_check(s, 0);\n if (SSL_in_init(s) || SSL_in_before(s)) {\n if ((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {\n struct ssl_async_args args;\n args.s = s;\n ret = ssl_start_async_job(s, &args, ssl_do_handshake_intern);\n } else {\n ret = s->handshake_func(s);\n }\n }\n return ret;\n}', 'static int ssl_write_early_finish(SSL *s)\n{\n int ret;\n if (s->early_data_state != SSL_EARLY_DATA_WRITE_RETRY) {\n SSLerr(SSL_F_SSL_WRITE_EARLY_FINISH, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n s->early_data_state = SSL_EARLY_DATA_WRITING;\n ret = ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_END_OF_EARLY_DATA);\n if (ret <= 0) {\n s->early_data_state = SSL_EARLY_DATA_WRITE_RETRY;\n return 0;\n }\n s->early_data_state = SSL_EARLY_DATA_FINISHED_WRITING;\n EVP_CIPHER_CTX_free(s->enc_write_ctx);\n s->enc_write_ctx = NULL;\n ossl_statem_set_in_init(s, 1);\n return 1;\n}', 'int ssl3_send_alert(SSL *s, int level, int desc)\n{\n if (SSL_TREAT_AS_TLS13(s))\n desc = tls13_alert_code(desc);\n else\n desc = s->method->ssl3_enc->alert_value(desc);\n if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION)\n desc = SSL_AD_HANDSHAKE_FAILURE;\n if (desc < 0)\n return -1;\n if ((level == SSL3_AL_FATAL) && (s->session != NULL))\n SSL_CTX_remove_session(s->session_ctx, s->session);\n s->s3->alert_dispatch = 1;\n s->s3->send_alert[0] = level;\n s->s3->send_alert[1] = desc;\n if (!RECORD_LAYER_write_pending(&s->rlayer)) {\n return s->method->ssl_dispatch_alert(s);\n }\n return -1;\n}', 'int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)\n{\n return remove_session_lock(ctx, c, 1);\n}', 'static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)\n{\n SSL_SESSION *r;\n int ret = 0;\n if ((c != NULL) && (c->session_id_length != 0)) {\n if (lck)\n CRYPTO_THREAD_write_lock(ctx->lock);\n if ((r = lh_SSL_SESSION_retrieve(ctx->sessions, c)) == c) {\n ret = 1;\n r = lh_SSL_SESSION_delete(ctx->sessions, c);\n SSL_SESSION_list_remove(ctx, c);\n }\n c->not_resumable = 1;\n if (lck)\n CRYPTO_THREAD_unlock(ctx->lock);\n if (ret)\n SSL_SESSION_free(r);\n if (ctx->remove_session_cb != NULL)\n ctx->remove_session_cb(ctx, c);\n } else\n ret = 0;\n return (ret);\n}', 'DEFINE_LHASH_OF(SSL_SESSION)', 'void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)\n{\n unsigned long hash;\n OPENSSL_LH_NODE *nn, **rn;\n void *ret;\n lh->error = 0;\n rn = getrn(lh, data, &hash);\n if (*rn == NULL) {\n lh->num_no_delete++;\n return (NULL);\n } else {\n nn = *rn;\n *rn = nn->next;\n ret = nn->data;\n OPENSSL_free(nn);\n lh->num_delete++;\n }\n lh->num_items--;\n if ((lh->num_nodes > MIN_NODES) &&\n (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))\n contract(lh);\n return (ret);\n}']
36,478
0
https://github.com/openssl/openssl/blob/28443085c771309898b3f4f36f3e79802c59723d/ssl/statem/statem_clnt.c/#L2771
static int tls_construct_cke_rsa(SSL *s, WPACKET *pkt, int *al) { #ifndef OPENSSL_NO_RSA unsigned char *encdata = NULL; EVP_PKEY *pkey = NULL; EVP_PKEY_CTX *pctx = NULL; size_t enclen; unsigned char *pms = NULL; size_t pmslen = 0; if (s->session->peer == NULL) { SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR); return 0; } pkey = X509_get0_pubkey(s->session->peer); if (EVP_PKEY_get0_RSA(pkey) == NULL) { SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR); return 0; } pmslen = SSL_MAX_MASTER_KEY_LENGTH; pms = OPENSSL_malloc(pmslen); if (pms == NULL) { SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_MALLOC_FAILURE); *al = SSL_AD_INTERNAL_ERROR; return 0; } pms[0] = s->client_version >> 8; pms[1] = s->client_version & 0xff; if (RAND_bytes(pms + 2, (int)(pmslen - 2)) <= 0) { goto err; } if (s->version > SSL3_VERSION && !WPACKET_start_sub_packet_u16(pkt)) { SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR); goto err; } pctx = EVP_PKEY_CTX_new(pkey, NULL); if (pctx == NULL || EVP_PKEY_encrypt_init(pctx) <= 0 || EVP_PKEY_encrypt(pctx, NULL, &enclen, pms, pmslen) <= 0) { SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_EVP_LIB); goto err; } if (!WPACKET_allocate_bytes(pkt, enclen, &encdata) || EVP_PKEY_encrypt(pctx, encdata, &enclen, pms, pmslen) <= 0) { SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, SSL_R_BAD_RSA_ENCRYPT); goto err; } EVP_PKEY_CTX_free(pctx); pctx = NULL; if (s->version > SSL3_VERSION && !WPACKET_close(pkt)) { SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR); goto err; } s->s3->tmp.pms = pms; s->s3->tmp.pmslen = pmslen; if (!ssl_log_rsa_client_key_exchange(s, encdata, enclen, pms, pmslen)) goto err; return 1; err: OPENSSL_clear_free(pms, pmslen); EVP_PKEY_CTX_free(pctx); return 0; #else SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR); *al = SSL_AD_INTERNAL_ERROR; return 0; #endif }
['static int tls_construct_cke_rsa(SSL *s, WPACKET *pkt, int *al)\n{\n#ifndef OPENSSL_NO_RSA\n unsigned char *encdata = NULL;\n EVP_PKEY *pkey = NULL;\n EVP_PKEY_CTX *pctx = NULL;\n size_t enclen;\n unsigned char *pms = NULL;\n size_t pmslen = 0;\n if (s->session->peer == NULL) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n pkey = X509_get0_pubkey(s->session->peer);\n if (EVP_PKEY_get0_RSA(pkey) == NULL) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n pmslen = SSL_MAX_MASTER_KEY_LENGTH;\n pms = OPENSSL_malloc(pmslen);\n if (pms == NULL) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_MALLOC_FAILURE);\n *al = SSL_AD_INTERNAL_ERROR;\n return 0;\n }\n pms[0] = s->client_version >> 8;\n pms[1] = s->client_version & 0xff;\n if (RAND_bytes(pms + 2, (int)(pmslen - 2)) <= 0) {\n goto err;\n }\n if (s->version > SSL3_VERSION && !WPACKET_start_sub_packet_u16(pkt)) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n pctx = EVP_PKEY_CTX_new(pkey, NULL);\n if (pctx == NULL || EVP_PKEY_encrypt_init(pctx) <= 0\n || EVP_PKEY_encrypt(pctx, NULL, &enclen, pms, pmslen) <= 0) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_EVP_LIB);\n goto err;\n }\n if (!WPACKET_allocate_bytes(pkt, enclen, &encdata)\n || EVP_PKEY_encrypt(pctx, encdata, &enclen, pms, pmslen) <= 0) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, SSL_R_BAD_RSA_ENCRYPT);\n goto err;\n }\n EVP_PKEY_CTX_free(pctx);\n pctx = NULL;\n if (s->version > SSL3_VERSION && !WPACKET_close(pkt)) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n s->s3->tmp.pms = pms;\n s->s3->tmp.pmslen = pmslen;\n if (!ssl_log_rsa_client_key_exchange(s, encdata, enclen, pms, pmslen))\n goto err;\n return 1;\n err:\n OPENSSL_clear_free(pms, pmslen);\n EVP_PKEY_CTX_free(pctx);\n return 0;\n#else\n SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);\n *al = SSL_AD_INTERNAL_ERROR;\n return 0;\n#endif\n}', 'EVP_PKEY *X509_get0_pubkey(const X509 *x)\n{\n if (x == NULL)\n return NULL;\n return X509_PUBKEY_get0(x->cert_info.key);\n}', 'EVP_PKEY *X509_PUBKEY_get0(X509_PUBKEY *key)\n{\n EVP_PKEY *ret = NULL;\n if (key == NULL || key->public_key == NULL)\n return NULL;\n if (key->pkey != NULL)\n return key->pkey;\n x509_pubkey_decode(&ret, key);\n if (ret != NULL) {\n X509err(X509_F_X509_PUBKEY_GET0, ERR_R_INTERNAL_ERROR);\n EVP_PKEY_free(ret);\n }\n return NULL;\n}', 'RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey)\n{\n if (pkey->type != EVP_PKEY_RSA) {\n EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY);\n return NULL;\n }\n return pkey->pkey.rsa;\n}', '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 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}', '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 == NULL) {\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}', 'void CRYPTO_clear_free(void *str, size_t num, const char *file, int line)\n{\n if (str == NULL)\n return;\n if (num)\n OPENSSL_cleanse(str, num);\n CRYPTO_free(str, file, line);\n}', 'void CRYPTO_free(void *str, const char *file, int line)\n{\n if (free_impl != NULL && free_impl != &CRYPTO_free) {\n free_impl(str, file, line);\n return;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_free(str, 0, file, line);\n free(str);\n CRYPTO_mem_debug_free(str, 1, file, line);\n } else {\n free(str);\n }\n#else\n free(str);\n#endif\n}']
36,479
0
https://github.com/openssl/openssl/blob/313fce7b61ecaf5879cf84b256bdd0964134836e/apps/ca.c/#L2902
int unpack_revinfo(ASN1_TIME **prevtm, int *preason, ASN1_OBJECT **phold, ASN1_GENERALIZEDTIME **pinvtm, const char *str) { char *tmp = NULL; char *rtime_str, *reason_str = NULL, *arg_str = NULL, *p; int reason_code = -1; int ret = 0; unsigned int i; ASN1_OBJECT *hold = NULL; ASN1_GENERALIZEDTIME *comp_time = NULL; tmp = BUF_strdup(str); p = strchr(tmp, ','); rtime_str = tmp; if (p) { *p = '\0'; p++; reason_str = p; p = strchr(p, ','); if (p) { *p = '\0'; arg_str = p + 1; } } if (prevtm) { *prevtm = ASN1_UTCTIME_new(); if (!ASN1_UTCTIME_set_string(*prevtm, rtime_str)) { BIO_printf(bio_err, "invalid revocation date %s\n", rtime_str); goto err; } } if (reason_str) { for (i = 0; i < NUM_REASONS; i++) { if(!strcasecmp(reason_str, crl_reasons[i])) { reason_code = i; break; } } if (reason_code == OCSP_REVOKED_STATUS_NOSTATUS) { BIO_printf(bio_err, "invalid reason code %s\n", reason_str); goto err; } if (reason_code == 7) reason_code = OCSP_REVOKED_STATUS_REMOVEFROMCRL; else if (reason_code == 8) { if (!arg_str) { BIO_printf(bio_err, "missing hold instruction\n"); goto err; } reason_code = OCSP_REVOKED_STATUS_CERTIFICATEHOLD; hold = OBJ_txt2obj(arg_str, 0); if (!hold) { BIO_printf(bio_err, "invalid object identifier %s\n", arg_str); goto err; } if (phold) *phold = hold; } else if ((reason_code == 9) || (reason_code == 10)) { if (!arg_str) { BIO_printf(bio_err, "missing compromised time\n"); goto err; } comp_time = ASN1_GENERALIZEDTIME_new(); if (!ASN1_GENERALIZEDTIME_set_string(comp_time, arg_str)) { BIO_printf(bio_err, "invalid compromised time %s\n", arg_str); goto err; } if (reason_code == 9) reason_code = OCSP_REVOKED_STATUS_KEYCOMPROMISE; else reason_code = OCSP_REVOKED_STATUS_CACOMPROMISE; } } if (preason) *preason = reason_code; if (pinvtm) *pinvtm = comp_time; else ASN1_GENERALIZEDTIME_free(comp_time); ret = 1; err: if (tmp) OPENSSL_free(tmp); if (!phold) ASN1_OBJECT_free(hold); if (!pinvtm) ASN1_GENERALIZEDTIME_free(comp_time); return ret; }
['int unpack_revinfo(ASN1_TIME **prevtm, int *preason, ASN1_OBJECT **phold, ASN1_GENERALIZEDTIME **pinvtm, const char *str)\n\t{\n\tchar *tmp = NULL;\n\tchar *rtime_str, *reason_str = NULL, *arg_str = NULL, *p;\n\tint reason_code = -1;\n\tint ret = 0;\n\tunsigned int i;\n\tASN1_OBJECT *hold = NULL;\n\tASN1_GENERALIZEDTIME *comp_time = NULL;\n\ttmp = BUF_strdup(str);\n\tp = strchr(tmp, \',\');\n\trtime_str = tmp;\n\tif (p)\n\t\t{\n\t\t*p = \'\\0\';\n\t\tp++;\n\t\treason_str = p;\n\t\tp = strchr(p, \',\');\n\t\tif (p)\n\t\t\t{\n\t\t\t*p = \'\\0\';\n\t\t\targ_str = p + 1;\n\t\t\t}\n\t\t}\n\tif (prevtm)\n\t\t{\n\t\t*prevtm = ASN1_UTCTIME_new();\n\t\tif (!ASN1_UTCTIME_set_string(*prevtm, rtime_str))\n\t\t\t{\n\t\t\tBIO_printf(bio_err, "invalid revocation date %s\\n", rtime_str);\n\t\t\tgoto err;\n\t\t\t}\n\t\t}\n\tif (reason_str)\n\t\t{\n\t\tfor (i = 0; i < NUM_REASONS; i++)\n\t\t\t{\n\t\t\tif(!strcasecmp(reason_str, crl_reasons[i]))\n\t\t\t\t{\n\t\t\t\treason_code = i;\n\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\tif (reason_code == OCSP_REVOKED_STATUS_NOSTATUS)\n\t\t\t{\n\t\t\tBIO_printf(bio_err, "invalid reason code %s\\n", reason_str);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (reason_code == 7)\n\t\t\treason_code = OCSP_REVOKED_STATUS_REMOVEFROMCRL;\n\t\telse if (reason_code == 8)\n\t\t\t{\n\t\t\tif (!arg_str)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err, "missing hold instruction\\n");\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\treason_code = OCSP_REVOKED_STATUS_CERTIFICATEHOLD;\n\t\t\thold = OBJ_txt2obj(arg_str, 0);\n\t\t\tif (!hold)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err, "invalid object identifier %s\\n", arg_str);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif (phold) *phold = hold;\n\t\t\t}\n\t\telse if ((reason_code == 9) || (reason_code == 10))\n\t\t\t{\n\t\t\tif (!arg_str)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err, "missing compromised time\\n");\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tcomp_time = ASN1_GENERALIZEDTIME_new();\n\t\t\tif (!ASN1_GENERALIZEDTIME_set_string(comp_time, arg_str))\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err, "invalid compromised time %s\\n", arg_str);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif (reason_code == 9)\n\t\t\t\treason_code = OCSP_REVOKED_STATUS_KEYCOMPROMISE;\n\t\t\telse\n\t\t\t\treason_code = OCSP_REVOKED_STATUS_CACOMPROMISE;\n\t\t\t}\n\t\t}\n\tif (preason) *preason = reason_code;\n\tif (pinvtm) *pinvtm = comp_time;\n\telse ASN1_GENERALIZEDTIME_free(comp_time);\n\tret = 1;\n\terr:\n\tif (tmp) OPENSSL_free(tmp);\n\tif (!phold) ASN1_OBJECT_free(hold);\n\tif (!pinvtm) ASN1_GENERALIZEDTIME_free(comp_time);\n\treturn ret;\n\t}', 'char *BUF_strdup(const char *str)\n\t{\n\tif (str == NULL) return(NULL);\n\treturn BUF_strndup(str, strlen(str));\n\t}', 'char *BUF_strndup(const char *str, size_t siz)\n\t{\n\tchar *ret;\n\tif (str == NULL) return(NULL);\n\tret=OPENSSL_malloc(siz+1);\n\tif (ret == NULL)\n\t\t{\n\t\tBUFerr(BUF_F_BUF_STRNDUP,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tBUF_strlcpy(ret,str,siz+1);\n\treturn(ret);\n\t}', 'void *CRYPTO_malloc(int num, const char *file, int line)\n\t{\n\tvoid *ret = NULL;\n\textern unsigned char cleanse_ctr;\n\tif (num <= 0) return NULL;\n\tallow_customize = 0;\n\tif (malloc_debug_func != NULL)\n\t\t{\n\t\tallow_customize_debug = 0;\n\t\tmalloc_debug_func(NULL, num, file, line, 0);\n\t\t}\n\tret = malloc_ex_func(num,file,line);\n#ifdef LEVITTE_DEBUG_MEM\n\tfprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\\n", ret, num);\n#endif\n\tif (malloc_debug_func != NULL)\n\t\tmalloc_debug_func(ret, num, file, line, 1);\n if(ret && (num > 2048))\n ((unsigned char *)ret)[0] = cleanse_ctr;\n\treturn ret;\n\t}', 'void ERR_put_error(int lib, int func, int reason, const char *file,\n\t int line)\n\t{\n\tERR_STATE *es;\n#ifdef _OSD_POSIX\n\tif (strncmp(file,"*POSIX(", sizeof("*POSIX(")-1) == 0) {\n\t\tchar *end;\n\t\tfile += sizeof("*POSIX(")-1;\n\t\tend = &file[strlen(file)-1];\n\t\tif (*end == \')\')\n\t\t\t*end = \'\\0\';\n\t\tif ((end = strrchr(file, \'/\')) != NULL)\n\t\t\tfile = &end[1];\n\t}\n#endif\n\tes=ERR_get_state();\n\tes->top=(es->top+1)%ERR_NUM_ERRORS;\n\tif (es->top == es->bottom)\n\t\tes->bottom=(es->bottom+1)%ERR_NUM_ERRORS;\n\tes->err_flags[es->top]=0;\n\tes->err_buffer[es->top]=ERR_PACK(lib,func,reason);\n\tes->err_file[es->top]=file;\n\tes->err_line[es->top]=line;\n\terr_clear_data(es,es->top);\n\t}', 'void CRYPTO_free(void *str)\n\t{\n\tif (free_debug_func != NULL)\n\t\tfree_debug_func(str, 0);\n#ifdef LEVITTE_DEBUG_MEM\n\tfprintf(stderr, "LEVITTE_DEBUG_MEM: < 0x%p\\n", str);\n#endif\n\tfree_func(str);\n\tif (free_debug_func != NULL)\n\t\tfree_debug_func(NULL, 1);\n\t}']
36,480
0
https://github.com/openssl/openssl/blob/ec04e866343d40a1e3e8e5db79557e279a2dd0d8/ssl/d1_lib.c/#L165
int dtls1_new(SSL *s) { DTLS1_STATE *d1; if (!DTLS_RECORD_LAYER_new(&s->rlayer)) { return 0; } if (!ssl3_new(s)) return (0); if ((d1 = OPENSSL_zalloc(sizeof(*d1))) == NULL) { ssl3_free(s); return (0); } d1->buffered_messages = pqueue_new(); d1->sent_messages = pqueue_new(); if (s->server) { d1->cookie_len = sizeof(s->d1->cookie); } d1->link_mtu = 0; d1->mtu = 0; if (d1->buffered_messages == NULL || d1->sent_messages == NULL) { pqueue_free(d1->buffered_messages); pqueue_free(d1->sent_messages); OPENSSL_free(d1); ssl3_free(s); return (0); } s->d1 = d1; s->method->ssl_clear(s); return (1); }
['int dtls1_new(SSL *s)\n{\n DTLS1_STATE *d1;\n if (!DTLS_RECORD_LAYER_new(&s->rlayer)) {\n return 0;\n }\n if (!ssl3_new(s))\n return (0);\n if ((d1 = OPENSSL_zalloc(sizeof(*d1))) == NULL) {\n ssl3_free(s);\n return (0);\n }\n d1->buffered_messages = pqueue_new();\n d1->sent_messages = pqueue_new();\n if (s->server) {\n d1->cookie_len = sizeof(s->d1->cookie);\n }\n d1->link_mtu = 0;\n d1->mtu = 0;\n if (d1->buffered_messages == NULL || d1->sent_messages == NULL) {\n pqueue_free(d1->buffered_messages);\n pqueue_free(d1->sent_messages);\n OPENSSL_free(d1);\n ssl3_free(s);\n return (0);\n }\n s->d1 = d1;\n s->method->ssl_clear(s);\n return (1);\n}', 'int DTLS_RECORD_LAYER_new(RECORD_LAYER *rl)\n{\n DTLS_RECORD_LAYER *d;\n if ((d = OPENSSL_malloc(sizeof(*d))) == NULL)\n return (0);\n rl->d = d;\n d->unprocessed_rcds.q = pqueue_new();\n d->processed_rcds.q = pqueue_new();\n d->buffered_app_data.q = pqueue_new();\n if (d->unprocessed_rcds.q == NULL || d->processed_rcds.q == NULL\n || d->buffered_app_data.q == NULL) {\n pqueue_free(d->unprocessed_rcds.q);\n pqueue_free(d->processed_rcds.q);\n pqueue_free(d->buffered_app_data.q);\n OPENSSL_free(d);\n rl->d = NULL;\n return (0);\n }\n return 1;\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}', 'pqueue_s *pqueue_new()\n{\n pqueue_s *pq = OPENSSL_zalloc(sizeof(*pq));\n return pq;\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}']
36,481
0
https://github.com/libav/libav/blob/e5b0fc170f85b00f7dd0ac514918fb5c95253d39/libavcodec/bitstream.h/#L139
static inline uint64_t get_val(BitstreamContext *bc, unsigned n) { #ifdef BITSTREAM_READER_LE uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1); bc->bits >>= n; #else uint64_t ret = bc->bits >> (64 - n); bc->bits <<= n; #endif bc->bits_left -= n; return ret; }
['static int decode_tonal_components(BitstreamContext *bc,\n TonalComponent *components, int num_bands)\n{\n int i, b, c, m;\n int nb_components, coding_mode_selector, coding_mode;\n int band_flags[4], mantissa[8];\n int component_count = 0;\n nb_components = bitstream_read(bc, 5);\n if (nb_components == 0)\n return 0;\n coding_mode_selector = bitstream_read(bc, 2);\n if (coding_mode_selector == 2)\n return AVERROR_INVALIDDATA;\n coding_mode = coding_mode_selector & 1;\n for (i = 0; i < nb_components; i++) {\n int coded_values_per_component, quant_step_index;\n for (b = 0; b <= num_bands; b++)\n band_flags[b] = bitstream_read_bit(bc);\n coded_values_per_component = bitstream_read(bc, 3);\n quant_step_index = bitstream_read(bc, 3);\n if (quant_step_index <= 1)\n return AVERROR_INVALIDDATA;\n if (coding_mode_selector == 3)\n coding_mode = bitstream_read_bit(bc);\n for (b = 0; b < (num_bands + 1) * 4; b++) {\n int coded_components;\n if (band_flags[b >> 2] == 0)\n continue;\n coded_components = bitstream_read(bc, 3);\n for (c = 0; c < coded_components; c++) {\n TonalComponent *cmp = &components[component_count];\n int sf_index, coded_values, max_coded_values;\n float scale_factor;\n sf_index = bitstream_read(bc, 6);\n if (component_count >= 64)\n return AVERROR_INVALIDDATA;\n cmp->pos = b * 64 + bitstream_read(bc, 6);\n max_coded_values = SAMPLES_PER_FRAME - cmp->pos;\n coded_values = coded_values_per_component + 1;\n coded_values = FFMIN(max_coded_values, coded_values);\n scale_factor = ff_atrac_sf_table[sf_index] *\n inv_max_quant[quant_step_index];\n read_quant_spectral_coeffs(bc, quant_step_index, coding_mode,\n mantissa, coded_values);\n cmp->num_coefs = coded_values;\n for (m = 0; m < coded_values; m++)\n cmp->coef[m] = mantissa[m] * scale_factor;\n component_count++;\n }\n }\n }\n return component_count;\n}', 'static inline uint32_t bitstream_read(BitstreamContext *bc, unsigned n)\n{\n if (!n)\n return 0;\n if (n > bc->bits_left) {\n refill_32(bc);\n if (bc->bits_left < 32)\n bc->bits_left = n;\n }\n return get_val(bc, n);\n}', 'static inline 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}']
36,482
0
https://github.com/openssl/openssl/blob/5850cc75ea0c1581a9034390f1ca77cadc596238/crypto/bn/bn_ctx.c/#L328
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['static int ecdsa_do_verify(const unsigned char *dgst, int dgst_len,\n const ECDSA_SIG *sig, EC_KEY *eckey)\n{\n int ret = -1, i;\n BN_CTX *ctx;\n BIGNUM *order, *u1, *u2, *m, *X;\n EC_POINT *point = NULL;\n const EC_GROUP *group;\n const EC_POINT *pub_key;\n if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||\n (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {\n ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_MISSING_PARAMETERS);\n return -1;\n }\n ctx = BN_CTX_new();\n if (!ctx) {\n ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);\n return -1;\n }\n BN_CTX_start(ctx);\n order = BN_CTX_get(ctx);\n u1 = BN_CTX_get(ctx);\n u2 = BN_CTX_get(ctx);\n m = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n if (!X) {\n ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);\n goto err;\n }\n if (!EC_GROUP_get_order(group, order, ctx)) {\n ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);\n goto err;\n }\n if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||\n BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||\n BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {\n ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_BAD_SIGNATURE);\n ret = 0;\n goto err;\n }\n if (!BN_mod_inverse(u2, sig->s, order, ctx)) {\n ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);\n goto err;\n }\n i = BN_num_bits(order);\n if (8 * dgst_len > i)\n dgst_len = (i + 7) / 8;\n if (!BN_bin2bn(dgst, dgst_len, m)) {\n ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);\n goto err;\n }\n if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {\n ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);\n goto err;\n }\n if (!BN_mod_mul(u1, m, u2, order, ctx)) {\n ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);\n goto err;\n }\n if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {\n ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);\n goto err;\n }\n if ((point = EC_POINT_new(group)) == NULL) {\n ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {\n ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);\n goto err;\n }\n if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==\n NID_X9_62_prime_field) {\n if (!EC_POINT_get_affine_coordinates_GFp(group, point, X, NULL, ctx)) {\n ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);\n goto err;\n }\n }\n#ifndef OPENSSL_NO_EC2M\n else {\n if (!EC_POINT_get_affine_coordinates_GF2m(group, point, X, NULL, ctx)) {\n ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);\n goto err;\n }\n }\n#endif\n if (!BN_nnmod(u1, X, order, ctx)) {\n ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);\n goto err;\n }\n ret = (BN_ucmp(u1, sig->r) == 0);\n err:\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n EC_POINT_free(point);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_start", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG_EXIT(ctx);\n}', 'BIGNUM *BN_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n{\n BIGNUM *rv;\n int noinv;\n rv = int_bn_mod_inverse(in, a, n, ctx, &noinv);\n if (noinv)\n BNerr(BN_F_BN_MOD_INVERSE, BN_R_NO_INVERSE);\n return rv;\n}', 'BIGNUM *int_bn_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,\n int *pnoinv)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM *ret = NULL;\n int sign;\n if (pnoinv)\n *pnoinv = 0;\n if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {\n return BN_mod_inverse_no_branch(in, a, n, ctx);\n }\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n if (!BN_nnmod(B, B, A, ctx))\n goto err;\n }\n sign = -1;\n if (BN_is_odd(n) && (BN_num_bits(n) <= (BN_BITS <= 32 ? 450 : 2048))) {\n int shift;\n while (!BN_is_zero(B)) {\n shift = 0;\n while (!BN_is_bit_set(B, shift)) {\n shift++;\n if (BN_is_odd(X)) {\n if (!BN_uadd(X, X, n))\n goto err;\n }\n if (!BN_rshift1(X, X))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(B, B, shift))\n goto err;\n }\n shift = 0;\n while (!BN_is_bit_set(A, shift)) {\n shift++;\n if (BN_is_odd(Y)) {\n if (!BN_uadd(Y, Y, n))\n goto err;\n }\n if (!BN_rshift1(Y, Y))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(A, A, shift))\n goto err;\n }\n if (BN_ucmp(B, A) >= 0) {\n if (!BN_uadd(X, X, Y))\n goto err;\n if (!BN_usub(B, B, A))\n goto err;\n } else {\n if (!BN_uadd(Y, Y, X))\n goto err;\n if (!BN_usub(A, A, B))\n goto err;\n }\n }\n } else {\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n if (BN_num_bits(A) == BN_num_bits(B)) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else if (BN_num_bits(A) == BN_num_bits(B) + 1) {\n if (!BN_lshift1(T, B))\n goto err;\n if (BN_ucmp(A, T) < 0) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else {\n if (!BN_sub(M, A, T))\n goto err;\n if (!BN_add(D, T, B))\n goto err;\n if (BN_ucmp(A, D) < 0) {\n if (!BN_set_word(D, 2))\n goto err;\n } else {\n if (!BN_set_word(D, 3))\n goto err;\n if (!BN_sub(M, M, B))\n goto err;\n }\n }\n } else {\n if (!BN_div(D, M, A, B, ctx))\n goto err;\n }\n tmp = A;\n A = B;\n B = M;\n if (BN_is_one(D)) {\n if (!BN_add(tmp, X, Y))\n goto err;\n } else {\n if (BN_is_word(D, 2)) {\n if (!BN_lshift1(tmp, X))\n goto err;\n } else if (BN_is_word(D, 4)) {\n if (!BN_lshift(tmp, X, 2))\n goto err;\n } else if (D->top == 1) {\n if (!BN_copy(tmp, X))\n goto err;\n if (!BN_mul_word(tmp, D->d[0]))\n goto err;\n } else {\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n }\n if (!BN_add(tmp, tmp, Y))\n goto err;\n }\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n if (pnoinv)\n *pnoinv = 1;\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return (ret);\n}', 'static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n,\n BN_CTX *ctx)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM local_A, local_B;\n BIGNUM *pA, *pB;\n BIGNUM *ret = NULL;\n int sign;\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n pB = &local_B;\n local_B.flags = 0;\n BN_with_flags(pB, B, BN_FLG_CONSTTIME);\n if (!BN_nnmod(B, pB, A, ctx))\n goto err;\n }\n sign = -1;\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n pA = &local_A;\n local_A.flags = 0;\n BN_with_flags(pA, A, BN_FLG_CONSTTIME);\n if (!BN_div(D, M, pA, B, ctx))\n goto err;\n tmp = A;\n A = B;\n B = M;\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n if (!BN_add(tmp, tmp, Y))\n goto err;\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n BNerr(BN_F_BN_MOD_INVERSE_NO_BRANCH, BN_R_NO_INVERSE);\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return (ret);\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return (0);\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return (0);\n }\n if (dv != NULL)\n BN_zero(dv);\n return (1);\n }\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (dv == NULL)\n res = BN_CTX_get(ctx);\n else\n res = dv;\n if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n res->neg = (num->neg ^ divisor->neg);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--, resp--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# ifdef BN_DEBUG_LEVITTE\n fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n", n0, n1, d0, q);\n# endif\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifdef BN_DEBUG_LEVITTE\n fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n", n0, n1, d0, q);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return (1);\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return (0);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
36,483
0
https://github.com/openssl/openssl/blob/9b02dc97e4963969da69675a871dbe80e6d31cda/crypto/bn/bn_ctx.c/#L273
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)\n{\n BIGNUM *kinv = NULL;\n BIGNUM *m;\n BIGNUM *xr;\n BN_CTX *ctx = NULL;\n int reason = ERR_R_BN_LIB;\n DSA_SIG *ret = NULL;\n int rv = 0;\n m = BN_new();\n xr = BN_new();\n if (m == NULL || xr == NULL)\n goto err;\n if (!dsa->p || !dsa->q || !dsa->g) {\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 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 if (!BN_mod_mul(xr, dsa->priv_key, ret->r, dsa->q, ctx))\n goto err;\n if (!BN_add(ret->s, xr, m))\n goto err;\n if (BN_cmp(ret->s, dsa->q) > 0)\n if (!BN_sub(ret->s, ret->s, dsa->q))\n goto err;\n if (!BN_mod_mul(ret->s, ret->s, kinv, 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(m);\n BN_clear_free(xr);\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 int ret = 0;\n if (!dsa->p || !dsa->q || !dsa->g) {\n DSAerr(DSA_F_DSA_SIGN_SETUP, DSA_R_MISSING_PARAMETERS);\n return 0;\n }\n k = BN_new();\n if (k == 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 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 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(k, k, dsa->q))\n goto err;\n if (BN_num_bits(k) <= BN_num_bits(dsa->q)) {\n if (!BN_add(k, k, dsa->q))\n goto err;\n }\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 = BN_mod_inverse(NULL, 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 return ret;\n}', 'int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,\n BN_CTX *ctx)\n{\n BIGNUM *t;\n int ret = 0;\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(m);\n BN_CTX_start(ctx);\n if ((t = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (a == b) {\n if (!BN_sqr(t, a, ctx))\n goto err;\n } else {\n if (!BN_mul(t, a, b, ctx))\n goto err;\n }\n if (!BN_nnmod(r, t, m, ctx))\n goto err;\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_start", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG_EXIT(ctx);\n}', 'int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = 0;\n int top, al, bl;\n BIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n int i;\n#endif\n#ifdef BN_RECURSION\n BIGNUM *t = NULL;\n int j = 0, k;\n#endif\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(r);\n al = a->top;\n bl = b->top;\n if ((al == 0) || (bl == 0)) {\n BN_zero(r);\n return 1;\n }\n top = al + bl;\n BN_CTX_start(ctx);\n if ((r == a) || (r == b)) {\n if ((rr = BN_CTX_get(ctx)) == NULL)\n goto err;\n } else\n rr = r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n i = al - bl;\n#endif\n#ifdef BN_MUL_COMBA\n if (i == 0) {\n# if 0\n if (al == 4) {\n if (bn_wexpand(rr, 8) == NULL)\n goto err;\n rr->top = 8;\n bn_mul_comba4(rr->d, a->d, b->d);\n goto end;\n }\n# endif\n if (al == 8) {\n if (bn_wexpand(rr, 16) == NULL)\n goto err;\n rr->top = 16;\n bn_mul_comba8(rr->d, a->d, b->d);\n goto end;\n }\n }\n#endif\n#ifdef BN_RECURSION\n if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {\n if (i >= -1 && i <= 1) {\n if (i >= 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n }\n if (i == -1) {\n j = BN_num_bits_word((BN_ULONG)bl);\n }\n j = 1 << (j - 1);\n assert(j <= al || j <= bl);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (al > j || bl > j) {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d,\n j, al - j, bl - j, t->d);\n } else {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);\n }\n rr->top = top;\n goto end;\n }\n }\n#endif\n if (bn_wexpand(rr, top) == NULL)\n goto err;\n rr->top = top;\n bn_mul_normal(rr->d, a->d, al, b->d, bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n end:\n#endif\n rr->neg = a->neg ^ b->neg;\n bn_correct_top(rr);\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(r);\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
36,484
0
https://github.com/openssl/openssl/blob/388de53c274dee20c07eee7ff892108668fb3a61/crypto/rand/rand_lib.c/#L787
void RAND_add(const void *buf, int num, double randomness) { const RAND_METHOD *meth = RAND_get_rand_method(); if (meth->add != NULL) meth->add(buf, num, randomness); }
['void RAND_add(const void *buf, int num, double randomness)\n{\n const RAND_METHOD *meth = RAND_get_rand_method();\n if (meth->add != NULL)\n meth->add(buf, num, randomness);\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}']
36,485
0
https://github.com/openssl/openssl/blob/38d1b3cc0271008b8bd130a2c4b442775b028a08/crypto/bn/bn_lib.c/#L1027
void bn_correct_top(BIGNUM *a) { BN_ULONG *ftl; int tmp_top = a->top; if (tmp_top > 0) { for (ftl = &(a->d[tmp_top]); tmp_top > 0; tmp_top--) { ftl--; if (*ftl != 0) break; } a->top = tmp_top; } if (a->top == 0) a->neg = 0; bn_pollute(a); }
['int RSA_X931_derive_ex(RSA *rsa, BIGNUM *p1, BIGNUM *p2, BIGNUM *q1,\n BIGNUM *q2, const BIGNUM *Xp1, const BIGNUM *Xp2,\n const BIGNUM *Xp, const BIGNUM *Xq1, const BIGNUM *Xq2,\n const BIGNUM *Xq, const BIGNUM *e, BN_GENCB *cb)\n{\n BIGNUM *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL;\n BN_CTX *ctx = NULL, *ctx2 = NULL;\n int ret = 0;\n if (!rsa)\n goto err;\n ctx = BN_CTX_new();\n if (ctx == NULL)\n goto err;\n BN_CTX_start(ctx);\n r0 = BN_CTX_get(ctx);\n r1 = BN_CTX_get(ctx);\n r2 = BN_CTX_get(ctx);\n r3 = BN_CTX_get(ctx);\n if (r3 == NULL)\n goto err;\n if (!rsa->e) {\n rsa->e = BN_dup(e);\n if (!rsa->e)\n goto err;\n } else\n e = rsa->e;\n if (Xp && rsa->p == NULL) {\n rsa->p = BN_new();\n if (rsa->p == NULL)\n goto err;\n if (!BN_X931_derive_prime_ex(rsa->p, p1, p2,\n Xp, Xp1, Xp2, e, ctx, cb))\n goto err;\n }\n if (Xq && rsa->q == NULL) {\n rsa->q = BN_new();\n if (rsa->q == NULL)\n goto err;\n if (!BN_X931_derive_prime_ex(rsa->q, q1, q2,\n Xq, Xq1, Xq2, e, ctx, cb))\n goto err;\n }\n if (rsa->p == NULL || rsa->q == NULL) {\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n return 2;\n }\n rsa->n = BN_new();\n if (rsa->n == NULL)\n goto err;\n if (!BN_mul(rsa->n, rsa->p, rsa->q, ctx))\n goto err;\n if (!BN_sub(r1, rsa->p, BN_value_one()))\n goto err;\n if (!BN_sub(r2, rsa->q, BN_value_one()))\n goto err;\n if (!BN_mul(r0, r1, r2, ctx))\n goto err;\n if (!BN_gcd(r3, r1, r2, ctx))\n goto err;\n if (!BN_div(r0, NULL, r0, r3, ctx))\n goto err;\n ctx2 = BN_CTX_new();\n if (ctx2 == NULL)\n goto err;\n rsa->d = BN_mod_inverse(NULL, rsa->e, r0, ctx2);\n if (rsa->d == NULL)\n goto err;\n rsa->dmp1 = BN_new();\n if (rsa->dmp1 == NULL)\n goto err;\n if (!BN_mod(rsa->dmp1, rsa->d, r1, ctx))\n goto err;\n rsa->dmq1 = BN_new();\n if (rsa->dmq1 == NULL)\n goto err;\n if (!BN_mod(rsa->dmq1, rsa->d, r2, ctx))\n goto err;\n rsa->iqmp = BN_mod_inverse(NULL, rsa->q, rsa->p, ctx2);\n ret = 1;\n err:\n if (ctx)\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n BN_CTX_free(ctx2);\n return ret;\n}', 'BIGNUM *BN_dup(const BIGNUM *a)\n{\n BIGNUM *t;\n if (a == NULL)\n return NULL;\n bn_check_top(a);\n t = BN_get_flags(a, BN_FLG_SECURE) ? BN_secure_new() : BN_new();\n if (t == NULL)\n return NULL;\n if (!BN_copy(t, a)) {\n BN_free(t);\n return NULL;\n }\n bn_check_top(t);\n return t;\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n int i;\n BN_ULONG *A;\n const BN_ULONG *B;\n bn_check_top(b);\n if (a == b)\n return (a);\n if (bn_wexpand(a, b->top) == NULL)\n return (NULL);\n#if 1\n A = a->d;\n B = b->d;\n for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {\n BN_ULONG a0, a1, a2, a3;\n a0 = B[0];\n a1 = B[1];\n a2 = B[2];\n a3 = B[3];\n A[0] = a0;\n A[1] = a1;\n A[2] = a2;\n A[3] = a3;\n }\n switch (b->top & 3) {\n case 3:\n A[2] = B[2];\n case 2:\n A[1] = B[1];\n case 1:\n A[0] = B[0];\n case 0:;\n }\n#else\n memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n#endif\n a->top = b->top;\n a->neg = b->neg;\n bn_check_top(a);\n return (a);\n}', 'int BN_X931_derive_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2,\n const BIGNUM *Xp, const BIGNUM *Xp1,\n const BIGNUM *Xp2, const BIGNUM *e, BN_CTX *ctx,\n BN_GENCB *cb)\n{\n int ret = 0;\n BIGNUM *t, *p1p2, *pm1;\n if (!BN_is_odd(e))\n return 0;\n BN_CTX_start(ctx);\n if (!p1)\n p1 = BN_CTX_get(ctx);\n if (!p2)\n p2 = BN_CTX_get(ctx);\n t = BN_CTX_get(ctx);\n p1p2 = BN_CTX_get(ctx);\n pm1 = BN_CTX_get(ctx);\n if (pm1 == NULL)\n goto err;\n if (!bn_x931_derive_pi(p1, Xp1, ctx, cb))\n goto err;\n if (!bn_x931_derive_pi(p2, Xp2, ctx, cb))\n goto err;\n if (!BN_mul(p1p2, p1, p2, ctx))\n goto err;\n if (!BN_mod_inverse(p, p2, p1, ctx))\n goto err;\n if (!BN_mul(p, p, p2, ctx))\n goto err;\n if (!BN_mod_inverse(t, p1, p2, ctx))\n goto err;\n if (!BN_mul(t, t, p1, ctx))\n goto err;\n if (!BN_sub(p, p, t))\n goto err;\n if (p->neg && !BN_add(p, p, p1p2))\n goto err;\n if (!BN_mod_sub(p, p, Xp, p1p2, ctx))\n goto err;\n if (!BN_add(p, p, Xp))\n goto err;\n for (;;) {\n int i = 1;\n BN_GENCB_call(cb, 0, i++);\n if (!BN_copy(pm1, p))\n goto err;\n if (!BN_sub_word(pm1, 1))\n goto err;\n if (!BN_gcd(t, pm1, e, ctx))\n goto err;\n if (BN_is_one(t)) {\n int r = BN_is_prime_fasttest_ex(p, 50, ctx, 1, cb);\n if (r < 0)\n goto err;\n if (r)\n break;\n }\n if (!BN_add(p, p, p1p2))\n goto err;\n }\n BN_GENCB_call(cb, 3, 0);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'BIGNUM *BN_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n{\n BIGNUM *rv;\n int noinv;\n rv = int_bn_mod_inverse(in, a, n, ctx, &noinv);\n if (noinv)\n BNerr(BN_F_BN_MOD_INVERSE, BN_R_NO_INVERSE);\n return rv;\n}', 'BIGNUM *int_bn_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,\n int *pnoinv)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM *ret = NULL;\n int sign;\n if (pnoinv)\n *pnoinv = 0;\n if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {\n return BN_mod_inverse_no_branch(in, a, n, ctx);\n }\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n if (!BN_nnmod(B, B, A, ctx))\n goto err;\n }\n sign = -1;\n if (BN_is_odd(n) && (BN_num_bits(n) <= 2048)) {\n int shift;\n while (!BN_is_zero(B)) {\n shift = 0;\n while (!BN_is_bit_set(B, shift)) {\n shift++;\n if (BN_is_odd(X)) {\n if (!BN_uadd(X, X, n))\n goto err;\n }\n if (!BN_rshift1(X, X))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(B, B, shift))\n goto err;\n }\n shift = 0;\n while (!BN_is_bit_set(A, shift)) {\n shift++;\n if (BN_is_odd(Y)) {\n if (!BN_uadd(Y, Y, n))\n goto err;\n }\n if (!BN_rshift1(Y, Y))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(A, A, shift))\n goto err;\n }\n if (BN_ucmp(B, A) >= 0) {\n if (!BN_uadd(X, X, Y))\n goto err;\n if (!BN_usub(B, B, A))\n goto err;\n } else {\n if (!BN_uadd(Y, Y, X))\n goto err;\n if (!BN_usub(A, A, B))\n goto err;\n }\n }\n } else {\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n if (BN_num_bits(A) == BN_num_bits(B)) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else if (BN_num_bits(A) == BN_num_bits(B) + 1) {\n if (!BN_lshift1(T, B))\n goto err;\n if (BN_ucmp(A, T) < 0) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else {\n if (!BN_sub(M, A, T))\n goto err;\n if (!BN_add(D, T, B))\n goto err;\n if (BN_ucmp(A, D) < 0) {\n if (!BN_set_word(D, 2))\n goto err;\n } else {\n if (!BN_set_word(D, 3))\n goto err;\n if (!BN_sub(M, M, B))\n goto err;\n }\n }\n } else {\n if (!BN_div(D, M, A, B, ctx))\n goto err;\n }\n tmp = A;\n A = B;\n B = M;\n if (BN_is_one(D)) {\n if (!BN_add(tmp, X, Y))\n goto err;\n } else {\n if (BN_is_word(D, 2)) {\n if (!BN_lshift1(tmp, X))\n goto err;\n } else if (BN_is_word(D, 4)) {\n if (!BN_lshift(tmp, X, 2))\n goto err;\n } else if (D->top == 1) {\n if (!BN_copy(tmp, X))\n goto err;\n if (!BN_mul_word(tmp, D->d[0]))\n goto err;\n } else {\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n }\n if (!BN_add(tmp, tmp, Y))\n goto err;\n }\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n if (pnoinv)\n *pnoinv = 1;\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return (ret);\n}', 'int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = 0;\n int top, al, bl;\n BIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n int i;\n#endif\n#ifdef BN_RECURSION\n BIGNUM *t = NULL;\n int j = 0, k;\n#endif\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(r);\n al = a->top;\n bl = b->top;\n if ((al == 0) || (bl == 0)) {\n BN_zero(r);\n return (1);\n }\n top = al + bl;\n BN_CTX_start(ctx);\n if ((r == a) || (r == b)) {\n if ((rr = BN_CTX_get(ctx)) == NULL)\n goto err;\n } else\n rr = r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n i = al - bl;\n#endif\n#ifdef BN_MUL_COMBA\n if (i == 0) {\n# if 0\n if (al == 4) {\n if (bn_wexpand(rr, 8) == NULL)\n goto err;\n rr->top = 8;\n bn_mul_comba4(rr->d, a->d, b->d);\n goto end;\n }\n# endif\n if (al == 8) {\n if (bn_wexpand(rr, 16) == NULL)\n goto err;\n rr->top = 16;\n bn_mul_comba8(rr->d, a->d, b->d);\n goto end;\n }\n }\n#endif\n#ifdef BN_RECURSION\n if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {\n if (i >= -1 && i <= 1) {\n if (i >= 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n }\n if (i == -1) {\n j = BN_num_bits_word((BN_ULONG)bl);\n }\n j = 1 << (j - 1);\n assert(j <= al || j <= bl);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (al > j || bl > j) {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d,\n j, al - j, bl - j, t->d);\n } else {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);\n }\n rr->top = top;\n goto end;\n }\n# if 0\n if (i == 1 && !BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BIGNUM *tmp_bn = (BIGNUM *)b;\n if (bn_wexpand(tmp_bn, al) == NULL)\n goto err;\n tmp_bn->d[bl] = 0;\n bl++;\n i--;\n } else if (i == -1 && !BN_get_flags(a, BN_FLG_STATIC_DATA)) {\n BIGNUM *tmp_bn = (BIGNUM *)a;\n if (bn_wexpand(tmp_bn, bl) == NULL)\n goto err;\n tmp_bn->d[al] = 0;\n al++;\n i++;\n }\n if (i == 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (al == j) {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, al, t->d);\n } else {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d, al - j, j, t->d);\n }\n rr->top = top;\n goto end;\n }\n# endif\n }\n#endif\n if (bn_wexpand(rr, top) == NULL)\n goto err;\n rr->top = top;\n bn_mul_normal(rr->d, a->d, al, b->d, bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n end:\n#endif\n rr->neg = a->neg ^ b->neg;\n bn_correct_top(rr);\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(r);\n BN_CTX_end(ctx);\n return (ret);\n}', 'void bn_correct_top(BIGNUM *a)\n{\n BN_ULONG *ftl;\n int tmp_top = a->top;\n if (tmp_top > 0) {\n for (ftl = &(a->d[tmp_top]); tmp_top > 0; tmp_top--) {\n ftl--;\n if (*ftl != 0)\n break;\n }\n a->top = tmp_top;\n }\n if (a->top == 0)\n a->neg = 0;\n bn_pollute(a);\n}']
36,486
0
https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/msmpeg4.c/#L850
static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr) { int sign, code; int pred, extquant; int extrabits = 0; if(s->msmpeg4_version==1){ int32_t *dc_val; pred = msmpeg4v1_pred_dc(s, n, &dc_val); *dc_val= level; }else{ int16_t *dc_val; pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr); if (n < 4) { *dc_val = level * s->y_dc_scale; } else { *dc_val = level * s->c_dc_scale; } } level -= pred; if(s->msmpeg4_version<=2){ if (n < 4) { put_bits(&s->pb, v2_dc_lum_table[level+256][1], v2_dc_lum_table[level+256][0]); }else{ put_bits(&s->pb, v2_dc_chroma_table[level+256][1], v2_dc_chroma_table[level+256][0]); } }else{ sign = 0; if (level < 0) { level = -level; sign = 1; } code = level; if (code > DC_MAX) code = DC_MAX; else if( s->msmpeg4_version>=6 ) { if( s->qscale == 1 ) { extquant = (level + 3) & 0x3; code = ((level+3)>>2); } else if( s->qscale == 2 ) { extquant = (level + 1) & 0x1; code = ((level+1)>>1); } } if (s->dc_table_index == 0) { if (n < 4) { put_bits(&s->pb, ff_table0_dc_lum[code][1], ff_table0_dc_lum[code][0]); } else { put_bits(&s->pb, ff_table0_dc_chroma[code][1], ff_table0_dc_chroma[code][0]); } } else { if (n < 4) { put_bits(&s->pb, ff_table1_dc_lum[code][1], ff_table1_dc_lum[code][0]); } else { put_bits(&s->pb, ff_table1_dc_chroma[code][1], ff_table1_dc_chroma[code][0]); } } if(s->msmpeg4_version>=6 && s->qscale<=2) extrabits = 3 - s->qscale; if (code == DC_MAX) put_bits(&s->pb, 8 + extrabits, level); else if(extrabits > 0) put_bits(&s->pb, extrabits, extquant); if (level != 0) { put_bits(&s->pb, 1, sign); } } }
['static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)\n{\n int sign, code;\n int pred, extquant;\n int extrabits = 0;\n if(s->msmpeg4_version==1){\n int32_t *dc_val;\n pred = msmpeg4v1_pred_dc(s, n, &dc_val);\n *dc_val= level;\n }else{\n int16_t *dc_val;\n pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);\n if (n < 4) {\n *dc_val = level * s->y_dc_scale;\n } else {\n *dc_val = level * s->c_dc_scale;\n }\n }\n level -= pred;\n if(s->msmpeg4_version<=2){\n if (n < 4) {\n put_bits(&s->pb,\n v2_dc_lum_table[level+256][1],\n v2_dc_lum_table[level+256][0]);\n }else{\n put_bits(&s->pb,\n v2_dc_chroma_table[level+256][1],\n v2_dc_chroma_table[level+256][0]);\n }\n }else{\n sign = 0;\n if (level < 0) {\n level = -level;\n sign = 1;\n }\n code = level;\n if (code > DC_MAX)\n code = DC_MAX;\n else if( s->msmpeg4_version>=6 ) {\n if( s->qscale == 1 ) {\n extquant = (level + 3) & 0x3;\n code = ((level+3)>>2);\n } else if( s->qscale == 2 ) {\n extquant = (level + 1) & 0x1;\n code = ((level+1)>>1);\n }\n }\n if (s->dc_table_index == 0) {\n if (n < 4) {\n put_bits(&s->pb, ff_table0_dc_lum[code][1], ff_table0_dc_lum[code][0]);\n } else {\n put_bits(&s->pb, ff_table0_dc_chroma[code][1], ff_table0_dc_chroma[code][0]);\n }\n } else {\n if (n < 4) {\n put_bits(&s->pb, ff_table1_dc_lum[code][1], ff_table1_dc_lum[code][0]);\n } else {\n put_bits(&s->pb, ff_table1_dc_chroma[code][1], ff_table1_dc_chroma[code][0]);\n }\n }\n if(s->msmpeg4_version>=6 && s->qscale<=2)\n extrabits = 3 - s->qscale;\n if (code == DC_MAX)\n put_bits(&s->pb, 8 + extrabits, level);\n else if(extrabits > 0)\n put_bits(&s->pb, extrabits, extquant);\n if (level != 0) {\n put_bits(&s->pb, 1, sign);\n }\n }\n}']
36,487
0
https://github.com/openssl/openssl/blob/141d7325e7e513102fb8c123dab62dbbd61e846d/crypto/err/err.c/#L378
void ERR_clear_error(void) { int i; ERR_STATE *es; es = ERR_get_state(); for (i = 0; i < ERR_NUM_ERRORS; i++) { err_clear(es, i); } es->top = es->bottom = 0; }
['void ERR_clear_error(void)\n{\n int i;\n ERR_STATE *es;\n es = ERR_get_state();\n for (i = 0; i < ERR_NUM_ERRORS; i++) {\n err_clear(es, i);\n }\n es->top = es->bottom = 0;\n}', 'ERR_STATE *ERR_get_state(void)\n{\n ERR_STATE *state = NULL;\n if (!RUN_ONCE(&err_init, err_do_init))\n return NULL;\n state = CRYPTO_THREAD_get_local(&err_thread_local);\n if (state == NULL) {\n state = OPENSSL_zalloc(sizeof(*state));\n if (state == NULL)\n return NULL;\n if (!CRYPTO_THREAD_set_local(&err_thread_local, state)) {\n ERR_STATE_free(state);\n return NULL;\n }\n OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);\n ossl_init_thread_start(OPENSSL_INIT_THREAD_ERR_STATE);\n }\n return state;\n}', 'int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))\n{\n if (pthread_once(once, init) != 0)\n return 0;\n return 1;\n}']
36,488
0
https://github.com/openssl/openssl/blob/019bfef89964105cdf9256b6a6bc0aa7790bd020/engines/ccgost/gost_pmeth.c/#L323
static int pkey_gost01_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey,int type) { struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx); EC_KEY *ec=NULL; if (data->sign_param_nid == NID_undef) { if (type == NID_id_GostR3410_2001_cc) { data->sign_param_nid = NID_id_GostR3410_2001_ParamSet_cc; } else { GOSTerr(GOST_F_PKEY_GOST01_KEYGEN, GOST_R_NO_PARAMETERS_SET); return 0; } } ec = EC_KEY_new(); if (!fill_GOST2001_params(ec,data->sign_param_nid)) { EC_KEY_free(ec); return 0; } gost2001_keygen(ec); EVP_PKEY_assign(pkey,type,ec); return 1; }
['static int pkey_gost01_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey,int type)\n\t{\n\tstruct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);\n\tEC_KEY *ec=NULL;\n\tif (data->sign_param_nid == NID_undef)\n\t\t{\n\t\tif (type == NID_id_GostR3410_2001_cc)\n\t\t\t{\n\t\t\tdata->sign_param_nid = NID_id_GostR3410_2001_ParamSet_cc;\n\t\t\t}\n\t\telse {\n\t\t\tGOSTerr(GOST_F_PKEY_GOST01_KEYGEN,\n\t\t\t\tGOST_R_NO_PARAMETERS_SET);\n\t\t\treturn 0;\n\t\t\t}\n\t\t}\n\tec = EC_KEY_new();\n\tif (!fill_GOST2001_params(ec,data->sign_param_nid))\n\t\t{\n\t\tEC_KEY_free(ec);\n\t\treturn 0;\n\t\t}\n\tgost2001_keygen(ec);\n\tEVP_PKEY_assign(pkey,type,ec);\n\treturn 1;\n\t}', 'void *EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx)\n\t{\n\treturn ctx->data;\n\t}', 'EC_KEY *EC_KEY_new(void)\n\t{\n\tEC_KEY *ret;\n\tret=(EC_KEY *)OPENSSL_malloc(sizeof(EC_KEY));\n\tif (ret == NULL)\n\t\t{\n\t\tECerr(EC_F_EC_KEY_NEW, ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tret->version = 1;\n\tret->group = NULL;\n\tret->pub_key = NULL;\n\tret->priv_key= NULL;\n\tret->enc_flag= 0;\n\tret->conv_form = POINT_CONVERSION_UNCOMPRESSED;\n\tret->references= 1;\n\tret->method_data = NULL;\n\treturn(ret);\n\t}', 'void *CRYPTO_malloc(int num, const char *file, int line)\n\t{\n\tvoid *ret = NULL;\n\textern unsigned char cleanse_ctr;\n\tif (num <= 0) return NULL;\n\tallow_customize = 0;\n\tif (malloc_debug_func != NULL)\n\t\t{\n\t\tallow_customize_debug = 0;\n\t\tmalloc_debug_func(NULL, num, file, line, 0);\n\t\t}\n\tret = malloc_ex_func(num,file,line);\n#ifdef LEVITTE_DEBUG_MEM\n\tfprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\\n", ret, num);\n#endif\n\tif (malloc_debug_func != NULL)\n\t\tmalloc_debug_func(ret, num, file, line, 1);\n if(ret && (num > 2048))\n ((unsigned char *)ret)[0] = cleanse_ctr;\n\treturn ret;\n\t}', 'void ERR_put_error(int lib, int func, int reason, const char *file,\n\t int line)\n\t{\n\tERR_STATE *es;\n#ifdef _OSD_POSIX\n\tif (strncmp(file,"*POSIX(", sizeof("*POSIX(")-1) == 0) {\n\t\tchar *end;\n\t\tfile += sizeof("*POSIX(")-1;\n\t\tend = &file[strlen(file)-1];\n\t\tif (*end == \')\')\n\t\t\t*end = \'\\0\';\n\t\tif ((end = strrchr(file, \'/\')) != NULL)\n\t\t\tfile = &end[1];\n\t}\n#endif\n\tes=ERR_get_state();\n\tes->top=(es->top+1)%ERR_NUM_ERRORS;\n\tif (es->top == es->bottom)\n\t\tes->bottom=(es->bottom+1)%ERR_NUM_ERRORS;\n\tes->err_flags[es->top]=0;\n\tes->err_buffer[es->top]=ERR_PACK(lib,func,reason);\n\tes->err_file[es->top]=file;\n\tes->err_line[es->top]=line;\n\terr_clear_data(es,es->top);\n\t}']
36,489
1
https://github.com/openssl/openssl/blob/21f198ec4874f7e2780a0afd0bdd3c038f69ed11/crypto/asn1/a_digest.c/#L41
int ASN1_digest(i2d_of_void *i2d, const EVP_MD *type, char *data, unsigned char *md, unsigned int *len) { int i; unsigned char *str, *p; i = i2d(data, NULL); if ((str = OPENSSL_malloc(i)) == NULL) { ASN1err(ASN1_F_ASN1_DIGEST, ERR_R_MALLOC_FAILURE); return (0); } p = str; i2d(data, &p); if (!EVP_Digest(str, i, md, len, type, NULL)) return 0; OPENSSL_free(str); return (1); }
['int ASN1_digest(i2d_of_void *i2d, const EVP_MD *type, char *data,\n unsigned char *md, unsigned int *len)\n{\n int i;\n unsigned char *str, *p;\n i = i2d(data, NULL);\n if ((str = OPENSSL_malloc(i)) == NULL) {\n ASN1err(ASN1_F_ASN1_DIGEST, ERR_R_MALLOC_FAILURE);\n return (0);\n }\n p = str;\n i2d(data, &p);\n if (!EVP_Digest(str, i, md, len, type, NULL))\n return 0;\n OPENSSL_free(str);\n return (1);\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}']
36,490
0
https://github.com/libav/libav/blob/4ab26cb4cc9af2ab2199105aa273aa23e1f27911/libavcodec/mpeg12enc.c/#L317
static av_always_inline void put_qscale(MpegEncContext *s) { if(s->q_scale_type){ assert(s->qscale>=1 && s->qscale <=12); put_bits(&s->pb, 5, inv_non_linear_qscale[s->qscale]); }else{ put_bits(&s->pb, 5, s->qscale); } }
['static av_always_inline void encode_mb_internal(MpegEncContext *s,\n int motion_x, int motion_y,\n int mb_block_height,\n int mb_block_count)\n{\n int16_t weight[8][64];\n DCTELEM orig[8][64];\n const int mb_x = s->mb_x;\n const int mb_y = s->mb_y;\n int i;\n int skip_dct[8];\n int dct_offset = s->linesize * 8;\n uint8_t *ptr_y, *ptr_cb, *ptr_cr;\n int wrap_y, wrap_c;\n for (i = 0; i < mb_block_count; i++)\n skip_dct[i] = s->skipdct;\n if (s->adaptive_quant) {\n const int last_qp = s->qscale;\n const int mb_xy = mb_x + mb_y * s->mb_stride;\n s->lambda = s->lambda_table[mb_xy];\n update_qscale(s);\n if (!(s->mpv_flags & FF_MPV_FLAG_QP_RD)) {\n s->qscale = s->current_picture_ptr->f.qscale_table[mb_xy];\n s->dquant = s->qscale - last_qp;\n if (s->out_format == FMT_H263) {\n s->dquant = av_clip(s->dquant, -2, 2);\n if (s->codec_id == AV_CODEC_ID_MPEG4) {\n if (!s->mb_intra) {\n if (s->pict_type == AV_PICTURE_TYPE_B) {\n if (s->dquant & 1 || s->mv_dir & MV_DIRECT)\n s->dquant = 0;\n }\n if (s->mv_type == MV_TYPE_8X8)\n s->dquant = 0;\n }\n }\n }\n }\n ff_set_qscale(s, last_qp + s->dquant);\n } else if (s->mpv_flags & FF_MPV_FLAG_QP_RD)\n ff_set_qscale(s, s->qscale + s->dquant);\n wrap_y = s->linesize;\n wrap_c = s->uvlinesize;\n ptr_y = s->new_picture.f.data[0] +\n (mb_y * 16 * wrap_y) + mb_x * 16;\n ptr_cb = s->new_picture.f.data[1] +\n (mb_y * mb_block_height * wrap_c) + mb_x * 8;\n ptr_cr = s->new_picture.f.data[2] +\n (mb_y * mb_block_height * wrap_c) + mb_x * 8;\n if (mb_x * 16 + 16 > s->width || mb_y * 16 + 16 > s->height) {\n uint8_t *ebuf = s->edge_emu_buffer + 32;\n s->dsp.emulated_edge_mc(ebuf, ptr_y, wrap_y, 16, 16, mb_x * 16,\n mb_y * 16, s->width, s->height);\n ptr_y = ebuf;\n s->dsp.emulated_edge_mc(ebuf + 18 * wrap_y, ptr_cb, wrap_c, 8,\n mb_block_height, mb_x * 8, mb_y * 8,\n s->width >> 1, s->height >> 1);\n ptr_cb = ebuf + 18 * wrap_y;\n s->dsp.emulated_edge_mc(ebuf + 18 * wrap_y + 8, ptr_cr, wrap_c, 8,\n mb_block_height, mb_x * 8, mb_y * 8,\n s->width >> 1, s->height >> 1);\n ptr_cr = ebuf + 18 * wrap_y + 8;\n }\n if (s->mb_intra) {\n if (s->flags & CODEC_FLAG_INTERLACED_DCT) {\n int progressive_score, interlaced_score;\n s->interlaced_dct = 0;\n progressive_score = s->dsp.ildct_cmp[4](s, ptr_y,\n NULL, wrap_y, 8) +\n s->dsp.ildct_cmp[4](s, ptr_y + wrap_y * 8,\n NULL, wrap_y, 8) - 400;\n if (progressive_score > 0) {\n interlaced_score = s->dsp.ildct_cmp[4](s, ptr_y,\n NULL, wrap_y * 2, 8) +\n s->dsp.ildct_cmp[4](s, ptr_y + wrap_y,\n NULL, wrap_y * 2, 8);\n if (progressive_score > interlaced_score) {\n s->interlaced_dct = 1;\n dct_offset = wrap_y;\n wrap_y <<= 1;\n if (s->chroma_format == CHROMA_422)\n wrap_c <<= 1;\n }\n }\n }\n s->dsp.get_pixels(s->block[0], ptr_y , wrap_y);\n s->dsp.get_pixels(s->block[1], ptr_y + 8 , wrap_y);\n s->dsp.get_pixels(s->block[2], ptr_y + dct_offset , wrap_y);\n s->dsp.get_pixels(s->block[3], ptr_y + dct_offset + 8 , wrap_y);\n if (s->flags & CODEC_FLAG_GRAY) {\n skip_dct[4] = 1;\n skip_dct[5] = 1;\n } else {\n s->dsp.get_pixels(s->block[4], ptr_cb, wrap_c);\n s->dsp.get_pixels(s->block[5], ptr_cr, wrap_c);\n if (!s->chroma_y_shift) {\n s->dsp.get_pixels(s->block[6],\n ptr_cb + (dct_offset >> 1), wrap_c);\n s->dsp.get_pixels(s->block[7],\n ptr_cr + (dct_offset >> 1), wrap_c);\n }\n }\n } else {\n op_pixels_func (*op_pix)[4];\n qpel_mc_func (*op_qpix)[16];\n uint8_t *dest_y, *dest_cb, *dest_cr;\n dest_y = s->dest[0];\n dest_cb = s->dest[1];\n dest_cr = s->dest[2];\n if ((!s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) {\n op_pix = s->dsp.put_pixels_tab;\n op_qpix = s->dsp.put_qpel_pixels_tab;\n } else {\n op_pix = s->dsp.put_no_rnd_pixels_tab;\n op_qpix = s->dsp.put_no_rnd_qpel_pixels_tab;\n }\n if (s->mv_dir & MV_DIR_FORWARD) {\n MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data,\n op_pix, op_qpix);\n op_pix = s->dsp.avg_pixels_tab;\n op_qpix = s->dsp.avg_qpel_pixels_tab;\n }\n if (s->mv_dir & MV_DIR_BACKWARD) {\n MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data,\n op_pix, op_qpix);\n }\n if (s->flags & CODEC_FLAG_INTERLACED_DCT) {\n int progressive_score, interlaced_score;\n s->interlaced_dct = 0;\n progressive_score = s->dsp.ildct_cmp[0](s, dest_y,\n ptr_y, wrap_y,\n 8) +\n s->dsp.ildct_cmp[0](s, dest_y + wrap_y * 8,\n ptr_y + wrap_y * 8, wrap_y,\n 8) - 400;\n if (s->avctx->ildct_cmp == FF_CMP_VSSE)\n progressive_score -= 400;\n if (progressive_score > 0) {\n interlaced_score = s->dsp.ildct_cmp[0](s, dest_y,\n ptr_y,\n wrap_y * 2, 8) +\n s->dsp.ildct_cmp[0](s, dest_y + wrap_y,\n ptr_y + wrap_y,\n wrap_y * 2, 8);\n if (progressive_score > interlaced_score) {\n s->interlaced_dct = 1;\n dct_offset = wrap_y;\n wrap_y <<= 1;\n if (s->chroma_format == CHROMA_422)\n wrap_c <<= 1;\n }\n }\n }\n s->dsp.diff_pixels(s->block[0], ptr_y, dest_y, wrap_y);\n s->dsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);\n s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset,\n dest_y + dct_offset, wrap_y);\n s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8,\n dest_y + dct_offset + 8, wrap_y);\n if (s->flags & CODEC_FLAG_GRAY) {\n skip_dct[4] = 1;\n skip_dct[5] = 1;\n } else {\n s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);\n s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);\n if (!s->chroma_y_shift) {\n s->dsp.diff_pixels(s->block[6], ptr_cb + (dct_offset >> 1),\n dest_cb + (dct_offset >> 1), wrap_c);\n s->dsp.diff_pixels(s->block[7], ptr_cr + (dct_offset >> 1),\n dest_cr + (dct_offset >> 1), wrap_c);\n }\n }\n if (s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] <\n 2 * s->qscale * s->qscale) {\n if (s->dsp.sad[1](NULL, ptr_y , dest_y,\n wrap_y, 8) < 20 * s->qscale)\n skip_dct[0] = 1;\n if (s->dsp.sad[1](NULL, ptr_y + 8,\n dest_y + 8, wrap_y, 8) < 20 * s->qscale)\n skip_dct[1] = 1;\n if (s->dsp.sad[1](NULL, ptr_y + dct_offset,\n dest_y + dct_offset, wrap_y, 8) < 20 * s->qscale)\n skip_dct[2] = 1;\n if (s->dsp.sad[1](NULL, ptr_y + dct_offset + 8,\n dest_y + dct_offset + 8,\n wrap_y, 8) < 20 * s->qscale)\n skip_dct[3] = 1;\n if (s->dsp.sad[1](NULL, ptr_cb, dest_cb,\n wrap_c, 8) < 20 * s->qscale)\n skip_dct[4] = 1;\n if (s->dsp.sad[1](NULL, ptr_cr, dest_cr,\n wrap_c, 8) < 20 * s->qscale)\n skip_dct[5] = 1;\n if (!s->chroma_y_shift) {\n if (s->dsp.sad[1](NULL, ptr_cb + (dct_offset >> 1),\n dest_cb + (dct_offset >> 1),\n wrap_c, 8) < 20 * s->qscale)\n skip_dct[6] = 1;\n if (s->dsp.sad[1](NULL, ptr_cr + (dct_offset >> 1),\n dest_cr + (dct_offset >> 1),\n wrap_c, 8) < 20 * s->qscale)\n skip_dct[7] = 1;\n }\n }\n }\n if (s->quantizer_noise_shaping) {\n if (!skip_dct[0])\n get_visual_weight(weight[0], ptr_y , wrap_y);\n if (!skip_dct[1])\n get_visual_weight(weight[1], ptr_y + 8, wrap_y);\n if (!skip_dct[2])\n get_visual_weight(weight[2], ptr_y + dct_offset , wrap_y);\n if (!skip_dct[3])\n get_visual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);\n if (!skip_dct[4])\n get_visual_weight(weight[4], ptr_cb , wrap_c);\n if (!skip_dct[5])\n get_visual_weight(weight[5], ptr_cr , wrap_c);\n if (!s->chroma_y_shift) {\n if (!skip_dct[6])\n get_visual_weight(weight[6], ptr_cb + (dct_offset >> 1),\n wrap_c);\n if (!skip_dct[7])\n get_visual_weight(weight[7], ptr_cr + (dct_offset >> 1),\n wrap_c);\n }\n memcpy(orig[0], s->block[0], sizeof(DCTELEM) * 64 * mb_block_count);\n }\n assert(s->out_format != FMT_MJPEG || s->qscale == 8);\n {\n for (i = 0; i < mb_block_count; i++) {\n if (!skip_dct[i]) {\n int overflow;\n s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);\n if (overflow)\n clip_coeffs(s, s->block[i], s->block_last_index[i]);\n } else\n s->block_last_index[i] = -1;\n }\n if (s->quantizer_noise_shaping) {\n for (i = 0; i < mb_block_count; i++) {\n if (!skip_dct[i]) {\n s->block_last_index[i] =\n dct_quantize_refine(s, s->block[i], weight[i],\n orig[i], i, s->qscale);\n }\n }\n }\n if (s->luma_elim_threshold && !s->mb_intra)\n for (i = 0; i < 4; i++)\n dct_single_coeff_elimination(s, i, s->luma_elim_threshold);\n if (s->chroma_elim_threshold && !s->mb_intra)\n for (i = 4; i < mb_block_count; i++)\n dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);\n if (s->mpv_flags & FF_MPV_FLAG_CBP_RD) {\n for (i = 0; i < mb_block_count; i++) {\n if (s->block_last_index[i] == -1)\n s->coded_score[i] = INT_MAX / 256;\n }\n }\n }\n if ((s->flags & CODEC_FLAG_GRAY) && s->mb_intra) {\n s->block_last_index[4] =\n s->block_last_index[5] = 0;\n s->block[4][0] =\n s->block[5][0] = (1024 + s->c_dc_scale / 2) / s->c_dc_scale;\n }\n if (s->alternate_scan && s->dct_quantize != ff_dct_quantize_c) {\n for (i = 0; i < mb_block_count; i++) {\n int j;\n if (s->block_last_index[i] > 0) {\n for (j = 63; j > 0; j--) {\n if (s->block[i][s->intra_scantable.permutated[j]])\n break;\n }\n s->block_last_index[i] = j;\n }\n }\n }\n switch(s->codec_id){\n case AV_CODEC_ID_MPEG1VIDEO:\n case AV_CODEC_ID_MPEG2VIDEO:\n if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)\n ff_mpeg1_encode_mb(s, s->block, motion_x, motion_y);\n break;\n case AV_CODEC_ID_MPEG4:\n if (CONFIG_MPEG4_ENCODER)\n ff_mpeg4_encode_mb(s, s->block, motion_x, motion_y);\n break;\n case AV_CODEC_ID_MSMPEG4V2:\n case AV_CODEC_ID_MSMPEG4V3:\n case AV_CODEC_ID_WMV1:\n if (CONFIG_MSMPEG4_ENCODER)\n ff_msmpeg4_encode_mb(s, s->block, motion_x, motion_y);\n break;\n case AV_CODEC_ID_WMV2:\n if (CONFIG_WMV2_ENCODER)\n ff_wmv2_encode_mb(s, s->block, motion_x, motion_y);\n break;\n case AV_CODEC_ID_H261:\n if (CONFIG_H261_ENCODER)\n ff_h261_encode_mb(s, s->block, motion_x, motion_y);\n break;\n case AV_CODEC_ID_H263:\n case AV_CODEC_ID_H263P:\n case AV_CODEC_ID_FLV1:\n case AV_CODEC_ID_RV10:\n case AV_CODEC_ID_RV20:\n if (CONFIG_H263_ENCODER)\n ff_h263_encode_mb(s, s->block, motion_x, motion_y);\n break;\n case AV_CODEC_ID_MJPEG:\n if (CONFIG_MJPEG_ENCODER)\n ff_mjpeg_encode_mb(s, s->block);\n break;\n default:\n assert(0);\n }\n}', 'void ff_mpeg1_encode_mb(MpegEncContext *s, DCTELEM block[6][64], int motion_x, int motion_y)\n{\n if (s->chroma_format == CHROMA_420) mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 6);\n else mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 8);\n}', 'static av_always_inline void mpeg1_encode_mb_internal(MpegEncContext *s,\n DCTELEM block[6][64],\n int motion_x, int motion_y,\n int mb_block_count)\n{\n int i, cbp;\n const int mb_x = s->mb_x;\n const int mb_y = s->mb_y;\n const int first_mb= mb_x == s->resync_mb_x && mb_y == s->resync_mb_y;\n cbp = 0;\n for(i=0;i<mb_block_count;i++) {\n if (s->block_last_index[i] >= 0)\n cbp |= 1 << (mb_block_count - 1 - i);\n }\n if (cbp == 0 && !first_mb && s->mv_type == MV_TYPE_16X16 &&\n (mb_x != s->mb_width - 1 || (mb_y != s->mb_height - 1 && s->codec_id == AV_CODEC_ID_MPEG1VIDEO)) &&\n ((s->pict_type == AV_PICTURE_TYPE_P && (motion_x | motion_y) == 0) ||\n (s->pict_type == AV_PICTURE_TYPE_B && s->mv_dir == s->last_mv_dir && (((s->mv_dir & MV_DIR_FORWARD) ? ((s->mv[0][0][0] - s->last_mv[0][0][0])|(s->mv[0][0][1] - s->last_mv[0][0][1])) : 0) |\n ((s->mv_dir & MV_DIR_BACKWARD) ? ((s->mv[1][0][0] - s->last_mv[1][0][0])|(s->mv[1][0][1] - s->last_mv[1][0][1])) : 0)) == 0))) {\n s->mb_skip_run++;\n s->qscale -= s->dquant;\n s->skip_count++;\n s->misc_bits++;\n s->last_bits++;\n if(s->pict_type == AV_PICTURE_TYPE_P){\n s->last_mv[0][1][0]= s->last_mv[0][0][0]=\n s->last_mv[0][1][1]= s->last_mv[0][0][1]= 0;\n }\n } else {\n if(first_mb){\n assert(s->mb_skip_run == 0);\n encode_mb_skip_run(s, s->mb_x);\n }else{\n encode_mb_skip_run(s, s->mb_skip_run);\n }\n if (s->pict_type == AV_PICTURE_TYPE_I) {\n if(s->dquant && cbp){\n put_mb_modes(s, 2, 1, 0, 0);\n put_qscale(s);\n }else{\n put_mb_modes(s, 1, 1, 0, 0);\n s->qscale -= s->dquant;\n }\n s->misc_bits+= get_bits_diff(s);\n s->i_count++;\n } else if (s->mb_intra) {\n if(s->dquant && cbp){\n put_mb_modes(s, 6, 0x01, 0, 0);\n put_qscale(s);\n }else{\n put_mb_modes(s, 5, 0x03, 0, 0);\n s->qscale -= s->dquant;\n }\n s->misc_bits+= get_bits_diff(s);\n s->i_count++;\n memset(s->last_mv, 0, sizeof(s->last_mv));\n } else if (s->pict_type == AV_PICTURE_TYPE_P) {\n if(s->mv_type == MV_TYPE_16X16){\n if (cbp != 0) {\n if ((motion_x|motion_y) == 0) {\n if(s->dquant){\n put_mb_modes(s, 5, 1, 0, 0);\n put_qscale(s);\n }else{\n put_mb_modes(s, 2, 1, 0, 0);\n }\n s->misc_bits+= get_bits_diff(s);\n } else {\n if(s->dquant){\n put_mb_modes(s, 5, 2, 1, 0);\n put_qscale(s);\n }else{\n put_mb_modes(s, 1, 1, 1, 0);\n }\n s->misc_bits+= get_bits_diff(s);\n mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code);\n mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code);\n s->mv_bits+= get_bits_diff(s);\n }\n } else {\n put_bits(&s->pb, 3, 1);\n if (!s->frame_pred_frame_dct)\n put_bits(&s->pb, 2, 2);\n s->misc_bits+= get_bits_diff(s);\n mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code);\n mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code);\n s->qscale -= s->dquant;\n s->mv_bits+= get_bits_diff(s);\n }\n s->last_mv[0][1][0]= s->last_mv[0][0][0]= motion_x;\n s->last_mv[0][1][1]= s->last_mv[0][0][1]= motion_y;\n }else{\n assert(!s->frame_pred_frame_dct && s->mv_type == MV_TYPE_FIELD);\n if (cbp) {\n if(s->dquant){\n put_mb_modes(s, 5, 2, 1, 1);\n put_qscale(s);\n }else{\n put_mb_modes(s, 1, 1, 1, 1);\n }\n } else {\n put_bits(&s->pb, 3, 1);\n put_bits(&s->pb, 2, 1);\n s->qscale -= s->dquant;\n }\n s->misc_bits+= get_bits_diff(s);\n for(i=0; i<2; i++){\n put_bits(&s->pb, 1, s->field_select[0][i]);\n mpeg1_encode_motion(s, s->mv[0][i][0] - s->last_mv[0][i][0] , s->f_code);\n mpeg1_encode_motion(s, s->mv[0][i][1] - (s->last_mv[0][i][1]>>1), s->f_code);\n s->last_mv[0][i][0]= s->mv[0][i][0];\n s->last_mv[0][i][1]= 2*s->mv[0][i][1];\n }\n s->mv_bits+= get_bits_diff(s);\n }\n if(cbp) {\n if (s->chroma_y_shift) {\n put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp][1], ff_mpeg12_mbPatTable[cbp][0]);\n } else {\n put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp>>2][1], ff_mpeg12_mbPatTable[cbp>>2][0]);\n put_sbits(&s->pb, 2, cbp);\n }\n }\n s->f_count++;\n } else{\n if(s->mv_type == MV_TYPE_16X16){\n if (cbp){\n if (s->dquant) {\n if(s->mv_dir == MV_DIR_FORWARD)\n put_mb_modes(s, 6, 3, 1, 0);\n else\n put_mb_modes(s, 8-s->mv_dir, 2, 1, 0);\n put_qscale(s);\n } else {\n put_mb_modes(s, 5-s->mv_dir, 3, 1, 0);\n }\n }else{\n put_bits(&s->pb, 5-s->mv_dir, 2);\n if (!s->frame_pred_frame_dct)\n put_bits(&s->pb, 2, 2);\n s->qscale -= s->dquant;\n }\n s->misc_bits += get_bits_diff(s);\n if (s->mv_dir&MV_DIR_FORWARD){\n mpeg1_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code);\n mpeg1_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code);\n s->last_mv[0][0][0]=s->last_mv[0][1][0]= s->mv[0][0][0];\n s->last_mv[0][0][1]=s->last_mv[0][1][1]= s->mv[0][0][1];\n s->f_count++;\n }\n if (s->mv_dir&MV_DIR_BACKWARD){\n mpeg1_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code);\n mpeg1_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code);\n s->last_mv[1][0][0]=s->last_mv[1][1][0]= s->mv[1][0][0];\n s->last_mv[1][0][1]=s->last_mv[1][1][1]= s->mv[1][0][1];\n s->b_count++;\n }\n }else{\n assert(s->mv_type == MV_TYPE_FIELD);\n assert(!s->frame_pred_frame_dct);\n if (cbp){\n if (s->dquant) {\n if(s->mv_dir == MV_DIR_FORWARD)\n put_mb_modes(s, 6, 3, 1, 1);\n else\n put_mb_modes(s, 8-s->mv_dir, 2, 1, 1);\n put_qscale(s);\n } else {\n put_mb_modes(s, 5-s->mv_dir, 3, 1, 1);\n }\n }else{\n put_bits(&s->pb, 5-s->mv_dir, 2);\n put_bits(&s->pb, 2, 1);\n s->qscale -= s->dquant;\n }\n s->misc_bits += get_bits_diff(s);\n if (s->mv_dir&MV_DIR_FORWARD){\n for(i=0; i<2; i++){\n put_bits(&s->pb, 1, s->field_select[0][i]);\n mpeg1_encode_motion(s, s->mv[0][i][0] - s->last_mv[0][i][0] , s->f_code);\n mpeg1_encode_motion(s, s->mv[0][i][1] - (s->last_mv[0][i][1]>>1), s->f_code);\n s->last_mv[0][i][0]= s->mv[0][i][0];\n s->last_mv[0][i][1]= 2*s->mv[0][i][1];\n }\n s->f_count++;\n }\n if (s->mv_dir&MV_DIR_BACKWARD){\n for(i=0; i<2; i++){\n put_bits(&s->pb, 1, s->field_select[1][i]);\n mpeg1_encode_motion(s, s->mv[1][i][0] - s->last_mv[1][i][0] , s->b_code);\n mpeg1_encode_motion(s, s->mv[1][i][1] - (s->last_mv[1][i][1]>>1), s->b_code);\n s->last_mv[1][i][0]= s->mv[1][i][0];\n s->last_mv[1][i][1]= 2*s->mv[1][i][1];\n }\n s->b_count++;\n }\n }\n s->mv_bits += get_bits_diff(s);\n if(cbp) {\n if (s->chroma_y_shift) {\n put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp][1], ff_mpeg12_mbPatTable[cbp][0]);\n } else {\n put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp>>2][1], ff_mpeg12_mbPatTable[cbp>>2][0]);\n put_sbits(&s->pb, 2, cbp);\n }\n }\n }\n for(i=0;i<mb_block_count;i++) {\n if (cbp & (1 << (mb_block_count - 1 - i))) {\n mpeg1_encode_block(s, block[i], i);\n }\n }\n s->mb_skip_run = 0;\n if(s->mb_intra)\n s->i_tex_bits+= get_bits_diff(s);\n else\n s->p_tex_bits+= get_bits_diff(s);\n }\n}', 'static av_always_inline void put_qscale(MpegEncContext *s)\n{\n if(s->q_scale_type){\n assert(s->qscale>=1 && s->qscale <=12);\n put_bits(&s->pb, 5, inv_non_linear_qscale[s->qscale]);\n }else{\n put_bits(&s->pb, 5, s->qscale);\n }\n}']
36,491
0
https://github.com/nginx/nginx/blob/e4ecddfdb0d2ffc872658e36028971ad9a873726/src/core/ngx_string.c/#L244
u_char * ngx_vsnprintf(u_char *buf, size_t max, const char *fmt, va_list args) { u_char *p, zero, *last; int d; float f, scale; size_t len, slen; int64_t i64; uint64_t ui64; ngx_msec_t ms; ngx_uint_t width, sign, hex, max_width, frac_width, i; ngx_str_t *v; ngx_variable_value_t *vv; if (max == 0) { return buf; } last = buf + max; while (*fmt && buf < last) { if (*fmt == '%') { i64 = 0; ui64 = 0; zero = (u_char) ((*++fmt == '0') ? '0' : ' '); width = 0; sign = 1; hex = 0; max_width = 0; frac_width = 0; slen = (size_t) -1; while (*fmt >= '0' && *fmt <= '9') { width = width * 10 + *fmt++ - '0'; } for ( ;; ) { switch (*fmt) { case 'u': sign = 0; fmt++; continue; case 'm': max_width = 1; fmt++; continue; case 'X': hex = 2; sign = 0; fmt++; continue; case 'x': hex = 1; sign = 0; fmt++; continue; case '.': fmt++; while (*fmt >= '0' && *fmt <= '9') { frac_width = frac_width * 10 + *fmt++ - '0'; } break; case '*': slen = va_arg(args, size_t); fmt++; continue; default: break; } break; } switch (*fmt) { case 'V': v = va_arg(args, ngx_str_t *); len = v->len; len = (buf + len < last) ? len : (size_t) (last - buf); buf = ngx_cpymem(buf, v->data, len); fmt++; continue; case 'v': vv = va_arg(args, ngx_variable_value_t *); len = vv->len; len = (buf + len < last) ? len : (size_t) (last - buf); buf = ngx_cpymem(buf, vv->data, len); fmt++; continue; case 's': p = va_arg(args, u_char *); if (slen == (size_t) -1) { while (*p && buf < last) { *buf++ = *p++; } } else { len = (buf + slen < last) ? slen : (size_t) (last - buf); buf = ngx_cpymem(buf, p, len); } fmt++; continue; case 'O': i64 = (int64_t) va_arg(args, off_t); sign = 1; break; case 'P': i64 = (int64_t) va_arg(args, ngx_pid_t); sign = 1; break; case 'T': i64 = (int64_t) va_arg(args, time_t); sign = 1; break; case 'M': ms = (ngx_msec_t) va_arg(args, ngx_msec_t); if ((ngx_msec_int_t) ms == -1) { sign = 1; i64 = -1; } else { sign = 0; ui64 = (uint64_t) ms; } break; case 'z': if (sign) { i64 = (int64_t) va_arg(args, ssize_t); } else { ui64 = (uint64_t) va_arg(args, size_t); } break; case 'i': if (sign) { i64 = (int64_t) va_arg(args, ngx_int_t); } else { ui64 = (uint64_t) va_arg(args, ngx_uint_t); } if (max_width) { width = NGX_INT_T_LEN; } break; case 'd': if (sign) { i64 = (int64_t) va_arg(args, int); } else { ui64 = (uint64_t) va_arg(args, u_int); } break; case 'l': if (sign) { i64 = (int64_t) va_arg(args, long); } else { ui64 = (uint64_t) va_arg(args, u_long); } break; case 'D': if (sign) { i64 = (int64_t) va_arg(args, int32_t); } else { ui64 = (uint64_t) va_arg(args, uint32_t); } break; case 'L': if (sign) { i64 = va_arg(args, int64_t); } else { ui64 = va_arg(args, uint64_t); } break; case 'A': if (sign) { i64 = (int64_t) va_arg(args, ngx_atomic_int_t); } else { ui64 = (uint64_t) va_arg(args, ngx_atomic_uint_t); } if (max_width) { width = NGX_ATOMIC_T_LEN; } break; case 'f': f = (float) va_arg(args, double); if (f < 0) { *buf++ = '-'; f = -f; } ui64 = (int64_t) f; buf = ngx_sprintf_num(buf, last, ui64, zero, 0, width); if (frac_width) { if (buf < last) { *buf++ = '.'; } scale = 1.0; for (i = 0; i < frac_width; i++) { scale *= 10.0; } ui64 = (uint64_t) ((f - (int64_t) ui64) * scale); buf = ngx_sprintf_num(buf, last, ui64, '0', 0, frac_width); } fmt++; continue; #if !(NGX_WIN32) case 'r': i64 = (int64_t) va_arg(args, rlim_t); sign = 1; break; #endif case 'p': ui64 = (uintptr_t) va_arg(args, void *); hex = 2; sign = 0; zero = '0'; width = NGX_PTR_SIZE * 2; break; case 'c': d = va_arg(args, int); *buf++ = (u_char) (d & 0xff); fmt++; continue; case 'Z': *buf++ = '\0'; fmt++; continue; case 'N': #if (NGX_WIN32) *buf++ = CR; #endif *buf++ = LF; fmt++; continue; case '%': *buf++ = '%'; fmt++; continue; default: *buf++ = *fmt++; continue; } if (sign) { if (i64 < 0) { *buf++ = '-'; ui64 = (uint64_t) -i64; } else { ui64 = (uint64_t) i64; } } buf = ngx_sprintf_num(buf, last, ui64, zero, hex, width); fmt++; } else { *buf++ = *fmt++; } } return buf; }
['static ngx_int_t\nngx_http_gzip_filter_deflate(ngx_http_request_t *r, ngx_http_gzip_ctx_t *ctx)\n{\n int rc;\n ngx_chain_t *cl;\n ngx_http_gzip_conf_t *conf;\n ngx_log_debug6(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "deflate in: ni:%p no:%p ai:%ud ao:%ud fl:%d redo:%d",\n ctx->zstream.next_in, ctx->zstream.next_out,\n ctx->zstream.avail_in, ctx->zstream.avail_out,\n ctx->flush, ctx->redo);\n rc = deflate(&ctx->zstream, ctx->flush);\n if (rc != Z_OK && rc != Z_STREAM_END) {\n ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,\n "deflate() failed: %d, %d", ctx->flush, rc);\n return NGX_ERROR;\n }\n ngx_log_debug5(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "deflate out: ni:%p no:%p ai:%ud ao:%ud rc:%d",\n ctx->zstream.next_in, ctx->zstream.next_out,\n ctx->zstream.avail_in, ctx->zstream.avail_out,\n rc);\n ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "gzip in_buf:%p pos:%p",\n ctx->in_buf, ctx->in_buf->pos);\n if (ctx->zstream.next_in) {\n ctx->in_buf->pos = ctx->zstream.next_in;\n if (ctx->zstream.avail_in == 0) {\n ctx->zstream.next_in = NULL;\n }\n }\n ctx->out_buf->last = ctx->zstream.next_out;\n if (ctx->zstream.avail_out == 0) {\n cl = ngx_alloc_chain_link(r->pool);\n if (cl == NULL) {\n return NGX_ERROR;\n }\n cl->buf = ctx->out_buf;\n cl->next = NULL;\n *ctx->last_out = cl;\n ctx->last_out = &cl->next;\n ctx->redo = 1;\n return NGX_AGAIN;\n }\n ctx->redo = 0;\n if (ctx->flush == Z_SYNC_FLUSH) {\n ctx->zstream.avail_out = 0;\n ctx->out_buf->flush = 1;\n ctx->flush = Z_NO_FLUSH;\n cl = ngx_alloc_chain_link(r->pool);\n if (cl == NULL) {\n return NGX_ERROR;\n }\n cl->buf = ctx->out_buf;\n cl->next = NULL;\n *ctx->last_out = cl;\n ctx->last_out = &cl->next;\n return NGX_OK;\n }\n if (rc == Z_STREAM_END) {\n if (ngx_http_gzip_filter_deflate_end(r, ctx) != NGX_OK) {\n return NGX_ERROR;\n }\n return NGX_OK;\n }\n conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);\n if (conf->no_buffer && ctx->in == NULL) {\n cl = ngx_alloc_chain_link(r->pool);\n if (cl == NULL) {\n return NGX_ERROR;\n }\n cl->buf = ctx->out_buf;\n cl->next = NULL;\n *ctx->last_out = cl;\n ctx->last_out = &cl->next;\n return NGX_OK;\n }\n return NGX_AGAIN;\n}', 'void\nngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,\n const char *fmt, ...)\n#else\nvoid\nngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,\n const char *fmt, va_list args)\n#endif\n{\n#if (NGX_HAVE_VARIADIC_MACROS)\n va_list args;\n#endif\n u_char errstr[NGX_MAX_ERROR_STR], *p, *last;\n if (log->file->fd == NGX_INVALID_FILE) {\n return;\n }\n last = errstr + NGX_MAX_ERROR_STR;\n ngx_memcpy(errstr, ngx_cached_err_log_time.data,\n ngx_cached_err_log_time.len);\n p = errstr + ngx_cached_err_log_time.len;\n p = ngx_snprintf(p, last - p, " [%s] ", err_levels[level]);\n p = ngx_snprintf(p, last - p, "%P#" NGX_TID_T_FMT ": ",\n ngx_log_pid, ngx_log_tid);\n if (log->connection) {\n p = ngx_snprintf(p, last - p, "*%uA ", log->connection);\n }\n#if (NGX_HAVE_VARIADIC_MACROS)\n va_start(args, fmt);\n p = ngx_vsnprintf(p, last - p, fmt, args);\n va_end(args);\n#else\n p = ngx_vsnprintf(p, last - p, fmt, args);\n#endif\n if (err) {\n if (p > last - 50) {\n p = last - 50;\n *p++ = \'.\';\n *p++ = \'.\';\n *p++ = \'.\';\n }\n#if (NGX_WIN32)\n p = ngx_snprintf(p, last - p, ((unsigned) err < 0x80000000)\n ? " (%d: " : " (%Xd: ", err);\n#else\n p = ngx_snprintf(p, last - p, " (%d: ", err);\n#endif\n p = ngx_strerror_r(err, p, last - p);\n if (p < last) {\n *p++ = \')\';\n }\n }\n if (level != NGX_LOG_DEBUG && log->handler) {\n p = log->handler(log, p, last - p);\n }\n if (p > last - NGX_LINEFEED_SIZE) {\n p = last - NGX_LINEFEED_SIZE;\n }\n ngx_linefeed(p);\n (void) ngx_write_fd(log->file->fd, errstr, p - errstr);\n}', 'u_char * ngx_cdecl\nngx_snprintf(u_char *buf, size_t max, const char *fmt, ...)\n{\n u_char *p;\n va_list args;\n va_start(args, fmt);\n p = ngx_vsnprintf(buf, max, fmt, args);\n va_end(args);\n return p;\n}', "u_char *\nngx_vsnprintf(u_char *buf, size_t max, const char *fmt, va_list args)\n{\n u_char *p, zero, *last;\n int d;\n float f, scale;\n size_t len, slen;\n int64_t i64;\n uint64_t ui64;\n ngx_msec_t ms;\n ngx_uint_t width, sign, hex, max_width, frac_width, i;\n ngx_str_t *v;\n ngx_variable_value_t *vv;\n if (max == 0) {\n return buf;\n }\n last = buf + max;\n while (*fmt && buf < last) {\n if (*fmt == '%') {\n i64 = 0;\n ui64 = 0;\n zero = (u_char) ((*++fmt == '0') ? '0' : ' ');\n width = 0;\n sign = 1;\n hex = 0;\n max_width = 0;\n frac_width = 0;\n slen = (size_t) -1;\n while (*fmt >= '0' && *fmt <= '9') {\n width = width * 10 + *fmt++ - '0';\n }\n for ( ;; ) {\n switch (*fmt) {\n case 'u':\n sign = 0;\n fmt++;\n continue;\n case 'm':\n max_width = 1;\n fmt++;\n continue;\n case 'X':\n hex = 2;\n sign = 0;\n fmt++;\n continue;\n case 'x':\n hex = 1;\n sign = 0;\n fmt++;\n continue;\n case '.':\n fmt++;\n while (*fmt >= '0' && *fmt <= '9') {\n frac_width = frac_width * 10 + *fmt++ - '0';\n }\n break;\n case '*':\n slen = va_arg(args, size_t);\n fmt++;\n continue;\n default:\n break;\n }\n break;\n }\n switch (*fmt) {\n case 'V':\n v = va_arg(args, ngx_str_t *);\n len = v->len;\n len = (buf + len < last) ? len : (size_t) (last - buf);\n buf = ngx_cpymem(buf, v->data, len);\n fmt++;\n continue;\n case 'v':\n vv = va_arg(args, ngx_variable_value_t *);\n len = vv->len;\n len = (buf + len < last) ? len : (size_t) (last - buf);\n buf = ngx_cpymem(buf, vv->data, len);\n fmt++;\n continue;\n case 's':\n p = va_arg(args, u_char *);\n if (slen == (size_t) -1) {\n while (*p && buf < last) {\n *buf++ = *p++;\n }\n } else {\n len = (buf + slen < last) ? slen : (size_t) (last - buf);\n buf = ngx_cpymem(buf, p, len);\n }\n fmt++;\n continue;\n case 'O':\n i64 = (int64_t) va_arg(args, off_t);\n sign = 1;\n break;\n case 'P':\n i64 = (int64_t) va_arg(args, ngx_pid_t);\n sign = 1;\n break;\n case 'T':\n i64 = (int64_t) va_arg(args, time_t);\n sign = 1;\n break;\n case 'M':\n ms = (ngx_msec_t) va_arg(args, ngx_msec_t);\n if ((ngx_msec_int_t) ms == -1) {\n sign = 1;\n i64 = -1;\n } else {\n sign = 0;\n ui64 = (uint64_t) ms;\n }\n break;\n case 'z':\n if (sign) {\n i64 = (int64_t) va_arg(args, ssize_t);\n } else {\n ui64 = (uint64_t) va_arg(args, size_t);\n }\n break;\n case 'i':\n if (sign) {\n i64 = (int64_t) va_arg(args, ngx_int_t);\n } else {\n ui64 = (uint64_t) va_arg(args, ngx_uint_t);\n }\n if (max_width) {\n width = NGX_INT_T_LEN;\n }\n break;\n case 'd':\n if (sign) {\n i64 = (int64_t) va_arg(args, int);\n } else {\n ui64 = (uint64_t) va_arg(args, u_int);\n }\n break;\n case 'l':\n if (sign) {\n i64 = (int64_t) va_arg(args, long);\n } else {\n ui64 = (uint64_t) va_arg(args, u_long);\n }\n break;\n case 'D':\n if (sign) {\n i64 = (int64_t) va_arg(args, int32_t);\n } else {\n ui64 = (uint64_t) va_arg(args, uint32_t);\n }\n break;\n case 'L':\n if (sign) {\n i64 = va_arg(args, int64_t);\n } else {\n ui64 = va_arg(args, uint64_t);\n }\n break;\n case 'A':\n if (sign) {\n i64 = (int64_t) va_arg(args, ngx_atomic_int_t);\n } else {\n ui64 = (uint64_t) va_arg(args, ngx_atomic_uint_t);\n }\n if (max_width) {\n width = NGX_ATOMIC_T_LEN;\n }\n break;\n case 'f':\n f = (float) va_arg(args, double);\n if (f < 0) {\n *buf++ = '-';\n f = -f;\n }\n ui64 = (int64_t) f;\n buf = ngx_sprintf_num(buf, last, ui64, zero, 0, width);\n if (frac_width) {\n if (buf < last) {\n *buf++ = '.';\n }\n scale = 1.0;\n for (i = 0; i < frac_width; i++) {\n scale *= 10.0;\n }\n ui64 = (uint64_t) ((f - (int64_t) ui64) * scale);\n buf = ngx_sprintf_num(buf, last, ui64, '0', 0, frac_width);\n }\n fmt++;\n continue;\n#if !(NGX_WIN32)\n case 'r':\n i64 = (int64_t) va_arg(args, rlim_t);\n sign = 1;\n break;\n#endif\n case 'p':\n ui64 = (uintptr_t) va_arg(args, void *);\n hex = 2;\n sign = 0;\n zero = '0';\n width = NGX_PTR_SIZE * 2;\n break;\n case 'c':\n d = va_arg(args, int);\n *buf++ = (u_char) (d & 0xff);\n fmt++;\n continue;\n case 'Z':\n *buf++ = '\\0';\n fmt++;\n continue;\n case 'N':\n#if (NGX_WIN32)\n *buf++ = CR;\n#endif\n *buf++ = LF;\n fmt++;\n continue;\n case '%':\n *buf++ = '%';\n fmt++;\n continue;\n default:\n *buf++ = *fmt++;\n continue;\n }\n if (sign) {\n if (i64 < 0) {\n *buf++ = '-';\n ui64 = (uint64_t) -i64;\n } else {\n ui64 = (uint64_t) i64;\n }\n }\n buf = ngx_sprintf_num(buf, last, ui64, zero, hex, width);\n fmt++;\n } else {\n *buf++ = *fmt++;\n }\n }\n return buf;\n}"]
36,492
0
https://github.com/openssl/openssl/blob/ec04e866343d40a1e3e8e5db79557e279a2dd0d8/crypto/bn/bn_depr.c/#L91
BIGNUM *BN_generate_prime(BIGNUM *ret, int bits, int safe, const BIGNUM *add, const BIGNUM *rem, void (*callback) (int, int, void *), void *cb_arg) { BN_GENCB cb; BIGNUM *rnd = NULL; BN_GENCB_set_old(&cb, callback, cb_arg); if (ret == NULL) { if ((rnd = BN_new()) == NULL) goto err; } else rnd = ret; if (!BN_generate_prime_ex(rnd, bits, safe, add, rem, &cb)) goto err; return ret; err: BN_free(rnd); return NULL; }
['BIGNUM *BN_generate_prime(BIGNUM *ret, int bits, int safe,\n const BIGNUM *add, const BIGNUM *rem,\n void (*callback) (int, int, void *), void *cb_arg)\n{\n BN_GENCB cb;\n BIGNUM *rnd = NULL;\n BN_GENCB_set_old(&cb, callback, cb_arg);\n if (ret == NULL) {\n if ((rnd = BN_new()) == NULL)\n goto err;\n } else\n rnd = ret;\n if (!BN_generate_prime_ex(rnd, bits, safe, add, rem, &cb))\n goto err;\n return ret;\n err:\n BN_free(rnd);\n return NULL;\n}', 'void BN_GENCB_set_old(BN_GENCB *gencb, void (*callback) (int, int, void *),\n void *cb_arg)\n{\n BN_GENCB *tmp_gencb = gencb;\n tmp_gencb->ver = 1;\n tmp_gencb->arg = cb_arg;\n tmp_gencb->cb.cb_1 = callback;\n}', 'BIGNUM *BN_new(void)\n{\n BIGNUM *ret;\n if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {\n BNerr(BN_F_BN_NEW, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n ret->flags = BN_FLG_MALLOCED;\n bn_check_top(ret);\n return (ret);\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (num <= 0)\n return NULL;\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)file;\n (void)line;\n ret = malloc(num);\n#endif\n#ifndef OPENSSL_CPUID_OBJ\n if (ret && (num > 2048)) {\n extern unsigned char cleanse_ctr;\n ((unsigned char *)ret)[0] = cleanse_ctr;\n }\n#endif\n return ret;\n}', 'void BN_free(BIGNUM *a)\n{\n if (a == NULL)\n return;\n bn_check_top(a);\n if (!BN_get_flags(a, BN_FLG_STATIC_DATA))\n bn_free_d(a);\n if (a->flags & BN_FLG_MALLOCED)\n OPENSSL_free(a);\n else {\n#if OPENSSL_API_COMPAT < 0x00908000L\n a->flags |= BN_FLG_FREE;\n#endif\n a->d = NULL;\n }\n}', 'int BN_get_flags(const BIGNUM *b, int n)\n{\n return b->flags & n;\n}']
36,493
1
https://github.com/libav/libav/blob/1a5fdf9519d7f4b9a1bdda623516c37744f569bd/libavcodec/alsdec.c/#L1255
static int revert_channel_correlation(ALSDecContext *ctx, ALSBlockData *bd, ALSChannelData **cd, int *reverted, unsigned int offset, int c) { ALSChannelData *ch = cd[c]; unsigned int dep = 0; unsigned int channels = ctx->avctx->channels; if (reverted[c]) return 0; reverted[c] = 1; while (dep < channels && !ch[dep].stop_flag) { revert_channel_correlation(ctx, bd, cd, reverted, offset, ch[dep].master_channel); dep++; } if (dep == channels) { av_log(ctx->avctx, AV_LOG_WARNING, "Invalid channel correlation!\n"); return AVERROR_INVALIDDATA; } bd->const_block = ctx->const_block + c; bd->shift_lsbs = ctx->shift_lsbs + c; bd->opt_order = ctx->opt_order + c; bd->store_prev_samples = ctx->store_prev_samples + c; bd->use_ltp = ctx->use_ltp + c; bd->ltp_lag = ctx->ltp_lag + c; bd->ltp_gain = ctx->ltp_gain[c]; bd->lpc_cof = ctx->lpc_cof[c]; bd->quant_cof = ctx->quant_cof[c]; bd->raw_samples = ctx->raw_samples[c] + offset; dep = 0; while (!ch[dep].stop_flag) { unsigned int smp; unsigned int begin = 1; unsigned int end = bd->block_length - 1; int64_t y; int32_t *master = ctx->raw_samples[ch[dep].master_channel] + offset; if (ch[dep].time_diff_flag) { int t = ch[dep].time_diff_index; if (ch[dep].time_diff_sign) { t = -t; begin -= t; } else { end -= t; } for (smp = begin; smp < end; smp++) { y = (1 << 6) + MUL64(ch[dep].weighting[0], master[smp - 1 ]) + MUL64(ch[dep].weighting[1], master[smp ]) + MUL64(ch[dep].weighting[2], master[smp + 1 ]) + MUL64(ch[dep].weighting[3], master[smp - 1 + t]) + MUL64(ch[dep].weighting[4], master[smp + t]) + MUL64(ch[dep].weighting[5], master[smp + 1 + t]); bd->raw_samples[smp] += y >> 7; } } else { for (smp = begin; smp < end; smp++) { y = (1 << 6) + MUL64(ch[dep].weighting[0], master[smp - 1]) + MUL64(ch[dep].weighting[1], master[smp ]) + MUL64(ch[dep].weighting[2], master[smp + 1]); bd->raw_samples[smp] += y >> 7; } } dep++; } return 0; }
['static int read_frame_data(ALSDecContext *ctx, unsigned int ra_frame)\n{\n ALSSpecificConfig *sconf = &ctx->sconf;\n AVCodecContext *avctx = ctx->avctx;\n GetBitContext *gb = &ctx->gb;\n unsigned int div_blocks[32];\n unsigned int c;\n unsigned int js_blocks[2];\n uint32_t bs_info = 0;\n int ret;\n if (sconf->ra_flag == RA_FLAG_FRAMES && ra_frame)\n skip_bits_long(gb, 32);\n if (sconf->mc_coding && sconf->joint_stereo) {\n ctx->js_switch = get_bits1(gb);\n align_get_bits(gb);\n }\n if (!sconf->mc_coding || ctx->js_switch) {\n int independent_bs = !sconf->joint_stereo;\n for (c = 0; c < avctx->channels; c++) {\n js_blocks[0] = 0;\n js_blocks[1] = 0;\n get_block_sizes(ctx, div_blocks, &bs_info);\n if (sconf->joint_stereo && sconf->block_switching)\n if (bs_info >> 31)\n independent_bs = 2;\n if (c == avctx->channels - 1)\n independent_bs = 1;\n if (independent_bs) {\n ret = decode_blocks_ind(ctx, ra_frame, c,\n div_blocks, js_blocks);\n if (ret < 0)\n return ret;\n independent_bs--;\n } else {\n ret = decode_blocks(ctx, ra_frame, c, div_blocks, js_blocks);\n if (ret < 0)\n return ret;\n c++;\n }\n memmove(ctx->raw_samples[c] - sconf->max_order,\n ctx->raw_samples[c] - sconf->max_order + sconf->frame_length,\n sizeof(*ctx->raw_samples[c]) * sconf->max_order);\n }\n } else {\n ALSBlockData bd = { 0 };\n int b, ret;\n int *reverted_channels = ctx->reverted_channels;\n unsigned int offset = 0;\n for (c = 0; c < avctx->channels; c++)\n if (ctx->chan_data[c] < ctx->chan_data_buffer) {\n av_log(ctx->avctx, AV_LOG_ERROR, "Invalid channel data!\\n");\n return AVERROR_INVALIDDATA;\n }\n memset(reverted_channels, 0, sizeof(*reverted_channels) * avctx->channels);\n bd.ra_block = ra_frame;\n bd.prev_raw_samples = ctx->prev_raw_samples;\n get_block_sizes(ctx, div_blocks, &bs_info);\n for (b = 0; b < ctx->num_blocks; b++) {\n bd.block_length = div_blocks[b];\n for (c = 0; c < avctx->channels; c++) {\n bd.const_block = ctx->const_block + c;\n bd.shift_lsbs = ctx->shift_lsbs + c;\n bd.opt_order = ctx->opt_order + c;\n bd.store_prev_samples = ctx->store_prev_samples + c;\n bd.use_ltp = ctx->use_ltp + c;\n bd.ltp_lag = ctx->ltp_lag + c;\n bd.ltp_gain = ctx->ltp_gain[c];\n bd.lpc_cof = ctx->lpc_cof[c];\n bd.quant_cof = ctx->quant_cof[c];\n bd.raw_samples = ctx->raw_samples[c] + offset;\n bd.raw_other = NULL;\n if ((ret = read_block(ctx, &bd)) < 0)\n return ret;\n if ((ret = read_channel_data(ctx, ctx->chan_data[c], c)) < 0)\n return ret;\n }\n for (c = 0; c < avctx->channels; c++) {\n ret = revert_channel_correlation(ctx, &bd, ctx->chan_data,\n reverted_channels, offset, c);\n if (ret < 0)\n return ret;\n }\n for (c = 0; c < avctx->channels; c++) {\n bd.const_block = ctx->const_block + c;\n bd.shift_lsbs = ctx->shift_lsbs + c;\n bd.opt_order = ctx->opt_order + c;\n bd.store_prev_samples = ctx->store_prev_samples + c;\n bd.use_ltp = ctx->use_ltp + c;\n bd.ltp_lag = ctx->ltp_lag + c;\n bd.ltp_gain = ctx->ltp_gain[c];\n bd.lpc_cof = ctx->lpc_cof[c];\n bd.quant_cof = ctx->quant_cof[c];\n bd.raw_samples = ctx->raw_samples[c] + offset;\n if ((ret = decode_block(ctx, &bd)) < 0)\n return ret;\n }\n memset(reverted_channels, 0, avctx->channels * sizeof(*reverted_channels));\n offset += div_blocks[b];\n bd.ra_block = 0;\n }\n for (c = 0; c < avctx->channels; c++)\n memmove(ctx->raw_samples[c] - sconf->max_order,\n ctx->raw_samples[c] - sconf->max_order + sconf->frame_length,\n sizeof(*ctx->raw_samples[c]) * sconf->max_order);\n }\n return 0;\n}', 'static void get_block_sizes(ALSDecContext *ctx, unsigned int *div_blocks,\n uint32_t *bs_info)\n{\n ALSSpecificConfig *sconf = &ctx->sconf;\n GetBitContext *gb = &ctx->gb;\n unsigned int *ptr_div_blocks = div_blocks;\n unsigned int b;\n if (sconf->block_switching) {\n unsigned int bs_info_len = 1 << (sconf->block_switching + 2);\n *bs_info = get_bits_long(gb, bs_info_len);\n *bs_info <<= (32 - bs_info_len);\n }\n ctx->num_blocks = 0;\n parse_bs_info(*bs_info, 0, 0, &ptr_div_blocks, &ctx->num_blocks);\n for (b = 0; b < ctx->num_blocks; b++)\n div_blocks[b] = ctx->sconf.frame_length >> div_blocks[b];\n if (ctx->cur_frame_length != ctx->sconf.frame_length) {\n unsigned int remaining = ctx->cur_frame_length;\n for (b = 0; b < ctx->num_blocks; b++) {\n if (remaining <= div_blocks[b]) {\n div_blocks[b] = remaining;\n ctx->num_blocks = b + 1;\n break;\n }\n remaining -= div_blocks[b];\n }\n }\n}', 'static int revert_channel_correlation(ALSDecContext *ctx, ALSBlockData *bd,\n ALSChannelData **cd, int *reverted,\n unsigned int offset, int c)\n{\n ALSChannelData *ch = cd[c];\n unsigned int dep = 0;\n unsigned int channels = ctx->avctx->channels;\n if (reverted[c])\n return 0;\n reverted[c] = 1;\n while (dep < channels && !ch[dep].stop_flag) {\n revert_channel_correlation(ctx, bd, cd, reverted, offset,\n ch[dep].master_channel);\n dep++;\n }\n if (dep == channels) {\n av_log(ctx->avctx, AV_LOG_WARNING, "Invalid channel correlation!\\n");\n return AVERROR_INVALIDDATA;\n }\n bd->const_block = ctx->const_block + c;\n bd->shift_lsbs = ctx->shift_lsbs + c;\n bd->opt_order = ctx->opt_order + c;\n bd->store_prev_samples = ctx->store_prev_samples + c;\n bd->use_ltp = ctx->use_ltp + c;\n bd->ltp_lag = ctx->ltp_lag + c;\n bd->ltp_gain = ctx->ltp_gain[c];\n bd->lpc_cof = ctx->lpc_cof[c];\n bd->quant_cof = ctx->quant_cof[c];\n bd->raw_samples = ctx->raw_samples[c] + offset;\n dep = 0;\n while (!ch[dep].stop_flag) {\n unsigned int smp;\n unsigned int begin = 1;\n unsigned int end = bd->block_length - 1;\n int64_t y;\n int32_t *master = ctx->raw_samples[ch[dep].master_channel] + offset;\n if (ch[dep].time_diff_flag) {\n int t = ch[dep].time_diff_index;\n if (ch[dep].time_diff_sign) {\n t = -t;\n begin -= t;\n } else {\n end -= t;\n }\n for (smp = begin; smp < end; smp++) {\n y = (1 << 6) +\n MUL64(ch[dep].weighting[0], master[smp - 1 ]) +\n MUL64(ch[dep].weighting[1], master[smp ]) +\n MUL64(ch[dep].weighting[2], master[smp + 1 ]) +\n MUL64(ch[dep].weighting[3], master[smp - 1 + t]) +\n MUL64(ch[dep].weighting[4], master[smp + t]) +\n MUL64(ch[dep].weighting[5], master[smp + 1 + t]);\n bd->raw_samples[smp] += y >> 7;\n }\n } else {\n for (smp = begin; smp < end; smp++) {\n y = (1 << 6) +\n MUL64(ch[dep].weighting[0], master[smp - 1]) +\n MUL64(ch[dep].weighting[1], master[smp ]) +\n MUL64(ch[dep].weighting[2], master[smp + 1]);\n bd->raw_samples[smp] += y >> 7;\n }\n }\n dep++;\n }\n return 0;\n}']
36,494
0
https://github.com/openssl/openssl/blob/7ef8206859f9a52f48e817c023c744fe00e82c5d/crypto/bio/bss_file.c/#L112
BIO *BIO_new_file(const char *filename, const char *mode) { BIO *ret; FILE *file; if ((file=fopen(filename,mode)) == NULL) { SYSerr(SYS_F_FOPEN,get_last_sys_error()); ERR_add_error_data(5,"fopen('",filename,"','",mode,"')"); BIOerr(BIO_F_BIO_NEW_FILE,ERR_R_SYS_LIB); return(NULL); } if ((ret=BIO_new(BIO_s_file_internal())) == NULL) return(NULL); BIO_set_fp(ret,file,BIO_CLOSE); return(ret); }
['BIO *BIO_new_file(const char *filename, const char *mode)\n\t{\n\tBIO *ret;\n\tFILE *file;\n\tif ((file=fopen(filename,mode)) == NULL)\n\t\t{\n\t\tSYSerr(SYS_F_FOPEN,get_last_sys_error());\n\t\tERR_add_error_data(5,"fopen(\'",filename,"\',\'",mode,"\')");\n\t\tBIOerr(BIO_F_BIO_NEW_FILE,ERR_R_SYS_LIB);\n\t\treturn(NULL);\n\t\t}\n\tif ((ret=BIO_new(BIO_s_file_internal())) == NULL)\n\t\treturn(NULL);\n\tBIO_set_fp(ret,file,BIO_CLOSE);\n\treturn(ret);\n\t}', 'BIO_METHOD *BIO_s_file(void)\n\t{\n\treturn(&methods_filep);\n\t}', 'BIO *BIO_new(BIO_METHOD *method)\n\t{\n\tBIO *ret=NULL;\n\tret=(BIO *)OPENSSL_malloc(sizeof(BIO));\n\tif (ret == NULL)\n\t\t{\n\t\tBIOerr(BIO_F_BIO_NEW,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tif (!BIO_set(ret,method))\n\t\t{\n\t\tOPENSSL_free(ret);\n\t\tret=NULL;\n\t\t}\n\treturn(ret);\n\t}', 'void *CRYPTO_malloc(int num, const char *file, int line)\n\t{\n\tvoid *ret = NULL;\n\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_func(num);\n#ifdef LEVITTE_DEBUG\n\tfprintf(stderr, "LEVITTE_DEBUG: > 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\treturn ret;\n\t}', 'int BIO_set(BIO *bio, BIO_METHOD *method)\n\t{\n\tbio->method=method;\n\tbio->callback=NULL;\n\tbio->cb_arg=NULL;\n\tbio->init=0;\n\tbio->shutdown=1;\n\tbio->flags=0;\n\tbio->retry_reason=0;\n\tbio->num=0;\n\tbio->ptr=NULL;\n\tbio->prev_bio=NULL;\n\tbio->next_bio=NULL;\n\tbio->references=1;\n\tbio->num_read=0L;\n\tbio->num_write=0L;\n\tCRYPTO_new_ex_data(bio_meth,bio,&bio->ex_data);\n\tif (method->create != NULL)\n\t\tif (!method->create(bio))\n\t\t\treturn(0);\n\treturn(1);\n\t}', 'long BIO_ctrl(BIO *b, int cmd, long larg, void *parg)\n\t{\n\tlong ret;\n\tlong (*cb)();\n\tif (b == NULL) return(0);\n\tif ((b->method == NULL) || (b->method->ctrl == NULL))\n\t\t{\n\t\tBIOerr(BIO_F_BIO_CTRL,BIO_R_UNSUPPORTED_METHOD);\n\t\treturn(-2);\n\t\t}\n\tcb=b->callback;\n\tif ((cb != NULL) &&\n\t\t((ret=cb(b,BIO_CB_CTRL,parg,cmd,larg,1L)) <= 0))\n\t\treturn(ret);\n\tret=b->method->ctrl(b,cmd,larg,parg);\n\tif (cb != NULL)\n\t\tret=cb(b,BIO_CB_CTRL|BIO_CB_RETURN,parg,cmd,\n\t\t\tlarg,ret);\n\treturn(ret);\n\t}', 'void ERR_put_error(int lib, int func, int reason, const char *file,\n\t int line)\n\t{\n\tERR_STATE *es;\n#ifdef _OSD_POSIX\n\tif (strncmp(file,"*POSIX(", sizeof("*POSIX(")-1) == 0) {\n\t\tchar *end;\n\t\tfile += sizeof("*POSIX(")-1;\n\t\tend = &file[strlen(file)-1];\n\t\tif (*end == \')\')\n\t\t\t*end = \'\\0\';\n\t\tif ((end = strrchr(file, \'/\')) != NULL)\n\t\t\tfile = &end[1];\n\t}\n#endif\n\tes=ERR_get_state();\n\tes->top=(es->top+1)%ERR_NUM_ERRORS;\n\tif (es->top == es->bottom)\n\t\tes->bottom=(es->bottom+1)%ERR_NUM_ERRORS;\n\tes->err_buffer[es->top]=ERR_PACK(lib,func,reason);\n\tes->err_file[es->top]=file;\n\tes->err_line[es->top]=line;\n\terr_clear_data(es,es->top);\n\t}']
36,495
0
https://github.com/openssl/openssl/blob/5dfc369ffcdc4722482c818e6ba6cf6e704c2cb5/crypto/des/des_enc.c/#L142
void des_encrypt(DES_LONG *data, des_key_schedule ks, int enc) { register DES_LONG l,r,t,u; #ifdef DES_PTR register const unsigned char *des_SP=(const unsigned char *)des_SPtrans; #endif #ifndef DES_UNROLL register int i; #endif register DES_LONG *s; r=data[0]; l=data[1]; IP(r,l); r=ROTATE(r,29)&0xffffffffL; l=ROTATE(l,29)&0xffffffffL; s=(DES_LONG *)ks; if (enc) { #ifdef DES_UNROLL D_ENCRYPT(l,r, 0); D_ENCRYPT(r,l, 2); D_ENCRYPT(l,r, 4); D_ENCRYPT(r,l, 6); D_ENCRYPT(l,r, 8); D_ENCRYPT(r,l,10); D_ENCRYPT(l,r,12); D_ENCRYPT(r,l,14); D_ENCRYPT(l,r,16); D_ENCRYPT(r,l,18); D_ENCRYPT(l,r,20); D_ENCRYPT(r,l,22); D_ENCRYPT(l,r,24); D_ENCRYPT(r,l,26); D_ENCRYPT(l,r,28); D_ENCRYPT(r,l,30); #else for (i=0; i<32; i+=8) { D_ENCRYPT(l,r,i+0); D_ENCRYPT(r,l,i+2); D_ENCRYPT(l,r,i+4); D_ENCRYPT(r,l,i+6); } #endif } else { #ifdef DES_UNROLL D_ENCRYPT(l,r,30); D_ENCRYPT(r,l,28); D_ENCRYPT(l,r,26); D_ENCRYPT(r,l,24); D_ENCRYPT(l,r,22); D_ENCRYPT(r,l,20); D_ENCRYPT(l,r,18); D_ENCRYPT(r,l,16); D_ENCRYPT(l,r,14); D_ENCRYPT(r,l,12); D_ENCRYPT(l,r,10); D_ENCRYPT(r,l, 8); D_ENCRYPT(l,r, 6); D_ENCRYPT(r,l, 4); D_ENCRYPT(l,r, 2); D_ENCRYPT(r,l, 0); #else for (i=30; i>0; i-=8) { D_ENCRYPT(l,r,i-0); D_ENCRYPT(r,l,i-2); D_ENCRYPT(l,r,i-4); D_ENCRYPT(r,l,i-6); } #endif } l=ROTATE(l,3)&0xffffffffL; r=ROTATE(r,3)&0xffffffffL; FP(r,l); data[0]=l; data[1]=r; l=r=t=u=0; }
['int _des_crypt(char *buf, int len, struct desparams *desp)\n\t{\n\tdes_key_schedule ks;\n\tint enc;\n\tdes_set_key(desp->des_key,ks);\n\tenc=(desp->des_dir == ENCRYPT)?DES_ENCRYPT:DES_DECRYPT;\n\tif (desp->des_mode == CBC)\n\t\tdes_ecb_encrypt(desp->UDES.UDES_buf,desp->UDES.UDES_buf,ks,\n\t\t\t\tenc);\n\telse\n\t\t{\n\t\tdes_ncbc_encrypt(desp->UDES.UDES_buf,desp->UDES.UDES_buf,\n\t\t\t\tlen,ks,desp->des_ivec,enc);\n#ifdef undef\n\t\ta=(char *)&(desp->UDES.UDES_buf[len-8]);\n\t\tb=(char *)&(desp->des_ivec[0]);\n\t\t*(a++)= *(b++); *(a++)= *(b++);\n\t\t*(a++)= *(b++); *(a++)= *(b++);\n\t\t*(a++)= *(b++); *(a++)= *(b++);\n\t\t*(a++)= *(b++); *(a++)= *(b++);\n#endif\n\t\t}\n\treturn(1);\n\t}', 'void des_ncbc_encrypt(const unsigned char *in, unsigned char *out, long length,\n\t des_key_schedule schedule, des_cblock ivec, int enc)\n\t{\n\tregister DES_LONG tin0,tin1;\n\tregister DES_LONG tout0,tout1,xor0,xor1;\n\tregister long l=length;\n\tDES_LONG tin[2];\n\tunsigned char *iv;\n\tiv=ivec;\n\tif (enc)\n\t\t{\n\t\tc2l(iv,tout0);\n\t\tc2l(iv,tout1);\n\t\tfor (l-=8; l>=0; l-=8)\n\t\t\t{\n\t\t\tc2l(in,tin0);\n\t\t\tc2l(in,tin1);\n\t\t\ttin0^=tout0; tin[0]=tin0;\n\t\t\ttin1^=tout1; tin[1]=tin1;\n\t\t\tdes_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);\n\t\t\ttout0=tin[0]; l2c(tout0,out);\n\t\t\ttout1=tin[1]; l2c(tout1,out);\n\t\t\t}\n\t\tif (l != -8)\n\t\t\t{\n\t\t\tc2ln(in,tin0,tin1,l+8);\n\t\t\ttin0^=tout0; tin[0]=tin0;\n\t\t\ttin1^=tout1; tin[1]=tin1;\n\t\t\tdes_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);\n\t\t\ttout0=tin[0]; l2c(tout0,out);\n\t\t\ttout1=tin[1]; l2c(tout1,out);\n\t\t\t}\n\t\tiv=ivec;\n\t\tl2c(tout0,iv);\n\t\tl2c(tout1,iv);\n\t\t}\n\telse\n\t\t{\n\t\tc2l(iv,xor0);\n\t\tc2l(iv,xor1);\n\t\tfor (l-=8; l>=0; l-=8)\n\t\t\t{\n\t\t\tc2l(in,tin0); tin[0]=tin0;\n\t\t\tc2l(in,tin1); tin[1]=tin1;\n\t\t\tdes_encrypt((DES_LONG *)tin,schedule,DES_DECRYPT);\n\t\t\ttout0=tin[0]^xor0;\n\t\t\ttout1=tin[1]^xor1;\n\t\t\tl2c(tout0,out);\n\t\t\tl2c(tout1,out);\n\t\t\txor0=tin0;\n\t\t\txor1=tin1;\n\t\t\t}\n\t\tif (l != -8)\n\t\t\t{\n\t\t\tc2l(in,tin0); tin[0]=tin0;\n\t\t\tc2l(in,tin1); tin[1]=tin1;\n\t\t\tdes_encrypt((DES_LONG *)tin,schedule,DES_DECRYPT);\n\t\t\ttout0=tin[0]^xor0;\n\t\t\ttout1=tin[1]^xor1;\n\t\t\tl2cn(tout0,tout1,out,l+8);\n\t\t\txor0=tin0;\n\t\t\txor1=tin1;\n\t\t\t}\n\t\tiv=ivec;\n\t\tl2c(xor0,iv);\n\t\tl2c(xor1,iv);\n\t\t}\n\ttin0=tin1=tout0=tout1=xor0=xor1=0;\n\ttin[0]=tin[1]=0;\n\t}', 'void des_encrypt(DES_LONG *data, des_key_schedule ks, int enc)\n\t{\n\tregister DES_LONG l,r,t,u;\n#ifdef DES_PTR\n\tregister const unsigned char *des_SP=(const unsigned char *)des_SPtrans;\n#endif\n#ifndef DES_UNROLL\n\tregister int i;\n#endif\n\tregister DES_LONG *s;\n\tr=data[0];\n\tl=data[1];\n\tIP(r,l);\n\tr=ROTATE(r,29)&0xffffffffL;\n\tl=ROTATE(l,29)&0xffffffffL;\n\ts=(DES_LONG *)ks;\n\tif (enc)\n\t\t{\n#ifdef DES_UNROLL\n\t\tD_ENCRYPT(l,r, 0);\n\t\tD_ENCRYPT(r,l, 2);\n\t\tD_ENCRYPT(l,r, 4);\n\t\tD_ENCRYPT(r,l, 6);\n\t\tD_ENCRYPT(l,r, 8);\n\t\tD_ENCRYPT(r,l,10);\n\t\tD_ENCRYPT(l,r,12);\n\t\tD_ENCRYPT(r,l,14);\n\t\tD_ENCRYPT(l,r,16);\n\t\tD_ENCRYPT(r,l,18);\n\t\tD_ENCRYPT(l,r,20);\n\t\tD_ENCRYPT(r,l,22);\n\t\tD_ENCRYPT(l,r,24);\n\t\tD_ENCRYPT(r,l,26);\n\t\tD_ENCRYPT(l,r,28);\n\t\tD_ENCRYPT(r,l,30);\n#else\n\t\tfor (i=0; i<32; i+=8)\n\t\t\t{\n\t\t\tD_ENCRYPT(l,r,i+0);\n\t\t\tD_ENCRYPT(r,l,i+2);\n\t\t\tD_ENCRYPT(l,r,i+4);\n\t\t\tD_ENCRYPT(r,l,i+6);\n\t\t\t}\n#endif\n\t\t}\n\telse\n\t\t{\n#ifdef DES_UNROLL\n\t\tD_ENCRYPT(l,r,30);\n\t\tD_ENCRYPT(r,l,28);\n\t\tD_ENCRYPT(l,r,26);\n\t\tD_ENCRYPT(r,l,24);\n\t\tD_ENCRYPT(l,r,22);\n\t\tD_ENCRYPT(r,l,20);\n\t\tD_ENCRYPT(l,r,18);\n\t\tD_ENCRYPT(r,l,16);\n\t\tD_ENCRYPT(l,r,14);\n\t\tD_ENCRYPT(r,l,12);\n\t\tD_ENCRYPT(l,r,10);\n\t\tD_ENCRYPT(r,l, 8);\n\t\tD_ENCRYPT(l,r, 6);\n\t\tD_ENCRYPT(r,l, 4);\n\t\tD_ENCRYPT(l,r, 2);\n\t\tD_ENCRYPT(r,l, 0);\n#else\n\t\tfor (i=30; i>0; i-=8)\n\t\t\t{\n\t\t\tD_ENCRYPT(l,r,i-0);\n\t\t\tD_ENCRYPT(r,l,i-2);\n\t\t\tD_ENCRYPT(l,r,i-4);\n\t\t\tD_ENCRYPT(r,l,i-6);\n\t\t\t}\n#endif\n\t\t}\n\tl=ROTATE(l,3)&0xffffffffL;\n\tr=ROTATE(r,3)&0xffffffffL;\n\tFP(r,l);\n\tdata[0]=l;\n\tdata[1]=r;\n\tl=r=t=u=0;\n\t}']
36,496
0
https://github.com/openssl/openssl/blob/183733f882056ea3e6fe95e665b85fcc6a45dcb4/ssl/bio_ssl.c/#L537
BIO *BIO_new_ssl(SSL_CTX *ctx, int client) { BIO *ret; SSL *ssl; if ((ret = BIO_new(BIO_f_ssl())) == NULL) return (NULL); if ((ssl = SSL_new(ctx)) == NULL) { BIO_free(ret); return (NULL); } if (client) SSL_set_connect_state(ssl); else SSL_set_accept_state(ssl); BIO_set_ssl(ret, ssl, BIO_CLOSE); return (ret); }
['BIO *BIO_new_ssl(SSL_CTX *ctx, int client)\n{\n BIO *ret;\n SSL *ssl;\n if ((ret = BIO_new(BIO_f_ssl())) == NULL)\n return (NULL);\n if ((ssl = SSL_new(ctx)) == NULL) {\n BIO_free(ret);\n return (NULL);\n }\n if (client)\n SSL_set_connect_state(ssl);\n else\n SSL_set_accept_state(ssl);\n BIO_set_ssl(ret, ssl, BIO_CLOSE);\n return (ret);\n}', 'BIO_METHOD *BIO_f_ssl(void)\n{\n return (&methods_sslp);\n}', 'BIO *BIO_new(BIO_METHOD *method)\n{\n BIO *ret = OPENSSL_malloc(sizeof(*ret));\n if (ret == NULL) {\n BIOerr(BIO_F_BIO_NEW, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n if (!BIO_set(ret, method)) {\n OPENSSL_free(ret);\n ret = NULL;\n }\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}', 'SSL *SSL_new(SSL_CTX *ctx)\n{\n SSL *s;\n if (ctx == NULL) {\n SSLerr(SSL_F_SSL_NEW, SSL_R_NULL_SSL_CTX);\n return (NULL);\n }\n if (ctx->method == NULL) {\n SSLerr(SSL_F_SSL_NEW, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);\n return (NULL);\n }\n s = OPENSSL_zalloc(sizeof(*s));\n if (s == NULL)\n goto err;\n RECORD_LAYER_init(&s->rlayer, s);\n s->options = ctx->options;\n s->min_proto_version = ctx->min_proto_version;\n s->max_proto_version = ctx->max_proto_version;\n s->mode = ctx->mode;\n s->max_cert_list = ctx->max_cert_list;\n s->references = 1;\n s->cert = ssl_cert_dup(ctx->cert);\n if (s->cert == NULL)\n goto err;\n RECORD_LAYER_set_read_ahead(&s->rlayer, ctx->read_ahead);\n s->msg_callback = ctx->msg_callback;\n s->msg_callback_arg = ctx->msg_callback_arg;\n s->verify_mode = ctx->verify_mode;\n s->not_resumable_session_cb = ctx->not_resumable_session_cb;\n s->sid_ctx_length = ctx->sid_ctx_length;\n OPENSSL_assert(s->sid_ctx_length <= sizeof s->sid_ctx);\n memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));\n s->verify_callback = ctx->default_verify_callback;\n s->generate_session_id = ctx->generate_session_id;\n s->param = X509_VERIFY_PARAM_new();\n if (s->param == NULL)\n goto err;\n X509_VERIFY_PARAM_inherit(s->param, ctx->param);\n s->quiet_shutdown = ctx->quiet_shutdown;\n s->max_send_fragment = ctx->max_send_fragment;\n CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);\n s->ctx = ctx;\n s->tlsext_debug_cb = 0;\n s->tlsext_debug_arg = NULL;\n s->tlsext_ticket_expected = 0;\n s->tlsext_status_type = -1;\n s->tlsext_status_expected = 0;\n s->tlsext_ocsp_ids = NULL;\n s->tlsext_ocsp_exts = NULL;\n s->tlsext_ocsp_resp = NULL;\n s->tlsext_ocsp_resplen = -1;\n CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);\n s->initial_ctx = ctx;\n# ifndef OPENSSL_NO_EC\n if (ctx->tlsext_ecpointformatlist) {\n s->tlsext_ecpointformatlist =\n OPENSSL_memdup(ctx->tlsext_ecpointformatlist,\n ctx->tlsext_ecpointformatlist_length);\n if (!s->tlsext_ecpointformatlist)\n goto err;\n s->tlsext_ecpointformatlist_length =\n ctx->tlsext_ecpointformatlist_length;\n }\n if (ctx->tlsext_ellipticcurvelist) {\n s->tlsext_ellipticcurvelist =\n OPENSSL_memdup(ctx->tlsext_ellipticcurvelist,\n ctx->tlsext_ellipticcurvelist_length);\n if (!s->tlsext_ellipticcurvelist)\n goto err;\n s->tlsext_ellipticcurvelist_length =\n ctx->tlsext_ellipticcurvelist_length;\n }\n# endif\n# ifndef OPENSSL_NO_NEXTPROTONEG\n s->next_proto_negotiated = NULL;\n# endif\n if (s->ctx->alpn_client_proto_list) {\n s->alpn_client_proto_list =\n OPENSSL_malloc(s->ctx->alpn_client_proto_list_len);\n if (s->alpn_client_proto_list == NULL)\n goto err;\n memcpy(s->alpn_client_proto_list, s->ctx->alpn_client_proto_list,\n s->ctx->alpn_client_proto_list_len);\n s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;\n }\n s->verified_chain = NULL;\n s->verify_result = X509_V_OK;\n s->default_passwd_callback = ctx->default_passwd_callback;\n s->default_passwd_callback_userdata = ctx->default_passwd_callback_userdata;\n s->method = ctx->method;\n if (!s->method->ssl_new(s))\n goto err;\n s->server = (ctx->method->ssl_accept == ssl_undefined_function) ? 0 : 1;\n if (!SSL_clear(s))\n goto err;\n CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);\n#ifndef OPENSSL_NO_PSK\n s->psk_client_callback = ctx->psk_client_callback;\n s->psk_server_callback = ctx->psk_server_callback;\n#endif\n s->job = NULL;\n return (s);\n err:\n SSL_free(s);\n SSLerr(SSL_F_SSL_NEW, ERR_R_MALLOC_FAILURE);\n return (NULL);\n}', 'int BIO_free(BIO *a)\n{\n int i;\n if (a == NULL)\n return (0);\n i = CRYPTO_add(&a->references, -1, CRYPTO_LOCK_BIO);\n#ifdef REF_PRINT\n REF_PRINT("BIO", a);\n#endif\n if (i > 0)\n return (1);\n#ifdef REF_CHECK\n if (i < 0) {\n fprintf(stderr, "BIO_free, bad reference count\\n");\n abort();\n }\n#endif\n if ((a->callback != NULL) &&\n ((i = (int)a->callback(a, BIO_CB_FREE, NULL, 0, 0L, 1L)) <= 0))\n return (i);\n CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data);\n if ((a->method != NULL) && (a->method->destroy != NULL))\n a->method->destroy(a);\n OPENSSL_free(a);\n return (1);\n}', 'int CRYPTO_add_lock(int *pointer, int amount, int type, const char *file,\n int line)\n{\n int ret = 0;\n if (add_lock_callback != NULL) {\n#ifdef LOCK_DEBUG\n int before = *pointer;\n#endif\n ret = add_lock_callback(pointer, amount, type, file, line);\n#ifdef LOCK_DEBUG\n {\n CRYPTO_THREADID id;\n CRYPTO_THREADID_current(&id);\n fprintf(stderr, "ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\\n",\n CRYPTO_THREADID_hash(&id), before, amount, ret,\n CRYPTO_get_lock_name(type), file, line);\n }\n#endif\n } else {\n CRYPTO_lock(CRYPTO_LOCK | CRYPTO_WRITE, type, file, line);\n ret = *pointer + amount;\n#ifdef LOCK_DEBUG\n {\n CRYPTO_THREADID id;\n CRYPTO_THREADID_current(&id);\n fprintf(stderr, "ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\\n",\n CRYPTO_THREADID_hash(&id),\n *pointer, amount, ret,\n CRYPTO_get_lock_name(type), file, line);\n }\n#endif\n *pointer = ret;\n CRYPTO_lock(CRYPTO_UNLOCK | CRYPTO_WRITE, type, file, line);\n }\n return (ret);\n}', 'void CRYPTO_lock(int mode, int type, const char *file, int line)\n{\n#ifdef LOCK_DEBUG\n {\n CRYPTO_THREADID id;\n char *rw_text, *operation_text;\n if (mode & CRYPTO_LOCK)\n operation_text = "lock ";\n else if (mode & CRYPTO_UNLOCK)\n operation_text = "unlock";\n else\n operation_text = "ERROR ";\n if (mode & CRYPTO_READ)\n rw_text = "r";\n else if (mode & CRYPTO_WRITE)\n rw_text = "w";\n else\n rw_text = "ERROR";\n CRYPTO_THREADID_current(&id);\n fprintf(stderr, "lock:%08lx:(%s)%s %-18s %s:%d\\n",\n CRYPTO_THREADID_hash(&id), rw_text, operation_text,\n CRYPTO_get_lock_name(type), file, line);\n }\n#endif\n if (type < 0) {\n if (dynlock_lock_callback != NULL) {\n struct CRYPTO_dynlock_value *pointer\n = CRYPTO_get_dynlock_value(type);\n OPENSSL_assert(pointer != NULL);\n dynlock_lock_callback(mode, pointer, file, line);\n CRYPTO_destroy_dynlockid(type);\n }\n } else if (locking_callback != NULL)\n locking_callback(mode, type, file, line);\n}']
36,497
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 svq1_decode_frame_header(AVCodecContext *avctx, AVFrame *frame)\n{\n SVQ1Context *s = avctx->priv_data;\n BitstreamContext *bc = &s->bc;\n int frame_size_code;\n bitstream_skip(bc, 8);\n s->nonref = 0;\n switch (bitstream_read(bc, 2)) {\n case 0:\n frame->pict_type = AV_PICTURE_TYPE_I;\n break;\n case 2:\n s->nonref = 1;\n case 1:\n frame->pict_type = AV_PICTURE_TYPE_P;\n break;\n default:\n av_log(avctx, AV_LOG_ERROR, "Invalid frame type.\\n");\n return AVERROR_INVALIDDATA;\n }\n if (frame->pict_type == AV_PICTURE_TYPE_I) {\n if (s->frame_code == 0x50 || s->frame_code == 0x60) {\n int csum = bitstream_read(bc, 16);\n csum = ff_svq1_packet_checksum(bc->buffer, bc->size_in_bits >> 3,\n csum);\n ff_dlog(avctx, "%s checksum (%02x) for packet data\\n",\n (csum == 0) ? "correct" : "incorrect", csum);\n }\n if ((s->frame_code ^ 0x10) >= 0x50) {\n uint8_t msg[256];\n svq1_parse_string(bc, msg);\n av_log(avctx, AV_LOG_INFO,\n "embedded message: \\"%s\\"\\n", (char *)msg);\n }\n bitstream_skip(bc, 2);\n bitstream_skip(bc, 2);\n bitstream_skip(bc, 1);\n frame_size_code = bitstream_read(bc, 3);\n if (frame_size_code == 7) {\n s->width = bitstream_read(bc, 12);\n s->height = bitstream_read(bc, 12);\n if (!s->width || !s->height)\n return AVERROR_INVALIDDATA;\n } else {\n s->width = ff_svq1_frame_size_table[frame_size_code][0];\n s->height = ff_svq1_frame_size_table[frame_size_code][1];\n }\n }\n if (bitstream_read_bit(bc) == 1) {\n bitstream_skip(bc, 1);\n bitstream_skip(bc, 1);\n if (bitstream_read(bc, 2) != 0)\n return AVERROR_INVALIDDATA;\n }\n if (bitstream_read_bit(bc) == 1) {\n bitstream_skip(bc, 1);\n bitstream_skip(bc, 4);\n bitstream_skip(bc, 1);\n bitstream_skip(bc, 2);\n while (bitstream_read_bit(bc) == 1)\n bitstream_skip(bc, 8);\n }\n return 0;\n}', 'static inline void bitstream_skip(BitstreamContext *bc, unsigned n)\n{\n if (n <= bc->bits_left)\n skip_remaining(bc, n);\n else {\n n -= bc->bits_left;\n skip_remaining(bc, bc->bits_left);\n if (n >= 64) {\n unsigned skip = n / 8;\n n -= skip * 8;\n bc->ptr += skip;\n }\n refill_64(bc);\n if (n)\n skip_remaining(bc, n);\n }\n}', 'static inline void skip_remaining(BitstreamContext *bc, unsigned n)\n{\n#ifdef BITSTREAM_READER_LE\n bc->bits >>= n;\n#else\n bc->bits <<= n;\n#endif\n bc->bits_left -= n;\n}']
36,498
0
https://github.com/nginx/nginx/blob/a54e3193bfa0e0b912cdc394564307be5bb68c17/src/core/ngx_slab.c/#L570
void ngx_slab_free_locked(ngx_slab_pool_t *pool, void *p) { size_t size; uintptr_t slab, m, *bitmap; ngx_uint_t i, n, type, slot, shift, map; ngx_slab_page_t *slots, *page; ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, ngx_cycle->log, 0, "slab free: %p", p); if ((u_char *) p < pool->start || (u_char *) p > pool->end) { ngx_slab_error(pool, NGX_LOG_ALERT, "ngx_slab_free(): outside of pool"); goto fail; } n = ((u_char *) p - pool->start) >> ngx_pagesize_shift; page = &pool->pages[n]; slab = page->slab; type = ngx_slab_page_type(page); switch (type) { case NGX_SLAB_SMALL: shift = slab & NGX_SLAB_SHIFT_MASK; size = (size_t) 1 << shift; if ((uintptr_t) p & (size - 1)) { goto wrong_chunk; } n = ((uintptr_t) p & (ngx_pagesize - 1)) >> shift; m = (uintptr_t) 1 << (n % (8 * sizeof(uintptr_t))); n /= 8 * sizeof(uintptr_t); bitmap = (uintptr_t *) ((uintptr_t) p & ~((uintptr_t) ngx_pagesize - 1)); if (bitmap[n] & m) { slot = shift - pool->min_shift; if (page->next == NULL) { slots = ngx_slab_slots(pool); page->next = slots[slot].next; slots[slot].next = page; page->prev = (uintptr_t) &slots[slot] | NGX_SLAB_SMALL; page->next->prev = (uintptr_t) page | NGX_SLAB_SMALL; } bitmap[n] &= ~m; n = (ngx_pagesize >> shift) / ((1 << shift) * 8); if (n == 0) { n = 1; } i = n / (8 * sizeof(uintptr_t)); m = ((uintptr_t) 1 << (n % (8 * sizeof(uintptr_t)))) - 1; if (bitmap[i] & ~m) { goto done; } map = (ngx_pagesize >> shift) / (8 * sizeof(uintptr_t)); for (i = i + 1; i < map; i++) { if (bitmap[i]) { goto done; } } ngx_slab_free_pages(pool, page, 1); pool->stats[slot].total -= (ngx_pagesize >> shift) - n; goto done; } goto chunk_already_free; case NGX_SLAB_EXACT: m = (uintptr_t) 1 << (((uintptr_t) p & (ngx_pagesize - 1)) >> ngx_slab_exact_shift); size = ngx_slab_exact_size; if ((uintptr_t) p & (size - 1)) { goto wrong_chunk; } if (slab & m) { slot = ngx_slab_exact_shift - pool->min_shift; if (slab == NGX_SLAB_BUSY) { slots = ngx_slab_slots(pool); page->next = slots[slot].next; slots[slot].next = page; page->prev = (uintptr_t) &slots[slot] | NGX_SLAB_EXACT; page->next->prev = (uintptr_t) page | NGX_SLAB_EXACT; } page->slab &= ~m; if (page->slab) { goto done; } ngx_slab_free_pages(pool, page, 1); pool->stats[slot].total -= 8 * sizeof(uintptr_t); goto done; } goto chunk_already_free; case NGX_SLAB_BIG: shift = slab & NGX_SLAB_SHIFT_MASK; size = (size_t) 1 << shift; if ((uintptr_t) p & (size - 1)) { goto wrong_chunk; } m = (uintptr_t) 1 << ((((uintptr_t) p & (ngx_pagesize - 1)) >> shift) + NGX_SLAB_MAP_SHIFT); if (slab & m) { slot = shift - pool->min_shift; if (page->next == NULL) { slots = ngx_slab_slots(pool); page->next = slots[slot].next; slots[slot].next = page; page->prev = (uintptr_t) &slots[slot] | NGX_SLAB_BIG; page->next->prev = (uintptr_t) page | NGX_SLAB_BIG; } page->slab &= ~m; if (page->slab & NGX_SLAB_MAP_MASK) { goto done; } ngx_slab_free_pages(pool, page, 1); pool->stats[slot].total -= ngx_pagesize >> shift; goto done; } goto chunk_already_free; case NGX_SLAB_PAGE: if ((uintptr_t) p & (ngx_pagesize - 1)) { goto wrong_chunk; } if (!(slab & NGX_SLAB_PAGE_START)) { ngx_slab_error(pool, NGX_LOG_ALERT, "ngx_slab_free(): page is already free"); goto fail; } if (slab == NGX_SLAB_PAGE_BUSY) { ngx_slab_error(pool, NGX_LOG_ALERT, "ngx_slab_free(): pointer to wrong page"); goto fail; } n = ((u_char *) p - pool->start) >> ngx_pagesize_shift; size = slab & ~NGX_SLAB_PAGE_START; ngx_slab_free_pages(pool, &pool->pages[n], size); ngx_slab_junk(p, size << ngx_pagesize_shift); return; } return; done: pool->stats[slot].used--; ngx_slab_junk(p, size); return; wrong_chunk: ngx_slab_error(pool, NGX_LOG_ALERT, "ngx_slab_free(): pointer to wrong chunk"); goto fail; chunk_already_free: ngx_slab_error(pool, NGX_LOG_ALERT, "ngx_slab_free(): chunk is already free"); fail: return; }
['static void\nngx_http_limit_req_expire(ngx_http_limit_req_ctx_t *ctx, ngx_uint_t n)\n{\n ngx_int_t excess;\n ngx_msec_t now;\n ngx_queue_t *q;\n ngx_msec_int_t ms;\n ngx_rbtree_node_t *node;\n ngx_http_limit_req_node_t *lr;\n now = ngx_current_msec;\n while (n < 3) {\n if (ngx_queue_empty(&ctx->sh->queue)) {\n return;\n }\n q = ngx_queue_last(&ctx->sh->queue);\n lr = ngx_queue_data(q, ngx_http_limit_req_node_t, queue);\n if (lr->count) {\n return;\n }\n if (n++ != 0) {\n ms = (ngx_msec_int_t) (now - lr->last);\n ms = ngx_abs(ms);\n if (ms < 60000) {\n return;\n }\n excess = lr->excess - ctx->rate * ms / 1000;\n if (excess > 0) {\n return;\n }\n }\n ngx_queue_remove(q);\n node = (ngx_rbtree_node_t *)\n ((u_char *) lr - offsetof(ngx_rbtree_node_t, color));\n ngx_rbtree_delete(&ctx->sh->rbtree, node);\n ngx_slab_free_locked(ctx->shpool, node);\n }\n}', 'void\nngx_slab_free_locked(ngx_slab_pool_t *pool, void *p)\n{\n size_t size;\n uintptr_t slab, m, *bitmap;\n ngx_uint_t i, n, type, slot, shift, map;\n ngx_slab_page_t *slots, *page;\n ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, ngx_cycle->log, 0, "slab free: %p", p);\n if ((u_char *) p < pool->start || (u_char *) p > pool->end) {\n ngx_slab_error(pool, NGX_LOG_ALERT, "ngx_slab_free(): outside of pool");\n goto fail;\n }\n n = ((u_char *) p - pool->start) >> ngx_pagesize_shift;\n page = &pool->pages[n];\n slab = page->slab;\n type = ngx_slab_page_type(page);\n switch (type) {\n case NGX_SLAB_SMALL:\n shift = slab & NGX_SLAB_SHIFT_MASK;\n size = (size_t) 1 << shift;\n if ((uintptr_t) p & (size - 1)) {\n goto wrong_chunk;\n }\n n = ((uintptr_t) p & (ngx_pagesize - 1)) >> shift;\n m = (uintptr_t) 1 << (n % (8 * sizeof(uintptr_t)));\n n /= 8 * sizeof(uintptr_t);\n bitmap = (uintptr_t *)\n ((uintptr_t) p & ~((uintptr_t) ngx_pagesize - 1));\n if (bitmap[n] & m) {\n slot = shift - pool->min_shift;\n if (page->next == NULL) {\n slots = ngx_slab_slots(pool);\n page->next = slots[slot].next;\n slots[slot].next = page;\n page->prev = (uintptr_t) &slots[slot] | NGX_SLAB_SMALL;\n page->next->prev = (uintptr_t) page | NGX_SLAB_SMALL;\n }\n bitmap[n] &= ~m;\n n = (ngx_pagesize >> shift) / ((1 << shift) * 8);\n if (n == 0) {\n n = 1;\n }\n i = n / (8 * sizeof(uintptr_t));\n m = ((uintptr_t) 1 << (n % (8 * sizeof(uintptr_t)))) - 1;\n if (bitmap[i] & ~m) {\n goto done;\n }\n map = (ngx_pagesize >> shift) / (8 * sizeof(uintptr_t));\n for (i = i + 1; i < map; i++) {\n if (bitmap[i]) {\n goto done;\n }\n }\n ngx_slab_free_pages(pool, page, 1);\n pool->stats[slot].total -= (ngx_pagesize >> shift) - n;\n goto done;\n }\n goto chunk_already_free;\n case NGX_SLAB_EXACT:\n m = (uintptr_t) 1 <<\n (((uintptr_t) p & (ngx_pagesize - 1)) >> ngx_slab_exact_shift);\n size = ngx_slab_exact_size;\n if ((uintptr_t) p & (size - 1)) {\n goto wrong_chunk;\n }\n if (slab & m) {\n slot = ngx_slab_exact_shift - pool->min_shift;\n if (slab == NGX_SLAB_BUSY) {\n slots = ngx_slab_slots(pool);\n page->next = slots[slot].next;\n slots[slot].next = page;\n page->prev = (uintptr_t) &slots[slot] | NGX_SLAB_EXACT;\n page->next->prev = (uintptr_t) page | NGX_SLAB_EXACT;\n }\n page->slab &= ~m;\n if (page->slab) {\n goto done;\n }\n ngx_slab_free_pages(pool, page, 1);\n pool->stats[slot].total -= 8 * sizeof(uintptr_t);\n goto done;\n }\n goto chunk_already_free;\n case NGX_SLAB_BIG:\n shift = slab & NGX_SLAB_SHIFT_MASK;\n size = (size_t) 1 << shift;\n if ((uintptr_t) p & (size - 1)) {\n goto wrong_chunk;\n }\n m = (uintptr_t) 1 << ((((uintptr_t) p & (ngx_pagesize - 1)) >> shift)\n + NGX_SLAB_MAP_SHIFT);\n if (slab & m) {\n slot = shift - pool->min_shift;\n if (page->next == NULL) {\n slots = ngx_slab_slots(pool);\n page->next = slots[slot].next;\n slots[slot].next = page;\n page->prev = (uintptr_t) &slots[slot] | NGX_SLAB_BIG;\n page->next->prev = (uintptr_t) page | NGX_SLAB_BIG;\n }\n page->slab &= ~m;\n if (page->slab & NGX_SLAB_MAP_MASK) {\n goto done;\n }\n ngx_slab_free_pages(pool, page, 1);\n pool->stats[slot].total -= ngx_pagesize >> shift;\n goto done;\n }\n goto chunk_already_free;\n case NGX_SLAB_PAGE:\n if ((uintptr_t) p & (ngx_pagesize - 1)) {\n goto wrong_chunk;\n }\n if (!(slab & NGX_SLAB_PAGE_START)) {\n ngx_slab_error(pool, NGX_LOG_ALERT,\n "ngx_slab_free(): page is already free");\n goto fail;\n }\n if (slab == NGX_SLAB_PAGE_BUSY) {\n ngx_slab_error(pool, NGX_LOG_ALERT,\n "ngx_slab_free(): pointer to wrong page");\n goto fail;\n }\n n = ((u_char *) p - pool->start) >> ngx_pagesize_shift;\n size = slab & ~NGX_SLAB_PAGE_START;\n ngx_slab_free_pages(pool, &pool->pages[n], size);\n ngx_slab_junk(p, size << ngx_pagesize_shift);\n return;\n }\n return;\ndone:\n pool->stats[slot].used--;\n ngx_slab_junk(p, size);\n return;\nwrong_chunk:\n ngx_slab_error(pool, NGX_LOG_ALERT,\n "ngx_slab_free(): pointer to wrong chunk");\n goto fail;\nchunk_already_free:\n ngx_slab_error(pool, NGX_LOG_ALERT,\n "ngx_slab_free(): chunk is already free");\nfail:\n return;\n}']
36,499
0
https://github.com/libav/libav/blob/a082ac412520cd5d812bd57e5ccdad2af557125b/libavcodec/proresenc.c/#L397
static inline int estimate_vlc(uint8_t codebook, int val) { unsigned int rice_order, exp_order, switch_bits, switch_val; int exponent; switch_bits = (codebook & 3) + 1; rice_order = codebook >> 5; exp_order = (codebook >> 2) & 7; switch_val = switch_bits << rice_order; if (val >= switch_val) { val -= switch_val - (1 << exp_order); exponent = av_log2(val); return exponent * 2 - exp_order + switch_bits + 1; } else { return (val >> rice_order) + rice_order + 1; } }
['static int estimate_slice_plane(ProresContext *ctx, int *error, int plane,\n const uint16_t *src, int linesize,\n int mbs_per_slice,\n int blocks_per_mb, int plane_size_factor,\n const int16_t *qmat)\n{\n int blocks_per_slice;\n int bits;\n blocks_per_slice = mbs_per_slice * blocks_per_mb;\n bits = estimate_dcs(error, ctx->blocks[plane], blocks_per_slice, qmat[0]);\n bits += estimate_acs(error, ctx->blocks[plane], blocks_per_slice,\n plane_size_factor, ctx->scantable.permutated, qmat);\n return FFALIGN(bits, 8);\n}', 'static int estimate_acs(int *error, DCTELEM *blocks, int blocks_per_slice,\n int plane_size_factor,\n const uint8_t *scan, const int16_t *qmat)\n{\n int idx, i;\n int run, level, run_cb, lev_cb;\n int max_coeffs, abs_level;\n int bits = 0;\n max_coeffs = blocks_per_slice << 6;\n run_cb = ff_prores_run_to_cb_index[4];\n lev_cb = ff_prores_lev_to_cb_index[2];\n run = 0;\n for (i = 1; i < 64; i++) {\n for (idx = scan[i]; idx < max_coeffs; idx += 64) {\n level = blocks[idx] / qmat[scan[i]];\n *error += FFABS(blocks[idx]) % qmat[scan[i]];\n if (level) {\n abs_level = FFABS(level);\n bits += estimate_vlc(ff_prores_ac_codebook[run_cb], run);\n bits += estimate_vlc(ff_prores_ac_codebook[lev_cb],\n abs_level - 1) + 1;\n run_cb = ff_prores_run_to_cb_index[FFMIN(run, 15)];\n lev_cb = ff_prores_lev_to_cb_index[FFMIN(abs_level, 9)];\n run = 0;\n } else {\n run++;\n }\n }\n }\n return bits;\n}', 'static inline int estimate_vlc(uint8_t codebook, int val)\n{\n unsigned int rice_order, exp_order, switch_bits, switch_val;\n int exponent;\n switch_bits = (codebook & 3) + 1;\n rice_order = codebook >> 5;\n exp_order = (codebook >> 2) & 7;\n switch_val = switch_bits << rice_order;\n if (val >= switch_val) {\n val -= switch_val - (1 << exp_order);\n exponent = av_log2(val);\n return exponent * 2 - exp_order + switch_bits + 1;\n } else {\n return (val >> rice_order) + rice_order + 1;\n }\n}', 'static av_always_inline av_const int av_log2_c(unsigned int v)\n{\n int n = 0;\n if (v & 0xffff0000) {\n v >>= 16;\n n += 16;\n }\n if (v & 0xff00) {\n v >>= 8;\n n += 8;\n }\n n += ff_log2_tab[v];\n return n;\n}']
36,500
0
https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/h264pred.c/#L257
static void pred4x4_vertical_right_c(uint8_t *src, uint8_t *topright, int stride){ const int lt= src[-1-1*stride]; LOAD_TOP_EDGE LOAD_LEFT_EDGE src[0+0*stride]= src[1+2*stride]=(lt + t0 + 1)>>1; src[1+0*stride]= src[2+2*stride]=(t0 + t1 + 1)>>1; src[2+0*stride]= src[3+2*stride]=(t1 + t2 + 1)>>1; src[3+0*stride]=(t2 + t3 + 1)>>1; src[0+1*stride]= src[1+3*stride]=(l0 + 2*lt + t0 + 2)>>2; src[1+1*stride]= src[2+3*stride]=(lt + 2*t0 + t1 + 2)>>2; src[2+1*stride]= src[3+3*stride]=(t0 + 2*t1 + t2 + 2)>>2; src[3+1*stride]=(t1 + 2*t2 + t3 + 2)>>2; src[0+2*stride]=(lt + 2*l0 + l1 + 2)>>2; src[0+3*stride]=(l0 + 2*l1 + l2 + 2)>>2; }
['static void pred4x4_vertical_right_c(uint8_t *src, uint8_t *topright, int stride){\n const int lt= src[-1-1*stride];\n LOAD_TOP_EDGE\n LOAD_LEFT_EDGE\n src[0+0*stride]=\n src[1+2*stride]=(lt + t0 + 1)>>1;\n src[1+0*stride]=\n src[2+2*stride]=(t0 + t1 + 1)>>1;\n src[2+0*stride]=\n src[3+2*stride]=(t1 + t2 + 1)>>1;\n src[3+0*stride]=(t2 + t3 + 1)>>1;\n src[0+1*stride]=\n src[1+3*stride]=(l0 + 2*lt + t0 + 2)>>2;\n src[1+1*stride]=\n src[2+3*stride]=(lt + 2*t0 + t1 + 2)>>2;\n src[2+1*stride]=\n src[3+3*stride]=(t0 + 2*t1 + t2 + 2)>>2;\n src[3+1*stride]=(t1 + 2*t2 + t3 + 2)>>2;\n src[0+2*stride]=(lt + 2*l0 + l1 + 2)>>2;\n src[0+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;\n}']