id
int64 1
36.7k
| label
int64 0
1
| bug_url
stringlengths 91
134
| bug_function
stringlengths 13
72.7k
| functions
stringlengths 17
79.2k
|
---|---|---|---|---|
1,001 | 0 |
https://github.com/openssl/openssl/blob/3b0ee0d2bf076649fa1d2d42281678ec1008a86f/crypto/modes/cfb128.c/#L199
|
static void cfbr_encrypt_block(const unsigned char *in,unsigned char *out,
int nbits,const void *key,
unsigned char ivec[16],int enc,
block128_f block)
{
int n,rem,num;
unsigned char ovec[16*2 + 1];
if (nbits<=0 || nbits>128) return;
memcpy(ovec,ivec,16);
(*block)(ivec,ivec,key);
num = (nbits+7)/8;
if (enc)
for(n=0 ; n < num ; ++n)
out[n] = (ovec[16+n] = in[n] ^ ivec[n]);
else
for(n=0 ; n < num ; ++n)
out[n] = (ovec[16+n] = in[n]) ^ ivec[n];
rem = nbits%8;
num = nbits/8;
if(rem==0)
memcpy(ivec,ovec+num,16);
else
for(n=0 ; n < 16 ; ++n)
ivec[n] = ovec[n+num]<<rem | ovec[n+num+1]>>(8-rem);
}
|
['void CRYPTO_cfb128_1_encrypt(const unsigned char *in, unsigned char *out,\n\t\t \tsize_t bits, const void *key,\n\t\t\tunsigned char ivec[16], int *num,\n\t\t\tint enc, block128_f block)\n{\n size_t n;\n unsigned char c[1],d[1];\n assert(in && out && key && ivec && num);\n assert(*num == 0);\n memset(out,0,(bits+7)/8);\n for(n=0 ; n<bits ; ++n)\n\t{\n\tc[0]=(in[n/8]&(1 << (7-n%8))) ? 0x80 : 0;\n\tcfbr_encrypt_block(c,d,1,key,ivec,enc,block);\n\tout[n/8]=(out[n/8]&~(1 << (7-n%8)))|((d[0]&0x80) >> (n%8));\n\t}\n}', 'static void cfbr_encrypt_block(const unsigned char *in,unsigned char *out,\n\t\t\t int nbits,const void *key,\n\t\t\t unsigned char ivec[16],int enc,\n\t\t\t block128_f block)\n{\n int n,rem,num;\n unsigned char ovec[16*2 + 1];\n if (nbits<=0 || nbits>128) return;\n\tmemcpy(ovec,ivec,16);\n\t(*block)(ivec,ivec,key);\n\tnum = (nbits+7)/8;\n\tif (enc)\n\t for(n=0 ; n < num ; ++n)\n\t\tout[n] = (ovec[16+n] = in[n] ^ ivec[n]);\n\telse\n\t for(n=0 ; n < num ; ++n)\n\t\tout[n] = (ovec[16+n] = in[n]) ^ ivec[n];\n\trem = nbits%8;\n\tnum = nbits/8;\n\tif(rem==0)\n\t memcpy(ivec,ovec+num,16);\n\telse\n\t for(n=0 ; n < 16 ; ++n)\n\t\tivec[n] = ovec[n+num]<<rem | ovec[n+num+1]>>(8-rem);\n}']
|
1,002 | 0 |
https://github.com/libav/libav/blob/a4be782cbde337ced47b04e2e6f79ba99e79ae1d/libswscale/swscale.c/#L2975
|
int sws_scale(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
int srcSliceH, uint8_t* dst[], int dstStride[])
{
int i;
const uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
if (srcSliceH == 0)
return 0;
if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
return 0;
}
if (c->sliceDir == 0) {
if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
}
if (usePal(c->srcFormat)) {
for (i=0; i<256; i++) {
int p, r, g, b,y,u,v;
if(c->srcFormat == PIX_FMT_PAL8) {
p=((uint32_t*)(src[1]))[i];
r= (p>>16)&0xFF;
g= (p>> 8)&0xFF;
b= p &0xFF;
} else if(c->srcFormat == PIX_FMT_RGB8) {
r= (i>>5 )*36;
g= ((i>>2)&7)*36;
b= (i&3 )*85;
} else if(c->srcFormat == PIX_FMT_BGR8) {
b= (i>>6 )*85;
g= ((i>>3)&7)*36;
r= (i&7 )*36;
} else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
r= (i>>3 )*255;
g= ((i>>1)&3)*85;
b= (i&1 )*255;
} else {
assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
b= (i>>3 )*255;
g= ((i>>1)&3)*85;
r= (i&1 )*255;
}
y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
c->pal_yuv[i]= y + (u<<8) + (v<<16);
switch(c->dstFormat) {
case PIX_FMT_BGR32:
#if !HAVE_BIGENDIAN
case PIX_FMT_RGB24:
#endif
c->pal_rgb[i]= r + (g<<8) + (b<<16);
break;
case PIX_FMT_BGR32_1:
#if HAVE_BIGENDIAN
case PIX_FMT_BGR24:
#endif
c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
break;
case PIX_FMT_RGB32_1:
#if HAVE_BIGENDIAN
case PIX_FMT_RGB24:
#endif
c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
break;
case PIX_FMT_RGB32:
#if !HAVE_BIGENDIAN
case PIX_FMT_BGR24:
#endif
default:
c->pal_rgb[i]= b + (g<<8) + (r<<16);
}
}
}
if (c->sliceDir == 1) {
int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
reset_ptr(src2, c->srcFormat);
reset_ptr((const uint8_t**)dst2, c->dstFormat);
if (srcSliceY + srcSliceH == c->srcH)
c->sliceDir = 0;
return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
} else {
int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
src2[0] += (srcSliceH-1)*srcStride[0];
if (!usePal(c->srcFormat))
src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
src2[3] += (srcSliceH-1)*srcStride[3];
dst2[0] += ( c->dstH -1)*dstStride[0];
dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
dst2[3] += ( c->dstH -1)*dstStride[3];
reset_ptr(src2, c->srcFormat);
reset_ptr((const uint8_t**)dst2, c->dstFormat);
if (!srcSliceY)
c->sliceDir = 0;
return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
}
}
|
['static void do_video_out(AVFormatContext *s,\n AVOutputStream *ost,\n AVInputStream *ist,\n AVFrame *in_picture,\n int *frame_size)\n{\n int nb_frames, i, ret;\n int64_t topBand, bottomBand, leftBand, rightBand;\n AVFrame *final_picture, *formatted_picture, *resampling_dst, *padding_src;\n AVFrame picture_crop_temp, picture_pad_temp;\n AVCodecContext *enc, *dec;\n avcodec_get_frame_defaults(&picture_crop_temp);\n avcodec_get_frame_defaults(&picture_pad_temp);\n enc = ost->st->codec;\n dec = ist->st->codec;\n nb_frames = 1;\n *frame_size = 0;\n if(video_sync_method){\n double vdelta;\n vdelta = get_sync_ipts(ost) / av_q2d(enc->time_base) - ost->sync_opts;\n if (vdelta < -1.1)\n nb_frames = 0;\n else if (video_sync_method == 2 || (video_sync_method<0 && (s->oformat->flags & AVFMT_VARIABLE_FPS))){\n if(vdelta<=-0.6){\n nb_frames=0;\n }else if(vdelta>0.6)\n ost->sync_opts= lrintf(get_sync_ipts(ost) / av_q2d(enc->time_base));\n }else if (vdelta > 1.1)\n nb_frames = lrintf(vdelta);\n if (nb_frames == 0){\n ++nb_frames_drop;\n if (verbose>2)\n fprintf(stderr, "*** drop!\\n");\n }else if (nb_frames > 1) {\n nb_frames_dup += nb_frames - 1;\n if (verbose>2)\n fprintf(stderr, "*** %d dup!\\n", nb_frames-1);\n }\n }else\n ost->sync_opts= lrintf(get_sync_ipts(ost) / av_q2d(enc->time_base));\n nb_frames= FFMIN(nb_frames, max_frames[CODEC_TYPE_VIDEO] - ost->frame_number);\n if (nb_frames <= 0)\n return;\n if (ost->video_crop) {\n if (av_picture_crop((AVPicture *)&picture_crop_temp, (AVPicture *)in_picture, dec->pix_fmt, ost->topBand, ost->leftBand) < 0) {\n fprintf(stderr, "error cropping picture\\n");\n if (exit_on_error)\n av_exit(1);\n return;\n }\n formatted_picture = &picture_crop_temp;\n } else {\n formatted_picture = in_picture;\n }\n final_picture = formatted_picture;\n padding_src = formatted_picture;\n resampling_dst = &ost->pict_tmp;\n if (ost->video_pad) {\n final_picture = &ost->pict_tmp;\n if (ost->video_resample) {\n if (av_picture_crop((AVPicture *)&picture_pad_temp, (AVPicture *)final_picture, enc->pix_fmt, ost->padtop, ost->padleft) < 0) {\n fprintf(stderr, "error padding picture\\n");\n if (exit_on_error)\n av_exit(1);\n return;\n }\n resampling_dst = &picture_pad_temp;\n }\n }\n if( (ost->resample_height != (ist->st->codec->height - (ost->topBand + ost->bottomBand)))\n || (ost->resample_width != (ist->st->codec->width - (ost->leftBand + ost->rightBand)))\n || (ost->resample_pix_fmt!= ist->st->codec->pix_fmt) ) {\n fprintf(stderr,"Input Stream #%d.%d frame size changed to %dx%d, %s\\n", ist->file_index, ist->index, ist->st->codec->width, ist->st->codec->height,avcodec_get_pix_fmt_name(ist->st->codec->pix_fmt));\n if(!ost->video_resample)\n av_exit(1);\n }\n if (ost->video_resample) {\n padding_src = NULL;\n final_picture = &ost->pict_tmp;\n if( (ost->resample_height != (ist->st->codec->height - (ost->topBand + ost->bottomBand)))\n || (ost->resample_width != (ist->st->codec->width - (ost->leftBand + ost->rightBand)))\n || (ost->resample_pix_fmt!= ist->st->codec->pix_fmt) ) {\n topBand = ((int64_t)ist->st->codec->height * ost->original_topBand / ost->original_height) & ~1;\n bottomBand = ((int64_t)ist->st->codec->height * ost->original_bottomBand / ost->original_height) & ~1;\n leftBand = ((int64_t)ist->st->codec->width * ost->original_leftBand / ost->original_width) & ~1;\n rightBand = ((int64_t)ist->st->codec->width * ost->original_rightBand / ost->original_width) & ~1;\n assert(topBand <= INT_MAX && topBand >= 0);\n assert(bottomBand <= INT_MAX && bottomBand >= 0);\n assert(leftBand <= INT_MAX && leftBand >= 0);\n assert(rightBand <= INT_MAX && rightBand >= 0);\n ost->topBand = topBand;\n ost->bottomBand = bottomBand;\n ost->leftBand = leftBand;\n ost->rightBand = rightBand;\n ost->resample_height = ist->st->codec->height - (ost->topBand + ost->bottomBand);\n ost->resample_width = ist->st->codec->width - (ost->leftBand + ost->rightBand);\n ost->resample_pix_fmt= ist->st->codec->pix_fmt;\n sws_freeContext(ost->img_resample_ctx);\n sws_flags = av_get_int(sws_opts, "sws_flags", NULL);\n ost->img_resample_ctx = sws_getContext(\n ist->st->codec->width - (ost->leftBand + ost->rightBand),\n ist->st->codec->height - (ost->topBand + ost->bottomBand),\n ist->st->codec->pix_fmt,\n ost->st->codec->width - (ost->padleft + ost->padright),\n ost->st->codec->height - (ost->padtop + ost->padbottom),\n ost->st->codec->pix_fmt,\n sws_flags, NULL, NULL, NULL);\n if (ost->img_resample_ctx == NULL) {\n fprintf(stderr, "Cannot get resampling context\\n");\n av_exit(1);\n }\n }\n sws_scale(ost->img_resample_ctx, formatted_picture->data, formatted_picture->linesize,\n 0, ost->resample_height, resampling_dst->data, resampling_dst->linesize);\n }\n if (ost->video_pad) {\n av_picture_pad((AVPicture*)final_picture, (AVPicture *)padding_src,\n enc->height, enc->width, enc->pix_fmt,\n ost->padtop, ost->padbottom, ost->padleft, ost->padright, padcolor);\n }\n for(i=0;i<nb_frames;i++) {\n AVPacket pkt;\n av_init_packet(&pkt);\n pkt.stream_index= ost->index;\n if (s->oformat->flags & AVFMT_RAWPICTURE) {\n AVFrame* old_frame = enc->coded_frame;\n enc->coded_frame = dec->coded_frame;\n pkt.data= (uint8_t *)final_picture;\n pkt.size= sizeof(AVPicture);\n pkt.pts= av_rescale_q(ost->sync_opts, enc->time_base, ost->st->time_base);\n pkt.flags |= PKT_FLAG_KEY;\n write_frame(s, &pkt, ost->st->codec, bitstream_filters[ost->file_index][pkt.stream_index]);\n enc->coded_frame = old_frame;\n } else {\n AVFrame big_picture;\n big_picture= *final_picture;\n big_picture.interlaced_frame = in_picture->interlaced_frame;\n if(avcodec_opts[CODEC_TYPE_VIDEO]->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME)){\n if(top_field_first == -1)\n big_picture.top_field_first = in_picture->top_field_first;\n else\n big_picture.top_field_first = top_field_first;\n }\n if (same_quality) {\n big_picture.quality = ist->st->quality;\n }else\n big_picture.quality = ost->st->quality;\n if(!me_threshold)\n big_picture.pict_type = 0;\n big_picture.pts= ost->sync_opts;\n ret = avcodec_encode_video(enc,\n bit_buffer, bit_buffer_size,\n &big_picture);\n if (ret < 0) {\n fprintf(stderr, "Video encoding failed\\n");\n av_exit(1);\n }\n if(ret>0){\n pkt.data= bit_buffer;\n pkt.size= ret;\n if(enc->coded_frame->pts != AV_NOPTS_VALUE)\n pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);\n if(enc->coded_frame->key_frame)\n pkt.flags |= PKT_FLAG_KEY;\n write_frame(s, &pkt, ost->st->codec, bitstream_filters[ost->file_index][pkt.stream_index]);\n *frame_size = ret;\n video_size += ret;\n if (ost->logfile && enc->stats_out) {\n fprintf(ost->logfile, "%s", enc->stats_out);\n }\n }\n }\n ost->sync_opts++;\n ost->frame_number++;\n }\n}', 'int sws_scale(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,\n int srcSliceH, uint8_t* dst[], int dstStride[])\n{\n int i;\n const uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};\n uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};\n if (srcSliceH == 0)\n return 0;\n if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {\n av_log(c, AV_LOG_ERROR, "Slices start in the middle!\\n");\n return 0;\n }\n if (c->sliceDir == 0) {\n if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;\n }\n if (usePal(c->srcFormat)) {\n for (i=0; i<256; i++) {\n int p, r, g, b,y,u,v;\n if(c->srcFormat == PIX_FMT_PAL8) {\n p=((uint32_t*)(src[1]))[i];\n r= (p>>16)&0xFF;\n g= (p>> 8)&0xFF;\n b= p &0xFF;\n } else if(c->srcFormat == PIX_FMT_RGB8) {\n r= (i>>5 )*36;\n g= ((i>>2)&7)*36;\n b= (i&3 )*85;\n } else if(c->srcFormat == PIX_FMT_BGR8) {\n b= (i>>6 )*85;\n g= ((i>>3)&7)*36;\n r= (i&7 )*36;\n } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {\n r= (i>>3 )*255;\n g= ((i>>1)&3)*85;\n b= (i&1 )*255;\n } else {\n assert(c->srcFormat == PIX_FMT_BGR4_BYTE);\n b= (i>>3 )*255;\n g= ((i>>1)&3)*85;\n r= (i&1 )*255;\n }\n y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);\n u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);\n v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);\n c->pal_yuv[i]= y + (u<<8) + (v<<16);\n switch(c->dstFormat) {\n case PIX_FMT_BGR32:\n#if !HAVE_BIGENDIAN\n case PIX_FMT_RGB24:\n#endif\n c->pal_rgb[i]= r + (g<<8) + (b<<16);\n break;\n case PIX_FMT_BGR32_1:\n#if HAVE_BIGENDIAN\n case PIX_FMT_BGR24:\n#endif\n c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;\n break;\n case PIX_FMT_RGB32_1:\n#if HAVE_BIGENDIAN\n case PIX_FMT_RGB24:\n#endif\n c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;\n break;\n case PIX_FMT_RGB32:\n#if !HAVE_BIGENDIAN\n case PIX_FMT_BGR24:\n#endif\n default:\n c->pal_rgb[i]= b + (g<<8) + (r<<16);\n }\n }\n }\n if (c->sliceDir == 1) {\n int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};\n int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};\n reset_ptr(src2, c->srcFormat);\n reset_ptr((const uint8_t**)dst2, c->dstFormat);\n if (srcSliceY + srcSliceH == c->srcH)\n c->sliceDir = 0;\n return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);\n } else {\n int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};\n int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};\n src2[0] += (srcSliceH-1)*srcStride[0];\n if (!usePal(c->srcFormat))\n src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];\n src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];\n src2[3] += (srcSliceH-1)*srcStride[3];\n dst2[0] += ( c->dstH -1)*dstStride[0];\n dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];\n dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];\n dst2[3] += ( c->dstH -1)*dstStride[3];\n reset_ptr(src2, c->srcFormat);\n reset_ptr((const uint8_t**)dst2, c->dstFormat);\n if (!srcSliceY)\n c->sliceDir = 0;\n return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);\n }\n}']
|
1,003 | 0 |
https://github.com/openssl/openssl/blob/305b68f1a2b6d4d0aa07a6ab47ac372f067a40bb/crypto/bn/bn_lib.c/#L231
|
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
BN_ULONG *a = NULL;
if (words > (INT_MAX / (4 * BN_BITS2))) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
return NULL;
}
if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
return NULL;
}
if (BN_get_flags(b, BN_FLG_SECURE))
a = OPENSSL_secure_zalloc(words * sizeof(*a));
else
a = OPENSSL_zalloc(words * sizeof(*a));
if (a == NULL) {
BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
return NULL;
}
assert(b->top <= words);
if (b->top > 0)
memcpy(a, b->d, sizeof(*a) * b->top);
return a;
}
|
['int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = 0;\n int top, al, bl;\n BIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n int i;\n#endif\n#ifdef BN_RECURSION\n BIGNUM *t = NULL;\n int j = 0, k;\n#endif\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(r);\n al = a->top;\n bl = b->top;\n if ((al == 0) || (bl == 0)) {\n BN_zero(r);\n return 1;\n }\n top = al + bl;\n BN_CTX_start(ctx);\n if ((r == a) || (r == b)) {\n if ((rr = BN_CTX_get(ctx)) == NULL)\n goto err;\n } else\n rr = r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n i = al - bl;\n#endif\n#ifdef BN_MUL_COMBA\n if (i == 0) {\n# if 0\n if (al == 4) {\n if (bn_wexpand(rr, 8) == NULL)\n goto err;\n rr->top = 8;\n bn_mul_comba4(rr->d, a->d, b->d);\n goto end;\n }\n# endif\n if (al == 8) {\n if (bn_wexpand(rr, 16) == NULL)\n goto err;\n rr->top = 16;\n bn_mul_comba8(rr->d, a->d, b->d);\n goto end;\n }\n }\n#endif\n#ifdef BN_RECURSION\n if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {\n if (i >= -1 && i <= 1) {\n if (i >= 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n }\n if (i == -1) {\n j = BN_num_bits_word((BN_ULONG)bl);\n }\n j = 1 << (j - 1);\n assert(j <= al || j <= bl);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (al > j || bl > j) {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d,\n j, al - j, bl - j, t->d);\n } else {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);\n }\n rr->top = top;\n goto end;\n }\n }\n#endif\n if (bn_wexpand(rr, top) == NULL)\n goto err;\n rr->top = top;\n bn_mul_normal(rr->d, a->d, al, b->d, bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n end:\n#endif\n rr->neg = a->neg ^ b->neg;\n bn_correct_top(rr);\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(r);\n BN_CTX_end(ctx);\n return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n a->flags &= ~BN_FLG_FIXED_TOP;\n bn_check_top(a);\n return 1;\n}', '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}']
|
1,004 | 0 |
https://github.com/openssl/openssl/blob/9b02dc97e4963969da69675a871dbe80e6d31cda/crypto/bn/bn_lib.c/#L260
|
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
BN_ULONG *a = NULL;
bn_check_top(b);
if (words > (INT_MAX / (4 * BN_BITS2))) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
return NULL;
}
if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
return NULL;
}
if (BN_get_flags(b, BN_FLG_SECURE))
a = OPENSSL_secure_zalloc(words * sizeof(*a));
else
a = OPENSSL_zalloc(words * sizeof(*a));
if (a == NULL) {
BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
return NULL;
}
assert(b->top <= words);
if (b->top > 0)
memcpy(a, b->d, sizeof(*a) * b->top);
return a;
}
|
['int DH_check(const DH *dh, int *ret)\n{\n int ok = 0, r;\n BN_CTX *ctx = NULL;\n BN_ULONG l;\n BIGNUM *t1 = NULL, *t2 = NULL;\n *ret = 0;\n ctx = BN_CTX_new();\n if (ctx == NULL)\n goto err;\n BN_CTX_start(ctx);\n t1 = BN_CTX_get(ctx);\n t2 = BN_CTX_get(ctx);\n if (t2 == NULL)\n goto err;\n if (dh->q) {\n if (BN_cmp(dh->g, BN_value_one()) <= 0)\n *ret |= DH_NOT_SUITABLE_GENERATOR;\n else if (BN_cmp(dh->g, dh->p) >= 0)\n *ret |= DH_NOT_SUITABLE_GENERATOR;\n else {\n if (!BN_mod_exp(t1, dh->g, dh->q, dh->p, ctx))\n goto err;\n if (!BN_is_one(t1))\n *ret |= DH_NOT_SUITABLE_GENERATOR;\n }\n r = BN_is_prime_ex(dh->q, BN_prime_checks, ctx, NULL);\n if (r < 0)\n goto err;\n if (!r)\n *ret |= DH_CHECK_Q_NOT_PRIME;\n if (!BN_div(t1, t2, dh->p, dh->q, ctx))\n goto err;\n if (!BN_is_one(t2))\n *ret |= DH_CHECK_INVALID_Q_VALUE;\n if (dh->j && BN_cmp(dh->j, t1))\n *ret |= DH_CHECK_INVALID_J_VALUE;\n } else if (BN_is_word(dh->g, DH_GENERATOR_2)) {\n l = BN_mod_word(dh->p, 24);\n if (l == (BN_ULONG)-1)\n goto err;\n if (l != 11)\n *ret |= DH_NOT_SUITABLE_GENERATOR;\n } else if (BN_is_word(dh->g, DH_GENERATOR_5)) {\n l = BN_mod_word(dh->p, 10);\n if (l == (BN_ULONG)-1)\n goto err;\n if ((l != 3) && (l != 7))\n *ret |= DH_NOT_SUITABLE_GENERATOR;\n } else\n *ret |= DH_UNABLE_TO_CHECK_GENERATOR;\n r = BN_is_prime_ex(dh->p, BN_prime_checks, ctx, NULL);\n if (r < 0)\n goto err;\n if (!r)\n *ret |= DH_CHECK_P_NOT_PRIME;\n else if (!dh->q) {\n if (!BN_rshift1(t1, dh->p))\n goto err;\n r = BN_is_prime_ex(t1, BN_prime_checks, ctx, NULL);\n if (r < 0)\n goto err;\n if (!r)\n *ret |= DH_CHECK_P_NOT_SAFE_PRIME;\n }\n ok = 1;\n err:\n if (ctx != NULL) {\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n }\n return ok;\n}', '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_rshift1(BIGNUM *r, const BIGNUM *a)\n{\n BN_ULONG *ap, *rp, t, c;\n int i, j;\n bn_check_top(r);\n bn_check_top(a);\n if (BN_is_zero(a)) {\n BN_zero(r);\n return 1;\n }\n i = a->top;\n ap = a->d;\n j = i - (ap[i - 1] == 1);\n if (a != r) {\n if (bn_wexpand(r, j) == NULL)\n return 0;\n r->neg = a->neg;\n }\n rp = r->d;\n t = ap[--i];\n c = (t & 1) ? BN_TBIT : 0;\n if (t >>= 1)\n rp[i] = t;\n while (i > 0) {\n t = ap[--i];\n rp[i] = ((t >> 1) & BN_MASK2) | c;\n c = (t & 1) ? BN_TBIT : 0;\n }\n r->top = j;\n if (!r->top)\n r->neg = 0;\n bn_check_top(r);\n return 1;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return 1;\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n INCREMENT(malloc_count);\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)(file); (void)(line);\n ret = malloc(num);\n#endif\n return ret;\n}']
|
1,005 | 0 |
https://github.com/openssl/openssl/blob/54d00677f305375eee65a0c9edb5f0980c5f020f/crypto/bn/bn_lib.c/#L232
|
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
BN_ULONG *a = NULL;
if (words > (INT_MAX / (4 * BN_BITS2))) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
return NULL;
}
if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
return NULL;
}
if (BN_get_flags(b, BN_FLG_SECURE))
a = OPENSSL_secure_zalloc(words * sizeof(*a));
else
a = OPENSSL_zalloc(words * sizeof(*a));
if (a == NULL) {
BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
return NULL;
}
assert(b->top <= words);
if (b->top > 0)
memcpy(a, b->d, sizeof(*a) * b->top);
return a;
}
|
['int 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}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ret->flags &= (~BN_FLG_CONSTTIME);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n a->flags &= ~BN_FLG_FIXED_TOP;\n bn_check_top(a);\n return 1;\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'int bn_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}']
|
1,006 | 0 |
https://github.com/libav/libav/blob/e58b75f7ff4733b0de17b2b91d1dac364627cb9d/libavformat/movenc.c/#L1085
|
static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track)
{
MOVStts *ctts_entries;
uint32_t entries = 0;
uint32_t atom_size;
int i;
ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries));
ctts_entries[0].count = 1;
ctts_entries[0].duration = track->cluster[0].cts;
for (i=1; i<track->entry; i++) {
if (track->cluster[i].cts == ctts_entries[entries].duration) {
ctts_entries[entries].count++;
} else {
entries++;
ctts_entries[entries].duration = track->cluster[i].cts;
ctts_entries[entries].count = 1;
}
}
entries++;
atom_size = 16 + (entries * 8);
avio_wb32(pb, atom_size);
ffio_wfourcc(pb, "ctts");
avio_wb32(pb, 0);
avio_wb32(pb, entries);
for (i=0; i<entries; i++) {
avio_wb32(pb, ctts_entries[i].count);
avio_wb32(pb, ctts_entries[i].duration);
}
av_free(ctts_entries);
return atom_size;
}
|
['static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track)\n{\n MOVStts *ctts_entries;\n uint32_t entries = 0;\n uint32_t atom_size;\n int i;\n ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries));\n ctts_entries[0].count = 1;\n ctts_entries[0].duration = track->cluster[0].cts;\n for (i=1; i<track->entry; i++) {\n if (track->cluster[i].cts == ctts_entries[entries].duration) {\n ctts_entries[entries].count++;\n } else {\n entries++;\n ctts_entries[entries].duration = track->cluster[i].cts;\n ctts_entries[entries].count = 1;\n }\n }\n entries++;\n atom_size = 16 + (entries * 8);\n avio_wb32(pb, atom_size);\n ffio_wfourcc(pb, "ctts");\n avio_wb32(pb, 0);\n avio_wb32(pb, entries);\n for (i=0; i<entries; i++) {\n avio_wb32(pb, ctts_entries[i].count);\n avio_wb32(pb, ctts_entries[i].duration);\n }\n av_free(ctts_entries);\n return atom_size;\n}', 'void *av_malloc(size_t size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n 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}']
|
1,007 | 0 |
https://github.com/openssl/openssl/blob/9c46f4b9cd4912b61cb546c48b678488d7f26ed6/crypto/asn1/tasn_dec.c/#L819
|
static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
const unsigned char **in, long inlen,
const ASN1_ITEM *it,
int tag, int aclass, char opt, ASN1_TLC *ctx)
{
int ret = 0, utype;
long plen;
char cst, inf, free_cont = 0;
const unsigned char *p;
BUF_MEM buf;
const unsigned char *cont = NULL;
long len;
if (!pval) {
ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
return 0;
}
if (it->itype == ASN1_ITYPE_MSTRING) {
utype = tag;
tag = -1;
} else
utype = it->utype;
if (utype == V_ASN1_ANY) {
unsigned char oclass;
if (tag >= 0) {
ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_TAGGED_ANY);
return 0;
}
if (opt) {
ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
ASN1_R_ILLEGAL_OPTIONAL_ANY);
return 0;
}
p = *in;
ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
&p, inlen, -1, 0, 0, ctx);
if (!ret) {
ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
return 0;
}
if (oclass != V_ASN1_UNIVERSAL)
utype = V_ASN1_OTHER;
}
if (tag == -1) {
tag = utype;
aclass = V_ASN1_UNIVERSAL;
}
p = *in;
ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
&p, inlen, tag, aclass, opt, ctx);
if (!ret) {
ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
return 0;
} else if (ret == -1)
return -1;
ret = 0;
if ((utype == V_ASN1_SEQUENCE)
|| (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) {
if (utype == V_ASN1_OTHER) {
asn1_tlc_clear(ctx);
}
else if (!cst) {
ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
ASN1_R_TYPE_NOT_CONSTRUCTED);
return 0;
}
cont = *in;
if (inf) {
if (!asn1_find_end(&p, plen, inf))
goto err;
len = p - cont;
} else {
len = p - cont + plen;
p += plen;
buf.data = NULL;
}
} else if (cst) {
if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
|| utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
|| utype == V_ASN1_ENUMERATED) {
ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_TYPE_NOT_PRIMITIVE);
return 0;
}
buf.length = 0;
buf.max = 0;
buf.data = NULL;
if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
free_cont = 1;
goto err;
}
len = buf.length;
if (!BUF_MEM_grow_clean(&buf, len + 1)) {
ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_MALLOC_FAILURE);
return 0;
}
buf.data[len] = 0;
cont = (const unsigned char *)buf.data;
free_cont = 1;
} else {
cont = p;
len = plen;
p += plen;
}
if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
goto err;
*in = p;
ret = 1;
err:
if (free_cont && buf.data)
OPENSSL_free(buf.data);
return ret;
}
|
['static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,\n const unsigned char **in, long inlen,\n const ASN1_ITEM *it,\n int tag, int aclass, char opt, ASN1_TLC *ctx)\n{\n int ret = 0, utype;\n long plen;\n char cst, inf, free_cont = 0;\n const unsigned char *p;\n BUF_MEM buf;\n const unsigned char *cont = NULL;\n long len;\n if (!pval) {\n ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);\n return 0;\n }\n if (it->itype == ASN1_ITYPE_MSTRING) {\n utype = tag;\n tag = -1;\n } else\n utype = it->utype;\n if (utype == V_ASN1_ANY) {\n unsigned char oclass;\n if (tag >= 0) {\n ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_TAGGED_ANY);\n return 0;\n }\n if (opt) {\n ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,\n ASN1_R_ILLEGAL_OPTIONAL_ANY);\n return 0;\n }\n p = *in;\n ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,\n &p, inlen, -1, 0, 0, ctx);\n if (!ret) {\n ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);\n return 0;\n }\n if (oclass != V_ASN1_UNIVERSAL)\n utype = V_ASN1_OTHER;\n }\n if (tag == -1) {\n tag = utype;\n aclass = V_ASN1_UNIVERSAL;\n }\n p = *in;\n ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,\n &p, inlen, tag, aclass, opt, ctx);\n if (!ret) {\n ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);\n return 0;\n } else if (ret == -1)\n return -1;\n ret = 0;\n if ((utype == V_ASN1_SEQUENCE)\n || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) {\n if (utype == V_ASN1_OTHER) {\n asn1_tlc_clear(ctx);\n }\n else if (!cst) {\n ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,\n ASN1_R_TYPE_NOT_CONSTRUCTED);\n return 0;\n }\n cont = *in;\n if (inf) {\n if (!asn1_find_end(&p, plen, inf))\n goto err;\n len = p - cont;\n } else {\n len = p - cont + plen;\n p += plen;\n buf.data = NULL;\n }\n } else if (cst) {\n if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN\n || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER\n || utype == V_ASN1_ENUMERATED) {\n ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_TYPE_NOT_PRIMITIVE);\n return 0;\n }\n buf.length = 0;\n buf.max = 0;\n buf.data = NULL;\n if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {\n free_cont = 1;\n goto err;\n }\n len = buf.length;\n if (!BUF_MEM_grow_clean(&buf, len + 1)) {\n ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n buf.data[len] = 0;\n cont = (const unsigned char *)buf.data;\n free_cont = 1;\n } else {\n cont = p;\n len = plen;\n p += plen;\n }\n if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))\n goto err;\n *in = p;\n ret = 1;\n err:\n if (free_cont && buf.data)\n OPENSSL_free(buf.data);\n return ret;\n}']
|
1,008 | 0 |
https://gitlab.com/libtiff/libtiff/blob/b69a1998bedfabc32cd541408bffdef05bd01e45/libtiff/tif_dirwrite.c/#L2312
|
static int
TIFFWriteDirectoryTagData(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 datatype, uint32 count, uint32 datalength, void* data)
{
static const char module[] = "TIFFWriteDirectoryTagData";
uint32 m;
m=0;
while (m<(*ndir))
{
assert(dir[m].tdir_tag!=tag);
if (dir[m].tdir_tag>tag)
break;
m++;
}
if (m<(*ndir))
{
uint32 n;
for (n=*ndir; n>m; n--)
dir[n]=dir[n-1];
}
dir[m].tdir_tag=tag;
dir[m].tdir_type=datatype;
dir[m].tdir_count=count;
dir[m].tdir_offset.toff_long8 = 0;
if (datalength<=((tif->tif_flags&TIFF_BIGTIFF)?0x8U:0x4U))
_TIFFmemcpy(&dir[m].tdir_offset,data,datalength);
else
{
uint64 na,nb;
na=tif->tif_dataoff;
nb=na+datalength;
if (!(tif->tif_flags&TIFF_BIGTIFF))
nb=(uint32)nb;
if ((nb<na)||(nb<datalength))
{
TIFFErrorExt(tif->tif_clientdata,module,"Maximum TIFF file size exceeded");
return(0);
}
if (!SeekOK(tif,na))
{
TIFFErrorExt(tif->tif_clientdata,module,"IO error writing tag data");
return(0);
}
assert(datalength<0x80000000UL);
if (!WriteOK(tif,data,(tmsize_t)datalength))
{
TIFFErrorExt(tif->tif_clientdata,module,"IO error writing tag data");
return(0);
}
tif->tif_dataoff=nb;
if (tif->tif_dataoff&1)
tif->tif_dataoff++;
if (!(tif->tif_flags&TIFF_BIGTIFF))
{
uint32 o;
o=(uint32)na;
if (tif->tif_flags&TIFF_SWAB)
TIFFSwabLong(&o);
_TIFFmemcpy(&dir[m].tdir_offset,&o,4);
}
else
{
dir[m].tdir_offset.toff_long8 = na;
if (tif->tif_flags&TIFF_SWAB)
TIFFSwabLong8(&dir[m].tdir_offset.toff_long8);
}
}
(*ndir)++;
return(1);
}
|
['static int\nTIFFWriteDirectorySec(TIFF* tif, int isimage, int imagedone, uint64* pdiroff)\n{\n\tstatic const char module[] = "TIFFWriteDirectorySec";\n\tuint32 ndir;\n\tTIFFDirEntry* dir;\n\tuint32 dirsize;\n\tvoid* dirmem;\n\tuint32 m;\n\tif (tif->tif_mode == O_RDONLY)\n\t\treturn (1);\n _TIFFFillStriles( tif );\n\tif (imagedone)\n\t{\n\t\tif (tif->tif_flags & TIFF_POSTENCODE)\n\t\t{\n\t\t\ttif->tif_flags &= ~TIFF_POSTENCODE;\n\t\t\tif (!(*tif->tif_postencode)(tif))\n\t\t\t{\n\t\t\t\tTIFFErrorExt(tif->tif_clientdata,module,\n\t\t\t\t "Error post-encoding before directory write");\n\t\t\t\treturn (0);\n\t\t\t}\n\t\t}\n\t\t(*tif->tif_close)(tif);\n\t\tif (tif->tif_rawcc > 0\n\t\t && (tif->tif_flags & TIFF_BEENWRITING) != 0 )\n\t\t{\n\t\t if( !TIFFFlushData1(tif) )\n {\n\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t "Error flushing data before directory write");\n\t\t\treturn (0);\n }\n\t\t}\n\t\tif ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)\n\t\t{\n\t\t\t_TIFFfree(tif->tif_rawdata);\n\t\t\ttif->tif_rawdata = NULL;\n\t\t\ttif->tif_rawcc = 0;\n\t\t\ttif->tif_rawdatasize = 0;\n tif->tif_rawdataoff = 0;\n tif->tif_rawdataloaded = 0;\n\t\t}\n\t\ttif->tif_flags &= ~(TIFF_BEENWRITING|TIFF_BUFFERSETUP);\n\t}\n\tdir=NULL;\n\tdirmem=NULL;\n\tdirsize=0;\n\twhile (1)\n\t{\n\t\tndir=0;\n\t\tif (isimage)\n\t\t{\n\t\t\tif (TIFFFieldSet(tif,FIELD_IMAGEDIMENSIONS))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_IMAGEWIDTH,tif->tif_dir.td_imagewidth))\n\t\t\t\t\tgoto bad;\n\t\t\t\tif (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_IMAGELENGTH,tif->tif_dir.td_imagelength))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_TILEDIMENSIONS))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_TILEWIDTH,tif->tif_dir.td_tilewidth))\n\t\t\t\t\tgoto bad;\n\t\t\t\tif (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_TILELENGTH,tif->tif_dir.td_tilelength))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_RESOLUTION))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_XRESOLUTION,tif->tif_dir.td_xresolution))\n\t\t\t\t\tgoto bad;\n\t\t\t\tif (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_YRESOLUTION,tif->tif_dir.td_yresolution))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_POSITION))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_XPOSITION,tif->tif_dir.td_xposition))\n\t\t\t\t\tgoto bad;\n\t\t\t\tif (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_YPOSITION,tif->tif_dir.td_yposition))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_SUBFILETYPE))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,TIFFTAG_SUBFILETYPE,tif->tif_dir.td_subfiletype))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_BITSPERSAMPLE,tif->tif_dir.td_bitspersample))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_COMPRESSION))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_COMPRESSION,tif->tif_dir.td_compression))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_PHOTOMETRIC))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_PHOTOMETRIC,tif->tif_dir.td_photometric))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_THRESHHOLDING))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_THRESHHOLDING,tif->tif_dir.td_threshholding))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_FILLORDER))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_FILLORDER,tif->tif_dir.td_fillorder))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_ORIENTATION))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_ORIENTATION,tif->tif_dir.td_orientation))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_SAMPLESPERPIXEL))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_SAMPLESPERPIXEL,tif->tif_dir.td_samplesperpixel))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_ROWSPERSTRIP))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_ROWSPERSTRIP,tif->tif_dir.td_rowsperstrip))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_MINSAMPLEVALUE))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_MINSAMPLEVALUE,tif->tif_dir.td_minsamplevalue))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_MAXSAMPLEVALUE))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_MAXSAMPLEVALUE,tif->tif_dir.td_maxsamplevalue))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_PLANARCONFIG))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_PLANARCONFIG,tif->tif_dir.td_planarconfig))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_RESOLUTIONUNIT))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_RESOLUTIONUNIT,tif->tif_dir.td_resolutionunit))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_PAGENUMBER))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_PAGENUMBER,2,&tif->tif_dir.td_pagenumber[0]))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_STRIPBYTECOUNTS))\n\t\t\t{\n\t\t\t\tif (!isTiled(tif))\n\t\t\t\t{\n\t\t\t\t\tif (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_STRIPBYTECOUNTS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripbytecount))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tif (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_TILEBYTECOUNTS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripbytecount))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_STRIPOFFSETS))\n\t\t\t{\n\t\t\t\tif (!isTiled(tif))\n\t\t\t\t{\n\t\t\t\t\tif (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_STRIPOFFSETS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripoffset))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tif (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_TILEOFFSETS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripoffset))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_COLORMAP))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagColormap(tif,&ndir,dir))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_EXTRASAMPLES))\n\t\t\t{\n\t\t\t\tif (tif->tif_dir.td_extrasamples)\n\t\t\t\t{\n\t\t\t\t\tuint16 na;\n\t\t\t\t\tuint16* nb;\n\t\t\t\t\tTIFFGetFieldDefaulted(tif,TIFFTAG_EXTRASAMPLES,&na,&nb);\n\t\t\t\t\tif (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_EXTRASAMPLES,na,nb))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_SAMPLEFORMAT))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_SAMPLEFORMAT,tif->tif_dir.td_sampleformat))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_SMINSAMPLEVALUE))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagSampleformatArray(tif,&ndir,dir,TIFFTAG_SMINSAMPLEVALUE,tif->tif_dir.td_samplesperpixel,tif->tif_dir.td_sminsamplevalue))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_SMAXSAMPLEVALUE))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagSampleformatArray(tif,&ndir,dir,TIFFTAG_SMAXSAMPLEVALUE,tif->tif_dir.td_samplesperpixel,tif->tif_dir.td_smaxsamplevalue))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_IMAGEDEPTH))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,TIFFTAG_IMAGEDEPTH,tif->tif_dir.td_imagedepth))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_TILEDEPTH))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,TIFFTAG_TILEDEPTH,tif->tif_dir.td_tiledepth))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_HALFTONEHINTS))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_HALFTONEHINTS,2,&tif->tif_dir.td_halftonehints[0]))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_YCBCRSUBSAMPLING))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_YCBCRSUBSAMPLING,2,&tif->tif_dir.td_ycbcrsubsampling[0]))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_YCBCRPOSITIONING))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_YCBCRPOSITIONING,tif->tif_dir.td_ycbcrpositioning))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_REFBLACKWHITE))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagRationalArray(tif,&ndir,dir,TIFFTAG_REFERENCEBLACKWHITE,6,tif->tif_dir.td_refblackwhite))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_TRANSFERFUNCTION))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagTransferfunction(tif,&ndir,dir))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_INKNAMES))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagAscii(tif,&ndir,dir,TIFFTAG_INKNAMES,tif->tif_dir.td_inknameslen,tif->tif_dir.td_inknames))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_SUBIFD))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagSubifd(tif,&ndir,dir))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\t{\n\t\t\t\tuint32 n;\n\t\t\t\tfor (n=0; n<tif->tif_nfields; n++) {\n\t\t\t\t\tconst TIFFField* o;\n\t\t\t\t\to = tif->tif_fields[n];\n\t\t\t\t\tif ((o->field_bit>=FIELD_CODEC)&&(TIFFFieldSet(tif,o->field_bit)))\n\t\t\t\t\t{\n\t\t\t\t\t\tswitch (o->get_field_type)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tcase TIFF_SETGET_ASCII:\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tuint32 pa;\n\t\t\t\t\t\t\t\t\tchar* pb;\n\t\t\t\t\t\t\t\t\tassert(o->field_type==TIFF_ASCII);\n\t\t\t\t\t\t\t\t\tassert(o->field_readcount==TIFF_VARIABLE);\n\t\t\t\t\t\t\t\t\tassert(o->field_passcount==0);\n\t\t\t\t\t\t\t\t\tTIFFGetField(tif,o->field_tag,&pb);\n\t\t\t\t\t\t\t\t\tpa=(uint32)(strlen(pb));\n\t\t\t\t\t\t\t\t\tif (!TIFFWriteDirectoryTagAscii(tif,&ndir,dir,o->field_tag,pa,pb))\n\t\t\t\t\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\tcase TIFF_SETGET_UINT16:\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tuint16 p;\n\t\t\t\t\t\t\t\t\tassert(o->field_type==TIFF_SHORT);\n\t\t\t\t\t\t\t\t\tassert(o->field_readcount==1);\n\t\t\t\t\t\t\t\t\tassert(o->field_passcount==0);\n\t\t\t\t\t\t\t\t\tTIFFGetField(tif,o->field_tag,&p);\n\t\t\t\t\t\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,o->field_tag,p))\n\t\t\t\t\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\tcase TIFF_SETGET_UINT32:\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tuint32 p;\n\t\t\t\t\t\t\t\t\tassert(o->field_type==TIFF_LONG);\n\t\t\t\t\t\t\t\t\tassert(o->field_readcount==1);\n\t\t\t\t\t\t\t\t\tassert(o->field_passcount==0);\n\t\t\t\t\t\t\t\t\tTIFFGetField(tif,o->field_tag,&p);\n\t\t\t\t\t\t\t\t\tif (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,o->field_tag,p))\n\t\t\t\t\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\tcase TIFF_SETGET_C32_UINT8:\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tuint32 pa;\n\t\t\t\t\t\t\t\t\tvoid* pb;\n\t\t\t\t\t\t\t\t\tassert(o->field_type==TIFF_UNDEFINED);\n\t\t\t\t\t\t\t\t\tassert(o->field_readcount==TIFF_VARIABLE2);\n\t\t\t\t\t\t\t\t\tassert(o->field_passcount==1);\n\t\t\t\t\t\t\t\t\tTIFFGetField(tif,o->field_tag,&pa,&pb);\n\t\t\t\t\t\t\t\t\tif (!TIFFWriteDirectoryTagUndefinedArray(tif,&ndir,dir,o->field_tag,pa,pb))\n\t\t\t\t\t\t\t\t\t\tgoto bad;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\tdefault:\n\t\t\t\t\t\t\t\tassert(0);\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tfor (m=0; m<(uint32)(tif->tif_dir.td_customValueCount); m++)\n\t\t{\n\t\t\tswitch (tif->tif_dir.td_customValues[m].info->field_type)\n\t\t\t{\n\t\t\t\tcase TIFF_ASCII:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagAscii(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_UNDEFINED:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagUndefinedArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_BYTE:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagByteArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_SBYTE:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagSbyteArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_SHORT:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_SSHORT:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagSshortArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_LONG:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagLongArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_SLONG:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagSlongArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_LONG8:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagLong8Array(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_SLONG8:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagSlong8Array(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_RATIONAL:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagRationalArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_SRATIONAL:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagSrationalArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_FLOAT:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagFloatArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_DOUBLE:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagDoubleArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_IFD:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagIfdArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tcase TIFF_IFD8:\n\t\t\t\t\tif (!TIFFWriteDirectoryTagIfdIfd8Array(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t\tbreak;\n\t\t\t\tdefault:\n\t\t\t\t\tassert(0);\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif (dir!=NULL)\n\t\t\tbreak;\n\t\tdir=_TIFFmalloc(ndir*sizeof(TIFFDirEntry));\n\t\tif (dir==NULL)\n\t\t{\n\t\t\tTIFFErrorExt(tif->tif_clientdata,module,"Out of memory");\n\t\t\tgoto bad;\n\t\t}\n\t\tif (isimage)\n\t\t{\n\t\t\tif ((tif->tif_diroff==0)&&(!TIFFLinkDirectory(tif)))\n\t\t\t\tgoto bad;\n\t\t}\n\t\telse\n\t\t\ttif->tif_diroff=(TIFFSeekFile(tif,0,SEEK_END)+1)&(~1);\n\t\tif (pdiroff!=NULL)\n\t\t\t*pdiroff=tif->tif_diroff;\n\t\tif (!(tif->tif_flags&TIFF_BIGTIFF))\n\t\t\tdirsize=2+ndir*12+4;\n\t\telse\n\t\t\tdirsize=8+ndir*20+8;\n\t\ttif->tif_dataoff=tif->tif_diroff+dirsize;\n\t\tif (!(tif->tif_flags&TIFF_BIGTIFF))\n\t\t\ttif->tif_dataoff=(uint32)tif->tif_dataoff;\n\t\tif ((tif->tif_dataoff<tif->tif_diroff)||(tif->tif_dataoff<(uint64)dirsize))\n\t\t{\n\t\t\tTIFFErrorExt(tif->tif_clientdata,module,"Maximum TIFF file size exceeded");\n\t\t\tgoto bad;\n\t\t}\n\t\tif (tif->tif_dataoff&1)\n\t\t\ttif->tif_dataoff++;\n\t\tif (isimage)\n\t\t\ttif->tif_curdir++;\n\t}\n\tif (isimage)\n\t{\n\t\tif (TIFFFieldSet(tif,FIELD_SUBIFD)&&(tif->tif_subifdoff==0))\n\t\t{\n\t\t\tuint32 na;\n\t\t\tTIFFDirEntry* nb;\n\t\t\tfor (na=0, nb=dir; ; na++, nb++)\n\t\t\t{\n\t\t\t\tassert(na<ndir);\n\t\t\t\tif (nb->tdir_tag==TIFFTAG_SUBIFD)\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t\tif (!(tif->tif_flags&TIFF_BIGTIFF))\n\t\t\t\ttif->tif_subifdoff=tif->tif_diroff+2+na*12+8;\n\t\t\telse\n\t\t\t\ttif->tif_subifdoff=tif->tif_diroff+8+na*20+12;\n\t\t}\n\t}\n\tdirmem=_TIFFmalloc(dirsize);\n\tif (dirmem==NULL)\n\t{\n\t\tTIFFErrorExt(tif->tif_clientdata,module,"Out of memory");\n\t\tgoto bad;\n\t}\n\tif (!(tif->tif_flags&TIFF_BIGTIFF))\n\t{\n\t\tuint8* n;\n\t\tuint32 nTmp;\n\t\tTIFFDirEntry* o;\n\t\tn=dirmem;\n\t\t*(uint16*)n=ndir;\n\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\tTIFFSwabShort((uint16*)n);\n\t\tn+=2;\n\t\to=dir;\n\t\tfor (m=0; m<ndir; m++)\n\t\t{\n\t\t\t*(uint16*)n=o->tdir_tag;\n\t\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\t\tTIFFSwabShort((uint16*)n);\n\t\t\tn+=2;\n\t\t\t*(uint16*)n=o->tdir_type;\n\t\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\t\tTIFFSwabShort((uint16*)n);\n\t\t\tn+=2;\n\t\t\tnTmp = (uint32)o->tdir_count;\n\t\t\t_TIFFmemcpy(n,&nTmp,4);\n\t\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\t\tTIFFSwabLong((uint32*)n);\n\t\t\tn+=4;\n\t\t\t_TIFFmemcpy(n,&o->tdir_offset,4);\n\t\t\tn+=4;\n\t\t\to++;\n\t\t}\n\t\tnTmp = (uint32)tif->tif_nextdiroff;\n\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\tTIFFSwabLong(&nTmp);\n\t\t_TIFFmemcpy(n,&nTmp,4);\n\t}\n\telse\n\t{\n\t\tuint8* n;\n\t\tTIFFDirEntry* o;\n\t\tn=dirmem;\n\t\t*(uint64*)n=ndir;\n\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\tTIFFSwabLong8((uint64*)n);\n\t\tn+=8;\n\t\to=dir;\n\t\tfor (m=0; m<ndir; m++)\n\t\t{\n\t\t\t*(uint16*)n=o->tdir_tag;\n\t\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\t\tTIFFSwabShort((uint16*)n);\n\t\t\tn+=2;\n\t\t\t*(uint16*)n=o->tdir_type;\n\t\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\t\tTIFFSwabShort((uint16*)n);\n\t\t\tn+=2;\n\t\t\t_TIFFmemcpy(n,&o->tdir_count,8);\n\t\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\t\tTIFFSwabLong8((uint64*)n);\n\t\t\tn+=8;\n\t\t\t_TIFFmemcpy(n,&o->tdir_offset,8);\n\t\t\tn+=8;\n\t\t\to++;\n\t\t}\n\t\t_TIFFmemcpy(n,&tif->tif_nextdiroff,8);\n\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\tTIFFSwabLong8((uint64*)n);\n\t}\n\t_TIFFfree(dir);\n\tdir=NULL;\n\tif (!SeekOK(tif,tif->tif_diroff))\n\t{\n\t\tTIFFErrorExt(tif->tif_clientdata,module,"IO error writing directory");\n\t\tgoto bad;\n\t}\n\tif (!WriteOK(tif,dirmem,(tmsize_t)dirsize))\n\t{\n\t\tTIFFErrorExt(tif->tif_clientdata,module,"IO error writing directory");\n\t\tgoto bad;\n\t}\n\t_TIFFfree(dirmem);\n\tif (imagedone)\n\t{\n\t\tTIFFFreeDirectory(tif);\n\t\ttif->tif_flags &= ~TIFF_DIRTYDIRECT;\n\t\ttif->tif_flags &= ~TIFF_DIRTYSTRIP;\n\t\t(*tif->tif_cleanup)(tif);\n\t\tTIFFCreateDirectory(tif);\n\t}\n\treturn(1);\nbad:\n\tif (dir!=NULL)\n\t\t_TIFFfree(dir);\n\tif (dirmem!=NULL)\n\t\t_TIFFfree(dirmem);\n\treturn(0);\n}', 'static int\nTIFFWriteDirectoryTagSubifd(TIFF* tif, uint32* ndir, TIFFDirEntry* dir)\n{\n\tstatic const char module[] = "TIFFWriteDirectoryTagSubifd";\n\tuint64 m;\n\tint n;\n\tif (tif->tif_dir.td_nsubifd==0)\n\t\treturn(1);\n\tif (dir==NULL)\n\t{\n\t\t(*ndir)++;\n\t\treturn(1);\n\t}\n\tm=tif->tif_dataoff;\n\tif (!(tif->tif_flags&TIFF_BIGTIFF))\n\t{\n\t\tuint32* o;\n\t\tuint64* pa;\n\t\tuint32* pb;\n\t\tuint16 p;\n\t\to=_TIFFmalloc(tif->tif_dir.td_nsubifd*sizeof(uint32));\n\t\tif (o==NULL)\n\t\t{\n\t\t\tTIFFErrorExt(tif->tif_clientdata,module,"Out of memory");\n\t\t\treturn(0);\n\t\t}\n\t\tpa=tif->tif_dir.td_subifd;\n\t\tpb=o;\n\t\tfor (p=0; p < tif->tif_dir.td_nsubifd; p++)\n\t\t{\n assert(pa != 0);\n\t\t\tassert(*pa <= 0xFFFFFFFFUL);\n\t\t\t*pb++=(uint32)(*pa++);\n\t\t}\n\t\tn=TIFFWriteDirectoryTagCheckedIfdArray(tif,ndir,dir,TIFFTAG_SUBIFD,tif->tif_dir.td_nsubifd,o);\n\t\t_TIFFfree(o);\n\t}\n\telse\n\t\tn=TIFFWriteDirectoryTagCheckedIfd8Array(tif,ndir,dir,TIFFTAG_SUBIFD,tif->tif_dir.td_nsubifd,tif->tif_dir.td_subifd);\n\tif (!n)\n\t\treturn(0);\n\ttif->tif_flags|=TIFF_INSUBIFD;\n\ttif->tif_nsubifd=tif->tif_dir.td_nsubifd;\n\tif (tif->tif_dir.td_nsubifd==1)\n\t\ttif->tif_subifdoff=0;\n\telse\n\t\ttif->tif_subifdoff=m;\n\treturn(1);\n}', 'static int\nTIFFWriteDirectoryTagCheckedIfdArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value)\n{\n\tassert(count<0x40000000);\n\tassert(sizeof(uint32)==4);\n\tif (tif->tif_flags&TIFF_SWAB)\n\t\tTIFFSwabArrayOfLong(value,count);\n\treturn(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_IFD,count,count*4,value));\n}', 'static int\nTIFFWriteDirectoryTagData(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 datatype, uint32 count, uint32 datalength, void* data)\n{\n\tstatic const char module[] = "TIFFWriteDirectoryTagData";\n\tuint32 m;\n\tm=0;\n\twhile (m<(*ndir))\n\t{\n\t\tassert(dir[m].tdir_tag!=tag);\n\t\tif (dir[m].tdir_tag>tag)\n\t\t\tbreak;\n\t\tm++;\n\t}\n\tif (m<(*ndir))\n\t{\n\t\tuint32 n;\n\t\tfor (n=*ndir; n>m; n--)\n\t\t\tdir[n]=dir[n-1];\n\t}\n\tdir[m].tdir_tag=tag;\n\tdir[m].tdir_type=datatype;\n\tdir[m].tdir_count=count;\n\tdir[m].tdir_offset.toff_long8 = 0;\n\tif (datalength<=((tif->tif_flags&TIFF_BIGTIFF)?0x8U:0x4U))\n\t\t_TIFFmemcpy(&dir[m].tdir_offset,data,datalength);\n\telse\n\t{\n\t\tuint64 na,nb;\n\t\tna=tif->tif_dataoff;\n\t\tnb=na+datalength;\n\t\tif (!(tif->tif_flags&TIFF_BIGTIFF))\n\t\t\tnb=(uint32)nb;\n\t\tif ((nb<na)||(nb<datalength))\n\t\t{\n\t\t\tTIFFErrorExt(tif->tif_clientdata,module,"Maximum TIFF file size exceeded");\n\t\t\treturn(0);\n\t\t}\n\t\tif (!SeekOK(tif,na))\n\t\t{\n\t\t\tTIFFErrorExt(tif->tif_clientdata,module,"IO error writing tag data");\n\t\t\treturn(0);\n\t\t}\n\t\tassert(datalength<0x80000000UL);\n\t\tif (!WriteOK(tif,data,(tmsize_t)datalength))\n\t\t{\n\t\t\tTIFFErrorExt(tif->tif_clientdata,module,"IO error writing tag data");\n\t\t\treturn(0);\n\t\t}\n\t\ttif->tif_dataoff=nb;\n\t\tif (tif->tif_dataoff&1)\n\t\t\ttif->tif_dataoff++;\n\t\tif (!(tif->tif_flags&TIFF_BIGTIFF))\n\t\t{\n\t\t\tuint32 o;\n\t\t\to=(uint32)na;\n\t\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\t\tTIFFSwabLong(&o);\n\t\t\t_TIFFmemcpy(&dir[m].tdir_offset,&o,4);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tdir[m].tdir_offset.toff_long8 = na;\n\t\t\tif (tif->tif_flags&TIFF_SWAB)\n\t\t\t\tTIFFSwabLong8(&dir[m].tdir_offset.toff_long8);\n\t\t}\n\t}\n\t(*ndir)++;\n\treturn(1);\n}']
|
1,009 | 0 |
https://github.com/openssl/openssl/blob/18e1e302452e6dea4500b6f981cee7e151294dea/crypto/bn/bn_ctx.c/#L268
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['static int test_mod_exp_zero(void)\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 if (!TEST_ptr(m = BN_new())\n || !TEST_ptr(a = BN_new())\n || !TEST_ptr(p = BN_new())\n || !TEST_ptr(r = BN_new()))\n goto err;\n BN_one(m);\n BN_one(a);\n BN_zero(p);\n if (!TEST_true(BN_rand(a, 1024, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY)))\n goto err;\n if (!TEST_true(BN_mod_exp(r, a, p, m, ctx)))\n goto err;\n if (!TEST_true(a_is_zero_mod_one("BN_mod_exp", r, a)))\n failed = 1;\n if (!TEST_true(BN_mod_exp_recp(r, a, p, m, ctx)))\n goto err;\n if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_recp", r, a)))\n failed = 1;\n if (!TEST_true(BN_mod_exp_simple(r, a, p, m, ctx)))\n goto err;\n if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_simple", r, a)))\n failed = 1;\n if (!TEST_true(BN_mod_exp_mont(r, a, p, m, ctx, NULL)))\n goto err;\n if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont", r, a)))\n failed = 1;\n if (!TEST_true(BN_mod_exp_mont_consttime(r, a, p, m, ctx, NULL)))\n goto err;\n if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont_consttime", r, a)))\n failed = 1;\n if (!TEST_true(BN_mod_exp_mont_word(r, one_word, p, m, ctx, NULL)))\n goto err;\n if (!TEST_BN_eq_zero(r)) {\n TEST_error("BN_mod_exp_mont_word failed: "\n "1 ** 0 mod 1 = r (should be 0)");\n BN_print_var(r);\n goto err;\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_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n BN_CTX *ctx)\n{\n int ret;\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n if (BN_is_odd(m)) {\n# ifdef MONT_EXP_WORD\n if (a->top == 1 && !a->neg\n && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(a, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(m, BN_FLG_CONSTTIME) == 0)) {\n BN_ULONG A = a->d[0];\n ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);\n } else\n# endif\n ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);\n } else\n#endif\n#ifdef RECP_MUL_MOD\n {\n ret = BN_mod_exp_recp(r, a, p, m, ctx);\n }\n#else\n {\n ret = BN_mod_exp_simple(r, a, p, m, ctx);\n }\n#endif\n bn_check_top(r);\n return ret;\n}', 'int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx)\n{\n int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n int start = 1;\n BIGNUM *aa;\n BIGNUM *val[TABLE_SIZE];\n BN_RECP_CTX recp;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(a, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n BNerr(BN_F_BN_MOD_EXP_RECP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_abs_is_word(m, 1)) {\n ret = 1;\n BN_zero(r);\n } else {\n ret = BN_one(r);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n aa = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (val[0] == NULL)\n goto err;\n BN_RECP_CTX_init(&recp);\n if (m->neg) {\n if (!BN_copy(aa, m))\n goto err;\n aa->neg = 0;\n if (BN_RECP_CTX_set(&recp, aa, ctx) <= 0)\n goto err;\n } else {\n if (BN_RECP_CTX_set(&recp, m, ctx) <= 0)\n goto err;\n }\n if (!BN_nnmod(val[0], a, m, ctx))\n goto err;\n if (BN_is_zero(val[0])) {\n BN_zero(r);\n ret = 1;\n goto err;\n }\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!BN_mod_mul_reciprocal(aa, val[0], val[0], &recp, ctx))\n goto err;\n j = 1 << (window - 1);\n for (i = 1; i < j; i++) {\n if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul_reciprocal(val[i], val[i - 1], aa, &recp, ctx))\n goto err;\n }\n }\n start = 1;\n wvalue = 0;\n wstart = bits - 1;\n wend = 0;\n if (!BN_one(r))\n goto err;\n for (;;) {\n if (BN_is_bit_set(p, wstart) == 0) {\n if (!start)\n if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))\n goto err;\n if (wstart == 0)\n break;\n wstart--;\n continue;\n }\n j = wstart;\n wvalue = 1;\n wend = 0;\n for (i = 1; i < window; i++) {\n if (wstart - i < 0)\n break;\n if (BN_is_bit_set(p, wstart - i)) {\n wvalue <<= (i - wend);\n wvalue |= 1;\n wend = i;\n }\n }\n j = wend + 1;\n if (!start)\n for (i = 0; i < j; i++) {\n if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))\n goto err;\n }\n if (!BN_mod_mul_reciprocal(r, r, val[wvalue >> 1], &recp, ctx))\n goto err;\n wstart -= wend + 1;\n wvalue = 0;\n start = 0;\n if (wstart < 0)\n break;\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_RECP_CTX_free(&recp);\n bn_check_top(r);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_start()", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG("LEAVE BN_CTX_start()", ctx);\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG("ENTER BN_CTX_get()", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ret->flags &= (~BN_FLG_CONSTTIME);\n ctx->used++;\n CTXDBG("LEAVE BN_CTX_get()", ctx);\n return ret;\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int ret;\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return 0;\n }\n if (divisor->d[divisor->top - 1] == 0) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n ret = bn_div_fixed_top(dv, rm, num, divisor, ctx);\n if (ret) {\n if (dv != NULL)\n bn_correct_top(dv);\n if (rm != NULL)\n bn_correct_top(rm);\n }\n return ret;\n}', 'int bn_div_fixed_top(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,\n const BIGNUM *divisor, BN_CTX *ctx)\n{\n int norm_shift, i, j, loop;\n BIGNUM *tmp, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnum, *wnumtop;\n BN_ULONG d0, d1;\n int num_n, div_n;\n assert(divisor->top > 0 && divisor->d[divisor->top - 1] != 0);\n bn_check_top(num);\n bn_check_top(divisor);\n bn_check_top(dv);\n bn_check_top(rm);\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n if (!BN_copy(sdiv, divisor))\n goto err;\n norm_shift = bn_left_align(sdiv);\n sdiv->neg = 0;\n if (!(bn_lshift_fixed_top(snum, num, norm_shift)))\n goto err;\n div_n = sdiv->top;\n num_n = snum->top;\n if (num_n <= div_n) {\n if (bn_wexpand(snum, div_n + 1) == NULL)\n goto err;\n memset(&(snum->d[num_n]), 0, (div_n - num_n + 1) * sizeof(BN_ULONG));\n snum->top = num_n = div_n + 1;\n }\n loop = num_n - div_n;\n wnum = &(snum->d[loop]);\n wnumtop = &(snum->d[num_n - 1]);\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n if (!bn_wexpand(res, loop))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop;\n res->flags |= BN_FLG_FIXED_TOP;\n resp = &(res->d[loop]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n for (i = 0; i < loop; i++, wnumtop--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W)\n q = bn_div_3_words(wnumtop, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnumtop[0];\n n1 = wnumtop[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n BN_ULONG n2 = (wnumtop == wnum) ? 0 : wnumtop[-2];\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | n2))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= n2)))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum--;\n l0 = bn_sub_words(wnum, wnum, tmp->d, div_n + 1);\n q -= l0;\n for (l0 = 0 - l0, j = 0; j < div_n; j++)\n tmp->d[j] = sdiv->d[j] & l0;\n l0 = bn_add_words(wnum, wnum, tmp->d, div_n);\n (*wnumtop) += l0;\n assert((*wnumtop) == 0);\n *--resp = q;\n }\n snum->neg = num->neg;\n snum->top = div_n;\n snum->flags |= BN_FLG_FIXED_TOP;\n if (rm != NULL)\n bn_rshift_fixed_top(rm, snum, norm_shift);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return 0;\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_end()", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG("LEAVE BN_CTX_end()", ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
|
1,010 | 0 |
https://github.com/openssl/openssl/blob/9b340281873643d2b8a33047dc8bfa607f7e0c3c/crypto/lhash/lhash.c/#L191
|
static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,
OPENSSL_LH_DOALL_FUNC func,
OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)
{
int i;
OPENSSL_LH_NODE *a, *n;
if (lh == NULL)
return;
for (i = lh->num_nodes - 1; i >= 0; i--) {
a = lh->b[i];
while (a != NULL) {
n = a->next;
if (use_arg)
func_arg(a->data, arg);
else
func(a->data);
a = n;
}
}
}
|
['static int test_tlsext_status_type(void)\n{\n SSL_CTX *cctx = NULL, *sctx = NULL;\n SSL *clientssl = NULL, *serverssl = NULL;\n int testresult = 0;\n STACK_OF(OCSP_RESPID) *ids = NULL;\n OCSP_RESPID *id = NULL;\n BIO *certbio = NULL;\n if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),\n TLS1_VERSION, TLS_MAX_VERSION,\n &sctx, &cctx, cert, privkey))\n return 0;\n if (SSL_CTX_get_tlsext_status_type(cctx) != -1)\n goto end;\n clientssl = SSL_new(cctx);\n if (!TEST_int_eq(SSL_get_tlsext_status_type(clientssl), -1)\n || !TEST_true(SSL_set_tlsext_status_type(clientssl,\n TLSEXT_STATUSTYPE_ocsp))\n || !TEST_int_eq(SSL_get_tlsext_status_type(clientssl),\n TLSEXT_STATUSTYPE_ocsp))\n goto end;\n SSL_free(clientssl);\n clientssl = NULL;\n if (!SSL_CTX_set_tlsext_status_type(cctx, TLSEXT_STATUSTYPE_ocsp)\n || SSL_CTX_get_tlsext_status_type(cctx) != TLSEXT_STATUSTYPE_ocsp)\n goto end;\n clientssl = SSL_new(cctx);\n if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp)\n goto end;\n SSL_free(clientssl);\n clientssl = NULL;\n SSL_CTX_set_tlsext_status_cb(cctx, ocsp_client_cb);\n SSL_CTX_set_tlsext_status_arg(cctx, &cdummyarg);\n SSL_CTX_set_tlsext_status_cb(sctx, ocsp_server_cb);\n SSL_CTX_set_tlsext_status_arg(sctx, &cdummyarg);\n if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,\n &clientssl, NULL, NULL))\n || !TEST_true(create_ssl_connection(serverssl, clientssl,\n SSL_ERROR_NONE))\n || !TEST_true(ocsp_client_called)\n || !TEST_true(ocsp_server_called))\n goto end;\n SSL_free(serverssl);\n SSL_free(clientssl);\n serverssl = NULL;\n clientssl = NULL;\n ocsp_client_called = 0;\n ocsp_server_called = 0;\n cdummyarg = 0;\n if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,\n &clientssl, NULL, NULL))\n || !TEST_false(create_ssl_connection(serverssl, clientssl,\n SSL_ERROR_NONE))\n || !TEST_false(ocsp_client_called)\n || !TEST_false(ocsp_server_called))\n goto end;\n SSL_free(serverssl);\n SSL_free(clientssl);\n serverssl = NULL;\n clientssl = NULL;\n ocsp_client_called = 0;\n ocsp_server_called = 0;\n cdummyarg = 2;\n if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,\n &clientssl, NULL, NULL)))\n goto end;\n if (!TEST_ptr(certbio = BIO_new_file(cert, "r"))\n || !TEST_ptr(id = OCSP_RESPID_new())\n || !TEST_ptr(ids = sk_OCSP_RESPID_new_null())\n || !TEST_ptr(ocspcert = PEM_read_bio_X509(certbio,\n NULL, NULL, NULL))\n || !TEST_true(OCSP_RESPID_set_by_key(id, ocspcert))\n || !TEST_true(sk_OCSP_RESPID_push(ids, id)))\n goto end;\n id = NULL;\n SSL_set_tlsext_status_ids(clientssl, ids);\n ids = NULL;\n BIO_free(certbio);\n certbio = NULL;\n if (!TEST_true(create_ssl_connection(serverssl, clientssl,\n SSL_ERROR_NONE))\n || !TEST_true(ocsp_client_called)\n || !TEST_true(ocsp_server_called))\n goto end;\n testresult = 1;\n end:\n SSL_free(serverssl);\n SSL_free(clientssl);\n SSL_CTX_free(sctx);\n SSL_CTX_free(cctx);\n sk_OCSP_RESPID_pop_free(ids, OCSP_RESPID_free);\n OCSP_RESPID_free(id);\n BIO_free(certbio);\n X509_free(ocspcert);\n ocspcert = NULL;\n return testresult;\n}', 'int create_ssl_objects(SSL_CTX *serverctx, SSL_CTX *clientctx, SSL **sssl,\n SSL **cssl, BIO *s_to_c_fbio, BIO *c_to_s_fbio)\n{\n SSL *serverssl = NULL, *clientssl = NULL;\n BIO *s_to_c_bio = NULL, *c_to_s_bio = NULL;\n if (*sssl != NULL)\n serverssl = *sssl;\n else if (!TEST_ptr(serverssl = SSL_new(serverctx)))\n goto error;\n if (*cssl != NULL)\n clientssl = *cssl;\n else if (!TEST_ptr(clientssl = SSL_new(clientctx)))\n goto error;\n if (SSL_is_dtls(clientssl)) {\n if (!TEST_ptr(s_to_c_bio = BIO_new(bio_s_mempacket_test()))\n || !TEST_ptr(c_to_s_bio = BIO_new(bio_s_mempacket_test())))\n goto error;\n } else {\n if (!TEST_ptr(s_to_c_bio = BIO_new(BIO_s_mem()))\n || !TEST_ptr(c_to_s_bio = BIO_new(BIO_s_mem())))\n goto error;\n }\n if (s_to_c_fbio != NULL\n && !TEST_ptr(s_to_c_bio = BIO_push(s_to_c_fbio, s_to_c_bio)))\n goto error;\n if (c_to_s_fbio != NULL\n && !TEST_ptr(c_to_s_bio = BIO_push(c_to_s_fbio, c_to_s_bio)))\n goto error;\n BIO_set_mem_eof_return(s_to_c_bio, -1);\n BIO_set_mem_eof_return(c_to_s_bio, -1);\n SSL_set_bio(serverssl, c_to_s_bio, s_to_c_bio);\n BIO_up_ref(s_to_c_bio);\n BIO_up_ref(c_to_s_bio);\n SSL_set_bio(clientssl, s_to_c_bio, c_to_s_bio);\n *sssl = serverssl;\n *cssl = clientssl;\n return 1;\n error:\n SSL_free(serverssl);\n SSL_free(clientssl);\n BIO_free(s_to_c_bio);\n BIO_free(c_to_s_bio);\n BIO_free(s_to_c_fbio);\n BIO_free(c_to_s_fbio);\n return 0;\n}', 'SSL *SSL_new(SSL_CTX *ctx)\n{\n SSL *s;\n if (ctx == NULL) {\n SSLerr(SSL_F_SSL_NEW, SSL_R_NULL_SSL_CTX);\n return NULL;\n }\n if (ctx->method == NULL) {\n SSLerr(SSL_F_SSL_NEW, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);\n return NULL;\n }\n s = OPENSSL_zalloc(sizeof(*s));\n if (s == NULL)\n goto err;\n s->references = 1;\n s->lock = CRYPTO_THREAD_lock_new();\n if (s->lock == NULL) {\n OPENSSL_free(s);\n s = NULL;\n goto err;\n }\n RECORD_LAYER_init(&s->rlayer, s);\n s->options = ctx->options;\n s->dane.flags = ctx->dane.flags;\n s->min_proto_version = ctx->min_proto_version;\n s->max_proto_version = ctx->max_proto_version;\n s->mode = ctx->mode;\n s->max_cert_list = ctx->max_cert_list;\n s->max_early_data = ctx->max_early_data;\n s->recv_max_early_data = ctx->recv_max_early_data;\n s->num_tickets = ctx->num_tickets;\n s->pha_enabled = ctx->pha_enabled;\n s->tls13_ciphersuites = sk_SSL_CIPHER_dup(ctx->tls13_ciphersuites);\n if (s->tls13_ciphersuites == NULL)\n goto err;\n s->cert = ssl_cert_dup(ctx->cert);\n if (s->cert == NULL)\n goto err;\n RECORD_LAYER_set_read_ahead(&s->rlayer, ctx->read_ahead);\n s->msg_callback = ctx->msg_callback;\n s->msg_callback_arg = ctx->msg_callback_arg;\n s->verify_mode = ctx->verify_mode;\n s->not_resumable_session_cb = ctx->not_resumable_session_cb;\n s->record_padding_cb = ctx->record_padding_cb;\n s->record_padding_arg = ctx->record_padding_arg;\n s->block_padding = ctx->block_padding;\n s->sid_ctx_length = ctx->sid_ctx_length;\n if (!ossl_assert(s->sid_ctx_length <= sizeof(s->sid_ctx)))\n goto err;\n memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));\n s->verify_callback = ctx->default_verify_callback;\n s->generate_session_id = ctx->generate_session_id;\n s->param = X509_VERIFY_PARAM_new();\n if (s->param == NULL)\n goto err;\n X509_VERIFY_PARAM_inherit(s->param, ctx->param);\n s->quiet_shutdown = ctx->quiet_shutdown;\n s->ext.max_fragment_len_mode = ctx->ext.max_fragment_len_mode;\n s->max_send_fragment = ctx->max_send_fragment;\n s->split_send_fragment = ctx->split_send_fragment;\n s->max_pipelines = ctx->max_pipelines;\n if (s->max_pipelines > 1)\n RECORD_LAYER_set_read_ahead(&s->rlayer, 1);\n if (ctx->default_read_buf_len > 0)\n SSL_set_default_read_buffer_len(s, ctx->default_read_buf_len);\n SSL_CTX_up_ref(ctx);\n s->ctx = ctx;\n s->ext.debug_cb = 0;\n s->ext.debug_arg = NULL;\n s->ext.ticket_expected = 0;\n s->ext.status_type = ctx->ext.status_type;\n s->ext.status_expected = 0;\n s->ext.ocsp.ids = NULL;\n s->ext.ocsp.exts = NULL;\n s->ext.ocsp.resp = NULL;\n s->ext.ocsp.resp_len = 0;\n SSL_CTX_up_ref(ctx);\n s->session_ctx = ctx;\n#ifndef OPENSSL_NO_EC\n if (ctx->ext.ecpointformats) {\n s->ext.ecpointformats =\n OPENSSL_memdup(ctx->ext.ecpointformats,\n ctx->ext.ecpointformats_len);\n if (!s->ext.ecpointformats)\n goto err;\n s->ext.ecpointformats_len =\n ctx->ext.ecpointformats_len;\n }\n if (ctx->ext.supportedgroups) {\n s->ext.supportedgroups =\n OPENSSL_memdup(ctx->ext.supportedgroups,\n ctx->ext.supportedgroups_len\n * sizeof(*ctx->ext.supportedgroups));\n if (!s->ext.supportedgroups)\n goto err;\n s->ext.supportedgroups_len = ctx->ext.supportedgroups_len;\n }\n#endif\n#ifndef OPENSSL_NO_NEXTPROTONEG\n s->ext.npn = NULL;\n#endif\n if (s->ctx->ext.alpn) {\n s->ext.alpn = OPENSSL_malloc(s->ctx->ext.alpn_len);\n if (s->ext.alpn == NULL)\n goto err;\n memcpy(s->ext.alpn, s->ctx->ext.alpn, s->ctx->ext.alpn_len);\n s->ext.alpn_len = s->ctx->ext.alpn_len;\n }\n s->verified_chain = NULL;\n s->verify_result = X509_V_OK;\n s->default_passwd_callback = ctx->default_passwd_callback;\n s->default_passwd_callback_userdata = ctx->default_passwd_callback_userdata;\n s->method = ctx->method;\n s->key_update = SSL_KEY_UPDATE_NONE;\n s->allow_early_data_cb = ctx->allow_early_data_cb;\n s->allow_early_data_cb_data = ctx->allow_early_data_cb_data;\n if (!s->method->ssl_new(s))\n goto err;\n s->server = (ctx->method->ssl_accept == ssl_undefined_function) ? 0 : 1;\n if (!SSL_clear(s))\n goto err;\n if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data))\n goto err;\n#ifndef OPENSSL_NO_PSK\n s->psk_client_callback = ctx->psk_client_callback;\n s->psk_server_callback = ctx->psk_server_callback;\n#endif\n s->psk_find_session_cb = ctx->psk_find_session_cb;\n s->psk_use_session_cb = ctx->psk_use_session_cb;\n s->job = NULL;\n#ifndef OPENSSL_NO_CT\n if (!SSL_set_ct_validation_callback(s, ctx->ct_validation_callback,\n ctx->ct_validation_callback_arg))\n goto err;\n#endif\n return s;\n err:\n SSL_free(s);\n SSLerr(SSL_F_SSL_NEW, ERR_R_MALLOC_FAILURE);\n return NULL;\n}', 'void SSL_free(SSL *s)\n{\n int i;\n if (s == NULL)\n return;\n CRYPTO_DOWN_REF(&s->references, &i, s->lock);\n REF_PRINT_COUNT("SSL", s);\n if (i > 0)\n return;\n REF_ASSERT_ISNT(i < 0);\n X509_VERIFY_PARAM_free(s->param);\n dane_final(&s->dane);\n CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);\n RECORD_LAYER_release(&s->rlayer);\n ssl_free_wbio_buffer(s);\n BIO_free_all(s->wbio);\n s->wbio = NULL;\n BIO_free_all(s->rbio);\n s->rbio = NULL;\n BUF_MEM_free(s->init_buf);\n sk_SSL_CIPHER_free(s->cipher_list);\n sk_SSL_CIPHER_free(s->cipher_list_by_id);\n sk_SSL_CIPHER_free(s->tls13_ciphersuites);\n if (s->session != NULL) {\n ssl_clear_bad_session(s);\n SSL_SESSION_free(s->session);\n }\n SSL_SESSION_free(s->psksession);\n OPENSSL_free(s->psksession_id);\n clear_ciphers(s);\n ssl_cert_free(s->cert);\n OPENSSL_free(s->ext.hostname);\n SSL_CTX_free(s->session_ctx);\n#ifndef OPENSSL_NO_EC\n OPENSSL_free(s->ext.ecpointformats);\n OPENSSL_free(s->ext.supportedgroups);\n#endif\n sk_X509_EXTENSION_pop_free(s->ext.ocsp.exts, X509_EXTENSION_free);\n#ifndef OPENSSL_NO_OCSP\n sk_OCSP_RESPID_pop_free(s->ext.ocsp.ids, OCSP_RESPID_free);\n#endif\n#ifndef OPENSSL_NO_CT\n SCT_LIST_free(s->scts);\n OPENSSL_free(s->ext.scts);\n#endif\n OPENSSL_free(s->ext.ocsp.resp);\n OPENSSL_free(s->ext.alpn);\n OPENSSL_free(s->ext.tls13_cookie);\n OPENSSL_free(s->clienthello);\n OPENSSL_free(s->pha_context);\n EVP_MD_CTX_free(s->pha_dgst);\n sk_X509_NAME_pop_free(s->ca_names, X509_NAME_free);\n sk_X509_NAME_pop_free(s->client_ca_names, X509_NAME_free);\n sk_X509_pop_free(s->verified_chain, X509_free);\n if (s->method != NULL)\n s->method->ssl_free(s);\n SSL_CTX_free(s->ctx);\n ASYNC_WAIT_CTX_free(s->waitctx);\n#if !defined(OPENSSL_NO_NEXTPROTONEG)\n OPENSSL_free(s->ext.npn);\n#endif\n#ifndef OPENSSL_NO_SRTP\n sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);\n#endif\n CRYPTO_THREAD_lock_free(s->lock);\n OPENSSL_free(s);\n}', 'void SSL_CTX_free(SSL_CTX *a)\n{\n int i;\n if (a == NULL)\n return;\n CRYPTO_DOWN_REF(&a->references, &i, a->lock);\n REF_PRINT_COUNT("SSL_CTX", a);\n if (i > 0)\n return;\n REF_ASSERT_ISNT(i < 0);\n X509_VERIFY_PARAM_free(a->param);\n dane_ctx_final(&a->dane);\n if (a->sessions != NULL)\n SSL_CTX_flush_sessions(a, 0);\n CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);\n lh_SSL_SESSION_free(a->sessions);\n X509_STORE_free(a->cert_store);\n#ifndef OPENSSL_NO_CT\n CTLOG_STORE_free(a->ctlog_store);\n#endif\n sk_SSL_CIPHER_free(a->cipher_list);\n sk_SSL_CIPHER_free(a->cipher_list_by_id);\n sk_SSL_CIPHER_free(a->tls13_ciphersuites);\n ssl_cert_free(a->cert);\n sk_X509_NAME_pop_free(a->ca_names, X509_NAME_free);\n sk_X509_NAME_pop_free(a->client_ca_names, X509_NAME_free);\n sk_X509_pop_free(a->extra_certs, X509_free);\n a->comp_methods = NULL;\n#ifndef OPENSSL_NO_SRTP\n sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);\n#endif\n#ifndef OPENSSL_NO_SRP\n SSL_CTX_SRP_CTX_free(a);\n#endif\n#ifndef OPENSSL_NO_ENGINE\n ENGINE_finish(a->client_cert_engine);\n#endif\n#ifndef OPENSSL_NO_EC\n OPENSSL_free(a->ext.ecpointformats);\n OPENSSL_free(a->ext.supportedgroups);\n#endif\n OPENSSL_free(a->ext.alpn);\n OPENSSL_secure_free(a->ext.secure);\n CRYPTO_THREAD_lock_free(a->lock);\n OPENSSL_free(a);\n}', 'void SSL_CTX_flush_sessions(SSL_CTX *s, long t)\n{\n unsigned long i;\n TIMEOUT_PARAM tp;\n tp.ctx = s;\n tp.cache = s->sessions;\n if (tp.cache == NULL)\n return;\n tp.time = t;\n CRYPTO_THREAD_write_lock(s->lock);\n i = lh_SSL_SESSION_get_down_load(s->sessions);\n lh_SSL_SESSION_set_down_load(s->sessions, 0);\n lh_SSL_SESSION_doall_TIMEOUT_PARAM(tp.cache, timeout_cb, &tp);\n lh_SSL_SESSION_set_down_load(s->sessions, i);\n CRYPTO_THREAD_unlock(s->lock);\n}', 'IMPLEMENT_LHASH_DOALL_ARG(SSL_SESSION, TIMEOUT_PARAM)', 'void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)\n{\n doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);\n}', 'static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,\n OPENSSL_LH_DOALL_FUNC func,\n OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)\n{\n int i;\n OPENSSL_LH_NODE *a, *n;\n if (lh == NULL)\n return;\n for (i = lh->num_nodes - 1; i >= 0; i--) {\n a = lh->b[i];\n while (a != NULL) {\n n = a->next;\n if (use_arg)\n func_arg(a->data, arg);\n else\n func(a->data);\n a = n;\n }\n }\n}']
|
1,011 | 0 |
https://github.com/libav/libav/blob/8dd25c52cbf57d4a28885abcc950c27a24606036/libavcodec/vp8.c/#L1090
|
static av_always_inline
void vp8_mc(VP8Context *s, int luma,
uint8_t *dst, uint8_t *src, const VP56mv *mv,
int x_off, int y_off, int block_w, int block_h,
int width, int height, int linesize,
vp8_mc_func mc_func[3][3])
{
if (AV_RN32A(mv)) {
static const uint8_t idx[8] = { 0, 1, 2, 1, 2, 1, 2, 1 };
int mx = (mv->x << luma)&7, mx_idx = idx[mx];
int my = (mv->y << luma)&7, my_idx = idx[my];
x_off += mv->x >> (3 - luma);
y_off += mv->y >> (3 - luma);
src += y_off * linesize + x_off;
if (x_off < 2 || x_off >= width - block_w - 3 ||
y_off < 2 || y_off >= height - block_h - 3) {
ff_emulated_edge_mc(s->edge_emu_buffer, src - 2 * linesize - 2, linesize,
block_w + 5, block_h + 5,
x_off - 2, y_off - 2, width, height);
src = s->edge_emu_buffer + 2 + linesize * 2;
}
mc_func[my_idx][mx_idx](dst, linesize, src, linesize, block_h, mx, my);
} else
mc_func[0][0](dst, linesize, src + y_off * linesize + x_off, linesize, block_h, 0, 0);
}
|
['static av_always_inline\nvoid inter_predict(VP8Context *s, uint8_t *dst[3], VP8Macroblock *mb,\n int mb_x, int mb_y)\n{\n int x_off = mb_x << 4, y_off = mb_y << 4;\n int width = 16*s->mb_width, height = 16*s->mb_height;\n AVFrame *ref = s->framep[mb->ref_frame];\n VP56mv *bmv = mb->bmv;\n if (mb->mode < VP8_MVMODE_SPLIT) {\n vp8_mc_part(s, dst, ref, x_off, y_off,\n 0, 0, 16, 16, width, height, &mb->mv);\n } else switch (mb->partitioning) {\n case VP8_SPLITMVMODE_4x4: {\n int x, y;\n VP56mv uvmv;\n for (y = 0; y < 4; y++) {\n for (x = 0; x < 4; x++) {\n vp8_mc(s, 1, dst[0] + 4*y*s->linesize + x*4,\n ref->data[0], &bmv[4*y + x],\n 4*x + x_off, 4*y + y_off, 4, 4,\n width, height, s->linesize,\n s->put_pixels_tab[2]);\n }\n }\n x_off >>= 1; y_off >>= 1; width >>= 1; height >>= 1;\n for (y = 0; y < 2; y++) {\n for (x = 0; x < 2; x++) {\n uvmv.x = mb->bmv[ 2*y * 4 + 2*x ].x +\n mb->bmv[ 2*y * 4 + 2*x+1].x +\n mb->bmv[(2*y+1) * 4 + 2*x ].x +\n mb->bmv[(2*y+1) * 4 + 2*x+1].x;\n uvmv.y = mb->bmv[ 2*y * 4 + 2*x ].y +\n mb->bmv[ 2*y * 4 + 2*x+1].y +\n mb->bmv[(2*y+1) * 4 + 2*x ].y +\n mb->bmv[(2*y+1) * 4 + 2*x+1].y;\n uvmv.x = (uvmv.x + 2 + (uvmv.x >> (INT_BIT-1))) >> 2;\n uvmv.y = (uvmv.y + 2 + (uvmv.y >> (INT_BIT-1))) >> 2;\n if (s->profile == 3) {\n uvmv.x &= ~7;\n uvmv.y &= ~7;\n }\n vp8_mc(s, 0, dst[1] + 4*y*s->uvlinesize + x*4,\n ref->data[1], &uvmv,\n 4*x + x_off, 4*y + y_off, 4, 4,\n width, height, s->uvlinesize,\n s->put_pixels_tab[2]);\n vp8_mc(s, 0, dst[2] + 4*y*s->uvlinesize + x*4,\n ref->data[2], &uvmv,\n 4*x + x_off, 4*y + y_off, 4, 4,\n width, height, s->uvlinesize,\n s->put_pixels_tab[2]);\n }\n }\n break;\n }\n case VP8_SPLITMVMODE_16x8:\n vp8_mc_part(s, dst, ref, x_off, y_off,\n 0, 0, 16, 8, width, height, &bmv[0]);\n vp8_mc_part(s, dst, ref, x_off, y_off,\n 0, 8, 16, 8, width, height, &bmv[1]);\n break;\n case VP8_SPLITMVMODE_8x16:\n vp8_mc_part(s, dst, ref, x_off, y_off,\n 0, 0, 8, 16, width, height, &bmv[0]);\n vp8_mc_part(s, dst, ref, x_off, y_off,\n 8, 0, 8, 16, width, height, &bmv[1]);\n break;\n case VP8_SPLITMVMODE_8x8:\n vp8_mc_part(s, dst, ref, x_off, y_off,\n 0, 0, 8, 8, width, height, &bmv[0]);\n vp8_mc_part(s, dst, ref, x_off, y_off,\n 8, 0, 8, 8, width, height, &bmv[1]);\n vp8_mc_part(s, dst, ref, x_off, y_off,\n 0, 8, 8, 8, width, height, &bmv[2]);\n vp8_mc_part(s, dst, ref, x_off, y_off,\n 8, 8, 8, 8, width, height, &bmv[3]);\n break;\n }\n}', 'static av_always_inline\nvoid vp8_mc(VP8Context *s, int luma,\n uint8_t *dst, uint8_t *src, const VP56mv *mv,\n int x_off, int y_off, int block_w, int block_h,\n int width, int height, int linesize,\n vp8_mc_func mc_func[3][3])\n{\n if (AV_RN32A(mv)) {\n static const uint8_t idx[8] = { 0, 1, 2, 1, 2, 1, 2, 1 };\n int mx = (mv->x << luma)&7, mx_idx = idx[mx];\n int my = (mv->y << luma)&7, my_idx = idx[my];\n x_off += mv->x >> (3 - luma);\n y_off += mv->y >> (3 - luma);\n src += y_off * linesize + x_off;\n if (x_off < 2 || x_off >= width - block_w - 3 ||\n y_off < 2 || y_off >= height - block_h - 3) {\n ff_emulated_edge_mc(s->edge_emu_buffer, src - 2 * linesize - 2, linesize,\n block_w + 5, block_h + 5,\n x_off - 2, y_off - 2, width, height);\n src = s->edge_emu_buffer + 2 + linesize * 2;\n }\n mc_func[my_idx][mx_idx](dst, linesize, src, linesize, block_h, mx, my);\n } else\n mc_func[0][0](dst, linesize, src + y_off * linesize + x_off, linesize, block_h, 0, 0);\n}']
|
1,012 | 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_http_upstream_create_round_robin_peer(ngx_http_request_t *r,\n ngx_http_upstream_resolved_t *ur)\n{\n u_char *p;\n size_t len;\n ngx_uint_t i, n;\n struct sockaddr_in *sin;\n ngx_http_upstream_rr_peers_t *peers;\n ngx_http_upstream_rr_peer_data_t *rrp;\n rrp = r->upstream->peer.data;\n if (rrp == NULL) {\n rrp = ngx_palloc(r->pool, sizeof(ngx_http_upstream_rr_peer_data_t));\n if (rrp == NULL) {\n return NGX_ERROR;\n }\n r->upstream->peer.data = rrp;\n }\n peers = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_rr_peers_t)\n + sizeof(ngx_http_upstream_rr_peer_t) * (ur->naddrs - 1));\n if (peers == NULL) {\n return NGX_ERROR;\n }\n peers->single = (ur->naddrs == 1);\n peers->number = ur->naddrs;\n peers->name = &ur->host;\n if (ur->sockaddr) {\n peers->peer[0].sockaddr = ur->sockaddr;\n peers->peer[0].socklen = ur->socklen;\n peers->peer[0].name = ur->host;\n peers->peer[0].weight = 1;\n peers->peer[0].current_weight = 1;\n peers->peer[0].max_fails = 1;\n peers->peer[0].fail_timeout = 10;\n } else {\n for (i = 0; i < ur->naddrs; i++) {\n len = NGX_INET_ADDRSTRLEN + sizeof(":65536") - 1;\n p = ngx_pnalloc(r->pool, len);\n if (p == NULL) {\n return NGX_ERROR;\n }\n len = ngx_inet_ntop(AF_INET, &ur->addrs[i], p, NGX_INET_ADDRSTRLEN);\n len = ngx_sprintf(&p[len], ":%d", ur->port) - p;\n sin = ngx_pcalloc(r->pool, sizeof(struct sockaddr_in));\n if (sin == NULL) {\n return NGX_ERROR;\n }\n sin->sin_family = AF_INET;\n sin->sin_port = htons(ur->port);\n sin->sin_addr.s_addr = ur->addrs[i];\n peers->peer[i].sockaddr = (struct sockaddr *) sin;\n peers->peer[i].socklen = sizeof(struct sockaddr_in);\n peers->peer[i].name.len = len;\n peers->peer[i].name.data = p;\n peers->peer[i].weight = 1;\n peers->peer[i].current_weight = 1;\n peers->peer[i].max_fails = 1;\n peers->peer[i].fail_timeout = 10;\n }\n }\n rrp->peers = peers;\n rrp->current = 0;\n if (rrp->peers->number <= 8 * sizeof(uintptr_t)) {\n rrp->tried = &rrp->data;\n rrp->data = 0;\n } else {\n n = (rrp->peers->number + (8 * sizeof(uintptr_t) - 1))\n / (8 * sizeof(uintptr_t));\n rrp->tried = ngx_pcalloc(r->pool, n * sizeof(uintptr_t));\n if (rrp->tried == NULL) {\n return NGX_ERROR;\n }\n }\n r->upstream->peer.get = ngx_http_upstream_get_round_robin_peer;\n r->upstream->peer.free = ngx_http_upstream_free_round_robin_peer;\n r->upstream->peer.tries = rrp->peers->number;\n#if (NGX_HTTP_SSL)\n r->upstream->peer.set_session =\n ngx_http_upstream_set_round_robin_peer_session;\n r->upstream->peer.save_session =\n ngx_http_upstream_save_round_robin_peer_session;\n#endif\n return NGX_OK;\n}', 'size_t\nngx_inet_ntop(int family, void *addr, u_char *text, size_t len)\n{\n u_char *p;\n switch (family) {\n case AF_INET:\n p = addr;\n return ngx_snprintf(text, len, "%ud.%ud.%ud.%ud",\n p[0], p[1], p[2], p[3])\n - text;\n#if (NGX_HAVE_INET6)\n case AF_INET6:\n return ngx_inet6_ntop(addr, text, len);\n#endif\n default:\n return 0;\n }\n}', 'u_char * ngx_cdecl\nngx_sprintf(u_char *buf, const char *fmt, ...)\n{\n u_char *p;\n va_list args;\n va_start(args, fmt);\n p = ngx_vsnprintf(buf, 65536, 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}"]
|
1,013 | 0 |
https://github.com/libav/libav/blob/fd7f59639c43f0ab6b83ad2c1ceccafc553d7845/ffmpeg.c/#L2739
|
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) {
av_log(NULL, AV_LOG_ERROR, "Unknown %s '%s'\n", codec_string, name);
av_exit(1);
}
if(codec->type != type) {
av_log(NULL, AV_LOG_ERROR, "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 av_log(NULL, AV_LOG_ERROR, "Unknown %s \'%s\'\\n", codec_string, name);\n av_exit(1);\n }\n if(codec->type != type) {\n av_log(NULL, AV_LOG_ERROR, "Invalid %s type \'%s\'\\n", codec_string, name);\n av_exit(1);\n }\n return codec->id;\n}']
|
1,014 | 0 |
https://github.com/libav/libav/blob/6a5b8ca4329039fad44ad50b6496948f4bfacb4c/libavcodec/aacdec.c/#L2746
|
static int aac_decode_er_frame(AVCodecContext *avctx, void *data,
int *got_frame_ptr, GetBitContext *gb)
{
AACContext *ac = avctx->priv_data;
const MPEG4AudioConfig *const m4ac = &ac->oc[1].m4ac;
ChannelElement *che;
int err, i;
int samples = m4ac->frame_length_short ? 960 : 1024;
int chan_config = m4ac->chan_config;
int aot = m4ac->object_type;
if (aot == AOT_ER_AAC_LD || aot == AOT_ER_AAC_ELD)
samples >>= 1;
ac->frame = data;
if ((err = frame_configure_elements(avctx)) < 0)
return err;
ac->avctx->profile = aot - 1;
ac->tags_mapped = 0;
if (chan_config < 0 || chan_config >= 8) {
avpriv_request_sample(avctx, "Unknown ER channel configuration %d",
chan_config);
return AVERROR_INVALIDDATA;
}
for (i = 0; i < tags_per_config[chan_config]; i++) {
const int elem_type = aac_channel_layout_map[chan_config-1][i][0];
const int elem_id = aac_channel_layout_map[chan_config-1][i][1];
if (!(che=get_che(ac, elem_type, elem_id))) {
av_log(ac->avctx, AV_LOG_ERROR,
"channel element %d.%d is not allocated\n",
elem_type, elem_id);
return AVERROR_INVALIDDATA;
}
if (aot != AOT_ER_AAC_ELD)
skip_bits(gb, 4);
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_LFE:
err = decode_ics(ac, &che->ch[0], gb, 0, 0);
break;
}
if (err < 0)
return err;
}
spectral_to_sample(ac);
ac->frame->nb_samples = samples;
ac->frame->sample_rate = avctx->sample_rate;
*got_frame_ptr = 1;
skip_bits_long(gb, get_bits_left(gb));
return 0;
}
|
['static int aac_decode_er_frame(AVCodecContext *avctx, void *data,\n int *got_frame_ptr, GetBitContext *gb)\n{\n AACContext *ac = avctx->priv_data;\n const MPEG4AudioConfig *const m4ac = &ac->oc[1].m4ac;\n ChannelElement *che;\n int err, i;\n int samples = m4ac->frame_length_short ? 960 : 1024;\n int chan_config = m4ac->chan_config;\n int aot = m4ac->object_type;\n if (aot == AOT_ER_AAC_LD || aot == AOT_ER_AAC_ELD)\n samples >>= 1;\n ac->frame = data;\n if ((err = frame_configure_elements(avctx)) < 0)\n return err;\n ac->avctx->profile = aot - 1;\n ac->tags_mapped = 0;\n if (chan_config < 0 || chan_config >= 8) {\n avpriv_request_sample(avctx, "Unknown ER channel configuration %d",\n chan_config);\n return AVERROR_INVALIDDATA;\n }\n for (i = 0; i < tags_per_config[chan_config]; i++) {\n const int elem_type = aac_channel_layout_map[chan_config-1][i][0];\n const int elem_id = aac_channel_layout_map[chan_config-1][i][1];\n if (!(che=get_che(ac, elem_type, elem_id))) {\n av_log(ac->avctx, AV_LOG_ERROR,\n "channel element %d.%d is not allocated\\n",\n elem_type, elem_id);\n return AVERROR_INVALIDDATA;\n }\n if (aot != AOT_ER_AAC_ELD)\n skip_bits(gb, 4);\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_LFE:\n err = decode_ics(ac, &che->ch[0], gb, 0, 0);\n break;\n }\n if (err < 0)\n return err;\n }\n spectral_to_sample(ac);\n ac->frame->nb_samples = samples;\n ac->frame->sample_rate = avctx->sample_rate;\n *got_frame_ptr = 1;\n skip_bits_long(gb, get_bits_left(gb));\n return 0;\n}']
|
1,015 | 0 |
https://github.com/openssl/openssl/blob/ed371b8cbac0d0349667558c061c1ae380cf75eb/crypto/bn/bn_lib.c/#L291
|
BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
{
bn_check_top(b);
if (a == b)
return a;
if (bn_wexpand(a, b->top) == NULL)
return NULL;
if (b->top > 0)
memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);
a->neg = b->neg;
a->top = b->top;
a->flags |= b->flags & BN_FLG_FIXED_TOP;
bn_check_top(a);
return a;
}
|
['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_is_odd(const BIGNUM *a)\n{\n return (a->top > 0) && (a->d[0] & 1);\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int ret;\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return 0;\n }\n if (divisor->d[divisor->top - 1] == 0) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n ret = bn_div_fixed_top(dv, rm, num, divisor, ctx);\n if (ret) {\n if (dv != NULL)\n bn_correct_top(dv);\n if (rm != NULL)\n bn_correct_top(rm);\n }\n return ret;\n}', 'int BN_is_zero(const BIGNUM *a)\n{\n return a->top == 0;\n}', 'int bn_div_fixed_top(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,\n const BIGNUM *divisor, BN_CTX *ctx)\n{\n int norm_shift, i, j, loop;\n BIGNUM *tmp, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnum, *wnumtop;\n BN_ULONG d0, d1;\n int num_n, div_n;\n assert(divisor->top > 0 && divisor->d[divisor->top - 1] != 0);\n bn_check_top(num);\n bn_check_top(divisor);\n bn_check_top(dv);\n bn_check_top(rm);\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n if (!BN_copy(sdiv, divisor))\n goto err;\n norm_shift = bn_left_align(sdiv);\n sdiv->neg = 0;\n if (!(bn_lshift_fixed_top(snum, num, norm_shift)))\n goto err;\n div_n = sdiv->top;\n num_n = snum->top;\n if (num_n <= div_n) {\n if (bn_wexpand(snum, div_n + 1) == NULL)\n goto err;\n memset(&(snum->d[num_n]), 0, (div_n - num_n + 1) * sizeof(BN_ULONG));\n snum->top = num_n = div_n + 1;\n }\n loop = num_n - div_n;\n wnum = &(snum->d[loop]);\n wnumtop = &(snum->d[num_n - 1]);\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n if (!bn_wexpand(res, loop))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop;\n res->flags |= BN_FLG_FIXED_TOP;\n resp = &(res->d[loop]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n for (i = 0; i < loop; i++, wnumtop--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W)\n q = bn_div_3_words(wnumtop, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnumtop[0];\n n1 = wnumtop[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n BN_ULONG n2 = (wnumtop == wnum) ? 0 : wnumtop[-2];\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | n2))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= n2)))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum--;\n l0 = bn_sub_words(wnum, wnum, tmp->d, div_n + 1);\n q -= l0;\n for (l0 = 0 - l0, j = 0; j < div_n; j++)\n tmp->d[j] = sdiv->d[j] & l0;\n l0 = bn_add_words(wnum, wnum, tmp->d, div_n);\n (*wnumtop) += l0;\n assert((*wnumtop) == 0);\n *--resp = q;\n }\n snum->neg = num->neg;\n snum->top = div_n;\n snum->flags |= BN_FLG_FIXED_TOP;\n if (rm != NULL)\n bn_rshift_fixed_top(rm, snum, norm_shift);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return 0;\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n bn_check_top(b);\n if (a == b)\n return a;\n if (bn_wexpand(a, b->top) == NULL)\n return NULL;\n if (b->top > 0)\n memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n a->neg = b->neg;\n a->top = b->top;\n a->flags |= b->flags & BN_FLG_FIXED_TOP;\n bn_check_top(a);\n return a;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']
|
1,016 | 0 |
https://github.com/openssl/openssl/blob/27a3d9f9aa1ca6137ffd13a23775709c6f1ef567/crypto/mem_dbg.c/#L692
|
static void print_leak(const MEM *m, MEM_LEAK *l)
{
char buf[1024];
char *bufp = buf;
APP_INFO *amip;
int ami_cnt;
struct tm *lcl = NULL;
unsigned long ti;
void *tip;
#define BUF_REMAIN (sizeof buf - (size_t)(bufp - buf))
if(m->addr == (char *)l->bio)
return;
if (options & V_CRYPTO_MDEBUG_TIME)
{
lcl = localtime(&m->time);
BIO_snprintf(bufp, BUF_REMAIN, "[%02d:%02d:%02d] ",
lcl->tm_hour,lcl->tm_min,lcl->tm_sec);
bufp += strlen(bufp);
}
BIO_snprintf(bufp, BUF_REMAIN, "%5lu file=%s, line=%d, ",
m->order,m->file,m->line);
bufp += strlen(bufp);
if (options & V_CRYPTO_MDEBUG_THREAD)
{
BIO_snprintf(bufp, BUF_REMAIN, "thread=%lu/%p, ", m->thread_id, m->thread_idptr);
bufp += strlen(bufp);
}
BIO_snprintf(bufp, BUF_REMAIN, "number=%d, address=%08lX\n",
m->num,(unsigned long)m->addr);
bufp += strlen(bufp);
BIO_puts(l->bio,buf);
l->chunks++;
l->bytes+=m->num;
amip=m->app_info;
ami_cnt=0;
if (!amip)
return;
ti=amip->thread_id;
tip=amip->thread_idptr;
do
{
int buf_len;
int info_len;
ami_cnt++;
memset(buf,'>',ami_cnt);
BIO_snprintf(buf + ami_cnt, sizeof buf - ami_cnt,
" thread=%lu/%p, file=%s, line=%d, info=\"",
amip->thread_id, amip->thread_idptr, amip->file, amip->line);
buf_len=strlen(buf);
info_len=strlen(amip->info);
if (128 - buf_len - 3 < info_len)
{
memcpy(buf + buf_len, amip->info, 128 - buf_len - 3);
buf_len = 128 - 3;
}
else
{
BUF_strlcpy(buf + buf_len, amip->info,
sizeof buf - buf_len);
buf_len = strlen(buf);
}
BIO_snprintf(buf + buf_len, sizeof buf - buf_len, "\"\n");
BIO_puts(l->bio,buf);
amip = amip->next;
}
while(amip && amip->thread_id == ti && amip->thread_idptr == tip);
#ifdef LEVITTE_DEBUG_MEM
if (amip)
{
fprintf(stderr, "Thread switch detected in backtrace!!!!\n");
abort();
}
#endif
}
|
['static void print_leak(const MEM *m, MEM_LEAK *l)\n\t{\n\tchar buf[1024];\n\tchar *bufp = buf;\n\tAPP_INFO *amip;\n\tint ami_cnt;\n\tstruct tm *lcl = NULL;\n\tunsigned long ti;\n\tvoid *tip;\n#define BUF_REMAIN (sizeof buf - (size_t)(bufp - buf))\n\tif(m->addr == (char *)l->bio)\n\t return;\n\tif (options & V_CRYPTO_MDEBUG_TIME)\n\t\t{\n\t\tlcl = localtime(&m->time);\n\t\tBIO_snprintf(bufp, BUF_REMAIN, "[%02d:%02d:%02d] ",\n\t\t\tlcl->tm_hour,lcl->tm_min,lcl->tm_sec);\n\t\tbufp += strlen(bufp);\n\t\t}\n\tBIO_snprintf(bufp, BUF_REMAIN, "%5lu file=%s, line=%d, ",\n\t\tm->order,m->file,m->line);\n\tbufp += strlen(bufp);\n\tif (options & V_CRYPTO_MDEBUG_THREAD)\n\t\t{\n\t\tBIO_snprintf(bufp, BUF_REMAIN, "thread=%lu/%p, ", m->thread_id, m->thread_idptr);\n\t\tbufp += strlen(bufp);\n\t\t}\n\tBIO_snprintf(bufp, BUF_REMAIN, "number=%d, address=%08lX\\n",\n\t\tm->num,(unsigned long)m->addr);\n\tbufp += strlen(bufp);\n\tBIO_puts(l->bio,buf);\n\tl->chunks++;\n\tl->bytes+=m->num;\n\tamip=m->app_info;\n\tami_cnt=0;\n\tif (!amip)\n\t\treturn;\n\tti=amip->thread_id;\n\ttip=amip->thread_idptr;\n\tdo\n\t\t{\n\t\tint buf_len;\n\t\tint info_len;\n\t\tami_cnt++;\n\t\tmemset(buf,\'>\',ami_cnt);\n\t\tBIO_snprintf(buf + ami_cnt, sizeof buf - ami_cnt,\n\t\t\t" thread=%lu/%p, file=%s, line=%d, info=\\"",\n\t\t\tamip->thread_id, amip->thread_idptr, amip->file, amip->line);\n\t\tbuf_len=strlen(buf);\n\t\tinfo_len=strlen(amip->info);\n\t\tif (128 - buf_len - 3 < info_len)\n\t\t\t{\n\t\t\tmemcpy(buf + buf_len, amip->info, 128 - buf_len - 3);\n\t\t\tbuf_len = 128 - 3;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tBUF_strlcpy(buf + buf_len, amip->info,\n\t\t\t\t sizeof buf - buf_len);\n\t\t\tbuf_len = strlen(buf);\n\t\t\t}\n\t\tBIO_snprintf(buf + buf_len, sizeof buf - buf_len, "\\"\\n");\n\t\tBIO_puts(l->bio,buf);\n\t\tamip = amip->next;\n\t\t}\n\twhile(amip && amip->thread_id == ti && amip->thread_idptr == tip);\n#ifdef LEVITTE_DEBUG_MEM\n\tif (amip)\n\t\t{\n\t\tfprintf(stderr, "Thread switch detected in backtrace!!!!\\n");\n\t\tabort();\n\t\t}\n#endif\n\t}']
|
1,017 | 0 |
https://github.com/openssl/openssl/blob/0350ef69add8758dd180e73cbc7c1961bf64e503/crypto/bn/bn_ctx.c/#L353
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['static EVP_PKEY *b2i_dss(const unsigned char **in, unsigned int length,\n unsigned int bitlen, int ispub)\n{\n const unsigned char *p = *in;\n EVP_PKEY *ret = NULL;\n DSA *dsa = NULL;\n BN_CTX *ctx = NULL;\n unsigned int nbyte;\n nbyte = (bitlen + 7) >> 3;\n dsa = DSA_new();\n ret = EVP_PKEY_new();\n if (!dsa || !ret)\n goto memerr;\n if (!read_lebn(&p, nbyte, &dsa->p))\n goto memerr;\n if (!read_lebn(&p, 20, &dsa->q))\n goto memerr;\n if (!read_lebn(&p, nbyte, &dsa->g))\n goto memerr;\n if (ispub) {\n if (!read_lebn(&p, nbyte, &dsa->pub_key))\n goto memerr;\n } else {\n if (!read_lebn(&p, 20, &dsa->priv_key))\n goto memerr;\n if (!(dsa->pub_key = BN_new()))\n goto memerr;\n if (!(ctx = BN_CTX_new()))\n goto memerr;\n if (!BN_mod_exp(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx))\n goto memerr;\n BN_CTX_free(ctx);\n }\n EVP_PKEY_set1_DSA(ret, dsa);\n DSA_free(dsa);\n *in = p;\n return ret;\n memerr:\n PEMerr(PEM_F_B2I_DSS, ERR_R_MALLOC_FAILURE);\n if (dsa)\n DSA_free(dsa);\n if (ret)\n EVP_PKEY_free(ret);\n if (ctx)\n BN_CTX_free(ctx);\n return NULL;\n}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n BN_CTX *ctx)\n{\n int ret;\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n if (BN_is_odd(m)) {\n# ifdef MONT_EXP_WORD\n if (a->top == 1 && !a->neg\n && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)) {\n BN_ULONG A = a->d[0];\n ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);\n } else\n# endif\n ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);\n } else\n#endif\n#ifdef RECP_MUL_MOD\n {\n ret = BN_mod_exp_recp(r, a, p, m, ctx);\n }\n#else\n {\n ret = BN_mod_exp_simple(r, a, p, m, ctx);\n }\n#endif\n bn_check_top(r);\n return (ret);\n}', 'int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx)\n{\n int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n int start = 1;\n BIGNUM *aa;\n BIGNUM *val[TABLE_SIZE];\n BN_RECP_CTX recp;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0) {\n BNerr(BN_F_BN_MOD_EXP_RECP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return -1;\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n ret = BN_one(r);\n return ret;\n }\n BN_CTX_start(ctx);\n aa = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (!aa || !val[0])\n goto err;\n BN_RECP_CTX_init(&recp);\n if (m->neg) {\n if (!BN_copy(aa, m))\n goto err;\n aa->neg = 0;\n if (BN_RECP_CTX_set(&recp, aa, ctx) <= 0)\n goto err;\n } else {\n if (BN_RECP_CTX_set(&recp, m, ctx) <= 0)\n goto err;\n }\n if (!BN_nnmod(val[0], a, m, ctx))\n goto err;\n if (BN_is_zero(val[0])) {\n BN_zero(r);\n ret = 1;\n goto err;\n }\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!BN_mod_mul_reciprocal(aa, val[0], val[0], &recp, ctx))\n goto err;\n j = 1 << (window - 1);\n for (i = 1; i < j; i++) {\n if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul_reciprocal(val[i], val[i - 1], aa, &recp, ctx))\n goto err;\n }\n }\n start = 1;\n wvalue = 0;\n wstart = bits - 1;\n wend = 0;\n if (!BN_one(r))\n goto err;\n for (;;) {\n if (BN_is_bit_set(p, wstart) == 0) {\n if (!start)\n if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))\n goto err;\n if (wstart == 0)\n break;\n wstart--;\n continue;\n }\n j = wstart;\n wvalue = 1;\n wend = 0;\n for (i = 1; i < window; i++) {\n if (wstart - i < 0)\n break;\n if (BN_is_bit_set(p, wstart - i)) {\n wvalue <<= (i - wend);\n wvalue |= 1;\n wend = i;\n }\n }\n j = wend + 1;\n if (!start)\n for (i = 0; i < j; i++) {\n if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))\n goto err;\n }\n if (!BN_mod_mul_reciprocal(r, r, val[wvalue >> 1], &recp, ctx))\n goto err;\n wstart -= wend + 1;\n wvalue = 0;\n start = 0;\n if (wstart < 0)\n break;\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_RECP_CTX_free(&recp);\n bn_check_top(r);\n return (ret);\n}', '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 tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (dv == NULL)\n res = BN_CTX_get(ctx);\n else\n res = dv;\n if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n res->neg = (num->neg ^ divisor->neg);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--, resp--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# ifdef BN_DEBUG_LEVITTE\n fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n", n0, n1, d0, q);\n# endif\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifdef BN_DEBUG_LEVITTE\n fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n", n0, n1, d0, q);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return (1);\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return (0);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
|
1,018 | 0 |
https://github.com/libav/libav/blob/1c3e117e0bd73ffc5a3abeb35b521fd048988f06/ffmpeg.c/#L3684
|
static int opt_streamid(const char *opt, const char *arg)
{
int idx;
char *p;
char idx_str[16];
strncpy(idx_str, arg, sizeof(idx_str));
idx_str[sizeof(idx_str)-1] = '\0';
p = strchr(idx_str, ':');
if (!p) {
fprintf(stderr,
"Invalid value '%s' for option '%s', required syntax is 'index:value'\n",
arg, opt);
ffmpeg_exit(1);
}
*p++ = '\0';
idx = parse_number_or_die(opt, idx_str, OPT_INT, 0, MAX_STREAMS-1);
streamid_map = grow_array(streamid_map, sizeof(*streamid_map), &nb_streamid_map, idx+1);
streamid_map[idx] = parse_number_or_die(opt, p, OPT_INT, 0, INT_MAX);
return 0;
}
|
['static int opt_streamid(const char *opt, const char *arg)\n{\n int idx;\n char *p;\n char idx_str[16];\n strncpy(idx_str, arg, sizeof(idx_str));\n idx_str[sizeof(idx_str)-1] = \'\\0\';\n p = strchr(idx_str, \':\');\n if (!p) {\n fprintf(stderr,\n "Invalid value \'%s\' for option \'%s\', required syntax is \'index:value\'\\n",\n arg, opt);\n ffmpeg_exit(1);\n }\n *p++ = \'\\0\';\n idx = parse_number_or_die(opt, idx_str, OPT_INT, 0, MAX_STREAMS-1);\n streamid_map = grow_array(streamid_map, sizeof(*streamid_map), &nb_streamid_map, idx+1);\n streamid_map[idx] = parse_number_or_die(opt, p, OPT_INT, 0, INT_MAX);\n return 0;\n}']
|
1,019 | 0 |
https://github.com/libav/libav/blob/e652cc9606068189cb512a36f0335a5cf2ecf287/avconv.c/#L3067
|
static int copy_metadata(char *outspec, char *inspec, AVFormatContext *oc, AVFormatContext *ic, OptionsContext *o)
{
AVDictionary **meta_in = NULL;
AVDictionary **meta_out;
int i, ret = 0;
char type_in, type_out;
const char *istream_spec = NULL, *ostream_spec = NULL;
int idx_in = 0, idx_out = 0;
parse_meta_type(inspec, &type_in, &idx_in, &istream_spec);
parse_meta_type(outspec, &type_out, &idx_out, &ostream_spec);
if (type_in == 'g' || type_out == 'g')
o->metadata_global_manual = 1;
if (type_in == 's' || type_out == 's')
o->metadata_streams_manual = 1;
if (type_in == 'c' || type_out == 'c')
o->metadata_chapters_manual = 1;
#define METADATA_CHECK_INDEX(index, nb_elems, desc)\
if ((index) < 0 || (index) >= (nb_elems)) {\
av_log(NULL, AV_LOG_FATAL, "Invalid %s index %d while processing metadata maps.\n",\
(desc), (index));\
exit_program(1);\
}
#define SET_DICT(type, meta, context, index)\
switch (type) {\
case 'g':\
meta = &context->metadata;\
break;\
case 'c':\
METADATA_CHECK_INDEX(index, context->nb_chapters, "chapter")\
meta = &context->chapters[index]->metadata;\
break;\
case 'p':\
METADATA_CHECK_INDEX(index, context->nb_programs, "program")\
meta = &context->programs[index]->metadata;\
break;\
}\
SET_DICT(type_in, meta_in, ic, idx_in);
SET_DICT(type_out, meta_out, oc, idx_out);
if (type_in == 's') {
for (i = 0; i < ic->nb_streams; i++) {
if ((ret = check_stream_specifier(ic, ic->streams[i], istream_spec)) > 0) {
meta_in = &ic->streams[i]->metadata;
break;
} else if (ret < 0)
exit_program(1);
}
if (!meta_in) {
av_log(NULL, AV_LOG_FATAL, "Stream specifier %s does not match any streams.\n", istream_spec);
exit_program(1);
}
}
if (type_out == 's') {
for (i = 0; i < oc->nb_streams; i++) {
if ((ret = check_stream_specifier(oc, oc->streams[i], ostream_spec)) > 0) {
meta_out = &oc->streams[i]->metadata;
av_dict_copy(meta_out, *meta_in, AV_DICT_DONT_OVERWRITE);
} else if (ret < 0)
exit_program(1);
}
} else
av_dict_copy(meta_out, *meta_in, AV_DICT_DONT_OVERWRITE);
return 0;
}
|
['static int copy_metadata(char *outspec, char *inspec, AVFormatContext *oc, AVFormatContext *ic, OptionsContext *o)\n{\n AVDictionary **meta_in = NULL;\n AVDictionary **meta_out;\n int i, ret = 0;\n char type_in, type_out;\n const char *istream_spec = NULL, *ostream_spec = NULL;\n int idx_in = 0, idx_out = 0;\n parse_meta_type(inspec, &type_in, &idx_in, &istream_spec);\n parse_meta_type(outspec, &type_out, &idx_out, &ostream_spec);\n if (type_in == \'g\' || type_out == \'g\')\n o->metadata_global_manual = 1;\n if (type_in == \'s\' || type_out == \'s\')\n o->metadata_streams_manual = 1;\n if (type_in == \'c\' || type_out == \'c\')\n o->metadata_chapters_manual = 1;\n#define METADATA_CHECK_INDEX(index, nb_elems, desc)\\\n if ((index) < 0 || (index) >= (nb_elems)) {\\\n av_log(NULL, AV_LOG_FATAL, "Invalid %s index %d while processing metadata maps.\\n",\\\n (desc), (index));\\\n exit_program(1);\\\n }\n#define SET_DICT(type, meta, context, index)\\\n switch (type) {\\\n case \'g\':\\\n meta = &context->metadata;\\\n break;\\\n case \'c\':\\\n METADATA_CHECK_INDEX(index, context->nb_chapters, "chapter")\\\n meta = &context->chapters[index]->metadata;\\\n break;\\\n case \'p\':\\\n METADATA_CHECK_INDEX(index, context->nb_programs, "program")\\\n meta = &context->programs[index]->metadata;\\\n break;\\\n }\\\n SET_DICT(type_in, meta_in, ic, idx_in);\n SET_DICT(type_out, meta_out, oc, idx_out);\n if (type_in == \'s\') {\n for (i = 0; i < ic->nb_streams; i++) {\n if ((ret = check_stream_specifier(ic, ic->streams[i], istream_spec)) > 0) {\n meta_in = &ic->streams[i]->metadata;\n break;\n } else if (ret < 0)\n exit_program(1);\n }\n if (!meta_in) {\n av_log(NULL, AV_LOG_FATAL, "Stream specifier %s does not match any streams.\\n", istream_spec);\n exit_program(1);\n }\n }\n if (type_out == \'s\') {\n for (i = 0; i < oc->nb_streams; i++) {\n if ((ret = check_stream_specifier(oc, oc->streams[i], ostream_spec)) > 0) {\n meta_out = &oc->streams[i]->metadata;\n av_dict_copy(meta_out, *meta_in, AV_DICT_DONT_OVERWRITE);\n } else if (ret < 0)\n exit_program(1);\n }\n } else\n av_dict_copy(meta_out, *meta_in, AV_DICT_DONT_OVERWRITE);\n return 0;\n}', 'static void parse_meta_type(char *arg, char *type, int *index, const char **stream_spec)\n{\n if (*arg) {\n *type = *arg;\n switch (*arg) {\n case \'g\':\n break;\n case \'s\':\n if (*(++arg) && *arg != \':\') {\n av_log(NULL, AV_LOG_FATAL, "Invalid metadata specifier %s.\\n", arg);\n exit_program(1);\n }\n *stream_spec = *arg == \':\' ? arg + 1 : "";\n break;\n case \'c\':\n case \'p\':\n if (*(++arg) == \':\')\n *index = strtol(++arg, NULL, 0);\n break;\n default:\n av_log(NULL, AV_LOG_FATAL, "Invalid metadata type %c.\\n", *arg);\n exit_program(1);\n }\n } else\n *type = \'g\';\n}']
|
1,020 | 0 |
https://github.com/libav/libav/blob/dccbd97d72991f4df63542e1ee03db2f8d7a0238/libavcodec/alacenc.c/#L488
|
static int alac_encode_frame(AVCodecContext *avctx, uint8_t *frame,
int buf_size, void *data)
{
AlacEncodeContext *s = avctx->priv_data;
PutBitContext *pb = &s->pbctx;
int i, out_bytes, verbatim_flag = 0;
if(avctx->frame_size > DEFAULT_FRAME_SIZE) {
av_log(avctx, AV_LOG_ERROR, "input frame size exceeded\n");
return -1;
}
if(buf_size < 2*s->max_coded_frame_size) {
av_log(avctx, AV_LOG_ERROR, "buffer size is too small\n");
return -1;
}
verbatim:
init_put_bits(pb, frame, buf_size);
if((s->compression_level == 0) || verbatim_flag) {
const int16_t *samples = data;
write_frame_header(s, 1);
for(i=0; i<avctx->frame_size*avctx->channels; i++) {
put_sbits(pb, 16, *samples++);
}
} else {
init_sample_buffers(s, data);
write_frame_header(s, 0);
write_compressed_frame(s);
}
put_bits(pb, 3, 7);
flush_put_bits(pb);
out_bytes = put_bits_count(pb) >> 3;
if(out_bytes > s->max_coded_frame_size) {
if(verbatim_flag || (s->compression_level == 0)) {
av_log(avctx, AV_LOG_ERROR, "error encoding frame\n");
return -1;
}
verbatim_flag = 1;
goto verbatim;
}
return out_bytes;
}
|
['static int alac_encode_frame(AVCodecContext *avctx, uint8_t *frame,\n int buf_size, void *data)\n{\n AlacEncodeContext *s = avctx->priv_data;\n PutBitContext *pb = &s->pbctx;\n int i, out_bytes, verbatim_flag = 0;\n if(avctx->frame_size > DEFAULT_FRAME_SIZE) {\n av_log(avctx, AV_LOG_ERROR, "input frame size exceeded\\n");\n return -1;\n }\n if(buf_size < 2*s->max_coded_frame_size) {\n av_log(avctx, AV_LOG_ERROR, "buffer size is too small\\n");\n return -1;\n }\nverbatim:\n init_put_bits(pb, frame, buf_size);\n if((s->compression_level == 0) || verbatim_flag) {\n const int16_t *samples = data;\n write_frame_header(s, 1);\n for(i=0; i<avctx->frame_size*avctx->channels; i++) {\n put_sbits(pb, 16, *samples++);\n }\n } else {\n init_sample_buffers(s, data);\n write_frame_header(s, 0);\n write_compressed_frame(s);\n }\n put_bits(pb, 3, 7);\n flush_put_bits(pb);\n out_bytes = put_bits_count(pb) >> 3;\n if(out_bytes > s->max_coded_frame_size) {\n if(verbatim_flag || (s->compression_level == 0)) {\n av_log(avctx, AV_LOG_ERROR, "error encoding frame\\n");\n return -1;\n }\n verbatim_flag = 1;\n goto verbatim;\n }\n return out_bytes;\n}', 'static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)\n{\n if(buffer_size < 0) {\n buffer_size = 0;\n buffer = NULL;\n }\n s->size_in_bits= 8*buffer_size;\n s->buf = buffer;\n s->buf_end = s->buf + buffer_size;\n#ifdef ALT_BITSTREAM_WRITER\n s->index=0;\n ((uint32_t*)(s->buf))[0]=0;\n#else\n s->buf_ptr = s->buf;\n s->bit_left=32;\n s->bit_buf=0;\n#endif\n}', 'static void write_frame_header(AlacEncodeContext *s, int is_verbatim)\n{\n put_bits(&s->pbctx, 3, s->avctx->channels-1);\n put_bits(&s->pbctx, 16, 0);\n put_bits(&s->pbctx, 1, 1);\n put_bits(&s->pbctx, 2, 0);\n put_bits(&s->pbctx, 1, is_verbatim);\n put_bits32(&s->pbctx, s->avctx->frame_size);\n}', 'static inline void put_bits(PutBitContext *s, int n, unsigned int value)\n#ifndef ALT_BITSTREAM_WRITER\n{\n unsigned int bit_buf;\n int bit_left;\n assert(n <= 31 && value < (1U << n));\n bit_buf = s->bit_buf;\n bit_left = s->bit_left;\n#ifdef BITSTREAM_WRITER_LE\n bit_buf |= value << (32 - bit_left);\n if (n >= bit_left) {\n#if !HAVE_FAST_UNALIGNED\n if (3 & (intptr_t) s->buf_ptr) {\n AV_WL32(s->buf_ptr, bit_buf);\n } else\n#endif\n *(uint32_t *)s->buf_ptr = av_le2ne32(bit_buf);\n s->buf_ptr+=4;\n bit_buf = (bit_left==32)?0:value >> bit_left;\n bit_left+=32;\n }\n bit_left-=n;\n#else\n if (n < bit_left) {\n bit_buf = (bit_buf<<n) | value;\n bit_left-=n;\n } else {\n bit_buf<<=bit_left;\n bit_buf |= value >> (n - bit_left);\n#if !HAVE_FAST_UNALIGNED\n if (3 & (intptr_t) s->buf_ptr) {\n AV_WB32(s->buf_ptr, bit_buf);\n } else\n#endif\n *(uint32_t *)s->buf_ptr = av_be2ne32(bit_buf);\n s->buf_ptr+=4;\n bit_left+=32 - n;\n bit_buf = value;\n }\n#endif\n s->bit_buf = bit_buf;\n s->bit_left = bit_left;\n}', 'static av_always_inline av_const uint32_t av_bswap32(uint32_t x)\n{\n x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);\n x= (x>>16) | (x<<16);\n return x;\n}', 'static void av_unused put_bits32(PutBitContext *s, uint32_t value)\n{\n int lo = value & 0xffff;\n int hi = value >> 16;\n#ifdef BITSTREAM_WRITER_LE\n put_bits(s, 16, lo);\n put_bits(s, 16, hi);\n#else\n put_bits(s, 16, hi);\n put_bits(s, 16, lo);\n#endif\n}']
|
1,021 | 0 |
https://github.com/openssl/openssl/blob/a8140a42f5ee9e4e1423b5b6b319dc4657659f6f/crypto/bn/bn_lib.c/#L232
|
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
BN_ULONG *a = NULL;
if (words > (INT_MAX / (4 * BN_BITS2))) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
return NULL;
}
if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
return NULL;
}
if (BN_get_flags(b, BN_FLG_SECURE))
a = OPENSSL_secure_zalloc(words * sizeof(*a));
else
a = OPENSSL_zalloc(words * sizeof(*a));
if (a == NULL) {
BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
return NULL;
}
assert(b->top <= words);
if (b->top > 0)
memcpy(a, b->d, sizeof(*a) * b->top);
return a;
}
|
['static int test_rand(void)\n{\n BIGNUM *bn = NULL;\n int st = 0;\n if (!TEST_ptr(bn = BN_new()))\n return 0;\n if (!TEST_false(BN_rand(bn, 0, 0 , 0 ))\n || !TEST_false(BN_rand(bn, 0, 1 , 1 ))\n || !TEST_true(BN_rand(bn, 1, 0 , 0 ))\n || !TEST_BN_eq_one(bn)\n || !TEST_false(BN_rand(bn, 1, 1 , 0 ))\n || !TEST_true(BN_rand(bn, 1, -1 , 1 ))\n || !TEST_BN_eq_one(bn)\n || !TEST_true(BN_rand(bn, 2, 1 , 0 ))\n || !TEST_BN_eq_word(bn, 3))\n goto err;\n st = 1;\n err:\n BN_free(bn);\n return st;\n}', 'int BN_rand(BIGNUM *rnd, int bits, int top, int bottom)\n{\n return bnrand(NORMAL, rnd, bits, top, bottom);\n}', 'static int bnrand(BNRAND_FLAG flag, BIGNUM *rnd, int bits, int top, int bottom)\n{\n unsigned char *buf = NULL;\n int b, ret = 0, bit, bytes, mask;\n if (bits == 0) {\n if (top != BN_RAND_TOP_ANY || bottom != BN_RAND_BOTTOM_ANY)\n goto toosmall;\n BN_zero(rnd);\n return 1;\n }\n if (bits < 0 || (bits == 1 && top > 0))\n goto toosmall;\n bytes = (bits + 7) / 8;\n bit = (bits - 1) % 8;\n mask = 0xff << (bit + 1);\n buf = OPENSSL_malloc(bytes);\n if (buf == NULL) {\n BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n#if defined(FIPS_MODE)\n BNerr(BN_F_BNRAND, ERR_R_INTERNAL_ERROR);\n goto err;\n#else\n b = flag == NORMAL ? RAND_bytes(buf, bytes) : RAND_priv_bytes(buf, bytes);\n#endif\n if (b <= 0)\n goto err;\n if (flag == TESTING) {\n int i;\n unsigned char c;\n for (i = 0; i < bytes; i++) {\n#if !defined(FIPS_MODE)\n if (RAND_bytes(&c, 1) <= 0)\n goto err;\n#endif\n if (c >= 128 && i > 0)\n buf[i] = buf[i - 1];\n else if (c < 42)\n buf[i] = 0;\n else if (c < 84)\n buf[i] = 255;\n }\n }\n if (top >= 0) {\n if (top) {\n if (bit == 0) {\n buf[0] = 1;\n buf[1] |= 0x80;\n } else {\n buf[0] |= (3 << (bit - 1));\n }\n } else {\n buf[0] |= (1 << bit);\n }\n }\n buf[0] &= ~mask;\n if (bottom)\n buf[bytes - 1] |= 1;\n if (!BN_bin2bn(buf, bytes, rnd))\n goto err;\n ret = 1;\n err:\n OPENSSL_clear_free(buf, bytes);\n bn_check_top(rnd);\n return ret;\ntoosmall:\n BNerr(BN_F_BNRAND, BN_R_BITS_TOO_SMALL);\n return 0;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n a->flags &= ~BN_FLG_FIXED_TOP;\n bn_check_top(a);\n return 1;\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}']
|
1,022 | 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 at1_unpack_dequant(BitstreamContext *bc, AT1SUCtx *su,\n float spec[AT1_SU_SAMPLES])\n{\n int bits_used, band_num, bfu_num, i;\n uint8_t idwls[AT1_MAX_BFU];\n uint8_t idsfs[AT1_MAX_BFU];\n su->num_bfus = bfu_amount_tab1[bitstream_read(bc, 3)];\n bits_used = su->num_bfus * 10 + 32 +\n bfu_amount_tab2[bitstream_read(bc, 2)] +\n (bfu_amount_tab3[bitstream_read(bc, 3)] << 1);\n for (i = 0; i < su->num_bfus; i++)\n idwls[i] = bitstream_read(bc, 4);\n for (i = 0; i < su->num_bfus; i++)\n idsfs[i] = bitstream_read(bc, 6);\n for (i = su->num_bfus; i < AT1_MAX_BFU; i++)\n idwls[i] = idsfs[i] = 0;\n for (band_num = 0; band_num < AT1_QMF_BANDS; band_num++) {\n for (bfu_num = bfu_bands_t[band_num]; bfu_num < bfu_bands_t[band_num+1]; bfu_num++) {\n int pos;\n int num_specs = specs_per_bfu[bfu_num];\n int word_len = !!idwls[bfu_num] + idwls[bfu_num];\n float scale_factor = ff_atrac_sf_table[idsfs[bfu_num]];\n bits_used += word_len * num_specs;\n if (bits_used > AT1_SU_MAX_BITS)\n return AVERROR_INVALIDDATA;\n pos = su->log2_block_count[band_num] ? bfu_start_short[bfu_num] : bfu_start_long[bfu_num];\n if (word_len) {\n float max_quant = 1.0 / (float)((1 << (word_len - 1)) - 1);\n for (i = 0; i < num_specs; i++) {\n spec[pos+i] = bitstream_read_signed(bc, word_len) * scale_factor * max_quant;\n }\n } else {\n memset(&spec[pos], 0, num_specs * sizeof(float));\n }\n }\n }\n return 0;\n}', 'static inline uint32_t bitstream_read(BitstreamContext *bc, unsigned n)\n{\n if (!n)\n return 0;\n if (n > bc->bits_left) {\n refill_32(bc);\n if (bc->bits_left < 32)\n bc->bits_left = n;\n }\n return get_val(bc, n);\n}', 'static inline uint64_t get_val(BitstreamContext *bc, unsigned n)\n{\n#ifdef BITSTREAM_READER_LE\n uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);\n bc->bits >>= n;\n#else\n uint64_t ret = bc->bits >> (64 - n);\n bc->bits <<= n;\n#endif\n bc->bits_left -= n;\n return ret;\n}']
|
1,023 | 0 |
https://github.com/openssl/openssl/blob/0350ef69add8758dd180e73cbc7c1961bf64e503/crypto/bn/bn_ctx.c/#L353
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group,\n EC_POINT *point,\n const BIGNUM *x_, int y_bit,\n BN_CTX *ctx)\n{\n BN_CTX *new_ctx = NULL;\n BIGNUM *tmp1, *tmp2, *x, *y;\n int ret = 0;\n ERR_clear_error();\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n y_bit = (y_bit != 0);\n BN_CTX_start(ctx);\n tmp1 = BN_CTX_get(ctx);\n tmp2 = BN_CTX_get(ctx);\n x = BN_CTX_get(ctx);\n y = BN_CTX_get(ctx);\n if (y == NULL)\n goto err;\n if (!BN_nnmod(x, x_, group->field, ctx))\n goto err;\n if (group->meth->field_decode == 0) {\n if (!group->meth->field_sqr(group, tmp2, x_, ctx))\n goto err;\n if (!group->meth->field_mul(group, tmp1, tmp2, x_, ctx))\n goto err;\n } else {\n if (!BN_mod_sqr(tmp2, x_, group->field, ctx))\n goto err;\n if (!BN_mod_mul(tmp1, tmp2, x_, group->field, ctx))\n goto err;\n }\n if (group->a_is_minus3) {\n if (!BN_mod_lshift1_quick(tmp2, x, group->field))\n goto err;\n if (!BN_mod_add_quick(tmp2, tmp2, x, group->field))\n goto err;\n if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n } else {\n if (group->meth->field_decode) {\n if (!group->meth->field_decode(group, tmp2, group->a, ctx))\n goto err;\n if (!BN_mod_mul(tmp2, tmp2, x, group->field, ctx))\n goto err;\n } else {\n if (!group->meth->field_mul(group, tmp2, group->a, x, ctx))\n goto err;\n }\n if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n }\n if (group->meth->field_decode) {\n if (!group->meth->field_decode(group, tmp2, group->b, ctx))\n goto err;\n if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n } else {\n if (!BN_mod_add_quick(tmp1, tmp1, group->b, group->field))\n goto err;\n }\n if (!BN_mod_sqrt(y, tmp1, group->field, ctx)) {\n unsigned long err = ERR_peek_last_error();\n if (ERR_GET_LIB(err) == ERR_LIB_BN\n && ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) {\n ERR_clear_error();\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSED_POINT);\n } else\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n ERR_R_BN_LIB);\n goto err;\n }\n if (y_bit != BN_is_odd(y)) {\n if (BN_is_zero(y)) {\n int kron;\n kron = BN_kronecker(x, group->field, ctx);\n if (kron == -2)\n goto err;\n if (kron == 1)\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSION_BIT);\n else\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSED_POINT);\n goto err;\n }\n if (!BN_usub(y, group->field, y))\n goto err;\n }\n if (y_bit != BN_is_odd(y)) {\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx))\n goto err;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n if (new_ctx != NULL)\n BN_CTX_free(new_ctx);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_start", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG_EXIT(ctx);\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return (0);\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return (0);\n }\n if (dv != NULL)\n BN_zero(dv);\n return (1);\n }\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (dv == NULL)\n res = BN_CTX_get(ctx);\n else\n res = dv;\n if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n res->neg = (num->neg ^ divisor->neg);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--, resp--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# ifdef BN_DEBUG_LEVITTE\n fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n", n0, n1, d0, q);\n# endif\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifdef BN_DEBUG_LEVITTE\n fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n", n0, n1, d0, q);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return (1);\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return (0);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
|
1,024 | 0 |
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/x509/x509_obj.c/#L97
|
char *X509_NAME_oneline(X509_NAME *a, char *buf, int len)
{
X509_NAME_ENTRY *ne;
int i;
int n, lold, l, l1, l2, num, j, type;
const char *s;
char *p;
unsigned char *q;
BUF_MEM *b = NULL;
static const char hex[17] = "0123456789ABCDEF";
int gs_doit[4];
char tmp_buf[80];
#ifdef CHARSET_EBCDIC
char ebcdic_buf[1024];
#endif
if (buf == NULL) {
if ((b = BUF_MEM_new()) == NULL)
goto err;
if (!BUF_MEM_grow(b, 200))
goto err;
b->data[0] = '\0';
len = 200;
}
if (a == NULL) {
if (b) {
buf = b->data;
OPENSSL_free(b);
}
strncpy(buf, "NO X509_NAME", len);
buf[len - 1] = '\0';
return buf;
}
len--;
l = 0;
for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
ne = sk_X509_NAME_ENTRY_value(a->entries, i);
n = OBJ_obj2nid(ne->object);
if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
i2t_ASN1_OBJECT(tmp_buf, sizeof(tmp_buf), ne->object);
s = tmp_buf;
}
l1 = strlen(s);
type = ne->value->type;
num = ne->value->length;
q = ne->value->data;
#ifdef CHARSET_EBCDIC
if (type == V_ASN1_GENERALSTRING ||
type == V_ASN1_VISIBLESTRING ||
type == V_ASN1_PRINTABLESTRING ||
type == V_ASN1_TELETEXSTRING ||
type == V_ASN1_VISIBLESTRING || type == V_ASN1_IA5STRING) {
ascii2ebcdic(ebcdic_buf, q, (num > sizeof ebcdic_buf)
? sizeof ebcdic_buf : num);
q = ebcdic_buf;
}
#endif
if ((type == V_ASN1_GENERALSTRING) && ((num % 4) == 0)) {
gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 0;
for (j = 0; j < num; j++)
if (q[j] != 0)
gs_doit[j & 3] = 1;
if (gs_doit[0] | gs_doit[1] | gs_doit[2])
gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1;
else {
gs_doit[0] = gs_doit[1] = gs_doit[2] = 0;
gs_doit[3] = 1;
}
} else
gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1;
for (l2 = j = 0; j < num; j++) {
if (!gs_doit[j & 3])
continue;
l2++;
#ifndef CHARSET_EBCDIC
if ((q[j] < ' ') || (q[j] > '~'))
l2 += 3;
#else
if ((os_toascii[q[j]] < os_toascii[' ']) ||
(os_toascii[q[j]] > os_toascii['~']))
l2 += 3;
#endif
}
lold = l;
l += 1 + l1 + 1 + l2;
if (b != NULL) {
if (!BUF_MEM_grow(b, l + 1))
goto err;
p = &(b->data[lold]);
} else if (l > len) {
break;
} else
p = &(buf[lold]);
*(p++) = '/';
memcpy(p, s, (unsigned int)l1);
p += l1;
*(p++) = '=';
#ifndef CHARSET_EBCDIC
q = ne->value->data;
#endif
for (j = 0; j < num; j++) {
if (!gs_doit[j & 3])
continue;
#ifndef CHARSET_EBCDIC
n = q[j];
if ((n < ' ') || (n > '~')) {
*(p++) = '\\';
*(p++) = 'x';
*(p++) = hex[(n >> 4) & 0x0f];
*(p++) = hex[n & 0x0f];
} else
*(p++) = n;
#else
n = os_toascii[q[j]];
if ((n < os_toascii[' ']) || (n > os_toascii['~'])) {
*(p++) = '\\';
*(p++) = 'x';
*(p++) = hex[(n >> 4) & 0x0f];
*(p++) = hex[n & 0x0f];
} else
*(p++) = q[j];
#endif
}
*p = '\0';
}
if (b != NULL) {
p = b->data;
OPENSSL_free(b);
} else
p = buf;
if (i == 0)
*p = '\0';
return (p);
err:
X509err(X509_F_X509_NAME_ONELINE, ERR_R_MALLOC_FAILURE);
BUF_MEM_free(b);
return (NULL);
}
|
['static int verify_callback(int ok, X509_STORE_CTX *ctx)\n{\n char *s, buf[256];\n s = X509_NAME_oneline(X509_get_subject_name(ctx->current_cert), buf,\n sizeof buf);\n if (s != NULL) {\n if (ok)\n printf("depth=%d %s\\n", ctx->error_depth, buf);\n else {\n fprintf(stderr, "depth=%d error=%d %s\\n",\n ctx->error_depth, ctx->error, buf);\n }\n }\n if (ok == 0) {\n switch (ctx->error) {\n default:\n fprintf(stderr, "Error string: %s\\n",\n X509_verify_cert_error_string(ctx->error));\n break;\n case X509_V_ERR_CERT_NOT_YET_VALID:\n case X509_V_ERR_CERT_HAS_EXPIRED:\n case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:\n ok = 1;\n }\n }\n if (ok == 1) {\n X509 *xs = ctx->current_cert;\n if (X509_get_extension_flags(xs) & EXFLAG_PROXY) {\n unsigned int *letters = X509_STORE_CTX_get_ex_data(ctx,\n get_proxy_auth_ex_data_idx\n ());\n if (letters) {\n int found_any = 0;\n int i;\n PROXY_CERT_INFO_EXTENSION *pci =\n X509_get_ext_d2i(xs, NID_proxyCertInfo,\n NULL, NULL);\n switch (OBJ_obj2nid(pci->proxyPolicy->policyLanguage)) {\n case NID_Independent:\n fprintf(stderr, " Independent proxy certificate");\n for (i = 0; i < 26; i++)\n letters[i] = 0;\n break;\n case NID_id_ppl_inheritAll:\n fprintf(stderr, " Proxy certificate inherits all");\n break;\n default:\n s = (char *)\n pci->proxyPolicy->policy->data;\n i = pci->proxyPolicy->policy->length;\n printf(" Certificate proxy rights = %*.*s", i,\n i, s);\n while (i-- > 0) {\n int c = *s++;\n if (isascii(c) && isalpha(c)) {\n if (islower(c))\n c = toupper(c);\n letters[c - \'A\']++;\n }\n }\n for (i = 0; i < 26; i++)\n if (letters[i] < 2)\n letters[i] = 0;\n else\n letters[i] = 1;\n }\n found_any = 0;\n printf(", resulting proxy rights = ");\n for (i = 0; i < 26; i++)\n if (letters[i]) {\n printf("%c", i + \'A\');\n found_any = 1;\n }\n if (!found_any)\n printf("none");\n printf("\\n");\n PROXY_CERT_INFO_EXTENSION_free(pci);\n }\n }\n }\n return (ok);\n}', 'char *X509_NAME_oneline(X509_NAME *a, char *buf, int len)\n{\n X509_NAME_ENTRY *ne;\n int i;\n int n, lold, l, l1, l2, num, j, type;\n const char *s;\n char *p;\n unsigned char *q;\n BUF_MEM *b = NULL;\n static const char hex[17] = "0123456789ABCDEF";\n int gs_doit[4];\n char tmp_buf[80];\n#ifdef CHARSET_EBCDIC\n char ebcdic_buf[1024];\n#endif\n if (buf == NULL) {\n if ((b = BUF_MEM_new()) == NULL)\n goto err;\n if (!BUF_MEM_grow(b, 200))\n goto err;\n b->data[0] = \'\\0\';\n len = 200;\n }\n if (a == NULL) {\n if (b) {\n buf = b->data;\n OPENSSL_free(b);\n }\n strncpy(buf, "NO X509_NAME", len);\n buf[len - 1] = \'\\0\';\n return buf;\n }\n len--;\n l = 0;\n for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {\n ne = sk_X509_NAME_ENTRY_value(a->entries, i);\n n = OBJ_obj2nid(ne->object);\n if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {\n i2t_ASN1_OBJECT(tmp_buf, sizeof(tmp_buf), ne->object);\n s = tmp_buf;\n }\n l1 = strlen(s);\n type = ne->value->type;\n num = ne->value->length;\n q = ne->value->data;\n#ifdef CHARSET_EBCDIC\n if (type == V_ASN1_GENERALSTRING ||\n type == V_ASN1_VISIBLESTRING ||\n type == V_ASN1_PRINTABLESTRING ||\n type == V_ASN1_TELETEXSTRING ||\n type == V_ASN1_VISIBLESTRING || type == V_ASN1_IA5STRING) {\n ascii2ebcdic(ebcdic_buf, q, (num > sizeof ebcdic_buf)\n ? sizeof ebcdic_buf : num);\n q = ebcdic_buf;\n }\n#endif\n if ((type == V_ASN1_GENERALSTRING) && ((num % 4) == 0)) {\n gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 0;\n for (j = 0; j < num; j++)\n if (q[j] != 0)\n gs_doit[j & 3] = 1;\n if (gs_doit[0] | gs_doit[1] | gs_doit[2])\n gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1;\n else {\n gs_doit[0] = gs_doit[1] = gs_doit[2] = 0;\n gs_doit[3] = 1;\n }\n } else\n gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1;\n for (l2 = j = 0; j < num; j++) {\n if (!gs_doit[j & 3])\n continue;\n l2++;\n#ifndef CHARSET_EBCDIC\n if ((q[j] < \' \') || (q[j] > \'~\'))\n l2 += 3;\n#else\n if ((os_toascii[q[j]] < os_toascii[\' \']) ||\n (os_toascii[q[j]] > os_toascii[\'~\']))\n l2 += 3;\n#endif\n }\n lold = l;\n l += 1 + l1 + 1 + l2;\n if (b != NULL) {\n if (!BUF_MEM_grow(b, l + 1))\n goto err;\n p = &(b->data[lold]);\n } else if (l > len) {\n break;\n } else\n p = &(buf[lold]);\n *(p++) = \'/\';\n memcpy(p, s, (unsigned int)l1);\n p += l1;\n *(p++) = \'=\';\n#ifndef CHARSET_EBCDIC\n q = ne->value->data;\n#endif\n for (j = 0; j < num; j++) {\n if (!gs_doit[j & 3])\n continue;\n#ifndef CHARSET_EBCDIC\n n = q[j];\n if ((n < \' \') || (n > \'~\')) {\n *(p++) = \'\\\\\';\n *(p++) = \'x\';\n *(p++) = hex[(n >> 4) & 0x0f];\n *(p++) = hex[n & 0x0f];\n } else\n *(p++) = n;\n#else\n n = os_toascii[q[j]];\n if ((n < os_toascii[\' \']) || (n > os_toascii[\'~\'])) {\n *(p++) = \'\\\\\';\n *(p++) = \'x\';\n *(p++) = hex[(n >> 4) & 0x0f];\n *(p++) = hex[n & 0x0f];\n } else\n *(p++) = q[j];\n#endif\n }\n *p = \'\\0\';\n }\n if (b != NULL) {\n p = b->data;\n OPENSSL_free(b);\n } else\n p = buf;\n if (i == 0)\n *p = \'\\0\';\n return (p);\n err:\n X509err(X509_F_X509_NAME_ONELINE, ERR_R_MALLOC_FAILURE);\n BUF_MEM_free(b);\n return (NULL);\n}']
|
1,025 | 0 |
https://github.com/openssl/openssl/blob/9dd4ac8cf17f2afd636e85ae0111d1df4104a475/crypto/evp/evp_key.c/#L147
|
int EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md,
const unsigned char *salt, const unsigned char *data,
int datal, int count, unsigned char *key,
unsigned char *iv)
{
EVP_MD_CTX *c;
unsigned char md_buf[EVP_MAX_MD_SIZE];
int niv, nkey, addmd = 0;
unsigned int mds = 0, i;
int rv = 0;
nkey = EVP_CIPHER_key_length(type);
niv = EVP_CIPHER_iv_length(type);
OPENSSL_assert(nkey <= EVP_MAX_KEY_LENGTH);
OPENSSL_assert(niv <= EVP_MAX_IV_LENGTH);
if (data == NULL)
return (nkey);
c = EVP_MD_CTX_new();
if (c == NULL)
goto err;
for (;;) {
if (!EVP_DigestInit_ex(c, md, NULL))
goto err;
if (addmd++)
if (!EVP_DigestUpdate(c, &(md_buf[0]), mds))
goto err;
if (!EVP_DigestUpdate(c, data, datal))
goto err;
if (salt != NULL)
if (!EVP_DigestUpdate(c, salt, PKCS5_SALT_LEN))
goto err;
if (!EVP_DigestFinal_ex(c, &(md_buf[0]), &mds))
goto err;
for (i = 1; i < (unsigned int)count; i++) {
if (!EVP_DigestInit_ex(c, md, NULL))
goto err;
if (!EVP_DigestUpdate(c, &(md_buf[0]), mds))
goto err;
if (!EVP_DigestFinal_ex(c, &(md_buf[0]), &mds))
goto err;
}
i = 0;
if (nkey) {
for (;;) {
if (nkey == 0)
break;
if (i == mds)
break;
if (key != NULL)
*(key++) = md_buf[i];
nkey--;
i++;
}
}
if (niv && (i != mds)) {
for (;;) {
if (niv == 0)
break;
if (i == mds)
break;
if (iv != NULL)
*(iv++) = md_buf[i];
niv--;
i++;
}
}
if ((nkey == 0) && (niv == 0))
break;
}
rv = EVP_CIPHER_key_length(type);
err:
EVP_MD_CTX_free(c);
OPENSSL_cleanse(md_buf, sizeof(md_buf));
return rv;
}
|
['int EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md,\n const unsigned char *salt, const unsigned char *data,\n int datal, int count, unsigned char *key,\n unsigned char *iv)\n{\n EVP_MD_CTX *c;\n unsigned char md_buf[EVP_MAX_MD_SIZE];\n int niv, nkey, addmd = 0;\n unsigned int mds = 0, i;\n int rv = 0;\n nkey = EVP_CIPHER_key_length(type);\n niv = EVP_CIPHER_iv_length(type);\n OPENSSL_assert(nkey <= EVP_MAX_KEY_LENGTH);\n OPENSSL_assert(niv <= EVP_MAX_IV_LENGTH);\n if (data == NULL)\n return (nkey);\n c = EVP_MD_CTX_new();\n if (c == NULL)\n goto err;\n for (;;) {\n if (!EVP_DigestInit_ex(c, md, NULL))\n goto err;\n if (addmd++)\n if (!EVP_DigestUpdate(c, &(md_buf[0]), mds))\n goto err;\n if (!EVP_DigestUpdate(c, data, datal))\n goto err;\n if (salt != NULL)\n if (!EVP_DigestUpdate(c, salt, PKCS5_SALT_LEN))\n goto err;\n if (!EVP_DigestFinal_ex(c, &(md_buf[0]), &mds))\n goto err;\n for (i = 1; i < (unsigned int)count; i++) {\n if (!EVP_DigestInit_ex(c, md, NULL))\n goto err;\n if (!EVP_DigestUpdate(c, &(md_buf[0]), mds))\n goto err;\n if (!EVP_DigestFinal_ex(c, &(md_buf[0]), &mds))\n goto err;\n }\n i = 0;\n if (nkey) {\n for (;;) {\n if (nkey == 0)\n break;\n if (i == mds)\n break;\n if (key != NULL)\n *(key++) = md_buf[i];\n nkey--;\n i++;\n }\n }\n if (niv && (i != mds)) {\n for (;;) {\n if (niv == 0)\n break;\n if (i == mds)\n break;\n if (iv != NULL)\n *(iv++) = md_buf[i];\n niv--;\n i++;\n }\n }\n if ((nkey == 0) && (niv == 0))\n break;\n }\n rv = EVP_CIPHER_key_length(type);\n err:\n EVP_MD_CTX_free(c);\n OPENSSL_cleanse(md_buf, sizeof(md_buf));\n return rv;\n}', 'int EVP_CIPHER_key_length(const EVP_CIPHER *cipher)\n{\n return cipher->key_len;\n}', 'int EVP_CIPHER_iv_length(const EVP_CIPHER *cipher)\n{\n return cipher->iv_len;\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_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count)\n{\n return ctx->update(ctx, data, count);\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}']
|
1,026 | 0 |
https://github.com/libav/libav/blob/f4f3300c09bb13eb7922e60888b55e3e0fb325e7/libavcodec/aacps.c/#L721
|
static void decorrelation(PSContext *ps, float (*out)[32][2], const float (*s)[32][2], int is34)
{
float power[34][PS_QMF_TIME_SLOTS] = {{0}};
float transient_gain[34][PS_QMF_TIME_SLOTS];
float *peak_decay_nrg = ps->peak_decay_nrg;
float *power_smooth = ps->power_smooth;
float *peak_decay_diff_smooth = ps->peak_decay_diff_smooth;
float (*delay)[PS_QMF_TIME_SLOTS + PS_MAX_DELAY][2] = ps->delay;
float (*ap_delay)[PS_AP_LINKS][PS_QMF_TIME_SLOTS + PS_MAX_AP_DELAY][2] = ps->ap_delay;
const int8_t *k_to_i = is34 ? k_to_i_34 : k_to_i_20;
const float peak_decay_factor = 0.76592833836465f;
const float transient_impact = 1.5f;
const float a_smooth = 0.25f;
int i, k, m, n;
int n0 = 0, nL = 32;
static const int link_delay[] = { 3, 4, 5 };
static const float a[] = { 0.65143905753106f,
0.56471812200776f,
0.48954165955695f };
if (is34 != ps->is34bands_old) {
memset(ps->peak_decay_nrg, 0, sizeof(ps->peak_decay_nrg));
memset(ps->power_smooth, 0, sizeof(ps->power_smooth));
memset(ps->peak_decay_diff_smooth, 0, sizeof(ps->peak_decay_diff_smooth));
memset(ps->delay, 0, sizeof(ps->delay));
memset(ps->ap_delay, 0, sizeof(ps->ap_delay));
}
for (n = n0; n < nL; n++) {
for (k = 0; k < NR_BANDS[is34]; k++) {
int i = k_to_i[k];
power[i][n] += s[k][n][0] * s[k][n][0] + s[k][n][1] * s[k][n][1];
}
}
for (i = 0; i < NR_PAR_BANDS[is34]; i++) {
for (n = n0; n < nL; n++) {
float decayed_peak = peak_decay_factor * peak_decay_nrg[i];
float denom;
peak_decay_nrg[i] = FFMAX(decayed_peak, power[i][n]);
power_smooth[i] += a_smooth * (power[i][n] - power_smooth[i]);
peak_decay_diff_smooth[i] += a_smooth * (peak_decay_nrg[i] - power[i][n] - peak_decay_diff_smooth[i]);
denom = transient_impact * peak_decay_diff_smooth[i];
transient_gain[i][n] = (denom > power_smooth[i]) ?
power_smooth[i] / denom : 1.0f;
}
}
for (k = 0; k < NR_ALLPASS_BANDS[is34]; k++) {
int b = k_to_i[k];
float g_decay_slope = 1.f - DECAY_SLOPE * (k - DECAY_CUTOFF[is34]);
float ag[PS_AP_LINKS];
g_decay_slope = av_clipf(g_decay_slope, 0.f, 1.f);
memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
for (m = 0; m < PS_AP_LINKS; m++) {
memcpy(ap_delay[k][m], ap_delay[k][m]+numQMFSlots, 5*sizeof(ap_delay[k][m][0]));
ag[m] = a[m] * g_decay_slope;
}
for (n = n0; n < nL; n++) {
float in_re = delay[k][n+PS_MAX_DELAY-2][0] * phi_fract[is34][k][0] -
delay[k][n+PS_MAX_DELAY-2][1] * phi_fract[is34][k][1];
float in_im = delay[k][n+PS_MAX_DELAY-2][0] * phi_fract[is34][k][1] +
delay[k][n+PS_MAX_DELAY-2][1] * phi_fract[is34][k][0];
for (m = 0; m < PS_AP_LINKS; m++) {
float a_re = ag[m] * in_re;
float a_im = ag[m] * in_im;
float link_delay_re = ap_delay[k][m][n+5-link_delay[m]][0];
float link_delay_im = ap_delay[k][m][n+5-link_delay[m]][1];
float fractional_delay_re = Q_fract_allpass[is34][k][m][0];
float fractional_delay_im = Q_fract_allpass[is34][k][m][1];
ap_delay[k][m][n+5][0] = in_re;
ap_delay[k][m][n+5][1] = in_im;
in_re = link_delay_re * fractional_delay_re - link_delay_im * fractional_delay_im - a_re;
in_im = link_delay_re * fractional_delay_im + link_delay_im * fractional_delay_re - a_im;
ap_delay[k][m][n+5][0] += ag[m] * in_re;
ap_delay[k][m][n+5][1] += ag[m] * in_im;
}
out[k][n][0] = transient_gain[b][n] * in_re;
out[k][n][1] = transient_gain[b][n] * in_im;
}
}
for (; k < SHORT_DELAY_BAND[is34]; k++) {
memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
for (n = n0; n < nL; n++) {
out[k][n][0] = transient_gain[k_to_i[k]][n] * delay[k][n+PS_MAX_DELAY-14][0];
out[k][n][1] = transient_gain[k_to_i[k]][n] * delay[k][n+PS_MAX_DELAY-14][1];
}
}
for (; k < NR_BANDS[is34]; k++) {
memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
for (n = n0; n < nL; n++) {
out[k][n][0] = transient_gain[k_to_i[k]][n] * delay[k][n+PS_MAX_DELAY-1][0];
out[k][n][1] = transient_gain[k_to_i[k]][n] * delay[k][n+PS_MAX_DELAY-1][1];
}
}
}
|
['static void decorrelation(PSContext *ps, float (*out)[32][2], const float (*s)[32][2], int is34)\n{\n float power[34][PS_QMF_TIME_SLOTS] = {{0}};\n float transient_gain[34][PS_QMF_TIME_SLOTS];\n float *peak_decay_nrg = ps->peak_decay_nrg;\n float *power_smooth = ps->power_smooth;\n float *peak_decay_diff_smooth = ps->peak_decay_diff_smooth;\n float (*delay)[PS_QMF_TIME_SLOTS + PS_MAX_DELAY][2] = ps->delay;\n float (*ap_delay)[PS_AP_LINKS][PS_QMF_TIME_SLOTS + PS_MAX_AP_DELAY][2] = ps->ap_delay;\n const int8_t *k_to_i = is34 ? k_to_i_34 : k_to_i_20;\n const float peak_decay_factor = 0.76592833836465f;\n const float transient_impact = 1.5f;\n const float a_smooth = 0.25f;\n int i, k, m, n;\n int n0 = 0, nL = 32;\n static const int link_delay[] = { 3, 4, 5 };\n static const float a[] = { 0.65143905753106f,\n 0.56471812200776f,\n 0.48954165955695f };\n if (is34 != ps->is34bands_old) {\n memset(ps->peak_decay_nrg, 0, sizeof(ps->peak_decay_nrg));\n memset(ps->power_smooth, 0, sizeof(ps->power_smooth));\n memset(ps->peak_decay_diff_smooth, 0, sizeof(ps->peak_decay_diff_smooth));\n memset(ps->delay, 0, sizeof(ps->delay));\n memset(ps->ap_delay, 0, sizeof(ps->ap_delay));\n }\n for (n = n0; n < nL; n++) {\n for (k = 0; k < NR_BANDS[is34]; k++) {\n int i = k_to_i[k];\n power[i][n] += s[k][n][0] * s[k][n][0] + s[k][n][1] * s[k][n][1];\n }\n }\n for (i = 0; i < NR_PAR_BANDS[is34]; i++) {\n for (n = n0; n < nL; n++) {\n float decayed_peak = peak_decay_factor * peak_decay_nrg[i];\n float denom;\n peak_decay_nrg[i] = FFMAX(decayed_peak, power[i][n]);\n power_smooth[i] += a_smooth * (power[i][n] - power_smooth[i]);\n peak_decay_diff_smooth[i] += a_smooth * (peak_decay_nrg[i] - power[i][n] - peak_decay_diff_smooth[i]);\n denom = transient_impact * peak_decay_diff_smooth[i];\n transient_gain[i][n] = (denom > power_smooth[i]) ?\n power_smooth[i] / denom : 1.0f;\n }\n }\n for (k = 0; k < NR_ALLPASS_BANDS[is34]; k++) {\n int b = k_to_i[k];\n float g_decay_slope = 1.f - DECAY_SLOPE * (k - DECAY_CUTOFF[is34]);\n float ag[PS_AP_LINKS];\n g_decay_slope = av_clipf(g_decay_slope, 0.f, 1.f);\n memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));\n memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));\n for (m = 0; m < PS_AP_LINKS; m++) {\n memcpy(ap_delay[k][m], ap_delay[k][m]+numQMFSlots, 5*sizeof(ap_delay[k][m][0]));\n ag[m] = a[m] * g_decay_slope;\n }\n for (n = n0; n < nL; n++) {\n float in_re = delay[k][n+PS_MAX_DELAY-2][0] * phi_fract[is34][k][0] -\n delay[k][n+PS_MAX_DELAY-2][1] * phi_fract[is34][k][1];\n float in_im = delay[k][n+PS_MAX_DELAY-2][0] * phi_fract[is34][k][1] +\n delay[k][n+PS_MAX_DELAY-2][1] * phi_fract[is34][k][0];\n for (m = 0; m < PS_AP_LINKS; m++) {\n float a_re = ag[m] * in_re;\n float a_im = ag[m] * in_im;\n float link_delay_re = ap_delay[k][m][n+5-link_delay[m]][0];\n float link_delay_im = ap_delay[k][m][n+5-link_delay[m]][1];\n float fractional_delay_re = Q_fract_allpass[is34][k][m][0];\n float fractional_delay_im = Q_fract_allpass[is34][k][m][1];\n ap_delay[k][m][n+5][0] = in_re;\n ap_delay[k][m][n+5][1] = in_im;\n in_re = link_delay_re * fractional_delay_re - link_delay_im * fractional_delay_im - a_re;\n in_im = link_delay_re * fractional_delay_im + link_delay_im * fractional_delay_re - a_im;\n ap_delay[k][m][n+5][0] += ag[m] * in_re;\n ap_delay[k][m][n+5][1] += ag[m] * in_im;\n }\n out[k][n][0] = transient_gain[b][n] * in_re;\n out[k][n][1] = transient_gain[b][n] * in_im;\n }\n }\n for (; k < SHORT_DELAY_BAND[is34]; k++) {\n memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));\n memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));\n for (n = n0; n < nL; n++) {\n out[k][n][0] = transient_gain[k_to_i[k]][n] * delay[k][n+PS_MAX_DELAY-14][0];\n out[k][n][1] = transient_gain[k_to_i[k]][n] * delay[k][n+PS_MAX_DELAY-14][1];\n }\n }\n for (; k < NR_BANDS[is34]; k++) {\n memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));\n memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));\n for (n = n0; n < nL; n++) {\n out[k][n][0] = transient_gain[k_to_i[k]][n] * delay[k][n+PS_MAX_DELAY-1][0];\n out[k][n][1] = transient_gain[k_to_i[k]][n] * delay[k][n+PS_MAX_DELAY-1][1];\n }\n }\n}']
|
1,027 | 0 |
https://github.com/libav/libav/blob/452a398fd6bdca3f301c5c8af3bc241bc16a777e/libavcodec/mpegaudiodec.c/#L894
|
void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
MPA_INT *window, int *dither_state,
OUT_INT *samples, int incr,
int32_t sb_samples[SBLIMIT])
{
int32_t tmp[32];
register MPA_INT *synth_buf;
register const MPA_INT *w, *w2, *p;
int j, offset, v;
OUT_INT *samples2;
#if FRAC_BITS <= 15
int sum, sum2;
#else
int64_t sum, sum2;
#endif
dct32(tmp, sb_samples);
offset = *synth_buf_offset;
synth_buf = synth_buf_ptr + offset;
for(j=0;j<32;j++) {
v = tmp[j];
#if FRAC_BITS <= 15
v = av_clip_int16(v);
#endif
synth_buf[j] = v;
}
memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));
samples2 = samples + 31 * incr;
w = window;
w2 = window + 31;
sum = *dither_state;
p = synth_buf + 16;
SUM8(MACS, sum, w, p);
p = synth_buf + 48;
SUM8(MLSS, sum, w + 32, p);
*samples = round_sample(&sum);
samples += incr;
w++;
for(j=1;j<16;j++) {
sum2 = 0;
p = synth_buf + 16 + j;
SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);
p = synth_buf + 48 - j;
SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);
*samples = round_sample(&sum);
samples += incr;
sum += sum2;
*samples2 = round_sample(&sum);
samples2 -= incr;
w++;
w2--;
}
p = synth_buf + 32;
SUM8(MLSS, sum, w + 32, p);
*samples = round_sample(&sum);
*dither_state= sum;
offset = (offset - 32) & 511;
*synth_buf_offset = offset;
}
|
['static void mpc_synth(MPCContext *c, int16_t *out)\n{\n int dither_state = 0;\n int i, ch;\n OUT_INT samples[MPA_MAX_CHANNELS * MPA_FRAME_SIZE], *samples_ptr;\n for(ch = 0; ch < 2; ch++){\n samples_ptr = samples + ch;\n for(i = 0; i < SAMPLES_PER_BAND; i++) {\n ff_mpa_synth_filter(c->synth_buf[ch], &(c->synth_buf_offset[ch]),\n 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 int32_t tmp[32];\n register MPA_INT *synth_buf;\n register const MPA_INT *w, *w2, *p;\n int j, offset, v;\n OUT_INT *samples2;\n#if FRAC_BITS <= 15\n int sum, sum2;\n#else\n int64_t sum, sum2;\n#endif\n dct32(tmp, sb_samples);\n offset = *synth_buf_offset;\n synth_buf = synth_buf_ptr + offset;\n for(j=0;j<32;j++) {\n v = tmp[j];\n#if FRAC_BITS <= 15\n v = av_clip_int16(v);\n#endif\n synth_buf[j] = v;\n }\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}']
|
1,028 | 0 |
https://github.com/openssl/openssl/blob/024d681e69cc1ea7177a7eae9aeb1947412950ed/crypto/mem.c/#L179
|
void CRYPTO_free(void *str, const char *file, int line)
{
if (free_impl != NULL && free_impl != &CRYPTO_free) {
free_impl(str, file, line);
return;
}
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
if (call_malloc_debug) {
CRYPTO_mem_debug_free(str, 0, file, line);
free(str);
CRYPTO_mem_debug_free(str, 1, file, line);
} else {
free(str);
}
#else
free(str);
#endif
}
|
['static void int_engine_module_finish(CONF_IMODULE *md)\n{\n ENGINE *e;\n while ((e = sk_ENGINE_pop(initialized_engines)))\n ENGINE_finish(e);\n sk_ENGINE_free(initialized_engines);\n initialized_engines = NULL;\n}', 'int ENGINE_finish(ENGINE *e)\n{\n int to_return = 1;\n if (e == NULL)\n return 1;\n CRYPTO_THREAD_write_lock(global_engine_lock);\n to_return = engine_unlocked_finish(e, 1);\n CRYPTO_THREAD_unlock(global_engine_lock);\n if (!to_return) {\n ENGINEerr(ENGINE_F_ENGINE_FINISH, ENGINE_R_FINISH_FAILED);\n return 0;\n }\n return to_return;\n}', 'int engine_unlocked_finish(ENGINE *e, int unlock_for_handlers)\n{\n int to_return = 1;\n e->funct_ref--;\n engine_ref_debug(e, 1, -1);\n if ((e->funct_ref == 0) && e->finish) {\n if (unlock_for_handlers)\n CRYPTO_THREAD_unlock(global_engine_lock);\n to_return = e->finish(e);\n if (unlock_for_handlers)\n CRYPTO_THREAD_write_lock(global_engine_lock);\n if (!to_return)\n return 0;\n }\n REF_ASSERT_ISNT(e->funct_ref < 0);\n if (!engine_free_util(e, 0)) {\n ENGINEerr(ENGINE_F_ENGINE_UNLOCKED_FINISH, ENGINE_R_FINISH_FAILED);\n return 0;\n }\n return to_return;\n}', 'int engine_free_util(ENGINE *e, int not_locked)\n{\n int i;\n if (e == NULL)\n return 1;\n#ifdef HAVE_ATOMICS\n CRYPTO_DOWN_REF(&e->struct_ref, &i, global_engine_lock);\n#else\n if (not_locked)\n CRYPTO_atomic_add(&e->struct_ref, -1, &i, global_engine_lock);\n else\n i = --e->struct_ref;\n#endif\n engine_ref_debug(e, 0, -1)\n if (i > 0)\n return 1;\n REF_ASSERT_ISNT(i < 0);\n engine_pkey_meths_free(e);\n engine_pkey_asn1_meths_free(e);\n if (e->destroy)\n e->destroy(e);\n CRYPTO_free_ex_data(CRYPTO_EX_INDEX_ENGINE, e, &e->ex_data);\n OPENSSL_free(e);\n return 1;\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}']
|
1,029 | 0 |
https://github.com/libav/libav/blob/bf12a81cc67d62dd45c58e29fa0e9177331cc151/libavcodec/g723_1enc.c/#L853
|
static void get_fcb_param(FCBParam *optim, int16_t *impulse_resp,
int16_t *buf, int pulse_cnt, int pitch_lag)
{
FCBParam param;
int16_t impulse_r[SUBFRAME_LEN];
int16_t temp_corr[SUBFRAME_LEN];
int16_t impulse_corr[SUBFRAME_LEN];
int ccr1[SUBFRAME_LEN];
int ccr2[SUBFRAME_LEN];
int amp, err, max, max_amp_index, min, scale, i, j, k, l;
int64_t temp;
memcpy(impulse_r, impulse_resp, sizeof(int16_t) * SUBFRAME_LEN);
param.dirac_train = 0;
if (pitch_lag < SUBFRAME_LEN - 2) {
param.dirac_train = 1;
ff_g723_1_gen_dirac_train(impulse_r, pitch_lag);
}
for (i = 0; i < SUBFRAME_LEN; i++)
temp_corr[i] = impulse_r[i] >> 1;
temp = ff_g723_1_dot_product(temp_corr, temp_corr, SUBFRAME_LEN);
scale = ff_g723_1_normalize_bits(temp, 31);
impulse_corr[0] = av_clipl_int32((temp << scale) + (1 << 15)) >> 16;
for (i = 1; i < SUBFRAME_LEN; i++) {
temp = ff_g723_1_dot_product(temp_corr + i, temp_corr,
SUBFRAME_LEN - i);
impulse_corr[i] = av_clipl_int32((temp << scale) + (1 << 15)) >> 16;
}
scale -= 4;
for (i = 0; i < SUBFRAME_LEN; i++) {
temp = ff_g723_1_dot_product(buf + i, impulse_r, SUBFRAME_LEN - i);
if (scale < 0)
ccr1[i] = temp >> -scale;
else
ccr1[i] = av_clipl_int32(temp << scale);
}
for (i = 0; i < GRID_SIZE; i++) {
max = 0;
for (j = i; j < SUBFRAME_LEN; j += GRID_SIZE) {
temp = FFABS(ccr1[j]);
if (temp >= max) {
max = temp;
param.pulse_pos[0] = j;
}
}
amp = max;
min = 1 << 30;
max_amp_index = GAIN_LEVELS - 2;
for (j = max_amp_index; j >= 2; j--) {
temp = av_clipl_int32((int64_t) fixed_cb_gain[j] *
impulse_corr[0] << 1);
temp = FFABS(temp - amp);
if (temp < min) {
min = temp;
max_amp_index = j;
}
}
max_amp_index--;
for (j = 1; j < 5; j++) {
for (k = i; k < SUBFRAME_LEN; k += GRID_SIZE) {
temp_corr[k] = 0;
ccr2[k] = ccr1[k];
}
param.amp_index = max_amp_index + j - 2;
amp = fixed_cb_gain[param.amp_index];
param.pulse_sign[0] = (ccr2[param.pulse_pos[0]] < 0) ? -amp : amp;
temp_corr[param.pulse_pos[0]] = 1;
for (k = 1; k < pulse_cnt; k++) {
max = INT_MIN;
for (l = i; l < SUBFRAME_LEN; l += GRID_SIZE) {
if (temp_corr[l])
continue;
temp = impulse_corr[FFABS(l - param.pulse_pos[k - 1])];
temp = av_clipl_int32((int64_t) temp *
param.pulse_sign[k - 1] << 1);
ccr2[l] -= temp;
temp = FFABS(ccr2[l]);
if (temp > max) {
max = temp;
param.pulse_pos[k] = l;
}
}
param.pulse_sign[k] = (ccr2[param.pulse_pos[k]] < 0) ?
-amp : amp;
temp_corr[param.pulse_pos[k]] = 1;
}
memset(temp_corr, 0, sizeof(int16_t) * SUBFRAME_LEN);
for (k = 0; k < pulse_cnt; k++)
temp_corr[param.pulse_pos[k]] = param.pulse_sign[k];
for (k = SUBFRAME_LEN - 1; k >= 0; k--) {
temp = 0;
for (l = 0; l <= k; l++) {
int prod = av_clipl_int32((int64_t) temp_corr[l] *
impulse_r[k - l] << 1);
temp = av_clipl_int32(temp + prod);
}
temp_corr[k] = temp << 2 >> 16;
}
err = 0;
for (k = 0; k < SUBFRAME_LEN; k++) {
int64_t prod;
prod = av_clipl_int32((int64_t) buf[k] * temp_corr[k] << 1);
err = av_clipl_int32(err - prod);
prod = av_clipl_int32((int64_t) temp_corr[k] * temp_corr[k]);
err = av_clipl_int32(err + prod);
}
if (err < optim->min_err) {
optim->min_err = err;
optim->grid_index = i;
optim->amp_index = param.amp_index;
optim->dirac_train = param.dirac_train;
for (k = 0; k < pulse_cnt; k++) {
optim->pulse_sign[k] = param.pulse_sign[k];
optim->pulse_pos[k] = param.pulse_pos[k];
}
}
}
}
}
|
['static void get_fcb_param(FCBParam *optim, int16_t *impulse_resp,\n int16_t *buf, int pulse_cnt, int pitch_lag)\n{\n FCBParam param;\n int16_t impulse_r[SUBFRAME_LEN];\n int16_t temp_corr[SUBFRAME_LEN];\n int16_t impulse_corr[SUBFRAME_LEN];\n int ccr1[SUBFRAME_LEN];\n int ccr2[SUBFRAME_LEN];\n int amp, err, max, max_amp_index, min, scale, i, j, k, l;\n int64_t temp;\n memcpy(impulse_r, impulse_resp, sizeof(int16_t) * SUBFRAME_LEN);\n param.dirac_train = 0;\n if (pitch_lag < SUBFRAME_LEN - 2) {\n param.dirac_train = 1;\n ff_g723_1_gen_dirac_train(impulse_r, pitch_lag);\n }\n for (i = 0; i < SUBFRAME_LEN; i++)\n temp_corr[i] = impulse_r[i] >> 1;\n temp = ff_g723_1_dot_product(temp_corr, temp_corr, SUBFRAME_LEN);\n scale = ff_g723_1_normalize_bits(temp, 31);\n impulse_corr[0] = av_clipl_int32((temp << scale) + (1 << 15)) >> 16;\n for (i = 1; i < SUBFRAME_LEN; i++) {\n temp = ff_g723_1_dot_product(temp_corr + i, temp_corr,\n SUBFRAME_LEN - i);\n impulse_corr[i] = av_clipl_int32((temp << scale) + (1 << 15)) >> 16;\n }\n scale -= 4;\n for (i = 0; i < SUBFRAME_LEN; i++) {\n temp = ff_g723_1_dot_product(buf + i, impulse_r, SUBFRAME_LEN - i);\n if (scale < 0)\n ccr1[i] = temp >> -scale;\n else\n ccr1[i] = av_clipl_int32(temp << scale);\n }\n for (i = 0; i < GRID_SIZE; i++) {\n max = 0;\n for (j = i; j < SUBFRAME_LEN; j += GRID_SIZE) {\n temp = FFABS(ccr1[j]);\n if (temp >= max) {\n max = temp;\n param.pulse_pos[0] = j;\n }\n }\n amp = max;\n min = 1 << 30;\n max_amp_index = GAIN_LEVELS - 2;\n for (j = max_amp_index; j >= 2; j--) {\n temp = av_clipl_int32((int64_t) fixed_cb_gain[j] *\n impulse_corr[0] << 1);\n temp = FFABS(temp - amp);\n if (temp < min) {\n min = temp;\n max_amp_index = j;\n }\n }\n max_amp_index--;\n for (j = 1; j < 5; j++) {\n for (k = i; k < SUBFRAME_LEN; k += GRID_SIZE) {\n temp_corr[k] = 0;\n ccr2[k] = ccr1[k];\n }\n param.amp_index = max_amp_index + j - 2;\n amp = fixed_cb_gain[param.amp_index];\n param.pulse_sign[0] = (ccr2[param.pulse_pos[0]] < 0) ? -amp : amp;\n temp_corr[param.pulse_pos[0]] = 1;\n for (k = 1; k < pulse_cnt; k++) {\n max = INT_MIN;\n for (l = i; l < SUBFRAME_LEN; l += GRID_SIZE) {\n if (temp_corr[l])\n continue;\n temp = impulse_corr[FFABS(l - param.pulse_pos[k - 1])];\n temp = av_clipl_int32((int64_t) temp *\n param.pulse_sign[k - 1] << 1);\n ccr2[l] -= temp;\n temp = FFABS(ccr2[l]);\n if (temp > max) {\n max = temp;\n param.pulse_pos[k] = l;\n }\n }\n param.pulse_sign[k] = (ccr2[param.pulse_pos[k]] < 0) ?\n -amp : amp;\n temp_corr[param.pulse_pos[k]] = 1;\n }\n memset(temp_corr, 0, sizeof(int16_t) * SUBFRAME_LEN);\n for (k = 0; k < pulse_cnt; k++)\n temp_corr[param.pulse_pos[k]] = param.pulse_sign[k];\n for (k = SUBFRAME_LEN - 1; k >= 0; k--) {\n temp = 0;\n for (l = 0; l <= k; l++) {\n int prod = av_clipl_int32((int64_t) temp_corr[l] *\n impulse_r[k - l] << 1);\n temp = av_clipl_int32(temp + prod);\n }\n temp_corr[k] = temp << 2 >> 16;\n }\n err = 0;\n for (k = 0; k < SUBFRAME_LEN; k++) {\n int64_t prod;\n prod = av_clipl_int32((int64_t) buf[k] * temp_corr[k] << 1);\n err = av_clipl_int32(err - prod);\n prod = av_clipl_int32((int64_t) temp_corr[k] * temp_corr[k]);\n err = av_clipl_int32(err + prod);\n }\n if (err < optim->min_err) {\n optim->min_err = err;\n optim->grid_index = i;\n optim->amp_index = param.amp_index;\n optim->dirac_train = param.dirac_train;\n for (k = 0; k < pulse_cnt; k++) {\n optim->pulse_sign[k] = param.pulse_sign[k];\n optim->pulse_pos[k] = param.pulse_pos[k];\n }\n }\n }\n }\n}']
|
1,030 | 0 |
https://github.com/libav/libav/blob/9328adcc8012a1b0e00c465c85b5453589a4f5f7/libavformat/utils.c/#L2528
|
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 avformat_free_context(s);\n *ps = NULL;\n avio_close(pb);\n}', 'static void flush_packet_queue(AVFormatContext *s)\n{\n free_packet_buffer(&s->internal->parse_queue, &s->internal->parse_queue_end);\n free_packet_buffer(&s->internal->packet_buffer, &s->internal->packet_buffer_end);\n free_packet_buffer(&s->internal->raw_packet_buffer, &s->internal->raw_packet_buffer_end);\n s->internal->raw_packet_buffer_remaining_size = RAW_PACKET_BUFFER_SIZE;\n}', 'int avio_close(AVIOContext *s)\n{\n AVIOInternal *internal;\n URLContext *h;\n if (!s)\n return 0;\n avio_flush(s);\n internal = s->opaque;\n h = internal->h;\n av_opt_free(internal);\n av_freep(&internal->protocols);\n av_freep(&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}']
|
1,031 | 0 |
https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/motion_est_template.c/#L763
|
static int umh_search(MpegEncContext * s, int *best, int dmin,
int src_index, int ref_index, int const penalty_factor,
int size, int h, int flags)
{
MotionEstContext * const c= &s->me;
me_cmp_func cmpf, chroma_cmpf;
LOAD_COMMON
LOAD_COMMON2
int map_generation= c->map_generation;
int x,y,x2,y2, i, j, d;
const int dia_size= c->dia_size&0xFE;
static const int hex[16][2]={{-4,-2}, {-4,-1}, {-4, 0}, {-4, 1}, {-4, 2},
{ 4,-2}, { 4,-1}, { 4, 0}, { 4, 1}, { 4, 2},
{-2, 3}, { 0, 4}, { 2, 3},
{-2,-3}, { 0,-4}, { 2,-3},};
cmpf= s->dsp.me_cmp[size];
chroma_cmpf= s->dsp.me_cmp[size+1];
x= best[0];
y= best[1];
for(x2=FFMAX(x-dia_size+1, xmin); x2<=FFMIN(x+dia_size-1,xmax); x2+=2){
CHECK_MV(x2, y);
}
for(y2=FFMAX(y-dia_size/2+1, ymin); y2<=FFMIN(y+dia_size/2-1,ymax); y2+=2){
CHECK_MV(x, y2);
}
x= best[0];
y= best[1];
for(y2=FFMAX(y-2, ymin); y2<=FFMIN(y+2,ymax); y2++){
for(x2=FFMAX(x-2, xmin); x2<=FFMIN(x+2,xmax); x2++){
CHECK_MV(x2, y2);
}
}
for(j=1; j<=dia_size/4; j++){
for(i=0; i<16; i++){
CHECK_CLIPPED_MV(x+hex[i][0]*j, y+hex[i][1]*j);
}
}
return hex_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags, 2);
}
|
['static int umh_search(MpegEncContext * s, int *best, int dmin,\n int src_index, int ref_index, int const penalty_factor,\n int size, int h, int flags)\n{\n MotionEstContext * const c= &s->me;\n me_cmp_func cmpf, chroma_cmpf;\n LOAD_COMMON\n LOAD_COMMON2\n int map_generation= c->map_generation;\n int x,y,x2,y2, i, j, d;\n const int dia_size= c->dia_size&0xFE;\n static const int hex[16][2]={{-4,-2}, {-4,-1}, {-4, 0}, {-4, 1}, {-4, 2},\n { 4,-2}, { 4,-1}, { 4, 0}, { 4, 1}, { 4, 2},\n {-2, 3}, { 0, 4}, { 2, 3},\n {-2,-3}, { 0,-4}, { 2,-3},};\n cmpf= s->dsp.me_cmp[size];\n chroma_cmpf= s->dsp.me_cmp[size+1];\n x= best[0];\n y= best[1];\n for(x2=FFMAX(x-dia_size+1, xmin); x2<=FFMIN(x+dia_size-1,xmax); x2+=2){\n CHECK_MV(x2, y);\n }\n for(y2=FFMAX(y-dia_size/2+1, ymin); y2<=FFMIN(y+dia_size/2-1,ymax); y2+=2){\n CHECK_MV(x, y2);\n }\n x= best[0];\n y= best[1];\n for(y2=FFMAX(y-2, ymin); y2<=FFMIN(y+2,ymax); y2++){\n for(x2=FFMAX(x-2, xmin); x2<=FFMIN(x+2,xmax); x2++){\n CHECK_MV(x2, y2);\n }\n }\n for(j=1; j<=dia_size/4; j++){\n for(i=0; i<16; i++){\n CHECK_CLIPPED_MV(x+hex[i][0]*j, y+hex[i][1]*j);\n }\n }\n return hex_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags, 2);\n}']
|
1,032 | 0 |
https://github.com/nginx/nginx/blob/40a366c5a8927ad152eec2ab5e65c1a1f70354e4/src/core/ngx_hash.c/#L962
|
ngx_int_t
ngx_hash_add_key(ngx_hash_keys_arrays_t *ha, ngx_str_t *key, void *value,
ngx_uint_t flags)
{
size_t len;
u_char *p;
ngx_str_t *name;
ngx_uint_t i, k, n, skip, last;
ngx_array_t *keys, *hwc;
ngx_hash_key_t *hk;
last = key->len;
if (flags & NGX_HASH_WILDCARD_KEY) {
n = 0;
for (i = 0; i < key->len; i++) {
if (key->data[i] == '*') {
if (++n > 1) {
return NGX_DECLINED;
}
}
if (key->data[i] == '.' && key->data[i + 1] == '.') {
return NGX_DECLINED;
}
}
if (key->len > 1 && key->data[0] == '.') {
skip = 1;
goto wildcard;
}
if (key->len > 2) {
if (key->data[0] == '*' && key->data[1] == '.') {
skip = 2;
goto wildcard;
}
if (key->data[i - 2] == '.' && key->data[i - 1] == '*') {
skip = 0;
last -= 2;
goto wildcard;
}
}
if (n) {
return NGX_DECLINED;
}
}
k = 0;
for (i = 0; i < last; i++) {
if (!(flags & NGX_HASH_READONLY_KEY)) {
key->data[i] = ngx_tolower(key->data[i]);
}
k = ngx_hash(k, key->data[i]);
}
k %= ha->hsize;
name = ha->keys_hash[k].elts;
if (name) {
for (i = 0; i < ha->keys_hash[k].nelts; i++) {
if (last != name[i].len) {
continue;
}
if (ngx_strncmp(key->data, name[i].data, last) == 0) {
return NGX_BUSY;
}
}
} else {
if (ngx_array_init(&ha->keys_hash[k], ha->temp_pool, 4,
sizeof(ngx_str_t))
!= NGX_OK)
{
return NGX_ERROR;
}
}
name = ngx_array_push(&ha->keys_hash[k]);
if (name == NULL) {
return NGX_ERROR;
}
*name = *key;
hk = ngx_array_push(&ha->keys);
if (hk == NULL) {
return NGX_ERROR;
}
hk->key = *key;
hk->key_hash = ngx_hash_key(key->data, last);
hk->value = value;
return NGX_OK;
wildcard:
k = ngx_hash_strlow(&key->data[skip], &key->data[skip], last - skip);
k %= ha->hsize;
if (skip == 1) {
name = ha->keys_hash[k].elts;
if (name) {
len = last - skip;
for (i = 0; i < ha->keys_hash[k].nelts; i++) {
if (len != name[i].len) {
continue;
}
if (ngx_strncmp(&key->data[1], name[i].data, len) == 0) {
return NGX_BUSY;
}
}
} else {
if (ngx_array_init(&ha->keys_hash[k], ha->temp_pool, 4,
sizeof(ngx_str_t))
!= NGX_OK)
{
return NGX_ERROR;
}
}
name = ngx_array_push(&ha->keys_hash[k]);
if (name == NULL) {
return NGX_ERROR;
}
name->len = last - 1;
name->data = ngx_pnalloc(ha->temp_pool, name->len);
if (name->data == NULL) {
return NGX_ERROR;
}
ngx_memcpy(name->data, &key->data[1], name->len);
}
if (skip) {
p = ngx_pnalloc(ha->temp_pool, last);
if (p == NULL) {
return NGX_ERROR;
}
len = 0;
n = 0;
for (i = last - 1; i; i--) {
if (key->data[i] == '.') {
ngx_memcpy(&p[n], &key->data[i + 1], len);
n += len;
p[n++] = '.';
len = 0;
continue;
}
len++;
}
if (len) {
ngx_memcpy(&p[n], &key->data[1], len);
n += len;
}
p[n] = '\0';
hwc = &ha->dns_wc_head;
keys = &ha->dns_wc_head_hash[k];
} else {
last++;
p = ngx_pnalloc(ha->temp_pool, last);
if (p == NULL) {
return NGX_ERROR;
}
ngx_cpystrn(p, key->data, last);
hwc = &ha->dns_wc_tail;
keys = &ha->dns_wc_tail_hash[k];
}
name = keys->elts;
if (name) {
len = last - skip;
for (i = 0; i < keys->nelts; i++) {
if (len != name[i].len) {
continue;
}
if (ngx_strncmp(key->data + skip, name[i].data, len) == 0) {
return NGX_BUSY;
}
}
} else {
if (ngx_array_init(keys, ha->temp_pool, 4, sizeof(ngx_str_t)) != NGX_OK)
{
return NGX_ERROR;
}
}
name = ngx_array_push(keys);
if (name == NULL) {
return NGX_ERROR;
}
name->len = last - skip;
name->data = ngx_pnalloc(ha->temp_pool, name->len);
if (name->data == NULL) {
return NGX_ERROR;
}
ngx_memcpy(name->data, key->data + skip, name->len);
hk = ngx_array_push(hwc);
if (hk == NULL) {
return NGX_ERROR;
}
hk->key.len = last - 1;
hk->key.data = p;
hk->key_hash = 0;
hk->value = value;
return NGX_OK;
}
|
['static ngx_int_t\nngx_http_proxy_add_variables(ngx_conf_t *cf)\n{\n ngx_http_variable_t *var, *v;\n for (v = ngx_http_proxy_vars; v->name.len; v++) {\n var = ngx_http_add_variable(cf, &v->name, v->flags);\n if (var == NULL) {\n return NGX_ERROR;\n }\n var->get_handler = v->get_handler;\n var->data = v->data;\n }\n return NGX_OK;\n}', 'ngx_http_variable_t *\nngx_http_add_variable(ngx_conf_t *cf, ngx_str_t *name, ngx_uint_t flags)\n{\n ngx_int_t rc;\n ngx_uint_t i;\n ngx_hash_key_t *key;\n ngx_http_variable_t *v;\n ngx_http_core_main_conf_t *cmcf;\n cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);\n key = cmcf->variables_keys->keys.elts;\n for (i = 0; i < cmcf->variables_keys->keys.nelts; i++) {\n if (name->len != key[i].key.len\n || ngx_strncasecmp(name->data, key[i].key.data, name->len) != 0)\n {\n continue;\n }\n v = key[i].value;\n if (!(v->flags & NGX_HTTP_VAR_CHANGEABLE)) {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "the duplicate \\"%V\\" variable", name);\n return NULL;\n }\n return v;\n }\n v = ngx_palloc(cf->pool, sizeof(ngx_http_variable_t));\n if (v == NULL) {\n return NULL;\n }\n v->name.len = name->len;\n v->name.data = ngx_pnalloc(cf->pool, name->len);\n if (v->name.data == NULL) {\n return NULL;\n }\n ngx_strlow(v->name.data, name->data, name->len);\n v->set_handler = NULL;\n v->get_handler = NULL;\n v->data = 0;\n v->flags = flags;\n v->index = 0;\n rc = ngx_hash_add_key(cmcf->variables_keys, &v->name, v, 0);\n if (rc == NGX_ERROR) {\n return NULL;\n }\n if (rc == NGX_BUSY) {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "conflicting variable name \\"%V\\"", name);\n return NULL;\n }\n return v;\n}', "ngx_int_t\nngx_hash_add_key(ngx_hash_keys_arrays_t *ha, ngx_str_t *key, void *value,\n ngx_uint_t flags)\n{\n size_t len;\n u_char *p;\n ngx_str_t *name;\n ngx_uint_t i, k, n, skip, last;\n ngx_array_t *keys, *hwc;\n ngx_hash_key_t *hk;\n last = key->len;\n if (flags & NGX_HASH_WILDCARD_KEY) {\n n = 0;\n for (i = 0; i < key->len; i++) {\n if (key->data[i] == '*') {\n if (++n > 1) {\n return NGX_DECLINED;\n }\n }\n if (key->data[i] == '.' && key->data[i + 1] == '.') {\n return NGX_DECLINED;\n }\n }\n if (key->len > 1 && key->data[0] == '.') {\n skip = 1;\n goto wildcard;\n }\n if (key->len > 2) {\n if (key->data[0] == '*' && key->data[1] == '.') {\n skip = 2;\n goto wildcard;\n }\n if (key->data[i - 2] == '.' && key->data[i - 1] == '*') {\n skip = 0;\n last -= 2;\n goto wildcard;\n }\n }\n if (n) {\n return NGX_DECLINED;\n }\n }\n k = 0;\n for (i = 0; i < last; i++) {\n if (!(flags & NGX_HASH_READONLY_KEY)) {\n key->data[i] = ngx_tolower(key->data[i]);\n }\n k = ngx_hash(k, key->data[i]);\n }\n k %= ha->hsize;\n name = ha->keys_hash[k].elts;\n if (name) {\n for (i = 0; i < ha->keys_hash[k].nelts; i++) {\n if (last != name[i].len) {\n continue;\n }\n if (ngx_strncmp(key->data, name[i].data, last) == 0) {\n return NGX_BUSY;\n }\n }\n } else {\n if (ngx_array_init(&ha->keys_hash[k], ha->temp_pool, 4,\n sizeof(ngx_str_t))\n != NGX_OK)\n {\n return NGX_ERROR;\n }\n }\n name = ngx_array_push(&ha->keys_hash[k]);\n if (name == NULL) {\n return NGX_ERROR;\n }\n *name = *key;\n hk = ngx_array_push(&ha->keys);\n if (hk == NULL) {\n return NGX_ERROR;\n }\n hk->key = *key;\n hk->key_hash = ngx_hash_key(key->data, last);\n hk->value = value;\n return NGX_OK;\nwildcard:\n k = ngx_hash_strlow(&key->data[skip], &key->data[skip], last - skip);\n k %= ha->hsize;\n if (skip == 1) {\n name = ha->keys_hash[k].elts;\n if (name) {\n len = last - skip;\n for (i = 0; i < ha->keys_hash[k].nelts; i++) {\n if (len != name[i].len) {\n continue;\n }\n if (ngx_strncmp(&key->data[1], name[i].data, len) == 0) {\n return NGX_BUSY;\n }\n }\n } else {\n if (ngx_array_init(&ha->keys_hash[k], ha->temp_pool, 4,\n sizeof(ngx_str_t))\n != NGX_OK)\n {\n return NGX_ERROR;\n }\n }\n name = ngx_array_push(&ha->keys_hash[k]);\n if (name == NULL) {\n return NGX_ERROR;\n }\n name->len = last - 1;\n name->data = ngx_pnalloc(ha->temp_pool, name->len);\n if (name->data == NULL) {\n return NGX_ERROR;\n }\n ngx_memcpy(name->data, &key->data[1], name->len);\n }\n if (skip) {\n p = ngx_pnalloc(ha->temp_pool, last);\n if (p == NULL) {\n return NGX_ERROR;\n }\n len = 0;\n n = 0;\n for (i = last - 1; i; i--) {\n if (key->data[i] == '.') {\n ngx_memcpy(&p[n], &key->data[i + 1], len);\n n += len;\n p[n++] = '.';\n len = 0;\n continue;\n }\n len++;\n }\n if (len) {\n ngx_memcpy(&p[n], &key->data[1], len);\n n += len;\n }\n p[n] = '\\0';\n hwc = &ha->dns_wc_head;\n keys = &ha->dns_wc_head_hash[k];\n } else {\n last++;\n p = ngx_pnalloc(ha->temp_pool, last);\n if (p == NULL) {\n return NGX_ERROR;\n }\n ngx_cpystrn(p, key->data, last);\n hwc = &ha->dns_wc_tail;\n keys = &ha->dns_wc_tail_hash[k];\n }\n name = keys->elts;\n if (name) {\n len = last - skip;\n for (i = 0; i < keys->nelts; i++) {\n if (len != name[i].len) {\n continue;\n }\n if (ngx_strncmp(key->data + skip, name[i].data, len) == 0) {\n return NGX_BUSY;\n }\n }\n } else {\n if (ngx_array_init(keys, ha->temp_pool, 4, sizeof(ngx_str_t)) != NGX_OK)\n {\n return NGX_ERROR;\n }\n }\n name = ngx_array_push(keys);\n if (name == NULL) {\n return NGX_ERROR;\n }\n name->len = last - skip;\n name->data = ngx_pnalloc(ha->temp_pool, name->len);\n if (name->data == NULL) {\n return NGX_ERROR;\n }\n ngx_memcpy(name->data, key->data + skip, name->len);\n hk = ngx_array_push(hwc);\n if (hk == NULL) {\n return NGX_ERROR;\n }\n hk->key.len = last - 1;\n hk->key.data = p;\n hk->key_hash = 0;\n hk->value = value;\n return NGX_OK;\n}"]
|
1,033 | 0 |
https://github.com/libav/libav/blob/e652cc9606068189cb512a36f0335a5cf2ecf287/libavcodec/bmp.c/#L166
|
static int bmp_decode_frame(AVCodecContext *avctx,
void *data, int *data_size,
AVPacket *avpkt)
{
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size;
BMPContext *s = avctx->priv_data;
AVFrame *picture = data;
AVFrame *p = &s->picture;
unsigned int fsize, hsize;
int width, height;
unsigned int depth;
BiCompression comp;
unsigned int ihsize;
int i, j, n, linesize;
uint32_t rgb[3];
uint8_t *ptr;
int dsize;
const uint8_t *buf0 = buf;
if(buf_size < 14){
av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size);
return -1;
}
if(bytestream_get_byte(&buf) != 'B' ||
bytestream_get_byte(&buf) != 'M') {
av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
return -1;
}
fsize = bytestream_get_le32(&buf);
if(buf_size < fsize){
av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d), trying to decode anyway\n",
buf_size, fsize);
fsize = buf_size;
}
buf += 2;
buf += 2;
hsize = bytestream_get_le32(&buf);
ihsize = bytestream_get_le32(&buf);
if(ihsize + 14 > hsize){
av_log(avctx, AV_LOG_ERROR, "invalid header size %d\n", hsize);
return -1;
}
if(fsize == 14 || fsize == ihsize + 14)
fsize = buf_size - 2;
if(fsize <= hsize){
av_log(avctx, AV_LOG_ERROR, "declared file size is less than header size (%d < %d)\n",
fsize, hsize);
return -1;
}
switch(ihsize){
case 40:
case 64:
case 108:
case 124:
width = bytestream_get_le32(&buf);
height = bytestream_get_le32(&buf);
break;
case 12:
width = bytestream_get_le16(&buf);
height = bytestream_get_le16(&buf);
break;
default:
av_log(avctx, AV_LOG_ERROR, "unsupported BMP file, patch welcome\n");
return -1;
}
if(bytestream_get_le16(&buf) != 1){
av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
return -1;
}
depth = bytestream_get_le16(&buf);
if(ihsize == 40)
comp = bytestream_get_le32(&buf);
else
comp = BMP_RGB;
if(comp != BMP_RGB && comp != BMP_BITFIELDS && comp != BMP_RLE4 && comp != BMP_RLE8){
av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
return -1;
}
if(comp == BMP_BITFIELDS){
buf += 20;
rgb[0] = bytestream_get_le32(&buf);
rgb[1] = bytestream_get_le32(&buf);
rgb[2] = bytestream_get_le32(&buf);
}
avctx->width = width;
avctx->height = height > 0? height: -height;
avctx->pix_fmt = PIX_FMT_NONE;
switch(depth){
case 32:
if(comp == BMP_BITFIELDS){
rgb[0] = (rgb[0] >> 15) & 3;
rgb[1] = (rgb[1] >> 15) & 3;
rgb[2] = (rgb[2] >> 15) & 3;
if(rgb[0] + rgb[1] + rgb[2] != 3 ||
rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]){
break;
}
} else {
rgb[0] = 2;
rgb[1] = 1;
rgb[2] = 0;
}
avctx->pix_fmt = PIX_FMT_BGR24;
break;
case 24:
avctx->pix_fmt = PIX_FMT_BGR24;
break;
case 16:
if(comp == BMP_RGB)
avctx->pix_fmt = PIX_FMT_RGB555;
else if (comp == BMP_BITFIELDS) {
if (rgb[0] == 0xF800 && rgb[1] == 0x07E0 && rgb[2] == 0x001F)
avctx->pix_fmt = PIX_FMT_RGB565;
else if (rgb[0] == 0x7C00 && rgb[1] == 0x03E0 && rgb[2] == 0x001F)
avctx->pix_fmt = PIX_FMT_RGB555;
else if (rgb[0] == 0x0F00 && rgb[1] == 0x00F0 && rgb[2] == 0x000F)
avctx->pix_fmt = PIX_FMT_RGB444;
else {
av_log(avctx, AV_LOG_ERROR, "Unknown bitfields %0X %0X %0X\n", rgb[0], rgb[1], rgb[2]);
return AVERROR(EINVAL);
}
}
break;
case 8:
if(hsize - ihsize - 14 > 0)
avctx->pix_fmt = PIX_FMT_PAL8;
else
avctx->pix_fmt = PIX_FMT_GRAY8;
break;
case 1:
case 4:
if(hsize - ihsize - 14 > 0){
avctx->pix_fmt = PIX_FMT_PAL8;
}else{
av_log(avctx, AV_LOG_ERROR, "Unknown palette for %d-colour BMP\n", 1<<depth);
return -1;
}
break;
default:
av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth);
return -1;
}
if(avctx->pix_fmt == PIX_FMT_NONE){
av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
return -1;
}
if(p->data[0])
avctx->release_buffer(avctx, p);
p->reference = 0;
if(avctx->get_buffer(avctx, p) < 0){
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
return -1;
}
p->pict_type = AV_PICTURE_TYPE_I;
p->key_frame = 1;
buf = buf0 + hsize;
dsize = buf_size - hsize;
n = ((avctx->width * depth) / 8 + 3) & ~3;
if(n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8){
av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
dsize, n * avctx->height);
return -1;
}
if(comp == BMP_RLE4 || comp == BMP_RLE8)
memset(p->data[0], 0, avctx->height * p->linesize[0]);
if(depth == 4 || depth == 8)
memset(p->data[1], 0, 1024);
if(height > 0){
ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
linesize = -p->linesize[0];
} else {
ptr = p->data[0];
linesize = p->linesize[0];
}
if(avctx->pix_fmt == PIX_FMT_PAL8){
int colors = 1 << depth;
if(ihsize >= 36){
int t;
buf = buf0 + 46;
t = bytestream_get_le32(&buf);
if(t < 0 || t > (1 << depth)){
av_log(avctx, AV_LOG_ERROR, "Incorrect number of colors - %X for bitdepth %d\n", t, depth);
}else if(t){
colors = t;
}
}
buf = buf0 + 14 + ihsize;
if((hsize-ihsize-14) < (colors << 2)){
for(i = 0; i < colors; i++)
((uint32_t*)p->data[1])[i] = bytestream_get_le24(&buf);
}else{
for(i = 0; i < colors; i++)
((uint32_t*)p->data[1])[i] = bytestream_get_le32(&buf);
}
buf = buf0 + hsize;
}
if(comp == BMP_RLE4 || comp == BMP_RLE8){
if(height < 0){
p->data[0] += p->linesize[0] * (avctx->height - 1);
p->linesize[0] = -p->linesize[0];
}
ff_msrle_decode(avctx, (AVPicture*)p, depth, buf, dsize);
if(height < 0){
p->data[0] += p->linesize[0] * (avctx->height - 1);
p->linesize[0] = -p->linesize[0];
}
}else{
switch(depth){
case 1:
for (i = 0; i < avctx->height; i++) {
int j;
for (j = 0; j < n; j++) {
ptr[j*8+0] = buf[j] >> 7;
ptr[j*8+1] = (buf[j] >> 6) & 1;
ptr[j*8+2] = (buf[j] >> 5) & 1;
ptr[j*8+3] = (buf[j] >> 4) & 1;
ptr[j*8+4] = (buf[j] >> 3) & 1;
ptr[j*8+5] = (buf[j] >> 2) & 1;
ptr[j*8+6] = (buf[j] >> 1) & 1;
ptr[j*8+7] = buf[j] & 1;
}
buf += n;
ptr += linesize;
}
break;
case 8:
case 24:
for(i = 0; i < avctx->height; i++){
memcpy(ptr, buf, n);
buf += n;
ptr += linesize;
}
break;
case 4:
for(i = 0; i < avctx->height; i++){
int j;
for(j = 0; j < n; j++){
ptr[j*2+0] = (buf[j] >> 4) & 0xF;
ptr[j*2+1] = buf[j] & 0xF;
}
buf += n;
ptr += linesize;
}
break;
case 16:
for(i = 0; i < avctx->height; i++){
const uint16_t *src = (const uint16_t *) buf;
uint16_t *dst = (uint16_t *) ptr;
for(j = 0; j < avctx->width; j++)
*dst++ = av_le2ne16(*src++);
buf += n;
ptr += linesize;
}
break;
case 32:
for(i = 0; i < avctx->height; i++){
const uint8_t *src = buf;
uint8_t *dst = ptr;
for(j = 0; j < avctx->width; j++){
dst[0] = src[rgb[2]];
dst[1] = src[rgb[1]];
dst[2] = src[rgb[0]];
dst += 3;
src += 4;
}
buf += n;
ptr += linesize;
}
break;
default:
av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
return -1;
}
}
*picture = s->picture;
*data_size = sizeof(AVPicture);
return buf_size;
}
|
['static int bmp_decode_frame(AVCodecContext *avctx,\n void *data, int *data_size,\n AVPacket *avpkt)\n{\n const uint8_t *buf = avpkt->data;\n int buf_size = avpkt->size;\n BMPContext *s = avctx->priv_data;\n AVFrame *picture = data;\n AVFrame *p = &s->picture;\n unsigned int fsize, hsize;\n int width, height;\n unsigned int depth;\n BiCompression comp;\n unsigned int ihsize;\n int i, j, n, linesize;\n uint32_t rgb[3];\n uint8_t *ptr;\n int dsize;\n const uint8_t *buf0 = buf;\n if(buf_size < 14){\n av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\\n", buf_size);\n return -1;\n }\n if(bytestream_get_byte(&buf) != \'B\' ||\n bytestream_get_byte(&buf) != \'M\') {\n av_log(avctx, AV_LOG_ERROR, "bad magic number\\n");\n return -1;\n }\n fsize = bytestream_get_le32(&buf);\n if(buf_size < fsize){\n av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d), trying to decode anyway\\n",\n buf_size, fsize);\n fsize = buf_size;\n }\n buf += 2;\n buf += 2;\n hsize = bytestream_get_le32(&buf);\n ihsize = bytestream_get_le32(&buf);\n if(ihsize + 14 > hsize){\n av_log(avctx, AV_LOG_ERROR, "invalid header size %d\\n", hsize);\n return -1;\n }\n if(fsize == 14 || fsize == ihsize + 14)\n fsize = buf_size - 2;\n if(fsize <= hsize){\n av_log(avctx, AV_LOG_ERROR, "declared file size is less than header size (%d < %d)\\n",\n fsize, hsize);\n return -1;\n }\n switch(ihsize){\n case 40:\n case 64:\n case 108:\n case 124:\n width = bytestream_get_le32(&buf);\n height = bytestream_get_le32(&buf);\n break;\n case 12:\n width = bytestream_get_le16(&buf);\n height = bytestream_get_le16(&buf);\n break;\n default:\n av_log(avctx, AV_LOG_ERROR, "unsupported BMP file, patch welcome\\n");\n return -1;\n }\n if(bytestream_get_le16(&buf) != 1){\n av_log(avctx, AV_LOG_ERROR, "invalid BMP header\\n");\n return -1;\n }\n depth = bytestream_get_le16(&buf);\n if(ihsize == 40)\n comp = bytestream_get_le32(&buf);\n else\n comp = BMP_RGB;\n if(comp != BMP_RGB && comp != BMP_BITFIELDS && comp != BMP_RLE4 && comp != BMP_RLE8){\n av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\\n", comp);\n return -1;\n }\n if(comp == BMP_BITFIELDS){\n buf += 20;\n rgb[0] = bytestream_get_le32(&buf);\n rgb[1] = bytestream_get_le32(&buf);\n rgb[2] = bytestream_get_le32(&buf);\n }\n avctx->width = width;\n avctx->height = height > 0? height: -height;\n avctx->pix_fmt = PIX_FMT_NONE;\n switch(depth){\n case 32:\n if(comp == BMP_BITFIELDS){\n rgb[0] = (rgb[0] >> 15) & 3;\n rgb[1] = (rgb[1] >> 15) & 3;\n rgb[2] = (rgb[2] >> 15) & 3;\n if(rgb[0] + rgb[1] + rgb[2] != 3 ||\n rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]){\n break;\n }\n } else {\n rgb[0] = 2;\n rgb[1] = 1;\n rgb[2] = 0;\n }\n avctx->pix_fmt = PIX_FMT_BGR24;\n break;\n case 24:\n avctx->pix_fmt = PIX_FMT_BGR24;\n break;\n case 16:\n if(comp == BMP_RGB)\n avctx->pix_fmt = PIX_FMT_RGB555;\n else if (comp == BMP_BITFIELDS) {\n if (rgb[0] == 0xF800 && rgb[1] == 0x07E0 && rgb[2] == 0x001F)\n avctx->pix_fmt = PIX_FMT_RGB565;\n else if (rgb[0] == 0x7C00 && rgb[1] == 0x03E0 && rgb[2] == 0x001F)\n avctx->pix_fmt = PIX_FMT_RGB555;\n else if (rgb[0] == 0x0F00 && rgb[1] == 0x00F0 && rgb[2] == 0x000F)\n avctx->pix_fmt = PIX_FMT_RGB444;\n else {\n av_log(avctx, AV_LOG_ERROR, "Unknown bitfields %0X %0X %0X\\n", rgb[0], rgb[1], rgb[2]);\n return AVERROR(EINVAL);\n }\n }\n break;\n case 8:\n if(hsize - ihsize - 14 > 0)\n avctx->pix_fmt = PIX_FMT_PAL8;\n else\n avctx->pix_fmt = PIX_FMT_GRAY8;\n break;\n case 1:\n case 4:\n if(hsize - ihsize - 14 > 0){\n avctx->pix_fmt = PIX_FMT_PAL8;\n }else{\n av_log(avctx, AV_LOG_ERROR, "Unknown palette for %d-colour BMP\\n", 1<<depth);\n return -1;\n }\n break;\n default:\n av_log(avctx, AV_LOG_ERROR, "depth %d not supported\\n", depth);\n return -1;\n }\n if(avctx->pix_fmt == PIX_FMT_NONE){\n av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\\n");\n return -1;\n }\n if(p->data[0])\n avctx->release_buffer(avctx, p);\n p->reference = 0;\n if(avctx->get_buffer(avctx, p) < 0){\n av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n return -1;\n }\n p->pict_type = AV_PICTURE_TYPE_I;\n p->key_frame = 1;\n buf = buf0 + hsize;\n dsize = buf_size - hsize;\n n = ((avctx->width * depth) / 8 + 3) & ~3;\n if(n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8){\n av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\\n",\n dsize, n * avctx->height);\n return -1;\n }\n if(comp == BMP_RLE4 || comp == BMP_RLE8)\n memset(p->data[0], 0, avctx->height * p->linesize[0]);\n if(depth == 4 || depth == 8)\n memset(p->data[1], 0, 1024);\n if(height > 0){\n ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];\n linesize = -p->linesize[0];\n } else {\n ptr = p->data[0];\n linesize = p->linesize[0];\n }\n if(avctx->pix_fmt == PIX_FMT_PAL8){\n int colors = 1 << depth;\n if(ihsize >= 36){\n int t;\n buf = buf0 + 46;\n t = bytestream_get_le32(&buf);\n if(t < 0 || t > (1 << depth)){\n av_log(avctx, AV_LOG_ERROR, "Incorrect number of colors - %X for bitdepth %d\\n", t, depth);\n }else if(t){\n colors = t;\n }\n }\n buf = buf0 + 14 + ihsize;\n if((hsize-ihsize-14) < (colors << 2)){\n for(i = 0; i < colors; i++)\n ((uint32_t*)p->data[1])[i] = bytestream_get_le24(&buf);\n }else{\n for(i = 0; i < colors; i++)\n ((uint32_t*)p->data[1])[i] = bytestream_get_le32(&buf);\n }\n buf = buf0 + hsize;\n }\n if(comp == BMP_RLE4 || comp == BMP_RLE8){\n if(height < 0){\n p->data[0] += p->linesize[0] * (avctx->height - 1);\n p->linesize[0] = -p->linesize[0];\n }\n ff_msrle_decode(avctx, (AVPicture*)p, depth, buf, dsize);\n if(height < 0){\n p->data[0] += p->linesize[0] * (avctx->height - 1);\n p->linesize[0] = -p->linesize[0];\n }\n }else{\n switch(depth){\n case 1:\n for (i = 0; i < avctx->height; i++) {\n int j;\n for (j = 0; j < n; j++) {\n ptr[j*8+0] = buf[j] >> 7;\n ptr[j*8+1] = (buf[j] >> 6) & 1;\n ptr[j*8+2] = (buf[j] >> 5) & 1;\n ptr[j*8+3] = (buf[j] >> 4) & 1;\n ptr[j*8+4] = (buf[j] >> 3) & 1;\n ptr[j*8+5] = (buf[j] >> 2) & 1;\n ptr[j*8+6] = (buf[j] >> 1) & 1;\n ptr[j*8+7] = buf[j] & 1;\n }\n buf += n;\n ptr += linesize;\n }\n break;\n case 8:\n case 24:\n for(i = 0; i < avctx->height; i++){\n memcpy(ptr, buf, n);\n buf += n;\n ptr += linesize;\n }\n break;\n case 4:\n for(i = 0; i < avctx->height; i++){\n int j;\n for(j = 0; j < n; j++){\n ptr[j*2+0] = (buf[j] >> 4) & 0xF;\n ptr[j*2+1] = buf[j] & 0xF;\n }\n buf += n;\n ptr += linesize;\n }\n break;\n case 16:\n for(i = 0; i < avctx->height; i++){\n const uint16_t *src = (const uint16_t *) buf;\n uint16_t *dst = (uint16_t *) ptr;\n for(j = 0; j < avctx->width; j++)\n *dst++ = av_le2ne16(*src++);\n buf += n;\n ptr += linesize;\n }\n break;\n case 32:\n for(i = 0; i < avctx->height; i++){\n const uint8_t *src = buf;\n uint8_t *dst = ptr;\n for(j = 0; j < avctx->width; j++){\n dst[0] = src[rgb[2]];\n dst[1] = src[rgb[1]];\n dst[2] = src[rgb[0]];\n dst += 3;\n src += 4;\n }\n buf += n;\n ptr += linesize;\n }\n break;\n default:\n av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\\n");\n return -1;\n }\n }\n *picture = s->picture;\n *data_size = sizeof(AVPicture);\n return buf_size;\n}']
|
1,034 | 0 |
https://github.com/openssl/openssl/blob/8b0d4242404f9e5da26e7594fa0864b2df4601af/crypto/bn/bn_lib.c/#L271
|
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
BN_ULONG *a = NULL;
bn_check_top(b);
if (words > (INT_MAX / (4 * BN_BITS2))) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
return NULL;
}
if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
return (NULL);
}
if (BN_get_flags(b, BN_FLG_SECURE))
a = OPENSSL_secure_zalloc(words * sizeof(*a));
else
a = OPENSSL_zalloc(words * sizeof(*a));
if (a == NULL) {
BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
return (NULL);
}
assert(b->top <= words);
if (b->top > 0)
memcpy(a, b->d, sizeof(*a) * b->top);
return a;
}
|
['int ec_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 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 = 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}']
|
1,035 | 0 |
https://github.com/openssl/openssl/blob/630fe1da888490b7dfef3fe0928b813ddff5d51a/crypto/ec/ec_mult.c/#L446
|
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 (!ec_point_is_compat(r, group)) {
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);
}
if ((scalar != NULL) && (num == 0)) {
return ec_mul_consttime(group, r, scalar, NULL, ctx);
}
if ((scalar == NULL) && (num == 1)) {
return ec_mul_consttime(group, r, scalars[0], points[0], ctx);
}
for (i = 0; i < num; i++) {
if (!ec_point_is_compat(points[i], group)) {
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 test_sm2_crypt(const EC_GROUP *group,\n const EVP_MD *digest,\n const char *privkey_hex,\n const char *message,\n const char *k_hex, const char *ctext_hex)\n{\n const size_t msg_len = strlen(message);\n BIGNUM *priv = NULL;\n EC_KEY *key = NULL;\n EC_POINT *pt = NULL;\n unsigned char *expected = OPENSSL_hexstr2buf(ctext_hex, NULL);\n size_t ctext_len = 0;\n size_t ptext_len = 0;\n uint8_t *ctext = NULL;\n uint8_t *recovered = NULL;\n size_t recovered_len = msg_len;\n int rc = 0;\n if (!TEST_ptr(expected)\n || !TEST_true(BN_hex2bn(&priv, privkey_hex)))\n goto done;\n key = EC_KEY_new();\n if (!TEST_ptr(key)\n || !TEST_true(EC_KEY_set_group(key, group))\n || !TEST_true(EC_KEY_set_private_key(key, priv)))\n goto done;\n pt = EC_POINT_new(group);\n if (!TEST_ptr(pt)\n || !TEST_true(EC_POINT_mul(group, pt, priv, NULL, NULL, NULL))\n || !TEST_true(EC_KEY_set_public_key(key, pt))\n || !TEST_true(sm2_ciphertext_size(key, digest, msg_len, &ctext_len)))\n goto done;\n ctext = OPENSSL_zalloc(ctext_len);\n if (!TEST_ptr(ctext))\n goto done;\n start_fake_rand(k_hex);\n if (!TEST_true(sm2_encrypt(key, digest, (const uint8_t *)message, msg_len,\n ctext, &ctext_len))) {\n restore_rand();\n goto done;\n }\n restore_rand();\n if (!TEST_mem_eq(ctext, ctext_len, expected, ctext_len))\n goto done;\n if (!TEST_true(sm2_plaintext_size(key, digest, ctext_len, &ptext_len))\n || !TEST_int_eq(ptext_len, msg_len))\n goto done;\n recovered = OPENSSL_zalloc(ptext_len);\n if (!TEST_ptr(recovered)\n || !TEST_true(sm2_decrypt(key, digest, ctext, ctext_len, recovered, &recovered_len))\n || !TEST_int_eq(recovered_len, msg_len)\n || !TEST_mem_eq(recovered, recovered_len, message, msg_len))\n goto done;\n rc = 1;\n done:\n BN_free(priv);\n EC_POINT_free(pt);\n OPENSSL_free(ctext);\n OPENSSL_free(recovered);\n OPENSSL_free(expected);\n EC_KEY_free(key);\n return rc;\n}', 'int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,\n const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)\n{\n const EC_POINT *points[1];\n const BIGNUM *scalars[1];\n points[0] = point;\n scalars[0] = p_scalar;\n return EC_POINTs_mul(group, r, g_scalar,\n (point != NULL\n && p_scalar != NULL), points, scalars, ctx);\n}', 'int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,\n size_t num, const EC_POINT *points[],\n const BIGNUM *scalars[], BN_CTX *ctx)\n{\n 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 (!ec_point_is_compat(r, group)) {\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 if ((scalar != NULL) && (num == 0)) {\n return ec_mul_consttime(group, r, scalar, NULL, ctx);\n }\n if ((scalar == NULL) && (num == 1)) {\n return ec_mul_consttime(group, r, scalars[0], points[0], ctx);\n }\n for (i = 0; i < num; i++) {\n if (!ec_point_is_compat(points[i], group)) {\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}']
|
1,036 | 0 |
https://github.com/libav/libav/blob/dc26318c2dd05893843147d8c5b169bd2f498c61/ffmpeg.c/#L3126
|
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);
exit_program(1);
}
if(codec->type != type) {
fprintf(stderr, "Invalid %s type '%s'\n", codec_string, name);
exit_program(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 exit_program(1);\n }\n if(codec->type != type) {\n fprintf(stderr, "Invalid %s type \'%s\'\\n", codec_string, name);\n exit_program(1);\n }\n return codec->id;\n}']
|
1,037 | 0 |
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/bn/bn_lib.c/#L352
|
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
BN_ULONG *A, *a = NULL;
const BN_ULONG *B;
int i;
bn_check_top(b);
if (words > (INT_MAX / (4 * BN_BITS2))) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
return NULL;
}
if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
return (NULL);
}
if (BN_get_flags(b,BN_FLG_SECURE))
a = A = OPENSSL_secure_malloc(words * sizeof(*a));
else
a = A = OPENSSL_malloc(words * sizeof(*a));
if (A == NULL) {
BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
return (NULL);
}
#ifdef PURIFY
memset(a, 0, sizeof(*a) * words);
#endif
#if 1
B = b->d;
if (B != NULL) {
for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {
BN_ULONG a0, a1, a2, a3;
a0 = B[0];
a1 = B[1];
a2 = B[2];
a3 = B[3];
A[0] = a0;
A[1] = a1;
A[2] = a2;
A[3] = a3;
}
switch (b->top & 3) {
case 3:
A[2] = B[2];
case 2:
A[1] = B[1];
case 1:
A[0] = B[0];
case 0:
;
}
}
#else
memset(A, 0, sizeof(*A) * words);
memcpy(A, b->d, sizeof(b->d[0]) * b->top);
#endif
return (a);
}
|
['static int probable_prime_dh_safe(BIGNUM *p, int bits, const BIGNUM *padd,\n const BIGNUM *rem, BN_CTX *ctx)\n{\n int i, ret = 0;\n BIGNUM *t1, *qadd, *q;\n bits--;\n BN_CTX_start(ctx);\n t1 = BN_CTX_get(ctx);\n q = BN_CTX_get(ctx);\n qadd = BN_CTX_get(ctx);\n if (qadd == NULL)\n goto err;\n if (!BN_rshift1(qadd, padd))\n goto err;\n if (!BN_rand(q, bits, 0, 1))\n goto err;\n if (!BN_mod(t1, q, qadd, ctx))\n goto err;\n if (!BN_sub(q, q, t1))\n goto err;\n if (rem == NULL) {\n if (!BN_add_word(q, 1))\n goto err;\n } else {\n if (!BN_rshift1(t1, rem))\n goto err;\n if (!BN_add(q, q, t1))\n goto err;\n }\n if (!BN_lshift1(p, q))\n goto err;\n if (!BN_add_word(p, 1))\n goto err;\n loop:\n for (i = 1; i < NUMPRIMES; i++) {\n if ((BN_mod_word(p, (BN_ULONG)primes[i]) == 0) ||\n (BN_mod_word(q, (BN_ULONG)primes[i]) == 0)) {\n if (!BN_add(p, p, padd))\n goto err;\n if (!BN_add(q, q, qadd))\n goto err;\n goto loop;\n }\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n bn_check_top(p);\n return (ret);\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return (1);\n}', 'int BN_rshift1(BIGNUM *r, const BIGNUM *a)\n{\n BN_ULONG *ap, *rp, t, c;\n int i, j;\n bn_check_top(r);\n bn_check_top(a);\n if (BN_is_zero(a)) {\n BN_zero(r);\n return (1);\n }\n i = a->top;\n ap = a->d;\n j = i - (ap[i - 1] == 1);\n if (a != r) {\n if (bn_wexpand(r, j) == NULL)\n return (0);\n r->neg = a->neg;\n }\n rp = r->d;\n t = ap[--i];\n c = (t & 1) ? BN_TBIT : 0;\n if (t >>= 1)\n rp[i] = t;\n while (i > 0) {\n t = ap[--i];\n rp[i] = ((t >> 1) & BN_MASK2) | c;\n c = (t & 1) ? BN_TBIT : 0;\n }\n r->top = j;\n bn_check_top(r);\n return (1);\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *A, *a = NULL;\n const BN_ULONG *B;\n int i;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return (NULL);\n }\n if (BN_get_flags(b,BN_FLG_SECURE))\n a = A = OPENSSL_secure_malloc(words * sizeof(*a));\n else\n a = A = OPENSSL_malloc(words * sizeof(*a));\n if (A == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n#ifdef PURIFY\n memset(a, 0, sizeof(*a) * words);\n#endif\n#if 1\n B = b->d;\n if (B != NULL) {\n for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {\n BN_ULONG a0, a1, a2, a3;\n a0 = B[0];\n a1 = B[1];\n a2 = B[2];\n a3 = B[3];\n A[0] = a0;\n A[1] = a1;\n A[2] = a2;\n A[3] = a3;\n }\n switch (b->top & 3) {\n case 3:\n A[2] = B[2];\n case 2:\n A[1] = B[1];\n case 1:\n A[0] = B[0];\n case 0:\n ;\n }\n }\n#else\n memset(A, 0, sizeof(*A) * words);\n memcpy(A, b->d, sizeof(b->d[0]) * b->top);\n#endif\n return (a);\n}']
|
1,038 | 0 |
https://github.com/libav/libav/blob/9d5ec50ead97e088d77317e77b18cef06cb3d053/libavcodec/metasound.c/#L316
|
static av_cold int metasound_decode_init(AVCodecContext *avctx)
{
int isampf, ibps;
TwinVQContext *tctx = avctx->priv_data;
uint32_t tag;
const MetasoundProps *props = codec_props;
if (!avctx->extradata || avctx->extradata_size < 16) {
av_log(avctx, AV_LOG_ERROR, "Missing or incomplete extradata\n");
return AVERROR_INVALIDDATA;
}
tag = AV_RL32(avctx->extradata + 12);
for (;;) {
if (!props->tag) {
av_log(avctx, AV_LOG_ERROR, "Could not find tag %08X\n", tag);
return AVERROR_INVALIDDATA;
}
if (props->tag == tag) {
avctx->sample_rate = props->sample_rate;
avctx->channels = props->channels;
avctx->bit_rate = props->bit_rate * 1000;
isampf = avctx->sample_rate / 1000;
break;
}
props++;
}
if (avctx->channels <= 0 || avctx->channels > TWINVQ_CHANNELS_MAX) {
av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels: %i\n",
avctx->channels);
return AVERROR_INVALIDDATA;
}
avctx->channel_layout = avctx->channels == 1 ? AV_CH_LAYOUT_MONO
: AV_CH_LAYOUT_STEREO;
ibps = avctx->bit_rate / (1000 * avctx->channels);
switch ((avctx->channels << 16) + (isampf << 8) + ibps) {
case (1 << 16) + ( 8 << 8) + 8:
tctx->mtab = &ff_metasound_mode0808;
break;
case (1 << 16) + (16 << 8) + 16:
tctx->mtab = &ff_metasound_mode1616;
break;
case (1 << 16) + (44 << 8) + 32:
tctx->mtab = &ff_metasound_mode4432;
break;
case (2 << 16) + (44 << 8) + 48:
tctx->mtab = &ff_metasound_mode4448s;
break;
default:
av_log(avctx, AV_LOG_ERROR,
"This version does not support %d kHz - %d kbit/s/ch mode.\n",
isampf, isampf);
return AVERROR(ENOSYS);
}
tctx->codec = TWINVQ_CODEC_METASOUND;
tctx->read_bitstream = metasound_read_bitstream;
tctx->dec_bark_env = dec_bark_env;
tctx->decode_ppc = decode_ppc;
return ff_twinvq_decode_init(avctx);
}
|
['static av_cold int metasound_decode_init(AVCodecContext *avctx)\n{\n int isampf, ibps;\n TwinVQContext *tctx = avctx->priv_data;\n uint32_t tag;\n const MetasoundProps *props = codec_props;\n if (!avctx->extradata || avctx->extradata_size < 16) {\n av_log(avctx, AV_LOG_ERROR, "Missing or incomplete extradata\\n");\n return AVERROR_INVALIDDATA;\n }\n tag = AV_RL32(avctx->extradata + 12);\n for (;;) {\n if (!props->tag) {\n av_log(avctx, AV_LOG_ERROR, "Could not find tag %08X\\n", tag);\n return AVERROR_INVALIDDATA;\n }\n if (props->tag == tag) {\n avctx->sample_rate = props->sample_rate;\n avctx->channels = props->channels;\n avctx->bit_rate = props->bit_rate * 1000;\n isampf = avctx->sample_rate / 1000;\n break;\n }\n props++;\n }\n if (avctx->channels <= 0 || avctx->channels > TWINVQ_CHANNELS_MAX) {\n av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels: %i\\n",\n avctx->channels);\n return AVERROR_INVALIDDATA;\n }\n avctx->channel_layout = avctx->channels == 1 ? AV_CH_LAYOUT_MONO\n : AV_CH_LAYOUT_STEREO;\n ibps = avctx->bit_rate / (1000 * avctx->channels);\n switch ((avctx->channels << 16) + (isampf << 8) + ibps) {\n case (1 << 16) + ( 8 << 8) + 8:\n tctx->mtab = &ff_metasound_mode0808;\n break;\n case (1 << 16) + (16 << 8) + 16:\n tctx->mtab = &ff_metasound_mode1616;\n break;\n case (1 << 16) + (44 << 8) + 32:\n tctx->mtab = &ff_metasound_mode4432;\n break;\n case (2 << 16) + (44 << 8) + 48:\n tctx->mtab = &ff_metasound_mode4448s;\n break;\n default:\n av_log(avctx, AV_LOG_ERROR,\n "This version does not support %d kHz - %d kbit/s/ch mode.\\n",\n isampf, isampf);\n return AVERROR(ENOSYS);\n }\n tctx->codec = TWINVQ_CODEC_METASOUND;\n tctx->read_bitstream = metasound_read_bitstream;\n tctx->dec_bark_env = dec_bark_env;\n tctx->decode_ppc = decode_ppc;\n return ff_twinvq_decode_init(avctx);\n}']
|
1,039 | 0 |
https://github.com/openssl/openssl/blob/05ec6a25f80ac8edfb7d7cb764d2dd68156a6965/crypto/lhash/lhash.c/#L123
|
void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
{
unsigned long hash;
OPENSSL_LH_NODE *nn, **rn;
void *ret;
lh->error = 0;
rn = getrn(lh, data, &hash);
if (*rn == NULL) {
lh->num_no_delete++;
return (NULL);
} else {
nn = *rn;
*rn = nn->next;
ret = nn->data;
OPENSSL_free(nn);
lh->num_delete++;
}
lh->num_items--;
if ((lh->num_nodes > MIN_NODES) &&
(lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
contract(lh);
return (ret);
}
|
['int create_ssl_connection(SSL_CTX *serverctx, SSL_CTX *clientctx, SSL **sssl,\n SSL **cssl, BIO *s_to_c_fbio, BIO *c_to_s_fbio)\n{\n int retc = -1, rets = -1, err, abortctr = 0;\n SSL *serverssl, *clientssl;\n BIO *s_to_c_bio = NULL, *c_to_s_bio = NULL;\n serverssl = SSL_new(serverctx);\n clientssl = SSL_new(clientctx);\n if (serverssl == NULL || clientssl == NULL) {\n printf("Failed to create SSL object\\n");\n goto error;\n }\n s_to_c_bio = BIO_new(BIO_s_mem());\n c_to_s_bio = BIO_new(BIO_s_mem());\n if (s_to_c_bio == NULL || c_to_s_bio == NULL) {\n printf("Failed to create mem BIOs\\n");\n goto error;\n }\n if (s_to_c_fbio != NULL)\n s_to_c_bio = BIO_push(s_to_c_fbio, s_to_c_bio);\n if (c_to_s_fbio != NULL)\n c_to_s_bio = BIO_push(c_to_s_fbio, c_to_s_bio);\n if (s_to_c_bio == NULL || c_to_s_bio == NULL) {\n printf("Failed to create chained BIOs\\n");\n goto error;\n }\n BIO_set_mem_eof_return(s_to_c_bio, -1);\n BIO_set_mem_eof_return(c_to_s_bio, -1);\n BIO_up_ref(s_to_c_bio);\n BIO_up_ref(c_to_s_bio);\n SSL_set_bio(serverssl, c_to_s_bio, s_to_c_bio);\n SSL_set_bio(clientssl, s_to_c_bio, c_to_s_bio);\n s_to_c_bio = c_to_s_bio = NULL;\n s_to_c_fbio = c_to_s_fbio = NULL;\n do {\n err = SSL_ERROR_WANT_WRITE;\n while (retc <= 0 && err == SSL_ERROR_WANT_WRITE) {\n retc = SSL_connect(clientssl);\n if (retc <= 0)\n err = SSL_get_error(clientssl, retc);\n }\n if (retc <= 0 && err != SSL_ERROR_WANT_READ) {\n printf("SSL_connect() failed %d, %d\\n", retc, err);\n goto error;\n }\n err = SSL_ERROR_WANT_WRITE;\n while (rets <= 0 && err == SSL_ERROR_WANT_WRITE) {\n rets = SSL_accept(serverssl);\n if (rets <= 0)\n err = SSL_get_error(serverssl, rets);\n }\n if (rets <= 0 && err != SSL_ERROR_WANT_READ) {\n printf("SSL_accept() failed %d, %d\\n", retc, err);\n goto error;\n }\n if (++abortctr == MAXLOOPS) {\n printf("No progress made\\n");\n goto error;\n }\n } while (retc <=0 || rets <= 0);\n *sssl = serverssl;\n *cssl = clientssl;\n return 1;\n error:\n SSL_free(serverssl);\n SSL_free(clientssl);\n BIO_free(s_to_c_bio);\n BIO_free(c_to_s_bio);\n BIO_free(s_to_c_fbio);\n BIO_free(c_to_s_fbio);\n return 0;\n}', 'SSL *SSL_new(SSL_CTX *ctx)\n{\n SSL *s;\n if (ctx == NULL) {\n SSLerr(SSL_F_SSL_NEW, SSL_R_NULL_SSL_CTX);\n return (NULL);\n }\n if (ctx->method == NULL) {\n SSLerr(SSL_F_SSL_NEW, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);\n return (NULL);\n }\n s = OPENSSL_zalloc(sizeof(*s));\n if (s == NULL)\n goto err;\n s->lock = CRYPTO_THREAD_lock_new();\n if (s->lock == NULL) {\n SSLerr(SSL_F_SSL_NEW, ERR_R_MALLOC_FAILURE);\n OPENSSL_free(s);\n return NULL;\n }\n RECORD_LAYER_init(&s->rlayer, s);\n s->options = ctx->options;\n s->dane.flags = ctx->dane.flags;\n s->min_proto_version = ctx->min_proto_version;\n s->max_proto_version = ctx->max_proto_version;\n s->mode = ctx->mode;\n s->max_cert_list = ctx->max_cert_list;\n s->references = 1;\n s->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 SSL_free(SSL *s)\n{\n int i;\n if (s == NULL)\n return;\n CRYPTO_atomic_add(&s->references, -1, &i, s->lock);\n REF_PRINT_COUNT("SSL", s);\n if (i > 0)\n return;\n REF_ASSERT_ISNT(i < 0);\n X509_VERIFY_PARAM_free(s->param);\n dane_final(&s->dane);\n CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);\n if (s->bbio != NULL) {\n if (s->bbio == s->wbio) {\n s->wbio = BIO_pop(s->wbio);\n }\n BIO_free(s->bbio);\n s->bbio = NULL;\n }\n if (s->wbio != s->rbio)\n BIO_free_all(s->wbio);\n BIO_free_all(s->rbio);\n BUF_MEM_free(s->init_buf);\n sk_SSL_CIPHER_free(s->cipher_list);\n sk_SSL_CIPHER_free(s->cipher_list_by_id);\n if (s->session != NULL) {\n ssl_clear_bad_session(s);\n SSL_SESSION_free(s->session);\n }\n clear_ciphers(s);\n ssl_cert_free(s->cert);\n OPENSSL_free(s->tlsext_hostname);\n SSL_CTX_free(s->initial_ctx);\n#ifndef OPENSSL_NO_EC\n OPENSSL_free(s->tlsext_ecpointformatlist);\n OPENSSL_free(s->tlsext_ellipticcurvelist);\n#endif\n sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts, X509_EXTENSION_free);\n#ifndef OPENSSL_NO_OCSP\n sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids, OCSP_RESPID_free);\n#endif\n#ifndef OPENSSL_NO_CT\n SCT_LIST_free(s->scts);\n OPENSSL_free(s->tlsext_scts);\n#endif\n OPENSSL_free(s->tlsext_ocsp_resp);\n OPENSSL_free(s->alpn_client_proto_list);\n sk_X509_NAME_pop_free(s->client_CA, X509_NAME_free);\n sk_X509_pop_free(s->verified_chain, X509_free);\n if (s->method != NULL)\n s->method->ssl_free(s);\n RECORD_LAYER_release(&s->rlayer);\n SSL_CTX_free(s->ctx);\n ASYNC_WAIT_CTX_free(s->waitctx);\n#if !defined(OPENSSL_NO_NEXTPROTONEG)\n OPENSSL_free(s->next_proto_negotiated);\n#endif\n#ifndef OPENSSL_NO_SRTP\n sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);\n#endif\n CRYPTO_THREAD_lock_free(s->lock);\n OPENSSL_free(s);\n}', 'int ssl_clear_bad_session(SSL *s)\n{\n if ((s->session != NULL) &&\n !(s->shutdown & SSL_SENT_SHUTDOWN) &&\n !(SSL_in_init(s) || SSL_in_before(s))) {\n SSL_CTX_remove_session(s->session_ctx, s->session);\n return (1);\n } else\n return (0);\n}', 'int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)\n{\n return remove_session_lock(ctx, c, 1);\n}', 'static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)\n{\n SSL_SESSION *r;\n int ret = 0;\n if ((c != NULL) && (c->session_id_length != 0)) {\n if (lck)\n CRYPTO_THREAD_write_lock(ctx->lock);\n if ((r = lh_SSL_SESSION_retrieve(ctx->sessions, c)) == c) {\n ret = 1;\n r = lh_SSL_SESSION_delete(ctx->sessions, c);\n SSL_SESSION_list_remove(ctx, c);\n }\n if (lck)\n CRYPTO_THREAD_unlock(ctx->lock);\n if (ret) {\n r->not_resumable = 1;\n if (ctx->remove_session_cb != NULL)\n ctx->remove_session_cb(ctx, r);\n SSL_SESSION_free(r);\n }\n } else\n ret = 0;\n return (ret);\n}', 'DEFINE_LHASH_OF(SSL_SESSION)', 'void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)\n{\n unsigned long hash;\n OPENSSL_LH_NODE *nn, **rn;\n void *ret;\n lh->error = 0;\n rn = getrn(lh, data, &hash);\n if (*rn == NULL) {\n lh->num_no_delete++;\n return (NULL);\n } else {\n nn = *rn;\n *rn = nn->next;\n ret = nn->data;\n OPENSSL_free(nn);\n lh->num_delete++;\n }\n lh->num_items--;\n if ((lh->num_nodes > MIN_NODES) &&\n (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))\n contract(lh);\n return (ret);\n}']
|
1,040 | 0 |
https://github.com/libav/libav/blob/15201e256035a3e8f9d3d7b96fc327467e1a8ead/libavcodec/vc1_parser.c/#L203
|
static int vc1_parse(AVCodecParserContext *s,
AVCodecContext *avctx,
const uint8_t **poutbuf, int *poutbuf_size,
const uint8_t *buf, int buf_size)
{
VC1ParseContext *vpc = s->priv_data;
int pic_found = vpc->pc.frame_start_found;
uint8_t *unesc_buffer = vpc->unesc_buffer;
size_t unesc_index = vpc->unesc_index;
VC1ParseSearchState search_state = vpc->search_state;
int next = END_NOT_FOUND;
int i = vpc->bytes_to_skip;
if (pic_found && buf_size == 0) {
memset(unesc_buffer + unesc_index, 0, UNESCAPED_THRESHOLD - unesc_index);
vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
next = 0;
}
while (i < buf_size) {
int start_code_found = 0;
uint8_t b;
while (i < buf_size && unesc_index < UNESCAPED_THRESHOLD) {
b = buf[i++];
unesc_buffer[unesc_index++] = b;
if (search_state <= ONE_ZERO)
search_state = b ? NO_MATCH : search_state + 1;
else if (search_state == TWO_ZEROS) {
if (b == 1)
search_state = ONE;
else if (b > 1) {
if (b == 3)
unesc_index--;
search_state = NO_MATCH;
}
}
else {
search_state = NO_MATCH;
start_code_found = 1;
break;
}
}
if ((s->flags & PARSER_FLAG_COMPLETE_FRAMES) &&
unesc_index >= UNESCAPED_THRESHOLD &&
vpc->prev_start_code == (VC1_CODE_FRAME & 0xFF))
{
vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
break;
}
if (unesc_index >= UNESCAPED_THRESHOLD && !start_code_found) {
while (i < buf_size) {
if (search_state == NO_MATCH) {
i += vpc->v.vc1dsp.startcode_find_candidate(buf + i, buf_size - i);
if (i < buf_size) {
search_state = ONE_ZERO;
}
i++;
} else {
b = buf[i++];
if (search_state == ONE_ZERO)
search_state = b ? NO_MATCH : TWO_ZEROS;
else if (search_state == TWO_ZEROS) {
if (b >= 1)
search_state = b == 1 ? ONE : NO_MATCH;
}
else {
search_state = NO_MATCH;
start_code_found = 1;
break;
}
}
}
}
if (start_code_found) {
vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
vpc->prev_start_code = b;
unesc_index = 0;
if (!(s->flags & PARSER_FLAG_COMPLETE_FRAMES)) {
if (!pic_found && (b == (VC1_CODE_FRAME & 0xFF) || b == (VC1_CODE_FIELD & 0xFF))) {
pic_found = 1;
}
else if (pic_found && b != (VC1_CODE_FIELD & 0xFF) && b != (VC1_CODE_SLICE & 0xFF)) {
next = i - 4;
pic_found = b == (VC1_CODE_FRAME & 0xFF);
break;
}
}
}
}
vpc->pc.frame_start_found = pic_found;
vpc->unesc_index = unesc_index;
vpc->search_state = search_state;
if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
next = buf_size;
} else {
if (ff_combine_frame(&vpc->pc, next, &buf, &buf_size) < 0) {
vpc->bytes_to_skip = 0;
*poutbuf = NULL;
*poutbuf_size = 0;
return buf_size;
}
}
vpc->bytes_to_skip = 4;
if (next < 0)
vpc->bytes_to_skip += next;
*poutbuf = buf;
*poutbuf_size = buf_size;
return next;
}
|
['static int vc1_parse(AVCodecParserContext *s,\n AVCodecContext *avctx,\n const uint8_t **poutbuf, int *poutbuf_size,\n const uint8_t *buf, int buf_size)\n{\n VC1ParseContext *vpc = s->priv_data;\n int pic_found = vpc->pc.frame_start_found;\n uint8_t *unesc_buffer = vpc->unesc_buffer;\n size_t unesc_index = vpc->unesc_index;\n VC1ParseSearchState search_state = vpc->search_state;\n int next = END_NOT_FOUND;\n int i = vpc->bytes_to_skip;\n if (pic_found && buf_size == 0) {\n memset(unesc_buffer + unesc_index, 0, UNESCAPED_THRESHOLD - unesc_index);\n vc1_extract_header(s, avctx, unesc_buffer, unesc_index);\n next = 0;\n }\n while (i < buf_size) {\n int start_code_found = 0;\n uint8_t b;\n while (i < buf_size && unesc_index < UNESCAPED_THRESHOLD) {\n b = buf[i++];\n unesc_buffer[unesc_index++] = b;\n if (search_state <= ONE_ZERO)\n search_state = b ? NO_MATCH : search_state + 1;\n else if (search_state == TWO_ZEROS) {\n if (b == 1)\n search_state = ONE;\n else if (b > 1) {\n if (b == 3)\n unesc_index--;\n search_state = NO_MATCH;\n }\n }\n else {\n search_state = NO_MATCH;\n start_code_found = 1;\n break;\n }\n }\n if ((s->flags & PARSER_FLAG_COMPLETE_FRAMES) &&\n unesc_index >= UNESCAPED_THRESHOLD &&\n vpc->prev_start_code == (VC1_CODE_FRAME & 0xFF))\n {\n vc1_extract_header(s, avctx, unesc_buffer, unesc_index);\n break;\n }\n if (unesc_index >= UNESCAPED_THRESHOLD && !start_code_found) {\n while (i < buf_size) {\n if (search_state == NO_MATCH) {\n i += vpc->v.vc1dsp.startcode_find_candidate(buf + i, buf_size - i);\n if (i < buf_size) {\n search_state = ONE_ZERO;\n }\n i++;\n } else {\n b = buf[i++];\n if (search_state == ONE_ZERO)\n search_state = b ? NO_MATCH : TWO_ZEROS;\n else if (search_state == TWO_ZEROS) {\n if (b >= 1)\n search_state = b == 1 ? ONE : NO_MATCH;\n }\n else {\n search_state = NO_MATCH;\n start_code_found = 1;\n break;\n }\n }\n }\n }\n if (start_code_found) {\n vc1_extract_header(s, avctx, unesc_buffer, unesc_index);\n vpc->prev_start_code = b;\n unesc_index = 0;\n if (!(s->flags & PARSER_FLAG_COMPLETE_FRAMES)) {\n if (!pic_found && (b == (VC1_CODE_FRAME & 0xFF) || b == (VC1_CODE_FIELD & 0xFF))) {\n pic_found = 1;\n }\n else if (pic_found && b != (VC1_CODE_FIELD & 0xFF) && b != (VC1_CODE_SLICE & 0xFF)) {\n next = i - 4;\n pic_found = b == (VC1_CODE_FRAME & 0xFF);\n break;\n }\n }\n }\n }\n vpc->pc.frame_start_found = pic_found;\n vpc->unesc_index = unesc_index;\n vpc->search_state = search_state;\n if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {\n next = buf_size;\n } else {\n if (ff_combine_frame(&vpc->pc, next, &buf, &buf_size) < 0) {\n vpc->bytes_to_skip = 0;\n *poutbuf = NULL;\n *poutbuf_size = 0;\n return buf_size;\n }\n }\n vpc->bytes_to_skip = 4;\n if (next < 0)\n vpc->bytes_to_skip += next;\n *poutbuf = buf;\n *poutbuf_size = buf_size;\n return next;\n}']
|
1,041 | 0 |
https://github.com/libav/libav/blob/688417399c69aadd4c287bdb0dec82ef8799011c/libavcodec/hevcdsp_template.c/#L901
|
PUT_HEVC_QPEL_HV(1, 1)
|
['QPEL(32)', 'PUT_HEVC_QPEL_HV(1, 1)']
|
1,042 | 0 |
https://github.com/openssl/openssl/blob/9b02dc97e4963969da69675a871dbe80e6d31cda/crypto/pem/pem_lib.c/#L760
|
static int get_name(BIO *bp, char **name, unsigned int flags)
{
char *linebuf;
int ret = 0;
size_t len;
linebuf = pem_malloc(LINESIZE + 1, flags);
if (linebuf == NULL) {
PEMerr(PEM_F_GET_NAME, ERR_R_MALLOC_FAILURE);
return 0;
}
do {
len = BIO_gets(bp, linebuf, LINESIZE);
if (len <= 0) {
PEMerr(PEM_F_GET_NAME, PEM_R_NO_START_LINE);
goto err;
}
len = sanitize_line(linebuf, len, flags & ~PEM_FLAG_ONLY_B64);
} while (strncmp(linebuf, beginstr, BEGINLEN) != 0
|| len < TAILLEN
|| strncmp(linebuf + len - TAILLEN, tailstr, TAILLEN) != 0);
linebuf[len - TAILLEN] = '\0';
len = len - BEGINLEN - TAILLEN + 1;
*name = pem_malloc(len, flags);
if (*name == NULL) {
PEMerr(PEM_F_GET_NAME, ERR_R_MALLOC_FAILURE);
goto err;
}
memcpy(*name, linebuf + BEGINLEN, len);
ret = 1;
err:
pem_free(linebuf, flags, LINESIZE + 1);
return ret;
}
|
["static int get_name(BIO *bp, char **name, unsigned int flags)\n{\n char *linebuf;\n int ret = 0;\n size_t len;\n linebuf = pem_malloc(LINESIZE + 1, flags);\n if (linebuf == NULL) {\n PEMerr(PEM_F_GET_NAME, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n do {\n len = BIO_gets(bp, linebuf, LINESIZE);\n if (len <= 0) {\n PEMerr(PEM_F_GET_NAME, PEM_R_NO_START_LINE);\n goto err;\n }\n len = sanitize_line(linebuf, len, flags & ~PEM_FLAG_ONLY_B64);\n } while (strncmp(linebuf, beginstr, BEGINLEN) != 0\n || len < TAILLEN\n || strncmp(linebuf + len - TAILLEN, tailstr, TAILLEN) != 0);\n linebuf[len - TAILLEN] = '\\0';\n len = len - BEGINLEN - TAILLEN + 1;\n *name = pem_malloc(len, flags);\n if (*name == NULL) {\n PEMerr(PEM_F_GET_NAME, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n memcpy(*name, linebuf + BEGINLEN, len);\n ret = 1;\nerr:\n pem_free(linebuf, flags, LINESIZE + 1);\n return ret;\n}", 'int BIO_gets(BIO *b, char *buf, int size)\n{\n int ret;\n size_t readbytes = 0;\n if ((b == NULL) || (b->method == NULL) || (b->method->bgets == NULL)) {\n BIOerr(BIO_F_BIO_GETS, BIO_R_UNSUPPORTED_METHOD);\n return -2;\n }\n if (size < 0) {\n BIOerr(BIO_F_BIO_GETS, BIO_R_INVALID_ARGUMENT);\n return 0;\n }\n if (b->callback != NULL || b->callback_ex != NULL) {\n ret = (int)bio_call_callback(b, BIO_CB_GETS, buf, size, 0, 0L, 1, NULL);\n if (ret <= 0)\n return ret;\n }\n if (!b->init) {\n BIOerr(BIO_F_BIO_GETS, BIO_R_UNINITIALIZED);\n return -2;\n }\n ret = b->method->bgets(b, buf, size);\n if (ret > 0) {\n readbytes = ret;\n ret = 1;\n }\n if (b->callback != NULL || b->callback_ex != NULL)\n ret = (int)bio_call_callback(b, BIO_CB_GETS | BIO_CB_RETURN, buf, size,\n 0, 0L, ret, &readbytes);\n if (ret > 0) {\n if (readbytes > (size_t)size)\n ret = -1;\n else\n ret = (int)readbytes;\n }\n return ret;\n}', "static int sanitize_line(char *linebuf, int len, unsigned int flags)\n{\n int i;\n if (flags & PEM_FLAG_EAY_COMPATIBLE) {\n while ((len >= 0) && (linebuf[len] <= ' '))\n len--;\n len++;\n } else if (flags & PEM_FLAG_ONLY_B64) {\n for (i = 0; i < len; ++i) {\n if (!ossl_isbase64(linebuf[i]) || linebuf[i] == '\\n'\n || linebuf[i] == '\\r')\n break;\n }\n len = i;\n } else {\n for (i = 0; i < len; ++i) {\n if (linebuf[i] == '\\n' || linebuf[i] == '\\r')\n break;\n if (ossl_iscntrl(linebuf[i]))\n linebuf[i] = ' ';\n }\n len = i;\n }\n linebuf[len++] = '\\n';\n linebuf[len] = '\\0';\n return len;\n}"]
|
1,043 | 0 |
https://github.com/libav/libav/blob/0ca0924c10d9617a5793964bf79655424ef32b68/libavcodec/vp3.c/#L1998
|
static int vp3_decode_frame(AVCodecContext *avctx,
void *data, int *got_frame,
AVPacket *avpkt)
{
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size;
Vp3DecodeContext *s = avctx->priv_data;
GetBitContext gb;
int i, ret;
init_get_bits(&gb, buf, buf_size * 8);
if (s->theora && get_bits1(&gb)) {
av_log(avctx, AV_LOG_ERROR,
"Header packet passed to frame decoder, skipping\n");
return -1;
}
s->keyframe = !get_bits1(&gb);
if (!s->theora)
skip_bits(&gb, 1);
for (i = 0; i < 3; i++)
s->last_qps[i] = s->qps[i];
s->nqps = 0;
do {
s->qps[s->nqps++] = get_bits(&gb, 6);
} while (s->theora >= 0x030200 && s->nqps < 3 && get_bits1(&gb));
for (i = s->nqps; i < 3; i++)
s->qps[i] = -1;
if (s->avctx->debug & FF_DEBUG_PICT_INFO)
av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n",
s->keyframe ? "key" : "", avctx->frame_number + 1, s->qps[0]);
s->skip_loop_filter = !s->filter_limit_values[s->qps[0]] ||
avctx->skip_loop_filter >= (s->keyframe ? AVDISCARD_ALL
: AVDISCARD_NONKEY);
if (s->qps[0] != s->last_qps[0])
init_loop_filter(s);
for (i = 0; i < s->nqps; i++)
if (s->qps[i] != s->last_qps[i] || s->qps[0] != s->last_qps[0])
init_dequantizer(s, i);
if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe)
return buf_size;
s->current_frame.f->pict_type = s->keyframe ? AV_PICTURE_TYPE_I
: AV_PICTURE_TYPE_P;
if (ff_thread_get_buffer(avctx, &s->current_frame, AV_GET_BUFFER_FLAG_REF) < 0) {
av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
goto error;
}
if (!s->edge_emu_buffer)
s->edge_emu_buffer = av_malloc(9 * FFABS(s->current_frame.f->linesize[0]));
if (s->keyframe) {
if (!s->theora) {
skip_bits(&gb, 4);
skip_bits(&gb, 4);
if (s->version) {
s->version = get_bits(&gb, 5);
if (avctx->frame_number == 0)
av_log(s->avctx, AV_LOG_DEBUG,
"VP version: %d\n", s->version);
}
}
if (s->version || s->theora) {
if (get_bits1(&gb))
av_log(s->avctx, AV_LOG_ERROR,
"Warning, unsupported keyframe coding type?!\n");
skip_bits(&gb, 2);
}
} else {
if (!s->golden_frame.f->data[0]) {
av_log(s->avctx, AV_LOG_WARNING,
"vp3: first frame not a keyframe\n");
s->golden_frame.f->pict_type = AV_PICTURE_TYPE_I;
if (ff_thread_get_buffer(avctx, &s->golden_frame,
AV_GET_BUFFER_FLAG_REF) < 0) {
av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
goto error;
}
ff_thread_release_buffer(avctx, &s->last_frame);
if ((ret = ff_thread_ref_frame(&s->last_frame,
&s->golden_frame)) < 0)
goto error;
ff_thread_report_progress(&s->last_frame, INT_MAX, 0);
}
}
memset(s->all_fragments, 0, s->fragment_count * sizeof(Vp3Fragment));
ff_thread_finish_setup(avctx);
if (unpack_superblocks(s, &gb)) {
av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n");
goto error;
}
if (unpack_modes(s, &gb)) {
av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n");
goto error;
}
if (unpack_vectors(s, &gb)) {
av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n");
goto error;
}
if (unpack_block_qpis(s, &gb)) {
av_log(s->avctx, AV_LOG_ERROR, "error in unpack_block_qpis\n");
goto error;
}
if (unpack_dct_coeffs(s, &gb)) {
av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n");
goto error;
}
for (i = 0; i < 3; i++) {
int height = s->height >> (i && s->chroma_y_shift);
if (s->flipped_image)
s->data_offset[i] = 0;
else
s->data_offset[i] = (height - 1) * s->current_frame.f->linesize[i];
}
s->last_slice_end = 0;
for (i = 0; i < s->c_superblock_height; i++)
render_slice(s, i);
for (i = 0; i < 3; i++) {
int row = (s->height >> (3 + (i && s->chroma_y_shift))) - 1;
apply_loop_filter(s, i, row, row + 1);
}
vp3_draw_horiz_band(s, s->avctx->height);
if ((ret = av_frame_ref(data, s->current_frame.f)) < 0)
return ret;
*got_frame = 1;
if (!HAVE_THREADS || !(s->avctx->active_thread_type & FF_THREAD_FRAME)) {
ret = update_frames(avctx);
if (ret < 0)
return ret;
}
return buf_size;
error:
ff_thread_report_progress(&s->current_frame, INT_MAX, 0);
if (!HAVE_THREADS || !(s->avctx->active_thread_type & FF_THREAD_FRAME))
av_frame_unref(s->current_frame.f);
return -1;
}
|
['static int vp3_decode_frame(AVCodecContext *avctx,\n void *data, int *got_frame,\n AVPacket *avpkt)\n{\n const uint8_t *buf = avpkt->data;\n int buf_size = avpkt->size;\n Vp3DecodeContext *s = avctx->priv_data;\n GetBitContext gb;\n int i, ret;\n init_get_bits(&gb, buf, buf_size * 8);\n if (s->theora && get_bits1(&gb)) {\n av_log(avctx, AV_LOG_ERROR,\n "Header packet passed to frame decoder, skipping\\n");\n return -1;\n }\n s->keyframe = !get_bits1(&gb);\n if (!s->theora)\n skip_bits(&gb, 1);\n for (i = 0; i < 3; i++)\n s->last_qps[i] = s->qps[i];\n s->nqps = 0;\n do {\n s->qps[s->nqps++] = get_bits(&gb, 6);\n } while (s->theora >= 0x030200 && s->nqps < 3 && get_bits1(&gb));\n for (i = s->nqps; i < 3; i++)\n s->qps[i] = -1;\n if (s->avctx->debug & FF_DEBUG_PICT_INFO)\n av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\\n",\n s->keyframe ? "key" : "", avctx->frame_number + 1, s->qps[0]);\n s->skip_loop_filter = !s->filter_limit_values[s->qps[0]] ||\n avctx->skip_loop_filter >= (s->keyframe ? AVDISCARD_ALL\n : AVDISCARD_NONKEY);\n if (s->qps[0] != s->last_qps[0])\n init_loop_filter(s);\n for (i = 0; i < s->nqps; i++)\n if (s->qps[i] != s->last_qps[i] || s->qps[0] != s->last_qps[0])\n init_dequantizer(s, i);\n if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe)\n return buf_size;\n s->current_frame.f->pict_type = s->keyframe ? AV_PICTURE_TYPE_I\n : AV_PICTURE_TYPE_P;\n if (ff_thread_get_buffer(avctx, &s->current_frame, AV_GET_BUFFER_FLAG_REF) < 0) {\n av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n goto error;\n }\n if (!s->edge_emu_buffer)\n s->edge_emu_buffer = av_malloc(9 * FFABS(s->current_frame.f->linesize[0]));\n if (s->keyframe) {\n if (!s->theora) {\n skip_bits(&gb, 4);\n skip_bits(&gb, 4);\n if (s->version) {\n s->version = get_bits(&gb, 5);\n if (avctx->frame_number == 0)\n av_log(s->avctx, AV_LOG_DEBUG,\n "VP version: %d\\n", s->version);\n }\n }\n if (s->version || s->theora) {\n if (get_bits1(&gb))\n av_log(s->avctx, AV_LOG_ERROR,\n "Warning, unsupported keyframe coding type?!\\n");\n skip_bits(&gb, 2);\n }\n } else {\n if (!s->golden_frame.f->data[0]) {\n av_log(s->avctx, AV_LOG_WARNING,\n "vp3: first frame not a keyframe\\n");\n s->golden_frame.f->pict_type = AV_PICTURE_TYPE_I;\n if (ff_thread_get_buffer(avctx, &s->golden_frame,\n AV_GET_BUFFER_FLAG_REF) < 0) {\n av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n goto error;\n }\n ff_thread_release_buffer(avctx, &s->last_frame);\n if ((ret = ff_thread_ref_frame(&s->last_frame,\n &s->golden_frame)) < 0)\n goto error;\n ff_thread_report_progress(&s->last_frame, INT_MAX, 0);\n }\n }\n memset(s->all_fragments, 0, s->fragment_count * sizeof(Vp3Fragment));\n ff_thread_finish_setup(avctx);\n if (unpack_superblocks(s, &gb)) {\n av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\\n");\n goto error;\n }\n if (unpack_modes(s, &gb)) {\n av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\\n");\n goto error;\n }\n if (unpack_vectors(s, &gb)) {\n av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\\n");\n goto error;\n }\n if (unpack_block_qpis(s, &gb)) {\n av_log(s->avctx, AV_LOG_ERROR, "error in unpack_block_qpis\\n");\n goto error;\n }\n if (unpack_dct_coeffs(s, &gb)) {\n av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\\n");\n goto error;\n }\n for (i = 0; i < 3; i++) {\n int height = s->height >> (i && s->chroma_y_shift);\n if (s->flipped_image)\n s->data_offset[i] = 0;\n else\n s->data_offset[i] = (height - 1) * s->current_frame.f->linesize[i];\n }\n s->last_slice_end = 0;\n for (i = 0; i < s->c_superblock_height; i++)\n render_slice(s, i);\n for (i = 0; i < 3; i++) {\n int row = (s->height >> (3 + (i && s->chroma_y_shift))) - 1;\n apply_loop_filter(s, i, row, row + 1);\n }\n vp3_draw_horiz_band(s, s->avctx->height);\n if ((ret = av_frame_ref(data, s->current_frame.f)) < 0)\n return ret;\n *got_frame = 1;\n if (!HAVE_THREADS || !(s->avctx->active_thread_type & FF_THREAD_FRAME)) {\n ret = update_frames(avctx);\n if (ret < 0)\n return ret;\n }\n return buf_size;\nerror:\n ff_thread_report_progress(&s->current_frame, INT_MAX, 0);\n if (!HAVE_THREADS || !(s->avctx->active_thread_type & FF_THREAD_FRAME))\n av_frame_unref(s->current_frame.f);\n return -1;\n}', 'static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,\n int bit_size)\n{\n int buffer_size;\n int ret = 0;\n if (bit_size > INT_MAX - 7 || bit_size < 0 || !buffer) {\n buffer_size = bit_size = 0;\n buffer = NULL;\n ret = AVERROR_INVALIDDATA;\n }\n buffer_size = (bit_size + 7) >> 3;\n s->buffer = buffer;\n s->size_in_bits = bit_size;\n#if !UNCHECKED_BITSTREAM_READER\n s->size_in_bits_plus8 = bit_size + 8;\n#endif\n s->buffer_end = buffer + buffer_size;\n s->index = 0;\n return ret;\n}', 'static inline unsigned int get_bits1(GetBitContext *s)\n{\n unsigned int index = s->index;\n uint8_t result = s->buffer[index >> 3];\n#ifdef BITSTREAM_READER_LE\n result >>= index & 7;\n result &= 1;\n#else\n result <<= index & 7;\n result >>= 8 - 1;\n#endif\n#if !UNCHECKED_BITSTREAM_READER\n if (s->index < s->size_in_bits_plus8)\n#endif\n index++;\n s->index = index;\n return result;\n}']
|
1,044 | 0 |
https://github.com/openssl/openssl/blob/57ebe74831d9875dde98d5088bb78f5c89396d6b/ssl/packet_locl.h/#L82
|
static inline void packet_forward(PACKET *pkt, size_t len)
{
pkt->curr += len;
pkt->remaining -= len;
}
|
['int ssl3_get_server_hello(SSL *s)\n{\n STACK_OF(SSL_CIPHER) *sk;\n const SSL_CIPHER *c;\n PACKET pkt, session_id;\n size_t session_id_len;\n unsigned char *cipherchars;\n int i, al = SSL_AD_INTERNAL_ERROR, ok;\n unsigned int compression;\n long n;\n#ifndef OPENSSL_NO_COMP\n SSL_COMP *comp;\n#endif\n s->first_packet = 1;\n n = s->method->ssl_get_message(s,\n SSL3_ST_CR_SRVR_HELLO_A,\n SSL3_ST_CR_SRVR_HELLO_B, -1, 20000, &ok);\n if (!ok)\n return ((int)n);\n s->first_packet = 0;\n if (SSL_IS_DTLS(s)) {\n if (s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) {\n if (s->d1->send_cookie == 0) {\n s->s3->tmp.reuse_message = 1;\n return 1;\n } else {\n al = SSL_AD_UNEXPECTED_MESSAGE;\n SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);\n goto f_err;\n }\n }\n }\n if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) {\n al = SSL_AD_UNEXPECTED_MESSAGE;\n SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);\n goto f_err;\n }\n if (!PACKET_buf_init(&pkt, s->init_msg, n)) {\n al = SSL_AD_INTERNAL_ERROR;\n SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR);\n goto f_err;\n }\n if (s->method->version == TLS_ANY_VERSION) {\n unsigned int sversion;\n if (!PACKET_get_net_2(&pkt, &sversion)) {\n al = SSL_AD_DECODE_ERROR;\n SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);\n goto f_err;\n }\n#if TLS_MAX_VERSION != TLS1_2_VERSION\n#error Code needs updating for new TLS version\n#endif\n#ifndef OPENSSL_NO_SSL3\n if ((sversion == SSL3_VERSION) && !(s->options & SSL_OP_NO_SSLv3)) {\n if (FIPS_mode()) {\n SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,\n SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);\n al = SSL_AD_PROTOCOL_VERSION;\n goto f_err;\n }\n s->method = SSLv3_client_method();\n } else\n#endif\n if ((sversion == TLS1_VERSION) && !(s->options & SSL_OP_NO_TLSv1)) {\n s->method = TLSv1_client_method();\n } else if ((sversion == TLS1_1_VERSION) &&\n !(s->options & SSL_OP_NO_TLSv1_1)) {\n s->method = TLSv1_1_client_method();\n } else if ((sversion == TLS1_2_VERSION) &&\n !(s->options & SSL_OP_NO_TLSv1_2)) {\n s->method = TLSv1_2_client_method();\n } else {\n SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNSUPPORTED_PROTOCOL);\n al = SSL_AD_PROTOCOL_VERSION;\n goto f_err;\n }\n s->session->ssl_version = s->version = s->method->version;\n if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {\n SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_VERSION_TOO_LOW);\n al = SSL_AD_PROTOCOL_VERSION;\n goto f_err;\n }\n } else if (s->method->version == DTLS_ANY_VERSION) {\n unsigned int hversion;\n int options;\n if (!PACKET_get_net_2(&pkt, &hversion)) {\n al = SSL_AD_DECODE_ERROR;\n SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);\n goto f_err;\n }\n options = s->options;\n if (hversion == DTLS1_2_VERSION && !(options & SSL_OP_NO_DTLSv1_2))\n s->method = DTLSv1_2_client_method();\n else if (tls1_suiteb(s)) {\n SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,\n SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);\n s->version = hversion;\n al = SSL_AD_PROTOCOL_VERSION;\n goto f_err;\n } else if (hversion == DTLS1_VERSION && !(options & SSL_OP_NO_DTLSv1))\n s->method = DTLSv1_client_method();\n else {\n SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);\n s->version = hversion;\n al = SSL_AD_PROTOCOL_VERSION;\n goto f_err;\n }\n s->session->ssl_version = s->version = s->method->version;\n } else {\n unsigned char *vers;\n if (!PACKET_get_bytes(&pkt, &vers, 2)) {\n al = SSL_AD_DECODE_ERROR;\n SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);\n goto f_err;\n }\n if ((vers[0] != (s->version >> 8))\n || (vers[1] != (s->version & 0xff))) {\n SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);\n s->version = (s->version & 0xff00) | vers[1];\n al = SSL_AD_PROTOCOL_VERSION;\n goto f_err;\n }\n }\n if (!PACKET_copy_bytes(&pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) {\n al = SSL_AD_DECODE_ERROR;\n SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);\n goto f_err;\n }\n s->hit = 0;\n if (!PACKET_get_length_prefixed_1(&pkt, &session_id)) {\n al = SSL_AD_DECODE_ERROR;\n SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);\n goto f_err;\n }\n session_id_len = PACKET_remaining(&session_id);\n if (session_id_len > sizeof s->session->session_id\n || session_id_len > SSL3_SESSION_ID_SIZE) {\n al = SSL_AD_ILLEGAL_PARAMETER;\n SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);\n goto f_err;\n }\n if (!PACKET_get_bytes(&pkt, &cipherchars, TLS_CIPHER_LEN)) {\n SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);\n al = SSL_AD_DECODE_ERROR;\n goto f_err;\n }\n if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&\n s->session->tlsext_tick) {\n SSL_CIPHER *pref_cipher = NULL;\n s->session->master_key_length = sizeof(s->session->master_key);\n if (s->tls_session_secret_cb(s, s->session->master_key,\n &s->session->master_key_length,\n NULL, &pref_cipher,\n s->tls_session_secret_cb_arg)) {\n s->session->cipher = pref_cipher ?\n pref_cipher : ssl_get_cipher_by_char(s, cipherchars);\n } else {\n SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR);\n al = SSL_AD_INTERNAL_ERROR;\n goto f_err;\n }\n }\n if (session_id_len != 0 && session_id_len == s->session->session_id_length\n && memcmp(PACKET_data(&session_id), s->session->session_id,\n session_id_len) == 0) {\n if (s->sid_ctx_length != s->session->sid_ctx_length\n || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {\n al = SSL_AD_ILLEGAL_PARAMETER;\n SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,\n SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);\n goto f_err;\n }\n s->hit = 1;\n } else {\n if (s->session->session_id_length > 0) {\n if (!ssl_get_new_session(s, 0)) {\n goto f_err;\n }\n }\n s->session->session_id_length = session_id_len;\n memcpy(s->session->session_id, PACKET_data(&session_id),\n session_id_len);\n }\n c = ssl_get_cipher_by_char(s, cipherchars);\n if (c == NULL) {\n al = SSL_AD_ILLEGAL_PARAMETER;\n SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);\n goto f_err;\n }\n if (!SSL_USE_TLS1_2_CIPHERS(s))\n s->s3->tmp.mask_ssl = SSL_TLSV1_2;\n else\n s->s3->tmp.mask_ssl = 0;\n if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) {\n al = SSL_AD_ILLEGAL_PARAMETER;\n SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);\n goto f_err;\n }\n sk = ssl_get_ciphers_by_id(s);\n i = sk_SSL_CIPHER_find(sk, c);\n if (i < 0) {\n al = SSL_AD_ILLEGAL_PARAMETER;\n SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);\n goto f_err;\n }\n if (s->session->cipher)\n s->session->cipher_id = s->session->cipher->id;\n if (s->hit && (s->session->cipher_id != c->id)) {\n al = SSL_AD_ILLEGAL_PARAMETER;\n SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,\n SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);\n goto f_err;\n }\n s->s3->tmp.new_cipher = c;\n if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s, 0))\n goto f_err;\n if (!PACKET_get_1(&pkt, &compression)) {\n SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);\n al = SSL_AD_DECODE_ERROR;\n goto f_err;\n }\n#ifdef OPENSSL_NO_COMP\n if (compression != 0) {\n al = SSL_AD_ILLEGAL_PARAMETER;\n SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,\n SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);\n goto f_err;\n }\n if (s->session->compress_meth != 0) {\n SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);\n goto f_err;\n }\n#else\n if (s->hit && compression != s->session->compress_meth) {\n al = SSL_AD_ILLEGAL_PARAMETER;\n SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,\n SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);\n goto f_err;\n }\n if (compression == 0)\n comp = NULL;\n else if (!ssl_allow_compression(s)) {\n al = SSL_AD_ILLEGAL_PARAMETER;\n SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);\n goto f_err;\n } else {\n comp = ssl3_comp_find(s->ctx->comp_methods, compression);\n }\n if (compression != 0 && comp == NULL) {\n al = SSL_AD_ILLEGAL_PARAMETER;\n SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,\n SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);\n goto f_err;\n } else {\n s->s3->tmp.new_compression = comp;\n }\n#endif\n if (!ssl_parse_serverhello_tlsext(s, &pkt)) {\n SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_PARSE_TLSEXT);\n goto err;\n }\n if (PACKET_remaining(&pkt) != 0) {\n al = SSL_AD_DECODE_ERROR;\n SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);\n goto f_err;\n }\n return (1);\n f_err:\n ssl3_send_alert(s, SSL3_AL_FATAL, al);\n err:\n s->state = SSL_ST_ERR;\n return (-1);\n}', 'static inline void packet_forward(PACKET *pkt, size_t len)\n{\n pkt->curr += len;\n pkt->remaining -= len;\n}']
|
1,045 | 0 |
https://github.com/libav/libav/blob/e5b0fc170f85b00f7dd0ac514918fb5c95253d39/libavcodec/atrac3plus.c/#L508
|
static int decode_channel_sf_idx(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
int ch_num, AVCodecContext *avctx)
{
int i, weight_idx = 0, delta, diff, num_long_vals,
delta_bits, min_val, vlc_sel, start_val;
VLC *vlc_tab;
Atrac3pChanParams *chan = &ctx->channels[ch_num];
Atrac3pChanParams *ref_chan = &ctx->channels[0];
switch (bitstream_read(bc, 2)) {
case 0:
for (i = 0; i < ctx->used_quant_units; i++)
chan->qu_sf_idx[i] = bitstream_read(bc, 6);
break;
case 1:
if (ch_num) {
vlc_tab = &sf_vlc_tabs[bitstream_read(bc, 2)];
for (i = 0; i < ctx->used_quant_units; i++) {
delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);
chan->qu_sf_idx[i] = (ref_chan->qu_sf_idx[i] + delta) & 0x3F;
}
} else {
weight_idx = bitstream_read(bc, 2);
if (weight_idx == 3) {
UNPACK_SF_VQ_SHAPE(bc, chan->qu_sf_idx, ctx->used_quant_units);
num_long_vals = bitstream_read(bc, 5);
delta_bits = bitstream_read(bc, 2);
min_val = bitstream_read(bc, 4) - 7;
for (i = 0; i < num_long_vals; i++)
chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] +
bitstream_read(bc, 4) - 7) & 0x3F;
for (i = num_long_vals; i < ctx->used_quant_units; i++)
chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] + min_val +
bitstream_read(bc, delta_bits)) & 0x3F;
} else {
num_long_vals = bitstream_read(bc, 5);
delta_bits = bitstream_read(bc, 3);
min_val = bitstream_read(bc, 6);
if (num_long_vals > ctx->used_quant_units || delta_bits == 7) {
av_log(avctx, AV_LOG_ERROR,
"SF mode 1: invalid parameters!\n");
return AVERROR_INVALIDDATA;
}
for (i = 0; i < num_long_vals; i++)
chan->qu_sf_idx[i] = bitstream_read(bc, 6);
for (i = num_long_vals; i < ctx->used_quant_units; i++)
chan->qu_sf_idx[i] = (min_val +
bitstream_read(bc, delta_bits)) & 0x3F;
}
}
break;
case 2:
if (ch_num) {
vlc_tab = &sf_vlc_tabs[bitstream_read(bc, 2)];
delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);
chan->qu_sf_idx[0] = (ref_chan->qu_sf_idx[0] + delta) & 0x3F;
for (i = 1; i < ctx->used_quant_units; i++) {
diff = ref_chan->qu_sf_idx[i] - ref_chan->qu_sf_idx[i - 1];
delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);
chan->qu_sf_idx[i] = (chan->qu_sf_idx[i - 1] + diff + delta) & 0x3F;
}
} else {
vlc_tab = &sf_vlc_tabs[bitstream_read(bc, 2) + 4];
UNPACK_SF_VQ_SHAPE(bc, chan->qu_sf_idx, ctx->used_quant_units);
for (i = 0; i < ctx->used_quant_units; i++) {
delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);
chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] +
sign_extend(delta, 4)) & 0x3F;
}
}
break;
case 3:
if (ch_num) {
for (i = 0; i < ctx->used_quant_units; i++)
chan->qu_sf_idx[i] = ref_chan->qu_sf_idx[i];
} else {
weight_idx = bitstream_read(bc, 2);
vlc_sel = bitstream_read(bc, 2);
vlc_tab = &sf_vlc_tabs[vlc_sel];
if (weight_idx == 3) {
vlc_tab = &sf_vlc_tabs[vlc_sel + 4];
UNPACK_SF_VQ_SHAPE(bc, chan->qu_sf_idx, ctx->used_quant_units);
diff = (bitstream_read(bc, 4) + 56) & 0x3F;
chan->qu_sf_idx[0] = (chan->qu_sf_idx[0] + diff) & 0x3F;
for (i = 1; i < ctx->used_quant_units; i++) {
delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);
diff = (diff + sign_extend(delta, 4)) & 0x3F;
chan->qu_sf_idx[i] = (diff + chan->qu_sf_idx[i]) & 0x3F;
}
} else {
chan->qu_sf_idx[0] = bitstream_read(bc, 6);
for (i = 1; i < ctx->used_quant_units; i++) {
delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);
chan->qu_sf_idx[i] = (chan->qu_sf_idx[i - 1] + delta) & 0x3F;
}
}
}
break;
}
if (weight_idx && weight_idx < 3)
return subtract_sf_weights(ctx, chan, weight_idx, avctx);
return 0;
}
|
['static int decode_channel_sf_idx(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,\n int ch_num, AVCodecContext *avctx)\n{\n int i, weight_idx = 0, delta, diff, num_long_vals,\n delta_bits, min_val, vlc_sel, start_val;\n VLC *vlc_tab;\n Atrac3pChanParams *chan = &ctx->channels[ch_num];\n Atrac3pChanParams *ref_chan = &ctx->channels[0];\n switch (bitstream_read(bc, 2)) {\n case 0:\n for (i = 0; i < ctx->used_quant_units; i++)\n chan->qu_sf_idx[i] = bitstream_read(bc, 6);\n break;\n case 1:\n if (ch_num) {\n vlc_tab = &sf_vlc_tabs[bitstream_read(bc, 2)];\n for (i = 0; i < ctx->used_quant_units; i++) {\n delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);\n chan->qu_sf_idx[i] = (ref_chan->qu_sf_idx[i] + delta) & 0x3F;\n }\n } else {\n weight_idx = bitstream_read(bc, 2);\n if (weight_idx == 3) {\n UNPACK_SF_VQ_SHAPE(bc, chan->qu_sf_idx, ctx->used_quant_units);\n num_long_vals = bitstream_read(bc, 5);\n delta_bits = bitstream_read(bc, 2);\n min_val = bitstream_read(bc, 4) - 7;\n for (i = 0; i < num_long_vals; i++)\n chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] +\n bitstream_read(bc, 4) - 7) & 0x3F;\n for (i = num_long_vals; i < ctx->used_quant_units; i++)\n chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] + min_val +\n bitstream_read(bc, delta_bits)) & 0x3F;\n } else {\n num_long_vals = bitstream_read(bc, 5);\n delta_bits = bitstream_read(bc, 3);\n min_val = bitstream_read(bc, 6);\n if (num_long_vals > ctx->used_quant_units || delta_bits == 7) {\n av_log(avctx, AV_LOG_ERROR,\n "SF mode 1: invalid parameters!\\n");\n return AVERROR_INVALIDDATA;\n }\n for (i = 0; i < num_long_vals; i++)\n chan->qu_sf_idx[i] = bitstream_read(bc, 6);\n for (i = num_long_vals; i < ctx->used_quant_units; i++)\n chan->qu_sf_idx[i] = (min_val +\n bitstream_read(bc, delta_bits)) & 0x3F;\n }\n }\n break;\n case 2:\n if (ch_num) {\n vlc_tab = &sf_vlc_tabs[bitstream_read(bc, 2)];\n delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);\n chan->qu_sf_idx[0] = (ref_chan->qu_sf_idx[0] + delta) & 0x3F;\n for (i = 1; i < ctx->used_quant_units; i++) {\n diff = ref_chan->qu_sf_idx[i] - ref_chan->qu_sf_idx[i - 1];\n delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);\n chan->qu_sf_idx[i] = (chan->qu_sf_idx[i - 1] + diff + delta) & 0x3F;\n }\n } else {\n vlc_tab = &sf_vlc_tabs[bitstream_read(bc, 2) + 4];\n UNPACK_SF_VQ_SHAPE(bc, chan->qu_sf_idx, ctx->used_quant_units);\n for (i = 0; i < ctx->used_quant_units; i++) {\n delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);\n chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] +\n sign_extend(delta, 4)) & 0x3F;\n }\n }\n break;\n case 3:\n if (ch_num) {\n for (i = 0; i < ctx->used_quant_units; i++)\n chan->qu_sf_idx[i] = ref_chan->qu_sf_idx[i];\n } else {\n weight_idx = bitstream_read(bc, 2);\n vlc_sel = bitstream_read(bc, 2);\n vlc_tab = &sf_vlc_tabs[vlc_sel];\n if (weight_idx == 3) {\n vlc_tab = &sf_vlc_tabs[vlc_sel + 4];\n UNPACK_SF_VQ_SHAPE(bc, chan->qu_sf_idx, ctx->used_quant_units);\n diff = (bitstream_read(bc, 4) + 56) & 0x3F;\n chan->qu_sf_idx[0] = (chan->qu_sf_idx[0] + diff) & 0x3F;\n for (i = 1; i < ctx->used_quant_units; i++) {\n delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);\n diff = (diff + sign_extend(delta, 4)) & 0x3F;\n chan->qu_sf_idx[i] = (diff + chan->qu_sf_idx[i]) & 0x3F;\n }\n } else {\n chan->qu_sf_idx[0] = bitstream_read(bc, 6);\n for (i = 1; i < ctx->used_quant_units; i++) {\n delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);\n chan->qu_sf_idx[i] = (chan->qu_sf_idx[i - 1] + delta) & 0x3F;\n }\n }\n }\n break;\n }\n if (weight_idx && weight_idx < 3)\n return subtract_sf_weights(ctx, chan, weight_idx, avctx);\n return 0;\n}', 'static inline uint32_t bitstream_read(BitstreamContext *bc, unsigned n)\n{\n if (!n)\n return 0;\n if (n > bc->bits_left) {\n refill_32(bc);\n if (bc->bits_left < 32)\n bc->bits_left = n;\n }\n return get_val(bc, n);\n}']
|
1,046 | 0 |
https://github.com/nginx/nginx/blob/030a1f959c9c673258fe53f968fab04fc9214b86/src/http/ngx_http_file_cache.c/#L1894
|
static void
ngx_http_file_cache_delete(ngx_http_file_cache_t *cache, ngx_queue_t *q,
u_char *name)
{
u_char *p;
size_t len;
ngx_path_t *path;
ngx_http_file_cache_node_t *fcn;
fcn = ngx_queue_data(q, ngx_http_file_cache_node_t, queue);
if (fcn->exists) {
cache->sh->size -= fcn->fs_size;
path = cache->path;
p = name + path->name.len + 1 + path->len;
p = ngx_hex_dump(p, (u_char *) &fcn->node.key,
sizeof(ngx_rbtree_key_t));
len = NGX_HTTP_CACHE_KEY_LEN - sizeof(ngx_rbtree_key_t);
p = ngx_hex_dump(p, fcn->key, len);
*p = '\0';
fcn->count++;
fcn->deleting = 1;
ngx_shmtx_unlock(&cache->shpool->mutex);
len = path->name.len + 1 + path->len + 2 * NGX_HTTP_CACHE_KEY_LEN;
ngx_create_hashed_filename(path, name, len);
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
"http file cache expire: \"%s\"", name);
if (ngx_delete_file(name) == NGX_FILE_ERROR) {
ngx_log_error(NGX_LOG_CRIT, ngx_cycle->log, ngx_errno,
ngx_delete_file_n " \"%s\" failed", name);
}
ngx_shmtx_lock(&cache->shpool->mutex);
fcn->count--;
fcn->deleting = 0;
}
if (fcn->count == 0) {
ngx_queue_remove(q);
ngx_rbtree_delete(&cache->sh->rbtree, &fcn->node);
ngx_slab_free_locked(cache->shpool, fcn);
cache->sh->count--;
}
}
|
['static time_t\nngx_http_file_cache_manager(void *data)\n{\n ngx_http_file_cache_t *cache = data;\n off_t size;\n time_t next, wait;\n ngx_uint_t count, watermark;\n next = ngx_http_file_cache_expire(cache);\n cache->last = ngx_current_msec;\n cache->files = 0;\n for ( ;; ) {\n ngx_shmtx_lock(&cache->shpool->mutex);\n size = cache->sh->size;\n count = cache->sh->count;\n watermark = cache->sh->watermark;\n ngx_shmtx_unlock(&cache->shpool->mutex);\n ngx_log_debug3(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,\n "http file cache size: %O c:%ui w:%i",\n size, count, (ngx_int_t) watermark);\n if (size < cache->max_size && count < watermark) {\n return next;\n }\n wait = ngx_http_file_cache_forced_expire(cache);\n if (wait > 0) {\n return wait;\n }\n if (ngx_quit || ngx_terminate) {\n return next;\n }\n }\n}', 'static time_t\nngx_http_file_cache_expire(ngx_http_file_cache_t *cache)\n{\n u_char *name, *p;\n size_t len;\n time_t now, wait;\n ngx_path_t *path;\n ngx_queue_t *q;\n ngx_http_file_cache_node_t *fcn;\n u_char key[2 * NGX_HTTP_CACHE_KEY_LEN];\n ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,\n "http file cache expire");\n path = cache->path;\n len = path->name.len + 1 + path->len + 2 * NGX_HTTP_CACHE_KEY_LEN;\n name = ngx_alloc(len + 1, ngx_cycle->log);\n if (name == NULL) {\n return 10;\n }\n ngx_memcpy(name, path->name.data, path->name.len);\n now = ngx_time();\n ngx_shmtx_lock(&cache->shpool->mutex);\n for ( ;; ) {\n if (ngx_quit || ngx_terminate) {\n wait = 1;\n break;\n }\n if (ngx_queue_empty(&cache->sh->queue)) {\n wait = 10;\n break;\n }\n q = ngx_queue_last(&cache->sh->queue);\n fcn = ngx_queue_data(q, ngx_http_file_cache_node_t, queue);\n wait = fcn->expire - now;\n if (wait > 0) {\n wait = wait > 10 ? 10 : wait;\n break;\n }\n ngx_log_debug6(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,\n "http file cache expire: #%d %d %02xd%02xd%02xd%02xd",\n fcn->count, fcn->exists,\n fcn->key[0], fcn->key[1], fcn->key[2], fcn->key[3]);\n if (fcn->count == 0) {\n ngx_http_file_cache_delete(cache, q, name);\n continue;\n }\n if (fcn->deleting) {\n wait = 1;\n break;\n }\n p = ngx_hex_dump(key, (u_char *) &fcn->node.key,\n sizeof(ngx_rbtree_key_t));\n len = NGX_HTTP_CACHE_KEY_LEN - sizeof(ngx_rbtree_key_t);\n (void) ngx_hex_dump(p, fcn->key, len);\n ngx_queue_remove(q);\n fcn->expire = ngx_time() + cache->inactive;\n ngx_queue_insert_head(&cache->sh->queue, &fcn->queue);\n ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0,\n "ignore long locked inactive cache entry %*s, count:%d",\n 2 * NGX_HTTP_CACHE_KEY_LEN, key, fcn->count);\n }\n ngx_shmtx_unlock(&cache->shpool->mutex);\n ngx_free(name);\n return wait;\n}', 'static void\nngx_http_file_cache_delete(ngx_http_file_cache_t *cache, ngx_queue_t *q,\n u_char *name)\n{\n u_char *p;\n size_t len;\n ngx_path_t *path;\n ngx_http_file_cache_node_t *fcn;\n fcn = ngx_queue_data(q, ngx_http_file_cache_node_t, queue);\n if (fcn->exists) {\n cache->sh->size -= fcn->fs_size;\n path = cache->path;\n p = name + path->name.len + 1 + path->len;\n p = ngx_hex_dump(p, (u_char *) &fcn->node.key,\n sizeof(ngx_rbtree_key_t));\n len = NGX_HTTP_CACHE_KEY_LEN - sizeof(ngx_rbtree_key_t);\n p = ngx_hex_dump(p, fcn->key, len);\n *p = \'\\0\';\n fcn->count++;\n fcn->deleting = 1;\n ngx_shmtx_unlock(&cache->shpool->mutex);\n len = path->name.len + 1 + path->len + 2 * NGX_HTTP_CACHE_KEY_LEN;\n ngx_create_hashed_filename(path, name, len);\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,\n "http file cache expire: \\"%s\\"", name);\n if (ngx_delete_file(name) == NGX_FILE_ERROR) {\n ngx_log_error(NGX_LOG_CRIT, ngx_cycle->log, ngx_errno,\n ngx_delete_file_n " \\"%s\\" failed", name);\n }\n ngx_shmtx_lock(&cache->shpool->mutex);\n fcn->count--;\n fcn->deleting = 0;\n }\n if (fcn->count == 0) {\n ngx_queue_remove(q);\n ngx_rbtree_delete(&cache->sh->rbtree, &fcn->node);\n ngx_slab_free_locked(cache->shpool, fcn);\n cache->sh->count--;\n }\n}', 'void\nngx_shmtx_unlock(ngx_shmtx_t *mtx)\n{\n if (mtx->spin != (ngx_uint_t) -1) {\n ngx_log_debug0(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0, "shmtx unlock");\n }\n if (ngx_atomic_cmp_set(mtx->lock, ngx_pid, 0)) {\n ngx_shmtx_wakeup(mtx);\n }\n}', 'void\nngx_shmtx_lock(ngx_shmtx_t *mtx)\n{\n ngx_uint_t i, n;\n ngx_log_debug0(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0, "shmtx lock");\n for ( ;; ) {\n if (*mtx->lock == 0 && ngx_atomic_cmp_set(mtx->lock, 0, ngx_pid)) {\n return;\n }\n if (ngx_ncpu > 1) {\n for (n = 1; n < mtx->spin; n <<= 1) {\n for (i = 0; i < n; i++) {\n ngx_cpu_pause();\n }\n if (*mtx->lock == 0\n && ngx_atomic_cmp_set(mtx->lock, 0, ngx_pid))\n {\n return;\n }\n }\n }\n#if (NGX_HAVE_POSIX_SEM)\n if (mtx->semaphore) {\n (void) ngx_atomic_fetch_add(mtx->wait, 1);\n if (*mtx->lock == 0 && ngx_atomic_cmp_set(mtx->lock, 0, ngx_pid)) {\n (void) ngx_atomic_fetch_add(mtx->wait, -1);\n return;\n }\n ngx_log_debug1(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0,\n "shmtx wait %uA", *mtx->wait);\n while (sem_wait(&mtx->sem) == -1) {\n ngx_err_t err;\n err = ngx_errno;\n if (err != NGX_EINTR) {\n ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, err,\n "sem_wait() failed while waiting on shmtx");\n break;\n }\n }\n ngx_log_debug0(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0,\n "shmtx awoke");\n continue;\n }\n#endif\n ngx_sched_yield();\n }\n}', 'static time_t\nngx_http_file_cache_forced_expire(ngx_http_file_cache_t *cache)\n{\n u_char *name;\n size_t len;\n time_t wait;\n ngx_uint_t tries;\n ngx_path_t *path;\n ngx_queue_t *q;\n ngx_http_file_cache_node_t *fcn;\n ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,\n "http file cache forced expire");\n path = cache->path;\n len = path->name.len + 1 + path->len + 2 * NGX_HTTP_CACHE_KEY_LEN;\n name = ngx_alloc(len + 1, ngx_cycle->log);\n if (name == NULL) {\n return 10;\n }\n ngx_memcpy(name, path->name.data, path->name.len);\n wait = 10;\n tries = 20;\n ngx_shmtx_lock(&cache->shpool->mutex);\n for (q = ngx_queue_last(&cache->sh->queue);\n q != ngx_queue_sentinel(&cache->sh->queue);\n q = ngx_queue_prev(q))\n {\n fcn = ngx_queue_data(q, ngx_http_file_cache_node_t, queue);\n ngx_log_debug6(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,\n "http file cache forced expire: #%d %d %02xd%02xd%02xd%02xd",\n fcn->count, fcn->exists,\n fcn->key[0], fcn->key[1], fcn->key[2], fcn->key[3]);\n if (fcn->count == 0) {\n ngx_http_file_cache_delete(cache, q, name);\n wait = 0;\n } else {\n if (--tries) {\n continue;\n }\n wait = 1;\n }\n break;\n }\n ngx_shmtx_unlock(&cache->shpool->mutex);\n ngx_free(name);\n return wait;\n}']
|
1,047 | 0 |
https://github.com/openssl/openssl/blob/38d1b3cc0271008b8bd130a2c4b442775b028a08/crypto/bn/bn_shift.c/#L110
|
int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)
{
int i, nw, lb, rb;
BN_ULONG *t, *f;
BN_ULONG l;
bn_check_top(r);
bn_check_top(a);
if (n < 0) {
BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);
return 0;
}
nw = n / BN_BITS2;
if (bn_wexpand(r, a->top + nw + 1) == NULL)
return (0);
r->neg = a->neg;
lb = n % BN_BITS2;
rb = BN_BITS2 - lb;
f = a->d;
t = r->d;
t[a->top + nw] = 0;
if (lb == 0)
for (i = a->top - 1; i >= 0; i--)
t[nw + i] = f[i];
else
for (i = a->top - 1; i >= 0; i--) {
l = f[i];
t[nw + i + 1] |= (l >> rb) & BN_MASK2;
t[nw + i] = (l << lb) & BN_MASK2;
}
memset(t, 0, sizeof(*t) * nw);
r->top = a->top + nw + 1;
bn_correct_top(r);
bn_check_top(r);
return (1);
}
|
['int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,\n const ECDSA_SIG *sig, EC_KEY *eckey)\n{\n int ret = -1, i;\n BN_CTX *ctx;\n const BIGNUM *order;\n BIGNUM *u1, *u2, *m, *X;\n EC_POINT *point = NULL;\n const EC_GROUP *group;\n const EC_POINT *pub_key;\n if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||\n (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_MISSING_PARAMETERS);\n return -1;\n }\n if (!EC_KEY_can_sign(eckey)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);\n return -1;\n }\n ctx = BN_CTX_new();\n if (ctx == NULL) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);\n return -1;\n }\n BN_CTX_start(ctx);\n u1 = BN_CTX_get(ctx);\n u2 = BN_CTX_get(ctx);\n m = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n if (X == NULL) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);\n goto err;\n }\n order = EC_GROUP_get0_order(group);\n if (order == NULL) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);\n goto err;\n }\n if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||\n BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||\n BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_BAD_SIGNATURE);\n ret = 0;\n goto err;\n }\n if (!BN_mod_inverse(u2, sig->s, order, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);\n goto err;\n }\n i = BN_num_bits(order);\n if (8 * dgst_len > i)\n dgst_len = (i + 7) / 8;\n if (!BN_bin2bn(dgst, dgst_len, m)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if (!BN_mod_mul(u1, m, u2, order, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if ((point = EC_POINT_new(group)) == NULL) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);\n goto err;\n }\n if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==\n NID_X9_62_prime_field) {\n if (!EC_POINT_get_affine_coordinates_GFp(group, point, X, NULL, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);\n goto err;\n }\n }\n#ifndef OPENSSL_NO_EC2M\n else {\n if (!EC_POINT_get_affine_coordinates_GF2m(group, point, X, NULL, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);\n goto err;\n }\n }\n#endif\n if (!BN_nnmod(u1, X, order, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);\n goto err;\n }\n ret = (BN_ucmp(u1, sig->r) == 0);\n err:\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n EC_POINT_free(point);\n return ret;\n}', 'BIGNUM *BN_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n{\n BIGNUM *rv;\n int noinv;\n rv = int_bn_mod_inverse(in, a, n, ctx, &noinv);\n if (noinv)\n BNerr(BN_F_BN_MOD_INVERSE, BN_R_NO_INVERSE);\n return rv;\n}', 'BIGNUM *int_bn_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,\n int *pnoinv)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM *ret = NULL;\n int sign;\n if (pnoinv)\n *pnoinv = 0;\n if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {\n return BN_mod_inverse_no_branch(in, a, n, ctx);\n }\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n if (!BN_nnmod(B, B, A, ctx))\n goto err;\n }\n sign = -1;\n if (BN_is_odd(n) && (BN_num_bits(n) <= 2048)) {\n int shift;\n while (!BN_is_zero(B)) {\n shift = 0;\n while (!BN_is_bit_set(B, shift)) {\n shift++;\n if (BN_is_odd(X)) {\n if (!BN_uadd(X, X, n))\n goto err;\n }\n if (!BN_rshift1(X, X))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(B, B, shift))\n goto err;\n }\n shift = 0;\n while (!BN_is_bit_set(A, shift)) {\n shift++;\n if (BN_is_odd(Y)) {\n if (!BN_uadd(Y, Y, n))\n goto err;\n }\n if (!BN_rshift1(Y, Y))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(A, A, shift))\n goto err;\n }\n if (BN_ucmp(B, A) >= 0) {\n if (!BN_uadd(X, X, Y))\n goto err;\n if (!BN_usub(B, B, A))\n goto err;\n } else {\n if (!BN_uadd(Y, Y, X))\n goto err;\n if (!BN_usub(A, A, B))\n goto err;\n }\n }\n } else {\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n if (BN_num_bits(A) == BN_num_bits(B)) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else if (BN_num_bits(A) == BN_num_bits(B) + 1) {\n if (!BN_lshift1(T, B))\n goto err;\n if (BN_ucmp(A, T) < 0) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else {\n if (!BN_sub(M, A, T))\n goto err;\n if (!BN_add(D, T, B))\n goto err;\n if (BN_ucmp(A, D) < 0) {\n if (!BN_set_word(D, 2))\n goto err;\n } else {\n if (!BN_set_word(D, 3))\n goto err;\n if (!BN_sub(M, M, B))\n goto err;\n }\n }\n } else {\n if (!BN_div(D, M, A, B, ctx))\n goto err;\n }\n tmp = A;\n A = B;\n B = M;\n if (BN_is_one(D)) {\n if (!BN_add(tmp, X, Y))\n goto err;\n } else {\n if (BN_is_word(D, 2)) {\n if (!BN_lshift1(tmp, X))\n goto err;\n } else if (BN_is_word(D, 4)) {\n if (!BN_lshift(tmp, X, 2))\n goto err;\n } else if (D->top == 1) {\n if (!BN_copy(tmp, X))\n goto err;\n if (!BN_mul_word(tmp, D->d[0]))\n goto err;\n } else {\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n }\n if (!BN_add(tmp, tmp, Y))\n goto err;\n }\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n if (pnoinv)\n *pnoinv = 1;\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return (ret);\n}', 'int BN_num_bits(const BIGNUM *a)\n{\n int i = a->top - 1;\n bn_check_top(a);\n if (BN_is_zero(a))\n return 0;\n return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));\n}', 'int BN_is_zero(const BIGNUM *a)\n{\n return a->top == 0;\n}', 'int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,\n BN_CTX *ctx)\n{\n BIGNUM *t;\n int ret = 0;\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(m);\n BN_CTX_start(ctx);\n if ((t = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (a == b) {\n if (!BN_sqr(t, a, ctx))\n goto err;\n } else {\n if (!BN_mul(t, a, b, ctx))\n goto err;\n }\n if (!BN_nnmod(r, t, m, ctx))\n goto err;\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return (ret);\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return (0);\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return (0);\n }\n if (dv != NULL)\n BN_zero(dv);\n return (1);\n }\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (dv == NULL)\n res = BN_CTX_get(ctx);\n else\n res = dv;\n if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n 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}']
|
1,048 | 0 |
https://github.com/openssl/openssl/blob/93d9121a775da2beb680d889929245309032fcc0/apps/x509.c/#L1224
|
static int sign(X509 *x, EVP_PKEY *pkey, int days, int clrext, const EVP_MD *digest,
CONF *conf, char *section)
{
EVP_PKEY *pktmp;
pktmp = X509_get_pubkey(x);
EVP_PKEY_copy_parameters(pktmp,pkey);
EVP_PKEY_save_parameters(pktmp,1);
EVP_PKEY_free(pktmp);
if (!X509_set_issuer_name(x,X509_get_subject_name(x))) goto err;
if (X509_gmtime_adj(X509_get_notBefore(x),0) == NULL) goto err;
if (X509_gmtime_adj(X509_get_notAfter(x),(long)60*60*24*days) == NULL)
goto err;
if (!X509_set_pubkey(x,pkey)) goto err;
if (clrext)
{
while (X509_get_ext_count(x) > 0) X509_delete_ext(x, 0);
}
if (conf)
{
X509V3_CTX ctx;
X509_set_version(x,2);
X509V3_set_ctx(&ctx, x, x, NULL, NULL, 0);
X509V3_set_nconf(&ctx, conf);
if (!X509V3_EXT_add_nconf(conf, &ctx, section, x)) goto err;
}
if (!X509_sign(x,pkey,digest)) goto err;
return 1;
err:
ERR_print_errors(bio_err);
return 0;
}
|
['static int sign(X509 *x, EVP_PKEY *pkey, int days, int clrext, const EVP_MD *digest,\n\t\t\t\t\t\tCONF *conf, char *section)\n\t{\n\tEVP_PKEY *pktmp;\n\tpktmp = X509_get_pubkey(x);\n\tEVP_PKEY_copy_parameters(pktmp,pkey);\n\tEVP_PKEY_save_parameters(pktmp,1);\n\tEVP_PKEY_free(pktmp);\n\tif (!X509_set_issuer_name(x,X509_get_subject_name(x))) goto err;\n\tif (X509_gmtime_adj(X509_get_notBefore(x),0) == NULL) goto err;\n\tif (X509_gmtime_adj(X509_get_notAfter(x),(long)60*60*24*days) == NULL)\n\t\tgoto err;\n\tif (!X509_set_pubkey(x,pkey)) goto err;\n\tif (clrext)\n\t\t{\n\t\twhile (X509_get_ext_count(x) > 0) X509_delete_ext(x, 0);\n\t\t}\n\tif (conf)\n\t\t{\n\t\tX509V3_CTX ctx;\n\t\tX509_set_version(x,2);\n X509V3_set_ctx(&ctx, x, x, NULL, NULL, 0);\n X509V3_set_nconf(&ctx, conf);\n if (!X509V3_EXT_add_nconf(conf, &ctx, section, x)) goto err;\n\t\t}\n\tif (!X509_sign(x,pkey,digest)) goto err;\n\treturn 1;\nerr:\n\tERR_print_errors(bio_err);\n\treturn 0;\n\t}', 'EVP_PKEY *X509_get_pubkey(X509 *x)\n\t{\n\tif ((x == NULL) || (x->cert_info == NULL))\n\t\treturn(NULL);\n\treturn(X509_PUBKEY_get(x->cert_info->key));\n\t}', 'EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key)\n\t{\n\tEVP_PKEY *ret=NULL;\n\tlong j;\n\tint type;\n\tunsigned char *p;\n#ifndef OPENSSL_NO_DSA\n\tconst unsigned char *cp;\n\tX509_ALGOR *a;\n#endif\n\tif (key == NULL) goto err;\n\tif (key->pkey != NULL)\n\t {\n\t CRYPTO_add(&key->pkey->references,1,CRYPTO_LOCK_EVP_PKEY);\n\t return(key->pkey);\n\t }\n\tif (key->public_key == NULL) goto err;\n\ttype=OBJ_obj2nid(key->algor->algorithm);\n\tp=key->public_key->data;\n j=key->public_key->length;\n if ((ret=d2i_PublicKey(type,NULL,&p,(long)j)) == NULL)\n\t\t{\n\t\tX509err(X509_F_X509_PUBKEY_GET,X509_R_ERR_ASN1_LIB);\n\t\tgoto err;\n\t\t}\n\tret->save_parameters=0;\n#ifndef OPENSSL_NO_DSA\n\ta=key->algor;\n\tif (ret->type == EVP_PKEY_DSA)\n\t\t{\n\t\tif (a->parameter && (a->parameter->type == V_ASN1_SEQUENCE))\n\t\t\t{\n\t\t\tret->pkey.dsa->write_params=0;\n\t\t\tcp=p=a->parameter->value.sequence->data;\n\t\t\tj=a->parameter->value.sequence->length;\n\t\t\tif (!d2i_DSAparams(&ret->pkey.dsa,&cp,(long)j))\n\t\t\t\tgoto err;\n\t\t\t}\n\t\tret->save_parameters=1;\n\t\t}\n#endif\n\tkey->pkey=ret;\n\tCRYPTO_add(&ret->references,1,CRYPTO_LOCK_EVP_PKEY);\n\treturn(ret);\nerr:\n\tif (ret != NULL)\n\t\tEVP_PKEY_free(ret);\n\treturn(NULL);\n\t}', 'int OBJ_obj2nid(const ASN1_OBJECT *a)\n\t{\n\tASN1_OBJECT **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=(ADDED_OBJ *)lh_retrieve(added,&ad);\n\t\tif (adp != NULL) return (adp->obj->nid);\n\t\t}\n\top=(ASN1_OBJECT **)OBJ_bsearch((char *)&a,(char *)obj_objs,NUM_OBJ,\n\t\tsizeof(ASN1_OBJECT *),obj_cmp);\n\tif (op == NULL)\n\t\treturn(NID_undef);\n\treturn((*op)->nid);\n\t}', 'void *lh_retrieve(LHASH *lh, const void *data)\n\t{\n\tunsigned long hash;\n\tLHASH_NODE **rn;\n\tconst void *ret;\n\tlh->error=0;\n\trn=getrn(lh,data,&hash);\n\tif (*rn == NULL)\n\t\t{\n\t\tlh->num_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((void *)ret);\n\t}', 'EVP_PKEY *d2i_PublicKey(int type, EVP_PKEY **a, unsigned char **pp,\n\t long length)\n\t{\n\tEVP_PKEY *ret;\n\tif ((a == NULL) || (*a == NULL))\n\t\t{\n\t\tif ((ret=EVP_PKEY_new()) == NULL)\n\t\t\t{\n\t\t\tASN1err(ASN1_F_D2I_PUBLICKEY,ERR_R_EVP_LIB);\n\t\t\treturn(NULL);\n\t\t\t}\n\t\t}\n\telse\tret= *a;\n\tret->save_type=type;\n\tret->type=EVP_PKEY_type(type);\n\tswitch (ret->type)\n\t\t{\n#ifndef OPENSSL_NO_RSA\n\tcase EVP_PKEY_RSA:\n\t\tif ((ret->pkey.rsa=d2i_RSAPublicKey(NULL,\n\t\t\t(const unsigned char **)pp,length)) == NULL)\n\t\t\t{\n\t\t\tASN1err(ASN1_F_D2I_PUBLICKEY,ERR_R_ASN1_LIB);\n\t\t\tgoto err;\n\t\t\t}\n\t\tbreak;\n#endif\n#ifndef OPENSSL_NO_DSA\n\tcase EVP_PKEY_DSA:\n\t\tif ((ret->pkey.dsa=d2i_DSAPublicKey(NULL,\n\t\t\t(const unsigned char **)pp,length)) == NULL)\n\t\t\t{\n\t\t\tASN1err(ASN1_F_D2I_PUBLICKEY,ERR_R_ASN1_LIB);\n\t\t\tgoto err;\n\t\t\t}\n\t\tbreak;\n#endif\n\tdefault:\n\t\tASN1err(ASN1_F_D2I_PUBLICKEY,ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE);\n\t\tgoto err;\n\t\t}\n\tif (a != NULL) (*a)=ret;\n\treturn(ret);\nerr:\n\tif ((ret != NULL) && ((a == NULL) || (*a != ret))) EVP_PKEY_free(ret);\n\treturn(NULL);\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}', 'int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)\n\t{\n#ifndef OPENSSL_NO_DSA\n\tif (pkey->type == EVP_PKEY_DSA)\n\t\t{\n\t\tint ret=pkey->save_parameters;\n\t\tif (mode >= 0)\n\t\t\tpkey->save_parameters=mode;\n\t\treturn(ret);\n\t\t}\n#endif\n\treturn(0);\n\t}']
|
1,049 | 0 |
https://github.com/libav/libav/blob/47399ccdfd93d337c96c76fbf591f0e3637131ef/libavcodec/bitstream.h/#L236
|
static inline void skip_remaining(BitstreamContext *bc, unsigned n)
{
#ifdef BITSTREAM_READER_LE
bc->bits >>= n;
#else
bc->bits <<= n;
#endif
bc->bits_left -= n;
}
|
['static int decode_pic(AVSContext *h)\n{\n int ret;\n int skip_count = -1;\n enum cavs_mb mb_type;\n if (!h->top_qp) {\n av_log(h->avctx, AV_LOG_ERROR, "No sequence header decoded yet\\n");\n return AVERROR_INVALIDDATA;\n }\n av_frame_unref(h->cur.f);\n bitstream_skip(&h->bc, 16);\n if (h->stc == PIC_PB_START_CODE) {\n h->cur.f->pict_type = bitstream_read(&h->bc, 2) + AV_PICTURE_TYPE_I;\n if (h->cur.f->pict_type > AV_PICTURE_TYPE_B) {\n av_log(h->avctx, AV_LOG_ERROR, "illegal picture type\\n");\n return AVERROR_INVALIDDATA;\n }\n if (!h->DPB[0].f->data[0] ||\n (!h->DPB[1].f->data[0] && h->cur.f->pict_type == AV_PICTURE_TYPE_B))\n return AVERROR_INVALIDDATA;\n } else {\n h->cur.f->pict_type = AV_PICTURE_TYPE_I;\n if (bitstream_read_bit(&h->bc))\n bitstream_skip(&h->bc, 24);\n if (h->low_delay || !(bitstream_peek(&h->bc, 9) & 1))\n h->stream_revision = 1;\n else if (bitstream_peek(&h->bc, 11) & 3)\n h->stream_revision = 1;\n if (h->stream_revision > 0)\n bitstream_skip(&h->bc, 1);\n }\n ret = ff_get_buffer(h->avctx, h->cur.f, h->cur.f->pict_type == AV_PICTURE_TYPE_B ?\n 0 : AV_GET_BUFFER_FLAG_REF);\n if (ret < 0)\n return ret;\n if (!h->edge_emu_buffer) {\n int alloc_size = FFALIGN(FFABS(h->cur.f->linesize[0]) + 32, 32);\n h->edge_emu_buffer = av_mallocz(alloc_size * 2 * 24);\n if (!h->edge_emu_buffer)\n return AVERROR(ENOMEM);\n }\n ff_cavs_init_pic(h);\n h->cur.poc = bitstream_read(&h->bc, 8) * 2;\n if (h->cur.f->pict_type != AV_PICTURE_TYPE_B) {\n h->dist[0] = (h->cur.poc - h->DPB[0].poc + 512) % 512;\n } else {\n h->dist[0] = (h->DPB[0].poc - h->cur.poc + 512) % 512;\n }\n h->dist[1] = (h->cur.poc - h->DPB[1].poc + 512) % 512;\n h->scale_den[0] = h->dist[0] ? 512/h->dist[0] : 0;\n h->scale_den[1] = h->dist[1] ? 512/h->dist[1] : 0;\n if (h->cur.f->pict_type == AV_PICTURE_TYPE_B) {\n h->sym_factor = h->dist[0] * h->scale_den[1];\n } else {\n h->direct_den[0] = h->dist[0] ? 16384 / h->dist[0] : 0;\n h->direct_den[1] = h->dist[1] ? 16384 / h->dist[1] : 0;\n }\n if (h->low_delay)\n get_ue_golomb(&h->bc);\n h->progressive = bitstream_read_bit(&h->bc);\n h->pic_structure = 1;\n if (!h->progressive)\n h->pic_structure = bitstream_read_bit(&h->bc);\n if (!h->pic_structure && h->stc == PIC_PB_START_CODE)\n bitstream_skip(&h->bc, 1);\n bitstream_skip(&h->bc, 1);\n bitstream_skip(&h->bc, 1);\n h->qp_fixed = bitstream_read_bit(&h->bc);\n h->qp = bitstream_read(&h->bc, 6);\n if (h->cur.f->pict_type == AV_PICTURE_TYPE_I) {\n if (!h->progressive && !h->pic_structure)\n bitstream_skip(&h->bc, 1);\n bitstream_skip(&h->bc, 4);\n } else {\n if (!(h->cur.f->pict_type == AV_PICTURE_TYPE_B && h->pic_structure == 1))\n h->ref_flag = bitstream_read_bit(&h->bc);\n bitstream_skip(&h->bc, 4);\n h->skip_mode_flag = bitstream_read_bit(&h->bc);\n }\n h->loop_filter_disable = bitstream_read_bit(&h->bc);\n if (!h->loop_filter_disable && bitstream_read_bit(&h->bc)) {\n h->alpha_offset = get_se_golomb(&h->bc);\n h->beta_offset = get_se_golomb(&h->bc);\n } else {\n h->alpha_offset = h->beta_offset = 0;\n }\n if (h->cur.f->pict_type == AV_PICTURE_TYPE_I) {\n do {\n check_for_slice(h);\n decode_mb_i(h, 0);\n } while (ff_cavs_next_mb(h));\n } else if (h->cur.f->pict_type == AV_PICTURE_TYPE_P) {\n do {\n if (check_for_slice(h))\n skip_count = -1;\n if (h->skip_mode_flag && (skip_count < 0))\n skip_count = get_ue_golomb(&h->bc);\n if (h->skip_mode_flag && skip_count--) {\n decode_mb_p(h, P_SKIP);\n } else {\n mb_type = get_ue_golomb(&h->bc) + P_SKIP + h->skip_mode_flag;\n if (mb_type > P_8X8)\n decode_mb_i(h, mb_type - P_8X8 - 1);\n else\n decode_mb_p(h, mb_type);\n }\n } while (ff_cavs_next_mb(h));\n } else {\n do {\n if (check_for_slice(h))\n skip_count = -1;\n if (h->skip_mode_flag && (skip_count < 0))\n skip_count = get_ue_golomb(&h->bc);\n if (h->skip_mode_flag && skip_count--) {\n decode_mb_b(h, B_SKIP);\n } else {\n mb_type = get_ue_golomb(&h->bc) + B_SKIP + h->skip_mode_flag;\n if (mb_type > B_8X8)\n decode_mb_i(h, mb_type - B_8X8 - 1);\n else\n decode_mb_b(h, mb_type);\n }\n } while (ff_cavs_next_mb(h));\n }\n if (h->cur.f->pict_type != AV_PICTURE_TYPE_B) {\n av_frame_unref(h->DPB[1].f);\n FFSWAP(AVSFrame, h->cur, h->DPB[1]);\n FFSWAP(AVSFrame, h->DPB[0], h->DPB[1]);\n }\n return 0;\n}', 'static inline void bitstream_skip(BitstreamContext *bc, unsigned n)\n{\n if (n < bc->bits_left)\n skip_remaining(bc, n);\n else {\n n -= bc->bits_left;\n bc->bits = 0;\n bc->bits_left = 0;\n if (n >= 64) {\n unsigned skip = n / 8;\n n -= skip * 8;\n bc->ptr += skip;\n }\n refill_64(bc);\n if (n)\n skip_remaining(bc, n);\n }\n}', 'static inline void refill_64(BitstreamContext *bc)\n{\n if (bc->ptr >= bc->buffer_end)\n return;\n#ifdef BITSTREAM_READER_LE\n bc->bits = AV_RL64(bc->ptr);\n#else\n bc->bits = AV_RB64(bc->ptr);\n#endif\n bc->ptr += 8;\n bc->bits_left = 64;\n}', 'static inline void skip_remaining(BitstreamContext *bc, unsigned n)\n{\n#ifdef BITSTREAM_READER_LE\n bc->bits >>= n;\n#else\n bc->bits <<= n;\n#endif\n bc->bits_left -= n;\n}']
|
1,050 | 0 |
https://github.com/libav/libav/blob/8305041e137f4f2a49669dd588bf6ccfbbac2b58/libavformat/dv.c/#L123
|
static int dv_extract_audio(uint8_t* frame, uint8_t* ppcm[4],
const DVprofile *sys)
{
int size, chan, i, j, d, of, smpls, freq, quant, half_ch;
uint16_t lc, rc;
const uint8_t* as_pack;
uint8_t *pcm, ipcm;
as_pack = dv_extract_pack(frame, dv_audio_source);
if (!as_pack)
return 0;
smpls = as_pack[1] & 0x3f;
freq = (as_pack[4] >> 3) & 0x07;
quant = as_pack[4] & 0x07;
if (quant > 1)
return -1;
size = (sys->audio_min_samples[freq] + smpls) * 4;
half_ch = sys->difseg_size / 2;
ipcm = (sys->height == 720 && !(frame[1] & 0x0C)) ? 2 : 0;
pcm = ppcm[ipcm++];
for (chan = 0; chan < sys->n_difchan; chan++) {
for (i = 0; i < sys->difseg_size; i++) {
frame += 6 * 80;
if (quant == 1 && i == half_ch) {
pcm = ppcm[ipcm++];
if (!pcm)
break;
}
for (j = 0; j < 9; j++) {
for (d = 8; d < 80; d += 2) {
if (quant == 0) {
of = sys->audio_shuffle[i][j] + (d - 8) / 2 * sys->audio_stride;
if (of*2 >= size)
continue;
pcm[of*2] = frame[d+1];
pcm[of*2+1] = frame[d];
if (pcm[of*2+1] == 0x80 && pcm[of*2] == 0x00)
pcm[of*2+1] = 0;
} else {
lc = ((uint16_t)frame[d] << 4) |
((uint16_t)frame[d+2] >> 4);
rc = ((uint16_t)frame[d+1] << 4) |
((uint16_t)frame[d+2] & 0x0f);
lc = (lc == 0x800 ? 0 : dv_audio_12to16(lc));
rc = (rc == 0x800 ? 0 : dv_audio_12to16(rc));
of = sys->audio_shuffle[i%half_ch][j] + (d - 8) / 3 * sys->audio_stride;
if (of*2 >= size)
continue;
pcm[of*2] = lc & 0xff;
pcm[of*2+1] = lc >> 8;
of = sys->audio_shuffle[i%half_ch+half_ch][j] +
(d - 8) / 3 * sys->audio_stride;
pcm[of*2] = rc & 0xff;
pcm[of*2+1] = rc >> 8;
++d;
}
}
frame += 16 * 80;
}
}
pcm = ppcm[ipcm++];
if (!pcm)
break;
}
return size;
}
|
['static int dv1394_read_packet(AVFormatContext *context, AVPacket *pkt)\n{\n struct dv1394_data *dv = context->priv_data;\n int size;\n size = avpriv_dv_get_packet(dv->dv_demux, pkt);\n if (size > 0)\n return size;\n if (!dv->avail) {\n struct dv1394_status s;\n struct pollfd p;\n if (dv->done) {\n if (ioctl(dv->fd, DV1394_RECEIVE_FRAMES, dv->done) < 0) {\n av_log(context, AV_LOG_ERROR, "DV1394: Ring buffer overflow. Reseting ..\\n");\n dv1394_reset(dv);\n dv1394_start(dv);\n }\n dv->done = 0;\n }\nrestart_poll:\n p.fd = dv->fd;\n p.events = POLLIN | POLLERR | POLLHUP;\n if (poll(&p, 1, -1) < 0) {\n if (errno == EAGAIN || errno == EINTR)\n goto restart_poll;\n av_log(context, AV_LOG_ERROR, "Poll failed: %s\\n", strerror(errno));\n return AVERROR(EIO);\n }\n if (ioctl(dv->fd, DV1394_GET_STATUS, &s) < 0) {\n av_log(context, AV_LOG_ERROR, "Failed to get status: %s\\n", strerror(errno));\n return AVERROR(EIO);\n }\n av_dlog(context, "DV1394: status\\n"\n "\\tactive_frame\\t%d\\n"\n "\\tfirst_clear_frame\\t%d\\n"\n "\\tn_clear_frames\\t%d\\n"\n "\\tdropped_frames\\t%d\\n",\n s.active_frame, s.first_clear_frame,\n s.n_clear_frames, s.dropped_frames);\n dv->avail = s.n_clear_frames;\n dv->index = s.first_clear_frame;\n dv->done = 0;\n if (s.dropped_frames) {\n av_log(context, AV_LOG_ERROR, "DV1394: Frame drop detected (%d). Reseting ..\\n",\n s.dropped_frames);\n dv1394_reset(dv);\n dv1394_start(dv);\n }\n }\n av_dlog(context, "index %d, avail %d, done %d\\n", dv->index, dv->avail,\n dv->done);\n size = avpriv_dv_produce_packet(dv->dv_demux, pkt,\n dv->ring + (dv->index * DV1394_PAL_FRAME_SIZE),\n DV1394_PAL_FRAME_SIZE);\n dv->index = (dv->index + 1) % DV1394_RING_FRAMES;\n dv->done++; dv->avail--;\n return size;\n}', 'int avpriv_dv_produce_packet(DVDemuxContext *c, AVPacket *pkt,\n uint8_t* buf, int buf_size)\n{\n int size, i;\n uint8_t *ppcm[4] = {0};\n if (buf_size < DV_PROFILE_BYTES ||\n !(c->sys = avpriv_dv_frame_profile(c->sys, buf, buf_size)) ||\n buf_size < c->sys->frame_size) {\n return -1;\n }\n size = dv_extract_audio_info(c, buf);\n for (i = 0; i < c->ach; i++) {\n c->audio_pkt[i].size = size;\n c->audio_pkt[i].pts = c->abytes * 30000*8 / c->ast[i]->codec->bit_rate;\n ppcm[i] = c->audio_buf[i];\n }\n dv_extract_audio(buf, ppcm, c->sys);\n if (c->sys->height == 720) {\n if (buf[1] & 0x0C) {\n c->audio_pkt[2].size = c->audio_pkt[3].size = 0;\n } else {\n c->audio_pkt[0].size = c->audio_pkt[1].size = 0;\n c->abytes += size;\n }\n } else {\n c->abytes += size;\n }\n size = dv_extract_video_info(c, buf);\n av_init_packet(pkt);\n pkt->data = buf;\n pkt->size = size;\n pkt->flags |= AV_PKT_FLAG_KEY;\n pkt->stream_index = c->vst->id;\n pkt->pts = c->frames;\n c->frames++;\n return size;\n}', 'const DVprofile* avpriv_dv_frame_profile(const DVprofile *sys,\n const uint8_t* frame, unsigned buf_size)\n{\n int i;\n int dsf = (frame[3] & 0x80) >> 7;\n int stype = frame[80*5 + 48 + 3] & 0x1f;\n if (dsf == 1 && stype == 0 && frame[4] & 0x07 ) {\n return &dv_profiles[2];\n }\n for (i=0; i<FF_ARRAY_ELEMS(dv_profiles); i++)\n if (dsf == dv_profiles[i].dsf && stype == dv_profiles[i].video_stype)\n return &dv_profiles[i];\n if (sys && buf_size == sys->frame_size)\n return sys;\n return NULL;\n}', 'static int dv_extract_audio(uint8_t* frame, uint8_t* ppcm[4],\n const DVprofile *sys)\n{\n int size, chan, i, j, d, of, smpls, freq, quant, half_ch;\n uint16_t lc, rc;\n const uint8_t* as_pack;\n uint8_t *pcm, ipcm;\n as_pack = dv_extract_pack(frame, dv_audio_source);\n if (!as_pack)\n return 0;\n smpls = as_pack[1] & 0x3f;\n freq = (as_pack[4] >> 3) & 0x07;\n quant = as_pack[4] & 0x07;\n if (quant > 1)\n return -1;\n size = (sys->audio_min_samples[freq] + smpls) * 4;\n half_ch = sys->difseg_size / 2;\n ipcm = (sys->height == 720 && !(frame[1] & 0x0C)) ? 2 : 0;\n pcm = ppcm[ipcm++];\n for (chan = 0; chan < sys->n_difchan; chan++) {\n for (i = 0; i < sys->difseg_size; i++) {\n frame += 6 * 80;\n if (quant == 1 && i == half_ch) {\n pcm = ppcm[ipcm++];\n if (!pcm)\n break;\n }\n for (j = 0; j < 9; j++) {\n for (d = 8; d < 80; d += 2) {\n if (quant == 0) {\n of = sys->audio_shuffle[i][j] + (d - 8) / 2 * sys->audio_stride;\n if (of*2 >= size)\n continue;\n pcm[of*2] = frame[d+1];\n pcm[of*2+1] = frame[d];\n if (pcm[of*2+1] == 0x80 && pcm[of*2] == 0x00)\n pcm[of*2+1] = 0;\n } else {\n lc = ((uint16_t)frame[d] << 4) |\n ((uint16_t)frame[d+2] >> 4);\n rc = ((uint16_t)frame[d+1] << 4) |\n ((uint16_t)frame[d+2] & 0x0f);\n lc = (lc == 0x800 ? 0 : dv_audio_12to16(lc));\n rc = (rc == 0x800 ? 0 : dv_audio_12to16(rc));\n of = sys->audio_shuffle[i%half_ch][j] + (d - 8) / 3 * sys->audio_stride;\n if (of*2 >= size)\n continue;\n pcm[of*2] = lc & 0xff;\n pcm[of*2+1] = lc >> 8;\n of = sys->audio_shuffle[i%half_ch+half_ch][j] +\n (d - 8) / 3 * sys->audio_stride;\n pcm[of*2] = rc & 0xff;\n pcm[of*2+1] = rc >> 8;\n ++d;\n }\n }\n frame += 16 * 80;\n }\n }\n pcm = ppcm[ipcm++];\n if (!pcm)\n break;\n }\n return size;\n}', 'static const uint8_t* dv_extract_pack(uint8_t* frame, enum dv_pack_type t)\n{\n int offs;\n switch (t) {\n case dv_audio_source:\n offs = (80*6 + 80*16*3 + 3);\n break;\n case dv_audio_control:\n offs = (80*6 + 80*16*4 + 3);\n break;\n case dv_video_control:\n offs = (80*5 + 48 + 5);\n break;\n default:\n return NULL;\n }\n return frame[offs] == t ? &frame[offs] : NULL;\n}']
|
1,051 | 0 |
https://github.com/openssl/openssl/blob/02703c74a4c10f3848cdd5e7ff5196ab45c7e67e/ssl/s3_clnt.c/#L2351
|
int ssl3_check_cert_and_algorithm(SSL *s)
{
int i,idx;
long algs;
EVP_PKEY *pkey=NULL;
SESS_CERT *sc;
#ifndef OPENSSL_NO_RSA
RSA *rsa;
#endif
#ifndef OPENSSL_NO_DH
DH *dh;
#endif
sc=s->session->sess_cert;
if (sc == NULL)
{
SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,ERR_R_INTERNAL_ERROR);
goto err;
}
algs=s->s3->tmp.new_cipher->algorithms;
if (algs & (SSL_aDH|SSL_aNULL|SSL_aKRB5))
return(1);
#ifndef OPENSSL_NO_RSA
rsa=s->session->sess_cert->peer_rsa_tmp;
#endif
#ifndef OPENSSL_NO_DH
dh=s->session->sess_cert->peer_dh_tmp;
#endif
idx=sc->peer_cert_type;
#ifndef OPENSSL_NO_ECDH
if (idx == SSL_PKEY_ECC)
{
if (check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509,
s->s3->tmp.new_cipher) == 0)
{
SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_BAD_ECC_CERT);
goto f_err;
}
else
{
return 1;
}
}
#endif
pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);
i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);
EVP_PKEY_free(pkey);
if ((algs & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
{
SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT);
goto f_err;
}
#ifndef OPENSSL_NO_DSA
else if ((algs & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
{
SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT);
goto f_err;
}
#endif
#ifndef OPENSSL_NO_RSA
if ((algs & SSL_kRSA) &&
!(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
{
SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT);
goto f_err;
}
#endif
#ifndef OPENSSL_NO_DH
if ((algs & SSL_kEDH) &&
!(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
{
SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY);
goto f_err;
}
else if ((algs & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))
{
SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT);
goto f_err;
}
#ifndef OPENSSL_NO_DSA
else if ((algs & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
{
SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT);
goto f_err;
}
#endif
#endif
if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP))
{
#ifndef OPENSSL_NO_RSA
if (algs & SSL_kRSA)
{
if (rsa == NULL
|| RSA_size(rsa)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
{
SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
goto f_err;
}
}
else
#endif
#ifndef OPENSSL_NO_DH
if (algs & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
{
if (dh == NULL
|| DH_size(dh)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
{
SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY);
goto f_err;
}
}
else
#endif
{
SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
goto f_err;
}
}
return(1);
f_err:
ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
err:
return(0);
}
|
['int ssl3_check_cert_and_algorithm(SSL *s)\n\t{\n\tint i,idx;\n\tlong algs;\n\tEVP_PKEY *pkey=NULL;\n\tSESS_CERT *sc;\n#ifndef OPENSSL_NO_RSA\n\tRSA *rsa;\n#endif\n#ifndef OPENSSL_NO_DH\n\tDH *dh;\n#endif\n\tsc=s->session->sess_cert;\n\tif (sc == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,ERR_R_INTERNAL_ERROR);\n\t\tgoto err;\n\t\t}\n\talgs=s->s3->tmp.new_cipher->algorithms;\n\tif (algs & (SSL_aDH|SSL_aNULL|SSL_aKRB5))\n\t\treturn(1);\n#ifndef OPENSSL_NO_RSA\n\trsa=s->session->sess_cert->peer_rsa_tmp;\n#endif\n#ifndef OPENSSL_NO_DH\n\tdh=s->session->sess_cert->peer_dh_tmp;\n#endif\n\tidx=sc->peer_cert_type;\n#ifndef OPENSSL_NO_ECDH\n\tif (idx == SSL_PKEY_ECC)\n\t\t{\n\t\tif (check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509,\n\t\t s->s3->tmp.new_cipher) == 0)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_BAD_ECC_CERT);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\treturn 1;\n\t\t\t}\n\t\t}\n#endif\n\tpkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);\n\ti=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);\n\tEVP_PKEY_free(pkey);\n\tif ((algs & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))\n\t\t{\n\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT);\n\t\tgoto f_err;\n\t\t}\n#ifndef OPENSSL_NO_DSA\n\telse if ((algs & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))\n\t\t{\n\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT);\n\t\tgoto f_err;\n\t\t}\n#endif\n#ifndef OPENSSL_NO_RSA\n\tif ((algs & SSL_kRSA) &&\n\t\t!(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))\n\t\t{\n\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT);\n\t\tgoto f_err;\n\t\t}\n#endif\n#ifndef OPENSSL_NO_DH\n\tif ((algs & SSL_kEDH) &&\n\t\t!(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))\n\t\t{\n\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY);\n\t\tgoto f_err;\n\t\t}\n\telse if ((algs & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))\n\t\t{\n\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT);\n\t\tgoto f_err;\n\t\t}\n#ifndef OPENSSL_NO_DSA\n\telse if ((algs & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))\n\t\t{\n\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT);\n\t\tgoto f_err;\n\t\t}\n#endif\n#endif\n\tif (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP))\n\t\t{\n#ifndef OPENSSL_NO_RSA\n\t\tif (algs & SSL_kRSA)\n\t\t\t{\n\t\t\tif (rsa == NULL\n\t\t\t || RSA_size(rsa)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\t}\n\t\telse\n#endif\n#ifndef OPENSSL_NO_DH\n\t\t\tif (algs & (SSL_kEDH|SSL_kDHr|SSL_kDHd))\n\t\t\t {\n\t\t\t if (dh == NULL\n\t\t\t\t|| DH_size(dh)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))\n\t\t\t\t{\n\t\t\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\t}\n\t\telse\n#endif\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\t}\n\treturn(1);\nf_err:\n\tssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);\nerr:\n\treturn(0);\n\t}', 'EVP_PKEY *X509_get_pubkey(X509 *x)\n\t{\n\tif ((x == NULL) || (x->cert_info == NULL))\n\t\treturn(NULL);\n\treturn(X509_PUBKEY_get(x->cert_info->key));\n\t}', 'EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key)\n\t{\n\tEVP_PKEY *ret=NULL;\n\tlong j;\n\tint type;\n\tconst unsigned char *p;\n#if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)\n\tconst unsigned char *cp;\n\tX509_ALGOR *a;\n#endif\n\tif (key == NULL) goto err;\n\tif (key->pkey != NULL)\n\t\t{\n\t\tCRYPTO_add(&key->pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);\n\t\treturn(key->pkey);\n\t\t}\n\tif (key->public_key == NULL) goto err;\n\ttype=OBJ_obj2nid(key->algor->algorithm);\n\tif ((ret = EVP_PKEY_new()) == NULL)\n\t\t{\n\t\tX509err(X509_F_X509_PUBKEY_GET, ERR_R_MALLOC_FAILURE);\n\t\tgoto err;\n\t\t}\n\tret->type = EVP_PKEY_type(type);\n#if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)\n\ta=key->algor;\n#endif\n\tif (0)\n\t\t;\n#ifndef OPENSSL_NO_DSA\n\telse if (ret->type == EVP_PKEY_DSA)\n\t\t{\n\t\tif (a->parameter && (a->parameter->type == V_ASN1_SEQUENCE))\n\t\t\t{\n\t\t\tif ((ret->pkey.dsa = DSA_new()) == NULL)\n\t\t\t\t{\n\t\t\t\tX509err(X509_F_X509_PUBKEY_GET, ERR_R_MALLOC_FAILURE);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tret->pkey.dsa->write_params=0;\n\t\t\tcp=p=a->parameter->value.sequence->data;\n\t\t\tj=a->parameter->value.sequence->length;\n\t\t\tif (!d2i_DSAparams(&ret->pkey.dsa, &cp, (long)j))\n\t\t\t\tgoto err;\n\t\t\t}\n\t\tret->save_parameters=1;\n\t\t}\n#endif\n#ifndef OPENSSL_NO_EC\n\telse if (ret->type == EVP_PKEY_EC)\n\t\t{\n\t\tif (a->parameter && (a->parameter->type == V_ASN1_SEQUENCE))\n\t\t\t{\n\t\t\tif ((ret->pkey.ec= EC_KEY_new()) == NULL)\n\t\t\t\t{\n\t\t\t\tX509err(X509_F_X509_PUBKEY_GET,\n\t\t\t\t\tERR_R_MALLOC_FAILURE);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tcp = p = a->parameter->value.sequence->data;\n\t\t\tj = a->parameter->value.sequence->length;\n\t\t\tif (!d2i_ECParameters(&ret->pkey.ec, &cp, (long)j))\n\t\t\t\t{\n\t\t\t\tX509err(X509_F_X509_PUBKEY_GET, ERR_R_EC_LIB);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\t}\n\t\telse if (a->parameter && (a->parameter->type == V_ASN1_OBJECT))\n\t\t\t{\n\t\t\tEC_KEY *ec_key;\n\t\t\tEC_GROUP *group;\n\t\t\tif (ret->pkey.ec == NULL)\n\t\t\t\tret->pkey.ec = EC_KEY_new();\n\t\t\tec_key = ret->pkey.ec;\n\t\t\tif (ec_key == NULL)\n\t\t\t\tgoto err;\n\t\t\tgroup = EC_GROUP_new_by_curve_name(OBJ_obj2nid(a->parameter->value.object));\n\t\t\tif (group == NULL)\n\t\t\t\tgoto err;\n\t\t\tEC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);\n\t\t\tif (EC_KEY_set_group(ec_key, group) == 0)\n\t\t\t\tgoto err;\n\t\t\tEC_GROUP_free(group);\n\t\t\t}\n\t\tret->save_parameters = 1;\n\t\t}\n#endif\n\tp=key->public_key->data;\n j=key->public_key->length;\n if (!d2i_PublicKey(type, &ret, &p, (long)j))\n\t\t{\n\t\tX509err(X509_F_X509_PUBKEY_GET, X509_R_ERR_ASN1_LIB);\n\t\tgoto err;\n\t\t}\n\tkey->pkey = ret;\n\tCRYPTO_add(&ret->references, 1, CRYPTO_LOCK_EVP_PKEY);\n\treturn(ret);\nerr:\n\tif (ret != NULL)\n\t\tEVP_PKEY_free(ret);\n\treturn(NULL);\n\t}', 'int OBJ_obj2nid(const ASN1_OBJECT *a)\n\t{\n\tASN1_OBJECT **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=(ADDED_OBJ *)lh_retrieve(added,&ad);\n\t\tif (adp != NULL) return (adp->obj->nid);\n\t\t}\n\top=(ASN1_OBJECT **)OBJ_bsearch((const char *)&a,(const char *)obj_objs,\n\t\tNUM_OBJ, sizeof(ASN1_OBJECT *),obj_cmp);\n\tif (op == NULL)\n\t\treturn(NID_undef);\n\treturn((*op)->nid);\n\t}', 'EVP_PKEY *EVP_PKEY_new(void)\n\t{\n\tEVP_PKEY *ret;\n\tret=(EVP_PKEY *)OPENSSL_malloc(sizeof(EVP_PKEY));\n\tif (ret == NULL)\n\t\t{\n\t\tEVPerr(EVP_F_EVP_PKEY_NEW,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tret->type=EVP_PKEY_NONE;\n\tret->references=1;\n\tret->pkey.ptr=NULL;\n\tret->attributes=NULL;\n\tret->save_parameters=1;\n\treturn(ret);\n\t}', 'void *CRYPTO_malloc(int num, const char *file, int line)\n\t{\n\tvoid *ret = NULL;\n\textern unsigned char cleanse_ctr;\n\tif (num <= 0) return NULL;\n\tallow_customize = 0;\n\tif (malloc_debug_func != NULL)\n\t\t{\n\t\tallow_customize_debug = 0;\n\t\tmalloc_debug_func(NULL, num, file, line, 0);\n\t\t}\n\tret = malloc_ex_func(num,file,line);\n#ifdef LEVITTE_DEBUG_MEM\n\tfprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\\n", ret, num);\n#endif\n\tif (malloc_debug_func != NULL)\n\t\tmalloc_debug_func(ret, num, file, line, 1);\n if(ret && (num > 2048))\n ((unsigned char *)ret)[0] = cleanse_ctr;\n\treturn ret;\n\t}', 'void ERR_put_error(int lib, int func, int reason, const char *file,\n\t int line)\n\t{\n\tERR_STATE *es;\n#ifdef _OSD_POSIX\n\tif (strncmp(file,"*POSIX(", sizeof("*POSIX(")-1) == 0) {\n\t\tchar *end;\n\t\tfile += sizeof("*POSIX(")-1;\n\t\tend = &file[strlen(file)-1];\n\t\tif (*end == \')\')\n\t\t\t*end = \'\\0\';\n\t\tif ((end = strrchr(file, \'/\')) != NULL)\n\t\t\tfile = &end[1];\n\t}\n#endif\n\tes=ERR_get_state();\n\tes->top=(es->top+1)%ERR_NUM_ERRORS;\n\tif (es->top == es->bottom)\n\t\tes->bottom=(es->bottom+1)%ERR_NUM_ERRORS;\n\tes->err_flags[es->top]=0;\n\tes->err_buffer[es->top]=ERR_PACK(lib,func,reason);\n\tes->err_file[es->top]=file;\n\tes->err_line[es->top]=line;\n\terr_clear_data(es,es->top);\n\t}']
|
1,052 | 0 |
https://github.com/openssl/openssl/blob/d40a1b865fddc3d67f8c06ff1f1466fad331c8f7/crypto/bn/bn_lib.c/#L250
|
int BN_num_bits(const BIGNUM *a)
{
int i = a->top - 1;
bn_check_top(a);
if (BN_is_zero(a)) return 0;
return ((i*BN_BITS2) + BN_num_bits_word(a->d[i]));
}
|
['static ASN1_INTEGER *next_serial(const char *serialfile)\n\t{\n\tint ret = 0;\n\tBIO *in = NULL;\n\tASN1_INTEGER *serial = NULL;\n\tBIGNUM *bn = NULL;\n\tif (!(serial = ASN1_INTEGER_new())) goto err;\n\tif (!(in = BIO_new_file(serialfile, "r")))\n\t\t{\n\t\tERR_clear_error();\n\t\tBIO_printf(bio_err, "Warning: could not open file %s for "\n\t\t\t "reading, using serial number: 1\\n", serialfile);\n\t\tif (!ASN1_INTEGER_set(serial, 1)) goto err;\n\t\t}\n\telse\n\t\t{\n\t\tchar buf[1024];\n\t\tif (!a2i_ASN1_INTEGER(in, serial, buf, sizeof(buf)))\n\t\t\t{\n\t\t\tBIO_printf(bio_err, "unable to load number from %s\\n",\n\t\t\t\t serialfile);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (!(bn = ASN1_INTEGER_to_BN(serial, NULL))) goto err;\n\t\tASN1_INTEGER_free(serial);\n\t\tserial = NULL;\n\t\tif (!BN_add_word(bn, 1)) goto err;\n\t\tif (!(serial = BN_to_ASN1_INTEGER(bn, NULL))) goto err;\n\t\t}\n\tret = 1;\n err:\n\tif (!ret)\n\t\t{\n\t\tASN1_INTEGER_free(serial);\n\t\tserial = NULL;\n\t\t}\n\tBIO_free_all(in);\n\tBN_free(bn);\n\treturn serial;\n\t}', 'BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn)\n\t{\n\tBIGNUM *ret;\n\tif ((ret=BN_bin2bn(ai->data,ai->length,bn)) == NULL)\n\t\tASN1err(ASN1_F_ASN1_INTEGER_TO_BN,ASN1_R_BN_LIB);\n\telse if(ai->type == V_ASN1_NEG_INTEGER)\n\t\tBN_set_negative(ret, 1);\n\treturn(ret);\n\t}', 'BIGNUM *BN_bin2bn(const unsigned char *s, size_t len, BIGNUM *ret)\n\t{\n\tunsigned int i,m;\n\tunsigned int n;\n\tBN_ULONG l;\n\tBIGNUM *bn = NULL;\n\tif (ret == NULL)\n\t\tret = bn = BN_new();\n\tif (ret == NULL) return(NULL);\n\tbn_check_top(ret);\n\tl=0;\n\tn=len;\n\tif (n == 0)\n\t\t{\n\t\tret->top=0;\n\t\treturn(ret);\n\t\t}\n\ti=((n-1)/BN_BYTES)+1;\n\tm=((n-1)%(BN_BYTES));\n\tif (bn_wexpand(ret, i) == NULL)\n\t\t{\n\t\tif (bn) BN_free(bn);\n\t\treturn NULL;\n\t\t}\n\tret->top=i;\n\tret->neg=0;\n\twhile (n--)\n\t\t{\n\t\tl=(l<<8L)| *(s++);\n\t\tif (m-- == 0)\n\t\t\t{\n\t\t\tret->d[--i]=l;\n\t\t\tl=0;\n\t\t\tm=BN_BYTES-1;\n\t\t\t}\n\t\t}\n\tbn_correct_top(ret);\n\treturn(ret);\n\t}', 'int BN_add_word(BIGNUM *a, BN_ULONG w)\n\t{\n\tBN_ULONG l;\n\tint i;\n\tbn_check_top(a);\n\tw &= BN_MASK2;\n\tif (!w) return 1;\n\tif(BN_is_zero(a)) return BN_set_word(a, w);\n\tif (a->neg)\n\t\t{\n\t\ta->neg=0;\n\t\ti=BN_sub_word(a,w);\n\t\tif (!BN_is_zero(a))\n\t\t\ta->neg=!(a->neg);\n\t\treturn(i);\n\t\t}\n\tif (((BN_ULONG)(a->d[a->top - 1] + 1) == 0) &&\n\t\t\t(bn_wexpand(a,a->top+1) == NULL))\n\t\treturn(0);\n\ti=0;\n\tfor (;;)\n\t\t{\n\t\tif (i >= a->top)\n\t\t\tl=w;\n\t\telse\n\t\t\tl=(a->d[i]+w)&BN_MASK2;\n\t\ta->d[i]=l;\n\t\tif (w > l)\n\t\t\tw=1;\n\t\telse\n\t\t\tbreak;\n\t\ti++;\n\t\t}\n\tif (i >= a->top)\n\t\ta->top++;\n\tbn_check_top(a);\n\treturn(1);\n\t}', 'ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai)\n\t{\n\tASN1_INTEGER *ret;\n\tsize_t len,j;\n\tif (ai == NULL)\n\t\tret=M_ASN1_INTEGER_new();\n\telse\n\t\tret=ai;\n\tif (ret == NULL)\n\t\t{\n\t\tASN1err(ASN1_F_BN_TO_ASN1_INTEGER,ERR_R_NESTED_ASN1_ERROR);\n\t\tgoto err;\n\t\t}\n\tif (BN_is_negative(bn))\n\t\tret->type = V_ASN1_NEG_INTEGER;\n\telse ret->type=V_ASN1_INTEGER;\n\tj=BN_num_bits(bn);\n\tlen=((j == 0)?0:((j/8)+1));\n\tif (ret->length < len+4)\n\t\t{\n\t\tunsigned char *new_data=OPENSSL_realloc(ret->data, len+4U);\n\t\tif (!new_data)\n\t\t\t{\n\t\t\tASN1err(ASN1_F_BN_TO_ASN1_INTEGER,ERR_R_MALLOC_FAILURE);\n\t\t\tgoto err;\n\t\t\t}\n\t\tret->data=new_data;\n\t\t}\n\tret->length=BN_bn2bin(bn,ret->data);\n\tif(!ret->length)\n\t\t{\n\t\tret->data[0] = 0;\n\t\tret->length = 1;\n\t\t}\n\treturn(ret);\nerr:\n\tif (ret != ai) M_ASN1_INTEGER_free(ret);\n\treturn(NULL);\n\t}', 'int BN_num_bits(const BIGNUM *a)\n\t{\n\tint i = a->top - 1;\n\tbn_check_top(a);\n\tif (BN_is_zero(a)) return 0;\n\treturn ((i*BN_BITS2) + BN_num_bits_word(a->d[i]));\n\t}']
|
1,053 | 0 |
https://github.com/openssl/openssl/blob/9b02dc97e4963969da69675a871dbe80e6d31cda/crypto/bn/bn_lib.c/#L260
|
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
BN_ULONG *a = NULL;
bn_check_top(b);
if (words > (INT_MAX / (4 * BN_BITS2))) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
return NULL;
}
if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
return NULL;
}
if (BN_get_flags(b, BN_FLG_SECURE))
a = OPENSSL_secure_zalloc(words * sizeof(*a));
else
a = OPENSSL_zalloc(words * sizeof(*a));
if (a == NULL) {
BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
return NULL;
}
assert(b->top <= words);
if (b->top > 0)
memcpy(a, b->d, sizeof(*a) * b->top);
return a;
}
|
['int BN_GF2m_mod_exp_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n const int p[], BN_CTX *ctx)\n{\n int ret = 0, i, n;\n BIGNUM *u;\n bn_check_top(a);\n bn_check_top(b);\n if (BN_is_zero(b))\n return BN_one(r);\n if (BN_abs_is_word(b, 1))\n return (BN_copy(r, a) != NULL);\n BN_CTX_start(ctx);\n if ((u = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (!BN_GF2m_mod_arr(u, a, p))\n goto err;\n n = BN_num_bits(b) - 1;\n for (i = n - 1; i >= 0; i--) {\n if (!BN_GF2m_mod_sqr_arr(u, u, p, ctx))\n goto err;\n if (BN_is_bit_set(b, i)) {\n if (!BN_GF2m_mod_mul_arr(u, u, a, p, ctx))\n goto err;\n }\n }\n if (!BN_copy(r, u))\n goto err;\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return 1;\n}', 'int BN_GF2m_mod_arr(BIGNUM *r, const BIGNUM *a, const int p[])\n{\n int j, k;\n int n, dN, d0, d1;\n BN_ULONG zz, *z;\n bn_check_top(a);\n if (!p[0]) {\n BN_zero(r);\n return 1;\n }\n if (a != r) {\n if (!bn_wexpand(r, a->top))\n return 0;\n for (j = 0; j < a->top; j++) {\n r->d[j] = a->d[j];\n }\n r->top = a->top;\n }\n z = r->d;\n dN = p[0] / BN_BITS2;\n for (j = r->top - 1; j > dN;) {\n zz = z[j];\n if (z[j] == 0) {\n j--;\n continue;\n }\n z[j] = 0;\n for (k = 1; p[k] != 0; k++) {\n n = p[0] - p[k];\n d0 = n % BN_BITS2;\n d1 = BN_BITS2 - d0;\n n /= BN_BITS2;\n z[j - n] ^= (zz >> d0);\n if (d0)\n z[j - n - 1] ^= (zz << d1);\n }\n n = dN;\n d0 = p[0] % BN_BITS2;\n d1 = BN_BITS2 - d0;\n z[j - n] ^= (zz >> d0);\n if (d0)\n z[j - n - 1] ^= (zz << d1);\n }\n while (j == dN) {\n d0 = p[0] % BN_BITS2;\n zz = z[dN] >> d0;\n if (zz == 0)\n break;\n d1 = BN_BITS2 - d0;\n if (d0)\n z[dN] = (z[dN] << d1) >> d1;\n else\n z[dN] = 0;\n z[0] ^= zz;\n for (k = 1; p[k] != 0; k++) {\n BN_ULONG tmp_ulong;\n n = p[k] / BN_BITS2;\n d0 = p[k] % BN_BITS2;\n d1 = BN_BITS2 - d0;\n z[n] ^= (zz << d0);\n if (d0 && (tmp_ulong = zz >> d1))\n z[n + 1] ^= tmp_ulong;\n }\n }\n bn_correct_top(r);\n return 1;\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}']
|
1,054 | 0 |
https://github.com/libav/libav/blob/df84d7d9bdf6b8e6896c711880f130b72738c828/libavcodec/mpegaudiodec.c/#L897
|
void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
MPA_INT *window, int *dither_state,
OUT_INT *samples, int incr,
int32_t sb_samples[SBLIMIT])
{
register MPA_INT *synth_buf;
register const MPA_INT *w, *w2, *p;
int j, offset;
OUT_INT *samples2;
#if FRAC_BITS <= 15
int32_t tmp[32];
int sum, sum2;
#else
int64_t sum, sum2;
#endif
offset = *synth_buf_offset;
synth_buf = synth_buf_ptr + offset;
#if FRAC_BITS <= 15
dct32(tmp, sb_samples);
for(j=0;j<32;j++) {
synth_buf[j] = av_clip_int16(tmp[j]);
}
#else
dct32(synth_buf, sb_samples);
#endif
memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));
samples2 = samples + 31 * incr;
w = window;
w2 = window + 31;
sum = *dither_state;
p = synth_buf + 16;
SUM8(MACS, sum, w, p);
p = synth_buf + 48;
SUM8(MLSS, sum, w + 32, p);
*samples = round_sample(&sum);
samples += incr;
w++;
for(j=1;j<16;j++) {
sum2 = 0;
p = synth_buf + 16 + j;
SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);
p = synth_buf + 48 - j;
SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);
*samples = round_sample(&sum);
samples += incr;
sum += sum2;
*samples2 = round_sample(&sum);
samples2 -= incr;
w++;
w2--;
}
p = synth_buf + 32;
SUM8(MLSS, sum, w + 32, p);
*samples = round_sample(&sum);
*dither_state= sum;
offset = (offset - 32) & 511;
*synth_buf_offset = offset;
}
|
['static void mpc_synth(MPCContext *c, int16_t *out)\n{\n int dither_state = 0;\n int i, ch;\n OUT_INT samples[MPA_MAX_CHANNELS * MPA_FRAME_SIZE], *samples_ptr;\n for(ch = 0; ch < 2; ch++){\n samples_ptr = samples + ch;\n for(i = 0; i < SAMPLES_PER_BAND; i++) {\n ff_mpa_synth_filter(c->synth_buf[ch], &(c->synth_buf_offset[ch]),\n ff_mpa_synth_window, &dither_state,\n samples_ptr, 2,\n c->sb_samples[ch][i]);\n samples_ptr += 64;\n }\n }\n for(i = 0; i < MPC_FRAME_SIZE*2; i++)\n *out++=samples[i];\n}', 'void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,\n MPA_INT *window, int *dither_state,\n OUT_INT *samples, int incr,\n int32_t sb_samples[SBLIMIT])\n{\n register MPA_INT *synth_buf;\n register const MPA_INT *w, *w2, *p;\n int j, offset;\n OUT_INT *samples2;\n#if FRAC_BITS <= 15\n int32_t tmp[32];\n int sum, sum2;\n#else\n int64_t sum, sum2;\n#endif\n offset = *synth_buf_offset;\n synth_buf = synth_buf_ptr + offset;\n#if FRAC_BITS <= 15\n dct32(tmp, sb_samples);\n for(j=0;j<32;j++) {\n synth_buf[j] = av_clip_int16(tmp[j]);\n }\n#else\n dct32(synth_buf, sb_samples);\n#endif\n memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));\n samples2 = samples + 31 * incr;\n w = window;\n w2 = window + 31;\n sum = *dither_state;\n p = synth_buf + 16;\n SUM8(MACS, sum, w, p);\n p = synth_buf + 48;\n SUM8(MLSS, sum, w + 32, p);\n *samples = round_sample(&sum);\n samples += incr;\n w++;\n for(j=1;j<16;j++) {\n sum2 = 0;\n p = synth_buf + 16 + j;\n SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);\n p = synth_buf + 48 - j;\n SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);\n *samples = round_sample(&sum);\n samples += incr;\n sum += sum2;\n *samples2 = round_sample(&sum);\n samples2 -= incr;\n w++;\n w2--;\n }\n p = synth_buf + 32;\n SUM8(MLSS, sum, w + 32, p);\n *samples = round_sample(&sum);\n *dither_state= sum;\n offset = (offset - 32) & 511;\n *synth_buf_offset = offset;\n}']
|
1,055 | 0 |
https://github.com/libav/libav/blob/87b2b40a3342185e7e99aca8c34c4201629489ca/ffmpeg.c/#L3130
|
static enum CodecID find_codec_or_die(const char *name, int type, int encoder, int strict)
{
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);
ffmpeg_exit(1);
}
if(codec->type != type) {
fprintf(stderr, "Invalid %s type '%s'\n", codec_string, name);
ffmpeg_exit(1);
}
if(codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
strict > FF_COMPLIANCE_EXPERIMENTAL) {
fprintf(stderr, "%s '%s' is experimental and might produce bad "
"results.\nAdd '-strict experimental' if you want to use it.\n",
codec_string, codec->name);
codec = encoder ?
avcodec_find_encoder(codec->id) :
avcodec_find_decoder(codec->id);
if (!(codec->capabilities & CODEC_CAP_EXPERIMENTAL))
fprintf(stderr, "Or use the non experimental %s '%s'.\n",
codec_string, codec->name);
ffmpeg_exit(1);
}
return codec->id;
}
|
['static enum CodecID find_codec_or_die(const char *name, int type, int encoder, int strict)\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 ffmpeg_exit(1);\n }\n if(codec->type != type) {\n fprintf(stderr, "Invalid %s type \'%s\'\\n", codec_string, name);\n ffmpeg_exit(1);\n }\n if(codec->capabilities & CODEC_CAP_EXPERIMENTAL &&\n strict > FF_COMPLIANCE_EXPERIMENTAL) {\n fprintf(stderr, "%s \'%s\' is experimental and might produce bad "\n "results.\\nAdd \'-strict experimental\' if you want to use it.\\n",\n codec_string, codec->name);\n codec = encoder ?\n avcodec_find_encoder(codec->id) :\n avcodec_find_decoder(codec->id);\n if (!(codec->capabilities & CODEC_CAP_EXPERIMENTAL))\n fprintf(stderr, "Or use the non experimental %s \'%s\'.\\n",\n codec_string, codec->name);\n ffmpeg_exit(1);\n }\n return codec->id;\n}']
|
1,056 | 0 |
https://github.com/libav/libav/blob/8f935b9271052be8f97d655081b94b68b6c23bfb/libavformat/nutdec.c/#L489
|
static int decode_syncpoint(NUTContext *nut, int64_t *ts, int64_t *back_ptr){
AVFormatContext *s= nut->avf;
AVIOContext *bc = s->pb;
int64_t end, tmp;
nut->last_syncpoint_pos= url_ftell(bc)-8;
end= get_packetheader(nut, bc, 1, SYNCPOINT_STARTCODE);
end += url_ftell(bc);
tmp= ff_get_v(bc);
*back_ptr= nut->last_syncpoint_pos - 16*ff_get_v(bc);
if(*back_ptr < 0)
return -1;
ff_nut_reset_ts(nut, nut->time_base[tmp % nut->time_base_count], tmp / nut->time_base_count);
if(skip_reserved(bc, end) || get_checksum(bc)){
av_log(s, AV_LOG_ERROR, "sync point checksum mismatch\n");
return -1;
}
*ts= tmp / s->nb_streams * av_q2d(nut->time_base[tmp % s->nb_streams])*AV_TIME_BASE;
ff_nut_add_sp(nut, nut->last_syncpoint_pos, *back_ptr, *ts);
return 0;
}
|
['static int decode_syncpoint(NUTContext *nut, int64_t *ts, int64_t *back_ptr){\n AVFormatContext *s= nut->avf;\n AVIOContext *bc = s->pb;\n int64_t end, tmp;\n nut->last_syncpoint_pos= url_ftell(bc)-8;\n end= get_packetheader(nut, bc, 1, SYNCPOINT_STARTCODE);\n end += url_ftell(bc);\n tmp= ff_get_v(bc);\n *back_ptr= nut->last_syncpoint_pos - 16*ff_get_v(bc);\n if(*back_ptr < 0)\n return -1;\n ff_nut_reset_ts(nut, nut->time_base[tmp % nut->time_base_count], tmp / nut->time_base_count);\n if(skip_reserved(bc, end) || get_checksum(bc)){\n av_log(s, AV_LOG_ERROR, "sync point checksum mismatch\\n");\n return -1;\n }\n *ts= tmp / s->nb_streams * av_q2d(nut->time_base[tmp % s->nb_streams])*AV_TIME_BASE;\n ff_nut_add_sp(nut, nut->last_syncpoint_pos, *back_ptr, *ts);\n return 0;\n}', 'int64_t url_ftell(AVIOContext *s)\n{\n return url_fseek(s, 0, SEEK_CUR);\n}', 'int64_t url_fseek(AVIOContext *s, int64_t offset, int whence)\n{\n int64_t offset1;\n int64_t pos;\n int force = whence & AVSEEK_FORCE;\n whence &= ~AVSEEK_FORCE;\n if(!s)\n return AVERROR(EINVAL);\n pos = s->pos - (s->write_flag ? 0 : (s->buf_end - s->buffer));\n if (whence != SEEK_CUR && whence != SEEK_SET)\n return AVERROR(EINVAL);\n if (whence == SEEK_CUR) {\n offset1 = pos + (s->buf_ptr - s->buffer);\n if (offset == 0)\n return offset1;\n offset += offset1;\n }\n offset1 = offset - pos;\n if (!s->must_flush &&\n offset1 >= 0 && offset1 <= (s->buf_end - s->buffer)) {\n s->buf_ptr = s->buffer + offset1;\n } else if ((s->is_streamed ||\n offset1 <= s->buf_end + SHORT_SEEK_THRESHOLD - s->buffer) &&\n !s->write_flag && offset1 >= 0 &&\n (whence != SEEK_END || force)) {\n while(s->pos < offset && !s->eof_reached)\n fill_buffer(s);\n if (s->eof_reached)\n return AVERROR_EOF;\n s->buf_ptr = s->buf_end + offset - s->pos;\n } else {\n int64_t res;\n#if CONFIG_MUXERS || CONFIG_NETWORK\n if (s->write_flag) {\n flush_buffer(s);\n s->must_flush = 1;\n }\n#endif\n if (!s->seek)\n return AVERROR(EPIPE);\n if ((res = s->seek(s->opaque, offset, SEEK_SET)) < 0)\n return res;\n if (!s->write_flag)\n s->buf_end = s->buffer;\n s->buf_ptr = s->buffer;\n s->pos = offset;\n }\n s->eof_reached = 0;\n return offset;\n}', 'uint64_t ff_get_v(AVIOContext *bc){\n uint64_t val = 0;\n int tmp;\n do{\n tmp = avio_r8(bc);\n val= (val<<7) + (tmp&127);\n }while(tmp&128);\n return val;\n}']
|
1,057 | 0 |
https://github.com/openssl/openssl/blob/cf3404fcc77aaf592c95326cbdd25612a8af6878/crypto/pem/pvkfmt.c/#L647
|
static int derive_pvk_key(unsigned char *key,
const unsigned char *salt, unsigned int saltlen,
const unsigned char *pass, int passlen)
{
EVP_MD_CTX *mctx = EVP_MD_CTX_new();
int rv = 1;
if (mctx == NULL
|| !EVP_DigestInit_ex(mctx, EVP_sha1(), NULL)
|| !EVP_DigestUpdate(mctx, salt, saltlen)
|| !EVP_DigestUpdate(mctx, pass, passlen)
|| !EVP_DigestFinal_ex(mctx, key, NULL))
rv = 0;
EVP_MD_CTX_free(mctx);
return rv;
}
|
['static int derive_pvk_key(unsigned char *key,\n const unsigned char *salt, unsigned int saltlen,\n const unsigned char *pass, int passlen)\n{\n EVP_MD_CTX *mctx = EVP_MD_CTX_new();\n int rv = 1;\n if (mctx == NULL\n || !EVP_DigestInit_ex(mctx, EVP_sha1(), NULL)\n || !EVP_DigestUpdate(mctx, salt, saltlen)\n || !EVP_DigestUpdate(mctx, pass, passlen)\n || !EVP_DigestFinal_ex(mctx, key, NULL))\n rv = 0;\n EVP_MD_CTX_free(mctx);\n return rv;\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}', 'const EVP_MD *EVP_sha1(void)\n{\n return (&sha1_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 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}']
|
1,058 | 0 |
https://github.com/openssl/openssl/blob/ef2499298b26fa84594c8e85fd645bc75179cfdd/crypto/x509v3/v3_utl.c/#L366
|
STACK_OF(CONF_VALUE) *X509V3_parse_list(const char *line)
{
char *p, *q, c;
char *ntmp, *vtmp;
STACK_OF(CONF_VALUE) *values = NULL;
char *linebuf;
int state;
linebuf = OPENSSL_strdup(line);
if (linebuf == NULL) {
X509V3err(X509V3_F_X509V3_PARSE_LIST, ERR_R_MALLOC_FAILURE);
goto err;
}
state = HDR_NAME;
ntmp = NULL;
for (p = linebuf, q = linebuf; (c = *p) && (c != '\r') && (c != '\n');
p++) {
switch (state) {
case HDR_NAME:
if (c == ':') {
state = HDR_VALUE;
*p = 0;
ntmp = strip_spaces(q);
if (!ntmp) {
X509V3err(X509V3_F_X509V3_PARSE_LIST,
X509V3_R_INVALID_NULL_NAME);
goto err;
}
q = p + 1;
} else if (c == ',') {
*p = 0;
ntmp = strip_spaces(q);
q = p + 1;
if (!ntmp) {
X509V3err(X509V3_F_X509V3_PARSE_LIST,
X509V3_R_INVALID_NULL_NAME);
goto err;
}
X509V3_add_value(ntmp, NULL, &values);
}
break;
case HDR_VALUE:
if (c == ',') {
state = HDR_NAME;
*p = 0;
vtmp = strip_spaces(q);
if (!vtmp) {
X509V3err(X509V3_F_X509V3_PARSE_LIST,
X509V3_R_INVALID_NULL_VALUE);
goto err;
}
X509V3_add_value(ntmp, vtmp, &values);
ntmp = NULL;
q = p + 1;
}
}
}
if (state == HDR_VALUE) {
vtmp = strip_spaces(q);
if (!vtmp) {
X509V3err(X509V3_F_X509V3_PARSE_LIST,
X509V3_R_INVALID_NULL_VALUE);
goto err;
}
X509V3_add_value(ntmp, vtmp, &values);
} else {
ntmp = strip_spaces(q);
if (!ntmp) {
X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME);
goto err;
}
X509V3_add_value(ntmp, NULL, &values);
}
OPENSSL_free(linebuf);
return values;
err:
OPENSSL_free(linebuf);
sk_CONF_VALUE_pop_free(values, X509V3_conf_free);
return NULL;
}
|
["STACK_OF(CONF_VALUE) *X509V3_parse_list(const char *line)\n{\n char *p, *q, c;\n char *ntmp, *vtmp;\n STACK_OF(CONF_VALUE) *values = NULL;\n char *linebuf;\n int state;\n linebuf = OPENSSL_strdup(line);\n if (linebuf == NULL) {\n X509V3err(X509V3_F_X509V3_PARSE_LIST, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n state = HDR_NAME;\n ntmp = NULL;\n for (p = linebuf, q = linebuf; (c = *p) && (c != '\\r') && (c != '\\n');\n p++) {\n switch (state) {\n case HDR_NAME:\n if (c == ':') {\n state = HDR_VALUE;\n *p = 0;\n ntmp = strip_spaces(q);\n if (!ntmp) {\n X509V3err(X509V3_F_X509V3_PARSE_LIST,\n X509V3_R_INVALID_NULL_NAME);\n goto err;\n }\n q = p + 1;\n } else if (c == ',') {\n *p = 0;\n ntmp = strip_spaces(q);\n q = p + 1;\n if (!ntmp) {\n X509V3err(X509V3_F_X509V3_PARSE_LIST,\n X509V3_R_INVALID_NULL_NAME);\n goto err;\n }\n X509V3_add_value(ntmp, NULL, &values);\n }\n break;\n case HDR_VALUE:\n if (c == ',') {\n state = HDR_NAME;\n *p = 0;\n vtmp = strip_spaces(q);\n if (!vtmp) {\n X509V3err(X509V3_F_X509V3_PARSE_LIST,\n X509V3_R_INVALID_NULL_VALUE);\n goto err;\n }\n X509V3_add_value(ntmp, vtmp, &values);\n ntmp = NULL;\n q = p + 1;\n }\n }\n }\n if (state == HDR_VALUE) {\n vtmp = strip_spaces(q);\n if (!vtmp) {\n X509V3err(X509V3_F_X509V3_PARSE_LIST,\n X509V3_R_INVALID_NULL_VALUE);\n goto err;\n }\n X509V3_add_value(ntmp, vtmp, &values);\n } else {\n ntmp = strip_spaces(q);\n if (!ntmp) {\n X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME);\n goto err;\n }\n X509V3_add_value(ntmp, NULL, &values);\n }\n OPENSSL_free(linebuf);\n return values;\n err:\n OPENSSL_free(linebuf);\n sk_CONF_VALUE_pop_free(values, X509V3_conf_free);\n return NULL;\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 (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 X509V3_add_value(const char *name, const char *value,\n STACK_OF(CONF_VALUE) **extlist)\n{\n CONF_VALUE *vtmp = NULL;\n char *tname = NULL, *tvalue = NULL;\n if (name && (tname = OPENSSL_strdup(name)) == NULL)\n goto err;\n if (value && (tvalue = OPENSSL_strdup(value)) == NULL)\n goto err;\n if ((vtmp = OPENSSL_malloc(sizeof(*vtmp))) == NULL)\n goto err;\n if (*extlist == NULL && (*extlist = sk_CONF_VALUE_new_null()) == NULL)\n goto err;\n vtmp->section = NULL;\n vtmp->name = tname;\n vtmp->value = tvalue;\n if (!sk_CONF_VALUE_push(*extlist, vtmp))\n goto err;\n return 1;\n err:\n X509V3err(X509V3_F_X509V3_ADD_VALUE, ERR_R_MALLOC_FAILURE);\n OPENSSL_free(vtmp);\n OPENSSL_free(tname);\n OPENSSL_free(tvalue);\n return 0;\n}', 'int sk_push(_STACK *st, void *data)\n{\n return (sk_insert(st, data, st->num));\n}']
|
1,059 | 0 |
https://github.com/libav/libav/blob/ccc87908a98e7fcbf69fb70ceda7efa5c6e545ec/libavformat/movenc.c/#L1991
|
static void mov_create_chapter_track(AVFormatContext *s, int tracknum)
{
MOVMuxContext *mov = s->priv_data;
MOVTrack *track = &mov->tracks[tracknum];
AVPacket pkt = { .stream_index = tracknum, .flags = AV_PKT_FLAG_KEY };
int i, len;
track->mode = mov->mode;
track->tag = MKTAG('t','e','x','t');
track->timescale = MOV_TIMESCALE;
track->enc = avcodec_alloc_context();
track->enc->codec_type = AVMEDIA_TYPE_SUBTITLE;
for (i = 0; i < s->nb_chapters; i++) {
AVChapter *c = s->chapters[i];
AVMetadataTag *t;
int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,MOV_TIMESCALE});
pkt.pts = pkt.dts = av_rescale_q(c->start, c->time_base, (AVRational){1,MOV_TIMESCALE});
pkt.duration = end - pkt.dts;
if ((t = av_metadata_get(c->metadata, "title", NULL, 0))) {
len = strlen(t->value);
pkt.size = len+2;
pkt.data = av_malloc(pkt.size);
AV_WB16(pkt.data, len);
memcpy(pkt.data+2, t->value, len);
ff_mov_write_packet(s, &pkt);
av_freep(&pkt.data);
}
}
}
|
['static void mov_create_chapter_track(AVFormatContext *s, int tracknum)\n{\n MOVMuxContext *mov = s->priv_data;\n MOVTrack *track = &mov->tracks[tracknum];\n AVPacket pkt = { .stream_index = tracknum, .flags = AV_PKT_FLAG_KEY };\n int i, len;\n track->mode = mov->mode;\n track->tag = MKTAG(\'t\',\'e\',\'x\',\'t\');\n track->timescale = MOV_TIMESCALE;\n track->enc = avcodec_alloc_context();\n track->enc->codec_type = AVMEDIA_TYPE_SUBTITLE;\n for (i = 0; i < s->nb_chapters; i++) {\n AVChapter *c = s->chapters[i];\n AVMetadataTag *t;\n int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,MOV_TIMESCALE});\n pkt.pts = pkt.dts = av_rescale_q(c->start, c->time_base, (AVRational){1,MOV_TIMESCALE});\n pkt.duration = end - pkt.dts;\n if ((t = av_metadata_get(c->metadata, "title", NULL, 0))) {\n len = strlen(t->value);\n pkt.size = len+2;\n pkt.data = av_malloc(pkt.size);\n AV_WB16(pkt.data, len);\n memcpy(pkt.data+2, t->value, len);\n ff_mov_write_packet(s, &pkt);\n av_freep(&pkt.data);\n }\n }\n}', 'AVCodecContext *avcodec_alloc_context(void){\n return avcodec_alloc_context2(AVMEDIA_TYPE_UNKNOWN);\n}', 'AVCodecContext *avcodec_alloc_context2(enum AVMediaType codec_type){\n AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));\n if(avctx==NULL) return NULL;\n avcodec_get_context_defaults2(avctx, codec_type);\n return avctx;\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}', 'int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq){\n int64_t b= bq.num * (int64_t)cq.den;\n int64_t c= cq.num * (int64_t)bq.den;\n return av_rescale_rnd(a, b, c, AV_ROUND_NEAR_INF);\n}', 'static av_always_inline av_const uint16_t bswap_16(uint16_t x)\n{\n x= (x>>8) | (x<<8);\n return x;\n}']
|
1,060 | 0 |
https://github.com/openssl/openssl/blob/0bde1089f895718db2fe2637fda4a0c2ed6df904/crypto/rsa/rsa_sign.c/#L217
|
int RSA_verify(int dtype, unsigned char *m, unsigned int m_len,
unsigned char *sigbuf, unsigned int siglen, RSA *rsa)
{
int i,ret=0,sigtype;
unsigned char *p,*s;
X509_SIG *sig=NULL;
if (siglen != (unsigned int)RSA_size(rsa))
{
RSAerr(RSA_F_RSA_VERIFY,RSA_R_WRONG_SIGNATURE_LENGTH);
return(0);
}
if(rsa->flags & RSA_FLAG_SIGN_VER)
return rsa->meth->rsa_verify(dtype, m, m_len, sigbuf, siglen, rsa);
s=(unsigned char *)Malloc((unsigned int)siglen);
if (s == NULL)
{
RSAerr(RSA_F_RSA_VERIFY,ERR_R_MALLOC_FAILURE);
goto err;
}
if((dtype == NID_md5_sha1) && (m_len != SSL_SIG_LENGTH) ) {
RSAerr(RSA_F_RSA_VERIFY,RSA_R_INVALID_MESSAGE_LENGTH);
return(0);
}
i=RSA_public_decrypt((int)siglen,sigbuf,s,rsa,RSA_PKCS1_PADDING);
if (i <= 0) goto err;
if(dtype == NID_md5_sha1) {
if((i != SSL_SIG_LENGTH) || memcmp(s, m, SSL_SIG_LENGTH))
RSAerr(RSA_F_RSA_VERIFY,RSA_R_BAD_SIGNATURE);
else ret = 1;
} else {
p=s;
sig=d2i_X509_SIG(NULL,&p,(long)i);
if (sig == NULL) goto err;
sigtype=OBJ_obj2nid(sig->algor->algorithm);
#ifdef RSA_DEBUG
fprintf(stderr,"in(%s) expect(%s)\n",OBJ_nid2ln(sigtype),
OBJ_nid2ln(dtype));
#endif
if (sigtype != dtype)
{
if (((dtype == NID_md5) &&
(sigtype == NID_md5WithRSAEncryption)) ||
((dtype == NID_md2) &&
(sigtype == NID_md2WithRSAEncryption)))
{
#if !defined(NO_STDIO) && !defined(WIN16)
fprintf(stderr,"signature has problems, re-make with post SSLeay045\n");
#endif
}
else
{
RSAerr(RSA_F_RSA_VERIFY,
RSA_R_ALGORITHM_MISMATCH);
goto err;
}
}
if ( ((unsigned int)sig->digest->length != m_len) ||
(memcmp(m,sig->digest->data,m_len) != 0))
{
RSAerr(RSA_F_RSA_VERIFY,RSA_R_BAD_SIGNATURE);
}
else
ret=1;
}
err:
if (sig != NULL) X509_SIG_free(sig);
memset(s,0,(unsigned int)siglen);
Free(s);
return(ret);
}
|
['int RSA_verify(int dtype, unsigned char *m, unsigned int m_len,\n\t unsigned char *sigbuf, unsigned int siglen, RSA *rsa)\n\t{\n\tint i,ret=0,sigtype;\n\tunsigned char *p,*s;\n\tX509_SIG *sig=NULL;\n\tif (siglen != (unsigned int)RSA_size(rsa))\n\t\t{\n\t\tRSAerr(RSA_F_RSA_VERIFY,RSA_R_WRONG_SIGNATURE_LENGTH);\n\t\treturn(0);\n\t\t}\n\tif(rsa->flags & RSA_FLAG_SIGN_VER)\n\t return rsa->meth->rsa_verify(dtype, m, m_len, sigbuf, siglen, rsa);\n\ts=(unsigned char *)Malloc((unsigned int)siglen);\n\tif (s == NULL)\n\t\t{\n\t\tRSAerr(RSA_F_RSA_VERIFY,ERR_R_MALLOC_FAILURE);\n\t\tgoto err;\n\t\t}\n\tif((dtype == NID_md5_sha1) && (m_len != SSL_SIG_LENGTH) ) {\n\t\t\tRSAerr(RSA_F_RSA_VERIFY,RSA_R_INVALID_MESSAGE_LENGTH);\n\t\t\treturn(0);\n\t}\n\ti=RSA_public_decrypt((int)siglen,sigbuf,s,rsa,RSA_PKCS1_PADDING);\n\tif (i <= 0) goto err;\n\tif(dtype == NID_md5_sha1) {\n\t\tif((i != SSL_SIG_LENGTH) || memcmp(s, m, SSL_SIG_LENGTH))\n\t\t\t\tRSAerr(RSA_F_RSA_VERIFY,RSA_R_BAD_SIGNATURE);\n\t\telse ret = 1;\n\t} else {\n\t\tp=s;\n\t\tsig=d2i_X509_SIG(NULL,&p,(long)i);\n\t\tif (sig == NULL) goto err;\n\t\tsigtype=OBJ_obj2nid(sig->algor->algorithm);\n\t#ifdef RSA_DEBUG\n\t\tfprintf(stderr,"in(%s) expect(%s)\\n",OBJ_nid2ln(sigtype),\n\t\t\tOBJ_nid2ln(dtype));\n\t#endif\n\t\tif (sigtype != dtype)\n\t\t\t{\n\t\t\tif (((dtype == NID_md5) &&\n\t\t\t\t(sigtype == NID_md5WithRSAEncryption)) ||\n\t\t\t\t((dtype == NID_md2) &&\n\t\t\t\t(sigtype == NID_md2WithRSAEncryption)))\n\t\t\t\t{\n\t#if !defined(NO_STDIO) && !defined(WIN16)\n\t\t\t\tfprintf(stderr,"signature has problems, re-make with post SSLeay045\\n");\n\t#endif\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tRSAerr(RSA_F_RSA_VERIFY,\n\t\t\t\t\t\tRSA_R_ALGORITHM_MISMATCH);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\t}\n\t\tif (\t((unsigned int)sig->digest->length != m_len) ||\n\t\t\t(memcmp(m,sig->digest->data,m_len) != 0))\n\t\t\t{\n\t\t\tRSAerr(RSA_F_RSA_VERIFY,RSA_R_BAD_SIGNATURE);\n\t\t\t}\n\t\telse\n\t\t\tret=1;\n\t}\nerr:\n\tif (sig != NULL) X509_SIG_free(sig);\n\tmemset(s,0,(unsigned int)siglen);\n\tFree(s);\n\treturn(ret);\n\t}', 'int RSA_size(RSA *r)\n\t{\n\treturn(BN_num_bytes(r->n));\n\t}', 'int BN_num_bits(const BIGNUM *a)\n\t{\n\tBN_ULONG l;\n\tint i;\n\tbn_check_top(a);\n\tif (a->top == 0) return(0);\n\tl=a->d[a->top-1];\n\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}', 'void *CRYPTO_malloc(int num, const char *file, int line)\n\t{\n\tchar *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}', 'void CRYPTO_free(void *str)\n\t{\n\tif (free_debug_func != NULL)\n\t\tfree_debug_func(str, 0);\n#ifdef LEVITTE_DEBUG\n\tfprintf(stderr, "LEVITTE_DEBUG: < 0x%p\\n", str);\n#endif\n\tfree_func(str);\n\tif (free_debug_func != NULL)\n\t\tfree_debug_func(NULL, 1);\n\t}']
|
1,061 | 0 |
https://github.com/openssl/openssl/blob/ec04e866343d40a1e3e8e5db79557e279a2dd0d8/apps/ts.c/#L815
|
static ASN1_INTEGER *next_serial(const char *serialfile)
{
int ret = 0;
BIO *in = NULL;
ASN1_INTEGER *serial = NULL;
BIGNUM *bn = NULL;
if ((serial = ASN1_INTEGER_new()) == NULL)
goto err;
if ((in = BIO_new_file(serialfile, "r")) == NULL) {
ERR_clear_error();
BIO_printf(bio_err, "Warning: could not open file %s for "
"reading, using serial number: 1\n", serialfile);
if (!ASN1_INTEGER_set(serial, 1))
goto err;
} else {
char buf[1024];
if (!a2i_ASN1_INTEGER(in, serial, buf, sizeof(buf))) {
BIO_printf(bio_err, "unable to load number from %s\n",
serialfile);
goto err;
}
if ((bn = ASN1_INTEGER_to_BN(serial, NULL)) == NULL)
goto err;
ASN1_INTEGER_free(serial);
serial = NULL;
if (!BN_add_word(bn, 1))
goto err;
if ((serial = BN_to_ASN1_INTEGER(bn, NULL)) == NULL)
goto err;
}
ret = 1;
err:
if (!ret) {
ASN1_INTEGER_free(serial);
serial = NULL;
}
BIO_free_all(in);
BN_free(bn);
return serial;
}
|
['static ASN1_INTEGER *next_serial(const char *serialfile)\n{\n int ret = 0;\n BIO *in = NULL;\n ASN1_INTEGER *serial = NULL;\n BIGNUM *bn = NULL;\n if ((serial = ASN1_INTEGER_new()) == NULL)\n goto err;\n if ((in = BIO_new_file(serialfile, "r")) == NULL) {\n ERR_clear_error();\n BIO_printf(bio_err, "Warning: could not open file %s for "\n "reading, using serial number: 1\\n", serialfile);\n if (!ASN1_INTEGER_set(serial, 1))\n goto err;\n } else {\n char buf[1024];\n if (!a2i_ASN1_INTEGER(in, serial, buf, sizeof(buf))) {\n BIO_printf(bio_err, "unable to load number from %s\\n",\n serialfile);\n goto err;\n }\n if ((bn = ASN1_INTEGER_to_BN(serial, NULL)) == NULL)\n goto err;\n ASN1_INTEGER_free(serial);\n serial = NULL;\n if (!BN_add_word(bn, 1))\n goto err;\n if ((serial = BN_to_ASN1_INTEGER(bn, NULL)) == NULL)\n goto err;\n }\n ret = 1;\n err:\n if (!ret) {\n ASN1_INTEGER_free(serial);\n serial = NULL;\n }\n BIO_free_all(in);\n BN_free(bn);\n return serial;\n}', 'IMPLEMENT_ASN1_STRING_FUNCTIONS(ASN1_INTEGER)', 'ASN1_STRING *ASN1_STRING_type_new(int type)\n{\n ASN1_STRING *ret;\n ret = OPENSSL_zalloc(sizeof(*ret));\n if (ret == NULL) {\n ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n ret->type = type;\n return (ret);\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (num <= 0)\n return NULL;\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)file;\n (void)line;\n ret = malloc(num);\n#endif\n#ifndef OPENSSL_CPUID_OBJ\n if (ret && (num > 2048)) {\n extern unsigned char cleanse_ctr;\n ((unsigned char *)ret)[0] = cleanse_ctr;\n }\n#endif\n return ret;\n}', 'void ASN1_STRING_free(ASN1_STRING *a)\n{\n if (a == NULL)\n return;\n if (!(a->flags & ASN1_STRING_FLAG_NDEF))\n OPENSSL_free(a->data);\n if (!(a->flags & ASN1_STRING_FLAG_EMBED))\n OPENSSL_free(a);\n}']
|
1,062 | 0 |
https://github.com/libav/libav/blob/d9cf5f516974c64e01846ca685301014b38cf224/libavcodec/mss2.c/#L486
|
static int mss2_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
AVPacket *avpkt)
{
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size;
MSS2Context *ctx = avctx->priv_data;
MSS12Context *c = &ctx->c;
GetBitContext gb;
GetByteContext gB;
ArithCoder acoder;
int keyframe, has_wmv9, has_mv, is_rle, is_555, ret;
Rectangle wmv9rects[MAX_WMV9_RECTANGLES], *r;
int used_rects = 0, i, implicit_rect = 0, av_uninit(wmv9_mask);
av_assert0(FF_INPUT_BUFFER_PADDING_SIZE >=
ARITH2_PADDING + (MIN_CACHE_BITS + 7) / 8);
init_get_bits(&gb, buf, buf_size * 8);
if (keyframe = get_bits1(&gb))
skip_bits(&gb, 7);
has_wmv9 = get_bits1(&gb);
has_mv = keyframe ? 0 : get_bits1(&gb);
is_rle = get_bits1(&gb);
is_555 = is_rle && get_bits1(&gb);
if (c->slice_split > 0)
ctx->split_position = c->slice_split;
else if (c->slice_split < 0) {
if (get_bits1(&gb)) {
if (get_bits1(&gb)) {
if (get_bits1(&gb))
ctx->split_position = get_bits(&gb, 16);
else
ctx->split_position = get_bits(&gb, 12);
} else
ctx->split_position = get_bits(&gb, 8) << 4;
} else {
if (keyframe)
ctx->split_position = avctx->height / 2;
}
} else
ctx->split_position = avctx->height;
if (c->slice_split && (ctx->split_position < 1 - is_555 ||
ctx->split_position > avctx->height - 1))
return AVERROR_INVALIDDATA;
align_get_bits(&gb);
buf += get_bits_count(&gb) >> 3;
buf_size -= get_bits_count(&gb) >> 3;
if (buf_size < 1)
return AVERROR_INVALIDDATA;
if (is_555 && (has_wmv9 || has_mv || c->slice_split && ctx->split_position))
return AVERROR_INVALIDDATA;
avctx->pix_fmt = is_555 ? AV_PIX_FMT_RGB555 : AV_PIX_FMT_RGB24;
if (ctx->pic.data[0] && ctx->pic.format != avctx->pix_fmt)
avctx->release_buffer(avctx, &ctx->pic);
if (has_wmv9) {
bytestream2_init(&gB, buf, buf_size + ARITH2_PADDING);
arith2_init(&acoder, &gB);
implicit_rect = !arith2_get_bit(&acoder);
while (arith2_get_bit(&acoder)) {
if (used_rects == MAX_WMV9_RECTANGLES)
return AVERROR_INVALIDDATA;
r = &wmv9rects[used_rects];
if (!used_rects)
r->x = arith2_get_number(&acoder, avctx->width);
else
r->x = arith2_get_number(&acoder, avctx->width -
wmv9rects[used_rects - 1].x) +
wmv9rects[used_rects - 1].x;
r->y = arith2_get_number(&acoder, avctx->height);
r->w = arith2_get_number(&acoder, avctx->width - r->x) + 1;
r->h = arith2_get_number(&acoder, avctx->height - r->y) + 1;
used_rects++;
}
if (implicit_rect && used_rects) {
av_log(avctx, AV_LOG_ERROR, "implicit_rect && used_rects > 0\n");
return AVERROR_INVALIDDATA;
}
if (implicit_rect) {
wmv9rects[0].x = 0;
wmv9rects[0].y = 0;
wmv9rects[0].w = avctx->width;
wmv9rects[0].h = avctx->height;
used_rects = 1;
}
for (i = 0; i < used_rects; i++) {
if (!implicit_rect && arith2_get_bit(&acoder)) {
av_log(avctx, AV_LOG_ERROR, "Unexpected grandchildren\n");
return AVERROR_INVALIDDATA;
}
if (!i) {
wmv9_mask = arith2_get_bit(&acoder) - 1;
if (!wmv9_mask)
wmv9_mask = arith2_get_number(&acoder, 256);
}
wmv9rects[i].coded = arith2_get_number(&acoder, 2);
}
buf += arith2_get_consumed_bytes(&acoder);
buf_size -= arith2_get_consumed_bytes(&acoder);
if (buf_size < 1)
return AVERROR_INVALIDDATA;
}
c->mvX = c->mvY = 0;
if (keyframe && !is_555) {
if ((i = decode_pal_v2(c, buf, buf_size)) < 0)
return AVERROR_INVALIDDATA;
buf += i;
buf_size -= i;
} else if (has_mv) {
buf += 4;
buf_size -= 4;
if (buf_size < 1)
return AVERROR_INVALIDDATA;
c->mvX = AV_RB16(buf - 4) - avctx->width;
c->mvY = AV_RB16(buf - 2) - avctx->height;
}
if (c->mvX < 0 || c->mvY < 0) {
FFSWAP(AVFrame, ctx->pic, ctx->last_pic);
FFSWAP(uint8_t *, c->pal_pic, c->last_pal_pic);
if (ctx->pic.data[0])
avctx->release_buffer(avctx, &ctx->pic);
ctx->pic.reference = 3;
ctx->pic.buffer_hints = FF_BUFFER_HINTS_VALID |
FF_BUFFER_HINTS_READABLE |
FF_BUFFER_HINTS_PRESERVE |
FF_BUFFER_HINTS_REUSABLE;
if ((ret = ff_get_buffer(avctx, &ctx->pic)) < 0) {
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
return ret;
}
if (ctx->last_pic.data[0]) {
av_assert0(ctx->pic.linesize[0] == ctx->last_pic.linesize[0]);
c->last_rgb_pic = ctx->last_pic.data[0] +
ctx->last_pic.linesize[0] * (avctx->height - 1);
} else {
av_log(avctx, AV_LOG_ERROR, "Missing keyframe\n");
return AVERROR_INVALIDDATA;
}
} else {
if (ctx->last_pic.data[0])
avctx->release_buffer(avctx, &ctx->last_pic);
ctx->pic.reference = 3;
ctx->pic.buffer_hints = FF_BUFFER_HINTS_VALID |
FF_BUFFER_HINTS_READABLE |
FF_BUFFER_HINTS_PRESERVE |
FF_BUFFER_HINTS_REUSABLE;
if ((ret = avctx->reget_buffer(avctx, &ctx->pic)) < 0) {
av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
return ret;
}
c->last_rgb_pic = NULL;
}
c->rgb_pic = ctx->pic.data[0] +
ctx->pic.linesize[0] * (avctx->height - 1);
c->rgb_stride = -ctx->pic.linesize[0];
ctx->pic.key_frame = keyframe;
ctx->pic.pict_type = keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
if (is_555) {
bytestream2_init(&gB, buf, buf_size);
if (decode_555(&gB, (uint16_t *)c->rgb_pic, c->rgb_stride >> 1,
keyframe, avctx->width, avctx->height))
return AVERROR_INVALIDDATA;
buf_size -= bytestream2_tell(&gB);
} else {
if (keyframe) {
c->corrupted = 0;
ff_mss12_slicecontext_reset(&ctx->sc[0]);
if (c->slice_split)
ff_mss12_slicecontext_reset(&ctx->sc[1]);
}
if (is_rle) {
init_get_bits(&gb, buf, buf_size * 8);
if (ret = decode_rle(&gb, c->pal_pic, c->pal_stride,
c->rgb_pic, c->rgb_stride, c->pal, keyframe,
ctx->split_position, 0,
avctx->width, avctx->height))
return ret;
align_get_bits(&gb);
if (c->slice_split)
if (ret = decode_rle(&gb, c->pal_pic, c->pal_stride,
c->rgb_pic, c->rgb_stride, c->pal, keyframe,
ctx->split_position, 1,
avctx->width, avctx->height))
return ret;
align_get_bits(&gb);
buf += get_bits_count(&gb) >> 3;
buf_size -= get_bits_count(&gb) >> 3;
} else if (!implicit_rect || wmv9_mask != -1) {
if (c->corrupted)
return AVERROR_INVALIDDATA;
bytestream2_init(&gB, buf, buf_size + ARITH2_PADDING);
arith2_init(&acoder, &gB);
c->keyframe = keyframe;
if (c->corrupted = ff_mss12_decode_rect(&ctx->sc[0], &acoder, 0, 0,
avctx->width,
ctx->split_position))
return AVERROR_INVALIDDATA;
buf += arith2_get_consumed_bytes(&acoder);
buf_size -= arith2_get_consumed_bytes(&acoder);
if (c->slice_split) {
if (buf_size < 1)
return AVERROR_INVALIDDATA;
bytestream2_init(&gB, buf, buf_size + ARITH2_PADDING);
arith2_init(&acoder, &gB);
if (c->corrupted = ff_mss12_decode_rect(&ctx->sc[1], &acoder, 0,
ctx->split_position,
avctx->width,
avctx->height - ctx->split_position))
return AVERROR_INVALIDDATA;
buf += arith2_get_consumed_bytes(&acoder);
buf_size -= arith2_get_consumed_bytes(&acoder);
}
} else
memset(c->pal_pic, 0, c->pal_stride * avctx->height);
}
if (has_wmv9) {
for (i = 0; i < used_rects; i++) {
int x = wmv9rects[i].x;
int y = wmv9rects[i].y;
int w = wmv9rects[i].w;
int h = wmv9rects[i].h;
if (wmv9rects[i].coded) {
int WMV9codedFrameSize;
if (buf_size < 4 || !(WMV9codedFrameSize = AV_RL24(buf)))
return AVERROR_INVALIDDATA;
if (ret = decode_wmv9(avctx, buf + 3, buf_size - 3,
x, y, w, h, wmv9_mask))
return ret;
buf += WMV9codedFrameSize + 3;
buf_size -= WMV9codedFrameSize + 3;
} else {
uint8_t *dst = c->rgb_pic + y * c->rgb_stride + x * 3;
if (wmv9_mask != -1) {
ctx->dsp.mss2_gray_fill_masked(dst, c->rgb_stride,
wmv9_mask,
c->pal_pic + y * c->pal_stride + x,
c->pal_stride,
w, h);
} else {
do {
memset(dst, 0x80, w * 3);
dst += c->rgb_stride;
} while (--h);
}
}
}
}
if (buf_size)
av_log(avctx, AV_LOG_WARNING, "buffer not fully consumed\n");
*got_frame = 1;
*(AVFrame *)data = ctx->pic;
return avpkt->size;
}
|
['static int mss2_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,\n AVPacket *avpkt)\n{\n const uint8_t *buf = avpkt->data;\n int buf_size = avpkt->size;\n MSS2Context *ctx = avctx->priv_data;\n MSS12Context *c = &ctx->c;\n GetBitContext gb;\n GetByteContext gB;\n ArithCoder acoder;\n int keyframe, has_wmv9, has_mv, is_rle, is_555, ret;\n Rectangle wmv9rects[MAX_WMV9_RECTANGLES], *r;\n int used_rects = 0, i, implicit_rect = 0, av_uninit(wmv9_mask);\n av_assert0(FF_INPUT_BUFFER_PADDING_SIZE >=\n ARITH2_PADDING + (MIN_CACHE_BITS + 7) / 8);\n init_get_bits(&gb, buf, buf_size * 8);\n if (keyframe = get_bits1(&gb))\n skip_bits(&gb, 7);\n has_wmv9 = get_bits1(&gb);\n has_mv = keyframe ? 0 : get_bits1(&gb);\n is_rle = get_bits1(&gb);\n is_555 = is_rle && get_bits1(&gb);\n if (c->slice_split > 0)\n ctx->split_position = c->slice_split;\n else if (c->slice_split < 0) {\n if (get_bits1(&gb)) {\n if (get_bits1(&gb)) {\n if (get_bits1(&gb))\n ctx->split_position = get_bits(&gb, 16);\n else\n ctx->split_position = get_bits(&gb, 12);\n } else\n ctx->split_position = get_bits(&gb, 8) << 4;\n } else {\n if (keyframe)\n ctx->split_position = avctx->height / 2;\n }\n } else\n ctx->split_position = avctx->height;\n if (c->slice_split && (ctx->split_position < 1 - is_555 ||\n ctx->split_position > avctx->height - 1))\n return AVERROR_INVALIDDATA;\n align_get_bits(&gb);\n buf += get_bits_count(&gb) >> 3;\n buf_size -= get_bits_count(&gb) >> 3;\n if (buf_size < 1)\n return AVERROR_INVALIDDATA;\n if (is_555 && (has_wmv9 || has_mv || c->slice_split && ctx->split_position))\n return AVERROR_INVALIDDATA;\n avctx->pix_fmt = is_555 ? AV_PIX_FMT_RGB555 : AV_PIX_FMT_RGB24;\n if (ctx->pic.data[0] && ctx->pic.format != avctx->pix_fmt)\n avctx->release_buffer(avctx, &ctx->pic);\n if (has_wmv9) {\n bytestream2_init(&gB, buf, buf_size + ARITH2_PADDING);\n arith2_init(&acoder, &gB);\n implicit_rect = !arith2_get_bit(&acoder);\n while (arith2_get_bit(&acoder)) {\n if (used_rects == MAX_WMV9_RECTANGLES)\n return AVERROR_INVALIDDATA;\n r = &wmv9rects[used_rects];\n if (!used_rects)\n r->x = arith2_get_number(&acoder, avctx->width);\n else\n r->x = arith2_get_number(&acoder, avctx->width -\n wmv9rects[used_rects - 1].x) +\n wmv9rects[used_rects - 1].x;\n r->y = arith2_get_number(&acoder, avctx->height);\n r->w = arith2_get_number(&acoder, avctx->width - r->x) + 1;\n r->h = arith2_get_number(&acoder, avctx->height - r->y) + 1;\n used_rects++;\n }\n if (implicit_rect && used_rects) {\n av_log(avctx, AV_LOG_ERROR, "implicit_rect && used_rects > 0\\n");\n return AVERROR_INVALIDDATA;\n }\n if (implicit_rect) {\n wmv9rects[0].x = 0;\n wmv9rects[0].y = 0;\n wmv9rects[0].w = avctx->width;\n wmv9rects[0].h = avctx->height;\n used_rects = 1;\n }\n for (i = 0; i < used_rects; i++) {\n if (!implicit_rect && arith2_get_bit(&acoder)) {\n av_log(avctx, AV_LOG_ERROR, "Unexpected grandchildren\\n");\n return AVERROR_INVALIDDATA;\n }\n if (!i) {\n wmv9_mask = arith2_get_bit(&acoder) - 1;\n if (!wmv9_mask)\n wmv9_mask = arith2_get_number(&acoder, 256);\n }\n wmv9rects[i].coded = arith2_get_number(&acoder, 2);\n }\n buf += arith2_get_consumed_bytes(&acoder);\n buf_size -= arith2_get_consumed_bytes(&acoder);\n if (buf_size < 1)\n return AVERROR_INVALIDDATA;\n }\n c->mvX = c->mvY = 0;\n if (keyframe && !is_555) {\n if ((i = decode_pal_v2(c, buf, buf_size)) < 0)\n return AVERROR_INVALIDDATA;\n buf += i;\n buf_size -= i;\n } else if (has_mv) {\n buf += 4;\n buf_size -= 4;\n if (buf_size < 1)\n return AVERROR_INVALIDDATA;\n c->mvX = AV_RB16(buf - 4) - avctx->width;\n c->mvY = AV_RB16(buf - 2) - avctx->height;\n }\n if (c->mvX < 0 || c->mvY < 0) {\n FFSWAP(AVFrame, ctx->pic, ctx->last_pic);\n FFSWAP(uint8_t *, c->pal_pic, c->last_pal_pic);\n if (ctx->pic.data[0])\n avctx->release_buffer(avctx, &ctx->pic);\n ctx->pic.reference = 3;\n ctx->pic.buffer_hints = FF_BUFFER_HINTS_VALID |\n FF_BUFFER_HINTS_READABLE |\n FF_BUFFER_HINTS_PRESERVE |\n FF_BUFFER_HINTS_REUSABLE;\n if ((ret = ff_get_buffer(avctx, &ctx->pic)) < 0) {\n av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n return ret;\n }\n if (ctx->last_pic.data[0]) {\n av_assert0(ctx->pic.linesize[0] == ctx->last_pic.linesize[0]);\n c->last_rgb_pic = ctx->last_pic.data[0] +\n ctx->last_pic.linesize[0] * (avctx->height - 1);\n } else {\n av_log(avctx, AV_LOG_ERROR, "Missing keyframe\\n");\n return AVERROR_INVALIDDATA;\n }\n } else {\n if (ctx->last_pic.data[0])\n avctx->release_buffer(avctx, &ctx->last_pic);\n ctx->pic.reference = 3;\n ctx->pic.buffer_hints = FF_BUFFER_HINTS_VALID |\n FF_BUFFER_HINTS_READABLE |\n FF_BUFFER_HINTS_PRESERVE |\n FF_BUFFER_HINTS_REUSABLE;\n if ((ret = avctx->reget_buffer(avctx, &ctx->pic)) < 0) {\n av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\\n");\n return ret;\n }\n c->last_rgb_pic = NULL;\n }\n c->rgb_pic = ctx->pic.data[0] +\n ctx->pic.linesize[0] * (avctx->height - 1);\n c->rgb_stride = -ctx->pic.linesize[0];\n ctx->pic.key_frame = keyframe;\n ctx->pic.pict_type = keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;\n if (is_555) {\n bytestream2_init(&gB, buf, buf_size);\n if (decode_555(&gB, (uint16_t *)c->rgb_pic, c->rgb_stride >> 1,\n keyframe, avctx->width, avctx->height))\n return AVERROR_INVALIDDATA;\n buf_size -= bytestream2_tell(&gB);\n } else {\n if (keyframe) {\n c->corrupted = 0;\n ff_mss12_slicecontext_reset(&ctx->sc[0]);\n if (c->slice_split)\n ff_mss12_slicecontext_reset(&ctx->sc[1]);\n }\n if (is_rle) {\n init_get_bits(&gb, buf, buf_size * 8);\n if (ret = decode_rle(&gb, c->pal_pic, c->pal_stride,\n c->rgb_pic, c->rgb_stride, c->pal, keyframe,\n ctx->split_position, 0,\n avctx->width, avctx->height))\n return ret;\n align_get_bits(&gb);\n if (c->slice_split)\n if (ret = decode_rle(&gb, c->pal_pic, c->pal_stride,\n c->rgb_pic, c->rgb_stride, c->pal, keyframe,\n ctx->split_position, 1,\n avctx->width, avctx->height))\n return ret;\n align_get_bits(&gb);\n buf += get_bits_count(&gb) >> 3;\n buf_size -= get_bits_count(&gb) >> 3;\n } else if (!implicit_rect || wmv9_mask != -1) {\n if (c->corrupted)\n return AVERROR_INVALIDDATA;\n bytestream2_init(&gB, buf, buf_size + ARITH2_PADDING);\n arith2_init(&acoder, &gB);\n c->keyframe = keyframe;\n if (c->corrupted = ff_mss12_decode_rect(&ctx->sc[0], &acoder, 0, 0,\n avctx->width,\n ctx->split_position))\n return AVERROR_INVALIDDATA;\n buf += arith2_get_consumed_bytes(&acoder);\n buf_size -= arith2_get_consumed_bytes(&acoder);\n if (c->slice_split) {\n if (buf_size < 1)\n return AVERROR_INVALIDDATA;\n bytestream2_init(&gB, buf, buf_size + ARITH2_PADDING);\n arith2_init(&acoder, &gB);\n if (c->corrupted = ff_mss12_decode_rect(&ctx->sc[1], &acoder, 0,\n ctx->split_position,\n avctx->width,\n avctx->height - ctx->split_position))\n return AVERROR_INVALIDDATA;\n buf += arith2_get_consumed_bytes(&acoder);\n buf_size -= arith2_get_consumed_bytes(&acoder);\n }\n } else\n memset(c->pal_pic, 0, c->pal_stride * avctx->height);\n }\n if (has_wmv9) {\n for (i = 0; i < used_rects; i++) {\n int x = wmv9rects[i].x;\n int y = wmv9rects[i].y;\n int w = wmv9rects[i].w;\n int h = wmv9rects[i].h;\n if (wmv9rects[i].coded) {\n int WMV9codedFrameSize;\n if (buf_size < 4 || !(WMV9codedFrameSize = AV_RL24(buf)))\n return AVERROR_INVALIDDATA;\n if (ret = decode_wmv9(avctx, buf + 3, buf_size - 3,\n x, y, w, h, wmv9_mask))\n return ret;\n buf += WMV9codedFrameSize + 3;\n buf_size -= WMV9codedFrameSize + 3;\n } else {\n uint8_t *dst = c->rgb_pic + y * c->rgb_stride + x * 3;\n if (wmv9_mask != -1) {\n ctx->dsp.mss2_gray_fill_masked(dst, c->rgb_stride,\n wmv9_mask,\n c->pal_pic + y * c->pal_stride + x,\n c->pal_stride,\n w, h);\n } else {\n do {\n memset(dst, 0x80, w * 3);\n dst += c->rgb_stride;\n } while (--h);\n }\n }\n }\n }\n if (buf_size)\n av_log(avctx, AV_LOG_WARNING, "buffer not fully consumed\\n");\n *got_frame = 1;\n *(AVFrame *)data = ctx->pic;\n return avpkt->size;\n}', 'static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,\n int bit_size)\n{\n int buffer_size;\n int ret = 0;\n if (bit_size > INT_MAX - 7 || bit_size <= 0) {\n buffer_size = bit_size = 0;\n buffer = NULL;\n ret = AVERROR_INVALIDDATA;\n }\n buffer_size = (bit_size + 7) >> 3;\n s->buffer = buffer;\n s->size_in_bits = bit_size;\n#if !UNCHECKED_BITSTREAM_READER\n s->size_in_bits_plus8 = bit_size + 8;\n#endif\n s->buffer_end = buffer + buffer_size;\n s->index = 0;\n return ret;\n}', 'static inline unsigned int get_bits1(GetBitContext *s)\n{\n unsigned int index = s->index;\n uint8_t result = s->buffer[index>>3];\n#ifdef BITSTREAM_READER_LE\n result >>= index & 7;\n result &= 1;\n#else\n result <<= index & 7;\n result >>= 8 - 1;\n#endif\n#if !UNCHECKED_BITSTREAM_READER\n if (s->index < s->size_in_bits_plus8)\n#endif\n index++;\n s->index = index;\n return result;\n}']
|
1,063 | 0 |
https://github.com/libav/libav/blob/7ed63ca2e7817e837facd29b01d25a1a69087916/libavcodec/h264_cavlc.c/#L767
|
int ff_h264_decode_mb_cavlc(H264Context *h){
MpegEncContext * const s = &h->s;
int mb_xy;
int partition_count;
unsigned int mb_type, cbp;
int dct8x8_allowed= h->pps.transform_8x8_mode;
mb_xy = h->mb_xy = s->mb_x + s->mb_y*s->mb_stride;
tprintf(s->avctx, "pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y);
cbp = 0;
if(h->slice_type_nos != FF_I_TYPE){
if(s->mb_skip_run==-1)
s->mb_skip_run= get_ue_golomb(&s->gb);
if (s->mb_skip_run--) {
if(FRAME_MBAFF && (s->mb_y&1) == 0){
if(s->mb_skip_run==0)
h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
}
decode_mb_skip(h);
return 0;
}
}
if(FRAME_MBAFF){
if( (s->mb_y&1) == 0 )
h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);
}
h->prev_mb_skipped= 0;
mb_type= get_ue_golomb(&s->gb);
if(h->slice_type_nos == FF_B_TYPE){
if(mb_type < 23){
partition_count= b_mb_type_info[mb_type].partition_count;
mb_type= b_mb_type_info[mb_type].type;
}else{
mb_type -= 23;
goto decode_intra_mb;
}
}else if(h->slice_type_nos == FF_P_TYPE){
if(mb_type < 5){
partition_count= p_mb_type_info[mb_type].partition_count;
mb_type= p_mb_type_info[mb_type].type;
}else{
mb_type -= 5;
goto decode_intra_mb;
}
}else{
assert(h->slice_type_nos == FF_I_TYPE);
if(h->slice_type == FF_SI_TYPE && mb_type)
mb_type--;
decode_intra_mb:
if(mb_type > 25){
av_log(h->s.avctx, AV_LOG_ERROR, "mb_type %d in %c slice too large at %d %d\n", mb_type, av_get_pict_type_char(h->slice_type), s->mb_x, s->mb_y);
return -1;
}
partition_count=0;
cbp= i_mb_type_info[mb_type].cbp;
h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
mb_type= i_mb_type_info[mb_type].type;
}
if(MB_FIELD)
mb_type |= MB_TYPE_INTERLACED;
h->slice_table[ mb_xy ]= h->slice_num;
if(IS_INTRA_PCM(mb_type)){
unsigned int x;
align_get_bits(&s->gb);
for(x=0; x < (CHROMA ? 384 : 256); x++){
((uint8_t*)h->mb)[x]= get_bits(&s->gb, 8);
}
s->current_picture.qscale_table[mb_xy]= 0;
memset(h->non_zero_count[mb_xy], 16, 32);
s->current_picture.mb_type[mb_xy]= mb_type;
return 0;
}
if(MB_MBAFF){
h->ref_count[0] <<= 1;
h->ref_count[1] <<= 1;
}
fill_decode_neighbors(h, mb_type);
fill_decode_caches(h, mb_type);
if(IS_INTRA(mb_type)){
int pred_mode;
if(IS_INTRA4x4(mb_type)){
int i;
int di = 1;
if(dct8x8_allowed && get_bits1(&s->gb)){
mb_type |= MB_TYPE_8x8DCT;
di = 4;
}
for(i=0; i<16; i+=di){
int mode= pred_intra_mode(h, i);
if(!get_bits1(&s->gb)){
const int rem_mode= get_bits(&s->gb, 3);
mode = rem_mode + (rem_mode >= mode);
}
if(di==4)
fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );
else
h->intra4x4_pred_mode_cache[ scan8[i] ] = mode;
}
ff_h264_write_back_intra_pred_mode(h);
if( ff_h264_check_intra4x4_pred_mode(h) < 0)
return -1;
}else{
h->intra16x16_pred_mode= ff_h264_check_intra_pred_mode(h, h->intra16x16_pred_mode);
if(h->intra16x16_pred_mode < 0)
return -1;
}
if(CHROMA){
pred_mode= ff_h264_check_intra_pred_mode(h, get_ue_golomb_31(&s->gb));
if(pred_mode < 0)
return -1;
h->chroma_pred_mode= pred_mode;
}
}else if(partition_count==4){
int i, j, sub_partition_count[4], list, ref[2][4];
if(h->slice_type_nos == FF_B_TYPE){
for(i=0; i<4; i++){
h->sub_mb_type[i]= get_ue_golomb_31(&s->gb);
if(h->sub_mb_type[i] >=13){
av_log(h->s.avctx, AV_LOG_ERROR, "B sub_mb_type %u out of range at %d %d\n", h->sub_mb_type[i], s->mb_x, s->mb_y);
return -1;
}
sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
}
if( IS_DIRECT(h->sub_mb_type[0]|h->sub_mb_type[1]|h->sub_mb_type[2]|h->sub_mb_type[3])) {
ff_h264_pred_direct_motion(h, &mb_type);
h->ref_cache[0][scan8[4]] =
h->ref_cache[1][scan8[4]] =
h->ref_cache[0][scan8[12]] =
h->ref_cache[1][scan8[12]] = PART_NOT_AVAILABLE;
}
}else{
assert(h->slice_type_nos == FF_P_TYPE);
for(i=0; i<4; i++){
h->sub_mb_type[i]= get_ue_golomb_31(&s->gb);
if(h->sub_mb_type[i] >=4){
av_log(h->s.avctx, AV_LOG_ERROR, "P sub_mb_type %u out of range at %d %d\n", h->sub_mb_type[i], s->mb_x, s->mb_y);
return -1;
}
sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;
}
}
for(list=0; list<h->list_count; list++){
int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list];
for(i=0; i<4; i++){
if(IS_DIRECT(h->sub_mb_type[i])) continue;
if(IS_DIR(h->sub_mb_type[i], 0, list)){
unsigned int tmp;
if(ref_count == 1){
tmp= 0;
}else if(ref_count == 2){
tmp= get_bits1(&s->gb)^1;
}else{
tmp= get_ue_golomb_31(&s->gb);
if(tmp>=ref_count){
av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", tmp);
return -1;
}
}
ref[list][i]= tmp;
}else{
ref[list][i] = -1;
}
}
}
if(dct8x8_allowed)
dct8x8_allowed = get_dct8x8_allowed(h);
for(list=0; list<h->list_count; list++){
for(i=0; i<4; i++){
if(IS_DIRECT(h->sub_mb_type[i])) {
h->ref_cache[list][ scan8[4*i] ] = h->ref_cache[list][ scan8[4*i]+1 ];
continue;
}
h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ]=
h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];
if(IS_DIR(h->sub_mb_type[i], 0, list)){
const int sub_mb_type= h->sub_mb_type[i];
const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;
for(j=0; j<sub_partition_count[i]; j++){
int mx, my;
const int index= 4*i + block_width*j;
int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];
pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my);
mx += get_se_golomb(&s->gb);
my += get_se_golomb(&s->gb);
tprintf(s->avctx, "final mv:%d %d\n", mx, my);
if(IS_SUB_8X8(sub_mb_type)){
mv_cache[ 1 ][0]=
mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;
mv_cache[ 1 ][1]=
mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;
}else if(IS_SUB_8X4(sub_mb_type)){
mv_cache[ 1 ][0]= mx;
mv_cache[ 1 ][1]= my;
}else if(IS_SUB_4X8(sub_mb_type)){
mv_cache[ 8 ][0]= mx;
mv_cache[ 8 ][1]= my;
}
mv_cache[ 0 ][0]= mx;
mv_cache[ 0 ][1]= my;
}
}else{
uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];
p[0] = p[1]=
p[8] = p[9]= 0;
}
}
}
}else if(IS_DIRECT(mb_type)){
ff_h264_pred_direct_motion(h, &mb_type);
dct8x8_allowed &= h->sps.direct_8x8_inference_flag;
}else{
int list, mx, my, i;
if(IS_16X16(mb_type)){
for(list=0; list<h->list_count; list++){
unsigned int val;
if(IS_DIR(mb_type, 0, list)){
if(h->ref_count[list]==1){
val= 0;
}else if(h->ref_count[list]==2){
val= get_bits1(&s->gb)^1;
}else{
val= get_ue_golomb_31(&s->gb);
if(val >= h->ref_count[list]){
av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
return -1;
}
}
}else
val= LIST_NOT_USED&0xFF;
fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, val, 1);
}
for(list=0; list<h->list_count; list++){
unsigned int val;
if(IS_DIR(mb_type, 0, list)){
pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my);
mx += get_se_golomb(&s->gb);
my += get_se_golomb(&s->gb);
tprintf(s->avctx, "final mv:%d %d\n", mx, my);
val= pack16to32(mx,my);
}else
val=0;
fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, val, 4);
}
}
else if(IS_16X8(mb_type)){
for(list=0; list<h->list_count; list++){
for(i=0; i<2; i++){
unsigned int val;
if(IS_DIR(mb_type, i, list)){
if(h->ref_count[list] == 1){
val= 0;
}else if(h->ref_count[list] == 2){
val= get_bits1(&s->gb)^1;
}else{
val= get_ue_golomb_31(&s->gb);
if(val >= h->ref_count[list]){
av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
return -1;
}
}
}else
val= LIST_NOT_USED&0xFF;
fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 1);
}
}
for(list=0; list<h->list_count; list++){
for(i=0; i<2; i++){
unsigned int val;
if(IS_DIR(mb_type, i, list)){
pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my);
mx += get_se_golomb(&s->gb);
my += get_se_golomb(&s->gb);
tprintf(s->avctx, "final mv:%d %d\n", mx, my);
val= pack16to32(mx,my);
}else
val=0;
fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 4);
}
}
}else{
assert(IS_8X16(mb_type));
for(list=0; list<h->list_count; list++){
for(i=0; i<2; i++){
unsigned int val;
if(IS_DIR(mb_type, i, list)){
if(h->ref_count[list]==1){
val= 0;
}else if(h->ref_count[list]==2){
val= get_bits1(&s->gb)^1;
}else{
val= get_ue_golomb_31(&s->gb);
if(val >= h->ref_count[list]){
av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\n", val);
return -1;
}
}
}else
val= LIST_NOT_USED&0xFF;
fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 1);
}
}
for(list=0; list<h->list_count; list++){
for(i=0; i<2; i++){
unsigned int val;
if(IS_DIR(mb_type, i, list)){
pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);
mx += get_se_golomb(&s->gb);
my += get_se_golomb(&s->gb);
tprintf(s->avctx, "final mv:%d %d\n", mx, my);
val= pack16to32(mx,my);
}else
val=0;
fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 4);
}
}
}
}
if(IS_INTER(mb_type))
write_back_motion(h, mb_type);
if(!IS_INTRA16x16(mb_type)){
cbp= get_ue_golomb(&s->gb);
if(cbp > 47){
av_log(h->s.avctx, AV_LOG_ERROR, "cbp too large (%u) at %d %d\n", cbp, s->mb_x, s->mb_y);
return -1;
}
if(CHROMA){
if(IS_INTRA4x4(mb_type)) cbp= golomb_to_intra4x4_cbp[cbp];
else cbp= golomb_to_inter_cbp [cbp];
}else{
if(IS_INTRA4x4(mb_type)) cbp= golomb_to_intra4x4_cbp_gray[cbp];
else cbp= golomb_to_inter_cbp_gray[cbp];
}
}
if(dct8x8_allowed && (cbp&15) && !IS_INTRA(mb_type)){
mb_type |= MB_TYPE_8x8DCT*get_bits1(&s->gb);
}
h->cbp=
h->cbp_table[mb_xy]= cbp;
s->current_picture.mb_type[mb_xy]= mb_type;
if(cbp || IS_INTRA16x16(mb_type)){
int i8x8, i4x4, chroma_idx;
int dquant;
GetBitContext *gb= IS_INTRA(mb_type) ? h->intra_gb_ptr : h->inter_gb_ptr;
const uint8_t *scan, *scan8x8, *dc_scan;
if(IS_INTERLACED(mb_type)){
scan8x8= s->qscale ? h->field_scan8x8_cavlc : h->field_scan8x8_cavlc_q0;
scan= s->qscale ? h->field_scan : h->field_scan_q0;
dc_scan= luma_dc_field_scan;
}else{
scan8x8= s->qscale ? h->zigzag_scan8x8_cavlc : h->zigzag_scan8x8_cavlc_q0;
scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;
dc_scan= luma_dc_zigzag_scan;
}
dquant= get_se_golomb(&s->gb);
s->qscale += dquant;
if(((unsigned)s->qscale) > 51){
if(s->qscale<0) s->qscale+= 52;
else s->qscale-= 52;
if(((unsigned)s->qscale) > 51){
av_log(h->s.avctx, AV_LOG_ERROR, "dquant out of range (%d) at %d %d\n", dquant, s->mb_x, s->mb_y);
return -1;
}
}
h->chroma_qp[0]= get_chroma_qp(h, 0, s->qscale);
h->chroma_qp[1]= get_chroma_qp(h, 1, s->qscale);
if(IS_INTRA16x16(mb_type)){
if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, h->dequant4_coeff[0][s->qscale], 16) < 0){
return -1;
}
assert((cbp&15) == 0 || (cbp&15) == 15);
if(cbp&15){
for(i8x8=0; i8x8<4; i8x8++){
for(i4x4=0; i4x4<4; i4x4++){
const int index= i4x4 + 4*i8x8;
if( decode_residual(h, h->intra_gb_ptr, h->mb + 16*index, index, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 ){
return -1;
}
}
}
}else{
fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);
}
}else{
for(i8x8=0; i8x8<4; i8x8++){
if(cbp & (1<<i8x8)){
if(IS_8x8DCT(mb_type)){
DCTELEM *buf = &h->mb[64*i8x8];
uint8_t *nnz;
for(i4x4=0; i4x4<4; i4x4++){
if( decode_residual(h, gb, buf, i4x4+4*i8x8, scan8x8+16*i4x4,
h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 16) <0 )
return -1;
}
nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
nnz[0] += nnz[1] + nnz[8] + nnz[9];
}else{
for(i4x4=0; i4x4<4; i4x4++){
const int index= i4x4 + 4*i8x8;
if( decode_residual(h, gb, h->mb + 16*index, index, scan, h->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][s->qscale], 16) <0 ){
return -1;
}
}
}
}else{
uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
}
}
}
if(cbp&0x30){
for(chroma_idx=0; chroma_idx<2; chroma_idx++)
if( decode_residual(h, gb, h->mb + 256 + 16*4*chroma_idx, CHROMA_DC_BLOCK_INDEX, chroma_dc_scan, NULL, 4) < 0){
return -1;
}
}
if(cbp&0x20){
for(chroma_idx=0; chroma_idx<2; chroma_idx++){
const uint32_t *qmul = h->dequant4_coeff[chroma_idx+1+(IS_INTRA( mb_type ) ? 0:3)][h->chroma_qp[chroma_idx]];
for(i4x4=0; i4x4<4; i4x4++){
const int index= 16 + 4*chroma_idx + i4x4;
if( decode_residual(h, gb, h->mb + 16*index, index, scan + 1, qmul, 15) < 0){
return -1;
}
}
}
}else{
uint8_t * const nnz= &h->non_zero_count_cache[0];
nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
}
}else{
uint8_t * const nnz= &h->non_zero_count_cache[0];
fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);
nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =
nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;
}
s->current_picture.qscale_table[mb_xy]= s->qscale;
write_back_non_zero_count(h);
if(MB_MBAFF){
h->ref_count[0] >>= 1;
h->ref_count[1] >>= 1;
}
return 0;
}
|
['int ff_h264_decode_mb_cavlc(H264Context *h){\n MpegEncContext * const s = &h->s;\n int mb_xy;\n int partition_count;\n unsigned int mb_type, cbp;\n int dct8x8_allowed= h->pps.transform_8x8_mode;\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 cbp = 0;\n if(h->slice_type_nos != FF_I_TYPE){\n if(s->mb_skip_run==-1)\n s->mb_skip_run= get_ue_golomb(&s->gb);\n if (s->mb_skip_run--) {\n if(FRAME_MBAFF && (s->mb_y&1) == 0){\n if(s->mb_skip_run==0)\n h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);\n }\n decode_mb_skip(h);\n return 0;\n }\n }\n if(FRAME_MBAFF){\n if( (s->mb_y&1) == 0 )\n h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&s->gb);\n }\n h->prev_mb_skipped= 0;\n mb_type= get_ue_golomb(&s->gb);\n if(h->slice_type_nos == FF_B_TYPE){\n if(mb_type < 23){\n partition_count= b_mb_type_info[mb_type].partition_count;\n mb_type= b_mb_type_info[mb_type].type;\n }else{\n mb_type -= 23;\n goto decode_intra_mb;\n }\n }else if(h->slice_type_nos == FF_P_TYPE){\n if(mb_type < 5){\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 -= 5;\n goto decode_intra_mb;\n }\n }else{\n assert(h->slice_type_nos == FF_I_TYPE);\n if(h->slice_type == FF_SI_TYPE && mb_type)\n mb_type--;\ndecode_intra_mb:\n if(mb_type > 25){\n av_log(h->s.avctx, AV_LOG_ERROR, "mb_type %d in %c slice too large at %d %d\\n", mb_type, av_get_pict_type_char(h->slice_type), s->mb_x, s->mb_y);\n return -1;\n }\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 unsigned int x;\n align_get_bits(&s->gb);\n for(x=0; x < (CHROMA ? 384 : 256); x++){\n ((uint8_t*)h->mb)[x]= get_bits(&s->gb, 8);\n }\n s->current_picture.qscale_table[mb_xy]= 0;\n memset(h->non_zero_count[mb_xy], 16, 32);\n s->current_picture.mb_type[mb_xy]= mb_type;\n return 0;\n }\n if(MB_MBAFF){\n h->ref_count[0] <<= 1;\n h->ref_count[1] <<= 1;\n }\n fill_decode_neighbors(h, mb_type);\n fill_decode_caches(h, mb_type);\n if(IS_INTRA(mb_type)){\n int pred_mode;\n if(IS_INTRA4x4(mb_type)){\n int i;\n int di = 1;\n if(dct8x8_allowed && get_bits1(&s->gb)){\n mb_type |= MB_TYPE_8x8DCT;\n di = 4;\n }\n for(i=0; i<16; i+=di){\n int mode= pred_intra_mode(h, i);\n if(!get_bits1(&s->gb)){\n const int rem_mode= get_bits(&s->gb, 3);\n mode = rem_mode + (rem_mode >= mode);\n }\n if(di==4)\n fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );\n else\n h->intra4x4_pred_mode_cache[ scan8[i] ] = mode;\n }\n ff_h264_write_back_intra_pred_mode(h);\n if( ff_h264_check_intra4x4_pred_mode(h) < 0)\n 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)\n return -1;\n }\n if(CHROMA){\n pred_mode= ff_h264_check_intra_pred_mode(h, get_ue_golomb_31(&s->gb));\n if(pred_mode < 0)\n return -1;\n h->chroma_pred_mode= pred_mode;\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 == FF_B_TYPE){\n for(i=0; i<4; i++){\n h->sub_mb_type[i]= get_ue_golomb_31(&s->gb);\n if(h->sub_mb_type[i] >=13){\n av_log(h->s.avctx, AV_LOG_ERROR, "B sub_mb_type %u out of range at %d %d\\n", h->sub_mb_type[i], s->mb_x, s->mb_y);\n return -1;\n }\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]|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 }\n }else{\n assert(h->slice_type_nos == FF_P_TYPE);\n for(i=0; i<4; i++){\n h->sub_mb_type[i]= get_ue_golomb_31(&s->gb);\n if(h->sub_mb_type[i] >=4){\n av_log(h->s.avctx, AV_LOG_ERROR, "P sub_mb_type %u out of range at %d %d\\n", h->sub_mb_type[i], s->mb_x, s->mb_y);\n return -1;\n }\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 int ref_count= IS_REF0(mb_type) ? 1 : h->ref_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 unsigned int tmp;\n if(ref_count == 1){\n tmp= 0;\n }else if(ref_count == 2){\n tmp= get_bits1(&s->gb)^1;\n }else{\n tmp= get_ue_golomb_31(&s->gb);\n if(tmp>=ref_count){\n av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\\n", tmp);\n return -1;\n }\n }\n ref[list][i]= tmp;\n }else{\n ref[list][i] = -1;\n }\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 if(IS_DIRECT(h->sub_mb_type[i])) {\n h->ref_cache[list][ scan8[4*i] ] = h->ref_cache[list][ scan8[4*i]+1 ];\n continue;\n }\n h->ref_cache[list][ scan8[4*i] ]=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 if(IS_DIR(h->sub_mb_type[i], 0, list)){\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 mx, my;\n const int index= 4*i + block_width*j;\n int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];\n pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my);\n mx += get_se_golomb(&s->gb);\n my += get_se_golomb(&s->gb);\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 }else if(IS_SUB_8X4(sub_mb_type)){\n mv_cache[ 1 ][0]= mx;\n mv_cache[ 1 ][1]= my;\n }else if(IS_SUB_4X8(sub_mb_type)){\n mv_cache[ 8 ][0]= mx;\n mv_cache[ 8 ][1]= my;\n }\n mv_cache[ 0 ][0]= mx;\n mv_cache[ 0 ][1]= my;\n }\n }else{\n uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];\n p[0] = p[1]=\n p[8] = p[9]= 0;\n }\n }\n }\n }else if(IS_DIRECT(mb_type)){\n ff_h264_pred_direct_motion(h, &mb_type);\n dct8x8_allowed &= h->sps.direct_8x8_inference_flag;\n }else{\n int list, mx, my, i;\n if(IS_16X16(mb_type)){\n for(list=0; list<h->list_count; list++){\n unsigned int val;\n if(IS_DIR(mb_type, 0, list)){\n if(h->ref_count[list]==1){\n val= 0;\n }else if(h->ref_count[list]==2){\n val= get_bits1(&s->gb)^1;\n }else{\n val= get_ue_golomb_31(&s->gb);\n if(val >= h->ref_count[list]){\n av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\\n", val);\n return -1;\n }\n }\n }else\n val= LIST_NOT_USED&0xFF;\n fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, val, 1);\n }\n for(list=0; list<h->list_count; list++){\n unsigned int val;\n if(IS_DIR(mb_type, 0, list)){\n pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my);\n mx += get_se_golomb(&s->gb);\n my += get_se_golomb(&s->gb);\n tprintf(s->avctx, "final mv:%d %d\\n", mx, my);\n val= pack16to32(mx,my);\n }else\n val=0;\n fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, val, 4);\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 unsigned int val;\n if(IS_DIR(mb_type, i, list)){\n if(h->ref_count[list] == 1){\n val= 0;\n }else if(h->ref_count[list] == 2){\n val= get_bits1(&s->gb)^1;\n }else{\n val= get_ue_golomb_31(&s->gb);\n if(val >= h->ref_count[list]){\n av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\\n", val);\n return -1;\n }\n }\n }else\n val= LIST_NOT_USED&0xFF;\n fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 1);\n }\n }\n for(list=0; list<h->list_count; list++){\n for(i=0; i<2; i++){\n unsigned int val;\n if(IS_DIR(mb_type, i, list)){\n pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my);\n mx += get_se_golomb(&s->gb);\n my += get_se_golomb(&s->gb);\n tprintf(s->avctx, "final mv:%d %d\\n", mx, my);\n val= pack16to32(mx,my);\n }else\n val=0;\n fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 4);\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 unsigned int val;\n if(IS_DIR(mb_type, i, list)){\n if(h->ref_count[list]==1){\n val= 0;\n }else if(h->ref_count[list]==2){\n val= get_bits1(&s->gb)^1;\n }else{\n val= get_ue_golomb_31(&s->gb);\n if(val >= h->ref_count[list]){\n av_log(h->s.avctx, AV_LOG_ERROR, "ref %u overflow\\n", val);\n return -1;\n }\n }\n }else\n val= LIST_NOT_USED&0xFF;\n fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 1);\n }\n }\n for(list=0; list<h->list_count; list++){\n for(i=0; i<2; i++){\n unsigned int val;\n if(IS_DIR(mb_type, i, list)){\n pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my);\n mx += get_se_golomb(&s->gb);\n my += get_se_golomb(&s->gb);\n tprintf(s->avctx, "final mv:%d %d\\n", mx, my);\n val= pack16to32(mx,my);\n }else\n val=0;\n fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 4);\n }\n }\n }\n }\n if(IS_INTER(mb_type))\n write_back_motion(h, mb_type);\n if(!IS_INTRA16x16(mb_type)){\n cbp= get_ue_golomb(&s->gb);\n if(cbp > 47){\n av_log(h->s.avctx, AV_LOG_ERROR, "cbp too large (%u) at %d %d\\n", cbp, s->mb_x, s->mb_y);\n return -1;\n }\n if(CHROMA){\n if(IS_INTRA4x4(mb_type)) cbp= golomb_to_intra4x4_cbp[cbp];\n else cbp= golomb_to_inter_cbp [cbp];\n }else{\n if(IS_INTRA4x4(mb_type)) cbp= golomb_to_intra4x4_cbp_gray[cbp];\n else cbp= golomb_to_inter_cbp_gray[cbp];\n }\n }\n if(dct8x8_allowed && (cbp&15) && !IS_INTRA(mb_type)){\n mb_type |= MB_TYPE_8x8DCT*get_bits1(&s->gb);\n }\n h->cbp=\n h->cbp_table[mb_xy]= cbp;\n s->current_picture.mb_type[mb_xy]= mb_type;\n if(cbp || IS_INTRA16x16(mb_type)){\n int i8x8, i4x4, chroma_idx;\n int dquant;\n GetBitContext *gb= IS_INTRA(mb_type) ? h->intra_gb_ptr : h->inter_gb_ptr;\n const uint8_t *scan, *scan8x8, *dc_scan;\n if(IS_INTERLACED(mb_type)){\n scan8x8= s->qscale ? h->field_scan8x8_cavlc : h->field_scan8x8_cavlc_q0;\n scan= s->qscale ? h->field_scan : h->field_scan_q0;\n dc_scan= luma_dc_field_scan;\n }else{\n scan8x8= s->qscale ? h->zigzag_scan8x8_cavlc : h->zigzag_scan8x8_cavlc_q0;\n scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;\n dc_scan= luma_dc_zigzag_scan;\n }\n dquant= get_se_golomb(&s->gb);\n s->qscale += dquant;\n if(((unsigned)s->qscale) > 51){\n if(s->qscale<0) s->qscale+= 52;\n else s->qscale-= 52;\n if(((unsigned)s->qscale) > 51){\n av_log(h->s.avctx, AV_LOG_ERROR, "dquant out of range (%d) at %d %d\\n", dquant, s->mb_x, s->mb_y);\n return -1;\n }\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 if(IS_INTRA16x16(mb_type)){\n if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, h->dequant4_coeff[0][s->qscale], 16) < 0){\n return -1;\n }\n assert((cbp&15) == 0 || (cbp&15) == 15);\n if(cbp&15){\n for(i8x8=0; i8x8<4; i8x8++){\n for(i4x4=0; i4x4<4; i4x4++){\n const int index= i4x4 + 4*i8x8;\n if( decode_residual(h, h->intra_gb_ptr, h->mb + 16*index, index, scan + 1, h->dequant4_coeff[0][s->qscale], 15) < 0 ){\n return -1;\n }\n }\n }\n }else{\n fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);\n }\n }else{\n for(i8x8=0; i8x8<4; i8x8++){\n if(cbp & (1<<i8x8)){\n if(IS_8x8DCT(mb_type)){\n DCTELEM *buf = &h->mb[64*i8x8];\n uint8_t *nnz;\n for(i4x4=0; i4x4<4; i4x4++){\n if( decode_residual(h, gb, buf, i4x4+4*i8x8, scan8x8+16*i4x4,\n h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 16) <0 )\n return -1;\n }\n nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];\n nnz[0] += nnz[1] + nnz[8] + nnz[9];\n }else{\n for(i4x4=0; i4x4<4; i4x4++){\n const int index= i4x4 + 4*i8x8;\n if( decode_residual(h, gb, h->mb + 16*index, index, scan, h->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][s->qscale], 16) <0 ){\n return -1;\n }\n }\n }\n }else{\n uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];\n nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;\n }\n }\n }\n if(cbp&0x30){\n for(chroma_idx=0; chroma_idx<2; chroma_idx++)\n if( decode_residual(h, gb, h->mb + 256 + 16*4*chroma_idx, CHROMA_DC_BLOCK_INDEX, chroma_dc_scan, NULL, 4) < 0){\n return -1;\n }\n }\n if(cbp&0x20){\n for(chroma_idx=0; chroma_idx<2; chroma_idx++){\n const uint32_t *qmul = h->dequant4_coeff[chroma_idx+1+(IS_INTRA( mb_type ) ? 0:3)][h->chroma_qp[chroma_idx]];\n for(i4x4=0; i4x4<4; i4x4++){\n const int index= 16 + 4*chroma_idx + i4x4;\n if( decode_residual(h, gb, h->mb + 16*index, index, scan + 1, qmul, 15) < 0){\n return -1;\n }\n }\n }\n }else{\n uint8_t * const nnz= &h->non_zero_count_cache[0];\n nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =\n nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0;\n }\n }else{\n uint8_t * const nnz= &h->non_zero_count_cache[0];\n fill_rectangle(&nnz[scan8[0]], 4, 4, 8, 0, 1);\n nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] =\n nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 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}']
|
1,064 | 0 |
https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/resample2.c/#L187
|
AVResampleContext *av_resample_init(int out_rate, int in_rate, int filter_size, int phase_shift, int linear, double cutoff){
AVResampleContext *c= av_mallocz(sizeof(AVResampleContext));
double factor= FFMIN(out_rate * cutoff / in_rate, 1.0);
int phase_count= 1<<phase_shift;
c->phase_shift= phase_shift;
c->phase_mask= phase_count-1;
c->linear= linear;
c->filter_length= FFMAX((int)ceil(filter_size/factor), 1);
c->filter_bank= av_mallocz(c->filter_length*(phase_count+1)*sizeof(FELEM));
av_build_filter(c->filter_bank, factor, c->filter_length, phase_count, 1<<FILTER_SHIFT, WINDOW_TYPE);
memcpy(&c->filter_bank[c->filter_length*phase_count+1], c->filter_bank, (c->filter_length-1)*sizeof(FELEM));
c->filter_bank[c->filter_length*phase_count]= c->filter_bank[c->filter_length - 1];
c->src_incr= out_rate;
c->ideal_dst_incr= c->dst_incr= in_rate * phase_count;
c->index= -phase_count*((c->filter_length-1)/2);
return c;
}
|
['AVResampleContext *av_resample_init(int out_rate, int in_rate, int filter_size, int phase_shift, int linear, double cutoff){\n AVResampleContext *c= av_mallocz(sizeof(AVResampleContext));\n double factor= FFMIN(out_rate * cutoff / in_rate, 1.0);\n int phase_count= 1<<phase_shift;\n c->phase_shift= phase_shift;\n c->phase_mask= phase_count-1;\n c->linear= linear;\n c->filter_length= FFMAX((int)ceil(filter_size/factor), 1);\n c->filter_bank= av_mallocz(c->filter_length*(phase_count+1)*sizeof(FELEM));\n av_build_filter(c->filter_bank, factor, c->filter_length, phase_count, 1<<FILTER_SHIFT, WINDOW_TYPE);\n memcpy(&c->filter_bank[c->filter_length*phase_count+1], c->filter_bank, (c->filter_length-1)*sizeof(FELEM));\n c->filter_bank[c->filter_length*phase_count]= c->filter_bank[c->filter_length - 1];\n c->src_incr= out_rate;\n c->ideal_dst_incr= c->dst_incr= in_rate * phase_count;\n c->index= -phase_count*((c->filter_length-1)/2);\n return c;\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}']
|
1,065 | 0 |
https://github.com/openssl/openssl/blob/d00b1d62d62036dc21c78658a28da4a6279e6fe2/crypto/bn/bn_ctx.c/#L354
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['static int test_exp_mod_zero() {\n\tBIGNUM *a = NULL, *p = NULL, *m = NULL;\n\tBIGNUM *r = NULL;\n\tBN_CTX *ctx = BN_CTX_new();\n\tint ret = 1;\n\tm = BN_new();\n\tif(!m) goto err;\n\tBN_one(m);\n\ta = BN_new();\n\tif(!a) goto err;\n\tBN_one(a);\n\tp = BN_new();\n\tif(!p) goto err;\n\tBN_zero(p);\n\tr = BN_new();\n\tif(!r) goto err;\n\tBN_mod_exp(r, a, p, m, ctx);\n\tBN_CTX_free(ctx);\n\tif (BN_is_zero(r))\n\t\tret = 0;\n\telse\n\t\t{\n\t\tprintf("1**0 mod 1 = ");\n\t\tBN_print_fp(stdout, r);\n\t\tprintf(", should be 0\\n");\n\t\t}\nerr:\n\tBN_free(r);\n\tBN_free(a);\n\tBN_free(p);\n\tBN_free(m);\n\treturn ret;\n}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n\t BN_CTX *ctx)\n\t{\n\tint ret;\n\tbn_check_top(a);\n\tbn_check_top(p);\n\tbn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n\tif (BN_is_odd(m))\n\t\t{\n# ifdef MONT_EXP_WORD\n\t\tif (a->top == 1 && !a->neg && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0))\n\t\t\t{\n\t\t\tBN_ULONG A = a->d[0];\n\t\t\tret=BN_mod_exp_mont_word(r,A,p,m,ctx,NULL);\n\t\t\t}\n\t\telse\n# endif\n\t\t\tret=BN_mod_exp_mont(r,a,p,m,ctx,NULL);\n\t\t}\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\tbn_check_top(r);\n\treturn(ret);\n\t}', 'int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,\n\t\t const BIGNUM *m, BN_CTX *ctx)\n\t{\n\tint i,j,bits,ret=0,wstart,wend,window,wvalue;\n\tint start=1;\n\tBIGNUM *aa;\n\tBIGNUM *val[TABLE_SIZE];\n\tBN_RECP_CTX recp;\n\tif (BN_get_flags(p, BN_FLG_CONSTTIME) != 0)\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_EXP_RECP,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n\t\treturn -1;\n\t\t}\n\tbits=BN_num_bits(p);\n\tif (bits == 0)\n\t\t{\n\t\tret = BN_one(r);\n\t\treturn ret;\n\t\t}\n\tBN_CTX_start(ctx);\n\taa = BN_CTX_get(ctx);\n\tval[0] = BN_CTX_get(ctx);\n\tif(!aa || !val[0]) goto err;\n\tBN_RECP_CTX_init(&recp);\n\tif (m->neg)\n\t\t{\n\t\tif (!BN_copy(aa, m)) goto err;\n\t\taa->neg = 0;\n\t\tif (BN_RECP_CTX_set(&recp,aa,ctx) <= 0) goto err;\n\t\t}\n\telse\n\t\t{\n\t\tif (BN_RECP_CTX_set(&recp,m,ctx) <= 0) goto err;\n\t\t}\n\tif (!BN_nnmod(val[0],a,m,ctx)) goto err;\n\tif (BN_is_zero(val[0]))\n\t\t{\n\t\tBN_zero(r);\n\t\tret = 1;\n\t\tgoto err;\n\t\t}\n\twindow = BN_window_bits_for_exponent_size(bits);\n\tif (window > 1)\n\t\t{\n\t\tif (!BN_mod_mul_reciprocal(aa,val[0],val[0],&recp,ctx))\n\t\t\tgoto err;\n\t\tj=1<<(window-1);\n\t\tfor (i=1; i<j; i++)\n\t\t\t{\n\t\t\tif(((val[i] = BN_CTX_get(ctx)) == NULL) ||\n\t\t\t\t\t!BN_mod_mul_reciprocal(val[i],val[i-1],\n\t\t\t\t\t\taa,&recp,ctx))\n\t\t\t\tgoto err;\n\t\t\t}\n\t\t}\n\tstart=1;\n\twvalue=0;\n\twstart=bits-1;\n\twend=0;\n\tif (!BN_one(r)) 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\tif (!BN_mod_mul_reciprocal(r,r,r,&recp,ctx))\n\t\t\t\tgoto err;\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_reciprocal(r,r,r,&recp,ctx))\n\t\t\t\t\tgoto err;\n\t\t\t\t}\n\t\tif (!BN_mod_mul_reciprocal(r,r,val[wvalue>>1],&recp,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\tret=1;\nerr:\n\tBN_CTX_end(ctx);\n\tBN_RECP_CTX_free(&recp);\n\tbn_check_top(r);\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\tint no_branch=0;\n\tif (num->top > 0 && num->d[num->top - 1] == 0)\n\t\t{\n\t\tBNerr(BN_F_BN_DIV,BN_R_NOT_INITIALIZED);\n\t\treturn 0;\n\t\t}\n\tbn_check_top(num);\n\tif ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0))\n\t\t{\n\t\tno_branch=1;\n\t\t}\n\tbn_check_top(dv);\n\tbn_check_top(rm);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\n\t\t}\n\tif (!no_branch && BN_ucmp(num,divisor) < 0)\n\t\t{\n\t\tif (rm != NULL)\n\t\t\t{ if (BN_copy(rm,num) == NULL) return(0); }\n\t\tif (dv != NULL) BN_zero(dv);\n\t\treturn(1);\n\t\t}\n\tBN_CTX_start(ctx);\n\ttmp=BN_CTX_get(ctx);\n\tsnum=BN_CTX_get(ctx);\n\tsdiv=BN_CTX_get(ctx);\n\tif (dv == NULL)\n\t\tres=BN_CTX_get(ctx);\n\telse\tres=dv;\n\tif (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)\n\t\tgoto err;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tif (!(BN_lshift(sdiv,divisor,norm_shift))) goto err;\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tif (!(BN_lshift(snum,num,norm_shift))) goto err;\n\tsnum->neg=0;\n\tif (no_branch)\n\t\t{\n\t\tif (snum->top <= sdiv->top+1)\n\t\t\t{\n\t\t\tif (bn_wexpand(snum, sdiv->top + 2) == NULL) goto err;\n\t\t\tfor (i = snum->top; i < sdiv->top + 2; i++) snum->d[i] = 0;\n\t\t\tsnum->top = sdiv->top + 2;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (bn_wexpand(snum, snum->top + 1) == NULL) goto err;\n\t\t\tsnum->d[snum->top] = 0;\n\t\t\tsnum->top ++;\n\t\t\t}\n\t\t}\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\twnum.neg = 0;\n\twnum.d = &(snum->d[loop]);\n\twnum.top = div_n;\n\twnum.dmax = snum->dmax - loop;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop-no_branch;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (!no_branch)\n\t\t{\n\t\tif (BN_ucmp(&wnum,sdiv) >= 0)\n\t\t\t{\n\t\t\tbn_clear_top2max(&wnum);\n\t\t\tbn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n\t\t\t*resp=1;\n\t\t\t}\n\t\telse\n\t\t\tres->top--;\n\t\t}\n\tif (res->top == 0)\n\t\tres->neg = 0;\n\telse\n\t\tresp--;\n\tfor (i=0; i<loop-1; i++, wnump--, resp--)\n\t\t{\n\t\tBN_ULONG q,l0;\n#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n\t\tBN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG);\n\t\tq=bn_div_3_words(wnump,d1,d0);\n#else\n\t\tBN_ULONG n0,n1,rem=0;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\t{\n#ifdef BN_LLONG\n\t\t\tBN_ULLONG t2;\n#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n\t\t\tq=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);\n#else\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tt2 -= d1;\n\t\t\t\t}\n#else\n\t\t\tBN_ULONG t2l,t2h;\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#if defined(BN_UMULT_LOHI)\n\t\t\tBN_UMULT_LOHI(t2l,t2h,d1,q);\n#elif defined(BN_UMULT_HIGH)\n\t\t\tt2l = d1 * q;\n\t\t\tt2h = BN_UMULT_HIGH(d1,q);\n#else\n\t\t\t{\n\t\t\tBN_ULONG ql, qh;\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n\t\t\t}\n#endif\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif ((t2h < rem) ||\n\t\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t\t}\n#endif\n\t\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\twnum.d--;\n\t\tif (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1))\n\t\t\t{\n\t\t\tq--;\n\t\t\tif (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n\t\t\t\t(*wnump)++;\n\t\t\t}\n\t\t*resp = q;\n\t\t}\n\tbn_correct_top(snum);\n\tif (rm != NULL)\n\t\t{\n\t\tint neg = num->neg;\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\tif (!BN_is_zero(rm))\n\t\t\trm->neg = neg;\n\t\tbn_check_top(rm);\n\t\t}\n\tif (no_branch)\tbn_correct_top(res);\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'void BN_CTX_end(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_end", ctx);\n\tif(ctx->err_stack)\n\t\tctx->err_stack--;\n\telse\n\t\t{\n\t\tunsigned int fp = BN_STACK_pop(&ctx->stack);\n\t\tif(fp < ctx->used)\n\t\t\tBN_POOL_release(&ctx->pool, ctx->used - fp);\n\t\tctx->used = fp;\n\t\tctx->too_many = 0;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n\t{\n\treturn st->indexes[--(st->depth)];\n\t}']
|
1,066 | 0 |
https://github.com/openssl/openssl/blob/f006217bb628d05a2d5b866ff252bd94e3477e1f/crypto/bn/bn_lib.c/#L351
|
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
BN_ULONG *A, *a = NULL;
const BN_ULONG *B;
int i;
bn_check_top(b);
if (words > (INT_MAX / (4 * BN_BITS2))) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
return NULL;
}
if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
return (NULL);
}
if (BN_get_flags(b,BN_FLG_SECURE))
a = A = OPENSSL_secure_malloc(words * sizeof(*a));
else
a = A = OPENSSL_malloc(words * sizeof(*a));
if (A == NULL) {
BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
return (NULL);
}
#ifdef PURIFY
memset(a, 0, sizeof(*a) * words);
#endif
#if 1
B = b->d;
if (B != NULL) {
for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {
BN_ULONG a0, a1, a2, a3;
a0 = B[0];
a1 = B[1];
a2 = B[2];
a3 = B[3];
A[0] = a0;
A[1] = a1;
A[2] = a2;
A[3] = a3;
}
switch (b->top & 3) {
case 3:
A[2] = B[2];
case 2:
A[1] = B[1];
case 1:
A[0] = B[0];
case 0:
;
}
}
#else
memset(A, 0, sizeof(*A) * words);
memcpy(A, b->d, sizeof(b->d[0]) * b->top);
#endif
return (a);
}
|
['static EC_KEY *mk_eckey(int nid, const unsigned char *p, size_t plen)\n{\n int ok = 0;\n EC_KEY *k = NULL;\n BIGNUM *priv = NULL;\n EC_POINT *pub = NULL;\n const EC_GROUP *grp;\n k = EC_KEY_new_by_curve_name(nid);\n if (!k)\n goto err;\n priv = BN_bin2bn(p, plen, NULL);\n if (!priv)\n goto err;\n if (!EC_KEY_set_private_key(k, priv))\n goto err;\n grp = EC_KEY_get0_group(k);\n pub = EC_POINT_new(grp);\n if (!pub)\n goto err;\n if (!EC_POINT_mul(grp, pub, priv, NULL, NULL, NULL))\n goto err;\n if (!EC_KEY_set_public_key(k, pub))\n goto err;\n ok = 1;\n err:\n BN_clear_free(priv);\n EC_POINT_free(pub);\n if (ok)\n return k;\n EC_KEY_free(k);\n return NULL;\n}', 'EC_KEY *EC_KEY_new_by_curve_name(int nid)\n{\n EC_KEY *ret = EC_KEY_new();\n if (ret == NULL)\n return NULL;\n ret->group = EC_GROUP_new_by_curve_name(nid);\n if (ret->group == NULL) {\n EC_KEY_free(ret);\n return NULL;\n }\n if (ret->meth->set_group != NULL\n && ret->meth->set_group(ret, ret->group) == 0) {\n EC_KEY_free(ret);\n return NULL;\n }\n return ret;\n}', 'EC_KEY *EC_KEY_new(void)\n{\n return EC_KEY_new_method(NULL);\n}', 'EC_KEY *EC_KEY_new_method(ENGINE *engine)\n{\n EC_KEY *ret = OPENSSL_zalloc(sizeof(*ret));\n if (ret == NULL) {\n ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EC_KEY, ret, &ret->ex_data)) {\n OPENSSL_free(ret);\n return NULL;\n }\n ret->meth = EC_KEY_get_default_method();\n#ifndef OPENSSL_NO_ENGINE\n if (engine != NULL) {\n if (!ENGINE_init(engine)) {\n ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_ENGINE_LIB);\n OPENSSL_free(ret);\n return NULL;\n }\n ret->engine = engine;\n } else\n ret->engine = ENGINE_get_default_EC();\n if (ret->engine != NULL) {\n ret->meth = ENGINE_get_EC(ret->engine);\n if (ret->meth == NULL) {\n ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_ENGINE_LIB);\n ENGINE_finish(ret->engine);\n OPENSSL_free(ret);\n return NULL;\n }\n }\n#endif\n ret->version = 1;\n ret->conv_form = POINT_CONVERSION_UNCOMPRESSED;\n ret->references = 1;\n if (ret->meth->init != NULL && ret->meth->init(ret) == 0) {\n EC_KEY_free(ret);\n return NULL;\n }\n return ret;\n}', 'void EC_KEY_free(EC_KEY *r)\n{\n int i;\n if (r == NULL)\n return;\n i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_EC);\n#ifdef REF_PRINT\n REF_PRINT("EC_KEY", r);\n#endif\n if (i > 0)\n return;\n#ifdef REF_CHECK\n if (i < 0) {\n fprintf(stderr, "EC_KEY_free, bad reference count\\n");\n abort();\n }\n#endif\n if (r->meth->finish != NULL)\n r->meth->finish(r);\n#ifndef OPENSSL_NO_ENGINE\n if (r->engine != NULL)\n ENGINE_finish(r->engine);\n#endif\n CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY, r, &r->ex_data);\n EC_GROUP_free(r->group);\n EC_POINT_free(r->pub_key);\n BN_clear_free(r->priv_key);\n OPENSSL_clear_free((void *)r, sizeof(EC_KEY));\n}', 'BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)\n{\n unsigned int i, m;\n unsigned int n;\n BN_ULONG l;\n BIGNUM *bn = NULL;\n if (ret == NULL)\n ret = bn = BN_new();\n if (ret == NULL)\n return (NULL);\n bn_check_top(ret);\n for ( ; len > 0 && *s == 0; s++, len--)\n continue;\n n = len;\n if (n == 0) {\n ret->top = 0;\n return (ret);\n }\n i = ((n - 1) / BN_BYTES) + 1;\n m = ((n - 1) % (BN_BYTES));\n if (bn_wexpand(ret, (int)i) == NULL) {\n BN_free(bn);\n return NULL;\n }\n ret->top = i;\n ret->neg = 0;\n l = 0;\n while (n--) {\n l = (l << 8L) | *(s++);\n if (m-- == 0) {\n ret->d[--i] = l;\n l = 0;\n m = BN_BYTES - 1;\n }\n }\n bn_correct_top(ret);\n return (ret);\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *A, *a = NULL;\n const BN_ULONG *B;\n int i;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return (NULL);\n }\n if (BN_get_flags(b,BN_FLG_SECURE))\n a = A = OPENSSL_secure_malloc(words * sizeof(*a));\n else\n a = A = OPENSSL_malloc(words * sizeof(*a));\n if (A == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n#ifdef PURIFY\n memset(a, 0, sizeof(*a) * words);\n#endif\n#if 1\n B = b->d;\n if (B != NULL) {\n for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {\n BN_ULONG a0, a1, a2, a3;\n a0 = B[0];\n a1 = B[1];\n a2 = B[2];\n a3 = B[3];\n A[0] = a0;\n A[1] = a1;\n A[2] = a2;\n A[3] = a3;\n }\n switch (b->top & 3) {\n case 3:\n A[2] = B[2];\n case 2:\n A[1] = B[1];\n case 1:\n A[0] = B[0];\n case 0:\n ;\n }\n }\n#else\n memset(A, 0, sizeof(*A) * words);\n memcpy(A, b->d, sizeof(b->d[0]) * b->top);\n#endif\n return (a);\n}']
|
1,067 | 0 |
https://github.com/libav/libav/blob/73b02e24604961e49a63ca34203d8f6c56612117/libavformat/dv.c/#L122
|
static int dv_extract_audio(uint8_t* frame, uint8_t* ppcm[4],
const DVprofile *sys)
{
int size, chan, i, j, d, of, smpls, freq, quant, half_ch;
uint16_t lc, rc;
const uint8_t* as_pack;
uint8_t *pcm, ipcm;
as_pack = dv_extract_pack(frame, dv_audio_source);
if (!as_pack)
return 0;
smpls = as_pack[1] & 0x3f;
freq = (as_pack[4] >> 3) & 0x07;
quant = as_pack[4] & 0x07;
if (quant > 1)
return -1;
size = (sys->audio_min_samples[freq] + smpls) * 4;
half_ch = sys->difseg_size / 2;
ipcm = (sys->height == 720 && !(frame[1] & 0x0C)) ? 2 : 0;
pcm = ppcm[ipcm++];
for (chan = 0; chan < sys->n_difchan; chan++) {
for (i = 0; i < sys->difseg_size; i++) {
frame += 6 * 80;
if (quant == 1 && i == half_ch) {
pcm = ppcm[ipcm++];
if (!pcm)
break;
}
for (j = 0; j < 9; j++) {
for (d = 8; d < 80; d += 2) {
if (quant == 0) {
of = sys->audio_shuffle[i][j] + (d - 8) / 2 * sys->audio_stride;
if (of*2 >= size)
continue;
pcm[of*2] = frame[d+1];
pcm[of*2+1] = frame[d];
if (pcm[of*2+1] == 0x80 && pcm[of*2] == 0x00)
pcm[of*2+1] = 0;
} else {
lc = ((uint16_t)frame[d] << 4) |
((uint16_t)frame[d+2] >> 4);
rc = ((uint16_t)frame[d+1] << 4) |
((uint16_t)frame[d+2] & 0x0f);
lc = (lc == 0x800 ? 0 : dv_audio_12to16(lc));
rc = (rc == 0x800 ? 0 : dv_audio_12to16(rc));
of = sys->audio_shuffle[i%half_ch][j] + (d - 8) / 3 * sys->audio_stride;
if (of*2 >= size)
continue;
pcm[of*2] = lc & 0xff;
pcm[of*2+1] = lc >> 8;
of = sys->audio_shuffle[i%half_ch+half_ch][j] +
(d - 8) / 3 * sys->audio_stride;
pcm[of*2] = rc & 0xff;
pcm[of*2+1] = rc >> 8;
++d;
}
}
frame += 16 * 80;
}
}
pcm = ppcm[ipcm++];
if (!pcm)
break;
}
return size;
}
|
['static int dv_read_packet(AVFormatContext *s, AVPacket *pkt)\n{\n int size;\n RawDVContext *c = s->priv_data;\n size = dv_get_packet(c->dv_demux, pkt);\n if (size < 0) {\n if (!c->dv_demux->sys)\n return AVERROR(EIO);\n size = c->dv_demux->sys->frame_size;\n if (get_buffer(s->pb, c->buf, size) <= 0)\n return AVERROR(EIO);\n size = dv_produce_packet(c->dv_demux, pkt, c->buf, size);\n }\n return size;\n}', 'int get_buffer(ByteIOContext *s, unsigned char *buf, int size)\n{\n int len, size1;\n size1 = size;\n while (size > 0) {\n len = s->buf_end - s->buf_ptr;\n if (len > size)\n len = size;\n if (len == 0) {\n if(size > s->buffer_size && !s->update_checksum){\n if(s->read_packet)\n len = s->read_packet(s->opaque, buf, size);\n if (len <= 0) {\n s->eof_reached = 1;\n if(len<0)\n s->error= len;\n break;\n } else {\n s->pos += len;\n size -= len;\n buf += len;\n s->buf_ptr = s->buffer;\n s->buf_end = s->buffer ;\n }\n }else{\n fill_buffer(s);\n len = s->buf_end - s->buf_ptr;\n if (len == 0)\n break;\n }\n } else {\n memcpy(buf, s->buf_ptr, len);\n buf += len;\n s->buf_ptr += len;\n size -= len;\n }\n }\n return size1 - size;\n}', 'int dv_produce_packet(DVDemuxContext *c, AVPacket *pkt,\n uint8_t* buf, int buf_size)\n{\n int size, i;\n uint8_t *ppcm[4] = {0};\n if (buf_size < DV_PROFILE_BYTES ||\n !(c->sys = dv_frame_profile(c->sys, buf, buf_size)) ||\n buf_size < c->sys->frame_size) {\n return -1;\n }\n size = dv_extract_audio_info(c, buf);\n for (i = 0; i < c->ach; i++) {\n c->audio_pkt[i].size = size;\n c->audio_pkt[i].pts = c->abytes * 30000*8 / c->ast[i]->codec->bit_rate;\n ppcm[i] = c->audio_buf[i];\n }\n dv_extract_audio(buf, ppcm, c->sys);\n c->abytes += size;\n if (c->sys->height == 720) {\n if (buf[1] & 0x0C)\n c->audio_pkt[2].size = c->audio_pkt[3].size = 0;\n else\n c->audio_pkt[0].size = c->audio_pkt[1].size = 0;\n }\n size = dv_extract_video_info(c, buf);\n av_init_packet(pkt);\n pkt->data = buf;\n pkt->size = size;\n pkt->flags |= PKT_FLAG_KEY;\n pkt->stream_index = c->vst->id;\n pkt->pts = c->frames;\n c->frames++;\n return size;\n}', 'static inline\nconst DVprofile* dv_frame_profile(const DVprofile *sys,\n const uint8_t* frame, unsigned buf_size)\n{\n int i;\n int dsf = (frame[3] & 0x80) >> 7;\n int stype = frame[80*5 + 48 + 3] & 0x1f;\n if (dsf == 1 && stype == 0 && frame[5] & 0x07) {\n return &dv_profiles[2];\n }\n for (i=0; i<FF_ARRAY_ELEMS(dv_profiles); i++)\n if (dsf == dv_profiles[i].dsf && stype == dv_profiles[i].video_stype)\n return &dv_profiles[i];\n if (sys && buf_size == sys->frame_size)\n return sys;\n return NULL;\n}', 'static int dv_extract_audio(uint8_t* frame, uint8_t* ppcm[4],\n const DVprofile *sys)\n{\n int size, chan, i, j, d, of, smpls, freq, quant, half_ch;\n uint16_t lc, rc;\n const uint8_t* as_pack;\n uint8_t *pcm, ipcm;\n as_pack = dv_extract_pack(frame, dv_audio_source);\n if (!as_pack)\n return 0;\n smpls = as_pack[1] & 0x3f;\n freq = (as_pack[4] >> 3) & 0x07;\n quant = as_pack[4] & 0x07;\n if (quant > 1)\n return -1;\n size = (sys->audio_min_samples[freq] + smpls) * 4;\n half_ch = sys->difseg_size / 2;\n ipcm = (sys->height == 720 && !(frame[1] & 0x0C)) ? 2 : 0;\n pcm = ppcm[ipcm++];\n for (chan = 0; chan < sys->n_difchan; chan++) {\n for (i = 0; i < sys->difseg_size; i++) {\n frame += 6 * 80;\n if (quant == 1 && i == half_ch) {\n pcm = ppcm[ipcm++];\n if (!pcm)\n break;\n }\n for (j = 0; j < 9; j++) {\n for (d = 8; d < 80; d += 2) {\n if (quant == 0) {\n of = sys->audio_shuffle[i][j] + (d - 8) / 2 * sys->audio_stride;\n if (of*2 >= size)\n continue;\n pcm[of*2] = frame[d+1];\n pcm[of*2+1] = frame[d];\n if (pcm[of*2+1] == 0x80 && pcm[of*2] == 0x00)\n pcm[of*2+1] = 0;\n } else {\n lc = ((uint16_t)frame[d] << 4) |\n ((uint16_t)frame[d+2] >> 4);\n rc = ((uint16_t)frame[d+1] << 4) |\n ((uint16_t)frame[d+2] & 0x0f);\n lc = (lc == 0x800 ? 0 : dv_audio_12to16(lc));\n rc = (rc == 0x800 ? 0 : dv_audio_12to16(rc));\n of = sys->audio_shuffle[i%half_ch][j] + (d - 8) / 3 * sys->audio_stride;\n if (of*2 >= size)\n continue;\n pcm[of*2] = lc & 0xff;\n pcm[of*2+1] = lc >> 8;\n of = sys->audio_shuffle[i%half_ch+half_ch][j] +\n (d - 8) / 3 * sys->audio_stride;\n pcm[of*2] = rc & 0xff;\n pcm[of*2+1] = rc >> 8;\n ++d;\n }\n }\n frame += 16 * 80;\n }\n }\n pcm = ppcm[ipcm++];\n if (!pcm)\n break;\n }\n return size;\n}', 'static const uint8_t* dv_extract_pack(uint8_t* frame, enum dv_pack_type t)\n{\n int offs;\n switch (t) {\n case dv_audio_source:\n offs = (80*6 + 80*16*3 + 3);\n break;\n case dv_audio_control:\n offs = (80*6 + 80*16*4 + 3);\n break;\n case dv_video_control:\n offs = (80*5 + 48 + 5);\n break;\n default:\n return NULL;\n }\n return frame[offs] == t ? &frame[offs] : NULL;\n}']
|
1,068 | 0 |
https://github.com/openssl/openssl/blob/2d5d70b15559f9813054ddb11b30b816daf62ebe/crypto/bn/bn_lib.c/#L679
|
int BN_set_bit(BIGNUM *a, int n)
{
int i, j, k;
if (n < 0)
return 0;
i = n / BN_BITS2;
j = n % BN_BITS2;
if (a->top <= i) {
if (bn_wexpand(a, i + 1) == NULL)
return (0);
for (k = a->top; k < i + 1; k++)
a->d[k] = 0;
a->top = i + 1;
}
a->d[i] |= (((BN_ULONG)1) << j);
bn_check_top(a);
return (1);
}
|
['EC_GROUP *d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len)\n{\n EC_GROUP *group = NULL;\n ECPKPARAMETERS *params = NULL;\n if ((params = d2i_ECPKPARAMETERS(NULL, in, len)) == NULL) {\n ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_D2I_ECPKPARAMETERS_FAILURE);\n ECPKPARAMETERS_free(params);\n return NULL;\n }\n if ((group = ec_asn1_pkparameters2group(params)) == NULL) {\n ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_PKPARAMETERS2GROUP_FAILURE);\n ECPKPARAMETERS_free(params);\n return NULL;\n }\n if (a) {\n EC_GROUP_clear_free(*a);\n *a = group;\n }\n ECPKPARAMETERS_free(params);\n return (group);\n}', 'EC_GROUP *ec_asn1_pkparameters2group(const ECPKPARAMETERS *params)\n{\n EC_GROUP *ret = NULL;\n int tmp = 0;\n if (params == NULL) {\n ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, EC_R_MISSING_PARAMETERS);\n return NULL;\n }\n if (params->type == 0) {\n tmp = OBJ_obj2nid(params->value.named_curve);\n if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) {\n ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP,\n EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);\n return NULL;\n }\n EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE);\n } else if (params->type == 1) {\n ret = ec_asn1_parameters2group(params->value.parameters);\n if (!ret) {\n ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, ERR_R_EC_LIB);\n return NULL;\n }\n EC_GROUP_set_asn1_flag(ret, 0x0);\n } else if (params->type == 2) {\n return NULL;\n } else {\n ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, EC_R_ASN1_ERROR);\n return NULL;\n }\n return ret;\n}', 'static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *params)\n{\n int ok = 0, tmp;\n EC_GROUP *ret = NULL;\n BIGNUM *p = NULL, *a = NULL, *b = NULL;\n EC_POINT *point = NULL;\n long field_bits;\n if (!params->fieldID || !params->fieldID->fieldType ||\n !params->fieldID->p.ptr) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);\n goto err;\n }\n if (!params->curve || !params->curve->a ||\n !params->curve->a->data || !params->curve->b ||\n !params->curve->b->data) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);\n goto err;\n }\n a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL);\n if (a == NULL) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);\n goto err;\n }\n b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);\n if (b == NULL) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);\n goto err;\n }\n tmp = OBJ_obj2nid(params->fieldID->fieldType);\n if (tmp == NID_X9_62_characteristic_two_field)\n#ifdef OPENSSL_NO_EC2M\n {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_GF2M_NOT_SUPPORTED);\n goto err;\n }\n#else\n {\n X9_62_CHARACTERISTIC_TWO *char_two;\n char_two = params->fieldID->p.char_two;\n field_bits = char_two->m;\n if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_FIELD_TOO_LARGE);\n goto err;\n }\n if ((p = BN_new()) == NULL) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n tmp = OBJ_obj2nid(char_two->type);\n if (tmp == NID_X9_62_tpBasis) {\n long tmp_long;\n if (!char_two->p.tpBasis) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);\n goto err;\n }\n tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis);\n if (!(char_two->m > tmp_long && tmp_long > 0)) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP,\n EC_R_INVALID_TRINOMIAL_BASIS);\n goto err;\n }\n if (!BN_set_bit(p, (int)char_two->m))\n goto err;\n if (!BN_set_bit(p, (int)tmp_long))\n goto err;\n if (!BN_set_bit(p, 0))\n goto err;\n } else if (tmp == NID_X9_62_ppBasis) {\n X9_62_PENTANOMIAL *penta;\n penta = char_two->p.ppBasis;\n if (!penta) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);\n goto err;\n }\n if (!\n (char_two->m > penta->k3 && penta->k3 > penta->k2\n && penta->k2 > penta->k1 && penta->k1 > 0)) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP,\n EC_R_INVALID_PENTANOMIAL_BASIS);\n goto err;\n }\n if (!BN_set_bit(p, (int)char_two->m))\n goto err;\n if (!BN_set_bit(p, (int)penta->k1))\n goto err;\n if (!BN_set_bit(p, (int)penta->k2))\n goto err;\n if (!BN_set_bit(p, (int)penta->k3))\n goto err;\n if (!BN_set_bit(p, 0))\n goto err;\n } else if (tmp == NID_X9_62_onBasis) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_NOT_IMPLEMENTED);\n goto err;\n } else {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);\n goto err;\n }\n ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL);\n }\n#endif\n else if (tmp == NID_X9_62_prime_field) {\n if (!params->fieldID->p.prime) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);\n goto err;\n }\n p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL);\n if (p == NULL) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);\n goto err;\n }\n if (BN_is_negative(p) || BN_is_zero(p)) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_FIELD);\n goto err;\n }\n field_bits = BN_num_bits(p);\n if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_FIELD_TOO_LARGE);\n goto err;\n }\n ret = EC_GROUP_new_curve_GFp(p, a, b, NULL);\n } else {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_FIELD);\n goto err;\n }\n if (ret == NULL) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);\n goto err;\n }\n if (params->curve->seed != NULL) {\n OPENSSL_free(ret->seed);\n if ((ret->seed = OPENSSL_malloc(params->curve->seed->length)) == NULL) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n memcpy(ret->seed, params->curve->seed->data,\n params->curve->seed->length);\n ret->seed_len = params->curve->seed->length;\n }\n if (!params->order || !params->base || !params->base->data) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);\n goto err;\n }\n if ((point = EC_POINT_new(ret)) == NULL)\n goto err;\n EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t)\n (params->base->data[0] & ~0x01));\n if (!EC_POINT_oct2point(ret, point, params->base->data,\n params->base->length, NULL)) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);\n goto err;\n }\n if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);\n goto err;\n }\n if (BN_is_negative(a) || BN_is_zero(a)) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_GROUP_ORDER);\n goto err;\n }\n if (BN_num_bits(a) > (int)field_bits + 1) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_GROUP_ORDER);\n goto err;\n }\n if (params->cofactor == NULL) {\n BN_free(b);\n b = NULL;\n } else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);\n goto err;\n }\n if (!EC_GROUP_set_generator(ret, point, a, b)) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);\n goto err;\n }\n ok = 1;\n err:\n if (!ok) {\n EC_GROUP_clear_free(ret);\n ret = NULL;\n }\n BN_free(p);\n BN_free(a);\n BN_free(b);\n EC_POINT_free(point);\n return (ret);\n}', 'int BN_set_bit(BIGNUM *a, int n)\n{\n int i, j, k;\n if (n < 0)\n return 0;\n i = n / BN_BITS2;\n j = n % BN_BITS2;\n if (a->top <= i) {\n if (bn_wexpand(a, i + 1) == NULL)\n return (0);\n for (k = a->top; k < i + 1; k++)\n a->d[k] = 0;\n a->top = i + 1;\n }\n a->d[i] |= (((BN_ULONG)1) << j);\n bn_check_top(a);\n return (1);\n}']
|
1,069 | 0 |
https://github.com/nginx/nginx/blob/e4ecddfdb0d2ffc872658e36028971ad9a873726/src/core/ngx_string.c/#L244
|
u_char *
ngx_vsnprintf(u_char *buf, size_t max, const char *fmt, va_list args)
{
u_char *p, zero, *last;
int d;
float f, scale;
size_t len, slen;
int64_t i64;
uint64_t ui64;
ngx_msec_t ms;
ngx_uint_t width, sign, hex, max_width, frac_width, i;
ngx_str_t *v;
ngx_variable_value_t *vv;
if (max == 0) {
return buf;
}
last = buf + max;
while (*fmt && buf < last) {
if (*fmt == '%') {
i64 = 0;
ui64 = 0;
zero = (u_char) ((*++fmt == '0') ? '0' : ' ');
width = 0;
sign = 1;
hex = 0;
max_width = 0;
frac_width = 0;
slen = (size_t) -1;
while (*fmt >= '0' && *fmt <= '9') {
width = width * 10 + *fmt++ - '0';
}
for ( ;; ) {
switch (*fmt) {
case 'u':
sign = 0;
fmt++;
continue;
case 'm':
max_width = 1;
fmt++;
continue;
case 'X':
hex = 2;
sign = 0;
fmt++;
continue;
case 'x':
hex = 1;
sign = 0;
fmt++;
continue;
case '.':
fmt++;
while (*fmt >= '0' && *fmt <= '9') {
frac_width = frac_width * 10 + *fmt++ - '0';
}
break;
case '*':
slen = va_arg(args, size_t);
fmt++;
continue;
default:
break;
}
break;
}
switch (*fmt) {
case 'V':
v = va_arg(args, ngx_str_t *);
len = v->len;
len = (buf + len < last) ? len : (size_t) (last - buf);
buf = ngx_cpymem(buf, v->data, len);
fmt++;
continue;
case 'v':
vv = va_arg(args, ngx_variable_value_t *);
len = vv->len;
len = (buf + len < last) ? len : (size_t) (last - buf);
buf = ngx_cpymem(buf, vv->data, len);
fmt++;
continue;
case 's':
p = va_arg(args, u_char *);
if (slen == (size_t) -1) {
while (*p && buf < last) {
*buf++ = *p++;
}
} else {
len = (buf + slen < last) ? slen : (size_t) (last - buf);
buf = ngx_cpymem(buf, p, len);
}
fmt++;
continue;
case 'O':
i64 = (int64_t) va_arg(args, off_t);
sign = 1;
break;
case 'P':
i64 = (int64_t) va_arg(args, ngx_pid_t);
sign = 1;
break;
case 'T':
i64 = (int64_t) va_arg(args, time_t);
sign = 1;
break;
case 'M':
ms = (ngx_msec_t) va_arg(args, ngx_msec_t);
if ((ngx_msec_int_t) ms == -1) {
sign = 1;
i64 = -1;
} else {
sign = 0;
ui64 = (uint64_t) ms;
}
break;
case 'z':
if (sign) {
i64 = (int64_t) va_arg(args, ssize_t);
} else {
ui64 = (uint64_t) va_arg(args, size_t);
}
break;
case 'i':
if (sign) {
i64 = (int64_t) va_arg(args, ngx_int_t);
} else {
ui64 = (uint64_t) va_arg(args, ngx_uint_t);
}
if (max_width) {
width = NGX_INT_T_LEN;
}
break;
case 'd':
if (sign) {
i64 = (int64_t) va_arg(args, int);
} else {
ui64 = (uint64_t) va_arg(args, u_int);
}
break;
case 'l':
if (sign) {
i64 = (int64_t) va_arg(args, long);
} else {
ui64 = (uint64_t) va_arg(args, u_long);
}
break;
case 'D':
if (sign) {
i64 = (int64_t) va_arg(args, int32_t);
} else {
ui64 = (uint64_t) va_arg(args, uint32_t);
}
break;
case 'L':
if (sign) {
i64 = va_arg(args, int64_t);
} else {
ui64 = va_arg(args, uint64_t);
}
break;
case 'A':
if (sign) {
i64 = (int64_t) va_arg(args, ngx_atomic_int_t);
} else {
ui64 = (uint64_t) va_arg(args, ngx_atomic_uint_t);
}
if (max_width) {
width = NGX_ATOMIC_T_LEN;
}
break;
case 'f':
f = (float) va_arg(args, double);
if (f < 0) {
*buf++ = '-';
f = -f;
}
ui64 = (int64_t) f;
buf = ngx_sprintf_num(buf, last, ui64, zero, 0, width);
if (frac_width) {
if (buf < last) {
*buf++ = '.';
}
scale = 1.0;
for (i = 0; i < frac_width; i++) {
scale *= 10.0;
}
ui64 = (uint64_t) ((f - (int64_t) ui64) * scale);
buf = ngx_sprintf_num(buf, last, ui64, '0', 0, frac_width);
}
fmt++;
continue;
#if !(NGX_WIN32)
case 'r':
i64 = (int64_t) va_arg(args, rlim_t);
sign = 1;
break;
#endif
case 'p':
ui64 = (uintptr_t) va_arg(args, void *);
hex = 2;
sign = 0;
zero = '0';
width = NGX_PTR_SIZE * 2;
break;
case 'c':
d = va_arg(args, int);
*buf++ = (u_char) (d & 0xff);
fmt++;
continue;
case 'Z':
*buf++ = '\0';
fmt++;
continue;
case 'N':
#if (NGX_WIN32)
*buf++ = CR;
#endif
*buf++ = LF;
fmt++;
continue;
case '%':
*buf++ = '%';
fmt++;
continue;
default:
*buf++ = *fmt++;
continue;
}
if (sign) {
if (i64 < 0) {
*buf++ = '-';
ui64 = (uint64_t) -i64;
} else {
ui64 = (uint64_t) i64;
}
}
buf = ngx_sprintf_num(buf, last, ui64, zero, hex, width);
fmt++;
} else {
*buf++ = *fmt++;
}
}
return buf;
}
|
['static ngx_int_t\nngx_epoll_process_events(ngx_cycle_t *cycle, ngx_msec_t timer, ngx_uint_t flags)\n{\n int events;\n uint32_t revents;\n ngx_int_t instance, i;\n ngx_uint_t level;\n ngx_err_t err;\n ngx_log_t *log;\n ngx_event_t *rev, *wev, **queue;\n ngx_connection_t *c;\n ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,\n "epoll timer: %M", timer);\n events = epoll_wait(ep, event_list, (int) nevents, timer);\n if (events == -1) {\n err = ngx_errno;\n } else {\n err = 0;\n }\n if (flags & NGX_UPDATE_TIME) {\n ngx_time_update(0, 0);\n }\n if (err) {\n if (err == NGX_EINTR) {\n if (ngx_event_timer_alarm) {\n ngx_event_timer_alarm = 0;\n return NGX_OK;\n }\n level = NGX_LOG_INFO;\n } else {\n level = NGX_LOG_ALERT;\n }\n ngx_log_error(level, cycle->log, err, "epoll_wait() failed");\n return NGX_ERROR;\n }\n if (events == 0) {\n if (timer != NGX_TIMER_INFINITE) {\n return NGX_OK;\n }\n ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,\n "epoll_wait() returned no events without timeout");\n return NGX_ERROR;\n }\n ngx_mutex_lock(ngx_posted_events_mutex);\n log = cycle->log;\n for (i = 0; i < events; i++) {\n c = event_list[i].data.ptr;\n instance = (uintptr_t) c & 1;\n c = (ngx_connection_t *) ((uintptr_t) c & (uintptr_t) ~1);\n rev = c->read;\n if (c->fd == -1 || rev->instance != instance) {\n ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,\n "epoll: stale event %p", c);\n continue;\n }\n#if (NGX_DEBUG0)\n log = c->log ? c->log : cycle->log;\n#endif\n revents = event_list[i].events;\n ngx_log_debug3(NGX_LOG_DEBUG_EVENT, log, 0,\n "epoll: fd:%d ev:%04XD d:%p",\n c->fd, revents, event_list[i].data.ptr);\n if (revents & (EPOLLERR|EPOLLHUP)) {\n ngx_log_debug2(NGX_LOG_DEBUG_EVENT, log, 0,\n "epoll_wait() error on fd:%d ev:%04XD",\n c->fd, revents);\n }\n#if 0\n if (revents & ~(EPOLLIN|EPOLLOUT|EPOLLERR|EPOLLHUP)) {\n ngx_log_error(NGX_LOG_ALERT, log, 0,\n "strange epoll_wait() events fd:%d ev:%04XD",\n c->fd, revents);\n }\n#endif\n if ((revents & (EPOLLERR|EPOLLHUP))\n && (revents & (EPOLLIN|EPOLLOUT)) == 0)\n {\n revents |= EPOLLIN|EPOLLOUT;\n }\n if ((revents & EPOLLIN) && rev->active) {\n if ((flags & NGX_POST_THREAD_EVENTS) && !rev->accept) {\n rev->posted_ready = 1;\n } else {\n rev->ready = 1;\n }\n if (flags & NGX_POST_EVENTS) {\n queue = (ngx_event_t **) (rev->accept ?\n &ngx_posted_accept_events : &ngx_posted_events);\n ngx_locked_post_event(rev, queue);\n } else {\n rev->handler(rev);\n }\n }\n wev = c->write;\n if ((revents & EPOLLOUT) && wev->active) {\n if (flags & NGX_POST_THREAD_EVENTS) {\n wev->posted_ready = 1;\n } else {\n wev->ready = 1;\n }\n if (flags & NGX_POST_EVENTS) {\n ngx_locked_post_event(wev, &ngx_posted_events);\n } else {\n wev->handler(wev);\n }\n }\n }\n ngx_mutex_unlock(ngx_posted_events_mutex);\n return NGX_OK;\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}"]
|
1,070 | 0 |
https://github.com/openssl/openssl/blob/0bde1089f895718db2fe2637fda4a0c2ed6df904/crypto/lhash/lhash.c/#L356
|
static void contract(LHASH *lh)
{
LHASH_NODE **n,*n1,*np;
np=lh->b[lh->p+lh->pmax-1];
lh->b[lh->p+lh->pmax-1]=NULL;
if (lh->p == 0)
{
n=(LHASH_NODE **)Realloc(lh->b,
(unsigned int)(sizeof(LHASH_NODE *)*lh->pmax));
if (n == NULL)
{
lh->error++;
return;
}
lh->num_contract_reallocs++;
lh->num_alloc_nodes/=2;
lh->pmax/=2;
lh->p=lh->pmax-1;
lh->b=n;
}
else
lh->p--;
lh->num_nodes--;
lh->num_contracts++;
n1=lh->b[(int)lh->p];
if (n1 == NULL)
lh->b[(int)lh->p]=np;
else
{
while (n1->next != NULL)
n1=n1->next;
n1->next=np;
}
}
|
['void ERR_remove_state(unsigned long pid)\n\t{\n\tERR_STATE *p,tmp;\n\tif (thread_hash == NULL)\n\t\treturn;\n\tif (pid == 0)\n\t\tpid=(unsigned long)CRYPTO_thread_id();\n\ttmp.pid=pid;\n\tCRYPTO_w_lock(CRYPTO_LOCK_ERR);\n\tp=(ERR_STATE *)lh_delete(thread_hash,&tmp);\n\tCRYPTO_w_unlock(CRYPTO_LOCK_ERR);\n\tif (p != NULL) ERR_STATE_free(p);\n\t}', 'void *lh_delete(LHASH *lh, void *data)\n\t{\n\tunsigned long hash;\n\tLHASH_NODE *nn,**rn;\n\tvoid *ret;\n\tlh->error=0;\n\trn=getrn(lh,data,&hash);\n\tif (*rn == NULL)\n\t\t{\n\t\tlh->num_no_delete++;\n\t\treturn(NULL);\n\t\t}\n\telse\n\t\t{\n\t\tnn= *rn;\n\t\t*rn=nn->next;\n\t\tret=nn->data;\n\t\tFree(nn);\n\t\tlh->num_delete++;\n\t\t}\n\tlh->num_items--;\n\tif ((lh->num_nodes > MIN_NODES) &&\n\t\t(lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes)))\n\t\tcontract(lh);\n\treturn(ret);\n\t}', 'static void contract(LHASH *lh)\n\t{\n\tLHASH_NODE **n,*n1,*np;\n\tnp=lh->b[lh->p+lh->pmax-1];\n\tlh->b[lh->p+lh->pmax-1]=NULL;\n\tif (lh->p == 0)\n\t\t{\n\t\tn=(LHASH_NODE **)Realloc(lh->b,\n\t\t\t(unsigned int)(sizeof(LHASH_NODE *)*lh->pmax));\n\t\tif (n == NULL)\n\t\t\t{\n\t\t\tlh->error++;\n\t\t\treturn;\n\t\t\t}\n\t\tlh->num_contract_reallocs++;\n\t\tlh->num_alloc_nodes/=2;\n\t\tlh->pmax/=2;\n\t\tlh->p=lh->pmax-1;\n\t\tlh->b=n;\n\t\t}\n\telse\n\t\tlh->p--;\n\tlh->num_nodes--;\n\tlh->num_contracts++;\n\tn1=lh->b[(int)lh->p];\n\tif (n1 == NULL)\n\t\tlh->b[(int)lh->p]=np;\n\telse\n\t\t{\n\t\twhile (n1->next != NULL)\n\t\t\tn1=n1->next;\n\t\tn1->next=np;\n\t\t}\n\t}']
|
1,071 | 0 |
https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/bmp.c/#L122
|
static int bmp_decode_frame(AVCodecContext *avctx,
void *data, int *data_size,
const uint8_t *buf, int buf_size)
{
BMPContext *s = avctx->priv_data;
AVFrame *picture = data;
AVFrame *p = &s->picture;
unsigned int fsize, hsize;
int width, height;
unsigned int depth;
BiCompression comp;
unsigned int ihsize;
int i, j, n, linesize;
uint32_t rgb[3];
uint8_t *ptr;
int dsize;
const uint8_t *buf0 = buf;
if(buf_size < 14){
av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size);
return -1;
}
if(bytestream_get_byte(&buf) != 'B' ||
bytestream_get_byte(&buf) != 'M') {
av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
return -1;
}
fsize = bytestream_get_le32(&buf);
if(buf_size < fsize){
av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
buf_size, fsize);
return -1;
}
buf += 2;
buf += 2;
hsize = bytestream_get_le32(&buf);
if(fsize <= hsize){
av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
fsize, hsize);
return -1;
}
ihsize = bytestream_get_le32(&buf);
if(ihsize + 14 > hsize){
av_log(avctx, AV_LOG_ERROR, "invalid header size %d\n", hsize);
return -1;
}
width = bytestream_get_le32(&buf);
height = bytestream_get_le32(&buf);
if(bytestream_get_le16(&buf) != 1){
av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n");
return -1;
}
depth = bytestream_get_le16(&buf);
if(ihsize > 16)
comp = bytestream_get_le32(&buf);
else
comp = BMP_RGB;
if(comp != BMP_RGB && comp != BMP_BITFIELDS){
av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp);
return -1;
}
if(comp == BMP_BITFIELDS){
buf += 20;
rgb[0] = bytestream_get_le32(&buf);
rgb[1] = bytestream_get_le32(&buf);
rgb[2] = bytestream_get_le32(&buf);
}
avctx->width = width;
avctx->height = height > 0? height: -height;
avctx->pix_fmt = PIX_FMT_NONE;
switch(depth){
case 32:
if(comp == BMP_BITFIELDS){
rgb[0] = (rgb[0] >> 15) & 3;
rgb[1] = (rgb[1] >> 15) & 3;
rgb[2] = (rgb[2] >> 15) & 3;
if(rgb[0] + rgb[1] + rgb[2] != 3 ||
rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]){
break;
}
} else {
rgb[0] = 2;
rgb[1] = 1;
rgb[2] = 0;
}
avctx->pix_fmt = PIX_FMT_BGR24;
break;
case 24:
avctx->pix_fmt = PIX_FMT_BGR24;
break;
case 16:
if(comp == BMP_RGB)
avctx->pix_fmt = PIX_FMT_RGB555;
break;
default:
av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth);
return -1;
}
if(avctx->pix_fmt == PIX_FMT_NONE){
av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
return -1;
}
if(p->data[0])
avctx->release_buffer(avctx, p);
p->reference = 0;
if(avctx->get_buffer(avctx, p) < 0){
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
return -1;
}
p->pict_type = FF_I_TYPE;
p->key_frame = 1;
buf = buf0 + hsize;
dsize = buf_size - hsize;
n = (avctx->width * (depth / 8) + 3) & ~3;
if(n * avctx->height > dsize){
av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n",
dsize, n * avctx->height);
return -1;
}
if(height > 0){
ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];
linesize = -p->linesize[0];
} else {
ptr = p->data[0];
linesize = p->linesize[0];
}
switch(depth){
case 24:
for(i = 0; i < avctx->height; i++){
memcpy(ptr, buf, avctx->width*(depth>>3));
buf += n;
ptr += linesize;
}
break;
case 16:
for(i = 0; i < avctx->height; i++){
const uint16_t *src = (const uint16_t *) buf;
uint16_t *dst = (uint16_t *) ptr;
for(j = 0; j < avctx->width; j++)
*dst++ = le2me_16(*src++);
buf += n;
ptr += linesize;
}
break;
case 32:
for(i = 0; i < avctx->height; i++){
const uint8_t *src = buf;
uint8_t *dst = ptr;
for(j = 0; j < avctx->width; j++){
dst[0] = src[rgb[2]];
dst[1] = src[rgb[1]];
dst[2] = src[rgb[0]];
dst += 3;
src += 4;
}
buf += n;
ptr += linesize;
}
break;
default:
av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n");
return -1;
}
*picture = s->picture;
*data_size = sizeof(AVPicture);
return buf_size;
}
|
['static int bmp_decode_frame(AVCodecContext *avctx,\n void *data, int *data_size,\n const uint8_t *buf, int buf_size)\n{\n BMPContext *s = avctx->priv_data;\n AVFrame *picture = data;\n AVFrame *p = &s->picture;\n unsigned int fsize, hsize;\n int width, height;\n unsigned int depth;\n BiCompression comp;\n unsigned int ihsize;\n int i, j, n, linesize;\n uint32_t rgb[3];\n uint8_t *ptr;\n int dsize;\n const uint8_t *buf0 = buf;\n if(buf_size < 14){\n av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\\n", buf_size);\n return -1;\n }\n if(bytestream_get_byte(&buf) != \'B\' ||\n bytestream_get_byte(&buf) != \'M\') {\n av_log(avctx, AV_LOG_ERROR, "bad magic number\\n");\n return -1;\n }\n fsize = bytestream_get_le32(&buf);\n if(buf_size < fsize){\n av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\\n",\n buf_size, fsize);\n return -1;\n }\n buf += 2;\n buf += 2;\n hsize = bytestream_get_le32(&buf);\n if(fsize <= hsize){\n av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\\n",\n fsize, hsize);\n return -1;\n }\n ihsize = bytestream_get_le32(&buf);\n if(ihsize + 14 > hsize){\n av_log(avctx, AV_LOG_ERROR, "invalid header size %d\\n", hsize);\n return -1;\n }\n width = bytestream_get_le32(&buf);\n height = bytestream_get_le32(&buf);\n if(bytestream_get_le16(&buf) != 1){\n av_log(avctx, AV_LOG_ERROR, "invalid BMP header\\n");\n return -1;\n }\n depth = bytestream_get_le16(&buf);\n if(ihsize > 16)\n comp = bytestream_get_le32(&buf);\n else\n comp = BMP_RGB;\n if(comp != BMP_RGB && comp != BMP_BITFIELDS){\n av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\\n", comp);\n return -1;\n }\n if(comp == BMP_BITFIELDS){\n buf += 20;\n rgb[0] = bytestream_get_le32(&buf);\n rgb[1] = bytestream_get_le32(&buf);\n rgb[2] = bytestream_get_le32(&buf);\n }\n avctx->width = width;\n avctx->height = height > 0? height: -height;\n avctx->pix_fmt = PIX_FMT_NONE;\n switch(depth){\n case 32:\n if(comp == BMP_BITFIELDS){\n rgb[0] = (rgb[0] >> 15) & 3;\n rgb[1] = (rgb[1] >> 15) & 3;\n rgb[2] = (rgb[2] >> 15) & 3;\n if(rgb[0] + rgb[1] + rgb[2] != 3 ||\n rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]){\n break;\n }\n } else {\n rgb[0] = 2;\n rgb[1] = 1;\n rgb[2] = 0;\n }\n avctx->pix_fmt = PIX_FMT_BGR24;\n break;\n case 24:\n avctx->pix_fmt = PIX_FMT_BGR24;\n break;\n case 16:\n if(comp == BMP_RGB)\n avctx->pix_fmt = PIX_FMT_RGB555;\n break;\n default:\n av_log(avctx, AV_LOG_ERROR, "depth %d not supported\\n", depth);\n return -1;\n }\n if(avctx->pix_fmt == PIX_FMT_NONE){\n av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\\n");\n return -1;\n }\n if(p->data[0])\n avctx->release_buffer(avctx, p);\n p->reference = 0;\n if(avctx->get_buffer(avctx, p) < 0){\n av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n return -1;\n }\n p->pict_type = FF_I_TYPE;\n p->key_frame = 1;\n buf = buf0 + hsize;\n dsize = buf_size - hsize;\n n = (avctx->width * (depth / 8) + 3) & ~3;\n if(n * avctx->height > dsize){\n av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\\n",\n dsize, n * avctx->height);\n return -1;\n }\n if(height > 0){\n ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];\n linesize = -p->linesize[0];\n } else {\n ptr = p->data[0];\n linesize = p->linesize[0];\n }\n switch(depth){\n case 24:\n for(i = 0; i < avctx->height; i++){\n memcpy(ptr, buf, avctx->width*(depth>>3));\n buf += n;\n ptr += linesize;\n }\n break;\n case 16:\n for(i = 0; i < avctx->height; i++){\n const uint16_t *src = (const uint16_t *) buf;\n uint16_t *dst = (uint16_t *) ptr;\n for(j = 0; j < avctx->width; j++)\n *dst++ = le2me_16(*src++);\n buf += n;\n ptr += linesize;\n }\n break;\n case 32:\n for(i = 0; i < avctx->height; i++){\n const uint8_t *src = buf;\n uint8_t *dst = ptr;\n for(j = 0; j < avctx->width; j++){\n dst[0] = src[rgb[2]];\n dst[1] = src[rgb[1]];\n dst[2] = src[rgb[0]];\n dst += 3;\n src += 4;\n }\n buf += n;\n ptr += linesize;\n }\n break;\n default:\n av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\\n");\n return -1;\n }\n *picture = s->picture;\n *data_size = sizeof(AVPicture);\n return buf_size;\n}']
|
1,072 | 0 |
https://github.com/openssl/openssl/blob/c10d1bc81cb047cbd53f8cc430632b6a4a70252d/crypto/x509v3/pcy_tree.c/#L367
|
static int tree_add_unmatched(X509_POLICY_LEVEL *curr,
const X509_POLICY_CACHE *cache,
const ASN1_OBJECT *id,
X509_POLICY_NODE *node, X509_POLICY_TREE *tree)
{
X509_POLICY_DATA *data;
if (id == NULL)
id = node->data->valid_policy;
data = policy_data_new(NULL, id, node_critical(node));
if (data == NULL)
return 0;
data->qualifier_set = cache->anyPolicy->qualifier_set;
data->flags |= POLICY_DATA_FLAG_SHARED_QUALIFIERS;
if (!level_add_node(curr, data, node, tree)) {
policy_data_free(data);
return 0;
}
return 1;
}
|
['static int tree_add_unmatched(X509_POLICY_LEVEL *curr,\n const X509_POLICY_CACHE *cache,\n const ASN1_OBJECT *id,\n X509_POLICY_NODE *node, X509_POLICY_TREE *tree)\n{\n X509_POLICY_DATA *data;\n if (id == NULL)\n id = node->data->valid_policy;\n data = policy_data_new(NULL, id, node_critical(node));\n if (data == NULL)\n return 0;\n data->qualifier_set = cache->anyPolicy->qualifier_set;\n data->flags |= POLICY_DATA_FLAG_SHARED_QUALIFIERS;\n if (!level_add_node(curr, data, node, tree)) {\n policy_data_free(data);\n return 0;\n }\n return 1;\n}', 'X509_POLICY_DATA *policy_data_new(POLICYINFO *policy,\n const ASN1_OBJECT *cid, int crit)\n{\n X509_POLICY_DATA *ret;\n ASN1_OBJECT *id;\n if (!policy && !cid)\n return NULL;\n if (cid) {\n id = OBJ_dup(cid);\n if (!id)\n return NULL;\n } else\n id = NULL;\n ret = OPENSSL_zalloc(sizeof(*ret));\n if (ret == NULL)\n return NULL;\n ret->expected_policy_set = sk_ASN1_OBJECT_new_null();\n if (ret->expected_policy_set == NULL) {\n OPENSSL_free(ret);\n ASN1_OBJECT_free(id);\n return NULL;\n }\n if (crit)\n ret->flags = POLICY_DATA_FLAG_CRITICAL;\n if (id)\n ret->valid_policy = id;\n else {\n ret->valid_policy = policy->policyid;\n policy->policyid = NULL;\n }\n if (policy) {\n ret->qualifier_set = policy->qualifiers;\n policy->qualifiers = NULL;\n }\n return ret;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (num <= 0)\n return NULL;\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)file;\n (void)line;\n ret = malloc(num);\n#endif\n#ifndef OPENSSL_CPUID_OBJ\n if (ret && (num > 2048)) {\n extern unsigned char cleanse_ctr;\n ((unsigned char *)ret)[0] = cleanse_ctr;\n }\n#endif\n return ret;\n}', 'DEFINE_STACK_OF(ASN1_OBJECT)', '_STACK *sk_new_null(void)\n{\n return sk_new((int (*)(const void *, const void *))0);\n}', '_STACK *sk_new(int (*c) (const void *, const void *))\n{\n _STACK *ret;\n if ((ret = OPENSSL_zalloc(sizeof(_STACK))) == NULL)\n goto err;\n if ((ret->data = OPENSSL_zalloc(sizeof(*ret->data) * MIN_NODES)) == NULL)\n goto err;\n ret->comp = c;\n ret->num_alloc = MIN_NODES;\n return (ret);\n err:\n OPENSSL_free(ret);\n return (NULL);\n}']
|
1,073 | 0 |
https://github.com/libav/libav/blob/a893655bdaa726a82424367b6456d195be12ebbc/libavcodec/ffv1.h/#L150
|
static inline int get_context(PlaneContext *p, int16_t *src,
int16_t *last, int16_t *last2)
{
const int LT = last[-1];
const int T = last[0];
const int RT = last[1];
const int L = src[-1];
if (p->quant_table[3][127]) {
const int TT = last2[0];
const int LL = src[-2];
return p->quant_table[0][(L - LT) & 0xFF] +
p->quant_table[1][(LT - T) & 0xFF] +
p->quant_table[2][(T - RT) & 0xFF] +
p->quant_table[3][(LL - L) & 0xFF] +
p->quant_table[4][(TT - T) & 0xFF];
} else
return p->quant_table[0][(L - LT) & 0xFF] +
p->quant_table[1][(LT - T) & 0xFF] +
p->quant_table[2][(T - RT) & 0xFF];
}
|
['static int decode_slice(AVCodecContext *c, void *arg)\n{\n FFV1Context *fs = *(void **)arg;\n FFV1Context *f = fs->avctx->priv_data;\n int width, height, x, y, ret;\n const int ps = (av_pix_fmt_desc_get(c->pix_fmt)->flags & PIX_FMT_PLANAR)\n ? (c->bits_per_raw_sample > 8) + 1\n : 4;\n AVFrame *const p = &f->picture;\n if (f->version > 2) {\n if (decode_slice_header(f, fs) < 0) {\n fs->slice_damaged = 1;\n return AVERROR_INVALIDDATA;\n }\n }\n if ((ret = ffv1_init_slice_state(f, fs)) < 0)\n return ret;\n if (f->picture.key_frame)\n ffv1_clear_slice_state(f, fs);\n width = fs->slice_width;\n height = fs->slice_height;\n x = fs->slice_x;\n y = fs->slice_y;\n if (!fs->ac) {\n if (f->version == 3 && f->minor_version > 1 || f->version > 3)\n get_rac(&fs->c, (uint8_t[]) { 129 });\n fs->ac_byte_count = f->version > 2 || (!x && !y) ? fs->c.bytestream - fs->c.bytestream_start - 1 : 0;\n init_get_bits(&fs->gb, fs->c.bytestream_start + fs->ac_byte_count,\n (fs->c.bytestream_end - fs->c.bytestream_start -\n fs->ac_byte_count) * 8);\n }\n av_assert1(width && height);\n if (f->colorspace == 0) {\n const int chroma_width = -((-width) >> f->chroma_h_shift);\n const int chroma_height = -((-height) >> f->chroma_v_shift);\n const int cx = x >> f->chroma_h_shift;\n const int cy = y >> f->chroma_v_shift;\n decode_plane(fs, p->data[0] + ps * x + y * p->linesize[0], width,\n height, p->linesize[0],\n 0);\n if (f->chroma_planes) {\n decode_plane(fs, p->data[1] + ps * cx + cy * p->linesize[1],\n chroma_width, chroma_height, p->linesize[1],\n 1);\n decode_plane(fs, p->data[2] + ps * cx + cy * p->linesize[2],\n chroma_width, chroma_height, p->linesize[2],\n 1);\n }\n if (fs->transparency)\n decode_plane(fs, p->data[3] + ps * x + y * p->linesize[3], width,\n height, p->linesize[3],\n 2);\n } else {\n uint8_t *planes[3] = { p->data[0] + ps * x + y * p->linesize[0],\n p->data[1] + ps * x + y * p->linesize[1],\n p->data[2] + ps * x + y * p->linesize[2] };\n decode_rgb_frame(fs, planes, width, height, p->linesize);\n }\n if (fs->ac && f->version > 2) {\n int v;\n get_rac(&fs->c, (uint8_t[]) { 129 });\n v = fs->c.bytestream_end - fs->c.bytestream - 2 - 5 * f->ec;\n if (v) {\n av_log(f->avctx, AV_LOG_ERROR, "bytestream end mismatching by %d\\n",\n v);\n fs->slice_damaged = 1;\n }\n }\n emms_c();\n return 0;\n}', 'void ffv1_clear_slice_state(FFV1Context *f, FFV1Context *fs)\n{\n int i, j;\n for (i = 0; i < f->plane_count; i++) {\n PlaneContext *p = &fs->plane[i];\n p->interlace_bit_state[0] = 128;\n p->interlace_bit_state[1] = 128;\n if (fs->ac) {\n if (f->initial_states[p->quant_table_index]) {\n memcpy(p->state, f->initial_states[p->quant_table_index],\n CONTEXT_SIZE * p->context_count);\n } else\n memset(p->state, 128, CONTEXT_SIZE * p->context_count);\n } else {\n for (j = 0; j < p->context_count; j++) {\n p->vlc_state[j].drift = 0;\n p->vlc_state[j].error_sum = 4;\n p->vlc_state[j].bias = 0;\n p->vlc_state[j].count = 1;\n }\n }\n }\n}', 'static void decode_rgb_frame(FFV1Context *s, uint8_t *src[3], int w, int h,\n int stride[3])\n{\n int x, y, p;\n int16_t *sample[4][2];\n int lbd = s->avctx->bits_per_raw_sample <= 8;\n int bits = s->avctx->bits_per_raw_sample > 0\n ? s->avctx->bits_per_raw_sample\n : 8;\n int offset = 1 << bits;\n for (x = 0; x < 4; x++) {\n sample[x][0] = s->sample_buffer + x * 2 * (w + 6) + 3;\n sample[x][1] = s->sample_buffer + (x * 2 + 1) * (w + 6) + 3;\n }\n s->run_index = 0;\n memset(s->sample_buffer, 0, 8 * (w + 6) * sizeof(*s->sample_buffer));\n for (y = 0; y < h; y++) {\n for (p = 0; p < 3 + s->transparency; p++) {\n int16_t *temp = sample[p][0];\n sample[p][0] = sample[p][1];\n sample[p][1] = temp;\n sample[p][1][-1] = sample[p][0][0];\n sample[p][0][w] = sample[p][0][w - 1];\n if (lbd)\n decode_line(s, w, sample[p], (p + 1) / 2, 9);\n else\n decode_line(s, w, sample[p], (p + 1) / 2, bits + 1);\n }\n for (x = 0; x < w; x++) {\n int g = sample[0][1][x];\n int b = sample[1][1][x];\n int r = sample[2][1][x];\n int a = sample[3][1][x];\n b -= offset;\n r -= offset;\n g -= (b + r) >> 2;\n b += g;\n r += g;\n if (lbd)\n *((uint32_t *)(src[0] + x * 4 + stride[0] * y)) = b +\n (g << 8) + (r << 16) + (a << 24);\n else {\n *((uint16_t *)(src[0] + x * 2 + stride[0] * y)) = b;\n *((uint16_t *)(src[1] + x * 2 + stride[1] * y)) = g;\n *((uint16_t *)(src[2] + x * 2 + stride[2] * y)) = r;\n }\n }\n }\n}', 'static av_always_inline void decode_line(FFV1Context *s, int w,\n int16_t *sample[2],\n int plane_index, int bits)\n{\n PlaneContext *const p = &s->plane[plane_index];\n RangeCoder *const c = &s->c;\n int x;\n int run_count = 0;\n int run_mode = 0;\n int run_index = s->run_index;\n for (x = 0; x < w; x++) {\n int diff, context, sign;\n context = get_context(p, sample[1] + x, sample[0] + x, sample[1] + x);\n if (context < 0) {\n context = -context;\n sign = 1;\n } else\n sign = 0;\n av_assert2(context < p->context_count);\n if (s->ac) {\n diff = get_symbol_inline(c, p->state[context], 1);\n } else {\n if (context == 0 && run_mode == 0)\n run_mode = 1;\n if (run_mode) {\n if (run_count == 0 && run_mode == 1) {\n if (get_bits1(&s->gb)) {\n run_count = 1 << ff_log2_run[run_index];\n if (x + run_count <= w)\n run_index++;\n } else {\n if (ff_log2_run[run_index])\n run_count = get_bits(&s->gb, ff_log2_run[run_index]);\n else\n run_count = 0;\n if (run_index)\n run_index--;\n run_mode = 2;\n }\n }\n run_count--;\n if (run_count < 0) {\n run_mode = 0;\n run_count = 0;\n diff = get_vlc_symbol(&s->gb, &p->vlc_state[context],\n bits);\n if (diff >= 0)\n diff++;\n } else\n diff = 0;\n } else\n diff = get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);\n av_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\\n",\n run_count, run_index, run_mode, x, get_bits_count(&s->gb));\n }\n if (sign)\n diff = -diff;\n sample[1][x] = (predict(sample[1] + x, sample[0] + x) + diff) &\n ((1 << bits) - 1);\n }\n s->run_index = run_index;\n}', 'static inline int get_context(PlaneContext *p, int16_t *src,\n int16_t *last, int16_t *last2)\n{\n const int LT = last[-1];\n const int T = last[0];\n const int RT = last[1];\n const int L = src[-1];\n if (p->quant_table[3][127]) {\n const int TT = last2[0];\n const int LL = src[-2];\n return p->quant_table[0][(L - LT) & 0xFF] +\n p->quant_table[1][(LT - T) & 0xFF] +\n p->quant_table[2][(T - RT) & 0xFF] +\n p->quant_table[3][(LL - L) & 0xFF] +\n p->quant_table[4][(TT - T) & 0xFF];\n } else\n return p->quant_table[0][(L - LT) & 0xFF] +\n p->quant_table[1][(LT - T) & 0xFF] +\n p->quant_table[2][(T - RT) & 0xFF];\n}']
|
1,074 | 0 |
https://github.com/openssl/openssl/blob/972c87dfc7e765bd28a4964519c362f0d3a58ca4/crypto/bn/bn_lib.c/#L233
|
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
BN_ULONG *a = NULL;
bn_check_top(b);
if (words > (INT_MAX / (4 * BN_BITS2))) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
return NULL;
}
if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
return NULL;
}
if (BN_get_flags(b, BN_FLG_SECURE))
a = OPENSSL_secure_zalloc(words * sizeof(*a));
else
a = OPENSSL_zalloc(words * sizeof(*a));
if (a == NULL) {
BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
return NULL;
}
assert(b->top <= words);
if (b->top > 0)
memcpy(a, b->d, sizeof(*a) * b->top);
return a;
}
|
['int SRP_create_verifier_BN(const char *user, const char *pass, BIGNUM **salt,\n BIGNUM **verifier, const BIGNUM *N,\n const BIGNUM *g)\n{\n int result = 0;\n BIGNUM *x = NULL;\n BN_CTX *bn_ctx = BN_CTX_new();\n unsigned char tmp2[MAX_LEN];\n BIGNUM *salttmp = NULL;\n if ((user == NULL) ||\n (pass == NULL) ||\n (salt == NULL) ||\n (verifier == NULL) || (N == NULL) || (g == NULL) || (bn_ctx == NULL))\n goto err;\n if (*salt == NULL) {\n if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)\n goto err;\n salttmp = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);\n } else {\n salttmp = *salt;\n }\n x = SRP_Calc_x(salttmp, user, pass);\n *verifier = BN_new();\n if (*verifier == NULL)\n goto err;\n if (!BN_mod_exp(*verifier, g, x, N, bn_ctx)) {\n BN_clear_free(*verifier);\n goto err;\n }\n result = 1;\n *salt = salttmp;\n err:\n if (salt != NULL && *salt != salttmp)\n BN_clear_free(salttmp);\n BN_clear_free(x);\n BN_CTX_free(bn_ctx);\n return result;\n}', '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}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n BN_CTX *ctx)\n{\n int ret;\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n if (BN_is_odd(m)) {\n# ifdef MONT_EXP_WORD\n if (a->top == 1 && !a->neg\n && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(a, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(m, BN_FLG_CONSTTIME) == 0)) {\n BN_ULONG A = a->d[0];\n ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);\n } else\n# endif\n ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);\n } else\n#endif\n#ifdef RECP_MUL_MOD\n {\n ret = BN_mod_exp_recp(r, a, p, m, ctx);\n }\n#else\n {\n ret = BN_mod_exp_simple(r, a, p, m, ctx);\n }\n#endif\n bn_check_top(r);\n return ret;\n}', 'int BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n BN_MONT_CTX *mont = NULL;\n int b, bits, ret = 0;\n int r_is_one;\n BN_ULONG w, next_w;\n BIGNUM *r, *t;\n BIGNUM *swap_tmp;\n#define BN_MOD_MUL_WORD(r, w, m) \\\n (BN_mul_word(r, (w)) && \\\n ( \\\n (BN_mod(t, r, m, ctx) && (swap_tmp = r, r = t, t = swap_tmp, 1))))\n#define BN_TO_MONTGOMERY_WORD(r, w, mont) \\\n (BN_set_word(r, (w)) && BN_to_montgomery(r, r, (mont), ctx))\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n BNerr(BN_F_BN_MOD_EXP_MONT_WORD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT_WORD, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n if (m->top == 1)\n a %= m->d[0];\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_is_one(m)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n if (a == 0) {\n BN_zero(rr);\n ret = 1;\n return ret;\n }\n BN_CTX_start(ctx);\n r = BN_CTX_get(ctx);\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n r_is_one = 1;\n w = a;\n for (b = bits - 2; b >= 0; b--) {\n next_w = w * w;\n if ((next_w / w) != w) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n next_w = 1;\n }\n w = next_w;\n if (!r_is_one) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (BN_is_bit_set(p, b)) {\n next_w = w * a;\n if ((next_w / a) != w) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n next_w = a;\n }\n w = next_w;\n }\n }\n if (w != 1) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n }\n if (r_is_one) {\n if (!BN_one(rr))\n goto err;\n } else {\n if (!BN_from_montgomery(rr, r, mont, ctx))\n goto err;\n }\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n BN_CTX_end(ctx);\n bn_check_top(rr);\n return ret;\n}', 'int BN_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_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}']
|
1,075 | 0 |
https://github.com/libav/libav/blob/d3789eeeed3423bd1ca9dc40030a2f7a21ea5332/libavformat/smush.c/#L170
|
static int smush_read_header(AVFormatContext *ctx)
{
SMUSHContext *smush = ctx->priv_data;
AVIOContext *pb = ctx->pb;
AVStream *vst, *ast;
uint32_t magic, nframes, size, subversion, i;
uint32_t width = 0, height = 0, got_audio = 0, read = 0;
uint32_t sample_rate, channels, palette[256];
magic = avio_rb32(pb);
avio_skip(pb, 4);
if (magic == MKBETAG('A', 'N', 'I', 'M')) {
if (avio_rb32(pb) != MKBETAG('A', 'H', 'D', 'R'))
return AVERROR_INVALIDDATA;
size = avio_rb32(pb);
if (size < 3 * 256 + 6)
return AVERROR_INVALIDDATA;
smush->version = 0;
subversion = avio_rl16(pb);
nframes = avio_rl16(pb);
if (!nframes)
return AVERROR_INVALIDDATA;
avio_skip(pb, 2);
for (i = 0; i < 256; i++)
palette[i] = avio_rb24(pb);
avio_skip(pb, size - (3 * 256 + 6));
} else if (magic == MKBETAG('S', 'A', 'N', 'M')) {
if (avio_rb32(pb) != MKBETAG('S', 'H', 'D', 'R'))
return AVERROR_INVALIDDATA;
size = avio_rb32(pb);
if (size < 14)
return AVERROR_INVALIDDATA;
smush->version = 1;
subversion = avio_rl16(pb);
nframes = avio_rl32(pb);
if (!nframes)
return AVERROR_INVALIDDATA;
avio_skip(pb, 2);
width = avio_rl16(pb);
height = avio_rl16(pb);
avio_skip(pb, 2);
avio_skip(pb, size - 14);
if (avio_rb32(pb) != MKBETAG('F', 'L', 'H', 'D'))
return AVERROR_INVALIDDATA;
size = avio_rb32(pb);
while (!got_audio && ((read + 8) < size)) {
uint32_t sig, chunk_size;
if (pb->eof_reached)
return AVERROR_EOF;
sig = avio_rb32(pb);
chunk_size = avio_rb32(pb);
read += 8;
switch (sig) {
case MKBETAG('W', 'a', 'v', 'e'):
got_audio = 1;
sample_rate = avio_rl32(pb);
if (!sample_rate)
return AVERROR_INVALIDDATA;
channels = avio_rl32(pb);
if (!channels)
return AVERROR_INVALIDDATA;
avio_skip(pb, chunk_size - 8);
read += chunk_size;
break;
case MKBETAG('B', 'l', '1', '6'):
case MKBETAG('A', 'N', 'N', 'O'):
avio_skip(pb, chunk_size);
read += chunk_size;
break;
default:
return AVERROR_INVALIDDATA;
break;
}
}
avio_skip(pb, size - read);
} else {
av_log(ctx, AV_LOG_ERROR, "Wrong magic\n");
return AVERROR_INVALIDDATA;
}
vst = avformat_new_stream(ctx, 0);
if (!vst)
return AVERROR(ENOMEM);
smush->video_stream_index = vst->index;
avpriv_set_pts_info(vst, 64, 1, 15);
vst->start_time = 0;
vst->duration =
vst->nb_frames = nframes;
vst->avg_frame_rate = av_inv_q(vst->time_base);
vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
vst->codec->codec_id = AV_CODEC_ID_SANM;
vst->codec->codec_tag = 0;
vst->codec->width = width;
vst->codec->height = height;
if (!smush->version) {
av_free(vst->codec->extradata);
vst->codec->extradata_size = 1024 + 2;
vst->codec->extradata = av_malloc(vst->codec->extradata_size +
FF_INPUT_BUFFER_PADDING_SIZE);
if (!vst->codec->extradata)
return AVERROR(ENOMEM);
AV_WL16(vst->codec->extradata, subversion);
for (i = 0; i < 256; i++)
AV_WL32(vst->codec->extradata + 2 + i * 4, palette[i]);
}
if (got_audio) {
ast = avformat_new_stream(ctx, 0);
if (!ast)
return AVERROR(ENOMEM);
smush->audio_stream_index = ast->index;
ast->start_time = 0;
ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
ast->codec->codec_id = AV_CODEC_ID_ADPCM_VIMA;
ast->codec->codec_tag = 0;
ast->codec->sample_rate = sample_rate;
ast->codec->channels = channels;
avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
}
return 0;
}
|
['static int smush_read_header(AVFormatContext *ctx)\n{\n SMUSHContext *smush = ctx->priv_data;\n AVIOContext *pb = ctx->pb;\n AVStream *vst, *ast;\n uint32_t magic, nframes, size, subversion, i;\n uint32_t width = 0, height = 0, got_audio = 0, read = 0;\n uint32_t sample_rate, channels, palette[256];\n magic = avio_rb32(pb);\n avio_skip(pb, 4);\n if (magic == MKBETAG(\'A\', \'N\', \'I\', \'M\')) {\n if (avio_rb32(pb) != MKBETAG(\'A\', \'H\', \'D\', \'R\'))\n return AVERROR_INVALIDDATA;\n size = avio_rb32(pb);\n if (size < 3 * 256 + 6)\n return AVERROR_INVALIDDATA;\n smush->version = 0;\n subversion = avio_rl16(pb);\n nframes = avio_rl16(pb);\n if (!nframes)\n return AVERROR_INVALIDDATA;\n avio_skip(pb, 2);\n for (i = 0; i < 256; i++)\n palette[i] = avio_rb24(pb);\n avio_skip(pb, size - (3 * 256 + 6));\n } else if (magic == MKBETAG(\'S\', \'A\', \'N\', \'M\')) {\n if (avio_rb32(pb) != MKBETAG(\'S\', \'H\', \'D\', \'R\'))\n return AVERROR_INVALIDDATA;\n size = avio_rb32(pb);\n if (size < 14)\n return AVERROR_INVALIDDATA;\n smush->version = 1;\n subversion = avio_rl16(pb);\n nframes = avio_rl32(pb);\n if (!nframes)\n return AVERROR_INVALIDDATA;\n avio_skip(pb, 2);\n width = avio_rl16(pb);\n height = avio_rl16(pb);\n avio_skip(pb, 2);\n avio_skip(pb, size - 14);\n if (avio_rb32(pb) != MKBETAG(\'F\', \'L\', \'H\', \'D\'))\n return AVERROR_INVALIDDATA;\n size = avio_rb32(pb);\n while (!got_audio && ((read + 8) < size)) {\n uint32_t sig, chunk_size;\n if (pb->eof_reached)\n return AVERROR_EOF;\n sig = avio_rb32(pb);\n chunk_size = avio_rb32(pb);\n read += 8;\n switch (sig) {\n case MKBETAG(\'W\', \'a\', \'v\', \'e\'):\n got_audio = 1;\n sample_rate = avio_rl32(pb);\n if (!sample_rate)\n return AVERROR_INVALIDDATA;\n channels = avio_rl32(pb);\n if (!channels)\n return AVERROR_INVALIDDATA;\n avio_skip(pb, chunk_size - 8);\n read += chunk_size;\n break;\n case MKBETAG(\'B\', \'l\', \'1\', \'6\'):\n case MKBETAG(\'A\', \'N\', \'N\', \'O\'):\n avio_skip(pb, chunk_size);\n read += chunk_size;\n break;\n default:\n return AVERROR_INVALIDDATA;\n break;\n }\n }\n avio_skip(pb, size - read);\n } else {\n av_log(ctx, AV_LOG_ERROR, "Wrong magic\\n");\n return AVERROR_INVALIDDATA;\n }\n vst = avformat_new_stream(ctx, 0);\n if (!vst)\n return AVERROR(ENOMEM);\n smush->video_stream_index = vst->index;\n avpriv_set_pts_info(vst, 64, 1, 15);\n vst->start_time = 0;\n vst->duration =\n vst->nb_frames = nframes;\n vst->avg_frame_rate = av_inv_q(vst->time_base);\n vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;\n vst->codec->codec_id = AV_CODEC_ID_SANM;\n vst->codec->codec_tag = 0;\n vst->codec->width = width;\n vst->codec->height = height;\n if (!smush->version) {\n av_free(vst->codec->extradata);\n vst->codec->extradata_size = 1024 + 2;\n vst->codec->extradata = av_malloc(vst->codec->extradata_size +\n FF_INPUT_BUFFER_PADDING_SIZE);\n if (!vst->codec->extradata)\n return AVERROR(ENOMEM);\n AV_WL16(vst->codec->extradata, subversion);\n for (i = 0; i < 256; i++)\n AV_WL32(vst->codec->extradata + 2 + i * 4, palette[i]);\n }\n if (got_audio) {\n ast = avformat_new_stream(ctx, 0);\n if (!ast)\n return AVERROR(ENOMEM);\n smush->audio_stream_index = ast->index;\n ast->start_time = 0;\n ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;\n ast->codec->codec_id = AV_CODEC_ID_ADPCM_VIMA;\n ast->codec->codec_tag = 0;\n ast->codec->sample_rate = sample_rate;\n ast->codec->channels = channels;\n avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);\n }\n return 0;\n}']
|
1,076 | 0 |
https://github.com/libav/libav/blob/cb6632809d27a0dfe2f63fea1c656bd39ce046ed/libavformat/utils.c/#L2678
|
void avformat_free_context(AVFormatContext *s)
{
int i;
AVStream *st;
av_opt_free(s);
if (s->iformat && s->iformat->priv_class && s->priv_data)
av_opt_free(s->priv_data);
for(i=0;i<s->nb_streams;i++) {
st = s->streams[i];
if (st->parser) {
av_parser_close(st->parser);
}
if (st->attached_pic.data)
av_free_packet(&st->attached_pic);
av_dict_free(&st->metadata);
av_free(st->index_entries);
av_free(st->codec->extradata);
av_free(st->codec->subtitle_header);
av_free(st->codec);
av_free(st->priv_data);
av_free(st->info);
av_free(st);
}
for(i=s->nb_programs-1; i>=0; i--) {
av_dict_free(&s->programs[i]->metadata);
av_freep(&s->programs[i]->stream_index);
av_freep(&s->programs[i]);
}
av_freep(&s->programs);
av_freep(&s->priv_data);
while(s->nb_chapters--) {
av_dict_free(&s->chapters[s->nb_chapters]->metadata);
av_free(s->chapters[s->nb_chapters]);
}
av_freep(&s->chapters);
av_dict_free(&s->metadata);
av_freep(&s->streams);
av_free(s);
}
|
['static int probe_file(const char *filename)\n{\n AVFormatContext *fmt_ctx;\n int ret, i;\n if ((ret = open_input_file(&fmt_ctx, filename)))\n return ret;\n if (do_show_format)\n show_format(fmt_ctx);\n if (do_show_streams) {\n probe_array_header("streams");\n for (i = 0; i < fmt_ctx->nb_streams; i++)\n show_stream(fmt_ctx, i);\n probe_array_footer("streams");\n }\n if (do_show_packets)\n show_packets(fmt_ctx);\n close_input_file(&fmt_ctx);\n return 0;\n}', 'static void close_input_file(AVFormatContext **ctx_ptr)\n{\n int i;\n AVFormatContext *fmt_ctx = *ctx_ptr;\n for (i = 0; i < fmt_ctx->nb_streams; i++) {\n AVStream *stream = fmt_ctx->streams[i];\n avcodec_close(stream->codec);\n }\n avformat_close_input(ctx_ptr);\n}', '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}', 'void avformat_free_context(AVFormatContext *s)\n{\n int i;\n AVStream *st;\n av_opt_free(s);\n if (s->iformat && s->iformat->priv_class && s->priv_data)\n av_opt_free(s->priv_data);\n for(i=0;i<s->nb_streams;i++) {\n st = s->streams[i];\n if (st->parser) {\n av_parser_close(st->parser);\n }\n if (st->attached_pic.data)\n av_free_packet(&st->attached_pic);\n av_dict_free(&st->metadata);\n av_free(st->index_entries);\n av_free(st->codec->extradata);\n av_free(st->codec->subtitle_header);\n av_free(st->codec);\n av_free(st->priv_data);\n av_free(st->info);\n av_free(st);\n }\n for(i=s->nb_programs-1; i>=0; i--) {\n av_dict_free(&s->programs[i]->metadata);\n av_freep(&s->programs[i]->stream_index);\n av_freep(&s->programs[i]);\n }\n av_freep(&s->programs);\n av_freep(&s->priv_data);\n while(s->nb_chapters--) {\n av_dict_free(&s->chapters[s->nb_chapters]->metadata);\n av_free(s->chapters[s->nb_chapters]);\n }\n av_freep(&s->chapters);\n av_dict_free(&s->metadata);\n av_freep(&s->streams);\n av_free(s);\n}']
|
1,077 | 0 |
https://github.com/libav/libav/blob/044a950d8230c881c7e6833f9999498784f3fc76/libavcodec/bink.c/#L887
|
static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *pkt)
{
BinkContext * const c = avctx->priv_data;
GetBitContext gb;
int blk;
int i, j, plane, plane_idx, bx, by;
uint8_t *dst, *prev, *ref, *ref_start, *ref_end;
int v, col[2];
const uint8_t *scan;
int xoff, yoff;
DECLARE_ALIGNED_16(DCTELEM, block[64]);
DECLARE_ALIGNED_16(uint8_t, ublock[64]);
int coordmap[64];
if(c->pic.data[0])
avctx->release_buffer(avctx, &c->pic);
if(avctx->get_buffer(avctx, &c->pic) < 0){
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
return -1;
}
init_get_bits(&gb, pkt->data, pkt->size*8);
if (c->version >= 'i')
skip_bits_long(&gb, 32);
for (plane = 0; plane < 3; plane++) {
const int stride = c->pic.linesize[plane];
int bw = plane ? (avctx->width + 15) >> 4 : (avctx->width + 7) >> 3;
int bh = plane ? (avctx->height + 15) >> 4 : (avctx->height + 7) >> 3;
init_lengths(c, avctx->width >> !!plane, bw);
for (i = 0; i < BINK_NB_SRC; i++)
read_bundle(&gb, c, i);
plane_idx = (!plane || !c->swap_planes) ? plane : (plane ^ 3);
ref_start = c->last.data[plane_idx];
ref_end = c->last.data[plane_idx]
+ (bw - 1 + c->last.linesize[plane_idx] * (bh - 1)) * 8;
for (i = 0; i < 64; i++)
coordmap[i] = (i & 7) + (i >> 3) * stride;
for (by = 0; by < bh; by++) {
if (read_block_types(avctx, &gb, &c->bundle[BINK_SRC_BLOCK_TYPES]) < 0)
return -1;
if (read_block_types(avctx, &gb, &c->bundle[BINK_SRC_SUB_BLOCK_TYPES]) < 0)
return -1;
if (read_colors(&gb, &c->bundle[BINK_SRC_COLORS], c) < 0)
return -1;
if (read_patterns(avctx, &gb, &c->bundle[BINK_SRC_PATTERN]) < 0)
return -1;
if (read_motion_values(avctx, &gb, &c->bundle[BINK_SRC_X_OFF]) < 0)
return -1;
if (read_motion_values(avctx, &gb, &c->bundle[BINK_SRC_Y_OFF]) < 0)
return -1;
if (read_dcs(avctx, &gb, &c->bundle[BINK_SRC_INTRA_DC], DC_START_BITS, 0) < 0)
return -1;
if (read_dcs(avctx, &gb, &c->bundle[BINK_SRC_INTER_DC], DC_START_BITS, 1) < 0)
return -1;
if (read_runs(avctx, &gb, &c->bundle[BINK_SRC_RUN]) < 0)
return -1;
if (by == bh)
break;
dst = c->pic.data[plane_idx] + 8*by*stride;
prev = c->last.data[plane_idx] + 8*by*stride;
for (bx = 0; bx < bw; bx++, dst += 8, prev += 8) {
blk = get_value(c, BINK_SRC_BLOCK_TYPES);
if ((by & 1) && blk == SCALED_BLOCK) {
bx++;
dst += 8;
prev += 8;
continue;
}
switch (blk) {
case SKIP_BLOCK:
c->dsp.put_pixels_tab[1][0](dst, prev, stride, 8);
break;
case SCALED_BLOCK:
blk = get_value(c, BINK_SRC_SUB_BLOCK_TYPES);
switch (blk) {
case RUN_BLOCK:
scan = bink_patterns[get_bits(&gb, 4)];
i = 0;
do {
int run = get_value(c, BINK_SRC_RUN) + 1;
i += run;
if (i > 64) {
av_log(avctx, AV_LOG_ERROR, "Run went out of bounds\n");
return -1;
}
if (get_bits1(&gb)) {
v = get_value(c, BINK_SRC_COLORS);
for (j = 0; j < run; j++)
ublock[*scan++] = v;
} else {
for (j = 0; j < run; j++)
ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
}
} while (i < 63);
if (i == 63)
ublock[*scan++] = get_value(c, BINK_SRC_COLORS);
break;
case INTRA_BLOCK:
c->dsp.clear_block(block);
block[0] = get_value(c, BINK_SRC_INTRA_DC);
read_dct_coeffs(&gb, block, c->scantable.permutated, 1);
c->dsp.idct(block);
c->dsp.put_pixels_nonclamped(block, ublock, 8);
break;
case FILL_BLOCK:
v = get_value(c, BINK_SRC_COLORS);
c->dsp.fill_block_tab[0](dst, v, stride, 16);
break;
case PATTERN_BLOCK:
for (i = 0; i < 2; i++)
col[i] = get_value(c, BINK_SRC_COLORS);
for (j = 0; j < 8; j++) {
v = get_value(c, BINK_SRC_PATTERN);
for (i = 0; i < 8; i++, v >>= 1)
ublock[i + j*8] = col[v & 1];
}
break;
case RAW_BLOCK:
for (j = 0; j < 8; j++)
for (i = 0; i < 8; i++)
ublock[i + j*8] = get_value(c, BINK_SRC_COLORS);
break;
default:
av_log(avctx, AV_LOG_ERROR, "Incorrect 16x16 block type %d\n", blk);
return -1;
}
if (blk != FILL_BLOCK)
c->dsp.scale_block(ublock, dst, stride);
bx++;
dst += 8;
prev += 8;
break;
case MOTION_BLOCK:
xoff = get_value(c, BINK_SRC_X_OFF);
yoff = get_value(c, BINK_SRC_Y_OFF);
ref = prev + xoff + yoff * stride;
if (ref < ref_start || ref > ref_end) {
av_log(avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
bx*8 + xoff, by*8 + yoff);
return -1;
}
c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
break;
case RUN_BLOCK:
scan = bink_patterns[get_bits(&gb, 4)];
i = 0;
do {
int run = get_value(c, BINK_SRC_RUN) + 1;
i += run;
if (i > 64) {
av_log(avctx, AV_LOG_ERROR, "Run went out of bounds\n");
return -1;
}
if (get_bits1(&gb)) {
v = get_value(c, BINK_SRC_COLORS);
for (j = 0; j < run; j++)
dst[coordmap[*scan++]] = v;
} else {
for (j = 0; j < run; j++)
dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
}
} while (i < 63);
if (i == 63)
dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);
break;
case RESIDUE_BLOCK:
xoff = get_value(c, BINK_SRC_X_OFF);
yoff = get_value(c, BINK_SRC_Y_OFF);
ref = prev + xoff + yoff * stride;
if (ref < ref_start || ref > ref_end) {
av_log(avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\n",
bx*8 + xoff, by*8 + yoff);
return -1;
}
c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
c->dsp.clear_block(block);
v = get_bits(&gb, 7);
read_residue(&gb, block, v);
c->dsp.add_pixels8(dst, block, stride);
break;
case INTRA_BLOCK:
c->dsp.clear_block(block);
block[0] = get_value(c, BINK_SRC_INTRA_DC);
read_dct_coeffs(&gb, block, c->scantable.permutated, 1);
c->dsp.idct_put(dst, stride, block);
break;
case FILL_BLOCK:
v = get_value(c, BINK_SRC_COLORS);
c->dsp.fill_block_tab[1](dst, v, stride, 8);
break;
case INTER_BLOCK:
xoff = get_value(c, BINK_SRC_X_OFF);
yoff = get_value(c, BINK_SRC_Y_OFF);
ref = prev + xoff + yoff * stride;
c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
c->dsp.clear_block(block);
block[0] = get_value(c, BINK_SRC_INTER_DC);
read_dct_coeffs(&gb, block, c->scantable.permutated, 0);
c->dsp.idct_add(dst, stride, block);
break;
case PATTERN_BLOCK:
for (i = 0; i < 2; i++)
col[i] = get_value(c, BINK_SRC_COLORS);
for (i = 0; i < 8; i++) {
v = get_value(c, BINK_SRC_PATTERN);
for (j = 0; j < 8; j++, v >>= 1)
dst[i*stride + j] = col[v & 1];
}
break;
case RAW_BLOCK:
for (i = 0; i < 8; i++)
memcpy(dst + i*stride, c->bundle[BINK_SRC_COLORS].cur_ptr + i*8, 8);
c->bundle[BINK_SRC_COLORS].cur_ptr += 64;
break;
default:
av_log(avctx, AV_LOG_ERROR, "Unknown block type %d\n", blk);
return -1;
}
}
}
if (get_bits_count(&gb) & 0x1F)
skip_bits_long(&gb, 32 - (get_bits_count(&gb) & 0x1F));
}
emms_c();
*data_size = sizeof(AVFrame);
*(AVFrame*)data = c->pic;
FFSWAP(AVFrame, c->pic, c->last);
return pkt->size;
}
|
['static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *pkt)\n{\n BinkContext * const c = avctx->priv_data;\n GetBitContext gb;\n int blk;\n int i, j, plane, plane_idx, bx, by;\n uint8_t *dst, *prev, *ref, *ref_start, *ref_end;\n int v, col[2];\n const uint8_t *scan;\n int xoff, yoff;\n DECLARE_ALIGNED_16(DCTELEM, block[64]);\n DECLARE_ALIGNED_16(uint8_t, ublock[64]);\n int coordmap[64];\n if(c->pic.data[0])\n avctx->release_buffer(avctx, &c->pic);\n if(avctx->get_buffer(avctx, &c->pic) < 0){\n av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n return -1;\n }\n init_get_bits(&gb, pkt->data, pkt->size*8);\n if (c->version >= \'i\')\n skip_bits_long(&gb, 32);\n for (plane = 0; plane < 3; plane++) {\n const int stride = c->pic.linesize[plane];\n int bw = plane ? (avctx->width + 15) >> 4 : (avctx->width + 7) >> 3;\n int bh = plane ? (avctx->height + 15) >> 4 : (avctx->height + 7) >> 3;\n init_lengths(c, avctx->width >> !!plane, bw);\n for (i = 0; i < BINK_NB_SRC; i++)\n read_bundle(&gb, c, i);\n plane_idx = (!plane || !c->swap_planes) ? plane : (plane ^ 3);\n ref_start = c->last.data[plane_idx];\n ref_end = c->last.data[plane_idx]\n + (bw - 1 + c->last.linesize[plane_idx] * (bh - 1)) * 8;\n for (i = 0; i < 64; i++)\n coordmap[i] = (i & 7) + (i >> 3) * stride;\n for (by = 0; by < bh; by++) {\n if (read_block_types(avctx, &gb, &c->bundle[BINK_SRC_BLOCK_TYPES]) < 0)\n return -1;\n if (read_block_types(avctx, &gb, &c->bundle[BINK_SRC_SUB_BLOCK_TYPES]) < 0)\n return -1;\n if (read_colors(&gb, &c->bundle[BINK_SRC_COLORS], c) < 0)\n return -1;\n if (read_patterns(avctx, &gb, &c->bundle[BINK_SRC_PATTERN]) < 0)\n return -1;\n if (read_motion_values(avctx, &gb, &c->bundle[BINK_SRC_X_OFF]) < 0)\n return -1;\n if (read_motion_values(avctx, &gb, &c->bundle[BINK_SRC_Y_OFF]) < 0)\n return -1;\n if (read_dcs(avctx, &gb, &c->bundle[BINK_SRC_INTRA_DC], DC_START_BITS, 0) < 0)\n return -1;\n if (read_dcs(avctx, &gb, &c->bundle[BINK_SRC_INTER_DC], DC_START_BITS, 1) < 0)\n return -1;\n if (read_runs(avctx, &gb, &c->bundle[BINK_SRC_RUN]) < 0)\n return -1;\n if (by == bh)\n break;\n dst = c->pic.data[plane_idx] + 8*by*stride;\n prev = c->last.data[plane_idx] + 8*by*stride;\n for (bx = 0; bx < bw; bx++, dst += 8, prev += 8) {\n blk = get_value(c, BINK_SRC_BLOCK_TYPES);\n if ((by & 1) && blk == SCALED_BLOCK) {\n bx++;\n dst += 8;\n prev += 8;\n continue;\n }\n switch (blk) {\n case SKIP_BLOCK:\n c->dsp.put_pixels_tab[1][0](dst, prev, stride, 8);\n break;\n case SCALED_BLOCK:\n blk = get_value(c, BINK_SRC_SUB_BLOCK_TYPES);\n switch (blk) {\n case RUN_BLOCK:\n scan = bink_patterns[get_bits(&gb, 4)];\n i = 0;\n do {\n int run = get_value(c, BINK_SRC_RUN) + 1;\n i += run;\n if (i > 64) {\n av_log(avctx, AV_LOG_ERROR, "Run went out of bounds\\n");\n return -1;\n }\n if (get_bits1(&gb)) {\n v = get_value(c, BINK_SRC_COLORS);\n for (j = 0; j < run; j++)\n ublock[*scan++] = v;\n } else {\n for (j = 0; j < run; j++)\n ublock[*scan++] = get_value(c, BINK_SRC_COLORS);\n }\n } while (i < 63);\n if (i == 63)\n ublock[*scan++] = get_value(c, BINK_SRC_COLORS);\n break;\n case INTRA_BLOCK:\n c->dsp.clear_block(block);\n block[0] = get_value(c, BINK_SRC_INTRA_DC);\n read_dct_coeffs(&gb, block, c->scantable.permutated, 1);\n c->dsp.idct(block);\n c->dsp.put_pixels_nonclamped(block, ublock, 8);\n break;\n case FILL_BLOCK:\n v = get_value(c, BINK_SRC_COLORS);\n c->dsp.fill_block_tab[0](dst, v, stride, 16);\n break;\n case PATTERN_BLOCK:\n for (i = 0; i < 2; i++)\n col[i] = get_value(c, BINK_SRC_COLORS);\n for (j = 0; j < 8; j++) {\n v = get_value(c, BINK_SRC_PATTERN);\n for (i = 0; i < 8; i++, v >>= 1)\n ublock[i + j*8] = col[v & 1];\n }\n break;\n case RAW_BLOCK:\n for (j = 0; j < 8; j++)\n for (i = 0; i < 8; i++)\n ublock[i + j*8] = get_value(c, BINK_SRC_COLORS);\n break;\n default:\n av_log(avctx, AV_LOG_ERROR, "Incorrect 16x16 block type %d\\n", blk);\n return -1;\n }\n if (blk != FILL_BLOCK)\n c->dsp.scale_block(ublock, dst, stride);\n bx++;\n dst += 8;\n prev += 8;\n break;\n case MOTION_BLOCK:\n xoff = get_value(c, BINK_SRC_X_OFF);\n yoff = get_value(c, BINK_SRC_Y_OFF);\n ref = prev + xoff + yoff * stride;\n if (ref < ref_start || ref > ref_end) {\n av_log(avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\\n",\n bx*8 + xoff, by*8 + yoff);\n return -1;\n }\n c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);\n break;\n case RUN_BLOCK:\n scan = bink_patterns[get_bits(&gb, 4)];\n i = 0;\n do {\n int run = get_value(c, BINK_SRC_RUN) + 1;\n i += run;\n if (i > 64) {\n av_log(avctx, AV_LOG_ERROR, "Run went out of bounds\\n");\n return -1;\n }\n if (get_bits1(&gb)) {\n v = get_value(c, BINK_SRC_COLORS);\n for (j = 0; j < run; j++)\n dst[coordmap[*scan++]] = v;\n } else {\n for (j = 0; j < run; j++)\n dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);\n }\n } while (i < 63);\n if (i == 63)\n dst[coordmap[*scan++]] = get_value(c, BINK_SRC_COLORS);\n break;\n case RESIDUE_BLOCK:\n xoff = get_value(c, BINK_SRC_X_OFF);\n yoff = get_value(c, BINK_SRC_Y_OFF);\n ref = prev + xoff + yoff * stride;\n if (ref < ref_start || ref > ref_end) {\n av_log(avctx, AV_LOG_ERROR, "Copy out of bounds @%d, %d\\n",\n bx*8 + xoff, by*8 + yoff);\n return -1;\n }\n c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);\n c->dsp.clear_block(block);\n v = get_bits(&gb, 7);\n read_residue(&gb, block, v);\n c->dsp.add_pixels8(dst, block, stride);\n break;\n case INTRA_BLOCK:\n c->dsp.clear_block(block);\n block[0] = get_value(c, BINK_SRC_INTRA_DC);\n read_dct_coeffs(&gb, block, c->scantable.permutated, 1);\n c->dsp.idct_put(dst, stride, block);\n break;\n case FILL_BLOCK:\n v = get_value(c, BINK_SRC_COLORS);\n c->dsp.fill_block_tab[1](dst, v, stride, 8);\n break;\n case INTER_BLOCK:\n xoff = get_value(c, BINK_SRC_X_OFF);\n yoff = get_value(c, BINK_SRC_Y_OFF);\n ref = prev + xoff + yoff * stride;\n c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);\n c->dsp.clear_block(block);\n block[0] = get_value(c, BINK_SRC_INTER_DC);\n read_dct_coeffs(&gb, block, c->scantable.permutated, 0);\n c->dsp.idct_add(dst, stride, block);\n break;\n case PATTERN_BLOCK:\n for (i = 0; i < 2; i++)\n col[i] = get_value(c, BINK_SRC_COLORS);\n for (i = 0; i < 8; i++) {\n v = get_value(c, BINK_SRC_PATTERN);\n for (j = 0; j < 8; j++, v >>= 1)\n dst[i*stride + j] = col[v & 1];\n }\n break;\n case RAW_BLOCK:\n for (i = 0; i < 8; i++)\n memcpy(dst + i*stride, c->bundle[BINK_SRC_COLORS].cur_ptr + i*8, 8);\n c->bundle[BINK_SRC_COLORS].cur_ptr += 64;\n break;\n default:\n av_log(avctx, AV_LOG_ERROR, "Unknown block type %d\\n", blk);\n return -1;\n }\n }\n }\n if (get_bits_count(&gb) & 0x1F)\n skip_bits_long(&gb, 32 - (get_bits_count(&gb) & 0x1F));\n }\n emms_c();\n *data_size = sizeof(AVFrame);\n *(AVFrame*)data = c->pic;\n FFSWAP(AVFrame, c->pic, c->last);\n return pkt->size;\n}']
|
1,078 | 0 |
https://github.com/libav/libav/blob/12f0388f9cb32016ac0dacaeca631b088b29bb96/libavcodec/flicvideo.c/#L390
|
static int flic_decode_frame_8BPP(AVCodecContext *avctx,
void *data, int *got_frame,
const uint8_t *buf, int buf_size)
{
FlicDecodeContext *s = avctx->priv_data;
GetByteContext g2;
int stream_ptr_after_color_chunk;
int pixel_ptr;
int palette_ptr;
unsigned char palette_idx1;
unsigned char palette_idx2;
unsigned int frame_size;
int num_chunks;
unsigned int chunk_size;
int chunk_type;
int i, j, ret;
int color_packets;
int color_changes;
int color_shift;
unsigned char r, g, b;
int lines;
int compressed_lines;
int starting_line;
signed short line_packets;
int y_ptr;
int byte_run;
int pixel_skip;
int pixel_countdown;
unsigned char *pixels;
unsigned int pixel_limit;
bytestream2_init(&g2, buf, buf_size);
if ((ret = ff_reget_buffer(avctx, s->frame)) < 0) {
av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
return ret;
}
pixels = s->frame->data[0];
pixel_limit = s->avctx->height * s->frame->linesize[0];
frame_size = bytestream2_get_le32(&g2);
bytestream2_skip(&g2, 2);
num_chunks = bytestream2_get_le16(&g2);
bytestream2_skip(&g2, 8);
frame_size -= 16;
while ((frame_size > 0) && (num_chunks > 0)) {
chunk_size = bytestream2_get_le32(&g2);
chunk_type = bytestream2_get_le16(&g2);
switch (chunk_type) {
case FLI_256_COLOR:
case FLI_COLOR:
stream_ptr_after_color_chunk = bytestream2_tell(&g2) + chunk_size - 6;
if ((chunk_type == FLI_256_COLOR) && (s->fli_type != FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE))
color_shift = 0;
else
color_shift = 2;
color_packets = bytestream2_get_le16(&g2);
palette_ptr = 0;
for (i = 0; i < color_packets; i++) {
palette_ptr += bytestream2_get_byte(&g2);
color_changes = bytestream2_get_byte(&g2);
if (color_changes == 0)
color_changes = 256;
for (j = 0; j < color_changes; j++) {
unsigned int entry;
if ((unsigned)palette_ptr >= 256)
palette_ptr = 0;
r = bytestream2_get_byte(&g2) << color_shift;
g = bytestream2_get_byte(&g2) << color_shift;
b = bytestream2_get_byte(&g2) << color_shift;
entry = (r << 16) | (g << 8) | b;
if (s->palette[palette_ptr] != entry)
s->new_palette = 1;
s->palette[palette_ptr++] = entry;
}
}
if (stream_ptr_after_color_chunk - bytestream2_tell(&g2) > 0)
bytestream2_skip(&g2, stream_ptr_after_color_chunk - bytestream2_tell(&g2));
break;
case FLI_DELTA:
y_ptr = 0;
compressed_lines = bytestream2_get_le16(&g2);
while (compressed_lines > 0) {
line_packets = bytestream2_get_le16(&g2);
if ((line_packets & 0xC000) == 0xC000) {
line_packets = -line_packets;
y_ptr += line_packets * s->frame->linesize[0];
} else if ((line_packets & 0xC000) == 0x4000) {
av_log(avctx, AV_LOG_ERROR, "Undefined opcode (%x) in DELTA_FLI\n", line_packets);
} else if ((line_packets & 0xC000) == 0x8000) {
pixel_ptr= y_ptr + s->frame->linesize[0] - 1;
CHECK_PIXEL_PTR(0);
pixels[pixel_ptr] = line_packets & 0xff;
} else {
compressed_lines--;
pixel_ptr = y_ptr;
CHECK_PIXEL_PTR(0);
pixel_countdown = s->avctx->width;
for (i = 0; i < line_packets; i++) {
pixel_skip = bytestream2_get_byte(&g2);
pixel_ptr += pixel_skip;
pixel_countdown -= pixel_skip;
byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
if (byte_run < 0) {
byte_run = -byte_run;
palette_idx1 = bytestream2_get_byte(&g2);
palette_idx2 = bytestream2_get_byte(&g2);
CHECK_PIXEL_PTR(byte_run * 2);
for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
pixels[pixel_ptr++] = palette_idx1;
pixels[pixel_ptr++] = palette_idx2;
}
} else {
CHECK_PIXEL_PTR(byte_run * 2);
for (j = 0; j < byte_run * 2; j++, pixel_countdown--) {
pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
}
}
}
y_ptr += s->frame->linesize[0];
}
}
break;
case FLI_LC:
starting_line = bytestream2_get_le16(&g2);
y_ptr = 0;
y_ptr += starting_line * s->frame->linesize[0];
compressed_lines = bytestream2_get_le16(&g2);
while (compressed_lines > 0) {
pixel_ptr = y_ptr;
CHECK_PIXEL_PTR(0);
pixel_countdown = s->avctx->width;
line_packets = bytestream2_get_byte(&g2);
if (line_packets > 0) {
for (i = 0; i < line_packets; i++) {
pixel_skip = bytestream2_get_byte(&g2);
pixel_ptr += pixel_skip;
pixel_countdown -= pixel_skip;
byte_run = sign_extend(bytestream2_get_byte(&g2),8);
if (byte_run > 0) {
CHECK_PIXEL_PTR(byte_run);
for (j = 0; j < byte_run; j++, pixel_countdown--) {
pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
}
} else if (byte_run < 0) {
byte_run = -byte_run;
palette_idx1 = bytestream2_get_byte(&g2);
CHECK_PIXEL_PTR(byte_run);
for (j = 0; j < byte_run; j++, pixel_countdown--) {
pixels[pixel_ptr++] = palette_idx1;
}
}
}
}
y_ptr += s->frame->linesize[0];
compressed_lines--;
}
break;
case FLI_BLACK:
memset(pixels, 0,
s->frame->linesize[0] * s->avctx->height);
break;
case FLI_BRUN:
y_ptr = 0;
for (lines = 0; lines < s->avctx->height; lines++) {
pixel_ptr = y_ptr;
bytestream2_skip(&g2, 1);
pixel_countdown = s->avctx->width;
while (pixel_countdown > 0) {
byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
if (!byte_run) {
av_log(avctx, AV_LOG_ERROR, "Invalid byte run value.\n");
return AVERROR_INVALIDDATA;
}
if (byte_run > 0) {
palette_idx1 = bytestream2_get_byte(&g2);
CHECK_PIXEL_PTR(byte_run);
for (j = 0; j < byte_run; j++) {
pixels[pixel_ptr++] = palette_idx1;
pixel_countdown--;
if (pixel_countdown < 0)
av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
pixel_countdown, lines);
}
} else {
byte_run = -byte_run;
CHECK_PIXEL_PTR(byte_run);
for (j = 0; j < byte_run; j++) {
pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
pixel_countdown--;
if (pixel_countdown < 0)
av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
pixel_countdown, lines);
}
}
}
y_ptr += s->frame->linesize[0];
}
break;
case FLI_COPY:
if (chunk_size - 6 > s->avctx->width * s->avctx->height) {
av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
"bigger than image, skipping chunk\n", chunk_size - 6);
bytestream2_skip(&g2, chunk_size - 6);
} else {
for (y_ptr = 0; y_ptr < s->frame->linesize[0] * s->avctx->height;
y_ptr += s->frame->linesize[0]) {
bytestream2_get_buffer(&g2, &pixels[y_ptr],
s->avctx->width);
}
}
break;
case FLI_MINI:
bytestream2_skip(&g2, chunk_size - 6);
break;
default:
av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
break;
}
frame_size -= chunk_size;
num_chunks--;
}
if ((bytestream2_get_bytes_left(&g2) != 0) &&
(bytestream2_get_bytes_left(&g2) != 1))
av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
"and final chunk ptr = %d\n", buf_size,
buf_size - bytestream2_get_bytes_left(&g2));
memcpy(s->frame->data[1], s->palette, AVPALETTE_SIZE);
if (s->new_palette) {
s->frame->palette_has_changed = 1;
s->new_palette = 0;
}
if ((ret = av_frame_ref(data, s->frame)) < 0)
return ret;
*got_frame = 1;
return buf_size;
}
|
['static int flic_decode_frame_8BPP(AVCodecContext *avctx,\n void *data, int *got_frame,\n const uint8_t *buf, int buf_size)\n{\n FlicDecodeContext *s = avctx->priv_data;\n GetByteContext g2;\n int stream_ptr_after_color_chunk;\n int pixel_ptr;\n int palette_ptr;\n unsigned char palette_idx1;\n unsigned char palette_idx2;\n unsigned int frame_size;\n int num_chunks;\n unsigned int chunk_size;\n int chunk_type;\n int i, j, ret;\n int color_packets;\n int color_changes;\n int color_shift;\n unsigned char r, g, b;\n int lines;\n int compressed_lines;\n int starting_line;\n signed short line_packets;\n int y_ptr;\n int byte_run;\n int pixel_skip;\n int pixel_countdown;\n unsigned char *pixels;\n unsigned int pixel_limit;\n bytestream2_init(&g2, buf, buf_size);\n if ((ret = ff_reget_buffer(avctx, s->frame)) < 0) {\n av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\\n");\n return ret;\n }\n pixels = s->frame->data[0];\n pixel_limit = s->avctx->height * s->frame->linesize[0];\n frame_size = bytestream2_get_le32(&g2);\n bytestream2_skip(&g2, 2);\n num_chunks = bytestream2_get_le16(&g2);\n bytestream2_skip(&g2, 8);\n frame_size -= 16;\n while ((frame_size > 0) && (num_chunks > 0)) {\n chunk_size = bytestream2_get_le32(&g2);\n chunk_type = bytestream2_get_le16(&g2);\n switch (chunk_type) {\n case FLI_256_COLOR:\n case FLI_COLOR:\n stream_ptr_after_color_chunk = bytestream2_tell(&g2) + chunk_size - 6;\n if ((chunk_type == FLI_256_COLOR) && (s->fli_type != FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE))\n color_shift = 0;\n else\n color_shift = 2;\n color_packets = bytestream2_get_le16(&g2);\n palette_ptr = 0;\n for (i = 0; i < color_packets; i++) {\n palette_ptr += bytestream2_get_byte(&g2);\n color_changes = bytestream2_get_byte(&g2);\n if (color_changes == 0)\n color_changes = 256;\n for (j = 0; j < color_changes; j++) {\n unsigned int entry;\n if ((unsigned)palette_ptr >= 256)\n palette_ptr = 0;\n r = bytestream2_get_byte(&g2) << color_shift;\n g = bytestream2_get_byte(&g2) << color_shift;\n b = bytestream2_get_byte(&g2) << color_shift;\n entry = (r << 16) | (g << 8) | b;\n if (s->palette[palette_ptr] != entry)\n s->new_palette = 1;\n s->palette[palette_ptr++] = entry;\n }\n }\n if (stream_ptr_after_color_chunk - bytestream2_tell(&g2) > 0)\n bytestream2_skip(&g2, stream_ptr_after_color_chunk - bytestream2_tell(&g2));\n break;\n case FLI_DELTA:\n y_ptr = 0;\n compressed_lines = bytestream2_get_le16(&g2);\n while (compressed_lines > 0) {\n line_packets = bytestream2_get_le16(&g2);\n if ((line_packets & 0xC000) == 0xC000) {\n line_packets = -line_packets;\n y_ptr += line_packets * s->frame->linesize[0];\n } else if ((line_packets & 0xC000) == 0x4000) {\n av_log(avctx, AV_LOG_ERROR, "Undefined opcode (%x) in DELTA_FLI\\n", line_packets);\n } else if ((line_packets & 0xC000) == 0x8000) {\n pixel_ptr= y_ptr + s->frame->linesize[0] - 1;\n CHECK_PIXEL_PTR(0);\n pixels[pixel_ptr] = line_packets & 0xff;\n } else {\n compressed_lines--;\n pixel_ptr = y_ptr;\n CHECK_PIXEL_PTR(0);\n pixel_countdown = s->avctx->width;\n for (i = 0; i < line_packets; i++) {\n pixel_skip = bytestream2_get_byte(&g2);\n pixel_ptr += pixel_skip;\n pixel_countdown -= pixel_skip;\n byte_run = sign_extend(bytestream2_get_byte(&g2), 8);\n if (byte_run < 0) {\n byte_run = -byte_run;\n palette_idx1 = bytestream2_get_byte(&g2);\n palette_idx2 = bytestream2_get_byte(&g2);\n CHECK_PIXEL_PTR(byte_run * 2);\n for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {\n pixels[pixel_ptr++] = palette_idx1;\n pixels[pixel_ptr++] = palette_idx2;\n }\n } else {\n CHECK_PIXEL_PTR(byte_run * 2);\n for (j = 0; j < byte_run * 2; j++, pixel_countdown--) {\n pixels[pixel_ptr++] = bytestream2_get_byte(&g2);\n }\n }\n }\n y_ptr += s->frame->linesize[0];\n }\n }\n break;\n case FLI_LC:\n starting_line = bytestream2_get_le16(&g2);\n y_ptr = 0;\n y_ptr += starting_line * s->frame->linesize[0];\n compressed_lines = bytestream2_get_le16(&g2);\n while (compressed_lines > 0) {\n pixel_ptr = y_ptr;\n CHECK_PIXEL_PTR(0);\n pixel_countdown = s->avctx->width;\n line_packets = bytestream2_get_byte(&g2);\n if (line_packets > 0) {\n for (i = 0; i < line_packets; i++) {\n pixel_skip = bytestream2_get_byte(&g2);\n pixel_ptr += pixel_skip;\n pixel_countdown -= pixel_skip;\n byte_run = sign_extend(bytestream2_get_byte(&g2),8);\n if (byte_run > 0) {\n CHECK_PIXEL_PTR(byte_run);\n for (j = 0; j < byte_run; j++, pixel_countdown--) {\n pixels[pixel_ptr++] = bytestream2_get_byte(&g2);\n }\n } else if (byte_run < 0) {\n byte_run = -byte_run;\n palette_idx1 = bytestream2_get_byte(&g2);\n CHECK_PIXEL_PTR(byte_run);\n for (j = 0; j < byte_run; j++, pixel_countdown--) {\n pixels[pixel_ptr++] = palette_idx1;\n }\n }\n }\n }\n y_ptr += s->frame->linesize[0];\n compressed_lines--;\n }\n break;\n case FLI_BLACK:\n memset(pixels, 0,\n s->frame->linesize[0] * s->avctx->height);\n break;\n case FLI_BRUN:\n y_ptr = 0;\n for (lines = 0; lines < s->avctx->height; lines++) {\n pixel_ptr = y_ptr;\n bytestream2_skip(&g2, 1);\n pixel_countdown = s->avctx->width;\n while (pixel_countdown > 0) {\n byte_run = sign_extend(bytestream2_get_byte(&g2), 8);\n if (!byte_run) {\n av_log(avctx, AV_LOG_ERROR, "Invalid byte run value.\\n");\n return AVERROR_INVALIDDATA;\n }\n if (byte_run > 0) {\n palette_idx1 = bytestream2_get_byte(&g2);\n CHECK_PIXEL_PTR(byte_run);\n for (j = 0; j < byte_run; j++) {\n pixels[pixel_ptr++] = palette_idx1;\n pixel_countdown--;\n if (pixel_countdown < 0)\n av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\\n",\n pixel_countdown, lines);\n }\n } else {\n byte_run = -byte_run;\n CHECK_PIXEL_PTR(byte_run);\n for (j = 0; j < byte_run; j++) {\n pixels[pixel_ptr++] = bytestream2_get_byte(&g2);\n pixel_countdown--;\n if (pixel_countdown < 0)\n av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\\n",\n pixel_countdown, lines);\n }\n }\n }\n y_ptr += s->frame->linesize[0];\n }\n break;\n case FLI_COPY:\n if (chunk_size - 6 > s->avctx->width * s->avctx->height) {\n av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \\\n "bigger than image, skipping chunk\\n", chunk_size - 6);\n bytestream2_skip(&g2, chunk_size - 6);\n } else {\n for (y_ptr = 0; y_ptr < s->frame->linesize[0] * s->avctx->height;\n y_ptr += s->frame->linesize[0]) {\n bytestream2_get_buffer(&g2, &pixels[y_ptr],\n s->avctx->width);\n }\n }\n break;\n case FLI_MINI:\n bytestream2_skip(&g2, chunk_size - 6);\n break;\n default:\n av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\\n", chunk_type);\n break;\n }\n frame_size -= chunk_size;\n num_chunks--;\n }\n if ((bytestream2_get_bytes_left(&g2) != 0) &&\n (bytestream2_get_bytes_left(&g2) != 1))\n av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \\\n "and final chunk ptr = %d\\n", buf_size,\n buf_size - bytestream2_get_bytes_left(&g2));\n memcpy(s->frame->data[1], s->palette, AVPALETTE_SIZE);\n if (s->new_palette) {\n s->frame->palette_has_changed = 1;\n s->new_palette = 0;\n }\n if ((ret = av_frame_ref(data, s->frame)) < 0)\n return ret;\n *got_frame = 1;\n return buf_size;\n}', 'int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)\n{\n AVFrame *tmp;\n int ret;\n av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);\n if (!frame->data[0])\n return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);\n if (av_frame_is_writable(frame))\n return ff_decode_frame_props(avctx, frame);\n tmp = av_frame_alloc();\n if (!tmp)\n return AVERROR(ENOMEM);\n av_frame_move_ref(tmp, frame);\n ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);\n if (ret < 0) {\n av_frame_free(&tmp);\n return ret;\n }\n av_frame_copy(frame, tmp);\n av_frame_free(&tmp);\n return 0;\n}']
|
1,079 | 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 test_rand()\n{\n BIGNUM *bn = BN_new();\n int st = 0;\n if (bn == NULL)\n return 0;\n if (BN_rand(bn, 0, 0 , 0 )) {\n fprintf(stderr, "BN_rand1 gave a bad result.\\n");\n goto err;\n }\n if (BN_rand(bn, 0, 1 , 1 )) {\n fprintf(stderr, "BN_rand2 gave a bad result.\\n");\n goto err;\n }\n if (!BN_rand(bn, 1, 0 , 0 ) || !BN_is_word(bn, 1)) {\n fprintf(stderr, "BN_rand3 gave a bad result.\\n");\n goto err;\n }\n if (BN_rand(bn, 1, 1 , 0 )) {\n fprintf(stderr, "BN_rand4 gave a bad result.\\n");\n goto err;\n }\n if (!BN_rand(bn, 1, -1 , 1 ) || !BN_is_word(bn, 1)) {\n fprintf(stderr, "BN_rand5 gave a bad result.\\n");\n goto err;\n }\n if (!BN_rand(bn, 2, 1 , 0 ) || !BN_is_word(bn, 3)) {\n fprintf(stderr, "BN_rand6 gave a bad result.\\n");\n goto err;\n }\n st = 1;\nerr:\n BN_free(bn);\n return st;\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}', '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}']
|
1,080 | 0 |
https://github.com/openssl/openssl/blob/3a87a9b9db07f8d3c6d9aa7f20e01f053007a703/crypto/x509v3/v3_utl.c/#L504
|
static STACK *get_email(X509_NAME *name, GENERAL_NAMES *gens)
{
STACK *ret = NULL;
X509_NAME_ENTRY *ne;
ASN1_IA5STRING *email;
GENERAL_NAME *gen;
int i;
i = -1;
while((i = X509_NAME_get_index_by_NID(name,
NID_pkcs9_emailAddress, i)) >= 0) {
ne = X509_NAME_get_entry(name, i);
email = X509_NAME_ENTRY_get_data(ne);
if(!append_ia5(&ret, email)) return NULL;
}
for(i = 0; i < sk_GENERAL_NAME_num(gens); i++)
{
gen = sk_GENERAL_NAME_value(gens, i);
if(gen->type != GEN_EMAIL) continue;
if(!append_ia5(&ret, gen->d.ia5)) return NULL;
}
return ret;
}
|
['static STACK *get_email(X509_NAME *name, GENERAL_NAMES *gens)\n{\n\tSTACK *ret = NULL;\n\tX509_NAME_ENTRY *ne;\n\tASN1_IA5STRING *email;\n\tGENERAL_NAME *gen;\n\tint i;\n\ti = -1;\n\twhile((i = X509_NAME_get_index_by_NID(name,\n\t\t\t\t\t NID_pkcs9_emailAddress, i)) >= 0) {\n\t\tne = X509_NAME_get_entry(name, i);\n\t\temail = X509_NAME_ENTRY_get_data(ne);\n\t\tif(!append_ia5(&ret, email)) return NULL;\n\t}\n\tfor(i = 0; i < sk_GENERAL_NAME_num(gens); i++)\n\t{\n\t\tgen = sk_GENERAL_NAME_value(gens, i);\n\t\tif(gen->type != GEN_EMAIL) continue;\n\t\tif(!append_ia5(&ret, gen->d.ia5)) return NULL;\n\t}\n\treturn ret;\n}', 'int X509_NAME_get_index_by_NID(X509_NAME *name, int nid, int lastpos)\n\t{\n\tASN1_OBJECT *obj;\n\tobj=OBJ_nid2obj(nid);\n\tif (obj == NULL) return(-2);\n\treturn(X509_NAME_get_index_by_OBJ(name,obj,lastpos));\n\t}', 'ASN1_OBJECT *OBJ_nid2obj(int n)\n\t{\n\tADDED_OBJ ad,*adp;\n\tASN1_OBJECT ob;\n\tif ((n >= 0) && (n < NUM_NID))\n\t\t{\n\t\tif ((n != NID_undef) && (nid_objs[n].nid == NID_undef))\n\t\t\t{\n\t\t\tOBJerr(OBJ_F_OBJ_NID2OBJ,OBJ_R_UNKNOWN_NID);\n\t\t\treturn(NULL);\n\t\t\t}\n\t\treturn((ASN1_OBJECT *)&(nid_objs[n]));\n\t\t}\n\telse if (added == NULL)\n\t\treturn(NULL);\n\telse\n\t\t{\n\t\tad.type=ADDED_NID;\n\t\tad.obj= &ob;\n\t\tob.nid=n;\n\t\tadp=(ADDED_OBJ *)lh_retrieve(added,&ad);\n\t\tif (adp != NULL)\n\t\t\treturn(adp->obj);\n\t\telse\n\t\t\t{\n\t\t\tOBJerr(OBJ_F_OBJ_NID2OBJ,OBJ_R_UNKNOWN_NID);\n\t\t\treturn(NULL);\n\t\t\t}\n\t\t}\n\t}', 'X509_NAME_ENTRY *X509_NAME_get_entry(X509_NAME *name, int loc)\n\t{\n\tif(name == NULL || sk_X509_NAME_ENTRY_num(name->entries) <= loc\n\t || loc < 0)\n\t\treturn(NULL);\n\telse\n\t\treturn(sk_X509_NAME_ENTRY_value(name->entries,loc));\n\t}', 'int sk_num(const STACK *st)\n{\n\tif(st == NULL) return -1;\n\treturn st->num;\n}', 'char *sk_value(const STACK *st, int i)\n{\n\tif(!st || (i < 0) || (i >= st->num)) return NULL;\n\treturn st->data[i];\n}', 'ASN1_STRING *X509_NAME_ENTRY_get_data(X509_NAME_ENTRY *ne)\n\t{\n\tif (ne == NULL) return(NULL);\n\treturn(ne->value);\n\t}', 'static int append_ia5(STACK **sk, ASN1_IA5STRING *email)\n{\n\tchar *emtmp;\n\tif(email->type != V_ASN1_IA5STRING) return 1;\n\tif(!email->data || !email->length) return 1;\n\tif(!*sk) *sk = sk_new(sk_strcmp);\n\tif(!*sk) return 0;\n\tif(sk_find(*sk, (char *)email->data) != -1) return 1;\n\temtmp = BUF_strdup((char *)email->data);\n\tif(!emtmp || !sk_push(*sk, emtmp)) {\n\t\tX509_email_free(*sk);\n\t\t*sk = NULL;\n\t\treturn 0;\n\t}\n\treturn 1;\n}']
|
1,081 | 0 |
https://gitlab.com/libtiff/libtiff/blob/537cd1da1856d734a353511e063f755d77aae927/tools/tiffcp.c/#L1167
|
static void
cpStripToTile(uint8* out, uint8* in,
uint32 rows, uint32 cols, int outskew, int64 inskew)
{
while (rows-- > 0) {
uint32 j = cols;
while (j-- > 0)
*out++ = *in++;
out += outskew;
in += inskew;
}
}
|
['static void\ncpStripToTile(uint8* out, uint8* in,\n uint32 rows, uint32 cols, int outskew, int64 inskew)\n{\n\twhile (rows-- > 0) {\n\t\tuint32 j = cols;\n\t\twhile (j-- > 0)\n\t\t\t*out++ = *in++;\n\t\tout += outskew;\n\t\tin += inskew;\n\t}\n}']
|
1,082 | 0 |
https://github.com/openssl/openssl/blob/e3713c365c2657236439fea00822a43aa396d112/crypto/mem.c/#L303
|
void CRYPTO_free(void *str, const char *file, int line)
{
INCREMENT(free_count);
if (free_impl != NULL && free_impl != &CRYPTO_free) {
free_impl(str, file, line);
return;
}
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
if (call_malloc_debug) {
CRYPTO_mem_debug_free(str, 0, file, line);
free(str);
CRYPTO_mem_debug_free(str, 1, file, line);
} else {
free(str);
}
#else
free(str);
#endif
}
|
['static int test_SU_stack(void)\n{\n STACK_OF(SU) *s = sk_SU_new_null();\n SU v[10];\n const int n = OSSL_NELEM(v);\n int i;\n int testresult = 0;\n for (i = 0; i < n; i++) {\n if ((i & 1) == 0)\n v[i].n = i;\n else\n v[i].c = \'A\' + i;\n if (!TEST_int_eq(sk_SU_num(s), i)) {\n TEST_info("SU stack size %d", i);\n goto end;\n }\n sk_SU_push(s, v + i);\n }\n if (!TEST_int_eq(sk_SU_num(s), n))\n goto end;\n for (i = 0; i < n; i++)\n if (!TEST_ptr_eq(sk_SU_value(s, i), v + i)) {\n TEST_info("SU pointer check %d", i);\n goto end;\n }\n testresult = 1;\nend:\n sk_SU_free(s);\n return testresult;\n}', 'DEFINE_STACK_OF_CONST(SU)', 'int OPENSSL_sk_push(OPENSSL_STACK *st, const void *data)\n{\n if (st == NULL)\n return -1;\n return OPENSSL_sk_insert(st, data, st->num);\n}', 'int OPENSSL_sk_insert(OPENSSL_STACK *st, const void *data, int loc)\n{\n if (st == NULL || st->num == max_nodes)\n return 0;\n if (!sk_reserve(st, 1, 0))\n return 0;\n if ((loc >= st->num) || (loc < 0)) {\n st->data[st->num] = data;\n } else {\n memmove(&st->data[loc + 1], &st->data[loc],\n sizeof(st->data[0]) * (st->num - loc));\n st->data[loc] = data;\n }\n st->num++;\n st->sorted = 0;\n return st->num;\n}', 'static int sk_reserve(OPENSSL_STACK *st, int n, int exact)\n{\n const void **tmpdata;\n int num_alloc;\n if (n > max_nodes - st->num)\n return 0;\n num_alloc = st->num + n;\n if (num_alloc < min_nodes)\n num_alloc = min_nodes;\n if (st->data == NULL) {\n st->data = OPENSSL_zalloc(sizeof(void *) * num_alloc);\n if (st->data == NULL)\n return 0;\n st->num_alloc = num_alloc;\n return 1;\n }\n if (!exact) {\n if (num_alloc <= st->num_alloc)\n return 1;\n num_alloc = compute_growth(num_alloc, st->num_alloc);\n if (num_alloc == 0)\n return 0;\n } else if (num_alloc == st->num_alloc) {\n return 1;\n }\n tmpdata = OPENSSL_realloc((void *)st->data, sizeof(void *) * num_alloc);\n if (tmpdata == NULL)\n return 0;\n st->data = tmpdata;\n st->num_alloc = num_alloc;\n return 1;\n}', 'void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)\n{\n INCREMENT(realloc_count);\n if (realloc_impl != NULL && realloc_impl != &CRYPTO_realloc)\n return realloc_impl(str, num, file, line);\n FAILTEST();\n if (str == NULL)\n return CRYPTO_malloc(num, file, line);\n if (num == 0) {\n CRYPTO_free(str, file, line);\n return NULL;\n }\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n void *ret;\n CRYPTO_mem_debug_realloc(str, NULL, num, 0, file, line);\n ret = realloc(str, num);\n CRYPTO_mem_debug_realloc(str, ret, num, 1, file, line);\n return ret;\n }\n#else\n (void)(file); (void)(line);\n#endif\n return realloc(str, num);\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}']
|
1,083 | 0 |
https://github.com/openssl/openssl/blob/5850cc75ea0c1581a9034390f1ca77cadc596238/crypto/bn/bn_ctx.c/#L328
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['BN_BLINDING *BN_BLINDING_create_param(BN_BLINDING *b,\n const BIGNUM *e, BIGNUM *m, BN_CTX *ctx,\n int (*bn_mod_exp) (BIGNUM *r,\n const BIGNUM *a,\n const BIGNUM *p,\n const BIGNUM *m,\n BN_CTX *ctx,\n BN_MONT_CTX *m_ctx),\n BN_MONT_CTX *m_ctx)\n{\n int retry_counter = 32;\n BN_BLINDING *ret = NULL;\n if (b == NULL)\n ret = BN_BLINDING_new(NULL, NULL, m);\n else\n ret = b;\n if (ret == NULL)\n goto err;\n if (ret->A == NULL && (ret->A = BN_new()) == NULL)\n goto err;\n if (ret->Ai == NULL && (ret->Ai = BN_new()) == NULL)\n goto err;\n if (e != NULL) {\n BN_free(ret->e);\n ret->e = BN_dup(e);\n }\n if (ret->e == NULL)\n goto err;\n if (bn_mod_exp != NULL)\n ret->bn_mod_exp = bn_mod_exp;\n if (m_ctx != NULL)\n ret->m_ctx = m_ctx;\n do {\n int rv;\n if (!BN_rand_range(ret->A, ret->mod))\n goto err;\n if (!int_bn_mod_inverse(ret->Ai, ret->A, ret->mod, ctx, &rv)) {\n if (rv) {\n if (retry_counter-- == 0) {\n BNerr(BN_F_BN_BLINDING_CREATE_PARAM,\n BN_R_TOO_MANY_ITERATIONS);\n goto err;\n }\n } else\n goto err;\n } else\n break;\n } while (1);\n if (ret->bn_mod_exp != NULL && ret->m_ctx != NULL) {\n if (!ret->bn_mod_exp\n (ret->A, ret->A, ret->e, ret->mod, ctx, ret->m_ctx))\n goto err;\n } else {\n if (!BN_mod_exp(ret->A, ret->A, ret->e, ret->mod, ctx))\n goto err;\n }\n return ret;\n err:\n if (b == NULL) {\n BN_BLINDING_free(ret);\n ret = NULL;\n }\n return ret;\n}', 'BIGNUM *int_bn_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,\n int *pnoinv)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM *ret = NULL;\n int sign;\n if (pnoinv)\n *pnoinv = 0;\n if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {\n return BN_mod_inverse_no_branch(in, a, n, ctx);\n }\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n if (!BN_nnmod(B, B, A, ctx))\n goto err;\n }\n sign = -1;\n if (BN_is_odd(n) && (BN_num_bits(n) <= (BN_BITS <= 32 ? 450 : 2048))) {\n int shift;\n while (!BN_is_zero(B)) {\n shift = 0;\n while (!BN_is_bit_set(B, shift)) {\n shift++;\n if (BN_is_odd(X)) {\n if (!BN_uadd(X, X, n))\n goto err;\n }\n if (!BN_rshift1(X, X))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(B, B, shift))\n goto err;\n }\n shift = 0;\n while (!BN_is_bit_set(A, shift)) {\n shift++;\n if (BN_is_odd(Y)) {\n if (!BN_uadd(Y, Y, n))\n goto err;\n }\n if (!BN_rshift1(Y, Y))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(A, A, shift))\n goto err;\n }\n if (BN_ucmp(B, A) >= 0) {\n if (!BN_uadd(X, X, Y))\n goto err;\n if (!BN_usub(B, B, A))\n goto err;\n } else {\n if (!BN_uadd(Y, Y, X))\n goto err;\n if (!BN_usub(A, A, B))\n goto err;\n }\n }\n } else {\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n if (BN_num_bits(A) == BN_num_bits(B)) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else if (BN_num_bits(A) == BN_num_bits(B) + 1) {\n if (!BN_lshift1(T, B))\n goto err;\n if (BN_ucmp(A, T) < 0) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else {\n if (!BN_sub(M, A, T))\n goto err;\n if (!BN_add(D, T, B))\n goto err;\n if (BN_ucmp(A, D) < 0) {\n if (!BN_set_word(D, 2))\n goto err;\n } else {\n if (!BN_set_word(D, 3))\n goto err;\n if (!BN_sub(M, M, B))\n goto err;\n }\n }\n } else {\n if (!BN_div(D, M, A, B, ctx))\n goto err;\n }\n tmp = A;\n A = B;\n B = M;\n if (BN_is_one(D)) {\n if (!BN_add(tmp, X, Y))\n goto err;\n } else {\n if (BN_is_word(D, 2)) {\n if (!BN_lshift1(tmp, X))\n goto err;\n } else if (BN_is_word(D, 4)) {\n if (!BN_lshift(tmp, X, 2))\n goto err;\n } else if (D->top == 1) {\n if (!BN_copy(tmp, X))\n goto err;\n if (!BN_mul_word(tmp, D->d[0]))\n goto err;\n } else {\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n }\n if (!BN_add(tmp, tmp, Y))\n goto err;\n }\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n if (pnoinv)\n *pnoinv = 1;\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return (ret);\n}', 'static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n,\n BN_CTX *ctx)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM local_A, local_B;\n BIGNUM *pA, *pB;\n BIGNUM *ret = NULL;\n int sign;\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n pB = &local_B;\n local_B.flags = 0;\n BN_with_flags(pB, B, BN_FLG_CONSTTIME);\n if (!BN_nnmod(B, pB, A, ctx))\n goto err;\n }\n sign = -1;\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n pA = &local_A;\n local_A.flags = 0;\n BN_with_flags(pA, A, BN_FLG_CONSTTIME);\n if (!BN_div(D, M, pA, B, ctx))\n goto err;\n tmp = A;\n A = B;\n B = M;\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n if (!BN_add(tmp, tmp, Y))\n goto err;\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n BNerr(BN_F_BN_MOD_INVERSE_NO_BRANCH, BN_R_NO_INVERSE);\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return (ret);\n}', '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 tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (dv == NULL)\n res = BN_CTX_get(ctx);\n else\n res = dv;\n if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n res->neg = (num->neg ^ divisor->neg);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--, resp--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# ifdef BN_DEBUG_LEVITTE\n fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n", n0, n1, d0, q);\n# endif\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifdef BN_DEBUG_LEVITTE\n fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n", n0, n1, d0, q);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return (1);\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return (0);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
|
1,084 | 0 |
https://github.com/libav/libav/blob/ea5fa19427fdaba332b908758f5779add7084bbe/libavcodec/wmaenc.c/#L54
|
static int encode_init(AVCodecContext * avctx){
WMACodecContext *s = avctx->priv_data;
int i, flags1, flags2;
uint8_t *extradata;
s->avctx = avctx;
if(avctx->channels > MAX_CHANNELS) {
av_log(avctx, AV_LOG_ERROR, "too many channels: got %i, need %i or fewer",
avctx->channels, MAX_CHANNELS);
return AVERROR(EINVAL);
}
if(avctx->bit_rate < 24*1000) {
av_log(avctx, AV_LOG_ERROR, "bitrate too low: got %i, need 24000 or higher\n",
avctx->bit_rate);
return AVERROR(EINVAL);
}
flags1 = 0;
flags2 = 1;
if (avctx->codec->id == CODEC_ID_WMAV1) {
extradata= av_malloc(4);
avctx->extradata_size= 4;
AV_WL16(extradata, flags1);
AV_WL16(extradata+2, flags2);
} else if (avctx->codec->id == CODEC_ID_WMAV2) {
extradata= av_mallocz(10);
avctx->extradata_size= 10;
AV_WL32(extradata, flags1);
AV_WL16(extradata+4, flags2);
}else
assert(0);
avctx->extradata= extradata;
s->use_exp_vlc = flags2 & 0x0001;
s->use_bit_reservoir = flags2 & 0x0002;
s->use_variable_block_len = flags2 & 0x0004;
ff_wma_init(avctx, flags2);
for(i = 0; i < s->nb_block_sizes; i++)
ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 0, 1.0);
avctx->block_align=
s->block_align= avctx->bit_rate*(int64_t)s->frame_len / (avctx->sample_rate*8);
avctx->frame_size= s->frame_len;
return 0;
}
|
['static int encode_init(AVCodecContext * avctx){\n WMACodecContext *s = avctx->priv_data;\n int i, flags1, flags2;\n uint8_t *extradata;\n s->avctx = avctx;\n if(avctx->channels > MAX_CHANNELS) {\n av_log(avctx, AV_LOG_ERROR, "too many channels: got %i, need %i or fewer",\n avctx->channels, MAX_CHANNELS);\n return AVERROR(EINVAL);\n }\n if(avctx->bit_rate < 24*1000) {\n av_log(avctx, AV_LOG_ERROR, "bitrate too low: got %i, need 24000 or higher\\n",\n avctx->bit_rate);\n return AVERROR(EINVAL);\n }\n flags1 = 0;\n flags2 = 1;\n if (avctx->codec->id == CODEC_ID_WMAV1) {\n extradata= av_malloc(4);\n avctx->extradata_size= 4;\n AV_WL16(extradata, flags1);\n AV_WL16(extradata+2, flags2);\n } else if (avctx->codec->id == CODEC_ID_WMAV2) {\n extradata= av_mallocz(10);\n avctx->extradata_size= 10;\n AV_WL32(extradata, flags1);\n AV_WL16(extradata+4, flags2);\n }else\n assert(0);\n avctx->extradata= extradata;\n s->use_exp_vlc = flags2 & 0x0001;\n s->use_bit_reservoir = flags2 & 0x0002;\n s->use_variable_block_len = flags2 & 0x0004;\n ff_wma_init(avctx, flags2);\n for(i = 0; i < s->nb_block_sizes; i++)\n ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 0, 1.0);\n avctx->block_align=\n s->block_align= avctx->bit_rate*(int64_t)s->frame_len / (avctx->sample_rate*8);\n avctx->frame_size= s->frame_len;\n return 0;\n}', 'void *av_malloc(size_t size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-32) )\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size+32);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&31) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr,32,size))\n ptr = NULL;\n#elif HAVE_MEMALIGN\n ptr = memalign(32,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}']
|
1,085 | 0 |
https://github.com/openssl/openssl/blob/06a2b07bb07d4dad4f00358f4723bbe277e03dc7/crypto/x509/x509_trs.c/#L118
|
int X509_check_trust(X509 *x, int id, int flags)
{
X509_TRUST *pt;
int idx;
if(id == -1) return 1;
idx = X509_TRUST_get_by_id(id);
if(idx == -1) return default_trust(id, x, flags);
pt = X509_TRUST_get0(idx);
return pt->check_trust(pt, x, flags);
}
|
['int X509_check_trust(X509 *x, int id, int flags)\n{\n\tX509_TRUST *pt;\n\tint idx;\n\tif(id == -1) return 1;\n\tidx = X509_TRUST_get_by_id(id);\n\tif(idx == -1) return default_trust(id, x, flags);\n\tpt = X509_TRUST_get0(idx);\n\treturn pt->check_trust(pt, x, flags);\n}', 'int X509_TRUST_get_by_id(int id)\n{\n\tX509_TRUST tmp;\n\tint idx;\n\tif((id >= X509_TRUST_MIN) && (id <= X509_TRUST_MAX))\n\t\t\t\t return id - X509_TRUST_MIN;\n\ttmp.trust = id;\n\tif(!trtable) return -1;\n\tidx = sk_X509_TRUST_find(trtable, &tmp);\n\tif(idx == -1) return -1;\n\treturn idx + X509_TRUST_COUNT;\n}', 'X509_TRUST * X509_TRUST_get0(int idx)\n{\n\tif(idx < 0) return NULL;\n\tif(idx < X509_TRUST_COUNT) return trstandard + idx;\n\treturn sk_X509_TRUST_value(trtable, idx - X509_TRUST_COUNT);\n}']
|
1,086 | 0 |
https://github.com/openssl/openssl/blob/1275c4569e048280dd423b6231a92b4a4fde97e2/crypto/bn/bn_lib.c/#L229
|
int BN_num_bits_word(BN_ULONG l)
{
static const char bits[256]={
0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
};
#if defined(SIXTY_FOUR_BIT_LONG)
if (l & 0xffffffff00000000L)
{
if (l & 0xffff000000000000L)
{
if (l & 0xff00000000000000L)
{
return(bits[(int)(l>>56)]+56);
}
else return(bits[(int)(l>>48)]+48);
}
else
{
if (l & 0x0000ff0000000000L)
{
return(bits[(int)(l>>40)]+40);
}
else return(bits[(int)(l>>32)]+32);
}
}
else
#else
#ifdef SIXTY_FOUR_BIT
if (l & 0xffffffff00000000LL)
{
if (l & 0xffff000000000000LL)
{
if (l & 0xff00000000000000LL)
{
return(bits[(int)(l>>56)]+56);
}
else return(bits[(int)(l>>48)]+48);
}
else
{
if (l & 0x0000ff0000000000LL)
{
return(bits[(int)(l>>40)]+40);
}
else return(bits[(int)(l>>32)]+32);
}
}
else
#endif
#endif
{
#if defined(THIRTY_TWO_BIT) || defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
if (l & 0xffff0000L)
{
if (l & 0xff000000L)
return(bits[(int)(l>>24L)]+24);
else return(bits[(int)(l>>16L)]+16);
}
else
#endif
{
#if defined(SIXTEEN_BIT) || defined(THIRTY_TWO_BIT) || defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
if (l & 0xff00L)
return(bits[(int)(l>>8)]+8);
else
#endif
return(bits[(int)(l )] );
}
}
}
|
["char *BN_bn2dec(const BIGNUM *a)\n\t{\n\tint i=0,num;\n\tchar *buf=NULL;\n\tchar *p;\n\tBIGNUM *t=NULL;\n\tBN_ULONG *bn_data=NULL,*lp;\n\ti=BN_num_bits(a)*3;\n\tnum=(i/10+i/1000+3)+1;\n\tbn_data=(BN_ULONG *)OPENSSL_malloc((num/BN_DEC_NUM+1)*sizeof(BN_ULONG));\n\tbuf=(char *)OPENSSL_malloc(num+3);\n\tif ((buf == NULL) || (bn_data == NULL))\n\t\t{\n\t\tBNerr(BN_F_BN_BN2DEC,ERR_R_MALLOC_FAILURE);\n\t\tgoto err;\n\t\t}\n\tif ((t=BN_dup(a)) == NULL) goto err;\n#define BUF_REMAIN (num+3 - (size_t)(p - buf))\n\tp=buf;\n\tlp=bn_data;\n\tif (t->neg) *(p++)='-';\n\tif (BN_is_zero(t))\n\t\t{\n\t\t*(p++)='0';\n\t\t*(p++)='\\0';\n\t\t}\n\telse\n\t\t{\n\t\ti=0;\n\t\twhile (!BN_is_zero(t))\n\t\t\t{\n\t\t\t*lp=BN_div_word(t,BN_DEC_CONV);\n\t\t\tlp++;\n\t\t\t}\n\t\tlp--;\n\t\tBIO_snprintf(p,BUF_REMAIN,BN_DEC_FMT1,*lp);\n\t\twhile (*p) p++;\n\t\twhile (lp != bn_data)\n\t\t\t{\n\t\t\tlp--;\n\t\t\tBIO_snprintf(p,BUF_REMAIN,BN_DEC_FMT2,*lp);\n\t\t\twhile (*p) p++;\n\t\t\t}\n\t\t}\nerr:\n\tif (bn_data != NULL) OPENSSL_free(bn_data);\n\tif (t != NULL) BN_free(t);\n\treturn(buf);\n\t}", 'BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w)\n\t{\n\tBN_ULONG ret = 0;\n\tint i, j;\n\tbn_check_top(a);\n\tw &= BN_MASK2;\n\tif (!w)\n\t\treturn 0;\n\tif (a->top == 0)\n\t\treturn 0;\n\tj = BN_BITS2 - BN_num_bits_word(w);\n\tw <<= j;\n\tif (!BN_lshift(a, a, j))\n\t\treturn 0;\n\tfor (i=a->top-1; i>=0; i--)\n\t\t{\n\t\tBN_ULONG l,d;\n\t\tl=a->d[i];\n\t\td=bn_div_words(ret,l,w);\n\t\tret=(l-((d*w)&BN_MASK2))&BN_MASK2;\n\t\ta->d[i]=d;\n\t\t}\n\tif ((a->top > 0) && (a->d[a->top-1] == 0))\n\t\ta->top--;\n\tret >>= j;\n\tbn_check_top(a);\n\treturn(ret);\n\t}', 'int BN_num_bits_word(BN_ULONG l)\n\t{\n\tstatic const char bits[256]={\n\t\t0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,\n\t\t5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,\n\t\t6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,\n\t\t6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,\n\t\t7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,\n\t\t7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,\n\t\t7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,\n\t\t7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,\n\t\t8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,\n\t\t8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,\n\t\t8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,\n\t\t8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,\n\t\t8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,\n\t\t8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,\n\t\t8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,\n\t\t8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,\n\t\t};\n#if defined(SIXTY_FOUR_BIT_LONG)\n\tif (l & 0xffffffff00000000L)\n\t\t{\n\t\tif (l & 0xffff000000000000L)\n\t\t\t{\n\t\t\tif (l & 0xff00000000000000L)\n\t\t\t\t{\n\t\t\t\treturn(bits[(int)(l>>56)]+56);\n\t\t\t\t}\n\t\t\telse\treturn(bits[(int)(l>>48)]+48);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (l & 0x0000ff0000000000L)\n\t\t\t\t{\n\t\t\t\treturn(bits[(int)(l>>40)]+40);\n\t\t\t\t}\n\t\t\telse\treturn(bits[(int)(l>>32)]+32);\n\t\t\t}\n\t\t}\n\telse\n#else\n#ifdef SIXTY_FOUR_BIT\n\tif (l & 0xffffffff00000000LL)\n\t\t{\n\t\tif (l & 0xffff000000000000LL)\n\t\t\t{\n\t\t\tif (l & 0xff00000000000000LL)\n\t\t\t\t{\n\t\t\t\treturn(bits[(int)(l>>56)]+56);\n\t\t\t\t}\n\t\t\telse\treturn(bits[(int)(l>>48)]+48);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (l & 0x0000ff0000000000LL)\n\t\t\t\t{\n\t\t\t\treturn(bits[(int)(l>>40)]+40);\n\t\t\t\t}\n\t\t\telse\treturn(bits[(int)(l>>32)]+32);\n\t\t\t}\n\t\t}\n\telse\n#endif\n#endif\n\t\t{\n#if defined(THIRTY_TWO_BIT) || defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)\n\t\tif (l & 0xffff0000L)\n\t\t\t{\n\t\t\tif (l & 0xff000000L)\n\t\t\t\treturn(bits[(int)(l>>24L)]+24);\n\t\t\telse\treturn(bits[(int)(l>>16L)]+16);\n\t\t\t}\n\t\telse\n#endif\n\t\t\t{\n#if defined(SIXTEEN_BIT) || defined(THIRTY_TWO_BIT) || defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)\n\t\t\tif (l & 0xff00L)\n\t\t\t\treturn(bits[(int)(l>>8)]+8);\n\t\t\telse\n#endif\n\t\t\t\treturn(bits[(int)(l )] );\n\t\t\t}\n\t\t}\n\t}']
|
1,087 | 0 |
https://github.com/libav/libav/blob/710b0e27025948b7511821c2f888ff2d74a59e14/avconv_opt.c/#L826
|
static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, enum AVMediaType type)
{
OutputStream *ost;
AVStream *st = avformat_new_stream(oc, NULL);
int idx = oc->nb_streams - 1, ret = 0;
char *bsf = NULL, *next, *codec_tag = NULL;
AVBitStreamFilterContext *bsfc, *bsfc_prev = NULL;
double qscale = -1;
if (!st) {
av_log(NULL, AV_LOG_FATAL, "Could not alloc stream.\n");
exit_program(1);
}
if (oc->nb_streams - 1 < o->nb_streamid_map)
st->id = o->streamid_map[oc->nb_streams - 1];
GROW_ARRAY(output_streams, nb_output_streams);
if (!(ost = av_mallocz(sizeof(*ost))))
exit_program(1);
output_streams[nb_output_streams - 1] = ost;
ost->file_index = nb_output_files - 1;
ost->index = idx;
ost->st = st;
st->codec->codec_type = type;
choose_encoder(o, oc, ost);
if (ost->enc) {
AVIOContext *s = NULL;
char *buf = NULL, *arg = NULL, *preset = NULL;
ost->opts = filter_codec_opts(o->g->codec_opts, ost->enc->id, oc, st, ost->enc);
MATCH_PER_STREAM_OPT(presets, str, preset, oc, st);
if (preset && (!(ret = get_preset_file_2(preset, ost->enc->name, &s)))) {
do {
buf = get_line(s);
if (!buf[0] || buf[0] == '#') {
av_free(buf);
continue;
}
if (!(arg = strchr(buf, '='))) {
av_log(NULL, AV_LOG_FATAL, "Invalid line found in the preset file.\n");
exit_program(1);
}
*arg++ = 0;
av_dict_set(&ost->opts, buf, arg, AV_DICT_DONT_OVERWRITE);
av_free(buf);
} while (!s->eof_reached);
avio_close(s);
}
if (ret) {
av_log(NULL, AV_LOG_FATAL,
"Preset %s specified for stream %d:%d, but could not be opened.\n",
preset, ost->file_index, ost->index);
exit_program(1);
}
} else {
ost->opts = filter_codec_opts(o->g->codec_opts, AV_CODEC_ID_NONE, oc, st, NULL);
}
avcodec_get_context_defaults3(st->codec, ost->enc);
st->codec->codec_type = type;
ost->max_frames = INT64_MAX;
MATCH_PER_STREAM_OPT(max_frames, i64, ost->max_frames, oc, st);
MATCH_PER_STREAM_OPT(bitstream_filters, str, bsf, oc, st);
while (bsf) {
if (next = strchr(bsf, ','))
*next++ = 0;
if (!(bsfc = av_bitstream_filter_init(bsf))) {
av_log(NULL, AV_LOG_FATAL, "Unknown bitstream filter %s\n", bsf);
exit_program(1);
}
if (bsfc_prev)
bsfc_prev->next = bsfc;
else
ost->bitstream_filters = bsfc;
bsfc_prev = bsfc;
bsf = next;
}
MATCH_PER_STREAM_OPT(codec_tags, str, codec_tag, oc, st);
if (codec_tag) {
uint32_t tag = strtol(codec_tag, &next, 0);
if (*next)
tag = AV_RL32(codec_tag);
st->codec->codec_tag = tag;
}
MATCH_PER_STREAM_OPT(qscale, dbl, qscale, oc, st);
if (qscale >= 0) {
st->codec->flags |= CODEC_FLAG_QSCALE;
st->codec->global_quality = FF_QP2LAMBDA * qscale;
}
if (oc->oformat->flags & AVFMT_GLOBALHEADER)
st->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
av_opt_get_int(o->g->sws_opts, "sws_flags", 0, &ost->sws_flags);
av_dict_copy(&ost->resample_opts, o->g->resample_opts, 0);
ost->pix_fmts[0] = ost->pix_fmts[1] = AV_PIX_FMT_NONE;
ost->last_mux_dts = AV_NOPTS_VALUE;
return ost;
}
|
['static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, enum AVMediaType type)\n{\n OutputStream *ost;\n AVStream *st = avformat_new_stream(oc, NULL);\n int idx = oc->nb_streams - 1, ret = 0;\n char *bsf = NULL, *next, *codec_tag = NULL;\n AVBitStreamFilterContext *bsfc, *bsfc_prev = NULL;\n double qscale = -1;\n if (!st) {\n av_log(NULL, AV_LOG_FATAL, "Could not alloc stream.\\n");\n exit_program(1);\n }\n if (oc->nb_streams - 1 < o->nb_streamid_map)\n st->id = o->streamid_map[oc->nb_streams - 1];\n GROW_ARRAY(output_streams, nb_output_streams);\n if (!(ost = av_mallocz(sizeof(*ost))))\n exit_program(1);\n output_streams[nb_output_streams - 1] = ost;\n ost->file_index = nb_output_files - 1;\n ost->index = idx;\n ost->st = st;\n st->codec->codec_type = type;\n choose_encoder(o, oc, ost);\n if (ost->enc) {\n AVIOContext *s = NULL;\n char *buf = NULL, *arg = NULL, *preset = NULL;\n ost->opts = filter_codec_opts(o->g->codec_opts, ost->enc->id, oc, st, ost->enc);\n MATCH_PER_STREAM_OPT(presets, str, preset, oc, st);\n if (preset && (!(ret = get_preset_file_2(preset, ost->enc->name, &s)))) {\n do {\n buf = get_line(s);\n if (!buf[0] || buf[0] == \'#\') {\n av_free(buf);\n continue;\n }\n if (!(arg = strchr(buf, \'=\'))) {\n av_log(NULL, AV_LOG_FATAL, "Invalid line found in the preset file.\\n");\n exit_program(1);\n }\n *arg++ = 0;\n av_dict_set(&ost->opts, buf, arg, AV_DICT_DONT_OVERWRITE);\n av_free(buf);\n } while (!s->eof_reached);\n avio_close(s);\n }\n if (ret) {\n av_log(NULL, AV_LOG_FATAL,\n "Preset %s specified for stream %d:%d, but could not be opened.\\n",\n preset, ost->file_index, ost->index);\n exit_program(1);\n }\n } else {\n ost->opts = filter_codec_opts(o->g->codec_opts, AV_CODEC_ID_NONE, oc, st, NULL);\n }\n avcodec_get_context_defaults3(st->codec, ost->enc);\n st->codec->codec_type = type;\n ost->max_frames = INT64_MAX;\n MATCH_PER_STREAM_OPT(max_frames, i64, ost->max_frames, oc, st);\n MATCH_PER_STREAM_OPT(bitstream_filters, str, bsf, oc, st);\n while (bsf) {\n if (next = strchr(bsf, \',\'))\n *next++ = 0;\n if (!(bsfc = av_bitstream_filter_init(bsf))) {\n av_log(NULL, AV_LOG_FATAL, "Unknown bitstream filter %s\\n", bsf);\n exit_program(1);\n }\n if (bsfc_prev)\n bsfc_prev->next = bsfc;\n else\n ost->bitstream_filters = bsfc;\n bsfc_prev = bsfc;\n bsf = next;\n }\n MATCH_PER_STREAM_OPT(codec_tags, str, codec_tag, oc, st);\n if (codec_tag) {\n uint32_t tag = strtol(codec_tag, &next, 0);\n if (*next)\n tag = AV_RL32(codec_tag);\n st->codec->codec_tag = tag;\n }\n MATCH_PER_STREAM_OPT(qscale, dbl, qscale, oc, st);\n if (qscale >= 0) {\n st->codec->flags |= CODEC_FLAG_QSCALE;\n st->codec->global_quality = FF_QP2LAMBDA * qscale;\n }\n if (oc->oformat->flags & AVFMT_GLOBALHEADER)\n st->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;\n av_opt_get_int(o->g->sws_opts, "sws_flags", 0, &ost->sws_flags);\n av_dict_copy(&ost->resample_opts, o->g->resample_opts, 0);\n ost->pix_fmts[0] = ost->pix_fmts[1] = AV_PIX_FMT_NONE;\n ost->last_mux_dts = AV_NOPTS_VALUE;\n return ost;\n}']
|
1,088 | 0 |
https://github.com/openssl/openssl/blob/fa9bb6201e1d16ba8ccab938833d140ef81a7f73/crypto/evp/p_sign.c/#L88
|
int EVP_SignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
unsigned int *siglen, EVP_PKEY *pkey)
{
unsigned char m[EVP_MAX_MD_SIZE];
unsigned int m_len = 0;
int i = 0;
size_t sltmp;
EVP_PKEY_CTX *pkctx = NULL;
*siglen = 0;
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_SIGNFINAL, 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;
}
sltmp = (size_t)EVP_PKEY_size(pkey);
i = 0;
pkctx = EVP_PKEY_CTX_new(pkey, NULL);
if (pkctx == NULL)
goto err;
if (EVP_PKEY_sign_init(pkctx) <= 0)
goto err;
if (EVP_PKEY_CTX_set_signature_md(pkctx, EVP_MD_CTX_md(ctx)) <= 0)
goto err;
if (EVP_PKEY_sign(pkctx, sigret, &sltmp, m, m_len) <= 0)
goto err;
*siglen = sltmp;
i = 1;
err:
EVP_PKEY_CTX_free(pkctx);
return i;
}
|
['int EVP_SignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,\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 size_t sltmp;\n EVP_PKEY_CTX *pkctx = NULL;\n *siglen = 0;\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_SIGNFINAL, 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 sltmp = (size_t)EVP_PKEY_size(pkey);\n i = 0;\n pkctx = EVP_PKEY_CTX_new(pkey, NULL);\n if (pkctx == NULL)\n goto err;\n if (EVP_PKEY_sign_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 if (EVP_PKEY_sign(pkctx, sigret, &sltmp, m, m_len) <= 0)\n goto err;\n *siglen = sltmp;\n i = 1;\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 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 (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_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 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);\n ret = engine_unlocked_init(e);\n CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);\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}', 'int engine_unlocked_init(ENGINE *e)\n{\n int to_return = 1;\n if ((e->funct_ref == 0) && e->init)\n to_return = e->init(e);\n if (to_return) {\n e->struct_ref++;\n e->funct_ref++;\n engine_ref_debug(e, 0, 1);\n engine_ref_debug(e, 1, 1);\n }\n return to_return;\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}']
|
1,089 | 0 |
https://github.com/openssl/openssl/blob/4ebb5293fc7ae1fbb7c5cd8bbe114049bcd8685e/engines/e_4758_cca.c/#L751
|
static int cca_rsa_sign(int type, const unsigned char *m, unsigned int m_len,
unsigned char *sigret, unsigned int *siglen, const RSA *rsa)
{
long returnCode;
long reasonCode;
long exitDataLength = 0;
unsigned char exitData[8];
long ruleArrayLength = 1;
unsigned char ruleArray[8] = "PKCS-1.1";
long outputLength=256;
long outputBitLength;
long keyTokenLength;
unsigned char *hashBuffer = NULL;
unsigned char* keyToken = (unsigned char*)RSA_get_ex_data(rsa, hndidx);
long length = SSL_SIG_LEN;
long keyLength ;
X509_SIG sig;
ASN1_TYPE parameter;
X509_ALGOR algorithm;
ASN1_OCTET_STRING digest;
keyTokenLength = *(long*)keyToken;
keyToken+=sizeof(long);
if (type == NID_md5 || type == NID_sha1)
{
sig.algor = &algorithm;
algorithm.algorithm = OBJ_nid2obj(type);
if (!algorithm.algorithm)
{
CCA4758err(CCA4758_F_IBM_4758_CCA_SIGN,
CCA4758_R_UNKNOWN_ALGORITHM_TYPE);
return 0;
}
if (!algorithm.algorithm->length)
{
CCA4758err(CCA4758_F_IBM_4758_CCA_SIGN,
CCA4758_R_ASN1_OID_UNKNOWN_FOR_MD);
return 0;
}
parameter.type = V_ASN1_NULL;
parameter.value.ptr = NULL;
algorithm.parameter = ¶meter;
sig.digest = &digest;
sig.digest->data = (unsigned char*)m;
sig.digest->length = m_len;
length = i2d_X509_SIG(&sig, NULL);
}
keyLength = RSA_size(rsa);
if (length - RSA_PKCS1_PADDING > keyLength)
{
CCA4758err(CCA4758_F_IBM_4758_CCA_SIGN,
CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
return 0;
}
switch (type)
{
case NID_md5_sha1 :
if (m_len != SSL_SIG_LEN)
{
CCA4758err(CCA4758_F_IBM_4758_CCA_SIGN,
CCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
return 0;
}
hashBuffer = (unsigned char*)m;
length = m_len;
break;
case NID_md5 :
{
unsigned char *ptr;
ptr = hashBuffer = OPENSSL_malloc(
(unsigned int)keyLength+1);
if (!hashBuffer)
{
CCA4758err(CCA4758_F_IBM_4758_CCA_VERIFY,
ERR_R_MALLOC_FAILURE);
return 0;
}
i2d_X509_SIG(&sig, &ptr);
}
break;
case NID_sha1 :
{
unsigned char *ptr;
ptr = hashBuffer = OPENSSL_malloc(
(unsigned int)keyLength+1);
if (!hashBuffer)
{
CCA4758err(CCA4758_F_IBM_4758_CCA_VERIFY,
ERR_R_MALLOC_FAILURE);
return 0;
}
i2d_X509_SIG(&sig, &ptr);
}
break;
default:
return 0;
}
digitalSignatureGenerate(&returnCode, &reasonCode, &exitDataLength,
exitData, &ruleArrayLength, ruleArray, &keyTokenLength,
keyToken, &length, hashBuffer, &outputLength, &outputBitLength,
sigret);
if (type == NID_sha1 || type == NID_md5)
{
OPENSSL_cleanse(hashBuffer, keyLength+1);
OPENSSL_free(hashBuffer);
}
*siglen = outputLength;
return ((returnCode || reasonCode) ? 0 : 1);
}
|
['static int cca_rsa_sign(int type, const unsigned char *m, unsigned int m_len,\n\t\tunsigned char *sigret, unsigned int *siglen, const RSA *rsa)\n\t{\n\tlong returnCode;\n\tlong reasonCode;\n\tlong exitDataLength = 0;\n\tunsigned char exitData[8];\n\tlong ruleArrayLength = 1;\n\tunsigned char ruleArray[8] = "PKCS-1.1";\n\tlong outputLength=256;\n\tlong outputBitLength;\n\tlong keyTokenLength;\n\tunsigned char *hashBuffer = NULL;\n\tunsigned char* keyToken = (unsigned char*)RSA_get_ex_data(rsa, hndidx);\n\tlong length = SSL_SIG_LEN;\n\tlong keyLength ;\n\tX509_SIG sig;\n\tASN1_TYPE parameter;\n\tX509_ALGOR algorithm;\n\tASN1_OCTET_STRING digest;\n\tkeyTokenLength = *(long*)keyToken;\n\tkeyToken+=sizeof(long);\n\tif (type == NID_md5 || type == NID_sha1)\n\t\t{\n\t\tsig.algor = &algorithm;\n\t\talgorithm.algorithm = OBJ_nid2obj(type);\n\t\tif (!algorithm.algorithm)\n\t\t\t{\n\t\t\tCCA4758err(CCA4758_F_IBM_4758_CCA_SIGN,\n\t\t\t\tCCA4758_R_UNKNOWN_ALGORITHM_TYPE);\n\t\t\treturn 0;\n\t\t\t}\n\t\tif (!algorithm.algorithm->length)\n\t\t\t{\n\t\t\tCCA4758err(CCA4758_F_IBM_4758_CCA_SIGN,\n\t\t\t\tCCA4758_R_ASN1_OID_UNKNOWN_FOR_MD);\n\t\t\treturn 0;\n\t\t\t}\n\t\tparameter.type = V_ASN1_NULL;\n\t\tparameter.value.ptr = NULL;\n\t\talgorithm.parameter = ¶meter;\n\t\tsig.digest = &digest;\n\t\tsig.digest->data = (unsigned char*)m;\n\t\tsig.digest->length = m_len;\n\t\tlength = i2d_X509_SIG(&sig, NULL);\n\t\t}\n\tkeyLength = RSA_size(rsa);\n\tif (length - RSA_PKCS1_PADDING > keyLength)\n\t\t{\n\t\tCCA4758err(CCA4758_F_IBM_4758_CCA_SIGN,\n\t\t\tCCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);\n\t\treturn 0;\n\t\t}\n\tswitch (type)\n\t\t{\n\t\tcase NID_md5_sha1 :\n\t\t\tif (m_len != SSL_SIG_LEN)\n\t\t\t\t{\n\t\t\t\tCCA4758err(CCA4758_F_IBM_4758_CCA_SIGN,\n\t\t\t\tCCA4758_R_SIZE_TOO_LARGE_OR_TOO_SMALL);\n\t\t\t\treturn 0;\n\t\t\t\t}\n\t\t\thashBuffer = (unsigned char*)m;\n\t\t\tlength = m_len;\n\t\t\tbreak;\n\t\tcase NID_md5 :\n\t\t\t{\n\t\t\tunsigned char *ptr;\n\t\t\tptr = hashBuffer = OPENSSL_malloc(\n\t\t\t\t\t(unsigned int)keyLength+1);\n\t\t\tif (!hashBuffer)\n\t\t\t\t{\n\t\t\t\tCCA4758err(CCA4758_F_IBM_4758_CCA_VERIFY,\n\t\t\t\t\t\tERR_R_MALLOC_FAILURE);\n\t\t\t\treturn 0;\n\t\t\t\t}\n\t\t\ti2d_X509_SIG(&sig, &ptr);\n\t\t\t}\n\t\t\tbreak;\n\t\tcase NID_sha1 :\n\t\t\t{\n\t\t\tunsigned char *ptr;\n\t\t\tptr = hashBuffer = OPENSSL_malloc(\n\t\t\t\t\t(unsigned int)keyLength+1);\n\t\t\tif (!hashBuffer)\n\t\t\t\t{\n\t\t\t\tCCA4758err(CCA4758_F_IBM_4758_CCA_VERIFY,\n\t\t\t\t\t\tERR_R_MALLOC_FAILURE);\n\t\t\t\treturn 0;\n\t\t\t\t}\n\t\t\ti2d_X509_SIG(&sig, &ptr);\n\t\t\t}\n\t\t\tbreak;\n\t\tdefault:\n\t\t\treturn 0;\n\t\t}\n\tdigitalSignatureGenerate(&returnCode, &reasonCode, &exitDataLength,\n\t\texitData, &ruleArrayLength, ruleArray, &keyTokenLength,\n\t\tkeyToken, &length, hashBuffer, &outputLength, &outputBitLength,\n\t\tsigret);\n\tif (type == NID_sha1 || type == NID_md5)\n\t\t{\n\t\tOPENSSL_cleanse(hashBuffer, keyLength+1);\n\t\tOPENSSL_free(hashBuffer);\n\t\t}\n\t*siglen = outputLength;\n\treturn ((returnCode || reasonCode) ? 0 : 1);\n\t}', 'void *RSA_get_ex_data(const RSA *r, int idx)\n\t{\n\treturn(CRYPTO_get_ex_data(&r->ex_data,idx));\n\t}', 'void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)\n\t{\n\tif (ad->sk == NULL)\n\t\treturn(0);\n\telse if (idx >= sk_num(ad->sk))\n\t\treturn(0);\n\telse\n\t\treturn(sk_value(ad->sk,idx));\n\t}', 'int sk_num(const STACK *st)\n{\n\tif(st == NULL) return -1;\n\treturn st->num;\n}']
|
1,090 | 0 |
https://github.com/openssl/openssl/blob/3208ff58ca59d143b49dd2f1c05fbc33cf35e64f/apps/ca.c/#L3123
|
int unpack_revinfo(ASN1_TIME **prevtm, int *preason, ASN1_OBJECT **phold, ASN1_GENERALIZEDTIME **pinvtm, char *str)
{
char *tmp = NULL;
char *rtime_str, *reason_str = NULL, *arg_str = NULL, *p;
int reason_code = -1;
int i, ret = 0;
ASN1_OBJECT *hold = NULL;
ASN1_GENERALIZEDTIME *comp_time = NULL;
tmp = BUF_strdup(str);
p = strchr(tmp, ',');
rtime_str = tmp;
if (p)
{
*p = '\0';
p++;
reason_str = p;
p = strchr(p, ',');
if (p)
{
*p = '\0';
arg_str = p + 1;
}
}
if (prevtm)
{
*prevtm = ASN1_UTCTIME_new();
if (!ASN1_UTCTIME_set_string(*prevtm, rtime_str))
{
BIO_printf(bio_err, "invalid revocation date %s\n", rtime_str);
goto err;
}
}
if (reason_str)
{
for (i = 0; i < NUM_REASONS; i++)
{
if(!strcasecmp(reason_str, crl_reasons[i]))
{
reason_code = i;
break;
}
}
if (reason_code == OCSP_REVOKED_STATUS_NOSTATUS)
{
BIO_printf(bio_err, "invalid reason code %s\n", reason_str);
goto err;
}
if (reason_code == 7)
reason_code = OCSP_REVOKED_STATUS_REMOVEFROMCRL;
else if (reason_code == 8)
{
if (!arg_str)
{
BIO_printf(bio_err, "missing hold instruction\n");
goto err;
}
reason_code = OCSP_REVOKED_STATUS_CERTIFICATEHOLD;
hold = OBJ_txt2obj(arg_str, 0);
if (!hold)
{
BIO_printf(bio_err, "invalid object identifier %s\n", arg_str);
goto err;
}
if (phold) *phold = hold;
}
else if ((reason_code == 9) || (reason_code == 10))
{
if (!arg_str)
{
BIO_printf(bio_err, "missing compromised time\n");
goto err;
}
comp_time = ASN1_GENERALIZEDTIME_new();
if (!ASN1_GENERALIZEDTIME_set_string(comp_time, arg_str))
{
BIO_printf(bio_err, "invalid compromised time %s\n", arg_str);
goto err;
}
if (reason_code == 9)
reason_code = OCSP_REVOKED_STATUS_KEYCOMPROMISE;
else
reason_code = OCSP_REVOKED_STATUS_CACOMPROMISE;
}
}
if (preason) *preason = reason_code;
if (pinvtm) *pinvtm = comp_time;
else ASN1_GENERALIZEDTIME_free(comp_time);
ret = 1;
err:
if (tmp) OPENSSL_free(tmp);
if (!phold) ASN1_OBJECT_free(hold);
if (!pinvtm) ASN1_GENERALIZEDTIME_free(comp_time);
return ret;
}
|
['int unpack_revinfo(ASN1_TIME **prevtm, int *preason, ASN1_OBJECT **phold, ASN1_GENERALIZEDTIME **pinvtm, char *str)\n\t{\n\tchar *tmp = NULL;\n\tchar *rtime_str, *reason_str = NULL, *arg_str = NULL, *p;\n\tint reason_code = -1;\n\tint i, ret = 0;\n\tASN1_OBJECT *hold = NULL;\n\tASN1_GENERALIZEDTIME *comp_time = NULL;\n\ttmp = BUF_strdup(str);\n\tp = strchr(tmp, \',\');\n\trtime_str = tmp;\n\tif (p)\n\t\t{\n\t\t*p = \'\\0\';\n\t\tp++;\n\t\treason_str = p;\n\t\tp = strchr(p, \',\');\n\t\tif (p)\n\t\t\t{\n\t\t\t*p = \'\\0\';\n\t\t\targ_str = p + 1;\n\t\t\t}\n\t\t}\n\tif (prevtm)\n\t\t{\n\t\t*prevtm = ASN1_UTCTIME_new();\n\t\tif (!ASN1_UTCTIME_set_string(*prevtm, rtime_str))\n\t\t\t{\n\t\t\tBIO_printf(bio_err, "invalid revocation date %s\\n", rtime_str);\n\t\t\tgoto err;\n\t\t\t}\n\t\t}\n\tif (reason_str)\n\t\t{\n\t\tfor (i = 0; i < NUM_REASONS; i++)\n\t\t\t{\n\t\t\tif(!strcasecmp(reason_str, crl_reasons[i]))\n\t\t\t\t{\n\t\t\t\treason_code = i;\n\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\tif (reason_code == OCSP_REVOKED_STATUS_NOSTATUS)\n\t\t\t{\n\t\t\tBIO_printf(bio_err, "invalid reason code %s\\n", reason_str);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (reason_code == 7)\n\t\t\treason_code = OCSP_REVOKED_STATUS_REMOVEFROMCRL;\n\t\telse if (reason_code == 8)\n\t\t\t{\n\t\t\tif (!arg_str)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err, "missing hold instruction\\n");\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\treason_code = OCSP_REVOKED_STATUS_CERTIFICATEHOLD;\n\t\t\thold = OBJ_txt2obj(arg_str, 0);\n\t\t\tif (!hold)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err, "invalid object identifier %s\\n", arg_str);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif (phold) *phold = hold;\n\t\t\t}\n\t\telse if ((reason_code == 9) || (reason_code == 10))\n\t\t\t{\n\t\t\tif (!arg_str)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err, "missing compromised time\\n");\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tcomp_time = ASN1_GENERALIZEDTIME_new();\n\t\t\tif (!ASN1_GENERALIZEDTIME_set_string(comp_time, arg_str))\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err, "invalid compromised time %s\\n", arg_str);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif (reason_code == 9)\n\t\t\t\treason_code = OCSP_REVOKED_STATUS_KEYCOMPROMISE;\n\t\t\telse\n\t\t\t\treason_code = OCSP_REVOKED_STATUS_CACOMPROMISE;\n\t\t\t}\n\t\t}\n\tif (preason) *preason = reason_code;\n\tif (pinvtm) *pinvtm = comp_time;\n\telse ASN1_GENERALIZEDTIME_free(comp_time);\n\tret = 1;\n\terr:\n\tif (tmp) OPENSSL_free(tmp);\n\tif (!phold) ASN1_OBJECT_free(hold);\n\tif (!pinvtm) ASN1_GENERALIZEDTIME_free(comp_time);\n\treturn ret;\n\t}', 'char *BUF_strdup(const char *str)\n\t{\n\tchar *ret;\n\tint n;\n\tif (str == NULL) return(NULL);\n\tn=strlen(str);\n\tret=OPENSSL_malloc(n+1);\n\tif (ret == NULL)\n\t\t{\n\t\tBUFerr(BUF_F_BUF_STRDUP,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tmemcpy(ret,str,n+1);\n\treturn(ret);\n\t}', 'void *CRYPTO_malloc(int num, const char *file, int line)\n\t{\n\tvoid *ret = NULL;\n\tallow_customize = 0;\n\tif (malloc_debug_func != NULL)\n\t\t{\n\t\tallow_customize_debug = 0;\n\t\tmalloc_debug_func(NULL, num, file, line, 0);\n\t\t}\n\tret = malloc_ex_func(num,file,line);\n#ifdef LEVITTE_DEBUG_MEM\n\tfprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\\n", ret, num);\n#endif\n\tif (malloc_debug_func != NULL)\n\t\tmalloc_debug_func(ret, num, file, line, 1);\n\treturn ret;\n\t}', 'void ERR_put_error(int lib, int func, int reason, const char *file,\n\t int line)\n\t{\n\tERR_STATE *es;\n#ifdef _OSD_POSIX\n\tif (strncmp(file,"*POSIX(", sizeof("*POSIX(")-1) == 0) {\n\t\tchar *end;\n\t\tfile += sizeof("*POSIX(")-1;\n\t\tend = &file[strlen(file)-1];\n\t\tif (*end == \')\')\n\t\t\t*end = \'\\0\';\n\t\tif ((end = strrchr(file, \'/\')) != NULL)\n\t\t\tfile = &end[1];\n\t}\n#endif\n\tes=ERR_get_state();\n\tes->top=(es->top+1)%ERR_NUM_ERRORS;\n\tif (es->top == es->bottom)\n\t\tes->bottom=(es->bottom+1)%ERR_NUM_ERRORS;\n\tes->err_buffer[es->top]=ERR_PACK(lib,func,reason);\n\tes->err_file[es->top]=file;\n\tes->err_line[es->top]=line;\n\terr_clear_data(es,es->top);\n\t}', 'void CRYPTO_free(void *str)\n\t{\n\tif (free_debug_func != NULL)\n\t\tfree_debug_func(str, 0);\n#ifdef LEVITTE_DEBUG_MEM\n\tfprintf(stderr, "LEVITTE_DEBUG_MEM: < 0x%p\\n", str);\n#endif\n\tfree_func(str);\n\tif (free_debug_func != NULL)\n\t\tfree_debug_func(NULL, 1);\n\t}']
|
1,091 | 0 |
https://github.com/libav/libav/blob/1db9da523815beb8e9fdcbc63205b3473616c6f0/libavformat/mov.c/#L1134
|
static int mov_read_ctts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)
{
AVStream *st = c->fc->streams[c->fc->nb_streams-1];
MOVStreamContext *sc = st->priv_data;
unsigned int i, entries;
get_byte(pb);
get_be24(pb);
entries = get_be32(pb);
if(entries >= UINT_MAX / sizeof(MOV_stts_t))
return -1;
sc->ctts_count = entries;
sc->ctts_data = av_malloc(entries * sizeof(MOV_stts_t));
if (!sc->ctts_data)
return -1;
dprintf(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
for(i=0; i<entries; i++) {
int count =get_be32(pb);
int duration =get_be32(pb);
if (duration < 0) {
av_log(c->fc, AV_LOG_WARNING, "negative ctts, ignoring\n");
sc->ctts_count = 0;
url_fskip(pb, 8 * (entries - i - 1));
break;
}
sc->ctts_data[i].count = count;
sc->ctts_data[i].duration= duration;
sc->time_rate= ff_gcd(sc->time_rate, duration);
}
return 0;
}
|
['static int mov_read_ctts(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)\n{\n AVStream *st = c->fc->streams[c->fc->nb_streams-1];\n MOVStreamContext *sc = st->priv_data;\n unsigned int i, entries;\n get_byte(pb);\n get_be24(pb);\n entries = get_be32(pb);\n if(entries >= UINT_MAX / sizeof(MOV_stts_t))\n return -1;\n sc->ctts_count = entries;\n sc->ctts_data = av_malloc(entries * sizeof(MOV_stts_t));\n if (!sc->ctts_data)\n return -1;\n dprintf(c->fc, "track[%i].ctts.entries = %i\\n", c->fc->nb_streams-1, entries);\n for(i=0; i<entries; i++) {\n int count =get_be32(pb);\n int duration =get_be32(pb);\n if (duration < 0) {\n av_log(c->fc, AV_LOG_WARNING, "negative ctts, ignoring\\n");\n sc->ctts_count = 0;\n url_fskip(pb, 8 * (entries - i - 1));\n break;\n }\n sc->ctts_data[i].count = count;\n sc->ctts_data[i].duration= duration;\n sc->time_rate= ff_gcd(sc->time_rate, duration);\n }\n return 0;\n}']
|
1,092 | 0 |
https://github.com/libav/libav/blob/8a49d2bcbe7573bb4b765728b2578fac0d19763f/libavcodec/eamad.c/#L203
|
static void decode_mb(MadContext *s, AVFrame *frame, int inter)
{
int mv_map = 0;
int mv_x, mv_y;
int j;
if (inter) {
int v = decode210(&s->gb);
if (v < 2) {
mv_map = v ? get_bits(&s->gb, 6) : 63;
mv_x = decode_motion(&s->gb);
mv_y = decode_motion(&s->gb);
} else {
mv_map = 0;
}
}
for (j=0; j<6; j++) {
if (mv_map & (1<<j)) {
int add = 2*decode_motion(&s->gb);
comp_block(s, frame, s->mb_x, s->mb_y, j, mv_x, mv_y, add);
} else {
s->dsp.clear_block(s->block);
decode_block_intra(s, s->block);
idct_put(s, frame, s->block, s->mb_x, s->mb_y, j);
}
}
}
|
['static void decode_mb(MadContext *s, AVFrame *frame, int inter)\n{\n int mv_map = 0;\n int mv_x, mv_y;\n int j;\n if (inter) {\n int v = decode210(&s->gb);\n if (v < 2) {\n mv_map = v ? get_bits(&s->gb, 6) : 63;\n mv_x = decode_motion(&s->gb);\n mv_y = decode_motion(&s->gb);\n } else {\n mv_map = 0;\n }\n }\n for (j=0; j<6; j++) {\n if (mv_map & (1<<j)) {\n int add = 2*decode_motion(&s->gb);\n comp_block(s, frame, s->mb_x, s->mb_y, j, mv_x, mv_y, add);\n } else {\n s->dsp.clear_block(s->block);\n decode_block_intra(s, s->block);\n idct_put(s, frame, s->block, s->mb_x, s->mb_y, j);\n }\n }\n}']
|
1,093 | 0 |
https://github.com/libav/libav/blob/14fa75eab4e410858476cdcac8a1ec8b27f8a221/libavformat/rtsp.c/#L1699
|
static int sdp_read_header(AVFormatContext *s, AVFormatParameters *ap)
{
RTSPState *rt = s->priv_data;
RTSPStream *rtsp_st;
int size, i, err;
char *content;
char url[1024];
if (!ff_network_init())
return AVERROR(EIO);
content = av_malloc(SDP_MAX_SIZE);
size = get_buffer(s->pb, content, SDP_MAX_SIZE - 1);
if (size <= 0) {
av_free(content);
return AVERROR_INVALIDDATA;
}
content[size] ='\0';
ff_sdp_parse(s, content);
av_free(content);
for (i = 0; i < rt->nb_rtsp_streams; i++) {
char namebuf[50];
rtsp_st = rt->rtsp_streams[i];
getnameinfo((struct sockaddr*) &rtsp_st->sdp_ip, sizeof(rtsp_st->sdp_ip),
namebuf, sizeof(namebuf), NULL, 0, NI_NUMERICHOST);
ff_url_join(url, sizeof(url), "rtp", NULL,
namebuf, rtsp_st->sdp_port,
"?localport=%d&ttl=%d", rtsp_st->sdp_port,
rtsp_st->sdp_ttl);
if (url_open(&rtsp_st->rtp_handle, url, URL_RDWR) < 0) {
err = AVERROR_INVALIDDATA;
goto fail;
}
if ((err = rtsp_open_transport_ctx(s, rtsp_st)))
goto fail;
}
return 0;
fail:
ff_rtsp_close_streams(s);
ff_network_close();
return err;
}
|
['static int sdp_read_header(AVFormatContext *s, AVFormatParameters *ap)\n{\n RTSPState *rt = s->priv_data;\n RTSPStream *rtsp_st;\n int size, i, err;\n char *content;\n char url[1024];\n if (!ff_network_init())\n return AVERROR(EIO);\n content = av_malloc(SDP_MAX_SIZE);\n size = get_buffer(s->pb, content, SDP_MAX_SIZE - 1);\n if (size <= 0) {\n av_free(content);\n return AVERROR_INVALIDDATA;\n }\n content[size] =\'\\0\';\n ff_sdp_parse(s, content);\n av_free(content);\n for (i = 0; i < rt->nb_rtsp_streams; i++) {\n char namebuf[50];\n rtsp_st = rt->rtsp_streams[i];\n getnameinfo((struct sockaddr*) &rtsp_st->sdp_ip, sizeof(rtsp_st->sdp_ip),\n namebuf, sizeof(namebuf), NULL, 0, NI_NUMERICHOST);\n ff_url_join(url, sizeof(url), "rtp", NULL,\n namebuf, rtsp_st->sdp_port,\n "?localport=%d&ttl=%d", rtsp_st->sdp_port,\n rtsp_st->sdp_ttl);\n if (url_open(&rtsp_st->rtp_handle, url, URL_RDWR) < 0) {\n err = AVERROR_INVALIDDATA;\n goto fail;\n }\n if ((err = rtsp_open_transport_ctx(s, rtsp_st)))\n goto fail;\n }\n return 0;\nfail:\n ff_rtsp_close_streams(s);\n ff_network_close();\n return err;\n}', 'static inline int ff_network_init(void)\n{\n#if HAVE_WINSOCK2_H\n WSADATA wsaData;\n if (WSAStartup(MAKEWORD(1,1), &wsaData))\n return 0;\n#endif\n return 1;\n}', 'void *av_malloc(FF_INTERNAL_MEM_TYPE size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-16) )\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size+16);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&15) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr,16,size))\n ptr = NULL;\n#elif HAVE_MEMALIGN\n ptr = memalign(16,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}']
|
1,094 | 0 |
https://github.com/libav/libav/blob/dd2d3b766b20196d0b65a82e3d897ccecbf7adb8/libavcodec/utils.c/#L864
|
static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
{
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
}
|
['static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)\n{\n const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);\n return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;\n}', 'const AVPixFmtDescriptor *av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)\n{\n if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB)\n return NULL;\n return &av_pix_fmt_descriptors[pix_fmt];\n}']
|
1,095 | 0 |
https://github.com/libav/libav/blob/92c5052db971a2a3dfb7c3fc4317b55f401d7811/ffmpeg.c/#L3727
|
static void opt_output_file(const char *filename)
{
AVFormatContext *oc;
int err, use_video, use_audio, use_subtitle;
int input_has_video, input_has_audio, input_has_subtitle;
AVFormatParameters params, *ap = ¶ms;
AVOutputFormat *file_oformat;
if (!strcmp(filename, "-"))
filename = "pipe:";
oc = avformat_alloc_context();
if (!oc) {
print_error(filename, AVERROR(ENOMEM));
ffmpeg_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);
ffmpeg_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);
ffmpeg_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);
ffmpeg_exit(1);
}
} else {
use_video = file_oformat->video_codec != CODEC_ID_NONE || video_stream_copy || video_codec_name;
use_audio = file_oformat->audio_codec != CODEC_ID_NONE || audio_stream_copy || audio_codec_name;
use_subtitle = file_oformat->subtitle_codec != CODEC_ID_NONE || subtitle_stream_copy || subtitle_codec_name;
if (nb_input_files > 0) {
check_audio_video_sub_inputs(&input_has_video, &input_has_audio,
&input_has_subtitle);
if (!input_has_video)
use_video = 0;
if (!input_has_audio)
use_audio = 0;
if (!input_has_subtitle)
use_subtitle = 0;
}
if (audio_disable) {
use_audio = 0;
}
if (video_disable) {
use_video = 0;
}
if (subtitle_disable) {
use_subtitle = 0;
}
if (use_video) {
new_video_stream(oc);
}
if (use_audio) {
new_audio_stream(oc);
}
if (use_subtitle) {
new_subtitle_stream(oc);
}
oc->timestamp = recording_timestamp;
for(; metadata_count>0; metadata_count--){
av_metadata_set2(&oc->metadata, metadata[metadata_count-1].key,
metadata[metadata_count-1].value, 0);
}
av_metadata_conv(oc, oc->oformat->metadata_conv, NULL);
}
output_files[nb_output_files++] = oc;
if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
if (!av_filename_number_test(oc->filename)) {
print_error(oc->filename, AVERROR_NUMEXPECTED);
ffmpeg_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");
ffmpeg_exit(1);
}
}
else {
fprintf(stderr,"File '%s' already exists. Exiting.\n", filename);
ffmpeg_exit(1);
}
}
}
if ((err = url_fopen(&oc->pb, filename, URL_WRONLY)) < 0) {
print_error(filename, err);
ffmpeg_exit(1);
}
}
memset(ap, 0, sizeof(*ap));
if (av_set_parameters(oc, ap) < 0) {
fprintf(stderr, "%s: Invalid encoding parameters\n",
oc->filename);
ffmpeg_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, NULL);
nb_streamid_map = 0;
}
|
['static void opt_output_file(const char *filename)\n{\n AVFormatContext *oc;\n int err, use_video, use_audio, use_subtitle;\n int input_has_video, input_has_audio, input_has_subtitle;\n AVFormatParameters params, *ap = ¶ms;\n AVOutputFormat *file_oformat;\n if (!strcmp(filename, "-"))\n filename = "pipe:";\n oc = avformat_alloc_context();\n if (!oc) {\n print_error(filename, AVERROR(ENOMEM));\n ffmpeg_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 ffmpeg_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 ffmpeg_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 ffmpeg_exit(1);\n }\n } else {\n use_video = file_oformat->video_codec != CODEC_ID_NONE || video_stream_copy || video_codec_name;\n use_audio = file_oformat->audio_codec != CODEC_ID_NONE || audio_stream_copy || audio_codec_name;\n use_subtitle = file_oformat->subtitle_codec != CODEC_ID_NONE || subtitle_stream_copy || subtitle_codec_name;\n if (nb_input_files > 0) {\n check_audio_video_sub_inputs(&input_has_video, &input_has_audio,\n &input_has_subtitle);\n if (!input_has_video)\n use_video = 0;\n if (!input_has_audio)\n use_audio = 0;\n if (!input_has_subtitle)\n use_subtitle = 0;\n }\n if (audio_disable) {\n use_audio = 0;\n }\n if (video_disable) {\n use_video = 0;\n }\n if (subtitle_disable) {\n use_subtitle = 0;\n }\n if (use_video) {\n new_video_stream(oc);\n }\n if (use_audio) {\n new_audio_stream(oc);\n }\n if (use_subtitle) {\n new_subtitle_stream(oc);\n }\n oc->timestamp = recording_timestamp;\n for(; metadata_count>0; metadata_count--){\n av_metadata_set2(&oc->metadata, metadata[metadata_count-1].key,\n metadata[metadata_count-1].value, 0);\n }\n av_metadata_conv(oc, oc->oformat->metadata_conv, NULL);\n }\n output_files[nb_output_files++] = oc;\n if (oc->oformat->flags & AVFMT_NEEDNUMBER) {\n if (!av_filename_number_test(oc->filename)) {\n print_error(oc->filename, AVERROR_NUMEXPECTED);\n ffmpeg_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 ffmpeg_exit(1);\n }\n }\n else {\n fprintf(stderr,"File \'%s\' already exists. Exiting.\\n", filename);\n ffmpeg_exit(1);\n }\n }\n }\n if ((err = url_fopen(&oc->pb, filename, URL_WRONLY)) < 0) {\n print_error(filename, err);\n ffmpeg_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 ffmpeg_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, NULL);\n nb_streamid_map = 0;\n}', 'AVFormatContext *avformat_alloc_context(void)\n{\n AVFormatContext *ic;\n ic = av_malloc(sizeof(AVFormatContext));\n if (!ic) return ic;\n avformat_get_context_defaults(ic);\n ic->av_class = &av_format_context_class;\n return ic;\n}', 'void *av_malloc(unsigned int size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-16) )\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size+16);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&15) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr,16,size))\n ptr = NULL;\n#elif HAVE_MEMALIGN\n ptr = memalign(16,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}', 'void print_error(const char *filename, int err)\n{\n char errbuf[128];\n const char *errbuf_ptr = errbuf;\n if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)\n errbuf_ptr = strerror(AVUNERROR(err));\n fprintf(stderr, "%s: %s\\n", filename, errbuf_ptr);\n}', 'int av_strerror(int errnum, char *errbuf, size_t errbuf_size)\n{\n int ret = 0;\n const char *errstr = NULL;\n switch (errnum) {\n case AVERROR_EOF: errstr = "End of file"; break;\n case AVERROR_INVALIDDATA: errstr = "Invalid data found when processing input"; break;\n case AVERROR_NUMEXPECTED: errstr = "Number syntax expected in filename"; break;\n case AVERROR_PATCHWELCOME: errstr = "Not yet implemented in FFmpeg, patches welcome"; break;\n }\n if (errstr) {\n av_strlcpy(errbuf, errstr, errbuf_size);\n } else {\n#if HAVE_STRERROR_R\n ret = strerror_r(AVUNERROR(errnum), errbuf, errbuf_size);\n#else\n ret = -1;\n#endif\n if (ret < 0)\n snprintf(errbuf, errbuf_size, "Error number %d occurred", errnum);\n }\n return ret;\n}']
|
1,096 | 0 |
https://github.com/libav/libav/blob/cc20fbcd39c7b60602edae4f7deb092ecfd3c975/libavcodec/vp9dsp.c/#L1615
|
static av_always_inline void loop_filter(uint8_t *dst, ptrdiff_t stride,
int E, int I, int H,
ptrdiff_t stridea, ptrdiff_t strideb,
int wd)
{
int i;
for (i = 0; i < 8; i++, dst += stridea) {
int p7, p6, p5, p4;
int p3 = dst[strideb * -4], p2 = dst[strideb * -3];
int p1 = dst[strideb * -2], p0 = dst[strideb * -1];
int q0 = dst[strideb * +0], q1 = dst[strideb * +1];
int q2 = dst[strideb * +2], q3 = dst[strideb * +3];
int q4, q5, q6, q7;
int fm = FFABS(p3 - p2) <= I && FFABS(p2 - p1) <= I &&
FFABS(p1 - p0) <= I && FFABS(q1 - q0) <= I &&
FFABS(q2 - q1) <= I && FFABS(q3 - q2) <= I &&
FFABS(p0 - q0) * 2 + (FFABS(p1 - q1) >> 1) <= E;
int flat8out, flat8in;
if (!fm)
continue;
if (wd >= 16) {
p7 = dst[strideb * -8];
p6 = dst[strideb * -7];
p5 = dst[strideb * -6];
p4 = dst[strideb * -5];
q4 = dst[strideb * +4];
q5 = dst[strideb * +5];
q6 = dst[strideb * +6];
q7 = dst[strideb * +7];
flat8out = FFABS(p7 - p0) <= 1 && FFABS(p6 - p0) <= 1 &&
FFABS(p5 - p0) <= 1 && FFABS(p4 - p0) <= 1 &&
FFABS(q4 - q0) <= 1 && FFABS(q5 - q0) <= 1 &&
FFABS(q6 - q0) <= 1 && FFABS(q7 - q0) <= 1;
}
if (wd >= 8)
flat8in = FFABS(p3 - p0) <= 1 && FFABS(p2 - p0) <= 1 &&
FFABS(p1 - p0) <= 1 && FFABS(q1 - q0) <= 1 &&
FFABS(q2 - q0) <= 1 && FFABS(q3 - q0) <= 1;
if (wd >= 16 && flat8out && flat8in) {
dst[strideb * -7] = (p7 + p7 + p7 + p7 + p7 + p7 + p7 + p6 * 2 +
p5 + p4 + p3 + p2 + p1 + p0 + q0 + 8) >> 4;
dst[strideb * -6] = (p7 + p7 + p7 + p7 + p7 + p7 + p6 + p5 * 2 +
p4 + p3 + p2 + p1 + p0 + q0 + q1 + 8) >> 4;
dst[strideb * -5] = (p7 + p7 + p7 + p7 + p7 + p6 + p5 + p4 * 2 +
p3 + p2 + p1 + p0 + q0 + q1 + q2 + 8) >> 4;
dst[strideb * -4] = (p7 + p7 + p7 + p7 + p6 + p5 + p4 + p3 * 2 +
p2 + p1 + p0 + q0 + q1 + q2 + q3 + 8) >> 4;
dst[strideb * -3] = (p7 + p7 + p7 + p6 + p5 + p4 + p3 + p2 * 2 +
p1 + p0 + q0 + q1 + q2 + q3 + q4 + 8) >> 4;
dst[strideb * -2] = (p7 + p7 + p6 + p5 + p4 + p3 + p2 + p1 * 2 +
p0 + q0 + q1 + q2 + q3 + q4 + q5 + 8) >> 4;
dst[strideb * -1] = (p7 + p6 + p5 + p4 + p3 + p2 + p1 + p0 * 2 +
q0 + q1 + q2 + q3 + q4 + q5 + q6 + 8) >> 4;
dst[strideb * +0] = (p6 + p5 + p4 + p3 + p2 + p1 + p0 + q0 * 2 +
q1 + q2 + q3 + q4 + q5 + q6 + q7 + 8) >> 4;
dst[strideb * +1] = (p5 + p4 + p3 + p2 + p1 + p0 + q0 + q1 * 2 +
q2 + q3 + q4 + q5 + q6 + q7 + q7 + 8) >> 4;
dst[strideb * +2] = (p4 + p3 + p2 + p1 + p0 + q0 + q1 + q2 * 2 +
q3 + q4 + q5 + q6 + q7 + q7 + q7 + 8) >> 4;
dst[strideb * +3] = (p3 + p2 + p1 + p0 + q0 + q1 + q2 + q3 * 2 +
q4 + q5 + q6 + q7 + q7 + q7 + q7 + 8) >> 4;
dst[strideb * +4] = (p2 + p1 + p0 + q0 + q1 + q2 + q3 + q4 * 2 +
q5 + q6 + q7 + q7 + q7 + q7 + q7 + 8) >> 4;
dst[strideb * +5] = (p1 + p0 + q0 + q1 + q2 + q3 + q4 + q5 * 2 +
q6 + q7 + q7 + q7 + q7 + q7 + q7 + 8) >> 4;
dst[strideb * +6] = (p0 + q0 + q1 + q2 + q3 + q4 + q5 + q6 * 2 +
q7 + q7 + q7 + q7 + q7 + q7 + q7 + 8) >> 4;
} else if (wd >= 8 && flat8in) {
dst[strideb * -3] = (p3 + p3 + p3 + 2 * p2 + p1 + p0 + q0 + 4) >> 3;
dst[strideb * -2] = (p3 + p3 + p2 + 2 * p1 + p0 + q0 + q1 + 4) >> 3;
dst[strideb * -1] = (p3 + p2 + p1 + 2 * p0 + q0 + q1 + q2 + 4) >> 3;
dst[strideb * +0] = (p2 + p1 + p0 + 2 * q0 + q1 + q2 + q3 + 4) >> 3;
dst[strideb * +1] = (p1 + p0 + q0 + 2 * q1 + q2 + q3 + q3 + 4) >> 3;
dst[strideb * +2] = (p0 + q0 + q1 + 2 * q2 + q3 + q3 + q3 + 4) >> 3;
} else {
int hev = FFABS(p1 - p0) > H || FFABS(q1 - q0) > H;
if (hev) {
int f = av_clip_int8(3 * (q0 - p0) + av_clip_int8(p1 - q1));
int f1 = FFMIN(f + 4, 127) >> 3;
int f2 = FFMIN(f + 3, 127) >> 3;
dst[strideb * -1] = av_clip_uint8(p0 + f2);
dst[strideb * +0] = av_clip_uint8(q0 - f1);
} else {
int f = av_clip_int8(3 * (q0 - p0));
int f1 = FFMIN(f + 4, 127) >> 3;
int f2 = FFMIN(f + 3, 127) >> 3;
dst[strideb * -1] = av_clip_uint8(p0 + f2);
dst[strideb * +0] = av_clip_uint8(q0 - f1);
f = (f1 + 1) >> 1;
dst[strideb * -2] = av_clip_uint8(p1 + f);
dst[strideb * +1] = av_clip_uint8(q1 - f);
}
}
}
}
|
['static av_always_inline void loop_filter(uint8_t *dst, ptrdiff_t stride,\n int E, int I, int H,\n ptrdiff_t stridea, ptrdiff_t strideb,\n int wd)\n{\n int i;\n for (i = 0; i < 8; i++, dst += stridea) {\n int p7, p6, p5, p4;\n int p3 = dst[strideb * -4], p2 = dst[strideb * -3];\n int p1 = dst[strideb * -2], p0 = dst[strideb * -1];\n int q0 = dst[strideb * +0], q1 = dst[strideb * +1];\n int q2 = dst[strideb * +2], q3 = dst[strideb * +3];\n int q4, q5, q6, q7;\n int fm = FFABS(p3 - p2) <= I && FFABS(p2 - p1) <= I &&\n FFABS(p1 - p0) <= I && FFABS(q1 - q0) <= I &&\n FFABS(q2 - q1) <= I && FFABS(q3 - q2) <= I &&\n FFABS(p0 - q0) * 2 + (FFABS(p1 - q1) >> 1) <= E;\n int flat8out, flat8in;\n if (!fm)\n continue;\n if (wd >= 16) {\n p7 = dst[strideb * -8];\n p6 = dst[strideb * -7];\n p5 = dst[strideb * -6];\n p4 = dst[strideb * -5];\n q4 = dst[strideb * +4];\n q5 = dst[strideb * +5];\n q6 = dst[strideb * +6];\n q7 = dst[strideb * +7];\n flat8out = FFABS(p7 - p0) <= 1 && FFABS(p6 - p0) <= 1 &&\n FFABS(p5 - p0) <= 1 && FFABS(p4 - p0) <= 1 &&\n FFABS(q4 - q0) <= 1 && FFABS(q5 - q0) <= 1 &&\n FFABS(q6 - q0) <= 1 && FFABS(q7 - q0) <= 1;\n }\n if (wd >= 8)\n flat8in = FFABS(p3 - p0) <= 1 && FFABS(p2 - p0) <= 1 &&\n FFABS(p1 - p0) <= 1 && FFABS(q1 - q0) <= 1 &&\n FFABS(q2 - q0) <= 1 && FFABS(q3 - q0) <= 1;\n if (wd >= 16 && flat8out && flat8in) {\n dst[strideb * -7] = (p7 + p7 + p7 + p7 + p7 + p7 + p7 + p6 * 2 +\n p5 + p4 + p3 + p2 + p1 + p0 + q0 + 8) >> 4;\n dst[strideb * -6] = (p7 + p7 + p7 + p7 + p7 + p7 + p6 + p5 * 2 +\n p4 + p3 + p2 + p1 + p0 + q0 + q1 + 8) >> 4;\n dst[strideb * -5] = (p7 + p7 + p7 + p7 + p7 + p6 + p5 + p4 * 2 +\n p3 + p2 + p1 + p0 + q0 + q1 + q2 + 8) >> 4;\n dst[strideb * -4] = (p7 + p7 + p7 + p7 + p6 + p5 + p4 + p3 * 2 +\n p2 + p1 + p0 + q0 + q1 + q2 + q3 + 8) >> 4;\n dst[strideb * -3] = (p7 + p7 + p7 + p6 + p5 + p4 + p3 + p2 * 2 +\n p1 + p0 + q0 + q1 + q2 + q3 + q4 + 8) >> 4;\n dst[strideb * -2] = (p7 + p7 + p6 + p5 + p4 + p3 + p2 + p1 * 2 +\n p0 + q0 + q1 + q2 + q3 + q4 + q5 + 8) >> 4;\n dst[strideb * -1] = (p7 + p6 + p5 + p4 + p3 + p2 + p1 + p0 * 2 +\n q0 + q1 + q2 + q3 + q4 + q5 + q6 + 8) >> 4;\n dst[strideb * +0] = (p6 + p5 + p4 + p3 + p2 + p1 + p0 + q0 * 2 +\n q1 + q2 + q3 + q4 + q5 + q6 + q7 + 8) >> 4;\n dst[strideb * +1] = (p5 + p4 + p3 + p2 + p1 + p0 + q0 + q1 * 2 +\n q2 + q3 + q4 + q5 + q6 + q7 + q7 + 8) >> 4;\n dst[strideb * +2] = (p4 + p3 + p2 + p1 + p0 + q0 + q1 + q2 * 2 +\n q3 + q4 + q5 + q6 + q7 + q7 + q7 + 8) >> 4;\n dst[strideb * +3] = (p3 + p2 + p1 + p0 + q0 + q1 + q2 + q3 * 2 +\n q4 + q5 + q6 + q7 + q7 + q7 + q7 + 8) >> 4;\n dst[strideb * +4] = (p2 + p1 + p0 + q0 + q1 + q2 + q3 + q4 * 2 +\n q5 + q6 + q7 + q7 + q7 + q7 + q7 + 8) >> 4;\n dst[strideb * +5] = (p1 + p0 + q0 + q1 + q2 + q3 + q4 + q5 * 2 +\n q6 + q7 + q7 + q7 + q7 + q7 + q7 + 8) >> 4;\n dst[strideb * +6] = (p0 + q0 + q1 + q2 + q3 + q4 + q5 + q6 * 2 +\n q7 + q7 + q7 + q7 + q7 + q7 + q7 + 8) >> 4;\n } else if (wd >= 8 && flat8in) {\n dst[strideb * -3] = (p3 + p3 + p3 + 2 * p2 + p1 + p0 + q0 + 4) >> 3;\n dst[strideb * -2] = (p3 + p3 + p2 + 2 * p1 + p0 + q0 + q1 + 4) >> 3;\n dst[strideb * -1] = (p3 + p2 + p1 + 2 * p0 + q0 + q1 + q2 + 4) >> 3;\n dst[strideb * +0] = (p2 + p1 + p0 + 2 * q0 + q1 + q2 + q3 + 4) >> 3;\n dst[strideb * +1] = (p1 + p0 + q0 + 2 * q1 + q2 + q3 + q3 + 4) >> 3;\n dst[strideb * +2] = (p0 + q0 + q1 + 2 * q2 + q3 + q3 + q3 + 4) >> 3;\n } else {\n int hev = FFABS(p1 - p0) > H || FFABS(q1 - q0) > H;\n if (hev) {\n int f = av_clip_int8(3 * (q0 - p0) + av_clip_int8(p1 - q1));\n int f1 = FFMIN(f + 4, 127) >> 3;\n int f2 = FFMIN(f + 3, 127) >> 3;\n dst[strideb * -1] = av_clip_uint8(p0 + f2);\n dst[strideb * +0] = av_clip_uint8(q0 - f1);\n } else {\n int f = av_clip_int8(3 * (q0 - p0));\n int f1 = FFMIN(f + 4, 127) >> 3;\n int f2 = FFMIN(f + 3, 127) >> 3;\n dst[strideb * -1] = av_clip_uint8(p0 + f2);\n dst[strideb * +0] = av_clip_uint8(q0 - f1);\n f = (f1 + 1) >> 1;\n dst[strideb * -2] = av_clip_uint8(p1 + f);\n dst[strideb * +1] = av_clip_uint8(q1 - f);\n }\n }\n }\n}']
|
1,097 | 0 |
https://github.com/libav/libav/blob/2af3dc8698707f800f83f5fc890571a6a119866e/libavcodec/h264.c/#L351
|
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.f.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->f.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);
}
}
}
}
}
|
['int ff_rv34_decode_frame(AVCodecContext *avctx,\n void *data, int *data_size,\n AVPacket *avpkt)\n{\n const uint8_t *buf = avpkt->data;\n int buf_size = avpkt->size;\n RV34DecContext *r = avctx->priv_data;\n MpegEncContext *s = &r->s;\n AVFrame *pict = data;\n SliceInfo si;\n int i;\n int slice_count;\n const uint8_t *slices_hdr = NULL;\n int last = 0;\n if (buf_size == 0) {\n if (s->low_delay==0 && s->next_picture_ptr) {\n *pict = *(AVFrame*)s->next_picture_ptr;\n s->next_picture_ptr = NULL;\n *data_size = sizeof(AVFrame);\n }\n return 0;\n }\n if(!avctx->slice_count){\n slice_count = (*buf++) + 1;\n slices_hdr = buf + 4;\n buf += 8 * slice_count;\n buf_size -= 1 + 8 * slice_count;\n }else\n slice_count = avctx->slice_count;\n if(get_slice_offset(avctx, slices_hdr, 0) < 0 ||\n get_slice_offset(avctx, slices_hdr, 0) > buf_size){\n av_log(avctx, AV_LOG_ERROR, "Slice offset is invalid\\n");\n return AVERROR_INVALIDDATA;\n }\n init_get_bits(&s->gb, buf+get_slice_offset(avctx, slices_hdr, 0), (buf_size-get_slice_offset(avctx, slices_hdr, 0))*8);\n if(r->parse_slice_header(r, &r->s.gb, &si) < 0 || si.start){\n av_log(avctx, AV_LOG_ERROR, "First slice header is incorrect\\n");\n return AVERROR_INVALIDDATA;\n }\n if ((!s->last_picture_ptr || !s->last_picture_ptr->f.data[0]) &&\n si.type == AV_PICTURE_TYPE_B) {\n av_log(avctx, AV_LOG_ERROR, "Invalid decoder state: B-frame without "\n "reference data.\\n");\n return AVERROR_INVALIDDATA;\n }\n if( (avctx->skip_frame >= AVDISCARD_NONREF && si.type==AV_PICTURE_TYPE_B)\n || (avctx->skip_frame >= AVDISCARD_NONKEY && si.type!=AV_PICTURE_TYPE_I)\n || avctx->skip_frame >= AVDISCARD_ALL)\n return avpkt->size;\n for(i = 0; i < slice_count; i++){\n int offset = get_slice_offset(avctx, slices_hdr, i);\n int size;\n if(i+1 == slice_count)\n size = buf_size - offset;\n else\n size = get_slice_offset(avctx, slices_hdr, i+1) - offset;\n if(offset < 0 || offset > buf_size){\n av_log(avctx, AV_LOG_ERROR, "Slice offset is invalid\\n");\n break;\n }\n r->si.end = s->mb_width * s->mb_height;\n if(i+1 < slice_count){\n if (get_slice_offset(avctx, slices_hdr, i+1) < 0 ||\n get_slice_offset(avctx, slices_hdr, i+1) > buf_size) {\n av_log(avctx, AV_LOG_ERROR, "Slice offset is invalid\\n");\n break;\n }\n init_get_bits(&s->gb, buf+get_slice_offset(avctx, slices_hdr, i+1), (buf_size-get_slice_offset(avctx, slices_hdr, i+1))*8);\n if(r->parse_slice_header(r, &r->s.gb, &si) < 0){\n if(i+2 < slice_count)\n size = get_slice_offset(avctx, slices_hdr, i+2) - offset;\n else\n size = buf_size - offset;\n }else\n r->si.end = si.start;\n }\n if (size < 0 || size > buf_size - offset) {\n av_log(avctx, AV_LOG_ERROR, "Slice size is invalid\\n");\n break;\n }\n last = rv34_decode_slice(r, r->si.end, buf + offset, size);\n s->mb_num_left = r->s.mb_x + r->s.mb_y*r->s.mb_width - r->si.start;\n if(last)\n break;\n }\n if(last && s->current_picture_ptr){\n if(r->loop_filter)\n r->loop_filter(r, s->mb_height - 1);\n ff_er_frame_end(s);\n MPV_frame_end(s);\n if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))\n ff_thread_report_progress(&s->current_picture_ptr->f, INT_MAX, 0);\n if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {\n *pict = *(AVFrame*)s->current_picture_ptr;\n } else if (s->last_picture_ptr != NULL) {\n *pict = *(AVFrame*)s->last_picture_ptr;\n }\n if(s->last_picture_ptr || s->low_delay){\n *data_size = sizeof(AVFrame);\n ff_print_debug_info(s, pict);\n }\n s->current_picture_ptr = NULL;\n }\n return avpkt->size;\n}', 'void ff_er_frame_end(MpegEncContext *s)\n{\n int i, mb_x, mb_y, error, error_type, dc_error, mv_error, ac_error;\n int distance;\n int threshold_part[4] = { 100, 100, 100 };\n int threshold = 50;\n int is_intra_likely;\n int size = s->b8_stride * 2 * s->mb_height;\n Picture *pic = s->current_picture_ptr;\n if (!s->err_recognition || s->error_count == 0 || s->avctx->lowres ||\n s->avctx->hwaccel ||\n s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU ||\n s->picture_structure != PICT_FRAME ||\n s->error_count == 3 * s->mb_width *\n (s->avctx->skip_top + s->avctx->skip_bottom)) {\n return;\n };\n if (s->current_picture.f.motion_val[0] == NULL) {\n av_log(s->avctx, AV_LOG_ERROR, "Warning MVs not available\\n");\n for (i = 0; i < 2; i++) {\n pic->f.ref_index[i] = av_mallocz(s->mb_stride * s->mb_height * 4 * sizeof(uint8_t));\n pic->motion_val_base[i] = av_mallocz((size + 4) * 2 * sizeof(uint16_t));\n pic->f.motion_val[i] = pic->motion_val_base[i] + 4;\n }\n pic->f.motion_subsample_log2 = 3;\n s->current_picture = *s->current_picture_ptr;\n }\n if (s->avctx->debug & FF_DEBUG_ER) {\n for (mb_y = 0; mb_y < s->mb_height; mb_y++) {\n for (mb_x = 0; mb_x < s->mb_width; mb_x++) {\n int status = s->error_status_table[mb_x + mb_y * s->mb_stride];\n av_log(s->avctx, AV_LOG_DEBUG, "%2X ", status);\n }\n av_log(s->avctx, AV_LOG_DEBUG, "\\n");\n }\n }\n for (error_type = 1; error_type <= 3; error_type++) {\n int end_ok = 0;\n for (i = s->mb_num - 1; i >= 0; i--) {\n const int mb_xy = s->mb_index2xy[i];\n int error = s->error_status_table[mb_xy];\n if (error & (1 << error_type))\n end_ok = 1;\n if (error & (8 << error_type))\n end_ok = 1;\n if (!end_ok)\n s->error_status_table[mb_xy] |= 1 << error_type;\n if (error & VP_START)\n end_ok = 0;\n }\n }\n if (s->partitioned_frame) {\n int end_ok = 0;\n for (i = s->mb_num - 1; i >= 0; i--) {\n const int mb_xy = s->mb_index2xy[i];\n int error = s->error_status_table[mb_xy];\n if (error & ER_AC_END)\n end_ok = 0;\n if ((error & ER_MV_END) ||\n (error & ER_DC_END) ||\n (error & ER_AC_ERROR))\n end_ok = 1;\n if (!end_ok)\n s->error_status_table[mb_xy]|= ER_AC_ERROR;\n if (error & VP_START)\n end_ok = 0;\n }\n }\n if (s->err_recognition & AV_EF_EXPLODE) {\n int end_ok = 1;\n for (i = s->mb_num - 2; i >= s->mb_width + 100; i--) {\n const int mb_xy = s->mb_index2xy[i];\n int error1 = s->error_status_table[mb_xy];\n int error2 = s->error_status_table[s->mb_index2xy[i + 1]];\n if (error1 & VP_START)\n end_ok = 1;\n if (error2 == (VP_START | ER_MB_ERROR | ER_MB_END) &&\n error1 != (VP_START | ER_MB_ERROR | ER_MB_END) &&\n ((error1 & ER_AC_END) || (error1 & ER_DC_END) ||\n (error1 & ER_MV_END))) {\n end_ok = 0;\n }\n if (!end_ok)\n s->error_status_table[mb_xy] |= ER_MB_ERROR;\n }\n }\n distance = 9999999;\n for (error_type = 1; error_type <= 3; error_type++) {\n for (i = s->mb_num - 1; i >= 0; i--) {\n const int mb_xy = s->mb_index2xy[i];\n int error = s->error_status_table[mb_xy];\n if (!s->mbskip_table[mb_xy])\n distance++;\n if (error & (1 << error_type))\n distance = 0;\n if (s->partitioned_frame) {\n if (distance < threshold_part[error_type - 1])\n s->error_status_table[mb_xy] |= 1 << error_type;\n } else {\n if (distance < threshold)\n s->error_status_table[mb_xy] |= 1 << error_type;\n }\n if (error & VP_START)\n distance = 9999999;\n }\n }\n error = 0;\n for (i = 0; i < s->mb_num; i++) {\n const int mb_xy = s->mb_index2xy[i];\n int old_error = s->error_status_table[mb_xy];\n if (old_error & VP_START) {\n error = old_error & ER_MB_ERROR;\n } else {\n error |= old_error & ER_MB_ERROR;\n s->error_status_table[mb_xy] |= error;\n }\n }\n if (!s->partitioned_frame) {\n for (i = 0; i < s->mb_num; i++) {\n const int mb_xy = s->mb_index2xy[i];\n error = s->error_status_table[mb_xy];\n if (error & ER_MB_ERROR)\n error |= ER_MB_ERROR;\n s->error_status_table[mb_xy] = error;\n }\n }\n dc_error = ac_error = mv_error = 0;\n for (i = 0; i < s->mb_num; i++) {\n const int mb_xy = s->mb_index2xy[i];\n error = s->error_status_table[mb_xy];\n if (error & ER_DC_ERROR)\n dc_error++;\n if (error & ER_AC_ERROR)\n ac_error++;\n if (error & ER_MV_ERROR)\n mv_error++;\n }\n av_log(s->avctx, AV_LOG_INFO, "concealing %d DC, %d AC, %d MV errors\\n",\n dc_error, ac_error, mv_error);\n is_intra_likely = is_intra_more_likely(s);\n for (i = 0; i < s->mb_num; i++) {\n const int mb_xy = s->mb_index2xy[i];\n error = s->error_status_table[mb_xy];\n if (!((error & ER_DC_ERROR) && (error & ER_MV_ERROR)))\n continue;\n if (is_intra_likely)\n s->current_picture.f.mb_type[mb_xy] = MB_TYPE_INTRA4x4;\n else\n s->current_picture.f.mb_type[mb_xy] = MB_TYPE_16x16 | MB_TYPE_L0;\n }\n if (!s->last_picture.f.data[0] && !s->next_picture.f.data[0])\n for (i = 0; i < s->mb_num; i++) {\n const int mb_xy = s->mb_index2xy[i];\n if (!IS_INTRA(s->current_picture.f.mb_type[mb_xy]))\n s->current_picture.f.mb_type[mb_xy] = MB_TYPE_INTRA4x4;\n }\n for (mb_y = 0; mb_y < s->mb_height; mb_y++) {\n for (mb_x = 0; mb_x < s->mb_width; mb_x++) {\n const int mb_xy = mb_x + mb_y * s->mb_stride;\n const int mb_type = s->current_picture.f.mb_type[mb_xy];\n int dir = !s->last_picture.f.data[0];\n error = s->error_status_table[mb_xy];\n if (IS_INTRA(mb_type))\n continue;\n if (error & ER_MV_ERROR)\n continue;\n if (!(error & ER_AC_ERROR))\n continue;\n s->mv_dir = dir ? MV_DIR_BACKWARD : MV_DIR_FORWARD;\n s->mb_intra = 0;\n s->mb_skipped = 0;\n if (IS_8X8(mb_type)) {\n int mb_index = mb_x * 2 + mb_y * 2 * s->b8_stride;\n int j;\n s->mv_type = MV_TYPE_8X8;\n for (j = 0; j < 4; j++) {\n s->mv[0][j][0] = s->current_picture.f.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][0];\n s->mv[0][j][1] = s->current_picture.f.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][1];\n }\n } else {\n s->mv_type = MV_TYPE_16X16;\n s->mv[0][0][0] = s->current_picture.f.motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][0];\n s->mv[0][0][1] = s->current_picture.f.motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][1];\n }\n s->dsp.clear_blocks(s->block[0]);\n s->mb_x = mb_x;\n s->mb_y = mb_y;\n decode_mb(s, 0 );\n }\n }\n if (s->pict_type == AV_PICTURE_TYPE_B) {\n for (mb_y = 0; mb_y < s->mb_height; mb_y++) {\n for (mb_x = 0; mb_x < s->mb_width; mb_x++) {\n int xy = mb_x * 2 + mb_y * 2 * s->b8_stride;\n const int mb_xy = mb_x + mb_y * s->mb_stride;\n const int mb_type = s->current_picture.f.mb_type[mb_xy];\n error = s->error_status_table[mb_xy];\n if (IS_INTRA(mb_type))\n continue;\n if (!(error & ER_MV_ERROR))\n continue;\n if (!(error & ER_AC_ERROR))\n continue;\n s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;\n if (!s->last_picture.f.data[0])\n s->mv_dir &= ~MV_DIR_FORWARD;\n if (!s->next_picture.f.data[0])\n s->mv_dir &= ~MV_DIR_BACKWARD;\n s->mb_intra = 0;\n s->mv_type = MV_TYPE_16X16;\n s->mb_skipped = 0;\n if (s->pp_time) {\n int time_pp = s->pp_time;\n int time_pb = s->pb_time;\n if (s->avctx->codec_id == CODEC_ID_H264) {\n } else {\n ff_thread_await_progress((AVFrame *) s->next_picture_ptr, mb_y, 0);\n }\n s->mv[0][0][0] = s->next_picture.f.motion_val[0][xy][0] * time_pb / time_pp;\n s->mv[0][0][1] = s->next_picture.f.motion_val[0][xy][1] * time_pb / time_pp;\n s->mv[1][0][0] = s->next_picture.f.motion_val[0][xy][0] * (time_pb - time_pp) / time_pp;\n s->mv[1][0][1] = s->next_picture.f.motion_val[0][xy][1] * (time_pb - time_pp) / time_pp;\n } else {\n s->mv[0][0][0] = 0;\n s->mv[0][0][1] = 0;\n s->mv[1][0][0] = 0;\n s->mv[1][0][1] = 0;\n }\n s->dsp.clear_blocks(s->block[0]);\n s->mb_x = mb_x;\n s->mb_y = mb_y;\n decode_mb(s, 0);\n }\n }\n } else\n guess_mv(s);\n if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)\n goto ec_clean;\n for (mb_y = 0; mb_y < s->mb_height; mb_y++) {\n for (mb_x = 0; mb_x < s->mb_width; mb_x++) {\n int dc, dcu, dcv, y, n;\n int16_t *dc_ptr;\n uint8_t *dest_y, *dest_cb, *dest_cr;\n const int mb_xy = mb_x + mb_y * s->mb_stride;\n const int mb_type = s->current_picture.f.mb_type[mb_xy];\n error = s->error_status_table[mb_xy];\n if (IS_INTRA(mb_type) && s->partitioned_frame)\n continue;\n dest_y = s->current_picture.f.data[0] + mb_x * 16 + mb_y * 16 * s->linesize;\n dest_cb = s->current_picture.f.data[1] + mb_x * 8 + mb_y * 8 * s->uvlinesize;\n dest_cr = s->current_picture.f.data[2] + mb_x * 8 + mb_y * 8 * s->uvlinesize;\n dc_ptr = &s->dc_val[0][mb_x * 2 + mb_y * 2 * s->b8_stride];\n for (n = 0; n < 4; n++) {\n dc = 0;\n for (y = 0; y < 8; y++) {\n int x;\n for (x = 0; x < 8; x++)\n dc += dest_y[x + (n & 1) * 8 +\n (y + (n >> 1) * 8) * s->linesize];\n }\n dc_ptr[(n & 1) + (n >> 1) * s->b8_stride] = (dc + 4) >> 3;\n }\n dcu = dcv = 0;\n for (y = 0; y < 8; y++) {\n int x;\n for (x = 0; x < 8; x++) {\n dcu += dest_cb[x + y * s->uvlinesize];\n dcv += dest_cr[x + y * s->uvlinesize];\n }\n }\n s->dc_val[1][mb_x + mb_y * s->mb_stride] = (dcu + 4) >> 3;\n s->dc_val[2][mb_x + mb_y * s->mb_stride] = (dcv + 4) >> 3;\n }\n }\n guess_dc(s, s->dc_val[0], s->mb_width * 2, s->mb_height * 2, s->b8_stride, 1);\n guess_dc(s, s->dc_val[1], s->mb_width, s->mb_height, s->mb_stride, 0);\n guess_dc(s, s->dc_val[2], s->mb_width, s->mb_height, s->mb_stride, 0);\n filter181(s->dc_val[0], s->mb_width * 2, s->mb_height * 2, s->b8_stride);\n for (mb_y = 0; mb_y < s->mb_height; mb_y++) {\n for (mb_x = 0; mb_x < s->mb_width; mb_x++) {\n uint8_t *dest_y, *dest_cb, *dest_cr;\n const int mb_xy = mb_x + mb_y * s->mb_stride;\n const int mb_type = s->current_picture.f.mb_type[mb_xy];\n error = s->error_status_table[mb_xy];\n if (IS_INTER(mb_type))\n continue;\n if (!(error & ER_AC_ERROR))\n continue;\n dest_y = s->current_picture.f.data[0] + mb_x * 16 + mb_y * 16 * s->linesize;\n dest_cb = s->current_picture.f.data[1] + mb_x * 8 + mb_y * 8 * s->uvlinesize;\n dest_cr = s->current_picture.f.data[2] + mb_x * 8 + mb_y * 8 * s->uvlinesize;\n put_dc(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);\n }\n }\n if (s->avctx->error_concealment & FF_EC_DEBLOCK) {\n h_block_filter(s, s->current_picture.f.data[0], s->mb_width * 2,\n s->mb_height * 2, s->linesize, 1);\n h_block_filter(s, s->current_picture.f.data[1], s->mb_width,\n s->mb_height , s->uvlinesize, 0);\n h_block_filter(s, s->current_picture.f.data[2], s->mb_width,\n s->mb_height , s->uvlinesize, 0);\n v_block_filter(s, s->current_picture.f.data[0], s->mb_width * 2,\n s->mb_height * 2, s->linesize, 1);\n v_block_filter(s, s->current_picture.f.data[1], s->mb_width,\n s->mb_height , s->uvlinesize, 0);\n v_block_filter(s, s->current_picture.f.data[2], s->mb_width,\n s->mb_height , s->uvlinesize, 0);\n }\nec_clean:\n for (i = 0; i < s->mb_num; i++) {\n const int mb_xy = s->mb_index2xy[i];\n int error = s->error_status_table[mb_xy];\n if (s->pict_type != AV_PICTURE_TYPE_B &&\n (error & (ER_DC_ERROR | ER_MV_ERROR | ER_AC_ERROR))) {\n s->mbskip_table[mb_xy] = 0;\n }\n s->mbintra_table[mb_xy] = 1;\n }\n}', 'static int is_intra_more_likely(MpegEncContext *s)\n{\n int is_intra_likely, i, j, undamaged_count, skip_amount, mb_x, mb_y;\n if (!s->last_picture_ptr || !s->last_picture_ptr->f.data[0])\n return 1;\n undamaged_count = 0;\n for (i = 0; i < s->mb_num; i++) {\n const int mb_xy = s->mb_index2xy[i];\n const int error = s->error_status_table[mb_xy];\n if (!((error & ER_DC_ERROR) && (error & ER_MV_ERROR)))\n undamaged_count++;\n }\n if (s->codec_id == CODEC_ID_H264) {\n H264Context *h = (void*) s;\n if (h->list_count <= 0 || h->ref_count[0] <= 0 ||\n !h->ref_list[0][0].f.data[0])\n return 1;\n }\n if (undamaged_count < 5)\n return 0;\n if (CONFIG_MPEG_XVMC_DECODER &&\n s->avctx->xvmc_acceleration &&\n s->pict_type == AV_PICTURE_TYPE_I)\n return 1;\n skip_amount = FFMAX(undamaged_count / 50, 1);\n is_intra_likely = 0;\n j = 0;\n for (mb_y = 0; mb_y < s->mb_height - 1; mb_y++) {\n for (mb_x = 0; mb_x < s->mb_width; mb_x++) {\n int error;\n const int mb_xy = mb_x + mb_y * s->mb_stride;\n error = s->error_status_table[mb_xy];\n if ((error & ER_DC_ERROR) && (error & ER_MV_ERROR))\n continue;\n j++;\n if ((j % skip_amount) != 0)\n continue;\n if (s->pict_type == AV_PICTURE_TYPE_I) {\n uint8_t *mb_ptr = s->current_picture.f.data[0] +\n mb_x * 16 + mb_y * 16 * s->linesize;\n uint8_t *last_mb_ptr = s->last_picture.f.data[0] +\n mb_x * 16 + mb_y * 16 * s->linesize;\n if (s->avctx->codec_id == CODEC_ID_H264) {\n } else {\n ff_thread_await_progress((AVFrame *) s->last_picture_ptr,\n mb_y, 0);\n }\n is_intra_likely += s->dsp.sad[0](NULL, last_mb_ptr, mb_ptr,\n s->linesize, 16);\n is_intra_likely -= s->dsp.sad[0](NULL, last_mb_ptr,\n last_mb_ptr + s->linesize * 16,\n s->linesize, 16);\n } else {\n if (IS_INTRA(s->current_picture.f.mb_type[mb_xy]))\n is_intra_likely++;\n else\n is_intra_likely--;\n }\n }\n }\n return is_intra_likely > 0;\n}', 'static void decode_mb(MpegEncContext *s, int ref)\n{\n s->dest[0] = s->current_picture.f.data[0] + (s->mb_y * 16 * s->linesize) + s->mb_x * 16;\n s->dest[1] = s->current_picture.f.data[1] + (s->mb_y * (16 >> s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16 >> s->chroma_x_shift);\n s->dest[2] = s->current_picture.f.data[2] + (s->mb_y * (16 >> s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16 >> s->chroma_x_shift);\n if (CONFIG_H264_DECODER && s->codec_id == CODEC_ID_H264) {\n H264Context *h = (void*)s;\n h->mb_xy = s->mb_x + s->mb_y * s->mb_stride;\n memset(h->non_zero_count_cache, 0, sizeof(h->non_zero_count_cache));\n assert(ref >= 0);\n if (ref >= h->ref_count[0])\n ref = 0;\n fill_rectangle(&s->current_picture.f.ref_index[0][4 * h->mb_xy],\n 2, 2, 2, ref, 1);\n fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);\n fill_rectangle(h->mv_cache[0][scan8[0]], 4, 4, 8,\n pack16to32(s->mv[0][0][0], s->mv[0][0][1]), 4);\n assert(!FRAME_MBAFF);\n ff_h264_hl_decode_mb(h);\n } else {\n assert(ref == 0);\n MPV_decode_mb(s, s->block);\n }\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.f.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.f.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.f.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 hl_motion(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, pixel_shift, 3);\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}', '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 chroma_idc)\n{\n MpegEncContext * const s = &h->s;\n const int mb_xy= h->mb_xy;\n const int mb_type = s->current_picture.f.mb_type[mb_xy];\n assert(IS_INTER(mb_type));\n if(HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))\n await_references(h);\n prefetch_motion(h, 0, pixel_shift, chroma_idc);\n if(IS_16X16(mb_type)){\n mc_part(h, 0, 1, 16, 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, chroma_idc);\n }else if(IS_16X8(mb_type)){\n mc_part(h, 0, 0, 8, 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, weight_avg,\n IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1),\n pixel_shift, chroma_idc);\n mc_part(h, 8, 0, 8, 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, weight_avg,\n IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1),\n pixel_shift, chroma_idc);\n }else if(IS_8X16(mb_type)){\n mc_part(h, 0, 0, 16, 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[1], &weight_avg[1],\n IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1),\n pixel_shift, chroma_idc);\n mc_part(h, 4, 0, 16, 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[1], &weight_avg[1],\n IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1),\n pixel_shift, chroma_idc);\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, 8, 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[1], &weight_avg[1],\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),\n pixel_shift, chroma_idc);\n }else if(IS_SUB_8X4(sub_mb_type)){\n mc_part(h, n , 0, 4, 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[1], &weight_avg[1],\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),\n pixel_shift, chroma_idc);\n mc_part(h, n+2, 0, 4, 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[1], &weight_avg[1],\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),\n pixel_shift, chroma_idc);\n }else if(IS_SUB_4X8(sub_mb_type)){\n mc_part(h, n , 0, 8, 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[2], &weight_avg[2],\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),\n pixel_shift, chroma_idc);\n mc_part(h, n+1, 0, 8, 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[2], &weight_avg[2],\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),\n pixel_shift, chroma_idc);\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, 4, 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[2], &weight_avg[2],\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),\n pixel_shift, chroma_idc);\n }\n }\n }\n }\n prefetch_motion(h, 1, pixel_shift, chroma_idc);\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.f.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->f.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}']
|
1,098 | 0 |
https://github.com/openssl/openssl/blob/985de8634000df9b33b8ac4519fa10a99e43b314/engines/e_4758cca.c/#L607
|
static int cca_rsa_priv_dec(int flen, const unsigned char *from,
unsigned char *to, RSA *rsa,int padding)
{
long returnCode;
long reasonCode;
long lflen = flen;
long exitDataLength = 0;
unsigned char exitData[8];
long ruleArrayLength = 1;
unsigned char ruleArray[8] = "PKCS-1.2";
long dataStructureLength = 0;
unsigned char dataStructure[8];
long outputLength = RSA_size(rsa);
long keyTokenLength;
unsigned char* keyToken = (unsigned char*)RSA_get_ex_data(rsa, hndidx);
keyTokenLength = *(long*)keyToken;
keyToken+=sizeof(long);
pkaDecrypt(&returnCode, &reasonCode, &exitDataLength, exitData,
&ruleArrayLength, ruleArray, &lflen, (unsigned char*)from,
&dataStructureLength, dataStructure, &keyTokenLength,
keyToken, &outputLength, to);
return (returnCode | reasonCode) ? 0 : 1;
}
|
['static int cca_rsa_priv_dec(int flen, const unsigned char *from,\n\t\t\tunsigned char *to, RSA *rsa,int padding)\n\t{\n\tlong returnCode;\n\tlong reasonCode;\n\tlong lflen = flen;\n\tlong exitDataLength = 0;\n\tunsigned char exitData[8];\n\tlong ruleArrayLength = 1;\n\tunsigned char ruleArray[8] = "PKCS-1.2";\n\tlong dataStructureLength = 0;\n\tunsigned char dataStructure[8];\n\tlong outputLength = RSA_size(rsa);\n\tlong keyTokenLength;\n\tunsigned char* keyToken = (unsigned char*)RSA_get_ex_data(rsa, hndidx);\n\tkeyTokenLength = *(long*)keyToken;\n\tkeyToken+=sizeof(long);\n\tpkaDecrypt(&returnCode, &reasonCode, &exitDataLength, exitData,\n\t\t&ruleArrayLength, ruleArray, &lflen, (unsigned char*)from,\n\t\t&dataStructureLength, dataStructure, &keyTokenLength,\n\t\tkeyToken, &outputLength, to);\n\treturn (returnCode | reasonCode) ? 0 : 1;\n\t}', 'int RSA_size(const RSA *r)\n\t{\n\treturn(BN_num_bytes(r->n));\n\t}', 'int BN_num_bits(const BIGNUM *a)\n\t{\n\tint i = a->top - 1;\n\tbn_check_top(a);\n\tif (BN_is_zero(a)) return 0;\n\treturn ((i*BN_BITS2) + BN_num_bits_word(a->d[i]));\n\t}', 'void *RSA_get_ex_data(const RSA *r, int idx)\n\t{\n\treturn(CRYPTO_get_ex_data(&r->ex_data,idx));\n\t}', 'void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)\n\t{\n\tif (ad->sk == NULL)\n\t\treturn(0);\n\telse if (idx >= sk_void_num(ad->sk))\n\t\treturn(0);\n\telse\n\t\treturn(sk_void_value(ad->sk,idx));\n\t}', 'int sk_num(const _STACK *st)\n{\n\tif(st == NULL) return -1;\n\treturn st->num;\n}', 'void *sk_value(const _STACK *st, int i)\n{\n\tif(!st || (i < 0) || (i >= st->num)) return NULL;\n\treturn st->data[i];\n}']
|
1,099 | 0 |
https://github.com/libav/libav/blob/dad7a9c7c0ae8ebc56f2e3a24e6fa4da5c2cd491/libavcodec/bitstream.h/#L237
|
static inline void skip_remaining(BitstreamContext *bc, unsigned n)
{
#ifdef BITSTREAM_READER_LE
bc->bits >>= n;
#else
bc->bits <<= n;
#endif
bc->bits_left -= n;
}
|
['static int decode_seq_header(AVSContext *h)\n{\n int frame_rate_code;\n int width, height;\n h->profile = bitstream_read(&h->bc, 8);\n h->level = bitstream_read(&h->bc, 8);\n bitstream_skip(&h->bc, 1);\n width = bitstream_read(&h->bc, 14);\n height = bitstream_read(&h->bc, 14);\n if ((h->width || h->height) && (h->width != width || h->height != height)) {\n avpriv_report_missing_feature(h->avctx,\n "Width/height changing in CAVS");\n return AVERROR_PATCHWELCOME;\n }\n h->width = width;\n h->height = height;\n bitstream_skip(&h->bc, 2);\n bitstream_skip(&h->bc, 3);\n h->aspect_ratio = bitstream_read(&h->bc, 4);\n frame_rate_code = bitstream_read(&h->bc, 4);\n bitstream_skip(&h->bc, 18);\n bitstream_skip(&h->bc, 1);\n bitstream_skip(&h->bc, 12);\n h->low_delay = bitstream_read_bit(&h->bc);\n h->mb_width = (h->width + 15) >> 4;\n h->mb_height = (h->height + 15) >> 4;\n h->avctx->framerate = ff_mpeg12_frame_rate_tab[frame_rate_code];\n h->avctx->width = h->width;\n h->avctx->height = h->height;\n if (!h->top_qp)\n ff_cavs_init_top_lines(h);\n return 0;\n}', 'static inline void bitstream_skip(BitstreamContext *bc, unsigned n)\n{\n if (n <= bc->bits_left)\n skip_remaining(bc, n);\n else {\n n -= bc->bits_left;\n skip_remaining(bc, bc->bits_left);\n if (n >= 64) {\n unsigned skip = n / 8;\n n -= skip * 8;\n bc->ptr += skip;\n }\n refill_64(bc);\n if (n)\n skip_remaining(bc, n);\n }\n}', 'static inline void skip_remaining(BitstreamContext *bc, unsigned n)\n{\n#ifdef BITSTREAM_READER_LE\n bc->bits >>= n;\n#else\n bc->bits <<= n;\n#endif\n bc->bits_left -= n;\n}']
|
1,100 | 0 |
https://github.com/libav/libav/blob/af2faf2076b96ab85cc51d5e970574079373c396/libswscale/swscale.c/#L1165
|
static void yuv2packed2_c(SwsContext *c, const uint16_t *buf0,
const uint16_t *buf1, const uint16_t *ubuf0,
const uint16_t *ubuf1, const uint16_t *vbuf0,
const uint16_t *vbuf1, const uint16_t *abuf0,
const uint16_t *abuf1, uint8_t *dest, int dstW,
int yalpha, int uvalpha, int y)
{
int yalpha1=4095- yalpha;
int uvalpha1=4095-uvalpha;
int i;
YSCALE_YUV_2_ANYRGB_C(YSCALE_YUV_2_RGB2_C)
}
|
['static void yuv2packed2_c(SwsContext *c, const uint16_t *buf0,\n const uint16_t *buf1, const uint16_t *ubuf0,\n const uint16_t *ubuf1, const uint16_t *vbuf0,\n const uint16_t *vbuf1, const uint16_t *abuf0,\n const uint16_t *abuf1, uint8_t *dest, int dstW,\n int yalpha, int uvalpha, int y)\n{\n int yalpha1=4095- yalpha;\n int uvalpha1=4095-uvalpha;\n int i;\n YSCALE_YUV_2_ANYRGB_C(YSCALE_YUV_2_RGB2_C)\n}']
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.