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
|
|---|---|---|---|---|
2,601
| 0
|
https://github.com/openssl/openssl/blob/0f3e6045898e9aa5d0249e61c874b1f153ae54fa/crypto/lhash/lhash.c/#L243
|
char *lh_delete(LHASH *lh, char *data)
{
unsigned long hash;
LHASH_NODE *nn,**rn;
char *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((char *)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 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_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 *)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->default_cert=ssl_cert_new()) == NULL)\n\t\tgoto err;\n\tret->default_passwd_callback=NULL;\n\tret->client_cert_cb=NULL;\n\tret->sessions=lh_new(SSL_SESSION_hash,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(unsigned long (*h)(), int (*c)())\n\t{\n\tLHASH *ret;\n\tint i;\n\tif ((ret=(LHASH *)Malloc(sizeof(LHASH))) == NULL)\n\t\tgoto err0;\n\tif ((ret->b=(LHASH_NODE **)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)?(int (*)())strcmp:c);\n\tret->hash=((h == NULL)?(unsigned long (*)())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\tFree((char *)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 *)Malloc(sizeof(SSL));\n\tif (s == NULL) goto err;\n\tmemset(s,0,sizeof(SSL));\n\tif (ctx->default_cert != NULL)\n\t\t{\n\t\tCRYPTO_add(&ctx->default_cert->references,1,\n\t\t\t CRYPTO_LOCK_SSL_CERT);\n\t\ts->cert=ctx->default_cert;\n\t\t}\n\telse\n\t\ts->cert=NULL;\n\ts->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\tCRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);\n\ts->ctx=ctx;\n\ts->verify_result=X509_V_OK;\n\ts->method=ctx->method;\n\tif (!s->method->ssl_new(s))\n\t\t{\n\t\tSSL_CTX_free(ctx);\n\t\tFree(s);\n\t\tgoto err;\n\t\t}\n\ts->quiet_shutdown=ctx->quiet_shutdown;\n\ts->references=1;\n\ts->server=(ctx->method->ssl_accept == ssl_undefined_function)?0:1;\n\ts->options=ctx->options;\n\tSSL_clear(s);\n\tCRYPTO_new_ex_data(ssl_meth,(char *)s,&s->ex_data);\n\treturn(s);\nerr:\n\tSSLerr(SSL_F_SSL_NEW,ERR_R_MALLOC_FAILURE);\n\treturn(NULL);\n\t}', '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#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\ts->read_ahead=s->ctx->read_ahead;\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,(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}']
|
2,602
| 0
|
https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/motion_est_template.c/#L311
|
static int qpel_motion_search(MpegEncContext * s,
int *mx_ptr, int *my_ptr, int dmin,
int src_index, int ref_index,
int size, int h)
{
MotionEstContext * const c= &s->me;
const int mx = *mx_ptr;
const int my = *my_ptr;
const int penalty_factor= c->sub_penalty_factor;
const int map_generation= c->map_generation;
const int subpel_quality= c->avctx->me_subpel_quality;
uint32_t *map= c->map;
me_cmp_func cmpf, chroma_cmpf;
me_cmp_func cmp_sub, chroma_cmp_sub;
LOAD_COMMON
int flags= c->sub_flags;
cmpf= s->dsp.me_cmp[size];
chroma_cmpf= s->dsp.me_cmp[size+1];
cmp_sub= s->dsp.me_sub_cmp[size];
chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
if(c->skip){
*mx_ptr = 0;
*my_ptr = 0;
return dmin;
}
if(c->avctx->me_cmp != c->avctx->me_sub_cmp){
dmin= cmp(s, mx, my, 0, 0, size, h, ref_index, src_index, cmp_sub, chroma_cmp_sub, flags);
if(mx || my || size>0)
dmin += (mv_penalty[4*mx - pred_x] + mv_penalty[4*my - pred_y])*penalty_factor;
}
if (mx > xmin && mx < xmax &&
my > ymin && my < ymax) {
int bx=4*mx, by=4*my;
int d= dmin;
int i, nx, ny;
const int index= (my<<ME_MAP_SHIFT) + mx;
const int t= score_map[(index-(1<<ME_MAP_SHIFT) )&(ME_MAP_SIZE-1)];
const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)];
const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)];
const int b= score_map[(index+(1<<ME_MAP_SHIFT) )&(ME_MAP_SIZE-1)];
const int c= score_map[(index )&(ME_MAP_SIZE-1)];
int best[8];
int best_pos[8][2];
memset(best, 64, sizeof(int)*8);
#if 1
if(s->me.dia_size>=2){
const int tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
const int bl= score_map[(index+(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
const int tr= score_map[(index-(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
const int br= score_map[(index+(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
for(ny= -3; ny <= 3; ny++){
for(nx= -3; nx <= 3; nx++){
const int64_t t2= nx*nx*(tr + tl - 2*t) + 4*nx*(tr-tl) + 32*t;
const int64_t c2= nx*nx*( r + l - 2*c) + 4*nx*( r- l) + 32*c;
const int64_t b2= nx*nx*(br + bl - 2*b) + 4*nx*(br-bl) + 32*b;
int score= (ny*ny*(b2 + t2 - 2*c2) + 4*ny*(b2 - t2) + 32*c2 + 512)>>10;
int i;
if((nx&3)==0 && (ny&3)==0) continue;
score += (mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
for(i=0; i<8; i++){
if(score < best[i]){
memmove(&best[i+1], &best[i], sizeof(int)*(7-i));
memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));
best[i]= score;
best_pos[i][0]= nx + 4*mx;
best_pos[i][1]= ny + 4*my;
break;
}
}
}
}
}else{
int tl;
const int cx = 4*(r - l);
const int cx2= r + l - 2*c;
const int cy = 4*(b - t);
const int cy2= b + t - 2*c;
int cxy;
if(map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)] == (my<<ME_MAP_MV_BITS) + mx + map_generation && 0){
tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
}else{
tl= cmp(s, mx-1, my-1, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);
}
cxy= 2*tl + (cx + cy)/4 - (cx2 + cy2) - 2*c;
assert(16*cx2 + 4*cx + 32*c == 32*r);
assert(16*cx2 - 4*cx + 32*c == 32*l);
assert(16*cy2 + 4*cy + 32*c == 32*b);
assert(16*cy2 - 4*cy + 32*c == 32*t);
assert(16*cxy + 16*cy2 + 16*cx2 - 4*cy - 4*cx + 32*c == 32*tl);
for(ny= -3; ny <= 3; ny++){
for(nx= -3; nx <= 3; nx++){
int score= ny*nx*cxy + nx*nx*cx2 + ny*ny*cy2 + nx*cx + ny*cy + 32*c;
int i;
if((nx&3)==0 && (ny&3)==0) continue;
score += 32*(mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
for(i=0; i<8; i++){
if(score < best[i]){
memmove(&best[i+1], &best[i], sizeof(int)*(7-i));
memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));
best[i]= score;
best_pos[i][0]= nx + 4*mx;
best_pos[i][1]= ny + 4*my;
break;
}
}
}
}
}
for(i=0; i<subpel_quality; i++){
nx= best_pos[i][0];
ny= best_pos[i][1];
CHECK_QUARTER_MV(nx&3, ny&3, nx>>2, ny>>2)
}
#if 0
const int tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
const int bl= score_map[(index+(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
const int tr= score_map[(index-(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
const int br= score_map[(index+(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
if(tl<br){
static int stats[7][7], count;
count++;
stats[4*mx - bx + 3][4*my - by + 3]++;
if(256*256*256*64 % count ==0){
for(i=0; i<49; i++){
if((i%7)==0) printf("\n");
printf("%6d ", stats[0][i]);
}
printf("\n");
}
}
#endif
#else
CHECK_QUARTER_MV(2, 2, mx-1, my-1)
CHECK_QUARTER_MV(0, 2, mx , my-1)
CHECK_QUARTER_MV(2, 2, mx , my-1)
CHECK_QUARTER_MV(2, 0, mx , my )
CHECK_QUARTER_MV(2, 2, mx , my )
CHECK_QUARTER_MV(0, 2, mx , my )
CHECK_QUARTER_MV(2, 2, mx-1, my )
CHECK_QUARTER_MV(2, 0, mx-1, my )
nx= bx;
ny= by;
for(i=0; i<8; i++){
int ox[8]= {0, 1, 1, 1, 0,-1,-1,-1};
int oy[8]= {1, 1, 0,-1,-1,-1, 0, 1};
CHECK_QUARTER_MV((nx + ox[i])&3, (ny + oy[i])&3, (nx + ox[i])>>2, (ny + oy[i])>>2)
}
#endif
#if 0
CHECK_QUARTER_MV(1, 3, mx-1, my-1)
CHECK_QUARTER_MV(1, 2, mx-1, my-1)
CHECK_QUARTER_MV(1, 1, mx-1, my-1)
CHECK_QUARTER_MV(2, 1, mx-1, my-1)
CHECK_QUARTER_MV(3, 1, mx-1, my-1)
CHECK_QUARTER_MV(0, 1, mx , my-1)
CHECK_QUARTER_MV(1, 1, mx , my-1)
CHECK_QUARTER_MV(2, 1, mx , my-1)
CHECK_QUARTER_MV(3, 1, mx , my-1)
CHECK_QUARTER_MV(3, 2, mx , my-1)
CHECK_QUARTER_MV(3, 3, mx , my-1)
CHECK_QUARTER_MV(3, 0, mx , my )
CHECK_QUARTER_MV(3, 1, mx , my )
CHECK_QUARTER_MV(3, 2, mx , my )
CHECK_QUARTER_MV(3, 3, mx , my )
CHECK_QUARTER_MV(2, 3, mx , my )
CHECK_QUARTER_MV(1, 3, mx , my )
CHECK_QUARTER_MV(0, 3, mx , my )
CHECK_QUARTER_MV(3, 3, mx-1, my )
CHECK_QUARTER_MV(2, 3, mx-1, my )
CHECK_QUARTER_MV(1, 3, mx-1, my )
CHECK_QUARTER_MV(1, 2, mx-1, my )
CHECK_QUARTER_MV(1, 1, mx-1, my )
CHECK_QUARTER_MV(1, 0, mx-1, my )
#endif
assert(bx >= xmin*4 && bx <= xmax*4 && by >= ymin*4 && by <= ymax*4);
*mx_ptr = bx;
*my_ptr = by;
}else{
*mx_ptr =4*mx;
*my_ptr =4*my;
}
return dmin;
}
|
['static int qpel_motion_search(MpegEncContext * s,\n int *mx_ptr, int *my_ptr, int dmin,\n int src_index, int ref_index,\n int size, int h)\n{\n MotionEstContext * const c= &s->me;\n const int mx = *mx_ptr;\n const int my = *my_ptr;\n const int penalty_factor= c->sub_penalty_factor;\n const int map_generation= c->map_generation;\n const int subpel_quality= c->avctx->me_subpel_quality;\n uint32_t *map= c->map;\n me_cmp_func cmpf, chroma_cmpf;\n me_cmp_func cmp_sub, chroma_cmp_sub;\n LOAD_COMMON\n int flags= c->sub_flags;\n cmpf= s->dsp.me_cmp[size];\n chroma_cmpf= s->dsp.me_cmp[size+1];\n cmp_sub= s->dsp.me_sub_cmp[size];\n chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];\n if(c->skip){\n *mx_ptr = 0;\n *my_ptr = 0;\n return dmin;\n }\n if(c->avctx->me_cmp != c->avctx->me_sub_cmp){\n dmin= cmp(s, mx, my, 0, 0, size, h, ref_index, src_index, cmp_sub, chroma_cmp_sub, flags);\n if(mx || my || size>0)\n dmin += (mv_penalty[4*mx - pred_x] + mv_penalty[4*my - pred_y])*penalty_factor;\n }\n if (mx > xmin && mx < xmax &&\n my > ymin && my < ymax) {\n int bx=4*mx, by=4*my;\n int d= dmin;\n int i, nx, ny;\n const int index= (my<<ME_MAP_SHIFT) + mx;\n const int t= score_map[(index-(1<<ME_MAP_SHIFT) )&(ME_MAP_SIZE-1)];\n const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)];\n const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)];\n const int b= score_map[(index+(1<<ME_MAP_SHIFT) )&(ME_MAP_SIZE-1)];\n const int c= score_map[(index )&(ME_MAP_SIZE-1)];\n int best[8];\n int best_pos[8][2];\n memset(best, 64, sizeof(int)*8);\n#if 1\n if(s->me.dia_size>=2){\n const int tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];\n const int bl= score_map[(index+(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];\n const int tr= score_map[(index-(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];\n const int br= score_map[(index+(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];\n for(ny= -3; ny <= 3; ny++){\n for(nx= -3; nx <= 3; nx++){\n const int64_t t2= nx*nx*(tr + tl - 2*t) + 4*nx*(tr-tl) + 32*t;\n const int64_t c2= nx*nx*( r + l - 2*c) + 4*nx*( r- l) + 32*c;\n const int64_t b2= nx*nx*(br + bl - 2*b) + 4*nx*(br-bl) + 32*b;\n int score= (ny*ny*(b2 + t2 - 2*c2) + 4*ny*(b2 - t2) + 32*c2 + 512)>>10;\n int i;\n if((nx&3)==0 && (ny&3)==0) continue;\n score += (mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;\n for(i=0; i<8; i++){\n if(score < best[i]){\n memmove(&best[i+1], &best[i], sizeof(int)*(7-i));\n memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));\n best[i]= score;\n best_pos[i][0]= nx + 4*mx;\n best_pos[i][1]= ny + 4*my;\n break;\n }\n }\n }\n }\n }else{\n int tl;\n const int cx = 4*(r - l);\n const int cx2= r + l - 2*c;\n const int cy = 4*(b - t);\n const int cy2= b + t - 2*c;\n int cxy;\n if(map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)] == (my<<ME_MAP_MV_BITS) + mx + map_generation && 0){\n tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];\n }else{\n tl= cmp(s, mx-1, my-1, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);\n }\n cxy= 2*tl + (cx + cy)/4 - (cx2 + cy2) - 2*c;\n assert(16*cx2 + 4*cx + 32*c == 32*r);\n assert(16*cx2 - 4*cx + 32*c == 32*l);\n assert(16*cy2 + 4*cy + 32*c == 32*b);\n assert(16*cy2 - 4*cy + 32*c == 32*t);\n assert(16*cxy + 16*cy2 + 16*cx2 - 4*cy - 4*cx + 32*c == 32*tl);\n for(ny= -3; ny <= 3; ny++){\n for(nx= -3; nx <= 3; nx++){\n int score= ny*nx*cxy + nx*nx*cx2 + ny*ny*cy2 + nx*cx + ny*cy + 32*c;\n int i;\n if((nx&3)==0 && (ny&3)==0) continue;\n score += 32*(mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;\n for(i=0; i<8; i++){\n if(score < best[i]){\n memmove(&best[i+1], &best[i], sizeof(int)*(7-i));\n memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));\n best[i]= score;\n best_pos[i][0]= nx + 4*mx;\n best_pos[i][1]= ny + 4*my;\n break;\n }\n }\n }\n }\n }\n for(i=0; i<subpel_quality; i++){\n nx= best_pos[i][0];\n ny= best_pos[i][1];\n CHECK_QUARTER_MV(nx&3, ny&3, nx>>2, ny>>2)\n }\n#if 0\n const int tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];\n const int bl= score_map[(index+(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];\n const int tr= score_map[(index-(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];\n const int br= score_map[(index+(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];\n if(tl<br){\n static int stats[7][7], count;\n count++;\n stats[4*mx - bx + 3][4*my - by + 3]++;\n if(256*256*256*64 % count ==0){\n for(i=0; i<49; i++){\n if((i%7)==0) printf("\\n");\n printf("%6d ", stats[0][i]);\n }\n printf("\\n");\n }\n }\n#endif\n#else\n CHECK_QUARTER_MV(2, 2, mx-1, my-1)\n CHECK_QUARTER_MV(0, 2, mx , my-1)\n CHECK_QUARTER_MV(2, 2, mx , my-1)\n CHECK_QUARTER_MV(2, 0, mx , my )\n CHECK_QUARTER_MV(2, 2, mx , my )\n CHECK_QUARTER_MV(0, 2, mx , my )\n CHECK_QUARTER_MV(2, 2, mx-1, my )\n CHECK_QUARTER_MV(2, 0, mx-1, my )\n nx= bx;\n ny= by;\n for(i=0; i<8; i++){\n int ox[8]= {0, 1, 1, 1, 0,-1,-1,-1};\n int oy[8]= {1, 1, 0,-1,-1,-1, 0, 1};\n CHECK_QUARTER_MV((nx + ox[i])&3, (ny + oy[i])&3, (nx + ox[i])>>2, (ny + oy[i])>>2)\n }\n#endif\n#if 0\n CHECK_QUARTER_MV(1, 3, mx-1, my-1)\n CHECK_QUARTER_MV(1, 2, mx-1, my-1)\n CHECK_QUARTER_MV(1, 1, mx-1, my-1)\n CHECK_QUARTER_MV(2, 1, mx-1, my-1)\n CHECK_QUARTER_MV(3, 1, mx-1, my-1)\n CHECK_QUARTER_MV(0, 1, mx , my-1)\n CHECK_QUARTER_MV(1, 1, mx , my-1)\n CHECK_QUARTER_MV(2, 1, mx , my-1)\n CHECK_QUARTER_MV(3, 1, mx , my-1)\n CHECK_QUARTER_MV(3, 2, mx , my-1)\n CHECK_QUARTER_MV(3, 3, mx , my-1)\n CHECK_QUARTER_MV(3, 0, mx , my )\n CHECK_QUARTER_MV(3, 1, mx , my )\n CHECK_QUARTER_MV(3, 2, mx , my )\n CHECK_QUARTER_MV(3, 3, mx , my )\n CHECK_QUARTER_MV(2, 3, mx , my )\n CHECK_QUARTER_MV(1, 3, mx , my )\n CHECK_QUARTER_MV(0, 3, mx , my )\n CHECK_QUARTER_MV(3, 3, mx-1, my )\n CHECK_QUARTER_MV(2, 3, mx-1, my )\n CHECK_QUARTER_MV(1, 3, mx-1, my )\n CHECK_QUARTER_MV(1, 2, mx-1, my )\n CHECK_QUARTER_MV(1, 1, mx-1, my )\n CHECK_QUARTER_MV(1, 0, mx-1, my )\n#endif\n assert(bx >= xmin*4 && bx <= xmax*4 && by >= ymin*4 && by <= ymax*4);\n *mx_ptr = bx;\n *my_ptr = by;\n }else{\n *mx_ptr =4*mx;\n *my_ptr =4*my;\n }\n return dmin;\n}']
|
2,603
| 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 ec_GF2m_simple_oct2point(const EC_GROUP *group, EC_POINT *point,\n const unsigned char *buf, size_t len,\n BN_CTX *ctx)\n{\n point_conversion_form_t form;\n int y_bit;\n BN_CTX *new_ctx = NULL;\n BIGNUM *x, *y, *yxi;\n size_t field_len, enc_len;\n int ret = 0;\n if (len == 0) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);\n return 0;\n }\n form = buf[0];\n y_bit = form & 1;\n form = form & ~1U;\n if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)\n && (form != POINT_CONVERSION_UNCOMPRESSED)\n && (form != POINT_CONVERSION_HYBRID)) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n return 0;\n }\n if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n return 0;\n }\n if (form == 0) {\n if (len != 1) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n return 0;\n }\n return EC_POINT_set_to_infinity(group, point);\n }\n field_len = (EC_GROUP_get_degree(group) + 7) / 8;\n enc_len =\n (form ==\n POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;\n if (len != enc_len) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n return 0;\n }\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n BN_CTX_start(ctx);\n x = BN_CTX_get(ctx);\n y = BN_CTX_get(ctx);\n yxi = BN_CTX_get(ctx);\n if (yxi == NULL)\n goto err;\n if (!BN_bin2bn(buf + 1, field_len, x))\n goto err;\n if (BN_ucmp(x, group->field) >= 0) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n goto err;\n }\n if (form == POINT_CONVERSION_COMPRESSED) {\n if (!EC_POINT_set_compressed_coordinates_GF2m\n (group, point, x, y_bit, ctx))\n goto err;\n } else {\n if (!BN_bin2bn(buf + 1 + field_len, field_len, y))\n goto err;\n if (BN_ucmp(y, group->field) >= 0) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n goto err;\n }\n if (form == POINT_CONVERSION_HYBRID) {\n if (!group->meth->field_div(group, yxi, y, x, ctx))\n goto err;\n if (y_bit != BN_is_odd(yxi)) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n goto err;\n }\n }\n if (!EC_POINT_set_affine_coordinates_GF2m(group, point, x, y, ctx))\n goto err;\n }\n if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_POINT_IS_NOT_ON_CURVE);\n goto err;\n }\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}', '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_bin2bn(const unsigned char *s, int len, BIGNUM *ret)\n{\n unsigned int i, m;\n unsigned int n;\n BN_ULONG l;\n BIGNUM *bn = NULL;\n if (ret == NULL)\n ret = bn = BN_new();\n if (ret == NULL)\n return (NULL);\n bn_check_top(ret);\n for ( ; len > 0 && *s == 0; s++, len--)\n continue;\n n = len;\n if (n == 0) {\n ret->top = 0;\n return (ret);\n }\n i = ((n - 1) / BN_BYTES) + 1;\n m = ((n - 1) % (BN_BYTES));\n if (bn_wexpand(ret, (int)i) == NULL) {\n BN_free(bn);\n return NULL;\n }\n ret->top = i;\n ret->neg = 0;\n l = 0;\n while (n--) {\n l = (l << 8L) | *(s++);\n if (m-- == 0) {\n ret->d[--i] = l;\n l = 0;\n m = BN_BYTES - 1;\n }\n }\n bn_correct_top(ret);\n return (ret);\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *A, *a = NULL;\n const BN_ULONG *B;\n int i;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return (NULL);\n }\n if (BN_get_flags(b,BN_FLG_SECURE))\n a = A = OPENSSL_secure_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}']
|
2,604
| 0
|
https://github.com/libav/libav/blob/dff6197dfb9b2d145729885c63f865c5fdd184aa/libavcodec/yop.c/#L129
|
static int yop_paint_block(YopDecContext *s, int tag)
{
if (s->src_end - s->srcptr < paint_lut[tag][3]) {
av_log(s->avctx, AV_LOG_ERROR, "Packet too small.\n");
return AVERROR_INVALIDDATA;
}
s->dstptr[0] = s->srcptr[0];
s->dstptr[1] = s->srcptr[paint_lut[tag][0]];
s->dstptr[s->frame.linesize[0]] = s->srcptr[paint_lut[tag][1]];
s->dstptr[s->frame.linesize[0] + 1] = s->srcptr[paint_lut[tag][2]];
s->srcptr += paint_lut[tag][3];
return 0;
}
|
['static int yop_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,\n AVPacket *avpkt)\n{\n YopDecContext *s = avctx->priv_data;\n int tag, firstcolor, is_odd_frame;\n int ret, i, x, y;\n uint32_t *palette;\n if (avpkt->size < 4 + 3 * s->num_pal_colors) {\n av_log(avctx, AV_LOG_ERROR, "Packet too small.\\n");\n return AVERROR_INVALIDDATA;\n }\n if (s->frame.data[0])\n avctx->release_buffer(avctx, &s->frame);\n ret = ff_get_buffer(avctx, &s->frame);\n if (ret < 0) {\n av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n return ret;\n }\n if (!avctx->frame_number)\n memset(s->frame.data[1], 0, AVPALETTE_SIZE);\n s->dstbuf = s->frame.data[0];\n s->dstptr = s->frame.data[0];\n s->srcptr = avpkt->data + 4;\n s->src_end = avpkt->data + avpkt->size;\n s->low_nibble = NULL;\n is_odd_frame = avpkt->data[0];\n firstcolor = s->first_color[is_odd_frame];\n palette = (uint32_t *)s->frame.data[1];\n for (i = 0; i < s->num_pal_colors; i++, s->srcptr += 3)\n palette[i + firstcolor] = (s->srcptr[0] << 18) |\n (s->srcptr[1] << 10) |\n (s->srcptr[2] << 2);\n s->frame.palette_has_changed = 1;\n for (y = 0; y < avctx->height; y += 2) {\n for (x = 0; x < avctx->width; x += 2) {\n if (s->srcptr - avpkt->data >= avpkt->size) {\n av_log(avctx, AV_LOG_ERROR, "Packet too small.\\n");\n return AVERROR_INVALIDDATA;\n }\n tag = yop_get_next_nibble(s);\n if (tag != 0xf) {\n ret = yop_paint_block(s, tag);\n if (ret < 0)\n return ret;\n } else {\n tag = yop_get_next_nibble(s);\n ret = yop_copy_previous_block(s, tag);\n if (ret < 0) {\n avctx->release_buffer(avctx, &s->frame);\n return ret;\n }\n }\n s->dstptr += 2;\n }\n s->dstptr += 2*s->frame.linesize[0] - x;\n }\n *got_frame = 1;\n *(AVFrame *) data = s->frame;\n return avpkt->size;\n}', 'static uint8_t yop_get_next_nibble(YopDecContext *s)\n{\n int ret;\n if (s->low_nibble) {\n ret = *s->low_nibble & 0xf;\n s->low_nibble = NULL;\n }else {\n s->low_nibble = s->srcptr++;\n ret = *s->low_nibble >> 4;\n }\n return ret;\n}', 'static int yop_paint_block(YopDecContext *s, int tag)\n{\n if (s->src_end - s->srcptr < paint_lut[tag][3]) {\n av_log(s->avctx, AV_LOG_ERROR, "Packet too small.\\n");\n return AVERROR_INVALIDDATA;\n }\n s->dstptr[0] = s->srcptr[0];\n s->dstptr[1] = s->srcptr[paint_lut[tag][0]];\n s->dstptr[s->frame.linesize[0]] = s->srcptr[paint_lut[tag][1]];\n s->dstptr[s->frame.linesize[0] + 1] = s->srcptr[paint_lut[tag][2]];\n s->srcptr += paint_lut[tag][3];\n return 0;\n}']
|
2,605
| 0
|
https://github.com/libav/libav/blob/a082ac412520cd5d812bd57e5ccdad2af557125b/libavcodec/proresenc.c/#L397
|
static inline int estimate_vlc(uint8_t codebook, int val)
{
unsigned int rice_order, exp_order, switch_bits, switch_val;
int exponent;
switch_bits = (codebook & 3) + 1;
rice_order = codebook >> 5;
exp_order = (codebook >> 2) & 7;
switch_val = switch_bits << rice_order;
if (val >= switch_val) {
val -= switch_val - (1 << exp_order);
exponent = av_log2(val);
return exponent * 2 - exp_order + switch_bits + 1;
} else {
return (val >> rice_order) + rice_order + 1;
}
}
|
['static int estimate_dcs(int *error, DCTELEM *blocks, int blocks_per_slice,\n int scale)\n{\n int i;\n int codebook = 3, code, dc, prev_dc, delta, sign, new_sign;\n int bits;\n prev_dc = (blocks[0] - 0x4000) / scale;\n bits = estimate_vlc(FIRST_DC_CB, MAKE_CODE(prev_dc));\n codebook = 3;\n blocks += 64;\n *error += FFABS(blocks[0] - 0x4000) % scale;\n for (i = 1; i < blocks_per_slice; i++, blocks += 64) {\n dc = (blocks[0] - 0x4000) / scale;\n *error += FFABS(blocks[0] - 0x4000) % scale;\n delta = dc - prev_dc;\n new_sign = GET_SIGN(delta);\n delta = (delta ^ sign) - sign;\n code = MAKE_CODE(delta);\n bits += estimate_vlc(ff_prores_dc_codebook[codebook], code);\n codebook = (code + (code & 1)) >> 1;\n codebook = FFMIN(codebook, 3);\n sign = new_sign;\n prev_dc = dc;\n }\n return bits;\n}', 'static inline int estimate_vlc(uint8_t codebook, int val)\n{\n unsigned int rice_order, exp_order, switch_bits, switch_val;\n int exponent;\n switch_bits = (codebook & 3) + 1;\n rice_order = codebook >> 5;\n exp_order = (codebook >> 2) & 7;\n switch_val = switch_bits << rice_order;\n if (val >= switch_val) {\n val -= switch_val - (1 << exp_order);\n exponent = av_log2(val);\n return exponent * 2 - exp_order + switch_bits + 1;\n } else {\n return (val >> rice_order) + rice_order + 1;\n }\n}', 'static av_always_inline av_const int av_log2_c(unsigned int v)\n{\n int n = 0;\n if (v & 0xffff0000) {\n v >>= 16;\n n += 16;\n }\n if (v & 0xff00) {\n v >>= 8;\n n += 8;\n }\n n += ff_log2_tab[v];\n return n;\n}']
|
2,606
| 0
|
https://github.com/openssl/openssl/blob/1a5a1a93f6c48b135a2b384f7e571abb7b90fc55/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;
}
}
}
|
['static int sv_body(char *hostname, int s, unsigned char *context)\n\t{\n\tchar *buf=NULL;\n\tfd_set readfds;\n\tint ret=1,width;\n\tint k,i;\n\tunsigned long l;\n\tSSL *con=NULL;\n\tBIO *sbio;\n#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE)\n\tstruct timeval tv;\n#endif\n\tif ((buf=OPENSSL_malloc(bufsize)) == NULL)\n\t\t{\n\t\tBIO_printf(bio_err,"out of memory\\n");\n\t\tgoto err;\n\t\t}\n#ifdef FIONBIO\n\tif (s_nbio)\n\t\t{\n\t\tunsigned long sl=1;\n\t\tif (!s_quiet)\n\t\t\tBIO_printf(bio_err,"turning on non blocking io\\n");\n\t\tif (BIO_socket_ioctl(s,FIONBIO,&sl) < 0)\n\t\t\tERR_print_errors(bio_err);\n\t\t}\n#endif\n\tif (con == NULL) {\n\t\tcon=SSL_new(ctx);\n#ifndef OPENSSL_NO_KRB5\n\t\tif ((con->kssl_ctx = kssl_ctx_new()) != NULL)\n {\n kssl_ctx_setstring(con->kssl_ctx, KSSL_SERVICE,\n\t\t\t\t\t\t\t\tKRB5SVC);\n kssl_ctx_setstring(con->kssl_ctx, KSSL_KEYTAB,\n\t\t\t\t\t\t\t\tKRB5KEYTAB);\n }\n#endif\n\t\tif(context)\n\t\t SSL_set_session_id_context(con, context,\n\t\t\t\t\t\t strlen((char *)context));\n\t}\n\tSSL_clear(con);\n\tif (SSL_version(con) == DTLS1_VERSION)\n\t\t{\n\t\tstruct timeval timeout;\n\t\tsbio=BIO_new_dgram(s,BIO_NOCLOSE);\n\t\tif (enable_timeouts)\n\t\t\t{\n\t\t\ttimeout.tv_sec = 0;\n\t\t\ttimeout.tv_usec = DGRAM_RCV_TIMEOUT;\n\t\t\tBIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);\n\t\t\ttimeout.tv_sec = 0;\n\t\t\ttimeout.tv_usec = DGRAM_SND_TIMEOUT;\n\t\t\tBIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);\n\t\t\t}\n\t\tif (socket_mtu > 0)\n\t\t\t{\n\t\t\tSSL_set_options(con, SSL_OP_NO_QUERY_MTU);\n\t\t\tSSL_set_mtu(con, socket_mtu);\n\t\t\t}\n\t\telse\n\t\t\tBIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);\n SSL_set_options(con, SSL_OP_COOKIE_EXCHANGE);\n\t\t}\n\telse\n\t\tsbio=BIO_new_socket(s,BIO_NOCLOSE);\n\tif (s_nbio_test)\n\t\t{\n\t\tBIO *test;\n\t\ttest=BIO_new(BIO_f_nbio_test());\n\t\tsbio=BIO_push(test,sbio);\n\t\t}\n\tSSL_set_bio(con,sbio,sbio);\n\tSSL_set_accept_state(con);\n\tif (s_debug)\n\t\t{\n\t\tcon->debug=1;\n\t\tBIO_set_callback(SSL_get_rbio(con),bio_dump_callback);\n\t\tBIO_set_callback_arg(SSL_get_rbio(con),bio_s_out);\n\t\t}\n\tif (s_msg)\n\t\t{\n\t\tSSL_set_msg_callback(con, msg_cb);\n\t\tSSL_set_msg_callback_arg(con, bio_s_out);\n\t\t}\n\twidth=s+1;\n\tfor (;;)\n\t\t{\n\t\tint read_from_terminal;\n\t\tint read_from_sslcon;\n\t\tread_from_terminal = 0;\n\t\tread_from_sslcon = SSL_pending(con);\n\t\tif (!read_from_sslcon)\n\t\t\t{\n\t\t\tFD_ZERO(&readfds);\n#if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_NETWARE)\n\t\t\tFD_SET(fileno(stdin),&readfds);\n#endif\n\t\t\tFD_SET(s,&readfds);\n#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE)\n\t\t\ttv.tv_sec = 1;\n\t\t\ttv.tv_usec = 0;\n\t\t\ti=select(width,(void *)&readfds,NULL,NULL,&tv);\n\t\t\tif((i < 0) || (!i && !_kbhit() ) )continue;\n\t\t\tif(_kbhit())\n\t\t\t\tread_from_terminal = 1;\n#else\n\t\t\ti=select(width,(void *)&readfds,NULL,NULL,NULL);\n\t\t\tif (i <= 0) continue;\n\t\t\tif (FD_ISSET(fileno(stdin),&readfds))\n\t\t\t\tread_from_terminal = 1;\n#endif\n\t\t\tif (FD_ISSET(s,&readfds))\n\t\t\t\tread_from_sslcon = 1;\n\t\t\t}\n\t\tif (read_from_terminal)\n\t\t\t{\n\t\t\tif (s_crlf)\n\t\t\t\t{\n\t\t\t\tint j, lf_num;\n\t\t\t\ti=raw_read_stdin(buf, bufsize/2);\n\t\t\t\tlf_num = 0;\n\t\t\t\tfor (j = 0; j < i; j++)\n\t\t\t\t\tif (buf[j] == \'\\n\')\n\t\t\t\t\t\tlf_num++;\n\t\t\t\tfor (j = i-1; j >= 0; j--)\n\t\t\t\t\t{\n\t\t\t\t\tbuf[j+lf_num] = buf[j];\n\t\t\t\t\tif (buf[j] == \'\\n\')\n\t\t\t\t\t\t{\n\t\t\t\t\t\tlf_num--;\n\t\t\t\t\t\ti++;\n\t\t\t\t\t\tbuf[j+lf_num] = \'\\r\';\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\tassert(lf_num == 0);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\ti=raw_read_stdin(buf,bufsize);\n\t\t\tif (!s_quiet)\n\t\t\t\t{\n\t\t\t\tif ((i <= 0) || (buf[0] == \'Q\'))\n\t\t\t\t\t{\n\t\t\t\t\tBIO_printf(bio_s_out,"DONE\\n");\n\t\t\t\t\tSHUTDOWN(s);\n\t\t\t\t\tclose_accept_socket();\n\t\t\t\t\tret= -11;\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t\tif ((i <= 0) || (buf[0] == \'q\'))\n\t\t\t\t\t{\n\t\t\t\t\tBIO_printf(bio_s_out,"DONE\\n");\n\t\t\t\t\tif (SSL_version(con) != DTLS1_VERSION)\n SHUTDOWN(s);\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t\tif ((buf[0] == \'r\') &&\n\t\t\t\t\t((buf[1] == \'\\n\') || (buf[1] == \'\\r\')))\n\t\t\t\t\t{\n\t\t\t\t\tSSL_renegotiate(con);\n\t\t\t\t\ti=SSL_do_handshake(con);\n\t\t\t\t\tprintf("SSL_do_handshake -> %d\\n",i);\n\t\t\t\t\ti=0;\n\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\tif ((buf[0] == \'R\') &&\n\t\t\t\t\t((buf[1] == \'\\n\') || (buf[1] == \'\\r\')))\n\t\t\t\t\t{\n\t\t\t\t\tSSL_set_verify(con,\n\t\t\t\t\t\tSSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,NULL);\n\t\t\t\t\tSSL_renegotiate(con);\n\t\t\t\t\ti=SSL_do_handshake(con);\n\t\t\t\t\tprintf("SSL_do_handshake -> %d\\n",i);\n\t\t\t\t\ti=0;\n\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\tif (buf[0] == \'P\')\n\t\t\t\t\t{\n\t\t\t\t\tstatic const char *str="Lets print some clear text\\n";\n\t\t\t\t\tBIO_write(SSL_get_wbio(con),str,strlen(str));\n\t\t\t\t\t}\n\t\t\t\tif (buf[0] == \'S\')\n\t\t\t\t\t{\n\t\t\t\t\tprint_stats(bio_s_out,SSL_get_SSL_CTX(con));\n\t\t\t\t\t}\n\t\t\t\t}\n#ifdef CHARSET_EBCDIC\n\t\t\tebcdic2ascii(buf,buf,i);\n#endif\n\t\t\tl=k=0;\n\t\t\tfor (;;)\n\t\t\t\t{\n#ifdef RENEG\n{ static count=0; if (++count == 100) { count=0; SSL_renegotiate(con); } }\n#endif\n\t\t\t\tk=SSL_write(con,&(buf[l]),(unsigned int)i);\n\t\t\t\tswitch (SSL_get_error(con,k))\n\t\t\t\t\t{\n\t\t\t\tcase SSL_ERROR_NONE:\n\t\t\t\t\tbreak;\n\t\t\t\tcase SSL_ERROR_WANT_WRITE:\n\t\t\t\tcase SSL_ERROR_WANT_READ:\n\t\t\t\tcase SSL_ERROR_WANT_X509_LOOKUP:\n\t\t\t\t\tBIO_printf(bio_s_out,"Write BLOCK\\n");\n\t\t\t\t\tbreak;\n\t\t\t\tcase SSL_ERROR_SYSCALL:\n\t\t\t\tcase SSL_ERROR_SSL:\n\t\t\t\t\tBIO_printf(bio_s_out,"ERROR\\n");\n\t\t\t\t\tERR_print_errors(bio_err);\n\t\t\t\t\tret=1;\n\t\t\t\t\tgoto err;\n\t\t\t\tcase SSL_ERROR_ZERO_RETURN:\n\t\t\t\t\tBIO_printf(bio_s_out,"DONE\\n");\n\t\t\t\t\tret=1;\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t\tl+=k;\n\t\t\t\ti-=k;\n\t\t\t\tif (i <= 0) break;\n\t\t\t\t}\n\t\t\t}\n\t\tif (read_from_sslcon)\n\t\t\t{\n\t\t\tif (!SSL_is_init_finished(con))\n\t\t\t\t{\n\t\t\t\ti=init_ssl_connection(con);\n\t\t\t\tif (i < 0)\n\t\t\t\t\t{\n\t\t\t\t\tret=0;\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t\telse if (i == 0)\n\t\t\t\t\t{\n\t\t\t\t\tret=1;\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\nagain:\n\t\t\t\ti=SSL_read(con,(char *)buf,bufsize);\n\t\t\t\tswitch (SSL_get_error(con,i))\n\t\t\t\t\t{\n\t\t\t\tcase SSL_ERROR_NONE:\n#ifdef CHARSET_EBCDIC\n\t\t\t\t\tascii2ebcdic(buf,buf,i);\n#endif\n\t\t\t\t\traw_write_stdout(buf,\n\t\t\t\t\t\t(unsigned int)i);\n\t\t\t\t\tif (SSL_pending(con)) goto again;\n\t\t\t\t\tbreak;\n\t\t\t\tcase SSL_ERROR_WANT_WRITE:\n\t\t\t\tcase SSL_ERROR_WANT_READ:\n\t\t\t\tcase SSL_ERROR_WANT_X509_LOOKUP:\n\t\t\t\t\tBIO_printf(bio_s_out,"Read BLOCK\\n");\n\t\t\t\t\tbreak;\n\t\t\t\tcase SSL_ERROR_SYSCALL:\n\t\t\t\tcase SSL_ERROR_SSL:\n\t\t\t\t\tBIO_printf(bio_s_out,"ERROR\\n");\n\t\t\t\t\tERR_print_errors(bio_err);\n\t\t\t\t\tret=1;\n\t\t\t\t\tgoto err;\n\t\t\t\tcase SSL_ERROR_ZERO_RETURN:\n\t\t\t\t\tBIO_printf(bio_s_out,"DONE\\n");\n\t\t\t\t\tret=1;\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\nerr:\n\tif (con != NULL)\n\t\t{\n\t\tBIO_printf(bio_s_out,"shutting down SSL\\n");\n#if 1\n\t\tSSL_set_shutdown(con,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);\n#else\n\t\tSSL_shutdown(con);\n#endif\n\t\tSSL_free(con);\n\t\t}\n\tBIO_printf(bio_s_out,"CONNECTION CLOSED\\n");\n\tif (buf != NULL)\n\t\t{\n\t\tOPENSSL_cleanse(buf,bufsize);\n\t\tOPENSSL_free(buf);\n\t\t}\n\tif (ret >= 0)\n\t\tBIO_printf(bio_s_out,"ACCEPT\\n");\n\treturn(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 *)OPENSSL_malloc(sizeof(SSL));\n\tif (s == NULL) goto err;\n\tmemset(s,0,sizeof(SSL));\n#ifndef\tOPENSSL_NO_KRB5\n\ts->kssl_ctx = kssl_ctx_new();\n#endif\n\ts->options=ctx->options;\n\ts->mode=ctx->mode;\n\ts->max_cert_list=ctx->max_cert_list;\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->read_ahead=ctx->read_ahead;\n\ts->msg_callback=ctx->msg_callback;\n\ts->msg_callback_arg=ctx->msg_callback_arg;\n\ts->verify_mode=ctx->verify_mode;\n#if 0\n\ts->verify_depth=ctx->verify_depth;\n#endif\n\ts->sid_ctx_length=ctx->sid_ctx_length;\n\tOPENSSL_assert(s->sid_ctx_length <= sizeof s->sid_ctx);\n\tmemcpy(&s->sid_ctx,&ctx->sid_ctx,sizeof(s->sid_ctx));\n\ts->verify_callback=ctx->default_verify_callback;\n\ts->generate_session_id=ctx->generate_session_id;\n\ts->param = X509_VERIFY_PARAM_new();\n\tif (!s->param)\n\t\tgoto err;\n\tX509_VERIFY_PARAM_inherit(s->param, ctx->param);\n#if 0\n\ts->purpose = ctx->purpose;\n\ts->trust = ctx->trust;\n#endif\n\ts->quiet_shutdown=ctx->quiet_shutdown;\n\ts->max_send_fragment = ctx->max_send_fragment;\n\tCRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);\n\ts->ctx=ctx;\n#ifndef OPENSSL_NO_TLSEXT\n\tCRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);\n\ts->initial_ctx=ctx;\n#endif\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->references=1;\n\ts->server=(ctx->method->ssl_accept == ssl_undefined_function)?0:1;\n\tSSL_clear(s);\n\tCRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);\n#ifndef OPENSSL_NO_PSK\n\ts->psk_client_callback=ctx->psk_client_callback;\n\ts->psk_server_callback=ctx->psk_server_callback;\n#endif\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\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\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->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,ERR_R_INTERNAL_ERROR);\n\t\treturn 0;\n\t\t}\n#endif\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\ts->first_packet=0;\n#if 1\n\tif (!s->in_handshake && (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}', 'void SSL_free(SSL *s)\n\t{\n\tint i;\n\tif(s == NULL)\n\t return;\n\ti=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL);\n#ifdef REF_PRINT\n\tREF_PRINT("SSL",s);\n#endif\n\tif (i > 0) return;\n#ifdef REF_CHECK\n\tif (i < 0)\n\t\t{\n\t\tfprintf(stderr,"SSL_free, bad reference count\\n");\n\t\tabort();\n\t\t}\n#endif\n\tif (s->param)\n\t\tX509_VERIFY_PARAM_free(s->param);\n\tCRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);\n\tif (s->bbio != NULL)\n\t\t{\n\t\tif (s->bbio == s->wbio)\n\t\t\t{\n\t\t\ts->wbio=BIO_pop(s->wbio);\n\t\t\t}\n\t\tBIO_free(s->bbio);\n\t\ts->bbio=NULL;\n\t\t}\n\tif (s->rbio != NULL)\n\t\tBIO_free_all(s->rbio);\n\tif ((s->wbio != NULL) && (s->wbio != s->rbio))\n\t\tBIO_free_all(s->wbio);\n\tif (s->init_buf != NULL) BUF_MEM_free(s->init_buf);\n\tif (s->cipher_list != NULL) sk_SSL_CIPHER_free(s->cipher_list);\n\tif (s->cipher_list_by_id != NULL) sk_SSL_CIPHER_free(s->cipher_list_by_id);\n\tif (s->session != NULL)\n\t\t{\n\t\tssl_clear_bad_session(s);\n\t\tSSL_SESSION_free(s->session);\n\t\t}\n\tssl_clear_cipher_ctx(s);\n\tif (s->cert != NULL) ssl_cert_free(s->cert);\n\tif (s->ctx) SSL_CTX_free(s->ctx);\n#ifndef OPENSSL_NO_TLSEXT\n\tif (s->initial_ctx) SSL_CTX_free(s->initial_ctx);\n#endif\n\tif (s->client_CA != NULL)\n\t\tsk_X509_NAME_pop_free(s->client_CA,X509_NAME_free);\n\tif (s->method != NULL) s->method->ssl_free(s);\n#ifndef\tOPENSSL_NO_KRB5\n\tif (s->kssl_ctx != NULL)\n\t\tkssl_ctx_free(s->kssl_ctx);\n#endif\n\tOPENSSL_free(s);\n\t}', 'void SSL_CTX_free(SSL_CTX *a)\n\t{\n\tint i;\n\tif (a == NULL) return;\n\ti=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX);\n#ifdef REF_PRINT\n\tREF_PRINT("SSL_CTX",a);\n#endif\n\tif (i > 0) return;\n#ifdef REF_CHECK\n\tif (i < 0)\n\t\t{\n\t\tfprintf(stderr,"SSL_CTX_free, bad reference count\\n");\n\t\tabort();\n\t\t}\n#endif\n\tif (a->param)\n\t\tX509_VERIFY_PARAM_free(a->param);\n\tif (a->sessions != NULL)\n\t\tSSL_CTX_flush_sessions(a,0);\n\tCRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);\n\tif (a->sessions != NULL)\n\t\tlh_free(a->sessions);\n\tif (a->cert_store != NULL)\n\t\tX509_STORE_free(a->cert_store);\n\tif (a->cipher_list != NULL)\n\t\tsk_SSL_CIPHER_free(a->cipher_list);\n\tif (a->cipher_list_by_id != NULL)\n\t\tsk_SSL_CIPHER_free(a->cipher_list_by_id);\n\tif (a->cert != NULL)\n\t\tssl_cert_free(a->cert);\n\tif (a->client_CA != NULL)\n\t\tsk_X509_NAME_pop_free(a->client_CA,X509_NAME_free);\n\tif (a->extra_certs != NULL)\n\t\tsk_X509_pop_free(a->extra_certs,X509_free);\n#if 0\n\tif (a->comp_methods != NULL)\n\t\tsk_SSL_COMP_pop_free(a->comp_methods,SSL_COMP_free);\n#else\n\ta->comp_methods = NULL;\n#endif\n#ifndef OPENSSL_NO_PSK\n\tif (a->psk_identity_hint)\n\t\tOPENSSL_free(a->psk_identity_hint);\n#endif\n\tOPENSSL_free(a);\n\t}', 'void SSL_CTX_flush_sessions(SSL_CTX *s, long t)\n\t{\n\tunsigned long i;\n\tTIMEOUT_PARAM tp;\n\ttp.ctx=s;\n\ttp.cache=s->sessions;\n\tif (tp.cache == NULL) return;\n\ttp.time=t;\n\tCRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);\n\ti=tp.cache->down_load;\n\ttp.cache->down_load=0;\n\tlh_doall_arg(tp.cache, LHASH_DOALL_ARG_FN(timeout), &tp);\n\ttp.cache->down_load=i;\n\tCRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n\t}', 'void lh_doall_arg(LHASH *lh, LHASH_DOALL_ARG_FN_TYPE func, void *arg)\n\t{\n\tdoall_util_fn(lh, 1, (LHASH_DOALL_FN_TYPE)0, func, arg);\n\t}', 'static void doall_util_fn(LHASH *lh, int use_arg, LHASH_DOALL_FN_TYPE func,\n\t\t\t LHASH_DOALL_ARG_FN_TYPE func_arg, void *arg)\n\t{\n\tint i;\n\tLHASH_NODE *a,*n;\n\tfor (i=lh->num_nodes-1; i>=0; i--)\n\t\t{\n\t\ta=lh->b[i];\n\t\twhile (a != NULL)\n\t\t\t{\n\t\t\tn=a->next;\n\t\t\tif(use_arg)\n\t\t\t\tfunc_arg(a->data,arg);\n\t\t\telse\n\t\t\t\tfunc(a->data);\n\t\t\ta=n;\n\t\t\t}\n\t\t}\n\t}']
|
2,607
| 0
|
https://github.com/openssl/openssl/blob/4b8515baa6edef1a771f9e4e3fbc0395b4a629e8/crypto/bn/bn_ctx.c/#L273
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['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_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 0;\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_is_one(m)) {\n ret = 1;\n BN_zero(r);\n } else {\n ret = BN_one(r);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n aa = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (val[0] == NULL)\n goto err;\n BN_RECP_CTX_init(&recp);\n if (m->neg) {\n if (!BN_copy(aa, m))\n goto err;\n aa->neg = 0;\n if (BN_RECP_CTX_set(&recp, aa, ctx) <= 0)\n goto err;\n } else {\n if (BN_RECP_CTX_set(&recp, m, ctx) <= 0)\n goto err;\n }\n if (!BN_nnmod(val[0], a, m, ctx))\n goto err;\n if (BN_is_zero(val[0])) {\n BN_zero(r);\n ret = 1;\n goto err;\n }\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!BN_mod_mul_reciprocal(aa, val[0], val[0], &recp, ctx))\n goto err;\n j = 1 << (window - 1);\n for (i = 1; i < j; i++) {\n if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul_reciprocal(val[i], val[i - 1], aa, &recp, ctx))\n goto err;\n }\n }\n start = 1;\n wvalue = 0;\n wstart = bits - 1;\n wend = 0;\n if (!BN_one(r))\n goto err;\n for (;;) {\n if (BN_is_bit_set(p, wstart) == 0) {\n if (!start)\n if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))\n goto err;\n if (wstart == 0)\n break;\n wstart--;\n continue;\n }\n j = wstart;\n wvalue = 1;\n wend = 0;\n for (i = 1; i < window; i++) {\n if (wstart - i < 0)\n break;\n if (BN_is_bit_set(p, wstart - i)) {\n wvalue <<= (i - wend);\n wvalue |= 1;\n wend = i;\n }\n }\n j = wend + 1;\n if (!start)\n for (i = 0; i < j; i++) {\n if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))\n goto err;\n }\n if (!BN_mod_mul_reciprocal(r, r, val[wvalue >> 1], &recp, ctx))\n goto err;\n wstart -= wend + 1;\n wvalue = 0;\n start = 0;\n if (wstart < 0)\n break;\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_RECP_CTX_free(&recp);\n bn_check_top(r);\n return (ret);\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_start", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG_EXIT(ctx);\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return (0);\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return (0);\n }\n if (dv != NULL)\n BN_zero(dv);\n return (1);\n }\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n resp++;\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n resp--;\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return (1);\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return (0);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
|
2,608
| 0
|
https://github.com/libav/libav/blob/18b59956e0e94017f1b519bb42c7c937b2f9f8a4/libavcodec/mp3_header_compress_bsf.c/#L52
|
static int mp3_header_compress(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
uint8_t **poutbuf, int *poutbuf_size,
const uint8_t *buf, int buf_size, int keyframe){
uint32_t header, extraheader;
int mode_extension, header_size;
if(avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){
av_log(avctx, AV_LOG_ERROR, "not standards compliant\n");
return -1;
}
header = AV_RB32(buf);
mode_extension= (header>>4)&3;
if(ff_mpa_check_header(header) < 0 || (header&0x60000) != 0x20000){
output_unchanged:
*poutbuf= (uint8_t *) buf;
*poutbuf_size= buf_size;
av_log(avctx, AV_LOG_INFO, "cannot compress %08X\n", header);
return 0;
}
if(avctx->extradata_size == 0){
avctx->extradata_size=15;
avctx->extradata= av_malloc(avctx->extradata_size);
strcpy(avctx->extradata, "FFCMP3 0.0");
memcpy(avctx->extradata+11, buf, 4);
}
if(avctx->extradata_size != 15){
av_log(avctx, AV_LOG_ERROR, "Extradata invalid\n");
return -1;
}
extraheader = AV_RB32(avctx->extradata+11);
if((extraheader&MP3_MASK) != (header&MP3_MASK))
goto output_unchanged;
header_size= (header&0x10000) ? 4 : 6;
*poutbuf_size= buf_size - header_size;
*poutbuf= av_malloc(buf_size - header_size + FF_INPUT_BUFFER_PADDING_SIZE);
memcpy(*poutbuf, buf + header_size, buf_size - header_size + FF_INPUT_BUFFER_PADDING_SIZE);
if(avctx->channels==2){
if((header & (3<<19)) != 3<<19){
(*poutbuf)[1] &= 0x3F;
(*poutbuf)[1] |= mode_extension<<6;
FFSWAP(int, (*poutbuf)[1], (*poutbuf)[2]);
}else{
(*poutbuf)[1] &= 0x8F;
(*poutbuf)[1] |= mode_extension<<4;
}
}
return 1;
}
|
['static int mp3_header_compress(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,\n uint8_t **poutbuf, int *poutbuf_size,\n const uint8_t *buf, int buf_size, int keyframe){\n uint32_t header, extraheader;\n int mode_extension, header_size;\n if(avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){\n av_log(avctx, AV_LOG_ERROR, "not standards compliant\\n");\n return -1;\n }\n header = AV_RB32(buf);\n mode_extension= (header>>4)&3;\n if(ff_mpa_check_header(header) < 0 || (header&0x60000) != 0x20000){\noutput_unchanged:\n *poutbuf= (uint8_t *) buf;\n *poutbuf_size= buf_size;\n av_log(avctx, AV_LOG_INFO, "cannot compress %08X\\n", header);\n return 0;\n }\n if(avctx->extradata_size == 0){\n avctx->extradata_size=15;\n avctx->extradata= av_malloc(avctx->extradata_size);\n strcpy(avctx->extradata, "FFCMP3 0.0");\n memcpy(avctx->extradata+11, buf, 4);\n }\n if(avctx->extradata_size != 15){\n av_log(avctx, AV_LOG_ERROR, "Extradata invalid\\n");\n return -1;\n }\n extraheader = AV_RB32(avctx->extradata+11);\n if((extraheader&MP3_MASK) != (header&MP3_MASK))\n goto output_unchanged;\n header_size= (header&0x10000) ? 4 : 6;\n *poutbuf_size= buf_size - header_size;\n *poutbuf= av_malloc(buf_size - header_size + FF_INPUT_BUFFER_PADDING_SIZE);\n memcpy(*poutbuf, buf + header_size, buf_size - header_size + FF_INPUT_BUFFER_PADDING_SIZE);\n if(avctx->channels==2){\n if((header & (3<<19)) != 3<<19){\n (*poutbuf)[1] &= 0x3F;\n (*poutbuf)[1] |= mode_extension<<6;\n FFSWAP(int, (*poutbuf)[1], (*poutbuf)[2]);\n }else{\n (*poutbuf)[1] &= 0x8F;\n (*poutbuf)[1] |= mode_extension<<4;\n }\n }\n return 1;\n}', 'static av_always_inline av_const uint32_t av_bswap32(uint32_t x)\n{\n return AV_BSWAP32C(x);\n}', 'static inline int ff_mpa_check_header(uint32_t header){\n if ((header & 0xffe00000) != 0xffe00000)\n return -1;\n if ((header & (3<<17)) == 0)\n return -1;\n if ((header & (0xf<<12)) == 0xf<<12)\n return -1;\n if ((header & (3<<10)) == 3<<10)\n return -1;\n return 0;\n}', 'void *av_malloc(size_t size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n 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_MEMALIGN\n ptr = memalign(32,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}']
|
2,609
| 0
|
https://github.com/libav/libav/blob/2f99117f6ff24ce5be2abb9e014cb8b86c2aa0e0/libavcodec/bitstream.h/#L68
|
static inline void refill_32(BitstreamContext *bc)
{
if (bc->ptr >= bc->buffer_end)
return;
#ifdef BITSTREAM_READER_LE
bc->bits = (uint64_t)AV_RL32(bc->ptr) << bc->bits_left | bc->bits;
#else
bc->bits = bc->bits | (uint64_t)AV_RB32(bc->ptr) << (32 - bc->bits_left);
#endif
bc->ptr += 4;
bc->bits_left += 32;
}
|
['static int twinvq_read_bitstream(AVCodecContext *avctx, TwinVQContext *tctx,\n const uint8_t *buf, int buf_size)\n{\n TwinVQFrameData *bits = &tctx->bits[0];\n const TwinVQModeTab *mtab = tctx->mtab;\n int channels = tctx->avctx->channels;\n int sub;\n BitstreamContext bc;\n int i, j, k;\n bitstream_init(&bc, buf, buf_size * 8);\n bitstream_skip(&bc, bitstream_read(&bc, 8));\n bits->window_type = bitstream_read(&bc, TWINVQ_WINDOW_TYPE_BITS);\n if (bits->window_type > 8) {\n av_log(avctx, AV_LOG_ERROR, "Invalid window type, broken sample?\\n");\n return AVERROR_INVALIDDATA;\n }\n bits->ftype = ff_twinvq_wtype_to_ftype_table[tctx->bits[0].window_type];\n sub = mtab->fmode[bits->ftype].sub;\n read_cb_data(tctx, &bc, bits->main_coeffs, bits->ftype);\n for (i = 0; i < channels; i++)\n for (j = 0; j < sub; j++)\n for (k = 0; k < mtab->fmode[bits->ftype].bark_n_coef; k++)\n bits->bark1[i][j][k] =\n bitstream_read(&bc, mtab->fmode[bits->ftype].bark_n_bit);\n for (i = 0; i < channels; i++)\n for (j = 0; j < sub; j++)\n bits->bark_use_hist[i][j] = bitstream_read_bit(&bc);\n if (bits->ftype == TWINVQ_FT_LONG) {\n for (i = 0; i < channels; i++)\n bits->gain_bits[i] = bitstream_read(&bc, TWINVQ_GAIN_BITS);\n } else {\n for (i = 0; i < channels; i++) {\n bits->gain_bits[i] = bitstream_read(&bc, TWINVQ_GAIN_BITS);\n for (j = 0; j < sub; j++)\n bits->sub_gain_bits[i * sub + j] = bitstream_read(&bc, TWINVQ_SUB_GAIN_BITS);\n }\n }\n for (i = 0; i < channels; i++) {\n bits->lpc_hist_idx[i] = bitstream_read(&bc, mtab->lsp_bit0);\n bits->lpc_idx1[i] = bitstream_read(&bc, mtab->lsp_bit1);\n for (j = 0; j < mtab->lsp_split; j++)\n bits->lpc_idx2[i][j] = bitstream_read(&bc, mtab->lsp_bit2);\n }\n if (bits->ftype == TWINVQ_FT_LONG) {\n read_cb_data(tctx, &bc, bits->ppc_coeffs, 3);\n for (i = 0; i < channels; i++) {\n bits->p_coef[i] = bitstream_read(&bc, mtab->ppc_period_bit);\n bits->g_coef[i] = bitstream_read(&bc, mtab->pgain_bit);\n }\n }\n return 0;\n}', 'static inline int bitstream_init(BitstreamContext *bc, const uint8_t *buffer,\n unsigned bit_size)\n{\n unsigned buffer_size;\n if (bit_size > INT_MAX - 7 || !buffer) {\n buffer =\n bc->buffer =\n bc->ptr = NULL;\n bc->bits_left = 0;\n return AVERROR_INVALIDDATA;\n }\n buffer_size = (bit_size + 7) >> 3;\n bc->buffer = buffer;\n bc->buffer_end = buffer + buffer_size;\n bc->ptr = bc->buffer;\n bc->size_in_bits = bit_size;\n bc->bits_left = 0;\n bc->bits = 0;\n refill_64(bc);\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 void refill_32(BitstreamContext *bc)\n{\n if (bc->ptr >= bc->buffer_end)\n return;\n#ifdef BITSTREAM_READER_LE\n bc->bits = (uint64_t)AV_RL32(bc->ptr) << bc->bits_left | bc->bits;\n#else\n bc->bits = bc->bits | (uint64_t)AV_RB32(bc->ptr) << (32 - bc->bits_left);\n#endif\n bc->ptr += 4;\n bc->bits_left += 32;\n}']
|
2,610
| 0
|
https://github.com/openssl/openssl/blob/249a77f5fb6407185e0a6ad44cd88eda2b6f8946/apps/crl2p7.c/#L144
|
int MAIN(int argc, char **argv)
{
int i,badops=0;
BIO *in=NULL,*out=NULL;
int informat,outformat;
char *infile,*outfile,*prog,*certfile;
PKCS7 *p7 = NULL;
PKCS7_SIGNED *p7s = NULL;
X509_CRL *crl=NULL;
STACK_OF(STRING) *certflst=NULL;
STACK_OF(X509_CRL) *crl_stack=NULL;
STACK_OF(X509) *cert_stack=NULL;
int ret=1,nocrl=0;
apps_startup();
if (bio_err == NULL)
if ((bio_err=BIO_new(BIO_s_file())) != NULL)
BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
infile=NULL;
outfile=NULL;
informat=FORMAT_PEM;
outformat=FORMAT_PEM;
prog=argv[0];
argc--;
argv++;
while (argc >= 1)
{
if (strcmp(*argv,"-inform") == 0)
{
if (--argc < 1) goto bad;
informat=str2fmt(*(++argv));
}
else if (strcmp(*argv,"-outform") == 0)
{
if (--argc < 1) goto bad;
outformat=str2fmt(*(++argv));
}
else if (strcmp(*argv,"-in") == 0)
{
if (--argc < 1) goto bad;
infile= *(++argv);
}
else if (strcmp(*argv,"-nocrl") == 0)
{
nocrl=1;
}
else if (strcmp(*argv,"-out") == 0)
{
if (--argc < 1) goto bad;
outfile= *(++argv);
}
else if (strcmp(*argv,"-certfile") == 0)
{
if (--argc < 1) goto bad;
if(!certflst) certflst = sk_STRING_new_null();
sk_STRING_push(certflst,*(++argv));
}
else
{
BIO_printf(bio_err,"unknown option %s\n",*argv);
badops=1;
break;
}
argc--;
argv++;
}
if (badops)
{
bad:
BIO_printf(bio_err,"%s [options] <infile >outfile\n",prog);
BIO_printf(bio_err,"where options are\n");
BIO_printf(bio_err," -inform arg input format - DER or PEM\n");
BIO_printf(bio_err," -outform arg output format - DER or PEM\n");
BIO_printf(bio_err," -in arg input file\n");
BIO_printf(bio_err," -out arg output file\n");
BIO_printf(bio_err," -certfile arg certificates file of chain to a trusted CA\n");
BIO_printf(bio_err," (can be used more than once)\n");
BIO_printf(bio_err," -nocrl no crl to load, just certs from '-certfile'\n");
ret = 1;
goto end;
}
ERR_load_crypto_strings();
in=BIO_new(BIO_s_file());
out=BIO_new(BIO_s_file());
if ((in == NULL) || (out == NULL))
{
ERR_print_errors(bio_err);
goto end;
}
if (!nocrl)
{
if (infile == NULL)
BIO_set_fp(in,stdin,BIO_NOCLOSE);
else
{
if (BIO_read_filename(in,infile) <= 0)
{
perror(infile);
goto end;
}
}
if (informat == FORMAT_ASN1)
crl=d2i_X509_CRL_bio(in,NULL);
else if (informat == FORMAT_PEM)
crl=PEM_read_bio_X509_CRL(in,NULL,NULL,NULL);
else {
BIO_printf(bio_err,"bad input format specified for input crl\n");
goto end;
}
if (crl == NULL)
{
BIO_printf(bio_err,"unable to load CRL\n");
ERR_print_errors(bio_err);
goto end;
}
}
if ((p7=PKCS7_new()) == NULL) goto end;
if ((p7s=PKCS7_SIGNED_new()) == NULL) goto end;
p7->type=OBJ_nid2obj(NID_pkcs7_signed);
p7->d.sign=p7s;
p7s->contents->type=OBJ_nid2obj(NID_pkcs7_data);
if (!ASN1_INTEGER_set(p7s->version,1)) goto end;
if ((crl_stack=sk_X509_CRL_new_null()) == NULL) goto end;
p7s->crl=crl_stack;
if (crl != NULL)
{
sk_X509_CRL_push(crl_stack,crl);
crl=NULL;
}
if ((cert_stack=sk_X509_new_null()) == NULL) goto end;
p7s->cert=cert_stack;
if(certflst) for(i = 0; i < sk_STRING_num(certflst); i++) {
certfile = sk_STRING_value(certflst, i);
if (add_certs_from_file(cert_stack,certfile) < 0)
{
BIO_printf(bio_err, "error loading certificates\n");
ERR_print_errors(bio_err);
goto end;
}
}
sk_STRING_free(certflst);
if (outfile == NULL)
{
BIO_set_fp(out,stdout,BIO_NOCLOSE);
#ifdef OPENSSL_SYS_VMS
{
BIO *tmpbio = BIO_new(BIO_f_linebuffer());
out = BIO_push(tmpbio, out);
}
#endif
}
else
{
if (BIO_write_filename(out,outfile) <= 0)
{
perror(outfile);
goto end;
}
}
if (outformat == FORMAT_ASN1)
i=i2d_PKCS7_bio(out,p7);
else if (outformat == FORMAT_PEM)
i=PEM_write_bio_PKCS7(out,p7);
else {
BIO_printf(bio_err,"bad output format specified for outfile\n");
goto end;
}
if (!i)
{
BIO_printf(bio_err,"unable to write pkcs7 object\n");
ERR_print_errors(bio_err);
goto end;
}
ret=0;
end:
if (in != NULL) BIO_free(in);
if (out != NULL) BIO_free_all(out);
if (p7 != NULL) PKCS7_free(p7);
if (crl != NULL) X509_CRL_free(crl);
apps_shutdown();
OPENSSL_EXIT(ret);
}
|
['int MAIN(int argc, char **argv)\n\t{\n\tint i,badops=0;\n\tBIO *in=NULL,*out=NULL;\n\tint informat,outformat;\n\tchar *infile,*outfile,*prog,*certfile;\n\tPKCS7 *p7 = NULL;\n\tPKCS7_SIGNED *p7s = NULL;\n\tX509_CRL *crl=NULL;\n\tSTACK_OF(STRING) *certflst=NULL;\n\tSTACK_OF(X509_CRL) *crl_stack=NULL;\n\tSTACK_OF(X509) *cert_stack=NULL;\n\tint ret=1,nocrl=0;\n\tapps_startup();\n\tif (bio_err == NULL)\n\t\tif ((bio_err=BIO_new(BIO_s_file())) != NULL)\n\t\t\tBIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);\n\tinfile=NULL;\n\toutfile=NULL;\n\tinformat=FORMAT_PEM;\n\toutformat=FORMAT_PEM;\n\tprog=argv[0];\n\targc--;\n\targv++;\n\twhile (argc >= 1)\n\t\t{\n\t\tif \t(strcmp(*argv,"-inform") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tinformat=str2fmt(*(++argv));\n\t\t\t}\n\t\telse if (strcmp(*argv,"-outform") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\toutformat=str2fmt(*(++argv));\n\t\t\t}\n\t\telse if (strcmp(*argv,"-in") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tinfile= *(++argv);\n\t\t\t}\n\t\telse if (strcmp(*argv,"-nocrl") == 0)\n\t\t\t{\n\t\t\tnocrl=1;\n\t\t\t}\n\t\telse if (strcmp(*argv,"-out") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\toutfile= *(++argv);\n\t\t\t}\n\t\telse if (strcmp(*argv,"-certfile") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tif(!certflst) certflst = sk_STRING_new_null();\n\t\t\tsk_STRING_push(certflst,*(++argv));\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"unknown option %s\\n",*argv);\n\t\t\tbadops=1;\n\t\t\tbreak;\n\t\t\t}\n\t\targc--;\n\t\targv++;\n\t\t}\n\tif (badops)\n\t\t{\nbad:\n\t\tBIO_printf(bio_err,"%s [options] <infile >outfile\\n",prog);\n\t\tBIO_printf(bio_err,"where options are\\n");\n\t\tBIO_printf(bio_err," -inform arg input format - DER or PEM\\n");\n\t\tBIO_printf(bio_err," -outform arg output format - DER or PEM\\n");\n\t\tBIO_printf(bio_err," -in arg input file\\n");\n\t\tBIO_printf(bio_err," -out arg output file\\n");\n\t\tBIO_printf(bio_err," -certfile arg certificates file of chain to a trusted CA\\n");\n\t\tBIO_printf(bio_err," (can be used more than once)\\n");\n\t\tBIO_printf(bio_err," -nocrl no crl to load, just certs from \'-certfile\'\\n");\n\t\tret = 1;\n\t\tgoto end;\n\t\t}\n\tERR_load_crypto_strings();\n\tin=BIO_new(BIO_s_file());\n\tout=BIO_new(BIO_s_file());\n\tif ((in == NULL) || (out == NULL))\n\t\t{\n\t\tERR_print_errors(bio_err);\n\t\tgoto end;\n\t\t}\n\tif (!nocrl)\n\t\t{\n\t\tif (infile == NULL)\n\t\t\tBIO_set_fp(in,stdin,BIO_NOCLOSE);\n\t\telse\n\t\t\t{\n\t\t\tif (BIO_read_filename(in,infile) <= 0)\n\t\t\t\t{\n\t\t\t\tperror(infile);\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\t}\n\t\tif \t(informat == FORMAT_ASN1)\n\t\t\tcrl=d2i_X509_CRL_bio(in,NULL);\n\t\telse if (informat == FORMAT_PEM)\n\t\t\tcrl=PEM_read_bio_X509_CRL(in,NULL,NULL,NULL);\n\t\telse\t{\n\t\t\tBIO_printf(bio_err,"bad input format specified for input crl\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\tif (crl == NULL)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"unable to load CRL\\n");\n\t\t\tERR_print_errors(bio_err);\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n\tif ((p7=PKCS7_new()) == NULL) goto end;\n\tif ((p7s=PKCS7_SIGNED_new()) == NULL) goto end;\n\tp7->type=OBJ_nid2obj(NID_pkcs7_signed);\n\tp7->d.sign=p7s;\n\tp7s->contents->type=OBJ_nid2obj(NID_pkcs7_data);\n\tif (!ASN1_INTEGER_set(p7s->version,1)) goto end;\n\tif ((crl_stack=sk_X509_CRL_new_null()) == NULL) goto end;\n\tp7s->crl=crl_stack;\n\tif (crl != NULL)\n\t\t{\n\t\tsk_X509_CRL_push(crl_stack,crl);\n\t\tcrl=NULL;\n\t\t}\n\tif ((cert_stack=sk_X509_new_null()) == NULL) goto end;\n\tp7s->cert=cert_stack;\n\tif(certflst) for(i = 0; i < sk_STRING_num(certflst); i++) {\n\t\tcertfile = sk_STRING_value(certflst, i);\n\t\tif (add_certs_from_file(cert_stack,certfile) < 0)\n\t\t\t{\n\t\t\tBIO_printf(bio_err, "error loading certificates\\n");\n\t\t\tERR_print_errors(bio_err);\n\t\t\tgoto end;\n\t\t\t}\n\t}\n\tsk_STRING_free(certflst);\n\tif (outfile == NULL)\n\t\t{\n\t\tBIO_set_fp(out,stdout,BIO_NOCLOSE);\n#ifdef OPENSSL_SYS_VMS\n\t\t{\n\t\tBIO *tmpbio = BIO_new(BIO_f_linebuffer());\n\t\tout = BIO_push(tmpbio, out);\n\t\t}\n#endif\n\t\t}\n\telse\n\t\t{\n\t\tif (BIO_write_filename(out,outfile) <= 0)\n\t\t\t{\n\t\t\tperror(outfile);\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n\tif \t(outformat == FORMAT_ASN1)\n\t\ti=i2d_PKCS7_bio(out,p7);\n\telse if (outformat == FORMAT_PEM)\n\t\ti=PEM_write_bio_PKCS7(out,p7);\n\telse\t{\n\t\tBIO_printf(bio_err,"bad output format specified for outfile\\n");\n\t\tgoto end;\n\t\t}\n\tif (!i)\n\t\t{\n\t\tBIO_printf(bio_err,"unable to write pkcs7 object\\n");\n\t\tERR_print_errors(bio_err);\n\t\tgoto end;\n\t\t}\n\tret=0;\nend:\n\tif (in != NULL) BIO_free(in);\n\tif (out != NULL) BIO_free_all(out);\n\tif (p7 != NULL) PKCS7_free(p7);\n\tif (crl != NULL) X509_CRL_free(crl);\n\tapps_shutdown();\n\tOPENSSL_EXIT(ret);\n\t}', '_STACK *sk_new_null(void)\n\t{\n\treturn sk_new((int (*)(const void * const *, const void * const *))0);\n\t}', '_STACK *sk_new(int (*c)(const void * const *, const void * const *))\n\t{\n\t_STACK *ret;\n\tint i;\n\tif ((ret=OPENSSL_malloc(sizeof(_STACK))) == NULL)\n\t\tgoto err;\n\tif ((ret->data=OPENSSL_malloc(sizeof(char *)*MIN_NODES)) == NULL)\n\t\tgoto err;\n\tfor (i=0; i<MIN_NODES; i++)\n\t\tret->data[i]=NULL;\n\tret->comp=c;\n\tret->num_alloc=MIN_NODES;\n\tret->num=0;\n\tret->sorted=0;\n\treturn(ret);\nerr:\n\tif(ret)\n\t\tOPENSSL_free(ret);\n\treturn(NULL);\n\t}', 'void *CRYPTO_malloc(int num, const char *file, int line)\n\t{\n\tvoid *ret = NULL;\n\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}', 'int sk_push(_STACK *st, void *data)\n\t{\n\treturn(sk_insert(st,data,st->num));\n\t}']
|
2,611
| 0
|
https://github.com/openssl/openssl/blob/388de53c274dee20c07eee7ff892108668fb3a61/apps/speed.c/#L972
|
static int EVP_Update_loop_ccm(void *args)
{
loopargs_t *tempargs = *(loopargs_t **) args;
unsigned char *buf = tempargs->buf;
EVP_CIPHER_CTX *ctx = tempargs->ctx;
int outl, count;
unsigned char tag[12];
#ifndef SIGALRM
int nb_iter = save_count * 4 * lengths[0] / lengths[testnum];
#endif
if (decrypt) {
for (count = 0; COND(nb_iter); count++) {
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, sizeof(tag), tag);
EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv);
EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
}
} else {
for (count = 0; COND(nb_iter); count++) {
EVP_EncryptUpdate(ctx, NULL, &outl, NULL, lengths[testnum]);
EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
}
}
if (decrypt)
EVP_DecryptFinal_ex(ctx, buf, &outl);
else
EVP_EncryptFinal_ex(ctx, buf, &outl);
return count;
}
|
['static int EVP_Update_loop_ccm(void *args)\n{\n loopargs_t *tempargs = *(loopargs_t **) args;\n unsigned char *buf = tempargs->buf;\n EVP_CIPHER_CTX *ctx = tempargs->ctx;\n int outl, count;\n unsigned char tag[12];\n#ifndef SIGALRM\n int nb_iter = save_count * 4 * lengths[0] / lengths[testnum];\n#endif\n if (decrypt) {\n for (count = 0; COND(nb_iter); count++) {\n EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, sizeof(tag), tag);\n EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv);\n EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);\n }\n } else {\n for (count = 0; COND(nb_iter); count++) {\n EVP_EncryptUpdate(ctx, NULL, &outl, NULL, lengths[testnum]);\n EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);\n }\n }\n if (decrypt)\n EVP_DecryptFinal_ex(ctx, buf, &outl);\n else\n EVP_EncryptFinal_ex(ctx, buf, &outl);\n return count;\n}', 'int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,\n const unsigned char *in, int inl)\n{\n int i, j, bl, cmpl = inl;\n if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))\n cmpl = (cmpl + 7) / 8;\n bl = ctx->cipher->block_size;\n if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {\n if (bl == 1 && is_partially_overlapping(out, in, cmpl)) {\n EVPerr(EVP_F_EVP_ENCRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);\n return 0;\n }\n i = ctx->cipher->do_cipher(ctx, out, in, inl);\n if (i < 0)\n return 0;\n else\n *outl = i;\n return 1;\n }\n if (inl <= 0) {\n *outl = 0;\n return inl == 0;\n }\n if (is_partially_overlapping(out + ctx->buf_len, in, cmpl)) {\n EVPerr(EVP_F_EVP_ENCRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);\n return 0;\n }\n if (ctx->buf_len == 0 && (inl & (ctx->block_mask)) == 0) {\n if (ctx->cipher->do_cipher(ctx, out, in, inl)) {\n *outl = inl;\n return 1;\n } else {\n *outl = 0;\n return 0;\n }\n }\n i = ctx->buf_len;\n OPENSSL_assert(bl <= (int)sizeof(ctx->buf));\n if (i != 0) {\n if (bl - i > inl) {\n memcpy(&(ctx->buf[i]), in, inl);\n ctx->buf_len += inl;\n *outl = 0;\n return 1;\n } else {\n j = bl - i;\n memcpy(&(ctx->buf[i]), in, j);\n inl -= j;\n in += j;\n if (!ctx->cipher->do_cipher(ctx, out, ctx->buf, bl))\n return 0;\n out += bl;\n *outl = bl;\n }\n } else\n *outl = 0;\n i = inl & (bl - 1);\n inl -= i;\n if (inl > 0) {\n if (!ctx->cipher->do_cipher(ctx, out, in, inl))\n return 0;\n *outl += inl;\n }\n if (i != 0)\n memcpy(ctx->buf, &(in[inl]), i);\n ctx->buf_len = i;\n return 1;\n}', 'int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags)\n{\n return (ctx->flags & flags);\n}', 'int is_partially_overlapping(const void *ptr1, const void *ptr2, int len)\n{\n PTRDIFF_T diff = (PTRDIFF_T)ptr1-(PTRDIFF_T)ptr2;\n int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) |\n (diff > (0 - (PTRDIFF_T)len)));\n return overlapped;\n}']
|
2,612
| 0
|
https://github.com/openssl/openssl/blob/b2cf7c6452aae1e85ade176bf54ad89bf0263eb2/ssl/ssl_lib.c/#L1377
|
STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s,unsigned char *p,int num,
STACK_OF(SSL_CIPHER) **skp)
{
const SSL_CIPHER *c;
STACK_OF(SSL_CIPHER) *sk;
int i,n;
n=ssl_put_cipher_by_char(s,NULL,NULL);
if ((num%n) != 0)
{
SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
return(NULL);
}
if ((skp == NULL) || (*skp == NULL))
sk=sk_SSL_CIPHER_new_null();
else
{
sk= *skp;
sk_SSL_CIPHER_zero(sk);
}
for (i=0; i<num; i+=n)
{
c=ssl_get_cipher_by_char(s,p);
p+=n;
if (c != NULL)
{
if (!sk_SSL_CIPHER_push(sk,c))
{
SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,ERR_R_MALLOC_FAILURE);
goto err;
}
}
}
if (skp != NULL)
*skp=sk;
return(sk);
err:
if ((skp == NULL) || (*skp == NULL))
sk_SSL_CIPHER_free(sk);
return(NULL);
}
|
['STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s,unsigned char *p,int num,\n\t\t\t\t\t STACK_OF(SSL_CIPHER) **skp)\n\t{\n\tconst SSL_CIPHER *c;\n\tSTACK_OF(SSL_CIPHER) *sk;\n\tint i,n;\n\tn=ssl_put_cipher_by_char(s,NULL,NULL);\n\tif ((num%n) != 0)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);\n\t\treturn(NULL);\n\t\t}\n\tif ((skp == NULL) || (*skp == NULL))\n\t\tsk=sk_SSL_CIPHER_new_null();\n\telse\n\t\t{\n\t\tsk= *skp;\n\t\tsk_SSL_CIPHER_zero(sk);\n\t\t}\n\tfor (i=0; i<num; i+=n)\n\t\t{\n\t\tc=ssl_get_cipher_by_char(s,p);\n\t\tp+=n;\n\t\tif (c != NULL)\n\t\t\t{\n\t\t\tif (!sk_SSL_CIPHER_push(sk,c))\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,ERR_R_MALLOC_FAILURE);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\tif (skp != NULL)\n\t\t*skp=sk;\n\treturn(sk);\nerr:\n\tif ((skp == NULL) || (*skp == NULL))\n\t\tsk_SSL_CIPHER_free(sk);\n\treturn(NULL);\n\t}', '_STACK *sk_new_null(void)\n\t{\n\treturn sk_new((int (*)(const void *, const void *))0);\n\t}', '_STACK *sk_new(int (*c)(const void *, const void *))\n\t{\n\t_STACK *ret;\n\tint i;\n\tif ((ret=OPENSSL_malloc(sizeof(_STACK))) == NULL)\n\t\tgoto err;\n\tif ((ret->data=OPENSSL_malloc(sizeof(char *)*MIN_NODES)) == NULL)\n\t\tgoto err;\n\tfor (i=0; i<MIN_NODES; i++)\n\t\tret->data[i]=NULL;\n\tret->comp=c;\n\tret->num_alloc=MIN_NODES;\n\tret->num=0;\n\tret->sorted=0;\n\treturn(ret);\nerr:\n\tif(ret)\n\t\tOPENSSL_free(ret);\n\treturn(NULL);\n\t}', 'void *CRYPTO_malloc(int num, const char *file, int line)\n\t{\n\tvoid *ret = NULL;\n\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}', 'int sk_push(_STACK *st, void *data)\n\t{\n\treturn(sk_insert(st,data,st->num));\n\t}']
|
2,613
| 0
|
https://github.com/libav/libav/blob/fd16f567987524a769d5d4f1f69089f000386ac2/libavcodec/dca.c/#L794
|
static int dca_subframe_header(DCAContext *s, int base_channel, int block_index)
{
int j, k;
if (get_bits_left(&s->gb) < 0)
return AVERROR_INVALIDDATA;
if (!base_channel) {
s->subsubframes[s->current_subframe] = get_bits(&s->gb, 2) + 1;
s->partial_samples[s->current_subframe] = get_bits(&s->gb, 3);
}
for (j = base_channel; j < s->prim_channels; j++) {
for (k = 0; k < s->subband_activity[j]; k++)
s->prediction_mode[j][k] = get_bits(&s->gb, 1);
}
for (j = base_channel; j < s->prim_channels; j++) {
for (k = 0; k < s->subband_activity[j]; k++) {
if (s->prediction_mode[j][k] > 0) {
s->prediction_vq[j][k] = get_bits(&s->gb, 12);
}
}
}
for (j = base_channel; j < s->prim_channels; j++) {
for (k = 0; k < s->vq_start_subband[j]; k++) {
if (s->bitalloc_huffman[j] == 6)
s->bitalloc[j][k] = get_bits(&s->gb, 5);
else if (s->bitalloc_huffman[j] == 5)
s->bitalloc[j][k] = get_bits(&s->gb, 4);
else if (s->bitalloc_huffman[j] == 7) {
av_log(s->avctx, AV_LOG_ERROR,
"Invalid bit allocation index\n");
return AVERROR_INVALIDDATA;
} else {
s->bitalloc[j][k] =
get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
}
if (s->bitalloc[j][k] > 26) {
return AVERROR_INVALIDDATA;
}
}
}
for (j = base_channel; j < s->prim_channels; j++) {
for (k = 0; k < s->subband_activity[j]; k++) {
s->transition_mode[j][k] = 0;
if (s->subsubframes[s->current_subframe] > 1 &&
k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
s->transition_mode[j][k] =
get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
}
}
}
if (get_bits_left(&s->gb) < 0)
return AVERROR_INVALIDDATA;
for (j = base_channel; j < s->prim_channels; j++) {
const uint32_t *scale_table;
int scale_sum;
memset(s->scale_factor[j], 0,
s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
if (s->scalefactor_huffman[j] == 6)
scale_table = scale_factor_quant7;
else
scale_table = scale_factor_quant6;
scale_sum = 0;
for (k = 0; k < s->subband_activity[j]; k++) {
if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum);
s->scale_factor[j][k][0] = scale_table[scale_sum];
}
if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum);
s->scale_factor[j][k][1] = scale_table[scale_sum];
}
}
}
for (j = base_channel; j < s->prim_channels; j++) {
if (s->joint_intensity[j] > 0)
s->joint_huff[j] = get_bits(&s->gb, 3);
}
if (get_bits_left(&s->gb) < 0)
return AVERROR_INVALIDDATA;
for (j = base_channel; j < s->prim_channels; j++) {
int source_channel;
if (s->joint_intensity[j] > 0) {
int scale = 0;
source_channel = s->joint_intensity[j] - 1;
for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
scale = get_scale(&s->gb, s->joint_huff[j], 0);
scale += 64;
s->joint_scale_factor[j][k] = scale;
}
if (!(s->debug_flag & 0x02)) {
av_log(s->avctx, AV_LOG_DEBUG,
"Joint stereo coding not supported\n");
s->debug_flag |= 0x02;
}
}
}
if (!base_channel && s->prim_channels > 2) {
if (s->downmix) {
for (j = base_channel; j < s->prim_channels; j++) {
s->downmix_coef[j][0] = get_bits(&s->gb, 7);
s->downmix_coef[j][1] = get_bits(&s->gb, 7);
}
} else {
int am = s->amode & DCA_CHANNEL_MASK;
for (j = base_channel; j < s->prim_channels; j++) {
s->downmix_coef[j][0] = dca_default_coeffs[am][j][0];
s->downmix_coef[j][1] = dca_default_coeffs[am][j][1];
}
}
}
if (!base_channel && s->dynrange)
s->dynrange_coef = get_bits(&s->gb, 8);
if (s->crc_present) {
get_bits(&s->gb, 16);
}
for (j = base_channel; j < s->prim_channels; j++)
for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
if (!base_channel && s->lfe) {
int lfe_samples = 2 * s->lfe * (4 + block_index);
int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
float lfe_scale;
for (j = lfe_samples; j < lfe_end_sample; j++) {
s->lfe_data[j] = get_sbits(&s->gb, 8);
}
s->lfe_scale_factor = scale_factor_quant7[get_bits(&s->gb, 8)];
lfe_scale = 0.035 * s->lfe_scale_factor;
for (j = lfe_samples; j < lfe_end_sample; j++)
s->lfe_data[j] *= lfe_scale;
}
#ifdef TRACE
av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n",
s->subsubframes[s->current_subframe]);
av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n",
s->partial_samples[s->current_subframe]);
for (j = base_channel; j < s->prim_channels; j++) {
av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:");
for (k = 0; k < s->subband_activity[j]; k++)
av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]);
av_log(s->avctx, AV_LOG_DEBUG, "\n");
}
for (j = base_channel; j < s->prim_channels; j++) {
for (k = 0; k < s->subband_activity[j]; k++)
av_log(s->avctx, AV_LOG_DEBUG,
"prediction coefs: %f, %f, %f, %f\n",
(float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192,
(float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192,
(float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192,
(float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192);
}
for (j = base_channel; j < s->prim_channels; j++) {
av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: ");
for (k = 0; k < s->vq_start_subband[j]; k++)
av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]);
av_log(s->avctx, AV_LOG_DEBUG, "\n");
}
for (j = base_channel; j < s->prim_channels; j++) {
av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:");
for (k = 0; k < s->subband_activity[j]; k++)
av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]);
av_log(s->avctx, AV_LOG_DEBUG, "\n");
}
for (j = base_channel; j < s->prim_channels; j++) {
av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:");
for (k = 0; k < s->subband_activity[j]; k++) {
if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0)
av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]);
if (k < s->vq_start_subband[j] && s->transition_mode[j][k])
av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]);
}
av_log(s->avctx, AV_LOG_DEBUG, "\n");
}
for (j = base_channel; j < s->prim_channels; j++) {
if (s->joint_intensity[j] > 0) {
int source_channel = s->joint_intensity[j] - 1;
av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n");
for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)
av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);
av_log(s->avctx, AV_LOG_DEBUG, "\n");
}
}
if (!base_channel && s->prim_channels > 2 && s->downmix) {
av_log(s->avctx, AV_LOG_DEBUG, "Downmix coeffs:\n");
for (j = 0; j < s->prim_channels; j++) {
av_log(s->avctx, AV_LOG_DEBUG, "Channel 0, %d = %f\n", j,
dca_downmix_coeffs[s->downmix_coef[j][0]]);
av_log(s->avctx, AV_LOG_DEBUG, "Channel 1, %d = %f\n", j,
dca_downmix_coeffs[s->downmix_coef[j][1]]);
}
av_log(s->avctx, AV_LOG_DEBUG, "\n");
}
for (j = base_channel; j < s->prim_channels; j++)
for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]);
if (!base_channel && s->lfe) {
int lfe_samples = 2 * s->lfe * (4 + block_index);
int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
for (j = lfe_samples; j < lfe_end_sample; j++)
av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);
av_log(s->avctx, AV_LOG_DEBUG, "\n");
}
#endif
return 0;
}
|
['static int dca_decode_frame(AVCodecContext *avctx, void *data,\n int *got_frame_ptr, AVPacket *avpkt)\n{\n const uint8_t *buf = avpkt->data;\n int buf_size = avpkt->size;\n int lfe_samples;\n int num_core_channels = 0;\n int i, ret;\n float *samples_flt;\n int16_t *samples_s16;\n DCAContext *s = avctx->priv_data;\n int channels;\n int core_ss_end;\n s->xch_present = 0;\n s->dca_buffer_size = dca_convert_bitstream(buf, buf_size, s->dca_buffer,\n DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE);\n if (s->dca_buffer_size == AVERROR_INVALIDDATA) {\n av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\\n");\n return AVERROR_INVALIDDATA;\n }\n init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);\n if ((ret = dca_parse_frame_header(s)) < 0) {\n return ret;\n }\n avctx->sample_rate = s->sample_rate;\n avctx->bit_rate = s->bit_rate;\n avctx->frame_size = s->sample_blocks * 32;\n s->profile = FF_PROFILE_DTS;\n for (i = 0; i < (s->sample_blocks / 8); i++) {\n if ((ret = dca_decode_block(s, 0, i))) {\n av_log(avctx, AV_LOG_ERROR, "error decoding block\\n");\n return ret;\n }\n }\n num_core_channels = s->prim_channels;\n if (s->ext_coding)\n s->core_ext_mask = dca_ext_audio_descr_mask[s->ext_descr];\n else\n s->core_ext_mask = 0;\n core_ss_end = FFMIN(s->frame_size, s->dca_buffer_size) * 8;\n if (s->core_ext_mask < 0 || s->core_ext_mask & DCA_EXT_XCH) {\n s->core_ext_mask = FFMAX(s->core_ext_mask, 0);\n skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);\n while (core_ss_end - get_bits_count(&s->gb) >= 32) {\n uint32_t bits = get_bits_long(&s->gb, 32);\n switch (bits) {\n case 0x5a5a5a5a: {\n int ext_amode, xch_fsize;\n s->xch_base_channel = s->prim_channels;\n xch_fsize = show_bits(&s->gb, 10);\n if ((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) &&\n (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1))\n continue;\n skip_bits(&s->gb, 10);\n s->core_ext_mask |= DCA_EXT_XCH;\n if ((ext_amode = get_bits(&s->gb, 4)) != 1) {\n av_log(avctx, AV_LOG_ERROR, "XCh extension amode %d not"\n " supported!\\n", ext_amode);\n continue;\n }\n dca_parse_audio_coding_header(s, s->xch_base_channel);\n for (i = 0; i < (s->sample_blocks / 8); i++)\n if ((ret = dca_decode_block(s, s->xch_base_channel, i))) {\n av_log(avctx, AV_LOG_ERROR, "error decoding XCh extension\\n");\n continue;\n }\n s->xch_present = 1;\n break;\n }\n case 0x47004a03:\n s->core_ext_mask |= DCA_EXT_XXCH;\n break;\n case 0x1d95f262: {\n int fsize96 = show_bits(&s->gb, 12) + 1;\n if (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + fsize96)\n continue;\n av_log(avctx, AV_LOG_DEBUG, "X96 extension found at %d bits\\n",\n get_bits_count(&s->gb));\n skip_bits(&s->gb, 12);\n av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\\n", fsize96);\n av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\\n", get_bits(&s->gb, 4));\n s->core_ext_mask |= DCA_EXT_X96;\n break;\n }\n }\n skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);\n }\n } else {\n skip_bits_long(&s->gb, core_ss_end - get_bits_count(&s->gb));\n }\n if (s->core_ext_mask & DCA_EXT_X96)\n s->profile = FF_PROFILE_DTS_96_24;\n else if (s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH))\n s->profile = FF_PROFILE_DTS_ES;\n if (s->dca_buffer_size - s->frame_size > 32 &&\n get_bits_long(&s->gb, 32) == DCA_HD_MARKER)\n dca_exss_parse_header(s);\n avctx->profile = s->profile;\n channels = s->prim_channels + !!s->lfe;\n if (s->amode < 16) {\n avctx->channel_layout = dca_core_channel_layout[s->amode];\n if (s->xch_present && (!avctx->request_channels ||\n avctx->request_channels > num_core_channels + !!s->lfe)) {\n avctx->channel_layout |= AV_CH_BACK_CENTER;\n if (s->lfe) {\n avctx->channel_layout |= AV_CH_LOW_FREQUENCY;\n s->channel_order_tab = dca_channel_reorder_lfe_xch[s->amode];\n } else {\n s->channel_order_tab = dca_channel_reorder_nolfe_xch[s->amode];\n }\n } else {\n channels = num_core_channels + !!s->lfe;\n s->xch_present = 0;\n if (s->lfe) {\n avctx->channel_layout |= AV_CH_LOW_FREQUENCY;\n s->channel_order_tab = dca_channel_reorder_lfe[s->amode];\n } else\n s->channel_order_tab = dca_channel_reorder_nolfe[s->amode];\n }\n if (channels > !!s->lfe &&\n s->channel_order_tab[channels - 1 - !!s->lfe] < 0)\n return AVERROR_INVALIDDATA;\n if (avctx->request_channels == 2 && s->prim_channels > 2) {\n channels = 2;\n s->output = DCA_STEREO;\n avctx->channel_layout = AV_CH_LAYOUT_STEREO;\n }\n } else {\n av_log(avctx, AV_LOG_ERROR, "Non standard configuration %d !\\n", s->amode);\n return AVERROR_INVALIDDATA;\n }\n if (s->is_channels_set == 0) {\n s->is_channels_set = 1;\n avctx->channels = channels;\n }\n if (avctx->channels != channels) {\n av_log(avctx, AV_LOG_ERROR, "DCA decoder does not support number of "\n "channels changing in stream. Skipping frame.\\n");\n return AVERROR_PATCHWELCOME;\n }\n s->frame.nb_samples = 256 * (s->sample_blocks / 8);\n if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {\n av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n return ret;\n }\n samples_flt = (float *) s->frame.data[0];\n samples_s16 = (int16_t *) s->frame.data[0];\n for (i = 0; i < (s->sample_blocks / 8); i++) {\n dca_filter_channels(s, i);\n if ((s->source_pcm_res & 1) && s->xch_present) {\n float *back_chan = s->samples + s->channel_order_tab[s->xch_base_channel] * 256;\n float *lt_chan = s->samples + s->channel_order_tab[s->xch_base_channel - 2] * 256;\n float *rt_chan = s->samples + s->channel_order_tab[s->xch_base_channel - 1] * 256;\n s->dsp.vector_fmac_scalar(lt_chan, back_chan, -M_SQRT1_2, 256);\n s->dsp.vector_fmac_scalar(rt_chan, back_chan, -M_SQRT1_2, 256);\n }\n if (avctx->sample_fmt == AV_SAMPLE_FMT_FLT) {\n s->fmt_conv.float_interleave(samples_flt, s->samples_chanptr, 256,\n channels);\n samples_flt += 256 * channels;\n } else {\n s->fmt_conv.float_to_int16_interleave(samples_s16,\n s->samples_chanptr, 256,\n channels);\n samples_s16 += 256 * channels;\n }\n }\n lfe_samples = 2 * s->lfe * (s->sample_blocks / 8);\n for (i = 0; i < 2 * s->lfe * 4; i++)\n s->lfe_data[i] = s->lfe_data[i + lfe_samples];\n *got_frame_ptr = 1;\n *(AVFrame *) data = s->frame;\n return buf_size;\n}', 'static int dca_parse_frame_header(DCAContext *s)\n{\n init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);\n skip_bits_long(&s->gb, 32);\n s->frame_type = get_bits(&s->gb, 1);\n s->samples_deficit = get_bits(&s->gb, 5) + 1;\n s->crc_present = get_bits(&s->gb, 1);\n s->sample_blocks = get_bits(&s->gb, 7) + 1;\n s->frame_size = get_bits(&s->gb, 14) + 1;\n if (s->frame_size < 95)\n return AVERROR_INVALIDDATA;\n s->amode = get_bits(&s->gb, 6);\n s->sample_rate = dca_sample_rates[get_bits(&s->gb, 4)];\n if (!s->sample_rate)\n return AVERROR_INVALIDDATA;\n s->bit_rate_index = get_bits(&s->gb, 5);\n s->bit_rate = dca_bit_rates[s->bit_rate_index];\n if (!s->bit_rate)\n return AVERROR_INVALIDDATA;\n s->downmix = get_bits(&s->gb, 1);\n s->dynrange = get_bits(&s->gb, 1);\n s->timestamp = get_bits(&s->gb, 1);\n s->aux_data = get_bits(&s->gb, 1);\n s->hdcd = get_bits(&s->gb, 1);\n s->ext_descr = get_bits(&s->gb, 3);\n s->ext_coding = get_bits(&s->gb, 1);\n s->aspf = get_bits(&s->gb, 1);\n s->lfe = get_bits(&s->gb, 2);\n s->predictor_history = get_bits(&s->gb, 1);\n if (s->crc_present)\n s->header_crc = get_bits(&s->gb, 16);\n s->multirate_inter = get_bits(&s->gb, 1);\n s->version = get_bits(&s->gb, 4);\n s->copy_history = get_bits(&s->gb, 2);\n s->source_pcm_res = get_bits(&s->gb, 3);\n s->front_sum = get_bits(&s->gb, 1);\n s->surround_sum = get_bits(&s->gb, 1);\n s->dialog_norm = get_bits(&s->gb, 4);\n s->output = s->amode;\n if (s->lfe)\n s->output |= DCA_LFE;\n#ifdef TRACE\n av_log(s->avctx, AV_LOG_DEBUG, "frame type: %i\\n", s->frame_type);\n av_log(s->avctx, AV_LOG_DEBUG, "samples deficit: %i\\n", s->samples_deficit);\n av_log(s->avctx, AV_LOG_DEBUG, "crc present: %i\\n", s->crc_present);\n av_log(s->avctx, AV_LOG_DEBUG, "sample blocks: %i (%i samples)\\n",\n s->sample_blocks, s->sample_blocks * 32);\n av_log(s->avctx, AV_LOG_DEBUG, "frame size: %i bytes\\n", s->frame_size);\n av_log(s->avctx, AV_LOG_DEBUG, "amode: %i (%i channels)\\n",\n s->amode, dca_channels[s->amode]);\n av_log(s->avctx, AV_LOG_DEBUG, "sample rate: %i Hz\\n",\n s->sample_rate);\n av_log(s->avctx, AV_LOG_DEBUG, "bit rate: %i bits/s\\n",\n s->bit_rate);\n av_log(s->avctx, AV_LOG_DEBUG, "downmix: %i\\n", s->downmix);\n av_log(s->avctx, AV_LOG_DEBUG, "dynrange: %i\\n", s->dynrange);\n av_log(s->avctx, AV_LOG_DEBUG, "timestamp: %i\\n", s->timestamp);\n av_log(s->avctx, AV_LOG_DEBUG, "aux_data: %i\\n", s->aux_data);\n av_log(s->avctx, AV_LOG_DEBUG, "hdcd: %i\\n", s->hdcd);\n av_log(s->avctx, AV_LOG_DEBUG, "ext descr: %i\\n", s->ext_descr);\n av_log(s->avctx, AV_LOG_DEBUG, "ext coding: %i\\n", s->ext_coding);\n av_log(s->avctx, AV_LOG_DEBUG, "aspf: %i\\n", s->aspf);\n av_log(s->avctx, AV_LOG_DEBUG, "lfe: %i\\n", s->lfe);\n av_log(s->avctx, AV_LOG_DEBUG, "predictor history: %i\\n",\n s->predictor_history);\n av_log(s->avctx, AV_LOG_DEBUG, "header crc: %i\\n", s->header_crc);\n av_log(s->avctx, AV_LOG_DEBUG, "multirate inter: %i\\n",\n s->multirate_inter);\n av_log(s->avctx, AV_LOG_DEBUG, "version number: %i\\n", s->version);\n av_log(s->avctx, AV_LOG_DEBUG, "copy history: %i\\n", s->copy_history);\n av_log(s->avctx, AV_LOG_DEBUG,\n "source pcm resolution: %i (%i bits/sample)\\n",\n s->source_pcm_res, dca_bits_per_sample[s->source_pcm_res]);\n av_log(s->avctx, AV_LOG_DEBUG, "front sum: %i\\n", s->front_sum);\n av_log(s->avctx, AV_LOG_DEBUG, "surround sum: %i\\n", s->surround_sum);\n av_log(s->avctx, AV_LOG_DEBUG, "dialog norm: %i\\n", s->dialog_norm);\n av_log(s->avctx, AV_LOG_DEBUG, "\\n");\n#endif\n s->subframes = get_bits(&s->gb, 4) + 1;\n return dca_parse_audio_coding_header(s, 0);\n}', 'static int dca_decode_block(DCAContext *s, int base_channel, int block_index)\n{\n int ret;\n if (s->current_subframe >= s->subframes) {\n av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",\n s->current_subframe, s->subframes);\n return AVERROR_INVALIDDATA;\n }\n if (!s->current_subsubframe) {\n#ifdef TRACE\n av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\\n");\n#endif\n if ((ret = dca_subframe_header(s, base_channel, block_index)))\n return ret;\n }\n#ifdef TRACE\n av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\\n");\n#endif\n if ((ret = dca_subsubframe(s, base_channel, block_index)))\n return ret;\n s->current_subsubframe++;\n if (s->current_subsubframe >= s->subsubframes[s->current_subframe]) {\n s->current_subsubframe = 0;\n s->current_subframe++;\n }\n if (s->current_subframe >= s->subframes) {\n#ifdef TRACE\n av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\\n");\n#endif\n if ((ret = dca_subframe_footer(s, base_channel)))\n return ret;\n }\n return 0;\n}', 'static int dca_subframe_header(DCAContext *s, int base_channel, int block_index)\n{\n int j, k;\n if (get_bits_left(&s->gb) < 0)\n return AVERROR_INVALIDDATA;\n if (!base_channel) {\n s->subsubframes[s->current_subframe] = get_bits(&s->gb, 2) + 1;\n s->partial_samples[s->current_subframe] = get_bits(&s->gb, 3);\n }\n for (j = base_channel; j < s->prim_channels; j++) {\n for (k = 0; k < s->subband_activity[j]; k++)\n s->prediction_mode[j][k] = get_bits(&s->gb, 1);\n }\n for (j = base_channel; j < s->prim_channels; j++) {\n for (k = 0; k < s->subband_activity[j]; k++) {\n if (s->prediction_mode[j][k] > 0) {\n s->prediction_vq[j][k] = get_bits(&s->gb, 12);\n }\n }\n }\n for (j = base_channel; j < s->prim_channels; j++) {\n for (k = 0; k < s->vq_start_subband[j]; k++) {\n if (s->bitalloc_huffman[j] == 6)\n s->bitalloc[j][k] = get_bits(&s->gb, 5);\n else if (s->bitalloc_huffman[j] == 5)\n s->bitalloc[j][k] = get_bits(&s->gb, 4);\n else if (s->bitalloc_huffman[j] == 7) {\n av_log(s->avctx, AV_LOG_ERROR,\n "Invalid bit allocation index\\n");\n return AVERROR_INVALIDDATA;\n } else {\n s->bitalloc[j][k] =\n get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);\n }\n if (s->bitalloc[j][k] > 26) {\n return AVERROR_INVALIDDATA;\n }\n }\n }\n for (j = base_channel; j < s->prim_channels; j++) {\n for (k = 0; k < s->subband_activity[j]; k++) {\n s->transition_mode[j][k] = 0;\n if (s->subsubframes[s->current_subframe] > 1 &&\n k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {\n s->transition_mode[j][k] =\n get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);\n }\n }\n }\n if (get_bits_left(&s->gb) < 0)\n return AVERROR_INVALIDDATA;\n for (j = base_channel; j < s->prim_channels; j++) {\n const uint32_t *scale_table;\n int scale_sum;\n memset(s->scale_factor[j], 0,\n s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);\n if (s->scalefactor_huffman[j] == 6)\n scale_table = scale_factor_quant7;\n else\n scale_table = scale_factor_quant6;\n scale_sum = 0;\n for (k = 0; k < s->subband_activity[j]; k++) {\n if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {\n scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum);\n s->scale_factor[j][k][0] = scale_table[scale_sum];\n }\n if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {\n scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum);\n s->scale_factor[j][k][1] = scale_table[scale_sum];\n }\n }\n }\n for (j = base_channel; j < s->prim_channels; j++) {\n if (s->joint_intensity[j] > 0)\n s->joint_huff[j] = get_bits(&s->gb, 3);\n }\n if (get_bits_left(&s->gb) < 0)\n return AVERROR_INVALIDDATA;\n for (j = base_channel; j < s->prim_channels; j++) {\n int source_channel;\n if (s->joint_intensity[j] > 0) {\n int scale = 0;\n source_channel = s->joint_intensity[j] - 1;\n for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {\n scale = get_scale(&s->gb, s->joint_huff[j], 0);\n scale += 64;\n s->joint_scale_factor[j][k] = scale;\n }\n if (!(s->debug_flag & 0x02)) {\n av_log(s->avctx, AV_LOG_DEBUG,\n "Joint stereo coding not supported\\n");\n s->debug_flag |= 0x02;\n }\n }\n }\n if (!base_channel && s->prim_channels > 2) {\n if (s->downmix) {\n for (j = base_channel; j < s->prim_channels; j++) {\n s->downmix_coef[j][0] = get_bits(&s->gb, 7);\n s->downmix_coef[j][1] = get_bits(&s->gb, 7);\n }\n } else {\n int am = s->amode & DCA_CHANNEL_MASK;\n for (j = base_channel; j < s->prim_channels; j++) {\n s->downmix_coef[j][0] = dca_default_coeffs[am][j][0];\n s->downmix_coef[j][1] = dca_default_coeffs[am][j][1];\n }\n }\n }\n if (!base_channel && s->dynrange)\n s->dynrange_coef = get_bits(&s->gb, 8);\n if (s->crc_present) {\n get_bits(&s->gb, 16);\n }\n for (j = base_channel; j < s->prim_channels; j++)\n for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)\n s->high_freq_vq[j][k] = get_bits(&s->gb, 10);\n if (!base_channel && s->lfe) {\n int lfe_samples = 2 * s->lfe * (4 + block_index);\n int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);\n float lfe_scale;\n for (j = lfe_samples; j < lfe_end_sample; j++) {\n s->lfe_data[j] = get_sbits(&s->gb, 8);\n }\n s->lfe_scale_factor = scale_factor_quant7[get_bits(&s->gb, 8)];\n lfe_scale = 0.035 * s->lfe_scale_factor;\n for (j = lfe_samples; j < lfe_end_sample; j++)\n s->lfe_data[j] *= lfe_scale;\n }\n#ifdef TRACE\n av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\\n",\n s->subsubframes[s->current_subframe]);\n av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\\n",\n s->partial_samples[s->current_subframe]);\n for (j = base_channel; j < s->prim_channels; j++) {\n av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:");\n for (k = 0; k < s->subband_activity[j]; k++)\n av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]);\n av_log(s->avctx, AV_LOG_DEBUG, "\\n");\n }\n for (j = base_channel; j < s->prim_channels; j++) {\n for (k = 0; k < s->subband_activity[j]; k++)\n av_log(s->avctx, AV_LOG_DEBUG,\n "prediction coefs: %f, %f, %f, %f\\n",\n (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192,\n (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192,\n (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192,\n (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192);\n }\n for (j = base_channel; j < s->prim_channels; j++) {\n av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: ");\n for (k = 0; k < s->vq_start_subband[j]; k++)\n av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]);\n av_log(s->avctx, AV_LOG_DEBUG, "\\n");\n }\n for (j = base_channel; j < s->prim_channels; j++) {\n av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:");\n for (k = 0; k < s->subband_activity[j]; k++)\n av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]);\n av_log(s->avctx, AV_LOG_DEBUG, "\\n");\n }\n for (j = base_channel; j < s->prim_channels; j++) {\n av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:");\n for (k = 0; k < s->subband_activity[j]; k++) {\n if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0)\n av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]);\n if (k < s->vq_start_subband[j] && s->transition_mode[j][k])\n av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]);\n }\n av_log(s->avctx, AV_LOG_DEBUG, "\\n");\n }\n for (j = base_channel; j < s->prim_channels; j++) {\n if (s->joint_intensity[j] > 0) {\n int source_channel = s->joint_intensity[j] - 1;\n av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\\n");\n for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)\n av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);\n av_log(s->avctx, AV_LOG_DEBUG, "\\n");\n }\n }\n if (!base_channel && s->prim_channels > 2 && s->downmix) {\n av_log(s->avctx, AV_LOG_DEBUG, "Downmix coeffs:\\n");\n for (j = 0; j < s->prim_channels; j++) {\n av_log(s->avctx, AV_LOG_DEBUG, "Channel 0, %d = %f\\n", j,\n dca_downmix_coeffs[s->downmix_coef[j][0]]);\n av_log(s->avctx, AV_LOG_DEBUG, "Channel 1, %d = %f\\n", j,\n dca_downmix_coeffs[s->downmix_coef[j][1]]);\n }\n av_log(s->avctx, AV_LOG_DEBUG, "\\n");\n }\n for (j = base_channel; j < s->prim_channels; j++)\n for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)\n av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\\n", s->high_freq_vq[j][k]);\n if (!base_channel && s->lfe) {\n int lfe_samples = 2 * s->lfe * (4 + block_index);\n int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);\n av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\\n");\n for (j = lfe_samples; j < lfe_end_sample; j++)\n av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);\n av_log(s->avctx, AV_LOG_DEBUG, "\\n");\n }\n#endif\n return 0;\n}']
|
2,614
| 0
|
https://github.com/openssl/openssl/blob/3ad4af89cf7380aa94d1995e05e713d59e1c469a/crypto/ts/ts_rsp_verify.c/#L243
|
static int ts_verify_cert(X509_STORE *store, STACK_OF(X509) *untrusted,
X509 *signer, STACK_OF(X509) **chain)
{
X509_STORE_CTX *cert_ctx = NULL;
int i;
int ret = 0;
*chain = NULL;
cert_ctx = X509_STORE_CTX_new();
if (cert_ctx == NULL) {
TSerr(TS_F_TS_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
goto err;
}
if (!X509_STORE_CTX_init(cert_ctx, store, signer, untrusted))
goto end;
X509_STORE_CTX_set_purpose(cert_ctx, X509_PURPOSE_TIMESTAMP_SIGN);
i = X509_verify_cert(cert_ctx);
if (i <= 0) {
int j = X509_STORE_CTX_get_error(cert_ctx);
TSerr(TS_F_TS_VERIFY_CERT, TS_R_CERTIFICATE_VERIFY_ERROR);
ERR_add_error_data(2, "Verify error:",
X509_verify_cert_error_string(j));
goto err;
}
*chain = X509_STORE_CTX_get1_chain(cert_ctx);
ret = 1;
goto end;
err:
ret = 0;
end:
X509_STORE_CTX_free(cert_ctx);
return ret;
}
|
['static int ts_verify_cert(X509_STORE *store, STACK_OF(X509) *untrusted,\n X509 *signer, STACK_OF(X509) **chain)\n{\n X509_STORE_CTX *cert_ctx = NULL;\n int i;\n int ret = 0;\n *chain = NULL;\n cert_ctx = X509_STORE_CTX_new();\n if (cert_ctx == NULL) {\n TSerr(TS_F_TS_VERIFY_CERT, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (!X509_STORE_CTX_init(cert_ctx, store, signer, untrusted))\n goto end;\n X509_STORE_CTX_set_purpose(cert_ctx, X509_PURPOSE_TIMESTAMP_SIGN);\n i = X509_verify_cert(cert_ctx);\n if (i <= 0) {\n int j = X509_STORE_CTX_get_error(cert_ctx);\n TSerr(TS_F_TS_VERIFY_CERT, TS_R_CERTIFICATE_VERIFY_ERROR);\n ERR_add_error_data(2, "Verify error:",\n X509_verify_cert_error_string(j));\n goto err;\n }\n *chain = X509_STORE_CTX_get1_chain(cert_ctx);\n ret = 1;\n goto end;\nerr:\n ret = 0;\nend:\n X509_STORE_CTX_free(cert_ctx);\n return ret;\n}', 'X509_STORE_CTX *X509_STORE_CTX_new(void)\n{\n X509_STORE_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));\n if (ctx == NULL) {\n X509err(X509_F_X509_STORE_CTX_NEW, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n return ctx;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n 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}', 'void X509_STORE_CTX_free(X509_STORE_CTX *ctx)\n{\n if (ctx == NULL)\n return;\n X509_STORE_CTX_cleanup(ctx);\n OPENSSL_free(ctx);\n}', 'void CRYPTO_free(void *str, const char *file, int line)\n{\n if (free_impl != NULL && free_impl != &CRYPTO_free) {\n free_impl(str, file, line);\n return;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_free(str, 0, file, line);\n free(str);\n CRYPTO_mem_debug_free(str, 1, file, line);\n } else {\n free(str);\n }\n#else\n free(str);\n#endif\n}']
|
2,615
| 0
|
https://github.com/openssl/openssl/blob/a68d8c7b77a3d46d591b89cfd0ecd2a2242e4613/crypto/cms/cms_pwri.c/#L222
|
static int kek_unwrap_key(unsigned char *out, size_t *outlen,
const unsigned char *in, size_t inlen,
EVP_CIPHER_CTX *ctx)
{
size_t blocklen = EVP_CIPHER_CTX_block_size(ctx);
unsigned char *tmp;
int outl, rv = 0;
if (inlen < 2 * blocklen) {
return 0;
}
if (inlen % blocklen) {
return 0;
}
tmp = OPENSSL_malloc(inlen);
if (tmp == NULL)
return 0;
if (!EVP_DecryptUpdate(ctx, tmp + inlen - 2 * blocklen, &outl,
in + inlen - 2 * blocklen, blocklen * 2)
|| !EVP_DecryptUpdate(ctx, tmp, &outl,
tmp + inlen - blocklen, blocklen)
|| !EVP_DecryptUpdate(ctx, tmp, &outl, in, inlen - blocklen)
|| !EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, NULL)
|| !EVP_DecryptUpdate(ctx, tmp, &outl, tmp, inlen))
goto err;
if (((tmp[1] ^ tmp[4]) & (tmp[2] ^ tmp[5]) & (tmp[3] ^ tmp[6])) != 0xff) {
goto err;
}
if (inlen < (size_t)(tmp[0] - 4)) {
goto err;
}
*outlen = (size_t)tmp[0];
memcpy(out, tmp + 4, *outlen);
rv = 1;
err:
OPENSSL_clear_free(tmp, inlen);
return rv;
}
|
['BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms)\n{\n CMS_EncryptedContentInfo *ec;\n STACK_OF(CMS_RecipientInfo) *rinfos;\n CMS_RecipientInfo *ri;\n int i, ok = 0;\n BIO *ret;\n ec = cms->d.envelopedData->encryptedContentInfo;\n ret = cms_EncryptedContent_init_bio(ec);\n if (!ret || !ec->cipher)\n return ret;\n rinfos = cms->d.envelopedData->recipientInfos;\n for (i = 0; i < sk_CMS_RecipientInfo_num(rinfos); i++) {\n ri = sk_CMS_RecipientInfo_value(rinfos, i);\n if (CMS_RecipientInfo_encrypt(cms, ri) <= 0) {\n CMSerr(CMS_F_CMS_ENVELOPEDDATA_INIT_BIO,\n CMS_R_ERROR_SETTING_RECIPIENTINFO);\n goto err;\n }\n }\n cms_env_set_version(cms->d.envelopedData);\n ok = 1;\n err:\n ec->cipher = NULL;\n OPENSSL_clear_free(ec->key, ec->keylen);\n ec->key = NULL;\n ec->keylen = 0;\n if (ok)\n return ret;\n BIO_free(ret);\n return NULL;\n}', 'int CMS_RecipientInfo_encrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)\n{\n switch (ri->type) {\n case CMS_RECIPINFO_TRANS:\n return cms_RecipientInfo_ktri_encrypt(cms, ri);\n case CMS_RECIPINFO_AGREE:\n return cms_RecipientInfo_kari_encrypt(cms, ri);\n case CMS_RECIPINFO_KEK:\n return cms_RecipientInfo_kekri_encrypt(cms, ri);\n case CMS_RECIPINFO_PASS:\n return cms_RecipientInfo_pwri_crypt(cms, ri, 1);\n default:\n CMSerr(CMS_F_CMS_RECIPIENTINFO_ENCRYPT,\n CMS_R_UNSUPPORTED_RECIPIENT_TYPE);\n return 0;\n }\n}', 'int cms_RecipientInfo_pwri_crypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri,\n int en_de)\n{\n CMS_EncryptedContentInfo *ec;\n CMS_PasswordRecipientInfo *pwri;\n int r = 0;\n X509_ALGOR *algtmp, *kekalg = NULL;\n EVP_CIPHER_CTX *kekctx = NULL;\n const EVP_CIPHER *kekcipher;\n unsigned char *key = NULL;\n size_t keylen;\n ec = cms->d.envelopedData->encryptedContentInfo;\n pwri = ri->d.pwri;\n if (!pwri->pass) {\n CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, CMS_R_NO_PASSWORD);\n return 0;\n }\n algtmp = pwri->keyEncryptionAlgorithm;\n if (!algtmp || OBJ_obj2nid(algtmp->algorithm) != NID_id_alg_PWRI_KEK) {\n CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,\n CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM);\n return 0;\n }\n kekalg = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(X509_ALGOR),\n algtmp->parameter);\n if (kekalg == NULL) {\n CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,\n CMS_R_INVALID_KEY_ENCRYPTION_PARAMETER);\n return 0;\n }\n kekcipher = EVP_get_cipherbyobj(kekalg->algorithm);\n if (!kekcipher) {\n CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, CMS_R_UNKNOWN_CIPHER);\n return 0;\n }\n kekctx = EVP_CIPHER_CTX_new();\n if (kekctx == NULL) {\n CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n if (!EVP_CipherInit_ex(kekctx, kekcipher, NULL, NULL, NULL, en_de))\n goto err;\n EVP_CIPHER_CTX_set_padding(kekctx, 0);\n if (EVP_CIPHER_asn1_to_param(kekctx, kekalg->parameter) < 0) {\n CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,\n CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);\n goto err;\n }\n algtmp = pwri->keyDerivationAlgorithm;\n if (EVP_PBE_CipherInit(algtmp->algorithm,\n (char *)pwri->pass, pwri->passlen,\n algtmp->parameter, kekctx, en_de) < 0) {\n CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, ERR_R_EVP_LIB);\n goto err;\n }\n if (en_de) {\n if (!kek_wrap_key(NULL, &keylen, ec->key, ec->keylen, kekctx))\n goto err;\n key = OPENSSL_malloc(keylen);\n if (key == NULL)\n goto err;\n if (!kek_wrap_key(key, &keylen, ec->key, ec->keylen, kekctx))\n goto err;\n pwri->encryptedKey->data = key;\n pwri->encryptedKey->length = keylen;\n } else {\n key = OPENSSL_malloc(pwri->encryptedKey->length);\n if (key == NULL) {\n CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (!kek_unwrap_key(key, &keylen,\n pwri->encryptedKey->data,\n pwri->encryptedKey->length, kekctx)) {\n CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, CMS_R_UNWRAP_FAILURE);\n goto err;\n }\n ec->key = key;\n ec->keylen = keylen;\n }\n r = 1;\n err:\n EVP_CIPHER_CTX_free(kekctx);\n if (!r)\n OPENSSL_free(key);\n X509_ALGOR_free(kekalg);\n return r;\n}', 'static int kek_unwrap_key(unsigned char *out, size_t *outlen,\n const unsigned char *in, size_t inlen,\n EVP_CIPHER_CTX *ctx)\n{\n size_t blocklen = EVP_CIPHER_CTX_block_size(ctx);\n unsigned char *tmp;\n int outl, rv = 0;\n if (inlen < 2 * blocklen) {\n return 0;\n }\n if (inlen % blocklen) {\n return 0;\n }\n tmp = OPENSSL_malloc(inlen);\n if (tmp == NULL)\n return 0;\n if (!EVP_DecryptUpdate(ctx, tmp + inlen - 2 * blocklen, &outl,\n in + inlen - 2 * blocklen, blocklen * 2)\n || !EVP_DecryptUpdate(ctx, tmp, &outl,\n tmp + inlen - blocklen, blocklen)\n || !EVP_DecryptUpdate(ctx, tmp, &outl, in, inlen - blocklen)\n || !EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, NULL)\n || !EVP_DecryptUpdate(ctx, tmp, &outl, tmp, inlen))\n goto err;\n if (((tmp[1] ^ tmp[4]) & (tmp[2] ^ tmp[5]) & (tmp[3] ^ tmp[6])) != 0xff) {\n goto err;\n }\n if (inlen < (size_t)(tmp[0] - 4)) {\n goto err;\n }\n *outlen = (size_t)tmp[0];\n memcpy(out, tmp + 4, *outlen);\n rv = 1;\n err:\n OPENSSL_clear_free(tmp, inlen);\n return rv;\n}', 'int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,\n const unsigned char *in, int inl)\n{\n int fix_len;\n unsigned int b;\n if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {\n if (is_partially_overlapping(out, in, inl)) {\n EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);\n return 0;\n }\n fix_len = ctx->cipher->do_cipher(ctx, out, in, inl);\n if (fix_len < 0) {\n *outl = 0;\n return 0;\n } else\n *outl = fix_len;\n return 1;\n }\n if (inl <= 0) {\n *outl = 0;\n return inl == 0;\n }\n if (ctx->flags & EVP_CIPH_NO_PADDING)\n return EVP_EncryptUpdate(ctx, out, outl, in, inl);\n b = ctx->cipher->block_size;\n OPENSSL_assert(b <= sizeof ctx->final);\n if (ctx->final_used) {\n if (((PTRDIFF_T)out == (PTRDIFF_T)in)\n || is_partially_overlapping(out, in, b)) {\n EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);\n return 0;\n }\n memcpy(out, ctx->final, b);\n out += b;\n fix_len = 1;\n } else\n fix_len = 0;\n if (!EVP_EncryptUpdate(ctx, out, outl, in, inl))\n return 0;\n if (b > 1 && !ctx->buf_len) {\n *outl -= b;\n ctx->final_used = 1;\n memcpy(ctx->final, &out[*outl], b);\n } else\n ctx->final_used = 0;\n if (fix_len)\n *outl += b;\n return 1;\n}', '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}']
|
2,616
| 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)];
}
|
['int BN_GF2m_mod_solve_quad_arr(BIGNUM *r, const BIGNUM *a_, const int p[],\n BN_CTX *ctx)\n{\n int ret = 0, count = 0, j;\n BIGNUM *a, *z, *rho, *w, *w2, *tmp;\n bn_check_top(a_);\n if (!p[0]) {\n BN_zero(r);\n return 1;\n }\n BN_CTX_start(ctx);\n a = BN_CTX_get(ctx);\n z = BN_CTX_get(ctx);\n w = BN_CTX_get(ctx);\n if (w == NULL)\n goto err;\n if (!BN_GF2m_mod_arr(a, a_, p))\n goto err;\n if (BN_is_zero(a)) {\n BN_zero(r);\n ret = 1;\n goto err;\n }\n if (p[0] & 0x1) {\n if (!BN_copy(z, a))\n goto err;\n for (j = 1; j <= (p[0] - 1) / 2; j++) {\n if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))\n goto err;\n if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))\n goto err;\n if (!BN_GF2m_add(z, z, a))\n goto err;\n }\n } else {\n rho = BN_CTX_get(ctx);\n w2 = BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (tmp == NULL)\n goto err;\n do {\n if (!BN_priv_rand(rho, p[0], BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))\n goto err;\n if (!BN_GF2m_mod_arr(rho, rho, p))\n goto err;\n BN_zero(z);\n if (!BN_copy(w, rho))\n goto err;\n for (j = 1; j <= p[0] - 1; j++) {\n if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))\n goto err;\n if (!BN_GF2m_mod_sqr_arr(w2, w, p, ctx))\n goto err;\n if (!BN_GF2m_mod_mul_arr(tmp, w2, a, p, ctx))\n goto err;\n if (!BN_GF2m_add(z, z, tmp))\n goto err;\n if (!BN_GF2m_add(w, w2, rho))\n goto err;\n }\n count++;\n } while (BN_is_zero(w) && (count < MAX_ITERATIONS));\n if (BN_is_zero(w)) {\n BNerr(BN_F_BN_GF2M_MOD_SOLVE_QUAD_ARR, BN_R_TOO_MANY_ITERATIONS);\n goto err;\n }\n }\n if (!BN_GF2m_mod_sqr_arr(w, z, p, ctx))\n goto err;\n if (!BN_GF2m_add(w, z, w))\n goto err;\n if (BN_GF2m_cmp(w, a)) {\n BNerr(BN_F_BN_GF2M_MOD_SOLVE_QUAD_ARR, BN_R_NO_SOLUTION);\n goto err;\n }\n if (!BN_copy(r, z))\n goto err;\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_start()", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG("LEAVE BN_CTX_start()", ctx);\n}', 'int BN_GF2m_mod_sqr_arr(BIGNUM *r, const BIGNUM *a, const int p[],\n BN_CTX *ctx)\n{\n int i, ret = 0;\n BIGNUM *s;\n bn_check_top(a);\n BN_CTX_start(ctx);\n if ((s = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (!bn_wexpand(s, 2 * a->top))\n goto err;\n for (i = a->top - 1; i >= 0; i--) {\n s->d[2 * i + 1] = SQR1(a->d[i]);\n s->d[2 * i] = SQR0(a->d[i]);\n }\n s->top = 2 * a->top;\n bn_correct_top(s);\n if (!BN_GF2m_mod_arr(r, s, p))\n goto err;\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_CTX_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}']
|
2,617
| 0
|
https://github.com/libav/libav/blob/b19d493f2b9b59e50547c6ccb0d4d68c29267923/libswscale/swscale.c/#L3317
|
SwsVector *sws_getConstVec(double c, int length){
int i;
double *coeff= av_malloc(length*sizeof(double));
SwsVector *vec= av_malloc(sizeof(SwsVector));
vec->coeff= coeff;
vec->length= length;
for (i=0; i<length; i++)
coeff[i]= c;
return vec;
}
|
['SwsVector *sws_getConstVec(double c, int length){\n int i;\n double *coeff= av_malloc(length*sizeof(double));\n SwsVector *vec= av_malloc(sizeof(SwsVector));\n vec->coeff= coeff;\n vec->length= length;\n for (i=0; i<length; i++)\n coeff[i]= c;\n return vec;\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}']
|
2,618
| 0
|
https://github.com/libav/libav/blob/3dde147ff92764b907db49b5237df7fd26359444/libavformat/rtmpproto.c/#L413
|
static void rtmp_calc_digest(const uint8_t *src, int len, int gap,
const uint8_t *key, int keylen, uint8_t *dst)
{
struct AVSHA *sha;
uint8_t hmac_buf[64+32] = {0};
int i;
sha = av_mallocz(av_sha_size);
if (keylen < 64) {
memcpy(hmac_buf, key, keylen);
} else {
av_sha_init(sha, 256);
av_sha_update(sha,key, keylen);
av_sha_final(sha, hmac_buf);
}
for (i = 0; i < 64; i++)
hmac_buf[i] ^= HMAC_IPAD_VAL;
av_sha_init(sha, 256);
av_sha_update(sha, hmac_buf, 64);
if (gap <= 0) {
av_sha_update(sha, src, len);
} else {
av_sha_update(sha, src, gap);
av_sha_update(sha, src + gap + 32, len - gap - 32);
}
av_sha_final(sha, hmac_buf + 64);
for (i = 0; i < 64; i++)
hmac_buf[i] ^= HMAC_IPAD_VAL ^ HMAC_OPAD_VAL;
av_sha_init(sha, 256);
av_sha_update(sha, hmac_buf, 64+32);
av_sha_final(sha, dst);
av_free(sha);
}
|
['static void rtmp_calc_digest(const uint8_t *src, int len, int gap,\n const uint8_t *key, int keylen, uint8_t *dst)\n{\n struct AVSHA *sha;\n uint8_t hmac_buf[64+32] = {0};\n int i;\n sha = av_mallocz(av_sha_size);\n if (keylen < 64) {\n memcpy(hmac_buf, key, keylen);\n } else {\n av_sha_init(sha, 256);\n av_sha_update(sha,key, keylen);\n av_sha_final(sha, hmac_buf);\n }\n for (i = 0; i < 64; i++)\n hmac_buf[i] ^= HMAC_IPAD_VAL;\n av_sha_init(sha, 256);\n av_sha_update(sha, hmac_buf, 64);\n if (gap <= 0) {\n av_sha_update(sha, src, len);\n } else {\n av_sha_update(sha, src, gap);\n av_sha_update(sha, src + gap + 32, len - gap - 32);\n }\n av_sha_final(sha, hmac_buf + 64);\n for (i = 0; i < 64; i++)\n hmac_buf[i] ^= HMAC_IPAD_VAL ^ HMAC_OPAD_VAL;\n av_sha_init(sha, 256);\n av_sha_update(sha, hmac_buf, 64+32);\n av_sha_final(sha, dst);\n av_free(sha);\n}', 'void *av_mallocz(size_t size)\n{\n void *ptr = av_malloc(size);\n if (ptr)\n memset(ptr, 0, size);\n return ptr;\n}', 'void *av_malloc(size_t size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-32) )\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size+32);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&31) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr,32,size))\n ptr = NULL;\n#elif HAVE_MEMALIGN\n ptr = memalign(32,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}', 'int av_sha_init(AVSHA* ctx, int bits)\n{\n ctx->digest_len = bits >> 5;\n switch (bits) {\n case 160:\n ctx->state[0] = 0x67452301;\n ctx->state[1] = 0xEFCDAB89;\n ctx->state[2] = 0x98BADCFE;\n ctx->state[3] = 0x10325476;\n ctx->state[4] = 0xC3D2E1F0;\n ctx->transform = sha1_transform;\n break;\n case 224:\n ctx->state[0] = 0xC1059ED8;\n ctx->state[1] = 0x367CD507;\n ctx->state[2] = 0x3070DD17;\n ctx->state[3] = 0xF70E5939;\n ctx->state[4] = 0xFFC00B31;\n ctx->state[5] = 0x68581511;\n ctx->state[6] = 0x64F98FA7;\n ctx->state[7] = 0xBEFA4FA4;\n ctx->transform = sha256_transform;\n break;\n case 256:\n ctx->state[0] = 0x6A09E667;\n ctx->state[1] = 0xBB67AE85;\n ctx->state[2] = 0x3C6EF372;\n ctx->state[3] = 0xA54FF53A;\n ctx->state[4] = 0x510E527F;\n ctx->state[5] = 0x9B05688C;\n ctx->state[6] = 0x1F83D9AB;\n ctx->state[7] = 0x5BE0CD19;\n ctx->transform = sha256_transform;\n break;\n default:\n return -1;\n }\n ctx->count = 0;\n return 0;\n}']
|
2,619
| 0
|
https://gitlab.com/libtiff/libtiff/blob/69ce2652ef2feae25a4569eb57b837dde0a1bd71/libtiff/tif_tile.c/#L128
|
uint32
TIFFNumberOfTiles(TIFF* tif)
{
TIFFDirectory *td = &tif->tif_dir;
uint32 dx = td->td_tilewidth;
uint32 dy = td->td_tilelength;
uint32 dz = td->td_tiledepth;
uint32 ntiles;
if (dx == (uint32) -1)
dx = td->td_imagewidth;
if (dy == (uint32) -1)
dy = td->td_imagelength;
if (dz == (uint32) -1)
dz = td->td_imagedepth;
ntiles = (dx == 0 || dy == 0 || dz == 0) ? 0 :
_TIFFMultiply32(tif, _TIFFMultiply32(tif, TIFFhowmany_32(td->td_imagewidth, dx),
TIFFhowmany_32(td->td_imagelength, dy),
"TIFFNumberOfTiles"),
TIFFhowmany_32(td->td_imagedepth, dz), "TIFFNumberOfTiles");
if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
ntiles = _TIFFMultiply32(tif, ntiles, td->td_samplesperpixel,
"TIFFNumberOfTiles");
return (ntiles);
}
|
['static void TIFFWriteOvrRow( TIFFOvrCache * psCache )\n{\n int\t\tnRet, iTileX, iTileY = psCache->nBlockOffset;\n unsigned char *pabyData;\n toff_t\tnBaseDirOffset;\n uint32 RowsInStrip;\n if( TIFFIsByteSwapped(psCache->hTIFF) )\n {\n if( psCache->nBitsPerPixel == 16 )\n TIFFSwabArrayOfShort( (uint16 *) psCache->pabyRow1Blocks,\n (psCache->nBytesPerBlock * psCache->nSamples) / 2 );\n else if( psCache->nBitsPerPixel == 32 )\n TIFFSwabArrayOfLong( (uint32 *) psCache->pabyRow1Blocks,\n (psCache->nBytesPerBlock * psCache->nSamples) / 4 );\n else if( psCache->nBitsPerPixel == 64 )\n TIFFSwabArrayOfDouble( (double *) psCache->pabyRow1Blocks,\n (psCache->nBytesPerBlock * psCache->nSamples) / 8 );\n }\n nBaseDirOffset = TIFFCurrentDirOffset( psCache->hTIFF );\n nRet = TIFFSetSubDirectory( psCache->hTIFF, psCache->nDirOffset );\n assert( nRet == 1 );\n\tfor( iTileX = 0; iTileX < psCache->nBlocksPerRow; iTileX++ )\n\t{\n\t\tint nTileID;\n\t\tif (psCache->nPlanarConfig == PLANARCONFIG_SEPARATE)\n\t\t{\n\t\t\tint iSample;\n\t\t\tfor( iSample = 0; iSample < psCache->nSamples; iSample++ )\n\t\t\t{\n\t\t\t\tpabyData = TIFFGetOvrBlock( psCache, iTileX, iTileY, iSample );\n\t\t\t\tif( psCache->bTiled )\n\t\t\t\t{\n\t\t\t\t\tnTileID = TIFFComputeTile( psCache->hTIFF,\n\t\t\t\t\t iTileX * psCache->nBlockXSize,\n\t\t\t\t\t iTileY * psCache->nBlockYSize,\n\t\t\t\t\t 0, (tsample_t) iSample );\n\t\t\t\t\tTIFFWriteEncodedTile( psCache->hTIFF, nTileID,\n\t\t\t\t\t pabyData,\n\t\t\t\t\t TIFFTileSize(psCache->hTIFF) );\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tnTileID = TIFFComputeStrip( psCache->hTIFF,\n\t\t\t\t\t iTileY * psCache->nBlockYSize,\n\t\t\t\t\t (tsample_t) iSample );\n\t\t\t\t\tRowsInStrip=psCache->nBlockYSize;\n\t\t\t\t\tif ((iTileY+1)*psCache->nBlockYSize>psCache->nYSize)\n\t\t\t\t\t\tRowsInStrip=psCache->nYSize-iTileY*psCache->nBlockYSize;\n\t\t\t\t\tTIFFWriteEncodedStrip( psCache->hTIFF, nTileID,\n\t\t\t\t\t pabyData,\n\t\t\t\t\t TIFFVStripSize(psCache->hTIFF,RowsInStrip) );\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\telse\n\t\t{\n\t\t\tpabyData = TIFFGetOvrBlock( psCache, iTileX, iTileY, 0 );\n\t\t\tif( psCache->bTiled )\n\t\t\t{\n\t\t\t\tnTileID = TIFFComputeTile( psCache->hTIFF,\n\t\t\t\t iTileX * psCache->nBlockXSize,\n\t\t\t\t iTileY * psCache->nBlockYSize,\n\t\t\t\t 0, 0 );\n\t\t\t\tTIFFWriteEncodedTile( psCache->hTIFF, nTileID,\n\t\t\t\t pabyData,\n\t\t\t\t TIFFTileSize(psCache->hTIFF) );\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tnTileID = TIFFComputeStrip( psCache->hTIFF,\n\t\t\t\t iTileY * psCache->nBlockYSize,\n\t\t\t\t 0 );\n\t\t\t\tRowsInStrip=psCache->nBlockYSize;\n\t\t\t\tif ((iTileY+1)*psCache->nBlockYSize>psCache->nYSize)\n\t\t\t\t\tRowsInStrip=psCache->nYSize-iTileY*psCache->nBlockYSize;\n\t\t\t\tTIFFWriteEncodedStrip( psCache->hTIFF, nTileID,\n\t\t\t\t pabyData,\n\t\t\t\t TIFFVStripSize(psCache->hTIFF,RowsInStrip) );\n\t\t\t}\n\t\t}\n\t}\n pabyData = psCache->pabyRow1Blocks;\n psCache->pabyRow1Blocks = psCache->pabyRow2Blocks;\n psCache->pabyRow2Blocks = pabyData;\n _TIFFmemset( pabyData, 0, psCache->nBytesPerRow );\n psCache->nBlockOffset++;\n TIFFFlush( psCache->hTIFF );\n TIFFSetSubDirectory( psCache->hTIFF, nBaseDirOffset );\n}', 'int\nTIFFSetSubDirectory(TIFF* tif, uint64 diroff)\n{\n\ttif->tif_nextdiroff = diroff;\n\ttif->tif_dirnumber = 0;\n\treturn (TIFFReadDirectory(tif));\n}', 'int\nTIFFReadDirectory(TIFF* tif)\n{\n\tstatic const char module[] = "TIFFReadDirectory";\n\tTIFFDirEntry* dir;\n\tuint16 dircount;\n\tTIFFDirEntry* dp;\n\tuint16 di;\n\tconst TIFFField* fip;\n\tuint32 fii=FAILED_FII;\n toff_t nextdiroff;\n int bitspersample_read = FALSE;\n int color_channels;\n\ttif->tif_diroff=tif->tif_nextdiroff;\n\tif (!TIFFCheckDirOffset(tif,tif->tif_nextdiroff))\n\t\treturn 0;\n\t(*tif->tif_cleanup)(tif);\n\ttif->tif_curdir++;\n nextdiroff = tif->tif_nextdiroff;\n\tdircount=TIFFFetchDirectory(tif,nextdiroff,&dir,&tif->tif_nextdiroff);\n\tif (!dircount)\n\t{\n\t\tTIFFErrorExt(tif->tif_clientdata,module,\n\t\t "Failed to read directory at offset " TIFF_UINT64_FORMAT,nextdiroff);\n\t\treturn 0;\n\t}\n\tTIFFReadDirectoryCheckOrder(tif,dir,dircount);\n\t{\n\t\tTIFFDirEntry* ma;\n\t\tuint16 mb;\n\t\tfor (ma=dir, mb=0; mb<dircount; ma++, mb++)\n\t\t{\n\t\t\tTIFFDirEntry* na;\n\t\t\tuint16 nb;\n\t\t\tfor (na=ma+1, nb=mb+1; nb<dircount; na++, nb++)\n\t\t\t{\n\t\t\t\tif (ma->tdir_tag==na->tdir_tag)\n\t\t\t\t\tna->tdir_tag=IGNORE;\n\t\t\t}\n\t\t}\n\t}\n\ttif->tif_flags &= ~TIFF_BEENWRITING;\n\ttif->tif_flags &= ~TIFF_BUF4WRITE;\n\ttif->tif_flags &= ~TIFF_CHOPPEDUPARRAYS;\n\tTIFFFreeDirectory(tif);\n\tTIFFDefaultDirectory(tif);\n\tTIFFSetField(tif,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);\n\tdp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_SAMPLESPERPIXEL);\n\tif (dp)\n\t{\n\t\tif (!TIFFFetchNormalTag(tif,dp,0))\n\t\t\tgoto bad;\n\t\tdp->tdir_tag=IGNORE;\n\t}\n\tdp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_COMPRESSION);\n\tif (dp)\n\t{\n\t\tuint16 value;\n\t\tenum TIFFReadDirEntryErr err;\n\t\terr=TIFFReadDirEntryShort(tif,dp,&value);\n\t\tif (err==TIFFReadDirEntryErrCount)\n\t\t\terr=TIFFReadDirEntryPersampleShort(tif,dp,&value);\n\t\tif (err!=TIFFReadDirEntryErrOk)\n\t\t{\n\t\t\tTIFFReadDirEntryOutputErr(tif,err,module,"Compression",0);\n\t\t\tgoto bad;\n\t\t}\n\t\tif (!TIFFSetField(tif,TIFFTAG_COMPRESSION,value))\n\t\t\tgoto bad;\n\t\tdp->tdir_tag=IGNORE;\n\t}\n\telse\n\t{\n\t\tif (!TIFFSetField(tif,TIFFTAG_COMPRESSION,COMPRESSION_NONE))\n\t\t\tgoto bad;\n\t}\n\tfor (di=0, dp=dir; di<dircount; di++, dp++)\n\t{\n\t\tif (dp->tdir_tag!=IGNORE)\n\t\t{\n\t\t\tTIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);\n\t\t\tif (fii == FAILED_FII)\n\t\t\t{\n\t\t\t\tTIFFWarningExt(tif->tif_clientdata, module,\n\t\t\t\t "Unknown field with tag %d (0x%x) encountered",\n\t\t\t\t dp->tdir_tag,dp->tdir_tag);\n\t\t\t\tif (!_TIFFMergeFields(tif,\n\t\t\t\t\t_TIFFCreateAnonField(tif,\n\t\t\t\t\t\tdp->tdir_tag,\n\t\t\t\t\t\t(TIFFDataType) dp->tdir_type),\n\t\t\t\t\t1)) {\n\t\t\t\t\tTIFFWarningExt(tif->tif_clientdata,\n\t\t\t\t\t module,\n\t\t\t\t\t "Registering anonymous field with tag %d (0x%x) failed",\n\t\t\t\t\t dp->tdir_tag,\n\t\t\t\t\t dp->tdir_tag);\n\t\t\t\t\tdp->tdir_tag=IGNORE;\n\t\t\t\t} else {\n\t\t\t\t\tTIFFReadDirectoryFindFieldInfo(tif,dp->tdir_tag,&fii);\n\t\t\t\t\tassert(fii != FAILED_FII);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (dp->tdir_tag!=IGNORE)\n\t\t{\n\t\t\tfip=tif->tif_fields[fii];\n\t\t\tif (fip->field_bit==FIELD_IGNORE)\n\t\t\t\tdp->tdir_tag=IGNORE;\n\t\t\telse\n\t\t\t{\n\t\t\t\tswitch (dp->tdir_tag)\n\t\t\t\t{\n\t\t\t\t\tcase TIFFTAG_STRIPOFFSETS:\n\t\t\t\t\tcase TIFFTAG_STRIPBYTECOUNTS:\n\t\t\t\t\tcase TIFFTAG_TILEOFFSETS:\n\t\t\t\t\tcase TIFFTAG_TILEBYTECOUNTS:\n\t\t\t\t\t\tTIFFSetFieldBit(tif,fip->field_bit);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase TIFFTAG_IMAGEWIDTH:\n\t\t\t\t\tcase TIFFTAG_IMAGELENGTH:\n\t\t\t\t\tcase TIFFTAG_IMAGEDEPTH:\n\t\t\t\t\tcase TIFFTAG_TILELENGTH:\n\t\t\t\t\tcase TIFFTAG_TILEWIDTH:\n\t\t\t\t\tcase TIFFTAG_TILEDEPTH:\n\t\t\t\t\tcase TIFFTAG_PLANARCONFIG:\n\t\t\t\t\tcase TIFFTAG_ROWSPERSTRIP:\n\t\t\t\t\tcase TIFFTAG_EXTRASAMPLES:\n\t\t\t\t\t\tif (!TIFFFetchNormalTag(tif,dp,0))\n\t\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t\tdp->tdir_tag=IGNORE;\n\t\t\t\t\t\tbreak;\n default:\n if( !_TIFFCheckFieldIsValidForCodec(tif, dp->tdir_tag) )\n dp->tdir_tag=IGNORE;\n break;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tif ((tif->tif_dir.td_compression==COMPRESSION_OJPEG)&&\n\t (tif->tif_dir.td_planarconfig==PLANARCONFIG_SEPARATE))\n\t{\n if (!_TIFFFillStriles(tif))\n goto bad;\n\t\tdp=TIFFReadDirectoryFindEntry(tif,dir,dircount,TIFFTAG_STRIPOFFSETS);\n\t\tif ((dp!=0)&&(dp->tdir_count==1))\n\t\t{\n\t\t\tdp=TIFFReadDirectoryFindEntry(tif,dir,dircount,\n\t\t\t TIFFTAG_STRIPBYTECOUNTS);\n\t\t\tif ((dp!=0)&&(dp->tdir_count==1))\n\t\t\t{\n\t\t\t\ttif->tif_dir.td_planarconfig=PLANARCONFIG_CONTIG;\n\t\t\t\tTIFFWarningExt(tif->tif_clientdata,module,\n\t\t\t\t "Planarconfig tag value assumed incorrect, "\n\t\t\t\t "assuming data is contig instead of chunky");\n\t\t\t}\n\t\t}\n\t}\n\tif (!TIFFFieldSet(tif,FIELD_IMAGEDIMENSIONS))\n\t{\n\t\tMissingRequired(tif,"ImageLength");\n\t\tgoto bad;\n\t}\n\tif (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) {\n\t\ttif->tif_dir.td_nstrips = TIFFNumberOfStrips(tif);\n\t\ttif->tif_dir.td_tilewidth = tif->tif_dir.td_imagewidth;\n\t\ttif->tif_dir.td_tilelength = tif->tif_dir.td_rowsperstrip;\n\t\ttif->tif_dir.td_tiledepth = tif->tif_dir.td_imagedepth;\n\t\ttif->tif_flags &= ~TIFF_ISTILED;\n\t} else {\n\t\ttif->tif_dir.td_nstrips = TIFFNumberOfTiles(tif);\n\t\ttif->tif_flags |= TIFF_ISTILED;\n\t}\n\tif (!tif->tif_dir.td_nstrips) {\n\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t "Cannot handle zero number of %s",\n\t\t isTiled(tif) ? "tiles" : "strips");\n\t\tgoto bad;\n\t}\n\ttif->tif_dir.td_stripsperimage = tif->tif_dir.td_nstrips;\n\tif (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE)\n\t\ttif->tif_dir.td_stripsperimage /= tif->tif_dir.td_samplesperpixel;\n\tif (!TIFFFieldSet(tif, FIELD_STRIPOFFSETS)) {\n#ifdef OJPEG_SUPPORT\n\t\tif ((tif->tif_dir.td_compression==COMPRESSION_OJPEG) &&\n\t\t (isTiled(tif)==0) &&\n\t\t (tif->tif_dir.td_nstrips==1)) {\n\t\t\tTIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);\n\t\t} else\n#endif\n {\n\t\t\tMissingRequired(tif,\n\t\t\t\tisTiled(tif) ? "TileOffsets" : "StripOffsets");\n\t\t\tgoto bad;\n\t\t}\n\t}\n\tfor (di=0, dp=dir; di<dircount; di++, dp++)\n\t{\n\t\tswitch (dp->tdir_tag)\n\t\t{\n\t\t\tcase IGNORE:\n\t\t\t\tbreak;\n\t\t\tcase TIFFTAG_MINSAMPLEVALUE:\n\t\t\tcase TIFFTAG_MAXSAMPLEVALUE:\n\t\t\tcase TIFFTAG_BITSPERSAMPLE:\n\t\t\tcase TIFFTAG_DATATYPE:\n\t\t\tcase TIFFTAG_SAMPLEFORMAT:\n\t\t\t\t{\n\t\t\t\t\tuint16 value;\n\t\t\t\t\tenum TIFFReadDirEntryErr err;\n\t\t\t\t\terr=TIFFReadDirEntryShort(tif,dp,&value);\n\t\t\t\t\tif (err==TIFFReadDirEntryErrCount)\n\t\t\t\t\t\terr=TIFFReadDirEntryPersampleShort(tif,dp,&value);\n\t\t\t\t\tif (err!=TIFFReadDirEntryErrOk)\n\t\t\t\t\t{\n\t\t\t\t\t\tfip = TIFFFieldWithTag(tif,dp->tdir_tag);\n\t\t\t\t\t\tTIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t}\n\t\t\t\t\tif (!TIFFSetField(tif,dp->tdir_tag,value))\n\t\t\t\t\t\tgoto bad;\n if( dp->tdir_tag == TIFFTAG_BITSPERSAMPLE )\n bitspersample_read = TRUE;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase TIFFTAG_SMINSAMPLEVALUE:\n\t\t\tcase TIFFTAG_SMAXSAMPLEVALUE:\n\t\t\t\t{\n\t\t\t\t\tdouble *data = NULL;\n\t\t\t\t\tenum TIFFReadDirEntryErr err;\n\t\t\t\t\tuint32 saved_flags;\n\t\t\t\t\tint m;\n\t\t\t\t\tif (dp->tdir_count != (uint64)tif->tif_dir.td_samplesperpixel)\n\t\t\t\t\t\terr = TIFFReadDirEntryErrCount;\n\t\t\t\t\telse\n\t\t\t\t\t\terr = TIFFReadDirEntryDoubleArray(tif, dp, &data);\n\t\t\t\t\tif (err!=TIFFReadDirEntryErrOk)\n\t\t\t\t\t{\n\t\t\t\t\t\tfip = TIFFFieldWithTag(tif,dp->tdir_tag);\n\t\t\t\t\t\tTIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",0);\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t}\n\t\t\t\t\tsaved_flags = tif->tif_flags;\n\t\t\t\t\ttif->tif_flags |= TIFF_PERSAMPLE;\n\t\t\t\t\tm = TIFFSetField(tif,dp->tdir_tag,data);\n\t\t\t\t\ttif->tif_flags = saved_flags;\n\t\t\t\t\t_TIFFfree(data);\n\t\t\t\t\tif (!m)\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase TIFFTAG_STRIPOFFSETS:\n\t\t\tcase TIFFTAG_TILEOFFSETS:\n _TIFFmemcpy( &(tif->tif_dir.td_stripoffset_entry),\n dp, sizeof(TIFFDirEntry) );\n\t\t\t\tbreak;\n\t\t\tcase TIFFTAG_STRIPBYTECOUNTS:\n\t\t\tcase TIFFTAG_TILEBYTECOUNTS:\n _TIFFmemcpy( &(tif->tif_dir.td_stripbytecount_entry),\n dp, sizeof(TIFFDirEntry) );\n\t\t\t\tbreak;\n\t\t\tcase TIFFTAG_COLORMAP:\n\t\t\tcase TIFFTAG_TRANSFERFUNCTION:\n\t\t\t\t{\n\t\t\t\t\tenum TIFFReadDirEntryErr err;\n\t\t\t\t\tuint32 countpersample;\n\t\t\t\t\tuint32 countrequired;\n\t\t\t\t\tuint32 incrementpersample;\n\t\t\t\t\tuint16* value=NULL;\n if( !bitspersample_read )\n {\n fip = TIFFFieldWithTag(tif,dp->tdir_tag);\n TIFFWarningExt(tif->tif_clientdata,module,\n "Ignoring %s since BitsPerSample tag not found",\n fip ? fip->field_name : "unknown tagname");\n continue;\n }\n\t\t\t\t\tif (tif->tif_dir.td_bitspersample > 24)\n\t\t\t\t\t{\n\t\t\t\t\t fip = TIFFFieldWithTag(tif,dp->tdir_tag);\n\t\t\t\t\t TIFFWarningExt(tif->tif_clientdata,module,\n\t\t\t\t\t\t"Ignoring %s because BitsPerSample=%d>24",\n\t\t\t\t\t\tfip ? fip->field_name : "unknown tagname",\n\t\t\t\t\t\ttif->tif_dir.td_bitspersample);\n\t\t\t\t\t continue;\n\t\t\t\t\t}\n\t\t\t\t\tcountpersample=(1U<<tif->tif_dir.td_bitspersample);\n\t\t\t\t\tif ((dp->tdir_tag==TIFFTAG_TRANSFERFUNCTION)&&(dp->tdir_count==(uint64)countpersample))\n\t\t\t\t\t{\n\t\t\t\t\t\tcountrequired=countpersample;\n\t\t\t\t\t\tincrementpersample=0;\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tcountrequired=3*countpersample;\n\t\t\t\t\t\tincrementpersample=countpersample;\n\t\t\t\t\t}\n\t\t\t\t\tif (dp->tdir_count!=(uint64)countrequired)\n\t\t\t\t\t\terr=TIFFReadDirEntryErrCount;\n\t\t\t\t\telse\n\t\t\t\t\t\terr=TIFFReadDirEntryShortArray(tif,dp,&value);\n\t\t\t\t\tif (err!=TIFFReadDirEntryErrOk)\n {\n\t\t\t\t\t\tfip = TIFFFieldWithTag(tif,dp->tdir_tag);\n\t\t\t\t\t\tTIFFReadDirEntryOutputErr(tif,err,module,fip ? fip->field_name : "unknown tagname",1);\n }\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tTIFFSetField(tif,dp->tdir_tag,value,value+incrementpersample,value+2*incrementpersample);\n\t\t\t\t\t\t_TIFFfree(value);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase TIFFTAG_OSUBFILETYPE:\n\t\t\t\t{\n\t\t\t\t\tuint16 valueo;\n\t\t\t\t\tuint32 value;\n\t\t\t\t\tif (TIFFReadDirEntryShort(tif,dp,&valueo)==TIFFReadDirEntryErrOk)\n\t\t\t\t\t{\n\t\t\t\t\t\tswitch (valueo)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tcase OFILETYPE_REDUCEDIMAGE: value=FILETYPE_REDUCEDIMAGE; break;\n\t\t\t\t\t\t\tcase OFILETYPE_PAGE: value=FILETYPE_PAGE; break;\n\t\t\t\t\t\t\tdefault: value=0; break;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (value!=0)\n\t\t\t\t\t\t\tTIFFSetField(tif,TIFFTAG_SUBFILETYPE,value);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\t(void) TIFFFetchNormalTag(tif, dp, TRUE);\n\t\t\t\tbreak;\n\t\t}\n\t}\n if( tif->tif_mode == O_RDWR &&\n tif->tif_dir.td_stripoffset_entry.tdir_tag != 0 &&\n tif->tif_dir.td_stripoffset_entry.tdir_count == 0 &&\n tif->tif_dir.td_stripoffset_entry.tdir_type == 0 &&\n tif->tif_dir.td_stripoffset_entry.tdir_offset.toff_long8 == 0 &&\n tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0 &&\n tif->tif_dir.td_stripbytecount_entry.tdir_count == 0 &&\n tif->tif_dir.td_stripbytecount_entry.tdir_type == 0 &&\n tif->tif_dir.td_stripbytecount_entry.tdir_offset.toff_long8 == 0 )\n {\n TIFFSetupStrips(tif);\n }\n else if( !(tif->tif_flags&TIFF_DEFERSTRILELOAD) )\n {\n if( tif->tif_dir.td_stripoffset_entry.tdir_tag != 0 )\n {\n if (!TIFFFetchStripThing(tif,&(tif->tif_dir.td_stripoffset_entry),\n tif->tif_dir.td_nstrips,\n &tif->tif_dir.td_stripoffset_p))\n {\n goto bad;\n }\n }\n if( tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0 )\n {\n if (!TIFFFetchStripThing(tif,&(tif->tif_dir.td_stripbytecount_entry),\n tif->tif_dir.td_nstrips,\n &tif->tif_dir.td_stripbytecount_p))\n {\n goto bad;\n }\n }\n }\n\tif (tif->tif_dir.td_compression==COMPRESSION_OJPEG)\n\t{\n\t\tif (!TIFFFieldSet(tif,FIELD_PHOTOMETRIC))\n\t\t{\n\t\t\tTIFFWarningExt(tif->tif_clientdata, module,\n\t\t\t "Photometric tag is missing, assuming data is YCbCr");\n\t\t\tif (!TIFFSetField(tif,TIFFTAG_PHOTOMETRIC,PHOTOMETRIC_YCBCR))\n\t\t\t\tgoto bad;\n\t\t}\n\t\telse if (tif->tif_dir.td_photometric==PHOTOMETRIC_RGB)\n\t\t{\n\t\t\ttif->tif_dir.td_photometric=PHOTOMETRIC_YCBCR;\n\t\t\tTIFFWarningExt(tif->tif_clientdata, module,\n\t\t\t "Photometric tag value assumed incorrect, "\n\t\t\t "assuming data is YCbCr instead of RGB");\n\t\t}\n\t\tif (!TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))\n\t\t{\n\t\t\tTIFFWarningExt(tif->tif_clientdata,module,\n\t\t\t "BitsPerSample tag is missing, assuming 8 bits per sample");\n\t\t\tif (!TIFFSetField(tif,TIFFTAG_BITSPERSAMPLE,8))\n\t\t\t\tgoto bad;\n\t\t}\n\t\tif (!TIFFFieldSet(tif,FIELD_SAMPLESPERPIXEL))\n\t\t{\n\t\t\tif (tif->tif_dir.td_photometric==PHOTOMETRIC_RGB)\n\t\t\t{\n\t\t\t\tTIFFWarningExt(tif->tif_clientdata,module,\n\t\t\t\t "SamplesPerPixel tag is missing, "\n\t\t\t\t "assuming correct SamplesPerPixel value is 3");\n\t\t\t\tif (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,3))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (tif->tif_dir.td_photometric==PHOTOMETRIC_YCBCR)\n\t\t\t{\n\t\t\t\tTIFFWarningExt(tif->tif_clientdata,module,\n\t\t\t\t "SamplesPerPixel tag is missing, "\n\t\t\t\t "applying correct SamplesPerPixel value of 3");\n\t\t\t\tif (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,3))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\telse if ((tif->tif_dir.td_photometric==PHOTOMETRIC_MINISWHITE)\n\t\t\t\t || (tif->tif_dir.td_photometric==PHOTOMETRIC_MINISBLACK))\n\t\t\t{\n\t\t\t\tif (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,1))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t}\n\t}\n color_channels = _TIFFGetMaxColorChannels(tif->tif_dir.td_photometric);\n if (color_channels && tif->tif_dir.td_samplesperpixel - tif->tif_dir.td_extrasamples > color_channels) {\n uint16 old_extrasamples;\n uint16 *new_sampleinfo;\n TIFFWarningExt(tif->tif_clientdata,module, "Sum of Photometric type-related "\n "color channels and ExtraSamples doesn\'t match SamplesPerPixel. "\n "Defining non-color channels as ExtraSamples.");\n old_extrasamples = tif->tif_dir.td_extrasamples;\n tif->tif_dir.td_extrasamples = (uint16) (tif->tif_dir.td_samplesperpixel - color_channels);\n new_sampleinfo = (uint16*) _TIFFcalloc(tif->tif_dir.td_extrasamples, sizeof(uint16));\n if (!new_sampleinfo) {\n TIFFErrorExt(tif->tif_clientdata, module, "Failed to allocate memory for "\n "temporary new sampleinfo array (%d 16 bit elements)",\n tif->tif_dir.td_extrasamples);\n goto bad;\n }\n memcpy(new_sampleinfo, tif->tif_dir.td_sampleinfo, old_extrasamples * sizeof(uint16));\n _TIFFsetShortArray(&tif->tif_dir.td_sampleinfo, new_sampleinfo, tif->tif_dir.td_extrasamples);\n _TIFFfree(new_sampleinfo);\n }\n\tif (tif->tif_dir.td_photometric == PHOTOMETRIC_PALETTE &&\n\t !TIFFFieldSet(tif, FIELD_COLORMAP)) {\n\t\tif ( tif->tif_dir.td_bitspersample>=8 && tif->tif_dir.td_samplesperpixel==3)\n\t\t\ttif->tif_dir.td_photometric = PHOTOMETRIC_RGB;\n\t\telse if (tif->tif_dir.td_bitspersample>=8)\n\t\t\ttif->tif_dir.td_photometric = PHOTOMETRIC_MINISBLACK;\n\t\telse {\n\t\t\tMissingRequired(tif, "Colormap");\n\t\t\tgoto bad;\n\t\t}\n\t}\n\tif (tif->tif_dir.td_compression!=COMPRESSION_OJPEG)\n\t{\n\t\tif (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS)) {\n\t\t\tif ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&\n\t\t\t tif->tif_dir.td_nstrips > 1) ||\n\t\t\t (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE &&\n\t\t\t tif->tif_dir.td_nstrips != (uint32)tif->tif_dir.td_samplesperpixel)) {\n\t\t\t MissingRequired(tif, "StripByteCounts");\n\t\t\t goto bad;\n\t\t\t}\n\t\t\tTIFFWarningExt(tif->tif_clientdata, module,\n\t\t\t\t"TIFF directory is missing required "\n\t\t\t\t"\\"StripByteCounts\\" field, calculating from imagelength");\n\t\t\tif (EstimateStripByteCounts(tif, dir, dircount) < 0)\n\t\t\t goto bad;\n\t\t} else if (tif->tif_dir.td_nstrips == 1\n && !(tif->tif_flags&TIFF_ISTILED)\n\t\t\t && ByteCountLooksBad(tif)) {\n\t\t\tTIFFWarningExt(tif->tif_clientdata, module,\n\t\t\t "Bogus \\"StripByteCounts\\" field, ignoring and calculating from imagelength");\n\t\t\tif(EstimateStripByteCounts(tif, dir, dircount) < 0)\n\t\t\t goto bad;\n\t\t} else if (!(tif->tif_flags&TIFF_DEFERSTRILELOAD)\n\t\t\t && tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG\n\t\t\t && tif->tif_dir.td_nstrips > 2\n\t\t\t && tif->tif_dir.td_compression == COMPRESSION_NONE\n\t\t\t && TIFFGetStrileByteCount(tif, 0) != TIFFGetStrileByteCount(tif, 1)\n\t\t\t && TIFFGetStrileByteCount(tif, 0) != 0\n\t\t\t && TIFFGetStrileByteCount(tif, 1) != 0 ) {\n\t\t\tTIFFWarningExt(tif->tif_clientdata, module,\n\t\t\t "Wrong \\"StripByteCounts\\" field, ignoring and calculating from imagelength");\n\t\t\tif (EstimateStripByteCounts(tif, dir, dircount) < 0)\n\t\t\t goto bad;\n\t\t}\n\t}\n\tif (dir)\n\t{\n\t\t_TIFFfree(dir);\n\t\tdir=NULL;\n\t}\n\tif (!TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE))\n\t{\n\t\tif (tif->tif_dir.td_bitspersample>=16)\n\t\t\ttif->tif_dir.td_maxsamplevalue=0xFFFF;\n\t\telse\n\t\t\ttif->tif_dir.td_maxsamplevalue = (uint16)((1L<<tif->tif_dir.td_bitspersample)-1);\n\t}\n#ifdef STRIPBYTECOUNTSORTED_UNUSED\n\tif (!(tif->tif_flags&TIFF_DEFERSTRILELOAD) && tif->tif_dir.td_nstrips > 1) {\n\t\tuint32 strip;\n\t\ttif->tif_dir.td_stripbytecountsorted = 1;\n\t\tfor (strip = 1; strip < tif->tif_dir.td_nstrips; strip++) {\n\t\t\tif (TIFFGetStrileOffset(tif, strip - 1) >\n\t\t\t TIFFGetStrileOffset(tif, strip)) {\n\t\t\t\ttif->tif_dir.td_stripbytecountsorted = 0;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n#endif\n\t(*tif->tif_fixuptags)(tif);\n\tif ((tif->tif_dir.td_planarconfig==PLANARCONFIG_CONTIG)&&\n\t (tif->tif_dir.td_nstrips==1)&&\n\t (tif->tif_dir.td_compression==COMPRESSION_NONE)&&\n\t ((tif->tif_flags&(TIFF_STRIPCHOP|TIFF_ISTILED))==TIFF_STRIPCHOP))\n {\n ChopUpSingleUncompressedStrip(tif);\n }\n if( tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&\n tif->tif_dir.td_compression == COMPRESSION_NONE &&\n (tif->tif_flags&(TIFF_STRIPCHOP|TIFF_ISTILED)) == TIFF_STRIPCHOP &&\n TIFFStripSize64(tif) > 0x7FFFFFFFUL )\n {\n TryChopUpUncompressedBigTiff(tif);\n }\n\ttif->tif_flags &= ~TIFF_DIRTYDIRECT;\n\ttif->tif_flags &= ~TIFF_DIRTYSTRIP;\n\ttif->tif_row = (uint32) -1;\n\ttif->tif_curstrip = (uint32) -1;\n\ttif->tif_col = (uint32) -1;\n\ttif->tif_curtile = (uint32) -1;\n\ttif->tif_tilesize = (tmsize_t) -1;\n\ttif->tif_scanlinesize = TIFFScanlineSize(tif);\n\tif (!tif->tif_scanlinesize) {\n\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t "Cannot handle zero scanline size");\n\t\treturn (0);\n\t}\n\tif (isTiled(tif)) {\n\t\ttif->tif_tilesize = TIFFTileSize(tif);\n\t\tif (!tif->tif_tilesize) {\n\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t "Cannot handle zero tile size");\n\t\t\treturn (0);\n\t\t}\n\t} else {\n\t\tif (!TIFFStripSize(tif)) {\n\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t "Cannot handle zero strip size");\n\t\t\treturn (0);\n\t\t}\n\t}\n\treturn (1);\nbad:\n\tif (dir)\n\t\t_TIFFfree(dir);\n\treturn (0);\n}', 'int\nTIFFFlush(TIFF* tif)\n{\n if( tif->tif_mode == O_RDONLY )\n return 1;\n if (!TIFFFlushData(tif))\n return (0);\n if( (tif->tif_flags & TIFF_DIRTYSTRIP)\n && !(tif->tif_flags & TIFF_DIRTYDIRECT)\n && tif->tif_mode == O_RDWR )\n {\n if( TIFFForceStrileArrayWriting(tif) )\n return 1;\n }\n if ((tif->tif_flags & (TIFF_DIRTYDIRECT|TIFF_DIRTYSTRIP))\n && !TIFFRewriteDirectory(tif))\n return (0);\n return (1);\n}', 'int TIFFForceStrileArrayWriting(TIFF* tif)\n{\n static const char module[] = "TIFFForceStrileArrayWriting";\n const int isTiled = TIFFIsTiled(tif);\n if (tif->tif_mode == O_RDONLY)\n {\n TIFFErrorExt(tif->tif_clientdata, tif->tif_name,\n "File opened in read-only mode");\n return 0;\n }\n if( tif->tif_diroff == 0 )\n {\n TIFFErrorExt(tif->tif_clientdata, module,\n "Directory has not yet been written");\n return 0;\n }\n if( (tif->tif_flags & TIFF_DIRTYDIRECT) != 0 )\n {\n TIFFErrorExt(tif->tif_clientdata, module,\n "Directory has changes other than the strile arrays. "\n "TIFFRewriteDirectory() should be called instead");\n return 0;\n }\n if( !(tif->tif_flags & TIFF_DIRTYSTRIP) )\n {\n if( !(tif->tif_dir.td_stripoffset_entry.tdir_tag != 0 &&\n tif->tif_dir.td_stripoffset_entry.tdir_count == 0 &&\n tif->tif_dir.td_stripoffset_entry.tdir_type == 0 &&\n tif->tif_dir.td_stripoffset_entry.tdir_offset.toff_long8 == 0 &&\n tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0 &&\n tif->tif_dir.td_stripbytecount_entry.tdir_count == 0 &&\n tif->tif_dir.td_stripbytecount_entry.tdir_type == 0 &&\n tif->tif_dir.td_stripbytecount_entry.tdir_offset.toff_long8 == 0) )\n {\n TIFFErrorExt(tif->tif_clientdata, module,\n "Function not called together with "\n "TIFFDeferStrileArrayWriting()");\n return 0;\n }\n if (tif->tif_dir.td_stripoffset_p == NULL && !TIFFSetupStrips(tif))\n return 0;\n }\n if( _TIFFRewriteField( tif,\n isTiled ? TIFFTAG_TILEOFFSETS :\n TIFFTAG_STRIPOFFSETS,\n TIFF_LONG8,\n tif->tif_dir.td_nstrips,\n tif->tif_dir.td_stripoffset_p )\n && _TIFFRewriteField( tif,\n isTiled ? TIFFTAG_TILEBYTECOUNTS :\n TIFFTAG_STRIPBYTECOUNTS,\n TIFF_LONG8,\n tif->tif_dir.td_nstrips,\n tif->tif_dir.td_stripbytecount_p ) )\n {\n tif->tif_flags &= ~TIFF_DIRTYSTRIP;\n tif->tif_flags &= ~TIFF_BEENWRITING;\n return 1;\n }\n return 0;\n}', 'int\nTIFFSetupStrips(TIFF* tif)\n{\n\tTIFFDirectory* td = &tif->tif_dir;\n\tif (isTiled(tif))\n\t\ttd->td_stripsperimage =\n\t\t isUnspecified(tif, FIELD_TILEDIMENSIONS) ?\n\t\t\ttd->td_samplesperpixel : TIFFNumberOfTiles(tif);\n\telse\n\t\ttd->td_stripsperimage =\n\t\t isUnspecified(tif, FIELD_ROWSPERSTRIP) ?\n\t\t\ttd->td_samplesperpixel : TIFFNumberOfStrips(tif);\n\ttd->td_nstrips = td->td_stripsperimage;\n\tif (td->td_planarconfig == PLANARCONFIG_SEPARATE)\n\t\ttd->td_stripsperimage /= td->td_samplesperpixel;\n\ttd->td_stripoffset_p = (uint64 *)\n _TIFFCheckMalloc(tif, td->td_nstrips, sizeof (uint64),\n "for \\"StripOffsets\\" array");\n\ttd->td_stripbytecount_p = (uint64 *)\n _TIFFCheckMalloc(tif, td->td_nstrips, sizeof (uint64),\n "for \\"StripByteCounts\\" array");\n\tif (td->td_stripoffset_p == NULL || td->td_stripbytecount_p == NULL)\n\t\treturn (0);\n\t_TIFFmemset(td->td_stripoffset_p, 0, td->td_nstrips*sizeof (uint64));\n\t_TIFFmemset(td->td_stripbytecount_p, 0, td->td_nstrips*sizeof (uint64));\n\tTIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);\n\tTIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);\n\treturn (1);\n}', 'uint32\nTIFFNumberOfTiles(TIFF* tif)\n{\n\tTIFFDirectory *td = &tif->tif_dir;\n\tuint32 dx = td->td_tilewidth;\n\tuint32 dy = td->td_tilelength;\n\tuint32 dz = td->td_tiledepth;\n\tuint32 ntiles;\n\tif (dx == (uint32) -1)\n\t\tdx = td->td_imagewidth;\n\tif (dy == (uint32) -1)\n\t\tdy = td->td_imagelength;\n\tif (dz == (uint32) -1)\n\t\tdz = td->td_imagedepth;\n\tntiles = (dx == 0 || dy == 0 || dz == 0) ? 0 :\n\t _TIFFMultiply32(tif, _TIFFMultiply32(tif, TIFFhowmany_32(td->td_imagewidth, dx),\n\t TIFFhowmany_32(td->td_imagelength, dy),\n\t "TIFFNumberOfTiles"),\n\t TIFFhowmany_32(td->td_imagedepth, dz), "TIFFNumberOfTiles");\n\tif (td->td_planarconfig == PLANARCONFIG_SEPARATE)\n\t\tntiles = _TIFFMultiply32(tif, ntiles, td->td_samplesperpixel,\n\t\t "TIFFNumberOfTiles");\n\treturn (ntiles);\n}']
|
2,620
| 0
|
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/bn/bn_shift.c/#L162
|
int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)
{
int i, nw, lb, rb;
BN_ULONG *t, *f;
BN_ULONG l;
bn_check_top(r);
bn_check_top(a);
if (n < 0) {
BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);
return 0;
}
r->neg = a->neg;
nw = n / BN_BITS2;
if (bn_wexpand(r, a->top + nw + 1) == NULL)
return (0);
lb = n % BN_BITS2;
rb = BN_BITS2 - lb;
f = a->d;
t = r->d;
t[a->top + nw] = 0;
if (lb == 0)
for (i = a->top - 1; i >= 0; i--)
t[nw + i] = f[i];
else
for (i = a->top - 1; i >= 0; i--) {
l = f[i];
t[nw + i + 1] |= (l >> rb) & BN_MASK2;
t[nw + i] = (l << lb) & BN_MASK2;
}
memset(t, 0, sizeof(*t) * nw);
r->top = a->top + nw + 1;
bn_correct_top(r);
bn_check_top(r);
return (1);
}
|
['int test_div_recp(BIO *bp, BN_CTX *ctx)\n{\n BIGNUM *a, *b, *c, *d, *e;\n BN_RECP_CTX *recp;\n int i;\n recp = BN_RECP_CTX_new();\n a = BN_new();\n b = BN_new();\n c = BN_new();\n d = BN_new();\n e = BN_new();\n for (i = 0; i < num0 + num1; i++) {\n if (i < num1) {\n BN_bntest_rand(a, 400, 0, 0);\n BN_copy(b, a);\n BN_lshift(a, a, i);\n BN_add_word(a, i);\n } else\n BN_bntest_rand(b, 50 + 3 * (i - num1), 0, 0);\n a->neg = rand_neg();\n b->neg = rand_neg();\n BN_RECP_CTX_set(recp, b, ctx);\n BN_div_recp(d, c, a, recp, ctx);\n if (bp != NULL) {\n if (!results) {\n BN_print(bp, a);\n BIO_puts(bp, " / ");\n BN_print(bp, b);\n BIO_puts(bp, " - ");\n }\n BN_print(bp, d);\n BIO_puts(bp, "\\n");\n if (!results) {\n BN_print(bp, a);\n BIO_puts(bp, " % ");\n BN_print(bp, b);\n BIO_puts(bp, " - ");\n }\n BN_print(bp, c);\n BIO_puts(bp, "\\n");\n }\n BN_mul(e, d, b, ctx);\n BN_add(d, e, c);\n BN_sub(d, d, a);\n if (!BN_is_zero(d)) {\n fprintf(stderr, "Reciprocal division test failed!\\n");\n fprintf(stderr, "a=");\n BN_print_fp(stderr, a);\n fprintf(stderr, "\\nb=");\n BN_print_fp(stderr, b);\n fprintf(stderr, "\\n");\n return 0;\n }\n }\n BN_free(a);\n BN_free(b);\n BN_free(c);\n BN_free(d);\n BN_free(e);\n BN_RECP_CTX_free(recp);\n return (1);\n}', 'int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l;\n bn_check_top(r);\n bn_check_top(a);\n if (n < 0) {\n BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n r->neg = a->neg;\n nw = n / BN_BITS2;\n if (bn_wexpand(r, a->top + nw + 1) == NULL)\n return (0);\n lb = n % BN_BITS2;\n rb = BN_BITS2 - lb;\n f = a->d;\n t = r->d;\n t[a->top + nw] = 0;\n if (lb == 0)\n for (i = a->top - 1; i >= 0; i--)\n t[nw + i] = f[i];\n else\n for (i = a->top - 1; i >= 0; i--) {\n l = f[i];\n t[nw + i + 1] |= (l >> rb) & BN_MASK2;\n t[nw + i] = (l << lb) & BN_MASK2;\n }\n memset(t, 0, sizeof(*t) * nw);\n r->top = a->top + nw + 1;\n bn_correct_top(r);\n bn_check_top(r);\n return (1);\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']
|
2,621
| 0
|
https://github.com/openssl/openssl/blob/d40a1b865fddc3d67f8c06ff1f1466fad331c8f7/crypto/bn/bn_lib.c/#L709
|
int BN_cmp(const BIGNUM *a, const BIGNUM *b)
{
int i;
int gt,lt;
BN_ULONG t1,t2;
if ((a == NULL) || (b == NULL))
{
if (a != NULL)
return(-1);
else if (b != NULL)
return(1);
else
return(0);
}
bn_check_top(a);
bn_check_top(b);
if (a->neg != b->neg)
{
if (a->neg)
return(-1);
else return(1);
}
if (a->neg == 0)
{ gt=1; lt= -1; }
else { gt= -1; lt=1; }
if (a->top > b->top) return(gt);
if (a->top < b->top) return(lt);
for (i=a->top-1; i>=0; i--)
{
t1=a->d[i];
t2=b->d[i];
if (t1 > t2) return(gt);
if (t1 < t2) return(lt);
}
return(0);
}
|
['int BN_is_prime_fasttest_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,\n\t\tint do_trial_division, BN_GENCB *cb)\n\t{\n\tint i, j, ret = -1;\n\tint k;\n\tBN_CTX *ctx = NULL;\n\tBIGNUM *A1, *A1_odd, *check;\n\tBN_MONT_CTX *mont = NULL;\n\tconst BIGNUM *A = NULL;\n\tif (BN_cmp(a, BN_value_one()) <= 0)\n\t\treturn 0;\n\tif (checks == BN_prime_checks)\n\t\tchecks = BN_prime_checks_for_size(BN_num_bits(a));\n\tif (!BN_is_odd(a))\n\t\treturn BN_is_word(a, 2);\n\tif (do_trial_division)\n\t\t{\n\t\tfor (i = 1; i < NUMPRIMES; i++)\n\t\t\tif (BN_mod_word(a, primes[i]) == 0)\n\t\t\t\treturn 0;\n\t\tif(!BN_GENCB_call(cb, 1, -1))\n\t\t\tgoto err;\n\t\t}\n\tif (ctx_passed != NULL)\n\t\tctx = ctx_passed;\n\telse\n\t\tif ((ctx=BN_CTX_new()) == NULL)\n\t\t\tgoto err;\n\tBN_CTX_start(ctx);\n\tif (a->neg)\n\t\t{\n\t\tBIGNUM *t;\n\t\tif ((t = BN_CTX_get(ctx)) == NULL) goto err;\n\t\tBN_copy(t, a);\n\t\tt->neg = 0;\n\t\tA = t;\n\t\t}\n\telse\n\t\tA = a;\n\tA1 = BN_CTX_get(ctx);\n\tA1_odd = BN_CTX_get(ctx);\n\tcheck = BN_CTX_get(ctx);\n\tif (check == NULL) goto err;\n\tif (!BN_copy(A1, A))\n\t\tgoto err;\n\tif (!BN_sub_word(A1, 1))\n\t\tgoto err;\n\tif (BN_is_zero(A1))\n\t\t{\n\t\tret = 0;\n\t\tgoto err;\n\t\t}\n\tk = 1;\n\twhile (!BN_is_bit_set(A1, k))\n\t\tk++;\n\tif (!BN_rshift(A1_odd, A1, k))\n\t\tgoto err;\n\tmont = BN_MONT_CTX_new();\n\tif (mont == NULL)\n\t\tgoto err;\n\tif (!BN_MONT_CTX_set(mont, A, ctx))\n\t\tgoto err;\n\tfor (i = 0; i < checks; i++)\n\t\t{\n\t\tif (!BN_pseudo_rand_range(check, A1))\n\t\t\tgoto err;\n\t\tif (!BN_add_word(check, 1))\n\t\t\tgoto err;\n\t\tj = witness(check, A, A1, A1_odd, k, ctx, mont);\n\t\tif (j == -1) goto err;\n\t\tif (j)\n\t\t\t{\n\t\t\tret=0;\n\t\t\tgoto err;\n\t\t\t}\n\t\tif(!BN_GENCB_call(cb, 1, i))\n\t\t\tgoto err;\n\t\t}\n\tret=1;\nerr:\n\tif (ctx != NULL)\n\t\t{\n\t\tBN_CTX_end(ctx);\n\t\tif (ctx_passed == NULL)\n\t\t\tBN_CTX_free(ctx);\n\t\t}\n\tif (mont != NULL)\n\t\tBN_MONT_CTX_free(mont);\n\treturn(ret);\n\t}', 'int BN_cmp(const BIGNUM *a, const BIGNUM *b)\n\t{\n\tint i;\n\tint gt,lt;\n\tBN_ULONG t1,t2;\n\tif ((a == NULL) || (b == NULL))\n\t\t{\n\t\tif (a != NULL)\n\t\t\treturn(-1);\n\t\telse if (b != NULL)\n\t\t\treturn(1);\n\t\telse\n\t\t\treturn(0);\n\t\t}\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tif (a->neg != b->neg)\n\t\t{\n\t\tif (a->neg)\n\t\t\treturn(-1);\n\t\telse\treturn(1);\n\t\t}\n\tif (a->neg == 0)\n\t\t{ gt=1; lt= -1; }\n\telse\t{ gt= -1; lt=1; }\n\tif (a->top > b->top) return(gt);\n\tif (a->top < b->top) return(lt);\n\tfor (i=a->top-1; i>=0; i--)\n\t\t{\n\t\tt1=a->d[i];\n\t\tt2=b->d[i];\n\t\tif (t1 > t2) return(gt);\n\t\tif (t1 < t2) return(lt);\n\t\t}\n\treturn(0);\n\t}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n\t{\n\tint i;\n\tBN_ULONG *A;\n\tconst BN_ULONG *B;\n\tbn_check_top(b);\n\tif (a == b) return(a);\n\tif (bn_wexpand(a,b->top) == NULL) return(NULL);\n#if 1\n\tA=a->d;\n\tB=b->d;\n\tfor (i=b->top>>2; i>0; i--,A+=4,B+=4)\n\t\t{\n\t\tBN_ULONG a0,a1,a2,a3;\n\t\ta0=B[0]; a1=B[1]; a2=B[2]; a3=B[3];\n\t\tA[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3;\n\t\t}\n\tswitch (b->top&3)\n\t\t{\n\t\tcase 3: A[2]=B[2];\n\t\tcase 2: A[1]=B[1];\n\t\tcase 1: A[0]=B[0];\n\t\tcase 0: ;\n\t\t}\n#else\n\tmemcpy(a->d,b->d,sizeof(b->d[0])*b->top);\n#endif\n\ta->top=b->top;\n\ta->neg=b->neg;\n\tbn_check_top(a);\n\treturn(a);\n\t}', 'int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)\n\t{\n\tint ret = 0;\n\tBIGNUM *Ri,*R;\n\tBN_CTX_start(ctx);\n\tif((Ri = BN_CTX_get(ctx)) == NULL) goto err;\n\tR= &(mont->RR);\n\tif (!BN_copy(&(mont->N),mod)) goto err;\n\tmont->N.neg = 0;\n#ifdef MONT_WORD\n\t\t{\n\t\tBIGNUM tmod;\n\t\tBN_ULONG buf[2];\n\t\tBN_init(&tmod);\n\t\ttmod.d=buf;\n\t\ttmod.dmax=2;\n\t\ttmod.neg=0;\n\t\tmont->ri=(BN_num_bits(mod)+(BN_BITS2-1))/BN_BITS2*BN_BITS2;\n#if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)\n\t\tBN_zero(R);\n\t\tif (!(BN_set_bit(R,2*BN_BITS2))) goto err;\n\t\t\t\t\t\t\t\ttmod.top=0;\n\t\tif ((buf[0] = mod->d[0]))\t\t\ttmod.top=1;\n\t\tif ((buf[1] = mod->top>1 ? mod->d[1] : 0))\ttmod.top=2;\n\t\tif ((BN_mod_inverse(Ri,R,&tmod,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tif (!BN_lshift(Ri,Ri,2*BN_BITS2)) goto err;\n\t\tif (!BN_is_zero(Ri))\n\t\t\t{\n\t\t\tif (!BN_sub_word(Ri,1)) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (bn_expand(Ri,(int)sizeof(BN_ULONG)*2) == NULL)\n\t\t\t\tgoto err;\n\t\t\tRi->neg=0;\n\t\t\tRi->d[0]=BN_MASK2;\n\t\t\tRi->d[1]=BN_MASK2;\n\t\t\tRi->top=2;\n\t\t\t}\n\t\tif (!BN_div(Ri,NULL,Ri,&tmod,ctx)) goto err;\n\t\tmont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n\t\tmont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;\n#else\n\t\tBN_zero(R);\n\t\tif (!(BN_set_bit(R,BN_BITS2))) goto err;\n\t\tbuf[0]=mod->d[0];\n\t\tbuf[1]=0;\n\t\ttmod.top = buf[0] != 0 ? 1 : 0;\n\t\tif ((BN_mod_inverse(Ri,R,&tmod,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tif (!BN_lshift(Ri,Ri,BN_BITS2)) goto err;\n\t\tif (!BN_is_zero(Ri))\n\t\t\t{\n\t\t\tif (!BN_sub_word(Ri,1)) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (!BN_set_word(Ri,BN_MASK2)) goto err;\n\t\t\t}\n\t\tif (!BN_div(Ri,NULL,Ri,&tmod,ctx)) goto err;\n\t\tmont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n\t\tmont->n0[1] = 0;\n#endif\n\t\t}\n#else\n\t\t{\n\t\tmont->ri=BN_num_bits(&mont->N);\n\t\tBN_zero(R);\n\t\tif (!BN_set_bit(R,mont->ri)) goto err;\n\t\tif ((BN_mod_inverse(Ri,R,&mont->N,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tif (!BN_lshift(Ri,Ri,mont->ri)) goto err;\n\t\tif (!BN_sub_word(Ri,1)) goto err;\n\t\tif (!BN_div(&(mont->Ni),NULL,Ri,&mont->N,ctx)) goto err;\n\t\t}\n#endif\n\tBN_zero(&(mont->RR));\n\tif (!BN_set_bit(&(mont->RR),mont->ri*2)) goto err;\n\tif (!BN_mod(&(mont->RR),&(mont->RR),&(mont->N),ctx)) goto err;\n\tret = 1;\nerr:\n\tBN_CTX_end(ctx);\n\treturn ret;\n\t}', 'int\tBN_pseudo_rand_range(BIGNUM *r, const BIGNUM *range)\n\t{\n\treturn bn_rand_range(1, r, range);\n\t}', 'static int bn_rand_range(int pseudo, BIGNUM *r, const BIGNUM *range)\n\t{\n\tint (*bn_rand)(BIGNUM *, size_t, int, int)\n\t = pseudo ? BN_pseudo_rand : BN_rand;\n\tint n;\n\tint count = 100;\n\tif (range->neg || BN_is_zero(range))\n\t\t{\n\t\tBNerr(BN_F_BN_RAND_RANGE, BN_R_INVALID_RANGE);\n\t\treturn 0;\n\t\t}\n\tn = BN_num_bits(range);\n\tif (n == 1)\n\t\tBN_zero(r);\n\telse if (!BN_is_bit_set(range, n - 2) && !BN_is_bit_set(range, n - 3))\n\t\t{\n\t\tdo\n\t\t\t{\n\t\t\tif (!bn_rand(r, n + 1, -1, 0)) return 0;\n\t\t\tif (BN_cmp(r ,range) >= 0)\n\t\t\t\t{\n\t\t\t\tif (!BN_sub(r, r, range)) return 0;\n\t\t\t\tif (BN_cmp(r, range) >= 0)\n\t\t\t\t\tif (!BN_sub(r, r, range)) return 0;\n\t\t\t\t}\n\t\t\tif (!--count)\n\t\t\t\t{\n\t\t\t\tBNerr(BN_F_BN_RAND_RANGE, BN_R_TOO_MANY_ITERATIONS);\n\t\t\t\treturn 0;\n\t\t\t\t}\n\t\t\t}\n\t\twhile (BN_cmp(r, range) >= 0);\n\t\t}\n\telse\n\t\t{\n\t\tdo\n\t\t\t{\n\t\t\tif (!bn_rand(r, n, -1, 0)) return 0;\n\t\t\tif (!--count)\n\t\t\t\t{\n\t\t\t\tBNerr(BN_F_BN_RAND_RANGE, BN_R_TOO_MANY_ITERATIONS);\n\t\t\t\treturn 0;\n\t\t\t\t}\n\t\t\t}\n\t\twhile (BN_cmp(r, range) >= 0);\n\t\t}\n\tbn_check_top(r);\n\treturn 1;\n\t}']
|
2,622
| 0
|
https://github.com/libav/libav/blob/c4bfa098072ba338d83555d6e2199f7e1e64ffff/libavformat/utils.c/#L2578
|
void avformat_close_input(AVFormatContext **ps)
{
AVFormatContext *s = *ps;
AVIOContext *pb = s->pb;
if ((s->iformat && s->iformat->flags & AVFMT_NOFILE) ||
(s->flags & AVFMT_FLAG_CUSTOM_IO))
pb = NULL;
flush_packet_queue(s);
if (s->iformat) {
if (s->iformat->read_close)
s->iformat->read_close(s);
}
avformat_free_context(s);
*ps = NULL;
avio_close(pb);
}
|
['void avformat_close_input(AVFormatContext **ps)\n{\n AVFormatContext *s = *ps;\n AVIOContext *pb = s->pb;\n if ((s->iformat && s->iformat->flags & AVFMT_NOFILE) ||\n (s->flags & AVFMT_FLAG_CUSTOM_IO))\n pb = NULL;\n flush_packet_queue(s);\n if (s->iformat) {\n if (s->iformat->read_close)\n s->iformat->read_close(s);\n }\n avformat_free_context(s);\n *ps = NULL;\n avio_close(pb);\n}', 'int avio_close(AVIOContext *s)\n{\n URLContext *h;\n if (!s)\n return 0;\n avio_flush(s);\n h = s->opaque;\n av_freep(&s->buffer);\n av_free(s);\n return ffurl_close(h);\n}', 'void avio_flush(AVIOContext *s)\n{\n flush_buffer(s);\n s->must_flush = 0;\n}', 'static void flush_buffer(AVIOContext *s)\n{\n if (s->buf_ptr > s->buffer) {\n if (s->write_packet && !s->error) {\n int ret = s->write_packet(s->opaque, s->buffer,\n s->buf_ptr - s->buffer);\n if (ret < 0) {\n s->error = ret;\n }\n }\n if (s->update_checksum) {\n s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,\n s->buf_ptr - s->checksum_ptr);\n s->checksum_ptr = s->buffer;\n }\n s->pos += s->buf_ptr - s->buffer;\n }\n s->buf_ptr = s->buffer;\n}']
|
2,623
| 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 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 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 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_MISSING_PARAMETERS);\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 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 == NULL) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if (!EC_GROUP_get_order(group, order, ctx)) {\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}', '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_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = 0;\n int top, al, bl;\n BIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n int i;\n#endif\n#ifdef BN_RECURSION\n BIGNUM *t = NULL;\n int j = 0, k;\n#endif\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(r);\n al = a->top;\n bl = b->top;\n if ((al == 0) || (bl == 0)) {\n BN_zero(r);\n return (1);\n }\n top = al + bl;\n BN_CTX_start(ctx);\n if ((r == a) || (r == b)) {\n if ((rr = BN_CTX_get(ctx)) == NULL)\n goto err;\n } else\n rr = r;\n rr->neg = a->neg ^ b->neg;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n i = al - bl;\n#endif\n#ifdef BN_MUL_COMBA\n if (i == 0) {\n# if 0\n if (al == 4) {\n if (bn_wexpand(rr, 8) == NULL)\n goto err;\n rr->top = 8;\n bn_mul_comba4(rr->d, a->d, b->d);\n goto end;\n }\n# endif\n if (al == 8) {\n if (bn_wexpand(rr, 16) == NULL)\n goto err;\n rr->top = 16;\n bn_mul_comba8(rr->d, a->d, b->d);\n goto end;\n }\n }\n#endif\n#ifdef BN_RECURSION\n if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {\n if (i >= -1 && i <= 1) {\n if (i >= 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n }\n if (i == -1) {\n j = BN_num_bits_word((BN_ULONG)bl);\n }\n j = 1 << (j - 1);\n assert(j <= al || j <= bl);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (al > j || bl > j) {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d,\n j, al - j, bl - j, t->d);\n } else {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);\n }\n rr->top = top;\n goto end;\n }\n# if 0\n if (i == 1 && !BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BIGNUM *tmp_bn = (BIGNUM *)b;\n if (bn_wexpand(tmp_bn, al) == NULL)\n goto err;\n tmp_bn->d[bl] = 0;\n bl++;\n i--;\n } else if (i == -1 && !BN_get_flags(a, BN_FLG_STATIC_DATA)) {\n BIGNUM *tmp_bn = (BIGNUM *)a;\n if (bn_wexpand(tmp_bn, bl) == NULL)\n goto err;\n tmp_bn->d[al] = 0;\n al++;\n i++;\n }\n if (i == 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (al == j) {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, al, t->d);\n } else {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d, al - j, j, t->d);\n }\n rr->top = top;\n goto end;\n }\n# endif\n }\n#endif\n if (bn_wexpand(rr, top) == NULL)\n goto err;\n rr->top = top;\n bn_mul_normal(rr->d, a->d, al, b->d, bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n end:\n#endif\n bn_correct_top(rr);\n if (r != rr)\n BN_copy(r, rr);\n ret = 1;\n err:\n bn_check_top(r);\n BN_CTX_end(ctx);\n return (ret);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
|
2,624
| 0
|
https://github.com/openssl/openssl/blob/9c46f4b9cd4912b61cb546c48b678488d7f26ed6/apps/speed.c/#L1687
|
int MAIN(int argc, char **argv)
{
unsigned char *buf_malloc = NULL, *buf2_malloc = NULL;
unsigned char *buf = NULL, *buf2 = NULL;
int mret = 1;
long count = 0, save_count = 0;
int i, j, k;
# if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA)
long rsa_count;
# endif
# ifndef OPENSSL_NO_RSA
unsigned rsa_num;
# endif
unsigned char md[EVP_MAX_MD_SIZE];
# ifndef OPENSSL_NO_MD2
unsigned char md2[MD2_DIGEST_LENGTH];
# endif
# ifndef OPENSSL_NO_MDC2
unsigned char mdc2[MDC2_DIGEST_LENGTH];
# endif
# ifndef OPENSSL_NO_MD4
unsigned char md4[MD4_DIGEST_LENGTH];
# endif
# ifndef OPENSSL_NO_MD5
unsigned char md5[MD5_DIGEST_LENGTH];
unsigned char hmac[MD5_DIGEST_LENGTH];
# endif
# ifndef OPENSSL_NO_SHA
unsigned char sha[SHA_DIGEST_LENGTH];
# ifndef OPENSSL_NO_SHA256
unsigned char sha256[SHA256_DIGEST_LENGTH];
# endif
# ifndef OPENSSL_NO_SHA512
unsigned char sha512[SHA512_DIGEST_LENGTH];
# endif
# endif
# ifndef OPENSSL_NO_WHIRLPOOL
unsigned char whirlpool[WHIRLPOOL_DIGEST_LENGTH];
# endif
# ifndef OPENSSL_NO_RMD160
unsigned char rmd160[RIPEMD160_DIGEST_LENGTH];
# endif
# ifndef OPENSSL_NO_RC4
RC4_KEY rc4_ks;
# endif
# ifndef OPENSSL_NO_RC5
RC5_32_KEY rc5_ks;
# endif
# ifndef OPENSSL_NO_RC2
RC2_KEY rc2_ks;
# endif
# ifndef OPENSSL_NO_IDEA
IDEA_KEY_SCHEDULE idea_ks;
# endif
# ifndef OPENSSL_NO_SEED
SEED_KEY_SCHEDULE seed_ks;
# endif
# ifndef OPENSSL_NO_BF
BF_KEY bf_ks;
# endif
# ifndef OPENSSL_NO_CAST
CAST_KEY cast_ks;
# endif
static const unsigned char key16[16] = {
0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12
};
# ifndef OPENSSL_NO_AES
static const unsigned char key24[24] = {
0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
};
static const unsigned char key32[32] = {
0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,
0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56
};
# endif
# ifndef OPENSSL_NO_CAMELLIA
static const unsigned char ckey24[24] = {
0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
};
static const unsigned char ckey32[32] = {
0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,
0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56
};
# endif
# ifndef OPENSSL_NO_AES
# define MAX_BLOCK_SIZE 128
# else
# define MAX_BLOCK_SIZE 64
# endif
unsigned char DES_iv[8];
unsigned char iv[2 * MAX_BLOCK_SIZE / 8];
# ifndef OPENSSL_NO_DES
static DES_cblock key =
{ 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 };
static DES_cblock key2 =
{ 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12 };
static DES_cblock key3 =
{ 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34 };
DES_key_schedule sch;
DES_key_schedule sch2;
DES_key_schedule sch3;
# endif
# ifndef OPENSSL_NO_AES
AES_KEY aes_ks1, aes_ks2, aes_ks3;
# endif
# ifndef OPENSSL_NO_CAMELLIA
CAMELLIA_KEY camellia_ks1, camellia_ks2, camellia_ks3;
# endif
# define D_MD2 0
# define D_MDC2 1
# define D_MD4 2
# define D_MD5 3
# define D_HMAC 4
# define D_SHA1 5
# define D_RMD160 6
# define D_RC4 7
# define D_CBC_DES 8
# define D_EDE3_DES 9
# define D_CBC_IDEA 10
# define D_CBC_SEED 11
# define D_CBC_RC2 12
# define D_CBC_RC5 13
# define D_CBC_BF 14
# define D_CBC_CAST 15
# define D_CBC_128_AES 16
# define D_CBC_192_AES 17
# define D_CBC_256_AES 18
# define D_CBC_128_CML 19
# define D_CBC_192_CML 20
# define D_CBC_256_CML 21
# define D_EVP 22
# define D_SHA256 23
# define D_SHA512 24
# define D_WHIRLPOOL 25
# define D_IGE_128_AES 26
# define D_IGE_192_AES 27
# define D_IGE_256_AES 28
# define D_GHASH 29
double d = 0.0;
long c[ALGOR_NUM][SIZE_NUM];
# ifndef OPENSSL_SYS_WIN32
# endif
# define R_DSA_512 0
# define R_DSA_1024 1
# define R_DSA_2048 2
# define R_RSA_512 0
# define R_RSA_1024 1
# define R_RSA_2048 2
# define R_RSA_3072 3
# define R_RSA_4096 4
# define R_RSA_7680 5
# define R_RSA_15360 6
# define R_EC_P160 0
# define R_EC_P192 1
# define R_EC_P224 2
# define R_EC_P256 3
# define R_EC_P384 4
# define R_EC_P521 5
# define R_EC_K163 6
# define R_EC_K233 7
# define R_EC_K283 8
# define R_EC_K409 9
# define R_EC_K571 10
# define R_EC_B163 11
# define R_EC_B233 12
# define R_EC_B283 13
# define R_EC_B409 14
# define R_EC_B571 15
# ifndef OPENSSL_NO_RSA
RSA *rsa_key[RSA_NUM];
long rsa_c[RSA_NUM][2];
static unsigned int rsa_bits[RSA_NUM] = {
512, 1024, 2048, 3072, 4096, 7680, 15360
};
static unsigned char *rsa_data[RSA_NUM] = {
test512, test1024, test2048, test3072, test4096, test7680, test15360
};
static int rsa_data_length[RSA_NUM] = {
sizeof(test512), sizeof(test1024),
sizeof(test2048), sizeof(test3072),
sizeof(test4096), sizeof(test7680),
sizeof(test15360)
};
# endif
# ifndef OPENSSL_NO_DSA
DSA *dsa_key[DSA_NUM];
long dsa_c[DSA_NUM][2];
static unsigned int dsa_bits[DSA_NUM] = { 512, 1024, 2048 };
# endif
# ifndef OPENSSL_NO_EC
static unsigned int test_curves[EC_NUM] = {
NID_secp160r1,
NID_X9_62_prime192v1,
NID_secp224r1,
NID_X9_62_prime256v1,
NID_secp384r1,
NID_secp521r1,
NID_sect163k1,
NID_sect233k1,
NID_sect283k1,
NID_sect409k1,
NID_sect571k1,
NID_sect163r2,
NID_sect233r1,
NID_sect283r1,
NID_sect409r1,
NID_sect571r1
};
static const char *test_curves_names[EC_NUM] = {
"secp160r1",
"nistp192",
"nistp224",
"nistp256",
"nistp384",
"nistp521",
"nistk163",
"nistk233",
"nistk283",
"nistk409",
"nistk571",
"nistb163",
"nistb233",
"nistb283",
"nistb409",
"nistb571"
};
static int test_curves_bits[EC_NUM] = {
160, 192, 224, 256, 384, 521,
163, 233, 283, 409, 571,
163, 233, 283, 409, 571
};
# endif
# ifndef OPENSSL_NO_ECDSA
unsigned char ecdsasig[256];
unsigned int ecdsasiglen;
EC_KEY *ecdsa[EC_NUM];
long ecdsa_c[EC_NUM][2];
# endif
# ifndef OPENSSL_NO_ECDH
EC_KEY *ecdh_a[EC_NUM], *ecdh_b[EC_NUM];
unsigned char secret_a[MAX_ECDH_SIZE], secret_b[MAX_ECDH_SIZE];
int secret_size_a, secret_size_b;
int ecdh_checks = 0;
int secret_idx = 0;
long ecdh_c[EC_NUM][2];
# endif
int rsa_doit[RSA_NUM];
int dsa_doit[DSA_NUM];
# ifndef OPENSSL_NO_ECDSA
int ecdsa_doit[EC_NUM];
# endif
# ifndef OPENSSL_NO_ECDH
int ecdh_doit[EC_NUM];
# endif
int doit[ALGOR_NUM];
int pr_header = 0;
const EVP_CIPHER *evp_cipher = NULL;
const EVP_MD *evp_md = NULL;
int decrypt = 0;
# ifndef NO_FORK
int multi = 0;
# endif
int multiblock = 0;
int misalign = MAX_MISALIGNMENT + 1;
# ifndef TIMES
usertime = -1;
# endif
apps_startup();
memset(results, 0, sizeof(results));
# ifndef OPENSSL_NO_DSA
memset(dsa_key, 0, sizeof(dsa_key));
# endif
# ifndef OPENSSL_NO_ECDSA
for (i = 0; i < EC_NUM; i++)
ecdsa[i] = NULL;
# endif
# ifndef OPENSSL_NO_ECDH
for (i = 0; i < EC_NUM; i++) {
ecdh_a[i] = NULL;
ecdh_b[i] = NULL;
}
# endif
if (bio_err == NULL)
if ((bio_err = BIO_new(BIO_s_file())) != NULL)
BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
if (!load_config(bio_err, NULL))
goto end;
# ifndef OPENSSL_NO_RSA
memset(rsa_key, 0, sizeof(rsa_key));
for (i = 0; i < RSA_NUM; i++)
rsa_key[i] = NULL;
# endif
if ((buf_malloc =
(unsigned char *)OPENSSL_malloc(BUFSIZE + misalign)) == NULL) {
BIO_printf(bio_err, "out of memory\n");
goto end;
}
if ((buf2_malloc =
(unsigned char *)OPENSSL_malloc(BUFSIZE + misalign)) == NULL) {
BIO_printf(bio_err, "out of memory\n");
goto end;
}
misalign = 0;
buf = buf_malloc;
buf2 = buf2_malloc;
memset(c, 0, sizeof(c));
memset(DES_iv, 0, sizeof(DES_iv));
memset(iv, 0, sizeof(iv));
for (i = 0; i < ALGOR_NUM; i++)
doit[i] = 0;
for (i = 0; i < RSA_NUM; i++)
rsa_doit[i] = 0;
for (i = 0; i < DSA_NUM; i++)
dsa_doit[i] = 0;
# ifndef OPENSSL_NO_ECDSA
for (i = 0; i < EC_NUM; i++)
ecdsa_doit[i] = 0;
# endif
# ifndef OPENSSL_NO_ECDH
for (i = 0; i < EC_NUM; i++)
ecdh_doit[i] = 0;
# endif
j = 0;
argc--;
argv++;
while (argc) {
if ((argc > 0) && (strcmp(*argv, "-elapsed") == 0)) {
usertime = 0;
j--;
} else if ((argc > 0) && (strcmp(*argv, "-evp") == 0)) {
argc--;
argv++;
if (argc == 0) {
BIO_printf(bio_err, "no EVP given\n");
goto end;
}
evp_cipher = EVP_get_cipherbyname(*argv);
if (!evp_cipher) {
evp_md = EVP_get_digestbyname(*argv);
}
if (!evp_cipher && !evp_md) {
BIO_printf(bio_err, "%s is an unknown cipher or digest\n",
*argv);
goto end;
}
doit[D_EVP] = 1;
} else if (argc > 0 && !strcmp(*argv, "-decrypt")) {
decrypt = 1;
j--;
}
# ifndef OPENSSL_NO_ENGINE
else if ((argc > 0) && (strcmp(*argv, "-engine") == 0)) {
argc--;
argv++;
if (argc == 0) {
BIO_printf(bio_err, "no engine given\n");
goto end;
}
setup_engine(bio_err, *argv, 0);
j--;
}
# endif
# ifndef NO_FORK
else if ((argc > 0) && (strcmp(*argv, "-multi") == 0)) {
argc--;
argv++;
if (argc == 0) {
BIO_printf(bio_err, "no multi count given\n");
goto end;
}
multi = atoi(argv[0]);
if (multi <= 0) {
BIO_printf(bio_err, "bad multi count\n");
goto end;
}
j--;
}
# endif
else if (argc > 0 && !strcmp(*argv, "-mr")) {
mr = 1;
j--;
} else if (argc > 0 && !strcmp(*argv, "-mb")) {
multiblock = 1;
j--;
} else if (argc > 0 && !strcmp(*argv, "-misalign")) {
argc--;
argv++;
if (argc == 0) {
BIO_printf(bio_err, "no misalignment given\n");
goto end;
}
misalign = atoi(argv[0]);
if (misalign < 0 || misalign > MAX_MISALIGNMENT) {
BIO_printf(bio_err,
"misalignment is outsize permitted range 0-%d\n",
MAX_MISALIGNMENT);
goto end;
}
buf = buf_malloc + misalign;
buf2 = buf2_malloc + misalign;
j--;
} else
# ifndef OPENSSL_NO_MD2
if (strcmp(*argv, "md2") == 0)
doit[D_MD2] = 1;
else
# endif
# ifndef OPENSSL_NO_MDC2
if (strcmp(*argv, "mdc2") == 0)
doit[D_MDC2] = 1;
else
# endif
# ifndef OPENSSL_NO_MD4
if (strcmp(*argv, "md4") == 0)
doit[D_MD4] = 1;
else
# endif
# ifndef OPENSSL_NO_MD5
if (strcmp(*argv, "md5") == 0)
doit[D_MD5] = 1;
else
# endif
# ifndef OPENSSL_NO_MD5
if (strcmp(*argv, "hmac") == 0)
doit[D_HMAC] = 1;
else
# endif
# ifndef OPENSSL_NO_SHA
if (strcmp(*argv, "sha1") == 0)
doit[D_SHA1] = 1;
else if (strcmp(*argv, "sha") == 0)
doit[D_SHA1] = 1, doit[D_SHA256] = 1, doit[D_SHA512] = 1;
else
# ifndef OPENSSL_NO_SHA256
if (strcmp(*argv, "sha256") == 0)
doit[D_SHA256] = 1;
else
# endif
# ifndef OPENSSL_NO_SHA512
if (strcmp(*argv, "sha512") == 0)
doit[D_SHA512] = 1;
else
# endif
# endif
# ifndef OPENSSL_NO_WHIRLPOOL
if (strcmp(*argv, "whirlpool") == 0)
doit[D_WHIRLPOOL] = 1;
else
# endif
# ifndef OPENSSL_NO_RMD160
if (strcmp(*argv, "ripemd") == 0)
doit[D_RMD160] = 1;
else if (strcmp(*argv, "rmd160") == 0)
doit[D_RMD160] = 1;
else if (strcmp(*argv, "ripemd160") == 0)
doit[D_RMD160] = 1;
else
# endif
# ifndef OPENSSL_NO_RC4
if (strcmp(*argv, "rc4") == 0)
doit[D_RC4] = 1;
else
# endif
# ifndef OPENSSL_NO_DES
if (strcmp(*argv, "des-cbc") == 0)
doit[D_CBC_DES] = 1;
else if (strcmp(*argv, "des-ede3") == 0)
doit[D_EDE3_DES] = 1;
else
# endif
# ifndef OPENSSL_NO_AES
if (strcmp(*argv, "aes-128-cbc") == 0)
doit[D_CBC_128_AES] = 1;
else if (strcmp(*argv, "aes-192-cbc") == 0)
doit[D_CBC_192_AES] = 1;
else if (strcmp(*argv, "aes-256-cbc") == 0)
doit[D_CBC_256_AES] = 1;
else if (strcmp(*argv, "aes-128-ige") == 0)
doit[D_IGE_128_AES] = 1;
else if (strcmp(*argv, "aes-192-ige") == 0)
doit[D_IGE_192_AES] = 1;
else if (strcmp(*argv, "aes-256-ige") == 0)
doit[D_IGE_256_AES] = 1;
else
# endif
# ifndef OPENSSL_NO_CAMELLIA
if (strcmp(*argv, "camellia-128-cbc") == 0)
doit[D_CBC_128_CML] = 1;
else if (strcmp(*argv, "camellia-192-cbc") == 0)
doit[D_CBC_192_CML] = 1;
else if (strcmp(*argv, "camellia-256-cbc") == 0)
doit[D_CBC_256_CML] = 1;
else
# endif
# ifndef OPENSSL_NO_RSA
# if 0
if (strcmp(*argv, "rsaref") == 0) {
RSA_set_default_openssl_method(RSA_PKCS1_RSAref());
j--;
} else
# endif
# ifndef RSA_NULL
if (strcmp(*argv, "openssl") == 0) {
RSA_set_default_method(RSA_PKCS1_SSLeay());
j--;
} else
# endif
# endif
if (strcmp(*argv, "dsa512") == 0)
dsa_doit[R_DSA_512] = 2;
else if (strcmp(*argv, "dsa1024") == 0)
dsa_doit[R_DSA_1024] = 2;
else if (strcmp(*argv, "dsa2048") == 0)
dsa_doit[R_DSA_2048] = 2;
else if (strcmp(*argv, "rsa512") == 0)
rsa_doit[R_RSA_512] = 2;
else if (strcmp(*argv, "rsa1024") == 0)
rsa_doit[R_RSA_1024] = 2;
else if (strcmp(*argv, "rsa2048") == 0)
rsa_doit[R_RSA_2048] = 2;
else if (strcmp(*argv, "rsa3072") == 0)
rsa_doit[R_RSA_3072] = 2;
else if (strcmp(*argv, "rsa4096") == 0)
rsa_doit[R_RSA_4096] = 2;
else if (strcmp(*argv, "rsa7680") == 0)
rsa_doit[R_RSA_7680] = 2;
else if (strcmp(*argv, "rsa15360") == 0)
rsa_doit[R_RSA_15360] = 2;
else
# ifndef OPENSSL_NO_RC2
if (strcmp(*argv, "rc2-cbc") == 0)
doit[D_CBC_RC2] = 1;
else if (strcmp(*argv, "rc2") == 0)
doit[D_CBC_RC2] = 1;
else
# endif
# ifndef OPENSSL_NO_RC5
if (strcmp(*argv, "rc5-cbc") == 0)
doit[D_CBC_RC5] = 1;
else if (strcmp(*argv, "rc5") == 0)
doit[D_CBC_RC5] = 1;
else
# endif
# ifndef OPENSSL_NO_IDEA
if (strcmp(*argv, "idea-cbc") == 0)
doit[D_CBC_IDEA] = 1;
else if (strcmp(*argv, "idea") == 0)
doit[D_CBC_IDEA] = 1;
else
# endif
# ifndef OPENSSL_NO_SEED
if (strcmp(*argv, "seed-cbc") == 0)
doit[D_CBC_SEED] = 1;
else if (strcmp(*argv, "seed") == 0)
doit[D_CBC_SEED] = 1;
else
# endif
# ifndef OPENSSL_NO_BF
if (strcmp(*argv, "bf-cbc") == 0)
doit[D_CBC_BF] = 1;
else if (strcmp(*argv, "blowfish") == 0)
doit[D_CBC_BF] = 1;
else if (strcmp(*argv, "bf") == 0)
doit[D_CBC_BF] = 1;
else
# endif
# ifndef OPENSSL_NO_CAST
if (strcmp(*argv, "cast-cbc") == 0)
doit[D_CBC_CAST] = 1;
else if (strcmp(*argv, "cast") == 0)
doit[D_CBC_CAST] = 1;
else if (strcmp(*argv, "cast5") == 0)
doit[D_CBC_CAST] = 1;
else
# endif
# ifndef OPENSSL_NO_DES
if (strcmp(*argv, "des") == 0) {
doit[D_CBC_DES] = 1;
doit[D_EDE3_DES] = 1;
} else
# endif
# ifndef OPENSSL_NO_AES
if (strcmp(*argv, "aes") == 0) {
doit[D_CBC_128_AES] = 1;
doit[D_CBC_192_AES] = 1;
doit[D_CBC_256_AES] = 1;
} else if (strcmp(*argv, "ghash") == 0) {
doit[D_GHASH] = 1;
} else
# endif
# ifndef OPENSSL_NO_CAMELLIA
if (strcmp(*argv, "camellia") == 0) {
doit[D_CBC_128_CML] = 1;
doit[D_CBC_192_CML] = 1;
doit[D_CBC_256_CML] = 1;
} else
# endif
# ifndef OPENSSL_NO_RSA
if (strcmp(*argv, "rsa") == 0) {
rsa_doit[R_RSA_512] = 1;
rsa_doit[R_RSA_1024] = 1;
rsa_doit[R_RSA_2048] = 1;
rsa_doit[R_RSA_3072] = 1;
rsa_doit[R_RSA_4096] = 1;
rsa_doit[R_RSA_7680] = 1;
rsa_doit[R_RSA_15360] = 1;
} else
# endif
# ifndef OPENSSL_NO_DSA
if (strcmp(*argv, "dsa") == 0) {
dsa_doit[R_DSA_512] = 1;
dsa_doit[R_DSA_1024] = 1;
dsa_doit[R_DSA_2048] = 1;
} else
# endif
# ifndef OPENSSL_NO_ECDSA
if (strcmp(*argv, "ecdsap160") == 0)
ecdsa_doit[R_EC_P160] = 2;
else if (strcmp(*argv, "ecdsap192") == 0)
ecdsa_doit[R_EC_P192] = 2;
else if (strcmp(*argv, "ecdsap224") == 0)
ecdsa_doit[R_EC_P224] = 2;
else if (strcmp(*argv, "ecdsap256") == 0)
ecdsa_doit[R_EC_P256] = 2;
else if (strcmp(*argv, "ecdsap384") == 0)
ecdsa_doit[R_EC_P384] = 2;
else if (strcmp(*argv, "ecdsap521") == 0)
ecdsa_doit[R_EC_P521] = 2;
else if (strcmp(*argv, "ecdsak163") == 0)
ecdsa_doit[R_EC_K163] = 2;
else if (strcmp(*argv, "ecdsak233") == 0)
ecdsa_doit[R_EC_K233] = 2;
else if (strcmp(*argv, "ecdsak283") == 0)
ecdsa_doit[R_EC_K283] = 2;
else if (strcmp(*argv, "ecdsak409") == 0)
ecdsa_doit[R_EC_K409] = 2;
else if (strcmp(*argv, "ecdsak571") == 0)
ecdsa_doit[R_EC_K571] = 2;
else if (strcmp(*argv, "ecdsab163") == 0)
ecdsa_doit[R_EC_B163] = 2;
else if (strcmp(*argv, "ecdsab233") == 0)
ecdsa_doit[R_EC_B233] = 2;
else if (strcmp(*argv, "ecdsab283") == 0)
ecdsa_doit[R_EC_B283] = 2;
else if (strcmp(*argv, "ecdsab409") == 0)
ecdsa_doit[R_EC_B409] = 2;
else if (strcmp(*argv, "ecdsab571") == 0)
ecdsa_doit[R_EC_B571] = 2;
else if (strcmp(*argv, "ecdsa") == 0) {
for (i = 0; i < EC_NUM; i++)
ecdsa_doit[i] = 1;
} else
# endif
# ifndef OPENSSL_NO_ECDH
if (strcmp(*argv, "ecdhp160") == 0)
ecdh_doit[R_EC_P160] = 2;
else if (strcmp(*argv, "ecdhp192") == 0)
ecdh_doit[R_EC_P192] = 2;
else if (strcmp(*argv, "ecdhp224") == 0)
ecdh_doit[R_EC_P224] = 2;
else if (strcmp(*argv, "ecdhp256") == 0)
ecdh_doit[R_EC_P256] = 2;
else if (strcmp(*argv, "ecdhp384") == 0)
ecdh_doit[R_EC_P384] = 2;
else if (strcmp(*argv, "ecdhp521") == 0)
ecdh_doit[R_EC_P521] = 2;
else if (strcmp(*argv, "ecdhk163") == 0)
ecdh_doit[R_EC_K163] = 2;
else if (strcmp(*argv, "ecdhk233") == 0)
ecdh_doit[R_EC_K233] = 2;
else if (strcmp(*argv, "ecdhk283") == 0)
ecdh_doit[R_EC_K283] = 2;
else if (strcmp(*argv, "ecdhk409") == 0)
ecdh_doit[R_EC_K409] = 2;
else if (strcmp(*argv, "ecdhk571") == 0)
ecdh_doit[R_EC_K571] = 2;
else if (strcmp(*argv, "ecdhb163") == 0)
ecdh_doit[R_EC_B163] = 2;
else if (strcmp(*argv, "ecdhb233") == 0)
ecdh_doit[R_EC_B233] = 2;
else if (strcmp(*argv, "ecdhb283") == 0)
ecdh_doit[R_EC_B283] = 2;
else if (strcmp(*argv, "ecdhb409") == 0)
ecdh_doit[R_EC_B409] = 2;
else if (strcmp(*argv, "ecdhb571") == 0)
ecdh_doit[R_EC_B571] = 2;
else if (strcmp(*argv, "ecdh") == 0) {
for (i = 0; i < EC_NUM; i++)
ecdh_doit[i] = 1;
} else
# endif
{
BIO_printf(bio_err, "Error: bad option or value\n");
BIO_printf(bio_err, "\n");
BIO_printf(bio_err, "Available values:\n");
# ifndef OPENSSL_NO_MD2
BIO_printf(bio_err, "md2 ");
# endif
# ifndef OPENSSL_NO_MDC2
BIO_printf(bio_err, "mdc2 ");
# endif
# ifndef OPENSSL_NO_MD4
BIO_printf(bio_err, "md4 ");
# endif
# ifndef OPENSSL_NO_MD5
BIO_printf(bio_err, "md5 ");
# ifndef OPENSSL_NO_HMAC
BIO_printf(bio_err, "hmac ");
# endif
# endif
# ifndef OPENSSL_NO_SHA1
BIO_printf(bio_err, "sha1 ");
# endif
# ifndef OPENSSL_NO_SHA256
BIO_printf(bio_err, "sha256 ");
# endif
# ifndef OPENSSL_NO_SHA512
BIO_printf(bio_err, "sha512 ");
# endif
# ifndef OPENSSL_NO_WHIRLPOOL
BIO_printf(bio_err, "whirlpool");
# endif
# ifndef OPENSSL_NO_RMD160
BIO_printf(bio_err, "rmd160");
# endif
# if !defined(OPENSSL_NO_MD2) || !defined(OPENSSL_NO_MDC2) || \
!defined(OPENSSL_NO_MD4) || !defined(OPENSSL_NO_MD5) || \
!defined(OPENSSL_NO_SHA1) || !defined(OPENSSL_NO_RMD160) || \
!defined(OPENSSL_NO_WHIRLPOOL)
BIO_printf(bio_err, "\n");
# endif
# ifndef OPENSSL_NO_IDEA
BIO_printf(bio_err, "idea-cbc ");
# endif
# ifndef OPENSSL_NO_SEED
BIO_printf(bio_err, "seed-cbc ");
# endif
# ifndef OPENSSL_NO_RC2
BIO_printf(bio_err, "rc2-cbc ");
# endif
# ifndef OPENSSL_NO_RC5
BIO_printf(bio_err, "rc5-cbc ");
# endif
# ifndef OPENSSL_NO_BF
BIO_printf(bio_err, "bf-cbc");
# endif
# if !defined(OPENSSL_NO_IDEA) || !defined(OPENSSL_NO_SEED) || !defined(OPENSSL_NO_RC2) || \
!defined(OPENSSL_NO_BF) || !defined(OPENSSL_NO_RC5)
BIO_printf(bio_err, "\n");
# endif
# ifndef OPENSSL_NO_DES
BIO_printf(bio_err, "des-cbc des-ede3 ");
# endif
# ifndef OPENSSL_NO_AES
BIO_printf(bio_err, "aes-128-cbc aes-192-cbc aes-256-cbc ");
BIO_printf(bio_err, "aes-128-ige aes-192-ige aes-256-ige ");
# endif
# ifndef OPENSSL_NO_CAMELLIA
BIO_printf(bio_err, "\n");
BIO_printf(bio_err,
"camellia-128-cbc camellia-192-cbc camellia-256-cbc ");
# endif
# ifndef OPENSSL_NO_RC4
BIO_printf(bio_err, "rc4");
# endif
BIO_printf(bio_err, "\n");
# ifndef OPENSSL_NO_RSA
BIO_printf(bio_err,
"rsa512 rsa1024 rsa2048 rsa3072 rsa4096\n");
BIO_printf(bio_err, "rsa7680 rsa15360\n");
# endif
# ifndef OPENSSL_NO_DSA
BIO_printf(bio_err, "dsa512 dsa1024 dsa2048\n");
# endif
# ifndef OPENSSL_NO_ECDSA
BIO_printf(bio_err, "ecdsap160 ecdsap192 ecdsap224 "
"ecdsap256 ecdsap384 ecdsap521\n");
BIO_printf(bio_err,
"ecdsak163 ecdsak233 ecdsak283 ecdsak409 ecdsak571\n");
BIO_printf(bio_err,
"ecdsab163 ecdsab233 ecdsab283 ecdsab409 ecdsab571\n");
BIO_printf(bio_err, "ecdsa\n");
# endif
# ifndef OPENSSL_NO_ECDH
BIO_printf(bio_err, "ecdhp160 ecdhp192 ecdhp224 "
"ecdhp256 ecdhp384 ecdhp521\n");
BIO_printf(bio_err,
"ecdhk163 ecdhk233 ecdhk283 ecdhk409 ecdhk571\n");
BIO_printf(bio_err,
"ecdhb163 ecdhb233 ecdhb283 ecdhb409 ecdhb571\n");
BIO_printf(bio_err, "ecdh\n");
# endif
# ifndef OPENSSL_NO_IDEA
BIO_printf(bio_err, "idea ");
# endif
# ifndef OPENSSL_NO_SEED
BIO_printf(bio_err, "seed ");
# endif
# ifndef OPENSSL_NO_RC2
BIO_printf(bio_err, "rc2 ");
# endif
# ifndef OPENSSL_NO_DES
BIO_printf(bio_err, "des ");
# endif
# ifndef OPENSSL_NO_AES
BIO_printf(bio_err, "aes ");
# endif
# ifndef OPENSSL_NO_CAMELLIA
BIO_printf(bio_err, "camellia ");
# endif
# ifndef OPENSSL_NO_RSA
BIO_printf(bio_err, "rsa ");
# endif
# ifndef OPENSSL_NO_BF
BIO_printf(bio_err, "blowfish");
# endif
# if !defined(OPENSSL_NO_IDEA) || !defined(OPENSSL_NO_SEED) || \
!defined(OPENSSL_NO_RC2) || !defined(OPENSSL_NO_DES) || \
!defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_BF) || \
!defined(OPENSSL_NO_AES) || !defined(OPENSSL_NO_CAMELLIA)
BIO_printf(bio_err, "\n");
# endif
BIO_printf(bio_err, "\n");
BIO_printf(bio_err, "Available options:\n");
# if defined(TIMES) || defined(USE_TOD)
BIO_printf(bio_err, "-elapsed "
"measure time in real time instead of CPU user time.\n");
# endif
# ifndef OPENSSL_NO_ENGINE
BIO_printf(bio_err,
"-engine e "
"use engine e, possibly a hardware device.\n");
# endif
BIO_printf(bio_err, "-evp e " "use EVP e.\n");
BIO_printf(bio_err,
"-decrypt "
"time decryption instead of encryption (only EVP).\n");
BIO_printf(bio_err,
"-mr "
"produce machine readable output.\n");
BIO_printf(bio_err,
"-mb "
"perform multi-block benchmark (for specific ciphers)\n");
BIO_printf(bio_err,
"-misalign n "
"perform benchmark with misaligned data\n");
# ifndef NO_FORK
BIO_printf(bio_err,
"-multi n " "run n benchmarks in parallel.\n");
# endif
goto end;
}
argc--;
argv++;
j++;
}
# ifndef NO_FORK
if (multi && do_multi(multi))
goto show_res;
# endif
if (j == 0) {
for (i = 0; i < ALGOR_NUM; i++) {
if (i != D_EVP)
doit[i] = 1;
}
for (i = 0; i < RSA_NUM; i++)
rsa_doit[i] = 1;
for (i = 0; i < DSA_NUM; i++)
dsa_doit[i] = 1;
# ifndef OPENSSL_NO_ECDSA
for (i = 0; i < EC_NUM; i++)
ecdsa_doit[i] = 1;
# endif
# ifndef OPENSSL_NO_ECDH
for (i = 0; i < EC_NUM; i++)
ecdh_doit[i] = 1;
# endif
}
for (i = 0; i < ALGOR_NUM; i++)
if (doit[i])
pr_header++;
if (usertime == 0 && !mr)
BIO_printf(bio_err,
"You have chosen to measure elapsed time "
"instead of user CPU time.\n");
# ifndef OPENSSL_NO_RSA
for (i = 0; i < RSA_NUM; i++) {
const unsigned char *p;
p = rsa_data[i];
rsa_key[i] = d2i_RSAPrivateKey(NULL, &p, rsa_data_length[i]);
if (rsa_key[i] == NULL) {
BIO_printf(bio_err, "internal error loading RSA key number %d\n",
i);
goto end;
}
# if 0
else {
BIO_printf(bio_err,
mr ? "+RK:%d:"
: "Loaded RSA key, %d bit modulus and e= 0x",
BN_num_bits(rsa_key[i]->n));
BN_print(bio_err, rsa_key[i]->e);
BIO_printf(bio_err, "\n");
}
# endif
}
# endif
# ifndef OPENSSL_NO_DSA
dsa_key[0] = get_dsa512();
dsa_key[1] = get_dsa1024();
dsa_key[2] = get_dsa2048();
# endif
# ifndef OPENSSL_NO_DES
DES_set_key_unchecked(&key, &sch);
DES_set_key_unchecked(&key2, &sch2);
DES_set_key_unchecked(&key3, &sch3);
# endif
# ifndef OPENSSL_NO_AES
AES_set_encrypt_key(key16, 128, &aes_ks1);
AES_set_encrypt_key(key24, 192, &aes_ks2);
AES_set_encrypt_key(key32, 256, &aes_ks3);
# endif
# ifndef OPENSSL_NO_CAMELLIA
Camellia_set_key(key16, 128, &camellia_ks1);
Camellia_set_key(ckey24, 192, &camellia_ks2);
Camellia_set_key(ckey32, 256, &camellia_ks3);
# endif
# ifndef OPENSSL_NO_IDEA
idea_set_encrypt_key(key16, &idea_ks);
# endif
# ifndef OPENSSL_NO_SEED
SEED_set_key(key16, &seed_ks);
# endif
# ifndef OPENSSL_NO_RC4
RC4_set_key(&rc4_ks, 16, key16);
# endif
# ifndef OPENSSL_NO_RC2
RC2_set_key(&rc2_ks, 16, key16, 128);
# endif
# ifndef OPENSSL_NO_RC5
RC5_32_set_key(&rc5_ks, 16, key16, 12);
# endif
# ifndef OPENSSL_NO_BF
BF_set_key(&bf_ks, 16, key16);
# endif
# ifndef OPENSSL_NO_CAST
CAST_set_key(&cast_ks, 16, key16);
# endif
# ifndef OPENSSL_NO_RSA
memset(rsa_c, 0, sizeof(rsa_c));
# endif
# ifndef SIGALRM
# ifndef OPENSSL_NO_DES
BIO_printf(bio_err, "First we calculate the approximate speed ...\n");
count = 10;
do {
long it;
count *= 2;
Time_F(START);
for (it = count; it; it--)
DES_ecb_encrypt((DES_cblock *)buf,
(DES_cblock *)buf, &sch, DES_ENCRYPT);
d = Time_F(STOP);
} while (d < 3);
save_count = count;
c[D_MD2][0] = count / 10;
c[D_MDC2][0] = count / 10;
c[D_MD4][0] = count;
c[D_MD5][0] = count;
c[D_HMAC][0] = count;
c[D_SHA1][0] = count;
c[D_RMD160][0] = count;
c[D_RC4][0] = count * 5;
c[D_CBC_DES][0] = count;
c[D_EDE3_DES][0] = count / 3;
c[D_CBC_IDEA][0] = count;
c[D_CBC_SEED][0] = count;
c[D_CBC_RC2][0] = count;
c[D_CBC_RC5][0] = count;
c[D_CBC_BF][0] = count;
c[D_CBC_CAST][0] = count;
c[D_CBC_128_AES][0] = count;
c[D_CBC_192_AES][0] = count;
c[D_CBC_256_AES][0] = count;
c[D_CBC_128_CML][0] = count;
c[D_CBC_192_CML][0] = count;
c[D_CBC_256_CML][0] = count;
c[D_SHA256][0] = count;
c[D_SHA512][0] = count;
c[D_WHIRLPOOL][0] = count;
c[D_IGE_128_AES][0] = count;
c[D_IGE_192_AES][0] = count;
c[D_IGE_256_AES][0] = count;
c[D_GHASH][0] = count;
for (i = 1; i < SIZE_NUM; i++) {
long l0, l1;
l0 = (long)lengths[0];
l1 = (long)lengths[i];
c[D_MD2][i] = c[D_MD2][0] * 4 * l0 / l1;
c[D_MDC2][i] = c[D_MDC2][0] * 4 * l0 / l1;
c[D_MD4][i] = c[D_MD4][0] * 4 * l0 / l1;
c[D_MD5][i] = c[D_MD5][0] * 4 * l0 / l1;
c[D_HMAC][i] = c[D_HMAC][0] * 4 * l0 / l1;
c[D_SHA1][i] = c[D_SHA1][0] * 4 * l0 / l1;
c[D_RMD160][i] = c[D_RMD160][0] * 4 * l0 / l1;
c[D_SHA256][i] = c[D_SHA256][0] * 4 * l0 / l1;
c[D_SHA512][i] = c[D_SHA512][0] * 4 * l0 / l1;
c[D_WHIRLPOOL][i] = c[D_WHIRLPOOL][0] * 4 * l0 / l1;
l0 = (long)lengths[i - 1];
c[D_RC4][i] = c[D_RC4][i - 1] * l0 / l1;
c[D_CBC_DES][i] = c[D_CBC_DES][i - 1] * l0 / l1;
c[D_EDE3_DES][i] = c[D_EDE3_DES][i - 1] * l0 / l1;
c[D_CBC_IDEA][i] = c[D_CBC_IDEA][i - 1] * l0 / l1;
c[D_CBC_SEED][i] = c[D_CBC_SEED][i - 1] * l0 / l1;
c[D_CBC_RC2][i] = c[D_CBC_RC2][i - 1] * l0 / l1;
c[D_CBC_RC5][i] = c[D_CBC_RC5][i - 1] * l0 / l1;
c[D_CBC_BF][i] = c[D_CBC_BF][i - 1] * l0 / l1;
c[D_CBC_CAST][i] = c[D_CBC_CAST][i - 1] * l0 / l1;
c[D_CBC_128_AES][i] = c[D_CBC_128_AES][i - 1] * l0 / l1;
c[D_CBC_192_AES][i] = c[D_CBC_192_AES][i - 1] * l0 / l1;
c[D_CBC_256_AES][i] = c[D_CBC_256_AES][i - 1] * l0 / l1;
c[D_CBC_128_CML][i] = c[D_CBC_128_CML][i - 1] * l0 / l1;
c[D_CBC_192_CML][i] = c[D_CBC_192_CML][i - 1] * l0 / l1;
c[D_CBC_256_CML][i] = c[D_CBC_256_CML][i - 1] * l0 / l1;
c[D_IGE_128_AES][i] = c[D_IGE_128_AES][i - 1] * l0 / l1;
c[D_IGE_192_AES][i] = c[D_IGE_192_AES][i - 1] * l0 / l1;
c[D_IGE_256_AES][i] = c[D_IGE_256_AES][i - 1] * l0 / l1;
}
# ifndef OPENSSL_NO_RSA
rsa_c[R_RSA_512][0] = count / 2000;
rsa_c[R_RSA_512][1] = count / 400;
for (i = 1; i < RSA_NUM; i++) {
rsa_c[i][0] = rsa_c[i - 1][0] / 8;
rsa_c[i][1] = rsa_c[i - 1][1] / 4;
if ((rsa_doit[i] <= 1) && (rsa_c[i][0] == 0))
rsa_doit[i] = 0;
else {
if (rsa_c[i][0] == 0) {
rsa_c[i][0] = 1;
rsa_c[i][1] = 20;
}
}
}
# endif
# ifndef OPENSSL_NO_DSA
dsa_c[R_DSA_512][0] = count / 1000;
dsa_c[R_DSA_512][1] = count / 1000 / 2;
for (i = 1; i < DSA_NUM; i++) {
dsa_c[i][0] = dsa_c[i - 1][0] / 4;
dsa_c[i][1] = dsa_c[i - 1][1] / 4;
if ((dsa_doit[i] <= 1) && (dsa_c[i][0] == 0))
dsa_doit[i] = 0;
else {
if (dsa_c[i] == 0) {
dsa_c[i][0] = 1;
dsa_c[i][1] = 1;
}
}
}
# endif
# ifndef OPENSSL_NO_ECDSA
ecdsa_c[R_EC_P160][0] = count / 1000;
ecdsa_c[R_EC_P160][1] = count / 1000 / 2;
for (i = R_EC_P192; i <= R_EC_P521; i++) {
ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
if ((ecdsa_doit[i] <= 1) && (ecdsa_c[i][0] == 0))
ecdsa_doit[i] = 0;
else {
if (ecdsa_c[i] == 0) {
ecdsa_c[i][0] = 1;
ecdsa_c[i][1] = 1;
}
}
}
ecdsa_c[R_EC_K163][0] = count / 1000;
ecdsa_c[R_EC_K163][1] = count / 1000 / 2;
for (i = R_EC_K233; i <= R_EC_K571; i++) {
ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
if ((ecdsa_doit[i] <= 1) && (ecdsa_c[i][0] == 0))
ecdsa_doit[i] = 0;
else {
if (ecdsa_c[i] == 0) {
ecdsa_c[i][0] = 1;
ecdsa_c[i][1] = 1;
}
}
}
ecdsa_c[R_EC_B163][0] = count / 1000;
ecdsa_c[R_EC_B163][1] = count / 1000 / 2;
for (i = R_EC_B233; i <= R_EC_B571; i++) {
ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
if ((ecdsa_doit[i] <= 1) && (ecdsa_c[i][0] == 0))
ecdsa_doit[i] = 0;
else {
if (ecdsa_c[i] == 0) {
ecdsa_c[i][0] = 1;
ecdsa_c[i][1] = 1;
}
}
}
# endif
# ifndef OPENSSL_NO_ECDH
ecdh_c[R_EC_P160][0] = count / 1000;
ecdh_c[R_EC_P160][1] = count / 1000;
for (i = R_EC_P192; i <= R_EC_P521; i++) {
ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
ecdh_c[i][1] = ecdh_c[i - 1][1] / 2;
if ((ecdh_doit[i] <= 1) && (ecdh_c[i][0] == 0))
ecdh_doit[i] = 0;
else {
if (ecdh_c[i] == 0) {
ecdh_c[i][0] = 1;
ecdh_c[i][1] = 1;
}
}
}
ecdh_c[R_EC_K163][0] = count / 1000;
ecdh_c[R_EC_K163][1] = count / 1000;
for (i = R_EC_K233; i <= R_EC_K571; i++) {
ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
ecdh_c[i][1] = ecdh_c[i - 1][1] / 2;
if ((ecdh_doit[i] <= 1) && (ecdh_c[i][0] == 0))
ecdh_doit[i] = 0;
else {
if (ecdh_c[i] == 0) {
ecdh_c[i][0] = 1;
ecdh_c[i][1] = 1;
}
}
}
ecdh_c[R_EC_B163][0] = count / 1000;
ecdh_c[R_EC_B163][1] = count / 1000;
for (i = R_EC_B233; i <= R_EC_B571; i++) {
ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
ecdh_c[i][1] = ecdh_c[i - 1][1] / 2;
if ((ecdh_doit[i] <= 1) && (ecdh_c[i][0] == 0))
ecdh_doit[i] = 0;
else {
if (ecdh_c[i] == 0) {
ecdh_c[i][0] = 1;
ecdh_c[i][1] = 1;
}
}
}
# endif
# define COND(d) (count < (d))
# define COUNT(d) (d)
# else
# error "You cannot disable DES on systems without SIGALRM."
# endif
# else
# define COND(c) (run && count<0x7fffffff)
# define COUNT(d) (count)
# ifndef _WIN32
signal(SIGALRM, sig_done);
# endif
# endif
# ifndef OPENSSL_NO_MD2
if (doit[D_MD2]) {
for (j = 0; j < SIZE_NUM; j++) {
print_message(names[D_MD2], c[D_MD2][j], lengths[j]);
Time_F(START);
for (count = 0, run = 1; COND(c[D_MD2][j]); count++)
EVP_Digest(buf, (unsigned long)lengths[j], &(md2[0]), NULL,
EVP_md2(), NULL);
d = Time_F(STOP);
print_result(D_MD2, j, count, d);
}
}
# endif
# ifndef OPENSSL_NO_MDC2
if (doit[D_MDC2]) {
for (j = 0; j < SIZE_NUM; j++) {
print_message(names[D_MDC2], c[D_MDC2][j], lengths[j]);
Time_F(START);
for (count = 0, run = 1; COND(c[D_MDC2][j]); count++)
EVP_Digest(buf, (unsigned long)lengths[j], &(mdc2[0]), NULL,
EVP_mdc2(), NULL);
d = Time_F(STOP);
print_result(D_MDC2, j, count, d);
}
}
# endif
# ifndef OPENSSL_NO_MD4
if (doit[D_MD4]) {
for (j = 0; j < SIZE_NUM; j++) {
print_message(names[D_MD4], c[D_MD4][j], lengths[j]);
Time_F(START);
for (count = 0, run = 1; COND(c[D_MD4][j]); count++)
EVP_Digest(&(buf[0]), (unsigned long)lengths[j], &(md4[0]),
NULL, EVP_md4(), NULL);
d = Time_F(STOP);
print_result(D_MD4, j, count, d);
}
}
# endif
# ifndef OPENSSL_NO_MD5
if (doit[D_MD5]) {
for (j = 0; j < SIZE_NUM; j++) {
print_message(names[D_MD5], c[D_MD5][j], lengths[j]);
Time_F(START);
for (count = 0, run = 1; COND(c[D_MD5][j]); count++)
MD5(buf, lengths[j], md5);
d = Time_F(STOP);
print_result(D_MD5, j, count, d);
}
}
# endif
# if !defined(OPENSSL_NO_MD5) && !defined(OPENSSL_NO_HMAC)
if (doit[D_HMAC]) {
HMAC_CTX hctx;
HMAC_CTX_init(&hctx);
HMAC_Init_ex(&hctx, (unsigned char *)"This is a key...",
16, EVP_md5(), NULL);
for (j = 0; j < SIZE_NUM; j++) {
print_message(names[D_HMAC], c[D_HMAC][j], lengths[j]);
Time_F(START);
for (count = 0, run = 1; COND(c[D_HMAC][j]); count++) {
HMAC_Init_ex(&hctx, NULL, 0, NULL, NULL);
HMAC_Update(&hctx, buf, lengths[j]);
HMAC_Final(&hctx, &(hmac[0]), NULL);
}
d = Time_F(STOP);
print_result(D_HMAC, j, count, d);
}
HMAC_CTX_cleanup(&hctx);
}
# endif
# ifndef OPENSSL_NO_SHA
if (doit[D_SHA1]) {
for (j = 0; j < SIZE_NUM; j++) {
print_message(names[D_SHA1], c[D_SHA1][j], lengths[j]);
Time_F(START);
for (count = 0, run = 1; COND(c[D_SHA1][j]); count++)
# if 0
EVP_Digest(buf, (unsigned long)lengths[j], &(sha[0]), NULL,
EVP_sha1(), NULL);
# else
SHA1(buf, lengths[j], sha);
# endif
d = Time_F(STOP);
print_result(D_SHA1, j, count, d);
}
}
# ifndef OPENSSL_NO_SHA256
if (doit[D_SHA256]) {
for (j = 0; j < SIZE_NUM; j++) {
print_message(names[D_SHA256], c[D_SHA256][j], lengths[j]);
Time_F(START);
for (count = 0, run = 1; COND(c[D_SHA256][j]); count++)
SHA256(buf, lengths[j], sha256);
d = Time_F(STOP);
print_result(D_SHA256, j, count, d);
}
}
# endif
# ifndef OPENSSL_NO_SHA512
if (doit[D_SHA512]) {
for (j = 0; j < SIZE_NUM; j++) {
print_message(names[D_SHA512], c[D_SHA512][j], lengths[j]);
Time_F(START);
for (count = 0, run = 1; COND(c[D_SHA512][j]); count++)
SHA512(buf, lengths[j], sha512);
d = Time_F(STOP);
print_result(D_SHA512, j, count, d);
}
}
# endif
# endif
# ifndef OPENSSL_NO_WHIRLPOOL
if (doit[D_WHIRLPOOL]) {
for (j = 0; j < SIZE_NUM; j++) {
print_message(names[D_WHIRLPOOL], c[D_WHIRLPOOL][j], lengths[j]);
Time_F(START);
for (count = 0, run = 1; COND(c[D_WHIRLPOOL][j]); count++)
WHIRLPOOL(buf, lengths[j], whirlpool);
d = Time_F(STOP);
print_result(D_WHIRLPOOL, j, count, d);
}
}
# endif
# ifndef OPENSSL_NO_RMD160
if (doit[D_RMD160]) {
for (j = 0; j < SIZE_NUM; j++) {
print_message(names[D_RMD160], c[D_RMD160][j], lengths[j]);
Time_F(START);
for (count = 0, run = 1; COND(c[D_RMD160][j]); count++)
EVP_Digest(buf, (unsigned long)lengths[j], &(rmd160[0]), NULL,
EVP_ripemd160(), NULL);
d = Time_F(STOP);
print_result(D_RMD160, j, count, d);
}
}
# endif
# ifndef OPENSSL_NO_RC4
if (doit[D_RC4]) {
for (j = 0; j < SIZE_NUM; j++) {
print_message(names[D_RC4], c[D_RC4][j], lengths[j]);
Time_F(START);
for (count = 0, run = 1; COND(c[D_RC4][j]); count++)
RC4(&rc4_ks, (unsigned int)lengths[j], buf, buf);
d = Time_F(STOP);
print_result(D_RC4, j, count, d);
}
}
# endif
# ifndef OPENSSL_NO_DES
if (doit[D_CBC_DES]) {
for (j = 0; j < SIZE_NUM; j++) {
print_message(names[D_CBC_DES], c[D_CBC_DES][j], lengths[j]);
Time_F(START);
for (count = 0, run = 1; COND(c[D_CBC_DES][j]); count++)
DES_ncbc_encrypt(buf, buf, lengths[j], &sch,
&DES_iv, DES_ENCRYPT);
d = Time_F(STOP);
print_result(D_CBC_DES, j, count, d);
}
}
if (doit[D_EDE3_DES]) {
for (j = 0; j < SIZE_NUM; j++) {
print_message(names[D_EDE3_DES], c[D_EDE3_DES][j], lengths[j]);
Time_F(START);
for (count = 0, run = 1; COND(c[D_EDE3_DES][j]); count++)
DES_ede3_cbc_encrypt(buf, buf, lengths[j],
&sch, &sch2, &sch3,
&DES_iv, DES_ENCRYPT);
d = Time_F(STOP);
print_result(D_EDE3_DES, j, count, d);
}
}
# endif
# ifndef OPENSSL_NO_AES
if (doit[D_CBC_128_AES]) {
for (j = 0; j < SIZE_NUM; j++) {
print_message(names[D_CBC_128_AES], c[D_CBC_128_AES][j],
lengths[j]);
Time_F(START);
for (count = 0, run = 1; COND(c[D_CBC_128_AES][j]); count++)
AES_cbc_encrypt(buf, buf,
(unsigned long)lengths[j], &aes_ks1,
iv, AES_ENCRYPT);
d = Time_F(STOP);
print_result(D_CBC_128_AES, j, count, d);
}
}
if (doit[D_CBC_192_AES]) {
for (j = 0; j < SIZE_NUM; j++) {
print_message(names[D_CBC_192_AES], c[D_CBC_192_AES][j],
lengths[j]);
Time_F(START);
for (count = 0, run = 1; COND(c[D_CBC_192_AES][j]); count++)
AES_cbc_encrypt(buf, buf,
(unsigned long)lengths[j], &aes_ks2,
iv, AES_ENCRYPT);
d = Time_F(STOP);
print_result(D_CBC_192_AES, j, count, d);
}
}
if (doit[D_CBC_256_AES]) {
for (j = 0; j < SIZE_NUM; j++) {
print_message(names[D_CBC_256_AES], c[D_CBC_256_AES][j],
lengths[j]);
Time_F(START);
for (count = 0, run = 1; COND(c[D_CBC_256_AES][j]); count++)
AES_cbc_encrypt(buf, buf,
(unsigned long)lengths[j], &aes_ks3,
iv, AES_ENCRYPT);
d = Time_F(STOP);
print_result(D_CBC_256_AES, j, count, d);
}
}
if (doit[D_IGE_128_AES]) {
for (j = 0; j < SIZE_NUM; j++) {
print_message(names[D_IGE_128_AES], c[D_IGE_128_AES][j],
lengths[j]);
Time_F(START);
for (count = 0, run = 1; COND(c[D_IGE_128_AES][j]); count++)
AES_ige_encrypt(buf, buf2,
(unsigned long)lengths[j], &aes_ks1,
iv, AES_ENCRYPT);
d = Time_F(STOP);
print_result(D_IGE_128_AES, j, count, d);
}
}
if (doit[D_IGE_192_AES]) {
for (j = 0; j < SIZE_NUM; j++) {
print_message(names[D_IGE_192_AES], c[D_IGE_192_AES][j],
lengths[j]);
Time_F(START);
for (count = 0, run = 1; COND(c[D_IGE_192_AES][j]); count++)
AES_ige_encrypt(buf, buf2,
(unsigned long)lengths[j], &aes_ks2,
iv, AES_ENCRYPT);
d = Time_F(STOP);
print_result(D_IGE_192_AES, j, count, d);
}
}
if (doit[D_IGE_256_AES]) {
for (j = 0; j < SIZE_NUM; j++) {
print_message(names[D_IGE_256_AES], c[D_IGE_256_AES][j],
lengths[j]);
Time_F(START);
for (count = 0, run = 1; COND(c[D_IGE_256_AES][j]); count++)
AES_ige_encrypt(buf, buf2,
(unsigned long)lengths[j], &aes_ks3,
iv, AES_ENCRYPT);
d = Time_F(STOP);
print_result(D_IGE_256_AES, j, count, d);
}
}
if (doit[D_GHASH]) {
GCM128_CONTEXT *ctx =
CRYPTO_gcm128_new(&aes_ks1, (block128_f) AES_encrypt);
CRYPTO_gcm128_setiv(ctx, (unsigned char *)"0123456789ab", 12);
for (j = 0; j < SIZE_NUM; j++) {
print_message(names[D_GHASH], c[D_GHASH][j], lengths[j]);
Time_F(START);
for (count = 0, run = 1; COND(c[D_GHASH][j]); count++)
CRYPTO_gcm128_aad(ctx, buf, lengths[j]);
d = Time_F(STOP);
print_result(D_GHASH, j, count, d);
}
CRYPTO_gcm128_release(ctx);
}
# endif
# ifndef OPENSSL_NO_CAMELLIA
if (doit[D_CBC_128_CML]) {
for (j = 0; j < SIZE_NUM; j++) {
print_message(names[D_CBC_128_CML], c[D_CBC_128_CML][j],
lengths[j]);
Time_F(START);
for (count = 0, run = 1; COND(c[D_CBC_128_CML][j]); count++)
Camellia_cbc_encrypt(buf, buf,
(unsigned long)lengths[j], &camellia_ks1,
iv, CAMELLIA_ENCRYPT);
d = Time_F(STOP);
print_result(D_CBC_128_CML, j, count, d);
}
}
if (doit[D_CBC_192_CML]) {
for (j = 0; j < SIZE_NUM; j++) {
print_message(names[D_CBC_192_CML], c[D_CBC_192_CML][j],
lengths[j]);
Time_F(START);
for (count = 0, run = 1; COND(c[D_CBC_192_CML][j]); count++)
Camellia_cbc_encrypt(buf, buf,
(unsigned long)lengths[j], &camellia_ks2,
iv, CAMELLIA_ENCRYPT);
d = Time_F(STOP);
print_result(D_CBC_192_CML, j, count, d);
}
}
if (doit[D_CBC_256_CML]) {
for (j = 0; j < SIZE_NUM; j++) {
print_message(names[D_CBC_256_CML], c[D_CBC_256_CML][j],
lengths[j]);
Time_F(START);
for (count = 0, run = 1; COND(c[D_CBC_256_CML][j]); count++)
Camellia_cbc_encrypt(buf, buf,
(unsigned long)lengths[j], &camellia_ks3,
iv, CAMELLIA_ENCRYPT);
d = Time_F(STOP);
print_result(D_CBC_256_CML, j, count, d);
}
}
# endif
# ifndef OPENSSL_NO_IDEA
if (doit[D_CBC_IDEA]) {
for (j = 0; j < SIZE_NUM; j++) {
print_message(names[D_CBC_IDEA], c[D_CBC_IDEA][j], lengths[j]);
Time_F(START);
for (count = 0, run = 1; COND(c[D_CBC_IDEA][j]); count++)
idea_cbc_encrypt(buf, buf,
(unsigned long)lengths[j], &idea_ks,
iv, IDEA_ENCRYPT);
d = Time_F(STOP);
print_result(D_CBC_IDEA, j, count, d);
}
}
# endif
# ifndef OPENSSL_NO_SEED
if (doit[D_CBC_SEED]) {
for (j = 0; j < SIZE_NUM; j++) {
print_message(names[D_CBC_SEED], c[D_CBC_SEED][j], lengths[j]);
Time_F(START);
for (count = 0, run = 1; COND(c[D_CBC_SEED][j]); count++)
SEED_cbc_encrypt(buf, buf,
(unsigned long)lengths[j], &seed_ks, iv, 1);
d = Time_F(STOP);
print_result(D_CBC_SEED, j, count, d);
}
}
# endif
# ifndef OPENSSL_NO_RC2
if (doit[D_CBC_RC2]) {
for (j = 0; j < SIZE_NUM; j++) {
print_message(names[D_CBC_RC2], c[D_CBC_RC2][j], lengths[j]);
Time_F(START);
for (count = 0, run = 1; COND(c[D_CBC_RC2][j]); count++)
RC2_cbc_encrypt(buf, buf,
(unsigned long)lengths[j], &rc2_ks,
iv, RC2_ENCRYPT);
d = Time_F(STOP);
print_result(D_CBC_RC2, j, count, d);
}
}
# endif
# ifndef OPENSSL_NO_RC5
if (doit[D_CBC_RC5]) {
for (j = 0; j < SIZE_NUM; j++) {
print_message(names[D_CBC_RC5], c[D_CBC_RC5][j], lengths[j]);
Time_F(START);
for (count = 0, run = 1; COND(c[D_CBC_RC5][j]); count++)
RC5_32_cbc_encrypt(buf, buf,
(unsigned long)lengths[j], &rc5_ks,
iv, RC5_ENCRYPT);
d = Time_F(STOP);
print_result(D_CBC_RC5, j, count, d);
}
}
# endif
# ifndef OPENSSL_NO_BF
if (doit[D_CBC_BF]) {
for (j = 0; j < SIZE_NUM; j++) {
print_message(names[D_CBC_BF], c[D_CBC_BF][j], lengths[j]);
Time_F(START);
for (count = 0, run = 1; COND(c[D_CBC_BF][j]); count++)
BF_cbc_encrypt(buf, buf,
(unsigned long)lengths[j], &bf_ks,
iv, BF_ENCRYPT);
d = Time_F(STOP);
print_result(D_CBC_BF, j, count, d);
}
}
# endif
# ifndef OPENSSL_NO_CAST
if (doit[D_CBC_CAST]) {
for (j = 0; j < SIZE_NUM; j++) {
print_message(names[D_CBC_CAST], c[D_CBC_CAST][j], lengths[j]);
Time_F(START);
for (count = 0, run = 1; COND(c[D_CBC_CAST][j]); count++)
CAST_cbc_encrypt(buf, buf,
(unsigned long)lengths[j], &cast_ks,
iv, CAST_ENCRYPT);
d = Time_F(STOP);
print_result(D_CBC_CAST, j, count, d);
}
}
# endif
if (doit[D_EVP]) {
# ifdef EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
if (multiblock && evp_cipher) {
if (!
(EVP_CIPHER_flags(evp_cipher) &
EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {
fprintf(stderr, "%s is not multi-block capable\n",
OBJ_nid2ln(evp_cipher->nid));
goto end;
}
multiblock_speed(evp_cipher);
mret = 0;
goto end;
}
# endif
for (j = 0; j < SIZE_NUM; j++) {
if (evp_cipher) {
EVP_CIPHER_CTX ctx;
int outl;
names[D_EVP] = OBJ_nid2ln(evp_cipher->nid);
print_message(names[D_EVP], save_count, lengths[j]);
EVP_CIPHER_CTX_init(&ctx);
if (decrypt)
EVP_DecryptInit_ex(&ctx, evp_cipher, NULL, key16, iv);
else
EVP_EncryptInit_ex(&ctx, evp_cipher, NULL, key16, iv);
EVP_CIPHER_CTX_set_padding(&ctx, 0);
Time_F(START);
if (decrypt)
for (count = 0, run = 1;
COND(save_count * 4 * lengths[0] / lengths[j]);
count++)
EVP_DecryptUpdate(&ctx, buf, &outl, buf, lengths[j]);
else
for (count = 0, run = 1;
COND(save_count * 4 * lengths[0] / lengths[j]);
count++)
EVP_EncryptUpdate(&ctx, buf, &outl, buf, lengths[j]);
if (decrypt)
EVP_DecryptFinal_ex(&ctx, buf, &outl);
else
EVP_EncryptFinal_ex(&ctx, buf, &outl);
d = Time_F(STOP);
EVP_CIPHER_CTX_cleanup(&ctx);
}
if (evp_md) {
names[D_EVP] = OBJ_nid2ln(evp_md->type);
print_message(names[D_EVP], save_count, lengths[j]);
Time_F(START);
for (count = 0, run = 1;
COND(save_count * 4 * lengths[0] / lengths[j]); count++)
EVP_Digest(buf, lengths[j], &(md[0]), NULL, evp_md, NULL);
d = Time_F(STOP);
}
print_result(D_EVP, j, count, d);
}
}
# ifndef OPENSSL_SYS_WIN32
# endif
RAND_pseudo_bytes(buf, 36);
# ifndef OPENSSL_NO_RSA
for (j = 0; j < RSA_NUM; j++) {
int ret;
if (!rsa_doit[j])
continue;
ret = RSA_sign(NID_md5_sha1, buf, 36, buf2, &rsa_num, rsa_key[j]);
if (ret == 0) {
BIO_printf(bio_err,
"RSA sign failure. No RSA sign will be done.\n");
ERR_print_errors(bio_err);
rsa_count = 1;
} else {
pkey_print_message("private", "rsa",
rsa_c[j][0], rsa_bits[j], RSA_SECONDS);
Time_F(START);
for (count = 0, run = 1; COND(rsa_c[j][0]); count++) {
ret = RSA_sign(NID_md5_sha1, buf, 36, buf2,
&rsa_num, rsa_key[j]);
if (ret == 0) {
BIO_printf(bio_err, "RSA sign failure\n");
ERR_print_errors(bio_err);
count = 1;
break;
}
}
d = Time_F(STOP);
BIO_printf(bio_err,
mr ? "+R1:%ld:%d:%.2f\n"
: "%ld %d bit private RSA's in %.2fs\n",
count, rsa_bits[j], d);
rsa_results[j][0] = d / (double)count;
rsa_count = count;
}
# if 1
ret = RSA_verify(NID_md5_sha1, buf, 36, buf2, rsa_num, rsa_key[j]);
if (ret <= 0) {
BIO_printf(bio_err,
"RSA verify failure. No RSA verify will be done.\n");
ERR_print_errors(bio_err);
rsa_doit[j] = 0;
} else {
pkey_print_message("public", "rsa",
rsa_c[j][1], rsa_bits[j], RSA_SECONDS);
Time_F(START);
for (count = 0, run = 1; COND(rsa_c[j][1]); count++) {
ret = RSA_verify(NID_md5_sha1, buf, 36, buf2,
rsa_num, rsa_key[j]);
if (ret <= 0) {
BIO_printf(bio_err, "RSA verify failure\n");
ERR_print_errors(bio_err);
count = 1;
break;
}
}
d = Time_F(STOP);
BIO_printf(bio_err,
mr ? "+R2:%ld:%d:%.2f\n"
: "%ld %d bit public RSA's in %.2fs\n",
count, rsa_bits[j], d);
rsa_results[j][1] = d / (double)count;
}
# endif
if (rsa_count <= 1) {
for (j++; j < RSA_NUM; j++)
rsa_doit[j] = 0;
}
}
# endif
RAND_pseudo_bytes(buf, 20);
# ifndef OPENSSL_NO_DSA
if (RAND_status() != 1) {
RAND_seed(rnd_seed, sizeof rnd_seed);
rnd_fake = 1;
}
for (j = 0; j < DSA_NUM; j++) {
unsigned int kk;
int ret;
if (!dsa_doit[j])
continue;
ret = DSA_sign(EVP_PKEY_DSA, buf, 20, buf2, &kk, dsa_key[j]);
if (ret == 0) {
BIO_printf(bio_err,
"DSA sign failure. No DSA sign will be done.\n");
ERR_print_errors(bio_err);
rsa_count = 1;
} else {
pkey_print_message("sign", "dsa",
dsa_c[j][0], dsa_bits[j], DSA_SECONDS);
Time_F(START);
for (count = 0, run = 1; COND(dsa_c[j][0]); count++) {
ret = DSA_sign(EVP_PKEY_DSA, buf, 20, buf2, &kk, dsa_key[j]);
if (ret == 0) {
BIO_printf(bio_err, "DSA sign failure\n");
ERR_print_errors(bio_err);
count = 1;
break;
}
}
d = Time_F(STOP);
BIO_printf(bio_err,
mr ? "+R3:%ld:%d:%.2f\n"
: "%ld %d bit DSA signs in %.2fs\n",
count, dsa_bits[j], d);
dsa_results[j][0] = d / (double)count;
rsa_count = count;
}
ret = DSA_verify(EVP_PKEY_DSA, buf, 20, buf2, kk, dsa_key[j]);
if (ret <= 0) {
BIO_printf(bio_err,
"DSA verify failure. No DSA verify will be done.\n");
ERR_print_errors(bio_err);
dsa_doit[j] = 0;
} else {
pkey_print_message("verify", "dsa",
dsa_c[j][1], dsa_bits[j], DSA_SECONDS);
Time_F(START);
for (count = 0, run = 1; COND(dsa_c[j][1]); count++) {
ret = DSA_verify(EVP_PKEY_DSA, buf, 20, buf2, kk, dsa_key[j]);
if (ret <= 0) {
BIO_printf(bio_err, "DSA verify failure\n");
ERR_print_errors(bio_err);
count = 1;
break;
}
}
d = Time_F(STOP);
BIO_printf(bio_err,
mr ? "+R4:%ld:%d:%.2f\n"
: "%ld %d bit DSA verify in %.2fs\n",
count, dsa_bits[j], d);
dsa_results[j][1] = d / (double)count;
}
if (rsa_count <= 1) {
for (j++; j < DSA_NUM; j++)
dsa_doit[j] = 0;
}
}
if (rnd_fake)
RAND_cleanup();
# endif
# ifndef OPENSSL_NO_ECDSA
if (RAND_status() != 1) {
RAND_seed(rnd_seed, sizeof rnd_seed);
rnd_fake = 1;
}
for (j = 0; j < EC_NUM; j++) {
int ret;
if (!ecdsa_doit[j])
continue;
ecdsa[j] = EC_KEY_new_by_curve_name(test_curves[j]);
if (ecdsa[j] == NULL) {
BIO_printf(bio_err, "ECDSA failure.\n");
ERR_print_errors(bio_err);
rsa_count = 1;
} else {
# if 1
EC_KEY_precompute_mult(ecdsa[j], NULL);
# endif
EC_KEY_generate_key(ecdsa[j]);
ret = ECDSA_sign(0, buf, 20, ecdsasig, &ecdsasiglen, ecdsa[j]);
if (ret == 0) {
BIO_printf(bio_err,
"ECDSA sign failure. No ECDSA sign will be done.\n");
ERR_print_errors(bio_err);
rsa_count = 1;
} else {
pkey_print_message("sign", "ecdsa",
ecdsa_c[j][0],
test_curves_bits[j], ECDSA_SECONDS);
Time_F(START);
for (count = 0, run = 1; COND(ecdsa_c[j][0]); count++) {
ret = ECDSA_sign(0, buf, 20,
ecdsasig, &ecdsasiglen, ecdsa[j]);
if (ret == 0) {
BIO_printf(bio_err, "ECDSA sign failure\n");
ERR_print_errors(bio_err);
count = 1;
break;
}
}
d = Time_F(STOP);
BIO_printf(bio_err,
mr ? "+R5:%ld:%d:%.2f\n" :
"%ld %d bit ECDSA signs in %.2fs \n",
count, test_curves_bits[j], d);
ecdsa_results[j][0] = d / (double)count;
rsa_count = count;
}
ret = ECDSA_verify(0, buf, 20, ecdsasig, ecdsasiglen, ecdsa[j]);
if (ret != 1) {
BIO_printf(bio_err,
"ECDSA verify failure. No ECDSA verify will be done.\n");
ERR_print_errors(bio_err);
ecdsa_doit[j] = 0;
} else {
pkey_print_message("verify", "ecdsa",
ecdsa_c[j][1],
test_curves_bits[j], ECDSA_SECONDS);
Time_F(START);
for (count = 0, run = 1; COND(ecdsa_c[j][1]); count++) {
ret =
ECDSA_verify(0, buf, 20, ecdsasig, ecdsasiglen,
ecdsa[j]);
if (ret != 1) {
BIO_printf(bio_err, "ECDSA verify failure\n");
ERR_print_errors(bio_err);
count = 1;
break;
}
}
d = Time_F(STOP);
BIO_printf(bio_err,
mr ? "+R6:%ld:%d:%.2f\n"
: "%ld %d bit ECDSA verify in %.2fs\n",
count, test_curves_bits[j], d);
ecdsa_results[j][1] = d / (double)count;
}
if (rsa_count <= 1) {
for (j++; j < EC_NUM; j++)
ecdsa_doit[j] = 0;
}
}
}
if (rnd_fake)
RAND_cleanup();
# endif
# ifndef OPENSSL_NO_ECDH
if (RAND_status() != 1) {
RAND_seed(rnd_seed, sizeof rnd_seed);
rnd_fake = 1;
}
for (j = 0; j < EC_NUM; j++) {
if (!ecdh_doit[j])
continue;
ecdh_a[j] = EC_KEY_new_by_curve_name(test_curves[j]);
ecdh_b[j] = EC_KEY_new_by_curve_name(test_curves[j]);
if ((ecdh_a[j] == NULL) || (ecdh_b[j] == NULL)) {
BIO_printf(bio_err, "ECDH failure.\n");
ERR_print_errors(bio_err);
rsa_count = 1;
} else {
if (!EC_KEY_generate_key(ecdh_a[j]) ||
!EC_KEY_generate_key(ecdh_b[j])) {
BIO_printf(bio_err, "ECDH key generation failure.\n");
ERR_print_errors(bio_err);
rsa_count = 1;
} else {
int field_size, outlen;
void *(*kdf) (const void *in, size_t inlen, void *out,
size_t *xoutlen);
field_size =
EC_GROUP_get_degree(EC_KEY_get0_group(ecdh_a[j]));
if (field_size <= 24 * 8) {
outlen = KDF1_SHA1_len;
kdf = KDF1_SHA1;
} else {
outlen = (field_size + 7) / 8;
kdf = NULL;
}
secret_size_a =
ECDH_compute_key(secret_a, outlen,
EC_KEY_get0_public_key(ecdh_b[j]),
ecdh_a[j], kdf);
secret_size_b =
ECDH_compute_key(secret_b, outlen,
EC_KEY_get0_public_key(ecdh_a[j]),
ecdh_b[j], kdf);
if (secret_size_a != secret_size_b)
ecdh_checks = 0;
else
ecdh_checks = 1;
for (secret_idx = 0; (secret_idx < secret_size_a)
&& (ecdh_checks == 1); secret_idx++) {
if (secret_a[secret_idx] != secret_b[secret_idx])
ecdh_checks = 0;
}
if (ecdh_checks == 0) {
BIO_printf(bio_err, "ECDH computations don't match.\n");
ERR_print_errors(bio_err);
rsa_count = 1;
}
pkey_print_message("", "ecdh",
ecdh_c[j][0],
test_curves_bits[j], ECDH_SECONDS);
Time_F(START);
for (count = 0, run = 1; COND(ecdh_c[j][0]); count++) {
ECDH_compute_key(secret_a, outlen,
EC_KEY_get0_public_key(ecdh_b[j]),
ecdh_a[j], kdf);
}
d = Time_F(STOP);
BIO_printf(bio_err,
mr ? "+R7:%ld:%d:%.2f\n" :
"%ld %d-bit ECDH ops in %.2fs\n", count,
test_curves_bits[j], d);
ecdh_results[j][0] = d / (double)count;
rsa_count = count;
}
}
if (rsa_count <= 1) {
for (j++; j < EC_NUM; j++)
ecdh_doit[j] = 0;
}
}
if (rnd_fake)
RAND_cleanup();
# endif
# ifndef NO_FORK
show_res:
# endif
if (!mr) {
fprintf(stdout, "%s\n", SSLeay_version(SSLEAY_VERSION));
fprintf(stdout, "%s\n", SSLeay_version(SSLEAY_BUILT_ON));
printf("options:");
printf("%s ", BN_options());
# ifndef OPENSSL_NO_MD2
printf("%s ", MD2_options());
# endif
# ifndef OPENSSL_NO_RC4
printf("%s ", RC4_options());
# endif
# ifndef OPENSSL_NO_DES
printf("%s ", DES_options());
# endif
# ifndef OPENSSL_NO_AES
printf("%s ", AES_options());
# endif
# ifndef OPENSSL_NO_IDEA
printf("%s ", idea_options());
# endif
# ifndef OPENSSL_NO_BF
printf("%s ", BF_options());
# endif
fprintf(stdout, "\n%s\n", SSLeay_version(SSLEAY_CFLAGS));
}
if (pr_header) {
if (mr)
fprintf(stdout, "+H");
else {
fprintf(stdout,
"The 'numbers' are in 1000s of bytes per second processed.\n");
fprintf(stdout, "type ");
}
for (j = 0; j < SIZE_NUM; j++)
fprintf(stdout, mr ? ":%d" : "%7d bytes", lengths[j]);
fprintf(stdout, "\n");
}
for (k = 0; k < ALGOR_NUM; k++) {
if (!doit[k])
continue;
if (mr)
fprintf(stdout, "+F:%d:%s", k, names[k]);
else
fprintf(stdout, "%-13s", names[k]);
for (j = 0; j < SIZE_NUM; j++) {
if (results[k][j] > 10000 && !mr)
fprintf(stdout, " %11.2fk", results[k][j] / 1e3);
else
fprintf(stdout, mr ? ":%.2f" : " %11.2f ", results[k][j]);
}
fprintf(stdout, "\n");
}
# ifndef OPENSSL_NO_RSA
j = 1;
for (k = 0; k < RSA_NUM; k++) {
if (!rsa_doit[k])
continue;
if (j && !mr) {
printf("%18ssign verify sign/s verify/s\n", " ");
j = 0;
}
if (mr)
fprintf(stdout, "+F2:%u:%u:%f:%f\n",
k, rsa_bits[k], rsa_results[k][0], rsa_results[k][1]);
else
fprintf(stdout, "rsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
rsa_bits[k], rsa_results[k][0], rsa_results[k][1],
1.0 / rsa_results[k][0], 1.0 / rsa_results[k][1]);
}
# endif
# ifndef OPENSSL_NO_DSA
j = 1;
for (k = 0; k < DSA_NUM; k++) {
if (!dsa_doit[k])
continue;
if (j && !mr) {
printf("%18ssign verify sign/s verify/s\n", " ");
j = 0;
}
if (mr)
fprintf(stdout, "+F3:%u:%u:%f:%f\n",
k, dsa_bits[k], dsa_results[k][0], dsa_results[k][1]);
else
fprintf(stdout, "dsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
dsa_bits[k], dsa_results[k][0], dsa_results[k][1],
1.0 / dsa_results[k][0], 1.0 / dsa_results[k][1]);
}
# endif
# ifndef OPENSSL_NO_ECDSA
j = 1;
for (k = 0; k < EC_NUM; k++) {
if (!ecdsa_doit[k])
continue;
if (j && !mr) {
printf("%30ssign verify sign/s verify/s\n", " ");
j = 0;
}
if (mr)
fprintf(stdout, "+F4:%u:%u:%f:%f\n",
k, test_curves_bits[k],
ecdsa_results[k][0], ecdsa_results[k][1]);
else
fprintf(stdout,
"%4u bit ecdsa (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
test_curves_bits[k],
test_curves_names[k],
ecdsa_results[k][0], ecdsa_results[k][1],
1.0 / ecdsa_results[k][0], 1.0 / ecdsa_results[k][1]);
}
# endif
# ifndef OPENSSL_NO_ECDH
j = 1;
for (k = 0; k < EC_NUM; k++) {
if (!ecdh_doit[k])
continue;
if (j && !mr) {
printf("%30sop op/s\n", " ");
j = 0;
}
if (mr)
fprintf(stdout, "+F5:%u:%u:%f:%f\n",
k, test_curves_bits[k],
ecdh_results[k][0], 1.0 / ecdh_results[k][0]);
else
fprintf(stdout, "%4u bit ecdh (%s) %8.4fs %8.1f\n",
test_curves_bits[k],
test_curves_names[k],
ecdh_results[k][0], 1.0 / ecdh_results[k][0]);
}
# endif
mret = 0;
end:
ERR_print_errors(bio_err);
if (buf_malloc != NULL)
OPENSSL_free(buf_malloc);
if (buf2_malloc != NULL)
OPENSSL_free(buf2_malloc);
# ifndef OPENSSL_NO_RSA
for (i = 0; i < RSA_NUM; i++)
if (rsa_key[i] != NULL)
RSA_free(rsa_key[i]);
# endif
# ifndef OPENSSL_NO_DSA
for (i = 0; i < DSA_NUM; i++)
if (dsa_key[i] != NULL)
DSA_free(dsa_key[i]);
# endif
# ifndef OPENSSL_NO_ECDSA
for (i = 0; i < EC_NUM; i++)
if (ecdsa[i] != NULL)
EC_KEY_free(ecdsa[i]);
# endif
# ifndef OPENSSL_NO_ECDH
for (i = 0; i < EC_NUM; i++) {
if (ecdh_a[i] != NULL)
EC_KEY_free(ecdh_a[i]);
if (ecdh_b[i] != NULL)
EC_KEY_free(ecdh_b[i]);
}
# endif
apps_shutdown();
OPENSSL_EXIT(mret);
}
|
['int MAIN(int argc, char **argv)\n{\n unsigned char *buf_malloc = NULL, *buf2_malloc = NULL;\n unsigned char *buf = NULL, *buf2 = NULL;\n int mret = 1;\n long count = 0, save_count = 0;\n int i, j, k;\n# if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA)\n long rsa_count;\n# endif\n# ifndef OPENSSL_NO_RSA\n unsigned rsa_num;\n# endif\n unsigned char md[EVP_MAX_MD_SIZE];\n# ifndef OPENSSL_NO_MD2\n unsigned char md2[MD2_DIGEST_LENGTH];\n# endif\n# ifndef OPENSSL_NO_MDC2\n unsigned char mdc2[MDC2_DIGEST_LENGTH];\n# endif\n# ifndef OPENSSL_NO_MD4\n unsigned char md4[MD4_DIGEST_LENGTH];\n# endif\n# ifndef OPENSSL_NO_MD5\n unsigned char md5[MD5_DIGEST_LENGTH];\n unsigned char hmac[MD5_DIGEST_LENGTH];\n# endif\n# ifndef OPENSSL_NO_SHA\n unsigned char sha[SHA_DIGEST_LENGTH];\n# ifndef OPENSSL_NO_SHA256\n unsigned char sha256[SHA256_DIGEST_LENGTH];\n# endif\n# ifndef OPENSSL_NO_SHA512\n unsigned char sha512[SHA512_DIGEST_LENGTH];\n# endif\n# endif\n# ifndef OPENSSL_NO_WHIRLPOOL\n unsigned char whirlpool[WHIRLPOOL_DIGEST_LENGTH];\n# endif\n# ifndef OPENSSL_NO_RMD160\n unsigned char rmd160[RIPEMD160_DIGEST_LENGTH];\n# endif\n# ifndef OPENSSL_NO_RC4\n RC4_KEY rc4_ks;\n# endif\n# ifndef OPENSSL_NO_RC5\n RC5_32_KEY rc5_ks;\n# endif\n# ifndef OPENSSL_NO_RC2\n RC2_KEY rc2_ks;\n# endif\n# ifndef OPENSSL_NO_IDEA\n IDEA_KEY_SCHEDULE idea_ks;\n# endif\n# ifndef OPENSSL_NO_SEED\n SEED_KEY_SCHEDULE seed_ks;\n# endif\n# ifndef OPENSSL_NO_BF\n BF_KEY bf_ks;\n# endif\n# ifndef OPENSSL_NO_CAST\n CAST_KEY cast_ks;\n# endif\n static const unsigned char key16[16] = {\n 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,\n 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12\n };\n# ifndef OPENSSL_NO_AES\n static const unsigned char key24[24] = {\n 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,\n 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,\n 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34\n };\n static const unsigned char key32[32] = {\n 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,\n 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,\n 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,\n 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56\n };\n# endif\n# ifndef OPENSSL_NO_CAMELLIA\n static const unsigned char ckey24[24] = {\n 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,\n 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,\n 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34\n };\n static const unsigned char ckey32[32] = {\n 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,\n 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,\n 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,\n 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56\n };\n# endif\n# ifndef OPENSSL_NO_AES\n# define MAX_BLOCK_SIZE 128\n# else\n# define MAX_BLOCK_SIZE 64\n# endif\n unsigned char DES_iv[8];\n unsigned char iv[2 * MAX_BLOCK_SIZE / 8];\n# ifndef OPENSSL_NO_DES\n static DES_cblock key =\n { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 };\n static DES_cblock key2 =\n { 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12 };\n static DES_cblock key3 =\n { 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34 };\n DES_key_schedule sch;\n DES_key_schedule sch2;\n DES_key_schedule sch3;\n# endif\n# ifndef OPENSSL_NO_AES\n AES_KEY aes_ks1, aes_ks2, aes_ks3;\n# endif\n# ifndef OPENSSL_NO_CAMELLIA\n CAMELLIA_KEY camellia_ks1, camellia_ks2, camellia_ks3;\n# endif\n# define D_MD2 0\n# define D_MDC2 1\n# define D_MD4 2\n# define D_MD5 3\n# define D_HMAC 4\n# define D_SHA1 5\n# define D_RMD160 6\n# define D_RC4 7\n# define D_CBC_DES 8\n# define D_EDE3_DES 9\n# define D_CBC_IDEA 10\n# define D_CBC_SEED 11\n# define D_CBC_RC2 12\n# define D_CBC_RC5 13\n# define D_CBC_BF 14\n# define D_CBC_CAST 15\n# define D_CBC_128_AES 16\n# define D_CBC_192_AES 17\n# define D_CBC_256_AES 18\n# define D_CBC_128_CML 19\n# define D_CBC_192_CML 20\n# define D_CBC_256_CML 21\n# define D_EVP 22\n# define D_SHA256 23\n# define D_SHA512 24\n# define D_WHIRLPOOL 25\n# define D_IGE_128_AES 26\n# define D_IGE_192_AES 27\n# define D_IGE_256_AES 28\n# define D_GHASH 29\n double d = 0.0;\n long c[ALGOR_NUM][SIZE_NUM];\n# ifndef OPENSSL_SYS_WIN32\n# endif\n# define R_DSA_512 0\n# define R_DSA_1024 1\n# define R_DSA_2048 2\n# define R_RSA_512 0\n# define R_RSA_1024 1\n# define R_RSA_2048 2\n# define R_RSA_3072 3\n# define R_RSA_4096 4\n# define R_RSA_7680 5\n# define R_RSA_15360 6\n# define R_EC_P160 0\n# define R_EC_P192 1\n# define R_EC_P224 2\n# define R_EC_P256 3\n# define R_EC_P384 4\n# define R_EC_P521 5\n# define R_EC_K163 6\n# define R_EC_K233 7\n# define R_EC_K283 8\n# define R_EC_K409 9\n# define R_EC_K571 10\n# define R_EC_B163 11\n# define R_EC_B233 12\n# define R_EC_B283 13\n# define R_EC_B409 14\n# define R_EC_B571 15\n# ifndef OPENSSL_NO_RSA\n RSA *rsa_key[RSA_NUM];\n long rsa_c[RSA_NUM][2];\n static unsigned int rsa_bits[RSA_NUM] = {\n 512, 1024, 2048, 3072, 4096, 7680, 15360\n };\n static unsigned char *rsa_data[RSA_NUM] = {\n test512, test1024, test2048, test3072, test4096, test7680, test15360\n };\n static int rsa_data_length[RSA_NUM] = {\n sizeof(test512), sizeof(test1024),\n sizeof(test2048), sizeof(test3072),\n sizeof(test4096), sizeof(test7680),\n sizeof(test15360)\n };\n# endif\n# ifndef OPENSSL_NO_DSA\n DSA *dsa_key[DSA_NUM];\n long dsa_c[DSA_NUM][2];\n static unsigned int dsa_bits[DSA_NUM] = { 512, 1024, 2048 };\n# endif\n# ifndef OPENSSL_NO_EC\n static unsigned int test_curves[EC_NUM] = {\n NID_secp160r1,\n NID_X9_62_prime192v1,\n NID_secp224r1,\n NID_X9_62_prime256v1,\n NID_secp384r1,\n NID_secp521r1,\n NID_sect163k1,\n NID_sect233k1,\n NID_sect283k1,\n NID_sect409k1,\n NID_sect571k1,\n NID_sect163r2,\n NID_sect233r1,\n NID_sect283r1,\n NID_sect409r1,\n NID_sect571r1\n };\n static const char *test_curves_names[EC_NUM] = {\n "secp160r1",\n "nistp192",\n "nistp224",\n "nistp256",\n "nistp384",\n "nistp521",\n "nistk163",\n "nistk233",\n "nistk283",\n "nistk409",\n "nistk571",\n "nistb163",\n "nistb233",\n "nistb283",\n "nistb409",\n "nistb571"\n };\n static int test_curves_bits[EC_NUM] = {\n 160, 192, 224, 256, 384, 521,\n 163, 233, 283, 409, 571,\n 163, 233, 283, 409, 571\n };\n# endif\n# ifndef OPENSSL_NO_ECDSA\n unsigned char ecdsasig[256];\n unsigned int ecdsasiglen;\n EC_KEY *ecdsa[EC_NUM];\n long ecdsa_c[EC_NUM][2];\n# endif\n# ifndef OPENSSL_NO_ECDH\n EC_KEY *ecdh_a[EC_NUM], *ecdh_b[EC_NUM];\n unsigned char secret_a[MAX_ECDH_SIZE], secret_b[MAX_ECDH_SIZE];\n int secret_size_a, secret_size_b;\n int ecdh_checks = 0;\n int secret_idx = 0;\n long ecdh_c[EC_NUM][2];\n# endif\n int rsa_doit[RSA_NUM];\n int dsa_doit[DSA_NUM];\n# ifndef OPENSSL_NO_ECDSA\n int ecdsa_doit[EC_NUM];\n# endif\n# ifndef OPENSSL_NO_ECDH\n int ecdh_doit[EC_NUM];\n# endif\n int doit[ALGOR_NUM];\n int pr_header = 0;\n const EVP_CIPHER *evp_cipher = NULL;\n const EVP_MD *evp_md = NULL;\n int decrypt = 0;\n# ifndef NO_FORK\n int multi = 0;\n# endif\n int multiblock = 0;\n int misalign = MAX_MISALIGNMENT + 1;\n# ifndef TIMES\n usertime = -1;\n# endif\n apps_startup();\n memset(results, 0, sizeof(results));\n# ifndef OPENSSL_NO_DSA\n memset(dsa_key, 0, sizeof(dsa_key));\n# endif\n# ifndef OPENSSL_NO_ECDSA\n for (i = 0; i < EC_NUM; i++)\n ecdsa[i] = NULL;\n# endif\n# ifndef OPENSSL_NO_ECDH\n for (i = 0; i < EC_NUM; i++) {\n ecdh_a[i] = NULL;\n ecdh_b[i] = NULL;\n }\n# endif\n if (bio_err == NULL)\n if ((bio_err = BIO_new(BIO_s_file())) != NULL)\n BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);\n if (!load_config(bio_err, NULL))\n goto end;\n# ifndef OPENSSL_NO_RSA\n memset(rsa_key, 0, sizeof(rsa_key));\n for (i = 0; i < RSA_NUM; i++)\n rsa_key[i] = NULL;\n# endif\n if ((buf_malloc =\n (unsigned char *)OPENSSL_malloc(BUFSIZE + misalign)) == NULL) {\n BIO_printf(bio_err, "out of memory\\n");\n goto end;\n }\n if ((buf2_malloc =\n (unsigned char *)OPENSSL_malloc(BUFSIZE + misalign)) == NULL) {\n BIO_printf(bio_err, "out of memory\\n");\n goto end;\n }\n misalign = 0;\n buf = buf_malloc;\n buf2 = buf2_malloc;\n memset(c, 0, sizeof(c));\n memset(DES_iv, 0, sizeof(DES_iv));\n memset(iv, 0, sizeof(iv));\n for (i = 0; i < ALGOR_NUM; i++)\n doit[i] = 0;\n for (i = 0; i < RSA_NUM; i++)\n rsa_doit[i] = 0;\n for (i = 0; i < DSA_NUM; i++)\n dsa_doit[i] = 0;\n# ifndef OPENSSL_NO_ECDSA\n for (i = 0; i < EC_NUM; i++)\n ecdsa_doit[i] = 0;\n# endif\n# ifndef OPENSSL_NO_ECDH\n for (i = 0; i < EC_NUM; i++)\n ecdh_doit[i] = 0;\n# endif\n j = 0;\n argc--;\n argv++;\n while (argc) {\n if ((argc > 0) && (strcmp(*argv, "-elapsed") == 0)) {\n usertime = 0;\n j--;\n } else if ((argc > 0) && (strcmp(*argv, "-evp") == 0)) {\n argc--;\n argv++;\n if (argc == 0) {\n BIO_printf(bio_err, "no EVP given\\n");\n goto end;\n }\n evp_cipher = EVP_get_cipherbyname(*argv);\n if (!evp_cipher) {\n evp_md = EVP_get_digestbyname(*argv);\n }\n if (!evp_cipher && !evp_md) {\n BIO_printf(bio_err, "%s is an unknown cipher or digest\\n",\n *argv);\n goto end;\n }\n doit[D_EVP] = 1;\n } else if (argc > 0 && !strcmp(*argv, "-decrypt")) {\n decrypt = 1;\n j--;\n }\n# ifndef OPENSSL_NO_ENGINE\n else if ((argc > 0) && (strcmp(*argv, "-engine") == 0)) {\n argc--;\n argv++;\n if (argc == 0) {\n BIO_printf(bio_err, "no engine given\\n");\n goto end;\n }\n setup_engine(bio_err, *argv, 0);\n j--;\n }\n# endif\n# ifndef NO_FORK\n else if ((argc > 0) && (strcmp(*argv, "-multi") == 0)) {\n argc--;\n argv++;\n if (argc == 0) {\n BIO_printf(bio_err, "no multi count given\\n");\n goto end;\n }\n multi = atoi(argv[0]);\n if (multi <= 0) {\n BIO_printf(bio_err, "bad multi count\\n");\n goto end;\n }\n j--;\n }\n# endif\n else if (argc > 0 && !strcmp(*argv, "-mr")) {\n mr = 1;\n j--;\n } else if (argc > 0 && !strcmp(*argv, "-mb")) {\n multiblock = 1;\n j--;\n } else if (argc > 0 && !strcmp(*argv, "-misalign")) {\n argc--;\n argv++;\n if (argc == 0) {\n BIO_printf(bio_err, "no misalignment given\\n");\n goto end;\n }\n misalign = atoi(argv[0]);\n if (misalign < 0 || misalign > MAX_MISALIGNMENT) {\n BIO_printf(bio_err,\n "misalignment is outsize permitted range 0-%d\\n",\n MAX_MISALIGNMENT);\n goto end;\n }\n buf = buf_malloc + misalign;\n buf2 = buf2_malloc + misalign;\n j--;\n } else\n# ifndef OPENSSL_NO_MD2\n if (strcmp(*argv, "md2") == 0)\n doit[D_MD2] = 1;\n else\n# endif\n# ifndef OPENSSL_NO_MDC2\n if (strcmp(*argv, "mdc2") == 0)\n doit[D_MDC2] = 1;\n else\n# endif\n# ifndef OPENSSL_NO_MD4\n if (strcmp(*argv, "md4") == 0)\n doit[D_MD4] = 1;\n else\n# endif\n# ifndef OPENSSL_NO_MD5\n if (strcmp(*argv, "md5") == 0)\n doit[D_MD5] = 1;\n else\n# endif\n# ifndef OPENSSL_NO_MD5\n if (strcmp(*argv, "hmac") == 0)\n doit[D_HMAC] = 1;\n else\n# endif\n# ifndef OPENSSL_NO_SHA\n if (strcmp(*argv, "sha1") == 0)\n doit[D_SHA1] = 1;\n else if (strcmp(*argv, "sha") == 0)\n doit[D_SHA1] = 1, doit[D_SHA256] = 1, doit[D_SHA512] = 1;\n else\n# ifndef OPENSSL_NO_SHA256\n if (strcmp(*argv, "sha256") == 0)\n doit[D_SHA256] = 1;\n else\n# endif\n# ifndef OPENSSL_NO_SHA512\n if (strcmp(*argv, "sha512") == 0)\n doit[D_SHA512] = 1;\n else\n# endif\n# endif\n# ifndef OPENSSL_NO_WHIRLPOOL\n if (strcmp(*argv, "whirlpool") == 0)\n doit[D_WHIRLPOOL] = 1;\n else\n# endif\n# ifndef OPENSSL_NO_RMD160\n if (strcmp(*argv, "ripemd") == 0)\n doit[D_RMD160] = 1;\n else if (strcmp(*argv, "rmd160") == 0)\n doit[D_RMD160] = 1;\n else if (strcmp(*argv, "ripemd160") == 0)\n doit[D_RMD160] = 1;\n else\n# endif\n# ifndef OPENSSL_NO_RC4\n if (strcmp(*argv, "rc4") == 0)\n doit[D_RC4] = 1;\n else\n# endif\n# ifndef OPENSSL_NO_DES\n if (strcmp(*argv, "des-cbc") == 0)\n doit[D_CBC_DES] = 1;\n else if (strcmp(*argv, "des-ede3") == 0)\n doit[D_EDE3_DES] = 1;\n else\n# endif\n# ifndef OPENSSL_NO_AES\n if (strcmp(*argv, "aes-128-cbc") == 0)\n doit[D_CBC_128_AES] = 1;\n else if (strcmp(*argv, "aes-192-cbc") == 0)\n doit[D_CBC_192_AES] = 1;\n else if (strcmp(*argv, "aes-256-cbc") == 0)\n doit[D_CBC_256_AES] = 1;\n else if (strcmp(*argv, "aes-128-ige") == 0)\n doit[D_IGE_128_AES] = 1;\n else if (strcmp(*argv, "aes-192-ige") == 0)\n doit[D_IGE_192_AES] = 1;\n else if (strcmp(*argv, "aes-256-ige") == 0)\n doit[D_IGE_256_AES] = 1;\n else\n# endif\n# ifndef OPENSSL_NO_CAMELLIA\n if (strcmp(*argv, "camellia-128-cbc") == 0)\n doit[D_CBC_128_CML] = 1;\n else if (strcmp(*argv, "camellia-192-cbc") == 0)\n doit[D_CBC_192_CML] = 1;\n else if (strcmp(*argv, "camellia-256-cbc") == 0)\n doit[D_CBC_256_CML] = 1;\n else\n# endif\n# ifndef OPENSSL_NO_RSA\n# if 0\n if (strcmp(*argv, "rsaref") == 0) {\n RSA_set_default_openssl_method(RSA_PKCS1_RSAref());\n j--;\n } else\n# endif\n# ifndef RSA_NULL\n if (strcmp(*argv, "openssl") == 0) {\n RSA_set_default_method(RSA_PKCS1_SSLeay());\n j--;\n } else\n# endif\n# endif\n if (strcmp(*argv, "dsa512") == 0)\n dsa_doit[R_DSA_512] = 2;\n else if (strcmp(*argv, "dsa1024") == 0)\n dsa_doit[R_DSA_1024] = 2;\n else if (strcmp(*argv, "dsa2048") == 0)\n dsa_doit[R_DSA_2048] = 2;\n else if (strcmp(*argv, "rsa512") == 0)\n rsa_doit[R_RSA_512] = 2;\n else if (strcmp(*argv, "rsa1024") == 0)\n rsa_doit[R_RSA_1024] = 2;\n else if (strcmp(*argv, "rsa2048") == 0)\n rsa_doit[R_RSA_2048] = 2;\n else if (strcmp(*argv, "rsa3072") == 0)\n rsa_doit[R_RSA_3072] = 2;\n else if (strcmp(*argv, "rsa4096") == 0)\n rsa_doit[R_RSA_4096] = 2;\n else if (strcmp(*argv, "rsa7680") == 0)\n rsa_doit[R_RSA_7680] = 2;\n else if (strcmp(*argv, "rsa15360") == 0)\n rsa_doit[R_RSA_15360] = 2;\n else\n# ifndef OPENSSL_NO_RC2\n if (strcmp(*argv, "rc2-cbc") == 0)\n doit[D_CBC_RC2] = 1;\n else if (strcmp(*argv, "rc2") == 0)\n doit[D_CBC_RC2] = 1;\n else\n# endif\n# ifndef OPENSSL_NO_RC5\n if (strcmp(*argv, "rc5-cbc") == 0)\n doit[D_CBC_RC5] = 1;\n else if (strcmp(*argv, "rc5") == 0)\n doit[D_CBC_RC5] = 1;\n else\n# endif\n# ifndef OPENSSL_NO_IDEA\n if (strcmp(*argv, "idea-cbc") == 0)\n doit[D_CBC_IDEA] = 1;\n else if (strcmp(*argv, "idea") == 0)\n doit[D_CBC_IDEA] = 1;\n else\n# endif\n# ifndef OPENSSL_NO_SEED\n if (strcmp(*argv, "seed-cbc") == 0)\n doit[D_CBC_SEED] = 1;\n else if (strcmp(*argv, "seed") == 0)\n doit[D_CBC_SEED] = 1;\n else\n# endif\n# ifndef OPENSSL_NO_BF\n if (strcmp(*argv, "bf-cbc") == 0)\n doit[D_CBC_BF] = 1;\n else if (strcmp(*argv, "blowfish") == 0)\n doit[D_CBC_BF] = 1;\n else if (strcmp(*argv, "bf") == 0)\n doit[D_CBC_BF] = 1;\n else\n# endif\n# ifndef OPENSSL_NO_CAST\n if (strcmp(*argv, "cast-cbc") == 0)\n doit[D_CBC_CAST] = 1;\n else if (strcmp(*argv, "cast") == 0)\n doit[D_CBC_CAST] = 1;\n else if (strcmp(*argv, "cast5") == 0)\n doit[D_CBC_CAST] = 1;\n else\n# endif\n# ifndef OPENSSL_NO_DES\n if (strcmp(*argv, "des") == 0) {\n doit[D_CBC_DES] = 1;\n doit[D_EDE3_DES] = 1;\n } else\n# endif\n# ifndef OPENSSL_NO_AES\n if (strcmp(*argv, "aes") == 0) {\n doit[D_CBC_128_AES] = 1;\n doit[D_CBC_192_AES] = 1;\n doit[D_CBC_256_AES] = 1;\n } else if (strcmp(*argv, "ghash") == 0) {\n doit[D_GHASH] = 1;\n } else\n# endif\n# ifndef OPENSSL_NO_CAMELLIA\n if (strcmp(*argv, "camellia") == 0) {\n doit[D_CBC_128_CML] = 1;\n doit[D_CBC_192_CML] = 1;\n doit[D_CBC_256_CML] = 1;\n } else\n# endif\n# ifndef OPENSSL_NO_RSA\n if (strcmp(*argv, "rsa") == 0) {\n rsa_doit[R_RSA_512] = 1;\n rsa_doit[R_RSA_1024] = 1;\n rsa_doit[R_RSA_2048] = 1;\n rsa_doit[R_RSA_3072] = 1;\n rsa_doit[R_RSA_4096] = 1;\n rsa_doit[R_RSA_7680] = 1;\n rsa_doit[R_RSA_15360] = 1;\n } else\n# endif\n# ifndef OPENSSL_NO_DSA\n if (strcmp(*argv, "dsa") == 0) {\n dsa_doit[R_DSA_512] = 1;\n dsa_doit[R_DSA_1024] = 1;\n dsa_doit[R_DSA_2048] = 1;\n } else\n# endif\n# ifndef OPENSSL_NO_ECDSA\n if (strcmp(*argv, "ecdsap160") == 0)\n ecdsa_doit[R_EC_P160] = 2;\n else if (strcmp(*argv, "ecdsap192") == 0)\n ecdsa_doit[R_EC_P192] = 2;\n else if (strcmp(*argv, "ecdsap224") == 0)\n ecdsa_doit[R_EC_P224] = 2;\n else if (strcmp(*argv, "ecdsap256") == 0)\n ecdsa_doit[R_EC_P256] = 2;\n else if (strcmp(*argv, "ecdsap384") == 0)\n ecdsa_doit[R_EC_P384] = 2;\n else if (strcmp(*argv, "ecdsap521") == 0)\n ecdsa_doit[R_EC_P521] = 2;\n else if (strcmp(*argv, "ecdsak163") == 0)\n ecdsa_doit[R_EC_K163] = 2;\n else if (strcmp(*argv, "ecdsak233") == 0)\n ecdsa_doit[R_EC_K233] = 2;\n else if (strcmp(*argv, "ecdsak283") == 0)\n ecdsa_doit[R_EC_K283] = 2;\n else if (strcmp(*argv, "ecdsak409") == 0)\n ecdsa_doit[R_EC_K409] = 2;\n else if (strcmp(*argv, "ecdsak571") == 0)\n ecdsa_doit[R_EC_K571] = 2;\n else if (strcmp(*argv, "ecdsab163") == 0)\n ecdsa_doit[R_EC_B163] = 2;\n else if (strcmp(*argv, "ecdsab233") == 0)\n ecdsa_doit[R_EC_B233] = 2;\n else if (strcmp(*argv, "ecdsab283") == 0)\n ecdsa_doit[R_EC_B283] = 2;\n else if (strcmp(*argv, "ecdsab409") == 0)\n ecdsa_doit[R_EC_B409] = 2;\n else if (strcmp(*argv, "ecdsab571") == 0)\n ecdsa_doit[R_EC_B571] = 2;\n else if (strcmp(*argv, "ecdsa") == 0) {\n for (i = 0; i < EC_NUM; i++)\n ecdsa_doit[i] = 1;\n } else\n# endif\n# ifndef OPENSSL_NO_ECDH\n if (strcmp(*argv, "ecdhp160") == 0)\n ecdh_doit[R_EC_P160] = 2;\n else if (strcmp(*argv, "ecdhp192") == 0)\n ecdh_doit[R_EC_P192] = 2;\n else if (strcmp(*argv, "ecdhp224") == 0)\n ecdh_doit[R_EC_P224] = 2;\n else if (strcmp(*argv, "ecdhp256") == 0)\n ecdh_doit[R_EC_P256] = 2;\n else if (strcmp(*argv, "ecdhp384") == 0)\n ecdh_doit[R_EC_P384] = 2;\n else if (strcmp(*argv, "ecdhp521") == 0)\n ecdh_doit[R_EC_P521] = 2;\n else if (strcmp(*argv, "ecdhk163") == 0)\n ecdh_doit[R_EC_K163] = 2;\n else if (strcmp(*argv, "ecdhk233") == 0)\n ecdh_doit[R_EC_K233] = 2;\n else if (strcmp(*argv, "ecdhk283") == 0)\n ecdh_doit[R_EC_K283] = 2;\n else if (strcmp(*argv, "ecdhk409") == 0)\n ecdh_doit[R_EC_K409] = 2;\n else if (strcmp(*argv, "ecdhk571") == 0)\n ecdh_doit[R_EC_K571] = 2;\n else if (strcmp(*argv, "ecdhb163") == 0)\n ecdh_doit[R_EC_B163] = 2;\n else if (strcmp(*argv, "ecdhb233") == 0)\n ecdh_doit[R_EC_B233] = 2;\n else if (strcmp(*argv, "ecdhb283") == 0)\n ecdh_doit[R_EC_B283] = 2;\n else if (strcmp(*argv, "ecdhb409") == 0)\n ecdh_doit[R_EC_B409] = 2;\n else if (strcmp(*argv, "ecdhb571") == 0)\n ecdh_doit[R_EC_B571] = 2;\n else if (strcmp(*argv, "ecdh") == 0) {\n for (i = 0; i < EC_NUM; i++)\n ecdh_doit[i] = 1;\n } else\n# endif\n {\n BIO_printf(bio_err, "Error: bad option or value\\n");\n BIO_printf(bio_err, "\\n");\n BIO_printf(bio_err, "Available values:\\n");\n# ifndef OPENSSL_NO_MD2\n BIO_printf(bio_err, "md2 ");\n# endif\n# ifndef OPENSSL_NO_MDC2\n BIO_printf(bio_err, "mdc2 ");\n# endif\n# ifndef OPENSSL_NO_MD4\n BIO_printf(bio_err, "md4 ");\n# endif\n# ifndef OPENSSL_NO_MD5\n BIO_printf(bio_err, "md5 ");\n# ifndef OPENSSL_NO_HMAC\n BIO_printf(bio_err, "hmac ");\n# endif\n# endif\n# ifndef OPENSSL_NO_SHA1\n BIO_printf(bio_err, "sha1 ");\n# endif\n# ifndef OPENSSL_NO_SHA256\n BIO_printf(bio_err, "sha256 ");\n# endif\n# ifndef OPENSSL_NO_SHA512\n BIO_printf(bio_err, "sha512 ");\n# endif\n# ifndef OPENSSL_NO_WHIRLPOOL\n BIO_printf(bio_err, "whirlpool");\n# endif\n# ifndef OPENSSL_NO_RMD160\n BIO_printf(bio_err, "rmd160");\n# endif\n# if !defined(OPENSSL_NO_MD2) || !defined(OPENSSL_NO_MDC2) || \\\n !defined(OPENSSL_NO_MD4) || !defined(OPENSSL_NO_MD5) || \\\n !defined(OPENSSL_NO_SHA1) || !defined(OPENSSL_NO_RMD160) || \\\n !defined(OPENSSL_NO_WHIRLPOOL)\n BIO_printf(bio_err, "\\n");\n# endif\n# ifndef OPENSSL_NO_IDEA\n BIO_printf(bio_err, "idea-cbc ");\n# endif\n# ifndef OPENSSL_NO_SEED\n BIO_printf(bio_err, "seed-cbc ");\n# endif\n# ifndef OPENSSL_NO_RC2\n BIO_printf(bio_err, "rc2-cbc ");\n# endif\n# ifndef OPENSSL_NO_RC5\n BIO_printf(bio_err, "rc5-cbc ");\n# endif\n# ifndef OPENSSL_NO_BF\n BIO_printf(bio_err, "bf-cbc");\n# endif\n# if !defined(OPENSSL_NO_IDEA) || !defined(OPENSSL_NO_SEED) || !defined(OPENSSL_NO_RC2) || \\\n !defined(OPENSSL_NO_BF) || !defined(OPENSSL_NO_RC5)\n BIO_printf(bio_err, "\\n");\n# endif\n# ifndef OPENSSL_NO_DES\n BIO_printf(bio_err, "des-cbc des-ede3 ");\n# endif\n# ifndef OPENSSL_NO_AES\n BIO_printf(bio_err, "aes-128-cbc aes-192-cbc aes-256-cbc ");\n BIO_printf(bio_err, "aes-128-ige aes-192-ige aes-256-ige ");\n# endif\n# ifndef OPENSSL_NO_CAMELLIA\n BIO_printf(bio_err, "\\n");\n BIO_printf(bio_err,\n "camellia-128-cbc camellia-192-cbc camellia-256-cbc ");\n# endif\n# ifndef OPENSSL_NO_RC4\n BIO_printf(bio_err, "rc4");\n# endif\n BIO_printf(bio_err, "\\n");\n# ifndef OPENSSL_NO_RSA\n BIO_printf(bio_err,\n "rsa512 rsa1024 rsa2048 rsa3072 rsa4096\\n");\n BIO_printf(bio_err, "rsa7680 rsa15360\\n");\n# endif\n# ifndef OPENSSL_NO_DSA\n BIO_printf(bio_err, "dsa512 dsa1024 dsa2048\\n");\n# endif\n# ifndef OPENSSL_NO_ECDSA\n BIO_printf(bio_err, "ecdsap160 ecdsap192 ecdsap224 "\n "ecdsap256 ecdsap384 ecdsap521\\n");\n BIO_printf(bio_err,\n "ecdsak163 ecdsak233 ecdsak283 ecdsak409 ecdsak571\\n");\n BIO_printf(bio_err,\n "ecdsab163 ecdsab233 ecdsab283 ecdsab409 ecdsab571\\n");\n BIO_printf(bio_err, "ecdsa\\n");\n# endif\n# ifndef OPENSSL_NO_ECDH\n BIO_printf(bio_err, "ecdhp160 ecdhp192 ecdhp224 "\n "ecdhp256 ecdhp384 ecdhp521\\n");\n BIO_printf(bio_err,\n "ecdhk163 ecdhk233 ecdhk283 ecdhk409 ecdhk571\\n");\n BIO_printf(bio_err,\n "ecdhb163 ecdhb233 ecdhb283 ecdhb409 ecdhb571\\n");\n BIO_printf(bio_err, "ecdh\\n");\n# endif\n# ifndef OPENSSL_NO_IDEA\n BIO_printf(bio_err, "idea ");\n# endif\n# ifndef OPENSSL_NO_SEED\n BIO_printf(bio_err, "seed ");\n# endif\n# ifndef OPENSSL_NO_RC2\n BIO_printf(bio_err, "rc2 ");\n# endif\n# ifndef OPENSSL_NO_DES\n BIO_printf(bio_err, "des ");\n# endif\n# ifndef OPENSSL_NO_AES\n BIO_printf(bio_err, "aes ");\n# endif\n# ifndef OPENSSL_NO_CAMELLIA\n BIO_printf(bio_err, "camellia ");\n# endif\n# ifndef OPENSSL_NO_RSA\n BIO_printf(bio_err, "rsa ");\n# endif\n# ifndef OPENSSL_NO_BF\n BIO_printf(bio_err, "blowfish");\n# endif\n# if !defined(OPENSSL_NO_IDEA) || !defined(OPENSSL_NO_SEED) || \\\n !defined(OPENSSL_NO_RC2) || !defined(OPENSSL_NO_DES) || \\\n !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_BF) || \\\n !defined(OPENSSL_NO_AES) || !defined(OPENSSL_NO_CAMELLIA)\n BIO_printf(bio_err, "\\n");\n# endif\n BIO_printf(bio_err, "\\n");\n BIO_printf(bio_err, "Available options:\\n");\n# if defined(TIMES) || defined(USE_TOD)\n BIO_printf(bio_err, "-elapsed "\n "measure time in real time instead of CPU user time.\\n");\n# endif\n# ifndef OPENSSL_NO_ENGINE\n BIO_printf(bio_err,\n "-engine e "\n "use engine e, possibly a hardware device.\\n");\n# endif\n BIO_printf(bio_err, "-evp e " "use EVP e.\\n");\n BIO_printf(bio_err,\n "-decrypt "\n "time decryption instead of encryption (only EVP).\\n");\n BIO_printf(bio_err,\n "-mr "\n "produce machine readable output.\\n");\n BIO_printf(bio_err,\n "-mb "\n "perform multi-block benchmark (for specific ciphers)\\n");\n BIO_printf(bio_err,\n "-misalign n "\n "perform benchmark with misaligned data\\n");\n# ifndef NO_FORK\n BIO_printf(bio_err,\n "-multi n " "run n benchmarks in parallel.\\n");\n# endif\n goto end;\n }\n argc--;\n argv++;\n j++;\n }\n# ifndef NO_FORK\n if (multi && do_multi(multi))\n goto show_res;\n# endif\n if (j == 0) {\n for (i = 0; i < ALGOR_NUM; i++) {\n if (i != D_EVP)\n doit[i] = 1;\n }\n for (i = 0; i < RSA_NUM; i++)\n rsa_doit[i] = 1;\n for (i = 0; i < DSA_NUM; i++)\n dsa_doit[i] = 1;\n# ifndef OPENSSL_NO_ECDSA\n for (i = 0; i < EC_NUM; i++)\n ecdsa_doit[i] = 1;\n# endif\n# ifndef OPENSSL_NO_ECDH\n for (i = 0; i < EC_NUM; i++)\n ecdh_doit[i] = 1;\n# endif\n }\n for (i = 0; i < ALGOR_NUM; i++)\n if (doit[i])\n pr_header++;\n if (usertime == 0 && !mr)\n BIO_printf(bio_err,\n "You have chosen to measure elapsed time "\n "instead of user CPU time.\\n");\n# ifndef OPENSSL_NO_RSA\n for (i = 0; i < RSA_NUM; i++) {\n const unsigned char *p;\n p = rsa_data[i];\n rsa_key[i] = d2i_RSAPrivateKey(NULL, &p, rsa_data_length[i]);\n if (rsa_key[i] == NULL) {\n BIO_printf(bio_err, "internal error loading RSA key number %d\\n",\n i);\n goto end;\n }\n# if 0\n else {\n BIO_printf(bio_err,\n mr ? "+RK:%d:"\n : "Loaded RSA key, %d bit modulus and e= 0x",\n BN_num_bits(rsa_key[i]->n));\n BN_print(bio_err, rsa_key[i]->e);\n BIO_printf(bio_err, "\\n");\n }\n# endif\n }\n# endif\n# ifndef OPENSSL_NO_DSA\n dsa_key[0] = get_dsa512();\n dsa_key[1] = get_dsa1024();\n dsa_key[2] = get_dsa2048();\n# endif\n# ifndef OPENSSL_NO_DES\n DES_set_key_unchecked(&key, &sch);\n DES_set_key_unchecked(&key2, &sch2);\n DES_set_key_unchecked(&key3, &sch3);\n# endif\n# ifndef OPENSSL_NO_AES\n AES_set_encrypt_key(key16, 128, &aes_ks1);\n AES_set_encrypt_key(key24, 192, &aes_ks2);\n AES_set_encrypt_key(key32, 256, &aes_ks3);\n# endif\n# ifndef OPENSSL_NO_CAMELLIA\n Camellia_set_key(key16, 128, &camellia_ks1);\n Camellia_set_key(ckey24, 192, &camellia_ks2);\n Camellia_set_key(ckey32, 256, &camellia_ks3);\n# endif\n# ifndef OPENSSL_NO_IDEA\n idea_set_encrypt_key(key16, &idea_ks);\n# endif\n# ifndef OPENSSL_NO_SEED\n SEED_set_key(key16, &seed_ks);\n# endif\n# ifndef OPENSSL_NO_RC4\n RC4_set_key(&rc4_ks, 16, key16);\n# endif\n# ifndef OPENSSL_NO_RC2\n RC2_set_key(&rc2_ks, 16, key16, 128);\n# endif\n# ifndef OPENSSL_NO_RC5\n RC5_32_set_key(&rc5_ks, 16, key16, 12);\n# endif\n# ifndef OPENSSL_NO_BF\n BF_set_key(&bf_ks, 16, key16);\n# endif\n# ifndef OPENSSL_NO_CAST\n CAST_set_key(&cast_ks, 16, key16);\n# endif\n# ifndef OPENSSL_NO_RSA\n memset(rsa_c, 0, sizeof(rsa_c));\n# endif\n# ifndef SIGALRM\n# ifndef OPENSSL_NO_DES\n BIO_printf(bio_err, "First we calculate the approximate speed ...\\n");\n count = 10;\n do {\n long it;\n count *= 2;\n Time_F(START);\n for (it = count; it; it--)\n DES_ecb_encrypt((DES_cblock *)buf,\n (DES_cblock *)buf, &sch, DES_ENCRYPT);\n d = Time_F(STOP);\n } while (d < 3);\n save_count = count;\n c[D_MD2][0] = count / 10;\n c[D_MDC2][0] = count / 10;\n c[D_MD4][0] = count;\n c[D_MD5][0] = count;\n c[D_HMAC][0] = count;\n c[D_SHA1][0] = count;\n c[D_RMD160][0] = count;\n c[D_RC4][0] = count * 5;\n c[D_CBC_DES][0] = count;\n c[D_EDE3_DES][0] = count / 3;\n c[D_CBC_IDEA][0] = count;\n c[D_CBC_SEED][0] = count;\n c[D_CBC_RC2][0] = count;\n c[D_CBC_RC5][0] = count;\n c[D_CBC_BF][0] = count;\n c[D_CBC_CAST][0] = count;\n c[D_CBC_128_AES][0] = count;\n c[D_CBC_192_AES][0] = count;\n c[D_CBC_256_AES][0] = count;\n c[D_CBC_128_CML][0] = count;\n c[D_CBC_192_CML][0] = count;\n c[D_CBC_256_CML][0] = count;\n c[D_SHA256][0] = count;\n c[D_SHA512][0] = count;\n c[D_WHIRLPOOL][0] = count;\n c[D_IGE_128_AES][0] = count;\n c[D_IGE_192_AES][0] = count;\n c[D_IGE_256_AES][0] = count;\n c[D_GHASH][0] = count;\n for (i = 1; i < SIZE_NUM; i++) {\n long l0, l1;\n l0 = (long)lengths[0];\n l1 = (long)lengths[i];\n c[D_MD2][i] = c[D_MD2][0] * 4 * l0 / l1;\n c[D_MDC2][i] = c[D_MDC2][0] * 4 * l0 / l1;\n c[D_MD4][i] = c[D_MD4][0] * 4 * l0 / l1;\n c[D_MD5][i] = c[D_MD5][0] * 4 * l0 / l1;\n c[D_HMAC][i] = c[D_HMAC][0] * 4 * l0 / l1;\n c[D_SHA1][i] = c[D_SHA1][0] * 4 * l0 / l1;\n c[D_RMD160][i] = c[D_RMD160][0] * 4 * l0 / l1;\n c[D_SHA256][i] = c[D_SHA256][0] * 4 * l0 / l1;\n c[D_SHA512][i] = c[D_SHA512][0] * 4 * l0 / l1;\n c[D_WHIRLPOOL][i] = c[D_WHIRLPOOL][0] * 4 * l0 / l1;\n l0 = (long)lengths[i - 1];\n c[D_RC4][i] = c[D_RC4][i - 1] * l0 / l1;\n c[D_CBC_DES][i] = c[D_CBC_DES][i - 1] * l0 / l1;\n c[D_EDE3_DES][i] = c[D_EDE3_DES][i - 1] * l0 / l1;\n c[D_CBC_IDEA][i] = c[D_CBC_IDEA][i - 1] * l0 / l1;\n c[D_CBC_SEED][i] = c[D_CBC_SEED][i - 1] * l0 / l1;\n c[D_CBC_RC2][i] = c[D_CBC_RC2][i - 1] * l0 / l1;\n c[D_CBC_RC5][i] = c[D_CBC_RC5][i - 1] * l0 / l1;\n c[D_CBC_BF][i] = c[D_CBC_BF][i - 1] * l0 / l1;\n c[D_CBC_CAST][i] = c[D_CBC_CAST][i - 1] * l0 / l1;\n c[D_CBC_128_AES][i] = c[D_CBC_128_AES][i - 1] * l0 / l1;\n c[D_CBC_192_AES][i] = c[D_CBC_192_AES][i - 1] * l0 / l1;\n c[D_CBC_256_AES][i] = c[D_CBC_256_AES][i - 1] * l0 / l1;\n c[D_CBC_128_CML][i] = c[D_CBC_128_CML][i - 1] * l0 / l1;\n c[D_CBC_192_CML][i] = c[D_CBC_192_CML][i - 1] * l0 / l1;\n c[D_CBC_256_CML][i] = c[D_CBC_256_CML][i - 1] * l0 / l1;\n c[D_IGE_128_AES][i] = c[D_IGE_128_AES][i - 1] * l0 / l1;\n c[D_IGE_192_AES][i] = c[D_IGE_192_AES][i - 1] * l0 / l1;\n c[D_IGE_256_AES][i] = c[D_IGE_256_AES][i - 1] * l0 / l1;\n }\n# ifndef OPENSSL_NO_RSA\n rsa_c[R_RSA_512][0] = count / 2000;\n rsa_c[R_RSA_512][1] = count / 400;\n for (i = 1; i < RSA_NUM; i++) {\n rsa_c[i][0] = rsa_c[i - 1][0] / 8;\n rsa_c[i][1] = rsa_c[i - 1][1] / 4;\n if ((rsa_doit[i] <= 1) && (rsa_c[i][0] == 0))\n rsa_doit[i] = 0;\n else {\n if (rsa_c[i][0] == 0) {\n rsa_c[i][0] = 1;\n rsa_c[i][1] = 20;\n }\n }\n }\n# endif\n# ifndef OPENSSL_NO_DSA\n dsa_c[R_DSA_512][0] = count / 1000;\n dsa_c[R_DSA_512][1] = count / 1000 / 2;\n for (i = 1; i < DSA_NUM; i++) {\n dsa_c[i][0] = dsa_c[i - 1][0] / 4;\n dsa_c[i][1] = dsa_c[i - 1][1] / 4;\n if ((dsa_doit[i] <= 1) && (dsa_c[i][0] == 0))\n dsa_doit[i] = 0;\n else {\n if (dsa_c[i] == 0) {\n dsa_c[i][0] = 1;\n dsa_c[i][1] = 1;\n }\n }\n }\n# endif\n# ifndef OPENSSL_NO_ECDSA\n ecdsa_c[R_EC_P160][0] = count / 1000;\n ecdsa_c[R_EC_P160][1] = count / 1000 / 2;\n for (i = R_EC_P192; i <= R_EC_P521; i++) {\n ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;\n ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;\n if ((ecdsa_doit[i] <= 1) && (ecdsa_c[i][0] == 0))\n ecdsa_doit[i] = 0;\n else {\n if (ecdsa_c[i] == 0) {\n ecdsa_c[i][0] = 1;\n ecdsa_c[i][1] = 1;\n }\n }\n }\n ecdsa_c[R_EC_K163][0] = count / 1000;\n ecdsa_c[R_EC_K163][1] = count / 1000 / 2;\n for (i = R_EC_K233; i <= R_EC_K571; i++) {\n ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;\n ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;\n if ((ecdsa_doit[i] <= 1) && (ecdsa_c[i][0] == 0))\n ecdsa_doit[i] = 0;\n else {\n if (ecdsa_c[i] == 0) {\n ecdsa_c[i][0] = 1;\n ecdsa_c[i][1] = 1;\n }\n }\n }\n ecdsa_c[R_EC_B163][0] = count / 1000;\n ecdsa_c[R_EC_B163][1] = count / 1000 / 2;\n for (i = R_EC_B233; i <= R_EC_B571; i++) {\n ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;\n ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;\n if ((ecdsa_doit[i] <= 1) && (ecdsa_c[i][0] == 0))\n ecdsa_doit[i] = 0;\n else {\n if (ecdsa_c[i] == 0) {\n ecdsa_c[i][0] = 1;\n ecdsa_c[i][1] = 1;\n }\n }\n }\n# endif\n# ifndef OPENSSL_NO_ECDH\n ecdh_c[R_EC_P160][0] = count / 1000;\n ecdh_c[R_EC_P160][1] = count / 1000;\n for (i = R_EC_P192; i <= R_EC_P521; i++) {\n ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;\n ecdh_c[i][1] = ecdh_c[i - 1][1] / 2;\n if ((ecdh_doit[i] <= 1) && (ecdh_c[i][0] == 0))\n ecdh_doit[i] = 0;\n else {\n if (ecdh_c[i] == 0) {\n ecdh_c[i][0] = 1;\n ecdh_c[i][1] = 1;\n }\n }\n }\n ecdh_c[R_EC_K163][0] = count / 1000;\n ecdh_c[R_EC_K163][1] = count / 1000;\n for (i = R_EC_K233; i <= R_EC_K571; i++) {\n ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;\n ecdh_c[i][1] = ecdh_c[i - 1][1] / 2;\n if ((ecdh_doit[i] <= 1) && (ecdh_c[i][0] == 0))\n ecdh_doit[i] = 0;\n else {\n if (ecdh_c[i] == 0) {\n ecdh_c[i][0] = 1;\n ecdh_c[i][1] = 1;\n }\n }\n }\n ecdh_c[R_EC_B163][0] = count / 1000;\n ecdh_c[R_EC_B163][1] = count / 1000;\n for (i = R_EC_B233; i <= R_EC_B571; i++) {\n ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;\n ecdh_c[i][1] = ecdh_c[i - 1][1] / 2;\n if ((ecdh_doit[i] <= 1) && (ecdh_c[i][0] == 0))\n ecdh_doit[i] = 0;\n else {\n if (ecdh_c[i] == 0) {\n ecdh_c[i][0] = 1;\n ecdh_c[i][1] = 1;\n }\n }\n }\n# endif\n# define COND(d) (count < (d))\n# define COUNT(d) (d)\n# else\n# error "You cannot disable DES on systems without SIGALRM."\n# endif\n# else\n# define COND(c) (run && count<0x7fffffff)\n# define COUNT(d) (count)\n# ifndef _WIN32\n signal(SIGALRM, sig_done);\n# endif\n# endif\n# ifndef OPENSSL_NO_MD2\n if (doit[D_MD2]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_MD2], c[D_MD2][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_MD2][j]); count++)\n EVP_Digest(buf, (unsigned long)lengths[j], &(md2[0]), NULL,\n EVP_md2(), NULL);\n d = Time_F(STOP);\n print_result(D_MD2, j, count, d);\n }\n }\n# endif\n# ifndef OPENSSL_NO_MDC2\n if (doit[D_MDC2]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_MDC2], c[D_MDC2][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_MDC2][j]); count++)\n EVP_Digest(buf, (unsigned long)lengths[j], &(mdc2[0]), NULL,\n EVP_mdc2(), NULL);\n d = Time_F(STOP);\n print_result(D_MDC2, j, count, d);\n }\n }\n# endif\n# ifndef OPENSSL_NO_MD4\n if (doit[D_MD4]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_MD4], c[D_MD4][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_MD4][j]); count++)\n EVP_Digest(&(buf[0]), (unsigned long)lengths[j], &(md4[0]),\n NULL, EVP_md4(), NULL);\n d = Time_F(STOP);\n print_result(D_MD4, j, count, d);\n }\n }\n# endif\n# ifndef OPENSSL_NO_MD5\n if (doit[D_MD5]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_MD5], c[D_MD5][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_MD5][j]); count++)\n MD5(buf, lengths[j], md5);\n d = Time_F(STOP);\n print_result(D_MD5, j, count, d);\n }\n }\n# endif\n# if !defined(OPENSSL_NO_MD5) && !defined(OPENSSL_NO_HMAC)\n if (doit[D_HMAC]) {\n HMAC_CTX hctx;\n HMAC_CTX_init(&hctx);\n HMAC_Init_ex(&hctx, (unsigned char *)"This is a key...",\n 16, EVP_md5(), NULL);\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_HMAC], c[D_HMAC][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_HMAC][j]); count++) {\n HMAC_Init_ex(&hctx, NULL, 0, NULL, NULL);\n HMAC_Update(&hctx, buf, lengths[j]);\n HMAC_Final(&hctx, &(hmac[0]), NULL);\n }\n d = Time_F(STOP);\n print_result(D_HMAC, j, count, d);\n }\n HMAC_CTX_cleanup(&hctx);\n }\n# endif\n# ifndef OPENSSL_NO_SHA\n if (doit[D_SHA1]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_SHA1], c[D_SHA1][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_SHA1][j]); count++)\n# if 0\n EVP_Digest(buf, (unsigned long)lengths[j], &(sha[0]), NULL,\n EVP_sha1(), NULL);\n# else\n SHA1(buf, lengths[j], sha);\n# endif\n d = Time_F(STOP);\n print_result(D_SHA1, j, count, d);\n }\n }\n# ifndef OPENSSL_NO_SHA256\n if (doit[D_SHA256]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_SHA256], c[D_SHA256][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_SHA256][j]); count++)\n SHA256(buf, lengths[j], sha256);\n d = Time_F(STOP);\n print_result(D_SHA256, j, count, d);\n }\n }\n# endif\n# ifndef OPENSSL_NO_SHA512\n if (doit[D_SHA512]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_SHA512], c[D_SHA512][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_SHA512][j]); count++)\n SHA512(buf, lengths[j], sha512);\n d = Time_F(STOP);\n print_result(D_SHA512, j, count, d);\n }\n }\n# endif\n# endif\n# ifndef OPENSSL_NO_WHIRLPOOL\n if (doit[D_WHIRLPOOL]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_WHIRLPOOL], c[D_WHIRLPOOL][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_WHIRLPOOL][j]); count++)\n WHIRLPOOL(buf, lengths[j], whirlpool);\n d = Time_F(STOP);\n print_result(D_WHIRLPOOL, j, count, d);\n }\n }\n# endif\n# ifndef OPENSSL_NO_RMD160\n if (doit[D_RMD160]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_RMD160], c[D_RMD160][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_RMD160][j]); count++)\n EVP_Digest(buf, (unsigned long)lengths[j], &(rmd160[0]), NULL,\n EVP_ripemd160(), NULL);\n d = Time_F(STOP);\n print_result(D_RMD160, j, count, d);\n }\n }\n# endif\n# ifndef OPENSSL_NO_RC4\n if (doit[D_RC4]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_RC4], c[D_RC4][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_RC4][j]); count++)\n RC4(&rc4_ks, (unsigned int)lengths[j], buf, buf);\n d = Time_F(STOP);\n print_result(D_RC4, j, count, d);\n }\n }\n# endif\n# ifndef OPENSSL_NO_DES\n if (doit[D_CBC_DES]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_CBC_DES], c[D_CBC_DES][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_CBC_DES][j]); count++)\n DES_ncbc_encrypt(buf, buf, lengths[j], &sch,\n &DES_iv, DES_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_CBC_DES, j, count, d);\n }\n }\n if (doit[D_EDE3_DES]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_EDE3_DES], c[D_EDE3_DES][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_EDE3_DES][j]); count++)\n DES_ede3_cbc_encrypt(buf, buf, lengths[j],\n &sch, &sch2, &sch3,\n &DES_iv, DES_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_EDE3_DES, j, count, d);\n }\n }\n# endif\n# ifndef OPENSSL_NO_AES\n if (doit[D_CBC_128_AES]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_CBC_128_AES], c[D_CBC_128_AES][j],\n lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_CBC_128_AES][j]); count++)\n AES_cbc_encrypt(buf, buf,\n (unsigned long)lengths[j], &aes_ks1,\n iv, AES_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_CBC_128_AES, j, count, d);\n }\n }\n if (doit[D_CBC_192_AES]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_CBC_192_AES], c[D_CBC_192_AES][j],\n lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_CBC_192_AES][j]); count++)\n AES_cbc_encrypt(buf, buf,\n (unsigned long)lengths[j], &aes_ks2,\n iv, AES_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_CBC_192_AES, j, count, d);\n }\n }\n if (doit[D_CBC_256_AES]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_CBC_256_AES], c[D_CBC_256_AES][j],\n lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_CBC_256_AES][j]); count++)\n AES_cbc_encrypt(buf, buf,\n (unsigned long)lengths[j], &aes_ks3,\n iv, AES_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_CBC_256_AES, j, count, d);\n }\n }\n if (doit[D_IGE_128_AES]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_IGE_128_AES], c[D_IGE_128_AES][j],\n lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_IGE_128_AES][j]); count++)\n AES_ige_encrypt(buf, buf2,\n (unsigned long)lengths[j], &aes_ks1,\n iv, AES_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_IGE_128_AES, j, count, d);\n }\n }\n if (doit[D_IGE_192_AES]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_IGE_192_AES], c[D_IGE_192_AES][j],\n lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_IGE_192_AES][j]); count++)\n AES_ige_encrypt(buf, buf2,\n (unsigned long)lengths[j], &aes_ks2,\n iv, AES_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_IGE_192_AES, j, count, d);\n }\n }\n if (doit[D_IGE_256_AES]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_IGE_256_AES], c[D_IGE_256_AES][j],\n lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_IGE_256_AES][j]); count++)\n AES_ige_encrypt(buf, buf2,\n (unsigned long)lengths[j], &aes_ks3,\n iv, AES_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_IGE_256_AES, j, count, d);\n }\n }\n if (doit[D_GHASH]) {\n GCM128_CONTEXT *ctx =\n CRYPTO_gcm128_new(&aes_ks1, (block128_f) AES_encrypt);\n CRYPTO_gcm128_setiv(ctx, (unsigned char *)"0123456789ab", 12);\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_GHASH], c[D_GHASH][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_GHASH][j]); count++)\n CRYPTO_gcm128_aad(ctx, buf, lengths[j]);\n d = Time_F(STOP);\n print_result(D_GHASH, j, count, d);\n }\n CRYPTO_gcm128_release(ctx);\n }\n# endif\n# ifndef OPENSSL_NO_CAMELLIA\n if (doit[D_CBC_128_CML]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_CBC_128_CML], c[D_CBC_128_CML][j],\n lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_CBC_128_CML][j]); count++)\n Camellia_cbc_encrypt(buf, buf,\n (unsigned long)lengths[j], &camellia_ks1,\n iv, CAMELLIA_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_CBC_128_CML, j, count, d);\n }\n }\n if (doit[D_CBC_192_CML]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_CBC_192_CML], c[D_CBC_192_CML][j],\n lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_CBC_192_CML][j]); count++)\n Camellia_cbc_encrypt(buf, buf,\n (unsigned long)lengths[j], &camellia_ks2,\n iv, CAMELLIA_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_CBC_192_CML, j, count, d);\n }\n }\n if (doit[D_CBC_256_CML]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_CBC_256_CML], c[D_CBC_256_CML][j],\n lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_CBC_256_CML][j]); count++)\n Camellia_cbc_encrypt(buf, buf,\n (unsigned long)lengths[j], &camellia_ks3,\n iv, CAMELLIA_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_CBC_256_CML, j, count, d);\n }\n }\n# endif\n# ifndef OPENSSL_NO_IDEA\n if (doit[D_CBC_IDEA]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_CBC_IDEA], c[D_CBC_IDEA][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_CBC_IDEA][j]); count++)\n idea_cbc_encrypt(buf, buf,\n (unsigned long)lengths[j], &idea_ks,\n iv, IDEA_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_CBC_IDEA, j, count, d);\n }\n }\n# endif\n# ifndef OPENSSL_NO_SEED\n if (doit[D_CBC_SEED]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_CBC_SEED], c[D_CBC_SEED][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_CBC_SEED][j]); count++)\n SEED_cbc_encrypt(buf, buf,\n (unsigned long)lengths[j], &seed_ks, iv, 1);\n d = Time_F(STOP);\n print_result(D_CBC_SEED, j, count, d);\n }\n }\n# endif\n# ifndef OPENSSL_NO_RC2\n if (doit[D_CBC_RC2]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_CBC_RC2], c[D_CBC_RC2][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_CBC_RC2][j]); count++)\n RC2_cbc_encrypt(buf, buf,\n (unsigned long)lengths[j], &rc2_ks,\n iv, RC2_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_CBC_RC2, j, count, d);\n }\n }\n# endif\n# ifndef OPENSSL_NO_RC5\n if (doit[D_CBC_RC5]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_CBC_RC5], c[D_CBC_RC5][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_CBC_RC5][j]); count++)\n RC5_32_cbc_encrypt(buf, buf,\n (unsigned long)lengths[j], &rc5_ks,\n iv, RC5_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_CBC_RC5, j, count, d);\n }\n }\n# endif\n# ifndef OPENSSL_NO_BF\n if (doit[D_CBC_BF]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_CBC_BF], c[D_CBC_BF][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_CBC_BF][j]); count++)\n BF_cbc_encrypt(buf, buf,\n (unsigned long)lengths[j], &bf_ks,\n iv, BF_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_CBC_BF, j, count, d);\n }\n }\n# endif\n# ifndef OPENSSL_NO_CAST\n if (doit[D_CBC_CAST]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_CBC_CAST], c[D_CBC_CAST][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_CBC_CAST][j]); count++)\n CAST_cbc_encrypt(buf, buf,\n (unsigned long)lengths[j], &cast_ks,\n iv, CAST_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_CBC_CAST, j, count, d);\n }\n }\n# endif\n if (doit[D_EVP]) {\n# ifdef EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK\n if (multiblock && evp_cipher) {\n if (!\n (EVP_CIPHER_flags(evp_cipher) &\n EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {\n fprintf(stderr, "%s is not multi-block capable\\n",\n OBJ_nid2ln(evp_cipher->nid));\n goto end;\n }\n multiblock_speed(evp_cipher);\n mret = 0;\n goto end;\n }\n# endif\n for (j = 0; j < SIZE_NUM; j++) {\n if (evp_cipher) {\n EVP_CIPHER_CTX ctx;\n int outl;\n names[D_EVP] = OBJ_nid2ln(evp_cipher->nid);\n print_message(names[D_EVP], save_count, lengths[j]);\n EVP_CIPHER_CTX_init(&ctx);\n if (decrypt)\n EVP_DecryptInit_ex(&ctx, evp_cipher, NULL, key16, iv);\n else\n EVP_EncryptInit_ex(&ctx, evp_cipher, NULL, key16, iv);\n EVP_CIPHER_CTX_set_padding(&ctx, 0);\n Time_F(START);\n if (decrypt)\n for (count = 0, run = 1;\n COND(save_count * 4 * lengths[0] / lengths[j]);\n count++)\n EVP_DecryptUpdate(&ctx, buf, &outl, buf, lengths[j]);\n else\n for (count = 0, run = 1;\n COND(save_count * 4 * lengths[0] / lengths[j]);\n count++)\n EVP_EncryptUpdate(&ctx, buf, &outl, buf, lengths[j]);\n if (decrypt)\n EVP_DecryptFinal_ex(&ctx, buf, &outl);\n else\n EVP_EncryptFinal_ex(&ctx, buf, &outl);\n d = Time_F(STOP);\n EVP_CIPHER_CTX_cleanup(&ctx);\n }\n if (evp_md) {\n names[D_EVP] = OBJ_nid2ln(evp_md->type);\n print_message(names[D_EVP], save_count, lengths[j]);\n Time_F(START);\n for (count = 0, run = 1;\n COND(save_count * 4 * lengths[0] / lengths[j]); count++)\n EVP_Digest(buf, lengths[j], &(md[0]), NULL, evp_md, NULL);\n d = Time_F(STOP);\n }\n print_result(D_EVP, j, count, d);\n }\n }\n# ifndef OPENSSL_SYS_WIN32\n# endif\n RAND_pseudo_bytes(buf, 36);\n# ifndef OPENSSL_NO_RSA\n for (j = 0; j < RSA_NUM; j++) {\n int ret;\n if (!rsa_doit[j])\n continue;\n ret = RSA_sign(NID_md5_sha1, buf, 36, buf2, &rsa_num, rsa_key[j]);\n if (ret == 0) {\n BIO_printf(bio_err,\n "RSA sign failure. No RSA sign will be done.\\n");\n ERR_print_errors(bio_err);\n rsa_count = 1;\n } else {\n pkey_print_message("private", "rsa",\n rsa_c[j][0], rsa_bits[j], RSA_SECONDS);\n Time_F(START);\n for (count = 0, run = 1; COND(rsa_c[j][0]); count++) {\n ret = RSA_sign(NID_md5_sha1, buf, 36, buf2,\n &rsa_num, rsa_key[j]);\n if (ret == 0) {\n BIO_printf(bio_err, "RSA sign failure\\n");\n ERR_print_errors(bio_err);\n count = 1;\n break;\n }\n }\n d = Time_F(STOP);\n BIO_printf(bio_err,\n mr ? "+R1:%ld:%d:%.2f\\n"\n : "%ld %d bit private RSA\'s in %.2fs\\n",\n count, rsa_bits[j], d);\n rsa_results[j][0] = d / (double)count;\n rsa_count = count;\n }\n# if 1\n ret = RSA_verify(NID_md5_sha1, buf, 36, buf2, rsa_num, rsa_key[j]);\n if (ret <= 0) {\n BIO_printf(bio_err,\n "RSA verify failure. No RSA verify will be done.\\n");\n ERR_print_errors(bio_err);\n rsa_doit[j] = 0;\n } else {\n pkey_print_message("public", "rsa",\n rsa_c[j][1], rsa_bits[j], RSA_SECONDS);\n Time_F(START);\n for (count = 0, run = 1; COND(rsa_c[j][1]); count++) {\n ret = RSA_verify(NID_md5_sha1, buf, 36, buf2,\n rsa_num, rsa_key[j]);\n if (ret <= 0) {\n BIO_printf(bio_err, "RSA verify failure\\n");\n ERR_print_errors(bio_err);\n count = 1;\n break;\n }\n }\n d = Time_F(STOP);\n BIO_printf(bio_err,\n mr ? "+R2:%ld:%d:%.2f\\n"\n : "%ld %d bit public RSA\'s in %.2fs\\n",\n count, rsa_bits[j], d);\n rsa_results[j][1] = d / (double)count;\n }\n# endif\n if (rsa_count <= 1) {\n for (j++; j < RSA_NUM; j++)\n rsa_doit[j] = 0;\n }\n }\n# endif\n RAND_pseudo_bytes(buf, 20);\n# ifndef OPENSSL_NO_DSA\n if (RAND_status() != 1) {\n RAND_seed(rnd_seed, sizeof rnd_seed);\n rnd_fake = 1;\n }\n for (j = 0; j < DSA_NUM; j++) {\n unsigned int kk;\n int ret;\n if (!dsa_doit[j])\n continue;\n ret = DSA_sign(EVP_PKEY_DSA, buf, 20, buf2, &kk, dsa_key[j]);\n if (ret == 0) {\n BIO_printf(bio_err,\n "DSA sign failure. No DSA sign will be done.\\n");\n ERR_print_errors(bio_err);\n rsa_count = 1;\n } else {\n pkey_print_message("sign", "dsa",\n dsa_c[j][0], dsa_bits[j], DSA_SECONDS);\n Time_F(START);\n for (count = 0, run = 1; COND(dsa_c[j][0]); count++) {\n ret = DSA_sign(EVP_PKEY_DSA, buf, 20, buf2, &kk, dsa_key[j]);\n if (ret == 0) {\n BIO_printf(bio_err, "DSA sign failure\\n");\n ERR_print_errors(bio_err);\n count = 1;\n break;\n }\n }\n d = Time_F(STOP);\n BIO_printf(bio_err,\n mr ? "+R3:%ld:%d:%.2f\\n"\n : "%ld %d bit DSA signs in %.2fs\\n",\n count, dsa_bits[j], d);\n dsa_results[j][0] = d / (double)count;\n rsa_count = count;\n }\n ret = DSA_verify(EVP_PKEY_DSA, buf, 20, buf2, kk, dsa_key[j]);\n if (ret <= 0) {\n BIO_printf(bio_err,\n "DSA verify failure. No DSA verify will be done.\\n");\n ERR_print_errors(bio_err);\n dsa_doit[j] = 0;\n } else {\n pkey_print_message("verify", "dsa",\n dsa_c[j][1], dsa_bits[j], DSA_SECONDS);\n Time_F(START);\n for (count = 0, run = 1; COND(dsa_c[j][1]); count++) {\n ret = DSA_verify(EVP_PKEY_DSA, buf, 20, buf2, kk, dsa_key[j]);\n if (ret <= 0) {\n BIO_printf(bio_err, "DSA verify failure\\n");\n ERR_print_errors(bio_err);\n count = 1;\n break;\n }\n }\n d = Time_F(STOP);\n BIO_printf(bio_err,\n mr ? "+R4:%ld:%d:%.2f\\n"\n : "%ld %d bit DSA verify in %.2fs\\n",\n count, dsa_bits[j], d);\n dsa_results[j][1] = d / (double)count;\n }\n if (rsa_count <= 1) {\n for (j++; j < DSA_NUM; j++)\n dsa_doit[j] = 0;\n }\n }\n if (rnd_fake)\n RAND_cleanup();\n# endif\n# ifndef OPENSSL_NO_ECDSA\n if (RAND_status() != 1) {\n RAND_seed(rnd_seed, sizeof rnd_seed);\n rnd_fake = 1;\n }\n for (j = 0; j < EC_NUM; j++) {\n int ret;\n if (!ecdsa_doit[j])\n continue;\n ecdsa[j] = EC_KEY_new_by_curve_name(test_curves[j]);\n if (ecdsa[j] == NULL) {\n BIO_printf(bio_err, "ECDSA failure.\\n");\n ERR_print_errors(bio_err);\n rsa_count = 1;\n } else {\n# if 1\n EC_KEY_precompute_mult(ecdsa[j], NULL);\n# endif\n EC_KEY_generate_key(ecdsa[j]);\n ret = ECDSA_sign(0, buf, 20, ecdsasig, &ecdsasiglen, ecdsa[j]);\n if (ret == 0) {\n BIO_printf(bio_err,\n "ECDSA sign failure. No ECDSA sign will be done.\\n");\n ERR_print_errors(bio_err);\n rsa_count = 1;\n } else {\n pkey_print_message("sign", "ecdsa",\n ecdsa_c[j][0],\n test_curves_bits[j], ECDSA_SECONDS);\n Time_F(START);\n for (count = 0, run = 1; COND(ecdsa_c[j][0]); count++) {\n ret = ECDSA_sign(0, buf, 20,\n ecdsasig, &ecdsasiglen, ecdsa[j]);\n if (ret == 0) {\n BIO_printf(bio_err, "ECDSA sign failure\\n");\n ERR_print_errors(bio_err);\n count = 1;\n break;\n }\n }\n d = Time_F(STOP);\n BIO_printf(bio_err,\n mr ? "+R5:%ld:%d:%.2f\\n" :\n "%ld %d bit ECDSA signs in %.2fs \\n",\n count, test_curves_bits[j], d);\n ecdsa_results[j][0] = d / (double)count;\n rsa_count = count;\n }\n ret = ECDSA_verify(0, buf, 20, ecdsasig, ecdsasiglen, ecdsa[j]);\n if (ret != 1) {\n BIO_printf(bio_err,\n "ECDSA verify failure. No ECDSA verify will be done.\\n");\n ERR_print_errors(bio_err);\n ecdsa_doit[j] = 0;\n } else {\n pkey_print_message("verify", "ecdsa",\n ecdsa_c[j][1],\n test_curves_bits[j], ECDSA_SECONDS);\n Time_F(START);\n for (count = 0, run = 1; COND(ecdsa_c[j][1]); count++) {\n ret =\n ECDSA_verify(0, buf, 20, ecdsasig, ecdsasiglen,\n ecdsa[j]);\n if (ret != 1) {\n BIO_printf(bio_err, "ECDSA verify failure\\n");\n ERR_print_errors(bio_err);\n count = 1;\n break;\n }\n }\n d = Time_F(STOP);\n BIO_printf(bio_err,\n mr ? "+R6:%ld:%d:%.2f\\n"\n : "%ld %d bit ECDSA verify in %.2fs\\n",\n count, test_curves_bits[j], d);\n ecdsa_results[j][1] = d / (double)count;\n }\n if (rsa_count <= 1) {\n for (j++; j < EC_NUM; j++)\n ecdsa_doit[j] = 0;\n }\n }\n }\n if (rnd_fake)\n RAND_cleanup();\n# endif\n# ifndef OPENSSL_NO_ECDH\n if (RAND_status() != 1) {\n RAND_seed(rnd_seed, sizeof rnd_seed);\n rnd_fake = 1;\n }\n for (j = 0; j < EC_NUM; j++) {\n if (!ecdh_doit[j])\n continue;\n ecdh_a[j] = EC_KEY_new_by_curve_name(test_curves[j]);\n ecdh_b[j] = EC_KEY_new_by_curve_name(test_curves[j]);\n if ((ecdh_a[j] == NULL) || (ecdh_b[j] == NULL)) {\n BIO_printf(bio_err, "ECDH failure.\\n");\n ERR_print_errors(bio_err);\n rsa_count = 1;\n } else {\n if (!EC_KEY_generate_key(ecdh_a[j]) ||\n !EC_KEY_generate_key(ecdh_b[j])) {\n BIO_printf(bio_err, "ECDH key generation failure.\\n");\n ERR_print_errors(bio_err);\n rsa_count = 1;\n } else {\n int field_size, outlen;\n void *(*kdf) (const void *in, size_t inlen, void *out,\n size_t *xoutlen);\n field_size =\n EC_GROUP_get_degree(EC_KEY_get0_group(ecdh_a[j]));\n if (field_size <= 24 * 8) {\n outlen = KDF1_SHA1_len;\n kdf = KDF1_SHA1;\n } else {\n outlen = (field_size + 7) / 8;\n kdf = NULL;\n }\n secret_size_a =\n ECDH_compute_key(secret_a, outlen,\n EC_KEY_get0_public_key(ecdh_b[j]),\n ecdh_a[j], kdf);\n secret_size_b =\n ECDH_compute_key(secret_b, outlen,\n EC_KEY_get0_public_key(ecdh_a[j]),\n ecdh_b[j], kdf);\n if (secret_size_a != secret_size_b)\n ecdh_checks = 0;\n else\n ecdh_checks = 1;\n for (secret_idx = 0; (secret_idx < secret_size_a)\n && (ecdh_checks == 1); secret_idx++) {\n if (secret_a[secret_idx] != secret_b[secret_idx])\n ecdh_checks = 0;\n }\n if (ecdh_checks == 0) {\n BIO_printf(bio_err, "ECDH computations don\'t match.\\n");\n ERR_print_errors(bio_err);\n rsa_count = 1;\n }\n pkey_print_message("", "ecdh",\n ecdh_c[j][0],\n test_curves_bits[j], ECDH_SECONDS);\n Time_F(START);\n for (count = 0, run = 1; COND(ecdh_c[j][0]); count++) {\n ECDH_compute_key(secret_a, outlen,\n EC_KEY_get0_public_key(ecdh_b[j]),\n ecdh_a[j], kdf);\n }\n d = Time_F(STOP);\n BIO_printf(bio_err,\n mr ? "+R7:%ld:%d:%.2f\\n" :\n "%ld %d-bit ECDH ops in %.2fs\\n", count,\n test_curves_bits[j], d);\n ecdh_results[j][0] = d / (double)count;\n rsa_count = count;\n }\n }\n if (rsa_count <= 1) {\n for (j++; j < EC_NUM; j++)\n ecdh_doit[j] = 0;\n }\n }\n if (rnd_fake)\n RAND_cleanup();\n# endif\n# ifndef NO_FORK\n show_res:\n# endif\n if (!mr) {\n fprintf(stdout, "%s\\n", SSLeay_version(SSLEAY_VERSION));\n fprintf(stdout, "%s\\n", SSLeay_version(SSLEAY_BUILT_ON));\n printf("options:");\n printf("%s ", BN_options());\n# ifndef OPENSSL_NO_MD2\n printf("%s ", MD2_options());\n# endif\n# ifndef OPENSSL_NO_RC4\n printf("%s ", RC4_options());\n# endif\n# ifndef OPENSSL_NO_DES\n printf("%s ", DES_options());\n# endif\n# ifndef OPENSSL_NO_AES\n printf("%s ", AES_options());\n# endif\n# ifndef OPENSSL_NO_IDEA\n printf("%s ", idea_options());\n# endif\n# ifndef OPENSSL_NO_BF\n printf("%s ", BF_options());\n# endif\n fprintf(stdout, "\\n%s\\n", SSLeay_version(SSLEAY_CFLAGS));\n }\n if (pr_header) {\n if (mr)\n fprintf(stdout, "+H");\n else {\n fprintf(stdout,\n "The \'numbers\' are in 1000s of bytes per second processed.\\n");\n fprintf(stdout, "type ");\n }\n for (j = 0; j < SIZE_NUM; j++)\n fprintf(stdout, mr ? ":%d" : "%7d bytes", lengths[j]);\n fprintf(stdout, "\\n");\n }\n for (k = 0; k < ALGOR_NUM; k++) {\n if (!doit[k])\n continue;\n if (mr)\n fprintf(stdout, "+F:%d:%s", k, names[k]);\n else\n fprintf(stdout, "%-13s", names[k]);\n for (j = 0; j < SIZE_NUM; j++) {\n if (results[k][j] > 10000 && !mr)\n fprintf(stdout, " %11.2fk", results[k][j] / 1e3);\n else\n fprintf(stdout, mr ? ":%.2f" : " %11.2f ", results[k][j]);\n }\n fprintf(stdout, "\\n");\n }\n# ifndef OPENSSL_NO_RSA\n j = 1;\n for (k = 0; k < RSA_NUM; k++) {\n if (!rsa_doit[k])\n continue;\n if (j && !mr) {\n printf("%18ssign verify sign/s verify/s\\n", " ");\n j = 0;\n }\n if (mr)\n fprintf(stdout, "+F2:%u:%u:%f:%f\\n",\n k, rsa_bits[k], rsa_results[k][0], rsa_results[k][1]);\n else\n fprintf(stdout, "rsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\\n",\n rsa_bits[k], rsa_results[k][0], rsa_results[k][1],\n 1.0 / rsa_results[k][0], 1.0 / rsa_results[k][1]);\n }\n# endif\n# ifndef OPENSSL_NO_DSA\n j = 1;\n for (k = 0; k < DSA_NUM; k++) {\n if (!dsa_doit[k])\n continue;\n if (j && !mr) {\n printf("%18ssign verify sign/s verify/s\\n", " ");\n j = 0;\n }\n if (mr)\n fprintf(stdout, "+F3:%u:%u:%f:%f\\n",\n k, dsa_bits[k], dsa_results[k][0], dsa_results[k][1]);\n else\n fprintf(stdout, "dsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\\n",\n dsa_bits[k], dsa_results[k][0], dsa_results[k][1],\n 1.0 / dsa_results[k][0], 1.0 / dsa_results[k][1]);\n }\n# endif\n# ifndef OPENSSL_NO_ECDSA\n j = 1;\n for (k = 0; k < EC_NUM; k++) {\n if (!ecdsa_doit[k])\n continue;\n if (j && !mr) {\n printf("%30ssign verify sign/s verify/s\\n", " ");\n j = 0;\n }\n if (mr)\n fprintf(stdout, "+F4:%u:%u:%f:%f\\n",\n k, test_curves_bits[k],\n ecdsa_results[k][0], ecdsa_results[k][1]);\n else\n fprintf(stdout,\n "%4u bit ecdsa (%s) %8.4fs %8.4fs %8.1f %8.1f\\n",\n test_curves_bits[k],\n test_curves_names[k],\n ecdsa_results[k][0], ecdsa_results[k][1],\n 1.0 / ecdsa_results[k][0], 1.0 / ecdsa_results[k][1]);\n }\n# endif\n# ifndef OPENSSL_NO_ECDH\n j = 1;\n for (k = 0; k < EC_NUM; k++) {\n if (!ecdh_doit[k])\n continue;\n if (j && !mr) {\n printf("%30sop op/s\\n", " ");\n j = 0;\n }\n if (mr)\n fprintf(stdout, "+F5:%u:%u:%f:%f\\n",\n k, test_curves_bits[k],\n ecdh_results[k][0], 1.0 / ecdh_results[k][0]);\n else\n fprintf(stdout, "%4u bit ecdh (%s) %8.4fs %8.1f\\n",\n test_curves_bits[k],\n test_curves_names[k],\n ecdh_results[k][0], 1.0 / ecdh_results[k][0]);\n }\n# endif\n mret = 0;\n end:\n ERR_print_errors(bio_err);\n if (buf_malloc != NULL)\n OPENSSL_free(buf_malloc);\n if (buf2_malloc != NULL)\n OPENSSL_free(buf2_malloc);\n# ifndef OPENSSL_NO_RSA\n for (i = 0; i < RSA_NUM; i++)\n if (rsa_key[i] != NULL)\n RSA_free(rsa_key[i]);\n# endif\n# ifndef OPENSSL_NO_DSA\n for (i = 0; i < DSA_NUM; i++)\n if (dsa_key[i] != NULL)\n DSA_free(dsa_key[i]);\n# endif\n# ifndef OPENSSL_NO_ECDSA\n for (i = 0; i < EC_NUM; i++)\n if (ecdsa[i] != NULL)\n EC_KEY_free(ecdsa[i]);\n# endif\n# ifndef OPENSSL_NO_ECDH\n for (i = 0; i < EC_NUM; i++) {\n if (ecdh_a[i] != NULL)\n EC_KEY_free(ecdh_a[i]);\n if (ecdh_b[i] != NULL)\n EC_KEY_free(ecdh_b[i]);\n }\n# endif\n apps_shutdown();\n OPENSSL_EXIT(mret);\n}']
|
2,625
| 0
|
https://github.com/openssl/openssl/blob/24b8e4b2c835d6bf52c2768d4d4a78ed7d7e85bb/ssl/packet.c/#L49
|
int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
{
assert(pkt->subs != NULL && len != 0);
if (pkt->subs == NULL || len == 0)
return 0;
if (pkt->maxsize - pkt->written < len)
return 0;
if (pkt->staticbuf == NULL && (pkt->buf->length - pkt->written < len)) {
size_t newlen;
size_t reflen;
reflen = (len > pkt->buf->length) ? len : pkt->buf->length;
if (reflen > SIZE_MAX / 2) {
newlen = SIZE_MAX;
} else {
newlen = reflen * 2;
if (newlen < DEFAULT_BUF_SIZE)
newlen = DEFAULT_BUF_SIZE;
}
if (BUF_MEM_grow(pkt->buf, newlen) == 0)
return 0;
}
if (allocbytes != NULL)
*allocbytes = WPACKET_get_curr(pkt);
return 1;
}
|
['int tls_construct_server_use_srtp(SSL *s, WPACKET *pkt, int *al)\n{\n if (s->srtp_profile == NULL)\n return 1;\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp)\n || !WPACKET_start_sub_packet_u16(pkt)\n || !WPACKET_put_bytes_u16(pkt, 2)\n || !WPACKET_put_bytes_u16(pkt, s->srtp_profile->id)\n || !WPACKET_put_bytes_u8(pkt, 0)\n || !WPACKET_close(pkt)) {\n SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_USE_SRTP, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n return 1;\n}', 'int WPACKET_put_bytes__(WPACKET *pkt, unsigned int val, size_t size)\n{\n unsigned char *data;\n assert(size <= sizeof(unsigned int));\n if (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 assert(pkt->subs != NULL && len != 0);\n if (pkt->subs == NULL || len == 0)\n return 0;\n if (pkt->maxsize - pkt->written < len)\n return 0;\n if (pkt->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}']
|
2,626
| 0
|
https://github.com/libav/libav/blob/4860abb116674c7be31e825db05cdcfd30f3aff2/libavformat/gxf.c/#L88
|
static int get_sindex(AVFormatContext *s, int id, int format) {
int i;
AVStream *st = NULL;
for (i = 0; i < s->nb_streams; i++) {
if (s->streams[i]->id == id)
return i;
}
st = av_new_stream(s, id);
switch (format) {
case 3:
case 4:
st->codec->codec_type = CODEC_TYPE_VIDEO;
st->codec->codec_id = CODEC_ID_MJPEG;
break;
case 13:
case 15:
st->codec->codec_type = CODEC_TYPE_VIDEO;
st->codec->codec_id = CODEC_ID_DVVIDEO;
break;
case 14:
case 16:
st->codec->codec_type = CODEC_TYPE_VIDEO;
st->codec->codec_id = CODEC_ID_DVVIDEO;
break;
case 11:
case 12:
case 20:
st->codec->codec_type = CODEC_TYPE_VIDEO;
st->codec->codec_id = CODEC_ID_MPEG2VIDEO;
st->need_parsing = AVSTREAM_PARSE_HEADERS;
break;
case 22:
case 23:
st->codec->codec_type = CODEC_TYPE_VIDEO;
st->codec->codec_id = CODEC_ID_MPEG1VIDEO;
st->need_parsing = AVSTREAM_PARSE_HEADERS;
break;
case 9:
st->codec->codec_type = CODEC_TYPE_AUDIO;
st->codec->codec_id = CODEC_ID_PCM_S24LE;
st->codec->channels = 1;
st->codec->sample_rate = 48000;
st->codec->bit_rate = 3 * 1 * 48000 * 8;
st->codec->block_align = 3 * 1;
st->codec->bits_per_sample = 24;
break;
case 10:
st->codec->codec_type = CODEC_TYPE_AUDIO;
st->codec->codec_id = CODEC_ID_PCM_S16LE;
st->codec->channels = 1;
st->codec->sample_rate = 48000;
st->codec->bit_rate = 2 * 1 * 48000 * 8;
st->codec->block_align = 2 * 1;
st->codec->bits_per_sample = 16;
break;
case 17:
st->codec->codec_type = CODEC_TYPE_AUDIO;
st->codec->codec_id = CODEC_ID_AC3;
st->codec->channels = 2;
st->codec->sample_rate = 48000;
break;
case 7:
case 8:
case 24:
st->codec->codec_type = CODEC_TYPE_DATA;
st->codec->codec_id = CODEC_ID_NONE;
break;
default:
st->codec->codec_type = CODEC_TYPE_UNKNOWN;
st->codec->codec_id = CODEC_ID_NONE;
break;
}
return s->nb_streams - 1;
}
|
['static int get_sindex(AVFormatContext *s, int id, int format) {\n int i;\n AVStream *st = NULL;\n for (i = 0; i < s->nb_streams; i++) {\n if (s->streams[i]->id == id)\n return i;\n }\n st = av_new_stream(s, id);\n switch (format) {\n case 3:\n case 4:\n st->codec->codec_type = CODEC_TYPE_VIDEO;\n st->codec->codec_id = CODEC_ID_MJPEG;\n break;\n case 13:\n case 15:\n st->codec->codec_type = CODEC_TYPE_VIDEO;\n st->codec->codec_id = CODEC_ID_DVVIDEO;\n break;\n case 14:\n case 16:\n st->codec->codec_type = CODEC_TYPE_VIDEO;\n st->codec->codec_id = CODEC_ID_DVVIDEO;\n break;\n case 11:\n case 12:\n case 20:\n st->codec->codec_type = CODEC_TYPE_VIDEO;\n st->codec->codec_id = CODEC_ID_MPEG2VIDEO;\n st->need_parsing = AVSTREAM_PARSE_HEADERS;\n break;\n case 22:\n case 23:\n st->codec->codec_type = CODEC_TYPE_VIDEO;\n st->codec->codec_id = CODEC_ID_MPEG1VIDEO;\n st->need_parsing = AVSTREAM_PARSE_HEADERS;\n break;\n case 9:\n st->codec->codec_type = CODEC_TYPE_AUDIO;\n st->codec->codec_id = CODEC_ID_PCM_S24LE;\n st->codec->channels = 1;\n st->codec->sample_rate = 48000;\n st->codec->bit_rate = 3 * 1 * 48000 * 8;\n st->codec->block_align = 3 * 1;\n st->codec->bits_per_sample = 24;\n break;\n case 10:\n st->codec->codec_type = CODEC_TYPE_AUDIO;\n st->codec->codec_id = CODEC_ID_PCM_S16LE;\n st->codec->channels = 1;\n st->codec->sample_rate = 48000;\n st->codec->bit_rate = 2 * 1 * 48000 * 8;\n st->codec->block_align = 2 * 1;\n st->codec->bits_per_sample = 16;\n break;\n case 17:\n st->codec->codec_type = CODEC_TYPE_AUDIO;\n st->codec->codec_id = CODEC_ID_AC3;\n st->codec->channels = 2;\n st->codec->sample_rate = 48000;\n break;\n case 7:\n case 8:\n case 24:\n st->codec->codec_type = CODEC_TYPE_DATA;\n st->codec->codec_id = CODEC_ID_NONE;\n break;\n default:\n st->codec->codec_type = CODEC_TYPE_UNKNOWN;\n st->codec->codec_id = CODEC_ID_NONE;\n break;\n }\n return s->nb_streams - 1;\n}', 'AVStream *av_new_stream(AVFormatContext *s, int id)\n{\n AVStream *st;\n int i;\n if (s->nb_streams >= MAX_STREAMS)\n return NULL;\n st = av_mallocz(sizeof(AVStream));\n if (!st)\n return NULL;\n st->codec= avcodec_alloc_context();\n if (s->iformat) {\n st->codec->bit_rate = 0;\n }\n st->index = s->nb_streams;\n st->id = id;\n st->start_time = AV_NOPTS_VALUE;\n st->duration = AV_NOPTS_VALUE;\n st->cur_dts = AV_NOPTS_VALUE;\n st->first_dts = AV_NOPTS_VALUE;\n av_set_pts_info(st, 33, 1, 90000);\n st->last_IP_pts = AV_NOPTS_VALUE;\n for(i=0; i<MAX_REORDER_DELAY+1; i++)\n st->pts_buffer[i]= AV_NOPTS_VALUE;\n s->streams[s->nb_streams++] = st;\n return st;\n}']
|
2,627
| 0
|
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/test/bntest.c/#L1050
|
int test_mod_exp(BIO *bp, BN_CTX *ctx)
{
BIGNUM *a, *b, *c, *d, *e;
int i;
a = BN_new();
b = BN_new();
c = BN_new();
d = BN_new();
e = BN_new();
BN_one(a);
BN_one(b);
BN_zero(c);
if (BN_mod_exp(d, a, b, c, ctx)) {
fprintf(stderr, "BN_mod_exp with zero modulus succeeded!\n");
return 0;
}
BN_bntest_rand(c, 30, 0, 1);
for (i = 0; i < num2; i++) {
BN_bntest_rand(a, 20 + i * 5, 0, 0);
BN_bntest_rand(b, 2 + i, 0, 0);
if (!BN_mod_exp(d, a, b, c, ctx))
return (0);
if (bp != NULL) {
if (!results) {
BN_print(bp, a);
BIO_puts(bp, " ^ ");
BN_print(bp, b);
BIO_puts(bp, " % ");
BN_print(bp, c);
BIO_puts(bp, " - ");
}
BN_print(bp, d);
BIO_puts(bp, "\n");
}
BN_exp(e, a, b, ctx);
BN_sub(e, e, d);
BN_div(a, b, e, c, ctx);
if (!BN_is_zero(b)) {
fprintf(stderr, "Modulo exponentiation test failed!\n");
return 0;
}
}
BN_hex2bn(&a, "050505050505");
BN_hex2bn(&b, "02");
BN_hex2bn(&c,
"4141414141414141414141274141414141414141414141414141414141414141"
"4141414141414141414141414141414141414141414141414141414141414141"
"4141414141414141414141800000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000001");
BN_mod_exp(d, a, b, c, ctx);
BN_mul(e, a, a, ctx);
if (BN_cmp(d, e)) {
fprintf(stderr, "BN_mod_exp and BN_mul produce different results!\n");
return 0;
}
BN_free(a);
BN_free(b);
BN_free(c);
BN_free(d);
BN_free(e);
return (1);
}
|
['int test_mod_exp(BIO *bp, BN_CTX *ctx)\n{\n BIGNUM *a, *b, *c, *d, *e;\n int i;\n a = BN_new();\n b = BN_new();\n c = BN_new();\n d = BN_new();\n e = BN_new();\n BN_one(a);\n BN_one(b);\n BN_zero(c);\n if (BN_mod_exp(d, a, b, c, ctx)) {\n fprintf(stderr, "BN_mod_exp with zero modulus succeeded!\\n");\n return 0;\n }\n BN_bntest_rand(c, 30, 0, 1);\n for (i = 0; i < num2; i++) {\n BN_bntest_rand(a, 20 + i * 5, 0, 0);\n BN_bntest_rand(b, 2 + i, 0, 0);\n if (!BN_mod_exp(d, a, b, c, ctx))\n return (0);\n if (bp != NULL) {\n if (!results) {\n BN_print(bp, a);\n BIO_puts(bp, " ^ ");\n BN_print(bp, b);\n BIO_puts(bp, " % ");\n BN_print(bp, c);\n BIO_puts(bp, " - ");\n }\n BN_print(bp, d);\n BIO_puts(bp, "\\n");\n }\n BN_exp(e, a, b, ctx);\n BN_sub(e, e, d);\n BN_div(a, b, e, c, ctx);\n if (!BN_is_zero(b)) {\n fprintf(stderr, "Modulo exponentiation test failed!\\n");\n return 0;\n }\n }\n BN_hex2bn(&a, "050505050505");\n BN_hex2bn(&b, "02");\n BN_hex2bn(&c,\n "4141414141414141414141274141414141414141414141414141414141414141"\n "4141414141414141414141414141414141414141414141414141414141414141"\n "4141414141414141414141800000000000000000000000000000000000000000"\n "0000000000000000000000000000000000000000000000000000000000000000"\n "0000000000000000000000000000000000000000000000000000000000000000"\n "0000000000000000000000000000000000000000000000000000000001");\n BN_mod_exp(d, a, b, c, ctx);\n BN_mul(e, a, a, ctx);\n if (BN_cmp(d, e)) {\n fprintf(stderr, "BN_mod_exp and BN_mul produce different results!\\n");\n return 0;\n }\n BN_free(a);\n BN_free(b);\n BN_free(c);\n BN_free(d);\n BN_free(e);\n return (1);\n}', '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}', '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}']
|
2,628
| 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 void synthfilt_build_sb_samples(QDM2Context *q, BitstreamContext *bc,\n int length, int sb_min, int sb_max)\n{\n int sb, j, k, n, ch, run, channels;\n int joined_stereo, zero_encoding;\n int type34_first;\n float type34_div = 0;\n float type34_predictor;\n float samples[10], sign_bits[16];\n if (length == 0) {\n for (sb=sb_min; sb < sb_max; sb++)\n build_sb_samples_from_noise(q, sb);\n return;\n }\n for (sb = sb_min; sb < sb_max; sb++) {\n channels = q->nb_channels;\n if (q->nb_channels <= 1 || sb < 12)\n joined_stereo = 0;\n else if (sb >= 24)\n joined_stereo = 1;\n else\n joined_stereo = (bitstream_bits_left(bc) >= 1) ? bitstream_read_bit(bc) : 0;\n if (joined_stereo) {\n if (bitstream_bits_left(bc) >= 16)\n for (j = 0; j < 16; j++)\n sign_bits[j] = bitstream_read_bit(bc);\n for (j = 0; j < 64; j++)\n if (q->coding_method[1][sb][j] > q->coding_method[0][sb][j])\n q->coding_method[0][sb][j] = q->coding_method[1][sb][j];\n if (fix_coding_method_array(sb, q->nb_channels,\n q->coding_method)) {\n build_sb_samples_from_noise(q, sb);\n continue;\n }\n channels = 1;\n }\n for (ch = 0; ch < channels; ch++) {\n FIX_NOISE_IDX(q->noise_idx);\n zero_encoding = (bitstream_bits_left(bc) >= 1) ? bitstream_read_bit(bc) : 0;\n type34_predictor = 0.0;\n type34_first = 1;\n for (j = 0; j < 128; ) {\n switch (q->coding_method[ch][sb][j / 2]) {\n case 8:\n if (bitstream_bits_left(bc) >= 10) {\n if (zero_encoding) {\n for (k = 0; k < 5; k++) {\n if ((j + 2 * k) >= 128)\n break;\n samples[2 * k] = bitstream_read_bit(bc) ? dequant_1bit[joined_stereo][2 * bitstream_read_bit(bc)] : 0;\n }\n } else {\n n = bitstream_read(bc, 8);\n for (k = 0; k < 5; k++)\n samples[2 * k] = dequant_1bit[joined_stereo][random_dequant_index[n][k]];\n }\n for (k = 0; k < 5; k++)\n samples[2 * k + 1] = SB_DITHERING_NOISE(sb,q->noise_idx);\n } else {\n for (k = 0; k < 10; k++)\n samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);\n }\n run = 10;\n break;\n case 10:\n if (bitstream_bits_left(bc) >= 1) {\n float f = 0.81;\n if (bitstream_read_bit(bc))\n f = -f;\n f -= noise_samples[((sb + 1) * (j +5 * ch + 1)) & 127] * 9.0 / 40.0;\n samples[0] = f;\n } else {\n samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);\n }\n run = 1;\n break;\n case 16:\n if (bitstream_bits_left(bc) >= 10) {\n if (zero_encoding) {\n for (k = 0; k < 5; k++) {\n if ((j + k) >= 128)\n break;\n samples[k] = (bitstream_read_bit(bc) == 0) ? 0 : dequant_1bit[joined_stereo][2 * bitstream_read_bit(bc)];\n }\n } else {\n n = bitstream_read (bc, 8);\n for (k = 0; k < 5; k++)\n samples[k] = dequant_1bit[joined_stereo][random_dequant_index[n][k]];\n }\n } else {\n for (k = 0; k < 5; k++)\n samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);\n }\n run = 5;\n break;\n case 24:\n if (bitstream_bits_left(bc) >= 7) {\n n = bitstream_read(bc, 7);\n for (k = 0; k < 3; k++)\n samples[k] = (random_dequant_type24[n][k] - 2.0) * 0.5;\n } else {\n for (k = 0; k < 3; k++)\n samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);\n }\n run = 3;\n break;\n case 30:\n if (bitstream_bits_left(bc) >= 4) {\n unsigned index = qdm2_get_vlc(bc, &vlc_tab_type30, 0, 1);\n if (index < FF_ARRAY_ELEMS(type30_dequant)) {\n samples[0] = type30_dequant[index];\n } else\n samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);\n } else\n samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);\n run = 1;\n break;\n case 34:\n if (bitstream_bits_left(bc) >= 7) {\n if (type34_first) {\n type34_div = (float)(1 << bitstream_read(bc, 2));\n samples[0] = ((float)bitstream_read(bc, 5) - 16.0) / 15.0;\n type34_predictor = samples[0];\n type34_first = 0;\n } else {\n unsigned index = qdm2_get_vlc(bc, &vlc_tab_type34, 0, 1);\n if (index < FF_ARRAY_ELEMS(type34_delta)) {\n samples[0] = type34_delta[index] / type34_div + type34_predictor;\n type34_predictor = samples[0];\n } else\n samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);\n }\n } else {\n samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);\n }\n run = 1;\n break;\n default:\n samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);\n run = 1;\n break;\n }\n if (joined_stereo) {\n for (k = 0; k < run && j + k < 128; k++) {\n q->sb_samples[0][j + k][sb] =\n q->tone_level[0][sb][(j + k) / 2] * samples[k];\n if (q->nb_channels == 2) {\n if (sign_bits[(j + k) / 8])\n q->sb_samples[1][j + k][sb] =\n q->tone_level[1][sb][(j + k) / 2] * -samples[k];\n else\n q->sb_samples[1][j + k][sb] =\n q->tone_level[1][sb][(j + k) / 2] * samples[k];\n }\n }\n } else {\n for (k = 0; k < run; k++)\n if ((j + k) < 128)\n q->sb_samples[ch][j + k][sb] = q->tone_level[ch][sb][(j + k)/2] * samples[k];\n }\n j += run;\n }\n }\n }\n}', 'static inline unsigned bitstream_read_bit(BitstreamContext *bc)\n{\n if (!bc->bits_left)\n refill_64(bc);\n return get_val(bc, 1);\n}', 'static inline uint64_t get_val(BitstreamContext *bc, unsigned n)\n{\n#ifdef BITSTREAM_READER_LE\n uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);\n bc->bits >>= n;\n#else\n uint64_t ret = bc->bits >> (64 - n);\n bc->bits <<= n;\n#endif\n bc->bits_left -= n;\n return ret;\n}']
|
2,629
| 0
|
https://github.com/nginx/nginx/blob/e4ecddfdb0d2ffc872658e36028971ad9a873726/src/core/ngx_string.c/#L244
|
u_char *
ngx_vsnprintf(u_char *buf, size_t max, const char *fmt, va_list args)
{
u_char *p, zero, *last;
int d;
float f, scale;
size_t len, slen;
int64_t i64;
uint64_t ui64;
ngx_msec_t ms;
ngx_uint_t width, sign, hex, max_width, frac_width, i;
ngx_str_t *v;
ngx_variable_value_t *vv;
if (max == 0) {
return buf;
}
last = buf + max;
while (*fmt && buf < last) {
if (*fmt == '%') {
i64 = 0;
ui64 = 0;
zero = (u_char) ((*++fmt == '0') ? '0' : ' ');
width = 0;
sign = 1;
hex = 0;
max_width = 0;
frac_width = 0;
slen = (size_t) -1;
while (*fmt >= '0' && *fmt <= '9') {
width = width * 10 + *fmt++ - '0';
}
for ( ;; ) {
switch (*fmt) {
case 'u':
sign = 0;
fmt++;
continue;
case 'm':
max_width = 1;
fmt++;
continue;
case 'X':
hex = 2;
sign = 0;
fmt++;
continue;
case 'x':
hex = 1;
sign = 0;
fmt++;
continue;
case '.':
fmt++;
while (*fmt >= '0' && *fmt <= '9') {
frac_width = frac_width * 10 + *fmt++ - '0';
}
break;
case '*':
slen = va_arg(args, size_t);
fmt++;
continue;
default:
break;
}
break;
}
switch (*fmt) {
case 'V':
v = va_arg(args, ngx_str_t *);
len = v->len;
len = (buf + len < last) ? len : (size_t) (last - buf);
buf = ngx_cpymem(buf, v->data, len);
fmt++;
continue;
case 'v':
vv = va_arg(args, ngx_variable_value_t *);
len = vv->len;
len = (buf + len < last) ? len : (size_t) (last - buf);
buf = ngx_cpymem(buf, vv->data, len);
fmt++;
continue;
case 's':
p = va_arg(args, u_char *);
if (slen == (size_t) -1) {
while (*p && buf < last) {
*buf++ = *p++;
}
} else {
len = (buf + slen < last) ? slen : (size_t) (last - buf);
buf = ngx_cpymem(buf, p, len);
}
fmt++;
continue;
case 'O':
i64 = (int64_t) va_arg(args, off_t);
sign = 1;
break;
case 'P':
i64 = (int64_t) va_arg(args, ngx_pid_t);
sign = 1;
break;
case 'T':
i64 = (int64_t) va_arg(args, time_t);
sign = 1;
break;
case 'M':
ms = (ngx_msec_t) va_arg(args, ngx_msec_t);
if ((ngx_msec_int_t) ms == -1) {
sign = 1;
i64 = -1;
} else {
sign = 0;
ui64 = (uint64_t) ms;
}
break;
case 'z':
if (sign) {
i64 = (int64_t) va_arg(args, ssize_t);
} else {
ui64 = (uint64_t) va_arg(args, size_t);
}
break;
case 'i':
if (sign) {
i64 = (int64_t) va_arg(args, ngx_int_t);
} else {
ui64 = (uint64_t) va_arg(args, ngx_uint_t);
}
if (max_width) {
width = NGX_INT_T_LEN;
}
break;
case 'd':
if (sign) {
i64 = (int64_t) va_arg(args, int);
} else {
ui64 = (uint64_t) va_arg(args, u_int);
}
break;
case 'l':
if (sign) {
i64 = (int64_t) va_arg(args, long);
} else {
ui64 = (uint64_t) va_arg(args, u_long);
}
break;
case 'D':
if (sign) {
i64 = (int64_t) va_arg(args, int32_t);
} else {
ui64 = (uint64_t) va_arg(args, uint32_t);
}
break;
case 'L':
if (sign) {
i64 = va_arg(args, int64_t);
} else {
ui64 = va_arg(args, uint64_t);
}
break;
case 'A':
if (sign) {
i64 = (int64_t) va_arg(args, ngx_atomic_int_t);
} else {
ui64 = (uint64_t) va_arg(args, ngx_atomic_uint_t);
}
if (max_width) {
width = NGX_ATOMIC_T_LEN;
}
break;
case 'f':
f = (float) va_arg(args, double);
if (f < 0) {
*buf++ = '-';
f = -f;
}
ui64 = (int64_t) f;
buf = ngx_sprintf_num(buf, last, ui64, zero, 0, width);
if (frac_width) {
if (buf < last) {
*buf++ = '.';
}
scale = 1.0;
for (i = 0; i < frac_width; i++) {
scale *= 10.0;
}
ui64 = (uint64_t) ((f - (int64_t) ui64) * scale);
buf = ngx_sprintf_num(buf, last, ui64, '0', 0, frac_width);
}
fmt++;
continue;
#if !(NGX_WIN32)
case 'r':
i64 = (int64_t) va_arg(args, rlim_t);
sign = 1;
break;
#endif
case 'p':
ui64 = (uintptr_t) va_arg(args, void *);
hex = 2;
sign = 0;
zero = '0';
width = NGX_PTR_SIZE * 2;
break;
case 'c':
d = va_arg(args, int);
*buf++ = (u_char) (d & 0xff);
fmt++;
continue;
case 'Z':
*buf++ = '\0';
fmt++;
continue;
case 'N':
#if (NGX_WIN32)
*buf++ = CR;
#endif
*buf++ = LF;
fmt++;
continue;
case '%':
*buf++ = '%';
fmt++;
continue;
default:
*buf++ = *fmt++;
continue;
}
if (sign) {
if (i64 < 0) {
*buf++ = '-';
ui64 = (uint64_t) -i64;
} else {
ui64 = (uint64_t) i64;
}
}
buf = ngx_sprintf_num(buf, last, ui64, zero, hex, width);
fmt++;
} else {
*buf++ = *fmt++;
}
}
return buf;
}
|
['ngx_int_t\nngx_regex_exec_array(ngx_array_t *a, ngx_str_t *s, ngx_log_t *log)\n{\n ngx_int_t n;\n ngx_uint_t i;\n ngx_regex_elt_t *re;\n re = a->elts;\n for (i = 0; i < a->nelts; i++) {\n n = ngx_regex_exec(re[i].regex, s, NULL, 0);\n if (n == NGX_REGEX_NO_MATCHED) {\n continue;\n }\n if (n < 0) {\n ngx_log_error(NGX_LOG_ALERT, log, 0,\n ngx_regex_exec_n " failed: %d on \\"%V\\" using \\"%s\\"",\n n, s, re[i].name);\n return NGX_ERROR;\n }\n return NGX_OK;\n }\n return NGX_DECLINED;\n}', 'void\nngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,\n const char *fmt, ...)\n#else\nvoid\nngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,\n const char *fmt, va_list args)\n#endif\n{\n#if (NGX_HAVE_VARIADIC_MACROS)\n va_list args;\n#endif\n u_char errstr[NGX_MAX_ERROR_STR], *p, *last;\n if (log->file->fd == NGX_INVALID_FILE) {\n return;\n }\n last = errstr + NGX_MAX_ERROR_STR;\n ngx_memcpy(errstr, ngx_cached_err_log_time.data,\n ngx_cached_err_log_time.len);\n p = errstr + ngx_cached_err_log_time.len;\n p = ngx_snprintf(p, last - p, " [%s] ", err_levels[level]);\n p = ngx_snprintf(p, last - p, "%P#" NGX_TID_T_FMT ": ",\n ngx_log_pid, ngx_log_tid);\n if (log->connection) {\n p = ngx_snprintf(p, last - p, "*%uA ", log->connection);\n }\n#if (NGX_HAVE_VARIADIC_MACROS)\n va_start(args, fmt);\n p = ngx_vsnprintf(p, last - p, fmt, args);\n va_end(args);\n#else\n p = ngx_vsnprintf(p, last - p, fmt, args);\n#endif\n if (err) {\n if (p > last - 50) {\n p = last - 50;\n *p++ = \'.\';\n *p++ = \'.\';\n *p++ = \'.\';\n }\n#if (NGX_WIN32)\n p = ngx_snprintf(p, last - p, ((unsigned) err < 0x80000000)\n ? " (%d: " : " (%Xd: ", err);\n#else\n p = ngx_snprintf(p, last - p, " (%d: ", err);\n#endif\n p = ngx_strerror_r(err, p, last - p);\n if (p < last) {\n *p++ = \')\';\n }\n }\n if (level != NGX_LOG_DEBUG && log->handler) {\n p = log->handler(log, p, last - p);\n }\n if (p > last - NGX_LINEFEED_SIZE) {\n p = last - NGX_LINEFEED_SIZE;\n }\n ngx_linefeed(p);\n (void) ngx_write_fd(log->file->fd, errstr, p - errstr);\n}', 'u_char * ngx_cdecl\nngx_snprintf(u_char *buf, size_t max, const char *fmt, ...)\n{\n u_char *p;\n va_list args;\n va_start(args, fmt);\n p = ngx_vsnprintf(buf, max, fmt, args);\n va_end(args);\n return p;\n}', "u_char *\nngx_vsnprintf(u_char *buf, size_t max, const char *fmt, va_list args)\n{\n u_char *p, zero, *last;\n int d;\n float f, scale;\n size_t len, slen;\n int64_t i64;\n uint64_t ui64;\n ngx_msec_t ms;\n ngx_uint_t width, sign, hex, max_width, frac_width, i;\n ngx_str_t *v;\n ngx_variable_value_t *vv;\n if (max == 0) {\n return buf;\n }\n last = buf + max;\n while (*fmt && buf < last) {\n if (*fmt == '%') {\n i64 = 0;\n ui64 = 0;\n zero = (u_char) ((*++fmt == '0') ? '0' : ' ');\n width = 0;\n sign = 1;\n hex = 0;\n max_width = 0;\n frac_width = 0;\n slen = (size_t) -1;\n while (*fmt >= '0' && *fmt <= '9') {\n width = width * 10 + *fmt++ - '0';\n }\n for ( ;; ) {\n switch (*fmt) {\n case 'u':\n sign = 0;\n fmt++;\n continue;\n case 'm':\n max_width = 1;\n fmt++;\n continue;\n case 'X':\n hex = 2;\n sign = 0;\n fmt++;\n continue;\n case 'x':\n hex = 1;\n sign = 0;\n fmt++;\n continue;\n case '.':\n fmt++;\n while (*fmt >= '0' && *fmt <= '9') {\n frac_width = frac_width * 10 + *fmt++ - '0';\n }\n break;\n case '*':\n slen = va_arg(args, size_t);\n fmt++;\n continue;\n default:\n break;\n }\n break;\n }\n switch (*fmt) {\n case 'V':\n v = va_arg(args, ngx_str_t *);\n len = v->len;\n len = (buf + len < last) ? len : (size_t) (last - buf);\n buf = ngx_cpymem(buf, v->data, len);\n fmt++;\n continue;\n case 'v':\n vv = va_arg(args, ngx_variable_value_t *);\n len = vv->len;\n len = (buf + len < last) ? len : (size_t) (last - buf);\n buf = ngx_cpymem(buf, vv->data, len);\n fmt++;\n continue;\n case 's':\n p = va_arg(args, u_char *);\n if (slen == (size_t) -1) {\n while (*p && buf < last) {\n *buf++ = *p++;\n }\n } else {\n len = (buf + slen < last) ? slen : (size_t) (last - buf);\n buf = ngx_cpymem(buf, p, len);\n }\n fmt++;\n continue;\n case 'O':\n i64 = (int64_t) va_arg(args, off_t);\n sign = 1;\n break;\n case 'P':\n i64 = (int64_t) va_arg(args, ngx_pid_t);\n sign = 1;\n break;\n case 'T':\n i64 = (int64_t) va_arg(args, time_t);\n sign = 1;\n break;\n case 'M':\n ms = (ngx_msec_t) va_arg(args, ngx_msec_t);\n if ((ngx_msec_int_t) ms == -1) {\n sign = 1;\n i64 = -1;\n } else {\n sign = 0;\n ui64 = (uint64_t) ms;\n }\n break;\n case 'z':\n if (sign) {\n i64 = (int64_t) va_arg(args, ssize_t);\n } else {\n ui64 = (uint64_t) va_arg(args, size_t);\n }\n break;\n case 'i':\n if (sign) {\n i64 = (int64_t) va_arg(args, ngx_int_t);\n } else {\n ui64 = (uint64_t) va_arg(args, ngx_uint_t);\n }\n if (max_width) {\n width = NGX_INT_T_LEN;\n }\n break;\n case 'd':\n if (sign) {\n i64 = (int64_t) va_arg(args, int);\n } else {\n ui64 = (uint64_t) va_arg(args, u_int);\n }\n break;\n case 'l':\n if (sign) {\n i64 = (int64_t) va_arg(args, long);\n } else {\n ui64 = (uint64_t) va_arg(args, u_long);\n }\n break;\n case 'D':\n if (sign) {\n i64 = (int64_t) va_arg(args, int32_t);\n } else {\n ui64 = (uint64_t) va_arg(args, uint32_t);\n }\n break;\n case 'L':\n if (sign) {\n i64 = va_arg(args, int64_t);\n } else {\n ui64 = va_arg(args, uint64_t);\n }\n break;\n case 'A':\n if (sign) {\n i64 = (int64_t) va_arg(args, ngx_atomic_int_t);\n } else {\n ui64 = (uint64_t) va_arg(args, ngx_atomic_uint_t);\n }\n if (max_width) {\n width = NGX_ATOMIC_T_LEN;\n }\n break;\n case 'f':\n f = (float) va_arg(args, double);\n if (f < 0) {\n *buf++ = '-';\n f = -f;\n }\n ui64 = (int64_t) f;\n buf = ngx_sprintf_num(buf, last, ui64, zero, 0, width);\n if (frac_width) {\n if (buf < last) {\n *buf++ = '.';\n }\n scale = 1.0;\n for (i = 0; i < frac_width; i++) {\n scale *= 10.0;\n }\n ui64 = (uint64_t) ((f - (int64_t) ui64) * scale);\n buf = ngx_sprintf_num(buf, last, ui64, '0', 0, frac_width);\n }\n fmt++;\n continue;\n#if !(NGX_WIN32)\n case 'r':\n i64 = (int64_t) va_arg(args, rlim_t);\n sign = 1;\n break;\n#endif\n case 'p':\n ui64 = (uintptr_t) va_arg(args, void *);\n hex = 2;\n sign = 0;\n zero = '0';\n width = NGX_PTR_SIZE * 2;\n break;\n case 'c':\n d = va_arg(args, int);\n *buf++ = (u_char) (d & 0xff);\n fmt++;\n continue;\n case 'Z':\n *buf++ = '\\0';\n fmt++;\n continue;\n case 'N':\n#if (NGX_WIN32)\n *buf++ = CR;\n#endif\n *buf++ = LF;\n fmt++;\n continue;\n case '%':\n *buf++ = '%';\n fmt++;\n continue;\n default:\n *buf++ = *fmt++;\n continue;\n }\n if (sign) {\n if (i64 < 0) {\n *buf++ = '-';\n ui64 = (uint64_t) -i64;\n } else {\n ui64 = (uint64_t) i64;\n }\n }\n buf = ngx_sprintf_num(buf, last, ui64, zero, hex, width);\n fmt++;\n } else {\n *buf++ = *fmt++;\n }\n }\n return buf;\n}"]
|
2,630
| 0
|
https://github.com/openssl/openssl/blob/d65c3615f6c658478503f4862f8055203a98038c/crypto/bn/bn_lib.c/#L469
|
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);
}
|
['static int generate_key(DH *dh)\n{\n int ok = 0;\n int generate_new_key = 0;\n unsigned l;\n BN_CTX *ctx;\n BN_MONT_CTX *mont = NULL;\n BIGNUM *pub_key = NULL, *priv_key = NULL;\n ctx = BN_CTX_new();\n if (ctx == NULL)\n goto err;\n if (dh->priv_key == NULL) {\n priv_key = BN_secure_new();\n if (priv_key == NULL)\n goto err;\n generate_new_key = 1;\n } else\n priv_key = dh->priv_key;\n if (dh->pub_key == NULL) {\n pub_key = BN_new();\n if (pub_key == NULL)\n goto err;\n } else\n pub_key = dh->pub_key;\n if (dh->flags & DH_FLAG_CACHE_MONT_P) {\n mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,\n dh->lock, dh->p, ctx);\n if (!mont)\n goto err;\n }\n if (generate_new_key) {\n if (dh->q) {\n do {\n if (!BN_rand_range(priv_key, dh->q))\n goto err;\n }\n while (BN_is_zero(priv_key) || BN_is_one(priv_key));\n } else {\n l = dh->length ? dh->length : BN_num_bits(dh->p) - 1;\n if (!BN_rand(priv_key, l, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))\n goto err;\n }\n }\n {\n BIGNUM *prk = BN_new();\n if (prk == NULL)\n goto err;\n BN_with_flags(prk, priv_key, BN_FLG_CONSTTIME);\n if (!dh->meth->bn_mod_exp(dh, pub_key, dh->g, prk, dh->p, ctx, mont)) {\n BN_free(prk);\n goto err;\n }\n BN_free(prk);\n }\n dh->pub_key = pub_key;\n dh->priv_key = priv_key;\n ok = 1;\n err:\n if (ok != 1)\n DHerr(DH_F_GENERATE_KEY, ERR_R_BN_LIB);\n if (pub_key != dh->pub_key)\n BN_free(pub_key);\n if (priv_key != dh->priv_key)\n BN_free(priv_key);\n BN_CTX_free(ctx);\n return (ok);\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) {\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 time(&tim);\n RAND_add(&tim, sizeof(tim), 0.0);\n if (RAND_bytes(buf, bytes) <= 0)\n goto err;\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);\ntoosmall:\n BNerr(BN_F_BNRAND, BN_R_BITS_TOO_SMALL);\n return 0;\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}']
|
2,631
| 0
|
https://github.com/libav/libav/blob/366ba2dee1f2b17825b42e2164d3b9879f0271b1/libavcodec/h264_loopfilter.c/#L266
|
static av_always_inline void h264_filter_mb_fast_internal(const H264Context *h,
H264SliceContext *sl,
int mb_x, int mb_y,
uint8_t *img_y,
uint8_t *img_cb,
uint8_t *img_cr,
unsigned int linesize,
unsigned int uvlinesize,
int pixel_shift)
{
int chroma = !(CONFIG_GRAY && (h->flags & AV_CODEC_FLAG_GRAY));
int chroma444 = CHROMA444(h);
int chroma422 = CHROMA422(h);
int mb_xy = sl->mb_xy;
int left_type = sl->left_type[LTOP];
int top_type = sl->top_type;
int qp_bd_offset = 6 * (h->ps.sps->bit_depth_luma - 8);
int a = 52 + sl->slice_alpha_c0_offset - qp_bd_offset;
int b = 52 + sl->slice_beta_offset - qp_bd_offset;
int mb_type = h->cur_pic.mb_type[mb_xy];
int qp = h->cur_pic.qscale_table[mb_xy];
int qp0 = h->cur_pic.qscale_table[mb_xy - 1];
int qp1 = h->cur_pic.qscale_table[sl->top_mb_xy];
int qpc = get_chroma_qp( h, 0, qp );
int qpc0 = get_chroma_qp( h, 0, qp0 );
int qpc1 = get_chroma_qp( h, 0, qp1 );
qp0 = (qp + qp0 + 1) >> 1;
qp1 = (qp + qp1 + 1) >> 1;
qpc0 = (qpc + qpc0 + 1) >> 1;
qpc1 = (qpc + qpc1 + 1) >> 1;
if( IS_INTRA(mb_type) ) {
static const int16_t bS4[4] = {4,4,4,4};
static const int16_t bS3[4] = {3,3,3,3};
const int16_t *bSH = FIELD_PICTURE(h) ? bS3 : bS4;
if(left_type)
filter_mb_edgev( &img_y[4*0<<pixel_shift], linesize, bS4, qp0, a, b, h, 1);
if( IS_8x8DCT(mb_type) ) {
filter_mb_edgev( &img_y[4*2<<pixel_shift], linesize, bS3, qp, a, b, h, 0);
if(top_type){
filter_mb_edgeh( &img_y[4*0*linesize], linesize, bSH, qp1, a, b, h, 1);
}
filter_mb_edgeh( &img_y[4*2*linesize], linesize, bS3, qp, a, b, h, 0);
} else {
filter_mb_edgev( &img_y[4*1<<pixel_shift], linesize, bS3, qp, a, b, h, 0);
filter_mb_edgev( &img_y[4*2<<pixel_shift], linesize, bS3, qp, a, b, h, 0);
filter_mb_edgev( &img_y[4*3<<pixel_shift], linesize, bS3, qp, a, b, h, 0);
if(top_type){
filter_mb_edgeh( &img_y[4*0*linesize], linesize, bSH, qp1, a, b, h, 1);
}
filter_mb_edgeh( &img_y[4*1*linesize], linesize, bS3, qp, a, b, h, 0);
filter_mb_edgeh( &img_y[4*2*linesize], linesize, bS3, qp, a, b, h, 0);
filter_mb_edgeh( &img_y[4*3*linesize], linesize, bS3, qp, a, b, h, 0);
}
if(chroma){
if(chroma444){
if(left_type){
filter_mb_edgev( &img_cb[4*0<<pixel_shift], linesize, bS4, qpc0, a, b, h, 1);
filter_mb_edgev( &img_cr[4*0<<pixel_shift], linesize, bS4, qpc0, a, b, h, 1);
}
if( IS_8x8DCT(mb_type) ) {
filter_mb_edgev( &img_cb[4*2<<pixel_shift], linesize, bS3, qpc, a, b, h, 0);
filter_mb_edgev( &img_cr[4*2<<pixel_shift], linesize, bS3, qpc, a, b, h, 0);
if(top_type){
filter_mb_edgeh( &img_cb[4*0*linesize], linesize, bSH, qpc1, a, b, h, 1 );
filter_mb_edgeh( &img_cr[4*0*linesize], linesize, bSH, qpc1, a, b, h, 1 );
}
filter_mb_edgeh( &img_cb[4*2*linesize], linesize, bS3, qpc, a, b, h, 0);
filter_mb_edgeh( &img_cr[4*2*linesize], linesize, bS3, qpc, a, b, h, 0);
} else {
filter_mb_edgev( &img_cb[4*1<<pixel_shift], linesize, bS3, qpc, a, b, h, 0);
filter_mb_edgev( &img_cr[4*1<<pixel_shift], linesize, bS3, qpc, a, b, h, 0);
filter_mb_edgev( &img_cb[4*2<<pixel_shift], linesize, bS3, qpc, a, b, h, 0);
filter_mb_edgev( &img_cr[4*2<<pixel_shift], linesize, bS3, qpc, a, b, h, 0);
filter_mb_edgev( &img_cb[4*3<<pixel_shift], linesize, bS3, qpc, a, b, h, 0);
filter_mb_edgev( &img_cr[4*3<<pixel_shift], linesize, bS3, qpc, a, b, h, 0);
if(top_type){
filter_mb_edgeh( &img_cb[4*0*linesize], linesize, bSH, qpc1, a, b, h, 1);
filter_mb_edgeh( &img_cr[4*0*linesize], linesize, bSH, qpc1, a, b, h, 1);
}
filter_mb_edgeh( &img_cb[4*1*linesize], linesize, bS3, qpc, a, b, h, 0);
filter_mb_edgeh( &img_cr[4*1*linesize], linesize, bS3, qpc, a, b, h, 0);
filter_mb_edgeh( &img_cb[4*2*linesize], linesize, bS3, qpc, a, b, h, 0);
filter_mb_edgeh( &img_cr[4*2*linesize], linesize, bS3, qpc, a, b, h, 0);
filter_mb_edgeh( &img_cb[4*3*linesize], linesize, bS3, qpc, a, b, h, 0);
filter_mb_edgeh( &img_cr[4*3*linesize], linesize, bS3, qpc, a, b, h, 0);
}
}else if(chroma422){
if(left_type){
filter_mb_edgecv(&img_cb[2*0<<pixel_shift], uvlinesize, bS4, qpc0, a, b, h, 1);
filter_mb_edgecv(&img_cr[2*0<<pixel_shift], uvlinesize, bS4, qpc0, a, b, h, 1);
}
filter_mb_edgecv(&img_cb[2*2<<pixel_shift], uvlinesize, bS3, qpc, a, b, h, 0);
filter_mb_edgecv(&img_cr[2*2<<pixel_shift], uvlinesize, bS3, qpc, a, b, h, 0);
if(top_type){
filter_mb_edgech(&img_cb[4*0*uvlinesize], uvlinesize, bSH, qpc1, a, b, h, 1);
filter_mb_edgech(&img_cr[4*0*uvlinesize], uvlinesize, bSH, qpc1, a, b, h, 1);
}
filter_mb_edgech(&img_cb[4*1*uvlinesize], uvlinesize, bS3, qpc, a, b, h, 0);
filter_mb_edgech(&img_cr[4*1*uvlinesize], uvlinesize, bS3, qpc, a, b, h, 0);
filter_mb_edgech(&img_cb[4*2*uvlinesize], uvlinesize, bS3, qpc, a, b, h, 0);
filter_mb_edgech(&img_cr[4*2*uvlinesize], uvlinesize, bS3, qpc, a, b, h, 0);
filter_mb_edgech(&img_cb[4*3*uvlinesize], uvlinesize, bS3, qpc, a, b, h, 0);
filter_mb_edgech(&img_cr[4*3*uvlinesize], uvlinesize, bS3, qpc, a, b, h, 0);
}else{
if(left_type){
filter_mb_edgecv( &img_cb[2*0<<pixel_shift], uvlinesize, bS4, qpc0, a, b, h, 1);
filter_mb_edgecv( &img_cr[2*0<<pixel_shift], uvlinesize, bS4, qpc0, a, b, h, 1);
}
filter_mb_edgecv( &img_cb[2*2<<pixel_shift], uvlinesize, bS3, qpc, a, b, h, 0);
filter_mb_edgecv( &img_cr[2*2<<pixel_shift], uvlinesize, bS3, qpc, a, b, h, 0);
if(top_type){
filter_mb_edgech( &img_cb[2*0*uvlinesize], uvlinesize, bSH, qpc1, a, b, h, 1);
filter_mb_edgech( &img_cr[2*0*uvlinesize], uvlinesize, bSH, qpc1, a, b, h, 1);
}
filter_mb_edgech( &img_cb[2*2*uvlinesize], uvlinesize, bS3, qpc, a, b, h, 0);
filter_mb_edgech( &img_cr[2*2*uvlinesize], uvlinesize, bS3, qpc, a, b, h, 0);
}
}
return;
} else {
LOCAL_ALIGNED_8(int16_t, bS, [2], [4][4]);
int edges;
if( IS_8x8DCT(mb_type) && (sl->cbp&7) == 7 && !chroma444 ) {
edges = 4;
AV_WN64A(bS[0][0], 0x0002000200020002ULL);
AV_WN64A(bS[0][2], 0x0002000200020002ULL);
AV_WN64A(bS[1][0], 0x0002000200020002ULL);
AV_WN64A(bS[1][2], 0x0002000200020002ULL);
} else {
int mask_edge1 = (3*(((5*mb_type)>>5)&1)) | (mb_type>>4);
int mask_edge0 = 3*((mask_edge1>>1) & ((5*left_type)>>5)&1);
int step = 1+(mb_type>>24);
edges = 4 - 3*((mb_type>>3) & !(sl->cbp & 15));
h->h264dsp.h264_loop_filter_strength(bS, sl->non_zero_count_cache, sl->ref_cache, sl->mv_cache,
sl->list_count==2, edges, step, mask_edge0, mask_edge1, FIELD_PICTURE(h));
}
if( IS_INTRA(left_type) )
AV_WN64A(bS[0][0], 0x0004000400040004ULL);
if( IS_INTRA(top_type) )
AV_WN64A(bS[1][0], FIELD_PICTURE(h) ? 0x0003000300030003ULL : 0x0004000400040004ULL);
#define FILTER(hv,dir,edge,intra)\
if(AV_RN64A(bS[dir][edge])) { \
filter_mb_edge##hv( &img_y[4*edge*(dir?linesize:1<<pixel_shift)], linesize, bS[dir][edge], edge ? qp : qp##dir, a, b, h, intra );\
if(chroma){\
if(chroma444){\
filter_mb_edge##hv( &img_cb[4*edge*(dir?linesize:1<<pixel_shift)], linesize, bS[dir][edge], edge ? qpc : qpc##dir, a, b, h, intra );\
filter_mb_edge##hv( &img_cr[4*edge*(dir?linesize:1<<pixel_shift)], linesize, bS[dir][edge], edge ? qpc : qpc##dir, a, b, h, intra );\
} else if(!(edge&1)) {\
filter_mb_edgec##hv( &img_cb[2*edge*(dir?uvlinesize:1<<pixel_shift)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir, a, b, h, intra );\
filter_mb_edgec##hv( &img_cr[2*edge*(dir?uvlinesize:1<<pixel_shift)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir, a, b, h, intra );\
}\
}\
}
if(left_type)
FILTER(v,0,0,1);
if( edges == 1 ) {
if(top_type)
FILTER(h,1,0,1);
} else if( IS_8x8DCT(mb_type) ) {
FILTER(v,0,2,0);
if(top_type)
FILTER(h,1,0,1);
FILTER(h,1,2,0);
} else {
FILTER(v,0,1,0);
FILTER(v,0,2,0);
FILTER(v,0,3,0);
if(top_type)
FILTER(h,1,0,1);
FILTER(h,1,1,0);
FILTER(h,1,2,0);
FILTER(h,1,3,0);
}
#undef FILTER
}
}
|
['static int h264_decode_frame(AVCodecContext *avctx, void *data,\n int *got_frame, AVPacket *avpkt)\n{\n const uint8_t *buf = avpkt->data;\n int buf_size = avpkt->size;\n H264Context *h = avctx->priv_data;\n AVFrame *pict = data;\n int buf_index = 0;\n int ret;\n h->flags = avctx->flags;\n h->setup_finished = 0;\nout:\n if (buf_size == 0) {\n H264Picture *out;\n int i, out_idx;\n h->cur_pic_ptr = NULL;\n out = h->delayed_pic[0];\n out_idx = 0;\n for (i = 1;\n h->delayed_pic[i] &&\n !h->delayed_pic[i]->f->key_frame &&\n !h->delayed_pic[i]->mmco_reset;\n i++)\n if (h->delayed_pic[i]->poc < out->poc) {\n out = h->delayed_pic[i];\n out_idx = i;\n }\n for (i = out_idx; h->delayed_pic[i]; i++)\n h->delayed_pic[i] = h->delayed_pic[i + 1];\n if (out) {\n ret = output_frame(h, pict, out->f);\n if (ret < 0)\n return ret;\n *got_frame = 1;\n }\n return buf_index;\n }\n buf_index = decode_nal_units(h, buf, buf_size);\n if (buf_index < 0)\n return AVERROR_INVALIDDATA;\n if (!h->cur_pic_ptr && h->nal_unit_type == NAL_END_SEQUENCE) {\n buf_size = 0;\n goto out;\n }\n if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS) && !h->cur_pic_ptr) {\n if (avctx->skip_frame >= AVDISCARD_NONREF)\n return 0;\n av_log(avctx, AV_LOG_ERROR, "no frame!\\n");\n return AVERROR_INVALIDDATA;\n }\n if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS) ||\n (h->mb_y >= h->mb_height && h->mb_height)) {\n if (avctx->flags2 & AV_CODEC_FLAG2_CHUNKS)\n decode_postinit(h, 1);\n ff_h264_field_end(h, &h->slice_ctx[0], 0);\n *got_frame = 0;\n if (h->next_output_pic && ((avctx->flags & AV_CODEC_FLAG_OUTPUT_CORRUPT) ||\n h->next_output_pic->recovered)) {\n if (!h->next_output_pic->recovered)\n h->next_output_pic->f->flags |= AV_FRAME_FLAG_CORRUPT;\n ret = output_frame(h, pict, h->next_output_pic->f);\n if (ret < 0)\n return ret;\n *got_frame = 1;\n }\n }\n assert(pict->buf[0] || !*got_frame);\n return get_consumed_bytes(buf_index, buf_size);\n}', 'static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size)\n{\n AVCodecContext *const avctx = h->avctx;\n unsigned context_count = 0;\n int nals_needed = 0;\n int i, ret = 0;\n if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS)) {\n h->current_slice = 0;\n if (!h->first_field)\n h->cur_pic_ptr = NULL;\n ff_h264_sei_uninit(&h->sei);\n }\n ret = ff_h2645_packet_split(&h->pkt, buf, buf_size, avctx, h->is_avc,\n h->nal_length_size, avctx->codec_id);\n if (ret < 0) {\n av_log(avctx, AV_LOG_ERROR,\n "Error splitting the input into NAL units.\\n");\n return ret;\n }\n if (avctx->active_thread_type & FF_THREAD_FRAME)\n nals_needed = get_last_needed_nal(h);\n for (i = 0; i < h->pkt.nb_nals; i++) {\n H2645NAL *nal = &h->pkt.nals[i];\n H264SliceContext *sl = &h->slice_ctx[context_count];\n int err;\n if (avctx->skip_frame >= AVDISCARD_NONREF &&\n nal->ref_idc == 0 && nal->type != NAL_SEI)\n continue;\n h->nal_ref_idc = nal->ref_idc;\n h->nal_unit_type = nal->type;\n err = 0;\n switch (nal->type) {\n case NAL_IDR_SLICE:\n if (nal->type != NAL_IDR_SLICE) {\n av_log(h->avctx, AV_LOG_ERROR,\n "Invalid mix of idr and non-idr slices\\n");\n ret = -1;\n goto end;\n }\n idr(h);\n case NAL_SLICE:\n sl->gb = nal->gb;\n if ((err = ff_h264_decode_slice_header(h, sl)))\n break;\n if (h->sei.recovery_point.recovery_frame_cnt >= 0 && h->recovery_frame < 0) {\n h->recovery_frame = (h->poc.frame_num + h->sei.recovery_point.recovery_frame_cnt) &\n ((1 << h->ps.sps->log2_max_frame_num) - 1);\n }\n h->cur_pic_ptr->f->key_frame |=\n (nal->type == NAL_IDR_SLICE) || (h->sei.recovery_point.recovery_frame_cnt >= 0);\n if (nal->type == NAL_IDR_SLICE || h->recovery_frame == h->poc.frame_num) {\n h->recovery_frame = -1;\n h->cur_pic_ptr->recovered = 1;\n }\n if (nal->type == NAL_IDR_SLICE)\n h->frame_recovered |= FRAME_RECOVERED_IDR;\n h->cur_pic_ptr->recovered |= !!(h->frame_recovered & FRAME_RECOVERED_IDR);\n if (h->current_slice == 1) {\n if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS))\n decode_postinit(h, i >= nals_needed);\n if (h->avctx->hwaccel &&\n (ret = h->avctx->hwaccel->start_frame(h->avctx, NULL, 0)) < 0)\n return ret;\n }\n if (sl->redundant_pic_count == 0 &&\n (avctx->skip_frame < AVDISCARD_NONREF || nal->ref_idc) &&\n (avctx->skip_frame < AVDISCARD_BIDIR ||\n sl->slice_type_nos != AV_PICTURE_TYPE_B) &&\n (avctx->skip_frame < AVDISCARD_NONKEY ||\n h->cur_pic_ptr->f->key_frame) &&\n avctx->skip_frame < AVDISCARD_ALL) {\n if (avctx->hwaccel) {\n ret = avctx->hwaccel->decode_slice(avctx, nal->raw_data, nal->raw_size);\n if (ret < 0)\n return ret;\n } else\n context_count++;\n }\n break;\n case NAL_DPA:\n case NAL_DPB:\n case NAL_DPC:\n avpriv_request_sample(avctx, "data partitioning");\n ret = AVERROR(ENOSYS);\n goto end;\n break;\n case NAL_SEI:\n ret = ff_h264_sei_decode(&h->sei, &nal->gb, &h->ps, avctx);\n if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))\n goto end;\n break;\n case NAL_SPS:\n ret = ff_h264_decode_seq_parameter_set(&nal->gb, avctx, &h->ps);\n if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))\n goto end;\n break;\n case NAL_PPS:\n ret = ff_h264_decode_picture_parameter_set(&nal->gb, avctx, &h->ps,\n nal->size_bits);\n if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))\n goto end;\n break;\n case NAL_AUD:\n case NAL_END_SEQUENCE:\n case NAL_END_STREAM:\n case NAL_FILLER_DATA:\n case NAL_SPS_EXT:\n case NAL_AUXILIARY_SLICE:\n break;\n case NAL_FF_IGNORE:\n break;\n default:\n av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\\n",\n nal->type, nal->size_bits);\n }\n if (context_count == h->nb_slice_ctx) {\n ret = ff_h264_execute_decode_slices(h, context_count);\n if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))\n goto end;\n context_count = 0;\n }\n if (err < 0) {\n av_log(h->avctx, AV_LOG_ERROR, "decode_slice_header error\\n");\n sl->ref_count[0] = sl->ref_count[1] = sl->list_count = 0;\n }\n }\n if (context_count) {\n ret = ff_h264_execute_decode_slices(h, context_count);\n if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))\n goto end;\n }\n ret = 0;\nend:\n if (h->cur_pic_ptr && !h->droppable) {\n ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,\n h->picture_structure == PICT_BOTTOM_FIELD);\n }\n return (ret < 0) ? ret : buf_size;\n}', 'int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length,\n void *logctx, int is_nalff, int nal_length_size,\n enum AVCodecID codec_id)\n{\n int consumed, ret = 0;\n pkt->nb_nals = 0;\n while (length >= 4) {\n H2645NAL *nal;\n int extract_length = 0;\n int skip_trailing_zeros = 1;\n if (is_nalff) {\n int i;\n for (i = 0; i < nal_length_size; i++)\n extract_length = (extract_length << 8) | buf[i];\n buf += nal_length_size;\n length -= nal_length_size;\n if (extract_length > length) {\n av_log(logctx, AV_LOG_ERROR, "Invalid NAL unit size.\\n");\n return AVERROR_INVALIDDATA;\n }\n } else {\n if (buf[2] == 0) {\n length--;\n buf++;\n continue;\n }\n if (buf[0] != 0 || buf[1] != 0 || buf[2] != 1)\n return AVERROR_INVALIDDATA;\n buf += 3;\n length -= 3;\n extract_length = length;\n }\n if (pkt->nals_allocated < pkt->nb_nals + 1) {\n int new_size = pkt->nals_allocated + 1;\n H2645NAL *tmp = av_realloc_array(pkt->nals, new_size, sizeof(*tmp));\n if (!tmp)\n return AVERROR(ENOMEM);\n pkt->nals = tmp;\n memset(pkt->nals + pkt->nals_allocated, 0,\n (new_size - pkt->nals_allocated) * sizeof(*tmp));\n pkt->nals_allocated = new_size;\n }\n nal = &pkt->nals[pkt->nb_nals++];\n consumed = ff_h2645_extract_rbsp(buf, extract_length, nal);\n if (consumed < 0)\n return consumed;\n if (consumed < length - 3 &&\n buf[consumed] == 0x00 && buf[consumed + 1] == 0x00 &&\n buf[consumed + 2] == 0x01 && buf[consumed + 3] == 0xE0)\n skip_trailing_zeros = 0;\n nal->size_bits = get_bit_length(nal, skip_trailing_zeros);\n ret = init_get_bits(&nal->gb, nal->data, nal->size_bits);\n if (ret < 0)\n return ret;\n if (codec_id == AV_CODEC_ID_HEVC)\n ret = hevc_parse_nal_header(nal, logctx);\n else\n ret = h264_parse_nal_header(nal, logctx);\n if (ret <= 0) {\n if (ret < 0) {\n av_log(logctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\\n",\n nal->type);\n }\n pkt->nb_nals--;\n }\n buf += consumed;\n length -= consumed;\n }\n return 0;\n}', 'static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,\n int bit_size)\n{\n int buffer_size;\n int ret = 0;\n if (bit_size > INT_MAX - 7 || bit_size < 0 || !buffer) {\n bit_size = 0;\n buffer = NULL;\n ret = AVERROR_INVALIDDATA;\n }\n buffer_size = (bit_size + 7) >> 3;\n s->buffer = buffer;\n s->size_in_bits = bit_size;\n#if !UNCHECKED_BITSTREAM_READER\n s->size_in_bits_plus8 = bit_size + 8;\n#endif\n s->buffer_end = buffer + buffer_size;\n s->index = 0;\n return ret;\n}', 'int ff_h264_execute_decode_slices(H264Context *h, unsigned context_count)\n{\n AVCodecContext *const avctx = h->avctx;\n H264SliceContext *sl;\n int i, j;\n if (h->avctx->hwaccel)\n return 0;\n if (context_count == 1) {\n int ret;\n h->slice_ctx[0].next_slice_idx = h->mb_width * h->mb_height;\n h->postpone_filter = 0;\n ret = decode_slice(avctx, &h->slice_ctx[0]);\n h->mb_y = h->slice_ctx[0].mb_y;\n return ret;\n } else {\n for (i = 0; i < context_count; i++) {\n int next_slice_idx = h->mb_width * h->mb_height;\n int slice_idx;\n sl = &h->slice_ctx[i];\n sl->er.error_count = 0;\n slice_idx = sl->mb_y * h->mb_width + sl->mb_x;\n for (j = 0; j < context_count; j++) {\n H264SliceContext *sl2 = &h->slice_ctx[j];\n int slice_idx2 = sl2->mb_y * h->mb_width + sl2->mb_x;\n if (i == j || slice_idx2 < slice_idx)\n continue;\n next_slice_idx = FFMIN(next_slice_idx, slice_idx2);\n }\n sl->next_slice_idx = next_slice_idx;\n }\n avctx->execute(avctx, decode_slice, h->slice_ctx,\n NULL, context_count, sizeof(h->slice_ctx[0]));\n sl = &h->slice_ctx[context_count - 1];\n h->mb_y = sl->mb_y;\n for (i = 1; i < context_count; i++)\n h->slice_ctx[0].er.error_count += h->slice_ctx[i].er.error_count;\n if (h->postpone_filter) {\n h->postpone_filter = 0;\n for (i = 0; i < context_count; i++) {\n int y_end, x_end;\n sl = &h->slice_ctx[i];\n y_end = FFMIN(sl->mb_y + 1, h->mb_height);\n x_end = (sl->mb_y >= h->mb_height) ? h->mb_width : sl->mb_x;\n for (j = sl->resync_mb_y; j < y_end; j += 1 + FIELD_OR_MBAFF_PICTURE(h)) {\n sl->mb_y = j;\n loop_filter(h, sl, j > sl->resync_mb_y ? 0 : sl->resync_mb_x,\n j == y_end - 1 ? x_end : h->mb_width);\n }\n }\n }\n }\n return 0;\n}', 'static void loop_filter(const H264Context *h, H264SliceContext *sl, int start_x, int end_x)\n{\n uint8_t *dest_y, *dest_cb, *dest_cr;\n int linesize, uvlinesize, mb_x, mb_y;\n const int end_mb_y = sl->mb_y + FRAME_MBAFF(h);\n const int old_slice_type = sl->slice_type;\n const int pixel_shift = h->pixel_shift;\n const int block_h = 16 >> h->chroma_y_shift;\n if (h->postpone_filter)\n return;\n if (sl->deblocking_filter) {\n for (mb_x = start_x; mb_x < end_x; mb_x++)\n for (mb_y = end_mb_y - FRAME_MBAFF(h); mb_y <= end_mb_y; mb_y++) {\n int mb_xy, mb_type;\n mb_xy = sl->mb_xy = mb_x + mb_y * h->mb_stride;\n mb_type = h->cur_pic.mb_type[mb_xy];\n if (FRAME_MBAFF(h))\n sl->mb_mbaff =\n sl->mb_field_decoding_flag = !!IS_INTERLACED(mb_type);\n sl->mb_x = mb_x;\n sl->mb_y = mb_y;\n dest_y = h->cur_pic.f->data[0] +\n ((mb_x << pixel_shift) + mb_y * sl->linesize) * 16;\n dest_cb = h->cur_pic.f->data[1] +\n (mb_x << pixel_shift) * (8 << CHROMA444(h)) +\n mb_y * sl->uvlinesize * block_h;\n dest_cr = h->cur_pic.f->data[2] +\n (mb_x << pixel_shift) * (8 << CHROMA444(h)) +\n mb_y * sl->uvlinesize * block_h;\n if (MB_FIELD(sl)) {\n linesize = sl->mb_linesize = sl->linesize * 2;\n uvlinesize = sl->mb_uvlinesize = sl->uvlinesize * 2;\n if (mb_y & 1) {\n dest_y -= sl->linesize * 15;\n dest_cb -= sl->uvlinesize * (block_h - 1);\n dest_cr -= sl->uvlinesize * (block_h - 1);\n }\n } else {\n linesize = sl->mb_linesize = sl->linesize;\n uvlinesize = sl->mb_uvlinesize = sl->uvlinesize;\n }\n backup_mb_border(h, sl, dest_y, dest_cb, dest_cr, linesize,\n uvlinesize, 0);\n if (fill_filter_caches(h, sl, mb_type))\n continue;\n sl->chroma_qp[0] = get_chroma_qp(h, 0, h->cur_pic.qscale_table[mb_xy]);\n sl->chroma_qp[1] = get_chroma_qp(h, 1, h->cur_pic.qscale_table[mb_xy]);\n if (FRAME_MBAFF(h)) {\n ff_h264_filter_mb(h, sl, mb_x, mb_y, dest_y, dest_cb, dest_cr,\n linesize, uvlinesize);\n } else {\n ff_h264_filter_mb_fast(h, sl, mb_x, mb_y, dest_y, dest_cb,\n dest_cr, linesize, uvlinesize);\n }\n }\n }\n sl->slice_type = old_slice_type;\n sl->mb_x = end_x;\n sl->mb_y = end_mb_y - FRAME_MBAFF(h);\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}', 'void ff_h264_filter_mb_fast(const H264Context *h, H264SliceContext *sl,\n int mb_x, int mb_y, uint8_t *img_y,\n uint8_t *img_cb, uint8_t *img_cr,\n unsigned int linesize, unsigned int uvlinesize)\n{\n assert(!FRAME_MBAFF(h));\n if(!h->h264dsp.h264_loop_filter_strength || h->ps.pps->chroma_qp_diff) {\n ff_h264_filter_mb(h, sl, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize);\n return;\n }\n#if CONFIG_SMALL\n h264_filter_mb_fast_internal(h, sl, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize, h->pixel_shift);\n#else\n if(h->pixel_shift){\n h264_filter_mb_fast_internal(h, sl, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize, 1);\n }else{\n h264_filter_mb_fast_internal(h, sl, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize, 0);\n }\n#endif\n}', 'static av_always_inline void h264_filter_mb_fast_internal(const H264Context *h,\n H264SliceContext *sl,\n int mb_x, int mb_y,\n uint8_t *img_y,\n uint8_t *img_cb,\n uint8_t *img_cr,\n unsigned int linesize,\n unsigned int uvlinesize,\n int pixel_shift)\n{\n int chroma = !(CONFIG_GRAY && (h->flags & AV_CODEC_FLAG_GRAY));\n int chroma444 = CHROMA444(h);\n int chroma422 = CHROMA422(h);\n int mb_xy = sl->mb_xy;\n int left_type = sl->left_type[LTOP];\n int top_type = sl->top_type;\n int qp_bd_offset = 6 * (h->ps.sps->bit_depth_luma - 8);\n int a = 52 + sl->slice_alpha_c0_offset - qp_bd_offset;\n int b = 52 + sl->slice_beta_offset - qp_bd_offset;\n int mb_type = h->cur_pic.mb_type[mb_xy];\n int qp = h->cur_pic.qscale_table[mb_xy];\n int qp0 = h->cur_pic.qscale_table[mb_xy - 1];\n int qp1 = h->cur_pic.qscale_table[sl->top_mb_xy];\n int qpc = get_chroma_qp( h, 0, qp );\n int qpc0 = get_chroma_qp( h, 0, qp0 );\n int qpc1 = get_chroma_qp( h, 0, qp1 );\n qp0 = (qp + qp0 + 1) >> 1;\n qp1 = (qp + qp1 + 1) >> 1;\n qpc0 = (qpc + qpc0 + 1) >> 1;\n qpc1 = (qpc + qpc1 + 1) >> 1;\n if( IS_INTRA(mb_type) ) {\n static const int16_t bS4[4] = {4,4,4,4};\n static const int16_t bS3[4] = {3,3,3,3};\n const int16_t *bSH = FIELD_PICTURE(h) ? bS3 : bS4;\n if(left_type)\n filter_mb_edgev( &img_y[4*0<<pixel_shift], linesize, bS4, qp0, a, b, h, 1);\n if( IS_8x8DCT(mb_type) ) {\n filter_mb_edgev( &img_y[4*2<<pixel_shift], linesize, bS3, qp, a, b, h, 0);\n if(top_type){\n filter_mb_edgeh( &img_y[4*0*linesize], linesize, bSH, qp1, a, b, h, 1);\n }\n filter_mb_edgeh( &img_y[4*2*linesize], linesize, bS3, qp, a, b, h, 0);\n } else {\n filter_mb_edgev( &img_y[4*1<<pixel_shift], linesize, bS3, qp, a, b, h, 0);\n filter_mb_edgev( &img_y[4*2<<pixel_shift], linesize, bS3, qp, a, b, h, 0);\n filter_mb_edgev( &img_y[4*3<<pixel_shift], linesize, bS3, qp, a, b, h, 0);\n if(top_type){\n filter_mb_edgeh( &img_y[4*0*linesize], linesize, bSH, qp1, a, b, h, 1);\n }\n filter_mb_edgeh( &img_y[4*1*linesize], linesize, bS3, qp, a, b, h, 0);\n filter_mb_edgeh( &img_y[4*2*linesize], linesize, bS3, qp, a, b, h, 0);\n filter_mb_edgeh( &img_y[4*3*linesize], linesize, bS3, qp, a, b, h, 0);\n }\n if(chroma){\n if(chroma444){\n if(left_type){\n filter_mb_edgev( &img_cb[4*0<<pixel_shift], linesize, bS4, qpc0, a, b, h, 1);\n filter_mb_edgev( &img_cr[4*0<<pixel_shift], linesize, bS4, qpc0, a, b, h, 1);\n }\n if( IS_8x8DCT(mb_type) ) {\n filter_mb_edgev( &img_cb[4*2<<pixel_shift], linesize, bS3, qpc, a, b, h, 0);\n filter_mb_edgev( &img_cr[4*2<<pixel_shift], linesize, bS3, qpc, a, b, h, 0);\n if(top_type){\n filter_mb_edgeh( &img_cb[4*0*linesize], linesize, bSH, qpc1, a, b, h, 1 );\n filter_mb_edgeh( &img_cr[4*0*linesize], linesize, bSH, qpc1, a, b, h, 1 );\n }\n filter_mb_edgeh( &img_cb[4*2*linesize], linesize, bS3, qpc, a, b, h, 0);\n filter_mb_edgeh( &img_cr[4*2*linesize], linesize, bS3, qpc, a, b, h, 0);\n } else {\n filter_mb_edgev( &img_cb[4*1<<pixel_shift], linesize, bS3, qpc, a, b, h, 0);\n filter_mb_edgev( &img_cr[4*1<<pixel_shift], linesize, bS3, qpc, a, b, h, 0);\n filter_mb_edgev( &img_cb[4*2<<pixel_shift], linesize, bS3, qpc, a, b, h, 0);\n filter_mb_edgev( &img_cr[4*2<<pixel_shift], linesize, bS3, qpc, a, b, h, 0);\n filter_mb_edgev( &img_cb[4*3<<pixel_shift], linesize, bS3, qpc, a, b, h, 0);\n filter_mb_edgev( &img_cr[4*3<<pixel_shift], linesize, bS3, qpc, a, b, h, 0);\n if(top_type){\n filter_mb_edgeh( &img_cb[4*0*linesize], linesize, bSH, qpc1, a, b, h, 1);\n filter_mb_edgeh( &img_cr[4*0*linesize], linesize, bSH, qpc1, a, b, h, 1);\n }\n filter_mb_edgeh( &img_cb[4*1*linesize], linesize, bS3, qpc, a, b, h, 0);\n filter_mb_edgeh( &img_cr[4*1*linesize], linesize, bS3, qpc, a, b, h, 0);\n filter_mb_edgeh( &img_cb[4*2*linesize], linesize, bS3, qpc, a, b, h, 0);\n filter_mb_edgeh( &img_cr[4*2*linesize], linesize, bS3, qpc, a, b, h, 0);\n filter_mb_edgeh( &img_cb[4*3*linesize], linesize, bS3, qpc, a, b, h, 0);\n filter_mb_edgeh( &img_cr[4*3*linesize], linesize, bS3, qpc, a, b, h, 0);\n }\n }else if(chroma422){\n if(left_type){\n filter_mb_edgecv(&img_cb[2*0<<pixel_shift], uvlinesize, bS4, qpc0, a, b, h, 1);\n filter_mb_edgecv(&img_cr[2*0<<pixel_shift], uvlinesize, bS4, qpc0, a, b, h, 1);\n }\n filter_mb_edgecv(&img_cb[2*2<<pixel_shift], uvlinesize, bS3, qpc, a, b, h, 0);\n filter_mb_edgecv(&img_cr[2*2<<pixel_shift], uvlinesize, bS3, qpc, a, b, h, 0);\n if(top_type){\n filter_mb_edgech(&img_cb[4*0*uvlinesize], uvlinesize, bSH, qpc1, a, b, h, 1);\n filter_mb_edgech(&img_cr[4*0*uvlinesize], uvlinesize, bSH, qpc1, a, b, h, 1);\n }\n filter_mb_edgech(&img_cb[4*1*uvlinesize], uvlinesize, bS3, qpc, a, b, h, 0);\n filter_mb_edgech(&img_cr[4*1*uvlinesize], uvlinesize, bS3, qpc, a, b, h, 0);\n filter_mb_edgech(&img_cb[4*2*uvlinesize], uvlinesize, bS3, qpc, a, b, h, 0);\n filter_mb_edgech(&img_cr[4*2*uvlinesize], uvlinesize, bS3, qpc, a, b, h, 0);\n filter_mb_edgech(&img_cb[4*3*uvlinesize], uvlinesize, bS3, qpc, a, b, h, 0);\n filter_mb_edgech(&img_cr[4*3*uvlinesize], uvlinesize, bS3, qpc, a, b, h, 0);\n }else{\n if(left_type){\n filter_mb_edgecv( &img_cb[2*0<<pixel_shift], uvlinesize, bS4, qpc0, a, b, h, 1);\n filter_mb_edgecv( &img_cr[2*0<<pixel_shift], uvlinesize, bS4, qpc0, a, b, h, 1);\n }\n filter_mb_edgecv( &img_cb[2*2<<pixel_shift], uvlinesize, bS3, qpc, a, b, h, 0);\n filter_mb_edgecv( &img_cr[2*2<<pixel_shift], uvlinesize, bS3, qpc, a, b, h, 0);\n if(top_type){\n filter_mb_edgech( &img_cb[2*0*uvlinesize], uvlinesize, bSH, qpc1, a, b, h, 1);\n filter_mb_edgech( &img_cr[2*0*uvlinesize], uvlinesize, bSH, qpc1, a, b, h, 1);\n }\n filter_mb_edgech( &img_cb[2*2*uvlinesize], uvlinesize, bS3, qpc, a, b, h, 0);\n filter_mb_edgech( &img_cr[2*2*uvlinesize], uvlinesize, bS3, qpc, a, b, h, 0);\n }\n }\n return;\n } else {\n LOCAL_ALIGNED_8(int16_t, bS, [2], [4][4]);\n int edges;\n if( IS_8x8DCT(mb_type) && (sl->cbp&7) == 7 && !chroma444 ) {\n edges = 4;\n AV_WN64A(bS[0][0], 0x0002000200020002ULL);\n AV_WN64A(bS[0][2], 0x0002000200020002ULL);\n AV_WN64A(bS[1][0], 0x0002000200020002ULL);\n AV_WN64A(bS[1][2], 0x0002000200020002ULL);\n } else {\n int mask_edge1 = (3*(((5*mb_type)>>5)&1)) | (mb_type>>4);\n int mask_edge0 = 3*((mask_edge1>>1) & ((5*left_type)>>5)&1);\n int step = 1+(mb_type>>24);\n edges = 4 - 3*((mb_type>>3) & !(sl->cbp & 15));\n h->h264dsp.h264_loop_filter_strength(bS, sl->non_zero_count_cache, sl->ref_cache, sl->mv_cache,\n sl->list_count==2, edges, step, mask_edge0, mask_edge1, FIELD_PICTURE(h));\n }\n if( IS_INTRA(left_type) )\n AV_WN64A(bS[0][0], 0x0004000400040004ULL);\n if( IS_INTRA(top_type) )\n AV_WN64A(bS[1][0], FIELD_PICTURE(h) ? 0x0003000300030003ULL : 0x0004000400040004ULL);\n#define FILTER(hv,dir,edge,intra)\\\n if(AV_RN64A(bS[dir][edge])) { \\\n filter_mb_edge##hv( &img_y[4*edge*(dir?linesize:1<<pixel_shift)], linesize, bS[dir][edge], edge ? qp : qp##dir, a, b, h, intra );\\\n if(chroma){\\\n if(chroma444){\\\n filter_mb_edge##hv( &img_cb[4*edge*(dir?linesize:1<<pixel_shift)], linesize, bS[dir][edge], edge ? qpc : qpc##dir, a, b, h, intra );\\\n filter_mb_edge##hv( &img_cr[4*edge*(dir?linesize:1<<pixel_shift)], linesize, bS[dir][edge], edge ? qpc : qpc##dir, a, b, h, intra );\\\n } else if(!(edge&1)) {\\\n filter_mb_edgec##hv( &img_cb[2*edge*(dir?uvlinesize:1<<pixel_shift)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir, a, b, h, intra );\\\n filter_mb_edgec##hv( &img_cr[2*edge*(dir?uvlinesize:1<<pixel_shift)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir, a, b, h, intra );\\\n }\\\n }\\\n }\n if(left_type)\n FILTER(v,0,0,1);\n if( edges == 1 ) {\n if(top_type)\n FILTER(h,1,0,1);\n } else if( IS_8x8DCT(mb_type) ) {\n FILTER(v,0,2,0);\n if(top_type)\n FILTER(h,1,0,1);\n FILTER(h,1,2,0);\n } else {\n FILTER(v,0,1,0);\n FILTER(v,0,2,0);\n FILTER(v,0,3,0);\n if(top_type)\n FILTER(h,1,0,1);\n FILTER(h,1,1,0);\n FILTER(h,1,2,0);\n FILTER(h,1,3,0);\n }\n#undef FILTER\n }\n}']
|
2,632
| 0
|
https://github.com/libav/libav/blob/a2fb4bcb0189f6421608e0dec1a38c65910763f6/libavcodec/aacdec.c/#L492
|
static int decode_audio_specific_config(AACContext *ac,
AVCodecContext *avctx,
MPEG4AudioConfig *m4ac,
const uint8_t *data, int bit_size,
int sync_extension)
{
GetBitContext gb;
int i;
av_dlog(avctx, "extradata size %d\n", avctx->extradata_size);
for (i = 0; i < avctx->extradata_size; i++)
av_dlog(avctx, "%02x ", avctx->extradata[i]);
av_dlog(avctx, "\n");
init_get_bits(&gb, data, bit_size);
if ((i = avpriv_mpeg4audio_get_config(m4ac, data, bit_size, sync_extension)) < 0)
return -1;
if (m4ac->sampling_index > 12) {
av_log(avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", m4ac->sampling_index);
return -1;
}
if (m4ac->sbr == 1 && m4ac->ps == -1)
m4ac->ps = 1;
skip_bits_long(&gb, i);
switch (m4ac->object_type) {
case AOT_AAC_MAIN:
case AOT_AAC_LC:
case AOT_AAC_LTP:
if (decode_ga_specific_config(ac, avctx, &gb, m4ac, m4ac->chan_config))
return -1;
break;
default:
av_log(avctx, AV_LOG_ERROR, "Audio object type %s%d is not supported.\n",
m4ac->sbr == 1? "SBR+" : "", m4ac->object_type);
return -1;
}
av_dlog(avctx, "AOT %d chan config %d sampling index %d (%d) SBR %d PS %d\n",
m4ac->object_type, m4ac->chan_config, m4ac->sampling_index,
m4ac->sample_rate, m4ac->sbr, m4ac->ps);
return get_bits_count(&gb);
}
|
['static int decode_audio_specific_config(AACContext *ac,\n AVCodecContext *avctx,\n MPEG4AudioConfig *m4ac,\n const uint8_t *data, int bit_size,\n int sync_extension)\n{\n GetBitContext gb;\n int i;\n av_dlog(avctx, "extradata size %d\\n", avctx->extradata_size);\n for (i = 0; i < avctx->extradata_size; i++)\n av_dlog(avctx, "%02x ", avctx->extradata[i]);\n av_dlog(avctx, "\\n");\n init_get_bits(&gb, data, bit_size);\n if ((i = avpriv_mpeg4audio_get_config(m4ac, data, bit_size, sync_extension)) < 0)\n return -1;\n if (m4ac->sampling_index > 12) {\n av_log(avctx, AV_LOG_ERROR, "invalid sampling rate index %d\\n", m4ac->sampling_index);\n return -1;\n }\n if (m4ac->sbr == 1 && m4ac->ps == -1)\n m4ac->ps = 1;\n skip_bits_long(&gb, i);\n switch (m4ac->object_type) {\n case AOT_AAC_MAIN:\n case AOT_AAC_LC:\n case AOT_AAC_LTP:\n if (decode_ga_specific_config(ac, avctx, &gb, m4ac, m4ac->chan_config))\n return -1;\n break;\n default:\n av_log(avctx, AV_LOG_ERROR, "Audio object type %s%d is not supported.\\n",\n m4ac->sbr == 1? "SBR+" : "", m4ac->object_type);\n return -1;\n }\n av_dlog(avctx, "AOT %d chan config %d sampling index %d (%d) SBR %d PS %d\\n",\n m4ac->object_type, m4ac->chan_config, m4ac->sampling_index,\n m4ac->sample_rate, m4ac->sbr, m4ac->ps);\n return get_bits_count(&gb);\n}', 'static inline void init_get_bits(GetBitContext *s,\n const uint8_t *buffer, int bit_size)\n{\n int buffer_size = (bit_size+7)>>3;\n if (buffer_size < 0 || bit_size < 0) {\n buffer_size = bit_size = 0;\n buffer = NULL;\n }\n s->buffer = buffer;\n s->size_in_bits = bit_size;\n s->buffer_end = buffer + buffer_size;\n#ifdef ALT_BITSTREAM_READER\n s->index = 0;\n#elif defined A32_BITSTREAM_READER\n s->buffer_ptr = (uint32_t*)((intptr_t)buffer & ~3);\n s->bit_count = 32 + 8*((intptr_t)buffer & 3);\n skip_bits_long(s, 0);\n#endif\n}', 'static inline void skip_bits_long(GetBitContext *s, int n){\n s->index += n;\n}']
|
2,633
| 0
|
https://github.com/libav/libav/blob/9181976348cac563977a0450c90f27ad72018f1d/libavformat/matroskaenc.c/#L215
|
static void put_ebml_void(AVIOContext *pb, uint64_t size)
{
int64_t currentpos = avio_tell(pb);
assert(size >= 2);
put_ebml_id(pb, EBML_ID_VOID);
if (size < 10)
put_ebml_num(pb, size-1, 0);
else
put_ebml_num(pb, size-9, 8);
while(avio_tell(pb) < currentpos + size)
avio_w8(pb, 0);
}
|
['static int mkv_write_trailer(AVFormatContext *s)\n{\n MatroskaMuxContext *mkv = s->priv_data;\n AVIOContext *pb = s->pb;\n int64_t currentpos, cuespos;\n int ret;\n if (mkv->cur_audio_pkt.size > 0) {\n ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt);\n mkv->cur_audio_pkt.size = 0;\n if (ret < 0) {\n av_log(s, AV_LOG_ERROR, "Could not write cached audio packet ret:%d\\n", ret);\n return ret;\n }\n }\n if (mkv->dyn_bc) {\n end_ebml_master(mkv->dyn_bc, mkv->cluster);\n mkv_flush_dynbuf(s);\n } else if (mkv->cluster_pos) {\n end_ebml_master(pb, mkv->cluster);\n }\n if (pb->seekable) {\n if (mkv->cues->num_entries) {\n cuespos = mkv_write_cues(pb, mkv->cues, s->nb_streams);\n ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_CUES, cuespos);\n if (ret < 0) return ret;\n }\n mkv_write_seekhead(pb, mkv->main_seekhead);\n av_log(s, AV_LOG_DEBUG, "end duration = %" PRIu64 "\\n", mkv->duration);\n currentpos = avio_tell(pb);\n avio_seek(pb, mkv->duration_offset, SEEK_SET);\n put_ebml_float(pb, MATROSKA_ID_DURATION, mkv->duration);\n avio_seek(pb, currentpos, SEEK_SET);\n }\n end_ebml_master(pb, mkv->segment);\n av_free(mkv->tracks);\n av_destruct_packet(&mkv->cur_audio_pkt);\n avio_flush(pb);\n return 0;\n}', 'static void end_ebml_master(AVIOContext *pb, ebml_master master)\n{\n int64_t pos = avio_tell(pb);\n if (avio_seek(pb, master.pos - master.sizebytes, SEEK_SET) < 0)\n return;\n put_ebml_num(pb, pos - master.pos, master.sizebytes);\n avio_seek(pb, pos, SEEK_SET);\n}', 'static int64_t mkv_write_seekhead(AVIOContext *pb, mkv_seekhead *seekhead)\n{\n ebml_master metaseek, seekentry;\n int64_t currentpos;\n int i;\n currentpos = avio_tell(pb);\n if (seekhead->reserved_size > 0)\n if (avio_seek(pb, seekhead->filepos, SEEK_SET) < 0)\n return -1;\n metaseek = start_ebml_master(pb, MATROSKA_ID_SEEKHEAD, seekhead->reserved_size);\n for (i = 0; i < seekhead->num_entries; i++) {\n mkv_seekhead_entry *entry = &seekhead->entries[i];\n seekentry = start_ebml_master(pb, MATROSKA_ID_SEEKENTRY, MAX_SEEKENTRY_SIZE);\n put_ebml_id(pb, MATROSKA_ID_SEEKID);\n put_ebml_num(pb, ebml_id_size(entry->elementid), 0);\n put_ebml_id(pb, entry->elementid);\n put_ebml_uint(pb, MATROSKA_ID_SEEKPOSITION, entry->segmentpos);\n end_ebml_master(pb, seekentry);\n }\n end_ebml_master(pb, metaseek);\n if (seekhead->reserved_size > 0) {\n uint64_t remaining = seekhead->filepos + seekhead->reserved_size - avio_tell(pb);\n put_ebml_void(pb, remaining);\n avio_seek(pb, currentpos, SEEK_SET);\n currentpos = seekhead->filepos;\n }\n av_free(seekhead->entries);\n av_free(seekhead);\n return currentpos;\n}', 'static void put_ebml_void(AVIOContext *pb, uint64_t size)\n{\n int64_t currentpos = avio_tell(pb);\n assert(size >= 2);\n put_ebml_id(pb, EBML_ID_VOID);\n if (size < 10)\n put_ebml_num(pb, size-1, 0);\n else\n put_ebml_num(pb, size-9, 8);\n while(avio_tell(pb) < currentpos + size)\n avio_w8(pb, 0);\n}']
|
2,634
| 0
|
https://github.com/openssl/openssl/blob/d40a1b865fddc3d67f8c06ff1f1466fad331c8f7/crypto/ui/ui_lib.c/#L434
|
char *UI_construct_prompt(UI *ui, const char *object_desc,
const char *object_name)
{
char *prompt = NULL;
if (ui->meth->ui_construct_prompt)
prompt = ui->meth->ui_construct_prompt(ui,
object_desc, object_name);
else
{
char prompt1[] = "Enter ";
char prompt2[] = " for ";
char prompt3[] = ":";
int len = 0;
if (object_desc == NULL)
return NULL;
len = sizeof(prompt1) - 1 + strlen(object_desc);
if (object_name)
len += sizeof(prompt2) - 1 + strlen(object_name);
len += sizeof(prompt3) - 1;
prompt = (char *)OPENSSL_malloc(len + 1);
BUF_strlcpy(prompt, prompt1, len + 1);
BUF_strlcat(prompt, object_desc, len + 1);
if (object_name)
{
BUF_strlcat(prompt, prompt2, len + 1);
BUF_strlcat(prompt, object_name, len + 1);
}
BUF_strlcat(prompt, prompt3, len + 1);
}
return prompt;
}
|
['char *UI_construct_prompt(UI *ui, const char *object_desc,\n\tconst char *object_name)\n\t{\n\tchar *prompt = NULL;\n\tif (ui->meth->ui_construct_prompt)\n\t\tprompt = ui->meth->ui_construct_prompt(ui,\n\t\t\tobject_desc, object_name);\n\telse\n\t\t{\n\t\tchar prompt1[] = "Enter ";\n\t\tchar prompt2[] = " for ";\n\t\tchar prompt3[] = ":";\n\t\tint len = 0;\n\t\tif (object_desc == NULL)\n\t\t\treturn NULL;\n\t\tlen = sizeof(prompt1) - 1 + strlen(object_desc);\n\t\tif (object_name)\n\t\t\tlen += sizeof(prompt2) - 1 + strlen(object_name);\n\t\tlen += sizeof(prompt3) - 1;\n\t\tprompt = (char *)OPENSSL_malloc(len + 1);\n\t\tBUF_strlcpy(prompt, prompt1, len + 1);\n\t\tBUF_strlcat(prompt, object_desc, len + 1);\n\t\tif (object_name)\n\t\t\t{\n\t\t\tBUF_strlcat(prompt, prompt2, len + 1);\n\t\t\tBUF_strlcat(prompt, object_name, len + 1);\n\t\t\t}\n\t\tBUF_strlcat(prompt, prompt3, len + 1);\n\t\t}\n\treturn prompt;\n\t}', 'void *CRYPTO_malloc(size_t 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}', 'size_t BUF_strlcat(char *dst, const char *src, size_t size)\n\t{\n\tsize_t l = 0;\n\tfor(; size > 0 && *dst; size--, dst++)\n\t\tl++;\n\treturn l + BUF_strlcpy(dst, src, size);\n\t}']
|
2,635
| 0
|
https://github.com/openssl/openssl/blob/49cd47eaababc8c57871b929080fc1357e2ad7b8/crypto/x509/x509_cmp.c/#L57
|
unsigned long X509_issuer_and_serial_hash(X509 *a)
{
unsigned long ret = 0;
EVP_MD_CTX *ctx = EVP_MD_CTX_new();
unsigned char md[16];
char *f;
if (ctx == NULL)
goto err;
f = X509_NAME_oneline(a->cert_info.issuer, NULL, 0);
if (!EVP_DigestInit_ex(ctx, EVP_md5(), NULL))
goto err;
if (!EVP_DigestUpdate(ctx, (unsigned char *)f, strlen(f)))
goto err;
OPENSSL_free(f);
if (!EVP_DigestUpdate
(ctx, (unsigned char *)a->cert_info.serialNumber.data,
(unsigned long)a->cert_info.serialNumber.length))
goto err;
if (!EVP_DigestFinal_ex(ctx, &(md[0]), NULL))
goto err;
ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)
) & 0xffffffffL;
err:
EVP_MD_CTX_free(ctx);
return ret;
}
|
['unsigned long X509_issuer_and_serial_hash(X509 *a)\n{\n unsigned long ret = 0;\n EVP_MD_CTX *ctx = EVP_MD_CTX_new();\n unsigned char md[16];\n char *f;\n if (ctx == NULL)\n goto err;\n f = X509_NAME_oneline(a->cert_info.issuer, NULL, 0);\n if (!EVP_DigestInit_ex(ctx, EVP_md5(), NULL))\n goto err;\n if (!EVP_DigestUpdate(ctx, (unsigned char *)f, strlen(f)))\n goto err;\n OPENSSL_free(f);\n if (!EVP_DigestUpdate\n (ctx, (unsigned char *)a->cert_info.serialNumber.data,\n (unsigned long)a->cert_info.serialNumber.length))\n goto err;\n if (!EVP_DigestFinal_ex(ctx, &(md[0]), NULL))\n goto err;\n ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |\n ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)\n ) & 0xffffffffL;\n err:\n EVP_MD_CTX_free(ctx);\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}', 'const EVP_MD *EVP_md5(void)\n{\n return &md5_md;\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}']
|
2,636
| 0
|
https://github.com/openssl/openssl/blob/0a52d38b31ee56479c80509716c3bd46b764190a/crypto/bn/bn_mul.c/#L1012
|
int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
{
int ret=0;
int top,al,bl;
BIGNUM *rr;
#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
int i;
#endif
#ifdef BN_RECURSION
BIGNUM *t;
int j,k;
#endif
#ifdef BN_COUNT
fprintf(stderr,"BN_mul %d * %d\n",a->top,b->top);
#endif
bn_check_top(a);
bn_check_top(b);
bn_check_top(r);
al=a->top;
bl=b->top;
if ((al == 0) || (bl == 0))
{
BN_zero(r);
return(1);
}
top=al+bl;
BN_CTX_start(ctx);
if ((r == a) || (r == b))
{
if ((rr = BN_CTX_get(ctx)) == NULL) goto err;
}
else
rr = r;
rr->neg=a->neg^b->neg;
#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
i = al-bl;
#endif
#ifdef BN_MUL_COMBA
if (i == 0)
{
# if 0
if (al == 4)
{
if (bn_wexpand(rr,8) == NULL) goto err;
rr->top=8;
bn_mul_comba4(rr->d,a->d,b->d);
goto end;
}
# endif
if (al == 8)
{
if (bn_wexpand(rr,16) == NULL) goto err;
rr->top=16;
bn_mul_comba8(rr->d,a->d,b->d);
goto end;
}
}
#endif
#ifdef BN_RECURSION
if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL))
{
if (i >= -1 && i <= 1)
{
int sav_j =0;
if (i >= 0)
{
j = BN_num_bits_word((BN_ULONG)al);
}
if (i == -1)
{
j = BN_num_bits_word((BN_ULONG)bl);
}
sav_j = j;
j = 1<<(j-1);
assert(j <= al || j <= bl);
k = j+j;
t = BN_CTX_get(ctx);
if (al > j || bl > j)
{
bn_wexpand(t,k*4);
bn_wexpand(rr,k*4);
bn_mul_part_recursive(rr->d,a->d,b->d,
j,al-j,bl-j,t->d);
}
else
{
bn_wexpand(t,k*2);
bn_wexpand(rr,k*2);
bn_mul_recursive(rr->d,a->d,b->d,
j,al-j,bl-j,t->d);
}
rr->top=top;
goto end;
}
#if 0
if (i == 1 && !BN_get_flags(b,BN_FLG_STATIC_DATA))
{
BIGNUM *tmp_bn = (BIGNUM *)b;
bn_wexpand(tmp_bn,al);
tmp_bn->d[bl]=0;
bl++;
i--;
}
else if (i == -1 && !BN_get_flags(a,BN_FLG_STATIC_DATA))
{
BIGNUM *tmp_bn = (BIGNUM *)a;
bn_wexpand(tmp_bn,bl);
tmp_bn->d[al]=0;
al++;
i++;
}
if (i == 0)
{
j=BN_num_bits_word((BN_ULONG)al);
j=1<<(j-1);
k=j+j;
t = BN_CTX_get(ctx);
if (al == j)
{
bn_wexpand(t,k*2);
bn_wexpand(rr,k*2);
bn_mul_recursive(rr->d,a->d,b->d,al,t->d);
}
else
{
bn_wexpand(t,k*4);
bn_wexpand(rr,k*4);
bn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d);
}
rr->top=top;
goto end;
}
#endif
}
#endif
if (bn_wexpand(rr,top) == NULL) goto err;
rr->top=top;
bn_mul_normal(rr->d,a->d,al,b->d,bl);
#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
end:
#endif
bn_fix_top(rr);
if (r != rr) BN_copy(r,rr);
ret=1;
err:
BN_CTX_end(ctx);
return(ret);
}
|
['int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n\t{\n\tint ret=0;\n\tint top,al,bl;\n\tBIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\tint i;\n#endif\n#ifdef BN_RECURSION\n\tBIGNUM *t;\n\tint j,k;\n#endif\n#ifdef BN_COUNT\n\tfprintf(stderr,"BN_mul %d * %d\\n",a->top,b->top);\n#endif\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tbn_check_top(r);\n\tal=a->top;\n\tbl=b->top;\n\tif ((al == 0) || (bl == 0))\n\t\t{\n\t\tBN_zero(r);\n\t\treturn(1);\n\t\t}\n\ttop=al+bl;\n\tBN_CTX_start(ctx);\n\tif ((r == a) || (r == b))\n\t\t{\n\t\tif ((rr = BN_CTX_get(ctx)) == NULL) goto err;\n\t\t}\n\telse\n\t\trr = r;\n\trr->neg=a->neg^b->neg;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\ti = al-bl;\n#endif\n#ifdef BN_MUL_COMBA\n\tif (i == 0)\n\t\t{\n# if 0\n\t\tif (al == 4)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,8) == NULL) goto err;\n\t\t\trr->top=8;\n\t\t\tbn_mul_comba4(rr->d,a->d,b->d);\n\t\t\tgoto end;\n\t\t\t}\n# endif\n\t\tif (al == 8)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,16) == NULL) goto err;\n\t\t\trr->top=16;\n\t\t\tbn_mul_comba8(rr->d,a->d,b->d);\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n#endif\n#ifdef BN_RECURSION\n\tif ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL))\n\t\t{\n\t\tif (i >= -1 && i <= 1)\n\t\t\t{\n\t\t\tint sav_j =0;\n\t\t\tif (i >= 0)\n\t\t\t\t{\n\t\t\t\tj = BN_num_bits_word((BN_ULONG)al);\n\t\t\t\t}\n\t\t\tif (i == -1)\n\t\t\t\t{\n\t\t\t\tj = BN_num_bits_word((BN_ULONG)bl);\n\t\t\t\t}\n\t\t\tsav_j = j;\n\t\t\tj = 1<<(j-1);\n\t\t\tassert(j <= al || j <= bl);\n\t\t\tk = j+j;\n\t\t\tt = BN_CTX_get(ctx);\n\t\t\tif (al > j || bl > j)\n\t\t\t\t{\n\t\t\t\tbn_wexpand(t,k*4);\n\t\t\t\tbn_wexpand(rr,k*4);\n\t\t\t\tbn_mul_part_recursive(rr->d,a->d,b->d,\n\t\t\t\t\tj,al-j,bl-j,t->d);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tbn_wexpand(t,k*2);\n\t\t\t\tbn_wexpand(rr,k*2);\n\t\t\t\tbn_mul_recursive(rr->d,a->d,b->d,\n\t\t\t\t\tj,al-j,bl-j,t->d);\n\t\t\t\t}\n\t\t\trr->top=top;\n\t\t\tgoto end;\n\t\t\t}\n#if 0\n\t\tif (i == 1 && !BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tBIGNUM *tmp_bn = (BIGNUM *)b;\n\t\t\tbn_wexpand(tmp_bn,al);\n\t\t\ttmp_bn->d[bl]=0;\n\t\t\tbl++;\n\t\t\ti--;\n\t\t\t}\n\t\telse if (i == -1 && !BN_get_flags(a,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tBIGNUM *tmp_bn = (BIGNUM *)a;\n\t\t\tbn_wexpand(tmp_bn,bl);\n\t\t\ttmp_bn->d[al]=0;\n\t\t\tal++;\n\t\t\ti++;\n\t\t\t}\n\t\tif (i == 0)\n\t\t\t{\n\t\t\tj=BN_num_bits_word((BN_ULONG)al);\n\t\t\tj=1<<(j-1);\n\t\t\tk=j+j;\n\t\t\tt = BN_CTX_get(ctx);\n\t\t\tif (al == j)\n\t\t\t\t{\n\t\t\t\tbn_wexpand(t,k*2);\n\t\t\t\tbn_wexpand(rr,k*2);\n\t\t\t\tbn_mul_recursive(rr->d,a->d,b->d,al,t->d);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tbn_wexpand(t,k*4);\n\t\t\t\tbn_wexpand(rr,k*4);\n\t\t\t\tbn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d);\n\t\t\t\t}\n\t\t\trr->top=top;\n\t\t\tgoto end;\n\t\t\t}\n#endif\n\t\t}\n#endif\n\tif (bn_wexpand(rr,top) == NULL) goto err;\n\trr->top=top;\n\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\nend:\n#endif\n\tbn_fix_top(rr);\n\tif (r != rr) BN_copy(r,rr);\n\tret=1;\nerr:\n\tBN_CTX_end(ctx);\n\treturn(ret);\n\t}']
|
2,637
| 0
|
https://github.com/openssl/openssl/blob/5dfc369ffcdc4722482c818e6ba6cf6e704c2cb5/crypto/objects/obj_lib.c/#L93
|
ASN1_OBJECT *OBJ_dup(ASN1_OBJECT *o)
{
ASN1_OBJECT *r;
int i;
char *ln=NULL;
if (o == NULL) return(NULL);
if (!(o->flags & ASN1_OBJECT_FLAG_DYNAMIC))
return(o);
r=ASN1_OBJECT_new();
if (r == NULL)
{
OBJerr(OBJ_F_OBJ_DUP,ERR_R_ASN1_LIB);
return(NULL);
}
r->data=Malloc(o->length);
if (r->data == NULL)
goto err;
memcpy(r->data,o->data,o->length);
r->length=o->length;
r->nid=o->nid;
r->ln=r->sn=NULL;
if (o->ln != NULL)
{
i=strlen(o->ln)+1;
r->ln=ln=Malloc(i);
if (r->ln == NULL) goto err;
memcpy(ln,o->ln,i);
}
if (o->sn != NULL)
{
char *s;
i=strlen(o->sn)+1;
r->sn=s=Malloc(i);
if (r->sn == NULL) goto err;
memcpy(s,o->sn,i);
}
r->flags=o->flags|(ASN1_OBJECT_FLAG_DYNAMIC|
ASN1_OBJECT_FLAG_DYNAMIC_STRINGS|ASN1_OBJECT_FLAG_DYNAMIC_DATA);
return(r);
err:
OBJerr(OBJ_F_OBJ_DUP,ERR_R_MALLOC_FAILURE);
if (r != NULL)
{
if (ln != NULL) Free(ln);
if (r->data != NULL) Free(r->data);
Free(r);
}
return(NULL);
}
|
['int MAIN(int argc, char **argv)\n\t{\n\tint ret=1;\n\tX509_REQ *req=NULL;\n\tX509 *x=NULL,*xca=NULL;\n\tEVP_PKEY *Upkey=NULL,*CApkey=NULL;\n\tint i,num,badops=0;\n\tBIO *out=NULL;\n\tBIO *STDout=NULL;\n\tint informat,outformat,keyformat,CAformat,CAkeyformat;\n\tchar *infile=NULL,*outfile=NULL,*keyfile=NULL,*CAfile=NULL;\n\tchar *CAkeyfile=NULL,*CAserial=NULL;\n\tint text=0,serial=0,hash=0,subject=0,issuer=0,startdate=0,enddate=0;\n\tint noout=0,sign_flag=0,CA_flag=0,CA_createserial=0;\n\tint C=0;\n\tint x509req=0,days=DEF_DAYS,modulus=0;\n\tchar **pp;\n\tX509_STORE *ctx=NULL;\n\tX509_REQ *rq=NULL;\n\tint fingerprint=0;\n\tchar buf[256];\n\tconst EVP_MD *md_alg,*digest=EVP_md5();\n\tLHASH *extconf = NULL;\n\tchar *extsect = NULL, *extfile = NULL;\n\treqfile=0;\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\tinformat=FORMAT_PEM;\n\toutformat=FORMAT_PEM;\n\tkeyformat=FORMAT_PEM;\n\tCAformat=FORMAT_PEM;\n\tCAkeyformat=FORMAT_PEM;\n\tctx=X509_STORE_new();\n\tif (ctx == NULL) goto end;\n\tX509_STORE_set_verify_cb_func(ctx,callb);\n\targc--;\n\targv++;\n\tnum=0;\n\twhile (argc >= 1)\n\t\t{\n\t\tif \t(strcmp(*argv,"-inform") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tinformat=str2fmt(*(++argv));\n\t\t\t}\n\t\telse if (strcmp(*argv,"-outform") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\toutformat=str2fmt(*(++argv));\n\t\t\t}\n\t\telse if (strcmp(*argv,"-keyform") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tkeyformat=str2fmt(*(++argv));\n\t\t\t}\n\t\telse if (strcmp(*argv,"-req") == 0)\n\t\t\treqfile=1;\n\t\telse if (strcmp(*argv,"-CAform") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tCAformat=str2fmt(*(++argv));\n\t\t\t}\n\t\telse if (strcmp(*argv,"-CAkeyform") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tCAformat=str2fmt(*(++argv));\n\t\t\t}\n\t\telse if (strcmp(*argv,"-days") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tdays=atoi(*(++argv));\n\t\t\tif (days == 0)\n\t\t\t\t{\n\t\t\t\tBIO_printf(STDout,"bad number of days\\n");\n\t\t\t\tgoto bad;\n\t\t\t\t}\n\t\t\t}\n\t\telse if (strcmp(*argv,"-config") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\textfile= *(++argv);\n\t\t\t}\n\t\telse if (strcmp(*argv,"-in") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tinfile= *(++argv);\n\t\t\t}\n\t\telse if (strcmp(*argv,"-out") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\toutfile= *(++argv);\n\t\t\t}\n\t\telse if (strcmp(*argv,"-signkey") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tkeyfile= *(++argv);\n\t\t\tsign_flag= ++num;\n\t\t\t}\n\t\telse if (strcmp(*argv,"-CA") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tCAfile= *(++argv);\n\t\t\tCA_flag= ++num;\n\t\t\t}\n\t\telse if (strcmp(*argv,"-CAkey") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tCAkeyfile= *(++argv);\n\t\t\t}\n\t\telse if (strcmp(*argv,"-CAserial") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tCAserial= *(++argv);\n\t\t\t}\n\t\telse if (strcmp(*argv,"-C") == 0)\n\t\t\tC= ++num;\n\t\telse if (strcmp(*argv,"-serial") == 0)\n\t\t\tserial= ++num;\n\t\telse if (strcmp(*argv,"-modulus") == 0)\n\t\t\tmodulus= ++num;\n\t\telse if (strcmp(*argv,"-x509toreq") == 0)\n\t\t\tx509req= ++num;\n\t\telse if (strcmp(*argv,"-text") == 0)\n\t\t\ttext= ++num;\n\t\telse if (strcmp(*argv,"-hash") == 0)\n\t\t\thash= ++num;\n\t\telse if (strcmp(*argv,"-subject") == 0)\n\t\t\tsubject= ++num;\n\t\telse if (strcmp(*argv,"-issuer") == 0)\n\t\t\tissuer= ++num;\n\t\telse if (strcmp(*argv,"-fingerprint") == 0)\n\t\t\tfingerprint= ++num;\n\t\telse if (strcmp(*argv,"-dates") == 0)\n\t\t\t{\n\t\t\tstartdate= ++num;\n\t\t\tenddate= ++num;\n\t\t\t}\n\t\telse if (strcmp(*argv,"-startdate") == 0)\n\t\t\tstartdate= ++num;\n\t\telse if (strcmp(*argv,"-enddate") == 0)\n\t\t\tenddate= ++num;\n\t\telse if (strcmp(*argv,"-noout") == 0)\n\t\t\tnoout= ++num;\n\t\telse if (strcmp(*argv,"-CAcreateserial") == 0)\n\t\t\tCA_createserial= ++num;\n\t\telse if ((md_alg=EVP_get_digestbyname(&((*argv)[1]))) != NULL)\n\t\t\t{\n\t\t\tdigest=md_alg;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"unknown option %s\\n",*argv);\n\t\t\tbadops=1;\n\t\t\tbreak;\n\t\t\t}\n\t\targc--;\n\t\targv++;\n\t\t}\n\tif (badops)\n\t\t{\nbad:\n\t\tfor (pp=x509_usage; (*pp != NULL); pp++)\n\t\t\tBIO_printf(bio_err,*pp);\n\t\tgoto end;\n\t\t}\n\tERR_load_crypto_strings();\n\tX509V3_add_standard_extensions();\n\tif (!X509_STORE_set_default_paths(ctx))\n\t\t{\n\t\tERR_print_errors(bio_err);\n\t\tgoto end;\n\t\t}\n\tif ((CAkeyfile == NULL) && (CA_flag) && (CAformat == FORMAT_PEM))\n\t\t{ CAkeyfile=CAfile; }\n\telse if ((CA_flag) && (CAkeyfile == NULL))\n\t\t{\n\t\tBIO_printf(bio_err,"need to specify a CAkey if using the CA command\\n");\n\t\tgoto end;\n\t\t}\n\tif (extfile) {\n\t\tlong errorline;\n\t\tX509V3_CTX ctx2;\n\t\tif (!(extconf=CONF_load(NULL,extfile,&errorline))) {\n\t\t\tif (errorline <= 0)\n\t\t\t\tBIO_printf(bio_err,\n\t\t\t\t\t"error loading the config file \'%s\'\\n",\n\t\t\t\t\t\t\t\textfile);\n \telse\n \tBIO_printf(bio_err,\n\t\t\t\t "error on line %ld of config file \'%s\'\\n"\n\t\t\t\t\t\t\t,errorline,extfile);\n\t\t\tgoto end;\n\t\t}\n\t\tif(!(extsect = CONF_get_string(extconf, "default",\n\t\t\t\t\t "extensions"))) extsect = "default";\n\t\tX509V3_set_ctx_test(&ctx2);\n\t\tX509V3_set_conf_lhash(&ctx2, extconf);\n\t\tif(!X509V3_EXT_add_conf(extconf, &ctx2, extsect, NULL)) {\n\t\t\tBIO_printf(bio_err,\n\t\t\t\t"Error Loading extension section %s\\n",\n\t\t\t\t\t\t\t\t extsect);\n\t\t\tERR_print_errors(bio_err);\n\t\t\tgoto end;\n }\n\t}\n\tif (reqfile)\n\t\t{\n\t\tEVP_PKEY *pkey;\n\t\tX509_CINF *ci;\n\t\tBIO *in;\n\t\tif (!sign_flag && !CA_flag)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"We need a private key to sign with\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\tin=BIO_new(BIO_s_file());\n\t\tif (in == NULL)\n\t\t\t{\n\t\t\tERR_print_errors(bio_err);\n\t\t\tgoto end;\n\t\t\t}\n\t\tif (infile == NULL)\n\t\t\tBIO_set_fp(in,stdin,BIO_NOCLOSE|BIO_FP_TEXT);\n\t\telse\n\t\t\t{\n\t\t\tif (BIO_read_filename(in,infile) <= 0)\n\t\t\t\t{\n\t\t\t\tperror(infile);\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\t}\n\t\treq=PEM_read_bio_X509_REQ(in,NULL,NULL);\n\t\tBIO_free(in);\n\t\tif (req == NULL) { perror(infile); goto end; }\n\t\tif (\t(req->req_info == NULL) ||\n\t\t\t(req->req_info->pubkey == NULL) ||\n\t\t\t(req->req_info->pubkey->public_key == NULL) ||\n\t\t\t(req->req_info->pubkey->public_key->data == NULL))\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"The certificate request appears to corrupted\\n");\n\t\t\tBIO_printf(bio_err,"It does not contain a public key\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\tif ((pkey=X509_REQ_get_pubkey(req)) == NULL)\n\t {\n\t BIO_printf(bio_err,"error unpacking public key\\n");\n\t goto end;\n\t }\n\t\ti=X509_REQ_verify(req,pkey);\n\t\tEVP_PKEY_free(pkey);\n\t\tif (i < 0)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"Signature verification error\\n");\n\t\t\tERR_print_errors(bio_err);\n\t\t\tgoto end;\n\t\t\t}\n\t if (i == 0)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"Signature did not match the certificate request\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\telse\n\t\t\tBIO_printf(bio_err,"Signature ok\\n");\n\t\tX509_NAME_oneline(req->req_info->subject,buf,256);\n\t\tBIO_printf(bio_err,"subject=%s\\n",buf);\n\t\tif ((x=X509_new()) == NULL) goto end;\n\t\tci=x->cert_info;\n\t\tif (!ASN1_INTEGER_set(X509_get_serialNumber(x),0)) goto end;\n\t\tif (!X509_set_issuer_name(x,req->req_info->subject)) goto end;\n\t\tif (!X509_set_subject_name(x,req->req_info->subject)) goto end;\n\t\tX509_gmtime_adj(X509_get_notBefore(x),0);\n\t X509_gmtime_adj(X509_get_notAfter(x),(long)60*60*24*days);\n#if 0\n\t\tX509_PUBKEY_free(ci->key);\n\t\tci->key=req->req_info->pubkey;\n\t req->req_info->pubkey=NULL;\n#else\n\t\tpkey = X509_REQ_get_pubkey(req);\n\t\tX509_set_pubkey(x,pkey);\n\t\tEVP_PKEY_free(pkey);\n#endif\n\t\t}\n\telse\n\t\tx=load_cert(infile,informat);\n\tif (x == NULL) goto end;\n\tif (CA_flag)\n\t\t{\n\t\txca=load_cert(CAfile,CAformat);\n\t\tif (xca == NULL) goto end;\n\t\t}\n\tif (!noout || text)\n\t\t{\n\t\tOBJ_create("2.99999.3",\n\t\t\t"SET.ex3","SET x509v3 extension 3");\n\t\tout=BIO_new(BIO_s_file());\n\t\tif (out == NULL)\n\t\t\t{\n\t\t\tERR_print_errors(bio_err);\n\t\t\tgoto end;\n\t\t\t}\n\t\tif (outfile == NULL)\n\t\t\tBIO_set_fp(out,stdout,BIO_NOCLOSE);\n\t\telse\n\t\t\t{\n\t\t\tif (BIO_write_filename(out,outfile) <= 0)\n\t\t\t\t{\n\t\t\t\tperror(outfile);\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\tif (num)\n\t\t{\n\t\tfor (i=1; i<=num; i++)\n\t\t\t{\n\t\t\tif (issuer == i)\n\t\t\t\t{\n\t\t\t\tX509_NAME_oneline(X509_get_issuer_name(x),\n\t\t\t\t\tbuf,256);\n\t\t\t\tBIO_printf(STDout,"issuer= %s\\n",buf);\n\t\t\t\t}\n\t\t\telse if (subject == i)\n\t\t\t\t{\n\t\t\t\tX509_NAME_oneline(X509_get_subject_name(x),\n\t\t\t\t\tbuf,256);\n\t\t\t\tBIO_printf(STDout,"subject=%s\\n",buf);\n\t\t\t\t}\n\t\t\telse if (serial == i)\n\t\t\t\t{\n\t\t\t\tBIO_printf(STDout,"serial=");\n\t\t\t\ti2a_ASN1_INTEGER(STDout,x->cert_info->serialNumber);\n\t\t\t\tBIO_printf(STDout,"\\n");\n\t\t\t\t}\n\t\t\telse if (hash == i)\n\t\t\t\t{\n\t\t\t\tBIO_printf(STDout,"%08lx\\n",X509_subject_name_hash(x));\n\t\t\t\t}\n\t\t\telse\n\t\t\t\tif (modulus == i)\n\t\t\t\t{\n\t\t\t\tEVP_PKEY *pkey;\n\t\t\t\tpkey=X509_get_pubkey(x);\n\t\t\t\tif (pkey == NULL)\n\t\t\t\t\t{\n\t\t\t\t\tBIO_printf(bio_err,"Modulus=unavailable\\n");\n\t\t\t\t\tERR_print_errors(bio_err);\n\t\t\t\t\tgoto end;\n\t\t\t\t\t}\n\t\t\t\tBIO_printf(STDout,"Modulus=");\n#ifndef NO_RSA\n\t\t\t\tif (pkey->type == EVP_PKEY_RSA)\n\t\t\t\t\tBN_print(STDout,pkey->pkey.rsa->n);\n\t\t\t\telse\n#endif\n#ifndef NO_DSA\n\t\t\t\tif (pkey->type == EVP_PKEY_DSA)\n\t\t\t\t\tBN_print(STDout,pkey->pkey.dsa->pub_key);\n\t\t\t\telse\n#endif\n\t\t\t\t\tBIO_printf(STDout,"Wrong Algorithm type");\n\t\t\t\tBIO_printf(STDout,"\\n");\n\t\t\t\tEVP_PKEY_free(pkey);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\tif (C == i)\n\t\t\t\t{\n\t\t\t\tunsigned char *d;\n\t\t\t\tchar *m;\n\t\t\t\tint y,z;\n\t\t\t\tX509_NAME_oneline(X509_get_subject_name(x),\n\t\t\t\t\tbuf,256);\n\t\t\t\tBIO_printf(STDout,"/* subject:%s */\\n",buf);\n\t\t\t\tm=X509_NAME_oneline(\n\t\t\t\t\tX509_get_issuer_name(x),buf,256);\n\t\t\t\tBIO_printf(STDout,"/* issuer :%s */\\n",buf);\n\t\t\t\tz=i2d_X509(x,NULL);\n\t\t\t\tm=Malloc(z);\n\t\t\t\td=(unsigned char *)m;\n\t\t\t\tz=i2d_X509_NAME(X509_get_subject_name(x),&d);\n\t\t\t\tBIO_printf(STDout,"unsigned char XXX_subject_name[%d]={\\n",z);\n\t\t\t\td=(unsigned char *)m;\n\t\t\t\tfor (y=0; y<z; y++)\n\t\t\t\t\t{\n\t\t\t\t\tBIO_printf(STDout,"0x%02X,",d[y]);\n\t\t\t\t\tif ((y & 0x0f) == 0x0f) BIO_printf(STDout,"\\n");\n\t\t\t\t\t}\n\t\t\t\tif (y%16 != 0) BIO_printf(STDout,"\\n");\n\t\t\t\tBIO_printf(STDout,"};\\n");\n\t\t\t\tz=i2d_X509_PUBKEY(X509_get_X509_PUBKEY(x),&d);\n\t\t\t\tBIO_printf(STDout,"unsigned char XXX_public_key[%d]={\\n",z);\n\t\t\t\td=(unsigned char *)m;\n\t\t\t\tfor (y=0; y<z; y++)\n\t\t\t\t\t{\n\t\t\t\t\tBIO_printf(STDout,"0x%02X,",d[y]);\n\t\t\t\t\tif ((y & 0x0f) == 0x0f)\n\t\t\t\t\t\tBIO_printf(STDout,"\\n");\n\t\t\t\t\t}\n\t\t\t\tif (y%16 != 0) BIO_printf(STDout,"\\n");\n\t\t\t\tBIO_printf(STDout,"};\\n");\n\t\t\t\tz=i2d_X509(x,&d);\n\t\t\t\tBIO_printf(STDout,"unsigned char XXX_certificate[%d]={\\n",z);\n\t\t\t\td=(unsigned char *)m;\n\t\t\t\tfor (y=0; y<z; y++)\n\t\t\t\t\t{\n\t\t\t\t\tBIO_printf(STDout,"0x%02X,",d[y]);\n\t\t\t\t\tif ((y & 0x0f) == 0x0f)\n\t\t\t\t\t\tBIO_printf(STDout,"\\n");\n\t\t\t\t\t}\n\t\t\t\tif (y%16 != 0) BIO_printf(STDout,"\\n");\n\t\t\t\tBIO_printf(STDout,"};\\n");\n\t\t\t\tFree(m);\n\t\t\t\t}\n\t\t\telse if (text == i)\n\t\t\t\t{\n\t\t\t\tX509_print(out,x);\n\t\t\t\t}\n\t\t\telse if (startdate == i)\n\t\t\t\t{\n\t\t\t\tBIO_puts(STDout,"notBefore=");\n\t\t\t\tASN1_TIME_print(STDout,X509_get_notBefore(x));\n\t\t\t\tBIO_puts(STDout,"\\n");\n\t\t\t\t}\n\t\t\telse if (enddate == i)\n\t\t\t\t{\n\t\t\t\tBIO_puts(STDout,"notAfter=");\n\t\t\t\tASN1_TIME_print(STDout,X509_get_notAfter(x));\n\t\t\t\tBIO_puts(STDout,"\\n");\n\t\t\t\t}\n\t\t\telse if (fingerprint == i)\n\t\t\t\t{\n\t\t\t\tint j;\n\t\t\t\tunsigned int n;\n\t\t\t\tunsigned char md[EVP_MAX_MD_SIZE];\n\t\t\t\tif (!X509_digest(x,EVP_md5(),md,&n))\n\t\t\t\t\t{\n\t\t\t\t\tBIO_printf(bio_err,"out of memory\\n");\n\t\t\t\t\tgoto end;\n\t\t\t\t\t}\n\t\t\t\tBIO_printf(STDout,"MD5 Fingerprint=");\n\t\t\t\tfor (j=0; j<(int)n; j++)\n\t\t\t\t\t{\n\t\t\t\t\tBIO_printf(STDout,"%02X%c",md[j],\n\t\t\t\t\t\t(j+1 == (int)n)\n\t\t\t\t\t\t?\'\\n\':\':\');\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\telse if ((sign_flag == i) && (x509req == 0))\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err,"Getting Private key\\n");\n\t\t\t\tif (Upkey == NULL)\n\t\t\t\t\t{\n\t\t\t\t\tUpkey=load_key(keyfile,keyformat);\n\t\t\t\t\tif (Upkey == NULL) goto end;\n\t\t\t\t\t}\n#ifndef NO_DSA\n\t\t if (Upkey->type == EVP_PKEY_DSA)\n\t\t digest=EVP_dss1();\n#endif\n\t\t\t\tif (!sign(x,Upkey,days,digest,\n\t\t\t\t\t\t extconf, extsect)) goto end;\n\t\t\t\t}\n\t\t\telse if (CA_flag == i)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err,"Getting CA Private Key\\n");\n\t\t\t\tif (CAkeyfile != NULL)\n\t\t\t\t\t{\n\t\t\t\t\tCApkey=load_key(CAkeyfile,CAkeyformat);\n\t\t\t\t\tif (CApkey == NULL) goto end;\n\t\t\t\t\t}\n#ifndef NO_DSA\n\t\t if (CApkey->type == EVP_PKEY_DSA)\n\t\t digest=EVP_dss1();\n#endif\n\t\t\t\tif (!x509_certify(ctx,CAfile,digest,x,xca,\n\t\t\t\t\tCApkey, CAserial,CA_createserial,days,\n\t\t\t\t\textconf, extsect))\n\t\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\telse if (x509req == i)\n\t\t\t\t{\n\t\t\t\tEVP_PKEY *pk;\n\t\t\t\tBIO_printf(bio_err,"Getting request Private Key\\n");\n\t\t\t\tif (keyfile == NULL)\n\t\t\t\t\t{\n\t\t\t\t\tBIO_printf(bio_err,"no request key file specified\\n");\n\t\t\t\t\tgoto end;\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tpk=load_key(keyfile,FORMAT_PEM);\n\t\t\t\t\tif (pk == NULL) goto end;\n\t\t\t\t\t}\n\t\t\t\tBIO_printf(bio_err,"Generating certificate request\\n");\n\t\t\t\trq=X509_to_X509_REQ(x,pk,EVP_md5());\n\t\t\t\tEVP_PKEY_free(pk);\n\t\t\t\tif (rq == NULL)\n\t\t\t\t\t{\n\t\t\t\t\tERR_print_errors(bio_err);\n\t\t\t\t\tgoto end;\n\t\t\t\t\t}\n\t\t\t\tif (!noout)\n\t\t\t\t\t{\n\t\t\t\t\tX509_REQ_print(out,rq);\n\t\t\t\t\tPEM_write_bio_X509_REQ(out,rq);\n\t\t\t\t\t}\n\t\t\t\tnoout=1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\tif (noout)\n\t\t{\n\t\tret=0;\n\t\tgoto end;\n\t\t}\n\tif \t(outformat == FORMAT_ASN1)\n\t\ti=i2d_X509_bio(out,x);\n\telse if (outformat == FORMAT_PEM)\n\t\ti=PEM_write_bio_X509(out,x);\n\telse if (outformat == FORMAT_NETSCAPE)\n\t\t{\n\t\tASN1_HEADER ah;\n\t\tASN1_OCTET_STRING os;\n\t\tos.data=(unsigned char *)CERT_HDR;\n\t\tos.length=strlen(CERT_HDR);\n\t\tah.header= &os;\n\t\tah.data=(char *)x;\n\t\tah.meth=X509_asn1_meth();\n\t\ti=ASN1_i2d_bio(i2d_ASN1_HEADER,out,(unsigned char *)&ah);\n\t\t}\n\telse\t{\n\t\tBIO_printf(bio_err,"bad output format specified for outfile\\n");\n\t\tgoto end;\n\t\t}\n\tif (!i) {\n\t\tBIO_printf(bio_err,"unable to write certificate\\n");\n\t\tERR_print_errors(bio_err);\n\t\tgoto end;\n\t\t}\n\tret=0;\nend:\n\tOBJ_cleanup();\n\tCONF_free(extconf);\n\tBIO_free(out);\n\tBIO_free(STDout);\n\tX509_STORE_free(ctx);\n\tX509_REQ_free(req);\n\tX509_free(x);\n\tX509_free(xca);\n\tEVP_PKEY_free(Upkey);\n\tEVP_PKEY_free(CApkey);\n\tX509_REQ_free(rq);\n\tX509V3_EXT_cleanup();\n\tEXIT(ret);\n\t}', 'int OBJ_create(char *oid, char *sn, char *ln)\n\t{\n\tint ok=0;\n\tASN1_OBJECT *op=NULL;\n\tunsigned char *buf;\n\tint i;\n\ti=a2d_ASN1_OBJECT(NULL,0,oid,-1);\n\tif (i <= 0) return(0);\n\tif ((buf=(unsigned char *)Malloc(i)) == NULL)\n\t\t{\n\t\tOBJerr(OBJ_F_OBJ_CREATE,OBJ_R_MALLOC_FAILURE);\n\t\treturn(0);\n\t\t}\n\ti=a2d_ASN1_OBJECT(buf,i,oid,-1);\n\top=(ASN1_OBJECT *)ASN1_OBJECT_create(OBJ_new_nid(1),buf,i,sn,ln);\n\tif (op == NULL)\n\t\tgoto err;\n\tok=OBJ_add_object(op);\nerr:\n\tASN1_OBJECT_free(op);\n\tFree((char *)buf);\n\treturn(ok);\n\t}', 'int OBJ_add_object(ASN1_OBJECT *obj)\n\t{\n\tASN1_OBJECT *o;\n\tADDED_OBJ *ao[4],*aop;\n\tint i;\n\tif (added == NULL)\n\t\tif (!init_added()) return(0);\n\tif ((o=OBJ_dup(obj)) == NULL) goto err;\n\tao[ADDED_DATA]=NULL;\n\tao[ADDED_SNAME]=NULL;\n\tao[ADDED_LNAME]=NULL;\n\tao[ADDED_NID]=NULL;\n\tao[ADDED_NID]=(ADDED_OBJ *)Malloc(sizeof(ADDED_OBJ));\n\tif ((o->length != 0) && (obj->data != NULL))\n\t\tao[ADDED_DATA]=(ADDED_OBJ *)Malloc(sizeof(ADDED_OBJ));\n\tif (o->sn != NULL)\n\t\tao[ADDED_SNAME]=(ADDED_OBJ *)Malloc(sizeof(ADDED_OBJ));\n\tif (o->ln != NULL)\n\t\tao[ADDED_LNAME]=(ADDED_OBJ *)Malloc(sizeof(ADDED_OBJ));\n\tfor (i=ADDED_DATA; i<=ADDED_NID; i++)\n\t\t{\n\t\tif (ao[i] != NULL)\n\t\t\t{\n\t\t\tao[i]->type=i;\n\t\t\tao[i]->obj=o;\n\t\t\taop=(ADDED_OBJ *)lh_insert(added,(char *)ao[i]);\n\t\t\tif (aop != NULL)\n\t\t\t\tFree(aop);\n\t\t\t}\n\t\t}\n\to->flags&= ~(ASN1_OBJECT_FLAG_DYNAMIC|ASN1_OBJECT_FLAG_DYNAMIC_STRINGS|\n\t\t\tASN1_OBJECT_FLAG_DYNAMIC_DATA);\n\treturn(o->nid);\nerr:\n\tfor (i=ADDED_DATA; i<=ADDED_NID; i++)\n\t\tif (ao[i] != NULL) Free(ao[i]);\n\tif (o != NULL) Free(o);\n\treturn(NID_undef);\n\t}', 'ASN1_OBJECT *OBJ_dup(ASN1_OBJECT *o)\n\t{\n\tASN1_OBJECT *r;\n\tint i;\n\tchar *ln=NULL;\n\tif (o == NULL) return(NULL);\n\tif (!(o->flags & ASN1_OBJECT_FLAG_DYNAMIC))\n\t\treturn(o);\n\tr=ASN1_OBJECT_new();\n\tif (r == NULL)\n\t\t{\n\t\tOBJerr(OBJ_F_OBJ_DUP,ERR_R_ASN1_LIB);\n\t\treturn(NULL);\n\t\t}\n\tr->data=Malloc(o->length);\n\tif (r->data == NULL)\n\t\tgoto err;\n\tmemcpy(r->data,o->data,o->length);\n\tr->length=o->length;\n\tr->nid=o->nid;\n\tr->ln=r->sn=NULL;\n\tif (o->ln != NULL)\n\t\t{\n\t\ti=strlen(o->ln)+1;\n\t\tr->ln=ln=Malloc(i);\n\t\tif (r->ln == NULL) goto err;\n\t\tmemcpy(ln,o->ln,i);\n\t\t}\n\tif (o->sn != NULL)\n\t\t{\n\t\tchar *s;\n\t\ti=strlen(o->sn)+1;\n\t\tr->sn=s=Malloc(i);\n\t\tif (r->sn == NULL) goto err;\n\t\tmemcpy(s,o->sn,i);\n\t\t}\n\tr->flags=o->flags|(ASN1_OBJECT_FLAG_DYNAMIC|\n\t\tASN1_OBJECT_FLAG_DYNAMIC_STRINGS|ASN1_OBJECT_FLAG_DYNAMIC_DATA);\n\treturn(r);\nerr:\n\tOBJerr(OBJ_F_OBJ_DUP,ERR_R_MALLOC_FAILURE);\n\tif (r != NULL)\n\t\t{\n\t\tif (ln != NULL) Free(ln);\n\t\tif (r->data != NULL) Free(r->data);\n\t\tFree(r);\n\t\t}\n\treturn(NULL);\n\t}']
|
2,638
| 0
|
https://github.com/openssl/openssl/blob/d2b53fcdef9e7b482ff1e2d5f305ae798f3d1be2/ssl/statem/statem_lib.c/#L807
|
static int ssl_add_cert_chain(SSL *s, WPACKET *pkt, CERT_PKEY *cpk, int *al)
{
int i, chain_count;
X509 *x;
STACK_OF(X509) *extra_certs;
STACK_OF(X509) *chain = NULL;
X509_STORE *chain_store;
int tmpal = SSL_AD_INTERNAL_ERROR;
if (cpk == NULL || cpk->x509 == NULL)
return 1;
x = cpk->x509;
if (cpk->chain != NULL)
extra_certs = cpk->chain;
else
extra_certs = s->ctx->extra_certs;
if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs)
chain_store = NULL;
else if (s->cert->chain_store)
chain_store = s->cert->chain_store;
else
chain_store = s->ctx->cert_store;
if (chain_store != NULL) {
X509_STORE_CTX *xs_ctx = X509_STORE_CTX_new();
if (xs_ctx == NULL) {
SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_MALLOC_FAILURE);
goto err;
}
if (!X509_STORE_CTX_init(xs_ctx, chain_store, x, NULL)) {
X509_STORE_CTX_free(xs_ctx);
SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_X509_LIB);
goto err;
}
(void)X509_verify_cert(xs_ctx);
ERR_clear_error();
chain = X509_STORE_CTX_get0_chain(xs_ctx);
i = ssl_security_cert_chain(s, chain, NULL, 0);
if (i != 1) {
#if 0
SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_EE_KEY_TOO_SMALL);
SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_CA_KEY_TOO_SMALL);
SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_CA_MD_TOO_WEAK);
#endif
X509_STORE_CTX_free(xs_ctx);
SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, i);
goto err;
}
chain_count = sk_X509_num(chain);
for (i = 0; i < chain_count; i++) {
x = sk_X509_value(chain, i);
if (!ssl_add_cert_to_wpacket(s, pkt, x, i, &tmpal)) {
X509_STORE_CTX_free(xs_ctx);
goto err;
}
}
X509_STORE_CTX_free(xs_ctx);
} else {
i = ssl_security_cert_chain(s, extra_certs, x, 0);
if (i != 1) {
SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, i);
goto err;
}
if (!ssl_add_cert_to_wpacket(s, pkt, x, 0, &tmpal))
goto err;
for (i = 0; i < sk_X509_num(extra_certs); i++) {
x = sk_X509_value(extra_certs, i);
if (!ssl_add_cert_to_wpacket(s, pkt, x, i + 1, &tmpal))
goto err;
}
}
return 1;
err:
*al = tmpal;
return 0;
}
|
['static int ssl_add_cert_chain(SSL *s, WPACKET *pkt, CERT_PKEY *cpk, int *al)\n{\n int i, chain_count;\n X509 *x;\n STACK_OF(X509) *extra_certs;\n STACK_OF(X509) *chain = NULL;\n X509_STORE *chain_store;\n int tmpal = SSL_AD_INTERNAL_ERROR;\n if (cpk == NULL || cpk->x509 == NULL)\n return 1;\n x = cpk->x509;\n if (cpk->chain != NULL)\n extra_certs = cpk->chain;\n else\n extra_certs = s->ctx->extra_certs;\n if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs)\n chain_store = NULL;\n else if (s->cert->chain_store)\n chain_store = s->cert->chain_store;\n else\n chain_store = s->ctx->cert_store;\n if (chain_store != NULL) {\n X509_STORE_CTX *xs_ctx = X509_STORE_CTX_new();\n if (xs_ctx == NULL) {\n SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (!X509_STORE_CTX_init(xs_ctx, chain_store, x, NULL)) {\n X509_STORE_CTX_free(xs_ctx);\n SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_X509_LIB);\n goto err;\n }\n (void)X509_verify_cert(xs_ctx);\n ERR_clear_error();\n chain = X509_STORE_CTX_get0_chain(xs_ctx);\n i = ssl_security_cert_chain(s, chain, NULL, 0);\n if (i != 1) {\n#if 0\n SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_EE_KEY_TOO_SMALL);\n SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_CA_KEY_TOO_SMALL);\n SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_CA_MD_TOO_WEAK);\n#endif\n X509_STORE_CTX_free(xs_ctx);\n SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, i);\n goto err;\n }\n chain_count = sk_X509_num(chain);\n for (i = 0; i < chain_count; i++) {\n x = sk_X509_value(chain, i);\n if (!ssl_add_cert_to_wpacket(s, pkt, x, i, &tmpal)) {\n X509_STORE_CTX_free(xs_ctx);\n goto err;\n }\n }\n X509_STORE_CTX_free(xs_ctx);\n } else {\n i = ssl_security_cert_chain(s, extra_certs, x, 0);\n if (i != 1) {\n SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, i);\n goto err;\n }\n if (!ssl_add_cert_to_wpacket(s, pkt, x, 0, &tmpal))\n goto err;\n for (i = 0; i < sk_X509_num(extra_certs); i++) {\n x = sk_X509_value(extra_certs, i);\n if (!ssl_add_cert_to_wpacket(s, pkt, x, i + 1, &tmpal))\n goto err;\n }\n }\n return 1;\n err:\n *al = tmpal;\n return 0;\n}', 'X509_STORE_CTX *X509_STORE_CTX_new(void)\n{\n X509_STORE_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));\n if (ctx == NULL) {\n X509err(X509_F_X509_STORE_CTX_NEW, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n return ctx;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num <= 0)\n return NULL;\n FAILTEST();\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n osslargused(file); osslargused(line);\n ret = malloc(num);\n#endif\n return ret;\n}', 'void X509_STORE_CTX_free(X509_STORE_CTX *ctx)\n{\n if (ctx == NULL)\n return;\n X509_STORE_CTX_cleanup(ctx);\n OPENSSL_free(ctx);\n}', 'void CRYPTO_free(void *str, const char *file, int line)\n{\n if (free_impl != NULL && free_impl != &CRYPTO_free) {\n free_impl(str, file, line);\n return;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_free(str, 0, file, line);\n free(str);\n CRYPTO_mem_debug_free(str, 1, file, line);\n } else {\n free(str);\n }\n#else\n free(str);\n#endif\n}']
|
2,639
| 1
|
https://github.com/openssl/openssl/blob/9b10986d7742a5105ac8c5f4eba8b103caf57ae9/crypto/bn/bn_sqr.c/#L118
|
void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)
{
int i, j, max;
const BN_ULONG *ap;
BN_ULONG *rp;
max = n * 2;
ap = a;
rp = r;
rp[0] = rp[max - 1] = 0;
rp++;
j = n;
if (--j > 0) {
ap++;
rp[j] = bn_mul_words(rp, ap, j, ap[-1]);
rp += 2;
}
for (i = n - 2; i > 0; i--) {
j--;
ap++;
rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);
rp += 2;
}
bn_add_words(r, r, r, max);
bn_sqr_words(tmp, a, n);
bn_add_words(r, r, tmp, max);
}
|
['static ECDSA_SIG *sm2_sig_gen(const EC_KEY *key, const BIGNUM *e)\n{\n const BIGNUM *dA = EC_KEY_get0_private_key(key);\n const EC_GROUP *group = EC_KEY_get0_group(key);\n const BIGNUM *order = EC_GROUP_get0_order(group);\n ECDSA_SIG *sig = NULL;\n EC_POINT *kG = NULL;\n BN_CTX *ctx = NULL;\n BIGNUM *k = NULL;\n BIGNUM *rk = NULL;\n BIGNUM *r = NULL;\n BIGNUM *s = NULL;\n BIGNUM *x1 = NULL;\n BIGNUM *tmp = NULL;\n kG = EC_POINT_new(group);\n ctx = BN_CTX_new();\n if (kG == NULL || ctx == NULL) {\n SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);\n goto done;\n }\n BN_CTX_start(ctx);\n k = BN_CTX_get(ctx);\n rk = BN_CTX_get(ctx);\n x1 = BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (tmp == NULL) {\n SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);\n goto done;\n }\n r = BN_new();\n s = BN_new();\n if (r == NULL || s == NULL) {\n SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);\n goto done;\n }\n for (;;) {\n if (!BN_priv_rand_range(k, order)) {\n SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR);\n goto done;\n }\n if (!EC_POINT_mul(group, kG, k, NULL, NULL, ctx)\n || !EC_POINT_get_affine_coordinates(group, kG, x1, NULL,\n ctx)\n || !BN_mod_add(r, e, x1, order, ctx)) {\n SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR);\n goto done;\n }\n if (BN_is_zero(r))\n continue;\n if (!BN_add(rk, r, k)) {\n SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR);\n goto done;\n }\n if (BN_cmp(rk, order) == 0)\n continue;\n if (!BN_add(s, dA, BN_value_one())\n || !ec_group_do_inverse_ord(group, s, s, ctx)\n || !BN_mod_mul(tmp, dA, r, order, ctx)\n || !BN_sub(tmp, k, tmp)\n || !BN_mod_mul(s, s, tmp, order, ctx)) {\n SM2err(SM2_F_SM2_SIG_GEN, ERR_R_BN_LIB);\n goto done;\n }\n sig = ECDSA_SIG_new();\n if (sig == NULL) {\n SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);\n goto done;\n }\n ECDSA_SIG_set0(sig, r, s);\n break;\n }\n done:\n if (sig == NULL) {\n BN_free(r);\n BN_free(s);\n }\n BN_CTX_free(ctx);\n EC_POINT_free(kG);\n return sig;\n}', 'int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int ret, r_neg, cmp_res;\n bn_check_top(a);\n bn_check_top(b);\n if (a->neg == b->neg) {\n r_neg = a->neg;\n ret = BN_uadd(r, a, b);\n } else {\n cmp_res = BN_ucmp(a, b);\n if (cmp_res > 0) {\n r_neg = a->neg;\n ret = BN_usub(r, a, b);\n } else if (cmp_res < 0) {\n r_neg = b->neg;\n ret = BN_usub(r, b, a);\n } else {\n r_neg = 0;\n BN_zero(r);\n ret = 1;\n }\n }\n r->neg = r_neg;\n bn_check_top(r);\n return ret;\n}', 'int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int max, min, dif;\n BN_ULONG t1, t2, borrow, *rp;\n const BN_ULONG *ap, *bp;\n bn_check_top(a);\n bn_check_top(b);\n max = a->top;\n min = b->top;\n dif = max - min;\n if (dif < 0) {\n BNerr(BN_F_BN_USUB, BN_R_ARG2_LT_ARG3);\n return 0;\n }\n if (bn_wexpand(r, max) == NULL)\n return 0;\n ap = a->d;\n bp = b->d;\n rp = r->d;\n borrow = bn_sub_words(rp, ap, bp, min);\n ap += min;\n rp += min;\n while (dif) {\n dif--;\n t1 = *(ap++);\n t2 = (t1 - borrow) & BN_MASK2;\n *(rp++) = t2;\n borrow &= (t1 == 0);\n }\n while (max && *--rp == 0)\n max--;\n r->top = max;\n r->neg = 0;\n bn_pollute(r);\n return 1;\n}', 'int ec_group_do_inverse_ord(const EC_GROUP *group, BIGNUM *res,\n const BIGNUM *x, BN_CTX *ctx)\n{\n if (group->meth->field_inverse_mod_ord != NULL)\n return group->meth->field_inverse_mod_ord(group, res, x, ctx);\n else\n return ec_field_inverse_mod_ord(group, res, x, ctx);\n}', 'static int ec_field_inverse_mod_ord(const EC_GROUP *group, BIGNUM *r,\n const BIGNUM *x, BN_CTX *ctx)\n{\n BIGNUM *e = NULL;\n BN_CTX *new_ctx = NULL;\n int ret = 0;\n if (group->mont_data == NULL)\n return 0;\n if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL)\n return 0;\n BN_CTX_start(ctx);\n if ((e = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (!BN_set_word(e, 2))\n goto err;\n if (!BN_sub(e, group->order, e))\n goto err;\n if (!BN_mod_exp_mont(r, x, e, group->order, ctx, group->mont_data))\n goto err;\n ret = 1;\n err:\n if (ctx != NULL)\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\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_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_sqr_fixed_top(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int max, al;\n int ret = 0;\n BIGNUM *tmp, *rr;\n bn_check_top(a);\n al = a->top;\n if (al <= 0) {\n r->top = 0;\n r->neg = 0;\n return 1;\n }\n BN_CTX_start(ctx);\n rr = (a != r) ? r : BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (rr == NULL || tmp == NULL)\n goto err;\n max = 2 * al;\n if (bn_wexpand(rr, max) == NULL)\n goto err;\n if (al == 4) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[8];\n bn_sqr_normal(rr->d, a->d, 4, t);\n#else\n bn_sqr_comba4(rr->d, a->d);\n#endif\n } else if (al == 8) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[16];\n bn_sqr_normal(rr->d, a->d, 8, t);\n#else\n bn_sqr_comba8(rr->d, a->d);\n#endif\n } else {\n#if defined(BN_RECURSION)\n if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {\n BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];\n bn_sqr_normal(rr->d, a->d, al, t);\n } else {\n int j, k;\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n if (al == j) {\n if (bn_wexpand(tmp, k * 2) == NULL)\n goto err;\n bn_sqr_recursive(rr->d, a->d, al, tmp->d);\n } else {\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n }\n }\n#else\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n#endif\n }\n rr->neg = 0;\n rr->top = max;\n rr->flags |= BN_FLG_FIXED_TOP;\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(rr);\n bn_check_top(tmp);\n BN_CTX_end(ctx);\n return ret;\n}', 'void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)\n{\n int i, j, max;\n const BN_ULONG *ap;\n BN_ULONG *rp;\n max = n * 2;\n ap = a;\n rp = r;\n rp[0] = rp[max - 1] = 0;\n rp++;\n j = n;\n if (--j > 0) {\n ap++;\n rp[j] = bn_mul_words(rp, ap, j, ap[-1]);\n rp += 2;\n }\n for (i = n - 2; i > 0; i--) {\n j--;\n ap++;\n rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);\n rp += 2;\n }\n bn_add_words(r, r, r, max);\n bn_sqr_words(tmp, a, n);\n bn_add_words(r, r, tmp, max);\n}']
|
2,640
| 0
|
https://github.com/openssl/openssl/blob/8b0d4242404f9e5da26e7594fa0864b2df4601af/crypto/bn/bn_lib.c/#L271
|
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
BN_ULONG *a = NULL;
bn_check_top(b);
if (words > (INT_MAX / (4 * BN_BITS2))) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
return NULL;
}
if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
return (NULL);
}
if (BN_get_flags(b, BN_FLG_SECURE))
a = OPENSSL_secure_zalloc(words * sizeof(*a));
else
a = OPENSSL_zalloc(words * sizeof(*a));
if (a == NULL) {
BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
return (NULL);
}
assert(b->top <= words);
if (b->top > 0)
memcpy(a, b->d, sizeof(*a) * b->top);
return a;
}
|
['static int file_lshift(STANZA *s)\n{\n BIGNUM *a = getBN(s, "A");\n BIGNUM *lshift = getBN(s, "LShift");\n BIGNUM *ret = BN_new();\n int n = 0;\n int st = 0;\n if (a == NULL || lshift == NULL || ret == NULL || !getint(s, &n, "N"))\n goto err;\n if (!BN_lshift(ret, a, n)\n || !equalBN("A << N", lshift, ret)\n || !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 fprintf(stderr, "Can\'t find %s in test at line %d\\n",\n attribute, s->start);\n return NULL;\n }\n if (parseBN(&ret, hex) != (int)strlen(hex)) {\n fprintf(stderr, "Could not decode \'%s\'.\\n", 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) && isxdigit((unsigned char)a[i]); i++)\n continue;\n if (i == 0 || i > INT_MAX/4)\n goto err;\n num = i + neg;\n if (bn == NULL)\n return (num);\n if (*bn == NULL) {\n if ((ret = BN_new()) == NULL)\n return (0);\n } else {\n ret = *bn;\n BN_zero(ret);\n }\n if (bn_expand(ret, i * 4) == NULL)\n goto err;\n j = 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 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}']
|
2,641
| 0
|
https://github.com/openssl/openssl/blob/a5fcd09e7552dedf87d5a1ff5d07a0397bc057cb/crypto/asn1/a_bytes.c/#L161
|
int i2d_ASN1_bytes(ASN1_STRING *a, unsigned char **pp, int tag, int xclass)
{
int ret,r,constructed;
unsigned char *p;
if (a == NULL) return(0);
if (tag == V_ASN1_BIT_STRING)
return(i2d_ASN1_BIT_STRING(a,pp));
ret=a->length;
r=ASN1_object_size(0,ret,tag);
if (pp == NULL) return(r);
p= *pp;
if ((tag == V_ASN1_SEQUENCE) || (tag == V_ASN1_SET))
constructed=1;
else
constructed=0;
ASN1_put_object(&p,constructed,ret,tag,xclass);
memcpy(p,a->data,a->length);
p+=a->length;
*pp= p;
return(r);
}
|
['int RSA_sign_ASN1_OCTET_STRING(int type, unsigned char *m, unsigned int m_len,\n\t unsigned char *sigret, unsigned int *siglen, RSA *rsa)\n\t{\n\tASN1_OCTET_STRING sig;\n\tint i,j,ret=1;\n\tunsigned char *p,*s;\n\tsig.type=V_ASN1_OCTET_STRING;\n\tsig.length=m_len;\n\tsig.data=m;\n\ti=i2d_ASN1_OCTET_STRING(&sig,NULL);\n\tj=RSA_size(rsa);\n\tif ((i-RSA_PKCS1_PADDING) > j)\n\t\t{\n\t\tRSAerr(RSA_F_RSA_SIGN_ASN1_OCTET_STRING,RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);\n\t\treturn(0);\n\t\t}\n\ts=(unsigned char *)Malloc((unsigned int)j+1);\n\tif (s == NULL)\n\t\t{\n\t\tRSAerr(RSA_F_RSA_SIGN_ASN1_OCTET_STRING,ERR_R_MALLOC_FAILURE);\n\t\treturn(0);\n\t\t}\n\tp=s;\n\ti2d_ASN1_OCTET_STRING(&sig,&p);\n\ti=RSA_private_encrypt(i,s,sigret,rsa,RSA_PKCS1_PADDING);\n\tif (i <= 0)\n\t\tret=0;\n\telse\n\t\t*siglen=i;\n\tmemset(s,0,(unsigned int)j+1);\n\tFree(s);\n\treturn(ret);\n\t}', 'int i2d_ASN1_OCTET_STRING(ASN1_OCTET_STRING *a, unsigned char **pp)\n{ return M_i2d_ASN1_OCTET_STRING(a, pp); }', 'int i2d_ASN1_bytes(ASN1_STRING *a, unsigned char **pp, int tag, int xclass)\n\t{\n\tint ret,r,constructed;\n\tunsigned char *p;\n\tif (a == NULL) return(0);\n\tif (tag == V_ASN1_BIT_STRING)\n\t\treturn(i2d_ASN1_BIT_STRING(a,pp));\n\tret=a->length;\n\tr=ASN1_object_size(0,ret,tag);\n\tif (pp == NULL) return(r);\n\tp= *pp;\n\tif ((tag == V_ASN1_SEQUENCE) || (tag == V_ASN1_SET))\n\t\tconstructed=1;\n\telse\n\t\tconstructed=0;\n\tASN1_put_object(&p,constructed,ret,tag,xclass);\n\tmemcpy(p,a->data,a->length);\n\tp+=a->length;\n\t*pp= p;\n\treturn(r);\n\t}', 'void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,\n\t int xclass)\n\t{\n\tunsigned char *p= *pp;\n\tint i;\n\ti=(constructed)?V_ASN1_CONSTRUCTED:0;\n\ti|=(xclass&V_ASN1_PRIVATE);\n\tif (tag < 31)\n\t\t*(p++)=i|(tag&V_ASN1_PRIMITIVE_TAG);\n\telse\n\t\t{\n\t\t*(p++)=i|V_ASN1_PRIMITIVE_TAG;\n\t\twhile (tag > 0x7f)\n\t\t\t{\n\t\t\t*(p++)=(tag&0x7f)|0x80;\n\t\t\ttag>>=7;\n\t\t\t}\n\t\t*(p++)=(tag&0x7f);\n\t\t}\n\tif ((constructed == 2) && (length == 0))\n\t\t*(p++)=0x80;\n\telse\n\t\tasn1_put_length(&p,length);\n\t*pp=p;\n\t}']
|
2,642
| 0
|
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/bn/bn_nist.c/#L448
|
int BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
BN_CTX *ctx)
{
int top = a->top, i;
int carry;
register BN_ULONG *r_d, *a_d = a->d;
union {
BN_ULONG bn[BN_NIST_192_TOP];
unsigned int ui[BN_NIST_192_TOP * sizeof(BN_ULONG) /
sizeof(unsigned int)];
} buf;
BN_ULONG c_d[BN_NIST_192_TOP], *res;
PTR_SIZE_INT mask;
static const BIGNUM _bignum_nist_p_192_sqr = {
(BN_ULONG *)_nist_p_192_sqr,
OSSL_NELEM(_nist_p_192_sqr),
OSSL_NELEM(_nist_p_192_sqr),
0, BN_FLG_STATIC_DATA
};
field = &_bignum_nist_p_192;
if (BN_is_negative(a) || BN_ucmp(a, &_bignum_nist_p_192_sqr) >= 0)
return BN_nnmod(r, a, field, ctx);
i = BN_ucmp(field, a);
if (i == 0) {
BN_zero(r);
return 1;
} else if (i > 0)
return (r == a) ? 1 : (BN_copy(r, a) != NULL);
if (r != a) {
if (!bn_wexpand(r, BN_NIST_192_TOP))
return 0;
r_d = r->d;
nist_cp_bn(r_d, a_d, BN_NIST_192_TOP);
} else
r_d = a_d;
nist_cp_bn_0(buf.bn, a_d + BN_NIST_192_TOP, top - BN_NIST_192_TOP,
BN_NIST_192_TOP);
#if defined(NIST_INT64)
{
NIST_INT64 acc;
unsigned int *rp = (unsigned int *)r_d;
const unsigned int *bp = (const unsigned int *)buf.ui;
acc = rp[0];
acc += bp[3 * 2 - 6];
acc += bp[5 * 2 - 6];
rp[0] = (unsigned int)acc;
acc >>= 32;
acc += rp[1];
acc += bp[3 * 2 - 5];
acc += bp[5 * 2 - 5];
rp[1] = (unsigned int)acc;
acc >>= 32;
acc += rp[2];
acc += bp[3 * 2 - 6];
acc += bp[4 * 2 - 6];
acc += bp[5 * 2 - 6];
rp[2] = (unsigned int)acc;
acc >>= 32;
acc += rp[3];
acc += bp[3 * 2 - 5];
acc += bp[4 * 2 - 5];
acc += bp[5 * 2 - 5];
rp[3] = (unsigned int)acc;
acc >>= 32;
acc += rp[4];
acc += bp[4 * 2 - 6];
acc += bp[5 * 2 - 6];
rp[4] = (unsigned int)acc;
acc >>= 32;
acc += rp[5];
acc += bp[4 * 2 - 5];
acc += bp[5 * 2 - 5];
rp[5] = (unsigned int)acc;
carry = (int)(acc >> 32);
}
#else
{
BN_ULONG t_d[BN_NIST_192_TOP];
nist_set_192(t_d, buf.bn, 0, 3, 3);
carry = (int)bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP);
nist_set_192(t_d, buf.bn, 4, 4, 0);
carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP);
nist_set_192(t_d, buf.bn, 5, 5, 5)
carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP);
}
#endif
if (carry > 0)
carry =
(int)bn_sub_words(r_d, r_d, _nist_p_192[carry - 1],
BN_NIST_192_TOP);
else
carry = 1;
mask =
0 - (PTR_SIZE_INT) bn_sub_words(c_d, r_d, _nist_p_192[0],
BN_NIST_192_TOP);
mask &= 0 - (PTR_SIZE_INT) carry;
res = c_d;
res = (BN_ULONG *)
(((PTR_SIZE_INT) res & ~mask) | ((PTR_SIZE_INT) r_d & mask));
nist_cp_bn(r_d, res, BN_NIST_192_TOP);
r->top = BN_NIST_192_TOP;
bn_correct_top(r);
return 1;
}
|
['int BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,\n BN_CTX *ctx)\n{\n int top = a->top, i;\n int carry;\n register BN_ULONG *r_d, *a_d = a->d;\n union {\n BN_ULONG bn[BN_NIST_192_TOP];\n unsigned int ui[BN_NIST_192_TOP * sizeof(BN_ULONG) /\n sizeof(unsigned int)];\n } buf;\n BN_ULONG c_d[BN_NIST_192_TOP], *res;\n PTR_SIZE_INT mask;\n static const BIGNUM _bignum_nist_p_192_sqr = {\n (BN_ULONG *)_nist_p_192_sqr,\n OSSL_NELEM(_nist_p_192_sqr),\n OSSL_NELEM(_nist_p_192_sqr),\n 0, BN_FLG_STATIC_DATA\n };\n field = &_bignum_nist_p_192;\n if (BN_is_negative(a) || BN_ucmp(a, &_bignum_nist_p_192_sqr) >= 0)\n return BN_nnmod(r, a, field, ctx);\n i = BN_ucmp(field, a);\n if (i == 0) {\n BN_zero(r);\n return 1;\n } else if (i > 0)\n return (r == a) ? 1 : (BN_copy(r, a) != NULL);\n if (r != a) {\n if (!bn_wexpand(r, BN_NIST_192_TOP))\n return 0;\n r_d = r->d;\n nist_cp_bn(r_d, a_d, BN_NIST_192_TOP);\n } else\n r_d = a_d;\n nist_cp_bn_0(buf.bn, a_d + BN_NIST_192_TOP, top - BN_NIST_192_TOP,\n BN_NIST_192_TOP);\n#if defined(NIST_INT64)\n {\n NIST_INT64 acc;\n unsigned int *rp = (unsigned int *)r_d;\n const unsigned int *bp = (const unsigned int *)buf.ui;\n acc = rp[0];\n acc += bp[3 * 2 - 6];\n acc += bp[5 * 2 - 6];\n rp[0] = (unsigned int)acc;\n acc >>= 32;\n acc += rp[1];\n acc += bp[3 * 2 - 5];\n acc += bp[5 * 2 - 5];\n rp[1] = (unsigned int)acc;\n acc >>= 32;\n acc += rp[2];\n acc += bp[3 * 2 - 6];\n acc += bp[4 * 2 - 6];\n acc += bp[5 * 2 - 6];\n rp[2] = (unsigned int)acc;\n acc >>= 32;\n acc += rp[3];\n acc += bp[3 * 2 - 5];\n acc += bp[4 * 2 - 5];\n acc += bp[5 * 2 - 5];\n rp[3] = (unsigned int)acc;\n acc >>= 32;\n acc += rp[4];\n acc += bp[4 * 2 - 6];\n acc += bp[5 * 2 - 6];\n rp[4] = (unsigned int)acc;\n acc >>= 32;\n acc += rp[5];\n acc += bp[4 * 2 - 5];\n acc += bp[5 * 2 - 5];\n rp[5] = (unsigned int)acc;\n carry = (int)(acc >> 32);\n }\n#else\n {\n BN_ULONG t_d[BN_NIST_192_TOP];\n nist_set_192(t_d, buf.bn, 0, 3, 3);\n carry = (int)bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP);\n nist_set_192(t_d, buf.bn, 4, 4, 0);\n carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP);\n nist_set_192(t_d, buf.bn, 5, 5, 5)\n carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP);\n }\n#endif\n if (carry > 0)\n carry =\n (int)bn_sub_words(r_d, r_d, _nist_p_192[carry - 1],\n BN_NIST_192_TOP);\n else\n carry = 1;\n mask =\n 0 - (PTR_SIZE_INT) bn_sub_words(c_d, r_d, _nist_p_192[0],\n BN_NIST_192_TOP);\n mask &= 0 - (PTR_SIZE_INT) carry;\n res = c_d;\n res = (BN_ULONG *)\n (((PTR_SIZE_INT) res & ~mask) | ((PTR_SIZE_INT) r_d & mask));\n nist_cp_bn(r_d, res, BN_NIST_192_TOP);\n r->top = BN_NIST_192_TOP;\n bn_correct_top(r);\n return 1;\n}']
|
2,643
| 0
|
https://github.com/libav/libav/blob/f40f329e9219a8dd7e585345a8ea294fa66562b9/ffmpeg.c/#L3085
|
static enum CodecID find_codec_or_die(const char *name, int type, int encoder)
{
const char *codec_string = encoder ? "encoder" : "decoder";
AVCodec *codec;
if(!name)
return CODEC_ID_NONE;
codec = encoder ?
avcodec_find_encoder_by_name(name) :
avcodec_find_decoder_by_name(name);
if(!codec) {
fprintf(stderr, "Unknown %s '%s'\n", codec_string, name);
av_exit(1);
}
if(codec->type != type) {
fprintf(stderr, "Invalid %s type '%s'\n", codec_string, name);
av_exit(1);
}
return codec->id;
}
|
['static enum CodecID find_codec_or_die(const char *name, int type, int encoder)\n{\n const char *codec_string = encoder ? "encoder" : "decoder";\n AVCodec *codec;\n if(!name)\n return CODEC_ID_NONE;\n codec = encoder ?\n avcodec_find_encoder_by_name(name) :\n avcodec_find_decoder_by_name(name);\n if(!codec) {\n fprintf(stderr, "Unknown %s \'%s\'\\n", codec_string, name);\n av_exit(1);\n }\n if(codec->type != type) {\n fprintf(stderr, "Invalid %s type \'%s\'\\n", codec_string, name);\n av_exit(1);\n }\n return codec->id;\n}']
|
2,644
| 0
|
https://github.com/libav/libav/blob/6cecd63005b29a1dc3a5104e6ac85fd112705122/libavcodec/aac.c/#L1618
|
static int aac_decode_frame(AVCodecContext * avccontext, void * data, int * data_size, AVPacket *avpkt) {
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size;
AACContext * ac = avccontext->priv_data;
ChannelElement * che = NULL;
GetBitContext gb;
enum RawDataBlockType elem_type;
int err, elem_id, data_size_tmp;
init_get_bits(&gb, buf, buf_size*8);
if (show_bits(&gb, 12) == 0xfff) {
if ((err = parse_adts_frame_header(ac, &gb)) < 0) {
av_log(avccontext, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
return -1;
}
if (ac->m4ac.sampling_index > 12) {
av_log(ac->avccontext, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->m4ac.sampling_index);
return -1;
}
}
while ((elem_type = get_bits(&gb, 3)) != TYPE_END) {
elem_id = get_bits(&gb, 4);
err = -1;
if(elem_type < TYPE_DSE && !(che=get_che(ac, elem_type, elem_id))) {
av_log(ac->avccontext, AV_LOG_ERROR, "channel element %d.%d is not allocated\n", elem_type, elem_id);
return -1;
}
switch (elem_type) {
case TYPE_SCE:
err = decode_ics(ac, &che->ch[0], &gb, 0, 0);
break;
case TYPE_CPE:
err = decode_cpe(ac, &gb, che);
break;
case TYPE_CCE:
err = decode_cce(ac, &gb, che);
break;
case TYPE_LFE:
err = decode_ics(ac, &che->ch[0], &gb, 0, 0);
break;
case TYPE_DSE:
skip_data_stream_element(&gb);
err = 0;
break;
case TYPE_PCE:
{
enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];
memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));
if((err = decode_pce(ac, new_che_pos, &gb)))
break;
err = output_configure(ac, ac->che_pos, new_che_pos, 0);
break;
}
case TYPE_FIL:
if (elem_id == 15)
elem_id += get_bits(&gb, 8) - 1;
while (elem_id > 0)
elem_id -= decode_extension_payload(ac, &gb, elem_id);
err = 0;
break;
default:
err = -1;
break;
}
if(err)
return err;
}
spectral_to_sample(ac);
if (!ac->is_saved) {
ac->is_saved = 1;
*data_size = 0;
return buf_size;
}
data_size_tmp = 1024 * avccontext->channels * sizeof(int16_t);
if(*data_size < data_size_tmp) {
av_log(avccontext, AV_LOG_ERROR,
"Output buffer too small (%d) or trying to output too many samples (%d) for this frame.\n",
*data_size, data_size_tmp);
return -1;
}
*data_size = data_size_tmp;
ac->dsp.float_to_int16_interleave(data, (const float **)ac->output_data, 1024, avccontext->channels);
return buf_size;
}
|
['static int aac_decode_frame(AVCodecContext * avccontext, void * data, int * data_size, AVPacket *avpkt) {\n const uint8_t *buf = avpkt->data;\n int buf_size = avpkt->size;\n AACContext * ac = avccontext->priv_data;\n ChannelElement * che = NULL;\n GetBitContext gb;\n enum RawDataBlockType elem_type;\n int err, elem_id, data_size_tmp;\n init_get_bits(&gb, buf, buf_size*8);\n if (show_bits(&gb, 12) == 0xfff) {\n if ((err = parse_adts_frame_header(ac, &gb)) < 0) {\n av_log(avccontext, AV_LOG_ERROR, "Error decoding AAC frame header.\\n");\n return -1;\n }\n if (ac->m4ac.sampling_index > 12) {\n av_log(ac->avccontext, AV_LOG_ERROR, "invalid sampling rate index %d\\n", ac->m4ac.sampling_index);\n return -1;\n }\n }\n while ((elem_type = get_bits(&gb, 3)) != TYPE_END) {\n elem_id = get_bits(&gb, 4);\n err = -1;\n if(elem_type < TYPE_DSE && !(che=get_che(ac, elem_type, elem_id))) {\n av_log(ac->avccontext, AV_LOG_ERROR, "channel element %d.%d is not allocated\\n", elem_type, elem_id);\n return -1;\n }\n switch (elem_type) {\n case TYPE_SCE:\n err = decode_ics(ac, &che->ch[0], &gb, 0, 0);\n break;\n case TYPE_CPE:\n err = decode_cpe(ac, &gb, che);\n break;\n case TYPE_CCE:\n err = decode_cce(ac, &gb, che);\n break;\n case TYPE_LFE:\n err = decode_ics(ac, &che->ch[0], &gb, 0, 0);\n break;\n case TYPE_DSE:\n skip_data_stream_element(&gb);\n err = 0;\n break;\n case TYPE_PCE:\n {\n enum ChannelPosition new_che_pos[4][MAX_ELEM_ID];\n memset(new_che_pos, 0, 4 * MAX_ELEM_ID * sizeof(new_che_pos[0][0]));\n if((err = decode_pce(ac, new_che_pos, &gb)))\n break;\n err = output_configure(ac, ac->che_pos, new_che_pos, 0);\n break;\n }\n case TYPE_FIL:\n if (elem_id == 15)\n elem_id += get_bits(&gb, 8) - 1;\n while (elem_id > 0)\n elem_id -= decode_extension_payload(ac, &gb, elem_id);\n err = 0;\n break;\n default:\n err = -1;\n break;\n }\n if(err)\n return err;\n }\n spectral_to_sample(ac);\n if (!ac->is_saved) {\n ac->is_saved = 1;\n *data_size = 0;\n return buf_size;\n }\n data_size_tmp = 1024 * avccontext->channels * sizeof(int16_t);\n if(*data_size < data_size_tmp) {\n av_log(avccontext, AV_LOG_ERROR,\n "Output buffer too small (%d) or trying to output too many samples (%d) for this frame.\\n",\n *data_size, data_size_tmp);\n return -1;\n }\n *data_size = data_size_tmp;\n ac->dsp.float_to_int16_interleave(data, (const float **)ac->output_data, 1024, avccontext->channels);\n return buf_size;\n}', 'static inline void init_get_bits(GetBitContext *s,\n const uint8_t *buffer, int bit_size)\n{\n int buffer_size= (bit_size+7)>>3;\n if(buffer_size < 0 || bit_size < 0) {\n buffer_size = bit_size = 0;\n buffer = NULL;\n }\n s->buffer= buffer;\n s->size_in_bits= bit_size;\n s->buffer_end= buffer + buffer_size;\n#ifdef ALT_BITSTREAM_READER\n s->index=0;\n#elif defined LIBMPEG2_BITSTREAM_READER\n s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));\n s->bit_count = 16 + 8*((intptr_t)buffer&1);\n skip_bits_long(s, 0);\n#elif defined A32_BITSTREAM_READER\n s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));\n s->bit_count = 32 + 8*((intptr_t)buffer&3);\n skip_bits_long(s, 0);\n#endif\n}', 'static inline unsigned int show_bits(GetBitContext *s, int n){\n register int tmp;\n OPEN_READER(re, s)\n UPDATE_CACHE(re, s)\n tmp= SHOW_UBITS(re, s, n);\n return tmp;\n}']
|
2,645
| 0
|
https://github.com/openssl/openssl/blob/305b68f1a2b6d4d0aa07a6ab47ac372f067a40bb/crypto/bn/bn_lib.c/#L231
|
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
BN_ULONG *a = NULL;
if (words > (INT_MAX / (4 * BN_BITS2))) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
return NULL;
}
if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
return NULL;
}
if (BN_get_flags(b, BN_FLG_SECURE))
a = OPENSSL_secure_zalloc(words * sizeof(*a));
else
a = OPENSSL_zalloc(words * sizeof(*a));
if (a == NULL) {
BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
return NULL;
}
assert(b->top <= words);
if (b->top > 0)
memcpy(a, b->d, sizeof(*a) * b->top);
return a;
}
|
['static DH *get_dh1024(void)\n{\n static unsigned char dh1024_p[] = {\n 0xF8, 0x81, 0x89, 0x7D, 0x14, 0x24, 0xC5, 0xD1, 0xE6, 0xF7, 0xBF,\n 0x3A,\n 0xE4, 0x90, 0xF4, 0xFC, 0x73, 0xFB, 0x34, 0xB5, 0xFA, 0x4C, 0x56,\n 0xA2,\n 0xEA, 0xA7, 0xE9, 0xC0, 0xC0, 0xCE, 0x89, 0xE1, 0xFA, 0x63, 0x3F,\n 0xB0,\n 0x6B, 0x32, 0x66, 0xF1, 0xD1, 0x7B, 0xB0, 0x00, 0x8F, 0xCA, 0x87,\n 0xC2,\n 0xAE, 0x98, 0x89, 0x26, 0x17, 0xC2, 0x05, 0xD2, 0xEC, 0x08, 0xD0,\n 0x8C,\n 0xFF, 0x17, 0x52, 0x8C, 0xC5, 0x07, 0x93, 0x03, 0xB1, 0xF6, 0x2F,\n 0xB8,\n 0x1C, 0x52, 0x47, 0x27, 0x1B, 0xDB, 0xD1, 0x8D, 0x9D, 0x69, 0x1D,\n 0x52,\n 0x4B, 0x32, 0x81, 0xAA, 0x7F, 0x00, 0xC8, 0xDC, 0xE6, 0xD9, 0xCC,\n 0xC1,\n 0x11, 0x2D, 0x37, 0x34, 0x6C, 0xEA, 0x02, 0x97, 0x4B, 0x0E, 0xBB,\n 0xB1,\n 0x71, 0x33, 0x09, 0x15, 0xFD, 0xDD, 0x23, 0x87, 0x07, 0x5E, 0x89,\n 0xAB,\n 0x6B, 0x7C, 0x5F, 0xEC, 0xA6, 0x24, 0xDC, 0x53,\n };\n static unsigned char dh1024_g[] = {\n 0x02,\n };\n DH *dh;\n BIGNUM *p, *g;\n if ((dh = DH_new()) == NULL)\n return NULL;\n p = BN_bin2bn(dh1024_p, sizeof(dh1024_p), NULL);\n g = BN_bin2bn(dh1024_g, sizeof(dh1024_g), NULL);\n if ((p == NULL) || (g == NULL) || !DH_set0_pqg(dh, p, NULL, g)) {\n DH_free(dh);\n BN_free(p);\n BN_free(g);\n return NULL;\n }\n return dh;\n}', 'BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)\n{\n unsigned int i, m;\n unsigned int n;\n BN_ULONG l;\n BIGNUM *bn = NULL;\n if (ret == NULL)\n ret = bn = BN_new();\n if (ret == NULL)\n return NULL;\n bn_check_top(ret);\n for ( ; len > 0 && *s == 0; s++, len--)\n continue;\n n = len;\n if (n == 0) {\n ret->top = 0;\n return ret;\n }\n i = ((n - 1) / BN_BYTES) + 1;\n m = ((n - 1) % (BN_BYTES));\n if (bn_wexpand(ret, (int)i) == NULL) {\n BN_free(bn);\n return NULL;\n }\n ret->top = i;\n ret->neg = 0;\n l = 0;\n while (n--) {\n l = (l << 8L) | *(s++);\n if (m-- == 0) {\n ret->d[--i] = l;\n l = 0;\n m = BN_BYTES - 1;\n }\n }\n bn_correct_top(ret);\n return ret;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n 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}']
|
2,646
| 0
|
https://github.com/openssl/openssl/blob/9c4fe782607d8542c5f55ef1b5c687fef1da5d75/crypto/lhash/lhash.c/#L240
|
void *lh_delete(LHASH *lh, const void *data)
{
unsigned long hash;
LHASH_NODE *nn,**rn;
void *ret;
lh->error=0;
rn=getrn(lh,data,&hash);
if (*rn == NULL)
{
lh->num_no_delete++;
return(NULL);
}
else
{
nn= *rn;
*rn=nn->next;
ret=nn->data;
OPENSSL_free(nn);
lh->num_delete++;
}
lh->num_items--;
if ((lh->num_nodes > MIN_NODES) &&
(lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes)))
contract(lh);
return(ret);
}
|
['int dtls1_accept(SSL *s)\n\t{\n\tBUF_MEM *buf;\n\tunsigned long l,Time=time(NULL);\n\tvoid (*cb)(const SSL *ssl,int type,int val)=NULL;\n\tlong num1;\n\tint ret= -1;\n\tint new_state,state,skip=0;\n\tRAND_add(&Time,sizeof(Time),0);\n\tERR_clear_error();\n\tclear_sys_error();\n\tif (s->info_callback != NULL)\n\t\tcb=s->info_callback;\n\telse if (s->ctx->info_callback != NULL)\n\t\tcb=s->ctx->info_callback;\n\ts->in_handshake++;\n\tif (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);\n\tif (s->cert == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_DTLS1_ACCEPT,SSL_R_NO_CERTIFICATE_SET);\n\t\treturn(-1);\n\t\t}\n\tfor (;;)\n\t\t{\n\t\tstate=s->state;\n\t\tswitch (s->state)\n\t\t\t{\n\t\tcase SSL_ST_RENEGOTIATE:\n\t\t\ts->new_session=1;\n\t\tcase SSL_ST_BEFORE:\n\t\tcase SSL_ST_ACCEPT:\n\t\tcase SSL_ST_BEFORE|SSL_ST_ACCEPT:\n\t\tcase SSL_ST_OK|SSL_ST_ACCEPT:\n\t\t\ts->server=1;\n\t\t\tif (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);\n\t\t\tif ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00))\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_DTLS1_ACCEPT, ERR_R_INTERNAL_ERROR);\n\t\t\t\treturn -1;\n\t\t\t\t}\n\t\t\ts->type=SSL_ST_ACCEPT;\n\t\t\tif (s->init_buf == NULL)\n\t\t\t\t{\n\t\t\t\tif ((buf=BUF_MEM_new()) == NULL)\n\t\t\t\t\t{\n\t\t\t\t\tret= -1;\n\t\t\t\t\tgoto end;\n\t\t\t\t\t}\n\t\t\t\tif (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))\n\t\t\t\t\t{\n\t\t\t\t\tret= -1;\n\t\t\t\t\tgoto end;\n\t\t\t\t\t}\n\t\t\t\ts->init_buf=buf;\n\t\t\t\t}\n\t\t\tif (!ssl3_setup_buffers(s))\n\t\t\t\t{\n\t\t\t\tret= -1;\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\ts->init_num=0;\n\t\t\tif (s->state != SSL_ST_RENEGOTIATE)\n\t\t\t\t{\n\t\t\t\tif (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; }\n\t\t\t\tssl3_init_finished_mac(s);\n\t\t\t\ts->state=SSL3_ST_SR_CLNT_HELLO_A;\n\t\t\t\ts->ctx->stats.sess_accept++;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\ts->ctx->stats.sess_accept_renegotiate++;\n\t\t\t\ts->state=SSL3_ST_SW_HELLO_REQ_A;\n\t\t\t\t}\n if ( (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE))\n s->d1->send_cookie = 1;\n else\n s->d1->send_cookie = 0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_SW_HELLO_REQ_A:\n\t\tcase SSL3_ST_SW_HELLO_REQ_B:\n\t\t\ts->shutdown=0;\n\t\t\tret=dtls1_send_hello_request(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C;\n\t\t\ts->state=SSL3_ST_SW_FLUSH;\n\t\t\ts->init_num=0;\n\t\t\tssl3_init_finished_mac(s);\n\t\t\tbreak;\n\t\tcase SSL3_ST_SW_HELLO_REQ_C:\n\t\t\ts->state=SSL_ST_OK;\n\t\t\tbreak;\n\t\tcase SSL3_ST_SR_CLNT_HELLO_A:\n\t\tcase SSL3_ST_SR_CLNT_HELLO_B:\n\t\tcase SSL3_ST_SR_CLNT_HELLO_C:\n\t\t\ts->shutdown=0;\n\t\t\tret=ssl3_get_client_hello(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->new_session = 2;\n\t\t\tif ( s->d1->send_cookie)\n\t\t\t\ts->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A;\n\t\t\telse\n\t\t\t\ts->state = SSL3_ST_SW_SRVR_HELLO_A;\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A:\n\t\tcase DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B:\n\t\t\tret = dtls1_send_hello_verify_request(s);\n\t\t\tif ( ret <= 0) goto end;\n\t\t\ts->d1->send_cookie = 0;\n\t\t\ts->state=SSL3_ST_SW_FLUSH;\n\t\t\ts->s3->tmp.next_state=SSL3_ST_SR_CLNT_HELLO_A;\n\t\t\tbreak;\n\t\tcase SSL3_ST_SW_SRVR_HELLO_A:\n\t\tcase SSL3_ST_SW_SRVR_HELLO_B:\n\t\t\tret=dtls1_send_server_hello(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\tif (s->hit)\n\t\t\t\ts->state=SSL3_ST_SW_CHANGE_A;\n\t\t\telse\n\t\t\t\ts->state=SSL3_ST_SW_CERT_A;\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_SW_CERT_A:\n\t\tcase SSL3_ST_SW_CERT_B:\n\t\t\tif (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))\n\t\t\t\t{\n\t\t\t\tret=dtls1_send_server_certificate(s);\n\t\t\t\tif (ret <= 0) goto end;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\tskip=1;\n\t\t\ts->state=SSL3_ST_SW_KEY_EXCH_A;\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_SW_KEY_EXCH_A:\n\t\tcase SSL3_ST_SW_KEY_EXCH_B:\n\t\t\tl=s->s3->tmp.new_cipher->algorithms;\n\t\t\tif ((s->options & SSL_OP_EPHEMERAL_RSA)\n#ifndef OPENSSL_NO_KRB5\n\t\t\t\t&& !(l & SSL_KRB5)\n#endif\n\t\t\t\t)\n\t\t\t\ts->s3->tmp.use_rsa_tmp=1;\n\t\t\telse\n\t\t\t\ts->s3->tmp.use_rsa_tmp=0;\n\t\t\tif (s->s3->tmp.use_rsa_tmp\n\t\t\t || (l & (SSL_DH|SSL_kFZA))\n\t\t\t || ((l & SSL_kRSA)\n\t\t\t\t&& (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL\n\t\t\t\t || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)\n\t\t\t\t\t&& EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)\n\t\t\t\t\t)\n\t\t\t\t )\n\t\t\t\t)\n\t\t\t )\n\t\t\t\t{\n\t\t\t\tret=dtls1_send_server_key_exchange(s);\n\t\t\t\tif (ret <= 0) goto end;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\tskip=1;\n\t\t\ts->state=SSL3_ST_SW_CERT_REQ_A;\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_SW_CERT_REQ_A:\n\t\tcase SSL3_ST_SW_CERT_REQ_B:\n\t\t\tif (\n\t\t\t\t!(s->verify_mode & SSL_VERIFY_PEER) ||\n\t\t\t\t((s->session->peer != NULL) &&\n\t\t\t\t (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||\n\t\t\t\t((s->s3->tmp.new_cipher->algorithms & SSL_aNULL) &&\n\t\t\t\t !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||\n (s->s3->tmp.new_cipher->algorithms & SSL_aKRB5))\n\t\t\t\t{\n\t\t\t\tskip=1;\n\t\t\t\ts->s3->tmp.cert_request=0;\n\t\t\t\ts->state=SSL3_ST_SW_SRVR_DONE_A;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\ts->s3->tmp.cert_request=1;\n\t\t\t\tret=dtls1_send_certificate_request(s);\n\t\t\t\tif (ret <= 0) goto end;\n#ifndef NETSCAPE_HANG_BUG\n\t\t\t\ts->state=SSL3_ST_SW_SRVR_DONE_A;\n#else\n\t\t\t\ts->state=SSL3_ST_SW_FLUSH;\n\t\t\t\ts->s3->tmp.next_state=SSL3_ST_SR_CERT_A;\n#endif\n\t\t\t\ts->init_num=0;\n\t\t\t\t}\n\t\t\tbreak;\n\t\tcase SSL3_ST_SW_SRVR_DONE_A:\n\t\tcase SSL3_ST_SW_SRVR_DONE_B:\n\t\t\tret=dtls1_send_server_done(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->s3->tmp.next_state=SSL3_ST_SR_CERT_A;\n\t\t\ts->state=SSL3_ST_SW_FLUSH;\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_SW_FLUSH:\n\t\t\tnum1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);\n\t\t\tif (num1 > 0)\n\t\t\t\t{\n\t\t\t\ts->rwstate=SSL_WRITING;\n\t\t\t\tnum1=BIO_flush(s->wbio);\n\t\t\t\tif (num1 <= 0) { ret= -1; goto end; }\n\t\t\t\ts->rwstate=SSL_NOTHING;\n\t\t\t\t}\n\t\t\ts->state=s->s3->tmp.next_state;\n\t\t\tbreak;\n\t\tcase SSL3_ST_SR_CERT_A:\n\t\tcase SSL3_ST_SR_CERT_B:\n\t\t\tret = ssl3_check_client_hello(s);\n\t\t\tif (ret <= 0)\n\t\t\t\tgoto end;\n\t\t\tif (ret == 2)\n\t\t\t\ts->state = SSL3_ST_SR_CLNT_HELLO_C;\n\t\t\telse {\n\t\t\t\tret=ssl3_get_client_certificate(s);\n\t\t\t\tif (ret <= 0) goto end;\n\t\t\t\ts->init_num=0;\n\t\t\t\ts->state=SSL3_ST_SR_KEY_EXCH_A;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase SSL3_ST_SR_KEY_EXCH_A:\n\t\tcase SSL3_ST_SR_KEY_EXCH_B:\n\t\t\tret=ssl3_get_client_key_exchange(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->state=SSL3_ST_SR_CERT_VRFY_A;\n\t\t\ts->init_num=0;\n\t\t\ts->method->ssl3_enc->cert_verify_mac(s,\n\t\t\t\t&(s->s3->finish_dgst1),\n\t\t\t\t&(s->s3->tmp.cert_verify_md[0]));\n\t\t\ts->method->ssl3_enc->cert_verify_mac(s,\n\t\t\t\t&(s->s3->finish_dgst2),\n\t\t\t\t&(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]));\n\t\t\tbreak;\n\t\tcase SSL3_ST_SR_CERT_VRFY_A:\n\t\tcase SSL3_ST_SR_CERT_VRFY_B:\n\t\t\tret=ssl3_get_cert_verify(s);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->state=SSL3_ST_SR_FINISHED_A;\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_SR_FINISHED_A:\n\t\tcase SSL3_ST_SR_FINISHED_B:\n\t\t\tret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A,\n\t\t\t\tSSL3_ST_SR_FINISHED_B);\n\t\t\tif (ret <= 0) goto end;\n\t\t\tif (s->hit)\n\t\t\t\ts->state=SSL_ST_OK;\n\t\t\telse\n\t\t\t\ts->state=SSL3_ST_SW_CHANGE_A;\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL3_ST_SW_CHANGE_A:\n\t\tcase SSL3_ST_SW_CHANGE_B:\n\t\t\ts->session->cipher=s->s3->tmp.new_cipher;\n\t\t\tif (!s->method->ssl3_enc->setup_key_block(s))\n\t\t\t\t{ ret= -1; goto end; }\n\t\t\tret=dtls1_send_change_cipher_spec(s,\n\t\t\t\tSSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->state=SSL3_ST_SW_FINISHED_A;\n\t\t\ts->init_num=0;\n\t\t\tif (!s->method->ssl3_enc->change_cipher_state(s,\n\t\t\t\tSSL3_CHANGE_CIPHER_SERVER_WRITE))\n\t\t\t\t{\n\t\t\t\tret= -1;\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\tdtls1_reset_seq_numbers(s, SSL3_CC_WRITE);\n\t\t\tbreak;\n\t\tcase SSL3_ST_SW_FINISHED_A:\n\t\tcase SSL3_ST_SW_FINISHED_B:\n\t\t\tret=dtls1_send_finished(s,\n\t\t\t\tSSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B,\n\t\t\t\ts->method->ssl3_enc->server_finished_label,\n\t\t\t\ts->method->ssl3_enc->server_finished_label_len);\n\t\t\tif (ret <= 0) goto end;\n\t\t\ts->state=SSL3_ST_SW_FLUSH;\n\t\t\tif (s->hit)\n\t\t\t\ts->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A;\n\t\t\telse\n\t\t\t\ts->s3->tmp.next_state=SSL_ST_OK;\n\t\t\ts->init_num=0;\n\t\t\tbreak;\n\t\tcase SSL_ST_OK:\n\t\t\tssl3_cleanup_key_block(s);\n#if 0\n\t\t\tBUF_MEM_free(s->init_buf);\n\t\t\ts->init_buf=NULL;\n#endif\n\t\t\tssl_free_wbio_buffer(s);\n\t\t\ts->init_num=0;\n\t\t\tif (s->new_session == 2)\n\t\t\t\t{\n\t\t\t\ts->new_session=0;\n\t\t\t\tssl_update_cache(s,SSL_SESS_CACHE_SERVER);\n\t\t\t\ts->ctx->stats.sess_accept_good++;\n\t\t\t\ts->handshake_func=dtls1_accept;\n\t\t\t\tif (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);\n\t\t\t\t}\n\t\t\tret = 1;\n\t\t\ts->d1->handshake_read_seq = 0;\n\t\t\ts->d1->handshake_write_seq = 0;\n\t\t\tgoto end;\n\t\tdefault:\n\t\t\tSSLerr(SSL_F_DTLS1_ACCEPT,SSL_R_UNKNOWN_STATE);\n\t\t\tret= -1;\n\t\t\tgoto end;\n\t\t\t}\n\t\tif (!s->s3->tmp.reuse_message && !skip)\n\t\t\t{\n\t\t\tif (s->debug)\n\t\t\t\t{\n\t\t\t\tif ((ret=BIO_flush(s->wbio)) <= 0)\n\t\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\tif ((cb != NULL) && (s->state != state))\n\t\t\t\t{\n\t\t\t\tnew_state=s->state;\n\t\t\t\ts->state=state;\n\t\t\t\tcb(s,SSL_CB_ACCEPT_LOOP,1);\n\t\t\t\ts->state=new_state;\n\t\t\t\t}\n\t\t\t}\n\t\tskip=0;\n\t\t}\nend:\n\ts->in_handshake--;\n\tif (cb != NULL)\n\t\tcb(s,SSL_CB_ACCEPT_EXIT,ret);\n\treturn(ret);\n\t}', 'int 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->algorithms & SSL_MKEY_MASK;\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->algorithms & 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}', 'void ssl3_send_alert(SSL *s, int level, int desc)\n\t{\n\tdesc=s->method->ssl3_enc->alert_value(desc);\n\tif (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION)\n\t\tdesc = SSL_AD_HANDSHAKE_FAILURE;\n\tif (desc < 0) return;\n\tif ((level == 2) && (s->session != NULL))\n\t\tSSL_CTX_remove_session(s->ctx,s->session);\n\ts->s3->alert_dispatch=1;\n\ts->s3->send_alert[0]=level;\n\ts->s3->send_alert[1]=desc;\n\tif (s->s3->wbuf.left == 0)\n\t\ts->method->ssl_dispatch_alert(s);\n\t}', 'int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)\n{\n\treturn remove_session_lock(ctx, c, 1);\n}', 'static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)\n\t{\n\tSSL_SESSION *r;\n\tint ret=0;\n\tif ((c != NULL) && (c->session_id_length != 0))\n\t\t{\n\t\tif(lck) CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);\n\t\tif ((r = (SSL_SESSION *)lh_retrieve(ctx->sessions,c)) == c)\n\t\t\t{\n\t\t\tret=1;\n\t\t\tr=(SSL_SESSION *)lh_delete(ctx->sessions,c);\n\t\t\tSSL_SESSION_list_remove(ctx,c);\n\t\t\t}\n\t\tif(lck) CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n\t\tif (ret)\n\t\t\t{\n\t\t\tr->not_resumable=1;\n\t\t\tif (ctx->remove_session_cb != NULL)\n\t\t\t\tctx->remove_session_cb(ctx,r);\n\t\t\tSSL_SESSION_free(r);\n\t\t\t}\n\t\t}\n\telse\n\t\tret=0;\n\treturn(ret);\n\t}', 'void *lh_delete(LHASH *lh, const void *data)\n\t{\n\tunsigned long hash;\n\tLHASH_NODE *nn,**rn;\n\tvoid *ret;\n\tlh->error=0;\n\trn=getrn(lh,data,&hash);\n\tif (*rn == NULL)\n\t\t{\n\t\tlh->num_no_delete++;\n\t\treturn(NULL);\n\t\t}\n\telse\n\t\t{\n\t\tnn= *rn;\n\t\t*rn=nn->next;\n\t\tret=nn->data;\n\t\tOPENSSL_free(nn);\n\t\tlh->num_delete++;\n\t\t}\n\tlh->num_items--;\n\tif ((lh->num_nodes > MIN_NODES) &&\n\t\t(lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes)))\n\t\tcontract(lh);\n\treturn(ret);\n\t}']
|
2,647
| 0
|
https://github.com/libav/libav/blob/cf6bae6883607f83f3b042b7b9d711197f736e2a/libavcodec/mpegaudiodec.c/#L710
|
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}']
|
2,648
| 0
|
https://github.com/libav/libav/blob/0dc7df28dd50167722550fb20c57327398e32e9f/ffmpeg.c/#L3461
|
static void opt_output_file(const char *filename)
{
AVFormatContext *oc;
int use_video, use_audio, use_subtitle;
int input_has_video, input_has_audio, input_has_subtitle;
AVFormatParameters params, *ap = ¶ms;
AVOutputFormat *file_oformat;
if (!strcmp(filename, "-"))
filename = "pipe:";
oc = avformat_alloc_context();
if (!oc) {
print_error(filename, AVERROR(ENOMEM));
av_exit(1);
}
if (last_asked_format) {
file_oformat = av_guess_format(last_asked_format, NULL, NULL);
if (!file_oformat) {
fprintf(stderr, "Requested output format '%s' is not a suitable output format\n", last_asked_format);
av_exit(1);
}
last_asked_format = NULL;
} else {
file_oformat = av_guess_format(NULL, filename, NULL);
if (!file_oformat) {
fprintf(stderr, "Unable to find a suitable output format for '%s'\n",
filename);
av_exit(1);
}
}
oc->oformat = file_oformat;
av_strlcpy(oc->filename, filename, sizeof(oc->filename));
if (!strcmp(file_oformat->name, "ffm") &&
av_strstart(filename, "http:", NULL)) {
int err = read_ffserver_streams(oc, filename);
if (err < 0) {
print_error(filename, err);
av_exit(1);
}
} else {
use_video = file_oformat->video_codec != CODEC_ID_NONE || video_stream_copy || video_codec_name;
use_audio = file_oformat->audio_codec != CODEC_ID_NONE || audio_stream_copy || audio_codec_name;
use_subtitle = file_oformat->subtitle_codec != CODEC_ID_NONE || subtitle_stream_copy || subtitle_codec_name;
if (nb_input_files > 0) {
check_audio_video_sub_inputs(&input_has_video, &input_has_audio,
&input_has_subtitle);
if (!input_has_video)
use_video = 0;
if (!input_has_audio)
use_audio = 0;
if (!input_has_subtitle)
use_subtitle = 0;
}
if (audio_disable) {
use_audio = 0;
}
if (video_disable) {
use_video = 0;
}
if (subtitle_disable) {
use_subtitle = 0;
}
if (use_video) {
new_video_stream(oc);
}
if (use_audio) {
new_audio_stream(oc);
}
if (use_subtitle) {
new_subtitle_stream(oc);
}
oc->timestamp = rec_timestamp;
for(; metadata_count>0; metadata_count--){
av_metadata_set(&oc->metadata, metadata[metadata_count-1].key,
metadata[metadata_count-1].value);
}
av_metadata_conv(oc, oc->oformat->metadata_conv, NULL);
}
output_files[nb_output_files++] = oc;
if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
if (!av_filename_number_test(oc->filename)) {
print_error(oc->filename, AVERROR_NUMEXPECTED);
av_exit(1);
}
}
if (!(oc->oformat->flags & AVFMT_NOFILE)) {
if (!file_overwrite &&
(strchr(filename, ':') == NULL ||
filename[1] == ':' ||
av_strstart(filename, "file:", NULL))) {
if (url_exist(filename)) {
if (!using_stdin) {
fprintf(stderr,"File '%s' already exists. Overwrite ? [y/N] ", filename);
fflush(stderr);
if (!read_yesno()) {
fprintf(stderr, "Not overwriting - exiting\n");
av_exit(1);
}
}
else {
fprintf(stderr,"File '%s' already exists. Exiting.\n", filename);
av_exit(1);
}
}
}
if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
fprintf(stderr, "Could not open '%s'\n", filename);
av_exit(1);
}
}
memset(ap, 0, sizeof(*ap));
if (av_set_parameters(oc, ap) < 0) {
fprintf(stderr, "%s: Invalid encoding parameters\n",
oc->filename);
av_exit(1);
}
oc->preload= (int)(mux_preload*AV_TIME_BASE);
oc->max_delay= (int)(mux_max_delay*AV_TIME_BASE);
oc->loop_output = loop_output;
oc->flags |= AVFMT_FLAG_NONBLOCK;
set_context_opts(oc, avformat_opts, AV_OPT_FLAG_ENCODING_PARAM);
}
|
['static void opt_output_file(const char *filename)\n{\n AVFormatContext *oc;\n int use_video, use_audio, use_subtitle;\n int input_has_video, input_has_audio, input_has_subtitle;\n AVFormatParameters params, *ap = ¶ms;\n AVOutputFormat *file_oformat;\n if (!strcmp(filename, "-"))\n filename = "pipe:";\n oc = avformat_alloc_context();\n if (!oc) {\n print_error(filename, AVERROR(ENOMEM));\n av_exit(1);\n }\n if (last_asked_format) {\n file_oformat = av_guess_format(last_asked_format, NULL, NULL);\n if (!file_oformat) {\n fprintf(stderr, "Requested output format \'%s\' is not a suitable output format\\n", last_asked_format);\n av_exit(1);\n }\n last_asked_format = NULL;\n } else {\n file_oformat = av_guess_format(NULL, filename, NULL);\n if (!file_oformat) {\n fprintf(stderr, "Unable to find a suitable output format for \'%s\'\\n",\n filename);\n av_exit(1);\n }\n }\n oc->oformat = file_oformat;\n av_strlcpy(oc->filename, filename, sizeof(oc->filename));\n if (!strcmp(file_oformat->name, "ffm") &&\n av_strstart(filename, "http:", NULL)) {\n int err = read_ffserver_streams(oc, filename);\n if (err < 0) {\n print_error(filename, err);\n av_exit(1);\n }\n } else {\n use_video = file_oformat->video_codec != CODEC_ID_NONE || video_stream_copy || video_codec_name;\n use_audio = file_oformat->audio_codec != CODEC_ID_NONE || audio_stream_copy || audio_codec_name;\n use_subtitle = file_oformat->subtitle_codec != CODEC_ID_NONE || subtitle_stream_copy || subtitle_codec_name;\n if (nb_input_files > 0) {\n check_audio_video_sub_inputs(&input_has_video, &input_has_audio,\n &input_has_subtitle);\n if (!input_has_video)\n use_video = 0;\n if (!input_has_audio)\n use_audio = 0;\n if (!input_has_subtitle)\n use_subtitle = 0;\n }\n if (audio_disable) {\n use_audio = 0;\n }\n if (video_disable) {\n use_video = 0;\n }\n if (subtitle_disable) {\n use_subtitle = 0;\n }\n if (use_video) {\n new_video_stream(oc);\n }\n if (use_audio) {\n new_audio_stream(oc);\n }\n if (use_subtitle) {\n new_subtitle_stream(oc);\n }\n oc->timestamp = rec_timestamp;\n for(; metadata_count>0; metadata_count--){\n av_metadata_set(&oc->metadata, metadata[metadata_count-1].key,\n metadata[metadata_count-1].value);\n }\n av_metadata_conv(oc, oc->oformat->metadata_conv, NULL);\n }\n output_files[nb_output_files++] = oc;\n if (oc->oformat->flags & AVFMT_NEEDNUMBER) {\n if (!av_filename_number_test(oc->filename)) {\n print_error(oc->filename, AVERROR_NUMEXPECTED);\n av_exit(1);\n }\n }\n if (!(oc->oformat->flags & AVFMT_NOFILE)) {\n if (!file_overwrite &&\n (strchr(filename, \':\') == NULL ||\n filename[1] == \':\' ||\n av_strstart(filename, "file:", NULL))) {\n if (url_exist(filename)) {\n if (!using_stdin) {\n fprintf(stderr,"File \'%s\' already exists. Overwrite ? [y/N] ", filename);\n fflush(stderr);\n if (!read_yesno()) {\n fprintf(stderr, "Not overwriting - exiting\\n");\n av_exit(1);\n }\n }\n else {\n fprintf(stderr,"File \'%s\' already exists. Exiting.\\n", filename);\n av_exit(1);\n }\n }\n }\n if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {\n fprintf(stderr, "Could not open \'%s\'\\n", filename);\n av_exit(1);\n }\n }\n memset(ap, 0, sizeof(*ap));\n if (av_set_parameters(oc, ap) < 0) {\n fprintf(stderr, "%s: Invalid encoding parameters\\n",\n oc->filename);\n av_exit(1);\n }\n oc->preload= (int)(mux_preload*AV_TIME_BASE);\n oc->max_delay= (int)(mux_max_delay*AV_TIME_BASE);\n oc->loop_output = loop_output;\n oc->flags |= AVFMT_FLAG_NONBLOCK;\n set_context_opts(oc, avformat_opts, AV_OPT_FLAG_ENCODING_PARAM);\n}', 'AVFormatContext *avformat_alloc_context(void)\n{\n AVFormatContext *ic;\n ic = av_malloc(sizeof(AVFormatContext));\n if (!ic) return ic;\n avformat_get_context_defaults(ic);\n ic->av_class = &av_format_context_class;\n return ic;\n}', 'void *av_malloc(unsigned int size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-16) )\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size+16);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&15) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr,16,size))\n ptr = NULL;\n#elif HAVE_MEMALIGN\n ptr = memalign(16,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}', 'void print_error(const char *filename, int err)\n{\n char errbuf[128];\n switch(err) {\n#if CONFIG_NETWORK\n case AVERROR(FF_NETERROR(EPROTONOSUPPORT)):\n fprintf(stderr, "%s: Unsupported network protocol\\n", filename);\n break;\n#endif\n default:\n av_strerror(err, errbuf, sizeof(errbuf));\n fprintf(stderr, "%s: %s\\n", filename, errbuf);\n }\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_NOTSUPP: errstr = "Operation not supported"; 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 snprintf(errbuf, errbuf_size, "Error number %d occurred", errnum);\n#endif\n }\n return ret;\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}']
|
2,649
| 0
|
https://github.com/openssl/openssl/blob/95ed0e7c1f4206191c1b0288e352010e70e252db/apps/apps.c/#L2823
|
BIO *bio_open_owner(const char *filename, int format, int private)
{
FILE *fp = NULL;
BIO *b = NULL;
int fd = -1, bflags, mode, binmode;
if (!private || filename == NULL || strcmp(filename, "-") == 0)
return bio_open_default(filename, 'w', format);
mode = O_WRONLY;
#ifdef O_CREAT
mode |= O_CREAT;
#endif
#ifdef O_TRUNC
mode |= O_TRUNC;
#endif
binmode = istext(format);
if (binmode) {
#ifdef O_BINARY
mode |= O_BINARY;
#elif defined(_O_BINARY)
mode |= _O_BINARY;
#endif
}
fd = open(filename, mode, 0600);
if (fd < 0)
goto err;
fp = fdopen(fd, modestr('w', format));
if (fp == NULL)
goto err;
bflags = BIO_CLOSE;
if (!binmode)
bflags |= BIO_FP_TEXT;
b = BIO_new_fp(fp, bflags);
if (b)
return b;
err:
BIO_printf(bio_err, "%s: Can't open \"%s\" for writing, %s\n",
opt_getprog(), filename, strerror(errno));
ERR_print_errors(bio_err);
if (fp)
fclose(fp);
else if (fd >= 0)
close(fd);
return NULL;
}
|
['BIO *bio_open_owner(const char *filename, int format, int private)\n{\n FILE *fp = NULL;\n BIO *b = NULL;\n int fd = -1, bflags, mode, binmode;\n if (!private || filename == NULL || strcmp(filename, "-") == 0)\n return bio_open_default(filename, \'w\', format);\n mode = O_WRONLY;\n#ifdef O_CREAT\n mode |= O_CREAT;\n#endif\n#ifdef O_TRUNC\n mode |= O_TRUNC;\n#endif\n binmode = istext(format);\n if (binmode) {\n#ifdef O_BINARY\n mode |= O_BINARY;\n#elif defined(_O_BINARY)\n mode |= _O_BINARY;\n#endif\n }\n fd = open(filename, mode, 0600);\n if (fd < 0)\n goto err;\n fp = fdopen(fd, modestr(\'w\', format));\n if (fp == NULL)\n goto err;\n bflags = BIO_CLOSE;\n if (!binmode)\n bflags |= BIO_FP_TEXT;\n b = BIO_new_fp(fp, bflags);\n if (b)\n return b;\n err:\n BIO_printf(bio_err, "%s: Can\'t open \\"%s\\" for writing, %s\\n",\n opt_getprog(), filename, strerror(errno));\n ERR_print_errors(bio_err);\n if (fp)\n fclose(fp);\n else if (fd >= 0)\n close(fd);\n return NULL;\n}', 'static int istext(int format)\n{\n return (format & B_FORMAT_TEXT) == B_FORMAT_TEXT;\n}', 'static const char *modestr(char mode, int format)\n{\n OPENSSL_assert(mode == \'a\' || mode == \'r\' || mode == \'w\');\n switch (mode) {\n case \'a\':\n return istext(format) ? "a" : "ab";\n case \'r\':\n return istext(format) ? "r" : "rb";\n case \'w\':\n return istext(format) ? "w" : "wb";\n }\n return NULL;\n}']
|
2,650
| 0
|
https://github.com/openssl/openssl/blob/260a16f33682a819414fcba6161708a5e6bdff50/crypto/ex_data.c/#L39
|
static EX_CALLBACKS *get_and_lock(OPENSSL_CTX *ctx, int class_index)
{
EX_CALLBACKS *ip;
OSSL_EX_DATA_GLOBAL *global = NULL;
if (class_index < 0 || class_index >= CRYPTO_EX_INDEX__COUNT) {
CRYPTOerr(CRYPTO_F_GET_AND_LOCK, ERR_R_PASSED_INVALID_ARGUMENT);
return NULL;
}
global = openssl_ctx_get_ex_data_global(ctx);
if (global->ex_data_lock == NULL) {
return NULL;
}
ip = &global->ex_data[class_index];
CRYPTO_THREAD_write_lock(global->ex_data_lock);
return ip;
}
|
['static EX_CALLBACKS *get_and_lock(OPENSSL_CTX *ctx, int class_index)\n{\n EX_CALLBACKS *ip;\n OSSL_EX_DATA_GLOBAL *global = NULL;\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 global = openssl_ctx_get_ex_data_global(ctx);\n if (global->ex_data_lock == NULL) {\n return NULL;\n }\n ip = &global->ex_data[class_index];\n CRYPTO_THREAD_write_lock(global->ex_data_lock);\n return ip;\n}', 'OSSL_EX_DATA_GLOBAL *openssl_ctx_get_ex_data_global(OPENSSL_CTX *ctx)\n{\n#ifndef FIPS_MODE\n if (ctx == NULL) {\n if (!RUN_ONCE(&default_context_init, do_default_context_init))\n return NULL;\n ctx = default_context;\n }\n#endif\n if (ctx == NULL)\n return NULL;\n return &ctx->global;\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}']
|
2,651
| 0
|
https://github.com/openssl/openssl/blob/f325fba50c6e1cab053e449c420f76256b551867/apps/apps.c/#L1902
|
int pkey_ctrl_string(EVP_PKEY_CTX *ctx, const char *value)
{
int rv;
char *stmp, *vtmp = NULL;
stmp = OPENSSL_strdup(value);
if (!stmp)
return -1;
vtmp = strchr(stmp, ':');
if (vtmp) {
*vtmp = 0;
vtmp++;
}
rv = EVP_PKEY_CTX_ctrl_str(ctx, stmp, vtmp);
OPENSSL_free(stmp);
return rv;
}
|
["int pkey_ctrl_string(EVP_PKEY_CTX *ctx, const char *value)\n{\n int rv;\n char *stmp, *vtmp = NULL;\n stmp = OPENSSL_strdup(value);\n if (!stmp)\n return -1;\n vtmp = strchr(stmp, ':');\n if (vtmp) {\n *vtmp = 0;\n vtmp++;\n }\n rv = EVP_PKEY_CTX_ctrl_str(ctx, stmp, vtmp);\n OPENSSL_free(stmp);\n return rv;\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}', '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 (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 EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,\n const char *name, const char *value)\n{\n if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {\n EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);\n return -2;\n }\n if (strcmp(name, "digest") == 0) {\n const EVP_MD *md;\n if (value == NULL || (md = EVP_get_digestbyname(value)) == NULL) {\n EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_INVALID_DIGEST);\n return 0;\n }\n return EVP_PKEY_CTX_set_signature_md(ctx, md);\n }\n return ctx->pmeth->ctrl_str(ctx, name, value);\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}']
|
2,652
| 0
|
https://github.com/openssl/openssl/blob/8fa6a40be2935ca109a28cc43d28cd27051ada01/crypto/bn/bn_ctx.c/#L353
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['BN_BLINDING *BN_BLINDING_create_param(BN_BLINDING *b,\n\tconst BIGNUM *e, BIGNUM *m, BN_CTX *ctx,\n\tint (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,\n\t\t\t const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx),\n\tBN_MONT_CTX *m_ctx)\n{\n\tint retry_counter = 32;\n\tBN_BLINDING *ret = NULL;\n\tif (b == NULL)\n\t\tret = BN_BLINDING_new(NULL, NULL, m);\n\telse\n\t\tret = b;\n\tif (ret == NULL)\n\t\tgoto err;\n\tif (ret->A == NULL && (ret->A = BN_new()) == NULL)\n\t\tgoto err;\n\tif (ret->Ai == NULL && (ret->Ai\t= BN_new()) == NULL)\n\t\tgoto err;\n\tif (e != NULL)\n\t\t{\n\t\tif (ret->e != NULL)\n\t\t\tBN_free(ret->e);\n\t\tret->e = BN_dup(e);\n\t\t}\n\tif (ret->e == NULL)\n\t\tgoto err;\n\tif (bn_mod_exp != NULL)\n\t\tret->bn_mod_exp = bn_mod_exp;\n\tif (m_ctx != NULL)\n\t\tret->m_ctx = m_ctx;\n\tdo {\n\t\tif (!BN_rand_range(ret->A, ret->mod)) goto err;\n\t\tif (BN_mod_inverse(ret->Ai, ret->A, ret->mod, ctx) == NULL)\n\t\t\t{\n\t\t\tunsigned long error = ERR_peek_last_error();\n\t\t\tif (ERR_GET_REASON(error) == BN_R_NO_INVERSE)\n\t\t\t\t{\n\t\t\t\tif (retry_counter-- == 0)\n\t\t\t\t{\n\t\t\t\t\tBNerr(BN_F_BN_BLINDING_CREATE_PARAM,\n\t\t\t\t\t\tBN_R_TOO_MANY_ITERATIONS);\n\t\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\t\tERR_clear_error();\n\t\t\t\t}\n\t\t\telse\n\t\t\t\tgoto err;\n\t\t\t}\n\t\telse\n\t\t\tbreak;\n\t} while (1);\n\tif (ret->bn_mod_exp != NULL && ret->m_ctx != NULL)\n\t\t{\n\t\tif (!ret->bn_mod_exp(ret->A, ret->A, ret->e, ret->mod, ctx, ret->m_ctx))\n\t\t\tgoto err;\n\t\t}\n\telse\n\t\t{\n\t\tif (!BN_mod_exp(ret->A, ret->A, ret->e, ret->mod, ctx))\n\t\t\tgoto err;\n\t\t}\n\treturn ret;\nerr:\n\tif (b == NULL && ret != NULL)\n\t\t{\n\t\tBN_BLINDING_free(ret);\n\t\tret = NULL;\n\t\t}\n\treturn ret;\n}', 'BIGNUM *BN_mod_inverse(BIGNUM *in,\n\tconst BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n\t{\n\tBIGNUM *A,*B,*X,*Y,*M,*D,*T,*R=NULL;\n\tBIGNUM *ret=NULL;\n\tint sign;\n\tbn_check_top(a);\n\tbn_check_top(n);\n\tBN_CTX_start(ctx);\n\tA = BN_CTX_get(ctx);\n\tB = BN_CTX_get(ctx);\n\tX = BN_CTX_get(ctx);\n\tD = BN_CTX_get(ctx);\n\tM = BN_CTX_get(ctx);\n\tY = BN_CTX_get(ctx);\n\tT = BN_CTX_get(ctx);\n\tif (T == NULL) goto err;\n\tif (in == NULL)\n\t\tR=BN_new();\n\telse\n\t\tR=in;\n\tif (R == NULL) goto err;\n\tBN_one(X);\n\tBN_zero(Y);\n\tif (BN_copy(B,a) == NULL) goto err;\n\tif (BN_copy(A,n) == NULL) goto err;\n\tA->neg = 0;\n\tif (B->neg || (BN_ucmp(B, A) >= 0))\n\t\t{\n\t\tif (!BN_nnmod(B, B, A, ctx)) goto err;\n\t\t}\n\tsign = -1;\n\tif (BN_is_odd(n) && (BN_num_bits(n) <= (BN_BITS <= 32 ? 450 : 2048)))\n\t\t{\n\t\tint shift;\n\t\twhile (!BN_is_zero(B))\n\t\t\t{\n\t\t\tshift = 0;\n\t\t\twhile (!BN_is_bit_set(B, shift))\n\t\t\t\t{\n\t\t\t\tshift++;\n\t\t\t\tif (BN_is_odd(X))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_uadd(X, X, n)) goto err;\n\t\t\t\t\t}\n\t\t\t\tif (!BN_rshift1(X, X)) goto err;\n\t\t\t\t}\n\t\t\tif (shift > 0)\n\t\t\t\t{\n\t\t\t\tif (!BN_rshift(B, B, shift)) goto err;\n\t\t\t\t}\n\t\t\tshift = 0;\n\t\t\twhile (!BN_is_bit_set(A, shift))\n\t\t\t\t{\n\t\t\t\tshift++;\n\t\t\t\tif (BN_is_odd(Y))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_uadd(Y, Y, n)) goto err;\n\t\t\t\t\t}\n\t\t\t\tif (!BN_rshift1(Y, Y)) goto err;\n\t\t\t\t}\n\t\t\tif (shift > 0)\n\t\t\t\t{\n\t\t\t\tif (!BN_rshift(A, A, shift)) goto err;\n\t\t\t\t}\n\t\t\tif (BN_ucmp(B, A) >= 0)\n\t\t\t\t{\n\t\t\t\tif (!BN_uadd(X, X, Y)) goto err;\n\t\t\t\tif (!BN_usub(B, B, A)) goto err;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (!BN_uadd(Y, Y, X)) goto err;\n\t\t\t\tif (!BN_usub(A, A, B)) goto err;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\twhile (!BN_is_zero(B))\n\t\t\t{\n\t\t\tBIGNUM *tmp;\n\t\t\tif (BN_num_bits(A) == BN_num_bits(B))\n\t\t\t\t{\n\t\t\t\tif (!BN_one(D)) goto err;\n\t\t\t\tif (!BN_sub(M,A,B)) goto err;\n\t\t\t\t}\n\t\t\telse if (BN_num_bits(A) == BN_num_bits(B) + 1)\n\t\t\t\t{\n\t\t\t\tif (!BN_lshift1(T,B)) goto err;\n\t\t\t\tif (BN_ucmp(A,T) < 0)\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_one(D)) goto err;\n\t\t\t\t\tif (!BN_sub(M,A,B)) goto err;\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_sub(M,A,T)) goto err;\n\t\t\t\t\tif (!BN_add(D,T,B)) goto err;\n\t\t\t\t\tif (BN_ucmp(A,D) < 0)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tif (!BN_set_word(D,2)) goto err;\n\t\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t\t{\n\t\t\t\t\t\tif (!BN_set_word(D,3)) goto err;\n\t\t\t\t\t\tif (!BN_sub(M,M,B)) goto err;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (!BN_div(D,M,A,B,ctx)) goto err;\n\t\t\t\t}\n\t\t\ttmp=A;\n\t\t\tA=B;\n\t\t\tB=M;\n\t\t\tif (BN_is_one(D))\n\t\t\t\t{\n\t\t\t\tif (!BN_add(tmp,X,Y)) goto err;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (BN_is_word(D,2))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_lshift1(tmp,X)) goto err;\n\t\t\t\t\t}\n\t\t\t\telse if (BN_is_word(D,4))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_lshift(tmp,X,2)) goto err;\n\t\t\t\t\t}\n\t\t\t\telse if (D->top == 1)\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_copy(tmp,X)) goto err;\n\t\t\t\t\tif (!BN_mul_word(tmp,D->d[0])) goto err;\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_mul(tmp,D,X,ctx)) goto err;\n\t\t\t\t\t}\n\t\t\t\tif (!BN_add(tmp,tmp,Y)) goto err;\n\t\t\t\t}\n\t\t\tM=Y;\n\t\t\tY=X;\n\t\t\tX=tmp;\n\t\t\tsign = -sign;\n\t\t\t}\n\t\t}\n\tif (sign < 0)\n\t\t{\n\t\tif (!BN_sub(Y,n,Y)) goto err;\n\t\t}\n\tif (BN_is_one(A))\n\t\t{\n\t\tif (!Y->neg && BN_ucmp(Y,n) < 0)\n\t\t\t{\n\t\t\tif (!BN_copy(R,Y)) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (!BN_nnmod(R,Y,n,ctx)) goto err;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_INVERSE,BN_R_NO_INVERSE);\n\t\tgoto err;\n\t\t}\n\tret=R;\nerr:\n\tif ((ret == NULL) && (in == NULL)) BN_free(R);\n\tBN_CTX_end(ctx);\n\tif (ret)\n\t\tbn_check_top(ret);\n\treturn(ret);\n\t}', 'void BN_CTX_start(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_start", ctx);\n\tif(ctx->err_stack || ctx->too_many)\n\t\tctx->err_stack++;\n\telse if(!BN_STACK_push(&ctx->stack, ctx->used))\n\t\t{\n\t\tBNerr(BN_F_BN_CTX_START,BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n\t\tctx->err_stack++;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n\t{\n\tif (!(BN_mod(r,m,d,ctx)))\n\t\treturn 0;\n\tif (!r->neg)\n\t\treturn 1;\n\treturn (d->neg ? BN_sub : BN_add)(r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n\t BN_CTX *ctx)\n\t{\n\tint norm_shift,i,loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tint num_n,div_n;\n\tif (dv)\n\t\tbn_check_top(dv);\n\tif (rm)\n\t\tbn_check_top(rm);\n\tbn_check_top(num);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\n\t\t}\n\tif (BN_ucmp(num,divisor) < 0)\n\t\t{\n\t\tif (rm != NULL)\n\t\t\t{ if (BN_copy(rm,num) == NULL) return(0); }\n\t\tif (dv != NULL) BN_zero(dv);\n\t\treturn(1);\n\t\t}\n\tBN_CTX_start(ctx);\n\ttmp=BN_CTX_get(ctx);\n\tsnum=BN_CTX_get(ctx);\n\tsdiv=BN_CTX_get(ctx);\n\tif (dv == NULL)\n\t\tres=BN_CTX_get(ctx);\n\telse\tres=dv;\n\tif (sdiv == NULL || res == NULL) goto err;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tif (!(BN_lshift(sdiv,divisor,norm_shift))) goto err;\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tif (!(BN_lshift(snum,num,norm_shift))) goto err;\n\tsnum->neg=0;\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\twnum.neg = 0;\n\twnum.d = &(snum->d[loop]);\n\twnum.top = div_n;\n\twnum.dmax = snum->dmax - loop;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (BN_ucmp(&wnum,sdiv) >= 0)\n\t\t{\n\t\tbn_clear_top2max(&wnum);\n\t\tbn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n\t\t*resp=1;\n\t\t}\n\telse\n\t\tres->top--;\n\tif (res->top == 0)\n\t\tres->neg = 0;\n\telse\n\t\tresp--;\n\tfor (i=0; i<loop-1; i++, wnump--, resp--)\n\t\t{\n\t\tBN_ULONG q,l0;\n#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n\t\tBN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG);\n\t\tq=bn_div_3_words(wnump,d1,d0);\n#else\n\t\tBN_ULONG n0,n1,rem=0;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\t{\n#ifdef BN_LLONG\n\t\t\tBN_ULLONG t2;\n#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n\t\t\tq=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);\n#else\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tt2 -= d1;\n\t\t\t\t}\n#else\n\t\t\tBN_ULONG t2l,t2h,ql,qh;\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#if defined(BN_UMULT_LOHI)\n\t\t\tBN_UMULT_LOHI(t2l,t2h,d1,q);\n#elif defined(BN_UMULT_HIGH)\n\t\t\tt2l = d1 * q;\n\t\t\tt2h = BN_UMULT_HIGH(d1,q);\n#else\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n#endif\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif ((t2h < rem) ||\n\t\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t\t}\n#endif\n\t\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\twnum.d--;\n\t\tif (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1))\n\t\t\t{\n\t\t\tq--;\n\t\t\tif (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n\t\t\t\t(*wnump)++;\n\t\t\t}\n\t\t*resp = q;\n\t\t}\n\tbn_correct_top(snum);\n\tif (rm != NULL)\n\t\t{\n\t\tint neg = num->neg;\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\tif (!BN_is_zero(rm))\n\t\t\trm->neg = neg;\n\t\tbn_check_top(rm);\n\t\t}\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tif (rm)\n\t\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'void BN_CTX_end(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_end", ctx);\n\tif(ctx->err_stack)\n\t\tctx->err_stack--;\n\telse\n\t\t{\n\t\tunsigned int fp = BN_STACK_pop(&ctx->stack);\n\t\tif(fp < ctx->used)\n\t\t\tBN_POOL_release(&ctx->pool, ctx->used - fp);\n\t\tctx->used = fp;\n\t\tctx->too_many = 0;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n\t{\n\treturn st->indexes[--(st->depth)];\n\t}']
|
2,653
| 0
|
https://github.com/openssl/openssl/blob/e02c519cd32a55e6ad39a0cfbeeda775f9115f28/crypto/bn/bn_lib.c/#L232
|
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
BN_ULONG *a = NULL;
if (words > (INT_MAX / (4 * BN_BITS2))) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
return NULL;
}
if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
return NULL;
}
if (BN_get_flags(b, BN_FLG_SECURE))
a = OPENSSL_secure_zalloc(words * sizeof(*a));
else
a = OPENSSL_zalloc(words * sizeof(*a));
if (a == NULL) {
BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
return NULL;
}
assert(b->top <= words);
if (b->top > 0)
memcpy(a, b->d, sizeof(*a) * b->top);
return a;
}
|
['int bn_sqr_fixed_top(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int max, al;\n int ret = 0;\n BIGNUM *tmp, *rr;\n bn_check_top(a);\n al = a->top;\n if (al <= 0) {\n r->top = 0;\n r->neg = 0;\n return 1;\n }\n BN_CTX_start(ctx);\n rr = (a != r) ? r : BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (rr == NULL || tmp == NULL)\n goto err;\n max = 2 * al;\n if (bn_wexpand(rr, max) == NULL)\n goto err;\n if (al == 4) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[8];\n bn_sqr_normal(rr->d, a->d, 4, t);\n#else\n bn_sqr_comba4(rr->d, a->d);\n#endif\n } else if (al == 8) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[16];\n bn_sqr_normal(rr->d, a->d, 8, t);\n#else\n bn_sqr_comba8(rr->d, a->d);\n#endif\n } else {\n#if defined(BN_RECURSION)\n if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {\n BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];\n bn_sqr_normal(rr->d, a->d, al, t);\n } else {\n int j, k;\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n if (al == j) {\n if (bn_wexpand(tmp, k * 2) == NULL)\n goto err;\n bn_sqr_recursive(rr->d, a->d, al, tmp->d);\n } else {\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n }\n }\n#else\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n#endif\n }\n rr->neg = 0;\n rr->top = max;\n rr->flags |= BN_FLG_FIXED_TOP;\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(rr);\n bn_check_top(tmp);\n BN_CTX_end(ctx);\n return ret;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n INCREMENT(malloc_count);\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n if (allow_customize) {\n allow_customize = 0;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)(file); (void)(line);\n ret = malloc(num);\n#endif\n return ret;\n}']
|
2,654
| 0
|
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/x509/x509_lu.c/#L386
|
int X509_STORE_add_crl(X509_STORE *ctx, X509_CRL *x)
{
X509_OBJECT *obj;
int ret = 1;
if (x == NULL)
return 0;
obj = OPENSSL_malloc(sizeof(*obj));
if (obj == NULL) {
X509err(X509_F_X509_STORE_ADD_CRL, ERR_R_MALLOC_FAILURE);
return 0;
}
obj->type = X509_LU_CRL;
obj->data.crl = x;
CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE);
X509_OBJECT_up_ref_count(obj);
if (X509_OBJECT_retrieve_match(ctx->objs, obj)) {
X509_OBJECT_free_contents(obj);
OPENSSL_free(obj);
X509err(X509_F_X509_STORE_ADD_CRL, X509_R_CERT_ALREADY_IN_HASH_TABLE);
ret = 0;
} else
sk_X509_OBJECT_push(ctx->objs, obj);
CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);
return ret;
}
|
['int X509_STORE_add_crl(X509_STORE *ctx, X509_CRL *x)\n{\n X509_OBJECT *obj;\n int ret = 1;\n if (x == NULL)\n return 0;\n obj = OPENSSL_malloc(sizeof(*obj));\n if (obj == NULL) {\n X509err(X509_F_X509_STORE_ADD_CRL, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n obj->type = X509_LU_CRL;\n obj->data.crl = x;\n CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE);\n X509_OBJECT_up_ref_count(obj);\n if (X509_OBJECT_retrieve_match(ctx->objs, obj)) {\n X509_OBJECT_free_contents(obj);\n OPENSSL_free(obj);\n X509err(X509_F_X509_STORE_ADD_CRL, X509_R_CERT_ALREADY_IN_HASH_TABLE);\n ret = 0;\n } else\n sk_X509_OBJECT_push(ctx->objs, obj);\n CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);\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 CRYPTO_lock(int mode, int type, const char *file, int line)\n{\n#ifdef LOCK_DEBUG\n {\n CRYPTO_THREADID id;\n char *rw_text, *operation_text;\n if (mode & CRYPTO_LOCK)\n operation_text = "lock ";\n else if (mode & CRYPTO_UNLOCK)\n operation_text = "unlock";\n else\n operation_text = "ERROR ";\n if (mode & CRYPTO_READ)\n rw_text = "r";\n else if (mode & CRYPTO_WRITE)\n rw_text = "w";\n else\n rw_text = "ERROR";\n CRYPTO_THREADID_current(&id);\n fprintf(stderr, "lock:%08lx:(%s)%s %-18s %s:%d\\n",\n CRYPTO_THREADID_hash(&id), rw_text, operation_text,\n CRYPTO_get_lock_name(type), file, line);\n }\n#endif\n if (type < 0) {\n if (dynlock_lock_callback != NULL) {\n struct CRYPTO_dynlock_value *pointer\n = CRYPTO_get_dynlock_value(type);\n OPENSSL_assert(pointer != NULL);\n dynlock_lock_callback(mode, pointer, file, line);\n CRYPTO_destroy_dynlockid(type);\n }\n } else if (locking_callback != NULL)\n locking_callback(mode, type, file, line);\n}', 'void X509_OBJECT_up_ref_count(X509_OBJECT *a)\n{\n switch (a->type) {\n default:\n break;\n case X509_LU_X509:\n X509_up_ref(a->data.x509);\n break;\n case X509_LU_CRL:\n X509_CRL_up_ref(a->data.crl);\n break;\n }\n}', 'void X509_CRL_up_ref(X509_CRL *crl)\n{\n CRYPTO_add(&crl->references, 1, CRYPTO_LOCK_X509_CRL);\n}', 'int CRYPTO_add_lock(int *pointer, int amount, int type, const char *file,\n int line)\n{\n int ret = 0;\n if (add_lock_callback != NULL) {\n#ifdef LOCK_DEBUG\n int before = *pointer;\n#endif\n ret = add_lock_callback(pointer, amount, type, file, line);\n#ifdef LOCK_DEBUG\n {\n CRYPTO_THREADID id;\n CRYPTO_THREADID_current(&id);\n fprintf(stderr, "ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\\n",\n CRYPTO_THREADID_hash(&id), before, amount, ret,\n CRYPTO_get_lock_name(type), file, line);\n }\n#endif\n } else {\n CRYPTO_lock(CRYPTO_LOCK | CRYPTO_WRITE, type, file, line);\n ret = *pointer + amount;\n#ifdef LOCK_DEBUG\n {\n CRYPTO_THREADID id;\n CRYPTO_THREADID_current(&id);\n fprintf(stderr, "ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\\n",\n CRYPTO_THREADID_hash(&id),\n *pointer, amount, ret,\n CRYPTO_get_lock_name(type), file, line);\n }\n#endif\n *pointer = ret;\n CRYPTO_lock(CRYPTO_UNLOCK | CRYPTO_WRITE, type, file, line);\n }\n return (ret);\n}', 'int sk_push(_STACK *st, void *data)\n{\n return (sk_insert(st, data, st->num));\n}', 'int sk_insert(_STACK *st, void *data, int loc)\n{\n char **s;\n if (st == NULL)\n return 0;\n if (st->num_alloc <= st->num + 1) {\n s = OPENSSL_realloc((char *)st->data,\n (unsigned int)sizeof(char *) * st->num_alloc * 2);\n if (s == NULL)\n return (0);\n st->data = s;\n st->num_alloc *= 2;\n }\n if ((loc >= (int)st->num) || (loc < 0))\n st->data[st->num] = data;\n else {\n memmove(&(st->data[loc + 1]),\n &(st->data[loc]), sizeof(char *) * (st->num - loc));\n st->data[loc] = data;\n }\n st->num++;\n st->sorted = 0;\n return (st->num);\n}', 'void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)\n{\n if (str == NULL)\n return CRYPTO_malloc(num, file, line);\n if (num == 0) {\n CRYPTO_free(str);\n return NULL;\n }\n allow_customize = 0;\n#ifdef CRYPTO_MDEBUG\n if (call_malloc_debug) {\n void *ret;\n CRYPTO_mem_debug_realloc(str, NULL, num, 0, file, line);\n ret = realloc(str, num);\n CRYPTO_mem_debug_realloc(str, ret, num, 1, file, line);\n return ret;\n }\n#else\n (void)file;\n (void)line;\n#endif\n return realloc(str, num);\n}']
|
2,655
| 0
|
https://github.com/openssl/openssl/blob/8b0d4242404f9e5da26e7594fa0864b2df4601af/crypto/bn/bn_lib.c/#L271
|
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
BN_ULONG *a = NULL;
bn_check_top(b);
if (words > (INT_MAX / (4 * BN_BITS2))) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
return NULL;
}
if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
return (NULL);
}
if (BN_get_flags(b, BN_FLG_SECURE))
a = OPENSSL_secure_zalloc(words * sizeof(*a));
else
a = OPENSSL_zalloc(words * sizeof(*a));
if (a == NULL) {
BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
return (NULL);
}
assert(b->top <= words);
if (b->top > 0)
memcpy(a, b->d, sizeof(*a) * b->top);
return a;
}
|
['int BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)\n{\n int i, bits, ret = 0;\n BIGNUM *v, *rr;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0) {\n BNerr(BN_F_BN_EXP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n BN_CTX_start(ctx);\n if ((r == a) || (r == p))\n rr = BN_CTX_get(ctx);\n else\n rr = r;\n v = BN_CTX_get(ctx);\n if (rr == NULL || v == NULL)\n goto err;\n if (BN_copy(v, a) == NULL)\n goto err;\n bits = BN_num_bits(p);\n if (BN_is_odd(p)) {\n if (BN_copy(rr, a) == NULL)\n goto err;\n } else {\n if (!BN_one(rr))\n goto err;\n }\n for (i = 1; i < bits; i++) {\n if (!BN_sqr(v, v, ctx))\n goto err;\n if (BN_is_bit_set(p, i)) {\n if (!BN_mul(rr, rr, v, ctx))\n goto err;\n }\n }\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n bn_check_top(r);\n return (ret);\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return (1);\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n bn_check_top(b);\n if (a == b)\n return a;\n if (bn_wexpand(a, b->top) == NULL)\n return NULL;\n if (b->top > 0)\n memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n a->top = b->top;\n a->neg = b->neg;\n bn_check_top(a);\n return a;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', '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}']
|
2,656
| 0
|
https://github.com/libav/libav/blob/6961bdface651ac9bd3621738aeb3b65969033b6/libavfilter/graphparser.c/#L221
|
static void append_inout(AVFilterInOut **inouts, AVFilterInOut **element)
{
while (*inouts && (*inouts)->next)
inouts = &((*inouts)->next);
if (!*inouts)
*inouts = *element;
else
(*inouts)->next = *element;
*element = NULL;
}
|
['static void append_inout(AVFilterInOut **inouts, AVFilterInOut **element)\n{\n while (*inouts && (*inouts)->next)\n inouts = &((*inouts)->next);\n if (!*inouts)\n *inouts = *element;\n else\n (*inouts)->next = *element;\n *element = NULL;\n}']
|
2,657
| 0
|
https://github.com/openssl/openssl/blob/67dc995eaf538ea309c6292a1a5073465201f55b/include/internal/constant_time_locl.h/#L121
|
static ossl_inline size_t constant_time_lt_s(size_t a, size_t b)
{
return constant_time_msb_s(a ^ ((a ^ b) | ((a - b) ^ b)));
}
|
['int dtls1_process_record(SSL *s, DTLS1_BITMAP *bitmap)\n{\n int i, al;\n int enc_err;\n SSL_SESSION *sess;\n SSL3_RECORD *rr;\n int imac_size;\n size_t mac_size;\n unsigned char md[EVP_MAX_MD_SIZE];\n rr = RECORD_LAYER_get_rrec(&s->rlayer);\n sess = s->session;\n rr->input = &(RECORD_LAYER_get_packet(&s->rlayer)[DTLS1_RT_HEADER_LENGTH]);\n if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {\n al = SSL_AD_RECORD_OVERFLOW;\n SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);\n goto f_err;\n }\n rr->data = rr->input;\n rr->orig_len = rr->length;\n if (SSL_READ_ETM(s) && s->read_hash) {\n unsigned char *mac;\n mac_size = EVP_MD_CTX_size(s->read_hash);\n if (!ossl_assert(mac_size <= EVP_MAX_MD_SIZE)) {\n al = SSL_AD_INTERNAL_ERROR;\n SSLerr(SSL_F_DTLS1_PROCESS_RECORD, ERR_R_INTERNAL_ERROR);\n goto f_err;\n }\n if (rr->orig_len < mac_size) {\n al = SSL_AD_DECODE_ERROR;\n SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_LENGTH_TOO_SHORT);\n goto f_err;\n }\n rr->length -= mac_size;\n mac = rr->data + rr->length;\n i = s->method->ssl3_enc->mac(s, rr, md, 0 );\n if (i == 0 || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0) {\n al = SSL_AD_BAD_RECORD_MAC;\n SSLerr(SSL_F_DTLS1_PROCESS_RECORD,\n SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);\n goto f_err;\n }\n }\n enc_err = s->method->ssl3_enc->enc(s, rr, 1, 0);\n if (enc_err == 0) {\n rr->length = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto err;\n }\n#ifdef SSL_DEBUG\n printf("dec %ld\\n", rr->length);\n {\n size_t z;\n for (z = 0; z < rr->length; z++)\n printf("%02X%c", rr->data[z], ((z + 1) % 16) ? \' \' : \'\\n\');\n }\n printf("\\n");\n#endif\n if ((sess != NULL) && !SSL_READ_ETM(s) &&\n (s->enc_read_ctx != NULL) && (EVP_MD_CTX_md(s->read_hash) != NULL)) {\n unsigned char *mac = NULL;\n unsigned char mac_tmp[EVP_MAX_MD_SIZE];\n imac_size = EVP_MD_CTX_size(s->read_hash);\n if (imac_size < 0) {\n al = SSL_AD_INTERNAL_ERROR;\n SSLerr(SSL_F_DTLS1_PROCESS_RECORD, ERR_LIB_EVP);\n goto f_err;\n }\n mac_size = (size_t)imac_size;\n if (!ossl_assert(mac_size <= EVP_MAX_MD_SIZE)) {\n al = SSL_AD_INTERNAL_ERROR;\n SSLerr(SSL_F_DTLS1_PROCESS_RECORD, ERR_R_INTERNAL_ERROR);\n goto f_err;\n }\n if (rr->orig_len < mac_size ||\n (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE &&\n rr->orig_len < mac_size + 1)) {\n al = SSL_AD_DECODE_ERROR;\n SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_LENGTH_TOO_SHORT);\n goto f_err;\n }\n if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) {\n mac = mac_tmp;\n if (!ssl3_cbc_copy_mac(mac_tmp, rr, mac_size)) {\n al = SSL_AD_INTERNAL_ERROR;\n SSLerr(SSL_F_DTLS1_PROCESS_RECORD, ERR_R_INTERNAL_ERROR);\n goto f_err;\n }\n rr->length -= mac_size;\n } else {\n rr->length -= mac_size;\n mac = &rr->data[rr->length];\n }\n i = s->method->ssl3_enc->mac(s, rr, md, 0 );\n if (i == 0 || mac == NULL\n || CRYPTO_memcmp(md, mac, mac_size) != 0)\n enc_err = -1;\n if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)\n enc_err = -1;\n }\n if (enc_err < 0) {\n rr->length = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto err;\n }\n if (s->expand != NULL) {\n if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {\n al = SSL_AD_RECORD_OVERFLOW;\n SSLerr(SSL_F_DTLS1_PROCESS_RECORD,\n SSL_R_COMPRESSED_LENGTH_TOO_LONG);\n goto f_err;\n }\n if (!ssl3_do_uncompress(s, rr)) {\n al = SSL_AD_DECOMPRESSION_FAILURE;\n SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_BAD_DECOMPRESSION);\n goto f_err;\n }\n }\n if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH) {\n al = SSL_AD_RECORD_OVERFLOW;\n SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_DATA_LENGTH_TOO_LONG);\n goto f_err;\n }\n rr->off = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n dtls1_record_bitmap_update(s, bitmap);\n return (1);\n f_err:\n ssl3_send_alert(s, SSL3_AL_FATAL, al);\n err:\n return (0);\n}', 'int ssl3_cbc_copy_mac(unsigned char *out,\n const SSL3_RECORD *rec, size_t md_size)\n{\n#if defined(CBC_MAC_ROTATE_IN_PLACE)\n unsigned char rotated_mac_buf[64 + EVP_MAX_MD_SIZE];\n unsigned char *rotated_mac;\n#else\n unsigned char rotated_mac[EVP_MAX_MD_SIZE];\n#endif\n size_t mac_end = rec->length;\n size_t mac_start = mac_end - md_size;\n size_t in_mac;\n size_t scan_start = 0;\n size_t i, j;\n size_t rotate_offset;\n if (!ossl_assert(rec->orig_len >= md_size\n && md_size <= EVP_MAX_MD_SIZE))\n return 0;\n#if defined(CBC_MAC_ROTATE_IN_PLACE)\n rotated_mac = rotated_mac_buf + ((0 - (size_t)rotated_mac_buf) & 63);\n#endif\n if (rec->orig_len > md_size + 255 + 1)\n scan_start = rec->orig_len - (md_size + 255 + 1);\n in_mac = 0;\n rotate_offset = 0;\n memset(rotated_mac, 0, md_size);\n for (i = scan_start, j = 0; i < rec->orig_len; i++) {\n size_t mac_started = constant_time_eq_s(i, mac_start);\n size_t mac_ended = constant_time_lt_s(i, mac_end);\n unsigned char b = rec->data[i];\n in_mac |= mac_started;\n in_mac &= mac_ended;\n rotate_offset |= j & mac_started;\n rotated_mac[j++] |= b & in_mac;\n j &= constant_time_lt_s(j, md_size);\n }\n#if defined(CBC_MAC_ROTATE_IN_PLACE)\n j = 0;\n for (i = 0; i < md_size; i++) {\n ((volatile unsigned char *)rotated_mac)[rotate_offset ^ 32];\n out[j++] = rotated_mac[rotate_offset++];\n rotate_offset &= constant_time_lt_s(rotate_offset, md_size);\n }\n#else\n memset(out, 0, md_size);\n rotate_offset = md_size - rotate_offset;\n rotate_offset &= constant_time_lt_s(rotate_offset, md_size);\n for (i = 0; i < md_size; i++) {\n for (j = 0; j < md_size; j++)\n out[j] |= rotated_mac[i] & constant_time_eq_8_s(j, rotate_offset);\n rotate_offset++;\n rotate_offset &= constant_time_lt_s(rotate_offset, md_size);\n }\n#endif\n return 1;\n}', 'static ossl_inline size_t constant_time_lt_s(size_t a, size_t b)\n{\n return constant_time_msb_s(a ^ ((a ^ b) | ((a - b) ^ b)));\n}']
|
2,658
| 0
|
https://github.com/openssl/openssl/blob/5dfc369ffcdc4722482c818e6ba6cf6e704c2cb5/crypto/bn/bn_mul.c/#L648
|
int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)
{
int top,al,bl;
BIGNUM *rr;
#ifdef BN_RECURSION
BIGNUM *t;
int i,j,k;
#endif
#ifdef BN_COUNT
printf("BN_mul %d * %d\n",a->top,b->top);
#endif
bn_check_top(a);
bn_check_top(b);
bn_check_top(r);
al=a->top;
bl=b->top;
r->neg=a->neg^b->neg;
if ((al == 0) || (bl == 0))
{
BN_zero(r);
return(1);
}
top=al+bl;
if ((r == a) || (r == b))
rr= &(ctx->bn[ctx->tos+1]);
else
rr=r;
#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
if (al == bl)
{
# ifdef BN_MUL_COMBA
if (al == 8)
{
if (bn_wexpand(rr,16) == NULL) return(0);
r->top=16;
bn_mul_comba8(rr->d,a->d,b->d);
goto end;
}
else
# endif
#ifdef BN_RECURSION
if (al < BN_MULL_SIZE_NORMAL)
#endif
{
if (bn_wexpand(rr,top) == NULL) return(0);
rr->top=top;
bn_mul_normal(rr->d,a->d,al,b->d,bl);
goto end;
}
# ifdef BN_RECURSION
goto symetric;
# endif
}
#endif
#ifdef BN_RECURSION
else if ((al < BN_MULL_SIZE_NORMAL) || (bl < BN_MULL_SIZE_NORMAL))
{
if (bn_wexpand(rr,top) == NULL) return(0);
rr->top=top;
bn_mul_normal(rr->d,a->d,al,b->d,bl);
goto end;
}
else
{
i=(al-bl);
if ((i == 1) && !BN_get_flags(b,BN_FLG_STATIC_DATA))
{
bn_wexpand(b,al);
b->d[bl]=0;
bl++;
goto symetric;
}
else if ((i == -1) && !BN_get_flags(a,BN_FLG_STATIC_DATA))
{
bn_wexpand(a,bl);
a->d[al]=0;
al++;
goto symetric;
}
}
#endif
if (bn_wexpand(rr,top) == NULL) return(0);
rr->top=top;
bn_mul_normal(rr->d,a->d,al,b->d,bl);
#ifdef BN_RECURSION
if (0)
{
symetric:
j=BN_num_bits_word((BN_ULONG)al);
j=1<<(j-1);
k=j+j;
t= &(ctx->bn[ctx->tos]);
if (al == j)
{
bn_wexpand(t,k*2);
bn_wexpand(rr,k*2);
bn_mul_recursive(rr->d,a->d,b->d,al,t->d);
}
else
{
bn_wexpand(a,k);
bn_wexpand(b,k);
bn_wexpand(t,k*4);
bn_wexpand(rr,k*4);
for (i=a->top; i<k; i++)
a->d[i]=0;
for (i=b->top; i<k; i++)
b->d[i]=0;
bn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d);
}
rr->top=top;
}
#endif
#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
end:
#endif
bn_fix_top(rr);
if (r != rr) BN_copy(r,rr);
return(1);
}
|
['int test_mod_exp(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,30,0,1);\n\tfor (i=0; i<6; i++)\n\t\t{\n\t\tBN_rand(a,20+i*5,0,0);\n\t\tBN_rand(b,2+i,0,0);\n\t\tif (!BN_mod_exp(d,a,b,c,ctx))\n\t\t\treturn(00);\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,d);\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}', 'int BN_mod_exp(BIGNUM *r, BIGNUM *a, BIGNUM *p, BIGNUM *m, BN_CTX *ctx)\n\t{\n\tint ret;\n\tbn_check_top(a);\n\tbn_check_top(p);\n\tbn_check_top(m);\n#ifdef MONT_MUL_MOD\n\tif (BN_is_odd(m))\n\t\t{ ret=BN_mod_exp_mont(r,a,p,m,ctx,NULL); }\n\telse\n#endif\n#ifdef RECP_MUL_MOD\n\t\t{ ret=BN_mod_exp_recp(r,a,p,m,ctx); }\n#else\n\t\t{ ret=BN_mod_exp_simple(r,a,p,m,ctx); }\n#endif\n\treturn(ret);\n\t}', 'int BN_mod_exp_mont(BIGNUM *rr, BIGNUM *a, BIGNUM *p, BIGNUM *m, BN_CTX *ctx,\n\t 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,*aa,*r;\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, BN_MONT_CTX *mont,\n\t BN_CTX *ctx)\n\t{\n\tBIGNUM *tmp,*tmp2;\n tmp= &(ctx->bn[ctx->tos]);\n tmp2= &(ctx->bn[ctx->tos]);\n\tctx->tos+=2;\n\tbn_check_top(tmp);\n\tbn_check_top(tmp2);\n\tif (a == b)\n\t\t{\n#if 0\n\t\tbn_wexpand(tmp,a->top*2);\n\t\tbn_wexpand(tmp2,a->top*4);\n\t\tbn_sqr_recursive(tmp->d,a->d,a->top,tmp2->d);\n\t\ttmp->top=a->top*2;\n\t\tif (tmp->d[tmp->top-1] == 0)\n\t\t\ttmp->top--;\n#else\n\t\tif (!BN_sqr(tmp,a,ctx)) goto err;\n#endif\n\t\t}\n\telse\n\t\t{\n\t\tif (!BN_mul(tmp,a,b,ctx)) goto err;\n\t\t}\n\tif (!BN_from_montgomery(r,tmp,mont,ctx)) goto err;\n\tctx->tos-=2;\n\treturn(1);\nerr:\n\treturn(0);\n\t}', 'int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)\n\t{\n\tint top,al,bl;\n\tBIGNUM *rr;\n#ifdef BN_RECURSION\n\tBIGNUM *t;\n\tint i,j,k;\n#endif\n#ifdef BN_COUNT\nprintf("BN_mul %d * %d\\n",a->top,b->top);\n#endif\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tbn_check_top(r);\n\tal=a->top;\n\tbl=b->top;\n\tr->neg=a->neg^b->neg;\n\tif ((al == 0) || (bl == 0))\n\t\t{\n\t\tBN_zero(r);\n\t\treturn(1);\n\t\t}\n\ttop=al+bl;\n\tif ((r == a) || (r == b))\n\t\trr= &(ctx->bn[ctx->tos+1]);\n\telse\n\t\trr=r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\tif (al == bl)\n\t\t{\n# ifdef BN_MUL_COMBA\n if (al == 8)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,16) == NULL) return(0);\n\t\t\tr->top=16;\n\t\t\tbn_mul_comba8(rr->d,a->d,b->d);\n\t\t\tgoto end;\n\t\t\t}\n\t\telse\n# endif\n#ifdef BN_RECURSION\n\t\tif (al < BN_MULL_SIZE_NORMAL)\n#endif\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,top) == NULL) return(0);\n\t\t\trr->top=top;\n\t\t\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n\t\t\tgoto end;\n\t\t\t}\n# ifdef BN_RECURSION\n\t\tgoto symetric;\n# endif\n\t\t}\n#endif\n#ifdef BN_RECURSION\n\telse if ((al < BN_MULL_SIZE_NORMAL) || (bl < BN_MULL_SIZE_NORMAL))\n\t\t{\n\t\tif (bn_wexpand(rr,top) == NULL) return(0);\n\t\trr->top=top;\n\t\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n\t\tgoto end;\n\t\t}\n\telse\n\t\t{\n\t\ti=(al-bl);\n\t\tif ((i == 1) && !BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tbn_wexpand(b,al);\n\t\t\tb->d[bl]=0;\n\t\t\tbl++;\n\t\t\tgoto symetric;\n\t\t\t}\n\t\telse if ((i == -1) && !BN_get_flags(a,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tbn_wexpand(a,bl);\n\t\t\ta->d[al]=0;\n\t\t\tal++;\n\t\t\tgoto symetric;\n\t\t\t}\n\t\t}\n#endif\n\tif (bn_wexpand(rr,top) == NULL) return(0);\n\trr->top=top;\n\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n#ifdef BN_RECURSION\n\tif (0)\n\t\t{\nsymetric:\n\t\tj=BN_num_bits_word((BN_ULONG)al);\n\t\tj=1<<(j-1);\n\t\tk=j+j;\n\t\tt= &(ctx->bn[ctx->tos]);\n\t\tif (al == j)\n\t\t\t{\n\t\t\tbn_wexpand(t,k*2);\n\t\t\tbn_wexpand(rr,k*2);\n\t\t\tbn_mul_recursive(rr->d,a->d,b->d,al,t->d);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tbn_wexpand(a,k);\n\t\t\tbn_wexpand(b,k);\n\t\t\tbn_wexpand(t,k*4);\n\t\t\tbn_wexpand(rr,k*4);\n\t\t\tfor (i=a->top; i<k; i++)\n\t\t\t\ta->d[i]=0;\n\t\t\tfor (i=b->top; i<k; i++)\n\t\t\t\tb->d[i]=0;\n\t\t\tbn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d);\n\t\t\t}\n\t\trr->top=top;\n\t\t}\n#endif\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\nend:\n#endif\n\tbn_fix_top(rr);\n\tif (r != rr) BN_copy(r,rr);\n\treturn(1);\n\t}']
|
2,659
| 0
|
https://github.com/libav/libav/blob/1d0feb5d1ac04d187b335f0e8d411c9f40b3a885/libavcodec/mpegvideo.c/#L189
|
const uint8_t *avpriv_mpv_find_start_code(const uint8_t *restrict p,
const uint8_t *end,
uint32_t * restrict state)
{
int i;
assert(p <= end);
if (p >= end)
return end;
for (i = 0; i < 3; i++) {
uint32_t tmp = *state << 8;
*state = tmp + *(p++);
if (tmp == 0x100 || p == end)
return p;
}
while (p < end) {
if (p[-1] > 1 ) p += 3;
else if (p[-2] ) p += 2;
else if (p[-3]|(p[-1]-1)) p++;
else {
p++;
break;
}
}
p = FFMIN(p, end) - 4;
*state = AV_RB32(p);
return p + 4;
}
|
['static int decode_chunks(AVCodecContext *avctx,\n AVFrame *picture, int *got_output,\n const uint8_t *buf, int buf_size)\n{\n Mpeg1Context *s = avctx->priv_data;\n MpegEncContext *s2 = &s->mpeg_enc_ctx;\n const uint8_t *buf_ptr = buf;\n const uint8_t *buf_end = buf + buf_size;\n int ret, input_size;\n int last_code = 0;\n for (;;) {\n uint32_t start_code = -1;\n buf_ptr = avpriv_mpv_find_start_code(buf_ptr, buf_end, &start_code);\n if (start_code > 0x1ff) {\n if (s2->pict_type != AV_PICTURE_TYPE_B || avctx->skip_frame <= AVDISCARD_DEFAULT) {\n if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)) {\n int i;\n avctx->execute(avctx, slice_decode_thread, &s2->thread_context[0], NULL, s->slice_count, sizeof(void*));\n for (i = 0; i < s->slice_count; i++)\n s2->er.error_count += s2->thread_context[i]->er.error_count;\n }\n if (CONFIG_MPEG_VDPAU_DECODER && avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)\n ff_vdpau_mpeg_picture_complete(s2, buf, buf_size, s->slice_count);\n if (slice_end(avctx, picture)) {\n if (s2->last_picture_ptr || s2->low_delay)\n *got_output = 1;\n }\n }\n s2->pict_type = 0;\n return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);\n }\n input_size = buf_end - buf_ptr;\n if (avctx->debug & FF_DEBUG_STARTCODE) {\n av_log(avctx, AV_LOG_DEBUG, "%3X at %td left %d\\n", start_code, buf_ptr-buf, input_size);\n }\n switch (start_code) {\n case SEQ_START_CODE:\n if (last_code == 0) {\n mpeg1_decode_sequence(avctx, buf_ptr, input_size);\n s->sync=1;\n } else {\n av_log(avctx, AV_LOG_ERROR, "ignoring SEQ_START_CODE after %X\\n", last_code);\n if (avctx->err_recognition & AV_EF_EXPLODE)\n return AVERROR_INVALIDDATA;\n }\n break;\n case PICTURE_START_CODE:\n if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) && s->slice_count) {\n int i;\n avctx->execute(avctx, slice_decode_thread,\n s2->thread_context, NULL,\n s->slice_count, sizeof(void*));\n for (i = 0; i < s->slice_count; i++)\n s2->er.error_count += s2->thread_context[i]->er.error_count;\n s->slice_count = 0;\n }\n if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {\n ret = mpeg_decode_postinit(avctx);\n if (ret < 0) {\n av_log(avctx, AV_LOG_ERROR, "mpeg_decode_postinit() failure\\n");\n return ret;\n }\n if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)\n s2->pict_type = 0;\n s2->first_slice = 1;\n last_code = PICTURE_START_CODE;\n } else {\n av_log(avctx, AV_LOG_ERROR, "ignoring pic after %X\\n", last_code);\n if (avctx->err_recognition & AV_EF_EXPLODE)\n return AVERROR_INVALIDDATA;\n }\n break;\n case EXT_START_CODE:\n init_get_bits(&s2->gb, buf_ptr, input_size*8);\n switch (get_bits(&s2->gb, 4)) {\n case 0x1:\n if (last_code == 0) {\n mpeg_decode_sequence_extension(s);\n } else {\n av_log(avctx, AV_LOG_ERROR, "ignoring seq ext after %X\\n", last_code);\n if (avctx->err_recognition & AV_EF_EXPLODE)\n return AVERROR_INVALIDDATA;\n }\n break;\n case 0x2:\n mpeg_decode_sequence_display_extension(s);\n break;\n case 0x3:\n mpeg_decode_quant_matrix_extension(s2);\n break;\n case 0x7:\n mpeg_decode_picture_display_extension(s);\n break;\n case 0x8:\n if (last_code == PICTURE_START_CODE) {\n mpeg_decode_picture_coding_extension(s);\n } else {\n av_log(avctx, AV_LOG_ERROR, "ignoring pic cod ext after %X\\n", last_code);\n if (avctx->err_recognition & AV_EF_EXPLODE)\n return AVERROR_INVALIDDATA;\n }\n break;\n }\n break;\n case USER_START_CODE:\n mpeg_decode_user_data(avctx, buf_ptr, input_size);\n break;\n case GOP_START_CODE:\n if (last_code == 0) {\n s2->first_field=0;\n mpeg_decode_gop(avctx, buf_ptr, input_size);\n s->sync=1;\n } else {\n av_log(avctx, AV_LOG_ERROR, "ignoring GOP_START_CODE after %X\\n", last_code);\n if (avctx->err_recognition & AV_EF_EXPLODE)\n return AVERROR_INVALIDDATA;\n }\n break;\n default:\n if (start_code >= SLICE_MIN_START_CODE &&\n start_code <= SLICE_MAX_START_CODE && last_code != 0) {\n const int field_pic = s2->picture_structure != PICT_FRAME;\n int mb_y = (start_code - SLICE_MIN_START_CODE) << field_pic;\n last_code = SLICE_MIN_START_CODE;\n if (s2->picture_structure == PICT_BOTTOM_FIELD)\n mb_y++;\n if (mb_y >= s2->mb_height) {\n av_log(s2->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\\n", mb_y, s2->mb_height);\n return -1;\n }\n if (s2->last_picture_ptr == NULL) {\n if (s2->pict_type == AV_PICTURE_TYPE_B) {\n if (!s->closed_gop)\n break;\n }\n }\n if (s2->pict_type == AV_PICTURE_TYPE_I)\n s->sync=1;\n if (s2->next_picture_ptr == NULL) {\n if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) break;\n }\n if ((avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type == AV_PICTURE_TYPE_B) ||\n (avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type != AV_PICTURE_TYPE_I) ||\n avctx->skip_frame >= AVDISCARD_ALL)\n break;\n if (!s->mpeg_enc_ctx_allocated)\n break;\n if (s2->codec_id == AV_CODEC_ID_MPEG2VIDEO) {\n if (mb_y < avctx->skip_top || mb_y >= s2->mb_height - avctx->skip_bottom)\n break;\n }\n if (!s2->pict_type) {\n av_log(avctx, AV_LOG_ERROR, "Missing picture start code\\n");\n if (avctx->err_recognition & AV_EF_EXPLODE)\n return AVERROR_INVALIDDATA;\n break;\n }\n if (s2->first_slice) {\n s2->first_slice = 0;\n if (mpeg_field_start(s2, buf, buf_size) < 0)\n return -1;\n }\n if (!s2->current_picture_ptr) {\n av_log(avctx, AV_LOG_ERROR, "current_picture not initialized\\n");\n return AVERROR_INVALIDDATA;\n }\n if (avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) {\n s->slice_count++;\n break;\n }\n if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)) {\n int threshold = (s2->mb_height * s->slice_count +\n s2->slice_context_count / 2) /\n s2->slice_context_count;\n if (threshold <= mb_y) {\n MpegEncContext *thread_context = s2->thread_context[s->slice_count];\n thread_context->start_mb_y = mb_y;\n thread_context->end_mb_y = s2->mb_height;\n if (s->slice_count) {\n s2->thread_context[s->slice_count-1]->end_mb_y = mb_y;\n ret = ff_update_duplicate_context(thread_context,\n s2);\n if (ret < 0)\n return ret;\n }\n init_get_bits(&thread_context->gb, buf_ptr, input_size*8);\n s->slice_count++;\n }\n buf_ptr += 2;\n } else {\n ret = mpeg_decode_slice(s2, mb_y, &buf_ptr, input_size);\n emms_c();\n if (ret < 0) {\n if (avctx->err_recognition & AV_EF_EXPLODE)\n return ret;\n if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)\n ff_er_add_slice(&s2->er, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x, s2->mb_y, ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);\n } else {\n ff_er_add_slice(&s2->er, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x-1, s2->mb_y, ER_AC_END | ER_DC_END | ER_MV_END);\n }\n }\n }\n break;\n }\n }\n}', 'const uint8_t *avpriv_mpv_find_start_code(const uint8_t *restrict p,\n const uint8_t *end,\n uint32_t * restrict state)\n{\n int i;\n assert(p <= end);\n if (p >= end)\n return end;\n for (i = 0; i < 3; i++) {\n uint32_t tmp = *state << 8;\n *state = tmp + *(p++);\n if (tmp == 0x100 || p == end)\n return p;\n }\n while (p < end) {\n if (p[-1] > 1 ) p += 3;\n else if (p[-2] ) p += 2;\n else if (p[-3]|(p[-1]-1)) p++;\n else {\n p++;\n break;\n }\n }\n p = FFMIN(p, end) - 4;\n *state = AV_RB32(p);\n return p + 4;\n}']
|
2,660
| 0
|
https://github.com/openssl/openssl/blob/31db43df0859210a32af3708df08f0149c46ede0/ssl/ssl_ciph.c/#L1395
|
STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method,
STACK_OF(SSL_CIPHER) **cipher_list,
STACK_OF(SSL_CIPHER) **cipher_list_by_id,
const char *rule_str)
{
int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases;
unsigned long disabled_mkey, disabled_auth, disabled_enc, disabled_mac, disabled_ssl;
STACK_OF(SSL_CIPHER) *cipherstack, *tmp_cipher_list;
const char *rule_p;
CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
const SSL_CIPHER **ca_list = NULL;
if (rule_str == NULL || cipher_list == NULL || cipher_list_by_id == NULL)
return NULL;
ssl_cipher_get_disabled(&disabled_mkey, &disabled_auth, &disabled_enc, &disabled_mac, &disabled_ssl);
num_of_ciphers = ssl_method->num_ciphers();
#ifdef KSSL_DEBUG
printf("ssl_create_cipher_list() for %d ciphers\n", num_of_ciphers);
#endif
co_list = (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * num_of_ciphers);
if (co_list == NULL)
{
SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST,ERR_R_MALLOC_FAILURE);
return(NULL);
}
ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers,
disabled_mkey, disabled_auth, disabled_enc, disabled_mac, disabled_ssl,
co_list, &head, &tail);
ssl_cipher_apply_rule(0, SSL_kEECDH, 0, 0, 0, 0, 0, CIPHER_ADD, -1, &head, &tail);
ssl_cipher_apply_rule(0, SSL_kEECDH, 0, 0, 0, 0, 0, CIPHER_DEL, -1, &head, &tail);
ssl_cipher_apply_rule(0, 0, 0, SSL_AES, 0, 0, 0, CIPHER_ADD, -1, &head, &tail);
ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ADD, -1, &head, &tail);
ssl_cipher_apply_rule(0, 0, 0, 0, SSL_MD5, 0, 0, CIPHER_ORD, -1, &head, &tail);
ssl_cipher_apply_rule(0, 0, SSL_aNULL, 0, 0, 0, 0, CIPHER_ORD, -1, &head, &tail);
ssl_cipher_apply_rule(0, 0, SSL_aECDH, 0, 0, 0, 0, CIPHER_ORD, -1, &head, &tail);
ssl_cipher_apply_rule(0, SSL_kRSA, 0, 0, 0, 0, 0, CIPHER_ORD, -1, &head, &tail);
ssl_cipher_apply_rule(0, SSL_kPSK, 0,0, 0, 0, 0, CIPHER_ORD, -1, &head, &tail);
ssl_cipher_apply_rule(0, SSL_kKRB5, 0,0, 0, 0, 0, CIPHER_ORD, -1, &head, &tail);
ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, 0, 0, 0, CIPHER_ORD, -1, &head, &tail);
if (!ssl_cipher_strength_sort(&head, &tail))
{
OPENSSL_free(co_list);
return NULL;
}
ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_DEL, -1, &head, &tail);
num_of_group_aliases = sizeof(cipher_aliases) / sizeof(SSL_CIPHER);
num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1;
ca_list = OPENSSL_malloc(sizeof(SSL_CIPHER *) * num_of_alias_max);
if (ca_list == NULL)
{
OPENSSL_free(co_list);
SSLerr(SSL_F_SSL_CREATE_CIPHER_LIST,ERR_R_MALLOC_FAILURE);
return(NULL);
}
ssl_cipher_collect_aliases(ca_list, num_of_group_aliases,
disabled_mkey, disabled_auth, disabled_enc,
disabled_mac, disabled_ssl, head);
ok = 1;
rule_p = rule_str;
if (strncmp(rule_str,"DEFAULT",7) == 0)
{
ok = ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST,
&head, &tail, ca_list);
rule_p += 7;
if (*rule_p == ':')
rule_p++;
}
if (ok && (strlen(rule_p) > 0))
ok = ssl_cipher_process_rulestr(rule_p, &head, &tail, ca_list);
OPENSSL_free((void *)ca_list);
if (!ok)
{
OPENSSL_free(co_list);
return(NULL);
}
if ((cipherstack = sk_SSL_CIPHER_new_null()) == NULL)
{
OPENSSL_free(co_list);
return(NULL);
}
for (curr = head; curr != NULL; curr = curr->next)
{
if (curr->active)
{
sk_SSL_CIPHER_push(cipherstack, curr->cipher);
#ifdef CIPHER_DEBUG
printf("<%s>\n",curr->cipher->name);
#endif
}
}
OPENSSL_free(co_list);
tmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);
if (tmp_cipher_list == NULL)
{
sk_SSL_CIPHER_free(cipherstack);
return NULL;
}
if (*cipher_list != NULL)
sk_SSL_CIPHER_free(*cipher_list);
*cipher_list = cipherstack;
if (*cipher_list_by_id != NULL)
sk_SSL_CIPHER_free(*cipher_list_by_id);
*cipher_list_by_id = tmp_cipher_list;
(void)sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id,ssl_cipher_ptr_id_cmp);
sk_SSL_CIPHER_sort(*cipher_list_by_id);
return(cipherstack);
}
|
['SSL_CTX *SSL_CTX_new(const 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=0;\n\tret->remove_session_cb=0;\n\tret->get_session_cb=0;\n\tret->generate_session_id=0;\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=0;\n\tret->app_verify_arg=NULL;\n\tret->max_cert_list=SSL_MAX_CERT_LIST_DEFAULT;\n\tret->read_ahead=0;\n\tret->msg_callback=0;\n\tret->msg_callback_arg=NULL;\n\tret->verify_mode=SSL_VERIFY_NONE;\n#if 0\n\tret->verify_depth=-1;\n#endif\n\tret->sid_ctx_length=0;\n\tret->default_verify_callback=NULL;\n\tif ((ret->cert=ssl_cert_new()) == NULL)\n\t\tgoto err;\n\tret->default_passwd_callback=0;\n\tret->default_passwd_callback_userdata=NULL;\n\tret->client_cert_cb=0;\n\tret->app_gen_cookie_cb=0;\n\tret->app_verify_cookie_cb=0;\n\tret->sessions=lh_SSL_SESSION_new();\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\tmeth->version == SSL2_VERSION ? "SSLv2" : SSL_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\tret->param = X509_VERIFY_PARAM_new();\n\tif (!ret->param)\n\t\tgoto err;\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(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);\n\tret->extra_certs=NULL;\n\tret->comp_methods=SSL_COMP_get_compression_methods();\n\tret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;\n#ifndef OPENSSL_NO_TLSEXT\n\tret->tlsext_servername_callback = 0;\n\tret->tlsext_servername_arg = NULL;\n\tif ((RAND_pseudo_bytes(ret->tlsext_tick_key_name, 16) <= 0)\n\t\t|| (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0)\n\t\t|| (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0))\n\t\tret->options |= SSL_OP_NO_TICKET;\n\tret->tlsext_status_cb = 0;\n\tret->tlsext_status_arg = NULL;\n#endif\n#ifndef OPENSSL_NO_PSK\n\tret->psk_identity_hint=NULL;\n\tret->psk_client_callback=NULL;\n\tret->psk_server_callback=NULL;\n#endif\n#ifndef OPENSSL_NO_BUF_FREELISTS\n\tret->freelist_max_len = SSL_MAX_BUF_FREELIST_LEN_DEFAULT;\n\tret->rbuf_freelist = OPENSSL_malloc(sizeof(SSL3_BUF_FREELIST));\n\tif (!ret->rbuf_freelist)\n\t\tgoto err;\n\tret->rbuf_freelist->chunklen = 0;\n\tret->rbuf_freelist->len = 0;\n\tret->rbuf_freelist->head = NULL;\n\tret->wbuf_freelist = OPENSSL_malloc(sizeof(SSL3_BUF_FREELIST));\n\tif (!ret->wbuf_freelist)\n\t\t{\n\t\tOPENSSL_free(ret->rbuf_freelist);\n\t\tgoto err;\n\t\t}\n\tret->wbuf_freelist->chunklen = 0;\n\tret->wbuf_freelist->len = 0;\n\tret->wbuf_freelist->head = NULL;\n#endif\n#ifndef OPENSSL_NO_ENGINE\n\tret->client_cert_engine = NULL;\n#ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO\n#define eng_strx(x)\t#x\n#define eng_str(x)\teng_strx(x)\n\t{\n\tENGINE *eng;\n\teng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));\n\tif (!eng)\n\t\t{\n\t\tERR_clear_error();\n\t\tENGINE_load_builtin_engines();\n\t\teng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));\n\t\t}\n\tif (!eng || !SSL_CTX_set_client_cert_engine(ret, eng))\n\t\tERR_clear_error();\n\t}\n#endif\n#endif\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}', 'STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *ssl_method,\n\t\tSTACK_OF(SSL_CIPHER) **cipher_list,\n\t\tSTACK_OF(SSL_CIPHER) **cipher_list_by_id,\n\t\tconst char *rule_str)\n\t{\n\tint ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases;\n\tunsigned long disabled_mkey, disabled_auth, disabled_enc, disabled_mac, disabled_ssl;\n\tSTACK_OF(SSL_CIPHER) *cipherstack, *tmp_cipher_list;\n\tconst char *rule_p;\n\tCIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;\n\tconst SSL_CIPHER **ca_list = NULL;\n\tif (rule_str == NULL || cipher_list == NULL || cipher_list_by_id == NULL)\n\t\treturn NULL;\n\tssl_cipher_get_disabled(&disabled_mkey, &disabled_auth, &disabled_enc, &disabled_mac, &disabled_ssl);\n\tnum_of_ciphers = ssl_method->num_ciphers();\n#ifdef KSSL_DEBUG\n\tprintf("ssl_create_cipher_list() for %d ciphers\\n", num_of_ciphers);\n#endif\n\tco_list = (CIPHER_ORDER *)OPENSSL_malloc(sizeof(CIPHER_ORDER) * num_of_ciphers);\n\tif (co_list == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_CREATE_CIPHER_LIST,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tssl_cipher_collect_ciphers(ssl_method, num_of_ciphers,\n\t disabled_mkey, disabled_auth, disabled_enc, disabled_mac, disabled_ssl,\n\t co_list, &head, &tail);\n\tssl_cipher_apply_rule(0, SSL_kEECDH, 0, 0, 0, 0, 0, CIPHER_ADD, -1, &head, &tail);\n\tssl_cipher_apply_rule(0, SSL_kEECDH, 0, 0, 0, 0, 0, CIPHER_DEL, -1, &head, &tail);\n\tssl_cipher_apply_rule(0, 0, 0, SSL_AES, 0, 0, 0, CIPHER_ADD, -1, &head, &tail);\n\tssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ADD, -1, &head, &tail);\n\tssl_cipher_apply_rule(0, 0, 0, 0, SSL_MD5, 0, 0, CIPHER_ORD, -1, &head, &tail);\n\tssl_cipher_apply_rule(0, 0, SSL_aNULL, 0, 0, 0, 0, CIPHER_ORD, -1, &head, &tail);\n\tssl_cipher_apply_rule(0, 0, SSL_aECDH, 0, 0, 0, 0, CIPHER_ORD, -1, &head, &tail);\n\tssl_cipher_apply_rule(0, SSL_kRSA, 0, 0, 0, 0, 0, CIPHER_ORD, -1, &head, &tail);\n\tssl_cipher_apply_rule(0, SSL_kPSK, 0,0, 0, 0, 0, CIPHER_ORD, -1, &head, &tail);\n\tssl_cipher_apply_rule(0, SSL_kKRB5, 0,0, 0, 0, 0, CIPHER_ORD, -1, &head, &tail);\n\tssl_cipher_apply_rule(0, 0, 0, SSL_RC4, 0, 0, 0, CIPHER_ORD, -1, &head, &tail);\n\tif (!ssl_cipher_strength_sort(&head, &tail))\n\t\t{\n\t\tOPENSSL_free(co_list);\n\t\treturn NULL;\n\t\t}\n\tssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_DEL, -1, &head, &tail);\n\tnum_of_group_aliases = sizeof(cipher_aliases) / sizeof(SSL_CIPHER);\n\tnum_of_alias_max = num_of_ciphers + num_of_group_aliases + 1;\n\tca_list = OPENSSL_malloc(sizeof(SSL_CIPHER *) * num_of_alias_max);\n\tif (ca_list == NULL)\n\t\t{\n\t\tOPENSSL_free(co_list);\n\t\tSSLerr(SSL_F_SSL_CREATE_CIPHER_LIST,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tssl_cipher_collect_aliases(ca_list, num_of_group_aliases,\n\t disabled_mkey, disabled_auth, disabled_enc,\n\t\t\t\t disabled_mac, disabled_ssl, head);\n\tok = 1;\n\trule_p = rule_str;\n\tif (strncmp(rule_str,"DEFAULT",7) == 0)\n\t\t{\n\t\tok = ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST,\n\t\t\t&head, &tail, ca_list);\n\t\trule_p += 7;\n\t\tif (*rule_p == \':\')\n\t\t\trule_p++;\n\t\t}\n\tif (ok && (strlen(rule_p) > 0))\n\t\tok = ssl_cipher_process_rulestr(rule_p, &head, &tail, ca_list);\n\tOPENSSL_free((void *)ca_list);\n\tif (!ok)\n\t\t{\n\t\tOPENSSL_free(co_list);\n\t\treturn(NULL);\n\t\t}\n\tif ((cipherstack = sk_SSL_CIPHER_new_null()) == NULL)\n\t\t{\n\t\tOPENSSL_free(co_list);\n\t\treturn(NULL);\n\t\t}\n\tfor (curr = head; curr != NULL; curr = curr->next)\n\t\t{\n\t\tif (curr->active)\n\t\t\t{\n\t\t\tsk_SSL_CIPHER_push(cipherstack, curr->cipher);\n#ifdef CIPHER_DEBUG\n\t\t\tprintf("<%s>\\n",curr->cipher->name);\n#endif\n\t\t\t}\n\t\t}\n\tOPENSSL_free(co_list);\n\ttmp_cipher_list = sk_SSL_CIPHER_dup(cipherstack);\n\tif (tmp_cipher_list == NULL)\n\t\t{\n\t\tsk_SSL_CIPHER_free(cipherstack);\n\t\treturn NULL;\n\t\t}\n\tif (*cipher_list != NULL)\n\t\tsk_SSL_CIPHER_free(*cipher_list);\n\t*cipher_list = cipherstack;\n\tif (*cipher_list_by_id != NULL)\n\t\tsk_SSL_CIPHER_free(*cipher_list_by_id);\n\t*cipher_list_by_id = tmp_cipher_list;\n\t(void)sk_SSL_CIPHER_set_cmp_func(*cipher_list_by_id,ssl_cipher_ptr_id_cmp);\n\tsk_SSL_CIPHER_sort(*cipher_list_by_id);\n\treturn(cipherstack);\n\t}']
|
2,661
| 0
|
https://github.com/openssl/openssl/blob/a68d8c7b77a3d46d591b89cfd0ecd2a2242e4613/crypto/evp/p_verify.c/#L38
|
int EVP_VerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sigbuf,
unsigned int siglen, EVP_PKEY *pkey)
{
unsigned char m[EVP_MAX_MD_SIZE];
unsigned int m_len = 0;
int i = 0;
EVP_PKEY_CTX *pkctx = NULL;
if (EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_FINALISE)) {
if (!EVP_DigestFinal_ex(ctx, m, &m_len))
goto err;
} else {
int rv = 0;
EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
if (tmp_ctx == NULL) {
EVPerr(EVP_F_EVP_VERIFYFINAL, ERR_R_MALLOC_FAILURE);
return 0;
}
rv = EVP_MD_CTX_copy_ex(tmp_ctx, ctx);
if (rv)
rv = EVP_DigestFinal_ex(tmp_ctx, m, &m_len);
EVP_MD_CTX_free(tmp_ctx);
if (!rv)
return 0;
}
i = -1;
pkctx = EVP_PKEY_CTX_new(pkey, NULL);
if (pkctx == NULL)
goto err;
if (EVP_PKEY_verify_init(pkctx) <= 0)
goto err;
if (EVP_PKEY_CTX_set_signature_md(pkctx, EVP_MD_CTX_md(ctx)) <= 0)
goto err;
i = EVP_PKEY_verify(pkctx, sigbuf, siglen, m, m_len);
err:
EVP_PKEY_CTX_free(pkctx);
return i;
}
|
['int EVP_VerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sigbuf,\n unsigned int siglen, EVP_PKEY *pkey)\n{\n unsigned char m[EVP_MAX_MD_SIZE];\n unsigned int m_len = 0;\n int i = 0;\n EVP_PKEY_CTX *pkctx = NULL;\n if (EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_FINALISE)) {\n if (!EVP_DigestFinal_ex(ctx, m, &m_len))\n goto err;\n } else {\n int rv = 0;\n EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();\n if (tmp_ctx == NULL) {\n EVPerr(EVP_F_EVP_VERIFYFINAL, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n rv = EVP_MD_CTX_copy_ex(tmp_ctx, ctx);\n if (rv)\n rv = EVP_DigestFinal_ex(tmp_ctx, m, &m_len);\n EVP_MD_CTX_free(tmp_ctx);\n if (!rv)\n return 0;\n }\n i = -1;\n pkctx = EVP_PKEY_CTX_new(pkey, NULL);\n if (pkctx == NULL)\n goto err;\n if (EVP_PKEY_verify_init(pkctx) <= 0)\n goto err;\n if (EVP_PKEY_CTX_set_signature_md(pkctx, EVP_MD_CTX_md(ctx)) <= 0)\n goto err;\n i = EVP_PKEY_verify(pkctx, sigbuf, siglen, m, m_len);\n err:\n EVP_PKEY_CTX_free(pkctx);\n return i;\n}', 'int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags)\n{\n return (ctx->flags & flags);\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 EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)\n{\n unsigned char *tmp_buf;\n if ((in == NULL) || (in->digest == NULL)) {\n EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_INPUT_NOT_INITIALIZED);\n return 0;\n }\n#ifndef OPENSSL_NO_ENGINE\n if (in->engine && !ENGINE_init(in->engine)) {\n EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_ENGINE_LIB);\n return 0;\n }\n#endif\n if (out->digest == in->digest) {\n tmp_buf = out->md_data;\n EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);\n } else\n tmp_buf = NULL;\n EVP_MD_CTX_reset(out);\n memcpy(out, in, sizeof(*out));\n out->md_data = NULL;\n out->pctx = NULL;\n if (in->md_data && out->digest->ctx_size) {\n if (tmp_buf)\n out->md_data = tmp_buf;\n else {\n out->md_data = OPENSSL_malloc(out->digest->ctx_size);\n if (out->md_data == NULL) {\n EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n }\n memcpy(out->md_data, in->md_data, out->digest->ctx_size);\n }\n out->update = in->update;\n if (in->pctx) {\n out->pctx = EVP_PKEY_CTX_dup(in->pctx);\n if (!out->pctx) {\n EVP_MD_CTX_reset(out);\n return 0;\n }\n }\n if (out->digest->copy)\n return out->digest->copy(out, in);\n return 1;\n}', 'int ENGINE_init(ENGINE *e)\n{\n int ret;\n if (e == NULL) {\n ENGINEerr(ENGINE_F_ENGINE_INIT, ERR_R_PASSED_NULL_PARAMETER);\n return 0;\n }\n if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {\n ENGINEerr(ENGINE_F_ENGINE_INIT, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n CRYPTO_THREAD_write_lock(global_engine_lock);\n ret = engine_unlocked_init(e);\n CRYPTO_THREAD_unlock(global_engine_lock);\n return ret;\n}', 'int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))\n{\n if (pthread_once(once, init) != 0)\n return 0;\n return 1;\n}', '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}']
|
2,662
| 0
|
https://github.com/openssl/openssl/blob/8b0d4242404f9e5da26e7594fa0864b2df4601af/crypto/bn/bn_sqr.c/#L120
|
void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)
{
int i, j, max;
const BN_ULONG *ap;
BN_ULONG *rp;
max = n * 2;
ap = a;
rp = r;
rp[0] = rp[max - 1] = 0;
rp++;
j = n;
if (--j > 0) {
ap++;
rp[j] = bn_mul_words(rp, ap, j, ap[-1]);
rp += 2;
}
for (i = n - 2; i > 0; i--) {
j--;
ap++;
rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);
rp += 2;
}
bn_add_words(r, r, r, max);
bn_sqr_words(tmp, a, n);
bn_add_words(r, r, tmp, max);
}
|
['static int test_exp_mod_zero()\n{\n BIGNUM *a = NULL, *p = NULL, *m = NULL;\n BIGNUM *r = NULL;\n BN_ULONG one_word = 1;\n BN_CTX *ctx = BN_CTX_new();\n int ret = 1, failed = 0;\n m = BN_new();\n if (!m)\n goto err;\n BN_one(m);\n a = BN_new();\n if (!a)\n goto err;\n BN_one(a);\n p = BN_new();\n if (!p)\n goto err;\n BN_zero(p);\n r = BN_new();\n if (!r)\n goto err;\n if (!BN_rand(a, 1024, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))\n goto err;\n if (!BN_mod_exp(r, a, p, m, ctx))\n goto err;\n if (!a_is_zero_mod_one("BN_mod_exp", r, a))\n failed = 1;\n if (!BN_mod_exp_recp(r, a, p, m, ctx))\n goto err;\n if (!a_is_zero_mod_one("BN_mod_exp_recp", r, a))\n failed = 1;\n if (!BN_mod_exp_simple(r, a, p, m, ctx))\n goto err;\n if (!a_is_zero_mod_one("BN_mod_exp_simple", r, a))\n failed = 1;\n if (!BN_mod_exp_mont(r, a, p, m, ctx, NULL))\n goto err;\n if (!a_is_zero_mod_one("BN_mod_exp_mont", r, a))\n failed = 1;\n if (!BN_mod_exp_mont_consttime(r, a, p, m, ctx, NULL)) {\n goto err;\n }\n if (!a_is_zero_mod_one("BN_mod_exp_mont_consttime", r, a))\n failed = 1;\n if (!BN_mod_exp_mont_word(r, one_word, p, m, ctx, NULL))\n goto err;\n if (!BN_is_zero(r)) {\n fprintf(stderr, "BN_mod_exp_mont_word failed:\\n");\n fprintf(stderr, "1 ** 0 mod 1 = r (should be 0)\\n");\n fprintf(stderr, "r = ");\n BN_print_fp(stderr, r);\n fprintf(stderr, "\\n");\n return 0;\n }\n ret = failed;\n err:\n BN_free(r);\n BN_free(a);\n BN_free(p);\n BN_free(m);\n BN_CTX_free(ctx);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return (1);\n}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n BN_CTX *ctx)\n{\n int ret;\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n if (BN_is_odd(m)) {\n# ifdef MONT_EXP_WORD\n if (a->top == 1 && !a->neg\n && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)) {\n BN_ULONG A = a->d[0];\n ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);\n } else\n# endif\n ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);\n } else\n#endif\n#ifdef RECP_MUL_MOD\n {\n ret = BN_mod_exp_recp(r, a, p, m, ctx);\n }\n#else\n {\n ret = BN_mod_exp_simple(r, a, p, m, ctx);\n }\n#endif\n bn_check_top(r);\n return (ret);\n}', 'int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n int start = 1;\n BIGNUM *d, *r;\n const BIGNUM *aa;\n BIGNUM *val[TABLE_SIZE];\n BN_MONT_CTX *mont = NULL;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0) {\n return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont);\n }\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT, BN_R_CALLED_WITH_EVEN_MODULUS);\n return (0);\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_is_one(m)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n d = BN_CTX_get(ctx);\n r = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (!d || !r || !val[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 if (a->neg || BN_ucmp(a, m) >= 0) {\n if (!BN_nnmod(val[0], a, m, ctx))\n goto err;\n aa = val[0];\n } else\n aa = a;\n if (BN_is_zero(aa)) {\n BN_zero(rr);\n ret = 1;\n goto err;\n }\n if (!BN_to_montgomery(val[0], aa, mont, ctx))\n goto err;\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!BN_mod_mul_montgomery(d, val[0], val[0], mont, ctx))\n goto err;\n j = 1 << (window - 1);\n for (i = 1; i < j; i++) {\n if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul_montgomery(val[i], val[i - 1], d, mont, ctx))\n goto err;\n }\n }\n start = 1;\n wvalue = 0;\n wstart = bits - 1;\n wend = 0;\n#if 1\n j = m->top;\n if (m->d[j - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n if (bn_wexpand(r, j) == NULL)\n goto err;\n r->d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < j; i++)\n r->d[i] = (~m->d[i]) & BN_MASK2;\n r->top = j;\n bn_correct_top(r);\n } else\n#endif\n if (!BN_to_montgomery(r, BN_value_one(), mont, ctx))\n goto err;\n for (;;) {\n if (BN_is_bit_set(p, wstart) == 0) {\n if (!start) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (wstart == 0)\n break;\n wstart--;\n continue;\n }\n j = wstart;\n wvalue = 1;\n wend = 0;\n for (i = 1; i < window; i++) {\n if (wstart - i < 0)\n break;\n if (BN_is_bit_set(p, wstart - i)) {\n wvalue <<= (i - wend);\n wvalue |= 1;\n wend = i;\n }\n }\n j = wend + 1;\n if (!start)\n for (i = 0; i < j; i++) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (!BN_mod_mul_montgomery(r, r, val[wvalue >> 1], mont, ctx))\n goto err;\n wstart -= wend + 1;\n wvalue = 0;\n start = 0;\n if (wstart < 0)\n break;\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n j = mont->N.top;\n val[0]->d[0] = 1;\n for (i = 1; i < j; i++)\n val[0]->d[i] = 0;\n val[0]->top = j;\n if (!BN_mod_mul_montgomery(rr, r, val[0], mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, r, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n BN_CTX_end(ctx);\n bn_check_top(rr);\n return (ret);\n}', 'int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx,\n BN_MONT_CTX *in_mont)\n{\n int i, bits, ret = 0, window, wvalue;\n int top;\n BN_MONT_CTX *mont = NULL;\n int numPowers;\n unsigned char *powerbufFree = NULL;\n int powerbufLen = 0;\n unsigned char *powerbuf = NULL;\n BIGNUM tmp, am;\n#if defined(SPARC_T4_MONT)\n unsigned int t4 = 0;\n#endif\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT_CONSTTIME, BN_R_CALLED_WITH_EVEN_MODULUS);\n return (0);\n }\n top = m->top;\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_is_one(m)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n#ifdef RSAZ_ENABLED\n if ((16 == a->top) && (16 == p->top) && (BN_num_bits(m) == 1024)\n && rsaz_avx2_eligible()) {\n if (NULL == bn_wexpand(rr, 16))\n goto err;\n RSAZ_1024_mod_exp_avx2(rr->d, a->d, p->d, m->d, mont->RR.d,\n mont->n0[0]);\n rr->top = 16;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n } else if ((8 == a->top) && (8 == p->top) && (BN_num_bits(m) == 512)) {\n if (NULL == bn_wexpand(rr, 8))\n goto err;\n RSAZ_512_mod_exp(rr->d, a->d, p->d, m->d, mont->n0[0], mont->RR.d);\n rr->top = 8;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n }\n#endif\n window = BN_window_bits_for_ctime_exponent_size(bits);\n#if defined(SPARC_T4_MONT)\n if (window >= 5 && (top & 15) == 0 && top <= 64 &&\n (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) ==\n (CFR_MONTMUL | CFR_MONTSQR) && (t4 = OPENSSL_sparcv9cap_P[0]))\n window = 5;\n else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window >= 5) {\n window = 5;\n powerbufLen += top * sizeof(mont->N.d[0]);\n }\n#endif\n (void)0;\n numPowers = 1 << window;\n powerbufLen += sizeof(m->d[0]) * (top * numPowers +\n ((2 * top) >\n numPowers ? (2 * top) : numPowers));\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree =\n alloca(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH);\n else\n#endif\n if ((powerbufFree =\n OPENSSL_malloc(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH))\n == NULL)\n goto err;\n powerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);\n memset(powerbuf, 0, powerbufLen);\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree = NULL;\n#endif\n tmp.d = (BN_ULONG *)(powerbuf + sizeof(m->d[0]) * top * numPowers);\n am.d = tmp.d + top;\n tmp.top = am.top = 0;\n tmp.dmax = am.dmax = top;\n tmp.neg = am.neg = 0;\n tmp.flags = am.flags = BN_FLG_STATIC_DATA;\n#if 1\n if (m->d[top - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n tmp.d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < top; i++)\n tmp.d[i] = (~m->d[i]) & BN_MASK2;\n tmp.top = top;\n } else\n#endif\n if (!BN_to_montgomery(&tmp, BN_value_one(), mont, ctx))\n goto err;\n if (a->neg || BN_ucmp(a, m) >= 0) {\n if (!BN_mod(&am, a, m, ctx))\n goto err;\n if (!BN_to_montgomery(&am, &am, mont, ctx))\n goto err;\n } else if (!BN_to_montgomery(&am, a, mont, ctx))\n goto err;\n#if defined(SPARC_T4_MONT)\n if (t4) {\n typedef int (*bn_pwr5_mont_f) (BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_8(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_16(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_24(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_32(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n static const bn_pwr5_mont_f pwr5_funcs[4] = {\n bn_pwr5_mont_t4_8, bn_pwr5_mont_t4_16,\n bn_pwr5_mont_t4_24, bn_pwr5_mont_t4_32\n };\n bn_pwr5_mont_f pwr5_worker = pwr5_funcs[top / 16 - 1];\n typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap, const void *bp,\n const BN_ULONG *np, const BN_ULONG *n0);\n int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n static const bn_mul_mont_f mul_funcs[4] = {\n bn_mul_mont_t4_8, bn_mul_mont_t4_16,\n bn_mul_mont_t4_24, bn_mul_mont_t4_32\n };\n bn_mul_mont_f mul_worker = mul_funcs[top / 16 - 1];\n void bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_gather5_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_flip_n_scatter5_t4(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5_t4(BN_ULONG *out, size_t num,\n void *table, size_t power);\n void bn_flip_t4(BN_ULONG *dst, BN_ULONG *src, size_t num);\n BN_ULONG *np = mont->N.d, *n0 = mont->n0;\n int stride = 5 * (6 - (top / 16 - 1));\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 0);\n bn_flip_n_scatter5_t4(am.d, top, powerbuf, 1);\n if (!(*mul_worker) (tmp.d, am.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, am.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, am.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 2);\n for (i = 3; i < 32; i++) {\n if (!(*mul_worker) (tmp.d, tmp.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, tmp.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, tmp.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, i);\n }\n np = alloca(top * sizeof(BN_ULONG));\n top /= 2;\n bn_flip_t4(np, mont->N.d, top);\n bits--;\n for (wvalue = 0, i = bits % 5; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_gather5_t4(tmp.d, top, powerbuf, wvalue);\n while (bits >= 0) {\n if (bits < stride)\n stride = bits + 1;\n bits -= stride;\n wvalue = bn_get_bits(p, bits + 1);\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n bits += stride - 5;\n wvalue >>= stride - 5;\n wvalue &= 31;\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5_t4(tmp.d, tmp.d, powerbuf, np, n0, top,\n wvalue);\n }\n bn_flip_t4(tmp.d, tmp.d, top);\n top *= 2;\n tmp.top = top;\n bn_correct_top(&tmp);\n OPENSSL_cleanse(np, top * sizeof(BN_ULONG));\n } else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window == 5 && top > 1) {\n void bn_mul_mont_gather5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_scatter5(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5(BN_ULONG *out, size_t num, void *table, size_t power);\n void bn_power5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n int bn_get_bits5(const BN_ULONG *ap, int off);\n int bn_from_montgomery(BN_ULONG *rp, const BN_ULONG *ap,\n const BN_ULONG *not_used, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n BN_ULONG *n0 = mont->n0, *np;\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n for (np = am.d + top, i = 0; i < top; i++)\n np[i] = mont->N.d[i];\n bn_scatter5(tmp.d, top, powerbuf, 0);\n bn_scatter5(am.d, am.top, powerbuf, 1);\n bn_mul_mont(tmp.d, am.d, am.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2);\n# if 0\n for (i = 3; i < 32; i++) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# else\n for (i = 4; i < 32; i *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n for (i = 3; i < 8; i += 2) {\n int j;\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n for (j = 2 * i; j < 32; j *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, j);\n }\n }\n for (; i < 16; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2 * i);\n }\n for (; i < 32; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# endif\n bits--;\n for (wvalue = 0, i = bits % 5; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_gather5(tmp.d, top, powerbuf, wvalue);\n if (top & 7)\n while (bits >= 0) {\n for (wvalue = 0, i = 0; i < 5; i++, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5(tmp.d, tmp.d, powerbuf, np, n0, top,\n wvalue);\n } else {\n while (bits >= 0) {\n wvalue = bn_get_bits5(p->d, bits - 4);\n bits -= 5;\n bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top, wvalue);\n }\n }\n ret = bn_from_montgomery(tmp.d, tmp.d, NULL, np, n0, top);\n tmp.top = top;\n bn_correct_top(&tmp);\n if (ret) {\n if (!BN_copy(rr, &tmp))\n ret = 0;\n goto err;\n }\n } else\n#endif\n {\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 0, window))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, window))\n goto err;\n if (window > 1) {\n if (!BN_mod_mul_montgomery(&tmp, &am, &am, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 2,\n window))\n goto err;\n for (i = 3; i < numPowers; i++) {\n if (!BN_mod_mul_montgomery(&tmp, &am, &tmp, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, i,\n window))\n goto err;\n }\n }\n bits--;\n for (wvalue = 0, i = bits % window; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&tmp, top, powerbuf, wvalue,\n window))\n goto err;\n while (bits >= 0) {\n wvalue = 0;\n for (i = 0; i < window; i++, bits--) {\n if (!BN_mod_mul_montgomery(&tmp, &tmp, &tmp, mont, ctx))\n goto err;\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n }\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&am, top, powerbuf, wvalue,\n window))\n goto err;\n if (!BN_mod_mul_montgomery(&tmp, &tmp, &am, mont, ctx))\n goto err;\n }\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n am.d[0] = 1;\n for (i = 1; i < top; i++)\n am.d[i] = 0;\n if (!BN_mod_mul_montgomery(rr, &tmp, &am, mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, &tmp, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n if (powerbuf != NULL) {\n OPENSSL_cleanse(powerbuf, powerbufLen);\n OPENSSL_free(powerbufFree);\n }\n BN_CTX_end(ctx);\n return (ret);\n}', 'int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,\n BN_CTX *ctx)\n{\n return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);\n}', 'int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n BN_MONT_CTX *mont, BN_CTX *ctx)\n{\n BIGNUM *tmp;\n int ret = 0;\n#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\n int num = mont->N.top;\n if (num > 1 && a->top == num && b->top == num) {\n if (bn_wexpand(r, num) == NULL)\n return (0);\n if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {\n r->neg = a->neg ^ b->neg;\n r->top = num;\n bn_correct_top(r);\n return (1);\n }\n }\n#endif\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n if (tmp == NULL)\n goto err;\n bn_check_top(tmp);\n if (a == b) {\n if (!BN_sqr(tmp, a, ctx))\n goto err;\n } else {\n if (!BN_mul(tmp, a, b, ctx))\n goto err;\n }\n#ifdef MONT_WORD\n if (!BN_from_montgomery_word(r, tmp, mont))\n goto err;\n#else\n if (!BN_from_montgomery(r, tmp, mont, ctx))\n goto err;\n#endif\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return (ret);\n}', 'int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int max, al;\n int ret = 0;\n BIGNUM *tmp, *rr;\n bn_check_top(a);\n al = a->top;\n if (al <= 0) {\n r->top = 0;\n r->neg = 0;\n return 1;\n }\n BN_CTX_start(ctx);\n rr = (a != r) ? r : BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (!rr || !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 (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(rr);\n bn_check_top(tmp);\n BN_CTX_end(ctx);\n return (ret);\n}', 'void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)\n{\n int i, j, max;\n const BN_ULONG *ap;\n BN_ULONG *rp;\n max = n * 2;\n ap = a;\n rp = r;\n rp[0] = rp[max - 1] = 0;\n rp++;\n j = n;\n if (--j > 0) {\n ap++;\n rp[j] = bn_mul_words(rp, ap, j, ap[-1]);\n rp += 2;\n }\n for (i = n - 2; i > 0; i--) {\n j--;\n ap++;\n rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);\n rp += 2;\n }\n bn_add_words(r, r, r, max);\n bn_sqr_words(tmp, a, n);\n bn_add_words(r, r, tmp, max);\n}']
|
2,663
| 0
|
https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavformat/flvdec.c/#L363
|
static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
{
int ret, i, type, size, pts, flags, is_audio, next, pos;
AVStream *st = NULL;
for(;;){
pos = url_ftell(s->pb);
url_fskip(s->pb, 4);
type = get_byte(s->pb);
size = get_be24(s->pb);
pts = get_be24(s->pb);
pts |= get_byte(s->pb) << 24;
if (url_feof(s->pb))
return AVERROR(EIO);
url_fskip(s->pb, 3);
flags = 0;
if(size == 0)
continue;
next= size + url_ftell(s->pb);
if (type == FLV_TAG_TYPE_AUDIO) {
is_audio=1;
flags = get_byte(s->pb);
} else if (type == FLV_TAG_TYPE_VIDEO) {
is_audio=0;
flags = get_byte(s->pb);
} else {
if (type == FLV_TAG_TYPE_META && size > 13+1+4)
flv_read_metabody(s, next);
else
av_log(s, AV_LOG_ERROR, "skipping flv packet: type %d, size %d, flags %d\n", type, size, flags);
url_fseek(s->pb, next, SEEK_SET);
continue;
}
for(i=0;i<s->nb_streams;i++) {
st = s->streams[i];
if (st->id == is_audio)
break;
}
if(i == s->nb_streams){
av_log(NULL, AV_LOG_ERROR, "invalid stream\n");
st= create_stream(s, is_audio);
}
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
){
url_fseek(s->pb, next, SEEK_SET);
continue;
}
if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY)
av_add_index_entry(st, pos, pts, size, 0, AVINDEX_KEYFRAME);
break;
}
if(!url_is_streamed(s->pb) && s->duration==AV_NOPTS_VALUE){
int size;
const int pos= url_ftell(s->pb);
const int fsize= url_fsize(s->pb);
url_fseek(s->pb, fsize-4, SEEK_SET);
size= get_be32(s->pb);
url_fseek(s->pb, fsize-3-size, SEEK_SET);
if(size == get_be24(s->pb) + 11){
s->duration= get_be24(s->pb) * (int64_t)AV_TIME_BASE / 1000;
}
url_fseek(s->pb, pos, SEEK_SET);
}
if(is_audio){
if(!st->codec->sample_rate || !st->codec->bits_per_sample || (!st->codec->codec_id && !st->codec->codec_tag)) {
st->codec->channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
if((flags & FLV_AUDIO_CODECID_MASK) == FLV_CODECID_NELLYMOSER_8HZ_MONO)
st->codec->sample_rate= 8000;
else
st->codec->sample_rate = (44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >> FLV_AUDIO_SAMPLERATE_OFFSET) >> 3);
st->codec->bits_per_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
flv_set_audio_codec(s, st, flags & FLV_AUDIO_CODECID_MASK);
}
}else{
size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK);
}
ret= av_get_packet(s->pb, pkt, size - 1);
if (ret <= 0) {
return AVERROR(EIO);
}
pkt->size = ret;
pkt->pts = pts;
pkt->stream_index = st->index;
if (is_audio || ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY))
pkt->flags |= PKT_FLAG_KEY;
return ret;
}
|
['static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)\n{\n int ret, i, type, size, pts, flags, is_audio, next, pos;\n AVStream *st = NULL;\n for(;;){\n pos = url_ftell(s->pb);\n url_fskip(s->pb, 4);\n type = get_byte(s->pb);\n size = get_be24(s->pb);\n pts = get_be24(s->pb);\n pts |= get_byte(s->pb) << 24;\n if (url_feof(s->pb))\n return AVERROR(EIO);\n url_fskip(s->pb, 3);\n flags = 0;\n if(size == 0)\n continue;\n next= size + url_ftell(s->pb);\n if (type == FLV_TAG_TYPE_AUDIO) {\n is_audio=1;\n flags = get_byte(s->pb);\n } else if (type == FLV_TAG_TYPE_VIDEO) {\n is_audio=0;\n flags = get_byte(s->pb);\n } else {\n if (type == FLV_TAG_TYPE_META && size > 13+1+4)\n flv_read_metabody(s, next);\n else\n av_log(s, AV_LOG_ERROR, "skipping flv packet: type %d, size %d, flags %d\\n", type, size, flags);\n url_fseek(s->pb, next, SEEK_SET);\n continue;\n }\n for(i=0;i<s->nb_streams;i++) {\n st = s->streams[i];\n if (st->id == is_audio)\n break;\n }\n if(i == s->nb_streams){\n av_log(NULL, AV_LOG_ERROR, "invalid stream\\n");\n st= create_stream(s, is_audio);\n }\n if( (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || is_audio))\n ||(st->discard >= AVDISCARD_BIDIR && ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && !is_audio))\n || st->discard >= AVDISCARD_ALL\n ){\n url_fseek(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, pts, size, 0, AVINDEX_KEYFRAME);\n break;\n }\n if(!url_is_streamed(s->pb) && s->duration==AV_NOPTS_VALUE){\n int size;\n const int pos= url_ftell(s->pb);\n const int fsize= url_fsize(s->pb);\n url_fseek(s->pb, fsize-4, SEEK_SET);\n size= get_be32(s->pb);\n url_fseek(s->pb, fsize-3-size, SEEK_SET);\n if(size == get_be24(s->pb) + 11){\n s->duration= get_be24(s->pb) * (int64_t)AV_TIME_BASE / 1000;\n }\n url_fseek(s->pb, pos, SEEK_SET);\n }\n if(is_audio){\n if(!st->codec->sample_rate || !st->codec->bits_per_sample || (!st->codec->codec_id && !st->codec->codec_tag)) {\n st->codec->channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;\n if((flags & FLV_AUDIO_CODECID_MASK) == FLV_CODECID_NELLYMOSER_8HZ_MONO)\n st->codec->sample_rate= 8000;\n else\n st->codec->sample_rate = (44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >> FLV_AUDIO_SAMPLERATE_OFFSET) >> 3);\n st->codec->bits_per_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;\n flv_set_audio_codec(s, st, flags & FLV_AUDIO_CODECID_MASK);\n }\n }else{\n size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK);\n }\n ret= av_get_packet(s->pb, pkt, size - 1);\n if (ret <= 0) {\n return AVERROR(EIO);\n }\n pkt->size = ret;\n pkt->pts = pts;\n pkt->stream_index = st->index;\n if (is_audio || ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY))\n pkt->flags |= PKT_FLAG_KEY;\n return ret;\n}']
|
2,664
| 0
|
https://github.com/libav/libav/blob/04de5bf56c1f1f946272f436d9c8cb82c63d30b4/libavcodec/ac3enc.c/#L2802
|
static av_cold void set_bandwidth(AC3EncodeContext *s)
{
int blk, ch;
int av_uninit(cpl_start);
if (s->cutoff) {
int fbw_coeffs;
fbw_coeffs = s->cutoff * 2 * AC3_MAX_COEFS / s->sample_rate;
s->bandwidth_code = av_clip((fbw_coeffs - 73) / 3, 0, 60);
} else {
s->bandwidth_code = ac3_bandwidth_tab[s->fbw_channels-1][s->bit_alloc.sr_code][s->frame_size_code/2];
}
for (ch = 1; ch <= s->fbw_channels; ch++) {
s->start_freq[ch] = 0;
for (blk = 0; blk < AC3_MAX_BLOCKS; blk++)
s->blocks[blk].end_freq[ch] = s->bandwidth_code * 3 + 73;
}
if (s->lfe_on) {
s->start_freq[s->lfe_channel] = 0;
for (blk = 0; blk < AC3_MAX_BLOCKS; blk++)
s->blocks[blk].end_freq[ch] = 7;
}
if (s->cpl_enabled) {
if (s->options.cpl_start >= 0) {
cpl_start = s->options.cpl_start;
} else {
cpl_start = ac3_coupling_start_tab[s->channel_mode-2][s->bit_alloc.sr_code][s->frame_size_code/2];
if (cpl_start < 0)
s->cpl_enabled = 0;
}
}
if (s->cpl_enabled) {
int i, cpl_start_band, cpl_end_band;
uint8_t *cpl_band_sizes = s->cpl_band_sizes;
cpl_end_band = s->bandwidth_code / 4 + 3;
cpl_start_band = av_clip(cpl_start, 0, FFMIN(cpl_end_band-1, 15));
s->num_cpl_subbands = cpl_end_band - cpl_start_band;
s->num_cpl_bands = 1;
*cpl_band_sizes = 12;
for (i = cpl_start_band + 1; i < cpl_end_band; i++) {
if (ff_eac3_default_cpl_band_struct[i]) {
*cpl_band_sizes += 12;
} else {
s->num_cpl_bands++;
cpl_band_sizes++;
*cpl_band_sizes = 12;
}
}
s->start_freq[CPL_CH] = cpl_start_band * 12 + 37;
s->cpl_end_freq = cpl_end_band * 12 + 37;
for (blk = 0; blk < AC3_MAX_BLOCKS; blk++)
s->blocks[blk].end_freq[CPL_CH] = s->cpl_end_freq;
}
}
|
['static av_cold int ac3_encode_init(AVCodecContext *avctx)\n{\n AC3EncodeContext *s = avctx->priv_data;\n int ret, frame_size_58;\n s->eac3 = avctx->codec_id == CODEC_ID_EAC3;\n avctx->frame_size = AC3_FRAME_SIZE;\n ff_ac3_common_init();\n ret = validate_options(avctx, s);\n if (ret)\n return ret;\n s->bitstream_mode = avctx->audio_service_type;\n if (s->bitstream_mode == AV_AUDIO_SERVICE_TYPE_KARAOKE)\n s->bitstream_mode = 0x7;\n s->bits_written = 0;\n s->samples_written = 0;\n frame_size_58 = (( s->frame_size >> 2) + ( s->frame_size >> 4)) << 1;\n s->crc_inv[0] = pow_poly((CRC16_POLY >> 1), (8 * frame_size_58) - 16, CRC16_POLY);\n if (s->bit_alloc.sr_code == 1) {\n frame_size_58 = (((s->frame_size+2) >> 2) + ((s->frame_size+2) >> 4)) << 1;\n s->crc_inv[1] = pow_poly((CRC16_POLY >> 1), (8 * frame_size_58) - 16, CRC16_POLY);\n }\n set_bandwidth(s);\n exponent_init(s);\n bit_alloc_init(s);\n ret = mdct_init(avctx, &s->mdct, 9);\n if (ret)\n goto init_fail;\n ret = allocate_buffers(avctx);\n if (ret)\n goto init_fail;\n avctx->coded_frame= avcodec_alloc_frame();\n dsputil_init(&s->dsp, avctx);\n ff_ac3dsp_init(&s->ac3dsp, avctx->flags & CODEC_FLAG_BITEXACT);\n dprint_options(avctx);\n return 0;\ninit_fail:\n ac3_encode_close(avctx);\n return ret;\n}', 'static av_cold int validate_options(AVCodecContext *avctx, AC3EncodeContext *s)\n{\n int i, ret, max_sr;\n if (!avctx->channel_layout) {\n av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The "\n "encoder will guess the layout, but it "\n "might be incorrect.\\n");\n }\n ret = set_channel_info(s, avctx->channels, &avctx->channel_layout);\n if (ret) {\n av_log(avctx, AV_LOG_ERROR, "invalid channel layout\\n");\n return ret;\n }\n max_sr = s->eac3 ? 2 : 8;\n for (i = 0; i <= max_sr; i++) {\n if ((ff_ac3_sample_rate_tab[i % 3] >> (i / 3)) == avctx->sample_rate)\n break;\n }\n if (i > max_sr) {\n av_log(avctx, AV_LOG_ERROR, "invalid sample rate\\n");\n return AVERROR(EINVAL);\n }\n s->sample_rate = avctx->sample_rate;\n s->bit_alloc.sr_shift = i / 3;\n s->bit_alloc.sr_code = i % 3;\n s->bitstream_id = s->eac3 ? 16 : 8 + s->bit_alloc.sr_shift;\n if (s->eac3) {\n int max_br, min_br, wpf, min_br_dist, min_br_code;\n max_br = 2048 * s->sample_rate / AC3_FRAME_SIZE * 16;\n min_br = ((s->sample_rate + (AC3_FRAME_SIZE-1)) / AC3_FRAME_SIZE) * 16;\n if (avctx->bit_rate < min_br || avctx->bit_rate > max_br) {\n av_log(avctx, AV_LOG_ERROR, "invalid bit rate. must be %d to %d "\n "for this sample rate\\n", min_br, max_br);\n return AVERROR(EINVAL);\n }\n wpf = (avctx->bit_rate / 16) * AC3_FRAME_SIZE / s->sample_rate;\n av_assert1(wpf > 0 && wpf <= 2048);\n min_br_code = -1;\n min_br_dist = INT_MAX;\n for (i = 0; i < 19; i++) {\n int br_dist = abs(ff_ac3_bitrate_tab[i] * 1000 - avctx->bit_rate);\n if (br_dist < min_br_dist) {\n min_br_dist = br_dist;\n min_br_code = i;\n }\n }\n s->frame_size_code = min_br_code << 1;\n while (wpf > 1 && wpf * s->sample_rate / AC3_FRAME_SIZE * 16 > avctx->bit_rate)\n wpf--;\n s->frame_size_min = 2 * wpf;\n } else {\n for (i = 0; i < 19; i++) {\n if ((ff_ac3_bitrate_tab[i] >> s->bit_alloc.sr_shift)*1000 == avctx->bit_rate)\n break;\n }\n if (i == 19) {\n av_log(avctx, AV_LOG_ERROR, "invalid bit rate\\n");\n return AVERROR(EINVAL);\n }\n s->frame_size_code = i << 1;\n s->frame_size_min = 2 * ff_ac3_frame_size_tab[s->frame_size_code][s->bit_alloc.sr_code];\n }\n s->bit_rate = avctx->bit_rate;\n s->frame_size = s->frame_size_min;\n if (avctx->cutoff < 0) {\n av_log(avctx, AV_LOG_ERROR, "invalid cutoff frequency\\n");\n return AVERROR(EINVAL);\n }\n s->cutoff = avctx->cutoff;\n if (s->cutoff > (s->sample_rate >> 1))\n s->cutoff = s->sample_rate >> 1;\n if ((avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_KARAOKE &&\n avctx->channels == 1) ||\n ((avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_COMMENTARY ||\n avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_EMERGENCY ||\n avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_VOICE_OVER)\n && avctx->channels > 1)) {\n av_log(avctx, AV_LOG_ERROR, "invalid audio service type for the "\n "specified number of channels\\n");\n return AVERROR(EINVAL);\n }\n if (!s->eac3) {\n ret = validate_metadata(avctx);\n if (ret)\n return ret;\n }\n s->rematrixing_enabled = s->options.stereo_rematrixing &&\n (s->channel_mode == AC3_CHMODE_STEREO);\n s->cpl_enabled = s->options.channel_coupling &&\n s->channel_mode >= AC3_CHMODE_STEREO &&\n CONFIG_AC3ENC_FLOAT;\n return 0;\n}', 'static av_cold int set_channel_info(AC3EncodeContext *s, int channels,\n int64_t *channel_layout)\n{\n int ch_layout;\n if (channels < 1 || channels > AC3_MAX_CHANNELS)\n return AVERROR(EINVAL);\n if ((uint64_t)*channel_layout > 0x7FF)\n return AVERROR(EINVAL);\n ch_layout = *channel_layout;\n if (!ch_layout)\n ch_layout = avcodec_guess_channel_layout(channels, CODEC_ID_AC3, NULL);\n s->lfe_on = !!(ch_layout & AV_CH_LOW_FREQUENCY);\n s->channels = channels;\n s->fbw_channels = channels - s->lfe_on;\n s->lfe_channel = s->lfe_on ? s->fbw_channels + 1 : -1;\n if (s->lfe_on)\n ch_layout -= AV_CH_LOW_FREQUENCY;\n switch (ch_layout) {\n case AV_CH_LAYOUT_MONO: s->channel_mode = AC3_CHMODE_MONO; break;\n case AV_CH_LAYOUT_STEREO: s->channel_mode = AC3_CHMODE_STEREO; break;\n case AV_CH_LAYOUT_SURROUND: s->channel_mode = AC3_CHMODE_3F; break;\n case AV_CH_LAYOUT_2_1: s->channel_mode = AC3_CHMODE_2F1R; break;\n case AV_CH_LAYOUT_4POINT0: s->channel_mode = AC3_CHMODE_3F1R; break;\n case AV_CH_LAYOUT_QUAD:\n case AV_CH_LAYOUT_2_2: s->channel_mode = AC3_CHMODE_2F2R; break;\n case AV_CH_LAYOUT_5POINT0:\n case AV_CH_LAYOUT_5POINT0_BACK: s->channel_mode = AC3_CHMODE_3F2R; break;\n default:\n return AVERROR(EINVAL);\n }\n s->has_center = (s->channel_mode & 0x01) && s->channel_mode != AC3_CHMODE_MONO;\n s->has_surround = s->channel_mode & 0x04;\n s->channel_map = ff_ac3_enc_channel_map[s->channel_mode][s->lfe_on];\n *channel_layout = ch_layout;\n if (s->lfe_on)\n *channel_layout |= AV_CH_LOW_FREQUENCY;\n return 0;\n}', 'static av_cold void set_bandwidth(AC3EncodeContext *s)\n{\n int blk, ch;\n int av_uninit(cpl_start);\n if (s->cutoff) {\n int fbw_coeffs;\n fbw_coeffs = s->cutoff * 2 * AC3_MAX_COEFS / s->sample_rate;\n s->bandwidth_code = av_clip((fbw_coeffs - 73) / 3, 0, 60);\n } else {\n s->bandwidth_code = ac3_bandwidth_tab[s->fbw_channels-1][s->bit_alloc.sr_code][s->frame_size_code/2];\n }\n for (ch = 1; ch <= s->fbw_channels; ch++) {\n s->start_freq[ch] = 0;\n for (blk = 0; blk < AC3_MAX_BLOCKS; blk++)\n s->blocks[blk].end_freq[ch] = s->bandwidth_code * 3 + 73;\n }\n if (s->lfe_on) {\n s->start_freq[s->lfe_channel] = 0;\n for (blk = 0; blk < AC3_MAX_BLOCKS; blk++)\n s->blocks[blk].end_freq[ch] = 7;\n }\n if (s->cpl_enabled) {\n if (s->options.cpl_start >= 0) {\n cpl_start = s->options.cpl_start;\n } else {\n cpl_start = ac3_coupling_start_tab[s->channel_mode-2][s->bit_alloc.sr_code][s->frame_size_code/2];\n if (cpl_start < 0)\n s->cpl_enabled = 0;\n }\n }\n if (s->cpl_enabled) {\n int i, cpl_start_band, cpl_end_band;\n uint8_t *cpl_band_sizes = s->cpl_band_sizes;\n cpl_end_band = s->bandwidth_code / 4 + 3;\n cpl_start_band = av_clip(cpl_start, 0, FFMIN(cpl_end_band-1, 15));\n s->num_cpl_subbands = cpl_end_band - cpl_start_band;\n s->num_cpl_bands = 1;\n *cpl_band_sizes = 12;\n for (i = cpl_start_band + 1; i < cpl_end_band; i++) {\n if (ff_eac3_default_cpl_band_struct[i]) {\n *cpl_band_sizes += 12;\n } else {\n s->num_cpl_bands++;\n cpl_band_sizes++;\n *cpl_band_sizes = 12;\n }\n }\n s->start_freq[CPL_CH] = cpl_start_band * 12 + 37;\n s->cpl_end_freq = cpl_end_band * 12 + 37;\n for (blk = 0; blk < AC3_MAX_BLOCKS; blk++)\n s->blocks[blk].end_freq[CPL_CH] = s->cpl_end_freq;\n }\n}']
|
2,665
| 0
|
https://github.com/openssl/openssl/blob/9f519addc09b2005fa8c6cde36e3267de02577bb/crypto/evp/m_sigver.c/#L206
|
int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig,
size_t siglen)
{
unsigned char md[EVP_MAX_MD_SIZE];
int r = 0;
unsigned int mdlen = 0;
int vctx = 0;
if (ctx->pctx->pmeth->verifyctx)
vctx = 1;
else
vctx = 0;
if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
if (vctx) {
r = ctx->pctx->pmeth->verifyctx(ctx->pctx, sig, siglen, ctx);
} else
r = EVP_DigestFinal_ex(ctx, md, &mdlen);
} else {
EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
if (tmp_ctx == NULL || !EVP_MD_CTX_copy_ex(tmp_ctx, ctx))
return -1;
if (vctx) {
r = tmp_ctx->pctx->pmeth->verifyctx(tmp_ctx->pctx,
sig, siglen, tmp_ctx);
} else
r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);
EVP_MD_CTX_free(tmp_ctx);
}
if (vctx || !r)
return r;
return EVP_PKEY_verify(ctx->pctx, sig, siglen, md, mdlen);
}
|
['int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig,\n size_t siglen)\n{\n unsigned char md[EVP_MAX_MD_SIZE];\n int r = 0;\n unsigned int mdlen = 0;\n int vctx = 0;\n if (ctx->pctx->pmeth->verifyctx)\n vctx = 1;\n else\n vctx = 0;\n if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {\n if (vctx) {\n r = ctx->pctx->pmeth->verifyctx(ctx->pctx, sig, siglen, ctx);\n } else\n r = EVP_DigestFinal_ex(ctx, md, &mdlen);\n } else {\n EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();\n if (tmp_ctx == NULL || !EVP_MD_CTX_copy_ex(tmp_ctx, ctx))\n return -1;\n if (vctx) {\n r = tmp_ctx->pctx->pmeth->verifyctx(tmp_ctx->pctx,\n sig, siglen, tmp_ctx);\n } else\n r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);\n EVP_MD_CTX_free(tmp_ctx);\n }\n if (vctx || !r)\n return r;\n return EVP_PKEY_verify(ctx->pctx, sig, siglen, md, mdlen);\n}', 'EVP_MD_CTX *EVP_MD_CTX_new(void)\n{\n return OPENSSL_zalloc(sizeof(EVP_MD_CTX));\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num <= 0)\n return NULL;\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n osslargused(file); osslargused(line);\n ret = malloc(num);\n#endif\n return ret;\n}', 'int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)\n{\n unsigned char *tmp_buf;\n if ((in == NULL) || (in->digest == NULL)) {\n EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_INPUT_NOT_INITIALIZED);\n return 0;\n }\n#ifndef OPENSSL_NO_ENGINE\n if (in->engine && !ENGINE_init(in->engine)) {\n EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_ENGINE_LIB);\n return 0;\n }\n#endif\n if (out->digest == in->digest) {\n tmp_buf = out->md_data;\n EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);\n } else\n tmp_buf = NULL;\n EVP_MD_CTX_reset(out);\n memcpy(out, in, sizeof(*out));\n out->md_data = NULL;\n out->pctx = NULL;\n if (in->md_data && out->digest->ctx_size) {\n if (tmp_buf)\n out->md_data = tmp_buf;\n else {\n out->md_data = OPENSSL_malloc(out->digest->ctx_size);\n if (out->md_data == NULL) {\n EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n }\n memcpy(out->md_data, in->md_data, out->digest->ctx_size);\n }\n out->update = in->update;\n if (in->pctx) {\n out->pctx = EVP_PKEY_CTX_dup(in->pctx);\n if (!out->pctx) {\n EVP_MD_CTX_reset(out);\n return 0;\n }\n }\n if (out->digest->copy)\n return out->digest->copy(out, in);\n return 1;\n}']
|
2,666
| 0
|
https://github.com/openssl/openssl/blob/8478351737d7edac0f82dd4fc7f2caff994ce93d/crypto/x509/x509_vfy.c/#L2644
|
static int build_chain(X509_STORE_CTX *ctx)
{
struct dane_st *dane = (struct dane_st *)ctx->dane;
int num = sk_X509_num(ctx->chain);
X509 *cert = sk_X509_value(ctx->chain, num - 1);
int ss = cert_self_signed(cert);
STACK_OF(X509) *sktmp = NULL;
unsigned int search;
int may_trusted = 0;
int may_alternate = 0;
int trust = X509_TRUST_UNTRUSTED;
int alt_untrusted = 0;
int depth;
int ok = 0;
int i;
OPENSSL_assert(num == 1 && ctx->num_untrusted == num);
#define S_DOUNTRUSTED (1 << 0)
#define S_DOTRUSTED (1 << 1)
#define S_DOALTERNATE (1 << 2)
search = (ctx->untrusted != NULL) ? S_DOUNTRUSTED : 0;
if (DANETLS_HAS_PKIX(dane) || !DANETLS_HAS_DANE(dane)) {
if (search == 0 || ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST)
search |= S_DOTRUSTED;
else if (!(ctx->param->flags & X509_V_FLAG_NO_ALT_CHAINS))
may_alternate = 1;
may_trusted = 1;
}
if (ctx->untrusted && (sktmp = sk_X509_dup(ctx->untrusted)) == NULL) {
X509err(X509_F_BUILD_CHAIN, ERR_R_MALLOC_FAILURE);
return 0;
}
if (DANETLS_ENABLED(dane) && dane->certs != NULL) {
for (i = 0; i < sk_X509_num(dane->certs); ++i) {
if (!sk_X509_push(sktmp, sk_X509_value(dane->certs, i))) {
sk_X509_free(sktmp);
X509err(X509_F_BUILD_CHAIN, ERR_R_MALLOC_FAILURE);
return 0;
}
}
}
if (ctx->param->depth > INT_MAX/2)
ctx->param->depth = INT_MAX/2;
depth = ctx->param->depth + 1;
while (search != 0) {
X509 *x;
X509 *xtmp = NULL;
if ((search & S_DOTRUSTED) != 0) {
STACK_OF(X509) *hide = ctx->chain;
i = num = sk_X509_num(ctx->chain);
if ((search & S_DOALTERNATE) != 0) {
i = alt_untrusted;
}
x = sk_X509_value(ctx->chain, i-1);
ctx->chain = NULL;
ok = (depth < num) ? 0 : ctx->get_issuer(&xtmp, ctx, x);
ctx->chain = hide;
if (ok < 0) {
trust = X509_TRUST_REJECTED;
search = 0;
continue;
}
if (ok > 0) {
if ((search & S_DOALTERNATE) != 0) {
OPENSSL_assert(num > i && i > 0 && ss == 0);
search &= ~S_DOALTERNATE;
for (; num > i; --num)
X509_free(sk_X509_pop(ctx->chain));
ctx->num_untrusted = num;
if (DANETLS_ENABLED(dane) &&
dane->mdpth >= ctx->num_untrusted) {
dane->mdpth = -1;
X509_free(dane->mcert);
dane->mcert = NULL;
}
if (DANETLS_ENABLED(dane) &&
dane->pdpth >= ctx->num_untrusted)
dane->pdpth = -1;
}
if (ss == 0) {
if (!sk_X509_push(ctx->chain, x = xtmp)) {
X509_free(xtmp);
X509err(X509_F_BUILD_CHAIN, ERR_R_MALLOC_FAILURE);
trust = X509_TRUST_REJECTED;
search = 0;
continue;
}
ss = cert_self_signed(x);
} else if (num == ctx->num_untrusted) {
if (X509_cmp(x, xtmp) != 0) {
X509_free(xtmp);
ok = 0;
} else {
X509_free(x);
ctx->num_untrusted = --num;
(void) sk_X509_set(ctx->chain, num, x = xtmp);
}
}
if (ok) {
OPENSSL_assert(ctx->num_untrusted <= num);
search &= ~S_DOUNTRUSTED;
switch (trust = check_trust(ctx, num)) {
case X509_TRUST_TRUSTED:
case X509_TRUST_REJECTED:
search = 0;
continue;
}
if (ss == 0)
continue;
}
}
if ((search & S_DOUNTRUSTED) == 0) {
if ((search & S_DOALTERNATE) != 0 && --alt_untrusted > 0)
continue;
if (!may_alternate || (search & S_DOALTERNATE) != 0 ||
ctx->num_untrusted < 2)
break;
search |= S_DOALTERNATE;
alt_untrusted = ctx->num_untrusted - 1;
ss = 0;
}
}
if ((search & S_DOUNTRUSTED) != 0) {
num = sk_X509_num(ctx->chain);
OPENSSL_assert(num == ctx->num_untrusted);
x = sk_X509_value(ctx->chain, num-1);
xtmp = (depth < num) ? NULL : find_issuer(ctx, sktmp, x);
if (xtmp == NULL) {
search &= ~S_DOUNTRUSTED;
if (may_trusted)
search |= S_DOTRUSTED;
continue;
}
if (!sk_X509_push(ctx->chain, x = xtmp)) {
X509err(X509_F_BUILD_CHAIN, ERR_R_MALLOC_FAILURE);
trust = X509_TRUST_REJECTED;
search = 0;
continue;
}
X509_up_ref(x);
++ctx->num_untrusted;
ss = cert_self_signed(xtmp);
(void) sk_X509_delete_ptr(sktmp, x);
switch (trust = check_dane_issuer(ctx, ctx->num_untrusted - 1)) {
case X509_TRUST_TRUSTED:
case X509_TRUST_REJECTED:
search = 0;
continue;
}
}
}
sk_X509_free(sktmp);
num = sk_X509_num(ctx->chain);
if (num <= depth) {
if (trust == X509_TRUST_UNTRUSTED && DANETLS_HAS_DANE_TA(dane))
trust = check_dane_pkeys(ctx);
if (trust == X509_TRUST_UNTRUSTED && num == ctx->num_untrusted)
trust = check_trust(ctx, num);
}
switch (trust) {
case X509_TRUST_TRUSTED:
return 1;
case X509_TRUST_REJECTED:
return 0;
case X509_TRUST_UNTRUSTED:
default:
num = sk_X509_num(ctx->chain);
ctx->current_cert = sk_X509_value(ctx->chain, num - 1);
ctx->error_depth = num-1;
if (num > depth)
ctx->error = X509_V_ERR_CERT_CHAIN_TOO_LONG;
else if (DANETLS_ENABLED(dane) &&
(!DANETLS_HAS_PKIX(dane) || dane->pdpth >= 0))
ctx->error = X509_V_ERR_CERT_UNTRUSTED;
else if (ss && sk_X509_num(ctx->chain) == 1)
ctx->error = X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;
else if (ss)
ctx->error = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
else if (ctx->num_untrusted == num)
ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;
else
ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT;
if (DANETLS_ENABLED(dane))
dane_reset(dane);
return ctx->verify_cb(0, ctx);
}
}
|
['static int build_chain(X509_STORE_CTX *ctx)\n{\n struct dane_st *dane = (struct dane_st *)ctx->dane;\n int num = sk_X509_num(ctx->chain);\n X509 *cert = sk_X509_value(ctx->chain, num - 1);\n int ss = cert_self_signed(cert);\n STACK_OF(X509) *sktmp = NULL;\n unsigned int search;\n int may_trusted = 0;\n int may_alternate = 0;\n int trust = X509_TRUST_UNTRUSTED;\n int alt_untrusted = 0;\n int depth;\n int ok = 0;\n int i;\n OPENSSL_assert(num == 1 && ctx->num_untrusted == num);\n#define S_DOUNTRUSTED (1 << 0)\n#define S_DOTRUSTED (1 << 1)\n#define S_DOALTERNATE (1 << 2)\n search = (ctx->untrusted != NULL) ? S_DOUNTRUSTED : 0;\n if (DANETLS_HAS_PKIX(dane) || !DANETLS_HAS_DANE(dane)) {\n if (search == 0 || ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST)\n search |= S_DOTRUSTED;\n else if (!(ctx->param->flags & X509_V_FLAG_NO_ALT_CHAINS))\n may_alternate = 1;\n may_trusted = 1;\n }\n if (ctx->untrusted && (sktmp = sk_X509_dup(ctx->untrusted)) == NULL) {\n X509err(X509_F_BUILD_CHAIN, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n if (DANETLS_ENABLED(dane) && dane->certs != NULL) {\n for (i = 0; i < sk_X509_num(dane->certs); ++i) {\n if (!sk_X509_push(sktmp, sk_X509_value(dane->certs, i))) {\n sk_X509_free(sktmp);\n X509err(X509_F_BUILD_CHAIN, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n }\n }\n if (ctx->param->depth > INT_MAX/2)\n ctx->param->depth = INT_MAX/2;\n depth = ctx->param->depth + 1;\n while (search != 0) {\n X509 *x;\n X509 *xtmp = NULL;\n if ((search & S_DOTRUSTED) != 0) {\n STACK_OF(X509) *hide = ctx->chain;\n i = num = sk_X509_num(ctx->chain);\n if ((search & S_DOALTERNATE) != 0) {\n i = alt_untrusted;\n }\n x = sk_X509_value(ctx->chain, i-1);\n ctx->chain = NULL;\n ok = (depth < num) ? 0 : ctx->get_issuer(&xtmp, ctx, x);\n ctx->chain = hide;\n if (ok < 0) {\n trust = X509_TRUST_REJECTED;\n search = 0;\n continue;\n }\n if (ok > 0) {\n if ((search & S_DOALTERNATE) != 0) {\n OPENSSL_assert(num > i && i > 0 && ss == 0);\n search &= ~S_DOALTERNATE;\n for (; num > i; --num)\n X509_free(sk_X509_pop(ctx->chain));\n ctx->num_untrusted = num;\n if (DANETLS_ENABLED(dane) &&\n dane->mdpth >= ctx->num_untrusted) {\n dane->mdpth = -1;\n X509_free(dane->mcert);\n dane->mcert = NULL;\n }\n if (DANETLS_ENABLED(dane) &&\n dane->pdpth >= ctx->num_untrusted)\n dane->pdpth = -1;\n }\n if (ss == 0) {\n if (!sk_X509_push(ctx->chain, x = xtmp)) {\n X509_free(xtmp);\n X509err(X509_F_BUILD_CHAIN, ERR_R_MALLOC_FAILURE);\n trust = X509_TRUST_REJECTED;\n search = 0;\n continue;\n }\n ss = cert_self_signed(x);\n } else if (num == ctx->num_untrusted) {\n if (X509_cmp(x, xtmp) != 0) {\n X509_free(xtmp);\n ok = 0;\n } else {\n X509_free(x);\n ctx->num_untrusted = --num;\n (void) sk_X509_set(ctx->chain, num, x = xtmp);\n }\n }\n if (ok) {\n OPENSSL_assert(ctx->num_untrusted <= num);\n search &= ~S_DOUNTRUSTED;\n switch (trust = check_trust(ctx, num)) {\n case X509_TRUST_TRUSTED:\n case X509_TRUST_REJECTED:\n search = 0;\n continue;\n }\n if (ss == 0)\n continue;\n }\n }\n if ((search & S_DOUNTRUSTED) == 0) {\n if ((search & S_DOALTERNATE) != 0 && --alt_untrusted > 0)\n continue;\n if (!may_alternate || (search & S_DOALTERNATE) != 0 ||\n ctx->num_untrusted < 2)\n break;\n search |= S_DOALTERNATE;\n alt_untrusted = ctx->num_untrusted - 1;\n ss = 0;\n }\n }\n if ((search & S_DOUNTRUSTED) != 0) {\n num = sk_X509_num(ctx->chain);\n OPENSSL_assert(num == ctx->num_untrusted);\n x = sk_X509_value(ctx->chain, num-1);\n xtmp = (depth < num) ? NULL : find_issuer(ctx, sktmp, x);\n if (xtmp == NULL) {\n search &= ~S_DOUNTRUSTED;\n if (may_trusted)\n search |= S_DOTRUSTED;\n continue;\n }\n if (!sk_X509_push(ctx->chain, x = xtmp)) {\n X509err(X509_F_BUILD_CHAIN, ERR_R_MALLOC_FAILURE);\n trust = X509_TRUST_REJECTED;\n search = 0;\n continue;\n }\n X509_up_ref(x);\n ++ctx->num_untrusted;\n ss = cert_self_signed(xtmp);\n (void) sk_X509_delete_ptr(sktmp, x);\n switch (trust = check_dane_issuer(ctx, ctx->num_untrusted - 1)) {\n case X509_TRUST_TRUSTED:\n case X509_TRUST_REJECTED:\n search = 0;\n continue;\n }\n }\n }\n sk_X509_free(sktmp);\n num = sk_X509_num(ctx->chain);\n if (num <= depth) {\n if (trust == X509_TRUST_UNTRUSTED && DANETLS_HAS_DANE_TA(dane))\n trust = check_dane_pkeys(ctx);\n if (trust == X509_TRUST_UNTRUSTED && num == ctx->num_untrusted)\n trust = check_trust(ctx, num);\n }\n switch (trust) {\n case X509_TRUST_TRUSTED:\n return 1;\n case X509_TRUST_REJECTED:\n return 0;\n case X509_TRUST_UNTRUSTED:\n default:\n num = sk_X509_num(ctx->chain);\n ctx->current_cert = sk_X509_value(ctx->chain, num - 1);\n ctx->error_depth = num-1;\n if (num > depth)\n ctx->error = X509_V_ERR_CERT_CHAIN_TOO_LONG;\n else if (DANETLS_ENABLED(dane) &&\n (!DANETLS_HAS_PKIX(dane) || dane->pdpth >= 0))\n ctx->error = X509_V_ERR_CERT_UNTRUSTED;\n else if (ss && sk_X509_num(ctx->chain) == 1)\n ctx->error = X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;\n else if (ss)\n ctx->error = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;\n else if (ctx->num_untrusted == num)\n ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;\n else\n ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT;\n if (DANETLS_ENABLED(dane))\n dane_reset(dane);\n return ctx->verify_cb(0, ctx);\n }\n}', 'DEFINE_STACK_OF(X509)', 'int sk_num(const _STACK *st)\n{\n if (st == NULL)\n return -1;\n return st->num;\n}', 'void *sk_value(const _STACK *st, int i)\n{\n if (!st || (i < 0) || (i >= st->num))\n return NULL;\n return st->data[i];\n}', 'static int cert_self_signed(X509 *x)\n{\n X509_check_purpose(x, -1, 0);\n if (x->ex_flags & EXFLAG_SS)\n return 1;\n else\n return 0;\n}']
|
2,667
| 0
|
https://gitlab.com/libtiff/libtiff/blob/86f9adb7fd7623c3708c3754c4cdc9a593b7f079/libtiff/tif_swab.c/#L296
|
void
TIFFReverseBits(uint8* cp, tmsize_t n)
{
for (; n > 8; n -= 8) {
cp[0] = TIFFBitRevTable[cp[0]];
cp[1] = TIFFBitRevTable[cp[1]];
cp[2] = TIFFBitRevTable[cp[2]];
cp[3] = TIFFBitRevTable[cp[3]];
cp[4] = TIFFBitRevTable[cp[4]];
cp[5] = TIFFBitRevTable[cp[5]];
cp[6] = TIFFBitRevTable[cp[6]];
cp[7] = TIFFBitRevTable[cp[7]];
cp += 8;
}
while (n-- > 0)
*cp = TIFFBitRevTable[*cp], cp++;
}
|
['static void\nquant_fsdither(TIFF* in, TIFF* out)\n{\n\tunsigned char *outline, *inputline, *inptr;\n\tshort *thisline, *nextline;\n\tregister unsigned char\t*outptr;\n\tregister short *thisptr, *nextptr;\n\tregister uint32 i, j;\n\tuint32 imax, jmax;\n\tint lastline, lastpixel;\n\timax = imagelength - 1;\n\tjmax = imagewidth - 1;\n\tinputline = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(in));\n\tthisline = (short *)_TIFFmalloc(imagewidth * 3 * sizeof (short));\n\tnextline = (short *)_TIFFmalloc(imagewidth * 3 * sizeof (short));\n\toutline = (unsigned char *) _TIFFmalloc(TIFFScanlineSize(out));\n\tGetInputLine(in, 0, goto bad);\n\tfor (i = 1; i <= imagelength; ++i) {\n\t\tSWAP(short *, thisline, nextline);\n\t\tlastline = (i >= imax);\n\t\tif (i <= imax)\n\t\t\tGetInputLine(in, i, break);\n\t\tthisptr = thisline;\n\t\tnextptr = nextline;\n\t\toutptr = outline;\n\t\tfor (j = 0; j < imagewidth; ++j) {\n\t\t\tint red, green, blue;\n\t\t\tregister int oval, r2, g2, b2;\n\t\t\tlastpixel = (j == jmax);\n\t\t\tGetComponent(*thisptr++, r2, red);\n\t\t\tGetComponent(*thisptr++, g2, green);\n\t\t\tGetComponent(*thisptr++, b2, blue);\n\t\t\toval = histogram[r2][g2][b2];\n\t\t\tif (oval == -1) {\n\t\t\t\tint ci;\n\t\t\t\tregister int cj, tmp, d2, dist;\n\t\t\t\tregister C_cell\t*cell;\n\t\t\t\tcell = *(ColorCells +\n\t\t\t\t (((r2>>(B_DEPTH-C_DEPTH)) << C_DEPTH*2) +\n\t\t\t\t ((g2>>(B_DEPTH-C_DEPTH)) << C_DEPTH ) +\n\t\t\t\t (b2>>(B_DEPTH-C_DEPTH))));\n\t\t\t\tif (cell == NULL)\n\t\t\t\t\tcell = create_colorcell(red,\n\t\t\t\t\t green, blue);\n\t\t\t\tdist = 9999999;\n\t\t\t\tfor (ci = 0; ci < cell->num_ents && dist > cell->entries[ci][1]; ++ci) {\n\t\t\t\t\tcj = cell->entries[ci][0];\n\t\t\t\t\td2 = (rm[cj] >> COLOR_SHIFT) - r2;\n\t\t\t\t\td2 *= d2;\n\t\t\t\t\ttmp = (gm[cj] >> COLOR_SHIFT) - g2;\n\t\t\t\t\td2 += tmp*tmp;\n\t\t\t\t\ttmp = (bm[cj] >> COLOR_SHIFT) - b2;\n\t\t\t\t\td2 += tmp*tmp;\n\t\t\t\t\tif (d2 < dist) {\n\t\t\t\t\t\tdist = d2;\n\t\t\t\t\t\toval = cj;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\thistogram[r2][g2][b2] = oval;\n\t\t\t}\n\t\t\t*outptr++ = oval;\n\t\t\tred -= rm[oval];\n\t\t\tgreen -= gm[oval];\n\t\t\tblue -= bm[oval];\n\t\t\tif (!lastpixel) {\n\t\t\t\tthisptr[0] += blue * 7 / 16;\n\t\t\t\tthisptr[1] += green * 7 / 16;\n\t\t\t\tthisptr[2] += red * 7 / 16;\n\t\t\t}\n\t\t\tif (!lastline) {\n\t\t\t\tif (j != 0) {\n\t\t\t\t\tnextptr[-3] += blue * 3 / 16;\n\t\t\t\t\tnextptr[-2] += green * 3 / 16;\n\t\t\t\t\tnextptr[-1] += red * 3 / 16;\n\t\t\t\t}\n\t\t\t\tnextptr[0] += blue * 5 / 16;\n\t\t\t\tnextptr[1] += green * 5 / 16;\n\t\t\t\tnextptr[2] += red * 5 / 16;\n\t\t\t\tif (!lastpixel) {\n\t\t\t\t\tnextptr[3] += blue / 16;\n\t\t\t\t nextptr[4] += green / 16;\n\t\t\t\t nextptr[5] += red / 16;\n\t\t\t\t}\n\t\t\t\tnextptr += 3;\n\t\t\t}\n\t\t}\n\t\tif (TIFFWriteScanline(out, outline, i-1, 0) < 0)\n\t\t\tbreak;\n\t}\nbad:\n\t_TIFFfree(inputline);\n\t_TIFFfree(thisline);\n\t_TIFFfree(nextline);\n\t_TIFFfree(outline);\n}', 'int\nTIFFWriteScanline(TIFF* tif, void* buf, uint32 row, uint16 sample)\n{\n\tstatic const char module[] = "TIFFWriteScanline";\n\tregister TIFFDirectory *td;\n\tint status, imagegrew = 0;\n\tuint32 strip;\n\tif (!WRITECHECKSTRIPS(tif, module))\n\t\treturn (-1);\n\tif (!BUFFERCHECK(tif))\n\t\treturn (-1);\n tif->tif_flags |= TIFF_BUF4WRITE;\n\ttd = &tif->tif_dir;\n\tif (row >= td->td_imagelength) {\n\t\tif (td->td_planarconfig == PLANARCONFIG_SEPARATE) {\n\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t "Can not change \\"ImageLength\\" when using separate planes");\n\t\t\treturn (-1);\n\t\t}\n\t\ttd->td_imagelength = row+1;\n\t\timagegrew = 1;\n\t}\n\tif (td->td_planarconfig == PLANARCONFIG_SEPARATE) {\n\t\tif (sample >= td->td_samplesperpixel) {\n\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t "%lu: Sample out of range, max %lu",\n\t\t\t (unsigned long) sample, (unsigned long) td->td_samplesperpixel);\n\t\t\treturn (-1);\n\t\t}\n\t\tstrip = sample*td->td_stripsperimage + row/td->td_rowsperstrip;\n\t} else\n\t\tstrip = row / td->td_rowsperstrip;\n\tif (strip >= td->td_nstrips && !TIFFGrowStrips(tif, 1, module))\n\t\treturn (-1);\n\tif (strip != tif->tif_curstrip) {\n\t\tif (!TIFFFlushData(tif))\n\t\t\treturn (-1);\n\t\ttif->tif_curstrip = strip;\n\t\tif (strip >= td->td_stripsperimage && imagegrew)\n\t\t\ttd->td_stripsperimage =\n\t\t\t TIFFhowmany_32(td->td_imagelength,td->td_rowsperstrip);\n\t\ttif->tif_row =\n\t\t (strip % td->td_stripsperimage) * td->td_rowsperstrip;\n\t\tif ((tif->tif_flags & TIFF_CODERSETUP) == 0) {\n\t\t\tif (!(*tif->tif_setupencode)(tif))\n\t\t\t\treturn (-1);\n\t\t\ttif->tif_flags |= TIFF_CODERSETUP;\n\t\t}\n\t\ttif->tif_rawcc = 0;\n\t\ttif->tif_rawcp = tif->tif_rawdata;\n\t\tif( td->td_stripbytecount[strip] > 0 )\n\t\t{\n\t\t\ttd->td_stripbytecount[strip] = 0;\n\t\t\ttif->tif_curoff = 0;\n\t\t}\n\t\tif (!(*tif->tif_preencode)(tif, sample))\n\t\t\treturn (-1);\n\t\ttif->tif_flags |= TIFF_POSTENCODE;\n\t}\n\tif (row != tif->tif_row) {\n\t\tif (row < tif->tif_row) {\n\t\t\ttif->tif_row = (strip % td->td_stripsperimage) *\n\t\t\t td->td_rowsperstrip;\n\t\t\ttif->tif_rawcp = tif->tif_rawdata;\n\t\t}\n\t\tif (!(*tif->tif_seek)(tif, row - tif->tif_row))\n\t\t\treturn (-1);\n\t\ttif->tif_row = row;\n\t}\n\ttif->tif_postdecode( tif, (uint8*) buf, tif->tif_scanlinesize );\n\tstatus = (*tif->tif_encoderow)(tif, (uint8*) buf,\n\t tif->tif_scanlinesize, sample);\n\ttif->tif_row = row + 1;\n\treturn (status);\n}', 'int\nTIFFFlushData(TIFF* tif)\n{\n\tif ((tif->tif_flags & TIFF_BEENWRITING) == 0)\n\t\treturn (1);\n\tif (tif->tif_flags & TIFF_POSTENCODE) {\n\t\ttif->tif_flags &= ~TIFF_POSTENCODE;\n\t\tif (!(*tif->tif_postencode)(tif))\n\t\t\treturn (0);\n\t}\n\treturn (TIFFFlushData1(tif));\n}', 'int\nTIFFFlushData1(TIFF* tif)\n{\n\tif (tif->tif_rawcc > 0 && tif->tif_flags & TIFF_BUF4WRITE ) {\n\t\tif (!isFillOrder(tif, tif->tif_dir.td_fillorder) &&\n\t\t (tif->tif_flags & TIFF_NOBITREV) == 0)\n\t\t\tTIFFReverseBits((uint8*)tif->tif_rawdata,\n\t\t\t tif->tif_rawcc);\n\t\tif (!TIFFAppendToStrip(tif,\n\t\t isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip,\n\t\t tif->tif_rawdata, tif->tif_rawcc))\n\t\t\treturn (0);\n\t\ttif->tif_rawcc = 0;\n\t\ttif->tif_rawcp = tif->tif_rawdata;\n\t}\n\treturn (1);\n}', 'void\nTIFFReverseBits(uint8* cp, tmsize_t n)\n{\n\tfor (; n > 8; n -= 8) {\n\t\tcp[0] = TIFFBitRevTable[cp[0]];\n\t\tcp[1] = TIFFBitRevTable[cp[1]];\n\t\tcp[2] = TIFFBitRevTable[cp[2]];\n\t\tcp[3] = TIFFBitRevTable[cp[3]];\n\t\tcp[4] = TIFFBitRevTable[cp[4]];\n\t\tcp[5] = TIFFBitRevTable[cp[5]];\n\t\tcp[6] = TIFFBitRevTable[cp[6]];\n\t\tcp[7] = TIFFBitRevTable[cp[7]];\n\t\tcp += 8;\n\t}\n\twhile (n-- > 0)\n\t\t*cp = TIFFBitRevTable[*cp], cp++;\n}']
|
2,668
| 0
|
https://github.com/openssl/openssl/blob/33af4421f2ae5e4d0da3a121f51820f4b49a724c/engines/e_4758cca.c/#L548
|
static EVP_PKEY *ibm_4758_load_pubkey(ENGINE* e, const char* key_id,
UI_METHOD *ui_method, void *callback_data)
{
RSA *rtmp = NULL;
EVP_PKEY *res = NULL;
unsigned char* keyToken = NULL;
long keyTokenLength = MAX_CCA_PKA_TOKEN_SIZE;
long returnCode;
long reasonCode;
long exitDataLength = 0;
long ruleArrayLength = 0;
unsigned char exitData[8];
unsigned char ruleArray[8];
unsigned char keyLabel[64];
unsigned long keyLabelLength = strlen(key_id);
unsigned char modulus[512];
long modulusFieldLength = sizeof(modulus);
long modulusLength = 0;
unsigned char exponent[512];
long exponentLength = sizeof(exponent);
if (keyLabelLength > sizeof(keyLabel))
{
CCA4758err(CCA4758_F_IBM_4758_LOAD_PUBKEY,
CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
return NULL;
}
memset(keyLabel,' ', sizeof(keyLabel));
memcpy(keyLabel, key_id, keyLabelLength);
keyToken = OPENSSL_malloc(MAX_CCA_PKA_TOKEN_SIZE + sizeof(long));
if (!keyToken)
{
CCA4758err(CCA4758_F_IBM_4758_LOAD_PUBKEY,
ERR_R_MALLOC_FAILURE);
goto err;
}
keyRecordRead(&returnCode, &reasonCode, &exitDataLength, exitData,
&ruleArrayLength, ruleArray, keyLabel, &keyTokenLength,
keyToken+sizeof(long));
if (returnCode)
{
CCA4758err(CCA4758_F_IBM_4758_LOAD_PUBKEY,
ERR_R_MALLOC_FAILURE);
goto err;
}
if (!getModulusAndExponent(keyToken+sizeof(long), &exponentLength,
exponent, &modulusLength, &modulusFieldLength, modulus))
{
CCA4758err(CCA4758_F_IBM_4758_LOAD_PUBKEY,
CCA4758_R_FAILED_LOADING_PUBLIC_KEY);
goto err;
}
(*(long*)keyToken) = keyTokenLength;
rtmp = RSA_new_method(e);
RSA_set_ex_data(rtmp, hndidx, (char *)keyToken);
rtmp->e = BN_bin2bn(exponent, exponentLength, NULL);
rtmp->n = BN_bin2bn(modulus, modulusFieldLength, NULL);
rtmp->flags |= RSA_FLAG_EXT_PKEY;
res = EVP_PKEY_new();
EVP_PKEY_assign_RSA(res, rtmp);
return res;
err:
if (keyToken)
OPENSSL_free(keyToken);
return NULL;
}
|
["static EVP_PKEY *ibm_4758_load_pubkey(ENGINE* e, const char* key_id,\n\t\t\tUI_METHOD *ui_method, void *callback_data)\n\t{\n\tRSA *rtmp = NULL;\n\tEVP_PKEY *res = NULL;\n\tunsigned char* keyToken = NULL;\n\tlong keyTokenLength = MAX_CCA_PKA_TOKEN_SIZE;\n\tlong returnCode;\n\tlong reasonCode;\n\tlong exitDataLength = 0;\n\tlong ruleArrayLength = 0;\n\tunsigned char exitData[8];\n\tunsigned char ruleArray[8];\n\tunsigned char keyLabel[64];\n\tunsigned long keyLabelLength = strlen(key_id);\n\tunsigned char modulus[512];\n\tlong modulusFieldLength = sizeof(modulus);\n\tlong modulusLength = 0;\n\tunsigned char exponent[512];\n\tlong exponentLength = sizeof(exponent);\n\tif (keyLabelLength > sizeof(keyLabel))\n\t\t{\n\t\tCCA4758err(CCA4758_F_IBM_4758_LOAD_PUBKEY,\n\t\t\tCCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);\n\t\treturn NULL;\n\t\t}\n\tmemset(keyLabel,' ', sizeof(keyLabel));\n\tmemcpy(keyLabel, key_id, keyLabelLength);\n\tkeyToken = OPENSSL_malloc(MAX_CCA_PKA_TOKEN_SIZE + sizeof(long));\n\tif (!keyToken)\n\t\t{\n\t\tCCA4758err(CCA4758_F_IBM_4758_LOAD_PUBKEY,\n\t\t\t\tERR_R_MALLOC_FAILURE);\n\t\tgoto err;\n\t\t}\n\tkeyRecordRead(&returnCode, &reasonCode, &exitDataLength, exitData,\n\t\t&ruleArrayLength, ruleArray, keyLabel, &keyTokenLength,\n\t\tkeyToken+sizeof(long));\n\tif (returnCode)\n\t\t{\n\t\tCCA4758err(CCA4758_F_IBM_4758_LOAD_PUBKEY,\n\t\t\t\tERR_R_MALLOC_FAILURE);\n\t\tgoto err;\n\t\t}\n\tif (!getModulusAndExponent(keyToken+sizeof(long), &exponentLength,\n\t\t\texponent, &modulusLength, &modulusFieldLength, modulus))\n\t\t{\n\t\tCCA4758err(CCA4758_F_IBM_4758_LOAD_PUBKEY,\n\t\t\tCCA4758_R_FAILED_LOADING_PUBLIC_KEY);\n\t\tgoto err;\n\t\t}\n\t(*(long*)keyToken) = keyTokenLength;\n\trtmp = RSA_new_method(e);\n\tRSA_set_ex_data(rtmp, hndidx, (char *)keyToken);\n\trtmp->e = BN_bin2bn(exponent, exponentLength, NULL);\n\trtmp->n = BN_bin2bn(modulus, modulusFieldLength, NULL);\n\trtmp->flags |= RSA_FLAG_EXT_PKEY;\n\tres = EVP_PKEY_new();\n\tEVP_PKEY_assign_RSA(res, rtmp);\n\treturn res;\nerr:\n\tif (keyToken)\n\t\tOPENSSL_free(keyToken);\n\treturn NULL;\n\t}", 'void *CRYPTO_malloc(int num, const char *file, int line)\n\t{\n\tvoid *ret = NULL;\n\textern unsigned char cleanse_ctr;\n\tif (num <= 0) return NULL;\n\tallow_customize = 0;\n\tif (malloc_debug_func != NULL)\n\t\t{\n\t\tallow_customize_debug = 0;\n\t\tmalloc_debug_func(NULL, num, file, line, 0);\n\t\t}\n\tret = malloc_ex_func(num,file,line);\n#ifdef LEVITTE_DEBUG_MEM\n\tfprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\\n", ret, num);\n#endif\n\tif (malloc_debug_func != NULL)\n\t\tmalloc_debug_func(ret, num, file, line, 1);\n if(ret && (num > 2048))\n ((unsigned char *)ret)[0] = cleanse_ctr;\n\treturn ret;\n\t}', 'static int getModulusAndExponent(const unsigned char*token, long *exponentLength,\n\t\tunsigned char *exponent, long *modulusLength, long *modulusFieldLength,\n\t\tunsigned char *modulus)\n\t{\n\tunsigned long len;\n\tif (*token++ != (char)0x1E)\n\t\treturn 0;\n\tif (*token++)\n\t\treturn 0;\n\tlen = *token++;\n\tlen = len << 8;\n\tlen |= (unsigned char)*token++;\n\ttoken += 4;\n\tif (*token++ == (char)0x04)\n\t\t{\n\t\tif (*token++)\n\t\t\treturn 0;\n\t\tlen = *token++;\n\t\tlen = len << 8;\n\t\tlen |= (unsigned char)*token++;\n\t\ttoken+=2;\n\t\tlen = *token++;\n\t\tlen = len << 8;\n\t\tlen |= (unsigned char)*token++;\n\t\t*exponentLength = len;\n\t\tlen = *token++;\n\t\tlen = len << 8;\n\t\tlen |= (unsigned char)*token++;\n\t\t*modulusLength = len;\n\t\tlen = *token++;\n\t\tlen = len << 8;\n\t\tlen |= (unsigned char)*token++;\n\t\t*modulusFieldLength = len;\n\t\tmemcpy(exponent, token, *exponentLength);\n\t\ttoken+= *exponentLength;\n\t\tmemcpy(modulus, token, *modulusFieldLength);\n\t\treturn 1;\n\t\t}\n\treturn 0;\n\t}', 'RSA *RSA_new_method(ENGINE *engine)\n\t{\n\tRSA *ret;\n\tret=(RSA *)OPENSSL_malloc(sizeof(RSA));\n\tif (ret == NULL)\n\t\t{\n\t\tRSAerr(RSA_F_RSA_NEW_METHOD,ERR_R_MALLOC_FAILURE);\n\t\treturn NULL;\n\t\t}\n\tret->meth = RSA_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\tRSAerr(RSA_F_RSA_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_RSA();\n\tif(ret->engine)\n\t\t{\n\t\tret->meth = ENGINE_get_RSA(ret->engine);\n\t\tif(!ret->meth)\n\t\t\t{\n\t\t\tRSAerr(RSA_F_RSA_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->n=NULL;\n\tret->e=NULL;\n\tret->d=NULL;\n\tret->p=NULL;\n\tret->q=NULL;\n\tret->dmp1=NULL;\n\tret->dmq1=NULL;\n\tret->iqmp=NULL;\n\tret->references=1;\n\tret->_method_mod_n=NULL;\n\tret->_method_mod_p=NULL;\n\tret->_method_mod_q=NULL;\n\tret->blinding=NULL;\n\tret->mt_blinding=NULL;\n\tret->bignum_data=NULL;\n\tret->flags=ret->meth->flags;\n\tCRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, 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_RSA, ret, &ret->ex_data);\n\t\tOPENSSL_free(ret);\n\t\tret=NULL;\n\t\t}\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}']
|
2,669
| 0
|
https://github.com/openssl/openssl/blob/ec04e866343d40a1e3e8e5db79557e279a2dd0d8/crypto/evp/pmeth_gn.c/#L113
|
int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
{
int ret;
if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) {
EVPerr(EVP_F_EVP_PKEY_PARAMGEN,
EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
return -2;
}
if (ctx->operation != EVP_PKEY_OP_PARAMGEN) {
EVPerr(EVP_F_EVP_PKEY_PARAMGEN, EVP_R_OPERATON_NOT_INITIALIZED);
return -1;
}
if (ppkey == NULL)
return -1;
if (*ppkey == NULL)
*ppkey = EVP_PKEY_new();
if (*ppkey == NULL) {
EVPerr(EVP_F_EVP_PKEY_PARAMGEN, ERR_R_MALLOC_FAILURE);
return -1;
}
ret = ctx->pmeth->paramgen(ctx, *ppkey);
if (ret <= 0) {
EVP_PKEY_free(*ppkey);
*ppkey = NULL;
}
return ret;
}
|
['int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)\n{\n int ret;\n if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) {\n EVPerr(EVP_F_EVP_PKEY_PARAMGEN,\n EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);\n return -2;\n }\n if (ctx->operation != EVP_PKEY_OP_PARAMGEN) {\n EVPerr(EVP_F_EVP_PKEY_PARAMGEN, EVP_R_OPERATON_NOT_INITIALIZED);\n return -1;\n }\n if (ppkey == NULL)\n return -1;\n if (*ppkey == NULL)\n *ppkey = EVP_PKEY_new();\n if (*ppkey == NULL) {\n EVPerr(EVP_F_EVP_PKEY_PARAMGEN, ERR_R_MALLOC_FAILURE);\n return -1;\n }\n ret = ctx->pmeth->paramgen(ctx, *ppkey);\n if (ret <= 0) {\n EVP_PKEY_free(*ppkey);\n *ppkey = NULL;\n }\n return ret;\n}', 'EVP_PKEY *EVP_PKEY_new(void)\n{\n EVP_PKEY *ret;\n ret = OPENSSL_malloc(sizeof(*ret));\n if (ret == NULL) {\n EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n ret->type = EVP_PKEY_NONE;\n ret->save_type = EVP_PKEY_NONE;\n ret->references = 1;\n ret->ameth = NULL;\n ret->engine = NULL;\n ret->pkey.ptr = NULL;\n ret->attributes = NULL;\n ret->save_parameters = 1;\n return (ret);\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (num <= 0)\n return NULL;\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)file;\n (void)line;\n ret = malloc(num);\n#endif\n#ifndef OPENSSL_CPUID_OBJ\n if (ret && (num > 2048)) {\n extern unsigned char cleanse_ctr;\n ((unsigned char *)ret)[0] = cleanse_ctr;\n }\n#endif\n return ret;\n}', 'void EVP_PKEY_free(EVP_PKEY *x)\n{\n int i;\n if (x == NULL)\n return;\n i = CRYPTO_add(&x->references, -1, CRYPTO_LOCK_EVP_PKEY);\n#ifdef REF_PRINT\n REF_PRINT("EVP_PKEY", x);\n#endif\n if (i > 0)\n return;\n#ifdef REF_CHECK\n if (i < 0) {\n fprintf(stderr, "EVP_PKEY_free, bad reference count\\n");\n abort();\n }\n#endif\n EVP_PKEY_free_it(x);\n sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);\n OPENSSL_free(x);\n}', 'int CRYPTO_add_lock(int *pointer, int amount, int type, const char *file,\n int line)\n{\n int ret = 0;\n if (add_lock_callback != NULL) {\n#ifdef LOCK_DEBUG\n int before = *pointer;\n#endif\n ret = add_lock_callback(pointer, amount, type, file, line);\n#ifdef LOCK_DEBUG\n {\n CRYPTO_THREADID id;\n CRYPTO_THREADID_current(&id);\n fprintf(stderr, "ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\\n",\n CRYPTO_THREADID_hash(&id), before, amount, ret,\n CRYPTO_get_lock_name(type), file, line);\n }\n#endif\n } else {\n CRYPTO_lock(CRYPTO_LOCK | CRYPTO_WRITE, type, file, line);\n ret = *pointer + amount;\n#ifdef LOCK_DEBUG\n {\n CRYPTO_THREADID id;\n CRYPTO_THREADID_current(&id);\n fprintf(stderr, "ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\\n",\n CRYPTO_THREADID_hash(&id),\n *pointer, amount, ret,\n CRYPTO_get_lock_name(type), file, line);\n }\n#endif\n *pointer = ret;\n CRYPTO_lock(CRYPTO_UNLOCK | CRYPTO_WRITE, type, file, line);\n }\n return (ret);\n}', 'void CRYPTO_lock(int mode, int type, const char *file, int line)\n{\n#ifdef LOCK_DEBUG\n {\n CRYPTO_THREADID id;\n char *rw_text, *operation_text;\n if (mode & CRYPTO_LOCK)\n operation_text = "lock ";\n else if (mode & CRYPTO_UNLOCK)\n operation_text = "unlock";\n else\n operation_text = "ERROR ";\n if (mode & CRYPTO_READ)\n rw_text = "r";\n else if (mode & CRYPTO_WRITE)\n rw_text = "w";\n else\n rw_text = "ERROR";\n CRYPTO_THREADID_current(&id);\n fprintf(stderr, "lock:%08lx:(%s)%s %-18s %s:%d\\n",\n CRYPTO_THREADID_hash(&id), rw_text, operation_text,\n CRYPTO_get_lock_name(type), file, line);\n }\n#endif\n if (type < 0) {\n if (dynlock_lock_callback != NULL) {\n struct CRYPTO_dynlock_value *pointer\n = CRYPTO_get_dynlock_value(type);\n OPENSSL_assert(pointer != NULL);\n dynlock_lock_callback(mode, pointer, file, line);\n CRYPTO_destroy_dynlockid(type);\n }\n } else if (locking_callback != NULL)\n locking_callback(mode, type, file, line);\n}']
|
2,670
| 0
|
https://github.com/openssl/openssl/blob/babb379849ffb4112792f266f92e9ebb2bd35332/crypto/pkcs7/pk7_doit.c/#L1112
|
PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx)
{
STACK_OF(PKCS7_RECIP_INFO) *rsk;
PKCS7_RECIP_INFO *ri;
int i;
i=OBJ_obj2nid(p7->type);
if (i != NID_pkcs7_signedAndEnveloped)
return NULL;
if (p7->d.signed_and_enveloped == NULL)
return NULL;
rsk=p7->d.signed_and_enveloped->recipientinfo;
if (rsk == NULL)
return NULL;
ri=sk_PKCS7_RECIP_INFO_value(rsk,0);
if (sk_PKCS7_RECIP_INFO_num(rsk) <= idx) return(NULL);
ri=sk_PKCS7_RECIP_INFO_value(rsk,idx);
return(ri->issuer_and_serial);
}
|
['PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx)\n\t{\n\tSTACK_OF(PKCS7_RECIP_INFO) *rsk;\n\tPKCS7_RECIP_INFO *ri;\n\tint i;\n\ti=OBJ_obj2nid(p7->type);\n\tif (i != NID_pkcs7_signedAndEnveloped)\n\t\treturn NULL;\n\tif (p7->d.signed_and_enveloped == NULL)\n\t\treturn NULL;\n\trsk=p7->d.signed_and_enveloped->recipientinfo;\n\tif (rsk == NULL)\n\t\treturn NULL;\n\tri=sk_PKCS7_RECIP_INFO_value(rsk,0);\n\tif (sk_PKCS7_RECIP_INFO_num(rsk) <= idx) return(NULL);\n\tri=sk_PKCS7_RECIP_INFO_value(rsk,idx);\n\treturn(ri->issuer_and_serial);\n\t}', 'int OBJ_obj2nid(const ASN1_OBJECT *a)\n\t{\n\tconst unsigned int *op;\n\tADDED_OBJ ad,*adp;\n\tif (a == NULL)\n\t\treturn(NID_undef);\n\tif (a->nid != 0)\n\t\treturn(a->nid);\n\tif (added != NULL)\n\t\t{\n\t\tad.type=ADDED_DATA;\n\t\tad.obj=(ASN1_OBJECT *)a;\n\t\tadp=lh_ADDED_OBJ_retrieve(added,&ad);\n\t\tif (adp != NULL) return (adp->obj->nid);\n\t\t}\n\top=OBJ_bsearch(const ASN1_OBJECT *, &a, const unsigned int, obj_objs,\n\t\t NUM_OBJ, obj_cmp);\n\tif (op == NULL)\n\t\treturn(NID_undef);\n\treturn(nid_objs[*op].nid);\n\t}', 'void *lh_retrieve(_LHASH *lh, const void *data)\n\t{\n\tunsigned long hash;\n\tLHASH_NODE **rn;\n\tvoid *ret;\n\tlh->error=0;\n\trn=getrn(lh,data,&hash);\n\tif (*rn == NULL)\n\t\t{\n\t\tlh->num_retrieve_miss++;\n\t\treturn(NULL);\n\t\t}\n\telse\n\t\t{\n\t\tret= (*rn)->data;\n\t\tlh->num_retrieve++;\n\t\t}\n\treturn(ret);\n\t}', 'void *sk_value(const _STACK *st, int i)\n{\n\tif(!st || (i < 0) || (i >= st->num)) return NULL;\n\treturn st->data[i];\n}', 'int sk_num(const _STACK *st)\n{\n\tif(st == NULL) return -1;\n\treturn st->num;\n}']
|
2,671
| 0
|
https://github.com/libav/libav/blob/366ba2dee1f2b17825b42e2164d3b9879f0271b1/libavcodec/h264_slice.c/#L1516
|
int ff_h264_decode_slice_header(H264Context *h, H264SliceContext *sl)
{
const SPS *sps;
const PPS *pps;
unsigned int first_mb_in_slice;
unsigned int pps_id;
int ret;
unsigned int slice_type, tmp, i, j;
int last_pic_structure, last_pic_droppable;
int needs_reinit = 0;
int field_pic_flag, bottom_field_flag;
int frame_num, droppable, picture_structure;
int mb_aff_frame = 0;
first_mb_in_slice = get_ue_golomb(&sl->gb);
if (first_mb_in_slice == 0) {
if (h->current_slice && h->cur_pic_ptr && FIELD_PICTURE(h)) {
ff_h264_field_end(h, sl, 1);
}
h->current_slice = 0;
if (!h->first_field) {
if (h->cur_pic_ptr && !h->droppable) {
ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
h->picture_structure == PICT_BOTTOM_FIELD);
}
h->cur_pic_ptr = NULL;
}
}
slice_type = get_ue_golomb_31(&sl->gb);
if (slice_type > 9) {
av_log(h->avctx, AV_LOG_ERROR,
"slice type %d too large at %d\n",
slice_type, first_mb_in_slice);
return AVERROR_INVALIDDATA;
}
if (slice_type > 4) {
slice_type -= 5;
sl->slice_type_fixed = 1;
} else
sl->slice_type_fixed = 0;
slice_type = ff_h264_golomb_to_pict_type[slice_type];
sl->slice_type = slice_type;
sl->slice_type_nos = slice_type & 3;
if (h->nal_unit_type == NAL_IDR_SLICE &&
sl->slice_type_nos != AV_PICTURE_TYPE_I) {
av_log(h->avctx, AV_LOG_ERROR, "A non-intra slice in an IDR NAL unit.\n");
return AVERROR_INVALIDDATA;
}
pps_id = get_ue_golomb(&sl->gb);
if (pps_id >= MAX_PPS_COUNT) {
av_log(h->avctx, AV_LOG_ERROR, "pps_id %u out of range\n", pps_id);
return AVERROR_INVALIDDATA;
}
if (!h->ps.pps_list[pps_id]) {
av_log(h->avctx, AV_LOG_ERROR,
"non-existing PPS %u referenced\n",
pps_id);
return AVERROR_INVALIDDATA;
}
if (!h->setup_finished) {
h->ps.pps = (const PPS*)h->ps.pps_list[pps_id]->data;
} else if (h->ps.pps != (const PPS*)h->ps.pps_list[pps_id]->data) {
av_log(h->avctx, AV_LOG_ERROR, "PPS changed between slices\n");
return AVERROR_INVALIDDATA;
}
if (!h->ps.sps_list[h->ps.pps->sps_id]) {
av_log(h->avctx, AV_LOG_ERROR,
"non-existing SPS %u referenced\n",
h->ps.pps->sps_id);
return AVERROR_INVALIDDATA;
}
if (h->ps.sps != (const SPS*)h->ps.sps_list[h->ps.pps->sps_id]->data) {
h->ps.sps = (SPS*)h->ps.sps_list[h->ps.pps->sps_id]->data;
if (h->bit_depth_luma != h->ps.sps->bit_depth_luma ||
h->chroma_format_idc != h->ps.sps->chroma_format_idc)
needs_reinit = 1;
}
pps = h->ps.pps;
sps = h->ps.sps;
if (!h->setup_finished) {
h->avctx->profile = ff_h264_get_profile(sps);
h->avctx->level = sps->level_idc;
h->avctx->refs = sps->ref_frame_count;
if (h->mb_width != sps->mb_width ||
h->mb_height != sps->mb_height * (2 - sps->frame_mbs_only_flag))
needs_reinit = 1;
h->mb_width = sps->mb_width;
h->mb_height = sps->mb_height * (2 - sps->frame_mbs_only_flag);
h->mb_num = h->mb_width * h->mb_height;
h->mb_stride = h->mb_width + 1;
h->b_stride = h->mb_width * 4;
h->chroma_y_shift = sps->chroma_format_idc <= 1;
h->width = 16 * h->mb_width;
h->height = 16 * h->mb_height;
ret = init_dimensions(h);
if (ret < 0)
return ret;
if (sps->video_signal_type_present_flag) {
h->avctx->color_range = sps->full_range ? AVCOL_RANGE_JPEG
: AVCOL_RANGE_MPEG;
if (sps->colour_description_present_flag) {
if (h->avctx->colorspace != sps->colorspace)
needs_reinit = 1;
h->avctx->color_primaries = sps->color_primaries;
h->avctx->color_trc = sps->color_trc;
h->avctx->colorspace = sps->colorspace;
}
}
}
if (h->context_initialized && needs_reinit) {
h->context_initialized = 0;
if (sl != h->slice_ctx) {
av_log(h->avctx, AV_LOG_ERROR,
"changing width %d -> %d / height %d -> %d on "
"slice %d\n",
h->width, h->avctx->coded_width,
h->height, h->avctx->coded_height,
h->current_slice + 1);
return AVERROR_INVALIDDATA;
}
ff_h264_flush_change(h);
if ((ret = get_pixel_format(h)) < 0)
return ret;
h->avctx->pix_fmt = ret;
av_log(h->avctx, AV_LOG_INFO, "Reinit context to %dx%d, "
"pix_fmt: %d\n", h->width, h->height, h->avctx->pix_fmt);
if ((ret = h264_slice_header_init(h)) < 0) {
av_log(h->avctx, AV_LOG_ERROR,
"h264_slice_header_init() failed\n");
return ret;
}
}
if (!h->context_initialized) {
if (sl != h->slice_ctx) {
av_log(h->avctx, AV_LOG_ERROR,
"Cannot (re-)initialize context during parallel decoding.\n");
return AVERROR_PATCHWELCOME;
}
if ((ret = get_pixel_format(h)) < 0)
return ret;
h->avctx->pix_fmt = ret;
if ((ret = h264_slice_header_init(h)) < 0) {
av_log(h->avctx, AV_LOG_ERROR,
"h264_slice_header_init() failed\n");
return ret;
}
}
frame_num = get_bits(&sl->gb, sps->log2_max_frame_num);
if (!h->setup_finished)
h->poc.frame_num = frame_num;
sl->mb_mbaff = 0;
last_pic_structure = h->picture_structure;
last_pic_droppable = h->droppable;
droppable = h->nal_ref_idc == 0;
if (sps->frame_mbs_only_flag) {
picture_structure = PICT_FRAME;
} else {
field_pic_flag = get_bits1(&sl->gb);
if (field_pic_flag) {
bottom_field_flag = get_bits1(&sl->gb);
picture_structure = PICT_TOP_FIELD + bottom_field_flag;
} else {
picture_structure = PICT_FRAME;
mb_aff_frame = sps->mb_aff;
}
}
if (!h->setup_finished) {
h->droppable = droppable;
h->picture_structure = picture_structure;
h->mb_aff_frame = mb_aff_frame;
}
sl->mb_field_decoding_flag = h->picture_structure != PICT_FRAME;
if (h->current_slice != 0) {
if (last_pic_structure != picture_structure ||
last_pic_droppable != droppable) {
av_log(h->avctx, AV_LOG_ERROR,
"Changing field mode (%d -> %d) between slices is not allowed\n",
last_pic_structure, h->picture_structure);
return AVERROR_INVALIDDATA;
} else if (!h->cur_pic_ptr) {
av_log(h->avctx, AV_LOG_ERROR,
"unset cur_pic_ptr on slice %d\n",
h->current_slice + 1);
return AVERROR_INVALIDDATA;
}
} else {
if (h->poc.frame_num != h->poc.prev_frame_num) {
int unwrap_prev_frame_num = h->poc.prev_frame_num;
int max_frame_num = 1 << sps->log2_max_frame_num;
if (unwrap_prev_frame_num > h->poc.frame_num)
unwrap_prev_frame_num -= max_frame_num;
if ((h->poc.frame_num - unwrap_prev_frame_num) > sps->ref_frame_count) {
unwrap_prev_frame_num = (h->poc.frame_num - sps->ref_frame_count) - 1;
if (unwrap_prev_frame_num < 0)
unwrap_prev_frame_num += max_frame_num;
h->poc.prev_frame_num = unwrap_prev_frame_num;
}
}
if (h->first_field) {
assert(h->cur_pic_ptr);
assert(h->cur_pic_ptr->f->buf[0]);
assert(h->cur_pic_ptr->reference != DELAYED_PIC_REF);
if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
if (!last_pic_droppable && last_pic_structure != PICT_FRAME) {
ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
last_pic_structure == PICT_TOP_FIELD);
}
} else {
if (h->cur_pic_ptr->frame_num != h->poc.frame_num) {
if (!last_pic_droppable && last_pic_structure != PICT_FRAME) {
ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
last_pic_structure == PICT_TOP_FIELD);
}
} else {
if (!((last_pic_structure == PICT_TOP_FIELD &&
h->picture_structure == PICT_BOTTOM_FIELD) ||
(last_pic_structure == PICT_BOTTOM_FIELD &&
h->picture_structure == PICT_TOP_FIELD))) {
av_log(h->avctx, AV_LOG_ERROR,
"Invalid field mode combination %d/%d\n",
last_pic_structure, h->picture_structure);
h->picture_structure = last_pic_structure;
h->droppable = last_pic_droppable;
return AVERROR_INVALIDDATA;
} else if (last_pic_droppable != h->droppable) {
avpriv_request_sample(h->avctx,
"Found reference and non-reference fields in the same frame, which");
h->picture_structure = last_pic_structure;
h->droppable = last_pic_droppable;
return AVERROR_PATCHWELCOME;
}
}
}
}
while (h->poc.frame_num != h->poc.prev_frame_num &&
h->poc.frame_num != (h->poc.prev_frame_num + 1) % (1 << sps->log2_max_frame_num)) {
H264Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;
av_log(h->avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n",
h->poc.frame_num, h->poc.prev_frame_num);
ret = initialize_cur_frame(h);
if (ret < 0) {
h->first_field = 0;
return ret;
}
h->poc.prev_frame_num++;
h->poc.prev_frame_num %= 1 << sps->log2_max_frame_num;
h->cur_pic_ptr->frame_num = h->poc.prev_frame_num;
ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 0);
ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 1);
ret = ff_generate_sliding_window_mmcos(h, 1);
if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
return ret;
ret = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
return ret;
if (h->short_ref_count) {
if (prev &&
h->short_ref[0]->f->width == prev->f->width &&
h->short_ref[0]->f->height == prev->f->height &&
h->short_ref[0]->f->format == prev->f->format) {
av_image_copy(h->short_ref[0]->f->data,
h->short_ref[0]->f->linesize,
(const uint8_t **)prev->f->data,
prev->f->linesize,
prev->f->format,
h->mb_width * 16,
h->mb_height * 16);
h->short_ref[0]->poc = prev->poc + 2;
}
h->short_ref[0]->frame_num = h->poc.prev_frame_num;
}
}
if (h->first_field) {
assert(h->cur_pic_ptr);
assert(h->cur_pic_ptr->f->buf[0]);
assert(h->cur_pic_ptr->reference != DELAYED_PIC_REF);
if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
h->cur_pic_ptr = NULL;
h->first_field = FIELD_PICTURE(h);
} else {
if (h->cur_pic_ptr->frame_num != h->poc.frame_num) {
h->first_field = 1;
h->cur_pic_ptr = NULL;
} else {
h->first_field = 0;
}
}
} else {
h->first_field = FIELD_PICTURE(h);
}
if (!FIELD_PICTURE(h) || h->first_field) {
if (h264_frame_start(h) < 0) {
h->first_field = 0;
return AVERROR_INVALIDDATA;
}
} else {
release_unused_pictures(h, 0);
}
}
assert(h->mb_num == h->mb_width * h->mb_height);
if (first_mb_in_slice << FIELD_OR_MBAFF_PICTURE(h) >= h->mb_num ||
first_mb_in_slice >= h->mb_num) {
av_log(h->avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
return AVERROR_INVALIDDATA;
}
sl->resync_mb_x = sl->mb_x = first_mb_in_slice % h->mb_width;
sl->resync_mb_y = sl->mb_y = (first_mb_in_slice / h->mb_width) <<
FIELD_OR_MBAFF_PICTURE(h);
if (h->picture_structure == PICT_BOTTOM_FIELD)
sl->resync_mb_y = sl->mb_y = sl->mb_y + 1;
assert(sl->mb_y < h->mb_height);
if (h->picture_structure == PICT_FRAME) {
h->curr_pic_num = h->poc.frame_num;
h->max_pic_num = 1 << sps->log2_max_frame_num;
} else {
h->curr_pic_num = 2 * h->poc.frame_num + 1;
h->max_pic_num = 1 << (sps->log2_max_frame_num + 1);
}
if (h->nal_unit_type == NAL_IDR_SLICE)
get_ue_golomb(&sl->gb);
if (sps->poc_type == 0) {
int poc_lsb = get_bits(&sl->gb, sps->log2_max_poc_lsb);
if (!h->setup_finished)
h->poc.poc_lsb = poc_lsb;
if (pps->pic_order_present == 1 && h->picture_structure == PICT_FRAME) {
int delta_poc_bottom = get_se_golomb(&sl->gb);
if (!h->setup_finished)
h->poc.delta_poc_bottom = delta_poc_bottom;
}
}
if (sps->poc_type == 1 && !sps->delta_pic_order_always_zero_flag) {
int delta_poc = get_se_golomb(&sl->gb);
if (!h->setup_finished)
h->poc.delta_poc[0] = delta_poc;
if (pps->pic_order_present == 1 && h->picture_structure == PICT_FRAME) {
delta_poc = get_se_golomb(&sl->gb);
if (!h->setup_finished)
h->poc.delta_poc[1] = delta_poc;
}
}
if (!h->setup_finished)
ff_h264_init_poc(h->cur_pic_ptr->field_poc, &h->cur_pic_ptr->poc,
sps, &h->poc, h->picture_structure, h->nal_ref_idc);
if (pps->redundant_pic_cnt_present)
sl->redundant_pic_count = get_ue_golomb(&sl->gb);
if (sl->slice_type_nos == AV_PICTURE_TYPE_B)
sl->direct_spatial_mv_pred = get_bits1(&sl->gb);
ret = ff_h264_parse_ref_count(&sl->list_count, sl->ref_count,
&sl->gb, pps, sl->slice_type_nos,
h->picture_structure);
if (ret < 0)
return ret;
if (sl->slice_type_nos != AV_PICTURE_TYPE_I) {
ret = ff_h264_decode_ref_pic_list_reordering(h, sl);
if (ret < 0) {
sl->ref_count[1] = sl->ref_count[0] = 0;
return ret;
}
}
if ((pps->weighted_pred && sl->slice_type_nos == AV_PICTURE_TYPE_P) ||
(pps->weighted_bipred_idc == 1 &&
sl->slice_type_nos == AV_PICTURE_TYPE_B))
ff_h264_pred_weight_table(&sl->gb, sps, sl->ref_count,
sl->slice_type_nos, &sl->pwt);
else if (pps->weighted_bipred_idc == 2 &&
sl->slice_type_nos == AV_PICTURE_TYPE_B) {
implicit_weight_table(h, sl, -1);
} else {
sl->pwt.use_weight = 0;
for (i = 0; i < 2; i++) {
sl->pwt.luma_weight_flag[i] = 0;
sl->pwt.chroma_weight_flag[i] = 0;
}
}
if (h->nal_ref_idc) {
ret = ff_h264_decode_ref_pic_marking(h, &sl->gb,
!(h->avctx->active_thread_type & FF_THREAD_FRAME) ||
h->current_slice == 0);
if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
return AVERROR_INVALIDDATA;
}
if (FRAME_MBAFF(h)) {
ff_h264_fill_mbaff_ref_list(sl);
if (pps->weighted_bipred_idc == 2 && sl->slice_type_nos == AV_PICTURE_TYPE_B) {
implicit_weight_table(h, sl, 0);
implicit_weight_table(h, sl, 1);
}
}
if (sl->slice_type_nos == AV_PICTURE_TYPE_B && !sl->direct_spatial_mv_pred)
ff_h264_direct_dist_scale_factor(h, sl);
ff_h264_direct_ref_list_init(h, sl);
if (sl->slice_type_nos != AV_PICTURE_TYPE_I && pps->cabac) {
tmp = get_ue_golomb_31(&sl->gb);
if (tmp > 2) {
av_log(h->avctx, AV_LOG_ERROR, "cabac_init_idc %u overflow\n", tmp);
return AVERROR_INVALIDDATA;
}
sl->cabac_init_idc = tmp;
}
sl->last_qscale_diff = 0;
tmp = pps->init_qp + get_se_golomb(&sl->gb);
if (tmp > 51 + 6 * (sps->bit_depth_luma - 8)) {
av_log(h->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
return AVERROR_INVALIDDATA;
}
sl->qscale = tmp;
sl->chroma_qp[0] = get_chroma_qp(h, 0, sl->qscale);
sl->chroma_qp[1] = get_chroma_qp(h, 1, sl->qscale);
if (sl->slice_type == AV_PICTURE_TYPE_SP)
get_bits1(&sl->gb);
if (sl->slice_type == AV_PICTURE_TYPE_SP ||
sl->slice_type == AV_PICTURE_TYPE_SI)
get_se_golomb(&sl->gb);
sl->deblocking_filter = 1;
sl->slice_alpha_c0_offset = 0;
sl->slice_beta_offset = 0;
if (pps->deblocking_filter_parameters_present) {
tmp = get_ue_golomb_31(&sl->gb);
if (tmp > 2) {
av_log(h->avctx, AV_LOG_ERROR,
"deblocking_filter_idc %u out of range\n", tmp);
return AVERROR_INVALIDDATA;
}
sl->deblocking_filter = tmp;
if (sl->deblocking_filter < 2)
sl->deblocking_filter ^= 1;
if (sl->deblocking_filter) {
sl->slice_alpha_c0_offset = get_se_golomb(&sl->gb) * 2;
sl->slice_beta_offset = get_se_golomb(&sl->gb) * 2;
if (sl->slice_alpha_c0_offset > 12 ||
sl->slice_alpha_c0_offset < -12 ||
sl->slice_beta_offset > 12 ||
sl->slice_beta_offset < -12) {
av_log(h->avctx, AV_LOG_ERROR,
"deblocking filter parameters %d %d out of range\n",
sl->slice_alpha_c0_offset, sl->slice_beta_offset);
return AVERROR_INVALIDDATA;
}
}
}
if (h->avctx->skip_loop_filter >= AVDISCARD_ALL ||
(h->avctx->skip_loop_filter >= AVDISCARD_NONKEY &&
sl->slice_type_nos != AV_PICTURE_TYPE_I) ||
(h->avctx->skip_loop_filter >= AVDISCARD_BIDIR &&
sl->slice_type_nos == AV_PICTURE_TYPE_B) ||
(h->avctx->skip_loop_filter >= AVDISCARD_NONREF &&
h->nal_ref_idc == 0))
sl->deblocking_filter = 0;
if (sl->deblocking_filter == 1 && h->nb_slice_ctx > 1) {
if (h->avctx->flags2 & AV_CODEC_FLAG2_FAST) {
sl->deblocking_filter = 2;
} else {
h->postpone_filter = 1;
}
}
sl->qp_thresh = 15 -
FFMIN(sl->slice_alpha_c0_offset, sl->slice_beta_offset) -
FFMAX3(0,
pps->chroma_qp_index_offset[0],
pps->chroma_qp_index_offset[1]) +
6 * (sps->bit_depth_luma - 8);
sl->slice_num = ++h->current_slice;
if (sl->slice_num >= MAX_SLICES) {
av_log(h->avctx, AV_LOG_ERROR,
"Too many slices, increase MAX_SLICES and recompile\n");
}
for (j = 0; j < 2; j++) {
int id_list[16];
int *ref2frm = h->ref2frm[sl->slice_num & (MAX_SLICES - 1)][j];
for (i = 0; i < 16; i++) {
id_list[i] = 60;
if (j < sl->list_count && i < sl->ref_count[j] &&
sl->ref_list[j][i].parent->f->buf[0]) {
int k;
AVBuffer *buf = sl->ref_list[j][i].parent->f->buf[0]->buffer;
for (k = 0; k < h->short_ref_count; k++)
if (h->short_ref[k]->f->buf[0]->buffer == buf) {
id_list[i] = k;
break;
}
for (k = 0; k < h->long_ref_count; k++)
if (h->long_ref[k] && h->long_ref[k]->f->buf[0]->buffer == buf) {
id_list[i] = h->short_ref_count + k;
break;
}
}
}
ref2frm[0] =
ref2frm[1] = -1;
for (i = 0; i < 16; i++)
ref2frm[i + 2] = 4 * id_list[i] + (sl->ref_list[j][i].reference & 3);
ref2frm[18 + 0] =
ref2frm[18 + 1] = -1;
for (i = 16; i < 48; i++)
ref2frm[i + 4] = 4 * id_list[(i - 16) >> 1] +
(sl->ref_list[j][i].reference & 3);
}
if (h->avctx->debug & FF_DEBUG_PICT_INFO) {
av_log(h->avctx, AV_LOG_DEBUG,
"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",
sl->slice_num,
(h->picture_structure == PICT_FRAME ? "F" : h->picture_structure == PICT_TOP_FIELD ? "T" : "B"),
first_mb_in_slice,
av_get_picture_type_char(sl->slice_type),
sl->slice_type_fixed ? " fix" : "",
h->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",
pps_id, h->poc.frame_num,
h->cur_pic_ptr->field_poc[0],
h->cur_pic_ptr->field_poc[1],
sl->ref_count[0], sl->ref_count[1],
sl->qscale,
sl->deblocking_filter,
sl->slice_alpha_c0_offset, sl->slice_beta_offset,
sl->pwt.use_weight,
sl->pwt.use_weight == 1 && sl->pwt.use_weight_chroma ? "c" : "",
sl->slice_type == AV_PICTURE_TYPE_B ? (sl->direct_spatial_mv_pred ? "SPAT" : "TEMP") : "");
}
return 0;
}
|
['int ff_h264_decode_slice_header(H264Context *h, H264SliceContext *sl)\n{\n const SPS *sps;\n const PPS *pps;\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 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 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->ps.pps_list[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->ps.pps = (const PPS*)h->ps.pps_list[pps_id]->data;\n } else if (h->ps.pps != (const PPS*)h->ps.pps_list[pps_id]->data) {\n av_log(h->avctx, AV_LOG_ERROR, "PPS changed between slices\\n");\n return AVERROR_INVALIDDATA;\n }\n if (!h->ps.sps_list[h->ps.pps->sps_id]) {\n av_log(h->avctx, AV_LOG_ERROR,\n "non-existing SPS %u referenced\\n",\n h->ps.pps->sps_id);\n return AVERROR_INVALIDDATA;\n }\n if (h->ps.sps != (const SPS*)h->ps.sps_list[h->ps.pps->sps_id]->data) {\n h->ps.sps = (SPS*)h->ps.sps_list[h->ps.pps->sps_id]->data;\n if (h->bit_depth_luma != h->ps.sps->bit_depth_luma ||\n h->chroma_format_idc != h->ps.sps->chroma_format_idc)\n needs_reinit = 1;\n }\n pps = h->ps.pps;\n sps = h->ps.sps;\n if (!h->setup_finished) {\n h->avctx->profile = ff_h264_get_profile(sps);\n h->avctx->level = sps->level_idc;\n h->avctx->refs = sps->ref_frame_count;\n if (h->mb_width != sps->mb_width ||\n h->mb_height != sps->mb_height * (2 - sps->frame_mbs_only_flag))\n needs_reinit = 1;\n h->mb_width = sps->mb_width;\n h->mb_height = sps->mb_height * (2 - 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 = 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 (sps->video_signal_type_present_flag) {\n h->avctx->color_range = sps->full_range ? AVCOL_RANGE_JPEG\n : AVCOL_RANGE_MPEG;\n if (sps->colour_description_present_flag) {\n if (h->avctx->colorspace != sps->colorspace)\n needs_reinit = 1;\n h->avctx->color_primaries = sps->color_primaries;\n h->avctx->color_trc = sps->color_trc;\n h->avctx->colorspace = 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 frame_num = get_bits(&sl->gb, sps->log2_max_frame_num);\n if (!h->setup_finished)\n h->poc.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 (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 = 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->poc.frame_num != h->poc.prev_frame_num) {\n int unwrap_prev_frame_num = h->poc.prev_frame_num;\n int max_frame_num = 1 << sps->log2_max_frame_num;\n if (unwrap_prev_frame_num > h->poc.frame_num)\n unwrap_prev_frame_num -= max_frame_num;\n if ((h->poc.frame_num - unwrap_prev_frame_num) > sps->ref_frame_count) {\n unwrap_prev_frame_num = (h->poc.frame_num - sps->ref_frame_count) - 1;\n if (unwrap_prev_frame_num < 0)\n unwrap_prev_frame_num += max_frame_num;\n h->poc.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->poc.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->poc.frame_num != h->poc.prev_frame_num &&\n h->poc.frame_num != (h->poc.prev_frame_num + 1) % (1 << 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->poc.frame_num, h->poc.prev_frame_num);\n ret = initialize_cur_frame(h);\n if (ret < 0) {\n h->first_field = 0;\n return ret;\n }\n h->poc.prev_frame_num++;\n h->poc.prev_frame_num %= 1 << sps->log2_max_frame_num;\n h->cur_pic_ptr->frame_num = h->poc.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->poc.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->poc.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->poc.frame_num;\n h->max_pic_num = 1 << sps->log2_max_frame_num;\n } else {\n h->curr_pic_num = 2 * h->poc.frame_num + 1;\n h->max_pic_num = 1 << (sps->log2_max_frame_num + 1);\n }\n if (h->nal_unit_type == NAL_IDR_SLICE)\n get_ue_golomb(&sl->gb);\n if (sps->poc_type == 0) {\n int poc_lsb = get_bits(&sl->gb, sps->log2_max_poc_lsb);\n if (!h->setup_finished)\n h->poc.poc_lsb = poc_lsb;\n if (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->poc.delta_poc_bottom = delta_poc_bottom;\n }\n }\n if (sps->poc_type == 1 && !sps->delta_pic_order_always_zero_flag) {\n int delta_poc = get_se_golomb(&sl->gb);\n if (!h->setup_finished)\n h->poc.delta_poc[0] = delta_poc;\n if (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->poc.delta_poc[1] = delta_poc;\n }\n }\n if (!h->setup_finished)\n ff_h264_init_poc(h->cur_pic_ptr->field_poc, &h->cur_pic_ptr->poc,\n sps, &h->poc, h->picture_structure, h->nal_ref_idc);\n if (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, 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 ((pps->weighted_pred && sl->slice_type_nos == AV_PICTURE_TYPE_P) ||\n (pps->weighted_bipred_idc == 1 &&\n sl->slice_type_nos == AV_PICTURE_TYPE_B))\n ff_h264_pred_weight_table(&sl->gb, sps, sl->ref_count,\n sl->slice_type_nos, &sl->pwt);\n else if (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(sl);\n if (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 && 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 = pps->init_qp + get_se_golomb(&sl->gb);\n if (tmp > 51 + 6 * (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 (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->nb_slice_ctx > 1) {\n if (h->avctx->flags2 & AV_CODEC_FLAG2_FAST) {\n sl->deblocking_filter = 2;\n } else {\n h->postpone_filter = 1;\n }\n }\n sl->qp_thresh = 15 -\n FFMIN(sl->slice_alpha_c0_offset, sl->slice_beta_offset) -\n FFMAX3(0,\n pps->chroma_qp_index_offset[0],\n pps->chroma_qp_index_offset[1]) +\n 6 * (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 = h->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->poc.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}']
|
2,672
| 0
|
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/bn/bn_ctx.c/#L328
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['int ec_GF2m_simple_make_affine(const EC_GROUP *group, EC_POINT *point,\n BN_CTX *ctx)\n{\n BN_CTX *new_ctx = NULL;\n BIGNUM *x, *y;\n int ret = 0;\n if (point->Z_is_one || EC_POINT_is_at_infinity(group, point))\n return 1;\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n BN_CTX_start(ctx);\n x = BN_CTX_get(ctx);\n y = BN_CTX_get(ctx);\n if (y == NULL)\n goto err;\n if (!EC_POINT_get_affine_coordinates_GF2m(group, point, x, y, ctx))\n goto err;\n if (!BN_copy(point->X, x))\n goto err;\n if (!BN_copy(point->Y, y))\n goto err;\n if (!BN_one(point->Z))\n goto err;\n ret = 1;\n err:\n if (ctx)\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}', '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}']
|
2,673
| 0
|
https://github.com/nginx/nginx/blob/78aacc838bac5c92fe92f8533526515766cfce15/src/http/ngx_http_core_module.c/#L1802
|
void
ngx_http_set_exten(ngx_http_request_t *r)
{
ngx_int_t i;
ngx_str_null(&r->exten);
for (i = r->uri.len - 1; i > 1; i--) {
if (r->uri.data[i] == '.' && r->uri.data[i - 1] != '/') {
r->exten.len = r->uri.len - i - 1;
r->exten.data = &r->uri.data[i + 1];
return;
} else if (r->uri.data[i] == '/') {
return;
}
}
return;
}
|
['static void\nngx_http_upstream_resolve_handler(ngx_resolver_ctx_t *ctx)\n{\n ngx_connection_t *c;\n ngx_http_request_t *r;\n ngx_http_upstream_t *u;\n ngx_http_upstream_resolved_t *ur;\n r = ctx->data;\n c = r->connection;\n u = r->upstream;\n ur = u->resolved;\n if (ctx->state) {\n ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,\n "%V could not be resolved (%i: %s)",\n &ctx->name, ctx->state,\n ngx_resolver_strerror(ctx->state));\n ngx_http_upstream_finalize_request(r, u, NGX_HTTP_BAD_GATEWAY);\n goto failed;\n }\n ur->naddrs = ctx->naddrs;\n ur->addrs = ctx->addrs;\n#if (NGX_DEBUG)\n {\n in_addr_t addr;\n ngx_uint_t i;\n for (i = 0; i < ctx->naddrs; i++) {\n addr = ntohl(ur->addrs[i]);\n ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "name was resolved to %ud.%ud.%ud.%ud",\n (addr >> 24) & 0xff, (addr >> 16) & 0xff,\n (addr >> 8) & 0xff, addr & 0xff);\n }\n }\n#endif\n if (ngx_http_upstream_create_round_robin_peer(r, ur) != NGX_OK) {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n goto failed;\n }\n ngx_resolve_name_done(ctx);\n ur->ctx = NULL;\n ngx_http_upstream_connect(r, u);\nfailed:\n ngx_http_run_posted_requests(c);\n}', 'static void\nngx_http_upstream_finalize_request(ngx_http_request_t *r,\n ngx_http_upstream_t *u, ngx_int_t rc)\n{\n ngx_time_t *tp;\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "finalize http upstream request: %i", rc);\n if (u->cleanup) {\n *u->cleanup = NULL;\n u->cleanup = NULL;\n }\n if (u->resolved && u->resolved->ctx) {\n ngx_resolve_name_done(u->resolved->ctx);\n u->resolved->ctx = NULL;\n }\n if (u->state && u->state->response_sec) {\n tp = ngx_timeofday();\n u->state->response_sec = tp->sec - u->state->response_sec;\n u->state->response_msec = tp->msec - u->state->response_msec;\n if (u->pipe && u->pipe->read_length) {\n u->state->response_length = u->pipe->read_length;\n }\n }\n u->finalize_request(r, rc);\n if (u->peer.free && u->peer.sockaddr) {\n u->peer.free(&u->peer, u->peer.data, 0);\n u->peer.sockaddr = NULL;\n }\n if (u->peer.connection) {\n#if (NGX_HTTP_SSL)\n if (u->peer.connection->ssl) {\n u->peer.connection->ssl->no_wait_shutdown = 1;\n (void) ngx_ssl_shutdown(u->peer.connection);\n }\n#endif\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "close http upstream connection: %d",\n u->peer.connection->fd);\n if (u->peer.connection->pool) {\n ngx_destroy_pool(u->peer.connection->pool);\n }\n ngx_close_connection(u->peer.connection);\n }\n u->peer.connection = NULL;\n if (u->pipe && u->pipe->temp_file) {\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "http upstream temp fd: %d",\n u->pipe->temp_file->file.fd);\n }\n if (u->store && u->pipe && u->pipe->temp_file\n && u->pipe->temp_file->file.fd != NGX_INVALID_FILE)\n {\n if (ngx_delete_file(u->pipe->temp_file->file.name.data)\n == NGX_FILE_ERROR)\n {\n ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,\n ngx_delete_file_n " \\"%s\\" failed",\n u->pipe->temp_file->file.name.data);\n }\n }\n#if (NGX_HTTP_CACHE)\n if (r->cache) {\n if (u->cacheable) {\n if (rc == NGX_HTTP_BAD_GATEWAY || rc == NGX_HTTP_GATEWAY_TIME_OUT) {\n time_t valid;\n valid = ngx_http_file_cache_valid(u->conf->cache_valid, rc);\n if (valid) {\n r->cache->valid_sec = ngx_time() + valid;\n r->cache->error = rc;\n }\n }\n }\n ngx_http_file_cache_free(r->cache, u->pipe->temp_file);\n }\n#endif\n if (rc == NGX_DECLINED) {\n return;\n }\n r->connection->log->action = "sending to client";\n if (!u->header_sent\n || rc == NGX_HTTP_REQUEST_TIME_OUT\n || rc == NGX_HTTP_CLIENT_CLOSED_REQUEST)\n {\n ngx_http_finalize_request(r, rc);\n return;\n }\n if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {\n rc = 0;\n }\n if (r->header_only) {\n ngx_http_finalize_request(r, rc);\n return;\n }\n if (rc == 0) {\n rc = ngx_http_send_special(r, NGX_HTTP_LAST);\n }\n ngx_http_finalize_request(r, rc);\n}', 'void\nngx_http_finalize_request(ngx_http_request_t *r, ngx_int_t rc)\n{\n ngx_connection_t *c;\n ngx_http_request_t *pr;\n ngx_http_core_loc_conf_t *clcf;\n c = r->connection;\n ngx_log_debug5(NGX_LOG_DEBUG_HTTP, c->log, 0,\n "http finalize request: %d, \\"%V?%V\\" a:%d, c:%d",\n rc, &r->uri, &r->args, r == c->data, r->main->count);\n if (rc == NGX_DONE) {\n ngx_http_finalize_connection(r);\n return;\n }\n if (rc == NGX_OK && r->filter_finalize) {\n c->error = 1;\n }\n if (rc == NGX_DECLINED) {\n r->content_handler = NULL;\n r->write_event_handler = ngx_http_core_run_phases;\n ngx_http_core_run_phases(r);\n return;\n }\n if (r != r->main && r->post_subrequest) {\n rc = r->post_subrequest->handler(r, r->post_subrequest->data, rc);\n }\n if (rc == NGX_ERROR\n || rc == NGX_HTTP_REQUEST_TIME_OUT\n || rc == NGX_HTTP_CLIENT_CLOSED_REQUEST\n || c->error)\n {\n if (ngx_http_post_action(r) == NGX_OK) {\n return;\n }\n if (r->main->blocked) {\n r->write_event_handler = ngx_http_request_finalizer;\n }\n ngx_http_terminate_request(r, rc);\n return;\n }\n if (rc >= NGX_HTTP_SPECIAL_RESPONSE\n || rc == NGX_HTTP_CREATED\n || rc == NGX_HTTP_NO_CONTENT)\n {\n if (rc == NGX_HTTP_CLOSE) {\n ngx_http_terminate_request(r, rc);\n return;\n }\n if (r == r->main) {\n if (c->read->timer_set) {\n ngx_del_timer(c->read);\n }\n if (c->write->timer_set) {\n ngx_del_timer(c->write);\n }\n }\n c->read->handler = ngx_http_request_handler;\n c->write->handler = ngx_http_request_handler;\n ngx_http_finalize_request(r, ngx_http_special_response_handler(r, rc));\n return;\n }\n if (r != r->main) {\n if (r->buffered || r->postponed) {\n if (ngx_http_set_write_handler(r) != NGX_OK) {\n ngx_http_terminate_request(r, 0);\n }\n return;\n }\n pr = r->parent;\n if (r == c->data) {\n r->main->count--;\n r->main->subrequests++;\n if (!r->logged) {\n clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);\n if (clcf->log_subrequest) {\n ngx_http_log_request(r);\n }\n r->logged = 1;\n } else {\n ngx_log_error(NGX_LOG_ALERT, c->log, 0,\n "subrequest: \\"%V?%V\\" logged again",\n &r->uri, &r->args);\n }\n r->done = 1;\n if (pr->postponed && pr->postponed->request == r) {\n pr->postponed = pr->postponed->next;\n }\n c->data = pr;\n } else {\n ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,\n "http finalize non-active request: \\"%V?%V\\"",\n &r->uri, &r->args);\n r->write_event_handler = ngx_http_request_finalizer;\n if (r->waited) {\n r->done = 1;\n }\n }\n if (ngx_http_post_request(pr, NULL) != NGX_OK) {\n r->main->count++;\n ngx_http_terminate_request(r, 0);\n return;\n }\n ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,\n "http wake parent request: \\"%V?%V\\"",\n &pr->uri, &pr->args);\n return;\n }\n if (r->buffered || c->buffered || r->postponed || r->blocked) {\n if (ngx_http_set_write_handler(r) != NGX_OK) {\n ngx_http_terminate_request(r, 0);\n }\n return;\n }\n if (r != c->data) {\n ngx_log_error(NGX_LOG_ALERT, c->log, 0,\n "http finalize non-active request: \\"%V?%V\\"",\n &r->uri, &r->args);\n return;\n }\n r->done = 1;\n r->write_event_handler = ngx_http_request_empty_handler;\n if (!r->post_action) {\n r->request_complete = 1;\n }\n if (ngx_http_post_action(r) == NGX_OK) {\n return;\n }\n if (c->read->timer_set) {\n ngx_del_timer(c->read);\n }\n if (c->write->timer_set) {\n c->write->delayed = 0;\n ngx_del_timer(c->write);\n }\n if (c->read->eof) {\n ngx_http_close_request(r, 0);\n return;\n }\n ngx_http_finalize_connection(r);\n}', 'ngx_int_t\nngx_http_special_response_handler(ngx_http_request_t *r, ngx_int_t error)\n{\n ngx_uint_t i, err;\n ngx_http_err_page_t *err_page;\n ngx_http_core_loc_conf_t *clcf;\n ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "http special response: %d, \\"%V?%V\\"",\n error, &r->uri, &r->args);\n r->err_status = error;\n if (r->keepalive) {\n switch (error) {\n case NGX_HTTP_BAD_REQUEST:\n case NGX_HTTP_REQUEST_ENTITY_TOO_LARGE:\n case NGX_HTTP_REQUEST_URI_TOO_LARGE:\n case NGX_HTTP_TO_HTTPS:\n case NGX_HTTPS_CERT_ERROR:\n case NGX_HTTPS_NO_CERT:\n case NGX_HTTP_INTERNAL_SERVER_ERROR:\n case NGX_HTTP_NOT_IMPLEMENTED:\n r->keepalive = 0;\n }\n }\n if (r->lingering_close) {\n switch (error) {\n case NGX_HTTP_BAD_REQUEST:\n case NGX_HTTP_TO_HTTPS:\n case NGX_HTTPS_CERT_ERROR:\n case NGX_HTTPS_NO_CERT:\n r->lingering_close = 0;\n }\n }\n r->headers_out.content_type.len = 0;\n clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);\n if (!r->error_page && clcf->error_pages && r->uri_changes != 0) {\n if (clcf->recursive_error_pages == 0) {\n r->error_page = 1;\n }\n err_page = clcf->error_pages->elts;\n for (i = 0; i < clcf->error_pages->nelts; i++) {\n if (err_page[i].status == error) {\n return ngx_http_send_error_page(r, &err_page[i]);\n }\n }\n }\n r->expect_tested = 1;\n if (ngx_http_discard_request_body(r) != NGX_OK) {\n r->keepalive = 0;\n }\n if (clcf->msie_refresh\n && r->headers_in.msie\n && (error == NGX_HTTP_MOVED_PERMANENTLY\n || error == NGX_HTTP_MOVED_TEMPORARILY))\n {\n return ngx_http_send_refresh(r);\n }\n if (error == NGX_HTTP_CREATED) {\n err = 0;\n } else if (error == NGX_HTTP_NO_CONTENT) {\n err = 0;\n } else if (error >= NGX_HTTP_MOVED_PERMANENTLY\n && error < NGX_HTTP_LAST_3XX)\n {\n err = error - NGX_HTTP_MOVED_PERMANENTLY + NGX_HTTP_OFF_3XX;\n } else if (error >= NGX_HTTP_BAD_REQUEST\n && error < NGX_HTTP_LAST_4XX)\n {\n err = error - NGX_HTTP_BAD_REQUEST + NGX_HTTP_OFF_4XX;\n } else if (error >= NGX_HTTP_NGINX_CODES\n && error < NGX_HTTP_LAST_5XX)\n {\n err = error - NGX_HTTP_NGINX_CODES + NGX_HTTP_OFF_5XX;\n switch (error) {\n case NGX_HTTP_TO_HTTPS:\n case NGX_HTTPS_CERT_ERROR:\n case NGX_HTTPS_NO_CERT:\n case NGX_HTTP_REQUEST_HEADER_TOO_LARGE:\n r->err_status = NGX_HTTP_BAD_REQUEST;\n break;\n }\n } else {\n err = 0;\n }\n return ngx_http_send_special_response(r, clcf, err);\n}', 'static ngx_int_t\nngx_http_send_error_page(ngx_http_request_t *r, ngx_http_err_page_t *err_page)\n{\n ngx_int_t overwrite;\n ngx_str_t uri, args;\n ngx_table_elt_t *location;\n ngx_http_core_loc_conf_t *clcf;\n overwrite = err_page->overwrite;\n if (overwrite && overwrite != NGX_HTTP_OK) {\n r->expect_tested = 1;\n }\n if (overwrite >= 0) {\n r->err_status = overwrite;\n }\n if (ngx_http_complex_value(r, &err_page->value, &uri) != NGX_OK) {\n return NGX_ERROR;\n }\n if (uri.data[0] == \'/\') {\n if (err_page->value.lengths) {\n ngx_http_split_args(r, &uri, &args);\n } else {\n args = err_page->args;\n }\n if (r->method != NGX_HTTP_HEAD) {\n r->method = NGX_HTTP_GET;\n r->method_name = ngx_http_get_name;\n }\n return ngx_http_internal_redirect(r, &uri, &args);\n }\n if (uri.data[0] == \'@\') {\n return ngx_http_named_location(r, &uri);\n }\n location = ngx_list_push(&r->headers_out.headers);\n if (location == NULL) {\n return NGX_ERROR;\n }\n if (overwrite != NGX_HTTP_MOVED_PERMANENTLY\n && overwrite != NGX_HTTP_MOVED_TEMPORARILY\n && overwrite != NGX_HTTP_SEE_OTHER\n && overwrite != NGX_HTTP_TEMPORARY_REDIRECT)\n {\n r->err_status = NGX_HTTP_MOVED_TEMPORARILY;\n }\n location->hash = 1;\n ngx_str_set(&location->key, "Location");\n location->value = uri;\n ngx_http_clear_location(r);\n r->headers_out.location = location;\n clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);\n if (clcf->msie_refresh && r->headers_in.msie) {\n return ngx_http_send_refresh(r);\n }\n return ngx_http_send_special_response(r, clcf, r->err_status\n - NGX_HTTP_MOVED_PERMANENTLY\n + NGX_HTTP_OFF_3XX);\n}', 'ngx_int_t\nngx_http_internal_redirect(ngx_http_request_t *r,\n ngx_str_t *uri, ngx_str_t *args)\n{\n ngx_http_core_srv_conf_t *cscf;\n r->uri_changes--;\n if (r->uri_changes == 0) {\n ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,\n "rewrite or internal redirection cycle "\n "while internally redirecting to \\"%V\\"", uri);\n r->main->count++;\n ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);\n return NGX_DONE;\n }\n r->uri = *uri;\n if (args) {\n r->args = *args;\n } else {\n ngx_str_null(&r->args);\n }\n ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "internal redirect: \\"%V?%V\\"", uri, &r->args);\n ngx_http_set_exten(r);\n ngx_memzero(r->ctx, sizeof(void *) * ngx_http_max_module);\n cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);\n r->loc_conf = cscf->ctx->loc_conf;\n ngx_http_update_location_config(r);\n#if (NGX_HTTP_CACHE)\n r->cache = NULL;\n#endif\n r->internal = 1;\n r->valid_unparsed_uri = 0;\n r->add_uri_to_alias = 0;\n r->main->count++;\n ngx_http_handler(r);\n return NGX_DONE;\n}', "void\nngx_http_set_exten(ngx_http_request_t *r)\n{\n ngx_int_t i;\n ngx_str_null(&r->exten);\n for (i = r->uri.len - 1; i > 1; i--) {\n if (r->uri.data[i] == '.' && r->uri.data[i - 1] != '/') {\n r->exten.len = r->uri.len - i - 1;\n r->exten.data = &r->uri.data[i + 1];\n return;\n } else if (r->uri.data[i] == '/') {\n return;\n }\n }\n return;\n}"]
|
2,674
| 0
|
https://github.com/openssl/openssl/blob/5c98b2caf5ce545fbf77611431c7084979da8177/crypto/bn/bn_ctx.c/#L353
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['static int RSA_eay_public_encrypt(int flen, const unsigned char *from,\n\t unsigned char *to, RSA *rsa, int padding)\n\t{\n\tBIGNUM *f,*ret;\n\tint i,j,k,num=0,r= -1;\n\tunsigned char *buf=NULL;\n\tBN_CTX *ctx=NULL;\n\tif ((ctx=BN_CTX_new()) == NULL) goto err;\n\tBN_CTX_start(ctx);\n\tf = BN_CTX_get(ctx);\n\tret = BN_CTX_get(ctx);\n\tnum=BN_num_bytes(rsa->n);\n\tbuf = OPENSSL_malloc(num);\n\tif (!f || !ret || !buf)\n\t\t{\n\t\tRSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,ERR_R_MALLOC_FAILURE);\n\t\tgoto err;\n\t\t}\n\tswitch (padding)\n\t\t{\n\tcase RSA_PKCS1_PADDING:\n\t\ti=RSA_padding_add_PKCS1_type_2(buf,num,from,flen);\n\t\tbreak;\n#ifndef OPENSSL_NO_SHA\n\tcase RSA_PKCS1_OAEP_PADDING:\n\t i=RSA_padding_add_PKCS1_OAEP(buf,num,from,flen,NULL,0);\n\t\tbreak;\n#endif\n\tcase RSA_SSLV23_PADDING:\n\t\ti=RSA_padding_add_SSLv23(buf,num,from,flen);\n\t\tbreak;\n\tcase RSA_NO_PADDING:\n\t\ti=RSA_padding_add_none(buf,num,from,flen);\n\t\tbreak;\n\tdefault:\n\t\tRSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);\n\t\tgoto err;\n\t\t}\n\tif (i <= 0) goto err;\n\tif (BN_bin2bn(buf,num,f) == NULL) goto err;\n\tif (BN_ucmp(f, rsa->n) >= 0)\n\t\t{\n\t\tRSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);\n\t\tgoto err;\n\t\t}\n\tMONT_HELPER(rsa, ctx, n, rsa->flags & RSA_FLAG_CACHE_PUBLIC, goto err);\n\tif (!rsa->meth->bn_mod_exp(ret,f,rsa->e,rsa->n,ctx,\n\t\trsa->_method_mod_n)) goto err;\n\tj=BN_num_bytes(ret);\n\ti=BN_bn2bin(ret,&(to[num-j]));\n\tfor (k=0; k<(num-i); k++)\n\t\tto[k]=0;\n\tr=num;\nerr:\n\tif (ctx != NULL)\n\t\t{\n\t\tBN_CTX_end(ctx);\n\t\tBN_CTX_free(ctx);\n\t\t}\n\tif (buf != NULL)\n\t\t{\n\t\tOPENSSL_cleanse(buf,num);\n\t\tOPENSSL_free(buf);\n\t\t}\n\treturn(r);\n\t}', 'void BN_CTX_start(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_start", ctx);\n\tif(ctx->err_stack || ctx->too_many)\n\t\tctx->err_stack++;\n\telse if(!BN_STACK_push(&ctx->stack, ctx->used))\n\t\t{\n\t\tBNerr(BN_F_BN_CTX_GET,BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n\t\tctx->err_stack++;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'static int rsa_eay_mont_helper(BN_MONT_CTX **ptr, const BIGNUM *modulus, BN_CTX *ctx)\n\t{\n\tBN_MONT_CTX *bn_mont_ctx;\n\tif((bn_mont_ctx = BN_MONT_CTX_new()) == NULL)\n\t\treturn 0;\n\tif(!BN_MONT_CTX_set(bn_mont_ctx, modulus, ctx))\n\t\t{\n\t\tBN_MONT_CTX_free(bn_mont_ctx);\n\t\treturn 0;\n\t\t}\n\tif (*ptr == NULL)\n\t\t{\n\t\tCRYPTO_w_lock(CRYPTO_LOCK_RSA);\n\t\tif (*ptr == NULL)\n\t\t\t{\n\t\t\t*ptr = bn_mont_ctx;\n\t\t\tbn_mont_ctx = NULL;\n\t\t\t}\n\t\tCRYPTO_w_unlock(CRYPTO_LOCK_RSA);\n\t\t}\n\tif (bn_mont_ctx)\n\t\tBN_MONT_CTX_free(bn_mont_ctx);\n\treturn 1;\n\t}', 'int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)\n\t{\n\tBIGNUM Ri,*R;\n\tBN_init(&Ri);\n\tR= &(mont->RR);\n\tBN_copy(&(mont->N),mod);\n\tmont->N.neg = 0;\n#ifdef MONT_WORD\n\t\t{\n\t\tBIGNUM tmod;\n\t\tBN_ULONG buf[2];\n\t\tmont->ri=(BN_num_bits(mod)+(BN_BITS2-1))/BN_BITS2*BN_BITS2;\n\t\tBN_zero(R);\n\t\tif (!(BN_set_bit(R,BN_BITS2))) goto err;\n\t\tbuf[0]=mod->d[0];\n\t\tbuf[1]=0;\n\t\ttmod.d=buf;\n\t\ttmod.top=1;\n\t\ttmod.dmax=2;\n\t\ttmod.neg=0;\n\t\tif ((BN_mod_inverse(&Ri,R,&tmod,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tif (!BN_lshift(&Ri,&Ri,BN_BITS2)) goto err;\n\t\tif (!BN_is_zero(&Ri))\n\t\t\t{\n\t\t\tif (!BN_sub_word(&Ri,1)) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (!BN_set_word(&Ri,BN_MASK2)) goto err;\n\t\t\t}\n\t\tif (!BN_div(&Ri,NULL,&Ri,&tmod,ctx)) goto err;\n\t\tmont->n0 = (Ri.top > 0) ? Ri.d[0] : 0;\n\t\tBN_free(&Ri);\n\t\t}\n#else\n\t\t{\n\t\tmont->ri=BN_num_bits(&mont->N);\n\t\tBN_zero(R);\n\t\tif (!BN_set_bit(R,mont->ri)) goto err;\n\t\tif ((BN_mod_inverse(&Ri,R,&mont->N,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tif (!BN_lshift(&Ri,&Ri,mont->ri)) goto err;\n\t\tif (!BN_sub_word(&Ri,1)) goto err;\n\t\tif (!BN_div(&(mont->Ni),NULL,&Ri,&mont->N,ctx)) goto err;\n\t\tBN_free(&Ri);\n\t\t}\n#endif\n\tBN_zero(&(mont->RR));\n\tif (!BN_set_bit(&(mont->RR),mont->ri*2)) goto err;\n\tif (!BN_mod(&(mont->RR),&(mont->RR),&(mont->N),ctx)) goto err;\n\treturn(1);\nerr:\n\treturn(0);\n\t}', 'BIGNUM *BN_mod_inverse(BIGNUM *in,\n\tconst BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n\t{\n\tBIGNUM *A,*B,*X,*Y,*M,*D,*T,*R=NULL;\n\tBIGNUM *ret=NULL;\n\tint sign;\n\tbn_check_top(a);\n\tbn_check_top(n);\n\tBN_CTX_start(ctx);\n\tA = BN_CTX_get(ctx);\n\tB = BN_CTX_get(ctx);\n\tX = BN_CTX_get(ctx);\n\tD = BN_CTX_get(ctx);\n\tM = BN_CTX_get(ctx);\n\tY = BN_CTX_get(ctx);\n\tT = BN_CTX_get(ctx);\n\tif (T == NULL) goto err;\n\tif (in == NULL)\n\t\tR=BN_new();\n\telse\n\t\tR=in;\n\tif (R == NULL) goto err;\n\tBN_one(X);\n\tBN_zero(Y);\n\tif (BN_copy(B,a) == NULL) goto err;\n\tif (BN_copy(A,n) == NULL) goto err;\n\tA->neg = 0;\n\tif (B->neg || (BN_ucmp(B, A) >= 0))\n\t\t{\n\t\tif (!BN_nnmod(B, B, A, ctx)) goto err;\n\t\t}\n\tsign = -1;\n\tif (BN_is_odd(n) && (BN_num_bits(n) <= (BN_BITS <= 32 ? 450 : 2048)))\n\t\t{\n\t\tint shift;\n\t\twhile (!BN_is_zero(B))\n\t\t\t{\n\t\t\tshift = 0;\n\t\t\twhile (!BN_is_bit_set(B, shift))\n\t\t\t\t{\n\t\t\t\tshift++;\n\t\t\t\tif (BN_is_odd(X))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_uadd(X, X, n)) goto err;\n\t\t\t\t\t}\n\t\t\t\tif (!BN_rshift1(X, X)) goto err;\n\t\t\t\t}\n\t\t\tif (shift > 0)\n\t\t\t\t{\n\t\t\t\tif (!BN_rshift(B, B, shift)) goto err;\n\t\t\t\t}\n\t\t\tshift = 0;\n\t\t\twhile (!BN_is_bit_set(A, shift))\n\t\t\t\t{\n\t\t\t\tshift++;\n\t\t\t\tif (BN_is_odd(Y))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_uadd(Y, Y, n)) goto err;\n\t\t\t\t\t}\n\t\t\t\tif (!BN_rshift1(Y, Y)) goto err;\n\t\t\t\t}\n\t\t\tif (shift > 0)\n\t\t\t\t{\n\t\t\t\tif (!BN_rshift(A, A, shift)) goto err;\n\t\t\t\t}\n\t\t\tif (BN_ucmp(B, A) >= 0)\n\t\t\t\t{\n\t\t\t\tif (!BN_uadd(X, X, Y)) goto err;\n\t\t\t\tif (!BN_usub(B, B, A)) goto err;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (!BN_uadd(Y, Y, X)) goto err;\n\t\t\t\tif (!BN_usub(A, A, B)) goto err;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\twhile (!BN_is_zero(B))\n\t\t\t{\n\t\t\tBIGNUM *tmp;\n\t\t\tif (BN_num_bits(A) == BN_num_bits(B))\n\t\t\t\t{\n\t\t\t\tif (!BN_one(D)) goto err;\n\t\t\t\tif (!BN_sub(M,A,B)) goto err;\n\t\t\t\t}\n\t\t\telse if (BN_num_bits(A) == BN_num_bits(B) + 1)\n\t\t\t\t{\n\t\t\t\tif (!BN_lshift1(T,B)) goto err;\n\t\t\t\tif (BN_ucmp(A,T) < 0)\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_one(D)) goto err;\n\t\t\t\t\tif (!BN_sub(M,A,B)) goto err;\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_sub(M,A,T)) goto err;\n\t\t\t\t\tif (!BN_add(D,T,B)) goto err;\n\t\t\t\t\tif (BN_ucmp(A,D) < 0)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tif (!BN_set_word(D,2)) goto err;\n\t\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t\t{\n\t\t\t\t\t\tif (!BN_set_word(D,3)) goto err;\n\t\t\t\t\t\tif (!BN_sub(M,M,B)) goto err;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (!BN_div(D,M,A,B,ctx)) goto err;\n\t\t\t\t}\n\t\t\ttmp=A;\n\t\t\tA=B;\n\t\t\tB=M;\n\t\t\tif (BN_is_one(D))\n\t\t\t\t{\n\t\t\t\tif (!BN_add(tmp,X,Y)) goto err;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (BN_is_word(D,2))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_lshift1(tmp,X)) goto err;\n\t\t\t\t\t}\n\t\t\t\telse if (BN_is_word(D,4))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_lshift(tmp,X,2)) goto err;\n\t\t\t\t\t}\n\t\t\t\telse if (D->top == 1)\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_copy(tmp,X)) goto err;\n\t\t\t\t\tif (!BN_mul_word(tmp,D->d[0])) goto err;\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_mul(tmp,D,X,ctx)) goto err;\n\t\t\t\t\t}\n\t\t\t\tif (!BN_add(tmp,tmp,Y)) goto err;\n\t\t\t\t}\n\t\t\tM=Y;\n\t\t\tY=X;\n\t\t\tX=tmp;\n\t\t\tsign = -sign;\n\t\t\t}\n\t\t}\n\tif (sign < 0)\n\t\t{\n\t\tif (!BN_sub(Y,n,Y)) goto err;\n\t\t}\n\tif (BN_is_one(A))\n\t\t{\n\t\tif (!Y->neg && BN_ucmp(Y,n) < 0)\n\t\t\t{\n\t\t\tif (!BN_copy(R,Y)) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (!BN_nnmod(R,Y,n,ctx)) goto err;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_INVERSE,BN_R_NO_INVERSE);\n\t\tgoto err;\n\t\t}\n\tret=R;\nerr:\n\tif ((ret == NULL) && (in == NULL)) BN_free(R);\n\tBN_CTX_end(ctx);\n\tif (ret)\n\t\tbn_check_top(ret);\n\treturn(ret);\n\t}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n\t{\n\tif (!(BN_mod(r,m,d,ctx)))\n\t\treturn 0;\n\tif (!r->neg)\n\t\treturn 1;\n\treturn (d->neg ? BN_sub : BN_add)(r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n\t BN_CTX *ctx)\n\t{\n\tint norm_shift,i,loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tint num_n,div_n;\n\tif (dv)\n\t\tbn_check_top(dv);\n\tif (rm)\n\t\tbn_check_top(rm);\n\tbn_check_top(num);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\n\t\t}\n\tif (BN_ucmp(num,divisor) < 0)\n\t\t{\n\t\tif (rm != NULL)\n\t\t\t{ if (BN_copy(rm,num) == NULL) return(0); }\n\t\tif (dv != NULL) BN_zero(dv);\n\t\treturn(1);\n\t\t}\n\tBN_CTX_start(ctx);\n\ttmp=BN_CTX_get(ctx);\n\tsnum=BN_CTX_get(ctx);\n\tsdiv=BN_CTX_get(ctx);\n\tif (dv == NULL)\n\t\tres=BN_CTX_get(ctx);\n\telse\tres=dv;\n\tif (sdiv == NULL || res == NULL) goto err;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tif (!(BN_lshift(sdiv,divisor,norm_shift))) goto err;\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tif (!(BN_lshift(snum,num,norm_shift))) goto err;\n\tsnum->neg=0;\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\twnum.neg = 0;\n\twnum.d = &(snum->d[loop]);\n\twnum.top = div_n;\n\twnum.dmax = snum->dmax - loop;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (BN_ucmp(&wnum,sdiv) >= 0)\n\t\t{\n\t\tbn_clear_top2max(&wnum);\n\t\tbn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n\t\t*resp=1;\n\t\t}\n\telse\n\t\tres->top--;\n\tif (res->top == 0)\n\t\tres->neg = 0;\n\telse\n\t\tresp--;\n\tfor (i=0; i<loop-1; i++, wnump--, resp--)\n\t\t{\n\t\tBN_ULONG q,l0;\n#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n\t\tBN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG);\n\t\tq=bn_div_3_words(wnump,d1,d0);\n#else\n\t\tBN_ULONG n0,n1,rem=0;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\t{\n#ifdef BN_LLONG\n\t\t\tBN_ULLONG t2;\n#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n\t\t\tq=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);\n#else\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tt2 -= d1;\n\t\t\t\t}\n#else\n\t\t\tBN_ULONG t2l,t2h,ql,qh;\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#if defined(BN_UMULT_LOHI)\n\t\t\tBN_UMULT_LOHI(t2l,t2h,d1,q);\n#elif defined(BN_UMULT_HIGH)\n\t\t\tt2l = d1 * q;\n\t\t\tt2h = BN_UMULT_HIGH(d1,q);\n#else\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n#endif\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif ((t2h < rem) ||\n\t\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t\t}\n#endif\n\t\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\twnum.d--;\n\t\tif (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1))\n\t\t\t{\n\t\t\tq--;\n\t\t\tif (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n\t\t\t\t(*wnump)++;\n\t\t\t}\n\t\t*resp = q;\n\t\t}\n\tbn_correct_top(snum);\n\tif (rm != NULL)\n\t\t{\n\t\tint neg = num->neg;\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\tif (!BN_is_zero(rm))\n\t\t\trm->neg = neg;\n\t\tbn_check_top(rm);\n\t\t}\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tif (rm)\n\t\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'void BN_CTX_end(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_end", ctx);\n\tif(ctx->err_stack)\n\t\tctx->err_stack--;\n\telse\n\t\t{\n\t\tunsigned int fp = BN_STACK_pop(&ctx->stack);\n\t\tif(fp < ctx->used)\n\t\t\tBN_POOL_release(&ctx->pool, ctx->used - fp);\n\t\tctx->used = fp;\n\t\tctx->too_many = 0;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n\t{\n\treturn st->indexes[--(st->depth)];\n\t}']
|
2,675
| 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_block(BinkAudioContext *s, float **out, int use_dct)\n{\n int ch, i, j, k;\n float q, quant[25];\n int width, coeff;\n BitstreamContext *bc = &s->bc;\n if (use_dct)\n bitstream_skip(bc, 2);\n for (ch = 0; ch < s->channels; ch++) {\n FFTSample *coeffs = out[ch];\n if (s->version_b) {\n if (bitstream_bits_left(bc) < 64)\n return AVERROR_INVALIDDATA;\n coeffs[0] = av_int2float(bitstream_read(bc, 32)) * s->root;\n coeffs[1] = av_int2float(bitstream_read(bc, 32)) * s->root;\n } else {\n if (bitstream_bits_left(bc) < 58)\n return AVERROR_INVALIDDATA;\n coeffs[0] = get_float(bc) * s->root;\n coeffs[1] = get_float(bc) * s->root;\n }\n if (bitstream_bits_left(bc) < s->num_bands * 8)\n return AVERROR_INVALIDDATA;\n for (i = 0; i < s->num_bands; i++) {\n int value = bitstream_read(bc, 8);\n quant[i] = quant_table[FFMIN(value, 95)];\n }\n k = 0;\n q = quant[0];\n i = 2;\n while (i < s->frame_len) {\n if (s->version_b) {\n j = i + 16;\n } else {\n int v = bitstream_read_bit(bc);\n if (v) {\n v = bitstream_read(bc, 4);\n j = i + rle_length_tab[v] * 8;\n } else {\n j = i + 8;\n }\n }\n j = FFMIN(j, s->frame_len);\n width = bitstream_read(bc, 4);\n if (width == 0) {\n memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));\n i = j;\n while (s->bands[k] < i)\n q = quant[k++];\n } else {\n while (i < j) {\n if (s->bands[k] == i)\n q = quant[k++];\n coeff = bitstream_read(bc, width);\n if (coeff) {\n int v;\n v = bitstream_read_bit(bc);\n if (v)\n coeffs[i] = -q * coeff;\n else\n coeffs[i] = q * coeff;\n } else {\n coeffs[i] = 0.0f;\n }\n i++;\n }\n }\n }\n if (CONFIG_BINKAUDIO_DCT_DECODER && use_dct) {\n coeffs[0] /= 0.5;\n s->trans.dct.dct_calc(&s->trans.dct, coeffs);\n }\n else if (CONFIG_BINKAUDIO_RDFT_DECODER)\n s->trans.rdft.rdft_calc(&s->trans.rdft, coeffs);\n }\n for (ch = 0; ch < s->channels; ch++) {\n int j;\n int count = s->overlap_len * s->channels;\n if (!s->first) {\n j = ch;\n for (i = 0; i < s->overlap_len; i++, j += s->channels)\n out[ch][i] = (s->previous[ch][i] * (count - j) +\n out[ch][i] * j) / count;\n }\n memcpy(s->previous[ch], &out[ch][s->frame_len - s->overlap_len],\n s->overlap_len * sizeof(*s->previous[ch]));\n }\n s->first = 0;\n return 0;\n}', 'static inline uint32_t bitstream_read(BitstreamContext *bc, unsigned n)\n{\n if (!n)\n return 0;\n if (n > bc->bits_left) {\n refill_32(bc);\n if (bc->bits_left < 32)\n bc->bits_left = n;\n }\n return get_val(bc, n);\n}', 'static inline 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}']
|
2,676
| 0
|
https://github.com/openssl/openssl/blob/f305ecdac0b7048e7ef38a7196f4393fa7ceff38/crypto/bn/bn_ctx.c/#L300
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['int ec_key_simple_generate_public_key(EC_KEY *eckey)\n{\n return EC_POINT_mul(eckey->group, eckey->pub_key, eckey->priv_key, NULL,\n NULL, NULL);\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#ifndef FIPS_MODE\n BN_CTX *new_ctx = NULL;\n if (ctx == NULL)\n ctx = new_ctx = BN_CTX_secure_new();\n#endif\n if (ctx == NULL) {\n ECerr(EC_F_EC_POINTS_MUL, ERR_R_INTERNAL_ERROR);\n return 0;\n }\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 (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#ifndef FIPS_MODE\n BN_CTX_free(new_ctx);\n#endif\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 != group->order) && (scalar != NULL) && (num == 0)) {\n return ec_scalar_mul_ladder(group, r, scalar, NULL, ctx);\n }\n if ((scalar == NULL) && (num == 1) && (scalars[0] != group->order)) {\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 + 2) == NULL)\n || (bn_wexpand(lambda, group_top + 2) == 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 + 2);\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_clear_free(s);\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_start()", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG("LEAVE BN_CTX_start()", ctx);\n}', 'int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = bn_mul_fixed_top(r, a, b, ctx);\n bn_correct_top(r);\n bn_check_top(r);\n return ret;\n}', 'int bn_mul_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = 0;\n int top, al, bl;\n BIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n int i;\n#endif\n#ifdef BN_RECURSION\n BIGNUM *t = NULL;\n int j = 0, k;\n#endif\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(r);\n al = a->top;\n bl = b->top;\n if ((al == 0) || (bl == 0)) {\n BN_zero(r);\n return 1;\n }\n top = al + bl;\n BN_CTX_start(ctx);\n if ((r == a) || (r == b)) {\n if ((rr = BN_CTX_get(ctx)) == NULL)\n goto err;\n } else\n rr = r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n i = al - bl;\n#endif\n#ifdef BN_MUL_COMBA\n if (i == 0) {\n# if 0\n if (al == 4) {\n if (bn_wexpand(rr, 8) == NULL)\n goto err;\n rr->top = 8;\n bn_mul_comba4(rr->d, a->d, b->d);\n goto end;\n }\n# endif\n if (al == 8) {\n if (bn_wexpand(rr, 16) == NULL)\n goto err;\n rr->top = 16;\n bn_mul_comba8(rr->d, a->d, b->d);\n goto end;\n }\n }\n#endif\n#ifdef BN_RECURSION\n if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {\n if (i >= -1 && i <= 1) {\n if (i >= 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n }\n if (i == -1) {\n j = BN_num_bits_word((BN_ULONG)bl);\n }\n j = 1 << (j - 1);\n assert(j <= al || j <= bl);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (al > j || bl > j) {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d,\n j, al - j, bl - j, t->d);\n } else {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);\n }\n rr->top = top;\n goto end;\n }\n }\n#endif\n if (bn_wexpand(rr, top) == NULL)\n goto err;\n rr->top = top;\n bn_mul_normal(rr->d, a->d, al, b->d, bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n end:\n#endif\n rr->neg = a->neg ^ b->neg;\n rr->flags |= BN_FLG_FIXED_TOP;\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(r);\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n if (ctx == NULL)\n return;\n CTXDBG("ENTER BN_CTX_end()", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG("LEAVE BN_CTX_end()", ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
|
2,677
| 0
|
https://github.com/libav/libav/blob/4860abb116674c7be31e825db05cdcfd30f3aff2/ffmpeg.c/#L3132
|
static void new_subtitle_stream(AVFormatContext *oc)
{
AVStream *st;
AVCodecContext *subtitle_enc;
int i;
st = av_new_stream(oc, oc->nb_streams);
if (!st) {
fprintf(stderr, "Could not alloc stream\n");
av_exit(1);
}
avcodec_get_context_defaults2(st->codec, CODEC_TYPE_SUBTITLE);
bitstream_filters[nb_output_files][oc->nb_streams - 1]= subtitle_bitstream_filters;
subtitle_bitstream_filters= NULL;
subtitle_enc = st->codec;
subtitle_enc->codec_type = CODEC_TYPE_SUBTITLE;
if (subtitle_stream_copy) {
st->stream_copy = 1;
} else {
for(i=0; i<opt_name_count; i++){
char buf[256];
const AVOption *opt;
const char *str= av_get_string(avctx_opts[CODEC_TYPE_SUBTITLE], opt_names[i], &opt, buf, sizeof(buf));
if(str && (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) && (opt->flags & AV_OPT_FLAG_ENCODING_PARAM))
av_set_string(subtitle_enc, opt_names[i], str);
}
subtitle_enc->codec_id = find_codec_or_die(subtitle_codec_name, CODEC_TYPE_SUBTITLE, 1);
}
if (subtitle_language) {
av_strlcpy(st->language, subtitle_language, sizeof(st->language));
av_free(subtitle_language);
subtitle_language = NULL;
}
subtitle_disable = 0;
av_freep(&subtitle_codec_name);
subtitle_stream_copy = 0;
}
|
['static void new_subtitle_stream(AVFormatContext *oc)\n{\n AVStream *st;\n AVCodecContext *subtitle_enc;\n int i;\n st = av_new_stream(oc, oc->nb_streams);\n if (!st) {\n fprintf(stderr, "Could not alloc stream\\n");\n av_exit(1);\n }\n avcodec_get_context_defaults2(st->codec, CODEC_TYPE_SUBTITLE);\n bitstream_filters[nb_output_files][oc->nb_streams - 1]= subtitle_bitstream_filters;\n subtitle_bitstream_filters= NULL;\n subtitle_enc = st->codec;\n subtitle_enc->codec_type = CODEC_TYPE_SUBTITLE;\n if (subtitle_stream_copy) {\n st->stream_copy = 1;\n } else {\n for(i=0; i<opt_name_count; i++){\n char buf[256];\n const AVOption *opt;\n const char *str= av_get_string(avctx_opts[CODEC_TYPE_SUBTITLE], opt_names[i], &opt, buf, sizeof(buf));\n if(str && (opt->flags & AV_OPT_FLAG_SUBTITLE_PARAM) && (opt->flags & AV_OPT_FLAG_ENCODING_PARAM))\n av_set_string(subtitle_enc, opt_names[i], str);\n }\n subtitle_enc->codec_id = find_codec_or_die(subtitle_codec_name, CODEC_TYPE_SUBTITLE, 1);\n }\n if (subtitle_language) {\n av_strlcpy(st->language, subtitle_language, sizeof(st->language));\n av_free(subtitle_language);\n subtitle_language = NULL;\n }\n subtitle_disable = 0;\n av_freep(&subtitle_codec_name);\n subtitle_stream_copy = 0;\n}', 'AVStream *av_new_stream(AVFormatContext *s, int id)\n{\n AVStream *st;\n int i;\n if (s->nb_streams >= MAX_STREAMS)\n return NULL;\n st = av_mallocz(sizeof(AVStream));\n if (!st)\n return NULL;\n st->codec= avcodec_alloc_context();\n if (s->iformat) {\n st->codec->bit_rate = 0;\n }\n st->index = s->nb_streams;\n st->id = id;\n st->start_time = AV_NOPTS_VALUE;\n st->duration = AV_NOPTS_VALUE;\n st->cur_dts = AV_NOPTS_VALUE;\n st->first_dts = AV_NOPTS_VALUE;\n av_set_pts_info(st, 33, 1, 90000);\n st->last_IP_pts = AV_NOPTS_VALUE;\n for(i=0; i<MAX_REORDER_DELAY+1; i++)\n st->pts_buffer[i]= AV_NOPTS_VALUE;\n s->streams[s->nb_streams++] = st;\n return st;\n}']
|
2,678
| 0
|
https://github.com/openssl/openssl/blob/8b0d4242404f9e5da26e7594fa0864b2df4601af/crypto/bn/bn_lib.c/#L271
|
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
BN_ULONG *a = NULL;
bn_check_top(b);
if (words > (INT_MAX / (4 * BN_BITS2))) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
return NULL;
}
if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
return (NULL);
}
if (BN_get_flags(b, BN_FLG_SECURE))
a = OPENSSL_secure_zalloc(words * sizeof(*a));
else
a = OPENSSL_zalloc(words * sizeof(*a));
if (a == NULL) {
BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
return (NULL);
}
assert(b->top <= words);
if (b->top > 0)
memcpy(a, b->d, sizeof(*a) * b->top);
return a;
}
|
['int BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y,\n BN_RECP_CTX *recp, BN_CTX *ctx)\n{\n int ret = 0;\n BIGNUM *a;\n const BIGNUM *ca;\n BN_CTX_start(ctx);\n if ((a = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (y != NULL) {\n if (x == y) {\n if (!BN_sqr(a, x, ctx))\n goto err;\n } else {\n if (!BN_mul(a, x, y, ctx))\n goto err;\n }\n ca = a;\n } else\n ca = x;\n ret = BN_div_recp(NULL, r, ca, recp, ctx);\n err:\n BN_CTX_end(ctx);\n bn_check_top(r);\n return (ret);\n}', '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_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 (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}', '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}']
|
2,679
| 0
|
https://github.com/openssl/openssl/blob/a14715888bc4b5bd2b1da3f8ac7d4cabef8c9cb8/crypto/ec/ec_mult.c/#L327
|
int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
size_t num, const EC_POINT *points[], const BIGNUM *scalars[],
BN_CTX *ctx)
{
BN_CTX *new_ctx = NULL;
const EC_POINT *generator = NULL;
EC_POINT *tmp = NULL;
size_t totalnum;
size_t blocksize = 0, numblocks = 0;
size_t pre_points_per_block = 0;
size_t i, j;
int k;
int r_is_inverted = 0;
int r_is_at_infinity = 1;
size_t *wsize = NULL;
signed char **wNAF = NULL;
size_t *wNAF_len = NULL;
size_t max_len = 0;
size_t num_val;
EC_POINT **val = NULL;
EC_POINT **v;
EC_POINT ***val_sub = NULL;
const EC_PRE_COMP *pre_comp = NULL;
int num_scalar = 0;
int ret = 0;
if (group->meth != r->meth) {
ECerr(EC_F_EC_WNAF_MUL, EC_R_INCOMPATIBLE_OBJECTS);
return 0;
}
if ((scalar == NULL) && (num == 0)) {
return EC_POINT_set_to_infinity(group, r);
}
for (i = 0; i < num; i++) {
if (group->meth != points[i]->meth) {
ECerr(EC_F_EC_WNAF_MUL, EC_R_INCOMPATIBLE_OBJECTS);
return 0;
}
}
if (ctx == NULL) {
ctx = new_ctx = BN_CTX_new();
if (ctx == NULL)
goto err;
}
if (scalar != NULL) {
generator = EC_GROUP_get0_generator(group);
if (generator == NULL) {
ECerr(EC_F_EC_WNAF_MUL, EC_R_UNDEFINED_GENERATOR);
goto err;
}
pre_comp = 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:
BN_CTX_free(new_ctx);
EC_POINT_free(tmp);
OPENSSL_free(wsize);
OPENSSL_free(wNAF_len);
if (wNAF != NULL) {
signed char **w;
for (w = wNAF; *w != NULL; w++)
OPENSSL_free(*w);
OPENSSL_free(wNAF);
}
if (val != NULL) {
for (v = val; *v != NULL; v++)
EC_POINT_clear_free(*v);
OPENSSL_free(val);
}
OPENSSL_free(val_sub);
return ret;
}
|
['static int prime_field_tests(void)\n{\n BN_CTX *ctx = NULL;\n BIGNUM *p = NULL, *a = NULL, *b = NULL, *scalar3 = NULL;\n EC_GROUP *group = NULL, *tmp = NULL;\n EC_GROUP *P_160 = NULL, *P_192 = NULL, *P_224 = NULL,\n *P_256 = NULL, *P_384 = NULL, *P_521 = NULL;\n EC_POINT *P = NULL, *Q = NULL, *R = NULL;\n BIGNUM *x = NULL, *y = NULL, *z = NULL, *yplusone = NULL;\n const EC_POINT *points[4];\n const BIGNUM *scalars[4];\n unsigned char buf[100];\n size_t len, r = 0;\n int k;\n if (!TEST_ptr(ctx = BN_CTX_new())\n || !TEST_ptr(p = BN_new())\n || !TEST_ptr(a = BN_new())\n || !TEST_ptr(b = BN_new())\n || !TEST_true(BN_hex2bn(&p, "17"))\n || !TEST_true(BN_hex2bn(&a, "1"))\n || !TEST_true(BN_hex2bn(&b, "1"))\n || !TEST_ptr(group = EC_GROUP_new(EC_GFp_mont_method()))\n || !TEST_true(EC_GROUP_set_curve_GFp(group, p, a, b, ctx))\n || !TEST_ptr(tmp = EC_GROUP_new(EC_GROUP_method_of(group)))\n || !TEST_true(EC_GROUP_copy(tmp, group)))\n goto err;\n EC_GROUP_free(group);\n group = tmp;\n tmp = NULL;\n if (!TEST_true(EC_GROUP_get_curve_GFp(group, p, a, b, ctx)))\n goto err;\n TEST_info("Curve defined by Weierstrass equation");\n TEST_note(" y^2 = x^3 + a*x + b (mod p)");\n test_output_bignum("a", a);\n test_output_bignum("b", b);\n test_output_bignum("p", p);\n buf[0] = 0;\n if (!TEST_ptr(P = EC_POINT_new(group))\n || !TEST_ptr(Q = EC_POINT_new(group))\n || !TEST_ptr(R = EC_POINT_new(group))\n || !TEST_true(EC_POINT_set_to_infinity(group, P))\n || !TEST_true(EC_POINT_is_at_infinity(group, P))\n || !TEST_true(EC_POINT_oct2point(group, Q, buf, 1, ctx))\n || !TEST_true(EC_POINT_add(group, P, P, Q, ctx))\n || !TEST_true(EC_POINT_is_at_infinity(group, P))\n || !TEST_ptr(x = BN_new())\n || !TEST_ptr(y = BN_new())\n || !TEST_ptr(z = BN_new())\n || !TEST_ptr(yplusone = BN_new())\n || !TEST_true(BN_hex2bn(&x, "D"))\n || !TEST_true(EC_POINT_set_compressed_coordinates_GFp(group, Q, x, 1,\n ctx)))\n goto err;\n if (!TEST_int_gt(EC_POINT_is_on_curve(group, Q, ctx), 0)) {\n if (!TEST_true(EC_POINT_get_affine_coordinates_GFp(group, Q, x, y,\n ctx)))\n goto err;\n TEST_info("Point is not on curve");\n test_output_bignum("x", x);\n test_output_bignum("y", y);\n goto err;\n }\n TEST_note("A cyclic subgroup:");\n k = 100;\n do {\n if (!TEST_int_ne(k--, 0))\n goto err;\n if (EC_POINT_is_at_infinity(group, P)) {\n TEST_note(" point at infinity");\n } else {\n if (!TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y,\n ctx)))\n goto err;\n test_output_bignum("x", x);\n test_output_bignum("y", y);\n }\n if (!TEST_true(EC_POINT_copy(R, P))\n || !TEST_true(EC_POINT_add(group, P, P, Q, ctx)))\n goto err;\n } while (!EC_POINT_is_at_infinity(group, P));\n if (!TEST_true(EC_POINT_add(group, P, Q, R, ctx))\n || !TEST_true(EC_POINT_is_at_infinity(group, P)))\n goto err;\n len =\n EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf,\n sizeof(buf), ctx);\n if (!TEST_size_t_ne(len, 0)\n || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))\n || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))\n goto err;\n test_output_memory("Generator as octet string, compressed form:",\n buf, len);\n len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED,\n buf, sizeof(buf), ctx);\n if (!TEST_size_t_ne(len, 0)\n || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))\n || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))\n goto err;\n test_output_memory("Generator as octet string, uncompressed form:",\n buf, len);\n len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID,\n buf, sizeof(buf), ctx);\n if (!TEST_size_t_ne(len, 0)\n || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))\n || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))\n goto err;\n test_output_memory("Generator as octet string, hybrid form:",\n buf, len);\n if (!TEST_true(EC_POINT_get_Jprojective_coordinates_GFp(group, R, x, y, z,\n ctx)))\n goto err;\n TEST_info("A representation of the inverse of that generator in");\n TEST_note("Jacobian projective coordinates");\n test_output_bignum("x", x);\n test_output_bignum("y", y);\n test_output_bignum("z", z);\n if (!TEST_true(EC_POINT_invert(group, P, ctx))\n || !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx))\n || !TEST_true(BN_hex2bn(&p, "FFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF"))\n || !TEST_int_eq(1, BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))\n || !TEST_true(BN_hex2bn(&a, "FFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC"))\n || !TEST_true(BN_hex2bn(&b, "1C97BEFC"\n "54BD7A8B65ACF89F81D4D4ADC565FA45"))\n || !TEST_true(EC_GROUP_set_curve_GFp(group, p, a, b, ctx))\n || !TEST_true(BN_hex2bn(&x, "4A96B568"\n "8EF573284664698968C38BB913CBFC82"))\n || !TEST_true(BN_hex2bn(&y, "23a62855"\n "3168947d59dcc912042351377ac5fb32"))\n || !TEST_true(BN_add(yplusone, y, BN_value_one()))\n || !TEST_false(EC_POINT_set_affine_coordinates_GFp(group, P, x,\n yplusone, ctx))\n || !TEST_true(EC_POINT_set_affine_coordinates_GFp(group, P, x, y, ctx))\n || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)\n || !TEST_true(BN_hex2bn(&z, "0100000000"\n "000000000001F4C8F927AED3CA752257"))\n || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))\n || !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)))\n goto err;\n TEST_info("SEC2 curve secp160r1 -- Generator");\n test_output_bignum("x", x);\n test_output_bignum("y", y);\n if (!TEST_true(BN_hex2bn(&z, "23a62855"\n "3168947d59dcc912042351377ac5fb32"))\n || !TEST_BN_eq(y, z)\n || !TEST_int_eq(EC_GROUP_get_degree(group), 160)\n || !group_order_tests(group)\n || !TEST_ptr(P_160 = EC_GROUP_new(EC_GROUP_method_of(group)))\n || !TEST_true(EC_GROUP_copy(P_160, group))\n || !TEST_true(BN_hex2bn(&p, "FFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"))\n || !TEST_int_eq(1, BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))\n || !TEST_true(BN_hex2bn(&a, "FFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC"))\n || !TEST_true(BN_hex2bn(&b, "64210519E59C80E7"\n "0FA7E9AB72243049FEB8DEECC146B9B1"))\n || !TEST_true(EC_GROUP_set_curve_GFp(group, p, a, b, ctx))\n || !TEST_true(BN_hex2bn(&x, "188DA80EB03090F6"\n "7CBF20EB43A18800F4FF0AFD82FF1012"))\n || !TEST_true(EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1,\n ctx))\n || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)\n || !TEST_true(BN_hex2bn(&z, "FFFFFFFFFFFFFFFF"\n "FFFFFFFF99DEF836146BC9B1B4D22831"))\n || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))\n || !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)))\n goto err;\n TEST_info("NIST curve P-192 -- Generator");\n test_output_bignum("x", x);\n test_output_bignum("y", y);\n if (!TEST_true(BN_hex2bn(&z, "07192B95FFC8DA78"\n "631011ED6B24CDD573F977A11E794811"))\n || !TEST_BN_eq(y, z)\n || !TEST_true(BN_add(yplusone, y, BN_value_one()))\n || !TEST_false(EC_POINT_set_affine_coordinates_GFp(group, P, x,\n yplusone, ctx))\n || !TEST_int_eq(EC_GROUP_get_degree(group), 192)\n || !group_order_tests(group)\n || !TEST_ptr(P_192 = EC_GROUP_new(EC_GROUP_method_of(group)))\n || !TEST_true(EC_GROUP_copy(P_192, group))\n || !TEST_true(BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFF000000000000000000000001"))\n || !TEST_int_eq(1, BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))\n || !TEST_true(BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE"))\n || !TEST_true(BN_hex2bn(&b, "B4050A850C04B3ABF5413256"\n "5044B0B7D7BFD8BA270B39432355FFB4"))\n || !TEST_true(EC_GROUP_set_curve_GFp(group, p, a, b, ctx))\n || !TEST_true(BN_hex2bn(&x, "B70E0CBD6BB4BF7F321390B9"\n "4A03C1D356C21122343280D6115C1D21"))\n || !TEST_true(EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0,\n ctx))\n || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)\n || !TEST_true(BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFF16A2E0B8F03E13DD29455C5C2A3D"))\n || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))\n || !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)))\n goto err;\n TEST_info("NIST curve P-224 -- Generator");\n test_output_bignum("x", x);\n test_output_bignum("y", y);\n if (!TEST_true(BN_hex2bn(&z, "BD376388B5F723FB4C22DFE6"\n "CD4375A05A07476444D5819985007E34"))\n || !TEST_BN_eq(y, z)\n || !TEST_true(BN_add(yplusone, y, BN_value_one()))\n || !TEST_false(EC_POINT_set_affine_coordinates_GFp(group, P, x,\n yplusone, ctx))\n || !TEST_int_eq(EC_GROUP_get_degree(group), 224)\n || !group_order_tests(group)\n || !TEST_ptr(P_224 = EC_GROUP_new(EC_GROUP_method_of(group)))\n || !TEST_true(EC_GROUP_copy(P_224, group))\n || !TEST_true(BN_hex2bn(&p, "FFFFFFFF000000010000000000000000"\n "00000000FFFFFFFFFFFFFFFFFFFFFFFF"))\n || !TEST_int_eq(1, BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))\n || !TEST_true(BN_hex2bn(&a, "FFFFFFFF000000010000000000000000"\n "00000000FFFFFFFFFFFFFFFFFFFFFFFC"))\n || !TEST_true(BN_hex2bn(&b, "5AC635D8AA3A93E7B3EBBD55769886BC"\n "651D06B0CC53B0F63BCE3C3E27D2604B"))\n || !TEST_true(EC_GROUP_set_curve_GFp(group, p, a, b, ctx))\n || !TEST_true(BN_hex2bn(&x, "6B17D1F2E12C4247F8BCE6E563A440F2"\n "77037D812DEB33A0F4A13945D898C296"))\n || !TEST_true(EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1,\n ctx))\n || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)\n || !TEST_true(BN_hex2bn(&z, "FFFFFFFF00000000FFFFFFFFFFFFFFFF"\n "BCE6FAADA7179E84F3B9CAC2FC632551"))\n || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))\n || !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)))\n goto err;\n TEST_info("NIST curve P-256 -- Generator");\n test_output_bignum("x", x);\n test_output_bignum("y", y);\n if (!TEST_true(BN_hex2bn(&z, "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"\n "2BCE33576B315ECECBB6406837BF51F5"))\n || !TEST_BN_eq(y, z)\n || !TEST_true(BN_add(yplusone, y, BN_value_one()))\n || !TEST_false(EC_POINT_set_affine_coordinates_GFp(group, P, x,\n yplusone, ctx))\n || !TEST_int_eq(EC_GROUP_get_degree(group), 256)\n || !group_order_tests(group)\n || !TEST_ptr(P_256 = EC_GROUP_new(EC_GROUP_method_of(group)))\n || !TEST_true(EC_GROUP_copy(P_256, group))\n || !TEST_true(BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE"\n "FFFFFFFF0000000000000000FFFFFFFF"))\n || !TEST_int_eq(1, BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))\n || !TEST_true(BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE"\n "FFFFFFFF0000000000000000FFFFFFFC"))\n || !TEST_true(BN_hex2bn(&b, "B3312FA7E23EE7E4988E056BE3F82D19"\n "181D9C6EFE8141120314088F5013875A"\n "C656398D8A2ED19D2A85C8EDD3EC2AEF"))\n || !TEST_true(EC_GROUP_set_curve_GFp(group, p, a, b, ctx))\n || !TEST_true(BN_hex2bn(&x, "AA87CA22BE8B05378EB1C71EF320AD74"\n "6E1D3B628BA79B9859F741E082542A38"\n "5502F25DBF55296C3A545E3872760AB7"))\n || !TEST_true(EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1,\n ctx))\n || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)\n || !TEST_true(BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFC7634D81F4372DDF"\n "581A0DB248B0A77AECEC196ACCC52973"))\n || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))\n || !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)))\n goto err;\n TEST_info("NIST curve P-384 -- Generator");\n test_output_bignum("x", x);\n test_output_bignum("y", y);\n if (!TEST_true(BN_hex2bn(&z, "3617DE4A96262C6F5D9E98BF9292DC29"\n "F8F41DBD289A147CE9DA3113B5F0B8C0"\n "0A60B1CE1D7E819D7A431D7C90EA0E5F"))\n || !TEST_BN_eq(y, z)\n || !TEST_true(BN_add(yplusone, y, BN_value_one()))\n || !TEST_false(EC_POINT_set_affine_coordinates_GFp(group, P, x,\n yplusone, ctx))\n || !TEST_int_eq(EC_GROUP_get_degree(group), 384)\n || !group_order_tests(group)\n || !TEST_ptr(P_384 = EC_GROUP_new(EC_GROUP_method_of(group)))\n || !TEST_true(EC_GROUP_copy(P_384, group))\n || !TEST_true(BN_hex2bn(&p, "1FF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"))\n || !TEST_int_eq(1, BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))\n || !TEST_true(BN_hex2bn(&a, "1FF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC"))\n || !TEST_true(BN_hex2bn(&b, "051"\n "953EB9618E1C9A1F929A21A0B68540EE"\n "A2DA725B99B315F3B8B489918EF109E1"\n "56193951EC7E937B1652C0BD3BB1BF07"\n "3573DF883D2C34F1EF451FD46B503F00"))\n || !TEST_true(EC_GROUP_set_curve_GFp(group, p, a, b, ctx))\n || !TEST_true(BN_hex2bn(&x, "C6"\n "858E06B70404E9CD9E3ECB662395B442"\n "9C648139053FB521F828AF606B4D3DBA"\n "A14B5E77EFE75928FE1DC127A2FFA8DE"\n "3348B3C1856A429BF97E7E31C2E5BD66"))\n || !TEST_true(EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0,\n ctx))\n || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)\n || !TEST_true(BN_hex2bn(&z, "1FF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA"\n "51868783BF2F966B7FCC0148F709A5D0"\n "3BB5C9B8899C47AEBB6FB71E91386409"))\n || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))\n || !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)))\n goto err;\n TEST_info("NIST curve P-521 -- Generator");\n test_output_bignum("x", x);\n test_output_bignum("y", y);\n if (!TEST_true(BN_hex2bn(&z, "118"\n "39296A789A3BC0045C8A5FB42C7D1BD9"\n "98F54449579B446817AFBD17273E662C"\n "97EE72995EF42640C550B9013FAD0761"\n "353C7086A272C24088BE94769FD16650"))\n || !TEST_BN_eq(y, z)\n || !TEST_true(BN_add(yplusone, y, BN_value_one()))\n || !TEST_false(EC_POINT_set_affine_coordinates_GFp(group, P, x,\n yplusone, ctx))\n || !TEST_int_eq(EC_GROUP_get_degree(group), 521)\n || !group_order_tests(group)\n || !TEST_ptr(P_521 = EC_GROUP_new(EC_GROUP_method_of(group)))\n || !TEST_true(EC_GROUP_copy(P_521, group))\n || !TEST_true(EC_POINT_set_affine_coordinates_GFp(group, P, x, y, ctx))\n || !TEST_true(EC_POINT_copy(Q, P))\n || !TEST_false(EC_POINT_is_at_infinity(group, Q))\n || !TEST_true(EC_POINT_dbl(group, P, P, ctx))\n || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)\n || !TEST_true(EC_POINT_invert(group, Q, ctx))\n || !TEST_true(EC_POINT_add(group, R, P, Q, ctx))\n || !TEST_true(EC_POINT_add(group, R, R, Q, ctx))\n || !TEST_true(EC_POINT_is_at_infinity(group, R))\n || !TEST_false(EC_POINT_is_at_infinity(group, Q)))\n goto err;\n points[0] = Q;\n points[1] = Q;\n points[2] = Q;\n points[3] = Q;\n if (!TEST_true(EC_GROUP_get_order(group, z, ctx))\n || !TEST_true(BN_add(y, z, BN_value_one()))\n || !TEST_BN_even(y)\n || !TEST_true(BN_rshift1(y, y)))\n goto err;\n scalars[0] = y;\n scalars[1] = y;\n TEST_note("combined multiplication ...");\n if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))\n || !TEST_true(EC_POINTs_mul(group, R, z, 2, points, scalars, ctx))\n || !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx))\n || !TEST_int_eq(0, EC_POINT_cmp(group, R, Q, ctx))\n || !TEST_true(BN_rand(y, BN_num_bits(y), 0, 0))\n || !TEST_true(BN_add(z, z, y)))\n goto err;\n BN_set_negative(z, 1);\n scalars[0] = y;\n scalars[1] = z;\n if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))\n || !TEST_true(EC_POINT_is_at_infinity(group, P))\n || !TEST_true(BN_rand(x, BN_num_bits(y) - 1, 0, 0))\n || !TEST_true(BN_add(z, x, y)))\n goto err;\n BN_set_negative(z, 1);\n scalars[0] = x;\n scalars[1] = y;\n scalars[2] = z;\n if (!TEST_ptr(scalar3 = BN_new()))\n goto err;\n BN_zero(scalar3);\n scalars[3] = scalar3;\n if (!TEST_true(EC_POINTs_mul(group, P, NULL, 4, points, scalars, ctx))\n || !TEST_true(EC_POINT_is_at_infinity(group, P)))\n goto err;\n TEST_note(" ok\\n");\n r = 1;\nerr:\n BN_CTX_free(ctx);\n BN_free(p);\n BN_free(a);\n BN_free(b);\n EC_GROUP_free(group);\n EC_GROUP_free(tmp);\n EC_POINT_free(P);\n EC_POINT_free(Q);\n EC_POINT_free(R);\n BN_free(x);\n BN_free(y);\n BN_free(z);\n BN_free(yplusone);\n BN_free(scalar3);\n EC_GROUP_free(P_160);\n EC_GROUP_free(P_192);\n EC_GROUP_free(P_224);\n EC_GROUP_free(P_256);\n EC_GROUP_free(P_384);\n EC_GROUP_free(P_521);\n return r;\n}', 'int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,\n size_t num, const EC_POINT *points[],\n const BIGNUM *scalars[], BN_CTX *ctx)\n{\n if (group->meth->mul == 0)\n return ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);\n return group->meth->mul(group, r, scalar, num, points, scalars, ctx);\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 BN_CTX *new_ctx = NULL;\n const EC_POINT *generator = NULL;\n EC_POINT *tmp = NULL;\n size_t totalnum;\n size_t blocksize = 0, numblocks = 0;\n size_t pre_points_per_block = 0;\n size_t i, j;\n int k;\n int r_is_inverted = 0;\n int r_is_at_infinity = 1;\n size_t *wsize = NULL;\n signed char **wNAF = NULL;\n size_t *wNAF_len = NULL;\n size_t max_len = 0;\n size_t num_val;\n EC_POINT **val = NULL;\n EC_POINT **v;\n EC_POINT ***val_sub = NULL;\n const EC_PRE_COMP *pre_comp = NULL;\n int num_scalar = 0;\n int ret = 0;\n if (group->meth != r->meth) {\n ECerr(EC_F_EC_WNAF_MUL, EC_R_INCOMPATIBLE_OBJECTS);\n return 0;\n }\n if ((scalar == NULL) && (num == 0)) {\n return EC_POINT_set_to_infinity(group, r);\n }\n for (i = 0; i < num; i++) {\n if (group->meth != points[i]->meth) {\n ECerr(EC_F_EC_WNAF_MUL, EC_R_INCOMPATIBLE_OBJECTS);\n return 0;\n }\n }\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n goto err;\n }\n if (scalar != NULL) {\n generator = EC_GROUP_get0_generator(group);\n if (generator == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, EC_R_UNDEFINED_GENERATOR);\n goto err;\n }\n pre_comp = 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 BN_CTX_free(new_ctx);\n EC_POINT_free(tmp);\n OPENSSL_free(wsize);\n OPENSSL_free(wNAF_len);\n if (wNAF != NULL) {\n signed char **w;\n for (w = wNAF; *w != NULL; w++)\n OPENSSL_free(*w);\n OPENSSL_free(wNAF);\n }\n if (val != NULL) {\n for (v = val; *v != NULL; v++)\n EC_POINT_clear_free(*v);\n OPENSSL_free(val);\n }\n OPENSSL_free(val_sub);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n 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}']
|
2,680
| 0
|
https://github.com/openssl/openssl/blob/b2a4e959c917430acc2ef5e7f5aa0d6e15a91fd6/apps/speed.c/#L2442
|
static int do_multi(int multi)
{
int n;
int fd[2];
int *fds;
static char sep[]=":";
fds=malloc(multi*sizeof *fds);
for(n=0 ; n < multi ; ++n)
{
pipe(fd);
if(fork())
{
close(fd[1]);
fds[n]=fd[0];
}
else
{
close(fd[0]);
close(1);
dup(fd[1]);
close(fd[1]);
mr=1;
usertime=0;
return 0;
}
printf("Forked child %d\n",n);
}
for(n=0 ; n < multi ; ++n)
{
FILE *f;
char buf[1024];
char *p;
f=fdopen(fds[n],"r");
while(fgets(buf,sizeof buf,f))
{
p=strchr(buf,'\n');
if(p)
*p='\0';
if(buf[0] != '+')
{
fprintf(stderr,"Don't understand line '%s' from child %d\n",
buf,n);
continue;
}
printf("Got: %s from %d\n",buf,n);
if(!strncmp(buf,"+F:",3))
{
int alg;
int j;
p=buf+3;
alg=atoi(sstrsep(&p,sep));
sstrsep(&p,sep);
for(j=0 ; j < SIZE_NUM ; ++j)
results[alg][j]+=atof(sstrsep(&p,sep));
}
else if(!strncmp(buf,"+F2:",4))
{
int k;
double d;
p=buf+4;
k=atoi(sstrsep(&p,sep));
sstrsep(&p,sep);
d=atof(sstrsep(&p,sep));
if(n)
rsa_results[k][0]=1/(1/rsa_results[k][0]+1/d);
else
rsa_results[k][0]=d;
d=atof(sstrsep(&p,sep));
if(n)
rsa_results[k][1]=1/(1/rsa_results[k][1]+1/d);
else
rsa_results[k][1]=d;
}
else if(!strncmp(buf,"+F2:",4))
{
int k;
double d;
p=buf+4;
k=atoi(sstrsep(&p,sep));
sstrsep(&p,sep);
d=atof(sstrsep(&p,sep));
if(n)
rsa_results[k][0]=1/(1/rsa_results[k][0]+1/d);
else
rsa_results[k][0]=d;
d=atof(sstrsep(&p,sep));
if(n)
rsa_results[k][1]=1/(1/rsa_results[k][1]+1/d);
else
rsa_results[k][1]=d;
}
else if(!strncmp(buf,"+F3:",4))
{
int k;
double d;
p=buf+4;
k=atoi(sstrsep(&p,sep));
sstrsep(&p,sep);
d=atof(sstrsep(&p,sep));
if(n)
dsa_results[k][0]=1/(1/dsa_results[k][0]+1/d);
else
dsa_results[k][0]=d;
d=atof(sstrsep(&p,sep));
if(n)
dsa_results[k][1]=1/(1/dsa_results[k][1]+1/d);
else
dsa_results[k][1]=d;
}
#ifndef OPENSSL_NO_ECDSA
else if(!strncmp(buf,"+F4:",4))
{
int k;
double d;
p=buf+4;
k=atoi(sstrsep(&p,sep));
sstrsep(&p,sep);
d=atof(sstrsep(&p,sep));
if(n)
ecdsa_results[k][0]=1/(1/ecdsa_results[k][0]+1/d);
else
ecdsa_results[k][0]=d;
d=atof(sstrsep(&p,sep));
if(n)
ecdsa_results[k][1]=1/(1/ecdsa_results[k][1]+1/d);
else
ecdsa_results[k][1]=d;
}
#endif
#ifndef OPENSSL_NO_ECDH
else if(!strncmp(buf,"+F5:",4))
{
int k;
double d;
p=buf+4;
k=atoi(sstrsep(&p,sep));
sstrsep(&p,sep);
d=atof(sstrsep(&p,sep));
if(n)
ecdh_results[k][0]=1/(1/ecdh_results[k][0]+1/d);
else
ecdh_results[k][0]=d;
}
#endif
else if(!strncmp(buf,"+H:",3))
{
}
else
fprintf(stderr,"Unknown type '%s' from child %d\n",buf,n);
}
}
return 1;
}
|
['static int do_multi(int multi)\n\t{\n\tint n;\n\tint fd[2];\n\tint *fds;\n\tstatic char sep[]=":";\n\tfds=malloc(multi*sizeof *fds);\n\tfor(n=0 ; n < multi ; ++n)\n\t\t{\n\t\tpipe(fd);\n\t\tif(fork())\n\t\t\t{\n\t\t\tclose(fd[1]);\n\t\t\tfds[n]=fd[0];\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tclose(fd[0]);\n\t\t\tclose(1);\n\t\t\tdup(fd[1]);\n\t\t\tclose(fd[1]);\n\t\t\tmr=1;\n\t\t\tusertime=0;\n\t\t\treturn 0;\n\t\t\t}\n\t\tprintf("Forked child %d\\n",n);\n\t\t}\n\tfor(n=0 ; n < multi ; ++n)\n\t\t{\n\t\tFILE *f;\n\t\tchar buf[1024];\n\t\tchar *p;\n\t\tf=fdopen(fds[n],"r");\n\t\twhile(fgets(buf,sizeof buf,f))\n\t\t\t{\n\t\t\tp=strchr(buf,\'\\n\');\n\t\t\tif(p)\n\t\t\t\t*p=\'\\0\';\n\t\t\tif(buf[0] != \'+\')\n\t\t\t\t{\n\t\t\t\tfprintf(stderr,"Don\'t understand line \'%s\' from child %d\\n",\n\t\t\t\t\t\tbuf,n);\n\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\tprintf("Got: %s from %d\\n",buf,n);\n\t\t\tif(!strncmp(buf,"+F:",3))\n\t\t\t\t{\n\t\t\t\tint alg;\n\t\t\t\tint j;\n\t\t\t\tp=buf+3;\n\t\t\t\talg=atoi(sstrsep(&p,sep));\n\t\t\t\tsstrsep(&p,sep);\n\t\t\t\tfor(j=0 ; j < SIZE_NUM ; ++j)\n\t\t\t\t\tresults[alg][j]+=atof(sstrsep(&p,sep));\n\t\t\t\t}\n\t\t\telse if(!strncmp(buf,"+F2:",4))\n\t\t\t\t{\n\t\t\t\tint k;\n\t\t\t\tdouble d;\n\t\t\t\tp=buf+4;\n\t\t\t\tk=atoi(sstrsep(&p,sep));\n\t\t\t\tsstrsep(&p,sep);\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\trsa_results[k][0]=1/(1/rsa_results[k][0]+1/d);\n\t\t\t\telse\n\t\t\t\t\trsa_results[k][0]=d;\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\trsa_results[k][1]=1/(1/rsa_results[k][1]+1/d);\n\t\t\t\telse\n\t\t\t\t\trsa_results[k][1]=d;\n\t\t\t\t}\n\t\t\telse if(!strncmp(buf,"+F2:",4))\n\t\t\t\t{\n\t\t\t\tint k;\n\t\t\t\tdouble d;\n\t\t\t\tp=buf+4;\n\t\t\t\tk=atoi(sstrsep(&p,sep));\n\t\t\t\tsstrsep(&p,sep);\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\trsa_results[k][0]=1/(1/rsa_results[k][0]+1/d);\n\t\t\t\telse\n\t\t\t\t\trsa_results[k][0]=d;\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\trsa_results[k][1]=1/(1/rsa_results[k][1]+1/d);\n\t\t\t\telse\n\t\t\t\t\trsa_results[k][1]=d;\n\t\t\t\t}\n\t\t\telse if(!strncmp(buf,"+F3:",4))\n\t\t\t\t{\n\t\t\t\tint k;\n\t\t\t\tdouble d;\n\t\t\t\tp=buf+4;\n\t\t\t\tk=atoi(sstrsep(&p,sep));\n\t\t\t\tsstrsep(&p,sep);\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\tdsa_results[k][0]=1/(1/dsa_results[k][0]+1/d);\n\t\t\t\telse\n\t\t\t\t\tdsa_results[k][0]=d;\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\tdsa_results[k][1]=1/(1/dsa_results[k][1]+1/d);\n\t\t\t\telse\n\t\t\t\t\tdsa_results[k][1]=d;\n\t\t\t\t}\n#ifndef OPENSSL_NO_ECDSA\n\t\t\telse if(!strncmp(buf,"+F4:",4))\n\t\t\t\t{\n\t\t\t\tint k;\n\t\t\t\tdouble d;\n\t\t\t\tp=buf+4;\n\t\t\t\tk=atoi(sstrsep(&p,sep));\n\t\t\t\tsstrsep(&p,sep);\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\tecdsa_results[k][0]=1/(1/ecdsa_results[k][0]+1/d);\n\t\t\t\telse\n\t\t\t\t\tecdsa_results[k][0]=d;\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\tecdsa_results[k][1]=1/(1/ecdsa_results[k][1]+1/d);\n\t\t\t\telse\n\t\t\t\t\tecdsa_results[k][1]=d;\n\t\t\t\t}\n#endif\n#ifndef OPENSSL_NO_ECDH\n\t\t\telse if(!strncmp(buf,"+F5:",4))\n\t\t\t\t{\n\t\t\t\tint k;\n\t\t\t\tdouble d;\n\t\t\t\tp=buf+4;\n\t\t\t\tk=atoi(sstrsep(&p,sep));\n\t\t\t\tsstrsep(&p,sep);\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\tecdh_results[k][0]=1/(1/ecdh_results[k][0]+1/d);\n\t\t\t\telse\n\t\t\t\t\tecdh_results[k][0]=d;\n\t\t\t\t}\n#endif\n\t\t\telse if(!strncmp(buf,"+H:",3))\n\t\t\t\t{\n\t\t\t\t}\n\t\t\telse\n\t\t\t\tfprintf(stderr,"Unknown type \'%s\' from child %d\\n",buf,n);\n\t\t\t}\n\t\t}\n\treturn 1;\n\t}']
|
2,681
| 0
|
https://github.com/libav/libav/blob/47399ccdfd93d337c96c76fbf591f0e3637131ef/libavcodec/bitstream.h/#L138
|
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 decode_subframe(FLACContext *s, int channel)\n{\n int32_t *decoded = s->decoded[channel];\n int type, wasted = 0;\n int bps = s->bps;\n int i, tmp, ret;\n if (channel == 0) {\n if (s->ch_mode == FLAC_CHMODE_RIGHT_SIDE)\n bps++;\n } else {\n if (s->ch_mode == FLAC_CHMODE_LEFT_SIDE || s->ch_mode == FLAC_CHMODE_MID_SIDE)\n bps++;\n }\n if (bitstream_read_bit(&s->bc)) {\n av_log(s->avctx, AV_LOG_ERROR, "invalid subframe padding\\n");\n return AVERROR_INVALIDDATA;\n }\n type = bitstream_read(&s->bc, 6);\n if (bitstream_read_bit(&s->bc)) {\n int left = bitstream_bits_left(&s->bc);\n wasted = 1;\n if ( left < 0 ||\n (left < bps && !bitstream_peek(&s->bc, left)) ||\n !bitstream_peek(&s->bc, bps)) {\n av_log(s->avctx, AV_LOG_ERROR,\n "Invalid number of wasted bits > available bits (%d) - left=%d\\n",\n bps, left);\n return AVERROR_INVALIDDATA;\n }\n while (!bitstream_read_bit(&s->bc))\n wasted++;\n bps -= wasted;\n }\n if (bps > 32) {\n avpriv_report_missing_feature(s->avctx, "Decorrelated bit depth > 32");\n return AVERROR_PATCHWELCOME;\n }\n if (type == 0) {\n tmp = bitstream_read_signed(&s->bc, bps);\n for (i = 0; i < s->blocksize; i++)\n decoded[i] = tmp;\n } else if (type == 1) {\n for (i = 0; i < s->blocksize; i++)\n decoded[i] = bitstream_read_signed(&s->bc, bps);\n } else if ((type >= 8) && (type <= 12)) {\n if ((ret = decode_subframe_fixed(s, decoded, type & ~0x8, bps)) < 0)\n return ret;\n } else if (type >= 32) {\n if ((ret = decode_subframe_lpc(s, decoded, (type & ~0x20)+1, bps)) < 0)\n return ret;\n } else {\n av_log(s->avctx, AV_LOG_ERROR, "invalid coding type\\n");\n return AVERROR_INVALIDDATA;\n }\n if (wasted) {\n int i;\n for (i = 0; i < s->blocksize; i++)\n decoded[i] <<= wasted;\n }\n return 0;\n}', 'static inline uint32_t bitstream_read(BitstreamContext *bc, unsigned n)\n{\n if (!n)\n return 0;\n if (n > bc->bits_left) {\n refill_32(bc);\n if (bc->bits_left < 32)\n bc->bits_left = n;\n }\n return get_val(bc, n);\n}', 'static inline 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}']
|
2,682
| 0
|
https://github.com/libav/libav/blob/1efa772e20be5869817b2370a557bb14e7ce2fff/libavutil/mem.c/#L145
|
void av_free(void *ptr)
{
#if CONFIG_MEMALIGN_HACK
if (ptr)
free((char*)ptr - ((char*)ptr)[-1]);
#else
free(ptr);
#endif
}
|
['static int swf_write_trailer(AVFormatContext *s)\n{\n SWFContext *swf = s->priv_data;\n AVIOContext *pb = s->pb;\n AVCodecContext *enc, *video_enc;\n int file_size, i;\n video_enc = NULL;\n for(i=0;i<s->nb_streams;i++) {\n enc = s->streams[i]->codec;\n if (enc->codec_type == AVMEDIA_TYPE_VIDEO)\n video_enc = enc;\n else\n av_fifo_free(swf->audio_fifo);\n }\n put_swf_tag(s, TAG_END);\n put_swf_end_tag(s);\n put_flush_packet(s->pb);\n if (!url_is_streamed(s->pb) && video_enc) {\n file_size = url_ftell(pb);\n url_fseek(pb, 4, SEEK_SET);\n put_le32(pb, file_size);\n url_fseek(pb, swf->duration_pos, SEEK_SET);\n put_le16(pb, swf->video_frame_number);\n url_fseek(pb, swf->vframes_pos, SEEK_SET);\n put_le16(pb, swf->video_frame_number);\n url_fseek(pb, file_size, SEEK_SET);\n }\n return 0;\n}', 'void av_fifo_free(AVFifoBuffer *f)\n{\n if(f){\n av_free(f->buffer);\n av_free(f);\n }\n}', 'void av_free(void *ptr)\n{\n#if CONFIG_MEMALIGN_HACK\n if (ptr)\n free((char*)ptr - ((char*)ptr)[-1]);\n#else\n free(ptr);\n#endif\n}']
|
2,683
| 0
|
https://github.com/openssl/openssl/blob/9b95f1df4b575daa8e59722454202fd6dc7027b2/crypto/bn/bn_add.c/#L227
|
int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
{
int max,min;
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);
if (a->top < b->top)
{
BNerr(BN_F_BN_USUB,BN_R_ARG2_LT_ARG3);
return(0);
}
max=a->top;
min=b->top;
if (bn_wexpand(r,max) == NULL) return(0);
ap=a->d;
bp=b->d;
rp=r->d;
#if 1
carry=0;
for (i=0; i<min; 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;
i=min;
#endif
if (carry)
{
while (i < max)
{
i++;
t1= *(ap++);
t2=(t1-1)&BN_MASK2;
*(rp++)=t2;
if (t1 > t2) break;
}
}
#if 0
memcpy(rp,ap,sizeof(*rp)*(max-i));
#else
if (rp != ap)
{
for (;;)
{
if (i++ >= max) break;
rp[0]=ap[0];
if (i++ >= max) break;
rp[1]=ap[1];
if (i++ >= max) break;
rp[2]=ap[2];
if (i++ >= max) break;
rp[3]=ap[3];
rp+=4;
ap+=4;
}
}
#endif
r->top=max;
bn_fix_top(r);
return(1);
}
|
['int BN_mod_mul_reciprocal(BIGNUM *r, BIGNUM *x, BIGNUM *y, BN_RECP_CTX *recp,\n\t BN_CTX *ctx)\n\t{\n\tint ret=0;\n\tBIGNUM *a;\n\tBN_CTX_start(ctx);\n\tif ((a = BN_CTX_get(ctx)) == NULL) goto err;\n\tif (y != NULL)\n\t\t{\n\t\tif (x == y)\n\t\t\t{ if (!BN_sqr(a,x,ctx)) goto err; }\n\t\telse\n\t\t\t{ if (!BN_mul(a,x,y,ctx)) goto err; }\n\t\t}\n\telse\n\t\ta=x;\n\tBN_div_recp(NULL,r,a,recp,ctx);\n\tret=1;\nerr:\n\tBN_CTX_end(ctx);\n\treturn(ret);\n\t}', 'int BN_div_recp(BIGNUM *dv, BIGNUM *rem, BIGNUM *m, BN_RECP_CTX *recp,\n\t BN_CTX *ctx)\n\t{\n\tint i,j,ret=0;\n\tBIGNUM *a,*b,*d,*r;\n\tBN_CTX_start(ctx);\n\ta=BN_CTX_get(ctx);\n\tb=BN_CTX_get(ctx);\n\tif (dv != NULL)\n\t\td=dv;\n\telse\n\t\td=BN_CTX_get(ctx);\n\tif (rem != NULL)\n\t\tr=rem;\n\telse\n\t\tr=BN_CTX_get(ctx);\n\tif (a == NULL || b == NULL || d == NULL || r == NULL) goto err;\n\tif (BN_ucmp(m,&(recp->N)) < 0)\n\t\t{\n\t\tBN_zero(d);\n\t\tBN_copy(r,m);\n\t\tBN_CTX_end(ctx);\n\t\treturn(1);\n\t\t}\n\ti=BN_num_bits(m);\n\tj=recp->num_bits<<1;\n\tif (j>i) i=j;\n\tj>>=1;\n\tif (i != recp->shift)\n\t\trecp->shift=BN_reciprocal(&(recp->Nr),&(recp->N),\n\t\t\ti,ctx);\n\tif (!BN_rshift(a,m,j)) goto err;\n\tif (!BN_mul(b,a,&(recp->Nr),ctx)) goto err;\n\tif (!BN_rshift(d,b,i-j)) goto err;\n\td->neg=0;\n\tif (!BN_mul(b,&(recp->N),d,ctx)) goto err;\n\tif (!BN_usub(r,m,b)) goto err;\n\tr->neg=0;\n#if 1\n\tj=0;\n\twhile (BN_ucmp(r,&(recp->N)) >= 0)\n\t\t{\n\t\tif (j++ > 2)\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_MOD_MUL_RECIPROCAL,BN_R_BAD_RECIPROCAL);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (!BN_usub(r,r,&(recp->N))) goto err;\n\t\tif (!BN_add_word(d,1)) goto err;\n\t\t}\n#endif\n\tr->neg=BN_is_zero(r)?0:m->neg;\n\td->neg=m->neg^recp->N.neg;\n\tret=1;\nerr:\n\tBN_CTX_end(ctx);\n\treturn(ret);\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_num_bits(const BIGNUM *a)\n\t{\n\tBN_ULONG l;\n\tint i;\n\tbn_check_top(a);\n\tif (a->top == 0) return(0);\n\tl=a->d[a->top-1];\n\ti=(a->top-1)*BN_BITS2;\n\tif (l == 0)\n\t\t{\n#if !defined(NO_STDIO) && !defined(WIN16)\n\t\tfprintf(stderr,"BAD TOP VALUE\\n");\n#endif\n\t\tabort();\n\t\t}\n\treturn(i+BN_num_bits_word(l));\n\t}', 'int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n\t{\n\tint max,min;\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\tif (a->top < b->top)\n\t\t{\n\t\tBNerr(BN_F_BN_USUB,BN_R_ARG2_LT_ARG3);\n\t\treturn(0);\n\t\t}\n\tmax=a->top;\n\tmin=b->top;\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=0; i<min; 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\ti=min;\n#endif\n\tif (carry)\n\t\t{\n\t\twhile (i < max)\n\t\t\t{\n\t\t\ti++;\n\t\t\tt1= *(ap++);\n\t\t\tt2=(t1-1)&BN_MASK2;\n\t\t\t*(rp++)=t2;\n\t\t\tif (t1 > t2) break;\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 (i++ >= max) break;\n\t\t\trp[0]=ap[0];\n\t\t\tif (i++ >= max) break;\n\t\t\trp[1]=ap[1];\n\t\t\tif (i++ >= max) break;\n\t\t\trp[2]=ap[2];\n\t\t\tif (i++ >= max) 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\tbn_fix_top(r);\n\treturn(1);\n\t}']
|
2,684
| 0
|
https://github.com/openssl/openssl/blob/f4eb24839228675386d0cbfd3e5c2291763a0be4/test/exdatatest.c/#L206
|
static int test_exdata(void)
{
MYOBJ *t1, *t2, *t3;
MYOBJ_EX_DATA *ex_data;
const char *cp;
char *p;
gbl_result = 1;
p = OPENSSL_strdup("hello world");
saved_argl = 21;
saved_argp = OPENSSL_malloc(1);
saved_idx = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP,
saved_argl, saved_argp,
exnew, exdup, exfree);
saved_idx2 = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP,
saved_argl, saved_argp,
exnew2, exdup2, exfree2);
t1 = MYOBJ_new();
t2 = MYOBJ_new();
if (!TEST_int_eq(t1->st, 1) || !TEST_int_eq(t2->st, 1))
return 0;
if (!TEST_ptr(CRYPTO_get_ex_data(&t1->ex_data, saved_idx2)))
return 0;
if (!TEST_ptr(CRYPTO_get_ex_data(&t2->ex_data, saved_idx2)))
return 0;
MYOBJ_sethello(t1, p);
cp = MYOBJ_gethello(t1);
if (!TEST_ptr_eq(cp, p))
return 0;
MYOBJ_sethello2(t1, p);
cp = MYOBJ_gethello2(t1);
if (!TEST_ptr_eq(cp, p))
return 0;
cp = MYOBJ_gethello(t2);
if (!TEST_ptr_null(cp))
return 0;
cp = MYOBJ_gethello2(t2);
if (!TEST_ptr_null(cp))
return 0;
t3 = MYOBJ_dup(t1);
if (!TEST_int_eq(t3->st, 1))
return 0;
ex_data = CRYPTO_get_ex_data(&t3->ex_data, saved_idx2);
if (!TEST_ptr(ex_data))
return 0;
if (!TEST_int_eq(ex_data->dup, 1))
return 0;
cp = MYOBJ_gethello(t3);
if (!TEST_ptr_eq(cp, p))
return 0;
cp = MYOBJ_gethello2(t3);
if (!TEST_ptr_eq(cp, p))
return 0;
MYOBJ_free(t1);
MYOBJ_free(t2);
MYOBJ_free(t3);
OPENSSL_free(saved_argp);
OPENSSL_free(p);
if (gbl_result)
return 1;
else
return 0;
}
|
['static int test_exdata(void)\n{\n MYOBJ *t1, *t2, *t3;\n MYOBJ_EX_DATA *ex_data;\n const char *cp;\n char *p;\n gbl_result = 1;\n p = OPENSSL_strdup("hello world");\n saved_argl = 21;\n saved_argp = OPENSSL_malloc(1);\n saved_idx = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP,\n saved_argl, saved_argp,\n exnew, exdup, exfree);\n saved_idx2 = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP,\n saved_argl, saved_argp,\n exnew2, exdup2, exfree2);\n t1 = MYOBJ_new();\n t2 = MYOBJ_new();\n if (!TEST_int_eq(t1->st, 1) || !TEST_int_eq(t2->st, 1))\n return 0;\n if (!TEST_ptr(CRYPTO_get_ex_data(&t1->ex_data, saved_idx2)))\n return 0;\n if (!TEST_ptr(CRYPTO_get_ex_data(&t2->ex_data, saved_idx2)))\n return 0;\n MYOBJ_sethello(t1, p);\n cp = MYOBJ_gethello(t1);\n if (!TEST_ptr_eq(cp, p))\n return 0;\n MYOBJ_sethello2(t1, p);\n cp = MYOBJ_gethello2(t1);\n if (!TEST_ptr_eq(cp, p))\n return 0;\n cp = MYOBJ_gethello(t2);\n if (!TEST_ptr_null(cp))\n return 0;\n cp = MYOBJ_gethello2(t2);\n if (!TEST_ptr_null(cp))\n return 0;\n t3 = MYOBJ_dup(t1);\n if (!TEST_int_eq(t3->st, 1))\n return 0;\n ex_data = CRYPTO_get_ex_data(&t3->ex_data, saved_idx2);\n if (!TEST_ptr(ex_data))\n return 0;\n if (!TEST_int_eq(ex_data->dup, 1))\n return 0;\n cp = MYOBJ_gethello(t3);\n if (!TEST_ptr_eq(cp, p))\n return 0;\n cp = MYOBJ_gethello2(t3);\n if (!TEST_ptr_eq(cp, p))\n return 0;\n MYOBJ_free(t1);\n MYOBJ_free(t2);\n MYOBJ_free(t3);\n OPENSSL_free(saved_argp);\n OPENSSL_free(p);\n if (gbl_result)\n return 1;\n else\n return 0;\n}', 'char *CRYPTO_strdup(const char *str, const char* file, int line)\n{\n char *ret;\n if (str == NULL)\n return NULL;\n ret = CRYPTO_malloc(strlen(str) + 1, file, line);\n if (ret != NULL)\n strcpy(ret, str);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)(file); (void)(line);\n ret = malloc(num);\n#endif\n return ret;\n}', 'DEFINE_COMPARISONS(int, int, "%d")']
|
2,685
| 0
|
https://github.com/openssl/openssl/blob/507db4c5313288d55eeb8434b0111201ba363b28/ssl/s3_cbc.c/#L422
|
int ssl3_cbc_digest_record(const EVP_MD_CTX *ctx,
unsigned char *md_out,
size_t *md_out_size,
const unsigned char header[13],
const unsigned char *data,
size_t data_plus_mac_size,
size_t data_plus_mac_plus_padding_size,
const unsigned char *mac_secret,
unsigned mac_secret_length, char is_sslv3)
{
union {
double align;
unsigned char c[sizeof(LARGEST_DIGEST_CTX)];
} md_state;
void (*md_final_raw) (void *ctx, unsigned char *md_out);
void (*md_transform) (void *ctx, const unsigned char *block);
unsigned md_size, md_block_size = 64;
unsigned sslv3_pad_length = 40, header_length, variance_blocks,
len, max_mac_bytes, num_blocks,
num_starting_blocks, k, mac_end_offset, c, index_a, index_b;
unsigned int bits;
unsigned char length_bytes[MAX_HASH_BIT_COUNT_BYTES];
unsigned char hmac_pad[MAX_HASH_BLOCK_SIZE];
unsigned char first_block[MAX_HASH_BLOCK_SIZE];
unsigned char mac_out[EVP_MAX_MD_SIZE];
unsigned i, j, md_out_size_u;
EVP_MD_CTX *md_ctx = NULL;
unsigned md_length_size = 8;
char length_is_big_endian = 1;
int ret;
OPENSSL_assert(data_plus_mac_plus_padding_size < 1024 * 1024);
switch (EVP_MD_CTX_type(ctx)) {
case NID_md5:
if (MD5_Init((MD5_CTX *)md_state.c) <= 0)
return 0;
md_final_raw = tls1_md5_final_raw;
md_transform =
(void (*)(void *ctx, const unsigned char *block))MD5_Transform;
md_size = 16;
sslv3_pad_length = 48;
length_is_big_endian = 0;
break;
case NID_sha1:
if (SHA1_Init((SHA_CTX *)md_state.c) <= 0)
return 0;
md_final_raw = tls1_sha1_final_raw;
md_transform =
(void (*)(void *ctx, const unsigned char *block))SHA1_Transform;
md_size = 20;
break;
case NID_sha224:
if (SHA224_Init((SHA256_CTX *)md_state.c) <= 0)
return 0;
md_final_raw = tls1_sha256_final_raw;
md_transform =
(void (*)(void *ctx, const unsigned char *block))SHA256_Transform;
md_size = 224 / 8;
break;
case NID_sha256:
if (SHA256_Init((SHA256_CTX *)md_state.c) <= 0)
return 0;
md_final_raw = tls1_sha256_final_raw;
md_transform =
(void (*)(void *ctx, const unsigned char *block))SHA256_Transform;
md_size = 32;
break;
case NID_sha384:
if (SHA384_Init((SHA512_CTX *)md_state.c) <= 0)
return 0;
md_final_raw = tls1_sha512_final_raw;
md_transform =
(void (*)(void *ctx, const unsigned char *block))SHA512_Transform;
md_size = 384 / 8;
md_block_size = 128;
md_length_size = 16;
break;
case NID_sha512:
if (SHA512_Init((SHA512_CTX *)md_state.c) <= 0)
return 0;
md_final_raw = tls1_sha512_final_raw;
md_transform =
(void (*)(void *ctx, const unsigned char *block))SHA512_Transform;
md_size = 64;
md_block_size = 128;
md_length_size = 16;
break;
default:
OPENSSL_assert(0);
if (md_out_size)
*md_out_size = 0;
return 0;
}
OPENSSL_assert(md_length_size <= MAX_HASH_BIT_COUNT_BYTES);
OPENSSL_assert(md_block_size <= MAX_HASH_BLOCK_SIZE);
OPENSSL_assert(md_size <= EVP_MAX_MD_SIZE);
header_length = 13;
if (is_sslv3) {
header_length = mac_secret_length + sslv3_pad_length + 8 +
1 +
2 ;
}
variance_blocks = is_sslv3 ? 2 : 6;
len = data_plus_mac_plus_padding_size + header_length;
max_mac_bytes = len - md_size - 1;
num_blocks =
(max_mac_bytes + 1 + md_length_size + md_block_size -
1) / md_block_size;
num_starting_blocks = 0;
k = 0;
mac_end_offset = data_plus_mac_size + header_length - md_size;
c = mac_end_offset % md_block_size;
index_a = mac_end_offset / md_block_size;
index_b = (mac_end_offset + md_length_size) / md_block_size;
if (num_blocks > variance_blocks + (is_sslv3 ? 1 : 0)) {
num_starting_blocks = num_blocks - variance_blocks;
k = md_block_size * num_starting_blocks;
}
bits = 8 * mac_end_offset;
if (!is_sslv3) {
bits += 8 * md_block_size;
memset(hmac_pad, 0, md_block_size);
OPENSSL_assert(mac_secret_length <= sizeof(hmac_pad));
memcpy(hmac_pad, mac_secret, mac_secret_length);
for (i = 0; i < md_block_size; i++)
hmac_pad[i] ^= 0x36;
md_transform(md_state.c, hmac_pad);
}
if (length_is_big_endian) {
memset(length_bytes, 0, md_length_size - 4);
length_bytes[md_length_size - 4] = (unsigned char)(bits >> 24);
length_bytes[md_length_size - 3] = (unsigned char)(bits >> 16);
length_bytes[md_length_size - 2] = (unsigned char)(bits >> 8);
length_bytes[md_length_size - 1] = (unsigned char)bits;
} else {
memset(length_bytes, 0, md_length_size);
length_bytes[md_length_size - 5] = (unsigned char)(bits >> 24);
length_bytes[md_length_size - 6] = (unsigned char)(bits >> 16);
length_bytes[md_length_size - 7] = (unsigned char)(bits >> 8);
length_bytes[md_length_size - 8] = (unsigned char)bits;
}
if (k > 0) {
if (is_sslv3) {
unsigned overhang;
if (header_length <= md_block_size) {
return 0;
}
overhang = header_length - md_block_size;
md_transform(md_state.c, header);
memcpy(first_block, header + md_block_size, overhang);
memcpy(first_block + overhang, data, md_block_size - overhang);
md_transform(md_state.c, first_block);
for (i = 1; i < k / md_block_size - 1; i++)
md_transform(md_state.c, data + md_block_size * i - overhang);
} else {
memcpy(first_block, header, 13);
memcpy(first_block + 13, data, md_block_size - 13);
md_transform(md_state.c, first_block);
for (i = 1; i < k / md_block_size; i++)
md_transform(md_state.c, data + md_block_size * i - 13);
}
}
memset(mac_out, 0, sizeof(mac_out));
for (i = num_starting_blocks; i <= num_starting_blocks + variance_blocks;
i++) {
unsigned char block[MAX_HASH_BLOCK_SIZE];
unsigned char is_block_a = constant_time_eq_8(i, index_a);
unsigned char is_block_b = constant_time_eq_8(i, index_b);
for (j = 0; j < md_block_size; j++) {
unsigned char b = 0, is_past_c, is_past_cp1;
if (k < header_length)
b = header[k];
else if (k < data_plus_mac_plus_padding_size + header_length)
b = data[k - header_length];
k++;
is_past_c = is_block_a & constant_time_ge_8(j, c);
is_past_cp1 = is_block_a & constant_time_ge_8(j, c + 1);
b = constant_time_select_8(is_past_c, 0x80, b);
b = b & ~is_past_cp1;
b &= ~is_block_b | is_block_a;
if (j >= md_block_size - md_length_size) {
b = constant_time_select_8(is_block_b,
length_bytes[j -
(md_block_size -
md_length_size)], b);
}
block[j] = b;
}
md_transform(md_state.c, block);
md_final_raw(md_state.c, block);
for (j = 0; j < md_size; j++)
mac_out[j] |= block[j] & is_block_b;
}
md_ctx = EVP_MD_CTX_new();
if (md_ctx == NULL)
goto err;
if (EVP_DigestInit_ex(md_ctx, EVP_MD_CTX_md(ctx), NULL ) <= 0)
goto err;
if (is_sslv3) {
memset(hmac_pad, 0x5c, sslv3_pad_length);
if (EVP_DigestUpdate(md_ctx, mac_secret, mac_secret_length) <= 0
|| EVP_DigestUpdate(md_ctx, hmac_pad, sslv3_pad_length) <= 0
|| EVP_DigestUpdate(md_ctx, mac_out, md_size) <= 0)
goto err;
} else {
for (i = 0; i < md_block_size; i++)
hmac_pad[i] ^= 0x6a;
if (EVP_DigestUpdate(md_ctx, hmac_pad, md_block_size) <= 0
|| EVP_DigestUpdate(md_ctx, mac_out, md_size) <= 0)
goto err;
}
ret = EVP_DigestFinal(md_ctx, md_out, &md_out_size_u);
if (ret && md_out_size)
*md_out_size = md_out_size_u;
EVP_MD_CTX_free(md_ctx);
return 1;
err:
EVP_MD_CTX_free(md_ctx);
return 0;
}
|
['int n_ssl3_mac(SSL *ssl, unsigned char *md, int send)\n{\n SSL3_RECORD *rec;\n unsigned char *mac_sec, *seq;\n const EVP_MD_CTX *hash;\n unsigned char *p, rec_char;\n size_t md_size;\n int npad;\n int t;\n if (send) {\n rec = RECORD_LAYER_get_wrec(&ssl->rlayer);\n mac_sec = &(ssl->s3->write_mac_secret[0]);\n seq = RECORD_LAYER_get_write_sequence(&ssl->rlayer);\n hash = ssl->write_hash;\n } else {\n rec = RECORD_LAYER_get_rrec(&ssl->rlayer);\n mac_sec = &(ssl->s3->read_mac_secret[0]);\n seq = RECORD_LAYER_get_read_sequence(&ssl->rlayer);\n hash = ssl->read_hash;\n }\n t = EVP_MD_CTX_size(hash);\n if (t < 0)\n return -1;\n md_size = t;\n npad = (48 / md_size) * md_size;\n if (!send &&\n EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&\n ssl3_cbc_record_digest_supported(hash)) {\n unsigned char header[75];\n unsigned j = 0;\n memcpy(header + j, mac_sec, md_size);\n j += md_size;\n memcpy(header + j, ssl3_pad_1, npad);\n j += npad;\n memcpy(header + j, seq, 8);\n j += 8;\n header[j++] = rec->type;\n header[j++] = rec->length >> 8;\n header[j++] = rec->length & 0xff;\n if (ssl3_cbc_digest_record(hash,\n md, &md_size,\n header, rec->input,\n rec->length + md_size, rec->orig_len,\n mac_sec, md_size, 1) <= 0)\n return -1;\n } else {\n unsigned int md_size_u;\n EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();\n if (md_ctx == NULL)\n return -1;\n rec_char = rec->type;\n p = md;\n s2n(rec->length, p);\n if (EVP_MD_CTX_copy_ex(md_ctx, hash) <= 0\n || EVP_DigestUpdate(md_ctx, mac_sec, md_size) <= 0\n || EVP_DigestUpdate(md_ctx, ssl3_pad_1, npad) <= 0\n || EVP_DigestUpdate(md_ctx, seq, 8) <= 0\n || EVP_DigestUpdate(md_ctx, &rec_char, 1) <= 0\n || EVP_DigestUpdate(md_ctx, md, 2) <= 0\n || EVP_DigestUpdate(md_ctx, rec->input, rec->length) <= 0\n || EVP_DigestFinal_ex(md_ctx, md, NULL) <= 0\n || EVP_MD_CTX_copy_ex(md_ctx, hash) <= 0\n || EVP_DigestUpdate(md_ctx, mac_sec, md_size) <= 0\n || EVP_DigestUpdate(md_ctx, ssl3_pad_2, npad) <= 0\n || EVP_DigestUpdate(md_ctx, md, md_size) <= 0\n || EVP_DigestFinal_ex(md_ctx, md, &md_size_u) <= 0) {\n EVP_MD_CTX_reset(md_ctx);\n return -1;\n }\n md_size = md_size_u;\n EVP_MD_CTX_free(md_ctx);\n }\n ssl3_record_sequence_update(seq);\n return (md_size);\n}', 'int EVP_MD_size(const EVP_MD *md)\n{\n if (!md) {\n EVPerr(EVP_F_EVP_MD_SIZE, EVP_R_MESSAGE_DIGEST_IS_NULL);\n return -1;\n }\n return md->md_size;\n}', 'int ssl3_cbc_digest_record(const EVP_MD_CTX *ctx,\n unsigned char *md_out,\n size_t *md_out_size,\n const unsigned char header[13],\n const unsigned char *data,\n size_t data_plus_mac_size,\n size_t data_plus_mac_plus_padding_size,\n const unsigned char *mac_secret,\n unsigned mac_secret_length, char is_sslv3)\n{\n union {\n double align;\n unsigned char c[sizeof(LARGEST_DIGEST_CTX)];\n } md_state;\n void (*md_final_raw) (void *ctx, unsigned char *md_out);\n void (*md_transform) (void *ctx, const unsigned char *block);\n unsigned md_size, md_block_size = 64;\n unsigned sslv3_pad_length = 40, header_length, variance_blocks,\n len, max_mac_bytes, num_blocks,\n num_starting_blocks, k, mac_end_offset, c, index_a, index_b;\n unsigned int bits;\n unsigned char length_bytes[MAX_HASH_BIT_COUNT_BYTES];\n unsigned char hmac_pad[MAX_HASH_BLOCK_SIZE];\n unsigned char first_block[MAX_HASH_BLOCK_SIZE];\n unsigned char mac_out[EVP_MAX_MD_SIZE];\n unsigned i, j, md_out_size_u;\n EVP_MD_CTX *md_ctx = NULL;\n unsigned md_length_size = 8;\n char length_is_big_endian = 1;\n int ret;\n OPENSSL_assert(data_plus_mac_plus_padding_size < 1024 * 1024);\n switch (EVP_MD_CTX_type(ctx)) {\n case NID_md5:\n if (MD5_Init((MD5_CTX *)md_state.c) <= 0)\n return 0;\n md_final_raw = tls1_md5_final_raw;\n md_transform =\n (void (*)(void *ctx, const unsigned char *block))MD5_Transform;\n md_size = 16;\n sslv3_pad_length = 48;\n length_is_big_endian = 0;\n break;\n case NID_sha1:\n if (SHA1_Init((SHA_CTX *)md_state.c) <= 0)\n return 0;\n md_final_raw = tls1_sha1_final_raw;\n md_transform =\n (void (*)(void *ctx, const unsigned char *block))SHA1_Transform;\n md_size = 20;\n break;\n case NID_sha224:\n if (SHA224_Init((SHA256_CTX *)md_state.c) <= 0)\n return 0;\n md_final_raw = tls1_sha256_final_raw;\n md_transform =\n (void (*)(void *ctx, const unsigned char *block))SHA256_Transform;\n md_size = 224 / 8;\n break;\n case NID_sha256:\n if (SHA256_Init((SHA256_CTX *)md_state.c) <= 0)\n return 0;\n md_final_raw = tls1_sha256_final_raw;\n md_transform =\n (void (*)(void *ctx, const unsigned char *block))SHA256_Transform;\n md_size = 32;\n break;\n case NID_sha384:\n if (SHA384_Init((SHA512_CTX *)md_state.c) <= 0)\n return 0;\n md_final_raw = tls1_sha512_final_raw;\n md_transform =\n (void (*)(void *ctx, const unsigned char *block))SHA512_Transform;\n md_size = 384 / 8;\n md_block_size = 128;\n md_length_size = 16;\n break;\n case NID_sha512:\n if (SHA512_Init((SHA512_CTX *)md_state.c) <= 0)\n return 0;\n md_final_raw = tls1_sha512_final_raw;\n md_transform =\n (void (*)(void *ctx, const unsigned char *block))SHA512_Transform;\n md_size = 64;\n md_block_size = 128;\n md_length_size = 16;\n break;\n default:\n OPENSSL_assert(0);\n if (md_out_size)\n *md_out_size = 0;\n return 0;\n }\n OPENSSL_assert(md_length_size <= MAX_HASH_BIT_COUNT_BYTES);\n OPENSSL_assert(md_block_size <= MAX_HASH_BLOCK_SIZE);\n OPENSSL_assert(md_size <= EVP_MAX_MD_SIZE);\n header_length = 13;\n if (is_sslv3) {\n header_length = mac_secret_length + sslv3_pad_length + 8 +\n 1 +\n 2 ;\n }\n variance_blocks = is_sslv3 ? 2 : 6;\n len = data_plus_mac_plus_padding_size + header_length;\n max_mac_bytes = len - md_size - 1;\n num_blocks =\n (max_mac_bytes + 1 + md_length_size + md_block_size -\n 1) / md_block_size;\n num_starting_blocks = 0;\n k = 0;\n mac_end_offset = data_plus_mac_size + header_length - md_size;\n c = mac_end_offset % md_block_size;\n index_a = mac_end_offset / md_block_size;\n index_b = (mac_end_offset + md_length_size) / md_block_size;\n if (num_blocks > variance_blocks + (is_sslv3 ? 1 : 0)) {\n num_starting_blocks = num_blocks - variance_blocks;\n k = md_block_size * num_starting_blocks;\n }\n bits = 8 * mac_end_offset;\n if (!is_sslv3) {\n bits += 8 * md_block_size;\n memset(hmac_pad, 0, md_block_size);\n OPENSSL_assert(mac_secret_length <= sizeof(hmac_pad));\n memcpy(hmac_pad, mac_secret, mac_secret_length);\n for (i = 0; i < md_block_size; i++)\n hmac_pad[i] ^= 0x36;\n md_transform(md_state.c, hmac_pad);\n }\n if (length_is_big_endian) {\n memset(length_bytes, 0, md_length_size - 4);\n length_bytes[md_length_size - 4] = (unsigned char)(bits >> 24);\n length_bytes[md_length_size - 3] = (unsigned char)(bits >> 16);\n length_bytes[md_length_size - 2] = (unsigned char)(bits >> 8);\n length_bytes[md_length_size - 1] = (unsigned char)bits;\n } else {\n memset(length_bytes, 0, md_length_size);\n length_bytes[md_length_size - 5] = (unsigned char)(bits >> 24);\n length_bytes[md_length_size - 6] = (unsigned char)(bits >> 16);\n length_bytes[md_length_size - 7] = (unsigned char)(bits >> 8);\n length_bytes[md_length_size - 8] = (unsigned char)bits;\n }\n if (k > 0) {\n if (is_sslv3) {\n unsigned overhang;\n if (header_length <= md_block_size) {\n return 0;\n }\n overhang = header_length - md_block_size;\n md_transform(md_state.c, header);\n memcpy(first_block, header + md_block_size, overhang);\n memcpy(first_block + overhang, data, md_block_size - overhang);\n md_transform(md_state.c, first_block);\n for (i = 1; i < k / md_block_size - 1; i++)\n md_transform(md_state.c, data + md_block_size * i - overhang);\n } else {\n memcpy(first_block, header, 13);\n memcpy(first_block + 13, data, md_block_size - 13);\n md_transform(md_state.c, first_block);\n for (i = 1; i < k / md_block_size; i++)\n md_transform(md_state.c, data + md_block_size * i - 13);\n }\n }\n memset(mac_out, 0, sizeof(mac_out));\n for (i = num_starting_blocks; i <= num_starting_blocks + variance_blocks;\n i++) {\n unsigned char block[MAX_HASH_BLOCK_SIZE];\n unsigned char is_block_a = constant_time_eq_8(i, index_a);\n unsigned char is_block_b = constant_time_eq_8(i, index_b);\n for (j = 0; j < md_block_size; j++) {\n unsigned char b = 0, is_past_c, is_past_cp1;\n if (k < header_length)\n b = header[k];\n else if (k < data_plus_mac_plus_padding_size + header_length)\n b = data[k - header_length];\n k++;\n is_past_c = is_block_a & constant_time_ge_8(j, c);\n is_past_cp1 = is_block_a & constant_time_ge_8(j, c + 1);\n b = constant_time_select_8(is_past_c, 0x80, b);\n b = b & ~is_past_cp1;\n b &= ~is_block_b | is_block_a;\n if (j >= md_block_size - md_length_size) {\n b = constant_time_select_8(is_block_b,\n length_bytes[j -\n (md_block_size -\n md_length_size)], b);\n }\n block[j] = b;\n }\n md_transform(md_state.c, block);\n md_final_raw(md_state.c, block);\n for (j = 0; j < md_size; j++)\n mac_out[j] |= block[j] & is_block_b;\n }\n md_ctx = EVP_MD_CTX_new();\n if (md_ctx == NULL)\n goto err;\n if (EVP_DigestInit_ex(md_ctx, EVP_MD_CTX_md(ctx), NULL ) <= 0)\n goto err;\n if (is_sslv3) {\n memset(hmac_pad, 0x5c, sslv3_pad_length);\n if (EVP_DigestUpdate(md_ctx, mac_secret, mac_secret_length) <= 0\n || EVP_DigestUpdate(md_ctx, hmac_pad, sslv3_pad_length) <= 0\n || EVP_DigestUpdate(md_ctx, mac_out, md_size) <= 0)\n goto err;\n } else {\n for (i = 0; i < md_block_size; i++)\n hmac_pad[i] ^= 0x6a;\n if (EVP_DigestUpdate(md_ctx, hmac_pad, md_block_size) <= 0\n || EVP_DigestUpdate(md_ctx, mac_out, md_size) <= 0)\n goto err;\n }\n ret = EVP_DigestFinal(md_ctx, md_out, &md_out_size_u);\n if (ret && md_out_size)\n *md_out_size = md_out_size_u;\n EVP_MD_CTX_free(md_ctx);\n return 1;\nerr:\n EVP_MD_CTX_free(md_ctx);\n return 0;\n}']
|
2,686
| 0
|
https://github.com/openssl/openssl/blob/9b02dc97e4963969da69675a871dbe80e6d31cda/crypto/bn/bn_sqr.c/#L114
|
void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)
{
int i, j, max;
const BN_ULONG *ap;
BN_ULONG *rp;
max = n * 2;
ap = a;
rp = r;
rp[0] = rp[max - 1] = 0;
rp++;
j = n;
if (--j > 0) {
ap++;
rp[j] = bn_mul_words(rp, ap, j, ap[-1]);
rp += 2;
}
for (i = n - 2; i > 0; i--) {
j--;
ap++;
rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);
rp += 2;
}
bn_add_words(r, r, r, max);
bn_sqr_words(tmp, a, n);
bn_add_words(r, r, tmp, max);
}
|
['int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group,\n EC_POINT *point,\n const BIGNUM *x_, int y_bit,\n BN_CTX *ctx)\n{\n BN_CTX *new_ctx = NULL;\n BIGNUM *tmp1, *tmp2, *x, *y;\n int ret = 0;\n ERR_clear_error();\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n y_bit = (y_bit != 0);\n BN_CTX_start(ctx);\n tmp1 = BN_CTX_get(ctx);\n tmp2 = BN_CTX_get(ctx);\n x = BN_CTX_get(ctx);\n y = BN_CTX_get(ctx);\n if (y == NULL)\n goto err;\n if (!BN_nnmod(x, x_, group->field, ctx))\n goto err;\n if (group->meth->field_decode == 0) {\n if (!group->meth->field_sqr(group, tmp2, x_, ctx))\n goto err;\n if (!group->meth->field_mul(group, tmp1, tmp2, x_, ctx))\n goto err;\n } else {\n if (!BN_mod_sqr(tmp2, x_, group->field, ctx))\n goto err;\n if (!BN_mod_mul(tmp1, tmp2, x_, group->field, ctx))\n goto err;\n }\n if (group->a_is_minus3) {\n if (!BN_mod_lshift1_quick(tmp2, x, group->field))\n goto err;\n if (!BN_mod_add_quick(tmp2, tmp2, x, group->field))\n goto err;\n if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n } else {\n if (group->meth->field_decode) {\n if (!group->meth->field_decode(group, tmp2, group->a, ctx))\n goto err;\n if (!BN_mod_mul(tmp2, tmp2, x, group->field, ctx))\n goto err;\n } else {\n if (!group->meth->field_mul(group, tmp2, group->a, x, ctx))\n goto err;\n }\n if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n }\n if (group->meth->field_decode) {\n if (!group->meth->field_decode(group, tmp2, group->b, ctx))\n goto err;\n if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n } else {\n if (!BN_mod_add_quick(tmp1, tmp1, group->b, group->field))\n goto err;\n }\n if (!BN_mod_sqrt(y, tmp1, group->field, ctx)) {\n unsigned long err = ERR_peek_last_error();\n if (ERR_GET_LIB(err) == ERR_LIB_BN\n && ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) {\n ERR_clear_error();\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSED_POINT);\n } else\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n ERR_R_BN_LIB);\n goto err;\n }\n if (y_bit != BN_is_odd(y)) {\n if (BN_is_zero(y)) {\n int kron;\n kron = BN_kronecker(x, group->field, ctx);\n if (kron == -2)\n goto err;\n if (kron == 1)\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSION_BIT);\n else\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSED_POINT);\n goto err;\n }\n if (!BN_usub(y, group->field, y))\n goto err;\n }\n if (y_bit != BN_is_odd(y)) {\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx))\n goto err;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return 1;\n}', '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_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx)\n{\n if (!BN_sqr(r, a, ctx))\n return 0;\n return BN_mod(r, r, m, ctx);\n}', 'int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int max, al;\n int ret = 0;\n BIGNUM *tmp, *rr;\n bn_check_top(a);\n al = a->top;\n if (al <= 0) {\n r->top = 0;\n r->neg = 0;\n return 1;\n }\n BN_CTX_start(ctx);\n rr = (a != r) ? r : BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (rr == NULL || tmp == NULL)\n goto err;\n max = 2 * al;\n if (bn_wexpand(rr, max) == NULL)\n goto err;\n if (al == 4) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[8];\n bn_sqr_normal(rr->d, a->d, 4, t);\n#else\n bn_sqr_comba4(rr->d, a->d);\n#endif\n } else if (al == 8) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[16];\n bn_sqr_normal(rr->d, a->d, 8, t);\n#else\n bn_sqr_comba8(rr->d, a->d);\n#endif\n } else {\n#if defined(BN_RECURSION)\n if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {\n BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];\n bn_sqr_normal(rr->d, a->d, al, t);\n } else {\n int j, k;\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n if (al == j) {\n if (bn_wexpand(tmp, k * 2) == NULL)\n goto err;\n bn_sqr_recursive(rr->d, a->d, al, tmp->d);\n } else {\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n }\n }\n#else\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n#endif\n }\n rr->neg = 0;\n if (a->d[al - 1] == (a->d[al - 1] & BN_MASK2l))\n rr->top = max - 1;\n else\n rr->top = max;\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(rr);\n bn_check_top(tmp);\n BN_CTX_end(ctx);\n return ret;\n}', 'void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)\n{\n int i, j, max;\n const BN_ULONG *ap;\n BN_ULONG *rp;\n max = n * 2;\n ap = a;\n rp = r;\n rp[0] = rp[max - 1] = 0;\n rp++;\n j = n;\n if (--j > 0) {\n ap++;\n rp[j] = bn_mul_words(rp, ap, j, ap[-1]);\n rp += 2;\n }\n for (i = n - 2; i > 0; i--) {\n j--;\n ap++;\n rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);\n rp += 2;\n }\n bn_add_words(r, r, r, max);\n bn_sqr_words(tmp, a, n);\n bn_add_words(r, r, tmp, max);\n}']
|
2,687
| 0
|
https://github.com/openssl/openssl/blob/c922ebe23247ff9ee07310fa30647623c0547cd9/ssl/packet.c/#L49
|
int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
{
assert(pkt->subs != NULL && len != 0);
if (pkt->subs == NULL || len == 0)
return 0;
if (pkt->maxsize - pkt->written < len)
return 0;
if (pkt->staticbuf == NULL && (pkt->buf->length - pkt->written < len)) {
size_t newlen;
size_t reflen;
reflen = (len > pkt->buf->length) ? len : pkt->buf->length;
if (reflen > SIZE_MAX / 2) {
newlen = SIZE_MAX;
} else {
newlen = reflen * 2;
if (newlen < DEFAULT_BUF_SIZE)
newlen = DEFAULT_BUF_SIZE;
}
if (BUF_MEM_grow(pkt->buf, newlen) == 0)
return 0;
}
if (allocbytes != NULL)
*allocbytes = WPACKET_get_curr(pkt);
return 1;
}
|
['int tls_construct_stoc_next_proto_neg(SSL *s, WPACKET *pkt, int *al)\n{\n const unsigned char *npa;\n unsigned int npalen;\n int ret;\n int next_proto_neg_seen = s->s3->next_proto_neg_seen;\n s->s3->next_proto_neg_seen = 0;\n if (!next_proto_neg_seen || s->ctx->next_protos_advertised_cb == NULL)\n return 1;\n ret = s->ctx->next_protos_advertised_cb(s, &npa, &npalen,\n s->ctx->next_protos_advertised_cb_arg);\n if (ret == SSL_TLSEXT_ERR_OK) {\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg)\n || !WPACKET_sub_memcpy_u16(pkt, npa, npalen)) {\n SSLerr(SSL_F_TLS_CONSTRUCT_STOC_NEXT_PROTO_NEG,\n ERR_R_INTERNAL_ERROR);\n return 0;\n }\n s->s3->next_proto_neg_seen = 1;\n }\n return 1;\n}', 'int WPACKET_put_bytes__(WPACKET *pkt, unsigned int val, size_t size)\n{\n unsigned char *data;\n assert(size <= sizeof(unsigned int));\n if (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_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,\n size_t lenbytes)\n{\n if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)\n || !WPACKET_memcpy(pkt, src, len)\n || !WPACKET_close(pkt))\n return 0;\n return 1;\n}', 'int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)\n{\n WPACKET_SUB *sub;\n unsigned char *lenchars;\n assert(pkt->subs != NULL);\n if (pkt->subs == NULL)\n return 0;\n sub = OPENSSL_zalloc(sizeof(*sub));\n if (sub == NULL)\n return 0;\n sub->parent = pkt->subs;\n pkt->subs = sub;\n sub->pwritten = pkt->written + lenbytes;\n sub->lenbytes = lenbytes;\n if (lenbytes == 0) {\n sub->packet_len = 0;\n return 1;\n }\n if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))\n return 0;\n sub->packet_len = lenchars - GETBUF(pkt);\n return 1;\n}', 'int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)\n{\n unsigned char *dest;\n if (len == 0)\n return 1;\n if (!WPACKET_allocate_bytes(pkt, len, &dest))\n return 0;\n memcpy(dest, src, len);\n return 1;\n}', 'int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)\n{\n if (!WPACKET_reserve_bytes(pkt, len, allocbytes))\n return 0;\n pkt->written += len;\n pkt->curr += len;\n return 1;\n}', 'int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)\n{\n assert(pkt->subs != NULL && len != 0);\n if (pkt->subs == NULL || len == 0)\n return 0;\n if (pkt->maxsize - pkt->written < len)\n return 0;\n if (pkt->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}']
|
2,688
| 0
|
https://github.com/libav/libav/blob/5718e3487ba3b26aba341070be0b6b0b4de45ea3/libavcodec/h264.c/#L2549
|
static void implicit_weight_table(H264Context *h, int field)
{
int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;
for (i = 0; i < 2; i++) {
h->luma_weight_flag[i] = 0;
h->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 (h->ref_count[0] == 1 && h->ref_count[1] == 1 && !FRAME_MBAFF(h) &&
h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2 * cur_poc) {
h->use_weight = 0;
h->use_weight_chroma = 0;
return;
}
ref_start = 0;
ref_count0 = h->ref_count[0];
ref_count1 = h->ref_count[1];
} else {
cur_poc = h->cur_pic_ptr->field_poc[field];
ref_start = 16;
ref_count0 = 16 + 2 * h->ref_count[0];
ref_count1 = 16 + 2 * h->ref_count[1];
}
h->use_weight = 2;
h->use_weight_chroma = 2;
h->luma_log2_weight_denom = 5;
h->chroma_log2_weight_denom = 5;
for (ref0 = ref_start; ref0 < ref_count0; ref0++) {
int poc0 = h->ref_list[0][ref0].poc;
for (ref1 = ref_start; ref1 < ref_count1; ref1++) {
int w = 32;
if (!h->ref_list[0][ref0].long_ref && !h->ref_list[1][ref1].long_ref) {
int poc1 = h->ref_list[1][ref1].poc;
int td = av_clip(poc1 - poc0, -128, 127);
if (td) {
int tb = av_clip(cur_poc - poc0, -128, 127);
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) {
h->implicit_weight[ref0][ref1][0] =
h->implicit_weight[ref0][ref1][1] = w;
} else {
h->implicit_weight[ref0][ref1][field] = w;
}
}
}
}
|
['static int decode_slice_header(H264Context *h, H264Context *h0)\n{\n unsigned int first_mb_in_slice;\n unsigned int pps_id;\n int num_ref_idx_active_override_flag, max_refs, ret;\n unsigned int slice_type, tmp, i, j;\n int default_ref_list_done = 0;\n int last_pic_structure, last_pic_droppable;\n int needs_reinit = 0;\n int field_pic_flag, bottom_field_flag;\n h->me.qpel_put = h->h264qpel.put_h264_qpel_pixels_tab;\n h->me.qpel_avg = h->h264qpel.avg_h264_qpel_pixels_tab;\n first_mb_in_slice = get_ue_golomb(&h->gb);\n if (first_mb_in_slice == 0) {\n if (h0->current_slice && FIELD_PICTURE(h)) {\n field_end(h, 1);\n }\n h0->current_slice = 0;\n if (!h0->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(&h->gb);\n if (slice_type > 9) {\n av_log(h->avctx, AV_LOG_ERROR,\n "slice type too large (%d) at %d %d\\n",\n h->slice_type, h->mb_x, h->mb_y);\n return AVERROR_INVALIDDATA;\n }\n if (slice_type > 4) {\n slice_type -= 5;\n h->slice_type_fixed = 1;\n } else\n h->slice_type_fixed = 0;\n slice_type = golomb_to_pict_type[slice_type];\n if (slice_type == AV_PICTURE_TYPE_I ||\n (h0->current_slice != 0 && slice_type == h0->last_slice_type)) {\n default_ref_list_done = 1;\n }\n h->slice_type = slice_type;\n h->slice_type_nos = slice_type & 3;\n h->pict_type = h->slice_type;\n pps_id = get_ue_golomb(&h->gb);\n if (pps_id >= MAX_PPS_COUNT) {\n av_log(h->avctx, AV_LOG_ERROR, "pps_id out of range\\n");\n return AVERROR_INVALIDDATA;\n }\n if (!h0->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 h->pps = *h0->pps_buffers[pps_id];\n if (!h0->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->current_sps_id ||\n h0->sps_buffers[h->pps.sps_id]->new) {\n h0->sps_buffers[h->pps.sps_id]->new = 0;\n h->current_sps_id = h->pps.sps_id;\n h->sps = *h0->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 h->bit_depth_luma = h->sps.bit_depth_luma;\n h->chroma_format_idc = h->sps.chroma_format_idc;\n needs_reinit = 1;\n }\n if ((ret = h264_set_parameter_from_sps(h)) < 0)\n return ret;\n }\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 if (h->context_initialized &&\n (h->width != h->avctx->coded_width ||\n h->height != h->avctx->coded_height ||\n needs_reinit)) {\n if (h != h0) {\n av_log(h->avctx, AV_LOG_ERROR, "changing width/height on "\n "slice %d\\n", h0->current_slice + 1);\n return AVERROR_INVALIDDATA;\n }\n 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, 1)) < 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 (h != h0) {\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)) < 0) {\n av_log(h->avctx, AV_LOG_ERROR,\n "h264_slice_header_init() failed\\n");\n return ret;\n }\n }\n if (h == h0 && h->dequant_coeff_pps != pps_id) {\n h->dequant_coeff_pps = pps_id;\n init_dequant_tables(h);\n }\n h->frame_num = get_bits(&h->gb, h->sps.log2_max_frame_num);\n h->mb_mbaff = 0;\n h->mb_aff_frame = 0;\n last_pic_structure = h0->picture_structure;\n last_pic_droppable = h0->droppable;\n h->droppable = h->nal_ref_idc == 0;\n if (h->sps.frame_mbs_only_flag) {\n h->picture_structure = PICT_FRAME;\n } else {\n field_pic_flag = get_bits1(&h->gb);\n if (field_pic_flag) {\n bottom_field_flag = get_bits1(&h->gb);\n h->picture_structure = PICT_TOP_FIELD + bottom_field_flag;\n } else {\n h->picture_structure = PICT_FRAME;\n h->mb_aff_frame = h->sps.mb_aff;\n }\n }\n h->mb_field_decoding_flag = h->picture_structure != PICT_FRAME;\n if (h0->current_slice != 0) {\n if (last_pic_structure != h->picture_structure ||\n last_pic_droppable != h->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 h->picture_structure = last_pic_structure;\n h->droppable = last_pic_droppable;\n return AVERROR_INVALIDDATA;\n } else if (!h0->cur_pic_ptr) {\n av_log(h->avctx, AV_LOG_ERROR,\n "unset cur_pic_ptr on %d. slice\\n",\n h0->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 (h0->first_field) {\n assert(h0->cur_pic_ptr);\n assert(h0->cur_pic_ptr->f.data[0]);\n assert(h0->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(&h0->cur_pic_ptr->tf, INT_MAX,\n last_pic_structure == PICT_TOP_FIELD);\n }\n } else {\n if (h0->cur_pic_ptr->frame_num != h->frame_num) {\n if (!last_pic_droppable && last_pic_structure != PICT_FRAME) {\n ff_thread_report_progress(&h0->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 Picture *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 = h264_frame_start(h);\n if (ret < 0)\n return ret;\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 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 h->avctx->pix_fmt,\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 (h0->first_field) {\n assert(h0->cur_pic_ptr);\n assert(h0->cur_pic_ptr->f.data[0]);\n assert(h0->cur_pic_ptr->reference != DELAYED_PIC_REF);\n if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {\n h0->cur_pic_ptr = NULL;\n h0->first_field = FIELD_PICTURE(h);\n } else {\n if (h0->cur_pic_ptr->frame_num != h->frame_num) {\n h0->first_field = 1;\n h0->cur_pic_ptr = NULL;\n } else {\n h0->first_field = 0;\n }\n }\n } else {\n h0->first_field = FIELD_PICTURE(h);\n }\n if (!FIELD_PICTURE(h) || h0->first_field) {\n if (h264_frame_start(h) < 0) {\n h0->first_field = 0;\n return AVERROR_INVALIDDATA;\n }\n } else {\n release_unused_pictures(h, 0);\n }\n }\n if (h != h0 && (ret = clone_slice(h, h0)) < 0)\n return ret;\n h->cur_pic_ptr->frame_num = h->frame_num;\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 h->resync_mb_x = h->mb_x = first_mb_in_slice % h->mb_width;\n h->resync_mb_y = h->mb_y = (first_mb_in_slice / h->mb_width) <<\n FIELD_OR_MBAFF_PICTURE(h);\n if (h->picture_structure == PICT_BOTTOM_FIELD)\n h->resync_mb_y = h->mb_y = h->mb_y + 1;\n assert(h->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(&h->gb);\n if (h->sps.poc_type == 0) {\n h->poc_lsb = get_bits(&h->gb, h->sps.log2_max_poc_lsb);\n if (h->pps.pic_order_present == 1 && h->picture_structure == PICT_FRAME)\n h->delta_poc_bottom = get_se_golomb(&h->gb);\n }\n if (h->sps.poc_type == 1 && !h->sps.delta_pic_order_always_zero_flag) {\n h->delta_poc[0] = get_se_golomb(&h->gb);\n if (h->pps.pic_order_present == 1 && h->picture_structure == PICT_FRAME)\n h->delta_poc[1] = get_se_golomb(&h->gb);\n }\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 h->redundant_pic_count = get_ue_golomb(&h->gb);\n h->ref_count[0] = h->pps.ref_count[0];\n h->ref_count[1] = h->pps.ref_count[1];\n if (h->slice_type_nos != AV_PICTURE_TYPE_I) {\n if (h->slice_type_nos == AV_PICTURE_TYPE_B)\n h->direct_spatial_mv_pred = get_bits1(&h->gb);\n num_ref_idx_active_override_flag = get_bits1(&h->gb);\n if (num_ref_idx_active_override_flag) {\n h->ref_count[0] = get_ue_golomb(&h->gb) + 1;\n if (h->ref_count[0] < 1)\n return AVERROR_INVALIDDATA;\n if (h->slice_type_nos == AV_PICTURE_TYPE_B) {\n h->ref_count[1] = get_ue_golomb(&h->gb) + 1;\n if (h->ref_count[1] < 1)\n return AVERROR_INVALIDDATA;\n }\n }\n if (h->slice_type_nos == AV_PICTURE_TYPE_B)\n h->list_count = 2;\n else\n h->list_count = 1;\n } else {\n h->list_count = 0;\n h->ref_count[0] = h->ref_count[1] = 0;\n }\n max_refs = h->picture_structure == PICT_FRAME ? 16 : 32;\n if (h->ref_count[0] > max_refs || h->ref_count[1] > max_refs) {\n av_log(h->avctx, AV_LOG_ERROR, "reference overflow\\n");\n h->ref_count[0] = h->ref_count[1] = 0;\n return AVERROR_INVALIDDATA;\n }\n if (!default_ref_list_done)\n ff_h264_fill_default_ref_list(h);\n if (h->slice_type_nos != AV_PICTURE_TYPE_I) {\n ret = ff_h264_decode_ref_pic_list_reordering(h);\n if (ret < 0) {\n h->ref_count[1] = h->ref_count[0] = 0;\n return ret;\n }\n }\n if ((h->pps.weighted_pred && h->slice_type_nos == AV_PICTURE_TYPE_P) ||\n (h->pps.weighted_bipred_idc == 1 &&\n h->slice_type_nos == AV_PICTURE_TYPE_B))\n pred_weight_table(h);\n else if (h->pps.weighted_bipred_idc == 2 &&\n h->slice_type_nos == AV_PICTURE_TYPE_B) {\n implicit_weight_table(h, -1);\n } else {\n h->use_weight = 0;\n for (i = 0; i < 2; i++) {\n h->luma_weight_flag[i] = 0;\n h->chroma_weight_flag[i] = 0;\n }\n }\n if (h->nal_ref_idc) {\n ret = ff_h264_decode_ref_pic_marking(h0, &h->gb,\n !(h->avctx->active_thread_type & FF_THREAD_FRAME) ||\n h0->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);\n if (h->pps.weighted_bipred_idc == 2 && h->slice_type_nos == AV_PICTURE_TYPE_B) {\n implicit_weight_table(h, 0);\n implicit_weight_table(h, 1);\n }\n }\n if (h->slice_type_nos == AV_PICTURE_TYPE_B && !h->direct_spatial_mv_pred)\n ff_h264_direct_dist_scale_factor(h);\n ff_h264_direct_ref_list_init(h);\n if (h->slice_type_nos != AV_PICTURE_TYPE_I && h->pps.cabac) {\n tmp = get_ue_golomb_31(&h->gb);\n if (tmp > 2) {\n av_log(h->avctx, AV_LOG_ERROR, "cabac_init_idc overflow\\n");\n return AVERROR_INVALIDDATA;\n }\n h->cabac_init_idc = tmp;\n }\n h->last_qscale_diff = 0;\n tmp = h->pps.init_qp + get_se_golomb(&h->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 h->qscale = tmp;\n h->chroma_qp[0] = get_chroma_qp(h, 0, h->qscale);\n h->chroma_qp[1] = get_chroma_qp(h, 1, h->qscale);\n if (h->slice_type == AV_PICTURE_TYPE_SP)\n get_bits1(&h->gb);\n if (h->slice_type == AV_PICTURE_TYPE_SP ||\n h->slice_type == AV_PICTURE_TYPE_SI)\n get_se_golomb(&h->gb);\n h->deblocking_filter = 1;\n h->slice_alpha_c0_offset = 52;\n h->slice_beta_offset = 52;\n if (h->pps.deblocking_filter_parameters_present) {\n tmp = get_ue_golomb_31(&h->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 h->deblocking_filter = tmp;\n if (h->deblocking_filter < 2)\n h->deblocking_filter ^= 1;\n if (h->deblocking_filter) {\n h->slice_alpha_c0_offset += get_se_golomb(&h->gb) << 1;\n h->slice_beta_offset += get_se_golomb(&h->gb) << 1;\n if (h->slice_alpha_c0_offset > 104U ||\n h->slice_beta_offset > 104U) {\n av_log(h->avctx, AV_LOG_ERROR,\n "deblocking filter parameters %d %d out of range\\n",\n h->slice_alpha_c0_offset, h->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 h->slice_type_nos != AV_PICTURE_TYPE_I) ||\n (h->avctx->skip_loop_filter >= AVDISCARD_BIDIR &&\n h->slice_type_nos == AV_PICTURE_TYPE_B) ||\n (h->avctx->skip_loop_filter >= AVDISCARD_NONREF &&\n h->nal_ref_idc == 0))\n h->deblocking_filter = 0;\n if (h->deblocking_filter == 1 && h0->max_contexts > 1) {\n if (h->avctx->flags2 & CODEC_FLAG2_FAST) {\n h->deblocking_filter = 2;\n } else {\n h0->max_contexts = 1;\n if (!h0->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 h0->single_decode_warning = 1;\n }\n if (h != h0) {\n av_log(h->avctx, AV_LOG_ERROR,\n "Deblocking switched inside frame.\\n");\n return 1;\n }\n }\n }\n h->qp_thresh = 15 + 52 -\n FFMIN(h->slice_alpha_c0_offset, h->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 h0->last_slice_type = slice_type;\n h->slice_num = ++h0->current_slice;\n if (h->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 = h->ref2frm[h->slice_num & (MAX_SLICES - 1)][j];\n for (i = 0; i < 16; i++) {\n id_list[i] = 60;\n if (j < h->list_count && i < h->ref_count[j] &&\n h->ref_list[j][i].f.buf[0]) {\n int k;\n AVBuffer *buf = h->ref_list[j][i].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] + (h->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 (h->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 h->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(h->slice_type),\n h->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 h->ref_count[0], h->ref_count[1],\n h->qscale,\n h->deblocking_filter,\n h->slice_alpha_c0_offset / 2 - 26, h->slice_beta_offset / 2 - 26,\n h->use_weight,\n h->use_weight == 1 && h->use_weight_chroma ? "c" : "",\n h->slice_type == AV_PICTURE_TYPE_B ? (h->direct_spatial_mv_pred ? "SPAT" : "TEMP") : "");\n }\n return 0;\n}', 'int ff_init_poc(H264Context *h, int pic_field_poc[2], int *pic_poc)\n{\n const int max_frame_num = 1 << h->sps.log2_max_frame_num;\n int field_poc[2];\n h->frame_num_offset = h->prev_frame_num_offset;\n if (h->frame_num < h->prev_frame_num)\n h->frame_num_offset += max_frame_num;\n if (h->sps.poc_type == 0) {\n const int max_poc_lsb = 1 << h->sps.log2_max_poc_lsb;\n if (h->poc_lsb < h->prev_poc_lsb &&\n h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb / 2)\n h->poc_msb = h->prev_poc_msb + max_poc_lsb;\n else if (h->poc_lsb > h->prev_poc_lsb &&\n h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb / 2)\n h->poc_msb = h->prev_poc_msb - max_poc_lsb;\n else\n h->poc_msb = h->prev_poc_msb;\n field_poc[0] =\n field_poc[1] = h->poc_msb + h->poc_lsb;\n if (h->picture_structure == PICT_FRAME)\n field_poc[1] += h->delta_poc_bottom;\n } else if (h->sps.poc_type == 1) {\n int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;\n int i;\n if (h->sps.poc_cycle_length != 0)\n abs_frame_num = h->frame_num_offset + h->frame_num;\n else\n abs_frame_num = 0;\n if (h->nal_ref_idc == 0 && abs_frame_num > 0)\n abs_frame_num--;\n expected_delta_per_poc_cycle = 0;\n for (i = 0; i < h->sps.poc_cycle_length; i++)\n expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[i];\n if (abs_frame_num > 0) {\n int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;\n int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;\n expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;\n for (i = 0; i <= frame_num_in_poc_cycle; i++)\n expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[i];\n } else\n expectedpoc = 0;\n if (h->nal_ref_idc == 0)\n expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;\n field_poc[0] = expectedpoc + h->delta_poc[0];\n field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;\n if (h->picture_structure == PICT_FRAME)\n field_poc[1] += h->delta_poc[1];\n } else {\n int poc = 2 * (h->frame_num_offset + h->frame_num);\n if (!h->nal_ref_idc)\n poc--;\n field_poc[0] = poc;\n field_poc[1] = poc;\n }\n if (h->picture_structure != PICT_BOTTOM_FIELD)\n pic_field_poc[0] = field_poc[0];\n if (h->picture_structure != PICT_TOP_FIELD)\n pic_field_poc[1] = field_poc[1];\n *pic_poc = FFMIN(pic_field_poc[0], pic_field_poc[1]);\n return 0;\n}', 'static void implicit_weight_table(H264Context *h, int field)\n{\n int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;\n for (i = 0; i < 2; i++) {\n h->luma_weight_flag[i] = 0;\n h->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 (h->ref_count[0] == 1 && h->ref_count[1] == 1 && !FRAME_MBAFF(h) &&\n h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2 * cur_poc) {\n h->use_weight = 0;\n h->use_weight_chroma = 0;\n return;\n }\n ref_start = 0;\n ref_count0 = h->ref_count[0];\n ref_count1 = h->ref_count[1];\n } else {\n cur_poc = h->cur_pic_ptr->field_poc[field];\n ref_start = 16;\n ref_count0 = 16 + 2 * h->ref_count[0];\n ref_count1 = 16 + 2 * h->ref_count[1];\n }\n h->use_weight = 2;\n h->use_weight_chroma = 2;\n h->luma_log2_weight_denom = 5;\n h->chroma_log2_weight_denom = 5;\n for (ref0 = ref_start; ref0 < ref_count0; ref0++) {\n int poc0 = h->ref_list[0][ref0].poc;\n for (ref1 = ref_start; ref1 < ref_count1; ref1++) {\n int w = 32;\n if (!h->ref_list[0][ref0].long_ref && !h->ref_list[1][ref1].long_ref) {\n int poc1 = h->ref_list[1][ref1].poc;\n int td = av_clip(poc1 - poc0, -128, 127);\n if (td) {\n int tb = av_clip(cur_poc - poc0, -128, 127);\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 h->implicit_weight[ref0][ref1][0] =\n h->implicit_weight[ref0][ref1][1] = w;\n } else {\n h->implicit_weight[ref0][ref1][field] = w;\n }\n }\n }\n}']
|
2,689
| 0
|
https://github.com/openssl/openssl/blob/ea32151f7b9353f8906188d007c6893704ac17bb/ssl/ssl_lib.c/#L542
|
SSL *SSL_new(SSL_CTX *ctx)
{
SSL *s;
if (ctx == NULL) {
SSLerr(SSL_F_SSL_NEW, SSL_R_NULL_SSL_CTX);
return (NULL);
}
if (ctx->method == NULL) {
SSLerr(SSL_F_SSL_NEW, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
return (NULL);
}
s = OPENSSL_zalloc(sizeof(*s));
if (s == NULL)
goto err;
s->lock = CRYPTO_THREAD_lock_new();
if (s->lock == NULL) {
SSLerr(SSL_F_SSL_NEW, ERR_R_MALLOC_FAILURE);
OPENSSL_free(s);
return NULL;
}
RECORD_LAYER_init(&s->rlayer, s);
s->options = ctx->options;
s->min_proto_version = ctx->min_proto_version;
s->max_proto_version = ctx->max_proto_version;
s->mode = ctx->mode;
s->max_cert_list = ctx->max_cert_list;
s->references = 1;
s->cert = ssl_cert_dup(ctx->cert);
if (s->cert == NULL)
goto err;
RECORD_LAYER_set_read_ahead(&s->rlayer, ctx->read_ahead);
s->msg_callback = ctx->msg_callback;
s->msg_callback_arg = ctx->msg_callback_arg;
s->verify_mode = ctx->verify_mode;
s->not_resumable_session_cb = ctx->not_resumable_session_cb;
s->sid_ctx_length = ctx->sid_ctx_length;
OPENSSL_assert(s->sid_ctx_length <= sizeof s->sid_ctx);
memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
s->verify_callback = ctx->default_verify_callback;
s->generate_session_id = ctx->generate_session_id;
s->param = X509_VERIFY_PARAM_new();
if (s->param == NULL)
goto err;
X509_VERIFY_PARAM_inherit(s->param, ctx->param);
s->quiet_shutdown = ctx->quiet_shutdown;
s->max_send_fragment = ctx->max_send_fragment;
s->split_send_fragment = ctx->split_send_fragment;
s->max_pipelines = ctx->max_pipelines;
if (s->max_pipelines > 1)
RECORD_LAYER_set_read_ahead(&s->rlayer, 1);
if (ctx->default_read_buf_len > 0)
SSL_set_default_read_buffer_len(s, ctx->default_read_buf_len);
SSL_CTX_up_ref(ctx);
s->ctx = ctx;
s->tlsext_debug_cb = 0;
s->tlsext_debug_arg = NULL;
s->tlsext_ticket_expected = 0;
s->tlsext_status_type = ctx->tlsext_status_type;
s->tlsext_status_expected = 0;
s->tlsext_ocsp_ids = NULL;
s->tlsext_ocsp_exts = NULL;
s->tlsext_ocsp_resp = NULL;
s->tlsext_ocsp_resplen = -1;
SSL_CTX_up_ref(ctx);
s->initial_ctx = ctx;
# ifndef OPENSSL_NO_EC
if (ctx->tlsext_ecpointformatlist) {
s->tlsext_ecpointformatlist =
OPENSSL_memdup(ctx->tlsext_ecpointformatlist,
ctx->tlsext_ecpointformatlist_length);
if (!s->tlsext_ecpointformatlist)
goto err;
s->tlsext_ecpointformatlist_length =
ctx->tlsext_ecpointformatlist_length;
}
if (ctx->tlsext_ellipticcurvelist) {
s->tlsext_ellipticcurvelist =
OPENSSL_memdup(ctx->tlsext_ellipticcurvelist,
ctx->tlsext_ellipticcurvelist_length);
if (!s->tlsext_ellipticcurvelist)
goto err;
s->tlsext_ellipticcurvelist_length =
ctx->tlsext_ellipticcurvelist_length;
}
# endif
# ifndef OPENSSL_NO_NEXTPROTONEG
s->next_proto_negotiated = NULL;
# endif
if (s->ctx->alpn_client_proto_list) {
s->alpn_client_proto_list =
OPENSSL_malloc(s->ctx->alpn_client_proto_list_len);
if (s->alpn_client_proto_list == NULL)
goto err;
memcpy(s->alpn_client_proto_list, s->ctx->alpn_client_proto_list,
s->ctx->alpn_client_proto_list_len);
s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
}
s->verified_chain = NULL;
s->verify_result = X509_V_OK;
s->default_passwd_callback = ctx->default_passwd_callback;
s->default_passwd_callback_userdata = ctx->default_passwd_callback_userdata;
s->method = ctx->method;
if (!s->method->ssl_new(s))
goto err;
s->server = (ctx->method->ssl_accept == ssl_undefined_function) ? 0 : 1;
if (!SSL_clear(s))
goto err;
if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data))
goto err;
#ifndef OPENSSL_NO_PSK
s->psk_client_callback = ctx->psk_client_callback;
s->psk_server_callback = ctx->psk_server_callback;
#endif
s->job = NULL;
#ifndef OPENSSL_NO_CT
if (!SSL_set_ct_validation_callback(s, ctx->ct_validation_callback,
ctx->ct_validation_callback_arg))
goto err;
#endif
return s;
err:
SSL_free(s);
SSLerr(SSL_F_SSL_NEW, ERR_R_MALLOC_FAILURE);
return NULL;
}
|
['SSL *SSL_new(SSL_CTX *ctx)\n{\n SSL *s;\n if (ctx == NULL) {\n SSLerr(SSL_F_SSL_NEW, SSL_R_NULL_SSL_CTX);\n return (NULL);\n }\n if (ctx->method == NULL) {\n SSLerr(SSL_F_SSL_NEW, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);\n return (NULL);\n }\n s = OPENSSL_zalloc(sizeof(*s));\n if (s == NULL)\n goto err;\n s->lock = CRYPTO_THREAD_lock_new();\n if (s->lock == NULL) {\n SSLerr(SSL_F_SSL_NEW, ERR_R_MALLOC_FAILURE);\n OPENSSL_free(s);\n return NULL;\n }\n RECORD_LAYER_init(&s->rlayer, s);\n s->options = ctx->options;\n s->min_proto_version = ctx->min_proto_version;\n s->max_proto_version = ctx->max_proto_version;\n s->mode = ctx->mode;\n s->max_cert_list = ctx->max_cert_list;\n s->references = 1;\n s->cert = ssl_cert_dup(ctx->cert);\n if (s->cert == NULL)\n goto err;\n RECORD_LAYER_set_read_ahead(&s->rlayer, ctx->read_ahead);\n s->msg_callback = ctx->msg_callback;\n s->msg_callback_arg = ctx->msg_callback_arg;\n s->verify_mode = ctx->verify_mode;\n s->not_resumable_session_cb = ctx->not_resumable_session_cb;\n s->sid_ctx_length = ctx->sid_ctx_length;\n OPENSSL_assert(s->sid_ctx_length <= sizeof s->sid_ctx);\n memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));\n s->verify_callback = ctx->default_verify_callback;\n s->generate_session_id = ctx->generate_session_id;\n s->param = X509_VERIFY_PARAM_new();\n if (s->param == NULL)\n goto err;\n X509_VERIFY_PARAM_inherit(s->param, ctx->param);\n s->quiet_shutdown = ctx->quiet_shutdown;\n s->max_send_fragment = ctx->max_send_fragment;\n s->split_send_fragment = ctx->split_send_fragment;\n s->max_pipelines = ctx->max_pipelines;\n if (s->max_pipelines > 1)\n RECORD_LAYER_set_read_ahead(&s->rlayer, 1);\n if (ctx->default_read_buf_len > 0)\n SSL_set_default_read_buffer_len(s, ctx->default_read_buf_len);\n SSL_CTX_up_ref(ctx);\n s->ctx = ctx;\n s->tlsext_debug_cb = 0;\n s->tlsext_debug_arg = NULL;\n s->tlsext_ticket_expected = 0;\n s->tlsext_status_type = ctx->tlsext_status_type;\n s->tlsext_status_expected = 0;\n s->tlsext_ocsp_ids = NULL;\n s->tlsext_ocsp_exts = NULL;\n s->tlsext_ocsp_resp = NULL;\n s->tlsext_ocsp_resplen = -1;\n SSL_CTX_up_ref(ctx);\n s->initial_ctx = ctx;\n# ifndef OPENSSL_NO_EC\n if (ctx->tlsext_ecpointformatlist) {\n s->tlsext_ecpointformatlist =\n OPENSSL_memdup(ctx->tlsext_ecpointformatlist,\n ctx->tlsext_ecpointformatlist_length);\n if (!s->tlsext_ecpointformatlist)\n goto err;\n s->tlsext_ecpointformatlist_length =\n ctx->tlsext_ecpointformatlist_length;\n }\n if (ctx->tlsext_ellipticcurvelist) {\n s->tlsext_ellipticcurvelist =\n OPENSSL_memdup(ctx->tlsext_ellipticcurvelist,\n ctx->tlsext_ellipticcurvelist_length);\n if (!s->tlsext_ellipticcurvelist)\n goto err;\n s->tlsext_ellipticcurvelist_length =\n ctx->tlsext_ellipticcurvelist_length;\n }\n# endif\n# ifndef OPENSSL_NO_NEXTPROTONEG\n s->next_proto_negotiated = NULL;\n# endif\n if (s->ctx->alpn_client_proto_list) {\n s->alpn_client_proto_list =\n OPENSSL_malloc(s->ctx->alpn_client_proto_list_len);\n if (s->alpn_client_proto_list == NULL)\n goto err;\n memcpy(s->alpn_client_proto_list, s->ctx->alpn_client_proto_list,\n s->ctx->alpn_client_proto_list_len);\n s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;\n }\n s->verified_chain = NULL;\n s->verify_result = X509_V_OK;\n s->default_passwd_callback = ctx->default_passwd_callback;\n s->default_passwd_callback_userdata = ctx->default_passwd_callback_userdata;\n s->method = ctx->method;\n if (!s->method->ssl_new(s))\n goto err;\n s->server = (ctx->method->ssl_accept == ssl_undefined_function) ? 0 : 1;\n if (!SSL_clear(s))\n goto err;\n 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}', '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}']
|
2,690
| 0
|
https://github.com/openssl/openssl/blob/219f3ca61c2902425047974441d953e6042250ec/crypto/objects/o_names.c/#L193
|
int OBJ_NAME_add(const char *name, int type, const char *data)
{
OBJ_NAME *onp, *ret;
int alias;
if ((names_lh == NULL) && !OBJ_NAME_init())
return (0);
alias = type & OBJ_NAME_ALIAS;
type &= ~OBJ_NAME_ALIAS;
onp = OPENSSL_malloc(sizeof(*onp));
if (onp == NULL) {
return (0);
}
onp->name = name;
onp->alias = alias;
onp->type = type;
onp->data = data;
ret = lh_OBJ_NAME_insert(names_lh, onp);
if (ret != NULL) {
if ((name_funcs_stack != NULL)
&& (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) {
sk_NAME_FUNCS_value(name_funcs_stack,
ret->type)->free_func(ret->name, ret->type,
ret->data);
}
OPENSSL_free(ret);
} else {
if (lh_OBJ_NAME_error(names_lh)) {
return (0);
}
}
return (1);
}
|
['int OBJ_NAME_add(const char *name, int type, const char *data)\n{\n OBJ_NAME *onp, *ret;\n int alias;\n if ((names_lh == NULL) && !OBJ_NAME_init())\n return (0);\n alias = type & OBJ_NAME_ALIAS;\n type &= ~OBJ_NAME_ALIAS;\n onp = OPENSSL_malloc(sizeof(*onp));\n if (onp == NULL) {\n return (0);\n }\n onp->name = name;\n onp->alias = alias;\n onp->type = type;\n onp->data = data;\n ret = lh_OBJ_NAME_insert(names_lh, onp);\n if (ret != NULL) {\n if ((name_funcs_stack != NULL)\n && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) {\n sk_NAME_FUNCS_value(name_funcs_stack,\n ret->type)->free_func(ret->name, ret->type,\n ret->data);\n }\n OPENSSL_free(ret);\n } else {\n if (lh_OBJ_NAME_error(names_lh)) {\n return (0);\n }\n }\n return (1);\n}', 'int OBJ_NAME_init(void)\n{\n if (names_lh != NULL)\n return (1);\n CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);\n names_lh = lh_OBJ_NAME_new(obj_name_hash, obj_name_cmp);\n CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);\n return (names_lh != NULL);\n}', 'int CRYPTO_mem_ctrl(int mode)\n{\n#ifdef OPENSSL_NO_CRYPTO_MDEBUG\n return mode - mode;\n#else\n int ret = mh_mode;\n CRYPTO_w_lock(CRYPTO_LOCK_MALLOC);\n switch (mode) {\n default:\n break;\n case CRYPTO_MEM_CHECK_ON:\n mh_mode = CRYPTO_MEM_CHECK_ON | CRYPTO_MEM_CHECK_ENABLE;\n num_disable = 0;\n break;\n case CRYPTO_MEM_CHECK_OFF:\n mh_mode = 0;\n num_disable = 0;\n break;\n case CRYPTO_MEM_CHECK_DISABLE:\n if (mh_mode & CRYPTO_MEM_CHECK_ON) {\n CRYPTO_THREADID cur;\n CRYPTO_THREADID_current(&cur);\n if (!num_disable\n || CRYPTO_THREADID_cmp(&disabling_threadid, &cur)) {\n CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC);\n CRYPTO_w_lock(CRYPTO_LOCK_MALLOC2);\n CRYPTO_w_lock(CRYPTO_LOCK_MALLOC);\n mh_mode &= ~CRYPTO_MEM_CHECK_ENABLE;\n CRYPTO_THREADID_cpy(&disabling_threadid, &cur);\n }\n num_disable++;\n }\n break;\n case CRYPTO_MEM_CHECK_ENABLE:\n if (mh_mode & CRYPTO_MEM_CHECK_ON) {\n if (num_disable) {\n num_disable--;\n if (num_disable == 0) {\n mh_mode |= CRYPTO_MEM_CHECK_ENABLE;\n CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC2);\n }\n }\n }\n break;\n }\n CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC);\n return (ret);\n#endif\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}', '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}']
|
2,691
| 0
|
https://github.com/openssl/openssl/blob/0fd2d5fa3c58e0a11f059becd1fcf063c05ea292/ssl/t1_lib.c/#L3079
|
static int tls_decrypt_ticket(SSL *s, const unsigned char *etick,
int eticklen, const unsigned char *sess_id,
int sesslen, SSL_SESSION **psess)
{
SSL_SESSION *sess;
unsigned char *sdec;
const unsigned char *p;
int slen, mlen, renew_ticket = 0;
unsigned char tick_hmac[EVP_MAX_MD_SIZE];
HMAC_CTX *hctx = NULL;
EVP_CIPHER_CTX *ctx;
SSL_CTX *tctx = s->initial_ctx;
if (eticklen < 48)
return 2;
hctx = HMAC_CTX_new();
if (hctx == NULL)
return -2;
ctx = EVP_CIPHER_CTX_new();
if (tctx->tlsext_ticket_key_cb) {
unsigned char *nctick = (unsigned char *)etick;
int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
ctx, hctx, 0);
if (rv < 0)
return -1;
if (rv == 0)
return 2;
if (rv == 2)
renew_ticket = 1;
} else {
if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
return 2;
if (HMAC_Init_ex(hctx, tctx->tlsext_tick_hmac_key, 16,
EVP_sha256(), NULL) <= 0
|| EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL,
tctx->tlsext_tick_aes_key,
etick + 16) <= 0) {
goto err;
}
}
mlen = HMAC_size(hctx);
if (mlen < 0) {
goto err;
}
eticklen -= mlen;
if (HMAC_Update(hctx, etick, eticklen) <= 0
|| HMAC_Final(hctx, tick_hmac, NULL) <= 0) {
goto err;
}
HMAC_CTX_free(hctx);
if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
EVP_CIPHER_CTX_free(ctx);
return 2;
}
p = etick + 16 + EVP_CIPHER_CTX_iv_length(ctx);
eticklen -= 16 + EVP_CIPHER_CTX_iv_length(ctx);
sdec = OPENSSL_malloc(eticklen);
if (sdec == NULL
|| EVP_DecryptUpdate(ctx, sdec, &slen, p, eticklen) <= 0) {
EVP_CIPHER_CTX_free(ctx);
return -1;
}
if (EVP_DecryptFinal(ctx, sdec + slen, &mlen) <= 0) {
EVP_CIPHER_CTX_free(ctx);
OPENSSL_free(sdec);
return 2;
}
slen += mlen;
EVP_CIPHER_CTX_free(ctx);
ctx = NULL;
p = sdec;
sess = d2i_SSL_SESSION(NULL, &p, slen);
OPENSSL_free(sdec);
if (sess) {
if (sesslen)
memcpy(sess->session_id, sess_id, sesslen);
sess->session_id_length = sesslen;
*psess = sess;
if (renew_ticket)
return 4;
else
return 3;
}
ERR_clear_error();
return 2;
err:
EVP_CIPHER_CTX_free(ctx);
HMAC_CTX_free(hctx);
return -1;
}
|
['static int tls_decrypt_ticket(SSL *s, const unsigned char *etick,\n int eticklen, const unsigned char *sess_id,\n int sesslen, SSL_SESSION **psess)\n{\n SSL_SESSION *sess;\n unsigned char *sdec;\n const unsigned char *p;\n int slen, mlen, renew_ticket = 0;\n unsigned char tick_hmac[EVP_MAX_MD_SIZE];\n HMAC_CTX *hctx = NULL;\n EVP_CIPHER_CTX *ctx;\n SSL_CTX *tctx = s->initial_ctx;\n if (eticklen < 48)\n return 2;\n hctx = HMAC_CTX_new();\n if (hctx == NULL)\n return -2;\n ctx = EVP_CIPHER_CTX_new();\n if (tctx->tlsext_ticket_key_cb) {\n unsigned char *nctick = (unsigned char *)etick;\n int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,\n ctx, hctx, 0);\n if (rv < 0)\n return -1;\n if (rv == 0)\n return 2;\n if (rv == 2)\n renew_ticket = 1;\n } else {\n if (memcmp(etick, tctx->tlsext_tick_key_name, 16))\n return 2;\n if (HMAC_Init_ex(hctx, tctx->tlsext_tick_hmac_key, 16,\n EVP_sha256(), NULL) <= 0\n || EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL,\n tctx->tlsext_tick_aes_key,\n etick + 16) <= 0) {\n goto err;\n }\n }\n mlen = HMAC_size(hctx);\n if (mlen < 0) {\n goto err;\n }\n eticklen -= mlen;\n if (HMAC_Update(hctx, etick, eticklen) <= 0\n || HMAC_Final(hctx, tick_hmac, NULL) <= 0) {\n goto err;\n }\n HMAC_CTX_free(hctx);\n if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {\n EVP_CIPHER_CTX_free(ctx);\n return 2;\n }\n p = etick + 16 + EVP_CIPHER_CTX_iv_length(ctx);\n eticklen -= 16 + EVP_CIPHER_CTX_iv_length(ctx);\n sdec = OPENSSL_malloc(eticklen);\n if (sdec == NULL\n || EVP_DecryptUpdate(ctx, sdec, &slen, p, eticklen) <= 0) {\n EVP_CIPHER_CTX_free(ctx);\n return -1;\n }\n if (EVP_DecryptFinal(ctx, sdec + slen, &mlen) <= 0) {\n EVP_CIPHER_CTX_free(ctx);\n OPENSSL_free(sdec);\n return 2;\n }\n slen += mlen;\n EVP_CIPHER_CTX_free(ctx);\n ctx = NULL;\n p = sdec;\n sess = d2i_SSL_SESSION(NULL, &p, slen);\n OPENSSL_free(sdec);\n if (sess) {\n if (sesslen)\n memcpy(sess->session_id, sess_id, sesslen);\n sess->session_id_length = sesslen;\n *psess = sess;\n if (renew_ticket)\n return 4;\n else\n return 3;\n }\n ERR_clear_error();\n return 2;\nerr:\n EVP_CIPHER_CTX_free(ctx);\n HMAC_CTX_free(hctx);\n return -1;\n}', 'HMAC_CTX *HMAC_CTX_new(void)\n{\n HMAC_CTX *ctx = OPENSSL_zalloc(sizeof(HMAC_CTX));\n if (ctx != NULL) {\n if (!HMAC_CTX_reset(ctx)) {\n HMAC_CTX_free(ctx);\n return NULL;\n }\n }\n return ctx;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n 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}', 'EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void)\n{\n return OPENSSL_zalloc(sizeof(EVP_CIPHER_CTX));\n}']
|
2,692
| 0
|
https://github.com/libav/libav/blob/3b2fbe67bd63b00331db2a9b213f6d420418a312/libavcodec/opus_celt.c/#L788
|
static void celt_decode_allocation(CeltContext *s, OpusRangeCoder *rc)
{
int cap[CELT_MAX_BANDS];
int boost[CELT_MAX_BANDS];
int threshold[CELT_MAX_BANDS];
int bits1[CELT_MAX_BANDS];
int bits2[CELT_MAX_BANDS];
int trim_offset[CELT_MAX_BANDS];
int skip_startband = s->startband;
int dynalloc = 6;
int alloctrim = 5;
int extrabits = 0;
int skip_bit = 0;
int intensitystereo_bit = 0;
int dualstereo_bit = 0;
int remaining, bandbits;
int low, high, total, done;
int totalbits;
int consumed;
int i, j;
consumed = opus_rc_tell(rc);
s->spread = CELT_SPREAD_NORMAL;
if (consumed + 4 <= s->framebits)
s->spread = opus_rc_getsymbol(rc, celt_model_spread);
for (i = 0; i < CELT_MAX_BANDS; i++) {
cap[i] = (celt_static_caps[s->duration][s->coded_channels - 1][i] + 64)
* celt_freq_range[i] << (s->coded_channels - 1) << s->duration >> 2;
}
totalbits = s->framebits << 3;
consumed = opus_rc_tell_frac(rc);
for (i = s->startband; i < s->endband; i++) {
int quanta, band_dynalloc;
boost[i] = 0;
quanta = celt_freq_range[i] << (s->coded_channels - 1) << s->duration;
quanta = FFMIN(quanta << 3, FFMAX(6 << 3, quanta));
band_dynalloc = dynalloc;
while (consumed + (band_dynalloc<<3) < totalbits && boost[i] < cap[i]) {
int add = opus_rc_p2model(rc, band_dynalloc);
consumed = opus_rc_tell_frac(rc);
if (!add)
break;
boost[i] += quanta;
totalbits -= quanta;
band_dynalloc = 1;
}
if (boost[i])
dynalloc = FFMAX(2, dynalloc - 1);
}
if (consumed + (6 << 3) <= totalbits)
alloctrim = opus_rc_getsymbol(rc, celt_model_alloc_trim);
totalbits = (s->framebits << 3) - opus_rc_tell_frac(rc) - 1;
s->anticollapse_bit = 0;
if (s->blocks > 1 && s->duration >= 2 &&
totalbits >= ((s->duration + 2) << 3))
s->anticollapse_bit = 1 << 3;
totalbits -= s->anticollapse_bit;
if (totalbits >= 1 << 3)
skip_bit = 1 << 3;
totalbits -= skip_bit;
if (s->coded_channels == 2) {
intensitystereo_bit = celt_log2_frac[s->endband - s->startband];
if (intensitystereo_bit <= totalbits) {
totalbits -= intensitystereo_bit;
if (totalbits >= 1 << 3) {
dualstereo_bit = 1 << 3;
totalbits -= 1 << 3;
}
} else
intensitystereo_bit = 0;
}
for (i = s->startband; i < s->endband; i++) {
int trim = alloctrim - 5 - s->duration;
int band = celt_freq_range[i] * (s->endband - i - 1);
int duration = s->duration + 3;
int scale = duration + s->coded_channels - 1;
threshold[i] = FFMAX(3 * celt_freq_range[i] << duration >> 4,
s->coded_channels << 3);
trim_offset[i] = trim * (band << scale) >> 6;
if (celt_freq_range[i] << s->duration == 1)
trim_offset[i] -= s->coded_channels << 3;
}
low = 1;
high = CELT_VECTORS - 1;
while (low <= high) {
int center = (low + high) >> 1;
done = total = 0;
for (i = s->endband - 1; i >= s->startband; i--) {
bandbits = celt_freq_range[i] * celt_static_alloc[center][i]
<< (s->coded_channels - 1) << s->duration >> 2;
if (bandbits)
bandbits = FFMAX(0, bandbits + trim_offset[i]);
bandbits += boost[i];
if (bandbits >= threshold[i] || done) {
done = 1;
total += FFMIN(bandbits, cap[i]);
} else if (bandbits >= s->coded_channels << 3)
total += s->coded_channels << 3;
}
if (total > totalbits)
high = center - 1;
else
low = center + 1;
}
high = low--;
for (i = s->startband; i < s->endband; i++) {
bits1[i] = celt_freq_range[i] * celt_static_alloc[low][i]
<< (s->coded_channels - 1) << s->duration >> 2;
bits2[i] = high >= CELT_VECTORS ? cap[i] :
celt_freq_range[i] * celt_static_alloc[high][i]
<< (s->coded_channels - 1) << s->duration >> 2;
if (bits1[i])
bits1[i] = FFMAX(0, bits1[i] + trim_offset[i]);
if (bits2[i])
bits2[i] = FFMAX(0, bits2[i] + trim_offset[i]);
if (low)
bits1[i] += boost[i];
bits2[i] += boost[i];
if (boost[i])
skip_startband = i;
bits2[i] = FFMAX(0, bits2[i] - bits1[i]);
}
low = 0;
high = 1 << CELT_ALLOC_STEPS;
for (i = 0; i < CELT_ALLOC_STEPS; i++) {
int center = (low + high) >> 1;
done = total = 0;
for (j = s->endband - 1; j >= s->startband; j--) {
bandbits = bits1[j] + (center * bits2[j] >> CELT_ALLOC_STEPS);
if (bandbits >= threshold[j] || done) {
done = 1;
total += FFMIN(bandbits, cap[j]);
} else if (bandbits >= s->coded_channels << 3)
total += s->coded_channels << 3;
}
if (total > totalbits)
high = center;
else
low = center;
}
done = total = 0;
for (i = s->endband - 1; i >= s->startband; i--) {
bandbits = bits1[i] + (low * bits2[i] >> CELT_ALLOC_STEPS);
if (bandbits >= threshold[i] || done)
done = 1;
else
bandbits = (bandbits >= s->coded_channels << 3) ?
s->coded_channels << 3 : 0;
bandbits = FFMIN(bandbits, cap[i]);
s->pulses[i] = bandbits;
total += bandbits;
}
for (s->codedbands = s->endband; ; s->codedbands--) {
int allocation;
j = s->codedbands - 1;
if (j == skip_startband) {
totalbits += skip_bit;
break;
}
remaining = totalbits - total;
bandbits = remaining / (celt_freq_bands[j+1] - celt_freq_bands[s->startband]);
remaining -= bandbits * (celt_freq_bands[j+1] - celt_freq_bands[s->startband]);
allocation = s->pulses[j] + bandbits * celt_freq_range[j]
+ FFMAX(0, remaining - (celt_freq_bands[j] - celt_freq_bands[s->startband]));
if (allocation >= FFMAX(threshold[j], (s->coded_channels + 1) <<3 )) {
if (opus_rc_p2model(rc, 1))
break;
total += 1 << 3;
allocation -= 1 << 3;
}
total -= s->pulses[j];
if (intensitystereo_bit) {
total -= intensitystereo_bit;
intensitystereo_bit = celt_log2_frac[j - s->startband];
total += intensitystereo_bit;
}
total += s->pulses[j] = (allocation >= s->coded_channels << 3) ?
s->coded_channels << 3 : 0;
}
s->intensitystereo = 0;
s->dualstereo = 0;
if (intensitystereo_bit)
s->intensitystereo = s->startband +
opus_rc_unimodel(rc, s->codedbands + 1 - s->startband);
if (s->intensitystereo <= s->startband)
totalbits += dualstereo_bit;
else if (dualstereo_bit)
s->dualstereo = opus_rc_p2model(rc, 1);
remaining = totalbits - total;
bandbits = remaining / (celt_freq_bands[s->codedbands] - celt_freq_bands[s->startband]);
remaining -= bandbits * (celt_freq_bands[s->codedbands] - celt_freq_bands[s->startband]);
for (i = s->startband; i < s->codedbands; i++) {
int bits = FFMIN(remaining, celt_freq_range[i]);
s->pulses[i] += bits + bandbits * celt_freq_range[i];
remaining -= bits;
}
for (i = s->startband; i < s->codedbands; i++) {
int N = celt_freq_range[i] << s->duration;
int prev_extra = extrabits;
s->pulses[i] += extrabits;
if (N > 1) {
int dof;
int temp;
int offset;
int fine_bits, max_bits;
extrabits = FFMAX(0, s->pulses[i] - cap[i]);
s->pulses[i] -= extrabits;
dof = N * s->coded_channels
+ (s->coded_channels == 2 && N > 2 && !s->dualstereo && i < s->intensitystereo);
temp = dof * (celt_log_freq_range[i] + (s->duration<<3));
offset = (temp >> 1) - dof * CELT_FINE_OFFSET;
if (N == 2)
offset += dof<<1;
if (s->pulses[i] + offset < 2 * (dof << 3))
offset += temp >> 2;
else if (s->pulses[i] + offset < 3 * (dof << 3))
offset += temp >> 3;
fine_bits = (s->pulses[i] + offset + (dof << 2)) / (dof << 3);
max_bits = FFMIN((s->pulses[i]>>3) >> (s->coded_channels - 1),
CELT_MAX_FINE_BITS);
max_bits = FFMAX(max_bits, 0);
s->fine_bits[i] = av_clip(fine_bits, 0, max_bits);
s->fine_priority[i] = (s->fine_bits[i] * (dof<<3) >= s->pulses[i] + offset);
s->pulses[i] -= s->fine_bits[i] << (s->coded_channels - 1) << 3;
} else {
extrabits = FFMAX(0, s->pulses[i] - (s->coded_channels << 3));
s->pulses[i] -= extrabits;
s->fine_bits[i] = 0;
s->fine_priority[i] = 1;
}
if (extrabits > 0) {
int fineextra = FFMIN(extrabits >> (s->coded_channels + 2),
CELT_MAX_FINE_BITS - s->fine_bits[i]);
s->fine_bits[i] += fineextra;
fineextra <<= s->coded_channels + 2;
s->fine_priority[i] = (fineextra >= extrabits - prev_extra);
extrabits -= fineextra;
}
}
s->remaining = extrabits;
for (; i < s->endband; i++) {
s->fine_bits[i] = s->pulses[i] >> (s->coded_channels - 1) >> 3;
s->pulses[i] = 0;
s->fine_priority[i] = s->fine_bits[i] < 1;
}
}
|
['static void celt_decode_allocation(CeltContext *s, OpusRangeCoder *rc)\n{\n int cap[CELT_MAX_BANDS];\n int boost[CELT_MAX_BANDS];\n int threshold[CELT_MAX_BANDS];\n int bits1[CELT_MAX_BANDS];\n int bits2[CELT_MAX_BANDS];\n int trim_offset[CELT_MAX_BANDS];\n int skip_startband = s->startband;\n int dynalloc = 6;\n int alloctrim = 5;\n int extrabits = 0;\n int skip_bit = 0;\n int intensitystereo_bit = 0;\n int dualstereo_bit = 0;\n int remaining, bandbits;\n int low, high, total, done;\n int totalbits;\n int consumed;\n int i, j;\n consumed = opus_rc_tell(rc);\n s->spread = CELT_SPREAD_NORMAL;\n if (consumed + 4 <= s->framebits)\n s->spread = opus_rc_getsymbol(rc, celt_model_spread);\n for (i = 0; i < CELT_MAX_BANDS; i++) {\n cap[i] = (celt_static_caps[s->duration][s->coded_channels - 1][i] + 64)\n * celt_freq_range[i] << (s->coded_channels - 1) << s->duration >> 2;\n }\n totalbits = s->framebits << 3;\n consumed = opus_rc_tell_frac(rc);\n for (i = s->startband; i < s->endband; i++) {\n int quanta, band_dynalloc;\n boost[i] = 0;\n quanta = celt_freq_range[i] << (s->coded_channels - 1) << s->duration;\n quanta = FFMIN(quanta << 3, FFMAX(6 << 3, quanta));\n band_dynalloc = dynalloc;\n while (consumed + (band_dynalloc<<3) < totalbits && boost[i] < cap[i]) {\n int add = opus_rc_p2model(rc, band_dynalloc);\n consumed = opus_rc_tell_frac(rc);\n if (!add)\n break;\n boost[i] += quanta;\n totalbits -= quanta;\n band_dynalloc = 1;\n }\n if (boost[i])\n dynalloc = FFMAX(2, dynalloc - 1);\n }\n if (consumed + (6 << 3) <= totalbits)\n alloctrim = opus_rc_getsymbol(rc, celt_model_alloc_trim);\n totalbits = (s->framebits << 3) - opus_rc_tell_frac(rc) - 1;\n s->anticollapse_bit = 0;\n if (s->blocks > 1 && s->duration >= 2 &&\n totalbits >= ((s->duration + 2) << 3))\n s->anticollapse_bit = 1 << 3;\n totalbits -= s->anticollapse_bit;\n if (totalbits >= 1 << 3)\n skip_bit = 1 << 3;\n totalbits -= skip_bit;\n if (s->coded_channels == 2) {\n intensitystereo_bit = celt_log2_frac[s->endband - s->startband];\n if (intensitystereo_bit <= totalbits) {\n totalbits -= intensitystereo_bit;\n if (totalbits >= 1 << 3) {\n dualstereo_bit = 1 << 3;\n totalbits -= 1 << 3;\n }\n } else\n intensitystereo_bit = 0;\n }\n for (i = s->startband; i < s->endband; i++) {\n int trim = alloctrim - 5 - s->duration;\n int band = celt_freq_range[i] * (s->endband - i - 1);\n int duration = s->duration + 3;\n int scale = duration + s->coded_channels - 1;\n threshold[i] = FFMAX(3 * celt_freq_range[i] << duration >> 4,\n s->coded_channels << 3);\n trim_offset[i] = trim * (band << scale) >> 6;\n if (celt_freq_range[i] << s->duration == 1)\n trim_offset[i] -= s->coded_channels << 3;\n }\n low = 1;\n high = CELT_VECTORS - 1;\n while (low <= high) {\n int center = (low + high) >> 1;\n done = total = 0;\n for (i = s->endband - 1; i >= s->startband; i--) {\n bandbits = celt_freq_range[i] * celt_static_alloc[center][i]\n << (s->coded_channels - 1) << s->duration >> 2;\n if (bandbits)\n bandbits = FFMAX(0, bandbits + trim_offset[i]);\n bandbits += boost[i];\n if (bandbits >= threshold[i] || done) {\n done = 1;\n total += FFMIN(bandbits, cap[i]);\n } else if (bandbits >= s->coded_channels << 3)\n total += s->coded_channels << 3;\n }\n if (total > totalbits)\n high = center - 1;\n else\n low = center + 1;\n }\n high = low--;\n for (i = s->startband; i < s->endband; i++) {\n bits1[i] = celt_freq_range[i] * celt_static_alloc[low][i]\n << (s->coded_channels - 1) << s->duration >> 2;\n bits2[i] = high >= CELT_VECTORS ? cap[i] :\n celt_freq_range[i] * celt_static_alloc[high][i]\n << (s->coded_channels - 1) << s->duration >> 2;\n if (bits1[i])\n bits1[i] = FFMAX(0, bits1[i] + trim_offset[i]);\n if (bits2[i])\n bits2[i] = FFMAX(0, bits2[i] + trim_offset[i]);\n if (low)\n bits1[i] += boost[i];\n bits2[i] += boost[i];\n if (boost[i])\n skip_startband = i;\n bits2[i] = FFMAX(0, bits2[i] - bits1[i]);\n }\n low = 0;\n high = 1 << CELT_ALLOC_STEPS;\n for (i = 0; i < CELT_ALLOC_STEPS; i++) {\n int center = (low + high) >> 1;\n done = total = 0;\n for (j = s->endband - 1; j >= s->startband; j--) {\n bandbits = bits1[j] + (center * bits2[j] >> CELT_ALLOC_STEPS);\n if (bandbits >= threshold[j] || done) {\n done = 1;\n total += FFMIN(bandbits, cap[j]);\n } else if (bandbits >= s->coded_channels << 3)\n total += s->coded_channels << 3;\n }\n if (total > totalbits)\n high = center;\n else\n low = center;\n }\n done = total = 0;\n for (i = s->endband - 1; i >= s->startband; i--) {\n bandbits = bits1[i] + (low * bits2[i] >> CELT_ALLOC_STEPS);\n if (bandbits >= threshold[i] || done)\n done = 1;\n else\n bandbits = (bandbits >= s->coded_channels << 3) ?\n s->coded_channels << 3 : 0;\n bandbits = FFMIN(bandbits, cap[i]);\n s->pulses[i] = bandbits;\n total += bandbits;\n }\n for (s->codedbands = s->endband; ; s->codedbands--) {\n int allocation;\n j = s->codedbands - 1;\n if (j == skip_startband) {\n totalbits += skip_bit;\n break;\n }\n remaining = totalbits - total;\n bandbits = remaining / (celt_freq_bands[j+1] - celt_freq_bands[s->startband]);\n remaining -= bandbits * (celt_freq_bands[j+1] - celt_freq_bands[s->startband]);\n allocation = s->pulses[j] + bandbits * celt_freq_range[j]\n + FFMAX(0, remaining - (celt_freq_bands[j] - celt_freq_bands[s->startband]));\n if (allocation >= FFMAX(threshold[j], (s->coded_channels + 1) <<3 )) {\n if (opus_rc_p2model(rc, 1))\n break;\n total += 1 << 3;\n allocation -= 1 << 3;\n }\n total -= s->pulses[j];\n if (intensitystereo_bit) {\n total -= intensitystereo_bit;\n intensitystereo_bit = celt_log2_frac[j - s->startband];\n total += intensitystereo_bit;\n }\n total += s->pulses[j] = (allocation >= s->coded_channels << 3) ?\n s->coded_channels << 3 : 0;\n }\n s->intensitystereo = 0;\n s->dualstereo = 0;\n if (intensitystereo_bit)\n s->intensitystereo = s->startband +\n opus_rc_unimodel(rc, s->codedbands + 1 - s->startband);\n if (s->intensitystereo <= s->startband)\n totalbits += dualstereo_bit;\n else if (dualstereo_bit)\n s->dualstereo = opus_rc_p2model(rc, 1);\n remaining = totalbits - total;\n bandbits = remaining / (celt_freq_bands[s->codedbands] - celt_freq_bands[s->startband]);\n remaining -= bandbits * (celt_freq_bands[s->codedbands] - celt_freq_bands[s->startband]);\n for (i = s->startband; i < s->codedbands; i++) {\n int bits = FFMIN(remaining, celt_freq_range[i]);\n s->pulses[i] += bits + bandbits * celt_freq_range[i];\n remaining -= bits;\n }\n for (i = s->startband; i < s->codedbands; i++) {\n int N = celt_freq_range[i] << s->duration;\n int prev_extra = extrabits;\n s->pulses[i] += extrabits;\n if (N > 1) {\n int dof;\n int temp;\n int offset;\n int fine_bits, max_bits;\n extrabits = FFMAX(0, s->pulses[i] - cap[i]);\n s->pulses[i] -= extrabits;\n dof = N * s->coded_channels\n + (s->coded_channels == 2 && N > 2 && !s->dualstereo && i < s->intensitystereo);\n temp = dof * (celt_log_freq_range[i] + (s->duration<<3));\n offset = (temp >> 1) - dof * CELT_FINE_OFFSET;\n if (N == 2)\n offset += dof<<1;\n if (s->pulses[i] + offset < 2 * (dof << 3))\n offset += temp >> 2;\n else if (s->pulses[i] + offset < 3 * (dof << 3))\n offset += temp >> 3;\n fine_bits = (s->pulses[i] + offset + (dof << 2)) / (dof << 3);\n max_bits = FFMIN((s->pulses[i]>>3) >> (s->coded_channels - 1),\n CELT_MAX_FINE_BITS);\n max_bits = FFMAX(max_bits, 0);\n s->fine_bits[i] = av_clip(fine_bits, 0, max_bits);\n s->fine_priority[i] = (s->fine_bits[i] * (dof<<3) >= s->pulses[i] + offset);\n s->pulses[i] -= s->fine_bits[i] << (s->coded_channels - 1) << 3;\n } else {\n extrabits = FFMAX(0, s->pulses[i] - (s->coded_channels << 3));\n s->pulses[i] -= extrabits;\n s->fine_bits[i] = 0;\n s->fine_priority[i] = 1;\n }\n if (extrabits > 0) {\n int fineextra = FFMIN(extrabits >> (s->coded_channels + 2),\n CELT_MAX_FINE_BITS - s->fine_bits[i]);\n s->fine_bits[i] += fineextra;\n fineextra <<= s->coded_channels + 2;\n s->fine_priority[i] = (fineextra >= extrabits - prev_extra);\n extrabits -= fineextra;\n }\n }\n s->remaining = extrabits;\n for (; i < s->endband; i++) {\n s->fine_bits[i] = s->pulses[i] >> (s->coded_channels - 1) >> 3;\n s->pulses[i] = 0;\n s->fine_priority[i] = s->fine_bits[i] < 1;\n }\n}']
|
2,693
| 0
|
https://github.com/libav/libav/blob/a4baf96abd64cda68e3e4692b182bdf60745a14d/libavutil/mem.c/#L173
|
void av_free(void *ptr)
{
#if CONFIG_MEMALIGN_HACK
if (ptr)
free((char *)ptr - ((char *)ptr)[-1]);
#elif HAVE_ALIGNED_MALLOC
_aligned_free(ptr);
#else
free(ptr);
#endif
}
|
['static int swf_write_trailer(AVFormatContext *s)\n{\n SWFContext *swf = s->priv_data;\n AVIOContext *pb = s->pb;\n AVCodecContext *enc, *video_enc;\n int file_size, i;\n video_enc = NULL;\n for(i=0;i<s->nb_streams;i++) {\n enc = s->streams[i]->codec;\n if (enc->codec_type == AVMEDIA_TYPE_VIDEO)\n video_enc = enc;\n else\n av_fifo_free(swf->audio_fifo);\n }\n put_swf_tag(s, TAG_END);\n put_swf_end_tag(s);\n if (s->pb->seekable && video_enc) {\n file_size = avio_tell(pb);\n avio_seek(pb, 4, SEEK_SET);\n avio_wl32(pb, file_size);\n avio_seek(pb, swf->duration_pos, SEEK_SET);\n avio_wl16(pb, swf->video_frame_number);\n avio_seek(pb, swf->vframes_pos, SEEK_SET);\n avio_wl16(pb, swf->video_frame_number);\n avio_seek(pb, file_size, SEEK_SET);\n }\n return 0;\n}', 'void av_fifo_free(AVFifoBuffer *f)\n{\n if (f) {\n av_free(f->buffer);\n av_free(f);\n }\n}', 'void av_free(void *ptr)\n{\n#if CONFIG_MEMALIGN_HACK\n if (ptr)\n free((char *)ptr - ((char *)ptr)[-1]);\n#elif HAVE_ALIGNED_MALLOC\n _aligned_free(ptr);\n#else\n free(ptr);\n#endif\n}']
|
2,694
| 0
|
https://github.com/openssl/openssl/blob/877e8e970c3c94c43ce1db50fdbb8e9b0342b90e/crypto/bn/bn_asm.c/#L405
|
BN_ULONG bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)
{
BN_ULONG t1,t2;
int c=0;
assert(n >= 0);
if (n <= 0) return((BN_ULONG)0);
#ifndef OPENSSL_SMALL_FOOTPRINT
while (n&~3)
{
t1=a[0]; t2=b[0];
r[0]=(t1-t2-c)&BN_MASK2;
if (t1 != t2) c=(t1 < t2);
t1=a[1]; t2=b[1];
r[1]=(t1-t2-c)&BN_MASK2;
if (t1 != t2) c=(t1 < t2);
t1=a[2]; t2=b[2];
r[2]=(t1-t2-c)&BN_MASK2;
if (t1 != t2) c=(t1 < t2);
t1=a[3]; t2=b[3];
r[3]=(t1-t2-c)&BN_MASK2;
if (t1 != t2) c=(t1 < t2);
a+=4; b+=4; r+=4; n-=4;
}
#endif
while (n)
{
t1=a[0]; t2=b[0];
r[0]=(t1-t2-c)&BN_MASK2;
if (t1 != t2) c=(t1 < t2);
a++; b++; r++; n--;
}
return(c);
}
|
['int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n\t\t const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n\t{\n\tint i,bits,ret=0,idx,window,wvalue;\n\tint top;\n \tBIGNUM *r;\n\tconst BIGNUM *aa;\n\tBN_MONT_CTX *mont=NULL;\n\tint numPowers;\n\tunsigned char *powerbufFree=NULL;\n\tint powerbufLen = 0;\n\tunsigned char *powerbuf=NULL;\n\tBIGNUM *computeTemp=NULL, *am=NULL;\n\tbn_check_top(a);\n\tbn_check_top(p);\n\tbn_check_top(m);\n\ttop = m->top;\n\tif (!(m->d[0] & 1))\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_EXP_MONT_CONSTTIME,BN_R_CALLED_WITH_EVEN_MODULUS);\n\t\treturn(0);\n\t\t}\n\tbits=BN_num_bits(p);\n\tif (bits == 0)\n\t\t{\n\t\tret = BN_one(rr);\n\t\treturn ret;\n\t\t}\n\tBN_CTX_start(ctx);\n\tr = BN_CTX_get(ctx);\n\tif (r == NULL) goto err;\n\tif (in_mont != NULL)\n\t\tmont=in_mont;\n\telse\n\t\t{\n\t\tif ((mont=BN_MONT_CTX_new()) == NULL) goto err;\n\t\tif (!BN_MONT_CTX_set(mont,m,ctx)) goto err;\n\t\t}\n\twindow = BN_window_bits_for_ctime_exponent_size(bits);\n\tnumPowers = 1 << window;\n\tpowerbufLen = sizeof(m->d[0])*top*numPowers;\n\tif ((powerbufFree=(unsigned char*)OPENSSL_malloc(powerbufLen+MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH)) == NULL)\n\t\tgoto err;\n\tpowerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);\n\tmemset(powerbuf, 0, powerbufLen);\n \tif (!BN_to_montgomery(r,BN_value_one(),mont,ctx)) goto err;\n\tif (!MOD_EXP_CTIME_COPY_TO_PREBUF(r, top, powerbuf, 0, numPowers)) goto err;\n\tcomputeTemp = BN_CTX_get(ctx);\n\tam = BN_CTX_get(ctx);\n\tif (computeTemp==NULL || am==NULL) goto err;\n\tif (a->neg || BN_ucmp(a,m) >= 0)\n\t\t{\n\t\tif (!BN_mod(am,a,m,ctx))\n\t\t\tgoto err;\n\t\taa= am;\n\t\t}\n\telse\n\t\taa=a;\n\tif (!BN_to_montgomery(am,aa,mont,ctx)) goto err;\n\tif (!BN_copy(computeTemp, am)) goto err;\n\tif (!MOD_EXP_CTIME_COPY_TO_PREBUF(am, top, powerbuf, 1, numPowers)) goto err;\n\tif (window > 1)\n\t\t{\n\t\tfor (i=2; i<numPowers; i++)\n\t\t\t{\n\t\t\tif (!BN_mod_mul_montgomery(computeTemp,am,computeTemp,mont,ctx))\n\t\t\t\tgoto err;\n\t\t\tif (!MOD_EXP_CTIME_COPY_TO_PREBUF(computeTemp, top, powerbuf, i, numPowers)) goto err;\n\t\t\t}\n\t\t}\n \tbits = ((bits+window-1)/window)*window;\n \tidx=bits-1;\n \twhile (idx >= 0)\n \t\t{\n \t\twvalue=0;\n \t\tfor (i=0; i<window; i++,idx--)\n \t\t\t{\n\t\t\tif (!BN_mod_mul_montgomery(r,r,r,mont,ctx))\tgoto err;\n\t\t\twvalue = (wvalue<<1)+BN_is_bit_set(p,idx);\n \t\t\t}\n\t\tif (!MOD_EXP_CTIME_COPY_FROM_PREBUF(computeTemp, top, powerbuf, wvalue, numPowers)) goto err;\n \t\tif (!BN_mod_mul_montgomery(r,r,computeTemp,mont,ctx)) goto err;\n \t\t}\n\tif (!BN_from_montgomery(rr,r,mont,ctx)) goto err;\n\tret=1;\nerr:\n\tif ((in_mont == NULL) && (mont != NULL)) BN_MONT_CTX_free(mont);\n\tif (powerbuf!=NULL)\n\t\t{\n\t\tOPENSSL_cleanse(powerbuf,powerbufLen);\n\t\tOPENSSL_free(powerbufFree);\n\t\t}\n \tif (am!=NULL) BN_clear(am);\n \tif (computeTemp!=NULL) BN_clear(computeTemp);\n\tBN_CTX_end(ctx);\n\treturn(ret);\n\t}', 'const BIGNUM *BN_value_one(void)\n\t{\n\tstatic BN_ULONG data_one=1L;\n\tstatic BIGNUM const_one={&data_one,1,1,0,BN_FLG_STATIC_DATA};\n\treturn(&const_one);\n\t}', 'int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n\t\t\t BN_MONT_CTX *mont, BN_CTX *ctx)\n\t{\n\tBIGNUM *tmp;\n\tint ret=0;\n#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\n\tint num = mont->N.top;\n\tif (num>1 && a->top==num && b->top==num)\n\t\t{\n\t\tif (bn_wexpand(r,num) == NULL) return(0);\n\t\tif (bn_mul_mont(r->d,a->d,b->d,mont->N.d,mont->n0,num))\n\t\t\t{\n\t\t\tr->neg = a->neg^b->neg;\n\t\t\tr->top = num;\n\t\t\tbn_correct_top(r);\n\t\t\treturn(1);\n\t\t\t}\n\t\t}\n#endif\n\tBN_CTX_start(ctx);\n\ttmp = BN_CTX_get(ctx);\n\tif (tmp == NULL) goto err;\n\tbn_check_top(tmp);\n\tif (a == b)\n\t\t{\n\t\tif (!BN_sqr(tmp,a,ctx)) goto err;\n\t\t}\n\telse\n\t\t{\n\t\tif (!BN_mul(tmp,a,b,ctx)) goto err;\n\t\t}\n#ifdef MONT_WORD\n\tif (!BN_from_montgomery_word(r,tmp,mont)) goto err;\n#else\n\tif (!BN_from_montgomery(r,tmp,mont,ctx)) goto err;\n#endif\n\tbn_check_top(r);\n\tret=1;\nerr:\n\tBN_CTX_end(ctx);\n\treturn(ret);\n\t}', 'int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n\t{\n\tint ret=0;\n\tint top,al,bl;\n\tBIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\tint i;\n#endif\n#ifdef BN_RECURSION\n\tBIGNUM *t=NULL;\n\tint j=0,k;\n#endif\n#ifdef BN_COUNT\n\tfprintf(stderr,"BN_mul %d * %d\\n",a->top,b->top);\n#endif\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tbn_check_top(r);\n\tal=a->top;\n\tbl=b->top;\n\tif ((al == 0) || (bl == 0))\n\t\t{\n\t\tBN_zero(r);\n\t\treturn(1);\n\t\t}\n\ttop=al+bl;\n\tBN_CTX_start(ctx);\n\tif ((r == a) || (r == b))\n\t\t{\n\t\tif ((rr = BN_CTX_get(ctx)) == NULL) goto err;\n\t\t}\n\telse\n\t\trr = r;\n\trr->neg=a->neg^b->neg;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\ti = al-bl;\n#endif\n#ifdef BN_MUL_COMBA\n\tif (i == 0)\n\t\t{\n# if 0\n\t\tif (al == 4)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,8) == NULL) goto err;\n\t\t\trr->top=8;\n\t\t\tbn_mul_comba4(rr->d,a->d,b->d);\n\t\t\tgoto end;\n\t\t\t}\n# endif\n\t\tif (al == 8)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,16) == NULL) goto err;\n\t\t\trr->top=16;\n\t\t\tbn_mul_comba8(rr->d,a->d,b->d);\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n#endif\n#ifdef BN_RECURSION\n\tif ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL))\n\t\t{\n\t\tif (i >= -1 && i <= 1)\n\t\t\t{\n\t\t\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}', 'void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n,\n\t int tna, int tnb, BN_ULONG *t)\n\t{\n\tint i,j,n2=n*2;\n\tint c1,c2,neg,zero;\n\tBN_ULONG ln,lo,*p;\n# ifdef BN_COUNT\n\tfprintf(stderr," bn_mul_part_recursive (%d+%d) * (%d+%d)\\n",\n\t\ttna, n, tnb, n);\n# endif\n\tif (n < 8)\n\t\t{\n\t\tbn_mul_normal(r,a,n+tna,b,n+tnb);\n\t\treturn;\n\t\t}\n\tc1=bn_cmp_part_words(a,&(a[n]),tna,n-tna);\n\tc2=bn_cmp_part_words(&(b[n]),b,tnb,tnb-n);\n\tzero=neg=0;\n\tswitch (c1*3+c2)\n\t\t{\n\tcase -4:\n\t\tbn_sub_part_words(t, &(a[n]),a, tna,tna-n);\n\t\tbn_sub_part_words(&(t[n]),b, &(b[n]),tnb,n-tnb);\n\t\tbreak;\n\tcase -3:\n\t\tzero=1;\n\tcase -2:\n\t\tbn_sub_part_words(t, &(a[n]),a, tna,tna-n);\n\t\tbn_sub_part_words(&(t[n]),&(b[n]),b, tnb,tnb-n);\n\t\tneg=1;\n\t\tbreak;\n\tcase -1:\n\tcase 0:\n\tcase 1:\n\t\tzero=1;\n\tcase 2:\n\t\tbn_sub_part_words(t, a, &(a[n]),tna,n-tna);\n\t\tbn_sub_part_words(&(t[n]),b, &(b[n]),tnb,n-tnb);\n\t\tneg=1;\n\t\tbreak;\n\tcase 3:\n\t\tzero=1;\n\tcase 4:\n\t\tbn_sub_part_words(t, a, &(a[n]),tna,n-tna);\n\t\tbn_sub_part_words(&(t[n]),&(b[n]),b, tnb,tnb-n);\n\t\tbreak;\n\t\t}\n# if 0\n\tif (n == 4)\n\t\t{\n\t\tbn_mul_comba4(&(t[n2]),t,&(t[n]));\n\t\tbn_mul_comba4(r,a,b);\n\t\tbn_mul_normal(&(r[n2]),&(a[n]),tn,&(b[n]),tn);\n\t\tmemset(&(r[n2+tn*2]),0,sizeof(BN_ULONG)*(n2-tn*2));\n\t\t}\n\telse\n# endif\n\tif (n == 8)\n\t\t{\n\t\tbn_mul_comba8(&(t[n2]),t,&(t[n]));\n\t\tbn_mul_comba8(r,a,b);\n\t\tbn_mul_normal(&(r[n2]),&(a[n]),tna,&(b[n]),tnb);\n\t\tmemset(&(r[n2+tna+tnb]),0,sizeof(BN_ULONG)*(n2-tna-tnb));\n\t\t}\n\telse\n\t\t{\n\t\tp= &(t[n2*2]);\n\t\tbn_mul_recursive(&(t[n2]),t,&(t[n]),n,0,0,p);\n\t\tbn_mul_recursive(r,a,b,n,0,0,p);\n\t\ti=n/2;\n\t\tif (tna > tnb)\n\t\t\tj = tna - i;\n\t\telse\n\t\t\tj = tnb - i;\n\t\tif (j == 0)\n\t\t\t{\n\t\t\tbn_mul_recursive(&(r[n2]),&(a[n]),&(b[n]),\n\t\t\t\ti,tna-i,tnb-i,p);\n\t\t\tmemset(&(r[n2+i*2]),0,sizeof(BN_ULONG)*(n2-i*2));\n\t\t\t}\n\t\telse if (j > 0)\n\t\t\t\t{\n\t\t\t\tbn_mul_part_recursive(&(r[n2]),&(a[n]),&(b[n]),\n\t\t\t\t\ti,tna-i,tnb-i,p);\n\t\t\t\tmemset(&(r[n2+tna+tnb]),0,\n\t\t\t\t\tsizeof(BN_ULONG)*(n2-tna-tnb));\n\t\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tmemset(&(r[n2]),0,sizeof(BN_ULONG)*n2);\n\t\t\tif (tna < BN_MUL_RECURSIVE_SIZE_NORMAL\n\t\t\t\t&& tnb < BN_MUL_RECURSIVE_SIZE_NORMAL)\n\t\t\t\t{\n\t\t\t\tbn_mul_normal(&(r[n2]),&(a[n]),tna,&(b[n]),tnb);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tfor (;;)\n\t\t\t\t\t{\n\t\t\t\t\ti/=2;\n\t\t\t\t\tif (i < tna && i < tnb)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tbn_mul_part_recursive(&(r[n2]),\n\t\t\t\t\t\t\t&(a[n]),&(b[n]),\n\t\t\t\t\t\t\ti,tna-i,tnb-i,p);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\telse if (i <= tna && i <= tnb)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tbn_mul_recursive(&(r[n2]),\n\t\t\t\t\t\t\t&(a[n]),&(b[n]),\n\t\t\t\t\t\t\ti,tna-i,tnb-i,p);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\tc1=(int)(bn_add_words(t,r,&(r[n2]),n2));\n\tif (neg)\n\t\t{\n\t\tc1-=(int)(bn_sub_words(&(t[n2]),t,&(t[n2]),n2));\n\t\t}\n\telse\n\t\t{\n\t\tc1+=(int)(bn_add_words(&(t[n2]),&(t[n2]),t,n2));\n\t\t}\n\tc1+=(int)(bn_add_words(&(r[n]),&(r[n]),&(t[n2]),n2));\n\tif (c1)\n\t\t{\n\t\tp= &(r[n+n2]);\n\t\tlo= *p;\n\t\tln=(lo+c1)&BN_MASK2;\n\t\t*p=ln;\n\t\tif (ln < (BN_ULONG)c1)\n\t\t\t{\n\t\t\tdo\t{\n\t\t\t\tp++;\n\t\t\t\tlo= *p;\n\t\t\t\tln=(lo+1)&BN_MASK2;\n\t\t\t\t*p=ln;\n\t\t\t\t} while (ln == 0);\n\t\t\t}\n\t\t}\n\t}', 'int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b,\n\tint cl, int dl)\n\t{\n\tint n,i;\n\tn = cl-1;\n\tif (dl < 0)\n\t\t{\n\t\tfor (i=dl; i<0; i++)\n\t\t\t{\n\t\t\tif (b[n-i] != 0)\n\t\t\t\treturn -1;\n\t\t\t}\n\t\t}\n\tif (dl > 0)\n\t\t{\n\t\tfor (i=dl; i>0; i--)\n\t\t\t{\n\t\t\tif (a[n+i] != 0)\n\t\t\t\treturn 1;\n\t\t\t}\n\t\t}\n\treturn bn_cmp_words(a,b,cl);\n\t}', 'BN_ULONG bn_sub_part_words(BN_ULONG *r,\n\tconst BN_ULONG *a, const BN_ULONG *b,\n\tint cl, int dl)\n\t{\n\tBN_ULONG c, t;\n\tassert(cl >= 0);\n\tc = bn_sub_words(r, a, b, cl);\n\tif (dl == 0)\n\t\treturn c;\n\tr += cl;\n\ta += cl;\n\tb += cl;\n\tif (dl < 0)\n\t\t{\n#ifdef BN_COUNT\n\t\tfprintf(stderr, " bn_sub_part_words %d + %d (dl < 0, c = %d)\\n", cl, dl, c);\n#endif\n\t\tfor (;;)\n\t\t\t{\n\t\t\tt = b[0];\n\t\t\tr[0] = (0-t-c)&BN_MASK2;\n\t\t\tif (t != 0) c=1;\n\t\t\tif (++dl >= 0) break;\n\t\t\tt = b[1];\n\t\t\tr[1] = (0-t-c)&BN_MASK2;\n\t\t\tif (t != 0) c=1;\n\t\t\tif (++dl >= 0) break;\n\t\t\tt = b[2];\n\t\t\tr[2] = (0-t-c)&BN_MASK2;\n\t\t\tif (t != 0) c=1;\n\t\t\tif (++dl >= 0) break;\n\t\t\tt = b[3];\n\t\t\tr[3] = (0-t-c)&BN_MASK2;\n\t\t\tif (t != 0) c=1;\n\t\t\tif (++dl >= 0) break;\n\t\t\tb += 4;\n\t\t\tr += 4;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tint save_dl = dl;\n#ifdef BN_COUNT\n\t\tfprintf(stderr, " bn_sub_part_words %d + %d (dl > 0, c = %d)\\n", cl, dl, c);\n#endif\n\t\twhile(c)\n\t\t\t{\n\t\t\tt = a[0];\n\t\t\tr[0] = (t-c)&BN_MASK2;\n\t\t\tif (t != 0) c=0;\n\t\t\tif (--dl <= 0) break;\n\t\t\tt = a[1];\n\t\t\tr[1] = (t-c)&BN_MASK2;\n\t\t\tif (t != 0) c=0;\n\t\t\tif (--dl <= 0) break;\n\t\t\tt = a[2];\n\t\t\tr[2] = (t-c)&BN_MASK2;\n\t\t\tif (t != 0) c=0;\n\t\t\tif (--dl <= 0) break;\n\t\t\tt = a[3];\n\t\t\tr[3] = (t-c)&BN_MASK2;\n\t\t\tif (t != 0) c=0;\n\t\t\tif (--dl <= 0) break;\n\t\t\tsave_dl = dl;\n\t\t\ta += 4;\n\t\t\tr += 4;\n\t\t\t}\n\t\tif (dl > 0)\n\t\t\t{\n#ifdef BN_COUNT\n\t\t\tfprintf(stderr, " bn_sub_part_words %d + %d (dl > 0, c == 0)\\n", cl, dl);\n#endif\n\t\t\tif (save_dl > dl)\n\t\t\t\t{\n\t\t\t\tswitch (save_dl - dl)\n\t\t\t\t\t{\n\t\t\t\tcase 1:\n\t\t\t\t\tr[1] = a[1];\n\t\t\t\t\tif (--dl <= 0) break;\n\t\t\t\tcase 2:\n\t\t\t\t\tr[2] = a[2];\n\t\t\t\t\tif (--dl <= 0) break;\n\t\t\t\tcase 3:\n\t\t\t\t\tr[3] = a[3];\n\t\t\t\t\tif (--dl <= 0) break;\n\t\t\t\t\t}\n\t\t\t\ta += 4;\n\t\t\t\tr += 4;\n\t\t\t\t}\n\t\t\t}\n\t\tif (dl > 0)\n\t\t\t{\n#ifdef BN_COUNT\n\t\t\tfprintf(stderr, " bn_sub_part_words %d + %d (dl > 0, copy)\\n", cl, dl);\n#endif\n\t\t\tfor(;;)\n\t\t\t\t{\n\t\t\t\tr[0] = a[0];\n\t\t\t\tif (--dl <= 0) break;\n\t\t\t\tr[1] = a[1];\n\t\t\t\tif (--dl <= 0) break;\n\t\t\t\tr[2] = a[2];\n\t\t\t\tif (--dl <= 0) break;\n\t\t\t\tr[3] = a[3];\n\t\t\t\tif (--dl <= 0) break;\n\t\t\t\ta += 4;\n\t\t\t\tr += 4;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\treturn c;\n\t}', 'BN_ULONG bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)\n {\n\tBN_ULONG t1,t2;\n\tint c=0;\n\tassert(n >= 0);\n\tif (n <= 0) return((BN_ULONG)0);\n#ifndef OPENSSL_SMALL_FOOTPRINT\n\twhile (n&~3)\n\t\t{\n\t\tt1=a[0]; t2=b[0];\n\t\tr[0]=(t1-t2-c)&BN_MASK2;\n\t\tif (t1 != t2) c=(t1 < t2);\n\t\tt1=a[1]; t2=b[1];\n\t\tr[1]=(t1-t2-c)&BN_MASK2;\n\t\tif (t1 != t2) c=(t1 < t2);\n\t\tt1=a[2]; t2=b[2];\n\t\tr[2]=(t1-t2-c)&BN_MASK2;\n\t\tif (t1 != t2) c=(t1 < t2);\n\t\tt1=a[3]; t2=b[3];\n\t\tr[3]=(t1-t2-c)&BN_MASK2;\n\t\tif (t1 != t2) c=(t1 < t2);\n\t\ta+=4; b+=4; r+=4; n-=4;\n\t\t}\n#endif\n\twhile (n)\n\t\t{\n\t\tt1=a[0]; t2=b[0];\n\t\tr[0]=(t1-t2-c)&BN_MASK2;\n\t\tif (t1 != t2) c=(t1 < t2);\n\t\ta++; b++; r++; n--;\n\t\t}\n\treturn(c);\n\t}']
|
2,695
| 0
|
https://github.com/libav/libav/blob/b9630bcf8215034f5a50e1f6835d4c4366413618/libavcodec/vc1dec.c/#L3475
|
static void vc1_draw_sprites(VC1Context *v, SpriteData* sd)
{
int i, plane, row, sprite;
int sr_cache[2][2] = { { -1, -1 }, { -1, -1 } };
uint8_t* src_h[2][2];
int xoff[2], xadv[2], yoff[2], yadv[2], alpha;
int ysub[2];
MpegEncContext *s = &v->s;
for (i = 0; i < 2; i++) {
xoff[i] = av_clip(sd->coefs[i][2], 0, v->sprite_width-1 << 16);
xadv[i] = sd->coefs[i][0];
if (xadv[i] != 1<<16 || (v->sprite_width<<16) - (v->output_width<<16) - xoff[i])
xadv[i] = av_clip(xadv[i], 0, ((v->sprite_width<<16) - xoff[i] - 1) / v->output_width);
yoff[i] = av_clip(sd->coefs[i][5], 0, v->sprite_height-1 << 16);
yadv[i] = av_clip(sd->coefs[i][4], 0, ((v->sprite_height<<16) - yoff[i]) / v->output_height);
}
alpha = av_clip(sd->coefs[1][6], 0, (1<<16) - 1);
for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++) {
int width = v->output_width>>!!plane;
for (row = 0; row < v->output_height>>!!plane; row++) {
uint8_t *dst = v->sprite_output_frame.data[plane] +
v->sprite_output_frame.linesize[plane] * row;
for (sprite = 0; sprite <= v->two_sprites; sprite++) {
uint8_t *iplane = s->current_picture.f.data[plane];
int iline = s->current_picture.f.linesize[plane];
int ycoord = yoff[sprite] + yadv[sprite]*row;
int yline = ycoord>>16;
ysub[sprite] = ycoord&0xFFFF;
if (sprite) {
iplane = s->last_picture.f.data[plane];
iline = s->last_picture.f.linesize[plane];
}
if (!(xoff[sprite]&0xFFFF) && xadv[sprite] == 1<<16) {
src_h[sprite][0] = iplane+(xoff[sprite]>>16)+ yline *iline;
if (ysub[sprite])
src_h[sprite][1] = iplane+(xoff[sprite]>>16)+(yline+1)*iline;
} else {
if (sr_cache[sprite][0] != yline) {
if (sr_cache[sprite][1] == yline) {
FFSWAP(uint8_t*, v->sr_rows[sprite][0], v->sr_rows[sprite][1]);
FFSWAP(int, sr_cache[sprite][0], sr_cache[sprite][1]);
} else {
v->vc1dsp.sprite_h(v->sr_rows[sprite][0], iplane+yline*iline, xoff[sprite], xadv[sprite], width);
sr_cache[sprite][0] = yline;
}
}
if (ysub[sprite] && sr_cache[sprite][1] != yline + 1) {
v->vc1dsp.sprite_h(v->sr_rows[sprite][1], iplane+(yline+1)*iline, xoff[sprite], xadv[sprite], width);
sr_cache[sprite][1] = yline + 1;
}
src_h[sprite][0] = v->sr_rows[sprite][0];
src_h[sprite][1] = v->sr_rows[sprite][1];
}
}
if (!v->two_sprites) {
if (ysub[0]) {
v->vc1dsp.sprite_v_single(dst, src_h[0][0], src_h[0][1], ysub[0], width);
} else {
memcpy(dst, src_h[0][0], width);
}
} else {
if (ysub[0] && ysub[1]) {
v->vc1dsp.sprite_v_double_twoscale(dst, src_h[0][0], src_h[0][1], ysub[0],
src_h[1][0], src_h[1][1], ysub[1], alpha, width);
} else if (ysub[0]) {
v->vc1dsp.sprite_v_double_onescale(dst, src_h[0][0], src_h[0][1], ysub[0],
src_h[1][0], alpha, width);
} else if (ysub[1]) {
v->vc1dsp.sprite_v_double_onescale(dst, src_h[1][0], src_h[1][1], ysub[1],
src_h[0][0], (1<<16)-1-alpha, width);
} else {
v->vc1dsp.sprite_v_double_noscale(dst, src_h[0][0], src_h[1][0], alpha, width);
}
}
}
if (!plane) {
for (i = 0; i < 2; i++) {
xoff[i] >>= 1;
yoff[i] >>= 1;
}
}
}
}
|
['static void vc1_draw_sprites(VC1Context *v, SpriteData* sd)\n{\n int i, plane, row, sprite;\n int sr_cache[2][2] = { { -1, -1 }, { -1, -1 } };\n uint8_t* src_h[2][2];\n int xoff[2], xadv[2], yoff[2], yadv[2], alpha;\n int ysub[2];\n MpegEncContext *s = &v->s;\n for (i = 0; i < 2; i++) {\n xoff[i] = av_clip(sd->coefs[i][2], 0, v->sprite_width-1 << 16);\n xadv[i] = sd->coefs[i][0];\n if (xadv[i] != 1<<16 || (v->sprite_width<<16) - (v->output_width<<16) - xoff[i])\n xadv[i] = av_clip(xadv[i], 0, ((v->sprite_width<<16) - xoff[i] - 1) / v->output_width);\n yoff[i] = av_clip(sd->coefs[i][5], 0, v->sprite_height-1 << 16);\n yadv[i] = av_clip(sd->coefs[i][4], 0, ((v->sprite_height<<16) - yoff[i]) / v->output_height);\n }\n alpha = av_clip(sd->coefs[1][6], 0, (1<<16) - 1);\n for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++) {\n int width = v->output_width>>!!plane;\n for (row = 0; row < v->output_height>>!!plane; row++) {\n uint8_t *dst = v->sprite_output_frame.data[plane] +\n v->sprite_output_frame.linesize[plane] * row;\n for (sprite = 0; sprite <= v->two_sprites; sprite++) {\n uint8_t *iplane = s->current_picture.f.data[plane];\n int iline = s->current_picture.f.linesize[plane];\n int ycoord = yoff[sprite] + yadv[sprite]*row;\n int yline = ycoord>>16;\n ysub[sprite] = ycoord&0xFFFF;\n if (sprite) {\n iplane = s->last_picture.f.data[plane];\n iline = s->last_picture.f.linesize[plane];\n }\n if (!(xoff[sprite]&0xFFFF) && xadv[sprite] == 1<<16) {\n src_h[sprite][0] = iplane+(xoff[sprite]>>16)+ yline *iline;\n if (ysub[sprite])\n src_h[sprite][1] = iplane+(xoff[sprite]>>16)+(yline+1)*iline;\n } else {\n if (sr_cache[sprite][0] != yline) {\n if (sr_cache[sprite][1] == yline) {\n FFSWAP(uint8_t*, v->sr_rows[sprite][0], v->sr_rows[sprite][1]);\n FFSWAP(int, sr_cache[sprite][0], sr_cache[sprite][1]);\n } else {\n v->vc1dsp.sprite_h(v->sr_rows[sprite][0], iplane+yline*iline, xoff[sprite], xadv[sprite], width);\n sr_cache[sprite][0] = yline;\n }\n }\n if (ysub[sprite] && sr_cache[sprite][1] != yline + 1) {\n v->vc1dsp.sprite_h(v->sr_rows[sprite][1], iplane+(yline+1)*iline, xoff[sprite], xadv[sprite], width);\n sr_cache[sprite][1] = yline + 1;\n }\n src_h[sprite][0] = v->sr_rows[sprite][0];\n src_h[sprite][1] = v->sr_rows[sprite][1];\n }\n }\n if (!v->two_sprites) {\n if (ysub[0]) {\n v->vc1dsp.sprite_v_single(dst, src_h[0][0], src_h[0][1], ysub[0], width);\n } else {\n memcpy(dst, src_h[0][0], width);\n }\n } else {\n if (ysub[0] && ysub[1]) {\n v->vc1dsp.sprite_v_double_twoscale(dst, src_h[0][0], src_h[0][1], ysub[0],\n src_h[1][0], src_h[1][1], ysub[1], alpha, width);\n } else if (ysub[0]) {\n v->vc1dsp.sprite_v_double_onescale(dst, src_h[0][0], src_h[0][1], ysub[0],\n src_h[1][0], alpha, width);\n } else if (ysub[1]) {\n v->vc1dsp.sprite_v_double_onescale(dst, src_h[1][0], src_h[1][1], ysub[1],\n src_h[0][0], (1<<16)-1-alpha, width);\n } else {\n v->vc1dsp.sprite_v_double_noscale(dst, src_h[0][0], src_h[1][0], alpha, width);\n }\n }\n }\n if (!plane) {\n for (i = 0; i < 2; i++) {\n xoff[i] >>= 1;\n yoff[i] >>= 1;\n }\n }\n }\n}']
|
2,696
| 0
|
https://github.com/openssl/openssl/blob/7ef8206859f9a52f48e817c023c744fe00e82c5d/crypto/bio/bss_file.c/#L110
|
BIO *BIO_new_file(const char *filename, const char *mode)
{
BIO *ret;
FILE *file;
if ((file=fopen(filename,mode)) == NULL)
{
SYSerr(SYS_F_FOPEN,get_last_sys_error());
ERR_add_error_data(5,"fopen('",filename,"','",mode,"')");
BIOerr(BIO_F_BIO_NEW_FILE,ERR_R_SYS_LIB);
return(NULL);
}
if ((ret=BIO_new(BIO_s_file_internal())) == NULL)
return(NULL);
BIO_set_fp(ret,file,BIO_CLOSE);
return(ret);
}
|
['BIO *BIO_new_file(const char *filename, const char *mode)\n\t{\n\tBIO *ret;\n\tFILE *file;\n\tif ((file=fopen(filename,mode)) == NULL)\n\t\t{\n\t\tSYSerr(SYS_F_FOPEN,get_last_sys_error());\n\t\tERR_add_error_data(5,"fopen(\'",filename,"\',\'",mode,"\')");\n\t\tBIOerr(BIO_F_BIO_NEW_FILE,ERR_R_SYS_LIB);\n\t\treturn(NULL);\n\t\t}\n\tif ((ret=BIO_new(BIO_s_file_internal())) == NULL)\n\t\treturn(NULL);\n\tBIO_set_fp(ret,file,BIO_CLOSE);\n\treturn(ret);\n\t}', 'BIO_METHOD *BIO_s_file(void)\n\t{\n\treturn(&methods_filep);\n\t}', 'BIO *BIO_new(BIO_METHOD *method)\n\t{\n\tBIO *ret=NULL;\n\tret=(BIO *)OPENSSL_malloc(sizeof(BIO));\n\tif (ret == NULL)\n\t\t{\n\t\tBIOerr(BIO_F_BIO_NEW,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tif (!BIO_set(ret,method))\n\t\t{\n\t\tOPENSSL_free(ret);\n\t\tret=NULL;\n\t\t}\n\treturn(ret);\n\t}', 'void *CRYPTO_malloc(int num, const char *file, int line)\n\t{\n\tvoid *ret = NULL;\n\tallow_customize = 0;\n\tif (malloc_debug_func != NULL)\n\t\t{\n\t\tallow_customize_debug = 0;\n\t\tmalloc_debug_func(NULL, num, file, line, 0);\n\t\t}\n\tret = malloc_func(num);\n#ifdef LEVITTE_DEBUG\n\tfprintf(stderr, "LEVITTE_DEBUG: > 0x%p (%d)\\n", ret, num);\n#endif\n\tif (malloc_debug_func != NULL)\n\t\tmalloc_debug_func(ret, num, file, line, 1);\n\treturn ret;\n\t}', 'void ERR_put_error(int lib, int func, int reason, const char *file,\n\t int line)\n\t{\n\tERR_STATE *es;\n#ifdef _OSD_POSIX\n\tif (strncmp(file,"*POSIX(", sizeof("*POSIX(")-1) == 0) {\n\t\tchar *end;\n\t\tfile += sizeof("*POSIX(")-1;\n\t\tend = &file[strlen(file)-1];\n\t\tif (*end == \')\')\n\t\t\t*end = \'\\0\';\n\t\tif ((end = strrchr(file, \'/\')) != NULL)\n\t\t\tfile = &end[1];\n\t}\n#endif\n\tes=ERR_get_state();\n\tes->top=(es->top+1)%ERR_NUM_ERRORS;\n\tif (es->top == es->bottom)\n\t\tes->bottom=(es->bottom+1)%ERR_NUM_ERRORS;\n\tes->err_buffer[es->top]=ERR_PACK(lib,func,reason);\n\tes->err_file[es->top]=file;\n\tes->err_line[es->top]=line;\n\terr_clear_data(es,es->top);\n\t}']
|
2,697
| 0
|
https://github.com/libav/libav/blob/cf53c48615658a6019ffb7e8453913bbfd38cb27/libavcodec/h264.c/#L357
|
static void await_references(H264Context *h){
MpegEncContext * const s = &h->s;
const int mb_xy= h->mb_xy;
const int mb_type= s->current_picture.mb_type[mb_xy];
int refs[2][48];
int nrefs[2] = {0};
int ref, list;
memset(refs, -1, sizeof(refs));
if(IS_16X16(mb_type)){
get_lowest_part_y(h, refs, 0, 16, 0,
IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
}else if(IS_16X8(mb_type)){
get_lowest_part_y(h, refs, 0, 8, 0,
IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
get_lowest_part_y(h, refs, 8, 8, 8,
IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
}else if(IS_8X16(mb_type)){
get_lowest_part_y(h, refs, 0, 16, 0,
IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
get_lowest_part_y(h, refs, 4, 16, 0,
IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
}else{
int i;
assert(IS_8X8(mb_type));
for(i=0; i<4; i++){
const int sub_mb_type= h->sub_mb_type[i];
const int n= 4*i;
int y_offset= (i&2)<<2;
if(IS_SUB_8X8(sub_mb_type)){
get_lowest_part_y(h, refs, n , 8, y_offset,
IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
}else if(IS_SUB_8X4(sub_mb_type)){
get_lowest_part_y(h, refs, n , 4, y_offset,
IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
get_lowest_part_y(h, refs, n+2, 4, y_offset+4,
IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
}else if(IS_SUB_4X8(sub_mb_type)){
get_lowest_part_y(h, refs, n , 8, y_offset,
IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
get_lowest_part_y(h, refs, n+1, 8, y_offset,
IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
}else{
int j;
assert(IS_SUB_4X4(sub_mb_type));
for(j=0; j<4; j++){
int sub_y_offset= y_offset + 2*(j&2);
get_lowest_part_y(h, refs, n+j, 4, sub_y_offset,
IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
}
}
}
}
for(list=h->list_count-1; list>=0; list--){
for(ref=0; ref<48 && nrefs[list]; ref++){
int row = refs[list][ref];
if(row >= 0){
Picture *ref_pic = &h->ref_list[list][ref];
int ref_field = ref_pic->reference - 1;
int ref_field_picture = ref_pic->field_picture;
int pic_height = 16*s->mb_height >> ref_field_picture;
row <<= MB_MBAFF;
nrefs[list]--;
if(!FIELD_PICTURE && ref_field_picture){
ff_thread_await_progress((AVFrame*)ref_pic, FFMIN((row >> 1) - !(row&1), pic_height-1), 1);
ff_thread_await_progress((AVFrame*)ref_pic, FFMIN((row >> 1) , pic_height-1), 0);
}else if(FIELD_PICTURE && !ref_field_picture){
ff_thread_await_progress((AVFrame*)ref_pic, FFMIN(row*2 + ref_field , pic_height-1), 0);
}else if(FIELD_PICTURE){
ff_thread_await_progress((AVFrame*)ref_pic, FFMIN(row, pic_height-1), ref_field);
}else{
ff_thread_await_progress((AVFrame*)ref_pic, FFMIN(row, pic_height-1), 0);
}
}
}
}
}
|
['static int decode_slice(struct AVCodecContext *avctx, void *arg){\n H264Context *h = *(void**)arg;\n MpegEncContext * const s = &h->s;\n const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;\n int lf_x_start = s->mb_x;\n s->mb_skip_run= -1;\n h->is_complex = FRAME_MBAFF || s->picture_structure != PICT_FRAME || s->codec_id != CODEC_ID_H264 ||\n (CONFIG_GRAY && (s->flags&CODEC_FLAG_GRAY));\n if( h->pps.cabac ) {\n align_get_bits( &s->gb );\n ff_init_cabac_states( &h->cabac);\n ff_init_cabac_decoder( &h->cabac,\n s->gb.buffer + get_bits_count(&s->gb)/8,\n (get_bits_left(&s->gb) + 7)/8);\n ff_h264_init_cabac_states(h);\n for(;;){\n int ret = ff_h264_decode_mb_cabac(h);\n int eos;\n if(ret>=0) ff_h264_hl_decode_mb(h);\n if( ret >= 0 && FRAME_MBAFF ) {\n s->mb_y++;\n ret = ff_h264_decode_mb_cabac(h);\n if(ret>=0) ff_h264_hl_decode_mb(h);\n s->mb_y--;\n }\n eos = get_cabac_terminate( &h->cabac );\n if((s->workaround_bugs & FF_BUG_TRUNCATED) && h->cabac.bytestream > h->cabac.bytestream_end + 2){\n ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);\n if (s->mb_x >= lf_x_start) loop_filter(h, lf_x_start, s->mb_x + 1);\n return 0;\n }\n if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 2) {\n av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d, bytestream (%td)\\n", s->mb_x, s->mb_y, h->cabac.bytestream_end - h->cabac.bytestream);\n ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);\n return -1;\n }\n if( ++s->mb_x >= s->mb_width ) {\n loop_filter(h, lf_x_start, s->mb_x);\n s->mb_x = lf_x_start = 0;\n decode_finish_row(h);\n ++s->mb_y;\n if(FIELD_OR_MBAFF_PICTURE) {\n ++s->mb_y;\n if(FRAME_MBAFF && s->mb_y < s->mb_height)\n predict_field_decoding_flag(h);\n }\n }\n if( eos || s->mb_y >= s->mb_height ) {\n tprintf(s->avctx, "slice end %d %d\\n", get_bits_count(&s->gb), s->gb.size_in_bits);\n ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);\n if (s->mb_x > lf_x_start) loop_filter(h, lf_x_start, s->mb_x);\n return 0;\n }\n }\n } else {\n for(;;){\n int ret = ff_h264_decode_mb_cavlc(h);\n if(ret>=0) ff_h264_hl_decode_mb(h);\n if(ret>=0 && FRAME_MBAFF){\n s->mb_y++;\n ret = ff_h264_decode_mb_cavlc(h);\n if(ret>=0) ff_h264_hl_decode_mb(h);\n s->mb_y--;\n }\n if(ret<0){\n av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\\n", s->mb_x, s->mb_y);\n ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);\n return -1;\n }\n if(++s->mb_x >= s->mb_width){\n loop_filter(h, lf_x_start, s->mb_x);\n s->mb_x = lf_x_start = 0;\n decode_finish_row(h);\n ++s->mb_y;\n if(FIELD_OR_MBAFF_PICTURE) {\n ++s->mb_y;\n if(FRAME_MBAFF && s->mb_y < s->mb_height)\n predict_field_decoding_flag(h);\n }\n if(s->mb_y >= s->mb_height){\n tprintf(s->avctx, "slice end %d %d\\n", get_bits_count(&s->gb), s->gb.size_in_bits);\n if(get_bits_count(&s->gb) == s->gb.size_in_bits ) {\n ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);\n return 0;\n }else{\n ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);\n return -1;\n }\n }\n }\n if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){\n tprintf(s->avctx, "slice end %d %d\\n", get_bits_count(&s->gb), s->gb.size_in_bits);\n if(get_bits_count(&s->gb) == s->gb.size_in_bits ){\n ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);\n if (s->mb_x > lf_x_start) loop_filter(h, lf_x_start, s->mb_x);\n return 0;\n }else{\n ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);\n return -1;\n }\n }\n }\n }\n#if 0\n for(;s->mb_y < s->mb_height; s->mb_y++){\n for(;s->mb_x < s->mb_width; s->mb_x++){\n int ret= decode_mb(h);\n ff_h264_hl_decode_mb(h);\n if(ret<0){\n av_log(s->avctx, AV_LOG_ERROR, "error while decoding MB %d %d\\n", s->mb_x, s->mb_y);\n ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);\n return -1;\n }\n if(++s->mb_x >= s->mb_width){\n s->mb_x=0;\n if(++s->mb_y >= s->mb_height){\n if(get_bits_count(s->gb) == s->gb.size_in_bits){\n ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);\n return 0;\n }else{\n ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);\n return -1;\n }\n }\n }\n if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){\n if(get_bits_count(s->gb) == s->gb.size_in_bits){\n ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);\n return 0;\n }else{\n ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);\n return -1;\n }\n }\n }\n s->mb_x=0;\n ff_draw_horiz_band(s, 16*s->mb_y, 16);\n }\n#endif\n return -1;\n}', '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}', 'void ff_h264_hl_decode_mb(H264Context *h){\n MpegEncContext * const s = &h->s;\n const int mb_xy= h->mb_xy;\n const int mb_type= s->current_picture.mb_type[mb_xy];\n int is_complex = CONFIG_SMALL || h->is_complex || IS_INTRA_PCM(mb_type) || s->qscale == 0;\n if (CHROMA444) {\n if(is_complex || h->pixel_shift)\n hl_decode_mb_444_complex(h);\n else\n hl_decode_mb_444_simple(h);\n } else if (is_complex) {\n hl_decode_mb_complex(h);\n } else if (h->pixel_shift) {\n hl_decode_mb_simple_16(h);\n } else\n hl_decode_mb_simple_8(h);\n}', 'static void av_noinline hl_decode_mb_444_complex(H264Context *h){\n hl_decode_mb_444_internal(h, 0, h->pixel_shift);\n}', 'static av_always_inline void hl_decode_mb_444_internal(H264Context *h, int simple, int pixel_shift){\n MpegEncContext * const s = &h->s;\n const int mb_x= s->mb_x;\n const int mb_y= s->mb_y;\n const int mb_xy= h->mb_xy;\n const int mb_type= s->current_picture.mb_type[mb_xy];\n uint8_t *dest[3];\n int linesize;\n int i, j, p;\n int *block_offset = &h->block_offset[0];\n const int transform_bypass = !simple && (s->qscale == 0 && h->sps.transform_bypass);\n const int plane_count = (simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)) ? 3 : 1;\n for (p = 0; p < plane_count; p++)\n {\n dest[p] = s->current_picture.data[p] + ((mb_x << pixel_shift) + mb_y * s->linesize) * 16;\n s->dsp.prefetch(dest[p] + (s->mb_x&3)*4*s->linesize + (64 << pixel_shift), s->linesize, 4);\n }\n h->list_counts[mb_xy]= h->list_count;\n if (!simple && MB_FIELD) {\n linesize = h->mb_linesize = h->mb_uvlinesize = s->linesize * 2;\n block_offset = &h->block_offset[48];\n if(mb_y&1)\n for (p = 0; p < 3; p++)\n dest[p] -= s->linesize*15;\n if(FRAME_MBAFF) {\n int list;\n for(list=0; list<h->list_count; list++){\n if(!USES_LIST(mb_type, list))\n continue;\n if(IS_16X16(mb_type)){\n int8_t *ref = &h->ref_cache[list][scan8[0]];\n fill_rectangle(ref, 4, 4, 8, (16+*ref)^(s->mb_y&1), 1);\n }else{\n for(i=0; i<16; i+=4){\n int ref = h->ref_cache[list][scan8[i]];\n if(ref >= 0)\n fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, (16+ref)^(s->mb_y&1), 1);\n }\n }\n }\n }\n } else {\n linesize = h->mb_linesize = h->mb_uvlinesize = s->linesize;\n }\n if (!simple && IS_INTRA_PCM(mb_type)) {\n if (pixel_shift) {\n const int bit_depth = h->sps.bit_depth_luma;\n GetBitContext gb;\n init_get_bits(&gb, (uint8_t*)h->mb, 768*bit_depth);\n for (p = 0; p < plane_count; p++) {\n for (i = 0; i < 16; i++) {\n uint16_t *tmp = (uint16_t*)(dest[p] + i*linesize);\n for (j = 0; j < 16; j++)\n tmp[j] = get_bits(&gb, bit_depth);\n }\n }\n } else {\n for (p = 0; p < plane_count; p++) {\n for (i = 0; i < 16; i++) {\n memcpy(dest[p] + i*linesize, h->mb + p*128 + i*8, 16);\n }\n }\n }\n } else {\n if(IS_INTRA(mb_type)){\n if(h->deblocking_filter)\n xchg_mb_border(h, dest[0], dest[1], dest[2], linesize, linesize, 1, 1, simple, pixel_shift);\n for (p = 0; p < plane_count; p++)\n hl_decode_mb_predict_luma(h, mb_type, 1, simple, transform_bypass, pixel_shift, block_offset, linesize, dest[p], p);\n if(h->deblocking_filter)\n xchg_mb_border(h, dest[0], dest[1], dest[2], linesize, linesize, 0, 1, simple, pixel_shift);\n }else{\n if (pixel_shift) {\n hl_motion_16(h, dest[0], dest[1], dest[2],\n s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,\n s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,\n h->h264dsp.weight_h264_pixels_tab,\n h->h264dsp.biweight_h264_pixels_tab, 1);\n } else\n hl_motion_8(h, dest[0], dest[1], dest[2],\n s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,\n s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,\n h->h264dsp.weight_h264_pixels_tab,\n h->h264dsp.biweight_h264_pixels_tab, 1);\n }\n for (p = 0; p < plane_count; p++)\n hl_decode_mb_idct_luma(h, mb_type, 1, simple, transform_bypass, pixel_shift, block_offset, linesize, dest[p], p);\n }\n if(h->cbp || IS_INTRA(mb_type))\n {\n s->dsp.clear_blocks(h->mb);\n s->dsp.clear_blocks(h->mb+(24*16<<pixel_shift));\n }\n}', 'hl_motion_fn(1, 16)', 'static av_always_inline void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,\n qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),\n qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),\n h264_weight_func *weight_op, h264_biweight_func *weight_avg,\n int pixel_shift, int chroma444){\n MpegEncContext * const s = &h->s;\n const int mb_xy= h->mb_xy;\n const int mb_type= s->current_picture.mb_type[mb_xy];\n assert(IS_INTER(mb_type));\n if(HAVE_PTHREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))\n await_references(h);\n prefetch_motion(h, 0, pixel_shift, chroma444);\n if(IS_16X16(mb_type)){\n mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,\n qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],\n weight_op, weight_avg,\n IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1),\n pixel_shift, chroma444);\n }else if(IS_16X8(mb_type)){\n mc_part(h, 0, 0, 4, 8 << pixel_shift, dest_y, dest_cb, dest_cr, 0, 0,\n qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],\n &weight_op[1], &weight_avg[1],\n IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1),\n pixel_shift, chroma444);\n mc_part(h, 8, 0, 4, 8 << pixel_shift, dest_y, dest_cb, dest_cr, 0, 4,\n qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],\n &weight_op[1], &weight_avg[1],\n IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1),\n pixel_shift, chroma444);\n }else if(IS_8X16(mb_type)){\n mc_part(h, 0, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,\n qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],\n &weight_op[2], &weight_avg[2],\n IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1),\n pixel_shift, chroma444);\n mc_part(h, 4, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,\n qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],\n &weight_op[2], &weight_avg[2],\n IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1),\n pixel_shift, chroma444);\n }else{\n int i;\n assert(IS_8X8(mb_type));\n for(i=0; i<4; i++){\n const int sub_mb_type= h->sub_mb_type[i];\n const int n= 4*i;\n int x_offset= (i&1)<<2;\n int y_offset= (i&2)<<1;\n if(IS_SUB_8X8(sub_mb_type)){\n mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,\n qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],\n &weight_op[3], &weight_avg[3],\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),\n pixel_shift, chroma444);\n }else if(IS_SUB_8X4(sub_mb_type)){\n mc_part(h, n , 0, 2, 4 << pixel_shift, dest_y, dest_cb, dest_cr, x_offset, y_offset,\n qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],\n &weight_op[4], &weight_avg[4],\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),\n pixel_shift, chroma444);\n mc_part(h, n+2, 0, 2, 4 << pixel_shift, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,\n qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],\n &weight_op[4], &weight_avg[4],\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),\n pixel_shift, chroma444);\n }else if(IS_SUB_4X8(sub_mb_type)){\n mc_part(h, n , 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,\n qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],\n &weight_op[5], &weight_avg[5],\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),\n pixel_shift, chroma444);\n mc_part(h, n+1, 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,\n qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],\n &weight_op[5], &weight_avg[5],\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),\n pixel_shift, chroma444);\n }else{\n int j;\n assert(IS_SUB_4X4(sub_mb_type));\n for(j=0; j<4; j++){\n int sub_x_offset= x_offset + 2*(j&1);\n int sub_y_offset= y_offset + (j&2);\n mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,\n qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],\n &weight_op[6], &weight_avg[6],\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),\n pixel_shift, chroma444);\n }\n }\n }\n }\n prefetch_motion(h, 1, pixel_shift, chroma444);\n}', 'static void await_references(H264Context *h){\n MpegEncContext * const s = &h->s;\n const int mb_xy= h->mb_xy;\n const int mb_type= s->current_picture.mb_type[mb_xy];\n int refs[2][48];\n int nrefs[2] = {0};\n int ref, list;\n memset(refs, -1, sizeof(refs));\n if(IS_16X16(mb_type)){\n get_lowest_part_y(h, refs, 0, 16, 0,\n IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);\n }else if(IS_16X8(mb_type)){\n get_lowest_part_y(h, refs, 0, 8, 0,\n IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);\n get_lowest_part_y(h, refs, 8, 8, 8,\n IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);\n }else if(IS_8X16(mb_type)){\n get_lowest_part_y(h, refs, 0, 16, 0,\n IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);\n get_lowest_part_y(h, refs, 4, 16, 0,\n IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);\n }else{\n int i;\n assert(IS_8X8(mb_type));\n for(i=0; i<4; i++){\n const int sub_mb_type= h->sub_mb_type[i];\n const int n= 4*i;\n int y_offset= (i&2)<<2;\n if(IS_SUB_8X8(sub_mb_type)){\n get_lowest_part_y(h, refs, n , 8, y_offset,\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);\n }else if(IS_SUB_8X4(sub_mb_type)){\n get_lowest_part_y(h, refs, n , 4, y_offset,\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);\n get_lowest_part_y(h, refs, n+2, 4, y_offset+4,\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);\n }else if(IS_SUB_4X8(sub_mb_type)){\n get_lowest_part_y(h, refs, n , 8, y_offset,\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);\n get_lowest_part_y(h, refs, n+1, 8, y_offset,\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);\n }else{\n int j;\n assert(IS_SUB_4X4(sub_mb_type));\n for(j=0; j<4; j++){\n int sub_y_offset= y_offset + 2*(j&2);\n get_lowest_part_y(h, refs, n+j, 4, sub_y_offset,\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);\n }\n }\n }\n }\n for(list=h->list_count-1; list>=0; list--){\n for(ref=0; ref<48 && nrefs[list]; ref++){\n int row = refs[list][ref];\n if(row >= 0){\n Picture *ref_pic = &h->ref_list[list][ref];\n int ref_field = ref_pic->reference - 1;\n int ref_field_picture = ref_pic->field_picture;\n int pic_height = 16*s->mb_height >> ref_field_picture;\n row <<= MB_MBAFF;\n nrefs[list]--;\n if(!FIELD_PICTURE && ref_field_picture){\n ff_thread_await_progress((AVFrame*)ref_pic, FFMIN((row >> 1) - !(row&1), pic_height-1), 1);\n ff_thread_await_progress((AVFrame*)ref_pic, FFMIN((row >> 1) , pic_height-1), 0);\n }else if(FIELD_PICTURE && !ref_field_picture){\n ff_thread_await_progress((AVFrame*)ref_pic, FFMIN(row*2 + ref_field , pic_height-1), 0);\n }else if(FIELD_PICTURE){\n ff_thread_await_progress((AVFrame*)ref_pic, FFMIN(row, pic_height-1), ref_field);\n }else{\n ff_thread_await_progress((AVFrame*)ref_pic, FFMIN(row, pic_height-1), 0);\n }\n }\n }\n }\n}']
|
2,698
| 0
|
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/bn/bn_mont.c/#L208
|
static int BN_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont)
{
BIGNUM *n;
BN_ULONG *ap, *np, *rp, n0, v, carry;
int nl, max, i;
n = &(mont->N);
nl = n->top;
if (nl == 0) {
ret->top = 0;
return (1);
}
max = (2 * nl);
if (bn_wexpand(r, max) == NULL)
return (0);
r->neg ^= n->neg;
np = n->d;
rp = r->d;
i = max - r->top;
if (i)
memset(&rp[r->top], 0, sizeof(*rp) * i);
r->top = max;
n0 = mont->n0[0];
for (carry = 0, i = 0; i < nl; i++, rp++) {
v = bn_mul_add_words(rp, np, nl, (rp[0] * n0) & BN_MASK2);
v = (v + carry + rp[nl]) & BN_MASK2;
carry |= (v != rp[nl]);
carry &= (v <= rp[nl]);
rp[nl] = v;
}
if (bn_wexpand(ret, nl) == NULL)
return (0);
ret->top = nl;
ret->neg = r->neg;
rp = ret->d;
ap = &(r->d[nl]);
# define BRANCH_FREE 1
# if BRANCH_FREE
{
BN_ULONG *nrp;
size_t m;
v = bn_sub_words(rp, ap, np, nl) - carry;
m = (0 - (size_t)v);
nrp =
(BN_ULONG *)(((PTR_SIZE_INT) rp & ~m) | ((PTR_SIZE_INT) ap & m));
for (i = 0, nl -= 4; i < nl; i += 4) {
BN_ULONG t1, t2, t3, t4;
t1 = nrp[i + 0];
t2 = nrp[i + 1];
t3 = nrp[i + 2];
ap[i + 0] = 0;
t4 = nrp[i + 3];
ap[i + 1] = 0;
rp[i + 0] = t1;
ap[i + 2] = 0;
rp[i + 1] = t2;
ap[i + 3] = 0;
rp[i + 2] = t3;
rp[i + 3] = t4;
}
for (nl += 4; i < nl; i++)
rp[i] = nrp[i], ap[i] = 0;
}
# else
if (bn_sub_words(rp, ap, np, nl) - carry)
memcpy(rp, ap, nl * sizeof(BN_ULONG));
# endif
bn_correct_top(r);
bn_correct_top(ret);
bn_check_top(ret);
return (1);
}
|
['int ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p,\n const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n BN_CTX *new_ctx = NULL;\n BN_MONT_CTX *mont = NULL;\n BIGNUM *one = NULL;\n int ret = 0;\n BN_MONT_CTX_free(group->field_data1);\n group->field_data1 = NULL;\n BN_free(group->field_data2);\n group->field_data2 = NULL;\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n mont = BN_MONT_CTX_new();\n if (mont == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, p, ctx)) {\n ECerr(EC_F_EC_GFP_MONT_GROUP_SET_CURVE, ERR_R_BN_LIB);\n goto err;\n }\n one = BN_new();\n if (one == NULL)\n goto err;\n if (!BN_to_montgomery(one, BN_value_one(), mont, ctx))\n goto err;\n group->field_data1 = mont;\n mont = NULL;\n group->field_data2 = one;\n one = NULL;\n ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);\n if (!ret) {\n BN_MONT_CTX_free(group->field_data1);\n group->field_data1 = NULL;\n BN_free(group->field_data2);\n group->field_data2 = NULL;\n }\n err:\n BN_CTX_free(new_ctx);\n BN_MONT_CTX_free(mont);\n return ret;\n}', 'int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)\n{\n int ret = 0;\n BIGNUM *Ri, *R;\n if (BN_is_zero(mod))\n return 0;\n BN_CTX_start(ctx);\n if ((Ri = BN_CTX_get(ctx)) == NULL)\n goto err;\n R = &(mont->RR);\n if (!BN_copy(&(mont->N), mod))\n goto err;\n mont->N.neg = 0;\n#ifdef MONT_WORD\n {\n BIGNUM tmod;\n BN_ULONG buf[2];\n bn_init(&tmod);\n tmod.d = buf;\n tmod.dmax = 2;\n tmod.neg = 0;\n mont->ri = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2;\n# if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)\n BN_zero(R);\n if (!(BN_set_bit(R, 2 * BN_BITS2)))\n goto err;\n tmod.top = 0;\n if ((buf[0] = mod->d[0]))\n tmod.top = 1;\n if ((buf[1] = mod->top > 1 ? mod->d[1] : 0))\n tmod.top = 2;\n if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, 2 * BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (bn_expand(Ri, (int)sizeof(BN_ULONG) * 2) == NULL)\n goto err;\n Ri->neg = 0;\n Ri->d[0] = BN_MASK2;\n Ri->d[1] = BN_MASK2;\n Ri->top = 2;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;\n# else\n BN_zero(R);\n if (!(BN_set_bit(R, BN_BITS2)))\n goto err;\n buf[0] = mod->d[0];\n buf[1] = 0;\n tmod.top = buf[0] != 0 ? 1 : 0;\n if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (!BN_set_word(Ri, BN_MASK2))\n goto err;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = 0;\n# endif\n }\n#else\n {\n mont->ri = BN_num_bits(&mont->N);\n BN_zero(R);\n if (!BN_set_bit(R, mont->ri))\n goto err;\n if ((BN_mod_inverse(Ri, R, &mont->N, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, mont->ri))\n goto err;\n if (!BN_sub_word(Ri, 1))\n goto err;\n if (!BN_div(&(mont->Ni), NULL, Ri, &mont->N, ctx))\n goto err;\n }\n#endif\n BN_zero(&(mont->RR));\n if (!BN_set_bit(&(mont->RR), mont->ri * 2))\n goto err;\n if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx))\n goto err;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,\n BN_CTX *ctx)\n{\n return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);\n}', 'int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n BN_MONT_CTX *mont, BN_CTX *ctx)\n{\n BIGNUM *tmp;\n int ret = 0;\n#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\n int num = mont->N.top;\n if (num > 1 && a->top == num && b->top == num) {\n if (bn_wexpand(r, num) == NULL)\n return (0);\n if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {\n r->neg = a->neg ^ b->neg;\n r->top = num;\n bn_correct_top(r);\n return (1);\n }\n }\n#endif\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n if (tmp == NULL)\n goto err;\n bn_check_top(tmp);\n if (a == b) {\n if (!BN_sqr(tmp, a, ctx))\n goto err;\n } else {\n if (!BN_mul(tmp, a, b, ctx))\n goto err;\n }\n#ifdef MONT_WORD\n if (!BN_from_montgomery_word(r, tmp, mont))\n goto err;\n#else\n if (!BN_from_montgomery(r, tmp, mont, ctx))\n goto err;\n#endif\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return (ret);\n}', 'static int BN_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont)\n{\n BIGNUM *n;\n BN_ULONG *ap, *np, *rp, n0, v, carry;\n int nl, max, i;\n n = &(mont->N);\n nl = n->top;\n if (nl == 0) {\n ret->top = 0;\n return (1);\n }\n max = (2 * nl);\n if (bn_wexpand(r, max) == NULL)\n return (0);\n r->neg ^= n->neg;\n np = n->d;\n rp = r->d;\n i = max - r->top;\n if (i)\n memset(&rp[r->top], 0, sizeof(*rp) * i);\n r->top = max;\n n0 = mont->n0[0];\n for (carry = 0, i = 0; i < nl; i++, rp++) {\n v = bn_mul_add_words(rp, np, nl, (rp[0] * n0) & BN_MASK2);\n v = (v + carry + rp[nl]) & BN_MASK2;\n carry |= (v != rp[nl]);\n carry &= (v <= rp[nl]);\n rp[nl] = v;\n }\n if (bn_wexpand(ret, nl) == NULL)\n return (0);\n ret->top = nl;\n ret->neg = r->neg;\n rp = ret->d;\n ap = &(r->d[nl]);\n# define BRANCH_FREE 1\n# if BRANCH_FREE\n {\n BN_ULONG *nrp;\n size_t m;\n v = bn_sub_words(rp, ap, np, nl) - carry;\n m = (0 - (size_t)v);\n nrp =\n (BN_ULONG *)(((PTR_SIZE_INT) rp & ~m) | ((PTR_SIZE_INT) ap & m));\n for (i = 0, nl -= 4; i < nl; i += 4) {\n BN_ULONG t1, t2, t3, t4;\n t1 = nrp[i + 0];\n t2 = nrp[i + 1];\n t3 = nrp[i + 2];\n ap[i + 0] = 0;\n t4 = nrp[i + 3];\n ap[i + 1] = 0;\n rp[i + 0] = t1;\n ap[i + 2] = 0;\n rp[i + 1] = t2;\n ap[i + 3] = 0;\n rp[i + 2] = t3;\n rp[i + 3] = t4;\n }\n for (nl += 4; i < nl; i++)\n rp[i] = nrp[i], ap[i] = 0;\n }\n# else\n if (bn_sub_words(rp, ap, np, nl) - carry)\n memcpy(rp, ap, nl * sizeof(BN_ULONG));\n# endif\n bn_correct_top(r);\n bn_correct_top(ret);\n bn_check_top(ret);\n return (1);\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']
|
2,699
| 0
|
https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/mpegaudiodec.c/#L2525
|
static int decode_init_mp3on4(AVCodecContext * avctx)
{
MP3On4DecodeContext *s = avctx->priv_data;
int i;
if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
return -1;
}
s->chan_cfg = (((unsigned char *)avctx->extradata)[1] >> 3) & 0x0f;
s->frames = mp3Frames[s->chan_cfg];
if(!s->frames) {
av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
return -1;
}
avctx->channels = mp3Channels[s->chan_cfg];
s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
avctx->priv_data = s->mp3decctx[0];
decode_init(avctx);
avctx->priv_data = s;
s->mp3decctx[0]->adu_mode = 1;
for (i = 1; i < s->frames; i++) {
s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
s->mp3decctx[i]->compute_antialias = s->mp3decctx[0]->compute_antialias;
s->mp3decctx[i]->adu_mode = 1;
s->mp3decctx[i]->avctx = avctx;
}
return 0;
}
|
['static int decode_init_mp3on4(AVCodecContext * avctx)\n{\n MP3On4DecodeContext *s = avctx->priv_data;\n int i;\n if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {\n av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\\n");\n return -1;\n }\n s->chan_cfg = (((unsigned char *)avctx->extradata)[1] >> 3) & 0x0f;\n s->frames = mp3Frames[s->chan_cfg];\n if(!s->frames) {\n av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\\n");\n return -1;\n }\n avctx->channels = mp3Channels[s->chan_cfg];\n s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));\n avctx->priv_data = s->mp3decctx[0];\n decode_init(avctx);\n avctx->priv_data = s;\n s->mp3decctx[0]->adu_mode = 1;\n for (i = 1; i < s->frames; i++) {\n s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));\n s->mp3decctx[i]->compute_antialias = s->mp3decctx[0]->compute_antialias;\n s->mp3decctx[i]->adu_mode = 1;\n s->mp3decctx[i]->avctx = avctx;\n }\n return 0;\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}']
|
2,700
| 0
|
https://github.com/openssl/openssl/blob/43a0449fe6ce18b750803be8a115a412a7235496/crypto/bn/bn_shift.c/#L112
|
int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)
{
int i, nw, lb, rb;
BN_ULONG *t, *f;
BN_ULONG l;
bn_check_top(r);
bn_check_top(a);
if (n < 0) {
BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);
return 0;
}
nw = n / BN_BITS2;
if (bn_wexpand(r, a->top + nw + 1) == NULL)
return (0);
r->neg = a->neg;
lb = n % BN_BITS2;
rb = BN_BITS2 - lb;
f = a->d;
t = r->d;
t[a->top + nw] = 0;
if (lb == 0)
for (i = a->top - 1; i >= 0; i--)
t[nw + i] = f[i];
else
for (i = a->top - 1; i >= 0; i--) {
l = f[i];
t[nw + i + 1] |= (l >> rb) & BN_MASK2;
t[nw + i] = (l << lb) & BN_MASK2;
}
memset(t, 0, sizeof(*t) * nw);
r->top = a->top + nw + 1;
bn_correct_top(r);
bn_check_top(r);
return (1);
}
|
['int bn_probable_prime_dh(BIGNUM *rnd, int bits,\n const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx)\n{\n int i, ret = 0;\n BIGNUM *t1;\n BN_CTX_start(ctx);\n if ((t1 = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (!BN_rand(rnd, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))\n goto err;\n if (!BN_mod(t1, rnd, add, ctx))\n goto err;\n if (!BN_sub(rnd, rnd, t1))\n goto err;\n if (rem == NULL) {\n if (!BN_add_word(rnd, 1))\n goto err;\n } else {\n if (!BN_add(rnd, rnd, rem))\n goto err;\n }\n loop:\n for (i = 1; i < NUMPRIMES; i++) {\n BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);\n if (mod == (BN_ULONG)-1)\n goto err;\n if (mod <= 1) {\n if (!BN_add(rnd, rnd, add))\n goto err;\n goto loop;\n }\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n bn_check_top(rnd);\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 testing, 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) {\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 time(&tim);\n RAND_add(&tim, sizeof(tim), 0.0);\n if (RAND_bytes(buf, bytes) <= 0)\n goto err;\n if (testing) {\n int i;\n unsigned char c;\n for (i = 0; i < bytes; i++) {\n if (RAND_bytes(&c, 1) <= 0)\n goto err;\n if (c >= 128 && i > 0)\n buf[i] = buf[i - 1];\n else if (c < 42)\n buf[i] = 0;\n else if (c < 84)\n buf[i] = 255;\n }\n }\n if (top >= 0) {\n if (top) {\n if (bit == 0) {\n buf[0] = 1;\n buf[1] |= 0x80;\n } else {\n buf[0] |= (3 << (bit - 1));\n }\n } else {\n buf[0] |= (1 << bit);\n }\n }\n buf[0] &= ~mask;\n if (bottom)\n buf[bytes - 1] |= 1;\n if (!BN_bin2bn(buf, bytes, rnd))\n goto err;\n ret = 1;\n err:\n OPENSSL_clear_free(buf, bytes);\n bn_check_top(rnd);\n return (ret);\ntoosmall:\n BNerr(BN_F_BNRAND, BN_R_BITS_TOO_SMALL);\n return 0;\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int 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}']
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.