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
|
---|---|---|---|---|
501 | 0 | https://github.com/libav/libav/blob/fc8b1075e1f74ab14798e8698bd818eb1a14b938/libavformat/oggparsespeex.c/#L68 | static int speex_header(AVFormatContext *s, int idx) {
struct ogg *ogg = s->priv_data;
struct ogg_stream *os = ogg->streams + idx;
struct speex_params *spxp = os->private;
AVStream *st = s->streams[idx];
uint8_t *p = os->buf + os->pstart;
if (!spxp) {
spxp = av_mallocz(sizeof(*spxp));
os->private = spxp;
}
if (spxp->seq > 1)
return 0;
if (spxp->seq == 0) {
int frames_per_packet;
st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
st->codec->codec_id = CODEC_ID_SPEEX;
st->codec->sample_rate = AV_RL32(p + 36);
st->codec->channels = AV_RL32(p + 48);
st->codec->frame_size = AV_RL32(p + 56);
frames_per_packet = AV_RL32(p + 64);
if (frames_per_packet)
st->codec->frame_size *= frames_per_packet;
st->codec->extradata_size = os->psize;
st->codec->extradata = av_malloc(st->codec->extradata_size
+ FF_INPUT_BUFFER_PADDING_SIZE);
memcpy(st->codec->extradata, p, st->codec->extradata_size);
st->time_base.num = 1;
st->time_base.den = st->codec->sample_rate;
} else
ff_vorbis_comment(s, &st->metadata, p, os->psize);
spxp->seq++;
return 1;
} | ['static int speex_header(AVFormatContext *s, int idx) {\n struct ogg *ogg = s->priv_data;\n struct ogg_stream *os = ogg->streams + idx;\n struct speex_params *spxp = os->private;\n AVStream *st = s->streams[idx];\n uint8_t *p = os->buf + os->pstart;\n if (!spxp) {\n spxp = av_mallocz(sizeof(*spxp));\n os->private = spxp;\n }\n if (spxp->seq > 1)\n return 0;\n if (spxp->seq == 0) {\n int frames_per_packet;\n st->codec->codec_type = AVMEDIA_TYPE_AUDIO;\n st->codec->codec_id = CODEC_ID_SPEEX;\n st->codec->sample_rate = AV_RL32(p + 36);\n st->codec->channels = AV_RL32(p + 48);\n st->codec->frame_size = AV_RL32(p + 56);\n frames_per_packet = AV_RL32(p + 64);\n if (frames_per_packet)\n st->codec->frame_size *= frames_per_packet;\n st->codec->extradata_size = os->psize;\n st->codec->extradata = av_malloc(st->codec->extradata_size\n + FF_INPUT_BUFFER_PADDING_SIZE);\n memcpy(st->codec->extradata, p, st->codec->extradata_size);\n st->time_base.num = 1;\n st->time_base.den = st->codec->sample_rate;\n } else\n ff_vorbis_comment(s, &st->metadata, p, os->psize);\n spxp->seq++;\n return 1;\n}'] |
502 | 0 | https://github.com/openssl/openssl/blob/297c1149f6b8b0f9ade5f2bf12e148056ef77734/test/ssl_test_ctx.c/#L801 | SSL_TEST_CTX *SSL_TEST_CTX_create(const CONF *conf, const char *test_section)
{
STACK_OF(CONF_VALUE) *sk_conf = NULL;
SSL_TEST_CTX *ctx = NULL;
int i;
size_t j;
if (!TEST_ptr(sk_conf = NCONF_get_section(conf, test_section))
|| !TEST_ptr(ctx = SSL_TEST_CTX_new()))
goto err;
for (i = 0; i < sk_CONF_VALUE_num(sk_conf); i++) {
int found = 0;
const CONF_VALUE *option = sk_CONF_VALUE_value(sk_conf, i);
if (strcmp(option->name, "client") == 0) {
if (!parse_client_options(&ctx->extra.client, conf, option->value))
goto err;
} else if (strcmp(option->name, "server") == 0) {
if (!parse_server_options(&ctx->extra.server, conf, option->value))
goto err;
} else if (strcmp(option->name, "server2") == 0) {
if (!parse_server_options(&ctx->extra.server2, conf, option->value))
goto err;
} else if (strcmp(option->name, "resume-client") == 0) {
if (!parse_client_options(&ctx->resume_extra.client, conf,
option->value))
goto err;
} else if (strcmp(option->name, "resume-server") == 0) {
if (!parse_server_options(&ctx->resume_extra.server, conf,
option->value))
goto err;
} else if (strcmp(option->name, "resume-server2") == 0) {
if (!parse_server_options(&ctx->resume_extra.server2, conf,
option->value))
goto err;
} else {
for (j = 0; j < OSSL_NELEM(ssl_test_ctx_options); j++) {
if (strcmp(option->name, ssl_test_ctx_options[j].name) == 0) {
if (!ssl_test_ctx_options[j].parse(ctx, option->value)) {
TEST_info("Bad value %s for option %s",
option->value, option->name);
goto err;
}
found = 1;
break;
}
}
if (!found) {
TEST_info("Unknown test option: %s", option->name);
goto err;
}
}
}
goto done;
err:
SSL_TEST_CTX_free(ctx);
ctx = NULL;
done:
return ctx;
} | ['SSL_TEST_CTX *SSL_TEST_CTX_create(const CONF *conf, const char *test_section)\n{\n STACK_OF(CONF_VALUE) *sk_conf = NULL;\n SSL_TEST_CTX *ctx = NULL;\n int i;\n size_t j;\n if (!TEST_ptr(sk_conf = NCONF_get_section(conf, test_section))\n || !TEST_ptr(ctx = SSL_TEST_CTX_new()))\n goto err;\n for (i = 0; i < sk_CONF_VALUE_num(sk_conf); i++) {\n int found = 0;\n const CONF_VALUE *option = sk_CONF_VALUE_value(sk_conf, i);\n if (strcmp(option->name, "client") == 0) {\n if (!parse_client_options(&ctx->extra.client, conf, option->value))\n goto err;\n } else if (strcmp(option->name, "server") == 0) {\n if (!parse_server_options(&ctx->extra.server, conf, option->value))\n goto err;\n } else if (strcmp(option->name, "server2") == 0) {\n if (!parse_server_options(&ctx->extra.server2, conf, option->value))\n goto err;\n } else if (strcmp(option->name, "resume-client") == 0) {\n if (!parse_client_options(&ctx->resume_extra.client, conf,\n option->value))\n goto err;\n } else if (strcmp(option->name, "resume-server") == 0) {\n if (!parse_server_options(&ctx->resume_extra.server, conf,\n option->value))\n goto err;\n } else if (strcmp(option->name, "resume-server2") == 0) {\n if (!parse_server_options(&ctx->resume_extra.server2, conf,\n option->value))\n goto err;\n } else {\n for (j = 0; j < OSSL_NELEM(ssl_test_ctx_options); j++) {\n if (strcmp(option->name, ssl_test_ctx_options[j].name) == 0) {\n if (!ssl_test_ctx_options[j].parse(ctx, option->value)) {\n TEST_info("Bad value %s for option %s",\n option->value, option->name);\n goto err;\n }\n found = 1;\n break;\n }\n }\n if (!found) {\n TEST_info("Unknown test option: %s", option->name);\n goto err;\n }\n }\n }\n goto done;\n err:\n SSL_TEST_CTX_free(ctx);\n ctx = NULL;\n done:\n return ctx;\n}', 'STACK_OF(CONF_VALUE) *NCONF_get_section(const CONF *conf, const char *section)\n{\n if (conf == NULL) {\n CONFerr(CONF_F_NCONF_GET_SECTION, CONF_R_NO_CONF);\n return NULL;\n }\n if (section == NULL) {\n CONFerr(CONF_F_NCONF_GET_SECTION, CONF_R_NO_SECTION);\n return NULL;\n }\n return _CONF_get_section_values(conf, section);\n}', 'STACK_OF(CONF_VALUE) *_CONF_get_section_values(const CONF *conf,\n const char *section)\n{\n CONF_VALUE *v;\n v = _CONF_get_section(conf, section);\n if (v != NULL)\n return ((STACK_OF(CONF_VALUE) *)v->value);\n else\n return (NULL);\n}', 'CONF_VALUE *_CONF_get_section(const CONF *conf, const char *section)\n{\n CONF_VALUE *v, vv;\n if ((conf == NULL) || (section == NULL))\n return (NULL);\n vv.name = NULL;\n vv.section = (char *)section;\n v = lh_CONF_VALUE_retrieve(conf->data, &vv);\n return (v);\n}', 'void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)\n{\n unsigned long hash;\n OPENSSL_LH_NODE **rn;\n void *ret;\n int scratch;\n lh->error = 0;\n rn = getrn(lh, data, &hash);\n if (*rn == NULL) {\n CRYPTO_atomic_add(&lh->num_retrieve_miss, 1, &scratch, lh->retrieve_stats_lock);\n return NULL;\n } else {\n ret = (*rn)->data;\n CRYPTO_atomic_add(&lh->num_retrieve, 1, &scratch, lh->retrieve_stats_lock);\n }\n return ret;\n}', 'int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)\n{\n# if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL)\n if (__atomic_is_lock_free(sizeof(*val), val)) {\n *ret = __atomic_add_fetch(val, amount, __ATOMIC_ACQ_REL);\n return 1;\n }\n# endif\n if (!CRYPTO_THREAD_write_lock(lock))\n return 0;\n *val += amount;\n *ret = *val;\n if (!CRYPTO_THREAD_unlock(lock))\n return 0;\n return 1;\n}', 'int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock)\n{\n# ifdef USE_RWLOCK\n if (pthread_rwlock_wrlock(lock) != 0)\n return 0;\n# else\n if (pthread_mutex_lock(lock) != 0)\n return 0;\n# endif\n return 1;\n}', 'int test_ptr(const char *file, int line, const char *s, const void *p)\n{\n if (p != NULL)\n return 1;\n test_fail_message(NULL, file, line, "ptr", s, "NULL", "!=", "%p", p);\n return 0;\n}'] |
503 | 0 | https://github.com/openssl/openssl/blob/a4af39ac4482355ffdd61fb61231a0c79b96997b/crypto/lhash/lhash.c/#L359 | static void contract(LHASH *lh)
{
LHASH_NODE **n,*n1,*np;
np=lh->b[lh->p+lh->pmax-1];
lh->b[lh->p+lh->pmax-1]=NULL;
if (lh->p == 0)
{
n=(LHASH_NODE **)Realloc((char *)lh->b,
(unsigned int)(sizeof(LHASH_NODE *)*lh->pmax));
if (n == NULL)
{
lh->error++;
return;
}
lh->num_contract_reallocs++;
lh->num_alloc_nodes/=2;
lh->pmax/=2;
lh->p=lh->pmax-1;
lh->b=n;
}
else
lh->p--;
lh->num_nodes--;
lh->num_contracts++;
n1=lh->b[(int)lh->p];
if (n1 == NULL)
lh->b[(int)lh->p]=np;
else
{
while (n1->next != NULL)
n1=n1->next;
n1->next=np;
}
} | ['int MAIN(int argc, char **argv)\n\t{\n\tint ret=1,i;\n\tint verbose=0;\n\tchar **pp;\n\tconst char *p;\n\tint badops=0;\n\tSSL_CTX *ctx=NULL;\n\tSSL *ssl=NULL;\n\tchar *ciphers=NULL;\n\tSSL_METHOD *meth=NULL;\n\tSTACK_OF(SSL_CIPHER) *sk;\n\tchar buf[512];\n\tBIO *STDout=NULL;\n#if !defined(NO_SSL2) && !defined(NO_SSL3)\n\tmeth=SSLv23_server_method();\n#elif !defined(NO_SSL3)\n\tmeth=SSLv3_server_method();\n#elif !defined(NO_SSL2)\n\tmeth=SSLv2_server_method();\n#endif\n\tapps_startup();\n\tif (bio_err == NULL)\n\t\tbio_err=BIO_new_fp(stderr,BIO_NOCLOSE);\n\tSTDout=BIO_new_fp(stdout,BIO_NOCLOSE);\n\targc--;\n\targv++;\n\twhile (argc >= 1)\n\t\t{\n\t\tif (strcmp(*argv,"-v") == 0)\n\t\t\tverbose=1;\n#ifndef NO_SSL2\n\t\telse if (strcmp(*argv,"-ssl2") == 0)\n\t\t\tmeth=SSLv2_client_method();\n#endif\n#ifndef NO_SSL3\n\t\telse if (strcmp(*argv,"-ssl3") == 0)\n\t\t\tmeth=SSLv3_client_method();\n#endif\n\t\telse if ((strncmp(*argv,"-h",2) == 0) ||\n\t\t\t (strcmp(*argv,"-?") == 0))\n\t\t\t{\n\t\t\tbadops=1;\n\t\t\tbreak;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tciphers= *argv;\n\t\t\t}\n\t\targc--;\n\t\targv++;\n\t\t}\n\tif (badops)\n\t\t{\n\t\tfor (pp=ciphers_usage; (*pp != NULL); pp++)\n\t\t\tBIO_printf(bio_err,*pp);\n\t\tgoto end;\n\t\t}\n\tSSLeay_add_ssl_algorithms();\n\tctx=SSL_CTX_new(meth);\n\tif (ctx == NULL) goto err;\n\tif (ciphers != NULL)\n\t\tSSL_CTX_set_cipher_list(ctx,ciphers);\n\tssl=SSL_new(ctx);\n\tif (ssl == NULL) goto err;\n\tif (!verbose)\n\t\t{\n\t\tfor (i=0; ; i++)\n\t\t\t{\n\t\t\tp=SSL_get_cipher_list(ssl,i);\n\t\t\tif (p == NULL) break;\n\t\t\tif (i != 0) BIO_printf(STDout,":");\n\t\t\tBIO_printf(STDout,"%s",p);\n\t\t\t}\n\t\tBIO_printf(STDout,"\\n");\n\t\t}\n\telse\n\t\t{\n\t\tsk=SSL_get_ciphers(ssl);\n\t\tfor (i=0; i<sk_SSL_CIPHER_num(sk); i++)\n\t\t\t{\n\t\t\tBIO_puts(STDout,SSL_CIPHER_description(\n\t\t\t\tsk_SSL_CIPHER_value(sk,i),\n\t\t\t\tbuf,512));\n\t\t\t}\n\t\t}\n\tret=0;\n\tif (0)\n\t\t{\nerr:\n\t\tSSL_load_error_strings();\n\t\tERR_print_errors(bio_err);\n\t\t}\nend:\n\tif (ctx != NULL) SSL_CTX_free(ctx);\n\tif (ssl != NULL) SSL_free(ssl);\n\tif (STDout != NULL) BIO_free(STDout);\n\tEXIT(ret);\n\t}', '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->cert != NULL)\n\t\t{\n\t\ts->cert = ssl_cert_dup(ctx->cert);\n\t\tif (s->cert == NULL)\n\t\t\tgoto err;\n\t\t}\n\telse\n\t\ts->cert=NULL;\n\ts->sid_ctx_length=ctx->sid_ctx_length;\n\tmemcpy(&s->sid_ctx,&ctx->sid_ctx,sizeof(s->sid_ctx));\n\ts->verify_mode=ctx->verify_mode;\n\ts->verify_depth=ctx->verify_depth;\n\ts->verify_callback=ctx->default_verify_callback;\n\ts->purpose = ctx->purpose;\n\ts->trust = ctx->trust;\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\tgoto err;\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\ts->mode=ctx->mode;\n\tSSL_clear(s);\n\tCRYPTO_new_ex_data(ssl_meth,(char *)s,&s->ex_data);\n\treturn(s);\nerr:\n\tif (s != NULL)\n\t\t{\n\t\tif (s->cert != NULL)\n\t\t\tssl_cert_free(s->cert);\n\t\tif (s->ctx != NULL)\n\t\t\tSSL_CTX_free(s->ctx);\n\t\tFree(s);\n\t\t}\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{\n\treturn remove_session_lock(ctx, c, 1);\n}', 'static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)\n\t{\n\tSSL_SESSION *r;\n\tint ret=0;\n\tif ((c != NULL) && (c->session_id_length != 0))\n\t\t{\n\t\tif(lck) CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);\n\t\tr=(SSL_SESSION *)lh_delete(ctx->sessions,(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\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}', '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}'] |
504 | 1 | https://github.com/openssl/openssl/blob/3da2e9c4ee45989a426ff513dc6c6250d1e460de/crypto/bn/bn_shift.c/#L112 | int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)
{
int i, nw, lb, rb;
BN_ULONG *t, *f;
BN_ULONG l;
bn_check_top(r);
bn_check_top(a);
if (n < 0) {
BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);
return 0;
}
nw = n / BN_BITS2;
if (bn_wexpand(r, a->top + nw + 1) == NULL)
return 0;
r->neg = a->neg;
lb = n % BN_BITS2;
rb = BN_BITS2 - lb;
f = a->d;
t = r->d;
t[a->top + nw] = 0;
if (lb == 0)
for (i = a->top - 1; i >= 0; i--)
t[nw + i] = f[i];
else
for (i = a->top - 1; i >= 0; i--) {
l = f[i];
t[nw + i + 1] |= (l >> rb) & BN_MASK2;
t[nw + i] = (l << lb) & BN_MASK2;
}
memset(t, 0, sizeof(*t) * nw);
r->top = a->top + nw + 1;
bn_correct_top(r);
bn_check_top(r);
return 1;
} | ['static int ssl_srp_verify_param_cb(SSL *s, void *arg)\n{\n SRP_ARG *srp_arg = (SRP_ARG *)arg;\n BIGNUM *N = NULL, *g = NULL;\n if (((N = SSL_get_srp_N(s)) == NULL) || ((g = SSL_get_srp_g(s)) == NULL))\n return 0;\n if (srp_arg->debug || srp_arg->msg || srp_arg->amp == 1) {\n BIO_printf(bio_err, "SRP parameters:\\n");\n BIO_printf(bio_err, "\\tN=");\n BN_print(bio_err, N);\n BIO_printf(bio_err, "\\n\\tg=");\n BN_print(bio_err, g);\n BIO_printf(bio_err, "\\n");\n }\n if (SRP_check_known_gN_param(g, N))\n return 1;\n if (srp_arg->amp == 1) {\n if (srp_arg->debug)\n BIO_printf(bio_err,\n "SRP param N and g are not known params, going to check deeper.\\n");\n if (BN_num_bits(g) <= BN_BITS && srp_Verify_N_and_g(N, g))\n return 1;\n }\n BIO_printf(bio_err, "SRP param N and g rejected.\\n");\n return 0;\n}', 'int BN_print(BIO *bp, const BIGNUM *a)\n{\n int i, j, v, z = 0;\n int ret = 0;\n if ((a->neg) && BIO_write(bp, "-", 1) != 1)\n goto end;\n if (BN_is_zero(a) && BIO_write(bp, "0", 1) != 1)\n goto end;\n for (i = a->top - 1; i >= 0; i--) {\n for (j = BN_BITS2 - 4; j >= 0; j -= 4) {\n v = (int)((a->d[i] >> j) & 0x0f);\n if (z || v != 0) {\n if (BIO_write(bp, &Hex[v], 1) != 1)\n goto end;\n z = 1;\n }\n }\n }\n ret = 1;\n end:\n return ret;\n}', 'char *SRP_check_known_gN_param(const BIGNUM *g, const BIGNUM *N)\n{\n size_t i;\n if ((g == NULL) || (N == NULL))\n return NULL;\n for (i = 0; i < KNOWN_GN_NUMBER; i++) {\n if (BN_cmp(knowngN[i].g, g) == 0 && BN_cmp(knowngN[i].N, N) == 0)\n return knowngN[i].id;\n }\n return NULL;\n}', 'int BN_num_bits(const BIGNUM *a)\n{\n int i = a->top - 1;\n bn_check_top(a);\n if (BN_is_zero(a))\n return 0;\n return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));\n}', 'int BN_is_zero(const BIGNUM *a)\n{\n return a->top == 0;\n}', '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_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n BN_CTX *ctx)\n{\n int ret;\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n if (BN_is_odd(m)) {\n# ifdef MONT_EXP_WORD\n if (a->top == 1 && !a->neg\n && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(a, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(m, BN_FLG_CONSTTIME) == 0)) {\n BN_ULONG A = a->d[0];\n ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);\n } else\n# endif\n ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);\n } else\n#endif\n#ifdef RECP_MUL_MOD\n {\n ret = BN_mod_exp_recp(r, a, p, m, ctx);\n }\n#else\n {\n ret = BN_mod_exp_simple(r, a, p, m, ctx);\n }\n#endif\n bn_check_top(r);\n return ret;\n}', 'int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n int start = 1;\n BIGNUM *d, *r;\n const BIGNUM *aa;\n BIGNUM *val[TABLE_SIZE];\n BN_MONT_CTX *mont = NULL;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(a, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont);\n }\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_abs_is_word(m, 1)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n d = BN_CTX_get(ctx);\n r = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (val[0] == NULL)\n goto err;\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n if (a->neg || BN_ucmp(a, m) >= 0) {\n if (!BN_nnmod(val[0], a, m, ctx))\n goto err;\n aa = val[0];\n } else\n aa = a;\n if (!bn_to_mont_fixed_top(val[0], aa, mont, ctx))\n goto err;\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!bn_mul_mont_fixed_top(d, val[0], val[0], mont, ctx))\n goto err;\n j = 1 << (window - 1);\n for (i = 1; i < j; i++) {\n if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n !bn_mul_mont_fixed_top(val[i], val[i - 1], d, mont, ctx))\n goto err;\n }\n }\n start = 1;\n wvalue = 0;\n wstart = bits - 1;\n wend = 0;\n#if 1\n j = m->top;\n if (m->d[j - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n if (bn_wexpand(r, j) == NULL)\n goto err;\n r->d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < j; i++)\n r->d[i] = (~m->d[i]) & BN_MASK2;\n r->top = j;\n r->flags |= BN_FLG_FIXED_TOP;\n } else\n#endif\n if (!bn_to_mont_fixed_top(r, BN_value_one(), mont, ctx))\n goto err;\n for (;;) {\n if (BN_is_bit_set(p, wstart) == 0) {\n if (!start) {\n if (!bn_mul_mont_fixed_top(r, r, r, mont, ctx))\n goto err;\n }\n if (wstart == 0)\n break;\n wstart--;\n continue;\n }\n j = wstart;\n wvalue = 1;\n wend = 0;\n for (i = 1; i < window; i++) {\n if (wstart - i < 0)\n break;\n if (BN_is_bit_set(p, wstart - i)) {\n wvalue <<= (i - wend);\n wvalue |= 1;\n wend = i;\n }\n }\n j = wend + 1;\n if (!start)\n for (i = 0; i < j; i++) {\n if (!bn_mul_mont_fixed_top(r, r, r, mont, ctx))\n goto err;\n }\n if (!bn_mul_mont_fixed_top(r, r, val[wvalue >> 1], mont, ctx))\n goto err;\n wstart -= wend + 1;\n wvalue = 0;\n start = 0;\n if (wstart < 0)\n break;\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n j = mont->N.top;\n val[0]->d[0] = 1;\n for (i = 1; i < j; i++)\n val[0]->d[i] = 0;\n val[0]->top = j;\n if (!BN_mod_mul_montgomery(rr, r, val[0], mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, r, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n BN_CTX_end(ctx);\n bn_check_top(rr);\n return ret;\n}', 'int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx,\n BN_MONT_CTX *in_mont)\n{\n int i, bits, ret = 0, window, wvalue, wmask, window0;\n int top;\n BN_MONT_CTX *mont = NULL;\n int numPowers;\n unsigned char *powerbufFree = NULL;\n int powerbufLen = 0;\n unsigned char *powerbuf = NULL;\n BIGNUM tmp, am;\n#if defined(SPARC_T4_MONT)\n unsigned int t4 = 0;\n#endif\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT_CONSTTIME, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n top = m->top;\n bits = p->top * BN_BITS2;\n if (bits == 0) {\n if (BN_abs_is_word(m, 1)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n#ifdef RSAZ_ENABLED\n if (!a->neg) {\n if ((16 == a->top) && (16 == p->top) && (BN_num_bits(m) == 1024)\n && rsaz_avx2_eligible()) {\n if (NULL == bn_wexpand(rr, 16))\n goto err;\n RSAZ_1024_mod_exp_avx2(rr->d, a->d, p->d, m->d, mont->RR.d,\n mont->n0[0]);\n rr->top = 16;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n } else if ((8 == a->top) && (8 == p->top) && (BN_num_bits(m) == 512)) {\n if (NULL == bn_wexpand(rr, 8))\n goto err;\n RSAZ_512_mod_exp(rr->d, a->d, p->d, m->d, mont->n0[0], mont->RR.d);\n rr->top = 8;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n }\n }\n#endif\n window = BN_window_bits_for_ctime_exponent_size(bits);\n#if defined(SPARC_T4_MONT)\n if (window >= 5 && (top & 15) == 0 && top <= 64 &&\n (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) ==\n (CFR_MONTMUL | CFR_MONTSQR) && (t4 = OPENSSL_sparcv9cap_P[0]))\n window = 5;\n else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window >= 5) {\n window = 5;\n powerbufLen += top * sizeof(mont->N.d[0]);\n }\n#endif\n (void)0;\n numPowers = 1 << window;\n powerbufLen += sizeof(m->d[0]) * (top * numPowers +\n ((2 * top) >\n numPowers ? (2 * top) : numPowers));\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree =\n alloca(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH);\n else\n#endif\n if ((powerbufFree =\n OPENSSL_malloc(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH))\n == NULL)\n goto err;\n powerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);\n memset(powerbuf, 0, powerbufLen);\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree = NULL;\n#endif\n tmp.d = (BN_ULONG *)(powerbuf + sizeof(m->d[0]) * top * numPowers);\n am.d = tmp.d + top;\n tmp.top = am.top = 0;\n tmp.dmax = am.dmax = top;\n tmp.neg = am.neg = 0;\n tmp.flags = am.flags = BN_FLG_STATIC_DATA;\n#if 1\n if (m->d[top - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n tmp.d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < top; i++)\n tmp.d[i] = (~m->d[i]) & BN_MASK2;\n tmp.top = top;\n } else\n#endif\n if (!bn_to_mont_fixed_top(&tmp, BN_value_one(), mont, ctx))\n goto err;\n if (a->neg || BN_ucmp(a, m) >= 0) {\n if (!BN_nnmod(&am, a, m, ctx))\n goto err;\n if (!bn_to_mont_fixed_top(&am, &am, mont, ctx))\n goto err;\n } else if (!bn_to_mont_fixed_top(&am, a, mont, ctx))\n goto err;\n#if defined(SPARC_T4_MONT)\n if (t4) {\n typedef int (*bn_pwr5_mont_f) (BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_8(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_16(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_24(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_32(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n static const bn_pwr5_mont_f pwr5_funcs[4] = {\n bn_pwr5_mont_t4_8, bn_pwr5_mont_t4_16,\n bn_pwr5_mont_t4_24, bn_pwr5_mont_t4_32\n };\n bn_pwr5_mont_f pwr5_worker = pwr5_funcs[top / 16 - 1];\n typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap, const void *bp,\n const BN_ULONG *np, const BN_ULONG *n0);\n int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n static const bn_mul_mont_f mul_funcs[4] = {\n bn_mul_mont_t4_8, bn_mul_mont_t4_16,\n bn_mul_mont_t4_24, bn_mul_mont_t4_32\n };\n bn_mul_mont_f mul_worker = mul_funcs[top / 16 - 1];\n void bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_gather5_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_flip_n_scatter5_t4(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5_t4(BN_ULONG *out, size_t num,\n void *table, size_t power);\n void bn_flip_t4(BN_ULONG *dst, BN_ULONG *src, size_t num);\n BN_ULONG *np = mont->N.d, *n0 = mont->n0;\n int stride = 5 * (6 - (top / 16 - 1));\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 0);\n bn_flip_n_scatter5_t4(am.d, top, powerbuf, 1);\n if (!(*mul_worker) (tmp.d, am.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, am.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, am.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 2);\n for (i = 3; i < 32; i++) {\n if (!(*mul_worker) (tmp.d, tmp.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, tmp.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, tmp.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, i);\n }\n np = alloca(top * sizeof(BN_ULONG));\n top /= 2;\n bn_flip_t4(np, mont->N.d, top);\n window0 = (bits - 1) % 5 + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n bn_gather5_t4(tmp.d, top, powerbuf, wvalue);\n while (bits > 0) {\n if (bits < stride)\n stride = bits;\n bits -= stride;\n wvalue = bn_get_bits(p, bits);\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n bits += stride - 5;\n wvalue >>= stride - 5;\n wvalue &= 31;\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5_t4(tmp.d, tmp.d, powerbuf, np, n0, top,\n wvalue);\n }\n bn_flip_t4(tmp.d, tmp.d, top);\n top *= 2;\n tmp.top = top;\n bn_correct_top(&tmp);\n OPENSSL_cleanse(np, top * sizeof(BN_ULONG));\n } else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window == 5 && top > 1) {\n void bn_mul_mont_gather5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_scatter5(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5(BN_ULONG *out, size_t num, void *table, size_t power);\n void bn_power5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n int bn_get_bits5(const BN_ULONG *ap, int off);\n int bn_from_montgomery(BN_ULONG *rp, const BN_ULONG *ap,\n const BN_ULONG *not_used, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n BN_ULONG *n0 = mont->n0, *np;\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n for (np = am.d + top, i = 0; i < top; i++)\n np[i] = mont->N.d[i];\n bn_scatter5(tmp.d, top, powerbuf, 0);\n bn_scatter5(am.d, am.top, powerbuf, 1);\n bn_mul_mont(tmp.d, am.d, am.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2);\n# if 0\n for (i = 3; i < 32; i++) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# else\n for (i = 4; i < 32; i *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n for (i = 3; i < 8; i += 2) {\n int j;\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n for (j = 2 * i; j < 32; j *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, j);\n }\n }\n for (; i < 16; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2 * i);\n }\n for (; i < 32; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# endif\n window0 = (bits - 1) % 5 + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n bn_gather5(tmp.d, top, powerbuf, wvalue);\n if (top & 7) {\n while (bits > 0) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5(tmp.d, tmp.d, powerbuf, np, n0, top,\n bn_get_bits5(p->d, bits -= 5));\n }\n } else {\n while (bits > 0) {\n bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top,\n bn_get_bits5(p->d, bits -= 5));\n }\n }\n ret = bn_from_montgomery(tmp.d, tmp.d, NULL, np, n0, top);\n tmp.top = top;\n bn_correct_top(&tmp);\n if (ret) {\n if (!BN_copy(rr, &tmp))\n ret = 0;\n goto err;\n }\n } else\n#endif\n {\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 0, window))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, window))\n goto err;\n if (window > 1) {\n if (!bn_mul_mont_fixed_top(&tmp, &am, &am, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 2,\n window))\n goto err;\n for (i = 3; i < numPowers; i++) {\n if (!bn_mul_mont_fixed_top(&tmp, &am, &tmp, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, i,\n window))\n goto err;\n }\n }\n window0 = (bits - 1) % window + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&tmp, top, powerbuf, wvalue,\n window))\n goto err;\n wmask = (1 << window) - 1;\n while (bits > 0) {\n for (i = 0; i < window; i++)\n if (!bn_mul_mont_fixed_top(&tmp, &tmp, &tmp, mont, ctx))\n goto err;\n bits -= window;\n wvalue = bn_get_bits(p, bits) & wmask;\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&am, top, powerbuf, wvalue,\n window))\n goto err;\n if (!bn_mul_mont_fixed_top(&tmp, &tmp, &am, mont, ctx))\n goto err;\n }\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n am.d[0] = 1;\n for (i = 1; i < top; i++)\n am.d[i] = 0;\n if (!BN_mod_mul_montgomery(rr, &tmp, &am, mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, &tmp, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n if (powerbuf != NULL) {\n OPENSSL_cleanse(powerbuf, powerbufLen);\n OPENSSL_free(powerbufFree);\n }\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, j, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return 0;\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return 0;\n }\n if (dv != NULL)\n BN_zero(dv);\n return 1;\n }\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.flags = BN_FLG_STATIC_DATA;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n resp++;\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W)\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n l0 = bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1);\n q -= l0;\n for (l0 = 0 - l0, j = 0; j < div_n; j++)\n tmp->d[j] = sdiv->d[j] & l0;\n l0 = bn_add_words(wnum.d, wnum.d, tmp->d, div_n);\n (*wnump) += l0;\n resp--;\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return 0;\n}', 'int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l;\n bn_check_top(r);\n bn_check_top(a);\n if (n < 0) {\n BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n nw = n / BN_BITS2;\n if (bn_wexpand(r, a->top + nw + 1) == NULL)\n return 0;\n r->neg = a->neg;\n lb = n % BN_BITS2;\n rb = BN_BITS2 - lb;\n f = a->d;\n t = r->d;\n t[a->top + nw] = 0;\n if (lb == 0)\n for (i = a->top - 1; i >= 0; i--)\n t[nw + i] = f[i];\n else\n for (i = a->top - 1; i >= 0; i--) {\n l = f[i];\n t[nw + i + 1] |= (l >> rb) & BN_MASK2;\n t[nw + i] = (l << lb) & BN_MASK2;\n }\n memset(t, 0, sizeof(*t) * nw);\n r->top = a->top + nw + 1;\n bn_correct_top(r);\n bn_check_top(r);\n return 1;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}'] |
505 | 0 | https://github.com/openssl/openssl/blob/e3713c365c2657236439fea00822a43aa396d112/crypto/bn/bn_lib.c/#L322 | BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
{
bn_check_top(b);
if (a == b)
return a;
if (bn_wexpand(a, b->top) == NULL)
return NULL;
if (b->top > 0)
memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);
if (BN_get_flags(b, BN_FLG_CONSTTIME) != 0)
BN_set_flags(a, BN_FLG_CONSTTIME);
a->top = b->top;
a->neg = b->neg;
bn_check_top(a);
return a;
} | ['static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)\n{\n BIGNUM *r1, *m1, *vrfy;\n int ret = 0;\n BN_CTX_start(ctx);\n r1 = BN_CTX_get(ctx);\n m1 = BN_CTX_get(ctx);\n vrfy = BN_CTX_get(ctx);\n if (vrfy == NULL)\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 }\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 (!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 (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}', 'void BN_with_flags(BIGNUM *dest, const BIGNUM *b, int flags)\n{\n dest->d = b->d;\n dest->top = b->top;\n dest->dmax = b->dmax;\n dest->neg = b->neg;\n dest->flags = ((dest->flags & BN_FLG_MALLOCED)\n | (b->flags & ~BN_FLG_MALLOCED)\n | BN_FLG_STATIC_DATA | flags);\n}', 'BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, CRYPTO_RWLOCK *lock,\n const BIGNUM *mod, BN_CTX *ctx)\n{\n BN_MONT_CTX *ret;\n CRYPTO_THREAD_read_lock(lock);\n ret = *pmont;\n CRYPTO_THREAD_unlock(lock);\n if (ret)\n return ret;\n ret = BN_MONT_CTX_new();\n if (ret == NULL)\n return NULL;\n if (!BN_MONT_CTX_set(ret, mod, ctx)) {\n BN_MONT_CTX_free(ret);\n return NULL;\n }\n CRYPTO_THREAD_write_lock(lock);\n if (*pmont) {\n BN_MONT_CTX_free(ret);\n ret = *pmont;\n } else\n *pmont = ret;\n CRYPTO_THREAD_unlock(lock);\n return ret;\n}', 'int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)\n{\n int ret = 0;\n BIGNUM *Ri, *R;\n if (BN_is_zero(mod))\n return 0;\n BN_CTX_start(ctx);\n if ((Ri = BN_CTX_get(ctx)) == NULL)\n goto err;\n R = &(mont->RR);\n if (!BN_copy(&(mont->N), mod))\n goto err;\n mont->N.neg = 0;\n#ifdef MONT_WORD\n {\n BIGNUM tmod;\n BN_ULONG buf[2];\n bn_init(&tmod);\n tmod.d = buf;\n tmod.dmax = 2;\n tmod.neg = 0;\n if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)\n BN_set_flags(&tmod, BN_FLG_CONSTTIME);\n mont->ri = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2;\n# if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)\n BN_zero(R);\n if (!(BN_set_bit(R, 2 * BN_BITS2)))\n goto err;\n tmod.top = 0;\n if ((buf[0] = mod->d[0]))\n tmod.top = 1;\n if ((buf[1] = mod->top > 1 ? mod->d[1] : 0))\n tmod.top = 2;\n if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, 2 * BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (bn_expand(Ri, (int)sizeof(BN_ULONG) * 2) == NULL)\n goto err;\n Ri->neg = 0;\n Ri->d[0] = BN_MASK2;\n Ri->d[1] = BN_MASK2;\n Ri->top = 2;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;\n# else\n BN_zero(R);\n if (!(BN_set_bit(R, BN_BITS2)))\n goto err;\n buf[0] = mod->d[0];\n buf[1] = 0;\n tmod.top = buf[0] != 0 ? 1 : 0;\n if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (!BN_set_word(Ri, BN_MASK2))\n goto err;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = 0;\n# endif\n }\n#else\n {\n mont->ri = BN_num_bits(&mont->N);\n BN_zero(R);\n if (!BN_set_bit(R, mont->ri))\n goto err;\n if ((BN_mod_inverse(Ri, R, &mont->N, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, mont->ri))\n goto err;\n if (!BN_sub_word(Ri, 1))\n goto err;\n if (!BN_div(&(mont->Ni), NULL, Ri, &mont->N, ctx))\n goto err;\n }\n#endif\n BN_zero(&(mont->RR));\n if (!BN_set_bit(&(mont->RR), mont->ri * 2))\n goto err;\n if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx))\n goto err;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n bn_check_top(b);\n if (a == b)\n return a;\n if (bn_wexpand(a, b->top) == NULL)\n return NULL;\n if (b->top > 0)\n memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n if (BN_get_flags(b, BN_FLG_CONSTTIME) != 0)\n BN_set_flags(a, BN_FLG_CONSTTIME);\n a->top = b->top;\n a->neg = b->neg;\n bn_check_top(a);\n return a;\n}'] |
506 | 0 | https://github.com/openssl/openssl/blob/6bf24568bc1e263348efd0394b30cc7492992127/crypto/bn/bn_add.c/#L230 | int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
{
int max,min,dif;
register BN_ULONG t1,t2,*ap,*bp,*rp;
int i,carry;
#if defined(IRIX_CC_BUG) && !defined(LINT)
int dummy;
#endif
bn_check_top(a);
bn_check_top(b);
max = a->top;
min = b->top;
dif = max - min;
if (dif < 0)
{
BNerr(BN_F_BN_USUB,BN_R_ARG2_LT_ARG3);
return(0);
}
if (bn_wexpand(r,max) == NULL) return(0);
ap=a->d;
bp=b->d;
rp=r->d;
#if 1
carry=0;
for (i = min; i != 0; i--)
{
t1= *(ap++);
t2= *(bp++);
if (carry)
{
carry=(t1 <= t2);
t1=(t1-t2-1)&BN_MASK2;
}
else
{
carry=(t1 < t2);
t1=(t1-t2)&BN_MASK2;
}
#if defined(IRIX_CC_BUG) && !defined(LINT)
dummy=t1;
#endif
*(rp++)=t1&BN_MASK2;
}
#else
carry=bn_sub_words(rp,ap,bp,min);
ap+=min;
bp+=min;
rp+=min;
#endif
if (carry)
{
if (!dif)
return 0;
while (dif)
{
dif--;
t1 = *(ap++);
t2 = (t1-1)&BN_MASK2;
*(rp++) = t2;
if (t1)
break;
}
}
#if 0
memcpy(rp,ap,sizeof(*rp)*(max-i));
#else
if (rp != ap)
{
for (;;)
{
if (!dif--) break;
rp[0]=ap[0];
if (!dif--) break;
rp[1]=ap[1];
if (!dif--) break;
rp[2]=ap[2];
if (!dif--) break;
rp[3]=ap[3];
rp+=4;
ap+=4;
}
}
#endif
r->top=max;
r->neg=0;
bn_correct_top(r);
return(1);
} | ['int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,\n\tBN_CTX *ctx)\n\t{\n\tint\ttop = a->top, i;\n\tint\tcarry;\n\tBN_ULONG *r_d, *a_d = a->d;\n\tBN_ULONG t_d[BN_NIST_224_TOP],\n\t buf[BN_NIST_224_TOP],\n\t\t c_d[BN_NIST_224_TOP],\n\t\t*res;\n\tsize_t mask;\n\tunion { bn_addsub_f f; size_t p; } u;\n\tfield = &_bignum_nist_p_224;\n \tif (BN_is_negative(a) || a->top > 2*BN_NIST_224_TOP)\n\t\treturn BN_nnmod(r, field, a, ctx);\n\ti = BN_ucmp(field, a);\n\tif (i == 0)\n\t\t{\n\t\tBN_zero(r);\n\t\treturn 1;\n\t\t}\n\telse if (i > 0)\n\t\treturn (r == a)? 1 : (BN_copy(r ,a) != NULL);\n\tif (r != a)\n\t\t{\n\t\tif (!bn_wexpand(r, BN_NIST_224_TOP))\n\t\t\treturn 0;\n\t\tr_d = r->d;\n\t\tnist_cp_bn(r_d, a_d, BN_NIST_224_TOP);\n\t\t}\n\telse\n\t\tr_d = a_d;\n#if BN_BITS2==64\n\tnist_cp_bn_0(t_d, a_d + (BN_NIST_224_TOP-1), top - (BN_NIST_224_TOP-1), BN_NIST_224_TOP);\n\tnist_set_224(buf, t_d, 14, 13, 12, 11, 10, 9, 8);\n\tr_d[BN_NIST_224_TOP-1] &= BN_MASK2l;\n#else\n\tnist_cp_bn_0(buf, a_d + BN_NIST_224_TOP, top - BN_NIST_224_TOP, BN_NIST_224_TOP);\n#endif\n\tnist_set_224(t_d, buf, 10, 9, 8, 7, 0, 0, 0);\n\tcarry = bn_add_words(r_d, r_d, t_d, BN_NIST_224_TOP);\n\tnist_set_224(t_d, buf, 0, 13, 12, 11, 0, 0, 0);\n\tcarry += bn_add_words(r_d, r_d, t_d, BN_NIST_224_TOP);\n\tnist_set_224(t_d, buf, 13, 12, 11, 10, 9, 8, 7);\n\tcarry -= bn_sub_words(r_d, r_d, t_d, BN_NIST_224_TOP);\n\tnist_set_224(t_d, buf, 0, 0, 0, 0, 13, 12, 11);\n\tcarry -= bn_sub_words(r_d, r_d, t_d, BN_NIST_224_TOP);\n#if BN_BITS2==64\n\tcarry = (int)(r_d[BN_NIST_224_TOP-1]>>32);\n#endif\n\tu.f = bn_sub_words;\n\tif (carry > 0)\n\t\t{\n\t\tcarry = bn_sub_words(r_d,r_d,_nist_p_224[carry-1],BN_NIST_224_TOP);\n#if BN_BITS2==64\n\t\tcarry=(int)(~(r_d[BN_NIST_224_TOP-1]>>32))&1;\n#endif\n\t\t}\n\telse if (carry < 0)\n\t\t{\n\t\tcarry = bn_add_words(r_d,r_d,_nist_p_224[-carry-1],BN_NIST_224_TOP);\n\t\tmask = 0-(size_t)carry;\n\t\tu.p = ((size_t)bn_sub_words&mask) | ((size_t)bn_add_words&~mask);\n\t\t}\n\telse\n\t\tcarry = 1;\n\tmask = 0-(size_t)(*u.f)(c_d,r_d,_nist_p_224[0],BN_NIST_224_TOP);\n\tmask &= 0-(size_t)carry;\n\tres = (BN_ULONG *)(((size_t)c_d&~mask) | ((size_t)r_d&mask));\n\tnist_cp_bn(r_d, res, BN_NIST_224_TOP);\n\tr->top = BN_NIST_224_TOP;\n\tbn_correct_top(r);\n\tif (BN_ucmp(field, r) <= 0)\n\t\t{\n\t\tif (!BN_usub(r, r, field)) return 0;\n\t\t}\n\treturn 1;\n\t}', 'static void nist_cp_bn(BN_ULONG *buf, BN_ULONG *a, int top)\n\t{\n\tint i;\n\tBN_ULONG *_tmp1 = (buf), *_tmp2 = (a);\n\tfor (i = (top); i != 0; i--)\n\t\t*_tmp1++ = *_tmp2++;\n\t}', 'int BN_ucmp(const BIGNUM *a, const BIGNUM *b)\n\t{\n\tint i;\n\tBN_ULONG t1,t2,*ap,*bp;\n\tbn_check_top(a);\n\tbn_check_top(b);\n\ti=a->top-b->top;\n\tif (i != 0) return(i);\n\tap=a->d;\n\tbp=b->d;\n\tfor (i=a->top-1; i>=0; i--)\n\t\t{\n\t\tt1= ap[i];\n\t\tt2= bp[i];\n\t\tif (t1 != t2)\n\t\t\treturn((t1 > t2) ? 1 : -1);\n\t\t}\n\treturn(0);\n\t}', 'int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n\t{\n\tint max,min,dif;\n\tregister BN_ULONG t1,t2,*ap,*bp,*rp;\n\tint i,carry;\n#if defined(IRIX_CC_BUG) && !defined(LINT)\n\tint dummy;\n#endif\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tmax = a->top;\n\tmin = b->top;\n\tdif = max - min;\n\tif (dif < 0)\n\t\t{\n\t\tBNerr(BN_F_BN_USUB,BN_R_ARG2_LT_ARG3);\n\t\treturn(0);\n\t\t}\n\tif (bn_wexpand(r,max) == NULL) return(0);\n\tap=a->d;\n\tbp=b->d;\n\trp=r->d;\n#if 1\n\tcarry=0;\n\tfor (i = min; i != 0; i--)\n\t\t{\n\t\tt1= *(ap++);\n\t\tt2= *(bp++);\n\t\tif (carry)\n\t\t\t{\n\t\t\tcarry=(t1 <= t2);\n\t\t\tt1=(t1-t2-1)&BN_MASK2;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tcarry=(t1 < t2);\n\t\t\tt1=(t1-t2)&BN_MASK2;\n\t\t\t}\n#if defined(IRIX_CC_BUG) && !defined(LINT)\n\t\tdummy=t1;\n#endif\n\t\t*(rp++)=t1&BN_MASK2;\n\t\t}\n#else\n\tcarry=bn_sub_words(rp,ap,bp,min);\n\tap+=min;\n\tbp+=min;\n\trp+=min;\n#endif\n\tif (carry)\n\t\t{\n\t\tif (!dif)\n\t\t\treturn 0;\n\t\twhile (dif)\n\t\t\t{\n\t\t\tdif--;\n\t\t\tt1 = *(ap++);\n\t\t\tt2 = (t1-1)&BN_MASK2;\n\t\t\t*(rp++) = t2;\n\t\t\tif (t1)\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n#if 0\n\tmemcpy(rp,ap,sizeof(*rp)*(max-i));\n#else\n\tif (rp != ap)\n\t\t{\n\t\tfor (;;)\n\t\t\t{\n\t\t\tif (!dif--) break;\n\t\t\trp[0]=ap[0];\n\t\t\tif (!dif--) break;\n\t\t\trp[1]=ap[1];\n\t\t\tif (!dif--) break;\n\t\t\trp[2]=ap[2];\n\t\t\tif (!dif--) break;\n\t\t\trp[3]=ap[3];\n\t\t\trp+=4;\n\t\t\tap+=4;\n\t\t\t}\n\t\t}\n#endif\n\tr->top=max;\n\tr->neg=0;\n\tbn_correct_top(r);\n\treturn(1);\n\t}'] |
507 | 0 | https://github.com/openssl/openssl/blob/7141ba31969d0b378d08104a51f8f99b9187b9d5/ssl/statem/statem_clnt.c/#L2474 | 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;
# ifdef PKCS1_CHECK
if (s->options & SSL_OP_PKCS1_CHECK_1)
(*p)[1]++;
if (s->options & SSL_OP_PKCS1_CHECK_2)
tmp_buf[0] = 0x70;
# endif
if (s->version > SSL3_VERSION && !WPACKET_close(pkt)) {
SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);
goto err;
}
s->s3->tmp.pms = pms;
s->s3->tmp.pmslen = pmslen;
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# ifdef PKCS1_CHECK\n if (s->options & SSL_OP_PKCS1_CHECK_1)\n (*p)[1]++;\n if (s->options & SSL_OP_PKCS1_CHECK_2)\n tmp_buf[0] = 0x70;\n# endif\n if (s->version > SSL3_VERSION && !WPACKET_close(pkt)) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n s->s3->tmp.pms = pms;\n s->s3->tmp.pmslen = pmslen;\n 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}', 'static int x509_pubkey_decode(EVP_PKEY **ppkey, X509_PUBKEY *key)\n {\n EVP_PKEY *pkey = EVP_PKEY_new();\n if (pkey == NULL) {\n X509err(X509_F_X509_PUBKEY_DECODE, ERR_R_MALLOC_FAILURE);\n return -1;\n }\n if (!EVP_PKEY_set_type(pkey, OBJ_obj2nid(key->algor->algorithm))) {\n X509err(X509_F_X509_PUBKEY_DECODE, X509_R_UNSUPPORTED_ALGORITHM);\n goto error;\n }\n if (pkey->ameth->pub_decode) {\n if (!pkey->ameth->pub_decode(pkey, key)) {\n X509err(X509_F_X509_PUBKEY_DECODE, X509_R_PUBLIC_KEY_DECODE_ERROR);\n goto error;\n }\n } else {\n X509err(X509_F_X509_PUBKEY_DECODE, X509_R_METHOD_NOT_SUPPORTED);\n goto error;\n }\n *ppkey = pkey;\n return 1;\n error:\n EVP_PKEY_free(pkey);\n return 0;\n}', 'RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey)\n{\n if (pkey->type != EVP_PKEY_RSA) {\n EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY);\n return NULL;\n }\n return pkey->pkey.rsa;\n}'] |
508 | 0 | https://github.com/openssl/openssl/blob/84c15db551ce1d167b901a3bde2b21880b084384/crypto/bn/bn_asm.c/#L542 | void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
{
#ifdef BN_LLONG
BN_ULLONG t;
#else
BN_ULONG bl,bh;
#endif
BN_ULONG t1,t2;
BN_ULONG c1,c2,c3;
c1=0;
c2=0;
c3=0;
mul_add_c(a[0],b[0],c1,c2,c3);
r[0]=c1;
c1=0;
mul_add_c(a[0],b[1],c2,c3,c1);
mul_add_c(a[1],b[0],c2,c3,c1);
r[1]=c2;
c2=0;
mul_add_c(a[2],b[0],c3,c1,c2);
mul_add_c(a[1],b[1],c3,c1,c2);
mul_add_c(a[0],b[2],c3,c1,c2);
r[2]=c3;
c3=0;
mul_add_c(a[0],b[3],c1,c2,c3);
mul_add_c(a[1],b[2],c1,c2,c3);
mul_add_c(a[2],b[1],c1,c2,c3);
mul_add_c(a[3],b[0],c1,c2,c3);
r[3]=c1;
c1=0;
mul_add_c(a[4],b[0],c2,c3,c1);
mul_add_c(a[3],b[1],c2,c3,c1);
mul_add_c(a[2],b[2],c2,c3,c1);
mul_add_c(a[1],b[3],c2,c3,c1);
mul_add_c(a[0],b[4],c2,c3,c1);
r[4]=c2;
c2=0;
mul_add_c(a[0],b[5],c3,c1,c2);
mul_add_c(a[1],b[4],c3,c1,c2);
mul_add_c(a[2],b[3],c3,c1,c2);
mul_add_c(a[3],b[2],c3,c1,c2);
mul_add_c(a[4],b[1],c3,c1,c2);
mul_add_c(a[5],b[0],c3,c1,c2);
r[5]=c3;
c3=0;
mul_add_c(a[6],b[0],c1,c2,c3);
mul_add_c(a[5],b[1],c1,c2,c3);
mul_add_c(a[4],b[2],c1,c2,c3);
mul_add_c(a[3],b[3],c1,c2,c3);
mul_add_c(a[2],b[4],c1,c2,c3);
mul_add_c(a[1],b[5],c1,c2,c3);
mul_add_c(a[0],b[6],c1,c2,c3);
r[6]=c1;
c1=0;
mul_add_c(a[0],b[7],c2,c3,c1);
mul_add_c(a[1],b[6],c2,c3,c1);
mul_add_c(a[2],b[5],c2,c3,c1);
mul_add_c(a[3],b[4],c2,c3,c1);
mul_add_c(a[4],b[3],c2,c3,c1);
mul_add_c(a[5],b[2],c2,c3,c1);
mul_add_c(a[6],b[1],c2,c3,c1);
mul_add_c(a[7],b[0],c2,c3,c1);
r[7]=c2;
c2=0;
mul_add_c(a[7],b[1],c3,c1,c2);
mul_add_c(a[6],b[2],c3,c1,c2);
mul_add_c(a[5],b[3],c3,c1,c2);
mul_add_c(a[4],b[4],c3,c1,c2);
mul_add_c(a[3],b[5],c3,c1,c2);
mul_add_c(a[2],b[6],c3,c1,c2);
mul_add_c(a[1],b[7],c3,c1,c2);
r[8]=c3;
c3=0;
mul_add_c(a[2],b[7],c1,c2,c3);
mul_add_c(a[3],b[6],c1,c2,c3);
mul_add_c(a[4],b[5],c1,c2,c3);
mul_add_c(a[5],b[4],c1,c2,c3);
mul_add_c(a[6],b[3],c1,c2,c3);
mul_add_c(a[7],b[2],c1,c2,c3);
r[9]=c1;
c1=0;
mul_add_c(a[7],b[3],c2,c3,c1);
mul_add_c(a[6],b[4],c2,c3,c1);
mul_add_c(a[5],b[5],c2,c3,c1);
mul_add_c(a[4],b[6],c2,c3,c1);
mul_add_c(a[3],b[7],c2,c3,c1);
r[10]=c2;
c2=0;
mul_add_c(a[4],b[7],c3,c1,c2);
mul_add_c(a[5],b[6],c3,c1,c2);
mul_add_c(a[6],b[5],c3,c1,c2);
mul_add_c(a[7],b[4],c3,c1,c2);
r[11]=c3;
c3=0;
mul_add_c(a[7],b[5],c1,c2,c3);
mul_add_c(a[6],b[6],c1,c2,c3);
mul_add_c(a[5],b[7],c1,c2,c3);
r[12]=c1;
c1=0;
mul_add_c(a[6],b[7],c2,c3,c1);
mul_add_c(a[7],b[6],c2,c3,c1);
r[13]=c2;
c2=0;
mul_add_c(a[7],b[7],c3,c1,c2);
r[14]=c3;
r[15]=c1;
} | ['int DH_generate_key(DH *dh)\n\t{\n\tint ok=0;\n\tunsigned int i;\n\tBN_CTX ctx;\n\tBN_MONT_CTX *mont;\n\tBIGNUM *pub_key=NULL,*priv_key=NULL;\n\tBN_CTX_init(&ctx);\n\tif (dh->priv_key == NULL)\n\t\t{\n\t\ti=dh->length;\n\t\tif (i == 0)\n\t\t\t{\n\t\t\ti=BN_num_bits(dh->p)-1;\n\t\t\t}\n\t\tpriv_key=BN_new();\n\t\tif (priv_key == NULL) goto err;\n\t\tif (!BN_rand(priv_key,i,0,0)) goto err;\n\t\t}\n\telse\n\t\tpriv_key=dh->priv_key;\n\tif (dh->pub_key == NULL)\n\t\t{\n\t\tpub_key=BN_new();\n\t\tif (pub_key == NULL) goto err;\n\t\t}\n\telse\n\t\tpub_key=dh->pub_key;\n\tif ((dh->method_mont_p == NULL) && (dh->flags & DH_FLAG_CACHE_MONT_P))\n\t\t{\n\t\tif ((dh->method_mont_p=(char *)BN_MONT_CTX_new()) != NULL)\n\t\t\tif (!BN_MONT_CTX_set((BN_MONT_CTX *)dh->method_mont_p,\n\t\t\t\tdh->p,&ctx)) goto err;\n\t\t}\n\tmont=(BN_MONT_CTX *)dh->method_mont_p;\n\tif (!BN_mod_exp_mont(pub_key,dh->g,priv_key,dh->p,&ctx,mont)) goto err;\n\tdh->pub_key=pub_key;\n\tdh->priv_key=priv_key;\n\tok=1;\nerr:\n\tif (ok != 1)\n\t\tDHerr(DH_F_DH_GENERATE_KEY,ERR_R_BN_LIB);\n\tif ((pub_key != NULL) && (dh->pub_key == NULL)) BN_free(pub_key);\n\tif ((priv_key != NULL) && (dh->priv_key == NULL)) BN_free(priv_key);\n\tBN_CTX_free(&ctx);\n\treturn(ok);\n\t}', 'int BN_mod_exp_mont(BIGNUM *rr, BIGNUM *a, const BIGNUM *p,\n\t\t const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n\t{\n\tint i,j,bits,ret=0,wstart,wend,window,wvalue;\n\tint start=1,ts=0;\n\tBIGNUM *d,*r;\n\tBIGNUM *aa;\n\tBIGNUM val[TABLE_SIZE];\n\tBN_MONT_CTX *mont=NULL;\n\tbn_check_top(a);\n\tbn_check_top(p);\n\tbn_check_top(m);\n\tif (!(m->d[0] & 1))\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_EXP_MONT,BN_R_CALLED_WITH_EVEN_MODULUS);\n\t\treturn(0);\n\t\t}\n\td= &(ctx->bn[ctx->tos++]);\n\tr= &(ctx->bn[ctx->tos++]);\n\tbits=BN_num_bits(p);\n\tif (bits == 0)\n\t\t{\n\t\tBN_one(r);\n\t\treturn(1);\n\t\t}\n#if 1\n\tif (in_mont != NULL)\n\t\tmont=in_mont;\n\telse\n#endif\n\t\t{\n\t\tif ((mont=BN_MONT_CTX_new()) == NULL) goto err;\n\t\tif (!BN_MONT_CTX_set(mont,m,ctx)) goto err;\n\t\t}\n\tBN_init(&val[0]);\n\tts=1;\n\tif (BN_ucmp(a,m) >= 0)\n\t\t{\n\t\tBN_mod(&(val[0]),a,m,ctx);\n\t\taa= &(val[0]);\n\t\t}\n\telse\n\t\taa=a;\n\tif (!BN_to_montgomery(&(val[0]),aa,mont,ctx)) goto err;\n\tif (!BN_mod_mul_montgomery(d,&(val[0]),&(val[0]),mont,ctx)) goto err;\n\tif (bits <= 20)\n\t\twindow=1;\n\telse if (bits >= 256)\n\t\twindow=5;\n\telse if (bits >= 128)\n\t\twindow=4;\n\telse\n\t\twindow=3;\n\tj=1<<(window-1);\n\tfor (i=1; i<j; i++)\n\t\t{\n\t\tBN_init(&(val[i]));\n\t\tif (!BN_mod_mul_montgomery(&(val[i]),&(val[i-1]),d,mont,ctx))\n\t\t\tgoto err;\n\t\t}\n\tts=i;\n\tstart=1;\n\twvalue=0;\n\twstart=bits-1;\n\twend=0;\n if (!BN_to_montgomery(r,BN_value_one(),mont,ctx)) goto err;\n\tfor (;;)\n\t\t{\n\t\tif (BN_is_bit_set(p,wstart) == 0)\n\t\t\t{\n\t\t\tif (!start)\n\t\t\t\t{\n\t\t\t\tif (!BN_mod_mul_montgomery(r,r,r,mont,ctx))\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif (wstart == 0) break;\n\t\t\twstart--;\n\t\t\tcontinue;\n\t\t\t}\n\t\tj=wstart;\n\t\twvalue=1;\n\t\twend=0;\n\t\tfor (i=1; i<window; i++)\n\t\t\t{\n\t\t\tif (wstart-i < 0) break;\n\t\t\tif (BN_is_bit_set(p,wstart-i))\n\t\t\t\t{\n\t\t\t\twvalue<<=(i-wend);\n\t\t\t\twvalue|=1;\n\t\t\t\twend=i;\n\t\t\t\t}\n\t\t\t}\n\t\tj=wend+1;\n\t\tif (!start)\n\t\t\tfor (i=0; i<j; i++)\n\t\t\t\t{\n\t\t\t\tif (!BN_mod_mul_montgomery(r,r,r,mont,ctx))\n\t\t\t\t\tgoto err;\n\t\t\t\t}\n\t\tif (!BN_mod_mul_montgomery(r,r,&(val[wvalue>>1]),mont,ctx))\n\t\t\tgoto err;\n\t\twstart-=wend+1;\n\t\twvalue=0;\n\t\tstart=0;\n\t\tif (wstart < 0) break;\n\t\t}\n\tBN_from_montgomery(rr,r,mont,ctx);\n\tret=1;\nerr:\n\tif ((in_mont == NULL) && (mont != NULL)) BN_MONT_CTX_free(mont);\n\tctx->tos-=2;\n\tfor (i=0; i<ts; i++)\n\t\tBN_clear_free(&(val[i]));\n\treturn(ret);\n\t}', 'int BN_mod_mul_montgomery(BIGNUM *r, BIGNUM *a, BIGNUM *b,\n\t\t\t BN_MONT_CTX *mont, BN_CTX *ctx)\n\t{\n\tBIGNUM *tmp,*tmp2;\n tmp= &(ctx->bn[ctx->tos]);\n tmp2= &(ctx->bn[ctx->tos]);\n\tctx->tos+=2;\n\tbn_check_top(tmp);\n\tbn_check_top(tmp2);\n\tif (a == b)\n\t\t{\n#if 0\n\t\tbn_wexpand(tmp,a->top*2);\n\t\tbn_wexpand(tmp2,a->top*4);\n\t\tbn_sqr_recursive(tmp->d,a->d,a->top,tmp2->d);\n\t\ttmp->top=a->top*2;\n\t\tif (tmp->d[tmp->top-1] == 0)\n\t\t\ttmp->top--;\n#else\n\t\tif (!BN_sqr(tmp,a,ctx)) goto err;\n#endif\n\t\t}\n\telse\n\t\t{\n\t\tif (!BN_mul(tmp,a,b,ctx)) goto err;\n\t\t}\n\tif (!BN_from_montgomery(r,tmp,mont,ctx)) goto err;\n\tctx->tos-=2;\n\treturn(1);\nerr:\n\treturn(0);\n\t}', 'int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)\n\t{\n\tint top,al,bl;\n\tBIGNUM *rr;\n#ifdef BN_RECURSION\n\tBIGNUM *t;\n\tint i,j,k;\n#endif\n#ifdef BN_COUNT\nprintf("BN_mul %d * %d\\n",a->top,b->top);\n#endif\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tbn_check_top(r);\n\tal=a->top;\n\tbl=b->top;\n\tr->neg=a->neg^b->neg;\n\tif ((al == 0) || (bl == 0))\n\t\t{\n\t\tBN_zero(r);\n\t\treturn(1);\n\t\t}\n\ttop=al+bl;\n\tif ((r == a) || (r == b))\n\t\trr= &(ctx->bn[ctx->tos+1]);\n\telse\n\t\trr=r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\tif (al == bl)\n\t\t{\n# ifdef BN_MUL_COMBA\n if (al == 8)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,16) == NULL) return(0);\n\t\t\tr->top=16;\n\t\t\tbn_mul_comba8(rr->d,a->d,b->d);\n\t\t\tgoto end;\n\t\t\t}\n\t\telse\n# endif\n#ifdef BN_RECURSION\n\t\tif (al < BN_MULL_SIZE_NORMAL)\n#endif\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,top) == NULL) return(0);\n\t\t\trr->top=top;\n\t\t\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n\t\t\tgoto end;\n\t\t\t}\n# ifdef BN_RECURSION\n\t\tgoto symetric;\n# endif\n\t\t}\n#endif\n#ifdef BN_RECURSION\n\telse if ((al < BN_MULL_SIZE_NORMAL) || (bl < BN_MULL_SIZE_NORMAL))\n\t\t{\n\t\tif (bn_wexpand(rr,top) == NULL) return(0);\n\t\trr->top=top;\n\t\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n\t\tgoto end;\n\t\t}\n\telse\n\t\t{\n\t\ti=(al-bl);\n\t\tif ((i == 1) && !BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tbn_wexpand(b,al);\n\t\t\tb->d[bl]=0;\n\t\t\tbl++;\n\t\t\tgoto symetric;\n\t\t\t}\n\t\telse if ((i == -1) && !BN_get_flags(a,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tbn_wexpand(a,bl);\n\t\t\ta->d[al]=0;\n\t\t\tal++;\n\t\t\tgoto symetric;\n\t\t\t}\n\t\t}\n#endif\n\tif (bn_wexpand(rr,top) == NULL) return(0);\n\trr->top=top;\n\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n#ifdef BN_RECURSION\n\tif (0)\n\t\t{\nsymetric:\n\t\tj=BN_num_bits_word((BN_ULONG)al);\n\t\tj=1<<(j-1);\n\t\tk=j+j;\n\t\tt= &(ctx->bn[ctx->tos]);\n\t\tif (al == j)\n\t\t\t{\n\t\t\tbn_wexpand(t,k*2);\n\t\t\tbn_wexpand(rr,k*2);\n\t\t\tbn_mul_recursive(rr->d,a->d,b->d,al,t->d);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tbn_wexpand(a,k);\n\t\t\tbn_wexpand(b,k);\n\t\t\tbn_wexpand(t,k*4);\n\t\t\tbn_wexpand(rr,k*4);\n\t\t\tfor (i=a->top; i<k; i++)\n\t\t\t\ta->d[i]=0;\n\t\t\tfor (i=b->top; i<k; i++)\n\t\t\t\tb->d[i]=0;\n\t\t\tbn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d);\n\t\t\t}\n\t\trr->top=top;\n\t\t}\n#endif\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\nend:\n#endif\n\tbn_fix_top(rr);\n\tif (r != rr) BN_copy(r,rr);\n\treturn(1);\n\t}', 'void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,\n\t BN_ULONG *t)\n\t{\n\tint n=n2/2,c1,c2;\n\tunsigned int neg,zero;\n\tBN_ULONG ln,lo,*p;\n#ifdef BN_COUNT\nprintf(" bn_mul_recursive %d * %d\\n",n2,n2);\n#endif\n#ifdef BN_MUL_COMBA\n if (n2 == 8)\n\t\t{\n\t\tbn_mul_comba8(r,a,b);\n\t\treturn;\n\t\t}\n#endif\n\tif (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL)\n\t\t{\n\t\tbn_mul_normal(r,a,n2,b,n2);\n\t\treturn;\n\t\t}\n\tc1=bn_cmp_words(a,&(a[n]),n);\n\tc2=bn_cmp_words(&(b[n]),b,n);\n\tzero=neg=0;\n\tswitch (c1*3+c2)\n\t\t{\n\tcase -4:\n\t\tbn_sub_words(t, &(a[n]),a, n);\n\t\tbn_sub_words(&(t[n]),b, &(b[n]),n);\n\t\tbreak;\n\tcase -3:\n\t\tzero=1;\n\t\tbreak;\n\tcase -2:\n\t\tbn_sub_words(t, &(a[n]),a, n);\n\t\tbn_sub_words(&(t[n]),&(b[n]),b, n);\n\t\tneg=1;\n\t\tbreak;\n\tcase -1:\n\tcase 0:\n\tcase 1:\n\t\tzero=1;\n\t\tbreak;\n\tcase 2:\n\t\tbn_sub_words(t, a, &(a[n]),n);\n\t\tbn_sub_words(&(t[n]),b, &(b[n]),n);\n\t\tneg=1;\n\t\tbreak;\n\tcase 3:\n\t\tzero=1;\n\t\tbreak;\n\tcase 4:\n\t\tbn_sub_words(t, a, &(a[n]),n);\n\t\tbn_sub_words(&(t[n]),&(b[n]),b, n);\n\t\tbreak;\n\t\t}\n#ifdef BN_MUL_COMBA\n\tif (n == 4)\n\t\t{\n\t\tif (!zero)\n\t\t\tbn_mul_comba4(&(t[n2]),t,&(t[n]));\n\t\telse\n\t\t\tmemset(&(t[n2]),0,8*sizeof(BN_ULONG));\n\t\tbn_mul_comba4(r,a,b);\n\t\tbn_mul_comba4(&(r[n2]),&(a[n]),&(b[n]));\n\t\t}\n\telse if (n == 8)\n\t\t{\n\t\tif (!zero)\n\t\t\tbn_mul_comba8(&(t[n2]),t,&(t[n]));\n\t\telse\n\t\t\tmemset(&(t[n2]),0,16*sizeof(BN_ULONG));\n\t\tbn_mul_comba8(r,a,b);\n\t\tbn_mul_comba8(&(r[n2]),&(a[n]),&(b[n]));\n\t\t}\n\telse\n#endif\n\t\t{\n\t\tp= &(t[n2*2]);\n\t\tif (!zero)\n\t\t\tbn_mul_recursive(&(t[n2]),t,&(t[n]),n,p);\n\t\telse\n\t\t\tmemset(&(t[n2]),0,n2*sizeof(BN_ULONG));\n\t\tbn_mul_recursive(r,a,b,n,p);\n\t\tbn_mul_recursive(&(r[n2]),&(a[n]),&(b[n]),n,p);\n\t\t}\n\tc1=(int)(bn_add_words(t,r,&(r[n2]),n2));\n\tif (neg)\n\t\t{\n\t\tc1-=(int)(bn_sub_words(&(t[n2]),t,&(t[n2]),n2));\n\t\t}\n\telse\n\t\t{\n\t\tc1+=(int)(bn_add_words(&(t[n2]),&(t[n2]),t,n2));\n\t\t}\n\tc1+=(int)(bn_add_words(&(r[n]),&(r[n]),&(t[n2]),n2));\n\tif (c1)\n\t\t{\n\t\tp= &(r[n+n2]);\n\t\tlo= *p;\n\t\tln=(lo+c1)&BN_MASK2;\n\t\t*p=ln;\n\t\tif (ln < (BN_ULONG)c1)\n\t\t\t{\n\t\t\tdo\t{\n\t\t\t\tp++;\n\t\t\t\tlo= *p;\n\t\t\t\tln=(lo+1)&BN_MASK2;\n\t\t\t\t*p=ln;\n\t\t\t\t} while (ln == 0);\n\t\t\t}\n\t\t}\n\t}', 'void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)\n\t{\n#ifdef BN_LLONG\n\tBN_ULLONG t;\n#else\n\tBN_ULONG bl,bh;\n#endif\n\tBN_ULONG t1,t2;\n\tBN_ULONG c1,c2,c3;\n\tc1=0;\n\tc2=0;\n\tc3=0;\n\tmul_add_c(a[0],b[0],c1,c2,c3);\n\tr[0]=c1;\n\tc1=0;\n\tmul_add_c(a[0],b[1],c2,c3,c1);\n\tmul_add_c(a[1],b[0],c2,c3,c1);\n\tr[1]=c2;\n\tc2=0;\n\tmul_add_c(a[2],b[0],c3,c1,c2);\n\tmul_add_c(a[1],b[1],c3,c1,c2);\n\tmul_add_c(a[0],b[2],c3,c1,c2);\n\tr[2]=c3;\n\tc3=0;\n\tmul_add_c(a[0],b[3],c1,c2,c3);\n\tmul_add_c(a[1],b[2],c1,c2,c3);\n\tmul_add_c(a[2],b[1],c1,c2,c3);\n\tmul_add_c(a[3],b[0],c1,c2,c3);\n\tr[3]=c1;\n\tc1=0;\n\tmul_add_c(a[4],b[0],c2,c3,c1);\n\tmul_add_c(a[3],b[1],c2,c3,c1);\n\tmul_add_c(a[2],b[2],c2,c3,c1);\n\tmul_add_c(a[1],b[3],c2,c3,c1);\n\tmul_add_c(a[0],b[4],c2,c3,c1);\n\tr[4]=c2;\n\tc2=0;\n\tmul_add_c(a[0],b[5],c3,c1,c2);\n\tmul_add_c(a[1],b[4],c3,c1,c2);\n\tmul_add_c(a[2],b[3],c3,c1,c2);\n\tmul_add_c(a[3],b[2],c3,c1,c2);\n\tmul_add_c(a[4],b[1],c3,c1,c2);\n\tmul_add_c(a[5],b[0],c3,c1,c2);\n\tr[5]=c3;\n\tc3=0;\n\tmul_add_c(a[6],b[0],c1,c2,c3);\n\tmul_add_c(a[5],b[1],c1,c2,c3);\n\tmul_add_c(a[4],b[2],c1,c2,c3);\n\tmul_add_c(a[3],b[3],c1,c2,c3);\n\tmul_add_c(a[2],b[4],c1,c2,c3);\n\tmul_add_c(a[1],b[5],c1,c2,c3);\n\tmul_add_c(a[0],b[6],c1,c2,c3);\n\tr[6]=c1;\n\tc1=0;\n\tmul_add_c(a[0],b[7],c2,c3,c1);\n\tmul_add_c(a[1],b[6],c2,c3,c1);\n\tmul_add_c(a[2],b[5],c2,c3,c1);\n\tmul_add_c(a[3],b[4],c2,c3,c1);\n\tmul_add_c(a[4],b[3],c2,c3,c1);\n\tmul_add_c(a[5],b[2],c2,c3,c1);\n\tmul_add_c(a[6],b[1],c2,c3,c1);\n\tmul_add_c(a[7],b[0],c2,c3,c1);\n\tr[7]=c2;\n\tc2=0;\n\tmul_add_c(a[7],b[1],c3,c1,c2);\n\tmul_add_c(a[6],b[2],c3,c1,c2);\n\tmul_add_c(a[5],b[3],c3,c1,c2);\n\tmul_add_c(a[4],b[4],c3,c1,c2);\n\tmul_add_c(a[3],b[5],c3,c1,c2);\n\tmul_add_c(a[2],b[6],c3,c1,c2);\n\tmul_add_c(a[1],b[7],c3,c1,c2);\n\tr[8]=c3;\n\tc3=0;\n\tmul_add_c(a[2],b[7],c1,c2,c3);\n\tmul_add_c(a[3],b[6],c1,c2,c3);\n\tmul_add_c(a[4],b[5],c1,c2,c3);\n\tmul_add_c(a[5],b[4],c1,c2,c3);\n\tmul_add_c(a[6],b[3],c1,c2,c3);\n\tmul_add_c(a[7],b[2],c1,c2,c3);\n\tr[9]=c1;\n\tc1=0;\n\tmul_add_c(a[7],b[3],c2,c3,c1);\n\tmul_add_c(a[6],b[4],c2,c3,c1);\n\tmul_add_c(a[5],b[5],c2,c3,c1);\n\tmul_add_c(a[4],b[6],c2,c3,c1);\n\tmul_add_c(a[3],b[7],c2,c3,c1);\n\tr[10]=c2;\n\tc2=0;\n\tmul_add_c(a[4],b[7],c3,c1,c2);\n\tmul_add_c(a[5],b[6],c3,c1,c2);\n\tmul_add_c(a[6],b[5],c3,c1,c2);\n\tmul_add_c(a[7],b[4],c3,c1,c2);\n\tr[11]=c3;\n\tc3=0;\n\tmul_add_c(a[7],b[5],c1,c2,c3);\n\tmul_add_c(a[6],b[6],c1,c2,c3);\n\tmul_add_c(a[5],b[7],c1,c2,c3);\n\tr[12]=c1;\n\tc1=0;\n\tmul_add_c(a[6],b[7],c2,c3,c1);\n\tmul_add_c(a[7],b[6],c2,c3,c1);\n\tr[13]=c2;\n\tc2=0;\n\tmul_add_c(a[7],b[7],c3,c1,c2);\n\tr[14]=c3;\n\tr[15]=c1;\n\t}'] |
509 | 0 | https://github.com/openssl/openssl/blob/d6ee8f3dc4414cd97bd63b801f8644f0ff8a1f17/crypto/bio/b_print.c/#L353 | static int
_dopr(char **sbuffer,
char **buffer,
size_t *maxlen,
size_t *retlen, int *truncated, const char *format, va_list args)
{
char ch;
int64_t value;
LDOUBLE fvalue;
char *strvalue;
int min;
int max;
int state;
int flags;
int cflags;
size_t currlen;
state = DP_S_DEFAULT;
flags = currlen = cflags = min = 0;
max = -1;
ch = *format++;
while (state != DP_S_DONE) {
if (ch == '\0' || (buffer == NULL && currlen >= *maxlen))
state = DP_S_DONE;
switch (state) {
case DP_S_DEFAULT:
if (ch == '%')
state = DP_S_FLAGS;
else
if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))
return 0;
ch = *format++;
break;
case DP_S_FLAGS:
switch (ch) {
case '-':
flags |= DP_F_MINUS;
ch = *format++;
break;
case '+':
flags |= DP_F_PLUS;
ch = *format++;
break;
case ' ':
flags |= DP_F_SPACE;
ch = *format++;
break;
case '#':
flags |= DP_F_NUM;
ch = *format++;
break;
case '0':
flags |= DP_F_ZERO;
ch = *format++;
break;
default:
state = DP_S_MIN;
break;
}
break;
case DP_S_MIN:
if (ossl_isdigit(ch)) {
min = 10 * min + char_to_int(ch);
ch = *format++;
} else if (ch == '*') {
min = va_arg(args, int);
ch = *format++;
state = DP_S_DOT;
} else
state = DP_S_DOT;
break;
case DP_S_DOT:
if (ch == '.') {
state = DP_S_MAX;
ch = *format++;
} else
state = DP_S_MOD;
break;
case DP_S_MAX:
if (ossl_isdigit(ch)) {
if (max < 0)
max = 0;
max = 10 * max + char_to_int(ch);
ch = *format++;
} else if (ch == '*') {
max = va_arg(args, int);
ch = *format++;
state = DP_S_MOD;
} else
state = DP_S_MOD;
break;
case DP_S_MOD:
switch (ch) {
case 'h':
cflags = DP_C_SHORT;
ch = *format++;
break;
case 'l':
if (*format == 'l') {
cflags = DP_C_LLONG;
format++;
} else
cflags = DP_C_LONG;
ch = *format++;
break;
case 'q':
case 'j':
cflags = DP_C_LLONG;
ch = *format++;
break;
case 'L':
cflags = DP_C_LDOUBLE;
ch = *format++;
break;
case 'z':
cflags = DP_C_SIZE;
ch = *format++;
break;
default:
break;
}
state = DP_S_CONV;
break;
case DP_S_CONV:
switch (ch) {
case 'd':
case 'i':
switch (cflags) {
case DP_C_SHORT:
value = (short int)va_arg(args, int);
break;
case DP_C_LONG:
value = va_arg(args, long int);
break;
case DP_C_LLONG:
value = va_arg(args, int64_t);
break;
case DP_C_SIZE:
value = va_arg(args, ossl_ssize_t);
break;
default:
value = va_arg(args, int);
break;
}
if (!fmtint(sbuffer, buffer, &currlen, maxlen, value, 10, min,
max, flags))
return 0;
break;
case 'X':
flags |= DP_F_UP;
case 'x':
case 'o':
case 'u':
flags |= DP_F_UNSIGNED;
switch (cflags) {
case DP_C_SHORT:
value = (unsigned short int)va_arg(args, unsigned int);
break;
case DP_C_LONG:
value = va_arg(args, unsigned long int);
break;
case DP_C_LLONG:
value = va_arg(args, uint64_t);
break;
case DP_C_SIZE:
value = va_arg(args, size_t);
break;
default:
value = va_arg(args, unsigned int);
break;
}
if (!fmtint(sbuffer, buffer, &currlen, maxlen, value,
ch == 'o' ? 8 : (ch == 'u' ? 10 : 16),
min, max, flags))
return 0;
break;
case 'f':
if (cflags == DP_C_LDOUBLE)
fvalue = va_arg(args, LDOUBLE);
else
fvalue = va_arg(args, double);
if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,
flags, F_FORMAT))
return 0;
break;
case 'E':
flags |= DP_F_UP;
case 'e':
if (cflags == DP_C_LDOUBLE)
fvalue = va_arg(args, LDOUBLE);
else
fvalue = va_arg(args, double);
if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,
flags, E_FORMAT))
return 0;
break;
case 'G':
flags |= DP_F_UP;
case 'g':
if (cflags == DP_C_LDOUBLE)
fvalue = va_arg(args, LDOUBLE);
else
fvalue = va_arg(args, double);
if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,
flags, G_FORMAT))
return 0;
break;
case 'c':
if (!doapr_outch(sbuffer, buffer, &currlen, maxlen,
va_arg(args, int)))
return 0;
break;
case 's':
strvalue = va_arg(args, char *);
if (max < 0) {
if (buffer)
max = INT_MAX;
else
max = *maxlen;
}
if (!fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue,
flags, min, max))
return 0;
break;
case 'p':
value = (size_t)va_arg(args, void *);
if (!fmtint(sbuffer, buffer, &currlen, maxlen,
value, 16, min, max, flags | DP_F_NUM))
return 0;
break;
case 'n':
{
int *num;
num = va_arg(args, int *);
*num = currlen;
}
break;
case '%':
if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))
return 0;
break;
case 'w':
ch = *format++;
break;
default:
break;
}
ch = *format++;
state = DP_S_DEFAULT;
flags = cflags = min = 0;
max = -1;
break;
case DP_S_DONE:
break;
default:
break;
}
}
if (buffer == NULL) {
*truncated = (currlen > *maxlen - 1);
if (*truncated)
currlen = *maxlen - 1;
}
if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, '\0'))
return 0;
*retlen = currlen - 1;
return 1;
} | ['long BIO_debug_callback(BIO *bio, int cmd, const char *argp,\n int argi, long argl, long ret)\n{\n BIO *b;\n char buf[256];\n char *p;\n long r = 1;\n int len, left;\n if (BIO_CB_RETURN & cmd)\n r = ret;\n len = BIO_snprintf(buf, sizeof(buf), "BIO[%p]: ", (void *)bio);\n if (len < 0)\n len = 0;\n p = buf + len;\n left = sizeof(buf) - len;\n switch (cmd) {\n case BIO_CB_FREE:\n BIO_snprintf(p, left, "Free - %s\\n", bio->method->name);\n break;\n case BIO_CB_READ:\n if (bio->method->type & BIO_TYPE_DESCRIPTOR)\n BIO_snprintf(p, left, "read(%d,%lu) - %s fd=%d\\n",\n bio->num, (unsigned long)argi,\n bio->method->name, bio->num);\n else\n BIO_snprintf(p, left, "read(%d,%lu) - %s\\n",\n bio->num, (unsigned long)argi, bio->method->name);\n break;\n case BIO_CB_WRITE:\n if (bio->method->type & BIO_TYPE_DESCRIPTOR)\n BIO_snprintf(p, left, "write(%d,%lu) - %s fd=%d\\n",\n bio->num, (unsigned long)argi,\n bio->method->name, bio->num);\n else\n BIO_snprintf(p, left, "write(%d,%lu) - %s\\n",\n bio->num, (unsigned long)argi, bio->method->name);\n break;\n case BIO_CB_PUTS:\n BIO_snprintf(p, left, "puts() - %s\\n", bio->method->name);\n break;\n case BIO_CB_GETS:\n BIO_snprintf(p, left, "gets(%lu) - %s\\n", (unsigned long)argi,\n bio->method->name);\n break;\n case BIO_CB_CTRL:\n BIO_snprintf(p, left, "ctrl(%lu) - %s\\n", (unsigned long)argi,\n bio->method->name);\n break;\n case BIO_CB_RETURN | BIO_CB_READ:\n BIO_snprintf(p, left, "read return %ld\\n", ret);\n break;\n case BIO_CB_RETURN | BIO_CB_WRITE:\n BIO_snprintf(p, left, "write return %ld\\n", ret);\n break;\n case BIO_CB_RETURN | BIO_CB_GETS:\n BIO_snprintf(p, left, "gets return %ld\\n", ret);\n break;\n case BIO_CB_RETURN | BIO_CB_PUTS:\n BIO_snprintf(p, left, "puts return %ld\\n", ret);\n break;\n case BIO_CB_RETURN | BIO_CB_CTRL:\n BIO_snprintf(p, left, "ctrl return %ld\\n", ret);\n break;\n default:\n BIO_snprintf(p, left, "bio callback - unknown type (%d)\\n", cmd);\n break;\n }\n b = (BIO *)bio->cb_arg;\n if (b != NULL)\n BIO_write(b, buf, strlen(buf));\n#if !defined(OPENSSL_NO_STDIO)\n else\n fputs(buf, stderr);\n#endif\n return r;\n}', 'int BIO_snprintf(char *buf, size_t n, const char *format, ...)\n{\n va_list args;\n int ret;\n va_start(args, format);\n ret = BIO_vsnprintf(buf, n, format, args);\n va_end(args);\n return ret;\n}', 'int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args)\n{\n size_t retlen;\n int truncated;\n if (!_dopr(&buf, NULL, &n, &retlen, &truncated, format, args))\n return -1;\n if (truncated)\n return -1;\n else\n return (retlen <= INT_MAX) ? (int)retlen : -1;\n}', "static int\n_dopr(char **sbuffer,\n char **buffer,\n size_t *maxlen,\n size_t *retlen, int *truncated, const char *format, va_list args)\n{\n char ch;\n int64_t value;\n LDOUBLE fvalue;\n char *strvalue;\n int min;\n int max;\n int state;\n int flags;\n int cflags;\n size_t currlen;\n state = DP_S_DEFAULT;\n flags = currlen = cflags = min = 0;\n max = -1;\n ch = *format++;\n while (state != DP_S_DONE) {\n if (ch == '\\0' || (buffer == NULL && currlen >= *maxlen))\n state = DP_S_DONE;\n switch (state) {\n case DP_S_DEFAULT:\n if (ch == '%')\n state = DP_S_FLAGS;\n else\n if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))\n return 0;\n ch = *format++;\n break;\n case DP_S_FLAGS:\n switch (ch) {\n case '-':\n flags |= DP_F_MINUS;\n ch = *format++;\n break;\n case '+':\n flags |= DP_F_PLUS;\n ch = *format++;\n break;\n case ' ':\n flags |= DP_F_SPACE;\n ch = *format++;\n break;\n case '#':\n flags |= DP_F_NUM;\n ch = *format++;\n break;\n case '0':\n flags |= DP_F_ZERO;\n ch = *format++;\n break;\n default:\n state = DP_S_MIN;\n break;\n }\n break;\n case DP_S_MIN:\n if (ossl_isdigit(ch)) {\n min = 10 * min + char_to_int(ch);\n ch = *format++;\n } else if (ch == '*') {\n min = va_arg(args, int);\n ch = *format++;\n state = DP_S_DOT;\n } else\n state = DP_S_DOT;\n break;\n case DP_S_DOT:\n if (ch == '.') {\n state = DP_S_MAX;\n ch = *format++;\n } else\n state = DP_S_MOD;\n break;\n case DP_S_MAX:\n if (ossl_isdigit(ch)) {\n if (max < 0)\n max = 0;\n max = 10 * max + char_to_int(ch);\n ch = *format++;\n } else if (ch == '*') {\n max = va_arg(args, int);\n ch = *format++;\n state = DP_S_MOD;\n } else\n state = DP_S_MOD;\n break;\n case DP_S_MOD:\n switch (ch) {\n case 'h':\n cflags = DP_C_SHORT;\n ch = *format++;\n break;\n case 'l':\n if (*format == 'l') {\n cflags = DP_C_LLONG;\n format++;\n } else\n cflags = DP_C_LONG;\n ch = *format++;\n break;\n case 'q':\n case 'j':\n cflags = DP_C_LLONG;\n ch = *format++;\n break;\n case 'L':\n cflags = DP_C_LDOUBLE;\n ch = *format++;\n break;\n case 'z':\n cflags = DP_C_SIZE;\n ch = *format++;\n break;\n default:\n break;\n }\n state = DP_S_CONV;\n break;\n case DP_S_CONV:\n switch (ch) {\n case 'd':\n case 'i':\n switch (cflags) {\n case DP_C_SHORT:\n value = (short int)va_arg(args, int);\n break;\n case DP_C_LONG:\n value = va_arg(args, long int);\n break;\n case DP_C_LLONG:\n value = va_arg(args, int64_t);\n break;\n case DP_C_SIZE:\n value = va_arg(args, ossl_ssize_t);\n break;\n default:\n value = va_arg(args, int);\n break;\n }\n if (!fmtint(sbuffer, buffer, &currlen, maxlen, value, 10, min,\n max, flags))\n return 0;\n break;\n case 'X':\n flags |= DP_F_UP;\n case 'x':\n case 'o':\n case 'u':\n flags |= DP_F_UNSIGNED;\n switch (cflags) {\n case DP_C_SHORT:\n value = (unsigned short int)va_arg(args, unsigned int);\n break;\n case DP_C_LONG:\n value = va_arg(args, unsigned long int);\n break;\n case DP_C_LLONG:\n value = va_arg(args, uint64_t);\n break;\n case DP_C_SIZE:\n value = va_arg(args, size_t);\n break;\n default:\n value = va_arg(args, unsigned int);\n break;\n }\n if (!fmtint(sbuffer, buffer, &currlen, maxlen, value,\n ch == 'o' ? 8 : (ch == 'u' ? 10 : 16),\n min, max, flags))\n return 0;\n break;\n case 'f':\n if (cflags == DP_C_LDOUBLE)\n fvalue = va_arg(args, LDOUBLE);\n else\n fvalue = va_arg(args, double);\n if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,\n flags, F_FORMAT))\n return 0;\n break;\n case 'E':\n flags |= DP_F_UP;\n case 'e':\n if (cflags == DP_C_LDOUBLE)\n fvalue = va_arg(args, LDOUBLE);\n else\n fvalue = va_arg(args, double);\n if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,\n flags, E_FORMAT))\n return 0;\n break;\n case 'G':\n flags |= DP_F_UP;\n case 'g':\n if (cflags == DP_C_LDOUBLE)\n fvalue = va_arg(args, LDOUBLE);\n else\n fvalue = va_arg(args, double);\n if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,\n flags, G_FORMAT))\n return 0;\n break;\n case 'c':\n if (!doapr_outch(sbuffer, buffer, &currlen, maxlen,\n va_arg(args, int)))\n return 0;\n break;\n case 's':\n strvalue = va_arg(args, char *);\n if (max < 0) {\n if (buffer)\n max = INT_MAX;\n else\n max = *maxlen;\n }\n if (!fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue,\n flags, min, max))\n return 0;\n break;\n case 'p':\n value = (size_t)va_arg(args, void *);\n if (!fmtint(sbuffer, buffer, &currlen, maxlen,\n value, 16, min, max, flags | DP_F_NUM))\n return 0;\n break;\n case 'n':\n {\n int *num;\n num = va_arg(args, int *);\n *num = currlen;\n }\n break;\n case '%':\n if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))\n return 0;\n break;\n case 'w':\n ch = *format++;\n break;\n default:\n break;\n }\n ch = *format++;\n state = DP_S_DEFAULT;\n flags = cflags = min = 0;\n max = -1;\n break;\n case DP_S_DONE:\n break;\n default:\n break;\n }\n }\n if (buffer == NULL) {\n *truncated = (currlen > *maxlen - 1);\n if (*truncated)\n currlen = *maxlen - 1;\n }\n if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, '\\0'))\n return 0;\n *retlen = currlen - 1;\n return 1;\n}"] |
510 | 0 | https://github.com/openssl/openssl/blob/e1cfd184dafb3e0759c567d7ca13a92b5491ff89/test/evp_test.c/#L1768 | static int encode_test_init(EVP_TEST *t, const char *encoding)
{
ENCODE_DATA *edata = OPENSSL_zalloc(sizeof(*edata));
if (strcmp(encoding, "canonical") == 0) {
edata->encoding = BASE64_CANONICAL_ENCODING;
} else if (strcmp(encoding, "valid") == 0) {
edata->encoding = BASE64_VALID_ENCODING;
} else if (strcmp(encoding, "invalid") == 0) {
edata->encoding = BASE64_INVALID_ENCODING;
t->expected_err = OPENSSL_strdup("DECODE_ERROR");
if (t->expected_err == NULL)
return 0;
} else {
TEST_info("Bad encoding: %s. Should be one of "
"{canonical, valid, invalid}", encoding);
return 0;
}
t->data = edata;
return 1;
} | ['static int encode_test_init(EVP_TEST *t, const char *encoding)\n{\n ENCODE_DATA *edata = OPENSSL_zalloc(sizeof(*edata));\n if (strcmp(encoding, "canonical") == 0) {\n edata->encoding = BASE64_CANONICAL_ENCODING;\n } else if (strcmp(encoding, "valid") == 0) {\n edata->encoding = BASE64_VALID_ENCODING;\n } else if (strcmp(encoding, "invalid") == 0) {\n edata->encoding = BASE64_INVALID_ENCODING;\n t->expected_err = OPENSSL_strdup("DECODE_ERROR");\n if (t->expected_err == NULL)\n return 0;\n } else {\n TEST_info("Bad encoding: %s. Should be one of "\n "{canonical, valid, invalid}", encoding);\n return 0;\n }\n t->data = edata;\n return 1;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n osslargused(file); osslargused(line);\n ret = malloc(num);\n#endif\n return ret;\n}'] |
511 | 0 | https://github.com/openssl/openssl/blob/1697a81bafcc9a65012b62ba01c1661bf40e5edb/crypto/dsa/dsa_lib.c/#L156 | DSA *DSA_new_method(ENGINE *engine)
{
DSA *ret = OPENSSL_zalloc(sizeof(*ret));
if (ret == NULL) {
DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
return NULL;
}
ret->references = 1;
ret->lock = CRYPTO_THREAD_lock_new();
if (ret->lock == NULL) {
OPENSSL_free(ret);
return NULL;
}
ret->meth = DSA_get_default_method();
#ifndef OPENSSL_NO_ENGINE
ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW;
if (engine) {
if (!ENGINE_init(engine)) {
DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB);
goto err;
}
ret->engine = engine;
} else
ret->engine = ENGINE_get_default_DSA();
if (ret->engine) {
ret->meth = ENGINE_get_DSA(ret->engine);
if (ret->meth == NULL) {
DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB);
goto err;
}
}
#endif
ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW;
if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data))
goto err;
if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_INIT_FAIL);
err:
DSA_free(ret);
ret = NULL;
}
return ret;
} | ['DSA *DSA_new_method(ENGINE *engine)\n{\n DSA *ret = OPENSSL_zalloc(sizeof(*ret));\n if (ret == NULL) {\n DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n ret->references = 1;\n ret->lock = CRYPTO_THREAD_lock_new();\n if (ret->lock == NULL) {\n OPENSSL_free(ret);\n return NULL;\n }\n ret->meth = DSA_get_default_method();\n#ifndef OPENSSL_NO_ENGINE\n ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW;\n if (engine) {\n if (!ENGINE_init(engine)) {\n DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB);\n goto err;\n }\n ret->engine = engine;\n } else\n ret->engine = ENGINE_get_default_DSA();\n if (ret->engine) {\n ret->meth = ENGINE_get_DSA(ret->engine);\n if (ret->meth == NULL) {\n DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB);\n goto err;\n }\n }\n#endif\n ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW;\n if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data))\n goto err;\n if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {\n DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_INIT_FAIL);\nerr:\n DSA_free(ret);\n ret = NULL;\n }\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 (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num <= 0)\n return NULL;\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n osslargused(file); osslargused(line);\n ret = malloc(num);\n#endif\n return ret;\n}', 'CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void)\n{\n CRYPTO_RWLOCK *lock = OPENSSL_zalloc(sizeof(pthread_rwlock_t));\n if (lock == NULL)\n return NULL;\n if (pthread_rwlock_init(lock, NULL) != 0) {\n OPENSSL_free(lock);\n return NULL;\n }\n return lock;\n}', 'const DSA_METHOD *DSA_get_default_method(void)\n{\n if (!default_DSA_method)\n default_DSA_method = DSA_OpenSSL();\n return default_DSA_method;\n}', 'ENGINE *ENGINE_get_default_DSA(void)\n{\n return engine_table_select(&dsa_table, dummy_nid);\n}', 'ENGINE *engine_table_select(ENGINE_TABLE **table, int nid)\n#else\nENGINE *engine_table_select_tmp(ENGINE_TABLE **table, int nid, const char *f,\n int l)\n#endif\n{\n ENGINE *ret = NULL;\n ENGINE_PILE tmplate, *fnd = NULL;\n int initres, loop = 0;\n if (!(*table)) {\n#ifdef ENGINE_TABLE_DEBUG\n fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, nothing "\n "registered!\\n", f, l, nid);\n#endif\n return NULL;\n }\n ERR_set_mark();\n CRYPTO_THREAD_write_lock(global_engine_lock);\n if (!int_table_check(table, 0))\n goto end;\n tmplate.nid = nid;\n fnd = lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate);\n if (!fnd)\n goto end;\n if (fnd->funct && engine_unlocked_init(fnd->funct)) {\n#ifdef ENGINE_TABLE_DEBUG\n fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, using "\n "ENGINE \'%s\' cached\\n", f, l, nid, fnd->funct->id);\n#endif\n ret = fnd->funct;\n goto end;\n }\n if (fnd->uptodate) {\n ret = fnd->funct;\n goto end;\n }\n trynext:\n ret = sk_ENGINE_value(fnd->sk, loop++);\n if (!ret) {\n#ifdef ENGINE_TABLE_DEBUG\n fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, no "\n "registered implementations would initialise\\n", f, l, nid);\n#endif\n goto end;\n }\n if ((ret->funct_ref > 0) || !(table_flags & ENGINE_TABLE_FLAG_NOINIT))\n initres = engine_unlocked_init(ret);\n else\n initres = 0;\n if (initres) {\n if ((fnd->funct != ret) && engine_unlocked_init(ret)) {\n if (fnd->funct)\n engine_unlocked_finish(fnd->funct, 0);\n fnd->funct = ret;\n#ifdef ENGINE_TABLE_DEBUG\n fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, "\n "setting default to \'%s\'\\n", f, l, nid, ret->id);\n#endif\n }\n#ifdef ENGINE_TABLE_DEBUG\n fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, using "\n "newly initialised \'%s\'\\n", f, l, nid, ret->id);\n#endif\n goto end;\n }\n goto trynext;\n end:\n if (fnd)\n fnd->uptodate = 1;\n#ifdef ENGINE_TABLE_DEBUG\n if (ret)\n fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, caching "\n "ENGINE \'%s\'\\n", f, l, nid, ret->id);\n else\n fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, caching "\n "\'no matching ENGINE\'\\n", f, l, nid);\n#endif\n CRYPTO_THREAD_unlock(global_engine_lock);\n ERR_pop_to_mark();\n return ret;\n}', 'int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad)\n{\n int mx, i;\n void *ptr;\n EX_CALLBACK **storage = NULL;\n EX_CALLBACK *stack[10];\n EX_CALLBACKS *ip = get_and_lock(class_index);\n if (ip == NULL)\n return 0;\n ad->sk = NULL;\n mx = sk_EX_CALLBACK_num(ip->meth);\n if (mx > 0) {\n if (mx < (int)OSSL_NELEM(stack))\n storage = stack;\n else\n storage = OPENSSL_malloc(sizeof(*storage) * mx);\n if (storage != NULL)\n for (i = 0; i < mx; i++)\n storage[i] = sk_EX_CALLBACK_value(ip->meth, i);\n }\n CRYPTO_THREAD_unlock(ex_data_lock);\n if (mx > 0 && storage == NULL) {\n CRYPTOerr(CRYPTO_F_CRYPTO_NEW_EX_DATA, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n for (i = 0; i < mx; i++) {\n if (storage[i] && storage[i]->new_func) {\n ptr = CRYPTO_get_ex_data(ad, i);\n storage[i]->new_func(obj, ptr, ad, i,\n storage[i]->argl, storage[i]->argp);\n }\n }\n if (storage != stack)\n OPENSSL_free(storage);\n return 1;\n}', 'static EX_CALLBACKS *get_and_lock(int class_index)\n{\n EX_CALLBACKS *ip;\n if (class_index < 0 || class_index >= CRYPTO_EX_INDEX__COUNT) {\n CRYPTOerr(CRYPTO_F_GET_AND_LOCK, ERR_R_PASSED_INVALID_ARGUMENT);\n return NULL;\n }\n CRYPTO_THREAD_run_once(&ex_data_init, do_ex_data_init);\n if (ex_data_lock == NULL) {\n return NULL;\n }\n ip = &ex_data[class_index];\n CRYPTO_THREAD_write_lock(ex_data_lock);\n return ip;\n}', 'int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))\n{\n if (pthread_once(once, init) != 0)\n return 0;\n return 1;\n}', 'void DSA_free(DSA *r)\n{\n int i;\n if (r == NULL)\n return;\n CRYPTO_atomic_add(&r->references, -1, &i, r->lock);\n REF_PRINT_COUNT("DSA", r);\n if (i > 0)\n return;\n REF_ASSERT_ISNT(i < 0);\n if (r->meth->finish)\n r->meth->finish(r);\n#ifndef OPENSSL_NO_ENGINE\n ENGINE_finish(r->engine);\n#endif\n CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data);\n CRYPTO_THREAD_lock_free(r->lock);\n BN_clear_free(r->p);\n BN_clear_free(r->q);\n BN_clear_free(r->g);\n BN_clear_free(r->pub_key);\n BN_clear_free(r->priv_key);\n OPENSSL_free(r);\n}', 'int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)\n{\n#ifdef __ATOMIC_RELAXED\n *ret = __atomic_add_fetch(val, amount, __ATOMIC_RELAXED);\n#else\n if (!CRYPTO_THREAD_write_lock(lock))\n return 0;\n *val += amount;\n *ret = *val;\n if (!CRYPTO_THREAD_unlock(lock))\n return 0;\n#endif\n return 1;\n}'] |
512 | 0 | https://github.com/openssl/openssl/blob/f1e793cc974094da7da52c6958ad7b972b4a446d/test/sha1test.c/#L98 | static int test_sha1_big(void)
{
static unsigned char buf[1000];
EVP_MD_CTX *c;
unsigned char md[SHA_DIGEST_LENGTH];
int i, testresult = 0;
c = EVP_MD_CTX_new();
if (!TEST_ptr(c))
goto end;
memset(buf, 'a', 1000);
#ifdef CHARSET_EBCDIC
ebcdic2ascii(buf, buf, 1000);
#endif
if (!TEST_true(EVP_DigestInit_ex(c, EVP_sha1(), NULL)))
goto end;
for (i = 0; i < 1000; i++)
if (!TEST_true(EVP_DigestUpdate(c, buf, 1000)))
goto end;
if (!TEST_true(EVP_DigestFinal_ex(c, md, NULL)))
goto end;
if (!TEST_str_eq(pt(md), bigret))
goto end;
testresult = 1;
end:
EVP_MD_CTX_free(c);
return testresult;
} | ["static int test_sha1_big(void)\n{\n static unsigned char buf[1000];\n EVP_MD_CTX *c;\n unsigned char md[SHA_DIGEST_LENGTH];\n int i, testresult = 0;\n c = EVP_MD_CTX_new();\n if (!TEST_ptr(c))\n goto end;\n memset(buf, 'a', 1000);\n#ifdef CHARSET_EBCDIC\n ebcdic2ascii(buf, buf, 1000);\n#endif\n if (!TEST_true(EVP_DigestInit_ex(c, EVP_sha1(), NULL)))\n goto end;\n for (i = 0; i < 1000; i++)\n if (!TEST_true(EVP_DigestUpdate(c, buf, 1000)))\n goto end;\n if (!TEST_true(EVP_DigestFinal_ex(c, md, NULL)))\n goto end;\n if (!TEST_str_eq(pt(md), bigret))\n goto end;\n testresult = 1;\n end:\n EVP_MD_CTX_free(c);\n return testresult;\n}", 'EVP_MD_CTX *EVP_MD_CTX_new(void)\n{\n return OPENSSL_zalloc(sizeof(EVP_MD_CTX));\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n 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 test_ptr(const char *file, int line, const char *s, const void *p)\n{\n if (p != NULL)\n return 1;\n test_fail_message(NULL, file, line, "ptr", "%s [%p] != NULL", s, p);\n return 0;\n}', 'const EVP_MD *EVP_sha1(void)\n{\n return (&sha1_md);\n}', 'int test_true(const char *file, int line, const char *s, int b)\n{\n if (b)\n return 1;\n test_fail_message(NULL, file, line, "bool", "%s [false] == true", s);\n return 0;\n}', 'static void test_fail_message(const char *prefix, const char *file, int line,\n const char *type, const char *fmt, ...)\n{\n va_list ap;\n va_start(ap, fmt);\n test_fail_message_va(prefix, file, line, type, fmt, ap);\n va_end(ap);\n}', 'static void test_fail_message_va(const char *prefix, const char *file, int line,\n const char *type, const char *fmt, va_list ap)\n{\n fputs(prefix != NULL ? prefix : "ERROR", stderr);\n fputs(":", stderr);\n if (type)\n fprintf(stderr, " (%s)", type);\n if (fmt != NULL) {\n fputc(\' \', stderr);\n vfprintf(stderr, fmt, ap);\n }\n if (file != NULL) {\n fprintf(stderr, " @ %s:%d", file, line);\n }\n fputc(\'\\n\', stderr);\n}', 'void EVP_MD_CTX_free(EVP_MD_CTX *ctx)\n{\n EVP_MD_CTX_reset(ctx);\n OPENSSL_free(ctx);\n}', 'void CRYPTO_free(void *str, const char *file, int line)\n{\n if (free_impl != NULL && free_impl != &CRYPTO_free) {\n free_impl(str, file, line);\n return;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_free(str, 0, file, line);\n free(str);\n CRYPTO_mem_debug_free(str, 1, file, line);\n } else {\n free(str);\n }\n#else\n free(str);\n#endif\n}'] |
513 | 0 | https://github.com/openssl/openssl/blob/37842dfaebcf28b4ca452c6abd93ebde1b4aa6dc/crypto/bn/bn_mul.c/#L657 | void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb)
{
BN_ULONG *rr;
if (na < nb) {
int itmp;
BN_ULONG *ltmp;
itmp = na;
na = nb;
nb = itmp;
ltmp = a;
a = b;
b = ltmp;
}
rr = &(r[na]);
if (nb <= 0) {
(void)bn_mul_words(r, a, na, 0);
return;
} else
rr[0] = bn_mul_words(r, a, na, b[0]);
for (;;) {
if (--nb <= 0)
return;
rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]);
if (--nb <= 0)
return;
rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]);
if (--nb <= 0)
return;
rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]);
if (--nb <= 0)
return;
rr[4] = bn_mul_add_words(&(r[4]), a, na, b[4]);
rr += 4;
r += 4;
b += 4;
}
} | ['static int test_mod_exp(int round)\n{\n BN_CTX *ctx;\n unsigned char c;\n int ret = 0;\n BIGNUM *r_mont = NULL;\n BIGNUM *r_mont_const = NULL;\n BIGNUM *r_recp = NULL;\n BIGNUM *r_simple = NULL;\n BIGNUM *a = NULL;\n BIGNUM *b = NULL;\n BIGNUM *m = NULL;\n if (!TEST_ptr(ctx = BN_CTX_new()))\n goto err;\n if (!TEST_ptr(r_mont = BN_new())\n || !TEST_ptr(r_mont_const = BN_new())\n || !TEST_ptr(r_recp = BN_new())\n || !TEST_ptr(r_simple = BN_new())\n || !TEST_ptr(a = BN_new())\n || !TEST_ptr(b = BN_new())\n || !TEST_ptr(m = BN_new()))\n goto err;\n RAND_bytes(&c, 1);\n c = (c % BN_BITS) - BN_BITS2;\n BN_rand(a, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY);\n RAND_bytes(&c, 1);\n c = (c % BN_BITS) - BN_BITS2;\n BN_rand(b, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY);\n RAND_bytes(&c, 1);\n c = (c % BN_BITS) - BN_BITS2;\n BN_rand(m, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD);\n if (!TEST_true(BN_mod(a, a, m, ctx))\n || !TEST_true(BN_mod(b, b, m, ctx))\n || !TEST_true(BN_mod_exp_mont(r_mont, a, b, m, ctx, NULL))\n || !TEST_true(BN_mod_exp_recp(r_recp, a, b, m, ctx))\n || !TEST_true(BN_mod_exp_simple(r_simple, a, b, m, ctx))\n || !TEST_true(BN_mod_exp_mont_consttime(r_mont_const, a, b, m, ctx, NULL)))\n goto err;\n if (!TEST_BN_eq(r_simple, r_mont)\n || !TEST_BN_eq(r_simple, r_recp)\n || !TEST_BN_eq(r_simple, r_mont_const)) {\n if (BN_cmp(r_simple, r_mont) != 0)\n TEST_info("simple and mont results differ");\n if (BN_cmp(r_simple, r_mont_const) != 0)\n TEST_info("simple and mont const time results differ");\n if (BN_cmp(r_simple, r_recp) != 0)\n TEST_info("simple and recp results differ");\n BN_print_var(a);\n BN_print_var(b);\n BN_print_var(m);\n BN_print_var(r_simple);\n BN_print_var(r_recp);\n BN_print_var(r_mont);\n BN_print_var(r_mont_const);\n goto err;\n }\n ret = 1;\n err:\n BN_free(r_mont);\n BN_free(r_mont_const);\n BN_free(r_recp);\n BN_free(r_simple);\n BN_free(a);\n BN_free(b);\n BN_free(m);\n BN_CTX_free(ctx);\n return ret;\n}', 'int BN_rand(BIGNUM *rnd, int bits, int top, int bottom)\n{\n return bnrand(NORMAL, rnd, bits, top, bottom);\n}', 'static int bnrand(BNRAND_FLAG flag, BIGNUM *rnd, int bits, int top, int bottom)\n{\n unsigned char *buf = NULL;\n int b, ret = 0, bit, bytes, mask;\n if (bits == 0) {\n if (top != BN_RAND_TOP_ANY || bottom != BN_RAND_BOTTOM_ANY)\n goto toosmall;\n BN_zero(rnd);\n return 1;\n }\n if (bits < 0 || (bits == 1 && top > 0))\n goto toosmall;\n bytes = (bits + 7) / 8;\n bit = (bits - 1) % 8;\n mask = 0xff << (bit + 1);\n buf = OPENSSL_malloc(bytes);\n if (buf == NULL) {\n BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n b = flag == NORMAL ? RAND_bytes(buf, bytes) : RAND_priv_bytes(buf, bytes);\n if (b <= 0)\n goto err;\n if (flag == TESTING) {\n int i;\n unsigned char c;\n for (i = 0; i < bytes; i++) {\n if (RAND_bytes(&c, 1) <= 0)\n goto err;\n if (c >= 128 && i > 0)\n buf[i] = buf[i - 1];\n else if (c < 42)\n buf[i] = 0;\n else if (c < 84)\n buf[i] = 255;\n }\n }\n if (top >= 0) {\n if (top) {\n if (bit == 0) {\n buf[0] = 1;\n buf[1] |= 0x80;\n } else {\n buf[0] |= (3 << (bit - 1));\n }\n } else {\n buf[0] |= (1 << bit);\n }\n }\n buf[0] &= ~mask;\n if (bottom)\n buf[bytes - 1] |= 1;\n if (!BN_bin2bn(buf, bytes, rnd))\n goto err;\n ret = 1;\n err:\n OPENSSL_clear_free(buf, bytes);\n bn_check_top(rnd);\n return ret;\ntoosmall:\n BNerr(BN_F_BNRAND, BN_R_BITS_TOO_SMALL);\n return 0;\n}', 'int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n int start = 1;\n BIGNUM *d, *r;\n const BIGNUM *aa;\n BIGNUM *val[TABLE_SIZE];\n BN_MONT_CTX *mont = NULL;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(a, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont);\n }\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_abs_is_word(m, 1)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n d = BN_CTX_get(ctx);\n r = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (val[0] == NULL)\n goto err;\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n if (a->neg || BN_ucmp(a, m) >= 0) {\n if (!BN_nnmod(val[0], a, m, ctx))\n goto err;\n aa = val[0];\n } else\n aa = a;\n if (!bn_to_mont_fixed_top(val[0], aa, mont, ctx))\n goto err;\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!bn_mul_mont_fixed_top(d, val[0], val[0], mont, ctx))\n goto err;\n j = 1 << (window - 1);\n for (i = 1; i < j; i++) {\n if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n !bn_mul_mont_fixed_top(val[i], val[i - 1], d, mont, ctx))\n goto err;\n }\n }\n start = 1;\n wvalue = 0;\n wstart = bits - 1;\n wend = 0;\n#if 1\n j = m->top;\n if (m->d[j - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n if (bn_wexpand(r, j) == NULL)\n goto err;\n r->d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < j; i++)\n r->d[i] = (~m->d[i]) & BN_MASK2;\n r->top = j;\n r->flags |= BN_FLG_FIXED_TOP;\n } else\n#endif\n if (!bn_to_mont_fixed_top(r, BN_value_one(), mont, ctx))\n goto err;\n for (;;) {\n if (BN_is_bit_set(p, wstart) == 0) {\n if (!start) {\n if (!bn_mul_mont_fixed_top(r, r, r, mont, ctx))\n goto err;\n }\n if (wstart == 0)\n break;\n wstart--;\n continue;\n }\n j = wstart;\n wvalue = 1;\n wend = 0;\n for (i = 1; i < window; i++) {\n if (wstart - i < 0)\n break;\n if (BN_is_bit_set(p, wstart - i)) {\n wvalue <<= (i - wend);\n wvalue |= 1;\n wend = i;\n }\n }\n j = wend + 1;\n if (!start)\n for (i = 0; i < j; i++) {\n if (!bn_mul_mont_fixed_top(r, r, r, mont, ctx))\n goto err;\n }\n if (!bn_mul_mont_fixed_top(r, r, val[wvalue >> 1], mont, ctx))\n goto err;\n wstart -= wend + 1;\n wvalue = 0;\n start = 0;\n if (wstart < 0)\n break;\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n j = mont->N.top;\n val[0]->d[0] = 1;\n for (i = 1; i < j; i++)\n val[0]->d[i] = 0;\n val[0]->top = j;\n if (!BN_mod_mul_montgomery(rr, r, val[0], mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, r, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n BN_CTX_end(ctx);\n bn_check_top(rr);\n return ret;\n}', 'int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx,\n BN_MONT_CTX *in_mont)\n{\n int i, bits, ret = 0, window, wvalue, wmask, window0;\n int top;\n BN_MONT_CTX *mont = NULL;\n int numPowers;\n unsigned char *powerbufFree = NULL;\n int powerbufLen = 0;\n unsigned char *powerbuf = NULL;\n BIGNUM tmp, am;\n#if defined(SPARC_T4_MONT)\n unsigned int t4 = 0;\n#endif\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT_CONSTTIME, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n top = m->top;\n bits = p->top * BN_BITS2;\n if (bits == 0) {\n if (BN_abs_is_word(m, 1)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n if (a->neg || BN_ucmp(a, m) >= 0) {\n BIGNUM *reduced = BN_CTX_get(ctx);\n if (reduced == NULL\n || !BN_nnmod(reduced, a, m, ctx)) {\n goto err;\n }\n a = reduced;\n }\n#ifdef RSAZ_ENABLED\n if ((16 == a->top) && (16 == p->top) && (BN_num_bits(m) == 1024)\n && rsaz_avx2_eligible()) {\n if (NULL == bn_wexpand(rr, 16))\n goto err;\n RSAZ_1024_mod_exp_avx2(rr->d, a->d, p->d, m->d, mont->RR.d,\n mont->n0[0]);\n rr->top = 16;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n } else if ((8 == a->top) && (8 == p->top) && (BN_num_bits(m) == 512)) {\n if (NULL == bn_wexpand(rr, 8))\n goto err;\n RSAZ_512_mod_exp(rr->d, a->d, p->d, m->d, mont->n0[0], mont->RR.d);\n rr->top = 8;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n }\n#endif\n window = BN_window_bits_for_ctime_exponent_size(bits);\n#if defined(SPARC_T4_MONT)\n if (window >= 5 && (top & 15) == 0 && top <= 64 &&\n (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) ==\n (CFR_MONTMUL | CFR_MONTSQR) && (t4 = OPENSSL_sparcv9cap_P[0]))\n window = 5;\n else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window >= 5) {\n window = 5;\n powerbufLen += top * sizeof(mont->N.d[0]);\n }\n#endif\n (void)0;\n numPowers = 1 << window;\n powerbufLen += sizeof(m->d[0]) * (top * numPowers +\n ((2 * top) >\n numPowers ? (2 * top) : numPowers));\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree =\n alloca(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH);\n else\n#endif\n if ((powerbufFree =\n OPENSSL_malloc(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH))\n == NULL)\n goto err;\n powerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);\n memset(powerbuf, 0, powerbufLen);\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree = NULL;\n#endif\n tmp.d = (BN_ULONG *)(powerbuf + sizeof(m->d[0]) * top * numPowers);\n am.d = tmp.d + top;\n tmp.top = am.top = 0;\n tmp.dmax = am.dmax = top;\n tmp.neg = am.neg = 0;\n tmp.flags = am.flags = BN_FLG_STATIC_DATA;\n#if 1\n if (m->d[top - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n tmp.d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < top; i++)\n tmp.d[i] = (~m->d[i]) & BN_MASK2;\n tmp.top = top;\n } else\n#endif\n if (!bn_to_mont_fixed_top(&tmp, BN_value_one(), mont, ctx))\n goto err;\n if (!bn_to_mont_fixed_top(&am, a, mont, ctx))\n goto err;\n#if defined(SPARC_T4_MONT)\n if (t4) {\n typedef int (*bn_pwr5_mont_f) (BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_8(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_16(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_24(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_32(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n static const bn_pwr5_mont_f pwr5_funcs[4] = {\n bn_pwr5_mont_t4_8, bn_pwr5_mont_t4_16,\n bn_pwr5_mont_t4_24, bn_pwr5_mont_t4_32\n };\n bn_pwr5_mont_f pwr5_worker = pwr5_funcs[top / 16 - 1];\n typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap, const void *bp,\n const BN_ULONG *np, const BN_ULONG *n0);\n int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n static const bn_mul_mont_f mul_funcs[4] = {\n bn_mul_mont_t4_8, bn_mul_mont_t4_16,\n bn_mul_mont_t4_24, bn_mul_mont_t4_32\n };\n bn_mul_mont_f mul_worker = mul_funcs[top / 16 - 1];\n void bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_gather5_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_flip_n_scatter5_t4(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5_t4(BN_ULONG *out, size_t num,\n void *table, size_t power);\n void bn_flip_t4(BN_ULONG *dst, BN_ULONG *src, size_t num);\n BN_ULONG *np = mont->N.d, *n0 = mont->n0;\n int stride = 5 * (6 - (top / 16 - 1));\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 0);\n bn_flip_n_scatter5_t4(am.d, top, powerbuf, 1);\n if (!(*mul_worker) (tmp.d, am.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, am.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, am.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 2);\n for (i = 3; i < 32; i++) {\n if (!(*mul_worker) (tmp.d, tmp.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, tmp.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, tmp.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, i);\n }\n np = alloca(top * sizeof(BN_ULONG));\n top /= 2;\n bn_flip_t4(np, mont->N.d, top);\n window0 = (bits - 1) % 5 + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n bn_gather5_t4(tmp.d, top, powerbuf, wvalue);\n while (bits > 0) {\n if (bits < stride)\n stride = bits;\n bits -= stride;\n wvalue = bn_get_bits(p, bits);\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n bits += stride - 5;\n wvalue >>= stride - 5;\n wvalue &= 31;\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5_t4(tmp.d, tmp.d, powerbuf, np, n0, top,\n wvalue);\n }\n bn_flip_t4(tmp.d, tmp.d, top);\n top *= 2;\n tmp.top = top;\n bn_correct_top(&tmp);\n OPENSSL_cleanse(np, top * sizeof(BN_ULONG));\n } else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window == 5 && top > 1) {\n void bn_mul_mont_gather5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_scatter5(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5(BN_ULONG *out, size_t num, void *table, size_t power);\n void bn_power5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n int bn_get_bits5(const BN_ULONG *ap, int off);\n int bn_from_montgomery(BN_ULONG *rp, const BN_ULONG *ap,\n const BN_ULONG *not_used, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n BN_ULONG *n0 = mont->n0, *np;\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n for (np = am.d + top, i = 0; i < top; i++)\n np[i] = mont->N.d[i];\n bn_scatter5(tmp.d, top, powerbuf, 0);\n bn_scatter5(am.d, am.top, powerbuf, 1);\n bn_mul_mont(tmp.d, am.d, am.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2);\n# if 0\n for (i = 3; i < 32; i++) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# else\n for (i = 4; i < 32; i *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n for (i = 3; i < 8; i += 2) {\n int j;\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n for (j = 2 * i; j < 32; j *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, j);\n }\n }\n for (; i < 16; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2 * i);\n }\n for (; i < 32; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# endif\n window0 = (bits - 1) % 5 + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n bn_gather5(tmp.d, top, powerbuf, wvalue);\n if (top & 7) {\n while (bits > 0) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5(tmp.d, tmp.d, powerbuf, np, n0, top,\n bn_get_bits5(p->d, bits -= 5));\n }\n } else {\n while (bits > 0) {\n bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top,\n bn_get_bits5(p->d, bits -= 5));\n }\n }\n ret = bn_from_montgomery(tmp.d, tmp.d, NULL, np, n0, top);\n tmp.top = top;\n bn_correct_top(&tmp);\n if (ret) {\n if (!BN_copy(rr, &tmp))\n ret = 0;\n goto err;\n }\n } else\n#endif\n {\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 0, window))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, window))\n goto err;\n if (window > 1) {\n if (!bn_mul_mont_fixed_top(&tmp, &am, &am, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 2,\n window))\n goto err;\n for (i = 3; i < numPowers; i++) {\n if (!bn_mul_mont_fixed_top(&tmp, &am, &tmp, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, i,\n window))\n goto err;\n }\n }\n window0 = (bits - 1) % window + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&tmp, top, powerbuf, wvalue,\n window))\n goto err;\n wmask = (1 << window) - 1;\n while (bits > 0) {\n for (i = 0; i < window; i++)\n if (!bn_mul_mont_fixed_top(&tmp, &tmp, &tmp, mont, ctx))\n goto err;\n bits -= window;\n wvalue = bn_get_bits(p, bits) & wmask;\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&am, top, powerbuf, wvalue,\n window))\n goto err;\n if (!bn_mul_mont_fixed_top(&tmp, &tmp, &am, mont, ctx))\n goto err;\n }\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n am.d[0] = 1;\n for (i = 1; i < top; i++)\n am.d[i] = 0;\n if (!BN_mod_mul_montgomery(rr, &tmp, &am, mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, &tmp, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n if (powerbuf != NULL) {\n OPENSSL_cleanse(powerbuf, powerbufLen);\n OPENSSL_free(powerbufFree);\n }\n BN_CTX_end(ctx);\n return ret;\n}', 'int bn_to_mont_fixed_top(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,\n BN_CTX *ctx)\n{\n return bn_mul_mont_fixed_top(r, a, &(mont->RR), mont, ctx);\n}', 'int bn_mul_mont_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n BN_MONT_CTX *mont, BN_CTX *ctx)\n{\n BIGNUM *tmp;\n int ret = 0;\n int num = mont->N.top;\n#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\n if (num > 1 && a->top == num && b->top == num) {\n if (bn_wexpand(r, num) == NULL)\n return 0;\n if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {\n r->neg = a->neg ^ b->neg;\n r->top = num;\n r->flags |= BN_FLG_FIXED_TOP;\n return 1;\n }\n }\n#endif\n if ((a->top + b->top) > 2 * num)\n return 0;\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n if (tmp == NULL)\n goto err;\n bn_check_top(tmp);\n if (a == b) {\n if (!bn_sqr_fixed_top(tmp, a, ctx))\n goto err;\n } else {\n if (!bn_mul_fixed_top(tmp, a, b, ctx))\n goto err;\n }\n#ifdef MONT_WORD\n if (!bn_from_montgomery_word(r, tmp, mont))\n goto err;\n#else\n if (!BN_from_montgomery(r, tmp, mont, ctx))\n goto err;\n#endif\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'int bn_mul_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = 0;\n int top, al, bl;\n BIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n int i;\n#endif\n#ifdef BN_RECURSION\n BIGNUM *t = NULL;\n int j = 0, k;\n#endif\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(r);\n al = a->top;\n bl = b->top;\n if ((al == 0) || (bl == 0)) {\n BN_zero(r);\n return 1;\n }\n top = al + bl;\n BN_CTX_start(ctx);\n if ((r == a) || (r == b)) {\n if ((rr = BN_CTX_get(ctx)) == NULL)\n goto err;\n } else\n rr = r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n i = al - bl;\n#endif\n#ifdef BN_MUL_COMBA\n if (i == 0) {\n# if 0\n if (al == 4) {\n if (bn_wexpand(rr, 8) == NULL)\n goto err;\n rr->top = 8;\n bn_mul_comba4(rr->d, a->d, b->d);\n goto end;\n }\n# endif\n if (al == 8) {\n if (bn_wexpand(rr, 16) == NULL)\n goto err;\n rr->top = 16;\n bn_mul_comba8(rr->d, a->d, b->d);\n goto end;\n }\n }\n#endif\n#ifdef BN_RECURSION\n if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {\n if (i >= -1 && i <= 1) {\n if (i >= 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n }\n if (i == -1) {\n j = BN_num_bits_word((BN_ULONG)bl);\n }\n j = 1 << (j - 1);\n assert(j <= al || j <= bl);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (al > j || bl > j) {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d,\n j, al - j, bl - j, t->d);\n } else {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);\n }\n rr->top = top;\n goto end;\n }\n }\n#endif\n if (bn_wexpand(rr, top) == NULL)\n goto err;\n rr->top = top;\n bn_mul_normal(rr->d, a->d, al, b->d, bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n end:\n#endif\n rr->neg = a->neg ^ b->neg;\n rr->flags |= BN_FLG_FIXED_TOP;\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(r);\n BN_CTX_end(ctx);\n return ret;\n}', 'void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n,\n int tna, int tnb, BN_ULONG *t)\n{\n int i, j, n2 = n * 2;\n int c1, c2, neg;\n BN_ULONG ln, lo, *p;\n if (n < 8) {\n bn_mul_normal(r, a, n + tna, b, n + tnb);\n return;\n }\n c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);\n c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);\n neg = 0;\n switch (c1 * 3 + c2) {\n case -4:\n bn_sub_part_words(t, &(a[n]), a, tna, tna - n);\n bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb);\n break;\n case -3:\n case -2:\n bn_sub_part_words(t, &(a[n]), a, tna, tna - n);\n bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);\n neg = 1;\n break;\n case -1:\n case 0:\n case 1:\n case 2:\n bn_sub_part_words(t, a, &(a[n]), tna, n - tna);\n bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb);\n neg = 1;\n break;\n case 3:\n case 4:\n bn_sub_part_words(t, a, &(a[n]), tna, n - tna);\n bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);\n break;\n }\n# if 0\n if (n == 4) {\n bn_mul_comba4(&(t[n2]), t, &(t[n]));\n bn_mul_comba4(r, a, b);\n bn_mul_normal(&(r[n2]), &(a[n]), tn, &(b[n]), tn);\n memset(&r[n2 + tn * 2], 0, sizeof(*r) * (n2 - tn * 2));\n } else\n# endif\n if (n == 8) {\n bn_mul_comba8(&(t[n2]), t, &(t[n]));\n bn_mul_comba8(r, a, b);\n bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);\n memset(&r[n2 + tna + tnb], 0, sizeof(*r) * (n2 - tna - tnb));\n } else {\n p = &(t[n2 * 2]);\n bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);\n bn_mul_recursive(r, a, b, n, 0, 0, p);\n i = n / 2;\n if (tna > tnb)\n j = tna - i;\n else\n j = tnb - i;\n if (j == 0) {\n bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n memset(&r[n2 + i * 2], 0, sizeof(*r) * (n2 - i * 2));\n } else if (j > 0) {\n bn_mul_part_recursive(&(r[n2]), &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n memset(&(r[n2 + tna + tnb]), 0,\n sizeof(BN_ULONG) * (n2 - tna - tnb));\n } else {\n memset(&r[n2], 0, sizeof(*r) * n2);\n if (tna < BN_MUL_RECURSIVE_SIZE_NORMAL\n && tnb < BN_MUL_RECURSIVE_SIZE_NORMAL) {\n bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);\n } else {\n for (;;) {\n i /= 2;\n if (i < tna || i < tnb) {\n bn_mul_part_recursive(&(r[n2]),\n &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n break;\n } else if (i == tna || i == tnb) {\n bn_mul_recursive(&(r[n2]),\n &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n break;\n }\n }\n }\n }\n }\n c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));\n if (neg) {\n c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));\n } else {\n c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));\n }\n c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));\n if (c1) {\n p = &(r[n + n2]);\n lo = *p;\n ln = (lo + c1) & BN_MASK2;\n *p = ln;\n if (ln < (BN_ULONG)c1) {\n do {\n p++;\n lo = *p;\n ln = (lo + 1) & BN_MASK2;\n *p = ln;\n } while (ln == 0);\n }\n }\n}', 'void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb)\n{\n BN_ULONG *rr;\n if (na < nb) {\n int itmp;\n BN_ULONG *ltmp;\n itmp = na;\n na = nb;\n nb = itmp;\n ltmp = a;\n a = b;\n b = ltmp;\n }\n rr = &(r[na]);\n if (nb <= 0) {\n (void)bn_mul_words(r, a, na, 0);\n return;\n } else\n rr[0] = bn_mul_words(r, a, na, b[0]);\n for (;;) {\n if (--nb <= 0)\n return;\n rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]);\n if (--nb <= 0)\n return;\n rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]);\n if (--nb <= 0)\n return;\n rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]);\n if (--nb <= 0)\n return;\n rr[4] = bn_mul_add_words(&(r[4]), a, na, b[4]);\n rr += 4;\n r += 4;\n b += 4;\n }\n}'] |
514 | 1 | https://github.com/openssl/openssl/blob/96826bfc84c63207b720543443626029946a0fc7/engines/e_sureware.c/#L709 | static EVP_PKEY* sureware_load_public(ENGINE *e,const char *key_id,char *hptr,unsigned long el,char keytype)
{
EVP_PKEY *res = NULL;
#ifndef OPENSSL_NO_RSA
RSA *rsatmp = NULL;
#endif
#ifndef OPENSSL_NO_DSA
DSA *dsatmp=NULL;
#endif
char msg[64]="sureware_load_public";
int ret=0;
if(!p_surewarehk_Load_Rsa_Pubkey || !p_surewarehk_Load_Dsa_Pubkey)
{
SUREWAREerr(SUREWARE_F_SUREWARE_LOAD_PUBLIC,ENGINE_R_NOT_INITIALISED);
goto err;
}
switch (keytype)
{
#ifndef OPENSSL_NO_RSA
case 1:
rsatmp = RSA_new_method(e);
RSA_set_ex_data(rsatmp,rsaHndidx,hptr);
rsatmp->flags |= RSA_FLAG_EXT_PKEY;
rsatmp->e = BN_new();
rsatmp->n = BN_new();
bn_expand2(rsatmp->e, el/sizeof(BN_ULONG));
bn_expand2(rsatmp->n, el/sizeof(BN_ULONG));
if (!rsatmp->e || rsatmp->e->dmax!=(int)(el/sizeof(BN_ULONG))||
!rsatmp->n || rsatmp->n->dmax!=(int)(el/sizeof(BN_ULONG)))
goto err;
ret=p_surewarehk_Load_Rsa_Pubkey(msg,key_id,el,
(unsigned long *)rsatmp->n->d,
(unsigned long *)rsatmp->e->d);
surewarehk_error_handling(msg,SUREWARE_F_SUREWARE_LOAD_PUBLIC,ret);
if (ret!=1)
{
SUREWAREerr(SUREWARE_F_SUREWARE_LOAD_PUBLIC,ENGINE_R_FAILED_LOADING_PUBLIC_KEY);
goto err;
}
rsatmp->e->top=el/sizeof(BN_ULONG);
bn_fix_top(rsatmp->e);
rsatmp->n->top=el/sizeof(BN_ULONG);
bn_fix_top(rsatmp->n);
res = EVP_PKEY_new();
EVP_PKEY_assign_RSA(res, rsatmp);
break;
#endif
#ifndef OPENSSL_NO_DSA
case 2:
dsatmp = DSA_new_method(e);
DSA_set_ex_data(dsatmp,dsaHndidx,hptr);
dsatmp->pub_key = BN_new();
dsatmp->p = BN_new();
dsatmp->q = BN_new();
dsatmp->g = BN_new();
bn_expand2(dsatmp->pub_key, el/sizeof(BN_ULONG));
bn_expand2(dsatmp->p, el/sizeof(BN_ULONG));
bn_expand2(dsatmp->q, 20/sizeof(BN_ULONG));
bn_expand2(dsatmp->g, el/sizeof(BN_ULONG));
if (!dsatmp->pub_key || dsatmp->pub_key->dmax!=(int)(el/sizeof(BN_ULONG))||
!dsatmp->p || dsatmp->p->dmax!=(int)(el/sizeof(BN_ULONG)) ||
!dsatmp->q || dsatmp->q->dmax!=20/sizeof(BN_ULONG) ||
!dsatmp->g || dsatmp->g->dmax!=(int)(el/sizeof(BN_ULONG)))
goto err;
ret=p_surewarehk_Load_Dsa_Pubkey(msg,key_id,el,
(unsigned long *)dsatmp->pub_key->d,
(unsigned long *)dsatmp->p->d,
(unsigned long *)dsatmp->q->d,
(unsigned long *)dsatmp->g->d);
surewarehk_error_handling(msg,SUREWARE_F_SUREWARE_LOAD_PUBLIC,ret);
if (ret!=1)
{
SUREWAREerr(SUREWARE_F_SUREWARE_LOAD_PUBLIC,ENGINE_R_FAILED_LOADING_PUBLIC_KEY);
goto err;
}
dsatmp->pub_key->top=el/sizeof(BN_ULONG);
bn_fix_top(dsatmp->pub_key);
dsatmp->p->top=el/sizeof(BN_ULONG);
bn_fix_top(dsatmp->p);
dsatmp->q->top=20/sizeof(BN_ULONG);
bn_fix_top(dsatmp->q);
dsatmp->g->top=el/sizeof(BN_ULONG);
bn_fix_top(dsatmp->g);
res = EVP_PKEY_new();
EVP_PKEY_assign_DSA(res, dsatmp);
break;
#endif
default:
SUREWAREerr(SUREWARE_F_SUREWARE_LOAD_PUBLIC,ENGINE_R_FAILED_LOADING_PRIVATE_KEY);
goto err;
}
return res;
err:
#ifndef OPENSSL_NO_RSA
if (rsatmp)
RSA_free(rsatmp);
#endif
#ifndef OPENSSL_NO_DSA
if (dsatmp)
DSA_free(dsatmp);
#endif
return NULL;
} | ['static EVP_PKEY* sureware_load_public(ENGINE *e,const char *key_id,char *hptr,unsigned long el,char keytype)\n{\n\tEVP_PKEY *res = NULL;\n#ifndef OPENSSL_NO_RSA\n\tRSA *rsatmp = NULL;\n#endif\n#ifndef OPENSSL_NO_DSA\n\tDSA *dsatmp=NULL;\n#endif\n\tchar msg[64]="sureware_load_public";\n\tint ret=0;\n\tif(!p_surewarehk_Load_Rsa_Pubkey || !p_surewarehk_Load_Dsa_Pubkey)\n\t{\n\t\tSUREWAREerr(SUREWARE_F_SUREWARE_LOAD_PUBLIC,ENGINE_R_NOT_INITIALISED);\n\t\tgoto err;\n\t}\n\tswitch (keytype)\n\t{\n#ifndef OPENSSL_NO_RSA\n\tcase 1:\n\t\trsatmp = RSA_new_method(e);\n\t\tRSA_set_ex_data(rsatmp,rsaHndidx,hptr);\n\t\trsatmp->flags |= RSA_FLAG_EXT_PKEY;\n\t\trsatmp->e = BN_new();\n\t\trsatmp->n = BN_new();\n\t\tbn_expand2(rsatmp->e, el/sizeof(BN_ULONG));\n\t\tbn_expand2(rsatmp->n, el/sizeof(BN_ULONG));\n\t\tif (!rsatmp->e || rsatmp->e->dmax!=(int)(el/sizeof(BN_ULONG))||\n\t\t\t!rsatmp->n || rsatmp->n->dmax!=(int)(el/sizeof(BN_ULONG)))\n\t\t\tgoto err;\n\t\tret=p_surewarehk_Load_Rsa_Pubkey(msg,key_id,el,\n\t\t\t\t\t\t (unsigned long *)rsatmp->n->d,\n\t\t\t\t\t\t (unsigned long *)rsatmp->e->d);\n\t\tsurewarehk_error_handling(msg,SUREWARE_F_SUREWARE_LOAD_PUBLIC,ret);\n\t\tif (ret!=1)\n\t\t{\n\t\t\tSUREWAREerr(SUREWARE_F_SUREWARE_LOAD_PUBLIC,ENGINE_R_FAILED_LOADING_PUBLIC_KEY);\n\t\t\tgoto err;\n\t\t}\n\t\trsatmp->e->top=el/sizeof(BN_ULONG);\n\t\tbn_fix_top(rsatmp->e);\n\t\trsatmp->n->top=el/sizeof(BN_ULONG);\n\t\tbn_fix_top(rsatmp->n);\n\t\tres = EVP_PKEY_new();\n\t\tEVP_PKEY_assign_RSA(res, rsatmp);\n\t\tbreak;\n#endif\n#ifndef OPENSSL_NO_DSA\n\tcase 2:\n\t\tdsatmp = DSA_new_method(e);\n\t\tDSA_set_ex_data(dsatmp,dsaHndidx,hptr);\n\t\tdsatmp->pub_key = BN_new();\n\t\tdsatmp->p = BN_new();\n\t\tdsatmp->q = BN_new();\n\t\tdsatmp->g = BN_new();\n\t\tbn_expand2(dsatmp->pub_key, el/sizeof(BN_ULONG));\n\t\tbn_expand2(dsatmp->p, el/sizeof(BN_ULONG));\n\t\tbn_expand2(dsatmp->q, 20/sizeof(BN_ULONG));\n\t\tbn_expand2(dsatmp->g, el/sizeof(BN_ULONG));\n\t\tif (!dsatmp->pub_key || dsatmp->pub_key->dmax!=(int)(el/sizeof(BN_ULONG))||\n\t\t\t!dsatmp->p || dsatmp->p->dmax!=(int)(el/sizeof(BN_ULONG)) ||\n\t\t\t!dsatmp->q || dsatmp->q->dmax!=20/sizeof(BN_ULONG) ||\n\t\t\t!dsatmp->g || dsatmp->g->dmax!=(int)(el/sizeof(BN_ULONG)))\n\t\t\tgoto err;\n\t\tret=p_surewarehk_Load_Dsa_Pubkey(msg,key_id,el,\n\t\t\t\t\t\t (unsigned long *)dsatmp->pub_key->d,\n\t\t\t\t\t\t (unsigned long *)dsatmp->p->d,\n\t\t\t\t\t\t (unsigned long *)dsatmp->q->d,\n\t\t\t\t\t\t (unsigned long *)dsatmp->g->d);\n\t\tsurewarehk_error_handling(msg,SUREWARE_F_SUREWARE_LOAD_PUBLIC,ret);\n\t\tif (ret!=1)\n\t\t{\n\t\t\tSUREWAREerr(SUREWARE_F_SUREWARE_LOAD_PUBLIC,ENGINE_R_FAILED_LOADING_PUBLIC_KEY);\n\t\t\tgoto err;\n\t\t}\n\t\tdsatmp->pub_key->top=el/sizeof(BN_ULONG);\n\t\tbn_fix_top(dsatmp->pub_key);\n\t\tdsatmp->p->top=el/sizeof(BN_ULONG);\n\t\tbn_fix_top(dsatmp->p);\n\t\tdsatmp->q->top=20/sizeof(BN_ULONG);\n\t\tbn_fix_top(dsatmp->q);\n\t\tdsatmp->g->top=el/sizeof(BN_ULONG);\n\t\tbn_fix_top(dsatmp->g);\n\t\tres = EVP_PKEY_new();\n\t\tEVP_PKEY_assign_DSA(res, dsatmp);\n\t\tbreak;\n#endif\n\tdefault:\n\t\tSUREWAREerr(SUREWARE_F_SUREWARE_LOAD_PUBLIC,ENGINE_R_FAILED_LOADING_PRIVATE_KEY);\n\t\tgoto err;\n\t}\n\treturn res;\n err:\n#ifndef OPENSSL_NO_RSA\n\tif (rsatmp)\n\t\tRSA_free(rsatmp);\n#endif\n#ifndef OPENSSL_NO_DSA\n\tif (dsatmp)\n\t\tDSA_free(dsatmp);\n#endif\n\treturn NULL;\n}', 'DSA *DSA_new_method(ENGINE *engine)\n\t{\n\tDSA *ret;\n\tret=(DSA *)OPENSSL_malloc(sizeof(DSA));\n\tif (ret == NULL)\n\t\t{\n\t\tDSAerr(DSA_F_DSA_NEW_METHOD,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tret->meth = DSA_get_default_method();\n#ifndef OPENSSL_NO_ENGINE\n\tif (engine)\n\t\t{\n\t\tif (!ENGINE_init(engine))\n\t\t\t{\n\t\t\tDSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB);\n\t\t\tOPENSSL_free(ret);\n\t\t\treturn NULL;\n\t\t\t}\n\t\tret->engine = engine;\n\t\t}\n\telse\n\t\tret->engine = ENGINE_get_default_DSA();\n\tif(ret->engine)\n\t\t{\n\t\tret->meth = ENGINE_get_DSA(ret->engine);\n\t\tif(!ret->meth)\n\t\t\t{\n\t\t\tDSAerr(DSA_F_DSA_NEW_METHOD,\n\t\t\t\tERR_R_ENGINE_LIB);\n\t\t\tENGINE_finish(ret->engine);\n\t\t\tOPENSSL_free(ret);\n\t\t\treturn NULL;\n\t\t\t}\n\t\t}\n#endif\n\tret->pad=0;\n\tret->version=0;\n\tret->write_params=1;\n\tret->p=NULL;\n\tret->q=NULL;\n\tret->g=NULL;\n\tret->pub_key=NULL;\n\tret->priv_key=NULL;\n\tret->kinv=NULL;\n\tret->r=NULL;\n\tret->method_mont_p=NULL;\n\tret->references=1;\n\tret->flags=ret->meth->flags;\n\tCRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data);\n\tif ((ret->meth->init != NULL) && !ret->meth->init(ret))\n\t\t{\n#ifndef OPENSSL_NO_ENGINE\n\t\tif (ret->engine)\n\t\t\tENGINE_finish(ret->engine);\n#endif\n\t\tCRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data);\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\tif (num <= 0) return NULL;\n\tallow_customize = 0;\n\tif (malloc_debug_func != NULL)\n\t\t{\n\t\tallow_customize_debug = 0;\n\t\tmalloc_debug_func(NULL, num, file, line, 0);\n\t\t}\n\tret = malloc_ex_func(num,file,line);\n#ifdef LEVITTE_DEBUG_MEM\n\tfprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\\n", ret, num);\n#endif\n\tif (malloc_debug_func != NULL)\n\t\tmalloc_debug_func(ret, num, file, line, 1);\n#ifndef OPENSSL_CPUID_OBJ\n if(ret && (num > 2048))\n\t{\textern unsigned char cleanse_ctr;\n ((unsigned char *)ret)[0] = cleanse_ctr;\n\t}\n#endif\n\treturn ret;\n\t}', 'void ERR_put_error(int lib, int func, int reason, const char *file,\n\t int line)\n\t{\n\tERR_STATE *es;\n#ifdef _OSD_POSIX\n\tif (strncmp(file,"*POSIX(", sizeof("*POSIX(")-1) == 0) {\n\t\tchar *end;\n\t\tfile += sizeof("*POSIX(")-1;\n\t\tend = &file[strlen(file)-1];\n\t\tif (*end == \')\')\n\t\t\t*end = \'\\0\';\n\t\tif ((end = strrchr(file, \'/\')) != NULL)\n\t\t\tfile = &end[1];\n\t}\n#endif\n\tes=ERR_get_state();\n\tes->top=(es->top+1)%ERR_NUM_ERRORS;\n\tif (es->top == es->bottom)\n\t\tes->bottom=(es->bottom+1)%ERR_NUM_ERRORS;\n\tes->err_flags[es->top]=0;\n\tes->err_buffer[es->top]=ERR_PACK(lib,func,reason);\n\tes->err_file[es->top]=file;\n\tes->err_line[es->top]=line;\n\terr_clear_data(es,es->top);\n\t}'] |
515 | 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_channel_sf_idx(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,\n int ch_num, AVCodecContext *avctx)\n{\n int i, weight_idx = 0, delta, diff, num_long_vals,\n delta_bits, min_val, vlc_sel, start_val;\n VLC *vlc_tab;\n Atrac3pChanParams *chan = &ctx->channels[ch_num];\n Atrac3pChanParams *ref_chan = &ctx->channels[0];\n switch (bitstream_read(bc, 2)) {\n case 0:\n for (i = 0; i < ctx->used_quant_units; i++)\n chan->qu_sf_idx[i] = bitstream_read(bc, 6);\n break;\n case 1:\n if (ch_num) {\n vlc_tab = &sf_vlc_tabs[bitstream_read(bc, 2)];\n for (i = 0; i < ctx->used_quant_units; i++) {\n delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);\n chan->qu_sf_idx[i] = (ref_chan->qu_sf_idx[i] + delta) & 0x3F;\n }\n } else {\n weight_idx = bitstream_read(bc, 2);\n if (weight_idx == 3) {\n UNPACK_SF_VQ_SHAPE(bc, chan->qu_sf_idx, ctx->used_quant_units);\n num_long_vals = bitstream_read(bc, 5);\n delta_bits = bitstream_read(bc, 2);\n min_val = bitstream_read(bc, 4) - 7;\n for (i = 0; i < num_long_vals; i++)\n chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] +\n bitstream_read(bc, 4) - 7) & 0x3F;\n for (i = num_long_vals; i < ctx->used_quant_units; i++)\n chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] + min_val +\n bitstream_read(bc, delta_bits)) & 0x3F;\n } else {\n num_long_vals = bitstream_read(bc, 5);\n delta_bits = bitstream_read(bc, 3);\n min_val = bitstream_read(bc, 6);\n if (num_long_vals > ctx->used_quant_units || delta_bits == 7) {\n av_log(avctx, AV_LOG_ERROR,\n "SF mode 1: invalid parameters!\\n");\n return AVERROR_INVALIDDATA;\n }\n for (i = 0; i < num_long_vals; i++)\n chan->qu_sf_idx[i] = bitstream_read(bc, 6);\n for (i = num_long_vals; i < ctx->used_quant_units; i++)\n chan->qu_sf_idx[i] = (min_val +\n bitstream_read(bc, delta_bits)) & 0x3F;\n }\n }\n break;\n case 2:\n if (ch_num) {\n vlc_tab = &sf_vlc_tabs[bitstream_read(bc, 2)];\n delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);\n chan->qu_sf_idx[0] = (ref_chan->qu_sf_idx[0] + delta) & 0x3F;\n for (i = 1; i < ctx->used_quant_units; i++) {\n diff = ref_chan->qu_sf_idx[i] - ref_chan->qu_sf_idx[i - 1];\n delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);\n chan->qu_sf_idx[i] = (chan->qu_sf_idx[i - 1] + diff + delta) & 0x3F;\n }\n } else {\n vlc_tab = &sf_vlc_tabs[bitstream_read(bc, 2) + 4];\n UNPACK_SF_VQ_SHAPE(bc, chan->qu_sf_idx, ctx->used_quant_units);\n for (i = 0; i < ctx->used_quant_units; i++) {\n delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);\n chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] +\n sign_extend(delta, 4)) & 0x3F;\n }\n }\n break;\n case 3:\n if (ch_num) {\n for (i = 0; i < ctx->used_quant_units; i++)\n chan->qu_sf_idx[i] = ref_chan->qu_sf_idx[i];\n } else {\n weight_idx = bitstream_read(bc, 2);\n vlc_sel = bitstream_read(bc, 2);\n vlc_tab = &sf_vlc_tabs[vlc_sel];\n if (weight_idx == 3) {\n vlc_tab = &sf_vlc_tabs[vlc_sel + 4];\n UNPACK_SF_VQ_SHAPE(bc, chan->qu_sf_idx, ctx->used_quant_units);\n diff = (bitstream_read(bc, 4) + 56) & 0x3F;\n chan->qu_sf_idx[0] = (chan->qu_sf_idx[0] + diff) & 0x3F;\n for (i = 1; i < ctx->used_quant_units; i++) {\n delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);\n diff = (diff + sign_extend(delta, 4)) & 0x3F;\n chan->qu_sf_idx[i] = (diff + chan->qu_sf_idx[i]) & 0x3F;\n }\n } else {\n chan->qu_sf_idx[0] = bitstream_read(bc, 6);\n for (i = 1; i < ctx->used_quant_units; i++) {\n delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);\n chan->qu_sf_idx[i] = (chan->qu_sf_idx[i - 1] + delta) & 0x3F;\n }\n }\n }\n break;\n }\n if (weight_idx && weight_idx < 3)\n return subtract_sf_weights(ctx, chan, weight_idx, avctx);\n return 0;\n}', 'static inline uint32_t bitstream_read(BitstreamContext *bc, unsigned n)\n{\n if (!n)\n return 0;\n if (n > bc->bits_left) {\n refill_32(bc);\n if (bc->bits_left < 32)\n bc->bits_left = n;\n }\n return get_val(bc, n);\n}', 'static inline uint64_t get_val(BitstreamContext *bc, unsigned n)\n{\n#ifdef BITSTREAM_READER_LE\n uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);\n bc->bits >>= n;\n#else\n uint64_t ret = bc->bits >> (64 - n);\n bc->bits <<= n;\n#endif\n bc->bits_left -= n;\n return ret;\n}'] |
516 | 0 | https://github.com/libav/libav/blob/cf6bae6883607f83f3b042b7b9d711197f736e2a/libavcodec/mpegaudiodec.c/#L737 | static void dct32(int32_t *out, int32_t *tab)
{
int tmp0, tmp1;
BF( 0, 31, COS0_0 , 1);
BF(15, 16, COS0_15, 5);
BF( 0, 15, COS1_0 , 1);
BF(16, 31,-COS1_0 , 1);
BF( 7, 24, COS0_7 , 1);
BF( 8, 23, COS0_8 , 1);
BF( 7, 8, COS1_7 , 4);
BF(23, 24,-COS1_7 , 4);
BF( 0, 7, COS2_0 , 1);
BF( 8, 15,-COS2_0 , 1);
BF(16, 23, COS2_0 , 1);
BF(24, 31,-COS2_0 , 1);
BF( 3, 28, COS0_3 , 1);
BF(12, 19, COS0_12, 2);
BF( 3, 12, COS1_3 , 1);
BF(19, 28,-COS1_3 , 1);
BF( 4, 27, COS0_4 , 1);
BF(11, 20, COS0_11, 2);
BF( 4, 11, COS1_4 , 1);
BF(20, 27,-COS1_4 , 1);
BF( 3, 4, COS2_3 , 3);
BF(11, 12,-COS2_3 , 3);
BF(19, 20, COS2_3 , 3);
BF(27, 28,-COS2_3 , 3);
BF( 0, 3, COS3_0 , 1);
BF( 4, 7,-COS3_0 , 1);
BF( 8, 11, COS3_0 , 1);
BF(12, 15,-COS3_0 , 1);
BF(16, 19, COS3_0 , 1);
BF(20, 23,-COS3_0 , 1);
BF(24, 27, COS3_0 , 1);
BF(28, 31,-COS3_0 , 1);
BF( 1, 30, COS0_1 , 1);
BF(14, 17, COS0_14, 3);
BF( 1, 14, COS1_1 , 1);
BF(17, 30,-COS1_1 , 1);
BF( 6, 25, COS0_6 , 1);
BF( 9, 22, COS0_9 , 1);
BF( 6, 9, COS1_6 , 2);
BF(22, 25,-COS1_6 , 2);
BF( 1, 6, COS2_1 , 1);
BF( 9, 14,-COS2_1 , 1);
BF(17, 22, COS2_1 , 1);
BF(25, 30,-COS2_1 , 1);
BF( 2, 29, COS0_2 , 1);
BF(13, 18, COS0_13, 3);
BF( 2, 13, COS1_2 , 1);
BF(18, 29,-COS1_2 , 1);
BF( 5, 26, COS0_5 , 1);
BF(10, 21, COS0_10, 1);
BF( 5, 10, COS1_5 , 2);
BF(21, 26,-COS1_5 , 2);
BF( 2, 5, COS2_2 , 1);
BF(10, 13,-COS2_2 , 1);
BF(18, 21, COS2_2 , 1);
BF(26, 29,-COS2_2 , 1);
BF( 1, 2, COS3_1 , 2);
BF( 5, 6,-COS3_1 , 2);
BF( 9, 10, COS3_1 , 2);
BF(13, 14,-COS3_1 , 2);
BF(17, 18, COS3_1 , 2);
BF(21, 22,-COS3_1 , 2);
BF(25, 26, COS3_1 , 2);
BF(29, 30,-COS3_1 , 2);
BF1( 0, 1, 2, 3);
BF2( 4, 5, 6, 7);
BF1( 8, 9, 10, 11);
BF2(12, 13, 14, 15);
BF1(16, 17, 18, 19);
BF2(20, 21, 22, 23);
BF1(24, 25, 26, 27);
BF2(28, 29, 30, 31);
ADD( 8, 12);
ADD(12, 10);
ADD(10, 14);
ADD(14, 9);
ADD( 9, 13);
ADD(13, 11);
ADD(11, 15);
out[ 0] = tab[0];
out[16] = tab[1];
out[ 8] = tab[2];
out[24] = tab[3];
out[ 4] = tab[4];
out[20] = tab[5];
out[12] = tab[6];
out[28] = tab[7];
out[ 2] = tab[8];
out[18] = tab[9];
out[10] = tab[10];
out[26] = tab[11];
out[ 6] = tab[12];
out[22] = tab[13];
out[14] = tab[14];
out[30] = tab[15];
ADD(24, 28);
ADD(28, 26);
ADD(26, 30);
ADD(30, 25);
ADD(25, 29);
ADD(29, 27);
ADD(27, 31);
out[ 1] = tab[16] + tab[24];
out[17] = tab[17] + tab[25];
out[ 9] = tab[18] + tab[26];
out[25] = tab[19] + tab[27];
out[ 5] = tab[20] + tab[28];
out[21] = tab[21] + tab[29];
out[13] = tab[22] + tab[30];
out[29] = tab[23] + tab[31];
out[ 3] = tab[24] + tab[20];
out[19] = tab[25] + tab[21];
out[11] = tab[26] + tab[22];
out[27] = tab[27] + tab[23];
out[ 7] = tab[28] + tab[18];
out[23] = tab[29] + tab[19];
out[15] = tab[30] + tab[17];
out[31] = tab[31];
} | ['static void mpc_synth(MPCContext *c, int16_t *out)\n{\n int dither_state = 0;\n int i, ch;\n OUT_INT samples[MPA_MAX_CHANNELS * MPA_FRAME_SIZE], *samples_ptr;\n for(ch = 0; ch < 2; ch++){\n samples_ptr = samples + ch;\n for(i = 0; i < SAMPLES_PER_BAND; i++) {\n ff_mpa_synth_filter(c->synth_buf[ch], &(c->synth_buf_offset[ch]),\n mpa_window, &dither_state,\n samples_ptr, 2,\n c->sb_samples[ch][i]);\n samples_ptr += 64;\n }\n }\n for(i = 0; i < MPC_FRAME_SIZE*2; i++)\n *out++=samples[i];\n}', 'void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,\n MPA_INT *window, int *dither_state,\n OUT_INT *samples, int incr,\n int32_t sb_samples[SBLIMIT])\n{\n register MPA_INT *synth_buf;\n register const MPA_INT *w, *w2, *p;\n int j, offset;\n OUT_INT *samples2;\n#if FRAC_BITS <= 15\n int32_t tmp[32];\n int sum, sum2;\n#else\n int64_t sum, sum2;\n#endif\n offset = *synth_buf_offset;\n synth_buf = synth_buf_ptr + offset;\n#if FRAC_BITS <= 15\n dct32(tmp, sb_samples);\n for(j=0;j<32;j++) {\n synth_buf[j] = av_clip_int16(tmp[j]);\n }\n#else\n dct32(synth_buf, sb_samples);\n#endif\n memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));\n samples2 = samples + 31 * incr;\n w = window;\n w2 = window + 31;\n sum = *dither_state;\n p = synth_buf + 16;\n SUM8(MACS, sum, w, p);\n p = synth_buf + 48;\n SUM8(MLSS, sum, w + 32, p);\n *samples = round_sample(&sum);\n samples += incr;\n w++;\n for(j=1;j<16;j++) {\n sum2 = 0;\n p = synth_buf + 16 + j;\n SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);\n p = synth_buf + 48 - j;\n SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);\n *samples = round_sample(&sum);\n samples += incr;\n sum += sum2;\n *samples2 = round_sample(&sum);\n samples2 -= incr;\n w++;\n w2--;\n }\n p = synth_buf + 32;\n SUM8(MLSS, sum, w + 32, p);\n *samples = round_sample(&sum);\n *dither_state= sum;\n offset = (offset - 32) & 511;\n *synth_buf_offset = offset;\n}', 'static void dct32(int32_t *out, int32_t *tab)\n{\n int tmp0, tmp1;\n BF( 0, 31, COS0_0 , 1);\n BF(15, 16, COS0_15, 5);\n BF( 0, 15, COS1_0 , 1);\n BF(16, 31,-COS1_0 , 1);\n BF( 7, 24, COS0_7 , 1);\n BF( 8, 23, COS0_8 , 1);\n BF( 7, 8, COS1_7 , 4);\n BF(23, 24,-COS1_7 , 4);\n BF( 0, 7, COS2_0 , 1);\n BF( 8, 15,-COS2_0 , 1);\n BF(16, 23, COS2_0 , 1);\n BF(24, 31,-COS2_0 , 1);\n BF( 3, 28, COS0_3 , 1);\n BF(12, 19, COS0_12, 2);\n BF( 3, 12, COS1_3 , 1);\n BF(19, 28,-COS1_3 , 1);\n BF( 4, 27, COS0_4 , 1);\n BF(11, 20, COS0_11, 2);\n BF( 4, 11, COS1_4 , 1);\n BF(20, 27,-COS1_4 , 1);\n BF( 3, 4, COS2_3 , 3);\n BF(11, 12,-COS2_3 , 3);\n BF(19, 20, COS2_3 , 3);\n BF(27, 28,-COS2_3 , 3);\n BF( 0, 3, COS3_0 , 1);\n BF( 4, 7,-COS3_0 , 1);\n BF( 8, 11, COS3_0 , 1);\n BF(12, 15,-COS3_0 , 1);\n BF(16, 19, COS3_0 , 1);\n BF(20, 23,-COS3_0 , 1);\n BF(24, 27, COS3_0 , 1);\n BF(28, 31,-COS3_0 , 1);\n BF( 1, 30, COS0_1 , 1);\n BF(14, 17, COS0_14, 3);\n BF( 1, 14, COS1_1 , 1);\n BF(17, 30,-COS1_1 , 1);\n BF( 6, 25, COS0_6 , 1);\n BF( 9, 22, COS0_9 , 1);\n BF( 6, 9, COS1_6 , 2);\n BF(22, 25,-COS1_6 , 2);\n BF( 1, 6, COS2_1 , 1);\n BF( 9, 14,-COS2_1 , 1);\n BF(17, 22, COS2_1 , 1);\n BF(25, 30,-COS2_1 , 1);\n BF( 2, 29, COS0_2 , 1);\n BF(13, 18, COS0_13, 3);\n BF( 2, 13, COS1_2 , 1);\n BF(18, 29,-COS1_2 , 1);\n BF( 5, 26, COS0_5 , 1);\n BF(10, 21, COS0_10, 1);\n BF( 5, 10, COS1_5 , 2);\n BF(21, 26,-COS1_5 , 2);\n BF( 2, 5, COS2_2 , 1);\n BF(10, 13,-COS2_2 , 1);\n BF(18, 21, COS2_2 , 1);\n BF(26, 29,-COS2_2 , 1);\n BF( 1, 2, COS3_1 , 2);\n BF( 5, 6,-COS3_1 , 2);\n BF( 9, 10, COS3_1 , 2);\n BF(13, 14,-COS3_1 , 2);\n BF(17, 18, COS3_1 , 2);\n BF(21, 22,-COS3_1 , 2);\n BF(25, 26, COS3_1 , 2);\n BF(29, 30,-COS3_1 , 2);\n BF1( 0, 1, 2, 3);\n BF2( 4, 5, 6, 7);\n BF1( 8, 9, 10, 11);\n BF2(12, 13, 14, 15);\n BF1(16, 17, 18, 19);\n BF2(20, 21, 22, 23);\n BF1(24, 25, 26, 27);\n BF2(28, 29, 30, 31);\n ADD( 8, 12);\n ADD(12, 10);\n ADD(10, 14);\n ADD(14, 9);\n ADD( 9, 13);\n ADD(13, 11);\n ADD(11, 15);\n out[ 0] = tab[0];\n out[16] = tab[1];\n out[ 8] = tab[2];\n out[24] = tab[3];\n out[ 4] = tab[4];\n out[20] = tab[5];\n out[12] = tab[6];\n out[28] = tab[7];\n out[ 2] = tab[8];\n out[18] = tab[9];\n out[10] = tab[10];\n out[26] = tab[11];\n out[ 6] = tab[12];\n out[22] = tab[13];\n out[14] = tab[14];\n out[30] = tab[15];\n ADD(24, 28);\n ADD(28, 26);\n ADD(26, 30);\n ADD(30, 25);\n ADD(25, 29);\n ADD(29, 27);\n ADD(27, 31);\n out[ 1] = tab[16] + tab[24];\n out[17] = tab[17] + tab[25];\n out[ 9] = tab[18] + tab[26];\n out[25] = tab[19] + tab[27];\n out[ 5] = tab[20] + tab[28];\n out[21] = tab[21] + tab[29];\n out[13] = tab[22] + tab[30];\n out[29] = tab[23] + tab[31];\n out[ 3] = tab[24] + tab[20];\n out[19] = tab[25] + tab[21];\n out[11] = tab[26] + tab[22];\n out[27] = tab[27] + tab[23];\n out[ 7] = tab[28] + tab[18];\n out[23] = tab[29] + tab[19];\n out[15] = tab[30] + tab[17];\n out[31] = tab[31];\n}'] |
517 | 0 | https://github.com/openssl/openssl/blob/d4b009d5f88875ac0e3ac0b2b9689ed16a4c88dc/crypto/lhash/lhash.c/#L249 | static void doall_util_fn(_LHASH *lh, int use_arg, LHASH_DOALL_FN_TYPE func,
LHASH_DOALL_ARG_FN_TYPE func_arg, void *arg)
{
int i;
LHASH_NODE *a, *n;
if (lh == NULL)
return;
for (i = lh->num_nodes - 1; i >= 0; i--) {
a = lh->b[i];
while (a != NULL) {
n = a->next;
if (use_arg)
func_arg(a->data, arg);
else
func(a->data);
a = n;
}
}
} | ['int ciphers_main(int argc, char **argv)\n{\n SSL_CTX *ctx = NULL;\n SSL *ssl = NULL;\n STACK_OF(SSL_CIPHER) *sk = NULL;\n const SSL_METHOD *meth = TLS_server_method();\n int ret = 1, i, verbose = 0, Verbose = 0, use_supported = 0;\n#ifndef OPENSSL_NO_SSL_TRACE\n int stdname = 0;\n#endif\n#ifndef OPENSSL_NO_PSK\n int psk = 0;\n#endif\n const char *p;\n char *ciphers = NULL, *prog;\n char buf[512];\n OPTION_CHOICE o;\n prog = opt_init(argc, argv, ciphers_options);\n while ((o = opt_next()) != OPT_EOF) {\n switch (o) {\n case OPT_EOF:\n case OPT_ERR:\n opthelp:\n BIO_printf(bio_err, "%s: Use -help for summary.\\n", prog);\n goto end;\n case OPT_HELP:\n opt_help(ciphers_options);\n ret = 0;\n goto end;\n case OPT_V:\n verbose = 1;\n break;\n case OPT_UPPER_V:\n verbose = Verbose = 1;\n break;\n case OPT_S:\n use_supported = 1;\n break;\n case OPT_STDNAME:\n#ifndef OPENSSL_NO_SSL_TRACE\n stdname = verbose = 1;\n#endif\n break;\n case OPT_SSL3:\n#ifndef OPENSSL_NO_SSL3\n meth = SSLv3_client_method();\n#endif\n break;\n case OPT_TLS1:\n meth = TLSv1_client_method();\n break;\n case OPT_TLS1_1:\n meth = TLSv1_1_client_method();\n break;\n case OPT_TLS1_2:\n meth = TLSv1_2_client_method();\n break;\n case OPT_PSK:\n#ifndef OPENSSL_NO_PSK\n psk = 1;\n#endif\n break;\n }\n }\n argv = opt_rest();\n argc = opt_num_rest();\n if (argc == 1)\n ciphers = *argv;\n else if (argc != 0)\n goto opthelp;\n ctx = SSL_CTX_new(meth);\n if (ctx == NULL)\n goto err;\n#ifndef OPENSSL_NO_PSK\n if (psk)\n SSL_CTX_set_psk_client_callback(ctx, dummy_psk);\n#endif\n if (ciphers != NULL) {\n if (!SSL_CTX_set_cipher_list(ctx, ciphers)) {\n BIO_printf(bio_err, "Error in cipher list\\n");\n goto err;\n }\n }\n ssl = SSL_new(ctx);\n if (ssl == NULL)\n goto err;\n if (use_supported)\n sk = SSL_get1_supported_ciphers(ssl);\n else\n sk = SSL_get_ciphers(ssl);\n if (!verbose) {\n for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {\n const SSL_CIPHER *c = sk_SSL_CIPHER_value(sk, i);\n p = SSL_CIPHER_get_name(c);\n if (p == NULL)\n break;\n if (i != 0)\n BIO_printf(bio_out, ":");\n BIO_printf(bio_out, "%s", p);\n }\n BIO_printf(bio_out, "\\n");\n } else {\n for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {\n const SSL_CIPHER *c;\n c = sk_SSL_CIPHER_value(sk, i);\n if (Verbose) {\n unsigned long id = SSL_CIPHER_get_id(c);\n int id0 = (int)(id >> 24);\n int id1 = (int)((id >> 16) & 0xffL);\n int id2 = (int)((id >> 8) & 0xffL);\n int id3 = (int)(id & 0xffL);\n if ((id & 0xff000000L) == 0x03000000L)\n BIO_printf(bio_out, " 0x%02X,0x%02X - ", id2, id3);\n else\n BIO_printf(bio_out, "0x%02X,0x%02X,0x%02X,0x%02X - ", id0, id1, id2, id3);\n }\n#ifndef OPENSSL_NO_SSL_TRACE\n if (stdname) {\n const char *nm = SSL_CIPHER_standard_name(c);\n if (nm == NULL)\n nm = "UNKNOWN";\n BIO_printf(bio_out, "%s - ", nm);\n }\n#endif\n BIO_puts(bio_out, SSL_CIPHER_description(c, buf, sizeof buf));\n }\n }\n ret = 0;\n goto end;\n err:\n ERR_print_errors(bio_err);\n end:\n if (use_supported)\n sk_SSL_CIPHER_free(sk);\n SSL_CTX_free(ctx);\n SSL_free(ssl);\n return (ret);\n}', 'SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)\n{\n SSL_CTX *ret = NULL;\n if (meth == NULL) {\n SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_NULL_SSL_METHOD_PASSED);\n return (NULL);\n }\n if (FIPS_mode() && (meth->version < TLS1_VERSION)) {\n SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_AT_LEAST_TLS_1_0_NEEDED_IN_FIPS_MODE);\n return NULL;\n }\n if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {\n SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);\n goto err;\n }\n ret = OPENSSL_zalloc(sizeof(*ret));\n if (ret == NULL)\n goto err;\n ret->method = meth;\n ret->min_proto_version = 0;\n ret->max_proto_version = 0;\n ret->session_cache_mode = SSL_SESS_CACHE_SERVER;\n ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;\n ret->session_timeout = meth->get_timeout();\n ret->references = 1;\n ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;\n ret->verify_mode = SSL_VERIFY_NONE;\n if ((ret->cert = ssl_cert_new()) == NULL)\n goto err;\n ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);\n if (ret->sessions == NULL)\n goto err;\n ret->cert_store = X509_STORE_new();\n if (ret->cert_store == NULL)\n goto err;\n if (!ssl_create_cipher_list(ret->method,\n &ret->cipher_list, &ret->cipher_list_by_id,\n SSL_DEFAULT_CIPHER_LIST, ret->cert)\n || sk_SSL_CIPHER_num(ret->cipher_list) <= 0) {\n SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_LIBRARY_HAS_NO_CIPHERS);\n goto err2;\n }\n ret->param = X509_VERIFY_PARAM_new();\n if (ret->param == NULL)\n goto err;\n if ((ret->md5 = EVP_get_digestbyname("ssl3-md5")) == NULL) {\n SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES);\n goto err2;\n }\n if ((ret->sha1 = EVP_get_digestbyname("ssl3-sha1")) == NULL) {\n SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES);\n goto err2;\n }\n if ((ret->client_CA = sk_X509_NAME_new_null()) == NULL)\n goto err;\n CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);\n if (!(meth->ssl3_enc->enc_flags & SSL_ENC_FLAG_DTLS))\n ret->comp_methods = SSL_COMP_get_compression_methods();\n ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;\n if ((RAND_bytes(ret->tlsext_tick_key_name, 16) <= 0)\n || (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0)\n || (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0))\n ret->options |= SSL_OP_NO_TICKET;\n#ifndef OPENSSL_NO_SRP\n if (!SSL_CTX_SRP_CTX_init(ret))\n goto err;\n#endif\n#ifndef OPENSSL_NO_ENGINE\n# ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO\n# define eng_strx(x) #x\n# define eng_str(x) eng_strx(x)\n {\n ENGINE *eng;\n eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));\n if (!eng) {\n ERR_clear_error();\n ENGINE_load_builtin_engines();\n eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));\n }\n if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng))\n ERR_clear_error();\n }\n# endif\n#endif\n ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;\n return (ret);\n err:\n SSLerr(SSL_F_SSL_CTX_NEW, ERR_R_MALLOC_FAILURE);\n err2:\n SSL_CTX_free(ret);\n return (NULL);\n}', 'DEFINE_LHASH_OF(SSL_SESSION)', '_LHASH *lh_new(LHASH_HASH_FN_TYPE h, LHASH_COMP_FN_TYPE c)\n{\n _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) ? (LHASH_COMP_FN_TYPE)strcmp : c);\n ret->hash = ((h == NULL) ? (LHASH_HASH_FN_TYPE)lh_strhash : h);\n ret->num_nodes = MIN_NODES / 2;\n ret->num_alloc_nodes = MIN_NODES;\n ret->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 SSL_CTX_free(SSL_CTX *a)\n{\n int i;\n if (a == NULL)\n return;\n i = CRYPTO_add(&a->references, -1, CRYPTO_LOCK_SSL_CTX);\n#ifdef REF_PRINT\n REF_PRINT("SSL_CTX", a);\n#endif\n if (i > 0)\n return;\n#ifdef REF_CHECK\n if (i < 0) {\n fprintf(stderr, "SSL_CTX_free, bad reference count\\n");\n abort();\n }\n#endif\n 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 sk_SSL_CIPHER_free(a->cipher_list);\n sk_SSL_CIPHER_free(a->cipher_list_by_id);\n ssl_cert_free(a->cert);\n sk_X509_NAME_pop_free(a->client_CA, X509_NAME_free);\n sk_X509_pop_free(a->extra_certs, X509_free);\n a->comp_methods = NULL;\n#ifndef OPENSSL_NO_SRTP\n sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);\n#endif\n#ifndef OPENSSL_NO_SRP\n SSL_CTX_SRP_CTX_free(a);\n#endif\n#ifndef OPENSSL_NO_ENGINE\n if (a->client_cert_engine)\n ENGINE_finish(a->client_cert_engine);\n#endif\n#ifndef OPENSSL_NO_EC\n OPENSSL_free(a->tlsext_ecpointformatlist);\n OPENSSL_free(a->tlsext_ellipticcurvelist);\n#endif\n OPENSSL_free(a->alpn_client_proto_list);\n OPENSSL_free(a);\n}', 'void SSL_CTX_flush_sessions(SSL_CTX *s, long t)\n{\n unsigned long i;\n TIMEOUT_PARAM tp;\n tp.ctx = s;\n tp.cache = s->sessions;\n if (tp.cache == NULL)\n return;\n tp.time = t;\n CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);\n i = CHECKED_LHASH_OF(SSL_SESSION, tp.cache)->down_load;\n CHECKED_LHASH_OF(SSL_SESSION, tp.cache)->down_load = 0;\n lh_SSL_SESSION_doall_TIMEOUT_PARAM(tp.cache, timeout_cb, &tp);\n CHECKED_LHASH_OF(SSL_SESSION, tp.cache)->down_load = i;\n CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n}', 'IMPLEMENT_LHASH_DOALL_ARG(SSL_SESSION, TIMEOUT_PARAM)', 'void lh_doall_arg(_LHASH *lh, LHASH_DOALL_ARG_FN_TYPE func, void *arg)\n{\n doall_util_fn(lh, 1, (LHASH_DOALL_FN_TYPE)0, func, arg);\n}', 'static void doall_util_fn(_LHASH *lh, int use_arg, LHASH_DOALL_FN_TYPE func,\n LHASH_DOALL_ARG_FN_TYPE func_arg, void *arg)\n{\n int i;\n LHASH_NODE *a, *n;\n if (lh == NULL)\n return;\n for (i = lh->num_nodes - 1; i >= 0; i--) {\n a = lh->b[i];\n while (a != NULL) {\n n = a->next;\n if (use_arg)\n func_arg(a->data, arg);\n else\n func(a->data);\n a = n;\n }\n }\n}'] |
518 | 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)];
} | ['static int field_tests_default(int n)\n{\n BN_CTX *ctx = NULL;\n EC_GROUP *group = NULL;\n int nid = curves[n].nid;\n int ret = 0;\n TEST_info("Testing curve %s\\n", OBJ_nid2sn(nid));\n if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))\n || !TEST_ptr(ctx = BN_CTX_new())\n || !group_field_tests(group, ctx))\n goto err;\n ret = 1;\n err:\n if (group != NULL)\n EC_GROUP_free(group);\n if (ctx != NULL)\n BN_CTX_free(ctx);\n return ret;\n}', 'static int group_field_tests(const EC_GROUP *group, BN_CTX *ctx)\n{\n BIGNUM *a = NULL, *b = NULL, *c = NULL;\n int ret = 0;\n if (group->meth->field_inv == NULL || group->meth->field_mul == NULL)\n return 1;\n BN_CTX_start(ctx);\n a = BN_CTX_get(ctx);\n b = BN_CTX_get(ctx);\n if (!TEST_ptr(c = BN_CTX_get(ctx))\n || !TEST_true(group->meth->field_inv(group, b, BN_value_one(), ctx))\n || !TEST_true(BN_is_one(b))\n || !TEST_true(BN_pseudo_rand(a, BN_num_bits(group->field) - 1,\n BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))\n || !TEST_true(group->meth->field_inv(group, b, a, ctx))\n || (group->meth->field_encode &&\n !TEST_true(group->meth->field_encode(group, a, a, ctx)))\n || (group->meth->field_encode &&\n !TEST_true(group->meth->field_encode(group, b, b, ctx)))\n || !TEST_true(group->meth->field_mul(group, c, a, b, ctx))\n || (group->meth->field_decode &&\n !TEST_true(group->meth->field_decode(group, c, c, ctx)))\n || !TEST_true(BN_is_one(c)))\n goto err;\n BN_zero(a);\n if (!TEST_false(group->meth->field_inv(group, b, a, ctx))\n || !TEST_true(ERR_GET_LIB(ERR_peek_last_error()) == ERR_LIB_EC)\n || !TEST_true(ERR_GET_REASON(ERR_peek_last_error()) ==\n EC_R_CANNOT_INVERT)\n || !TEST_false(group->meth->field_inv(group, b, group->field, ctx))\n || !TEST_true(ERR_GET_LIB(ERR_peek_last_error()) == ERR_LIB_EC)\n || !TEST_true(ERR_GET_REASON(ERR_peek_last_error()) ==\n EC_R_CANNOT_INVERT))\n goto err;\n ERR_clear_error();\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_start()", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG("LEAVE BN_CTX_start()", ctx);\n}', '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}'] |
519 | 0 | https://github.com/libav/libav/blob/baf35bb4bc4fe7a2a4113c50989d11dd9ef81e76/libavcodec/motion_est.c/#L365 | int ff_init_me(MpegEncContext *s){
MotionEstContext * const c= &s->me;
int cache_size= FFMIN(ME_MAP_SIZE>>ME_MAP_SHIFT, 1<<ME_MAP_SHIFT);
int dia_size= FFMAX(FFABS(s->avctx->dia_size)&255, FFABS(s->avctx->pre_dia_size)&255);
if(FFMIN(s->avctx->dia_size, s->avctx->pre_dia_size) < -ME_MAP_SIZE){
av_log(s->avctx, AV_LOG_ERROR, "ME_MAP size is too small for SAB diamond\n");
return -1;
}
if (s->me_method != ME_ZERO &&
s->me_method != ME_EPZS &&
s->me_method != ME_X1) {
av_log(s->avctx, AV_LOG_ERROR, "me_method is only allowed to be set to zero and epzs; for hex,umh,full and others see dia_size\n");
return -1;
}
c->avctx= s->avctx;
if(cache_size < 2*dia_size && !c->stride){
av_log(s->avctx, AV_LOG_INFO, "ME_MAP size may be a little small for the selected diamond size\n");
}
ff_set_cmp(&s->dsp, s->dsp.me_pre_cmp, c->avctx->me_pre_cmp);
ff_set_cmp(&s->dsp, s->dsp.me_cmp, c->avctx->me_cmp);
ff_set_cmp(&s->dsp, s->dsp.me_sub_cmp, c->avctx->me_sub_cmp);
ff_set_cmp(&s->dsp, s->dsp.mb_cmp, c->avctx->mb_cmp);
c->flags = get_flags(c, 0, c->avctx->me_cmp &FF_CMP_CHROMA);
c->sub_flags= get_flags(c, 0, c->avctx->me_sub_cmp&FF_CMP_CHROMA);
c->mb_flags = get_flags(c, 0, c->avctx->mb_cmp &FF_CMP_CHROMA);
if(s->flags&CODEC_FLAG_QPEL){
c->sub_motion_search= qpel_motion_search;
c->qpel_avg= s->dsp.avg_qpel_pixels_tab;
if(s->no_rounding) c->qpel_put= s->dsp.put_no_rnd_qpel_pixels_tab;
else c->qpel_put= s->dsp.put_qpel_pixels_tab;
}else{
if(c->avctx->me_sub_cmp&FF_CMP_CHROMA)
c->sub_motion_search= hpel_motion_search;
else if( c->avctx->me_sub_cmp == FF_CMP_SAD
&& c->avctx-> me_cmp == FF_CMP_SAD
&& c->avctx-> mb_cmp == FF_CMP_SAD)
c->sub_motion_search= sad_hpel_motion_search;
else
c->sub_motion_search= hpel_motion_search;
}
c->hpel_avg= s->dsp.avg_pixels_tab;
if(s->no_rounding) c->hpel_put= s->dsp.put_no_rnd_pixels_tab;
else c->hpel_put= s->dsp.put_pixels_tab;
if(s->linesize){
c->stride = s->linesize;
c->uvstride= s->uvlinesize;
}else{
c->stride = 16*s->mb_width + 32;
c->uvstride= 8*s->mb_width + 16;
}
if((c->avctx->me_cmp&FF_CMP_CHROMA) ){
s->dsp.me_cmp[2]= zero_cmp;
}
if((c->avctx->me_sub_cmp&FF_CMP_CHROMA) && !s->dsp.me_sub_cmp[2]){
s->dsp.me_sub_cmp[2]= zero_cmp;
}
c->hpel_put[2][0]= c->hpel_put[2][1]=
c->hpel_put[2][2]= c->hpel_put[2][3]= zero_hpel;
if(s->codec_id == AV_CODEC_ID_H261){
c->sub_motion_search= no_sub_motion_search;
}
return 0;
} | ['int ff_init_me(MpegEncContext *s){\n MotionEstContext * const c= &s->me;\n int cache_size= FFMIN(ME_MAP_SIZE>>ME_MAP_SHIFT, 1<<ME_MAP_SHIFT);\n int dia_size= FFMAX(FFABS(s->avctx->dia_size)&255, FFABS(s->avctx->pre_dia_size)&255);\n if(FFMIN(s->avctx->dia_size, s->avctx->pre_dia_size) < -ME_MAP_SIZE){\n av_log(s->avctx, AV_LOG_ERROR, "ME_MAP size is too small for SAB diamond\\n");\n return -1;\n }\n if (s->me_method != ME_ZERO &&\n s->me_method != ME_EPZS &&\n s->me_method != ME_X1) {\n av_log(s->avctx, AV_LOG_ERROR, "me_method is only allowed to be set to zero and epzs; for hex,umh,full and others see dia_size\\n");\n return -1;\n }\n c->avctx= s->avctx;\n if(cache_size < 2*dia_size && !c->stride){\n av_log(s->avctx, AV_LOG_INFO, "ME_MAP size may be a little small for the selected diamond size\\n");\n }\n ff_set_cmp(&s->dsp, s->dsp.me_pre_cmp, c->avctx->me_pre_cmp);\n ff_set_cmp(&s->dsp, s->dsp.me_cmp, c->avctx->me_cmp);\n ff_set_cmp(&s->dsp, s->dsp.me_sub_cmp, c->avctx->me_sub_cmp);\n ff_set_cmp(&s->dsp, s->dsp.mb_cmp, c->avctx->mb_cmp);\n c->flags = get_flags(c, 0, c->avctx->me_cmp &FF_CMP_CHROMA);\n c->sub_flags= get_flags(c, 0, c->avctx->me_sub_cmp&FF_CMP_CHROMA);\n c->mb_flags = get_flags(c, 0, c->avctx->mb_cmp &FF_CMP_CHROMA);\n if(s->flags&CODEC_FLAG_QPEL){\n c->sub_motion_search= qpel_motion_search;\n c->qpel_avg= s->dsp.avg_qpel_pixels_tab;\n if(s->no_rounding) c->qpel_put= s->dsp.put_no_rnd_qpel_pixels_tab;\n else c->qpel_put= s->dsp.put_qpel_pixels_tab;\n }else{\n if(c->avctx->me_sub_cmp&FF_CMP_CHROMA)\n c->sub_motion_search= hpel_motion_search;\n else if( c->avctx->me_sub_cmp == FF_CMP_SAD\n && c->avctx-> me_cmp == FF_CMP_SAD\n && c->avctx-> mb_cmp == FF_CMP_SAD)\n c->sub_motion_search= sad_hpel_motion_search;\n else\n c->sub_motion_search= hpel_motion_search;\n }\n c->hpel_avg= s->dsp.avg_pixels_tab;\n if(s->no_rounding) c->hpel_put= s->dsp.put_no_rnd_pixels_tab;\n else c->hpel_put= s->dsp.put_pixels_tab;\n if(s->linesize){\n c->stride = s->linesize;\n c->uvstride= s->uvlinesize;\n }else{\n c->stride = 16*s->mb_width + 32;\n c->uvstride= 8*s->mb_width + 16;\n }\n if((c->avctx->me_cmp&FF_CMP_CHROMA) ){\n s->dsp.me_cmp[2]= zero_cmp;\n }\n if((c->avctx->me_sub_cmp&FF_CMP_CHROMA) && !s->dsp.me_sub_cmp[2]){\n s->dsp.me_sub_cmp[2]= zero_cmp;\n }\n c->hpel_put[2][0]= c->hpel_put[2][1]=\n c->hpel_put[2][2]= c->hpel_put[2][3]= zero_hpel;\n if(s->codec_id == AV_CODEC_ID_H261){\n c->sub_motion_search= no_sub_motion_search;\n }\n return 0;\n}'] |
520 | 0 | https://github.com/libav/libav/blob/cf53c48615658a6019ffb7e8453913bbfd38cb27/libavcodec/h264.h/#L977 | static void fill_decode_caches(H264Context *h, int mb_type){
MpegEncContext * const s = &h->s;
int topleft_xy, top_xy, topright_xy, left_xy[2];
int topleft_type, top_type, topright_type, left_type[2];
const uint8_t * left_block= h->left_block;
int i;
topleft_xy = h->topleft_mb_xy ;
top_xy = h->top_mb_xy ;
topright_xy = h->topright_mb_xy;
left_xy[0] = h->left_mb_xy[0] ;
left_xy[1] = h->left_mb_xy[1] ;
topleft_type = h->topleft_type ;
top_type = h->top_type ;
topright_type= h->topright_type ;
left_type[0] = h->left_type[0] ;
left_type[1] = h->left_type[1] ;
if(!IS_SKIP(mb_type)){
if(IS_INTRA(mb_type)){
int type_mask= h->pps.constrained_intra_pred ? IS_INTRA(-1) : -1;
h->topleft_samples_available=
h->top_samples_available=
h->left_samples_available= 0xFFFF;
h->topright_samples_available= 0xEEEA;
if(!(top_type & type_mask)){
h->topleft_samples_available= 0xB3FF;
h->top_samples_available= 0x33FF;
h->topright_samples_available= 0x26EA;
}
if(IS_INTERLACED(mb_type) != IS_INTERLACED(left_type[0])){
if(IS_INTERLACED(mb_type)){
if(!(left_type[0] & type_mask)){
h->topleft_samples_available&= 0xDFFF;
h->left_samples_available&= 0x5FFF;
}
if(!(left_type[1] & type_mask)){
h->topleft_samples_available&= 0xFF5F;
h->left_samples_available&= 0xFF5F;
}
}else{
int left_typei = s->current_picture.mb_type[left_xy[0] + s->mb_stride];
assert(left_xy[0] == left_xy[1]);
if(!((left_typei & type_mask) && (left_type[0] & type_mask))){
h->topleft_samples_available&= 0xDF5F;
h->left_samples_available&= 0x5F5F;
}
}
}else{
if(!(left_type[0] & type_mask)){
h->topleft_samples_available&= 0xDF5F;
h->left_samples_available&= 0x5F5F;
}
}
if(!(topleft_type & type_mask))
h->topleft_samples_available&= 0x7FFF;
if(!(topright_type & type_mask))
h->topright_samples_available&= 0xFBFF;
if(IS_INTRA4x4(mb_type)){
if(IS_INTRA4x4(top_type)){
AV_COPY32(h->intra4x4_pred_mode_cache+4+8*0, h->intra4x4_pred_mode + h->mb2br_xy[top_xy]);
}else{
h->intra4x4_pred_mode_cache[4+8*0]=
h->intra4x4_pred_mode_cache[5+8*0]=
h->intra4x4_pred_mode_cache[6+8*0]=
h->intra4x4_pred_mode_cache[7+8*0]= 2 - 3*!(top_type & type_mask);
}
for(i=0; i<2; i++){
if(IS_INTRA4x4(left_type[i])){
int8_t *mode= h->intra4x4_pred_mode + h->mb2br_xy[left_xy[i]];
h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= mode[6-left_block[0+2*i]];
h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= mode[6-left_block[1+2*i]];
}else{
h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=
h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= 2 - 3*!(left_type[i] & type_mask);
}
}
}
}
if(top_type){
AV_COPY32(&h->non_zero_count_cache[4+8* 0], &h->non_zero_count[top_xy][4*3]);
if(CHROMA444){
AV_COPY32(&h->non_zero_count_cache[4+8* 5], &h->non_zero_count[top_xy][4* 7]);
AV_COPY32(&h->non_zero_count_cache[4+8*10], &h->non_zero_count[top_xy][4*11]);
}else{
AV_COPY32(&h->non_zero_count_cache[4+8* 5], &h->non_zero_count[top_xy][4* 5]);
AV_COPY32(&h->non_zero_count_cache[4+8*10], &h->non_zero_count[top_xy][4* 9]);
}
}else{
uint32_t top_empty = CABAC && !IS_INTRA(mb_type) ? 0 : 0x40404040;
AV_WN32A(&h->non_zero_count_cache[4+8* 0], top_empty);
AV_WN32A(&h->non_zero_count_cache[4+8* 5], top_empty);
AV_WN32A(&h->non_zero_count_cache[4+8*10], top_empty);
}
for (i=0; i<2; i++) {
if(left_type[i]){
h->non_zero_count_cache[3+8* 1 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[8+0+2*i]];
h->non_zero_count_cache[3+8* 2 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[8+1+2*i]];
if(CHROMA444){
h->non_zero_count_cache[3+8* 6 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[8+0+2*i]+4*4];
h->non_zero_count_cache[3+8* 7 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[8+1+2*i]+4*4];
h->non_zero_count_cache[3+8*11 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[8+0+2*i]+8*4];
h->non_zero_count_cache[3+8*12 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[8+1+2*i]+8*4];
}else{
h->non_zero_count_cache[3+8* 6 + 8*i]= h->non_zero_count[left_xy[i]][left_block[8+4+2*i]];
h->non_zero_count_cache[3+8*11 + 8*i]= h->non_zero_count[left_xy[i]][left_block[8+5+2*i]];
}
}else{
h->non_zero_count_cache[3+8* 1 + 2*8*i]=
h->non_zero_count_cache[3+8* 2 + 2*8*i]=
h->non_zero_count_cache[3+8* 6 + 2*8*i]=
h->non_zero_count_cache[3+8* 7 + 2*8*i]=
h->non_zero_count_cache[3+8*11 + 2*8*i]=
h->non_zero_count_cache[3+8*12 + 2*8*i]= CABAC && !IS_INTRA(mb_type) ? 0 : 64;
}
}
if( CABAC ) {
if(top_type) {
h->top_cbp = h->cbp_table[top_xy];
} else {
h->top_cbp = IS_INTRA(mb_type) ? 0x7CF : 0x00F;
}
if (left_type[0]) {
h->left_cbp = (h->cbp_table[left_xy[0]] & 0x7F0)
| ((h->cbp_table[left_xy[0]]>>(left_block[0]&(~1)))&2)
| (((h->cbp_table[left_xy[1]]>>(left_block[2]&(~1)))&2) << 2);
} else {
h->left_cbp = IS_INTRA(mb_type) ? 0x7CF : 0x00F;
}
}
}
if(IS_INTER(mb_type) || (IS_DIRECT(mb_type) && h->direct_spatial_mv_pred)){
int list;
for(list=0; list<h->list_count; list++){
if(!USES_LIST(mb_type, list)){
continue;
}
assert(!(IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred));
h->mv_cache_clean[list]= 0;
if(USES_LIST(top_type, list)){
const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
AV_COPY128(h->mv_cache[list][scan8[0] + 0 - 1*8], s->current_picture.motion_val[list][b_xy + 0]);
h->ref_cache[list][scan8[0] + 0 - 1*8]=
h->ref_cache[list][scan8[0] + 1 - 1*8]= s->current_picture.ref_index[list][4*top_xy + 2];
h->ref_cache[list][scan8[0] + 2 - 1*8]=
h->ref_cache[list][scan8[0] + 3 - 1*8]= s->current_picture.ref_index[list][4*top_xy + 3];
}else{
AV_ZERO128(h->mv_cache[list][scan8[0] + 0 - 1*8]);
AV_WN32A(&h->ref_cache[list][scan8[0] + 0 - 1*8], ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101);
}
if(mb_type & (MB_TYPE_16x8|MB_TYPE_8x8)){
for(i=0; i<2; i++){
int cache_idx = scan8[0] - 1 + i*2*8;
if(USES_LIST(left_type[i], list)){
const int b_xy= h->mb2b_xy[left_xy[i]] + 3;
const int b8_xy= 4*left_xy[i] + 1;
AV_COPY32(h->mv_cache[list][cache_idx ], s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[0+i*2]]);
AV_COPY32(h->mv_cache[list][cache_idx+8], s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[1+i*2]]);
h->ref_cache[list][cache_idx ]= s->current_picture.ref_index[list][b8_xy + (left_block[0+i*2]&~1)];
h->ref_cache[list][cache_idx+8]= s->current_picture.ref_index[list][b8_xy + (left_block[1+i*2]&~1)];
}else{
AV_ZERO32(h->mv_cache [list][cache_idx ]);
AV_ZERO32(h->mv_cache [list][cache_idx+8]);
h->ref_cache[list][cache_idx ]=
h->ref_cache[list][cache_idx+8]= (left_type[i]) ? LIST_NOT_USED : PART_NOT_AVAILABLE;
}
}
}else{
if(USES_LIST(left_type[0], list)){
const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
const int b8_xy= 4*left_xy[0] + 1;
AV_COPY32(h->mv_cache[list][scan8[0] - 1], s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[0]]);
h->ref_cache[list][scan8[0] - 1]= s->current_picture.ref_index[list][b8_xy + (left_block[0]&~1)];
}else{
AV_ZERO32(h->mv_cache [list][scan8[0] - 1]);
h->ref_cache[list][scan8[0] - 1]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
}
}
if(USES_LIST(topright_type, list)){
const int b_xy= h->mb2b_xy[topright_xy] + 3*h->b_stride;
AV_COPY32(h->mv_cache[list][scan8[0] + 4 - 1*8], s->current_picture.motion_val[list][b_xy]);
h->ref_cache[list][scan8[0] + 4 - 1*8]= s->current_picture.ref_index[list][4*topright_xy + 2];
}else{
AV_ZERO32(h->mv_cache [list][scan8[0] + 4 - 1*8]);
h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
}
if(h->ref_cache[list][scan8[0] + 4 - 1*8] < 0){
if(USES_LIST(topleft_type, list)){
const int b_xy = h->mb2b_xy [topleft_xy] + 3 + h->b_stride + (h->topleft_partition & 2*h->b_stride);
const int b8_xy= 4*topleft_xy + 1 + (h->topleft_partition & 2);
AV_COPY32(h->mv_cache[list][scan8[0] - 1 - 1*8], s->current_picture.motion_val[list][b_xy]);
h->ref_cache[list][scan8[0] - 1 - 1*8]= s->current_picture.ref_index[list][b8_xy];
}else{
AV_ZERO32(h->mv_cache[list][scan8[0] - 1 - 1*8]);
h->ref_cache[list][scan8[0] - 1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
}
}
if((mb_type&(MB_TYPE_SKIP|MB_TYPE_DIRECT2)) && !FRAME_MBAFF)
continue;
if(!(mb_type&(MB_TYPE_SKIP|MB_TYPE_DIRECT2))) {
h->ref_cache[list][scan8[4 ]] =
h->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE;
AV_ZERO32(h->mv_cache [list][scan8[4 ]]);
AV_ZERO32(h->mv_cache [list][scan8[12]]);
if( CABAC ) {
if(USES_LIST(top_type, list)){
const int b_xy= h->mb2br_xy[top_xy];
AV_COPY64(h->mvd_cache[list][scan8[0] + 0 - 1*8], h->mvd_table[list][b_xy + 0]);
}else{
AV_ZERO64(h->mvd_cache[list][scan8[0] + 0 - 1*8]);
}
if(USES_LIST(left_type[0], list)){
const int b_xy= h->mb2br_xy[left_xy[0]] + 6;
AV_COPY16(h->mvd_cache[list][scan8[0] - 1 + 0*8], h->mvd_table[list][b_xy - left_block[0]]);
AV_COPY16(h->mvd_cache[list][scan8[0] - 1 + 1*8], h->mvd_table[list][b_xy - left_block[1]]);
}else{
AV_ZERO16(h->mvd_cache [list][scan8[0] - 1 + 0*8]);
AV_ZERO16(h->mvd_cache [list][scan8[0] - 1 + 1*8]);
}
if(USES_LIST(left_type[1], list)){
const int b_xy= h->mb2br_xy[left_xy[1]] + 6;
AV_COPY16(h->mvd_cache[list][scan8[0] - 1 + 2*8], h->mvd_table[list][b_xy - left_block[2]]);
AV_COPY16(h->mvd_cache[list][scan8[0] - 1 + 3*8], h->mvd_table[list][b_xy - left_block[3]]);
}else{
AV_ZERO16(h->mvd_cache [list][scan8[0] - 1 + 2*8]);
AV_ZERO16(h->mvd_cache [list][scan8[0] - 1 + 3*8]);
}
AV_ZERO16(h->mvd_cache [list][scan8[4 ]]);
AV_ZERO16(h->mvd_cache [list][scan8[12]]);
if(h->slice_type_nos == AV_PICTURE_TYPE_B){
fill_rectangle(&h->direct_cache[scan8[0]], 4, 4, 8, MB_TYPE_16x16>>1, 1);
if(IS_DIRECT(top_type)){
AV_WN32A(&h->direct_cache[scan8[0] - 1*8], 0x01010101u*(MB_TYPE_DIRECT2>>1));
}else if(IS_8X8(top_type)){
int b8_xy = 4*top_xy;
h->direct_cache[scan8[0] + 0 - 1*8]= h->direct_table[b8_xy + 2];
h->direct_cache[scan8[0] + 2 - 1*8]= h->direct_table[b8_xy + 3];
}else{
AV_WN32A(&h->direct_cache[scan8[0] - 1*8], 0x01010101*(MB_TYPE_16x16>>1));
}
if(IS_DIRECT(left_type[0]))
h->direct_cache[scan8[0] - 1 + 0*8]= MB_TYPE_DIRECT2>>1;
else if(IS_8X8(left_type[0]))
h->direct_cache[scan8[0] - 1 + 0*8]= h->direct_table[4*left_xy[0] + 1 + (left_block[0]&~1)];
else
h->direct_cache[scan8[0] - 1 + 0*8]= MB_TYPE_16x16>>1;
if(IS_DIRECT(left_type[1]))
h->direct_cache[scan8[0] - 1 + 2*8]= MB_TYPE_DIRECT2>>1;
else if(IS_8X8(left_type[1]))
h->direct_cache[scan8[0] - 1 + 2*8]= h->direct_table[4*left_xy[1] + 1 + (left_block[2]&~1)];
else
h->direct_cache[scan8[0] - 1 + 2*8]= MB_TYPE_16x16>>1;
}
}
}
if(FRAME_MBAFF){
#define MAP_MVS\
MAP_F2F(scan8[0] - 1 - 1*8, topleft_type)\
MAP_F2F(scan8[0] + 0 - 1*8, top_type)\
MAP_F2F(scan8[0] + 1 - 1*8, top_type)\
MAP_F2F(scan8[0] + 2 - 1*8, top_type)\
MAP_F2F(scan8[0] + 3 - 1*8, top_type)\
MAP_F2F(scan8[0] + 4 - 1*8, topright_type)\
MAP_F2F(scan8[0] - 1 + 0*8, left_type[0])\
MAP_F2F(scan8[0] - 1 + 1*8, left_type[0])\
MAP_F2F(scan8[0] - 1 + 2*8, left_type[1])\
MAP_F2F(scan8[0] - 1 + 3*8, left_type[1])
if(MB_FIELD){
#define MAP_F2F(idx, mb_type)\
if(!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
h->ref_cache[list][idx] <<= 1;\
h->mv_cache[list][idx][1] /= 2;\
h->mvd_cache[list][idx][1] >>=1;\
}
MAP_MVS
#undef MAP_F2F
}else{
#define MAP_F2F(idx, mb_type)\
if(IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
h->ref_cache[list][idx] >>= 1;\
h->mv_cache[list][idx][1] <<= 1;\
h->mvd_cache[list][idx][1] <<= 1;\
}
MAP_MVS
#undef MAP_F2F
}
}
}
}
h->neighbor_transform_size= !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[0]);
} | ['int ff_h264_decode_mb_cabac(H264Context *h) {\n MpegEncContext * const s = &h->s;\n int mb_xy;\n int mb_type, partition_count, cbp = 0;\n int dct8x8_allowed= h->pps.transform_8x8_mode;\n int decode_chroma = h->sps.chroma_format_idc == 1 || h->sps.chroma_format_idc == 2;\n const int pixel_shift = h->pixel_shift;\n mb_xy = h->mb_xy = s->mb_x + s->mb_y*s->mb_stride;\n tprintf(s->avctx, "pic:%d mb:%d/%d\\n", h->frame_num, s->mb_x, s->mb_y);\n if( h->slice_type_nos != AV_PICTURE_TYPE_I ) {\n int skip;\n if( FRAME_MBAFF && (s->mb_y&1)==1 && h->prev_mb_skipped )\n skip = h->next_mb_skipped;\n else\n skip = decode_cabac_mb_skip( h, s->mb_x, s->mb_y );\n if( skip ) {\n if( FRAME_MBAFF && (s->mb_y&1)==0 ){\n s->current_picture.mb_type[mb_xy] = MB_TYPE_SKIP;\n h->next_mb_skipped = decode_cabac_mb_skip( h, s->mb_x, s->mb_y+1 );\n if(!h->next_mb_skipped)\n h->mb_mbaff = h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);\n }\n decode_mb_skip(h);\n h->cbp_table[mb_xy] = 0;\n h->chroma_pred_mode_table[mb_xy] = 0;\n h->last_qscale_diff = 0;\n return 0;\n }\n }\n if(FRAME_MBAFF){\n if( (s->mb_y&1) == 0 )\n h->mb_mbaff =\n h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);\n }\n h->prev_mb_skipped = 0;\n fill_decode_neighbors(h, -(MB_FIELD));\n if( h->slice_type_nos == AV_PICTURE_TYPE_B ) {\n int ctx = 0;\n assert(h->slice_type_nos == AV_PICTURE_TYPE_B);\n if( !IS_DIRECT( h->left_type[0]-1 ) )\n ctx++;\n if( !IS_DIRECT( h->top_type-1 ) )\n ctx++;\n if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+ctx] ) ){\n mb_type= 0;\n }else if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+3] ) ) {\n mb_type= 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );\n }else{\n int bits;\n bits = get_cabac_noinline( &h->cabac, &h->cabac_state[27+4] ) << 3;\n bits+= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 2;\n bits+= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 1;\n bits+= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );\n if( bits < 8 ){\n mb_type= bits + 3;\n }else if( bits == 13 ){\n mb_type= decode_cabac_intra_mb_type(h, 32, 0);\n goto decode_intra_mb;\n }else if( bits == 14 ){\n mb_type= 11;\n }else if( bits == 15 ){\n mb_type= 22;\n }else{\n bits= ( bits<<1 ) + get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );\n mb_type= bits - 4;\n }\n }\n partition_count= b_mb_type_info[mb_type].partition_count;\n mb_type= b_mb_type_info[mb_type].type;\n } else if( h->slice_type_nos == AV_PICTURE_TYPE_P ) {\n if( get_cabac_noinline( &h->cabac, &h->cabac_state[14] ) == 0 ) {\n if( get_cabac_noinline( &h->cabac, &h->cabac_state[15] ) == 0 ) {\n mb_type= 3 * get_cabac_noinline( &h->cabac, &h->cabac_state[16] );\n } else {\n mb_type= 2 - get_cabac_noinline( &h->cabac, &h->cabac_state[17] );\n }\n partition_count= p_mb_type_info[mb_type].partition_count;\n mb_type= p_mb_type_info[mb_type].type;\n } else {\n mb_type= decode_cabac_intra_mb_type(h, 17, 0);\n goto decode_intra_mb;\n }\n } else {\n mb_type= decode_cabac_intra_mb_type(h, 3, 1);\n if(h->slice_type == AV_PICTURE_TYPE_SI && mb_type)\n mb_type--;\n assert(h->slice_type_nos == AV_PICTURE_TYPE_I);\ndecode_intra_mb:\n partition_count = 0;\n cbp= i_mb_type_info[mb_type].cbp;\n h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;\n mb_type= i_mb_type_info[mb_type].type;\n }\n if(MB_FIELD)\n mb_type |= MB_TYPE_INTERLACED;\n h->slice_table[ mb_xy ]= h->slice_num;\n if(IS_INTRA_PCM(mb_type)) {\n static const uint16_t mb_sizes[4] = {256,384,512,768};\n const int mb_size = mb_sizes[h->sps.chroma_format_idc]*h->sps.bit_depth_luma >> 3;\n const uint8_t *ptr;\n ptr= h->cabac.bytestream;\n if(h->cabac.low&0x1) ptr--;\n if(CABAC_BITS==16){\n if(h->cabac.low&0x1FF) ptr--;\n }\n memcpy(h->mb, ptr, mb_size); ptr+=mb_size;\n ff_init_cabac_decoder(&h->cabac, ptr, h->cabac.bytestream_end - ptr);\n h->cbp_table[mb_xy] = 0xf7ef;\n h->chroma_pred_mode_table[mb_xy] = 0;\n s->current_picture.qscale_table[mb_xy]= 0;\n memset(h->non_zero_count[mb_xy], 16, 48);\n s->current_picture.mb_type[mb_xy]= mb_type;\n h->last_qscale_diff = 0;\n return 0;\n }\n if(MB_MBAFF){\n h->ref_count[0] <<= 1;\n h->ref_count[1] <<= 1;\n }\n fill_decode_caches(h, mb_type);\n if( IS_INTRA( mb_type ) ) {\n int i, pred_mode;\n if( IS_INTRA4x4( mb_type ) ) {\n if( dct8x8_allowed && get_cabac_noinline( &h->cabac, &h->cabac_state[399 + h->neighbor_transform_size] ) ) {\n mb_type |= MB_TYPE_8x8DCT;\n for( i = 0; i < 16; i+=4 ) {\n int pred = pred_intra_mode( h, i );\n int mode = decode_cabac_mb_intra4x4_pred_mode( h, pred );\n fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );\n }\n } else {\n for( i = 0; i < 16; i++ ) {\n int pred = pred_intra_mode( h, i );\n h->intra4x4_pred_mode_cache[ scan8[i] ] = decode_cabac_mb_intra4x4_pred_mode( h, pred );\n }\n }\n ff_h264_write_back_intra_pred_mode(h);\n if( ff_h264_check_intra4x4_pred_mode(h) < 0 ) return -1;\n } else {\n h->intra16x16_pred_mode= ff_h264_check_intra_pred_mode( h, h->intra16x16_pred_mode );\n if( h->intra16x16_pred_mode < 0 ) return -1;\n }\n if(decode_chroma){\n h->chroma_pred_mode_table[mb_xy] =\n pred_mode = decode_cabac_mb_chroma_pre_mode( h );\n pred_mode= ff_h264_check_intra_pred_mode( h, pred_mode );\n if( pred_mode < 0 ) return -1;\n h->chroma_pred_mode= pred_mode;\n } else {\n h->chroma_pred_mode= DC_128_PRED8x8;\n }\n } else if( partition_count == 4 ) {\n int i, j, sub_partition_count[4], list, ref[2][4];\n if( h->slice_type_nos == AV_PICTURE_TYPE_B ) {\n for( i = 0; i < 4; i++ ) {\n h->sub_mb_type[i] = decode_cabac_b_mb_sub_type( h );\n sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;\n h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;\n }\n if( IS_DIRECT(h->sub_mb_type[0] | h->sub_mb_type[1] |\n h->sub_mb_type[2] | h->sub_mb_type[3]) ) {\n ff_h264_pred_direct_motion(h, &mb_type);\n h->ref_cache[0][scan8[4]] =\n h->ref_cache[1][scan8[4]] =\n h->ref_cache[0][scan8[12]] =\n h->ref_cache[1][scan8[12]] = PART_NOT_AVAILABLE;\n for( i = 0; i < 4; i++ )\n fill_rectangle( &h->direct_cache[scan8[4*i]], 2, 2, 8, (h->sub_mb_type[i]>>1)&0xFF, 1 );\n }\n } else {\n for( i = 0; i < 4; i++ ) {\n h->sub_mb_type[i] = decode_cabac_p_mb_sub_type( h );\n sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;\n h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;\n }\n }\n for( list = 0; list < h->list_count; list++ ) {\n for( i = 0; i < 4; i++ ) {\n if(IS_DIRECT(h->sub_mb_type[i])) continue;\n if(IS_DIR(h->sub_mb_type[i], 0, list)){\n if( h->ref_count[list] > 1 ){\n ref[list][i] = decode_cabac_mb_ref( h, list, 4*i );\n if(ref[list][i] >= (unsigned)h->ref_count[list]){\n av_log(s->avctx, AV_LOG_ERROR, "Reference %d >= %d\\n", ref[list][i], h->ref_count[list]);\n return -1;\n }\n }else\n ref[list][i] = 0;\n } else {\n ref[list][i] = -1;\n }\n h->ref_cache[list][ scan8[4*i]+1 ]=\n h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];\n }\n }\n if(dct8x8_allowed)\n dct8x8_allowed = get_dct8x8_allowed(h);\n for(list=0; list<h->list_count; list++){\n for(i=0; i<4; i++){\n h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ];\n if(IS_DIRECT(h->sub_mb_type[i])){\n fill_rectangle(h->mvd_cache[list][scan8[4*i]], 2, 2, 8, 0, 2);\n continue;\n }\n if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){\n const int sub_mb_type= h->sub_mb_type[i];\n const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;\n for(j=0; j<sub_partition_count[i]; j++){\n int mpx, mpy;\n int mx, my;\n const int index= 4*i + block_width*j;\n int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];\n uint8_t (* mvd_cache)[2]= &h->mvd_cache[list][ scan8[index] ];\n pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my);\n DECODE_CABAC_MB_MVD( h, list, index)\n tprintf(s->avctx, "final mv:%d %d\\n", mx, my);\n if(IS_SUB_8X8(sub_mb_type)){\n mv_cache[ 1 ][0]=\n mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;\n mv_cache[ 1 ][1]=\n mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;\n mvd_cache[ 1 ][0]=\n mvd_cache[ 8 ][0]= mvd_cache[ 9 ][0]= mpx;\n mvd_cache[ 1 ][1]=\n mvd_cache[ 8 ][1]= mvd_cache[ 9 ][1]= mpy;\n }else if(IS_SUB_8X4(sub_mb_type)){\n mv_cache[ 1 ][0]= mx;\n mv_cache[ 1 ][1]= my;\n mvd_cache[ 1 ][0]= mpx;\n mvd_cache[ 1 ][1]= mpy;\n }else if(IS_SUB_4X8(sub_mb_type)){\n mv_cache[ 8 ][0]= mx;\n mv_cache[ 8 ][1]= my;\n mvd_cache[ 8 ][0]= mpx;\n mvd_cache[ 8 ][1]= mpy;\n }\n mv_cache[ 0 ][0]= mx;\n mv_cache[ 0 ][1]= my;\n mvd_cache[ 0 ][0]= mpx;\n mvd_cache[ 0 ][1]= mpy;\n }\n }else{\n fill_rectangle(h->mv_cache [list][ scan8[4*i] ], 2, 2, 8, 0, 4);\n fill_rectangle(h->mvd_cache[list][ scan8[4*i] ], 2, 2, 8, 0, 2);\n }\n }\n }\n } else if( IS_DIRECT(mb_type) ) {\n ff_h264_pred_direct_motion(h, &mb_type);\n fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 2);\n fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 2);\n dct8x8_allowed &= h->sps.direct_8x8_inference_flag;\n } else {\n int list, i;\n if(IS_16X16(mb_type)){\n for(list=0; list<h->list_count; list++){\n if(IS_DIR(mb_type, 0, list)){\n int ref;\n if(h->ref_count[list] > 1){\n ref= decode_cabac_mb_ref(h, list, 0);\n if(ref >= (unsigned)h->ref_count[list]){\n av_log(s->avctx, AV_LOG_ERROR, "Reference %d >= %d\\n", ref, h->ref_count[list]);\n return -1;\n }\n }else\n ref=0;\n fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, ref, 1);\n }\n }\n for(list=0; list<h->list_count; list++){\n if(IS_DIR(mb_type, 0, list)){\n int mx,my,mpx,mpy;\n pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my);\n DECODE_CABAC_MB_MVD( h, list, 0)\n tprintf(s->avctx, "final mv:%d %d\\n", mx, my);\n fill_rectangle(h->mvd_cache[list][ scan8[0] ], 4, 4, 8, pack8to16(mpx,mpy), 2);\n fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);\n }\n }\n }\n else if(IS_16X8(mb_type)){\n for(list=0; list<h->list_count; list++){\n for(i=0; i<2; i++){\n if(IS_DIR(mb_type, i, list)){\n int ref;\n if(h->ref_count[list] > 1){\n ref= decode_cabac_mb_ref( h, list, 8*i );\n if(ref >= (unsigned)h->ref_count[list]){\n av_log(s->avctx, AV_LOG_ERROR, "Reference %d >= %d\\n", ref, h->ref_count[list]);\n return -1;\n }\n }else\n ref=0;\n fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, ref, 1);\n }else\n fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);\n }\n }\n for(list=0; list<h->list_count; list++){\n for(i=0; i<2; i++){\n if(IS_DIR(mb_type, i, list)){\n int mx,my,mpx,mpy;\n pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my);\n DECODE_CABAC_MB_MVD( h, list, 8*i)\n tprintf(s->avctx, "final mv:%d %d\\n", mx, my);\n fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack8to16(mpx,mpy), 2);\n fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);\n }else{\n fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 2);\n fill_rectangle(h-> mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);\n }\n }\n }\n }else{\n assert(IS_8X16(mb_type));\n for(list=0; list<h->list_count; list++){\n for(i=0; i<2; i++){\n if(IS_DIR(mb_type, i, list)){\n int ref;\n if(h->ref_count[list] > 1){\n ref= decode_cabac_mb_ref( h, list, 4*i );\n if(ref >= (unsigned)h->ref_count[list]){\n av_log(s->avctx, AV_LOG_ERROR, "Reference %d >= %d\\n", ref, h->ref_count[list]);\n return -1;\n }\n }else\n ref=0;\n fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, ref, 1);\n }else\n fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);\n }\n }\n for(list=0; list<h->list_count; list++){\n for(i=0; i<2; i++){\n if(IS_DIR(mb_type, i, list)){\n int mx,my,mpx,mpy;\n pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);\n DECODE_CABAC_MB_MVD( h, list, 4*i)\n tprintf(s->avctx, "final mv:%d %d\\n", mx, my);\n fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack8to16(mpx,mpy), 2);\n fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);\n }else{\n fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 2);\n fill_rectangle(h-> mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);\n }\n }\n }\n }\n }\n if( IS_INTER( mb_type ) ) {\n h->chroma_pred_mode_table[mb_xy] = 0;\n write_back_motion( h, mb_type );\n }\n if( !IS_INTRA16x16( mb_type ) ) {\n cbp = decode_cabac_mb_cbp_luma( h );\n if(decode_chroma)\n cbp |= decode_cabac_mb_cbp_chroma( h ) << 4;\n }\n h->cbp_table[mb_xy] = h->cbp = cbp;\n if( dct8x8_allowed && (cbp&15) && !IS_INTRA( mb_type ) ) {\n mb_type |= MB_TYPE_8x8DCT * get_cabac_noinline( &h->cabac, &h->cabac_state[399 + h->neighbor_transform_size] );\n }\n if (CHROMA444 && IS_8x8DCT(mb_type)){\n int i;\n for (i = 0; i < 2; i++){\n if (h->left_type[i] && !IS_8x8DCT(h->left_type[i])){\n h->non_zero_count_cache[3+8* 1 + 2*8*i]=\n h->non_zero_count_cache[3+8* 2 + 2*8*i]=\n h->non_zero_count_cache[3+8* 6 + 2*8*i]=\n h->non_zero_count_cache[3+8* 7 + 2*8*i]=\n h->non_zero_count_cache[3+8*11 + 2*8*i]=\n h->non_zero_count_cache[3+8*12 + 2*8*i]= IS_INTRA(mb_type) ? 64 : 0;\n }\n }\n if (h->top_type && !IS_8x8DCT(h->top_type)){\n uint32_t top_empty = CABAC && !IS_INTRA(mb_type) ? 0 : 0x40404040;\n AV_WN32A(&h->non_zero_count_cache[4+8* 0], top_empty);\n AV_WN32A(&h->non_zero_count_cache[4+8* 5], top_empty);\n AV_WN32A(&h->non_zero_count_cache[4+8*10], top_empty);\n }\n }\n s->current_picture.mb_type[mb_xy]= mb_type;\n if( cbp || IS_INTRA16x16( mb_type ) ) {\n const uint8_t *scan, *scan8x8;\n const uint32_t *qmul;\n if(IS_INTERLACED(mb_type)){\n scan8x8= s->qscale ? h->field_scan8x8 : h->field_scan8x8_q0;\n scan= s->qscale ? h->field_scan : h->field_scan_q0;\n }else{\n scan8x8= s->qscale ? h->zigzag_scan8x8 : h->zigzag_scan8x8_q0;\n scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;\n }\n if(get_cabac_noinline( &h->cabac, &h->cabac_state[60 + (h->last_qscale_diff != 0)])){\n int val = 1;\n int ctx= 2;\n const int max_qp = 51 + 6*(h->sps.bit_depth_luma-8);\n while( get_cabac_noinline( &h->cabac, &h->cabac_state[60 + ctx] ) ) {\n ctx= 3;\n val++;\n if(val > 2*max_qp){\n av_log(h->s.avctx, AV_LOG_ERROR, "cabac decode of qscale diff failed at %d %d\\n", s->mb_x, s->mb_y);\n return -1;\n }\n }\n if( val&0x01 )\n val= (val + 1)>>1 ;\n else\n val= -((val + 1)>>1);\n h->last_qscale_diff = val;\n s->qscale += val;\n if(((unsigned)s->qscale) > max_qp){\n if(s->qscale<0) s->qscale+= max_qp+1;\n else s->qscale-= max_qp+1;\n }\n h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);\n h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);\n }else\n h->last_qscale_diff=0;\n decode_cabac_luma_residual(h, scan, scan8x8, pixel_shift, mb_type, cbp, 0);\n if(CHROMA444){\n decode_cabac_luma_residual(h, scan, scan8x8, pixel_shift, mb_type, cbp, 1);\n decode_cabac_luma_residual(h, scan, scan8x8, pixel_shift, mb_type, cbp, 2);\n } else {\n if( cbp&0x30 ){\n int c;\n for( c = 0; c < 2; c++ ) {\n decode_cabac_residual_dc(h, h->mb + ((256 + 16*16*c) << pixel_shift), 3, CHROMA_DC_BLOCK_INDEX+c, chroma_dc_scan, 4);\n }\n }\n if( cbp&0x20 ) {\n int c, i;\n for( c = 0; c < 2; c++ ) {\n qmul = h->dequant4_coeff[c+1+(IS_INTRA( mb_type ) ? 0:3)][h->chroma_qp[c]];\n for( i = 0; i < 4; i++ ) {\n const int index = 16 + 16 * c + i;\n decode_cabac_residual_nondc(h, h->mb + (16*index << pixel_shift), 4, index, scan + 1, qmul, 15);\n }\n }\n } else {\n fill_rectangle(&h->non_zero_count_cache[scan8[16]], 4, 4, 8, 0, 1);\n fill_rectangle(&h->non_zero_count_cache[scan8[32]], 4, 4, 8, 0, 1);\n }\n }\n } else {\n fill_rectangle(&h->non_zero_count_cache[scan8[ 0]], 4, 4, 8, 0, 1);\n fill_rectangle(&h->non_zero_count_cache[scan8[16]], 4, 4, 8, 0, 1);\n fill_rectangle(&h->non_zero_count_cache[scan8[32]], 4, 4, 8, 0, 1);\n h->last_qscale_diff = 0;\n }\n s->current_picture.qscale_table[mb_xy]= s->qscale;\n write_back_non_zero_count(h);\n if(MB_MBAFF){\n h->ref_count[0] >>= 1;\n h->ref_count[1] >>= 1;\n }\n return 0;\n}', 'static void fill_decode_neighbors(H264Context *h, int mb_type){\n MpegEncContext * const s = &h->s;\n const int mb_xy= h->mb_xy;\n int topleft_xy, top_xy, topright_xy, left_xy[2];\n static const uint8_t left_block_options[4][32]={\n {0,1,2,3,7,10,8,11,3+0*4, 3+1*4, 3+2*4, 3+3*4, 1+4*4, 1+8*4, 1+5*4, 1+9*4},\n {2,2,3,3,8,11,8,11,3+2*4, 3+2*4, 3+3*4, 3+3*4, 1+5*4, 1+9*4, 1+5*4, 1+9*4},\n {0,0,1,1,7,10,7,10,3+0*4, 3+0*4, 3+1*4, 3+1*4, 1+4*4, 1+8*4, 1+4*4, 1+8*4},\n {0,2,0,2,7,10,7,10,3+0*4, 3+2*4, 3+0*4, 3+2*4, 1+4*4, 1+8*4, 1+4*4, 1+8*4}\n };\n h->topleft_partition= -1;\n top_xy = mb_xy - (s->mb_stride << MB_FIELD);\n topleft_xy = top_xy - 1;\n topright_xy= top_xy + 1;\n left_xy[1] = left_xy[0] = mb_xy-1;\n h->left_block = left_block_options[0];\n if(FRAME_MBAFF){\n const int left_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]);\n const int curr_mb_field_flag = IS_INTERLACED(mb_type);\n if(s->mb_y&1){\n if (left_mb_field_flag != curr_mb_field_flag) {\n left_xy[1] = left_xy[0] = mb_xy - s->mb_stride - 1;\n if (curr_mb_field_flag) {\n left_xy[1] += s->mb_stride;\n h->left_block = left_block_options[3];\n } else {\n topleft_xy += s->mb_stride;\n h->topleft_partition = 0;\n h->left_block = left_block_options[1];\n }\n }\n }else{\n if(curr_mb_field_flag){\n topleft_xy += s->mb_stride & (((s->current_picture.mb_type[top_xy - 1]>>7)&1)-1);\n topright_xy += s->mb_stride & (((s->current_picture.mb_type[top_xy + 1]>>7)&1)-1);\n top_xy += s->mb_stride & (((s->current_picture.mb_type[top_xy ]>>7)&1)-1);\n }\n if (left_mb_field_flag != curr_mb_field_flag) {\n if (curr_mb_field_flag) {\n left_xy[1] += s->mb_stride;\n h->left_block = left_block_options[3];\n } else {\n h->left_block = left_block_options[2];\n }\n }\n }\n }\n h->topleft_mb_xy = topleft_xy;\n h->top_mb_xy = top_xy;\n h->topright_mb_xy= topright_xy;\n h->left_mb_xy[0] = left_xy[0];\n h->left_mb_xy[1] = left_xy[1];\n h->topleft_type = s->current_picture.mb_type[topleft_xy] ;\n h->top_type = s->current_picture.mb_type[top_xy] ;\n h->topright_type= s->current_picture.mb_type[topright_xy];\n h->left_type[0] = s->current_picture.mb_type[left_xy[0]] ;\n h->left_type[1] = s->current_picture.mb_type[left_xy[1]] ;\n if(FMO){\n if(h->slice_table[topleft_xy ] != h->slice_num) h->topleft_type = 0;\n if(h->slice_table[top_xy ] != h->slice_num) h->top_type = 0;\n if(h->slice_table[left_xy[0] ] != h->slice_num) h->left_type[0] = h->left_type[1] = 0;\n }else{\n if(h->slice_table[topleft_xy ] != h->slice_num){\n h->topleft_type = 0;\n if(h->slice_table[top_xy ] != h->slice_num) h->top_type = 0;\n if(h->slice_table[left_xy[0] ] != h->slice_num) h->left_type[0] = h->left_type[1] = 0;\n }\n }\n if(h->slice_table[topright_xy] != h->slice_num) h->topright_type= 0;\n}', 'static void fill_decode_caches(H264Context *h, int mb_type){\n MpegEncContext * const s = &h->s;\n int topleft_xy, top_xy, topright_xy, left_xy[2];\n int topleft_type, top_type, topright_type, left_type[2];\n const uint8_t * left_block= h->left_block;\n int i;\n topleft_xy = h->topleft_mb_xy ;\n top_xy = h->top_mb_xy ;\n topright_xy = h->topright_mb_xy;\n left_xy[0] = h->left_mb_xy[0] ;\n left_xy[1] = h->left_mb_xy[1] ;\n topleft_type = h->topleft_type ;\n top_type = h->top_type ;\n topright_type= h->topright_type ;\n left_type[0] = h->left_type[0] ;\n left_type[1] = h->left_type[1] ;\n if(!IS_SKIP(mb_type)){\n if(IS_INTRA(mb_type)){\n int type_mask= h->pps.constrained_intra_pred ? IS_INTRA(-1) : -1;\n h->topleft_samples_available=\n h->top_samples_available=\n h->left_samples_available= 0xFFFF;\n h->topright_samples_available= 0xEEEA;\n if(!(top_type & type_mask)){\n h->topleft_samples_available= 0xB3FF;\n h->top_samples_available= 0x33FF;\n h->topright_samples_available= 0x26EA;\n }\n if(IS_INTERLACED(mb_type) != IS_INTERLACED(left_type[0])){\n if(IS_INTERLACED(mb_type)){\n if(!(left_type[0] & type_mask)){\n h->topleft_samples_available&= 0xDFFF;\n h->left_samples_available&= 0x5FFF;\n }\n if(!(left_type[1] & type_mask)){\n h->topleft_samples_available&= 0xFF5F;\n h->left_samples_available&= 0xFF5F;\n }\n }else{\n int left_typei = s->current_picture.mb_type[left_xy[0] + s->mb_stride];\n assert(left_xy[0] == left_xy[1]);\n if(!((left_typei & type_mask) && (left_type[0] & type_mask))){\n h->topleft_samples_available&= 0xDF5F;\n h->left_samples_available&= 0x5F5F;\n }\n }\n }else{\n if(!(left_type[0] & type_mask)){\n h->topleft_samples_available&= 0xDF5F;\n h->left_samples_available&= 0x5F5F;\n }\n }\n if(!(topleft_type & type_mask))\n h->topleft_samples_available&= 0x7FFF;\n if(!(topright_type & type_mask))\n h->topright_samples_available&= 0xFBFF;\n if(IS_INTRA4x4(mb_type)){\n if(IS_INTRA4x4(top_type)){\n AV_COPY32(h->intra4x4_pred_mode_cache+4+8*0, h->intra4x4_pred_mode + h->mb2br_xy[top_xy]);\n }else{\n h->intra4x4_pred_mode_cache[4+8*0]=\n h->intra4x4_pred_mode_cache[5+8*0]=\n h->intra4x4_pred_mode_cache[6+8*0]=\n h->intra4x4_pred_mode_cache[7+8*0]= 2 - 3*!(top_type & type_mask);\n }\n for(i=0; i<2; i++){\n if(IS_INTRA4x4(left_type[i])){\n int8_t *mode= h->intra4x4_pred_mode + h->mb2br_xy[left_xy[i]];\n h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= mode[6-left_block[0+2*i]];\n h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= mode[6-left_block[1+2*i]];\n }else{\n h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=\n h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= 2 - 3*!(left_type[i] & type_mask);\n }\n }\n }\n }\n if(top_type){\n AV_COPY32(&h->non_zero_count_cache[4+8* 0], &h->non_zero_count[top_xy][4*3]);\n if(CHROMA444){\n AV_COPY32(&h->non_zero_count_cache[4+8* 5], &h->non_zero_count[top_xy][4* 7]);\n AV_COPY32(&h->non_zero_count_cache[4+8*10], &h->non_zero_count[top_xy][4*11]);\n }else{\n AV_COPY32(&h->non_zero_count_cache[4+8* 5], &h->non_zero_count[top_xy][4* 5]);\n AV_COPY32(&h->non_zero_count_cache[4+8*10], &h->non_zero_count[top_xy][4* 9]);\n }\n }else{\n uint32_t top_empty = CABAC && !IS_INTRA(mb_type) ? 0 : 0x40404040;\n AV_WN32A(&h->non_zero_count_cache[4+8* 0], top_empty);\n AV_WN32A(&h->non_zero_count_cache[4+8* 5], top_empty);\n AV_WN32A(&h->non_zero_count_cache[4+8*10], top_empty);\n }\n for (i=0; i<2; i++) {\n if(left_type[i]){\n h->non_zero_count_cache[3+8* 1 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[8+0+2*i]];\n h->non_zero_count_cache[3+8* 2 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[8+1+2*i]];\n if(CHROMA444){\n h->non_zero_count_cache[3+8* 6 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[8+0+2*i]+4*4];\n h->non_zero_count_cache[3+8* 7 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[8+1+2*i]+4*4];\n h->non_zero_count_cache[3+8*11 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[8+0+2*i]+8*4];\n h->non_zero_count_cache[3+8*12 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[8+1+2*i]+8*4];\n }else{\n h->non_zero_count_cache[3+8* 6 + 8*i]= h->non_zero_count[left_xy[i]][left_block[8+4+2*i]];\n h->non_zero_count_cache[3+8*11 + 8*i]= h->non_zero_count[left_xy[i]][left_block[8+5+2*i]];\n }\n }else{\n h->non_zero_count_cache[3+8* 1 + 2*8*i]=\n h->non_zero_count_cache[3+8* 2 + 2*8*i]=\n h->non_zero_count_cache[3+8* 6 + 2*8*i]=\n h->non_zero_count_cache[3+8* 7 + 2*8*i]=\n h->non_zero_count_cache[3+8*11 + 2*8*i]=\n h->non_zero_count_cache[3+8*12 + 2*8*i]= CABAC && !IS_INTRA(mb_type) ? 0 : 64;\n }\n }\n if( CABAC ) {\n if(top_type) {\n h->top_cbp = h->cbp_table[top_xy];\n } else {\n h->top_cbp = IS_INTRA(mb_type) ? 0x7CF : 0x00F;\n }\n if (left_type[0]) {\n h->left_cbp = (h->cbp_table[left_xy[0]] & 0x7F0)\n | ((h->cbp_table[left_xy[0]]>>(left_block[0]&(~1)))&2)\n | (((h->cbp_table[left_xy[1]]>>(left_block[2]&(~1)))&2) << 2);\n } else {\n h->left_cbp = IS_INTRA(mb_type) ? 0x7CF : 0x00F;\n }\n }\n }\n if(IS_INTER(mb_type) || (IS_DIRECT(mb_type) && h->direct_spatial_mv_pred)){\n int list;\n for(list=0; list<h->list_count; list++){\n if(!USES_LIST(mb_type, list)){\n continue;\n }\n assert(!(IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred));\n h->mv_cache_clean[list]= 0;\n if(USES_LIST(top_type, list)){\n const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;\n AV_COPY128(h->mv_cache[list][scan8[0] + 0 - 1*8], s->current_picture.motion_val[list][b_xy + 0]);\n h->ref_cache[list][scan8[0] + 0 - 1*8]=\n h->ref_cache[list][scan8[0] + 1 - 1*8]= s->current_picture.ref_index[list][4*top_xy + 2];\n h->ref_cache[list][scan8[0] + 2 - 1*8]=\n h->ref_cache[list][scan8[0] + 3 - 1*8]= s->current_picture.ref_index[list][4*top_xy + 3];\n }else{\n AV_ZERO128(h->mv_cache[list][scan8[0] + 0 - 1*8]);\n AV_WN32A(&h->ref_cache[list][scan8[0] + 0 - 1*8], ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101);\n }\n if(mb_type & (MB_TYPE_16x8|MB_TYPE_8x8)){\n for(i=0; i<2; i++){\n int cache_idx = scan8[0] - 1 + i*2*8;\n if(USES_LIST(left_type[i], list)){\n const int b_xy= h->mb2b_xy[left_xy[i]] + 3;\n const int b8_xy= 4*left_xy[i] + 1;\n AV_COPY32(h->mv_cache[list][cache_idx ], s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[0+i*2]]);\n AV_COPY32(h->mv_cache[list][cache_idx+8], s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[1+i*2]]);\n h->ref_cache[list][cache_idx ]= s->current_picture.ref_index[list][b8_xy + (left_block[0+i*2]&~1)];\n h->ref_cache[list][cache_idx+8]= s->current_picture.ref_index[list][b8_xy + (left_block[1+i*2]&~1)];\n }else{\n AV_ZERO32(h->mv_cache [list][cache_idx ]);\n AV_ZERO32(h->mv_cache [list][cache_idx+8]);\n h->ref_cache[list][cache_idx ]=\n h->ref_cache[list][cache_idx+8]= (left_type[i]) ? LIST_NOT_USED : PART_NOT_AVAILABLE;\n }\n }\n }else{\n if(USES_LIST(left_type[0], list)){\n const int b_xy= h->mb2b_xy[left_xy[0]] + 3;\n const int b8_xy= 4*left_xy[0] + 1;\n AV_COPY32(h->mv_cache[list][scan8[0] - 1], s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[0]]);\n h->ref_cache[list][scan8[0] - 1]= s->current_picture.ref_index[list][b8_xy + (left_block[0]&~1)];\n }else{\n AV_ZERO32(h->mv_cache [list][scan8[0] - 1]);\n h->ref_cache[list][scan8[0] - 1]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;\n }\n }\n if(USES_LIST(topright_type, list)){\n const int b_xy= h->mb2b_xy[topright_xy] + 3*h->b_stride;\n AV_COPY32(h->mv_cache[list][scan8[0] + 4 - 1*8], s->current_picture.motion_val[list][b_xy]);\n h->ref_cache[list][scan8[0] + 4 - 1*8]= s->current_picture.ref_index[list][4*topright_xy + 2];\n }else{\n AV_ZERO32(h->mv_cache [list][scan8[0] + 4 - 1*8]);\n h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;\n }\n if(h->ref_cache[list][scan8[0] + 4 - 1*8] < 0){\n if(USES_LIST(topleft_type, list)){\n const int b_xy = h->mb2b_xy [topleft_xy] + 3 + h->b_stride + (h->topleft_partition & 2*h->b_stride);\n const int b8_xy= 4*topleft_xy + 1 + (h->topleft_partition & 2);\n AV_COPY32(h->mv_cache[list][scan8[0] - 1 - 1*8], s->current_picture.motion_val[list][b_xy]);\n h->ref_cache[list][scan8[0] - 1 - 1*8]= s->current_picture.ref_index[list][b8_xy];\n }else{\n AV_ZERO32(h->mv_cache[list][scan8[0] - 1 - 1*8]);\n h->ref_cache[list][scan8[0] - 1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;\n }\n }\n if((mb_type&(MB_TYPE_SKIP|MB_TYPE_DIRECT2)) && !FRAME_MBAFF)\n continue;\n if(!(mb_type&(MB_TYPE_SKIP|MB_TYPE_DIRECT2))) {\n h->ref_cache[list][scan8[4 ]] =\n h->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE;\n AV_ZERO32(h->mv_cache [list][scan8[4 ]]);\n AV_ZERO32(h->mv_cache [list][scan8[12]]);\n if( CABAC ) {\n if(USES_LIST(top_type, list)){\n const int b_xy= h->mb2br_xy[top_xy];\n AV_COPY64(h->mvd_cache[list][scan8[0] + 0 - 1*8], h->mvd_table[list][b_xy + 0]);\n }else{\n AV_ZERO64(h->mvd_cache[list][scan8[0] + 0 - 1*8]);\n }\n if(USES_LIST(left_type[0], list)){\n const int b_xy= h->mb2br_xy[left_xy[0]] + 6;\n AV_COPY16(h->mvd_cache[list][scan8[0] - 1 + 0*8], h->mvd_table[list][b_xy - left_block[0]]);\n AV_COPY16(h->mvd_cache[list][scan8[0] - 1 + 1*8], h->mvd_table[list][b_xy - left_block[1]]);\n }else{\n AV_ZERO16(h->mvd_cache [list][scan8[0] - 1 + 0*8]);\n AV_ZERO16(h->mvd_cache [list][scan8[0] - 1 + 1*8]);\n }\n if(USES_LIST(left_type[1], list)){\n const int b_xy= h->mb2br_xy[left_xy[1]] + 6;\n AV_COPY16(h->mvd_cache[list][scan8[0] - 1 + 2*8], h->mvd_table[list][b_xy - left_block[2]]);\n AV_COPY16(h->mvd_cache[list][scan8[0] - 1 + 3*8], h->mvd_table[list][b_xy - left_block[3]]);\n }else{\n AV_ZERO16(h->mvd_cache [list][scan8[0] - 1 + 2*8]);\n AV_ZERO16(h->mvd_cache [list][scan8[0] - 1 + 3*8]);\n }\n AV_ZERO16(h->mvd_cache [list][scan8[4 ]]);\n AV_ZERO16(h->mvd_cache [list][scan8[12]]);\n if(h->slice_type_nos == AV_PICTURE_TYPE_B){\n fill_rectangle(&h->direct_cache[scan8[0]], 4, 4, 8, MB_TYPE_16x16>>1, 1);\n if(IS_DIRECT(top_type)){\n AV_WN32A(&h->direct_cache[scan8[0] - 1*8], 0x01010101u*(MB_TYPE_DIRECT2>>1));\n }else if(IS_8X8(top_type)){\n int b8_xy = 4*top_xy;\n h->direct_cache[scan8[0] + 0 - 1*8]= h->direct_table[b8_xy + 2];\n h->direct_cache[scan8[0] + 2 - 1*8]= h->direct_table[b8_xy + 3];\n }else{\n AV_WN32A(&h->direct_cache[scan8[0] - 1*8], 0x01010101*(MB_TYPE_16x16>>1));\n }\n if(IS_DIRECT(left_type[0]))\n h->direct_cache[scan8[0] - 1 + 0*8]= MB_TYPE_DIRECT2>>1;\n else if(IS_8X8(left_type[0]))\n h->direct_cache[scan8[0] - 1 + 0*8]= h->direct_table[4*left_xy[0] + 1 + (left_block[0]&~1)];\n else\n h->direct_cache[scan8[0] - 1 + 0*8]= MB_TYPE_16x16>>1;\n if(IS_DIRECT(left_type[1]))\n h->direct_cache[scan8[0] - 1 + 2*8]= MB_TYPE_DIRECT2>>1;\n else if(IS_8X8(left_type[1]))\n h->direct_cache[scan8[0] - 1 + 2*8]= h->direct_table[4*left_xy[1] + 1 + (left_block[2]&~1)];\n else\n h->direct_cache[scan8[0] - 1 + 2*8]= MB_TYPE_16x16>>1;\n }\n }\n }\n if(FRAME_MBAFF){\n#define MAP_MVS\\\n MAP_F2F(scan8[0] - 1 - 1*8, topleft_type)\\\n MAP_F2F(scan8[0] + 0 - 1*8, top_type)\\\n MAP_F2F(scan8[0] + 1 - 1*8, top_type)\\\n MAP_F2F(scan8[0] + 2 - 1*8, top_type)\\\n MAP_F2F(scan8[0] + 3 - 1*8, top_type)\\\n MAP_F2F(scan8[0] + 4 - 1*8, topright_type)\\\n MAP_F2F(scan8[0] - 1 + 0*8, left_type[0])\\\n MAP_F2F(scan8[0] - 1 + 1*8, left_type[0])\\\n MAP_F2F(scan8[0] - 1 + 2*8, left_type[1])\\\n MAP_F2F(scan8[0] - 1 + 3*8, left_type[1])\n if(MB_FIELD){\n#define MAP_F2F(idx, mb_type)\\\n if(!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\\\n h->ref_cache[list][idx] <<= 1;\\\n h->mv_cache[list][idx][1] /= 2;\\\n h->mvd_cache[list][idx][1] >>=1;\\\n }\n MAP_MVS\n#undef MAP_F2F\n }else{\n#define MAP_F2F(idx, mb_type)\\\n if(IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\\\n h->ref_cache[list][idx] >>= 1;\\\n h->mv_cache[list][idx][1] <<= 1;\\\n h->mvd_cache[list][idx][1] <<= 1;\\\n }\n MAP_MVS\n#undef MAP_F2F\n }\n }\n }\n }\n h->neighbor_transform_size= !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[0]);\n}'] |
521 | 0 | https://github.com/openssl/openssl/blob/c504a5e78386aa9f02462d18a90da759f9131321/crypto/bn/bn_ctx.c/#L353 | 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\tconst BIGNUM *q, BN_CTX *ctx)\n{\n\tBIGNUM *ret = NULL, *r0, *r1, *r2;\n\tif (d == NULL || p == NULL || q == NULL)\n\t\treturn NULL;\n\tBN_CTX_start(ctx);\n\tr0 = BN_CTX_get(ctx);\n\tr1 = BN_CTX_get(ctx);\n\tr2 = BN_CTX_get(ctx);\n\tif (r2 == NULL)\n\t\tgoto err;\n\tif (!BN_sub(r1, p, BN_value_one())) goto err;\n\tif (!BN_sub(r2, q, BN_value_one())) goto err;\n\tif (!BN_mul(r0, r1, r2, ctx)) goto err;\n\tret = BN_mod_inverse(NULL, d, r0, ctx);\nerr:\n\tBN_CTX_end(ctx);\n\treturn ret;\n}', '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_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n\t{\n\tint ret=0;\n\tint top,al,bl;\n\tBIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\tint i;\n#endif\n#ifdef BN_RECURSION\n\tBIGNUM *t=NULL;\n\tint j=0,k;\n#endif\n#ifdef BN_COUNT\n\tfprintf(stderr,"BN_mul %d * %d\\n",a->top,b->top);\n#endif\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tbn_check_top(r);\n\tal=a->top;\n\tbl=b->top;\n\tif ((al == 0) || (bl == 0))\n\t\t{\n\t\tBN_zero(r);\n\t\treturn(1);\n\t\t}\n\ttop=al+bl;\n\tBN_CTX_start(ctx);\n\tif ((r == a) || (r == b))\n\t\t{\n\t\tif ((rr = BN_CTX_get(ctx)) == NULL) goto err;\n\t\t}\n\telse\n\t\trr = r;\n\trr->neg=a->neg^b->neg;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\ti = al-bl;\n#endif\n#ifdef BN_MUL_COMBA\n\tif (i == 0)\n\t\t{\n# if 0\n\t\tif (al == 4)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,8) == NULL) goto err;\n\t\t\trr->top=8;\n\t\t\tbn_mul_comba4(rr->d,a->d,b->d);\n\t\t\tgoto end;\n\t\t\t}\n# endif\n\t\tif (al == 8)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,16) == NULL) goto err;\n\t\t\trr->top=16;\n\t\t\tbn_mul_comba8(rr->d,a->d,b->d);\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n#endif\n#ifdef BN_RECURSION\n\tif ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL))\n\t\t{\n\t\tif (i >= -1 && i <= 1)\n\t\t\t{\n\t\t\tint sav_j =0;\n\t\t\tif (i >= 0)\n\t\t\t\t{\n\t\t\t\tj = BN_num_bits_word((BN_ULONG)al);\n\t\t\t\t}\n\t\t\tif (i == -1)\n\t\t\t\t{\n\t\t\t\tj = BN_num_bits_word((BN_ULONG)bl);\n\t\t\t\t}\n\t\t\tsav_j = j;\n\t\t\tj = 1<<(j-1);\n\t\t\tassert(j <= al || j <= bl);\n\t\t\tk = j+j;\n\t\t\tt = BN_CTX_get(ctx);\n\t\t\tif (al > j || bl > j)\n\t\t\t\t{\n\t\t\t\tbn_wexpand(t,k*4);\n\t\t\t\tbn_wexpand(rr,k*4);\n\t\t\t\tbn_mul_part_recursive(rr->d,a->d,b->d,\n\t\t\t\t\tj,al-j,bl-j,t->d);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tbn_wexpand(t,k*2);\n\t\t\t\tbn_wexpand(rr,k*2);\n\t\t\t\tbn_mul_recursive(rr->d,a->d,b->d,\n\t\t\t\t\tj,al-j,bl-j,t->d);\n\t\t\t\t}\n\t\t\trr->top=top;\n\t\t\tgoto end;\n\t\t\t}\n#if 0\n\t\tif (i == 1 && !BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tBIGNUM *tmp_bn = (BIGNUM *)b;\n\t\t\tif (bn_wexpand(tmp_bn,al) == NULL) goto err;\n\t\t\ttmp_bn->d[bl]=0;\n\t\t\tbl++;\n\t\t\ti--;\n\t\t\t}\n\t\telse if (i == -1 && !BN_get_flags(a,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tBIGNUM *tmp_bn = (BIGNUM *)a;\n\t\t\tif (bn_wexpand(tmp_bn,bl) == NULL) goto err;\n\t\t\ttmp_bn->d[al]=0;\n\t\t\tal++;\n\t\t\ti++;\n\t\t\t}\n\t\tif (i == 0)\n\t\t\t{\n\t\t\tj=BN_num_bits_word((BN_ULONG)al);\n\t\t\tj=1<<(j-1);\n\t\t\tk=j+j;\n\t\t\tt = BN_CTX_get(ctx);\n\t\t\tif (al == j)\n\t\t\t\t{\n\t\t\t\tif (bn_wexpand(t,k*2) == NULL) goto err;\n\t\t\t\tif (bn_wexpand(rr,k*2) == NULL) goto err;\n\t\t\t\tbn_mul_recursive(rr->d,a->d,b->d,al,t->d);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (bn_wexpand(t,k*4) == NULL) goto err;\n\t\t\t\tif (bn_wexpand(rr,k*4) == NULL) goto err;\n\t\t\t\tbn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d);\n\t\t\t\t}\n\t\t\trr->top=top;\n\t\t\tgoto end;\n\t\t\t}\n#endif\n\t\t}\n#endif\n\tif (bn_wexpand(rr,top) == NULL) goto err;\n\trr->top=top;\n\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\nend:\n#endif\n\tbn_correct_top(rr);\n\tif (r != rr) BN_copy(r,rr);\n\tret=1;\nerr:\n\tbn_check_top(r);\n\tBN_CTX_end(ctx);\n\treturn(ret);\n\t}', 'BIGNUM *BN_mod_inverse(BIGNUM *in,\n\tconst BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n\t{\n\tBIGNUM *A,*B,*X,*Y,*M,*D,*T,*R=NULL;\n\tBIGNUM *ret=NULL;\n\tint sign;\n\tif ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0))\n\t\t{\n\t\treturn BN_mod_inverse_no_branch(in, a, n, ctx);\n\t\t}\n\tbn_check_top(a);\n\tbn_check_top(n);\n\tBN_CTX_start(ctx);\n\tA = BN_CTX_get(ctx);\n\tB = BN_CTX_get(ctx);\n\tX = BN_CTX_get(ctx);\n\tD = BN_CTX_get(ctx);\n\tM = BN_CTX_get(ctx);\n\tY = BN_CTX_get(ctx);\n\tT = BN_CTX_get(ctx);\n\tif (T == NULL) goto err;\n\tif (in == NULL)\n\t\tR=BN_new();\n\telse\n\t\tR=in;\n\tif (R == NULL) goto err;\n\tBN_one(X);\n\tBN_zero(Y);\n\tif (BN_copy(B,a) == NULL) goto err;\n\tif (BN_copy(A,n) == NULL) goto err;\n\tA->neg = 0;\n\tif (B->neg || (BN_ucmp(B, A) >= 0))\n\t\t{\n\t\tif (!BN_nnmod(B, B, A, ctx)) goto err;\n\t\t}\n\tsign = -1;\n\tif (BN_is_odd(n) && (BN_num_bits(n) <= (BN_BITS <= 32 ? 450 : 2048)))\n\t\t{\n\t\tint shift;\n\t\twhile (!BN_is_zero(B))\n\t\t\t{\n\t\t\tshift = 0;\n\t\t\twhile (!BN_is_bit_set(B, shift))\n\t\t\t\t{\n\t\t\t\tshift++;\n\t\t\t\tif (BN_is_odd(X))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_uadd(X, X, n)) goto err;\n\t\t\t\t\t}\n\t\t\t\tif (!BN_rshift1(X, X)) goto err;\n\t\t\t\t}\n\t\t\tif (shift > 0)\n\t\t\t\t{\n\t\t\t\tif (!BN_rshift(B, B, shift)) goto err;\n\t\t\t\t}\n\t\t\tshift = 0;\n\t\t\twhile (!BN_is_bit_set(A, shift))\n\t\t\t\t{\n\t\t\t\tshift++;\n\t\t\t\tif (BN_is_odd(Y))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_uadd(Y, Y, n)) goto err;\n\t\t\t\t\t}\n\t\t\t\tif (!BN_rshift1(Y, Y)) goto err;\n\t\t\t\t}\n\t\t\tif (shift > 0)\n\t\t\t\t{\n\t\t\t\tif (!BN_rshift(A, A, shift)) goto err;\n\t\t\t\t}\n\t\t\tif (BN_ucmp(B, A) >= 0)\n\t\t\t\t{\n\t\t\t\tif (!BN_uadd(X, X, Y)) goto err;\n\t\t\t\tif (!BN_usub(B, B, A)) goto err;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (!BN_uadd(Y, Y, X)) goto err;\n\t\t\t\tif (!BN_usub(A, A, B)) goto err;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\twhile (!BN_is_zero(B))\n\t\t\t{\n\t\t\tBIGNUM *tmp;\n\t\t\tif (BN_num_bits(A) == BN_num_bits(B))\n\t\t\t\t{\n\t\t\t\tif (!BN_one(D)) goto err;\n\t\t\t\tif (!BN_sub(M,A,B)) goto err;\n\t\t\t\t}\n\t\t\telse if (BN_num_bits(A) == BN_num_bits(B) + 1)\n\t\t\t\t{\n\t\t\t\tif (!BN_lshift1(T,B)) goto err;\n\t\t\t\tif (BN_ucmp(A,T) < 0)\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_one(D)) goto err;\n\t\t\t\t\tif (!BN_sub(M,A,B)) goto err;\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_sub(M,A,T)) goto err;\n\t\t\t\t\tif (!BN_add(D,T,B)) goto err;\n\t\t\t\t\tif (BN_ucmp(A,D) < 0)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tif (!BN_set_word(D,2)) goto err;\n\t\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t\t{\n\t\t\t\t\t\tif (!BN_set_word(D,3)) goto err;\n\t\t\t\t\t\tif (!BN_sub(M,M,B)) goto err;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (!BN_div(D,M,A,B,ctx)) goto err;\n\t\t\t\t}\n\t\t\ttmp=A;\n\t\t\tA=B;\n\t\t\tB=M;\n\t\t\tif (BN_is_one(D))\n\t\t\t\t{\n\t\t\t\tif (!BN_add(tmp,X,Y)) goto err;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (BN_is_word(D,2))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_lshift1(tmp,X)) goto err;\n\t\t\t\t\t}\n\t\t\t\telse if (BN_is_word(D,4))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_lshift(tmp,X,2)) goto err;\n\t\t\t\t\t}\n\t\t\t\telse if (D->top == 1)\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_copy(tmp,X)) goto err;\n\t\t\t\t\tif (!BN_mul_word(tmp,D->d[0])) goto err;\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_mul(tmp,D,X,ctx)) goto err;\n\t\t\t\t\t}\n\t\t\t\tif (!BN_add(tmp,tmp,Y)) goto err;\n\t\t\t\t}\n\t\t\tM=Y;\n\t\t\tY=X;\n\t\t\tX=tmp;\n\t\t\tsign = -sign;\n\t\t\t}\n\t\t}\n\tif (sign < 0)\n\t\t{\n\t\tif (!BN_sub(Y,n,Y)) goto err;\n\t\t}\n\tif (BN_is_one(A))\n\t\t{\n\t\tif (!Y->neg && BN_ucmp(Y,n) < 0)\n\t\t\t{\n\t\t\tif (!BN_copy(R,Y)) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (!BN_nnmod(R,Y,n,ctx)) goto err;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_INVERSE,BN_R_NO_INVERSE);\n\t\tgoto err;\n\t\t}\n\tret=R;\nerr:\n\tif ((ret == NULL) && (in == NULL)) BN_free(R);\n\tBN_CTX_end(ctx);\n\tbn_check_top(ret);\n\treturn(ret);\n\t}', '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 ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0))\n\t\t{\n\t\treturn BN_div_no_branch(dv, rm, num, divisor, ctx);\n\t\t}\n\tbn_check_top(dv);\n\tbn_check_top(rm);\n\tbn_check_top(num);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\n\t\t}\n\tif (BN_ucmp(num,divisor) < 0)\n\t\t{\n\t\tif (rm != NULL)\n\t\t\t{ if (BN_copy(rm,num) == NULL) return(0); }\n\t\tif (dv != NULL) BN_zero(dv);\n\t\treturn(1);\n\t\t}\n\tBN_CTX_start(ctx);\n\ttmp=BN_CTX_get(ctx);\n\tsnum=BN_CTX_get(ctx);\n\tsdiv=BN_CTX_get(ctx);\n\tif (dv == NULL)\n\t\tres=BN_CTX_get(ctx);\n\telse\tres=dv;\n\tif (sdiv == NULL || res == NULL) goto err;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tif (!(BN_lshift(sdiv,divisor,norm_shift))) goto err;\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tif (!(BN_lshift(snum,num,norm_shift))) goto err;\n\tsnum->neg=0;\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\twnum.neg = 0;\n\twnum.d = &(snum->d[loop]);\n\twnum.top = div_n;\n\twnum.dmax = snum->dmax - loop;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (BN_ucmp(&wnum,sdiv) >= 0)\n\t\t{\n\t\tbn_clear_top2max(&wnum);\n\t\tbn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n\t\t*resp=1;\n\t\t}\n\telse\n\t\tres->top--;\n\tif (res->top == 0)\n\t\tres->neg = 0;\n\telse\n\t\tresp--;\n\tfor (i=0; i<loop-1; i++, wnump--, resp--)\n\t\t{\n\t\tBN_ULONG q,l0;\n#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n\t\tBN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG);\n\t\tq=bn_div_3_words(wnump,d1,d0);\n#else\n\t\tBN_ULONG n0,n1,rem=0;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\t{\n#ifdef BN_LLONG\n\t\t\tBN_ULLONG t2;\n#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n\t\t\tq=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);\n#else\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tt2 -= d1;\n\t\t\t\t}\n#else\n\t\t\tBN_ULONG t2l,t2h,ql,qh;\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#if defined(BN_UMULT_LOHI)\n\t\t\tBN_UMULT_LOHI(t2l,t2h,d1,q);\n#elif defined(BN_UMULT_HIGH)\n\t\t\tt2l = d1 * q;\n\t\t\tt2h = BN_UMULT_HIGH(d1,q);\n#else\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n#endif\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif ((t2h < rem) ||\n\t\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t\t}\n#endif\n\t\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\twnum.d--;\n\t\tif (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1))\n\t\t\t{\n\t\t\tq--;\n\t\t\tif (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n\t\t\t\t(*wnump)++;\n\t\t\t}\n\t\t*resp = q;\n\t\t}\n\tbn_correct_top(snum);\n\tif (rm != NULL)\n\t\t{\n\t\tint neg = num->neg;\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\tif (!BN_is_zero(rm))\n\t\t\trm->neg = neg;\n\t\tbn_check_top(rm);\n\t\t}\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'int BN_div_no_branch(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,\n\tconst BIGNUM *divisor, BN_CTX *ctx)\n\t{\n\tint norm_shift,i,loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tint num_n,div_n;\n\tbn_check_top(dv);\n\tbn_check_top(rm);\n\tbn_check_top(num);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV_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}'] |
522 | 0 | https://github.com/libav/libav/blob/6cecd63005b29a1dc3a5104e6ac85fd112705122/libavformat/movenc.c/#L820 | static int mov_write_ctts_tag(ByteIOContext *pb, MOVTrack *track)
{
MOVStts *ctts_entries;
uint32_t entries = 0;
uint32_t atom_size;
int i;
ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries));
ctts_entries[0].count = 1;
ctts_entries[0].duration = track->cluster[0].cts;
for (i=1; i<track->entry; i++) {
if (track->cluster[i].cts == ctts_entries[entries].duration) {
ctts_entries[entries].count++;
} else {
entries++;
ctts_entries[entries].duration = track->cluster[i].cts;
ctts_entries[entries].count = 1;
}
}
entries++;
atom_size = 16 + (entries * 8);
put_be32(pb, atom_size);
put_tag(pb, "ctts");
put_be32(pb, 0);
put_be32(pb, entries);
for (i=0; i<entries; i++) {
put_be32(pb, ctts_entries[i].count);
put_be32(pb, ctts_entries[i].duration);
}
av_free(ctts_entries);
return atom_size;
} | ['static int mov_write_ctts_tag(ByteIOContext *pb, MOVTrack *track)\n{\n MOVStts *ctts_entries;\n uint32_t entries = 0;\n uint32_t atom_size;\n int i;\n ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries));\n ctts_entries[0].count = 1;\n ctts_entries[0].duration = track->cluster[0].cts;\n for (i=1; i<track->entry; i++) {\n if (track->cluster[i].cts == ctts_entries[entries].duration) {\n ctts_entries[entries].count++;\n } else {\n entries++;\n ctts_entries[entries].duration = track->cluster[i].cts;\n ctts_entries[entries].count = 1;\n }\n }\n entries++;\n atom_size = 16 + (entries * 8);\n put_be32(pb, atom_size);\n put_tag(pb, "ctts");\n put_be32(pb, 0);\n put_be32(pb, entries);\n for (i=0; i<entries; i++) {\n put_be32(pb, ctts_entries[i].count);\n put_be32(pb, ctts_entries[i].duration);\n }\n av_free(ctts_entries);\n return atom_size;\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}'] |
523 | 0 | https://github.com/libav/libav/blob/c41b9842ceac42bedfd74a7ba2d02add82f818a9/libavcodec/rv40.c/#L487 | static void rv40_loop_filter(RV34DecContext *r, int row)
{
MpegEncContext *s = &r->s;
int mb_pos, mb_x;
int i, j, k;
uint8_t *Y, *C;
int alpha, beta, betaY, betaC;
int q;
int mbtype[4];
int mb_strong[4];
int clip[4];
int cbp[4];
int uvcbp[4][2];
int mvmasks[4];
mb_pos = row * s->mb_stride;
for(mb_x = 0; mb_x < s->mb_width; mb_x++, mb_pos++){
int mbtype = s->current_picture_ptr->f.mb_type[mb_pos];
if(IS_INTRA(mbtype) || IS_SEPARATE_DC(mbtype))
r->cbp_luma [mb_pos] = r->deblock_coefs[mb_pos] = 0xFFFF;
if(IS_INTRA(mbtype))
r->cbp_chroma[mb_pos] = 0xFF;
}
mb_pos = row * s->mb_stride;
for(mb_x = 0; mb_x < s->mb_width; mb_x++, mb_pos++){
int y_h_deblock, y_v_deblock;
int c_v_deblock[2], c_h_deblock[2];
int clip_left;
int avail[4];
int y_to_deblock, c_to_deblock[2];
q = s->current_picture_ptr->f.qscale_table[mb_pos];
alpha = rv40_alpha_tab[q];
beta = rv40_beta_tab [q];
betaY = betaC = beta * 3;
if(s->width * s->height <= 176*144)
betaY += beta;
avail[0] = 1;
avail[1] = row;
avail[2] = mb_x;
avail[3] = row < s->mb_height - 1;
for(i = 0; i < 4; i++){
if(avail[i]){
int pos = mb_pos + neighbour_offs_x[i] + neighbour_offs_y[i]*s->mb_stride;
mvmasks[i] = r->deblock_coefs[pos];
mbtype [i] = s->current_picture_ptr->f.mb_type[pos];
cbp [i] = r->cbp_luma[pos];
uvcbp[i][0] = r->cbp_chroma[pos] & 0xF;
uvcbp[i][1] = r->cbp_chroma[pos] >> 4;
}else{
mvmasks[i] = 0;
mbtype [i] = mbtype[0];
cbp [i] = 0;
uvcbp[i][0] = uvcbp[i][1] = 0;
}
mb_strong[i] = IS_INTRA(mbtype[i]) || IS_SEPARATE_DC(mbtype[i]);
clip[i] = rv40_filter_clip_tbl[mb_strong[i] + 1][q];
}
y_to_deblock = mvmasks[POS_CUR]
| (mvmasks[POS_BOTTOM] << 16);
y_h_deblock = y_to_deblock
| ((cbp[POS_CUR] << 4) & ~MASK_Y_TOP_ROW)
| ((cbp[POS_TOP] & MASK_Y_LAST_ROW) >> 12);
y_v_deblock = y_to_deblock
| ((cbp[POS_CUR] << 1) & ~MASK_Y_LEFT_COL)
| ((cbp[POS_LEFT] & MASK_Y_RIGHT_COL) >> 3);
if(!mb_x)
y_v_deblock &= ~MASK_Y_LEFT_COL;
if(!row)
y_h_deblock &= ~MASK_Y_TOP_ROW;
if(row == s->mb_height - 1 || (mb_strong[POS_CUR] || mb_strong[POS_BOTTOM]))
y_h_deblock &= ~(MASK_Y_TOP_ROW << 16);
for(i = 0; i < 2; i++){
c_to_deblock[i] = (uvcbp[POS_BOTTOM][i] << 4) | uvcbp[POS_CUR][i];
c_v_deblock[i] = c_to_deblock[i]
| ((uvcbp[POS_CUR] [i] << 1) & ~MASK_C_LEFT_COL)
| ((uvcbp[POS_LEFT][i] & MASK_C_RIGHT_COL) >> 1);
c_h_deblock[i] = c_to_deblock[i]
| ((uvcbp[POS_TOP][i] & MASK_C_LAST_ROW) >> 2)
| (uvcbp[POS_CUR][i] << 2);
if(!mb_x)
c_v_deblock[i] &= ~MASK_C_LEFT_COL;
if(!row)
c_h_deblock[i] &= ~MASK_C_TOP_ROW;
if(row == s->mb_height - 1 || mb_strong[POS_CUR] || mb_strong[POS_BOTTOM])
c_h_deblock[i] &= ~(MASK_C_TOP_ROW << 4);
}
for(j = 0; j < 16; j += 4){
Y = s->current_picture_ptr->f.data[0] + mb_x*16 + (row*16 + j) * s->linesize;
for(i = 0; i < 4; i++, Y += 4){
int ij = i + j;
int clip_cur = y_to_deblock & (MASK_CUR << ij) ? clip[POS_CUR] : 0;
int dither = j ? ij : i*4;
if(y_h_deblock & (MASK_BOTTOM << ij)){
rv40_adaptive_loop_filter(&r->rdsp, Y+4*s->linesize,
s->linesize, dither,
y_to_deblock & (MASK_BOTTOM << ij) ? clip[POS_CUR] : 0,
clip_cur, alpha, beta, betaY,
0, 0, 0);
}
if(y_v_deblock & (MASK_CUR << ij) && (i || !(mb_strong[POS_CUR] || mb_strong[POS_LEFT]))){
if(!i)
clip_left = mvmasks[POS_LEFT] & (MASK_RIGHT << j) ? clip[POS_LEFT] : 0;
else
clip_left = y_to_deblock & (MASK_CUR << (ij-1)) ? clip[POS_CUR] : 0;
rv40_adaptive_loop_filter(&r->rdsp, Y, s->linesize, dither,
clip_cur,
clip_left,
alpha, beta, betaY, 0, 0, 1);
}
if(!j && y_h_deblock & (MASK_CUR << i) && (mb_strong[POS_CUR] || mb_strong[POS_TOP])){
rv40_adaptive_loop_filter(&r->rdsp, Y, s->linesize, dither,
clip_cur,
mvmasks[POS_TOP] & (MASK_TOP << i) ? clip[POS_TOP] : 0,
alpha, beta, betaY, 0, 1, 0);
}
if(y_v_deblock & (MASK_CUR << ij) && !i && (mb_strong[POS_CUR] || mb_strong[POS_LEFT])){
clip_left = mvmasks[POS_LEFT] & (MASK_RIGHT << j) ? clip[POS_LEFT] : 0;
rv40_adaptive_loop_filter(&r->rdsp, Y, s->linesize, dither,
clip_cur,
clip_left,
alpha, beta, betaY, 0, 1, 1);
}
}
}
for(k = 0; k < 2; k++){
for(j = 0; j < 2; j++){
C = s->current_picture_ptr->f.data[k + 1] + mb_x*8 + (row*8 + j*4) * s->uvlinesize;
for(i = 0; i < 2; i++, C += 4){
int ij = i + j*2;
int clip_cur = c_to_deblock[k] & (MASK_CUR << ij) ? clip[POS_CUR] : 0;
if(c_h_deblock[k] & (MASK_CUR << (ij+2))){
int clip_bot = c_to_deblock[k] & (MASK_CUR << (ij+2)) ? clip[POS_CUR] : 0;
rv40_adaptive_loop_filter(&r->rdsp, C+4*s->uvlinesize, s->uvlinesize, i*8,
clip_bot,
clip_cur,
alpha, beta, betaC, 1, 0, 0);
}
if((c_v_deblock[k] & (MASK_CUR << ij)) && (i || !(mb_strong[POS_CUR] || mb_strong[POS_LEFT]))){
if(!i)
clip_left = uvcbp[POS_LEFT][k] & (MASK_CUR << (2*j+1)) ? clip[POS_LEFT] : 0;
else
clip_left = c_to_deblock[k] & (MASK_CUR << (ij-1)) ? clip[POS_CUR] : 0;
rv40_adaptive_loop_filter(&r->rdsp, C, s->uvlinesize, j*8,
clip_cur,
clip_left,
alpha, beta, betaC, 1, 0, 1);
}
if(!j && c_h_deblock[k] & (MASK_CUR << ij) && (mb_strong[POS_CUR] || mb_strong[POS_TOP])){
int clip_top = uvcbp[POS_TOP][k] & (MASK_CUR << (ij+2)) ? clip[POS_TOP] : 0;
rv40_adaptive_loop_filter(&r->rdsp, C, s->uvlinesize, i*8,
clip_cur,
clip_top,
alpha, beta, betaC, 1, 1, 0);
}
if(c_v_deblock[k] & (MASK_CUR << ij) && !i && (mb_strong[POS_CUR] || mb_strong[POS_LEFT])){
clip_left = uvcbp[POS_LEFT][k] & (MASK_CUR << (2*j+1)) ? clip[POS_LEFT] : 0;
rv40_adaptive_loop_filter(&r->rdsp, C, s->uvlinesize, j*8,
clip_cur,
clip_left,
alpha, beta, betaC, 1, 1, 1);
}
}
}
}
}
} | ['static void rv40_loop_filter(RV34DecContext *r, int row)\n{\n MpegEncContext *s = &r->s;\n int mb_pos, mb_x;\n int i, j, k;\n uint8_t *Y, *C;\n int alpha, beta, betaY, betaC;\n int q;\n int mbtype[4];\n int mb_strong[4];\n int clip[4];\n int cbp[4];\n int uvcbp[4][2];\n int mvmasks[4];\n mb_pos = row * s->mb_stride;\n for(mb_x = 0; mb_x < s->mb_width; mb_x++, mb_pos++){\n int mbtype = s->current_picture_ptr->f.mb_type[mb_pos];\n if(IS_INTRA(mbtype) || IS_SEPARATE_DC(mbtype))\n r->cbp_luma [mb_pos] = r->deblock_coefs[mb_pos] = 0xFFFF;\n if(IS_INTRA(mbtype))\n r->cbp_chroma[mb_pos] = 0xFF;\n }\n mb_pos = row * s->mb_stride;\n for(mb_x = 0; mb_x < s->mb_width; mb_x++, mb_pos++){\n int y_h_deblock, y_v_deblock;\n int c_v_deblock[2], c_h_deblock[2];\n int clip_left;\n int avail[4];\n int y_to_deblock, c_to_deblock[2];\n q = s->current_picture_ptr->f.qscale_table[mb_pos];\n alpha = rv40_alpha_tab[q];\n beta = rv40_beta_tab [q];\n betaY = betaC = beta * 3;\n if(s->width * s->height <= 176*144)\n betaY += beta;\n avail[0] = 1;\n avail[1] = row;\n avail[2] = mb_x;\n avail[3] = row < s->mb_height - 1;\n for(i = 0; i < 4; i++){\n if(avail[i]){\n int pos = mb_pos + neighbour_offs_x[i] + neighbour_offs_y[i]*s->mb_stride;\n mvmasks[i] = r->deblock_coefs[pos];\n mbtype [i] = s->current_picture_ptr->f.mb_type[pos];\n cbp [i] = r->cbp_luma[pos];\n uvcbp[i][0] = r->cbp_chroma[pos] & 0xF;\n uvcbp[i][1] = r->cbp_chroma[pos] >> 4;\n }else{\n mvmasks[i] = 0;\n mbtype [i] = mbtype[0];\n cbp [i] = 0;\n uvcbp[i][0] = uvcbp[i][1] = 0;\n }\n mb_strong[i] = IS_INTRA(mbtype[i]) || IS_SEPARATE_DC(mbtype[i]);\n clip[i] = rv40_filter_clip_tbl[mb_strong[i] + 1][q];\n }\n y_to_deblock = mvmasks[POS_CUR]\n | (mvmasks[POS_BOTTOM] << 16);\n y_h_deblock = y_to_deblock\n | ((cbp[POS_CUR] << 4) & ~MASK_Y_TOP_ROW)\n | ((cbp[POS_TOP] & MASK_Y_LAST_ROW) >> 12);\n y_v_deblock = y_to_deblock\n | ((cbp[POS_CUR] << 1) & ~MASK_Y_LEFT_COL)\n | ((cbp[POS_LEFT] & MASK_Y_RIGHT_COL) >> 3);\n if(!mb_x)\n y_v_deblock &= ~MASK_Y_LEFT_COL;\n if(!row)\n y_h_deblock &= ~MASK_Y_TOP_ROW;\n if(row == s->mb_height - 1 || (mb_strong[POS_CUR] || mb_strong[POS_BOTTOM]))\n y_h_deblock &= ~(MASK_Y_TOP_ROW << 16);\n for(i = 0; i < 2; i++){\n c_to_deblock[i] = (uvcbp[POS_BOTTOM][i] << 4) | uvcbp[POS_CUR][i];\n c_v_deblock[i] = c_to_deblock[i]\n | ((uvcbp[POS_CUR] [i] << 1) & ~MASK_C_LEFT_COL)\n | ((uvcbp[POS_LEFT][i] & MASK_C_RIGHT_COL) >> 1);\n c_h_deblock[i] = c_to_deblock[i]\n | ((uvcbp[POS_TOP][i] & MASK_C_LAST_ROW) >> 2)\n | (uvcbp[POS_CUR][i] << 2);\n if(!mb_x)\n c_v_deblock[i] &= ~MASK_C_LEFT_COL;\n if(!row)\n c_h_deblock[i] &= ~MASK_C_TOP_ROW;\n if(row == s->mb_height - 1 || mb_strong[POS_CUR] || mb_strong[POS_BOTTOM])\n c_h_deblock[i] &= ~(MASK_C_TOP_ROW << 4);\n }\n for(j = 0; j < 16; j += 4){\n Y = s->current_picture_ptr->f.data[0] + mb_x*16 + (row*16 + j) * s->linesize;\n for(i = 0; i < 4; i++, Y += 4){\n int ij = i + j;\n int clip_cur = y_to_deblock & (MASK_CUR << ij) ? clip[POS_CUR] : 0;\n int dither = j ? ij : i*4;\n if(y_h_deblock & (MASK_BOTTOM << ij)){\n rv40_adaptive_loop_filter(&r->rdsp, Y+4*s->linesize,\n s->linesize, dither,\n y_to_deblock & (MASK_BOTTOM << ij) ? clip[POS_CUR] : 0,\n clip_cur, alpha, beta, betaY,\n 0, 0, 0);\n }\n if(y_v_deblock & (MASK_CUR << ij) && (i || !(mb_strong[POS_CUR] || mb_strong[POS_LEFT]))){\n if(!i)\n clip_left = mvmasks[POS_LEFT] & (MASK_RIGHT << j) ? clip[POS_LEFT] : 0;\n else\n clip_left = y_to_deblock & (MASK_CUR << (ij-1)) ? clip[POS_CUR] : 0;\n rv40_adaptive_loop_filter(&r->rdsp, Y, s->linesize, dither,\n clip_cur,\n clip_left,\n alpha, beta, betaY, 0, 0, 1);\n }\n if(!j && y_h_deblock & (MASK_CUR << i) && (mb_strong[POS_CUR] || mb_strong[POS_TOP])){\n rv40_adaptive_loop_filter(&r->rdsp, Y, s->linesize, dither,\n clip_cur,\n mvmasks[POS_TOP] & (MASK_TOP << i) ? clip[POS_TOP] : 0,\n alpha, beta, betaY, 0, 1, 0);\n }\n if(y_v_deblock & (MASK_CUR << ij) && !i && (mb_strong[POS_CUR] || mb_strong[POS_LEFT])){\n clip_left = mvmasks[POS_LEFT] & (MASK_RIGHT << j) ? clip[POS_LEFT] : 0;\n rv40_adaptive_loop_filter(&r->rdsp, Y, s->linesize, dither,\n clip_cur,\n clip_left,\n alpha, beta, betaY, 0, 1, 1);\n }\n }\n }\n for(k = 0; k < 2; k++){\n for(j = 0; j < 2; j++){\n C = s->current_picture_ptr->f.data[k + 1] + mb_x*8 + (row*8 + j*4) * s->uvlinesize;\n for(i = 0; i < 2; i++, C += 4){\n int ij = i + j*2;\n int clip_cur = c_to_deblock[k] & (MASK_CUR << ij) ? clip[POS_CUR] : 0;\n if(c_h_deblock[k] & (MASK_CUR << (ij+2))){\n int clip_bot = c_to_deblock[k] & (MASK_CUR << (ij+2)) ? clip[POS_CUR] : 0;\n rv40_adaptive_loop_filter(&r->rdsp, C+4*s->uvlinesize, s->uvlinesize, i*8,\n clip_bot,\n clip_cur,\n alpha, beta, betaC, 1, 0, 0);\n }\n if((c_v_deblock[k] & (MASK_CUR << ij)) && (i || !(mb_strong[POS_CUR] || mb_strong[POS_LEFT]))){\n if(!i)\n clip_left = uvcbp[POS_LEFT][k] & (MASK_CUR << (2*j+1)) ? clip[POS_LEFT] : 0;\n else\n clip_left = c_to_deblock[k] & (MASK_CUR << (ij-1)) ? clip[POS_CUR] : 0;\n rv40_adaptive_loop_filter(&r->rdsp, C, s->uvlinesize, j*8,\n clip_cur,\n clip_left,\n alpha, beta, betaC, 1, 0, 1);\n }\n if(!j && c_h_deblock[k] & (MASK_CUR << ij) && (mb_strong[POS_CUR] || mb_strong[POS_TOP])){\n int clip_top = uvcbp[POS_TOP][k] & (MASK_CUR << (ij+2)) ? clip[POS_TOP] : 0;\n rv40_adaptive_loop_filter(&r->rdsp, C, s->uvlinesize, i*8,\n clip_cur,\n clip_top,\n alpha, beta, betaC, 1, 1, 0);\n }\n if(c_v_deblock[k] & (MASK_CUR << ij) && !i && (mb_strong[POS_CUR] || mb_strong[POS_LEFT])){\n clip_left = uvcbp[POS_LEFT][k] & (MASK_CUR << (2*j+1)) ? clip[POS_LEFT] : 0;\n rv40_adaptive_loop_filter(&r->rdsp, C, s->uvlinesize, j*8,\n clip_cur,\n clip_left,\n alpha, beta, betaC, 1, 1, 1);\n }\n }\n }\n }\n }\n}'] |
524 | 0 | https://github.com/openssl/openssl/blob/183733f882056ea3e6fe95e665b85fcc6a45dcb4/crypto/conf/conf_def.c/#L246 | static int def_load_bio(CONF *conf, BIO *in, long *line)
{
#define CONFBUFSIZE 512
int bufnum = 0, i, ii;
BUF_MEM *buff = NULL;
char *s, *p, *end;
int again;
long eline = 0;
char btmp[DECIMAL_SIZE(eline) + 1];
CONF_VALUE *v = NULL, *tv;
CONF_VALUE *sv = NULL;
char *section = NULL, *buf;
char *start, *psection, *pname;
void *h = (void *)(conf->data);
if ((buff = BUF_MEM_new()) == NULL) {
CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
goto err;
}
section = OPENSSL_strdup("default");
if (section == NULL) {
CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
goto err;
}
if (_CONF_new_data(conf) == 0) {
CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
goto err;
}
sv = _CONF_new_section(conf, section);
if (sv == NULL) {
CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
goto err;
}
bufnum = 0;
again = 0;
for (;;) {
if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) {
CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
goto err;
}
p = &(buff->data[bufnum]);
*p = '\0';
BIO_gets(in, p, CONFBUFSIZE - 1);
p[CONFBUFSIZE - 1] = '\0';
ii = i = strlen(p);
if (i == 0 && !again)
break;
again = 0;
while (i > 0) {
if ((p[i - 1] != '\r') && (p[i - 1] != '\n'))
break;
else
i--;
}
if (ii && i == ii)
again = 1;
else {
p[i] = '\0';
eline++;
}
bufnum += i;
v = NULL;
if (bufnum >= 1) {
p = &(buff->data[bufnum - 1]);
if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
bufnum--;
again = 1;
}
}
if (again)
continue;
bufnum = 0;
buf = buff->data;
clear_comments(conf, buf);
s = eat_ws(conf, buf);
if (IS_EOF(conf, *s))
continue;
if (*s == '[') {
char *ss;
s++;
start = eat_ws(conf, s);
ss = start;
again:
end = eat_alpha_numeric(conf, ss);
p = eat_ws(conf, end);
if (*p != ']') {
if (*p != '\0' && ss != p) {
ss = p;
goto again;
}
CONFerr(CONF_F_DEF_LOAD_BIO,
CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
goto err;
}
*end = '\0';
if (!str_copy(conf, NULL, §ion, start))
goto err;
if ((sv = _CONF_get_section(conf, section)) == NULL)
sv = _CONF_new_section(conf, section);
if (sv == NULL) {
CONFerr(CONF_F_DEF_LOAD_BIO,
CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
goto err;
}
continue;
} else {
pname = s;
psection = NULL;
end = eat_alpha_numeric(conf, s);
if ((end[0] == ':') && (end[1] == ':')) {
*end = '\0';
end += 2;
psection = pname;
pname = end;
end = eat_alpha_numeric(conf, end);
}
p = eat_ws(conf, end);
if (*p != '=') {
CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_MISSING_EQUAL_SIGN);
goto err;
}
*end = '\0';
p++;
start = eat_ws(conf, p);
while (!IS_EOF(conf, *p))
p++;
p--;
while ((p != start) && (IS_WS(conf, *p)))
p--;
p++;
*p = '\0';
if ((v = OPENSSL_malloc(sizeof(*v))) == NULL) {
CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
goto err;
}
if (psection == NULL)
psection = section;
v->name = OPENSSL_malloc(strlen(pname) + 1);
v->value = NULL;
if (v->name == NULL) {
CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
goto err;
}
OPENSSL_strlcpy(v->name, pname, strlen(pname) + 1);
if (!str_copy(conf, psection, &(v->value), start))
goto err;
if (strcmp(psection, section) != 0) {
if ((tv = _CONF_get_section(conf, psection))
== NULL)
tv = _CONF_new_section(conf, psection);
if (tv == NULL) {
CONFerr(CONF_F_DEF_LOAD_BIO,
CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
goto err;
}
} else
tv = sv;
if (_CONF_add_string(conf, tv, v) == 0) {
CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
goto err;
}
v = NULL;
}
}
BUF_MEM_free(buff);
OPENSSL_free(section);
return (1);
err:
BUF_MEM_free(buff);
OPENSSL_free(section);
if (line != NULL)
*line = eline;
BIO_snprintf(btmp, sizeof btmp, "%ld", eline);
ERR_add_error_data(2, "line ", btmp);
if (h != conf->data) {
CONF_free(conf->data);
conf->data = NULL;
}
if (v != NULL) {
OPENSSL_free(v->name);
OPENSSL_free(v->value);
OPENSSL_free(v);
}
return (0);
} | ['static int def_load_bio(CONF *conf, BIO *in, long *line)\n{\n#define CONFBUFSIZE 512\n int bufnum = 0, i, ii;\n BUF_MEM *buff = NULL;\n char *s, *p, *end;\n int again;\n long eline = 0;\n char btmp[DECIMAL_SIZE(eline) + 1];\n CONF_VALUE *v = NULL, *tv;\n CONF_VALUE *sv = NULL;\n char *section = NULL, *buf;\n char *start, *psection, *pname;\n void *h = (void *)(conf->data);\n if ((buff = BUF_MEM_new()) == NULL) {\n CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);\n goto err;\n }\n section = OPENSSL_strdup("default");\n if (section == NULL) {\n CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (_CONF_new_data(conf) == 0) {\n CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n sv = _CONF_new_section(conf, section);\n if (sv == NULL) {\n CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);\n goto err;\n }\n bufnum = 0;\n again = 0;\n for (;;) {\n if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) {\n CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);\n goto err;\n }\n p = &(buff->data[bufnum]);\n *p = \'\\0\';\n BIO_gets(in, p, CONFBUFSIZE - 1);\n p[CONFBUFSIZE - 1] = \'\\0\';\n ii = i = strlen(p);\n if (i == 0 && !again)\n break;\n again = 0;\n while (i > 0) {\n if ((p[i - 1] != \'\\r\') && (p[i - 1] != \'\\n\'))\n break;\n else\n i--;\n }\n if (ii && i == ii)\n again = 1;\n else {\n p[i] = \'\\0\';\n eline++;\n }\n bufnum += i;\n v = NULL;\n if (bufnum >= 1) {\n p = &(buff->data[bufnum - 1]);\n if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {\n bufnum--;\n again = 1;\n }\n }\n if (again)\n continue;\n bufnum = 0;\n buf = buff->data;\n clear_comments(conf, buf);\n s = eat_ws(conf, buf);\n if (IS_EOF(conf, *s))\n continue;\n if (*s == \'[\') {\n char *ss;\n s++;\n start = eat_ws(conf, s);\n ss = start;\n again:\n end = eat_alpha_numeric(conf, ss);\n p = eat_ws(conf, end);\n if (*p != \']\') {\n if (*p != \'\\0\' && ss != p) {\n ss = p;\n goto again;\n }\n CONFerr(CONF_F_DEF_LOAD_BIO,\n CONF_R_MISSING_CLOSE_SQUARE_BRACKET);\n goto err;\n }\n *end = \'\\0\';\n if (!str_copy(conf, NULL, §ion, start))\n goto err;\n if ((sv = _CONF_get_section(conf, section)) == NULL)\n sv = _CONF_new_section(conf, section);\n if (sv == NULL) {\n CONFerr(CONF_F_DEF_LOAD_BIO,\n CONF_R_UNABLE_TO_CREATE_NEW_SECTION);\n goto err;\n }\n continue;\n } else {\n pname = s;\n psection = NULL;\n end = eat_alpha_numeric(conf, s);\n if ((end[0] == \':\') && (end[1] == \':\')) {\n *end = \'\\0\';\n end += 2;\n psection = pname;\n pname = end;\n end = eat_alpha_numeric(conf, end);\n }\n p = eat_ws(conf, end);\n if (*p != \'=\') {\n CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_MISSING_EQUAL_SIGN);\n goto err;\n }\n *end = \'\\0\';\n p++;\n start = eat_ws(conf, p);\n while (!IS_EOF(conf, *p))\n p++;\n p--;\n while ((p != start) && (IS_WS(conf, *p)))\n p--;\n p++;\n *p = \'\\0\';\n if ((v = OPENSSL_malloc(sizeof(*v))) == NULL) {\n CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (psection == NULL)\n psection = section;\n v->name = OPENSSL_malloc(strlen(pname) + 1);\n v->value = NULL;\n if (v->name == NULL) {\n CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n OPENSSL_strlcpy(v->name, pname, strlen(pname) + 1);\n if (!str_copy(conf, psection, &(v->value), start))\n goto err;\n if (strcmp(psection, section) != 0) {\n if ((tv = _CONF_get_section(conf, psection))\n == NULL)\n tv = _CONF_new_section(conf, psection);\n if (tv == NULL) {\n CONFerr(CONF_F_DEF_LOAD_BIO,\n CONF_R_UNABLE_TO_CREATE_NEW_SECTION);\n goto err;\n }\n } else\n tv = sv;\n if (_CONF_add_string(conf, tv, v) == 0) {\n CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n v = NULL;\n }\n }\n BUF_MEM_free(buff);\n OPENSSL_free(section);\n return (1);\n err:\n BUF_MEM_free(buff);\n OPENSSL_free(section);\n if (line != NULL)\n *line = eline;\n BIO_snprintf(btmp, sizeof btmp, "%ld", eline);\n ERR_add_error_data(2, "line ", btmp);\n if (h != conf->data) {\n CONF_free(conf->data);\n conf->data = NULL;\n }\n if (v != NULL) {\n OPENSSL_free(v->name);\n OPENSSL_free(v->value);\n OPENSSL_free(v);\n }\n return (0);\n}', 'BUF_MEM *BUF_MEM_new(void)\n{\n BUF_MEM *ret;\n ret = OPENSSL_zalloc(sizeof(*ret));\n if (ret == NULL) {\n BUFerr(BUF_F_BUF_MEM_NEW, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\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}', 'char *CRYPTO_strdup(const char *str, const char* file, int line)\n{\n char *ret;\n size_t size;\n if (str == NULL)\n return NULL;\n size = strlen(str) + 1;\n ret = CRYPTO_malloc(size, file, line);\n if (ret != NULL)\n memcpy(ret, str, size);\n return ret;\n}', 'int _CONF_new_data(CONF *conf)\n{\n if (conf == NULL) {\n return 0;\n }\n if (conf->data == NULL) {\n conf->data = lh_CONF_VALUE_new(conf_value_hash, conf_value_cmp);\n if (conf->data == NULL)\n return 0;\n }\n return 1;\n}', 'CONF_VALUE *_CONF_new_section(CONF *conf, const char *section)\n{\n STACK_OF(CONF_VALUE) *sk = NULL;\n int i;\n CONF_VALUE *v = NULL, *vv;\n if ((sk = sk_CONF_VALUE_new_null()) == NULL)\n goto err;\n if ((v = OPENSSL_malloc(sizeof(*v))) == NULL)\n goto err;\n i = strlen(section) + 1;\n if ((v->section = OPENSSL_malloc(i)) == NULL)\n goto err;\n memcpy(v->section, section, i);\n v->name = NULL;\n v->value = (char *)sk;\n vv = lh_CONF_VALUE_insert(conf->data, v);\n OPENSSL_assert(vv == NULL);\n return v;\n err:\n sk_CONF_VALUE_free(sk);\n OPENSSL_free(v);\n return NULL;\n}', '_STACK *sk_new_null(void)\n{\n return sk_new((int (*)(const void *, const void *))0);\n}', '_STACK *sk_new(int (*c) (const void *, const void *))\n{\n _STACK *ret;\n if ((ret = OPENSSL_zalloc(sizeof(_STACK))) == NULL)\n goto err;\n if ((ret->data = OPENSSL_zalloc(sizeof(*ret->data) * MIN_NODES)) == NULL)\n goto err;\n ret->comp = c;\n ret->num_alloc = MIN_NODES;\n return (ret);\n err:\n OPENSSL_free(ret);\n return (NULL);\n}', 'void *lh_insert(_LHASH *lh, void *data)\n{\n unsigned long hash;\n LHASH_NODE *nn, **rn;\n void *ret;\n lh->error = 0;\n if (lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes))\n expand(lh);\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}'] |
525 | 0 | https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/bn/bn_ctx.c/#L328 | static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
} | ['int RSA_check_key_ex(const RSA *key, BN_GENCB *cb)\n{\n BIGNUM *i, *j, *k, *l, *m;\n BN_CTX *ctx;\n int r;\n int ret = 1;\n if (!key->p || !key->q || !key->n || !key->e || !key->d) {\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_VALUE_MISSING);\n return 0;\n }\n i = BN_new();\n j = BN_new();\n k = BN_new();\n l = BN_new();\n m = BN_new();\n ctx = BN_CTX_new();\n if (i == NULL || j == NULL || k == NULL || l == NULL ||\n m == NULL || ctx == NULL) {\n ret = -1;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n r = BN_is_prime_ex(key->p, BN_prime_checks, NULL, cb);\n if (r != 1) {\n ret = r;\n if (r != 0)\n goto err;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_P_NOT_PRIME);\n }\n r = BN_is_prime_ex(key->q, BN_prime_checks, NULL, cb);\n if (r != 1) {\n ret = r;\n if (r != 0)\n goto err;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_Q_NOT_PRIME);\n }\n r = BN_mul(i, key->p, key->q, ctx);\n if (!r) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(i, key->n) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_N_DOES_NOT_EQUAL_P_Q);\n }\n r = BN_sub(i, key->p, BN_value_one());\n if (!r) {\n ret = -1;\n goto err;\n }\n r = BN_sub(j, key->q, BN_value_one());\n if (!r) {\n ret = -1;\n goto err;\n }\n r = BN_mul(l, i, j, ctx);\n if (!r) {\n ret = -1;\n goto err;\n }\n r = BN_gcd(m, i, j, ctx);\n if (!r) {\n ret = -1;\n goto err;\n }\n r = BN_div(k, NULL, l, m, ctx);\n if (!r) {\n ret = -1;\n goto err;\n }\n r = BN_mod_mul(i, key->d, key->e, k, ctx);\n if (!r) {\n ret = -1;\n goto err;\n }\n if (!BN_is_one(i)) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_D_E_NOT_CONGRUENT_TO_1);\n }\n if (key->dmp1 != NULL && key->dmq1 != NULL && key->iqmp != NULL) {\n r = BN_sub(i, key->p, BN_value_one());\n if (!r) {\n ret = -1;\n goto err;\n }\n r = BN_mod(j, key->d, i, ctx);\n if (!r) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(j, key->dmp1) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_DMP1_NOT_CONGRUENT_TO_D);\n }\n r = BN_sub(i, key->q, BN_value_one());\n if (!r) {\n ret = -1;\n goto err;\n }\n r = BN_mod(j, key->d, i, ctx);\n if (!r) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(j, key->dmq1) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_DMQ1_NOT_CONGRUENT_TO_D);\n }\n if (!BN_mod_inverse(i, key->q, key->p, ctx)) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(i, key->iqmp) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_IQMP_NOT_INVERSE_OF_Q);\n }\n }\n err:\n BN_free(i);\n BN_free(j);\n BN_free(k);\n BN_free(l);\n BN_free(m);\n BN_CTX_free(ctx);\n return (ret);\n}', 'int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = 0;\n int top, al, bl;\n BIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n int i;\n#endif\n#ifdef BN_RECURSION\n BIGNUM *t = NULL;\n int j = 0, k;\n#endif\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(r);\n al = a->top;\n bl = b->top;\n if ((al == 0) || (bl == 0)) {\n BN_zero(r);\n return (1);\n }\n top = al + bl;\n BN_CTX_start(ctx);\n if ((r == a) || (r == b)) {\n if ((rr = BN_CTX_get(ctx)) == NULL)\n goto err;\n } else\n rr = r;\n rr->neg = a->neg ^ b->neg;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n i = al - bl;\n#endif\n#ifdef BN_MUL_COMBA\n if (i == 0) {\n# if 0\n if (al == 4) {\n if (bn_wexpand(rr, 8) == NULL)\n goto err;\n rr->top = 8;\n bn_mul_comba4(rr->d, a->d, b->d);\n goto end;\n }\n# endif\n if (al == 8) {\n if (bn_wexpand(rr, 16) == NULL)\n goto err;\n rr->top = 16;\n bn_mul_comba8(rr->d, a->d, b->d);\n goto end;\n }\n }\n#endif\n#ifdef BN_RECURSION\n if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {\n if (i >= -1 && i <= 1) {\n if (i >= 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n }\n if (i == -1) {\n j = BN_num_bits_word((BN_ULONG)bl);\n }\n j = 1 << (j - 1);\n assert(j <= al || j <= bl);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (al > j || bl > j) {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d,\n j, al - j, bl - j, t->d);\n } else {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);\n }\n rr->top = top;\n goto end;\n }\n# if 0\n if (i == 1 && !BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BIGNUM *tmp_bn = (BIGNUM *)b;\n if (bn_wexpand(tmp_bn, al) == NULL)\n goto err;\n tmp_bn->d[bl] = 0;\n bl++;\n i--;\n } else if (i == -1 && !BN_get_flags(a, BN_FLG_STATIC_DATA)) {\n BIGNUM *tmp_bn = (BIGNUM *)a;\n if (bn_wexpand(tmp_bn, bl) == NULL)\n goto err;\n tmp_bn->d[al] = 0;\n al++;\n i++;\n }\n if (i == 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (al == j) {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, al, t->d);\n } else {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d, al - j, j, t->d);\n }\n rr->top = top;\n goto end;\n }\n# endif\n }\n#endif\n if (bn_wexpand(rr, top) == NULL)\n goto err;\n rr->top = top;\n bn_mul_normal(rr->d, a->d, al, b->d, bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n end:\n#endif\n bn_correct_top(rr);\n if (r != rr)\n BN_copy(r, rr);\n ret = 1;\n err:\n bn_check_top(r);\n BN_CTX_end(ctx);\n return (ret);\n}', 'int BN_mod_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_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int max, al;\n int ret = 0;\n BIGNUM *tmp, *rr;\n bn_check_top(a);\n al = a->top;\n if (al <= 0) {\n r->top = 0;\n r->neg = 0;\n return 1;\n }\n BN_CTX_start(ctx);\n rr = (a != r) ? r : BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (!rr || !tmp)\n goto err;\n max = 2 * al;\n if (bn_wexpand(rr, max) == NULL)\n goto err;\n if (al == 4) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[8];\n bn_sqr_normal(rr->d, a->d, 4, t);\n#else\n bn_sqr_comba4(rr->d, a->d);\n#endif\n } else if (al == 8) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[16];\n bn_sqr_normal(rr->d, a->d, 8, t);\n#else\n bn_sqr_comba8(rr->d, a->d);\n#endif\n } else {\n#if defined(BN_RECURSION)\n if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {\n BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];\n bn_sqr_normal(rr->d, a->d, al, t);\n } else {\n int j, k;\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n if (al == j) {\n if (bn_wexpand(tmp, k * 2) == NULL)\n goto err;\n bn_sqr_recursive(rr->d, a->d, al, tmp->d);\n } else {\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n }\n }\n#else\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n#endif\n }\n rr->neg = 0;\n if (a->d[al - 1] == (a->d[al - 1] & BN_MASK2l))\n rr->top = max - 1;\n else\n rr->top = max;\n if (rr != r)\n BN_copy(r, rr);\n ret = 1;\n err:\n bn_check_top(rr);\n bn_check_top(tmp);\n BN_CTX_end(ctx);\n return (ret);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}'] |
526 | 0 | https://github.com/openssl/openssl/blob/cd353c7768e7f1dfdfe369be900666241ddedefb/crypto/rand/rand_lib.c/#L857 | int RAND_status(void)
{
const RAND_METHOD *meth = RAND_get_rand_method();
if (meth->status != NULL)
return meth->status();
return 0;
} | ['int RAND_status(void)\n{\n const RAND_METHOD *meth = RAND_get_rand_method();\n if (meth->status != NULL)\n return meth->status();\n return 0;\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}'] |
527 | 0 | https://github.com/openssl/openssl/blob/34216c04229ffaa564adb204cea87bc6b5ed4fb1/crypto/x509/x509_lu.c/#L304 | int X509_STORE_get_by_subject(X509_STORE_CTX *vs, int type, X509_NAME *name,
X509_OBJECT *ret)
{
X509_STORE *ctx=vs->ctx;
X509_LOOKUP *lu;
X509_OBJECT stmp,*tmp;
int i,j;
tmp=X509_OBJECT_retrieve_by_subject(ctx->certs,type,name);
if (tmp == NULL)
{
for (i=vs->current_method; i<sk_X509_LOOKUP_num(ctx->get_cert_methods); i++)
{
lu=sk_X509_LOOKUP_value(ctx->get_cert_methods,i);
j=X509_LOOKUP_by_subject(lu,type,name,&stmp);
if (j < 0)
{
vs->current_method=j;
return(j);
}
else if (j)
{
tmp= &stmp;
break;
}
}
vs->current_method=0;
if (tmp == NULL)
return(0);
}
ret->type=tmp->type;
ret->data.ptr=tmp->data.ptr;
X509_OBJECT_up_ref_count(ret);
return(1);
} | ['int X509_STORE_get_by_subject(X509_STORE_CTX *vs, int type, X509_NAME *name,\n\t X509_OBJECT *ret)\n\t{\n\tX509_STORE *ctx=vs->ctx;\n\tX509_LOOKUP *lu;\n\tX509_OBJECT stmp,*tmp;\n\tint i,j;\n\ttmp=X509_OBJECT_retrieve_by_subject(ctx->certs,type,name);\n\tif (tmp == NULL)\n\t\t{\n\t\tfor (i=vs->current_method; i<sk_X509_LOOKUP_num(ctx->get_cert_methods); i++)\n\t\t\t{\n\t\t\tlu=sk_X509_LOOKUP_value(ctx->get_cert_methods,i);\n\t\t\tj=X509_LOOKUP_by_subject(lu,type,name,&stmp);\n\t\t\tif (j < 0)\n\t\t\t\t{\n\t\t\t\tvs->current_method=j;\n\t\t\t\treturn(j);\n\t\t\t\t}\n\t\t\telse if (j)\n\t\t\t\t{\n\t\t\t\ttmp= &stmp;\n\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\tvs->current_method=0;\n\t\tif (tmp == NULL)\n\t\t\treturn(0);\n\t\t}\n\tret->type=tmp->type;\n\tret->data.ptr=tmp->data.ptr;\n\tX509_OBJECT_up_ref_count(ret);\n\treturn(1);\n\t}', 'X509_OBJECT *X509_OBJECT_retrieve_by_subject(LHASH *h, int type,\n\t X509_NAME *name)\n\t{\n\tX509_OBJECT stmp,*tmp;\n\tX509 x509_s;\n\tX509_CINF cinf_s;\n\tX509_CRL crl_s;\n\tX509_CRL_INFO crl_info_s;\n\tstmp.type=type;\n\tswitch (type)\n\t\t{\n\tcase X509_LU_X509:\n\t\tstmp.data.x509= &x509_s;\n\t\tx509_s.cert_info= &cinf_s;\n\t\tcinf_s.subject=name;\n\t\tbreak;\n\tcase X509_LU_CRL:\n\t\tstmp.data.crl= &crl_s;\n\t\tcrl_s.crl= &crl_info_s;\n\t\tcrl_info_s.issuer=name;\n\t\tbreak;\n\tdefault:\n\t\treturn NULL;\n\t\t}\n\ttmp=(X509_OBJECT *)lh_retrieve(h,&stmp);\n\treturn(tmp);\n\t}', 'int sk_num(const STACK *st)\n{\n\tif(st == NULL) return -1;\n\treturn st->num;\n}', 'char *sk_value(const STACK *st, int i)\n{\n\tif(st == NULL) return NULL;\n\treturn st->data[i];\n}', 'int X509_LOOKUP_by_subject(X509_LOOKUP *ctx, int type, X509_NAME *name,\n\t X509_OBJECT *ret)\n\t{\n\tif ((ctx->method == NULL) || (ctx->method->get_by_subject == NULL))\n\t\treturn(X509_LU_FAIL);\n\tif (ctx->skip) return(0);\n\treturn(ctx->method->get_by_subject(ctx,type,name,ret));\n\t}'] |
528 | 0 | https://github.com/openssl/openssl/blob/23dd0c9f8dc6f7edf4b872d13e5644dfbbee585b/crypto/err/err.c/#L398 | 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}'] |
529 | 0 | https://github.com/openssl/openssl/blob/5d1c09de1f2736e1d4b1877206d08455ec75f558/crypto/bn/bn_lib.c/#L291 | BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
{
bn_check_top(b);
if (a == b)
return a;
if (bn_wexpand(a, b->top) == NULL)
return NULL;
if (b->top > 0)
memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);
a->neg = b->neg;
a->top = b->top;
a->flags |= b->flags & BN_FLG_FIXED_TOP;
bn_check_top(a);
return a;
} | ['BIGNUM *SRP_Calc_client_key(const BIGNUM *N, const BIGNUM *B, const BIGNUM *g,\n const BIGNUM *x, const BIGNUM *a, const BIGNUM *u)\n{\n BIGNUM *tmp = NULL, *tmp2 = NULL, *tmp3 = NULL, *k = NULL, *K = NULL;\n BN_CTX *bn_ctx;\n if (u == NULL || B == NULL || N == NULL || g == NULL || x == NULL\n || a == NULL || (bn_ctx = BN_CTX_new()) == NULL)\n return NULL;\n if ((tmp = BN_new()) == NULL ||\n (tmp2 = BN_new()) == NULL ||\n (tmp3 = BN_new()) == NULL)\n goto err;\n if (!BN_mod_exp(tmp, g, x, N, bn_ctx))\n goto err;\n if ((k = srp_Calc_k(N, g)) == NULL)\n goto err;\n if (!BN_mod_mul(tmp2, tmp, k, N, bn_ctx))\n goto err;\n if (!BN_mod_sub(tmp, B, tmp2, N, bn_ctx))\n goto err;\n if (!BN_mul(tmp3, u, x, bn_ctx))\n goto err;\n if (!BN_add(tmp2, a, tmp3))\n goto err;\n K = BN_new();\n if (K != NULL && !BN_mod_exp(K, tmp, tmp2, N, bn_ctx)) {\n BN_free(K);\n K = NULL;\n }\n err:\n BN_CTX_free(bn_ctx);\n BN_clear_free(tmp);\n BN_clear_free(tmp2);\n BN_clear_free(tmp3);\n BN_free(k);\n return K;\n}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n BN_CTX *ctx)\n{\n int ret;\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n if (BN_is_odd(m)) {\n# ifdef MONT_EXP_WORD\n if (a->top == 1 && !a->neg\n && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(a, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(m, BN_FLG_CONSTTIME) == 0)) {\n BN_ULONG A = a->d[0];\n ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);\n } else\n# endif\n ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);\n } else\n#endif\n#ifdef RECP_MUL_MOD\n {\n ret = BN_mod_exp_recp(r, a, p, m, ctx);\n }\n#else\n {\n ret = BN_mod_exp_simple(r, a, p, m, ctx);\n }\n#endif\n bn_check_top(r);\n return ret;\n}', 'int BN_is_odd(const BIGNUM *a)\n{\n return (a->top > 0) && (a->d[0] & 1);\n}', 'int BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n BN_MONT_CTX *mont = NULL;\n int b, bits, ret = 0;\n int r_is_one;\n BN_ULONG w, next_w;\n BIGNUM *r, *t;\n BIGNUM *swap_tmp;\n#define BN_MOD_MUL_WORD(r, w, m) \\\n (BN_mul_word(r, (w)) && \\\n ( \\\n (BN_mod(t, r, m, ctx) && (swap_tmp = r, r = t, t = swap_tmp, 1))))\n#define BN_TO_MONTGOMERY_WORD(r, w, mont) \\\n (BN_set_word(r, (w)) && BN_to_montgomery(r, r, (mont), ctx))\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n BNerr(BN_F_BN_MOD_EXP_MONT_WORD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT_WORD, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n if (m->top == 1)\n a %= m->d[0];\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_abs_is_word(m, 1)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n if (a == 0) {\n BN_zero(rr);\n ret = 1;\n return ret;\n }\n BN_CTX_start(ctx);\n r = BN_CTX_get(ctx);\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n r_is_one = 1;\n w = a;\n for (b = bits - 2; b >= 0; b--) {\n next_w = w * w;\n if ((next_w / w) != w) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n next_w = 1;\n }\n w = next_w;\n if (!r_is_one) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (BN_is_bit_set(p, b)) {\n next_w = w * a;\n if ((next_w / a) != w) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n next_w = a;\n }\n w = next_w;\n }\n }\n if (w != 1) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n }\n if (r_is_one) {\n if (!BN_one(rr))\n goto err;\n } else {\n if (!BN_from_montgomery(rr, r, mont, ctx))\n goto err;\n }\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n BN_CTX_end(ctx);\n bn_check_top(rr);\n return ret;\n}', 'static BIGNUM *srp_Calc_k(const BIGNUM *N, const BIGNUM *g)\n{\n return srp_Calc_xy(N, g, N);\n}', 'static BIGNUM *srp_Calc_xy(const BIGNUM *x, const BIGNUM *y, const BIGNUM *N)\n{\n unsigned char digest[SHA_DIGEST_LENGTH];\n unsigned char *tmp = NULL;\n int numN = BN_num_bytes(N);\n BIGNUM *res = NULL;\n if (x != N && BN_ucmp(x, N) >= 0)\n return NULL;\n if (y != N && BN_ucmp(y, N) >= 0)\n return NULL;\n if ((tmp = OPENSSL_malloc(numN * 2)) == NULL)\n goto err;\n if (BN_bn2binpad(x, tmp, numN) < 0\n || BN_bn2binpad(y, tmp + numN, numN) < 0\n || !EVP_Digest(tmp, numN * 2, digest, NULL, EVP_sha1(), NULL))\n goto err;\n res = BN_bin2bn(digest, sizeof(digest), NULL);\n err:\n OPENSSL_free(tmp);\n return res;\n}', 'int BN_num_bits(const BIGNUM *a)\n{\n int i = a->top - 1;\n bn_check_top(a);\n if (BN_is_zero(a))\n return 0;\n return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));\n}', 'int BN_is_zero(const BIGNUM *a)\n{\n return a->top == 0;\n}', 'int BN_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_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,\n BN_CTX *ctx)\n{\n if (!BN_sub(r, a, b))\n return 0;\n return BN_nnmod(r, r, m, ctx);\n}', 'int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)\n{\n int i, ret = 0;\n BIGNUM *Ri, *R;\n if (BN_is_zero(mod))\n return 0;\n BN_CTX_start(ctx);\n if ((Ri = BN_CTX_get(ctx)) == NULL)\n goto err;\n R = &(mont->RR);\n if (!BN_copy(&(mont->N), mod))\n goto err;\n if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)\n BN_set_flags(&(mont->N), BN_FLG_CONSTTIME);\n mont->N.neg = 0;\n#ifdef MONT_WORD\n {\n BIGNUM tmod;\n BN_ULONG buf[2];\n bn_init(&tmod);\n tmod.d = buf;\n tmod.dmax = 2;\n tmod.neg = 0;\n if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)\n BN_set_flags(&tmod, BN_FLG_CONSTTIME);\n mont->ri = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2;\n# if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)\n BN_zero(R);\n if (!(BN_set_bit(R, 2 * BN_BITS2)))\n goto err;\n tmod.top = 0;\n if ((buf[0] = mod->d[0]))\n tmod.top = 1;\n if ((buf[1] = mod->top > 1 ? mod->d[1] : 0))\n tmod.top = 2;\n if (BN_is_one(&tmod))\n BN_zero(Ri);\n else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, 2 * BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (bn_expand(Ri, (int)sizeof(BN_ULONG) * 2) == NULL)\n goto err;\n Ri->neg = 0;\n Ri->d[0] = BN_MASK2;\n Ri->d[1] = BN_MASK2;\n Ri->top = 2;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;\n# else\n BN_zero(R);\n if (!(BN_set_bit(R, BN_BITS2)))\n goto err;\n buf[0] = mod->d[0];\n buf[1] = 0;\n tmod.top = buf[0] != 0 ? 1 : 0;\n if (BN_is_one(&tmod))\n BN_zero(Ri);\n else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (!BN_set_word(Ri, BN_MASK2))\n goto err;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = 0;\n# endif\n }\n#else\n {\n mont->ri = BN_num_bits(&mont->N);\n BN_zero(R);\n if (!BN_set_bit(R, mont->ri))\n goto err;\n if ((BN_mod_inverse(Ri, R, &mont->N, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, mont->ri))\n goto err;\n if (!BN_sub_word(Ri, 1))\n goto err;\n if (!BN_div(&(mont->Ni), NULL, Ri, &mont->N, ctx))\n goto err;\n }\n#endif\n BN_zero(&(mont->RR));\n if (!BN_set_bit(&(mont->RR), mont->ri * 2))\n goto err;\n if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx))\n goto err;\n for (i = mont->RR.top, ret = mont->N.top; i < ret; i++)\n mont->RR.d[i] = 0;\n mont->RR.top = ret;\n mont->RR.flags |= BN_FLG_FIXED_TOP;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n bn_check_top(b);\n if (a == b)\n return a;\n if (bn_wexpand(a, b->top) == NULL)\n return NULL;\n if (b->top > 0)\n memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n a->neg = b->neg;\n a->top = b->top;\n a->flags |= b->flags & BN_FLG_FIXED_TOP;\n bn_check_top(a);\n return a;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}'] |
530 | 0 | https://github.com/libav/libav/blob/7684a36113fa12c88ba80b5498f05849a6b58632/libavformat/flvdec.c/#L890 | static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
{
FLVContext *flv = s->priv_data;
int ret, i, type, size, flags, is_audio;
int64_t next, pos;
int64_t dts, pts = AV_NOPTS_VALUE;
int sample_rate = 0, channels = 0;
AVStream *st = NULL;
for (;; avio_skip(s->pb, 4)) {
pos = avio_tell(s->pb);
type = avio_r8(s->pb);
size = avio_rb24(s->pb);
dts = avio_rb24(s->pb);
dts |= avio_r8(s->pb) << 24;
av_dlog(s, "type:%d, size:%d, dts:%"PRId64"\n", type, size, dts);
if (s->pb->eof_reached)
return AVERROR_EOF;
avio_skip(s->pb, 3);
flags = 0;
if (flv->validate_next < flv->validate_count) {
int64_t validate_pos = flv->validate_index[flv->validate_next].pos;
if (pos == validate_pos) {
if (FFABS(dts - flv->validate_index[flv->validate_next].dts) <=
VALIDATE_INDEX_TS_THRESH) {
flv->validate_next++;
} else {
clear_index_entries(s, validate_pos);
flv->validate_count = 0;
}
} else if (pos > validate_pos) {
clear_index_entries(s, validate_pos);
flv->validate_count = 0;
}
}
if (size == 0)
continue;
next = size + avio_tell(s->pb);
if (type == FLV_TAG_TYPE_AUDIO) {
is_audio = 1;
flags = avio_r8(s->pb);
size--;
} else if (type == FLV_TAG_TYPE_VIDEO) {
is_audio = 0;
flags = avio_r8(s->pb);
size--;
if ((flags & 0xf0) == 0x50)
goto skip;
} else {
if (type == FLV_TAG_TYPE_META && size > 13 + 1 + 4)
if (flv_read_metabody(s, next) > 0) {
return flv_data_packet(s, pkt, dts, next);
} else
av_log(s, AV_LOG_DEBUG,
"skipping flv packet: type %d, size %d, flags %d\n",
type, size, flags);
skip:
avio_seek(s->pb, next, SEEK_SET);
continue;
}
if (!size)
continue;
for (i = 0; i < s->nb_streams; i++) {
st = s->streams[i];
if (is_audio && st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
if (flv_same_audio_codec(st->codec, flags))
break;
} else if (!is_audio &&
st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
if (flv_same_video_codec(st->codec, flags))
break;
}
}
if (i == s->nb_streams)
st = create_stream(s, is_audio ? AVMEDIA_TYPE_AUDIO
: AVMEDIA_TYPE_VIDEO);
av_dlog(s, "%d %X %d \n", is_audio, flags, st->discard);
if ((st->discard >= AVDISCARD_NONKEY &&
!((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || is_audio)) ||
(st->discard >= AVDISCARD_BIDIR &&
((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && !is_audio)) ||
st->discard >= AVDISCARD_ALL) {
avio_seek(s->pb, next, SEEK_SET);
continue;
}
if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY)
av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME);
break;
}
if (s->pb->seekable && (!s->duration || s->duration == AV_NOPTS_VALUE)) {
int size;
const int64_t pos = avio_tell(s->pb);
const int64_t fsize = avio_size(s->pb);
avio_seek(s->pb, fsize - 4, SEEK_SET);
size = avio_rb32(s->pb);
avio_seek(s->pb, fsize - 3 - size, SEEK_SET);
if (size == avio_rb24(s->pb) + 11) {
uint32_t ts = avio_rb24(s->pb);
ts |= avio_r8(s->pb) << 24;
s->duration = ts * (int64_t)AV_TIME_BASE / 1000;
}
avio_seek(s->pb, pos, SEEK_SET);
}
if (is_audio) {
int bits_per_coded_sample;
channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
sample_rate = 44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >>
FLV_AUDIO_SAMPLERATE_OFFSET) >> 3;
bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
if (!st->codec->channels || !st->codec->sample_rate ||
!st->codec->bits_per_coded_sample) {
st->codec->channels = channels;
st->codec->channel_layout = channels == 1
? AV_CH_LAYOUT_MONO
: AV_CH_LAYOUT_STEREO;
st->codec->sample_rate = sample_rate;
st->codec->bits_per_coded_sample = bits_per_coded_sample;
}
if (!st->codec->codec_id) {
flv_set_audio_codec(s, st, st->codec,
flags & FLV_AUDIO_CODECID_MASK);
flv->last_sample_rate =
sample_rate = st->codec->sample_rate;
flv->last_channels =
channels = st->codec->channels;
} else {
AVCodecContext ctx;
ctx.sample_rate = sample_rate;
flv_set_audio_codec(s, st, &ctx, flags & FLV_AUDIO_CODECID_MASK);
sample_rate = ctx.sample_rate;
}
} else {
size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK, 1);
}
if (st->codec->codec_id == AV_CODEC_ID_AAC ||
st->codec->codec_id == AV_CODEC_ID_H264) {
int type = avio_r8(s->pb);
size--;
if (st->codec->codec_id == AV_CODEC_ID_H264) {
int32_t cts = (avio_rb24(s->pb) + 0xff800000) ^ 0xff800000;
pts = dts + cts;
if (cts < 0) {
flv->wrong_dts = 1;
av_log(s, AV_LOG_WARNING,
"negative cts, previous timestamps might be wrong\n");
}
if (flv->wrong_dts)
dts = AV_NOPTS_VALUE;
}
if (type == 0) {
if (st->codec->extradata) {
if ((ret = flv_queue_extradata(flv, s->pb, is_audio, size)) < 0)
return ret;
ret = AVERROR(EAGAIN);
goto leave;
}
if ((ret = flv_get_extradata(s, st, size)) < 0)
return ret;
if (st->codec->codec_id == AV_CODEC_ID_AAC) {
MPEG4AudioConfig cfg;
avpriv_mpeg4audio_get_config(&cfg, st->codec->extradata,
st->codec->extradata_size * 8, 1);
st->codec->channels = cfg.channels;
st->codec->channel_layout = 0;
if (cfg.ext_sample_rate)
st->codec->sample_rate = cfg.ext_sample_rate;
else
st->codec->sample_rate = cfg.sample_rate;
av_dlog(s, "mp4a config channels %d sample rate %d\n",
st->codec->channels, st->codec->sample_rate);
}
ret = AVERROR(EAGAIN);
goto leave;
}
}
if (!size) {
ret = AVERROR(EAGAIN);
goto leave;
}
ret = av_get_packet(s->pb, pkt, size);
if (ret < 0)
return AVERROR(EIO);
pkt->size = ret;
pkt->dts = dts;
pkt->pts = pts == AV_NOPTS_VALUE ? dts : pts;
pkt->stream_index = st->index;
if (flv->new_extradata[is_audio]) {
uint8_t *side = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
flv->new_extradata_size[is_audio]);
if (side) {
memcpy(side, flv->new_extradata[is_audio],
flv->new_extradata_size[is_audio]);
av_freep(&flv->new_extradata[is_audio]);
flv->new_extradata_size[is_audio] = 0;
}
}
if (is_audio && (sample_rate != flv->last_sample_rate ||
channels != flv->last_channels)) {
flv->last_sample_rate = sample_rate;
flv->last_channels = channels;
ff_add_param_change(pkt, channels, 0, sample_rate, 0, 0);
}
if (is_audio || ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY))
pkt->flags |= AV_PKT_FLAG_KEY;
leave:
avio_skip(s->pb, 4);
return ret;
} | ['static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)\n{\n FLVContext *flv = s->priv_data;\n int ret, i, type, size, flags, is_audio;\n int64_t next, pos;\n int64_t dts, pts = AV_NOPTS_VALUE;\n int sample_rate = 0, channels = 0;\n AVStream *st = NULL;\n for (;; avio_skip(s->pb, 4)) {\n pos = avio_tell(s->pb);\n type = avio_r8(s->pb);\n size = avio_rb24(s->pb);\n dts = avio_rb24(s->pb);\n dts |= avio_r8(s->pb) << 24;\n av_dlog(s, "type:%d, size:%d, dts:%"PRId64"\\n", type, size, dts);\n if (s->pb->eof_reached)\n return AVERROR_EOF;\n avio_skip(s->pb, 3);\n flags = 0;\n if (flv->validate_next < flv->validate_count) {\n int64_t validate_pos = flv->validate_index[flv->validate_next].pos;\n if (pos == validate_pos) {\n if (FFABS(dts - flv->validate_index[flv->validate_next].dts) <=\n VALIDATE_INDEX_TS_THRESH) {\n flv->validate_next++;\n } else {\n clear_index_entries(s, validate_pos);\n flv->validate_count = 0;\n }\n } else if (pos > validate_pos) {\n clear_index_entries(s, validate_pos);\n flv->validate_count = 0;\n }\n }\n if (size == 0)\n continue;\n next = size + avio_tell(s->pb);\n if (type == FLV_TAG_TYPE_AUDIO) {\n is_audio = 1;\n flags = avio_r8(s->pb);\n size--;\n } else if (type == FLV_TAG_TYPE_VIDEO) {\n is_audio = 0;\n flags = avio_r8(s->pb);\n size--;\n if ((flags & 0xf0) == 0x50)\n goto skip;\n } else {\n if (type == FLV_TAG_TYPE_META && size > 13 + 1 + 4)\n if (flv_read_metabody(s, next) > 0) {\n return flv_data_packet(s, pkt, dts, next);\n } else\n av_log(s, AV_LOG_DEBUG,\n "skipping flv packet: type %d, size %d, flags %d\\n",\n type, size, flags);\nskip:\n avio_seek(s->pb, next, SEEK_SET);\n continue;\n }\n if (!size)\n continue;\n for (i = 0; i < s->nb_streams; i++) {\n st = s->streams[i];\n if (is_audio && st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {\n if (flv_same_audio_codec(st->codec, flags))\n break;\n } else if (!is_audio &&\n st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {\n if (flv_same_video_codec(st->codec, flags))\n break;\n }\n }\n if (i == s->nb_streams)\n st = create_stream(s, is_audio ? AVMEDIA_TYPE_AUDIO\n : AVMEDIA_TYPE_VIDEO);\n av_dlog(s, "%d %X %d \\n", is_audio, flags, st->discard);\n if ((st->discard >= AVDISCARD_NONKEY &&\n !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || is_audio)) ||\n (st->discard >= AVDISCARD_BIDIR &&\n ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && !is_audio)) ||\n st->discard >= AVDISCARD_ALL) {\n avio_seek(s->pb, next, SEEK_SET);\n continue;\n }\n if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY)\n av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME);\n break;\n }\n if (s->pb->seekable && (!s->duration || s->duration == AV_NOPTS_VALUE)) {\n int size;\n const int64_t pos = avio_tell(s->pb);\n const int64_t fsize = avio_size(s->pb);\n avio_seek(s->pb, fsize - 4, SEEK_SET);\n size = avio_rb32(s->pb);\n avio_seek(s->pb, fsize - 3 - size, SEEK_SET);\n if (size == avio_rb24(s->pb) + 11) {\n uint32_t ts = avio_rb24(s->pb);\n ts |= avio_r8(s->pb) << 24;\n s->duration = ts * (int64_t)AV_TIME_BASE / 1000;\n }\n avio_seek(s->pb, pos, SEEK_SET);\n }\n if (is_audio) {\n int bits_per_coded_sample;\n channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;\n sample_rate = 44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >>\n FLV_AUDIO_SAMPLERATE_OFFSET) >> 3;\n bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;\n if (!st->codec->channels || !st->codec->sample_rate ||\n !st->codec->bits_per_coded_sample) {\n st->codec->channels = channels;\n st->codec->channel_layout = channels == 1\n ? AV_CH_LAYOUT_MONO\n : AV_CH_LAYOUT_STEREO;\n st->codec->sample_rate = sample_rate;\n st->codec->bits_per_coded_sample = bits_per_coded_sample;\n }\n if (!st->codec->codec_id) {\n flv_set_audio_codec(s, st, st->codec,\n flags & FLV_AUDIO_CODECID_MASK);\n flv->last_sample_rate =\n sample_rate = st->codec->sample_rate;\n flv->last_channels =\n channels = st->codec->channels;\n } else {\n AVCodecContext ctx;\n ctx.sample_rate = sample_rate;\n flv_set_audio_codec(s, st, &ctx, flags & FLV_AUDIO_CODECID_MASK);\n sample_rate = ctx.sample_rate;\n }\n } else {\n size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK, 1);\n }\n if (st->codec->codec_id == AV_CODEC_ID_AAC ||\n st->codec->codec_id == AV_CODEC_ID_H264) {\n int type = avio_r8(s->pb);\n size--;\n if (st->codec->codec_id == AV_CODEC_ID_H264) {\n int32_t cts = (avio_rb24(s->pb) + 0xff800000) ^ 0xff800000;\n pts = dts + cts;\n if (cts < 0) {\n flv->wrong_dts = 1;\n av_log(s, AV_LOG_WARNING,\n "negative cts, previous timestamps might be wrong\\n");\n }\n if (flv->wrong_dts)\n dts = AV_NOPTS_VALUE;\n }\n if (type == 0) {\n if (st->codec->extradata) {\n if ((ret = flv_queue_extradata(flv, s->pb, is_audio, size)) < 0)\n return ret;\n ret = AVERROR(EAGAIN);\n goto leave;\n }\n if ((ret = flv_get_extradata(s, st, size)) < 0)\n return ret;\n if (st->codec->codec_id == AV_CODEC_ID_AAC) {\n MPEG4AudioConfig cfg;\n avpriv_mpeg4audio_get_config(&cfg, st->codec->extradata,\n st->codec->extradata_size * 8, 1);\n st->codec->channels = cfg.channels;\n st->codec->channel_layout = 0;\n if (cfg.ext_sample_rate)\n st->codec->sample_rate = cfg.ext_sample_rate;\n else\n st->codec->sample_rate = cfg.sample_rate;\n av_dlog(s, "mp4a config channels %d sample rate %d\\n",\n st->codec->channels, st->codec->sample_rate);\n }\n ret = AVERROR(EAGAIN);\n goto leave;\n }\n }\n if (!size) {\n ret = AVERROR(EAGAIN);\n goto leave;\n }\n ret = av_get_packet(s->pb, pkt, size);\n if (ret < 0)\n return AVERROR(EIO);\n pkt->size = ret;\n pkt->dts = dts;\n pkt->pts = pts == AV_NOPTS_VALUE ? dts : pts;\n pkt->stream_index = st->index;\n if (flv->new_extradata[is_audio]) {\n uint8_t *side = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,\n flv->new_extradata_size[is_audio]);\n if (side) {\n memcpy(side, flv->new_extradata[is_audio],\n flv->new_extradata_size[is_audio]);\n av_freep(&flv->new_extradata[is_audio]);\n flv->new_extradata_size[is_audio] = 0;\n }\n }\n if (is_audio && (sample_rate != flv->last_sample_rate ||\n channels != flv->last_channels)) {\n flv->last_sample_rate = sample_rate;\n flv->last_channels = channels;\n ff_add_param_change(pkt, channels, 0, sample_rate, 0, 0);\n }\n if (is_audio || ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY))\n pkt->flags |= AV_PKT_FLAG_KEY;\nleave:\n avio_skip(s->pb, 4);\n return ret;\n}'] |
531 | 0 | https://github.com/openssl/openssl/blob/9c46f4b9cd4912b61cb546c48b678488d7f26ed6/crypto/bn/bn_ctx.c/#L353 | static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
} | ['int DH_check(const DH *dh, int *ret)\n{\n int ok = 0;\n BN_CTX *ctx = NULL;\n BN_ULONG l;\n BIGNUM *t1 = NULL, *t2 = NULL;\n *ret = 0;\n ctx = BN_CTX_new();\n if (ctx == NULL)\n goto err;\n BN_CTX_start(ctx);\n t1 = BN_CTX_get(ctx);\n if (t1 == NULL)\n goto err;\n t2 = BN_CTX_get(ctx);\n if (t2 == NULL)\n goto err;\n if (dh->q) {\n if (BN_cmp(dh->g, BN_value_one()) <= 0)\n *ret |= DH_NOT_SUITABLE_GENERATOR;\n else if (BN_cmp(dh->g, dh->p) >= 0)\n *ret |= DH_NOT_SUITABLE_GENERATOR;\n else {\n if (!BN_mod_exp(t1, dh->g, dh->q, dh->p, ctx))\n goto err;\n if (!BN_is_one(t1))\n *ret |= DH_NOT_SUITABLE_GENERATOR;\n }\n if (!BN_is_prime_ex(dh->q, BN_prime_checks, ctx, NULL))\n *ret |= DH_CHECK_Q_NOT_PRIME;\n if (!BN_div(t1, t2, dh->p, dh->q, ctx))\n goto err;\n if (!BN_is_one(t2))\n *ret |= DH_CHECK_INVALID_Q_VALUE;\n if (dh->j && BN_cmp(dh->j, t1))\n *ret |= DH_CHECK_INVALID_J_VALUE;\n } else if (BN_is_word(dh->g, DH_GENERATOR_2)) {\n l = BN_mod_word(dh->p, 24);\n if (l != 11)\n *ret |= DH_NOT_SUITABLE_GENERATOR;\n }\n#if 0\n else if (BN_is_word(dh->g, DH_GENERATOR_3)) {\n l = BN_mod_word(dh->p, 12);\n if (l != 5)\n *ret |= DH_NOT_SUITABLE_GENERATOR;\n }\n#endif\n else if (BN_is_word(dh->g, DH_GENERATOR_5)) {\n l = BN_mod_word(dh->p, 10);\n if ((l != 3) && (l != 7))\n *ret |= DH_NOT_SUITABLE_GENERATOR;\n } else\n *ret |= DH_UNABLE_TO_CHECK_GENERATOR;\n if (!BN_is_prime_ex(dh->p, BN_prime_checks, ctx, NULL))\n *ret |= DH_CHECK_P_NOT_PRIME;\n else if (!dh->q) {\n if (!BN_rshift1(t1, dh->p))\n goto err;\n if (!BN_is_prime_ex(t1, BN_prime_checks, ctx, NULL))\n *ret |= DH_CHECK_P_NOT_SAFE_PRIME;\n }\n ok = 1;\n err:\n if (ctx != NULL) {\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n }\n return (ok);\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_start", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG_EXIT(ctx);\n}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n BN_CTX *ctx)\n{\n int ret;\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n if (BN_is_odd(m)) {\n# ifdef MONT_EXP_WORD\n if (a->top == 1 && !a->neg\n && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)) {\n BN_ULONG A = a->d[0];\n ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);\n } else\n# endif\n ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);\n } else\n#endif\n#ifdef RECP_MUL_MOD\n {\n ret = BN_mod_exp_recp(r, a, p, m, ctx);\n }\n#else\n {\n ret = BN_mod_exp_simple(r, a, p, m, ctx);\n }\n#endif\n bn_check_top(r);\n return (ret);\n}', 'int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx)\n{\n int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n int start = 1;\n BIGNUM *aa;\n BIGNUM *val[TABLE_SIZE];\n BN_RECP_CTX recp;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0) {\n BNerr(BN_F_BN_MOD_EXP_RECP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return -1;\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n ret = BN_one(r);\n return ret;\n }\n BN_CTX_start(ctx);\n aa = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (!aa || !val[0])\n goto err;\n BN_RECP_CTX_init(&recp);\n if (m->neg) {\n if (!BN_copy(aa, m))\n goto err;\n aa->neg = 0;\n if (BN_RECP_CTX_set(&recp, aa, ctx) <= 0)\n goto err;\n } else {\n if (BN_RECP_CTX_set(&recp, m, ctx) <= 0)\n goto err;\n }\n if (!BN_nnmod(val[0], a, m, ctx))\n goto err;\n if (BN_is_zero(val[0])) {\n BN_zero(r);\n ret = 1;\n goto err;\n }\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!BN_mod_mul_reciprocal(aa, val[0], val[0], &recp, ctx))\n goto err;\n j = 1 << (window - 1);\n for (i = 1; i < j; i++) {\n if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul_reciprocal(val[i], val[i - 1], aa, &recp, ctx))\n goto err;\n }\n }\n start = 1;\n wvalue = 0;\n wstart = bits - 1;\n wend = 0;\n if (!BN_one(r))\n goto err;\n for (;;) {\n if (BN_is_bit_set(p, wstart) == 0) {\n if (!start)\n if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))\n goto err;\n if (wstart == 0)\n break;\n wstart--;\n continue;\n }\n j = wstart;\n wvalue = 1;\n wend = 0;\n for (i = 1; i < window; i++) {\n if (wstart - i < 0)\n break;\n if (BN_is_bit_set(p, wstart - i)) {\n wvalue <<= (i - wend);\n wvalue |= 1;\n wend = i;\n }\n }\n j = wend + 1;\n if (!start)\n for (i = 0; i < j; i++) {\n if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))\n goto err;\n }\n if (!BN_mod_mul_reciprocal(r, r, val[wvalue >> 1], &recp, ctx))\n goto err;\n wstart -= wend + 1;\n wvalue = 0;\n start = 0;\n if (wstart < 0)\n break;\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_RECP_CTX_free(&recp);\n bn_check_top(r);\n return (ret);\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return (0);\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return (0);\n }\n if (dv != NULL)\n BN_zero(dv);\n return (1);\n }\n BN_CTX_start(ctx);\n 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}'] |
532 | 0 | https://github.com/libav/libav/blob/6aa4e88106a554cef1d2294bb0a18b8f843032ef/libavcodec/mpegvideo_enc.c/#L1096 | static int estimate_best_b_count(MpegEncContext *s)
{
AVCodec *codec = avcodec_find_encoder(s->avctx->codec_id);
AVCodecContext *c = avcodec_alloc_context3(NULL);
AVFrame input[FF_MAX_B_FRAMES + 2];
const int scale = s->avctx->brd_scale;
int i, j, out_size, p_lambda, b_lambda, lambda2;
int outbuf_size = s->width * s->height;
uint8_t *outbuf = av_malloc(outbuf_size);
int64_t best_rd = INT64_MAX;
int best_b_count = -1;
assert(scale >= 0 && scale <= 3);
p_lambda = s->last_lambda_for[AV_PICTURE_TYPE_P];
b_lambda = s->last_lambda_for[AV_PICTURE_TYPE_B];
if (!b_lambda)
b_lambda = p_lambda;
lambda2 = (b_lambda * b_lambda + (1 << FF_LAMBDA_SHIFT) / 2) >>
FF_LAMBDA_SHIFT;
c->width = s->width >> scale;
c->height = s->height >> scale;
c->flags = CODEC_FLAG_QSCALE | CODEC_FLAG_PSNR |
CODEC_FLAG_INPUT_PRESERVED ;
c->flags |= s->avctx->flags & CODEC_FLAG_QPEL;
c->mb_decision = s->avctx->mb_decision;
c->me_cmp = s->avctx->me_cmp;
c->mb_cmp = s->avctx->mb_cmp;
c->me_sub_cmp = s->avctx->me_sub_cmp;
c->pix_fmt = PIX_FMT_YUV420P;
c->time_base = s->avctx->time_base;
c->max_b_frames = s->max_b_frames;
if (avcodec_open2(c, codec, NULL) < 0)
return -1;
for (i = 0; i < s->max_b_frames + 2; i++) {
int ysize = c->width * c->height;
int csize = (c->width / 2) * (c->height / 2);
Picture pre_input, *pre_input_ptr = i ? s->input_picture[i - 1] :
s->next_picture_ptr;
avcodec_get_frame_defaults(&input[i]);
input[i].data[0] = av_malloc(ysize + 2 * csize);
input[i].data[1] = input[i].data[0] + ysize;
input[i].data[2] = input[i].data[1] + csize;
input[i].linesize[0] = c->width;
input[i].linesize[1] =
input[i].linesize[2] = c->width / 2;
if (pre_input_ptr && (!i || s->input_picture[i - 1])) {
pre_input = *pre_input_ptr;
if (pre_input.f.type != FF_BUFFER_TYPE_SHARED && i) {
pre_input.f.data[0] += INPLACE_OFFSET;
pre_input.f.data[1] += INPLACE_OFFSET;
pre_input.f.data[2] += INPLACE_OFFSET;
}
s->dsp.shrink[scale](input[i].data[0], input[i].linesize[0],
pre_input.f.data[0], pre_input.f.linesize[0],
c->width, c->height);
s->dsp.shrink[scale](input[i].data[1], input[i].linesize[1],
pre_input.f.data[1], pre_input.f.linesize[1],
c->width >> 1, c->height >> 1);
s->dsp.shrink[scale](input[i].data[2], input[i].linesize[2],
pre_input.f.data[2], pre_input.f.linesize[2],
c->width >> 1, c->height >> 1);
}
}
for (j = 0; j < s->max_b_frames + 1; j++) {
int64_t rd = 0;
if (!s->input_picture[j])
break;
c->error[0] = c->error[1] = c->error[2] = 0;
input[0].pict_type = AV_PICTURE_TYPE_I;
input[0].quality = 1 * FF_QP2LAMBDA;
out_size = avcodec_encode_video(c, outbuf,
outbuf_size, &input[0]);
for (i = 0; i < s->max_b_frames + 1; i++) {
int is_p = i % (j + 1) == j || i == s->max_b_frames;
input[i + 1].pict_type = is_p ?
AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B;
input[i + 1].quality = is_p ? p_lambda : b_lambda;
out_size = avcodec_encode_video(c, outbuf, outbuf_size,
&input[i + 1]);
rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
}
while (out_size) {
out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
}
rd += c->error[0] + c->error[1] + c->error[2];
if (rd < best_rd) {
best_rd = rd;
best_b_count = j;
}
}
av_freep(&outbuf);
avcodec_close(c);
av_freep(&c);
for (i = 0; i < s->max_b_frames + 2; i++) {
av_freep(&input[i].data[0]);
}
return best_b_count;
} | ['static int estimate_best_b_count(MpegEncContext *s)\n{\n AVCodec *codec = avcodec_find_encoder(s->avctx->codec_id);\n AVCodecContext *c = avcodec_alloc_context3(NULL);\n AVFrame input[FF_MAX_B_FRAMES + 2];\n const int scale = s->avctx->brd_scale;\n int i, j, out_size, p_lambda, b_lambda, lambda2;\n int outbuf_size = s->width * s->height;\n uint8_t *outbuf = av_malloc(outbuf_size);\n int64_t best_rd = INT64_MAX;\n int best_b_count = -1;\n assert(scale >= 0 && scale <= 3);\n p_lambda = s->last_lambda_for[AV_PICTURE_TYPE_P];\n b_lambda = s->last_lambda_for[AV_PICTURE_TYPE_B];\n if (!b_lambda)\n b_lambda = p_lambda;\n lambda2 = (b_lambda * b_lambda + (1 << FF_LAMBDA_SHIFT) / 2) >>\n FF_LAMBDA_SHIFT;\n c->width = s->width >> scale;\n c->height = s->height >> scale;\n c->flags = CODEC_FLAG_QSCALE | CODEC_FLAG_PSNR |\n CODEC_FLAG_INPUT_PRESERVED ;\n c->flags |= s->avctx->flags & CODEC_FLAG_QPEL;\n c->mb_decision = s->avctx->mb_decision;\n c->me_cmp = s->avctx->me_cmp;\n c->mb_cmp = s->avctx->mb_cmp;\n c->me_sub_cmp = s->avctx->me_sub_cmp;\n c->pix_fmt = PIX_FMT_YUV420P;\n c->time_base = s->avctx->time_base;\n c->max_b_frames = s->max_b_frames;\n if (avcodec_open2(c, codec, NULL) < 0)\n return -1;\n for (i = 0; i < s->max_b_frames + 2; i++) {\n int ysize = c->width * c->height;\n int csize = (c->width / 2) * (c->height / 2);\n Picture pre_input, *pre_input_ptr = i ? s->input_picture[i - 1] :\n s->next_picture_ptr;\n avcodec_get_frame_defaults(&input[i]);\n input[i].data[0] = av_malloc(ysize + 2 * csize);\n input[i].data[1] = input[i].data[0] + ysize;\n input[i].data[2] = input[i].data[1] + csize;\n input[i].linesize[0] = c->width;\n input[i].linesize[1] =\n input[i].linesize[2] = c->width / 2;\n if (pre_input_ptr && (!i || s->input_picture[i - 1])) {\n pre_input = *pre_input_ptr;\n if (pre_input.f.type != FF_BUFFER_TYPE_SHARED && i) {\n pre_input.f.data[0] += INPLACE_OFFSET;\n pre_input.f.data[1] += INPLACE_OFFSET;\n pre_input.f.data[2] += INPLACE_OFFSET;\n }\n s->dsp.shrink[scale](input[i].data[0], input[i].linesize[0],\n pre_input.f.data[0], pre_input.f.linesize[0],\n c->width, c->height);\n s->dsp.shrink[scale](input[i].data[1], input[i].linesize[1],\n pre_input.f.data[1], pre_input.f.linesize[1],\n c->width >> 1, c->height >> 1);\n s->dsp.shrink[scale](input[i].data[2], input[i].linesize[2],\n pre_input.f.data[2], pre_input.f.linesize[2],\n c->width >> 1, c->height >> 1);\n }\n }\n for (j = 0; j < s->max_b_frames + 1; j++) {\n int64_t rd = 0;\n if (!s->input_picture[j])\n break;\n c->error[0] = c->error[1] = c->error[2] = 0;\n input[0].pict_type = AV_PICTURE_TYPE_I;\n input[0].quality = 1 * FF_QP2LAMBDA;\n out_size = avcodec_encode_video(c, outbuf,\n outbuf_size, &input[0]);\n for (i = 0; i < s->max_b_frames + 1; i++) {\n int is_p = i % (j + 1) == j || i == s->max_b_frames;\n input[i + 1].pict_type = is_p ?\n AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B;\n input[i + 1].quality = is_p ? p_lambda : b_lambda;\n out_size = avcodec_encode_video(c, outbuf, outbuf_size,\n &input[i + 1]);\n rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);\n }\n while (out_size) {\n out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);\n rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);\n }\n rd += c->error[0] + c->error[1] + c->error[2];\n if (rd < best_rd) {\n best_rd = rd;\n best_b_count = j;\n }\n }\n av_freep(&outbuf);\n avcodec_close(c);\n av_freep(&c);\n for (i = 0; i < s->max_b_frames + 2; i++) {\n av_freep(&input[i].data[0]);\n }\n return best_b_count;\n}', 'AVCodecContext *avcodec_alloc_context3(AVCodec *codec){\n AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));\n if(avctx==NULL) return NULL;\n if(avcodec_get_context_defaults3(avctx, codec) < 0){\n av_free(avctx);\n return NULL;\n }\n return avctx;\n}', 'void *av_malloc(size_t size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n 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}'] |
533 | 0 | https://github.com/libav/libav/blob/fc322d6a70189da24dbd445c710bb214eb031ce7/libavcodec/bitstream.h/#L139 | static inline uint64_t get_val(BitstreamContext *bc, unsigned n)
{
#ifdef BITSTREAM_READER_LE
uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);
bc->bits >>= n;
#else
uint64_t ret = bc->bits >> (64 - n);
bc->bits <<= n;
#endif
bc->bits_left -= n;
return ret;
} | ['static inline int read_huff_channels(MLPDecodeContext *m, BitstreamContext *bc,\n unsigned int substr, unsigned int pos)\n{\n SubStream *s = &m->substream[substr];\n unsigned int mat, channel;\n for (mat = 0; mat < s->num_primitive_matrices; mat++)\n if (s->lsb_bypass[mat])\n m->bypassed_lsbs[pos + s->blockpos][mat] = bitstream_read_bit(bc);\n for (channel = s->min_channel; channel <= s->max_channel; channel++) {\n ChannelParams *cp = &s->channel_params[channel];\n int codebook = cp->codebook;\n int quant_step_size = s->quant_step_size[channel];\n int lsb_bits = cp->huff_lsbs - quant_step_size;\n int result = 0;\n if (codebook > 0)\n result = bitstream_read_vlc(bc, huff_vlc[codebook-1].table,\n VLC_BITS,\n (9 + VLC_BITS - 1) / VLC_BITS);\n if (result < 0)\n return AVERROR_INVALIDDATA;\n if (lsb_bits > 0)\n result = (result << lsb_bits) + bitstream_read(bc, lsb_bits);\n result += cp->sign_huff_offset;\n result <<= quant_step_size;\n m->sample_buffer[pos + s->blockpos][channel] = result;\n }\n return 0;\n}', 'static inline unsigned bitstream_read_bit(BitstreamContext *bc)\n{\n if (!bc->bits_left)\n refill_64(bc);\n return get_val(bc, 1);\n}', 'static inline uint64_t get_val(BitstreamContext *bc, unsigned n)\n{\n#ifdef BITSTREAM_READER_LE\n uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);\n bc->bits >>= n;\n#else\n uint64_t ret = bc->bits >> (64 - n);\n bc->bits <<= n;\n#endif\n bc->bits_left -= n;\n return ret;\n}'] |
534 | 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;
} | ['static int file_lshift(STANZA *s)\n{\n BIGNUM *a = NULL, *lshift = NULL, *ret = NULL;\n int n = 0, st = 0;\n if (!TEST_ptr(a = getBN(s, "A"))\n || !TEST_ptr(lshift = getBN(s, "LShift"))\n || !TEST_ptr(ret = BN_new()))\n if (!TEST_true(BN_lshift(ret, a, n))\n || !equalBN("A << N", lshift, ret)\n || !TEST_true(BN_rshift(ret, lshift, n))\n || !equalBN("A >> N", a, ret))\n goto err;\n st = 1;\nerr:\n BN_free(a);\n BN_free(lshift);\n BN_free(ret);\n return st;\n}', 'static BIGNUM *getBN(STANZA *s, const char *attribute)\n{\n const char *hex;\n BIGNUM *ret = NULL;\n if ((hex = findattr(s, attribute)) == NULL) {\n TEST_error("%s:%d: Can\'t find %s", s->test_file, s->start, attribute);\n return NULL;\n }\n if (parseBN(&ret, hex) != (int)strlen(hex)) {\n TEST_error("Could not decode \'%s\'", hex);\n return NULL;\n }\n return ret;\n}', 'static int parseBN(BIGNUM **out, const char *in)\n{\n *out = NULL;\n return BN_hex2bn(out, in);\n}', "int BN_hex2bn(BIGNUM **bn, const char *a)\n{\n BIGNUM *ret = NULL;\n BN_ULONG l = 0;\n int neg = 0, h, m, i, j, k, c;\n int num;\n if (a == NULL || *a == '\\0')\n return 0;\n if (*a == '-') {\n neg = 1;\n a++;\n }\n for (i = 0; i <= INT_MAX / 4 && ossl_isxdigit(a[i]); i++)\n continue;\n if (i == 0 || i > INT_MAX / 4)\n goto err;\n num = i + neg;\n if (bn == NULL)\n return num;\n if (*bn == NULL) {\n if ((ret = BN_new()) == NULL)\n return 0;\n } else {\n ret = *bn;\n BN_zero(ret);\n }\n if (bn_expand(ret, i * 4) == NULL)\n goto err;\n j = i;\n m = 0;\n h = 0;\n while (j > 0) {\n m = (BN_BYTES * 2 <= j) ? BN_BYTES * 2 : j;\n l = 0;\n for (;;) {\n c = a[j - m];\n k = OPENSSL_hexchar2int(c);\n if (k < 0)\n k = 0;\n l = (l << 4) | k;\n if (--m <= 0) {\n ret->d[h++] = l;\n break;\n }\n }\n j -= BN_BYTES * 2;\n }\n ret->top = h;\n bn_correct_top(ret);\n *bn = ret;\n bn_check_top(ret);\n if (ret->top != 0)\n ret->neg = neg;\n return num;\n err:\n if (*bn == NULL)\n BN_free(ret);\n return 0;\n}", 'int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l;\n bn_check_top(r);\n bn_check_top(a);\n if (n < 0) {\n BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n nw = n / BN_BITS2;\n if (bn_wexpand(r, a->top + nw + 1) == NULL)\n return 0;\n r->neg = a->neg;\n lb = n % BN_BITS2;\n rb = BN_BITS2 - lb;\n f = a->d;\n t = r->d;\n t[a->top + nw] = 0;\n if (lb == 0)\n for (i = a->top - 1; i >= 0; i--)\n t[nw + i] = f[i];\n else\n for (i = a->top - 1; i >= 0; i--) {\n l = f[i];\n t[nw + i + 1] |= (l >> rb) & BN_MASK2;\n t[nw + i] = (l << lb) & BN_MASK2;\n }\n memset(t, 0, sizeof(*t) * nw);\n r->top = a->top + nw + 1;\n bn_correct_top(r);\n bn_check_top(r);\n return 1;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n INCREMENT(malloc_count);\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)(file); (void)(line);\n ret = malloc(num);\n#endif\n return ret;\n}'] |
535 | 0 | https://github.com/openssl/openssl/blob/f305ecdac0b7048e7ef38a7196f4393fa7ceff38/crypto/bio/bss_conn.c/#L206 | static int conn_state(BIO *b, BIO_CONNECT *c)
{
int ret = -1, i;
BIO_info_cb *cb = NULL;
if (c->info_callback != NULL)
cb = c->info_callback;
for (;;) {
switch (c->state) {
case BIO_CONN_S_BEFORE:
if (c->param_hostname == NULL && c->param_service == NULL) {
BIOerr(BIO_F_CONN_STATE, BIO_R_NO_HOSTNAME_OR_SERVICE_SPECIFIED);
ERR_add_error_data(4,
"hostname=", c->param_hostname,
" service=", c->param_service);
goto exit_loop;
}
c->state = BIO_CONN_S_GET_ADDR;
break;
case BIO_CONN_S_GET_ADDR:
{
int family = AF_UNSPEC;
switch (c->connect_family) {
case BIO_FAMILY_IPV6:
if (1) {
#ifdef AF_INET6
family = AF_INET6;
} else {
#endif
BIOerr(BIO_F_CONN_STATE, BIO_R_UNAVAILABLE_IP_FAMILY);
goto exit_loop;
}
break;
case BIO_FAMILY_IPV4:
family = AF_INET;
break;
case BIO_FAMILY_IPANY:
family = AF_UNSPEC;
break;
default:
BIOerr(BIO_F_CONN_STATE, BIO_R_UNSUPPORTED_IP_FAMILY);
goto exit_loop;
}
if (BIO_lookup(c->param_hostname, c->param_service,
BIO_LOOKUP_CLIENT,
family, SOCK_STREAM, &c->addr_first) == 0)
goto exit_loop;
}
if (c->addr_first == NULL) {
BIOerr(BIO_F_CONN_STATE, BIO_R_LOOKUP_RETURNED_NOTHING);
goto exit_loop;
}
c->addr_iter = c->addr_first;
c->state = BIO_CONN_S_CREATE_SOCKET;
break;
case BIO_CONN_S_CREATE_SOCKET:
ret = BIO_socket(BIO_ADDRINFO_family(c->addr_iter),
BIO_ADDRINFO_socktype(c->addr_iter),
BIO_ADDRINFO_protocol(c->addr_iter), 0);
if (ret == (int)INVALID_SOCKET) {
ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
"calling socket(%s, %s)",
c->param_hostname, c->param_service);
BIOerr(BIO_F_CONN_STATE, BIO_R_UNABLE_TO_CREATE_SOCKET);
goto exit_loop;
}
b->num = ret;
c->state = BIO_CONN_S_CONNECT;
break;
case BIO_CONN_S_CONNECT:
BIO_clear_retry_flags(b);
ret = BIO_connect(b->num, BIO_ADDRINFO_address(c->addr_iter),
BIO_SOCK_KEEPALIVE | c->connect_mode);
b->retry_reason = 0;
if (ret == 0) {
if (BIO_sock_should_retry(ret)) {
BIO_set_retry_special(b);
c->state = BIO_CONN_S_BLOCKED_CONNECT;
b->retry_reason = BIO_RR_CONNECT;
ERR_clear_error();
} else if ((c->addr_iter = BIO_ADDRINFO_next(c->addr_iter))
!= NULL) {
BIO_closesocket(b->num);
c->state = BIO_CONN_S_CREATE_SOCKET;
ERR_clear_error();
break;
} else {
ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
"calling connect(%s, %s)",
c->param_hostname, c->param_service);
BIOerr(BIO_F_CONN_STATE, BIO_R_CONNECT_ERROR);
}
goto exit_loop;
} else {
c->state = BIO_CONN_S_OK;
}
break;
case BIO_CONN_S_BLOCKED_CONNECT:
i = BIO_sock_error(b->num);
if (i) {
BIO_clear_retry_flags(b);
ERR_raise_data(ERR_LIB_SYS, i,
"calling connect(%s, %s)",
c->param_hostname, c->param_service);
BIOerr(BIO_F_CONN_STATE, BIO_R_NBIO_CONNECT_ERROR);
ret = 0;
goto exit_loop;
} else
c->state = BIO_CONN_S_OK;
break;
case BIO_CONN_S_OK:
ret = 1;
goto exit_loop;
default:
goto exit_loop;
}
if (cb != NULL) {
if ((ret = cb((BIO *)b, c->state, ret)) == 0)
goto end;
}
}
exit_loop:
if (cb != NULL)
ret = cb((BIO *)b, c->state, ret);
end:
return ret;
} | ['static int conn_state(BIO *b, BIO_CONNECT *c)\n{\n int ret = -1, i;\n BIO_info_cb *cb = NULL;\n if (c->info_callback != NULL)\n cb = c->info_callback;\n for (;;) {\n switch (c->state) {\n case BIO_CONN_S_BEFORE:\n if (c->param_hostname == NULL && c->param_service == NULL) {\n BIOerr(BIO_F_CONN_STATE, BIO_R_NO_HOSTNAME_OR_SERVICE_SPECIFIED);\n ERR_add_error_data(4,\n "hostname=", c->param_hostname,\n " service=", c->param_service);\n goto exit_loop;\n }\n c->state = BIO_CONN_S_GET_ADDR;\n break;\n case BIO_CONN_S_GET_ADDR:\n {\n int family = AF_UNSPEC;\n switch (c->connect_family) {\n case BIO_FAMILY_IPV6:\n if (1) {\n#ifdef AF_INET6\n family = AF_INET6;\n } else {\n#endif\n BIOerr(BIO_F_CONN_STATE, BIO_R_UNAVAILABLE_IP_FAMILY);\n goto exit_loop;\n }\n break;\n case BIO_FAMILY_IPV4:\n family = AF_INET;\n break;\n case BIO_FAMILY_IPANY:\n family = AF_UNSPEC;\n break;\n default:\n BIOerr(BIO_F_CONN_STATE, BIO_R_UNSUPPORTED_IP_FAMILY);\n goto exit_loop;\n }\n if (BIO_lookup(c->param_hostname, c->param_service,\n BIO_LOOKUP_CLIENT,\n family, SOCK_STREAM, &c->addr_first) == 0)\n goto exit_loop;\n }\n if (c->addr_first == NULL) {\n BIOerr(BIO_F_CONN_STATE, BIO_R_LOOKUP_RETURNED_NOTHING);\n goto exit_loop;\n }\n c->addr_iter = c->addr_first;\n c->state = BIO_CONN_S_CREATE_SOCKET;\n break;\n case BIO_CONN_S_CREATE_SOCKET:\n ret = BIO_socket(BIO_ADDRINFO_family(c->addr_iter),\n BIO_ADDRINFO_socktype(c->addr_iter),\n BIO_ADDRINFO_protocol(c->addr_iter), 0);\n if (ret == (int)INVALID_SOCKET) {\n ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),\n "calling socket(%s, %s)",\n c->param_hostname, c->param_service);\n BIOerr(BIO_F_CONN_STATE, BIO_R_UNABLE_TO_CREATE_SOCKET);\n goto exit_loop;\n }\n b->num = ret;\n c->state = BIO_CONN_S_CONNECT;\n break;\n case BIO_CONN_S_CONNECT:\n BIO_clear_retry_flags(b);\n ret = BIO_connect(b->num, BIO_ADDRINFO_address(c->addr_iter),\n BIO_SOCK_KEEPALIVE | c->connect_mode);\n b->retry_reason = 0;\n if (ret == 0) {\n if (BIO_sock_should_retry(ret)) {\n BIO_set_retry_special(b);\n c->state = BIO_CONN_S_BLOCKED_CONNECT;\n b->retry_reason = BIO_RR_CONNECT;\n ERR_clear_error();\n } else if ((c->addr_iter = BIO_ADDRINFO_next(c->addr_iter))\n != NULL) {\n BIO_closesocket(b->num);\n c->state = BIO_CONN_S_CREATE_SOCKET;\n ERR_clear_error();\n break;\n } else {\n ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),\n "calling connect(%s, %s)",\n c->param_hostname, c->param_service);\n BIOerr(BIO_F_CONN_STATE, BIO_R_CONNECT_ERROR);\n }\n goto exit_loop;\n } else {\n c->state = BIO_CONN_S_OK;\n }\n break;\n case BIO_CONN_S_BLOCKED_CONNECT:\n i = BIO_sock_error(b->num);\n if (i) {\n BIO_clear_retry_flags(b);\n ERR_raise_data(ERR_LIB_SYS, i,\n "calling connect(%s, %s)",\n c->param_hostname, c->param_service);\n BIOerr(BIO_F_CONN_STATE, BIO_R_NBIO_CONNECT_ERROR);\n ret = 0;\n goto exit_loop;\n } else\n c->state = BIO_CONN_S_OK;\n break;\n case BIO_CONN_S_OK:\n ret = 1;\n goto exit_loop;\n default:\n goto exit_loop;\n }\n if (cb != NULL) {\n if ((ret = cb((BIO *)b, c->state, ret)) == 0)\n goto end;\n }\n }\n exit_loop:\n if (cb != NULL)\n ret = cb((BIO *)b, c->state, ret);\n end:\n return ret;\n}', 'int BIO_ADDRINFO_family(const BIO_ADDRINFO *bai)\n{\n if (bai != NULL)\n return bai->bai_family;\n return 0;\n}', 'int BIO_ADDRINFO_socktype(const BIO_ADDRINFO *bai)\n{\n if (bai != NULL)\n return bai->bai_socktype;\n return 0;\n}', 'int BIO_ADDRINFO_protocol(const BIO_ADDRINFO *bai)\n{\n if (bai != NULL) {\n if (bai->bai_protocol != 0)\n return bai->bai_protocol;\n#ifdef AF_UNIX\n if (bai->bai_family == AF_UNIX)\n return 0;\n#endif\n switch (bai->bai_socktype) {\n case SOCK_STREAM:\n return IPPROTO_TCP;\n case SOCK_DGRAM:\n return IPPROTO_UDP;\n default:\n break;\n }\n }\n return 0;\n}', 'int BIO_socket(int domain, int socktype, int protocol, int options)\n{\n int sock = -1;\n if (BIO_sock_init() != 1)\n return INVALID_SOCKET;\n sock = socket(domain, socktype, protocol);\n if (sock == -1) {\n ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),\n "calling socket()");\n BIOerr(BIO_F_BIO_SOCKET, BIO_R_UNABLE_TO_CREATE_SOCKET);\n return INVALID_SOCKET;\n }\n return sock;\n}', 'int BIO_sock_init(void)\n{\n# ifdef OPENSSL_SYS_WINDOWS\n static struct WSAData wsa_state;\n if (!wsa_init_done) {\n wsa_init_done = 1;\n memset(&wsa_state, 0, sizeof(wsa_state));\n if (WSAStartup(0x0202, &wsa_state) != 0) {\n ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),\n "calling wsastartup()");\n BIOerr(BIO_F_BIO_SOCK_INIT, BIO_R_WSASTARTUP);\n return -1;\n }\n }\n# endif\n# ifdef WATT32\n extern int _watt_do_exit;\n _watt_do_exit = 0;\n if (sock_init())\n return -1;\n# endif\n return 1;\n}'] |
536 | 0 | https://github.com/openssl/openssl/blob/3ad4af89cf7380aa94d1995e05e713d59e1c469a/crypto/o_str.c/#L303 | char *OPENSSL_buf2hexstr(const unsigned char *buffer, long len)
{
const static char hexdig[] = "0123456789ABCDEF";
char *tmp, *q;
const unsigned char *p;
int i;
if ((tmp = OPENSSL_malloc(len * 3 + 1)) == NULL) {
CRYPTOerr(CRYPTO_F_OPENSSL_BUF2HEXSTR, ERR_R_MALLOC_FAILURE);
return NULL;
}
q = tmp;
for (i = 0, p = buffer; i < len; i++, p++) {
*q++ = hexdig[(*p >> 4) & 0xf];
*q++ = hexdig[*p & 0xf];
*q++ = ':';
}
q[-1] = 0;
#ifdef CHARSET_EBCDIC
ebcdic2ascii(tmp, tmp, q - tmp - 1);
#endif
return tmp;
} | ['char *OPENSSL_buf2hexstr(const unsigned char *buffer, long len)\n{\n const static char hexdig[] = "0123456789ABCDEF";\n char *tmp, *q;\n const unsigned char *p;\n int i;\n if ((tmp = OPENSSL_malloc(len * 3 + 1)) == NULL) {\n CRYPTOerr(CRYPTO_F_OPENSSL_BUF2HEXSTR, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n q = tmp;\n for (i = 0, p = buffer; i < len; i++, p++) {\n *q++ = hexdig[(*p >> 4) & 0xf];\n *q++ = hexdig[*p & 0xf];\n *q++ = \':\';\n }\n q[-1] = 0;\n#ifdef CHARSET_EBCDIC\n ebcdic2ascii(tmp, tmp, q - tmp - 1);\n#endif\n return tmp;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num <= 0)\n return NULL;\n 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}'] |
537 | 0 | https://github.com/libav/libav/blob/44d16df413878588659dd8901bba016b5a869fd1/libavcodec/h264_slice.c/#L701 | static void implicit_weight_table(const H264Context *h, H264SliceContext *sl, int field)
{
int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;
for (i = 0; i < 2; i++) {
sl->pwt.luma_weight_flag[i] = 0;
sl->pwt.chroma_weight_flag[i] = 0;
}
if (field < 0) {
if (h->picture_structure == PICT_FRAME) {
cur_poc = h->cur_pic_ptr->poc;
} else {
cur_poc = h->cur_pic_ptr->field_poc[h->picture_structure - 1];
}
if (sl->ref_count[0] == 1 && sl->ref_count[1] == 1 && !FRAME_MBAFF(h) &&
sl->ref_list[0][0].poc + sl->ref_list[1][0].poc == 2 * cur_poc) {
sl->pwt.use_weight = 0;
sl->pwt.use_weight_chroma = 0;
return;
}
ref_start = 0;
ref_count0 = sl->ref_count[0];
ref_count1 = sl->ref_count[1];
} else {
cur_poc = h->cur_pic_ptr->field_poc[field];
ref_start = 16;
ref_count0 = 16 + 2 * sl->ref_count[0];
ref_count1 = 16 + 2 * sl->ref_count[1];
}
sl->pwt.use_weight = 2;
sl->pwt.use_weight_chroma = 2;
sl->pwt.luma_log2_weight_denom = 5;
sl->pwt.chroma_log2_weight_denom = 5;
for (ref0 = ref_start; ref0 < ref_count0; ref0++) {
int poc0 = sl->ref_list[0][ref0].poc;
for (ref1 = ref_start; ref1 < ref_count1; ref1++) {
int w = 32;
if (!sl->ref_list[0][ref0].parent->long_ref && !sl->ref_list[1][ref1].parent->long_ref) {
int poc1 = sl->ref_list[1][ref1].poc;
int td = av_clip_int8(poc1 - poc0);
if (td) {
int tb = av_clip_int8(cur_poc - poc0);
int tx = (16384 + (FFABS(td) >> 1)) / td;
int dist_scale_factor = (tb * tx + 32) >> 8;
if (dist_scale_factor >= -64 && dist_scale_factor <= 128)
w = 64 - dist_scale_factor;
}
}
if (field < 0) {
sl->pwt.implicit_weight[ref0][ref1][0] =
sl->pwt.implicit_weight[ref0][ref1][1] = w;
} else {
sl->pwt.implicit_weight[ref0][ref1][field] = w;
}
}
}
} | ['int ff_h264_decode_slice_header(H264Context *h, H264SliceContext *sl)\n{\n unsigned int first_mb_in_slice;\n unsigned int pps_id;\n int ret;\n unsigned int slice_type, tmp, i, j;\n int last_pic_structure, last_pic_droppable;\n int needs_reinit = 0;\n int field_pic_flag, bottom_field_flag;\n int frame_num, droppable, picture_structure;\n int mb_aff_frame = 0;\n h->qpel_put = h->h264qpel.put_h264_qpel_pixels_tab;\n h->qpel_avg = h->h264qpel.avg_h264_qpel_pixels_tab;\n first_mb_in_slice = get_ue_golomb(&sl->gb);\n if (first_mb_in_slice == 0) {\n if (h->current_slice && h->cur_pic_ptr && FIELD_PICTURE(h)) {\n ff_h264_field_end(h, sl, 1);\n }\n h->current_slice = 0;\n if (!h->first_field) {\n if (h->cur_pic_ptr && !h->droppable) {\n ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,\n h->picture_structure == PICT_BOTTOM_FIELD);\n }\n h->cur_pic_ptr = NULL;\n }\n }\n slice_type = get_ue_golomb_31(&sl->gb);\n if (slice_type > 9) {\n av_log(h->avctx, AV_LOG_ERROR,\n "slice type %d too large at %d\\n",\n slice_type, first_mb_in_slice);\n return AVERROR_INVALIDDATA;\n }\n if (slice_type > 4) {\n slice_type -= 5;\n sl->slice_type_fixed = 1;\n } else\n sl->slice_type_fixed = 0;\n slice_type = ff_h264_golomb_to_pict_type[slice_type];\n sl->slice_type = slice_type;\n sl->slice_type_nos = slice_type & 3;\n if (h->nal_unit_type == NAL_IDR_SLICE &&\n sl->slice_type_nos != AV_PICTURE_TYPE_I) {\n av_log(h->avctx, AV_LOG_ERROR, "A non-intra slice in an IDR NAL unit.\\n");\n return AVERROR_INVALIDDATA;\n }\n if (!h->setup_finished)\n h->pict_type = sl->slice_type;\n pps_id = get_ue_golomb(&sl->gb);\n if (pps_id >= MAX_PPS_COUNT) {\n av_log(h->avctx, AV_LOG_ERROR, "pps_id %u out of range\\n", pps_id);\n return AVERROR_INVALIDDATA;\n }\n if (!h->pps_buffers[pps_id]) {\n av_log(h->avctx, AV_LOG_ERROR,\n "non-existing PPS %u referenced\\n",\n pps_id);\n return AVERROR_INVALIDDATA;\n }\n if (!h->setup_finished) {\n h->pps = *h->pps_buffers[pps_id];\n } else if (h->dequant_coeff_pps != pps_id) {\n av_log(h->avctx, AV_LOG_ERROR, "PPS changed between slices\\n");\n return AVERROR_INVALIDDATA;\n }\n if (!h->sps_buffers[h->pps.sps_id]) {\n av_log(h->avctx, AV_LOG_ERROR,\n "non-existing SPS %u referenced\\n",\n h->pps.sps_id);\n return AVERROR_INVALIDDATA;\n }\n if (h->pps.sps_id != h->sps.sps_id ||\n h->sps_buffers[h->pps.sps_id]->new) {\n h->sps_buffers[h->pps.sps_id]->new = 0;\n h->sps = *h->sps_buffers[h->pps.sps_id];\n if (h->bit_depth_luma != h->sps.bit_depth_luma ||\n h->chroma_format_idc != h->sps.chroma_format_idc)\n needs_reinit = 1;\n if (h->flags & AV_CODEC_FLAG_LOW_DELAY ||\n (h->sps.bitstream_restriction_flag &&\n !h->sps.num_reorder_frames)) {\n if (h->avctx->has_b_frames > 1 || h->delayed_pic[0])\n av_log(h->avctx, AV_LOG_WARNING, "Delayed frames seen. "\n "Reenabling low delay requires a codec flush.\\n");\n else\n h->low_delay = 1;\n }\n if (h->avctx->has_b_frames < 2)\n h->avctx->has_b_frames = !h->low_delay;\n }\n if (!h->setup_finished) {\n h->avctx->profile = ff_h264_get_profile(&h->sps);\n h->avctx->level = h->sps.level_idc;\n h->avctx->refs = h->sps.ref_frame_count;\n if (h->mb_width != h->sps.mb_width ||\n h->mb_height != h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag))\n needs_reinit = 1;\n h->mb_width = h->sps.mb_width;\n h->mb_height = h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);\n h->mb_num = h->mb_width * h->mb_height;\n h->mb_stride = h->mb_width + 1;\n h->b_stride = h->mb_width * 4;\n h->chroma_y_shift = h->sps.chroma_format_idc <= 1;\n h->width = 16 * h->mb_width;\n h->height = 16 * h->mb_height;\n ret = init_dimensions(h);\n if (ret < 0)\n return ret;\n if (h->sps.video_signal_type_present_flag) {\n h->avctx->color_range = h->sps.full_range ? AVCOL_RANGE_JPEG\n : AVCOL_RANGE_MPEG;\n if (h->sps.colour_description_present_flag) {\n if (h->avctx->colorspace != h->sps.colorspace)\n needs_reinit = 1;\n h->avctx->color_primaries = h->sps.color_primaries;\n h->avctx->color_trc = h->sps.color_trc;\n h->avctx->colorspace = h->sps.colorspace;\n }\n }\n }\n if (h->context_initialized && needs_reinit) {\n h->context_initialized = 0;\n if (sl != h->slice_ctx) {\n av_log(h->avctx, AV_LOG_ERROR,\n "changing width %d -> %d / height %d -> %d on "\n "slice %d\\n",\n h->width, h->avctx->coded_width,\n h->height, h->avctx->coded_height,\n h->current_slice + 1);\n return AVERROR_INVALIDDATA;\n }\n ff_h264_flush_change(h);\n if ((ret = get_pixel_format(h)) < 0)\n return ret;\n h->avctx->pix_fmt = ret;\n av_log(h->avctx, AV_LOG_INFO, "Reinit context to %dx%d, "\n "pix_fmt: %d\\n", h->width, h->height, h->avctx->pix_fmt);\n if ((ret = h264_slice_header_init(h)) < 0) {\n av_log(h->avctx, AV_LOG_ERROR,\n "h264_slice_header_init() failed\\n");\n return ret;\n }\n }\n if (!h->context_initialized) {\n if (sl != h->slice_ctx) {\n av_log(h->avctx, AV_LOG_ERROR,\n "Cannot (re-)initialize context during parallel decoding.\\n");\n return AVERROR_PATCHWELCOME;\n }\n if ((ret = get_pixel_format(h)) < 0)\n return ret;\n h->avctx->pix_fmt = ret;\n if ((ret = h264_slice_header_init(h)) < 0) {\n av_log(h->avctx, AV_LOG_ERROR,\n "h264_slice_header_init() failed\\n");\n return ret;\n }\n }\n if (sl == h->slice_ctx && h->dequant_coeff_pps != pps_id) {\n h->dequant_coeff_pps = pps_id;\n ff_h264_init_dequant_tables(h);\n }\n frame_num = get_bits(&sl->gb, h->sps.log2_max_frame_num);\n if (!h->setup_finished)\n h->frame_num = frame_num;\n sl->mb_mbaff = 0;\n last_pic_structure = h->picture_structure;\n last_pic_droppable = h->droppable;\n droppable = h->nal_ref_idc == 0;\n if (h->sps.frame_mbs_only_flag) {\n picture_structure = PICT_FRAME;\n } else {\n field_pic_flag = get_bits1(&sl->gb);\n if (field_pic_flag) {\n bottom_field_flag = get_bits1(&sl->gb);\n picture_structure = PICT_TOP_FIELD + bottom_field_flag;\n } else {\n picture_structure = PICT_FRAME;\n mb_aff_frame = h->sps.mb_aff;\n }\n }\n if (!h->setup_finished) {\n h->droppable = droppable;\n h->picture_structure = picture_structure;\n h->mb_aff_frame = mb_aff_frame;\n }\n sl->mb_field_decoding_flag = h->picture_structure != PICT_FRAME;\n if (h->current_slice != 0) {\n if (last_pic_structure != picture_structure ||\n last_pic_droppable != droppable) {\n av_log(h->avctx, AV_LOG_ERROR,\n "Changing field mode (%d -> %d) between slices is not allowed\\n",\n last_pic_structure, h->picture_structure);\n return AVERROR_INVALIDDATA;\n } else if (!h->cur_pic_ptr) {\n av_log(h->avctx, AV_LOG_ERROR,\n "unset cur_pic_ptr on slice %d\\n",\n h->current_slice + 1);\n return AVERROR_INVALIDDATA;\n }\n } else {\n if (h->frame_num != h->prev_frame_num) {\n int unwrap_prev_frame_num = h->prev_frame_num;\n int max_frame_num = 1 << h->sps.log2_max_frame_num;\n if (unwrap_prev_frame_num > h->frame_num)\n unwrap_prev_frame_num -= max_frame_num;\n if ((h->frame_num - unwrap_prev_frame_num) > h->sps.ref_frame_count) {\n unwrap_prev_frame_num = (h->frame_num - h->sps.ref_frame_count) - 1;\n if (unwrap_prev_frame_num < 0)\n unwrap_prev_frame_num += max_frame_num;\n h->prev_frame_num = unwrap_prev_frame_num;\n }\n }\n if (h->first_field) {\n assert(h->cur_pic_ptr);\n assert(h->cur_pic_ptr->f->buf[0]);\n assert(h->cur_pic_ptr->reference != DELAYED_PIC_REF);\n if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {\n if (!last_pic_droppable && last_pic_structure != PICT_FRAME) {\n ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,\n last_pic_structure == PICT_TOP_FIELD);\n }\n } else {\n if (h->cur_pic_ptr->frame_num != h->frame_num) {\n if (!last_pic_droppable && last_pic_structure != PICT_FRAME) {\n ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,\n last_pic_structure == PICT_TOP_FIELD);\n }\n } else {\n if (!((last_pic_structure == PICT_TOP_FIELD &&\n h->picture_structure == PICT_BOTTOM_FIELD) ||\n (last_pic_structure == PICT_BOTTOM_FIELD &&\n h->picture_structure == PICT_TOP_FIELD))) {\n av_log(h->avctx, AV_LOG_ERROR,\n "Invalid field mode combination %d/%d\\n",\n last_pic_structure, h->picture_structure);\n h->picture_structure = last_pic_structure;\n h->droppable = last_pic_droppable;\n return AVERROR_INVALIDDATA;\n } else if (last_pic_droppable != h->droppable) {\n avpriv_request_sample(h->avctx,\n "Found reference and non-reference fields in the same frame, which");\n h->picture_structure = last_pic_structure;\n h->droppable = last_pic_droppable;\n return AVERROR_PATCHWELCOME;\n }\n }\n }\n }\n while (h->frame_num != h->prev_frame_num &&\n h->frame_num != (h->prev_frame_num + 1) % (1 << h->sps.log2_max_frame_num)) {\n H264Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;\n av_log(h->avctx, AV_LOG_DEBUG, "Frame num gap %d %d\\n",\n h->frame_num, h->prev_frame_num);\n ret = initialize_cur_frame(h);\n if (ret < 0) {\n h->first_field = 0;\n return ret;\n }\n h->prev_frame_num++;\n h->prev_frame_num %= 1 << h->sps.log2_max_frame_num;\n h->cur_pic_ptr->frame_num = h->prev_frame_num;\n ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 0);\n ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 1);\n ret = ff_generate_sliding_window_mmcos(h, 1);\n if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))\n return ret;\n ret = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);\n if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))\n return ret;\n if (h->short_ref_count) {\n if (prev &&\n h->short_ref[0]->f->width == prev->f->width &&\n h->short_ref[0]->f->height == prev->f->height &&\n h->short_ref[0]->f->format == prev->f->format) {\n av_image_copy(h->short_ref[0]->f->data,\n h->short_ref[0]->f->linesize,\n (const uint8_t **)prev->f->data,\n prev->f->linesize,\n prev->f->format,\n h->mb_width * 16,\n h->mb_height * 16);\n h->short_ref[0]->poc = prev->poc + 2;\n }\n h->short_ref[0]->frame_num = h->prev_frame_num;\n }\n }\n if (h->first_field) {\n assert(h->cur_pic_ptr);\n assert(h->cur_pic_ptr->f->buf[0]);\n assert(h->cur_pic_ptr->reference != DELAYED_PIC_REF);\n if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {\n h->cur_pic_ptr = NULL;\n h->first_field = FIELD_PICTURE(h);\n } else {\n if (h->cur_pic_ptr->frame_num != h->frame_num) {\n h->first_field = 1;\n h->cur_pic_ptr = NULL;\n } else {\n h->first_field = 0;\n }\n }\n } else {\n h->first_field = FIELD_PICTURE(h);\n }\n if (!FIELD_PICTURE(h) || h->first_field) {\n if (h264_frame_start(h) < 0) {\n h->first_field = 0;\n return AVERROR_INVALIDDATA;\n }\n } else {\n release_unused_pictures(h, 0);\n }\n }\n assert(h->mb_num == h->mb_width * h->mb_height);\n if (first_mb_in_slice << FIELD_OR_MBAFF_PICTURE(h) >= h->mb_num ||\n first_mb_in_slice >= h->mb_num) {\n av_log(h->avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\\n");\n return AVERROR_INVALIDDATA;\n }\n sl->resync_mb_x = sl->mb_x = first_mb_in_slice % h->mb_width;\n sl->resync_mb_y = sl->mb_y = (first_mb_in_slice / h->mb_width) <<\n FIELD_OR_MBAFF_PICTURE(h);\n if (h->picture_structure == PICT_BOTTOM_FIELD)\n sl->resync_mb_y = sl->mb_y = sl->mb_y + 1;\n assert(sl->mb_y < h->mb_height);\n if (h->picture_structure == PICT_FRAME) {\n h->curr_pic_num = h->frame_num;\n h->max_pic_num = 1 << h->sps.log2_max_frame_num;\n } else {\n h->curr_pic_num = 2 * h->frame_num + 1;\n h->max_pic_num = 1 << (h->sps.log2_max_frame_num + 1);\n }\n if (h->nal_unit_type == NAL_IDR_SLICE)\n get_ue_golomb(&sl->gb);\n if (h->sps.poc_type == 0) {\n int poc_lsb = get_bits(&sl->gb, h->sps.log2_max_poc_lsb);\n if (!h->setup_finished)\n h->poc_lsb = poc_lsb;\n if (h->pps.pic_order_present == 1 && h->picture_structure == PICT_FRAME) {\n int delta_poc_bottom = get_se_golomb(&sl->gb);\n if (!h->setup_finished)\n h->delta_poc_bottom = delta_poc_bottom;\n }\n }\n if (h->sps.poc_type == 1 && !h->sps.delta_pic_order_always_zero_flag) {\n int delta_poc = get_se_golomb(&sl->gb);\n if (!h->setup_finished)\n h->delta_poc[0] = delta_poc;\n if (h->pps.pic_order_present == 1 && h->picture_structure == PICT_FRAME) {\n delta_poc = get_se_golomb(&sl->gb);\n if (!h->setup_finished)\n h->delta_poc[1] = delta_poc;\n }\n }\n if (!h->setup_finished)\n ff_init_poc(h, h->cur_pic_ptr->field_poc, &h->cur_pic_ptr->poc);\n if (h->pps.redundant_pic_cnt_present)\n sl->redundant_pic_count = get_ue_golomb(&sl->gb);\n if (sl->slice_type_nos == AV_PICTURE_TYPE_B)\n sl->direct_spatial_mv_pred = get_bits1(&sl->gb);\n ret = ff_h264_parse_ref_count(&sl->list_count, sl->ref_count,\n &sl->gb, &h->pps, sl->slice_type_nos,\n h->picture_structure);\n if (ret < 0)\n return ret;\n if (sl->slice_type_nos != AV_PICTURE_TYPE_I) {\n ret = ff_h264_decode_ref_pic_list_reordering(h, sl);\n if (ret < 0) {\n sl->ref_count[1] = sl->ref_count[0] = 0;\n return ret;\n }\n }\n if ((h->pps.weighted_pred && sl->slice_type_nos == AV_PICTURE_TYPE_P) ||\n (h->pps.weighted_bipred_idc == 1 &&\n sl->slice_type_nos == AV_PICTURE_TYPE_B))\n ff_h264_pred_weight_table(&sl->gb, &h->sps, sl->ref_count,\n sl->slice_type_nos, &sl->pwt);\n else if (h->pps.weighted_bipred_idc == 2 &&\n sl->slice_type_nos == AV_PICTURE_TYPE_B) {\n implicit_weight_table(h, sl, -1);\n } else {\n sl->pwt.use_weight = 0;\n for (i = 0; i < 2; i++) {\n sl->pwt.luma_weight_flag[i] = 0;\n sl->pwt.chroma_weight_flag[i] = 0;\n }\n }\n if (h->nal_ref_idc) {\n ret = ff_h264_decode_ref_pic_marking(h, &sl->gb,\n !(h->avctx->active_thread_type & FF_THREAD_FRAME) ||\n h->current_slice == 0);\n if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))\n return AVERROR_INVALIDDATA;\n }\n if (FRAME_MBAFF(h)) {\n ff_h264_fill_mbaff_ref_list(h, sl);\n if (h->pps.weighted_bipred_idc == 2 && sl->slice_type_nos == AV_PICTURE_TYPE_B) {\n implicit_weight_table(h, sl, 0);\n implicit_weight_table(h, sl, 1);\n }\n }\n if (sl->slice_type_nos == AV_PICTURE_TYPE_B && !sl->direct_spatial_mv_pred)\n ff_h264_direct_dist_scale_factor(h, sl);\n ff_h264_direct_ref_list_init(h, sl);\n if (sl->slice_type_nos != AV_PICTURE_TYPE_I && h->pps.cabac) {\n tmp = get_ue_golomb_31(&sl->gb);\n if (tmp > 2) {\n av_log(h->avctx, AV_LOG_ERROR, "cabac_init_idc %u overflow\\n", tmp);\n return AVERROR_INVALIDDATA;\n }\n sl->cabac_init_idc = tmp;\n }\n sl->last_qscale_diff = 0;\n tmp = h->pps.init_qp + get_se_golomb(&sl->gb);\n if (tmp > 51 + 6 * (h->sps.bit_depth_luma - 8)) {\n av_log(h->avctx, AV_LOG_ERROR, "QP %u out of range\\n", tmp);\n return AVERROR_INVALIDDATA;\n }\n sl->qscale = tmp;\n sl->chroma_qp[0] = get_chroma_qp(h, 0, sl->qscale);\n sl->chroma_qp[1] = get_chroma_qp(h, 1, sl->qscale);\n if (sl->slice_type == AV_PICTURE_TYPE_SP)\n get_bits1(&sl->gb);\n if (sl->slice_type == AV_PICTURE_TYPE_SP ||\n sl->slice_type == AV_PICTURE_TYPE_SI)\n get_se_golomb(&sl->gb);\n sl->deblocking_filter = 1;\n sl->slice_alpha_c0_offset = 0;\n sl->slice_beta_offset = 0;\n if (h->pps.deblocking_filter_parameters_present) {\n tmp = get_ue_golomb_31(&sl->gb);\n if (tmp > 2) {\n av_log(h->avctx, AV_LOG_ERROR,\n "deblocking_filter_idc %u out of range\\n", tmp);\n return AVERROR_INVALIDDATA;\n }\n sl->deblocking_filter = tmp;\n if (sl->deblocking_filter < 2)\n sl->deblocking_filter ^= 1;\n if (sl->deblocking_filter) {\n sl->slice_alpha_c0_offset = get_se_golomb(&sl->gb) * 2;\n sl->slice_beta_offset = get_se_golomb(&sl->gb) * 2;\n if (sl->slice_alpha_c0_offset > 12 ||\n sl->slice_alpha_c0_offset < -12 ||\n sl->slice_beta_offset > 12 ||\n sl->slice_beta_offset < -12) {\n av_log(h->avctx, AV_LOG_ERROR,\n "deblocking filter parameters %d %d out of range\\n",\n sl->slice_alpha_c0_offset, sl->slice_beta_offset);\n return AVERROR_INVALIDDATA;\n }\n }\n }\n if (h->avctx->skip_loop_filter >= AVDISCARD_ALL ||\n (h->avctx->skip_loop_filter >= AVDISCARD_NONKEY &&\n sl->slice_type_nos != AV_PICTURE_TYPE_I) ||\n (h->avctx->skip_loop_filter >= AVDISCARD_BIDIR &&\n sl->slice_type_nos == AV_PICTURE_TYPE_B) ||\n (h->avctx->skip_loop_filter >= AVDISCARD_NONREF &&\n h->nal_ref_idc == 0))\n sl->deblocking_filter = 0;\n if (sl->deblocking_filter == 1 && h->max_contexts > 1) {\n if (h->avctx->flags2 & AV_CODEC_FLAG2_FAST) {\n sl->deblocking_filter = 2;\n } else {\n h->max_contexts = 1;\n if (!h->single_decode_warning) {\n av_log(h->avctx, AV_LOG_INFO,\n "Cannot parallelize deblocking type 1, decoding such frames in sequential order\\n");\n h->single_decode_warning = 1;\n }\n if (sl != h->slice_ctx) {\n av_log(h->avctx, AV_LOG_ERROR,\n "Deblocking switched inside frame.\\n");\n return 1;\n }\n }\n }\n sl->qp_thresh = 15 -\n FFMIN(sl->slice_alpha_c0_offset, sl->slice_beta_offset) -\n FFMAX3(0,\n h->pps.chroma_qp_index_offset[0],\n h->pps.chroma_qp_index_offset[1]) +\n 6 * (h->sps.bit_depth_luma - 8);\n sl->slice_num = ++h->current_slice;\n if (sl->slice_num >= MAX_SLICES) {\n av_log(h->avctx, AV_LOG_ERROR,\n "Too many slices, increase MAX_SLICES and recompile\\n");\n }\n for (j = 0; j < 2; j++) {\n int id_list[16];\n int *ref2frm = sl->ref2frm[sl->slice_num & (MAX_SLICES - 1)][j];\n for (i = 0; i < 16; i++) {\n id_list[i] = 60;\n if (j < sl->list_count && i < sl->ref_count[j] &&\n sl->ref_list[j][i].parent->f->buf[0]) {\n int k;\n AVBuffer *buf = sl->ref_list[j][i].parent->f->buf[0]->buffer;\n for (k = 0; k < h->short_ref_count; k++)\n if (h->short_ref[k]->f->buf[0]->buffer == buf) {\n id_list[i] = k;\n break;\n }\n for (k = 0; k < h->long_ref_count; k++)\n if (h->long_ref[k] && h->long_ref[k]->f->buf[0]->buffer == buf) {\n id_list[i] = h->short_ref_count + k;\n break;\n }\n }\n }\n ref2frm[0] =\n ref2frm[1] = -1;\n for (i = 0; i < 16; i++)\n ref2frm[i + 2] = 4 * id_list[i] + (sl->ref_list[j][i].reference & 3);\n ref2frm[18 + 0] =\n ref2frm[18 + 1] = -1;\n for (i = 16; i < 48; i++)\n ref2frm[i + 4] = 4 * id_list[(i - 16) >> 1] +\n (sl->ref_list[j][i].reference & 3);\n }\n if (h->avctx->debug & FF_DEBUG_PICT_INFO) {\n av_log(h->avctx, AV_LOG_DEBUG,\n "slice:%d %s mb:%d %c%s%s pps:%u frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d:%d:%d weight:%d%s %s\\n",\n sl->slice_num,\n (h->picture_structure == PICT_FRAME ? "F" : h->picture_structure == PICT_TOP_FIELD ? "T" : "B"),\n first_mb_in_slice,\n av_get_picture_type_char(sl->slice_type),\n sl->slice_type_fixed ? " fix" : "",\n h->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",\n pps_id, h->frame_num,\n h->cur_pic_ptr->field_poc[0],\n h->cur_pic_ptr->field_poc[1],\n sl->ref_count[0], sl->ref_count[1],\n sl->qscale,\n sl->deblocking_filter,\n sl->slice_alpha_c0_offset, sl->slice_beta_offset,\n sl->pwt.use_weight,\n sl->pwt.use_weight == 1 && sl->pwt.use_weight_chroma ? "c" : "",\n sl->slice_type == AV_PICTURE_TYPE_B ? (sl->direct_spatial_mv_pred ? "SPAT" : "TEMP") : "");\n }\n return 0;\n}', 'static void implicit_weight_table(const H264Context *h, H264SliceContext *sl, int field)\n{\n int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;\n for (i = 0; i < 2; i++) {\n sl->pwt.luma_weight_flag[i] = 0;\n sl->pwt.chroma_weight_flag[i] = 0;\n }\n if (field < 0) {\n if (h->picture_structure == PICT_FRAME) {\n cur_poc = h->cur_pic_ptr->poc;\n } else {\n cur_poc = h->cur_pic_ptr->field_poc[h->picture_structure - 1];\n }\n if (sl->ref_count[0] == 1 && sl->ref_count[1] == 1 && !FRAME_MBAFF(h) &&\n sl->ref_list[0][0].poc + sl->ref_list[1][0].poc == 2 * cur_poc) {\n sl->pwt.use_weight = 0;\n sl->pwt.use_weight_chroma = 0;\n return;\n }\n ref_start = 0;\n ref_count0 = sl->ref_count[0];\n ref_count1 = sl->ref_count[1];\n } else {\n cur_poc = h->cur_pic_ptr->field_poc[field];\n ref_start = 16;\n ref_count0 = 16 + 2 * sl->ref_count[0];\n ref_count1 = 16 + 2 * sl->ref_count[1];\n }\n sl->pwt.use_weight = 2;\n sl->pwt.use_weight_chroma = 2;\n sl->pwt.luma_log2_weight_denom = 5;\n sl->pwt.chroma_log2_weight_denom = 5;\n for (ref0 = ref_start; ref0 < ref_count0; ref0++) {\n int poc0 = sl->ref_list[0][ref0].poc;\n for (ref1 = ref_start; ref1 < ref_count1; ref1++) {\n int w = 32;\n if (!sl->ref_list[0][ref0].parent->long_ref && !sl->ref_list[1][ref1].parent->long_ref) {\n int poc1 = sl->ref_list[1][ref1].poc;\n int td = av_clip_int8(poc1 - poc0);\n if (td) {\n int tb = av_clip_int8(cur_poc - poc0);\n int tx = (16384 + (FFABS(td) >> 1)) / td;\n int dist_scale_factor = (tb * tx + 32) >> 8;\n if (dist_scale_factor >= -64 && dist_scale_factor <= 128)\n w = 64 - dist_scale_factor;\n }\n }\n if (field < 0) {\n sl->pwt.implicit_weight[ref0][ref1][0] =\n sl->pwt.implicit_weight[ref0][ref1][1] = w;\n } else {\n sl->pwt.implicit_weight[ref0][ref1][field] = w;\n }\n }\n }\n}'] |
538 | 0 | https://github.com/openssl/openssl/blob/e02c519cd32a55e6ad39a0cfbeeda775f9115f28/crypto/bn/bn_lib.c/#L365 | int BN_set_word(BIGNUM *a, BN_ULONG w)
{
bn_check_top(a);
if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
return 0;
a->neg = 0;
a->d[0] = w;
a->top = (w ? 1 : 0);
a->flags &= ~BN_FLG_FIXED_TOP;
bn_check_top(a);
return 1;
} | ['static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)\n{\n BIGNUM *r1, *m1, *vrfy, *r2, *m[RSA_MAX_PRIME_NUM - 2];\n int ret = 0, i, ex_primes = 0, smooth = 0;\n RSA_PRIME_INFO *pinfo;\n BN_CTX_start(ctx);\n r1 = BN_CTX_get(ctx);\n r2 = BN_CTX_get(ctx);\n m1 = BN_CTX_get(ctx);\n vrfy = BN_CTX_get(ctx);\n if (vrfy == NULL)\n goto err;\n if (rsa->version == RSA_ASN1_VERSION_MULTI\n && ((ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) <= 0\n || ex_primes > RSA_MAX_PRIME_NUM - 2))\n goto err;\n if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {\n BIGNUM *factor = BN_new();\n if (factor == NULL)\n goto err;\n if (!(BN_with_flags(factor, rsa->p, BN_FLG_CONSTTIME),\n BN_MONT_CTX_set_locked(&rsa->_method_mod_p, rsa->lock,\n factor, ctx))\n || !(BN_with_flags(factor, rsa->q, BN_FLG_CONSTTIME),\n BN_MONT_CTX_set_locked(&rsa->_method_mod_q, rsa->lock,\n factor, ctx))) {\n BN_free(factor);\n goto err;\n }\n for (i = 0; i < ex_primes; i++) {\n pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);\n BN_with_flags(factor, pinfo->r, BN_FLG_CONSTTIME);\n if (!BN_MONT_CTX_set_locked(&pinfo->m, rsa->lock, factor, ctx)) {\n BN_free(factor);\n goto err;\n }\n }\n BN_free(factor);\n smooth = (ex_primes == 0)\n && (rsa->meth->bn_mod_exp == BN_mod_exp_mont)\n && (BN_num_bits(rsa->q) == BN_num_bits(rsa->p));\n }\n if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)\n if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,\n rsa->n, ctx))\n goto err;\n if (smooth) {\n if (\n !bn_from_mont_fixed_top(m1, I, rsa->_method_mod_q, ctx)\n || !bn_to_mont_fixed_top(m1, m1, rsa->_method_mod_q, ctx)\n || !BN_mod_exp_mont_consttime(m1, m1, rsa->dmq1, rsa->q, ctx,\n rsa->_method_mod_q)\n || !bn_from_mont_fixed_top(r1, I, rsa->_method_mod_p, ctx)\n || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)\n || !BN_mod_exp_mont_consttime(r1, r1, rsa->dmp1, rsa->p, ctx,\n rsa->_method_mod_p)\n || !bn_mod_sub_fixed_top(r1, r1, m1, rsa->p)\n || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)\n || !bn_mul_mont_fixed_top(r1, r1, rsa->iqmp, rsa->_method_mod_p,\n ctx)\n || !bn_mul_fixed_top(r0, r1, rsa->q, ctx)\n || !bn_mod_add_fixed_top(r0, r0, m1, rsa->n))\n goto err;\n goto tail;\n }\n {\n BIGNUM *c = BN_new();\n if (c == NULL)\n goto err;\n BN_with_flags(c, I, BN_FLG_CONSTTIME);\n if (!BN_mod(r1, c, rsa->q, ctx)) {\n BN_free(c);\n goto err;\n }\n {\n BIGNUM *dmq1 = BN_new();\n if (dmq1 == NULL) {\n BN_free(c);\n goto err;\n }\n BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);\n if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx,\n rsa->_method_mod_q)) {\n BN_free(c);\n BN_free(dmq1);\n goto err;\n }\n BN_free(dmq1);\n }\n if (!BN_mod(r1, c, rsa->p, ctx)) {\n BN_free(c);\n goto err;\n }\n BN_free(c);\n }\n {\n BIGNUM *dmp1 = BN_new();\n if (dmp1 == NULL)\n goto err;\n BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);\n if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx,\n rsa->_method_mod_p)) {\n BN_free(dmp1);\n goto err;\n }\n BN_free(dmp1);\n }\n if (ex_primes > 0) {\n BIGNUM *di = BN_new(), *cc = BN_new();\n if (cc == NULL || di == NULL) {\n BN_free(cc);\n BN_free(di);\n goto err;\n }\n for (i = 0; i < ex_primes; i++) {\n if ((m[i] = BN_CTX_get(ctx)) == NULL) {\n BN_free(cc);\n BN_free(di);\n goto err;\n }\n pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);\n BN_with_flags(cc, I, BN_FLG_CONSTTIME);\n BN_with_flags(di, pinfo->d, BN_FLG_CONSTTIME);\n if (!BN_mod(r1, cc, pinfo->r, ctx)) {\n BN_free(cc);\n BN_free(di);\n goto err;\n }\n if (!rsa->meth->bn_mod_exp(m[i], r1, di, pinfo->r, ctx, pinfo->m)) {\n BN_free(cc);\n BN_free(di);\n goto err;\n }\n }\n BN_free(cc);\n BN_free(di);\n }\n if (!BN_sub(r0, r0, m1))\n goto err;\n if (BN_is_negative(r0))\n if (!BN_add(r0, r0, rsa->p))\n goto err;\n if (!BN_mul(r1, r0, rsa->iqmp, ctx))\n goto err;\n {\n BIGNUM *pr1 = BN_new();\n if (pr1 == NULL)\n goto err;\n BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);\n if (!BN_mod(r0, pr1, rsa->p, ctx)) {\n BN_free(pr1);\n goto err;\n }\n BN_free(pr1);\n }\n if (BN_is_negative(r0))\n if (!BN_add(r0, r0, rsa->p))\n goto err;\n if (!BN_mul(r1, r0, rsa->q, ctx))\n goto err;\n if (!BN_add(r0, r1, m1))\n goto err;\n if (ex_primes > 0) {\n BIGNUM *pr2 = BN_new();\n if (pr2 == NULL)\n goto err;\n for (i = 0; i < ex_primes; i++) {\n pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);\n if (!BN_sub(r1, m[i], r0)) {\n BN_free(pr2);\n goto err;\n }\n if (!BN_mul(r2, r1, pinfo->t, ctx)) {\n BN_free(pr2);\n goto err;\n }\n BN_with_flags(pr2, r2, BN_FLG_CONSTTIME);\n if (!BN_mod(r1, pr2, pinfo->r, ctx)) {\n BN_free(pr2);\n goto err;\n }\n if (BN_is_negative(r1))\n if (!BN_add(r1, r1, pinfo->r)) {\n BN_free(pr2);\n goto err;\n }\n if (!BN_mul(r1, r1, pinfo->pp, ctx)) {\n BN_free(pr2);\n goto err;\n }\n if (!BN_add(r0, r0, r1)) {\n BN_free(pr2);\n goto err;\n }\n }\n BN_free(pr2);\n }\n tail:\n if (rsa->e && rsa->n) {\n if (rsa->meth->bn_mod_exp == BN_mod_exp_mont) {\n if (!BN_mod_exp_mont(vrfy, r0, rsa->e, rsa->n, ctx,\n rsa->_method_mod_n))\n goto err;\n } else {\n bn_correct_top(r0);\n if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,\n rsa->_method_mod_n))\n goto err;\n }\n if (!BN_sub(vrfy, vrfy, I))\n goto err;\n if (BN_is_zero(vrfy)) {\n bn_correct_top(r0);\n ret = 1;\n goto err;\n }\n if (!BN_mod(vrfy, vrfy, rsa->n, ctx))\n goto err;\n if (BN_is_negative(vrfy))\n if (!BN_add(vrfy, vrfy, rsa->n))\n goto err;\n if (!BN_is_zero(vrfy)) {\n BIGNUM *d = BN_new();\n if (d == NULL)\n goto err;\n BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);\n if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx,\n rsa->_method_mod_n)) {\n BN_free(d);\n goto err;\n }\n BN_free(d);\n }\n }\n bn_correct_top(r0);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', '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}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\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_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}'] |
539 | 0 | https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/test/bntest.c/#L1475 | int test_gf2m_mod_div(BIO *bp, BN_CTX *ctx)
{
BIGNUM *a, *b[2], *c, *d, *e, *f;
int i, j, ret = 0;
int p0[] = { 163, 7, 6, 3, 0, -1 };
int p1[] = { 193, 15, 0, -1 };
a = BN_new();
b[0] = BN_new();
b[1] = BN_new();
c = BN_new();
d = BN_new();
e = BN_new();
f = BN_new();
BN_GF2m_arr2poly(p0, b[0]);
BN_GF2m_arr2poly(p1, b[1]);
for (i = 0; i < num0; i++) {
BN_bntest_rand(a, 512, 0, 0);
BN_bntest_rand(c, 512, 0, 0);
for (j = 0; j < 2; j++) {
BN_GF2m_mod_div(d, a, c, b[j], ctx);
BN_GF2m_mod_mul(e, d, c, b[j], ctx);
BN_GF2m_mod_div(f, a, e, b[j], ctx);
if (!BN_is_one(f)) {
fprintf(stderr, "GF(2^m) modular division test failed!\n");
goto err;
}
}
}
ret = 1;
err:
BN_free(a);
BN_free(b[0]);
BN_free(b[1]);
BN_free(c);
BN_free(d);
BN_free(e);
BN_free(f);
return ret;
} | ['int test_gf2m_mod_div(BIO *bp, BN_CTX *ctx)\n{\n BIGNUM *a, *b[2], *c, *d, *e, *f;\n int i, j, ret = 0;\n int p0[] = { 163, 7, 6, 3, 0, -1 };\n int p1[] = { 193, 15, 0, -1 };\n a = BN_new();\n b[0] = BN_new();\n b[1] = BN_new();\n c = BN_new();\n d = BN_new();\n e = BN_new();\n f = BN_new();\n BN_GF2m_arr2poly(p0, b[0]);\n BN_GF2m_arr2poly(p1, b[1]);\n for (i = 0; i < num0; i++) {\n BN_bntest_rand(a, 512, 0, 0);\n BN_bntest_rand(c, 512, 0, 0);\n for (j = 0; j < 2; j++) {\n BN_GF2m_mod_div(d, a, c, b[j], ctx);\n BN_GF2m_mod_mul(e, d, c, b[j], ctx);\n BN_GF2m_mod_div(f, a, e, b[j], ctx);\n if (!BN_is_one(f)) {\n fprintf(stderr, "GF(2^m) modular division test failed!\\n");\n goto err;\n }\n }\n }\n ret = 1;\n err:\n BN_free(a);\n BN_free(b[0]);\n BN_free(b[1]);\n BN_free(c);\n BN_free(d);\n BN_free(e);\n BN_free(f);\n return ret;\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#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}', '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}'] |
540 | 0 | https://github.com/openssl/openssl/blob/3fd5ece39b59d938d0cc84b8e5148d19044d15cf/test/ssltest_old.c/#L350 | static int cb_server_alpn(SSL *s, const unsigned char **out,
unsigned char *outlen, const unsigned char *in,
unsigned int inlen, void *arg)
{
unsigned char *protos;
size_t protos_len;
char* alpn_str = arg;
protos = next_protos_parse(&protos_len, alpn_str);
if (protos == NULL) {
fprintf(stderr, "failed to parser ALPN server protocol string: %s\n",
alpn_str);
abort();
}
if (SSL_select_next_proto
((unsigned char **)out, outlen, protos, protos_len, in,
inlen) != OPENSSL_NPN_NEGOTIATED) {
OPENSSL_free(protos);
return SSL_TLSEXT_ERR_NOACK;
}
alpn_selected = OPENSSL_malloc(*outlen);
memcpy(alpn_selected, *out, *outlen);
*out = alpn_selected;
OPENSSL_free(protos);
return SSL_TLSEXT_ERR_OK;
} | ['static int cb_server_alpn(SSL *s, const unsigned char **out,\n unsigned char *outlen, const unsigned char *in,\n unsigned int inlen, void *arg)\n{\n unsigned char *protos;\n size_t protos_len;\n char* alpn_str = arg;\n protos = next_protos_parse(&protos_len, alpn_str);\n if (protos == NULL) {\n fprintf(stderr, "failed to parser ALPN server protocol string: %s\\n",\n alpn_str);\n abort();\n }\n if (SSL_select_next_proto\n ((unsigned char **)out, outlen, protos, protos_len, in,\n inlen) != OPENSSL_NPN_NEGOTIATED) {\n OPENSSL_free(protos);\n return SSL_TLSEXT_ERR_NOACK;\n }\n alpn_selected = OPENSSL_malloc(*outlen);\n memcpy(alpn_selected, *out, *outlen);\n *out = alpn_selected;\n OPENSSL_free(protos);\n return SSL_TLSEXT_ERR_OK;\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}'] |
541 | 0 | https://github.com/libav/libav/blob/2c8077621b6466da205ba26fd20a9c906bb71893/libavformat/oggdec.c/#L190 | static int
ogg_new_buf(struct ogg *ogg, int idx)
{
struct ogg_stream *os = ogg->streams + idx;
uint8_t *nb = av_malloc(os->bufsize);
int size = os->bufpos - os->pstart;
if(os->buf){
memcpy(nb, os->buf + os->pstart, size);
av_free(os->buf);
}
os->buf = nb;
os->bufpos = size;
os->pstart = 0;
return 0;
} | ['static int\nogg_new_buf(struct ogg *ogg, int idx)\n{\n struct ogg_stream *os = ogg->streams + idx;\n uint8_t *nb = av_malloc(os->bufsize);\n int size = os->bufpos - os->pstart;\n if(os->buf){\n memcpy(nb, os->buf + os->pstart, size);\n av_free(os->buf);\n }\n os->buf = nb;\n os->bufpos = size;\n os->pstart = 0;\n return 0;\n}', 'void *av_malloc(unsigned int size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-16) )\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size+16);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&15) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr,16,size))\n ptr = NULL;\n#elif HAVE_MEMALIGN\n ptr = memalign(16,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}'] |
542 | 1 | https://github.com/libav/libav/blob/1a07df31128da3a0020b66502399989b91770d44/libavcodec/mpegvideo_enc.c/#L2925 | static int encode_thread(AVCodecContext *c, void *arg){
MpegEncContext *s= *(void**)arg;
int mb_x, mb_y, pdif = 0;
int chr_h= 16>>s->chroma_y_shift;
int i, j;
MpegEncContext best_s, backup_s;
uint8_t bit_buf[2][MAX_MB_BYTES];
uint8_t bit_buf2[2][MAX_MB_BYTES];
uint8_t bit_buf_tex[2][MAX_MB_BYTES];
PutBitContext pb[2], pb2[2], tex_pb[2];
for(i=0; i<2; i++){
init_put_bits(&pb [i], bit_buf [i], MAX_MB_BYTES);
init_put_bits(&pb2 [i], bit_buf2 [i], MAX_MB_BYTES);
init_put_bits(&tex_pb[i], bit_buf_tex[i], MAX_MB_BYTES);
}
s->last_bits= put_bits_count(&s->pb);
s->mv_bits=0;
s->misc_bits=0;
s->i_tex_bits=0;
s->p_tex_bits=0;
s->i_count=0;
s->f_count=0;
s->b_count=0;
s->skip_count=0;
for(i=0; i<3; i++){
s->last_dc[i] = 128 << s->intra_dc_precision;
s->current_picture.f->error[i] = 0;
}
s->mb_skip_run = 0;
memset(s->last_mv, 0, sizeof(s->last_mv));
s->last_mv_dir = 0;
switch(s->codec_id){
case AV_CODEC_ID_H263:
case AV_CODEC_ID_H263P:
case AV_CODEC_ID_FLV1:
if (CONFIG_H263_ENCODER)
s->gob_index = ff_h263_get_gob_height(s);
break;
case AV_CODEC_ID_MPEG4:
if(CONFIG_MPEG4_ENCODER && s->partitioned_frame)
ff_mpeg4_init_partitions(s);
break;
}
s->resync_mb_x=0;
s->resync_mb_y=0;
s->first_slice_line = 1;
s->ptr_lastgob = s->pb.buf;
for(mb_y= s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
s->mb_x=0;
s->mb_y= mb_y;
ff_set_qscale(s, s->qscale);
ff_init_block_index(s);
for(mb_x=0; mb_x < s->mb_width; mb_x++) {
int xy= mb_y*s->mb_stride + mb_x;
int mb_type= s->mb_type[xy];
int dmin= INT_MAX;
int dir;
if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < MAX_MB_BYTES){
av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
return -1;
}
if(s->data_partitioning){
if( s->pb2 .buf_end - s->pb2 .buf - (put_bits_count(&s-> pb2)>>3) < MAX_MB_BYTES
|| s->tex_pb.buf_end - s->tex_pb.buf - (put_bits_count(&s->tex_pb )>>3) < MAX_MB_BYTES){
av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
return -1;
}
}
s->mb_x = mb_x;
s->mb_y = mb_y;
ff_update_block_index(s);
if(CONFIG_H261_ENCODER && s->codec_id == AV_CODEC_ID_H261){
ff_h261_reorder_mb_index(s);
xy= s->mb_y*s->mb_stride + s->mb_x;
mb_type= s->mb_type[xy];
}
if(s->rtp_mode){
int current_packet_size, is_gob_start;
current_packet_size= ((put_bits_count(&s->pb)+7)>>3) - (s->ptr_lastgob - s->pb.buf);
is_gob_start= s->avctx->rtp_payload_size && current_packet_size >= s->avctx->rtp_payload_size && mb_y + mb_x>0;
if(s->start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
switch(s->codec_id){
case AV_CODEC_ID_H263:
case AV_CODEC_ID_H263P:
if(!s->h263_slice_structured)
if(s->mb_x || s->mb_y%s->gob_index) is_gob_start=0;
break;
case AV_CODEC_ID_MPEG2VIDEO:
if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
case AV_CODEC_ID_MPEG1VIDEO:
if(s->mb_skip_run) is_gob_start=0;
break;
}
if(is_gob_start){
if(s->start_mb_y != mb_y || mb_x!=0){
write_slice_end(s);
if(CONFIG_MPEG4_ENCODER && s->codec_id==AV_CODEC_ID_MPEG4 && s->partitioned_frame){
ff_mpeg4_init_partitions(s);
}
}
assert((put_bits_count(&s->pb)&7) == 0);
current_packet_size= put_bits_ptr(&s->pb) - s->ptr_lastgob;
if (s->error_rate && s->resync_mb_x + s->resync_mb_y > 0) {
int r= put_bits_count(&s->pb)/8 + s->picture_number + 16 + s->mb_x + s->mb_y;
int d = 100 / s->error_rate;
if(r % d == 0){
current_packet_size=0;
s->pb.buf_ptr= s->ptr_lastgob;
assert(put_bits_ptr(&s->pb) == s->ptr_lastgob);
}
}
if (s->avctx->rtp_callback){
int number_mb = (mb_y - s->resync_mb_y)*s->mb_width + mb_x - s->resync_mb_x;
s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, current_packet_size, number_mb);
}
update_mb_info(s, 1);
switch(s->codec_id){
case AV_CODEC_ID_MPEG4:
if (CONFIG_MPEG4_ENCODER) {
ff_mpeg4_encode_video_packet_header(s);
ff_mpeg4_clean_buffers(s);
}
break;
case AV_CODEC_ID_MPEG1VIDEO:
case AV_CODEC_ID_MPEG2VIDEO:
if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
ff_mpeg1_encode_slice_header(s);
ff_mpeg1_clean_buffers(s);
}
break;
case AV_CODEC_ID_H263:
case AV_CODEC_ID_H263P:
if (CONFIG_H263_ENCODER)
ff_h263_encode_gob_header(s, mb_y);
break;
}
if(s->flags&CODEC_FLAG_PASS1){
int bits= put_bits_count(&s->pb);
s->misc_bits+= bits - s->last_bits;
s->last_bits= bits;
}
s->ptr_lastgob += current_packet_size;
s->first_slice_line=1;
s->resync_mb_x=mb_x;
s->resync_mb_y=mb_y;
}
}
if( (s->resync_mb_x == s->mb_x)
&& s->resync_mb_y+1 == s->mb_y){
s->first_slice_line=0;
}
s->mb_skipped=0;
s->dquant=0;
update_mb_info(s, 0);
if (mb_type & (mb_type-1) || (s->mpv_flags & FF_MPV_FLAG_QP_RD)) {
int next_block=0;
int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
copy_context_before_encode(&backup_s, s, -1);
backup_s.pb= s->pb;
best_s.data_partitioning= s->data_partitioning;
best_s.partitioned_frame= s->partitioned_frame;
if(s->data_partitioning){
backup_s.pb2= s->pb2;
backup_s.tex_pb= s->tex_pb;
}
if(mb_type&CANDIDATE_MB_TYPE_INTER){
s->mv_dir = MV_DIR_FORWARD;
s->mv_type = MV_TYPE_16X16;
s->mb_intra= 0;
s->mv[0][0][0] = s->p_mv_table[xy][0];
s->mv[0][0][1] = s->p_mv_table[xy][1];
encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
&dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
}
if(mb_type&CANDIDATE_MB_TYPE_INTER_I){
s->mv_dir = MV_DIR_FORWARD;
s->mv_type = MV_TYPE_FIELD;
s->mb_intra= 0;
for(i=0; i<2; i++){
j= s->field_select[0][i] = s->p_field_select_table[i][xy];
s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
}
encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
&dmin, &next_block, 0, 0);
}
if(mb_type&CANDIDATE_MB_TYPE_SKIPPED){
s->mv_dir = MV_DIR_FORWARD;
s->mv_type = MV_TYPE_16X16;
s->mb_intra= 0;
s->mv[0][0][0] = 0;
s->mv[0][0][1] = 0;
encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
&dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
}
if(mb_type&CANDIDATE_MB_TYPE_INTER4V){
s->mv_dir = MV_DIR_FORWARD;
s->mv_type = MV_TYPE_8X8;
s->mb_intra= 0;
for(i=0; i<4; i++){
s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
}
encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
&dmin, &next_block, 0, 0);
}
if(mb_type&CANDIDATE_MB_TYPE_FORWARD){
s->mv_dir = MV_DIR_FORWARD;
s->mv_type = MV_TYPE_16X16;
s->mb_intra= 0;
s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
&dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
}
if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){
s->mv_dir = MV_DIR_BACKWARD;
s->mv_type = MV_TYPE_16X16;
s->mb_intra= 0;
s->mv[1][0][0] = s->b_back_mv_table[xy][0];
s->mv[1][0][1] = s->b_back_mv_table[xy][1];
encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
&dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
}
if(mb_type&CANDIDATE_MB_TYPE_BIDIR){
s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
s->mv_type = MV_TYPE_16X16;
s->mb_intra= 0;
s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
&dmin, &next_block, 0, 0);
}
if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){
s->mv_dir = MV_DIR_FORWARD;
s->mv_type = MV_TYPE_FIELD;
s->mb_intra= 0;
for(i=0; i<2; i++){
j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
}
encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
&dmin, &next_block, 0, 0);
}
if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){
s->mv_dir = MV_DIR_BACKWARD;
s->mv_type = MV_TYPE_FIELD;
s->mb_intra= 0;
for(i=0; i<2; i++){
j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
}
encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
&dmin, &next_block, 0, 0);
}
if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){
s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
s->mv_type = MV_TYPE_FIELD;
s->mb_intra= 0;
for(dir=0; dir<2; dir++){
for(i=0; i<2; i++){
j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
}
}
encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
&dmin, &next_block, 0, 0);
}
if(mb_type&CANDIDATE_MB_TYPE_INTRA){
s->mv_dir = 0;
s->mv_type = MV_TYPE_16X16;
s->mb_intra= 1;
s->mv[0][0][0] = 0;
s->mv[0][0][1] = 0;
encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
&dmin, &next_block, 0, 0);
if(s->h263_pred || s->h263_aic){
if(best_s.mb_intra)
s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
else
ff_clean_intra_table_entries(s);
}
}
if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) && dmin < INT_MAX) {
if(best_s.mv_type==MV_TYPE_16X16){
const int last_qp= backup_s.qscale;
int qpi, qp, dc[6];
int16_t ac[6][16];
const int mvdir= (best_s.mv_dir&MV_DIR_BACKWARD) ? 1 : 0;
static const int dquant_tab[4]={-1,1,-2,2};
assert(backup_s.dquant == 0);
s->mv_dir= best_s.mv_dir;
s->mv_type = MV_TYPE_16X16;
s->mb_intra= best_s.mb_intra;
s->mv[0][0][0] = best_s.mv[0][0][0];
s->mv[0][0][1] = best_s.mv[0][0][1];
s->mv[1][0][0] = best_s.mv[1][0][0];
s->mv[1][0][1] = best_s.mv[1][0][1];
qpi = s->pict_type == AV_PICTURE_TYPE_B ? 2 : 0;
for(; qpi<4; qpi++){
int dquant= dquant_tab[qpi];
qp= last_qp + dquant;
if(qp < s->avctx->qmin || qp > s->avctx->qmax)
continue;
backup_s.dquant= dquant;
if(s->mb_intra && s->dc_val[0]){
for(i=0; i<6; i++){
dc[i]= s->dc_val[0][ s->block_index[i] ];
memcpy(ac[i], s->ac_val[0][s->block_index[i]], sizeof(int16_t)*16);
}
}
encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
&dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]);
if(best_s.qscale != qp){
if(s->mb_intra && s->dc_val[0]){
for(i=0; i<6; i++){
s->dc_val[0][ s->block_index[i] ]= dc[i];
memcpy(s->ac_val[0][s->block_index[i]], ac[i], sizeof(int16_t)*16);
}
}
}
}
}
}
if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT){
int mx= s->b_direct_mv_table[xy][0];
int my= s->b_direct_mv_table[xy][1];
backup_s.dquant = 0;
s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
s->mb_intra= 0;
ff_mpeg4_set_direct_mv(s, mx, my);
encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
&dmin, &next_block, mx, my);
}
if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT0){
backup_s.dquant = 0;
s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
s->mb_intra= 0;
ff_mpeg4_set_direct_mv(s, 0, 0);
encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
&dmin, &next_block, 0, 0);
}
if (!best_s.mb_intra && s->mpv_flags & FF_MPV_FLAG_SKIP_RD) {
int coded=0;
for(i=0; i<6; i++)
coded |= s->block_last_index[i];
if(coded){
int mx,my;
memcpy(s->mv, best_s.mv, sizeof(s->mv));
if(CONFIG_MPEG4_ENCODER && best_s.mv_dir & MV_DIRECT){
mx=my=0;
ff_mpeg4_set_direct_mv(s, mx, my);
}else if(best_s.mv_dir&MV_DIR_BACKWARD){
mx= s->mv[1][0][0];
my= s->mv[1][0][1];
}else{
mx= s->mv[0][0][0];
my= s->mv[0][0][1];
}
s->mv_dir= best_s.mv_dir;
s->mv_type = best_s.mv_type;
s->mb_intra= 0;
backup_s.dquant= 0;
s->skipdct=1;
encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
&dmin, &next_block, mx, my);
s->skipdct=0;
}
}
s->current_picture.qscale_table[xy] = best_s.qscale;
copy_context_after_encode(s, &best_s, -1);
pb_bits_count= put_bits_count(&s->pb);
flush_put_bits(&s->pb);
avpriv_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
s->pb= backup_s.pb;
if(s->data_partitioning){
pb2_bits_count= put_bits_count(&s->pb2);
flush_put_bits(&s->pb2);
avpriv_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
s->pb2= backup_s.pb2;
tex_pb_bits_count= put_bits_count(&s->tex_pb);
flush_put_bits(&s->tex_pb);
avpriv_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
s->tex_pb= backup_s.tex_pb;
}
s->last_bits= put_bits_count(&s->pb);
if (CONFIG_H263_ENCODER &&
s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)
ff_h263_update_motion_val(s);
if(next_block==0){
s->hdsp.put_pixels_tab[0][0](s->dest[0], s->rd_scratchpad , s->linesize ,16);
s->hdsp.put_pixels_tab[1][0](s->dest[1], s->rd_scratchpad + 16*s->linesize , s->uvlinesize, 8);
s->hdsp.put_pixels_tab[1][0](s->dest[2], s->rd_scratchpad + 16*s->linesize + 8, s->uvlinesize, 8);
}
if(s->avctx->mb_decision == FF_MB_DECISION_BITS)
ff_mpv_decode_mb(s, s->block);
} else {
int motion_x = 0, motion_y = 0;
s->mv_type=MV_TYPE_16X16;
switch(mb_type){
case CANDIDATE_MB_TYPE_INTRA:
s->mv_dir = 0;
s->mb_intra= 1;
motion_x= s->mv[0][0][0] = 0;
motion_y= s->mv[0][0][1] = 0;
break;
case CANDIDATE_MB_TYPE_INTER:
s->mv_dir = MV_DIR_FORWARD;
s->mb_intra= 0;
motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
break;
case CANDIDATE_MB_TYPE_INTER_I:
s->mv_dir = MV_DIR_FORWARD;
s->mv_type = MV_TYPE_FIELD;
s->mb_intra= 0;
for(i=0; i<2; i++){
j= s->field_select[0][i] = s->p_field_select_table[i][xy];
s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
}
break;
case CANDIDATE_MB_TYPE_INTER4V:
s->mv_dir = MV_DIR_FORWARD;
s->mv_type = MV_TYPE_8X8;
s->mb_intra= 0;
for(i=0; i<4; i++){
s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
}
break;
case CANDIDATE_MB_TYPE_DIRECT:
if (CONFIG_MPEG4_ENCODER) {
s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
s->mb_intra= 0;
motion_x=s->b_direct_mv_table[xy][0];
motion_y=s->b_direct_mv_table[xy][1];
ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
}
break;
case CANDIDATE_MB_TYPE_DIRECT0:
if (CONFIG_MPEG4_ENCODER) {
s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
s->mb_intra= 0;
ff_mpeg4_set_direct_mv(s, 0, 0);
}
break;
case CANDIDATE_MB_TYPE_BIDIR:
s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
s->mb_intra= 0;
s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
break;
case CANDIDATE_MB_TYPE_BACKWARD:
s->mv_dir = MV_DIR_BACKWARD;
s->mb_intra= 0;
motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
break;
case CANDIDATE_MB_TYPE_FORWARD:
s->mv_dir = MV_DIR_FORWARD;
s->mb_intra= 0;
motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
break;
case CANDIDATE_MB_TYPE_FORWARD_I:
s->mv_dir = MV_DIR_FORWARD;
s->mv_type = MV_TYPE_FIELD;
s->mb_intra= 0;
for(i=0; i<2; i++){
j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
}
break;
case CANDIDATE_MB_TYPE_BACKWARD_I:
s->mv_dir = MV_DIR_BACKWARD;
s->mv_type = MV_TYPE_FIELD;
s->mb_intra= 0;
for(i=0; i<2; i++){
j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
}
break;
case CANDIDATE_MB_TYPE_BIDIR_I:
s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
s->mv_type = MV_TYPE_FIELD;
s->mb_intra= 0;
for(dir=0; dir<2; dir++){
for(i=0; i<2; i++){
j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
}
}
break;
default:
av_log(s->avctx, AV_LOG_ERROR, "illegal MB type\n");
}
encode_mb(s, motion_x, motion_y);
s->last_mv_dir = s->mv_dir;
if (CONFIG_H263_ENCODER &&
s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)
ff_h263_update_motion_val(s);
ff_mpv_decode_mb(s, s->block);
}
if(s->mb_intra ){
s->p_mv_table[xy][0]=0;
s->p_mv_table[xy][1]=0;
}
if(s->flags&CODEC_FLAG_PSNR){
int w= 16;
int h= 16;
if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
s->current_picture.f->error[0] += sse(
s, s->new_picture.f->data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
s->dest[0], w, h, s->linesize);
s->current_picture.f->error[1] += sse(
s, s->new_picture.f->data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*chr_h,
s->dest[1], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
s->current_picture.f->error[2] += sse(
s, s->new_picture.f->data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*chr_h,
s->dest[2], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
}
if(s->loop_filter){
if(CONFIG_H263_ENCODER && s->out_format == FMT_H263)
ff_h263_loop_filter(s);
}
av_dlog(s->avctx, "MB %d %d bits\n",
s->mb_x + s->mb_y * s->mb_stride, put_bits_count(&s->pb));
}
}
if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == AV_PICTURE_TYPE_I)
ff_msmpeg4_encode_ext_header(s);
write_slice_end(s);
if (s->avctx->rtp_callback) {
int number_mb = (mb_y - s->resync_mb_y)*s->mb_width - s->resync_mb_x;
pdif = put_bits_ptr(&s->pb) - s->ptr_lastgob;
emms_c();
s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, pdif, number_mb);
}
return 0;
} | ['static int encode_thread(AVCodecContext *c, void *arg){\n MpegEncContext *s= *(void**)arg;\n int mb_x, mb_y, pdif = 0;\n int chr_h= 16>>s->chroma_y_shift;\n int i, j;\n MpegEncContext best_s, backup_s;\n uint8_t bit_buf[2][MAX_MB_BYTES];\n uint8_t bit_buf2[2][MAX_MB_BYTES];\n uint8_t bit_buf_tex[2][MAX_MB_BYTES];\n PutBitContext pb[2], pb2[2], tex_pb[2];\n for(i=0; i<2; i++){\n init_put_bits(&pb [i], bit_buf [i], MAX_MB_BYTES);\n init_put_bits(&pb2 [i], bit_buf2 [i], MAX_MB_BYTES);\n init_put_bits(&tex_pb[i], bit_buf_tex[i], MAX_MB_BYTES);\n }\n s->last_bits= put_bits_count(&s->pb);\n s->mv_bits=0;\n s->misc_bits=0;\n s->i_tex_bits=0;\n s->p_tex_bits=0;\n s->i_count=0;\n s->f_count=0;\n s->b_count=0;\n s->skip_count=0;\n for(i=0; i<3; i++){\n s->last_dc[i] = 128 << s->intra_dc_precision;\n s->current_picture.f->error[i] = 0;\n }\n s->mb_skip_run = 0;\n memset(s->last_mv, 0, sizeof(s->last_mv));\n s->last_mv_dir = 0;\n switch(s->codec_id){\n case AV_CODEC_ID_H263:\n case AV_CODEC_ID_H263P:\n case AV_CODEC_ID_FLV1:\n if (CONFIG_H263_ENCODER)\n s->gob_index = ff_h263_get_gob_height(s);\n break;\n case AV_CODEC_ID_MPEG4:\n if(CONFIG_MPEG4_ENCODER && s->partitioned_frame)\n ff_mpeg4_init_partitions(s);\n break;\n }\n s->resync_mb_x=0;\n s->resync_mb_y=0;\n s->first_slice_line = 1;\n s->ptr_lastgob = s->pb.buf;\n for(mb_y= s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {\n s->mb_x=0;\n s->mb_y= mb_y;\n ff_set_qscale(s, s->qscale);\n ff_init_block_index(s);\n for(mb_x=0; mb_x < s->mb_width; mb_x++) {\n int xy= mb_y*s->mb_stride + mb_x;\n int mb_type= s->mb_type[xy];\n int dmin= INT_MAX;\n int dir;\n if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < MAX_MB_BYTES){\n av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\\n");\n return -1;\n }\n if(s->data_partitioning){\n if( s->pb2 .buf_end - s->pb2 .buf - (put_bits_count(&s-> pb2)>>3) < MAX_MB_BYTES\n || s->tex_pb.buf_end - s->tex_pb.buf - (put_bits_count(&s->tex_pb )>>3) < MAX_MB_BYTES){\n av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\\n");\n return -1;\n }\n }\n s->mb_x = mb_x;\n s->mb_y = mb_y;\n ff_update_block_index(s);\n if(CONFIG_H261_ENCODER && s->codec_id == AV_CODEC_ID_H261){\n ff_h261_reorder_mb_index(s);\n xy= s->mb_y*s->mb_stride + s->mb_x;\n mb_type= s->mb_type[xy];\n }\n if(s->rtp_mode){\n int current_packet_size, is_gob_start;\n current_packet_size= ((put_bits_count(&s->pb)+7)>>3) - (s->ptr_lastgob - s->pb.buf);\n is_gob_start= s->avctx->rtp_payload_size && current_packet_size >= s->avctx->rtp_payload_size && mb_y + mb_x>0;\n if(s->start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;\n switch(s->codec_id){\n case AV_CODEC_ID_H263:\n case AV_CODEC_ID_H263P:\n if(!s->h263_slice_structured)\n if(s->mb_x || s->mb_y%s->gob_index) is_gob_start=0;\n break;\n case AV_CODEC_ID_MPEG2VIDEO:\n if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;\n case AV_CODEC_ID_MPEG1VIDEO:\n if(s->mb_skip_run) is_gob_start=0;\n break;\n }\n if(is_gob_start){\n if(s->start_mb_y != mb_y || mb_x!=0){\n write_slice_end(s);\n if(CONFIG_MPEG4_ENCODER && s->codec_id==AV_CODEC_ID_MPEG4 && s->partitioned_frame){\n ff_mpeg4_init_partitions(s);\n }\n }\n assert((put_bits_count(&s->pb)&7) == 0);\n current_packet_size= put_bits_ptr(&s->pb) - s->ptr_lastgob;\n if (s->error_rate && s->resync_mb_x + s->resync_mb_y > 0) {\n int r= put_bits_count(&s->pb)/8 + s->picture_number + 16 + s->mb_x + s->mb_y;\n int d = 100 / s->error_rate;\n if(r % d == 0){\n current_packet_size=0;\n s->pb.buf_ptr= s->ptr_lastgob;\n assert(put_bits_ptr(&s->pb) == s->ptr_lastgob);\n }\n }\n if (s->avctx->rtp_callback){\n int number_mb = (mb_y - s->resync_mb_y)*s->mb_width + mb_x - s->resync_mb_x;\n s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, current_packet_size, number_mb);\n }\n update_mb_info(s, 1);\n switch(s->codec_id){\n case AV_CODEC_ID_MPEG4:\n if (CONFIG_MPEG4_ENCODER) {\n ff_mpeg4_encode_video_packet_header(s);\n ff_mpeg4_clean_buffers(s);\n }\n break;\n case AV_CODEC_ID_MPEG1VIDEO:\n case AV_CODEC_ID_MPEG2VIDEO:\n if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {\n ff_mpeg1_encode_slice_header(s);\n ff_mpeg1_clean_buffers(s);\n }\n break;\n case AV_CODEC_ID_H263:\n case AV_CODEC_ID_H263P:\n if (CONFIG_H263_ENCODER)\n ff_h263_encode_gob_header(s, mb_y);\n break;\n }\n if(s->flags&CODEC_FLAG_PASS1){\n int bits= put_bits_count(&s->pb);\n s->misc_bits+= bits - s->last_bits;\n s->last_bits= bits;\n }\n s->ptr_lastgob += current_packet_size;\n s->first_slice_line=1;\n s->resync_mb_x=mb_x;\n s->resync_mb_y=mb_y;\n }\n }\n if( (s->resync_mb_x == s->mb_x)\n && s->resync_mb_y+1 == s->mb_y){\n s->first_slice_line=0;\n }\n s->mb_skipped=0;\n s->dquant=0;\n update_mb_info(s, 0);\n if (mb_type & (mb_type-1) || (s->mpv_flags & FF_MPV_FLAG_QP_RD)) {\n int next_block=0;\n int pb_bits_count, pb2_bits_count, tex_pb_bits_count;\n copy_context_before_encode(&backup_s, s, -1);\n backup_s.pb= s->pb;\n best_s.data_partitioning= s->data_partitioning;\n best_s.partitioned_frame= s->partitioned_frame;\n if(s->data_partitioning){\n backup_s.pb2= s->pb2;\n backup_s.tex_pb= s->tex_pb;\n }\n if(mb_type&CANDIDATE_MB_TYPE_INTER){\n s->mv_dir = MV_DIR_FORWARD;\n s->mv_type = MV_TYPE_16X16;\n s->mb_intra= 0;\n s->mv[0][0][0] = s->p_mv_table[xy][0];\n s->mv[0][0][1] = s->p_mv_table[xy][1];\n encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,\n &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);\n }\n if(mb_type&CANDIDATE_MB_TYPE_INTER_I){\n s->mv_dir = MV_DIR_FORWARD;\n s->mv_type = MV_TYPE_FIELD;\n s->mb_intra= 0;\n for(i=0; i<2; i++){\n j= s->field_select[0][i] = s->p_field_select_table[i][xy];\n s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];\n s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];\n }\n encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,\n &dmin, &next_block, 0, 0);\n }\n if(mb_type&CANDIDATE_MB_TYPE_SKIPPED){\n s->mv_dir = MV_DIR_FORWARD;\n s->mv_type = MV_TYPE_16X16;\n s->mb_intra= 0;\n s->mv[0][0][0] = 0;\n s->mv[0][0][1] = 0;\n encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,\n &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);\n }\n if(mb_type&CANDIDATE_MB_TYPE_INTER4V){\n s->mv_dir = MV_DIR_FORWARD;\n s->mv_type = MV_TYPE_8X8;\n s->mb_intra= 0;\n for(i=0; i<4; i++){\n s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];\n s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];\n }\n encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,\n &dmin, &next_block, 0, 0);\n }\n if(mb_type&CANDIDATE_MB_TYPE_FORWARD){\n s->mv_dir = MV_DIR_FORWARD;\n s->mv_type = MV_TYPE_16X16;\n s->mb_intra= 0;\n s->mv[0][0][0] = s->b_forw_mv_table[xy][0];\n s->mv[0][0][1] = s->b_forw_mv_table[xy][1];\n encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,\n &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);\n }\n if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){\n s->mv_dir = MV_DIR_BACKWARD;\n s->mv_type = MV_TYPE_16X16;\n s->mb_intra= 0;\n s->mv[1][0][0] = s->b_back_mv_table[xy][0];\n s->mv[1][0][1] = s->b_back_mv_table[xy][1];\n encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,\n &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);\n }\n if(mb_type&CANDIDATE_MB_TYPE_BIDIR){\n s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;\n s->mv_type = MV_TYPE_16X16;\n s->mb_intra= 0;\n s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];\n s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];\n s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];\n s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];\n encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,\n &dmin, &next_block, 0, 0);\n }\n if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){\n s->mv_dir = MV_DIR_FORWARD;\n s->mv_type = MV_TYPE_FIELD;\n s->mb_intra= 0;\n for(i=0; i<2; i++){\n j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];\n s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];\n s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];\n }\n encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,\n &dmin, &next_block, 0, 0);\n }\n if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){\n s->mv_dir = MV_DIR_BACKWARD;\n s->mv_type = MV_TYPE_FIELD;\n s->mb_intra= 0;\n for(i=0; i<2; i++){\n j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];\n s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];\n s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];\n }\n encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,\n &dmin, &next_block, 0, 0);\n }\n if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){\n s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;\n s->mv_type = MV_TYPE_FIELD;\n s->mb_intra= 0;\n for(dir=0; dir<2; dir++){\n for(i=0; i<2; i++){\n j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];\n s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];\n s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];\n }\n }\n encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,\n &dmin, &next_block, 0, 0);\n }\n if(mb_type&CANDIDATE_MB_TYPE_INTRA){\n s->mv_dir = 0;\n s->mv_type = MV_TYPE_16X16;\n s->mb_intra= 1;\n s->mv[0][0][0] = 0;\n s->mv[0][0][1] = 0;\n encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,\n &dmin, &next_block, 0, 0);\n if(s->h263_pred || s->h263_aic){\n if(best_s.mb_intra)\n s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;\n else\n ff_clean_intra_table_entries(s);\n }\n }\n if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) && dmin < INT_MAX) {\n if(best_s.mv_type==MV_TYPE_16X16){\n const int last_qp= backup_s.qscale;\n int qpi, qp, dc[6];\n int16_t ac[6][16];\n const int mvdir= (best_s.mv_dir&MV_DIR_BACKWARD) ? 1 : 0;\n static const int dquant_tab[4]={-1,1,-2,2};\n assert(backup_s.dquant == 0);\n s->mv_dir= best_s.mv_dir;\n s->mv_type = MV_TYPE_16X16;\n s->mb_intra= best_s.mb_intra;\n s->mv[0][0][0] = best_s.mv[0][0][0];\n s->mv[0][0][1] = best_s.mv[0][0][1];\n s->mv[1][0][0] = best_s.mv[1][0][0];\n s->mv[1][0][1] = best_s.mv[1][0][1];\n qpi = s->pict_type == AV_PICTURE_TYPE_B ? 2 : 0;\n for(; qpi<4; qpi++){\n int dquant= dquant_tab[qpi];\n qp= last_qp + dquant;\n if(qp < s->avctx->qmin || qp > s->avctx->qmax)\n continue;\n backup_s.dquant= dquant;\n if(s->mb_intra && s->dc_val[0]){\n for(i=0; i<6; i++){\n dc[i]= s->dc_val[0][ s->block_index[i] ];\n memcpy(ac[i], s->ac_val[0][s->block_index[i]], sizeof(int16_t)*16);\n }\n }\n encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,\n &dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]);\n if(best_s.qscale != qp){\n if(s->mb_intra && s->dc_val[0]){\n for(i=0; i<6; i++){\n s->dc_val[0][ s->block_index[i] ]= dc[i];\n memcpy(s->ac_val[0][s->block_index[i]], ac[i], sizeof(int16_t)*16);\n }\n }\n }\n }\n }\n }\n if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT){\n int mx= s->b_direct_mv_table[xy][0];\n int my= s->b_direct_mv_table[xy][1];\n backup_s.dquant = 0;\n s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;\n s->mb_intra= 0;\n ff_mpeg4_set_direct_mv(s, mx, my);\n encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,\n &dmin, &next_block, mx, my);\n }\n if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT0){\n backup_s.dquant = 0;\n s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;\n s->mb_intra= 0;\n ff_mpeg4_set_direct_mv(s, 0, 0);\n encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,\n &dmin, &next_block, 0, 0);\n }\n if (!best_s.mb_intra && s->mpv_flags & FF_MPV_FLAG_SKIP_RD) {\n int coded=0;\n for(i=0; i<6; i++)\n coded |= s->block_last_index[i];\n if(coded){\n int mx,my;\n memcpy(s->mv, best_s.mv, sizeof(s->mv));\n if(CONFIG_MPEG4_ENCODER && best_s.mv_dir & MV_DIRECT){\n mx=my=0;\n ff_mpeg4_set_direct_mv(s, mx, my);\n }else if(best_s.mv_dir&MV_DIR_BACKWARD){\n mx= s->mv[1][0][0];\n my= s->mv[1][0][1];\n }else{\n mx= s->mv[0][0][0];\n my= s->mv[0][0][1];\n }\n s->mv_dir= best_s.mv_dir;\n s->mv_type = best_s.mv_type;\n s->mb_intra= 0;\n backup_s.dquant= 0;\n s->skipdct=1;\n encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,\n &dmin, &next_block, mx, my);\n s->skipdct=0;\n }\n }\n s->current_picture.qscale_table[xy] = best_s.qscale;\n copy_context_after_encode(s, &best_s, -1);\n pb_bits_count= put_bits_count(&s->pb);\n flush_put_bits(&s->pb);\n avpriv_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);\n s->pb= backup_s.pb;\n if(s->data_partitioning){\n pb2_bits_count= put_bits_count(&s->pb2);\n flush_put_bits(&s->pb2);\n avpriv_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);\n s->pb2= backup_s.pb2;\n tex_pb_bits_count= put_bits_count(&s->tex_pb);\n flush_put_bits(&s->tex_pb);\n avpriv_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);\n s->tex_pb= backup_s.tex_pb;\n }\n s->last_bits= put_bits_count(&s->pb);\n if (CONFIG_H263_ENCODER &&\n s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)\n ff_h263_update_motion_val(s);\n if(next_block==0){\n s->hdsp.put_pixels_tab[0][0](s->dest[0], s->rd_scratchpad , s->linesize ,16);\n s->hdsp.put_pixels_tab[1][0](s->dest[1], s->rd_scratchpad + 16*s->linesize , s->uvlinesize, 8);\n s->hdsp.put_pixels_tab[1][0](s->dest[2], s->rd_scratchpad + 16*s->linesize + 8, s->uvlinesize, 8);\n }\n if(s->avctx->mb_decision == FF_MB_DECISION_BITS)\n ff_mpv_decode_mb(s, s->block);\n } else {\n int motion_x = 0, motion_y = 0;\n s->mv_type=MV_TYPE_16X16;\n switch(mb_type){\n case CANDIDATE_MB_TYPE_INTRA:\n s->mv_dir = 0;\n s->mb_intra= 1;\n motion_x= s->mv[0][0][0] = 0;\n motion_y= s->mv[0][0][1] = 0;\n break;\n case CANDIDATE_MB_TYPE_INTER:\n s->mv_dir = MV_DIR_FORWARD;\n s->mb_intra= 0;\n motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];\n motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];\n break;\n case CANDIDATE_MB_TYPE_INTER_I:\n s->mv_dir = MV_DIR_FORWARD;\n s->mv_type = MV_TYPE_FIELD;\n s->mb_intra= 0;\n for(i=0; i<2; i++){\n j= s->field_select[0][i] = s->p_field_select_table[i][xy];\n s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];\n s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];\n }\n break;\n case CANDIDATE_MB_TYPE_INTER4V:\n s->mv_dir = MV_DIR_FORWARD;\n s->mv_type = MV_TYPE_8X8;\n s->mb_intra= 0;\n for(i=0; i<4; i++){\n s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];\n s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];\n }\n break;\n case CANDIDATE_MB_TYPE_DIRECT:\n if (CONFIG_MPEG4_ENCODER) {\n s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;\n s->mb_intra= 0;\n motion_x=s->b_direct_mv_table[xy][0];\n motion_y=s->b_direct_mv_table[xy][1];\n ff_mpeg4_set_direct_mv(s, motion_x, motion_y);\n }\n break;\n case CANDIDATE_MB_TYPE_DIRECT0:\n if (CONFIG_MPEG4_ENCODER) {\n s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;\n s->mb_intra= 0;\n ff_mpeg4_set_direct_mv(s, 0, 0);\n }\n break;\n case CANDIDATE_MB_TYPE_BIDIR:\n s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;\n s->mb_intra= 0;\n s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];\n s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];\n s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];\n s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];\n break;\n case CANDIDATE_MB_TYPE_BACKWARD:\n s->mv_dir = MV_DIR_BACKWARD;\n s->mb_intra= 0;\n motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];\n motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];\n break;\n case CANDIDATE_MB_TYPE_FORWARD:\n s->mv_dir = MV_DIR_FORWARD;\n s->mb_intra= 0;\n motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];\n motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];\n break;\n case CANDIDATE_MB_TYPE_FORWARD_I:\n s->mv_dir = MV_DIR_FORWARD;\n s->mv_type = MV_TYPE_FIELD;\n s->mb_intra= 0;\n for(i=0; i<2; i++){\n j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];\n s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];\n s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];\n }\n break;\n case CANDIDATE_MB_TYPE_BACKWARD_I:\n s->mv_dir = MV_DIR_BACKWARD;\n s->mv_type = MV_TYPE_FIELD;\n s->mb_intra= 0;\n for(i=0; i<2; i++){\n j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];\n s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];\n s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];\n }\n break;\n case CANDIDATE_MB_TYPE_BIDIR_I:\n s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;\n s->mv_type = MV_TYPE_FIELD;\n s->mb_intra= 0;\n for(dir=0; dir<2; dir++){\n for(i=0; i<2; i++){\n j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];\n s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];\n s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];\n }\n }\n break;\n default:\n av_log(s->avctx, AV_LOG_ERROR, "illegal MB type\\n");\n }\n encode_mb(s, motion_x, motion_y);\n s->last_mv_dir = s->mv_dir;\n if (CONFIG_H263_ENCODER &&\n s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)\n ff_h263_update_motion_val(s);\n ff_mpv_decode_mb(s, s->block);\n }\n if(s->mb_intra ){\n s->p_mv_table[xy][0]=0;\n s->p_mv_table[xy][1]=0;\n }\n if(s->flags&CODEC_FLAG_PSNR){\n int w= 16;\n int h= 16;\n if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;\n if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;\n s->current_picture.f->error[0] += sse(\n s, s->new_picture.f->data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,\n s->dest[0], w, h, s->linesize);\n s->current_picture.f->error[1] += sse(\n s, s->new_picture.f->data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*chr_h,\n s->dest[1], w>>1, h>>s->chroma_y_shift, s->uvlinesize);\n s->current_picture.f->error[2] += sse(\n s, s->new_picture.f->data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*chr_h,\n s->dest[2], w>>1, h>>s->chroma_y_shift, s->uvlinesize);\n }\n if(s->loop_filter){\n if(CONFIG_H263_ENCODER && s->out_format == FMT_H263)\n ff_h263_loop_filter(s);\n }\n av_dlog(s->avctx, "MB %d %d bits\\n",\n s->mb_x + s->mb_y * s->mb_stride, put_bits_count(&s->pb));\n }\n }\n if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == AV_PICTURE_TYPE_I)\n ff_msmpeg4_encode_ext_header(s);\n write_slice_end(s);\n if (s->avctx->rtp_callback) {\n int number_mb = (mb_y - s->resync_mb_y)*s->mb_width - s->resync_mb_x;\n pdif = put_bits_ptr(&s->pb) - s->ptr_lastgob;\n emms_c();\n s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, pdif, number_mb);\n }\n return 0;\n}'] |
543 | 0 | https://github.com/openssl/openssl/blob/9b02dc97e4963969da69675a871dbe80e6d31cda/crypto/bn/bn_shift.c/#L112 | int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)
{
int i, nw, lb, rb;
BN_ULONG *t, *f;
BN_ULONG l;
bn_check_top(r);
bn_check_top(a);
if (n < 0) {
BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);
return 0;
}
nw = n / BN_BITS2;
if (bn_wexpand(r, a->top + nw + 1) == NULL)
return 0;
r->neg = a->neg;
lb = n % BN_BITS2;
rb = BN_BITS2 - lb;
f = a->d;
t = r->d;
t[a->top + nw] = 0;
if (lb == 0)
for (i = a->top - 1; i >= 0; i--)
t[nw + i] = f[i];
else
for (i = a->top - 1; i >= 0; i--) {
l = f[i];
t[nw + i + 1] |= (l >> rb) & BN_MASK2;
t[nw + i] = (l << lb) & BN_MASK2;
}
memset(t, 0, sizeof(*t) * nw);
r->top = a->top + nw + 1;
bn_correct_top(r);
bn_check_top(r);
return 1;
} | ['int ossl_ecdsa_verify_sig(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 const BIGNUM *order;\n BIGNUM *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 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_MISSING_PARAMETERS);\n return -1;\n }\n if (!EC_KEY_can_sign(eckey)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);\n return -1;\n }\n ctx = BN_CTX_new();\n if (ctx == NULL) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);\n return -1;\n }\n BN_CTX_start(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 == NULL) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);\n goto err;\n }\n order = EC_GROUP_get0_order(group);\n if (order == NULL) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, 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 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_BAD_SIGNATURE);\n ret = 0;\n goto err;\n }\n if (!BN_mod_inverse(u2, sig->s, order, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, 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 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if (!BN_mod_mul(u1, m, u2, order, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if ((point = EC_POINT_new(group)) == NULL) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, 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 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, 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 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);\n goto err;\n }\n }\n#endif\n if (!BN_nnmod(u1, X, order, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, 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}', '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_num_bits(const BIGNUM *a)\n{\n int i = a->top - 1;\n bn_check_top(a);\n if (BN_is_zero(a))\n return 0;\n return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));\n}', 'int BN_is_zero(const BIGNUM *a)\n{\n return a->top == 0;\n}', 'int BN_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_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return 0;\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return 0;\n }\n if (dv != NULL)\n BN_zero(dv);\n return 1;\n }\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n resp++;\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n resp--;\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return 0;\n}', 'int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l;\n bn_check_top(r);\n bn_check_top(a);\n if (n < 0) {\n BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n nw = n / BN_BITS2;\n if (bn_wexpand(r, a->top + nw + 1) == NULL)\n return 0;\n r->neg = a->neg;\n lb = n % BN_BITS2;\n rb = BN_BITS2 - lb;\n f = a->d;\n t = r->d;\n t[a->top + nw] = 0;\n if (lb == 0)\n for (i = a->top - 1; i >= 0; i--)\n t[nw + i] = f[i];\n else\n for (i = a->top - 1; i >= 0; i--) {\n l = f[i];\n t[nw + i + 1] |= (l >> rb) & BN_MASK2;\n t[nw + i] = (l << lb) & BN_MASK2;\n }\n memset(t, 0, sizeof(*t) * nw);\n r->top = a->top + nw + 1;\n bn_correct_top(r);\n bn_check_top(r);\n return 1;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}'] |
544 | 0 | https://github.com/openssl/openssl/blob/ddc6a5c8f5900959bdbdfee79e1625a3f7808acd/crypto/rand/drbg_rand.c/#L283 | int ctr_generate(RAND_DRBG *drbg,
unsigned char *out, size_t outlen,
const unsigned char *adin, size_t adinlen)
{
RAND_DRBG_CTR *ctr = &drbg->ctr;
if (adin != NULL && adinlen != 0) {
ctr_update(drbg, adin, adinlen, NULL, 0, NULL, 0);
if (drbg->flags & RAND_DRBG_FLAG_CTR_USE_DF) {
adin = NULL;
adinlen = 1;
}
} else {
adinlen = 0;
}
for ( ; ; ) {
inc_128(ctr);
if (outlen < 16) {
AES_encrypt(ctr->V, ctr->K, &ctr->ks);
memcpy(out, ctr->K, outlen);
break;
}
AES_encrypt(ctr->V, out, &ctr->ks);
out += 16;
outlen -= 16;
if (outlen == 0)
break;
}
ctr_update(drbg, adin, adinlen, NULL, 0, NULL, 0);
return 1;
} | ['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 (ssl_randbytes(s, 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 if (!ssl_log_rsa_client_key_exchange(s, encdata, enclen, pms, pmslen))\n goto err;\n s->s3->tmp.pms = pms;\n s->s3->tmp.pmslen = pmslen;\n return 1;\n err:\n OPENSSL_clear_free(pms, pmslen);\n EVP_PKEY_CTX_free(pctx);\n return 0;\n#else\n SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);\n *al = SSL_AD_INTERNAL_ERROR;\n return 0;\n#endif\n}', 'int ssl_randbytes(SSL *s, unsigned char *rnd, size_t size)\n{\n if (s->drbg != NULL)\n return RAND_DRBG_generate(s->drbg, rnd, size, 0, NULL, 0);\n return RAND_bytes(rnd, (int)size);\n}', 'int RAND_DRBG_generate(RAND_DRBG *drbg, unsigned char *out, size_t outlen,\n int prediction_resistance,\n const unsigned char *adin, size_t adinlen)\n{\n if (drbg->state == DRBG_ERROR) {\n RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_IN_ERROR_STATE);\n return 0;\n }\n if (drbg->state == DRBG_UNINITIALISED) {\n RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_NOT_INSTANTIATED);\n return 0;\n }\n if (outlen > drbg->max_request) {\n RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_REQUEST_TOO_LARGE_FOR_DRBG);\n return 0;\n }\n if (adinlen > drbg->max_adin) {\n RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_ADDITIONAL_INPUT_TOO_LONG);\n return 0;\n }\n if (drbg->reseed_counter >= drbg->reseed_interval)\n drbg->state = DRBG_RESEED;\n if (drbg->state == DRBG_RESEED || prediction_resistance) {\n if (!RAND_DRBG_reseed(drbg, adin, adinlen)) {\n RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_RESEED_ERROR);\n return 0;\n }\n adin = NULL;\n adinlen = 0;\n }\n if (!ctr_generate(drbg, out, outlen, adin, adinlen)) {\n drbg->state = DRBG_ERROR;\n RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_GENERATE_ERROR);\n return 0;\n }\n if (drbg->reseed_counter >= drbg->reseed_interval)\n drbg->state = DRBG_RESEED;\n else\n drbg->reseed_counter++;\n return 1;\n}', 'int ctr_generate(RAND_DRBG *drbg,\n unsigned char *out, size_t outlen,\n const unsigned char *adin, size_t adinlen)\n{\n RAND_DRBG_CTR *ctr = &drbg->ctr;\n if (adin != NULL && adinlen != 0) {\n ctr_update(drbg, adin, adinlen, NULL, 0, NULL, 0);\n if (drbg->flags & RAND_DRBG_FLAG_CTR_USE_DF) {\n adin = NULL;\n adinlen = 1;\n }\n } else {\n adinlen = 0;\n }\n for ( ; ; ) {\n inc_128(ctr);\n if (outlen < 16) {\n AES_encrypt(ctr->V, ctr->K, &ctr->ks);\n memcpy(out, ctr->K, outlen);\n break;\n }\n AES_encrypt(ctr->V, out, &ctr->ks);\n out += 16;\n outlen -= 16;\n if (outlen == 0)\n break;\n }\n ctr_update(drbg, adin, adinlen, NULL, 0, NULL, 0);\n return 1;\n}'] |
545 | 0 | https://github.com/libav/libav/blob/60392480181f24ebf3ab48d8ac3614705de90152/libavformat/icecast.c/#L118 | static int icecast_open(URLContext *h, const char *uri, int flags)
{
IcecastContext *s = h->priv_data;
AVDictionary *opt_dict = NULL;
char h_url[1024], host[1024], auth[1024], path[1024];
char *headers = NULL, *user = NULL;
int port, ret;
if (flags & AVIO_FLAG_READ)
return AVERROR(ENOSYS);
headers = cat_header(headers, "Ice-Name", s->name);
headers = cat_header(headers, "Ice-Description", s->description);
headers = cat_header(headers, "Ice-URL", s->url);
headers = cat_header(headers, "Ice-Genre", s->genre);
headers = cat_header(headers, "Ice-Public", s->public ? "1" : "0");
if (!headers) {
ret = AVERROR(ENOMEM);
goto cleanup;
}
av_dict_set(&opt_dict, "method", s->legacy_icecast ? "SOURCE" : "PUT", 0);
av_dict_set(&opt_dict, "auth_type", "basic", 0);
av_dict_set(&opt_dict, "headers", headers, 0);
av_dict_set(&opt_dict, "chunked_post", "0", 0);
av_dict_set(&opt_dict, "send_expect_100", s->legacy_icecast ? "0" : "1", 0);
if (NOT_EMPTY(s->content_type))
av_dict_set(&opt_dict, "content_type", s->content_type, 0);
else
av_dict_set(&opt_dict, "content_type", "audio/mpeg", 0);
if (NOT_EMPTY(s->user_agent))
av_dict_set(&opt_dict, "user_agent", s->user_agent, 0);
av_url_split(NULL, 0, auth, sizeof(auth), host, sizeof(host),
&port, path, sizeof(path), uri);
if (auth[0]) {
char *sep = strchr(auth,':');
if (sep) {
*sep = 0;
sep++;
if (s->pass) {
av_free(s->pass);
av_log(h, AV_LOG_WARNING, "Overwriting -password <pass> with URI password!\n");
}
if (!(s->pass = av_strdup(sep))) {
ret = AVERROR(ENOMEM);
goto cleanup;
}
}
if (!(user = av_strdup(auth))) {
ret = AVERROR(ENOMEM);
goto cleanup;
}
}
snprintf(auth, sizeof(auth),
"%s:%s",
user ? user : DEFAULT_ICE_USER,
s->pass ? s->pass : "");
if (!path[0] || strcmp(path, "/") == 0) {
av_log(h, AV_LOG_ERROR, "No mountpoint (path) specified!\n");
ret = AVERROR(EIO);
goto cleanup;
}
ff_url_join(h_url, sizeof(h_url), "http", auth, host, port, "%s", path);
ret = ffurl_open(&s->hd, h_url, AVIO_FLAG_READ_WRITE, NULL, &opt_dict);
cleanup:
av_freep(&user);
av_freep(&headers);
av_dict_free(&opt_dict);
return ret;
} | ['static int icecast_open(URLContext *h, const char *uri, int flags)\n{\n IcecastContext *s = h->priv_data;\n AVDictionary *opt_dict = NULL;\n char h_url[1024], host[1024], auth[1024], path[1024];\n char *headers = NULL, *user = NULL;\n int port, ret;\n if (flags & AVIO_FLAG_READ)\n return AVERROR(ENOSYS);\n headers = cat_header(headers, "Ice-Name", s->name);\n headers = cat_header(headers, "Ice-Description", s->description);\n headers = cat_header(headers, "Ice-URL", s->url);\n headers = cat_header(headers, "Ice-Genre", s->genre);\n headers = cat_header(headers, "Ice-Public", s->public ? "1" : "0");\n if (!headers) {\n ret = AVERROR(ENOMEM);\n goto cleanup;\n }\n av_dict_set(&opt_dict, "method", s->legacy_icecast ? "SOURCE" : "PUT", 0);\n av_dict_set(&opt_dict, "auth_type", "basic", 0);\n av_dict_set(&opt_dict, "headers", headers, 0);\n av_dict_set(&opt_dict, "chunked_post", "0", 0);\n av_dict_set(&opt_dict, "send_expect_100", s->legacy_icecast ? "0" : "1", 0);\n if (NOT_EMPTY(s->content_type))\n av_dict_set(&opt_dict, "content_type", s->content_type, 0);\n else\n av_dict_set(&opt_dict, "content_type", "audio/mpeg", 0);\n if (NOT_EMPTY(s->user_agent))\n av_dict_set(&opt_dict, "user_agent", s->user_agent, 0);\n av_url_split(NULL, 0, auth, sizeof(auth), host, sizeof(host),\n &port, path, sizeof(path), uri);\n if (auth[0]) {\n char *sep = strchr(auth,\':\');\n if (sep) {\n *sep = 0;\n sep++;\n if (s->pass) {\n av_free(s->pass);\n av_log(h, AV_LOG_WARNING, "Overwriting -password <pass> with URI password!\\n");\n }\n if (!(s->pass = av_strdup(sep))) {\n ret = AVERROR(ENOMEM);\n goto cleanup;\n }\n }\n if (!(user = av_strdup(auth))) {\n ret = AVERROR(ENOMEM);\n goto cleanup;\n }\n }\n snprintf(auth, sizeof(auth),\n "%s:%s",\n user ? user : DEFAULT_ICE_USER,\n s->pass ? s->pass : "");\n if (!path[0] || strcmp(path, "/") == 0) {\n av_log(h, AV_LOG_ERROR, "No mountpoint (path) specified!\\n");\n ret = AVERROR(EIO);\n goto cleanup;\n }\n ff_url_join(h_url, sizeof(h_url), "http", auth, host, port, "%s", path);\n ret = ffurl_open(&s->hd, h_url, AVIO_FLAG_READ_WRITE, NULL, &opt_dict);\ncleanup:\n av_freep(&user);\n av_freep(&headers);\n av_dict_free(&opt_dict);\n return ret;\n}', 'static char *cat_header(char buf[], const char key[], const char value[])\n{\n if (NOT_EMPTY(value)) {\n int len = strlen(key) + strlen(value) + 5;\n int is_first = !buf;\n char *tmp = NULL;\n if (buf)\n len += strlen(buf);\n if (!(tmp = av_realloc(buf, len))) {\n av_freep(&buf);\n return NULL;\n } else {\n buf = tmp;\n }\n if (is_first)\n *buf = \'\\0\';\n av_strlcatf(buf, len, "%s: %s\\r\\n", key, value);\n }\n return buf;\n}', 'void *av_realloc(void *ptr, size_t size)\n{\n#if CONFIG_MEMALIGN_HACK\n int diff;\n#endif\n if (size > (INT_MAX - 16))\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n if (!ptr)\n return av_malloc(size);\n diff = ((char *)ptr)[-1];\n return (char *)realloc((char *)ptr - diff, size + diff) + diff;\n#elif HAVE_ALIGNED_MALLOC\n return _aligned_realloc(ptr, size, 32);\n#else\n return realloc(ptr, size);\n#endif\n}', 'size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...)\n{\n int len = strlen(dst);\n va_list vl;\n va_start(vl, fmt);\n len += vsnprintf(dst + len, size > len ? size - len : 0, fmt, vl);\n va_end(vl);\n return len;\n}'] |
546 | 0 | https://github.com/openssl/openssl/blob/622ec449e5fe8f1e6bed036705ea97c74c9be1b7/ssl/ssl_cert.c/#L741 | int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
const char *dir)
{
DIR *d;
struct dirent *dstruct;
int ret = 0;
CRYPTO_w_lock(CRYPTO_LOCK_READDIR);
d = opendir(dir);
if(!d)
{
SYSerr(SYS_F_OPENDIR, get_last_sys_error());
ERR_add_error_data(3, "opendir('", dir, "')");
SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK, ERR_R_SYS_LIB);
goto err;
}
while((dstruct=readdir(d)))
{
char buf[1024];
int r;
if(strlen(dir)+strlen(dstruct->d_name)+2 > sizeof buf)
{
SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK,SSL_R_PATH_TOO_LONG);
goto err;
}
r = BIO_snprintf(buf,sizeof buf,"%s/%s",dir,dstruct->d_name);
if (r <= 0 || r >= sizeof buf)
goto err;
if(!SSL_add_file_cert_subjects_to_stack(stack,buf))
goto err;
}
ret = 1;
err:
CRYPTO_w_unlock(CRYPTO_LOCK_READDIR);
return ret;
} | ['int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,\n\t\t\t\t const char *dir)\n\t{\n\tDIR *d;\n\tstruct dirent *dstruct;\n\tint ret = 0;\n\tCRYPTO_w_lock(CRYPTO_LOCK_READDIR);\n\td = opendir(dir);\n\tif(!d)\n\t\t{\n\t\tSYSerr(SYS_F_OPENDIR, get_last_sys_error());\n\t\tERR_add_error_data(3, "opendir(\'", dir, "\')");\n\t\tSSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK, ERR_R_SYS_LIB);\n\t\tgoto err;\n\t\t}\n\twhile((dstruct=readdir(d)))\n\t\t{\n\t\tchar buf[1024];\n\t\tint r;\n\t\tif(strlen(dir)+strlen(dstruct->d_name)+2 > sizeof buf)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK,SSL_R_PATH_TOO_LONG);\n\t\t\tgoto err;\n\t\t\t}\n\t\tr = BIO_snprintf(buf,sizeof buf,"%s/%s",dir,dstruct->d_name);\n\t\tif (r <= 0 || r >= sizeof buf)\n\t\t\tgoto err;\n\t\tif(!SSL_add_file_cert_subjects_to_stack(stack,buf))\n\t\t\tgoto err;\n\t\t}\n\tret = 1;\nerr:\n\tCRYPTO_w_unlock(CRYPTO_LOCK_READDIR);\n\treturn ret;\n\t}', 'void CRYPTO_lock(int mode, int type, const char *file, int line)\n\t{\n#ifdef LOCK_DEBUG\n\t\t{\n\t\tchar *rw_text,*operation_text;\n\t\tif (mode & CRYPTO_LOCK)\n\t\t\toperation_text="lock ";\n\t\telse if (mode & CRYPTO_UNLOCK)\n\t\t\toperation_text="unlock";\n\t\telse\n\t\t\toperation_text="ERROR ";\n\t\tif (mode & CRYPTO_READ)\n\t\t\trw_text="r";\n\t\telse if (mode & CRYPTO_WRITE)\n\t\t\trw_text="w";\n\t\telse\n\t\t\trw_text="ERROR";\n\t\tfprintf(stderr,"lock:%08lx:(%s)%s %-18s %s:%d\\n",\n\t\t\tCRYPTO_thread_id(), rw_text, operation_text,\n\t\t\tCRYPTO_get_lock_name(type), file, line);\n\t\t}\n#endif\n\tif (type < 0)\n\t\t{\n\t\tint i = -type - 1;\n\t\tstruct CRYPTO_dynlock_value *pointer\n\t\t\t= CRYPTO_get_dynlock_value(i);\n\t\tif (pointer)\n\t\t\t{\n\t\t\tdynlock_lock_callback(mode, pointer, file, line);\n\t\t\t}\n\t\tCRYPTO_destroy_dynlockid(i);\n\t\t}\n\telse\n\t\tif (locking_callback != NULL)\n\t\t\tlocking_callback(mode,type,file,line);\n\t}'] |
547 | 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)];
} | ['BN_BLINDING *BN_BLINDING_create_param(BN_BLINDING *b,\n const BIGNUM *e, BIGNUM *m, BN_CTX *ctx,\n int (*bn_mod_exp) (BIGNUM *r,\n const BIGNUM *a,\n const BIGNUM *p,\n const BIGNUM *m,\n BN_CTX *ctx,\n BN_MONT_CTX *m_ctx),\n BN_MONT_CTX *m_ctx)\n{\n int retry_counter = 32;\n BN_BLINDING *ret = NULL;\n if (b == NULL)\n ret = BN_BLINDING_new(NULL, NULL, m);\n else\n ret = b;\n if (ret == NULL)\n goto err;\n if (ret->A == NULL && (ret->A = BN_new()) == NULL)\n goto err;\n if (ret->Ai == NULL && (ret->Ai = BN_new()) == NULL)\n goto err;\n if (e != NULL) {\n BN_free(ret->e);\n ret->e = BN_dup(e);\n }\n if (ret->e == NULL)\n goto err;\n if (bn_mod_exp != NULL)\n ret->bn_mod_exp = bn_mod_exp;\n if (m_ctx != NULL)\n ret->m_ctx = m_ctx;\n do {\n int rv;\n if (!BN_priv_rand_range(ret->A, ret->mod))\n goto err;\n if (int_bn_mod_inverse(ret->Ai, ret->A, ret->mod, ctx, &rv))\n break;\n if (!rv)\n goto err;\n if (retry_counter-- == 0) {\n BNerr(BN_F_BN_BLINDING_CREATE_PARAM, BN_R_TOO_MANY_ITERATIONS);\n goto err;\n }\n } while (1);\n if (ret->bn_mod_exp != NULL && ret->m_ctx != NULL) {\n if (!ret->bn_mod_exp(ret->A, ret->A, ret->e, ret->mod, ctx, ret->m_ctx))\n goto err;\n } else {\n if (!BN_mod_exp(ret->A, ret->A, ret->e, ret->mod, ctx))\n goto err;\n }\n if (ret->m_ctx != NULL) {\n if (!bn_to_mont_fixed_top(ret->Ai, ret->Ai, ret->m_ctx, ctx)\n || !bn_to_mont_fixed_top(ret->A, ret->A, ret->m_ctx, ctx))\n goto err;\n }\n return ret;\n err:\n if (b == NULL) {\n BN_BLINDING_free(ret);\n ret = NULL;\n }\n return ret;\n}', 'BIGNUM *int_bn_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,\n int *pnoinv)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM *ret = NULL;\n int sign;\n if (BN_abs_is_word(n, 1) || BN_is_zero(n)) {\n if (pnoinv != NULL)\n *pnoinv = 1;\n return NULL;\n }\n if (pnoinv != NULL)\n *pnoinv = 0;\n if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {\n return BN_mod_inverse_no_branch(in, a, n, ctx);\n }\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n if (!BN_nnmod(B, B, A, ctx))\n goto err;\n }\n sign = -1;\n if (BN_is_odd(n) && (BN_num_bits(n) <= 2048)) {\n int shift;\n while (!BN_is_zero(B)) {\n shift = 0;\n while (!BN_is_bit_set(B, shift)) {\n shift++;\n if (BN_is_odd(X)) {\n if (!BN_uadd(X, X, n))\n goto err;\n }\n if (!BN_rshift1(X, X))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(B, B, shift))\n goto err;\n }\n shift = 0;\n while (!BN_is_bit_set(A, shift)) {\n shift++;\n if (BN_is_odd(Y)) {\n if (!BN_uadd(Y, Y, n))\n goto err;\n }\n if (!BN_rshift1(Y, Y))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(A, A, shift))\n goto err;\n }\n if (BN_ucmp(B, A) >= 0) {\n if (!BN_uadd(X, X, Y))\n goto err;\n if (!BN_usub(B, B, A))\n goto err;\n } else {\n if (!BN_uadd(Y, Y, X))\n goto err;\n if (!BN_usub(A, A, B))\n goto err;\n }\n }\n } else {\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n if (BN_num_bits(A) == BN_num_bits(B)) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else if (BN_num_bits(A) == BN_num_bits(B) + 1) {\n if (!BN_lshift1(T, B))\n goto err;\n if (BN_ucmp(A, T) < 0) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else {\n if (!BN_sub(M, A, T))\n goto err;\n if (!BN_add(D, T, B))\n goto err;\n if (BN_ucmp(A, D) < 0) {\n if (!BN_set_word(D, 2))\n goto err;\n } else {\n if (!BN_set_word(D, 3))\n goto err;\n if (!BN_sub(M, M, B))\n goto err;\n }\n }\n } else {\n if (!BN_div(D, M, A, B, ctx))\n goto err;\n }\n tmp = A;\n A = B;\n B = M;\n if (BN_is_one(D)) {\n if (!BN_add(tmp, X, Y))\n goto err;\n } else {\n if (BN_is_word(D, 2)) {\n if (!BN_lshift1(tmp, X))\n goto err;\n } else if (BN_is_word(D, 4)) {\n if (!BN_lshift(tmp, X, 2))\n goto err;\n } else if (D->top == 1) {\n if (!BN_copy(tmp, X))\n goto err;\n if (!BN_mul_word(tmp, D->d[0]))\n goto err;\n } else {\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n }\n if (!BN_add(tmp, tmp, Y))\n goto err;\n }\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n if (pnoinv)\n *pnoinv = 1;\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return ret;\n}', 'static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n,\n BN_CTX *ctx)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM *ret = NULL;\n int sign;\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n {\n BIGNUM local_B;\n bn_init(&local_B);\n BN_with_flags(&local_B, B, BN_FLG_CONSTTIME);\n if (!BN_nnmod(B, &local_B, A, ctx))\n goto err;\n }\n }\n sign = -1;\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n {\n BIGNUM local_A;\n bn_init(&local_A);\n BN_with_flags(&local_A, A, BN_FLG_CONSTTIME);\n if (!BN_div(D, M, &local_A, B, ctx))\n goto err;\n }\n tmp = A;\n A = B;\n B = M;\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n if (!BN_add(tmp, tmp, Y))\n goto err;\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n BNerr(BN_F_BN_MOD_INVERSE_NO_BRANCH, BN_R_NO_INVERSE);\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_start()", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG("LEAVE BN_CTX_start()", ctx);\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG("ENTER BN_CTX_get()", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ret->flags &= (~BN_FLG_CONSTTIME);\n ctx->used++;\n CTXDBG("LEAVE BN_CTX_get()", ctx);\n return ret;\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int ret;\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return 0;\n }\n if (divisor->d[divisor->top - 1] == 0) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n ret = bn_div_fixed_top(dv, rm, num, divisor, ctx);\n if (ret) {\n if (dv != NULL)\n bn_correct_top(dv);\n if (rm != NULL)\n bn_correct_top(rm);\n }\n return ret;\n}', 'int bn_div_fixed_top(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,\n const BIGNUM *divisor, BN_CTX *ctx)\n{\n int norm_shift, i, j, loop;\n BIGNUM *tmp, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnum, *wnumtop;\n BN_ULONG d0, d1;\n int num_n, div_n;\n assert(divisor->top > 0 && divisor->d[divisor->top - 1] != 0);\n bn_check_top(num);\n bn_check_top(divisor);\n bn_check_top(dv);\n bn_check_top(rm);\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n if (!BN_copy(sdiv, divisor))\n goto err;\n norm_shift = bn_left_align(sdiv);\n sdiv->neg = 0;\n if (!(bn_lshift_fixed_top(snum, num, norm_shift)))\n goto err;\n div_n = sdiv->top;\n num_n = snum->top;\n if (num_n <= div_n) {\n if (bn_wexpand(snum, div_n + 1) == NULL)\n goto err;\n memset(&(snum->d[num_n]), 0, (div_n - num_n + 1) * sizeof(BN_ULONG));\n snum->top = num_n = div_n + 1;\n }\n loop = num_n - div_n;\n wnum = &(snum->d[loop]);\n wnumtop = &(snum->d[num_n - 1]);\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n if (!bn_wexpand(res, loop))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop;\n res->flags |= BN_FLG_FIXED_TOP;\n resp = &(res->d[loop]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n for (i = 0; i < loop; i++, wnumtop--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W)\n q = bn_div_3_words(wnumtop, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnumtop[0];\n n1 = wnumtop[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n BN_ULONG n2 = (wnumtop == wnum) ? 0 : wnumtop[-2];\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | n2))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= n2)))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum--;\n l0 = bn_sub_words(wnum, wnum, tmp->d, div_n + 1);\n q -= l0;\n for (l0 = 0 - l0, j = 0; j < div_n; j++)\n tmp->d[j] = sdiv->d[j] & l0;\n l0 = bn_add_words(wnum, wnum, tmp->d, div_n);\n (*wnumtop) += l0;\n assert((*wnumtop) == 0);\n *--resp = q;\n }\n snum->neg = num->neg;\n snum->top = div_n;\n snum->flags |= BN_FLG_FIXED_TOP;\n if (rm != NULL)\n bn_rshift_fixed_top(rm, snum, norm_shift);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return 0;\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_end()", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG("LEAVE BN_CTX_end()", ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}'] |
548 | 0 | https://github.com/openssl/openssl/blob/ea32151f7b9353f8906188d007c6893704ac17bb/crypto/asn1/asn1_lib.c/#L124 | static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
long max)
{
const unsigned char *p = *pp;
unsigned long ret = 0;
unsigned long i;
if (max-- < 1)
return 0;
if (*p == 0x80) {
*inf = 1;
ret = 0;
p++;
} else {
*inf = 0;
i = *p & 0x7f;
if (*(p++) & 0x80) {
if (max < (long)i + 1)
return 0;
while (i && *p == 0) {
p++;
i--;
}
if (i > sizeof(long))
return 0;
while (i-- > 0) {
ret <<= 8L;
ret |= *(p++);
}
} else
ret = i;
}
if (ret > LONG_MAX)
return 0;
*pp = p;
*rl = (long)ret;
return 1;
} | ['void *PKCS12_item_decrypt_d2i(X509_ALGOR *algor, const ASN1_ITEM *it,\n const char *pass, int passlen,\n ASN1_OCTET_STRING *oct, int zbuf)\n{\n unsigned char *out;\n const unsigned char *p;\n void *ret;\n int outlen;\n if (!PKCS12_pbe_crypt(algor, pass, passlen, oct->data, oct->length,\n &out, &outlen, 0)) {\n PKCS12err(PKCS12_F_PKCS12_ITEM_DECRYPT_D2I,\n PKCS12_R_PKCS12_PBE_CRYPT_ERROR);\n return NULL;\n }\n p = out;\n#ifdef OPENSSL_DEBUG_DECRYPT\n {\n FILE *op;\n char fname[30];\n static int fnm = 1;\n sprintf(fname, "DER%d", fnm++);\n op = fopen(fname, "wb");\n fwrite(p, 1, outlen, op);\n fclose(op);\n }\n#endif\n ret = ASN1_item_d2i(NULL, &p, outlen, it);\n if (zbuf)\n OPENSSL_cleanse(out, outlen);\n if (!ret)\n PKCS12err(PKCS12_F_PKCS12_ITEM_DECRYPT_D2I, PKCS12_R_DECODE_ERROR);\n OPENSSL_free(out);\n return ret;\n}', 'unsigned char *PKCS12_pbe_crypt(X509_ALGOR *algor, const char *pass,\n int passlen, unsigned char *in, int inlen,\n unsigned char **data, int *datalen, int en_de)\n{\n unsigned char *out = NULL;\n int outlen, i;\n EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();\n if (ctx == NULL) {\n PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (!EVP_PBE_CipherInit(algor->algorithm, pass, passlen,\n algor->parameter, ctx, en_de)) {\n PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT,\n PKCS12_R_PKCS12_ALGOR_CIPHERINIT_ERROR);\n goto err;\n }\n if ((out = OPENSSL_malloc(inlen + EVP_CIPHER_CTX_block_size(ctx)))\n == NULL) {\n PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (!EVP_CipherUpdate(ctx, out, &i, in, inlen)) {\n OPENSSL_free(out);\n out = NULL;\n PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT, ERR_R_EVP_LIB);\n goto err;\n }\n outlen = i;\n if (!EVP_CipherFinal_ex(ctx, out + i, &i)) {\n OPENSSL_free(out);\n out = NULL;\n PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT,\n PKCS12_R_PKCS12_CIPHERFINAL_ERROR);\n goto err;\n }\n outlen += i;\n if (datalen)\n *datalen = outlen;\n if (data)\n *data = out;\n err:\n EVP_CIPHER_CTX_free(ctx);\n return out;\n}', 'ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,\n const unsigned char **in, long len,\n const ASN1_ITEM *it)\n{\n ASN1_TLC c;\n ASN1_VALUE *ptmpval = NULL;\n if (!pval)\n pval = &ptmpval;\n asn1_tlc_clear_nc(&c);\n if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)\n return *pval;\n return NULL;\n}', 'int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,\n const ASN1_ITEM *it,\n int tag, int aclass, char opt, ASN1_TLC *ctx)\n{\n int rv;\n rv = asn1_item_embed_d2i(pval, in, len, it, tag, aclass, opt, ctx);\n if (rv <= 0)\n ASN1_item_ex_free(pval, it);\n return rv;\n}', 'static int asn1_item_embed_d2i(ASN1_VALUE **pval, const unsigned char **in,\n long len, const ASN1_ITEM *it,\n int tag, int aclass, char opt, ASN1_TLC *ctx)\n{\n const ASN1_TEMPLATE *tt, *errtt = NULL;\n const ASN1_EXTERN_FUNCS *ef;\n const ASN1_AUX *aux = it->funcs;\n ASN1_aux_cb *asn1_cb;\n const unsigned char *p = NULL, *q;\n unsigned char oclass;\n char seq_eoc, seq_nolen, cst, isopt;\n long tmplen;\n int i;\n int otag;\n int ret = 0;\n ASN1_VALUE **pchptr;\n if (!pval)\n return 0;\n if (aux && aux->asn1_cb)\n asn1_cb = aux->asn1_cb;\n else\n asn1_cb = 0;\n switch (it->itype) {\n case ASN1_ITYPE_PRIMITIVE:\n if (it->templates) {\n if ((tag != -1) || opt) {\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I,\n ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);\n goto err;\n }\n return asn1_template_ex_d2i(pval, in, len,\n it->templates, opt, ctx);\n }\n return asn1_d2i_ex_primitive(pval, in, len, it,\n tag, aclass, opt, ctx);\n case ASN1_ITYPE_MSTRING:\n p = *in;\n ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,\n &p, len, -1, 0, 1, ctx);\n if (!ret) {\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);\n goto err;\n }\n if (oclass != V_ASN1_UNIVERSAL) {\n if (opt)\n return -1;\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MSTRING_NOT_UNIVERSAL);\n goto err;\n }\n if (!(ASN1_tag2bit(otag) & it->utype)) {\n if (opt)\n return -1;\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MSTRING_WRONG_TAG);\n goto err;\n }\n return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);\n case ASN1_ITYPE_EXTERN:\n ef = it->funcs;\n return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);\n case ASN1_ITYPE_CHOICE:\n if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))\n goto auxerr;\n if (*pval) {\n i = asn1_get_choice_selector(pval, it);\n if ((i >= 0) && (i < it->tcount)) {\n tt = it->templates + i;\n pchptr = asn1_get_field_ptr(pval, tt);\n asn1_template_free(pchptr, tt);\n asn1_set_choice_selector(pval, -1, it);\n }\n } else if (!ASN1_item_ex_new(pval, it)) {\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);\n goto err;\n }\n p = *in;\n for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {\n pchptr = asn1_get_field_ptr(pval, tt);\n ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx);\n if (ret == -1)\n continue;\n asn1_set_choice_selector(pval, i, it);\n if (ret > 0)\n break;\n errtt = tt;\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);\n goto err;\n }\n if (i == it->tcount) {\n if (opt) {\n ASN1_item_ex_free(pval, it);\n return -1;\n }\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_NO_MATCHING_CHOICE_TYPE);\n goto err;\n }\n if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))\n goto auxerr;\n *in = p;\n return 1;\n case ASN1_ITYPE_NDEF_SEQUENCE:\n case ASN1_ITYPE_SEQUENCE:\n p = *in;\n tmplen = len;\n if (tag == -1) {\n tag = V_ASN1_SEQUENCE;\n aclass = V_ASN1_UNIVERSAL;\n }\n ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,\n &p, len, tag, aclass, opt, ctx);\n if (!ret) {\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);\n goto err;\n } else if (ret == -1)\n return -1;\n if (aux && (aux->flags & ASN1_AFLG_BROKEN)) {\n len = tmplen - (p - *in);\n seq_nolen = 1;\n }\n else\n seq_nolen = seq_eoc;\n if (!cst) {\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);\n goto err;\n }\n if (!*pval && !ASN1_item_ex_new(pval, it)) {\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);\n goto err;\n }\n if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))\n goto auxerr;\n for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {\n if (tt->flags & ASN1_TFLG_ADB_MASK) {\n const ASN1_TEMPLATE *seqtt;\n ASN1_VALUE **pseqval;\n seqtt = asn1_do_adb(pval, tt, 1);\n pseqval = asn1_get_field_ptr(pval, seqtt);\n asn1_template_free(pseqval, seqtt);\n }\n }\n for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {\n const ASN1_TEMPLATE *seqtt;\n ASN1_VALUE **pseqval;\n seqtt = asn1_do_adb(pval, tt, 1);\n if (!seqtt)\n goto err;\n pseqval = asn1_get_field_ptr(pval, seqtt);\n if (!len)\n break;\n q = p;\n if (asn1_check_eoc(&p, len)) {\n if (!seq_eoc) {\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_UNEXPECTED_EOC);\n goto err;\n }\n len -= p - q;\n seq_eoc = 0;\n q = p;\n break;\n }\n if (i == (it->tcount - 1))\n isopt = 0;\n else\n isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);\n ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx);\n if (!ret) {\n errtt = seqtt;\n goto err;\n } else if (ret == -1) {\n asn1_template_free(pseqval, seqtt);\n continue;\n }\n len -= p - q;\n }\n if (seq_eoc && !asn1_check_eoc(&p, len)) {\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MISSING_EOC);\n goto err;\n }\n if (!seq_nolen && len) {\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_SEQUENCE_LENGTH_MISMATCH);\n goto err;\n }\n for (; i < it->tcount; tt++, i++) {\n const ASN1_TEMPLATE *seqtt;\n seqtt = asn1_do_adb(pval, tt, 1);\n if (!seqtt)\n goto err;\n if (seqtt->flags & ASN1_TFLG_OPTIONAL) {\n ASN1_VALUE **pseqval;\n pseqval = asn1_get_field_ptr(pval, seqtt);\n asn1_template_free(pseqval, seqtt);\n } else {\n errtt = seqtt;\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_FIELD_MISSING);\n goto err;\n }\n }\n if (!asn1_enc_save(pval, *in, p - *in, it))\n goto auxerr;\n if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))\n goto auxerr;\n *in = p;\n return 1;\n default:\n return 0;\n }\n auxerr:\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_AUX_ERROR);\n err:\n if (errtt)\n ERR_add_error_data(4, "Field=", errtt->field_name,\n ", Type=", it->sname);\n else\n ERR_add_error_data(2, "Type=", it->sname);\n return 0;\n}', 'static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,\n char *inf, char *cst,\n const unsigned char **in, long len,\n int exptag, int expclass, char opt, ASN1_TLC *ctx)\n{\n int i;\n int ptag, pclass;\n long plen;\n const unsigned char *p, *q;\n p = *in;\n q = p;\n if (ctx && ctx->valid) {\n i = ctx->ret;\n plen = ctx->plen;\n pclass = ctx->pclass;\n ptag = ctx->ptag;\n p += ctx->hdrlen;\n } else {\n i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);\n if (ctx) {\n ctx->ret = i;\n ctx->plen = plen;\n ctx->pclass = pclass;\n ctx->ptag = ptag;\n ctx->hdrlen = p - q;\n ctx->valid = 1;\n if (!(i & 0x81) && ((plen + ctx->hdrlen) > len)) {\n ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_TOO_LONG);\n asn1_tlc_clear(ctx);\n return 0;\n }\n }\n }\n if (i & 0x80) {\n ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER);\n asn1_tlc_clear(ctx);\n return 0;\n }\n if (exptag >= 0) {\n if ((exptag != ptag) || (expclass != pclass)) {\n if (opt)\n return -1;\n asn1_tlc_clear(ctx);\n ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);\n return 0;\n }\n asn1_tlc_clear(ctx);\n }\n if (i & 1)\n plen = len - (p - q);\n if (inf)\n *inf = i & 1;\n if (cst)\n *cst = i & V_ASN1_CONSTRUCTED;\n if (olen)\n *olen = plen;\n if (oclass)\n *oclass = pclass;\n if (otag)\n *otag = ptag;\n *in = p;\n return 1;\n}', 'int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,\n int *pclass, long omax)\n{\n int i, ret;\n long l;\n const unsigned char *p = *pp;\n int tag, xclass, inf;\n long max = omax;\n if (!max)\n goto err;\n ret = (*p & V_ASN1_CONSTRUCTED);\n xclass = (*p & V_ASN1_PRIVATE);\n i = *p & V_ASN1_PRIMITIVE_TAG;\n if (i == V_ASN1_PRIMITIVE_TAG) {\n p++;\n if (--max == 0)\n goto err;\n l = 0;\n while (*p & 0x80) {\n l <<= 7L;\n l |= *(p++) & 0x7f;\n if (--max == 0)\n goto err;\n if (l > (INT_MAX >> 7L))\n goto err;\n }\n l <<= 7L;\n l |= *(p++) & 0x7f;\n tag = (int)l;\n if (--max == 0)\n goto err;\n } else {\n tag = i;\n p++;\n if (--max == 0)\n goto err;\n }\n *ptag = tag;\n *pclass = xclass;\n if (!asn1_get_length(&p, &inf, plength, max))\n goto err;\n if (inf && !(ret & V_ASN1_CONSTRUCTED))\n goto err;\n if (*plength > (omax - (p - *pp))) {\n ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_TOO_LONG);\n ret |= 0x80;\n }\n *pp = p;\n return (ret | inf);\n err:\n ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_HEADER_TOO_LONG);\n return (0x80);\n}', 'static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,\n long max)\n{\n const unsigned char *p = *pp;\n unsigned long ret = 0;\n unsigned long i;\n if (max-- < 1)\n return 0;\n if (*p == 0x80) {\n *inf = 1;\n ret = 0;\n p++;\n } else {\n *inf = 0;\n i = *p & 0x7f;\n if (*(p++) & 0x80) {\n if (max < (long)i + 1)\n return 0;\n while (i && *p == 0) {\n p++;\n i--;\n }\n if (i > sizeof(long))\n return 0;\n while (i-- > 0) {\n ret <<= 8L;\n ret |= *(p++);\n }\n } else\n ret = i;\n }\n if (ret > LONG_MAX)\n return 0;\n *pp = p;\n *rl = (long)ret;\n return 1;\n}'] |
549 | 0 | https://github.com/openssl/openssl/blob/183733f882056ea3e6fe95e665b85fcc6a45dcb4/test/bntest.c/#L1242 | int test_gf2m_add(BIO *bp)
{
BIGNUM *a, *b, *c;
int i, ret = 0;
a = BN_new();
b = BN_new();
c = BN_new();
for (i = 0; i < num0; i++) {
BN_rand(a, 512, 0, 0);
BN_copy(b, BN_value_one());
a->neg = rand_neg();
b->neg = rand_neg();
BN_GF2m_add(c, a, b);
if ((BN_is_odd(a) && BN_is_odd(c))
|| (!BN_is_odd(a) && !BN_is_odd(c))) {
fprintf(stderr, "GF(2^m) addition test (a) failed!\n");
goto err;
}
BN_GF2m_add(c, c, c);
if (!BN_is_zero(c)) {
fprintf(stderr, "GF(2^m) addition test (b) failed!\n");
goto err;
}
}
ret = 1;
err:
BN_free(a);
BN_free(b);
BN_free(c);
return ret;
} | ['int test_gf2m_add(BIO *bp)\n{\n BIGNUM *a, *b, *c;\n int i, ret = 0;\n a = BN_new();\n b = BN_new();\n c = BN_new();\n for (i = 0; i < num0; i++) {\n BN_rand(a, 512, 0, 0);\n BN_copy(b, BN_value_one());\n a->neg = rand_neg();\n b->neg = rand_neg();\n BN_GF2m_add(c, a, b);\n if ((BN_is_odd(a) && BN_is_odd(c))\n || (!BN_is_odd(a) && !BN_is_odd(c))) {\n fprintf(stderr, "GF(2^m) addition test (a) failed!\\n");\n goto err;\n }\n BN_GF2m_add(c, c, c);\n if (!BN_is_zero(c)) {\n fprintf(stderr, "GF(2^m) addition test (b) failed!\\n");\n goto err;\n }\n }\n ret = 1;\n err:\n BN_free(a);\n BN_free(b);\n BN_free(c);\n return ret;\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}', 'int BN_rand(BIGNUM *rnd, int bits, int top, int bottom)\n{\n return bnrand(0, rnd, bits, top, bottom);\n}', 'static int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)\n{\n unsigned char *buf = NULL;\n int ret = 0, bit, bytes, mask;\n time_t tim;\n if (bits < 0 || (bits == 1 && top > 0)) {\n BNerr(BN_F_BNRAND, BN_R_BITS_TOO_SMALL);\n return 0;\n }\n if (bits == 0) {\n BN_zero(rnd);\n return 1;\n }\n bytes = (bits + 7) / 8;\n bit = (bits - 1) % 8;\n mask = 0xff << (bit + 1);\n buf = OPENSSL_malloc(bytes);\n if (buf == NULL) {\n BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n time(&tim);\n RAND_add(&tim, sizeof(tim), 0.0);\n if (pseudorand) {\n if (RAND_bytes(buf, bytes) <= 0)\n goto err;\n } else {\n if (RAND_bytes(buf, bytes) <= 0)\n goto err;\n }\n if (pseudorand == 2) {\n int i;\n unsigned char c;\n for (i = 0; i < bytes; i++) {\n if (RAND_bytes(&c, 1) <= 0)\n goto err;\n if (c >= 128 && i > 0)\n buf[i] = buf[i - 1];\n else if (c < 42)\n buf[i] = 0;\n else if (c < 84)\n buf[i] = 255;\n }\n }\n if (top >= 0) {\n if (top) {\n if (bit == 0) {\n buf[0] = 1;\n buf[1] |= 0x80;\n } else {\n buf[0] |= (3 << (bit - 1));\n }\n } else {\n buf[0] |= (1 << bit);\n }\n }\n buf[0] &= ~mask;\n if (bottom)\n buf[bytes - 1] |= 1;\n if (!BN_bin2bn(buf, bytes, rnd))\n goto err;\n ret = 1;\n err:\n OPENSSL_clear_free(buf, bytes);\n bn_check_top(rnd);\n return (ret);\n}', 'void CRYPTO_clear_free(void *str, size_t num)\n{\n if (str == NULL)\n return;\n if (num)\n OPENSSL_cleanse(str, num);\n CRYPTO_free(str);\n}', 'const BIGNUM *BN_value_one(void)\n{\n static const BN_ULONG data_one = 1L;\n static const BIGNUM const_one =\n { (BN_ULONG *)&data_one, 1, 1, 0, BN_FLG_STATIC_DATA };\n return (&const_one);\n}', 'int rand_neg(void)\n{\n static unsigned int neg = 0;\n static int sign[8] = { 0, 0, 0, 1, 1, 0, 1, 1 };\n return (sign[(neg++) % 8]);\n}'] |
550 | 0 | https://github.com/openssl/openssl/blob/7fcdbd839c629f5419a49bf8da28c968c8140c3d/ssl/statem/statem_lib.c/#L504 | MSG_PROCESS_RETURN tls_process_cert_verify(SSL *s, PACKET *pkt)
{
EVP_PKEY *pkey = NULL;
const unsigned char *data;
#ifndef OPENSSL_NO_GOST
unsigned char *gost_data = NULL;
#endif
MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR;
int j;
unsigned int len;
X509 *peer;
const EVP_MD *md = NULL;
size_t hdatalen = 0;
void *hdata;
unsigned char tls13tbs[TLS13_TBS_PREAMBLE_SIZE + EVP_MAX_MD_SIZE];
EVP_MD_CTX *mctx = EVP_MD_CTX_new();
EVP_PKEY_CTX *pctx = NULL;
if (mctx == NULL) {
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
ERR_R_MALLOC_FAILURE);
goto err;
}
peer = s->session->peer;
pkey = X509_get0_pubkey(peer);
if (pkey == NULL) {
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
ERR_R_INTERNAL_ERROR);
goto err;
}
if (ssl_cert_lookup_by_pkey(pkey, NULL) == NULL) {
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_CERT_VERIFY,
SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
goto err;
}
if (SSL_USE_SIGALGS(s)) {
unsigned int sigalg;
if (!PACKET_get_net_2(pkt, &sigalg)) {
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
SSL_R_BAD_PACKET);
goto err;
}
if (tls12_check_peer_sigalg(s, sigalg, pkey) <= 0) {
goto err;
}
#ifdef SSL_DEBUG
fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
#endif
} else if (!tls1_set_peer_legacy_sigalg(s, pkey)) {
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
ERR_R_INTERNAL_ERROR);
goto err;
}
if (!tls1_lookup_md(s->s3->tmp.peer_sigalg, &md)) {
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
ERR_R_INTERNAL_ERROR);
goto err;
}
#ifndef OPENSSL_NO_GOST
if (!SSL_USE_SIGALGS(s)
&& ((PACKET_remaining(pkt) == 64
&& (EVP_PKEY_id(pkey) == NID_id_GostR3410_2001
|| EVP_PKEY_id(pkey) == NID_id_GostR3410_2012_256))
|| (PACKET_remaining(pkt) == 128
&& EVP_PKEY_id(pkey) == NID_id_GostR3410_2012_512))) {
len = PACKET_remaining(pkt);
} else
#endif
if (!PACKET_get_net_2(pkt, &len)) {
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
SSL_R_LENGTH_MISMATCH);
goto err;
}
j = EVP_PKEY_size(pkey);
if (((int)len > j) || ((int)PACKET_remaining(pkt) > j)
|| (PACKET_remaining(pkt) == 0)) {
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
SSL_R_WRONG_SIGNATURE_SIZE);
goto err;
}
if (!PACKET_get_bytes(pkt, &data, len)) {
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
SSL_R_LENGTH_MISMATCH);
goto err;
}
if (!get_cert_verify_tbs_data(s, tls13tbs, &hdata, &hdatalen)) {
goto err;
}
#ifdef SSL_DEBUG
fprintf(stderr, "Using client verify alg %s\n", EVP_MD_name(md));
#endif
if (EVP_DigestVerifyInit(mctx, &pctx, md, NULL, pkey) <= 0) {
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
ERR_R_EVP_LIB);
goto err;
}
#ifndef OPENSSL_NO_GOST
{
int pktype = EVP_PKEY_id(pkey);
if (pktype == NID_id_GostR3410_2001
|| pktype == NID_id_GostR3410_2012_256
|| pktype == NID_id_GostR3410_2012_512) {
if ((gost_data = OPENSSL_malloc(len)) == NULL) {
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_MALLOC_FAILURE);
goto err;
}
BUF_reverse(gost_data, data, len);
data = gost_data;
}
}
#endif
if (SSL_USE_PSS(s)) {
if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
|| EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx,
RSA_PSS_SALTLEN_DIGEST) <= 0) {
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
ERR_R_EVP_LIB);
goto err;
}
}
if (s->version == SSL3_VERSION) {
if (EVP_DigestVerifyUpdate(mctx, hdata, hdatalen) <= 0
|| !EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET,
(int)s->session->master_key_length,
s->session->master_key)) {
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
ERR_R_EVP_LIB);
goto err;
}
if (EVP_DigestVerifyFinal(mctx, data, len) <= 0) {
SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
SSL_R_BAD_SIGNATURE);
goto err;
}
} else {
j = EVP_DigestVerify(mctx, data, len, hdata, hdatalen);
if (j <= 0) {
SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
SSL_R_BAD_SIGNATURE);
goto err;
}
}
ret = MSG_PROCESS_CONTINUE_READING;
err:
BIO_free(s->s3->handshake_buffer);
s->s3->handshake_buffer = NULL;
EVP_MD_CTX_free(mctx);
#ifndef OPENSSL_NO_GOST
OPENSSL_free(gost_data);
#endif
return ret;
} | ['MSG_PROCESS_RETURN tls_process_cert_verify(SSL *s, PACKET *pkt)\n{\n EVP_PKEY *pkey = NULL;\n const unsigned char *data;\n#ifndef OPENSSL_NO_GOST\n unsigned char *gost_data = NULL;\n#endif\n MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR;\n int j;\n unsigned int len;\n X509 *peer;\n const EVP_MD *md = NULL;\n size_t hdatalen = 0;\n void *hdata;\n unsigned char tls13tbs[TLS13_TBS_PREAMBLE_SIZE + EVP_MAX_MD_SIZE];\n EVP_MD_CTX *mctx = EVP_MD_CTX_new();\n EVP_PKEY_CTX *pctx = NULL;\n if (mctx == NULL) {\n SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,\n ERR_R_MALLOC_FAILURE);\n goto err;\n }\n peer = s->session->peer;\n pkey = X509_get0_pubkey(peer);\n if (pkey == NULL) {\n SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,\n ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if (ssl_cert_lookup_by_pkey(pkey, NULL) == NULL) {\n SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_CERT_VERIFY,\n SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);\n goto err;\n }\n if (SSL_USE_SIGALGS(s)) {\n unsigned int sigalg;\n if (!PACKET_get_net_2(pkt, &sigalg)) {\n SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,\n SSL_R_BAD_PACKET);\n goto err;\n }\n if (tls12_check_peer_sigalg(s, sigalg, pkey) <= 0) {\n goto err;\n }\n#ifdef SSL_DEBUG\n fprintf(stderr, "USING TLSv1.2 HASH %s\\n", EVP_MD_name(md));\n#endif\n } else if (!tls1_set_peer_legacy_sigalg(s, pkey)) {\n SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,\n ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if (!tls1_lookup_md(s->s3->tmp.peer_sigalg, &md)) {\n SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,\n ERR_R_INTERNAL_ERROR);\n goto err;\n }\n#ifndef OPENSSL_NO_GOST\n if (!SSL_USE_SIGALGS(s)\n && ((PACKET_remaining(pkt) == 64\n && (EVP_PKEY_id(pkey) == NID_id_GostR3410_2001\n || EVP_PKEY_id(pkey) == NID_id_GostR3410_2012_256))\n || (PACKET_remaining(pkt) == 128\n && EVP_PKEY_id(pkey) == NID_id_GostR3410_2012_512))) {\n len = PACKET_remaining(pkt);\n } else\n#endif\n if (!PACKET_get_net_2(pkt, &len)) {\n SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,\n SSL_R_LENGTH_MISMATCH);\n goto err;\n }\n j = EVP_PKEY_size(pkey);\n if (((int)len > j) || ((int)PACKET_remaining(pkt) > j)\n || (PACKET_remaining(pkt) == 0)) {\n SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,\n SSL_R_WRONG_SIGNATURE_SIZE);\n goto err;\n }\n if (!PACKET_get_bytes(pkt, &data, len)) {\n SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,\n SSL_R_LENGTH_MISMATCH);\n goto err;\n }\n if (!get_cert_verify_tbs_data(s, tls13tbs, &hdata, &hdatalen)) {\n goto err;\n }\n#ifdef SSL_DEBUG\n fprintf(stderr, "Using client verify alg %s\\n", EVP_MD_name(md));\n#endif\n if (EVP_DigestVerifyInit(mctx, &pctx, md, NULL, pkey) <= 0) {\n SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,\n ERR_R_EVP_LIB);\n goto err;\n }\n#ifndef OPENSSL_NO_GOST\n {\n int pktype = EVP_PKEY_id(pkey);\n if (pktype == NID_id_GostR3410_2001\n || pktype == NID_id_GostR3410_2012_256\n || pktype == NID_id_GostR3410_2012_512) {\n if ((gost_data = OPENSSL_malloc(len)) == NULL) {\n SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n BUF_reverse(gost_data, data, len);\n data = gost_data;\n }\n }\n#endif\n if (SSL_USE_PSS(s)) {\n if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0\n || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx,\n RSA_PSS_SALTLEN_DIGEST) <= 0) {\n SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,\n ERR_R_EVP_LIB);\n goto err;\n }\n }\n if (s->version == SSL3_VERSION) {\n if (EVP_DigestVerifyUpdate(mctx, hdata, hdatalen) <= 0\n || !EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET,\n (int)s->session->master_key_length,\n s->session->master_key)) {\n SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,\n ERR_R_EVP_LIB);\n goto err;\n }\n if (EVP_DigestVerifyFinal(mctx, data, len) <= 0) {\n SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,\n SSL_R_BAD_SIGNATURE);\n goto err;\n }\n } else {\n j = EVP_DigestVerify(mctx, data, len, hdata, hdatalen);\n if (j <= 0) {\n SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,\n SSL_R_BAD_SIGNATURE);\n goto err;\n }\n }\n ret = MSG_PROCESS_CONTINUE_READING;\n err:\n BIO_free(s->s3->handshake_buffer);\n s->s3->handshake_buffer = NULL;\n EVP_MD_CTX_free(mctx);\n#ifndef OPENSSL_NO_GOST\n OPENSSL_free(gost_data);\n#endif\n return ret;\n}', 'EVP_MD_CTX *EVP_MD_CTX_new(void)\n{\n return OPENSSL_zalloc(sizeof(EVP_MD_CTX));\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n INCREMENT(malloc_count);\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n 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}', 'EVP_PKEY *X509_get0_pubkey(const X509 *x)\n{\n if (x == NULL)\n return NULL;\n return X509_PUBKEY_get0(x->cert_info.key);\n}', 'EVP_PKEY *X509_PUBKEY_get0(X509_PUBKEY *key)\n{\n EVP_PKEY *ret = NULL;\n if (key == NULL || key->public_key == NULL)\n return NULL;\n if (key->pkey != NULL)\n return key->pkey;\n x509_pubkey_decode(&ret, key);\n if (ret != NULL) {\n X509err(X509_F_X509_PUBKEY_GET0, ERR_R_INTERNAL_ERROR);\n EVP_PKEY_free(ret);\n }\n return NULL;\n}', 'static int x509_pubkey_decode(EVP_PKEY **ppkey, X509_PUBKEY *key)\n{\n EVP_PKEY *pkey = EVP_PKEY_new();\n if (pkey == NULL) {\n X509err(X509_F_X509_PUBKEY_DECODE, ERR_R_MALLOC_FAILURE);\n return -1;\n }\n if (!EVP_PKEY_set_type(pkey, OBJ_obj2nid(key->algor->algorithm))) {\n X509err(X509_F_X509_PUBKEY_DECODE, X509_R_UNSUPPORTED_ALGORITHM);\n goto error;\n }\n if (pkey->ameth->pub_decode) {\n if (!pkey->ameth->pub_decode(pkey, key)) {\n X509err(X509_F_X509_PUBKEY_DECODE, X509_R_PUBLIC_KEY_DECODE_ERROR);\n goto error;\n }\n } else {\n X509err(X509_F_X509_PUBKEY_DECODE, X509_R_METHOD_NOT_SUPPORTED);\n goto error;\n }\n *ppkey = pkey;\n return 1;\n error:\n EVP_PKEY_free(pkey);\n return 0;\n}', 'void ossl_statem_fatal(SSL *s, int al, int func, int reason, const char *file,\n int line)\n{\n}', 'void EVP_MD_CTX_free(EVP_MD_CTX *ctx)\n{\n EVP_MD_CTX_reset(ctx);\n OPENSSL_free(ctx);\n}', 'void CRYPTO_free(void *str, const char *file, int line)\n{\n INCREMENT(free_count);\n if (free_impl != NULL && free_impl != &CRYPTO_free) {\n free_impl(str, file, line);\n return;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_free(str, 0, file, line);\n free(str);\n CRYPTO_mem_debug_free(str, 1, file, line);\n } else {\n free(str);\n }\n#else\n free(str);\n#endif\n}'] |
551 | 0 | https://github.com/libav/libav/blob/6961bdface651ac9bd3621738aeb3b65969033b6/libavfilter/formats.c/#L262 | void ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
{
FORMATS_REF(f, ref);
} | ['void ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)\n{\n FORMATS_REF(f, ref);\n}', 'void *av_realloc(void *ptr, size_t size)\n{\n#if CONFIG_MEMALIGN_HACK\n int diff;\n#endif\n if(size > (INT_MAX-16) )\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n if(!ptr) return av_malloc(size);\n diff= ((char*)ptr)[-1];\n return (char*)realloc((char*)ptr - diff, size + diff) + diff;\n#elif HAVE_ALIGNED_MALLOC\n return _aligned_realloc(ptr, size, 32);\n#else\n return realloc(ptr, size);\n#endif\n}'] |
552 | 0 | https://github.com/openssl/openssl/blob/5dc312215fa54b70cc8b4baec933bcc19525bc03/ssl/t1_lib.c/#L4057 | 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();
if (dhp == NULL)
return NULL;
dhp->g = BN_new();
if (dhp->g != NULL)
BN_set_word(dhp->g, 2);
if (dh_secbits >= 192)
dhp->p = get_rfc3526_prime_8192(NULL);
else
dhp->p = get_rfc3526_prime_3072(NULL);
if (dhp->p == NULL || dhp->g == NULL) {
DH_free(dhp);
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 if (dhp == NULL)\n return NULL;\n dhp->g = BN_new();\n if (dhp->g != NULL)\n BN_set_word(dhp->g, 2);\n if (dh_secbits >= 192)\n dhp->p = get_rfc3526_prime_8192(NULL);\n else\n dhp->p = get_rfc3526_prime_3072(NULL);\n if (dhp->p == NULL || dhp->g == NULL) {\n DH_free(dhp);\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}'] |
553 | 0 | https://github.com/libav/libav/blob/a451324dddf5d2ab4bcd6aa0f546596f71bdada3/libavformat/utils.c/#L462 | int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
{
int ret, i, err;
AVStream *st;
for (;;) {
AVPacketList *pktl = s->internal->raw_packet_buffer;
if (pktl) {
*pkt = pktl->pkt;
st = s->streams[pkt->stream_index];
if (st->codecpar->codec_id != AV_CODEC_ID_PROBE ||
!st->probe_packets ||
s->internal->raw_packet_buffer_remaining_size < pkt->size) {
AVProbeData *pd;
if (st->probe_packets)
if ((err = probe_codec(s, st, NULL)) < 0)
return err;
pd = &st->probe_data;
av_freep(&pd->buf);
pd->buf_size = 0;
s->internal->raw_packet_buffer = pktl->next;
s->internal->raw_packet_buffer_remaining_size += pkt->size;
av_free(pktl);
return 0;
}
}
pkt->data = NULL;
pkt->size = 0;
av_init_packet(pkt);
ret = s->iformat->read_packet(s, pkt);
if (ret < 0) {
if (!pktl || ret == AVERROR(EAGAIN))
return ret;
for (i = 0; i < s->nb_streams; i++) {
st = s->streams[i];
if (st->probe_packets)
if ((err = probe_codec(s, st, NULL)) < 0)
return err;
}
continue;
}
if (!pkt->buf) {
AVPacket tmp = { 0 };
ret = av_packet_ref(&tmp, pkt);
if (ret < 0)
return ret;
*pkt = tmp;
}
if ((s->flags & AVFMT_FLAG_DISCARD_CORRUPT) &&
(pkt->flags & AV_PKT_FLAG_CORRUPT)) {
av_log(s, AV_LOG_WARNING,
"Dropped corrupted packet (stream = %d)\n",
pkt->stream_index);
av_packet_unref(pkt);
continue;
}
st = s->streams[pkt->stream_index];
switch (st->codecpar->codec_type) {
case AVMEDIA_TYPE_VIDEO:
if (s->video_codec_id)
st->codecpar->codec_id = s->video_codec_id;
break;
case AVMEDIA_TYPE_AUDIO:
if (s->audio_codec_id)
st->codecpar->codec_id = s->audio_codec_id;
break;
case AVMEDIA_TYPE_SUBTITLE:
if (s->subtitle_codec_id)
st->codecpar->codec_id = s->subtitle_codec_id;
break;
}
if (!pktl && (st->codecpar->codec_id != AV_CODEC_ID_PROBE ||
!st->probe_packets))
return ret;
err = add_to_pktbuf(&s->internal->raw_packet_buffer, pkt,
&s->internal->raw_packet_buffer_end, 0);
if (err)
return err;
s->internal->raw_packet_buffer_remaining_size -= pkt->size;
if ((err = probe_codec(s, st, pkt)) < 0)
return err;
}
} | ['int ff_read_packet(AVFormatContext *s, AVPacket *pkt)\n{\n int ret, i, err;\n AVStream *st;\n for (;;) {\n AVPacketList *pktl = s->internal->raw_packet_buffer;\n if (pktl) {\n *pkt = pktl->pkt;\n st = s->streams[pkt->stream_index];\n if (st->codecpar->codec_id != AV_CODEC_ID_PROBE ||\n !st->probe_packets ||\n s->internal->raw_packet_buffer_remaining_size < pkt->size) {\n AVProbeData *pd;\n if (st->probe_packets)\n if ((err = probe_codec(s, st, NULL)) < 0)\n return err;\n pd = &st->probe_data;\n av_freep(&pd->buf);\n pd->buf_size = 0;\n s->internal->raw_packet_buffer = pktl->next;\n s->internal->raw_packet_buffer_remaining_size += pkt->size;\n av_free(pktl);\n return 0;\n }\n }\n pkt->data = NULL;\n pkt->size = 0;\n av_init_packet(pkt);\n ret = s->iformat->read_packet(s, pkt);\n if (ret < 0) {\n if (!pktl || ret == AVERROR(EAGAIN))\n return ret;\n for (i = 0; i < s->nb_streams; i++) {\n st = s->streams[i];\n if (st->probe_packets)\n if ((err = probe_codec(s, st, NULL)) < 0)\n return err;\n }\n continue;\n }\n if (!pkt->buf) {\n AVPacket tmp = { 0 };\n ret = av_packet_ref(&tmp, pkt);\n if (ret < 0)\n return ret;\n *pkt = tmp;\n }\n if ((s->flags & AVFMT_FLAG_DISCARD_CORRUPT) &&\n (pkt->flags & AV_PKT_FLAG_CORRUPT)) {\n av_log(s, AV_LOG_WARNING,\n "Dropped corrupted packet (stream = %d)\\n",\n pkt->stream_index);\n av_packet_unref(pkt);\n continue;\n }\n st = s->streams[pkt->stream_index];\n switch (st->codecpar->codec_type) {\n case AVMEDIA_TYPE_VIDEO:\n if (s->video_codec_id)\n st->codecpar->codec_id = s->video_codec_id;\n break;\n case AVMEDIA_TYPE_AUDIO:\n if (s->audio_codec_id)\n st->codecpar->codec_id = s->audio_codec_id;\n break;\n case AVMEDIA_TYPE_SUBTITLE:\n if (s->subtitle_codec_id)\n st->codecpar->codec_id = s->subtitle_codec_id;\n break;\n }\n if (!pktl && (st->codecpar->codec_id != AV_CODEC_ID_PROBE ||\n !st->probe_packets))\n return ret;\n err = add_to_pktbuf(&s->internal->raw_packet_buffer, pkt,\n &s->internal->raw_packet_buffer_end, 0);\n if (err)\n return err;\n s->internal->raw_packet_buffer_remaining_size -= pkt->size;\n if ((err = probe_codec(s, st, pkt)) < 0)\n return err;\n }\n}', 'void av_init_packet(AVPacket *pkt)\n{\n pkt->pts = AV_NOPTS_VALUE;\n pkt->dts = AV_NOPTS_VALUE;\n pkt->pos = -1;\n pkt->duration = 0;\n#if FF_API_CONVERGENCE_DURATION\nFF_DISABLE_DEPRECATION_WARNINGS\n pkt->convergence_duration = 0;\nFF_ENABLE_DEPRECATION_WARNINGS\n#endif\n pkt->flags = 0;\n pkt->stream_index = 0;\n pkt->buf = NULL;\n pkt->side_data = NULL;\n pkt->side_data_elems = 0;\n}', 'int av_packet_ref(AVPacket *dst, AVPacket *src)\n{\n int ret;\n ret = av_packet_copy_props(dst, src);\n if (ret < 0)\n return ret;\n if (!src->buf) {\n ret = packet_alloc(&dst->buf, src->size);\n if (ret < 0)\n goto fail;\n memcpy(dst->buf->data, src->data, src->size);\n dst->data = dst->buf->data;\n } else {\n dst->buf = av_buffer_ref(src->buf);\n if (!dst->buf) {\n ret = AVERROR(ENOMEM);\n goto fail;\n }\n dst->data = src->data;\n }\n dst->size = src->size;\n return 0;\nfail:\n av_packet_free_side_data(dst);\n return ret;\n}', 'int av_packet_copy_props(AVPacket *dst, const AVPacket *src)\n{\n int i;\n dst->pts = src->pts;\n dst->dts = src->dts;\n dst->pos = src->pos;\n dst->duration = src->duration;\n#if FF_API_CONVERGENCE_DURATION\nFF_DISABLE_DEPRECATION_WARNINGS\n dst->convergence_duration = src->convergence_duration;\nFF_ENABLE_DEPRECATION_WARNINGS\n#endif\n dst->flags = src->flags;\n dst->stream_index = src->stream_index;\n for (i = 0; i < src->side_data_elems; i++) {\n enum AVPacketSideDataType type = src->side_data[i].type;\n int size = src->side_data[i].size;\n uint8_t *src_data = src->side_data[i].data;\n uint8_t *dst_data = av_packet_new_side_data(dst, type, size);\n if (!dst_data) {\n av_packet_free_side_data(dst);\n return AVERROR(ENOMEM);\n }\n memcpy(dst_data, src_data, size);\n }\n return 0;\n}', 'static int packet_alloc(AVBufferRef **buf, int size)\n{\n int ret;\n if (size < 0 || size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)\n return AVERROR(EINVAL);\n ret = av_buffer_realloc(buf, size + AV_INPUT_BUFFER_PADDING_SIZE);\n if (ret < 0)\n return ret;\n memset((*buf)->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);\n return 0;\n}', 'int av_buffer_realloc(AVBufferRef **pbuf, int size)\n{\n AVBufferRef *buf = *pbuf;\n uint8_t *tmp;\n if (!buf) {\n uint8_t *data = av_realloc(NULL, size);\n if (!data)\n return AVERROR(ENOMEM);\n buf = av_buffer_create(data, size, av_buffer_default_free, NULL, 0);\n if (!buf) {\n av_freep(&data);\n return AVERROR(ENOMEM);\n }\n buf->buffer->flags |= BUFFER_FLAG_REALLOCATABLE;\n *pbuf = buf;\n return 0;\n } else if (buf->size == size)\n return 0;\n if (!(buf->buffer->flags & BUFFER_FLAG_REALLOCATABLE) ||\n !av_buffer_is_writable(buf) || buf->data != buf->buffer->data) {\n AVBufferRef *new = NULL;\n av_buffer_realloc(&new, size);\n if (!new)\n return AVERROR(ENOMEM);\n memcpy(new->data, buf->data, FFMIN(size, buf->size));\n av_buffer_unref(pbuf);\n *pbuf = new;\n return 0;\n }\n tmp = av_realloc(buf->buffer->data, size);\n if (!tmp)\n return AVERROR(ENOMEM);\n buf->buffer->data = buf->data = tmp;\n buf->buffer->size = buf->size = size;\n return 0;\n}', 'int av_buffer_is_writable(const AVBufferRef *buf)\n{\n if (buf->buffer->flags & AV_BUFFER_FLAG_READONLY)\n return 0;\n return atomic_load(&buf->buffer->refcount) == 1;\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}'] |
554 | 0 | https://github.com/openssl/openssl/blob/ed371b8cbac0d0349667558c061c1ae380cf75eb/crypto/bn/bn_lib.c/#L232 | static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
BN_ULONG *a = NULL;
if (words > (INT_MAX / (4 * BN_BITS2))) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
return NULL;
}
if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
return NULL;
}
if (BN_get_flags(b, BN_FLG_SECURE))
a = OPENSSL_secure_zalloc(words * sizeof(*a));
else
a = OPENSSL_zalloc(words * sizeof(*a));
if (a == NULL) {
BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
return NULL;
}
assert(b->top <= words);
if (b->top > 0)
memcpy(a, b->d, sizeof(*a) * b->top);
return a;
} | ['BIGNUM *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}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n a->flags &= ~BN_FLG_FIXED_TOP;\n bn_check_top(a);\n return 1;\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'int BN_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}', '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}'] |
555 | 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_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,\n 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;\n int ret = 0;\n if (EC_POINT_is_at_infinity(group, a)) {\n BN_zero(r->Z);\n r->Z_is_one = 0;\n return 1;\n }\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 if (n3 == NULL)\n goto err;\n if (a->Z_is_one) {\n if (!field_sqr(group, n0, a->X, ctx))\n goto err;\n if (!BN_mod_lshift1_quick(n1, n0, p))\n goto err;\n if (!BN_mod_add_quick(n0, n0, n1, p))\n goto err;\n if (!BN_mod_add_quick(n1, n0, group->a, p))\n goto err;\n } else if (group->a_is_minus3) {\n if (!field_sqr(group, n1, a->Z, ctx))\n goto err;\n if (!BN_mod_add_quick(n0, a->X, n1, p))\n goto err;\n if (!BN_mod_sub_quick(n2, a->X, n1, p))\n goto err;\n if (!field_mul(group, n1, n0, n2, ctx))\n goto err;\n if (!BN_mod_lshift1_quick(n0, n1, p))\n goto err;\n if (!BN_mod_add_quick(n1, n0, n1, p))\n goto err;\n } else {\n if (!field_sqr(group, n0, a->X, ctx))\n goto err;\n if (!BN_mod_lshift1_quick(n1, n0, p))\n goto err;\n if (!BN_mod_add_quick(n0, n0, n1, p))\n goto err;\n if (!field_sqr(group, n1, a->Z, ctx))\n goto err;\n if (!field_sqr(group, n1, n1, ctx))\n goto err;\n if (!field_mul(group, n1, n1, group->a, ctx))\n goto err;\n if (!BN_mod_add_quick(n1, n1, n0, p))\n goto err;\n }\n if (a->Z_is_one) {\n if (!BN_copy(n0, a->Y))\n goto err;\n } else {\n if (!field_mul(group, n0, a->Y, a->Z, ctx))\n goto err;\n }\n if (!BN_mod_lshift1_quick(r->Z, n0, p))\n goto err;\n r->Z_is_one = 0;\n if (!field_sqr(group, n3, a->Y, ctx))\n goto err;\n if (!field_mul(group, n2, a->X, n3, ctx))\n goto err;\n if (!BN_mod_lshift_quick(n2, n2, 2, p))\n goto err;\n if (!BN_mod_lshift1_quick(n0, n2, p))\n goto err;\n if (!field_sqr(group, r->X, n1, ctx))\n goto err;\n if (!BN_mod_sub_quick(r->X, r->X, n0, p))\n goto err;\n if (!field_sqr(group, n0, n3, ctx))\n goto err;\n if (!BN_mod_lshift_quick(n3, n0, 3, p))\n goto err;\n if (!BN_mod_sub_quick(n0, n2, r->X, p))\n goto err;\n if (!field_mul(group, n0, n1, n0, ctx))\n goto err;\n if (!BN_mod_sub_quick(r->Y, n0, n3, p))\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_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n const BIGNUM *m)\n{\n if (!BN_uadd(r, a, b))\n return 0;\n if (BN_ucmp(r, m) >= 0)\n return BN_usub(r, r, m);\n return 1;\n}', 'int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int max, min, dif;\n const BN_ULONG *ap, *bp;\n BN_ULONG *rp, carry, t1, t2;\n const BIGNUM *tmp;\n bn_check_top(a);\n bn_check_top(b);\n if (a->top < b->top) {\n tmp = a;\n a = b;\n b = tmp;\n }\n max = a->top;\n min = b->top;\n dif = max - min;\n if (bn_wexpand(r, max + 1) == NULL)\n return 0;\n r->top = max;\n ap = a->d;\n bp = b->d;\n rp = r->d;\n carry = bn_add_words(rp, ap, bp, min);\n rp += min;\n ap += min;\n bp += min;\n if (carry) {\n while (dif) {\n dif--;\n t1 = *(ap++);\n t2 = (t1 + 1) & BN_MASK2;\n *(rp++) = t2;\n if (t2) {\n carry = 0;\n break;\n }\n }\n if (carry) {\n *rp = 1;\n r->top++;\n }\n }\n if (dif && rp != ap)\n while (dif--)\n *(rp++) = *(ap++);\n r->neg = 0;\n bn_check_top(r);\n return 1;\n}', '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}'] |
556 | 0 | https://github.com/openssl/openssl/blob/681acb311bb7c68c9310d2e96bf2cf0e35443a22/crypto/x509/x509_vfy.c/#L1206 | static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl,
X509 **pissuer, int *pcrl_score)
{
X509 *crl_issuer = NULL;
X509_NAME *cnm = X509_CRL_get_issuer(crl);
int cidx = ctx->error_depth;
int i;
if (cidx != sk_X509_num(ctx->chain) - 1)
cidx++;
crl_issuer = sk_X509_value(ctx->chain, cidx);
if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
if (*pcrl_score & CRL_SCORE_ISSUER_NAME) {
*pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_ISSUER_CERT;
*pissuer = crl_issuer;
return;
}
}
for (cidx++; cidx < sk_X509_num(ctx->chain); cidx++) {
crl_issuer = sk_X509_value(ctx->chain, cidx);
if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
continue;
if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
*pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_SAME_PATH;
*pissuer = crl_issuer;
return;
}
}
if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT))
return;
for (i = 0; i < sk_X509_num(ctx->untrusted); i++) {
crl_issuer = sk_X509_value(ctx->untrusted, i);
if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
continue;
if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
*pissuer = crl_issuer;
*pcrl_score |= CRL_SCORE_AKID;
return;
}
}
} | ['static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl,\n X509 **pissuer, int *pcrl_score)\n{\n X509 *crl_issuer = NULL;\n X509_NAME *cnm = X509_CRL_get_issuer(crl);\n int cidx = ctx->error_depth;\n int i;\n if (cidx != sk_X509_num(ctx->chain) - 1)\n cidx++;\n crl_issuer = sk_X509_value(ctx->chain, cidx);\n if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {\n if (*pcrl_score & CRL_SCORE_ISSUER_NAME) {\n *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_ISSUER_CERT;\n *pissuer = crl_issuer;\n return;\n }\n }\n for (cidx++; cidx < sk_X509_num(ctx->chain); cidx++) {\n crl_issuer = sk_X509_value(ctx->chain, cidx);\n if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))\n continue;\n if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {\n *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_SAME_PATH;\n *pissuer = crl_issuer;\n return;\n }\n }\n if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT))\n return;\n for (i = 0; i < sk_X509_num(ctx->untrusted); i++) {\n crl_issuer = sk_X509_value(ctx->untrusted, i);\n if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))\n continue;\n if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {\n *pissuer = crl_issuer;\n *pcrl_score |= CRL_SCORE_AKID;\n return;\n }\n }\n}', 'X509_NAME *X509_CRL_get_issuer(const X509_CRL *crl)\n{\n return crl->crl.issuer;\n}', 'DEFINE_STACK_OF(X509)', 'int OPENSSL_sk_num(const OPENSSL_STACK *st)\n{\n if (st == NULL)\n return -1;\n return st->num;\n}', 'void *OPENSSL_sk_value(const OPENSSL_STACK *st, int i)\n{\n if (st == NULL || i < 0 || i >= st->num)\n return NULL;\n return (void *)st->data[i];\n}', 'int X509_check_akid(X509 *issuer, AUTHORITY_KEYID *akid)\n{\n if (!akid)\n return X509_V_OK;\n if (akid->keyid && issuer->skid &&\n ASN1_OCTET_STRING_cmp(akid->keyid, issuer->skid))\n return X509_V_ERR_AKID_SKID_MISMATCH;\n if (akid->serial &&\n ASN1_INTEGER_cmp(X509_get_serialNumber(issuer), akid->serial))\n return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;\n if (akid->issuer) {\n GENERAL_NAMES *gens;\n GENERAL_NAME *gen;\n X509_NAME *nm = NULL;\n int i;\n gens = akid->issuer;\n for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {\n gen = sk_GENERAL_NAME_value(gens, i);\n if (gen->type == GEN_DIRNAME) {\n nm = gen->d.dirn;\n break;\n }\n }\n if (nm && X509_NAME_cmp(nm, X509_get_issuer_name(issuer)))\n return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;\n }\n return X509_V_OK;\n}', 'X509_NAME *X509_get_subject_name(const X509 *a)\n{\n return (a->cert_info.subject);\n}'] |
557 | 0 | https://github.com/openssl/openssl/blob/33af4421f2ae5e4d0da3a121f51820f4b49a724c/apps/apps.c/#L2213 | int args_verify(char ***pargs, int *pargc,
int *badarg, BIO *err, X509_VERIFY_PARAM **pm)
{
ASN1_OBJECT *otmp = NULL;
unsigned long flags = 0;
int i;
int purpose = 0;
char **oldargs = *pargs;
char *arg = **pargs, *argn = (*pargs)[1];
if (!strcmp(arg, "-policy"))
{
if (!argn)
*badarg = 1;
else
{
otmp = OBJ_txt2obj(argn, 0);
if (!otmp)
{
BIO_printf(err, "Invalid Policy \"%s\"\n",
argn);
*badarg = 1;
}
}
(*pargs)++;
}
else if (strcmp(arg,"-purpose") == 0)
{
X509_PURPOSE *xptmp;
if (!argn)
*badarg = 1;
else
{
i = X509_PURPOSE_get_by_sname(argn);
if(i < 0)
{
BIO_printf(err, "unrecognized purpose\n");
*badarg = 1;
}
else
{
xptmp = X509_PURPOSE_get0(i);
purpose = X509_PURPOSE_get_id(xptmp);
}
}
(*pargs)++;
}
else if (!strcmp(arg, "-ignore_critical"))
flags |= X509_V_FLAG_IGNORE_CRITICAL;
else if (!strcmp(arg, "-issuer_checks"))
flags |= X509_V_FLAG_CB_ISSUER_CHECK;
else if (!strcmp(arg, "-crl_check"))
flags |= X509_V_FLAG_CRL_CHECK;
else if (!strcmp(arg, "-crl_check_all"))
flags |= X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL;
else if (!strcmp(arg, "-policy_check"))
flags |= X509_V_FLAG_POLICY_CHECK;
else if (!strcmp(arg, "-explicit_policy"))
flags |= X509_V_FLAG_EXPLICIT_POLICY;
else if (!strcmp(arg, "-x509_strict"))
flags |= X509_V_FLAG_X509_STRICT;
else if (!strcmp(arg, "-policy_print"))
flags |= X509_V_FLAG_NOTIFY_POLICY;
else
return 0;
if (*badarg)
{
if (*pm)
X509_VERIFY_PARAM_free(*pm);
*pm = NULL;
goto end;
}
if (!*pm && !(*pm = X509_VERIFY_PARAM_new()))
{
*badarg = 1;
goto end;
}
if (otmp)
X509_VERIFY_PARAM_add0_policy(*pm, otmp);
if (flags)
X509_VERIFY_PARAM_set_flags(*pm, flags);
if (purpose)
X509_VERIFY_PARAM_set_purpose(*pm, purpose);
end:
(*pargs)++;
if (pargc)
*pargc -= *pargs - oldargs;
return 1;
} | ['int args_verify(char ***pargs, int *pargc,\n\t\t\tint *badarg, BIO *err, X509_VERIFY_PARAM **pm)\n\t{\n\tASN1_OBJECT *otmp = NULL;\n\tunsigned long flags = 0;\n\tint i;\n\tint purpose = 0;\n\tchar **oldargs = *pargs;\n\tchar *arg = **pargs, *argn = (*pargs)[1];\n\tif (!strcmp(arg, "-policy"))\n\t\t{\n\t\tif (!argn)\n\t\t\t*badarg = 1;\n\t\telse\n\t\t\t{\n\t\t\totmp = OBJ_txt2obj(argn, 0);\n\t\t\tif (!otmp)\n\t\t\t\t{\n\t\t\t\tBIO_printf(err, "Invalid Policy \\"%s\\"\\n",\n\t\t\t\t\t\t\t\t\targn);\n\t\t\t\t*badarg = 1;\n\t\t\t\t}\n\t\t\t}\n\t\t(*pargs)++;\n\t\t}\n\telse if (strcmp(arg,"-purpose") == 0)\n\t\t{\n\t\tX509_PURPOSE *xptmp;\n\t\tif (!argn)\n\t\t\t*badarg = 1;\n\t\telse\n\t\t\t{\n\t\t\ti = X509_PURPOSE_get_by_sname(argn);\n\t\t\tif(i < 0)\n\t\t\t\t{\n\t\t\t\tBIO_printf(err, "unrecognized purpose\\n");\n\t\t\t\t*badarg = 1;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\txptmp = X509_PURPOSE_get0(i);\n\t\t\t\tpurpose = X509_PURPOSE_get_id(xptmp);\n\t\t\t\t}\n\t\t\t}\n\t\t(*pargs)++;\n\t\t}\n\telse if (!strcmp(arg, "-ignore_critical"))\n\t\tflags |= X509_V_FLAG_IGNORE_CRITICAL;\n\telse if (!strcmp(arg, "-issuer_checks"))\n\t\tflags |= X509_V_FLAG_CB_ISSUER_CHECK;\n\telse if (!strcmp(arg, "-crl_check"))\n\t\tflags |= X509_V_FLAG_CRL_CHECK;\n\telse if (!strcmp(arg, "-crl_check_all"))\n\t\tflags |= X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL;\n\telse if (!strcmp(arg, "-policy_check"))\n\t\tflags |= X509_V_FLAG_POLICY_CHECK;\n\telse if (!strcmp(arg, "-explicit_policy"))\n\t\tflags |= X509_V_FLAG_EXPLICIT_POLICY;\n\telse if (!strcmp(arg, "-x509_strict"))\n\t\tflags |= X509_V_FLAG_X509_STRICT;\n\telse if (!strcmp(arg, "-policy_print"))\n\t\tflags |= X509_V_FLAG_NOTIFY_POLICY;\n\telse\n\t\treturn 0;\n\tif (*badarg)\n\t\t{\n\t\tif (*pm)\n\t\t\tX509_VERIFY_PARAM_free(*pm);\n\t\t*pm = NULL;\n\t\tgoto end;\n\t\t}\n\tif (!*pm && !(*pm = X509_VERIFY_PARAM_new()))\n\t\t{\n\t\t*badarg = 1;\n\t\tgoto end;\n\t\t}\n\tif (otmp)\n\t\tX509_VERIFY_PARAM_add0_policy(*pm, otmp);\n\tif (flags)\n\t\tX509_VERIFY_PARAM_set_flags(*pm, flags);\n\tif (purpose)\n\t\tX509_VERIFY_PARAM_set_purpose(*pm, purpose);\n\tend:\n\t(*pargs)++;\n\tif (pargc)\n\t\t*pargc -= *pargs - oldargs;\n\treturn 1;\n\t}', 'X509_PURPOSE * X509_PURPOSE_get0(int idx)\n{\n\tif(idx < 0) return NULL;\n\tif(idx < (int)X509_PURPOSE_COUNT) return xstandard + idx;\n\treturn sk_X509_PURPOSE_value(xptable, idx - X509_PURPOSE_COUNT);\n}', 'char *sk_value(const STACK *st, int i)\n{\n\tif(!st || (i < 0) || (i >= st->num)) return NULL;\n\treturn st->data[i];\n}', 'int X509_PURPOSE_get_id(X509_PURPOSE *xp)\n{\n\treturn xp->purpose;\n}'] |
558 | 0 | https://github.com/openssl/openssl/blob/9c46f4b9cd4912b61cb546c48b678488d7f26ed6/crypto/bn/bn_ctx.c/#L353 | static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
} | ['int dsa_builtin_paramgen2(DSA *ret, size_t L, size_t N,\n const EVP_MD *evpmd, const unsigned char *seed_in,\n size_t seed_len, int idx, unsigned char *seed_out,\n int *counter_ret, unsigned long *h_ret,\n BN_GENCB *cb)\n{\n int ok = -1;\n unsigned char *seed = NULL, *seed_tmp = NULL;\n unsigned char md[EVP_MAX_MD_SIZE];\n int mdsize;\n BIGNUM *r0, *W, *X, *c, *test;\n BIGNUM *g = NULL, *q = NULL, *p = NULL;\n BN_MONT_CTX *mont = NULL;\n int i, k, n = 0, m = 0, qsize = N >> 3;\n int counter = 0;\n int r = 0;\n BN_CTX *ctx = NULL;\n EVP_MD_CTX mctx;\n unsigned int h = 2;\n EVP_MD_CTX_init(&mctx);\n if (evpmd == NULL) {\n if (N == 160)\n evpmd = EVP_sha1();\n else if (N == 224)\n evpmd = EVP_sha224();\n else\n evpmd = EVP_sha256();\n }\n mdsize = M_EVP_MD_size(evpmd);\n if (!ret->p || !ret->q || idx >= 0) {\n if (seed_len == 0)\n seed_len = mdsize;\n seed = OPENSSL_malloc(seed_len);\n if (seed_out)\n seed_tmp = seed_out;\n else\n seed_tmp = OPENSSL_malloc(seed_len);\n if (!seed || !seed_tmp)\n goto err;\n if (seed_in)\n memcpy(seed, seed_in, seed_len);\n }\n if ((ctx = BN_CTX_new()) == NULL)\n goto err;\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n BN_CTX_start(ctx);\n r0 = BN_CTX_get(ctx);\n g = BN_CTX_get(ctx);\n W = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n c = BN_CTX_get(ctx);\n test = BN_CTX_get(ctx);\n if (ret->p && ret->q) {\n p = ret->p;\n q = ret->q;\n if (idx >= 0)\n memcpy(seed_tmp, seed, seed_len);\n goto g_only;\n } else {\n p = BN_CTX_get(ctx);\n q = BN_CTX_get(ctx);\n }\n if (!BN_lshift(test, BN_value_one(), L - 1))\n goto err;\n for (;;) {\n for (;;) {\n unsigned char *pmd;\n if (!BN_GENCB_call(cb, 0, m++))\n goto err;\n if (!seed_in) {\n if (RAND_pseudo_bytes(seed, seed_len) < 0)\n goto err;\n }\n if (!EVP_Digest(seed, seed_len, md, NULL, evpmd, NULL))\n goto err;\n if (mdsize > qsize)\n pmd = md + mdsize - qsize;\n else\n pmd = md;\n if (mdsize < qsize)\n memset(md + mdsize, 0, qsize - mdsize);\n pmd[0] |= 0x80;\n pmd[qsize - 1] |= 0x01;\n if (!BN_bin2bn(pmd, qsize, q))\n goto err;\n r = BN_is_prime_fasttest_ex(q, DSS_prime_checks, ctx,\n seed_in ? 1 : 0, cb);\n if (r > 0)\n break;\n if (r != 0)\n goto err;\n if (seed_in) {\n ok = 0;\n DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN2, DSA_R_Q_NOT_PRIME);\n goto err;\n }\n }\n if (seed_out)\n memcpy(seed_out, seed, seed_len);\n if (!BN_GENCB_call(cb, 2, 0))\n goto err;\n if (!BN_GENCB_call(cb, 3, 0))\n goto err;\n counter = 0;\n n = (L - 1) / (mdsize << 3);\n for (;;) {\n if ((counter != 0) && !BN_GENCB_call(cb, 0, counter))\n goto err;\n BN_zero(W);\n for (k = 0; k <= n; k++) {\n for (i = seed_len - 1; i >= 0; i--) {\n seed[i]++;\n if (seed[i] != 0)\n break;\n }\n if (!EVP_Digest(seed, seed_len, md, NULL, evpmd, NULL))\n goto err;\n if (!BN_bin2bn(md, mdsize, r0))\n goto err;\n if (!BN_lshift(r0, r0, (mdsize << 3) * k))\n goto err;\n if (!BN_add(W, W, r0))\n goto err;\n }\n if (!BN_mask_bits(W, L - 1))\n goto err;\n if (!BN_copy(X, W))\n goto err;\n if (!BN_add(X, X, test))\n goto err;\n if (!BN_lshift1(r0, q))\n goto err;\n if (!BN_mod(c, X, r0, ctx))\n goto err;\n if (!BN_sub(r0, c, BN_value_one()))\n goto err;\n if (!BN_sub(p, X, r0))\n goto err;\n if (BN_cmp(p, test) >= 0) {\n r = BN_is_prime_fasttest_ex(p, DSS_prime_checks, ctx, 1, cb);\n if (r > 0)\n goto end;\n if (r != 0)\n goto err;\n }\n counter++;\n if (counter >= (int)(4 * L))\n break;\n }\n if (seed_in) {\n ok = 0;\n DSAerr(DSA_F_DSA_BUILTIN_PARAMGEN2, DSA_R_INVALID_PARAMETERS);\n goto err;\n }\n }\n end:\n if (!BN_GENCB_call(cb, 2, 1))\n goto err;\n g_only:\n if (!BN_sub(test, p, BN_value_one()))\n goto err;\n if (!BN_div(r0, NULL, test, q, ctx))\n goto err;\n if (idx < 0) {\n if (!BN_set_word(test, h))\n goto err;\n } else\n h = 1;\n if (!BN_MONT_CTX_set(mont, p, ctx))\n goto err;\n for (;;) {\n static const unsigned char ggen[4] = { 0x67, 0x67, 0x65, 0x6e };\n if (idx >= 0) {\n md[0] = idx & 0xff;\n md[1] = (h >> 8) & 0xff;\n md[2] = h & 0xff;\n if (!EVP_DigestInit_ex(&mctx, evpmd, NULL))\n goto err;\n if (!EVP_DigestUpdate(&mctx, seed_tmp, seed_len))\n goto err;\n if (!EVP_DigestUpdate(&mctx, ggen, sizeof(ggen)))\n goto err;\n if (!EVP_DigestUpdate(&mctx, md, 3))\n goto err;\n if (!EVP_DigestFinal_ex(&mctx, md, NULL))\n goto err;\n if (!BN_bin2bn(md, mdsize, test))\n goto err;\n }\n if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont))\n goto err;\n if (!BN_is_one(g))\n break;\n if (idx < 0 && !BN_add(test, test, BN_value_one()))\n goto err;\n h++;\n if (idx >= 0 && h > 0xffff)\n goto err;\n }\n if (!BN_GENCB_call(cb, 3, 1))\n goto err;\n ok = 1;\n err:\n if (ok == 1) {\n if (p != ret->p) {\n if (ret->p)\n BN_free(ret->p);\n ret->p = BN_dup(p);\n }\n if (q != ret->q) {\n if (ret->q)\n BN_free(ret->q);\n ret->q = BN_dup(q);\n }\n if (ret->g)\n BN_free(ret->g);\n ret->g = BN_dup(g);\n if (ret->p == NULL || ret->q == NULL || ret->g == NULL) {\n ok = -1;\n goto err;\n }\n if (counter_ret != NULL)\n *counter_ret = counter;\n if (h_ret != NULL)\n *h_ret = h;\n }\n if (seed)\n OPENSSL_free(seed);\n if (seed_out != seed_tmp)\n OPENSSL_free(seed_tmp);\n if (ctx) {\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n }\n if (mont != NULL)\n BN_MONT_CTX_free(mont);\n EVP_MD_CTX_cleanup(&mctx);\n return ok;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_start", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG_EXIT(ctx);\n}', 'int BN_is_prime_fasttest_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,\n int do_trial_division, BN_GENCB *cb)\n{\n int i, j, ret = -1;\n int k;\n BN_CTX *ctx = NULL;\n BIGNUM *A1, *A1_odd, *check;\n BN_MONT_CTX *mont = NULL;\n const BIGNUM *A = NULL;\n if (BN_cmp(a, BN_value_one()) <= 0)\n return 0;\n if (checks == BN_prime_checks)\n checks = BN_prime_checks_for_size(BN_num_bits(a));\n if (!BN_is_odd(a))\n return BN_is_word(a, 2);\n if (do_trial_division) {\n for (i = 1; i < NUMPRIMES; i++)\n if (BN_mod_word(a, primes[i]) == 0)\n return 0;\n if (!BN_GENCB_call(cb, 1, -1))\n goto err;\n }\n if (ctx_passed != NULL)\n ctx = ctx_passed;\n else if ((ctx = BN_CTX_new()) == NULL)\n goto err;\n BN_CTX_start(ctx);\n if (a->neg) {\n BIGNUM *t;\n if ((t = BN_CTX_get(ctx)) == NULL)\n goto err;\n BN_copy(t, a);\n t->neg = 0;\n A = t;\n } else\n A = a;\n A1 = BN_CTX_get(ctx);\n A1_odd = BN_CTX_get(ctx);\n check = BN_CTX_get(ctx);\n if (check == NULL)\n goto err;\n if (!BN_copy(A1, A))\n goto err;\n if (!BN_sub_word(A1, 1))\n goto err;\n if (BN_is_zero(A1)) {\n ret = 0;\n goto err;\n }\n k = 1;\n while (!BN_is_bit_set(A1, k))\n k++;\n if (!BN_rshift(A1_odd, A1, k))\n goto err;\n mont = BN_MONT_CTX_new();\n if (mont == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, A, ctx))\n goto err;\n for (i = 0; i < checks; i++) {\n if (!BN_pseudo_rand_range(check, A1))\n goto err;\n if (!BN_add_word(check, 1))\n goto err;\n j = witness(check, A, A1, A1_odd, k, ctx, mont);\n if (j == -1)\n goto err;\n if (j) {\n ret = 0;\n goto err;\n }\n if (!BN_GENCB_call(cb, 1, i))\n goto err;\n }\n ret = 1;\n err:\n if (ctx != NULL) {\n BN_CTX_end(ctx);\n if (ctx_passed == NULL)\n BN_CTX_free(ctx);\n }\n if (mont != NULL)\n BN_MONT_CTX_free(mont);\n return (ret);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return (0);\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return (0);\n }\n if (dv != NULL)\n BN_zero(dv);\n return (1);\n }\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (dv == NULL)\n res = BN_CTX_get(ctx);\n else\n res = dv;\n if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n 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}'] |
559 | 1 | https://github.com/openssl/openssl/blob/3da2e9c4ee45989a426ff513dc6c6250d1e460de/crypto/bn/bn_shift.c/#L112 | int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)
{
int i, nw, lb, rb;
BN_ULONG *t, *f;
BN_ULONG l;
bn_check_top(r);
bn_check_top(a);
if (n < 0) {
BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);
return 0;
}
nw = n / BN_BITS2;
if (bn_wexpand(r, a->top + nw + 1) == NULL)
return 0;
r->neg = a->neg;
lb = n % BN_BITS2;
rb = BN_BITS2 - lb;
f = a->d;
t = r->d;
t[a->top + nw] = 0;
if (lb == 0)
for (i = a->top - 1; i >= 0; i--)
t[nw + i] = f[i];
else
for (i = a->top - 1; i >= 0; i--) {
l = f[i];
t[nw + i + 1] |= (l >> rb) & BN_MASK2;
t[nw + i] = (l << lb) & BN_MASK2;
}
memset(t, 0, sizeof(*t) * nw);
r->top = a->top + nw + 1;
bn_correct_top(r);
bn_check_top(r);
return 1;
} | ['int 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 }\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}', 'int BN_gcd(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx)\n{\n BIGNUM *a, *b, *t;\n int ret = 0;\n bn_check_top(in_a);\n bn_check_top(in_b);\n BN_CTX_start(ctx);\n a = BN_CTX_get(ctx);\n b = BN_CTX_get(ctx);\n if (b == NULL)\n goto err;\n if (BN_copy(a, in_a) == NULL)\n goto err;\n if (BN_copy(b, in_b) == NULL)\n goto err;\n a->neg = 0;\n b->neg = 0;\n if (BN_cmp(a, b) < 0) {\n t = a;\n a = b;\n b = t;\n }\n t = euclid(a, b);\n if (t == NULL)\n goto err;\n if (BN_copy(r, t) == NULL)\n goto err;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n bn_check_top(r);\n return ret;\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, j, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return 0;\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return 0;\n }\n if (dv != NULL)\n BN_zero(dv);\n return 1;\n }\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.flags = BN_FLG_STATIC_DATA;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n resp++;\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W)\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n l0 = bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1);\n q -= l0;\n for (l0 = 0 - l0, j = 0; j < div_n; j++)\n tmp->d[j] = sdiv->d[j] & l0;\n l0 = bn_add_words(wnum.d, wnum.d, tmp->d, div_n);\n (*wnump) += l0;\n resp--;\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return 0;\n}', 'int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l;\n bn_check_top(r);\n bn_check_top(a);\n if (n < 0) {\n BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n nw = n / BN_BITS2;\n if (bn_wexpand(r, a->top + nw + 1) == NULL)\n return 0;\n r->neg = a->neg;\n lb = n % BN_BITS2;\n rb = BN_BITS2 - lb;\n f = a->d;\n t = r->d;\n t[a->top + nw] = 0;\n if (lb == 0)\n for (i = a->top - 1; i >= 0; i--)\n t[nw + i] = f[i];\n else\n for (i = a->top - 1; i >= 0; i--) {\n l = f[i];\n t[nw + i + 1] |= (l >> rb) & BN_MASK2;\n t[nw + i] = (l << lb) & BN_MASK2;\n }\n memset(t, 0, sizeof(*t) * nw);\n r->top = a->top + nw + 1;\n bn_correct_top(r);\n bn_check_top(r);\n return 1;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}'] |
560 | 0 | https://github.com/libav/libav/blob/67ce33162aa93bee1a5f9e8d6f00060329fa67da/ffmpeg.c/#L1013 | static void do_video_stats(AVFormatContext *os, AVOutputStream *ost,
int frame_size)
{
AVCodecContext *enc;
int frame_number;
double ti1, bitrate, avg_bitrate;
if (!vstats_file) {
vstats_file = fopen(vstats_filename, "w");
if (!vstats_file) {
perror("fopen");
av_exit(1);
}
}
enc = ost->st->codec;
if (enc->codec_type == CODEC_TYPE_VIDEO) {
frame_number = ost->frame_number;
fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number, enc->coded_frame->quality/(float)FF_QP2LAMBDA);
if (enc->flags&CODEC_FLAG_PSNR)
fprintf(vstats_file, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0]/(enc->width*enc->height*255.0*255.0)));
fprintf(vstats_file,"f_size= %6d ", frame_size);
ti1 = ost->sync_opts * av_q2d(enc->time_base);
if (ti1 < 0.01)
ti1 = 0.01;
bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;
avg_bitrate = (double)(video_size * 8) / ti1 / 1000.0;
fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
(double)video_size / 1024, ti1, bitrate, avg_bitrate);
fprintf(vstats_file,"type= %c\n", av_get_pict_type_char(enc->coded_frame->pict_type));
}
} | ['static void do_video_stats(AVFormatContext *os, AVOutputStream *ost,\n int frame_size)\n{\n AVCodecContext *enc;\n int frame_number;\n double ti1, bitrate, avg_bitrate;\n if (!vstats_file) {\n vstats_file = fopen(vstats_filename, "w");\n if (!vstats_file) {\n perror("fopen");\n av_exit(1);\n }\n }\n enc = ost->st->codec;\n if (enc->codec_type == CODEC_TYPE_VIDEO) {\n frame_number = ost->frame_number;\n fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number, enc->coded_frame->quality/(float)FF_QP2LAMBDA);\n if (enc->flags&CODEC_FLAG_PSNR)\n fprintf(vstats_file, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0]/(enc->width*enc->height*255.0*255.0)));\n fprintf(vstats_file,"f_size= %6d ", frame_size);\n ti1 = ost->sync_opts * av_q2d(enc->time_base);\n if (ti1 < 0.01)\n ti1 = 0.01;\n bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;\n avg_bitrate = (double)(video_size * 8) / ti1 / 1000.0;\n fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",\n (double)video_size / 1024, ti1, bitrate, avg_bitrate);\n fprintf(vstats_file,"type= %c\\n", av_get_pict_type_char(enc->coded_frame->pict_type));\n }\n}'] |
561 | 0 | https://github.com/openssl/openssl/blob/1a50eedf2a1fbb1e0e009ad616d8be678e4c6340/crypto/ec/ec_mult.c/#L494 | 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 internal_curve_test(int n)\n{\n EC_GROUP *group = NULL;\n int nid = curves[n].nid;\n if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))) {\n TEST_info("EC_GROUP_new_curve_name() failed with curve %s\\n",\n OBJ_nid2sn(nid));\n return 0;\n }\n if (!TEST_true(EC_GROUP_check(group, NULL))) {\n TEST_info("EC_GROUP_check() failed with curve %s\\n", OBJ_nid2sn(nid));\n EC_GROUP_free(group);\n return 0;\n }\n EC_GROUP_free(group);\n return 1;\n}', 'int EC_GROUP_check(const EC_GROUP *group, BN_CTX *ctx)\n{\n int ret = 0;\n const BIGNUM *order;\n BN_CTX *new_ctx = NULL;\n EC_POINT *point = NULL;\n if ((group->meth->flags & EC_FLAGS_CUSTOM_CURVE) != 0)\n return 1;\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL) {\n ECerr(EC_F_EC_GROUP_CHECK, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n }\n if (!EC_GROUP_check_discriminant(group, ctx)) {\n ECerr(EC_F_EC_GROUP_CHECK, EC_R_DISCRIMINANT_IS_ZERO);\n goto err;\n }\n if (group->generator == NULL) {\n ECerr(EC_F_EC_GROUP_CHECK, EC_R_UNDEFINED_GENERATOR);\n goto err;\n }\n if (EC_POINT_is_on_curve(group, group->generator, ctx) <= 0) {\n ECerr(EC_F_EC_GROUP_CHECK, EC_R_POINT_IS_NOT_ON_CURVE);\n goto err;\n }\n if ((point = EC_POINT_new(group)) == NULL)\n goto err;\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_GROUP_CHECK, EC_R_UNDEFINED_ORDER);\n goto err;\n }\n if (!EC_POINT_mul(group, point, order, NULL, NULL, ctx))\n goto err;\n if (!EC_POINT_is_at_infinity(group, point)) {\n ECerr(EC_F_EC_GROUP_CHECK, EC_R_INVALID_GROUP_ORDER);\n goto err;\n }\n ret = 1;\n err:\n BN_CTX_free(new_ctx);\n EC_POINT_free(point);\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}'] |
562 | 0 | https://github.com/openssl/openssl/blob/d40a1b865fddc3d67f8c06ff1f1466fad331c8f7/crypto/bn/bn_lib.c/#L250 | int BN_num_bits(const BIGNUM *a)
{
int i = a->top - 1;
bn_check_top(a);
if (BN_is_zero(a)) return 0;
return ((i*BN_BITS2) + BN_num_bits_word(a->d[i]));
} | ['int dtls1_send_server_key_exchange(SSL *s)\n\t{\n#ifndef OPENSSL_NO_RSA\n\tunsigned char *q;\n\tint j,num;\n\tRSA *rsa;\n\tunsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];\n\tunsigned int u;\n#endif\n#ifndef OPENSSL_NO_DH\n\tDH *dh=NULL,*dhp;\n#endif\n\tEVP_PKEY *pkey;\n\tunsigned char *p,*d;\n\tint al,i;\n\tunsigned long type;\n\tint n;\n\tCERT *cert;\n\tBIGNUM *r[4];\n\tint nr[4],kn;\n\tBUF_MEM *buf;\n\tEVP_MD_CTX md_ctx;\n\tEVP_MD_CTX_init(&md_ctx);\n\tif (s->state == SSL3_ST_SW_KEY_EXCH_A)\n\t\t{\n\t\ttype=s->s3->tmp.new_cipher->algorithm_mkey;\n\t\tcert=s->cert;\n\t\tbuf=s->init_buf;\n\t\tr[0]=r[1]=r[2]=r[3]=NULL;\n\t\tn=0;\n#ifndef OPENSSL_NO_RSA\n\t\tif (type & SSL_kRSA)\n\t\t\t{\n\t\t\trsa=cert->rsa_tmp;\n\t\t\tif ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL))\n\t\t\t\t{\n\t\t\t\trsa=s->cert->rsa_tmp_cb(s,\n\t\t\t\t SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),\n\t\t\t\t SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));\n\t\t\t\tif(rsa == NULL)\n\t\t\t\t{\n\t\t\t\t\tal=SSL_AD_HANDSHAKE_FAILURE;\n\t\t\t\t\tSSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY);\n\t\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\t\tRSA_up_ref(rsa);\n\t\t\t\tcert->rsa_tmp=rsa;\n\t\t\t\t}\n\t\t\tif (rsa == NULL)\n\t\t\t\t{\n\t\t\t\tal=SSL_AD_HANDSHAKE_FAILURE;\n\t\t\t\tSSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\tr[0]=rsa->n;\n\t\t\tr[1]=rsa->e;\n\t\t\ts->s3->tmp.use_rsa_tmp=1;\n\t\t\t}\n\t\telse\n#endif\n#ifndef OPENSSL_NO_DH\n\t\t\tif (type & SSL_kEDH)\n\t\t\t{\n\t\t\tdhp=cert->dh_tmp;\n\t\t\tif ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))\n\t\t\t\tdhp=s->cert->dh_tmp_cb(s,\n\t\t\t\t SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),\n\t\t\t\t SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));\n\t\t\tif (dhp == NULL)\n\t\t\t\t{\n\t\t\t\tal=SSL_AD_HANDSHAKE_FAILURE;\n\t\t\t\tSSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\tif (s->s3->tmp.dh != NULL)\n\t\t\t\t{\n\t\t\t\tDH_free(dh);\n\t\t\t\tSSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif ((dh=DHparams_dup(dhp)) == NULL)\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\ts->s3->tmp.dh=dh;\n\t\t\tif ((dhp->pub_key == NULL ||\n\t\t\t dhp->priv_key == NULL ||\n\t\t\t (s->options & SSL_OP_SINGLE_DH_USE)))\n\t\t\t\t{\n\t\t\t\tif(!DH_generate_key(dh))\n\t\t\t\t {\n\t\t\t\t SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,\n\t\t\t\t\t ERR_R_DH_LIB);\n\t\t\t\t goto err;\n\t\t\t\t }\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tdh->pub_key=BN_dup(dhp->pub_key);\n\t\t\t\tdh->priv_key=BN_dup(dhp->priv_key);\n\t\t\t\tif ((dh->pub_key == NULL) ||\n\t\t\t\t\t(dh->priv_key == NULL))\n\t\t\t\t\t{\n\t\t\t\t\tSSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\tr[0]=dh->p;\n\t\t\tr[1]=dh->g;\n\t\t\tr[2]=dh->pub_key;\n\t\t\t}\n\t\telse\n#endif\n\t\t\t{\n\t\t\tal=SSL_AD_HANDSHAKE_FAILURE;\n\t\t\tSSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tfor (i=0; r[i] != NULL; i++)\n\t\t\t{\n\t\t\tnr[i]=BN_num_bytes(r[i]);\n\t\t\tn+=2+nr[i];\n\t\t\t}\n\t\tif (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL))\n\t\t\t{\n\t\t\tif ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher))\n\t\t\t\t== NULL)\n\t\t\t\t{\n\t\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\tkn=EVP_PKEY_size(pkey);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tpkey=NULL;\n\t\t\tkn=0;\n\t\t\t}\n\t\tif (!BUF_MEM_grow_clean(buf,n+DTLS1_HM_HEADER_LENGTH+kn))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF);\n\t\t\tgoto err;\n\t\t\t}\n\t\td=(unsigned char *)s->init_buf->data;\n\t\tp= &(d[DTLS1_HM_HEADER_LENGTH]);\n\t\tfor (i=0; r[i] != NULL; i++)\n\t\t\t{\n\t\t\ts2n(nr[i],p);\n\t\t\tBN_bn2bin(r[i],p);\n\t\t\tp+=nr[i];\n\t\t\t}\n\t\tif (pkey != NULL)\n\t\t\t{\n#ifndef OPENSSL_NO_RSA\n\t\t\tif (pkey->type == EVP_PKEY_RSA)\n\t\t\t\t{\n\t\t\t\tq=md_buf;\n\t\t\t\tj=0;\n\t\t\t\tfor (num=2; num > 0; num--)\n\t\t\t\t\t{\n\t\t\t\t\tEVP_DigestInit_ex(&md_ctx,(num == 2)\n\t\t\t\t\t\t?s->ctx->md5:s->ctx->sha1, NULL);\n\t\t\t\t\tEVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);\n\t\t\t\t\tEVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);\n\t\t\t\t\tEVP_DigestUpdate(&md_ctx,&(d[DTLS1_HM_HEADER_LENGTH]),n);\n\t\t\t\t\tEVP_DigestFinal_ex(&md_ctx,q,\n\t\t\t\t\t\t(unsigned int *)&i);\n\t\t\t\t\tq+=i;\n\t\t\t\t\tj+=i;\n\t\t\t\t\t}\n\t\t\t\tif (RSA_sign(NID_md5_sha1, md_buf, j,\n\t\t\t\t\t&(p[2]), &u, pkey->pkey.rsa) <= 0)\n\t\t\t\t\t{\n\t\t\t\t\tSSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA);\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t\ts2n(u,p);\n\t\t\t\tn+=u+2;\n\t\t\t\t}\n\t\t\telse\n#endif\n#if !defined(OPENSSL_NO_DSA)\n\t\t\t\tif (pkey->type == EVP_PKEY_DSA)\n\t\t\t\t{\n\t\t\t\tEVP_SignInit_ex(&md_ctx,EVP_dss1(), NULL);\n\t\t\t\tEVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);\n\t\t\t\tEVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);\n\t\t\t\tEVP_SignUpdate(&md_ctx,&(d[DTLS1_HM_HEADER_LENGTH]),n);\n\t\t\t\tif (!EVP_SignFinal(&md_ctx,&(p[2]),\n\t\t\t\t\t(unsigned int *)&i,pkey))\n\t\t\t\t\t{\n\t\t\t\t\tSSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_DSA);\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t\ts2n(i,p);\n\t\t\t\tn+=i+2;\n\t\t\t\t}\n\t\t\telse\n#endif\n\t\t\t\t{\n\t\t\t\tal=SSL_AD_HANDSHAKE_FAILURE;\n\t\t\t\tSSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\t}\n\t\td = dtls1_set_message_header(s, d,\n\t\t\tSSL3_MT_SERVER_KEY_EXCHANGE, n, 0, n);\n\t\ts->init_num=n+DTLS1_HM_HEADER_LENGTH;\n\t\ts->init_off=0;\n\t\tdtls1_buffer_message(s, 0);\n\t\t}\n\ts->state = SSL3_ST_SW_KEY_EXCH_B;\n\tEVP_MD_CTX_cleanup(&md_ctx);\n\treturn(dtls1_do_write(s,SSL3_RT_HANDSHAKE));\nf_err:\n\tssl3_send_alert(s,SSL3_AL_FATAL,al);\nerr:\n\tEVP_MD_CTX_cleanup(&md_ctx);\n\treturn(-1);\n\t}', 'int BN_num_bits(const BIGNUM *a)\n\t{\n\tint i = a->top - 1;\n\tbn_check_top(a);\n\tif (BN_is_zero(a)) return 0;\n\treturn ((i*BN_BITS2) + BN_num_bits_word(a->d[i]));\n\t}'] |
563 | 0 | https://github.com/openssl/openssl/blob/40a706286febe0279336c96374c607daaa1b1d49/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);
} | ['static void int_thread_del_item(const ERR_STATE *d)\n\t{\n\tERR_STATE *p;\n\tLHASH_OF(ERR_STATE) *hash;\n\terr_fns_check();\n\thash = ERRFN(thread_get)(0);\n\tif (!hash)\n\t\treturn;\n\tCRYPTO_w_lock(CRYPTO_LOCK_ERR);\n\tp = lh_ERR_STATE_delete(hash, d);\n\tif (int_thread_hash_references == 1\n\t && int_thread_hash && lh_ERR_STATE_num_items(int_thread_hash) == 0)\n\t\t{\n\t\tlh_ERR_STATE_free(int_thread_hash);\n\t\tint_thread_hash = NULL;\n\t\t}\n\tCRYPTO_w_unlock(CRYPTO_LOCK_ERR);\n\tERRFN(thread_release)(&hash);\n\tif (p)\n\t\tERR_STATE_free(p);\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}'] |
564 | 0 | https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/qtrleenc.c/#L188 | static void qtrle_encode_line(QtrleEncContext *s, AVFrame *p, int line, uint8_t **buf)
{
int width=s->avctx->width;
int i;
signed char rlecode;
unsigned int bulkcount;
unsigned int skipcount;
unsigned int repeatcount;
int total_bulk_cost;
int total_skip_cost;
int total_repeat_cost;
int temp_cost;
int j;
uint8_t *this_line = p-> data[0] + line*p->linesize[0] + (width - 1)*s->pixel_size;
uint8_t *prev_line = s->previous_frame.data[0] + line*p->linesize[0] + (width - 1)*s->pixel_size;
s->length_table[width] = 0;
skipcount = 0;
for (i = width - 1; i >= 0; i--) {
if (!s->frame.key_frame && !memcmp(this_line, prev_line, s->pixel_size))
skipcount = FFMIN(skipcount + 1, MAX_RLE_SKIP);
else
skipcount = 0;
total_skip_cost = s->length_table[i + skipcount] + 2;
s->skip_table[i] = skipcount;
if (i < width - 1 && !memcmp(this_line, this_line + s->pixel_size, s->pixel_size))
repeatcount = FFMIN(repeatcount + 1, MAX_RLE_REPEAT);
else
repeatcount = 1;
total_repeat_cost = s->length_table[i + repeatcount] + 1 + s->pixel_size;
if (i == 0) {
total_skip_cost--;
total_repeat_cost++;
}
if (repeatcount > 1 && (skipcount == 0 || total_repeat_cost < total_skip_cost)) {
s->length_table[i] = total_repeat_cost;
s->rlecode_table[i] = -repeatcount;
}
else if (skipcount > 0) {
s->length_table[i] = total_skip_cost;
s->rlecode_table[i] = 0;
}
else {
int limit = FFMIN(width - i, MAX_RLE_BULK);
temp_cost = 1 + s->pixel_size + !i;
total_bulk_cost = INT_MAX;
for (j = 1; j <= limit; j++) {
if (s->length_table[i + j] + temp_cost < total_bulk_cost) {
total_bulk_cost = s->length_table[i + j] + temp_cost;
bulkcount = j;
}
temp_cost += s->pixel_size;
}
s->length_table[i] = total_bulk_cost;
s->rlecode_table[i] = bulkcount;
}
this_line -= s->pixel_size;
prev_line -= s->pixel_size;
}
i=0;
this_line = p-> data[0] + line*p->linesize[0];
prev_line = s->previous_frame.data[0] + line*p->linesize[0];
if (s->rlecode_table[0] == 0) {
bytestream_put_byte(buf, s->skip_table[0] + 1);
i += s->skip_table[0];
}
else bytestream_put_byte(buf, 1);
while (i < width) {
rlecode = s->rlecode_table[i];
bytestream_put_byte(buf, rlecode);
if (rlecode == 0) {
bytestream_put_byte(buf, s->skip_table[i] + 1);
i += s->skip_table[i];
}
else if (rlecode > 0) {
bytestream_put_buffer(buf, this_line + i*s->pixel_size, rlecode*s->pixel_size);
i += rlecode;
}
else {
bytestream_put_buffer(buf, this_line + i*s->pixel_size, s->pixel_size);
i -= rlecode;
}
}
bytestream_put_byte(buf, -1);
} | ['static void qtrle_encode_line(QtrleEncContext *s, AVFrame *p, int line, uint8_t **buf)\n{\n int width=s->avctx->width;\n int i;\n signed char rlecode;\n unsigned int bulkcount;\n unsigned int skipcount;\n unsigned int repeatcount;\n int total_bulk_cost;\n int total_skip_cost;\n int total_repeat_cost;\n int temp_cost;\n int j;\n uint8_t *this_line = p-> data[0] + line*p->linesize[0] + (width - 1)*s->pixel_size;\n uint8_t *prev_line = s->previous_frame.data[0] + line*p->linesize[0] + (width - 1)*s->pixel_size;\n s->length_table[width] = 0;\n skipcount = 0;\n for (i = width - 1; i >= 0; i--) {\n if (!s->frame.key_frame && !memcmp(this_line, prev_line, s->pixel_size))\n skipcount = FFMIN(skipcount + 1, MAX_RLE_SKIP);\n else\n skipcount = 0;\n total_skip_cost = s->length_table[i + skipcount] + 2;\n s->skip_table[i] = skipcount;\n if (i < width - 1 && !memcmp(this_line, this_line + s->pixel_size, s->pixel_size))\n repeatcount = FFMIN(repeatcount + 1, MAX_RLE_REPEAT);\n else\n repeatcount = 1;\n total_repeat_cost = s->length_table[i + repeatcount] + 1 + s->pixel_size;\n if (i == 0) {\n total_skip_cost--;\n total_repeat_cost++;\n }\n if (repeatcount > 1 && (skipcount == 0 || total_repeat_cost < total_skip_cost)) {\n s->length_table[i] = total_repeat_cost;\n s->rlecode_table[i] = -repeatcount;\n }\n else if (skipcount > 0) {\n s->length_table[i] = total_skip_cost;\n s->rlecode_table[i] = 0;\n }\n else {\n int limit = FFMIN(width - i, MAX_RLE_BULK);\n temp_cost = 1 + s->pixel_size + !i;\n total_bulk_cost = INT_MAX;\n for (j = 1; j <= limit; j++) {\n if (s->length_table[i + j] + temp_cost < total_bulk_cost) {\n total_bulk_cost = s->length_table[i + j] + temp_cost;\n bulkcount = j;\n }\n temp_cost += s->pixel_size;\n }\n s->length_table[i] = total_bulk_cost;\n s->rlecode_table[i] = bulkcount;\n }\n this_line -= s->pixel_size;\n prev_line -= s->pixel_size;\n }\n i=0;\n this_line = p-> data[0] + line*p->linesize[0];\n prev_line = s->previous_frame.data[0] + line*p->linesize[0];\n if (s->rlecode_table[0] == 0) {\n bytestream_put_byte(buf, s->skip_table[0] + 1);\n i += s->skip_table[0];\n }\n else bytestream_put_byte(buf, 1);\n while (i < width) {\n rlecode = s->rlecode_table[i];\n bytestream_put_byte(buf, rlecode);\n if (rlecode == 0) {\n bytestream_put_byte(buf, s->skip_table[i] + 1);\n i += s->skip_table[i];\n }\n else if (rlecode > 0) {\n bytestream_put_buffer(buf, this_line + i*s->pixel_size, rlecode*s->pixel_size);\n i += rlecode;\n }\n else {\n bytestream_put_buffer(buf, this_line + i*s->pixel_size, s->pixel_size);\n i -= rlecode;\n }\n }\n bytestream_put_byte(buf, -1);\n}'] |
565 | 0 | https://github.com/openssl/openssl/blob/6bc62a620e715f7580651ca932eab052aa527886/crypto/bn/bn_ctx.c/#L268 | static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
} | ['BIGNUM *SRP_Calc_A(const BIGNUM *a, const BIGNUM *N, const BIGNUM *g)\n{\n BN_CTX *bn_ctx;\n BIGNUM *A = NULL;\n if (a == NULL || N == NULL || g == NULL || (bn_ctx = BN_CTX_new()) == NULL)\n return NULL;\n if ((A = BN_new()) != NULL && !BN_mod_exp(A, g, a, N, bn_ctx)) {\n BN_free(A);\n A = NULL;\n }\n BN_CTX_free(bn_ctx);\n return A;\n}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n BN_CTX *ctx)\n{\n int ret;\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n if (BN_is_odd(m)) {\n# ifdef MONT_EXP_WORD\n if (a->top == 1 && !a->neg\n && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(a, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(m, BN_FLG_CONSTTIME) == 0)) {\n BN_ULONG A = a->d[0];\n ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);\n } else\n# endif\n ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);\n } else\n#endif\n#ifdef RECP_MUL_MOD\n {\n ret = BN_mod_exp_recp(r, a, p, m, ctx);\n }\n#else\n {\n ret = BN_mod_exp_simple(r, a, p, m, ctx);\n }\n#endif\n bn_check_top(r);\n return ret;\n}', 'int BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n BN_MONT_CTX *mont = NULL;\n int b, bits, ret = 0;\n int r_is_one;\n BN_ULONG w, next_w;\n BIGNUM *r, *t;\n BIGNUM *swap_tmp;\n#define BN_MOD_MUL_WORD(r, w, m) \\\n (BN_mul_word(r, (w)) && \\\n ( \\\n (BN_mod(t, r, m, ctx) && (swap_tmp = r, r = t, t = swap_tmp, 1))))\n#define BN_TO_MONTGOMERY_WORD(r, w, mont) \\\n (BN_set_word(r, (w)) && BN_to_montgomery(r, r, (mont), ctx))\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n BNerr(BN_F_BN_MOD_EXP_MONT_WORD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT_WORD, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n if (m->top == 1)\n a %= m->d[0];\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_abs_is_word(m, 1)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n if (a == 0) {\n BN_zero(rr);\n ret = 1;\n return ret;\n }\n BN_CTX_start(ctx);\n r = BN_CTX_get(ctx);\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n r_is_one = 1;\n w = a;\n for (b = bits - 2; b >= 0; b--) {\n next_w = w * w;\n if ((next_w / w) != w) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n next_w = 1;\n }\n w = next_w;\n if (!r_is_one) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (BN_is_bit_set(p, b)) {\n next_w = w * a;\n if ((next_w / a) != w) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n next_w = a;\n }\n w = next_w;\n }\n }\n if (w != 1) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n }\n if (r_is_one) {\n if (!BN_one(rr))\n goto err;\n } else {\n if (!BN_from_montgomery(rr, r, mont, ctx))\n goto err;\n }\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n BN_CTX_end(ctx);\n bn_check_top(rr);\n return ret;\n}', '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_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)\n{\n int i, ret = 0;\n BIGNUM *Ri, *R;\n if (BN_is_zero(mod))\n return 0;\n BN_CTX_start(ctx);\n if ((Ri = BN_CTX_get(ctx)) == NULL)\n goto err;\n R = &(mont->RR);\n if (!BN_copy(&(mont->N), mod))\n goto err;\n if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)\n BN_set_flags(&(mont->N), BN_FLG_CONSTTIME);\n mont->N.neg = 0;\n#ifdef MONT_WORD\n {\n BIGNUM tmod;\n BN_ULONG buf[2];\n bn_init(&tmod);\n tmod.d = buf;\n tmod.dmax = 2;\n tmod.neg = 0;\n if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)\n BN_set_flags(&tmod, BN_FLG_CONSTTIME);\n mont->ri = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2;\n# if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)\n BN_zero(R);\n if (!(BN_set_bit(R, 2 * BN_BITS2)))\n goto err;\n tmod.top = 0;\n if ((buf[0] = mod->d[0]))\n tmod.top = 1;\n if ((buf[1] = mod->top > 1 ? mod->d[1] : 0))\n tmod.top = 2;\n if (BN_is_one(&tmod))\n BN_zero(Ri);\n else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, 2 * BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (bn_expand(Ri, (int)sizeof(BN_ULONG) * 2) == NULL)\n goto err;\n Ri->neg = 0;\n Ri->d[0] = BN_MASK2;\n Ri->d[1] = BN_MASK2;\n Ri->top = 2;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;\n# else\n BN_zero(R);\n if (!(BN_set_bit(R, BN_BITS2)))\n goto err;\n buf[0] = mod->d[0];\n buf[1] = 0;\n tmod.top = buf[0] != 0 ? 1 : 0;\n if (BN_is_one(&tmod))\n BN_zero(Ri);\n else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (!BN_set_word(Ri, BN_MASK2))\n goto err;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = 0;\n# endif\n }\n#else\n {\n mont->ri = BN_num_bits(&mont->N);\n BN_zero(R);\n if (!BN_set_bit(R, mont->ri))\n goto err;\n if ((BN_mod_inverse(Ri, R, &mont->N, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, mont->ri))\n goto err;\n if (!BN_sub_word(Ri, 1))\n goto err;\n if (!BN_div(&(mont->Ni), NULL, Ri, &mont->N, ctx))\n goto err;\n }\n#endif\n BN_zero(&(mont->RR));\n if (!BN_set_bit(&(mont->RR), mont->ri * 2))\n goto err;\n if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx))\n goto err;\n for (i = mont->RR.top, ret = mont->N.top; i < ret; i++)\n mont->RR.d[i] = 0;\n mont->RR.top = ret;\n mont->RR.flags |= BN_FLG_FIXED_TOP;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'BIGNUM *BN_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 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}'] |
566 | 0 | https://github.com/openssl/openssl/blob/d8028b202bfe337200a0cc89b80983ea1838cb30/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 execute_test_large_message(const SSL_METHOD *smeth,\n const SSL_METHOD *cmeth, int read_ahead)\n{\n SSL_CTX *cctx = NULL, *sctx = NULL;\n SSL *clientssl = NULL, *serverssl = NULL;\n int testresult = 0;\n int i;\n BIO *certbio = NULL;\n X509 *chaincert = NULL;\n int certlen;\n if (!TEST_ptr(certbio = BIO_new_file(cert, "r")))\n goto end;\n chaincert = PEM_read_bio_X509(certbio, NULL, NULL, NULL);\n BIO_free(certbio);\n certbio = NULL;\n if (!TEST_ptr(chaincert))\n goto end;\n if (!TEST_true(create_ssl_ctx_pair(smeth, cmeth, &sctx,\n &cctx, cert, privkey)))\n goto end;\n if (read_ahead) {\n SSL_CTX_set_read_ahead(cctx, 1);\n }\n certlen = i2d_X509(chaincert, NULL);\n OPENSSL_assert(certlen * NUM_EXTRA_CERTS >\n (SSL3_RT_MAX_PLAIN_LENGTH * 4) / 3);\n for (i = 0; i < NUM_EXTRA_CERTS; i++) {\n if (!X509_up_ref(chaincert))\n goto end;\n if (!SSL_CTX_add_extra_chain_cert(sctx, chaincert)) {\n X509_free(chaincert);\n goto end;\n }\n }\n if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,\n NULL, NULL))\n || !TEST_true(create_ssl_connection(serverssl, clientssl,\n SSL_ERROR_NONE)))\n goto end;\n if (!TEST_true(SSL_clear(serverssl)))\n goto end;\n testresult = 1;\n end:\n X509_free(chaincert);\n SSL_free(serverssl);\n SSL_free(clientssl);\n SSL_CTX_free(sctx);\n SSL_CTX_free(cctx);\n return testresult;\n}', 'int create_ssl_objects(SSL_CTX *serverctx, SSL_CTX *clientctx, SSL **sssl,\n SSL **cssl, BIO *s_to_c_fbio, BIO *c_to_s_fbio)\n{\n SSL *serverssl = NULL, *clientssl = NULL;\n BIO *s_to_c_bio = NULL, *c_to_s_bio = NULL;\n if (*sssl != NULL)\n serverssl = *sssl;\n else if (!TEST_ptr(serverssl = SSL_new(serverctx)))\n goto error;\n if (*cssl != NULL)\n clientssl = *cssl;\n else if (!TEST_ptr(clientssl = SSL_new(clientctx)))\n goto error;\n if (SSL_is_dtls(clientssl)) {\n if (!TEST_ptr(s_to_c_bio = BIO_new(bio_s_mempacket_test()))\n || !TEST_ptr(c_to_s_bio = BIO_new(bio_s_mempacket_test())))\n goto error;\n } else {\n if (!TEST_ptr(s_to_c_bio = BIO_new(BIO_s_mem()))\n || !TEST_ptr(c_to_s_bio = BIO_new(BIO_s_mem())))\n goto error;\n }\n if (s_to_c_fbio != NULL\n && !TEST_ptr(s_to_c_bio = BIO_push(s_to_c_fbio, s_to_c_bio)))\n goto error;\n if (c_to_s_fbio != NULL\n && !TEST_ptr(c_to_s_bio = BIO_push(c_to_s_fbio, c_to_s_bio)))\n goto error;\n BIO_set_mem_eof_return(s_to_c_bio, -1);\n BIO_set_mem_eof_return(c_to_s_bio, -1);\n SSL_set_bio(serverssl, c_to_s_bio, s_to_c_bio);\n BIO_up_ref(s_to_c_bio);\n BIO_up_ref(c_to_s_bio);\n SSL_set_bio(clientssl, s_to_c_bio, c_to_s_bio);\n *sssl = serverssl;\n *cssl = clientssl;\n return 1;\n error:\n SSL_free(serverssl);\n SSL_free(clientssl);\n BIO_free(s_to_c_bio);\n BIO_free(c_to_s_bio);\n BIO_free(s_to_c_fbio);\n BIO_free(c_to_s_fbio);\n return 0;\n}', 'SSL *SSL_new(SSL_CTX *ctx)\n{\n SSL *s;\n if (ctx == NULL) {\n SSLerr(SSL_F_SSL_NEW, SSL_R_NULL_SSL_CTX);\n return (NULL);\n }\n if (ctx->method == NULL) {\n SSLerr(SSL_F_SSL_NEW, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);\n return (NULL);\n }\n s = OPENSSL_zalloc(sizeof(*s));\n if (s == NULL)\n goto err;\n s->lock = CRYPTO_THREAD_lock_new();\n if (s->lock == NULL) {\n SSLerr(SSL_F_SSL_NEW, ERR_R_MALLOC_FAILURE);\n OPENSSL_free(s);\n return NULL;\n }\n RECORD_LAYER_init(&s->rlayer, s);\n s->options = ctx->options;\n s->dane.flags = ctx->dane.flags;\n s->min_proto_version = ctx->min_proto_version;\n s->max_proto_version = ctx->max_proto_version;\n s->mode = ctx->mode;\n s->max_cert_list = ctx->max_cert_list;\n s->references = 1;\n s->max_early_data = ctx->max_early_data;\n s->cert = ssl_cert_dup(ctx->cert);\n if (s->cert == NULL)\n goto err;\n RECORD_LAYER_set_read_ahead(&s->rlayer, ctx->read_ahead);\n s->msg_callback = ctx->msg_callback;\n s->msg_callback_arg = ctx->msg_callback_arg;\n s->verify_mode = ctx->verify_mode;\n s->not_resumable_session_cb = ctx->not_resumable_session_cb;\n s->record_padding_cb = ctx->record_padding_cb;\n s->record_padding_arg = ctx->record_padding_arg;\n s->block_padding = ctx->block_padding;\n s->sid_ctx_length = ctx->sid_ctx_length;\n OPENSSL_assert(s->sid_ctx_length <= sizeof s->sid_ctx);\n memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));\n s->verify_callback = ctx->default_verify_callback;\n s->generate_session_id = ctx->generate_session_id;\n s->param = X509_VERIFY_PARAM_new();\n if (s->param == NULL)\n goto err;\n X509_VERIFY_PARAM_inherit(s->param, ctx->param);\n s->quiet_shutdown = ctx->quiet_shutdown;\n s->max_send_fragment = ctx->max_send_fragment;\n s->split_send_fragment = ctx->split_send_fragment;\n s->max_pipelines = ctx->max_pipelines;\n if (s->max_pipelines > 1)\n RECORD_LAYER_set_read_ahead(&s->rlayer, 1);\n if (ctx->default_read_buf_len > 0)\n SSL_set_default_read_buffer_len(s, ctx->default_read_buf_len);\n SSL_CTX_up_ref(ctx);\n s->ctx = ctx;\n s->ext.debug_cb = 0;\n s->ext.debug_arg = NULL;\n s->ext.ticket_expected = 0;\n s->ext.status_type = ctx->ext.status_type;\n s->ext.status_expected = 0;\n s->ext.ocsp.ids = NULL;\n s->ext.ocsp.exts = NULL;\n s->ext.ocsp.resp = NULL;\n s->ext.ocsp.resp_len = 0;\n SSL_CTX_up_ref(ctx);\n s->session_ctx = ctx;\n#ifndef OPENSSL_NO_EC\n if (ctx->ext.ecpointformats) {\n s->ext.ecpointformats =\n OPENSSL_memdup(ctx->ext.ecpointformats,\n ctx->ext.ecpointformats_len);\n if (!s->ext.ecpointformats)\n goto err;\n s->ext.ecpointformats_len =\n ctx->ext.ecpointformats_len;\n }\n if (ctx->ext.supportedgroups) {\n s->ext.supportedgroups =\n OPENSSL_memdup(ctx->ext.supportedgroups,\n ctx->ext.supportedgroups_len);\n if (!s->ext.supportedgroups)\n goto err;\n s->ext.supportedgroups_len = ctx->ext.supportedgroups_len;\n }\n#endif\n#ifndef OPENSSL_NO_NEXTPROTONEG\n s->ext.npn = NULL;\n#endif\n if (s->ctx->ext.alpn) {\n s->ext.alpn = OPENSSL_malloc(s->ctx->ext.alpn_len);\n if (s->ext.alpn == NULL)\n goto err;\n memcpy(s->ext.alpn, s->ctx->ext.alpn, s->ctx->ext.alpn_len);\n s->ext.alpn_len = s->ctx->ext.alpn_len;\n }\n s->verified_chain = NULL;\n s->verify_result = X509_V_OK;\n s->default_passwd_callback = ctx->default_passwd_callback;\n s->default_passwd_callback_userdata = ctx->default_passwd_callback_userdata;\n s->method = ctx->method;\n s->key_update = SSL_KEY_UPDATE_NONE;\n if (!s->method->ssl_new(s))\n goto err;\n s->server = (ctx->method->ssl_accept == ssl_undefined_function) ? 0 : 1;\n if (!SSL_clear(s))\n goto err;\n if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data))\n goto err;\n#ifndef OPENSSL_NO_PSK\n s->psk_client_callback = ctx->psk_client_callback;\n s->psk_server_callback = ctx->psk_server_callback;\n#endif\n s->job = NULL;\n#ifndef OPENSSL_NO_CT\n if (!SSL_set_ct_validation_callback(s, ctx->ct_validation_callback,\n ctx->ct_validation_callback_arg))\n goto err;\n#endif\n return s;\n err:\n SSL_free(s);\n SSLerr(SSL_F_SSL_NEW, ERR_R_MALLOC_FAILURE);\n return NULL;\n}', '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_clear_bad_session(SSL *s)\n{\n if ((s->session != NULL) &&\n !(s->shutdown & SSL_SENT_SHUTDOWN) &&\n !(SSL_in_init(s) || SSL_in_before(s))) {\n SSL_CTX_remove_session(s->session_ctx, s->session);\n return (1);\n } else\n return (0);\n}', 'int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)\n{\n return remove_session_lock(ctx, c, 1);\n}', 'static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)\n{\n SSL_SESSION *r;\n int ret = 0;\n if ((c != NULL) && (c->session_id_length != 0)) {\n if (lck)\n CRYPTO_THREAD_write_lock(ctx->lock);\n if ((r = lh_SSL_SESSION_retrieve(ctx->sessions, c)) == c) {\n ret = 1;\n r = lh_SSL_SESSION_delete(ctx->sessions, c);\n SSL_SESSION_list_remove(ctx, c);\n }\n 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}'] |
567 | 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_mod_mul(BIO *bp, BN_CTX *ctx)\n\t{\n\tBIGNUM *a,*b,*c,*d,*e;\n\tint i;\n\ta=BN_new();\n\tb=BN_new();\n\tc=BN_new();\n\td=BN_new();\n\te=BN_new();\n\tBN_rand(c,1024,0,0);\n\tfor (i=0; i<10; i++)\n\t\t{\n\t\tBN_rand(a,475+i*10,0,0);\n\t\tBN_rand(b,425+i*10,0,0);\n\t\ta->neg=rand_neg();\n\t\tb->neg=rand_neg();\n\t\tif (!BN_mod_mul(e,a,b,c,ctx))\n\t\t\t{\n\t\t\tunsigned long l;\n\t\t\twhile ((l=ERR_get_error()))\n\t\t\t\tfprintf(stderr,"ERROR:%s\\n",\n\t\t\t\t\tERR_error_string(l,NULL));\n\t\t\texit(1);\n\t\t\t}\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\tBN_print(bp,c);\n\t\t\t\tBIO_puts(bp," - ");\n\t\t\t\t}\n\t\t\tBN_print(bp,e);\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\tBN_free(e);\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}'] |
568 | 0 | https://github.com/libav/libav/blob/587874ef1c94a9b863d2f2db0e5d341e086ee232/libavcodec/proresdec.c/#L324 | static inline int decode_vlc_codeword(GetBitContext *gb, unsigned codebook)
{
unsigned int rice_order, exp_order, switch_bits;
unsigned int buf, code;
int log, prefix_len, len;
OPEN_READER(re, gb);
UPDATE_CACHE(re, gb);
buf = GET_CACHE(re, gb);
switch_bits = (codebook & 3) + 1;
rice_order = codebook >> 5;
exp_order = (codebook >> 2) & 7;
log = 31 - av_log2(buf);
if (log < switch_bits) {
if (!rice_order) {
code = log;
LAST_SKIP_BITS(re, gb, log + 1);
} else {
prefix_len = log + 1;
code = (log << rice_order) + NEG_USR32(buf << prefix_len, rice_order);
LAST_SKIP_BITS(re, gb, prefix_len + rice_order);
}
} else {
len = (log << 1) - switch_bits + exp_order + 1;
code = NEG_USR32(buf, len) - (1 << exp_order) + (switch_bits << rice_order);
LAST_SKIP_BITS(re, gb, len);
}
CLOSE_READER(re, gb);
return code;
} | ['static void decode_slice_plane(ProresContext *ctx, ProresThreadData *td,\n const uint8_t *buf,\n int data_size, uint16_t *out_ptr,\n int linesize, int mbs_per_slice,\n int blocks_per_mb, int plane_size_factor,\n const int16_t *qmat, int is_chroma)\n{\n GetBitContext gb;\n DCTELEM *block_ptr;\n int mb_num, blocks_per_slice;\n blocks_per_slice = mbs_per_slice * blocks_per_mb;\n memset(td->blocks, 0, 8 * 4 * 64 * sizeof(*td->blocks));\n init_get_bits(&gb, buf, data_size << 3);\n decode_dc_coeffs(&gb, td->blocks, blocks_per_slice);\n decode_ac_coeffs(&gb, td->blocks, blocks_per_slice,\n plane_size_factor, ctx->scantable.permutated);\n block_ptr = td->blocks;\n if (!is_chroma) {\n for (mb_num = 0; mb_num < mbs_per_slice; mb_num++, out_ptr += blocks_per_mb * 4) {\n ctx->dsp.idct_put(out_ptr, linesize, block_ptr, qmat);\n block_ptr += 64;\n if (blocks_per_mb > 2) {\n ctx->dsp.idct_put(out_ptr + 8, linesize, block_ptr, qmat);\n block_ptr += 64;\n }\n ctx->dsp.idct_put(out_ptr + linesize * 4, linesize, block_ptr, qmat);\n block_ptr += 64;\n if (blocks_per_mb > 2) {\n ctx->dsp.idct_put(out_ptr + linesize * 4 + 8, linesize, block_ptr, qmat);\n block_ptr += 64;\n }\n }\n } else {\n for (mb_num = 0; mb_num < mbs_per_slice; mb_num++, out_ptr += blocks_per_mb * 4) {\n ctx->dsp.idct_put(out_ptr, linesize, block_ptr, qmat);\n block_ptr += 64;\n ctx->dsp.idct_put(out_ptr + linesize * 4, linesize, block_ptr, qmat);\n block_ptr += 64;\n if (blocks_per_mb > 2) {\n ctx->dsp.idct_put(out_ptr + 8, linesize, block_ptr, qmat);\n block_ptr += 64;\n ctx->dsp.idct_put(out_ptr + linesize * 4 + 8, linesize, block_ptr, qmat);\n block_ptr += 64;\n }\n }\n }\n}', 'static inline void decode_dc_coeffs(GetBitContext *gb, DCTELEM *out,\n int nblocks)\n{\n DCTELEM prev_dc;\n int i, sign;\n int16_t delta;\n unsigned int code;\n code = decode_vlc_codeword(gb, FIRST_DC_CB);\n out[0] = prev_dc = TOSIGNED(code);\n out += 64;\n delta = 3;\n for (i = 1; i < nblocks; i++, out += 64) {\n code = decode_vlc_codeword(gb, ff_prores_dc_codebook[FFMIN(FFABS(delta), 3)]);\n sign = -(((delta >> 15) & 1) ^ (code & 1));\n delta = (((code + 1) >> 1) ^ sign) - sign;\n prev_dc += delta;\n out[0] = prev_dc;\n }\n}', 'static inline int decode_vlc_codeword(GetBitContext *gb, unsigned codebook)\n{\n unsigned int rice_order, exp_order, switch_bits;\n unsigned int buf, code;\n int log, prefix_len, len;\n OPEN_READER(re, gb);\n UPDATE_CACHE(re, gb);\n buf = GET_CACHE(re, gb);\n switch_bits = (codebook & 3) + 1;\n rice_order = codebook >> 5;\n exp_order = (codebook >> 2) & 7;\n log = 31 - av_log2(buf);\n if (log < switch_bits) {\n if (!rice_order) {\n code = log;\n LAST_SKIP_BITS(re, gb, log + 1);\n } else {\n prefix_len = log + 1;\n code = (log << rice_order) + NEG_USR32(buf << prefix_len, rice_order);\n LAST_SKIP_BITS(re, gb, prefix_len + rice_order);\n }\n } else {\n len = (log << 1) - switch_bits + exp_order + 1;\n code = NEG_USR32(buf, len) - (1 << exp_order) + (switch_bits << rice_order);\n LAST_SKIP_BITS(re, gb, len);\n }\n CLOSE_READER(re, gb);\n return code;\n}'] |
569 | 0 | https://github.com/openssl/openssl/blob/09977dd095f3c655c99b9e1810a213f7eafa7364/crypto/bn/bn_lib.c/#L342 | static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
BN_ULONG *A, *a = NULL;
const BN_ULONG *B;
int i;
bn_check_top(b);
if (words > (INT_MAX / (4 * BN_BITS2))) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
return NULL;
}
if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
return (NULL);
}
if (BN_get_flags(b,BN_FLG_SECURE))
a = A = OPENSSL_secure_zalloc(words * sizeof(*a));
else
a = A = OPENSSL_zalloc(words * sizeof(*a));
if (A == NULL) {
BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
return (NULL);
}
#if 1
B = b->d;
if (B != NULL) {
for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {
BN_ULONG a0, a1, a2, a3;
a0 = B[0];
a1 = B[1];
a2 = B[2];
a3 = B[3];
A[0] = a0;
A[1] = a1;
A[2] = a2;
A[3] = a3;
}
switch (b->top & 3) {
case 3:
A[2] = B[2];
case 2:
A[1] = B[1];
case 1:
A[0] = B[0];
case 0:
;
}
}
#else
memset(A, 0, sizeof(*A) * words);
memcpy(A, b->d, sizeof(b->d[0]) * b->top);
#endif
return (a);
} | ['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}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return (1);\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if(((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n int i;\n BN_ULONG *A;\n const BN_ULONG *B;\n bn_check_top(b);\n if (a == b)\n return (a);\n if (bn_wexpand(a, b->top) == NULL)\n return (NULL);\n#if 1\n A = a->d;\n B = b->d;\n for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {\n BN_ULONG a0, a1, a2, a3;\n a0 = B[0];\n a1 = B[1];\n a2 = B[2];\n a3 = B[3];\n A[0] = a0;\n A[1] = a1;\n A[2] = a2;\n A[3] = a3;\n }\n switch (b->top & 3) {\n case 3:\n A[2] = B[2];\n case 2:\n A[1] = B[1];\n case 1:\n A[0] = B[0];\n case 0:;\n }\n#else\n memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n#endif\n a->top = b->top;\n a->neg = b->neg;\n bn_check_top(a);\n return (a);\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *A, *a = NULL;\n const BN_ULONG *B;\n int i;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return (NULL);\n }\n if (BN_get_flags(b,BN_FLG_SECURE))\n a = A = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = A = OPENSSL_zalloc(words * sizeof(*a));\n if (A == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n#if 1\n B = b->d;\n if (B != NULL) {\n for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {\n BN_ULONG a0, a1, a2, a3;\n a0 = B[0];\n a1 = B[1];\n a2 = B[2];\n a3 = B[3];\n A[0] = a0;\n A[1] = a1;\n A[2] = a2;\n A[3] = a3;\n }\n switch (b->top & 3) {\n case 3:\n A[2] = B[2];\n case 2:\n A[1] = B[1];\n case 1:\n A[0] = B[0];\n case 0:\n ;\n }\n }\n#else\n memset(A, 0, sizeof(*A) * words);\n memcpy(A, b->d, sizeof(b->d[0]) * b->top);\n#endif\n return (a);\n}'] |
570 | 0 | https://github.com/openssl/openssl/blob/0bde1089f895718db2fe2637fda4a0c2ed6df904/crypto/lhash/lhash.c/#L240 | void *lh_delete(LHASH *lh, 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;
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 ssl_get_prev_session(SSL *s, unsigned char *session_id, int len)\n\t{\n\tSSL_SESSION *ret=NULL,data;\n\tint fatal = 0;\n\tdata.ssl_version=s->version;\n\tdata.session_id_length=len;\n\tif (len > SSL_MAX_SSL_SESSION_ID_LENGTH)\n\t\tgoto err;\n\tmemcpy(data.session_id,session_id,len);\n\tif (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_LOOKUP))\n\t\t{\n\t\tCRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);\n\t\tret=(SSL_SESSION *)lh_retrieve(s->ctx->sessions,&data);\n\t\tif (ret != NULL)\n\t\t CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);\n\t\tCRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);\n\t\t}\n\tif (ret == NULL)\n\t\t{\n\t\tint copy=1;\n\t\ts->ctx->stats.sess_miss++;\n\t\tret=NULL;\n\t\tif (s->ctx->get_session_cb != NULL\n\t\t && (ret=s->ctx->get_session_cb(s,session_id,len,©))\n\t\t != NULL)\n\t\t\t{\n\t\t\ts->ctx->stats.sess_cb_hit++;\n\t\t\tif (copy)\n\t\t\t\tCRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);\n\t\t\tSSL_CTX_add_session(s->ctx,ret);\n\t\t\t}\n\t\tif (ret == NULL)\n\t\t\tgoto err;\n\t\t}\n\tif((s->verify_mode&SSL_VERIFY_PEER)\n\t && (!s->sid_ctx_length || ret->sid_ctx_length != s->sid_ctx_length\n\t || memcmp(ret->sid_ctx,s->sid_ctx,ret->sid_ctx_length)))\n\t {\n\t\tif (s->sid_ctx_length == 0)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL_GET_PREV_SESSION,SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);\n\t\t\tfatal = 1;\n\t\t\tgoto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n#if 0\n\t\t\tSSLerr(SSL_F_SSL_GET_PREV_SESSION,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);\n#endif\n\t\t\tgoto err;\n\t\t\t}\n\t\t}\n\tif (ret->cipher == NULL)\n\t\t{\n\t\tunsigned char buf[5],*p;\n\t\tunsigned long l;\n\t\tp=buf;\n\t\tl=ret->cipher_id;\n\t\tl2n(l,p);\n\t\tif ((ret->ssl_version>>8) == SSL3_VERSION_MAJOR)\n\t\t\tret->cipher=ssl_get_cipher_by_char(s,&(buf[2]));\n\t\telse\n\t\t\tret->cipher=ssl_get_cipher_by_char(s,&(buf[1]));\n\t\tif (ret->cipher == NULL)\n\t\t\tgoto err;\n\t\t}\n#if 0\n\tCRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);\n#endif\n\tif ((long)(ret->time+ret->timeout) < (long)time(NULL))\n\t\t{\n\t\ts->ctx->stats.sess_timeout++;\n\t\tSSL_CTX_remove_session(s->ctx,ret);\n\t\tgoto err;\n\t\t}\n\ts->ctx->stats.sess_hit++;\n\tif (s->session != NULL)\n\t\tSSL_SESSION_free(s->session);\n\ts->session=ret;\n\ts->verify_result = s->session->verify_result;\n\treturn(1);\n err:\n\tif (ret != NULL)\n\t\tSSL_SESSION_free(ret);\n\tif (fatal)\n\t\treturn -1;\n\telse\n\t\treturn 0;\n\t}', 'int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)\n{\n\treturn remove_session_lock(ctx, c, 1);\n}', 'static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)\n\t{\n\tSSL_SESSION *r;\n\tint ret=0;\n\tif ((c != NULL) && (c->session_id_length != 0))\n\t\t{\n\t\tif(lck) CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);\n\t\tr=(SSL_SESSION *)lh_delete(ctx->sessions,c);\n\t\tif (r != NULL)\n\t\t\t{\n\t\t\tret=1;\n\t\t\tSSL_SESSION_list_remove(ctx,c);\n\t\t\t}\n\t\tif(lck) CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n\t\tif (ret)\n\t\t\t{\n\t\t\tr->not_resumable=1;\n\t\t\tif (ctx->remove_session_cb != NULL)\n\t\t\t\tctx->remove_session_cb(ctx,r);\n\t\t\tSSL_SESSION_free(r);\n\t\t\t}\n\t\t}\n\telse\n\t\tret=0;\n\treturn(ret);\n\t}', 'void *lh_delete(LHASH *lh, void *data)\n\t{\n\tunsigned long hash;\n\tLHASH_NODE *nn,**rn;\n\tvoid *ret;\n\tlh->error=0;\n\trn=getrn(lh,data,&hash);\n\tif (*rn == NULL)\n\t\t{\n\t\tlh->num_no_delete++;\n\t\treturn(NULL);\n\t\t}\n\telse\n\t\t{\n\t\tnn= *rn;\n\t\t*rn=nn->next;\n\t\tret=nn->data;\n\t\tFree(nn);\n\t\tlh->num_delete++;\n\t\t}\n\tlh->num_items--;\n\tif ((lh->num_nodes > MIN_NODES) &&\n\t\t(lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes)))\n\t\tcontract(lh);\n\treturn(ret);\n\t}'] |
571 | 0 | https://github.com/openssl/openssl/blob/23dd0c9f8dc6f7edf4b872d13e5644dfbbee585b/crypto/init.c/#L343 | int ossl_init_thread_start(uint64_t opts)
{
struct thread_local_inits_st *locals = ossl_init_get_thread_local(1);
if (locals == NULL)
return 0;
if (opts & OPENSSL_INIT_THREAD_ASYNC) {
#ifdef OPENSSL_INIT_DEBUG
fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
"marking thread for async\n");
#endif
locals->async = 1;
}
if (opts & OPENSSL_INIT_THREAD_ERR_STATE) {
#ifdef OPENSSL_INIT_DEBUG
fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "
"marking thread for err_state\n");
#endif
locals->err_state = 1;
}
return 1;
} | ['int ossl_init_thread_start(uint64_t opts)\n{\n struct thread_local_inits_st *locals = ossl_init_get_thread_local(1);\n if (locals == NULL)\n return 0;\n if (opts & OPENSSL_INIT_THREAD_ASYNC) {\n#ifdef OPENSSL_INIT_DEBUG\n fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "\n "marking thread for async\\n");\n#endif\n locals->async = 1;\n }\n if (opts & OPENSSL_INIT_THREAD_ERR_STATE) {\n#ifdef OPENSSL_INIT_DEBUG\n fprintf(stderr, "OPENSSL_INIT: ossl_init_thread_start: "\n "marking thread for err_state\\n");\n#endif\n locals->err_state = 1;\n }\n return 1;\n}', 'static struct thread_local_inits_st *ossl_init_get_thread_local(int alloc)\n{\n struct thread_local_inits_st *local =\n CRYPTO_THREAD_get_local(&threadstopkey);\n if (local == NULL && alloc) {\n local = OPENSSL_zalloc(sizeof *local);\n CRYPTO_THREAD_set_local(&threadstopkey, local);\n }\n if (!alloc) {\n CRYPTO_THREAD_set_local(&threadstopkey, NULL);\n }\n return local;\n}', 'void *CRYPTO_THREAD_get_local(CRYPTO_THREAD_LOCAL *key)\n{\n return pthread_getspecific(*key);\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num <= 0)\n return NULL;\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n osslargused(file); osslargused(line);\n ret = malloc(num);\n#endif\n return ret;\n}', 'int CRYPTO_THREAD_set_local(CRYPTO_THREAD_LOCAL *key, void *val)\n{\n if (pthread_setspecific(*key, val) != 0)\n return 0;\n return 1;\n}'] |
572 | 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)];
} | ['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}', '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 max, al;\n int ret = 0;\n BIGNUM *tmp, *rr;\n bn_check_top(a);\n al = a->top;\n if (al <= 0) {\n r->top = 0;\n r->neg = 0;\n return 1;\n }\n BN_CTX_start(ctx);\n rr = (a != r) ? r : BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (rr == NULL || tmp == NULL)\n goto err;\n max = 2 * al;\n if (bn_wexpand(rr, max) == NULL)\n goto err;\n if (al == 4) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[8];\n bn_sqr_normal(rr->d, a->d, 4, t);\n#else\n bn_sqr_comba4(rr->d, a->d);\n#endif\n } else if (al == 8) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[16];\n bn_sqr_normal(rr->d, a->d, 8, t);\n#else\n bn_sqr_comba8(rr->d, a->d);\n#endif\n } else {\n#if defined(BN_RECURSION)\n if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {\n BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];\n bn_sqr_normal(rr->d, a->d, al, t);\n } else {\n int j, k;\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n if (al == j) {\n if (bn_wexpand(tmp, k * 2) == NULL)\n goto err;\n bn_sqr_recursive(rr->d, a->d, al, tmp->d);\n } else {\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n }\n }\n#else\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n#endif\n }\n rr->neg = 0;\n if (a->d[al - 1] == (a->d[al - 1] & BN_MASK2l))\n rr->top = max - 1;\n else\n rr->top = max;\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(rr);\n bn_check_top(tmp);\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_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}'] |
573 | 0 | https://github.com/openssl/openssl/blob/9b02dc97e4963969da69675a871dbe80e6d31cda/crypto/bn/bn_lib.c/#L393 | 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 DH_check(const DH *dh, int *ret)\n{\n int ok = 0, r;\n BN_CTX *ctx = NULL;\n BN_ULONG l;\n BIGNUM *t1 = NULL, *t2 = NULL;\n *ret = 0;\n ctx = BN_CTX_new();\n if (ctx == NULL)\n goto err;\n BN_CTX_start(ctx);\n t1 = BN_CTX_get(ctx);\n t2 = BN_CTX_get(ctx);\n if (t2 == NULL)\n goto err;\n if (dh->q) {\n if (BN_cmp(dh->g, BN_value_one()) <= 0)\n *ret |= DH_NOT_SUITABLE_GENERATOR;\n else if (BN_cmp(dh->g, dh->p) >= 0)\n *ret |= DH_NOT_SUITABLE_GENERATOR;\n else {\n if (!BN_mod_exp(t1, dh->g, dh->q, dh->p, ctx))\n goto err;\n if (!BN_is_one(t1))\n *ret |= DH_NOT_SUITABLE_GENERATOR;\n }\n r = BN_is_prime_ex(dh->q, BN_prime_checks, ctx, NULL);\n if (r < 0)\n goto err;\n if (!r)\n *ret |= DH_CHECK_Q_NOT_PRIME;\n if (!BN_div(t1, t2, dh->p, dh->q, ctx))\n goto err;\n if (!BN_is_one(t2))\n *ret |= DH_CHECK_INVALID_Q_VALUE;\n if (dh->j && BN_cmp(dh->j, t1))\n *ret |= DH_CHECK_INVALID_J_VALUE;\n } else if (BN_is_word(dh->g, DH_GENERATOR_2)) {\n l = BN_mod_word(dh->p, 24);\n if (l == (BN_ULONG)-1)\n goto err;\n if (l != 11)\n *ret |= DH_NOT_SUITABLE_GENERATOR;\n } else if (BN_is_word(dh->g, DH_GENERATOR_5)) {\n l = BN_mod_word(dh->p, 10);\n if (l == (BN_ULONG)-1)\n goto err;\n if ((l != 3) && (l != 7))\n *ret |= DH_NOT_SUITABLE_GENERATOR;\n } else\n *ret |= DH_UNABLE_TO_CHECK_GENERATOR;\n r = BN_is_prime_ex(dh->p, BN_prime_checks, ctx, NULL);\n if (r < 0)\n goto err;\n if (!r)\n *ret |= DH_CHECK_P_NOT_PRIME;\n else if (!dh->q) {\n if (!BN_rshift1(t1, dh->p))\n goto err;\n r = BN_is_prime_ex(t1, BN_prime_checks, ctx, NULL);\n if (r < 0)\n goto err;\n if (!r)\n *ret |= DH_CHECK_P_NOT_SAFE_PRIME;\n }\n ok = 1;\n err:\n if (ctx != NULL) {\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n }\n return ok;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return 1;\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n BN_CTX *ctx)\n{\n int ret;\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n if (BN_is_odd(m)) {\n# ifdef MONT_EXP_WORD\n if (a->top == 1 && !a->neg\n && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(a, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(m, BN_FLG_CONSTTIME) == 0)) {\n BN_ULONG A = a->d[0];\n ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);\n } else\n# endif\n ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);\n } else\n#endif\n#ifdef RECP_MUL_MOD\n {\n ret = BN_mod_exp_recp(r, a, p, m, ctx);\n }\n#else\n {\n ret = BN_mod_exp_simple(r, a, p, m, ctx);\n }\n#endif\n bn_check_top(r);\n return ret;\n}', 'int BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n BN_MONT_CTX *mont = NULL;\n int b, bits, ret = 0;\n int r_is_one;\n BN_ULONG w, next_w;\n BIGNUM *r, *t;\n BIGNUM *swap_tmp;\n#define BN_MOD_MUL_WORD(r, w, m) \\\n (BN_mul_word(r, (w)) && \\\n ( \\\n (BN_mod(t, r, m, ctx) && (swap_tmp = r, r = t, t = swap_tmp, 1))))\n#define BN_TO_MONTGOMERY_WORD(r, w, mont) \\\n (BN_set_word(r, (w)) && BN_to_montgomery(r, r, (mont), ctx))\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n BNerr(BN_F_BN_MOD_EXP_MONT_WORD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT_WORD, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n if (m->top == 1)\n a %= m->d[0];\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_is_one(m)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n if (a == 0) {\n BN_zero(rr);\n ret = 1;\n return ret;\n }\n BN_CTX_start(ctx);\n r = BN_CTX_get(ctx);\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n r_is_one = 1;\n w = a;\n for (b = bits - 2; b >= 0; b--) {\n next_w = w * w;\n if ((next_w / w) != w) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n next_w = 1;\n }\n w = next_w;\n if (!r_is_one) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (BN_is_bit_set(p, b)) {\n next_w = w * a;\n if ((next_w / a) != w) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n next_w = a;\n }\n w = next_w;\n }\n }\n if (w != 1) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n }\n if (r_is_one) {\n if (!BN_one(rr))\n goto err;\n } else {\n if (!BN_from_montgomery(rr, r, mont, ctx))\n goto err;\n }\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n BN_CTX_end(ctx);\n bn_check_top(rr);\n return ret;\n}'] |
574 | 0 | https://github.com/openssl/openssl/blob/c148d7097811c18f277a8559753c770f4ff85771/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_update_cache(SSL *s,int mode)\n\t{\n\tint i;\n\tif (s->session->session_id_length == 0) return;\n\tif ((s->ctx->session_cache_mode & mode)\n\t\t&& (!s->hit)\n\t\t&& SSL_CTX_add_session(s->ctx,s->session)\n\t\t&& (s->ctx->new_session_cb != NULL))\n\t\t{\n\t\tCRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);\n\t\tif (!s->ctx->new_session_cb(s,s->session))\n\t\t\tSSL_SESSION_free(s->session);\n\t\t}\n\ti=s->ctx->session_cache_mode;\n\tif ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&\n\t\t((i & mode) == mode))\n\t\t{\n\t\tif ( (((mode & SSL_SESS_CACHE_CLIENT)\n\t\t\t?s->ctx->stats.sess_connect_good\n\t\t\t:s->ctx->stats.sess_accept_good) & 0xff) == 0xff)\n\t\t\t{\n\t\t\tSSL_CTX_flush_sessions(s->ctx,time(NULL));\n\t\t\t}\n\t\t}\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}'] |
575 | 0 | https://github.com/nginx/nginx/blob/ddb7cd1c410a7166d8e28092d714f782ed1d69b3/src/http/modules/ngx_http_limit_conn_module.c/#L728 | static char *
ngx_http_limit_conn(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_shm_zone_t *shm_zone;
ngx_http_limit_conn_conf_t *lccf = conf;
ngx_http_limit_conn_limit_t *limit, *limits;
ngx_str_t *value;
ngx_int_t n;
ngx_uint_t i;
value = cf->args->elts;
shm_zone = ngx_shared_memory_add(cf, &value[1], 0,
&ngx_http_limit_conn_module);
if (shm_zone == NULL) {
return NGX_CONF_ERROR;
}
limits = lccf->limits.elts;
if (limits == NULL) {
if (ngx_array_init(&lccf->limits, cf->pool, 1,
sizeof(ngx_http_limit_conn_limit_t))
!= NGX_OK)
{
return NGX_CONF_ERROR;
}
}
for (i = 0; i < lccf->limits.nelts; i++) {
if (shm_zone == limits[i].shm_zone) {
return "is duplicate";
}
}
n = ngx_atoi(value[2].data, value[2].len);
if (n <= 0) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid number of connections \"%V\"", &value[2]);
return NGX_CONF_ERROR;
}
if (n > 65535) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"connection limit must be less 65536");
return NGX_CONF_ERROR;
}
limit = ngx_array_push(&lccf->limits);
limit->conn = n;
limit->shm_zone = shm_zone;
return NGX_CONF_OK;
} | ['static char *\nngx_http_limit_conn(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)\n{\n ngx_shm_zone_t *shm_zone;\n ngx_http_limit_conn_conf_t *lccf = conf;\n ngx_http_limit_conn_limit_t *limit, *limits;\n ngx_str_t *value;\n ngx_int_t n;\n ngx_uint_t i;\n value = cf->args->elts;\n shm_zone = ngx_shared_memory_add(cf, &value[1], 0,\n &ngx_http_limit_conn_module);\n if (shm_zone == NULL) {\n return NGX_CONF_ERROR;\n }\n limits = lccf->limits.elts;\n if (limits == NULL) {\n if (ngx_array_init(&lccf->limits, cf->pool, 1,\n sizeof(ngx_http_limit_conn_limit_t))\n != NGX_OK)\n {\n return NGX_CONF_ERROR;\n }\n }\n for (i = 0; i < lccf->limits.nelts; i++) {\n if (shm_zone == limits[i].shm_zone) {\n return "is duplicate";\n }\n }\n n = ngx_atoi(value[2].data, value[2].len);\n if (n <= 0) {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "invalid number of connections \\"%V\\"", &value[2]);\n return NGX_CONF_ERROR;\n }\n if (n > 65535) {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "connection limit must be less 65536");\n return NGX_CONF_ERROR;\n }\n limit = ngx_array_push(&lccf->limits);\n limit->conn = n;\n limit->shm_zone = shm_zone;\n return NGX_CONF_OK;\n}', "ngx_int_t\nngx_atoi(u_char *line, size_t n)\n{\n ngx_int_t value;\n if (n == 0) {\n return NGX_ERROR;\n }\n for (value = 0; n--; line++) {\n if (*line < '0' || *line > '9') {\n return NGX_ERROR;\n }\n value = value * 10 + (*line - '0');\n }\n if (value < 0) {\n return NGX_ERROR;\n } else {\n return value;\n }\n}", 'void *\nngx_array_push(ngx_array_t *a)\n{\n void *elt, *new;\n size_t size;\n ngx_pool_t *p;\n if (a->nelts == a->nalloc) {\n size = a->size * a->nalloc;\n p = a->pool;\n if ((u_char *) a->elts + size == p->d.last\n && p->d.last + a->size <= p->d.end)\n {\n p->d.last += a->size;\n a->nalloc++;\n } else {\n new = ngx_palloc(p, 2 * size);\n if (new == NULL) {\n return NULL;\n }\n ngx_memcpy(new, a->elts, size);\n a->elts = new;\n a->nalloc *= 2;\n }\n }\n elt = (u_char *) a->elts + a->size * a->nelts;\n a->nelts++;\n return elt;\n}'] |
576 | 0 | https://github.com/libav/libav/blob/26f027fba1c5ab482fa2488fbe0fa36c8bb33b69/libavfilter/drawutils.c/#L75 | int ff_fill_line_with_color(uint8_t *line[4], int pixel_step[4], int w, uint8_t dst_color[4],
enum AVPixelFormat pix_fmt, uint8_t rgba_color[4],
int *is_packed_rgba, uint8_t rgba_map_ptr[4])
{
uint8_t rgba_map[4] = {0};
int i;
const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(pix_fmt);
int hsub = pix_desc->log2_chroma_w;
*is_packed_rgba = 1;
switch (pix_fmt) {
case AV_PIX_FMT_ARGB: rgba_map[ALPHA] = 0; rgba_map[RED ] = 1; rgba_map[GREEN] = 2; rgba_map[BLUE ] = 3; break;
case AV_PIX_FMT_ABGR: rgba_map[ALPHA] = 0; rgba_map[BLUE ] = 1; rgba_map[GREEN] = 2; rgba_map[RED ] = 3; break;
case AV_PIX_FMT_RGBA:
case AV_PIX_FMT_RGB24: rgba_map[RED ] = 0; rgba_map[GREEN] = 1; rgba_map[BLUE ] = 2; rgba_map[ALPHA] = 3; break;
case AV_PIX_FMT_BGRA:
case AV_PIX_FMT_BGR24: rgba_map[BLUE ] = 0; rgba_map[GREEN] = 1; rgba_map[RED ] = 2; rgba_map[ALPHA] = 3; break;
default:
*is_packed_rgba = 0;
}
if (*is_packed_rgba) {
pixel_step[0] = (av_get_bits_per_pixel(pix_desc))>>3;
for (i = 0; i < 4; i++)
dst_color[rgba_map[i]] = rgba_color[i];
line[0] = av_malloc(w * pixel_step[0]);
for (i = 0; i < w; i++)
memcpy(line[0] + i * pixel_step[0], dst_color, pixel_step[0]);
if (rgba_map_ptr)
memcpy(rgba_map_ptr, rgba_map, sizeof(rgba_map[0]) * 4);
} else {
int plane;
dst_color[0] = RGB_TO_Y_CCIR(rgba_color[0], rgba_color[1], rgba_color[2]);
dst_color[1] = RGB_TO_U_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
dst_color[2] = RGB_TO_V_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
dst_color[3] = rgba_color[3];
for (plane = 0; plane < 4; plane++) {
int line_size;
int hsub1 = (plane == 1 || plane == 2) ? hsub : 0;
pixel_step[plane] = 1;
line_size = (w >> hsub1) * pixel_step[plane];
line[plane] = av_malloc(line_size);
memset(line[plane], dst_color[plane], line_size);
}
}
return 0;
} | ['int ff_fill_line_with_color(uint8_t *line[4], int pixel_step[4], int w, uint8_t dst_color[4],\n enum AVPixelFormat pix_fmt, uint8_t rgba_color[4],\n int *is_packed_rgba, uint8_t rgba_map_ptr[4])\n{\n uint8_t rgba_map[4] = {0};\n int i;\n const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(pix_fmt);\n int hsub = pix_desc->log2_chroma_w;\n *is_packed_rgba = 1;\n switch (pix_fmt) {\n case AV_PIX_FMT_ARGB: rgba_map[ALPHA] = 0; rgba_map[RED ] = 1; rgba_map[GREEN] = 2; rgba_map[BLUE ] = 3; break;\n case AV_PIX_FMT_ABGR: rgba_map[ALPHA] = 0; rgba_map[BLUE ] = 1; rgba_map[GREEN] = 2; rgba_map[RED ] = 3; break;\n case AV_PIX_FMT_RGBA:\n case AV_PIX_FMT_RGB24: rgba_map[RED ] = 0; rgba_map[GREEN] = 1; rgba_map[BLUE ] = 2; rgba_map[ALPHA] = 3; break;\n case AV_PIX_FMT_BGRA:\n case AV_PIX_FMT_BGR24: rgba_map[BLUE ] = 0; rgba_map[GREEN] = 1; rgba_map[RED ] = 2; rgba_map[ALPHA] = 3; break;\n default:\n *is_packed_rgba = 0;\n }\n if (*is_packed_rgba) {\n pixel_step[0] = (av_get_bits_per_pixel(pix_desc))>>3;\n for (i = 0; i < 4; i++)\n dst_color[rgba_map[i]] = rgba_color[i];\n line[0] = av_malloc(w * pixel_step[0]);\n for (i = 0; i < w; i++)\n memcpy(line[0] + i * pixel_step[0], dst_color, pixel_step[0]);\n if (rgba_map_ptr)\n memcpy(rgba_map_ptr, rgba_map, sizeof(rgba_map[0]) * 4);\n } else {\n int plane;\n dst_color[0] = RGB_TO_Y_CCIR(rgba_color[0], rgba_color[1], rgba_color[2]);\n dst_color[1] = RGB_TO_U_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);\n dst_color[2] = RGB_TO_V_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);\n dst_color[3] = rgba_color[3];\n for (plane = 0; plane < 4; plane++) {\n int line_size;\n int hsub1 = (plane == 1 || plane == 2) ? hsub : 0;\n pixel_step[plane] = 1;\n line_size = (w >> hsub1) * pixel_step[plane];\n line[plane] = av_malloc(line_size);\n memset(line[plane], dst_color[plane], line_size);\n }\n }\n return 0;\n}', 'const AVPixFmtDescriptor *av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)\n{\n if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB)\n return NULL;\n return &av_pix_fmt_descriptors[pix_fmt];\n}', '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}'] |
577 | 0 | https://github.com/openssl/openssl/blob/54d00677f305375eee65a0c9edb5f0980c5f020f/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 ec_GF2m_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point,\n BN_CTX *ctx)\n{\n int ret = -1;\n BN_CTX *new_ctx = NULL;\n BIGNUM *lh, *y2;\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 if (EC_POINT_is_at_infinity(group, point))\n return 1;\n field_mul = group->meth->field_mul;\n field_sqr = group->meth->field_sqr;\n if (!point->Z_is_one)\n return -1;\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return -1;\n }\n BN_CTX_start(ctx);\n y2 = BN_CTX_get(ctx);\n lh = BN_CTX_get(ctx);\n if (lh == NULL)\n goto err;\n if (!BN_GF2m_add(lh, point->X, group->a))\n goto err;\n if (!field_mul(group, lh, lh, point->X, ctx))\n goto err;\n if (!BN_GF2m_add(lh, lh, point->Y))\n goto err;\n if (!field_mul(group, lh, lh, point->X, ctx))\n goto err;\n if (!BN_GF2m_add(lh, lh, group->b))\n goto err;\n if (!field_sqr(group, y2, point->Y, ctx))\n goto err;\n if (!BN_GF2m_add(lh, lh, y2))\n goto err;\n ret = BN_is_zero(lh);\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 ret->flags &= (~BN_FLG_CONSTTIME);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n a->flags &= ~BN_FLG_FIXED_TOP;\n bn_check_top(a);\n return 1;\n}', 'int BN_GF2m_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int i;\n const BIGNUM *at, *bt;\n bn_check_top(a);\n bn_check_top(b);\n if (a->top < b->top) {\n at = b;\n bt = a;\n } else {\n at = a;\n bt = b;\n }\n if (bn_wexpand(r, at->top) == NULL)\n return 0;\n for (i = 0; i < bt->top; i++) {\n r->d[i] = at->d[i] ^ bt->d[i];\n }\n for (; i < at->top; i++) {\n r->d[i] = at->d[i];\n }\n r->top = at->top;\n bn_correct_top(r);\n return 1;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}'] |
578 | 0 | https://github.com/openssl/openssl/blob/ea32151f7b9353f8906188d007c6893704ac17bb/crypto/lhash/lhash.c/#L123 | void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
{
unsigned long hash;
OPENSSL_LH_NODE *nn, **rn;
void *ret;
lh->error = 0;
rn = getrn(lh, data, &hash);
if (*rn == NULL) {
lh->num_no_delete++;
return (NULL);
} else {
nn = *rn;
*rn = nn->next;
ret = nn->data;
OPENSSL_free(nn);
lh->num_delete++;
}
lh->num_items--;
if ((lh->num_nodes > MIN_NODES) &&
(lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
contract(lh);
return (ret);
} | ['static int ssl_free(BIO *a)\n{\n BIO_SSL *bs;\n if (a == NULL)\n return (0);\n bs = BIO_get_data(a);\n if (bs->ssl != NULL)\n SSL_shutdown(bs->ssl);\n if (BIO_get_shutdown(a)) {\n if (BIO_get_init(a))\n SSL_free(bs->ssl);\n BIO_clear_flags(a, ~0);\n BIO_set_init(a, 0);\n }\n OPENSSL_free(bs);\n return 1;\n}', 'void SSL_free(SSL *s)\n{\n int i;\n if (s == NULL)\n return;\n CRYPTO_atomic_add(&s->references, -1, &i, s->lock);\n REF_PRINT_COUNT("SSL", s);\n if (i > 0)\n return;\n REF_ASSERT_ISNT(i < 0);\n X509_VERIFY_PARAM_free(s->param);\n dane_final(&s->dane);\n CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);\n if (s->bbio != NULL) {\n if (s->bbio == s->wbio) {\n s->wbio = BIO_pop(s->wbio);\n }\n BIO_free(s->bbio);\n s->bbio = NULL;\n }\n BIO_free_all(s->rbio);\n if (s->wbio != s->rbio)\n BIO_free_all(s->wbio);\n BUF_MEM_free(s->init_buf);\n sk_SSL_CIPHER_free(s->cipher_list);\n sk_SSL_CIPHER_free(s->cipher_list_by_id);\n if (s->session != NULL) {\n ssl_clear_bad_session(s);\n SSL_SESSION_free(s->session);\n }\n clear_ciphers(s);\n ssl_cert_free(s->cert);\n OPENSSL_free(s->tlsext_hostname);\n SSL_CTX_free(s->initial_ctx);\n#ifndef OPENSSL_NO_EC\n OPENSSL_free(s->tlsext_ecpointformatlist);\n OPENSSL_free(s->tlsext_ellipticcurvelist);\n#endif\n sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts, X509_EXTENSION_free);\n#ifndef OPENSSL_NO_OCSP\n sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids, OCSP_RESPID_free);\n#endif\n#ifndef OPENSSL_NO_CT\n SCT_LIST_free(s->scts);\n OPENSSL_free(s->tlsext_scts);\n#endif\n OPENSSL_free(s->tlsext_ocsp_resp);\n OPENSSL_free(s->alpn_client_proto_list);\n sk_X509_NAME_pop_free(s->client_CA, X509_NAME_free);\n sk_X509_pop_free(s->verified_chain, X509_free);\n if (s->method != NULL)\n s->method->ssl_free(s);\n RECORD_LAYER_release(&s->rlayer);\n SSL_CTX_free(s->ctx);\n ASYNC_WAIT_CTX_free(s->waitctx);\n#if !defined(OPENSSL_NO_NEXTPROTONEG)\n OPENSSL_free(s->next_proto_negotiated);\n#endif\n#ifndef OPENSSL_NO_SRTP\n sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);\n#endif\n CRYPTO_THREAD_lock_free(s->lock);\n OPENSSL_free(s);\n}', 'int ssl_clear_bad_session(SSL *s)\n{\n if ((s->session != NULL) &&\n !(s->shutdown & SSL_SENT_SHUTDOWN) &&\n !(SSL_in_init(s) || SSL_in_before(s))) {\n SSL_CTX_remove_session(s->ctx, s->session);\n return (1);\n } else\n return (0);\n}', 'int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)\n{\n return remove_session_lock(ctx, c, 1);\n}', 'static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)\n{\n SSL_SESSION *r;\n int ret = 0;\n if ((c != NULL) && (c->session_id_length != 0)) {\n if (lck)\n CRYPTO_THREAD_write_lock(ctx->lock);\n if ((r = lh_SSL_SESSION_retrieve(ctx->sessions, c)) == c) {\n ret = 1;\n r = lh_SSL_SESSION_delete(ctx->sessions, c);\n SSL_SESSION_list_remove(ctx, c);\n }\n if (lck)\n CRYPTO_THREAD_unlock(ctx->lock);\n if (ret) {\n r->not_resumable = 1;\n if (ctx->remove_session_cb != NULL)\n ctx->remove_session_cb(ctx, r);\n SSL_SESSION_free(r);\n }\n } else\n ret = 0;\n return (ret);\n}', 'DEFINE_LHASH_OF(SSL_SESSION)', 'void *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}'] |
579 | 0 | https://gitlab.com/libtiff/libtiff/blob/d85a64b6d6379c9f9b8de6ff3a26e380b0fc3e18/tools/tiff2pdf.c/#L1961 | void t2p_read_tiff_size_tile(T2P* t2p, TIFF* input, ttile_t tile){
uint64* tbc = NULL;
uint16 edge=0;
#ifdef JPEG_SUPPORT
unsigned char* jpt;
#endif
uint64 k;
edge |= t2p_tile_is_right_edge(t2p->tiff_tiles[t2p->pdf_page], tile);
edge |= t2p_tile_is_bottom_edge(t2p->tiff_tiles[t2p->pdf_page], tile);
if(t2p->pdf_transcode==T2P_TRANSCODE_RAW){
if(edge
#if defined(JPEG_SUPPORT) || defined(OJPEG_SUPPORT)
&& !(t2p->pdf_compression==T2P_COMPRESS_JPEG)
#endif
){
t2p->tiff_datasize=TIFFTileSize(input);
if (t2p->tiff_datasize == 0) {
t2p->t2p_error = T2P_ERR_ERROR;
}
return;
} else {
TIFFGetField(input, TIFFTAG_TILEBYTECOUNTS, &tbc);
k=tbc[tile];
#ifdef OJPEG_SUPPORT
if(t2p->tiff_compression==COMPRESSION_OJPEG){
k = checkAdd64(k, 2048, t2p);
}
#endif
#ifdef JPEG_SUPPORT
if(t2p->tiff_compression==COMPRESSION_JPEG) {
uint32 count = 0;
if(TIFFGetField(input, TIFFTAG_JPEGTABLES, &count, &jpt)!=0){
if(count > 4){
k = checkAdd64(k, count, t2p);
k -= 2;
}
}
}
#endif
t2p->tiff_datasize = (tsize_t) k;
if ((uint64) t2p->tiff_datasize != k) {
TIFFError(TIFF2PDF_MODULE, "Integer overflow");
t2p->t2p_error = T2P_ERR_ERROR;
}
return;
}
}
k = TIFFTileSize(input);
if(t2p->tiff_planar==PLANARCONFIG_SEPARATE){
k = checkMultiply64(k, t2p->tiff_samplesperpixel, t2p);
}
if (k == 0) {
t2p->t2p_error = T2P_ERR_ERROR;
}
t2p->tiff_datasize = (tsize_t) k;
if ((uint64) t2p->tiff_datasize != k) {
TIFFError(TIFF2PDF_MODULE, "Integer overflow");
t2p->t2p_error = T2P_ERR_ERROR;
}
return;
} | ['void t2p_read_tiff_size_tile(T2P* t2p, TIFF* input, ttile_t tile){\n\tuint64* tbc = NULL;\n\tuint16 edge=0;\n#ifdef JPEG_SUPPORT\n\tunsigned char* jpt;\n#endif\n uint64 k;\n\tedge |= t2p_tile_is_right_edge(t2p->tiff_tiles[t2p->pdf_page], tile);\n\tedge |= t2p_tile_is_bottom_edge(t2p->tiff_tiles[t2p->pdf_page], tile);\n\tif(t2p->pdf_transcode==T2P_TRANSCODE_RAW){\n\t\tif(edge\n#if defined(JPEG_SUPPORT) || defined(OJPEG_SUPPORT)\n\t\t&& !(t2p->pdf_compression==T2P_COMPRESS_JPEG)\n#endif\n\t\t){\n\t\t\tt2p->tiff_datasize=TIFFTileSize(input);\n\t\t\tif (t2p->tiff_datasize == 0) {\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t}\n\t\t\treturn;\n\t\t} else {\n\t\t\tTIFFGetField(input, TIFFTAG_TILEBYTECOUNTS, &tbc);\n\t\t\tk=tbc[tile];\n#ifdef OJPEG_SUPPORT\n\t\t\tif(t2p->tiff_compression==COMPRESSION_OJPEG){\n\t\t\t \tk = checkAdd64(k, 2048, t2p);\n\t\t\t}\n#endif\n#ifdef JPEG_SUPPORT\n\t\t\tif(t2p->tiff_compression==COMPRESSION_JPEG) {\n\t\t\t\tuint32 count = 0;\n\t\t\t\tif(TIFFGetField(input, TIFFTAG_JPEGTABLES, &count, &jpt)!=0){\n\t\t\t\t\tif(count > 4){\n\t\t\t\t\t\tk = checkAdd64(k, count, t2p);\n\t\t\t\t\t\tk -= 2;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n#endif\n\t\t\tt2p->tiff_datasize = (tsize_t) k;\n\t\t\tif ((uint64) t2p->tiff_datasize != k) {\n\t\t\t\tTIFFError(TIFF2PDF_MODULE, "Integer overflow");\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\t}\n\tk = TIFFTileSize(input);\n\tif(t2p->tiff_planar==PLANARCONFIG_SEPARATE){\n\t\tk = checkMultiply64(k, t2p->tiff_samplesperpixel, t2p);\n\t}\n\tif (k == 0) {\n\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t}\n\tt2p->tiff_datasize = (tsize_t) k;\n\tif ((uint64) t2p->tiff_datasize != k) {\n\t\tTIFFError(TIFF2PDF_MODULE, "Integer overflow");\n\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t}\n\treturn;\n}', 'int t2p_tile_is_right_edge(T2P_TILES tiles, ttile_t tile){\n\tif( ((tile+1) % tiles.tiles_tilecountx == 0)\n\t\t&& (tiles.tiles_edgetilewidth != 0) ){\n\t\treturn(1);\n\t} else {\n\t\treturn(0);\n\t}\n}', 'int t2p_tile_is_bottom_edge(T2P_TILES tiles, ttile_t tile){\n\tif( ((tile+1) > (tiles.tiles_tilecount-tiles.tiles_tilecountx) )\n\t\t&& (tiles.tiles_edgetilelength != 0) ){\n\t\treturn(1);\n\t} else {\n\t\treturn(0);\n\t}\n}', 'int\nTIFFGetField(TIFF* tif, uint32 tag, ...)\n{\n\tint status;\n\tva_list ap;\n\tva_start(ap, tag);\n\tstatus = TIFFVGetField(tif, tag, ap);\n\tva_end(ap);\n\treturn (status);\n}'] |
580 | 0 | https://github.com/openssl/openssl/blob/9f519addc09b2005fa8c6cde36e3267de02577bb/crypto/lhash/lhash.c/#L248 | static void doall_util_fn(_LHASH *lh, int use_arg, LHASH_DOALL_FN_TYPE func,
LHASH_DOALL_ARG_FN_TYPE func_arg, void *arg)
{
int i;
LHASH_NODE *a, *n;
if (lh == NULL)
return;
for (i = lh->num_nodes - 1; i >= 0; i--) {
a = lh->b[i];
while (a != NULL) {
n = a->next;
if (use_arg)
func_arg(a->data, arg);
else
func(a->data);
a = n;
}
}
} | ['const EVP_PKEY_ASN1_METHOD *ENGINE_pkey_asn1_find_str(ENGINE **pe,\n const char *str,\n int len)\n{\n ENGINE_FIND_STR fstr;\n fstr.e = NULL;\n fstr.ameth = NULL;\n fstr.str = str;\n fstr.len = len;\n CRYPTO_THREAD_run_once(&engine_lock_init, do_engine_lock_init);\n CRYPTO_THREAD_write_lock(global_engine_lock);\n engine_table_doall(pkey_asn1_meth_table, look_str_cb, &fstr);\n if (fstr.e) {\n fstr.e->struct_ref++;\n engine_ref_debug(fstr.e, 0, 1);\n }\n *pe = fstr.e;\n CRYPTO_THREAD_unlock(global_engine_lock);\n return fstr.ameth;\n}', 'void engine_table_doall(ENGINE_TABLE *table, engine_table_doall_cb *cb,\n void *arg)\n{\n ENGINE_PILE_DOALL dall;\n dall.cb = cb;\n dall.arg = arg;\n if (table)\n lh_ENGINE_PILE_doall_ENGINE_PILE_DOALL(&table->piles, int_dall, &dall);\n}', 'IMPLEMENT_LHASH_DOALL_ARG_CONST(ENGINE_PILE, ENGINE_PILE_DOALL)', 'void lh_doall_arg(_LHASH *lh, LHASH_DOALL_ARG_FN_TYPE func, void *arg)\n{\n doall_util_fn(lh, 1, (LHASH_DOALL_FN_TYPE)0, func, arg);\n}', 'static void doall_util_fn(_LHASH *lh, int use_arg, LHASH_DOALL_FN_TYPE func,\n LHASH_DOALL_ARG_FN_TYPE func_arg, void *arg)\n{\n int i;\n LHASH_NODE *a, *n;\n if (lh == NULL)\n return;\n for (i = lh->num_nodes - 1; i >= 0; i--) {\n a = lh->b[i];\n while (a != NULL) {\n n = a->next;\n if (use_arg)\n func_arg(a->data, arg);\n else\n func(a->data);\n a = n;\n }\n }\n}'] |
581 | 0 | https://github.com/openssl/openssl/blob/1fac96e4d6484a517f2ebe99b72016726391723c/crypto/des/des_enc.c/#L144 | void des_encrypt(DES_LONG *data, des_key_schedule ks, int enc)
{
register DES_LONG l,r,t,u;
#ifdef DES_PTR
register const unsigned char *des_SP=(const unsigned char *)des_SPtrans;
#endif
#ifndef DES_UNROLL
register int i;
#endif
register DES_LONG *s;
r=data[0];
l=data[1];
IP(r,l);
r=ROTATE(r,29)&0xffffffffL;
l=ROTATE(l,29)&0xffffffffL;
s=(DES_LONG *)ks;
if (enc)
{
#ifdef DES_UNROLL
D_ENCRYPT(l,r, 0);
D_ENCRYPT(r,l, 2);
D_ENCRYPT(l,r, 4);
D_ENCRYPT(r,l, 6);
D_ENCRYPT(l,r, 8);
D_ENCRYPT(r,l,10);
D_ENCRYPT(l,r,12);
D_ENCRYPT(r,l,14);
D_ENCRYPT(l,r,16);
D_ENCRYPT(r,l,18);
D_ENCRYPT(l,r,20);
D_ENCRYPT(r,l,22);
D_ENCRYPT(l,r,24);
D_ENCRYPT(r,l,26);
D_ENCRYPT(l,r,28);
D_ENCRYPT(r,l,30);
#else
for (i=0; i<32; i+=8)
{
D_ENCRYPT(l,r,i+0);
D_ENCRYPT(r,l,i+2);
D_ENCRYPT(l,r,i+4);
D_ENCRYPT(r,l,i+6);
}
#endif
}
else
{
#ifdef DES_UNROLL
D_ENCRYPT(l,r,30);
D_ENCRYPT(r,l,28);
D_ENCRYPT(l,r,26);
D_ENCRYPT(r,l,24);
D_ENCRYPT(l,r,22);
D_ENCRYPT(r,l,20);
D_ENCRYPT(l,r,18);
D_ENCRYPT(r,l,16);
D_ENCRYPT(l,r,14);
D_ENCRYPT(r,l,12);
D_ENCRYPT(l,r,10);
D_ENCRYPT(r,l, 8);
D_ENCRYPT(l,r, 6);
D_ENCRYPT(r,l, 4);
D_ENCRYPT(l,r, 2);
D_ENCRYPT(r,l, 0);
#else
for (i=30; i>0; i-=8)
{
D_ENCRYPT(l,r,i-0);
D_ENCRYPT(r,l,i-2);
D_ENCRYPT(l,r,i-4);
D_ENCRYPT(r,l,i-6);
}
#endif
}
l=ROTATE(l,3)&0xffffffffL;
r=ROTATE(r,3)&0xffffffffL;
FP(r,l);
data[0]=l;
data[1]=r;
l=r=t=u=0;
} | ['void des_string_to_2keys(const char *str, des_cblock *key1, des_cblock *key2)\n\t{\n\tdes_key_schedule ks;\n\tint i,length;\n\tregister unsigned char j;\n\tmemset(key1,0,8);\n\tmemset(key2,0,8);\n\tlength=strlen(str);\n#ifdef OLD_STR_TO_KEY\n\tif (length <= 8)\n\t\t{\n\t\tfor (i=0; i<length; i++)\n\t\t\t{\n\t\t\t(*key2)[i]=(*key1)[i]=(str[i]<<1);\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tfor (i=0; i<length; i++)\n\t\t\t{\n\t\t\tif ((i/8)&1)\n\t\t\t\t(*key2)[i%8]^=(str[i]<<1);\n\t\t\telse\n\t\t\t\t(*key1)[i%8]^=(str[i]<<1);\n\t\t\t}\n\t\t}\n#else\n\tfor (i=0; i<length; i++)\n\t\t{\n\t\tj=str[i];\n\t\tif ((i%32) < 16)\n\t\t\t{\n\t\t\tif ((i%16) < 8)\n\t\t\t\t(*key1)[i%8]^=(j<<1);\n\t\t\telse\n\t\t\t\t(*key2)[i%8]^=(j<<1);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tj=((j<<4)&0xf0)|((j>>4)&0x0f);\n\t\t\tj=((j<<2)&0xcc)|((j>>2)&0x33);\n\t\t\tj=((j<<1)&0xaa)|((j>>1)&0x55);\n\t\t\tif ((i%16) < 8)\n\t\t\t\t(*key1)[7-(i%8)]^=j;\n\t\t\telse\n\t\t\t\t(*key2)[7-(i%8)]^=j;\n\t\t\t}\n\t\t}\n\tif (length <= 8) memcpy(key2,key1,8);\n#endif\n\tdes_set_odd_parity(key1);\n\tdes_set_odd_parity(key2);\n\ti=des_check_key;\n\tdes_check_key=0;\n\tdes_set_key(key1,ks);\n\tdes_cbc_cksum((unsigned char*)str,key1,length,ks,key1);\n\tdes_set_key(key2,ks);\n\tdes_cbc_cksum((unsigned char*)str,key2,length,ks,key2);\n\tdes_check_key=i;\n\tmemset(ks,0,sizeof(ks));\n\tdes_set_odd_parity(key1);\n\tdes_set_odd_parity(key2);\n\t}', 'DES_LONG des_cbc_cksum(const unsigned char *in, des_cblock *output,\n\t\tlong length,\n\t\tdes_key_schedule schedule, const_des_cblock *ivec)\n\t{\n\tregister DES_LONG tout0,tout1,tin0,tin1;\n\tregister long l=length;\n\tDES_LONG tin[2];\n\tunsigned char *out = &(*output)[0];\n\tconst unsigned char *iv = &(*ivec)[0];\n\tc2l(iv,tout0);\n\tc2l(iv,tout1);\n\tfor (; l>0; l-=8)\n\t\t{\n\t\tif (l >= 8)\n\t\t\t{\n\t\t\tc2l(in,tin0);\n\t\t\tc2l(in,tin1);\n\t\t\t}\n\t\telse\n\t\t\tc2ln(in,tin0,tin1,l);\n\t\ttin0^=tout0; tin[0]=tin0;\n\t\ttin1^=tout1; tin[1]=tin1;\n\t\tdes_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);\n\t\ttout0=tin[0];\n\t\ttout1=tin[1];\n\t\t}\n\tif (out != NULL)\n\t\t{\n\t\tl2c(tout0,out);\n\t\tl2c(tout1,out);\n\t\t}\n\ttout0=tin0=tin1=tin[0]=tin[1]=0;\n\treturn(tout1);\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}'] |
582 | 0 | https://github.com/libav/libav/blob/f62c025a5ee898cdaed8f48d2c0ddfd89ac65f16/ffmpeg.c/#L3690 | static void opt_output_file(const char *filename)
{
AVFormatContext *oc;
int err, use_video, use_audio, use_subtitle;
int input_has_video, input_has_audio, input_has_subtitle;
AVFormatParameters params, *ap = ¶ms;
AVOutputFormat *file_oformat;
if (!strcmp(filename, "-"))
filename = "pipe:";
oc = avformat_alloc_context();
if (!oc) {
print_error(filename, AVERROR(ENOMEM));
av_exit(1);
}
if (last_asked_format) {
file_oformat = av_guess_format(last_asked_format, NULL, NULL);
if (!file_oformat) {
fprintf(stderr, "Requested output format '%s' is not a suitable output format\n", last_asked_format);
av_exit(1);
}
last_asked_format = NULL;
} else {
file_oformat = av_guess_format(NULL, filename, NULL);
if (!file_oformat) {
fprintf(stderr, "Unable to find a suitable output format for '%s'\n",
filename);
av_exit(1);
}
}
oc->oformat = file_oformat;
av_strlcpy(oc->filename, filename, sizeof(oc->filename));
if (!strcmp(file_oformat->name, "ffm") &&
av_strstart(filename, "http:", NULL)) {
int err = read_ffserver_streams(oc, filename);
if (err < 0) {
print_error(filename, err);
av_exit(1);
}
} else {
use_video = file_oformat->video_codec != CODEC_ID_NONE || video_stream_copy || video_codec_name;
use_audio = file_oformat->audio_codec != CODEC_ID_NONE || audio_stream_copy || audio_codec_name;
use_subtitle = file_oformat->subtitle_codec != CODEC_ID_NONE || subtitle_stream_copy || subtitle_codec_name;
if (nb_input_files > 0) {
check_audio_video_sub_inputs(&input_has_video, &input_has_audio,
&input_has_subtitle);
if (!input_has_video)
use_video = 0;
if (!input_has_audio)
use_audio = 0;
if (!input_has_subtitle)
use_subtitle = 0;
}
if (audio_disable) {
use_audio = 0;
}
if (video_disable) {
use_video = 0;
}
if (subtitle_disable) {
use_subtitle = 0;
}
if (use_video) {
new_video_stream(oc);
}
if (use_audio) {
new_audio_stream(oc);
}
if (use_subtitle) {
new_subtitle_stream(oc);
}
oc->timestamp = recording_timestamp;
for(; metadata_count>0; metadata_count--){
av_metadata_set2(&oc->metadata, metadata[metadata_count-1].key,
metadata[metadata_count-1].value, 0);
}
av_metadata_conv(oc, oc->oformat->metadata_conv, NULL);
}
output_files[nb_output_files++] = oc;
if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
if (!av_filename_number_test(oc->filename)) {
print_error(oc->filename, AVERROR_NUMEXPECTED);
av_exit(1);
}
}
if (!(oc->oformat->flags & AVFMT_NOFILE)) {
if (!file_overwrite &&
(strchr(filename, ':') == NULL ||
filename[1] == ':' ||
av_strstart(filename, "file:", NULL))) {
if (url_exist(filename)) {
if (!using_stdin) {
fprintf(stderr,"File '%s' already exists. Overwrite ? [y/N] ", filename);
fflush(stderr);
if (!read_yesno()) {
fprintf(stderr, "Not overwriting - exiting\n");
av_exit(1);
}
}
else {
fprintf(stderr,"File '%s' already exists. Exiting.\n", filename);
av_exit(1);
}
}
}
if ((err = url_fopen(&oc->pb, filename, URL_WRONLY)) < 0) {
print_error(filename, err);
av_exit(1);
}
}
memset(ap, 0, sizeof(*ap));
if (av_set_parameters(oc, ap) < 0) {
fprintf(stderr, "%s: Invalid encoding parameters\n",
oc->filename);
av_exit(1);
}
oc->preload= (int)(mux_preload*AV_TIME_BASE);
oc->max_delay= (int)(mux_max_delay*AV_TIME_BASE);
oc->loop_output = loop_output;
oc->flags |= AVFMT_FLAG_NONBLOCK;
set_context_opts(oc, avformat_opts, AV_OPT_FLAG_ENCODING_PARAM);
memset(streamid_map, 0, sizeof(streamid_map));
} | ['static void opt_output_file(const char *filename)\n{\n AVFormatContext *oc;\n int err, use_video, use_audio, use_subtitle;\n int input_has_video, input_has_audio, input_has_subtitle;\n AVFormatParameters params, *ap = ¶ms;\n AVOutputFormat *file_oformat;\n if (!strcmp(filename, "-"))\n filename = "pipe:";\n oc = avformat_alloc_context();\n if (!oc) {\n print_error(filename, AVERROR(ENOMEM));\n av_exit(1);\n }\n if (last_asked_format) {\n file_oformat = av_guess_format(last_asked_format, NULL, NULL);\n if (!file_oformat) {\n fprintf(stderr, "Requested output format \'%s\' is not a suitable output format\\n", last_asked_format);\n av_exit(1);\n }\n last_asked_format = NULL;\n } else {\n file_oformat = av_guess_format(NULL, filename, NULL);\n if (!file_oformat) {\n fprintf(stderr, "Unable to find a suitable output format for \'%s\'\\n",\n filename);\n av_exit(1);\n }\n }\n oc->oformat = file_oformat;\n av_strlcpy(oc->filename, filename, sizeof(oc->filename));\n if (!strcmp(file_oformat->name, "ffm") &&\n av_strstart(filename, "http:", NULL)) {\n int err = read_ffserver_streams(oc, filename);\n if (err < 0) {\n print_error(filename, err);\n av_exit(1);\n }\n } else {\n use_video = file_oformat->video_codec != CODEC_ID_NONE || video_stream_copy || video_codec_name;\n use_audio = file_oformat->audio_codec != CODEC_ID_NONE || audio_stream_copy || audio_codec_name;\n use_subtitle = file_oformat->subtitle_codec != CODEC_ID_NONE || subtitle_stream_copy || subtitle_codec_name;\n if (nb_input_files > 0) {\n check_audio_video_sub_inputs(&input_has_video, &input_has_audio,\n &input_has_subtitle);\n if (!input_has_video)\n use_video = 0;\n if (!input_has_audio)\n use_audio = 0;\n if (!input_has_subtitle)\n use_subtitle = 0;\n }\n if (audio_disable) {\n use_audio = 0;\n }\n if (video_disable) {\n use_video = 0;\n }\n if (subtitle_disable) {\n use_subtitle = 0;\n }\n if (use_video) {\n new_video_stream(oc);\n }\n if (use_audio) {\n new_audio_stream(oc);\n }\n if (use_subtitle) {\n new_subtitle_stream(oc);\n }\n oc->timestamp = recording_timestamp;\n for(; metadata_count>0; metadata_count--){\n av_metadata_set2(&oc->metadata, metadata[metadata_count-1].key,\n metadata[metadata_count-1].value, 0);\n }\n av_metadata_conv(oc, oc->oformat->metadata_conv, NULL);\n }\n output_files[nb_output_files++] = oc;\n if (oc->oformat->flags & AVFMT_NEEDNUMBER) {\n if (!av_filename_number_test(oc->filename)) {\n print_error(oc->filename, AVERROR_NUMEXPECTED);\n av_exit(1);\n }\n }\n if (!(oc->oformat->flags & AVFMT_NOFILE)) {\n if (!file_overwrite &&\n (strchr(filename, \':\') == NULL ||\n filename[1] == \':\' ||\n av_strstart(filename, "file:", NULL))) {\n if (url_exist(filename)) {\n if (!using_stdin) {\n fprintf(stderr,"File \'%s\' already exists. Overwrite ? [y/N] ", filename);\n fflush(stderr);\n if (!read_yesno()) {\n fprintf(stderr, "Not overwriting - exiting\\n");\n av_exit(1);\n }\n }\n else {\n fprintf(stderr,"File \'%s\' already exists. Exiting.\\n", filename);\n av_exit(1);\n }\n }\n }\n if ((err = url_fopen(&oc->pb, filename, URL_WRONLY)) < 0) {\n print_error(filename, err);\n av_exit(1);\n }\n }\n memset(ap, 0, sizeof(*ap));\n if (av_set_parameters(oc, ap) < 0) {\n fprintf(stderr, "%s: Invalid encoding parameters\\n",\n oc->filename);\n av_exit(1);\n }\n oc->preload= (int)(mux_preload*AV_TIME_BASE);\n oc->max_delay= (int)(mux_max_delay*AV_TIME_BASE);\n oc->loop_output = loop_output;\n oc->flags |= AVFMT_FLAG_NONBLOCK;\n set_context_opts(oc, avformat_opts, AV_OPT_FLAG_ENCODING_PARAM);\n memset(streamid_map, 0, sizeof(streamid_map));\n}', 'AVFormatContext *avformat_alloc_context(void)\n{\n AVFormatContext *ic;\n ic = av_malloc(sizeof(AVFormatContext));\n if (!ic) return ic;\n avformat_get_context_defaults(ic);\n ic->av_class = &av_format_context_class;\n return ic;\n}', 'void *av_malloc(unsigned int size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-16) )\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size+16);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&15) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr,16,size))\n ptr = NULL;\n#elif HAVE_MEMALIGN\n ptr = memalign(16,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}', 'void print_error(const char *filename, int err)\n{\n char errbuf[128];\n const char *errbuf_ptr = errbuf;\n if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)\n errbuf_ptr = strerror(AVUNERROR(err));\n fprintf(stderr, "%s: %s\\n", filename, errbuf_ptr);\n}', 'int av_strerror(int errnum, char *errbuf, size_t errbuf_size)\n{\n int ret = 0;\n const char *errstr = NULL;\n switch (errnum) {\n case AVERROR_EOF: errstr = "End of file"; break;\n case AVERROR_INVALIDDATA: errstr = "Invalid data found when processing input"; break;\n case AVERROR_NUMEXPECTED: errstr = "Number syntax expected in filename"; break;\n case AVERROR_PATCHWELCOME: errstr = "Not yet implemented in FFmpeg, patches welcome"; break;\n }\n if (errstr) {\n av_strlcpy(errbuf, errstr, errbuf_size);\n } else {\n#if HAVE_STRERROR_R\n ret = strerror_r(AVUNERROR(errnum), errbuf, errbuf_size);\n#else\n ret = -1;\n#endif\n if (ret < 0)\n snprintf(errbuf, errbuf_size, "Error number %d occurred", errnum);\n }\n return ret;\n}'] |
583 | 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 dsa_do_verify(const unsigned char *dgst, int dgst_len,\n DSA_SIG *sig, DSA *dsa)\n{\n BN_CTX *ctx;\n BIGNUM *u1, *u2, *t1;\n BN_MONT_CTX *mont = NULL;\n int ret = -1, i;\n if (!dsa->p || !dsa->q || !dsa->g) {\n DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_MISSING_PARAMETERS);\n return -1;\n }\n i = BN_num_bits(dsa->q);\n if (i != 160 && i != 224 && i != 256) {\n DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_BAD_Q_VALUE);\n return -1;\n }\n if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) {\n DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_MODULUS_TOO_LARGE);\n return -1;\n }\n u1 = BN_new();\n u2 = BN_new();\n t1 = BN_new();\n ctx = BN_CTX_new();\n if (!u1 || !u2 || !t1 || !ctx)\n goto err;\n if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||\n BN_ucmp(sig->r, dsa->q) >= 0) {\n ret = 0;\n goto err;\n }\n if (BN_is_zero(sig->s) || BN_is_negative(sig->s) ||\n BN_ucmp(sig->s, dsa->q) >= 0) {\n ret = 0;\n goto err;\n }\n if ((BN_mod_inverse(u2, sig->s, dsa->q, ctx)) == NULL)\n goto err;\n if (dgst_len > (i >> 3))\n dgst_len = (i >> 3);\n if (BN_bin2bn(dgst, dgst_len, u1) == NULL)\n goto err;\n if (!BN_mod_mul(u1, u1, u2, dsa->q, ctx))\n goto err;\n if (!BN_mod_mul(u2, sig->r, u2, dsa->q, ctx))\n goto err;\n if (dsa->flags & DSA_FLAG_CACHE_MONT_P) {\n mont = BN_MONT_CTX_set_locked(&dsa->method_mont_p,\n CRYPTO_LOCK_DSA, dsa->p, ctx);\n if (!mont)\n goto err;\n }\n DSA_MOD_EXP(goto err, dsa, t1, dsa->g, u1, dsa->pub_key, u2, dsa->p, ctx,\n mont);\n if (!BN_mod(u1, t1, dsa->q, ctx))\n goto err;\n ret = (BN_ucmp(u1, sig->r) == 0);\n err:\n if (ret < 0)\n DSAerr(DSA_F_DSA_DO_VERIFY, ERR_R_BN_LIB);\n BN_CTX_free(ctx);\n BN_free(u1);\n BN_free(u2);\n BN_free(t1);\n return (ret);\n}', 'BIGNUM *BN_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n{\n BIGNUM *rv;\n int noinv;\n rv = int_bn_mod_inverse(in, a, n, ctx, &noinv);\n if (noinv)\n BNerr(BN_F_BN_MOD_INVERSE, BN_R_NO_INVERSE);\n return rv;\n}', 'BIGNUM *int_bn_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,\n int *pnoinv)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM *ret = NULL;\n int sign;\n if (pnoinv)\n *pnoinv = 0;\n if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {\n return BN_mod_inverse_no_branch(in, a, n, ctx);\n }\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n if (!BN_nnmod(B, B, A, ctx))\n goto err;\n }\n sign = -1;\n if (BN_is_odd(n) && (BN_num_bits(n) <= (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}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_start", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG_EXIT(ctx);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,\n BN_CTX *ctx)\n{\n BIGNUM *t;\n int ret = 0;\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(m);\n BN_CTX_start(ctx);\n if ((t = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (a == b) {\n if (!BN_sqr(t, a, ctx))\n goto err;\n } else {\n if (!BN_mul(t, a, b, ctx))\n goto err;\n }\n if (!BN_nnmod(r, t, m, ctx))\n goto err;\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return (ret);\n}', 'int BN_mod_exp2_mont(BIGNUM *rr, const BIGNUM *a1, const BIGNUM *p1,\n const BIGNUM *a2, const BIGNUM *p2, const BIGNUM *m,\n BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n int i, j, bits, b, bits1, bits2, ret =\n 0, wpos1, wpos2, window1, window2, wvalue1, wvalue2;\n int r_is_one = 1;\n BIGNUM *d, *r;\n const BIGNUM *a_mod_m;\n BIGNUM *val1[TABLE_SIZE], *val2[TABLE_SIZE];\n BN_MONT_CTX *mont = NULL;\n bn_check_top(a1);\n bn_check_top(p1);\n bn_check_top(a2);\n bn_check_top(p2);\n bn_check_top(m);\n if (!(m->d[0] & 1)) {\n BNerr(BN_F_BN_MOD_EXP2_MONT, BN_R_CALLED_WITH_EVEN_MODULUS);\n return (0);\n }\n bits1 = BN_num_bits(p1);\n bits2 = BN_num_bits(p2);\n if ((bits1 == 0) && (bits2 == 0)) {\n ret = BN_one(rr);\n return ret;\n }\n bits = (bits1 > bits2) ? bits1 : bits2;\n BN_CTX_start(ctx);\n d = BN_CTX_get(ctx);\n r = BN_CTX_get(ctx);\n val1[0] = BN_CTX_get(ctx);\n val2[0] = BN_CTX_get(ctx);\n if (!d || !r || !val1[0] || !val2[0])\n goto err;\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n window1 = BN_window_bits_for_exponent_size(bits1);\n window2 = BN_window_bits_for_exponent_size(bits2);\n if (a1->neg || BN_ucmp(a1, m) >= 0) {\n if (!BN_mod(val1[0], a1, m, ctx))\n goto err;\n a_mod_m = val1[0];\n } else\n a_mod_m = a1;\n if (BN_is_zero(a_mod_m)) {\n BN_zero(rr);\n ret = 1;\n goto err;\n }\n if (!BN_to_montgomery(val1[0], a_mod_m, mont, ctx))\n goto err;\n if (window1 > 1) {\n if (!BN_mod_mul_montgomery(d, val1[0], val1[0], mont, ctx))\n goto err;\n j = 1 << (window1 - 1);\n for (i = 1; i < j; i++) {\n if (((val1[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul_montgomery(val1[i], val1[i - 1], d, mont, ctx))\n goto err;\n }\n }\n if (a2->neg || BN_ucmp(a2, m) >= 0) {\n if (!BN_mod(val2[0], a2, m, ctx))\n goto err;\n a_mod_m = val2[0];\n } else\n a_mod_m = a2;\n if (BN_is_zero(a_mod_m)) {\n BN_zero(rr);\n ret = 1;\n goto err;\n }\n if (!BN_to_montgomery(val2[0], a_mod_m, mont, ctx))\n goto err;\n if (window2 > 1) {\n if (!BN_mod_mul_montgomery(d, val2[0], val2[0], mont, ctx))\n goto err;\n j = 1 << (window2 - 1);\n for (i = 1; i < j; i++) {\n if (((val2[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul_montgomery(val2[i], val2[i - 1], d, mont, ctx))\n goto err;\n }\n }\n r_is_one = 1;\n wvalue1 = 0;\n wvalue2 = 0;\n wpos1 = 0;\n wpos2 = 0;\n if (!BN_to_montgomery(r, BN_value_one(), mont, ctx))\n goto err;\n for (b = bits - 1; b >= 0; b--) {\n if (!r_is_one) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (!wvalue1)\n if (BN_is_bit_set(p1, b)) {\n i = b - window1 + 1;\n while (!BN_is_bit_set(p1, i))\n i++;\n wpos1 = i;\n wvalue1 = 1;\n for (i = b - 1; i >= wpos1; i--) {\n wvalue1 <<= 1;\n if (BN_is_bit_set(p1, i))\n wvalue1++;\n }\n }\n if (!wvalue2)\n if (BN_is_bit_set(p2, b)) {\n i = b - window2 + 1;\n while (!BN_is_bit_set(p2, i))\n i++;\n wpos2 = i;\n wvalue2 = 1;\n for (i = b - 1; i >= wpos2; i--) {\n wvalue2 <<= 1;\n if (BN_is_bit_set(p2, i))\n wvalue2++;\n }\n }\n if (wvalue1 && b == wpos1) {\n if (!BN_mod_mul_montgomery(r, r, val1[wvalue1 >> 1], mont, ctx))\n goto err;\n wvalue1 = 0;\n r_is_one = 0;\n }\n if (wvalue2 && b == wpos2) {\n if (!BN_mod_mul_montgomery(r, r, val2[wvalue2 >> 1], mont, ctx))\n goto err;\n wvalue2 = 0;\n r_is_one = 0;\n }\n }\n if (!BN_from_montgomery(rr, r, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n BN_CTX_end(ctx);\n bn_check_top(rr);\n return (ret);\n}', 'int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)\n{\n int ret = 0;\n BIGNUM *Ri, *R;\n if (BN_is_zero(mod))\n return 0;\n BN_CTX_start(ctx);\n if ((Ri = BN_CTX_get(ctx)) == NULL)\n goto err;\n R = &(mont->RR);\n if (!BN_copy(&(mont->N), mod))\n goto err;\n mont->N.neg = 0;\n#ifdef MONT_WORD\n {\n BIGNUM tmod;\n BN_ULONG buf[2];\n BN_init(&tmod);\n tmod.d = buf;\n tmod.dmax = 2;\n tmod.neg = 0;\n mont->ri = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2;\n# if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)\n BN_zero(R);\n if (!(BN_set_bit(R, 2 * BN_BITS2)))\n goto err;\n tmod.top = 0;\n if ((buf[0] = mod->d[0]))\n tmod.top = 1;\n if ((buf[1] = mod->top > 1 ? mod->d[1] : 0))\n tmod.top = 2;\n if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, 2 * BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (bn_expand(Ri, (int)sizeof(BN_ULONG) * 2) == NULL)\n goto err;\n Ri->neg = 0;\n Ri->d[0] = BN_MASK2;\n Ri->d[1] = BN_MASK2;\n Ri->top = 2;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;\n# else\n BN_zero(R);\n if (!(BN_set_bit(R, BN_BITS2)))\n goto err;\n buf[0] = mod->d[0];\n buf[1] = 0;\n tmod.top = buf[0] != 0 ? 1 : 0;\n if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (!BN_set_word(Ri, BN_MASK2))\n goto err;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = 0;\n# endif\n }\n#else\n {\n mont->ri = BN_num_bits(&mont->N);\n BN_zero(R);\n if (!BN_set_bit(R, mont->ri))\n goto err;\n if ((BN_mod_inverse(Ri, R, &mont->N, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, mont->ri))\n goto err;\n if (!BN_sub_word(Ri, 1))\n goto err;\n if (!BN_div(&(mont->Ni), NULL, Ri, &mont->N, ctx))\n goto err;\n }\n#endif\n BN_zero(&(mont->RR));\n if (!BN_set_bit(&(mont->RR), mont->ri * 2))\n goto err;\n if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx))\n goto err;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', '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}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}'] |
584 | 0 | https://github.com/openssl/openssl/blob/e7d961e994620dd5dee6d80794a07fb9de1bab66/ssl/packet.c/#L48 | int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
{
if (!ossl_assert(pkt->subs != NULL && len != 0))
return 0;
if (pkt->maxsize - pkt->written < len)
return 0;
if (pkt->staticbuf == NULL && (pkt->buf->length - pkt->written < len)) {
size_t newlen;
size_t reflen;
reflen = (len > pkt->buf->length) ? len : pkt->buf->length;
if (reflen > SIZE_MAX / 2) {
newlen = SIZE_MAX;
} else {
newlen = reflen * 2;
if (newlen < DEFAULT_BUF_SIZE)
newlen = DEFAULT_BUF_SIZE;
}
if (BUF_MEM_grow(pkt->buf, newlen) == 0)
return 0;
}
if (allocbytes != NULL)
*allocbytes = WPACKET_get_curr(pkt);
return 1;
} | ['EXT_RETURN tls_construct_stoc_maxfragmentlen(SSL *s, WPACKET *pkt,\n unsigned int context, X509 *x,\n size_t chainidx)\n{\n if (!USE_MAX_FRAGMENT_LENGTH_EXT(s->session))\n return EXT_RETURN_NOT_SENT;\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_max_fragment_length)\n || !WPACKET_start_sub_packet_u16(pkt)\n || !WPACKET_put_bytes_u8(pkt, s->session->ext.max_fragment_len_mode)\n || !WPACKET_close(pkt)) {\n SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n SSL_F_TLS_CONSTRUCT_STOC_MAXFRAGMENTLEN, ERR_R_INTERNAL_ERROR);\n return EXT_RETURN_FAIL;\n }\n return EXT_RETURN_SENT;\n}', 'int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)\n{\n WPACKET_SUB *sub;\n unsigned char *lenchars;\n if (!ossl_assert(pkt->subs != NULL))\n return 0;\n sub = OPENSSL_zalloc(sizeof(*sub));\n if (sub == NULL)\n return 0;\n sub->parent = pkt->subs;\n pkt->subs = sub;\n sub->pwritten = pkt->written + lenbytes;\n sub->lenbytes = lenbytes;\n if (lenbytes == 0) {\n sub->packet_len = 0;\n return 1;\n }\n if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))\n return 0;\n sub->packet_len = lenchars - GETBUF(pkt);\n return 1;\n}', 'int WPACKET_put_bytes__(WPACKET *pkt, unsigned int val, size_t size)\n{\n unsigned char *data;\n if (!ossl_assert(size <= sizeof(unsigned int))\n || !WPACKET_allocate_bytes(pkt, size, &data)\n || !put_value(data, val, size))\n return 0;\n return 1;\n}', 'int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)\n{\n if (!WPACKET_reserve_bytes(pkt, len, allocbytes))\n return 0;\n pkt->written += len;\n pkt->curr += len;\n return 1;\n}', 'int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)\n{\n if (!ossl_assert(pkt->subs != NULL && len != 0))\n return 0;\n if (pkt->maxsize - pkt->written < len)\n return 0;\n if (pkt->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}'] |
585 | 0 | https://github.com/libav/libav/blob/9104cd5161ec7cb31361f3dabd73a8a813d4f7d0/libavcodec/h264.h/#L1081 | static av_always_inline int fill_caches(H264Context *h, int mb_type, int for_deblock){
MpegEncContext * const s = &h->s;
const int mb_xy= h->mb_xy;
int topleft_xy, top_xy, topright_xy, left_xy[2];
int topleft_type, top_type, topright_type, left_type[2];
const uint8_t * left_block;
int topleft_partition= -1;
int i;
static const uint8_t left_block_options[4][16]={
{0,1,2,3,7,10,8,11,7+0*8, 7+1*8, 7+2*8, 7+3*8, 2+0*8, 2+3*8, 2+1*8, 2+2*8},
{2,2,3,3,8,11,8,11,7+2*8, 7+2*8, 7+3*8, 7+3*8, 2+1*8, 2+2*8, 2+1*8, 2+2*8},
{0,0,1,1,7,10,7,10,7+0*8, 7+0*8, 7+1*8, 7+1*8, 2+0*8, 2+3*8, 2+0*8, 2+3*8},
{0,2,0,2,7,10,7,10,7+0*8, 7+2*8, 7+0*8, 7+2*8, 2+0*8, 2+3*8, 2+0*8, 2+3*8}
};
top_xy = mb_xy - (s->mb_stride << FIELD_PICTURE);
topleft_xy = top_xy - 1;
topright_xy= top_xy + 1;
left_xy[1] = left_xy[0] = mb_xy-1;
left_block = left_block_options[0];
if(FRAME_MBAFF){
const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
const int top_pair_xy = pair_xy - s->mb_stride;
const int topleft_pair_xy = top_pair_xy - 1;
const int topright_pair_xy = top_pair_xy + 1;
const int topleft_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[topleft_pair_xy]);
const int top_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);
const int topright_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[topright_pair_xy]);
const int left_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);
const int curr_mb_field_flag = IS_INTERLACED(mb_type);
const int bottom = (s->mb_y & 1);
tprintf(s->avctx, "fill_caches: curr_mb_field_flag:%d, left_mb_field_flag:%d, topleft_mb_field_flag:%d, top_mb_field_flag:%d, topright_mb_field_flag:%d\n", curr_mb_field_flag, left_mb_field_flag, topleft_mb_field_flag, top_mb_field_flag, topright_mb_field_flag);
if (curr_mb_field_flag && (bottom || top_mb_field_flag)){
top_xy -= s->mb_stride;
}
if (curr_mb_field_flag && (bottom || topleft_mb_field_flag)){
topleft_xy -= s->mb_stride;
} else if(bottom && !curr_mb_field_flag && left_mb_field_flag) {
topleft_xy += s->mb_stride;
topleft_partition = 0;
}
if (curr_mb_field_flag && (bottom || topright_mb_field_flag)){
topright_xy -= s->mb_stride;
}
if (left_mb_field_flag != curr_mb_field_flag) {
left_xy[1] = left_xy[0] = pair_xy - 1;
if (curr_mb_field_flag) {
left_xy[1] += s->mb_stride;
left_block = left_block_options[3];
} else {
left_block= left_block_options[2 - bottom];
}
}
}
h->top_mb_xy = top_xy;
h->left_mb_xy[0] = left_xy[0];
h->left_mb_xy[1] = left_xy[1];
if(for_deblock){
int qp_thresh = h->qp_thresh;
int qp = s->current_picture.qscale_table[mb_xy];
if(qp <= qp_thresh
&& (left_xy[0]<0 || ((qp + s->current_picture.qscale_table[left_xy[0]] + 1)>>1) <= qp_thresh)
&& (left_xy[1]<0 || ((qp + s->current_picture.qscale_table[left_xy[1]] + 1)>>1) <= qp_thresh)
&& (top_xy < 0 || ((qp + s->current_picture.qscale_table[top_xy ] + 1)>>1) <= qp_thresh)){
return 1;
}
if(IS_INTRA(mb_type))
return 0;
AV_COPY64(&h->non_zero_count_cache[0+8*1], &h->non_zero_count[mb_xy][ 0]);
AV_COPY64(&h->non_zero_count_cache[0+8*2], &h->non_zero_count[mb_xy][ 8]);
*((uint32_t*)&h->non_zero_count_cache[0+8*5])= *((uint32_t*)&h->non_zero_count[mb_xy][16]);
*((uint32_t*)&h->non_zero_count_cache[4+8*3])= *((uint32_t*)&h->non_zero_count[mb_xy][20]);
AV_COPY64(&h->non_zero_count_cache[0+8*4], &h->non_zero_count[mb_xy][24]);
h->cbp= h->cbp_table[mb_xy];
top_type = h->slice_table[top_xy ] < 0xFFFF ? s->current_picture.mb_type[top_xy] : 0;
left_type[0] = h->slice_table[left_xy[0] ] < 0xFFFF ? s->current_picture.mb_type[left_xy[0]] : 0;
left_type[1] = h->slice_table[left_xy[1] ] < 0xFFFF ? s->current_picture.mb_type[left_xy[1]] : 0;
{
int list;
for(list=0; list<h->list_count; list++){
int8_t *ref;
int y, b_stride;
int16_t (*mv_dst)[2];
int16_t (*mv_src)[2];
if(!USES_LIST(mb_type, list)){
fill_rectangle( h->mv_cache[list][scan8[0]], 4, 4, 8, pack16to32(0,0), 4);
*(uint32_t*)&h->ref_cache[list][scan8[ 0]] =
*(uint32_t*)&h->ref_cache[list][scan8[ 2]] =
*(uint32_t*)&h->ref_cache[list][scan8[ 8]] =
*(uint32_t*)&h->ref_cache[list][scan8[10]] = ((LIST_NOT_USED)&0xFF)*0x01010101;
continue;
}
ref = &s->current_picture.ref_index[list][h->mb2b8_xy[mb_xy]];
{
int (*ref2frm)[64] = h->ref2frm[ h->slice_num&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
*(uint32_t*)&h->ref_cache[list][scan8[ 0]] =
*(uint32_t*)&h->ref_cache[list][scan8[ 2]] = (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101;
ref += h->b8_stride;
*(uint32_t*)&h->ref_cache[list][scan8[ 8]] =
*(uint32_t*)&h->ref_cache[list][scan8[10]] = (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101;
}
b_stride = h->b_stride;
mv_dst = &h->mv_cache[list][scan8[0]];
mv_src = &s->current_picture.motion_val[list][4*s->mb_x + 4*s->mb_y*b_stride];
for(y=0; y<4; y++){
AV_COPY128(mv_dst + 8*y, mv_src + y*b_stride);
}
}
}
}else{
topleft_type = h->slice_table[topleft_xy ] == h->slice_num ? s->current_picture.mb_type[topleft_xy] : 0;
top_type = h->slice_table[top_xy ] == h->slice_num ? s->current_picture.mb_type[top_xy] : 0;
topright_type= h->slice_table[topright_xy] == h->slice_num ? s->current_picture.mb_type[topright_xy]: 0;
left_type[0] = h->slice_table[left_xy[0] ] == h->slice_num ? s->current_picture.mb_type[left_xy[0]] : 0;
left_type[1] = h->slice_table[left_xy[1] ] == h->slice_num ? s->current_picture.mb_type[left_xy[1]] : 0;
if(IS_INTRA(mb_type)){
int type_mask= h->pps.constrained_intra_pred ? IS_INTRA(-1) : -1;
h->topleft_samples_available=
h->top_samples_available=
h->left_samples_available= 0xFFFF;
h->topright_samples_available= 0xEEEA;
if(!(top_type & type_mask)){
h->topleft_samples_available= 0xB3FF;
h->top_samples_available= 0x33FF;
h->topright_samples_available= 0x26EA;
}
if(IS_INTERLACED(mb_type) != IS_INTERLACED(left_type[0])){
if(IS_INTERLACED(mb_type)){
if(!(left_type[0] & type_mask)){
h->topleft_samples_available&= 0xDFFF;
h->left_samples_available&= 0x5FFF;
}
if(!(left_type[1] & type_mask)){
h->topleft_samples_available&= 0xFF5F;
h->left_samples_available&= 0xFF5F;
}
}else{
int left_typei = h->slice_table[left_xy[0] + s->mb_stride ] == h->slice_num
? s->current_picture.mb_type[left_xy[0] + s->mb_stride] : 0;
assert(left_xy[0] == left_xy[1]);
if(!((left_typei & type_mask) && (left_type[0] & type_mask))){
h->topleft_samples_available&= 0xDF5F;
h->left_samples_available&= 0x5F5F;
}
}
}else{
if(!(left_type[0] & type_mask)){
h->topleft_samples_available&= 0xDF5F;
h->left_samples_available&= 0x5F5F;
}
}
if(!(topleft_type & type_mask))
h->topleft_samples_available&= 0x7FFF;
if(!(topright_type & type_mask))
h->topright_samples_available&= 0xFBFF;
if(IS_INTRA4x4(mb_type)){
if(IS_INTRA4x4(top_type)){
h->intra4x4_pred_mode_cache[4+8*0]= h->intra4x4_pred_mode[top_xy][4];
h->intra4x4_pred_mode_cache[5+8*0]= h->intra4x4_pred_mode[top_xy][5];
h->intra4x4_pred_mode_cache[6+8*0]= h->intra4x4_pred_mode[top_xy][6];
h->intra4x4_pred_mode_cache[7+8*0]= h->intra4x4_pred_mode[top_xy][3];
}else{
int pred;
if(!(top_type & type_mask))
pred= -1;
else{
pred= 2;
}
h->intra4x4_pred_mode_cache[4+8*0]=
h->intra4x4_pred_mode_cache[5+8*0]=
h->intra4x4_pred_mode_cache[6+8*0]=
h->intra4x4_pred_mode_cache[7+8*0]= pred;
}
for(i=0; i<2; i++){
if(IS_INTRA4x4(left_type[i])){
h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[0+2*i]];
h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[1+2*i]];
}else{
int pred;
if(!(left_type[i] & type_mask))
pred= -1;
else{
pred= 2;
}
h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=
h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= pred;
}
}
}
}
}
if(top_type){
*(uint32_t*)&h->non_zero_count_cache[4+8*0]= *(uint32_t*)&h->non_zero_count[top_xy][4+3*8];
if(!for_deblock){
h->non_zero_count_cache[1+8*0]= h->non_zero_count[top_xy][1+1*8];
h->non_zero_count_cache[2+8*0]= h->non_zero_count[top_xy][2+1*8];
h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][1+2*8];
h->non_zero_count_cache[2+8*3]= h->non_zero_count[top_xy][2+2*8];
}
}else if(!for_deblock){
h->non_zero_count_cache[1+8*0]=
h->non_zero_count_cache[2+8*0]=
h->non_zero_count_cache[1+8*3]=
h->non_zero_count_cache[2+8*3]=
*(uint32_t*)&h->non_zero_count_cache[4+8*0]= CABAC && !IS_INTRA(mb_type) ? 0 : 0x40404040;
}
for (i=0; i<2; i++) {
if(left_type[i]){
h->non_zero_count_cache[3+8*1 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[8+0+2*i]];
h->non_zero_count_cache[3+8*2 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[8+1+2*i]];
if(!for_deblock){
h->non_zero_count_cache[0+8*1 + 8*i]= h->non_zero_count[left_xy[i]][left_block[8+4+2*i]];
h->non_zero_count_cache[0+8*4 + 8*i]= h->non_zero_count[left_xy[i]][left_block[8+5+2*i]];
}
}else if(!for_deblock){
h->non_zero_count_cache[3+8*1 + 2*8*i]=
h->non_zero_count_cache[3+8*2 + 2*8*i]=
h->non_zero_count_cache[0+8*1 + 8*i]=
h->non_zero_count_cache[0+8*4 + 8*i]= CABAC && !IS_INTRA(mb_type) ? 0 : 64;
}
}
if(for_deblock && !CABAC && h->pps.transform_8x8_mode){
if(IS_8x8DCT(top_type)){
h->non_zero_count_cache[4+8*0]=
h->non_zero_count_cache[5+8*0]= h->cbp_table[top_xy] & 4;
h->non_zero_count_cache[6+8*0]=
h->non_zero_count_cache[7+8*0]= h->cbp_table[top_xy] & 8;
}
if(IS_8x8DCT(left_type[0])){
h->non_zero_count_cache[3+8*1]=
h->non_zero_count_cache[3+8*2]= h->cbp_table[left_xy[0]]&2;
}
if(IS_8x8DCT(left_type[1])){
h->non_zero_count_cache[3+8*3]=
h->non_zero_count_cache[3+8*4]= h->cbp_table[left_xy[1]]&8;
}
if(IS_8x8DCT(mb_type)){
h->non_zero_count_cache[scan8[0 ]]= h->non_zero_count_cache[scan8[1 ]]=
h->non_zero_count_cache[scan8[2 ]]= h->non_zero_count_cache[scan8[3 ]]= h->cbp & 1;
h->non_zero_count_cache[scan8[0+ 4]]= h->non_zero_count_cache[scan8[1+ 4]]=
h->non_zero_count_cache[scan8[2+ 4]]= h->non_zero_count_cache[scan8[3+ 4]]= h->cbp & 2;
h->non_zero_count_cache[scan8[0+ 8]]= h->non_zero_count_cache[scan8[1+ 8]]=
h->non_zero_count_cache[scan8[2+ 8]]= h->non_zero_count_cache[scan8[3+ 8]]= h->cbp & 4;
h->non_zero_count_cache[scan8[0+12]]= h->non_zero_count_cache[scan8[1+12]]=
h->non_zero_count_cache[scan8[2+12]]= h->non_zero_count_cache[scan8[3+12]]= h->cbp & 8;
}
}
if( CABAC && !for_deblock) {
if(top_type) {
h->top_cbp = h->cbp_table[top_xy];
} else if(IS_INTRA(mb_type)) {
h->top_cbp = 0x1C0;
} else {
h->top_cbp = 0;
}
if (left_type[0]) {
h->left_cbp = h->cbp_table[left_xy[0]] & 0x1f0;
} else if(IS_INTRA(mb_type)) {
h->left_cbp = 0x1C0;
} else {
h->left_cbp = 0;
}
if (left_type[0]) {
h->left_cbp |= ((h->cbp_table[left_xy[0]]>>((left_block[0]&(~1))+1))&0x1) << 1;
}
if (left_type[1]) {
h->left_cbp |= ((h->cbp_table[left_xy[1]]>>((left_block[2]&(~1))+1))&0x1) << 3;
}
}
#if 1
if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
int list;
for(list=0; list<h->list_count; list++){
if(!for_deblock && !USES_LIST(mb_type, list) && !IS_DIRECT(mb_type)){
continue;
}
h->mv_cache_clean[list]= 0;
if(USES_LIST(top_type, list)){
const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
const int b8_xy= h->mb2b8_xy[top_xy] + h->b8_stride;
AV_COPY128(h->mv_cache[list][scan8[0] + 0 - 1*8], s->current_picture.motion_val[list][b_xy + 0]);
if(for_deblock){
int (*ref2frm)[64] = h->ref2frm[ h->slice_table[top_xy]&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
h->ref_cache[list][scan8[0] + 0 - 1*8]=
h->ref_cache[list][scan8[0] + 1 - 1*8]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 0]];
h->ref_cache[list][scan8[0] + 2 - 1*8]=
h->ref_cache[list][scan8[0] + 3 - 1*8]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 1]];
}else{
h->ref_cache[list][scan8[0] + 0 - 1*8]=
h->ref_cache[list][scan8[0] + 1 - 1*8]= s->current_picture.ref_index[list][b8_xy + 0];
h->ref_cache[list][scan8[0] + 2 - 1*8]=
h->ref_cache[list][scan8[0] + 3 - 1*8]= s->current_picture.ref_index[list][b8_xy + 1];
}
}else{
AV_ZERO128(h->mv_cache[list][scan8[0] + 0 - 1*8]);
*(uint32_t*)&h->ref_cache[list][scan8[0] + 0 - 1*8]= (((for_deblock||top_type) ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101;
}
for(i=0; i<2; i++){
int cache_idx = scan8[0] - 1 + i*2*8;
if(USES_LIST(left_type[i], list)){
const int b_xy= h->mb2b_xy[left_xy[i]] + 3;
const int b8_xy= h->mb2b8_xy[left_xy[i]] + 1;
*(uint32_t*)h->mv_cache[list][cache_idx ]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[0+i*2]];
*(uint32_t*)h->mv_cache[list][cache_idx+8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[1+i*2]];
if(for_deblock){
int (*ref2frm)[64] = h->ref2frm[ h->slice_table[left_xy[i]]&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
h->ref_cache[list][cache_idx ]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[0+i*2]>>1)]];
h->ref_cache[list][cache_idx+8]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[1+i*2]>>1)]];
}else{
h->ref_cache[list][cache_idx ]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[0+i*2]>>1)];
h->ref_cache[list][cache_idx+8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[1+i*2]>>1)];
}
}else{
*(uint32_t*)h->mv_cache [list][cache_idx ]=
*(uint32_t*)h->mv_cache [list][cache_idx+8]= 0;
h->ref_cache[list][cache_idx ]=
h->ref_cache[list][cache_idx+8]= (for_deblock||left_type[i]) ? LIST_NOT_USED : PART_NOT_AVAILABLE;
}
}
if(for_deblock || ((IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred) && !FRAME_MBAFF))
continue;
if(USES_LIST(topleft_type, list)){
const int b_xy = h->mb2b_xy[topleft_xy] + 3 + h->b_stride + (topleft_partition & 2*h->b_stride);
const int b8_xy= h->mb2b8_xy[topleft_xy] + 1 + (topleft_partition & h->b8_stride);
*(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
h->ref_cache[list][scan8[0] - 1 - 1*8]= s->current_picture.ref_index[list][b8_xy];
}else{
*(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= 0;
h->ref_cache[list][scan8[0] - 1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
}
if(USES_LIST(topright_type, list)){
const int b_xy= h->mb2b_xy[topright_xy] + 3*h->b_stride;
const int b8_xy= h->mb2b8_xy[topright_xy] + h->b8_stride;
*(uint32_t*)h->mv_cache[list][scan8[0] + 4 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
h->ref_cache[list][scan8[0] + 4 - 1*8]= s->current_picture.ref_index[list][b8_xy];
}else{
*(uint32_t*)h->mv_cache [list][scan8[0] + 4 - 1*8]= 0;
h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
}
if((IS_SKIP(mb_type) || IS_DIRECT(mb_type)) && !FRAME_MBAFF)
continue;
h->ref_cache[list][scan8[5 ]+1] =
h->ref_cache[list][scan8[7 ]+1] =
h->ref_cache[list][scan8[13]+1] =
h->ref_cache[list][scan8[4 ]] =
h->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE;
*(uint32_t*)h->mv_cache [list][scan8[5 ]+1]=
*(uint32_t*)h->mv_cache [list][scan8[7 ]+1]=
*(uint32_t*)h->mv_cache [list][scan8[13]+1]=
*(uint32_t*)h->mv_cache [list][scan8[4 ]]=
*(uint32_t*)h->mv_cache [list][scan8[12]]= 0;
if( CABAC ) {
if(USES_LIST(top_type, list)){
const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
AV_COPY128(h->mvd_cache[list][scan8[0] + 0 - 1*8], h->mvd_table[list][b_xy + 0]);
}else{
AV_ZERO128(h->mvd_cache[list][scan8[0] + 0 - 1*8]);
}
if(USES_LIST(left_type[0], list)){
const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
*(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 0*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[0]];
*(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[1]];
}else{
*(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 0*8]=
*(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 1*8]= 0;
}
if(USES_LIST(left_type[1], list)){
const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
*(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 2*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[2]];
*(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 3*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[3]];
}else{
*(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 2*8]=
*(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 3*8]= 0;
}
*(uint32_t*)h->mvd_cache [list][scan8[5 ]+1]=
*(uint32_t*)h->mvd_cache [list][scan8[7 ]+1]=
*(uint32_t*)h->mvd_cache [list][scan8[13]+1]=
*(uint32_t*)h->mvd_cache [list][scan8[4 ]]=
*(uint32_t*)h->mvd_cache [list][scan8[12]]= 0;
if(h->slice_type_nos == FF_B_TYPE){
fill_rectangle(&h->direct_cache[scan8[0]], 4, 4, 8, 0, 1);
if(IS_DIRECT(top_type)){
*(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0x01010101;
}else if(IS_8X8(top_type)){
int b8_xy = h->mb2b8_xy[top_xy] + h->b8_stride;
h->direct_cache[scan8[0] + 0 - 1*8]= h->direct_table[b8_xy];
h->direct_cache[scan8[0] + 2 - 1*8]= h->direct_table[b8_xy + 1];
}else{
*(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0;
}
if(IS_DIRECT(left_type[0]))
h->direct_cache[scan8[0] - 1 + 0*8]= 1;
else if(IS_8X8(left_type[0]))
h->direct_cache[scan8[0] - 1 + 0*8]= h->direct_table[h->mb2b8_xy[left_xy[0]] + 1 + h->b8_stride*(left_block[0]>>1)];
else
h->direct_cache[scan8[0] - 1 + 0*8]= 0;
if(IS_DIRECT(left_type[1]))
h->direct_cache[scan8[0] - 1 + 2*8]= 1;
else if(IS_8X8(left_type[1]))
h->direct_cache[scan8[0] - 1 + 2*8]= h->direct_table[h->mb2b8_xy[left_xy[1]] + 1 + h->b8_stride*(left_block[2]>>1)];
else
h->direct_cache[scan8[0] - 1 + 2*8]= 0;
}
}
if(FRAME_MBAFF){
#define MAP_MVS\
MAP_F2F(scan8[0] - 1 - 1*8, topleft_type)\
MAP_F2F(scan8[0] + 0 - 1*8, top_type)\
MAP_F2F(scan8[0] + 1 - 1*8, top_type)\
MAP_F2F(scan8[0] + 2 - 1*8, top_type)\
MAP_F2F(scan8[0] + 3 - 1*8, top_type)\
MAP_F2F(scan8[0] + 4 - 1*8, topright_type)\
MAP_F2F(scan8[0] - 1 + 0*8, left_type[0])\
MAP_F2F(scan8[0] - 1 + 1*8, left_type[0])\
MAP_F2F(scan8[0] - 1 + 2*8, left_type[1])\
MAP_F2F(scan8[0] - 1 + 3*8, left_type[1])
if(MB_FIELD){
#define MAP_F2F(idx, mb_type)\
if(!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
h->ref_cache[list][idx] <<= 1;\
h->mv_cache[list][idx][1] /= 2;\
h->mvd_cache[list][idx][1] /= 2;\
}
MAP_MVS
#undef MAP_F2F
}else{
#define MAP_F2F(idx, mb_type)\
if(IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\
h->ref_cache[list][idx] >>= 1;\
h->mv_cache[list][idx][1] <<= 1;\
h->mvd_cache[list][idx][1] <<= 1;\
}
MAP_MVS
#undef MAP_F2F
}
}
}
}
#endif
if(!for_deblock)
h->neighbor_transform_size= !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[0]);
return 0;
} | ['static av_always_inline int fill_caches(H264Context *h, int mb_type, int for_deblock){\n MpegEncContext * const s = &h->s;\n const int mb_xy= h->mb_xy;\n int topleft_xy, top_xy, topright_xy, left_xy[2];\n int topleft_type, top_type, topright_type, left_type[2];\n const uint8_t * left_block;\n int topleft_partition= -1;\n int i;\n static const uint8_t left_block_options[4][16]={\n {0,1,2,3,7,10,8,11,7+0*8, 7+1*8, 7+2*8, 7+3*8, 2+0*8, 2+3*8, 2+1*8, 2+2*8},\n {2,2,3,3,8,11,8,11,7+2*8, 7+2*8, 7+3*8, 7+3*8, 2+1*8, 2+2*8, 2+1*8, 2+2*8},\n {0,0,1,1,7,10,7,10,7+0*8, 7+0*8, 7+1*8, 7+1*8, 2+0*8, 2+3*8, 2+0*8, 2+3*8},\n {0,2,0,2,7,10,7,10,7+0*8, 7+2*8, 7+0*8, 7+2*8, 2+0*8, 2+3*8, 2+0*8, 2+3*8}\n };\n top_xy = mb_xy - (s->mb_stride << FIELD_PICTURE);\n topleft_xy = top_xy - 1;\n topright_xy= top_xy + 1;\n left_xy[1] = left_xy[0] = mb_xy-1;\n left_block = left_block_options[0];\n if(FRAME_MBAFF){\n const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;\n const int top_pair_xy = pair_xy - s->mb_stride;\n const int topleft_pair_xy = top_pair_xy - 1;\n const int topright_pair_xy = top_pair_xy + 1;\n const int topleft_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[topleft_pair_xy]);\n const int top_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);\n const int topright_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[topright_pair_xy]);\n const int left_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);\n const int curr_mb_field_flag = IS_INTERLACED(mb_type);\n const int bottom = (s->mb_y & 1);\n tprintf(s->avctx, "fill_caches: curr_mb_field_flag:%d, left_mb_field_flag:%d, topleft_mb_field_flag:%d, top_mb_field_flag:%d, topright_mb_field_flag:%d\\n", curr_mb_field_flag, left_mb_field_flag, topleft_mb_field_flag, top_mb_field_flag, topright_mb_field_flag);\n if (curr_mb_field_flag && (bottom || top_mb_field_flag)){\n top_xy -= s->mb_stride;\n }\n if (curr_mb_field_flag && (bottom || topleft_mb_field_flag)){\n topleft_xy -= s->mb_stride;\n } else if(bottom && !curr_mb_field_flag && left_mb_field_flag) {\n topleft_xy += s->mb_stride;\n topleft_partition = 0;\n }\n if (curr_mb_field_flag && (bottom || topright_mb_field_flag)){\n topright_xy -= s->mb_stride;\n }\n if (left_mb_field_flag != curr_mb_field_flag) {\n left_xy[1] = left_xy[0] = pair_xy - 1;\n if (curr_mb_field_flag) {\n left_xy[1] += s->mb_stride;\n left_block = left_block_options[3];\n } else {\n left_block= left_block_options[2 - bottom];\n }\n }\n }\n h->top_mb_xy = top_xy;\n h->left_mb_xy[0] = left_xy[0];\n h->left_mb_xy[1] = left_xy[1];\n if(for_deblock){\n int qp_thresh = h->qp_thresh;\n int qp = s->current_picture.qscale_table[mb_xy];\n if(qp <= qp_thresh\n && (left_xy[0]<0 || ((qp + s->current_picture.qscale_table[left_xy[0]] + 1)>>1) <= qp_thresh)\n && (left_xy[1]<0 || ((qp + s->current_picture.qscale_table[left_xy[1]] + 1)>>1) <= qp_thresh)\n && (top_xy < 0 || ((qp + s->current_picture.qscale_table[top_xy ] + 1)>>1) <= qp_thresh)){\n return 1;\n }\n if(IS_INTRA(mb_type))\n return 0;\n AV_COPY64(&h->non_zero_count_cache[0+8*1], &h->non_zero_count[mb_xy][ 0]);\n AV_COPY64(&h->non_zero_count_cache[0+8*2], &h->non_zero_count[mb_xy][ 8]);\n *((uint32_t*)&h->non_zero_count_cache[0+8*5])= *((uint32_t*)&h->non_zero_count[mb_xy][16]);\n *((uint32_t*)&h->non_zero_count_cache[4+8*3])= *((uint32_t*)&h->non_zero_count[mb_xy][20]);\n AV_COPY64(&h->non_zero_count_cache[0+8*4], &h->non_zero_count[mb_xy][24]);\n h->cbp= h->cbp_table[mb_xy];\n top_type = h->slice_table[top_xy ] < 0xFFFF ? s->current_picture.mb_type[top_xy] : 0;\n left_type[0] = h->slice_table[left_xy[0] ] < 0xFFFF ? s->current_picture.mb_type[left_xy[0]] : 0;\n left_type[1] = h->slice_table[left_xy[1] ] < 0xFFFF ? s->current_picture.mb_type[left_xy[1]] : 0;\n {\n int list;\n for(list=0; list<h->list_count; list++){\n int8_t *ref;\n int y, b_stride;\n int16_t (*mv_dst)[2];\n int16_t (*mv_src)[2];\n if(!USES_LIST(mb_type, list)){\n fill_rectangle( h->mv_cache[list][scan8[0]], 4, 4, 8, pack16to32(0,0), 4);\n *(uint32_t*)&h->ref_cache[list][scan8[ 0]] =\n *(uint32_t*)&h->ref_cache[list][scan8[ 2]] =\n *(uint32_t*)&h->ref_cache[list][scan8[ 8]] =\n *(uint32_t*)&h->ref_cache[list][scan8[10]] = ((LIST_NOT_USED)&0xFF)*0x01010101;\n continue;\n }\n ref = &s->current_picture.ref_index[list][h->mb2b8_xy[mb_xy]];\n {\n int (*ref2frm)[64] = h->ref2frm[ h->slice_num&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);\n *(uint32_t*)&h->ref_cache[list][scan8[ 0]] =\n *(uint32_t*)&h->ref_cache[list][scan8[ 2]] = (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101;\n ref += h->b8_stride;\n *(uint32_t*)&h->ref_cache[list][scan8[ 8]] =\n *(uint32_t*)&h->ref_cache[list][scan8[10]] = (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101;\n }\n b_stride = h->b_stride;\n mv_dst = &h->mv_cache[list][scan8[0]];\n mv_src = &s->current_picture.motion_val[list][4*s->mb_x + 4*s->mb_y*b_stride];\n for(y=0; y<4; y++){\n AV_COPY128(mv_dst + 8*y, mv_src + y*b_stride);\n }\n }\n }\n }else{\n topleft_type = h->slice_table[topleft_xy ] == h->slice_num ? s->current_picture.mb_type[topleft_xy] : 0;\n top_type = h->slice_table[top_xy ] == h->slice_num ? s->current_picture.mb_type[top_xy] : 0;\n topright_type= h->slice_table[topright_xy] == h->slice_num ? s->current_picture.mb_type[topright_xy]: 0;\n left_type[0] = h->slice_table[left_xy[0] ] == h->slice_num ? s->current_picture.mb_type[left_xy[0]] : 0;\n left_type[1] = h->slice_table[left_xy[1] ] == h->slice_num ? s->current_picture.mb_type[left_xy[1]] : 0;\n if(IS_INTRA(mb_type)){\n int type_mask= h->pps.constrained_intra_pred ? IS_INTRA(-1) : -1;\n h->topleft_samples_available=\n h->top_samples_available=\n h->left_samples_available= 0xFFFF;\n h->topright_samples_available= 0xEEEA;\n if(!(top_type & type_mask)){\n h->topleft_samples_available= 0xB3FF;\n h->top_samples_available= 0x33FF;\n h->topright_samples_available= 0x26EA;\n }\n if(IS_INTERLACED(mb_type) != IS_INTERLACED(left_type[0])){\n if(IS_INTERLACED(mb_type)){\n if(!(left_type[0] & type_mask)){\n h->topleft_samples_available&= 0xDFFF;\n h->left_samples_available&= 0x5FFF;\n }\n if(!(left_type[1] & type_mask)){\n h->topleft_samples_available&= 0xFF5F;\n h->left_samples_available&= 0xFF5F;\n }\n }else{\n int left_typei = h->slice_table[left_xy[0] + s->mb_stride ] == h->slice_num\n ? s->current_picture.mb_type[left_xy[0] + s->mb_stride] : 0;\n assert(left_xy[0] == left_xy[1]);\n if(!((left_typei & type_mask) && (left_type[0] & type_mask))){\n h->topleft_samples_available&= 0xDF5F;\n h->left_samples_available&= 0x5F5F;\n }\n }\n }else{\n if(!(left_type[0] & type_mask)){\n h->topleft_samples_available&= 0xDF5F;\n h->left_samples_available&= 0x5F5F;\n }\n }\n if(!(topleft_type & type_mask))\n h->topleft_samples_available&= 0x7FFF;\n if(!(topright_type & type_mask))\n h->topright_samples_available&= 0xFBFF;\n if(IS_INTRA4x4(mb_type)){\n if(IS_INTRA4x4(top_type)){\n h->intra4x4_pred_mode_cache[4+8*0]= h->intra4x4_pred_mode[top_xy][4];\n h->intra4x4_pred_mode_cache[5+8*0]= h->intra4x4_pred_mode[top_xy][5];\n h->intra4x4_pred_mode_cache[6+8*0]= h->intra4x4_pred_mode[top_xy][6];\n h->intra4x4_pred_mode_cache[7+8*0]= h->intra4x4_pred_mode[top_xy][3];\n }else{\n int pred;\n if(!(top_type & type_mask))\n pred= -1;\n else{\n pred= 2;\n }\n h->intra4x4_pred_mode_cache[4+8*0]=\n h->intra4x4_pred_mode_cache[5+8*0]=\n h->intra4x4_pred_mode_cache[6+8*0]=\n h->intra4x4_pred_mode_cache[7+8*0]= pred;\n }\n for(i=0; i<2; i++){\n if(IS_INTRA4x4(left_type[i])){\n h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[0+2*i]];\n h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[1+2*i]];\n }else{\n int pred;\n if(!(left_type[i] & type_mask))\n pred= -1;\n else{\n pred= 2;\n }\n h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=\n h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= pred;\n }\n }\n }\n }\n }\n if(top_type){\n *(uint32_t*)&h->non_zero_count_cache[4+8*0]= *(uint32_t*)&h->non_zero_count[top_xy][4+3*8];\n if(!for_deblock){\n h->non_zero_count_cache[1+8*0]= h->non_zero_count[top_xy][1+1*8];\n h->non_zero_count_cache[2+8*0]= h->non_zero_count[top_xy][2+1*8];\n h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][1+2*8];\n h->non_zero_count_cache[2+8*3]= h->non_zero_count[top_xy][2+2*8];\n }\n }else if(!for_deblock){\n h->non_zero_count_cache[1+8*0]=\n h->non_zero_count_cache[2+8*0]=\n h->non_zero_count_cache[1+8*3]=\n h->non_zero_count_cache[2+8*3]=\n *(uint32_t*)&h->non_zero_count_cache[4+8*0]= CABAC && !IS_INTRA(mb_type) ? 0 : 0x40404040;\n }\n for (i=0; i<2; i++) {\n if(left_type[i]){\n h->non_zero_count_cache[3+8*1 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[8+0+2*i]];\n h->non_zero_count_cache[3+8*2 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[8+1+2*i]];\n if(!for_deblock){\n h->non_zero_count_cache[0+8*1 + 8*i]= h->non_zero_count[left_xy[i]][left_block[8+4+2*i]];\n h->non_zero_count_cache[0+8*4 + 8*i]= h->non_zero_count[left_xy[i]][left_block[8+5+2*i]];\n }\n }else if(!for_deblock){\n h->non_zero_count_cache[3+8*1 + 2*8*i]=\n h->non_zero_count_cache[3+8*2 + 2*8*i]=\n h->non_zero_count_cache[0+8*1 + 8*i]=\n h->non_zero_count_cache[0+8*4 + 8*i]= CABAC && !IS_INTRA(mb_type) ? 0 : 64;\n }\n }\n if(for_deblock && !CABAC && h->pps.transform_8x8_mode){\n if(IS_8x8DCT(top_type)){\n h->non_zero_count_cache[4+8*0]=\n h->non_zero_count_cache[5+8*0]= h->cbp_table[top_xy] & 4;\n h->non_zero_count_cache[6+8*0]=\n h->non_zero_count_cache[7+8*0]= h->cbp_table[top_xy] & 8;\n }\n if(IS_8x8DCT(left_type[0])){\n h->non_zero_count_cache[3+8*1]=\n h->non_zero_count_cache[3+8*2]= h->cbp_table[left_xy[0]]&2;\n }\n if(IS_8x8DCT(left_type[1])){\n h->non_zero_count_cache[3+8*3]=\n h->non_zero_count_cache[3+8*4]= h->cbp_table[left_xy[1]]&8;\n }\n if(IS_8x8DCT(mb_type)){\n h->non_zero_count_cache[scan8[0 ]]= h->non_zero_count_cache[scan8[1 ]]=\n h->non_zero_count_cache[scan8[2 ]]= h->non_zero_count_cache[scan8[3 ]]= h->cbp & 1;\n h->non_zero_count_cache[scan8[0+ 4]]= h->non_zero_count_cache[scan8[1+ 4]]=\n h->non_zero_count_cache[scan8[2+ 4]]= h->non_zero_count_cache[scan8[3+ 4]]= h->cbp & 2;\n h->non_zero_count_cache[scan8[0+ 8]]= h->non_zero_count_cache[scan8[1+ 8]]=\n h->non_zero_count_cache[scan8[2+ 8]]= h->non_zero_count_cache[scan8[3+ 8]]= h->cbp & 4;\n h->non_zero_count_cache[scan8[0+12]]= h->non_zero_count_cache[scan8[1+12]]=\n h->non_zero_count_cache[scan8[2+12]]= h->non_zero_count_cache[scan8[3+12]]= h->cbp & 8;\n }\n }\n if( CABAC && !for_deblock) {\n if(top_type) {\n h->top_cbp = h->cbp_table[top_xy];\n } else if(IS_INTRA(mb_type)) {\n h->top_cbp = 0x1C0;\n } else {\n h->top_cbp = 0;\n }\n if (left_type[0]) {\n h->left_cbp = h->cbp_table[left_xy[0]] & 0x1f0;\n } else if(IS_INTRA(mb_type)) {\n h->left_cbp = 0x1C0;\n } else {\n h->left_cbp = 0;\n }\n if (left_type[0]) {\n h->left_cbp |= ((h->cbp_table[left_xy[0]]>>((left_block[0]&(~1))+1))&0x1) << 1;\n }\n if (left_type[1]) {\n h->left_cbp |= ((h->cbp_table[left_xy[1]]>>((left_block[2]&(~1))+1))&0x1) << 3;\n }\n }\n#if 1\n if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){\n int list;\n for(list=0; list<h->list_count; list++){\n if(!for_deblock && !USES_LIST(mb_type, list) && !IS_DIRECT(mb_type)){\n continue;\n }\n h->mv_cache_clean[list]= 0;\n if(USES_LIST(top_type, list)){\n const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;\n const int b8_xy= h->mb2b8_xy[top_xy] + h->b8_stride;\n AV_COPY128(h->mv_cache[list][scan8[0] + 0 - 1*8], s->current_picture.motion_val[list][b_xy + 0]);\n if(for_deblock){\n int (*ref2frm)[64] = h->ref2frm[ h->slice_table[top_xy]&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);\n h->ref_cache[list][scan8[0] + 0 - 1*8]=\n h->ref_cache[list][scan8[0] + 1 - 1*8]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 0]];\n h->ref_cache[list][scan8[0] + 2 - 1*8]=\n h->ref_cache[list][scan8[0] + 3 - 1*8]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 1]];\n }else{\n h->ref_cache[list][scan8[0] + 0 - 1*8]=\n h->ref_cache[list][scan8[0] + 1 - 1*8]= s->current_picture.ref_index[list][b8_xy + 0];\n h->ref_cache[list][scan8[0] + 2 - 1*8]=\n h->ref_cache[list][scan8[0] + 3 - 1*8]= s->current_picture.ref_index[list][b8_xy + 1];\n }\n }else{\n AV_ZERO128(h->mv_cache[list][scan8[0] + 0 - 1*8]);\n *(uint32_t*)&h->ref_cache[list][scan8[0] + 0 - 1*8]= (((for_deblock||top_type) ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101;\n }\n for(i=0; i<2; i++){\n int cache_idx = scan8[0] - 1 + i*2*8;\n if(USES_LIST(left_type[i], list)){\n const int b_xy= h->mb2b_xy[left_xy[i]] + 3;\n const int b8_xy= h->mb2b8_xy[left_xy[i]] + 1;\n *(uint32_t*)h->mv_cache[list][cache_idx ]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[0+i*2]];\n *(uint32_t*)h->mv_cache[list][cache_idx+8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[1+i*2]];\n if(for_deblock){\n int (*ref2frm)[64] = h->ref2frm[ h->slice_table[left_xy[i]]&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);\n h->ref_cache[list][cache_idx ]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[0+i*2]>>1)]];\n h->ref_cache[list][cache_idx+8]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[1+i*2]>>1)]];\n }else{\n h->ref_cache[list][cache_idx ]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[0+i*2]>>1)];\n h->ref_cache[list][cache_idx+8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[1+i*2]>>1)];\n }\n }else{\n *(uint32_t*)h->mv_cache [list][cache_idx ]=\n *(uint32_t*)h->mv_cache [list][cache_idx+8]= 0;\n h->ref_cache[list][cache_idx ]=\n h->ref_cache[list][cache_idx+8]= (for_deblock||left_type[i]) ? LIST_NOT_USED : PART_NOT_AVAILABLE;\n }\n }\n if(for_deblock || ((IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred) && !FRAME_MBAFF))\n continue;\n if(USES_LIST(topleft_type, list)){\n const int b_xy = h->mb2b_xy[topleft_xy] + 3 + h->b_stride + (topleft_partition & 2*h->b_stride);\n const int b8_xy= h->mb2b8_xy[topleft_xy] + 1 + (topleft_partition & h->b8_stride);\n *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];\n h->ref_cache[list][scan8[0] - 1 - 1*8]= s->current_picture.ref_index[list][b8_xy];\n }else{\n *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= 0;\n h->ref_cache[list][scan8[0] - 1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;\n }\n if(USES_LIST(topright_type, list)){\n const int b_xy= h->mb2b_xy[topright_xy] + 3*h->b_stride;\n const int b8_xy= h->mb2b8_xy[topright_xy] + h->b8_stride;\n *(uint32_t*)h->mv_cache[list][scan8[0] + 4 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];\n h->ref_cache[list][scan8[0] + 4 - 1*8]= s->current_picture.ref_index[list][b8_xy];\n }else{\n *(uint32_t*)h->mv_cache [list][scan8[0] + 4 - 1*8]= 0;\n h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;\n }\n if((IS_SKIP(mb_type) || IS_DIRECT(mb_type)) && !FRAME_MBAFF)\n continue;\n h->ref_cache[list][scan8[5 ]+1] =\n h->ref_cache[list][scan8[7 ]+1] =\n h->ref_cache[list][scan8[13]+1] =\n h->ref_cache[list][scan8[4 ]] =\n h->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE;\n *(uint32_t*)h->mv_cache [list][scan8[5 ]+1]=\n *(uint32_t*)h->mv_cache [list][scan8[7 ]+1]=\n *(uint32_t*)h->mv_cache [list][scan8[13]+1]=\n *(uint32_t*)h->mv_cache [list][scan8[4 ]]=\n *(uint32_t*)h->mv_cache [list][scan8[12]]= 0;\n if( CABAC ) {\n if(USES_LIST(top_type, list)){\n const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;\n AV_COPY128(h->mvd_cache[list][scan8[0] + 0 - 1*8], h->mvd_table[list][b_xy + 0]);\n }else{\n AV_ZERO128(h->mvd_cache[list][scan8[0] + 0 - 1*8]);\n }\n if(USES_LIST(left_type[0], list)){\n const int b_xy= h->mb2b_xy[left_xy[0]] + 3;\n *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 0*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[0]];\n *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[1]];\n }else{\n *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 0*8]=\n *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 1*8]= 0;\n }\n if(USES_LIST(left_type[1], list)){\n const int b_xy= h->mb2b_xy[left_xy[1]] + 3;\n *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 2*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[2]];\n *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 3*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[3]];\n }else{\n *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 2*8]=\n *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 3*8]= 0;\n }\n *(uint32_t*)h->mvd_cache [list][scan8[5 ]+1]=\n *(uint32_t*)h->mvd_cache [list][scan8[7 ]+1]=\n *(uint32_t*)h->mvd_cache [list][scan8[13]+1]=\n *(uint32_t*)h->mvd_cache [list][scan8[4 ]]=\n *(uint32_t*)h->mvd_cache [list][scan8[12]]= 0;\n if(h->slice_type_nos == FF_B_TYPE){\n fill_rectangle(&h->direct_cache[scan8[0]], 4, 4, 8, 0, 1);\n if(IS_DIRECT(top_type)){\n *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0x01010101;\n }else if(IS_8X8(top_type)){\n int b8_xy = h->mb2b8_xy[top_xy] + h->b8_stride;\n h->direct_cache[scan8[0] + 0 - 1*8]= h->direct_table[b8_xy];\n h->direct_cache[scan8[0] + 2 - 1*8]= h->direct_table[b8_xy + 1];\n }else{\n *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0;\n }\n if(IS_DIRECT(left_type[0]))\n h->direct_cache[scan8[0] - 1 + 0*8]= 1;\n else if(IS_8X8(left_type[0]))\n h->direct_cache[scan8[0] - 1 + 0*8]= h->direct_table[h->mb2b8_xy[left_xy[0]] + 1 + h->b8_stride*(left_block[0]>>1)];\n else\n h->direct_cache[scan8[0] - 1 + 0*8]= 0;\n if(IS_DIRECT(left_type[1]))\n h->direct_cache[scan8[0] - 1 + 2*8]= 1;\n else if(IS_8X8(left_type[1]))\n h->direct_cache[scan8[0] - 1 + 2*8]= h->direct_table[h->mb2b8_xy[left_xy[1]] + 1 + h->b8_stride*(left_block[2]>>1)];\n else\n h->direct_cache[scan8[0] - 1 + 2*8]= 0;\n }\n }\n if(FRAME_MBAFF){\n#define MAP_MVS\\\n MAP_F2F(scan8[0] - 1 - 1*8, topleft_type)\\\n MAP_F2F(scan8[0] + 0 - 1*8, top_type)\\\n MAP_F2F(scan8[0] + 1 - 1*8, top_type)\\\n MAP_F2F(scan8[0] + 2 - 1*8, top_type)\\\n MAP_F2F(scan8[0] + 3 - 1*8, top_type)\\\n MAP_F2F(scan8[0] + 4 - 1*8, topright_type)\\\n MAP_F2F(scan8[0] - 1 + 0*8, left_type[0])\\\n MAP_F2F(scan8[0] - 1 + 1*8, left_type[0])\\\n MAP_F2F(scan8[0] - 1 + 2*8, left_type[1])\\\n MAP_F2F(scan8[0] - 1 + 3*8, left_type[1])\n if(MB_FIELD){\n#define MAP_F2F(idx, mb_type)\\\n if(!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\\\n h->ref_cache[list][idx] <<= 1;\\\n h->mv_cache[list][idx][1] /= 2;\\\n h->mvd_cache[list][idx][1] /= 2;\\\n }\n MAP_MVS\n#undef MAP_F2F\n }else{\n#define MAP_F2F(idx, mb_type)\\\n if(IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\\\n h->ref_cache[list][idx] >>= 1;\\\n h->mv_cache[list][idx][1] <<= 1;\\\n h->mvd_cache[list][idx][1] <<= 1;\\\n }\n MAP_MVS\n#undef MAP_F2F\n }\n }\n }\n }\n#endif\n if(!for_deblock)\n h->neighbor_transform_size= !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[0]);\n return 0;\n}'] |
586 | 0 | https://github.com/openssl/openssl/blob/3ba25ee86a3758cc659c954b59718d8397030768/crypto/lhash/lhash.c/#L240 | void *lh_delete(LHASH *lh, const void *data)
{
unsigned long hash;
LHASH_NODE *nn,**rn;
const void *ret;
lh->error=0;
rn=getrn(lh,data,&hash);
if (*rn == NULL)
{
lh->num_no_delete++;
return(NULL);
}
else
{
nn= *rn;
*rn=nn->next;
ret=nn->data;
OPENSSL_free(nn);
lh->num_delete++;
}
lh->num_items--;
if ((lh->num_nodes > MIN_NODES) &&
(lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes)))
contract(lh);
return((void *)ret);
} | ['int main(int argc, char *argv[])\n\t{\n\tchar *CApath=NULL,*CAfile=NULL;\n\tint badop=0;\n\tint bio_pair=0;\n\tint force=0;\n\tint tls1=0,ssl2=0,ssl3=0,ret=1;\n\tint client_auth=0;\n\tint server_auth=0,i;\n\tchar *server_cert=TEST_SERVER_CERT;\n\tchar *server_key=NULL;\n\tchar *client_cert=TEST_CLIENT_CERT;\n\tchar *client_key=NULL;\n\tSSL_CTX *s_ctx=NULL;\n\tSSL_CTX *c_ctx=NULL;\n\tSSL_METHOD *meth=NULL;\n\tSSL *c_ssl,*s_ssl;\n\tint number=1,reuse=0;\n\tlong bytes=1L;\n#ifndef NO_DH\n\tDH *dh;\n\tint dhe1024 = 0, dhe1024dsa = 0;\n#endif\n\tint no_dhe = 0;\n\tint print_time = 0;\n\tclock_t s_time = 0, c_time = 0;\n\tint comp = 0;\n\tCOMP_METHOD *cm = NULL;\n\tverbose = 0;\n\tdebug = 0;\n\tcipher = 0;\n\tCRYPTO_set_locking_callback(lock_dbg_cb);\n\tCRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);\n\tRAND_seed(rnd_seed, sizeof rnd_seed);\n\tbio_err=BIO_new_fp(stderr,BIO_NOCLOSE);\n\tbio_stdout=BIO_new_fp(stdout,BIO_NOCLOSE);\n\targc--;\n\targv++;\n\twhile (argc >= 1)\n\t\t{\n\t\tif\t(strcmp(*argv,"-server_auth") == 0)\n\t\t\tserver_auth=1;\n\t\telse if\t(strcmp(*argv,"-client_auth") == 0)\n\t\t\tclient_auth=1;\n\t\telse if\t(strcmp(*argv,"-v") == 0)\n\t\t\tverbose=1;\n\t\telse if\t(strcmp(*argv,"-d") == 0)\n\t\t\tdebug=1;\n\t\telse if\t(strcmp(*argv,"-reuse") == 0)\n\t\t\treuse=1;\n#ifndef NO_DH\n\t\telse if\t(strcmp(*argv,"-dhe1024") == 0)\n\t\t\tdhe1024=1;\n\t\telse if\t(strcmp(*argv,"-dhe1024dsa") == 0)\n\t\t\tdhe1024dsa=1;\n#endif\n\t\telse if\t(strcmp(*argv,"-no_dhe") == 0)\n\t\t\tno_dhe=1;\n\t\telse if\t(strcmp(*argv,"-ssl2") == 0)\n\t\t\tssl2=1;\n\t\telse if\t(strcmp(*argv,"-tls1") == 0)\n\t\t\ttls1=1;\n\t\telse if\t(strcmp(*argv,"-ssl3") == 0)\n\t\t\tssl3=1;\n\t\telse if\t(strncmp(*argv,"-num",4) == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tnumber= atoi(*(++argv));\n\t\t\tif (number == 0) number=1;\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-bytes") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tbytes= atol(*(++argv));\n\t\t\tif (bytes == 0L) bytes=1L;\n\t\t\ti=strlen(argv[0]);\n\t\t\tif (argv[0][i-1] == \'k\') bytes*=1024L;\n\t\t\tif (argv[0][i-1] == \'m\') bytes*=1024L*1024L;\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-cert") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tserver_cert= *(++argv);\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-s_cert") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tserver_cert= *(++argv);\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-key") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tserver_key= *(++argv);\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-s_key") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tserver_key= *(++argv);\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-c_cert") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tclient_cert= *(++argv);\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-c_key") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tclient_key= *(++argv);\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-cipher") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tcipher= *(++argv);\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-CApath") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tCApath= *(++argv);\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-CAfile") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tCAfile= *(++argv);\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-bio_pair") == 0)\n\t\t\t{\n\t\t\tbio_pair = 1;\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-f") == 0)\n\t\t\t{\n\t\t\tforce = 1;\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-time") == 0)\n\t\t\t{\n\t\t\tprint_time = 1;\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-zlib") == 0)\n\t\t\t{\n\t\t\tcomp = COMP_ZLIB;\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-rle") == 0)\n\t\t\t{\n\t\t\tcomp = COMP_RLE;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tfprintf(stderr,"unknown option %s\\n",*argv);\n\t\t\tbadop=1;\n\t\t\tbreak;\n\t\t\t}\n\t\targc--;\n\t\targv++;\n\t\t}\n\tif (badop)\n\t\t{\nbad:\n\t\tsv_usage();\n\t\tgoto end;\n\t\t}\n\tif (!ssl2 && !ssl3 && !tls1 && number > 1 && !reuse && !force)\n\t\t{\n\t\tfprintf(stderr, "This case cannot work. Use -f to perform "\n\t\t\t"the test anyway (and\\n-d to see what happens), "\n\t\t\t"or add one of -ssl2, -ssl3, -tls1, -reuse\\n"\n\t\t\t"to avoid protocol mismatch.\\n");\n\t\texit(1);\n\t\t}\n\tif (print_time)\n\t\t{\n\t\tif (!bio_pair)\n\t\t\t{\n\t\t\tfprintf(stderr, "Using BIO pair (-bio_pair)\\n");\n\t\t\tbio_pair = 1;\n\t\t\t}\n\t\tif (number < 50 && !force)\n\t\t\tfprintf(stderr, "Warning: For accurate timings, use more connections (e.g. -num 1000)\\n");\n\t\t}\n\tSSL_library_init();\n\tSSL_load_error_strings();\n\tif (comp == COMP_ZLIB) cm = COMP_zlib();\n\tif (comp == COMP_RLE) cm = COMP_rle();\n\tif (cm != NULL)\n\t\t{\n\t\tif (cm->type != NID_undef)\n\t\t\tSSL_COMP_add_compression_method(comp, cm);\n\t\telse\n\t\t\t{\n\t\t\tfprintf(stderr,\n\t\t\t\t"Warning: %s compression not supported\\n",\n\t\t\t\t(comp == COMP_RLE ? "rle" :\n\t\t\t\t\t(comp == COMP_ZLIB ? "zlib" :\n\t\t\t\t\t\t"unknown")));\n\t\t\tERR_print_errors_fp(stderr);\n\t\t\t}\n\t\t}\n#if !defined(NO_SSL2) && !defined(NO_SSL3)\n\tif (ssl2)\n\t\tmeth=SSLv2_method();\n\telse\n\tif (tls1)\n\t\tmeth=TLSv1_method();\n\telse\n\tif (ssl3)\n\t\tmeth=SSLv3_method();\n\telse\n\t\tmeth=SSLv23_method();\n#else\n#ifdef NO_SSL2\n\tmeth=SSLv3_method();\n#else\n\tmeth=SSLv2_method();\n#endif\n#endif\n\tc_ctx=SSL_CTX_new(meth);\n\ts_ctx=SSL_CTX_new(meth);\n\tif ((c_ctx == NULL) || (s_ctx == NULL))\n\t\t{\n\t\tERR_print_errors(bio_err);\n\t\tgoto end;\n\t\t}\n\tif (cipher != NULL)\n\t\t{\n\t\tSSL_CTX_set_cipher_list(c_ctx,cipher);\n\t\tSSL_CTX_set_cipher_list(s_ctx,cipher);\n\t\t}\n#ifndef NO_DH\n\tif (!no_dhe)\n\t\t{\n\t\tif (dhe1024dsa)\n\t\t\t{\n\t\t\tSSL_CTX_set_options(s_ctx, SSL_OP_SINGLE_DH_USE);\n\t\t\tdh=get_dh1024dsa();\n\t\t\t}\n\t\telse if (dhe1024)\n\t\t\tdh=get_dh1024();\n\t\telse\n\t\t\tdh=get_dh512();\n\t\tSSL_CTX_set_tmp_dh(s_ctx,dh);\n\t\tDH_free(dh);\n\t\t}\n#else\n\t(void)no_dhe;\n#endif\n#ifndef NO_RSA\n\tSSL_CTX_set_tmp_rsa_callback(s_ctx,tmp_rsa_cb);\n#endif\n\tif (!SSL_CTX_use_certificate_file(s_ctx,server_cert,SSL_FILETYPE_PEM))\n\t\t{\n\t\tERR_print_errors(bio_err);\n\t\t}\n\telse if (!SSL_CTX_use_PrivateKey_file(s_ctx,\n\t\t(server_key?server_key:server_cert), SSL_FILETYPE_PEM))\n\t\t{\n\t\tERR_print_errors(bio_err);\n\t\tgoto end;\n\t\t}\n\tif (client_auth)\n\t\t{\n\t\tSSL_CTX_use_certificate_file(c_ctx,client_cert,\n\t\t\tSSL_FILETYPE_PEM);\n\t\tSSL_CTX_use_PrivateKey_file(c_ctx,\n\t\t\t(client_key?client_key:client_cert),\n\t\t\tSSL_FILETYPE_PEM);\n\t\t}\n\tif (\t(!SSL_CTX_load_verify_locations(s_ctx,CAfile,CApath)) ||\n\t\t(!SSL_CTX_set_default_verify_paths(s_ctx)) ||\n\t\t(!SSL_CTX_load_verify_locations(c_ctx,CAfile,CApath)) ||\n\t\t(!SSL_CTX_set_default_verify_paths(c_ctx)))\n\t\t{\n\t\tERR_print_errors(bio_err);\n\t\t}\n\tif (client_auth)\n\t\t{\n\t\tBIO_printf(bio_err,"client authentication\\n");\n\t\tSSL_CTX_set_verify(s_ctx,\n\t\t\tSSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,\n\t\t\tverify_callback);\n\t\t}\n\tif (server_auth)\n\t\t{\n\t\tBIO_printf(bio_err,"server authentication\\n");\n\t\tSSL_CTX_set_verify(c_ctx,SSL_VERIFY_PEER,\n\t\t\tverify_callback);\n\t\t}\n\t{\n\t\tint session_id_context = 0;\n\t\tSSL_CTX_set_session_id_context(s_ctx, (void *)&session_id_context, sizeof session_id_context);\n\t}\n\tc_ssl=SSL_new(c_ctx);\n\ts_ssl=SSL_new(s_ctx);\n#ifndef NO_KRB5\n\tif (c_ssl && c_ssl->kssl_ctx)\n {\n char\tlocalhost[257];\n\t\tif (gethostname(localhost, 256) == 0)\n {\n\t\t\tkssl_ctx_setstring(c_ssl->kssl_ctx, KSSL_SERVER,\n localhost);\n\t\t\t}\n\t\t}\n#endif\n\tfor (i=0; i<number; i++)\n\t\t{\n\t\tif (!reuse) SSL_set_session(c_ssl,NULL);\n\t\tif (bio_pair)\n\t\t\tret=doit_biopair(s_ssl,c_ssl,bytes,&s_time,&c_time);\n\t\telse\n\t\t\tret=doit(s_ssl,c_ssl,bytes);\n\t\t}\n\tif (!verbose)\n\t\t{\n\t\tprint_details(c_ssl, "");\n\t\t}\n\tif ((number > 1) || (bytes > 1L))\n\t\tBIO_printf(bio_stdout, "%d handshakes of %ld bytes done\\n",number,bytes);\n\tif (print_time)\n\t\t{\n#ifdef CLOCKS_PER_SEC\n\t\tBIO_printf(bio_stdout, "Approximate total server time: %6.2f s\\n"\n\t\t\t"Approximate total client time: %6.2f s\\n",\n\t\t\t(double)s_time/CLOCKS_PER_SEC,\n\t\t\t(double)c_time/CLOCKS_PER_SEC);\n#else\n\t\tBIO_printf(bio_stdout,\n\t\t\t"Approximate total server time: %6.2f units\\n"\n\t\t\t"Approximate total client time: %6.2f units\\n",\n\t\t\t(double)s_time,\n\t\t\t(double)c_time);\n#endif\n\t\t}\n\tSSL_free(s_ssl);\n\tSSL_free(c_ssl);\nend:\n\tif (s_ctx != NULL) SSL_CTX_free(s_ctx);\n\tif (c_ctx != NULL) SSL_CTX_free(c_ctx);\n\tif (bio_stdout != NULL) BIO_free(bio_stdout);\n#ifndef NO_RSA\n\tfree_tmp_rsa();\n#endif\n\tERR_free_strings();\n\tERR_remove_state(0);\n\tEVP_cleanup();\n\tCRYPTO_mem_leaks(bio_err);\n\tif (bio_err != NULL) BIO_free(bio_err);\n\tEXIT(ret);\n\t}', 'SSL_CTX *SSL_CTX_new(SSL_METHOD *meth)\n\t{\n\tSSL_CTX *ret=NULL;\n\tif (meth == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_CTX_NEW,SSL_R_NULL_SSL_METHOD_PASSED);\n\t\treturn(NULL);\n\t\t}\n\tif (SSL_get_ex_data_X509_STORE_CTX_idx() < 0)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_CTX_NEW,SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);\n\t\tgoto err;\n\t\t}\n\tret=(SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));\n\tif (ret == NULL)\n\t\tgoto err;\n\tmemset(ret,0,sizeof(SSL_CTX));\n\tret->method=meth;\n\tret->cert_store=NULL;\n\tret->session_cache_mode=SSL_SESS_CACHE_SERVER;\n\tret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;\n\tret->session_cache_head=NULL;\n\tret->session_cache_tail=NULL;\n\tret->session_timeout=meth->get_timeout();\n\tret->new_session_cb=NULL;\n\tret->remove_session_cb=NULL;\n\tret->get_session_cb=NULL;\n\tmemset((char *)&ret->stats,0,sizeof(ret->stats));\n\tret->references=1;\n\tret->quiet_shutdown=0;\n\tret->info_callback=NULL;\n\tret->app_verify_callback=NULL;\n\tret->app_verify_arg=NULL;\n\tret->read_ahead=0;\n\tret->verify_mode=SSL_VERIFY_NONE;\n\tret->verify_depth=-1;\n\tret->default_verify_callback=NULL;\n\tif ((ret->cert=ssl_cert_new()) == NULL)\n\t\tgoto err;\n\tret->default_passwd_callback=NULL;\n\tret->default_passwd_callback_userdata=NULL;\n\tret->client_cert_cb=NULL;\n\tret->sessions=lh_new(LHASH_HASH_FN(SSL_SESSION_hash),\n\t\t\tLHASH_COMP_FN(SSL_SESSION_cmp));\n\tif (ret->sessions == NULL) goto err;\n\tret->cert_store=X509_STORE_new();\n\tif (ret->cert_store == NULL) goto err;\n\tssl_create_cipher_list(ret->method,\n\t\t&ret->cipher_list,&ret->cipher_list_by_id,\n\t\tSSL_DEFAULT_CIPHER_LIST);\n\tif (ret->cipher_list == NULL\n\t || sk_SSL_CIPHER_num(ret->cipher_list) <= 0)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_CTX_NEW,SSL_R_LIBRARY_HAS_NO_CIPHERS);\n\t\tgoto err2;\n\t\t}\n\tif ((ret->rsa_md5=EVP_get_digestbyname("ssl2-md5")) == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL2_MD5_ROUTINES);\n\t\tgoto err2;\n\t\t}\n\tif ((ret->md5=EVP_get_digestbyname("ssl3-md5")) == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES);\n\t\tgoto err2;\n\t\t}\n\tif ((ret->sha1=EVP_get_digestbyname("ssl3-sha1")) == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES);\n\t\tgoto err2;\n\t\t}\n\tif ((ret->client_CA=sk_X509_NAME_new_null()) == NULL)\n\t\tgoto err;\n\tCRYPTO_new_ex_data(ssl_ctx_meth,(char *)ret,&ret->ex_data);\n\tret->extra_certs=NULL;\n\tret->comp_methods=SSL_COMP_get_compression_methods();\n\treturn(ret);\nerr:\n\tSSLerr(SSL_F_SSL_CTX_NEW,ERR_R_MALLOC_FAILURE);\nerr2:\n\tif (ret != NULL) SSL_CTX_free(ret);\n\treturn(NULL);\n\t}', 'LHASH *lh_new(LHASH_HASH_FN_TYPE h, LHASH_COMP_FN_TYPE c)\n\t{\n\tLHASH *ret;\n\tint i;\n\tif ((ret=(LHASH *)OPENSSL_malloc(sizeof(LHASH))) == NULL)\n\t\tgoto err0;\n\tif ((ret->b=(LHASH_NODE **)OPENSSL_malloc(sizeof(LHASH_NODE *)*MIN_NODES)) == NULL)\n\t\tgoto err1;\n\tfor (i=0; i<MIN_NODES; i++)\n\t\tret->b[i]=NULL;\n\tret->comp=((c == NULL)?(LHASH_COMP_FN_TYPE)strcmp:c);\n\tret->hash=((h == NULL)?(LHASH_HASH_FN_TYPE)lh_strhash:h);\n\tret->num_nodes=MIN_NODES/2;\n\tret->num_alloc_nodes=MIN_NODES;\n\tret->p=0;\n\tret->pmax=MIN_NODES/2;\n\tret->up_load=UP_LOAD;\n\tret->down_load=DOWN_LOAD;\n\tret->num_items=0;\n\tret->num_expands=0;\n\tret->num_expand_reallocs=0;\n\tret->num_contracts=0;\n\tret->num_contract_reallocs=0;\n\tret->num_hash_calls=0;\n\tret->num_comp_calls=0;\n\tret->num_insert=0;\n\tret->num_replace=0;\n\tret->num_delete=0;\n\tret->num_no_delete=0;\n\tret->num_retrieve=0;\n\tret->num_retrieve_miss=0;\n\tret->num_hash_comps=0;\n\tret->error=0;\n\treturn(ret);\nerr1:\n\tOPENSSL_free(ret);\nerr0:\n\treturn(NULL);\n\t}', '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 *)OPENSSL_malloc(sizeof(SSL));\n\tif (s == NULL) goto err;\n\tmemset(s,0,sizeof(SSL));\n#ifndef\tNO_KRB5\n\ts->kssl_ctx = kssl_ctx_new();\n#endif\n\tif (ctx->cert != NULL)\n\t\t{\n\t\ts->cert = ssl_cert_dup(ctx->cert);\n\t\tif (s->cert == NULL)\n\t\t\tgoto err;\n\t\t}\n\telse\n\t\ts->cert=NULL;\n\ts->sid_ctx_length=ctx->sid_ctx_length;\n\tmemcpy(&s->sid_ctx,&ctx->sid_ctx,sizeof(s->sid_ctx));\n\ts->verify_mode=ctx->verify_mode;\n\ts->verify_depth=ctx->verify_depth;\n\ts->verify_callback=ctx->default_verify_callback;\n\ts->purpose = ctx->purpose;\n\ts->trust = ctx->trust;\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\tgoto err;\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\ts->mode=ctx->mode;\n\ts->read_ahead=ctx->read_ahead;\n\tSSL_clear(s);\n\tCRYPTO_new_ex_data(ssl_meth,s,&s->ex_data);\n\treturn(s);\nerr:\n\tif (s != NULL)\n\t\t{\n\t\tif (s->cert != NULL)\n\t\t\tssl_cert_free(s->cert);\n\t\tif (s->ctx != NULL)\n\t\t\tSSL_CTX_free(s->ctx);\n\t\tOPENSSL_free(s);\n\t\t}\n\tSSLerr(SSL_F_SSL_NEW,ERR_R_MALLOC_FAILURE);\n\treturn(NULL);\n\t}', 'int SSL_clear(SSL *s)\n\t{\n\tint state;\n\tif (s->method == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_CLEAR,SSL_R_NO_METHOD_SPECIFIED);\n\t\treturn(0);\n\t\t}\n\ts->error=0;\n\ts->hit=0;\n\ts->shutdown=0;\n#if 0\n\tif (s->new_session) return(1);\n#else\n\tif (s->new_session)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_CLEAR,SSL_R_INTERNAL_ERROR);\n\t\treturn 0;\n\t\t}\n#endif\n\tstate=s->state;\n\ts->type=0;\n\ts->state=SSL_ST_BEFORE|((s->server)?SSL_ST_ACCEPT:SSL_ST_CONNECT);\n\ts->version=s->method->version;\n\ts->client_version=s->version;\n\ts->rwstate=SSL_NOTHING;\n\ts->rstate=SSL_ST_READ_HEADER;\n#if 0\n\ts->read_ahead=s->ctx->read_ahead;\n#endif\n\tif (s->init_buf != NULL)\n\t\t{\n\t\tBUF_MEM_free(s->init_buf);\n\t\ts->init_buf=NULL;\n\t\t}\n\tssl_clear_cipher_ctx(s);\n\tif (ssl_clear_bad_session(s))\n\t\t{\n\t\tSSL_SESSION_free(s->session);\n\t\ts->session=NULL;\n\t\t}\n\ts->first_packet=0;\n#if 1\n\tif ((s->session == NULL) && (s->method != s->ctx->method))\n\t\t{\n\t\ts->method->ssl_free(s);\n\t\ts->method=s->ctx->method;\n\t\tif (!s->method->ssl_new(s))\n\t\t\treturn(0);\n\t\t}\n\telse\n#endif\n\t\ts->method->ssl_clear(s);\n\treturn(1);\n\t}', 'int ssl_clear_bad_session(SSL *s)\n\t{\n\tif (\t(s->session != NULL) &&\n\t\t!(s->shutdown & SSL_SENT_SHUTDOWN) &&\n\t\t!(SSL_in_init(s) || SSL_in_before(s)))\n\t\t{\n\t\tSSL_CTX_remove_session(s->ctx,s->session);\n\t\treturn(1);\n\t\t}\n\telse\n\t\treturn(0);\n\t}', 'int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)\n{\n\treturn remove_session_lock(ctx, c, 1);\n}', 'static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)\n\t{\n\tSSL_SESSION *r;\n\tint ret=0;\n\tif ((c != NULL) && (c->session_id_length != 0))\n\t\t{\n\t\tif(lck) CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);\n\t\tr=(SSL_SESSION *)lh_delete(ctx->sessions,c);\n\t\tif (r != NULL)\n\t\t\t{\n\t\t\tret=1;\n\t\t\tSSL_SESSION_list_remove(ctx,c);\n\t\t\t}\n\t\tif(lck) CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n\t\tif (ret)\n\t\t\t{\n\t\t\tr->not_resumable=1;\n\t\t\tif (ctx->remove_session_cb != NULL)\n\t\t\t\tctx->remove_session_cb(ctx,r);\n\t\t\tSSL_SESSION_free(r);\n\t\t\t}\n\t\t}\n\telse\n\t\tret=0;\n\treturn(ret);\n\t}', 'void *lh_delete(LHASH *lh, const void *data)\n\t{\n\tunsigned long hash;\n\tLHASH_NODE *nn,**rn;\n\tconst void *ret;\n\tlh->error=0;\n\trn=getrn(lh,data,&hash);\n\tif (*rn == NULL)\n\t\t{\n\t\tlh->num_no_delete++;\n\t\treturn(NULL);\n\t\t}\n\telse\n\t\t{\n\t\tnn= *rn;\n\t\t*rn=nn->next;\n\t\tret=nn->data;\n\t\tOPENSSL_free(nn);\n\t\tlh->num_delete++;\n\t\t}\n\tlh->num_items--;\n\tif ((lh->num_nodes > MIN_NODES) &&\n\t\t(lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes)))\n\t\tcontract(lh);\n\treturn((void *)ret);\n\t}'] |
587 | 0 | https://github.com/nginx/nginx/blob/ea9a1d745b5db26cac2d4b6e231ca6c4af0b4e5a/src/http/modules/ngx_http_geo_module.c/#L821 | static char *
ngx_http_geo_add_range(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx,
in_addr_t start, in_addr_t end)
{
in_addr_t n;
ngx_uint_t h, i, s, e;
ngx_array_t *a;
ngx_http_geo_range_t *range;
for (n = start; n <= end; n = (n + 0x10000) & 0xffff0000) {
h = n >> 16;
if (n == start) {
s = n & 0xffff;
} else {
s = 0;
}
if ((n | 0xffff) > end) {
e = end & 0xffff;
} else {
e = 0xffff;
}
a = (ngx_array_t *) ctx->high.low[h];
if (a == NULL) {
a = ngx_array_create(ctx->temp_pool, 64,
sizeof(ngx_http_geo_range_t));
if (a == NULL) {
return NGX_CONF_ERROR;
}
ctx->high.low[h] = (ngx_http_geo_range_t *) a;
}
i = a->nelts;
range = a->elts;
while (i) {
i--;
if (e < (ngx_uint_t) range[i].start) {
continue;
}
if (s > (ngx_uint_t) range[i].end) {
range = ngx_array_push(a);
if (range == NULL) {
return NGX_CONF_ERROR;
}
range = a->elts;
ngx_memmove(&range[i + 2], &range[i + 1],
(a->nelts - 2 - i) * sizeof(ngx_http_geo_range_t));
range[i + 1].start = (u_short) s;
range[i + 1].end = (u_short) e;
range[i + 1].value = ctx->value;
goto next;
}
if (s == (ngx_uint_t) range[i].start
&& e == (ngx_uint_t) range[i].end)
{
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
"duplicate range \"%V\", value: \"%v\", old value: \"%v\"",
ctx->net, ctx->value, range[i].value);
range[i].value = ctx->value;
goto next;
}
if (s > (ngx_uint_t) range[i].start
&& e < (ngx_uint_t) range[i].end)
{
range = ngx_array_push(a);
if (range == NULL) {
return NGX_CONF_ERROR;
}
range = ngx_array_push(a);
if (range == NULL) {
return NGX_CONF_ERROR;
}
range = a->elts;
ngx_memmove(&range[i + 3], &range[i + 1],
(a->nelts - 3 - i) * sizeof(ngx_http_geo_range_t));
range[i + 2].start = (u_short) (e + 1);
range[i + 2].end = range[i].end;
range[i + 2].value = range[i].value;
range[i + 1].start = (u_short) s;
range[i + 1].end = (u_short) e;
range[i + 1].value = ctx->value;
range[i].end = (u_short) (s - 1);
goto next;
}
if (s == (ngx_uint_t) range[i].start
&& e < (ngx_uint_t) range[i].end)
{
range = ngx_array_push(a);
if (range == NULL) {
return NGX_CONF_ERROR;
}
range = a->elts;
ngx_memmove(&range[i + 1], &range[i],
(a->nelts - 1 - i) * sizeof(ngx_http_geo_range_t));
range[i + 1].start = (u_short) (e + 1);
range[i].start = (u_short) s;
range[i].end = (u_short) e;
range[i].value = ctx->value;
goto next;
}
if (s > (ngx_uint_t) range[i].start
&& e == (ngx_uint_t) range[i].end)
{
range = ngx_array_push(a);
if (range == NULL) {
return NGX_CONF_ERROR;
}
range = a->elts;
ngx_memmove(&range[i + 2], &range[i + 1],
(a->nelts - 2 - i) * sizeof(ngx_http_geo_range_t));
range[i + 1].start = (u_short) s;
range[i + 1].end = (u_short) e;
range[i + 1].value = ctx->value;
range[i].end = (u_short) (s - 1);
goto next;
}
s = (ngx_uint_t) range[i].start;
e = (ngx_uint_t) range[i].end;
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"range \"%V\" overlaps \"%d.%d.%d.%d-%d.%d.%d.%d\"",
ctx->net,
h >> 8, h & 0xff, s >> 8, s & 0xff,
h >> 8, h & 0xff, e >> 8, e & 0xff);
return NGX_CONF_ERROR;
}
range = ngx_array_push(a);
if (range == NULL) {
return NGX_CONF_ERROR;
}
range = a->elts;
ngx_memmove(&range[1], &range[0],
(a->nelts - 1) * sizeof(ngx_http_geo_range_t));
range[0].start = (u_short) s;
range[0].end = (u_short) e;
range[0].value = ctx->value;
next:
if (h == 0xffff) {
break;
}
}
return NGX_CONF_OK;
} | ['static char *\nngx_http_geo_add_range(ngx_conf_t *cf, ngx_http_geo_conf_ctx_t *ctx,\n in_addr_t start, in_addr_t end)\n{\n in_addr_t n;\n ngx_uint_t h, i, s, e;\n ngx_array_t *a;\n ngx_http_geo_range_t *range;\n for (n = start; n <= end; n = (n + 0x10000) & 0xffff0000) {\n h = n >> 16;\n if (n == start) {\n s = n & 0xffff;\n } else {\n s = 0;\n }\n if ((n | 0xffff) > end) {\n e = end & 0xffff;\n } else {\n e = 0xffff;\n }\n a = (ngx_array_t *) ctx->high.low[h];\n if (a == NULL) {\n a = ngx_array_create(ctx->temp_pool, 64,\n sizeof(ngx_http_geo_range_t));\n if (a == NULL) {\n return NGX_CONF_ERROR;\n }\n ctx->high.low[h] = (ngx_http_geo_range_t *) a;\n }\n i = a->nelts;\n range = a->elts;\n while (i) {\n i--;\n if (e < (ngx_uint_t) range[i].start) {\n continue;\n }\n if (s > (ngx_uint_t) range[i].end) {\n range = ngx_array_push(a);\n if (range == NULL) {\n return NGX_CONF_ERROR;\n }\n range = a->elts;\n ngx_memmove(&range[i + 2], &range[i + 1],\n (a->nelts - 2 - i) * sizeof(ngx_http_geo_range_t));\n range[i + 1].start = (u_short) s;\n range[i + 1].end = (u_short) e;\n range[i + 1].value = ctx->value;\n goto next;\n }\n if (s == (ngx_uint_t) range[i].start\n && e == (ngx_uint_t) range[i].end)\n {\n ngx_conf_log_error(NGX_LOG_WARN, cf, 0,\n "duplicate range \\"%V\\", value: \\"%v\\", old value: \\"%v\\"",\n ctx->net, ctx->value, range[i].value);\n range[i].value = ctx->value;\n goto next;\n }\n if (s > (ngx_uint_t) range[i].start\n && e < (ngx_uint_t) range[i].end)\n {\n range = ngx_array_push(a);\n if (range == NULL) {\n return NGX_CONF_ERROR;\n }\n range = ngx_array_push(a);\n if (range == NULL) {\n return NGX_CONF_ERROR;\n }\n range = a->elts;\n ngx_memmove(&range[i + 3], &range[i + 1],\n (a->nelts - 3 - i) * sizeof(ngx_http_geo_range_t));\n range[i + 2].start = (u_short) (e + 1);\n range[i + 2].end = range[i].end;\n range[i + 2].value = range[i].value;\n range[i + 1].start = (u_short) s;\n range[i + 1].end = (u_short) e;\n range[i + 1].value = ctx->value;\n range[i].end = (u_short) (s - 1);\n goto next;\n }\n if (s == (ngx_uint_t) range[i].start\n && e < (ngx_uint_t) range[i].end)\n {\n range = ngx_array_push(a);\n if (range == NULL) {\n return NGX_CONF_ERROR;\n }\n range = a->elts;\n ngx_memmove(&range[i + 1], &range[i],\n (a->nelts - 1 - i) * sizeof(ngx_http_geo_range_t));\n range[i + 1].start = (u_short) (e + 1);\n range[i].start = (u_short) s;\n range[i].end = (u_short) e;\n range[i].value = ctx->value;\n goto next;\n }\n if (s > (ngx_uint_t) range[i].start\n && e == (ngx_uint_t) range[i].end)\n {\n range = ngx_array_push(a);\n if (range == NULL) {\n return NGX_CONF_ERROR;\n }\n range = a->elts;\n ngx_memmove(&range[i + 2], &range[i + 1],\n (a->nelts - 2 - i) * sizeof(ngx_http_geo_range_t));\n range[i + 1].start = (u_short) s;\n range[i + 1].end = (u_short) e;\n range[i + 1].value = ctx->value;\n range[i].end = (u_short) (s - 1);\n goto next;\n }\n s = (ngx_uint_t) range[i].start;\n e = (ngx_uint_t) range[i].end;\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "range \\"%V\\" overlaps \\"%d.%d.%d.%d-%d.%d.%d.%d\\"",\n ctx->net,\n h >> 8, h & 0xff, s >> 8, s & 0xff,\n h >> 8, h & 0xff, e >> 8, e & 0xff);\n return NGX_CONF_ERROR;\n }\n range = ngx_array_push(a);\n if (range == NULL) {\n return NGX_CONF_ERROR;\n }\n range = a->elts;\n ngx_memmove(&range[1], &range[0],\n (a->nelts - 1) * sizeof(ngx_http_geo_range_t));\n range[0].start = (u_short) s;\n range[0].end = (u_short) e;\n range[0].value = ctx->value;\n next:\n if (h == 0xffff) {\n break;\n }\n }\n return NGX_CONF_OK;\n}', 'ngx_array_t *\nngx_array_create(ngx_pool_t *p, ngx_uint_t n, size_t size)\n{\n ngx_array_t *a;\n a = ngx_palloc(p, sizeof(ngx_array_t));\n if (a == NULL) {\n return NULL;\n }\n if (ngx_array_init(a, p, n, size) != NGX_OK) {\n return NULL;\n }\n return a;\n}', 'static ngx_inline ngx_int_t\nngx_array_init(ngx_array_t *array, ngx_pool_t *pool, ngx_uint_t n, size_t size)\n{\n array->nelts = 0;\n array->size = size;\n array->nalloc = n;\n array->pool = pool;\n array->elts = ngx_palloc(pool, n * size);\n if (array->elts == NULL) {\n return NGX_ERROR;\n }\n return NGX_OK;\n}'] |
588 | 0 | https://github.com/libav/libav/blob/1db9da523815beb8e9fdcbc63205b3473616c6f0/ffmpeg.c/#L3290 | static void opt_output_file(const char *filename)
{
AVFormatContext *oc;
int use_video, use_audio, use_subtitle;
int input_has_video, input_has_audio, input_has_subtitle;
AVFormatParameters params, *ap = ¶ms;
if (!strcmp(filename, "-"))
filename = "pipe:";
oc = av_alloc_format_context();
if (!file_oformat) {
file_oformat = guess_format(NULL, filename, NULL);
if (!file_oformat) {
fprintf(stderr, "Unable to find a suitable output format for '%s'\n",
filename);
av_exit(1);
}
}
oc->oformat = file_oformat;
av_strlcpy(oc->filename, filename, sizeof(oc->filename));
if (!strcmp(file_oformat->name, "ffm") &&
av_strstart(filename, "http:", NULL)) {
int err = read_ffserver_streams(oc, filename);
if (err < 0) {
print_error(filename, err);
av_exit(1);
}
} else {
use_video = file_oformat->video_codec != CODEC_ID_NONE || video_stream_copy || video_codec_name;
use_audio = file_oformat->audio_codec != CODEC_ID_NONE || audio_stream_copy || audio_codec_name;
use_subtitle = file_oformat->subtitle_codec != CODEC_ID_NONE || subtitle_stream_copy || subtitle_codec_name;
if (nb_input_files > 0) {
check_audio_video_sub_inputs(&input_has_video, &input_has_audio,
&input_has_subtitle);
if (!input_has_video)
use_video = 0;
if (!input_has_audio)
use_audio = 0;
if (!input_has_subtitle)
use_subtitle = 0;
}
if (audio_disable) {
use_audio = 0;
}
if (video_disable) {
use_video = 0;
}
if (subtitle_disable) {
use_subtitle = 0;
}
if (use_video) {
new_video_stream(oc);
}
if (use_audio) {
new_audio_stream(oc);
}
if (use_subtitle) {
new_subtitle_stream(oc);
}
oc->timestamp = rec_timestamp;
if (str_title)
av_strlcpy(oc->title, str_title, sizeof(oc->title));
if (str_author)
av_strlcpy(oc->author, str_author, sizeof(oc->author));
if (str_copyright)
av_strlcpy(oc->copyright, str_copyright, sizeof(oc->copyright));
if (str_comment)
av_strlcpy(oc->comment, str_comment, sizeof(oc->comment));
if (str_album)
av_strlcpy(oc->album, str_album, sizeof(oc->album));
if (str_genre)
av_strlcpy(oc->genre, str_genre, sizeof(oc->genre));
}
output_files[nb_output_files++] = oc;
if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
if (!av_filename_number_test(oc->filename)) {
print_error(oc->filename, AVERROR_NUMEXPECTED);
av_exit(1);
}
}
if (!(oc->oformat->flags & AVFMT_NOFILE)) {
if (!file_overwrite &&
(strchr(filename, ':') == NULL ||
filename[1] == ':' ||
av_strstart(filename, "file:", NULL))) {
if (url_exist(filename)) {
int c;
if (!using_stdin) {
fprintf(stderr,"File '%s' already exists. Overwrite ? [y/N] ", filename);
fflush(stderr);
c = getchar();
if (toupper(c) != 'Y') {
fprintf(stderr, "Not overwriting - exiting\n");
av_exit(1);
}
}
else {
fprintf(stderr,"File '%s' already exists. Exiting.\n", filename);
av_exit(1);
}
}
}
if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
fprintf(stderr, "Could not open '%s'\n", filename);
av_exit(1);
}
}
memset(ap, 0, sizeof(*ap));
if (av_set_parameters(oc, ap) < 0) {
fprintf(stderr, "%s: Invalid encoding parameters\n",
oc->filename);
av_exit(1);
}
oc->preload= (int)(mux_preload*AV_TIME_BASE);
oc->max_delay= (int)(mux_max_delay*AV_TIME_BASE);
oc->loop_output = loop_output;
set_context_opts(oc, avformat_opts, AV_OPT_FLAG_ENCODING_PARAM);
file_oformat = NULL;
file_iformat = NULL;
} | ['static void opt_output_file(const char *filename)\n{\n AVFormatContext *oc;\n int use_video, use_audio, use_subtitle;\n int input_has_video, input_has_audio, input_has_subtitle;\n AVFormatParameters params, *ap = ¶ms;\n if (!strcmp(filename, "-"))\n filename = "pipe:";\n oc = av_alloc_format_context();\n if (!file_oformat) {\n file_oformat = guess_format(NULL, filename, NULL);\n if (!file_oformat) {\n fprintf(stderr, "Unable to find a suitable output format for \'%s\'\\n",\n filename);\n av_exit(1);\n }\n }\n oc->oformat = file_oformat;\n av_strlcpy(oc->filename, filename, sizeof(oc->filename));\n if (!strcmp(file_oformat->name, "ffm") &&\n av_strstart(filename, "http:", NULL)) {\n int err = read_ffserver_streams(oc, filename);\n if (err < 0) {\n print_error(filename, err);\n av_exit(1);\n }\n } else {\n use_video = file_oformat->video_codec != CODEC_ID_NONE || video_stream_copy || video_codec_name;\n use_audio = file_oformat->audio_codec != CODEC_ID_NONE || audio_stream_copy || audio_codec_name;\n use_subtitle = file_oformat->subtitle_codec != CODEC_ID_NONE || subtitle_stream_copy || subtitle_codec_name;\n if (nb_input_files > 0) {\n check_audio_video_sub_inputs(&input_has_video, &input_has_audio,\n &input_has_subtitle);\n if (!input_has_video)\n use_video = 0;\n if (!input_has_audio)\n use_audio = 0;\n if (!input_has_subtitle)\n use_subtitle = 0;\n }\n if (audio_disable) {\n use_audio = 0;\n }\n if (video_disable) {\n use_video = 0;\n }\n if (subtitle_disable) {\n use_subtitle = 0;\n }\n if (use_video) {\n new_video_stream(oc);\n }\n if (use_audio) {\n new_audio_stream(oc);\n }\n if (use_subtitle) {\n new_subtitle_stream(oc);\n }\n oc->timestamp = rec_timestamp;\n if (str_title)\n av_strlcpy(oc->title, str_title, sizeof(oc->title));\n if (str_author)\n av_strlcpy(oc->author, str_author, sizeof(oc->author));\n if (str_copyright)\n av_strlcpy(oc->copyright, str_copyright, sizeof(oc->copyright));\n if (str_comment)\n av_strlcpy(oc->comment, str_comment, sizeof(oc->comment));\n if (str_album)\n av_strlcpy(oc->album, str_album, sizeof(oc->album));\n if (str_genre)\n av_strlcpy(oc->genre, str_genre, sizeof(oc->genre));\n }\n output_files[nb_output_files++] = oc;\n if (oc->oformat->flags & AVFMT_NEEDNUMBER) {\n if (!av_filename_number_test(oc->filename)) {\n print_error(oc->filename, AVERROR_NUMEXPECTED);\n av_exit(1);\n }\n }\n if (!(oc->oformat->flags & AVFMT_NOFILE)) {\n if (!file_overwrite &&\n (strchr(filename, \':\') == NULL ||\n filename[1] == \':\' ||\n av_strstart(filename, "file:", NULL))) {\n if (url_exist(filename)) {\n int c;\n if (!using_stdin) {\n fprintf(stderr,"File \'%s\' already exists. Overwrite ? [y/N] ", filename);\n fflush(stderr);\n c = getchar();\n if (toupper(c) != \'Y\') {\n fprintf(stderr, "Not overwriting - exiting\\n");\n av_exit(1);\n }\n }\n else {\n fprintf(stderr,"File \'%s\' already exists. Exiting.\\n", filename);\n av_exit(1);\n }\n }\n }\n if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {\n fprintf(stderr, "Could not open \'%s\'\\n", filename);\n av_exit(1);\n }\n }\n memset(ap, 0, sizeof(*ap));\n if (av_set_parameters(oc, ap) < 0) {\n fprintf(stderr, "%s: Invalid encoding parameters\\n",\n oc->filename);\n av_exit(1);\n }\n oc->preload= (int)(mux_preload*AV_TIME_BASE);\n oc->max_delay= (int)(mux_max_delay*AV_TIME_BASE);\n oc->loop_output = loop_output;\n set_context_opts(oc, avformat_opts, AV_OPT_FLAG_ENCODING_PARAM);\n file_oformat = NULL;\n file_iformat = NULL;\n}', 'AVFormatContext *av_alloc_format_context(void)\n{\n AVFormatContext *ic;\n ic = av_malloc(sizeof(AVFormatContext));\n if (!ic) return ic;\n avformat_get_context_defaults(ic);\n ic->av_class = &av_format_context_class;\n return ic;\n}', 'void *av_malloc(unsigned int size)\n{\n void *ptr;\n#ifdef CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-16) )\n return NULL;\n#ifdef CONFIG_MEMALIGN_HACK\n ptr = malloc(size+16);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&15) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif defined (HAVE_MEMALIGN)\n ptr = memalign(16,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}', 'size_t av_strlcpy(char *dst, const char *src, size_t size)\n{\n size_t len = 0;\n while (++len < size && *src)\n *dst++ = *src++;\n if (len <= size)\n *dst = 0;\n return len + strlen(src) - 1;\n}'] |
589 | 0 | https://github.com/openssl/openssl/blob/f38edcab594b4934bd9625ef889934b2dfb5d1f0/crypto/bn/bn_lib.c/#L291 | BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
{
bn_check_top(b);
if (a == b)
return a;
if (bn_wexpand(a, b->top) == NULL)
return NULL;
if (b->top > 0)
memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);
a->neg = b->neg;
a->top = b->top;
a->flags |= b->flags & BN_FLG_FIXED_TOP;
bn_check_top(a);
return a;
} | ['int sm2_decrypt(const EC_KEY *key,\n const EVP_MD *digest,\n const uint8_t *ciphertext,\n size_t ciphertext_len, uint8_t *ptext_buf, size_t *ptext_len)\n{\n int rc = 0;\n int i;\n BN_CTX *ctx = NULL;\n const EC_GROUP *group = EC_KEY_get0_group(key);\n EC_POINT *C1 = NULL;\n struct SM2_Ciphertext_st *sm2_ctext = NULL;\n BIGNUM *x2 = NULL;\n BIGNUM *y2 = NULL;\n uint8_t *x2y2 = NULL;\n uint8_t *computed_C3 = NULL;\n const size_t field_size = ec_field_size(group);\n const int hash_size = EVP_MD_size(digest);\n uint8_t *msg_mask = NULL;\n const uint8_t *C2 = NULL;\n const uint8_t *C3 = NULL;\n int msg_len = 0;\n EVP_MD_CTX *hash = NULL;\n if (field_size == 0 || hash_size <= 0)\n goto done;\n memset(ptext_buf, 0xFF, *ptext_len);\n sm2_ctext = d2i_SM2_Ciphertext(NULL, &ciphertext, ciphertext_len);\n if (sm2_ctext == NULL) {\n SM2err(SM2_F_SM2_DECRYPT, SM2_R_ASN1_ERROR);\n goto done;\n }\n if (sm2_ctext->C3->length != hash_size) {\n SM2err(SM2_F_SM2_DECRYPT, SM2_R_INVALID_ENCODING);\n goto done;\n }\n C2 = sm2_ctext->C2->data;\n C3 = sm2_ctext->C3->data;\n msg_len = sm2_ctext->C2->length;\n ctx = BN_CTX_new();\n if (ctx == NULL) {\n SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);\n goto done;\n }\n BN_CTX_start(ctx);\n x2 = BN_CTX_get(ctx);\n y2 = BN_CTX_get(ctx);\n if (y2 == NULL) {\n SM2err(SM2_F_SM2_DECRYPT, ERR_R_BN_LIB);\n goto done;\n }\n msg_mask = OPENSSL_zalloc(msg_len);\n x2y2 = OPENSSL_zalloc(2 * field_size);\n computed_C3 = OPENSSL_zalloc(hash_size);\n if (msg_mask == NULL || x2y2 == NULL || computed_C3 == NULL) {\n SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);\n goto done;\n }\n C1 = EC_POINT_new(group);\n if (C1 == NULL) {\n SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);\n goto done;\n }\n if (!EC_POINT_set_affine_coordinates(group, C1, sm2_ctext->C1x,\n sm2_ctext->C1y, ctx)\n || !EC_POINT_mul(group, C1, NULL, C1, EC_KEY_get0_private_key(key),\n ctx)\n || !EC_POINT_get_affine_coordinates(group, C1, x2, y2, ctx)) {\n SM2err(SM2_F_SM2_DECRYPT, ERR_R_EC_LIB);\n goto done;\n }\n if (BN_bn2binpad(x2, x2y2, field_size) < 0\n || BN_bn2binpad(y2, x2y2 + field_size, field_size) < 0\n || !ECDH_KDF_X9_62(msg_mask, msg_len, x2y2, 2 * field_size, NULL, 0,\n digest)) {\n SM2err(SM2_F_SM2_DECRYPT, ERR_R_INTERNAL_ERROR);\n goto done;\n }\n for (i = 0; i != msg_len; ++i)\n ptext_buf[i] = C2[i] ^ msg_mask[i];\n hash = EVP_MD_CTX_new();\n if (hash == NULL) {\n SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);\n goto done;\n }\n if (!EVP_DigestInit(hash, digest)\n || !EVP_DigestUpdate(hash, x2y2, field_size)\n || !EVP_DigestUpdate(hash, ptext_buf, msg_len)\n || !EVP_DigestUpdate(hash, x2y2 + field_size, field_size)\n || !EVP_DigestFinal(hash, computed_C3, NULL)) {\n SM2err(SM2_F_SM2_DECRYPT, ERR_R_EVP_LIB);\n goto done;\n }\n if (CRYPTO_memcmp(computed_C3, C3, hash_size) != 0) {\n SM2err(SM2_F_SM2_DECRYPT, SM2_R_INVALID_DIGEST);\n goto done;\n }\n rc = 1;\n *ptext_len = msg_len;\n done:\n if (rc == 0)\n memset(ptext_buf, 0, *ptext_len);\n OPENSSL_free(msg_mask);\n OPENSSL_free(x2y2);\n OPENSSL_free(computed_C3);\n EC_POINT_free(C1);\n BN_CTX_free(ctx);\n SM2_Ciphertext_free(sm2_ctext);\n EVP_MD_CTX_free(hash);\n return rc;\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}', 'int ec_scalar_mul_ladder(const EC_GROUP *group, EC_POINT *r,\n const BIGNUM *scalar, const EC_POINT *point,\n BN_CTX *ctx)\n{\n int i, cardinality_bits, group_top, kbit, pbit, Z_is_one;\n EC_POINT *p = NULL;\n EC_POINT *s = NULL;\n BIGNUM *k = NULL;\n BIGNUM *lambda = NULL;\n BIGNUM *cardinality = NULL;\n int ret = 0;\n if (point != NULL && EC_POINT_is_at_infinity(group, point))\n return EC_POINT_set_to_infinity(group, r);\n if (BN_is_zero(group->order)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, EC_R_UNKNOWN_ORDER);\n return 0;\n }\n if (BN_is_zero(group->cofactor)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, EC_R_UNKNOWN_COFACTOR);\n return 0;\n }\n BN_CTX_start(ctx);\n if (((p = EC_POINT_new(group)) == NULL)\n || ((s = EC_POINT_new(group)) == NULL)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (point == NULL) {\n if (!EC_POINT_copy(p, group->generator)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_EC_LIB);\n goto err;\n }\n } else {\n if (!EC_POINT_copy(p, point)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_EC_LIB);\n goto err;\n }\n }\n EC_POINT_BN_set_flags(p, BN_FLG_CONSTTIME);\n EC_POINT_BN_set_flags(r, BN_FLG_CONSTTIME);\n EC_POINT_BN_set_flags(s, BN_FLG_CONSTTIME);\n cardinality = BN_CTX_get(ctx);\n lambda = BN_CTX_get(ctx);\n k = BN_CTX_get(ctx);\n if (k == NULL) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (!BN_mul(cardinality, group->order, group->cofactor, ctx)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_BN_LIB);\n goto err;\n }\n cardinality_bits = BN_num_bits(cardinality);\n group_top = bn_get_top(cardinality);\n if ((bn_wexpand(k, group_top + 1) == NULL)\n || (bn_wexpand(lambda, group_top + 1) == NULL)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_BN_LIB);\n goto err;\n }\n if (!BN_copy(k, scalar)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_BN_LIB);\n goto err;\n }\n BN_set_flags(k, BN_FLG_CONSTTIME);\n if ((BN_num_bits(k) > cardinality_bits) || (BN_is_negative(k))) {\n if (!BN_nnmod(k, k, cardinality, ctx)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_BN_LIB);\n goto err;\n }\n }\n if (!BN_add(lambda, k, cardinality)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_BN_LIB);\n goto err;\n }\n BN_set_flags(lambda, BN_FLG_CONSTTIME);\n if (!BN_add(k, lambda, cardinality)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_BN_LIB);\n goto err;\n }\n kbit = BN_is_bit_set(lambda, cardinality_bits);\n BN_consttime_swap(kbit, k, lambda, group_top + 1);\n group_top = bn_get_top(group->field);\n if ((bn_wexpand(s->X, group_top) == NULL)\n || (bn_wexpand(s->Y, group_top) == NULL)\n || (bn_wexpand(s->Z, group_top) == NULL)\n || (bn_wexpand(r->X, group_top) == NULL)\n || (bn_wexpand(r->Y, group_top) == NULL)\n || (bn_wexpand(r->Z, group_top) == NULL)\n || (bn_wexpand(p->X, group_top) == NULL)\n || (bn_wexpand(p->Y, group_top) == NULL)\n || (bn_wexpand(p->Z, group_top) == NULL)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_BN_LIB);\n goto err;\n }\n if (!ec_point_blind_coordinates(group, p, ctx)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, EC_R_POINT_COORDINATES_BLIND_FAILURE);\n goto err;\n }\n if (!ec_point_ladder_pre(group, r, s, p, ctx)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, EC_R_LADDER_PRE_FAILURE);\n goto err;\n }\n pbit = 1;\n#define EC_POINT_CSWAP(c, a, b, w, t) do { \\\n BN_consttime_swap(c, (a)->X, (b)->X, w); \\\n BN_consttime_swap(c, (a)->Y, (b)->Y, w); \\\n BN_consttime_swap(c, (a)->Z, (b)->Z, w); \\\n t = ((a)->Z_is_one ^ (b)->Z_is_one) & (c); \\\n (a)->Z_is_one ^= (t); \\\n (b)->Z_is_one ^= (t); \\\n} while(0)\n for (i = cardinality_bits - 1; i >= 0; i--) {\n kbit = BN_is_bit_set(k, i) ^ pbit;\n EC_POINT_CSWAP(kbit, r, s, group_top, Z_is_one);\n if (!ec_point_ladder_step(group, r, s, p, ctx)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, EC_R_LADDER_STEP_FAILURE);\n goto err;\n }\n pbit ^= kbit;\n }\n EC_POINT_CSWAP(pbit, r, s, group_top, Z_is_one);\n#undef EC_POINT_CSWAP\n if (!ec_point_ladder_post(group, r, s, p, ctx)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, EC_R_LADDER_POST_FAILURE);\n goto err;\n }\n ret = 1;\n err:\n EC_POINT_free(p);\n EC_POINT_free(s);\n BN_CTX_end(ctx);\n return ret;\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n bn_check_top(b);\n if (a == b)\n return a;\n if (bn_wexpand(a, b->top) == NULL)\n return NULL;\n if (b->top > 0)\n memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n a->neg = b->neg;\n a->top = b->top;\n a->flags |= b->flags & BN_FLG_FIXED_TOP;\n bn_check_top(a);\n return a;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}'] |
590 | 0 | https://github.com/libav/libav/blob/4860abb116674c7be31e825db05cdcfd30f3aff2/libavcodec/mpegvideo_enc.c/#L3121 | int dct_quantize_trellis_c(MpegEncContext *s,
DCTELEM *block, int n,
int qscale, int *overflow){
const int *qmat;
const uint8_t *scantable= s->intra_scantable.scantable;
const uint8_t *perm_scantable= s->intra_scantable.permutated;
int max=0;
unsigned int threshold1, threshold2;
int bias=0;
int run_tab[65];
int level_tab[65];
int score_tab[65];
int survivor[65];
int survivor_count;
int last_run=0;
int last_level=0;
int last_score= 0;
int last_i;
int coeff[2][64];
int coeff_count[64];
int qmul, qadd, start_i, last_non_zero, i, dc;
const int esc_length= s->ac_esc_length;
uint8_t * length;
uint8_t * last_length;
const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
s->dsp.fdct (block);
if(s->dct_error_sum)
s->denoise_dct(s, block);
qmul= qscale*16;
qadd= ((qscale-1)|1)*8;
if (s->mb_intra) {
int q;
if (!s->h263_aic) {
if (n < 4)
q = s->y_dc_scale;
else
q = s->c_dc_scale;
q = q << 3;
} else{
q = 1 << 3;
qadd=0;
}
block[0] = (block[0] + (q >> 1)) / q;
start_i = 1;
last_non_zero = 0;
qmat = s->q_intra_matrix[qscale];
if(s->mpeg_quant || s->out_format == FMT_MPEG1)
bias= 1<<(QMAT_SHIFT-1);
length = s->intra_ac_vlc_length;
last_length= s->intra_ac_vlc_last_length;
} else {
start_i = 0;
last_non_zero = -1;
qmat = s->q_inter_matrix[qscale];
length = s->inter_ac_vlc_length;
last_length= s->inter_ac_vlc_last_length;
}
last_i= start_i;
threshold1= (1<<QMAT_SHIFT) - bias - 1;
threshold2= (threshold1<<1);
for(i=63; i>=start_i; i--) {
const int j = scantable[i];
int level = block[j] * qmat[j];
if(((unsigned)(level+threshold1))>threshold2){
last_non_zero = i;
break;
}
}
for(i=start_i; i<=last_non_zero; i++) {
const int j = scantable[i];
int level = block[j] * qmat[j];
if(((unsigned)(level+threshold1))>threshold2){
if(level>0){
level= (bias + level)>>QMAT_SHIFT;
coeff[0][i]= level;
coeff[1][i]= level-1;
}else{
level= (bias - level)>>QMAT_SHIFT;
coeff[0][i]= -level;
coeff[1][i]= -level+1;
}
coeff_count[i]= FFMIN(level, 2);
assert(coeff_count[i]);
max |=level;
}else{
coeff[0][i]= (level>>31)|1;
coeff_count[i]= 1;
}
}
*overflow= s->max_qcoeff < max;
if(last_non_zero < start_i){
memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
return last_non_zero;
}
score_tab[start_i]= 0;
survivor[0]= start_i;
survivor_count= 1;
for(i=start_i; i<=last_non_zero; i++){
int level_index, j, zero_distortion;
int dct_coeff= FFABS(block[ scantable[i] ]);
int best_score=256*256*256*120;
if ( s->dsp.fdct == fdct_ifast
#ifndef FAAN_POSTSCALE
|| s->dsp.fdct == ff_faandct
#endif
)
dct_coeff= (dct_coeff*inv_aanscales[ scantable[i] ]) >> 12;
zero_distortion= dct_coeff*dct_coeff;
for(level_index=0; level_index < coeff_count[i]; level_index++){
int distortion;
int level= coeff[level_index][i];
const int alevel= FFABS(level);
int unquant_coeff;
assert(level);
if(s->out_format == FMT_H263){
unquant_coeff= alevel*qmul + qadd;
}else{
j= s->dsp.idct_permutation[ scantable[i] ];
if(s->mb_intra){
unquant_coeff = (int)( alevel * qscale * s->intra_matrix[j]) >> 3;
unquant_coeff = (unquant_coeff - 1) | 1;
}else{
unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
unquant_coeff = (unquant_coeff - 1) | 1;
}
unquant_coeff<<= 3;
}
distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
level+=64;
if((level&(~127)) == 0){
for(j=survivor_count-1; j>=0; j--){
int run= i - survivor[j];
int score= distortion + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
score += score_tab[i-run];
if(score < best_score){
best_score= score;
run_tab[i+1]= run;
level_tab[i+1]= level-64;
}
}
if(s->out_format == FMT_H263){
for(j=survivor_count-1; j>=0; j--){
int run= i - survivor[j];
int score= distortion + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
score += score_tab[i-run];
if(score < last_score){
last_score= score;
last_run= run;
last_level= level-64;
last_i= i+1;
}
}
}
}else{
distortion += esc_length*lambda;
for(j=survivor_count-1; j>=0; j--){
int run= i - survivor[j];
int score= distortion + score_tab[i-run];
if(score < best_score){
best_score= score;
run_tab[i+1]= run;
level_tab[i+1]= level-64;
}
}
if(s->out_format == FMT_H263){
for(j=survivor_count-1; j>=0; j--){
int run= i - survivor[j];
int score= distortion + score_tab[i-run];
if(score < last_score){
last_score= score;
last_run= run;
last_level= level-64;
last_i= i+1;
}
}
}
}
}
score_tab[i+1]= best_score;
if(last_non_zero <= 27){
for(; survivor_count; survivor_count--){
if(score_tab[ survivor[survivor_count-1] ] <= best_score)
break;
}
}else{
for(; survivor_count; survivor_count--){
if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
break;
}
}
survivor[ survivor_count++ ]= i+1;
}
if(s->out_format != FMT_H263){
last_score= 256*256*256*120;
for(i= survivor[0]; i<=last_non_zero + 1; i++){
int score= score_tab[i];
if(i) score += lambda*2;
if(score < last_score){
last_score= score;
last_i= i;
last_level= level_tab[i];
last_run= run_tab[i];
}
}
}
s->coded_score[n] = last_score;
dc= FFABS(block[0]);
last_non_zero= last_i - 1;
memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
if(last_non_zero < start_i)
return last_non_zero;
if(last_non_zero == 0 && start_i == 0){
int best_level= 0;
int best_score= dc * dc;
for(i=0; i<coeff_count[0]; i++){
int level= coeff[i][0];
int alevel= FFABS(level);
int unquant_coeff, score, distortion;
if(s->out_format == FMT_H263){
unquant_coeff= (alevel*qmul + qadd)>>3;
}else{
unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[0])) >> 4;
unquant_coeff = (unquant_coeff - 1) | 1;
}
unquant_coeff = (unquant_coeff + 4) >> 3;
unquant_coeff<<= 3 + 3;
distortion= (unquant_coeff - dc) * (unquant_coeff - dc);
level+=64;
if((level&(~127)) == 0) score= distortion + last_length[UNI_AC_ENC_INDEX(0, level)]*lambda;
else score= distortion + esc_length*lambda;
if(score < best_score){
best_score= score;
best_level= level - 64;
}
}
block[0]= best_level;
s->coded_score[n] = best_score - dc*dc;
if(best_level == 0) return -1;
else return last_non_zero;
}
i= last_i;
assert(last_level);
block[ perm_scantable[last_non_zero] ]= last_level;
i -= last_run + 1;
for(; i>start_i; i -= run_tab[i] + 1){
block[ perm_scantable[i-1] ]= level_tab[i];
}
return last_non_zero;
} | ['int dct_quantize_trellis_c(MpegEncContext *s,\n DCTELEM *block, int n,\n int qscale, int *overflow){\n const int *qmat;\n const uint8_t *scantable= s->intra_scantable.scantable;\n const uint8_t *perm_scantable= s->intra_scantable.permutated;\n int max=0;\n unsigned int threshold1, threshold2;\n int bias=0;\n int run_tab[65];\n int level_tab[65];\n int score_tab[65];\n int survivor[65];\n int survivor_count;\n int last_run=0;\n int last_level=0;\n int last_score= 0;\n int last_i;\n int coeff[2][64];\n int coeff_count[64];\n int qmul, qadd, start_i, last_non_zero, i, dc;\n const int esc_length= s->ac_esc_length;\n uint8_t * length;\n uint8_t * last_length;\n const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);\n s->dsp.fdct (block);\n if(s->dct_error_sum)\n s->denoise_dct(s, block);\n qmul= qscale*16;\n qadd= ((qscale-1)|1)*8;\n if (s->mb_intra) {\n int q;\n if (!s->h263_aic) {\n if (n < 4)\n q = s->y_dc_scale;\n else\n q = s->c_dc_scale;\n q = q << 3;\n } else{\n q = 1 << 3;\n qadd=0;\n }\n block[0] = (block[0] + (q >> 1)) / q;\n start_i = 1;\n last_non_zero = 0;\n qmat = s->q_intra_matrix[qscale];\n if(s->mpeg_quant || s->out_format == FMT_MPEG1)\n bias= 1<<(QMAT_SHIFT-1);\n length = s->intra_ac_vlc_length;\n last_length= s->intra_ac_vlc_last_length;\n } else {\n start_i = 0;\n last_non_zero = -1;\n qmat = s->q_inter_matrix[qscale];\n length = s->inter_ac_vlc_length;\n last_length= s->inter_ac_vlc_last_length;\n }\n last_i= start_i;\n threshold1= (1<<QMAT_SHIFT) - bias - 1;\n threshold2= (threshold1<<1);\n for(i=63; i>=start_i; i--) {\n const int j = scantable[i];\n int level = block[j] * qmat[j];\n if(((unsigned)(level+threshold1))>threshold2){\n last_non_zero = i;\n break;\n }\n }\n for(i=start_i; i<=last_non_zero; i++) {\n const int j = scantable[i];\n int level = block[j] * qmat[j];\n if(((unsigned)(level+threshold1))>threshold2){\n if(level>0){\n level= (bias + level)>>QMAT_SHIFT;\n coeff[0][i]= level;\n coeff[1][i]= level-1;\n }else{\n level= (bias - level)>>QMAT_SHIFT;\n coeff[0][i]= -level;\n coeff[1][i]= -level+1;\n }\n coeff_count[i]= FFMIN(level, 2);\n assert(coeff_count[i]);\n max |=level;\n }else{\n coeff[0][i]= (level>>31)|1;\n coeff_count[i]= 1;\n }\n }\n *overflow= s->max_qcoeff < max;\n if(last_non_zero < start_i){\n memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));\n return last_non_zero;\n }\n score_tab[start_i]= 0;\n survivor[0]= start_i;\n survivor_count= 1;\n for(i=start_i; i<=last_non_zero; i++){\n int level_index, j, zero_distortion;\n int dct_coeff= FFABS(block[ scantable[i] ]);\n int best_score=256*256*256*120;\n if ( s->dsp.fdct == fdct_ifast\n#ifndef FAAN_POSTSCALE\n || s->dsp.fdct == ff_faandct\n#endif\n )\n dct_coeff= (dct_coeff*inv_aanscales[ scantable[i] ]) >> 12;\n zero_distortion= dct_coeff*dct_coeff;\n for(level_index=0; level_index < coeff_count[i]; level_index++){\n int distortion;\n int level= coeff[level_index][i];\n const int alevel= FFABS(level);\n int unquant_coeff;\n assert(level);\n if(s->out_format == FMT_H263){\n unquant_coeff= alevel*qmul + qadd;\n }else{\n j= s->dsp.idct_permutation[ scantable[i] ];\n if(s->mb_intra){\n unquant_coeff = (int)( alevel * qscale * s->intra_matrix[j]) >> 3;\n unquant_coeff = (unquant_coeff - 1) | 1;\n }else{\n unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;\n unquant_coeff = (unquant_coeff - 1) | 1;\n }\n unquant_coeff<<= 3;\n }\n distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;\n level+=64;\n if((level&(~127)) == 0){\n for(j=survivor_count-1; j>=0; j--){\n int run= i - survivor[j];\n int score= distortion + length[UNI_AC_ENC_INDEX(run, level)]*lambda;\n score += score_tab[i-run];\n if(score < best_score){\n best_score= score;\n run_tab[i+1]= run;\n level_tab[i+1]= level-64;\n }\n }\n if(s->out_format == FMT_H263){\n for(j=survivor_count-1; j>=0; j--){\n int run= i - survivor[j];\n int score= distortion + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;\n score += score_tab[i-run];\n if(score < last_score){\n last_score= score;\n last_run= run;\n last_level= level-64;\n last_i= i+1;\n }\n }\n }\n }else{\n distortion += esc_length*lambda;\n for(j=survivor_count-1; j>=0; j--){\n int run= i - survivor[j];\n int score= distortion + score_tab[i-run];\n if(score < best_score){\n best_score= score;\n run_tab[i+1]= run;\n level_tab[i+1]= level-64;\n }\n }\n if(s->out_format == FMT_H263){\n for(j=survivor_count-1; j>=0; j--){\n int run= i - survivor[j];\n int score= distortion + score_tab[i-run];\n if(score < last_score){\n last_score= score;\n last_run= run;\n last_level= level-64;\n last_i= i+1;\n }\n }\n }\n }\n }\n score_tab[i+1]= best_score;\n if(last_non_zero <= 27){\n for(; survivor_count; survivor_count--){\n if(score_tab[ survivor[survivor_count-1] ] <= best_score)\n break;\n }\n }else{\n for(; survivor_count; survivor_count--){\n if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)\n break;\n }\n }\n survivor[ survivor_count++ ]= i+1;\n }\n if(s->out_format != FMT_H263){\n last_score= 256*256*256*120;\n for(i= survivor[0]; i<=last_non_zero + 1; i++){\n int score= score_tab[i];\n if(i) score += lambda*2;\n if(score < last_score){\n last_score= score;\n last_i= i;\n last_level= level_tab[i];\n last_run= run_tab[i];\n }\n }\n }\n s->coded_score[n] = last_score;\n dc= FFABS(block[0]);\n last_non_zero= last_i - 1;\n memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));\n if(last_non_zero < start_i)\n return last_non_zero;\n if(last_non_zero == 0 && start_i == 0){\n int best_level= 0;\n int best_score= dc * dc;\n for(i=0; i<coeff_count[0]; i++){\n int level= coeff[i][0];\n int alevel= FFABS(level);\n int unquant_coeff, score, distortion;\n if(s->out_format == FMT_H263){\n unquant_coeff= (alevel*qmul + qadd)>>3;\n }else{\n unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[0])) >> 4;\n unquant_coeff = (unquant_coeff - 1) | 1;\n }\n unquant_coeff = (unquant_coeff + 4) >> 3;\n unquant_coeff<<= 3 + 3;\n distortion= (unquant_coeff - dc) * (unquant_coeff - dc);\n level+=64;\n if((level&(~127)) == 0) score= distortion + last_length[UNI_AC_ENC_INDEX(0, level)]*lambda;\n else score= distortion + esc_length*lambda;\n if(score < best_score){\n best_score= score;\n best_level= level - 64;\n }\n }\n block[0]= best_level;\n s->coded_score[n] = best_score - dc*dc;\n if(best_level == 0) return -1;\n else return last_non_zero;\n }\n i= last_i;\n assert(last_level);\n block[ perm_scantable[last_non_zero] ]= last_level;\n i -= last_run + 1;\n for(; i>start_i; i -= run_tab[i] + 1){\n block[ perm_scantable[i-1] ]= level_tab[i];\n }\n return last_non_zero;\n}'] |
591 | 0 | https://github.com/openssl/openssl/blob/183733f882056ea3e6fe95e665b85fcc6a45dcb4/ssl/t1_lib.c/#L4320 | int ssl_security_cert_chain(SSL *s, STACK_OF(X509) *sk, X509 *x, int vfy)
{
int rv, start_idx, i;
if (x == NULL) {
x = sk_X509_value(sk, 0);
start_idx = 1;
} else
start_idx = 0;
rv = ssl_security_cert(s, NULL, x, vfy, 1);
if (rv != 1)
return rv;
for (i = start_idx; i < sk_X509_num(sk); i++) {
x = sk_X509_value(sk, i);
rv = ssl_security_cert(s, NULL, x, vfy, 0);
if (rv != 1)
return rv;
}
return 1;
} | ['int ssl_security_cert_chain(SSL *s, STACK_OF(X509) *sk, X509 *x, int vfy)\n{\n int rv, start_idx, i;\n if (x == NULL) {\n x = sk_X509_value(sk, 0);\n start_idx = 1;\n } else\n start_idx = 0;\n rv = ssl_security_cert(s, NULL, x, vfy, 1);\n if (rv != 1)\n return rv;\n for (i = start_idx; i < sk_X509_num(sk); i++) {\n x = sk_X509_value(sk, i);\n rv = ssl_security_cert(s, NULL, x, vfy, 0);\n if (rv != 1)\n return rv;\n }\n return 1;\n}', 'DEFINE_STACK_OF(X509)', 'void *sk_value(const _STACK *st, int i)\n{\n if (!st || (i < 0) || (i >= st->num))\n return NULL;\n return st->data[i];\n}'] |
592 | 0 | https://github.com/openssl/openssl/blob/e3713c365c2657236439fea00822a43aa396d112/test/evp_test.c/#L1562 | static int encode_test_run(EVP_TEST *t)
{
ENCODE_DATA *expected = t->data;
unsigned char *encode_out = NULL, *decode_out = NULL;
int output_len, chunk_len;
EVP_ENCODE_CTX *decode_ctx;
if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) {
t->err = "INTERNAL_ERROR";
goto err;
}
if (expected->encoding == BASE64_CANONICAL_ENCODING) {
EVP_ENCODE_CTX *encode_ctx;
if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new())
|| !TEST_ptr(encode_out =
OPENSSL_malloc(EVP_ENCODE_LENGTH(expected->input_len))))
goto err;
EVP_EncodeInit(encode_ctx);
EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
expected->input, expected->input_len);
output_len = chunk_len;
EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
output_len += chunk_len;
EVP_ENCODE_CTX_free(encode_ctx);
if (!TEST_mem_eq(expected->output, expected->output_len,
encode_out, output_len)) {
t->err = "BAD_ENCODING";
goto err;
}
}
if (!TEST_ptr(decode_out =
OPENSSL_malloc(EVP_DECODE_LENGTH(expected->output_len))))
goto err;
EVP_DecodeInit(decode_ctx);
if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, expected->output,
expected->output_len) < 0) {
t->err = "DECODE_ERROR";
goto err;
}
output_len = chunk_len;
if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
t->err = "DECODE_ERROR";
goto err;
}
output_len += chunk_len;
if (expected->encoding != BASE64_INVALID_ENCODING
&& !TEST_mem_eq(expected->input, expected->input_len,
decode_out, output_len)) {
t->err = "BAD_DECODING";
goto err;
}
t->err = NULL;
err:
OPENSSL_free(encode_out);
OPENSSL_free(decode_out);
EVP_ENCODE_CTX_free(decode_ctx);
return 1;
} | ['static int encode_test_run(EVP_TEST *t)\n{\n ENCODE_DATA *expected = t->data;\n unsigned char *encode_out = NULL, *decode_out = NULL;\n int output_len, chunk_len;\n EVP_ENCODE_CTX *decode_ctx;\n if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) {\n t->err = "INTERNAL_ERROR";\n goto err;\n }\n if (expected->encoding == BASE64_CANONICAL_ENCODING) {\n EVP_ENCODE_CTX *encode_ctx;\n if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new())\n || !TEST_ptr(encode_out =\n OPENSSL_malloc(EVP_ENCODE_LENGTH(expected->input_len))))\n goto err;\n EVP_EncodeInit(encode_ctx);\n EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,\n expected->input, expected->input_len);\n output_len = chunk_len;\n EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);\n output_len += chunk_len;\n EVP_ENCODE_CTX_free(encode_ctx);\n if (!TEST_mem_eq(expected->output, expected->output_len,\n encode_out, output_len)) {\n t->err = "BAD_ENCODING";\n goto err;\n }\n }\n if (!TEST_ptr(decode_out =\n OPENSSL_malloc(EVP_DECODE_LENGTH(expected->output_len))))\n goto err;\n EVP_DecodeInit(decode_ctx);\n if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, expected->output,\n expected->output_len) < 0) {\n t->err = "DECODE_ERROR";\n goto err;\n }\n output_len = chunk_len;\n if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {\n t->err = "DECODE_ERROR";\n goto err;\n }\n output_len += chunk_len;\n if (expected->encoding != BASE64_INVALID_ENCODING\n && !TEST_mem_eq(expected->input, expected->input_len,\n decode_out, output_len)) {\n t->err = "BAD_DECODING";\n goto err;\n }\n t->err = NULL;\n err:\n OPENSSL_free(encode_out);\n OPENSSL_free(decode_out);\n EVP_ENCODE_CTX_free(decode_ctx);\n return 1;\n}', 'EVP_ENCODE_CTX *EVP_ENCODE_CTX_new(void)\n{\n return OPENSSL_zalloc(sizeof(EVP_ENCODE_CTX));\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n INCREMENT(malloc_count);\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)(file); (void)(line);\n ret = malloc(num);\n#endif\n return ret;\n}', 'int test_ptr(const char *file, int line, const char *s, const void *p)\n{\n if (p != NULL)\n return 1;\n test_fail_message(NULL, file, line, "ptr", s, "NULL", "!=", "%p", p);\n return 0;\n}'] |
593 | 0 | https://github.com/openssl/openssl/blob/101e6e19f2accef4e4cdf10915a2a1c2e957b7e8/ssl/ssl_ciph.c/#L1748 | int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm)
{
SSL_COMP *comp;
if (cm == NULL || cm->type == NID_undef)
return 1;
if (id < 193 || id > 255)
{
SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD,SSL_R_COMPRESSION_ID_NOT_WITHIN_PRIVATE_RANGE);
return 0;
}
MemCheck_off();
comp=(SSL_COMP *)OPENSSL_malloc(sizeof(SSL_COMP));
comp->id=id;
comp->method=cm;
load_builtin_compressions();
if (ssl_comp_methods
&& sk_SSL_COMP_find(ssl_comp_methods,comp) >= 0)
{
OPENSSL_free(comp);
MemCheck_on();
SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD,SSL_R_DUPLICATE_COMPRESSION_ID);
return(1);
}
else if ((ssl_comp_methods == NULL)
|| !sk_SSL_COMP_push(ssl_comp_methods,comp))
{
OPENSSL_free(comp);
MemCheck_on();
SSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD,ERR_R_MALLOC_FAILURE);
return(1);
}
else
{
MemCheck_on();
return(0);
}
} | ['int SSL_COMP_add_compression_method(int id, COMP_METHOD *cm)\n\t{\n\tSSL_COMP *comp;\n if (cm == NULL || cm->type == NID_undef)\n return 1;\n\tif (id < 193 || id > 255)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD,SSL_R_COMPRESSION_ID_NOT_WITHIN_PRIVATE_RANGE);\n\t\treturn 0;\n\t\t}\n\tMemCheck_off();\n\tcomp=(SSL_COMP *)OPENSSL_malloc(sizeof(SSL_COMP));\n\tcomp->id=id;\n\tcomp->method=cm;\n\tload_builtin_compressions();\n\tif (ssl_comp_methods\n\t\t&& sk_SSL_COMP_find(ssl_comp_methods,comp) >= 0)\n\t\t{\n\t\tOPENSSL_free(comp);\n\t\tMemCheck_on();\n\t\tSSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD,SSL_R_DUPLICATE_COMPRESSION_ID);\n\t\treturn(1);\n\t\t}\n\telse if ((ssl_comp_methods == NULL)\n\t\t|| !sk_SSL_COMP_push(ssl_comp_methods,comp))\n\t\t{\n\t\tOPENSSL_free(comp);\n\t\tMemCheck_on();\n\t\tSSLerr(SSL_F_SSL_COMP_ADD_COMPRESSION_METHOD,ERR_R_MALLOC_FAILURE);\n\t\treturn(1);\n\t\t}\n\telse\n\t\t{\n\t\tMemCheck_on();\n\t\treturn(0);\n\t\t}\n\t}', 'int CRYPTO_mem_ctrl(int mode)\n\t{\n\tint ret=mh_mode;\n\tCRYPTO_w_lock(CRYPTO_LOCK_MALLOC);\n\tswitch (mode)\n\t\t{\n\tcase CRYPTO_MEM_CHECK_ON:\n\t\tmh_mode = CRYPTO_MEM_CHECK_ON|CRYPTO_MEM_CHECK_ENABLE;\n\t\tnum_disable = 0;\n\t\tbreak;\n\tcase CRYPTO_MEM_CHECK_OFF:\n\t\tmh_mode = 0;\n\t\tnum_disable = 0;\n\t\tbreak;\n\tcase CRYPTO_MEM_CHECK_DISABLE:\n\t\tif (mh_mode & CRYPTO_MEM_CHECK_ON)\n\t\t\t{\n\t\t\tCRYPTO_THREADID cur;\n\t\t\tCRYPTO_THREADID_current(&cur);\n\t\t\tif (!num_disable || CRYPTO_THREADID_cmp(&disabling_threadid, &cur))\n\t\t\t\t{\n\t\t\t\tCRYPTO_w_unlock(CRYPTO_LOCK_MALLOC);\n\t\t\t\tCRYPTO_w_lock(CRYPTO_LOCK_MALLOC2);\n\t\t\t\tCRYPTO_w_lock(CRYPTO_LOCK_MALLOC);\n\t\t\t\tmh_mode &= ~CRYPTO_MEM_CHECK_ENABLE;\n\t\t\t\tCRYPTO_THREADID_cpy(&disabling_threadid, &cur);\n\t\t\t\t}\n\t\t\tnum_disable++;\n\t\t\t}\n\t\tbreak;\n\tcase CRYPTO_MEM_CHECK_ENABLE:\n\t\tif (mh_mode & CRYPTO_MEM_CHECK_ON)\n\t\t\t{\n\t\t\tif (num_disable)\n\t\t\t\t{\n\t\t\t\tnum_disable--;\n\t\t\t\tif (num_disable == 0)\n\t\t\t\t\t{\n\t\t\t\t\tmh_mode|=CRYPTO_MEM_CHECK_ENABLE;\n\t\t\t\t\tCRYPTO_w_unlock(CRYPTO_LOCK_MALLOC2);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\tbreak;\n\tdefault:\n\t\tbreak;\n\t\t}\n\tCRYPTO_w_unlock(CRYPTO_LOCK_MALLOC);\n\treturn(ret);\n\t}', 'void CRYPTO_lock(int mode, int type, const char *file, int line)\n\t{\n#ifdef LOCK_DEBUG\n\t\t{\n\t\tCRYPTO_THREADID id;\n\t\tchar *rw_text,*operation_text;\n\t\tif (mode & CRYPTO_LOCK)\n\t\t\toperation_text="lock ";\n\t\telse if (mode & CRYPTO_UNLOCK)\n\t\t\toperation_text="unlock";\n\t\telse\n\t\t\toperation_text="ERROR ";\n\t\tif (mode & CRYPTO_READ)\n\t\t\trw_text="r";\n\t\telse if (mode & CRYPTO_WRITE)\n\t\t\trw_text="w";\n\t\telse\n\t\t\trw_text="ERROR";\n\t\tCRYPTO_THREADID_current(&id);\n\t\tfprintf(stderr,"lock:%08lx:(%s)%s %-18s %s:%d\\n",\n\t\t\tCRYPTO_THREADID_hash(&id), rw_text, operation_text,\n\t\t\tCRYPTO_get_lock_name(type), file, line);\n\t\t}\n#endif\n\tif (type < 0)\n\t\t{\n\t\tif (dynlock_lock_callback != NULL)\n\t\t\t{\n\t\t\tstruct CRYPTO_dynlock_value *pointer\n\t\t\t\t= CRYPTO_get_dynlock_value(type);\n\t\t\tOPENSSL_assert(pointer != NULL);\n\t\t\tdynlock_lock_callback(mode, pointer, file, line);\n\t\t\tCRYPTO_destroy_dynlockid(type);\n\t\t\t}\n\t\t}\n\telse\n\t\tif (locking_callback != NULL)\n\t\t\tlocking_callback(mode,type,file,line);\n\t}', 'void *CRYPTO_malloc(int num, const char *file, int line)\n\t{\n\tvoid *ret = NULL;\n\tif (num <= 0) return NULL;\n\tallow_customize = 0;\n\tif (malloc_debug_func != NULL)\n\t\t{\n\t\tallow_customize_debug = 0;\n\t\tmalloc_debug_func(NULL, num, file, line, 0);\n\t\t}\n\tret = malloc_ex_func(num,file,line);\n#ifdef LEVITTE_DEBUG_MEM\n\tfprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\\n", ret, num);\n#endif\n\tif (malloc_debug_func != NULL)\n\t\tmalloc_debug_func(ret, num, file, line, 1);\n#ifndef OPENSSL_CPUID_OBJ\n if(ret && (num > 2048))\n\t{\textern unsigned char cleanse_ctr;\n ((unsigned char *)ret)[0] = cleanse_ctr;\n\t}\n#endif\n\treturn ret;\n\t}'] |
594 | 0 | https://github.com/libav/libav/blob/dad7a9c7c0ae8ebc56f2e3a24e6fa4da5c2cd491/libavcodec/bitstream.h/#L237 | static inline void skip_remaining(BitstreamContext *bc, unsigned n)
{
#ifdef BITSTREAM_READER_LE
bc->bits >>= n;
#else
bc->bits <<= n;
#endif
bc->bits_left -= n;
} | ['static inline int get_ur_golomb_jpegls(BitstreamContext *bc, int k, int limit,\n int esc_len)\n{\n unsigned int buf;\n int log;\n buf = bitstream_peek(bc, 32);\n log = av_log2(buf);\n if (log - k >= 1 && 32 - log < limit) {\n buf >>= log - k;\n buf += (30 - log) << k;\n bitstream_skip(bc, 32 + k - log);\n return buf;\n } else {\n int i;\n for (i = 0; i < limit && bitstream_peek(bc, 1) == 0 && bitstream_bits_left(bc) > 0; i++)\n bitstream_skip(bc, 1);\n bitstream_skip(bc, 1);\n if (i < limit - 1) {\n if (k) {\n buf = bitstream_read(bc, k);\n } else {\n buf = 0;\n }\n return buf + (i << k);\n } else if (i == limit - 1) {\n buf = bitstream_read(bc, esc_len);\n return buf + 1;\n } else\n return -1;\n }\n}', 'static inline unsigned bitstream_peek(BitstreamContext *bc, unsigned n)\n{\n if (n > bc->bits_left)\n refill_32(bc);\n return show_val(bc, n);\n}', '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}'] |
595 | 0 | https://github.com/openssl/openssl/blob/a14715888bc4b5bd2b1da3f8ac7d4cabef8c9cb8/crypto/x509/x509_obj.c/#L56 | char *X509_NAME_oneline(const X509_NAME *a, char *buf, int len)
{
const X509_NAME_ENTRY *ne;
int i;
int n, lold, l, l1, l2, num, j, type;
const char *s;
char *p;
unsigned char *q;
BUF_MEM *b = NULL;
static const char hex[17] = "0123456789ABCDEF";
int gs_doit[4];
char tmp_buf[80];
#ifdef CHARSET_EBCDIC
unsigned char ebcdic_buf[1024];
#endif
if (buf == NULL) {
if ((b = BUF_MEM_new()) == NULL)
goto err;
if (!BUF_MEM_grow(b, 200))
goto err;
b->data[0] = '\0';
len = 200;
} else if (len == 0) {
return NULL;
}
if (a == NULL) {
if (b) {
buf = b->data;
OPENSSL_free(b);
}
strncpy(buf, "NO X509_NAME", len);
buf[len - 1] = '\0';
return buf;
}
len--;
l = 0;
for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
ne = sk_X509_NAME_ENTRY_value(a->entries, i);
n = OBJ_obj2nid(ne->object);
if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
i2t_ASN1_OBJECT(tmp_buf, sizeof(tmp_buf), ne->object);
s = tmp_buf;
}
l1 = strlen(s);
type = ne->value->type;
num = ne->value->length;
if (num > NAME_ONELINE_MAX) {
X509err(X509_F_X509_NAME_ONELINE, X509_R_NAME_TOO_LONG);
goto end;
}
q = ne->value->data;
#ifdef CHARSET_EBCDIC
if (type == V_ASN1_GENERALSTRING ||
type == V_ASN1_VISIBLESTRING ||
type == V_ASN1_PRINTABLESTRING ||
type == V_ASN1_TELETEXSTRING ||
type == V_ASN1_IA5STRING) {
if (num > (int)sizeof(ebcdic_buf))
num = sizeof(ebcdic_buf);
ascii2ebcdic(ebcdic_buf, q, num);
q = ebcdic_buf;
}
#endif
if ((type == V_ASN1_GENERALSTRING) && ((num % 4) == 0)) {
gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 0;
for (j = 0; j < num; j++)
if (q[j] != 0)
gs_doit[j & 3] = 1;
if (gs_doit[0] | gs_doit[1] | gs_doit[2])
gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1;
else {
gs_doit[0] = gs_doit[1] = gs_doit[2] = 0;
gs_doit[3] = 1;
}
} else
gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1;
for (l2 = j = 0; j < num; j++) {
if (!gs_doit[j & 3])
continue;
l2++;
#ifndef CHARSET_EBCDIC
if ((q[j] < ' ') || (q[j] > '~'))
l2 += 3;
#else
if ((os_toascii[q[j]] < os_toascii[' ']) ||
(os_toascii[q[j]] > os_toascii['~']))
l2 += 3;
#endif
}
lold = l;
l += 1 + l1 + 1 + l2;
if (l > NAME_ONELINE_MAX) {
X509err(X509_F_X509_NAME_ONELINE, X509_R_NAME_TOO_LONG);
goto end;
}
if (b != NULL) {
if (!BUF_MEM_grow(b, l + 1))
goto err;
p = &(b->data[lold]);
} else if (l > len) {
break;
} else
p = &(buf[lold]);
*(p++) = '/';
memcpy(p, s, (unsigned int)l1);
p += l1;
*(p++) = '=';
#ifndef CHARSET_EBCDIC
q = ne->value->data;
#endif
for (j = 0; j < num; j++) {
if (!gs_doit[j & 3])
continue;
#ifndef CHARSET_EBCDIC
n = q[j];
if ((n < ' ') || (n > '~')) {
*(p++) = '\\';
*(p++) = 'x';
*(p++) = hex[(n >> 4) & 0x0f];
*(p++) = hex[n & 0x0f];
} else
*(p++) = n;
#else
n = os_toascii[q[j]];
if ((n < os_toascii[' ']) || (n > os_toascii['~'])) {
*(p++) = '\\';
*(p++) = 'x';
*(p++) = hex[(n >> 4) & 0x0f];
*(p++) = hex[n & 0x0f];
} else
*(p++) = q[j];
#endif
}
*p = '\0';
}
if (b != NULL) {
p = b->data;
OPENSSL_free(b);
} else
p = buf;
if (i == 0)
*p = '\0';
return p;
err:
X509err(X509_F_X509_NAME_ONELINE, ERR_R_MALLOC_FAILURE);
end:
BUF_MEM_free(b);
return NULL;
} | ['static int verify_callback(int ok, X509_STORE_CTX *ctx)\n{\n char *s, buf[256];\n s = X509_NAME_oneline(X509_get_subject_name(X509_STORE_CTX_get_current_cert(ctx)),\n buf, sizeof(buf));\n if (s != NULL) {\n if (ok)\n printf("depth=%d %s\\n", X509_STORE_CTX_get_error_depth(ctx), buf);\n else {\n fprintf(stderr, "depth=%d error=%d %s\\n",\n X509_STORE_CTX_get_error_depth(ctx),\n X509_STORE_CTX_get_error(ctx), buf);\n }\n }\n if (ok == 0) {\n int i = X509_STORE_CTX_get_error(ctx);\n switch (i) {\n default:\n fprintf(stderr, "Error string: %s\\n",\n X509_verify_cert_error_string(i));\n break;\n case X509_V_ERR_CERT_NOT_YET_VALID:\n case X509_V_ERR_CERT_HAS_EXPIRED:\n case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:\n ok = 1;\n break;\n }\n }\n return ok;\n}', 'char *X509_NAME_oneline(const X509_NAME *a, char *buf, int len)\n{\n const X509_NAME_ENTRY *ne;\n int i;\n int n, lold, l, l1, l2, num, j, type;\n const char *s;\n char *p;\n unsigned char *q;\n BUF_MEM *b = NULL;\n static const char hex[17] = "0123456789ABCDEF";\n int gs_doit[4];\n char tmp_buf[80];\n#ifdef CHARSET_EBCDIC\n unsigned char ebcdic_buf[1024];\n#endif\n if (buf == NULL) {\n if ((b = BUF_MEM_new()) == NULL)\n goto err;\n if (!BUF_MEM_grow(b, 200))\n goto err;\n b->data[0] = \'\\0\';\n len = 200;\n } else if (len == 0) {\n return NULL;\n }\n if (a == NULL) {\n if (b) {\n buf = b->data;\n OPENSSL_free(b);\n }\n strncpy(buf, "NO X509_NAME", len);\n buf[len - 1] = \'\\0\';\n return buf;\n }\n len--;\n l = 0;\n for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {\n ne = sk_X509_NAME_ENTRY_value(a->entries, i);\n n = OBJ_obj2nid(ne->object);\n if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {\n i2t_ASN1_OBJECT(tmp_buf, sizeof(tmp_buf), ne->object);\n s = tmp_buf;\n }\n l1 = strlen(s);\n type = ne->value->type;\n num = ne->value->length;\n if (num > NAME_ONELINE_MAX) {\n X509err(X509_F_X509_NAME_ONELINE, X509_R_NAME_TOO_LONG);\n goto end;\n }\n q = ne->value->data;\n#ifdef CHARSET_EBCDIC\n if (type == V_ASN1_GENERALSTRING ||\n type == V_ASN1_VISIBLESTRING ||\n type == V_ASN1_PRINTABLESTRING ||\n type == V_ASN1_TELETEXSTRING ||\n type == V_ASN1_IA5STRING) {\n if (num > (int)sizeof(ebcdic_buf))\n num = sizeof(ebcdic_buf);\n ascii2ebcdic(ebcdic_buf, q, num);\n q = ebcdic_buf;\n }\n#endif\n if ((type == V_ASN1_GENERALSTRING) && ((num % 4) == 0)) {\n gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 0;\n for (j = 0; j < num; j++)\n if (q[j] != 0)\n gs_doit[j & 3] = 1;\n if (gs_doit[0] | gs_doit[1] | gs_doit[2])\n gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1;\n else {\n gs_doit[0] = gs_doit[1] = gs_doit[2] = 0;\n gs_doit[3] = 1;\n }\n } else\n gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1;\n for (l2 = j = 0; j < num; j++) {\n if (!gs_doit[j & 3])\n continue;\n l2++;\n#ifndef CHARSET_EBCDIC\n if ((q[j] < \' \') || (q[j] > \'~\'))\n l2 += 3;\n#else\n if ((os_toascii[q[j]] < os_toascii[\' \']) ||\n (os_toascii[q[j]] > os_toascii[\'~\']))\n l2 += 3;\n#endif\n }\n lold = l;\n l += 1 + l1 + 1 + l2;\n if (l > NAME_ONELINE_MAX) {\n X509err(X509_F_X509_NAME_ONELINE, X509_R_NAME_TOO_LONG);\n goto end;\n }\n if (b != NULL) {\n if (!BUF_MEM_grow(b, l + 1))\n goto err;\n p = &(b->data[lold]);\n } else if (l > len) {\n break;\n } else\n p = &(buf[lold]);\n *(p++) = \'/\';\n memcpy(p, s, (unsigned int)l1);\n p += l1;\n *(p++) = \'=\';\n#ifndef CHARSET_EBCDIC\n q = ne->value->data;\n#endif\n for (j = 0; j < num; j++) {\n if (!gs_doit[j & 3])\n continue;\n#ifndef CHARSET_EBCDIC\n n = q[j];\n if ((n < \' \') || (n > \'~\')) {\n *(p++) = \'\\\\\';\n *(p++) = \'x\';\n *(p++) = hex[(n >> 4) & 0x0f];\n *(p++) = hex[n & 0x0f];\n } else\n *(p++) = n;\n#else\n n = os_toascii[q[j]];\n if ((n < os_toascii[\' \']) || (n > os_toascii[\'~\'])) {\n *(p++) = \'\\\\\';\n *(p++) = \'x\';\n *(p++) = hex[(n >> 4) & 0x0f];\n *(p++) = hex[n & 0x0f];\n } else\n *(p++) = q[j];\n#endif\n }\n *p = \'\\0\';\n }\n if (b != NULL) {\n p = b->data;\n OPENSSL_free(b);\n } else\n p = buf;\n if (i == 0)\n *p = \'\\0\';\n return p;\n err:\n X509err(X509_F_X509_NAME_ONELINE, ERR_R_MALLOC_FAILURE);\n end:\n BUF_MEM_free(b);\n return NULL;\n}'] |
596 | 0 | https://github.com/libav/libav/blob/688417399c69aadd4c287bdb0dec82ef8799011c/libavcodec/hevcdsp_template.c/#L901 | PUT_HEVC_QPEL_HV(1, 1) | ['QPEL(16)', 'PUT_HEVC_QPEL_HV(1, 1)'] |
597 | 0 | https://github.com/libav/libav/blob/90f65dc6cf65d1c849a9fa372ac2dda427a258e2/avconv.c/#L4261 | static int opt_vstats(const char *opt, const char *arg)
{
char filename[40];
time_t today2 = time(NULL);
struct tm *today = localtime(&today2);
snprintf(filename, sizeof(filename), "vstats_%02d%02d%02d.log", today->tm_hour, today->tm_min,
today->tm_sec);
return opt_vstats_file(opt, filename);
} | ['static int opt_vstats(const char *opt, const char *arg)\n{\n char filename[40];\n time_t today2 = time(NULL);\n struct tm *today = localtime(&today2);\n snprintf(filename, sizeof(filename), "vstats_%02d%02d%02d.log", today->tm_hour, today->tm_min,\n today->tm_sec);\n return opt_vstats_file(opt, filename);\n}'] |
598 | 0 | https://github.com/libav/libav/blob/acf91215c74a91eb3b86af01dcb1d3c78d0e2310/libavformat/movenc.c/#L3209 | static void enable_tracks(AVFormatContext *s)
{
MOVMuxContext *mov = s->priv_data;
int i;
int enabled[AVMEDIA_TYPE_NB];
int first[AVMEDIA_TYPE_NB];
for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
enabled[i] = 0;
first[i] = -1;
}
for (i = 0; i < s->nb_streams; i++) {
AVStream *st = s->streams[i];
if (st->codec->codec_type <= AVMEDIA_TYPE_UNKNOWN ||
st->codec->codec_type >= AVMEDIA_TYPE_NB)
continue;
if (first[st->codec->codec_type] < 0)
first[st->codec->codec_type] = i;
if (st->disposition & AV_DISPOSITION_DEFAULT) {
mov->tracks[i].flags |= MOV_TRACK_ENABLED;
enabled[st->codec->codec_type]++;
}
}
for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
switch (i) {
case AVMEDIA_TYPE_VIDEO:
case AVMEDIA_TYPE_AUDIO:
case AVMEDIA_TYPE_SUBTITLE:
if (enabled[i] > 1)
mov->per_stream_grouping = 1;
if (!enabled[i] && first[i] >= 0)
mov->tracks[first[i]].flags |= MOV_TRACK_ENABLED;
break;
}
}
} | ['static void enable_tracks(AVFormatContext *s)\n{\n MOVMuxContext *mov = s->priv_data;\n int i;\n int enabled[AVMEDIA_TYPE_NB];\n int first[AVMEDIA_TYPE_NB];\n for (i = 0; i < AVMEDIA_TYPE_NB; i++) {\n enabled[i] = 0;\n first[i] = -1;\n }\n for (i = 0; i < s->nb_streams; i++) {\n AVStream *st = s->streams[i];\n if (st->codec->codec_type <= AVMEDIA_TYPE_UNKNOWN ||\n st->codec->codec_type >= AVMEDIA_TYPE_NB)\n continue;\n if (first[st->codec->codec_type] < 0)\n first[st->codec->codec_type] = i;\n if (st->disposition & AV_DISPOSITION_DEFAULT) {\n mov->tracks[i].flags |= MOV_TRACK_ENABLED;\n enabled[st->codec->codec_type]++;\n }\n }\n for (i = 0; i < AVMEDIA_TYPE_NB; i++) {\n switch (i) {\n case AVMEDIA_TYPE_VIDEO:\n case AVMEDIA_TYPE_AUDIO:\n case AVMEDIA_TYPE_SUBTITLE:\n if (enabled[i] > 1)\n mov->per_stream_grouping = 1;\n if (!enabled[i] && first[i] >= 0)\n mov->tracks[first[i]].flags |= MOV_TRACK_ENABLED;\n break;\n }\n }\n}'] |
599 | 0 | https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavformat/nsvdec.c/#L323 | static int nsv_parse_NSVf_header(AVFormatContext *s, AVFormatParameters *ap)
{
NSVContext *nsv = s->priv_data;
ByteIOContext *pb = s->pb;
unsigned int file_size, size;
int64_t duration;
int strings_size;
int table_entries;
int table_entries_used;
PRINT(("%s()\n", __FUNCTION__));
nsv->state = NSV_UNSYNC;
size = get_le32(pb);
if (size < 28)
return -1;
nsv->NSVf_end = size;
file_size = (uint32_t)get_le32(pb);
PRINT(("NSV NSVf chunk_size %u\n", size));
PRINT(("NSV NSVf file_size %u\n", file_size));
nsv->duration = duration = get_le32(pb);
PRINT(("NSV NSVf duration %"PRId64" ms\n", duration));
strings_size = get_le32(pb);
table_entries = get_le32(pb);
table_entries_used = get_le32(pb);
PRINT(("NSV NSVf info-strings size: %d, table entries: %d, bis %d\n",
strings_size, table_entries, table_entries_used));
if (url_feof(pb))
return -1;
PRINT(("NSV got header; filepos %"PRId64"\n", url_ftell(pb)));
if (strings_size > 0) {
char *strings;
char *p, *endp;
char *token, *value;
char quote;
p = strings = av_mallocz(strings_size + 1);
endp = strings + strings_size;
get_buffer(pb, strings, strings_size);
while (p < endp) {
while (*p == ' ')
p++;
if (p >= endp-2)
break;
token = p;
p = strchr(p, '=');
if (!p || p >= endp-2)
break;
*p++ = '\0';
quote = *p++;
value = p;
p = strchr(p, quote);
if (!p || p >= endp)
break;
*p++ = '\0';
PRINT(("NSV NSVf INFO: %s='%s'\n", token, value));
if (!strcmp(token, "ASPECT")) {
} else if (!strcmp(token, "CREATOR") || !strcmp(token, "Author")) {
strncpy(s->author, value, 512-1);
} else if (!strcmp(token, "Copyright")) {
strncpy(s->copyright, value, 512-1);
} else if (!strcmp(token, "TITLE") || !strcmp(token, "Title")) {
strncpy(s->title, value, 512-1);
}
}
av_free(strings);
}
if (url_feof(pb))
return -1;
PRINT(("NSV got infos; filepos %"PRId64"\n", url_ftell(pb)));
if (table_entries_used > 0) {
nsv->index_entries = table_entries_used;
if((unsigned)table_entries >= UINT_MAX / sizeof(uint32_t))
return -1;
nsv->nsvf_index_data = av_malloc(table_entries * sizeof(uint32_t));
#warning "FIXME: Byteswap buffer as needed"
get_buffer(pb, (unsigned char *)nsv->nsvf_index_data, table_entries * sizeof(uint32_t));
}
PRINT(("NSV got index; filepos %"PRId64"\n", url_ftell(pb)));
#ifdef DEBUG_DUMP_INDEX
#define V(v) ((v<0x20 || v > 127)?'.':v)
PRINT(("NSV %d INDEX ENTRIES:\n", table_entries));
PRINT(("NSV [dataoffset][fileoffset]\n", table_entries));
for (i = 0; i < table_entries; i++) {
unsigned char b[8];
url_fseek(pb, size + nsv->nsvf_index_data[i], SEEK_SET);
get_buffer(pb, b, 8);
PRINT(("NSV [0x%08lx][0x%08lx]: %02x %02x %02x %02x %02x %02x %02x %02x"
"%c%c%c%c%c%c%c%c\n",
nsv->nsvf_index_data[i], size + nsv->nsvf_index_data[i],
b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7],
V(b[0]), V(b[1]), V(b[2]), V(b[3]), V(b[4]), V(b[5]), V(b[6]), V(b[7]) ));
}
#undef V
#endif
url_fseek(pb, nsv->base_offset + size, SEEK_SET);
if (url_feof(pb))
return -1;
nsv->state = NSV_HAS_READ_NSVF;
return 0;
} | ['static int nsv_parse_NSVf_header(AVFormatContext *s, AVFormatParameters *ap)\n{\n NSVContext *nsv = s->priv_data;\n ByteIOContext *pb = s->pb;\n unsigned int file_size, size;\n int64_t duration;\n int strings_size;\n int table_entries;\n int table_entries_used;\n PRINT(("%s()\\n", __FUNCTION__));\n nsv->state = NSV_UNSYNC;\n size = get_le32(pb);\n if (size < 28)\n return -1;\n nsv->NSVf_end = size;\n file_size = (uint32_t)get_le32(pb);\n PRINT(("NSV NSVf chunk_size %u\\n", size));\n PRINT(("NSV NSVf file_size %u\\n", file_size));\n nsv->duration = duration = get_le32(pb);\n PRINT(("NSV NSVf duration %"PRId64" ms\\n", duration));\n strings_size = get_le32(pb);\n table_entries = get_le32(pb);\n table_entries_used = get_le32(pb);\n PRINT(("NSV NSVf info-strings size: %d, table entries: %d, bis %d\\n",\n strings_size, table_entries, table_entries_used));\n if (url_feof(pb))\n return -1;\n PRINT(("NSV got header; filepos %"PRId64"\\n", url_ftell(pb)));\n if (strings_size > 0) {\n char *strings;\n char *p, *endp;\n char *token, *value;\n char quote;\n p = strings = av_mallocz(strings_size + 1);\n endp = strings + strings_size;\n get_buffer(pb, strings, strings_size);\n while (p < endp) {\n while (*p == \' \')\n p++;\n if (p >= endp-2)\n break;\n token = p;\n p = strchr(p, \'=\');\n if (!p || p >= endp-2)\n break;\n *p++ = \'\\0\';\n quote = *p++;\n value = p;\n p = strchr(p, quote);\n if (!p || p >= endp)\n break;\n *p++ = \'\\0\';\n PRINT(("NSV NSVf INFO: %s=\'%s\'\\n", token, value));\n if (!strcmp(token, "ASPECT")) {\n } else if (!strcmp(token, "CREATOR") || !strcmp(token, "Author")) {\n strncpy(s->author, value, 512-1);\n } else if (!strcmp(token, "Copyright")) {\n strncpy(s->copyright, value, 512-1);\n } else if (!strcmp(token, "TITLE") || !strcmp(token, "Title")) {\n strncpy(s->title, value, 512-1);\n }\n }\n av_free(strings);\n }\n if (url_feof(pb))\n return -1;\n PRINT(("NSV got infos; filepos %"PRId64"\\n", url_ftell(pb)));\n if (table_entries_used > 0) {\n nsv->index_entries = table_entries_used;\n if((unsigned)table_entries >= UINT_MAX / sizeof(uint32_t))\n return -1;\n nsv->nsvf_index_data = av_malloc(table_entries * sizeof(uint32_t));\n#warning "FIXME: Byteswap buffer as needed"\n get_buffer(pb, (unsigned char *)nsv->nsvf_index_data, table_entries * sizeof(uint32_t));\n }\n PRINT(("NSV got index; filepos %"PRId64"\\n", url_ftell(pb)));\n#ifdef DEBUG_DUMP_INDEX\n#define V(v) ((v<0x20 || v > 127)?\'.\':v)\n PRINT(("NSV %d INDEX ENTRIES:\\n", table_entries));\n PRINT(("NSV [dataoffset][fileoffset]\\n", table_entries));\n for (i = 0; i < table_entries; i++) {\n unsigned char b[8];\n url_fseek(pb, size + nsv->nsvf_index_data[i], SEEK_SET);\n get_buffer(pb, b, 8);\n PRINT(("NSV [0x%08lx][0x%08lx]: %02x %02x %02x %02x %02x %02x %02x %02x"\n "%c%c%c%c%c%c%c%c\\n",\n nsv->nsvf_index_data[i], size + nsv->nsvf_index_data[i],\n b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7],\n V(b[0]), V(b[1]), V(b[2]), V(b[3]), V(b[4]), V(b[5]), V(b[6]), V(b[7]) ));\n }\n#undef V\n#endif\n url_fseek(pb, nsv->base_offset + size, SEEK_SET);\n if (url_feof(pb))\n return -1;\n nsv->state = NSV_HAS_READ_NSVF;\n return 0;\n}', 'unsigned int get_le32(ByteIOContext *s)\n{\n unsigned int val;\n val = get_le16(s);\n val |= get_le16(s) << 16;\n return val;\n}', 'unsigned int get_le16(ByteIOContext *s)\n{\n unsigned int val;\n val = get_byte(s);\n val |= get_byte(s) << 8;\n return val;\n}', 'int get_byte(ByteIOContext *s)\n{\n if (s->buf_ptr < s->buf_end) {\n return *s->buf_ptr++;\n } else {\n fill_buffer(s);\n if (s->buf_ptr < s->buf_end)\n return *s->buf_ptr++;\n else\n return 0;\n }\n}', 'int url_feof(ByteIOContext *s)\n{\n if(!s)\n return 0;\n return s->eof_reached;\n}', 'void *av_mallocz(unsigned int size)\n{\n void *ptr = av_malloc(size);\n if (ptr)\n memset(ptr, 0, size);\n return ptr;\n}', 'void *av_malloc(unsigned int size)\n{\n void *ptr;\n#ifdef CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-16) )\n return NULL;\n#ifdef CONFIG_MEMALIGN_HACK\n ptr = malloc(size+16);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&15) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif defined (HAVE_MEMALIGN)\n ptr = memalign(16,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}'] |
600 | 0 | https://github.com/openssl/openssl/blob/d59c7c81e3850dc667d61047850c3b6936eb5fca/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 == NULL) {\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 *ret = NULL;\n int sign;\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n {\n BIGNUM local_B;\n bn_init(&local_B);\n BN_with_flags(&local_B, B, BN_FLG_CONSTTIME);\n if (!BN_nnmod(B, &local_B, A, ctx))\n goto err;\n }\n }\n sign = -1;\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n {\n BIGNUM local_A;\n bn_init(&local_A);\n BN_with_flags(&local_A, A, BN_FLG_CONSTTIME);\n if (!BN_div(D, M, &local_A, B, ctx))\n goto err;\n }\n tmp = A;\n A = B;\n B = M;\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n if (!BN_add(tmp, tmp, Y))\n goto err;\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n BNerr(BN_F_BN_MOD_INVERSE_NO_BRANCH, BN_R_NO_INVERSE);\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return (ret);\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return (0);\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return (0);\n }\n if (dv != NULL)\n BN_zero(dv);\n return (1);\n }\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (dv == NULL)\n res = BN_CTX_get(ctx);\n else\n res = dv;\n if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n res->neg = (num->neg ^ divisor->neg);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--, resp--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# ifdef BN_DEBUG_LEVITTE\n fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n", n0, n1, d0, q);\n# endif\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifdef BN_DEBUG_LEVITTE\n fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n", n0, n1, d0, q);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return (1);\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return (0);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}'] |
Subsets and Splits