id
int64
1
36.7k
label
int64
0
1
bug_url
stringlengths
91
134
bug_function
stringlengths
13
72.7k
functions
stringlengths
17
79.2k
2,301
0
https://github.com/libav/libav/blob/a2fb4bcb0189f6421608e0dec1a38c65910763f6/libavcodec/cook.c/#L752
static void decouple_info(COOKContext *q, COOKSubpacket *p, int *decouple_tab) { int i; int vlc = get_bits1(&q->gb); int start = cplband[p->js_subband_start]; int end = cplband[p->subbands-1]; int length = end - start + 1; if (start > end) return; if (vlc) { for (i = 0; i < length; i++) decouple_tab[start + i] = get_vlc2(&q->gb, p->ccpl.table, p->ccpl.bits, 2); } else { for (i = 0; i < length; i++) decouple_tab[start + i] = get_bits(&q->gb, p->js_vlc_bits); } }
['static int cook_decode_frame(AVCodecContext *avctx, void *data,\n int *got_frame_ptr, AVPacket *avpkt)\n{\n const uint8_t *buf = avpkt->data;\n int buf_size = avpkt->size;\n COOKContext *q = avctx->priv_data;\n float *samples = NULL;\n int i, ret;\n int offset = 0;\n int chidx = 0;\n if (buf_size < avctx->block_align)\n return buf_size;\n if (q->discarded_packets >= 2) {\n q->frame.nb_samples = q->samples_per_channel;\n if ((ret = avctx->get_buffer(avctx, &q->frame)) < 0) {\n av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n return ret;\n }\n samples = (float *)q->frame.data[0];\n }\n q->subpacket[0].size = avctx->block_align;\n for(i=1;i<q->num_subpackets;i++){\n q->subpacket[i].size = 2 * buf[avctx->block_align - q->num_subpackets + i];\n q->subpacket[0].size -= q->subpacket[i].size + 1;\n if (q->subpacket[0].size < 0) {\n av_log(avctx,AV_LOG_DEBUG,"frame subpacket size total > avctx->block_align!\\n");\n return AVERROR_INVALIDDATA;\n }\n }\n for(i=0;i<q->num_subpackets;i++){\n q->subpacket[i].bits_per_subpacket = (q->subpacket[i].size*8)>>q->subpacket[i].bits_per_subpdiv;\n q->subpacket[i].ch_idx = chidx;\n av_log(avctx,AV_LOG_DEBUG,"subpacket[%i] size %i js %i %i block_align %i\\n",i,q->subpacket[i].size,q->subpacket[i].joint_stereo,offset,avctx->block_align);\n decode_subpacket(q, &q->subpacket[i], buf + offset, samples);\n offset += q->subpacket[i].size;\n chidx += q->subpacket[i].num_channels;\n av_log(avctx,AV_LOG_DEBUG,"subpacket[%i] %i %i\\n",i,q->subpacket[i].size * 8,get_bits_count(&q->gb));\n }\n if (q->discarded_packets < 2) {\n q->discarded_packets++;\n *got_frame_ptr = 0;\n return avctx->block_align;\n }\n *got_frame_ptr = 1;\n *(AVFrame *)data = q->frame;\n return avctx->block_align;\n}', 'static void decode_subpacket(COOKContext *q, COOKSubpacket *p,\n const uint8_t *inbuffer, float *outbuffer)\n{\n int sub_packet_size = p->size;\n memset(q->decode_buffer_1,0,sizeof(q->decode_buffer_1));\n decode_bytes_and_gain(q, p, inbuffer, &p->gains1);\n if (p->joint_stereo) {\n joint_decode(q, p, q->decode_buffer_1, q->decode_buffer_2);\n } else {\n mono_decode(q, p, q->decode_buffer_1);\n if (p->num_channels == 2) {\n decode_bytes_and_gain(q, p, inbuffer + sub_packet_size/2, &p->gains2);\n mono_decode(q, p, q->decode_buffer_2);\n }\n }\n mlt_compensate_output(q, q->decode_buffer_1, &p->gains1,\n p->mono_previous_buffer1, outbuffer, p->ch_idx);\n if (p->num_channels == 2) {\n if (p->joint_stereo) {\n mlt_compensate_output(q, q->decode_buffer_2, &p->gains1,\n p->mono_previous_buffer2, outbuffer, p->ch_idx + 1);\n } else {\n mlt_compensate_output(q, q->decode_buffer_2, &p->gains2,\n p->mono_previous_buffer2, outbuffer, p->ch_idx + 1);\n }\n }\n}', 'static void joint_decode(COOKContext *q, COOKSubpacket *p, float* mlt_buffer1,\n float* mlt_buffer2) {\n int i,j;\n int decouple_tab[SUBBAND_SIZE];\n float *decode_buffer = q->decode_buffer_0;\n int idx, cpl_tmp;\n float f1,f2;\n const float* cplscale;\n memset(decouple_tab, 0, sizeof(decouple_tab));\n memset(decode_buffer, 0, sizeof(q->decode_buffer_0));\n memset(mlt_buffer1, 0, 1024 * sizeof(*mlt_buffer1));\n memset(mlt_buffer2, 0, 1024 * sizeof(*mlt_buffer2));\n decouple_info(q, p, decouple_tab);\n mono_decode(q, p, decode_buffer);\n for (i=0 ; i<p->js_subband_start ; i++) {\n for (j=0 ; j<SUBBAND_SIZE ; j++) {\n mlt_buffer1[i*20+j] = decode_buffer[i*40+j];\n mlt_buffer2[i*20+j] = decode_buffer[i*40+20+j];\n }\n }\n idx = (1 << p->js_vlc_bits) - 1;\n for (i=p->js_subband_start ; i<p->subbands ; i++) {\n cpl_tmp = cplband[i];\n idx -=decouple_tab[cpl_tmp];\n cplscale = q->cplscales[p->js_vlc_bits-2];\n f1 = cplscale[decouple_tab[cpl_tmp]];\n f2 = cplscale[idx-1];\n q->decouple (q, p, i, f1, f2, decode_buffer, mlt_buffer1, mlt_buffer2);\n idx = (1 << p->js_vlc_bits) - 1;\n }\n}', 'static void decouple_info(COOKContext *q, COOKSubpacket *p, int *decouple_tab)\n{\n int i;\n int vlc = get_bits1(&q->gb);\n int start = cplband[p->js_subband_start];\n int end = cplband[p->subbands-1];\n int length = end - start + 1;\n if (start > end)\n return;\n if (vlc) {\n for (i = 0; i < length; i++)\n decouple_tab[start + i] = get_vlc2(&q->gb, p->ccpl.table, p->ccpl.bits, 2);\n } else {\n for (i = 0; i < length; i++)\n decouple_tab[start + i] = get_bits(&q->gb, p->js_vlc_bits);\n }\n}']
2,302
0
https://github.com/libav/libav/blob/b7847a3f2eb56591590ab065b65c335892af6e4c/libavcodec/motion_est.c/#L1267
int ff_pre_estimate_p_frame_motion(MpegEncContext * s, int mb_x, int mb_y) { MotionEstContext * const c= &s->me; int mx, my, dmin; int P[10][2]; const int shift= 1+s->quarter_sample; const int xy= mb_x + mb_y*s->mb_stride; init_ref(c, s->new_picture.data, s->last_picture.data, NULL, 16*mb_x, 16*mb_y, 0); assert(s->quarter_sample==0 || s->quarter_sample==1); c->pre_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_pre_cmp); c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV; get_limits(s, 16*mb_x, 16*mb_y); c->skip=0; P_LEFT[0] = s->p_mv_table[xy + 1][0]; P_LEFT[1] = s->p_mv_table[xy + 1][1]; if(P_LEFT[0] < (c->xmin<<shift)) P_LEFT[0] = (c->xmin<<shift); if (s->first_slice_line) { c->pred_x= P_LEFT[0]; c->pred_y= P_LEFT[1]; P_TOP[0]= P_TOPRIGHT[0]= P_MEDIAN[0]= P_TOP[1]= P_TOPRIGHT[1]= P_MEDIAN[1]= 0; } else { P_TOP[0] = s->p_mv_table[xy + s->mb_stride ][0]; P_TOP[1] = s->p_mv_table[xy + s->mb_stride ][1]; P_TOPRIGHT[0] = s->p_mv_table[xy + s->mb_stride - 1][0]; P_TOPRIGHT[1] = s->p_mv_table[xy + s->mb_stride - 1][1]; if(P_TOP[1] < (c->ymin<<shift)) P_TOP[1] = (c->ymin<<shift); if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift); if(P_TOPRIGHT[1] < (c->ymin<<shift)) P_TOPRIGHT[1]= (c->ymin<<shift); P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]); P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]); c->pred_x = P_MEDIAN[0]; c->pred_y = P_MEDIAN[1]; } dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16); s->p_mv_table[xy][0] = mx<<shift; s->p_mv_table[xy][1] = my<<shift; return dmin; }
['int ff_pre_estimate_p_frame_motion(MpegEncContext * s,\n int mb_x, int mb_y)\n{\n MotionEstContext * const c= &s->me;\n int mx, my, dmin;\n int P[10][2];\n const int shift= 1+s->quarter_sample;\n const int xy= mb_x + mb_y*s->mb_stride;\n init_ref(c, s->new_picture.data, s->last_picture.data, NULL, 16*mb_x, 16*mb_y, 0);\n assert(s->quarter_sample==0 || s->quarter_sample==1);\n c->pre_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_pre_cmp);\n c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;\n get_limits(s, 16*mb_x, 16*mb_y);\n c->skip=0;\n P_LEFT[0] = s->p_mv_table[xy + 1][0];\n P_LEFT[1] = s->p_mv_table[xy + 1][1];\n if(P_LEFT[0] < (c->xmin<<shift)) P_LEFT[0] = (c->xmin<<shift);\n if (s->first_slice_line) {\n c->pred_x= P_LEFT[0];\n c->pred_y= P_LEFT[1];\n P_TOP[0]= P_TOPRIGHT[0]= P_MEDIAN[0]=\n P_TOP[1]= P_TOPRIGHT[1]= P_MEDIAN[1]= 0;\n } else {\n P_TOP[0] = s->p_mv_table[xy + s->mb_stride ][0];\n P_TOP[1] = s->p_mv_table[xy + s->mb_stride ][1];\n P_TOPRIGHT[0] = s->p_mv_table[xy + s->mb_stride - 1][0];\n P_TOPRIGHT[1] = s->p_mv_table[xy + s->mb_stride - 1][1];\n if(P_TOP[1] < (c->ymin<<shift)) P_TOP[1] = (c->ymin<<shift);\n if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift);\n if(P_TOPRIGHT[1] < (c->ymin<<shift)) P_TOPRIGHT[1]= (c->ymin<<shift);\n P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);\n P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);\n c->pred_x = P_MEDIAN[0];\n c->pred_y = P_MEDIAN[1];\n }\n dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);\n s->p_mv_table[xy][0] = mx<<shift;\n s->p_mv_table[xy][1] = my<<shift;\n return dmin;\n}', 'static inline void init_ref(MotionEstContext *c, uint8_t *src[3], uint8_t *ref[3], uint8_t *ref2[3], int x, int y, int ref_index){\n const int offset[3]= {\n y*c-> stride + x,\n ((y*c->uvstride + x)>>1),\n ((y*c->uvstride + x)>>1),\n };\n int i;\n for(i=0; i<3; i++){\n c->src[0][i]= src [i] + offset[i];\n c->ref[0][i]= ref [i] + offset[i];\n }\n if(ref_index){\n for(i=0; i<3; i++){\n c->ref[ref_index][i]= ref2[i] + offset[i];\n }\n }\n}']
2,303
0
https://github.com/libav/libav/blob/e4e30256f87f177decf59b59e923d05ef64147df/libavcodec/mpegvideo_enc.c/#L1968
static av_always_inline void encode_mb_internal(MpegEncContext *s, int motion_x, int motion_y, int mb_block_height, int mb_block_count) { int16_t weight[8][64]; DCTELEM orig[8][64]; const int mb_x = s->mb_x; const int mb_y = s->mb_y; int i; int skip_dct[8]; int dct_offset = s->linesize * 8; uint8_t *ptr_y, *ptr_cb, *ptr_cr; int wrap_y, wrap_c; for (i = 0; i < mb_block_count; i++) skip_dct[i] = s->skipdct; if (s->adaptive_quant) { const int last_qp = s->qscale; const int mb_xy = mb_x + mb_y * s->mb_stride; s->lambda = s->lambda_table[mb_xy]; update_qscale(s); if (!(s->flags & CODEC_FLAG_QP_RD)) { s->qscale = s->current_picture_ptr->f.qscale_table[mb_xy]; s->dquant = s->qscale - last_qp; if (s->out_format == FMT_H263) { s->dquant = av_clip(s->dquant, -2, 2); if (s->codec_id == CODEC_ID_MPEG4) { if (!s->mb_intra) { if (s->pict_type == AV_PICTURE_TYPE_B) { if (s->dquant & 1 || s->mv_dir & MV_DIRECT) s->dquant = 0; } if (s->mv_type == MV_TYPE_8X8) s->dquant = 0; } } } } ff_set_qscale(s, last_qp + s->dquant); } else if (s->flags & CODEC_FLAG_QP_RD) ff_set_qscale(s, s->qscale + s->dquant); wrap_y = s->linesize; wrap_c = s->uvlinesize; ptr_y = s->new_picture.f.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16; ptr_cb = s->new_picture.f.data[1] + (mb_y * mb_block_height * wrap_c) + mb_x * 8; ptr_cr = s->new_picture.f.data[2] + (mb_y * mb_block_height * wrap_c) + mb_x * 8; if (mb_x * 16 + 16 > s->width || mb_y * 16 + 16 > s->height) { uint8_t *ebuf = s->edge_emu_buffer + 32; s->dsp.emulated_edge_mc(ebuf, ptr_y, wrap_y, 16, 16, mb_x * 16, mb_y * 16, s->width, s->height); ptr_y = ebuf; s->dsp.emulated_edge_mc(ebuf + 18 * wrap_y, ptr_cb, wrap_c, 8, mb_block_height, mb_x * 8, mb_y * 8, s->width >> 1, s->height >> 1); ptr_cb = ebuf + 18 * wrap_y; s->dsp.emulated_edge_mc(ebuf + 18 * wrap_y + 8, ptr_cr, wrap_c, 8, mb_block_height, mb_x * 8, mb_y * 8, s->width >> 1, s->height >> 1); ptr_cr = ebuf + 18 * wrap_y + 8; } if (s->mb_intra) { if (s->flags & CODEC_FLAG_INTERLACED_DCT) { int progressive_score, interlaced_score; s->interlaced_dct = 0; progressive_score = s->dsp.ildct_cmp[4](s, ptr_y, NULL, wrap_y, 8) + s->dsp.ildct_cmp[4](s, ptr_y + wrap_y * 8, NULL, wrap_y, 8) - 400; if (progressive_score > 0) { interlaced_score = s->dsp.ildct_cmp[4](s, ptr_y, NULL, wrap_y * 2, 8) + s->dsp.ildct_cmp[4](s, ptr_y + wrap_y, NULL, wrap_y * 2, 8); if (progressive_score > interlaced_score) { s->interlaced_dct = 1; dct_offset = wrap_y; wrap_y <<= 1; if (s->chroma_format == CHROMA_422) wrap_c <<= 1; } } } s->dsp.get_pixels(s->block[0], ptr_y , wrap_y); s->dsp.get_pixels(s->block[1], ptr_y + 8 , wrap_y); s->dsp.get_pixels(s->block[2], ptr_y + dct_offset , wrap_y); s->dsp.get_pixels(s->block[3], ptr_y + dct_offset + 8 , wrap_y); if (s->flags & CODEC_FLAG_GRAY) { skip_dct[4] = 1; skip_dct[5] = 1; } else { s->dsp.get_pixels(s->block[4], ptr_cb, wrap_c); s->dsp.get_pixels(s->block[5], ptr_cr, wrap_c); if (!s->chroma_y_shift) { s->dsp.get_pixels(s->block[6], ptr_cb + (dct_offset >> 1), wrap_c); s->dsp.get_pixels(s->block[7], ptr_cr + (dct_offset >> 1), wrap_c); } } } else { op_pixels_func (*op_pix)[4]; qpel_mc_func (*op_qpix)[16]; uint8_t *dest_y, *dest_cb, *dest_cr; dest_y = s->dest[0]; dest_cb = s->dest[1]; dest_cr = s->dest[2]; if ((!s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) { op_pix = s->dsp.put_pixels_tab; op_qpix = s->dsp.put_qpel_pixels_tab; } else { op_pix = s->dsp.put_no_rnd_pixels_tab; op_qpix = s->dsp.put_no_rnd_qpel_pixels_tab; } if (s->mv_dir & MV_DIR_FORWARD) { MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix, op_qpix); op_pix = s->dsp.avg_pixels_tab; op_qpix = s->dsp.avg_qpel_pixels_tab; } if (s->mv_dir & MV_DIR_BACKWARD) { MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix, op_qpix); } if (s->flags & CODEC_FLAG_INTERLACED_DCT) { int progressive_score, interlaced_score; s->interlaced_dct = 0; progressive_score = s->dsp.ildct_cmp[0](s, dest_y, ptr_y, wrap_y, 8) + s->dsp.ildct_cmp[0](s, dest_y + wrap_y * 8, ptr_y + wrap_y * 8, wrap_y, 8) - 400; if (s->avctx->ildct_cmp == FF_CMP_VSSE) progressive_score -= 400; if (progressive_score > 0) { interlaced_score = s->dsp.ildct_cmp[0](s, dest_y, ptr_y, wrap_y * 2, 8) + s->dsp.ildct_cmp[0](s, dest_y + wrap_y, ptr_y + wrap_y, wrap_y * 2, 8); if (progressive_score > interlaced_score) { s->interlaced_dct = 1; dct_offset = wrap_y; wrap_y <<= 1; if (s->chroma_format == CHROMA_422) wrap_c <<= 1; } } } s->dsp.diff_pixels(s->block[0], ptr_y, dest_y, wrap_y); s->dsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y); s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset, dest_y + dct_offset, wrap_y); s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y); if (s->flags & CODEC_FLAG_GRAY) { skip_dct[4] = 1; skip_dct[5] = 1; } else { s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c); s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c); if (!s->chroma_y_shift) { s->dsp.diff_pixels(s->block[6], ptr_cb + (dct_offset >> 1), dest_cb + (dct_offset >> 1), wrap_c); s->dsp.diff_pixels(s->block[7], ptr_cr + (dct_offset >> 1), dest_cr + (dct_offset >> 1), wrap_c); } } if (s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] < 2 * s->qscale * s->qscale) { if (s->dsp.sad[1](NULL, ptr_y , dest_y, wrap_y, 8) < 20 * s->qscale) skip_dct[0] = 1; if (s->dsp.sad[1](NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20 * s->qscale) skip_dct[1] = 1; if (s->dsp.sad[1](NULL, ptr_y + dct_offset, dest_y + dct_offset, wrap_y, 8) < 20 * s->qscale) skip_dct[2] = 1; if (s->dsp.sad[1](NULL, ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y, 8) < 20 * s->qscale) skip_dct[3] = 1; if (s->dsp.sad[1](NULL, ptr_cb, dest_cb, wrap_c, 8) < 20 * s->qscale) skip_dct[4] = 1; if (s->dsp.sad[1](NULL, ptr_cr, dest_cr, wrap_c, 8) < 20 * s->qscale) skip_dct[5] = 1; if (!s->chroma_y_shift) { if (s->dsp.sad[1](NULL, ptr_cb + (dct_offset >> 1), dest_cb + (dct_offset >> 1), wrap_c, 8) < 20 * s->qscale) skip_dct[6] = 1; if (s->dsp.sad[1](NULL, ptr_cr + (dct_offset >> 1), dest_cr + (dct_offset >> 1), wrap_c, 8) < 20 * s->qscale) skip_dct[7] = 1; } } } if (s->avctx->quantizer_noise_shaping) { if (!skip_dct[0]) get_visual_weight(weight[0], ptr_y , wrap_y); if (!skip_dct[1]) get_visual_weight(weight[1], ptr_y + 8, wrap_y); if (!skip_dct[2]) get_visual_weight(weight[2], ptr_y + dct_offset , wrap_y); if (!skip_dct[3]) get_visual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y); if (!skip_dct[4]) get_visual_weight(weight[4], ptr_cb , wrap_c); if (!skip_dct[5]) get_visual_weight(weight[5], ptr_cr , wrap_c); if (!s->chroma_y_shift) { if (!skip_dct[6]) get_visual_weight(weight[6], ptr_cb + (dct_offset >> 1), wrap_c); if (!skip_dct[7]) get_visual_weight(weight[7], ptr_cr + (dct_offset >> 1), wrap_c); } memcpy(orig[0], s->block[0], sizeof(DCTELEM) * 64 * mb_block_count); } assert(s->out_format != FMT_MJPEG || s->qscale == 8); { for (i = 0; i < mb_block_count; i++) { if (!skip_dct[i]) { int overflow; s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow); if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]); } else s->block_last_index[i] = -1; } if (s->avctx->quantizer_noise_shaping) { for (i = 0; i < mb_block_count; i++) { if (!skip_dct[i]) { s->block_last_index[i] = dct_quantize_refine(s, s->block[i], weight[i], orig[i], i, s->qscale); } } } if (s->luma_elim_threshold && !s->mb_intra) for (i = 0; i < 4; i++) dct_single_coeff_elimination(s, i, s->luma_elim_threshold); if (s->chroma_elim_threshold && !s->mb_intra) for (i = 4; i < mb_block_count; i++) dct_single_coeff_elimination(s, i, s->chroma_elim_threshold); if (s->flags & CODEC_FLAG_CBP_RD) { for (i = 0; i < mb_block_count; i++) { if (s->block_last_index[i] == -1) s->coded_score[i] = INT_MAX / 256; } } } if ((s->flags & CODEC_FLAG_GRAY) && s->mb_intra) { s->block_last_index[4] = s->block_last_index[5] = 0; s->block[4][0] = s->block[5][0] = (1024 + s->c_dc_scale / 2) / s->c_dc_scale; } if (s->alternate_scan && s->dct_quantize != dct_quantize_c) { for (i = 0; i < mb_block_count; i++) { int j; if (s->block_last_index[i] > 0) { for (j = 63; j > 0; j--) { if (s->block[i][s->intra_scantable.permutated[j]]) break; } s->block_last_index[i] = j; } } } switch(s->codec_id){ case CODEC_ID_MPEG1VIDEO: case CODEC_ID_MPEG2VIDEO: if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) mpeg1_encode_mb(s, s->block, motion_x, motion_y); break; case CODEC_ID_MPEG4: if (CONFIG_MPEG4_ENCODER) mpeg4_encode_mb(s, s->block, motion_x, motion_y); break; case CODEC_ID_MSMPEG4V2: case CODEC_ID_MSMPEG4V3: case CODEC_ID_WMV1: if (CONFIG_MSMPEG4_ENCODER) msmpeg4_encode_mb(s, s->block, motion_x, motion_y); break; case CODEC_ID_WMV2: if (CONFIG_WMV2_ENCODER) ff_wmv2_encode_mb(s, s->block, motion_x, motion_y); break; case CODEC_ID_H261: if (CONFIG_H261_ENCODER) ff_h261_encode_mb(s, s->block, motion_x, motion_y); break; case CODEC_ID_H263: case CODEC_ID_H263P: case CODEC_ID_FLV1: case CODEC_ID_RV10: case CODEC_ID_RV20: if (CONFIG_H263_ENCODER) h263_encode_mb(s, s->block, motion_x, motion_y); break; case CODEC_ID_MJPEG: if (CONFIG_MJPEG_ENCODER) ff_mjpeg_encode_mb(s, s->block); break; default: assert(0); } }
['static av_always_inline void encode_mb_internal(MpegEncContext *s,\n int motion_x, int motion_y,\n int mb_block_height,\n int mb_block_count)\n{\n int16_t weight[8][64];\n DCTELEM orig[8][64];\n const int mb_x = s->mb_x;\n const int mb_y = s->mb_y;\n int i;\n int skip_dct[8];\n int dct_offset = s->linesize * 8;\n uint8_t *ptr_y, *ptr_cb, *ptr_cr;\n int wrap_y, wrap_c;\n for (i = 0; i < mb_block_count; i++)\n skip_dct[i] = s->skipdct;\n if (s->adaptive_quant) {\n const int last_qp = s->qscale;\n const int mb_xy = mb_x + mb_y * s->mb_stride;\n s->lambda = s->lambda_table[mb_xy];\n update_qscale(s);\n if (!(s->flags & CODEC_FLAG_QP_RD)) {\n s->qscale = s->current_picture_ptr->f.qscale_table[mb_xy];\n s->dquant = s->qscale - last_qp;\n if (s->out_format == FMT_H263) {\n s->dquant = av_clip(s->dquant, -2, 2);\n if (s->codec_id == CODEC_ID_MPEG4) {\n if (!s->mb_intra) {\n if (s->pict_type == AV_PICTURE_TYPE_B) {\n if (s->dquant & 1 || s->mv_dir & MV_DIRECT)\n s->dquant = 0;\n }\n if (s->mv_type == MV_TYPE_8X8)\n s->dquant = 0;\n }\n }\n }\n }\n ff_set_qscale(s, last_qp + s->dquant);\n } else if (s->flags & CODEC_FLAG_QP_RD)\n ff_set_qscale(s, s->qscale + s->dquant);\n wrap_y = s->linesize;\n wrap_c = s->uvlinesize;\n ptr_y = s->new_picture.f.data[0] +\n (mb_y * 16 * wrap_y) + mb_x * 16;\n ptr_cb = s->new_picture.f.data[1] +\n (mb_y * mb_block_height * wrap_c) + mb_x * 8;\n ptr_cr = s->new_picture.f.data[2] +\n (mb_y * mb_block_height * wrap_c) + mb_x * 8;\n if (mb_x * 16 + 16 > s->width || mb_y * 16 + 16 > s->height) {\n uint8_t *ebuf = s->edge_emu_buffer + 32;\n s->dsp.emulated_edge_mc(ebuf, ptr_y, wrap_y, 16, 16, mb_x * 16,\n mb_y * 16, s->width, s->height);\n ptr_y = ebuf;\n s->dsp.emulated_edge_mc(ebuf + 18 * wrap_y, ptr_cb, wrap_c, 8,\n mb_block_height, mb_x * 8, mb_y * 8,\n s->width >> 1, s->height >> 1);\n ptr_cb = ebuf + 18 * wrap_y;\n s->dsp.emulated_edge_mc(ebuf + 18 * wrap_y + 8, ptr_cr, wrap_c, 8,\n mb_block_height, mb_x * 8, mb_y * 8,\n s->width >> 1, s->height >> 1);\n ptr_cr = ebuf + 18 * wrap_y + 8;\n }\n if (s->mb_intra) {\n if (s->flags & CODEC_FLAG_INTERLACED_DCT) {\n int progressive_score, interlaced_score;\n s->interlaced_dct = 0;\n progressive_score = s->dsp.ildct_cmp[4](s, ptr_y,\n NULL, wrap_y, 8) +\n s->dsp.ildct_cmp[4](s, ptr_y + wrap_y * 8,\n NULL, wrap_y, 8) - 400;\n if (progressive_score > 0) {\n interlaced_score = s->dsp.ildct_cmp[4](s, ptr_y,\n NULL, wrap_y * 2, 8) +\n s->dsp.ildct_cmp[4](s, ptr_y + wrap_y,\n NULL, wrap_y * 2, 8);\n if (progressive_score > interlaced_score) {\n s->interlaced_dct = 1;\n dct_offset = wrap_y;\n wrap_y <<= 1;\n if (s->chroma_format == CHROMA_422)\n wrap_c <<= 1;\n }\n }\n }\n s->dsp.get_pixels(s->block[0], ptr_y , wrap_y);\n s->dsp.get_pixels(s->block[1], ptr_y + 8 , wrap_y);\n s->dsp.get_pixels(s->block[2], ptr_y + dct_offset , wrap_y);\n s->dsp.get_pixels(s->block[3], ptr_y + dct_offset + 8 , wrap_y);\n if (s->flags & CODEC_FLAG_GRAY) {\n skip_dct[4] = 1;\n skip_dct[5] = 1;\n } else {\n s->dsp.get_pixels(s->block[4], ptr_cb, wrap_c);\n s->dsp.get_pixels(s->block[5], ptr_cr, wrap_c);\n if (!s->chroma_y_shift) {\n s->dsp.get_pixels(s->block[6],\n ptr_cb + (dct_offset >> 1), wrap_c);\n s->dsp.get_pixels(s->block[7],\n ptr_cr + (dct_offset >> 1), wrap_c);\n }\n }\n } else {\n op_pixels_func (*op_pix)[4];\n qpel_mc_func (*op_qpix)[16];\n uint8_t *dest_y, *dest_cb, *dest_cr;\n dest_y = s->dest[0];\n dest_cb = s->dest[1];\n dest_cr = s->dest[2];\n if ((!s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) {\n op_pix = s->dsp.put_pixels_tab;\n op_qpix = s->dsp.put_qpel_pixels_tab;\n } else {\n op_pix = s->dsp.put_no_rnd_pixels_tab;\n op_qpix = s->dsp.put_no_rnd_qpel_pixels_tab;\n }\n if (s->mv_dir & MV_DIR_FORWARD) {\n MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data,\n op_pix, op_qpix);\n op_pix = s->dsp.avg_pixels_tab;\n op_qpix = s->dsp.avg_qpel_pixels_tab;\n }\n if (s->mv_dir & MV_DIR_BACKWARD) {\n MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data,\n op_pix, op_qpix);\n }\n if (s->flags & CODEC_FLAG_INTERLACED_DCT) {\n int progressive_score, interlaced_score;\n s->interlaced_dct = 0;\n progressive_score = s->dsp.ildct_cmp[0](s, dest_y,\n ptr_y, wrap_y,\n 8) +\n s->dsp.ildct_cmp[0](s, dest_y + wrap_y * 8,\n ptr_y + wrap_y * 8, wrap_y,\n 8) - 400;\n if (s->avctx->ildct_cmp == FF_CMP_VSSE)\n progressive_score -= 400;\n if (progressive_score > 0) {\n interlaced_score = s->dsp.ildct_cmp[0](s, dest_y,\n ptr_y,\n wrap_y * 2, 8) +\n s->dsp.ildct_cmp[0](s, dest_y + wrap_y,\n ptr_y + wrap_y,\n wrap_y * 2, 8);\n if (progressive_score > interlaced_score) {\n s->interlaced_dct = 1;\n dct_offset = wrap_y;\n wrap_y <<= 1;\n if (s->chroma_format == CHROMA_422)\n wrap_c <<= 1;\n }\n }\n }\n s->dsp.diff_pixels(s->block[0], ptr_y, dest_y, wrap_y);\n s->dsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);\n s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset,\n dest_y + dct_offset, wrap_y);\n s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8,\n dest_y + dct_offset + 8, wrap_y);\n if (s->flags & CODEC_FLAG_GRAY) {\n skip_dct[4] = 1;\n skip_dct[5] = 1;\n } else {\n s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);\n s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);\n if (!s->chroma_y_shift) {\n s->dsp.diff_pixels(s->block[6], ptr_cb + (dct_offset >> 1),\n dest_cb + (dct_offset >> 1), wrap_c);\n s->dsp.diff_pixels(s->block[7], ptr_cr + (dct_offset >> 1),\n dest_cr + (dct_offset >> 1), wrap_c);\n }\n }\n if (s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] <\n 2 * s->qscale * s->qscale) {\n if (s->dsp.sad[1](NULL, ptr_y , dest_y,\n wrap_y, 8) < 20 * s->qscale)\n skip_dct[0] = 1;\n if (s->dsp.sad[1](NULL, ptr_y + 8,\n dest_y + 8, wrap_y, 8) < 20 * s->qscale)\n skip_dct[1] = 1;\n if (s->dsp.sad[1](NULL, ptr_y + dct_offset,\n dest_y + dct_offset, wrap_y, 8) < 20 * s->qscale)\n skip_dct[2] = 1;\n if (s->dsp.sad[1](NULL, ptr_y + dct_offset + 8,\n dest_y + dct_offset + 8,\n wrap_y, 8) < 20 * s->qscale)\n skip_dct[3] = 1;\n if (s->dsp.sad[1](NULL, ptr_cb, dest_cb,\n wrap_c, 8) < 20 * s->qscale)\n skip_dct[4] = 1;\n if (s->dsp.sad[1](NULL, ptr_cr, dest_cr,\n wrap_c, 8) < 20 * s->qscale)\n skip_dct[5] = 1;\n if (!s->chroma_y_shift) {\n if (s->dsp.sad[1](NULL, ptr_cb + (dct_offset >> 1),\n dest_cb + (dct_offset >> 1),\n wrap_c, 8) < 20 * s->qscale)\n skip_dct[6] = 1;\n if (s->dsp.sad[1](NULL, ptr_cr + (dct_offset >> 1),\n dest_cr + (dct_offset >> 1),\n wrap_c, 8) < 20 * s->qscale)\n skip_dct[7] = 1;\n }\n }\n }\n if (s->avctx->quantizer_noise_shaping) {\n if (!skip_dct[0])\n get_visual_weight(weight[0], ptr_y , wrap_y);\n if (!skip_dct[1])\n get_visual_weight(weight[1], ptr_y + 8, wrap_y);\n if (!skip_dct[2])\n get_visual_weight(weight[2], ptr_y + dct_offset , wrap_y);\n if (!skip_dct[3])\n get_visual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);\n if (!skip_dct[4])\n get_visual_weight(weight[4], ptr_cb , wrap_c);\n if (!skip_dct[5])\n get_visual_weight(weight[5], ptr_cr , wrap_c);\n if (!s->chroma_y_shift) {\n if (!skip_dct[6])\n get_visual_weight(weight[6], ptr_cb + (dct_offset >> 1),\n wrap_c);\n if (!skip_dct[7])\n get_visual_weight(weight[7], ptr_cr + (dct_offset >> 1),\n wrap_c);\n }\n memcpy(orig[0], s->block[0], sizeof(DCTELEM) * 64 * mb_block_count);\n }\n assert(s->out_format != FMT_MJPEG || s->qscale == 8);\n {\n for (i = 0; i < mb_block_count; i++) {\n if (!skip_dct[i]) {\n int overflow;\n s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);\n if (overflow)\n clip_coeffs(s, s->block[i], s->block_last_index[i]);\n } else\n s->block_last_index[i] = -1;\n }\n if (s->avctx->quantizer_noise_shaping) {\n for (i = 0; i < mb_block_count; i++) {\n if (!skip_dct[i]) {\n s->block_last_index[i] =\n dct_quantize_refine(s, s->block[i], weight[i],\n orig[i], i, s->qscale);\n }\n }\n }\n if (s->luma_elim_threshold && !s->mb_intra)\n for (i = 0; i < 4; i++)\n dct_single_coeff_elimination(s, i, s->luma_elim_threshold);\n if (s->chroma_elim_threshold && !s->mb_intra)\n for (i = 4; i < mb_block_count; i++)\n dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);\n if (s->flags & CODEC_FLAG_CBP_RD) {\n for (i = 0; i < mb_block_count; i++) {\n if (s->block_last_index[i] == -1)\n s->coded_score[i] = INT_MAX / 256;\n }\n }\n }\n if ((s->flags & CODEC_FLAG_GRAY) && s->mb_intra) {\n s->block_last_index[4] =\n s->block_last_index[5] = 0;\n s->block[4][0] =\n s->block[5][0] = (1024 + s->c_dc_scale / 2) / s->c_dc_scale;\n }\n if (s->alternate_scan && s->dct_quantize != dct_quantize_c) {\n for (i = 0; i < mb_block_count; i++) {\n int j;\n if (s->block_last_index[i] > 0) {\n for (j = 63; j > 0; j--) {\n if (s->block[i][s->intra_scantable.permutated[j]])\n break;\n }\n s->block_last_index[i] = j;\n }\n }\n }\n switch(s->codec_id){\n case CODEC_ID_MPEG1VIDEO:\n case CODEC_ID_MPEG2VIDEO:\n if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)\n mpeg1_encode_mb(s, s->block, motion_x, motion_y);\n break;\n case CODEC_ID_MPEG4:\n if (CONFIG_MPEG4_ENCODER)\n mpeg4_encode_mb(s, s->block, motion_x, motion_y);\n break;\n case CODEC_ID_MSMPEG4V2:\n case CODEC_ID_MSMPEG4V3:\n case CODEC_ID_WMV1:\n if (CONFIG_MSMPEG4_ENCODER)\n msmpeg4_encode_mb(s, s->block, motion_x, motion_y);\n break;\n case CODEC_ID_WMV2:\n if (CONFIG_WMV2_ENCODER)\n ff_wmv2_encode_mb(s, s->block, motion_x, motion_y);\n break;\n case CODEC_ID_H261:\n if (CONFIG_H261_ENCODER)\n ff_h261_encode_mb(s, s->block, motion_x, motion_y);\n break;\n case CODEC_ID_H263:\n case CODEC_ID_H263P:\n case CODEC_ID_FLV1:\n case CODEC_ID_RV10:\n case CODEC_ID_RV20:\n if (CONFIG_H263_ENCODER)\n h263_encode_mb(s, s->block, motion_x, motion_y);\n break;\n case CODEC_ID_MJPEG:\n if (CONFIG_MJPEG_ENCODER)\n ff_mjpeg_encode_mb(s, s->block);\n break;\n default:\n assert(0);\n }\n}']
2,304
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); }
['static int test_div_recip()\n{\n BIGNUM *a, *b, *c, *d, *e;\n BN_RECP_CTX *recp;\n int i;\n recp = BN_RECP_CTX_new();\n a = BN_new();\n b = BN_new();\n c = BN_new();\n d = BN_new();\n e = BN_new();\n for (i = 0; i < NUM0 + NUM1; i++) {\n if (i < NUM1) {\n BN_bntest_rand(a, 400, 0, 0);\n BN_copy(b, a);\n BN_lshift(a, a, i);\n BN_add_word(a, i);\n } else\n BN_bntest_rand(b, 50 + 3 * (i - NUM1), 0, 0);\n a->neg = rand_neg();\n b->neg = rand_neg();\n BN_RECP_CTX_set(recp, b, ctx);\n BN_div_recp(d, c, a, recp, ctx);\n BN_mul(e, d, b, ctx);\n BN_add(d, e, c);\n BN_sub(d, d, a);\n if (!BN_is_zero(d)) {\n printf("Reciprocal division test failed!\\n");\n printf("a=");\n BN_print_fp(stdout, a);\n printf("\\nb=");\n BN_print_fp(stdout, b);\n printf("\\n");\n return 0;\n }\n }\n BN_free(a);\n BN_free(b);\n BN_free(c);\n BN_free(d);\n BN_free(e);\n BN_RECP_CTX_free(recp);\n return 1;\n}', 'int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l;\n bn_check_top(r);\n bn_check_top(a);\n if (n < 0) {\n BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n 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}']
2,305
0
https://github.com/openssl/openssl/blob/3208ff58ca59d143b49dd2f1c05fbc33cf35e64f/crypto/lhash/lhash.c/#L240
void *lh_delete(LHASH *lh, const void *data) { unsigned long hash; LHASH_NODE *nn,**rn; const void *ret; lh->error=0; rn=getrn(lh,data,&hash); if (*rn == NULL) { lh->num_no_delete++; return(NULL); } else { nn= *rn; *rn=nn->next; ret=nn->data; OPENSSL_free(nn); lh->num_delete++; } lh->num_items--; if ((lh->num_nodes > MIN_NODES) && (lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes))) contract(lh); return((void *)ret); }
['int ssl_get_prev_session(SSL *s, unsigned char *session_id, int len)\n\t{\n\tSSL_SESSION *ret=NULL,data;\n\tint fatal = 0;\n\tdata.ssl_version=s->version;\n\tdata.session_id_length=len;\n\tif (len > SSL_MAX_SSL_SESSION_ID_LENGTH)\n\t\tgoto err;\n\tmemcpy(data.session_id,session_id,len);\n\tif (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_LOOKUP))\n\t\t{\n\t\tCRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);\n\t\tret=(SSL_SESSION *)lh_retrieve(s->ctx->sessions,&data);\n\t\tif (ret != NULL)\n\t\t CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);\n\t\tCRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);\n\t\t}\n\tif (ret == NULL)\n\t\t{\n\t\tint copy=1;\n\t\ts->ctx->stats.sess_miss++;\n\t\tret=NULL;\n\t\tif (s->ctx->get_session_cb != NULL\n\t\t && (ret=s->ctx->get_session_cb(s,session_id,len,&copy))\n\t\t != NULL)\n\t\t\t{\n\t\t\ts->ctx->stats.sess_cb_hit++;\n\t\t\tif (copy)\n\t\t\t\tCRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);\n\t\t\tSSL_CTX_add_session(s->ctx,ret);\n\t\t\t}\n\t\tif (ret == NULL)\n\t\t\tgoto err;\n\t\t}\n\tif((s->verify_mode&SSL_VERIFY_PEER)\n\t && (!s->sid_ctx_length || ret->sid_ctx_length != s->sid_ctx_length\n\t || memcmp(ret->sid_ctx,s->sid_ctx,ret->sid_ctx_length)))\n\t {\n\t\tif (s->sid_ctx_length == 0)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL_GET_PREV_SESSION,SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);\n\t\t\tfatal = 1;\n\t\t\tgoto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n#if 0\n\t\t\tSSLerr(SSL_F_SSL_GET_PREV_SESSION,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);\n#endif\n\t\t\tgoto err;\n\t\t\t}\n\t\t}\n\tif (ret->cipher == NULL)\n\t\t{\n\t\tunsigned char buf[5],*p;\n\t\tunsigned long l;\n\t\tp=buf;\n\t\tl=ret->cipher_id;\n\t\tl2n(l,p);\n\t\tif ((ret->ssl_version>>8) == SSL3_VERSION_MAJOR)\n\t\t\tret->cipher=ssl_get_cipher_by_char(s,&(buf[2]));\n\t\telse\n\t\t\tret->cipher=ssl_get_cipher_by_char(s,&(buf[1]));\n\t\tif (ret->cipher == NULL)\n\t\t\tgoto err;\n\t\t}\n#if 0\n\tCRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);\n#endif\n\tif ((long)(ret->time+ret->timeout) < (long)time(NULL))\n\t\t{\n\t\ts->ctx->stats.sess_timeout++;\n\t\tSSL_CTX_remove_session(s->ctx,ret);\n\t\tgoto err;\n\t\t}\n\ts->ctx->stats.sess_hit++;\n\tif (s->session != NULL)\n\t\tSSL_SESSION_free(s->session);\n\ts->session=ret;\n\ts->verify_result = s->session->verify_result;\n\treturn(1);\n err:\n\tif (ret != NULL)\n\t\tSSL_SESSION_free(ret);\n\tif (fatal)\n\t\treturn -1;\n\telse\n\t\treturn 0;\n\t}', 'int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)\n{\n\treturn remove_session_lock(ctx, c, 1);\n}', 'static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)\n\t{\n\tSSL_SESSION *r;\n\tint ret=0;\n\tif ((c != NULL) && (c->session_id_length != 0))\n\t\t{\n\t\tif(lck) CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);\n\t\tif ((r = (SSL_SESSION *)lh_retrieve(ctx->sessions,c)) == c)\n\t\t\t{\n\t\t\tret=1;\n\t\t\tr=(SSL_SESSION *)lh_delete(ctx->sessions,c);\n\t\t\tSSL_SESSION_list_remove(ctx,c);\n\t\t\t}\n\t\tif(lck) CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n\t\tif (ret)\n\t\t\t{\n\t\t\tr->not_resumable=1;\n\t\t\tif (ctx->remove_session_cb != NULL)\n\t\t\t\tctx->remove_session_cb(ctx,r);\n\t\t\tSSL_SESSION_free(r);\n\t\t\t}\n\t\t}\n\telse\n\t\tret=0;\n\treturn(ret);\n\t}', 'void *lh_delete(LHASH *lh, const void *data)\n\t{\n\tunsigned long hash;\n\tLHASH_NODE *nn,**rn;\n\tconst void *ret;\n\tlh->error=0;\n\trn=getrn(lh,data,&hash);\n\tif (*rn == NULL)\n\t\t{\n\t\tlh->num_no_delete++;\n\t\treturn(NULL);\n\t\t}\n\telse\n\t\t{\n\t\tnn= *rn;\n\t\t*rn=nn->next;\n\t\tret=nn->data;\n\t\tOPENSSL_free(nn);\n\t\tlh->num_delete++;\n\t\t}\n\tlh->num_items--;\n\tif ((lh->num_nodes > MIN_NODES) &&\n\t\t(lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes)))\n\t\tcontract(lh);\n\treturn((void *)ret);\n\t}']
2,306
0
https://github.com/openssl/openssl/blob/1fac96e4d6484a517f2ebe99b72016726391723c/crypto/bn/bn_lib.c/#L447
BIGNUM *bn_expand2(BIGNUM *b, int words) { BN_ULONG *A,*a; const BN_ULONG *B; int i; bn_check_top(b); if (words > b->max) { bn_check_top(b); if (BN_get_flags(b,BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND2,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return(NULL); } a=A=(BN_ULONG *)Malloc(sizeof(BN_ULONG)*(words+1)); if (A == NULL) { BNerr(BN_F_BN_EXPAND2,ERR_R_MALLOC_FAILURE); return(NULL); } #if 1 B=b->d; if (B != NULL) { #if 0 for (i=b->top&(~7); i>0; i-=8) { A[0]=B[0]; A[1]=B[1]; A[2]=B[2]; A[3]=B[3]; A[4]=B[4]; A[5]=B[5]; A[6]=B[6]; A[7]=B[7]; A+=8; B+=8; } switch (b->top&7) { case 7: A[6]=B[6]; case 6: A[5]=B[5]; case 5: A[4]=B[4]; case 4: A[3]=B[3]; case 3: A[2]=B[2]; case 2: A[1]=B[1]; case 1: A[0]=B[0]; case 0: ; } #else for (i=b->top>>2; i>0; i--,A+=4,B+=4) { BN_ULONG a0,a1,a2,a3; a0=B[0]; a1=B[1]; a2=B[2]; a3=B[3]; A[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3; } switch (b->top&3) { case 3: A[2]=B[2]; case 2: A[1]=B[1]; case 1: A[0]=B[0]; case 0: ; } #endif Free(b->d); } b->d=a; b->max=words; A= &(b->d[b->top]); for (i=(b->max - b->top)>>3; i>0; i--,A+=8) { A[0]=0; A[1]=0; A[2]=0; A[3]=0; A[4]=0; A[5]=0; A[6]=0; A[7]=0; } for (i=(b->max - b->top)&7; i>0; i--,A++) A[0]=0; #else memset(A,0,sizeof(BN_ULONG)*(words+1)); memcpy(A,b->d,sizeof(b->d[0])*b->top); b->d=a; b->max=words; #endif } return(b); }
['static int ssl3_get_key_exchange(SSL *s)\n\t{\n#ifndef NO_RSA\n\tunsigned char *q,md_buf[EVP_MAX_MD_SIZE*2];\n#endif\n\tEVP_MD_CTX md_ctx;\n\tunsigned char *param,*p;\n\tint al,i,j,param_len,ok;\n\tlong n,alg;\n\tEVP_PKEY *pkey=NULL;\n#ifndef NO_RSA\n\tRSA *rsa=NULL;\n#endif\n#ifndef NO_DH\n\tDH *dh=NULL;\n#endif\n\tn=ssl3_get_message(s,\n\t\tSSL3_ST_CR_KEY_EXCH_A,\n\t\tSSL3_ST_CR_KEY_EXCH_B,\n\t\t-1,\n\t\t1024*8,\n\t\t&ok);\n\tif (!ok) return((int)n);\n\tif (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)\n\t\t{\n\t\ts->s3->tmp.reuse_message=1;\n\t\treturn(1);\n\t\t}\n\tparam=p=(unsigned char *)s->init_buf->data;\n\tif (s->session->sess_cert != NULL)\n\t\t{\n#ifndef NO_RSA\n\t\tif (s->session->sess_cert->peer_rsa_tmp != NULL)\n\t\t\t{\n\t\t\tRSA_free(s->session->sess_cert->peer_rsa_tmp);\n\t\t\ts->session->sess_cert->peer_rsa_tmp=NULL;\n\t\t\t}\n#endif\n#ifndef NO_DH\n\t\tif (s->session->sess_cert->peer_dh_tmp)\n\t\t\t{\n\t\t\tDH_free(s->session->sess_cert->peer_dh_tmp);\n\t\t\ts->session->sess_cert->peer_dh_tmp=NULL;\n\t\t\t}\n#endif\n\t\t}\n\telse\n\t\t{\n\t\ts->session->sess_cert=ssl_sess_cert_new();\n\t\t}\n\tparam_len=0;\n\talg=s->s3->tmp.new_cipher->algorithms;\n#ifndef NO_RSA\n\tif (alg & SSL_kRSA)\n\t\t{\n\t\tif ((rsa=RSA_new()) == NULL)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);\n\t\t\tgoto err;\n\t\t\t}\n\t\tn2s(p,i);\n\t\tparam_len=i+2;\n\t\tif (param_len > n)\n\t\t\t{\n\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_MODULUS_LENGTH);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tif (!(rsa->n=BN_bin2bn(p,i,rsa->n)))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);\n\t\t\tgoto err;\n\t\t\t}\n\t\tp+=i;\n\t\tn2s(p,i);\n\t\tparam_len+=i+2;\n\t\tif (param_len > n)\n\t\t\t{\n\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_E_LENGTH);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tif (!(rsa->e=BN_bin2bn(p,i,rsa->e)))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);\n\t\t\tgoto err;\n\t\t\t}\n\t\tp+=i;\n\t\tn-=param_len;\n\t\tif (alg & SSL_aRSA)\n\t\t\tpkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);\n\t\telse\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);\n\t\t\tgoto err;\n\t\t\t}\n\t\ts->session->sess_cert->peer_rsa_tmp=rsa;\n\t\t}\n\telse\n#endif\n#ifndef NO_DH\n\t\tif (alg & SSL_kEDH)\n\t\t{\n\t\tif ((dh=DH_new()) == NULL)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_DH_LIB);\n\t\t\tgoto err;\n\t\t\t}\n\t\tn2s(p,i);\n\t\tparam_len=i+2;\n\t\tif (param_len > n)\n\t\t\t{\n\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_P_LENGTH);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tif (!(dh->p=BN_bin2bn(p,i,NULL)))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);\n\t\t\tgoto err;\n\t\t\t}\n\t\tp+=i;\n\t\tn2s(p,i);\n\t\tparam_len+=i+2;\n\t\tif (param_len > n)\n\t\t\t{\n\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_G_LENGTH);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tif (!(dh->g=BN_bin2bn(p,i,NULL)))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);\n\t\t\tgoto err;\n\t\t\t}\n\t\tp+=i;\n\t\tn2s(p,i);\n\t\tparam_len+=i+2;\n\t\tif (param_len > n)\n\t\t\t{\n\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_PUB_KEY_LENGTH);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tif (!(dh->pub_key=BN_bin2bn(p,i,NULL)))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);\n\t\t\tgoto err;\n\t\t\t}\n\t\tp+=i;\n\t\tn-=param_len;\n#ifndef NO_RSA\n\t\tif (alg & SSL_aRSA)\n\t\t\tpkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);\n\t\telse\n#endif\n#ifndef NO_DSA\n\t\tif (alg & SSL_aDSS)\n\t\t\tpkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509);\n#endif\n\t\ts->session->sess_cert->peer_dh_tmp=dh;\n\t\tdh=NULL;\n\t\t}\n\telse if ((alg & SSL_kDHr) || (alg & SSL_kDHd))\n\t\t{\n\t\tal=SSL_AD_ILLEGAL_PARAMETER;\n\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);\n\t\tgoto f_err;\n\t\t}\n#endif\n\tif (alg & SSL_aFZA)\n\t\t{\n\t\tal=SSL_AD_HANDSHAKE_FAILURE;\n\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);\n\t\tgoto f_err;\n\t\t}\n\tif (pkey != NULL)\n\t\t{\n\t\tn2s(p,i);\n\t\tn-=2;\n\t\tj=EVP_PKEY_size(pkey);\n\t\tif ((i != n) || (n > j) || (n <= 0))\n\t\t\t{\n\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE_LENGTH);\n\t\t\tgoto f_err;\n\t\t\t}\n#ifndef NO_RSA\n\t\tif (pkey->type == EVP_PKEY_RSA)\n\t\t\t{\n\t\t\tint num;\n\t\t\tj=0;\n\t\t\tq=md_buf;\n\t\t\tfor (num=2; num > 0; num--)\n\t\t\t\t{\n\t\t\t\tEVP_DigestInit(&md_ctx,(num == 2)\n\t\t\t\t\t?s->ctx->md5:s->ctx->sha1);\n\t\t\t\tEVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);\n\t\t\t\tEVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);\n\t\t\t\tEVP_DigestUpdate(&md_ctx,param,param_len);\n\t\t\t\tEVP_DigestFinal(&md_ctx,q,(unsigned int *)&i);\n\t\t\t\tq+=i;\n\t\t\t\tj+=i;\n\t\t\t\t}\n\t\t\ti=RSA_public_decrypt((int)n,p,p,pkey->pkey.rsa,\n\t\t\t\tRSA_PKCS1_PADDING);\n\t\t\tif (i <= 0)\n\t\t\t\t{\n\t\t\t\tal=SSL_AD_DECRYPT_ERROR;\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\tif ((j != i) || (memcmp(p,md_buf,i) != 0))\n\t\t\t\t{\n\t\t\t\tal=SSL_AD_DECRYPT_ERROR;\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);\n\t\t\t\tgoto f_err;\n\t\t\t\t}\n\t\t\t}\n\t\telse\n#endif\n#ifndef NO_DSA\n\t\t\tif (pkey->type == EVP_PKEY_DSA)\n\t\t\t{\n\t\t\tEVP_VerifyInit(&md_ctx,EVP_dss1());\n\t\t\tEVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);\n\t\t\tEVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);\n\t\t\tEVP_VerifyUpdate(&md_ctx,param,param_len);\n\t\t\tif (!EVP_VerifyFinal(&md_ctx,p,(int)n,pkey))\n\t\t\t\t{\n\t\t\t\tal=SSL_AD_DECRYPT_ERROR;\n\t\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);\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_GET_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);\n\t\t\tgoto err;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tif (!(alg & SSL_aNULL))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (n != 0)\n\t\t\t{\n\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\tSSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_EXTRA_DATA_IN_MESSAGE);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\t}\n\tEVP_PKEY_free(pkey);\n\treturn(1);\nf_err:\n\tssl3_send_alert(s,SSL3_AL_FATAL,al);\nerr:\n\tEVP_PKEY_free(pkey);\n\treturn(-1);\n\t}', 'BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)\n\t{\n\tunsigned int i,m;\n\tunsigned int n;\n\tBN_ULONG l;\n\tif (ret == NULL) ret=BN_new();\n\tif (ret == NULL) return(NULL);\n\tl=0;\n\tn=len;\n\tif (n == 0)\n\t\t{\n\t\tret->top=0;\n\t\treturn(ret);\n\t\t}\n\tif (bn_expand(ret,(int)(n+2)*8) == NULL)\n\t\treturn(NULL);\n\ti=((n-1)/BN_BYTES)+1;\n\tm=((n-1)%(BN_BYTES));\n\tret->top=i;\n\twhile (n-- > 0)\n\t\t{\n\t\tl=(l<<8L)| *(s++);\n\t\tif (m-- == 0)\n\t\t\t{\n\t\t\tret->d[--i]=l;\n\t\t\tl=0;\n\t\t\tm=BN_BYTES-1;\n\t\t\t}\n\t\t}\n\tbn_fix_top(ret);\n\treturn(ret);\n\t}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n\t{\n\tBN_ULONG *A,*a;\n\tconst BN_ULONG *B;\n\tint i;\n\tbn_check_top(b);\n\tif (words > b->max)\n\t\t{\n\t\tbn_check_top(b);\n\t\tif (BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_EXPAND2,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n\t\t\treturn(NULL);\n\t\t\t}\n\t\ta=A=(BN_ULONG *)Malloc(sizeof(BN_ULONG)*(words+1));\n\t\tif (A == NULL)\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_EXPAND2,ERR_R_MALLOC_FAILURE);\n\t\t\treturn(NULL);\n\t\t\t}\n#if 1\n\t\tB=b->d;\n\t\tif (B != NULL)\n\t\t\t{\n#if 0\n\t\t\tfor (i=b->top&(~7); i>0; i-=8)\n\t\t\t\t{\n\t\t\t\tA[0]=B[0]; A[1]=B[1]; A[2]=B[2]; A[3]=B[3];\n\t\t\t\tA[4]=B[4]; A[5]=B[5]; A[6]=B[6]; A[7]=B[7];\n\t\t\t\tA+=8;\n\t\t\t\tB+=8;\n\t\t\t\t}\n\t\t\tswitch (b->top&7)\n\t\t\t\t{\n\t\t\tcase 7:\n\t\t\t\tA[6]=B[6];\n\t\t\tcase 6:\n\t\t\t\tA[5]=B[5];\n\t\t\tcase 5:\n\t\t\t\tA[4]=B[4];\n\t\t\tcase 4:\n\t\t\t\tA[3]=B[3];\n\t\t\tcase 3:\n\t\t\t\tA[2]=B[2];\n\t\t\tcase 2:\n\t\t\t\tA[1]=B[1];\n\t\t\tcase 1:\n\t\t\t\tA[0]=B[0];\n\t\t\tcase 0:\n\t\t\t\t;\n\t\t\t\t}\n#else\n\t\t\tfor (i=b->top>>2; i>0; i--,A+=4,B+=4)\n\t\t\t\t{\n\t\t\t\tBN_ULONG a0,a1,a2,a3;\n\t\t\t\ta0=B[0]; a1=B[1]; a2=B[2]; a3=B[3];\n\t\t\t\tA[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3;\n\t\t\t\t}\n\t\t\tswitch (b->top&3)\n\t\t\t\t{\n\t\t\t\tcase 3:\tA[2]=B[2];\n\t\t\t\tcase 2:\tA[1]=B[1];\n\t\t\t\tcase 1:\tA[0]=B[0];\n\t\t\t\tcase 0:\t;\n\t\t\t\t}\n#endif\n\t\t\tFree(b->d);\n\t\t\t}\n\t\tb->d=a;\n\t\tb->max=words;\n\t\tA= &(b->d[b->top]);\n\t\tfor (i=(b->max - b->top)>>3; i>0; i--,A+=8)\n\t\t\t{\n\t\t\tA[0]=0; A[1]=0; A[2]=0; A[3]=0;\n\t\t\tA[4]=0; A[5]=0; A[6]=0; A[7]=0;\n\t\t\t}\n\t\tfor (i=(b->max - b->top)&7; i>0; i--,A++)\n\t\t\tA[0]=0;\n#else\n\t\t\tmemset(A,0,sizeof(BN_ULONG)*(words+1));\n\t\t\tmemcpy(A,b->d,sizeof(b->d[0])*b->top);\n\t\t\tb->d=a;\n\t\t\tb->max=words;\n#endif\n\t\t}\n\treturn(b);\n\t}']
2,307
0
https://github.com/openssl/openssl/blob/e3713c365c2657236439fea00822a43aa396d112/crypto/bn/bn_lib.c/#L711
int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n) { int i; BN_ULONG aa, bb; aa = a[n - 1]; bb = b[n - 1]; if (aa != bb) return ((aa > bb) ? 1 : -1); for (i = n - 2; i >= 0; i--) { aa = a[i]; bb = b[i]; if (aa != bb) return ((aa > bb) ? 1 : -1); } return (0); }
['static int test_modexp_mont5(void)\n{\n BIGNUM *a = NULL, *p = NULL, *m = NULL, *d = NULL, *e = NULL;\n BIGNUM *b = NULL, *n = NULL, *c = NULL;\n BN_MONT_CTX *mont = NULL;\n char *bigstring;\n int st = 0;\n if (!TEST_ptr(a = BN_new())\n || !TEST_ptr(p = BN_new())\n || !TEST_ptr(m = BN_new())\n || !TEST_ptr(d = BN_new())\n || !TEST_ptr(e = BN_new())\n || !TEST_ptr(b = BN_new())\n || !TEST_ptr(n = BN_new())\n || !TEST_ptr(c = BN_new())\n || !TEST_ptr(mont = BN_MONT_CTX_new()))\n goto err;\n BN_bntest_rand(m, 1024, 0, 1);\n BN_bntest_rand(a, 1024, 0, 0);\n BN_zero(p);\n if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL)))\n goto err;\n if (!TEST_BN_eq_one(d))\n goto err;\n BN_hex2bn(&a,\n "7878787878787878787878787878787878787878787878787878787878787878"\n "7878787878787878787878787878787878787878787878787878787878787878"\n "7878787878787878787878787878787878787878787878787878787878787878"\n "7878787878787878787878787878787878787878787878787878787878787878");\n BN_hex2bn(&b,\n "095D72C08C097BA488C5E439C655A192EAFB6380073D8C2664668EDDB4060744"\n "E16E57FB4EDB9AE10A0CEFCDC28A894F689A128379DB279D48A2E20849D68593"\n "9B7803BCF46CEBF5C533FB0DD35B080593DE5472E3FE5DB951B8BFF9B4CB8F03"\n "9CC638A5EE8CDD703719F8000E6A9F63BEED5F2FCD52FF293EA05A251BB4AB81");\n BN_hex2bn(&n,\n "D78AF684E71DB0C39CFF4E64FB9DB567132CB9C50CC98009FEB820B26F2DED9B"\n "91B9B5E2B83AE0AE4EB4E0523CA726BFBE969B89FD754F674CE99118C3F2D1C5"\n "D81FDC7C54E02B60262B241D53C040E99E45826ECA37A804668E690E1AFC1CA4"\n "2C9A15D84D4954425F0B7642FC0BD9D7B24E2618D2DCC9B729D944BADACFDDAF");\n BN_MONT_CTX_set(mont, n, ctx);\n BN_mod_mul_montgomery(c, a, b, mont, ctx);\n BN_mod_mul_montgomery(d, b, a, mont, ctx);\n if (!TEST_BN_eq(c, d))\n goto err;\n bigstring = glue(bn1strings);\n BN_hex2bn(&n, bigstring);\n OPENSSL_free(bigstring);\n bigstring = glue(bn2strings);\n BN_hex2bn(&a, bigstring);\n OPENSSL_free(bigstring);\n BN_free(b);\n b = BN_dup(a);\n BN_MONT_CTX_set(mont, n, ctx);\n BN_mod_mul_montgomery(c, a, a, mont, ctx);\n BN_mod_mul_montgomery(d, a, b, mont, ctx);\n if (!TEST_BN_eq(c, d))\n goto err;\n BN_bntest_rand(p, 1024, 0, 0);\n BN_zero(a);\n if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))\n || !TEST_BN_eq_zero(d))\n goto err;\n BN_one(a);\n BN_MONT_CTX_set(mont, m, ctx);\n if (!TEST_true(BN_from_montgomery(e, a, mont, ctx))\n || !TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))\n || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))\n || !TEST_BN_eq(a, d))\n goto err;\n BN_bntest_rand(e, 1024, 0, 0);\n if (!TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))\n || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))\n || !TEST_BN_eq(a, d))\n goto err;\n st = 1;\nerr:\n BN_MONT_CTX_free(mont);\n BN_free(a);\n BN_free(p);\n BN_free(m);\n BN_free(d);\n BN_free(e);\n BN_free(b);\n BN_free(n);\n BN_free(c);\n return st;\n}', 'int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom)\n{\n return bnrand(TESTING, rnd, bits, top, bottom);\n}', 'static int bnrand(BNRAND_FLAG flag, BIGNUM *rnd, int bits, int top, int bottom)\n{\n unsigned char *buf = NULL;\n int b, ret = 0, bit, bytes, mask;\n if (bits == 0) {\n if (top != BN_RAND_TOP_ANY || bottom != BN_RAND_BOTTOM_ANY)\n goto toosmall;\n BN_zero(rnd);\n return 1;\n }\n if (bits < 0 || (bits == 1 && top > 0))\n goto toosmall;\n bytes = (bits + 7) / 8;\n bit = (bits - 1) % 8;\n mask = 0xff << (bit + 1);\n buf = OPENSSL_malloc(bytes);\n if (buf == NULL) {\n BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n b = flag == NORMAL ? RAND_bytes(buf, bytes) : RAND_priv_bytes(buf, bytes);\n if (b <= 0)\n goto err;\n if (flag == TESTING) {\n int i;\n unsigned char c;\n for (i = 0; i < bytes; i++) {\n if (RAND_bytes(&c, 1) <= 0)\n goto err;\n if (c >= 128 && i > 0)\n buf[i] = buf[i - 1];\n else if (c < 42)\n buf[i] = 0;\n else if (c < 84)\n buf[i] = 255;\n }\n }\n if (top >= 0) {\n if (top) {\n if (bit == 0) {\n buf[0] = 1;\n buf[1] |= 0x80;\n } else {\n buf[0] |= (3 << (bit - 1));\n }\n } else {\n buf[0] |= (1 << bit);\n }\n }\n buf[0] &= ~mask;\n if (bottom)\n buf[bytes - 1] |= 1;\n if (!BN_bin2bn(buf, bytes, rnd))\n goto err;\n ret = 1;\n err:\n OPENSSL_clear_free(buf, bytes);\n bn_check_top(rnd);\n return (ret);\ntoosmall:\n BNerr(BN_F_BNRAND, BN_R_BITS_TOO_SMALL);\n return 0;\n}', 'int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx,\n BN_MONT_CTX *in_mont)\n{\n int i, bits, ret = 0, window, wvalue;\n int top;\n BN_MONT_CTX *mont = NULL;\n int numPowers;\n unsigned char *powerbufFree = NULL;\n int powerbufLen = 0;\n unsigned char *powerbuf = NULL;\n BIGNUM tmp, am;\n#if defined(SPARC_T4_MONT)\n unsigned int t4 = 0;\n#endif\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT_CONSTTIME, BN_R_CALLED_WITH_EVEN_MODULUS);\n return (0);\n }\n top = m->top;\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_is_one(m)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n#ifdef RSAZ_ENABLED\n if ((16 == a->top) && (16 == p->top) && (BN_num_bits(m) == 1024)\n && rsaz_avx2_eligible()) {\n if (NULL == bn_wexpand(rr, 16))\n goto err;\n RSAZ_1024_mod_exp_avx2(rr->d, a->d, p->d, m->d, mont->RR.d,\n mont->n0[0]);\n rr->top = 16;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n } else if ((8 == a->top) && (8 == p->top) && (BN_num_bits(m) == 512)) {\n if (NULL == bn_wexpand(rr, 8))\n goto err;\n RSAZ_512_mod_exp(rr->d, a->d, p->d, m->d, mont->n0[0], mont->RR.d);\n rr->top = 8;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n }\n#endif\n window = BN_window_bits_for_ctime_exponent_size(bits);\n#if defined(SPARC_T4_MONT)\n if (window >= 5 && (top & 15) == 0 && top <= 64 &&\n (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) ==\n (CFR_MONTMUL | CFR_MONTSQR) && (t4 = OPENSSL_sparcv9cap_P[0]))\n window = 5;\n else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window >= 5) {\n window = 5;\n powerbufLen += top * sizeof(mont->N.d[0]);\n }\n#endif\n (void)0;\n numPowers = 1 << window;\n powerbufLen += sizeof(m->d[0]) * (top * numPowers +\n ((2 * top) >\n numPowers ? (2 * top) : numPowers));\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree =\n alloca(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH);\n else\n#endif\n if ((powerbufFree =\n OPENSSL_malloc(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH))\n == NULL)\n goto err;\n powerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);\n memset(powerbuf, 0, powerbufLen);\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree = NULL;\n#endif\n tmp.d = (BN_ULONG *)(powerbuf + sizeof(m->d[0]) * top * numPowers);\n am.d = tmp.d + top;\n tmp.top = am.top = 0;\n tmp.dmax = am.dmax = top;\n tmp.neg = am.neg = 0;\n tmp.flags = am.flags = BN_FLG_STATIC_DATA;\n#if 1\n if (m->d[top - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n tmp.d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < top; i++)\n tmp.d[i] = (~m->d[i]) & BN_MASK2;\n tmp.top = top;\n } else\n#endif\n if (!BN_to_montgomery(&tmp, BN_value_one(), mont, ctx))\n goto err;\n if (a->neg || BN_ucmp(a, m) >= 0) {\n if (!BN_mod(&am, a, m, ctx))\n goto err;\n if (!BN_to_montgomery(&am, &am, mont, ctx))\n goto err;\n } else if (!BN_to_montgomery(&am, a, mont, ctx))\n goto err;\n#if defined(SPARC_T4_MONT)\n if (t4) {\n typedef int (*bn_pwr5_mont_f) (BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_8(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_16(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_24(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_32(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n static const bn_pwr5_mont_f pwr5_funcs[4] = {\n bn_pwr5_mont_t4_8, bn_pwr5_mont_t4_16,\n bn_pwr5_mont_t4_24, bn_pwr5_mont_t4_32\n };\n bn_pwr5_mont_f pwr5_worker = pwr5_funcs[top / 16 - 1];\n typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap, const void *bp,\n const BN_ULONG *np, const BN_ULONG *n0);\n int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n static const bn_mul_mont_f mul_funcs[4] = {\n bn_mul_mont_t4_8, bn_mul_mont_t4_16,\n bn_mul_mont_t4_24, bn_mul_mont_t4_32\n };\n bn_mul_mont_f mul_worker = mul_funcs[top / 16 - 1];\n void bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_gather5_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_flip_n_scatter5_t4(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5_t4(BN_ULONG *out, size_t num,\n void *table, size_t power);\n void bn_flip_t4(BN_ULONG *dst, BN_ULONG *src, size_t num);\n BN_ULONG *np = mont->N.d, *n0 = mont->n0;\n int stride = 5 * (6 - (top / 16 - 1));\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 0);\n bn_flip_n_scatter5_t4(am.d, top, powerbuf, 1);\n if (!(*mul_worker) (tmp.d, am.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, am.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, am.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 2);\n for (i = 3; i < 32; i++) {\n if (!(*mul_worker) (tmp.d, tmp.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, tmp.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, tmp.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, i);\n }\n np = alloca(top * sizeof(BN_ULONG));\n top /= 2;\n bn_flip_t4(np, mont->N.d, top);\n bits--;\n for (wvalue = 0, i = bits % 5; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_gather5_t4(tmp.d, top, powerbuf, wvalue);\n while (bits >= 0) {\n if (bits < stride)\n stride = bits + 1;\n bits -= stride;\n wvalue = bn_get_bits(p, bits + 1);\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n bits += stride - 5;\n wvalue >>= stride - 5;\n wvalue &= 31;\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5_t4(tmp.d, tmp.d, powerbuf, np, n0, top,\n wvalue);\n }\n bn_flip_t4(tmp.d, tmp.d, top);\n top *= 2;\n tmp.top = top;\n bn_correct_top(&tmp);\n OPENSSL_cleanse(np, top * sizeof(BN_ULONG));\n } else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window == 5 && top > 1) {\n void bn_mul_mont_gather5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_scatter5(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5(BN_ULONG *out, size_t num, void *table, size_t power);\n void bn_power5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n int bn_get_bits5(const BN_ULONG *ap, int off);\n int bn_from_montgomery(BN_ULONG *rp, const BN_ULONG *ap,\n const BN_ULONG *not_used, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n BN_ULONG *n0 = mont->n0, *np;\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n for (np = am.d + top, i = 0; i < top; i++)\n np[i] = mont->N.d[i];\n bn_scatter5(tmp.d, top, powerbuf, 0);\n bn_scatter5(am.d, am.top, powerbuf, 1);\n bn_mul_mont(tmp.d, am.d, am.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2);\n# if 0\n for (i = 3; i < 32; i++) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# else\n for (i = 4; i < 32; i *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n for (i = 3; i < 8; i += 2) {\n int j;\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n for (j = 2 * i; j < 32; j *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, j);\n }\n }\n for (; i < 16; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2 * i);\n }\n for (; i < 32; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# endif\n bits--;\n for (wvalue = 0, i = bits % 5; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_gather5(tmp.d, top, powerbuf, wvalue);\n if (top & 7)\n while (bits >= 0) {\n for (wvalue = 0, i = 0; i < 5; i++, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5(tmp.d, tmp.d, powerbuf, np, n0, top,\n wvalue);\n } else {\n while (bits >= 0) {\n wvalue = bn_get_bits5(p->d, bits - 4);\n bits -= 5;\n bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top, wvalue);\n }\n }\n ret = bn_from_montgomery(tmp.d, tmp.d, NULL, np, n0, top);\n tmp.top = top;\n bn_correct_top(&tmp);\n if (ret) {\n if (!BN_copy(rr, &tmp))\n ret = 0;\n goto err;\n }\n } else\n#endif\n {\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 0, window))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, window))\n goto err;\n if (window > 1) {\n if (!BN_mod_mul_montgomery(&tmp, &am, &am, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 2,\n window))\n goto err;\n for (i = 3; i < numPowers; i++) {\n if (!BN_mod_mul_montgomery(&tmp, &am, &tmp, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, i,\n window))\n goto err;\n }\n }\n bits--;\n for (wvalue = 0, i = bits % window; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&tmp, top, powerbuf, wvalue,\n window))\n goto err;\n while (bits >= 0) {\n wvalue = 0;\n for (i = 0; i < window; i++, bits--) {\n if (!BN_mod_mul_montgomery(&tmp, &tmp, &tmp, mont, ctx))\n goto err;\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n }\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&am, top, powerbuf, wvalue,\n window))\n goto err;\n if (!BN_mod_mul_montgomery(&tmp, &tmp, &am, mont, ctx))\n goto err;\n }\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n am.d[0] = 1;\n for (i = 1; i < top; i++)\n am.d[i] = 0;\n if (!BN_mod_mul_montgomery(rr, &tmp, &am, mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, &tmp, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n if (powerbuf != NULL) {\n OPENSSL_cleanse(powerbuf, powerbufLen);\n OPENSSL_free(powerbufFree);\n }\n BN_CTX_end(ctx);\n return (ret);\n}', 'int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,\n BN_CTX *ctx)\n{\n return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);\n}', 'int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n BN_MONT_CTX *mont, BN_CTX *ctx)\n{\n BIGNUM *tmp;\n int ret = 0;\n#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\n int num = mont->N.top;\n if (num > 1 && a->top == num && b->top == num) {\n if (bn_wexpand(r, num) == NULL)\n return (0);\n if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {\n r->neg = a->neg ^ b->neg;\n r->top = num;\n bn_correct_top(r);\n return 1;\n }\n }\n#endif\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n if (tmp == NULL)\n goto err;\n bn_check_top(tmp);\n if (a == b) {\n if (!BN_sqr(tmp, a, ctx))\n goto err;\n } else {\n if (!BN_mul(tmp, a, b, ctx))\n goto err;\n }\n#ifdef MONT_WORD\n if (!BN_from_montgomery_word(r, tmp, mont))\n goto err;\n#else\n if (!BN_from_montgomery(r, tmp, mont, ctx))\n goto err;\n#endif\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return (ret);\n}', 'int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = 0;\n int top, al, bl;\n BIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n int i;\n#endif\n#ifdef BN_RECURSION\n BIGNUM *t = NULL;\n int j = 0, k;\n#endif\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(r);\n al = a->top;\n bl = b->top;\n if ((al == 0) || (bl == 0)) {\n BN_zero(r);\n return 1;\n }\n top = al + bl;\n BN_CTX_start(ctx);\n if ((r == a) || (r == b)) {\n if ((rr = BN_CTX_get(ctx)) == NULL)\n goto err;\n } else\n rr = r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n i = al - bl;\n#endif\n#ifdef BN_MUL_COMBA\n if (i == 0) {\n# if 0\n if (al == 4) {\n if (bn_wexpand(rr, 8) == NULL)\n goto err;\n rr->top = 8;\n bn_mul_comba4(rr->d, a->d, b->d);\n goto end;\n }\n# endif\n if (al == 8) {\n if (bn_wexpand(rr, 16) == NULL)\n goto err;\n rr->top = 16;\n bn_mul_comba8(rr->d, a->d, b->d);\n goto end;\n }\n }\n#endif\n#ifdef BN_RECURSION\n if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {\n if (i >= -1 && i <= 1) {\n if (i >= 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n }\n if (i == -1) {\n j = BN_num_bits_word((BN_ULONG)bl);\n }\n j = 1 << (j - 1);\n assert(j <= al || j <= bl);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (al > j || bl > j) {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d,\n j, al - j, bl - j, t->d);\n } else {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);\n }\n rr->top = top;\n goto end;\n }\n }\n#endif\n if (bn_wexpand(rr, top) == NULL)\n goto err;\n rr->top = top;\n bn_mul_normal(rr->d, a->d, al, b->d, bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n end:\n#endif\n rr->neg = a->neg ^ b->neg;\n bn_correct_top(rr);\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(r);\n BN_CTX_end(ctx);\n return (ret);\n}', 'void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n,\n int tna, int tnb, BN_ULONG *t)\n{\n int i, j, n2 = n * 2;\n int c1, c2, neg;\n BN_ULONG ln, lo, *p;\n if (n < 8) {\n bn_mul_normal(r, a, n + tna, b, n + tnb);\n return;\n }\n c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);\n c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);\n neg = 0;\n switch (c1 * 3 + c2) {\n case -4:\n bn_sub_part_words(t, &(a[n]), a, tna, tna - n);\n bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb);\n break;\n case -3:\n case -2:\n bn_sub_part_words(t, &(a[n]), a, tna, tna - n);\n bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);\n neg = 1;\n break;\n case -1:\n case 0:\n case 1:\n case 2:\n bn_sub_part_words(t, a, &(a[n]), tna, n - tna);\n bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb);\n neg = 1;\n break;\n case 3:\n case 4:\n bn_sub_part_words(t, a, &(a[n]), tna, n - tna);\n bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);\n break;\n }\n# if 0\n if (n == 4) {\n bn_mul_comba4(&(t[n2]), t, &(t[n]));\n bn_mul_comba4(r, a, b);\n bn_mul_normal(&(r[n2]), &(a[n]), tn, &(b[n]), tn);\n memset(&r[n2 + tn * 2], 0, sizeof(*r) * (n2 - tn * 2));\n } else\n# endif\n if (n == 8) {\n bn_mul_comba8(&(t[n2]), t, &(t[n]));\n bn_mul_comba8(r, a, b);\n bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);\n memset(&r[n2 + tna + tnb], 0, sizeof(*r) * (n2 - tna - tnb));\n } else {\n p = &(t[n2 * 2]);\n bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);\n bn_mul_recursive(r, a, b, n, 0, 0, p);\n i = n / 2;\n if (tna > tnb)\n j = tna - i;\n else\n j = tnb - i;\n if (j == 0) {\n bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n memset(&r[n2 + i * 2], 0, sizeof(*r) * (n2 - i * 2));\n } else if (j > 0) {\n bn_mul_part_recursive(&(r[n2]), &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n memset(&(r[n2 + tna + tnb]), 0,\n sizeof(BN_ULONG) * (n2 - tna - tnb));\n } else {\n memset(&r[n2], 0, sizeof(*r) * n2);\n if (tna < BN_MUL_RECURSIVE_SIZE_NORMAL\n && tnb < BN_MUL_RECURSIVE_SIZE_NORMAL) {\n bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);\n } else {\n for (;;) {\n i /= 2;\n if (i < tna || i < tnb) {\n bn_mul_part_recursive(&(r[n2]),\n &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n break;\n } else if (i == tna || i == tnb) {\n bn_mul_recursive(&(r[n2]),\n &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n break;\n }\n }\n }\n }\n }\n c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));\n if (neg) {\n c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));\n } else {\n c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));\n }\n c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));\n if (c1) {\n p = &(r[n + n2]);\n lo = *p;\n ln = (lo + c1) & BN_MASK2;\n *p = ln;\n if (ln < (BN_ULONG)c1) {\n do {\n p++;\n lo = *p;\n ln = (lo + 1) & BN_MASK2;\n *p = ln;\n } while (ln == 0);\n }\n }\n}', 'int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl)\n{\n int n, i;\n n = cl - 1;\n if (dl < 0) {\n for (i = dl; i < 0; i++) {\n if (b[n - i] != 0)\n return -1;\n }\n }\n if (dl > 0) {\n for (i = dl; i > 0; i--) {\n if (a[n + i] != 0)\n return 1;\n }\n }\n return bn_cmp_words(a, b, cl);\n}', 'int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)\n{\n int i;\n BN_ULONG aa, bb;\n aa = a[n - 1];\n bb = b[n - 1];\n if (aa != bb)\n return ((aa > bb) ? 1 : -1);\n for (i = n - 2; i >= 0; i--) {\n aa = a[i];\n bb = b[i];\n if (aa != bb)\n return ((aa > bb) ? 1 : -1);\n }\n return (0);\n}']
2,308
0
https://github.com/openssl/openssl/blob/9b67b4b3caf071f490b95128f5dd44d9ce52032d/crypto/asn1/t_x509.c/#L336
int ASN1_UTCTIME_print(BIO *bp, ASN1_UTCTIME *tm) { char *v; int gmt=0; int i; int y=0,M=0,d=0,h=0,m=0,s=0; i=tm->length; v=(char *)tm->data; if (i < 10) goto err; if (v[i-1] == 'Z') gmt=1; for (i=0; i<10; i++) if ((v[i] > '9') || (v[i] < '0')) goto err; y= (v[0]-'0')*10+(v[1]-'0'); if (y < 50) y+=100; M= (v[2]-'0')*10+(v[3]-'0'); if ((M > 12) || (M < 1)) goto err; d= (v[4]-'0')*10+(v[5]-'0'); h= (v[6]-'0')*10+(v[7]-'0'); m= (v[8]-'0')*10+(v[9]-'0'); if ( (v[10] >= '0') && (v[10] <= '9') && (v[11] >= '0') && (v[11] <= '9')) s= (v[10]-'0')*10+(v[11]-'0'); if (BIO_printf(bp,"%s %2d %02d:%02d:%02d %d%s", mon[M-1],d,h,m,s,y+1900,(gmt)?" GMT":"") <= 0) return(0); else return(1); err: BIO_write(bp,"Bad time value",14); return(0); }
['ASN1_INTEGER *BN_to_ASN1_INTEGER(BIGNUM *bn, ASN1_INTEGER *ai)\n\t{\n\tASN1_INTEGER *ret;\n\tint len,j;\n\tif (ai == NULL)\n\t\tret=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->neg) ret->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\tret->data=(unsigned char *)Malloc(len+4);\n\tret->length=BN_bn2bin(bn,ret->data);\n\treturn(ret);\nerr:\n\tif (ret != ai) ASN1_INTEGER_free(ret);\n\treturn(NULL);\n\t}', 'int BN_bn2bin(const BIGNUM *a, unsigned char *to)\n\t{\n\tint n,i;\n\tBN_ULONG l;\n\tn=i=BN_num_bytes(a);\n\twhile (i-- > 0)\n\t\t{\n\t\tl=a->d[i/BN_BYTES];\n\t\t*(to++)=(unsigned char)(l>>(8*(i%BN_BYTES)))&0xff;\n\t\t}\n\treturn(n);\n\t}', 'int ASN1_UTCTIME_print(BIO *bp, ASN1_UTCTIME *tm)\n\t{\n\tchar *v;\n\tint gmt=0;\n\tint i;\n\tint y=0,M=0,d=0,h=0,m=0,s=0;\n\ti=tm->length;\n\tv=(char *)tm->data;\n\tif (i < 10) goto err;\n\tif (v[i-1] == \'Z\') gmt=1;\n\tfor (i=0; i<10; i++)\n\t\tif ((v[i] > \'9\') || (v[i] < \'0\')) goto err;\n\ty= (v[0]-\'0\')*10+(v[1]-\'0\');\n\tif (y < 50) y+=100;\n\tM= (v[2]-\'0\')*10+(v[3]-\'0\');\n\tif ((M > 12) || (M < 1)) goto err;\n\td= (v[4]-\'0\')*10+(v[5]-\'0\');\n\th= (v[6]-\'0\')*10+(v[7]-\'0\');\n\tm= (v[8]-\'0\')*10+(v[9]-\'0\');\n\tif (\t(v[10] >= \'0\') && (v[10] <= \'9\') &&\n\t\t(v[11] >= \'0\') && (v[11] <= \'9\'))\n\t\ts= (v[10]-\'0\')*10+(v[11]-\'0\');\n\tif (BIO_printf(bp,"%s %2d %02d:%02d:%02d %d%s",\n\t\tmon[M-1],d,h,m,s,y+1900,(gmt)?" GMT":"") <= 0)\n\t\treturn(0);\n\telse\n\t\treturn(1);\nerr:\n\tBIO_write(bp,"Bad time value",14);\n\treturn(0);\n\t}']
2,309
0
https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/crypto/bn/bn_lib.c/#L377
BIGNUM *bn_expand2(BIGNUM *b, int words) { BN_ULONG *A,*B,*a; int i,j; bn_check_top(b); if (words > b->max) { bn_check_top(b); if (BN_get_flags(b,BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND2,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return(NULL); } a=A=(BN_ULONG *)Malloc(sizeof(BN_ULONG)*(words+1)); if (A == NULL) { BNerr(BN_F_BN_EXPAND2,ERR_R_MALLOC_FAILURE); return(NULL); } memset(A,0x5c,sizeof(BN_ULONG)*(words+1)); #if 1 B=b->d; if (B != NULL) { for (i=b->top&(~7); i>0; i-=8) { A[0]=B[0]; A[1]=B[1]; A[2]=B[2]; A[3]=B[3]; A[4]=B[4]; A[5]=B[5]; A[6]=B[6]; A[7]=B[7]; A+=8; B+=8; } switch (b->top&7) { case 7: A[6]=B[6]; case 6: A[5]=B[5]; case 5: A[4]=B[4]; case 4: A[3]=B[3]; case 3: A[2]=B[2]; case 2: A[1]=B[1]; case 1: A[0]=B[0]; case 0: ; } Free(b->d); } b->d=a; b->max=words; B= &(b->d[b->top]); j=(b->max - b->top) & ~7; for (i=0; i<j; i+=8) { B[0]=0; B[1]=0; B[2]=0; B[3]=0; B[4]=0; B[5]=0; B[6]=0; B[7]=0; B+=8; } j=(b->max - b->top) & 7; for (i=0; i<j; i++) { B[0]=0; B++; } #else memcpy(a->d,b->d,sizeof(b->d[0])*b->top); #endif } return(b); }
['int BN_MONT_CTX_set(BN_MONT_CTX *mont, BIGNUM *mod, BN_CTX *ctx)\n\t{\n\tBIGNUM Ri,*R;\n\tBN_init(&Ri);\n\tR= &(mont->RR);\n\tBN_copy(&(mont->N),mod);\n#ifdef BN_RECURSION_MONT\n\tif (mont->N.top < BN_MONT_CTX_SET_SIZE_WORD)\n#endif\n\t\t{\n\t\tBIGNUM tmod;\n\t\tBN_ULONG buf[2];\n\t\tmont->use_word=1;\n\t\tmont->ri=(BN_num_bits(mod)+(BN_BITS2-1))/BN_BITS2*BN_BITS2;\n\t\tBN_zero(R);\n\t\tBN_set_bit(R,BN_BITS2);\n\t\tbuf[0]=mod->d[0];\n\t\tbuf[1]=0;\n\t\ttmod.d=buf;\n\t\ttmod.top=1;\n\t\ttmod.max=mod->max;\n\t\ttmod.neg=mod->neg;\n\t\tif ((BN_mod_inverse(&Ri,R,&tmod,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tBN_lshift(&Ri,&Ri,BN_BITS2);\n\t\tif (!BN_is_zero(&Ri))\n\t\t\t{\n#if 1\n\t\t\tBN_sub_word(&Ri,1);\n#else\n\t\t\tBN_usub(&Ri,&Ri,BN_value_one());\n#endif\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tBN_set_word(&Ri,BN_MASK2);\n\t\t\t}\n\t\tBN_div(&Ri,NULL,&Ri,&tmod,ctx);\n\t\tmont->n0=Ri.d[0];\n\t\tBN_free(&Ri);\n\t\t}\n#ifdef BN_RECURSION_MONT\n\telse\n\t\t{\n\t\tmont->use_word=0;\n\t\tmont->ri=(BN_num_bits(mod)+(BN_BITS2-1))/BN_BITS2*BN_BITS2;\n#if 1\n\t\tBN_zero(R);\n\t\tBN_set_bit(R,mont->ri);\n#else\n\t\tBN_lshift(R,BN_value_one(),mont->ri);\n#endif\n\t\tif ((BN_mod_inverse(&Ri,R,mod,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tBN_lshift(&Ri,&Ri,mont->ri);\n#if 1\n\t\tBN_sub_word(&Ri,1);\n#else\n\t\tBN_usub(&Ri,&Ri,BN_value_one());\n#endif\n\t\tBN_div(&(mont->Ni),NULL,&Ri,mod,ctx);\n\t\tBN_free(&Ri);\n\t\t}\n#endif\n#if 1\n\tBN_zero(&(mont->RR));\n\tBN_set_bit(&(mont->RR),mont->ri*2);\n#else\n\tBN_lshift(mont->RR,BN_value_one(),mont->ri*2);\n#endif\n\tBN_mod(&(mont->RR),&(mont->RR),&(mont->N),ctx);\n\treturn(1);\nerr:\n\treturn(0);\n\t}', 'BIGNUM *BN_mod_inverse(BIGNUM *in, BIGNUM *a, BIGNUM *n, BN_CTX *ctx)\n\t{\n\tBIGNUM *A,*B,*X,*Y,*M,*D,*R;\n\tBIGNUM *T,*ret=NULL;\n\tint sign;\n\tbn_check_top(a);\n\tbn_check_top(n);\n\tA= &(ctx->bn[ctx->tos]);\n\tB= &(ctx->bn[ctx->tos+1]);\n\tX= &(ctx->bn[ctx->tos+2]);\n\tD= &(ctx->bn[ctx->tos+3]);\n\tM= &(ctx->bn[ctx->tos+4]);\n\tY= &(ctx->bn[ctx->tos+5]);\n\tctx->tos+=6;\n\tif (in == NULL)\n\t\tR=BN_new();\n\telse\n\t\tR=in;\n\tif (R == NULL) goto err;\n\tBN_zero(X);\n\tBN_one(Y);\n\tif (BN_copy(A,a) == NULL) goto err;\n\tif (BN_copy(B,n) == NULL) goto err;\n\tsign=1;\n\twhile (!BN_is_zero(B))\n\t\t{\n\t\tif (!BN_div(D,M,A,B,ctx)) goto err;\n\t\tT=A;\n\t\tA=B;\n\t\tB=M;\n\t\tif (!BN_mul(T,D,X,ctx)) goto err;\n\t\tif (!BN_add(T,T,Y)) goto err;\n\t\tM=Y;\n\t\tY=X;\n\t\tX=T;\n\t\tsign= -sign;\n\t\t}\n\tif (sign < 0)\n\t\t{\n\t\tif (!BN_sub(Y,n,Y)) goto err;\n\t\t}\n\tif (BN_is_one(A))\n\t\t{ if (!BN_mod(R,Y,n,ctx)) goto err; }\n\telse\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_INVERSE,BN_R_NO_INVERSE);\n\t\tgoto err;\n\t\t}\n\tret=R;\nerr:\n\tif ((ret == NULL) && (in == NULL)) BN_free(R);\n\tctx->tos-=6;\n\treturn(ret);\n\t}', 'int BN_lshift(BIGNUM *r, BIGNUM *a, int n)\n\t{\n\tint i,nw,lb,rb;\n\tBN_ULONG *t,*f;\n\tBN_ULONG l;\n\tr->neg=a->neg;\n\tif (bn_wexpand(r,a->top+(n/BN_BITS2)+1) == NULL) return(0);\n\tnw=n/BN_BITS2;\n\tlb=n%BN_BITS2;\n\trb=BN_BITS2-lb;\n\tf=a->d;\n\tt=r->d;\n\tt[a->top+nw]=0;\n\tif (lb == 0)\n\t\tfor (i=a->top-1; i>=0; i--)\n\t\t\tt[nw+i]=f[i];\n\telse\n\t\tfor (i=a->top-1; i>=0; i--)\n\t\t\t{\n\t\t\tl=f[i];\n\t\t\tt[nw+i+1]|=(l>>rb)&BN_MASK2;\n\t\t\tt[nw+i]=(l<<lb)&BN_MASK2;\n\t\t\t}\n\tmemset(t,0,nw*sizeof(t[0]));\n\tr->top=a->top+nw+1;\n\tbn_fix_top(r);\n\treturn(1);\n\t}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n\t{\n\tint i,n;\n\tif (bn_expand(a,sizeof(BN_ULONG)*8) == NULL) return(0);\n\tn=sizeof(BN_ULONG)/BN_BYTES;\n\ta->neg=0;\n\ta->top=0;\n\ta->d[0]=(BN_ULONG)w&BN_MASK2;\n\tif (a->d[0] != 0) a->top=1;\n\tfor (i=1; i<n; i++)\n\t\t{\n#ifndef SIXTY_FOUR_BIT\n\t\tw>>=BN_BITS4;\n\t\tw>>=BN_BITS4;\n#endif\n\t\ta->d[i]=(BN_ULONG)w&BN_MASK2;\n\t\tif (a->d[i] != 0) a->top=i+1;\n\t\t}\n\treturn(1);\n\t}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n\t{\n\tBN_ULONG *A,*B,*a;\n\tint i,j;\n\tbn_check_top(b);\n\tif (words > b->max)\n\t\t{\n\t\tbn_check_top(b);\n\t\tif (BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_EXPAND2,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n\t\t\treturn(NULL);\n\t\t\t}\n\t\ta=A=(BN_ULONG *)Malloc(sizeof(BN_ULONG)*(words+1));\n\t\tif (A == NULL)\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_EXPAND2,ERR_R_MALLOC_FAILURE);\n\t\t\treturn(NULL);\n\t\t\t}\nmemset(A,0x5c,sizeof(BN_ULONG)*(words+1));\n#if 1\n\t\tB=b->d;\n\t\tif (B != NULL)\n\t\t\t{\n\t\t\tfor (i=b->top&(~7); i>0; i-=8)\n\t\t\t\t{\n\t\t\t\tA[0]=B[0]; A[1]=B[1]; A[2]=B[2]; A[3]=B[3];\n\t\t\t\tA[4]=B[4]; A[5]=B[5]; A[6]=B[6]; A[7]=B[7];\n\t\t\t\tA+=8;\n\t\t\t\tB+=8;\n\t\t\t\t}\n\t\t\tswitch (b->top&7)\n\t\t\t\t{\n\t\t\tcase 7:\n\t\t\t\tA[6]=B[6];\n\t\t\tcase 6:\n\t\t\t\tA[5]=B[5];\n\t\t\tcase 5:\n\t\t\t\tA[4]=B[4];\n\t\t\tcase 4:\n\t\t\t\tA[3]=B[3];\n\t\t\tcase 3:\n\t\t\t\tA[2]=B[2];\n\t\t\tcase 2:\n\t\t\t\tA[1]=B[1];\n\t\t\tcase 1:\n\t\t\t\tA[0]=B[0];\n\t\t\tcase 0:\n\t\t\t\t;\n\t\t\t\t}\n\t\t\tFree(b->d);\n\t\t\t}\n\t\tb->d=a;\n\t\tb->max=words;\n\t\tB= &(b->d[b->top]);\n\t\tj=(b->max - b->top) & ~7;\n\t\tfor (i=0; i<j; i+=8)\n\t\t\t{\n\t\t\tB[0]=0; B[1]=0; B[2]=0; B[3]=0;\n\t\t\tB[4]=0; B[5]=0; B[6]=0; B[7]=0;\n\t\t\tB+=8;\n\t\t\t}\n\t\tj=(b->max - b->top) & 7;\n\t\tfor (i=0; i<j; i++)\n\t\t\t{\n\t\t\tB[0]=0;\n\t\t\tB++;\n\t\t\t}\n#else\n\t\t\tmemcpy(a->d,b->d,sizeof(b->d[0])*b->top);\n#endif\n\t\t}\n\treturn(b);\n\t}']
2,310
0
https://github.com/openssl/openssl/blob/05bbf78afd4cc10c776ad141749c13c25a2daa5e/ssl/ssl_cert.c/#L751
int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack, const char *dir) { DIR *d; struct dirent *dstruct; int ret = 0; CRYPTO_w_lock(CRYPTO_LOCK_READDIR); d = opendir(dir); if(!d) { SYSerr(SYS_F_OPENDIR, get_last_sys_error()); ERR_add_error_data(3, "opendir('", dir, "')"); SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK, ERR_R_SYS_LIB); goto err; } while((dstruct=readdir(d))) { char buf[1024]; int r; if(strlen(dir)+strlen(dstruct->d_name)+2 > sizeof buf) { SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK,SSL_R_PATH_TOO_LONG); goto err; } r = BIO_snprintf(buf,sizeof buf,"%s/%s",dir,dstruct->d_name); if (r <= 0 || r >= sizeof buf) goto err; if(!SSL_add_file_cert_subjects_to_stack(stack,buf)) goto err; } ret = 1; err: CRYPTO_w_unlock(CRYPTO_LOCK_READDIR); return ret; }
['int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,\n\t\t\t\t const char *dir)\n\t{\n\tDIR *d;\n\tstruct dirent *dstruct;\n\tint ret = 0;\n\tCRYPTO_w_lock(CRYPTO_LOCK_READDIR);\n\td = opendir(dir);\n\tif(!d)\n\t\t{\n\t\tSYSerr(SYS_F_OPENDIR, get_last_sys_error());\n\t\tERR_add_error_data(3, "opendir(\'", dir, "\')");\n\t\tSSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK, ERR_R_SYS_LIB);\n\t\tgoto err;\n\t\t}\n\twhile((dstruct=readdir(d)))\n\t\t{\n\t\tchar buf[1024];\n\t\tint r;\n\t\tif(strlen(dir)+strlen(dstruct->d_name)+2 > sizeof buf)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK,SSL_R_PATH_TOO_LONG);\n\t\t\tgoto err;\n\t\t\t}\n\t\tr = BIO_snprintf(buf,sizeof buf,"%s/%s",dir,dstruct->d_name);\n\t\tif (r <= 0 || r >= sizeof buf)\n\t\t\tgoto err;\n\t\tif(!SSL_add_file_cert_subjects_to_stack(stack,buf))\n\t\t\tgoto err;\n\t\t}\n\tret = 1;\nerr:\n\tCRYPTO_w_unlock(CRYPTO_LOCK_READDIR);\n\treturn ret;\n\t}', 'void CRYPTO_lock(int mode, int type, const char *file, int line)\n\t{\n#ifdef LOCK_DEBUG\n\t\t{\n\t\tchar *rw_text,*operation_text;\n\t\tif (mode & CRYPTO_LOCK)\n\t\t\toperation_text="lock ";\n\t\telse if (mode & CRYPTO_UNLOCK)\n\t\t\toperation_text="unlock";\n\t\telse\n\t\t\toperation_text="ERROR ";\n\t\tif (mode & CRYPTO_READ)\n\t\t\trw_text="r";\n\t\telse if (mode & CRYPTO_WRITE)\n\t\t\trw_text="w";\n\t\telse\n\t\t\trw_text="ERROR";\n\t\tfprintf(stderr,"lock:%08lx:(%s)%s %-18s %s:%d\\n",\n\t\t\tCRYPTO_thread_id(), rw_text, operation_text,\n\t\t\tCRYPTO_get_lock_name(type), file, line);\n\t\t}\n#endif\n\tif (type < 0)\n\t\t{\n\t\tstruct CRYPTO_dynlock_value *pointer\n\t\t\t= CRYPTO_get_dynlock_value(type);\n\t\tif (pointer && dynlock_lock_callback)\n\t\t\t{\n\t\t\tdynlock_lock_callback(mode, pointer, file, line);\n\t\t\t}\n\t\tCRYPTO_destroy_dynlockid(type);\n\t\t}\n\telse\n\t\tif (locking_callback != NULL)\n\t\t\tlocking_callback(mode,type,file,line);\n\t}', 'void ERR_put_error(int lib, int func, int reason, const char *file,\n\t int line)\n\t{\n\tERR_STATE *es;\n#ifdef _OSD_POSIX\n\tif (strncmp(file,"*POSIX(", sizeof("*POSIX(")-1) == 0) {\n\t\tchar *end;\n\t\tfile += sizeof("*POSIX(")-1;\n\t\tend = &file[strlen(file)-1];\n\t\tif (*end == \')\')\n\t\t\t*end = \'\\0\';\n\t\tif ((end = strrchr(file, \'/\')) != NULL)\n\t\t\tfile = &end[1];\n\t}\n#endif\n\tes=ERR_get_state();\n\tes->top=(es->top+1)%ERR_NUM_ERRORS;\n\tif (es->top == es->bottom)\n\t\tes->bottom=(es->bottom+1)%ERR_NUM_ERRORS;\n\tes->err_buffer[es->top]=ERR_PACK(lib,func,reason);\n\tes->err_file[es->top]=file;\n\tes->err_line[es->top]=line;\n\terr_clear_data(es,es->top);\n\t}']
2,311
0
https://github.com/libav/libav/blob/25b6837f7cacd691b19cbc12b9dad1ce84a318a1/libavcodec/vp8dsp.c/#L345
NORMAL_LIMIT(7)
['NORMAL_LIMIT(7)']
2,312
0
https://github.com/openssl/openssl/blob/183733f882056ea3e6fe95e665b85fcc6a45dcb4/crypto/rsa/rsa_x931g.c/#L108
int RSA_X931_derive_ex(RSA *rsa, BIGNUM *p1, BIGNUM *p2, BIGNUM *q1, BIGNUM *q2, const BIGNUM *Xp1, const BIGNUM *Xp2, const BIGNUM *Xp, const BIGNUM *Xq1, const BIGNUM *Xq2, const BIGNUM *Xq, const BIGNUM *e, BN_GENCB *cb) { BIGNUM *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL; BN_CTX *ctx = NULL, *ctx2 = NULL; int ret = 0; if (!rsa) goto err; ctx = BN_CTX_new(); if (ctx == NULL) goto err; BN_CTX_start(ctx); r0 = BN_CTX_get(ctx); r1 = BN_CTX_get(ctx); r2 = BN_CTX_get(ctx); r3 = BN_CTX_get(ctx); if (r3 == NULL) goto err; if (!rsa->e) { rsa->e = BN_dup(e); if (!rsa->e) goto err; } else e = rsa->e; if (Xp && rsa->p == NULL) { rsa->p = BN_new(); if (rsa->p == NULL) goto err; if (!BN_X931_derive_prime_ex(rsa->p, p1, p2, Xp, Xp1, Xp2, e, ctx, cb)) goto err; } if (Xq && rsa->q == NULL) { rsa->q = BN_new(); if (rsa->q == NULL) goto err; if (!BN_X931_derive_prime_ex(rsa->q, q1, q2, Xq, Xq1, Xq2, e, ctx, cb)) goto err; } if (rsa->p == NULL || rsa->q == NULL) { BN_CTX_end(ctx); BN_CTX_free(ctx); return 2; } rsa->n = BN_new(); if (rsa->n == NULL) goto err; if (!BN_mul(rsa->n, rsa->p, rsa->q, ctx)) goto err; if (!BN_sub(r1, rsa->p, BN_value_one())) goto err; if (!BN_sub(r2, rsa->q, BN_value_one())) goto err; if (!BN_mul(r0, r1, r2, ctx)) goto err; if (!BN_gcd(r3, r1, r2, ctx)) goto err; if (!BN_div(r0, NULL, r0, r3, ctx)) goto err; ctx2 = BN_CTX_new(); if (ctx2 == NULL) goto err; rsa->d = BN_mod_inverse(NULL, rsa->e, r0, ctx2); if (rsa->d == NULL) goto err; rsa->dmp1 = BN_new(); if (rsa->dmp1 == NULL) goto err; if (!BN_mod(rsa->dmp1, rsa->d, r1, ctx)) goto err; rsa->dmq1 = BN_new(); if (rsa->dmq1 == NULL) goto err; if (!BN_mod(rsa->dmq1, rsa->d, r2, ctx)) goto err; rsa->iqmp = BN_mod_inverse(NULL, rsa->q, rsa->p, ctx2); ret = 1; err: if (ctx) BN_CTX_end(ctx); BN_CTX_free(ctx); BN_CTX_free(ctx2); return ret; }
['int RSA_X931_derive_ex(RSA *rsa, BIGNUM *p1, BIGNUM *p2, BIGNUM *q1,\n BIGNUM *q2, const BIGNUM *Xp1, const BIGNUM *Xp2,\n const BIGNUM *Xp, const BIGNUM *Xq1, const BIGNUM *Xq2,\n const BIGNUM *Xq, const BIGNUM *e, BN_GENCB *cb)\n{\n BIGNUM *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL;\n BN_CTX *ctx = NULL, *ctx2 = NULL;\n int ret = 0;\n if (!rsa)\n goto err;\n ctx = BN_CTX_new();\n if (ctx == NULL)\n goto err;\n BN_CTX_start(ctx);\n r0 = BN_CTX_get(ctx);\n r1 = BN_CTX_get(ctx);\n r2 = BN_CTX_get(ctx);\n r3 = BN_CTX_get(ctx);\n if (r3 == NULL)\n goto err;\n if (!rsa->e) {\n rsa->e = BN_dup(e);\n if (!rsa->e)\n goto err;\n } else\n e = rsa->e;\n if (Xp && rsa->p == NULL) {\n rsa->p = BN_new();\n if (rsa->p == NULL)\n goto err;\n if (!BN_X931_derive_prime_ex(rsa->p, p1, p2,\n Xp, Xp1, Xp2, e, ctx, cb))\n goto err;\n }\n if (Xq && rsa->q == NULL) {\n rsa->q = BN_new();\n if (rsa->q == NULL)\n goto err;\n if (!BN_X931_derive_prime_ex(rsa->q, q1, q2,\n Xq, Xq1, Xq2, e, ctx, cb))\n goto err;\n }\n if (rsa->p == NULL || rsa->q == NULL) {\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n return 2;\n }\n rsa->n = BN_new();\n if (rsa->n == NULL)\n goto err;\n if (!BN_mul(rsa->n, rsa->p, rsa->q, ctx))\n goto err;\n if (!BN_sub(r1, rsa->p, BN_value_one()))\n goto err;\n if (!BN_sub(r2, rsa->q, BN_value_one()))\n goto err;\n if (!BN_mul(r0, r1, r2, ctx))\n goto err;\n if (!BN_gcd(r3, r1, r2, ctx))\n goto err;\n if (!BN_div(r0, NULL, r0, r3, ctx))\n goto err;\n ctx2 = BN_CTX_new();\n if (ctx2 == NULL)\n goto err;\n rsa->d = BN_mod_inverse(NULL, rsa->e, r0, ctx2);\n if (rsa->d == NULL)\n goto err;\n rsa->dmp1 = BN_new();\n if (rsa->dmp1 == NULL)\n goto err;\n if (!BN_mod(rsa->dmp1, rsa->d, r1, ctx))\n goto err;\n rsa->dmq1 = BN_new();\n if (rsa->dmq1 == NULL)\n goto err;\n if (!BN_mod(rsa->dmq1, rsa->d, r2, ctx))\n goto err;\n rsa->iqmp = BN_mod_inverse(NULL, rsa->q, rsa->p, ctx2);\n ret = 1;\n err:\n if (ctx)\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n BN_CTX_free(ctx2);\n return ret;\n}', 'BN_CTX *BN_CTX_new(void)\n{\n BN_CTX *ret;\n if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {\n BNerr(BN_F_BN_CTX_NEW, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n BN_POOL_init(&ret->pool);\n BN_STACK_init(&ret->stack);\n return ret;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (num <= 0)\n return NULL;\n allow_customize = 0;\n#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}', 'static void BN_POOL_init(BN_POOL *p)\n{\n p->head = p->current = p->tail = NULL;\n p->used = p->size = 0;\n}', 'static void BN_STACK_init(BN_STACK *st)\n{\n st->indexes = NULL;\n st->depth = st->size = 0;\n}', '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}', 'static int BN_STACK_push(BN_STACK *st, unsigned int idx)\n{\n if (st->depth == st->size) {\n unsigned int newsize =\n st->size ? (st->size * 3 / 2) : BN_CTX_START_FRAMES;\n unsigned int *newitems = OPENSSL_malloc(sizeof(*newitems) * newsize);\n if (newitems == NULL)\n return 0;\n if (st->depth)\n memcpy(newitems, st->indexes, sizeof(*newitems) * st->depth);\n OPENSSL_free(st->indexes);\n st->indexes = newitems;\n st->size = newsize;\n }\n st->indexes[(st->depth)++] = idx;\n return 1;\n}', 'void CRYPTO_free(void *str)\n{\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_free(str, 0);\n free(str);\n CRYPTO_mem_debug_free(str, 1);\n } else {\n free(str);\n }\n#else\n free(str);\n#endif\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return (1);\n}', 'BIGNUM *BN_dup(const BIGNUM *a)\n{\n BIGNUM *t;\n if (a == NULL)\n return NULL;\n bn_check_top(a);\n t = BN_get_flags(a, BN_FLG_SECURE) ? BN_secure_new() : BN_new();\n if (t == NULL)\n return NULL;\n if (!BN_copy(t, a)) {\n BN_free(t);\n return NULL;\n }\n bn_check_top(t);\n return t;\n}', 'int BN_get_flags(const BIGNUM *b, int n)\n{\n return b->flags & n;\n}', 'BIGNUM *BN_new(void)\n{\n BIGNUM *ret;\n if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {\n BNerr(BN_F_BN_NEW, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n ret->flags = BN_FLG_MALLOCED;\n bn_check_top(ret);\n return (ret);\n}']
2,313
0
https://github.com/openssl/openssl/blob/e02c519cd32a55e6ad39a0cfbeeda775f9115f28/crypto/bn/bn_sqr.c/#L118
void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp) { int i, j, max; const BN_ULONG *ap; BN_ULONG *rp; max = n * 2; ap = a; rp = r; rp[0] = rp[max - 1] = 0; rp++; j = n; if (--j > 0) { ap++; rp[j] = bn_mul_words(rp, ap, j, ap[-1]); rp += 2; } for (i = n - 2; i > 0; i--) { j--; ap++; rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]); rp += 2; } bn_add_words(r, r, r, max); bn_sqr_words(tmp, a, n); bn_add_words(r, r, tmp, max); }
['static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)\n{\n BIGNUM *r1, *m1, *vrfy, *r2, *m[RSA_MAX_PRIME_NUM - 2];\n int ret = 0, i, ex_primes = 0, smooth = 0;\n RSA_PRIME_INFO *pinfo;\n BN_CTX_start(ctx);\n r1 = BN_CTX_get(ctx);\n r2 = BN_CTX_get(ctx);\n m1 = BN_CTX_get(ctx);\n vrfy = BN_CTX_get(ctx);\n if (vrfy == NULL)\n goto err;\n if (rsa->version == RSA_ASN1_VERSION_MULTI\n && ((ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) <= 0\n || ex_primes > RSA_MAX_PRIME_NUM - 2))\n goto err;\n if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {\n BIGNUM *factor = BN_new();\n if (factor == NULL)\n goto err;\n if (!(BN_with_flags(factor, rsa->p, BN_FLG_CONSTTIME),\n BN_MONT_CTX_set_locked(&rsa->_method_mod_p, rsa->lock,\n factor, ctx))\n || !(BN_with_flags(factor, rsa->q, BN_FLG_CONSTTIME),\n BN_MONT_CTX_set_locked(&rsa->_method_mod_q, rsa->lock,\n factor, ctx))) {\n BN_free(factor);\n goto err;\n }\n for (i = 0; i < ex_primes; i++) {\n pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);\n BN_with_flags(factor, pinfo->r, BN_FLG_CONSTTIME);\n if (!BN_MONT_CTX_set_locked(&pinfo->m, rsa->lock, factor, ctx)) {\n BN_free(factor);\n goto err;\n }\n }\n BN_free(factor);\n smooth = (ex_primes == 0)\n && (rsa->meth->bn_mod_exp == BN_mod_exp_mont)\n && (BN_num_bits(rsa->q) == BN_num_bits(rsa->p));\n }\n if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)\n if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,\n rsa->n, ctx))\n goto err;\n if (smooth) {\n if (\n !bn_from_mont_fixed_top(m1, I, rsa->_method_mod_q, ctx)\n || !bn_to_mont_fixed_top(m1, m1, rsa->_method_mod_q, ctx)\n || !BN_mod_exp_mont_consttime(m1, m1, rsa->dmq1, rsa->q, ctx,\n rsa->_method_mod_q)\n || !bn_from_mont_fixed_top(r1, I, rsa->_method_mod_p, ctx)\n || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)\n || !BN_mod_exp_mont_consttime(r1, r1, rsa->dmp1, rsa->p, ctx,\n rsa->_method_mod_p)\n || !bn_mod_sub_fixed_top(r1, r1, m1, rsa->p)\n || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)\n || !bn_mul_mont_fixed_top(r1, r1, rsa->iqmp, rsa->_method_mod_p,\n ctx)\n || !bn_mul_fixed_top(r0, r1, rsa->q, ctx)\n || !bn_mod_add_fixed_top(r0, r0, m1, rsa->n))\n goto err;\n goto tail;\n }\n {\n BIGNUM *c = BN_new();\n if (c == NULL)\n goto err;\n BN_with_flags(c, I, BN_FLG_CONSTTIME);\n if (!BN_mod(r1, c, rsa->q, ctx)) {\n BN_free(c);\n goto err;\n }\n {\n BIGNUM *dmq1 = BN_new();\n if (dmq1 == NULL) {\n BN_free(c);\n goto err;\n }\n BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);\n if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx,\n rsa->_method_mod_q)) {\n BN_free(c);\n BN_free(dmq1);\n goto err;\n }\n BN_free(dmq1);\n }\n if (!BN_mod(r1, c, rsa->p, ctx)) {\n BN_free(c);\n goto err;\n }\n BN_free(c);\n }\n {\n BIGNUM *dmp1 = BN_new();\n if (dmp1 == NULL)\n goto err;\n BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);\n if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx,\n rsa->_method_mod_p)) {\n BN_free(dmp1);\n goto err;\n }\n BN_free(dmp1);\n }\n if (ex_primes > 0) {\n BIGNUM *di = BN_new(), *cc = BN_new();\n if (cc == NULL || di == NULL) {\n BN_free(cc);\n BN_free(di);\n goto err;\n }\n for (i = 0; i < ex_primes; i++) {\n if ((m[i] = BN_CTX_get(ctx)) == NULL) {\n BN_free(cc);\n BN_free(di);\n goto err;\n }\n pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);\n BN_with_flags(cc, I, BN_FLG_CONSTTIME);\n BN_with_flags(di, pinfo->d, BN_FLG_CONSTTIME);\n if (!BN_mod(r1, cc, pinfo->r, ctx)) {\n BN_free(cc);\n BN_free(di);\n goto err;\n }\n if (!rsa->meth->bn_mod_exp(m[i], r1, di, pinfo->r, ctx, pinfo->m)) {\n BN_free(cc);\n BN_free(di);\n goto err;\n }\n }\n BN_free(cc);\n BN_free(di);\n }\n if (!BN_sub(r0, r0, m1))\n goto err;\n if (BN_is_negative(r0))\n if (!BN_add(r0, r0, rsa->p))\n goto err;\n if (!BN_mul(r1, r0, rsa->iqmp, ctx))\n goto err;\n {\n BIGNUM *pr1 = BN_new();\n if (pr1 == NULL)\n goto err;\n BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);\n if (!BN_mod(r0, pr1, rsa->p, ctx)) {\n BN_free(pr1);\n goto err;\n }\n BN_free(pr1);\n }\n if (BN_is_negative(r0))\n if (!BN_add(r0, r0, rsa->p))\n goto err;\n if (!BN_mul(r1, r0, rsa->q, ctx))\n goto err;\n if (!BN_add(r0, r1, m1))\n goto err;\n if (ex_primes > 0) {\n BIGNUM *pr2 = BN_new();\n if (pr2 == NULL)\n goto err;\n for (i = 0; i < ex_primes; i++) {\n pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);\n if (!BN_sub(r1, m[i], r0)) {\n BN_free(pr2);\n goto err;\n }\n if (!BN_mul(r2, r1, pinfo->t, ctx)) {\n BN_free(pr2);\n goto err;\n }\n BN_with_flags(pr2, r2, BN_FLG_CONSTTIME);\n if (!BN_mod(r1, pr2, pinfo->r, ctx)) {\n BN_free(pr2);\n goto err;\n }\n if (BN_is_negative(r1))\n if (!BN_add(r1, r1, pinfo->r)) {\n BN_free(pr2);\n goto err;\n }\n if (!BN_mul(r1, r1, pinfo->pp, ctx)) {\n BN_free(pr2);\n goto err;\n }\n if (!BN_add(r0, r0, r1)) {\n BN_free(pr2);\n goto err;\n }\n }\n BN_free(pr2);\n }\n tail:\n if (rsa->e && rsa->n) {\n if (rsa->meth->bn_mod_exp == BN_mod_exp_mont) {\n if (!BN_mod_exp_mont(vrfy, r0, rsa->e, rsa->n, ctx,\n rsa->_method_mod_n))\n goto err;\n } else {\n bn_correct_top(r0);\n if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,\n rsa->_method_mod_n))\n goto err;\n }\n if (!BN_sub(vrfy, vrfy, I))\n goto err;\n if (BN_is_zero(vrfy)) {\n bn_correct_top(r0);\n ret = 1;\n goto err;\n }\n if (!BN_mod(vrfy, vrfy, rsa->n, ctx))\n goto err;\n if (BN_is_negative(vrfy))\n if (!BN_add(vrfy, vrfy, rsa->n))\n goto err;\n if (!BN_is_zero(vrfy)) {\n BIGNUM *d = BN_new();\n if (d == NULL)\n goto err;\n BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);\n if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx,\n rsa->_method_mod_n)) {\n BN_free(d);\n goto err;\n }\n BN_free(d);\n }\n }\n bn_correct_top(r0);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'int bn_from_mont_fixed_top(BIGNUM *ret, const BIGNUM *a, BN_MONT_CTX *mont,\n BN_CTX *ctx)\n{\n int retn = 0;\n#ifdef MONT_WORD\n BIGNUM *t;\n BN_CTX_start(ctx);\n if ((t = BN_CTX_get(ctx)) && BN_copy(t, a)) {\n retn = bn_from_montgomery_word(ret, t, mont);\n }\n BN_CTX_end(ctx);\n#else\n BIGNUM *t1, *t2;\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 (!BN_copy(t1, a))\n goto err;\n BN_mask_bits(t1, mont->ri);\n if (!BN_mul(t2, t1, &mont->Ni, ctx))\n goto err;\n BN_mask_bits(t2, mont->ri);\n if (!BN_mul(t1, t2, &mont->N, ctx))\n goto err;\n if (!BN_add(t2, a, t1))\n goto err;\n if (!BN_rshift(ret, t2, mont->ri))\n goto err;\n if (BN_ucmp(ret, &(mont->N)) >= 0) {\n if (!BN_usub(ret, ret, &(mont->N)))\n goto err;\n }\n retn = 1;\n bn_check_top(ret);\n err:\n BN_CTX_end(ctx);\n#endif\n return retn;\n}', 'int bn_mul_mont_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n BN_MONT_CTX *mont, BN_CTX *ctx)\n{\n BIGNUM *tmp;\n int ret = 0;\n int num = mont->N.top;\n#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\n if (num > 1 && a->top == num && b->top == num) {\n if (bn_wexpand(r, num) == NULL)\n return 0;\n if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {\n r->neg = a->neg ^ b->neg;\n r->top = num;\n r->flags |= BN_FLG_FIXED_TOP;\n return 1;\n }\n }\n#endif\n if ((a->top + b->top) > 2 * num)\n return 0;\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n if (tmp == NULL)\n goto err;\n bn_check_top(tmp);\n if (a == b) {\n if (!bn_sqr_fixed_top(tmp, a, ctx))\n goto err;\n } else {\n if (!bn_mul_fixed_top(tmp, a, b, ctx))\n goto err;\n }\n#ifdef MONT_WORD\n if (!bn_from_montgomery_word(r, tmp, mont))\n goto err;\n#else\n if (!BN_from_montgomery(r, tmp, mont, ctx))\n goto err;\n#endif\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'int bn_sqr_fixed_top(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int max, al;\n int ret = 0;\n BIGNUM *tmp, *rr;\n bn_check_top(a);\n al = a->top;\n if (al <= 0) {\n r->top = 0;\n r->neg = 0;\n return 1;\n }\n BN_CTX_start(ctx);\n rr = (a != r) ? r : BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (rr == NULL || tmp == NULL)\n goto err;\n max = 2 * al;\n if (bn_wexpand(rr, max) == NULL)\n goto err;\n if (al == 4) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[8];\n bn_sqr_normal(rr->d, a->d, 4, t);\n#else\n bn_sqr_comba4(rr->d, a->d);\n#endif\n } else if (al == 8) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[16];\n bn_sqr_normal(rr->d, a->d, 8, t);\n#else\n bn_sqr_comba8(rr->d, a->d);\n#endif\n } else {\n#if defined(BN_RECURSION)\n if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {\n BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];\n bn_sqr_normal(rr->d, a->d, al, t);\n } else {\n int j, k;\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n if (al == j) {\n if (bn_wexpand(tmp, k * 2) == NULL)\n goto err;\n bn_sqr_recursive(rr->d, a->d, al, tmp->d);\n } else {\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n }\n }\n#else\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n#endif\n }\n rr->neg = 0;\n rr->top = max;\n rr->flags |= BN_FLG_FIXED_TOP;\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(rr);\n bn_check_top(tmp);\n BN_CTX_end(ctx);\n return ret;\n}', 'void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)\n{\n int i, j, max;\n const BN_ULONG *ap;\n BN_ULONG *rp;\n max = n * 2;\n ap = a;\n rp = r;\n rp[0] = rp[max - 1] = 0;\n rp++;\n j = n;\n if (--j > 0) {\n ap++;\n rp[j] = bn_mul_words(rp, ap, j, ap[-1]);\n rp += 2;\n }\n for (i = n - 2; i > 0; i--) {\n j--;\n ap++;\n rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);\n rp += 2;\n }\n bn_add_words(r, r, r, max);\n bn_sqr_words(tmp, a, n);\n bn_add_words(r, r, tmp, max);\n}']
2,314
0
https://github.com/openssl/openssl/blob/3a66e306e45215b2dac68f66eb6b1012a94f17e5/crypto/bn/bn_lib.c/#L588
int BN_set_word(BIGNUM *a, BN_ULONG w) { int i,n; if (bn_expand(a,sizeof(BN_ULONG)*8) == NULL) return(0); n=sizeof(BN_ULONG)/BN_BYTES; a->neg=0; a->top=0; a->d[0]=(BN_ULONG)w&BN_MASK2; if (a->d[0] != 0) a->top=1; for (i=1; i<n; i++) { #ifndef SIXTY_FOUR_BIT w>>=BN_BITS4; w>>=BN_BITS4; #else w=0; #endif a->d[i]=(BN_ULONG)w&BN_MASK2; if (a->d[i] != 0) a->top=i+1; } return(1); }
['static int RSA_eay_mod_exp(BIGNUM *r0, BIGNUM *I, RSA *rsa)\n\t{\n\tBIGNUM r1,m1;\n\tint ret=0;\n\tBN_CTX *ctx;\n\tif ((ctx=BN_CTX_new()) == NULL) goto err;\n\tBN_init(&m1);\n\tBN_init(&r1);\n\tif (rsa->flags & RSA_FLAG_CACHE_PRIVATE)\n\t\t{\n\t\tif (rsa->_method_mod_p == NULL)\n\t\t\t{\n\t\t\tif ((rsa->_method_mod_p=BN_MONT_CTX_new()) != NULL)\n\t\t\t\tif (!BN_MONT_CTX_set(rsa->_method_mod_p,rsa->p,\n\t\t\t\t\t\t ctx))\n\t\t\t\t\tgoto err;\n\t\t\t}\n\t\tif (rsa->_method_mod_q == NULL)\n\t\t\t{\n\t\t\tif ((rsa->_method_mod_q=BN_MONT_CTX_new()) != NULL)\n\t\t\t\tif (!BN_MONT_CTX_set(rsa->_method_mod_q,rsa->q,\n\t\t\t\t\t\t ctx))\n\t\t\t\t\tgoto err;\n\t\t\t}\n\t\t}\n\tif (!BN_mod(&r1,I,rsa->q,ctx)) goto err;\n\tif (!rsa->meth->bn_mod_exp(&m1,&r1,rsa->dmq1,rsa->q,ctx,\n\t\trsa->_method_mod_q)) goto err;\n\tif (!BN_mod(&r1,I,rsa->p,ctx)) goto err;\n\tif (!rsa->meth->bn_mod_exp(r0,&r1,rsa->dmp1,rsa->p,ctx,\n\t\trsa->_method_mod_p)) goto err;\n\tif (!BN_sub(r0,r0,&m1)) goto err;\n\tif (r0->neg)\n\t\tif (!BN_add(r0,r0,rsa->p)) goto err;\n\tif (!BN_mul(&r1,r0,rsa->iqmp,ctx)) goto err;\n\tif (!BN_mod(r0,&r1,rsa->p,ctx)) goto err;\n\tif (r0->neg)\n\t\tif (!BN_add(r0,r0,rsa->p)) goto err;\n\tif (!BN_mul(&r1,r0,rsa->q,ctx)) goto err;\n\tif (!BN_add(r0,&r1,&m1)) goto err;\n\tret=1;\nerr:\n\tBN_clear_free(&m1);\n\tBN_clear_free(&r1);\n\tBN_CTX_free(ctx);\n\treturn(ret);\n\t}', 'int BN_mod(BIGNUM *rem, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n\t{\n#if 0\n\tint i,nm,nd;\n\tBIGNUM *dv;\n\tif (BN_ucmp(m,d) < 0)\n\t\treturn((BN_copy(rem,m) == NULL)?0:1);\n\tdv= &(ctx->bn[ctx->tos]);\n\tif (!BN_copy(rem,m)) return(0);\n\tnm=BN_num_bits(rem);\n\tnd=BN_num_bits(d);\n\tif (!BN_lshift(dv,d,nm-nd)) return(0);\n\tfor (i=nm-nd; i>=0; i--)\n\t\t{\n\t\tif (BN_cmp(rem,dv) >= 0)\n\t\t\t{\n\t\t\tif (!BN_sub(rem,rem,dv)) return(0);\n\t\t\t}\n\t\tif (!BN_rshift1(dv,dv)) return(0);\n\t\t}\n\treturn(1);\n#else\n\treturn(BN_div(NULL,rem,m,d,ctx));\n#endif\n\t}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n\t BN_CTX *ctx)\n\t{\n\tint norm_shift,i,j,loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tint num_n,div_n;\n\tbn_check_top(num);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\n\t\t}\n\tif (BN_ucmp(num,divisor) < 0)\n\t\t{\n\t\tif (rm != NULL)\n\t\t\t{ if (BN_copy(rm,num) == NULL) return(0); }\n\t\tif (dv != NULL) BN_zero(dv);\n\t\treturn(1);\n\t\t}\n\ttmp= &(ctx->bn[ctx->tos]);\n\ttmp->neg=0;\n\tsnum= &(ctx->bn[ctx->tos+1]);\n\tsdiv= &(ctx->bn[ctx->tos+2]);\n\tif (dv == NULL)\n\t\tres= &(ctx->bn[ctx->tos+3]);\n\telse\tres=dv;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tBN_lshift(sdiv,divisor,norm_shift);\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tBN_lshift(snum,num,norm_shift);\n\tsnum->neg=0;\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\tBN_init(&wnum);\n\twnum.d=\t &(snum->d[loop]);\n\twnum.top= div_n;\n\twnum.max= snum->max+1;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (BN_ucmp(&wnum,sdiv) >= 0)\n\t\t{\n\t\tif (!BN_usub(&wnum,&wnum,sdiv)) goto err;\n\t\t*resp=1;\n\t\tres->d[res->top-1]=1;\n\t\t}\n\telse\n\t\tres->top--;\n\tresp--;\n\tfor (i=0; i<loop-1; i++)\n\t\t{\n\t\tBN_ULONG q,n0,n1;\n\t\tBN_ULONG l0;\n\t\twnum.d--; wnum.top++;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\tq=bn_div_words(n0,n1,d0);\n\t\t{\n#ifdef BN_LLONG\n\t\tBN_ULLONG t1,t2,rem;\n\t\tt1=((BN_ULLONG)n0<<BN_BITS2)|n1;\n\t\tfor (;;)\n\t\t\t{\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\trem=t1-(BN_ULLONG)q*d0;\n\t\t\tif ((rem>>BN_BITS2) ||\n\t\t\t\t(t2 <= ((BN_ULLONG)(rem<<BN_BITS2)+wnump[-2])))\n\t\t\t\tbreak;\n\t\t\tq--;\n\t\t\t}\n#else\n\t\tBN_ULONG t1l,t1h,t2l,t2h,t3l,t3h,ql,qh,t3t;\n\t\tt1h=n0;\n\t\tt1l=n1;\n\t\tfor (;;)\n\t\t\t{\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n\t\t\tt3t=LBITS(d0); t3h=HBITS(d0);\n\t\t\tmul64(t3t,t3h,ql,qh);\n\t\t\tt3l=(t1l-t3t)&BN_MASK2;\n\t\t\tif (t3l > t1l) t3h++;\n\t\t\tt3h=(t1h-t3h)&BN_MASK2;\n\t\t\tif (t3h) break;\n\t\t\tif (t2h < t3l) break;\n\t\t\tif ((t2h == t3l) && (t2l <= wnump[-2])) break;\n\t\t\tq--;\n\t\t\t}\n#endif\n\t\t}\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\tfor (j=div_n+1; j>0; j--)\n\t\t\tif (tmp->d[j-1]) break;\n\t\ttmp->top=j;\n\t\tj=wnum.top;\n\t\tBN_sub(&wnum,&wnum,tmp);\n\t\tsnum->top=snum->top+wnum.top-j;\n\t\tif (wnum.neg)\n\t\t\t{\n\t\t\tq--;\n\t\t\tj=wnum.top;\n\t\t\tBN_add(&wnum,&wnum,sdiv);\n\t\t\tsnum->top+=wnum.top-j;\n\t\t\t}\n\t\t*(resp--)=q;\n\t\twnump--;\n\t\t}\n\tif (rm != NULL)\n\t\t{\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\trm->neg=num->neg;\n\t\t}\n\treturn(1);\nerr:\n\treturn(0);\n\t}', 'int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)\n\t{\n\tint top,al,bl;\n\tBIGNUM *rr;\n#ifdef BN_RECURSION\n\tBIGNUM *t;\n\tint i,j,k;\n#endif\n#ifdef BN_COUNT\nprintf("BN_mul %d * %d\\n",a->top,b->top);\n#endif\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tbn_check_top(r);\n\tal=a->top;\n\tbl=b->top;\n\tr->neg=a->neg^b->neg;\n\tif ((al == 0) || (bl == 0))\n\t\t{\n\t\tBN_zero(r);\n\t\treturn(1);\n\t\t}\n\ttop=al+bl;\n\tif ((r == a) || (r == b))\n\t\trr= &(ctx->bn[ctx->tos+1]);\n\telse\n\t\trr=r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\tif (al == bl)\n\t\t{\n# ifdef BN_MUL_COMBA\n if (al == 8)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,16) == NULL) return(0);\n\t\t\tr->top=16;\n\t\t\tbn_mul_comba8(rr->d,a->d,b->d);\n\t\t\tgoto end;\n\t\t\t}\n\t\telse\n# endif\n#ifdef BN_RECURSION\n\t\tif (al < BN_MULL_SIZE_NORMAL)\n#endif\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,top) == NULL) return(0);\n\t\t\trr->top=top;\n\t\t\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n\t\t\tgoto end;\n\t\t\t}\n# ifdef BN_RECURSION\n\t\tgoto symetric;\n# endif\n\t\t}\n#endif\n#ifdef BN_RECURSION\n\telse if ((al < BN_MULL_SIZE_NORMAL) || (bl < BN_MULL_SIZE_NORMAL))\n\t\t{\n\t\tif (bn_wexpand(rr,top) == NULL) return(0);\n\t\trr->top=top;\n\t\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n\t\tgoto end;\n\t\t}\n\telse\n\t\t{\n\t\ti=(al-bl);\n\t\tif ((i == 1) && !BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tbn_wexpand(b,al);\n\t\t\tb->d[bl]=0;\n\t\t\tbl++;\n\t\t\tgoto symetric;\n\t\t\t}\n\t\telse if ((i == -1) && !BN_get_flags(a,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tbn_wexpand(a,bl);\n\t\t\ta->d[al]=0;\n\t\t\tal++;\n\t\t\tgoto symetric;\n\t\t\t}\n\t\t}\n#endif\n\tif (bn_wexpand(rr,top) == NULL) return(0);\n\trr->top=top;\n\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n#ifdef BN_RECURSION\n\tif (0)\n\t\t{\nsymetric:\n\t\tj=BN_num_bits_word((BN_ULONG)al);\n\t\tj=1<<(j-1);\n\t\tk=j+j;\n\t\tt= &(ctx->bn[ctx->tos]);\n\t\tif (al == j)\n\t\t\t{\n\t\t\tbn_wexpand(t,k*2);\n\t\t\tbn_wexpand(rr,k*2);\n\t\t\tbn_mul_recursive(rr->d,a->d,b->d,al,t->d);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tbn_wexpand(a,k);\n\t\t\tbn_wexpand(b,k);\n\t\t\tbn_wexpand(t,k*4);\n\t\t\tbn_wexpand(rr,k*4);\n\t\t\tfor (i=a->top; i<k; i++)\n\t\t\t\ta->d[i]=0;\n\t\t\tfor (i=b->top; i<k; i++)\n\t\t\t\tb->d[i]=0;\n\t\t\tbn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d);\n\t\t\t}\n\t\trr->top=top;\n\t\t}\n#endif\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\nend:\n#endif\n\tbn_fix_top(rr);\n\tif (r != rr) BN_copy(r,rr);\n\treturn(1);\n\t}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n\t{\n\tint i,n;\n\tif (bn_expand(a,sizeof(BN_ULONG)*8) == NULL) return(0);\n\tn=sizeof(BN_ULONG)/BN_BYTES;\n\ta->neg=0;\n\ta->top=0;\n\ta->d[0]=(BN_ULONG)w&BN_MASK2;\n\tif (a->d[0] != 0) a->top=1;\n\tfor (i=1; i<n; i++)\n\t\t{\n#ifndef SIXTY_FOUR_BIT\n\t\tw>>=BN_BITS4;\n\t\tw>>=BN_BITS4;\n#else\n\t\tw=0;\n#endif\n\t\ta->d[i]=(BN_ULONG)w&BN_MASK2;\n\t\tif (a->d[i] != 0) a->top=i+1;\n\t\t}\n\treturn(1);\n\t}']
2,315
0
https://github.com/openssl/openssl/blob/646d56956bd14c71964389f28d8baf3ba4f13723/crypto/bn/bn_asm.c/#L406
BN_ULONG bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n) { BN_ULONG t1,t2; int c=0; assert(n >= 0); if (n <= 0) return((BN_ULONG)0); for (;;) { t1=a[0]; t2=b[0]; r[0]=(t1-t2-c)&BN_MASK2; if (t1 != t2) c=(t1 < t2); if (--n <= 0) break; t1=a[1]; t2=b[1]; r[1]=(t1-t2-c)&BN_MASK2; if (t1 != t2) c=(t1 < t2); if (--n <= 0) break; t1=a[2]; t2=b[2]; r[2]=(t1-t2-c)&BN_MASK2; if (t1 != t2) c=(t1 < t2); if (--n <= 0) break; t1=a[3]; t2=b[3]; r[3]=(t1-t2-c)&BN_MASK2; if (t1 != t2) c=(t1 < t2); if (--n <= 0) break; a+=4; b+=4; r+=4; } return(c); }
['void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,\n\t BN_ULONG *t)\n\t{\n\tint n=n2/2,c1,c2;\n\tunsigned int neg,zero;\n\tBN_ULONG ln,lo,*p;\n# ifdef BN_COUNT\n\tfprintf(stderr," bn_mul_recursive %d * %d\\n",n2,n2);\n# endif\n# ifdef BN_MUL_COMBA\n# if 0\n\tif (n2 == 4)\n\t\t{\n\t\tbn_mul_comba4(r,a,b);\n\t\treturn;\n\t\t}\n# endif\n\tif (n2 == 8)\n\t\t{\n\t\tbn_mul_comba8(r,a,b);\n\t\treturn;\n\t\t}\n# endif\n\tif (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL)\n\t\t{\n\t\tbn_mul_normal(r,a,n2,b,n2);\n\t\treturn;\n\t\t}\n\tc1=bn_cmp_words(a,&(a[n]),n);\n\tc2=bn_cmp_words(&(b[n]),b,n);\n\tzero=neg=0;\n\tswitch (c1*3+c2)\n\t\t{\n\tcase -4:\n\t\tbn_sub_words(t, &(a[n]),a, n);\n\t\tbn_sub_words(&(t[n]),b, &(b[n]),n);\n\t\tbreak;\n\tcase -3:\n\t\tzero=1;\n\t\tbreak;\n\tcase -2:\n\t\tbn_sub_words(t, &(a[n]),a, n);\n\t\tbn_sub_words(&(t[n]),&(b[n]),b, n);\n\t\tneg=1;\n\t\tbreak;\n\tcase -1:\n\tcase 0:\n\tcase 1:\n\t\tzero=1;\n\t\tbreak;\n\tcase 2:\n\t\tbn_sub_words(t, a, &(a[n]),n);\n\t\tbn_sub_words(&(t[n]),b, &(b[n]),n);\n\t\tneg=1;\n\t\tbreak;\n\tcase 3:\n\t\tzero=1;\n\t\tbreak;\n\tcase 4:\n\t\tbn_sub_words(t, a, &(a[n]),n);\n\t\tbn_sub_words(&(t[n]),&(b[n]),b, n);\n\t\tbreak;\n\t\t}\n# ifdef BN_MUL_COMBA\n\tif (n == 4)\n\t\t{\n\t\tif (!zero)\n\t\t\tbn_mul_comba4(&(t[n2]),t,&(t[n]));\n\t\telse\n\t\t\tmemset(&(t[n2]),0,8*sizeof(BN_ULONG));\n\t\tbn_mul_comba4(r,a,b);\n\t\tbn_mul_comba4(&(r[n2]),&(a[n]),&(b[n]));\n\t\t}\n\telse if (n == 8)\n\t\t{\n\t\tif (!zero)\n\t\t\tbn_mul_comba8(&(t[n2]),t,&(t[n]));\n\t\telse\n\t\t\tmemset(&(t[n2]),0,16*sizeof(BN_ULONG));\n\t\tbn_mul_comba8(r,a,b);\n\t\tbn_mul_comba8(&(r[n2]),&(a[n]),&(b[n]));\n\t\t}\n\telse\n# endif\n\t\t{\n\t\tp= &(t[n2*2]);\n\t\tif (!zero)\n\t\t\tbn_mul_recursive(&(t[n2]),t,&(t[n]),n,p);\n\t\telse\n\t\t\tmemset(&(t[n2]),0,n2*sizeof(BN_ULONG));\n\t\tbn_mul_recursive(r,a,b,n,p);\n\t\tbn_mul_recursive(&(r[n2]),&(a[n]),&(b[n]),n,p);\n\t\t}\n\tc1=(int)(bn_add_words(t,r,&(r[n2]),n2));\n\tif (neg)\n\t\t{\n\t\tc1-=(int)(bn_sub_words(&(t[n2]),t,&(t[n2]),n2));\n\t\t}\n\telse\n\t\t{\n\t\tc1+=(int)(bn_add_words(&(t[n2]),&(t[n2]),t,n2));\n\t\t}\n\tc1+=(int)(bn_add_words(&(r[n]),&(r[n]),&(t[n2]),n2));\n\tif (c1)\n\t\t{\n\t\tp= &(r[n+n2]);\n\t\tlo= *p;\n\t\tln=(lo+c1)&BN_MASK2;\n\t\t*p=ln;\n\t\tif (ln < (BN_ULONG)c1)\n\t\t\t{\n\t\t\tdo\t{\n\t\t\t\tp++;\n\t\t\t\tlo= *p;\n\t\t\t\tln=(lo+1)&BN_MASK2;\n\t\t\t\t*p=ln;\n\t\t\t\t} while (ln == 0);\n\t\t\t}\n\t\t}\n\t}', 'BN_ULONG bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)\n {\n\tBN_ULONG c,l,t;\n\tassert(n >= 0);\n\tif (n <= 0) return((BN_ULONG)0);\n\tc=0;\n\tfor (;;)\n\t\t{\n\t\tt=a[0];\n\t\tt=(t+c)&BN_MASK2;\n\t\tc=(t < c);\n\t\tl=(t+b[0])&BN_MASK2;\n\t\tc+=(l < t);\n\t\tr[0]=l;\n\t\tif (--n <= 0) break;\n\t\tt=a[1];\n\t\tt=(t+c)&BN_MASK2;\n\t\tc=(t < c);\n\t\tl=(t+b[1])&BN_MASK2;\n\t\tc+=(l < t);\n\t\tr[1]=l;\n\t\tif (--n <= 0) break;\n\t\tt=a[2];\n\t\tt=(t+c)&BN_MASK2;\n\t\tc=(t < c);\n\t\tl=(t+b[2])&BN_MASK2;\n\t\tc+=(l < t);\n\t\tr[2]=l;\n\t\tif (--n <= 0) break;\n\t\tt=a[3];\n\t\tt=(t+c)&BN_MASK2;\n\t\tc=(t < c);\n\t\tl=(t+b[3])&BN_MASK2;\n\t\tc+=(l < t);\n\t\tr[3]=l;\n\t\tif (--n <= 0) break;\n\t\ta+=4;\n\t\tb+=4;\n\t\tr+=4;\n\t\t}\n\treturn((BN_ULONG)c);\n\t}', 'BN_ULONG bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)\n {\n\tBN_ULONG t1,t2;\n\tint c=0;\n\tassert(n >= 0);\n\tif (n <= 0) return((BN_ULONG)0);\n\tfor (;;)\n\t\t{\n\t\tt1=a[0]; t2=b[0];\n\t\tr[0]=(t1-t2-c)&BN_MASK2;\n\t\tif (t1 != t2) c=(t1 < t2);\n\t\tif (--n <= 0) break;\n\t\tt1=a[1]; t2=b[1];\n\t\tr[1]=(t1-t2-c)&BN_MASK2;\n\t\tif (t1 != t2) c=(t1 < t2);\n\t\tif (--n <= 0) break;\n\t\tt1=a[2]; t2=b[2];\n\t\tr[2]=(t1-t2-c)&BN_MASK2;\n\t\tif (t1 != t2) c=(t1 < t2);\n\t\tif (--n <= 0) break;\n\t\tt1=a[3]; t2=b[3];\n\t\tr[3]=(t1-t2-c)&BN_MASK2;\n\t\tif (t1 != t2) c=(t1 < t2);\n\t\tif (--n <= 0) break;\n\t\ta+=4;\n\t\tb+=4;\n\t\tr+=4;\n\t\t}\n\treturn(c);\n\t}']
2,316
0
https://github.com/libav/libav/blob/a6d1bd05c906fc7ad34fae1029a45ec3cbcc4fcc/ffserver.c/#L3946
static void load_module(const char *filename) { void *dll; void (*init_func)(void); dll = dlopen(filename, RTLD_NOW); if (!dll) { fprintf(stderr, "Could not load module '%s' - %s\n", filename, dlerror()); return; } init_func = dlsym(dll, "ffserver_module_init"); if (!init_func) { fprintf(stderr, "%s: init function 'ffserver_module_init()' not found\n", filename); dlclose(dll); } init_func(); }
['static void load_module(const char *filename)\n{\n void *dll;\n void (*init_func)(void);\n dll = dlopen(filename, RTLD_NOW);\n if (!dll) {\n fprintf(stderr, "Could not load module \'%s\' - %s\\n",\n filename, dlerror());\n return;\n }\n init_func = dlsym(dll, "ffserver_module_init");\n if (!init_func) {\n fprintf(stderr,\n "%s: init function \'ffserver_module_init()\' not found\\n",\n filename);\n dlclose(dll);\n }\n init_func();\n}']
2,317
0
https://github.com/libav/libav/blob/046f081b46c8479820409cf8f530b988221bd15b/ffmpeg.c/#L2815
static int opt_metadata(const char *opt, const char *arg) { char *mid= strchr(arg, '='); if(!mid){ fprintf(stderr, "Missing =\n"); ffmpeg_exit(1); } *mid++= 0; av_metadata_set2(&metadata, arg, mid, 0); return 0; }
['static int opt_metadata(const char *opt, const char *arg)\n{\n char *mid= strchr(arg, \'=\');\n if(!mid){\n fprintf(stderr, "Missing =\\n");\n ffmpeg_exit(1);\n }\n *mid++= 0;\n av_metadata_set2(&metadata, arg, mid, 0);\n return 0;\n}']
2,318
0
https://github.com/libav/libav/blob/3a26ccbf0d9f806d067e76a3f484170abecb36b3/libavcodec/h264_parser.c/#L221
static inline int parse_nal_units(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t *buf, int buf_size) { H264Context *h = s->priv_data; const uint8_t *buf_end = buf + buf_size; unsigned int pps_id; unsigned int slice_type; int state = -1, got_reset = 0; const uint8_t *ptr; int field_poc[2]; s->pict_type = AV_PICTURE_TYPE_I; s->key_frame = 0; s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN; h->avctx = avctx; ff_h264_reset_sei(h); if (!buf_size) return 0; for (;;) { int src_length, dst_length, consumed; buf = avpriv_find_start_code(buf, buf_end, &state); if (buf >= buf_end) break; --buf; src_length = buf_end - buf; switch (state & 0x1f) { case NAL_SLICE: case NAL_IDR_SLICE: if ((state & 0x1f) == NAL_IDR_SLICE || ((state >> 5) & 0x3) == 0) { if (src_length > 60) src_length = 60; } else { if (src_length > 1000) src_length = 1000; } break; } ptr = ff_h264_decode_nal(h, buf, &dst_length, &consumed, src_length); if (ptr == NULL || dst_length < 0) break; init_get_bits(&h->gb, ptr, 8 * dst_length); switch (h->nal_unit_type) { case NAL_SPS: ff_h264_decode_seq_parameter_set(h); break; case NAL_PPS: ff_h264_decode_picture_parameter_set(h, h->gb.size_in_bits); break; case NAL_SEI: ff_h264_decode_sei(h); break; case NAL_IDR_SLICE: s->key_frame = 1; h->prev_frame_num = 0; h->prev_frame_num_offset = 0; h->prev_poc_msb = h->prev_poc_lsb = 0; case NAL_SLICE: get_ue_golomb(&h->gb); slice_type = get_ue_golomb_31(&h->gb); s->pict_type = golomb_to_pict_type[slice_type % 5]; if (h->sei_recovery_frame_cnt >= 0) { s->key_frame = 1; } pps_id = get_ue_golomb(&h->gb); if (pps_id >= MAX_PPS_COUNT) { av_log(h->avctx, AV_LOG_ERROR, "pps_id %u out of range\n", pps_id); return -1; } if (!h->pps_buffers[pps_id]) { av_log(h->avctx, AV_LOG_ERROR, "non-existing PPS %u referenced\n", pps_id); return -1; } h->pps = *h->pps_buffers[pps_id]; if (!h->sps_buffers[h->pps.sps_id]) { av_log(h->avctx, AV_LOG_ERROR, "non-existing SPS %u referenced\n", h->pps.sps_id); return -1; } h->sps = *h->sps_buffers[h->pps.sps_id]; h->frame_num = get_bits(&h->gb, h->sps.log2_max_frame_num); avctx->profile = ff_h264_get_profile(&h->sps); avctx->level = h->sps.level_idc; if (h->sps.frame_mbs_only_flag) { h->picture_structure = PICT_FRAME; } else { if (get_bits1(&h->gb)) { h->picture_structure = PICT_TOP_FIELD + get_bits1(&h->gb); } else { h->picture_structure = PICT_FRAME; } } if (h->nal_unit_type == NAL_IDR_SLICE) get_ue_golomb(&h->gb); if (h->sps.poc_type == 0) { h->poc_lsb = get_bits(&h->gb, h->sps.log2_max_poc_lsb); if (h->pps.pic_order_present == 1 && h->picture_structure == PICT_FRAME) h->delta_poc_bottom = get_se_golomb(&h->gb); } if (h->sps.poc_type == 1 && !h->sps.delta_pic_order_always_zero_flag) { h->delta_poc[0] = get_se_golomb(&h->gb); if (h->pps.pic_order_present == 1 && h->picture_structure == PICT_FRAME) h->delta_poc[1] = get_se_golomb(&h->gb); } field_poc[0] = field_poc[1] = INT_MAX; ff_init_poc(h, field_poc, &s->output_picture_number); if (h->nal_ref_idc && h->nal_unit_type != NAL_IDR_SLICE) { got_reset = scan_mmco_reset(s); if (got_reset < 0) return got_reset; } h->prev_frame_num = got_reset ? 0 : h->frame_num; h->prev_frame_num_offset = got_reset ? 0 : h->frame_num_offset; if (h->nal_ref_idc != 0) { if (!got_reset) { h->prev_poc_msb = h->poc_msb; h->prev_poc_lsb = h->poc_lsb; } else { h->prev_poc_msb = 0; h->prev_poc_lsb = h->picture_structure == PICT_BOTTOM_FIELD ? 0 : field_poc[0]; } } if (h->sps.pic_struct_present_flag) { switch (h->sei_pic_struct) { case SEI_PIC_STRUCT_TOP_FIELD: case SEI_PIC_STRUCT_BOTTOM_FIELD: s->repeat_pict = 0; break; case SEI_PIC_STRUCT_FRAME: case SEI_PIC_STRUCT_TOP_BOTTOM: case SEI_PIC_STRUCT_BOTTOM_TOP: s->repeat_pict = 1; break; case SEI_PIC_STRUCT_TOP_BOTTOM_TOP: case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM: s->repeat_pict = 2; break; case SEI_PIC_STRUCT_FRAME_DOUBLING: s->repeat_pict = 3; break; case SEI_PIC_STRUCT_FRAME_TRIPLING: s->repeat_pict = 5; break; default: s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0; break; } } else { s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0; } if (h->picture_structure == PICT_FRAME) { s->picture_structure = AV_PICTURE_STRUCTURE_FRAME; if (h->sps.pic_struct_present_flag) { switch (h->sei_pic_struct) { case SEI_PIC_STRUCT_TOP_BOTTOM: case SEI_PIC_STRUCT_TOP_BOTTOM_TOP: s->field_order = AV_FIELD_TT; break; case SEI_PIC_STRUCT_BOTTOM_TOP: case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM: s->field_order = AV_FIELD_BB; break; default: s->field_order = AV_FIELD_PROGRESSIVE; break; } } else { if (field_poc[0] < field_poc[1]) s->field_order = AV_FIELD_TT; else if (field_poc[0] > field_poc[1]) s->field_order = AV_FIELD_BB; else s->field_order = AV_FIELD_PROGRESSIVE; } } else { if (h->picture_structure == PICT_TOP_FIELD) s->picture_structure = AV_PICTURE_STRUCTURE_TOP_FIELD; else s->picture_structure = AV_PICTURE_STRUCTURE_BOTTOM_FIELD; s->field_order = AV_FIELD_UNKNOWN; } return 0; } buf += consumed; } av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit\n"); return -1; }
['static inline int parse_nal_units(AVCodecParserContext *s,\n AVCodecContext *avctx,\n const uint8_t *buf, int buf_size)\n{\n H264Context *h = s->priv_data;\n const uint8_t *buf_end = buf + buf_size;\n unsigned int pps_id;\n unsigned int slice_type;\n int state = -1, got_reset = 0;\n const uint8_t *ptr;\n int field_poc[2];\n s->pict_type = AV_PICTURE_TYPE_I;\n s->key_frame = 0;\n s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;\n h->avctx = avctx;\n ff_h264_reset_sei(h);\n if (!buf_size)\n return 0;\n for (;;) {\n int src_length, dst_length, consumed;\n buf = avpriv_find_start_code(buf, buf_end, &state);\n if (buf >= buf_end)\n break;\n --buf;\n src_length = buf_end - buf;\n switch (state & 0x1f) {\n case NAL_SLICE:\n case NAL_IDR_SLICE:\n if ((state & 0x1f) == NAL_IDR_SLICE || ((state >> 5) & 0x3) == 0) {\n if (src_length > 60)\n src_length = 60;\n } else {\n if (src_length > 1000)\n src_length = 1000;\n }\n break;\n }\n ptr = ff_h264_decode_nal(h, buf, &dst_length, &consumed, src_length);\n if (ptr == NULL || dst_length < 0)\n break;\n init_get_bits(&h->gb, ptr, 8 * dst_length);\n switch (h->nal_unit_type) {\n case NAL_SPS:\n ff_h264_decode_seq_parameter_set(h);\n break;\n case NAL_PPS:\n ff_h264_decode_picture_parameter_set(h, h->gb.size_in_bits);\n break;\n case NAL_SEI:\n ff_h264_decode_sei(h);\n break;\n case NAL_IDR_SLICE:\n s->key_frame = 1;\n h->prev_frame_num = 0;\n h->prev_frame_num_offset = 0;\n h->prev_poc_msb =\n h->prev_poc_lsb = 0;\n case NAL_SLICE:\n get_ue_golomb(&h->gb);\n slice_type = get_ue_golomb_31(&h->gb);\n s->pict_type = golomb_to_pict_type[slice_type % 5];\n if (h->sei_recovery_frame_cnt >= 0) {\n s->key_frame = 1;\n }\n pps_id = get_ue_golomb(&h->gb);\n if (pps_id >= MAX_PPS_COUNT) {\n av_log(h->avctx, AV_LOG_ERROR,\n "pps_id %u out of range\\n", pps_id);\n return -1;\n }\n if (!h->pps_buffers[pps_id]) {\n av_log(h->avctx, AV_LOG_ERROR,\n "non-existing PPS %u referenced\\n", pps_id);\n return -1;\n }\n h->pps = *h->pps_buffers[pps_id];\n if (!h->sps_buffers[h->pps.sps_id]) {\n av_log(h->avctx, AV_LOG_ERROR,\n "non-existing SPS %u referenced\\n", h->pps.sps_id);\n return -1;\n }\n h->sps = *h->sps_buffers[h->pps.sps_id];\n h->frame_num = get_bits(&h->gb, h->sps.log2_max_frame_num);\n avctx->profile = ff_h264_get_profile(&h->sps);\n avctx->level = h->sps.level_idc;\n if (h->sps.frame_mbs_only_flag) {\n h->picture_structure = PICT_FRAME;\n } else {\n if (get_bits1(&h->gb)) {\n h->picture_structure = PICT_TOP_FIELD + get_bits1(&h->gb);\n } else {\n h->picture_structure = PICT_FRAME;\n }\n }\n if (h->nal_unit_type == NAL_IDR_SLICE)\n get_ue_golomb(&h->gb);\n if (h->sps.poc_type == 0) {\n h->poc_lsb = get_bits(&h->gb, h->sps.log2_max_poc_lsb);\n if (h->pps.pic_order_present == 1 &&\n h->picture_structure == PICT_FRAME)\n h->delta_poc_bottom = get_se_golomb(&h->gb);\n }\n if (h->sps.poc_type == 1 &&\n !h->sps.delta_pic_order_always_zero_flag) {\n h->delta_poc[0] = get_se_golomb(&h->gb);\n if (h->pps.pic_order_present == 1 &&\n h->picture_structure == PICT_FRAME)\n h->delta_poc[1] = get_se_golomb(&h->gb);\n }\n field_poc[0] = field_poc[1] = INT_MAX;\n ff_init_poc(h, field_poc, &s->output_picture_number);\n if (h->nal_ref_idc && h->nal_unit_type != NAL_IDR_SLICE) {\n got_reset = scan_mmco_reset(s);\n if (got_reset < 0)\n return got_reset;\n }\n h->prev_frame_num = got_reset ? 0 : h->frame_num;\n h->prev_frame_num_offset = got_reset ? 0 : h->frame_num_offset;\n if (h->nal_ref_idc != 0) {\n if (!got_reset) {\n h->prev_poc_msb = h->poc_msb;\n h->prev_poc_lsb = h->poc_lsb;\n } else {\n h->prev_poc_msb = 0;\n h->prev_poc_lsb =\n h->picture_structure == PICT_BOTTOM_FIELD ? 0 : field_poc[0];\n }\n }\n if (h->sps.pic_struct_present_flag) {\n switch (h->sei_pic_struct) {\n case SEI_PIC_STRUCT_TOP_FIELD:\n case SEI_PIC_STRUCT_BOTTOM_FIELD:\n s->repeat_pict = 0;\n break;\n case SEI_PIC_STRUCT_FRAME:\n case SEI_PIC_STRUCT_TOP_BOTTOM:\n case SEI_PIC_STRUCT_BOTTOM_TOP:\n s->repeat_pict = 1;\n break;\n case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:\n case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:\n s->repeat_pict = 2;\n break;\n case SEI_PIC_STRUCT_FRAME_DOUBLING:\n s->repeat_pict = 3;\n break;\n case SEI_PIC_STRUCT_FRAME_TRIPLING:\n s->repeat_pict = 5;\n break;\n default:\n s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;\n break;\n }\n } else {\n s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;\n }\n if (h->picture_structure == PICT_FRAME) {\n s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;\n if (h->sps.pic_struct_present_flag) {\n switch (h->sei_pic_struct) {\n case SEI_PIC_STRUCT_TOP_BOTTOM:\n case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:\n s->field_order = AV_FIELD_TT;\n break;\n case SEI_PIC_STRUCT_BOTTOM_TOP:\n case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:\n s->field_order = AV_FIELD_BB;\n break;\n default:\n s->field_order = AV_FIELD_PROGRESSIVE;\n break;\n }\n } else {\n if (field_poc[0] < field_poc[1])\n s->field_order = AV_FIELD_TT;\n else if (field_poc[0] > field_poc[1])\n s->field_order = AV_FIELD_BB;\n else\n s->field_order = AV_FIELD_PROGRESSIVE;\n }\n } else {\n if (h->picture_structure == PICT_TOP_FIELD)\n s->picture_structure = AV_PICTURE_STRUCTURE_TOP_FIELD;\n else\n s->picture_structure = AV_PICTURE_STRUCTURE_BOTTOM_FIELD;\n s->field_order = AV_FIELD_UNKNOWN;\n }\n return 0;\n }\n buf += consumed;\n }\n av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit\\n");\n return -1;\n}', 'void ff_h264_reset_sei(H264Context *h)\n{\n h->sei_recovery_frame_cnt = -1;\n h->sei_dpb_output_delay = 0;\n h->sei_cpb_removal_delay = -1;\n h->sei_buffering_period_present = 0;\n h->sei_frame_packing_present = 0;\n}', 'static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,\n int bit_size)\n{\n int buffer_size;\n int ret = 0;\n if (bit_size > INT_MAX - 7 || bit_size < 0 || !buffer) {\n 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}', 'int ff_h264_decode_seq_parameter_set(H264Context *h)\n{\n int profile_idc, level_idc, constraint_set_flags = 0;\n unsigned int sps_id;\n int i, log2_max_frame_num_minus4;\n SPS *sps;\n profile_idc = get_bits(&h->gb, 8);\n constraint_set_flags |= get_bits1(&h->gb) << 0;\n constraint_set_flags |= get_bits1(&h->gb) << 1;\n constraint_set_flags |= get_bits1(&h->gb) << 2;\n constraint_set_flags |= get_bits1(&h->gb) << 3;\n get_bits(&h->gb, 4);\n level_idc = get_bits(&h->gb, 8);\n sps_id = get_ue_golomb_31(&h->gb);\n if (sps_id >= MAX_SPS_COUNT) {\n av_log(h->avctx, AV_LOG_ERROR, "sps_id %u out of range\\n", sps_id);\n return AVERROR_INVALIDDATA;\n }\n sps = av_mallocz(sizeof(SPS));\n if (!sps)\n return AVERROR(ENOMEM);\n sps->sps_id = sps_id;\n sps->time_offset_length = 24;\n sps->profile_idc = profile_idc;\n sps->constraint_set_flags = constraint_set_flags;\n sps->level_idc = level_idc;\n memset(sps->scaling_matrix4, 16, sizeof(sps->scaling_matrix4));\n memset(sps->scaling_matrix8, 16, sizeof(sps->scaling_matrix8));\n sps->scaling_matrix_present = 0;\n if (sps->profile_idc == 100 || sps->profile_idc == 110 ||\n sps->profile_idc == 122 || sps->profile_idc == 244 ||\n sps->profile_idc == 44 || sps->profile_idc == 83 ||\n sps->profile_idc == 86 || sps->profile_idc == 118 ||\n sps->profile_idc == 128 || sps->profile_idc == 144) {\n sps->chroma_format_idc = get_ue_golomb_31(&h->gb);\n if (sps->chroma_format_idc > 3) {\n av_log(h->avctx, AV_LOG_ERROR,\n "chroma_format_idc %u out of range\\n",\n sps->chroma_format_idc);\n goto fail;\n } else if (sps->chroma_format_idc == 3) {\n sps->residual_color_transform_flag = get_bits1(&h->gb);\n }\n sps->bit_depth_luma = get_ue_golomb(&h->gb) + 8;\n sps->bit_depth_chroma = get_ue_golomb(&h->gb) + 8;\n if (sps->bit_depth_chroma != sps->bit_depth_luma) {\n avpriv_request_sample(h->avctx,\n "Different chroma and luma bit depth");\n goto fail;\n }\n sps->transform_bypass = get_bits1(&h->gb);\n decode_scaling_matrices(h, sps, NULL, 1,\n sps->scaling_matrix4, sps->scaling_matrix8);\n } else {\n sps->chroma_format_idc = 1;\n sps->bit_depth_luma = 8;\n sps->bit_depth_chroma = 8;\n }\n log2_max_frame_num_minus4 = get_ue_golomb(&h->gb);\n if (log2_max_frame_num_minus4 < MIN_LOG2_MAX_FRAME_NUM - 4 ||\n log2_max_frame_num_minus4 > MAX_LOG2_MAX_FRAME_NUM - 4) {\n av_log(h->avctx, AV_LOG_ERROR,\n "log2_max_frame_num_minus4 out of range (0-12): %d\\n",\n log2_max_frame_num_minus4);\n goto fail;\n }\n sps->log2_max_frame_num = log2_max_frame_num_minus4 + 4;\n sps->poc_type = get_ue_golomb_31(&h->gb);\n if (sps->poc_type == 0) {\n sps->log2_max_poc_lsb = get_ue_golomb(&h->gb) + 4;\n } else if (sps->poc_type == 1) {\n sps->delta_pic_order_always_zero_flag = get_bits1(&h->gb);\n sps->offset_for_non_ref_pic = get_se_golomb(&h->gb);\n sps->offset_for_top_to_bottom_field = get_se_golomb(&h->gb);\n sps->poc_cycle_length = get_ue_golomb(&h->gb);\n if ((unsigned)sps->poc_cycle_length >=\n FF_ARRAY_ELEMS(sps->offset_for_ref_frame)) {\n av_log(h->avctx, AV_LOG_ERROR,\n "poc_cycle_length overflow %u\\n", sps->poc_cycle_length);\n goto fail;\n }\n for (i = 0; i < sps->poc_cycle_length; i++)\n sps->offset_for_ref_frame[i] = get_se_golomb(&h->gb);\n } else if (sps->poc_type != 2) {\n av_log(h->avctx, AV_LOG_ERROR, "illegal POC type %d\\n", sps->poc_type);\n goto fail;\n }\n sps->ref_frame_count = get_ue_golomb_31(&h->gb);\n if (sps->ref_frame_count > MAX_PICTURE_COUNT - 2 ||\n sps->ref_frame_count >= 32U) {\n av_log(h->avctx, AV_LOG_ERROR,\n "too many reference frames %d\\n", sps->ref_frame_count);\n goto fail;\n }\n sps->gaps_in_frame_num_allowed_flag = get_bits1(&h->gb);\n sps->mb_width = get_ue_golomb(&h->gb) + 1;\n sps->mb_height = get_ue_golomb(&h->gb) + 1;\n if ((unsigned)sps->mb_width >= INT_MAX / 16 ||\n (unsigned)sps->mb_height >= INT_MAX / 16 ||\n av_image_check_size(16 * sps->mb_width,\n 16 * sps->mb_height, 0, h->avctx)) {\n av_log(h->avctx, AV_LOG_ERROR, "mb_width/height overflow\\n");\n goto fail;\n }\n sps->frame_mbs_only_flag = get_bits1(&h->gb);\n if (!sps->frame_mbs_only_flag)\n sps->mb_aff = get_bits1(&h->gb);\n else\n sps->mb_aff = 0;\n sps->direct_8x8_inference_flag = get_bits1(&h->gb);\n if (!sps->frame_mbs_only_flag && !sps->direct_8x8_inference_flag) {\n av_log(h->avctx, AV_LOG_ERROR,\n "This stream was generated by a broken encoder, invalid 8x8 inference\\n");\n goto fail;\n }\n#ifndef ALLOW_INTERLACE\n if (sps->mb_aff)\n av_log(h->avctx, AV_LOG_ERROR,\n "MBAFF support not included; enable it at compile-time.\\n");\n#endif\n sps->crop = get_bits1(&h->gb);\n if (sps->crop) {\n int crop_left = get_ue_golomb(&h->gb);\n int crop_right = get_ue_golomb(&h->gb);\n int crop_top = get_ue_golomb(&h->gb);\n int crop_bottom = get_ue_golomb(&h->gb);\n if (h->avctx->flags2 & CODEC_FLAG2_IGNORE_CROP) {\n av_log(h->avctx, AV_LOG_DEBUG, "discarding sps cropping, original "\n "values are l:%u r:%u t:%u b:%u\\n",\n crop_left, crop_right, crop_top, crop_bottom);\n sps->crop_left =\n sps->crop_right =\n sps->crop_top =\n sps->crop_bottom = 0;\n } else {\n int vsub = (sps->chroma_format_idc == 1) ? 1 : 0;\n int hsub = (sps->chroma_format_idc == 1 ||\n sps->chroma_format_idc == 2) ? 1 : 0;\n int step_x = 1 << hsub;\n int step_y = (2 - sps->frame_mbs_only_flag) << vsub;\n if (crop_left & (0x1F >> (sps->bit_depth_luma > 8)) &&\n !(h->avctx->flags & CODEC_FLAG_UNALIGNED)) {\n crop_left &= ~(0x1F >> (sps->bit_depth_luma > 8));\n av_log(h->avctx, AV_LOG_WARNING,\n "Reducing left cropping to %d "\n "chroma samples to preserve alignment.\\n",\n crop_left);\n }\n sps->crop_left = crop_left * step_x;\n sps->crop_right = crop_right * step_x;\n sps->crop_top = crop_top * step_y;\n sps->crop_bottom = crop_bottom * step_y;\n }\n } else {\n sps->crop_left =\n sps->crop_right =\n sps->crop_top =\n sps->crop_bottom =\n sps->crop = 0;\n }\n sps->vui_parameters_present_flag = get_bits1(&h->gb);\n if (sps->vui_parameters_present_flag) {\n int ret = decode_vui_parameters(h, sps);\n if (ret < 0 && h->avctx->err_recognition & AV_EF_EXPLODE)\n goto fail;\n }\n if (!sps->sar.den)\n sps->sar.den = 1;\n if (h->avctx->debug & FF_DEBUG_PICT_INFO) {\n static const char csp[4][5] = { "Gray", "420", "422", "444" };\n av_log(h->avctx, AV_LOG_DEBUG,\n "sps:%u profile:%d/%d poc:%d ref:%d %dx%d %s %s crop:%d/%d/%d/%d %s %s %d/%d\\n",\n sps_id, sps->profile_idc, sps->level_idc,\n sps->poc_type,\n sps->ref_frame_count,\n sps->mb_width, sps->mb_height,\n sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"),\n sps->direct_8x8_inference_flag ? "8B8" : "",\n sps->crop_left, sps->crop_right,\n sps->crop_top, sps->crop_bottom,\n sps->vui_parameters_present_flag ? "VUI" : "",\n csp[sps->chroma_format_idc],\n sps->timing_info_present_flag ? sps->num_units_in_tick : 0,\n sps->timing_info_present_flag ? sps->time_scale : 0);\n }\n sps->new = 1;\n av_free(h->sps_buffers[sps_id]);\n h->sps_buffers[sps_id] = sps;\n h->sps = *sps;\n return 0;\nfail:\n av_free(sps);\n return -1;\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}']
2,319
0
https://github.com/nginx/nginx/blob/07fc7dc7194e9928bf7e2b9dc66bb51ab9563cf3/src/http/ngx_http_header_filter_module.c/#L485
static ngx_int_t ngx_http_header_filter(ngx_http_request_t *r) { u_char *p; size_t len; ngx_str_t host, *status_line, tokens; ngx_buf_t *b; ngx_uint_t status, i, port; ngx_chain_t out; ngx_list_part_t *part; ngx_table_elt_t *header; ngx_connection_t *c; ngx_http_core_loc_conf_t *clcf; ngx_http_core_srv_conf_t *cscf; struct sockaddr_in *sin; #if (NGX_HAVE_INET6) struct sockaddr_in6 *sin6; #endif u_char addr[NGX_SOCKADDR_STRLEN]; if (r->header_sent) { return NGX_OK; } r->header_sent = 1; if (r != r->main) { return NGX_OK; } if (r->http_version < NGX_HTTP_VERSION_10) { return NGX_OK; } if (r->method == NGX_HTTP_HEAD) { r->header_only = 1; } if (r->headers_out.last_modified_time != -1) { if (r->headers_out.status != NGX_HTTP_OK && r->headers_out.status != NGX_HTTP_PARTIAL_CONTENT && r->headers_out.status != NGX_HTTP_NOT_MODIFIED) { r->headers_out.last_modified_time = -1; r->headers_out.last_modified = NULL; } } len = sizeof("HTTP/1.x ") - 1 + sizeof(CRLF) - 1 + sizeof(CRLF) - 1; if (r->headers_out.status_line.len) { len += r->headers_out.status_line.len; status_line = &r->headers_out.status_line; #if (NGX_SUPPRESS_WARN) status = 0; #endif } else { status = r->headers_out.status; if (status >= NGX_HTTP_OK && status < NGX_HTTP_LAST_2XX) { if (status == NGX_HTTP_NO_CONTENT) { r->header_only = 1; ngx_str_null(&r->headers_out.content_type); r->headers_out.last_modified_time = -1; r->headers_out.last_modified = NULL; r->headers_out.content_length = NULL; r->headers_out.content_length_n = -1; } status -= NGX_HTTP_OK; status_line = &ngx_http_status_lines[status]; len += ngx_http_status_lines[status].len; } else if (status >= NGX_HTTP_MOVED_PERMANENTLY && status < NGX_HTTP_LAST_3XX) { if (status == NGX_HTTP_NOT_MODIFIED) { r->header_only = 1; } status = status - NGX_HTTP_MOVED_PERMANENTLY + NGX_HTTP_OFF_3XX; status_line = &ngx_http_status_lines[status]; len += ngx_http_status_lines[status].len; } else if (status >= NGX_HTTP_BAD_REQUEST && status < NGX_HTTP_LAST_4XX) { status = status - NGX_HTTP_BAD_REQUEST + NGX_HTTP_OFF_4XX; status_line = &ngx_http_status_lines[status]; len += ngx_http_status_lines[status].len; } else if (status >= NGX_HTTP_INTERNAL_SERVER_ERROR && status < NGX_HTTP_LAST_5XX) { status = status - NGX_HTTP_INTERNAL_SERVER_ERROR + NGX_HTTP_OFF_5XX; status_line = &ngx_http_status_lines[status]; len += ngx_http_status_lines[status].len; } else { len += NGX_INT_T_LEN + 1 ; status_line = NULL; } if (status_line && status_line->len == 0) { status = r->headers_out.status; len += NGX_INT_T_LEN + 1 ; status_line = NULL; } } clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); #if (NGX_SUPPRESS_WARN) ngx_str_null(&tokens); #endif if (r->headers_out.server == NULL) { if (clcf->server_tokens == 0) { ngx_str_set(&tokens, ngx_http_server_string); } else if (clcf->server_tokens == 1) { ngx_str_set(&tokens, ngx_http_server_full_string); } else { if (ngx_http_complex_value(r, &clcf->server_tokens_value, &tokens) != NGX_OK) { return NGX_ERROR; } if (tokens.len == 3 && ngx_strncmp(tokens.data, "off", 3) == 0) { ngx_str_set(&tokens, ngx_http_server_string); } else if (tokens.len) { ngx_str_set(&tokens, ngx_http_server_full_string); } } len += tokens.len; } if (r->headers_out.date == NULL) { len += sizeof("Date: Mon, 28 Sep 1970 06:00:00 GMT" CRLF) - 1; } if (r->headers_out.content_type.len) { len += sizeof("Content-Type: ") - 1 + r->headers_out.content_type.len + 2; if (r->headers_out.content_type_len == r->headers_out.content_type.len && r->headers_out.charset.len) { len += sizeof("; charset=") - 1 + r->headers_out.charset.len; } } if (r->headers_out.content_length == NULL && r->headers_out.content_length_n >= 0) { len += sizeof("Content-Length: ") - 1 + NGX_OFF_T_LEN + 2; } if (r->headers_out.last_modified == NULL && r->headers_out.last_modified_time != -1) { len += sizeof("Last-Modified: Mon, 28 Sep 1970 06:00:00 GMT" CRLF) - 1; } c = r->connection; if (r->headers_out.location && r->headers_out.location->value.len && r->headers_out.location->value.data[0] == '/') { r->headers_out.location->hash = 0; if (clcf->server_name_in_redirect) { cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module); host = cscf->server_name; } else if (r->headers_in.server.len) { host = r->headers_in.server; } else { host.len = NGX_SOCKADDR_STRLEN; host.data = addr; if (ngx_connection_local_sockaddr(c, &host, 0) != NGX_OK) { return NGX_ERROR; } } switch (c->local_sockaddr->sa_family) { #if (NGX_HAVE_INET6) case AF_INET6: sin6 = (struct sockaddr_in6 *) c->local_sockaddr; port = ntohs(sin6->sin6_port); break; #endif #if (NGX_HAVE_UNIX_DOMAIN) case AF_UNIX: port = 0; break; #endif default: sin = (struct sockaddr_in *) c->local_sockaddr; port = ntohs(sin->sin_port); break; } len += sizeof("Location: https://") - 1 + host.len + r->headers_out.location->value.len + 2; if (clcf->port_in_redirect) { #if (NGX_HTTP_SSL) if (c->ssl) port = (port == 443) ? 0 : port; else #endif port = (port == 80) ? 0 : port; } else { port = 0; } if (port) { len += sizeof(":65535") - 1; } } else { ngx_str_null(&host); port = 0; } if (r->chunked) { len += sizeof("Transfer-Encoding: chunked" CRLF) - 1; } if (r->headers_out.status == NGX_HTTP_SWITCHING_PROTOCOLS) { len += sizeof("Connection: upgrade" CRLF) - 1; } else if (r->keepalive) { len += sizeof("Connection: keep-alive" CRLF) - 1; if (clcf->keepalive_header) { len += sizeof("Keep-Alive: timeout=") - 1 + NGX_TIME_T_LEN + 2; } } else { len += sizeof("Connection: close" CRLF) - 1; } #if (NGX_HTTP_GZIP) if (r->gzip_vary) { if (clcf->gzip_vary) { len += sizeof("Vary: Accept-Encoding" CRLF) - 1; } else { r->gzip_vary = 0; } } #endif part = &r->headers_out.headers.part; header = part->elts; for (i = 0; ; i++) { if (i >= part->nelts) { if (part->next == NULL) { break; } part = part->next; header = part->elts; i = 0; } if (header[i].hash == 0) { continue; } len += header[i].key.len + sizeof(": ") - 1 + header[i].value.len + sizeof(CRLF) - 1; } b = ngx_create_temp_buf(r->pool, len); if (b == NULL) { return NGX_ERROR; } b->last = ngx_cpymem(b->last, "HTTP/1.1 ", sizeof("HTTP/1.x ") - 1); if (status_line) { b->last = ngx_copy(b->last, status_line->data, status_line->len); } else { b->last = ngx_sprintf(b->last, "%03ui ", status); } *b->last++ = CR; *b->last++ = LF; if (r->headers_out.server == NULL && tokens.len) { b->last = ngx_cpymem(b->last, tokens.data, tokens.len); } if (r->headers_out.date == NULL) { b->last = ngx_cpymem(b->last, "Date: ", sizeof("Date: ") - 1); b->last = ngx_cpymem(b->last, ngx_cached_http_time.data, ngx_cached_http_time.len); *b->last++ = CR; *b->last++ = LF; } if (r->headers_out.content_type.len) { b->last = ngx_cpymem(b->last, "Content-Type: ", sizeof("Content-Type: ") - 1); p = b->last; b->last = ngx_copy(b->last, r->headers_out.content_type.data, r->headers_out.content_type.len); if (r->headers_out.content_type_len == r->headers_out.content_type.len && r->headers_out.charset.len) { b->last = ngx_cpymem(b->last, "; charset=", sizeof("; charset=") - 1); b->last = ngx_copy(b->last, r->headers_out.charset.data, r->headers_out.charset.len); r->headers_out.content_type.len = b->last - p; r->headers_out.content_type.data = p; } *b->last++ = CR; *b->last++ = LF; } if (r->headers_out.content_length == NULL && r->headers_out.content_length_n >= 0) { b->last = ngx_sprintf(b->last, "Content-Length: %O" CRLF, r->headers_out.content_length_n); } if (r->headers_out.last_modified == NULL && r->headers_out.last_modified_time != -1) { b->last = ngx_cpymem(b->last, "Last-Modified: ", sizeof("Last-Modified: ") - 1); b->last = ngx_http_time(b->last, r->headers_out.last_modified_time); *b->last++ = CR; *b->last++ = LF; } if (host.data) { p = b->last + sizeof("Location: ") - 1; b->last = ngx_cpymem(b->last, "Location: http", sizeof("Location: http") - 1); #if (NGX_HTTP_SSL) if (c->ssl) { *b->last++ ='s'; } #endif *b->last++ = ':'; *b->last++ = '/'; *b->last++ = '/'; b->last = ngx_copy(b->last, host.data, host.len); if (port) { b->last = ngx_sprintf(b->last, ":%ui", port); } b->last = ngx_copy(b->last, r->headers_out.location->value.data, r->headers_out.location->value.len); r->headers_out.location->value.len = b->last - p; r->headers_out.location->value.data = p; ngx_str_set(&r->headers_out.location->key, "Location"); *b->last++ = CR; *b->last++ = LF; } if (r->chunked) { b->last = ngx_cpymem(b->last, "Transfer-Encoding: chunked" CRLF, sizeof("Transfer-Encoding: chunked" CRLF) - 1); } if (r->headers_out.status == NGX_HTTP_SWITCHING_PROTOCOLS) { b->last = ngx_cpymem(b->last, "Connection: upgrade" CRLF, sizeof("Connection: upgrade" CRLF) - 1); } else if (r->keepalive) { b->last = ngx_cpymem(b->last, "Connection: keep-alive" CRLF, sizeof("Connection: keep-alive" CRLF) - 1); if (clcf->keepalive_header) { b->last = ngx_sprintf(b->last, "Keep-Alive: timeout=%T" CRLF, clcf->keepalive_header); } } else { b->last = ngx_cpymem(b->last, "Connection: close" CRLF, sizeof("Connection: close" CRLF) - 1); } #if (NGX_HTTP_GZIP) if (r->gzip_vary) { b->last = ngx_cpymem(b->last, "Vary: Accept-Encoding" CRLF, sizeof("Vary: Accept-Encoding" CRLF) - 1); } #endif part = &r->headers_out.headers.part; header = part->elts; for (i = 0; ; i++) { if (i >= part->nelts) { if (part->next == NULL) { break; } part = part->next; header = part->elts; i = 0; } if (header[i].hash == 0) { continue; } b->last = ngx_copy(b->last, header[i].key.data, header[i].key.len); *b->last++ = ':'; *b->last++ = ' '; b->last = ngx_copy(b->last, header[i].value.data, header[i].value.len); *b->last++ = CR; *b->last++ = LF; } ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "%*s", (size_t) (b->last - b->pos), b->pos); *b->last++ = CR; *b->last++ = LF; r->header_size = b->last - b->pos; if (r->header_only) { b->last_buf = 1; } out.buf = b; out.next = NULL; return ngx_http_write_filter(r, &out); }
['static ngx_int_t\nngx_http_header_filter(ngx_http_request_t *r)\n{\n u_char *p;\n size_t len;\n ngx_str_t host, *status_line, tokens;\n ngx_buf_t *b;\n ngx_uint_t status, i, port;\n ngx_chain_t out;\n ngx_list_part_t *part;\n ngx_table_elt_t *header;\n ngx_connection_t *c;\n ngx_http_core_loc_conf_t *clcf;\n ngx_http_core_srv_conf_t *cscf;\n struct sockaddr_in *sin;\n#if (NGX_HAVE_INET6)\n struct sockaddr_in6 *sin6;\n#endif\n u_char addr[NGX_SOCKADDR_STRLEN];\n if (r->header_sent) {\n return NGX_OK;\n }\n r->header_sent = 1;\n if (r != r->main) {\n return NGX_OK;\n }\n if (r->http_version < NGX_HTTP_VERSION_10) {\n return NGX_OK;\n }\n if (r->method == NGX_HTTP_HEAD) {\n r->header_only = 1;\n }\n if (r->headers_out.last_modified_time != -1) {\n if (r->headers_out.status != NGX_HTTP_OK\n && r->headers_out.status != NGX_HTTP_PARTIAL_CONTENT\n && r->headers_out.status != NGX_HTTP_NOT_MODIFIED)\n {\n r->headers_out.last_modified_time = -1;\n r->headers_out.last_modified = NULL;\n }\n }\n len = sizeof("HTTP/1.x ") - 1 + sizeof(CRLF) - 1\n + sizeof(CRLF) - 1;\n if (r->headers_out.status_line.len) {\n len += r->headers_out.status_line.len;\n status_line = &r->headers_out.status_line;\n#if (NGX_SUPPRESS_WARN)\n status = 0;\n#endif\n } else {\n status = r->headers_out.status;\n if (status >= NGX_HTTP_OK\n && status < NGX_HTTP_LAST_2XX)\n {\n if (status == NGX_HTTP_NO_CONTENT) {\n r->header_only = 1;\n ngx_str_null(&r->headers_out.content_type);\n r->headers_out.last_modified_time = -1;\n r->headers_out.last_modified = NULL;\n r->headers_out.content_length = NULL;\n r->headers_out.content_length_n = -1;\n }\n status -= NGX_HTTP_OK;\n status_line = &ngx_http_status_lines[status];\n len += ngx_http_status_lines[status].len;\n } else if (status >= NGX_HTTP_MOVED_PERMANENTLY\n && status < NGX_HTTP_LAST_3XX)\n {\n if (status == NGX_HTTP_NOT_MODIFIED) {\n r->header_only = 1;\n }\n status = status - NGX_HTTP_MOVED_PERMANENTLY + NGX_HTTP_OFF_3XX;\n status_line = &ngx_http_status_lines[status];\n len += ngx_http_status_lines[status].len;\n } else if (status >= NGX_HTTP_BAD_REQUEST\n && status < NGX_HTTP_LAST_4XX)\n {\n status = status - NGX_HTTP_BAD_REQUEST\n + NGX_HTTP_OFF_4XX;\n status_line = &ngx_http_status_lines[status];\n len += ngx_http_status_lines[status].len;\n } else if (status >= NGX_HTTP_INTERNAL_SERVER_ERROR\n && status < NGX_HTTP_LAST_5XX)\n {\n status = status - NGX_HTTP_INTERNAL_SERVER_ERROR\n + NGX_HTTP_OFF_5XX;\n status_line = &ngx_http_status_lines[status];\n len += ngx_http_status_lines[status].len;\n } else {\n len += NGX_INT_T_LEN + 1 ;\n status_line = NULL;\n }\n if (status_line && status_line->len == 0) {\n status = r->headers_out.status;\n len += NGX_INT_T_LEN + 1 ;\n status_line = NULL;\n }\n }\n clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);\n#if (NGX_SUPPRESS_WARN)\n ngx_str_null(&tokens);\n#endif\n if (r->headers_out.server == NULL) {\n if (clcf->server_tokens == 0) {\n ngx_str_set(&tokens, ngx_http_server_string);\n } else if (clcf->server_tokens == 1) {\n ngx_str_set(&tokens, ngx_http_server_full_string);\n } else {\n if (ngx_http_complex_value(r, &clcf->server_tokens_value, &tokens)\n != NGX_OK)\n {\n return NGX_ERROR;\n }\n if (tokens.len == 3\n && ngx_strncmp(tokens.data, "off", 3) == 0)\n {\n ngx_str_set(&tokens, ngx_http_server_string);\n } else if (tokens.len) {\n ngx_str_set(&tokens, ngx_http_server_full_string);\n }\n }\n len += tokens.len;\n }\n if (r->headers_out.date == NULL) {\n len += sizeof("Date: Mon, 28 Sep 1970 06:00:00 GMT" CRLF) - 1;\n }\n if (r->headers_out.content_type.len) {\n len += sizeof("Content-Type: ") - 1\n + r->headers_out.content_type.len + 2;\n if (r->headers_out.content_type_len == r->headers_out.content_type.len\n && r->headers_out.charset.len)\n {\n len += sizeof("; charset=") - 1 + r->headers_out.charset.len;\n }\n }\n if (r->headers_out.content_length == NULL\n && r->headers_out.content_length_n >= 0)\n {\n len += sizeof("Content-Length: ") - 1 + NGX_OFF_T_LEN + 2;\n }\n if (r->headers_out.last_modified == NULL\n && r->headers_out.last_modified_time != -1)\n {\n len += sizeof("Last-Modified: Mon, 28 Sep 1970 06:00:00 GMT" CRLF) - 1;\n }\n c = r->connection;\n if (r->headers_out.location\n && r->headers_out.location->value.len\n && r->headers_out.location->value.data[0] == \'/\')\n {\n r->headers_out.location->hash = 0;\n if (clcf->server_name_in_redirect) {\n cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);\n host = cscf->server_name;\n } else if (r->headers_in.server.len) {\n host = r->headers_in.server;\n } else {\n host.len = NGX_SOCKADDR_STRLEN;\n host.data = addr;\n if (ngx_connection_local_sockaddr(c, &host, 0) != NGX_OK) {\n return NGX_ERROR;\n }\n }\n switch (c->local_sockaddr->sa_family) {\n#if (NGX_HAVE_INET6)\n case AF_INET6:\n sin6 = (struct sockaddr_in6 *) c->local_sockaddr;\n port = ntohs(sin6->sin6_port);\n break;\n#endif\n#if (NGX_HAVE_UNIX_DOMAIN)\n case AF_UNIX:\n port = 0;\n break;\n#endif\n default:\n sin = (struct sockaddr_in *) c->local_sockaddr;\n port = ntohs(sin->sin_port);\n break;\n }\n len += sizeof("Location: https://") - 1\n + host.len\n + r->headers_out.location->value.len + 2;\n if (clcf->port_in_redirect) {\n#if (NGX_HTTP_SSL)\n if (c->ssl)\n port = (port == 443) ? 0 : port;\n else\n#endif\n port = (port == 80) ? 0 : port;\n } else {\n port = 0;\n }\n if (port) {\n len += sizeof(":65535") - 1;\n }\n } else {\n ngx_str_null(&host);\n port = 0;\n }\n if (r->chunked) {\n len += sizeof("Transfer-Encoding: chunked" CRLF) - 1;\n }\n if (r->headers_out.status == NGX_HTTP_SWITCHING_PROTOCOLS) {\n len += sizeof("Connection: upgrade" CRLF) - 1;\n } else if (r->keepalive) {\n len += sizeof("Connection: keep-alive" CRLF) - 1;\n if (clcf->keepalive_header) {\n len += sizeof("Keep-Alive: timeout=") - 1 + NGX_TIME_T_LEN + 2;\n }\n } else {\n len += sizeof("Connection: close" CRLF) - 1;\n }\n#if (NGX_HTTP_GZIP)\n if (r->gzip_vary) {\n if (clcf->gzip_vary) {\n len += sizeof("Vary: Accept-Encoding" CRLF) - 1;\n } else {\n r->gzip_vary = 0;\n }\n }\n#endif\n part = &r->headers_out.headers.part;\n header = part->elts;\n for (i = 0; ; i++) {\n if (i >= part->nelts) {\n if (part->next == NULL) {\n break;\n }\n part = part->next;\n header = part->elts;\n i = 0;\n }\n if (header[i].hash == 0) {\n continue;\n }\n len += header[i].key.len + sizeof(": ") - 1 + header[i].value.len\n + sizeof(CRLF) - 1;\n }\n b = ngx_create_temp_buf(r->pool, len);\n if (b == NULL) {\n return NGX_ERROR;\n }\n b->last = ngx_cpymem(b->last, "HTTP/1.1 ", sizeof("HTTP/1.x ") - 1);\n if (status_line) {\n b->last = ngx_copy(b->last, status_line->data, status_line->len);\n } else {\n b->last = ngx_sprintf(b->last, "%03ui ", status);\n }\n *b->last++ = CR; *b->last++ = LF;\n if (r->headers_out.server == NULL && tokens.len) {\n b->last = ngx_cpymem(b->last, tokens.data, tokens.len);\n }\n if (r->headers_out.date == NULL) {\n b->last = ngx_cpymem(b->last, "Date: ", sizeof("Date: ") - 1);\n b->last = ngx_cpymem(b->last, ngx_cached_http_time.data,\n ngx_cached_http_time.len);\n *b->last++ = CR; *b->last++ = LF;\n }\n if (r->headers_out.content_type.len) {\n b->last = ngx_cpymem(b->last, "Content-Type: ",\n sizeof("Content-Type: ") - 1);\n p = b->last;\n b->last = ngx_copy(b->last, r->headers_out.content_type.data,\n r->headers_out.content_type.len);\n if (r->headers_out.content_type_len == r->headers_out.content_type.len\n && r->headers_out.charset.len)\n {\n b->last = ngx_cpymem(b->last, "; charset=",\n sizeof("; charset=") - 1);\n b->last = ngx_copy(b->last, r->headers_out.charset.data,\n r->headers_out.charset.len);\n r->headers_out.content_type.len = b->last - p;\n r->headers_out.content_type.data = p;\n }\n *b->last++ = CR; *b->last++ = LF;\n }\n if (r->headers_out.content_length == NULL\n && r->headers_out.content_length_n >= 0)\n {\n b->last = ngx_sprintf(b->last, "Content-Length: %O" CRLF,\n r->headers_out.content_length_n);\n }\n if (r->headers_out.last_modified == NULL\n && r->headers_out.last_modified_time != -1)\n {\n b->last = ngx_cpymem(b->last, "Last-Modified: ",\n sizeof("Last-Modified: ") - 1);\n b->last = ngx_http_time(b->last, r->headers_out.last_modified_time);\n *b->last++ = CR; *b->last++ = LF;\n }\n if (host.data) {\n p = b->last + sizeof("Location: ") - 1;\n b->last = ngx_cpymem(b->last, "Location: http",\n sizeof("Location: http") - 1);\n#if (NGX_HTTP_SSL)\n if (c->ssl) {\n *b->last++ =\'s\';\n }\n#endif\n *b->last++ = \':\'; *b->last++ = \'/\'; *b->last++ = \'/\';\n b->last = ngx_copy(b->last, host.data, host.len);\n if (port) {\n b->last = ngx_sprintf(b->last, ":%ui", port);\n }\n b->last = ngx_copy(b->last, r->headers_out.location->value.data,\n r->headers_out.location->value.len);\n r->headers_out.location->value.len = b->last - p;\n r->headers_out.location->value.data = p;\n ngx_str_set(&r->headers_out.location->key, "Location");\n *b->last++ = CR; *b->last++ = LF;\n }\n if (r->chunked) {\n b->last = ngx_cpymem(b->last, "Transfer-Encoding: chunked" CRLF,\n sizeof("Transfer-Encoding: chunked" CRLF) - 1);\n }\n if (r->headers_out.status == NGX_HTTP_SWITCHING_PROTOCOLS) {\n b->last = ngx_cpymem(b->last, "Connection: upgrade" CRLF,\n sizeof("Connection: upgrade" CRLF) - 1);\n } else if (r->keepalive) {\n b->last = ngx_cpymem(b->last, "Connection: keep-alive" CRLF,\n sizeof("Connection: keep-alive" CRLF) - 1);\n if (clcf->keepalive_header) {\n b->last = ngx_sprintf(b->last, "Keep-Alive: timeout=%T" CRLF,\n clcf->keepalive_header);\n }\n } else {\n b->last = ngx_cpymem(b->last, "Connection: close" CRLF,\n sizeof("Connection: close" CRLF) - 1);\n }\n#if (NGX_HTTP_GZIP)\n if (r->gzip_vary) {\n b->last = ngx_cpymem(b->last, "Vary: Accept-Encoding" CRLF,\n sizeof("Vary: Accept-Encoding" CRLF) - 1);\n }\n#endif\n part = &r->headers_out.headers.part;\n header = part->elts;\n for (i = 0; ; i++) {\n if (i >= part->nelts) {\n if (part->next == NULL) {\n break;\n }\n part = part->next;\n header = part->elts;\n i = 0;\n }\n if (header[i].hash == 0) {\n continue;\n }\n b->last = ngx_copy(b->last, header[i].key.data, header[i].key.len);\n *b->last++ = \':\'; *b->last++ = \' \';\n b->last = ngx_copy(b->last, header[i].value.data, header[i].value.len);\n *b->last++ = CR; *b->last++ = LF;\n }\n ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,\n "%*s", (size_t) (b->last - b->pos), b->pos);\n *b->last++ = CR; *b->last++ = LF;\n r->header_size = b->last - b->pos;\n if (r->header_only) {\n b->last_buf = 1;\n }\n out.buf = b;\n out.next = NULL;\n return ngx_http_write_filter(r, &out);\n}']
2,320
0
https://github.com/openssl/openssl/blob/04485c5bc0dc7f49940e6d91b27cdcc7b83a8ab5/crypto/bn/bn_ctx.c/#L355
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['int gost_do_verify(const unsigned char *dgst, int dgst_len,\n\tDSA_SIG *sig, DSA *dsa)\n\t{\n\tBIGNUM *md, *tmp=NULL;\n\tBIGNUM *q2=NULL;\n\tBIGNUM *u=NULL,*v=NULL,*z1=NULL,*z2=NULL;\n\tBIGNUM *tmp2=NULL,*tmp3=NULL;\n\tint ok;\n\tBN_CTX *ctx = BN_CTX_new();\n\tBN_CTX_start(ctx);\n\tif (BN_cmp(sig->s,dsa->q)>=1||\n\t\tBN_cmp(sig->r,dsa->q)>=1)\n\t\t{\n\t\tGOSTerr(GOST_F_GOST_DO_VERIFY,GOST_R_SIGNATURE_PARTS_GREATER_THAN_Q);\n\t\treturn 0;\n\t\t}\n\tmd=hashsum2bn(dgst);\n\ttmp=BN_CTX_get(ctx);\n\tv=BN_CTX_get(ctx);\n\tq2=BN_CTX_get(ctx);\n\tz1=BN_CTX_get(ctx);\n\tz2=BN_CTX_get(ctx);\n\ttmp2=BN_CTX_get(ctx);\n\ttmp3=BN_CTX_get(ctx);\n\tu = BN_CTX_get(ctx);\n\tBN_mod(tmp,md,dsa->q,ctx);\n\tif (BN_is_zero(tmp))\n\t\t{\n\t\tBN_one(md);\n\t\t}\n\tBN_copy(q2,dsa->q);\n\tBN_sub_word(q2,2);\n\tBN_mod_exp(v,md,q2,dsa->q,ctx);\n\tBN_mod_mul(z1,sig->s,v,dsa->q,ctx);\n\tBN_sub(tmp,dsa->q,sig->r);\n\tBN_mod_mul(z2,tmp,v,dsa->p,ctx);\n\tBN_mod_exp(tmp,dsa->g,z1,dsa->p,ctx);\n\tBN_mod_exp(tmp2,dsa->pub_key,z2,dsa->p,ctx);\n\tBN_mod_mul(tmp3,tmp,tmp2,dsa->p,ctx);\n\tBN_mod(u,tmp3,dsa->q,ctx);\n\tok= BN_cmp(u,sig->r);\n\tBN_free(md);\n\tBN_CTX_end(ctx);\n\tBN_CTX_free(ctx);\n\tif (ok!=0)\n\t\t{\n\t\tGOSTerr(GOST_F_GOST_DO_VERIFY,GOST_R_SIGNATURE_MISMATCH);\n\t\t}\n\treturn (ok==0);\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_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}']
2,321
0
https://github.com/openssl/openssl/blob/c3be39f2e47ec6c538ef1060d35dbee5c286ea4f/test/evp_test.c/#L993
static int mac_test_parse(EVP_TEST *t, const char *keyword, const char *value) { MAC_DATA *mdata = t->data; if (strcmp(keyword, "Key") == 0) return parse_bin(value, &mdata->key, &mdata->key_len); if (strcmp(keyword, "IV") == 0) return parse_bin(value, &mdata->iv, &mdata->iv_len); if (strcmp(keyword, "Custom") == 0) return parse_bin(value, &mdata->custom, &mdata->custom_len); if (strcmp(keyword, "Salt") == 0) return parse_bin(value, &mdata->salt, &mdata->salt_len); if (strcmp(keyword, "Algorithm") == 0) { mdata->alg = OPENSSL_strdup(value); if (!mdata->alg) return 0; return 1; } if (strcmp(keyword, "Input") == 0) return parse_bin(value, &mdata->input, &mdata->input_len); if (strcmp(keyword, "Output") == 0) return parse_bin(value, &mdata->output, &mdata->output_len); if (strcmp(keyword, "Ctrl") == 0) return sk_OPENSSL_STRING_push(mdata->controls, OPENSSL_strdup(value)) != 0; return 0; }
['static int mac_test_parse(EVP_TEST *t,\n const char *keyword, const char *value)\n{\n MAC_DATA *mdata = t->data;\n if (strcmp(keyword, "Key") == 0)\n return parse_bin(value, &mdata->key, &mdata->key_len);\n if (strcmp(keyword, "IV") == 0)\n return parse_bin(value, &mdata->iv, &mdata->iv_len);\n if (strcmp(keyword, "Custom") == 0)\n return parse_bin(value, &mdata->custom, &mdata->custom_len);\n if (strcmp(keyword, "Salt") == 0)\n return parse_bin(value, &mdata->salt, &mdata->salt_len);\n if (strcmp(keyword, "Algorithm") == 0) {\n mdata->alg = OPENSSL_strdup(value);\n if (!mdata->alg)\n return 0;\n return 1;\n }\n if (strcmp(keyword, "Input") == 0)\n return parse_bin(value, &mdata->input, &mdata->input_len);\n if (strcmp(keyword, "Output") == 0)\n return parse_bin(value, &mdata->output, &mdata->output_len);\n if (strcmp(keyword, "Ctrl") == 0)\n return sk_OPENSSL_STRING_push(mdata->controls,\n OPENSSL_strdup(value)) != 0;\n return 0;\n}', 'char *CRYPTO_strdup(const char *str, const char* file, int line)\n{\n char *ret;\n if (str == NULL)\n return NULL;\n ret = CRYPTO_malloc(strlen(str) + 1, file, line);\n if (ret != NULL)\n strcpy(ret, str);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n INCREMENT(malloc_count);\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n if (allow_customize) {\n allow_customize = 0;\n }\n#if !defined(OPENSSL_NO_CRYPTO_MDEBUG) && !defined(FIPS_MODE)\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)(file); (void)(line);\n ret = malloc(num);\n#endif\n return ret;\n}', 'DEFINE_SPECIAL_STACK_OF(OPENSSL_STRING, char)', '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}']
2,322
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_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}', '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(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int ret = bn_sqr_fixed_top(r, a, ctx);\n bn_correct_top(r);\n bn_check_top(r);\n return ret;\n}', 'int bn_sqr_fixed_top(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int max, al;\n int ret = 0;\n BIGNUM *tmp, *rr;\n bn_check_top(a);\n al = a->top;\n if (al <= 0) {\n r->top = 0;\n r->neg = 0;\n return 1;\n }\n BN_CTX_start(ctx);\n rr = (a != r) ? r : BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (rr == NULL || tmp == NULL)\n goto err;\n max = 2 * al;\n if (bn_wexpand(rr, max) == NULL)\n goto err;\n if (al == 4) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[8];\n bn_sqr_normal(rr->d, a->d, 4, t);\n#else\n bn_sqr_comba4(rr->d, a->d);\n#endif\n } else if (al == 8) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[16];\n bn_sqr_normal(rr->d, a->d, 8, t);\n#else\n bn_sqr_comba8(rr->d, a->d);\n#endif\n } else {\n#if defined(BN_RECURSION)\n if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {\n BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];\n bn_sqr_normal(rr->d, a->d, al, t);\n } else {\n int j, k;\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n if (al == j) {\n if (bn_wexpand(tmp, k * 2) == NULL)\n goto err;\n bn_sqr_recursive(rr->d, a->d, al, tmp->d);\n } else {\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n }\n }\n#else\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n#endif\n }\n rr->neg = 0;\n rr->top = max;\n rr->flags |= BN_FLG_FIXED_TOP;\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(rr);\n bn_check_top(tmp);\n BN_CTX_end(ctx);\n return ret;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}']
2,323
1
https://github.com/openssl/openssl/blob/8e826a339f8cda20a4311fa88a1de782972cf40d/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; }
['EC_GROUP *EC_GROUP_new_from_ecparameters(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_GROUP_NEW_FROM_ECPARAMETERS, 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_GROUP_NEW_FROM_ECPARAMETERS, 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_GROUP_NEW_FROM_ECPARAMETERS, 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_GROUP_NEW_FROM_ECPARAMETERS, 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_GROUP_NEW_FROM_ECPARAMETERS, 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_GROUP_NEW_FROM_ECPARAMETERS, EC_R_FIELD_TOO_LARGE);\n goto err;\n }\n if ((p = BN_new()) == NULL) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, 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_GROUP_NEW_FROM_ECPARAMETERS, 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_GROUP_NEW_FROM_ECPARAMETERS,\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_GROUP_NEW_FROM_ECPARAMETERS, 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_GROUP_NEW_FROM_ECPARAMETERS,\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_GROUP_NEW_FROM_ECPARAMETERS, EC_R_NOT_IMPLEMENTED);\n goto err;\n } else {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, 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_GROUP_NEW_FROM_ECPARAMETERS, 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_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);\n goto err;\n }\n if (BN_is_negative(p) || BN_is_zero(p)) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, 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_GROUP_NEW_FROM_ECPARAMETERS, 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_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_FIELD);\n goto err;\n }\n if (ret == NULL) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, 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_GROUP_NEW_FROM_ECPARAMETERS, 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_GROUP_NEW_FROM_ECPARAMETERS, 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_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);\n goto err;\n }\n if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);\n goto err;\n }\n if (BN_is_negative(a) || BN_is_zero(a)) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_GROUP_ORDER);\n goto err;\n }\n if (BN_num_bits(a) > (int)field_bits + 1) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, 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_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);\n goto err;\n }\n if (!EC_GROUP_set_generator(ret, point, a, b)) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, 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}', '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_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}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return (NULL);\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)(file); (void)(line);\n ret = malloc(num);\n#endif\n return ret;\n}']
2,324
0
https://github.com/libav/libav/blob/6e4ca0749c2e6434e071dbd314076613fb3ef59b/ffmpeg.c/#L3690
static void new_subtitle_stream(AVFormatContext *oc, int file_idx) { AVStream *st; AVOutputStream *ost; AVCodec *codec=NULL; AVCodecContext *subtitle_enc; enum CodecID codec_id = CODEC_ID_NONE; st = av_new_stream(oc, oc->nb_streams < nb_streamid_map ? streamid_map[oc->nb_streams] : 0); if (!st) { fprintf(stderr, "Could not alloc stream\n"); ffmpeg_exit(1); } ost = new_output_stream(oc, file_idx); subtitle_enc = st->codec; if(!subtitle_stream_copy){ if (subtitle_codec_name) { codec_id = find_codec_or_die(subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, 1, avcodec_opts[AVMEDIA_TYPE_SUBTITLE]->strict_std_compliance); codec = avcodec_find_encoder_by_name(subtitle_codec_name); ost->enc = codec; } else { codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL, AVMEDIA_TYPE_SUBTITLE); codec = avcodec_find_encoder(codec_id); } } avcodec_get_context_defaults3(st->codec, codec); ost->bitstream_filters = subtitle_bitstream_filters; subtitle_bitstream_filters= NULL; subtitle_enc->codec_type = AVMEDIA_TYPE_SUBTITLE; if(subtitle_codec_tag) subtitle_enc->codec_tag= subtitle_codec_tag; if (oc->oformat->flags & AVFMT_GLOBALHEADER) { subtitle_enc->flags |= CODEC_FLAG_GLOBAL_HEADER; avcodec_opts[AVMEDIA_TYPE_SUBTITLE]->flags |= CODEC_FLAG_GLOBAL_HEADER; } if (subtitle_stream_copy) { st->stream_copy = 1; } else { subtitle_enc->codec_id = codec_id; set_context_opts(avcodec_opts[AVMEDIA_TYPE_SUBTITLE], subtitle_enc, AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_ENCODING_PARAM, codec); } if (subtitle_language) { av_dict_set(&st->metadata, "language", subtitle_language, 0); av_freep(&subtitle_language); } subtitle_disable = 0; av_freep(&subtitle_codec_name); subtitle_stream_copy = 0; }
['static void new_subtitle_stream(AVFormatContext *oc, int file_idx)\n{\n AVStream *st;\n AVOutputStream *ost;\n AVCodec *codec=NULL;\n AVCodecContext *subtitle_enc;\n enum CodecID codec_id = CODEC_ID_NONE;\n st = av_new_stream(oc, oc->nb_streams < nb_streamid_map ? streamid_map[oc->nb_streams] : 0);\n if (!st) {\n fprintf(stderr, "Could not alloc stream\\n");\n ffmpeg_exit(1);\n }\n ost = new_output_stream(oc, file_idx);\n subtitle_enc = st->codec;\n if(!subtitle_stream_copy){\n if (subtitle_codec_name) {\n codec_id = find_codec_or_die(subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, 1,\n avcodec_opts[AVMEDIA_TYPE_SUBTITLE]->strict_std_compliance);\n codec = avcodec_find_encoder_by_name(subtitle_codec_name);\n ost->enc = codec;\n } else {\n codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL, AVMEDIA_TYPE_SUBTITLE);\n codec = avcodec_find_encoder(codec_id);\n }\n }\n avcodec_get_context_defaults3(st->codec, codec);\n ost->bitstream_filters = subtitle_bitstream_filters;\n subtitle_bitstream_filters= NULL;\n subtitle_enc->codec_type = AVMEDIA_TYPE_SUBTITLE;\n if(subtitle_codec_tag)\n subtitle_enc->codec_tag= subtitle_codec_tag;\n if (oc->oformat->flags & AVFMT_GLOBALHEADER) {\n subtitle_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;\n avcodec_opts[AVMEDIA_TYPE_SUBTITLE]->flags |= CODEC_FLAG_GLOBAL_HEADER;\n }\n if (subtitle_stream_copy) {\n st->stream_copy = 1;\n } else {\n subtitle_enc->codec_id = codec_id;\n set_context_opts(avcodec_opts[AVMEDIA_TYPE_SUBTITLE], subtitle_enc, AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_ENCODING_PARAM, codec);\n }\n if (subtitle_language) {\n av_dict_set(&st->metadata, "language", subtitle_language, 0);\n av_freep(&subtitle_language);\n }\n subtitle_disable = 0;\n av_freep(&subtitle_codec_name);\n subtitle_stream_copy = 0;\n}', 'AVStream *av_new_stream(AVFormatContext *s, int id)\n{\n AVStream *st;\n int i;\n AVStream **streams;\n if (s->nb_streams >= INT_MAX/sizeof(*streams))\n return NULL;\n streams = av_realloc(s->streams, (s->nb_streams + 1) * sizeof(*streams));\n if (!streams)\n return NULL;\n s->streams = streams;\n st = av_mallocz(sizeof(AVStream));\n if (!st)\n return NULL;\n if (!(st->info = av_mallocz(sizeof(*st->info)))) {\n av_free(st);\n return NULL;\n }\n st->codec= avcodec_alloc_context();\n if (s->iformat) {\n st->codec->bit_rate = 0;\n }\n st->index = s->nb_streams;\n st->id = id;\n st->start_time = AV_NOPTS_VALUE;\n st->duration = AV_NOPTS_VALUE;\n st->cur_dts = 0;\n st->first_dts = AV_NOPTS_VALUE;\n st->probe_packets = MAX_PROBE_PACKETS;\n av_set_pts_info(st, 33, 1, 90000);\n st->last_IP_pts = AV_NOPTS_VALUE;\n for(i=0; i<MAX_REORDER_DELAY+1; i++)\n st->pts_buffer[i]= AV_NOPTS_VALUE;\n st->reference_dts = AV_NOPTS_VALUE;\n st->sample_aspect_ratio = (AVRational){0,1};\n s->streams[s->nb_streams++] = st;\n return st;\n}']
2,325
0
https://github.com/libav/libav/blob/7fa70598e83cca650717d02ac96bcf55e9f97c19/libavcodec/h264.c/#L5308
static av_always_inline void decode_cabac_residual_internal( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff, int is_dc ) { static const int significant_coeff_flag_offset[2][6] = { { 105+0, 105+15, 105+29, 105+44, 105+47, 402 }, { 277+0, 277+15, 277+29, 277+44, 277+47, 436 } }; static const int last_coeff_flag_offset[2][6] = { { 166+0, 166+15, 166+29, 166+44, 166+47, 417 }, { 338+0, 338+15, 338+29, 338+44, 338+47, 451 } }; static const int coeff_abs_level_m1_offset[6] = { 227+0, 227+10, 227+20, 227+30, 227+39, 426 }; static const uint8_t significant_coeff_flag_offset_8x8[2][63] = { { 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5, 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7, 7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11, 12,13,11, 6, 9,14,10, 9,11,12,13,11,14,10,12 }, { 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 7, 8, 4, 5, 6, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,13,13, 9, 9,10,10, 8,13,13, 9, 9,10,10,14,14,14,14,14 } }; static const uint8_t coeff_abs_level1_ctx[8] = { 1, 2, 3, 4, 0, 0, 0, 0 }; static const uint8_t coeff_abs_levelgt1_ctx[8] = { 5, 5, 5, 5, 6, 7, 8, 9 }; static const uint8_t coeff_abs_level_transition[2][8] = { { 1, 2, 3, 3, 4, 5, 6, 7 }, { 4, 4, 4, 4, 5, 6, 7, 7 } }; int index[64]; int av_unused last; int coeff_count = 0; int node_ctx = 0; uint8_t *significant_coeff_ctx_base; uint8_t *last_coeff_ctx_base; uint8_t *abs_level_m1_ctx_base; #if !ARCH_X86 #define CABAC_ON_STACK #endif #ifdef CABAC_ON_STACK #define CC &cc CABACContext cc; cc.range = h->cabac.range; cc.low = h->cabac.low; cc.bytestream= h->cabac.bytestream; #else #define CC &h->cabac #endif if( is_dc || cat != 5 ) { if( get_cabac( CC, &h->cabac_state[85 + get_cabac_cbf_ctx( h, cat, n, is_dc ) ] ) == 0 ) { if( !is_dc ) h->non_zero_count_cache[scan8[n]] = 0; #ifdef CABAC_ON_STACK h->cabac.range = cc.range ; h->cabac.low = cc.low ; h->cabac.bytestream= cc.bytestream; #endif return; } } significant_coeff_ctx_base = h->cabac_state + significant_coeff_flag_offset[MB_FIELD][cat]; last_coeff_ctx_base = h->cabac_state + last_coeff_flag_offset[MB_FIELD][cat]; abs_level_m1_ctx_base = h->cabac_state + coeff_abs_level_m1_offset[cat]; if( !is_dc && cat == 5 ) { #define DECODE_SIGNIFICANCE( coefs, sig_off, last_off ) \ for(last= 0; last < coefs; last++) { \ uint8_t *sig_ctx = significant_coeff_ctx_base + sig_off; \ if( get_cabac( CC, sig_ctx )) { \ uint8_t *last_ctx = last_coeff_ctx_base + last_off; \ index[coeff_count++] = last; \ if( get_cabac( CC, last_ctx ) ) { \ last= max_coeff; \ break; \ } \ } \ }\ if( last == max_coeff -1 ) {\ index[coeff_count++] = last;\ } const uint8_t *sig_off = significant_coeff_flag_offset_8x8[MB_FIELD]; #if ARCH_X86 && HAVE_7REGS && HAVE_EBX_AVAILABLE && !defined(BROKEN_RELOCATIONS) coeff_count= decode_significance_8x8_x86(CC, significant_coeff_ctx_base, index, sig_off); } else { coeff_count= decode_significance_x86(CC, max_coeff, significant_coeff_ctx_base, index); #else DECODE_SIGNIFICANCE( 63, sig_off[last], last_coeff_flag_offset_8x8[last] ); } else { DECODE_SIGNIFICANCE( max_coeff - 1, last, last ); #endif } assert(coeff_count > 0); if( is_dc ) { if( cat == 0 ) h->cbp_table[h->mb_xy] |= 0x100; else h->cbp_table[h->mb_xy] |= 0x40 << n; } else { if( cat == 5 ) fill_rectangle(&h->non_zero_count_cache[scan8[n]], 2, 2, 8, coeff_count, 1); else { assert( cat == 1 || cat == 2 || cat == 4 ); h->non_zero_count_cache[scan8[n]] = coeff_count; } } do { uint8_t *ctx = coeff_abs_level1_ctx[node_ctx] + abs_level_m1_ctx_base; int j= scantable[index[--coeff_count]]; if( get_cabac( CC, ctx ) == 0 ) { node_ctx = coeff_abs_level_transition[0][node_ctx]; if( is_dc ) { block[j] = get_cabac_bypass_sign( CC, -1); }else{ block[j] = (get_cabac_bypass_sign( CC, -qmul[j]) + 32) >> 6; } } else { int coeff_abs = 2; ctx = coeff_abs_levelgt1_ctx[node_ctx] + abs_level_m1_ctx_base; node_ctx = coeff_abs_level_transition[1][node_ctx]; while( coeff_abs < 15 && get_cabac( CC, ctx ) ) { coeff_abs++; } if( coeff_abs >= 15 ) { int j = 0; while( get_cabac_bypass( CC ) ) { j++; } coeff_abs=1; while( j-- ) { coeff_abs += coeff_abs + get_cabac_bypass( CC ); } coeff_abs+= 14; } if( is_dc ) { block[j] = get_cabac_bypass_sign( CC, -coeff_abs ); }else{ block[j] = (get_cabac_bypass_sign( CC, -coeff_abs ) * qmul[j] + 32) >> 6; } } } while( coeff_count ); #ifdef CABAC_ON_STACK h->cabac.range = cc.range ; h->cabac.low = cc.low ; h->cabac.bytestream= cc.bytestream; #endif }
['static int decode_mb_cabac(H264Context *h) {\n MpegEncContext * const s = &h->s;\n int mb_xy;\n int mb_type, partition_count, cbp = 0;\n int dct8x8_allowed= h->pps.transform_8x8_mode;\n mb_xy = h->mb_xy = s->mb_x + s->mb_y*s->mb_stride;\n tprintf(s->avctx, "pic:%d mb:%d/%d\\n", h->frame_num, s->mb_x, s->mb_y);\n if( h->slice_type_nos != FF_I_TYPE ) {\n int skip;\n if( FRAME_MBAFF && s->mb_x==0 && (s->mb_y&1)==0 )\n predict_field_decoding_flag(h);\n if( FRAME_MBAFF && (s->mb_y&1)==1 && h->prev_mb_skipped )\n skip = h->next_mb_skipped;\n else\n skip = decode_cabac_mb_skip( h, s->mb_x, s->mb_y );\n if( skip ) {\n if( FRAME_MBAFF && (s->mb_y&1)==0 ){\n s->current_picture.mb_type[mb_xy] = MB_TYPE_SKIP;\n h->next_mb_skipped = decode_cabac_mb_skip( h, s->mb_x, s->mb_y+1 );\n if(!h->next_mb_skipped)\n h->mb_mbaff = h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);\n }\n decode_mb_skip(h);\n h->cbp_table[mb_xy] = 0;\n h->chroma_pred_mode_table[mb_xy] = 0;\n h->last_qscale_diff = 0;\n return 0;\n }\n }\n if(FRAME_MBAFF){\n if( (s->mb_y&1) == 0 )\n h->mb_mbaff =\n h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);\n }\n h->prev_mb_skipped = 0;\n compute_mb_neighbors(h);\n if( h->slice_type_nos == FF_B_TYPE ) {\n mb_type = decode_cabac_mb_type_b( h );\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( get_cabac_noinline( &h->cabac, &h->cabac_state[14] ) == 0 ) {\n if( get_cabac_noinline( &h->cabac, &h->cabac_state[15] ) == 0 ) {\n mb_type= 3 * get_cabac_noinline( &h->cabac, &h->cabac_state[16] );\n } else {\n mb_type= 2 - get_cabac_noinline( &h->cabac, &h->cabac_state[17] );\n }\n partition_count= p_mb_type_info[mb_type].partition_count;\n mb_type= p_mb_type_info[mb_type].type;\n } else {\n mb_type= decode_cabac_intra_mb_type(h, 17, 0);\n goto decode_intra_mb;\n }\n } else {\n mb_type= decode_cabac_intra_mb_type(h, 3, 1);\n if(h->slice_type == FF_SI_TYPE && mb_type)\n mb_type--;\n assert(h->slice_type_nos == FF_I_TYPE);\ndecode_intra_mb:\n partition_count = 0;\n cbp= i_mb_type_info[mb_type].cbp;\n h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;\n mb_type= i_mb_type_info[mb_type].type;\n }\n if(MB_FIELD)\n mb_type |= MB_TYPE_INTERLACED;\n h->slice_table[ mb_xy ]= h->slice_num;\n if(IS_INTRA_PCM(mb_type)) {\n const uint8_t *ptr;\n ptr= h->cabac.bytestream;\n if(h->cabac.low&0x1) ptr--;\n if(CABAC_BITS==16){\n if(h->cabac.low&0x1FF) ptr--;\n }\n memcpy(h->mb, ptr, 256); ptr+=256;\n if(CHROMA){\n memcpy(h->mb+128, ptr, 128); ptr+=128;\n }\n ff_init_cabac_decoder(&h->cabac, ptr, h->cabac.bytestream_end - ptr);\n h->cbp_table[mb_xy] = 0x1ef;\n h->chroma_pred_mode_table[mb_xy] = 0;\n s->current_picture.qscale_table[mb_xy]= 0;\n memset(h->non_zero_count[mb_xy], 16, 16);\n s->current_picture.mb_type[mb_xy]= mb_type;\n h->last_qscale_diff = 0;\n return 0;\n }\n if(MB_MBAFF){\n h->ref_count[0] <<= 1;\n h->ref_count[1] <<= 1;\n }\n fill_caches(h, mb_type, 0);\n if( IS_INTRA( mb_type ) ) {\n int i, pred_mode;\n if( IS_INTRA4x4( mb_type ) ) {\n if( dct8x8_allowed && decode_cabac_mb_transform_size( h ) ) {\n mb_type |= MB_TYPE_8x8DCT;\n for( i = 0; i < 16; i+=4 ) {\n int pred = pred_intra_mode( h, i );\n int mode = decode_cabac_mb_intra4x4_pred_mode( h, pred );\n fill_rectangle( &h->intra4x4_pred_mode_cache[ scan8[i] ], 2, 2, 8, mode, 1 );\n }\n } else {\n for( i = 0; i < 16; i++ ) {\n int pred = pred_intra_mode( h, i );\n h->intra4x4_pred_mode_cache[ scan8[i] ] = decode_cabac_mb_intra4x4_pred_mode( h, pred );\n }\n }\n write_back_intra_pred_mode(h);\n if( check_intra4x4_pred_mode(h) < 0 ) return -1;\n } else {\n h->intra16x16_pred_mode= check_intra_pred_mode( h, h->intra16x16_pred_mode );\n if( h->intra16x16_pred_mode < 0 ) return -1;\n }\n if(CHROMA){\n h->chroma_pred_mode_table[mb_xy] =\n pred_mode = decode_cabac_mb_chroma_pre_mode( h );\n pred_mode= check_intra_pred_mode( h, pred_mode );\n if( pred_mode < 0 ) 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] = decode_cabac_b_mb_sub_type( h );\n sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;\n h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type;\n }\n if( IS_DIRECT(h->sub_mb_type[0] | h->sub_mb_type[1] |\n h->sub_mb_type[2] | h->sub_mb_type[3]) ) {\n 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 if( h->ref_count[0] > 1 || h->ref_count[1] > 1 ) {\n for( i = 0; i < 4; i++ )\n if( IS_DIRECT(h->sub_mb_type[i]) )\n fill_rectangle( &h->direct_cache[scan8[4*i]], 2, 2, 8, 1, 1 );\n }\n }\n } else {\n for( i = 0; i < 4; i++ ) {\n h->sub_mb_type[i] = decode_cabac_p_mb_sub_type( h );\n sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;\n h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type;\n }\n }\n for( list = 0; list < h->list_count; list++ ) {\n for( i = 0; i < 4; i++ ) {\n if(IS_DIRECT(h->sub_mb_type[i])) continue;\n if(IS_DIR(h->sub_mb_type[i], 0, list)){\n if( h->ref_count[list] > 1 ){\n ref[list][i] = decode_cabac_mb_ref( h, list, 4*i );\n if(ref[list][i] >= (unsigned)h->ref_count[list]){\n av_log(s->avctx, AV_LOG_ERROR, "Reference %d >= %d\\n", ref[list][i], h->ref_count[list]);\n return -1;\n }\n }else\n ref[list][i] = 0;\n } else {\n ref[list][i] = -1;\n }\n h->ref_cache[list][ scan8[4*i]+1 ]=\n h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i];\n }\n }\n if(dct8x8_allowed)\n dct8x8_allowed = get_dct8x8_allowed(h);\n for(list=0; list<h->list_count; list++){\n for(i=0; i<4; i++){\n h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ];\n if(IS_DIRECT(h->sub_mb_type[i])){\n fill_rectangle(h->mvd_cache[list][scan8[4*i]], 2, 2, 8, 0, 4);\n continue;\n }\n if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){\n const int sub_mb_type= h->sub_mb_type[i];\n const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1;\n for(j=0; j<sub_partition_count[i]; j++){\n int mpx, mpy;\n int mx, my;\n const int index= 4*i + block_width*j;\n int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ];\n int16_t (* mvd_cache)[2]= &h->mvd_cache[list][ scan8[index] ];\n pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mpx, &mpy);\n mx = mpx + decode_cabac_mb_mvd( h, list, index, 0 );\n my = mpy + decode_cabac_mb_mvd( h, list, index, 1 );\n tprintf(s->avctx, "final mv:%d %d\\n", mx, my);\n if(IS_SUB_8X8(sub_mb_type)){\n mv_cache[ 1 ][0]=\n mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx;\n mv_cache[ 1 ][1]=\n mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my;\n mvd_cache[ 1 ][0]=\n mvd_cache[ 8 ][0]= mvd_cache[ 9 ][0]= mx - mpx;\n mvd_cache[ 1 ][1]=\n mvd_cache[ 8 ][1]= mvd_cache[ 9 ][1]= my - mpy;\n }else if(IS_SUB_8X4(sub_mb_type)){\n mv_cache[ 1 ][0]= mx;\n mv_cache[ 1 ][1]= my;\n mvd_cache[ 1 ][0]= mx - mpx;\n mvd_cache[ 1 ][1]= my - mpy;\n }else if(IS_SUB_4X8(sub_mb_type)){\n mv_cache[ 8 ][0]= mx;\n mv_cache[ 8 ][1]= my;\n mvd_cache[ 8 ][0]= mx - mpx;\n mvd_cache[ 8 ][1]= my - mpy;\n }\n mv_cache[ 0 ][0]= mx;\n mv_cache[ 0 ][1]= my;\n mvd_cache[ 0 ][0]= mx - mpx;\n mvd_cache[ 0 ][1]= my - mpy;\n }\n }else{\n uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0];\n uint32_t *pd= (uint32_t *)&h->mvd_cache[list][ scan8[4*i] ][0];\n p[0] = p[1] = p[8] = p[9] = 0;\n pd[0]= pd[1]= pd[8]= pd[9]= 0;\n }\n }\n }\n } else if( IS_DIRECT(mb_type) ) {\n pred_direct_motion(h, &mb_type);\n fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);\n fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 4);\n dct8x8_allowed &= h->sps.direct_8x8_inference_flag;\n } else {\n int list, mx, my, i, mpx, mpy;\n if(IS_16X16(mb_type)){\n for(list=0; list<h->list_count; list++){\n if(IS_DIR(mb_type, 0, list)){\n int ref;\n if(h->ref_count[list] > 1){\n ref= decode_cabac_mb_ref(h, list, 0);\n if(ref >= (unsigned)h->ref_count[list]){\n av_log(s->avctx, AV_LOG_ERROR, "Reference %d >= %d\\n", ref, h->ref_count[list]);\n return -1;\n }\n }else\n ref=0;\n fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, ref, 1);\n }else\n fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, (uint8_t)LIST_NOT_USED, 1);\n }\n for(list=0; list<h->list_count; list++){\n if(IS_DIR(mb_type, 0, list)){\n pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mpx, &mpy);\n mx = mpx + decode_cabac_mb_mvd( h, list, 0, 0 );\n my = mpy + decode_cabac_mb_mvd( h, list, 0, 1 );\n tprintf(s->avctx, "final mv:%d %d\\n", mx, my);\n fill_rectangle(h->mvd_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx-mpx,my-mpy), 4);\n fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, pack16to32(mx,my), 4);\n }else\n fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, 0, 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 if(IS_DIR(mb_type, i, list)){\n int ref;\n if(h->ref_count[list] > 1){\n ref= decode_cabac_mb_ref( h, list, 8*i );\n if(ref >= (unsigned)h->ref_count[list]){\n av_log(s->avctx, AV_LOG_ERROR, "Reference %d >= %d\\n", ref, h->ref_count[list]);\n return -1;\n }\n }else\n ref=0;\n fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, ref, 1);\n }else\n fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (LIST_NOT_USED&0xFF), 1);\n }\n }\n for(list=0; list<h->list_count; list++){\n for(i=0; i<2; i++){\n if(IS_DIR(mb_type, i, list)){\n pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mpx, &mpy);\n mx = mpx + decode_cabac_mb_mvd( h, list, 8*i, 0 );\n my = mpy + decode_cabac_mb_mvd( h, list, 8*i, 1 );\n tprintf(s->avctx, "final mv:%d %d\\n", mx, my);\n fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx-mpx,my-mpy), 4);\n fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, pack16to32(mx,my), 4);\n }else{\n fill_rectangle(h->mvd_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);\n fill_rectangle(h-> mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, 0, 4);\n }\n }\n }\n }else{\n assert(IS_8X16(mb_type));\n for(list=0; list<h->list_count; list++){\n for(i=0; i<2; i++){\n if(IS_DIR(mb_type, i, list)){\n int ref;\n if(h->ref_count[list] > 1){\n ref= decode_cabac_mb_ref( h, list, 4*i );\n if(ref >= (unsigned)h->ref_count[list]){\n av_log(s->avctx, AV_LOG_ERROR, "Reference %d >= %d\\n", ref, h->ref_count[list]);\n return -1;\n }\n }else\n ref=0;\n fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, ref, 1);\n }else\n fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (LIST_NOT_USED&0xFF), 1);\n }\n }\n for(list=0; list<h->list_count; list++){\n for(i=0; i<2; i++){\n if(IS_DIR(mb_type, i, list)){\n pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mpx, &mpy);\n mx = mpx + decode_cabac_mb_mvd( h, list, 4*i, 0 );\n my = mpy + decode_cabac_mb_mvd( h, list, 4*i, 1 );\n tprintf(s->avctx, "final mv:%d %d\\n", mx, my);\n fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx-mpx,my-mpy), 4);\n fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, pack16to32(mx,my), 4);\n }else{\n fill_rectangle(h->mvd_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);\n fill_rectangle(h-> mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, 0, 4);\n }\n }\n }\n }\n }\n if( IS_INTER( mb_type ) ) {\n h->chroma_pred_mode_table[mb_xy] = 0;\n write_back_motion( h, mb_type );\n }\n if( !IS_INTRA16x16( mb_type ) ) {\n cbp = decode_cabac_mb_cbp_luma( h );\n if(CHROMA)\n cbp |= decode_cabac_mb_cbp_chroma( h ) << 4;\n }\n h->cbp_table[mb_xy] = h->cbp = cbp;\n if( dct8x8_allowed && (cbp&15) && !IS_INTRA( mb_type ) ) {\n if( decode_cabac_mb_transform_size( h ) )\n mb_type |= MB_TYPE_8x8DCT;\n }\n s->current_picture.mb_type[mb_xy]= mb_type;\n if( cbp || IS_INTRA16x16( mb_type ) ) {\n const uint8_t *scan, *scan8x8, *dc_scan;\n const uint32_t *qmul;\n int dqp;\n if(IS_INTERLACED(mb_type)){\n scan8x8= s->qscale ? h->field_scan8x8 : h->field_scan8x8_q0;\n scan= s->qscale ? h->field_scan : h->field_scan_q0;\n dc_scan= luma_dc_field_scan;\n }else{\n scan8x8= s->qscale ? h->zigzag_scan8x8 : h->zigzag_scan8x8_q0;\n scan= s->qscale ? h->zigzag_scan : h->zigzag_scan_q0;\n dc_scan= luma_dc_zigzag_scan;\n }\n h->last_qscale_diff = dqp = decode_cabac_mb_dqp( h );\n if( dqp == INT_MIN ){\n av_log(h->s.avctx, AV_LOG_ERROR, "cabac decode of qscale diff failed at %d %d\\n", s->mb_x, s->mb_y);\n return -1;\n }\n s->qscale += dqp;\n if(((unsigned)s->qscale) > 51){\n if(s->qscale<0) s->qscale+= 52;\n else s->qscale-= 52;\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 int i;\n decode_cabac_residual( h, h->mb, 0, 0, dc_scan, NULL, 16);\n if( cbp&15 ) {\n qmul = h->dequant4_coeff[0][s->qscale];\n for( i = 0; i < 16; i++ ) {\n decode_cabac_residual(h, h->mb + 16*i, 1, i, scan + 1, qmul, 15);\n }\n } else {\n fill_rectangle(&h->non_zero_count_cache[scan8[0]], 4, 4, 8, 0, 1);\n }\n } else {\n int i8x8, i4x4;\n for( i8x8 = 0; i8x8 < 4; i8x8++ ) {\n if( cbp & (1<<i8x8) ) {\n if( IS_8x8DCT(mb_type) ) {\n decode_cabac_residual(h, h->mb + 64*i8x8, 5, 4*i8x8,\n scan8x8, h->dequant8_coeff[IS_INTRA( mb_type ) ? 0:1][s->qscale], 64);\n } else {\n qmul = h->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][s->qscale];\n for( i4x4 = 0; i4x4 < 4; i4x4++ ) {\n const int index = 4*i8x8 + i4x4;\n decode_cabac_residual(h, h->mb + 16*index, 2, index, scan, qmul, 16);\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 int c;\n for( c = 0; c < 2; c++ ) {\n decode_cabac_residual(h, h->mb + 256 + 16*4*c, 3, c, chroma_dc_scan, NULL, 4);\n }\n }\n if( cbp&0x20 ) {\n int c, i;\n for( c = 0; c < 2; c++ ) {\n qmul = h->dequant4_coeff[c+1+(IS_INTRA( mb_type ) ? 0:3)][h->chroma_qp[c]];\n for( i = 0; i < 4; i++ ) {\n const int index = 16 + 4 * c + i;\n decode_cabac_residual(h, h->mb + 16*index, 4, index, scan + 1, qmul, 15);\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 h->last_qscale_diff = 0;\n }\n s->current_picture.qscale_table[mb_xy]= s->qscale;\n write_back_non_zero_count(h);\n if(MB_MBAFF){\n h->ref_count[0] >>= 1;\n h->ref_count[1] >>= 1;\n }\n return 0;\n}', 'static void fill_caches(H264Context *h, int mb_type, int for_deblock){\n MpegEncContext * const s = &h->s;\n const int mb_xy= h->mb_xy;\n int topleft_xy, top_xy, topright_xy, left_xy[2];\n int topleft_type, top_type, topright_type, left_type[2];\n const uint8_t * left_block;\n int topleft_partition= -1;\n int i;\n top_xy = mb_xy - (s->mb_stride << FIELD_PICTURE);\n if(for_deblock && (h->slice_num == 1 || h->slice_table[mb_xy] == h->slice_table[top_xy]) && !FRAME_MBAFF)\n return;\n topleft_xy = top_xy - 1;\n topright_xy= top_xy + 1;\n left_xy[1] = left_xy[0] = mb_xy-1;\n left_block = left_block_options[0];\n if(FRAME_MBAFF){\n const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;\n const int top_pair_xy = pair_xy - s->mb_stride;\n const int topleft_pair_xy = top_pair_xy - 1;\n const int topright_pair_xy = top_pair_xy + 1;\n const int topleft_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[topleft_pair_xy]);\n const int top_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[top_pair_xy]);\n const int topright_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[topright_pair_xy]);\n const int left_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[pair_xy-1]);\n const int curr_mb_field_flag = IS_INTERLACED(mb_type);\n const int bottom = (s->mb_y & 1);\n tprintf(s->avctx, "fill_caches: curr_mb_field_flag:%d, left_mb_field_flag:%d, topleft_mb_field_flag:%d, top_mb_field_flag:%d, topright_mb_field_flag:%d\\n", curr_mb_field_flag, left_mb_field_flag, topleft_mb_field_flag, top_mb_field_flag, topright_mb_field_flag);\n if (curr_mb_field_flag && (bottom || top_mb_field_flag)){\n top_xy -= s->mb_stride;\n }\n if (curr_mb_field_flag && (bottom || topleft_mb_field_flag)){\n topleft_xy -= s->mb_stride;\n } else if(bottom && !curr_mb_field_flag && left_mb_field_flag) {\n topleft_xy += s->mb_stride;\n topleft_partition = 0;\n }\n if (curr_mb_field_flag && (bottom || topright_mb_field_flag)){\n topright_xy -= s->mb_stride;\n }\n if (left_mb_field_flag != curr_mb_field_flag) {\n left_xy[1] = left_xy[0] = pair_xy - 1;\n if (curr_mb_field_flag) {\n left_xy[1] += s->mb_stride;\n left_block = left_block_options[3];\n } else {\n left_block= left_block_options[2 - bottom];\n }\n }\n }\n h->top_mb_xy = top_xy;\n h->left_mb_xy[0] = left_xy[0];\n h->left_mb_xy[1] = left_xy[1];\n if(for_deblock){\n topleft_type = 0;\n topright_type = 0;\n top_type = h->slice_table[top_xy ] < 0xFFFF ? s->current_picture.mb_type[top_xy] : 0;\n left_type[0] = h->slice_table[left_xy[0] ] < 0xFFFF ? s->current_picture.mb_type[left_xy[0]] : 0;\n left_type[1] = h->slice_table[left_xy[1] ] < 0xFFFF ? s->current_picture.mb_type[left_xy[1]] : 0;\n if(MB_MBAFF && !IS_INTRA(mb_type)){\n int list;\n for(list=0; list<h->list_count; list++){\n if(USES_LIST(mb_type,list)){\n int8_t *ref = &s->current_picture.ref_index[list][h->mb2b8_xy[mb_xy]];\n *(uint32_t*)&h->ref_cache[list][scan8[ 0]] =\n *(uint32_t*)&h->ref_cache[list][scan8[ 2]] = (pack16to32(ref[0],ref[1])&0x00FF00FF)*0x0101;\n ref += h->b8_stride;\n *(uint32_t*)&h->ref_cache[list][scan8[ 8]] =\n *(uint32_t*)&h->ref_cache[list][scan8[10]] = (pack16to32(ref[0],ref[1])&0x00FF00FF)*0x0101;\n }\n }\n }\n }else{\n topleft_type = h->slice_table[topleft_xy ] == h->slice_num ? s->current_picture.mb_type[topleft_xy] : 0;\n top_type = h->slice_table[top_xy ] == h->slice_num ? s->current_picture.mb_type[top_xy] : 0;\n topright_type= h->slice_table[topright_xy] == h->slice_num ? s->current_picture.mb_type[topright_xy]: 0;\n left_type[0] = h->slice_table[left_xy[0] ] == h->slice_num ? s->current_picture.mb_type[left_xy[0]] : 0;\n left_type[1] = h->slice_table[left_xy[1] ] == h->slice_num ? s->current_picture.mb_type[left_xy[1]] : 0;\n if(IS_INTRA(mb_type)){\n int type_mask= h->pps.constrained_intra_pred ? IS_INTRA(-1) : -1;\n h->topleft_samples_available=\n h->top_samples_available=\n h->left_samples_available= 0xFFFF;\n h->topright_samples_available= 0xEEEA;\n if(!(top_type & type_mask)){\n h->topleft_samples_available= 0xB3FF;\n h->top_samples_available= 0x33FF;\n h->topright_samples_available= 0x26EA;\n }\n if(IS_INTERLACED(mb_type) != IS_INTERLACED(left_type[0])){\n if(IS_INTERLACED(mb_type)){\n if(!(left_type[0] & type_mask)){\n h->topleft_samples_available&= 0xDFFF;\n h->left_samples_available&= 0x5FFF;\n }\n if(!(left_type[1] & type_mask)){\n h->topleft_samples_available&= 0xFF5F;\n h->left_samples_available&= 0xFF5F;\n }\n }else{\n int left_typei = h->slice_table[left_xy[0] + s->mb_stride ] == h->slice_num\n ? s->current_picture.mb_type[left_xy[0] + s->mb_stride] : 0;\n assert(left_xy[0] == left_xy[1]);\n if(!((left_typei & type_mask) && (left_type[0] & type_mask))){\n h->topleft_samples_available&= 0xDF5F;\n h->left_samples_available&= 0x5F5F;\n }\n }\n }else{\n if(!(left_type[0] & type_mask)){\n h->topleft_samples_available&= 0xDF5F;\n h->left_samples_available&= 0x5F5F;\n }\n }\n if(!(topleft_type & type_mask))\n h->topleft_samples_available&= 0x7FFF;\n if(!(topright_type & type_mask))\n h->topright_samples_available&= 0xFBFF;\n if(IS_INTRA4x4(mb_type)){\n if(IS_INTRA4x4(top_type)){\n h->intra4x4_pred_mode_cache[4+8*0]= h->intra4x4_pred_mode[top_xy][4];\n h->intra4x4_pred_mode_cache[5+8*0]= h->intra4x4_pred_mode[top_xy][5];\n h->intra4x4_pred_mode_cache[6+8*0]= h->intra4x4_pred_mode[top_xy][6];\n h->intra4x4_pred_mode_cache[7+8*0]= h->intra4x4_pred_mode[top_xy][3];\n }else{\n int pred;\n if(!(top_type & type_mask))\n pred= -1;\n else{\n pred= 2;\n }\n h->intra4x4_pred_mode_cache[4+8*0]=\n h->intra4x4_pred_mode_cache[5+8*0]=\n h->intra4x4_pred_mode_cache[6+8*0]=\n h->intra4x4_pred_mode_cache[7+8*0]= pred;\n }\n for(i=0; i<2; i++){\n if(IS_INTRA4x4(left_type[i])){\n h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[0+2*i]];\n h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[1+2*i]];\n }else{\n int pred;\n if(!(left_type[i] & type_mask))\n pred= -1;\n else{\n pred= 2;\n }\n h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=\n h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= pred;\n }\n }\n }\n }\n }\n if(top_type){\n h->non_zero_count_cache[4+8*0]= h->non_zero_count[top_xy][4];\n h->non_zero_count_cache[5+8*0]= h->non_zero_count[top_xy][5];\n h->non_zero_count_cache[6+8*0]= h->non_zero_count[top_xy][6];\n h->non_zero_count_cache[7+8*0]= h->non_zero_count[top_xy][3];\n h->non_zero_count_cache[1+8*0]= h->non_zero_count[top_xy][9];\n h->non_zero_count_cache[2+8*0]= h->non_zero_count[top_xy][8];\n h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][12];\n h->non_zero_count_cache[2+8*3]= h->non_zero_count[top_xy][11];\n }else{\n h->non_zero_count_cache[4+8*0]=\n h->non_zero_count_cache[5+8*0]=\n h->non_zero_count_cache[6+8*0]=\n h->non_zero_count_cache[7+8*0]=\n h->non_zero_count_cache[1+8*0]=\n h->non_zero_count_cache[2+8*0]=\n h->non_zero_count_cache[1+8*3]=\n h->non_zero_count_cache[2+8*3]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;\n }\n for (i=0; i<2; i++) {\n if(left_type[i]){\n h->non_zero_count_cache[3+8*1 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[0+2*i]];\n h->non_zero_count_cache[3+8*2 + 2*8*i]= h->non_zero_count[left_xy[i]][left_block[1+2*i]];\n h->non_zero_count_cache[0+8*1 + 8*i]= h->non_zero_count[left_xy[i]][left_block[4+2*i]];\n h->non_zero_count_cache[0+8*4 + 8*i]= h->non_zero_count[left_xy[i]][left_block[5+2*i]];\n }else{\n h->non_zero_count_cache[3+8*1 + 2*8*i]=\n h->non_zero_count_cache[3+8*2 + 2*8*i]=\n h->non_zero_count_cache[0+8*1 + 8*i]=\n h->non_zero_count_cache[0+8*4 + 8*i]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;\n }\n }\n if( h->pps.cabac ) {\n if(top_type) {\n h->top_cbp = h->cbp_table[top_xy];\n } else if(IS_INTRA(mb_type)) {\n h->top_cbp = 0x1C0;\n } else {\n h->top_cbp = 0;\n }\n if (left_type[0]) {\n h->left_cbp = h->cbp_table[left_xy[0]] & 0x1f0;\n } else if(IS_INTRA(mb_type)) {\n h->left_cbp = 0x1C0;\n } else {\n h->left_cbp = 0;\n }\n if (left_type[0]) {\n h->left_cbp |= ((h->cbp_table[left_xy[0]]>>((left_block[0]&(~1))+1))&0x1) << 1;\n }\n if (left_type[1]) {\n h->left_cbp |= ((h->cbp_table[left_xy[1]]>>((left_block[2]&(~1))+1))&0x1) << 3;\n }\n }\n#if 1\n if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){\n int list;\n for(list=0; list<h->list_count; list++){\n if(!USES_LIST(mb_type, list) && !IS_DIRECT(mb_type) && !h->deblocking_filter){\n continue;\n }\n h->mv_cache_clean[list]= 0;\n if(USES_LIST(top_type, list)){\n const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;\n const int b8_xy= h->mb2b8_xy[top_xy] + h->b8_stride;\n *(uint32_t*)h->mv_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 0];\n *(uint32_t*)h->mv_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 1];\n *(uint32_t*)h->mv_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 2];\n *(uint32_t*)h->mv_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 3];\n h->ref_cache[list][scan8[0] + 0 - 1*8]=\n h->ref_cache[list][scan8[0] + 1 - 1*8]= s->current_picture.ref_index[list][b8_xy + 0];\n h->ref_cache[list][scan8[0] + 2 - 1*8]=\n h->ref_cache[list][scan8[0] + 3 - 1*8]= s->current_picture.ref_index[list][b8_xy + 1];\n }else{\n *(uint32_t*)h->mv_cache [list][scan8[0] + 0 - 1*8]=\n *(uint32_t*)h->mv_cache [list][scan8[0] + 1 - 1*8]=\n *(uint32_t*)h->mv_cache [list][scan8[0] + 2 - 1*8]=\n *(uint32_t*)h->mv_cache [list][scan8[0] + 3 - 1*8]= 0;\n *(uint32_t*)&h->ref_cache[list][scan8[0] + 0 - 1*8]= ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101;\n }\n for(i=0; i<2; i++){\n int cache_idx = scan8[0] - 1 + i*2*8;\n if(USES_LIST(left_type[i], list)){\n const int b_xy= h->mb2b_xy[left_xy[i]] + 3;\n const int b8_xy= h->mb2b8_xy[left_xy[i]] + 1;\n *(uint32_t*)h->mv_cache[list][cache_idx ]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[0+i*2]];\n *(uint32_t*)h->mv_cache[list][cache_idx+8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[1+i*2]];\n h->ref_cache[list][cache_idx ]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[0+i*2]>>1)];\n h->ref_cache[list][cache_idx+8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[1+i*2]>>1)];\n }else{\n *(uint32_t*)h->mv_cache [list][cache_idx ]=\n *(uint32_t*)h->mv_cache [list][cache_idx+8]= 0;\n h->ref_cache[list][cache_idx ]=\n h->ref_cache[list][cache_idx+8]= left_type[i] ? LIST_NOT_USED : PART_NOT_AVAILABLE;\n }\n }\n if(for_deblock || ((IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred) && !FRAME_MBAFF))\n continue;\n if(USES_LIST(topleft_type, list)){\n const int b_xy = h->mb2b_xy[topleft_xy] + 3 + h->b_stride + (topleft_partition & 2*h->b_stride);\n const int b8_xy= h->mb2b8_xy[topleft_xy] + 1 + (topleft_partition & h->b8_stride);\n *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];\n h->ref_cache[list][scan8[0] - 1 - 1*8]= s->current_picture.ref_index[list][b8_xy];\n }else{\n *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= 0;\n h->ref_cache[list][scan8[0] - 1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;\n }\n if(USES_LIST(topright_type, list)){\n const int b_xy= h->mb2b_xy[topright_xy] + 3*h->b_stride;\n const int b8_xy= h->mb2b8_xy[topright_xy] + h->b8_stride;\n *(uint32_t*)h->mv_cache[list][scan8[0] + 4 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];\n h->ref_cache[list][scan8[0] + 4 - 1*8]= s->current_picture.ref_index[list][b8_xy];\n }else{\n *(uint32_t*)h->mv_cache [list][scan8[0] + 4 - 1*8]= 0;\n h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;\n }\n if((IS_SKIP(mb_type) || IS_DIRECT(mb_type)) && !FRAME_MBAFF)\n continue;\n h->ref_cache[list][scan8[5 ]+1] =\n h->ref_cache[list][scan8[7 ]+1] =\n h->ref_cache[list][scan8[13]+1] =\n h->ref_cache[list][scan8[4 ]] =\n h->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE;\n *(uint32_t*)h->mv_cache [list][scan8[5 ]+1]=\n *(uint32_t*)h->mv_cache [list][scan8[7 ]+1]=\n *(uint32_t*)h->mv_cache [list][scan8[13]+1]=\n *(uint32_t*)h->mv_cache [list][scan8[4 ]]=\n *(uint32_t*)h->mv_cache [list][scan8[12]]= 0;\n if( h->pps.cabac ) {\n if(USES_LIST(top_type, list)){\n const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;\n *(uint32_t*)h->mvd_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 0];\n *(uint32_t*)h->mvd_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 1];\n *(uint32_t*)h->mvd_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 2];\n *(uint32_t*)h->mvd_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 3];\n }else{\n *(uint32_t*)h->mvd_cache [list][scan8[0] + 0 - 1*8]=\n *(uint32_t*)h->mvd_cache [list][scan8[0] + 1 - 1*8]=\n *(uint32_t*)h->mvd_cache [list][scan8[0] + 2 - 1*8]=\n *(uint32_t*)h->mvd_cache [list][scan8[0] + 3 - 1*8]= 0;\n }\n if(USES_LIST(left_type[0], list)){\n const int b_xy= h->mb2b_xy[left_xy[0]] + 3;\n *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 0*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[0]];\n *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[1]];\n }else{\n *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 0*8]=\n *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 1*8]= 0;\n }\n if(USES_LIST(left_type[1], list)){\n const int b_xy= h->mb2b_xy[left_xy[1]] + 3;\n *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 2*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[2]];\n *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 3*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[3]];\n }else{\n *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 2*8]=\n *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 3*8]= 0;\n }\n *(uint32_t*)h->mvd_cache [list][scan8[5 ]+1]=\n *(uint32_t*)h->mvd_cache [list][scan8[7 ]+1]=\n *(uint32_t*)h->mvd_cache [list][scan8[13]+1]=\n *(uint32_t*)h->mvd_cache [list][scan8[4 ]]=\n *(uint32_t*)h->mvd_cache [list][scan8[12]]= 0;\n if(h->slice_type_nos == FF_B_TYPE){\n fill_rectangle(&h->direct_cache[scan8[0]], 4, 4, 8, 0, 1);\n if(IS_DIRECT(top_type)){\n *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0x01010101;\n }else if(IS_8X8(top_type)){\n int b8_xy = h->mb2b8_xy[top_xy] + h->b8_stride;\n h->direct_cache[scan8[0] + 0 - 1*8]= h->direct_table[b8_xy];\n h->direct_cache[scan8[0] + 2 - 1*8]= h->direct_table[b8_xy + 1];\n }else{\n *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0;\n }\n if(IS_DIRECT(left_type[0]))\n h->direct_cache[scan8[0] - 1 + 0*8]= 1;\n else if(IS_8X8(left_type[0]))\n h->direct_cache[scan8[0] - 1 + 0*8]= h->direct_table[h->mb2b8_xy[left_xy[0]] + 1 + h->b8_stride*(left_block[0]>>1)];\n else\n h->direct_cache[scan8[0] - 1 + 0*8]= 0;\n if(IS_DIRECT(left_type[1]))\n h->direct_cache[scan8[0] - 1 + 2*8]= 1;\n else if(IS_8X8(left_type[1]))\n h->direct_cache[scan8[0] - 1 + 2*8]= h->direct_table[h->mb2b8_xy[left_xy[1]] + 1 + h->b8_stride*(left_block[2]>>1)];\n else\n h->direct_cache[scan8[0] - 1 + 2*8]= 0;\n }\n }\n if(FRAME_MBAFF){\n#define MAP_MVS\\\n MAP_F2F(scan8[0] - 1 - 1*8, topleft_type)\\\n MAP_F2F(scan8[0] + 0 - 1*8, top_type)\\\n MAP_F2F(scan8[0] + 1 - 1*8, top_type)\\\n MAP_F2F(scan8[0] + 2 - 1*8, top_type)\\\n MAP_F2F(scan8[0] + 3 - 1*8, top_type)\\\n MAP_F2F(scan8[0] + 4 - 1*8, topright_type)\\\n MAP_F2F(scan8[0] - 1 + 0*8, left_type[0])\\\n MAP_F2F(scan8[0] - 1 + 1*8, left_type[0])\\\n MAP_F2F(scan8[0] - 1 + 2*8, left_type[1])\\\n MAP_F2F(scan8[0] - 1 + 3*8, left_type[1])\n if(MB_FIELD){\n#define MAP_F2F(idx, mb_type)\\\n if(!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\\\n h->ref_cache[list][idx] <<= 1;\\\n h->mv_cache[list][idx][1] /= 2;\\\n h->mvd_cache[list][idx][1] /= 2;\\\n }\n MAP_MVS\n#undef MAP_F2F\n }else{\n#define MAP_F2F(idx, mb_type)\\\n if(IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\\\n h->ref_cache[list][idx] >>= 1;\\\n h->mv_cache[list][idx][1] <<= 1;\\\n h->mvd_cache[list][idx][1] <<= 1;\\\n }\n MAP_MVS\n#undef MAP_F2F\n }\n }\n }\n }\n#endif\n h->neighbor_transform_size= !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[0]);\n}', 'static void decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff ) {\n#if CONFIG_SMALL\n decode_cabac_residual_internal(h, block, cat, n, scantable, qmul, max_coeff, cat == 0 || cat == 3);\n#else\n if( cat == 0 || cat == 3 ) decode_cabac_residual_dc(h, block, cat, n, scantable, qmul, max_coeff);\n else decode_cabac_residual_nondc(h, block, cat, n, scantable, qmul, max_coeff);\n#endif\n}', 'static void decode_cabac_residual_dc( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff ) {\n decode_cabac_residual_internal(h, block, cat, n, scantable, qmul, max_coeff, 1);\n}', 'static av_always_inline void decode_cabac_residual_internal( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff, int is_dc ) {\n static const int significant_coeff_flag_offset[2][6] = {\n { 105+0, 105+15, 105+29, 105+44, 105+47, 402 },\n { 277+0, 277+15, 277+29, 277+44, 277+47, 436 }\n };\n static const int last_coeff_flag_offset[2][6] = {\n { 166+0, 166+15, 166+29, 166+44, 166+47, 417 },\n { 338+0, 338+15, 338+29, 338+44, 338+47, 451 }\n };\n static const int coeff_abs_level_m1_offset[6] = {\n 227+0, 227+10, 227+20, 227+30, 227+39, 426\n };\n static const uint8_t significant_coeff_flag_offset_8x8[2][63] = {\n { 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,\n 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7,\n 7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11,\n 12,13,11, 6, 9,14,10, 9,11,12,13,11,14,10,12 },\n { 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 7, 8, 4, 5,\n 6, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,11,12,11,\n 9, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,13,13, 9,\n 9,10,10, 8,13,13, 9, 9,10,10,14,14,14,14,14 }\n };\n static const uint8_t coeff_abs_level1_ctx[8] = { 1, 2, 3, 4, 0, 0, 0, 0 };\n static const uint8_t coeff_abs_levelgt1_ctx[8] = { 5, 5, 5, 5, 6, 7, 8, 9 };\n static const uint8_t coeff_abs_level_transition[2][8] = {\n { 1, 2, 3, 3, 4, 5, 6, 7 },\n { 4, 4, 4, 4, 5, 6, 7, 7 }\n };\n int index[64];\n int av_unused last;\n int coeff_count = 0;\n int node_ctx = 0;\n uint8_t *significant_coeff_ctx_base;\n uint8_t *last_coeff_ctx_base;\n uint8_t *abs_level_m1_ctx_base;\n#if !ARCH_X86\n#define CABAC_ON_STACK\n#endif\n#ifdef CABAC_ON_STACK\n#define CC &cc\n CABACContext cc;\n cc.range = h->cabac.range;\n cc.low = h->cabac.low;\n cc.bytestream= h->cabac.bytestream;\n#else\n#define CC &h->cabac\n#endif\n if( is_dc || cat != 5 ) {\n if( get_cabac( CC, &h->cabac_state[85 + get_cabac_cbf_ctx( h, cat, n, is_dc ) ] ) == 0 ) {\n if( !is_dc )\n h->non_zero_count_cache[scan8[n]] = 0;\n#ifdef CABAC_ON_STACK\n h->cabac.range = cc.range ;\n h->cabac.low = cc.low ;\n h->cabac.bytestream= cc.bytestream;\n#endif\n return;\n }\n }\n significant_coeff_ctx_base = h->cabac_state\n + significant_coeff_flag_offset[MB_FIELD][cat];\n last_coeff_ctx_base = h->cabac_state\n + last_coeff_flag_offset[MB_FIELD][cat];\n abs_level_m1_ctx_base = h->cabac_state\n + coeff_abs_level_m1_offset[cat];\n if( !is_dc && cat == 5 ) {\n#define DECODE_SIGNIFICANCE( coefs, sig_off, last_off ) \\\n for(last= 0; last < coefs; last++) { \\\n uint8_t *sig_ctx = significant_coeff_ctx_base + sig_off; \\\n if( get_cabac( CC, sig_ctx )) { \\\n uint8_t *last_ctx = last_coeff_ctx_base + last_off; \\\n index[coeff_count++] = last; \\\n if( get_cabac( CC, last_ctx ) ) { \\\n last= max_coeff; \\\n break; \\\n } \\\n } \\\n }\\\n if( last == max_coeff -1 ) {\\\n index[coeff_count++] = last;\\\n }\n const uint8_t *sig_off = significant_coeff_flag_offset_8x8[MB_FIELD];\n#if ARCH_X86 && HAVE_7REGS && HAVE_EBX_AVAILABLE && !defined(BROKEN_RELOCATIONS)\n coeff_count= decode_significance_8x8_x86(CC, significant_coeff_ctx_base, index, sig_off);\n } else {\n coeff_count= decode_significance_x86(CC, max_coeff, significant_coeff_ctx_base, index);\n#else\n DECODE_SIGNIFICANCE( 63, sig_off[last], last_coeff_flag_offset_8x8[last] );\n } else {\n DECODE_SIGNIFICANCE( max_coeff - 1, last, last );\n#endif\n }\n assert(coeff_count > 0);\n if( is_dc ) {\n if( cat == 0 )\n h->cbp_table[h->mb_xy] |= 0x100;\n else\n h->cbp_table[h->mb_xy] |= 0x40 << n;\n } else {\n if( cat == 5 )\n fill_rectangle(&h->non_zero_count_cache[scan8[n]], 2, 2, 8, coeff_count, 1);\n else {\n assert( cat == 1 || cat == 2 || cat == 4 );\n h->non_zero_count_cache[scan8[n]] = coeff_count;\n }\n }\n do {\n uint8_t *ctx = coeff_abs_level1_ctx[node_ctx] + abs_level_m1_ctx_base;\n int j= scantable[index[--coeff_count]];\n if( get_cabac( CC, ctx ) == 0 ) {\n node_ctx = coeff_abs_level_transition[0][node_ctx];\n if( is_dc ) {\n block[j] = get_cabac_bypass_sign( CC, -1);\n }else{\n block[j] = (get_cabac_bypass_sign( CC, -qmul[j]) + 32) >> 6;\n }\n } else {\n int coeff_abs = 2;\n ctx = coeff_abs_levelgt1_ctx[node_ctx] + abs_level_m1_ctx_base;\n node_ctx = coeff_abs_level_transition[1][node_ctx];\n while( coeff_abs < 15 && get_cabac( CC, ctx ) ) {\n coeff_abs++;\n }\n if( coeff_abs >= 15 ) {\n int j = 0;\n while( get_cabac_bypass( CC ) ) {\n j++;\n }\n coeff_abs=1;\n while( j-- ) {\n coeff_abs += coeff_abs + get_cabac_bypass( CC );\n }\n coeff_abs+= 14;\n }\n if( is_dc ) {\n block[j] = get_cabac_bypass_sign( CC, -coeff_abs );\n }else{\n block[j] = (get_cabac_bypass_sign( CC, -coeff_abs ) * qmul[j] + 32) >> 6;\n }\n }\n } while( coeff_count );\n#ifdef CABAC_ON_STACK\n h->cabac.range = cc.range ;\n h->cabac.low = cc.low ;\n h->cabac.bytestream= cc.bytestream;\n#endif\n}']
2,326
0
https://github.com/libav/libav/blob/e5b0fc170f85b00f7dd0ac514918fb5c95253d39/libavcodec/bitstream.h/#L139
static inline uint64_t get_val(BitstreamContext *bc, unsigned n) { #ifdef BITSTREAM_READER_LE uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1); bc->bits >>= n; #else uint64_t ret = bc->bits >> (64 - n); bc->bits <<= n; #endif bc->bits_left -= n; return ret; }
['static int decode_spectrum(BitstreamContext *bc, float *output)\n{\n int num_subbands, coding_mode, i, j, first, last, subband_size;\n int subband_vlc_index[32], sf_index[32];\n int mantissas[128];\n float scale_factor;\n num_subbands = bitstream_read(bc, 5);\n coding_mode = bitstream_read_bit(bc);\n for (i = 0; i <= num_subbands; i++)\n subband_vlc_index[i] = bitstream_read(bc, 3);\n for (i = 0; i <= num_subbands; i++) {\n if (subband_vlc_index[i] != 0)\n sf_index[i] = bitstream_read(bc, 6);\n }\n for (i = 0; i <= num_subbands; i++) {\n first = subband_tab[i ];\n last = subband_tab[i + 1];\n subband_size = last - first;\n if (subband_vlc_index[i] != 0) {\n read_quant_spectral_coeffs(bc, subband_vlc_index[i], coding_mode,\n mantissas, subband_size);\n scale_factor = ff_atrac_sf_table[sf_index[i]] *\n inv_max_quant[subband_vlc_index[i]];\n for (j = 0; first < last; first++, j++)\n output[first] = mantissas[j] * scale_factor;\n } else {\n memset(output + first, 0, subband_size * sizeof(*output));\n }\n }\n first = subband_tab[i];\n memset(output + first, 0, (SAMPLES_PER_FRAME - first) * sizeof(*output));\n return num_subbands;\n}', 'static inline uint32_t bitstream_read(BitstreamContext *bc, unsigned n)\n{\n if (!n)\n return 0;\n if (n > bc->bits_left) {\n refill_32(bc);\n if (bc->bits_left < 32)\n bc->bits_left = n;\n }\n return get_val(bc, n);\n}', 'static inline unsigned bitstream_read_bit(BitstreamContext *bc)\n{\n if (!bc->bits_left)\n refill_64(bc);\n return get_val(bc, 1);\n}', 'static inline uint64_t get_val(BitstreamContext *bc, unsigned n)\n{\n#ifdef BITSTREAM_READER_LE\n uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);\n bc->bits >>= n;\n#else\n uint64_t ret = bc->bits >> (64 - n);\n bc->bits <<= n;\n#endif\n bc->bits_left -= n;\n return ret;\n}']
2,327
0
https://github.com/libav/libav/blob/645e75992d8876a5e0aa056739885d3afd08d431/ffmpeg.c/#L1390
static void do_video_stats(AVFormatContext *os, AVOutputStream *ost, int frame_size) { AVCodecContext *enc; int frame_number; double ti1, bitrate, avg_bitrate; if (!vstats_file) { vstats_file = fopen(vstats_filename, "w"); if (!vstats_file) { perror("fopen"); ffmpeg_exit(1); } } enc = ost->st->codec; if (enc->codec_type == AVMEDIA_TYPE_VIDEO) { frame_number = ost->frame_number; fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number, enc->coded_frame->quality/(float)FF_QP2LAMBDA); if (enc->flags&CODEC_FLAG_PSNR) fprintf(vstats_file, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0]/(enc->width*enc->height*255.0*255.0))); fprintf(vstats_file,"f_size= %6d ", frame_size); ti1 = ost->sync_opts * av_q2d(enc->time_base); if (ti1 < 0.01) ti1 = 0.01; bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0; avg_bitrate = (double)(video_size * 8) / ti1 / 1000.0; fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ", (double)video_size / 1024, ti1, bitrate, avg_bitrate); fprintf(vstats_file,"type= %c\n", av_get_pict_type_char(enc->coded_frame->pict_type)); } }
['static void do_video_stats(AVFormatContext *os, AVOutputStream *ost,\n int frame_size)\n{\n AVCodecContext *enc;\n int frame_number;\n double ti1, bitrate, avg_bitrate;\n if (!vstats_file) {\n vstats_file = fopen(vstats_filename, "w");\n if (!vstats_file) {\n perror("fopen");\n ffmpeg_exit(1);\n }\n }\n enc = ost->st->codec;\n if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {\n frame_number = ost->frame_number;\n fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number, enc->coded_frame->quality/(float)FF_QP2LAMBDA);\n if (enc->flags&CODEC_FLAG_PSNR)\n fprintf(vstats_file, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0]/(enc->width*enc->height*255.0*255.0)));\n fprintf(vstats_file,"f_size= %6d ", frame_size);\n ti1 = ost->sync_opts * av_q2d(enc->time_base);\n if (ti1 < 0.01)\n ti1 = 0.01;\n bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;\n avg_bitrate = (double)(video_size * 8) / ti1 / 1000.0;\n fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",\n (double)video_size / 1024, ti1, bitrate, avg_bitrate);\n fprintf(vstats_file,"type= %c\\n", av_get_pict_type_char(enc->coded_frame->pict_type));\n }\n}']
2,328
0
https://github.com/openssl/openssl/blob/e075341d66cf35527b37e449a39de9b68cee6bcb/apps/s_server.c/#L734
static int cert_status_cb(SSL *s, void *arg) { tlsextstatusctx *srctx = arg; BIO *err = srctx->err; char *host, *port, *path; int use_ssl; unsigned char *rspder = NULL; int rspderlen; STACK_OF(OPENSSL_STRING) *aia = NULL; X509 *x = NULL; X509_STORE_CTX inctx; X509_OBJECT obj; OCSP_REQUEST *req = NULL; OCSP_RESPONSE *resp = NULL; OCSP_CERTID *id = NULL; STACK_OF(X509_EXTENSION) *exts; int ret = SSL_TLSEXT_ERR_NOACK; int i; #if 0 STACK_OF(OCSP_RESPID) *ids; SSL_get_tlsext_status_ids(s, &ids); BIO_printf(err, "cert_status: received %d ids\n", sk_OCSP_RESPID_num(ids)); #endif if (srctx->verbose) BIO_puts(err, "cert_status: callback called\n"); x = SSL_get_certificate(s); aia = X509_get1_ocsp(x); if (aia) { if (!OCSP_parse_url(sk_OPENSSL_STRING_value(aia, 0), &host, &port, &path, &use_ssl)) { BIO_puts(err, "cert_status: can't parse AIA URL\n"); goto err; } if (srctx->verbose) BIO_printf(err, "cert_status: AIA URL: %s\n", sk_OPENSSL_STRING_value(aia, 0)); } else { if (!srctx->host) { BIO_puts(srctx->err, "cert_status: no AIA and no default responder URL\n"); goto done; } host = srctx->host; path = srctx->path; port = srctx->port; use_ssl = srctx->use_ssl; } if (!X509_STORE_CTX_init(&inctx, SSL_CTX_get_cert_store(SSL_get_SSL_CTX(s)), NULL, NULL)) goto err; if (X509_STORE_get_by_subject(&inctx,X509_LU_X509, X509_get_issuer_name(x),&obj) <= 0) { BIO_puts(err, "cert_status: Can't retrieve issuer certificate.\n"); X509_STORE_CTX_cleanup(&inctx); goto done; } req = OCSP_REQUEST_new(); if (!req) goto err; id = OCSP_cert_to_id(NULL, x, obj.data.x509); X509_free(obj.data.x509); X509_STORE_CTX_cleanup(&inctx); if (!id) goto err; if (!OCSP_request_add0_id(req, id)) goto err; id = NULL; SSL_get_tlsext_status_exts(s, &exts); for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) { X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i); if (!OCSP_REQUEST_add_ext(req, ext, -1)) goto err; } resp = process_responder(err, req, host, path, port, use_ssl, srctx->timeout); if (!resp) { BIO_puts(err, "cert_status: error querying responder\n"); goto done; } rspderlen = i2d_OCSP_RESPONSE(resp, &rspder); if (rspderlen <= 0) goto err; SSL_set_tlsext_status_ocsp_resp(s, rspder, rspderlen); if (srctx->verbose) { BIO_puts(err, "cert_status: ocsp response sent:\n"); OCSP_RESPONSE_print(err, resp, 2); } ret = SSL_TLSEXT_ERR_OK; done: if (ret != SSL_TLSEXT_ERR_OK) ERR_print_errors(err); if (aia) { OPENSSL_free(host); OPENSSL_free(path); OPENSSL_free(port); X509_email_free(aia); } if (id) OCSP_CERTID_free(id); if (req) OCSP_REQUEST_free(req); if (resp) OCSP_RESPONSE_free(resp); return ret; err: ret = SSL_TLSEXT_ERR_ALERT_FATAL; goto done; }
['static int cert_status_cb(SSL *s, void *arg)\n\t{\n\ttlsextstatusctx *srctx = arg;\n\tBIO *err = srctx->err;\n\tchar *host, *port, *path;\n\tint use_ssl;\n\tunsigned char *rspder = NULL;\n\tint rspderlen;\n\tSTACK_OF(OPENSSL_STRING) *aia = NULL;\n\tX509 *x = NULL;\n\tX509_STORE_CTX inctx;\n\tX509_OBJECT obj;\n\tOCSP_REQUEST *req = NULL;\n\tOCSP_RESPONSE *resp = NULL;\n\tOCSP_CERTID *id = NULL;\n\tSTACK_OF(X509_EXTENSION) *exts;\n\tint ret = SSL_TLSEXT_ERR_NOACK;\n\tint i;\n#if 0\nSTACK_OF(OCSP_RESPID) *ids;\nSSL_get_tlsext_status_ids(s, &ids);\nBIO_printf(err, "cert_status: received %d ids\\n", sk_OCSP_RESPID_num(ids));\n#endif\n\tif (srctx->verbose)\n\t\tBIO_puts(err, "cert_status: callback called\\n");\n\tx = SSL_get_certificate(s);\n\taia = X509_get1_ocsp(x);\n\tif (aia)\n\t\t{\n\t\tif (!OCSP_parse_url(sk_OPENSSL_STRING_value(aia, 0),\n\t\t\t&host, &port, &path, &use_ssl))\n\t\t\t{\n\t\t\tBIO_puts(err, "cert_status: can\'t parse AIA URL\\n");\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (srctx->verbose)\n\t\t\tBIO_printf(err, "cert_status: AIA URL: %s\\n",\n\t\t\t\t\tsk_OPENSSL_STRING_value(aia, 0));\n\t\t}\n\telse\n\t\t{\n\t\tif (!srctx->host)\n\t\t\t{\n\t\t\tBIO_puts(srctx->err, "cert_status: no AIA and no default responder URL\\n");\n\t\t\tgoto done;\n\t\t\t}\n\t\thost = srctx->host;\n\t\tpath = srctx->path;\n\t\tport = srctx->port;\n\t\tuse_ssl = srctx->use_ssl;\n\t\t}\n\tif (!X509_STORE_CTX_init(&inctx,\n\t\t\t\tSSL_CTX_get_cert_store(SSL_get_SSL_CTX(s)),\n\t\t\t\tNULL, NULL))\n\t\tgoto err;\n\tif (X509_STORE_get_by_subject(&inctx,X509_LU_X509,\n\t\t\t\tX509_get_issuer_name(x),&obj) <= 0)\n\t\t{\n\t\tBIO_puts(err, "cert_status: Can\'t retrieve issuer certificate.\\n");\n\t\tX509_STORE_CTX_cleanup(&inctx);\n\t\tgoto done;\n\t\t}\n\treq = OCSP_REQUEST_new();\n\tif (!req)\n\t\tgoto err;\n\tid = OCSP_cert_to_id(NULL, x, obj.data.x509);\n\tX509_free(obj.data.x509);\n\tX509_STORE_CTX_cleanup(&inctx);\n\tif (!id)\n\t\tgoto err;\n\tif (!OCSP_request_add0_id(req, id))\n\t\tgoto err;\n\tid = NULL;\n\tSSL_get_tlsext_status_exts(s, &exts);\n\tfor (i = 0; i < sk_X509_EXTENSION_num(exts); i++)\n\t\t{\n\t\tX509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i);\n\t\tif (!OCSP_REQUEST_add_ext(req, ext, -1))\n\t\t\tgoto err;\n\t\t}\n\tresp = process_responder(err, req, host, path, port, use_ssl,\n\t\t\t\t\tsrctx->timeout);\n\tif (!resp)\n\t\t{\n\t\tBIO_puts(err, "cert_status: error querying responder\\n");\n\t\tgoto done;\n\t\t}\n\trspderlen = i2d_OCSP_RESPONSE(resp, &rspder);\n\tif (rspderlen <= 0)\n\t\tgoto err;\n\tSSL_set_tlsext_status_ocsp_resp(s, rspder, rspderlen);\n\tif (srctx->verbose)\n\t\t{\n\t\tBIO_puts(err, "cert_status: ocsp response sent:\\n");\n\t\tOCSP_RESPONSE_print(err, resp, 2);\n\t\t}\n\tret = SSL_TLSEXT_ERR_OK;\n\tdone:\n\tif (ret != SSL_TLSEXT_ERR_OK)\n\t\tERR_print_errors(err);\n\tif (aia)\n\t\t{\n\t\tOPENSSL_free(host);\n\t\tOPENSSL_free(path);\n\t\tOPENSSL_free(port);\n\t\tX509_email_free(aia);\n\t\t}\n\tif (id)\n\t\tOCSP_CERTID_free(id);\n\tif (req)\n\t\tOCSP_REQUEST_free(req);\n\tif (resp)\n\t\tOCSP_RESPONSE_free(resp);\n\treturn ret;\n\terr:\n\tret = SSL_TLSEXT_ERR_ALERT_FATAL;\n\tgoto done;\n\t}', 'int BIO_puts(BIO *b, const char *in)\n\t{\n\tint i;\n\tlong (*cb)(BIO *,int,const char *,int,long,long);\n\tif ((b == NULL) || (b->method == NULL) || (b->method->bputs == NULL))\n\t\t{\n\t\tBIOerr(BIO_F_BIO_PUTS,BIO_R_UNSUPPORTED_METHOD);\n\t\treturn(-2);\n\t\t}\n\tcb=b->callback;\n\tif ((cb != NULL) &&\n\t\t((i=(int)cb(b,BIO_CB_PUTS,in,0,0L,1L)) <= 0))\n\t\t\treturn(i);\n\tif (!b->init)\n\t\t{\n\t\tBIOerr(BIO_F_BIO_PUTS,BIO_R_UNINITIALIZED);\n\t\treturn(-2);\n\t\t}\n\ti=b->method->bputs(b,in);\n\tif (i > 0) b->num_write+=(unsigned long)i;\n\tif (cb != NULL)\n\t\ti=(int)cb(b,BIO_CB_PUTS|BIO_CB_RETURN,in,0,\n\t\t\t0L,(long)i);\n\treturn(i);\n\t}', 'X509 *SSL_get_certificate(const SSL *s)\n\t{\n\tif (s->cert != NULL)\n\t\treturn(s->cert->key->x509);\n\telse\n\t\treturn(NULL);\n\t}']
2,329
0
https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/crypto/asn1/a_object.c/#L133
int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num) { int i,first,len=0,c; char tmp[24]; const char *p; unsigned long l; if (num == 0) return(0); else if (num == -1) num=strlen(buf); p=buf; c= *(p++); num--; if ((c >= '0') && (c <= '2')) { first=(c-'0')*40; } else { ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_FIRST_NUM_TOO_LARGE); goto err; } if (num <= 0) { ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_MISSING_SECOND_NUMBER); goto err; } c= *(p++); num--; for (;;) { if (num <= 0) break; if ((c != '.') && (c != ' ')) { ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_INVALID_SEPARATOR); goto err; } l=0; for (;;) { if (num <= 0) break; num--; c= *(p++); if ((c == ' ') || (c == '.')) break; if ((c < '0') || (c > '9')) { ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_INVALID_DIGIT); goto err; } l=l*10L+(long)(c-'0'); } if (len == 0) { if ((first < 2) && (l >= 40)) { ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_SECOND_NUMBER_TOO_LARGE); goto err; } l+=(long)first; } i=0; for (;;) { tmp[i++]=(unsigned char)l&0x7f; l>>=7L; if (l == 0L) break; } if (out != NULL) { if (len+i > olen) { ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_BUFFER_TOO_SMALL); goto err; } while (--i > 0) out[len++]=tmp[i]|0x80; out[len++]=tmp[0]; } else len+=i; } return(len); err: return(0); }
['int X509V3_EXT_add_conf(LHASH *conf, X509V3_CTX *ctx, char *section,\n\t X509 *cert)\n{\n\tX509_EXTENSION *ext;\n\tSTACK *nval;\n\tCONF_VALUE *val;\n\tint i;\n\tif(!(nval = CONF_get_section(conf, section))) return 0;\n\tfor(i = 0; i < sk_num(nval); i++) {\n\t\tval = (CONF_VALUE *)sk_value(nval, i);\n\t\tif(!(ext = X509V3_EXT_conf(conf, ctx, val->name, val->value)))\n\t\t\t\t\t\t\t\treturn 0;\n\t\tif(cert) X509_add_ext(cert, ext, -1);\n\t\tX509_EXTENSION_free(ext);\n\t}\n\treturn 1;\n}', 'X509_EXTENSION *X509V3_EXT_conf(LHASH *conf, X509V3_CTX *ctx, char *name,\n\t char *value)\n{\n\tint crit;\n\tint ext_type;\n\tX509_EXTENSION *ret;\n\tcrit = v3_check_critical(&value);\n\tif((ext_type = v3_check_generic(&value)))\n\t\treturn v3_generic_extension(name, value, crit, ext_type);\n\tret = do_ext_conf(conf, ctx, OBJ_sn2nid(name), crit, value);\n\tif(!ret) {\n\t\tX509V3err(X509V3_F_X509V3_EXT_CONF,X509V3_R_ERROR_IN_EXTENSION);\n\t\tERR_add_error_data(4,"name=", name, ", value=", value);\n\t}\n\treturn ret;\n}', 'static X509_EXTENSION *do_ext_conf(LHASH *conf, X509V3_CTX *ctx, int ext_nid,\n\t int crit, char *value)\n{\n\tX509_EXTENSION *ext = NULL;\n\tX509V3_EXT_METHOD *method;\n\tSTACK *nval;\n\tchar *ext_struc;\n\tunsigned char *ext_der, *p;\n\tint ext_len;\n\tASN1_OCTET_STRING *ext_oct;\n\tif(ext_nid == NID_undef) {\n\t\tX509V3err(X509V3_F_DO_EXT_CONF,X509V3_R_UNKNOWN_EXTENSION_NAME);\n\t\treturn NULL;\n\t}\n\tif(!(method = X509V3_EXT_get_nid(ext_nid))) {\n\t\tX509V3err(X509V3_F_DO_EXT_CONF,X509V3_R_UNKNOWN_EXTENSION);\n\t\treturn NULL;\n\t}\n\tif(method->v2i) {\n\t\tif(*value == \'@\') nval = CONF_get_section(conf, value + 1);\n\t\telse nval = X509V3_parse_list(value);\n\t\tif(!nval) {\n\t\t\tX509V3err(X509V3_F_X509V3_EXT_CONF,X509V3_R_INVALID_EXTENSION_STRING);\n\t\t\tERR_add_error_data(4, "name=", OBJ_nid2sn(ext_nid), ",section=", value);\n\t\t\treturn NULL;\n\t\t}\n\t\text_struc = method->v2i(method, ctx, nval);\n\t\tif(*value != \'@\') sk_pop_free(nval, X509V3_conf_free);\n\t\tif(!ext_struc) return NULL;\n\t} else if(method->s2i) {\n\t\tif(!(ext_struc = method->s2i(method, ctx, value))) return NULL;\n\t} else if(method->r2i) {\n\t\tif(!ctx->db) {\n\t\t\tX509V3err(X509V3_F_X509V3_EXT_CONF,X509V3_R_NO_CONFIG_DATABASE);\n\t\t\treturn NULL;\n\t\t}\n\t\tif(!(ext_struc = method->r2i(method, ctx, value))) return NULL;\n\t} else {\n\t\tX509V3err(X509V3_F_X509V3_EXT_CONF,X509V3_R_EXTENSION_SETTING_NOT_SUPPORTED);\n\t\tERR_add_error_data(2, "name=", OBJ_nid2sn(ext_nid));\n\t\treturn NULL;\n\t}\n\text_len = method->i2d(ext_struc, NULL);\n\text_der = Malloc(ext_len);\n\tp = ext_der;\n\tmethod->i2d(ext_struc, &p);\n\tmethod->ext_free(ext_struc);\n\text_oct = ASN1_OCTET_STRING_new();\n\text_oct->data = ext_der;\n\text_oct->length = ext_len;\n\text = X509_EXTENSION_create_by_NID(NULL, ext_nid, crit, ext_oct);\n\tASN1_OCTET_STRING_free(ext_oct);\n\treturn ext;\n}', 'STACK *X509V3_parse_list(char *line)\n{\n\tchar *p, *q, c;\n\tchar *ntmp, *vtmp;\n\tSTACK *values = NULL;\n\tchar *linebuf;\n\tint state;\n\tlinebuf = BUF_strdup(line);\n\tstate = HDR_NAME;\n\tntmp = NULL;\n\tfor(p = linebuf, q = linebuf; (c = *p) && (c!=\'\\r\') && (c!=\'\\n\'); p++) {\n\t\tswitch(state) {\n\t\t\tcase HDR_NAME:\n\t\t\tif(c == \':\') {\n\t\t\t\tstate = HDR_VALUE;\n\t\t\t\t*p = 0;\n\t\t\t\tntmp = strip_spaces(q);\n\t\t\t\tif(!ntmp) {\n\t\t\t\t\tX509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME);\n\t\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\t\tq = p + 1;\n\t\t\t} else if(c == \',\') {\n\t\t\t\t*p = 0;\n\t\t\t\tntmp = strip_spaces(q);\n\t\t\t\tq = p + 1;\n#ifdef DEBUG\n\t\t\t\tprintf("%s\\n", ntmp);\n#endif\n\t\t\t\tif(!ntmp) {\n\t\t\t\t\tX509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME);\n\t\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\t\tX509V3_add_value(ntmp, NULL, &values);\n\t\t\t}\n\t\t\tbreak ;\n\t\t\tcase HDR_VALUE:\n\t\t\tif(c == \',\') {\n\t\t\t\tstate = HDR_NAME;\n\t\t\t\t*p = 0;\n\t\t\t\tvtmp = strip_spaces(q);\n#ifdef DEBUG\n\t\t\t\tprintf("%s\\n", ntmp);\n#endif\n\t\t\t\tif(!vtmp) {\n\t\t\t\t\tX509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_VALUE);\n\t\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\t\tX509V3_add_value(ntmp, vtmp, &values);\n\t\t\t\tntmp = NULL;\n\t\t\t\tq = p + 1;\n\t\t\t}\n\t\t}\n\t}\n\tif(state == HDR_VALUE) {\n\t\tvtmp = strip_spaces(q);\n#ifdef DEBUG\n\t\tprintf("%s=%s\\n", ntmp, vtmp);\n#endif\n\t\tif(!vtmp) {\n\t\t\tX509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_VALUE);\n\t\t\tgoto err;\n\t\t}\n\t\tX509V3_add_value(ntmp, vtmp, &values);\n\t} else {\n\t\tntmp = strip_spaces(q);\n#ifdef DEBUG\n\t\tprintf("%s\\n", ntmp);\n#endif\n\t\tif(!ntmp) {\n\t\t\tX509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME);\n\t\t\tgoto err;\n\t\t}\n\t\tX509V3_add_value(ntmp, NULL, &values);\n\t}\nFree(linebuf);\nreturn values;\nerr:\nFree(linebuf);\nsk_pop_free(values, X509V3_conf_free);\nreturn NULL;\n}', 'static char *strip_spaces(char *name)\n{\n\tchar *p, *q;\n\tp = name;\n\twhile(*p && isspace(*p)) p++;\n\tif(!*p) return NULL;\n\tq = p + strlen(p) - 1;\n\twhile((q != p) && isspace(*q)) q--;\n\tif(p != q) q[1] = 0;\n\tif(!*p) return NULL;\n\treturn p;\n}', 'int X509V3_add_value(const char *name, const char *value, STACK **extlist)\n{\n\tCONF_VALUE *vtmp = NULL;\n\tchar *tname = NULL, *tvalue = NULL;\n\tif(name && !(tname = BUF_strdup(name))) goto err;\n\tif(value && !(tvalue = BUF_strdup(value))) goto err;;\n\tif(!(vtmp = (CONF_VALUE *)Malloc(sizeof(CONF_VALUE)))) goto err;\n\tif(!*extlist && !(*extlist = sk_new(NULL))) goto err;\n\tvtmp->section = NULL;\n\tvtmp->name = tname;\n\tvtmp->value = tvalue;\n\tif(!sk_push(*extlist, (char *)vtmp)) goto err;\n\treturn 1;\n\terr:\n\tX509V3err(X509V3_F_X509V3_ADD_VALUE,ERR_R_MALLOC_FAILURE);\n\tif(vtmp) Free(vtmp);\n\tif(tname) Free(tname);\n\tif(tvalue) Free(tvalue);\n\treturn 0;\n}', 'static X509_EXTENSION *v3_generic_extension(const char *ext, char *value,\n\t int crit, int type)\n{\nunsigned char *ext_der=NULL;\nlong ext_len;\nASN1_OBJECT *obj=NULL;\nASN1_OCTET_STRING *oct=NULL;\nX509_EXTENSION *extension=NULL;\nif(!(obj = OBJ_txt2obj(ext, 0))) {\n\tX509V3err(X509V3_F_V3_GENERIC_EXTENSION,X509V3_R_EXTENSION_NAME_ERROR);\n\tERR_add_error_data(2, "name=", ext);\n\tgoto err;\n}\nif(!(ext_der = string_to_hex(value, &ext_len))) {\n\tX509V3err(X509V3_F_V3_GENERIC_EXTENSION,X509V3_R_EXTENSION_VALUE_ERROR);\n\tERR_add_error_data(2, "value=", value);\n\tgoto err;\n}\nif(!(oct = ASN1_OCTET_STRING_new())) {\n\tX509V3err(X509V3_F_V3_GENERIC_EXTENSION,ERR_R_MALLOC_FAILURE);\n\tgoto err;\n}\noct->data = ext_der;\noct->length = ext_len;\next_der = NULL;\nextension = X509_EXTENSION_create_by_OBJ(NULL, obj, crit, oct);\nerr:\nASN1_OBJECT_free(obj);\nASN1_OCTET_STRING_free(oct);\nif(ext_der) Free(ext_der);\nreturn extension;\n}', 'ASN1_OBJECT *OBJ_txt2obj(const char *s, int no_name)\n\t{\n\tint nid = NID_undef;\n\tASN1_OBJECT *op=NULL;\n\tunsigned char *buf,*p;\n\tint i, j;\n\tif(!no_name) {\n\t\tif( ((nid = OBJ_sn2nid(s)) != NID_undef) ||\n\t\t\t((nid = OBJ_ln2nid(s)) != NID_undef) )\n\t\t\t\t\treturn OBJ_nid2obj(nid);\n\t}\n\ti=a2d_ASN1_OBJECT(NULL,0,s,-1);\n\tif (i <= 0) {\n\t\tERR_get_error();\n\t\treturn NULL;\n\t}\n\tj = ASN1_object_size(0,i,V_ASN1_OBJECT);\n\tif((buf=(unsigned char *)Malloc(j)) == NULL) return NULL;\n\tp = buf;\n\tASN1_put_object(&p,0,i,V_ASN1_OBJECT,V_ASN1_UNIVERSAL);\n\ta2d_ASN1_OBJECT(p,i,s,-1);\n\tp=buf;\n\top=d2i_ASN1_OBJECT(NULL,&p,i);\n\tFree(buf);\n\treturn op;\n\t}', "int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num)\n\t{\n\tint i,first,len=0,c;\n\tchar tmp[24];\n\tconst char *p;\n\tunsigned long l;\n\tif (num == 0)\n\t\treturn(0);\n\telse if (num == -1)\n\t\tnum=strlen(buf);\n\tp=buf;\n\tc= *(p++);\n\tnum--;\n\tif ((c >= '0') && (c <= '2'))\n\t\t{\n\t\tfirst=(c-'0')*40;\n\t\t}\n\telse\n\t\t{\n\t\tASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_FIRST_NUM_TOO_LARGE);\n\t\tgoto err;\n\t\t}\n\tif (num <= 0)\n\t\t{\n\t\tASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_MISSING_SECOND_NUMBER);\n\t\tgoto err;\n\t\t}\n\tc= *(p++);\n\tnum--;\n\tfor (;;)\n\t\t{\n\t\tif (num <= 0) break;\n\t\tif ((c != '.') && (c != ' '))\n\t\t\t{\n\t\t\tASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_INVALID_SEPARATOR);\n\t\t\tgoto err;\n\t\t\t}\n\t\tl=0;\n\t\tfor (;;)\n\t\t\t{\n\t\t\tif (num <= 0) break;\n\t\t\tnum--;\n\t\t\tc= *(p++);\n\t\t\tif ((c == ' ') || (c == '.'))\n\t\t\t\tbreak;\n\t\t\tif ((c < '0') || (c > '9'))\n\t\t\t\t{\n\t\t\t\tASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_INVALID_DIGIT);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tl=l*10L+(long)(c-'0');\n\t\t\t}\n\t\tif (len == 0)\n\t\t\t{\n\t\t\tif ((first < 2) && (l >= 40))\n\t\t\t\t{\n\t\t\t\tASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_SECOND_NUMBER_TOO_LARGE);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tl+=(long)first;\n\t\t\t}\n\t\ti=0;\n\t\tfor (;;)\n\t\t\t{\n\t\t\ttmp[i++]=(unsigned char)l&0x7f;\n\t\t\tl>>=7L;\n\t\t\tif (l == 0L) break;\n\t\t\t}\n\t\tif (out != NULL)\n\t\t\t{\n\t\t\tif (len+i > olen)\n\t\t\t\t{\n\t\t\t\tASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_BUFFER_TOO_SMALL);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\twhile (--i > 0)\n\t\t\t\tout[len++]=tmp[i]|0x80;\n\t\t\tout[len++]=tmp[0];\n\t\t\t}\n\t\telse\n\t\t\tlen+=i;\n\t\t}\n\treturn(len);\nerr:\n\treturn(0);\n\t}"]
2,330
0
https://github.com/openssl/openssl/blob/6bc62a620e715f7580651ca932eab052aa527886/crypto/bn/bn_ctx.c/#L268
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['int BN_BLINDING_convert_ex(BIGNUM *n, BIGNUM *r, BN_BLINDING *b, BN_CTX *ctx)\n{\n int ret = 1;\n bn_check_top(n);\n if ((b->A == NULL) || (b->Ai == NULL)) {\n BNerr(BN_F_BN_BLINDING_CONVERT_EX, BN_R_NOT_INITIALIZED);\n return 0;\n }\n if (b->counter == -1)\n b->counter = 0;\n else if (!BN_BLINDING_update(b, ctx))\n return 0;\n if (r != NULL && (BN_copy(r, b->Ai) == NULL))\n return 0;\n if (b->m_ctx != NULL)\n ret = BN_mod_mul_montgomery(n, n, b->A, b->m_ctx, ctx);\n else\n ret = BN_mod_mul(n, n, b->A, b->mod, ctx);\n return ret;\n}', 'int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n BN_MONT_CTX *mont, BN_CTX *ctx)\n{\n int ret = bn_mul_mont_fixed_top(r, a, b, mont, ctx);\n bn_correct_top(r);\n bn_check_top(r);\n return ret;\n}', 'int bn_mul_mont_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n BN_MONT_CTX *mont, BN_CTX *ctx)\n{\n BIGNUM *tmp;\n int ret = 0;\n int num = mont->N.top;\n#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\n if (num > 1 && a->top == num && b->top == num) {\n if (bn_wexpand(r, num) == NULL)\n return 0;\n if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {\n r->neg = a->neg ^ b->neg;\n r->top = num;\n r->flags |= BN_FLG_FIXED_TOP;\n return 1;\n }\n }\n#endif\n if ((a->top + b->top) > 2 * num)\n return 0;\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n if (tmp == NULL)\n goto err;\n bn_check_top(tmp);\n if (a == b) {\n if (!bn_sqr_fixed_top(tmp, a, ctx))\n goto err;\n } else {\n if (!bn_mul_fixed_top(tmp, a, b, ctx))\n goto err;\n }\n#ifdef MONT_WORD\n if (!bn_from_montgomery_word(r, tmp, mont))\n goto err;\n#else\n if (!BN_from_montgomery(r, tmp, mont, ctx))\n goto err;\n#endif\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_start()", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG("LEAVE BN_CTX_start()", ctx);\n}', 'int bn_sqr_fixed_top(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int max, al;\n int ret = 0;\n BIGNUM *tmp, *rr;\n bn_check_top(a);\n al = a->top;\n if (al <= 0) {\n r->top = 0;\n r->neg = 0;\n return 1;\n }\n BN_CTX_start(ctx);\n rr = (a != r) ? r : BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (rr == NULL || tmp == NULL)\n goto err;\n max = 2 * al;\n if (bn_wexpand(rr, max) == NULL)\n goto err;\n if (al == 4) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[8];\n bn_sqr_normal(rr->d, a->d, 4, t);\n#else\n bn_sqr_comba4(rr->d, a->d);\n#endif\n } else if (al == 8) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[16];\n bn_sqr_normal(rr->d, a->d, 8, t);\n#else\n bn_sqr_comba8(rr->d, a->d);\n#endif\n } else {\n#if defined(BN_RECURSION)\n if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {\n BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];\n bn_sqr_normal(rr->d, a->d, al, t);\n } else {\n int j, k;\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n if (al == j) {\n if (bn_wexpand(tmp, k * 2) == NULL)\n goto err;\n bn_sqr_recursive(rr->d, a->d, al, tmp->d);\n } else {\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n }\n }\n#else\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n#endif\n }\n rr->neg = 0;\n rr->top = max;\n rr->flags |= BN_FLG_FIXED_TOP;\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(rr);\n bn_check_top(tmp);\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_end()", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG("LEAVE BN_CTX_end()", ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
2,331
0
https://github.com/openssl/openssl/blob/183733f882056ea3e6fe95e665b85fcc6a45dcb4/crypto/srp/srp_lib.c/#L290
BIGNUM *SRP_Calc_client_key(BIGNUM *N, BIGNUM *B, BIGNUM *g, BIGNUM *x, BIGNUM *a, BIGNUM *u) { BIGNUM *tmp = NULL, *tmp2 = NULL, *tmp3 = NULL, *k = NULL, *K = NULL; BN_CTX *bn_ctx; if (u == NULL || B == NULL || N == NULL || g == NULL || x == NULL || a == NULL || (bn_ctx = BN_CTX_new()) == NULL) return NULL; if ((tmp = BN_new()) == NULL || (tmp2 = BN_new()) == NULL || (tmp3 = BN_new()) == NULL || (K = BN_new()) == NULL) goto err; if (!BN_mod_exp(tmp, g, x, N, bn_ctx)) goto err; if ((k = srp_Calc_k(N, g)) == NULL) goto err; if (!BN_mod_mul(tmp2, tmp, k, N, bn_ctx)) goto err; if (!BN_mod_sub(tmp, B, tmp2, N, bn_ctx)) goto err; if (!BN_mod_mul(tmp3, u, x, N, bn_ctx)) goto err; if (!BN_mod_add(tmp2, a, tmp3, N, bn_ctx)) goto err; if (!BN_mod_exp(K, tmp, tmp2, N, bn_ctx)) goto err; err: BN_CTX_free(bn_ctx); BN_clear_free(tmp); BN_clear_free(tmp2); BN_clear_free(tmp3); BN_free(k); return K; }
['BIGNUM *SRP_Calc_client_key(BIGNUM *N, BIGNUM *B, BIGNUM *g, BIGNUM *x,\n BIGNUM *a, BIGNUM *u)\n{\n BIGNUM *tmp = NULL, *tmp2 = NULL, *tmp3 = NULL, *k = NULL, *K = NULL;\n BN_CTX *bn_ctx;\n if (u == NULL || B == NULL || N == NULL || g == NULL || x == NULL\n || a == NULL || (bn_ctx = BN_CTX_new()) == NULL)\n return NULL;\n if ((tmp = BN_new()) == NULL ||\n (tmp2 = BN_new()) == NULL ||\n (tmp3 = BN_new()) == NULL ||\n (K = BN_new()) == NULL)\n goto err;\n if (!BN_mod_exp(tmp, g, x, N, bn_ctx))\n goto err;\n if ((k = srp_Calc_k(N, g)) == NULL)\n goto err;\n if (!BN_mod_mul(tmp2, tmp, k, N, bn_ctx))\n goto err;\n if (!BN_mod_sub(tmp, B, tmp2, N, bn_ctx))\n goto err;\n if (!BN_mod_mul(tmp3, u, x, N, bn_ctx))\n goto err;\n if (!BN_mod_add(tmp2, a, tmp3, N, bn_ctx))\n goto err;\n if (!BN_mod_exp(K, tmp, tmp2, N, bn_ctx))\n goto err;\n err:\n BN_CTX_free(bn_ctx);\n BN_clear_free(tmp);\n BN_clear_free(tmp2);\n BN_clear_free(tmp3);\n BN_free(k);\n return K;\n}', 'BN_CTX *BN_CTX_new(void)\n{\n BN_CTX *ret;\n if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {\n BNerr(BN_F_BN_CTX_NEW, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n BN_POOL_init(&ret->pool);\n BN_STACK_init(&ret->stack);\n return ret;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (num <= 0)\n return NULL;\n allow_customize = 0;\n#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}', 'static void BN_POOL_init(BN_POOL *p)\n{\n p->head = p->current = p->tail = NULL;\n p->used = p->size = 0;\n}', 'static void BN_STACK_init(BN_STACK *st)\n{\n st->indexes = NULL;\n st->depth = st->size = 0;\n}', 'BIGNUM *BN_new(void)\n{\n BIGNUM *ret;\n if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {\n BNerr(BN_F_BN_NEW, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n ret->flags = BN_FLG_MALLOCED;\n bn_check_top(ret);\n return (ret);\n}', 'void BN_CTX_free(BN_CTX *ctx)\n{\n if (ctx == NULL)\n return;\n#ifdef BN_CTX_DEBUG\n {\n BN_POOL_ITEM *pool = ctx->pool.head;\n fprintf(stderr, "BN_CTX_free, stack-size=%d, pool-bignums=%d\\n",\n ctx->stack.size, ctx->pool.size);\n fprintf(stderr, "dmaxs: ");\n while (pool) {\n unsigned loop = 0;\n while (loop < BN_CTX_POOL_SIZE)\n fprintf(stderr, "%02x ", pool->vals[loop++].dmax);\n pool = pool->next;\n }\n fprintf(stderr, "\\n");\n }\n#endif\n BN_STACK_finish(&ctx->stack);\n BN_POOL_finish(&ctx->pool);\n OPENSSL_free(ctx);\n}', 'static void BN_STACK_finish(BN_STACK *st)\n{\n OPENSSL_free(st->indexes);\n st->indexes = NULL;\n}', 'void CRYPTO_free(void *str)\n{\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_free(str, 0);\n free(str);\n CRYPTO_mem_debug_free(str, 1);\n } else {\n free(str);\n }\n#else\n free(str);\n#endif\n}', 'static void BN_POOL_finish(BN_POOL *p)\n{\n unsigned int loop;\n BIGNUM *bn;\n while (p->head) {\n for (loop = 0, bn = p->head->vals; loop++ < BN_CTX_POOL_SIZE; bn++)\n if (bn->d)\n BN_clear_free(bn);\n p->current = p->head->next;\n OPENSSL_free(p->head);\n p->head = p->current;\n }\n}', 'void BN_clear_free(BIGNUM *a)\n{\n int i;\n if (a == NULL)\n return;\n bn_check_top(a);\n if (a->d != NULL) {\n OPENSSL_cleanse(a->d, a->dmax * sizeof(a->d[0]));\n if (!BN_get_flags(a, BN_FLG_STATIC_DATA))\n bn_free_d(a);\n }\n i = BN_get_flags(a, BN_FLG_MALLOCED);\n OPENSSL_cleanse(a, sizeof(*a));\n if (i)\n OPENSSL_free(a);\n}', 'int BN_get_flags(const BIGNUM *b, int n)\n{\n return b->flags & n;\n}']
2,332
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 wma_decode_superframe(AVCodecContext *avctx, void *data,\n int *got_frame_ptr, AVPacket *avpkt)\n{\n AVFrame *frame = data;\n const uint8_t *buf = avpkt->data;\n int buf_size = avpkt->size;\n WMACodecContext *s = avctx->priv_data;\n int nb_frames, bit_offset, i, pos, len, ret;\n uint8_t *q;\n float **samples;\n int samples_offset;\n ff_tlog(avctx, "***decode_superframe:\\n");\n if (buf_size == 0) {\n s->last_superframe_len = 0;\n return 0;\n }\n if (buf_size < avctx->block_align) {\n av_log(avctx, AV_LOG_ERROR,\n "Input packet size too small (%d < %d)\\n",\n buf_size, avctx->block_align);\n return AVERROR_INVALIDDATA;\n }\n buf_size = avctx->block_align;\n bitstream_init8(&s->bc, buf, buf_size);\n if (s->use_bit_reservoir) {\n bitstream_skip(&s->bc, 4);\n nb_frames = bitstream_read(&s->bc, 4) - (s->last_superframe_len <= 0);\n } else\n nb_frames = 1;\n frame->nb_samples = nb_frames * s->frame_len;\n if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {\n av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n return ret;\n }\n samples = (float **) frame->extended_data;\n samples_offset = 0;\n if (s->use_bit_reservoir) {\n bit_offset = bitstream_read(&s->bc, s->byte_offset_bits + 3);\n if (bit_offset > bitstream_bits_left(&s->bc)) {\n av_log(avctx, AV_LOG_ERROR,\n "Invalid last frame bit offset %d > buf size %d (%d)\\n",\n bit_offset, bitstream_bits_left(&s->bc), buf_size);\n goto fail;\n }\n if (s->last_superframe_len > 0) {\n if ((s->last_superframe_len + ((bit_offset + 7) >> 3)) >\n MAX_CODED_SUPERFRAME_SIZE)\n goto fail;\n q = s->last_superframe + s->last_superframe_len;\n len = bit_offset;\n while (len > 7) {\n *q++ = bitstream_read(&s->bc, 8);\n len -= 8;\n }\n if (len > 0)\n *q++ = bitstream_read(&s->bc, len) << (8 - len);\n memset(q, 0, AV_INPUT_BUFFER_PADDING_SIZE);\n bitstream_init(&s->bc, s->last_superframe,\n s->last_superframe_len * 8 + bit_offset);\n if (s->last_bitoffset > 0)\n bitstream_skip(&s->bc, s->last_bitoffset);\n if (wma_decode_frame(s, samples, samples_offset) < 0)\n goto fail;\n samples_offset += s->frame_len;\n nb_frames--;\n }\n pos = bit_offset + 4 + 4 + s->byte_offset_bits + 3;\n if (pos >= MAX_CODED_SUPERFRAME_SIZE * 8 || pos > buf_size * 8)\n return AVERROR_INVALIDDATA;\n bitstream_init8(&s->bc, buf + (pos >> 3), buf_size - (pos >> 3));\n len = pos & 7;\n if (len > 0)\n bitstream_skip(&s->bc, len);\n s->reset_block_lengths = 1;\n for (i = 0; i < nb_frames; i++) {\n if (wma_decode_frame(s, samples, samples_offset) < 0)\n goto fail;\n samples_offset += s->frame_len;\n }\n pos = bitstream_tell(&s->bc) +\n ((bit_offset + 4 + 4 + s->byte_offset_bits + 3) & ~7);\n s->last_bitoffset = pos & 7;\n pos >>= 3;\n len = buf_size - pos;\n if (len > MAX_CODED_SUPERFRAME_SIZE || len < 0) {\n av_log(s->avctx, AV_LOG_ERROR, "len %d invalid\\n", len);\n goto fail;\n }\n s->last_superframe_len = len;\n memcpy(s->last_superframe, buf + pos, len);\n } else {\n if (wma_decode_frame(s, samples, samples_offset) < 0)\n goto fail;\n samples_offset += s->frame_len;\n }\n ff_dlog(s->avctx, "%d %d %d %d outbytes:%td eaten:%d\\n",\n s->frame_len_bits, s->block_len_bits, s->frame_len, s->block_len,\n (int8_t *) samples - (int8_t *) data, avctx->block_align);\n *got_frame_ptr = 1;\n return avctx->block_align;\nfail:\n s->last_superframe_len = 0;\n return -1;\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}']
2,333
0
https://github.com/openssl/openssl/blob/c7b5b9f4b1ec24743da20926f50418ba9fa92e87/crypto/poly1305/poly1305.c/#L471
void Poly1305_Update(POLY1305 *ctx, const unsigned char *inp, size_t len) { #ifdef POLY1305_ASM poly1305_blocks_f poly1305_blocks = ctx->func.blocks; #endif size_t rem, num; if ((num = ctx->num)) { rem = POLY1305_BLOCK_SIZE - num; if (len >= rem) { memcpy(ctx->data + num, inp, rem); poly1305_blocks(ctx->opaque, ctx->data, POLY1305_BLOCK_SIZE, 1); inp += rem; len -= rem; } else { memcpy(ctx->data + num, inp, len); ctx->num = num + len; return; } } rem = len % POLY1305_BLOCK_SIZE; len -= rem; if (len >= POLY1305_BLOCK_SIZE) { poly1305_blocks(ctx->opaque, inp, len, 1); inp += len; } if (rem) memcpy(ctx->data, inp, rem); ctx->num = rem; }
['static int chacha20_poly1305_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,\n const unsigned char *in, size_t len)\n{\n EVP_CHACHA_AEAD_CTX *actx = aead_data(ctx);\n size_t rem, plen = actx->tls_payload_length;\n static const unsigned char zero[POLY1305_BLOCK_SIZE] = { 0 };\n if (!actx->mac_inited) {\n actx->key.counter[0] = 0;\n memset(actx->key.buf, 0, sizeof(actx->key.buf));\n ChaCha20_ctr32(actx->key.buf, actx->key.buf, CHACHA_BLK_SIZE,\n actx->key.key.d, actx->key.counter);\n Poly1305_Init(POLY1305_ctx(actx), actx->key.buf);\n actx->key.counter[0] = 1;\n actx->mac_inited = 1;\n }\n if (in) {\n if (out == NULL) {\n Poly1305_Update(POLY1305_ctx(actx), in, len);\n actx->len.aad += len;\n actx->aad = 1;\n return len;\n } else {\n if (actx->aad) {\n if ((rem = (size_t)actx->len.aad % POLY1305_BLOCK_SIZE))\n Poly1305_Update(POLY1305_ctx(actx), zero,\n POLY1305_BLOCK_SIZE - rem);\n actx->aad = 0;\n }\n actx->tls_payload_length = NO_TLS_PAYLOAD_LENGTH;\n if (plen == NO_TLS_PAYLOAD_LENGTH)\n plen = len;\n else if (len != plen + POLY1305_BLOCK_SIZE)\n return -1;\n if (ctx->encrypt) {\n chacha_cipher(ctx, out, in, plen);\n Poly1305_Update(POLY1305_ctx(actx), out, plen);\n in += plen;\n out += plen;\n actx->len.text += plen;\n } else {\n Poly1305_Update(POLY1305_ctx(actx), in, plen);\n chacha_cipher(ctx, out, in, plen);\n in += plen;\n out += plen;\n actx->len.text += plen;\n }\n }\n }\n if (in == NULL\n || plen != len) {\n const union {\n long one;\n char little;\n } is_endian = { 1 };\n unsigned char temp[POLY1305_BLOCK_SIZE];\n if (actx->aad) {\n if ((rem = (size_t)actx->len.aad % POLY1305_BLOCK_SIZE))\n Poly1305_Update(POLY1305_ctx(actx), zero,\n POLY1305_BLOCK_SIZE - rem);\n actx->aad = 0;\n }\n if ((rem = (size_t)actx->len.text % POLY1305_BLOCK_SIZE))\n Poly1305_Update(POLY1305_ctx(actx), zero,\n POLY1305_BLOCK_SIZE - rem);\n if (is_endian.little) {\n Poly1305_Update(POLY1305_ctx(actx),\n (unsigned char *)&actx->len, POLY1305_BLOCK_SIZE);\n } else {\n temp[0] = (unsigned char)(actx->len.aad);\n temp[1] = (unsigned char)(actx->len.aad>>8);\n temp[2] = (unsigned char)(actx->len.aad>>16);\n temp[3] = (unsigned char)(actx->len.aad>>24);\n temp[4] = (unsigned char)(actx->len.aad>>32);\n temp[5] = (unsigned char)(actx->len.aad>>40);\n temp[6] = (unsigned char)(actx->len.aad>>48);\n temp[7] = (unsigned char)(actx->len.aad>>56);\n temp[8] = (unsigned char)(actx->len.text);\n temp[9] = (unsigned char)(actx->len.text>>8);\n temp[10] = (unsigned char)(actx->len.text>>16);\n temp[11] = (unsigned char)(actx->len.text>>24);\n temp[12] = (unsigned char)(actx->len.text>>32);\n temp[13] = (unsigned char)(actx->len.text>>40);\n temp[14] = (unsigned char)(actx->len.text>>48);\n temp[15] = (unsigned char)(actx->len.text>>56);\n Poly1305_Update(POLY1305_ctx(actx), temp, POLY1305_BLOCK_SIZE);\n }\n Poly1305_Final(POLY1305_ctx(actx), ctx->encrypt ? actx->tag\n : temp);\n actx->mac_inited = 0;\n if (in != NULL && len != plen) {\n if (ctx->encrypt) {\n memcpy(out, actx->tag, POLY1305_BLOCK_SIZE);\n } else {\n if (CRYPTO_memcmp(temp, in, POLY1305_BLOCK_SIZE)) {\n memset(out, 0, plen);\n return -1;\n }\n }\n }\n else if (!ctx->encrypt) {\n if (CRYPTO_memcmp(temp, actx->tag, actx->tag_len))\n return -1;\n }\n }\n return len;\n}', 'void Poly1305_Update(POLY1305 *ctx, const unsigned char *inp, size_t len)\n{\n#ifdef POLY1305_ASM\n poly1305_blocks_f poly1305_blocks = ctx->func.blocks;\n#endif\n size_t rem, num;\n if ((num = ctx->num)) {\n rem = POLY1305_BLOCK_SIZE - num;\n if (len >= rem) {\n memcpy(ctx->data + num, inp, rem);\n poly1305_blocks(ctx->opaque, ctx->data, POLY1305_BLOCK_SIZE, 1);\n inp += rem;\n len -= rem;\n } else {\n memcpy(ctx->data + num, inp, len);\n ctx->num = num + len;\n return;\n }\n }\n rem = len % POLY1305_BLOCK_SIZE;\n len -= rem;\n if (len >= POLY1305_BLOCK_SIZE) {\n poly1305_blocks(ctx->opaque, inp, len, 1);\n inp += len;\n }\n if (rem)\n memcpy(ctx->data, inp, rem);\n ctx->num = rem;\n}']
2,334
0
https://github.com/libav/libav/blob/124c21d79f2124d028890022e98ea853a834a964/libavcodec/vp6.c/#L224
static void vp6_build_huff_tree(VP56Context *s, uint8_t coeff_model[], const uint8_t *map, unsigned size, VLC *vlc) { Node nodes[2*size], *tmp = &nodes[size]; int a, b, i; tmp[0].count = 256; for (i=0; i<size-1; i++) { a = tmp[i].count * coeff_model[i] >> 8; b = tmp[i].count * (255 - coeff_model[i]) >> 8; nodes[map[2*i ]].count = a + !a; nodes[map[2*i+1]].count = b + !b; } ff_huff_build_tree(s->avctx, vlc, size, nodes, vp6_huff_cmp, FF_HUFFMAN_FLAG_HNODE_FIRST); }
['static void vp6_parse_coeff_models(VP56Context *s)\n{\n VP56RangeCoder *c = &s->c;\n VP56Model *model = s->modelp;\n int def_prob[11];\n int node, cg, ctx, pos;\n int ct;\n int pt;\n memset(def_prob, 0x80, sizeof(def_prob));\n for (pt=0; pt<2; pt++)\n for (node=0; node<11; node++)\n if (vp56_rac_get_prob(c, vp6_dccv_pct[pt][node])) {\n def_prob[node] = vp56_rac_gets_nn(c, 7);\n model->coeff_dccv[pt][node] = def_prob[node];\n } else if (s->framep[VP56_FRAME_CURRENT]->key_frame) {\n model->coeff_dccv[pt][node] = def_prob[node];\n }\n if (vp56_rac_get(c)) {\n for (pos=1; pos<64; pos++)\n if (vp56_rac_get_prob(c, vp6_coeff_reorder_pct[pos]))\n model->coeff_reorder[pos] = vp56_rac_gets(c, 4);\n vp6_coeff_order_table_init(s);\n }\n for (cg=0; cg<2; cg++)\n for (node=0; node<14; node++)\n if (vp56_rac_get_prob(c, vp6_runv_pct[cg][node]))\n model->coeff_runv[cg][node] = vp56_rac_gets_nn(c, 7);\n for (ct=0; ct<3; ct++)\n for (pt=0; pt<2; pt++)\n for (cg=0; cg<6; cg++)\n for (node=0; node<11; node++)\n if (vp56_rac_get_prob(c, vp6_ract_pct[ct][pt][cg][node])) {\n def_prob[node] = vp56_rac_gets_nn(c, 7);\n model->coeff_ract[pt][ct][cg][node] = def_prob[node];\n } else if (s->framep[VP56_FRAME_CURRENT]->key_frame) {\n model->coeff_ract[pt][ct][cg][node] = def_prob[node];\n }\n if (s->use_huffman) {\n for (pt=0; pt<2; pt++) {\n vp6_build_huff_tree(s, model->coeff_dccv[pt],\n vp6_huff_coeff_map, 12, &s->dccv_vlc[pt]);\n vp6_build_huff_tree(s, model->coeff_runv[pt],\n vp6_huff_run_map, 9, &s->runv_vlc[pt]);\n for (ct=0; ct<3; ct++)\n for (cg = 0; cg < 6; cg++)\n vp6_build_huff_tree(s, model->coeff_ract[pt][ct][cg],\n vp6_huff_coeff_map, 12,\n &s->ract_vlc[pt][ct][cg]);\n }\n memset(s->nb_null, 0, sizeof(s->nb_null));\n } else {\n for (pt=0; pt<2; pt++)\n for (ctx=0; ctx<3; ctx++)\n for (node=0; node<5; node++)\n model->coeff_dcct[pt][ctx][node] = av_clip(((model->coeff_dccv[pt][node] * vp6_dccv_lc[ctx][node][0] + 128) >> 8) + vp6_dccv_lc[ctx][node][1], 1, 255);\n }\n}', 'static void vp6_build_huff_tree(VP56Context *s, uint8_t coeff_model[],\n const uint8_t *map, unsigned size, VLC *vlc)\n{\n Node nodes[2*size], *tmp = &nodes[size];\n int a, b, i;\n tmp[0].count = 256;\n for (i=0; i<size-1; i++) {\n a = tmp[i].count * coeff_model[i] >> 8;\n b = tmp[i].count * (255 - coeff_model[i]) >> 8;\n nodes[map[2*i ]].count = a + !a;\n nodes[map[2*i+1]].count = b + !b;\n }\n ff_huff_build_tree(s->avctx, vlc, size, nodes, vp6_huff_cmp,\n FF_HUFFMAN_FLAG_HNODE_FIRST);\n}']
2,335
0
https://github.com/libav/libav/blob/ad1161799e096c4bae885f100f27f886755d479a/libavcodec/metasound.c/#L343
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 (2 << 16) + ( 8 << 8) + 8: tctx->mtab = &ff_metasound_mode0808s; break; case (1 << 16) + (11 << 8) + 10: tctx->mtab = &ff_metasound_mode1110; break; case (2 << 16) + (11 << 8) + 10: tctx->mtab = &ff_metasound_mode1110s; break; case (1 << 16) + (16 << 8) + 16: tctx->mtab = &ff_metasound_mode1616; break; case (2 << 16) + (16 << 8) + 16: tctx->mtab = &ff_metasound_mode1616s; break; case (1 << 16) + (22 << 8) + 24: tctx->mtab = &ff_metasound_mode2224; break; case (2 << 16) + (22 << 8) + 24: tctx->mtab = &ff_metasound_mode2224s; break; case (1 << 16) + (44 << 8) + 32: tctx->mtab = &ff_metasound_mode4432; break; case (2 << 16) + (44 << 8) + 32: tctx->mtab = &ff_metasound_mode4432s; break; case (1 << 16) + (44 << 8) + 40: tctx->mtab = &ff_metasound_mode4440; break; case (2 << 16) + (44 << 8) + 40: tctx->mtab = &ff_metasound_mode4440s; break; case (1 << 16) + (44 << 8) + 48: tctx->mtab = &ff_metasound_mode4448; 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, ibps); 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; tctx->frame_size = avctx->bit_rate * tctx->mtab->size / avctx->sample_rate; 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 (2 << 16) + ( 8 << 8) + 8:\n tctx->mtab = &ff_metasound_mode0808s;\n break;\n case (1 << 16) + (11 << 8) + 10:\n tctx->mtab = &ff_metasound_mode1110;\n break;\n case (2 << 16) + (11 << 8) + 10:\n tctx->mtab = &ff_metasound_mode1110s;\n break;\n case (1 << 16) + (16 << 8) + 16:\n tctx->mtab = &ff_metasound_mode1616;\n break;\n case (2 << 16) + (16 << 8) + 16:\n tctx->mtab = &ff_metasound_mode1616s;\n break;\n case (1 << 16) + (22 << 8) + 24:\n tctx->mtab = &ff_metasound_mode2224;\n break;\n case (2 << 16) + (22 << 8) + 24:\n tctx->mtab = &ff_metasound_mode2224s;\n break;\n case (1 << 16) + (44 << 8) + 32:\n tctx->mtab = &ff_metasound_mode4432;\n break;\n case (2 << 16) + (44 << 8) + 32:\n tctx->mtab = &ff_metasound_mode4432s;\n break;\n case (1 << 16) + (44 << 8) + 40:\n tctx->mtab = &ff_metasound_mode4440;\n break;\n case (2 << 16) + (44 << 8) + 40:\n tctx->mtab = &ff_metasound_mode4440s;\n break;\n case (1 << 16) + (44 << 8) + 48:\n tctx->mtab = &ff_metasound_mode4448;\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, ibps);\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 tctx->frame_size = avctx->bit_rate * tctx->mtab->size\n / avctx->sample_rate;\n return ff_twinvq_decode_init(avctx);\n}']
2,336
0
https://github.com/libav/libav/blob/bb770c5b522bdd81b65ea4391579e5ebdd62a047/libavcodec/wmavoice.c/#L1098
static int synth_frame(AVCodecContext *ctx, GetBitContext *gb, float *samples, const double *lsps, const double *prev_lsps, float *excitation, float *synth) { WMAVoiceContext *s = ctx->priv_data; int n, n_blocks_x2, log_n_blocks_x2, cur_pitch_val; int pitch[MAX_BLOCKS], last_block_pitch; int bd_idx = s->vbm_tree[get_vlc2(gb, frame_type_vlc.table, 6, 3)], block_nsamples = MAX_FRAMESIZE / frame_descs[bd_idx].n_blocks; if (bd_idx < 0) { av_log(ctx, AV_LOG_ERROR, "Invalid frame type VLC code, skipping\n"); return -1; } if (frame_descs[bd_idx].acb_type == ACB_TYPE_ASYMMETRIC) { n_blocks_x2 = frame_descs[bd_idx].n_blocks << 1; log_n_blocks_x2 = frame_descs[bd_idx].log_n_blocks + 1; cur_pitch_val = s->min_pitch_val + get_bits(gb, s->pitch_nbits); cur_pitch_val = FFMIN(cur_pitch_val, s->max_pitch_val - 1); if (s->last_acb_type == ACB_TYPE_NONE || 20 * abs(cur_pitch_val - s->last_pitch_val) > (cur_pitch_val + s->last_pitch_val)) s->last_pitch_val = cur_pitch_val; for (n = 0; n < frame_descs[bd_idx].n_blocks; n++) { int fac = n * 2 + 1; pitch[n] = (MUL16(fac, cur_pitch_val) + MUL16((n_blocks_x2 - fac), s->last_pitch_val) + frame_descs[bd_idx].n_blocks) >> log_n_blocks_x2; } s->pitch_diff_sh16 = ((cur_pitch_val - s->last_pitch_val) << 16) / MAX_FRAMESIZE; } switch (frame_descs[bd_idx].fcb_type) { case FCB_TYPE_SILENCE: s->silence_gain = wmavoice_gain_silence[get_bits(gb, 8)]; break; case FCB_TYPE_AW_PULSES: aw_parse_coords(s, gb, pitch); break; } for (n = 0; n < frame_descs[bd_idx].n_blocks; n++) { int bl_pitch_sh2; switch (frame_descs[bd_idx].acb_type) { case ACB_TYPE_HAMMING: { int block_pitch, t1 = (s->block_conv_table[1] - s->block_conv_table[0]) << 2, t2 = (s->block_conv_table[2] - s->block_conv_table[1]) << 1, t3 = s->block_conv_table[3] - s->block_conv_table[2] + 1; if (n == 0) { block_pitch = get_bits(gb, s->block_pitch_nbits); } else block_pitch = last_block_pitch - s->block_delta_pitch_hrange + get_bits(gb, s->block_delta_pitch_nbits); last_block_pitch = av_clip(block_pitch, s->block_delta_pitch_hrange, s->block_pitch_range - s->block_delta_pitch_hrange); if (block_pitch < t1) { bl_pitch_sh2 = (s->block_conv_table[0] << 2) + block_pitch; } else { block_pitch -= t1; if (block_pitch < t2) { bl_pitch_sh2 = (s->block_conv_table[1] << 2) + (block_pitch << 1); } else { block_pitch -= t2; if (block_pitch < t3) { bl_pitch_sh2 = (s->block_conv_table[2] + block_pitch) << 2; } else bl_pitch_sh2 = s->block_conv_table[3] << 2; } } pitch[n] = bl_pitch_sh2 >> 2; break; } case ACB_TYPE_ASYMMETRIC: { bl_pitch_sh2 = pitch[n] << 2; break; } default: bl_pitch_sh2 = 0; break; } synth_block(s, gb, n, block_nsamples, bl_pitch_sh2, lsps, prev_lsps, &frame_descs[bd_idx], &excitation[n * block_nsamples], &synth[n * block_nsamples]); } if (s->do_apf) { av_log_missing_feature(ctx, "APF", 0); s->do_apf = 0; } for (n = 0; n < 160; n++) samples[n] = av_clipf(synth[n], -1.0, 1.0); s->frame_cntr++; if (s->frame_cntr >= 0xFFFF) s->frame_cntr -= 0xFFFF; s->last_acb_type = frame_descs[bd_idx].acb_type; switch (frame_descs[bd_idx].acb_type) { case ACB_TYPE_NONE: s->last_pitch_val = 0; break; case ACB_TYPE_ASYMMETRIC: s->last_pitch_val = cur_pitch_val; break; case ACB_TYPE_HAMMING: s->last_pitch_val = pitch[frame_descs[bd_idx].n_blocks - 1]; break; } return 0; }
['static int synth_frame(AVCodecContext *ctx, GetBitContext *gb,\n float *samples,\n const double *lsps, const double *prev_lsps,\n float *excitation, float *synth)\n{\n WMAVoiceContext *s = ctx->priv_data;\n int n, n_blocks_x2, log_n_blocks_x2, cur_pitch_val;\n int pitch[MAX_BLOCKS], last_block_pitch;\n int bd_idx = s->vbm_tree[get_vlc2(gb, frame_type_vlc.table, 6, 3)],\n block_nsamples = MAX_FRAMESIZE / frame_descs[bd_idx].n_blocks;\n if (bd_idx < 0) {\n av_log(ctx, AV_LOG_ERROR,\n "Invalid frame type VLC code, skipping\\n");\n return -1;\n }\n if (frame_descs[bd_idx].acb_type == ACB_TYPE_ASYMMETRIC) {\n n_blocks_x2 = frame_descs[bd_idx].n_blocks << 1;\n log_n_blocks_x2 = frame_descs[bd_idx].log_n_blocks + 1;\n cur_pitch_val = s->min_pitch_val + get_bits(gb, s->pitch_nbits);\n cur_pitch_val = FFMIN(cur_pitch_val, s->max_pitch_val - 1);\n if (s->last_acb_type == ACB_TYPE_NONE ||\n 20 * abs(cur_pitch_val - s->last_pitch_val) >\n (cur_pitch_val + s->last_pitch_val))\n s->last_pitch_val = cur_pitch_val;\n for (n = 0; n < frame_descs[bd_idx].n_blocks; n++) {\n int fac = n * 2 + 1;\n pitch[n] = (MUL16(fac, cur_pitch_val) +\n MUL16((n_blocks_x2 - fac), s->last_pitch_val) +\n frame_descs[bd_idx].n_blocks) >> log_n_blocks_x2;\n }\n s->pitch_diff_sh16 =\n ((cur_pitch_val - s->last_pitch_val) << 16) / MAX_FRAMESIZE;\n }\n switch (frame_descs[bd_idx].fcb_type) {\n case FCB_TYPE_SILENCE:\n s->silence_gain = wmavoice_gain_silence[get_bits(gb, 8)];\n break;\n case FCB_TYPE_AW_PULSES:\n aw_parse_coords(s, gb, pitch);\n break;\n }\n for (n = 0; n < frame_descs[bd_idx].n_blocks; n++) {\n int bl_pitch_sh2;\n switch (frame_descs[bd_idx].acb_type) {\n case ACB_TYPE_HAMMING: {\n int block_pitch,\n t1 = (s->block_conv_table[1] - s->block_conv_table[0]) << 2,\n t2 = (s->block_conv_table[2] - s->block_conv_table[1]) << 1,\n t3 = s->block_conv_table[3] - s->block_conv_table[2] + 1;\n if (n == 0) {\n block_pitch = get_bits(gb, s->block_pitch_nbits);\n } else\n block_pitch = last_block_pitch - s->block_delta_pitch_hrange +\n get_bits(gb, s->block_delta_pitch_nbits);\n last_block_pitch = av_clip(block_pitch,\n s->block_delta_pitch_hrange,\n s->block_pitch_range -\n s->block_delta_pitch_hrange);\n if (block_pitch < t1) {\n bl_pitch_sh2 = (s->block_conv_table[0] << 2) + block_pitch;\n } else {\n block_pitch -= t1;\n if (block_pitch < t2) {\n bl_pitch_sh2 =\n (s->block_conv_table[1] << 2) + (block_pitch << 1);\n } else {\n block_pitch -= t2;\n if (block_pitch < t3) {\n bl_pitch_sh2 =\n (s->block_conv_table[2] + block_pitch) << 2;\n } else\n bl_pitch_sh2 = s->block_conv_table[3] << 2;\n }\n }\n pitch[n] = bl_pitch_sh2 >> 2;\n break;\n }\n case ACB_TYPE_ASYMMETRIC: {\n bl_pitch_sh2 = pitch[n] << 2;\n break;\n }\n default:\n bl_pitch_sh2 = 0;\n break;\n }\n synth_block(s, gb, n, block_nsamples, bl_pitch_sh2,\n lsps, prev_lsps, &frame_descs[bd_idx],\n &excitation[n * block_nsamples],\n &synth[n * block_nsamples]);\n }\n if (s->do_apf) {\n av_log_missing_feature(ctx, "APF", 0);\n s->do_apf = 0;\n }\n for (n = 0; n < 160; n++)\n samples[n] = av_clipf(synth[n], -1.0, 1.0);\n s->frame_cntr++;\n if (s->frame_cntr >= 0xFFFF) s->frame_cntr -= 0xFFFF;\n s->last_acb_type = frame_descs[bd_idx].acb_type;\n switch (frame_descs[bd_idx].acb_type) {\n case ACB_TYPE_NONE:\n s->last_pitch_val = 0;\n break;\n case ACB_TYPE_ASYMMETRIC:\n s->last_pitch_val = cur_pitch_val;\n break;\n case ACB_TYPE_HAMMING:\n s->last_pitch_val = pitch[frame_descs[bd_idx].n_blocks - 1];\n break;\n }\n return 0;\n}']
2,337
0
https://github.com/openssl/openssl/blob/9b02dc97e4963969da69675a871dbe80e6d31cda/crypto/bn/bn_ctx.c/#L273
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['int bn_probable_prime_dh(BIGNUM *rnd, int bits,\n const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx)\n{\n int i, ret = 0;\n BIGNUM *t1;\n BN_CTX_start(ctx);\n if ((t1 = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (!BN_priv_rand(rnd, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))\n goto err;\n if (!BN_mod(t1, rnd, add, ctx))\n goto err;\n if (!BN_sub(rnd, rnd, t1))\n goto err;\n if (rem == NULL) {\n if (!BN_add_word(rnd, 1))\n goto err;\n } else {\n if (!BN_add(rnd, rnd, rem))\n goto err;\n }\n loop:\n for (i = 1; i < NUMPRIMES; i++) {\n BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);\n if (mod == (BN_ULONG)-1)\n goto err;\n if (mod <= 1) {\n if (!BN_add(rnd, rnd, add))\n goto err;\n goto loop;\n }\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n bn_check_top(rnd);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_start", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG_EXIT(ctx);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
2,338
0
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/x509/x509_vfy.c/#L710
static int check_cert(X509_STORE_CTX *ctx) { X509_CRL *crl = NULL, *dcrl = NULL; X509 *x = NULL; int ok = 0, cnum = 0; unsigned int last_reasons = 0; cnum = ctx->error_depth; x = sk_X509_value(ctx->chain, cnum); ctx->current_cert = x; ctx->current_issuer = NULL; ctx->current_crl_score = 0; ctx->current_reasons = 0; while (ctx->current_reasons != CRLDP_ALL_REASONS) { last_reasons = ctx->current_reasons; if (ctx->get_crl) ok = ctx->get_crl(ctx, &crl, x); else ok = get_crl_delta(ctx, &crl, &dcrl, x); if (!ok) { ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL; ok = ctx->verify_cb(0, ctx); goto err; } ctx->current_crl = crl; ok = ctx->check_crl(ctx, crl); if (!ok) goto err; if (dcrl) { ok = ctx->check_crl(ctx, dcrl); if (!ok) goto err; ok = ctx->cert_crl(ctx, dcrl, x); if (!ok) goto err; } else ok = 1; if (ok != 2) { ok = ctx->cert_crl(ctx, crl, x); if (!ok) goto err; } X509_CRL_free(crl); X509_CRL_free(dcrl); crl = NULL; dcrl = NULL; if (last_reasons == ctx->current_reasons) { ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL; ok = ctx->verify_cb(0, ctx); goto err; } } err: X509_CRL_free(crl); X509_CRL_free(dcrl); ctx->current_crl = NULL; return ok; }
['static int check_cert(X509_STORE_CTX *ctx)\n{\n X509_CRL *crl = NULL, *dcrl = NULL;\n X509 *x = NULL;\n int ok = 0, cnum = 0;\n unsigned int last_reasons = 0;\n cnum = ctx->error_depth;\n x = sk_X509_value(ctx->chain, cnum);\n ctx->current_cert = x;\n ctx->current_issuer = NULL;\n ctx->current_crl_score = 0;\n ctx->current_reasons = 0;\n while (ctx->current_reasons != CRLDP_ALL_REASONS) {\n last_reasons = ctx->current_reasons;\n if (ctx->get_crl)\n ok = ctx->get_crl(ctx, &crl, x);\n else\n ok = get_crl_delta(ctx, &crl, &dcrl, x);\n if (!ok) {\n ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;\n ok = ctx->verify_cb(0, ctx);\n goto err;\n }\n ctx->current_crl = crl;\n ok = ctx->check_crl(ctx, crl);\n if (!ok)\n goto err;\n if (dcrl) {\n ok = ctx->check_crl(ctx, dcrl);\n if (!ok)\n goto err;\n ok = ctx->cert_crl(ctx, dcrl, x);\n if (!ok)\n goto err;\n } else\n ok = 1;\n if (ok != 2) {\n ok = ctx->cert_crl(ctx, crl, x);\n if (!ok)\n goto err;\n }\n X509_CRL_free(crl);\n X509_CRL_free(dcrl);\n crl = NULL;\n dcrl = NULL;\n if (last_reasons == ctx->current_reasons) {\n ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;\n ok = ctx->verify_cb(0, ctx);\n goto err;\n }\n }\n err:\n X509_CRL_free(crl);\n X509_CRL_free(dcrl);\n ctx->current_crl = NULL;\n return ok;\n}', 'DEFINE_STACK_OF(X509)', 'void *sk_value(const _STACK *st, int i)\n{\n if (!st || (i < 0) || (i >= st->num))\n return NULL;\n return st->data[i];\n}']
2,339
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 uint32_t bitstream_read(BitstreamContext *bc, unsigned n)\n{\n if (!n)\n return 0;\n if (n > bc->bits_left) {\n refill_32(bc);\n if (bc->bits_left < 32)\n bc->bits_left = n;\n }\n return get_val(bc, n);\n}', 'static inline void 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}']
2,340
0
https://github.com/openssl/openssl/blob/369782ac5027b7e4d46a708eca020d84d97ad034/crypto/lhash/lhash.c/#L282
void lh_doall_arg(LHASH *lh, void (*func)(), void *arg) { int i; LHASH_NODE *a,*n; for (i=lh->num_nodes-1; i>=0; i--) { a=lh->b[i]; while (a != NULL) { n=a->next; func(a->data,arg); a=n; } } }
['void CRYPTO_mem_leaks(BIO *b)\n\t{\n\tMEM_LEAK ml;\n\tchar buf[80];\n\tif (mh == NULL && amih == NULL)\n\t\treturn;\n\tml.bio=b;\n\tml.bytes=0;\n\tml.chunks=0;\n\tMemCheck_off();\n\tif (mh != NULL)\n\t\tlh_doall_arg(mh,(void (*)())print_leak,(char *)&ml);\n\tif (ml.chunks != 0)\n\t\t{\n\t\tsprintf(buf,"%ld bytes leaked in %d chunks\\n",\n\t\t\tml.bytes,ml.chunks);\n\t\tBIO_puts(b,buf);\n\t\t}\n\telse\n\t\t{\n\t\tCRYPTO_w_lock(CRYPTO_LOCK_MALLOC);\n\t\tif (mh != NULL)\n\t\t\t{\n\t\t\tlh_free(mh);\n\t\t\tmh = NULL;\n\t\t\t}\n\t\tif (amih != NULL)\n\t\t\t{\n\t\t\tif (lh_num_items(amih) == 0)\n\t\t\t\t{\n\t\t\t\tlh_free(amih);\n\t\t\t\tamih = NULL;\n\t\t\t\t}\n\t\t\t}\n\t\tCRYPTO_w_unlock(CRYPTO_LOCK_MALLOC);\n\t\t}\n\tMemCheck_on();\n#if 0\n\tlh_stats_bio(mh,b);\n\tlh_node_stats_bio(mh,b);\n\tlh_node_usage_stats_bio(mh,b);\n#endif\n\t}', 'void lh_doall_arg(LHASH *lh, void (*func)(), void *arg)\n\t{\n\tint i;\n\tLHASH_NODE *a,*n;\n\tfor (i=lh->num_nodes-1; i>=0; i--)\n\t\t{\n\t\ta=lh->b[i];\n\t\twhile (a != NULL)\n\t\t\t{\n\t\t\tn=a->next;\n\t\t\tfunc(a->data,arg);\n\t\t\ta=n;\n\t\t\t}\n\t\t}\n\t}']
2,341
0
https://github.com/libav/libav/blob/110d2af28e186d5ed120942f8b3d51acda59e341/libavcodec/pcm.c/#L51
static av_cold int pcm_encode_init(AVCodecContext *avctx) { avctx->frame_size = 1; switch(avctx->codec->id) { case CODEC_ID_PCM_ALAW: pcm_alaw_tableinit(); break; case CODEC_ID_PCM_MULAW: pcm_ulaw_tableinit(); break; default: break; } avctx->bits_per_coded_sample = av_get_bits_per_sample(avctx->codec->id); avctx->block_align = avctx->channels * avctx->bits_per_coded_sample/8; avctx->coded_frame= avcodec_alloc_frame(); avctx->coded_frame->key_frame= 1; return 0; }
['static av_cold int pcm_encode_init(AVCodecContext *avctx)\n{\n avctx->frame_size = 1;\n switch(avctx->codec->id) {\n case CODEC_ID_PCM_ALAW:\n pcm_alaw_tableinit();\n break;\n case CODEC_ID_PCM_MULAW:\n pcm_ulaw_tableinit();\n break;\n default:\n break;\n }\n avctx->bits_per_coded_sample = av_get_bits_per_sample(avctx->codec->id);\n avctx->block_align = avctx->channels * avctx->bits_per_coded_sample/8;\n avctx->coded_frame= avcodec_alloc_frame();\n avctx->coded_frame->key_frame= 1;\n return 0;\n}', 'int av_get_bits_per_sample(enum CodecID codec_id){\n switch(codec_id){\n case CODEC_ID_ADPCM_SBPRO_2:\n return 2;\n case CODEC_ID_ADPCM_SBPRO_3:\n return 3;\n case CODEC_ID_ADPCM_SBPRO_4:\n case CODEC_ID_ADPCM_CT:\n case CODEC_ID_ADPCM_IMA_WAV:\n case CODEC_ID_ADPCM_IMA_QT:\n case CODEC_ID_ADPCM_SWF:\n case CODEC_ID_ADPCM_MS:\n case CODEC_ID_ADPCM_YAMAHA:\n case CODEC_ID_ADPCM_G722:\n return 4;\n case CODEC_ID_PCM_ALAW:\n case CODEC_ID_PCM_MULAW:\n case CODEC_ID_PCM_S8:\n case CODEC_ID_PCM_U8:\n case CODEC_ID_PCM_ZORK:\n return 8;\n case CODEC_ID_PCM_S16BE:\n case CODEC_ID_PCM_S16LE:\n case CODEC_ID_PCM_S16LE_PLANAR:\n case CODEC_ID_PCM_U16BE:\n case CODEC_ID_PCM_U16LE:\n return 16;\n case CODEC_ID_PCM_S24DAUD:\n case CODEC_ID_PCM_S24BE:\n case CODEC_ID_PCM_S24LE:\n case CODEC_ID_PCM_U24BE:\n case CODEC_ID_PCM_U24LE:\n return 24;\n case CODEC_ID_PCM_S32BE:\n case CODEC_ID_PCM_S32LE:\n case CODEC_ID_PCM_U32BE:\n case CODEC_ID_PCM_U32LE:\n case CODEC_ID_PCM_F32BE:\n case CODEC_ID_PCM_F32LE:\n return 32;\n case CODEC_ID_PCM_F64BE:\n case CODEC_ID_PCM_F64LE:\n return 64;\n default:\n return 0;\n }\n}', 'AVFrame *avcodec_alloc_frame(void){\n AVFrame *pic= av_malloc(sizeof(AVFrame));\n if(pic==NULL) return NULL;\n avcodec_get_frame_defaults(pic);\n return pic;\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}']
2,342
0
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/asn1/ameth_lib.c/#L301
EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_new(int id, int flags, const char *pem_str, const char *info) { EVP_PKEY_ASN1_METHOD *ameth = OPENSSL_zalloc(sizeof(*ameth)); if (ameth == NULL) return NULL; ameth->pkey_id = id; ameth->pkey_base_id = id; ameth->pkey_flags = flags | ASN1_PKEY_DYNAMIC; if (info) { ameth->info = OPENSSL_strdup(info); if (!ameth->info) goto err; } if (pem_str) { ameth->pem_str = OPENSSL_strdup(pem_str); if (!ameth->pem_str) goto err; } return ameth; err: EVP_PKEY_asn1_free(ameth); return NULL; }
['EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_new(int id, int flags,\n const char *pem_str, const char *info)\n{\n EVP_PKEY_ASN1_METHOD *ameth = OPENSSL_zalloc(sizeof(*ameth));\n if (ameth == NULL)\n return NULL;\n ameth->pkey_id = id;\n ameth->pkey_base_id = id;\n ameth->pkey_flags = flags | ASN1_PKEY_DYNAMIC;\n if (info) {\n ameth->info = OPENSSL_strdup(info);\n if (!ameth->info)\n goto err;\n }\n if (pem_str) {\n ameth->pem_str = OPENSSL_strdup(pem_str);\n if (!ameth->pem_str)\n goto err;\n }\n return ameth;\n err:\n EVP_PKEY_asn1_free(ameth);\n return NULL;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (num <= 0)\n return NULL;\n allow_customize = 0;\n#ifdef CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)file;\n (void)line;\n ret = malloc(num);\n#endif\n#ifndef OPENSSL_CPUID_OBJ\n if (ret && (num > 2048)) {\n extern unsigned char cleanse_ctr;\n ((unsigned char *)ret)[0] = cleanse_ctr;\n }\n#endif\n return ret;\n}', 'char *CRYPTO_strdup(const char *str, const char* file, int line)\n{\n char *ret;\n if (str == NULL)\n return NULL;\n ret = CRYPTO_malloc(strlen(str) + 1, file, line);\n if (ret != NULL)\n strcpy(ret, str);\n return ret;\n}', 'void EVP_PKEY_asn1_free(EVP_PKEY_ASN1_METHOD *ameth)\n{\n if (ameth && (ameth->pkey_flags & ASN1_PKEY_DYNAMIC)) {\n OPENSSL_free(ameth->pem_str);\n OPENSSL_free(ameth->info);\n OPENSSL_free(ameth);\n }\n}']
2,343
0
https://github.com/openssl/openssl/blob/1a50eedf2a1fbb1e0e009ad616d8be678e4c6340/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; }
['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 || !TEST_size_t_eq(fake_rand_bytes_offset, fake_rand_size)) {\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 BN_hex2bn(BIGNUM **bn, const char *a)\n{\n BIGNUM *ret = NULL;\n BN_ULONG l = 0;\n int neg = 0, h, m, i, j, k, c;\n int num;\n if (a == NULL || *a == '\\0')\n return 0;\n if (*a == '-') {\n neg = 1;\n a++;\n }\n for (i = 0; i <= INT_MAX / 4 && ossl_isxdigit(a[i]); i++)\n continue;\n if (i == 0 || i > INT_MAX / 4)\n goto err;\n num = i + neg;\n if (bn == NULL)\n return num;\n if (*bn == NULL) {\n if ((ret = BN_new()) == NULL)\n return 0;\n } else {\n ret = *bn;\n BN_zero(ret);\n }\n if (bn_expand(ret, i * 4) == NULL)\n goto err;\n j = i;\n m = 0;\n h = 0;\n while (j > 0) {\n m = (BN_BYTES * 2 <= j) ? BN_BYTES * 2 : j;\n l = 0;\n for (;;) {\n c = a[j - m];\n k = OPENSSL_hexchar2int(c);\n if (k < 0)\n k = 0;\n l = (l << 4) | k;\n if (--m <= 0) {\n ret->d[h++] = l;\n break;\n }\n }\n j -= BN_BYTES * 2;\n }\n ret->top = h;\n bn_correct_top(ret);\n *bn = ret;\n bn_check_top(ret);\n if (ret->top != 0)\n ret->neg = neg;\n return num;\n err:\n if (*bn == NULL)\n BN_free(ret);\n return 0;\n}", 'int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,\n const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)\n{\n const EC_POINT *points[1];\n const BIGNUM *scalars[1];\n points[0] = point;\n scalars[0] = p_scalar;\n return EC_POINTs_mul(group, r, g_scalar,\n (point != NULL\n && p_scalar != NULL), points, scalars, ctx);\n}', 'int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,\n size_t num, const EC_POINT *points[],\n const BIGNUM *scalars[], BN_CTX *ctx)\n{\n int ret = 0;\n size_t i = 0;\n BN_CTX *new_ctx = NULL;\n if ((scalar == NULL) && (num == 0)) {\n return EC_POINT_set_to_infinity(group, r);\n }\n if (!ec_point_is_compat(r, group)) {\n ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);\n return 0;\n }\n for (i = 0; i < num; i++) {\n if (!ec_point_is_compat(points[i], group)) {\n ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);\n return 0;\n }\n }\n if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL) {\n ECerr(EC_F_EC_POINTS_MUL, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n if (group->meth->mul != NULL)\n ret = group->meth->mul(group, r, scalar, num, points, scalars, ctx);\n else\n ret = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,\n size_t num, const EC_POINT *points[], const BIGNUM *scalars[],\n BN_CTX *ctx)\n{\n const EC_POINT *generator = NULL;\n EC_POINT *tmp = NULL;\n size_t totalnum;\n size_t blocksize = 0, numblocks = 0;\n size_t pre_points_per_block = 0;\n size_t i, j;\n int k;\n int r_is_inverted = 0;\n int r_is_at_infinity = 1;\n size_t *wsize = NULL;\n signed char **wNAF = NULL;\n size_t *wNAF_len = NULL;\n size_t max_len = 0;\n size_t num_val;\n EC_POINT **val = NULL;\n EC_POINT **v;\n EC_POINT ***val_sub = NULL;\n const EC_PRE_COMP *pre_comp = NULL;\n int num_scalar = 0;\n int ret = 0;\n if (!BN_is_zero(group->order) && !BN_is_zero(group->cofactor)) {\n if ((scalar != NULL) && (num == 0)) {\n return ec_scalar_mul_ladder(group, r, scalar, NULL, ctx);\n }\n if ((scalar == NULL) && (num == 1)) {\n return ec_scalar_mul_ladder(group, r, scalars[0], points[0], ctx);\n }\n }\n if (scalar != NULL) {\n generator = EC_GROUP_get0_generator(group);\n if (generator == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, EC_R_UNDEFINED_GENERATOR);\n goto err;\n }\n pre_comp = group->pre_comp.ec;\n if (pre_comp && pre_comp->numblocks\n && (EC_POINT_cmp(group, generator, pre_comp->points[0], ctx) ==\n 0)) {\n blocksize = pre_comp->blocksize;\n numblocks = (BN_num_bits(scalar) / blocksize) + 1;\n if (numblocks > pre_comp->numblocks)\n numblocks = pre_comp->numblocks;\n pre_points_per_block = (size_t)1 << (pre_comp->w - 1);\n if (pre_comp->num != (pre_comp->numblocks * pre_points_per_block)) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n } else {\n pre_comp = NULL;\n numblocks = 1;\n num_scalar = 1;\n }\n }\n totalnum = num + numblocks;\n wsize = OPENSSL_malloc(totalnum * sizeof(wsize[0]));\n wNAF_len = OPENSSL_malloc(totalnum * sizeof(wNAF_len[0]));\n wNAF = OPENSSL_malloc((totalnum + 1) * sizeof(wNAF[0]));\n val_sub = OPENSSL_malloc(totalnum * sizeof(val_sub[0]));\n if (wNAF != NULL)\n wNAF[0] = NULL;\n if (wsize == NULL || wNAF_len == NULL || wNAF == NULL || val_sub == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n num_val = 0;\n for (i = 0; i < num + num_scalar; i++) {\n size_t bits;\n bits = i < num ? BN_num_bits(scalars[i]) : BN_num_bits(scalar);\n wsize[i] = EC_window_bits_for_scalar_size(bits);\n num_val += (size_t)1 << (wsize[i] - 1);\n wNAF[i + 1] = NULL;\n wNAF[i] =\n bn_compute_wNAF((i < num ? scalars[i] : scalar), wsize[i],\n &wNAF_len[i]);\n if (wNAF[i] == NULL)\n goto err;\n if (wNAF_len[i] > max_len)\n max_len = wNAF_len[i];\n }\n if (numblocks) {\n if (pre_comp == NULL) {\n if (num_scalar != 1) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n } else {\n signed char *tmp_wNAF = NULL;\n size_t tmp_len = 0;\n if (num_scalar != 0) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n wsize[num] = pre_comp->w;\n tmp_wNAF = bn_compute_wNAF(scalar, wsize[num], &tmp_len);\n if (!tmp_wNAF)\n goto err;\n if (tmp_len <= max_len) {\n numblocks = 1;\n totalnum = num + 1;\n wNAF[num] = tmp_wNAF;\n wNAF[num + 1] = NULL;\n wNAF_len[num] = tmp_len;\n val_sub[num] = pre_comp->points;\n } else {\n signed char *pp;\n EC_POINT **tmp_points;\n if (tmp_len < numblocks * blocksize) {\n numblocks = (tmp_len + blocksize - 1) / blocksize;\n if (numblocks > pre_comp->numblocks) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n OPENSSL_free(tmp_wNAF);\n goto err;\n }\n totalnum = num + numblocks;\n }\n pp = tmp_wNAF;\n tmp_points = pre_comp->points;\n for (i = num; i < totalnum; i++) {\n if (i < totalnum - 1) {\n wNAF_len[i] = blocksize;\n if (tmp_len < blocksize) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n OPENSSL_free(tmp_wNAF);\n goto err;\n }\n tmp_len -= blocksize;\n } else\n wNAF_len[i] = tmp_len;\n wNAF[i + 1] = NULL;\n wNAF[i] = OPENSSL_malloc(wNAF_len[i]);\n if (wNAF[i] == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);\n OPENSSL_free(tmp_wNAF);\n goto err;\n }\n memcpy(wNAF[i], pp, wNAF_len[i]);\n if (wNAF_len[i] > max_len)\n max_len = wNAF_len[i];\n if (*tmp_points == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n OPENSSL_free(tmp_wNAF);\n goto err;\n }\n val_sub[i] = tmp_points;\n tmp_points += pre_points_per_block;\n pp += blocksize;\n }\n OPENSSL_free(tmp_wNAF);\n }\n }\n }\n val = OPENSSL_malloc((num_val + 1) * sizeof(val[0]));\n if (val == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n val[num_val] = NULL;\n v = val;\n for (i = 0; i < num + num_scalar; i++) {\n val_sub[i] = v;\n for (j = 0; j < ((size_t)1 << (wsize[i] - 1)); j++) {\n *v = EC_POINT_new(group);\n if (*v == NULL)\n goto err;\n v++;\n }\n }\n if (!(v == val + num_val)) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if ((tmp = EC_POINT_new(group)) == NULL)\n goto err;\n for (i = 0; i < num + num_scalar; i++) {\n if (i < num) {\n if (!EC_POINT_copy(val_sub[i][0], points[i]))\n goto err;\n } else {\n if (!EC_POINT_copy(val_sub[i][0], generator))\n goto err;\n }\n if (wsize[i] > 1) {\n if (!EC_POINT_dbl(group, tmp, val_sub[i][0], ctx))\n goto err;\n for (j = 1; j < ((size_t)1 << (wsize[i] - 1)); j++) {\n if (!EC_POINT_add\n (group, val_sub[i][j], val_sub[i][j - 1], tmp, ctx))\n goto err;\n }\n }\n }\n if (!EC_POINTs_make_affine(group, num_val, val, ctx))\n goto err;\n r_is_at_infinity = 1;\n for (k = max_len - 1; k >= 0; k--) {\n if (!r_is_at_infinity) {\n if (!EC_POINT_dbl(group, r, r, ctx))\n goto err;\n }\n for (i = 0; i < totalnum; i++) {\n if (wNAF_len[i] > (size_t)k) {\n int digit = wNAF[i][k];\n int is_neg;\n if (digit) {\n is_neg = digit < 0;\n if (is_neg)\n digit = -digit;\n if (is_neg != r_is_inverted) {\n if (!r_is_at_infinity) {\n if (!EC_POINT_invert(group, r, ctx))\n goto err;\n }\n r_is_inverted = !r_is_inverted;\n }\n if (r_is_at_infinity) {\n if (!EC_POINT_copy(r, val_sub[i][digit >> 1]))\n goto err;\n r_is_at_infinity = 0;\n } else {\n if (!EC_POINT_add\n (group, r, r, val_sub[i][digit >> 1], ctx))\n goto err;\n }\n }\n }\n }\n }\n if (r_is_at_infinity) {\n if (!EC_POINT_set_to_infinity(group, r))\n goto err;\n } else {\n if (r_is_inverted)\n if (!EC_POINT_invert(group, r, ctx))\n goto err;\n }\n ret = 1;\n err:\n EC_POINT_free(tmp);\n OPENSSL_free(wsize);\n OPENSSL_free(wNAF_len);\n if (wNAF != NULL) {\n signed char **w;\n for (w = wNAF; *w != NULL; w++)\n OPENSSL_free(*w);\n OPENSSL_free(wNAF);\n }\n if (val != NULL) {\n for (v = val; *v != NULL; v++)\n EC_POINT_clear_free(*v);\n OPENSSL_free(val);\n }\n OPENSSL_free(val_sub);\n return ret;\n}', 'int ec_scalar_mul_ladder(const EC_GROUP *group, EC_POINT *r,\n const BIGNUM *scalar, const EC_POINT *point,\n BN_CTX *ctx)\n{\n int i, cardinality_bits, group_top, kbit, pbit, Z_is_one;\n EC_POINT *p = NULL;\n EC_POINT *s = NULL;\n BIGNUM *k = NULL;\n BIGNUM *lambda = NULL;\n BIGNUM *cardinality = NULL;\n int ret = 0;\n if (point != NULL && EC_POINT_is_at_infinity(group, point))\n return EC_POINT_set_to_infinity(group, r);\n if (BN_is_zero(group->order)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, EC_R_UNKNOWN_ORDER);\n return 0;\n }\n if (BN_is_zero(group->cofactor)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, EC_R_UNKNOWN_COFACTOR);\n return 0;\n }\n BN_CTX_start(ctx);\n if (((p = EC_POINT_new(group)) == NULL)\n || ((s = EC_POINT_new(group)) == NULL)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (point == NULL) {\n if (!EC_POINT_copy(p, group->generator)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_EC_LIB);\n goto err;\n }\n } else {\n if (!EC_POINT_copy(p, point)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_EC_LIB);\n goto err;\n }\n }\n EC_POINT_BN_set_flags(p, BN_FLG_CONSTTIME);\n EC_POINT_BN_set_flags(r, BN_FLG_CONSTTIME);\n EC_POINT_BN_set_flags(s, BN_FLG_CONSTTIME);\n cardinality = BN_CTX_get(ctx);\n lambda = BN_CTX_get(ctx);\n k = BN_CTX_get(ctx);\n if (k == NULL) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (!BN_mul(cardinality, group->order, group->cofactor, ctx)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_BN_LIB);\n goto err;\n }\n cardinality_bits = BN_num_bits(cardinality);\n group_top = bn_get_top(cardinality);\n if ((bn_wexpand(k, group_top + 1) == NULL)\n || (bn_wexpand(lambda, group_top + 1) == NULL)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_BN_LIB);\n goto err;\n }\n if (!BN_copy(k, scalar)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_BN_LIB);\n goto err;\n }\n BN_set_flags(k, BN_FLG_CONSTTIME);\n if ((BN_num_bits(k) > cardinality_bits) || (BN_is_negative(k))) {\n if (!BN_nnmod(k, k, cardinality, ctx)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_BN_LIB);\n goto err;\n }\n }\n if (!BN_add(lambda, k, cardinality)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_BN_LIB);\n goto err;\n }\n BN_set_flags(lambda, BN_FLG_CONSTTIME);\n if (!BN_add(k, lambda, cardinality)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_BN_LIB);\n goto err;\n }\n kbit = BN_is_bit_set(lambda, cardinality_bits);\n BN_consttime_swap(kbit, k, lambda, group_top + 1);\n group_top = bn_get_top(group->field);\n if ((bn_wexpand(s->X, group_top) == NULL)\n || (bn_wexpand(s->Y, group_top) == NULL)\n || (bn_wexpand(s->Z, group_top) == NULL)\n || (bn_wexpand(r->X, group_top) == NULL)\n || (bn_wexpand(r->Y, group_top) == NULL)\n || (bn_wexpand(r->Z, group_top) == NULL)\n || (bn_wexpand(p->X, group_top) == NULL)\n || (bn_wexpand(p->Y, group_top) == NULL)\n || (bn_wexpand(p->Z, group_top) == NULL)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_BN_LIB);\n goto err;\n }\n if (!ec_point_blind_coordinates(group, p, ctx)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, EC_R_POINT_COORDINATES_BLIND_FAILURE);\n goto err;\n }\n if (!ec_point_ladder_pre(group, r, s, p, ctx)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, EC_R_LADDER_PRE_FAILURE);\n goto err;\n }\n pbit = 1;\n#define EC_POINT_CSWAP(c, a, b, w, t) do { \\\n BN_consttime_swap(c, (a)->X, (b)->X, w); \\\n BN_consttime_swap(c, (a)->Y, (b)->Y, w); \\\n BN_consttime_swap(c, (a)->Z, (b)->Z, w); \\\n t = ((a)->Z_is_one ^ (b)->Z_is_one) & (c); \\\n (a)->Z_is_one ^= (t); \\\n (b)->Z_is_one ^= (t); \\\n} while(0)\n for (i = cardinality_bits - 1; i >= 0; i--) {\n kbit = BN_is_bit_set(k, i) ^ pbit;\n EC_POINT_CSWAP(kbit, r, s, group_top, Z_is_one);\n if (!ec_point_ladder_step(group, r, s, p, ctx)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, EC_R_LADDER_STEP_FAILURE);\n goto err;\n }\n pbit ^= kbit;\n }\n EC_POINT_CSWAP(pbit, r, s, group_top, Z_is_one);\n#undef EC_POINT_CSWAP\n if (!ec_point_ladder_post(group, r, s, p, ctx)) {\n ECerr(EC_F_EC_SCALAR_MUL_LADDER, EC_R_LADDER_POST_FAILURE);\n goto err;\n }\n ret = 1;\n err:\n EC_POINT_free(p);\n EC_POINT_free(s);\n BN_CTX_end(ctx);\n return ret;\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n bn_check_top(b);\n if (a == b)\n return a;\n if (bn_wexpand(a, b->top) == NULL)\n return NULL;\n if (b->top > 0)\n memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n a->neg = b->neg;\n a->top = b->top;\n a->flags |= b->flags & BN_FLG_FIXED_TOP;\n bn_check_top(a);\n return a;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']
2,344
0
https://github.com/openssl/openssl/blob/8ea92ddd13495e63855a61dcc96963ffdf6df495/crypto/buffer/buffer.c/#L146
int BUF_MEM_grow_clean(BUF_MEM *str, size_t len) { char *ret; size_t n; if (str->length >= len) { memset(&str->data[len],0,str->length-len); str->length=len; return(len); } if (str->max >= len) { memset(&str->data[str->length],0,len-str->length); str->length=len; return(len); } if (len > LIMIT_BEFORE_EXPANSION) { BUFerr(BUF_F_BUF_MEM_GROW,ERR_R_MALLOC_FAILURE); return 0; } n=(len+3)/3*4; if (str->data == NULL) ret=OPENSSL_malloc(n); else ret=OPENSSL_realloc_clean(str->data,str->max,n); if (ret == NULL) { BUFerr(BUF_F_BUF_MEM_GROW_CLEAN,ERR_R_MALLOC_FAILURE); len=0; } else { str->data=ret; str->max=n; memset(&str->data[str->length],0,len-str->length); str->length=len; } return(len); }
['static int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb)\n\t{\n\tBUF_MEM *b;\n\tunsigned char *p;\n\tint i;\n\tASN1_const_CTX c;\n\tsize_t want=HEADER_SIZE;\n\tint eos=0;\n\tsize_t off=0;\n\tsize_t len=0;\n\tb=BUF_MEM_new();\n\tif (b == NULL)\n\t\t{\n\t\tASN1err(ASN1_F_ASN1_D2I_READ_BIO,ERR_R_MALLOC_FAILURE);\n\t\treturn -1;\n\t\t}\n\tERR_clear_error();\n\tfor (;;)\n\t\t{\n\t\tif (want >= (len-off))\n\t\t\t{\n\t\t\twant-=(len-off);\n\t\t\tif (len + want < len || !BUF_MEM_grow_clean(b,len+want))\n\t\t\t\t{\n\t\t\t\tASN1err(ASN1_F_ASN1_D2I_READ_BIO,ERR_R_MALLOC_FAILURE);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\ti=BIO_read(in,&(b->data[len]),want);\n\t\t\tif ((i < 0) && ((len-off) == 0))\n\t\t\t\t{\n\t\t\t\tASN1err(ASN1_F_ASN1_D2I_READ_BIO,ASN1_R_NOT_ENOUGH_DATA);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif (i > 0)\n\t\t\t\t{\n\t\t\t\tif (len+i < len)\n\t\t\t\t\t{\n\t\t\t\t\tASN1err(ASN1_F_ASN1_D2I_READ_BIO,ASN1_R_TOO_LONG);\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t\tlen+=i;\n\t\t\t\t}\n\t\t\t}\n\t\tp=(unsigned char *)&(b->data[off]);\n\t\tc.p=p;\n\t\tc.inf=ASN1_get_object(&(c.p),&(c.slen),&(c.tag),&(c.xclass),\n\t\t\tlen-off);\n\t\tif (c.inf & 0x80)\n\t\t\t{\n\t\t\tunsigned long e;\n\t\t\te=ERR_GET_REASON(ERR_peek_error());\n\t\t\tif (e != ASN1_R_TOO_LONG)\n\t\t\t\tgoto err;\n\t\t\telse\n\t\t\t\tERR_clear_error();\n\t\t\t}\n\t\ti=c.p-p;\n\t\toff+=i;\n\t\tif (c.inf & 1)\n\t\t\t{\n\t\t\teos++;\n\t\t\tif (eos < 0)\n\t\t\t\t{\n\t\t\t\tASN1err(ASN1_F_ASN1_D2I_READ_BIO,ASN1_R_HEADER_TOO_LONG);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\twant=HEADER_SIZE;\n\t\t\t}\n\t\telse if (eos && (c.slen == 0) && (c.tag == V_ASN1_EOC))\n\t\t\t{\n\t\t\teos--;\n\t\t\tif (eos <= 0)\n\t\t\t\tbreak;\n\t\t\telse\n\t\t\t\twant=HEADER_SIZE;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\twant=c.slen;\n\t\t\tif (want > (len-off))\n\t\t\t\t{\n\t\t\t\twant-=(len-off);\n\t\t\t\tif (want > INT_MAX ||\n\t\t\t\t\tlen+want < len)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tASN1err(ASN1_F_ASN1_D2I_READ_BIO,ASN1_R_TOO_LONG);\n\t\t\t\t\t\tgoto err;\n\t\t\t\t\t\t}\n\t\t\t\tif (!BUF_MEM_grow_clean(b,len+want))\n\t\t\t\t\t{\n\t\t\t\t\tASN1err(ASN1_F_ASN1_D2I_READ_BIO,ERR_R_MALLOC_FAILURE);\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t\twhile (want > 0)\n\t\t\t\t\t{\n\t\t\t\t\ti=BIO_read(in,&(b->data[len]),want);\n\t\t\t\t\tif (i <= 0)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tASN1err(ASN1_F_ASN1_D2I_READ_BIO,\n\t\t\t\t\t\t ASN1_R_NOT_ENOUGH_DATA);\n\t\t\t\t\t\tgoto err;\n\t\t\t\t\t\t}\n\t\t\t\t\tlen+=i;\n\t\t\t\t\twant-=i;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\tif (off + c.slen < off)\n\t\t\t\t{\n\t\t\t\tASN1err(ASN1_F_ASN1_D2I_READ_BIO,ASN1_R_TOO_LONG);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\toff+=c.slen;\n\t\t\tif (eos <= 0)\n\t\t\t\t{\n\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\twant=HEADER_SIZE;\n\t\t\t}\n\t\t}\n\tif (off > INT_MAX)\n\t\t{\n\t\tASN1err(ASN1_F_ASN1_D2I_READ_BIO,ASN1_R_TOO_LONG);\n\t\tgoto err;\n\t\t}\n\t*pb = b;\n\treturn off;\nerr:\n\tif (b != NULL) BUF_MEM_free(b);\n\treturn -1;\n\t}', 'int BUF_MEM_grow_clean(BUF_MEM *str, size_t len)\n\t{\n\tchar *ret;\n\tsize_t n;\n\tif (str->length >= len)\n\t\t{\n\t\tmemset(&str->data[len],0,str->length-len);\n\t\tstr->length=len;\n\t\treturn(len);\n\t\t}\n\tif (str->max >= len)\n\t\t{\n\t\tmemset(&str->data[str->length],0,len-str->length);\n\t\tstr->length=len;\n\t\treturn(len);\n\t\t}\n\tif (len > LIMIT_BEFORE_EXPANSION)\n\t\t{\n\t\tBUFerr(BUF_F_BUF_MEM_GROW,ERR_R_MALLOC_FAILURE);\n\t\treturn 0;\n\t\t}\n\tn=(len+3)/3*4;\n\tif (str->data == NULL)\n\t\tret=OPENSSL_malloc(n);\n\telse\n\t\tret=OPENSSL_realloc_clean(str->data,str->max,n);\n\tif (ret == NULL)\n\t\t{\n\t\tBUFerr(BUF_F_BUF_MEM_GROW_CLEAN,ERR_R_MALLOC_FAILURE);\n\t\tlen=0;\n\t\t}\n\telse\n\t\t{\n\t\tstr->data=ret;\n\t\tstr->max=n;\n\t\tmemset(&str->data[str->length],0,len-str->length);\n\t\tstr->length=len;\n\t\t}\n\treturn(len);\n\t}']
2,345
1
https://github.com/libav/libav/blob/264808219d8be93aeda0b6ade8c64898b673f6bc/libavcodec/h264.c/#L3721
static int execute_decode_slices(H264Context *h, int context_count){ MpegEncContext * const s = &h->s; AVCodecContext * const avctx= s->avctx; H264Context *hx; int i; if (s->avctx->hwaccel || s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) return 0; if(context_count == 1) { return decode_slice(avctx, &h); } else { for(i = 1; i < context_count; i++) { hx = h->thread_context[i]; hx->s.err_recognition = avctx->err_recognition; hx->s.error_count = 0; } avctx->execute(avctx, decode_slice, h->thread_context, NULL, context_count, sizeof(void*)); hx = h->thread_context[context_count - 1]; s->mb_x = hx->s.mb_x; s->mb_y = hx->s.mb_y; s->dropable = hx->s.dropable; s->picture_structure = hx->s.picture_structure; for(i = 1; i < context_count; i++) h->s.error_count += h->thread_context[i]->s.error_count; } return 0; }
['static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size){\n MpegEncContext * const s = &h->s;\n AVCodecContext * const avctx= s->avctx;\n H264Context *hx;\n int buf_index;\n int context_count;\n int next_avc;\n int pass = !(avctx->active_thread_type & FF_THREAD_FRAME);\n int nals_needed=0;\n int nal_index;\n h->max_contexts = (HAVE_THREADS && (s->avctx->active_thread_type&FF_THREAD_SLICE)) ? avctx->thread_count : 1;\n if(!(s->flags2 & CODEC_FLAG2_CHUNKS)){\n h->current_slice = 0;\n if (!s->first_field)\n s->current_picture_ptr= NULL;\n ff_h264_reset_sei(h);\n }\n for(;pass <= 1;pass++){\n buf_index = 0;\n context_count = 0;\n next_avc = h->is_avc ? 0 : buf_size;\n nal_index = 0;\n for(;;){\n int consumed;\n int dst_length;\n int bit_length;\n uint8_t *ptr;\n int i, nalsize = 0;\n int err;\n if(buf_index >= next_avc) {\n if (buf_index >= buf_size - h->nal_length_size) break;\n nalsize = 0;\n for(i = 0; i < h->nal_length_size; i++)\n nalsize = (nalsize << 8) | buf[buf_index++];\n if(nalsize <= 0 || nalsize > buf_size - buf_index){\n av_log(h->s.avctx, AV_LOG_ERROR, "AVC: nal size %d\\n", nalsize);\n break;\n }\n next_avc= buf_index + nalsize;\n } else {\n for(; buf_index + 3 < next_avc; buf_index++){\n if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)\n break;\n }\n if(buf_index+3 >= buf_size) break;\n buf_index+=3;\n if(buf_index >= next_avc) continue;\n }\n hx = h->thread_context[context_count];\n ptr= ff_h264_decode_nal(hx, buf + buf_index, &dst_length, &consumed, next_avc - buf_index);\n if (ptr==NULL || dst_length < 0){\n return -1;\n }\n i= buf_index + consumed;\n if((s->workaround_bugs & FF_BUG_AUTODETECT) && i+3<next_avc &&\n buf[i]==0x00 && buf[i+1]==0x00 && buf[i+2]==0x01 && buf[i+3]==0xE0)\n s->workaround_bugs |= FF_BUG_TRUNCATED;\n if(!(s->workaround_bugs & FF_BUG_TRUNCATED)){\n while(ptr[dst_length - 1] == 0 && dst_length > 0)\n dst_length--;\n }\n bit_length= !dst_length ? 0 : (8*dst_length - ff_h264_decode_rbsp_trailing(h, ptr + dst_length - 1));\n if(s->avctx->debug&FF_DEBUG_STARTCODE){\n av_log(h->s.avctx, AV_LOG_DEBUG, "NAL %d at %d/%d length %d\\n", hx->nal_unit_type, buf_index, buf_size, dst_length);\n }\n if (h->is_avc && (nalsize != consumed) && nalsize){\n if (bit_length & 7)\n ptr[bit_length >> 3] = ptr[bit_length >> 3] & (0xff << 8 - (bit_length & 7));\n av_log(h->s.avctx, AV_LOG_DEBUG, "AVC: Consumed only %d bytes instead of %d\\n", consumed, nalsize);\n }\n buf_index += consumed;\n nal_index++;\n if(pass == 0) {\n switch (hx->nal_unit_type) {\n case NAL_SPS:\n case NAL_PPS:\n nals_needed = nal_index;\n break;\n case NAL_IDR_SLICE:\n case NAL_SLICE:\n init_get_bits(&hx->s.gb, ptr, bit_length);\n if (!get_ue_golomb(&hx->s.gb))\n nals_needed = nal_index;\n }\n continue;\n }\n if(avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0)\n continue;\n again:\n err = 0;\n switch(hx->nal_unit_type){\n case NAL_IDR_SLICE:\n if (h->nal_unit_type != NAL_IDR_SLICE) {\n av_log(h->s.avctx, AV_LOG_ERROR, "Invalid mix of idr and non-idr slices");\n return -1;\n }\n idr(h);\n case NAL_SLICE:\n init_get_bits(&hx->s.gb, ptr, bit_length);\n hx->intra_gb_ptr=\n hx->inter_gb_ptr= &hx->s.gb;\n hx->s.data_partitioning = 0;\n if((err = decode_slice_header(hx, h)))\n break;\n s->current_picture_ptr->f.key_frame |=\n (hx->nal_unit_type == NAL_IDR_SLICE) ||\n (h->sei_recovery_frame_cnt >= 0);\n if (h->current_slice == 1) {\n if(!(s->flags2 & CODEC_FLAG2_CHUNKS)) {\n decode_postinit(h, nal_index >= nals_needed);\n }\n if (s->avctx->hwaccel && s->avctx->hwaccel->start_frame(s->avctx, NULL, 0) < 0)\n return -1;\n if(CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)\n ff_vdpau_h264_picture_start(s);\n }\n if(hx->redundant_pic_count==0\n && (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)\n && (avctx->skip_frame < AVDISCARD_BIDIR || hx->slice_type_nos!=AV_PICTURE_TYPE_B)\n && (avctx->skip_frame < AVDISCARD_NONKEY || hx->slice_type_nos==AV_PICTURE_TYPE_I)\n && avctx->skip_frame < AVDISCARD_ALL){\n if(avctx->hwaccel) {\n if (avctx->hwaccel->decode_slice(avctx, &buf[buf_index - consumed], consumed) < 0)\n return -1;\n }else\n if(CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU){\n static const uint8_t start_code[] = {0x00, 0x00, 0x01};\n ff_vdpau_add_data_chunk(s, start_code, sizeof(start_code));\n ff_vdpau_add_data_chunk(s, &buf[buf_index - consumed], consumed );\n }else\n context_count++;\n }\n break;\n case NAL_DPA:\n init_get_bits(&hx->s.gb, ptr, bit_length);\n hx->intra_gb_ptr=\n hx->inter_gb_ptr= NULL;\n if ((err = decode_slice_header(hx, h)) < 0)\n break;\n hx->s.data_partitioning = 1;\n break;\n case NAL_DPB:\n init_get_bits(&hx->intra_gb, ptr, bit_length);\n hx->intra_gb_ptr= &hx->intra_gb;\n break;\n case NAL_DPC:\n init_get_bits(&hx->inter_gb, ptr, bit_length);\n hx->inter_gb_ptr= &hx->inter_gb;\n if(hx->redundant_pic_count==0 && hx->intra_gb_ptr && hx->s.data_partitioning\n && s->context_initialized\n && (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)\n && (avctx->skip_frame < AVDISCARD_BIDIR || hx->slice_type_nos!=AV_PICTURE_TYPE_B)\n && (avctx->skip_frame < AVDISCARD_NONKEY || hx->slice_type_nos==AV_PICTURE_TYPE_I)\n && avctx->skip_frame < AVDISCARD_ALL)\n context_count++;\n break;\n case NAL_SEI:\n init_get_bits(&s->gb, ptr, bit_length);\n ff_h264_decode_sei(h);\n break;\n case NAL_SPS:\n init_get_bits(&s->gb, ptr, bit_length);\n ff_h264_decode_seq_parameter_set(h);\n if (s->flags& CODEC_FLAG_LOW_DELAY ||\n (h->sps.bitstream_restriction_flag && !h->sps.num_reorder_frames))\n s->low_delay=1;\n if(avctx->has_b_frames < 2)\n avctx->has_b_frames= !s->low_delay;\n if (avctx->bits_per_raw_sample != h->sps.bit_depth_luma ||\n h->cur_chroma_format_idc != h->sps.chroma_format_idc) {\n if (h->sps.bit_depth_luma >= 8 && h->sps.bit_depth_luma <= 10) {\n avctx->bits_per_raw_sample = h->sps.bit_depth_luma;\n h->cur_chroma_format_idc = h->sps.chroma_format_idc;\n h->pixel_shift = h->sps.bit_depth_luma > 8;\n ff_h264dsp_init(&h->h264dsp, h->sps.bit_depth_luma, h->sps.chroma_format_idc);\n ff_h264_pred_init(&h->hpc, s->codec_id, h->sps.bit_depth_luma, h->sps.chroma_format_idc);\n s->dsp.dct_bits = h->sps.bit_depth_luma > 8 ? 32 : 16;\n dsputil_init(&s->dsp, s->avctx);\n } else {\n av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\\n", h->sps.bit_depth_luma);\n return -1;\n }\n }\n break;\n case NAL_PPS:\n init_get_bits(&s->gb, ptr, bit_length);\n ff_h264_decode_picture_parameter_set(h, bit_length);\n break;\n case NAL_AUD:\n case NAL_END_SEQUENCE:\n case NAL_END_STREAM:\n case NAL_FILLER_DATA:\n case NAL_SPS_EXT:\n case NAL_AUXILIARY_SLICE:\n break;\n default:\n av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\\n", hx->nal_unit_type, bit_length);\n }\n if(context_count == h->max_contexts) {\n execute_decode_slices(h, context_count);\n context_count = 0;\n }\n if (err < 0)\n av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\\n");\n else if(err == 1) {\n h->nal_unit_type = hx->nal_unit_type;\n h->nal_ref_idc = hx->nal_ref_idc;\n hx = h;\n goto again;\n }\n }\n }\n if(context_count)\n execute_decode_slices(h, context_count);\n return buf_index;\n}', 'static int execute_decode_slices(H264Context *h, int context_count){\n MpegEncContext * const s = &h->s;\n AVCodecContext * const avctx= s->avctx;\n H264Context *hx;\n int i;\n if (s->avctx->hwaccel || s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)\n return 0;\n if(context_count == 1) {\n return decode_slice(avctx, &h);\n } else {\n for(i = 1; i < context_count; i++) {\n hx = h->thread_context[i];\n hx->s.err_recognition = avctx->err_recognition;\n hx->s.error_count = 0;\n }\n avctx->execute(avctx, decode_slice,\n h->thread_context, NULL, context_count, sizeof(void*));\n hx = h->thread_context[context_count - 1];\n s->mb_x = hx->s.mb_x;\n s->mb_y = hx->s.mb_y;\n s->dropable = hx->s.dropable;\n s->picture_structure = hx->s.picture_structure;\n for(i = 1; i < context_count; i++)\n h->s.error_count += h->thread_context[i]->s.error_count;\n }\n return 0;\n}']
2,346
0
https://github.com/openssl/openssl/blob/09977dd095f3c655c99b9e1810a213f7eafa7364/crypto/bn/bn_sqr.c/#L161
void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp) { int i, j, max; const BN_ULONG *ap; BN_ULONG *rp; max = n * 2; ap = a; rp = r; rp[0] = rp[max - 1] = 0; rp++; j = n; if (--j > 0) { ap++; rp[j] = bn_mul_words(rp, ap, j, ap[-1]); rp += 2; } for (i = n - 2; i > 0; i--) { j--; ap++; rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]); rp += 2; } bn_add_words(r, r, r, max); bn_sqr_words(tmp, a, n); bn_add_words(r, r, tmp, max); }
['int ec_GF2m_simple_oct2point(const EC_GROUP *group, EC_POINT *point,\n const unsigned char *buf, size_t len,\n BN_CTX *ctx)\n{\n point_conversion_form_t form;\n int y_bit;\n BN_CTX *new_ctx = NULL;\n BIGNUM *x, *y, *yxi;\n size_t field_len, enc_len;\n int ret = 0;\n if (len == 0) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);\n return 0;\n }\n form = buf[0];\n y_bit = form & 1;\n form = form & ~1U;\n if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)\n && (form != POINT_CONVERSION_UNCOMPRESSED)\n && (form != POINT_CONVERSION_HYBRID)) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n return 0;\n }\n if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n return 0;\n }\n if (form == 0) {\n if (len != 1) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n return 0;\n }\n return EC_POINT_set_to_infinity(group, point);\n }\n field_len = (EC_GROUP_get_degree(group) + 7) / 8;\n enc_len =\n (form ==\n POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;\n if (len != enc_len) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n return 0;\n }\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n BN_CTX_start(ctx);\n x = BN_CTX_get(ctx);\n y = BN_CTX_get(ctx);\n yxi = BN_CTX_get(ctx);\n if (yxi == NULL)\n goto err;\n if (!BN_bin2bn(buf + 1, field_len, x))\n goto err;\n if (BN_ucmp(x, group->field) >= 0) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n goto err;\n }\n if (form == POINT_CONVERSION_COMPRESSED) {\n if (!EC_POINT_set_compressed_coordinates_GF2m\n (group, point, x, y_bit, ctx))\n goto err;\n } else {\n if (!BN_bin2bn(buf + 1 + field_len, field_len, y))\n goto err;\n if (BN_ucmp(y, group->field) >= 0) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n goto err;\n }\n if (form == POINT_CONVERSION_HYBRID) {\n if (!group->meth->field_div(group, yxi, y, x, ctx))\n goto err;\n if (y_bit != BN_is_odd(yxi)) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n goto err;\n }\n }\n if (!EC_POINT_set_affine_coordinates_GF2m(group, point, x, y, ctx))\n goto err;\n }\n if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_POINT_IS_NOT_ON_CURVE);\n goto err;\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'BIGNUM *BN_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 EC_POINT_set_compressed_coordinates_GF2m(const EC_GROUP *group,\n EC_POINT *point, const BIGNUM *x,\n int y_bit, BN_CTX *ctx)\n{\n if (group->meth->point_set_compressed_coordinates == 0\n && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {\n ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GF2M,\n ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n if (group->meth != point->meth) {\n ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GF2M,\n EC_R_INCOMPATIBLE_OBJECTS);\n return 0;\n }\n if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {\n if (group->meth->field_type == NID_X9_62_prime_field)\n return ec_GFp_simple_set_compressed_coordinates(group, point, x,\n y_bit, ctx);\n else\n return ec_GF2m_simple_set_compressed_coordinates(group, point, x,\n y_bit, ctx);\n }\n return group->meth->point_set_compressed_coordinates(group, point, x,\n y_bit, ctx);\n}', 'int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group,\n EC_POINT *point,\n const BIGNUM *x_, int y_bit,\n BN_CTX *ctx)\n{\n BN_CTX *new_ctx = NULL;\n BIGNUM *tmp1, *tmp2, *x, *y;\n int ret = 0;\n ERR_clear_error();\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n y_bit = (y_bit != 0);\n BN_CTX_start(ctx);\n tmp1 = BN_CTX_get(ctx);\n tmp2 = BN_CTX_get(ctx);\n x = BN_CTX_get(ctx);\n y = BN_CTX_get(ctx);\n if (y == NULL)\n goto err;\n if (!BN_nnmod(x, x_, group->field, ctx))\n goto err;\n if (group->meth->field_decode == 0) {\n if (!group->meth->field_sqr(group, tmp2, x_, ctx))\n goto err;\n if (!group->meth->field_mul(group, tmp1, tmp2, x_, ctx))\n goto err;\n } else {\n if (!BN_mod_sqr(tmp2, x_, group->field, ctx))\n goto err;\n if (!BN_mod_mul(tmp1, tmp2, x_, group->field, ctx))\n goto err;\n }\n if (group->a_is_minus3) {\n if (!BN_mod_lshift1_quick(tmp2, x, group->field))\n goto err;\n if (!BN_mod_add_quick(tmp2, tmp2, x, group->field))\n goto err;\n if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n } else {\n if (group->meth->field_decode) {\n if (!group->meth->field_decode(group, tmp2, group->a, ctx))\n goto err;\n if (!BN_mod_mul(tmp2, tmp2, x, group->field, ctx))\n goto err;\n } else {\n if (!group->meth->field_mul(group, tmp2, group->a, x, ctx))\n goto err;\n }\n if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n }\n if (group->meth->field_decode) {\n if (!group->meth->field_decode(group, tmp2, group->b, ctx))\n goto err;\n if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n } else {\n if (!BN_mod_add_quick(tmp1, tmp1, group->b, group->field))\n goto err;\n }\n if (!BN_mod_sqrt(y, tmp1, group->field, ctx)) {\n unsigned long err = ERR_peek_last_error();\n if (ERR_GET_LIB(err) == ERR_LIB_BN\n && ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) {\n ERR_clear_error();\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSED_POINT);\n } else\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n ERR_R_BN_LIB);\n goto err;\n }\n if (y_bit != BN_is_odd(y)) {\n if (BN_is_zero(y)) {\n int kron;\n kron = BN_kronecker(x, group->field, ctx);\n if (kron == -2)\n goto err;\n if (kron == 1)\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSION_BIT);\n else\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSED_POINT);\n goto err;\n }\n if (!BN_usub(y, group->field, y))\n goto err;\n }\n if (y_bit != BN_is_odd(y)) {\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx))\n goto err;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return (1);\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if(((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx)\n{\n if (!BN_sqr(r, a, ctx))\n return 0;\n return BN_mod(r, r, m, ctx);\n}', 'int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int max, al;\n int ret = 0;\n BIGNUM *tmp, *rr;\n bn_check_top(a);\n al = a->top;\n if (al <= 0) {\n r->top = 0;\n r->neg = 0;\n return 1;\n }\n BN_CTX_start(ctx);\n rr = (a != r) ? r : BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (!rr || !tmp)\n goto err;\n max = 2 * al;\n if (bn_wexpand(rr, max) == NULL)\n goto err;\n if (al == 4) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[8];\n bn_sqr_normal(rr->d, a->d, 4, t);\n#else\n bn_sqr_comba4(rr->d, a->d);\n#endif\n } else if (al == 8) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[16];\n bn_sqr_normal(rr->d, a->d, 8, t);\n#else\n bn_sqr_comba8(rr->d, a->d);\n#endif\n } else {\n#if defined(BN_RECURSION)\n if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {\n BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];\n bn_sqr_normal(rr->d, a->d, al, t);\n } else {\n int j, k;\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n if (al == j) {\n if (bn_wexpand(tmp, k * 2) == NULL)\n goto err;\n bn_sqr_recursive(rr->d, a->d, al, tmp->d);\n } else {\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n }\n }\n#else\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n#endif\n }\n rr->neg = 0;\n if (a->d[al - 1] == (a->d[al - 1] & BN_MASK2l))\n rr->top = max - 1;\n else\n rr->top = max;\n if (rr != r)\n BN_copy(r, rr);\n ret = 1;\n err:\n bn_check_top(rr);\n bn_check_top(tmp);\n BN_CTX_end(ctx);\n return (ret);\n}', 'void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)\n{\n int i, j, max;\n const BN_ULONG *ap;\n BN_ULONG *rp;\n max = n * 2;\n ap = a;\n rp = r;\n rp[0] = rp[max - 1] = 0;\n rp++;\n j = n;\n if (--j > 0) {\n ap++;\n rp[j] = bn_mul_words(rp, ap, j, ap[-1]);\n rp += 2;\n }\n for (i = n - 2; i > 0; i--) {\n j--;\n ap++;\n rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);\n rp += 2;\n }\n bn_add_words(r, r, r, max);\n bn_sqr_words(tmp, a, n);\n bn_add_words(r, r, tmp, max);\n}']
2,347
0
https://github.com/openssl/openssl/blob/9b02dc97e4963969da69675a871dbe80e6d31cda/crypto/bn/bn_mul.c/#L647
void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb) { BN_ULONG *rr; if (na < nb) { int itmp; BN_ULONG *ltmp; itmp = na; na = nb; nb = itmp; ltmp = a; a = b; b = ltmp; } rr = &(r[na]); if (nb <= 0) { (void)bn_mul_words(r, a, na, 0); return; } else rr[0] = bn_mul_words(r, a, na, b[0]); for (;;) { if (--nb <= 0) return; rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]); if (--nb <= 0) return; rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]); if (--nb <= 0) return; rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]); if (--nb <= 0) return; rr[4] = bn_mul_add_words(&(r[4]), a, na, b[4]); rr += 4; r += 4; b += 4; } }
['int RSA_X931_generate_key_ex(RSA *rsa, int bits, const BIGNUM *e,\n BN_GENCB *cb)\n{\n int ok = 0;\n BIGNUM *Xp = NULL, *Xq = NULL;\n BN_CTX *ctx = NULL;\n ctx = BN_CTX_new();\n if (ctx == NULL)\n goto error;\n BN_CTX_start(ctx);\n Xp = BN_CTX_get(ctx);\n Xq = BN_CTX_get(ctx);\n if (Xq == NULL)\n goto error;\n if (!BN_X931_generate_Xpq(Xp, Xq, bits, ctx))\n goto error;\n rsa->p = BN_new();\n rsa->q = BN_new();\n if (rsa->p == NULL || rsa->q == NULL)\n goto error;\n if (!BN_X931_generate_prime_ex(rsa->p, NULL, NULL, NULL, NULL, Xp,\n e, ctx, cb))\n goto error;\n if (!BN_X931_generate_prime_ex(rsa->q, NULL, NULL, NULL, NULL, Xq,\n e, ctx, cb))\n goto error;\n if (!RSA_X931_derive_ex(rsa, NULL, NULL, NULL, NULL,\n NULL, NULL, NULL, NULL, NULL, NULL, e, cb))\n goto error;\n ok = 1;\n error:\n if (ctx)\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n if (ok)\n return 1;\n return 0;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return 1;\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'int BN_X931_generate_Xpq(BIGNUM *Xp, BIGNUM *Xq, int nbits, BN_CTX *ctx)\n{\n BIGNUM *t;\n int i;\n if ((nbits < 1024) || (nbits & 0xff))\n return 0;\n nbits >>= 1;\n if (!BN_priv_rand(Xp, nbits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ANY))\n goto err;\n BN_CTX_start(ctx);\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n for (i = 0; i < 1000; i++) {\n if (!BN_priv_rand(Xq, nbits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ANY))\n goto err;\n BN_sub(t, Xp, Xq);\n if (BN_num_bits(t) > (nbits - 100))\n break;\n }\n BN_CTX_end(ctx);\n if (i < 1000)\n return 1;\n return 0;\n err:\n BN_CTX_end(ctx);\n return 0;\n}', 'int BN_priv_rand(BIGNUM *rnd, int bits, int top, int bottom)\n{\n return bnrand(PRIVATE, rnd, bits, top, bottom);\n}', 'static int bnrand(BNRAND_FLAG flag, BIGNUM *rnd, int bits, int top, int bottom)\n{\n unsigned char *buf = NULL;\n int b, ret = 0, bit, bytes, mask;\n if (bits == 0) {\n if (top != BN_RAND_TOP_ANY || bottom != BN_RAND_BOTTOM_ANY)\n goto toosmall;\n BN_zero(rnd);\n return 1;\n }\n if (bits < 0 || (bits == 1 && top > 0))\n goto toosmall;\n bytes = (bits + 7) / 8;\n bit = (bits - 1) % 8;\n mask = 0xff << (bit + 1);\n buf = OPENSSL_malloc(bytes);\n if (buf == NULL) {\n BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n b = flag == NORMAL ? RAND_bytes(buf, bytes) : RAND_priv_bytes(buf, bytes);\n if (b <= 0)\n goto err;\n if (flag == TESTING) {\n int i;\n unsigned char c;\n for (i = 0; i < bytes; i++) {\n if (RAND_bytes(&c, 1) <= 0)\n goto err;\n if (c >= 128 && i > 0)\n buf[i] = buf[i - 1];\n else if (c < 42)\n buf[i] = 0;\n else if (c < 84)\n buf[i] = 255;\n }\n }\n if (top >= 0) {\n if (top) {\n if (bit == 0) {\n buf[0] = 1;\n buf[1] |= 0x80;\n } else {\n buf[0] |= (3 << (bit - 1));\n }\n } else {\n buf[0] |= (1 << bit);\n }\n }\n buf[0] &= ~mask;\n if (bottom)\n buf[bytes - 1] |= 1;\n if (!BN_bin2bn(buf, bytes, rnd))\n goto err;\n ret = 1;\n err:\n OPENSSL_clear_free(buf, bytes);\n bn_check_top(rnd);\n return ret;\ntoosmall:\n BNerr(BN_F_BNRAND, BN_R_BITS_TOO_SMALL);\n return 0;\n}', '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}', 'int BN_X931_generate_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2,\n BIGNUM *Xp1, BIGNUM *Xp2,\n const BIGNUM *Xp,\n const BIGNUM *e, BN_CTX *ctx, BN_GENCB *cb)\n{\n int ret = 0;\n BN_CTX_start(ctx);\n if (Xp1 == NULL)\n Xp1 = BN_CTX_get(ctx);\n if (Xp2 == NULL)\n Xp2 = BN_CTX_get(ctx);\n if (Xp1 == NULL || Xp2 == NULL)\n goto error;\n if (!BN_priv_rand(Xp1, 101, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))\n goto error;\n if (!BN_priv_rand(Xp2, 101, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))\n goto error;\n if (!BN_X931_derive_prime_ex(p, p1, p2, Xp, Xp1, Xp2, e, ctx, cb))\n goto error;\n ret = 1;\n error:\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_X931_derive_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2,\n const BIGNUM *Xp, const BIGNUM *Xp1,\n const BIGNUM *Xp2, const BIGNUM *e, BN_CTX *ctx,\n BN_GENCB *cb)\n{\n int ret = 0;\n BIGNUM *t, *p1p2, *pm1;\n if (!BN_is_odd(e))\n return 0;\n BN_CTX_start(ctx);\n if (p1 == NULL)\n p1 = BN_CTX_get(ctx);\n if (p2 == NULL)\n p2 = BN_CTX_get(ctx);\n t = BN_CTX_get(ctx);\n p1p2 = BN_CTX_get(ctx);\n pm1 = BN_CTX_get(ctx);\n if (pm1 == NULL)\n goto err;\n if (!bn_x931_derive_pi(p1, Xp1, ctx, cb))\n goto err;\n if (!bn_x931_derive_pi(p2, Xp2, ctx, cb))\n goto err;\n if (!BN_mul(p1p2, p1, p2, ctx))\n goto err;\n if (!BN_mod_inverse(p, p2, p1, ctx))\n goto err;\n if (!BN_mul(p, p, p2, ctx))\n goto err;\n if (!BN_mod_inverse(t, p1, p2, ctx))\n goto err;\n if (!BN_mul(t, t, p1, ctx))\n goto err;\n if (!BN_sub(p, p, t))\n goto err;\n if (p->neg && !BN_add(p, p, p1p2))\n goto err;\n if (!BN_mod_sub(p, p, Xp, p1p2, ctx))\n goto err;\n if (!BN_add(p, p, Xp))\n goto err;\n for (;;) {\n int i = 1;\n BN_GENCB_call(cb, 0, i++);\n if (!BN_copy(pm1, p))\n goto err;\n if (!BN_sub_word(pm1, 1))\n goto err;\n if (!BN_gcd(t, pm1, e, ctx))\n goto err;\n if (BN_is_one(t)) {\n int r = BN_is_prime_fasttest_ex(p, 50, ctx, 1, cb);\n if (r < 0)\n goto err;\n if (r)\n break;\n }\n if (!BN_add(p, p, p1p2))\n goto err;\n }\n BN_GENCB_call(cb, 3, 0);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'BIGNUM *BN_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n{\n BIGNUM *rv;\n int noinv;\n rv = int_bn_mod_inverse(in, a, n, ctx, &noinv);\n if (noinv)\n BNerr(BN_F_BN_MOD_INVERSE, BN_R_NO_INVERSE);\n return rv;\n}', 'BIGNUM *int_bn_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,\n int *pnoinv)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM *ret = NULL;\n int sign;\n if (pnoinv)\n *pnoinv = 0;\n if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {\n return BN_mod_inverse_no_branch(in, a, n, ctx);\n }\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n if (!BN_nnmod(B, B, A, ctx))\n goto err;\n }\n sign = -1;\n if (BN_is_odd(n) && (BN_num_bits(n) <= 2048)) {\n int shift;\n while (!BN_is_zero(B)) {\n shift = 0;\n while (!BN_is_bit_set(B, shift)) {\n shift++;\n if (BN_is_odd(X)) {\n if (!BN_uadd(X, X, n))\n goto err;\n }\n if (!BN_rshift1(X, X))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(B, B, shift))\n goto err;\n }\n shift = 0;\n while (!BN_is_bit_set(A, shift)) {\n shift++;\n if (BN_is_odd(Y)) {\n if (!BN_uadd(Y, Y, n))\n goto err;\n }\n if (!BN_rshift1(Y, Y))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(A, A, shift))\n goto err;\n }\n if (BN_ucmp(B, A) >= 0) {\n if (!BN_uadd(X, X, Y))\n goto err;\n if (!BN_usub(B, B, A))\n goto err;\n } else {\n if (!BN_uadd(Y, Y, X))\n goto err;\n if (!BN_usub(A, A, B))\n goto err;\n }\n }\n } else {\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n if (BN_num_bits(A) == BN_num_bits(B)) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else if (BN_num_bits(A) == BN_num_bits(B) + 1) {\n if (!BN_lshift1(T, B))\n goto err;\n if (BN_ucmp(A, T) < 0) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else {\n if (!BN_sub(M, A, T))\n goto err;\n if (!BN_add(D, T, B))\n goto err;\n if (BN_ucmp(A, D) < 0) {\n if (!BN_set_word(D, 2))\n goto err;\n } else {\n if (!BN_set_word(D, 3))\n goto err;\n if (!BN_sub(M, M, B))\n goto err;\n }\n }\n } else {\n if (!BN_div(D, M, A, B, ctx))\n goto err;\n }\n tmp = A;\n A = B;\n B = M;\n if (BN_is_one(D)) {\n if (!BN_add(tmp, X, Y))\n goto err;\n } else {\n if (BN_is_word(D, 2)) {\n if (!BN_lshift1(tmp, X))\n goto err;\n } else if (BN_is_word(D, 4)) {\n if (!BN_lshift(tmp, X, 2))\n goto err;\n } else if (D->top == 1) {\n if (!BN_copy(tmp, X))\n goto err;\n if (!BN_mul_word(tmp, D->d[0]))\n goto err;\n } else {\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n }\n if (!BN_add(tmp, tmp, Y))\n goto err;\n }\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n if (pnoinv)\n *pnoinv = 1;\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return ret;\n}', 'int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = 0;\n int top, al, bl;\n BIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n int i;\n#endif\n#ifdef BN_RECURSION\n BIGNUM *t = NULL;\n int j = 0, k;\n#endif\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(r);\n al = a->top;\n bl = b->top;\n if ((al == 0) || (bl == 0)) {\n BN_zero(r);\n return 1;\n }\n top = al + bl;\n BN_CTX_start(ctx);\n if ((r == a) || (r == b)) {\n if ((rr = BN_CTX_get(ctx)) == NULL)\n goto err;\n } else\n rr = r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n i = al - bl;\n#endif\n#ifdef BN_MUL_COMBA\n if (i == 0) {\n# if 0\n if (al == 4) {\n if (bn_wexpand(rr, 8) == NULL)\n goto err;\n rr->top = 8;\n bn_mul_comba4(rr->d, a->d, b->d);\n goto end;\n }\n# endif\n if (al == 8) {\n if (bn_wexpand(rr, 16) == NULL)\n goto err;\n rr->top = 16;\n bn_mul_comba8(rr->d, a->d, b->d);\n goto end;\n }\n }\n#endif\n#ifdef BN_RECURSION\n if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {\n if (i >= -1 && i <= 1) {\n if (i >= 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n }\n if (i == -1) {\n j = BN_num_bits_word((BN_ULONG)bl);\n }\n j = 1 << (j - 1);\n assert(j <= al || j <= bl);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (al > j || bl > j) {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d,\n j, al - j, bl - j, t->d);\n } else {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);\n }\n rr->top = top;\n goto end;\n }\n }\n#endif\n if (bn_wexpand(rr, top) == NULL)\n goto err;\n rr->top = top;\n bn_mul_normal(rr->d, a->d, al, b->d, bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n end:\n#endif\n rr->neg = a->neg ^ b->neg;\n bn_correct_top(rr);\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(r);\n BN_CTX_end(ctx);\n return ret;\n}', 'void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n,\n int tna, int tnb, BN_ULONG *t)\n{\n int i, j, n2 = n * 2;\n int c1, c2, neg;\n BN_ULONG ln, lo, *p;\n if (n < 8) {\n bn_mul_normal(r, a, n + tna, b, n + tnb);\n return;\n }\n c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);\n c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);\n neg = 0;\n switch (c1 * 3 + c2) {\n case -4:\n bn_sub_part_words(t, &(a[n]), a, tna, tna - n);\n bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb);\n break;\n case -3:\n case -2:\n bn_sub_part_words(t, &(a[n]), a, tna, tna - n);\n bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);\n neg = 1;\n break;\n case -1:\n case 0:\n case 1:\n case 2:\n bn_sub_part_words(t, a, &(a[n]), tna, n - tna);\n bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb);\n neg = 1;\n break;\n case 3:\n case 4:\n bn_sub_part_words(t, a, &(a[n]), tna, n - tna);\n bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);\n break;\n }\n# if 0\n if (n == 4) {\n bn_mul_comba4(&(t[n2]), t, &(t[n]));\n bn_mul_comba4(r, a, b);\n bn_mul_normal(&(r[n2]), &(a[n]), tn, &(b[n]), tn);\n memset(&r[n2 + tn * 2], 0, sizeof(*r) * (n2 - tn * 2));\n } else\n# endif\n if (n == 8) {\n bn_mul_comba8(&(t[n2]), t, &(t[n]));\n bn_mul_comba8(r, a, b);\n bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);\n memset(&r[n2 + tna + tnb], 0, sizeof(*r) * (n2 - tna - tnb));\n } else {\n p = &(t[n2 * 2]);\n bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);\n bn_mul_recursive(r, a, b, n, 0, 0, p);\n i = n / 2;\n if (tna > tnb)\n j = tna - i;\n else\n j = tnb - i;\n if (j == 0) {\n bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n memset(&r[n2 + i * 2], 0, sizeof(*r) * (n2 - i * 2));\n } else if (j > 0) {\n bn_mul_part_recursive(&(r[n2]), &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n memset(&(r[n2 + tna + tnb]), 0,\n sizeof(BN_ULONG) * (n2 - tna - tnb));\n } else {\n memset(&r[n2], 0, sizeof(*r) * n2);\n if (tna < BN_MUL_RECURSIVE_SIZE_NORMAL\n && tnb < BN_MUL_RECURSIVE_SIZE_NORMAL) {\n bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);\n } else {\n for (;;) {\n i /= 2;\n if (i < tna || i < tnb) {\n bn_mul_part_recursive(&(r[n2]),\n &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n break;\n } else if (i == tna || i == tnb) {\n bn_mul_recursive(&(r[n2]),\n &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n break;\n }\n }\n }\n }\n }\n c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));\n if (neg) {\n c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));\n } else {\n c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));\n }\n c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));\n if (c1) {\n p = &(r[n + n2]);\n lo = *p;\n ln = (lo + c1) & BN_MASK2;\n *p = ln;\n if (ln < (BN_ULONG)c1) {\n do {\n p++;\n lo = *p;\n ln = (lo + 1) & BN_MASK2;\n *p = ln;\n } while (ln == 0);\n }\n }\n}', 'void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb)\n{\n BN_ULONG *rr;\n if (na < nb) {\n int itmp;\n BN_ULONG *ltmp;\n itmp = na;\n na = nb;\n nb = itmp;\n ltmp = a;\n a = b;\n b = ltmp;\n }\n rr = &(r[na]);\n if (nb <= 0) {\n (void)bn_mul_words(r, a, na, 0);\n return;\n } else\n rr[0] = bn_mul_words(r, a, na, b[0]);\n for (;;) {\n if (--nb <= 0)\n return;\n rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]);\n if (--nb <= 0)\n return;\n rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]);\n if (--nb <= 0)\n return;\n rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]);\n if (--nb <= 0)\n return;\n rr[4] = bn_mul_add_words(&(r[4]), a, na, b[4]);\n rr += 4;\n r += 4;\n b += 4;\n }\n}']
2,348
0
https://github.com/openssl/openssl/blob/1fac96e4d6484a517f2ebe99b72016726391723c/crypto/conf/conf.c/#L494
static int str_copy(LHASH *conf, char *section, char **pto, char *from) { int q,r,rr=0,to=0,len=0; char *s,*e,*rp,*p,*rrp,*np,*cp,v; BUF_MEM *buf; if ((buf=BUF_MEM_new()) == NULL) return(0); len=strlen(from)+1; if (!BUF_MEM_grow(buf,len)) goto err; for (;;) { if (IS_QUOTE(*from)) { q= *from; from++; while ((*from != '\0') && (*from != q)) { if (*from == '\\') { from++; if (*from == '\0') break; } buf->data[to++]= *(from++); } } else if (*from == '\\') { from++; v= *(from++); if (v == '\0') break; else if (v == 'r') v='\r'; else if (v == 'n') v='\n'; else if (v == 'b') v='\b'; else if (v == 't') v='\t'; buf->data[to++]= v; } else if (*from == '\0') break; else if (*from == '$') { rrp=NULL; s= &(from[1]); if (*s == '{') q='}'; else if (*s == '(') q=')'; else q=0; if (q) s++; cp=section; e=np=s; while (IS_ALPHA_NUMERIC(*e)) e++; if ((e[0] == ':') && (e[1] == ':')) { cp=np; rrp=e; rr= *e; *rrp='\0'; e+=2; np=e; while (IS_ALPHA_NUMERIC(*e)) e++; } r= *e; *e='\0'; rp=e; if (q) { if (r != q) { CONFerr(CONF_F_STR_COPY,CONF_R_NO_CLOSE_BRACE); goto err; } e++; } p=CONF_get_string(conf,cp,np); if (rrp != NULL) *rrp=rr; *rp=r; if (p == NULL) { CONFerr(CONF_F_STR_COPY,CONF_R_VARIABLE_HAS_NO_VALUE); goto err; } BUF_MEM_grow(buf,(strlen(p)+len-(e-from))); while (*p) buf->data[to++]= *(p++); from=e; } else buf->data[to++]= *(from++); } buf->data[to]='\0'; if (*pto != NULL) Free(*pto); *pto=buf->data; Free(buf); return(1); err: if (buf != NULL) BUF_MEM_free(buf); return(0); }
["static int str_copy(LHASH *conf, char *section, char **pto, char *from)\n\t{\n\tint q,r,rr=0,to=0,len=0;\n\tchar *s,*e,*rp,*p,*rrp,*np,*cp,v;\n\tBUF_MEM *buf;\n\tif ((buf=BUF_MEM_new()) == NULL) return(0);\n\tlen=strlen(from)+1;\n\tif (!BUF_MEM_grow(buf,len)) goto err;\n\tfor (;;)\n\t\t{\n\t\tif (IS_QUOTE(*from))\n\t\t\t{\n\t\t\tq= *from;\n\t\t\tfrom++;\n\t\t\twhile ((*from != '\\0') && (*from != q))\n\t\t\t\t{\n\t\t\t\tif (*from == '\\\\')\n\t\t\t\t\t{\n\t\t\t\t\tfrom++;\n\t\t\t\t\tif (*from == '\\0') break;\n\t\t\t\t\t}\n\t\t\t\tbuf->data[to++]= *(from++);\n\t\t\t\t}\n\t\t\t}\n\t\telse if (*from == '\\\\')\n\t\t\t{\n\t\t\tfrom++;\n\t\t\tv= *(from++);\n\t\t\tif (v == '\\0') break;\n\t\t\telse if (v == 'r') v='\\r';\n\t\t\telse if (v == 'n') v='\\n';\n\t\t\telse if (v == 'b') v='\\b';\n\t\t\telse if (v == 't') v='\\t';\n\t\t\tbuf->data[to++]= v;\n\t\t\t}\n\t\telse if (*from == '\\0')\n\t\t\tbreak;\n\t\telse if (*from == '$')\n\t\t\t{\n\t\t\trrp=NULL;\n\t\t\ts= &(from[1]);\n\t\t\tif (*s == '{')\n\t\t\t\tq='}';\n\t\t\telse if (*s == '(')\n\t\t\t\tq=')';\n\t\t\telse q=0;\n\t\t\tif (q) s++;\n\t\t\tcp=section;\n\t\t\te=np=s;\n\t\t\twhile (IS_ALPHA_NUMERIC(*e))\n\t\t\t\te++;\n\t\t\tif ((e[0] == ':') && (e[1] == ':'))\n\t\t\t\t{\n\t\t\t\tcp=np;\n\t\t\t\trrp=e;\n\t\t\t\trr= *e;\n\t\t\t\t*rrp='\\0';\n\t\t\t\te+=2;\n\t\t\t\tnp=e;\n\t\t\t\twhile (IS_ALPHA_NUMERIC(*e))\n\t\t\t\t\te++;\n\t\t\t\t}\n\t\t\tr= *e;\n\t\t\t*e='\\0';\n\t\t\trp=e;\n\t\t\tif (q)\n\t\t\t\t{\n\t\t\t\tif (r != q)\n\t\t\t\t\t{\n\t\t\t\t\tCONFerr(CONF_F_STR_COPY,CONF_R_NO_CLOSE_BRACE);\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t\te++;\n\t\t\t\t}\n\t\t\tp=CONF_get_string(conf,cp,np);\n\t\t\tif (rrp != NULL) *rrp=rr;\n\t\t\t*rp=r;\n\t\t\tif (p == NULL)\n\t\t\t\t{\n\t\t\t\tCONFerr(CONF_F_STR_COPY,CONF_R_VARIABLE_HAS_NO_VALUE);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tBUF_MEM_grow(buf,(strlen(p)+len-(e-from)));\n\t\t\twhile (*p)\n\t\t\t\tbuf->data[to++]= *(p++);\n\t\t\tfrom=e;\n\t\t\t}\n\t\telse\n\t\t\tbuf->data[to++]= *(from++);\n\t\t}\n\tbuf->data[to]='\\0';\n\tif (*pto != NULL) Free(*pto);\n\t*pto=buf->data;\n\tFree(buf);\n\treturn(1);\nerr:\n\tif (buf != NULL) BUF_MEM_free(buf);\n\treturn(0);\n\t}", 'BUF_MEM *BUF_MEM_new(void)\n\t{\n\tBUF_MEM *ret;\n\tret=Malloc(sizeof(BUF_MEM));\n\tif (ret == NULL)\n\t\t{\n\t\tBUFerr(BUF_F_BUF_MEM_NEW,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tret->length=0;\n\tret->max=0;\n\tret->data=NULL;\n\treturn(ret);\n\t}', 'int BUF_MEM_grow(BUF_MEM *str, int len)\n\t{\n\tchar *ret;\n\tunsigned int n;\n\tif (str->length >= len)\n\t\t{\n\t\tstr->length=len;\n\t\treturn(len);\n\t\t}\n\tif (str->max >= len)\n\t\t{\n\t\tmemset(&str->data[str->length],0,len-str->length);\n\t\tstr->length=len;\n\t\treturn(len);\n\t\t}\n\tn=(len+3)/3*4;\n\tif (str->data == NULL)\n\t\tret=Malloc(n);\n\telse\n\t\tret=Realloc(str->data,n);\n\tif (ret == NULL)\n\t\t{\n\t\tBUFerr(BUF_F_BUF_MEM_GROW,ERR_R_MALLOC_FAILURE);\n\t\tlen=0;\n\t\t}\n\telse\n\t\t{\n\t\tstr->data=ret;\n\t\tstr->length=len;\n\t\tstr->max=n;\n\t\t}\n\treturn(len);\n\t}']
2,349
0
https://github.com/openssl/openssl/blob/6e9fa57c6ddde7df49983251373a05cd663aac22/crypto/bn/bn_lib.c/#L342
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *A, *a = NULL; const BN_ULONG *B; int i; bn_check_top(b); if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return (NULL); } if (BN_get_flags(b,BN_FLG_SECURE)) a = A = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = A = OPENSSL_zalloc(words * sizeof(*a)); if (A == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return (NULL); } #if 1 B = b->d; if (B != NULL) { for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) { BN_ULONG a0, a1, a2, a3; a0 = B[0]; a1 = B[1]; a2 = B[2]; a3 = B[3]; A[0] = a0; A[1] = a1; A[2] = a2; A[3] = a3; } switch (b->top & 3) { case 3: A[2] = B[2]; case 2: A[1] = B[1]; case 1: A[0] = B[0]; case 0: ; } } #else memset(A, 0, sizeof(*A) * words); memcpy(A, b->d, sizeof(b->d[0]) * b->top); #endif return (a); }
['DSA *get_dsa2048()\n{\n DSA *dsa;\n BIGNUM *priv_key, *pub_key, *p, *q, *g;\n if ((dsa = DSA_new()) == NULL)\n return (NULL);\n priv_key = BN_bin2bn(dsa2048_priv, sizeof(dsa2048_priv), NULL);\n pub_key = BN_bin2bn(dsa2048_pub, sizeof(dsa2048_pub), NULL);\n p = BN_bin2bn(dsa2048_p, sizeof(dsa2048_p), NULL);\n q = BN_bin2bn(dsa2048_q, sizeof(dsa2048_q), NULL);\n g = BN_bin2bn(dsa2048_g, sizeof(dsa2048_g), NULL);\n if ((priv_key == NULL) || (pub_key == NULL) || (p == NULL) || (q == NULL)\n || (g == NULL)) {\n goto err;\n }\n if (!DSA_set0_pqg(dsa, p, q, g))\n goto err;\n p = q = g = NULL;\n if (!DSA_set0_key(dsa, pub_key, priv_key))\n goto err;\n return dsa;\n err:\n DSA_free(dsa);\n BN_free(priv_key);\n BN_free(pub_key);\n BN_free(p);\n BN_free(q);\n BN_free(g);\n return NULL;\n}', 'BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)\n{\n unsigned int i, m;\n unsigned int n;\n BN_ULONG l;\n BIGNUM *bn = NULL;\n if (ret == NULL)\n ret = bn = BN_new();\n if (ret == NULL)\n return (NULL);\n bn_check_top(ret);\n for ( ; len > 0 && *s == 0; s++, len--)\n continue;\n n = len;\n if (n == 0) {\n ret->top = 0;\n return (ret);\n }\n i = ((n - 1) / BN_BYTES) + 1;\n m = ((n - 1) % (BN_BYTES));\n if (bn_wexpand(ret, (int)i) == NULL) {\n BN_free(bn);\n return NULL;\n }\n ret->top = i;\n ret->neg = 0;\n l = 0;\n while (n--) {\n l = (l << 8L) | *(s++);\n if (m-- == 0) {\n ret->d[--i] = l;\n l = 0;\n m = BN_BYTES - 1;\n }\n }\n bn_correct_top(ret);\n return (ret);\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *A, *a = NULL;\n const BN_ULONG *B;\n int i;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return (NULL);\n }\n if (BN_get_flags(b,BN_FLG_SECURE))\n a = A = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = A = OPENSSL_zalloc(words * sizeof(*a));\n if (A == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n#if 1\n B = b->d;\n if (B != NULL) {\n for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {\n BN_ULONG a0, a1, a2, a3;\n a0 = B[0];\n a1 = B[1];\n a2 = B[2];\n a3 = B[3];\n A[0] = a0;\n A[1] = a1;\n A[2] = a2;\n A[3] = a3;\n }\n switch (b->top & 3) {\n case 3:\n A[2] = B[2];\n case 2:\n A[1] = B[1];\n case 1:\n A[0] = B[0];\n case 0:\n ;\n }\n }\n#else\n memset(A, 0, sizeof(*A) * words);\n memcpy(A, b->d, sizeof(b->d[0]) * b->top);\n#endif\n return (a);\n}']
2,350
0
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/bn/bn_lib.c/#L688
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); }
['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}']
2,351
0
https://github.com/libav/libav/blob/e53eecd0e7211973a1a9757f559bdd93a1848901/libavcodec/mjpegdec.c/#L1003
int ff_mjpeg_decode_sos(MJpegDecodeContext *s, const uint8_t *mb_bitmask, const AVFrame *reference) { int len, nb_components, i, h, v, predictor, point_transform; int index, id; const int block_size= s->lossless ? 1 : 8; int ilv, prev_shift; len = get_bits(&s->gb, 16); nb_components = get_bits(&s->gb, 8); if (nb_components == 0 || nb_components > MAX_COMPONENTS){ av_log(s->avctx, AV_LOG_ERROR, "decode_sos: nb_components (%d) unsupported\n", nb_components); return -1; } if (len != 6+2*nb_components) { av_log(s->avctx, AV_LOG_ERROR, "decode_sos: invalid len (%d)\n", len); return -1; } for(i=0;i<nb_components;i++) { id = get_bits(&s->gb, 8) - 1; av_log(s->avctx, AV_LOG_DEBUG, "component: %d\n", id); for(index=0;index<s->nb_components;index++) if (id == s->component_id[index]) break; if (index == s->nb_components) { av_log(s->avctx, AV_LOG_ERROR, "decode_sos: index(%d) out of components\n", index); return -1; } if (s->avctx->codec_tag == MKTAG('M', 'T', 'S', 'J') && nb_components == 3 && s->nb_components == 3 && i) index = 3 - i; s->comp_index[i] = index; s->nb_blocks[i] = s->h_count[index] * s->v_count[index]; s->h_scount[i] = s->h_count[index]; s->v_scount[i] = s->v_count[index]; s->dc_index[i] = get_bits(&s->gb, 4); s->ac_index[i] = get_bits(&s->gb, 4); if (s->dc_index[i] < 0 || s->ac_index[i] < 0 || s->dc_index[i] >= 4 || s->ac_index[i] >= 4) goto out_of_range; if (!s->vlcs[0][s->dc_index[i]].table || !s->vlcs[1][s->ac_index[i]].table) goto out_of_range; } predictor= get_bits(&s->gb, 8); ilv= get_bits(&s->gb, 8); prev_shift = get_bits(&s->gb, 4); point_transform= get_bits(&s->gb, 4); for(i=0;i<nb_components;i++) s->last_dc[i] = 1024; if (nb_components > 1) { s->mb_width = (s->width + s->h_max * block_size - 1) / (s->h_max * block_size); s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size); } else if(!s->ls) { h = s->h_max / s->h_scount[0]; v = s->v_max / s->v_scount[0]; s->mb_width = (s->width + h * block_size - 1) / (h * block_size); s->mb_height = (s->height + v * block_size - 1) / (v * block_size); s->nb_blocks[0] = 1; s->h_scount[0] = 1; s->v_scount[0] = 1; } if(s->avctx->debug & FF_DEBUG_PICT_INFO) av_log(s->avctx, AV_LOG_DEBUG, "%s %s p:%d >>:%d ilv:%d bits:%d %s\n", s->lossless ? "lossless" : "sequential DCT", s->rgb ? "RGB" : "", predictor, point_transform, ilv, s->bits, s->pegasus_rct ? "PRCT" : (s->rct ? "RCT" : "")); for (i = s->mjpb_skiptosod; i > 0; i--) skip_bits(&s->gb, 8); if(s->lossless){ if(CONFIG_JPEGLS_DECODER && s->ls){ if(ff_jpegls_decode_picture(s, predictor, point_transform, ilv) < 0) return -1; }else{ if(s->rgb){ if(ljpeg_decode_rgb_scan(s, predictor, point_transform) < 0) return -1; }else{ if(ljpeg_decode_yuv_scan(s, predictor, point_transform) < 0) return -1; } } }else{ if(s->progressive && predictor) { if(mjpeg_decode_scan_progressive_ac(s, predictor, ilv, prev_shift, point_transform, mb_bitmask, reference) < 0) return -1; } else { if(mjpeg_decode_scan(s, nb_components, prev_shift, point_transform, mb_bitmask, reference) < 0) return -1; } } emms_c(); return 0; out_of_range: av_log(s->avctx, AV_LOG_ERROR, "decode_sos: ac/dc index out of range\n"); return -1; }
['int ff_mjpeg_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 MJpegDecodeContext *s = avctx->priv_data;\n const uint8_t *buf_end, *buf_ptr;\n const uint8_t *unescaped_buf_ptr;\n int unescaped_buf_size;\n int start_code;\n AVFrame *picture = data;\n s->got_picture = 0;\n buf_ptr = buf;\n buf_end = buf + buf_size;\n while (buf_ptr < buf_end) {\n start_code = ff_mjpeg_find_marker(s, &buf_ptr, buf_end,\n &unescaped_buf_ptr, &unescaped_buf_size);\n {\n if (start_code < 0) {\n goto the_end;\n } else {\n av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%td\\n", start_code, buf_end - buf_ptr);\n init_get_bits(&s->gb, unescaped_buf_ptr, unescaped_buf_size*8);\n s->start_code = start_code;\n if(s->avctx->debug & FF_DEBUG_STARTCODE){\n av_log(avctx, AV_LOG_DEBUG, "startcode: %X\\n", start_code);\n }\n if (start_code >= 0xd0 && start_code <= 0xd7) {\n av_log(avctx, AV_LOG_DEBUG, "restart marker: %d\\n", start_code&0x0f);\n } else if (start_code >= APP0 && start_code <= APP15) {\n mjpeg_decode_app(s);\n } else if (start_code == COM){\n mjpeg_decode_com(s);\n }\n switch(start_code) {\n case SOI:\n s->restart_interval = 0;\n s->restart_count = 0;\n break;\n case DQT:\n ff_mjpeg_decode_dqt(s);\n break;\n case DHT:\n if(ff_mjpeg_decode_dht(s) < 0){\n av_log(avctx, AV_LOG_ERROR, "huffman table decode error\\n");\n return -1;\n }\n break;\n case SOF0:\n case SOF1:\n s->lossless=0;\n s->ls=0;\n s->progressive=0;\n if (ff_mjpeg_decode_sof(s) < 0)\n return -1;\n break;\n case SOF2:\n s->lossless=0;\n s->ls=0;\n s->progressive=1;\n if (ff_mjpeg_decode_sof(s) < 0)\n return -1;\n break;\n case SOF3:\n s->lossless=1;\n s->ls=0;\n s->progressive=0;\n if (ff_mjpeg_decode_sof(s) < 0)\n return -1;\n break;\n case SOF48:\n s->lossless=1;\n s->ls=1;\n s->progressive=0;\n if (ff_mjpeg_decode_sof(s) < 0)\n return -1;\n break;\n case LSE:\n if (!CONFIG_JPEGLS_DECODER || ff_jpegls_decode_lse(s) < 0)\n return -1;\n break;\n case EOI:\n s->cur_scan = 0;\n if ((s->buggy_avid && !s->interlaced) || s->restart_interval)\n break;\neoi_parser:\n if (!s->got_picture) {\n av_log(avctx, AV_LOG_WARNING, "Found EOI before any SOF, ignoring\\n");\n break;\n }\n if (s->interlaced) {\n s->bottom_field ^= 1;\n if (s->bottom_field == !s->interlace_polarity)\n goto not_the_end;\n }\n *picture = *s->picture_ptr;\n *data_size = sizeof(AVFrame);\n if(!s->lossless){\n picture->quality= FFMAX3(s->qscale[0], s->qscale[1], s->qscale[2]);\n picture->qstride= 0;\n picture->qscale_table= s->qscale_table;\n memset(picture->qscale_table, picture->quality, (s->width+15)/16);\n if(avctx->debug & FF_DEBUG_QP)\n av_log(avctx, AV_LOG_DEBUG, "QP: %d\\n", picture->quality);\n picture->quality*= FF_QP2LAMBDA;\n }\n goto the_end;\n case SOS:\n if (!s->got_picture) {\n av_log(avctx, AV_LOG_WARNING, "Can not process SOS before SOF, skipping\\n");\n break;\n }\n if (ff_mjpeg_decode_sos(s, NULL, NULL) < 0 &&\n (avctx->err_recognition & AV_EF_EXPLODE))\n return AVERROR_INVALIDDATA;\n if ((s->buggy_avid && !s->interlaced) || s->restart_interval)\n goto eoi_parser;\n break;\n case DRI:\n mjpeg_decode_dri(s);\n break;\n case SOF5:\n case SOF6:\n case SOF7:\n case SOF9:\n case SOF10:\n case SOF11:\n case SOF13:\n case SOF14:\n case SOF15:\n case JPG:\n av_log(avctx, AV_LOG_ERROR, "mjpeg: unsupported coding type (%x)\\n", start_code);\n break;\n }\nnot_the_end:\n buf_ptr += (get_bits_count(&s->gb)+7)/8;\n av_log(avctx, AV_LOG_DEBUG, "marker parser used %d bytes (%d bits)\\n",\n (get_bits_count(&s->gb)+7)/8, get_bits_count(&s->gb));\n }\n }\n }\n if (s->got_picture) {\n av_log(avctx, AV_LOG_WARNING, "EOI missing, emulating\\n");\n goto eoi_parser;\n }\n av_log(avctx, AV_LOG_FATAL, "No JPEG data found in image\\n");\n return -1;\nthe_end:\n av_log(avctx, AV_LOG_DEBUG, "mjpeg decode frame unused %td bytes\\n", buf_end - buf_ptr);\n return buf_ptr - buf;\n}', 'int ff_mjpeg_decode_sos(MJpegDecodeContext *s,\n const uint8_t *mb_bitmask, const AVFrame *reference)\n{\n int len, nb_components, i, h, v, predictor, point_transform;\n int index, id;\n const int block_size= s->lossless ? 1 : 8;\n int ilv, prev_shift;\n len = get_bits(&s->gb, 16);\n nb_components = get_bits(&s->gb, 8);\n if (nb_components == 0 || nb_components > MAX_COMPONENTS){\n av_log(s->avctx, AV_LOG_ERROR, "decode_sos: nb_components (%d) unsupported\\n", nb_components);\n return -1;\n }\n if (len != 6+2*nb_components)\n {\n av_log(s->avctx, AV_LOG_ERROR, "decode_sos: invalid len (%d)\\n", len);\n return -1;\n }\n for(i=0;i<nb_components;i++) {\n id = get_bits(&s->gb, 8) - 1;\n av_log(s->avctx, AV_LOG_DEBUG, "component: %d\\n", id);\n for(index=0;index<s->nb_components;index++)\n if (id == s->component_id[index])\n break;\n if (index == s->nb_components)\n {\n av_log(s->avctx, AV_LOG_ERROR, "decode_sos: index(%d) out of components\\n", index);\n return -1;\n }\n if (s->avctx->codec_tag == MKTAG(\'M\', \'T\', \'S\', \'J\')\n && nb_components == 3 && s->nb_components == 3 && i)\n index = 3 - i;\n s->comp_index[i] = index;\n s->nb_blocks[i] = s->h_count[index] * s->v_count[index];\n s->h_scount[i] = s->h_count[index];\n s->v_scount[i] = s->v_count[index];\n s->dc_index[i] = get_bits(&s->gb, 4);\n s->ac_index[i] = get_bits(&s->gb, 4);\n if (s->dc_index[i] < 0 || s->ac_index[i] < 0 ||\n s->dc_index[i] >= 4 || s->ac_index[i] >= 4)\n goto out_of_range;\n if (!s->vlcs[0][s->dc_index[i]].table || !s->vlcs[1][s->ac_index[i]].table)\n goto out_of_range;\n }\n predictor= get_bits(&s->gb, 8);\n ilv= get_bits(&s->gb, 8);\n prev_shift = get_bits(&s->gb, 4);\n point_transform= get_bits(&s->gb, 4);\n for(i=0;i<nb_components;i++)\n s->last_dc[i] = 1024;\n if (nb_components > 1) {\n s->mb_width = (s->width + s->h_max * block_size - 1) / (s->h_max * block_size);\n s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size);\n } else if(!s->ls) {\n h = s->h_max / s->h_scount[0];\n v = s->v_max / s->v_scount[0];\n s->mb_width = (s->width + h * block_size - 1) / (h * block_size);\n s->mb_height = (s->height + v * block_size - 1) / (v * block_size);\n s->nb_blocks[0] = 1;\n s->h_scount[0] = 1;\n s->v_scount[0] = 1;\n }\n if(s->avctx->debug & FF_DEBUG_PICT_INFO)\n av_log(s->avctx, AV_LOG_DEBUG, "%s %s p:%d >>:%d ilv:%d bits:%d %s\\n", s->lossless ? "lossless" : "sequential DCT", s->rgb ? "RGB" : "",\n predictor, point_transform, ilv, s->bits,\n s->pegasus_rct ? "PRCT" : (s->rct ? "RCT" : ""));\n for (i = s->mjpb_skiptosod; i > 0; i--)\n skip_bits(&s->gb, 8);\n if(s->lossless){\n if(CONFIG_JPEGLS_DECODER && s->ls){\n if(ff_jpegls_decode_picture(s, predictor, point_transform, ilv) < 0)\n return -1;\n }else{\n if(s->rgb){\n if(ljpeg_decode_rgb_scan(s, predictor, point_transform) < 0)\n return -1;\n }else{\n if(ljpeg_decode_yuv_scan(s, predictor, point_transform) < 0)\n return -1;\n }\n }\n }else{\n if(s->progressive && predictor) {\n if(mjpeg_decode_scan_progressive_ac(s, predictor, ilv, prev_shift, point_transform,\n mb_bitmask, reference) < 0)\n return -1;\n } else {\n if(mjpeg_decode_scan(s, nb_components, prev_shift, point_transform,\n mb_bitmask, reference) < 0)\n return -1;\n }\n }\n emms_c();\n return 0;\n out_of_range:\n av_log(s->avctx, AV_LOG_ERROR, "decode_sos: ac/dc index out of range\\n");\n return -1;\n}']
2,352
0
https://github.com/openssl/openssl/blob/6bc62a620e715f7580651ca932eab052aa527886/crypto/bn/bn_ctx.c/#L268
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)\n{\n int i, ret = 0;\n BIGNUM *Ri, *R;\n if (BN_is_zero(mod))\n return 0;\n BN_CTX_start(ctx);\n if ((Ri = BN_CTX_get(ctx)) == NULL)\n goto err;\n R = &(mont->RR);\n if (!BN_copy(&(mont->N), mod))\n goto err;\n if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)\n BN_set_flags(&(mont->N), BN_FLG_CONSTTIME);\n mont->N.neg = 0;\n#ifdef MONT_WORD\n {\n BIGNUM tmod;\n BN_ULONG buf[2];\n bn_init(&tmod);\n tmod.d = buf;\n tmod.dmax = 2;\n tmod.neg = 0;\n if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)\n BN_set_flags(&tmod, BN_FLG_CONSTTIME);\n mont->ri = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2;\n# if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)\n BN_zero(R);\n if (!(BN_set_bit(R, 2 * BN_BITS2)))\n goto err;\n tmod.top = 0;\n if ((buf[0] = mod->d[0]))\n tmod.top = 1;\n if ((buf[1] = mod->top > 1 ? mod->d[1] : 0))\n tmod.top = 2;\n if (BN_is_one(&tmod))\n BN_zero(Ri);\n else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, 2 * BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (bn_expand(Ri, (int)sizeof(BN_ULONG) * 2) == NULL)\n goto err;\n Ri->neg = 0;\n Ri->d[0] = BN_MASK2;\n Ri->d[1] = BN_MASK2;\n Ri->top = 2;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;\n# else\n BN_zero(R);\n if (!(BN_set_bit(R, BN_BITS2)))\n goto err;\n buf[0] = mod->d[0];\n buf[1] = 0;\n tmod.top = buf[0] != 0 ? 1 : 0;\n if (BN_is_one(&tmod))\n BN_zero(Ri);\n else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (!BN_set_word(Ri, BN_MASK2))\n goto err;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = 0;\n# endif\n }\n#else\n {\n mont->ri = BN_num_bits(&mont->N);\n BN_zero(R);\n if (!BN_set_bit(R, mont->ri))\n goto err;\n if ((BN_mod_inverse(Ri, R, &mont->N, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, mont->ri))\n goto err;\n if (!BN_sub_word(Ri, 1))\n goto err;\n if (!BN_div(&(mont->Ni), NULL, Ri, &mont->N, ctx))\n goto err;\n }\n#endif\n BN_zero(&(mont->RR));\n if (!BN_set_bit(&(mont->RR), mont->ri * 2))\n goto err;\n if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx))\n goto err;\n for (i = mont->RR.top, ret = mont->N.top; i < ret; i++)\n mont->RR.d[i] = 0;\n mont->RR.top = ret;\n mont->RR.flags |= BN_FLG_FIXED_TOP;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_start()", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG("LEAVE BN_CTX_start()", ctx);\n}', 'int BN_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}']
2,353
0
https://github.com/libav/libav/blob/b9478cfefb408ae1d476555b46893005e2d3bf4c/libavcodec/dvbsubdec.c/#L933
static void dvbsub_parse_clut_segment(AVCodecContext *avctx, const uint8_t *buf, int buf_size) { DVBSubContext *ctx = avctx->priv_data; const uint8_t *buf_end = buf + buf_size; int i, clut_id; DVBSubCLUT *clut; int entry_id, depth , full_range; int y, cr, cb, alpha; int r, g, b, r_add, g_add, b_add; av_dlog(avctx, "DVB clut packet:\n"); for (i=0; i < buf_size; i++) { av_dlog(avctx, "%02x ", buf[i]); if (i % 16 == 15) av_dlog(avctx, "\n"); } if (i % 16) av_dlog(avctx, "\n"); clut_id = *buf++; buf += 1; clut = get_clut(ctx, clut_id); if (!clut) { clut = av_malloc(sizeof(DVBSubCLUT)); memcpy(clut, &default_clut, sizeof(DVBSubCLUT)); clut->id = clut_id; clut->next = ctx->clut_list; ctx->clut_list = clut; } while (buf + 4 < buf_end) { entry_id = *buf++; depth = (*buf) & 0xe0; if (depth == 0) { av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf); return; } full_range = (*buf++) & 1; if (full_range) { y = *buf++; cr = *buf++; cb = *buf++; alpha = *buf++; } else { y = buf[0] & 0xfc; cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4; cb = (buf[1] << 2) & 0xf0; alpha = (buf[1] << 6) & 0xc0; buf += 2; } if (y == 0) alpha = 0xff; YUV_TO_RGB1_CCIR(cb, cr); YUV_TO_RGB2_CCIR(r, g, b, y); av_dlog(avctx, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha); if (depth & 0x80) clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha); if (depth & 0x40) clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha); if (depth & 0x20) clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha); } }
['static void dvbsub_parse_clut_segment(AVCodecContext *avctx,\n const uint8_t *buf, int buf_size)\n{\n DVBSubContext *ctx = avctx->priv_data;\n const uint8_t *buf_end = buf + buf_size;\n int i, clut_id;\n DVBSubCLUT *clut;\n int entry_id, depth , full_range;\n int y, cr, cb, alpha;\n int r, g, b, r_add, g_add, b_add;\n av_dlog(avctx, "DVB clut packet:\\n");\n for (i=0; i < buf_size; i++) {\n av_dlog(avctx, "%02x ", buf[i]);\n if (i % 16 == 15)\n av_dlog(avctx, "\\n");\n }\n if (i % 16)\n av_dlog(avctx, "\\n");\n clut_id = *buf++;\n buf += 1;\n clut = get_clut(ctx, clut_id);\n if (!clut) {\n clut = av_malloc(sizeof(DVBSubCLUT));\n memcpy(clut, &default_clut, sizeof(DVBSubCLUT));\n clut->id = clut_id;\n clut->next = ctx->clut_list;\n ctx->clut_list = clut;\n }\n while (buf + 4 < buf_end) {\n entry_id = *buf++;\n depth = (*buf) & 0xe0;\n if (depth == 0) {\n av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\\n", *buf);\n return;\n }\n full_range = (*buf++) & 1;\n if (full_range) {\n y = *buf++;\n cr = *buf++;\n cb = *buf++;\n alpha = *buf++;\n } else {\n y = buf[0] & 0xfc;\n cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;\n cb = (buf[1] << 2) & 0xf0;\n alpha = (buf[1] << 6) & 0xc0;\n buf += 2;\n }\n if (y == 0)\n alpha = 0xff;\n YUV_TO_RGB1_CCIR(cb, cr);\n YUV_TO_RGB2_CCIR(r, g, b, y);\n av_dlog(avctx, "clut %d := (%d,%d,%d,%d)\\n", entry_id, r, g, b, alpha);\n if (depth & 0x80)\n clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);\n if (depth & 0x40)\n clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);\n if (depth & 0x20)\n clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);\n }\n}', 'static DVBSubCLUT* get_clut(DVBSubContext *ctx, int clut_id)\n{\n DVBSubCLUT *ptr = ctx->clut_list;\n while (ptr && ptr->id != clut_id) {\n ptr = ptr->next;\n }\n return ptr;\n}', 'void *av_malloc(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}']
2,354
0
https://github.com/openssl/openssl/blob/f3ff481f318b10a223d6157bde9645e1797487c5/ssl/packet.c/#L25
int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes) { assert(pkt->subs != NULL && len != 0); if (pkt->subs == NULL || len == 0) return 0; if (pkt->maxsize - pkt->written < len) return 0; if (pkt->buf->length - pkt->written < len) { size_t newlen; if (pkt->buf->length > SIZE_MAX / 2) { newlen = SIZE_MAX; } else { newlen = (pkt->buf->length == 0) ? DEFAULT_BUF_SIZE : pkt->buf->length * 2; } if (BUF_MEM_grow(pkt->buf, newlen) == 0) return 0; } *allocbytes = (unsigned char *)pkt->buf->data + pkt->curr; pkt->written += len; pkt->curr += len; return 1; }
['int ssl_add_clienthello_tlsext(SSL *s, WPACKET *pkt, int *al)\n{\n#ifndef OPENSSL_NO_EC\n int using_ecc = 0;\n if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s)) {\n int i;\n unsigned long alg_k, alg_a;\n STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);\n for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {\n const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);\n alg_k = c->algorithm_mkey;\n alg_a = c->algorithm_auth;\n if ((alg_k & (SSL_kECDHE | SSL_kECDHEPSK))\n || (alg_a & SSL_aECDSA)) {\n using_ecc = 1;\n break;\n }\n }\n }\n#endif\n if (s->renegotiate) {\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate)\n || !WPACKET_sub_memcpy_u16(pkt, s->s3->previous_client_finished,\n s->s3->previous_client_finished_len)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n }\n if (s->client_version == SSL3_VERSION)\n goto done;\n if (s->tlsext_hostname != NULL) {\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name)\n || !WPACKET_start_sub_packet_u16(pkt)\n || !WPACKET_start_sub_packet_u16(pkt)\n || !WPACKET_put_bytes_u8(pkt, TLSEXT_NAMETYPE_host_name)\n || !WPACKET_sub_memcpy_u16(pkt, s->tlsext_hostname,\n strlen(s->tlsext_hostname))\n || !WPACKET_close(pkt)\n || !WPACKET_close(pkt)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n }\n#ifndef OPENSSL_NO_SRP\n if (s->srp_ctx.login != NULL) {\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_srp)\n || !WPACKET_start_sub_packet_u16(pkt)\n || !WPACKET_start_sub_packet_u8(pkt)\n || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)\n || !WPACKET_memcpy(pkt, s->srp_ctx.login,\n strlen(s->srp_ctx.login))\n || !WPACKET_close(pkt)\n || !WPACKET_close(pkt)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n }\n#endif\n#ifndef OPENSSL_NO_EC\n if (using_ecc) {\n const unsigned char *pcurves, *pformats;\n size_t num_curves, num_formats;\n size_t i;\n tls1_get_formatlist(s, &pformats, &num_formats);\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats)\n || !WPACKET_start_sub_packet_u16(pkt)\n || !WPACKET_sub_memcpy_u8(pkt, pformats, num_formats)\n || !WPACKET_close(pkt)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n pcurves = s->tlsext_ellipticcurvelist;\n if (!tls1_get_curvelist(s, 0, &pcurves, &num_curves)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_elliptic_curves)\n || !WPACKET_start_sub_packet_u16(pkt)\n || !WPACKET_start_sub_packet_u16(pkt)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n for (i = 0; i < num_curves; i++, pcurves += 2) {\n if (tls_curve_allowed(s, pcurves, SSL_SECOP_CURVE_SUPPORTED)) {\n if (!WPACKET_put_bytes_u8(pkt, pcurves[0])\n || !WPACKET_put_bytes_u8(pkt, pcurves[1])) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT,\n ERR_R_INTERNAL_ERROR);\n return 0;\n }\n }\n }\n if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n }\n#endif\n if (tls_use_ticket(s)) {\n int ticklen;\n if (!s->new_session && s->session && s->session->tlsext_tick)\n ticklen = s->session->tlsext_ticklen;\n else if (s->session && s->tlsext_session_ticket &&\n s->tlsext_session_ticket->data) {\n ticklen = s->tlsext_session_ticket->length;\n s->session->tlsext_tick = OPENSSL_malloc(ticklen);\n if (s->session->tlsext_tick == NULL) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n memcpy(s->session->tlsext_tick,\n s->tlsext_session_ticket->data, ticklen);\n s->session->tlsext_ticklen = ticklen;\n } else\n ticklen = 0;\n if (ticklen == 0 && s->tlsext_session_ticket &&\n s->tlsext_session_ticket->data == NULL)\n goto skip_ext;\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket)\n || !WPACKET_sub_memcpy_u16(pkt, s->session->tlsext_tick,\n ticklen)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n }\n skip_ext:\n if (SSL_CLIENT_USE_SIGALGS(s)) {\n size_t salglen;\n const unsigned char *salg;\n salglen = tls12_get_psigalgs(s, &salg);\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signature_algorithms)\n || !WPACKET_start_sub_packet_u16(pkt)\n || !WPACKET_start_sub_packet_u16(pkt)\n || !tls12_copy_sigalgs(s, pkt, salg, salglen)\n || !WPACKET_close(pkt)\n || !WPACKET_close(pkt)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n }\n#ifndef OPENSSL_NO_OCSP\n if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {\n int i;\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request)\n || !WPACKET_start_sub_packet_u16(pkt)\n || !WPACKET_put_bytes_u8(pkt, TLSEXT_STATUSTYPE_ocsp)\n || !WPACKET_start_sub_packet_u16(pkt)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) {\n unsigned char *idbytes;\n int idlen;\n OCSP_RESPID *id;\n id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);\n idlen = i2d_OCSP_RESPID(id, NULL);\n if (idlen <= 0\n || !WPACKET_sub_allocate_bytes_u16(pkt, idlen, &idbytes)\n || i2d_OCSP_RESPID(id, &idbytes) != idlen) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n }\n if (!WPACKET_close(pkt)\n || !WPACKET_start_sub_packet_u16(pkt)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n if (s->tlsext_ocsp_exts) {\n unsigned char *extbytes;\n int extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);\n if (extlen < 0) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n if (!WPACKET_allocate_bytes(pkt, extlen, &extbytes)\n || i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &extbytes)\n != extlen) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n }\n if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n }\n#endif\n#ifndef OPENSSL_NO_HEARTBEATS\n if (SSL_IS_DTLS(s)) {\n unsigned int mode;\n if (s->tlsext_heartbeat & SSL_DTLSEXT_HB_DONT_RECV_REQUESTS)\n mode = SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;\n else\n mode = SSL_DTLSEXT_HB_ENABLED;\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_heartbeat)\n || !WPACKET_start_sub_packet_u16(pkt)\n || !WPACKET_put_bytes_u8(pkt, mode)\n || !WPACKET_close(pkt)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n }\n#endif\n#ifndef OPENSSL_NO_NEXTPROTONEG\n if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len) {\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg)\n || !WPACKET_put_bytes_u16(pkt, 0)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n }\n#endif\n if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len) {\n if (!WPACKET_put_bytes_u16(pkt,\n TLSEXT_TYPE_application_layer_protocol_negotiation)\n || !WPACKET_start_sub_packet_u16(pkt)\n || !WPACKET_sub_memcpy_u16(pkt, s->alpn_client_proto_list,\n s->alpn_client_proto_list_len)\n || !WPACKET_close(pkt)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n s->s3->alpn_sent = 1;\n }\n#ifndef OPENSSL_NO_SRTP\n if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)) {\n STACK_OF(SRTP_PROTECTION_PROFILE) *clnt = 0;\n SRTP_PROTECTION_PROFILE *prof;\n int i, ct;\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp)\n || !WPACKET_start_sub_packet_u16(pkt)\n || !WPACKET_start_sub_packet_u16(pkt)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n ct = sk_SRTP_PROTECTION_PROFILE_num(clnt);\n for (i = 0; i < ct; i++) {\n prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i);\n if (prof == NULL || !WPACKET_put_bytes_u16(pkt, prof->id)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n }\n if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n }\n#endif\n custom_ext_init(&s->cert->cli_ext);\n if (!custom_ext_add(s, 0, pkt, al)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac)\n || !WPACKET_put_bytes_u16(pkt, 0)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n#ifndef OPENSSL_NO_CT\n if (s->ct_validation_callback != NULL) {\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signed_certificate_timestamp)\n || !WPACKET_put_bytes_u16(pkt, 0)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n }\n#endif\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret)\n || !WPACKET_put_bytes_u16(pkt, 0)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n if (s->options & SSL_OP_TLSEXT_PADDING) {\n unsigned char *padbytes;\n size_t hlen;\n if (!WPACKET_get_total_written(pkt, &hlen)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n if (hlen > 0xff && hlen < 0x200) {\n hlen = 0x200 - hlen;\n if (hlen >= 4)\n hlen -= 4;\n else\n hlen = 0;\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_padding)\n || !WPACKET_sub_allocate_bytes_u16(pkt, hlen, &padbytes)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n memset(padbytes, 0, hlen);\n }\n }\n done:\n return 1;\n}', 'int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)\n{\n WPACKET_SUB *sub;\n unsigned char *lenchars;\n assert(pkt->subs != NULL);\n if (pkt->subs == NULL)\n return 0;\n sub = OPENSSL_zalloc(sizeof(*sub));\n if (sub == NULL)\n return 0;\n sub->parent = pkt->subs;\n pkt->subs = sub;\n sub->pwritten = pkt->written + lenbytes;\n sub->lenbytes = lenbytes;\n if (lenbytes == 0) {\n sub->packet_len = 0;\n return 1;\n }\n if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))\n return 0;\n sub->packet_len = lenchars - (unsigned char *)pkt->buf->data;\n return 1;\n}', 'int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)\n{\n assert(pkt->subs != NULL && len != 0);\n if (pkt->subs == NULL || len == 0)\n return 0;\n if (pkt->maxsize - pkt->written < len)\n return 0;\n if (pkt->buf->length - pkt->written < len) {\n size_t newlen;\n if (pkt->buf->length > SIZE_MAX / 2) {\n newlen = SIZE_MAX;\n } else {\n newlen = (pkt->buf->length == 0) ? DEFAULT_BUF_SIZE\n : pkt->buf->length * 2;\n }\n if (BUF_MEM_grow(pkt->buf, newlen) == 0)\n return 0;\n }\n *allocbytes = (unsigned char *)pkt->buf->data + pkt->curr;\n pkt->written += len;\n pkt->curr += len;\n return 1;\n}']
2,355
0
https://github.com/openssl/openssl/blob/6fc1748ec65c94c195d02b59556434e36a5f7651/ssl/record/ssl3_record.c/#L1232
void ssl3_cbc_copy_mac(unsigned char *out, const SSL3_RECORD *rec, unsigned md_size) { #if defined(CBC_MAC_ROTATE_IN_PLACE) unsigned char rotated_mac_buf[64 + EVP_MAX_MD_SIZE]; unsigned char *rotated_mac; #else unsigned char rotated_mac[EVP_MAX_MD_SIZE]; #endif unsigned mac_end = rec->length; unsigned mac_start = mac_end - md_size; unsigned scan_start = 0; unsigned i, j; unsigned div_spoiler; unsigned rotate_offset; OPENSSL_assert(rec->orig_len >= md_size); OPENSSL_assert(md_size <= EVP_MAX_MD_SIZE); #if defined(CBC_MAC_ROTATE_IN_PLACE) rotated_mac = rotated_mac_buf + ((0 - (size_t)rotated_mac_buf) & 63); #endif if (rec->orig_len > md_size + 255 + 1) scan_start = rec->orig_len - (md_size + 255 + 1); div_spoiler = md_size >> 1; div_spoiler <<= (sizeof(div_spoiler) - 1) * 8; rotate_offset = (div_spoiler + mac_start - scan_start) % md_size; memset(rotated_mac, 0, md_size); for (i = scan_start, j = 0; i < rec->orig_len; i++) { unsigned char mac_started = constant_time_ge_8(i, mac_start); unsigned char mac_ended = constant_time_ge_8(i, mac_end); unsigned char b = rec->data[i]; rotated_mac[j++] |= b & mac_started & ~mac_ended; j &= constant_time_lt(j, md_size); } #if defined(CBC_MAC_ROTATE_IN_PLACE) j = 0; for (i = 0; i < md_size; i++) { ((volatile unsigned char *)rotated_mac)[rotate_offset ^ 32]; out[j++] = rotated_mac[rotate_offset++]; rotate_offset &= constant_time_lt(rotate_offset, md_size); } #else memset(out, 0, md_size); rotate_offset = md_size - rotate_offset; rotate_offset &= constant_time_lt(rotate_offset, md_size); for (i = 0; i < md_size; i++) { for (j = 0; j < md_size; j++) out[j] |= rotated_mac[i] & constant_time_eq_8(j, rotate_offset); rotate_offset++; rotate_offset &= constant_time_lt(rotate_offset, md_size); } #endif }
['int dtls1_get_record(SSL *s)\n{\n int ssl_major, ssl_minor;\n int i, n;\n SSL3_RECORD *rr;\n unsigned char *p = NULL;\n unsigned short version;\n DTLS1_BITMAP *bitmap;\n unsigned int is_next_epoch;\n rr = RECORD_LAYER_get_rrec(&s->rlayer);\n if (dtls1_process_buffered_records(s) < 0)\n return -1;\n if (dtls1_get_processed_record(s))\n return 1;\n again:\n if ((RECORD_LAYER_get_rstate(&s->rlayer) != SSL_ST_READ_BODY) ||\n (RECORD_LAYER_get_packet_length(&s->rlayer) < DTLS1_RT_HEADER_LENGTH)) {\n n = ssl3_read_n(s, DTLS1_RT_HEADER_LENGTH,\n SSL3_BUFFER_get_len(&s->rlayer.rbuf), 0, 1);\n if (n <= 0)\n return (n);\n if (RECORD_LAYER_get_packet_length(&s->rlayer) !=\n DTLS1_RT_HEADER_LENGTH) {\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto again;\n }\n RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_BODY);\n p = RECORD_LAYER_get_packet(&s->rlayer);\n if (s->msg_callback)\n s->msg_callback(0, 0, SSL3_RT_HEADER, p, DTLS1_RT_HEADER_LENGTH,\n s, s->msg_callback_arg);\n rr->type = *(p++);\n ssl_major = *(p++);\n ssl_minor = *(p++);\n version = (ssl_major << 8) | ssl_minor;\n n2s(p, rr->epoch);\n memcpy(&(RECORD_LAYER_get_read_sequence(&s->rlayer)[2]), p, 6);\n p += 6;\n n2s(p, rr->length);\n if (!s->first_packet) {\n if (version != s->version) {\n rr->length = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto again;\n }\n }\n if ((version & 0xff00) != (s->version & 0xff00)) {\n rr->length = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto again;\n }\n if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {\n rr->length = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto again;\n }\n }\n if (rr->length >\n RECORD_LAYER_get_packet_length(&s->rlayer) - DTLS1_RT_HEADER_LENGTH) {\n i = rr->length;\n n = ssl3_read_n(s, i, i, 1, 1);\n if (n != i) {\n rr->length = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto again;\n }\n }\n RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_HEADER);\n bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch);\n if (bitmap == NULL) {\n rr->length = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto again;\n }\n#ifndef OPENSSL_NO_SCTP\n if (!BIO_dgram_is_sctp(SSL_get_rbio(s))) {\n#endif\n if (!dtls1_record_replay_check(s, bitmap)) {\n rr->length = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto again;\n }\n#ifndef OPENSSL_NO_SCTP\n }\n#endif\n if (rr->length == 0)\n goto again;\n if (is_next_epoch) {\n if ((SSL_in_init(s) || ossl_statem_get_in_handshake(s))) {\n if (dtls1_buffer_record\n (s, &(DTLS_RECORD_LAYER_get_unprocessed_rcds(&s->rlayer)),\n rr->seq_num) < 0)\n return -1;\n dtls1_record_bitmap_update(s, bitmap);\n }\n rr->length = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto again;\n }\n if (!dtls1_process_record(s)) {\n rr->length = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto again;\n }\n dtls1_record_bitmap_update(s, bitmap);\n return (1);\n}', 'int dtls1_process_record(SSL *s)\n{\n int i, al;\n int enc_err;\n SSL_SESSION *sess;\n SSL3_RECORD *rr;\n unsigned int mac_size;\n unsigned char md[EVP_MAX_MD_SIZE];\n rr = RECORD_LAYER_get_rrec(&s->rlayer);\n sess = s->session;\n rr->input = &(RECORD_LAYER_get_packet(&s->rlayer)[DTLS1_RT_HEADER_LENGTH]);\n if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {\n al = SSL_AD_RECORD_OVERFLOW;\n SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);\n goto f_err;\n }\n rr->data = rr->input;\n rr->orig_len = rr->length;\n enc_err = s->method->ssl3_enc->enc(s, rr, 1, 0);\n if (enc_err == 0) {\n rr->length = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto err;\n }\n#ifdef SSL_DEBUG\n printf("dec %d\\n", rr->length);\n {\n unsigned int z;\n for (z = 0; z < rr->length; z++)\n printf("%02X%c", rr->data[z], ((z + 1) % 16) ? \' \' : \'\\n\');\n }\n printf("\\n");\n#endif\n if ((sess != NULL) &&\n (s->enc_read_ctx != NULL) && (EVP_MD_CTX_md(s->read_hash) != NULL)) {\n unsigned char *mac = NULL;\n unsigned char mac_tmp[EVP_MAX_MD_SIZE];\n mac_size = EVP_MD_CTX_size(s->read_hash);\n OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);\n if (rr->orig_len < mac_size ||\n (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE &&\n rr->orig_len < mac_size + 1)) {\n al = SSL_AD_DECODE_ERROR;\n SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_LENGTH_TOO_SHORT);\n goto f_err;\n }\n if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) {\n mac = mac_tmp;\n ssl3_cbc_copy_mac(mac_tmp, rr, mac_size);\n rr->length -= mac_size;\n } else {\n rr->length -= mac_size;\n mac = &rr->data[rr->length];\n }\n i = s->method->ssl3_enc->mac(s, rr, md, 0 );\n if (i < 0 || mac == NULL\n || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0)\n enc_err = -1;\n if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)\n enc_err = -1;\n }\n if (enc_err < 0) {\n rr->length = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto err;\n }\n if (s->expand != NULL) {\n if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {\n al = SSL_AD_RECORD_OVERFLOW;\n SSLerr(SSL_F_DTLS1_PROCESS_RECORD,\n SSL_R_COMPRESSED_LENGTH_TOO_LONG);\n goto f_err;\n }\n if (!ssl3_do_uncompress(s, rr)) {\n al = SSL_AD_DECOMPRESSION_FAILURE;\n SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_BAD_DECOMPRESSION);\n goto f_err;\n }\n }\n if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH) {\n al = SSL_AD_RECORD_OVERFLOW;\n SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_DATA_LENGTH_TOO_LONG);\n goto f_err;\n }\n rr->off = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n return (1);\n f_err:\n ssl3_send_alert(s, SSL3_AL_FATAL, al);\n err:\n return (0);\n}', 'void ssl3_cbc_copy_mac(unsigned char *out,\n const SSL3_RECORD *rec, unsigned md_size)\n{\n#if defined(CBC_MAC_ROTATE_IN_PLACE)\n unsigned char rotated_mac_buf[64 + EVP_MAX_MD_SIZE];\n unsigned char *rotated_mac;\n#else\n unsigned char rotated_mac[EVP_MAX_MD_SIZE];\n#endif\n unsigned mac_end = rec->length;\n unsigned mac_start = mac_end - md_size;\n unsigned scan_start = 0;\n unsigned i, j;\n unsigned div_spoiler;\n unsigned rotate_offset;\n OPENSSL_assert(rec->orig_len >= md_size);\n OPENSSL_assert(md_size <= EVP_MAX_MD_SIZE);\n#if defined(CBC_MAC_ROTATE_IN_PLACE)\n rotated_mac = rotated_mac_buf + ((0 - (size_t)rotated_mac_buf) & 63);\n#endif\n if (rec->orig_len > md_size + 255 + 1)\n scan_start = rec->orig_len - (md_size + 255 + 1);\n div_spoiler = md_size >> 1;\n div_spoiler <<= (sizeof(div_spoiler) - 1) * 8;\n rotate_offset = (div_spoiler + mac_start - scan_start) % md_size;\n memset(rotated_mac, 0, md_size);\n for (i = scan_start, j = 0; i < rec->orig_len; i++) {\n unsigned char mac_started = constant_time_ge_8(i, mac_start);\n unsigned char mac_ended = constant_time_ge_8(i, mac_end);\n unsigned char b = rec->data[i];\n rotated_mac[j++] |= b & mac_started & ~mac_ended;\n j &= constant_time_lt(j, md_size);\n }\n#if defined(CBC_MAC_ROTATE_IN_PLACE)\n j = 0;\n for (i = 0; i < md_size; i++) {\n ((volatile unsigned char *)rotated_mac)[rotate_offset ^ 32];\n out[j++] = rotated_mac[rotate_offset++];\n rotate_offset &= constant_time_lt(rotate_offset, md_size);\n }\n#else\n memset(out, 0, md_size);\n rotate_offset = md_size - rotate_offset;\n rotate_offset &= constant_time_lt(rotate_offset, md_size);\n for (i = 0; i < md_size; i++) {\n for (j = 0; j < md_size; j++)\n out[j] |= rotated_mac[i] & constant_time_eq_8(j, rotate_offset);\n rotate_offset++;\n rotate_offset &= constant_time_lt(rotate_offset, md_size);\n }\n#endif\n}']
2,356
0
https://github.com/nginx/nginx/blob/4fe0a09942f8aed90f84c77969847980e9aadd98/src/core/ngx_string.c/#L1006
ssize_t ngx_atosz(u_char *line, size_t n) { ssize_t value, cutoff, cutlim; if (n == 0) { return NGX_ERROR; } cutoff = NGX_MAX_SIZE_T_VALUE / 10; cutlim = NGX_MAX_SIZE_T_VALUE % 10; for (value = 0; n--; line++) { if (*line < '0' || *line > '9') { return NGX_ERROR; } if (value >= cutoff && (value > cutoff || *line - '0' > cutlim)) { return NGX_ERROR; } value = value * 10 + (*line - '0'); } return value; }
['char *\nngx_http_file_cache_set_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)\n{\n char *confp = conf;\n off_t max_size;\n u_char *last, *p;\n time_t inactive;\n size_t len;\n ssize_t size;\n ngx_str_t s, name, *value;\n ngx_int_t loader_files;\n ngx_msec_t loader_sleep, loader_threshold;\n ngx_uint_t i, n, use_temp_path;\n ngx_array_t *caches;\n ngx_http_file_cache_t *cache, **ce;\n cache = ngx_pcalloc(cf->pool, sizeof(ngx_http_file_cache_t));\n if (cache == NULL) {\n return NGX_CONF_ERROR;\n }\n cache->path = ngx_pcalloc(cf->pool, sizeof(ngx_path_t));\n if (cache->path == NULL) {\n return NGX_CONF_ERROR;\n }\n use_temp_path = 1;\n inactive = 600;\n loader_files = 100;\n loader_sleep = 50;\n loader_threshold = 200;\n name.len = 0;\n size = 0;\n max_size = NGX_MAX_OFF_T_VALUE;\n value = cf->args->elts;\n cache->path->name = value[1];\n if (cache->path->name.data[cache->path->name.len - 1] == \'/\') {\n cache->path->name.len--;\n }\n if (ngx_conf_full_name(cf->cycle, &cache->path->name, 0) != NGX_OK) {\n return NGX_CONF_ERROR;\n }\n for (i = 2; i < cf->args->nelts; i++) {\n if (ngx_strncmp(value[i].data, "levels=", 7) == 0) {\n p = value[i].data + 7;\n last = value[i].data + value[i].len;\n for (n = 0; n < 3 && p < last; n++) {\n if (*p > \'0\' && *p < \'3\') {\n cache->path->level[n] = *p++ - \'0\';\n cache->path->len += cache->path->level[n] + 1;\n if (p == last) {\n break;\n }\n if (*p++ == \':\' && n < 2 && p != last) {\n continue;\n }\n goto invalid_levels;\n }\n goto invalid_levels;\n }\n if (cache->path->len < 10 + 3) {\n continue;\n }\n invalid_levels:\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "invalid \\"levels\\" \\"%V\\"", &value[i]);\n return NGX_CONF_ERROR;\n }\n if (ngx_strncmp(value[i].data, "use_temp_path=", 14) == 0) {\n if (ngx_strcmp(&value[i].data[14], "on") == 0) {\n use_temp_path = 1;\n } else if (ngx_strcmp(&value[i].data[14], "off") == 0) {\n use_temp_path = 0;\n } else {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "invalid use_temp_path value \\"%V\\", "\n "it must be \\"on\\" or \\"off\\"",\n &value[i]);\n return NGX_CONF_ERROR;\n }\n continue;\n }\n if (ngx_strncmp(value[i].data, "keys_zone=", 10) == 0) {\n name.data = value[i].data + 10;\n p = (u_char *) ngx_strchr(name.data, \':\');\n if (p) {\n name.len = p - name.data;\n p++;\n s.len = value[i].data + value[i].len - p;\n s.data = p;\n size = ngx_parse_size(&s);\n if (size > 8191) {\n continue;\n }\n }\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "invalid keys zone size \\"%V\\"", &value[i]);\n return NGX_CONF_ERROR;\n }\n if (ngx_strncmp(value[i].data, "inactive=", 9) == 0) {\n s.len = value[i].len - 9;\n s.data = value[i].data + 9;\n inactive = ngx_parse_time(&s, 1);\n if (inactive == (time_t) NGX_ERROR) {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "invalid inactive value \\"%V\\"", &value[i]);\n return NGX_CONF_ERROR;\n }\n continue;\n }\n if (ngx_strncmp(value[i].data, "max_size=", 9) == 0) {\n s.len = value[i].len - 9;\n s.data = value[i].data + 9;\n max_size = ngx_parse_offset(&s);\n if (max_size < 0) {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "invalid max_size value \\"%V\\"", &value[i]);\n return NGX_CONF_ERROR;\n }\n continue;\n }\n if (ngx_strncmp(value[i].data, "loader_files=", 13) == 0) {\n loader_files = ngx_atoi(value[i].data + 13, value[i].len - 13);\n if (loader_files == NGX_ERROR) {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "invalid loader_files value \\"%V\\"", &value[i]);\n return NGX_CONF_ERROR;\n }\n continue;\n }\n if (ngx_strncmp(value[i].data, "loader_sleep=", 13) == 0) {\n s.len = value[i].len - 13;\n s.data = value[i].data + 13;\n loader_sleep = ngx_parse_time(&s, 0);\n if (loader_sleep == (ngx_msec_t) NGX_ERROR) {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "invalid loader_sleep value \\"%V\\"", &value[i]);\n return NGX_CONF_ERROR;\n }\n continue;\n }\n if (ngx_strncmp(value[i].data, "loader_threshold=", 17) == 0) {\n s.len = value[i].len - 17;\n s.data = value[i].data + 17;\n loader_threshold = ngx_parse_time(&s, 0);\n if (loader_threshold == (ngx_msec_t) NGX_ERROR) {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "invalid loader_threshold value \\"%V\\"", &value[i]);\n return NGX_CONF_ERROR;\n }\n continue;\n }\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "invalid parameter \\"%V\\"", &value[i]);\n return NGX_CONF_ERROR;\n }\n if (name.len == 0 || size == 0) {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "\\"%V\\" must have \\"keys_zone\\" parameter",\n &cmd->name);\n return NGX_CONF_ERROR;\n }\n cache->path->manager = ngx_http_file_cache_manager;\n cache->path->loader = ngx_http_file_cache_loader;\n cache->path->data = cache;\n cache->path->conf_file = cf->conf_file->file.name.data;\n cache->path->line = cf->conf_file->line;\n cache->loader_files = loader_files;\n cache->loader_sleep = loader_sleep;\n cache->loader_threshold = loader_threshold;\n if (ngx_add_path(cf, &cache->path) != NGX_OK) {\n return NGX_CONF_ERROR;\n }\n if (!use_temp_path) {\n cache->temp_path = ngx_pcalloc(cf->pool, sizeof(ngx_path_t));\n if (cache->temp_path == NULL) {\n return NGX_CONF_ERROR;\n }\n len = cache->path->name.len + sizeof("/temp") - 1;\n p = ngx_pnalloc(cf->pool, len + 1);\n if (p == NULL) {\n return NGX_CONF_ERROR;\n }\n cache->temp_path->name.len = len;\n cache->temp_path->name.data = p;\n p = ngx_cpymem(p, cache->path->name.data, cache->path->name.len);\n ngx_memcpy(p, "/temp", sizeof("/temp"));\n ngx_memcpy(&cache->temp_path->level, &cache->path->level,\n 3 * sizeof(size_t));\n cache->temp_path->len = cache->path->len;\n cache->temp_path->conf_file = cf->conf_file->file.name.data;\n cache->temp_path->line = cf->conf_file->line;\n if (ngx_add_path(cf, &cache->temp_path) != NGX_OK) {\n return NGX_CONF_ERROR;\n }\n }\n cache->shm_zone = ngx_shared_memory_add(cf, &name, size, cmd->post);\n if (cache->shm_zone == NULL) {\n return NGX_CONF_ERROR;\n }\n if (cache->shm_zone->data) {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "duplicate zone \\"%V\\"", &name);\n return NGX_CONF_ERROR;\n }\n cache->shm_zone->init = ngx_http_file_cache_init;\n cache->shm_zone->data = cache;\n cache->inactive = inactive;\n cache->max_size = max_size;\n caches = (ngx_array_t *) (confp + cmd->offset);\n ce = ngx_array_push(caches);\n if (ce == NULL) {\n return NGX_CONF_ERROR;\n }\n *ce = cache;\n return NGX_CONF_OK;\n}', "ssize_t\nngx_parse_size(ngx_str_t *line)\n{\n u_char unit;\n size_t len;\n ssize_t size, scale, max;\n len = line->len;\n unit = line->data[len - 1];\n switch (unit) {\n case 'K':\n case 'k':\n len--;\n max = NGX_MAX_SIZE_T_VALUE / 1024;\n scale = 1024;\n break;\n case 'M':\n case 'm':\n len--;\n max = NGX_MAX_SIZE_T_VALUE / (1024 * 1024);\n scale = 1024 * 1024;\n break;\n default:\n max = NGX_MAX_SIZE_T_VALUE;\n scale = 1;\n }\n size = ngx_atosz(line->data, len);\n if (size == NGX_ERROR || size > max) {\n return NGX_ERROR;\n }\n size *= scale;\n return size;\n}", "ssize_t\nngx_atosz(u_char *line, size_t n)\n{\n ssize_t value, cutoff, cutlim;\n if (n == 0) {\n return NGX_ERROR;\n }\n cutoff = NGX_MAX_SIZE_T_VALUE / 10;\n cutlim = NGX_MAX_SIZE_T_VALUE % 10;\n for (value = 0; n--; line++) {\n if (*line < '0' || *line > '9') {\n return NGX_ERROR;\n }\n if (value >= cutoff && (value > cutoff || *line - '0' > cutlim)) {\n return NGX_ERROR;\n }\n value = value * 10 + (*line - '0');\n }\n return value;\n}"]
2,357
0
https://github.com/openssl/openssl/blob/6d0b5ee1d6163732b886bc0567dbce08aeade4c1/ssl/statem/statem_clnt.c/#L2808
static int tls_construct_cke_rsa(SSL *s, WPACKET *pkt, int *al) { #ifndef OPENSSL_NO_RSA unsigned char *encdata = NULL; EVP_PKEY *pkey = NULL; EVP_PKEY_CTX *pctx = NULL; size_t enclen; unsigned char *pms = NULL; size_t pmslen = 0; if (s->session->peer == NULL) { SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR); return 0; } pkey = X509_get0_pubkey(s->session->peer); if (EVP_PKEY_get0_RSA(pkey) == NULL) { SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR); return 0; } pmslen = SSL_MAX_MASTER_KEY_LENGTH; pms = OPENSSL_malloc(pmslen); if (pms == NULL) { SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_MALLOC_FAILURE); *al = SSL_AD_INTERNAL_ERROR; return 0; } pms[0] = s->client_version >> 8; pms[1] = s->client_version & 0xff; if (RAND_bytes(pms + 2, (int)(pmslen - 2)) <= 0) { goto err; } if (s->version > SSL3_VERSION && !WPACKET_start_sub_packet_u16(pkt)) { SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR); goto err; } pctx = EVP_PKEY_CTX_new(pkey, NULL); if (pctx == NULL || EVP_PKEY_encrypt_init(pctx) <= 0 || EVP_PKEY_encrypt(pctx, NULL, &enclen, pms, pmslen) <= 0) { SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_EVP_LIB); goto err; } if (!WPACKET_allocate_bytes(pkt, enclen, &encdata) || EVP_PKEY_encrypt(pctx, encdata, &enclen, pms, pmslen) <= 0) { SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, SSL_R_BAD_RSA_ENCRYPT); goto err; } EVP_PKEY_CTX_free(pctx); pctx = NULL; if (s->version > SSL3_VERSION && !WPACKET_close(pkt)) { SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR); goto err; } s->s3->tmp.pms = pms; s->s3->tmp.pmslen = pmslen; if (!ssl_log_rsa_client_key_exchange(s, encdata, enclen, pms, pmslen)) goto err; return 1; err: OPENSSL_clear_free(pms, pmslen); EVP_PKEY_CTX_free(pctx); return 0; #else SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR); *al = SSL_AD_INTERNAL_ERROR; return 0; #endif }
['static int tls_construct_cke_rsa(SSL *s, WPACKET *pkt, int *al)\n{\n#ifndef OPENSSL_NO_RSA\n unsigned char *encdata = NULL;\n EVP_PKEY *pkey = NULL;\n EVP_PKEY_CTX *pctx = NULL;\n size_t enclen;\n unsigned char *pms = NULL;\n size_t pmslen = 0;\n if (s->session->peer == NULL) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n pkey = X509_get0_pubkey(s->session->peer);\n if (EVP_PKEY_get0_RSA(pkey) == NULL) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n pmslen = SSL_MAX_MASTER_KEY_LENGTH;\n pms = OPENSSL_malloc(pmslen);\n if (pms == NULL) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_MALLOC_FAILURE);\n *al = SSL_AD_INTERNAL_ERROR;\n return 0;\n }\n pms[0] = s->client_version >> 8;\n pms[1] = s->client_version & 0xff;\n if (RAND_bytes(pms + 2, (int)(pmslen - 2)) <= 0) {\n goto err;\n }\n if (s->version > SSL3_VERSION && !WPACKET_start_sub_packet_u16(pkt)) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n pctx = EVP_PKEY_CTX_new(pkey, NULL);\n if (pctx == NULL || EVP_PKEY_encrypt_init(pctx) <= 0\n || EVP_PKEY_encrypt(pctx, NULL, &enclen, pms, pmslen) <= 0) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_EVP_LIB);\n goto err;\n }\n if (!WPACKET_allocate_bytes(pkt, enclen, &encdata)\n || EVP_PKEY_encrypt(pctx, encdata, &enclen, pms, pmslen) <= 0) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, SSL_R_BAD_RSA_ENCRYPT);\n goto err;\n }\n EVP_PKEY_CTX_free(pctx);\n pctx = NULL;\n if (s->version > SSL3_VERSION && !WPACKET_close(pkt)) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n s->s3->tmp.pms = pms;\n s->s3->tmp.pmslen = pmslen;\n if (!ssl_log_rsa_client_key_exchange(s, encdata, enclen, pms, pmslen))\n goto err;\n return 1;\n err:\n OPENSSL_clear_free(pms, pmslen);\n EVP_PKEY_CTX_free(pctx);\n return 0;\n#else\n SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);\n *al = SSL_AD_INTERNAL_ERROR;\n return 0;\n#endif\n}', 'EVP_PKEY *X509_get0_pubkey(const X509 *x)\n{\n if (x == NULL)\n return NULL;\n return X509_PUBKEY_get0(x->cert_info.key);\n}', 'EVP_PKEY *X509_PUBKEY_get0(X509_PUBKEY *key)\n{\n EVP_PKEY *ret = NULL;\n if (key == NULL || key->public_key == NULL)\n return NULL;\n if (key->pkey != NULL)\n return key->pkey;\n x509_pubkey_decode(&ret, key);\n if (ret != NULL) {\n X509err(X509_F_X509_PUBKEY_GET0, ERR_R_INTERNAL_ERROR);\n EVP_PKEY_free(ret);\n }\n return NULL;\n}', 'static int x509_pubkey_decode(EVP_PKEY **ppkey, X509_PUBKEY *key)\n {\n EVP_PKEY *pkey = EVP_PKEY_new();\n if (pkey == NULL) {\n X509err(X509_F_X509_PUBKEY_DECODE, ERR_R_MALLOC_FAILURE);\n return -1;\n }\n if (!EVP_PKEY_set_type(pkey, OBJ_obj2nid(key->algor->algorithm))) {\n X509err(X509_F_X509_PUBKEY_DECODE, X509_R_UNSUPPORTED_ALGORITHM);\n goto error;\n }\n if (pkey->ameth->pub_decode) {\n if (!pkey->ameth->pub_decode(pkey, key)) {\n X509err(X509_F_X509_PUBKEY_DECODE, X509_R_PUBLIC_KEY_DECODE_ERROR);\n goto error;\n }\n } else {\n X509err(X509_F_X509_PUBKEY_DECODE, X509_R_METHOD_NOT_SUPPORTED);\n goto error;\n }\n *ppkey = pkey;\n return 1;\n error:\n EVP_PKEY_free(pkey);\n return 0;\n}', 'RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey)\n{\n if (pkey->type != EVP_PKEY_RSA) {\n EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY);\n return NULL;\n }\n return pkey->pkey.rsa;\n}']
2,358
0
https://github.com/openssl/openssl/blob/1ea01427c5195dafa4f00202237c5b7a389f034b/test/evp_test.c/#L1735
static int encode_test_init(struct evp_test *t, const char *encoding) { struct encode_data *edata = OPENSSL_zalloc(sizeof(*edata)); if (strcmp(encoding, "canonical") == 0) { edata->encoding = BASE64_CANONICAL_ENCODING; } else if (strcmp(encoding, "valid") == 0) { edata->encoding = BASE64_VALID_ENCODING; } else if (strcmp(encoding, "invalid") == 0) { edata->encoding = BASE64_INVALID_ENCODING; t->expected_err = OPENSSL_strdup("DECODE_ERROR"); if (t->expected_err == NULL) return 0; } else { fprintf(stderr, "Bad encoding: %s. Should be one of " "{canonical, valid, invalid}\n", encoding); return 0; } t->data = edata; return 1; }
['static int encode_test_init(struct evp_test *t, const char *encoding)\n{\n struct encode_data *edata = OPENSSL_zalloc(sizeof(*edata));\n if (strcmp(encoding, "canonical") == 0) {\n edata->encoding = BASE64_CANONICAL_ENCODING;\n } else if (strcmp(encoding, "valid") == 0) {\n edata->encoding = BASE64_VALID_ENCODING;\n } else if (strcmp(encoding, "invalid") == 0) {\n edata->encoding = BASE64_INVALID_ENCODING;\n t->expected_err = OPENSSL_strdup("DECODE_ERROR");\n if (t->expected_err == NULL)\n return 0;\n } else {\n fprintf(stderr, "Bad encoding: %s. Should be one of "\n "{canonical, valid, invalid}\\n", encoding);\n return 0;\n }\n t->data = edata;\n return 1;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (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}', '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}']
2,359
0
https://github.com/openssl/openssl/blob/313fce7b61ecaf5879cf84b256bdd0964134836e/crypto/bn/bn_ctx.c/#L353
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['int BN_mod_exp2_mont(BIGNUM *rr, const BIGNUM *a1, const BIGNUM *p1,\n\tconst BIGNUM *a2, const BIGNUM *p2, const BIGNUM *m,\n\tBN_CTX *ctx, BN_MONT_CTX *in_mont)\n\t{\n\tint i,j,bits,b,bits1,bits2,ret=0,wpos1,wpos2,window1,window2,wvalue1,wvalue2;\n\tint r_is_one=1;\n\tBIGNUM *d,*r;\n\tconst BIGNUM *a_mod_m;\n\tBIGNUM *val1[TABLE_SIZE], *val2[TABLE_SIZE];\n\tBN_MONT_CTX *mont=NULL;\n\tbn_check_top(a1);\n\tbn_check_top(p1);\n\tbn_check_top(a2);\n\tbn_check_top(p2);\n\tbn_check_top(m);\n\tif (!(m->d[0] & 1))\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_EXP2_MONT,BN_R_CALLED_WITH_EVEN_MODULUS);\n\t\treturn(0);\n\t\t}\n\tbits1=BN_num_bits(p1);\n\tbits2=BN_num_bits(p2);\n\tif ((bits1 == 0) && (bits2 == 0))\n\t\t{\n\t\tret = BN_one(rr);\n\t\treturn ret;\n\t\t}\n\tbits=(bits1 > bits2)?bits1:bits2;\n\tBN_CTX_start(ctx);\n\td = BN_CTX_get(ctx);\n\tr = BN_CTX_get(ctx);\n\tval1[0] = BN_CTX_get(ctx);\n\tval2[0] = BN_CTX_get(ctx);\n\tif(!d || !r || !val1[0] || !val2[0]) goto err;\n\tif (in_mont != NULL)\n\t\tmont=in_mont;\n\telse\n\t\t{\n\t\tif ((mont=BN_MONT_CTX_new()) == NULL) goto err;\n\t\tif (!BN_MONT_CTX_set(mont,m,ctx)) goto err;\n\t\t}\n\twindow1 = BN_window_bits_for_exponent_size(bits1);\n\twindow2 = BN_window_bits_for_exponent_size(bits2);\n\tif (a1->neg || BN_ucmp(a1,m) >= 0)\n\t\t{\n\t\tif (!BN_mod(val1[0],a1,m,ctx))\n\t\t\tgoto err;\n\t\ta_mod_m = val1[0];\n\t\t}\n\telse\n\t\ta_mod_m = a1;\n\tif (BN_is_zero(a_mod_m))\n\t\t{\n\t\tBN_zero(rr);\n\t\tret = 1;\n\t\tgoto err;\n\t\t}\n\tif (!BN_to_montgomery(val1[0],a_mod_m,mont,ctx)) goto err;\n\tif (window1 > 1)\n\t\t{\n\t\tif (!BN_mod_mul_montgomery(d,val1[0],val1[0],mont,ctx)) goto err;\n\t\tj=1<<(window1-1);\n\t\tfor (i=1; i<j; i++)\n\t\t\t{\n\t\t\tif(((val1[i] = BN_CTX_get(ctx)) == NULL) ||\n\t\t\t\t\t!BN_mod_mul_montgomery(val1[i],val1[i-1],\n\t\t\t\t\t\td,mont,ctx))\n\t\t\t\tgoto err;\n\t\t\t}\n\t\t}\n\tif (a2->neg || BN_ucmp(a2,m) >= 0)\n\t\t{\n\t\tif (!BN_mod(val2[0],a2,m,ctx))\n\t\t\tgoto err;\n\t\ta_mod_m = val2[0];\n\t\t}\n\telse\n\t\ta_mod_m = a2;\n\tif (BN_is_zero(a_mod_m))\n\t\t{\n\t\tBN_zero(rr);\n\t\tret = 1;\n\t\tgoto err;\n\t\t}\n\tif (!BN_to_montgomery(val2[0],a_mod_m,mont,ctx)) goto err;\n\tif (window2 > 1)\n\t\t{\n\t\tif (!BN_mod_mul_montgomery(d,val2[0],val2[0],mont,ctx)) goto err;\n\t\tj=1<<(window2-1);\n\t\tfor (i=1; i<j; i++)\n\t\t\t{\n\t\t\tif(((val2[i] = BN_CTX_get(ctx)) == NULL) ||\n\t\t\t\t\t!BN_mod_mul_montgomery(val2[i],val2[i-1],\n\t\t\t\t\t\td,mont,ctx))\n\t\t\t\tgoto err;\n\t\t\t}\n\t\t}\n\tr_is_one=1;\n\twvalue1=0;\n\twvalue2=0;\n\twpos1=0;\n\twpos2=0;\n\tif (!BN_to_montgomery(r,BN_value_one(),mont,ctx)) goto err;\n\tfor (b=bits-1; b>=0; b--)\n\t\t{\n\t\tif (!r_is_one)\n\t\t\t{\n\t\t\tif (!BN_mod_mul_montgomery(r,r,r,mont,ctx))\n\t\t\t\tgoto err;\n\t\t\t}\n\t\tif (!wvalue1)\n\t\t\tif (BN_is_bit_set(p1, b))\n\t\t\t\t{\n\t\t\t\ti = b-window1+1;\n\t\t\t\twhile (!BN_is_bit_set(p1, i))\n\t\t\t\t\ti++;\n\t\t\t\twpos1 = i;\n\t\t\t\twvalue1 = 1;\n\t\t\t\tfor (i = b-1; i >= wpos1; i--)\n\t\t\t\t\t{\n\t\t\t\t\twvalue1 <<= 1;\n\t\t\t\t\tif (BN_is_bit_set(p1, i))\n\t\t\t\t\t\twvalue1++;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\tif (!wvalue2)\n\t\t\tif (BN_is_bit_set(p2, b))\n\t\t\t\t{\n\t\t\t\ti = b-window2+1;\n\t\t\t\twhile (!BN_is_bit_set(p2, i))\n\t\t\t\t\ti++;\n\t\t\t\twpos2 = i;\n\t\t\t\twvalue2 = 1;\n\t\t\t\tfor (i = b-1; i >= wpos2; i--)\n\t\t\t\t\t{\n\t\t\t\t\twvalue2 <<= 1;\n\t\t\t\t\tif (BN_is_bit_set(p2, i))\n\t\t\t\t\t\twvalue2++;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\tif (wvalue1 && b == wpos1)\n\t\t\t{\n\t\t\tif (!BN_mod_mul_montgomery(r,r,val1[wvalue1>>1],mont,ctx))\n\t\t\t\tgoto err;\n\t\t\twvalue1 = 0;\n\t\t\tr_is_one = 0;\n\t\t\t}\n\t\tif (wvalue2 && b == wpos2)\n\t\t\t{\n\t\t\tif (!BN_mod_mul_montgomery(r,r,val2[wvalue2>>1],mont,ctx))\n\t\t\t\tgoto err;\n\t\t\twvalue2 = 0;\n\t\t\tr_is_one = 0;\n\t\t\t}\n\t\t}\n\tBN_from_montgomery(rr,r,mont,ctx);\n\tret=1;\nerr:\n\tif ((in_mont == NULL) && (mont != NULL)) BN_MONT_CTX_free(mont);\n\tBN_CTX_end(ctx);\n\tbn_check_top(rr);\n\treturn(ret);\n\t}', '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_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n\t BN_CTX *ctx)\n\t{\n\tint norm_shift,i,loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tint num_n,div_n;\n\tif ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0))\n\t\t{\n\t\treturn BN_div_no_branch(dv, rm, num, divisor, ctx);\n\t\t}\n\tbn_check_top(dv);\n\tbn_check_top(rm);\n\tbn_check_top(num);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\n\t\t}\n\tif (BN_ucmp(num,divisor) < 0)\n\t\t{\n\t\tif (rm != NULL)\n\t\t\t{ if (BN_copy(rm,num) == NULL) return(0); }\n\t\tif (dv != NULL) BN_zero(dv);\n\t\treturn(1);\n\t\t}\n\tBN_CTX_start(ctx);\n\ttmp=BN_CTX_get(ctx);\n\tsnum=BN_CTX_get(ctx);\n\tsdiv=BN_CTX_get(ctx);\n\tif (dv == NULL)\n\t\tres=BN_CTX_get(ctx);\n\telse\tres=dv;\n\tif (sdiv == NULL || res == NULL) goto err;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tif (!(BN_lshift(sdiv,divisor,norm_shift))) goto err;\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tif (!(BN_lshift(snum,num,norm_shift))) goto err;\n\tsnum->neg=0;\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\twnum.neg = 0;\n\twnum.d = &(snum->d[loop]);\n\twnum.top = div_n;\n\twnum.dmax = snum->dmax - loop;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (BN_ucmp(&wnum,sdiv) >= 0)\n\t\t{\n\t\tbn_clear_top2max(&wnum);\n\t\tbn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n\t\t*resp=1;\n\t\t}\n\telse\n\t\tres->top--;\n\tif (res->top == 0)\n\t\tres->neg = 0;\n\telse\n\t\tresp--;\n\tfor (i=0; i<loop-1; i++, wnump--, resp--)\n\t\t{\n\t\tBN_ULONG q,l0;\n#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n\t\tBN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG);\n\t\tq=bn_div_3_words(wnump,d1,d0);\n#else\n\t\tBN_ULONG n0,n1,rem=0;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\t{\n#ifdef BN_LLONG\n\t\t\tBN_ULLONG t2;\n#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n\t\t\tq=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);\n#else\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tt2 -= d1;\n\t\t\t\t}\n#else\n\t\t\tBN_ULONG t2l,t2h,ql,qh;\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#if defined(BN_UMULT_LOHI)\n\t\t\tBN_UMULT_LOHI(t2l,t2h,d1,q);\n#elif defined(BN_UMULT_HIGH)\n\t\t\tt2l = d1 * q;\n\t\t\tt2h = BN_UMULT_HIGH(d1,q);\n#else\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n#endif\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif ((t2h < rem) ||\n\t\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t\t}\n#endif\n\t\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\twnum.d--;\n\t\tif (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1))\n\t\t\t{\n\t\t\tq--;\n\t\t\tif (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n\t\t\t\t(*wnump)++;\n\t\t\t}\n\t\t*resp = q;\n\t\t}\n\tbn_correct_top(snum);\n\tif (rm != NULL)\n\t\t{\n\t\tint neg = num->neg;\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\tif (!BN_is_zero(rm))\n\t\t\trm->neg = neg;\n\t\tbn_check_top(rm);\n\t\t}\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'int BN_div_no_branch(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,\n\tconst BIGNUM *divisor, BN_CTX *ctx)\n\t{\n\tint norm_shift,i,loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tint num_n,div_n;\n\tbn_check_top(dv);\n\tbn_check_top(rm);\n\tbn_check_top(num);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\n\t\t}\n\tBN_CTX_start(ctx);\n\ttmp=BN_CTX_get(ctx);\n\tsnum=BN_CTX_get(ctx);\n\tsdiv=BN_CTX_get(ctx);\n\tif (dv == NULL)\n\t\tres=BN_CTX_get(ctx);\n\telse\tres=dv;\n\tif (sdiv == NULL || res == NULL) goto err;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tif (!(BN_lshift(sdiv,divisor,norm_shift))) goto err;\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tif (!(BN_lshift(snum,num,norm_shift))) goto err;\n\tsnum->neg=0;\n\tif (snum->top <= sdiv->top+1)\n\t\t{\n\t\tif (bn_wexpand(snum, sdiv->top + 2) == NULL) goto err;\n\t\tfor (i = snum->top; i < sdiv->top + 2; i++) snum->d[i] = 0;\n\t\tsnum->top = sdiv->top + 2;\n\t\t}\n\telse\n\t\t{\n\t\tif (bn_wexpand(snum, snum->top + 1) == NULL) goto err;\n\t\tsnum->d[snum->top] = 0;\n\t\tsnum->top ++;\n\t\t}\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\twnum.neg = 0;\n\twnum.d = &(snum->d[loop]);\n\twnum.top = div_n;\n\twnum.dmax = snum->dmax - loop;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop-1;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (res->top == 0)\n\t\tres->neg = 0;\n\telse\n\t\tresp--;\n\tfor (i=0; i<loop-1; i++, wnump--, resp--)\n\t\t{\n\t\tBN_ULONG q,l0;\n#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n\t\tBN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG);\n\t\tq=bn_div_3_words(wnump,d1,d0);\n#else\n\t\tBN_ULONG n0,n1,rem=0;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\t{\n#ifdef BN_LLONG\n\t\t\tBN_ULLONG t2;\n#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n\t\t\tq=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);\n#else\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tt2 -= d1;\n\t\t\t\t}\n#else\n\t\t\tBN_ULONG t2l,t2h,ql,qh;\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#if defined(BN_UMULT_LOHI)\n\t\t\tBN_UMULT_LOHI(t2l,t2h,d1,q);\n#elif defined(BN_UMULT_HIGH)\n\t\t\tt2l = d1 * q;\n\t\t\tt2h = BN_UMULT_HIGH(d1,q);\n#else\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n#endif\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif ((t2h < rem) ||\n\t\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t\t}\n#endif\n\t\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\twnum.d--;\n\t\tif (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1))\n\t\t\t{\n\t\t\tq--;\n\t\t\tif (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n\t\t\t\t(*wnump)++;\n\t\t\t}\n\t\t*resp = q;\n\t\t}\n\tbn_correct_top(snum);\n\tif (rm != NULL)\n\t\t{\n\t\tint neg = num->neg;\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\tif (!BN_is_zero(rm))\n\t\t\trm->neg = neg;\n\t\tbn_check_top(rm);\n\t\t}\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'void BN_CTX_end(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_end", ctx);\n\tif(ctx->err_stack)\n\t\tctx->err_stack--;\n\telse\n\t\t{\n\t\tunsigned int fp = BN_STACK_pop(&ctx->stack);\n\t\tif(fp < ctx->used)\n\t\t\tBN_POOL_release(&ctx->pool, ctx->used - fp);\n\t\tctx->used = fp;\n\t\tctx->too_many = 0;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n\t{\n\treturn st->indexes[--(st->depth)];\n\t}']
2,360
0
https://github.com/openssl/openssl/blob/5ea564f154ebe8bda2a0e091a312e2058edf437f/crypto/bn/bn_shift.c/#L165
int BN_rshift(BIGNUM *r, const BIGNUM *a, int n) { int i, j, nw, lb, rb; BN_ULONG *t, *f; BN_ULONG l, tmp; bn_check_top(r); bn_check_top(a); if (n < 0) { BNerr(BN_F_BN_RSHIFT, BN_R_INVALID_SHIFT); return 0; } nw = n / BN_BITS2; rb = n % BN_BITS2; lb = BN_BITS2 - rb; if (nw >= a->top || a->top == 0) { BN_zero(r); return (1); } i = (BN_num_bits(a) - n + (BN_BITS2 - 1)) / BN_BITS2; if (r != a) { if (bn_wexpand(r, i) == NULL) return (0); r->neg = a->neg; } else { if (n == 0) return 1; } f = &(a->d[nw]); t = r->d; j = a->top - nw; r->top = i; if (rb == 0) { for (i = j; i != 0; i--) *(t++) = *(f++); } else { l = *(f++); for (i = j - 1; i != 0; i--) { tmp = (l >> rb) & BN_MASK2; l = *(f++); *(t++) = (tmp | (l << lb)) & BN_MASK2; } if ((l = (l >> rb) & BN_MASK2)) *(t) = l; } if (!r->top) r->neg = 0; bn_check_top(r); return (1); }
['static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n,\n BN_CTX *ctx)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM *ret = NULL;\n int sign;\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n {\n BIGNUM local_B;\n bn_init(&local_B);\n BN_with_flags(&local_B, B, BN_FLG_CONSTTIME);\n if (!BN_nnmod(B, &local_B, A, ctx))\n goto err;\n }\n }\n sign = -1;\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n {\n BIGNUM local_A;\n bn_init(&local_A);\n BN_with_flags(&local_A, A, BN_FLG_CONSTTIME);\n if (!BN_div(D, M, &local_A, B, ctx))\n goto err;\n }\n tmp = A;\n A = B;\n B = M;\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n if (!BN_add(tmp, tmp, Y))\n goto err;\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n BNerr(BN_F_BN_MOD_INVERSE_NO_BRANCH, BN_R_NO_INVERSE);\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return (ret);\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return (1);\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'int BN_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}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'int BN_rshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, j, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l, tmp;\n bn_check_top(r);\n bn_check_top(a);\n if (n < 0) {\n BNerr(BN_F_BN_RSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n nw = n / BN_BITS2;\n rb = n % BN_BITS2;\n lb = BN_BITS2 - rb;\n if (nw >= a->top || a->top == 0) {\n BN_zero(r);\n return (1);\n }\n i = (BN_num_bits(a) - n + (BN_BITS2 - 1)) / BN_BITS2;\n if (r != a) {\n if (bn_wexpand(r, i) == NULL)\n return (0);\n r->neg = a->neg;\n } else {\n if (n == 0)\n return 1;\n }\n f = &(a->d[nw]);\n t = r->d;\n j = a->top - nw;\n r->top = i;\n if (rb == 0) {\n for (i = j; i != 0; i--)\n *(t++) = *(f++);\n } else {\n l = *(f++);\n for (i = j - 1; i != 0; i--) {\n tmp = (l >> rb) & BN_MASK2;\n l = *(f++);\n *(t++) = (tmp | (l << lb)) & BN_MASK2;\n }\n if ((l = (l >> rb) & BN_MASK2))\n *(t) = l;\n }\n if (!r->top)\n r->neg = 0;\n bn_check_top(r);\n return (1);\n}']
2,361
0
https://gitlab.com/libtiff/libtiff/blob/6dac309a9701d15ac52d895d566ddae2ed49db9b/libtiff/tif_swab.c/#L113
void TIFFSwabArrayOfLong(register uint32* lp, tmsize_t n) { register unsigned char *cp; register unsigned char t; assert(sizeof(uint32)==4); while (n-- > 0) { cp = (unsigned char *)lp; t = cp[3]; cp[3] = cp[0]; cp[0] = t; t = cp[2]; cp[2] = cp[1]; cp[1] = t; lp++; } }
['tsize_t t2p_write_pdf(T2P* t2p, TIFF* input, TIFF* output){\n\ttsize_t written=0;\n\tttile_t i2=0;\n\ttsize_t streamlen=0;\n\tuint16 i=0;\n\tt2p_read_tiff_init(t2p, input);\n\tif(t2p->t2p_error!=T2P_ERR_OK){return(0);}\n\tt2p->pdf_xrefoffsets= (uint32*) _TIFFmalloc(TIFFSafeMultiply(tmsize_t,t2p->pdf_xrefcount,sizeof(uint32)) );\n\tif(t2p->pdf_xrefoffsets==NULL){\n\t\tTIFFError(\n\t\t\tTIFF2PDF_MODULE,\n\t\t\t"Can\'t allocate %u bytes of memory for t2p_write_pdf",\n\t\t\t(unsigned int) (t2p->pdf_xrefcount * sizeof(uint32)) );\n\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\treturn(written);\n\t}\n\tt2p->pdf_xrefcount=0;\n\tt2p->pdf_catalog=1;\n\tt2p->pdf_info=2;\n\tt2p->pdf_pages=3;\n\twritten += t2p_write_pdf_header(t2p, output);\n\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\tt2p->pdf_catalog=t2p->pdf_xrefcount;\n\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\twritten += t2p_write_pdf_catalog(t2p, output);\n\twritten += t2p_write_pdf_obj_end(output);\n\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\tt2p->pdf_info=t2p->pdf_xrefcount;\n\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\twritten += t2p_write_pdf_info(t2p, input, output);\n\twritten += t2p_write_pdf_obj_end(output);\n\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\tt2p->pdf_pages=t2p->pdf_xrefcount;\n\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\twritten += t2p_write_pdf_pages(t2p, output);\n\twritten += t2p_write_pdf_obj_end(output);\n\tfor(t2p->pdf_page=0;t2p->pdf_page<t2p->tiff_pagecount;t2p->pdf_page++){\n\t\tt2p_read_tiff_data(t2p, input);\n\t\tif(t2p->t2p_error!=T2P_ERR_OK){return(0);}\n\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\twritten += t2p_write_pdf_page(t2p->pdf_xrefcount, t2p, output);\n\t\twritten += t2p_write_pdf_obj_end(output);\n\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\twritten += t2p_write_pdf_stream_dict_start(output);\n\t\twritten += t2p_write_pdf_stream_dict(0, t2p->pdf_xrefcount+1, output);\n\t\twritten += t2p_write_pdf_stream_dict_end(output);\n\t\twritten += t2p_write_pdf_stream_start(output);\n\t\tstreamlen=written;\n\t\twritten += t2p_write_pdf_page_content_stream(t2p, output);\n\t\tstreamlen=written-streamlen;\n\t\twritten += t2p_write_pdf_stream_end(output);\n\t\twritten += t2p_write_pdf_obj_end(output);\n\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\twritten += t2p_write_pdf_stream_length(streamlen, output);\n\t\twritten += t2p_write_pdf_obj_end(output);\n\t\tif(t2p->tiff_transferfunctioncount != 0){\n\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\twritten += t2p_write_pdf_transfer(t2p, output);\n\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t\tfor(i=0; i < t2p->tiff_transferfunctioncount; i++){\n\t\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\t\twritten += t2p_write_pdf_stream_dict_start(output);\n\t\t\t\twritten += t2p_write_pdf_transfer_dict(t2p, output, i);\n\t\t\t\twritten += t2p_write_pdf_stream_dict_end(output);\n\t\t\t\twritten += t2p_write_pdf_stream_start(output);\n\t\t\t\twritten += t2p_write_pdf_transfer_stream(t2p, output, i);\n\t\t\t\twritten += t2p_write_pdf_stream_end(output);\n\t\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t\t}\n\t\t}\n\t\tif( (t2p->pdf_colorspace & T2P_CS_PALETTE) != 0){\n\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\tt2p->pdf_palettecs=t2p->pdf_xrefcount;\n\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\twritten += t2p_write_pdf_stream_dict_start(output);\n\t\t\twritten += t2p_write_pdf_stream_dict(t2p->pdf_palettesize, 0, output);\n\t\t\twritten += t2p_write_pdf_stream_dict_end(output);\n\t\t\twritten += t2p_write_pdf_stream_start(output);\n\t\t\twritten += t2p_write_pdf_xobject_palettecs_stream(t2p, output);\n\t\t\twritten += t2p_write_pdf_stream_end(output);\n\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t}\n\t\tif( (t2p->pdf_colorspace & T2P_CS_ICCBASED) != 0){\n\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\tt2p->pdf_icccs=t2p->pdf_xrefcount;\n\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\twritten += t2p_write_pdf_stream_dict_start(output);\n\t\t\twritten += t2p_write_pdf_xobject_icccs_dict(t2p, output);\n\t\t\twritten += t2p_write_pdf_stream_dict_end(output);\n\t\t\twritten += t2p_write_pdf_stream_start(output);\n\t\t\twritten += t2p_write_pdf_xobject_icccs_stream(t2p, output);\n\t\t\twritten += t2p_write_pdf_stream_end(output);\n\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t}\n\t\tif(t2p->tiff_tiles[t2p->pdf_page].tiles_tilecount !=0){\n\t\t\tfor(i2=0;i2<t2p->tiff_tiles[t2p->pdf_page].tiles_tilecount;i2++){\n\t\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\t\twritten += t2p_write_pdf_stream_dict_start(output);\n\t\t\t\twritten += t2p_write_pdf_xobject_stream_dict(\n\t\t\t\t\ti2+1,\n\t\t\t\t\tt2p,\n\t\t\t\t\toutput);\n\t\t\t\twritten += t2p_write_pdf_stream_dict_end(output);\n\t\t\t\twritten += t2p_write_pdf_stream_start(output);\n\t\t\t\tstreamlen=written;\n\t\t\t\tt2p_read_tiff_size_tile(t2p, input, i2);\n\t\t\t\twritten += t2p_readwrite_pdf_image_tile(t2p, input, output, i2);\n\t\t\t\tt2p_write_advance_directory(t2p, output);\n\t\t\t\tif(t2p->t2p_error!=T2P_ERR_OK){return(0);}\n\t\t\t\tstreamlen=written-streamlen;\n\t\t\t\twritten += t2p_write_pdf_stream_end(output);\n\t\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\t\twritten += t2p_write_pdf_stream_length(streamlen, output);\n\t\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t\t}\n\t\t} else {\n\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\twritten += t2p_write_pdf_stream_dict_start(output);\n\t\t\twritten += t2p_write_pdf_xobject_stream_dict(\n\t\t\t\t0,\n\t\t\t\tt2p,\n\t\t\t\toutput);\n\t\t\twritten += t2p_write_pdf_stream_dict_end(output);\n\t\t\twritten += t2p_write_pdf_stream_start(output);\n\t\t\tstreamlen=written;\n\t\t\tt2p_read_tiff_size(t2p, input);\n\t\t\twritten += t2p_readwrite_pdf_image(t2p, input, output);\n\t\t\tt2p_write_advance_directory(t2p, output);\n\t\t\tif(t2p->t2p_error!=T2P_ERR_OK){return(0);}\n\t\t\tstreamlen=written-streamlen;\n\t\t\twritten += t2p_write_pdf_stream_end(output);\n\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t\tt2p->pdf_xrefoffsets[t2p->pdf_xrefcount++]=written;\n\t\t\twritten += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output);\n\t\t\twritten += t2p_write_pdf_stream_length(streamlen, output);\n\t\t\twritten += t2p_write_pdf_obj_end(output);\n\t\t}\n\t}\n\tt2p->pdf_startxref = written;\n\twritten += t2p_write_pdf_xreftable(t2p, output);\n\twritten += t2p_write_pdf_trailer(t2p, output);\n\tt2p_disable(output);\n\treturn(written);\n}', 'tsize_t t2p_readwrite_pdf_image_tile(T2P* t2p, TIFF* input, TIFF* output, ttile_t tile){\n\tuint16 edge=0;\n\ttsize_t written=0;\n\tunsigned char* buffer=NULL;\n\ttsize_t bufferoffset=0;\n\tunsigned char* samplebuffer=NULL;\n\ttsize_t samplebufferoffset=0;\n\ttsize_t read=0;\n\tuint16 i=0;\n\tttile_t tilecount=0;\n\tttile_t septilecount=0;\n\ttsize_t septilesize=0;\n#ifdef JPEG_SUPPORT\n\tunsigned char* jpt;\n\tfloat* xfloatp;\n\tuint32 xuint32=0;\n#endif\n\tif (t2p->t2p_error != T2P_ERR_OK)\n\t\treturn(0);\n\tedge |= t2p_tile_is_right_edge(t2p->tiff_tiles[t2p->pdf_page], tile);\n\tedge |= t2p_tile_is_bottom_edge(t2p->tiff_tiles[t2p->pdf_page], tile);\n\tif( (t2p->pdf_transcode == T2P_TRANSCODE_RAW) && ((edge == 0)\n#if defined(JPEG_SUPPORT) || defined(OJPEG_SUPPORT)\n\t\t|| (t2p->pdf_compression == T2P_COMPRESS_JPEG)\n#endif\n\t)\n\t){\n#ifdef CCITT_SUPPORT\n\t\tif(t2p->pdf_compression == T2P_COMPRESS_G4){\n\t\t\tbuffer= (unsigned char*) _TIFFmalloc(t2p->tiff_datasize);\n\t\t\tif(buffer==NULL){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Can\'t allocate %lu bytes of memory "\n "for t2p_readwrite_pdf_image_tile, %s",\n\t\t\t\t\t(unsigned long) t2p->tiff_datasize,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn(0);\n\t\t\t}\n\t\t\tTIFFReadRawTile(input, tile, (tdata_t) buffer, t2p->tiff_datasize);\n\t\t\tif (t2p->tiff_fillorder==FILLORDER_LSB2MSB){\n\t\t\t\t\tTIFFReverseBits(buffer, t2p->tiff_datasize);\n\t\t\t}\n\t\t\tt2pWriteFile(output, (tdata_t) buffer, t2p->tiff_datasize);\n\t\t\t_TIFFfree(buffer);\n\t\t\treturn(t2p->tiff_datasize);\n\t\t}\n#endif\n#ifdef ZIP_SUPPORT\n\t\tif(t2p->pdf_compression == T2P_COMPRESS_ZIP){\n\t\t\tbuffer= (unsigned char*) _TIFFmalloc(t2p->tiff_datasize);\n\t\t\tif(buffer==NULL){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Can\'t allocate %lu bytes of memory "\n "for t2p_readwrite_pdf_image_tile, %s",\n\t\t\t\t\t(unsigned long) t2p->tiff_datasize,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn(0);\n\t\t\t}\n\t\t\tTIFFReadRawTile(input, tile, (tdata_t) buffer, t2p->tiff_datasize);\n\t\t\tif (t2p->tiff_fillorder==FILLORDER_LSB2MSB){\n\t\t\t\t\tTIFFReverseBits(buffer, t2p->tiff_datasize);\n\t\t\t}\n\t\t\tt2pWriteFile(output, (tdata_t) buffer, t2p->tiff_datasize);\n\t\t\t_TIFFfree(buffer);\n\t\t\treturn(t2p->tiff_datasize);\n\t\t}\n#endif\n#ifdef OJPEG_SUPPORT\n\t\tif(t2p->tiff_compression == COMPRESSION_OJPEG){\n\t\t\tif(! t2p->pdf_ojpegdata){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"No support for OJPEG image %s with "\n "bad tables",\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn(0);\n\t\t\t}\n\t\t\tbuffer=(unsigned char*) _TIFFmalloc(t2p->tiff_datasize);\n\t\t\tif(buffer==NULL){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Can\'t allocate %lu bytes of memory "\n "for t2p_readwrite_pdf_image, %s",\n\t\t\t\t\t(unsigned long) t2p->tiff_datasize,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn(0);\n\t\t\t}\n\t\t\t_TIFFmemcpy(buffer, t2p->pdf_ojpegdata, t2p->pdf_ojpegdatalength);\n\t\t\tif(edge!=0){\n\t\t\t\tif(t2p_tile_is_bottom_edge(t2p->tiff_tiles[t2p->pdf_page], tile)){\n\t\t\t\t\tbuffer[7]=\n\t\t\t\t\t\t(t2p->tiff_tiles[t2p->pdf_page].tiles_edgetilelength >> 8) & 0xff;\n\t\t\t\t\tbuffer[8]=\n\t\t\t\t\t\t(t2p->tiff_tiles[t2p->pdf_page].tiles_edgetilelength ) & 0xff;\n\t\t\t\t}\n\t\t\t\tif(t2p_tile_is_right_edge(t2p->tiff_tiles[t2p->pdf_page], tile)){\n\t\t\t\t\tbuffer[9]=\n\t\t\t\t\t\t(t2p->tiff_tiles[t2p->pdf_page].tiles_edgetilewidth >> 8) & 0xff;\n\t\t\t\t\tbuffer[10]=\n\t\t\t\t\t\t(t2p->tiff_tiles[t2p->pdf_page].tiles_edgetilewidth ) & 0xff;\n\t\t\t\t}\n\t\t\t}\n\t\t\tbufferoffset=t2p->pdf_ojpegdatalength;\n\t\t\tbufferoffset+=TIFFReadRawTile(input,\n\t\t\t\t\ttile,\n\t\t\t\t\t(tdata_t) &(((unsigned char*)buffer)[bufferoffset]),\n\t\t\t\t\t-1);\n\t\t\t((unsigned char*)buffer)[bufferoffset++]=0xff;\n\t\t\t((unsigned char*)buffer)[bufferoffset++]=0xd9;\n\t\t\tt2pWriteFile(output, (tdata_t) buffer, bufferoffset);\n\t\t\t_TIFFfree(buffer);\n\t\t\treturn(bufferoffset);\n\t\t}\n#endif\n#ifdef JPEG_SUPPORT\n\t\tif(t2p->tiff_compression == COMPRESSION_JPEG){\n\t\t\tunsigned char table_end[2];\n\t\t\tuint32 count = 0;\n\t\t\tbuffer= (unsigned char*) _TIFFmalloc(t2p->tiff_datasize);\n\t\t\tif(buffer==NULL){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Can\'t allocate " TIFF_SIZE_FORMAT " bytes of memory "\n "for t2p_readwrite_pdf_image_tile, %s",\n (TIFF_SIZE_T) t2p->tiff_datasize,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn(0);\n\t\t\t}\n\t\t\tif(TIFFGetField(input, TIFFTAG_JPEGTABLES, &count, &jpt) != 0) {\n\t\t\t\tif (count > 4) {\n int retTIFFReadRawTile;\n\t\t\t\t\t_TIFFmemcpy(buffer, jpt, count - 2);\n\t\t\t\t\tbufferoffset += count - 2;\n\t\t\t\t\ttable_end[0] = buffer[bufferoffset-2];\n\t\t\t\t\ttable_end[1] = buffer[bufferoffset-1];\n\t\t\t\t\txuint32 = bufferoffset;\n bufferoffset -= 2;\n retTIFFReadRawTile= TIFFReadRawTile(\n\t\t\t\t\t\tinput,\n\t\t\t\t\t\ttile,\n\t\t\t\t\t\t(tdata_t) &(((unsigned char*)buffer)[bufferoffset]),\n\t\t\t\t\t\t-1);\n if( retTIFFReadRawTile < 0 )\n {\n _TIFFfree(buffer);\n t2p->t2p_error = T2P_ERR_ERROR;\n return(0);\n }\n\t\t\t\t\tbufferoffset += retTIFFReadRawTile;\n\t\t\t\t\tbuffer[xuint32-2]=table_end[0];\n\t\t\t\t\tbuffer[xuint32-1]=table_end[1];\n\t\t\t\t}\n\t\t\t}\n\t\t\tt2pWriteFile(output, (tdata_t) buffer, bufferoffset);\n\t\t\t_TIFFfree(buffer);\n\t\t\treturn(bufferoffset);\n\t\t}\n#endif\n\t\t(void)0;\n\t}\n\tif(t2p->pdf_sample==T2P_SAMPLE_NOTHING){\n\t\tbuffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize);\n\t\tif(buffer==NULL){\n\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t"Can\'t allocate %lu bytes of memory for "\n "t2p_readwrite_pdf_image_tile, %s",\n\t\t\t\t(unsigned long) t2p->tiff_datasize,\n\t\t\t\tTIFFFileName(input));\n\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\treturn(0);\n\t\t}\n\t\tread = TIFFReadEncodedTile(\n\t\t\tinput,\n\t\t\ttile,\n\t\t\t(tdata_t) &buffer[bufferoffset],\n\t\t\tt2p->tiff_datasize);\n\t\tif(read==-1){\n\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t"Error on decoding tile %u of %s",\n\t\t\t\ttile,\n\t\t\t\tTIFFFileName(input));\n\t\t\t_TIFFfree(buffer);\n\t\t\tt2p->t2p_error=T2P_ERR_ERROR;\n\t\t\treturn(0);\n\t\t}\n\t} else {\n\t\tif(t2p->pdf_sample == T2P_SAMPLE_PLANAR_SEPARATE_TO_CONTIG){\n\t\t\tseptilesize=TIFFTileSize(input);\n\t\t\tseptilecount=TIFFNumberOfTiles(input);\n\t\t\ttilecount=septilecount/t2p->tiff_samplesperpixel;\n\t\t\tbuffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize);\n\t\t\tif(buffer==NULL){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Can\'t allocate %lu bytes of memory "\n "for t2p_readwrite_pdf_image_tile, %s",\n\t\t\t\t\t(unsigned long) t2p->tiff_datasize,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn(0);\n\t\t\t}\n\t\t\tsamplebuffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize);\n\t\t\tif(samplebuffer==NULL){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Can\'t allocate %lu bytes of memory "\n "for t2p_readwrite_pdf_image_tile, %s",\n\t\t\t\t\t(unsigned long) t2p->tiff_datasize,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn(0);\n\t\t\t}\n\t\t\tsamplebufferoffset=0;\n\t\t\tfor(i=0;i<t2p->tiff_samplesperpixel;i++){\n\t\t\t\tread =\n\t\t\t\t\tTIFFReadEncodedTile(input,\n\t\t\t\t\t\ttile + i*tilecount,\n\t\t\t\t\t\t(tdata_t) &(samplebuffer[samplebufferoffset]),\n\t\t\t\t\t\tseptilesize);\n\t\t\t\tif(read==-1){\n\t\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t\t"Error on decoding tile %u of %s",\n\t\t\t\t\t\ttile + i*tilecount,\n\t\t\t\t\t\tTIFFFileName(input));\n\t\t\t\t\t\t_TIFFfree(samplebuffer);\n\t\t\t\t\t\t_TIFFfree(buffer);\n\t\t\t\t\tt2p->t2p_error=T2P_ERR_ERROR;\n\t\t\t\t\treturn(0);\n\t\t\t\t}\n\t\t\t\tsamplebufferoffset+=read;\n\t\t\t}\n\t\t\tt2p_sample_planar_separate_to_contig(\n\t\t\t\tt2p,\n\t\t\t\t&(buffer[bufferoffset]),\n\t\t\t\tsamplebuffer,\n\t\t\t\tsamplebufferoffset);\n\t\t\tbufferoffset+=samplebufferoffset;\n\t\t\t_TIFFfree(samplebuffer);\n\t\t}\n\t\tif(buffer==NULL){\n\t\t\tbuffer = (unsigned char*) _TIFFmalloc(t2p->tiff_datasize);\n\t\t\tif(buffer==NULL){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Can\'t allocate %lu bytes of memory "\n "for t2p_readwrite_pdf_image_tile, %s",\n\t\t\t\t\t(unsigned long) t2p->tiff_datasize,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn(0);\n\t\t\t}\n\t\t\tread = TIFFReadEncodedTile(\n\t\t\t\tinput,\n\t\t\t\ttile,\n\t\t\t\t(tdata_t) &buffer[bufferoffset],\n\t\t\t\tt2p->tiff_datasize);\n\t\t\tif(read==-1){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Error on decoding tile %u of %s",\n\t\t\t\t\ttile,\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\t_TIFFfree(buffer);\n\t\t\t\tt2p->t2p_error=T2P_ERR_ERROR;\n\t\t\t\treturn(0);\n\t\t\t}\n\t\t}\n\t\tif(t2p->pdf_sample & T2P_SAMPLE_RGBA_TO_RGB){\n\t\t\tt2p->tiff_datasize=t2p_sample_rgba_to_rgb(\n\t\t\t\t(tdata_t)buffer,\n\t\t\t\tt2p->tiff_tiles[t2p->pdf_page].tiles_tilewidth\n\t\t\t\t*t2p->tiff_tiles[t2p->pdf_page].tiles_tilelength);\n\t\t}\n\t\tif(t2p->pdf_sample & T2P_SAMPLE_RGBAA_TO_RGB){\n\t\t\tt2p->tiff_datasize=t2p_sample_rgbaa_to_rgb(\n\t\t\t\t(tdata_t)buffer,\n\t\t\t\tt2p->tiff_tiles[t2p->pdf_page].tiles_tilewidth\n\t\t\t\t*t2p->tiff_tiles[t2p->pdf_page].tiles_tilelength);\n\t\t}\n\t\tif(t2p->pdf_sample & T2P_SAMPLE_YCBCR_TO_RGB){\n\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t"No support for YCbCr to RGB in tile for %s",\n\t\t\t\tTIFFFileName(input));\n\t\t\t_TIFFfree(buffer);\n\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\treturn(0);\n\t\t}\n\t\tif(t2p->pdf_sample & T2P_SAMPLE_LAB_SIGNED_TO_UNSIGNED){\n\t\t\tt2p->tiff_datasize=t2p_sample_lab_signed_to_unsigned(\n\t\t\t\t(tdata_t)buffer,\n\t\t\t\tt2p->tiff_tiles[t2p->pdf_page].tiles_tilewidth\n\t\t\t\t*t2p->tiff_tiles[t2p->pdf_page].tiles_tilelength);\n\t\t}\n\t}\n\tif(t2p_tile_is_right_edge(t2p->tiff_tiles[t2p->pdf_page], tile) != 0){\n\t\tt2p_tile_collapse_left(\n\t\t\tbuffer,\n\t\t\tTIFFTileRowSize(input),\n\t\t\tt2p->tiff_tiles[t2p->pdf_page].tiles_tilewidth,\n\t\t\tt2p->tiff_tiles[t2p->pdf_page].tiles_edgetilewidth,\n\t\t\tt2p->tiff_tiles[t2p->pdf_page].tiles_tilelength);\n\t}\n\tt2p_disable(output);\n\tTIFFSetField(output, TIFFTAG_PHOTOMETRIC, t2p->tiff_photometric);\n\tTIFFSetField(output, TIFFTAG_BITSPERSAMPLE, t2p->tiff_bitspersample);\n\tTIFFSetField(output, TIFFTAG_SAMPLESPERPIXEL, t2p->tiff_samplesperpixel);\n\tif(t2p_tile_is_right_edge(t2p->tiff_tiles[t2p->pdf_page], tile) == 0){\n\t\tTIFFSetField(\n\t\t\toutput,\n\t\t\tTIFFTAG_IMAGEWIDTH,\n\t\t\tt2p->tiff_tiles[t2p->pdf_page].tiles_tilewidth);\n\t} else {\n\t\tTIFFSetField(\n\t\t\toutput,\n\t\t\tTIFFTAG_IMAGEWIDTH,\n\t\t\tt2p->tiff_tiles[t2p->pdf_page].tiles_edgetilewidth);\n\t}\n\tif(t2p_tile_is_bottom_edge(t2p->tiff_tiles[t2p->pdf_page], tile) == 0){\n\t\tTIFFSetField(\n\t\t\toutput,\n\t\t\tTIFFTAG_IMAGELENGTH,\n\t\t\tt2p->tiff_tiles[t2p->pdf_page].tiles_tilelength);\n\t\tTIFFSetField(\n\t\t\toutput,\n\t\t\tTIFFTAG_ROWSPERSTRIP,\n\t\t\tt2p->tiff_tiles[t2p->pdf_page].tiles_tilelength);\n\t} else {\n\t\tTIFFSetField(\n\t\t\toutput,\n\t\t\tTIFFTAG_IMAGELENGTH,\n\t\t\tt2p->tiff_tiles[t2p->pdf_page].tiles_edgetilelength);\n\t\tTIFFSetField(\n\t\t\toutput,\n\t\t\tTIFFTAG_ROWSPERSTRIP,\n\t\t\tt2p->tiff_tiles[t2p->pdf_page].tiles_edgetilelength);\n\t}\n\tTIFFSetField(output, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);\n\tTIFFSetField(output, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);\n\tswitch(t2p->pdf_compression){\n\tcase T2P_COMPRESS_NONE:\n\t\tTIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_NONE);\n\t\tbreak;\n#ifdef CCITT_SUPPORT\n\tcase T2P_COMPRESS_G4:\n\t\tTIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4);\n\t\tbreak;\n#endif\n#ifdef JPEG_SUPPORT\n\tcase T2P_COMPRESS_JPEG:\n\t\tif (t2p->tiff_photometric==PHOTOMETRIC_YCBCR) {\n\t\t\tuint16 hor = 0, ver = 0;\n\t\t\tif (TIFFGetField(input, TIFFTAG_YCBCRSUBSAMPLING, &hor, &ver)!=0) {\n\t\t\t\tif (hor != 0 && ver != 0) {\n\t\t\t\t\tTIFFSetField(output, TIFFTAG_YCBCRSUBSAMPLING, hor, ver);\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(TIFFGetField(input, TIFFTAG_REFERENCEBLACKWHITE, &xfloatp)!=0){\n\t\t\t\tTIFFSetField(output, TIFFTAG_REFERENCEBLACKWHITE, xfloatp);\n\t\t\t}\n\t\t}\n\t\tTIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_JPEG);\n\t\tTIFFSetField(output, TIFFTAG_JPEGTABLESMODE, 0);\n\t\tif(t2p->pdf_colorspace & (T2P_CS_RGB | T2P_CS_LAB)){\n\t\t\tTIFFSetField(output, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR);\n\t\t\tif(t2p->tiff_photometric != PHOTOMETRIC_YCBCR){\n\t\t\t\tTIFFSetField(output, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);\n\t\t\t} else {\n\t\t\t\tTIFFSetField(output, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RAW);\n\t\t\t}\n\t\t}\n\t\tif(t2p->pdf_colorspace & T2P_CS_GRAY){\n\t\t\t(void)0;\n\t\t}\n\t\tif(t2p->pdf_colorspace & T2P_CS_CMYK){\n\t\t\t(void)0;\n\t\t}\n\t\tif(t2p->pdf_defaultcompressionquality != 0){\n\t\t\tTIFFSetField(output,\n\t\t\t\tTIFFTAG_JPEGQUALITY,\n\t\t\t\tt2p->pdf_defaultcompressionquality);\n\t\t}\n\t\tbreak;\n#endif\n#ifdef ZIP_SUPPORT\n\tcase T2P_COMPRESS_ZIP:\n\t\tTIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE);\n\t\tif(t2p->pdf_defaultcompressionquality%100 != 0){\n\t\t\tTIFFSetField(output,\n\t\t\t\tTIFFTAG_PREDICTOR,\n\t\t\t\tt2p->pdf_defaultcompressionquality % 100);\n\t\t}\n\t\tif(t2p->pdf_defaultcompressionquality/100 != 0){\n\t\t\tTIFFSetField(output,\n\t\t\t\tTIFFTAG_ZIPQUALITY,\n\t\t\t\t(t2p->pdf_defaultcompressionquality / 100));\n\t\t}\n\t\tbreak;\n#endif\n\tdefault:\n\t\tbreak;\n\t}\n\tt2p_enable(output);\n\tt2p->outputwritten = 0;\n\tbufferoffset = TIFFWriteEncodedStrip(output, (tstrip_t) 0, buffer,\n\t\t\t\t\t TIFFStripSize(output));\n\tif (buffer != NULL) {\n\t\t_TIFFfree(buffer);\n\t\tbuffer = NULL;\n\t}\n\tif (bufferoffset == -1) {\n\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t "Error writing encoded tile to output PDF %s",\n\t\t\t TIFFFileName(output));\n\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\treturn(0);\n\t}\n\twritten = t2p->outputwritten;\n\treturn(written);\n}', 'void\nt2p_write_advance_directory(T2P* t2p, TIFF* output)\n{\n\tt2p_disable(output);\n\tif(!TIFFWriteDirectory(output)){\n\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t"Error writing virtual directory to output PDF %s",\n\t\t\tTIFFFileName(output));\n\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\treturn;\n\t}\n\tt2p_enable(output);\n\treturn;\n}', 'int\nTIFFWriteDirectory(TIFF* tif)\n{\n\treturn TIFFWriteDirectorySec(tif,TRUE,TRUE,NULL);\n}', 'static int\nTIFFWriteDirectorySec(TIFF* tif, int isimage, int imagedone, uint64* pdiroff)\n{\n\tstatic const char module[] = "TIFFWriteDirectorySec";\n\tuint32 ndir;\n\tTIFFDirEntry* dir;\n\tuint32 dirsize;\n\tvoid* dirmem;\n\tuint32 m;\n\tif (tif->tif_mode == O_RDONLY)\n\t\treturn (1);\n _TIFFFillStriles( tif );\n\tif (imagedone)\n\t{\n\t\tif (tif->tif_flags & TIFF_POSTENCODE)\n\t\t{\n\t\t\ttif->tif_flags &= ~TIFF_POSTENCODE;\n\t\t\tif (!(*tif->tif_postencode)(tif))\n\t\t\t{\n\t\t\t\tTIFFErrorExt(tif->tif_clientdata,module,\n\t\t\t\t "Error post-encoding before directory write");\n\t\t\t\treturn (0);\n\t\t\t}\n\t\t}\n\t\t(*tif->tif_close)(tif);\n\t\tif (tif->tif_rawcc > 0\n\t\t && (tif->tif_flags & TIFF_BEENWRITING) != 0 )\n\t\t{\n\t\t if( !TIFFFlushData1(tif) )\n {\n\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t "Error flushing data before directory write");\n\t\t\treturn (0);\n }\n\t\t}\n\t\tif ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)\n\t\t{\n\t\t\t_TIFFfree(tif->tif_rawdata);\n\t\t\ttif->tif_rawdata = NULL;\n\t\t\ttif->tif_rawcc = 0;\n\t\t\ttif->tif_rawdatasize = 0;\n tif->tif_rawdataoff = 0;\n tif->tif_rawdataloaded = 0;\n\t\t}\n\t\ttif->tif_flags &= ~(TIFF_BEENWRITING|TIFF_BUFFERSETUP);\n\t}\n\tdir=NULL;\n\tdirmem=NULL;\n\tdirsize=0;\n\twhile (1)\n\t{\n\t\tndir=0;\n\t\tif (isimage)\n\t\t{\n\t\t\tif (TIFFFieldSet(tif,FIELD_IMAGEDIMENSIONS))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_IMAGEWIDTH,tif->tif_dir.td_imagewidth))\n\t\t\t\t\tgoto bad;\n\t\t\t\tif (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_IMAGELENGTH,tif->tif_dir.td_imagelength))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_TILEDIMENSIONS))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_TILEWIDTH,tif->tif_dir.td_tilewidth))\n\t\t\t\t\tgoto bad;\n\t\t\t\tif (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_TILELENGTH,tif->tif_dir.td_tilelength))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_RESOLUTION))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_XRESOLUTION,tif->tif_dir.td_xresolution))\n\t\t\t\t\tgoto bad;\n\t\t\t\tif (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_YRESOLUTION,tif->tif_dir.td_yresolution))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_POSITION))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_XPOSITION,tif->tif_dir.td_xposition))\n\t\t\t\t\tgoto bad;\n\t\t\t\tif (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_YPOSITION,tif->tif_dir.td_yposition))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_SUBFILETYPE))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,TIFFTAG_SUBFILETYPE,tif->tif_dir.td_subfiletype))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_BITSPERSAMPLE,tif->tif_dir.td_bitspersample))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_COMPRESSION))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_COMPRESSION,tif->tif_dir.td_compression))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_PHOTOMETRIC))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_PHOTOMETRIC,tif->tif_dir.td_photometric))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_THRESHHOLDING))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_THRESHHOLDING,tif->tif_dir.td_threshholding))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_FILLORDER))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_FILLORDER,tif->tif_dir.td_fillorder))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_ORIENTATION))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_ORIENTATION,tif->tif_dir.td_orientation))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_SAMPLESPERPIXEL))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_SAMPLESPERPIXEL,tif->tif_dir.td_samplesperpixel))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_ROWSPERSTRIP))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_ROWSPERSTRIP,tif->tif_dir.td_rowsperstrip))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_MINSAMPLEVALUE))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_MINSAMPLEVALUE,tif->tif_dir.td_minsamplevalue))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_MAXSAMPLEVALUE))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_MAXSAMPLEVALUE,tif->tif_dir.td_maxsamplevalue))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_PLANARCONFIG))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_PLANARCONFIG,tif->tif_dir.td_planarconfig))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_RESOLUTIONUNIT))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_RESOLUTIONUNIT,tif->tif_dir.td_resolutionunit))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_PAGENUMBER))\n\t\t\t{\n\t\t\t\tif (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_PAGENUMBER,2,&tif->tif_dir.td_pagenumber[0]))\n\t\t\t\t\tgoto bad;\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_STRIPBYTECOUNTS))\n\t\t\t{\n\t\t\t\tif (!isTiled(tif))\n\t\t\t\t{\n\t\t\t\t\tif (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_STRIPBYTECOUNTS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripbytecount))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tif (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_TILEBYTECOUNTS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripbytecount))\n\t\t\t\t\t\tgoto bad;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (TIFFFieldSet(tif,FIELD_STRIPOFFSETS))\n\t\t\t{\n\t\t\t\tif (!isTiled(tif))\n\t\t\t\t{\n if (tif->tif_dir.td_stripoffset != NULL &&\n !TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_STRIPOFFSETS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripoffset))\n goto 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,(uint16)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,(uint16)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,(uint16)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,(uint16)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 uint16 tag = (uint16)tif->tif_dir.td_customValues[m].info->field_tag;\n uint32 count = tif->tif_dir.td_customValues[m].count;\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,tag,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,tag,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,tag,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,tag,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,tag,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,tag,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,tag,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,tag,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,tag,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,tag,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,tag,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,tag,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,tag,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,tag,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,tag,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,tag,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)&(~((toff_t)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\tif( na == ndir )\n {\n TIFFErrorExt(tif->tif_clientdata,module,\n "Cannot find SubIFD tag");\n goto bad;\n }\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=(uint16)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\nTIFFWriteDirectoryTagSampleformatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value)\n{\n\tstatic const char module[] = "TIFFWriteDirectoryTagSampleformatArray";\n\tvoid* conv;\n\tuint32 i;\n\tint ok;\n\tconv = _TIFFmalloc(count*sizeof(double));\n\tif (conv == NULL)\n\t{\n\t\tTIFFErrorExt(tif->tif_clientdata, module, "Out of memory");\n\t\treturn (0);\n\t}\n\tswitch (tif->tif_dir.td_sampleformat)\n\t{\n\t\tcase SAMPLEFORMAT_IEEEFP:\n\t\t\tif (tif->tif_dir.td_bitspersample<=32)\n\t\t\t{\n\t\t\t\tfor (i = 0; i < count; ++i)\n\t\t\t\t\t((float*)conv)[i] = TIFFClampDoubleToFloat(value[i]);\n\t\t\t\tok = TIFFWriteDirectoryTagFloatArray(tif,ndir,dir,tag,count,(float*)conv);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tok = TIFFWriteDirectoryTagDoubleArray(tif,ndir,dir,tag,count,value);\n\t\t\t}\n\t\t\tbreak;\n\t\tcase SAMPLEFORMAT_INT:\n\t\t\tif (tif->tif_dir.td_bitspersample<=8)\n\t\t\t{\n\t\t\t\tfor (i = 0; i < count; ++i)\n\t\t\t\t\t((int8*)conv)[i] = TIFFClampDoubleToInt8(value[i]);\n\t\t\t\tok = TIFFWriteDirectoryTagSbyteArray(tif,ndir,dir,tag,count,(int8*)conv);\n\t\t\t}\n\t\t\telse if (tif->tif_dir.td_bitspersample<=16)\n\t\t\t{\n\t\t\t\tfor (i = 0; i < count; ++i)\n\t\t\t\t\t((int16*)conv)[i] = TIFFClampDoubleToInt16(value[i]);\n\t\t\t\tok = TIFFWriteDirectoryTagSshortArray(tif,ndir,dir,tag,count,(int16*)conv);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tfor (i = 0; i < count; ++i)\n\t\t\t\t\t((int32*)conv)[i] = TIFFClampDoubleToInt32(value[i]);\n\t\t\t\tok = TIFFWriteDirectoryTagSlongArray(tif,ndir,dir,tag,count,(int32*)conv);\n\t\t\t}\n\t\t\tbreak;\n\t\tcase SAMPLEFORMAT_UINT:\n\t\t\tif (tif->tif_dir.td_bitspersample<=8)\n\t\t\t{\n\t\t\t\tfor (i = 0; i < count; ++i)\n\t\t\t\t\t((uint8*)conv)[i] = TIFFClampDoubleToUInt8(value[i]);\n\t\t\t\tok = TIFFWriteDirectoryTagByteArray(tif,ndir,dir,tag,count,(uint8*)conv);\n\t\t\t}\n\t\t\telse if (tif->tif_dir.td_bitspersample<=16)\n\t\t\t{\n\t\t\t\tfor (i = 0; i < count; ++i)\n\t\t\t\t\t((uint16*)conv)[i] = TIFFClampDoubleToUInt16(value[i]);\n\t\t\t\tok = TIFFWriteDirectoryTagShortArray(tif,ndir,dir,tag,count,(uint16*)conv);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tfor (i = 0; i < count; ++i)\n\t\t\t\t\t((uint32*)conv)[i] = TIFFClampDoubleToUInt32(value[i]);\n\t\t\t\tok = TIFFWriteDirectoryTagLongArray(tif,ndir,dir,tag,count,(uint32*)conv);\n\t\t\t}\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tok = 0;\n\t}\n\t_TIFFfree(conv);\n\treturn (ok);\n}', 'void*\n_TIFFmalloc(tmsize_t s)\n{\n if (s == 0)\n return ((void *) NULL);\n\treturn (malloc((size_t) s));\n}', 'static int\nTIFFWriteDirectoryTagSlongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int32* value)\n{\n\tif (dir==NULL)\n\t{\n\t\t(*ndir)++;\n\t\treturn(1);\n\t}\n\treturn(TIFFWriteDirectoryTagCheckedSlongArray(tif,ndir,dir,tag,count,value));\n}', 'static int\nTIFFWriteDirectoryTagCheckedSlongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int32* value)\n{\n\tassert(count<0x40000000);\n\tassert(sizeof(int32)==4);\n\tif (tif->tif_flags&TIFF_SWAB)\n\t\tTIFFSwabArrayOfLong((uint32*)value,count);\n\treturn(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SLONG,count,count*4,value));\n}', 'void\nTIFFSwabArrayOfLong(register uint32* lp, tmsize_t n)\n{\n\tregister unsigned char *cp;\n\tregister unsigned char t;\n\tassert(sizeof(uint32)==4);\n\twhile (n-- > 0) {\n\t\tcp = (unsigned char *)lp;\n\t\tt = cp[3]; cp[3] = cp[0]; cp[0] = t;\n\t\tt = cp[2]; cp[2] = cp[1]; cp[1] = t;\n\t\tlp++;\n\t}\n}']
2,362
0
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/pem/pem_lib.c/#L721
int PEM_read_bio(BIO *bp, char **name, char **header, unsigned char **data, long *len) { EVP_ENCODE_CTX *ctx = EVP_ENCODE_CTX_new(); int end = 0, i, k, bl = 0, hl = 0, nohead = 0; char buf[256]; BUF_MEM *nameB; BUF_MEM *headerB; BUF_MEM *dataB, *tmpB; if (ctx == NULL) { PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE); return (0); } nameB = BUF_MEM_new(); headerB = BUF_MEM_new(); dataB = BUF_MEM_new(); if ((nameB == NULL) || (headerB == NULL) || (dataB == NULL)) { goto err; } buf[254] = '\0'; for (;;) { i = BIO_gets(bp, buf, 254); if (i <= 0) { PEMerr(PEM_F_PEM_READ_BIO, PEM_R_NO_START_LINE); goto err; } while ((i >= 0) && (buf[i] <= ' ')) i--; buf[++i] = '\n'; buf[++i] = '\0'; if (strncmp(buf, "-----BEGIN ", 11) == 0) { i = strlen(&(buf[11])); if (strncmp(&(buf[11 + i - 6]), "-----\n", 6) != 0) continue; if (!BUF_MEM_grow(nameB, i + 9)) { PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE); goto err; } memcpy(nameB->data, &(buf[11]), i - 6); nameB->data[i - 6] = '\0'; break; } } hl = 0; if (!BUF_MEM_grow(headerB, 256)) { PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE); goto err; } headerB->data[0] = '\0'; for (;;) { i = BIO_gets(bp, buf, 254); if (i <= 0) break; while ((i >= 0) && (buf[i] <= ' ')) i--; buf[++i] = '\n'; buf[++i] = '\0'; if (buf[0] == '\n') break; if (!BUF_MEM_grow(headerB, hl + i + 9)) { PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE); goto err; } if (strncmp(buf, "-----END ", 9) == 0) { nohead = 1; break; } memcpy(&(headerB->data[hl]), buf, i); headerB->data[hl + i] = '\0'; hl += i; } bl = 0; if (!BUF_MEM_grow(dataB, 1024)) { PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE); goto err; } dataB->data[0] = '\0'; if (!nohead) { for (;;) { i = BIO_gets(bp, buf, 254); if (i <= 0) break; while ((i >= 0) && (buf[i] <= ' ')) i--; buf[++i] = '\n'; buf[++i] = '\0'; if (i != 65) end = 1; if (strncmp(buf, "-----END ", 9) == 0) break; if (i > 65) break; if (!BUF_MEM_grow_clean(dataB, i + bl + 9)) { PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE); goto err; } memcpy(&(dataB->data[bl]), buf, i); dataB->data[bl + i] = '\0'; bl += i; if (end) { buf[0] = '\0'; i = BIO_gets(bp, buf, 254); if (i <= 0) break; while ((i >= 0) && (buf[i] <= ' ')) i--; buf[++i] = '\n'; buf[++i] = '\0'; break; } } } else { tmpB = headerB; headerB = dataB; dataB = tmpB; bl = hl; } i = strlen(nameB->data); if ((strncmp(buf, "-----END ", 9) != 0) || (strncmp(nameB->data, &(buf[9]), i) != 0) || (strncmp(&(buf[9 + i]), "-----\n", 6) != 0)) { PEMerr(PEM_F_PEM_READ_BIO, PEM_R_BAD_END_LINE); goto err; } EVP_DecodeInit(ctx); i = EVP_DecodeUpdate(ctx, (unsigned char *)dataB->data, &bl, (unsigned char *)dataB->data, bl); if (i < 0) { PEMerr(PEM_F_PEM_READ_BIO, PEM_R_BAD_BASE64_DECODE); goto err; } i = EVP_DecodeFinal(ctx, (unsigned char *)&(dataB->data[bl]), &k); if (i < 0) { PEMerr(PEM_F_PEM_READ_BIO, PEM_R_BAD_BASE64_DECODE); goto err; } bl += k; if (bl == 0) goto err; *name = nameB->data; *header = headerB->data; *data = (unsigned char *)dataB->data; *len = bl; OPENSSL_free(nameB); OPENSSL_free(headerB); OPENSSL_free(dataB); EVP_ENCODE_CTX_free(ctx); return (1); err: BUF_MEM_free(nameB); BUF_MEM_free(headerB); BUF_MEM_free(dataB); EVP_ENCODE_CTX_free(ctx); return (0); }
['int PEM_read_bio(BIO *bp, char **name, char **header, unsigned char **data,\n long *len)\n{\n EVP_ENCODE_CTX *ctx = EVP_ENCODE_CTX_new();\n int end = 0, i, k, bl = 0, hl = 0, nohead = 0;\n char buf[256];\n BUF_MEM *nameB;\n BUF_MEM *headerB;\n BUF_MEM *dataB, *tmpB;\n if (ctx == NULL) {\n PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE);\n return (0);\n }\n nameB = BUF_MEM_new();\n headerB = BUF_MEM_new();\n dataB = BUF_MEM_new();\n if ((nameB == NULL) || (headerB == NULL) || (dataB == NULL)) {\n goto err;\n }\n buf[254] = \'\\0\';\n for (;;) {\n i = BIO_gets(bp, buf, 254);\n if (i <= 0) {\n PEMerr(PEM_F_PEM_READ_BIO, PEM_R_NO_START_LINE);\n goto err;\n }\n while ((i >= 0) && (buf[i] <= \' \'))\n i--;\n buf[++i] = \'\\n\';\n buf[++i] = \'\\0\';\n if (strncmp(buf, "-----BEGIN ", 11) == 0) {\n i = strlen(&(buf[11]));\n if (strncmp(&(buf[11 + i - 6]), "-----\\n", 6) != 0)\n continue;\n if (!BUF_MEM_grow(nameB, i + 9)) {\n PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n memcpy(nameB->data, &(buf[11]), i - 6);\n nameB->data[i - 6] = \'\\0\';\n break;\n }\n }\n hl = 0;\n if (!BUF_MEM_grow(headerB, 256)) {\n PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n headerB->data[0] = \'\\0\';\n for (;;) {\n i = BIO_gets(bp, buf, 254);\n if (i <= 0)\n break;\n while ((i >= 0) && (buf[i] <= \' \'))\n i--;\n buf[++i] = \'\\n\';\n buf[++i] = \'\\0\';\n if (buf[0] == \'\\n\')\n break;\n if (!BUF_MEM_grow(headerB, hl + i + 9)) {\n PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (strncmp(buf, "-----END ", 9) == 0) {\n nohead = 1;\n break;\n }\n memcpy(&(headerB->data[hl]), buf, i);\n headerB->data[hl + i] = \'\\0\';\n hl += i;\n }\n bl = 0;\n if (!BUF_MEM_grow(dataB, 1024)) {\n PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n dataB->data[0] = \'\\0\';\n if (!nohead) {\n for (;;) {\n i = BIO_gets(bp, buf, 254);\n if (i <= 0)\n break;\n while ((i >= 0) && (buf[i] <= \' \'))\n i--;\n buf[++i] = \'\\n\';\n buf[++i] = \'\\0\';\n if (i != 65)\n end = 1;\n if (strncmp(buf, "-----END ", 9) == 0)\n break;\n if (i > 65)\n break;\n if (!BUF_MEM_grow_clean(dataB, i + bl + 9)) {\n PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n memcpy(&(dataB->data[bl]), buf, i);\n dataB->data[bl + i] = \'\\0\';\n bl += i;\n if (end) {\n buf[0] = \'\\0\';\n i = BIO_gets(bp, buf, 254);\n if (i <= 0)\n break;\n while ((i >= 0) && (buf[i] <= \' \'))\n i--;\n buf[++i] = \'\\n\';\n buf[++i] = \'\\0\';\n break;\n }\n }\n } else {\n tmpB = headerB;\n headerB = dataB;\n dataB = tmpB;\n bl = hl;\n }\n i = strlen(nameB->data);\n if ((strncmp(buf, "-----END ", 9) != 0) ||\n (strncmp(nameB->data, &(buf[9]), i) != 0) ||\n (strncmp(&(buf[9 + i]), "-----\\n", 6) != 0)) {\n PEMerr(PEM_F_PEM_READ_BIO, PEM_R_BAD_END_LINE);\n goto err;\n }\n EVP_DecodeInit(ctx);\n i = EVP_DecodeUpdate(ctx,\n (unsigned char *)dataB->data, &bl,\n (unsigned char *)dataB->data, bl);\n if (i < 0) {\n PEMerr(PEM_F_PEM_READ_BIO, PEM_R_BAD_BASE64_DECODE);\n goto err;\n }\n i = EVP_DecodeFinal(ctx, (unsigned char *)&(dataB->data[bl]), &k);\n if (i < 0) {\n PEMerr(PEM_F_PEM_READ_BIO, PEM_R_BAD_BASE64_DECODE);\n goto err;\n }\n bl += k;\n if (bl == 0)\n goto err;\n *name = nameB->data;\n *header = headerB->data;\n *data = (unsigned char *)dataB->data;\n *len = bl;\n OPENSSL_free(nameB);\n OPENSSL_free(headerB);\n OPENSSL_free(dataB);\n EVP_ENCODE_CTX_free(ctx);\n return (1);\n err:\n BUF_MEM_free(nameB);\n BUF_MEM_free(headerB);\n BUF_MEM_free(dataB);\n EVP_ENCODE_CTX_free(ctx);\n return (0);\n}', 'size_t BUF_MEM_grow(BUF_MEM *str, size_t len)\n{\n char *ret;\n size_t n;\n if (str->length >= len) {\n str->length = len;\n return (len);\n }\n if (str->max >= len) {\n memset(&str->data[str->length], 0, len - str->length);\n str->length = len;\n return (len);\n }\n if (len > LIMIT_BEFORE_EXPANSION) {\n BUFerr(BUF_F_BUF_MEM_GROW, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n n = (len + 3) / 3 * 4;\n if ((str->flags & BUF_MEM_FLAG_SECURE))\n ret = sec_alloc_realloc(str, n);\n else\n ret = OPENSSL_realloc(str->data, n);\n if (ret == NULL) {\n BUFerr(BUF_F_BUF_MEM_GROW, ERR_R_MALLOC_FAILURE);\n len = 0;\n } else {\n str->data = ret;\n str->max = n;\n memset(&str->data[str->length], 0, len - str->length);\n str->length = len;\n }\n return (len);\n}']
2,363
0
https://github.com/openssl/openssl/blob/2fdf5d7c2354b76bcc429b5f2c582a580e12d50d/ssl/s3_both.c/#L396
int ssl_cert_type(X509 *x, EVP_PKEY *pkey) { EVP_PKEY *pk; int ret= -1,i,j; if (pkey == NULL) pk=X509_get_pubkey(x); else pk=pkey; if (pk == NULL) goto err; i=pk->type; if (i == EVP_PKEY_RSA) { ret=SSL_PKEY_RSA_ENC; if (x != NULL) { j=X509_get_ext_count(x); } } else if (i == EVP_PKEY_DSA) { ret=SSL_PKEY_DSA_SIGN; } else if (i == EVP_PKEY_DH) { if (x == NULL) ret=SSL_PKEY_DH_DSA; else { j=X509_get_signature_type(x); if (j == EVP_PKEY_RSA) ret=SSL_PKEY_DH_RSA; else if (j== EVP_PKEY_DSA) ret=SSL_PKEY_DH_DSA; else ret= -1; } } else ret= -1; err: if(!pkey) EVP_PKEY_free(pk); return(ret); }
['int ssl_cert_type(X509 *x, EVP_PKEY *pkey)\n\t{\n\tEVP_PKEY *pk;\n\tint ret= -1,i,j;\n\tif (pkey == NULL)\n\t\tpk=X509_get_pubkey(x);\n\telse\n\t\tpk=pkey;\n\tif (pk == NULL) goto err;\n\ti=pk->type;\n\tif (i == EVP_PKEY_RSA)\n\t\t{\n\t\tret=SSL_PKEY_RSA_ENC;\n\t\tif (x != NULL)\n\t\t\t{\n\t\t\tj=X509_get_ext_count(x);\n\t\t\t}\n\t\t}\n\telse if (i == EVP_PKEY_DSA)\n\t\t{\n\t\tret=SSL_PKEY_DSA_SIGN;\n\t\t}\n\telse if (i == EVP_PKEY_DH)\n\t\t{\n\t\tif (x == NULL)\n\t\t\tret=SSL_PKEY_DH_DSA;\n\t\telse\n\t\t\t{\n\t\t\tj=X509_get_signature_type(x);\n\t\t\tif (j == EVP_PKEY_RSA)\n\t\t\t\tret=SSL_PKEY_DH_RSA;\n\t\t\telse if (j== EVP_PKEY_DSA)\n\t\t\t\tret=SSL_PKEY_DH_DSA;\n\t\t\telse ret= -1;\n\t\t\t}\n\t\t}\n\telse\n\t\tret= -1;\nerr:\n\tif(!pkey) EVP_PKEY_free(pk);\n\treturn(ret);\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}', 'void EVP_PKEY_free(EVP_PKEY *x)\n\t{\n\tint i;\n\tif (x == NULL) return;\n\ti=CRYPTO_add(&x->references,-1,CRYPTO_LOCK_EVP_PKEY);\n#ifdef REF_PRINT\n\tREF_PRINT("EVP_PKEY",x);\n#endif\n\tif (i > 0) return;\n#ifdef REF_CHECK\n\tif (i < 0)\n\t\t{\n\t\tfprintf(stderr,"EVP_PKEY_free, bad reference count\\n");\n\t\tabort();\n\t\t}\n#endif\n\tEVP_PKEY_free_it(x);\n\tFree((char *)x);\n\t}', 'int CRYPTO_add_lock(int *pointer, int amount, int type, const char *file,\n\t int line)\n\t{\n\tint ret;\n\tif (add_lock_callback != NULL)\n\t\t{\n#ifdef LOCK_DEBUG\n\t\tint before= *pointer;\n#endif\n\t\tret=add_lock_callback(pointer,amount,type,file,line);\n#ifdef LOCK_DEBUG\n\t\tfprintf(stderr,"ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\\n",\n\t\t\tCRYPTO_thread_id(),\n\t\t\tbefore,amount,ret,\n\t\t\tCRYPTO_get_lock_name(type),\n\t\t\tfile,line);\n#endif\n\t\t*pointer=ret;\n\t\t}\n\telse\n\t\t{\n\t\tCRYPTO_lock(CRYPTO_LOCK|CRYPTO_WRITE,type,file,line);\n\t\tret= *pointer+amount;\n#ifdef LOCK_DEBUG\n\t\tfprintf(stderr,"ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\\n",\n\t\t\tCRYPTO_thread_id(),\n\t\t\t*pointer,amount,ret,\n\t\t\tCRYPTO_get_lock_name(type),\n\t\t\tfile,line);\n#endif\n\t\t*pointer=ret;\n\t\tCRYPTO_lock(CRYPTO_UNLOCK|CRYPTO_WRITE,type,file,line);\n\t\t}\n\treturn(ret);\n\t}']
2,364
0
https://gitlab.com/libtiff/libtiff/blob/b69a1998bedfabc32cd541408bffdef05bd01e45/libtiff/tif_getimage.c/#L2339
static int makecmap(TIFFRGBAImage* img) { int bitspersample = img->bitspersample; int nsamples = 8 / bitspersample; uint16* r = img->redcmap; uint16* g = img->greencmap; uint16* b = img->bluecmap; uint32 *p; int i; img->PALmap = (uint32**) _TIFFmalloc( 256*sizeof (uint32 *)+(256*nsamples*sizeof(uint32))); if (img->PALmap == NULL) { TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No space for Palette mapping table"); return (0); } p = (uint32*)(img->PALmap + 256); for (i = 0; i < 256; i++) { TIFFRGBValue c; img->PALmap[i] = p; #define CMAP(x) c = (TIFFRGBValue) x; *p++ = PACK(r[c]&0xff, g[c]&0xff, b[c]&0xff); switch (bitspersample) { case 1: CMAP(i>>7); CMAP((i>>6)&1); CMAP((i>>5)&1); CMAP((i>>4)&1); CMAP((i>>3)&1); CMAP((i>>2)&1); CMAP((i>>1)&1); CMAP(i&1); break; case 2: CMAP(i>>6); CMAP((i>>4)&3); CMAP((i>>2)&3); CMAP(i&3); break; case 4: CMAP(i>>4); CMAP(i&0xf); break; case 8: CMAP(i); break; } #undef CMAP } return (1); }
['int\nTIFFRGBAImageBegin(TIFFRGBAImage* img, TIFF* tif, int stop, char emsg[1024])\n{\n\tuint16* sampleinfo;\n\tuint16 extrasamples;\n\tuint16 planarconfig;\n\tuint16 compress;\n\tint colorchannels;\n\tuint16 *red_orig, *green_orig, *blue_orig;\n\tint n_color;\n\timg->row_offset = 0;\n\timg->col_offset = 0;\n\timg->redcmap = NULL;\n\timg->greencmap = NULL;\n\timg->bluecmap = NULL;\n\timg->req_orientation = ORIENTATION_BOTLEFT;\n\timg->tif = tif;\n\timg->stoponerr = stop;\n\tTIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &img->bitspersample);\n\tswitch (img->bitspersample) {\n\t\tcase 1:\n\t\tcase 2:\n\t\tcase 4:\n\t\tcase 8:\n\t\tcase 16:\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tsprintf(emsg, "Sorry, can not handle images with %d-bit samples",\n\t\t\t img->bitspersample);\n\t\t\tgoto fail_return;\n\t}\n\timg->alpha = 0;\n\tTIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &img->samplesperpixel);\n\tTIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,\n\t &extrasamples, &sampleinfo);\n\tif (extrasamples >= 1)\n\t{\n\t\tswitch (sampleinfo[0]) {\n\t\t\tcase EXTRASAMPLE_UNSPECIFIED:\n\t\t\t\tif (img->samplesperpixel > 3)\n\t\t\t\t\timg->alpha = EXTRASAMPLE_ASSOCALPHA;\n\t\t\t\tbreak;\n\t\t\tcase EXTRASAMPLE_ASSOCALPHA:\n\t\t\tcase EXTRASAMPLE_UNASSALPHA:\n\t\t\t\timg->alpha = sampleinfo[0];\n\t\t\t\tbreak;\n\t\t}\n\t}\n#ifdef DEFAULT_EXTRASAMPLE_AS_ALPHA\n\tif( !TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric))\n\t\timg->photometric = PHOTOMETRIC_MINISWHITE;\n\tif( extrasamples == 0\n\t && img->samplesperpixel == 4\n\t && img->photometric == PHOTOMETRIC_RGB )\n\t{\n\t\timg->alpha = EXTRASAMPLE_ASSOCALPHA;\n\t\textrasamples = 1;\n\t}\n#endif\n\tcolorchannels = img->samplesperpixel - extrasamples;\n\tTIFFGetFieldDefaulted(tif, TIFFTAG_COMPRESSION, &compress);\n\tTIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &planarconfig);\n\tif (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric)) {\n\t\tswitch (colorchannels) {\n\t\t\tcase 1:\n\t\t\t\tif (isCCITTCompression(tif))\n\t\t\t\t\timg->photometric = PHOTOMETRIC_MINISWHITE;\n\t\t\t\telse\n\t\t\t\t\timg->photometric = PHOTOMETRIC_MINISBLACK;\n\t\t\t\tbreak;\n\t\t\tcase 3:\n\t\t\t\timg->photometric = PHOTOMETRIC_RGB;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tsprintf(emsg, "Missing needed %s tag", photoTag);\n goto fail_return;\n\t\t}\n\t}\n\tswitch (img->photometric) {\n\t\tcase PHOTOMETRIC_PALETTE:\n\t\t\tif (!TIFFGetField(tif, TIFFTAG_COLORMAP,\n\t\t\t &red_orig, &green_orig, &blue_orig)) {\n\t\t\t\tsprintf(emsg, "Missing required \\"Colormap\\" tag");\n goto fail_return;\n\t\t\t}\n\t\t\tn_color = (1L << img->bitspersample);\n\t\t\timg->redcmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);\n\t\t\timg->greencmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);\n\t\t\timg->bluecmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);\n\t\t\tif( !img->redcmap || !img->greencmap || !img->bluecmap ) {\n\t\t\t\tsprintf(emsg, "Out of memory for colormap copy");\n goto fail_return;\n\t\t\t}\n\t\t\t_TIFFmemcpy( img->redcmap, red_orig, n_color * 2 );\n\t\t\t_TIFFmemcpy( img->greencmap, green_orig, n_color * 2 );\n\t\t\t_TIFFmemcpy( img->bluecmap, blue_orig, n_color * 2 );\n\t\tcase PHOTOMETRIC_MINISWHITE:\n\t\tcase PHOTOMETRIC_MINISBLACK:\n\t\t\tif (planarconfig == PLANARCONFIG_CONTIG\n\t\t\t && img->samplesperpixel != 1\n\t\t\t && img->bitspersample < 8 ) {\n\t\t\t\tsprintf(emsg,\n\t\t\t\t "Sorry, can not handle contiguous data with %s=%d, "\n\t\t\t\t "and %s=%d and Bits/Sample=%d",\n\t\t\t\t photoTag, img->photometric,\n\t\t\t\t "Samples/pixel", img->samplesperpixel,\n\t\t\t\t img->bitspersample);\n goto fail_return;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase PHOTOMETRIC_YCBCR:\n\t\t\tif (planarconfig == PLANARCONFIG_CONTIG)\n\t\t\t\tswitch (compress) {\n\t\t\t\t\tcase COMPRESSION_JPEG:\n\t\t\t\t\t\tTIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);\n\t\t\t\t\t\timg->photometric = PHOTOMETRIC_RGB;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tdefault:\n\t\t\t\t\t\t ;\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\tbreak;\n\t\tcase PHOTOMETRIC_RGB:\n\t\t\tif (colorchannels < 3) {\n\t\t\t\tsprintf(emsg, "Sorry, can not handle RGB image with %s=%d",\n\t\t\t\t "Color channels", colorchannels);\n goto fail_return;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase PHOTOMETRIC_SEPARATED:\n\t\t\t{\n\t\t\t\tuint16 inkset;\n\t\t\t\tTIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);\n\t\t\t\tif (inkset != INKSET_CMYK) {\n\t\t\t\t\tsprintf(emsg, "Sorry, can not handle separated image with %s=%d",\n\t\t\t\t\t "InkSet", inkset);\n goto fail_return;\n\t\t\t\t}\n\t\t\t\tif (img->samplesperpixel < 4) {\n\t\t\t\t\tsprintf(emsg, "Sorry, can not handle separated image with %s=%d",\n\t\t\t\t\t "Samples/pixel", img->samplesperpixel);\n goto fail_return;\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\tcase PHOTOMETRIC_LOGL:\n\t\t\tif (compress != COMPRESSION_SGILOG) {\n\t\t\t\tsprintf(emsg, "Sorry, LogL data must have %s=%d",\n\t\t\t\t "Compression", COMPRESSION_SGILOG);\n goto fail_return;\n\t\t\t}\n\t\t\tTIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);\n\t\t\timg->photometric = PHOTOMETRIC_MINISBLACK;\n\t\t\timg->bitspersample = 8;\n\t\t\tbreak;\n\t\tcase PHOTOMETRIC_LOGLUV:\n\t\t\tif (compress != COMPRESSION_SGILOG && compress != COMPRESSION_SGILOG24) {\n\t\t\t\tsprintf(emsg, "Sorry, LogLuv data must have %s=%d or %d",\n\t\t\t\t "Compression", COMPRESSION_SGILOG, COMPRESSION_SGILOG24);\n goto fail_return;\n\t\t\t}\n\t\t\tif (planarconfig != PLANARCONFIG_CONTIG) {\n\t\t\t\tsprintf(emsg, "Sorry, can not handle LogLuv images with %s=%d",\n\t\t\t\t "Planarconfiguration", planarconfig);\n\t\t\t\treturn (0);\n\t\t\t}\n\t\t\tTIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);\n\t\t\timg->photometric = PHOTOMETRIC_RGB;\n\t\t\timg->bitspersample = 8;\n\t\t\tbreak;\n\t\tcase PHOTOMETRIC_CIELAB:\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tsprintf(emsg, "Sorry, can not handle image with %s=%d",\n\t\t\t photoTag, img->photometric);\n goto fail_return;\n\t}\n\timg->Map = NULL;\n\timg->BWmap = NULL;\n\timg->PALmap = NULL;\n\timg->ycbcr = NULL;\n\timg->cielab = NULL;\n\timg->UaToAa = NULL;\n\timg->Bitdepth16To8 = NULL;\n\tTIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &img->width);\n\tTIFFGetField(tif, TIFFTAG_IMAGELENGTH, &img->height);\n\tTIFFGetFieldDefaulted(tif, TIFFTAG_ORIENTATION, &img->orientation);\n\timg->isContig =\n\t !(planarconfig == PLANARCONFIG_SEPARATE && img->samplesperpixel > 1);\n\tif (img->isContig) {\n\t\tif (!PickContigCase(img)) {\n\t\t\tsprintf(emsg, "Sorry, can not handle image");\n\t\t\tgoto fail_return;\n\t\t}\n\t} else {\n\t\tif (!PickSeparateCase(img)) {\n\t\t\tsprintf(emsg, "Sorry, can not handle image");\n\t\t\tgoto fail_return;\n\t\t}\n\t}\n\treturn 1;\n fail_return:\n _TIFFfree( img->redcmap );\n _TIFFfree( img->greencmap );\n _TIFFfree( img->bluecmap );\n img->redcmap = img->greencmap = img->bluecmap = NULL;\n return 0;\n}', 'static int\nPickContigCase(TIFFRGBAImage* img)\n{\n\timg->get = TIFFIsTiled(img->tif) ? gtTileContig : gtStripContig;\n\timg->put.contig = NULL;\n\tswitch (img->photometric) {\n\t\tcase PHOTOMETRIC_RGB:\n\t\t\tswitch (img->bitspersample) {\n\t\t\t\tcase 8:\n\t\t\t\t\tif (img->alpha == EXTRASAMPLE_ASSOCALPHA)\n\t\t\t\t\t\timg->put.contig = putRGBAAcontig8bittile;\n\t\t\t\t\telse if (img->alpha == EXTRASAMPLE_UNASSALPHA)\n\t\t\t\t\t{\n\t\t\t\t\t\tif (BuildMapUaToAa(img))\n\t\t\t\t\t\t\timg->put.contig = putRGBUAcontig8bittile;\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t\timg->put.contig = putRGBcontig8bittile;\n\t\t\t\t\tbreak;\n\t\t\t\tcase 16:\n\t\t\t\t\tif (img->alpha == EXTRASAMPLE_ASSOCALPHA)\n\t\t\t\t\t{\n\t\t\t\t\t\tif (BuildMapBitdepth16To8(img))\n\t\t\t\t\t\t\timg->put.contig = putRGBAAcontig16bittile;\n\t\t\t\t\t}\n\t\t\t\t\telse if (img->alpha == EXTRASAMPLE_UNASSALPHA)\n\t\t\t\t\t{\n\t\t\t\t\t\tif (BuildMapBitdepth16To8(img) &&\n\t\t\t\t\t\t BuildMapUaToAa(img))\n\t\t\t\t\t\t\timg->put.contig = putRGBUAcontig16bittile;\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tif (BuildMapBitdepth16To8(img))\n\t\t\t\t\t\t\timg->put.contig = putRGBcontig16bittile;\n\t\t\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase PHOTOMETRIC_SEPARATED:\n\t\t\tif (buildMap(img)) {\n\t\t\t\tif (img->bitspersample == 8) {\n\t\t\t\t\tif (!img->Map)\n\t\t\t\t\t\timg->put.contig = putRGBcontig8bitCMYKtile;\n\t\t\t\t\telse\n\t\t\t\t\t\timg->put.contig = putRGBcontig8bitCMYKMaptile;\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\tcase PHOTOMETRIC_PALETTE:\n\t\t\tif (buildMap(img)) {\n\t\t\t\tswitch (img->bitspersample) {\n\t\t\t\t\tcase 8:\n\t\t\t\t\t\timg->put.contig = put8bitcmaptile;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 4:\n\t\t\t\t\t\timg->put.contig = put4bitcmaptile;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 2:\n\t\t\t\t\t\timg->put.contig = put2bitcmaptile;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 1:\n\t\t\t\t\t\timg->put.contig = put1bitcmaptile;\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\tcase PHOTOMETRIC_MINISWHITE:\n\t\tcase PHOTOMETRIC_MINISBLACK:\n\t\t\tif (buildMap(img)) {\n\t\t\t\tswitch (img->bitspersample) {\n\t\t\t\t\tcase 16:\n\t\t\t\t\t\timg->put.contig = put16bitbwtile;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 8:\n\t\t\t\t\t\timg->put.contig = putgreytile;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 4:\n\t\t\t\t\t\timg->put.contig = put4bitbwtile;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 2:\n\t\t\t\t\t\timg->put.contig = put2bitbwtile;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 1:\n\t\t\t\t\t\timg->put.contig = put1bitbwtile;\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\tcase PHOTOMETRIC_YCBCR:\n\t\t\tif ((img->bitspersample==8) && (img->samplesperpixel==3))\n\t\t\t{\n\t\t\t\tif (initYCbCrConversion(img)!=0)\n\t\t\t\t{\n\t\t\t\t\tuint16 SubsamplingHor;\n\t\t\t\t\tuint16 SubsamplingVer;\n\t\t\t\t\tTIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, &SubsamplingHor, &SubsamplingVer);\n\t\t\t\t\tswitch ((SubsamplingHor<<4)|SubsamplingVer) {\n\t\t\t\t\t\tcase 0x44:\n\t\t\t\t\t\t\timg->put.contig = putcontig8bitYCbCr44tile;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase 0x42:\n\t\t\t\t\t\t\timg->put.contig = putcontig8bitYCbCr42tile;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase 0x41:\n\t\t\t\t\t\t\timg->put.contig = putcontig8bitYCbCr41tile;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase 0x22:\n\t\t\t\t\t\t\timg->put.contig = putcontig8bitYCbCr22tile;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase 0x21:\n\t\t\t\t\t\t\timg->put.contig = putcontig8bitYCbCr21tile;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase 0x12:\n\t\t\t\t\t\t\timg->put.contig = putcontig8bitYCbCr12tile;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase 0x11:\n\t\t\t\t\t\t\timg->put.contig = putcontig8bitYCbCr11tile;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\tcase PHOTOMETRIC_CIELAB:\n\t\t\tif (buildMap(img)) {\n\t\t\t\tif (img->bitspersample == 8)\n\t\t\t\t\timg->put.contig = initCIELabConversion(img);\n\t\t\t\tbreak;\n\t\t\t}\n\t}\n\treturn ((img->get!=NULL) && (img->put.contig!=NULL));\n}', 'static int\nbuildMap(TIFFRGBAImage* img)\n{\n switch (img->photometric) {\n case PHOTOMETRIC_RGB:\n case PHOTOMETRIC_YCBCR:\n case PHOTOMETRIC_SEPARATED:\n\tif (img->bitspersample == 8)\n\t break;\n case PHOTOMETRIC_MINISBLACK:\n case PHOTOMETRIC_MINISWHITE:\n\tif (!setupMap(img))\n\t return (0);\n\tbreak;\n case PHOTOMETRIC_PALETTE:\n\tif (checkcmap(img) == 16)\n\t cvtcmap(img);\n\telse\n\t TIFFWarningExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "Assuming 8-bit colormap");\n\tif (img->bitspersample <= 8 && !makecmap(img))\n\t return (0);\n\tbreak;\n }\n return (1);\n}', 'static int\nmakecmap(TIFFRGBAImage* img)\n{\n int bitspersample = img->bitspersample;\n int nsamples = 8 / bitspersample;\n uint16* r = img->redcmap;\n uint16* g = img->greencmap;\n uint16* b = img->bluecmap;\n uint32 *p;\n int i;\n img->PALmap = (uint32**) _TIFFmalloc(\n\t256*sizeof (uint32 *)+(256*nsamples*sizeof(uint32)));\n if (img->PALmap == NULL) {\n\t\tTIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No space for Palette mapping table");\n\t\treturn (0);\n\t}\n p = (uint32*)(img->PALmap + 256);\n for (i = 0; i < 256; i++) {\n\tTIFFRGBValue c;\n\timg->PALmap[i] = p;\n#define\tCMAP(x)\tc = (TIFFRGBValue) x; *p++ = PACK(r[c]&0xff, g[c]&0xff, b[c]&0xff);\n\tswitch (bitspersample) {\n\tcase 1:\n\t CMAP(i>>7);\n\t CMAP((i>>6)&1);\n\t CMAP((i>>5)&1);\n\t CMAP((i>>4)&1);\n\t CMAP((i>>3)&1);\n\t CMAP((i>>2)&1);\n\t CMAP((i>>1)&1);\n\t CMAP(i&1);\n\t break;\n\tcase 2:\n\t CMAP(i>>6);\n\t CMAP((i>>4)&3);\n\t CMAP((i>>2)&3);\n\t CMAP(i&3);\n\t break;\n\tcase 4:\n\t CMAP(i>>4);\n\t CMAP(i&0xf);\n\t break;\n\tcase 8:\n\t CMAP(i);\n\t break;\n\t}\n#undef CMAP\n }\n return (1);\n}']
2,365
1
https://github.com/openssl/openssl/blob/7999c65c9bfd80ec0f07f6eb5be5ce2e36927298/ssl/ssl_ciph.c/#L359
static void ssl_cipher_collect_ciphers(const SSL_METHOD *ssl_method, int num_of_ciphers, unsigned long mask, CIPHER_ORDER *list, CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p) { unsigned int i, list_num; SSL_CIPHER *c; list_num = 0; for (i = 0; i < num_of_ciphers; i++) { c = ssl_method->get_cipher(i); if ((c != NULL) && c->valid && !(c->algorithms & mask)) { list[list_num].cipher = c; list[list_num].next = NULL; list[list_num].prev = NULL; list[list_num].active = 0; list_num++; } } for (i = 1; i < list_num - 1; i++) { list[i].prev = &(list[i-1]); list[i].next = &(list[i+1]); } if (list_num > 0) { (*head_p) = &(list[0]); (*head_p)->prev = NULL; (*head_p)->next = &(list[1]); (*tail_p) = &(list[list_num - 1]); (*tail_p)->prev = &(list[list_num - 2]); (*tail_p)->next = NULL; } }
['static void ssl_cipher_collect_ciphers(const SSL_METHOD *ssl_method,\n\t\tint num_of_ciphers, unsigned long mask, CIPHER_ORDER *list,\n\t\tCIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)\n\t{\n\tunsigned int i, list_num;\n\tSSL_CIPHER *c;\n\tlist_num = 0;\n\tfor (i = 0; i < num_of_ciphers; i++)\n\t\t{\n\t\tc = ssl_method->get_cipher(i);\n\t\tif ((c != NULL) && c->valid && !(c->algorithms & mask))\n\t\t\t{\n\t\t\tlist[list_num].cipher = c;\n\t\t\tlist[list_num].next = NULL;\n\t\t\tlist[list_num].prev = NULL;\n\t\t\tlist[list_num].active = 0;\n\t\t\tlist_num++;\n\t\t\t}\n\t\t}\n\tfor (i = 1; i < list_num - 1; i++)\n\t\t{\n\t\tlist[i].prev = &(list[i-1]);\n\t\tlist[i].next = &(list[i+1]);\n\t\t}\n\tif (list_num > 0)\n\t\t{\n\t\t(*head_p) = &(list[0]);\n\t\t(*head_p)->prev = NULL;\n\t\t(*head_p)->next = &(list[1]);\n\t\t(*tail_p) = &(list[list_num - 1]);\n\t\t(*tail_p)->prev = &(list[list_num - 2]);\n\t\t(*tail_p)->next = NULL;\n\t\t}\n\t}']
2,366
0
https://github.com/openssl/openssl/blob/dc64dc2edd215d6cc5843c1bfe1f0b64bff26adc/providers/common/ciphers/cipher_ccm.c/#L320
static int ccm_tls_cipher(PROV_CCM_CTX *ctx, unsigned char *out, size_t *padlen, const unsigned char *in, size_t len) { int rv = 0; size_t olen = 0; if (out != in || len < (EVP_CCM_TLS_EXPLICIT_IV_LEN + (size_t)ctx->m)) goto err; if (ctx->enc) memcpy(out, ctx->buf, EVP_CCM_TLS_EXPLICIT_IV_LEN); memcpy(ctx->iv + EVP_CCM_TLS_FIXED_IV_LEN, in, EVP_CCM_TLS_EXPLICIT_IV_LEN); len -= EVP_CCM_TLS_EXPLICIT_IV_LEN + ctx->m; if (!ccm_set_iv(ctx, len)) goto err; if (!ctx->hw->setaad(ctx, ctx->buf, ctx->tls_aad_len)) goto err; in += EVP_CCM_TLS_EXPLICIT_IV_LEN; out += EVP_CCM_TLS_EXPLICIT_IV_LEN; if (ctx->enc) { if (!ctx->hw->auth_encrypt(ctx, in, out, len, out + len, ctx->m)) goto err; olen = len + EVP_CCM_TLS_EXPLICIT_IV_LEN + ctx->m; } else { if (!ctx->hw->auth_decrypt(ctx, in, out, len, (unsigned char *)in + len, ctx->m)) goto err; olen = len; } rv = 1; err: *padlen = olen; return rv; }
['int ccm_stream_final(void *vctx, unsigned char *out, size_t *outl,\n size_t outsize)\n{\n PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;\n int i;\n i = ccm_cipher_internal(ctx, out, outl, NULL, 0);\n if (i <= 0)\n return 0;\n *outl = 0;\n return 1;\n}', 'static int ccm_cipher_internal(PROV_CCM_CTX *ctx, unsigned char *out,\n size_t *padlen, const unsigned char *in,\n size_t len)\n{\n int rv = 0;\n size_t olen = 0;\n const PROV_CCM_HW *hw = ctx->hw;\n if (!ctx->key_set)\n return 0;\n if (ctx->tls_aad_len != UNINITIALISED_SIZET)\n return ccm_tls_cipher(ctx, out, padlen, in, len);\n if (in == NULL && out != NULL)\n goto finish;\n if (!ctx->iv_set)\n goto err;\n if (out == NULL) {\n if (in == NULL) {\n if (!ccm_set_iv(ctx, len))\n goto err;\n } else {\n if (!ctx->len_set && len)\n goto err;\n if (!hw->setaad(ctx, in, len))\n goto err;\n }\n } else {\n if (!ctx->len_set && !ccm_set_iv(ctx, len))\n goto err;\n if (ctx->enc) {\n if (!hw->auth_encrypt(ctx, in, out, len, NULL, 0))\n goto err;\n ctx->tag_set = 1;\n } else {\n if (!ctx->tag_set)\n goto err;\n if (!hw->auth_decrypt(ctx, in, out, len, ctx->buf, ctx->m))\n goto err;\n ctx->iv_set = 0;\n ctx->tag_set = 0;\n ctx->len_set = 0;\n }\n }\n olen = len;\nfinish:\n rv = 1;\nerr:\n *padlen = olen;\n return rv;\n}', 'static int ccm_tls_cipher(PROV_CCM_CTX *ctx,\n unsigned char *out, size_t *padlen,\n const unsigned char *in, size_t len)\n{\n int rv = 0;\n size_t olen = 0;\n if (out != in || len < (EVP_CCM_TLS_EXPLICIT_IV_LEN + (size_t)ctx->m))\n goto err;\n if (ctx->enc)\n memcpy(out, ctx->buf, EVP_CCM_TLS_EXPLICIT_IV_LEN);\n memcpy(ctx->iv + EVP_CCM_TLS_FIXED_IV_LEN, in, EVP_CCM_TLS_EXPLICIT_IV_LEN);\n len -= EVP_CCM_TLS_EXPLICIT_IV_LEN + ctx->m;\n if (!ccm_set_iv(ctx, len))\n goto err;\n if (!ctx->hw->setaad(ctx, ctx->buf, ctx->tls_aad_len))\n goto err;\n in += EVP_CCM_TLS_EXPLICIT_IV_LEN;\n out += EVP_CCM_TLS_EXPLICIT_IV_LEN;\n if (ctx->enc) {\n if (!ctx->hw->auth_encrypt(ctx, in, out, len, out + len, ctx->m))\n goto err;\n olen = len + EVP_CCM_TLS_EXPLICIT_IV_LEN + ctx->m;\n } else {\n if (!ctx->hw->auth_decrypt(ctx, in, out, len,\n (unsigned char *)in + len, ctx->m))\n goto err;\n olen = len;\n }\n rv = 1;\nerr:\n *padlen = olen;\n return rv;\n}']
2,367
0
https://github.com/openssl/openssl/blob/f9df0a7775f483c175cda5832360cccd1db6943a/crypto/lhash/lhash.c/#L122
void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data) { unsigned long hash; OPENSSL_LH_NODE *nn, **rn; void *ret; lh->error = 0; rn = getrn(lh, data, &hash); if (*rn == NULL) { lh->num_no_delete++; return NULL; } else { nn = *rn; *rn = nn->next; ret = nn->data; OPENSSL_free(nn); lh->num_delete++; } lh->num_items--; if ((lh->num_nodes > MIN_NODES) && (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes))) contract(lh); return ret; }
['static int test_cipher_name(void)\n{\n SSL_CTX *ctx = NULL;\n SSL *ssl = NULL;\n const SSL_CIPHER *c;\n STACK_OF(SSL_CIPHER) *sk = NULL;\n const char *ciphers = "ALL:eNULL", *p, *q, *r;\n int i, id = 0, ret = 0;\n p = SSL_CIPHER_standard_name(NULL);\n if (!TEST_str_eq(p, "(NONE)")) {\n TEST_info("test_cipher_name(std) failed: NULL input doesn\'t return \\"(NONE)\\"\\n");\n goto err;\n }\n p = OPENSSL_cipher_name(NULL);\n if (!TEST_str_eq(p, "(NONE)")) {\n TEST_info("test_cipher_name(ossl) failed: NULL input doesn\'t return \\"(NONE)\\"\\n");\n goto err;\n }\n p = OPENSSL_cipher_name("This is not a valid cipher");\n if (!TEST_str_eq(p, "(NONE)")) {\n TEST_info("test_cipher_name(ossl) failed: invalid input doesn\'t return \\"(NONE)\\"\\n");\n goto err;\n }\n ctx = SSL_CTX_new(TLS_server_method());\n if (ctx == NULL) {\n TEST_info("test_cipher_name failed: internal error\\n");\n goto err;\n }\n if (!SSL_CTX_set_cipher_list(ctx, ciphers)) {\n TEST_info("test_cipher_name failed: internal error\\n");\n goto err;\n }\n ssl = SSL_new(ctx);\n if (ssl == NULL) {\n TEST_info("test_cipher_name failed: internal error\\n");\n goto err;\n }\n sk = SSL_get_ciphers(ssl);\n if (sk == NULL) {\n TEST_info("test_cipher_name failed: internal error\\n");\n goto err;\n }\n for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {\n c = sk_SSL_CIPHER_value(sk, i);\n id = SSL_CIPHER_get_id(c) & 0xFFFF;\n if ((id == 0xFF85) || (id == 0xFF87))\n continue;\n p = SSL_CIPHER_standard_name(c);\n q = get_std_name_by_id(id);\n if (!TEST_ptr(p)) {\n TEST_info("test_cipher_name failed: expected %s, got NULL, cipher %x\\n",\n q, id);\n goto err;\n }\n if (!TEST_str_eq(p, q)) {\n TEST_info("test_cipher_name(std) failed: expected %s, got %s, cipher %x\\n",\n q, p, id);\n goto err;\n }\n q = SSL_CIPHER_get_name(c);\n r = OPENSSL_cipher_name(p);\n if (!TEST_str_eq(r, q)) {\n TEST_info("test_cipher_name(ossl) failed: expected %s, got %s, cipher %x\\n",\n q, r, id);\n goto err;\n }\n }\n ret = 1;\nerr:\n SSL_CTX_free(ctx);\n SSL_free(ssl);\n return ret;\n}', 'SSL *SSL_new(SSL_CTX *ctx)\n{\n SSL *s;\n if (ctx == NULL) {\n SSLerr(SSL_F_SSL_NEW, SSL_R_NULL_SSL_CTX);\n return (NULL);\n }\n if (ctx->method == NULL) {\n SSLerr(SSL_F_SSL_NEW, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);\n return (NULL);\n }\n s = OPENSSL_zalloc(sizeof(*s));\n if (s == NULL)\n goto err;\n s->lock = CRYPTO_THREAD_lock_new();\n if (s->lock == NULL)\n goto err;\n if (RAND_get_rand_method() == RAND_OpenSSL()) {\n s->drbg = RAND_DRBG_new(NID_aes_128_ctr, RAND_DRBG_FLAG_CTR_USE_DF,\n RAND_DRBG_get0_global());\n if (s->drbg == NULL\n || RAND_DRBG_instantiate(s->drbg, NULL, 0) == 0) {\n CRYPTO_THREAD_lock_free(s->lock);\n goto err;\n }\n }\n RECORD_LAYER_init(&s->rlayer, s);\n s->options = ctx->options;\n s->dane.flags = ctx->dane.flags;\n s->min_proto_version = ctx->min_proto_version;\n s->max_proto_version = ctx->max_proto_version;\n s->mode = ctx->mode;\n s->max_cert_list = ctx->max_cert_list;\n s->references = 1;\n s->max_early_data = ctx->max_early_data;\n s->cert = ssl_cert_dup(ctx->cert);\n if (s->cert == NULL)\n goto err;\n RECORD_LAYER_set_read_ahead(&s->rlayer, ctx->read_ahead);\n s->msg_callback = ctx->msg_callback;\n s->msg_callback_arg = ctx->msg_callback_arg;\n s->verify_mode = ctx->verify_mode;\n s->not_resumable_session_cb = ctx->not_resumable_session_cb;\n s->record_padding_cb = ctx->record_padding_cb;\n s->record_padding_arg = ctx->record_padding_arg;\n s->block_padding = ctx->block_padding;\n s->sid_ctx_length = ctx->sid_ctx_length;\n 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->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 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 ssl_free_wbio_buffer(s);\n BIO_free_all(s->wbio);\n BIO_free_all(s->rbio);\n BUF_MEM_free(s->init_buf);\n sk_SSL_CIPHER_free(s->cipher_list);\n sk_SSL_CIPHER_free(s->cipher_list_by_id);\n if (s->session != NULL) {\n ssl_clear_bad_session(s);\n SSL_SESSION_free(s->session);\n }\n 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 sk_X509_NAME_pop_free(s->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 RECORD_LAYER_release(&s->rlayer);\n SSL_CTX_free(s->ctx);\n ASYNC_WAIT_CTX_free(s->waitctx);\n#if !defined(OPENSSL_NO_NEXTPROTONEG)\n OPENSSL_free(s->ext.npn);\n#endif\n#ifndef OPENSSL_NO_SRTP\n sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);\n#endif\n RAND_DRBG_free(s->drbg);\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 c->not_resumable = 1;\n if (lck)\n CRYPTO_THREAD_unlock(ctx->lock);\n if (ret)\n SSL_SESSION_free(r);\n if (ctx->remove_session_cb != NULL)\n ctx->remove_session_cb(ctx, c);\n } else\n ret = 0;\n return (ret);\n}', 'DEFINE_LHASH_OF(SSL_SESSION)', 'void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)\n{\n unsigned long hash;\n OPENSSL_LH_NODE *nn, **rn;\n void *ret;\n lh->error = 0;\n rn = getrn(lh, data, &hash);\n if (*rn == NULL) {\n lh->num_no_delete++;\n return NULL;\n } else {\n nn = *rn;\n *rn = nn->next;\n ret = nn->data;\n OPENSSL_free(nn);\n lh->num_delete++;\n }\n lh->num_items--;\n if ((lh->num_nodes > MIN_NODES) &&\n (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))\n contract(lh);\n return ret;\n}']
2,368
0
https://github.com/openssl/openssl/blob/d40a1b865fddc3d67f8c06ff1f1466fad331c8f7/crypto/bn/bn_shift.c/#L150
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); r->neg=a->neg; nw=n/BN_BITS2; if (bn_wexpand(r,a->top+nw+1) == NULL) return(0); lb=n%BN_BITS2; rb=BN_BITS2-lb; f=a->d; t=r->d; t[a->top+nw]=0; if (lb == 0) for (i=a->top-1; i>=0; i--) t[nw+i]=f[i]; else for (i=a->top-1; i>=0; i--) { l=f[i]; t[nw+i+1]|=(l>>rb)&BN_MASK2; t[nw+i]=(l<<lb)&BN_MASK2; } memset(t,0,nw*sizeof(t[0])); r->top=a->top+nw+1; bn_correct_top(r); bn_check_top(r); return(1); }
['static int BN_div_no_branch(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,\n\tconst BIGNUM *divisor, BN_CTX *ctx)\n\t{\n\tint norm_shift,i,loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tsize_t num_n,div_n;\n\tbn_check_top(dv);\n\tbn_check_top(rm);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV_NO_BRANCH,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\n\t\t}\n\tBN_CTX_start(ctx);\n\ttmp=BN_CTX_get(ctx);\n\tsnum=BN_CTX_get(ctx);\n\tsdiv=BN_CTX_get(ctx);\n\tif (dv == NULL)\n\t\tres=BN_CTX_get(ctx);\n\telse\tres=dv;\n\tif (sdiv == NULL || res == NULL) goto err;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tif (!(BN_lshift(sdiv,divisor,norm_shift))) goto err;\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tif (!(BN_lshift(snum,num,norm_shift))) goto err;\n\tsnum->neg=0;\n\tif (snum->top <= sdiv->top+1)\n\t\t{\n\t\tif (bn_wexpand(snum, sdiv->top + 2) == NULL) goto err;\n\t\tfor (i = snum->top; i < sdiv->top + 2; i++) snum->d[i] = 0;\n\t\tsnum->top = sdiv->top + 2;\n\t\t}\n\telse\n\t\t{\n\t\tif (bn_wexpand(snum, snum->top + 1) == NULL) goto err;\n\t\tsnum->d[snum->top] = 0;\n\t\tsnum->top ++;\n\t\t}\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\twnum.neg = 0;\n\twnum.d = &(snum->d[loop]);\n\twnum.top = div_n;\n\twnum.dmax = snum->dmax - loop;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,loop+1U)) goto err;\n\tres->top=loop-1;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,div_n+1U)) goto err;\n\tif (res->top == 0)\n\t\tres->neg = 0;\n\telse\n\t\tresp--;\n\tfor (i=0; i<loop-1; i++, wnump--, resp--)\n\t\t{\n\t\tBN_ULONG q,l0;\n#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n\t\tBN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG);\n\t\tq=bn_div_3_words(wnump,d1,d0);\n#else\n\t\tBN_ULONG n0,n1,rem=0;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\t{\n#ifdef BN_LLONG\n\t\t\tBN_ULLONG t2;\n#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n\t\t\tq=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);\n#else\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tt2 -= d1;\n\t\t\t\t}\n#else\n\t\t\tBN_ULONG t2l,t2h;\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#if defined(BN_UMULT_LOHI)\n\t\t\tBN_UMULT_LOHI(t2l,t2h,d1,q);\n#elif defined(BN_UMULT_HIGH)\n\t\t\tt2l = d1 * q;\n\t\t\tt2h = BN_UMULT_HIGH(d1,q);\n#else\n\t\t\t{\n\t\t\tBN_ULONG ql, qh;\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n\t\t\t}\n#endif\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif ((t2h < rem) ||\n\t\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t\t}\n#endif\n\t\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\twnum.d--;\n\t\tif (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1))\n\t\t\t{\n\t\t\tq--;\n\t\t\tif (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n\t\t\t\t(*wnump)++;\n\t\t\t}\n\t\t*resp = q;\n\t\t}\n\tbn_correct_top(snum);\n\tif (rm != NULL)\n\t\t{\n\t\tint neg = num->neg;\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\tif (!BN_is_zero(rm))\n\t\t\trm->neg = neg;\n\t\tbn_check_top(rm);\n\t\t}\n\tbn_correct_top(res);\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', '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}', 'int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)\n\t{\n\tint i,nw,lb,rb;\n\tBN_ULONG *t,*f;\n\tBN_ULONG l;\n\tbn_check_top(r);\n\tbn_check_top(a);\n\tr->neg=a->neg;\n\tnw=n/BN_BITS2;\n\tif (bn_wexpand(r,a->top+nw+1) == NULL) return(0);\n\tlb=n%BN_BITS2;\n\trb=BN_BITS2-lb;\n\tf=a->d;\n\tt=r->d;\n\tt[a->top+nw]=0;\n\tif (lb == 0)\n\t\tfor (i=a->top-1; i>=0; i--)\n\t\t\tt[nw+i]=f[i];\n\telse\n\t\tfor (i=a->top-1; i>=0; i--)\n\t\t\t{\n\t\t\tl=f[i];\n\t\t\tt[nw+i+1]|=(l>>rb)&BN_MASK2;\n\t\t\tt[nw+i]=(l<<lb)&BN_MASK2;\n\t\t\t}\n\tmemset(t,0,nw*sizeof(t[0]));\n\tr->top=a->top+nw+1;\n\tbn_correct_top(r);\n\tbn_check_top(r);\n\treturn(1);\n\t}']
2,369
0
https://github.com/openssl/openssl/blob/adf652436a42a5132e708f8003b7621647f0a404/crypto/bn/bn_sqr.c/#L120
void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp) { int i, j, max; const BN_ULONG *ap; BN_ULONG *rp; max = n * 2; ap = a; rp = r; rp[0] = rp[max - 1] = 0; rp++; j = n; if (--j > 0) { ap++; rp[j] = bn_mul_words(rp, ap, j, ap[-1]); rp += 2; } for (i = n - 2; i > 0; i--) { j--; ap++; rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]); rp += 2; } bn_add_words(r, r, r, max); bn_sqr_words(tmp, a, n); bn_add_words(r, r, tmp, max); }
['int SSL_set_srp_server_param_pw(SSL *s, const char *user, const char *pass,\n const char *grp)\n{\n SRP_gN *GN = SRP_get_default_gN(grp);\n if (GN == NULL)\n return -1;\n s->srp_ctx.N = BN_dup(GN->N);\n s->srp_ctx.g = BN_dup(GN->g);\n BN_clear_free(s->srp_ctx.v);\n s->srp_ctx.v = NULL;\n BN_clear_free(s->srp_ctx.s);\n s->srp_ctx.s = NULL;\n if (!SRP_create_verifier_BN\n (user, pass, &s->srp_ctx.s, &s->srp_ctx.v, GN->N, GN->g))\n return -1;\n return 1;\n}', 'BIGNUM *BN_dup(const BIGNUM *a)\n{\n BIGNUM *t;\n if (a == NULL)\n return NULL;\n bn_check_top(a);\n t = BN_get_flags(a, BN_FLG_SECURE) ? BN_secure_new() : BN_new();\n if (t == NULL)\n return NULL;\n if (!BN_copy(t, a)) {\n BN_free(t);\n return NULL;\n }\n bn_check_top(t);\n return t;\n}', 'int SRP_create_verifier_BN(const char *user, const char *pass, BIGNUM **salt,\n BIGNUM **verifier, const BIGNUM *N,\n const BIGNUM *g)\n{\n int result = 0;\n BIGNUM *x = NULL;\n BN_CTX *bn_ctx = BN_CTX_new();\n unsigned char tmp2[MAX_LEN];\n BIGNUM *salttmp = NULL;\n if ((user == NULL) ||\n (pass == NULL) ||\n (salt == NULL) ||\n (verifier == NULL) || (N == NULL) || (g == NULL) || (bn_ctx == NULL))\n goto err;\n if (*salt == NULL) {\n if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)\n goto err;\n salttmp = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);\n } else {\n salttmp = *salt;\n }\n x = SRP_Calc_x(salttmp, user, pass);\n *verifier = BN_new();\n if (*verifier == NULL)\n goto err;\n if (!BN_mod_exp(*verifier, g, x, N, bn_ctx)) {\n BN_clear_free(*verifier);\n goto err;\n }\n result = 1;\n *salt = salttmp;\n err:\n if (salt != NULL && *salt != salttmp)\n BN_clear_free(salttmp);\n BN_clear_free(x);\n BN_CTX_free(bn_ctx);\n return result;\n}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n BN_CTX *ctx)\n{\n int ret;\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n if (BN_is_odd(m)) {\n# ifdef MONT_EXP_WORD\n if (a->top == 1 && !a->neg\n && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(a, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(m, BN_FLG_CONSTTIME) == 0)) {\n BN_ULONG A = a->d[0];\n ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);\n } else\n# endif\n ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);\n } else\n#endif\n#ifdef RECP_MUL_MOD\n {\n ret = BN_mod_exp_recp(r, a, p, m, ctx);\n }\n#else\n {\n ret = BN_mod_exp_simple(r, a, p, m, ctx);\n }\n#endif\n bn_check_top(r);\n return ret;\n}', 'int BN_is_odd(const BIGNUM *a)\n{\n return (a->top > 0) && (a->d[0] & 1);\n}', 'int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n int start = 1;\n BIGNUM *d, *r;\n const BIGNUM *aa;\n BIGNUM *val[TABLE_SIZE];\n BN_MONT_CTX *mont = NULL;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(a, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont);\n }\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_abs_is_word(m, 1)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n d = BN_CTX_get(ctx);\n r = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (val[0] == NULL)\n goto err;\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n if (a->neg || BN_ucmp(a, m) >= 0) {\n if (!BN_nnmod(val[0], a, m, ctx))\n goto err;\n aa = val[0];\n } else\n aa = a;\n if (BN_is_zero(aa)) {\n BN_zero(rr);\n ret = 1;\n goto err;\n }\n if (!BN_to_montgomery(val[0], aa, mont, ctx))\n goto err;\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!BN_mod_mul_montgomery(d, val[0], val[0], mont, ctx))\n goto err;\n j = 1 << (window - 1);\n for (i = 1; i < j; i++) {\n if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul_montgomery(val[i], val[i - 1], d, mont, ctx))\n goto err;\n }\n }\n start = 1;\n wvalue = 0;\n wstart = bits - 1;\n wend = 0;\n#if 1\n j = m->top;\n if (m->d[j - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n if (bn_wexpand(r, j) == NULL)\n goto err;\n r->d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < j; i++)\n r->d[i] = (~m->d[i]) & BN_MASK2;\n r->top = j;\n bn_correct_top(r);\n } else\n#endif\n if (!BN_to_montgomery(r, BN_value_one(), mont, ctx))\n goto err;\n for (;;) {\n if (BN_is_bit_set(p, wstart) == 0) {\n if (!start) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (wstart == 0)\n break;\n wstart--;\n continue;\n }\n j = wstart;\n wvalue = 1;\n wend = 0;\n for (i = 1; i < window; i++) {\n if (wstart - i < 0)\n break;\n if (BN_is_bit_set(p, wstart - i)) {\n wvalue <<= (i - wend);\n wvalue |= 1;\n wend = i;\n }\n }\n j = wend + 1;\n if (!start)\n for (i = 0; i < j; i++) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (!BN_mod_mul_montgomery(r, r, val[wvalue >> 1], mont, ctx))\n goto err;\n wstart -= wend + 1;\n wvalue = 0;\n start = 0;\n if (wstart < 0)\n break;\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n j = mont->N.top;\n val[0]->d[0] = 1;\n for (i = 1; i < j; i++)\n val[0]->d[i] = 0;\n val[0]->top = j;\n if (!BN_mod_mul_montgomery(rr, r, val[0], mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, r, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n BN_CTX_end(ctx);\n bn_check_top(rr);\n return ret;\n}', 'int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx,\n BN_MONT_CTX *in_mont)\n{\n int i, bits, ret = 0, window, wvalue;\n int top;\n BN_MONT_CTX *mont = NULL;\n int numPowers;\n unsigned char *powerbufFree = NULL;\n int powerbufLen = 0;\n unsigned char *powerbuf = NULL;\n BIGNUM tmp, am;\n#if defined(SPARC_T4_MONT)\n unsigned int t4 = 0;\n#endif\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT_CONSTTIME, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n top = m->top;\n bits = 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_montgomery(&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_montgomery(&am, &am, mont, ctx))\n goto err;\n } else if (!BN_to_montgomery(&am, a, mont, ctx))\n goto err;\n#if defined(SPARC_T4_MONT)\n if (t4) {\n typedef int (*bn_pwr5_mont_f) (BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_8(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_16(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_24(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_32(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n static const bn_pwr5_mont_f pwr5_funcs[4] = {\n bn_pwr5_mont_t4_8, bn_pwr5_mont_t4_16,\n bn_pwr5_mont_t4_24, bn_pwr5_mont_t4_32\n };\n bn_pwr5_mont_f pwr5_worker = pwr5_funcs[top / 16 - 1];\n typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap, const void *bp,\n const BN_ULONG *np, const BN_ULONG *n0);\n int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n static const bn_mul_mont_f mul_funcs[4] = {\n bn_mul_mont_t4_8, bn_mul_mont_t4_16,\n bn_mul_mont_t4_24, bn_mul_mont_t4_32\n };\n bn_mul_mont_f mul_worker = mul_funcs[top / 16 - 1];\n void bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_gather5_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_flip_n_scatter5_t4(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5_t4(BN_ULONG *out, size_t num,\n void *table, size_t power);\n void bn_flip_t4(BN_ULONG *dst, BN_ULONG *src, size_t num);\n BN_ULONG *np = mont->N.d, *n0 = mont->n0;\n int stride = 5 * (6 - (top / 16 - 1));\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 0);\n bn_flip_n_scatter5_t4(am.d, top, powerbuf, 1);\n if (!(*mul_worker) (tmp.d, am.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, am.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, am.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 2);\n for (i = 3; i < 32; i++) {\n if (!(*mul_worker) (tmp.d, tmp.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, tmp.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, tmp.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, i);\n }\n np = alloca(top * sizeof(BN_ULONG));\n top /= 2;\n bn_flip_t4(np, mont->N.d, top);\n bits--;\n for (wvalue = 0, i = bits % 5; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_gather5_t4(tmp.d, top, powerbuf, wvalue);\n while (bits >= 0) {\n if (bits < stride)\n stride = bits + 1;\n bits -= stride;\n wvalue = bn_get_bits(p, bits + 1);\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n bits += stride - 5;\n wvalue >>= stride - 5;\n wvalue &= 31;\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5_t4(tmp.d, tmp.d, powerbuf, np, n0, top,\n wvalue);\n }\n bn_flip_t4(tmp.d, tmp.d, top);\n top *= 2;\n tmp.top = top;\n bn_correct_top(&tmp);\n OPENSSL_cleanse(np, top * sizeof(BN_ULONG));\n } else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window == 5 && top > 1) {\n void bn_mul_mont_gather5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_scatter5(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5(BN_ULONG *out, size_t num, void *table, size_t power);\n void bn_power5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n int bn_get_bits5(const BN_ULONG *ap, int off);\n int bn_from_montgomery(BN_ULONG *rp, const BN_ULONG *ap,\n const BN_ULONG *not_used, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n BN_ULONG *n0 = mont->n0, *np;\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n for (np = am.d + top, i = 0; i < top; i++)\n np[i] = mont->N.d[i];\n bn_scatter5(tmp.d, top, powerbuf, 0);\n bn_scatter5(am.d, am.top, powerbuf, 1);\n bn_mul_mont(tmp.d, am.d, am.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2);\n# if 0\n for (i = 3; i < 32; i++) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# else\n for (i = 4; i < 32; i *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n for (i = 3; i < 8; i += 2) {\n int j;\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n for (j = 2 * i; j < 32; j *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, j);\n }\n }\n for (; i < 16; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2 * i);\n }\n for (; i < 32; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# endif\n bits--;\n for (wvalue = 0, i = bits % 5; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_gather5(tmp.d, top, powerbuf, wvalue);\n if (top & 7)\n while (bits >= 0) {\n for (wvalue = 0, i = 0; i < 5; i++, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5(tmp.d, tmp.d, powerbuf, np, n0, top,\n wvalue);\n } else {\n while (bits >= 0) {\n wvalue = bn_get_bits5(p->d, bits - 4);\n bits -= 5;\n bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top, wvalue);\n }\n }\n ret = bn_from_montgomery(tmp.d, tmp.d, NULL, np, n0, top);\n tmp.top = top;\n bn_correct_top(&tmp);\n if (ret) {\n if (!BN_copy(rr, &tmp))\n ret = 0;\n goto err;\n }\n } else\n#endif\n {\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 0, window))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, window))\n goto err;\n if (window > 1) {\n if (!BN_mod_mul_montgomery(&tmp, &am, &am, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 2,\n window))\n goto err;\n for (i = 3; i < numPowers; i++) {\n if (!BN_mod_mul_montgomery(&tmp, &am, &tmp, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, i,\n window))\n goto err;\n }\n }\n bits--;\n for (wvalue = 0, i = bits % window; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&tmp, top, powerbuf, wvalue,\n window))\n goto err;\n while (bits >= 0) {\n wvalue = 0;\n for (i = 0; i < window; i++, bits--) {\n if (!BN_mod_mul_montgomery(&tmp, &tmp, &tmp, mont, ctx))\n goto err;\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n }\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&am, top, powerbuf, wvalue,\n window))\n goto err;\n if (!BN_mod_mul_montgomery(&tmp, &tmp, &am, mont, ctx))\n goto err;\n }\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n am.d[0] = 1;\n for (i = 1; i < top; i++)\n am.d[i] = 0;\n if (!BN_mod_mul_montgomery(rr, &tmp, &am, mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, &tmp, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n if (powerbuf != NULL) {\n OPENSSL_cleanse(powerbuf, powerbufLen);\n OPENSSL_free(powerbufFree);\n }\n BN_CTX_end(ctx);\n return ret;\n}', 'static int MOD_EXP_CTIME_COPY_FROM_PREBUF(BIGNUM *b, int top,\n unsigned char *buf, int idx,\n int window)\n{\n int i, j;\n int width = 1 << window;\n volatile BN_ULONG *table = (volatile BN_ULONG *)buf;\n if (bn_wexpand(b, top) == NULL)\n return 0;\n if (window <= 3) {\n for (i = 0; i < top; i++, table += width) {\n BN_ULONG acc = 0;\n for (j = 0; j < width; j++) {\n acc |= table[j] &\n ((BN_ULONG)0 - (constant_time_eq_int(j,idx)&1));\n }\n b->d[i] = acc;\n }\n } else {\n int xstride = 1 << (window - 2);\n BN_ULONG y0, y1, y2, y3;\n i = idx >> (window - 2);\n idx &= xstride - 1;\n y0 = (BN_ULONG)0 - (constant_time_eq_int(i,0)&1);\n y1 = (BN_ULONG)0 - (constant_time_eq_int(i,1)&1);\n y2 = (BN_ULONG)0 - (constant_time_eq_int(i,2)&1);\n y3 = (BN_ULONG)0 - (constant_time_eq_int(i,3)&1);\n for (i = 0; i < top; i++, table += width) {\n BN_ULONG acc = 0;\n for (j = 0; j < xstride; j++) {\n acc |= ( (table[j + 0 * xstride] & y0) |\n (table[j + 1 * xstride] & y1) |\n (table[j + 2 * xstride] & y2) |\n (table[j + 3 * xstride] & y3) )\n & ((BN_ULONG)0 - (constant_time_eq_int(j,idx)&1));\n }\n b->d[i] = acc;\n }\n }\n b->top = top;\n bn_correct_top(b);\n return 1;\n}', 'void bn_correct_top(BIGNUM *a)\n{\n BN_ULONG *ftl;\n int tmp_top = a->top;\n if (tmp_top > 0) {\n for (ftl = &(a->d[tmp_top]); tmp_top > 0; tmp_top--) {\n ftl--;\n if (*ftl != 0)\n break;\n }\n a->top = tmp_top;\n }\n if (a->top == 0)\n a->neg = 0;\n bn_pollute(a);\n}', 'int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n BN_MONT_CTX *mont, BN_CTX *ctx)\n{\n BIGNUM *tmp;\n int ret = 0;\n#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\n int num = mont->N.top;\n if (num > 1 && a->top == num && b->top == num) {\n if (bn_wexpand(r, num) == NULL)\n return 0;\n if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {\n r->neg = a->neg ^ b->neg;\n r->top = num;\n bn_correct_top(r);\n return 1;\n }\n }\n#endif\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n if (tmp == NULL)\n goto err;\n bn_check_top(tmp);\n if (a == b) {\n if (!BN_sqr(tmp, a, ctx))\n goto err;\n } else {\n if (!BN_mul(tmp, a, b, ctx))\n goto err;\n }\n#ifdef MONT_WORD\n if (!BN_from_montgomery_word(r, tmp, mont))\n goto err;\n#else\n if (!BN_from_montgomery(r, tmp, mont, ctx))\n goto err;\n#endif\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int max, al;\n int ret = 0;\n BIGNUM *tmp, *rr;\n bn_check_top(a);\n al = a->top;\n if (al <= 0) {\n r->top = 0;\n r->neg = 0;\n return 1;\n }\n BN_CTX_start(ctx);\n rr = (a != r) ? r : BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (rr == NULL || tmp == NULL)\n goto err;\n max = 2 * al;\n if (bn_wexpand(rr, max) == NULL)\n goto err;\n if (al == 4) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[8];\n bn_sqr_normal(rr->d, a->d, 4, t);\n#else\n bn_sqr_comba4(rr->d, a->d);\n#endif\n } else if (al == 8) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[16];\n bn_sqr_normal(rr->d, a->d, 8, t);\n#else\n bn_sqr_comba8(rr->d, a->d);\n#endif\n } else {\n#if defined(BN_RECURSION)\n if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {\n BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];\n bn_sqr_normal(rr->d, a->d, al, t);\n } else {\n int j, k;\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n if (al == j) {\n if (bn_wexpand(tmp, k * 2) == NULL)\n goto err;\n bn_sqr_recursive(rr->d, a->d, al, tmp->d);\n } else {\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n }\n }\n#else\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n#endif\n }\n rr->neg = 0;\n if (a->d[al - 1] == (a->d[al - 1] & BN_MASK2l))\n rr->top = max - 1;\n else\n rr->top = max;\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(rr);\n bn_check_top(tmp);\n BN_CTX_end(ctx);\n return ret;\n}', 'void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)\n{\n int i, j, max;\n const BN_ULONG *ap;\n BN_ULONG *rp;\n max = n * 2;\n ap = a;\n rp = r;\n rp[0] = rp[max - 1] = 0;\n rp++;\n j = n;\n if (--j > 0) {\n ap++;\n rp[j] = bn_mul_words(rp, ap, j, ap[-1]);\n rp += 2;\n }\n for (i = n - 2; i > 0; i--) {\n j--;\n ap++;\n rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);\n rp += 2;\n }\n bn_add_words(r, r, r, max);\n bn_sqr_words(tmp, a, n);\n bn_add_words(r, r, tmp, max);\n}']
2,370
0
https://github.com/openssl/openssl/blob/1fac96e4d6484a517f2ebe99b72016726391723c/apps/apps.c/#L272
int chopup_args(ARGS *arg, char *buf, int *argc, char **argv[]) { int num,len,i; char *p; *argc=0; *argv=NULL; len=strlen(buf); i=0; if (arg->count == 0) { arg->count=20; arg->data=(char **)Malloc(sizeof(char *)*arg->count); } for (i=0; i<arg->count; i++) arg->data[i]=NULL; num=0; p=buf; for (;;) { if (!*p) break; while (*p && ((*p == ' ') || (*p == '\t') || (*p == '\n'))) p++; if (!*p) break; if (num >= arg->count) { arg->count+=20; arg->data=(char **)Realloc(arg->data, sizeof(char *)*arg->count); if (argc == 0) return(0); } arg->data[num++]=p; if ((*p == '\'') || (*p == '\"')) { i= *(p++); arg->data[num-1]++; while (*p && (*p != i)) p++; *p='\0'; } else { while (*p && ((*p != ' ') && (*p != '\t') && (*p != '\n'))) p++; if (*p == '\0') p--; else *p='\0'; } p++; } *argc=num; *argv=arg->data; return(1); }
['int chopup_args(ARGS *arg, char *buf, int *argc, char **argv[])\n\t{\n\tint num,len,i;\n\tchar *p;\n\t*argc=0;\n\t*argv=NULL;\n\tlen=strlen(buf);\n\ti=0;\n\tif (arg->count == 0)\n\t\t{\n\t\targ->count=20;\n\t\targ->data=(char **)Malloc(sizeof(char *)*arg->count);\n\t\t}\n\tfor (i=0; i<arg->count; i++)\n\t\targ->data[i]=NULL;\n\tnum=0;\n\tp=buf;\n\tfor (;;)\n\t\t{\n\t\tif (!*p) break;\n\t\twhile (*p && ((*p == \' \') || (*p == \'\\t\') || (*p == \'\\n\')))\n\t\t\tp++;\n\t\tif (!*p) break;\n\t\tif (num >= arg->count)\n\t\t\t{\n\t\t\targ->count+=20;\n\t\t\targ->data=(char **)Realloc(arg->data,\n\t\t\t\tsizeof(char *)*arg->count);\n\t\t\tif (argc == 0) return(0);\n\t\t\t}\n\t\targ->data[num++]=p;\n\t\tif ((*p == \'\\\'\') || (*p == \'\\"\'))\n\t\t\t{\n\t\t\ti= *(p++);\n\t\t\targ->data[num-1]++;\n\t\t\twhile (*p && (*p != i))\n\t\t\t\tp++;\n\t\t\t*p=\'\\0\';\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\twhile (*p && ((*p != \' \') &&\n\t\t\t\t(*p != \'\\t\') && (*p != \'\\n\')))\n\t\t\t\tp++;\n\t\t\tif (*p == \'\\0\')\n\t\t\t\tp--;\n\t\t\telse\n\t\t\t\t*p=\'\\0\';\n\t\t\t}\n\t\tp++;\n\t\t}\n\t*argc=num;\n\t*argv=arg->data;\n\treturn(1);\n\t}']
2,371
0
https://github.com/openssl/openssl/blob/92eb4c551d7433ba1e74e77001dab2e256f8a870/crypto/bn/bn_ctx.c/#L442
static void BN_POOL_release(BN_POOL *p, unsigned int num) { unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE; p->used -= num; while(num--) { bn_check_top(p->current->vals + offset); if(!offset) { offset = BN_CTX_POOL_SIZE - 1; p->current = p->current->prev; } else offset--; } }
['int BN_is_prime_fasttest_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,\n\t\tint do_trial_division, BN_GENCB *cb)\n\t{\n\tint i, j, ret = -1;\n\tint k;\n\tBN_CTX *ctx = NULL;\n\tBIGNUM *A1, *A1_odd, *check;\n\tBN_MONT_CTX *mont = NULL;\n\tconst BIGNUM *A = NULL;\n\tif (BN_cmp(a, BN_value_one()) <= 0)\n\t\treturn 0;\n\tif (checks == BN_prime_checks)\n\t\tchecks = BN_prime_checks_for_size(BN_num_bits(a));\n\tif (!BN_is_odd(a))\n\t\treturn BN_is_word(a, 2);\n\tif (do_trial_division)\n\t\t{\n\t\tfor (i = 1; i < NUMPRIMES; i++)\n\t\t\tif (BN_mod_word(a, primes[i]) == 0)\n\t\t\t\treturn 0;\n\t\tif(!BN_GENCB_call(cb, 1, -1))\n\t\t\tgoto err;\n\t\t}\n\tif (ctx_passed != NULL)\n\t\tctx = ctx_passed;\n\telse\n\t\tif ((ctx=BN_CTX_new()) == NULL)\n\t\t\tgoto err;\n\tBN_CTX_start(ctx);\n\tif (a->neg)\n\t\t{\n\t\tBIGNUM *t;\n\t\tif ((t = BN_CTX_get(ctx)) == NULL) goto err;\n\t\tBN_copy(t, a);\n\t\tt->neg = 0;\n\t\tA = t;\n\t\t}\n\telse\n\t\tA = a;\n\tA1 = BN_CTX_get(ctx);\n\tA1_odd = BN_CTX_get(ctx);\n\tcheck = BN_CTX_get(ctx);\n\tif (check == NULL) goto err;\n\tif (!BN_copy(A1, A))\n\t\tgoto err;\n\tif (!BN_sub_word(A1, 1))\n\t\tgoto err;\n\tif (BN_is_zero(A1))\n\t\t{\n\t\tret = 0;\n\t\tgoto err;\n\t\t}\n\tk = 1;\n\twhile (!BN_is_bit_set(A1, k))\n\t\tk++;\n\tif (!BN_rshift(A1_odd, A1, k))\n\t\tgoto err;\n\tmont = BN_MONT_CTX_new();\n\tif (mont == NULL)\n\t\tgoto err;\n\tif (!BN_MONT_CTX_set(mont, A, ctx))\n\t\tgoto err;\n\tfor (i = 0; i < checks; i++)\n\t\t{\n\t\tif (!BN_pseudo_rand_range(check, A1))\n\t\t\tgoto err;\n\t\tif (!BN_add_word(check, 1))\n\t\t\tgoto err;\n\t\tj = witness(check, A, A1, A1_odd, k, ctx, mont);\n\t\tif (j == -1) goto err;\n\t\tif (j)\n\t\t\t{\n\t\t\tret=0;\n\t\t\tgoto err;\n\t\t\t}\n\t\tif(!BN_GENCB_call(cb, 1, i))\n\t\t\tgoto err;\n\t\t}\n\tret=1;\nerr:\n\tif (ctx != NULL)\n\t\t{\n\t\tBN_CTX_end(ctx);\n\t\tif (ctx_passed == NULL)\n\t\t\tBN_CTX_free(ctx);\n\t\t}\n\tif (mont != NULL)\n\t\tBN_MONT_CTX_free(mont);\n\treturn(ret);\n\t}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n\t{\n\tBIGNUM *ret;\n\tCTXDBG_ENTRY("BN_CTX_get", ctx);\n\tif(ctx->err_stack || ctx->too_many) return NULL;\n\tif((ret = BN_POOL_get(&ctx->pool)) == NULL)\n\t\t{\n\t\tctx->too_many = 1;\n\t\tBNerr(BN_F_BN_CTX_GET,BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n\t\treturn NULL;\n\t\t}\n\tBN_zero(ret);\n\tctx->used++;\n\tCTXDBG_RET(ctx, ret);\n\treturn ret;\n\t}', 'int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)\n\t{\n\tint ret = 0;\n\tBIGNUM *Ri,*R;\n\tBN_CTX_start(ctx);\n\tif((Ri = BN_CTX_get(ctx)) == NULL) goto err;\n\tR= &(mont->RR);\n\tif (!BN_copy(&(mont->N),mod)) goto err;\n\tmont->N.neg = 0;\n#ifdef MONT_WORD\n\t\t{\n\t\tBIGNUM tmod;\n\t\tBN_ULONG buf[2];\n\t\tBN_init(&tmod);\n\t\ttmod.d=buf;\n\t\ttmod.dmax=2;\n\t\ttmod.neg=0;\n\t\tmont->ri=(BN_num_bits(mod)+(BN_BITS2-1))/BN_BITS2*BN_BITS2;\n#if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)\n\t\tBN_zero(R);\n\t\tif (!(BN_set_bit(R,2*BN_BITS2))) goto err;\n\t\t\t\t\t\t\t\ttmod.top=0;\n\t\tif ((buf[0] = mod->d[0]))\t\t\ttmod.top=1;\n\t\tif ((buf[1] = mod->top>1 ? mod->d[1] : 0))\ttmod.top=2;\n\t\tif ((BN_mod_inverse(Ri,R,&tmod,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tif (!BN_lshift(Ri,Ri,2*BN_BITS2)) goto err;\n\t\tif (!BN_is_zero(Ri))\n\t\t\t{\n\t\t\tif (!BN_sub_word(Ri,1)) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (bn_expand(Ri,(int)sizeof(BN_ULONG)*2) == NULL)\n\t\t\t\tgoto err;\n\t\t\tRi->neg=0;\n\t\t\tRi->d[0]=BN_MASK2;\n\t\t\tRi->d[1]=BN_MASK2;\n\t\t\tRi->top=2;\n\t\t\t}\n\t\tif (!BN_div(Ri,NULL,Ri,&tmod,ctx)) goto err;\n\t\tmont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n\t\tmont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;\n#else\n\t\tBN_zero(R);\n\t\tif (!(BN_set_bit(R,BN_BITS2))) goto err;\n\t\tbuf[0]=mod->d[0];\n\t\tbuf[1]=0;\n\t\ttmod.top = buf[0] != 0 ? 1 : 0;\n\t\tif ((BN_mod_inverse(Ri,R,&tmod,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tif (!BN_lshift(Ri,Ri,BN_BITS2)) goto err;\n\t\tif (!BN_is_zero(Ri))\n\t\t\t{\n\t\t\tif (!BN_sub_word(Ri,1)) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (!BN_set_word(Ri,BN_MASK2)) goto err;\n\t\t\t}\n\t\tif (!BN_div(Ri,NULL,Ri,&tmod,ctx)) goto err;\n\t\tmont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n\t\tmont->n0[1] = 0;\n#endif\n\t\t}\n#else\n\t\t{\n\t\tmont->ri=BN_num_bits(&mont->N);\n\t\tBN_zero(R);\n\t\tif (!BN_set_bit(R,mont->ri)) goto err;\n\t\tif ((BN_mod_inverse(Ri,R,&mont->N,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tif (!BN_lshift(Ri,Ri,mont->ri)) goto err;\n\t\tif (!BN_sub_word(Ri,1)) goto err;\n\t\tif (!BN_div(&(mont->Ni),NULL,Ri,&mont->N,ctx)) goto err;\n\t\t}\n#endif\n\tBN_zero(&(mont->RR));\n\tif (!BN_set_bit(&(mont->RR),mont->ri*2)) goto err;\n\tif (!BN_mod(&(mont->RR),&(mont->RR),&(mont->N),ctx)) goto err;\n\tret = 1;\nerr:\n\tBN_CTX_end(ctx);\n\treturn ret;\n\t}', 'BIGNUM *BN_mod_inverse(BIGNUM *in,\n\tconst BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n\t{\n\tBIGNUM *rv;\n\tint noinv;\n\trv = int_bn_mod_inverse(in, a, n, ctx, &noinv);\n\tif (noinv)\n\t\tBNerr(BN_F_BN_MOD_INVERSE,BN_R_NO_INVERSE);\n\treturn rv;\n\t}', 'BIGNUM *int_bn_mod_inverse(BIGNUM *in,\n\tconst BIGNUM *a, const BIGNUM *n, BN_CTX *ctx, int *pnoinv)\n\t{\n\tBIGNUM *A,*B,*X,*Y,*M,*D,*T,*R=NULL;\n\tBIGNUM *ret=NULL;\n\tint sign;\n\tif (pnoinv)\n\t\t*pnoinv = 0;\n\tif ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0))\n\t\t{\n\t\treturn BN_mod_inverse_no_branch(in, a, n, ctx);\n\t\t}\n\tbn_check_top(a);\n\tbn_check_top(n);\n\tBN_CTX_start(ctx);\n\tA = BN_CTX_get(ctx);\n\tB = BN_CTX_get(ctx);\n\tX = BN_CTX_get(ctx);\n\tD = BN_CTX_get(ctx);\n\tM = BN_CTX_get(ctx);\n\tY = BN_CTX_get(ctx);\n\tT = BN_CTX_get(ctx);\n\tif (T == NULL) goto err;\n\tif (in == NULL)\n\t\tR=BN_new();\n\telse\n\t\tR=in;\n\tif (R == NULL) goto err;\n\tBN_one(X);\n\tBN_zero(Y);\n\tif (BN_copy(B,a) == NULL) goto err;\n\tif (BN_copy(A,n) == NULL) goto err;\n\tA->neg = 0;\n\tif (B->neg || (BN_ucmp(B, A) >= 0))\n\t\t{\n\t\tif (!BN_nnmod(B, B, A, ctx)) goto err;\n\t\t}\n\tsign = -1;\n\tif (BN_is_odd(n) && (BN_num_bits(n) <= (BN_BITS <= 32 ? 450 : 2048)))\n\t\t{\n\t\tint shift;\n\t\twhile (!BN_is_zero(B))\n\t\t\t{\n\t\t\tshift = 0;\n\t\t\twhile (!BN_is_bit_set(B, shift))\n\t\t\t\t{\n\t\t\t\tshift++;\n\t\t\t\tif (BN_is_odd(X))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_uadd(X, X, n)) goto err;\n\t\t\t\t\t}\n\t\t\t\tif (!BN_rshift1(X, X)) goto err;\n\t\t\t\t}\n\t\t\tif (shift > 0)\n\t\t\t\t{\n\t\t\t\tif (!BN_rshift(B, B, shift)) goto err;\n\t\t\t\t}\n\t\t\tshift = 0;\n\t\t\twhile (!BN_is_bit_set(A, shift))\n\t\t\t\t{\n\t\t\t\tshift++;\n\t\t\t\tif (BN_is_odd(Y))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_uadd(Y, Y, n)) goto err;\n\t\t\t\t\t}\n\t\t\t\tif (!BN_rshift1(Y, Y)) goto err;\n\t\t\t\t}\n\t\t\tif (shift > 0)\n\t\t\t\t{\n\t\t\t\tif (!BN_rshift(A, A, shift)) goto err;\n\t\t\t\t}\n\t\t\tif (BN_ucmp(B, A) >= 0)\n\t\t\t\t{\n\t\t\t\tif (!BN_uadd(X, X, Y)) goto err;\n\t\t\t\tif (!BN_usub(B, B, A)) goto err;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (!BN_uadd(Y, Y, X)) goto err;\n\t\t\t\tif (!BN_usub(A, A, B)) goto err;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\twhile (!BN_is_zero(B))\n\t\t\t{\n\t\t\tBIGNUM *tmp;\n\t\t\tif (BN_num_bits(A) == BN_num_bits(B))\n\t\t\t\t{\n\t\t\t\tif (!BN_one(D)) goto err;\n\t\t\t\tif (!BN_sub(M,A,B)) goto err;\n\t\t\t\t}\n\t\t\telse if (BN_num_bits(A) == BN_num_bits(B) + 1)\n\t\t\t\t{\n\t\t\t\tif (!BN_lshift1(T,B)) goto err;\n\t\t\t\tif (BN_ucmp(A,T) < 0)\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_one(D)) goto err;\n\t\t\t\t\tif (!BN_sub(M,A,B)) goto err;\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_sub(M,A,T)) goto err;\n\t\t\t\t\tif (!BN_add(D,T,B)) goto err;\n\t\t\t\t\tif (BN_ucmp(A,D) < 0)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tif (!BN_set_word(D,2)) goto err;\n\t\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t\t{\n\t\t\t\t\t\tif (!BN_set_word(D,3)) goto err;\n\t\t\t\t\t\tif (!BN_sub(M,M,B)) goto err;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (!BN_div(D,M,A,B,ctx)) goto err;\n\t\t\t\t}\n\t\t\ttmp=A;\n\t\t\tA=B;\n\t\t\tB=M;\n\t\t\tif (BN_is_one(D))\n\t\t\t\t{\n\t\t\t\tif (!BN_add(tmp,X,Y)) goto err;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (BN_is_word(D,2))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_lshift1(tmp,X)) goto err;\n\t\t\t\t\t}\n\t\t\t\telse if (BN_is_word(D,4))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_lshift(tmp,X,2)) goto err;\n\t\t\t\t\t}\n\t\t\t\telse if (D->top == 1)\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_copy(tmp,X)) goto err;\n\t\t\t\t\tif (!BN_mul_word(tmp,D->d[0])) goto err;\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_mul(tmp,D,X,ctx)) goto err;\n\t\t\t\t\t}\n\t\t\t\tif (!BN_add(tmp,tmp,Y)) goto err;\n\t\t\t\t}\n\t\t\tM=Y;\n\t\t\tY=X;\n\t\t\tX=tmp;\n\t\t\tsign = -sign;\n\t\t\t}\n\t\t}\n\tif (sign < 0)\n\t\t{\n\t\tif (!BN_sub(Y,n,Y)) goto err;\n\t\t}\n\tif (BN_is_one(A))\n\t\t{\n\t\tif (!Y->neg && BN_ucmp(Y,n) < 0)\n\t\t\t{\n\t\t\tif (!BN_copy(R,Y)) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (!BN_nnmod(R,Y,n,ctx)) goto err;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tif (pnoinv)\n\t\t\t*pnoinv = 1;\n\t\tgoto err;\n\t\t}\n\tret=R;\nerr:\n\tif ((ret == NULL) && (in == NULL)) BN_free(R);\n\tBN_CTX_end(ctx);\n\tbn_check_top(ret);\n\treturn(ret);\n\t}', 'static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,\n\tconst BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n\t{\n\tBIGNUM *A,*B,*X,*Y,*M,*D,*T,*R=NULL;\n\tBIGNUM local_A, local_B;\n\tBIGNUM *pA, *pB;\n\tBIGNUM *ret=NULL;\n\tint sign;\n\tbn_check_top(a);\n\tbn_check_top(n);\n\tBN_CTX_start(ctx);\n\tA = BN_CTX_get(ctx);\n\tB = BN_CTX_get(ctx);\n\tX = BN_CTX_get(ctx);\n\tD = BN_CTX_get(ctx);\n\tM = BN_CTX_get(ctx);\n\tY = BN_CTX_get(ctx);\n\tT = BN_CTX_get(ctx);\n\tif (T == NULL) goto err;\n\tif (in == NULL)\n\t\tR=BN_new();\n\telse\n\t\tR=in;\n\tif (R == NULL) goto err;\n\tBN_one(X);\n\tBN_zero(Y);\n\tif (BN_copy(B,a) == NULL) goto err;\n\tif (BN_copy(A,n) == NULL) goto err;\n\tA->neg = 0;\n\tif (B->neg || (BN_ucmp(B, A) >= 0))\n\t\t{\n\t\tpB = &local_B;\n\t\tBN_with_flags(pB, B, BN_FLG_CONSTTIME);\n\t\tif (!BN_nnmod(B, pB, A, ctx)) goto err;\n\t\t}\n\tsign = -1;\n\twhile (!BN_is_zero(B))\n\t\t{\n\t\tBIGNUM *tmp;\n\t\tpA = &local_A;\n\t\tBN_with_flags(pA, A, BN_FLG_CONSTTIME);\n\t\tif (!BN_div(D,M,pA,B,ctx)) goto err;\n\t\ttmp=A;\n\t\tA=B;\n\t\tB=M;\n\t\tif (!BN_mul(tmp,D,X,ctx)) goto err;\n\t\tif (!BN_add(tmp,tmp,Y)) goto err;\n\t\tM=Y;\n\t\tY=X;\n\t\tX=tmp;\n\t\tsign = -sign;\n\t\t}\n\tif (sign < 0)\n\t\t{\n\t\tif (!BN_sub(Y,n,Y)) goto err;\n\t\t}\n\tif (BN_is_one(A))\n\t\t{\n\t\tif (!Y->neg && BN_ucmp(Y,n) < 0)\n\t\t\t{\n\t\t\tif (!BN_copy(R,Y)) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (!BN_nnmod(R,Y,n,ctx)) goto err;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_INVERSE_NO_BRANCH,BN_R_NO_INVERSE);\n\t\tgoto err;\n\t\t}\n\tret=R;\nerr:\n\tif ((ret == NULL) && (in == NULL)) BN_free(R);\n\tBN_CTX_end(ctx);\n\tbn_check_top(ret);\n\treturn(ret);\n\t}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n\t{\n\tif (!(BN_mod(r,m,d,ctx)))\n\t\treturn 0;\n\tif (!r->neg)\n\t\treturn 1;\n\treturn (d->neg ? BN_sub : BN_add)(r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n\t BN_CTX *ctx)\n\t{\n\tint norm_shift,i,loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tint num_n,div_n;\n\tif (num->top > 0 && num->d[num->top - 1] == 0)\n\t\t{\n\t\tBNerr(BN_F_BN_DIV,BN_R_NOT_INITIALIZED);\n\t\treturn 0;\n\t\t}\n\tbn_check_top(num);\n\tif ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0))\n\t\t{\n\t\treturn BN_div_no_branch(dv, rm, num, divisor, ctx);\n\t\t}\n\tbn_check_top(dv);\n\tbn_check_top(rm);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\n\t\t}\n\tif (BN_ucmp(num,divisor) < 0)\n\t\t{\n\t\tif (rm != NULL)\n\t\t\t{ if (BN_copy(rm,num) == NULL) return(0); }\n\t\tif (dv != NULL) BN_zero(dv);\n\t\treturn(1);\n\t\t}\n\tBN_CTX_start(ctx);\n\ttmp=BN_CTX_get(ctx);\n\tsnum=BN_CTX_get(ctx);\n\tsdiv=BN_CTX_get(ctx);\n\tif (dv == NULL)\n\t\tres=BN_CTX_get(ctx);\n\telse\tres=dv;\n\tif (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)\n\t\tgoto err;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tif (!(BN_lshift(sdiv,divisor,norm_shift))) goto err;\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tif (!(BN_lshift(snum,num,norm_shift))) goto err;\n\tsnum->neg=0;\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\twnum.neg = 0;\n\twnum.d = &(snum->d[loop]);\n\twnum.top = div_n;\n\twnum.dmax = snum->dmax - loop;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (BN_ucmp(&wnum,sdiv) >= 0)\n\t\t{\n\t\tbn_clear_top2max(&wnum);\n\t\tbn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n\t\t*resp=1;\n\t\t}\n\telse\n\t\tres->top--;\n\tif (res->top == 0)\n\t\tres->neg = 0;\n\telse\n\t\tresp--;\n\tfor (i=0; i<loop-1; i++, wnump--, resp--)\n\t\t{\n\t\tBN_ULONG q,l0;\n#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n\t\tBN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG);\n\t\tq=bn_div_3_words(wnump,d1,d0);\n#else\n\t\tBN_ULONG n0,n1,rem=0;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\t{\n#ifdef BN_LLONG\n\t\t\tBN_ULLONG t2;\n#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n\t\t\tq=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);\n#else\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tt2 -= d1;\n\t\t\t\t}\n#else\n\t\t\tBN_ULONG t2l,t2h;\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#if defined(BN_UMULT_LOHI)\n\t\t\tBN_UMULT_LOHI(t2l,t2h,d1,q);\n#elif defined(BN_UMULT_HIGH)\n\t\t\tt2l = d1 * q;\n\t\t\tt2h = BN_UMULT_HIGH(d1,q);\n#else\n\t\t\t{\n\t\t\tBN_ULONG ql, qh;\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n\t\t\t}\n#endif\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif ((t2h < rem) ||\n\t\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t\t}\n#endif\n\t\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\twnum.d--;\n\t\tif (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1))\n\t\t\t{\n\t\t\tq--;\n\t\t\tif (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n\t\t\t\t(*wnump)++;\n\t\t\t}\n\t\t*resp = q;\n\t\t}\n\tbn_correct_top(snum);\n\tif (rm != NULL)\n\t\t{\n\t\tint neg = num->neg;\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\tif (!BN_is_zero(rm))\n\t\t\trm->neg = neg;\n\t\tbn_check_top(rm);\n\t\t}\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'static int BN_div_no_branch(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,\n\tconst BIGNUM *divisor, BN_CTX *ctx)\n\t{\n\tint norm_shift,i,loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tint num_n,div_n;\n\tbn_check_top(dv);\n\tbn_check_top(rm);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV_NO_BRANCH,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\n\t\t}\n\tBN_CTX_start(ctx);\n\ttmp=BN_CTX_get(ctx);\n\tsnum=BN_CTX_get(ctx);\n\tsdiv=BN_CTX_get(ctx);\n\tif (dv == NULL)\n\t\tres=BN_CTX_get(ctx);\n\telse\tres=dv;\n\tif (sdiv == NULL || res == NULL) goto err;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tif (!(BN_lshift(sdiv,divisor,norm_shift))) goto err;\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tif (!(BN_lshift(snum,num,norm_shift))) goto err;\n\tsnum->neg=0;\n\tif (snum->top <= sdiv->top+1)\n\t\t{\n\t\tif (bn_wexpand(snum, sdiv->top + 2) == NULL) goto err;\n\t\tfor (i = snum->top; i < sdiv->top + 2; i++) snum->d[i] = 0;\n\t\tsnum->top = sdiv->top + 2;\n\t\t}\n\telse\n\t\t{\n\t\tif (bn_wexpand(snum, snum->top + 1) == NULL) goto err;\n\t\tsnum->d[snum->top] = 0;\n\t\tsnum->top ++;\n\t\t}\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\twnum.neg = 0;\n\twnum.d = &(snum->d[loop]);\n\twnum.top = div_n;\n\twnum.dmax = snum->dmax - loop;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop-1;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (res->top == 0)\n\t\tres->neg = 0;\n\telse\n\t\tresp--;\n\tfor (i=0; i<loop-1; i++, wnump--, resp--)\n\t\t{\n\t\tBN_ULONG q,l0;\n#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n\t\tBN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG);\n\t\tq=bn_div_3_words(wnump,d1,d0);\n#else\n\t\tBN_ULONG n0,n1,rem=0;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\t{\n#ifdef BN_LLONG\n\t\t\tBN_ULLONG t2;\n#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n\t\t\tq=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);\n#else\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tt2 -= d1;\n\t\t\t\t}\n#else\n\t\t\tBN_ULONG t2l,t2h;\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#if defined(BN_UMULT_LOHI)\n\t\t\tBN_UMULT_LOHI(t2l,t2h,d1,q);\n#elif defined(BN_UMULT_HIGH)\n\t\t\tt2l = d1 * q;\n\t\t\tt2h = BN_UMULT_HIGH(d1,q);\n#else\n\t\t\t{\n\t\t\tBN_ULONG ql, qh;\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n\t\t\t}\n#endif\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif ((t2h < rem) ||\n\t\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t\t}\n#endif\n\t\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\twnum.d--;\n\t\tif (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1))\n\t\t\t{\n\t\t\tq--;\n\t\t\tif (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n\t\t\t\t(*wnump)++;\n\t\t\t}\n\t\t*resp = q;\n\t\t}\n\tbn_correct_top(snum);\n\tif (rm != NULL)\n\t\t{\n\t\tint neg = num->neg;\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\tif (!BN_is_zero(rm))\n\t\t\trm->neg = neg;\n\t\tbn_check_top(rm);\n\t\t}\n\tbn_correct_top(res);\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'void BN_CTX_end(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_end", ctx);\n\tif(ctx->err_stack)\n\t\tctx->err_stack--;\n\telse\n\t\t{\n\t\tunsigned int fp = BN_STACK_pop(&ctx->stack);\n\t\tif(fp < ctx->used)\n\t\t\tBN_POOL_release(&ctx->pool, ctx->used - fp);\n\t\tctx->used = fp;\n\t\tctx->too_many = 0;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'static void BN_POOL_release(BN_POOL *p, unsigned int num)\n\t{\n\tunsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE;\n\tp->used -= num;\n\twhile(num--)\n\t\t{\n\t\tbn_check_top(p->current->vals + offset);\n\t\tif(!offset)\n\t\t\t{\n\t\t\toffset = BN_CTX_POOL_SIZE - 1;\n\t\t\tp->current = p->current->prev;\n\t\t\t}\n\t\telse\n\t\t\toffset--;\n\t\t}\n\t}']
2,372
0
https://github.com/openssl/openssl/blob/eedb9db9afd531e9dc69a33b73c7e1cd27153b0b/apps/pkcs12.c/#L759
static int get_cert_chain(X509 *cert, X509_STORE *store, STACK_OF(X509) **chain) { X509_STORE_CTX *store_ctx = NULL; STACK_OF(X509) *chn = NULL; int i = 0; store_ctx = X509_STORE_CTX_new(); if (store_ctx == NULL) { i = X509_V_ERR_UNSPECIFIED; goto end; } if (!X509_STORE_CTX_init(store_ctx, store, cert, NULL)) { i = X509_V_ERR_UNSPECIFIED; goto end; } if (X509_verify_cert(store_ctx) > 0) chn = X509_STORE_CTX_get1_chain(store_ctx); else if ((i = X509_STORE_CTX_get_error(store_ctx)) == 0) i = X509_V_ERR_UNSPECIFIED; end: X509_STORE_CTX_free(store_ctx); *chain = chn; return i; }
['static int get_cert_chain(X509 *cert, X509_STORE *store,\n STACK_OF(X509) **chain)\n{\n X509_STORE_CTX *store_ctx = NULL;\n STACK_OF(X509) *chn = NULL;\n int i = 0;\n store_ctx = X509_STORE_CTX_new();\n if (store_ctx == NULL) {\n i = X509_V_ERR_UNSPECIFIED;\n goto end;\n }\n if (!X509_STORE_CTX_init(store_ctx, store, cert, NULL)) {\n i = X509_V_ERR_UNSPECIFIED;\n goto end;\n }\n if (X509_verify_cert(store_ctx) > 0)\n chn = X509_STORE_CTX_get1_chain(store_ctx);\n else if ((i = X509_STORE_CTX_get_error(store_ctx)) == 0)\n i = X509_V_ERR_UNSPECIFIED;\nend:\n X509_STORE_CTX_free(store_ctx);\n *chain = chn;\n return i;\n}', 'X509_STORE_CTX *X509_STORE_CTX_new(void)\n{\n X509_STORE_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));\n if (ctx == NULL) {\n X509err(X509_F_X509_STORE_CTX_NEW, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n return ctx;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n 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}', 'STACK_OF(X509) *X509_STORE_CTX_get1_chain(X509_STORE_CTX *ctx)\n{\n if (!ctx->chain)\n return NULL;\n return X509_chain_up_ref(ctx->chain);\n}', 'void X509_STORE_CTX_free(X509_STORE_CTX *ctx)\n{\n if (ctx == NULL)\n return;\n X509_STORE_CTX_cleanup(ctx);\n OPENSSL_free(ctx);\n}', 'void CRYPTO_free(void *str, const char *file, int line)\n{\n if (free_impl != NULL && free_impl != &CRYPTO_free) {\n free_impl(str, file, line);\n return;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_free(str, 0, file, line);\n free(str);\n CRYPTO_mem_debug_free(str, 1, file, line);\n } else {\n free(str);\n }\n#else\n free(str);\n#endif\n}']
2,373
0
https://github.com/libav/libav/blob/4f0b80599a534dcca57be3184b89b98f82bf2a2c/libavcodec/smacker.c/#L306
static int decode_header_trees(SmackVContext *smk) { GetBitContext gb; int mmap_size, mclr_size, full_size, type_size; mmap_size = AV_RL32(smk->avctx->extradata); mclr_size = AV_RL32(smk->avctx->extradata + 4); full_size = AV_RL32(smk->avctx->extradata + 8); type_size = AV_RL32(smk->avctx->extradata + 12); init_get_bits(&gb, smk->avctx->extradata + 16, (smk->avctx->extradata_size - 16) * 8); if(!get_bits1(&gb)) { av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n"); smk->mmap_tbl = av_malloc(sizeof(int) * 2); smk->mmap_tbl[0] = 0; smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1; } else { smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size); } if(!get_bits1(&gb)) { av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n"); smk->mclr_tbl = av_malloc(sizeof(int) * 2); smk->mclr_tbl[0] = 0; smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1; } else { smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size); } if(!get_bits1(&gb)) { av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n"); smk->full_tbl = av_malloc(sizeof(int) * 2); smk->full_tbl[0] = 0; smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1; } else { smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size); } if(!get_bits1(&gb)) { av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n"); smk->type_tbl = av_malloc(sizeof(int) * 2); smk->type_tbl[0] = 0; smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1; } else { smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size); } return 0; }
['static int decode_header_trees(SmackVContext *smk) {\n GetBitContext gb;\n int mmap_size, mclr_size, full_size, type_size;\n mmap_size = AV_RL32(smk->avctx->extradata);\n mclr_size = AV_RL32(smk->avctx->extradata + 4);\n full_size = AV_RL32(smk->avctx->extradata + 8);\n type_size = AV_RL32(smk->avctx->extradata + 12);\n init_get_bits(&gb, smk->avctx->extradata + 16, (smk->avctx->extradata_size - 16) * 8);\n if(!get_bits1(&gb)) {\n av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\\n");\n smk->mmap_tbl = av_malloc(sizeof(int) * 2);\n smk->mmap_tbl[0] = 0;\n smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;\n } else {\n smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size);\n }\n if(!get_bits1(&gb)) {\n av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\\n");\n smk->mclr_tbl = av_malloc(sizeof(int) * 2);\n smk->mclr_tbl[0] = 0;\n smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;\n } else {\n smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size);\n }\n if(!get_bits1(&gb)) {\n av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\\n");\n smk->full_tbl = av_malloc(sizeof(int) * 2);\n smk->full_tbl[0] = 0;\n smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;\n } else {\n smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size);\n }\n if(!get_bits1(&gb)) {\n av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\\n");\n smk->type_tbl = av_malloc(sizeof(int) * 2);\n smk->type_tbl[0] = 0;\n smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;\n } else {\n smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size);\n }\n return 0;\n}', 'static inline void init_get_bits(GetBitContext *s,\n const uint8_t *buffer, int bit_size)\n{\n int buffer_size = (bit_size+7)>>3;\n if (buffer_size < 0 || bit_size < 0) {\n buffer_size = bit_size = 0;\n buffer = NULL;\n }\n s->buffer = buffer;\n s->size_in_bits = bit_size;\n s->buffer_end = buffer + buffer_size;\n#ifdef ALT_BITSTREAM_READER\n s->index = 0;\n#elif defined A32_BITSTREAM_READER\n s->buffer_ptr = (uint32_t*)((intptr_t)buffer & ~3);\n s->bit_count = 32 + 8*((intptr_t)buffer & 3);\n skip_bits_long(s, 0);\n#endif\n}', 'static inline unsigned int get_bits1(GetBitContext *s){\n#ifdef ALT_BITSTREAM_READER\n unsigned int index = s->index;\n uint8_t result = s->buffer[index>>3];\n#ifdef ALT_BITSTREAM_READER_LE\n result >>= index & 7;\n result &= 1;\n#else\n result <<= index & 7;\n result >>= 8 - 1;\n#endif\n index++;\n s->index = index;\n return result;\n#else\n return get_bits(s, 1);\n#endif\n}', '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}']
2,374
0
https://github.com/openssl/openssl/blob/9dd4ac8cf17f2afd636e85ae0111d1df4104a475/test/bntest.c/#L383
static int test_modexp_mont5() { BIGNUM *a, *p, *m, *d, *e, *b, *n, *c; BN_MONT_CTX *mont; char *bigstring; a = BN_new(); p = BN_new(); m = BN_new(); d = BN_new(); e = BN_new(); b = BN_new(); n = BN_new(); c = BN_new(); mont = BN_MONT_CTX_new(); BN_bntest_rand(m, 1024, 0, 1); BN_bntest_rand(a, 1024, 0, 0); BN_zero(p); if (!BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL)) return 0; if (!BN_is_one(d)) { printf("Modular exponentiation test failed!\n"); return 0; } BN_hex2bn(&a, "7878787878787878787878787878787878787878787878787878787878787878" "7878787878787878787878787878787878787878787878787878787878787878" "7878787878787878787878787878787878787878787878787878787878787878" "7878787878787878787878787878787878787878787878787878787878787878"); BN_hex2bn(&b, "095D72C08C097BA488C5E439C655A192EAFB6380073D8C2664668EDDB4060744" "E16E57FB4EDB9AE10A0CEFCDC28A894F689A128379DB279D48A2E20849D68593" "9B7803BCF46CEBF5C533FB0DD35B080593DE5472E3FE5DB951B8BFF9B4CB8F03" "9CC638A5EE8CDD703719F8000E6A9F63BEED5F2FCD52FF293EA05A251BB4AB81"); BN_hex2bn(&n, "D78AF684E71DB0C39CFF4E64FB9DB567132CB9C50CC98009FEB820B26F2DED9B" "91B9B5E2B83AE0AE4EB4E0523CA726BFBE969B89FD754F674CE99118C3F2D1C5" "D81FDC7C54E02B60262B241D53C040E99E45826ECA37A804668E690E1AFC1CA4" "2C9A15D84D4954425F0B7642FC0BD9D7B24E2618D2DCC9B729D944BADACFDDAF"); BN_MONT_CTX_set(mont, n, ctx); BN_mod_mul_montgomery(c, a, b, mont, ctx); BN_mod_mul_montgomery(d, b, a, mont, ctx); if (BN_cmp(c, d)) { fprintf(stderr, "Montgomery multiplication test failed:" " a*b != b*a.\n"); return 0; } bigstring = glue(bn1strings); BN_hex2bn(&n, bigstring); OPENSSL_free(bigstring); bigstring = glue(bn2strings); BN_hex2bn(&a, bigstring); OPENSSL_free(bigstring); BN_free(b); b = BN_dup(a); BN_MONT_CTX_set(mont, n, ctx); BN_mod_mul_montgomery(c, a, a, mont, ctx); BN_mod_mul_montgomery(d, a, b, mont, ctx); if (BN_cmp(c, d)) { fprintf(stderr, "Montgomery multiplication test failed:" " a**2 != a*a.\n"); return 0; } BN_bntest_rand(p, 1024, 0, 0); BN_zero(a); if (!BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL)) return 0; if (!BN_is_zero(d)) { fprintf(stderr, "Modular exponentiation test failed!\n"); return 0; } BN_one(a); BN_MONT_CTX_set(mont, m, ctx); if (!BN_from_montgomery(e, a, mont, ctx)) return 0; if (!BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL)) return 0; if (!BN_mod_exp_simple(a, e, p, m, ctx)) return 0; if (BN_cmp(a, d) != 0) { printf("Modular exponentiation test failed!\n"); return 0; } BN_bntest_rand(e, 1024, 0, 0); if (!BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL)) return 0; if (!BN_mod_exp_simple(a, e, p, m, ctx)) return 0; if (BN_cmp(a, d) != 0) { printf("Modular exponentiation test failed!\n"); return 0; } BN_MONT_CTX_free(mont); BN_free(a); BN_free(p); BN_free(m); BN_free(d); BN_free(e); BN_free(b); BN_free(n); BN_free(c); return 1; }
['static int test_modexp_mont5()\n{\n BIGNUM *a, *p, *m, *d, *e, *b, *n, *c;\n BN_MONT_CTX *mont;\n char *bigstring;\n a = BN_new();\n p = BN_new();\n m = BN_new();\n d = BN_new();\n e = BN_new();\n b = BN_new();\n n = BN_new();\n c = BN_new();\n mont = BN_MONT_CTX_new();\n BN_bntest_rand(m, 1024, 0, 1);\n BN_bntest_rand(a, 1024, 0, 0);\n BN_zero(p);\n if (!BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))\n return 0;\n if (!BN_is_one(d)) {\n printf("Modular exponentiation test failed!\\n");\n return 0;\n }\n BN_hex2bn(&a,\n "7878787878787878787878787878787878787878787878787878787878787878"\n "7878787878787878787878787878787878787878787878787878787878787878"\n "7878787878787878787878787878787878787878787878787878787878787878"\n "7878787878787878787878787878787878787878787878787878787878787878");\n BN_hex2bn(&b,\n "095D72C08C097BA488C5E439C655A192EAFB6380073D8C2664668EDDB4060744"\n "E16E57FB4EDB9AE10A0CEFCDC28A894F689A128379DB279D48A2E20849D68593"\n "9B7803BCF46CEBF5C533FB0DD35B080593DE5472E3FE5DB951B8BFF9B4CB8F03"\n "9CC638A5EE8CDD703719F8000E6A9F63BEED5F2FCD52FF293EA05A251BB4AB81");\n BN_hex2bn(&n,\n "D78AF684E71DB0C39CFF4E64FB9DB567132CB9C50CC98009FEB820B26F2DED9B"\n "91B9B5E2B83AE0AE4EB4E0523CA726BFBE969B89FD754F674CE99118C3F2D1C5"\n "D81FDC7C54E02B60262B241D53C040E99E45826ECA37A804668E690E1AFC1CA4"\n "2C9A15D84D4954425F0B7642FC0BD9D7B24E2618D2DCC9B729D944BADACFDDAF");\n BN_MONT_CTX_set(mont, n, ctx);\n BN_mod_mul_montgomery(c, a, b, mont, ctx);\n BN_mod_mul_montgomery(d, b, a, mont, ctx);\n if (BN_cmp(c, d)) {\n fprintf(stderr, "Montgomery multiplication test failed:"\n " a*b != b*a.\\n");\n return 0;\n }\n bigstring = glue(bn1strings);\n BN_hex2bn(&n, bigstring);\n OPENSSL_free(bigstring);\n bigstring = glue(bn2strings);\n BN_hex2bn(&a, bigstring);\n OPENSSL_free(bigstring);\n BN_free(b);\n b = BN_dup(a);\n BN_MONT_CTX_set(mont, n, ctx);\n BN_mod_mul_montgomery(c, a, a, mont, ctx);\n BN_mod_mul_montgomery(d, a, b, mont, ctx);\n if (BN_cmp(c, d)) {\n fprintf(stderr, "Montgomery multiplication test failed:"\n " a**2 != a*a.\\n");\n return 0;\n }\n BN_bntest_rand(p, 1024, 0, 0);\n BN_zero(a);\n if (!BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))\n return 0;\n if (!BN_is_zero(d)) {\n fprintf(stderr, "Modular exponentiation test failed!\\n");\n return 0;\n }\n BN_one(a);\n BN_MONT_CTX_set(mont, m, ctx);\n if (!BN_from_montgomery(e, a, mont, ctx))\n return 0;\n if (!BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))\n return 0;\n if (!BN_mod_exp_simple(a, e, p, m, ctx))\n return 0;\n if (BN_cmp(a, d) != 0) {\n printf("Modular exponentiation test failed!\\n");\n return 0;\n }\n BN_bntest_rand(e, 1024, 0, 0);\n if (!BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))\n return 0;\n if (!BN_mod_exp_simple(a, e, p, m, ctx))\n return 0;\n if (BN_cmp(a, d) != 0) {\n printf("Modular exponentiation test failed!\\n");\n return 0;\n }\n BN_MONT_CTX_free(mont);\n BN_free(a);\n BN_free(p);\n BN_free(m);\n BN_free(d);\n BN_free(e);\n BN_free(b);\n BN_free(n);\n BN_free(c);\n return 1;\n}', 'BN_MONT_CTX *BN_MONT_CTX_new(void)\n{\n BN_MONT_CTX *ret;\n if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL)\n return (NULL);\n BN_MONT_CTX_init(ret);\n ret->flags = BN_FLG_MALLOCED;\n return (ret);\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n osslargused(file); osslargused(line);\n ret = malloc(num);\n#endif\n return ret;\n}', 'void BN_MONT_CTX_init(BN_MONT_CTX *ctx)\n{\n ctx->ri = 0;\n bn_init(&(ctx->RR));\n bn_init(&(ctx->N));\n bn_init(&(ctx->Ni));\n ctx->n0[0] = ctx->n0[1] = 0;\n ctx->flags = 0;\n}', 'int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom)\n{\n return bnrand(2, rnd, bits, top, bottom);\n}', 'int BN_is_one(const BIGNUM *a)\n{\n return BN_abs_is_word(a, 1) && !a->neg;\n}', 'int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w)\n{\n return ((a->top == 1) && (a->d[0] == w)) || ((w == 0) && (a->top == 0));\n}']
2,375
0
https://github.com/openssl/openssl/blob/0f0db4dc2520777b889968c93e054693c5c58fdc/crypto/bn/bn_ctx.c/#L372
static void BN_POOL_release(BN_POOL *p, unsigned int num) { unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE; p->used -= num; while (num--) { bn_check_top(p->current->vals + offset); if (offset == 0) { offset = BN_CTX_POOL_SIZE - 1; p->current = p->current->prev; } else offset--; } }
['int BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y,\n BN_RECP_CTX *recp, BN_CTX *ctx)\n{\n int ret = 0;\n BIGNUM *a;\n const BIGNUM *ca;\n BN_CTX_start(ctx);\n if ((a = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (y != NULL) {\n if (x == y) {\n if (!BN_sqr(a, x, ctx))\n goto err;\n } else {\n if (!BN_mul(a, x, y, ctx))\n goto err;\n }\n ca = a;\n } else\n ca = x;\n ret = BN_div_recp(NULL, r, ca, recp, ctx);\n err:\n BN_CTX_end(ctx);\n bn_check_top(r);\n return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG("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_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m,\n BN_RECP_CTX *recp, BN_CTX *ctx)\n{\n int i, j, ret = 0;\n BIGNUM *a, *b, *d, *r;\n BN_CTX_start(ctx);\n d = (dv != NULL) ? dv : BN_CTX_get(ctx);\n r = (rem != NULL) ? rem : BN_CTX_get(ctx);\n a = BN_CTX_get(ctx);\n b = BN_CTX_get(ctx);\n if (b == NULL)\n goto err;\n if (BN_ucmp(m, &(recp->N)) < 0) {\n BN_zero(d);\n if (!BN_copy(r, m)) {\n BN_CTX_end(ctx);\n return 0;\n }\n BN_CTX_end(ctx);\n return 1;\n }\n i = BN_num_bits(m);\n j = recp->num_bits << 1;\n if (j > i)\n i = j;\n if (i != recp->shift)\n recp->shift = BN_reciprocal(&(recp->Nr), &(recp->N), i, ctx);\n if (recp->shift == -1)\n goto err;\n if (!BN_rshift(a, m, recp->num_bits))\n goto err;\n if (!BN_mul(b, a, &(recp->Nr), ctx))\n goto err;\n if (!BN_rshift(d, b, i - recp->num_bits))\n goto err;\n d->neg = 0;\n if (!BN_mul(b, &(recp->N), d, ctx))\n goto err;\n if (!BN_usub(r, m, b))\n goto err;\n r->neg = 0;\n j = 0;\n while (BN_ucmp(r, &(recp->N)) >= 0) {\n if (j++ > 2) {\n BNerr(BN_F_BN_DIV_RECP, BN_R_BAD_RECIPROCAL);\n goto err;\n }\n if (!BN_usub(r, r, &(recp->N)))\n goto err;\n if (!BN_add_word(d, 1))\n goto err;\n }\n r->neg = BN_is_zero(r) ? 0 : m->neg;\n d->neg = m->neg ^ recp->N.neg;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n bn_check_top(dv);\n bn_check_top(rem);\n return ret;\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n if (ctx == NULL)\n return;\n CTXDBG("ENTER BN_CTX_end()", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG("LEAVE BN_CTX_end()", ctx);\n}', 'static void BN_POOL_release(BN_POOL *p, unsigned int num)\n{\n unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE;\n p->used -= num;\n while (num--) {\n bn_check_top(p->current->vals + offset);\n if (offset == 0) {\n offset = BN_CTX_POOL_SIZE - 1;\n p->current = p->current->prev;\n } else\n offset--;\n }\n}']
2,376
0
https://github.com/openssl/openssl/blob/fa9bb6201e1d16ba8ccab938833d140ef81a7f73/crypto/x509/x509_lu.c/#L501
STACK_OF(X509) *X509_STORE_get1_certs(X509_STORE_CTX *ctx, X509_NAME *nm) { int i, idx, cnt; STACK_OF(X509) *sk; X509 *x; X509_OBJECT *obj; sk = sk_X509_new_null(); CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE); idx = x509_object_idx_cnt(ctx->ctx->objs, X509_LU_X509, nm, &cnt); if (idx < 0) { X509_OBJECT xobj; CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE); if (!X509_STORE_get_by_subject(ctx, X509_LU_X509, nm, &xobj)) { sk_X509_free(sk); return NULL; } X509_OBJECT_free_contents(&xobj); CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE); idx = x509_object_idx_cnt(ctx->ctx->objs, X509_LU_X509, nm, &cnt); if (idx < 0) { CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE); sk_X509_free(sk); return NULL; } } for (i = 0; i < cnt; i++, idx++) { obj = sk_X509_OBJECT_value(ctx->ctx->objs, idx); x = obj->data.x509; X509_up_ref(x); if (!sk_X509_push(sk, x)) { CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE); X509_free(x); sk_X509_pop_free(sk, X509_free); return NULL; } } CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE); return sk; }
['STACK_OF(X509) *X509_STORE_get1_certs(X509_STORE_CTX *ctx, X509_NAME *nm)\n{\n int i, idx, cnt;\n STACK_OF(X509) *sk;\n X509 *x;\n X509_OBJECT *obj;\n sk = sk_X509_new_null();\n CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE);\n idx = x509_object_idx_cnt(ctx->ctx->objs, X509_LU_X509, nm, &cnt);\n if (idx < 0) {\n X509_OBJECT xobj;\n CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);\n if (!X509_STORE_get_by_subject(ctx, X509_LU_X509, nm, &xobj)) {\n sk_X509_free(sk);\n return NULL;\n }\n X509_OBJECT_free_contents(&xobj);\n CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE);\n idx = x509_object_idx_cnt(ctx->ctx->objs, X509_LU_X509, nm, &cnt);\n if (idx < 0) {\n CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);\n sk_X509_free(sk);\n return NULL;\n }\n }\n for (i = 0; i < cnt; i++, idx++) {\n obj = sk_X509_OBJECT_value(ctx->ctx->objs, idx);\n x = obj->data.x509;\n X509_up_ref(x);\n if (!sk_X509_push(sk, x)) {\n CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);\n X509_free(x);\n sk_X509_pop_free(sk, X509_free);\n return NULL;\n }\n }\n CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);\n return sk;\n}', 'DEFINE_STACK_OF(X509)', '_STACK *sk_new_null(void)\n{\n return sk_new((int (*)(const void *, const void *))0);\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 X509_STORE_get_by_subject(X509_STORE_CTX *vs, X509_LOOKUP_TYPE type,\n X509_NAME *name, X509_OBJECT *ret)\n{\n X509_STORE *ctx = vs->ctx;\n X509_LOOKUP *lu;\n X509_OBJECT stmp, *tmp;\n int i, j;\n CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE);\n tmp = X509_OBJECT_retrieve_by_subject(ctx->objs, type, name);\n CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);\n if (tmp == NULL || type == X509_LU_CRL) {\n for (i = vs->current_method;\n i < sk_X509_LOOKUP_num(ctx->get_cert_methods); i++) {\n lu = sk_X509_LOOKUP_value(ctx->get_cert_methods, i);\n j = X509_LOOKUP_by_subject(lu, type, name, &stmp);\n if (j < 0) {\n vs->current_method = j;\n return j;\n } else if (j) {\n tmp = &stmp;\n break;\n }\n }\n vs->current_method = 0;\n if (tmp == NULL)\n return 0;\n }\n ret->type = tmp->type;\n ret->data.ptr = tmp->data.ptr;\n X509_OBJECT_up_ref_count(ret);\n return 1;\n}', 'X509_OBJECT *X509_OBJECT_retrieve_by_subject(STACK_OF(X509_OBJECT) *h,\n int type, X509_NAME *name)\n{\n int idx;\n idx = X509_OBJECT_idx_by_subject(h, type, name);\n if (idx == -1)\n return NULL;\n return sk_X509_OBJECT_value(h, idx);\n}', 'int X509_OBJECT_idx_by_subject(STACK_OF(X509_OBJECT) *h, int type,\n X509_NAME *name)\n{\n return x509_object_idx_cnt(h, type, name, NULL);\n}', 'void *sk_value(const _STACK *st, int i)\n{\n if (!st || (i < 0) || (i >= st->num))\n return NULL;\n return st->data[i];\n}', 'void X509_OBJECT_up_ref_count(X509_OBJECT *a)\n{\n switch (a->type) {\n default:\n break;\n case X509_LU_X509:\n X509_up_ref(a->data.x509);\n break;\n case X509_LU_CRL:\n X509_CRL_up_ref(a->data.crl);\n break;\n }\n}', 'void X509_up_ref(X509 *x)\n{\n CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);\n}', 'int CRYPTO_add_lock(int *pointer, int amount, int type, const char *file,\n int line)\n{\n int ret = 0;\n if (add_lock_callback != NULL) {\n#ifdef LOCK_DEBUG\n int before = *pointer;\n#endif\n ret = add_lock_callback(pointer, amount, type, file, line);\n#ifdef LOCK_DEBUG\n {\n CRYPTO_THREADID id;\n CRYPTO_THREADID_current(&id);\n fprintf(stderr, "ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\\n",\n CRYPTO_THREADID_hash(&id), before, amount, ret,\n CRYPTO_get_lock_name(type), file, line);\n }\n#endif\n } else {\n CRYPTO_lock(CRYPTO_LOCK | CRYPTO_WRITE, type, file, line);\n ret = *pointer + amount;\n#ifdef LOCK_DEBUG\n {\n CRYPTO_THREADID id;\n CRYPTO_THREADID_current(&id);\n fprintf(stderr, "ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\\n",\n CRYPTO_THREADID_hash(&id),\n *pointer, amount, ret,\n CRYPTO_get_lock_name(type), file, line);\n }\n#endif\n *pointer = ret;\n CRYPTO_lock(CRYPTO_UNLOCK | CRYPTO_WRITE, type, file, line);\n }\n return (ret);\n}', 'void X509_OBJECT_free_contents(X509_OBJECT *a)\n{\n if (!a)\n return;\n switch (a->type) {\n default:\n break;\n case X509_LU_X509:\n X509_free(a->data.x509);\n break;\n case X509_LU_CRL:\n X509_CRL_free(a->data.crl);\n break;\n }\n}', 'IMPLEMENT_ASN1_FUNCTIONS(X509)', 'void ASN1_item_free(ASN1_VALUE *val, const ASN1_ITEM *it)\n{\n asn1_item_embed_free(&val, it, 0);\n}']
2,377
0
https://github.com/libav/libav/blob/452a398fd6bdca3f301c5c8af3bc241bc16a777e/libavcodec/h264.c/#L2835
static int fill_default_ref_list(H264Context *h){ MpegEncContext * const s = &h->s; int i, len; if(h->slice_type_nos==FF_B_TYPE){ Picture *sorted[32]; int cur_poc, list; int lens[2]; if(FIELD_PICTURE) cur_poc= s->current_picture_ptr->field_poc[ s->picture_structure == PICT_BOTTOM_FIELD ]; else cur_poc= s->current_picture_ptr->poc; for(list= 0; list<2; list++){ len= add_sorted(sorted , h->short_ref, h->short_ref_count, cur_poc, 1^list); len+=add_sorted(sorted+len, h->short_ref, h->short_ref_count, cur_poc, 0^list); assert(len<=32); len= build_def_list(h->default_ref_list[list] , sorted , len, 0, s->picture_structure); len+=build_def_list(h->default_ref_list[list]+len, h->long_ref, 16 , 1, s->picture_structure); assert(len<=32); if(len < h->ref_count[list]) memset(&h->default_ref_list[list][len], 0, sizeof(Picture)*(h->ref_count[list] - len)); lens[list]= len; } if(lens[0] == lens[1] && lens[1] > 1){ for(i=0; h->default_ref_list[0][i].data[0] == h->default_ref_list[1][i].data[0] && i<lens[0]; i++); if(i == lens[0]) FFSWAP(Picture, h->default_ref_list[1][0], h->default_ref_list[1][1]); } }else{ len = build_def_list(h->default_ref_list[0] , h->short_ref, h->short_ref_count, 0, s->picture_structure); len+= build_def_list(h->default_ref_list[0]+len, h-> long_ref, 16 , 1, s->picture_structure); assert(len <= 32); if(len < h->ref_count[0]) memset(&h->default_ref_list[0][len], 0, sizeof(Picture)*(h->ref_count[0] - len)); } #ifdef TRACE for (i=0; i<h->ref_count[0]; i++) { tprintf(h->s.avctx, "List0: %s fn:%d 0x%p\n", (h->default_ref_list[0][i].long_ref ? "LT" : "ST"), h->default_ref_list[0][i].pic_id, h->default_ref_list[0][i].data[0]); } if(h->slice_type_nos==FF_B_TYPE){ for (i=0; i<h->ref_count[1]; i++) { tprintf(h->s.avctx, "List1: %s fn:%d 0x%p\n", (h->default_ref_list[1][i].long_ref ? "LT" : "ST"), h->default_ref_list[1][i].pic_id, h->default_ref_list[1][i].data[0]); } } #endif return 0; }
['static int fill_default_ref_list(H264Context *h){\n MpegEncContext * const s = &h->s;\n int i, len;\n if(h->slice_type_nos==FF_B_TYPE){\n Picture *sorted[32];\n int cur_poc, list;\n int lens[2];\n if(FIELD_PICTURE)\n cur_poc= s->current_picture_ptr->field_poc[ s->picture_structure == PICT_BOTTOM_FIELD ];\n else\n cur_poc= s->current_picture_ptr->poc;\n for(list= 0; list<2; list++){\n len= add_sorted(sorted , h->short_ref, h->short_ref_count, cur_poc, 1^list);\n len+=add_sorted(sorted+len, h->short_ref, h->short_ref_count, cur_poc, 0^list);\n assert(len<=32);\n len= build_def_list(h->default_ref_list[list] , sorted , len, 0, s->picture_structure);\n len+=build_def_list(h->default_ref_list[list]+len, h->long_ref, 16 , 1, s->picture_structure);\n assert(len<=32);\n if(len < h->ref_count[list])\n memset(&h->default_ref_list[list][len], 0, sizeof(Picture)*(h->ref_count[list] - len));\n lens[list]= len;\n }\n if(lens[0] == lens[1] && lens[1] > 1){\n for(i=0; h->default_ref_list[0][i].data[0] == h->default_ref_list[1][i].data[0] && i<lens[0]; i++);\n if(i == lens[0])\n FFSWAP(Picture, h->default_ref_list[1][0], h->default_ref_list[1][1]);\n }\n }else{\n len = build_def_list(h->default_ref_list[0] , h->short_ref, h->short_ref_count, 0, s->picture_structure);\n len+= build_def_list(h->default_ref_list[0]+len, h-> long_ref, 16 , 1, s->picture_structure);\n assert(len <= 32);\n if(len < h->ref_count[0])\n memset(&h->default_ref_list[0][len], 0, sizeof(Picture)*(h->ref_count[0] - len));\n }\n#ifdef TRACE\n for (i=0; i<h->ref_count[0]; i++) {\n tprintf(h->s.avctx, "List0: %s fn:%d 0x%p\\n", (h->default_ref_list[0][i].long_ref ? "LT" : "ST"), h->default_ref_list[0][i].pic_id, h->default_ref_list[0][i].data[0]);\n }\n if(h->slice_type_nos==FF_B_TYPE){\n for (i=0; i<h->ref_count[1]; i++) {\n tprintf(h->s.avctx, "List1: %s fn:%d 0x%p\\n", (h->default_ref_list[1][i].long_ref ? "LT" : "ST"), h->default_ref_list[1][i].pic_id, h->default_ref_list[1][i].data[0]);\n }\n }\n#endif\n return 0;\n}']
2,378
0
https://github.com/libav/libav/blob/392107ad079860fb41c3a9800b6d33ad4b058324/libavcodec/mpegvideo.c/#L821
int MPV_common_init(MpegEncContext *s) { int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y, threads = (s->encoding || (HAVE_THREADS && s->avctx->active_thread_type & FF_THREAD_SLICE)) ? s->avctx->thread_count : 1; if (s->codec_id == CODEC_ID_MPEG2VIDEO && !s->progressive_sequence) s->mb_height = (s->height + 31) / 32 * 2; else if (s->codec_id != CODEC_ID_H264) s->mb_height = (s->height + 15) / 16; if (s->avctx->pix_fmt == PIX_FMT_NONE) { av_log(s->avctx, AV_LOG_ERROR, "decoding to PIX_FMT_NONE is not supported.\n"); return -1; } if ((s->encoding || (s->avctx->active_thread_type & FF_THREAD_SLICE)) && (s->avctx->thread_count > MAX_THREADS || (s->avctx->thread_count > s->mb_height && s->mb_height))) { int max_threads = FFMIN(MAX_THREADS, s->mb_height); av_log(s->avctx, AV_LOG_WARNING, "too many threads (%d), reducing to %d\n", s->avctx->thread_count, max_threads); threads = max_threads; } if ((s->width || s->height) && av_image_check_size(s->width, s->height, 0, s->avctx)) return -1; ff_dct_common_init(s); s->flags = s->avctx->flags; s->flags2 = s->avctx->flags2; if (s->width && s->height) { s->mb_width = (s->width + 15) / 16; s->mb_stride = s->mb_width + 1; s->b8_stride = s->mb_width * 2 + 1; s->b4_stride = s->mb_width * 4 + 1; mb_array_size = s->mb_height * s->mb_stride; mv_table_size = (s->mb_height + 2) * s->mb_stride + 1; avcodec_get_chroma_sub_sample(s->avctx->pix_fmt,&(s->chroma_x_shift), &(s->chroma_y_shift) ); s->h_edge_pos = s->mb_width * 16; s->v_edge_pos = s->mb_height * 16; s->mb_num = s->mb_width * s->mb_height; s->block_wrap[0] = s->block_wrap[1] = s->block_wrap[2] = s->block_wrap[3] = s->b8_stride; s->block_wrap[4] = s->block_wrap[5] = s->mb_stride; y_size = s->b8_stride * (2 * s->mb_height + 1); c_size = s->mb_stride * (s->mb_height + 1); yc_size = y_size + 2 * c_size; s->codec_tag = avpriv_toupper4(s->avctx->codec_tag); s->stream_codec_tag = avpriv_toupper4(s->avctx->stream_codec_tag); s->avctx->coded_frame = (AVFrame *)&s->current_picture; FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int), fail); for (y = 0; y < s->mb_height; y++) for (x = 0; x < s->mb_width; x++) s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride; s->mb_index2xy[s->mb_height * s->mb_width] = (s->mb_height - 1) * s->mb_stride + s->mb_width; if (s->encoding) { FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail); FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail); FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail); FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail); FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail); FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail); s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1; s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1; s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1; s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base + s->mb_stride + 1; s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base + s->mb_stride + 1; s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1; if (s->msmpeg4_version) { FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats, 2 * 2 * (MAX_LEVEL + 1) * (MAX_RUN + 1) * 2 * sizeof(int), fail); } FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail); FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size * sizeof(uint16_t), fail); FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size * sizeof(int), fail); FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix, 64 * 32 * sizeof(int), fail); FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix, 64 * 32 * sizeof(int), fail); FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail); FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail); FF_ALLOCZ_OR_GOTO(s->avctx, s->input_picture, MAX_PICTURE_COUNT * sizeof(Picture *), fail); FF_ALLOCZ_OR_GOTO(s->avctx, s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture *), fail); if (s->avctx->noise_reduction) { FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset, 2 * 64 * sizeof(uint16_t), fail); } } } s->picture_count = MAX_PICTURE_COUNT * FFMAX(1, s->avctx->thread_count); FF_ALLOCZ_OR_GOTO(s->avctx, s->picture, s->picture_count * sizeof(Picture), fail); for (i = 0; i < s->picture_count; i++) { avcodec_get_frame_defaults((AVFrame *) &s->picture[i]); } if (s->width && s->height) { FF_ALLOCZ_OR_GOTO(s->avctx, s->error_status_table, mb_array_size * sizeof(uint8_t), fail); if (s->codec_id == CODEC_ID_MPEG4 || (s->flags & CODEC_FLAG_INTERLACED_ME)) { for (i = 0; i < 2; i++) { int j, k; for (j = 0; j < 2; j++) { for (k = 0; k < 2; k++) { FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_mv_table_base[i][j][k], mv_table_size * 2 * sizeof(int16_t), fail); s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] + s->mb_stride + 1; } FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j], mb_array_size * 2 * sizeof(uint8_t), fail); FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j], mv_table_size * 2 * sizeof(int16_t), fail); s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j] + s->mb_stride + 1; } FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i], mb_array_size * 2 * sizeof(uint8_t), fail); } } if (s->out_format == FMT_H263) { FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail); s->coded_block = s->coded_block_base + s->b8_stride + 1; FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table, mb_array_size * sizeof(uint8_t), fail); FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table, mb_array_size * sizeof(uint8_t), fail); } if (s->h263_pred || s->h263_plus || !s->encoding) { FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base, yc_size * sizeof(int16_t), fail); s->dc_val[0] = s->dc_val_base + s->b8_stride + 1; s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1; s->dc_val[2] = s->dc_val[1] + c_size; for (i = 0; i < yc_size; i++) s->dc_val_base[i] = 1024; } FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail); memset(s->mbintra_table, 1, mb_array_size); FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail); FF_ALLOCZ_OR_GOTO(s->avctx, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE, fail); s->parse_context.state = -1; if ((s->avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) || s->avctx->debug_mv) { s->visualization_buffer[0] = av_malloc((s->mb_width * 16 + 2 * EDGE_WIDTH) * s->mb_height * 16 + 2 * EDGE_WIDTH); s->visualization_buffer[1] = av_malloc((s->mb_width * 16 + 2 * EDGE_WIDTH) * s->mb_height * 16 + 2 * EDGE_WIDTH); s->visualization_buffer[2] = av_malloc((s->mb_width * 16 + 2 * EDGE_WIDTH) * s->mb_height * 16 + 2 * EDGE_WIDTH); } } s->context_initialized = 1; s->thread_context[0] = s; if (s->width && s->height) { if (s->encoding || (HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_SLICE)) { for (i = 1; i < threads; i++) { s->thread_context[i] = av_malloc(sizeof(MpegEncContext)); memcpy(s->thread_context[i], s, sizeof(MpegEncContext)); } for (i = 0; i < threads; i++) { if (init_duplicate_context(s->thread_context[i], s) < 0) goto fail; s->thread_context[i]->start_mb_y = (s->mb_height * (i) + s->avctx->thread_count / 2) / s->avctx->thread_count; s->thread_context[i]->end_mb_y = (s->mb_height * (i + 1) + s->avctx->thread_count / 2) / s->avctx->thread_count; } } else { if (init_duplicate_context(s, s) < 0) goto fail; s->start_mb_y = 0; s->end_mb_y = s->mb_height; } } return 0; fail: MPV_common_end(s); return -1; }
['int MPV_common_init(MpegEncContext *s)\n{\n int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y,\n threads = (s->encoding ||\n (HAVE_THREADS &&\n s->avctx->active_thread_type & FF_THREAD_SLICE)) ?\n s->avctx->thread_count : 1;\n if (s->codec_id == CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)\n s->mb_height = (s->height + 31) / 32 * 2;\n else if (s->codec_id != CODEC_ID_H264)\n s->mb_height = (s->height + 15) / 16;\n if (s->avctx->pix_fmt == PIX_FMT_NONE) {\n av_log(s->avctx, AV_LOG_ERROR,\n "decoding to PIX_FMT_NONE is not supported.\\n");\n return -1;\n }\n if ((s->encoding || (s->avctx->active_thread_type & FF_THREAD_SLICE)) &&\n (s->avctx->thread_count > MAX_THREADS ||\n (s->avctx->thread_count > s->mb_height && s->mb_height))) {\n int max_threads = FFMIN(MAX_THREADS, s->mb_height);\n av_log(s->avctx, AV_LOG_WARNING,\n "too many threads (%d), reducing to %d\\n",\n s->avctx->thread_count, max_threads);\n threads = max_threads;\n }\n if ((s->width || s->height) &&\n av_image_check_size(s->width, s->height, 0, s->avctx))\n return -1;\n ff_dct_common_init(s);\n s->flags = s->avctx->flags;\n s->flags2 = s->avctx->flags2;\n if (s->width && s->height) {\n s->mb_width = (s->width + 15) / 16;\n s->mb_stride = s->mb_width + 1;\n s->b8_stride = s->mb_width * 2 + 1;\n s->b4_stride = s->mb_width * 4 + 1;\n mb_array_size = s->mb_height * s->mb_stride;\n mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;\n avcodec_get_chroma_sub_sample(s->avctx->pix_fmt,&(s->chroma_x_shift),\n &(s->chroma_y_shift) );\n s->h_edge_pos = s->mb_width * 16;\n s->v_edge_pos = s->mb_height * 16;\n s->mb_num = s->mb_width * s->mb_height;\n s->block_wrap[0] =\n s->block_wrap[1] =\n s->block_wrap[2] =\n s->block_wrap[3] = s->b8_stride;\n s->block_wrap[4] =\n s->block_wrap[5] = s->mb_stride;\n y_size = s->b8_stride * (2 * s->mb_height + 1);\n c_size = s->mb_stride * (s->mb_height + 1);\n yc_size = y_size + 2 * c_size;\n s->codec_tag = avpriv_toupper4(s->avctx->codec_tag);\n s->stream_codec_tag = avpriv_toupper4(s->avctx->stream_codec_tag);\n s->avctx->coded_frame = (AVFrame *)&s->current_picture;\n FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int),\n fail);\n for (y = 0; y < s->mb_height; y++)\n for (x = 0; x < s->mb_width; x++)\n s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;\n s->mb_index2xy[s->mb_height * s->mb_width] =\n (s->mb_height - 1) * s->mb_stride + s->mb_width;\n if (s->encoding) {\n FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base,\n mv_table_size * 2 * sizeof(int16_t), fail);\n FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base,\n mv_table_size * 2 * sizeof(int16_t), fail);\n FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base,\n mv_table_size * 2 * sizeof(int16_t), fail);\n FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base,\n mv_table_size * 2 * sizeof(int16_t), fail);\n FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base,\n mv_table_size * 2 * sizeof(int16_t), fail);\n FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base,\n mv_table_size * 2 * sizeof(int16_t), fail);\n s->p_mv_table = s->p_mv_table_base +\n s->mb_stride + 1;\n s->b_forw_mv_table = s->b_forw_mv_table_base +\n s->mb_stride + 1;\n s->b_back_mv_table = s->b_back_mv_table_base +\n s->mb_stride + 1;\n s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base +\n s->mb_stride + 1;\n s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base +\n s->mb_stride + 1;\n s->b_direct_mv_table = s->b_direct_mv_table_base +\n s->mb_stride + 1;\n if (s->msmpeg4_version) {\n FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats,\n 2 * 2 * (MAX_LEVEL + 1) *\n (MAX_RUN + 1) * 2 * sizeof(int), fail);\n }\n FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);\n FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size *\n sizeof(uint16_t), fail);\n FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size *\n sizeof(int), fail);\n FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix,\n 64 * 32 * sizeof(int), fail);\n FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix,\n 64 * 32 * sizeof(int), fail);\n FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix16,\n 64 * 32 * 2 * sizeof(uint16_t), fail);\n FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix16,\n 64 * 32 * 2 * sizeof(uint16_t), fail);\n FF_ALLOCZ_OR_GOTO(s->avctx, s->input_picture,\n MAX_PICTURE_COUNT * sizeof(Picture *), fail);\n FF_ALLOCZ_OR_GOTO(s->avctx, s->reordered_input_picture,\n MAX_PICTURE_COUNT * sizeof(Picture *), fail);\n if (s->avctx->noise_reduction) {\n FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset,\n 2 * 64 * sizeof(uint16_t), fail);\n }\n }\n }\n s->picture_count = MAX_PICTURE_COUNT * FFMAX(1, s->avctx->thread_count);\n FF_ALLOCZ_OR_GOTO(s->avctx, s->picture,\n s->picture_count * sizeof(Picture), fail);\n for (i = 0; i < s->picture_count; i++) {\n avcodec_get_frame_defaults((AVFrame *) &s->picture[i]);\n }\n if (s->width && s->height) {\n FF_ALLOCZ_OR_GOTO(s->avctx, s->error_status_table,\n mb_array_size * sizeof(uint8_t), fail);\n if (s->codec_id == CODEC_ID_MPEG4 ||\n (s->flags & CODEC_FLAG_INTERLACED_ME)) {\n for (i = 0; i < 2; i++) {\n int j, k;\n for (j = 0; j < 2; j++) {\n for (k = 0; k < 2; k++) {\n FF_ALLOCZ_OR_GOTO(s->avctx,\n s->b_field_mv_table_base[i][j][k],\n mv_table_size * 2 * sizeof(int16_t),\n fail);\n s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +\n s->mb_stride + 1;\n }\n FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j],\n mb_array_size * 2 * sizeof(uint8_t),\n fail);\n FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j],\n mv_table_size * 2 * sizeof(int16_t),\n fail);\n s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j]\n + s->mb_stride + 1;\n }\n FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i],\n mb_array_size * 2 * sizeof(uint8_t),\n fail);\n }\n }\n if (s->out_format == FMT_H263) {\n FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail);\n s->coded_block = s->coded_block_base + s->b8_stride + 1;\n FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table,\n mb_array_size * sizeof(uint8_t), fail);\n FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table,\n mb_array_size * sizeof(uint8_t), fail);\n }\n if (s->h263_pred || s->h263_plus || !s->encoding) {\n FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base,\n yc_size * sizeof(int16_t), fail);\n s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;\n s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;\n s->dc_val[2] = s->dc_val[1] + c_size;\n for (i = 0; i < yc_size; i++)\n s->dc_val_base[i] = 1024;\n }\n FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);\n memset(s->mbintra_table, 1, mb_array_size);\n FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);\n FF_ALLOCZ_OR_GOTO(s->avctx, s->prev_pict_types,\n PREV_PICT_TYPES_BUFFER_SIZE, fail);\n s->parse_context.state = -1;\n if ((s->avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) ||\n s->avctx->debug_mv) {\n s->visualization_buffer[0] = av_malloc((s->mb_width * 16 +\n 2 * EDGE_WIDTH) * s->mb_height * 16 + 2 * EDGE_WIDTH);\n s->visualization_buffer[1] = av_malloc((s->mb_width * 16 +\n 2 * EDGE_WIDTH) * s->mb_height * 16 + 2 * EDGE_WIDTH);\n s->visualization_buffer[2] = av_malloc((s->mb_width * 16 +\n 2 * EDGE_WIDTH) * s->mb_height * 16 + 2 * EDGE_WIDTH);\n }\n }\n s->context_initialized = 1;\n s->thread_context[0] = s;\n if (s->width && s->height) {\n if (s->encoding || (HAVE_THREADS &&\n s->avctx->active_thread_type&FF_THREAD_SLICE)) {\n for (i = 1; i < threads; i++) {\n s->thread_context[i] = av_malloc(sizeof(MpegEncContext));\n memcpy(s->thread_context[i], s, sizeof(MpegEncContext));\n }\n for (i = 0; i < threads; i++) {\n if (init_duplicate_context(s->thread_context[i], s) < 0)\n goto fail;\n s->thread_context[i]->start_mb_y =\n (s->mb_height * (i) + s->avctx->thread_count / 2) /\n s->avctx->thread_count;\n s->thread_context[i]->end_mb_y =\n (s->mb_height * (i + 1) + s->avctx->thread_count / 2) /\n s->avctx->thread_count;\n }\n } else {\n if (init_duplicate_context(s, s) < 0)\n goto fail;\n s->start_mb_y = 0;\n s->end_mb_y = s->mb_height;\n }\n }\n return 0;\n fail:\n MPV_common_end(s);\n return -1;\n}']
2,379
0
https://github.com/openssl/openssl/blob/9f519addc09b2005fa8c6cde36e3267de02577bb/crypto/bn/bn_lib.c/#L342
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *A, *a = NULL; const BN_ULONG *B; int i; bn_check_top(b); if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return (NULL); } if (BN_get_flags(b,BN_FLG_SECURE)) a = A = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = A = OPENSSL_zalloc(words * sizeof(*a)); if (A == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return (NULL); } #if 1 B = b->d; if (B != NULL) { for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) { BN_ULONG a0, a1, a2, a3; a0 = B[0]; a1 = B[1]; a2 = B[2]; a3 = B[3]; A[0] = a0; A[1] = a1; A[2] = a2; A[3] = a3; } switch (b->top & 3) { case 3: A[2] = B[2]; case 2: A[1] = B[1]; case 1: A[0] = B[0]; case 0: ; } } #else memset(A, 0, sizeof(*A) * words); memcpy(A, b->d, sizeof(b->d[0]) * b->top); #endif return (a); }
['EC_GROUP *EC_GROUP_new_from_ecparameters(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_GROUP_NEW_FROM_ECPARAMETERS, 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_GROUP_NEW_FROM_ECPARAMETERS, 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_GROUP_NEW_FROM_ECPARAMETERS, 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_GROUP_NEW_FROM_ECPARAMETERS, 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_GROUP_NEW_FROM_ECPARAMETERS, 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_GROUP_NEW_FROM_ECPARAMETERS, EC_R_FIELD_TOO_LARGE);\n goto err;\n }\n if ((p = BN_new()) == NULL) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, 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_GROUP_NEW_FROM_ECPARAMETERS, 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_GROUP_NEW_FROM_ECPARAMETERS,\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_GROUP_NEW_FROM_ECPARAMETERS, 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_GROUP_NEW_FROM_ECPARAMETERS,\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_GROUP_NEW_FROM_ECPARAMETERS, EC_R_NOT_IMPLEMENTED);\n goto err;\n } else {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, 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_GROUP_NEW_FROM_ECPARAMETERS, 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_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);\n goto err;\n }\n if (BN_is_negative(p) || BN_is_zero(p)) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, 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_GROUP_NEW_FROM_ECPARAMETERS, 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_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_FIELD);\n goto err;\n }\n if (ret == NULL) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, 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_GROUP_NEW_FROM_ECPARAMETERS, 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_GROUP_NEW_FROM_ECPARAMETERS, 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_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);\n goto err;\n }\n if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);\n goto err;\n }\n if (BN_is_negative(a) || BN_is_zero(a)) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_GROUP_ORDER);\n goto err;\n }\n if (BN_num_bits(a) > (int)field_bits + 1) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, 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_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);\n goto err;\n }\n if (!EC_GROUP_set_generator(ret, point, a, b)) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, 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}', 'BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)\n{\n unsigned int i, m;\n unsigned int n;\n BN_ULONG l;\n BIGNUM *bn = NULL;\n if (ret == NULL)\n ret = bn = BN_new();\n if (ret == NULL)\n return (NULL);\n bn_check_top(ret);\n for ( ; len > 0 && *s == 0; s++, len--)\n continue;\n n = len;\n if (n == 0) {\n ret->top = 0;\n return (ret);\n }\n i = ((n - 1) / BN_BYTES) + 1;\n m = ((n - 1) % (BN_BYTES));\n if (bn_wexpand(ret, (int)i) == NULL) {\n BN_free(bn);\n return NULL;\n }\n ret->top = i;\n ret->neg = 0;\n l = 0;\n while (n--) {\n l = (l << 8L) | *(s++);\n if (m-- == 0) {\n ret->d[--i] = l;\n l = 0;\n m = BN_BYTES - 1;\n }\n }\n bn_correct_top(ret);\n return (ret);\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *A, *a = NULL;\n const BN_ULONG *B;\n int i;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return (NULL);\n }\n if (BN_get_flags(b,BN_FLG_SECURE))\n a = A = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = A = OPENSSL_zalloc(words * sizeof(*a));\n if (A == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n#if 1\n B = b->d;\n if (B != NULL) {\n for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {\n BN_ULONG a0, a1, a2, a3;\n a0 = B[0];\n a1 = B[1];\n a2 = B[2];\n a3 = B[3];\n A[0] = a0;\n A[1] = a1;\n A[2] = a2;\n A[3] = a3;\n }\n switch (b->top & 3) {\n case 3:\n A[2] = B[2];\n case 2:\n A[1] = B[1];\n case 1:\n A[0] = B[0];\n case 0:\n ;\n }\n }\n#else\n memset(A, 0, sizeof(*A) * words);\n memcpy(A, b->d, sizeof(b->d[0]) * b->top);\n#endif\n return (a);\n}']
2,380
0
https://github.com/openssl/openssl/blob/f3ff481f318b10a223d6157bde9645e1797487c5/ssl/packet.c/#L25
int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes) { assert(pkt->subs != NULL && len != 0); if (pkt->subs == NULL || len == 0) return 0; if (pkt->maxsize - pkt->written < len) return 0; if (pkt->buf->length - pkt->written < len) { size_t newlen; if (pkt->buf->length > SIZE_MAX / 2) { newlen = SIZE_MAX; } else { newlen = (pkt->buf->length == 0) ? DEFAULT_BUF_SIZE : pkt->buf->length * 2; } if (BUF_MEM_grow(pkt->buf, newlen) == 0) return 0; } *allocbytes = (unsigned char *)pkt->buf->data + pkt->curr; pkt->written += len; pkt->curr += len; return 1; }
['int ssl_add_clienthello_tlsext(SSL *s, WPACKET *pkt, int *al)\n{\n#ifndef OPENSSL_NO_EC\n int using_ecc = 0;\n if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s)) {\n int i;\n unsigned long alg_k, alg_a;\n STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);\n for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {\n const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);\n alg_k = c->algorithm_mkey;\n alg_a = c->algorithm_auth;\n if ((alg_k & (SSL_kECDHE | SSL_kECDHEPSK))\n || (alg_a & SSL_aECDSA)) {\n using_ecc = 1;\n break;\n }\n }\n }\n#endif\n if (s->renegotiate) {\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate)\n || !WPACKET_sub_memcpy_u16(pkt, s->s3->previous_client_finished,\n s->s3->previous_client_finished_len)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n }\n if (s->client_version == SSL3_VERSION)\n goto done;\n if (s->tlsext_hostname != NULL) {\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name)\n || !WPACKET_start_sub_packet_u16(pkt)\n || !WPACKET_start_sub_packet_u16(pkt)\n || !WPACKET_put_bytes_u8(pkt, TLSEXT_NAMETYPE_host_name)\n || !WPACKET_sub_memcpy_u16(pkt, s->tlsext_hostname,\n strlen(s->tlsext_hostname))\n || !WPACKET_close(pkt)\n || !WPACKET_close(pkt)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n }\n#ifndef OPENSSL_NO_SRP\n if (s->srp_ctx.login != NULL) {\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_srp)\n || !WPACKET_start_sub_packet_u16(pkt)\n || !WPACKET_start_sub_packet_u8(pkt)\n || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)\n || !WPACKET_memcpy(pkt, s->srp_ctx.login,\n strlen(s->srp_ctx.login))\n || !WPACKET_close(pkt)\n || !WPACKET_close(pkt)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n }\n#endif\n#ifndef OPENSSL_NO_EC\n if (using_ecc) {\n const unsigned char *pcurves, *pformats;\n size_t num_curves, num_formats;\n size_t i;\n tls1_get_formatlist(s, &pformats, &num_formats);\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats)\n || !WPACKET_start_sub_packet_u16(pkt)\n || !WPACKET_sub_memcpy_u8(pkt, pformats, num_formats)\n || !WPACKET_close(pkt)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n pcurves = s->tlsext_ellipticcurvelist;\n if (!tls1_get_curvelist(s, 0, &pcurves, &num_curves)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_elliptic_curves)\n || !WPACKET_start_sub_packet_u16(pkt)\n || !WPACKET_start_sub_packet_u16(pkt)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n for (i = 0; i < num_curves; i++, pcurves += 2) {\n if (tls_curve_allowed(s, pcurves, SSL_SECOP_CURVE_SUPPORTED)) {\n if (!WPACKET_put_bytes_u8(pkt, pcurves[0])\n || !WPACKET_put_bytes_u8(pkt, pcurves[1])) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT,\n ERR_R_INTERNAL_ERROR);\n return 0;\n }\n }\n }\n if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n }\n#endif\n if (tls_use_ticket(s)) {\n int ticklen;\n if (!s->new_session && s->session && s->session->tlsext_tick)\n ticklen = s->session->tlsext_ticklen;\n else if (s->session && s->tlsext_session_ticket &&\n s->tlsext_session_ticket->data) {\n ticklen = s->tlsext_session_ticket->length;\n s->session->tlsext_tick = OPENSSL_malloc(ticklen);\n if (s->session->tlsext_tick == NULL) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n memcpy(s->session->tlsext_tick,\n s->tlsext_session_ticket->data, ticklen);\n s->session->tlsext_ticklen = ticklen;\n } else\n ticklen = 0;\n if (ticklen == 0 && s->tlsext_session_ticket &&\n s->tlsext_session_ticket->data == NULL)\n goto skip_ext;\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket)\n || !WPACKET_sub_memcpy_u16(pkt, s->session->tlsext_tick,\n ticklen)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n }\n skip_ext:\n if (SSL_CLIENT_USE_SIGALGS(s)) {\n size_t salglen;\n const unsigned char *salg;\n salglen = tls12_get_psigalgs(s, &salg);\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signature_algorithms)\n || !WPACKET_start_sub_packet_u16(pkt)\n || !WPACKET_start_sub_packet_u16(pkt)\n || !tls12_copy_sigalgs(s, pkt, salg, salglen)\n || !WPACKET_close(pkt)\n || !WPACKET_close(pkt)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n }\n#ifndef OPENSSL_NO_OCSP\n if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {\n int i;\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request)\n || !WPACKET_start_sub_packet_u16(pkt)\n || !WPACKET_put_bytes_u8(pkt, TLSEXT_STATUSTYPE_ocsp)\n || !WPACKET_start_sub_packet_u16(pkt)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) {\n unsigned char *idbytes;\n int idlen;\n OCSP_RESPID *id;\n id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);\n idlen = i2d_OCSP_RESPID(id, NULL);\n if (idlen <= 0\n || !WPACKET_sub_allocate_bytes_u16(pkt, idlen, &idbytes)\n || i2d_OCSP_RESPID(id, &idbytes) != idlen) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n }\n if (!WPACKET_close(pkt)\n || !WPACKET_start_sub_packet_u16(pkt)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n if (s->tlsext_ocsp_exts) {\n unsigned char *extbytes;\n int extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);\n if (extlen < 0) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n if (!WPACKET_allocate_bytes(pkt, extlen, &extbytes)\n || i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &extbytes)\n != extlen) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n }\n if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n }\n#endif\n#ifndef OPENSSL_NO_HEARTBEATS\n if (SSL_IS_DTLS(s)) {\n unsigned int mode;\n if (s->tlsext_heartbeat & SSL_DTLSEXT_HB_DONT_RECV_REQUESTS)\n mode = SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;\n else\n mode = SSL_DTLSEXT_HB_ENABLED;\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_heartbeat)\n || !WPACKET_start_sub_packet_u16(pkt)\n || !WPACKET_put_bytes_u8(pkt, mode)\n || !WPACKET_close(pkt)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n }\n#endif\n#ifndef OPENSSL_NO_NEXTPROTONEG\n if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len) {\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg)\n || !WPACKET_put_bytes_u16(pkt, 0)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n }\n#endif\n if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len) {\n if (!WPACKET_put_bytes_u16(pkt,\n TLSEXT_TYPE_application_layer_protocol_negotiation)\n || !WPACKET_start_sub_packet_u16(pkt)\n || !WPACKET_sub_memcpy_u16(pkt, s->alpn_client_proto_list,\n s->alpn_client_proto_list_len)\n || !WPACKET_close(pkt)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n s->s3->alpn_sent = 1;\n }\n#ifndef OPENSSL_NO_SRTP\n if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)) {\n STACK_OF(SRTP_PROTECTION_PROFILE) *clnt = 0;\n SRTP_PROTECTION_PROFILE *prof;\n int i, ct;\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp)\n || !WPACKET_start_sub_packet_u16(pkt)\n || !WPACKET_start_sub_packet_u16(pkt)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n ct = sk_SRTP_PROTECTION_PROFILE_num(clnt);\n for (i = 0; i < ct; i++) {\n prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i);\n if (prof == NULL || !WPACKET_put_bytes_u16(pkt, prof->id)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n }\n if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n }\n#endif\n custom_ext_init(&s->cert->cli_ext);\n if (!custom_ext_add(s, 0, pkt, al)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac)\n || !WPACKET_put_bytes_u16(pkt, 0)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n#ifndef OPENSSL_NO_CT\n if (s->ct_validation_callback != NULL) {\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signed_certificate_timestamp)\n || !WPACKET_put_bytes_u16(pkt, 0)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n }\n#endif\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret)\n || !WPACKET_put_bytes_u16(pkt, 0)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n if (s->options & SSL_OP_TLSEXT_PADDING) {\n unsigned char *padbytes;\n size_t hlen;\n if (!WPACKET_get_total_written(pkt, &hlen)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n if (hlen > 0xff && hlen < 0x200) {\n hlen = 0x200 - hlen;\n if (hlen >= 4)\n hlen -= 4;\n else\n hlen = 0;\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_padding)\n || !WPACKET_sub_allocate_bytes_u16(pkt, hlen, &padbytes)) {\n SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n memset(padbytes, 0, hlen);\n }\n }\n done:\n return 1;\n}', 'int WPACKET_put_bytes__(WPACKET *pkt, unsigned int val, size_t size)\n{\n unsigned char *data;\n assert(size <= sizeof(unsigned int));\n if (size > sizeof(unsigned int)\n || !WPACKET_allocate_bytes(pkt, size, &data)\n || !put_value(data, val, size))\n return 0;\n return 1;\n}', 'int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)\n{\n WPACKET_SUB *sub;\n unsigned char *lenchars;\n assert(pkt->subs != NULL);\n if (pkt->subs == NULL)\n return 0;\n sub = OPENSSL_zalloc(sizeof(*sub));\n if (sub == NULL)\n return 0;\n sub->parent = pkt->subs;\n pkt->subs = sub;\n sub->pwritten = pkt->written + lenbytes;\n sub->lenbytes = lenbytes;\n if (lenbytes == 0) {\n sub->packet_len = 0;\n return 1;\n }\n if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))\n return 0;\n sub->packet_len = lenchars - (unsigned char *)pkt->buf->data;\n return 1;\n}', 'int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,\n size_t lenbytes)\n{\n if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)\n || !WPACKET_memcpy(pkt, src, len)\n || !WPACKET_close(pkt))\n return 0;\n return 1;\n}', 'int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)\n{\n unsigned char *dest;\n if (len == 0)\n return 1;\n if (!WPACKET_allocate_bytes(pkt, len, &dest))\n return 0;\n memcpy(dest, src, len);\n return 1;\n}', 'int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)\n{\n assert(pkt->subs != NULL && len != 0);\n if (pkt->subs == NULL || len == 0)\n return 0;\n if (pkt->maxsize - pkt->written < len)\n return 0;\n if (pkt->buf->length - pkt->written < len) {\n size_t newlen;\n if (pkt->buf->length > SIZE_MAX / 2) {\n newlen = SIZE_MAX;\n } else {\n newlen = (pkt->buf->length == 0) ? DEFAULT_BUF_SIZE\n : pkt->buf->length * 2;\n }\n if (BUF_MEM_grow(pkt->buf, newlen) == 0)\n return 0;\n }\n *allocbytes = (unsigned char *)pkt->buf->data + pkt->curr;\n pkt->written += len;\n pkt->curr += len;\n return 1;\n}']
2,381
0
https://github.com/openssl/openssl/blob/2864df8f9d3264e19b49a246e272fb513f4c1be3/crypto/bn/bn_ctx.c/#L270
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['static BIGNUM *dsa_mod_inverse_fermat(const BIGNUM *k, const BIGNUM *q,\n BN_CTX *ctx)\n{\n BIGNUM *res = NULL;\n BIGNUM *r, *e;\n if ((r = BN_new()) == NULL)\n return NULL;\n BN_CTX_start(ctx);\n if ((e = BN_CTX_get(ctx)) != NULL\n && BN_set_word(r, 2)\n && BN_sub(e, q, r)\n && BN_mod_exp_mont(r, k, e, q, ctx, NULL))\n res = r;\n else\n BN_free(r);\n BN_CTX_end(ctx);\n return res;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_start()", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG("LEAVE BN_CTX_start()", ctx);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n if (ctx == NULL)\n return;\n CTXDBG("ENTER BN_CTX_end()", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG("LEAVE BN_CTX_end()", ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
2,382
0
https://github.com/libav/libav/blob/556f8a066cb33241bf29e85d7e24c9acf7ea9043/libavcodec/h264.h/#L1064
static void fill_decode_caches(H264Context *h, int mb_type){ MpegEncContext * const s = &h->s; int topleft_xy, top_xy, topright_xy, left_xy[LEFT_MBS]; int topleft_type, top_type, topright_type, left_type[LEFT_MBS]; const uint8_t * left_block= h->left_block; int i; uint8_t *nnz; uint8_t *nnz_cache; topleft_xy = h->topleft_mb_xy; top_xy = h->top_mb_xy; topright_xy = h->topright_mb_xy; left_xy[LTOP] = h->left_mb_xy[LTOP]; left_xy[LBOT] = h->left_mb_xy[LBOT]; topleft_type = h->topleft_type; top_type = h->top_type; topright_type = h->topright_type; left_type[LTOP]= h->left_type[LTOP]; left_type[LBOT]= h->left_type[LBOT]; if(!IS_SKIP(mb_type)){ if(IS_INTRA(mb_type)){ int type_mask= h->pps.constrained_intra_pred ? IS_INTRA(-1) : -1; h->topleft_samples_available= h->top_samples_available= h->left_samples_available= 0xFFFF; h->topright_samples_available= 0xEEEA; if(!(top_type & type_mask)){ h->topleft_samples_available= 0xB3FF; h->top_samples_available= 0x33FF; h->topright_samples_available= 0x26EA; } if(IS_INTERLACED(mb_type) != IS_INTERLACED(left_type[LTOP])){ if(IS_INTERLACED(mb_type)){ if(!(left_type[LTOP] & type_mask)){ h->topleft_samples_available&= 0xDFFF; h->left_samples_available&= 0x5FFF; } if(!(left_type[LBOT] & type_mask)){ h->topleft_samples_available&= 0xFF5F; h->left_samples_available&= 0xFF5F; } }else{ int left_typei = s->current_picture.mb_type[left_xy[LTOP] + s->mb_stride]; assert(left_xy[LTOP] == left_xy[LBOT]); if(!((left_typei & type_mask) && (left_type[LTOP] & type_mask))){ h->topleft_samples_available&= 0xDF5F; h->left_samples_available&= 0x5F5F; } } }else{ if(!(left_type[LTOP] & type_mask)){ h->topleft_samples_available&= 0xDF5F; h->left_samples_available&= 0x5F5F; } } if(!(topleft_type & type_mask)) h->topleft_samples_available&= 0x7FFF; if(!(topright_type & type_mask)) h->topright_samples_available&= 0xFBFF; if(IS_INTRA4x4(mb_type)){ if(IS_INTRA4x4(top_type)){ AV_COPY32(h->intra4x4_pred_mode_cache+4+8*0, h->intra4x4_pred_mode + h->mb2br_xy[top_xy]); }else{ h->intra4x4_pred_mode_cache[4+8*0]= h->intra4x4_pred_mode_cache[5+8*0]= h->intra4x4_pred_mode_cache[6+8*0]= h->intra4x4_pred_mode_cache[7+8*0]= 2 - 3*!(top_type & type_mask); } for(i=0; i<2; i++){ if(IS_INTRA4x4(left_type[LEFT(i)])){ int8_t *mode= h->intra4x4_pred_mode + h->mb2br_xy[left_xy[LEFT(i)]]; h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= mode[6-left_block[0+2*i]]; h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= mode[6-left_block[1+2*i]]; }else{ h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= 2 - 3*!(left_type[LEFT(i)] & type_mask); } } } } nnz_cache = h->non_zero_count_cache; if(top_type){ nnz = h->non_zero_count[top_xy]; AV_COPY32(&nnz_cache[4+8* 0], &nnz[4*3]); if(CHROMA444){ AV_COPY32(&nnz_cache[4+8* 5], &nnz[4* 7]); AV_COPY32(&nnz_cache[4+8*10], &nnz[4*11]); }else{ AV_COPY32(&nnz_cache[4+8* 5], &nnz[4* 5]); AV_COPY32(&nnz_cache[4+8*10], &nnz[4* 9]); } }else{ uint32_t top_empty = CABAC && !IS_INTRA(mb_type) ? 0 : 0x40404040; AV_WN32A(&nnz_cache[4+8* 0], top_empty); AV_WN32A(&nnz_cache[4+8* 5], top_empty); AV_WN32A(&nnz_cache[4+8*10], top_empty); } for (i=0; i<2; i++) { if(left_type[LEFT(i)]){ nnz = h->non_zero_count[left_xy[LEFT(i)]]; nnz_cache[3+8* 1 + 2*8*i]= nnz[left_block[8+0+2*i]]; nnz_cache[3+8* 2 + 2*8*i]= nnz[left_block[8+1+2*i]]; if(CHROMA444){ nnz_cache[3+8* 6 + 2*8*i]= nnz[left_block[8+0+2*i]+4*4]; nnz_cache[3+8* 7 + 2*8*i]= nnz[left_block[8+1+2*i]+4*4]; nnz_cache[3+8*11 + 2*8*i]= nnz[left_block[8+0+2*i]+8*4]; nnz_cache[3+8*12 + 2*8*i]= nnz[left_block[8+1+2*i]+8*4]; }else{ nnz_cache[3+8* 6 + 8*i]= nnz[left_block[8+4+2*i]]; nnz_cache[3+8*11 + 8*i]= nnz[left_block[8+5+2*i]]; } }else{ nnz_cache[3+8* 1 + 2*8*i]= nnz_cache[3+8* 2 + 2*8*i]= nnz_cache[3+8* 6 + 2*8*i]= nnz_cache[3+8* 7 + 2*8*i]= nnz_cache[3+8*11 + 2*8*i]= nnz_cache[3+8*12 + 2*8*i]= CABAC && !IS_INTRA(mb_type) ? 0 : 64; } } if( CABAC ) { if(top_type) { h->top_cbp = h->cbp_table[top_xy]; } else { h->top_cbp = IS_INTRA(mb_type) ? 0x7CF : 0x00F; } if (left_type[LTOP]) { h->left_cbp = (h->cbp_table[left_xy[LTOP]] & 0x7F0) | ((h->cbp_table[left_xy[LTOP]]>>(left_block[0]&(~1)))&2) | (((h->cbp_table[left_xy[LBOT]]>>(left_block[2]&(~1)))&2) << 2); } else { h->left_cbp = IS_INTRA(mb_type) ? 0x7CF : 0x00F; } } } if(IS_INTER(mb_type) || (IS_DIRECT(mb_type) && h->direct_spatial_mv_pred)){ int list; int b_stride = h->b_stride; for(list=0; list<h->list_count; list++){ int8_t *ref_cache = &h->ref_cache[list][scan8[0]]; int8_t *ref = s->current_picture.ref_index[list]; int16_t (*mv_cache)[2] = &h->mv_cache[list][scan8[0]]; int16_t (*mv)[2] = s->current_picture.motion_val[list]; if(!USES_LIST(mb_type, list)){ continue; } assert(!(IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred)); if(USES_LIST(top_type, list)){ const int b_xy= h->mb2b_xy[top_xy] + 3*b_stride; AV_COPY128(mv_cache[0 - 1*8], mv[b_xy + 0]); ref_cache[0 - 1*8]= ref_cache[1 - 1*8]= ref[4*top_xy + 2]; ref_cache[2 - 1*8]= ref_cache[3 - 1*8]= ref[4*top_xy + 3]; }else{ AV_ZERO128(mv_cache[0 - 1*8]); AV_WN32A(&ref_cache[0 - 1*8], ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101); } if(mb_type & (MB_TYPE_16x8|MB_TYPE_8x8)){ for(i=0; i<2; i++){ int cache_idx = -1 + i*2*8; if(USES_LIST(left_type[LEFT(i)], list)){ const int b_xy= h->mb2b_xy[left_xy[LEFT(i)]] + 3; const int b8_xy= 4*left_xy[LEFT(i)] + 1; AV_COPY32(mv_cache[cache_idx ], mv[b_xy + b_stride*left_block[0+i*2]]); AV_COPY32(mv_cache[cache_idx+8], mv[b_xy + b_stride*left_block[1+i*2]]); ref_cache[cache_idx ]= ref[b8_xy + (left_block[0+i*2]&~1)]; ref_cache[cache_idx+8]= ref[b8_xy + (left_block[1+i*2]&~1)]; }else{ AV_ZERO32(mv_cache[cache_idx ]); AV_ZERO32(mv_cache[cache_idx+8]); ref_cache[cache_idx ]= ref_cache[cache_idx+8]= (left_type[LEFT(i)]) ? LIST_NOT_USED : PART_NOT_AVAILABLE; } } }else{ if(USES_LIST(left_type[LTOP], list)){ const int b_xy= h->mb2b_xy[left_xy[LTOP]] + 3; const int b8_xy= 4*left_xy[LTOP] + 1; AV_COPY32(mv_cache[-1], mv[b_xy + b_stride*left_block[0]]); ref_cache[-1]= ref[b8_xy + (left_block[0]&~1)]; }else{ AV_ZERO32(mv_cache[-1]); ref_cache[-1]= left_type[LTOP] ? LIST_NOT_USED : PART_NOT_AVAILABLE; } } if(USES_LIST(topright_type, list)){ const int b_xy= h->mb2b_xy[topright_xy] + 3*b_stride; AV_COPY32(mv_cache[4 - 1*8], mv[b_xy]); ref_cache[4 - 1*8]= ref[4*topright_xy + 2]; }else{ AV_ZERO32(mv_cache[4 - 1*8]); ref_cache[4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE; } if(ref_cache[4 - 1*8] < 0){ if(USES_LIST(topleft_type, list)){ const int b_xy = h->mb2b_xy[topleft_xy] + 3 + b_stride + (h->topleft_partition & 2*b_stride); const int b8_xy= 4*topleft_xy + 1 + (h->topleft_partition & 2); AV_COPY32(mv_cache[-1 - 1*8], mv[b_xy]); ref_cache[-1 - 1*8]= ref[b8_xy]; }else{ AV_ZERO32(mv_cache[-1 - 1*8]); ref_cache[-1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE; } } if((mb_type&(MB_TYPE_SKIP|MB_TYPE_DIRECT2)) && !FRAME_MBAFF) continue; if(!(mb_type&(MB_TYPE_SKIP|MB_TYPE_DIRECT2))){ uint8_t (*mvd_cache)[2] = &h->mvd_cache[list][scan8[0]]; uint8_t (*mvd)[2] = h->mvd_table[list]; ref_cache[2+8*0] = ref_cache[2+8*2] = PART_NOT_AVAILABLE; AV_ZERO32(mv_cache[2+8*0]); AV_ZERO32(mv_cache[2+8*2]); if( CABAC ) { if(USES_LIST(top_type, list)){ const int b_xy= h->mb2br_xy[top_xy]; AV_COPY64(mvd_cache[0 - 1*8], mvd[b_xy + 0]); }else{ AV_ZERO64(mvd_cache[0 - 1*8]); } if(USES_LIST(left_type[LTOP], list)){ const int b_xy= h->mb2br_xy[left_xy[LTOP]] + 6; AV_COPY16(mvd_cache[-1 + 0*8], mvd[b_xy - left_block[0]]); AV_COPY16(mvd_cache[-1 + 1*8], mvd[b_xy - left_block[1]]); }else{ AV_ZERO16(mvd_cache[-1 + 0*8]); AV_ZERO16(mvd_cache[-1 + 1*8]); } if(USES_LIST(left_type[LBOT], list)){ const int b_xy= h->mb2br_xy[left_xy[LBOT]] + 6; AV_COPY16(mvd_cache[-1 + 2*8], mvd[b_xy - left_block[2]]); AV_COPY16(mvd_cache[-1 + 3*8], mvd[b_xy - left_block[3]]); }else{ AV_ZERO16(mvd_cache[-1 + 2*8]); AV_ZERO16(mvd_cache[-1 + 3*8]); } AV_ZERO16(mvd_cache[2+8*0]); AV_ZERO16(mvd_cache[2+8*2]); if(h->slice_type_nos == AV_PICTURE_TYPE_B){ uint8_t *direct_cache = &h->direct_cache[scan8[0]]; uint8_t *direct_table = h->direct_table; fill_rectangle(direct_cache, 4, 4, 8, MB_TYPE_16x16>>1, 1); if(IS_DIRECT(top_type)){ AV_WN32A(&direct_cache[-1*8], 0x01010101u*(MB_TYPE_DIRECT2>>1)); }else if(IS_8X8(top_type)){ int b8_xy = 4*top_xy; direct_cache[0 - 1*8]= direct_table[b8_xy + 2]; direct_cache[2 - 1*8]= direct_table[b8_xy + 3]; }else{ AV_WN32A(&direct_cache[-1*8], 0x01010101*(MB_TYPE_16x16>>1)); } if(IS_DIRECT(left_type[LTOP])) direct_cache[-1 + 0*8]= MB_TYPE_DIRECT2>>1; else if(IS_8X8(left_type[LTOP])) direct_cache[-1 + 0*8]= direct_table[4*left_xy[LTOP] + 1 + (left_block[0]&~1)]; else direct_cache[-1 + 0*8]= MB_TYPE_16x16>>1; if(IS_DIRECT(left_type[LBOT])) direct_cache[-1 + 2*8]= MB_TYPE_DIRECT2>>1; else if(IS_8X8(left_type[LBOT])) direct_cache[-1 + 2*8]= direct_table[4*left_xy[LBOT] + 1 + (left_block[2]&~1)]; else direct_cache[-1 + 2*8]= MB_TYPE_16x16>>1; } } } if(FRAME_MBAFF){ #define MAP_MVS\ MAP_F2F(scan8[0] - 1 - 1*8, topleft_type)\ MAP_F2F(scan8[0] + 0 - 1*8, top_type)\ MAP_F2F(scan8[0] + 1 - 1*8, top_type)\ MAP_F2F(scan8[0] + 2 - 1*8, top_type)\ MAP_F2F(scan8[0] + 3 - 1*8, top_type)\ MAP_F2F(scan8[0] + 4 - 1*8, topright_type)\ MAP_F2F(scan8[0] - 1 + 0*8, left_type[LTOP])\ MAP_F2F(scan8[0] - 1 + 1*8, left_type[LTOP])\ MAP_F2F(scan8[0] - 1 + 2*8, left_type[LBOT])\ MAP_F2F(scan8[0] - 1 + 3*8, left_type[LBOT]) if(MB_FIELD){ #define MAP_F2F(idx, mb_type)\ if(!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\ h->ref_cache[list][idx] <<= 1;\ h->mv_cache[list][idx][1] /= 2;\ h->mvd_cache[list][idx][1] >>=1;\ } MAP_MVS #undef MAP_F2F }else{ #define MAP_F2F(idx, mb_type)\ if(IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\ h->ref_cache[list][idx] >>= 1;\ h->mv_cache[list][idx][1] <<= 1;\ h->mvd_cache[list][idx][1] <<= 1;\ } MAP_MVS #undef MAP_F2F } } } } h->neighbor_transform_size= !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[LTOP]); }
['static void fill_decode_caches(H264Context *h, int mb_type){\n MpegEncContext * const s = &h->s;\n int topleft_xy, top_xy, topright_xy, left_xy[LEFT_MBS];\n int topleft_type, top_type, topright_type, left_type[LEFT_MBS];\n const uint8_t * left_block= h->left_block;\n int i;\n uint8_t *nnz;\n uint8_t *nnz_cache;\n topleft_xy = h->topleft_mb_xy;\n top_xy = h->top_mb_xy;\n topright_xy = h->topright_mb_xy;\n left_xy[LTOP] = h->left_mb_xy[LTOP];\n left_xy[LBOT] = h->left_mb_xy[LBOT];\n topleft_type = h->topleft_type;\n top_type = h->top_type;\n topright_type = h->topright_type;\n left_type[LTOP]= h->left_type[LTOP];\n left_type[LBOT]= h->left_type[LBOT];\n if(!IS_SKIP(mb_type)){\n if(IS_INTRA(mb_type)){\n int type_mask= h->pps.constrained_intra_pred ? IS_INTRA(-1) : -1;\n h->topleft_samples_available=\n h->top_samples_available=\n h->left_samples_available= 0xFFFF;\n h->topright_samples_available= 0xEEEA;\n if(!(top_type & type_mask)){\n h->topleft_samples_available= 0xB3FF;\n h->top_samples_available= 0x33FF;\n h->topright_samples_available= 0x26EA;\n }\n if(IS_INTERLACED(mb_type) != IS_INTERLACED(left_type[LTOP])){\n if(IS_INTERLACED(mb_type)){\n if(!(left_type[LTOP] & type_mask)){\n h->topleft_samples_available&= 0xDFFF;\n h->left_samples_available&= 0x5FFF;\n }\n if(!(left_type[LBOT] & type_mask)){\n h->topleft_samples_available&= 0xFF5F;\n h->left_samples_available&= 0xFF5F;\n }\n }else{\n int left_typei = s->current_picture.mb_type[left_xy[LTOP] + s->mb_stride];\n assert(left_xy[LTOP] == left_xy[LBOT]);\n if(!((left_typei & type_mask) && (left_type[LTOP] & type_mask))){\n h->topleft_samples_available&= 0xDF5F;\n h->left_samples_available&= 0x5F5F;\n }\n }\n }else{\n if(!(left_type[LTOP] & type_mask)){\n h->topleft_samples_available&= 0xDF5F;\n h->left_samples_available&= 0x5F5F;\n }\n }\n if(!(topleft_type & type_mask))\n h->topleft_samples_available&= 0x7FFF;\n if(!(topright_type & type_mask))\n h->topright_samples_available&= 0xFBFF;\n if(IS_INTRA4x4(mb_type)){\n if(IS_INTRA4x4(top_type)){\n AV_COPY32(h->intra4x4_pred_mode_cache+4+8*0, h->intra4x4_pred_mode + h->mb2br_xy[top_xy]);\n }else{\n h->intra4x4_pred_mode_cache[4+8*0]=\n h->intra4x4_pred_mode_cache[5+8*0]=\n h->intra4x4_pred_mode_cache[6+8*0]=\n h->intra4x4_pred_mode_cache[7+8*0]= 2 - 3*!(top_type & type_mask);\n }\n for(i=0; i<2; i++){\n if(IS_INTRA4x4(left_type[LEFT(i)])){\n int8_t *mode= h->intra4x4_pred_mode + h->mb2br_xy[left_xy[LEFT(i)]];\n h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= mode[6-left_block[0+2*i]];\n h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= mode[6-left_block[1+2*i]];\n }else{\n h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=\n h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= 2 - 3*!(left_type[LEFT(i)] & type_mask);\n }\n }\n }\n }\n nnz_cache = h->non_zero_count_cache;\n if(top_type){\n nnz = h->non_zero_count[top_xy];\n AV_COPY32(&nnz_cache[4+8* 0], &nnz[4*3]);\n if(CHROMA444){\n AV_COPY32(&nnz_cache[4+8* 5], &nnz[4* 7]);\n AV_COPY32(&nnz_cache[4+8*10], &nnz[4*11]);\n }else{\n AV_COPY32(&nnz_cache[4+8* 5], &nnz[4* 5]);\n AV_COPY32(&nnz_cache[4+8*10], &nnz[4* 9]);\n }\n }else{\n uint32_t top_empty = CABAC && !IS_INTRA(mb_type) ? 0 : 0x40404040;\n AV_WN32A(&nnz_cache[4+8* 0], top_empty);\n AV_WN32A(&nnz_cache[4+8* 5], top_empty);\n AV_WN32A(&nnz_cache[4+8*10], top_empty);\n }\n for (i=0; i<2; i++) {\n if(left_type[LEFT(i)]){\n nnz = h->non_zero_count[left_xy[LEFT(i)]];\n nnz_cache[3+8* 1 + 2*8*i]= nnz[left_block[8+0+2*i]];\n nnz_cache[3+8* 2 + 2*8*i]= nnz[left_block[8+1+2*i]];\n if(CHROMA444){\n nnz_cache[3+8* 6 + 2*8*i]= nnz[left_block[8+0+2*i]+4*4];\n nnz_cache[3+8* 7 + 2*8*i]= nnz[left_block[8+1+2*i]+4*4];\n nnz_cache[3+8*11 + 2*8*i]= nnz[left_block[8+0+2*i]+8*4];\n nnz_cache[3+8*12 + 2*8*i]= nnz[left_block[8+1+2*i]+8*4];\n }else{\n nnz_cache[3+8* 6 + 8*i]= nnz[left_block[8+4+2*i]];\n nnz_cache[3+8*11 + 8*i]= nnz[left_block[8+5+2*i]];\n }\n }else{\n nnz_cache[3+8* 1 + 2*8*i]=\n nnz_cache[3+8* 2 + 2*8*i]=\n nnz_cache[3+8* 6 + 2*8*i]=\n nnz_cache[3+8* 7 + 2*8*i]=\n nnz_cache[3+8*11 + 2*8*i]=\n nnz_cache[3+8*12 + 2*8*i]= CABAC && !IS_INTRA(mb_type) ? 0 : 64;\n }\n }\n if( CABAC ) {\n if(top_type) {\n h->top_cbp = h->cbp_table[top_xy];\n } else {\n h->top_cbp = IS_INTRA(mb_type) ? 0x7CF : 0x00F;\n }\n if (left_type[LTOP]) {\n h->left_cbp = (h->cbp_table[left_xy[LTOP]] & 0x7F0)\n | ((h->cbp_table[left_xy[LTOP]]>>(left_block[0]&(~1)))&2)\n | (((h->cbp_table[left_xy[LBOT]]>>(left_block[2]&(~1)))&2) << 2);\n } else {\n h->left_cbp = IS_INTRA(mb_type) ? 0x7CF : 0x00F;\n }\n }\n }\n if(IS_INTER(mb_type) || (IS_DIRECT(mb_type) && h->direct_spatial_mv_pred)){\n int list;\n int b_stride = h->b_stride;\n for(list=0; list<h->list_count; list++){\n int8_t *ref_cache = &h->ref_cache[list][scan8[0]];\n int8_t *ref = s->current_picture.ref_index[list];\n int16_t (*mv_cache)[2] = &h->mv_cache[list][scan8[0]];\n int16_t (*mv)[2] = s->current_picture.motion_val[list];\n if(!USES_LIST(mb_type, list)){\n continue;\n }\n assert(!(IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred));\n if(USES_LIST(top_type, list)){\n const int b_xy= h->mb2b_xy[top_xy] + 3*b_stride;\n AV_COPY128(mv_cache[0 - 1*8], mv[b_xy + 0]);\n ref_cache[0 - 1*8]=\n ref_cache[1 - 1*8]= ref[4*top_xy + 2];\n ref_cache[2 - 1*8]=\n ref_cache[3 - 1*8]= ref[4*top_xy + 3];\n }else{\n AV_ZERO128(mv_cache[0 - 1*8]);\n AV_WN32A(&ref_cache[0 - 1*8], ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101);\n }\n if(mb_type & (MB_TYPE_16x8|MB_TYPE_8x8)){\n for(i=0; i<2; i++){\n int cache_idx = -1 + i*2*8;\n if(USES_LIST(left_type[LEFT(i)], list)){\n const int b_xy= h->mb2b_xy[left_xy[LEFT(i)]] + 3;\n const int b8_xy= 4*left_xy[LEFT(i)] + 1;\n AV_COPY32(mv_cache[cache_idx ], mv[b_xy + b_stride*left_block[0+i*2]]);\n AV_COPY32(mv_cache[cache_idx+8], mv[b_xy + b_stride*left_block[1+i*2]]);\n ref_cache[cache_idx ]= ref[b8_xy + (left_block[0+i*2]&~1)];\n ref_cache[cache_idx+8]= ref[b8_xy + (left_block[1+i*2]&~1)];\n }else{\n AV_ZERO32(mv_cache[cache_idx ]);\n AV_ZERO32(mv_cache[cache_idx+8]);\n ref_cache[cache_idx ]=\n ref_cache[cache_idx+8]= (left_type[LEFT(i)]) ? LIST_NOT_USED : PART_NOT_AVAILABLE;\n }\n }\n }else{\n if(USES_LIST(left_type[LTOP], list)){\n const int b_xy= h->mb2b_xy[left_xy[LTOP]] + 3;\n const int b8_xy= 4*left_xy[LTOP] + 1;\n AV_COPY32(mv_cache[-1], mv[b_xy + b_stride*left_block[0]]);\n ref_cache[-1]= ref[b8_xy + (left_block[0]&~1)];\n }else{\n AV_ZERO32(mv_cache[-1]);\n ref_cache[-1]= left_type[LTOP] ? LIST_NOT_USED : PART_NOT_AVAILABLE;\n }\n }\n if(USES_LIST(topright_type, list)){\n const int b_xy= h->mb2b_xy[topright_xy] + 3*b_stride;\n AV_COPY32(mv_cache[4 - 1*8], mv[b_xy]);\n ref_cache[4 - 1*8]= ref[4*topright_xy + 2];\n }else{\n AV_ZERO32(mv_cache[4 - 1*8]);\n ref_cache[4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;\n }\n if(ref_cache[4 - 1*8] < 0){\n if(USES_LIST(topleft_type, list)){\n const int b_xy = h->mb2b_xy[topleft_xy] + 3 + b_stride + (h->topleft_partition & 2*b_stride);\n const int b8_xy= 4*topleft_xy + 1 + (h->topleft_partition & 2);\n AV_COPY32(mv_cache[-1 - 1*8], mv[b_xy]);\n ref_cache[-1 - 1*8]= ref[b8_xy];\n }else{\n AV_ZERO32(mv_cache[-1 - 1*8]);\n ref_cache[-1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;\n }\n }\n if((mb_type&(MB_TYPE_SKIP|MB_TYPE_DIRECT2)) && !FRAME_MBAFF)\n continue;\n if(!(mb_type&(MB_TYPE_SKIP|MB_TYPE_DIRECT2))){\n uint8_t (*mvd_cache)[2] = &h->mvd_cache[list][scan8[0]];\n uint8_t (*mvd)[2] = h->mvd_table[list];\n ref_cache[2+8*0] =\n ref_cache[2+8*2] = PART_NOT_AVAILABLE;\n AV_ZERO32(mv_cache[2+8*0]);\n AV_ZERO32(mv_cache[2+8*2]);\n if( CABAC ) {\n if(USES_LIST(top_type, list)){\n const int b_xy= h->mb2br_xy[top_xy];\n AV_COPY64(mvd_cache[0 - 1*8], mvd[b_xy + 0]);\n }else{\n AV_ZERO64(mvd_cache[0 - 1*8]);\n }\n if(USES_LIST(left_type[LTOP], list)){\n const int b_xy= h->mb2br_xy[left_xy[LTOP]] + 6;\n AV_COPY16(mvd_cache[-1 + 0*8], mvd[b_xy - left_block[0]]);\n AV_COPY16(mvd_cache[-1 + 1*8], mvd[b_xy - left_block[1]]);\n }else{\n AV_ZERO16(mvd_cache[-1 + 0*8]);\n AV_ZERO16(mvd_cache[-1 + 1*8]);\n }\n if(USES_LIST(left_type[LBOT], list)){\n const int b_xy= h->mb2br_xy[left_xy[LBOT]] + 6;\n AV_COPY16(mvd_cache[-1 + 2*8], mvd[b_xy - left_block[2]]);\n AV_COPY16(mvd_cache[-1 + 3*8], mvd[b_xy - left_block[3]]);\n }else{\n AV_ZERO16(mvd_cache[-1 + 2*8]);\n AV_ZERO16(mvd_cache[-1 + 3*8]);\n }\n AV_ZERO16(mvd_cache[2+8*0]);\n AV_ZERO16(mvd_cache[2+8*2]);\n if(h->slice_type_nos == AV_PICTURE_TYPE_B){\n uint8_t *direct_cache = &h->direct_cache[scan8[0]];\n uint8_t *direct_table = h->direct_table;\n fill_rectangle(direct_cache, 4, 4, 8, MB_TYPE_16x16>>1, 1);\n if(IS_DIRECT(top_type)){\n AV_WN32A(&direct_cache[-1*8], 0x01010101u*(MB_TYPE_DIRECT2>>1));\n }else if(IS_8X8(top_type)){\n int b8_xy = 4*top_xy;\n direct_cache[0 - 1*8]= direct_table[b8_xy + 2];\n direct_cache[2 - 1*8]= direct_table[b8_xy + 3];\n }else{\n AV_WN32A(&direct_cache[-1*8], 0x01010101*(MB_TYPE_16x16>>1));\n }\n if(IS_DIRECT(left_type[LTOP]))\n direct_cache[-1 + 0*8]= MB_TYPE_DIRECT2>>1;\n else if(IS_8X8(left_type[LTOP]))\n direct_cache[-1 + 0*8]= direct_table[4*left_xy[LTOP] + 1 + (left_block[0]&~1)];\n else\n direct_cache[-1 + 0*8]= MB_TYPE_16x16>>1;\n if(IS_DIRECT(left_type[LBOT]))\n direct_cache[-1 + 2*8]= MB_TYPE_DIRECT2>>1;\n else if(IS_8X8(left_type[LBOT]))\n direct_cache[-1 + 2*8]= direct_table[4*left_xy[LBOT] + 1 + (left_block[2]&~1)];\n else\n direct_cache[-1 + 2*8]= MB_TYPE_16x16>>1;\n }\n }\n }\n if(FRAME_MBAFF){\n#define MAP_MVS\\\n MAP_F2F(scan8[0] - 1 - 1*8, topleft_type)\\\n MAP_F2F(scan8[0] + 0 - 1*8, top_type)\\\n MAP_F2F(scan8[0] + 1 - 1*8, top_type)\\\n MAP_F2F(scan8[0] + 2 - 1*8, top_type)\\\n MAP_F2F(scan8[0] + 3 - 1*8, top_type)\\\n MAP_F2F(scan8[0] + 4 - 1*8, topright_type)\\\n MAP_F2F(scan8[0] - 1 + 0*8, left_type[LTOP])\\\n MAP_F2F(scan8[0] - 1 + 1*8, left_type[LTOP])\\\n MAP_F2F(scan8[0] - 1 + 2*8, left_type[LBOT])\\\n MAP_F2F(scan8[0] - 1 + 3*8, left_type[LBOT])\n if(MB_FIELD){\n#define MAP_F2F(idx, mb_type)\\\n if(!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\\\n h->ref_cache[list][idx] <<= 1;\\\n h->mv_cache[list][idx][1] /= 2;\\\n h->mvd_cache[list][idx][1] >>=1;\\\n }\n MAP_MVS\n#undef MAP_F2F\n }else{\n#define MAP_F2F(idx, mb_type)\\\n if(IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0){\\\n h->ref_cache[list][idx] >>= 1;\\\n h->mv_cache[list][idx][1] <<= 1;\\\n h->mvd_cache[list][idx][1] <<= 1;\\\n }\n MAP_MVS\n#undef MAP_F2F\n }\n }\n }\n }\n h->neighbor_transform_size= !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[LTOP]);\n}']
2,383
0
https://github.com/openssl/openssl/blob/ea32151f7b9353f8906188d007c6893704ac17bb/crypto/bn/bn_lib.c/#L289
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *A, *a = NULL; const BN_ULONG *B; int i; bn_check_top(b); if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return (NULL); } if (BN_get_flags(b,BN_FLG_SECURE)) a = A = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = A = OPENSSL_zalloc(words * sizeof(*a)); if (A == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return (NULL); } #if 1 B = b->d; if (B != NULL) { for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) { BN_ULONG a0, a1, a2, a3; a0 = B[0]; a1 = B[1]; a2 = B[2]; a3 = B[3]; A[0] = a0; A[1] = a1; A[2] = a2; A[3] = a3; } switch (b->top & 3) { case 3: A[2] = B[2]; case 2: A[1] = B[1]; case 1: A[0] = B[0]; case 0: ; } } #else memset(A, 0, sizeof(*A) * words); memcpy(A, b->d, sizeof(b->d[0]) * b->top); #endif return (a); }
["char *BN_bn2dec(const BIGNUM *a)\n{\n int i = 0, num, ok = 0;\n char *buf = NULL;\n char *p;\n BIGNUM *t = NULL;\n BN_ULONG *bn_data = NULL, *lp;\n i = BN_num_bits(a) * 3;\n num = (i / 10 + i / 1000 + 1) + 1;\n bn_data = OPENSSL_malloc((num / BN_DEC_NUM + 1) * sizeof(BN_ULONG));\n buf = OPENSSL_malloc(num + 3);\n if ((buf == NULL) || (bn_data == NULL)) {\n BNerr(BN_F_BN_BN2DEC, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if ((t = BN_dup(a)) == NULL)\n goto err;\n#define BUF_REMAIN (num+3 - (size_t)(p - buf))\n p = buf;\n lp = bn_data;\n if (BN_is_zero(t)) {\n *(p++) = '0';\n *(p++) = '\\0';\n } else {\n if (BN_is_negative(t))\n *p++ = '-';\n i = 0;\n while (!BN_is_zero(t)) {\n *lp = BN_div_word(t, BN_DEC_CONV);\n lp++;\n }\n lp--;\n BIO_snprintf(p, BUF_REMAIN, BN_DEC_FMT1, *lp);\n while (*p)\n p++;\n while (lp != bn_data) {\n lp--;\n BIO_snprintf(p, BUF_REMAIN, BN_DEC_FMT2, *lp);\n while (*p)\n p++;\n }\n }\n ok = 1;\n err:\n OPENSSL_free(bn_data);\n BN_free(t);\n if (ok)\n return buf;\n OPENSSL_free(buf);\n return NULL;\n}", 'BIGNUM *BN_dup(const BIGNUM *a)\n{\n BIGNUM *t;\n if (a == NULL)\n return NULL;\n bn_check_top(a);\n t = BN_get_flags(a, BN_FLG_SECURE) ? BN_secure_new() : BN_new();\n if (t == NULL)\n return NULL;\n if (!BN_copy(t, a)) {\n BN_free(t);\n return NULL;\n }\n bn_check_top(t);\n return t;\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n int i;\n BN_ULONG *A;\n const BN_ULONG *B;\n bn_check_top(b);\n if (a == b)\n return (a);\n if (bn_wexpand(a, b->top) == NULL)\n return (NULL);\n#if 1\n A = a->d;\n B = b->d;\n for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {\n BN_ULONG a0, a1, a2, a3;\n a0 = B[0];\n a1 = B[1];\n a2 = B[2];\n a3 = B[3];\n A[0] = a0;\n A[1] = a1;\n A[2] = a2;\n A[3] = a3;\n }\n switch (b->top & 3) {\n case 3:\n A[2] = B[2];\n case 2:\n A[1] = B[1];\n case 1:\n A[0] = B[0];\n case 0:;\n }\n#else\n memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n#endif\n a->top = b->top;\n a->neg = b->neg;\n bn_check_top(a);\n return (a);\n}', 'BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w)\n{\n BN_ULONG ret = 0;\n int i, j;\n bn_check_top(a);\n w &= BN_MASK2;\n if (!w)\n return (BN_ULONG)-1;\n if (a->top == 0)\n return 0;\n j = BN_BITS2 - BN_num_bits_word(w);\n w <<= j;\n if (!BN_lshift(a, a, j))\n return (BN_ULONG)-1;\n for (i = a->top - 1; i >= 0; i--) {\n BN_ULONG l, d;\n l = a->d[i];\n d = bn_div_words(ret, l, w);\n ret = (l - ((d * w) & BN_MASK2)) & BN_MASK2;\n a->d[i] = d;\n }\n if ((a->top > 0) && (a->d[a->top - 1] == 0))\n a->top--;\n ret >>= j;\n bn_check_top(a);\n return (ret);\n}', 'int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l;\n bn_check_top(r);\n bn_check_top(a);\n if (n < 0) {\n BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n r->neg = a->neg;\n nw = n / BN_BITS2;\n if (bn_wexpand(r, a->top + nw + 1) == NULL)\n return (0);\n lb = n % BN_BITS2;\n rb = BN_BITS2 - lb;\n f = a->d;\n t = r->d;\n t[a->top + nw] = 0;\n if (lb == 0)\n for (i = a->top - 1; i >= 0; i--)\n t[nw + i] = f[i];\n else\n for (i = a->top - 1; i >= 0; i--) {\n l = f[i];\n t[nw + i + 1] |= (l >> rb) & BN_MASK2;\n t[nw + i] = (l << lb) & BN_MASK2;\n }\n memset(t, 0, sizeof(*t) * nw);\n r->top = a->top + nw + 1;\n bn_correct_top(r);\n bn_check_top(r);\n return (1);\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *A, *a = NULL;\n const BN_ULONG *B;\n int i;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return (NULL);\n }\n if (BN_get_flags(b,BN_FLG_SECURE))\n a = A = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = A = OPENSSL_zalloc(words * sizeof(*a));\n if (A == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n#if 1\n B = b->d;\n if (B != NULL) {\n for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {\n BN_ULONG a0, a1, a2, a3;\n a0 = B[0];\n a1 = B[1];\n a2 = B[2];\n a3 = B[3];\n A[0] = a0;\n A[1] = a1;\n A[2] = a2;\n A[3] = a3;\n }\n switch (b->top & 3) {\n case 3:\n A[2] = B[2];\n case 2:\n A[1] = B[1];\n case 1:\n A[0] = B[0];\n case 0:\n ;\n }\n }\n#else\n memset(A, 0, sizeof(*A) * words);\n memcpy(A, b->d, sizeof(b->d[0]) * b->top);\n#endif\n return (a);\n}']
2,384
0
https://github.com/openssl/openssl/blob/d858c87653257185ead1c5baf3d84cd7276dd912/apps/s_server.c/#L3000
static int rev_body(char *hostname, int s, int stype, unsigned char *context) { char *buf = NULL; int i; int ret = 1; SSL *con; BIO *io, *ssl_bio, *sbio; buf = app_malloc(bufsize, "server rev buffer"); io = BIO_new(BIO_f_buffer()); ssl_bio = BIO_new(BIO_f_ssl()); if ((io == NULL) || (ssl_bio == NULL)) goto err; if (!BIO_set_write_buffer_size(io, bufsize)) goto err; if ((con = SSL_new(ctx)) == NULL) goto err; if (s_tlsextdebug) { SSL_set_tlsext_debug_callback(con, tlsext_cb); SSL_set_tlsext_debug_arg(con, bio_s_out); } if (context && !SSL_set_session_id_context(con, context, strlen((char *)context))) { ERR_print_errors(bio_err); goto err; } sbio = BIO_new_socket(s, BIO_NOCLOSE); SSL_set_bio(con, sbio, sbio); SSL_set_accept_state(con); BIO_set_ssl(ssl_bio, con, BIO_CLOSE); BIO_push(io, ssl_bio); #ifdef CHARSET_EBCDIC io = BIO_push(BIO_new(BIO_f_ebcdic_filter()), io); #endif if (s_debug) { BIO_set_callback(SSL_get_rbio(con), bio_dump_callback); BIO_set_callback_arg(SSL_get_rbio(con), (char *)bio_s_out); } if (s_msg) { #ifndef OPENSSL_NO_SSL_TRACE if (s_msg == 2) SSL_set_msg_callback(con, SSL_trace); else #endif SSL_set_msg_callback(con, msg_cb); SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out); } for (;;) { i = BIO_do_handshake(io); if (i > 0) break; if (!BIO_should_retry(io)) { BIO_puts(bio_err, "CONNECTION FAILURE\n"); ERR_print_errors(bio_err); goto end; } #ifndef OPENSSL_NO_SRP if (BIO_should_io_special(io) && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) { BIO_printf(bio_s_out, "LOOKUP renego during accept\n"); srp_callback_parm.user = SRP_VBASE_get_by_user(srp_callback_parm.vb, srp_callback_parm.login); if (srp_callback_parm.user) BIO_printf(bio_s_out, "LOOKUP done %s\n", srp_callback_parm.user->info); else BIO_printf(bio_s_out, "LOOKUP not successful\n"); continue; } #endif } BIO_printf(bio_err, "CONNECTION ESTABLISHED\n"); print_ssl_summary(con); for (;;) { i = BIO_gets(io, buf, bufsize - 1); if (i < 0) { if (!BIO_should_retry(io)) { if (!s_quiet) ERR_print_errors(bio_err); goto err; } else { BIO_printf(bio_s_out, "read R BLOCK\n"); #ifndef OPENSSL_NO_SRP if (BIO_should_io_special(io) && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) { BIO_printf(bio_s_out, "LOOKUP renego during read\n"); srp_callback_parm.user = SRP_VBASE_get_by_user(srp_callback_parm.vb, srp_callback_parm.login); if (srp_callback_parm.user) BIO_printf(bio_s_out, "LOOKUP done %s\n", srp_callback_parm.user->info); else BIO_printf(bio_s_out, "LOOKUP not successful\n"); continue; } #endif #if defined(OPENSSL_SYS_NETWARE) delay(1000); #elif !defined(OPENSSL_SYS_MSDOS) sleep(1); #endif continue; } } else if (i == 0) { ret = 1; BIO_printf(bio_err, "CONNECTION CLOSED\n"); goto end; } else { char *p = buf + i - 1; while (i && (*p == '\n' || *p == '\r')) { p--; i--; } if (!s_ign_eof && (i == 5) && (strncmp(buf, "CLOSE", 5) == 0)) { ret = 1; BIO_printf(bio_err, "CONNECTION CLOSED\n"); goto end; } BUF_reverse((unsigned char *)buf, NULL, i); buf[i] = '\n'; BIO_write(io, buf, i + 1); for (;;) { i = BIO_flush(io); if (i > 0) break; if (!BIO_should_retry(io)) goto end; } } } end: SSL_set_shutdown(con, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN); err: OPENSSL_free(buf); BIO_free_all(io); return (ret); }
['static int rev_body(char *hostname, int s, int stype, unsigned char *context)\n{\n char *buf = NULL;\n int i;\n int ret = 1;\n SSL *con;\n BIO *io, *ssl_bio, *sbio;\n buf = app_malloc(bufsize, "server rev buffer");\n io = BIO_new(BIO_f_buffer());\n ssl_bio = BIO_new(BIO_f_ssl());\n if ((io == NULL) || (ssl_bio == NULL))\n goto err;\n if (!BIO_set_write_buffer_size(io, bufsize))\n goto err;\n if ((con = SSL_new(ctx)) == NULL)\n goto err;\n if (s_tlsextdebug) {\n SSL_set_tlsext_debug_callback(con, tlsext_cb);\n SSL_set_tlsext_debug_arg(con, bio_s_out);\n }\n if (context && !SSL_set_session_id_context(con, context,\n strlen((char *)context))) {\n ERR_print_errors(bio_err);\n goto err;\n }\n sbio = BIO_new_socket(s, BIO_NOCLOSE);\n SSL_set_bio(con, sbio, sbio);\n SSL_set_accept_state(con);\n BIO_set_ssl(ssl_bio, con, BIO_CLOSE);\n BIO_push(io, ssl_bio);\n#ifdef CHARSET_EBCDIC\n io = BIO_push(BIO_new(BIO_f_ebcdic_filter()), io);\n#endif\n if (s_debug) {\n BIO_set_callback(SSL_get_rbio(con), bio_dump_callback);\n BIO_set_callback_arg(SSL_get_rbio(con), (char *)bio_s_out);\n }\n if (s_msg) {\n#ifndef OPENSSL_NO_SSL_TRACE\n if (s_msg == 2)\n SSL_set_msg_callback(con, SSL_trace);\n else\n#endif\n SSL_set_msg_callback(con, msg_cb);\n SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out);\n }\n for (;;) {\n i = BIO_do_handshake(io);\n if (i > 0)\n break;\n if (!BIO_should_retry(io)) {\n BIO_puts(bio_err, "CONNECTION FAILURE\\n");\n ERR_print_errors(bio_err);\n goto end;\n }\n#ifndef OPENSSL_NO_SRP\n if (BIO_should_io_special(io)\n && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) {\n BIO_printf(bio_s_out, "LOOKUP renego during accept\\n");\n srp_callback_parm.user =\n SRP_VBASE_get_by_user(srp_callback_parm.vb,\n srp_callback_parm.login);\n if (srp_callback_parm.user)\n BIO_printf(bio_s_out, "LOOKUP done %s\\n",\n srp_callback_parm.user->info);\n else\n BIO_printf(bio_s_out, "LOOKUP not successful\\n");\n continue;\n }\n#endif\n }\n BIO_printf(bio_err, "CONNECTION ESTABLISHED\\n");\n print_ssl_summary(con);\n for (;;) {\n i = BIO_gets(io, buf, bufsize - 1);\n if (i < 0) {\n if (!BIO_should_retry(io)) {\n if (!s_quiet)\n ERR_print_errors(bio_err);\n goto err;\n } else {\n BIO_printf(bio_s_out, "read R BLOCK\\n");\n#ifndef OPENSSL_NO_SRP\n if (BIO_should_io_special(io)\n && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) {\n BIO_printf(bio_s_out, "LOOKUP renego during read\\n");\n srp_callback_parm.user =\n SRP_VBASE_get_by_user(srp_callback_parm.vb,\n srp_callback_parm.login);\n if (srp_callback_parm.user)\n BIO_printf(bio_s_out, "LOOKUP done %s\\n",\n srp_callback_parm.user->info);\n else\n BIO_printf(bio_s_out, "LOOKUP not successful\\n");\n continue;\n }\n#endif\n#if defined(OPENSSL_SYS_NETWARE)\n delay(1000);\n#elif !defined(OPENSSL_SYS_MSDOS)\n sleep(1);\n#endif\n continue;\n }\n } else if (i == 0) {\n ret = 1;\n BIO_printf(bio_err, "CONNECTION CLOSED\\n");\n goto end;\n } else {\n char *p = buf + i - 1;\n while (i && (*p == \'\\n\' || *p == \'\\r\')) {\n p--;\n i--;\n }\n if (!s_ign_eof && (i == 5) && (strncmp(buf, "CLOSE", 5) == 0)) {\n ret = 1;\n BIO_printf(bio_err, "CONNECTION CLOSED\\n");\n goto end;\n }\n BUF_reverse((unsigned char *)buf, NULL, i);\n buf[i] = \'\\n\';\n BIO_write(io, buf, i + 1);\n for (;;) {\n i = BIO_flush(io);\n if (i > 0)\n break;\n if (!BIO_should_retry(io))\n goto end;\n }\n }\n }\n end:\n SSL_set_shutdown(con, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);\n err:\n OPENSSL_free(buf);\n BIO_free_all(io);\n return (ret);\n}', 'void* app_malloc(int sz, const char *what)\n{\n void *vp = OPENSSL_malloc(sz);\n if (vp == NULL) {\n BIO_printf(bio_err, "%s: Could not allocate %d bytes for %s\\n",\n opt_getprog(), sz, what);\n ERR_print_errors(bio_err);\n exit(1);\n }\n return vp;\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}', 'BIO_METHOD *BIO_f_buffer(void)\n{\n return (&methods_buffer);\n}', 'BIO *BIO_new(BIO_METHOD *method)\n{\n BIO *ret = OPENSSL_malloc(sizeof(*ret));\n if (ret == NULL) {\n BIOerr(BIO_F_BIO_NEW, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n if (!BIO_set(ret, method)) {\n OPENSSL_free(ret);\n ret = NULL;\n }\n return (ret);\n}', 'void CRYPTO_free(void *str)\n{\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_free(str, 0);\n free(str);\n CRYPTO_mem_debug_free(str, 1);\n } else {\n free(str);\n }\n#else\n free(str);\n#endif\n}', 'BIO_METHOD *BIO_f_ssl(void)\n{\n return (&methods_sslp);\n}']
2,385
0
https://github.com/libav/libav/blob/13c7df3dc0625d7a559a7f02644c27755e0569bd/libavcodec/shorten.c/#L194
static int decode_wave_header(AVCodecContext *avctx, uint8_t *header, int header_size) { GetBitContext hb; int len; int chunk_size; short wave_format; init_get_bits(&hb, header, header_size*8); if (get_le32(&hb) != MKTAG('R','I','F','F')) { av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n"); return -1; } chunk_size = get_le32(&hb); if (get_le32(&hb) != MKTAG('W','A','V','E')) { av_log(avctx, AV_LOG_ERROR, "missing WAVE tag\n"); return -1; } while (get_le32(&hb) != MKTAG('f','m','t',' ')) { len = get_le32(&hb); skip_bits(&hb, 8*len); } len = get_le32(&hb); if (len < 16) { av_log(avctx, AV_LOG_ERROR, "fmt chunk was too short\n"); return -1; } wave_format = get_le16(&hb); switch (wave_format) { case WAVE_FORMAT_PCM: break; default: av_log(avctx, AV_LOG_ERROR, "unsupported wave format\n"); return -1; } avctx->channels = get_le16(&hb); avctx->sample_rate = get_le32(&hb); avctx->bit_rate = get_le32(&hb) * 8; avctx->block_align = get_le16(&hb); avctx->bits_per_coded_sample = get_le16(&hb); if (avctx->bits_per_coded_sample != 16) { av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample\n"); return -1; } len -= 16; if (len > 0) av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len); return 0; }
['static int decode_wave_header(AVCodecContext *avctx, uint8_t *header, int header_size)\n{\n GetBitContext hb;\n int len;\n int chunk_size;\n short wave_format;\n init_get_bits(&hb, header, header_size*8);\n if (get_le32(&hb) != MKTAG(\'R\',\'I\',\'F\',\'F\')) {\n av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\\n");\n return -1;\n }\n chunk_size = get_le32(&hb);\n if (get_le32(&hb) != MKTAG(\'W\',\'A\',\'V\',\'E\')) {\n av_log(avctx, AV_LOG_ERROR, "missing WAVE tag\\n");\n return -1;\n }\n while (get_le32(&hb) != MKTAG(\'f\',\'m\',\'t\',\' \')) {\n len = get_le32(&hb);\n skip_bits(&hb, 8*len);\n }\n len = get_le32(&hb);\n if (len < 16) {\n av_log(avctx, AV_LOG_ERROR, "fmt chunk was too short\\n");\n return -1;\n }\n wave_format = get_le16(&hb);\n switch (wave_format) {\n case WAVE_FORMAT_PCM:\n break;\n default:\n av_log(avctx, AV_LOG_ERROR, "unsupported wave format\\n");\n return -1;\n }\n avctx->channels = get_le16(&hb);\n avctx->sample_rate = get_le32(&hb);\n avctx->bit_rate = get_le32(&hb) * 8;\n avctx->block_align = get_le16(&hb);\n avctx->bits_per_coded_sample = get_le16(&hb);\n if (avctx->bits_per_coded_sample != 16) {\n av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample\\n");\n return -1;\n }\n len -= 16;\n if (len > 0)\n av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\\n", len);\n return 0;\n}', 'static inline void init_get_bits(GetBitContext *s,\n const uint8_t *buffer, int bit_size)\n{\n int buffer_size= (bit_size+7)>>3;\n if(buffer_size < 0 || bit_size < 0) {\n buffer_size = bit_size = 0;\n buffer = NULL;\n }\n s->buffer= buffer;\n s->size_in_bits= bit_size;\n s->buffer_end= buffer + buffer_size;\n#ifdef ALT_BITSTREAM_READER\n s->index=0;\n#elif defined LIBMPEG2_BITSTREAM_READER\n s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));\n s->bit_count = 16 + 8*((intptr_t)buffer&1);\n skip_bits_long(s, 0);\n#elif defined A32_BITSTREAM_READER\n s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));\n s->bit_count = 32 + 8*((intptr_t)buffer&3);\n skip_bits_long(s, 0);\n#endif\n}', 'static inline int get_le32(GetBitContext *gb)\n{\n return bswap_32(get_bits_long(gb, 32));\n}', 'static inline unsigned int get_bits_long(GetBitContext *s, int n){\n if(n<=17) return get_bits(s, n);\n else{\n#ifdef ALT_BITSTREAM_READER_LE\n int ret= get_bits(s, 16);\n return ret | (get_bits(s, n-16) << 16);\n#else\n int ret= get_bits(s, 16) << (n-16);\n return ret | get_bits(s, n-16);\n#endif\n }\n}', 'static inline unsigned int get_bits(GetBitContext *s, int n){\n register int tmp;\n OPEN_READER(re, s)\n UPDATE_CACHE(re, s)\n tmp= SHOW_UBITS(re, s, n);\n LAST_SKIP_BITS(re, s, n)\n CLOSE_READER(re, s)\n return tmp;\n}']
2,386
0
https://github.com/openssl/openssl/blob/eaeb1870d4ef887a89f0bcec6b5ee1ec7fafa00f/crypto/lhash/lhash.c/#L243
char *lh_delete(LHASH *lh, char *data) { unsigned long hash; LHASH_NODE *nn,**rn; char *ret; lh->error=0; rn=getrn(lh,data,&hash); if (*rn == NULL) { lh->num_no_delete++; return(NULL); } else { nn= *rn; *rn=nn->next; ret=nn->data; Free((char *)nn); lh->num_delete++; } lh->num_items--; if ((lh->num_nodes > MIN_NODES) && (lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes))) contract(lh); return(ret); }
['int MAIN(int argc, char **argv)\n\t{\n\tint ret=1;\n\tX509_REQ *req=NULL;\n\tX509 *x=NULL,*xca=NULL;\n\tEVP_PKEY *Upkey=NULL,*CApkey=NULL;\n\tint i,num,badops=0;\n\tBIO *out=NULL;\n\tBIO *STDout=NULL;\n\tint informat,outformat,keyformat,CAformat,CAkeyformat;\n\tchar *infile=NULL,*outfile=NULL,*keyfile=NULL,*CAfile=NULL;\n\tchar *CAkeyfile=NULL,*CAserial=NULL;\n\tint text=0,serial=0,hash=0,subject=0,issuer=0,startdate=0,enddate=0;\n\tint noout=0,sign_flag=0,CA_flag=0,CA_createserial=0;\n\tint C=0;\n\tint x509req=0,days=DEF_DAYS,modulus=0;\n\tchar **pp;\n\tX509_STORE *ctx=NULL;\n\tX509_REQ *rq=NULL;\n\tint fingerprint=0;\n\tchar buf[256];\n\tconst EVP_MD *md_alg,*digest=EVP_md5();\n\tLHASH *extconf = NULL;\n\tchar *extsect = NULL, *extfile = NULL;\n\treqfile=0;\n\tapps_startup();\n\tif (bio_err == NULL)\n\t\tbio_err=BIO_new_fp(stderr,BIO_NOCLOSE);\n\tSTDout=BIO_new_fp(stdout,BIO_NOCLOSE);\n\tinformat=FORMAT_PEM;\n\toutformat=FORMAT_PEM;\n\tkeyformat=FORMAT_PEM;\n\tCAformat=FORMAT_PEM;\n\tCAkeyformat=FORMAT_PEM;\n\tctx=X509_STORE_new();\n\tif (ctx == NULL) goto end;\n\tX509_STORE_set_verify_cb_func(ctx,callb);\n\targc--;\n\targv++;\n\tnum=0;\n\twhile (argc >= 1)\n\t\t{\n\t\tif \t(strcmp(*argv,"-inform") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tinformat=str2fmt(*(++argv));\n\t\t\t}\n\t\telse if (strcmp(*argv,"-outform") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\toutformat=str2fmt(*(++argv));\n\t\t\t}\n\t\telse if (strcmp(*argv,"-keyform") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tkeyformat=str2fmt(*(++argv));\n\t\t\t}\n\t\telse if (strcmp(*argv,"-req") == 0)\n\t\t\treqfile=1;\n\t\telse if (strcmp(*argv,"-CAform") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tCAformat=str2fmt(*(++argv));\n\t\t\t}\n\t\telse if (strcmp(*argv,"-CAkeyform") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tCAformat=str2fmt(*(++argv));\n\t\t\t}\n\t\telse if (strcmp(*argv,"-days") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tdays=atoi(*(++argv));\n\t\t\tif (days == 0)\n\t\t\t\t{\n\t\t\t\tBIO_printf(STDout,"bad number of days\\n");\n\t\t\t\tgoto bad;\n\t\t\t\t}\n\t\t\t}\n\t\telse if (strcmp(*argv,"-extfile") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\textfile= *(++argv);\n\t\t\t}\n\t\telse if (strcmp(*argv,"-in") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tinfile= *(++argv);\n\t\t\t}\n\t\telse if (strcmp(*argv,"-out") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\toutfile= *(++argv);\n\t\t\t}\n\t\telse if (strcmp(*argv,"-signkey") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tkeyfile= *(++argv);\n\t\t\tsign_flag= ++num;\n\t\t\t}\n\t\telse if (strcmp(*argv,"-CA") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tCAfile= *(++argv);\n\t\t\tCA_flag= ++num;\n\t\t\t}\n\t\telse if (strcmp(*argv,"-CAkey") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tCAkeyfile= *(++argv);\n\t\t\t}\n\t\telse if (strcmp(*argv,"-CAserial") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tCAserial= *(++argv);\n\t\t\t}\n\t\telse if (strcmp(*argv,"-C") == 0)\n\t\t\tC= ++num;\n\t\telse if (strcmp(*argv,"-serial") == 0)\n\t\t\tserial= ++num;\n\t\telse if (strcmp(*argv,"-modulus") == 0)\n\t\t\tmodulus= ++num;\n\t\telse if (strcmp(*argv,"-x509toreq") == 0)\n\t\t\tx509req= ++num;\n\t\telse if (strcmp(*argv,"-text") == 0)\n\t\t\ttext= ++num;\n\t\telse if (strcmp(*argv,"-hash") == 0)\n\t\t\thash= ++num;\n\t\telse if (strcmp(*argv,"-subject") == 0)\n\t\t\tsubject= ++num;\n\t\telse if (strcmp(*argv,"-issuer") == 0)\n\t\t\tissuer= ++num;\n\t\telse if (strcmp(*argv,"-fingerprint") == 0)\n\t\t\tfingerprint= ++num;\n\t\telse if (strcmp(*argv,"-dates") == 0)\n\t\t\t{\n\t\t\tstartdate= ++num;\n\t\t\tenddate= ++num;\n\t\t\t}\n\t\telse if (strcmp(*argv,"-startdate") == 0)\n\t\t\tstartdate= ++num;\n\t\telse if (strcmp(*argv,"-enddate") == 0)\n\t\t\tenddate= ++num;\n\t\telse if (strcmp(*argv,"-noout") == 0)\n\t\t\tnoout= ++num;\n\t\telse if (strcmp(*argv,"-CAcreateserial") == 0)\n\t\t\tCA_createserial= ++num;\n\t\telse if ((md_alg=EVP_get_digestbyname(&((*argv)[1]))) != NULL)\n\t\t\t{\n\t\t\tdigest=md_alg;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"unknown option %s\\n",*argv);\n\t\t\tbadops=1;\n\t\t\tbreak;\n\t\t\t}\n\t\targc--;\n\t\targv++;\n\t\t}\n\tif (badops)\n\t\t{\nbad:\n\t\tfor (pp=x509_usage; (*pp != NULL); pp++)\n\t\t\tBIO_printf(bio_err,*pp);\n\t\tgoto end;\n\t\t}\n\tERR_load_crypto_strings();\n\tX509V3_add_standard_extensions();\n\tif (!X509_STORE_set_default_paths(ctx))\n\t\t{\n\t\tERR_print_errors(bio_err);\n\t\tgoto end;\n\t\t}\n\tif ((CAkeyfile == NULL) && (CA_flag) && (CAformat == FORMAT_PEM))\n\t\t{ CAkeyfile=CAfile; }\n\telse if ((CA_flag) && (CAkeyfile == NULL))\n\t\t{\n\t\tBIO_printf(bio_err,"need to specify a CAkey if using the CA command\\n");\n\t\tgoto end;\n\t\t}\n\tif (extfile) {\n\t\tlong errorline;\n\t\tX509V3_CTX ctx2;\n\t\tif (!(extconf=CONF_load(NULL,extfile,&errorline))) {\n\t\t\tif (errorline <= 0)\n\t\t\t\tBIO_printf(bio_err,\n\t\t\t\t\t"error loading the config file \'%s\'\\n",\n\t\t\t\t\t\t\t\textfile);\n \telse\n \tBIO_printf(bio_err,\n\t\t\t\t "error on line %ld of config file \'%s\'\\n"\n\t\t\t\t\t\t\t,errorline,extfile);\n\t\t\tgoto end;\n\t\t}\n\t\tif(!(extsect = CONF_get_string(extconf, "default",\n\t\t\t\t\t "extensions"))) extsect = "default";\n\t\tX509V3_set_ctx_test(&ctx2);\n\t\tX509V3_set_conf_lhash(&ctx2, extconf);\n\t\tif(!X509V3_EXT_add_conf(extconf, &ctx2, extsect, NULL)) {\n\t\t\tBIO_printf(bio_err,\n\t\t\t\t"Error Loading extension section %s\\n",\n\t\t\t\t\t\t\t\t extsect);\n\t\t\tERR_print_errors(bio_err);\n\t\t\tgoto end;\n }\n\t}\n\tif (reqfile)\n\t\t{\n\t\tEVP_PKEY *pkey;\n\t\tX509_CINF *ci;\n\t\tBIO *in;\n\t\tif (!sign_flag && !CA_flag)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"We need a private key to sign with\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\tin=BIO_new(BIO_s_file());\n\t\tif (in == NULL)\n\t\t\t{\n\t\t\tERR_print_errors(bio_err);\n\t\t\tgoto end;\n\t\t\t}\n\t\tif (infile == NULL)\n\t\t\tBIO_set_fp(in,stdin,BIO_NOCLOSE|BIO_FP_TEXT);\n\t\telse\n\t\t\t{\n\t\t\tif (BIO_read_filename(in,infile) <= 0)\n\t\t\t\t{\n\t\t\t\tperror(infile);\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\t}\n\t\treq=PEM_read_bio_X509_REQ(in,NULL,NULL);\n\t\tBIO_free(in);\n\t\tif (req == NULL) { perror(infile); goto end; }\n\t\tif (\t(req->req_info == NULL) ||\n\t\t\t(req->req_info->pubkey == NULL) ||\n\t\t\t(req->req_info->pubkey->public_key == NULL) ||\n\t\t\t(req->req_info->pubkey->public_key->data == NULL))\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"The certificate request appears to corrupted\\n");\n\t\t\tBIO_printf(bio_err,"It does not contain a public key\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\tif ((pkey=X509_REQ_get_pubkey(req)) == NULL)\n\t {\n\t BIO_printf(bio_err,"error unpacking public key\\n");\n\t goto end;\n\t }\n\t\ti=X509_REQ_verify(req,pkey);\n\t\tEVP_PKEY_free(pkey);\n\t\tif (i < 0)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"Signature verification error\\n");\n\t\t\tERR_print_errors(bio_err);\n\t\t\tgoto end;\n\t\t\t}\n\t if (i == 0)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"Signature did not match the certificate request\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\telse\n\t\t\tBIO_printf(bio_err,"Signature ok\\n");\n\t\tX509_NAME_oneline(req->req_info->subject,buf,256);\n\t\tBIO_printf(bio_err,"subject=%s\\n",buf);\n\t\tif ((x=X509_new()) == NULL) goto end;\n\t\tci=x->cert_info;\n\t\tif (!ASN1_INTEGER_set(X509_get_serialNumber(x),0)) goto end;\n\t\tif (!X509_set_issuer_name(x,req->req_info->subject)) goto end;\n\t\tif (!X509_set_subject_name(x,req->req_info->subject)) goto end;\n\t\tX509_gmtime_adj(X509_get_notBefore(x),0);\n\t X509_gmtime_adj(X509_get_notAfter(x),(long)60*60*24*days);\n#if 0\n\t\tX509_PUBKEY_free(ci->key);\n\t\tci->key=req->req_info->pubkey;\n\t req->req_info->pubkey=NULL;\n#else\n\t\tpkey = X509_REQ_get_pubkey(req);\n\t\tX509_set_pubkey(x,pkey);\n\t\tEVP_PKEY_free(pkey);\n#endif\n\t\t}\n\telse\n\t\tx=load_cert(infile,informat);\n\tif (x == NULL) goto end;\n\tif (CA_flag)\n\t\t{\n\t\txca=load_cert(CAfile,CAformat);\n\t\tif (xca == NULL) goto end;\n\t\t}\n\tif (!noout || text)\n\t\t{\n\t\tOBJ_create("2.99999.3",\n\t\t\t"SET.ex3","SET x509v3 extension 3");\n\t\tout=BIO_new(BIO_s_file());\n\t\tif (out == NULL)\n\t\t\t{\n\t\t\tERR_print_errors(bio_err);\n\t\t\tgoto end;\n\t\t\t}\n\t\tif (outfile == NULL)\n\t\t\tBIO_set_fp(out,stdout,BIO_NOCLOSE);\n\t\telse\n\t\t\t{\n\t\t\tif (BIO_write_filename(out,outfile) <= 0)\n\t\t\t\t{\n\t\t\t\tperror(outfile);\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\tif (num)\n\t\t{\n\t\tfor (i=1; i<=num; i++)\n\t\t\t{\n\t\t\tif (issuer == i)\n\t\t\t\t{\n\t\t\t\tX509_NAME_oneline(X509_get_issuer_name(x),\n\t\t\t\t\tbuf,256);\n\t\t\t\tBIO_printf(STDout,"issuer= %s\\n",buf);\n\t\t\t\t}\n\t\t\telse if (subject == i)\n\t\t\t\t{\n\t\t\t\tX509_NAME_oneline(X509_get_subject_name(x),\n\t\t\t\t\tbuf,256);\n\t\t\t\tBIO_printf(STDout,"subject=%s\\n",buf);\n\t\t\t\t}\n\t\t\telse if (serial == i)\n\t\t\t\t{\n\t\t\t\tBIO_printf(STDout,"serial=");\n\t\t\t\ti2a_ASN1_INTEGER(STDout,x->cert_info->serialNumber);\n\t\t\t\tBIO_printf(STDout,"\\n");\n\t\t\t\t}\n\t\t\telse if (hash == i)\n\t\t\t\t{\n\t\t\t\tBIO_printf(STDout,"%08lx\\n",X509_subject_name_hash(x));\n\t\t\t\t}\n\t\t\telse\n\t\t\t\tif (modulus == i)\n\t\t\t\t{\n\t\t\t\tEVP_PKEY *pkey;\n\t\t\t\tpkey=X509_get_pubkey(x);\n\t\t\t\tif (pkey == NULL)\n\t\t\t\t\t{\n\t\t\t\t\tBIO_printf(bio_err,"Modulus=unavailable\\n");\n\t\t\t\t\tERR_print_errors(bio_err);\n\t\t\t\t\tgoto end;\n\t\t\t\t\t}\n\t\t\t\tBIO_printf(STDout,"Modulus=");\n#ifndef NO_RSA\n\t\t\t\tif (pkey->type == EVP_PKEY_RSA)\n\t\t\t\t\tBN_print(STDout,pkey->pkey.rsa->n);\n\t\t\t\telse\n#endif\n#ifndef NO_DSA\n\t\t\t\tif (pkey->type == EVP_PKEY_DSA)\n\t\t\t\t\tBN_print(STDout,pkey->pkey.dsa->pub_key);\n\t\t\t\telse\n#endif\n\t\t\t\t\tBIO_printf(STDout,"Wrong Algorithm type");\n\t\t\t\tBIO_printf(STDout,"\\n");\n\t\t\t\tEVP_PKEY_free(pkey);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\tif (C == i)\n\t\t\t\t{\n\t\t\t\tunsigned char *d;\n\t\t\t\tchar *m;\n\t\t\t\tint y,z;\n\t\t\t\tX509_NAME_oneline(X509_get_subject_name(x),\n\t\t\t\t\tbuf,256);\n\t\t\t\tBIO_printf(STDout,"/* subject:%s */\\n",buf);\n\t\t\t\tm=X509_NAME_oneline(\n\t\t\t\t\tX509_get_issuer_name(x),buf,256);\n\t\t\t\tBIO_printf(STDout,"/* issuer :%s */\\n",buf);\n\t\t\t\tz=i2d_X509(x,NULL);\n\t\t\t\tm=Malloc(z);\n\t\t\t\td=(unsigned char *)m;\n\t\t\t\tz=i2d_X509_NAME(X509_get_subject_name(x),&d);\n\t\t\t\tBIO_printf(STDout,"unsigned char XXX_subject_name[%d]={\\n",z);\n\t\t\t\td=(unsigned char *)m;\n\t\t\t\tfor (y=0; y<z; y++)\n\t\t\t\t\t{\n\t\t\t\t\tBIO_printf(STDout,"0x%02X,",d[y]);\n\t\t\t\t\tif ((y & 0x0f) == 0x0f) BIO_printf(STDout,"\\n");\n\t\t\t\t\t}\n\t\t\t\tif (y%16 != 0) BIO_printf(STDout,"\\n");\n\t\t\t\tBIO_printf(STDout,"};\\n");\n\t\t\t\tz=i2d_X509_PUBKEY(X509_get_X509_PUBKEY(x),&d);\n\t\t\t\tBIO_printf(STDout,"unsigned char XXX_public_key[%d]={\\n",z);\n\t\t\t\td=(unsigned char *)m;\n\t\t\t\tfor (y=0; y<z; y++)\n\t\t\t\t\t{\n\t\t\t\t\tBIO_printf(STDout,"0x%02X,",d[y]);\n\t\t\t\t\tif ((y & 0x0f) == 0x0f)\n\t\t\t\t\t\tBIO_printf(STDout,"\\n");\n\t\t\t\t\t}\n\t\t\t\tif (y%16 != 0) BIO_printf(STDout,"\\n");\n\t\t\t\tBIO_printf(STDout,"};\\n");\n\t\t\t\tz=i2d_X509(x,&d);\n\t\t\t\tBIO_printf(STDout,"unsigned char XXX_certificate[%d]={\\n",z);\n\t\t\t\td=(unsigned char *)m;\n\t\t\t\tfor (y=0; y<z; y++)\n\t\t\t\t\t{\n\t\t\t\t\tBIO_printf(STDout,"0x%02X,",d[y]);\n\t\t\t\t\tif ((y & 0x0f) == 0x0f)\n\t\t\t\t\t\tBIO_printf(STDout,"\\n");\n\t\t\t\t\t}\n\t\t\t\tif (y%16 != 0) BIO_printf(STDout,"\\n");\n\t\t\t\tBIO_printf(STDout,"};\\n");\n\t\t\t\tFree(m);\n\t\t\t\t}\n\t\t\telse if (text == i)\n\t\t\t\t{\n\t\t\t\tX509_print(out,x);\n\t\t\t\t}\n\t\t\telse if (startdate == i)\n\t\t\t\t{\n\t\t\t\tBIO_puts(STDout,"notBefore=");\n\t\t\t\tASN1_TIME_print(STDout,X509_get_notBefore(x));\n\t\t\t\tBIO_puts(STDout,"\\n");\n\t\t\t\t}\n\t\t\telse if (enddate == i)\n\t\t\t\t{\n\t\t\t\tBIO_puts(STDout,"notAfter=");\n\t\t\t\tASN1_TIME_print(STDout,X509_get_notAfter(x));\n\t\t\t\tBIO_puts(STDout,"\\n");\n\t\t\t\t}\n\t\t\telse if (fingerprint == i)\n\t\t\t\t{\n\t\t\t\tint j;\n\t\t\t\tunsigned int n;\n\t\t\t\tunsigned char md[EVP_MAX_MD_SIZE];\n\t\t\t\tif (!X509_digest(x,EVP_md5(),md,&n))\n\t\t\t\t\t{\n\t\t\t\t\tBIO_printf(bio_err,"out of memory\\n");\n\t\t\t\t\tgoto end;\n\t\t\t\t\t}\n\t\t\t\tBIO_printf(STDout,"MD5 Fingerprint=");\n\t\t\t\tfor (j=0; j<(int)n; j++)\n\t\t\t\t\t{\n\t\t\t\t\tBIO_printf(STDout,"%02X%c",md[j],\n\t\t\t\t\t\t(j+1 == (int)n)\n\t\t\t\t\t\t?\'\\n\':\':\');\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\telse if ((sign_flag == i) && (x509req == 0))\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err,"Getting Private key\\n");\n\t\t\t\tif (Upkey == NULL)\n\t\t\t\t\t{\n\t\t\t\t\tUpkey=load_key(keyfile,keyformat);\n\t\t\t\t\tif (Upkey == NULL) goto end;\n\t\t\t\t\t}\n#ifndef NO_DSA\n\t\t if (Upkey->type == EVP_PKEY_DSA)\n\t\t digest=EVP_dss1();\n#endif\n\t\t\t\tif (!sign(x,Upkey,days,digest,\n\t\t\t\t\t\t extconf, extsect)) goto end;\n\t\t\t\t}\n\t\t\telse if (CA_flag == i)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err,"Getting CA Private Key\\n");\n\t\t\t\tif (CAkeyfile != NULL)\n\t\t\t\t\t{\n\t\t\t\t\tCApkey=load_key(CAkeyfile,CAkeyformat);\n\t\t\t\t\tif (CApkey == NULL) goto end;\n\t\t\t\t\t}\n#ifndef NO_DSA\n\t\t if (CApkey->type == EVP_PKEY_DSA)\n\t\t digest=EVP_dss1();\n#endif\n\t\t\t\tif (!x509_certify(ctx,CAfile,digest,x,xca,\n\t\t\t\t\tCApkey, CAserial,CA_createserial,days,\n\t\t\t\t\textconf, extsect))\n\t\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\telse if (x509req == i)\n\t\t\t\t{\n\t\t\t\tEVP_PKEY *pk;\n\t\t\t\tBIO_printf(bio_err,"Getting request Private Key\\n");\n\t\t\t\tif (keyfile == NULL)\n\t\t\t\t\t{\n\t\t\t\t\tBIO_printf(bio_err,"no request key file specified\\n");\n\t\t\t\t\tgoto end;\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tpk=load_key(keyfile,FORMAT_PEM);\n\t\t\t\t\tif (pk == NULL) goto end;\n\t\t\t\t\t}\n\t\t\t\tBIO_printf(bio_err,"Generating certificate request\\n");\n\t\t\t\trq=X509_to_X509_REQ(x,pk,EVP_md5());\n\t\t\t\tEVP_PKEY_free(pk);\n\t\t\t\tif (rq == NULL)\n\t\t\t\t\t{\n\t\t\t\t\tERR_print_errors(bio_err);\n\t\t\t\t\tgoto end;\n\t\t\t\t\t}\n\t\t\t\tif (!noout)\n\t\t\t\t\t{\n\t\t\t\t\tX509_REQ_print(out,rq);\n\t\t\t\t\tPEM_write_bio_X509_REQ(out,rq);\n\t\t\t\t\t}\n\t\t\t\tnoout=1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\tif (noout)\n\t\t{\n\t\tret=0;\n\t\tgoto end;\n\t\t}\n\tif \t(outformat == FORMAT_ASN1)\n\t\ti=i2d_X509_bio(out,x);\n\telse if (outformat == FORMAT_PEM)\n\t\ti=PEM_write_bio_X509(out,x);\n\telse if (outformat == FORMAT_NETSCAPE)\n\t\t{\n\t\tASN1_HEADER ah;\n\t\tASN1_OCTET_STRING os;\n\t\tos.data=(unsigned char *)CERT_HDR;\n\t\tos.length=strlen(CERT_HDR);\n\t\tah.header= &os;\n\t\tah.data=(char *)x;\n\t\tah.meth=X509_asn1_meth();\n\t\ti=ASN1_i2d_bio(i2d_ASN1_HEADER,out,(unsigned char *)&ah);\n\t\t}\n\telse\t{\n\t\tBIO_printf(bio_err,"bad output format specified for outfile\\n");\n\t\tgoto end;\n\t\t}\n\tif (!i) {\n\t\tBIO_printf(bio_err,"unable to write certificate\\n");\n\t\tERR_print_errors(bio_err);\n\t\tgoto end;\n\t\t}\n\tret=0;\nend:\n\tOBJ_cleanup();\n\tCONF_free(extconf);\n\tBIO_free(out);\n\tBIO_free(STDout);\n\tX509_STORE_free(ctx);\n\tX509_REQ_free(req);\n\tX509_free(x);\n\tX509_free(xca);\n\tEVP_PKEY_free(Upkey);\n\tEVP_PKEY_free(CApkey);\n\tX509_REQ_free(rq);\n\tX509V3_EXT_cleanup();\n\tEXIT(ret);\n\t}', 'X509_STORE *X509_STORE_new(void)\n\t{\n\tX509_STORE *ret;\n\tif ((ret=(X509_STORE *)Malloc(sizeof(X509_STORE))) == NULL)\n\t\treturn(NULL);\n\tret->certs=lh_new(x509_object_hash,x509_object_cmp);\n\tret->cache=1;\n\tret->get_cert_methods=sk_X509_LOOKUP_new_null();\n\tret->verify=NULL;\n\tret->verify_cb=NULL;\n\tmemset(&ret->ex_data,0,sizeof(CRYPTO_EX_DATA));\n\tret->references=1;\n\tret->depth=0;\n\treturn(ret);\n\t}', 'LHASH *lh_new(unsigned long (*h)(), int (*c)())\n\t{\n\tLHASH *ret;\n\tint i;\n\tif ((ret=(LHASH *)Malloc(sizeof(LHASH))) == NULL)\n\t\tgoto err0;\n\tif ((ret->b=(LHASH_NODE **)Malloc(sizeof(LHASH_NODE *)*MIN_NODES)) == NULL)\n\t\tgoto err1;\n\tfor (i=0; i<MIN_NODES; i++)\n\t\tret->b[i]=NULL;\n\tret->comp=((c == NULL)?(int (*)())strcmp:c);\n\tret->hash=((h == NULL)?(unsigned long (*)())lh_strhash:h);\n\tret->num_nodes=MIN_NODES/2;\n\tret->num_alloc_nodes=MIN_NODES;\n\tret->p=0;\n\tret->pmax=MIN_NODES/2;\n\tret->up_load=UP_LOAD;\n\tret->down_load=DOWN_LOAD;\n\tret->num_items=0;\n\tret->num_expands=0;\n\tret->num_expand_reallocs=0;\n\tret->num_contracts=0;\n\tret->num_contract_reallocs=0;\n\tret->num_hash_calls=0;\n\tret->num_comp_calls=0;\n\tret->num_insert=0;\n\tret->num_replace=0;\n\tret->num_delete=0;\n\tret->num_no_delete=0;\n\tret->num_retrieve=0;\n\tret->num_retrieve_miss=0;\n\tret->num_hash_comps=0;\n\tret->error=0;\n\treturn(ret);\nerr1:\n\tFree((char *)ret);\nerr0:\n\treturn(NULL);\n\t}', 'static int x509_certify(X509_STORE *ctx, char *CAfile, const EVP_MD *digest,\n\t X509 *x, X509 *xca, EVP_PKEY *pkey, char *serialfile, int create,\n\t int days, LHASH *conf, char *section)\n\t{\n\tint ret=0;\n\tBIO *io=NULL;\n\tMS_STATIC char buf2[1024];\n\tchar *buf=NULL,*p;\n\tBIGNUM *serial=NULL;\n\tASN1_INTEGER *bs=NULL,bs2;\n\tX509_STORE_CTX xsc;\n\tEVP_PKEY *upkey;\n\tupkey = X509_get_pubkey(xca);\n\tEVP_PKEY_copy_parameters(upkey,pkey);\n\tEVP_PKEY_free(upkey);\n\tX509_STORE_CTX_init(&xsc,ctx,x,NULL);\n\tbuf=(char *)Malloc(EVP_PKEY_size(pkey)*2+\n\t\t((serialfile == NULL)\n\t\t\t?(strlen(CAfile)+strlen(POSTFIX)+1)\n\t\t\t:(strlen(serialfile)))+1);\n\tif (buf == NULL) { BIO_printf(bio_err,"out of mem\\n"); goto end; }\n\tif (serialfile == NULL)\n\t\t{\n\t\tstrcpy(buf,CAfile);\n\t\tfor (p=buf; *p; p++)\n\t\t\tif (*p == \'.\')\n\t\t\t\t{\n\t\t\t\t*p=\'\\0\';\n\t\t\t\tbreak;\n\t\t\t\t}\n\t\tstrcat(buf,POSTFIX);\n\t\t}\n\telse\n\t\tstrcpy(buf,serialfile);\n\tserial=BN_new();\n\tbs=ASN1_INTEGER_new();\n\tif ((serial == NULL) || (bs == NULL))\n\t\t{\n\t\tERR_print_errors(bio_err);\n\t\tgoto end;\n\t\t}\n\tio=BIO_new(BIO_s_file());\n\tif (io == NULL)\n\t\t{\n\t\tERR_print_errors(bio_err);\n\t\tgoto end;\n\t\t}\n\tif (BIO_read_filename(io,buf) <= 0)\n\t\t{\n\t\tif (!create)\n\t\t\t{\n\t\t\tperror(buf);\n\t\t\tgoto end;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tASN1_INTEGER_set(bs,0);\n\t\t\tBN_zero(serial);\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tif (!a2i_ASN1_INTEGER(io,bs,buf2,1024))\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"unable to load serial number from %s\\n",buf);\n\t\t\tERR_print_errors(bio_err);\n\t\t\tgoto end;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tserial=BN_bin2bn(bs->data,bs->length,serial);\n\t\t\tif (serial == NULL)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err,"error converting bin 2 bn");\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\tif (!BN_add_word(serial,1))\n\t\t{ BIO_printf(bio_err,"add_word failure\\n"); goto end; }\n\tbs2.data=(unsigned char *)buf2;\n\tbs2.length=BN_bn2bin(serial,bs2.data);\n\tif (BIO_write_filename(io,buf) <= 0)\n\t\t{\n\t\tBIO_printf(bio_err,"error attempting to write serial number file\\n");\n\t\tperror(buf);\n\t\tgoto end;\n\t\t}\n\ti2a_ASN1_INTEGER(io,&bs2);\n\tBIO_puts(io,"\\n");\n\tBIO_free(io);\n\tio=NULL;\n\tif (!X509_STORE_add_cert(ctx,x)) goto end;\n\tX509_STORE_CTX_set_cert(&xsc,x);\n\tif (!reqfile && !X509_verify_cert(&xsc))\n\t\tgoto end;\n\tif (!X509_check_private_key(xca,pkey))\n\t\t{\n\t\tBIO_printf(bio_err,"CA certificate and CA private key do not match\\n");\n\t\tgoto end;\n\t\t}\n\tif (!X509_set_issuer_name(x,X509_get_subject_name(xca))) goto end;\n\tif (!X509_set_serialNumber(x,bs)) goto end;\n\tif (X509_gmtime_adj(X509_get_notBefore(x),0L) == NULL)\n\t\tgoto end;\n\tif (X509_gmtime_adj(X509_get_notAfter(x),(long)60*60*24*days) == NULL)\n\t\tgoto end;\n\tupkey=X509_get_pubkey(x);\n\tif (!EVP_PKEY_missing_parameters(pkey) &&\n\t\t(EVP_PKEY_cmp_parameters(pkey,upkey) == 0))\n\t\t{\n\t\tEVP_PKEY_save_parameters(upkey,0);\n\t\tX509_set_pubkey(x,upkey);\n\t\t}\n\tEVP_PKEY_free(upkey);\n\tif(conf) {\n\t\tX509V3_CTX ctx2;\n\t\tX509_set_version(x,2);\n X509V3_set_ctx(&ctx2, xca, x, NULL, NULL, 0);\n X509V3_set_conf_lhash(&ctx2, conf);\n if(!X509V3_EXT_add_conf(conf, &ctx2, section, x)) goto end;\n\t}\n\tif (!X509_sign(x,pkey,digest)) goto end;\n\tret=1;\nend:\n\tX509_STORE_CTX_cleanup(&xsc);\n\tif (!ret)\n\t\tERR_print_errors(bio_err);\n\tif (buf != NULL) Free(buf);\n\tif (bs != NULL) ASN1_INTEGER_free(bs);\n\tif (io != NULL)\tBIO_free(io);\n\tif (serial != NULL) BN_free(serial);\n\treturn(ret);\n\t}', 'int X509_STORE_add_cert(X509_STORE *ctx, X509 *x)\n\t{\n\tX509_OBJECT *obj,*r;\n\tint ret=1;\n\tif (x == NULL) return(0);\n\tobj=(X509_OBJECT *)Malloc(sizeof(X509_OBJECT));\n\tif (obj == NULL)\n\t\t{\n\t\tX509err(X509_F_X509_STORE_ADD_CERT,ERR_R_MALLOC_FAILURE);\n\t\treturn(0);\n\t\t}\n\tobj->type=X509_LU_X509;\n\tobj->data.x509=x;\n\tCRYPTO_w_lock(CRYPTO_LOCK_X509_STORE);\n\tX509_OBJECT_up_ref_count(obj);\n\tr=(X509_OBJECT *)lh_insert(ctx->certs,(char *)obj);\n\tif (r != NULL)\n\t\t{\n\t\tlh_delete(ctx->certs,(char *)obj);\n\t\tX509_OBJECT_free_contents(obj);\n\t\tFree(obj);\n\t\tlh_insert(ctx->certs,(char *)r);\n\t\tX509err(X509_F_X509_STORE_ADD_CERT,X509_R_CERT_ALREADY_IN_HASH_TABLE);\n\t\tret=0;\n\t\t}\n\tCRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);\n\treturn(ret);\n\t}', 'char *lh_insert(LHASH *lh, char *data)\n\t{\n\tunsigned long hash;\n\tLHASH_NODE *nn,**rn;\n\tchar *ret;\n\tlh->error=0;\n\tif (lh->up_load <= (lh->num_items*LH_LOAD_MULT/lh->num_nodes))\n\t\texpand(lh);\n\trn=getrn(lh,data,&hash);\n\tif (*rn == NULL)\n\t\t{\n\t\tif ((nn=(LHASH_NODE *)Malloc(sizeof(LHASH_NODE))) == NULL)\n\t\t\t{\n\t\t\tlh->error++;\n\t\t\treturn(NULL);\n\t\t\t}\n\t\tnn->data=data;\n\t\tnn->next=NULL;\n#ifndef NO_HASH_COMP\n\t\tnn->hash=hash;\n#endif\n\t\t*rn=nn;\n\t\tret=NULL;\n\t\tlh->num_insert++;\n\t\tlh->num_items++;\n\t\t}\n\telse\n\t\t{\n\t\tret= (*rn)->data;\n\t\t(*rn)->data=data;\n\t\tlh->num_replace++;\n\t\t}\n\treturn(ret);\n\t}', 'char *lh_delete(LHASH *lh, char *data)\n\t{\n\tunsigned long hash;\n\tLHASH_NODE *nn,**rn;\n\tchar *ret;\n\tlh->error=0;\n\trn=getrn(lh,data,&hash);\n\tif (*rn == NULL)\n\t\t{\n\t\tlh->num_no_delete++;\n\t\treturn(NULL);\n\t\t}\n\telse\n\t\t{\n\t\tnn= *rn;\n\t\t*rn=nn->next;\n\t\tret=nn->data;\n\t\tFree((char *)nn);\n\t\tlh->num_delete++;\n\t\t}\n\tlh->num_items--;\n\tif ((lh->num_nodes > MIN_NODES) &&\n\t\t(lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes)))\n\t\tcontract(lh);\n\treturn(ret);\n\t}']
2,387
0
https://github.com/openssl/openssl/blob/c504a5e78386aa9f02462d18a90da759f9131321/crypto/bn/bn_ctx.c/#L353
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['int test_mod_exp(BIO *bp, BN_CTX *ctx)\n\t{\n\tBIGNUM *a,*b,*c,*d,*e;\n\tint i;\n\ta=BN_new();\n\tb=BN_new();\n\tc=BN_new();\n\td=BN_new();\n\te=BN_new();\n\tBN_bntest_rand(c,30,0,1);\n\tfor (i=0; i<num2; i++)\n\t\t{\n\t\tBN_bntest_rand(a,20+i*5,0,0);\n\t\tBN_bntest_rand(b,2+i,0,0);\n\t\tif (!BN_mod_exp(d,a,b,c,ctx))\n\t\t\treturn(00);\n\t\tif (bp != NULL)\n\t\t\t{\n\t\t\tif (!results)\n\t\t\t\t{\n\t\t\t\tBN_print(bp,a);\n\t\t\t\tBIO_puts(bp," ^ ");\n\t\t\t\tBN_print(bp,b);\n\t\t\t\tBIO_puts(bp," % ");\n\t\t\t\tBN_print(bp,c);\n\t\t\t\tBIO_puts(bp," - ");\n\t\t\t\t}\n\t\t\tBN_print(bp,d);\n\t\t\tBIO_puts(bp,"\\n");\n\t\t\t}\n\t\tBN_exp(e,a,b,ctx);\n\t\tBN_sub(e,e,d);\n\t\tBN_div(a,b,e,c,ctx);\n\t\tif(!BN_is_zero(b))\n\t\t {\n\t\t fprintf(stderr,"Modulo exponentiation test failed!\\n");\n\t\t return 0;\n\t\t }\n\t\t}\n\tBN_free(a);\n\tBN_free(b);\n\tBN_free(c);\n\tBN_free(d);\n\tBN_free(e);\n\treturn(1);\n\t}', 'int BN_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\tif ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0))\n\t\t{\n\t\treturn BN_div_no_branch(dv, rm, num, divisor, ctx);\n\t\t}\n\tbn_check_top(dv);\n\tbn_check_top(rm);\n\tbn_check_top(num);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\n\t\t}\n\tif (BN_ucmp(num,divisor) < 0)\n\t\t{\n\t\tif (rm != NULL)\n\t\t\t{ if (BN_copy(rm,num) == NULL) return(0); }\n\t\tif (dv != NULL) BN_zero(dv);\n\t\treturn(1);\n\t\t}\n\tBN_CTX_start(ctx);\n\ttmp=BN_CTX_get(ctx);\n\tsnum=BN_CTX_get(ctx);\n\tsdiv=BN_CTX_get(ctx);\n\tif (dv == NULL)\n\t\tres=BN_CTX_get(ctx);\n\telse\tres=dv;\n\tif (sdiv == NULL || res == NULL) goto err;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tif (!(BN_lshift(sdiv,divisor,norm_shift))) goto err;\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tif (!(BN_lshift(snum,num,norm_shift))) goto err;\n\tsnum->neg=0;\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\twnum.neg = 0;\n\twnum.d = &(snum->d[loop]);\n\twnum.top = div_n;\n\twnum.dmax = snum->dmax - loop;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (BN_ucmp(&wnum,sdiv) >= 0)\n\t\t{\n\t\tbn_clear_top2max(&wnum);\n\t\tbn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n\t\t*resp=1;\n\t\t}\n\telse\n\t\tres->top--;\n\tif (res->top == 0)\n\t\tres->neg = 0;\n\telse\n\t\tresp--;\n\tfor (i=0; i<loop-1; i++, wnump--, resp--)\n\t\t{\n\t\tBN_ULONG q,l0;\n#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n\t\tBN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG);\n\t\tq=bn_div_3_words(wnump,d1,d0);\n#else\n\t\tBN_ULONG n0,n1,rem=0;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\t{\n#ifdef BN_LLONG\n\t\t\tBN_ULLONG t2;\n#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n\t\t\tq=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);\n#else\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tt2 -= d1;\n\t\t\t\t}\n#else\n\t\t\tBN_ULONG t2l,t2h,ql,qh;\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#if defined(BN_UMULT_LOHI)\n\t\t\tBN_UMULT_LOHI(t2l,t2h,d1,q);\n#elif defined(BN_UMULT_HIGH)\n\t\t\tt2l = d1 * q;\n\t\t\tt2h = BN_UMULT_HIGH(d1,q);\n#else\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n#endif\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif ((t2h < rem) ||\n\t\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t\t}\n#endif\n\t\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\twnum.d--;\n\t\tif (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1))\n\t\t\t{\n\t\t\tq--;\n\t\t\tif (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n\t\t\t\t(*wnump)++;\n\t\t\t}\n\t\t*resp = q;\n\t\t}\n\tbn_correct_top(snum);\n\tif (rm != NULL)\n\t\t{\n\t\tint neg = num->neg;\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\tif (!BN_is_zero(rm))\n\t\t\trm->neg = neg;\n\t\tbn_check_top(rm);\n\t\t}\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'int BN_div_no_branch(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,\n\tconst BIGNUM *divisor, BN_CTX *ctx)\n\t{\n\tint norm_shift,i,loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tint num_n,div_n;\n\tbn_check_top(dv);\n\tbn_check_top(rm);\n\tbn_check_top(num);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV_NO_BRANCH,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\n\t\t}\n\tBN_CTX_start(ctx);\n\ttmp=BN_CTX_get(ctx);\n\tsnum=BN_CTX_get(ctx);\n\tsdiv=BN_CTX_get(ctx);\n\tif (dv == NULL)\n\t\tres=BN_CTX_get(ctx);\n\telse\tres=dv;\n\tif (sdiv == NULL || res == NULL) goto err;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tif (!(BN_lshift(sdiv,divisor,norm_shift))) goto err;\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tif (!(BN_lshift(snum,num,norm_shift))) goto err;\n\tsnum->neg=0;\n\tif (snum->top <= sdiv->top+1)\n\t\t{\n\t\tif (bn_wexpand(snum, sdiv->top + 2) == NULL) goto err;\n\t\tfor (i = snum->top; i < sdiv->top + 2; i++) snum->d[i] = 0;\n\t\tsnum->top = sdiv->top + 2;\n\t\t}\n\telse\n\t\t{\n\t\tif (bn_wexpand(snum, snum->top + 1) == NULL) goto err;\n\t\tsnum->d[snum->top] = 0;\n\t\tsnum->top ++;\n\t\t}\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\twnum.neg = 0;\n\twnum.d = &(snum->d[loop]);\n\twnum.top = div_n;\n\twnum.dmax = snum->dmax - loop;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop-1;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (res->top == 0)\n\t\tres->neg = 0;\n\telse\n\t\tresp--;\n\tfor (i=0; i<loop-1; i++, wnump--, resp--)\n\t\t{\n\t\tBN_ULONG q,l0;\n#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n\t\tBN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG);\n\t\tq=bn_div_3_words(wnump,d1,d0);\n#else\n\t\tBN_ULONG n0,n1,rem=0;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\t{\n#ifdef BN_LLONG\n\t\t\tBN_ULLONG t2;\n#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n\t\t\tq=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);\n#else\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tt2 -= d1;\n\t\t\t\t}\n#else\n\t\t\tBN_ULONG t2l,t2h,ql,qh;\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#if defined(BN_UMULT_LOHI)\n\t\t\tBN_UMULT_LOHI(t2l,t2h,d1,q);\n#elif defined(BN_UMULT_HIGH)\n\t\t\tt2l = d1 * q;\n\t\t\tt2h = BN_UMULT_HIGH(d1,q);\n#else\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n#endif\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif ((t2h < rem) ||\n\t\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t\t}\n#endif\n\t\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\twnum.d--;\n\t\tif (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1))\n\t\t\t{\n\t\t\tq--;\n\t\t\tif (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n\t\t\t\t(*wnump)++;\n\t\t\t}\n\t\t*resp = q;\n\t\t}\n\tbn_correct_top(snum);\n\tif (rm != NULL)\n\t\t{\n\t\tint neg = num->neg;\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\tif (!BN_is_zero(rm))\n\t\t\trm->neg = neg;\n\t\tbn_check_top(rm);\n\t\t}\n\tbn_correct_top(res);\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'void BN_CTX_end(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_end", ctx);\n\tif(ctx->err_stack)\n\t\tctx->err_stack--;\n\telse\n\t\t{\n\t\tunsigned int fp = BN_STACK_pop(&ctx->stack);\n\t\tif(fp < ctx->used)\n\t\t\tBN_POOL_release(&ctx->pool, ctx->used - fp);\n\t\tctx->used = fp;\n\t\tctx->too_many = 0;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n\t{\n\treturn st->indexes[--(st->depth)];\n\t}']
2,388
0
https://github.com/openssl/openssl/blob/065866b28d95b670b63a0ccf47cd0db2064a1b3d/crypto/x509/x509_vfy.c/#L805
int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose, int purpose, int trust) { int idx; if(!purpose) purpose = def_purpose; if(purpose) { X509_PURPOSE *ptmp; idx = X509_PURPOSE_get_by_id(purpose); if(idx == -1) { X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT, X509_R_UNKNOWN_PURPOSE_ID); return 0; } ptmp = X509_PURPOSE_get0(idx); if(ptmp->trust == X509_TRUST_DEFAULT) { idx = X509_PURPOSE_get_by_id(def_purpose); if(idx == -1) { X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT, X509_R_UNKNOWN_PURPOSE_ID); return 0; } ptmp = X509_PURPOSE_get0(idx); } if(!trust) trust = ptmp->trust; } if(trust) { idx = X509_TRUST_get_by_id(trust); if(idx == -1) { X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT, X509_R_UNKNOWN_TRUST_ID); return 0; } } if(purpose) ctx->purpose = purpose; if(trust) ctx->trust = trust; return 1; }
['int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose,\n\t\t\t\tint purpose, int trust)\n{\n\tint idx;\n\tif(!purpose) purpose = def_purpose;\n\tif(purpose) {\n\t\tX509_PURPOSE *ptmp;\n\t\tidx = X509_PURPOSE_get_by_id(purpose);\n\t\tif(idx == -1) {\n\t\t\tX509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,\n\t\t\t\t\t\tX509_R_UNKNOWN_PURPOSE_ID);\n\t\t\treturn 0;\n\t\t}\n\t\tptmp = X509_PURPOSE_get0(idx);\n\t\tif(ptmp->trust == X509_TRUST_DEFAULT) {\n\t\t\tidx = X509_PURPOSE_get_by_id(def_purpose);\n\t\t\tif(idx == -1) {\n\t\t\t\tX509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,\n\t\t\t\t\t\tX509_R_UNKNOWN_PURPOSE_ID);\n\t\t\t\treturn 0;\n\t\t\t}\n\t\t\tptmp = X509_PURPOSE_get0(idx);\n\t\t}\n\t\tif(!trust) trust = ptmp->trust;\n\t}\n\tif(trust) {\n\t\tidx = X509_TRUST_get_by_id(trust);\n\t\tif(idx == -1) {\n\t\t\tX509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,\n\t\t\t\t\t\tX509_R_UNKNOWN_TRUST_ID);\n\t\t\treturn 0;\n\t\t}\n\t}\n\tif(purpose) ctx->purpose = purpose;\n\tif(trust) ctx->trust = trust;\n\treturn 1;\n}', 'int X509_PURPOSE_get_by_id(int purpose)\n{\n\tX509_PURPOSE tmp;\n\tint idx;\n\tif((purpose >= X509_PURPOSE_MIN) && (purpose <= X509_PURPOSE_MAX))\n\t\treturn purpose - X509_PURPOSE_MIN;\n\ttmp.purpose = purpose;\n\tif(!xptable) return -1;\n\tidx = sk_X509_PURPOSE_find(xptable, &tmp);\n\tif(idx == -1) return -1;\n\treturn idx + X509_PURPOSE_COUNT;\n}', 'X509_PURPOSE * X509_PURPOSE_get0(int idx)\n{\n\tif(idx < 0) return NULL;\n\tif(idx < X509_PURPOSE_COUNT) return xstandard + idx;\n\treturn sk_X509_PURPOSE_value(xptable, idx - X509_PURPOSE_COUNT);\n}']
2,389
0
https://github.com/openssl/openssl/blob/6fc1748ec65c94c195d02b59556434e36a5f7651/ssl/s3_enc.c/#L422
int ssl3_final_finish_mac(SSL *s, const char *sender, int len, unsigned char *p) { int ret; EVP_MD_CTX *ctx = NULL; if (!ssl3_digest_cached_records(s, 0)) return 0; if (EVP_MD_CTX_type(s->s3->handshake_dgst) != NID_md5_sha1) { SSLerr(SSL_F_SSL3_FINAL_FINISH_MAC, SSL_R_NO_REQUIRED_DIGEST); return 0; } ctx = EVP_MD_CTX_new(); if (ctx == NULL) { SSLerr(SSL_F_SSL3_FINAL_FINISH_MAC, ERR_R_MALLOC_FAILURE); return 0; } if (!EVP_MD_CTX_copy_ex(ctx, s->s3->handshake_dgst)) { SSLerr(SSL_F_SSL3_FINAL_FINISH_MAC, ERR_R_INTERNAL_ERROR); return 0; } ret = EVP_MD_CTX_size(ctx); if (ret < 0) { EVP_MD_CTX_reset(ctx); return 0; } if ((sender != NULL && EVP_DigestUpdate(ctx, sender, len) <= 0) || EVP_MD_CTX_ctrl(ctx, EVP_CTRL_SSL3_MASTER_SECRET, s->session->master_key_length, s->session->master_key) <= 0 || EVP_DigestFinal_ex(ctx, p, NULL) <= 0) { SSLerr(SSL_F_SSL3_FINAL_FINISH_MAC, ERR_R_INTERNAL_ERROR); ret = 0; } EVP_MD_CTX_free(ctx); return ret; }
['int ssl3_final_finish_mac(SSL *s, const char *sender, int len, unsigned char *p)\n{\n int ret;\n EVP_MD_CTX *ctx = NULL;\n if (!ssl3_digest_cached_records(s, 0))\n return 0;\n if (EVP_MD_CTX_type(s->s3->handshake_dgst) != NID_md5_sha1) {\n SSLerr(SSL_F_SSL3_FINAL_FINISH_MAC, SSL_R_NO_REQUIRED_DIGEST);\n return 0;\n }\n ctx = EVP_MD_CTX_new();\n if (ctx == NULL) {\n SSLerr(SSL_F_SSL3_FINAL_FINISH_MAC, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n if (!EVP_MD_CTX_copy_ex(ctx, s->s3->handshake_dgst)) {\n SSLerr(SSL_F_SSL3_FINAL_FINISH_MAC, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n ret = EVP_MD_CTX_size(ctx);\n if (ret < 0) {\n EVP_MD_CTX_reset(ctx);\n return 0;\n }\n if ((sender != NULL && EVP_DigestUpdate(ctx, sender, len) <= 0)\n || EVP_MD_CTX_ctrl(ctx, EVP_CTRL_SSL3_MASTER_SECRET,\n s->session->master_key_length,\n s->session->master_key) <= 0\n || EVP_DigestFinal_ex(ctx, p, NULL) <= 0) {\n SSLerr(SSL_F_SSL3_FINAL_FINISH_MAC, ERR_R_INTERNAL_ERROR);\n ret = 0;\n }\n EVP_MD_CTX_free(ctx);\n return ret;\n}', 'int ssl3_digest_cached_records(SSL *s, int keep)\n{\n const EVP_MD *md;\n long hdatalen;\n void *hdata;\n if (s->s3->handshake_dgst == NULL) {\n hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);\n if (hdatalen <= 0) {\n SSLerr(SSL_F_SSL3_DIGEST_CACHED_RECORDS,\n SSL_R_BAD_HANDSHAKE_LENGTH);\n return 0;\n }\n s->s3->handshake_dgst = EVP_MD_CTX_new();\n if (s->s3->handshake_dgst == NULL) {\n SSLerr(SSL_F_SSL3_DIGEST_CACHED_RECORDS, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n md = ssl_handshake_md(s);\n if (md == NULL || !EVP_DigestInit_ex(s->s3->handshake_dgst, md, NULL)\n || !EVP_DigestUpdate(s->s3->handshake_dgst, hdata, hdatalen)) {\n SSLerr(SSL_F_SSL3_DIGEST_CACHED_RECORDS, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n }\n if (keep == 0) {\n BIO_free(s->s3->handshake_buffer);\n s->s3->handshake_buffer = NULL;\n }\n return 1;\n}', 'const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx)\n{\n if (!ctx)\n return NULL;\n return ctx->digest;\n}', 'int EVP_MD_type(const EVP_MD *md)\n{\n return md->type;\n}', 'EVP_MD_CTX *EVP_MD_CTX_new(void)\n{\n return OPENSSL_zalloc(sizeof(EVP_MD_CTX));\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num <= 0)\n return NULL;\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n osslargused(file); osslargused(line);\n ret = malloc(num);\n#endif\n return ret;\n}', 'int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in)\n{\n unsigned char *tmp_buf;\n if ((in == NULL) || (in->digest == NULL)) {\n EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, EVP_R_INPUT_NOT_INITIALIZED);\n return 0;\n }\n#ifndef OPENSSL_NO_ENGINE\n if (in->engine && !ENGINE_init(in->engine)) {\n EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_ENGINE_LIB);\n return 0;\n }\n#endif\n if (out->digest == in->digest) {\n tmp_buf = out->md_data;\n EVP_MD_CTX_set_flags(out, EVP_MD_CTX_FLAG_REUSE);\n } else\n tmp_buf = NULL;\n EVP_MD_CTX_reset(out);\n memcpy(out, in, sizeof(*out));\n out->md_data = NULL;\n out->pctx = NULL;\n if (in->md_data && out->digest->ctx_size) {\n if (tmp_buf)\n out->md_data = tmp_buf;\n else {\n out->md_data = OPENSSL_malloc(out->digest->ctx_size);\n if (out->md_data == NULL) {\n EVPerr(EVP_F_EVP_MD_CTX_COPY_EX, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n }\n memcpy(out->md_data, in->md_data, out->digest->ctx_size);\n }\n out->update = in->update;\n if (in->pctx) {\n out->pctx = EVP_PKEY_CTX_dup(in->pctx);\n if (!out->pctx) {\n EVP_MD_CTX_reset(out);\n return 0;\n }\n }\n if (out->digest->copy)\n return out->digest->copy(out, in);\n return 1;\n}', 'int ENGINE_init(ENGINE *e)\n{\n int ret;\n if (e == NULL) {\n ENGINEerr(ENGINE_F_ENGINE_INIT, ERR_R_PASSED_NULL_PARAMETER);\n return 0;\n }\n if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {\n ENGINEerr(ENGINE_F_ENGINE_INIT, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n CRYPTO_THREAD_write_lock(global_engine_lock);\n ret = engine_unlocked_init(e);\n CRYPTO_THREAD_unlock(global_engine_lock);\n return ret;\n}', 'int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))\n{\n if (pthread_once(once, init) != 0)\n return 0;\n return 1;\n}']
2,390
0
https://github.com/libav/libav/blob/dad7a9c7c0ae8ebc56f2e3a24e6fa4da5c2cd491/libavcodec/bitstream.h/#L139
static inline uint64_t get_val(BitstreamContext *bc, unsigned n) { #ifdef BITSTREAM_READER_LE uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1); bc->bits >>= n; #else uint64_t ret = bc->bits >> (64 - n); bc->bits <<= n; #endif bc->bits_left -= n; return ret; }
['static int decode_subframe(WMAProDecodeCtx *s)\n{\n int offset = s->samples_per_frame;\n int subframe_len = s->samples_per_frame;\n int i;\n int total_samples = s->samples_per_frame * s->avctx->channels;\n int transmit_coeffs = 0;\n int cur_subwoofer_cutoff;\n s->subframe_offset = bitstream_tell(&s->bc);\n for (i = 0; i < s->avctx->channels; i++) {\n s->channel[i].grouped = 0;\n if (offset > s->channel[i].decoded_samples) {\n offset = s->channel[i].decoded_samples;\n subframe_len =\n s->channel[i].subframe_len[s->channel[i].cur_subframe];\n }\n }\n ff_dlog(s->avctx,\n "processing subframe with offset %i len %i\\n", offset, subframe_len);\n s->channels_for_cur_subframe = 0;\n for (i = 0; i < s->avctx->channels; i++) {\n const int cur_subframe = s->channel[i].cur_subframe;\n total_samples -= s->channel[i].decoded_samples;\n if (offset == s->channel[i].decoded_samples &&\n subframe_len == s->channel[i].subframe_len[cur_subframe]) {\n total_samples -= s->channel[i].subframe_len[cur_subframe];\n s->channel[i].decoded_samples +=\n s->channel[i].subframe_len[cur_subframe];\n s->channel_indexes_for_cur_subframe[s->channels_for_cur_subframe] = i;\n ++s->channels_for_cur_subframe;\n }\n }\n if (!total_samples)\n s->parsed_all_subframes = 1;\n ff_dlog(s->avctx, "subframe is part of %i channels\\n",\n s->channels_for_cur_subframe);\n s->table_idx = av_log2(s->samples_per_frame/subframe_len);\n s->num_bands = s->num_sfb[s->table_idx];\n s->cur_sfb_offsets = s->sfb_offsets[s->table_idx];\n cur_subwoofer_cutoff = s->subwoofer_cutoffs[s->table_idx];\n offset += s->samples_per_frame >> 1;\n for (i = 0; i < s->channels_for_cur_subframe; i++) {\n int c = s->channel_indexes_for_cur_subframe[i];\n s->channel[c].coeffs = &s->channel[c].out[offset];\n }\n s->subframe_len = subframe_len;\n s->esc_len = av_log2(s->subframe_len - 1) + 1;\n if (bitstream_read_bit(&s->bc)) {\n int num_fill_bits;\n if (!(num_fill_bits = bitstream_read(&s->bc, 2))) {\n int len = bitstream_read(&s->bc, 4);\n num_fill_bits = bitstream_read(&s->bc, len) + 1;\n }\n if (num_fill_bits >= 0) {\n if (bitstream_tell(&s->bc) + num_fill_bits > s->num_saved_bits) {\n av_log(s->avctx, AV_LOG_ERROR, "invalid number of fill bits\\n");\n return AVERROR_INVALIDDATA;\n }\n bitstream_skip(&s->bc, num_fill_bits);\n }\n }\n if (bitstream_read_bit(&s->bc)) {\n avpriv_request_sample(s->avctx, "Reserved bit");\n return AVERROR_PATCHWELCOME;\n }\n if (decode_channel_transform(s) < 0)\n return AVERROR_INVALIDDATA;\n for (i = 0; i < s->channels_for_cur_subframe; i++) {\n int c = s->channel_indexes_for_cur_subframe[i];\n if ((s->channel[c].transmit_coefs = bitstream_read_bit(&s->bc)))\n transmit_coeffs = 1;\n }\n if (transmit_coeffs) {\n int step;\n int quant_step = 90 * s->bits_per_sample >> 4;\n if ((s->transmit_num_vec_coeffs = bitstream_read_bit(&s->bc))) {\n int num_bits = av_log2((s->subframe_len + 3)/4) + 1;\n for (i = 0; i < s->channels_for_cur_subframe; i++) {\n int c = s->channel_indexes_for_cur_subframe[i];\n int num_vec_coeffs = bitstream_read(&s->bc, num_bits) << 2;\n if (num_vec_coeffs + offset > FF_ARRAY_ELEMS(s->channel[c].out)) {\n av_log(s->avctx, AV_LOG_ERROR, "num_vec_coeffs %d is too large\\n", num_vec_coeffs);\n return AVERROR_INVALIDDATA;\n }\n s->channel[c].num_vec_coeffs = num_vec_coeffs;\n }\n } else {\n for (i = 0; i < s->channels_for_cur_subframe; i++) {\n int c = s->channel_indexes_for_cur_subframe[i];\n s->channel[c].num_vec_coeffs = s->subframe_len;\n }\n }\n step = bitstream_read_signed(&s->bc, 6);\n quant_step += step;\n if (step == -32 || step == 31) {\n const int sign = (step == 31) - 1;\n int quant = 0;\n while (bitstream_tell(&s->bc) + 5 < s->num_saved_bits &&\n (step = bitstream_read(&s->bc, 5)) == 31) {\n quant += 31;\n }\n quant_step += ((quant + step) ^ sign) - sign;\n }\n if (quant_step < 0) {\n av_log(s->avctx, AV_LOG_DEBUG, "negative quant step\\n");\n }\n if (s->channels_for_cur_subframe == 1) {\n s->channel[s->channel_indexes_for_cur_subframe[0]].quant_step = quant_step;\n } else {\n int modifier_len = bitstream_read(&s->bc, 3);\n for (i = 0; i < s->channels_for_cur_subframe; i++) {\n int c = s->channel_indexes_for_cur_subframe[i];\n s->channel[c].quant_step = quant_step;\n if (bitstream_read_bit(&s->bc)) {\n if (modifier_len) {\n s->channel[c].quant_step += bitstream_read(&s->bc, modifier_len) + 1;\n } else\n ++s->channel[c].quant_step;\n }\n }\n }\n if (decode_scale_factors(s) < 0)\n return AVERROR_INVALIDDATA;\n }\n ff_dlog(s->avctx, "BITSTREAM: subframe header length was %i\\n",\n bitstream_tell(&s->bc) - s->subframe_offset);\n for (i = 0; i < s->channels_for_cur_subframe; i++) {\n int c = s->channel_indexes_for_cur_subframe[i];\n if (s->channel[c].transmit_coefs &&\n bitstream_tell(&s->bc) < s->num_saved_bits) {\n decode_coeffs(s, c);\n } else\n memset(s->channel[c].coeffs, 0,\n sizeof(*s->channel[c].coeffs) * subframe_len);\n }\n ff_dlog(s->avctx, "BITSTREAM: subframe length was %i\\n",\n bitstream_tell(&s->bc) - s->subframe_offset);\n if (transmit_coeffs) {\n FFTContext *mdct = &s->mdct_ctx[av_log2(subframe_len) - WMAPRO_BLOCK_MIN_BITS];\n inverse_channel_transform(s);\n for (i = 0; i < s->channels_for_cur_subframe; i++) {\n int c = s->channel_indexes_for_cur_subframe[i];\n const int* sf = s->channel[c].scale_factors;\n int b;\n if (c == s->lfe_channel)\n memset(&s->tmp[cur_subwoofer_cutoff], 0, sizeof(*s->tmp) *\n (subframe_len - cur_subwoofer_cutoff));\n for (b = 0; b < s->num_bands; b++) {\n const int end = FFMIN(s->cur_sfb_offsets[b+1], s->subframe_len);\n const int exp = s->channel[c].quant_step -\n (s->channel[c].max_scale_factor - *sf++) *\n s->channel[c].scale_factor_step;\n const float quant = pow(10.0, exp / 20.0);\n int start = s->cur_sfb_offsets[b];\n s->fdsp.vector_fmul_scalar(s->tmp + start,\n s->channel[c].coeffs + start,\n quant, end - start);\n }\n mdct->imdct_half(mdct, s->channel[c].coeffs, s->tmp);\n }\n }\n wmapro_window(s);\n for (i = 0; i < s->channels_for_cur_subframe; i++) {\n int c = s->channel_indexes_for_cur_subframe[i];\n if (s->channel[c].cur_subframe >= s->channel[c].num_subframes) {\n av_log(s->avctx, AV_LOG_ERROR, "broken subframe\\n");\n return AVERROR_INVALIDDATA;\n }\n ++s->channel[c].cur_subframe;\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}']
2,391
0
https://github.com/openssl/openssl/blob/9b02dc97e4963969da69675a871dbe80e6d31cda/crypto/bn/bn_word.c/#L159
int BN_sub_word(BIGNUM *a, BN_ULONG w) { int i; bn_check_top(a); w &= BN_MASK2; if (!w) return 1; if (BN_is_zero(a)) { i = BN_set_word(a, w); if (i != 0) BN_set_negative(a, 1); return i; } if (a->neg) { a->neg = 0; i = BN_add_word(a, w); a->neg = 1; return i; } if ((a->top == 1) && (a->d[0] < w)) { a->d[0] = w - a->d[0]; a->neg = 1; return 1; } i = 0; for (;;) { if (a->d[i] >= w) { a->d[i] -= w; break; } else { a->d[i] = (a->d[i] - w) & BN_MASK2; i++; w = 1; } } if ((a->d[i] == 0) && (i == (a->top - 1))) a->top--; bn_check_top(a); return 1; }
['int DH_check_params(const DH *dh, int *ret)\n{\n int ok = 0;\n BIGNUM *tmp = NULL;\n BN_CTX *ctx = NULL;\n *ret = 0;\n ctx = BN_CTX_new();\n if (ctx == NULL)\n goto err;\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n if (tmp == NULL)\n goto err;\n if (!BN_is_odd(dh->p))\n *ret |= DH_CHECK_P_NOT_PRIME;\n if (BN_is_negative(dh->g) || BN_is_zero(dh->g) || BN_is_one(dh->g))\n *ret |= DH_NOT_SUITABLE_GENERATOR;\n if (BN_copy(tmp, dh->p) == NULL || !BN_sub_word(tmp, 1))\n goto err;\n if (BN_cmp(dh->g, tmp) >= 0)\n *ret |= DH_NOT_SUITABLE_GENERATOR;\n ok = 1;\n err:\n if (ctx != NULL) {\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n }\n return ok;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return 1;\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'int BN_sub_word(BIGNUM *a, BN_ULONG w)\n{\n int i;\n bn_check_top(a);\n w &= BN_MASK2;\n if (!w)\n return 1;\n if (BN_is_zero(a)) {\n i = BN_set_word(a, w);\n if (i != 0)\n BN_set_negative(a, 1);\n return i;\n }\n if (a->neg) {\n a->neg = 0;\n i = BN_add_word(a, w);\n a->neg = 1;\n return i;\n }\n if ((a->top == 1) && (a->d[0] < w)) {\n a->d[0] = w - a->d[0];\n a->neg = 1;\n return 1;\n }\n i = 0;\n for (;;) {\n if (a->d[i] >= w) {\n a->d[i] -= w;\n break;\n } else {\n a->d[i] = (a->d[i] - w) & BN_MASK2;\n i++;\n w = 1;\n }\n }\n if ((a->d[i] == 0) && (i == (a->top - 1)))\n a->top--;\n bn_check_top(a);\n return 1;\n}']
2,392
0
https://github.com/libav/libav/blob/2f99117f6ff24ce5be2abb9e014cb8b86c2aa0e0/libavcodec/bitstream.h/#L237
static inline void skip_remaining(BitstreamContext *bc, unsigned n) { #ifdef BITSTREAM_READER_LE bc->bits >>= n; #else bc->bits <<= n; #endif bc->bits_left -= n; }
['static int theora_header(AVFormatContext *s, int idx)\n{\n struct ogg *ogg = s->priv_data;\n struct ogg_stream *os = ogg->streams + idx;\n AVStream *st = s->streams[idx];\n TheoraParams *thp = os->private;\n int cds = st->codecpar->extradata_size + os->psize + 2;\n int err;\n uint8_t *cdp;\n if (!(os->buf[os->pstart] & 0x80))\n return 0;\n if (!thp) {\n thp = av_mallocz(sizeof(*thp));\n if (!thp)\n return AVERROR(ENOMEM);\n os->private = thp;\n }\n switch (os->buf[os->pstart]) {\n case 0x80: {\n BitstreamContext bc;\n AVRational timebase;\n bitstream_init(&bc, os->buf + os->pstart, os->psize * 8);\n bitstream_skip(&bc, 7 * 8);\n thp->version = bitstream_read(&bc, 24);\n if (thp->version < 0x030100) {\n av_log(s, AV_LOG_ERROR,\n "Too old or unsupported Theora (%x)\\n", thp->version);\n return AVERROR(ENOSYS);\n }\n st->codecpar->width = bitstream_read(&bc, 16) << 4;\n st->codecpar->height = bitstream_read(&bc, 16) << 4;\n if (thp->version >= 0x030400)\n bitstream_skip(&bc, 100);\n if (thp->version >= 0x030200) {\n int width = bitstream_read(&bc, 24);\n int height = bitstream_read(&bc, 24);\n if (width <= st->codecpar->width && width > st->codecpar->width - 16 &&\n height <= st->codecpar->height && height > st->codecpar->height - 16) {\n st->codecpar->width = width;\n st->codecpar->height = height;\n }\n bitstream_skip(&bc, 16);\n }\n timebase.den = bitstream_read(&bc, 32);\n timebase.num = bitstream_read(&bc, 32);\n if (!(timebase.num > 0 && timebase.den > 0)) {\n av_log(s, AV_LOG_WARNING, "Invalid time base in theora stream, assuming 25 FPS\\n");\n timebase.num = 1;\n timebase.den = 25;\n }\n avpriv_set_pts_info(st, 64, timebase.num, timebase.den);\n st->sample_aspect_ratio.num = bitstream_read(&bc, 24);\n st->sample_aspect_ratio.den = bitstream_read(&bc, 24);\n if (thp->version >= 0x030200)\n bitstream_skip(&bc, 38);\n if (thp->version >= 0x304000)\n bitstream_skip(&bc, 2);\n thp->gpshift = bitstream_read(&bc, 5);\n thp->gpmask = (1 << thp->gpshift) - 1;\n st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;\n st->codecpar->codec_id = AV_CODEC_ID_THEORA;\n st->need_parsing = AVSTREAM_PARSE_HEADERS;\n }\n break;\n case 0x81:\n ff_vorbis_stream_comment(s, st, os->buf + os->pstart + 7, os->psize - 7);\n case 0x82:\n if (!thp->version)\n return AVERROR_INVALIDDATA;\n break;\n default:\n return AVERROR_INVALIDDATA;\n }\n if ((err = av_reallocp(&st->codecpar->extradata,\n cds + AV_INPUT_BUFFER_PADDING_SIZE)) < 0) {\n st->codecpar->extradata_size = 0;\n return err;\n }\n cdp = st->codecpar->extradata + st->codecpar->extradata_size;\n *cdp++ = os->psize >> 8;\n *cdp++ = os->psize & 0xff;\n memcpy(cdp, os->buf + os->pstart, os->psize);\n st->codecpar->extradata_size = cds;\n return 1;\n}', 'static inline int bitstream_init(BitstreamContext *bc, const uint8_t *buffer,\n unsigned bit_size)\n{\n unsigned buffer_size;\n if (bit_size > INT_MAX - 7 || !buffer) {\n buffer =\n bc->buffer =\n bc->ptr = NULL;\n bc->bits_left = 0;\n return AVERROR_INVALIDDATA;\n }\n buffer_size = (bit_size + 7) >> 3;\n bc->buffer = buffer;\n bc->buffer_end = buffer + buffer_size;\n bc->ptr = bc->buffer;\n bc->size_in_bits = bit_size;\n bc->bits_left = 0;\n bc->bits = 0;\n refill_64(bc);\n return 0;\n}', 'static inline 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}']
2,393
0
https://github.com/libav/libav/blob/e5d403720ec4914169f55913a5a5555d908500b6/libavcodec/svq3.c/#L145
void ff_svq3_luma_dc_dequant_idct_c(DCTELEM *output, DCTELEM *input, int qp){ const int qmul = svq3_dequant_coeff[qp]; #define stride 16 int i; int temp[16]; static const uint8_t x_offset[4]={0, 1*stride, 4*stride, 5*stride}; for(i=0; i<4; i++){ const int z0 = 13*(input[4*i+0] + input[4*i+2]); const int z1 = 13*(input[4*i+0] - input[4*i+2]); const int z2 = 7* input[4*i+1] - 17*input[4*i+3]; const int z3 = 17* input[4*i+1] + 7*input[4*i+3]; temp[4*i+0] = z0+z3; temp[4*i+1] = z1+z2; temp[4*i+2] = z1-z2; temp[4*i+3] = z0-z3; } for(i=0; i<4; i++){ const int offset= x_offset[i]; const int z0= 13*(temp[4*0+i] + temp[4*2+i]); const int z1= 13*(temp[4*0+i] - temp[4*2+i]); const int z2= 7* temp[4*1+i] - 17*temp[4*3+i]; const int z3= 17* temp[4*1+i] + 7*temp[4*3+i]; output[stride* 0+offset] = ((z0 + z3)*qmul + 0x80000) >> 20; output[stride* 2+offset] = ((z1 + z2)*qmul + 0x80000) >> 20; output[stride* 8+offset] = ((z1 - z2)*qmul + 0x80000) >> 20; output[stride*10+offset] = ((z0 - z3)*qmul + 0x80000) >> 20; } }
['static av_always_inline void hl_decode_mb_predict_luma(H264Context *h,\n int mb_type, int is_h264,\n int simple,\n int transform_bypass,\n int pixel_shift,\n int *block_offset,\n int linesize,\n uint8_t *dest_y, int p)\n{\n MpegEncContext *const s = &h->s;\n void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);\n void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);\n int i;\n int qscale = p == 0 ? s->qscale : h->chroma_qp[p - 1];\n block_offset += 16 * p;\n if (IS_INTRA4x4(mb_type)) {\n if (simple || !s->encoding) {\n if (IS_8x8DCT(mb_type)) {\n if (transform_bypass) {\n idct_dc_add =\n idct_add = s->dsp.add_pixels8;\n } else {\n idct_dc_add = h->h264dsp.h264_idct8_dc_add;\n idct_add = h->h264dsp.h264_idct8_add;\n }\n for (i = 0; i < 16; i += 4) {\n uint8_t *const ptr = dest_y + block_offset[i];\n const int dir = h->intra4x4_pred_mode_cache[scan8[i]];\n if (transform_bypass && h->sps.profile_idc == 244 && dir <= 1) {\n h->hpc.pred8x8l_add[dir](ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);\n } else {\n const int nnz = h->non_zero_count_cache[scan8[i + p * 16]];\n h->hpc.pred8x8l[dir](ptr, (h->topleft_samples_available << i) & 0x8000,\n (h->topright_samples_available << i) & 0x4000, linesize);\n if (nnz) {\n if (nnz == 1 && dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))\n idct_dc_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);\n else\n idct_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);\n }\n }\n }\n } else {\n if (transform_bypass) {\n idct_dc_add =\n idct_add = s->dsp.add_pixels4;\n } else {\n idct_dc_add = h->h264dsp.h264_idct_dc_add;\n idct_add = h->h264dsp.h264_idct_add;\n }\n for (i = 0; i < 16; i++) {\n uint8_t *const ptr = dest_y + block_offset[i];\n const int dir = h->intra4x4_pred_mode_cache[scan8[i]];\n if (transform_bypass && h->sps.profile_idc == 244 && dir <= 1) {\n h->hpc.pred4x4_add[dir](ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);\n } else {\n uint8_t *topright;\n int nnz, tr;\n uint64_t tr_high;\n if (dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED) {\n const int topright_avail = (h->topright_samples_available << i) & 0x8000;\n assert(s->mb_y || linesize <= block_offset[i]);\n if (!topright_avail) {\n if (pixel_shift) {\n tr_high = ((uint16_t *)ptr)[3 - linesize / 2] * 0x0001000100010001ULL;\n topright = (uint8_t *)&tr_high;\n } else {\n tr = ptr[3 - linesize] * 0x01010101u;\n topright = (uint8_t *)&tr;\n }\n } else\n topright = ptr + (4 << pixel_shift) - linesize;\n } else\n topright = NULL;\n h->hpc.pred4x4[dir](ptr, topright, linesize);\n nnz = h->non_zero_count_cache[scan8[i + p * 16]];\n if (nnz) {\n if (is_h264) {\n if (nnz == 1 && dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))\n idct_dc_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);\n else\n idct_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);\n } else if (CONFIG_SVQ3_DECODER)\n ff_svq3_add_idct_c(ptr, h->mb + i * 16 + p * 256, linesize, qscale, 0);\n }\n }\n }\n }\n }\n } else {\n h->hpc.pred16x16[h->intra16x16_pred_mode](dest_y, linesize);\n if (is_h264) {\n if (h->non_zero_count_cache[scan8[LUMA_DC_BLOCK_INDEX + p]]) {\n if (!transform_bypass)\n h->h264dsp.h264_luma_dc_dequant_idct(h->mb + (p * 256 << pixel_shift),\n h->mb_luma_dc[p],\n h->dequant4_coeff[p][qscale][0]);\n else {\n static const uint8_t dc_mapping[16] = {\n 0 * 16, 1 * 16, 4 * 16, 5 * 16,\n 2 * 16, 3 * 16, 6 * 16, 7 * 16,\n 8 * 16, 9 * 16, 12 * 16, 13 * 16,\n 10 * 16, 11 * 16, 14 * 16, 15 * 16 };\n for (i = 0; i < 16; i++)\n dctcoef_set(h->mb + (p * 256 << pixel_shift),\n pixel_shift, dc_mapping[i],\n dctcoef_get(h->mb_luma_dc[p],\n pixel_shift, i));\n }\n }\n } else if (CONFIG_SVQ3_DECODER)\n ff_svq3_luma_dc_dequant_idct_c(h->mb + p * 256,\n h->mb_luma_dc[p], qscale);\n }\n}', 'void ff_svq3_luma_dc_dequant_idct_c(DCTELEM *output, DCTELEM *input, int qp){\n const int qmul = svq3_dequant_coeff[qp];\n#define stride 16\n int i;\n int temp[16];\n static const uint8_t x_offset[4]={0, 1*stride, 4*stride, 5*stride};\n for(i=0; i<4; i++){\n const int z0 = 13*(input[4*i+0] + input[4*i+2]);\n const int z1 = 13*(input[4*i+0] - input[4*i+2]);\n const int z2 = 7* input[4*i+1] - 17*input[4*i+3];\n const int z3 = 17* input[4*i+1] + 7*input[4*i+3];\n temp[4*i+0] = z0+z3;\n temp[4*i+1] = z1+z2;\n temp[4*i+2] = z1-z2;\n temp[4*i+3] = z0-z3;\n }\n for(i=0; i<4; i++){\n const int offset= x_offset[i];\n const int z0= 13*(temp[4*0+i] + temp[4*2+i]);\n const int z1= 13*(temp[4*0+i] - temp[4*2+i]);\n const int z2= 7* temp[4*1+i] - 17*temp[4*3+i];\n const int z3= 17* temp[4*1+i] + 7*temp[4*3+i];\n output[stride* 0+offset] = ((z0 + z3)*qmul + 0x80000) >> 20;\n output[stride* 2+offset] = ((z1 + z2)*qmul + 0x80000) >> 20;\n output[stride* 8+offset] = ((z1 - z2)*qmul + 0x80000) >> 20;\n output[stride*10+offset] = ((z0 - z3)*qmul + 0x80000) >> 20;\n }\n}']
2,394
0
https://gitlab.com/libtiff/libtiff/blob/bfbc717684115d7beb96c82255dad2dd4a4b8845/libtiff/tif_swab.c/#L113
void TIFFSwabArrayOfLong(register uint32* lp, tmsize_t n) { register unsigned char *cp; register unsigned char t; assert(sizeof(uint32)==4); while (n-- > 0) { cp = (unsigned char *)lp; t = cp[3]; cp[3] = cp[0]; cp[0] = t; t = cp[2]; cp[2] = cp[1]; cp[1] = t; lp++; } }
['static int\nwriteSingleSection(TIFF *in, TIFF *out, struct image_data *image,\n struct dump_opts *dump, uint32 width, uint32 length,\n double hres, double vres,\n unsigned char *sect_buff)\n {\n uint16 bps, spp;\n uint16 input_compression, input_photometric;\n uint16 input_jpeg_colormode, input_planar;\n struct cpTag* p;\n TIFFGetField(in, TIFFTAG_PHOTOMETRIC, &input_photometric);\n TIFFGetField(in, TIFFTAG_PHOTOMETRIC, &spp);\n TIFFGetField(in, TIFFTAG_PHOTOMETRIC, &bps);\n TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width);\n TIFFSetField(out, TIFFTAG_IMAGELENGTH, length);\n CopyField(TIFFTAG_BITSPERSAMPLE, bps);\n CopyField(TIFFTAG_SAMPLESPERPIXEL, spp);\n TIFFGetField(in, TIFFTAG_COMPRESSION, &input_compression);\n if (compression != (uint16)-1)\n TIFFSetField(out, TIFFTAG_COMPRESSION, compression);\n else\n {\n if (input_compression == COMPRESSION_OJPEG)\n {\n TIFFSetField(out, TIFFTAG_COMPRESSION, COMPRESSION_JPEG);\n compression = COMPRESSION_JPEG;\n }\n else\n CopyField(TIFFTAG_COMPRESSION, compression);\n }\n TIFFGetField(in, TIFFTAG_JPEGCOLORMODE, &input_jpeg_colormode);\n#ifdef DEBUG2\n TIFFError("writeSingleSection", "Input compression: %s",\n\t (input_compression == COMPRESSION_OJPEG) ? "Old Jpeg" :\n\t ((input_compression == COMPRESSION_JPEG) ? "New Jpeg" : "Non Jpeg"));\n#endif\n if (compression == COMPRESSION_JPEG)\n {\n if ((input_photometric == PHOTOMETRIC_PALETTE) ||\n (input_photometric == PHOTOMETRIC_MASK))\n {\n TIFFError ("writeSingleSection",\n "JPEG compression cannot be used with %s image data",\n\t\t (input_photometric == PHOTOMETRIC_PALETTE) ?\n "palette" : "mask");\n return (-1);\n }\n if (input_photometric == PHOTOMETRIC_RGB)\n {\n if (jpegcolormode == JPEGCOLORMODE_RGB)\n\tTIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR);\n else\n\tTIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);\n }\n else\n\tTIFFSetField(out, TIFFTAG_PHOTOMETRIC, input_photometric);\n }\n else\n {\n if (compression == COMPRESSION_SGILOG || compression == COMPRESSION_SGILOG24)\n TIFFSetField(out, TIFFTAG_PHOTOMETRIC, spp == 1 ?\n\t\t\tPHOTOMETRIC_LOGL : PHOTOMETRIC_LOGLUV);\n else\n TIFFSetField(out, TIFFTAG_PHOTOMETRIC, image->photometric);\n }\n if (((input_photometric == PHOTOMETRIC_LOGL) ||\n (input_photometric == PHOTOMETRIC_LOGLUV)) &&\n ((compression != COMPRESSION_SGILOG) &&\n (compression != COMPRESSION_SGILOG24)))\n {\n TIFFError("writeCroppedImage",\n "LogL and LogLuv source data require SGI_LOG or SGI_LOG24 compression");\n return (-1);\n }\n if (fillorder != 0)\n TIFFSetField(out, TIFFTAG_FILLORDER, fillorder);\n else\n CopyTag(TIFFTAG_FILLORDER, 1, TIFF_SHORT);\n TIFFSetField(out, TIFFTAG_ORIENTATION, image->orientation);\n if (outtiled == -1)\n outtiled = TIFFIsTiled(in);\n if (outtiled) {\n if (tilewidth == (uint32) 0)\n TIFFGetField(in, TIFFTAG_TILEWIDTH, &tilewidth);\n if (tilelength == (uint32) 0)\n TIFFGetField(in, TIFFTAG_TILELENGTH, &tilelength);\n if (tilewidth == 0 || tilelength == 0)\n TIFFDefaultTileSize(out, &tilewidth, &tilelength);\n TIFFDefaultTileSize(out, &tilewidth, &tilelength);\n TIFFSetField(out, TIFFTAG_TILEWIDTH, tilewidth);\n TIFFSetField(out, TIFFTAG_TILELENGTH, tilelength);\n } else {\n\tif (rowsperstrip == (uint32) 0)\n {\n\t if (!TIFFGetField(in, TIFFTAG_ROWSPERSTRIP, &rowsperstrip))\n\t rowsperstrip = TIFFDefaultStripSize(out, rowsperstrip);\n if (compression != COMPRESSION_JPEG)\n {\n \t if (rowsperstrip > length)\n\t rowsperstrip = length;\n\t }\n\t }\n\telse\n if (rowsperstrip == (uint32) -1)\n\t rowsperstrip = length;\n\tTIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip);\n\t}\n TIFFGetFieldDefaulted(in, TIFFTAG_PLANARCONFIG, &input_planar);\n if (config != (uint16) -1)\n TIFFSetField(out, TIFFTAG_PLANARCONFIG, config);\n else\n CopyField(TIFFTAG_PLANARCONFIG, config);\n if (spp <= 4)\n CopyTag(TIFFTAG_TRANSFERFUNCTION, 4, TIFF_SHORT);\n CopyTag(TIFFTAG_COLORMAP, 4, TIFF_SHORT);\n switch (compression) {\n case COMPRESSION_JPEG:\n if (((bps % 8) == 0) || ((bps % 12) == 0))\n\t {\n TIFFSetField(out, TIFFTAG_JPEGQUALITY, quality);\n\t TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);\n }\n else\n {\n\t TIFFError("writeCroppedImage",\n "JPEG compression requires 8 or 12 bits per sample");\n return (-1);\n }\n\t break;\n case COMPRESSION_LZW:\n case COMPRESSION_ADOBE_DEFLATE:\n case COMPRESSION_DEFLATE:\n\tif (predictor != (uint16)-1)\n TIFFSetField(out, TIFFTAG_PREDICTOR, predictor);\n\telse\n\t CopyField(TIFFTAG_PREDICTOR, predictor);\n\tbreak;\n case COMPRESSION_CCITTFAX3:\n case COMPRESSION_CCITTFAX4:\n\tif (compression == COMPRESSION_CCITTFAX3) {\n if (g3opts != (uint32) -1)\n\t TIFFSetField(out, TIFFTAG_GROUP3OPTIONS, g3opts);\n\t else\n\t CopyField(TIFFTAG_GROUP3OPTIONS, g3opts);\n\t} else\n\t CopyTag(TIFFTAG_GROUP4OPTIONS, 1, TIFF_LONG);\n\t CopyTag(TIFFTAG_BADFAXLINES, 1, TIFF_LONG);\n\t CopyTag(TIFFTAG_CLEANFAXDATA, 1, TIFF_LONG);\n\t CopyTag(TIFFTAG_CONSECUTIVEBADFAXLINES, 1, TIFF_LONG);\n\t CopyTag(TIFFTAG_FAXRECVPARAMS, 1, TIFF_LONG);\n\t CopyTag(TIFFTAG_FAXRECVTIME, 1, TIFF_LONG);\n\t CopyTag(TIFFTAG_FAXSUBADDRESS, 1, TIFF_ASCII);\n\tbreak;\n }\n { uint32 len32;\n void** data;\n if (TIFFGetField(in, TIFFTAG_ICCPROFILE, &len32, &data))\n TIFFSetField(out, TIFFTAG_ICCPROFILE, len32, data);\n }\n { uint16 ninks;\n const char* inknames;\n if (TIFFGetField(in, TIFFTAG_NUMBEROFINKS, &ninks)) {\n TIFFSetField(out, TIFFTAG_NUMBEROFINKS, ninks);\n if (TIFFGetField(in, TIFFTAG_INKNAMES, &inknames)) {\n\t int inknameslen = strlen(inknames) + 1;\n\t const char* cp = inknames;\n\t while (ninks > 1) {\n\t cp = strchr(cp, \'\\0\');\n\t if (cp) {\n\t cp++;\n\t inknameslen += (strlen(cp) + 1);\n\t }\n\t ninks--;\n }\n\t TIFFSetField(out, TIFFTAG_INKNAMES, inknameslen, inknames);\n }\n }\n }\n {\n unsigned short pg0, pg1;\n if (TIFFGetField(in, TIFFTAG_PAGENUMBER, &pg0, &pg1)) {\n if (pageNum < 0)\n\tTIFFSetField(out, TIFFTAG_PAGENUMBER, pg0, pg1);\n else\n\tTIFFSetField(out, TIFFTAG_PAGENUMBER, pageNum++, 0);\n }\n }\n for (p = tags; p < &tags[NTAGS]; p++)\n\t\tCopyTag(p->tag, p->count, p->type);\n TIFFSetField(out, TIFFTAG_XRESOLUTION, (float)hres);\n TIFFSetField(out, TIFFTAG_YRESOLUTION, (float)vres);\n if (outtiled)\n {\n if (config == PLANARCONFIG_CONTIG)\n writeBufferToContigTiles (out, sect_buff, length, width, spp, dump);\n else\n writeBufferToSeparateTiles (out, sect_buff, length, width, spp, dump);\n }\n else\n {\n if (config == PLANARCONFIG_CONTIG)\n writeBufferToContigStrips (out, sect_buff, length);\n else\n writeBufferToSeparateStrips(out, sect_buff, length, width, spp, dump);\n }\n if (!TIFFWriteDirectory(out))\n {\n TIFFClose(out);\n return (-1);\n }\n return (0);\n }', 'static int writeBufferToContigStrips(TIFF* out, uint8* buf, uint32 imagelength)\n {\n uint32 row, nrows, rowsperstrip;\n tstrip_t strip = 0;\n tsize_t stripsize;\n TIFFGetFieldDefaulted(out, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);\n for (row = 0; row < imagelength; row += rowsperstrip)\n {\n nrows = (row + rowsperstrip > imagelength) ?\n\t imagelength - row : rowsperstrip;\n stripsize = TIFFVStripSize(out, nrows);\n if (TIFFWriteEncodedStrip(out, strip++, buf, stripsize) < 0)\n {\n TIFFError(TIFFFileName(out), "Error, can\'t write strip %u", strip - 1);\n return 1;\n }\n buf += stripsize;\n }\n return 0;\n }', 'int\nTIFFGetFieldDefaulted(TIFF* tif, uint32 tag, ...)\n{\n\tint ok;\n\tva_list ap;\n\tva_start(ap, tag);\n\tok = TIFFVGetFieldDefaulted(tif, tag, ap);\n\tva_end(ap);\n\treturn (ok);\n}', 'int\nTIFFVGetFieldDefaulted(TIFF* tif, uint32 tag, va_list ap)\n{\n\tTIFFDirectory *td = &tif->tif_dir;\n\tif (TIFFVGetField(tif, tag, ap))\n\t\treturn (1);\n\tswitch (tag) {\n\tcase TIFFTAG_SUBFILETYPE:\n\t\t*va_arg(ap, uint32 *) = td->td_subfiletype;\n\t\treturn (1);\n\tcase TIFFTAG_BITSPERSAMPLE:\n\t\t*va_arg(ap, uint16 *) = td->td_bitspersample;\n\t\treturn (1);\n\tcase TIFFTAG_THRESHHOLDING:\n\t\t*va_arg(ap, uint16 *) = td->td_threshholding;\n\t\treturn (1);\n\tcase TIFFTAG_FILLORDER:\n\t\t*va_arg(ap, uint16 *) = td->td_fillorder;\n\t\treturn (1);\n\tcase TIFFTAG_ORIENTATION:\n\t\t*va_arg(ap, uint16 *) = td->td_orientation;\n\t\treturn (1);\n\tcase TIFFTAG_SAMPLESPERPIXEL:\n\t\t*va_arg(ap, uint16 *) = td->td_samplesperpixel;\n\t\treturn (1);\n\tcase TIFFTAG_ROWSPERSTRIP:\n\t\t*va_arg(ap, uint32 *) = td->td_rowsperstrip;\n\t\treturn (1);\n\tcase TIFFTAG_MINSAMPLEVALUE:\n\t\t*va_arg(ap, uint16 *) = td->td_minsamplevalue;\n\t\treturn (1);\n\tcase TIFFTAG_MAXSAMPLEVALUE:\n\t\t*va_arg(ap, uint16 *) = td->td_maxsamplevalue;\n\t\treturn (1);\n\tcase TIFFTAG_PLANARCONFIG:\n\t\t*va_arg(ap, uint16 *) = td->td_planarconfig;\n\t\treturn (1);\n\tcase TIFFTAG_RESOLUTIONUNIT:\n\t\t*va_arg(ap, uint16 *) = td->td_resolutionunit;\n\t\treturn (1);\n\tcase TIFFTAG_PREDICTOR:\n {\n\t\t\tTIFFPredictorState* sp = (TIFFPredictorState*) tif->tif_data;\n\t\t\t*va_arg(ap, uint16*) = (uint16) sp->predictor;\n\t\t\treturn 1;\n }\n\tcase TIFFTAG_DOTRANGE:\n\t\t*va_arg(ap, uint16 *) = 0;\n\t\t*va_arg(ap, uint16 *) = (1<<td->td_bitspersample)-1;\n\t\treturn (1);\n\tcase TIFFTAG_INKSET:\n\t\t*va_arg(ap, uint16 *) = INKSET_CMYK;\n\t\treturn 1;\n\tcase TIFFTAG_NUMBEROFINKS:\n\t\t*va_arg(ap, uint16 *) = 4;\n\t\treturn (1);\n\tcase TIFFTAG_EXTRASAMPLES:\n\t\t*va_arg(ap, uint16 *) = td->td_extrasamples;\n\t\t*va_arg(ap, uint16 **) = td->td_sampleinfo;\n\t\treturn (1);\n\tcase TIFFTAG_MATTEING:\n\t\t*va_arg(ap, uint16 *) =\n\t\t (td->td_extrasamples == 1 &&\n\t\t td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);\n\t\treturn (1);\n\tcase TIFFTAG_TILEDEPTH:\n\t\t*va_arg(ap, uint32 *) = td->td_tiledepth;\n\t\treturn (1);\n\tcase TIFFTAG_DATATYPE:\n\t\t*va_arg(ap, uint16 *) = td->td_sampleformat-1;\n\t\treturn (1);\n\tcase TIFFTAG_SAMPLEFORMAT:\n\t\t*va_arg(ap, uint16 *) = td->td_sampleformat;\n return(1);\n\tcase TIFFTAG_IMAGEDEPTH:\n\t\t*va_arg(ap, uint32 *) = td->td_imagedepth;\n\t\treturn (1);\n\tcase TIFFTAG_YCBCRCOEFFICIENTS:\n\t\t{\n\t\t\tstatic float ycbcrcoeffs[] = { 0.299f, 0.587f, 0.114f };\n\t\t\t*va_arg(ap, float **) = ycbcrcoeffs;\n\t\t\treturn 1;\n\t\t}\n\tcase TIFFTAG_YCBCRSUBSAMPLING:\n\t\t*va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[0];\n\t\t*va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[1];\n\t\treturn (1);\n\tcase TIFFTAG_YCBCRPOSITIONING:\n\t\t*va_arg(ap, uint16 *) = td->td_ycbcrpositioning;\n\t\treturn (1);\n\tcase TIFFTAG_WHITEPOINT:\n\t\t{\n\t\t\tstatic float whitepoint[2];\n\t\t\twhitepoint[0] =\tD50_X0 / (D50_X0 + D50_Y0 + D50_Z0);\n\t\t\twhitepoint[1] =\tD50_Y0 / (D50_X0 + D50_Y0 + D50_Z0);\n\t\t\t*va_arg(ap, float **) = whitepoint;\n\t\t\treturn 1;\n\t\t}\n\tcase TIFFTAG_TRANSFERFUNCTION:\n\t\tif (!td->td_transferfunction[0] &&\n\t\t !TIFFDefaultTransferFunction(td)) {\n\t\t\tTIFFErrorExt(tif->tif_clientdata, tif->tif_name, "No space for \\"TransferFunction\\" tag");\n\t\t\treturn (0);\n\t\t}\n\t\t*va_arg(ap, uint16 **) = td->td_transferfunction[0];\n\t\tif (td->td_samplesperpixel - td->td_extrasamples > 1) {\n\t\t\t*va_arg(ap, uint16 **) = td->td_transferfunction[1];\n\t\t\t*va_arg(ap, uint16 **) = td->td_transferfunction[2];\n\t\t}\n\t\treturn (1);\n\tcase TIFFTAG_REFERENCEBLACKWHITE:\n\t\tif (!td->td_refblackwhite && !TIFFDefaultRefBlackWhite(td))\n\t\t\treturn (0);\n\t\t*va_arg(ap, float **) = td->td_refblackwhite;\n\t\treturn (1);\n\t}\n\treturn 0;\n}', 'int\nTIFFWriteDirectory(TIFF* tif)\n{\n\treturn TIFFWriteDirectorySec(tif,TRUE,TRUE,NULL);\n}', 'static int\nTIFFWriteDirectorySec(TIFF* tif, int isimage, int imagedone, uint64* pdiroff)\n{\n\tstatic const char module[] = "TIFFWriteDirectorySec";\n\tuint32 ndir;\n\tTIFFDirEntry* dir;\n\tuint32 dirsize;\n\tvoid* dirmem;\n\tuint32 m;\n\tif (tif->tif_mode == O_RDONLY)\n\t\treturn (1);\n\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 (!TIFFWriteDirectoryTagSampleformatPerSample(tif,&ndir,dir,TIFFTAG_SMINSAMPLEVALUE,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 (!TIFFWriteDirectoryTagSampleformatPerSample(tif,&ndir,dir,TIFFTAG_SMAXSAMPLEVALUE,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_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}', 'void\nTIFFClose(TIFF* tif)\n{\n\tTIFFCloseProc closeproc = tif->tif_closeproc;\n\tthandle_t fd = tif->tif_clientdata;\n\tTIFFCleanup(tif);\n\t(void) (*closeproc)(fd);\n}', 'void\nTIFFCleanup(TIFF* tif)\n{\n\tif (tif->tif_mode != O_RDONLY)\n\t\tTIFFFlush(tif);\n\t(*tif->tif_cleanup)(tif);\n\tTIFFFreeDirectory(tif);\n\tif (tif->tif_dirlist)\n\t\t_TIFFfree(tif->tif_dirlist);\n\twhile( tif->tif_clientinfo )\n\t{\n\t\tTIFFClientInfoLink *link = tif->tif_clientinfo;\n\t\ttif->tif_clientinfo = link->next;\n\t\t_TIFFfree( link->name );\n\t\t_TIFFfree( link );\n\t}\n\tif (tif->tif_rawdata && (tif->tif_flags&TIFF_MYBUFFER))\n\t\t_TIFFfree(tif->tif_rawdata);\n\tif (isMapped(tif))\n\t\tTIFFUnmapFileContents(tif, tif->tif_base, (toff_t)tif->tif_size);\n\tif (tif->tif_fields && tif->tif_nfields > 0) {\n\t\tuint32 i;\n\t\tfor (i = 0; i < tif->tif_nfields; i++) {\n\t\t\tTIFFField *fld = tif->tif_fields[i];\n\t\t\tif (fld->field_bit == FIELD_CUSTOM &&\n\t\t\t strncmp("Tag ", fld->field_name, 4) == 0) {\n\t\t\t\t_TIFFfree(fld->field_name);\n\t\t\t\t_TIFFfree(fld);\n\t\t\t}\n\t\t}\n\t\t_TIFFfree(tif->tif_fields);\n\t}\n if (tif->tif_nfieldscompat > 0) {\n uint32 i;\n for (i = 0; i < tif->tif_nfieldscompat; i++) {\n if (tif->tif_fieldscompat[i].allocated_size)\n _TIFFfree(tif->tif_fieldscompat[i].fields);\n }\n _TIFFfree(tif->tif_fieldscompat);\n }\n\t_TIFFfree(tif);\n}', 'int\nTIFFFlush(TIFF* tif)\n{\n if( tif->tif_mode == O_RDONLY )\n return 1;\n if (!TIFFFlushData(tif))\n return (0);\n if( (tif->tif_flags & TIFF_DIRTYSTRIP)\n && !(tif->tif_flags & TIFF_DIRTYDIRECT)\n && tif->tif_mode == O_RDWR )\n {\n uint64 *offsets=NULL, *sizes=NULL;\n if( TIFFIsTiled(tif) )\n {\n if( TIFFGetField( tif, TIFFTAG_TILEOFFSETS, &offsets )\n && TIFFGetField( tif, TIFFTAG_TILEBYTECOUNTS, &sizes )\n && _TIFFRewriteField( tif, TIFFTAG_TILEOFFSETS, TIFF_LONG8,\n tif->tif_dir.td_nstrips, offsets )\n && _TIFFRewriteField( tif, TIFFTAG_TILEBYTECOUNTS, TIFF_LONG8,\n tif->tif_dir.td_nstrips, sizes ) )\n {\n tif->tif_flags &= ~TIFF_DIRTYSTRIP;\n tif->tif_flags &= ~TIFF_BEENWRITING;\n return 1;\n }\n }\n else\n {\n if( TIFFGetField( tif, TIFFTAG_STRIPOFFSETS, &offsets )\n && TIFFGetField( tif, TIFFTAG_STRIPBYTECOUNTS, &sizes )\n && _TIFFRewriteField( tif, TIFFTAG_STRIPOFFSETS, TIFF_LONG8,\n tif->tif_dir.td_nstrips, offsets )\n && _TIFFRewriteField( tif, TIFFTAG_STRIPBYTECOUNTS, TIFF_LONG8,\n tif->tif_dir.td_nstrips, sizes ) )\n {\n tif->tif_flags &= ~TIFF_DIRTYSTRIP;\n tif->tif_flags &= ~TIFF_BEENWRITING;\n return 1;\n }\n }\n }\n if ((tif->tif_flags & (TIFF_DIRTYDIRECT|TIFF_DIRTYSTRIP))\n && !TIFFRewriteDirectory(tif))\n return (0);\n return (1);\n}', 'int\nTIFFRewriteDirectory( TIFF *tif )\n{\n\tstatic const char module[] = "TIFFRewriteDirectory";\n\tif( tif->tif_diroff == 0 )\n\t\treturn TIFFWriteDirectory( tif );\n\tif (!(tif->tif_flags&TIFF_BIGTIFF))\n\t{\n\t\tif (tif->tif_header.classic.tiff_diroff == tif->tif_diroff)\n\t\t{\n\t\t\ttif->tif_header.classic.tiff_diroff = 0;\n\t\t\ttif->tif_diroff = 0;\n\t\t\tTIFFSeekFile(tif,4,SEEK_SET);\n\t\t\tif (!WriteOK(tif, &(tif->tif_header.classic.tiff_diroff),4))\n\t\t\t{\n\t\t\t\tTIFFErrorExt(tif->tif_clientdata, tif->tif_name,\n\t\t\t\t "Error updating TIFF header");\n\t\t\t\treturn (0);\n\t\t\t}\n\t\t}\n\t\telse\n\t\t{\n\t\t\tuint32 nextdir;\n\t\t\tnextdir = tif->tif_header.classic.tiff_diroff;\n\t\t\twhile(1) {\n\t\t\t\tuint16 dircount;\n\t\t\t\tuint32 nextnextdir;\n\t\t\t\tif (!SeekOK(tif, nextdir) ||\n\t\t\t\t !ReadOK(tif, &dircount, 2)) {\n\t\t\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t\t\t "Error fetching directory count");\n\t\t\t\t\treturn (0);\n\t\t\t\t}\n\t\t\t\tif (tif->tif_flags & TIFF_SWAB)\n\t\t\t\t\tTIFFSwabShort(&dircount);\n\t\t\t\t(void) TIFFSeekFile(tif,\n\t\t\t\t nextdir+2+dircount*12, SEEK_SET);\n\t\t\t\tif (!ReadOK(tif, &nextnextdir, 4)) {\n\t\t\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t\t\t "Error fetching directory link");\n\t\t\t\t\treturn (0);\n\t\t\t\t}\n\t\t\t\tif (tif->tif_flags & TIFF_SWAB)\n\t\t\t\t\tTIFFSwabLong(&nextnextdir);\n\t\t\t\tif (nextnextdir==tif->tif_diroff)\n\t\t\t\t{\n\t\t\t\t\tuint32 m;\n\t\t\t\t\tm=0;\n\t\t\t\t\t(void) TIFFSeekFile(tif,\n\t\t\t\t\t nextdir+2+dircount*12, SEEK_SET);\n\t\t\t\t\tif (!WriteOK(tif, &m, 4)) {\n\t\t\t\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t\t\t\t "Error writing directory link");\n\t\t\t\t\t\treturn (0);\n\t\t\t\t\t}\n\t\t\t\t\ttif->tif_diroff=0;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tnextdir=nextnextdir;\n\t\t\t}\n\t\t}\n\t}\n\telse\n\t{\n\t\tif (tif->tif_header.big.tiff_diroff == tif->tif_diroff)\n\t\t{\n\t\t\ttif->tif_header.big.tiff_diroff = 0;\n\t\t\ttif->tif_diroff = 0;\n\t\t\tTIFFSeekFile(tif,8,SEEK_SET);\n\t\t\tif (!WriteOK(tif, &(tif->tif_header.big.tiff_diroff),8))\n\t\t\t{\n\t\t\t\tTIFFErrorExt(tif->tif_clientdata, tif->tif_name,\n\t\t\t\t "Error updating TIFF header");\n\t\t\t\treturn (0);\n\t\t\t}\n\t\t}\n\t\telse\n\t\t{\n\t\t\tuint64 nextdir;\n\t\t\tnextdir = tif->tif_header.big.tiff_diroff;\n\t\t\twhile(1) {\n\t\t\t\tuint64 dircount64;\n\t\t\t\tuint16 dircount;\n\t\t\t\tuint64 nextnextdir;\n\t\t\t\tif (!SeekOK(tif, nextdir) ||\n\t\t\t\t !ReadOK(tif, &dircount64, 8)) {\n\t\t\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t\t\t "Error fetching directory count");\n\t\t\t\t\treturn (0);\n\t\t\t\t}\n\t\t\t\tif (tif->tif_flags & TIFF_SWAB)\n\t\t\t\t\tTIFFSwabLong8(&dircount64);\n\t\t\t\tif (dircount64>0xFFFF)\n\t\t\t\t{\n\t\t\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t\t\t "Sanity check on tag count failed, likely corrupt TIFF");\n\t\t\t\t\treturn (0);\n\t\t\t\t}\n\t\t\t\tdircount=(uint16)dircount64;\n\t\t\t\t(void) TIFFSeekFile(tif,\n\t\t\t\t nextdir+8+dircount*20, SEEK_SET);\n\t\t\t\tif (!ReadOK(tif, &nextnextdir, 8)) {\n\t\t\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t\t\t "Error fetching directory link");\n\t\t\t\t\treturn (0);\n\t\t\t\t}\n\t\t\t\tif (tif->tif_flags & TIFF_SWAB)\n\t\t\t\t\tTIFFSwabLong8(&nextnextdir);\n\t\t\t\tif (nextnextdir==tif->tif_diroff)\n\t\t\t\t{\n\t\t\t\t\tuint64 m;\n\t\t\t\t\tm=0;\n\t\t\t\t\t(void) TIFFSeekFile(tif,\n\t\t\t\t\t nextdir+8+dircount*20, SEEK_SET);\n\t\t\t\t\tif (!WriteOK(tif, &m, 8)) {\n\t\t\t\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t\t\t\t "Error writing directory link");\n\t\t\t\t\t\treturn (0);\n\t\t\t\t\t}\n\t\t\t\t\ttif->tif_diroff=0;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tnextdir=nextnextdir;\n\t\t\t}\n\t\t}\n\t}\n\treturn TIFFWriteDirectory( tif );\n}', 'static int\nTIFFWriteDirectoryTagSampleformatPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)\n{\n\tswitch (tif->tif_dir.td_sampleformat)\n\t{\n\t\tcase SAMPLEFORMAT_IEEEFP:\n\t\t\tif (tif->tif_dir.td_bitspersample<=32)\n\t\t\t\treturn(TIFFWriteDirectoryTagFloatPerSample(tif,ndir,dir,tag,(float)value));\n\t\t\telse\n\t\t\t\treturn(TIFFWriteDirectoryTagDoublePerSample(tif,ndir,dir,tag,value));\n\t\tcase SAMPLEFORMAT_INT:\n\t\t\tif (tif->tif_dir.td_bitspersample<=8)\n\t\t\t\treturn(TIFFWriteDirectoryTagSbytePerSample(tif,ndir,dir,tag,(int8)value));\n\t\t\telse if (tif->tif_dir.td_bitspersample<=16)\n\t\t\t\treturn(TIFFWriteDirectoryTagSshortPerSample(tif,ndir,dir,tag,(int16)value));\n\t\t\telse\n\t\t\t\treturn(TIFFWriteDirectoryTagSlongPerSample(tif,ndir,dir,tag,(int32)value));\n\t\tcase SAMPLEFORMAT_UINT:\n\t\t\tif (tif->tif_dir.td_bitspersample<=8)\n\t\t\t\treturn(TIFFWriteDirectoryTagBytePerSample(tif,ndir,dir,tag,(uint8)value));\n\t\t\telse if (tif->tif_dir.td_bitspersample<=16)\n\t\t\t\treturn(TIFFWriteDirectoryTagShortPerSample(tif,ndir,dir,tag,(uint16)value));\n\t\t\telse\n\t\t\t\treturn(TIFFWriteDirectoryTagLongPerSample(tif,ndir,dir,tag,(uint32)value));\n\t\tdefault:\n\t\t\treturn(1);\n\t}\n}', 'static int\nTIFFWriteDirectoryTagSlongPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value)\n{\n\tstatic const char module[] = "TIFFWriteDirectoryTagSlongPerSample";\n\tint32* m;\n\tint32* na;\n\tuint16 nb;\n\tint o;\n\tif (dir==NULL)\n\t{\n\t\t(*ndir)++;\n\t\treturn(1);\n\t}\n\tm=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(int32));\n\tif (m==NULL)\n\t{\n\t\tTIFFErrorExt(tif->tif_clientdata,module,"Out of memory");\n\t\treturn(0);\n\t}\n\tfor (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)\n\t\t*na=value;\n\to=TIFFWriteDirectoryTagCheckedSlongArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);\n\t_TIFFfree(m);\n\treturn(o);\n}', 'void*\n_TIFFmalloc(tmsize_t s)\n{\n\treturn (malloc((size_t) s));\n}', 'static int\nTIFFWriteDirectoryTagCheckedSlongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int32* value)\n{\n\tassert(count<0x40000000);\n\tassert(sizeof(int32)==4);\n\tif (tif->tif_flags&TIFF_SWAB)\n\t\tTIFFSwabArrayOfLong((uint32*)value,count);\n\treturn(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SLONG,count,count*4,value));\n}', 'void\nTIFFSwabArrayOfLong(register uint32* lp, tmsize_t n)\n{\n\tregister unsigned char *cp;\n\tregister unsigned char t;\n\tassert(sizeof(uint32)==4);\n\twhile (n-- > 0) {\n\t\tcp = (unsigned char *)lp;\n\t\tt = cp[3]; cp[3] = cp[0]; cp[0] = t;\n\t\tt = cp[2]; cp[2] = cp[1]; cp[1] = t;\n\t\tlp++;\n\t}\n}']
2,395
0
https://github.com/nginx/nginx/blob/4fe0a09942f8aed90f84c77969847980e9aadd98/src/core/ngx_parse.c/#L20
ssize_t ngx_parse_size(ngx_str_t *line) { u_char unit; size_t len; ssize_t size, scale, max; len = line->len; unit = line->data[len - 1]; switch (unit) { case 'K': case 'k': len--; max = NGX_MAX_SIZE_T_VALUE / 1024; scale = 1024; break; case 'M': case 'm': len--; max = NGX_MAX_SIZE_T_VALUE / (1024 * 1024); scale = 1024 * 1024; break; default: max = NGX_MAX_SIZE_T_VALUE; scale = 1; } size = ngx_atosz(line->data, len); if (size == NGX_ERROR || size > max) { return NGX_ERROR; } size *= scale; return size; }
['char *\nngx_conf_set_bufs_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)\n{\n char *p = conf;\n ngx_str_t *value;\n ngx_bufs_t *bufs;\n bufs = (ngx_bufs_t *) (p + cmd->offset);\n if (bufs->num) {\n return "is duplicate";\n }\n value = cf->args->elts;\n bufs->num = ngx_atoi(value[1].data, value[1].len);\n if (bufs->num == NGX_ERROR || bufs->num == 0) {\n return "invalid value";\n }\n bufs->size = ngx_parse_size(&value[2]);\n if (bufs->size == (size_t) NGX_ERROR || bufs->size == 0) {\n return "invalid value";\n }\n return NGX_CONF_OK;\n}', "ssize_t\nngx_parse_size(ngx_str_t *line)\n{\n u_char unit;\n size_t len;\n ssize_t size, scale, max;\n len = line->len;\n unit = line->data[len - 1];\n switch (unit) {\n case 'K':\n case 'k':\n len--;\n max = NGX_MAX_SIZE_T_VALUE / 1024;\n scale = 1024;\n break;\n case 'M':\n case 'm':\n len--;\n max = NGX_MAX_SIZE_T_VALUE / (1024 * 1024);\n scale = 1024 * 1024;\n break;\n default:\n max = NGX_MAX_SIZE_T_VALUE;\n scale = 1;\n }\n size = ngx_atosz(line->data, len);\n if (size == NGX_ERROR || size > max) {\n return NGX_ERROR;\n }\n size *= scale;\n return size;\n}"]
2,396
0
https://github.com/openssl/openssl/blob/ee6b68ce4c67870f9323d2a380eb949f447c56ee/test/testutil/tests.c/#L303
static char *print_mem_maybe_null(const void *s, size_t n, char outbuf[MEM_BUFFER_SIZE]) { size_t i; const unsigned char *p = (const unsigned char *)s; char *out = outbuf; int pad = 2 * n >= MEM_BUFFER_SIZE; if (s == NULL) return strcpy(outbuf, "(NULL)"); if (pad) { if ((out = OPENSSL_malloc(2 * n + 1)) == NULL) { out = outbuf; n = (MEM_BUFFER_SIZE - 4) / 2; } else { pad = 0; } } for (i = 0; i < 2 * n; ) { const unsigned char c = *p++; out[i++] = "0123456789abcdef"[c >> 4]; out[i++] = "0123456789abcdef"[c & 15]; } if (pad) { out[i++] = '.'; out[i++] = '.'; out[i++] = '.'; } out[i] = '\0'; return out; }
['static int test_sha384_long(void)\n{\n unsigned char md[SHA384_DIGEST_LENGTH];\n if (!TEST_true(EVP_Digest("abcdefgh" "bcdefghi" "cdefghij" "defghijk"\n "efghijkl" "fghijklm" "ghijklmn" "hijklmno"\n "ijklmnop" "jklmnopq" "klmnopqr" "lmnopqrs"\n "mnopqrst" "nopqrstu", 112, md, NULL,\n EVP_sha384(), NULL)))\n return 0;\n return TEST_mem_eq(md, sizeof(md), app_d2, sizeof(app_d2));\n}', 'int test_mem_eq(const char *file, int line, const char *st1, const char *st2,\n const void *s1, size_t n1, const void *s2, size_t n2)\n{\n char b1[MEM_BUFFER_SIZE], b2[MEM_BUFFER_SIZE];\n if (s1 == NULL && s2 == NULL)\n return 1;\n if (n1 != n2 || s1 == NULL || s2 == NULL || memcmp(s1, s2, n1) != 0) {\n char *m1 = print_mem_maybe_null(s1, n1, b1);\n char *m2 = print_mem_maybe_null(s2, n2, b2);\n test_fail_message(NULL, file, line, "memory",\n "%s %s [%zu] == %s %s [%zu]",\n st1, m1, n1, st2, m2, n2);\n if (m1 != b1)\n OPENSSL_free(m1);\n if (m2 != b2)\n OPENSSL_free(m2);\n return 0;\n }\n return 1;\n}', 'static char *print_mem_maybe_null(const void *s, size_t n,\n char outbuf[MEM_BUFFER_SIZE])\n{\n size_t i;\n const unsigned char *p = (const unsigned char *)s;\n char *out = outbuf;\n int pad = 2 * n >= MEM_BUFFER_SIZE;\n if (s == NULL)\n return strcpy(outbuf, "(NULL)");\n if (pad) {\n if ((out = OPENSSL_malloc(2 * n + 1)) == NULL) {\n out = outbuf;\n n = (MEM_BUFFER_SIZE - 4) / 2;\n } else {\n pad = 0;\n }\n }\n for (i = 0; i < 2 * n; ) {\n const unsigned char c = *p++;\n out[i++] = "0123456789abcdef"[c >> 4];\n out[i++] = "0123456789abcdef"[c & 15];\n }\n if (pad) {\n out[i++] = \'.\';\n out[i++] = \'.\';\n out[i++] = \'.\';\n }\n out[i] = \'\\0\';\n return out;\n}']
2,397
0
https://github.com/libav/libav/blob/555000c7d5c1e13043a948ebc48d2939b0ba6536/libavformat/utils.c/#L2668
void avformat_close_input(AVFormatContext **ps) { AVFormatContext *s = *ps; AVIOContext *pb = s->pb; if ((s->iformat && s->iformat->flags & AVFMT_NOFILE) || (s->flags & AVFMT_FLAG_CUSTOM_IO)) pb = NULL; flush_packet_queue(s); if (s->iformat) { if (s->iformat->read_close) s->iformat->read_close(s); } avformat_free_context(s); *ps = NULL; avio_close(pb); }
['void avformat_close_input(AVFormatContext **ps)\n{\n AVFormatContext *s = *ps;\n AVIOContext *pb = s->pb;\n if ((s->iformat && s->iformat->flags & AVFMT_NOFILE) ||\n (s->flags & AVFMT_FLAG_CUSTOM_IO))\n pb = NULL;\n flush_packet_queue(s);\n if (s->iformat) {\n if (s->iformat->read_close)\n s->iformat->read_close(s);\n }\n avformat_free_context(s);\n *ps = NULL;\n avio_close(pb);\n}', 'int avio_close(AVIOContext *s)\n{\n URLContext *h;\n if (!s)\n return 0;\n avio_flush(s);\n h = s->opaque;\n av_freep(&s->buffer);\n av_free(s);\n return ffurl_close(h);\n}', 'void avio_flush(AVIOContext *s)\n{\n flush_buffer(s);\n s->must_flush = 0;\n}', 'static void flush_buffer(AVIOContext *s)\n{\n if (s->buf_ptr > s->buffer) {\n if (s->write_packet && !s->error) {\n int ret = s->write_packet(s->opaque, s->buffer,\n s->buf_ptr - s->buffer);\n if (ret < 0) {\n s->error = ret;\n }\n }\n if (s->update_checksum) {\n s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,\n s->buf_ptr - s->checksum_ptr);\n s->checksum_ptr = s->buffer;\n }\n s->pos += s->buf_ptr - s->buffer;\n }\n s->buf_ptr = s->buffer;\n}']
2,398
0
https://github.com/openssl/openssl/blob/6c2c3e9ba9146ef8c9b1fd2b660357b657706969/crypto/lhash/lhash.c/#L359
static void contract(LHASH *lh) { LHASH_NODE **n,*n1,*np; np=lh->b[lh->p+lh->pmax-1]; lh->b[lh->p+lh->pmax-1]=NULL; if (lh->p == 0) { n=(LHASH_NODE **)Realloc((char *)lh->b, (unsigned int)(sizeof(LHASH_NODE *)*lh->pmax)); if (n == NULL) { lh->error++; return; } lh->num_contract_reallocs++; lh->num_alloc_nodes/=2; lh->pmax/=2; lh->p=lh->pmax-1; lh->b=n; } else lh->p--; lh->num_nodes--; lh->num_contracts++; n1=lh->b[(int)lh->p]; if (n1 == NULL) lh->b[(int)lh->p]=np; else { while (n1->next != NULL) n1=n1->next; n1->next=np; } }
['int MAIN(int argc, char **argv)\n\t{\n\tint off=0;\n\tSSL *con=NULL,*con2=NULL;\n\tint s,k,width,state=0;\n\tchar *cbuf=NULL,*sbuf=NULL;\n\tint cbuf_len,cbuf_off;\n\tint sbuf_len,sbuf_off;\n\tfd_set readfds,writefds;\n\tshort port=PORT;\n\tint full_log=1;\n\tchar *host=SSL_HOST_NAME;\n\tchar *cert_file=NULL,*key_file=NULL;\n\tchar *CApath=NULL,*CAfile=NULL,*cipher=NULL;\n\tint reconnect=0,badop=0,verify=SSL_VERIFY_NONE,bugs=0;\n\tint crlf=0;\n\tint write_tty,read_tty,write_ssl,read_ssl,tty_on,ssl_pending;\n\tSSL_CTX *ctx=NULL;\n\tint ret=1,in_init=1,i,nbio_test=0;\n\tint prexit = 0;\n\tSSL_METHOD *meth=NULL;\n\tBIO *sbio;\n#ifdef WINDOWS\n\tstruct timeval tv;\n#endif\n#if !defined(NO_SSL2) && !defined(NO_SSL3)\n\tmeth=SSLv23_client_method();\n#elif !defined(NO_SSL3)\n\tmeth=SSLv3_client_method();\n#elif !defined(NO_SSL2)\n\tmeth=SSLv2_client_method();\n#endif\n\tapps_startup();\n\tc_Pause=0;\n\tc_quiet=0;\n\tc_debug=0;\n\tc_showcerts=0;\n\tif (bio_err == NULL)\n\t\tbio_err=BIO_new_fp(stderr,BIO_NOCLOSE);\n\tif (\t((cbuf=Malloc(BUFSIZZ)) == NULL) ||\n\t\t((sbuf=Malloc(BUFSIZZ)) == NULL))\n\t\t{\n\t\tBIO_printf(bio_err,"out of memory\\n");\n\t\tgoto end;\n\t\t}\n\tverify_depth=0;\n\tverify_error=X509_V_OK;\n#ifdef FIONBIO\n\tc_nbio=0;\n#endif\n\targc--;\n\targv++;\n\twhile (argc >= 1)\n\t\t{\n\t\tif\t(strcmp(*argv,"-host") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\thost= *(++argv);\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-port") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tport=atoi(*(++argv));\n\t\t\tif (port == 0) goto bad;\n\t\t\t}\n\t\telse if (strcmp(*argv,"-connect") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tif (!extract_host_port(*(++argv),&host,NULL,&port))\n\t\t\t\tgoto bad;\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-verify") == 0)\n\t\t\t{\n\t\t\tverify=SSL_VERIFY_PEER;\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tverify_depth=atoi(*(++argv));\n\t\t\tBIO_printf(bio_err,"verify depth is %d\\n",verify_depth);\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-cert") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tcert_file= *(++argv);\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-prexit") == 0)\n\t\t\tprexit=1;\n\t\telse if\t(strcmp(*argv,"-crlf") == 0)\n\t\t\tcrlf=1;\n\t\telse if\t(strcmp(*argv,"-quiet") == 0)\n\t\t\tc_quiet=1;\n\t\telse if\t(strcmp(*argv,"-pause") == 0)\n\t\t\tc_Pause=1;\n\t\telse if\t(strcmp(*argv,"-debug") == 0)\n\t\t\tc_debug=1;\n\t\telse if\t(strcmp(*argv,"-showcerts") == 0)\n\t\t\tc_showcerts=1;\n\t\telse if\t(strcmp(*argv,"-nbio_test") == 0)\n\t\t\tnbio_test=1;\n\t\telse if\t(strcmp(*argv,"-state") == 0)\n\t\t\tstate=1;\n#ifndef NO_SSL2\n\t\telse if\t(strcmp(*argv,"-ssl2") == 0)\n\t\t\tmeth=SSLv2_client_method();\n#endif\n#ifndef NO_SSL3\n\t\telse if\t(strcmp(*argv,"-ssl3") == 0)\n\t\t\tmeth=SSLv3_client_method();\n#endif\n#ifndef NO_TLS1\n\t\telse if\t(strcmp(*argv,"-tls1") == 0)\n\t\t\tmeth=TLSv1_client_method();\n#endif\n\t\telse if (strcmp(*argv,"-bugs") == 0)\n\t\t\tbugs=1;\n\t\telse if\t(strcmp(*argv,"-key") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tkey_file= *(++argv);\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-reconnect") == 0)\n\t\t\t{\n\t\t\treconnect=5;\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-CApath") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tCApath= *(++argv);\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-CAfile") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tCAfile= *(++argv);\n\t\t\t}\n\t\telse if (strcmp(*argv,"-no_tls1") == 0)\n\t\t\toff|=SSL_OP_NO_TLSv1;\n\t\telse if (strcmp(*argv,"-no_ssl3") == 0)\n\t\t\toff|=SSL_OP_NO_SSLv3;\n\t\telse if (strcmp(*argv,"-no_ssl2") == 0)\n\t\t\toff|=SSL_OP_NO_SSLv2;\n\t\telse if\t(strcmp(*argv,"-cipher") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tcipher= *(++argv);\n\t\t\t}\n#ifdef FIONBIO\n\t\telse if (strcmp(*argv,"-nbio") == 0)\n\t\t\t{ c_nbio=1; }\n#endif\n\t\telse\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"unknown option %s\\n",*argv);\n\t\t\tbadop=1;\n\t\t\tbreak;\n\t\t\t}\n\t\targc--;\n\t\targv++;\n\t\t}\n\tif (badop)\n\t\t{\nbad:\n\t\tsc_usage();\n\t\tgoto end;\n\t\t}\n\tapp_RAND_load_file(NULL, bio_err, 0);\n\tif (bio_c_out == NULL)\n\t\t{\n\t\tif (c_quiet)\n\t\t\t{\n\t\t\tbio_c_out=BIO_new(BIO_s_null());\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (bio_c_out == NULL)\n\t\t\t\tbio_c_out=BIO_new_fp(stdout,BIO_NOCLOSE);\n\t\t\t}\n\t\t}\n\tSSLeay_add_ssl_algorithms();\n\tSSL_load_error_strings();\n\tctx=SSL_CTX_new(meth);\n\tif (ctx == NULL)\n\t\t{\n\t\tERR_print_errors(bio_err);\n\t\tgoto end;\n\t\t}\n\tif (bugs)\n\t\tSSL_CTX_set_options(ctx,SSL_OP_ALL|off);\n\telse\n\t\tSSL_CTX_set_options(ctx,off);\n\tif (state) SSL_CTX_set_info_callback(ctx,apps_ssl_info_callback);\n\tif (cipher != NULL)\n\t\tif(!SSL_CTX_set_cipher_list(ctx,cipher)) {\n\t\tBIO_printf(bio_err,"error seting cipher list\\n");\n\t\tERR_print_errors(bio_err);\n\t\tgoto end;\n\t}\n#if 0\n\telse\n\t\tSSL_CTX_set_cipher_list(ctx,getenv("SSL_CIPHER"));\n#endif\n\tSSL_CTX_set_verify(ctx,verify,verify_callback);\n\tif (!set_cert_stuff(ctx,cert_file,key_file))\n\t\tgoto end;\n\tif ((!SSL_CTX_load_verify_locations(ctx,CAfile,CApath)) ||\n\t\t(!SSL_CTX_set_default_verify_paths(ctx)))\n\t\t{\n\t\tERR_print_errors(bio_err);\n\t\t}\n\tcon=(SSL *)SSL_new(ctx);\nre_start:\n\tif (init_client(&s,host,port) == 0)\n\t\t{\n\t\tBIO_printf(bio_err,"connect:errno=%d\\n",get_last_socket_error());\n\t\tSHUTDOWN(s);\n\t\tgoto end;\n\t\t}\n\tBIO_printf(bio_c_out,"CONNECTED(%08X)\\n",s);\n#ifdef FIONBIO\n\tif (c_nbio)\n\t\t{\n\t\tunsigned long l=1;\n\t\tBIO_printf(bio_c_out,"turning on non blocking io\\n");\n\t\tif (BIO_socket_ioctl(s,FIONBIO,&l) < 0)\n\t\t\t{\n\t\t\tERR_print_errors(bio_err);\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n#endif\n\tif (c_Pause & 0x01) con->debug=1;\n\tsbio=BIO_new_socket(s,BIO_NOCLOSE);\n\tif (nbio_test)\n\t\t{\n\t\tBIO *test;\n\t\ttest=BIO_new(BIO_f_nbio_test());\n\t\tsbio=BIO_push(test,sbio);\n\t\t}\n\tif (c_debug)\n\t\t{\n\t\tcon->debug=1;\n\t\tBIO_set_callback(sbio,bio_dump_cb);\n\t\tBIO_set_callback_arg(sbio,bio_c_out);\n\t\t}\n\tSSL_set_bio(con,sbio,sbio);\n\tSSL_set_connect_state(con);\n\twidth=SSL_get_fd(con)+1;\n\tread_tty=1;\n\twrite_tty=0;\n\ttty_on=0;\n\tread_ssl=1;\n\twrite_ssl=1;\n\tcbuf_len=0;\n\tcbuf_off=0;\n\tsbuf_len=0;\n\tsbuf_off=0;\n\tfor (;;)\n\t\t{\n\t\tFD_ZERO(&readfds);\n\t\tFD_ZERO(&writefds);\n\t\tif (SSL_in_init(con) && !SSL_total_renegotiations(con))\n\t\t\t{\n\t\t\tin_init=1;\n\t\t\ttty_on=0;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\ttty_on=1;\n\t\t\tif (in_init)\n\t\t\t\t{\n\t\t\t\tin_init=0;\n\t\t\t\tprint_stuff(bio_c_out,con,full_log);\n\t\t\t\tif (full_log > 0) full_log--;\n\t\t\t\tif (reconnect)\n\t\t\t\t\t{\n\t\t\t\t\treconnect--;\n\t\t\t\t\tBIO_printf(bio_c_out,"drop connection and then reconnect\\n");\n\t\t\t\t\tSSL_shutdown(con);\n\t\t\t\t\tSSL_set_connect_state(con);\n\t\t\t\t\tSHUTDOWN(SSL_get_fd(con));\n\t\t\t\t\tgoto re_start;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\tssl_pending = read_ssl && SSL_pending(con);\n\t\tif (!ssl_pending)\n\t\t\t{\n#ifndef WINDOWS\n\t\t\tif (tty_on)\n\t\t\t\t{\n\t\t\t\tif (read_tty) FD_SET(fileno(stdin),&readfds);\n\t\t\t\tif (write_tty) FD_SET(fileno(stdout),&writefds);\n\t\t\t\t}\n\t\t\tif (read_ssl)\n\t\t\t\tFD_SET(SSL_get_fd(con),&readfds);\n\t\t\tif (write_ssl)\n\t\t\t\tFD_SET(SSL_get_fd(con),&writefds);\n#else\n\t\t\tif(!tty_on || !write_tty) {\n\t\t\t\tif (read_ssl)\n\t\t\t\t\tFD_SET(SSL_get_fd(con),&readfds);\n\t\t\t\tif (write_ssl)\n\t\t\t\t\tFD_SET(SSL_get_fd(con),&writefds);\n\t\t\t}\n#endif\n#ifdef WINDOWS\n\t\t\tif(!write_tty) {\n\t\t\t\tif(read_tty) {\n\t\t\t\t\ttv.tv_sec = 1;\n\t\t\t\t\ttv.tv_usec = 0;\n\t\t\t\t\ti=select(width,(void *)&readfds,(void *)&writefds,\n\t\t\t\t\t\t NULL,&tv);\n\t\t\t\t\tif(!i && (!_kbhit() || !read_tty) ) continue;\n\t\t\t\t} else \ti=select(width,(void *)&readfds,(void *)&writefds,\n\t\t\t\t\t NULL,NULL);\n\t\t\t}\n#else\n\t\t\ti=select(width,(void *)&readfds,(void *)&writefds,\n\t\t\t\t NULL,NULL);\n#endif\n\t\t\tif ( i < 0)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err,"bad select %d\\n",\n\t\t\t\tget_last_socket_error());\n\t\t\t\tgoto shut;\n\t\t\t\t}\n\t\t\t}\n\t\tif (!ssl_pending && FD_ISSET(SSL_get_fd(con),&writefds))\n\t\t\t{\n\t\t\tk=SSL_write(con,&(cbuf[cbuf_off]),\n\t\t\t\t(unsigned int)cbuf_len);\n\t\t\tswitch (SSL_get_error(con,k))\n\t\t\t\t{\n\t\t\tcase SSL_ERROR_NONE:\n\t\t\t\tcbuf_off+=k;\n\t\t\t\tcbuf_len-=k;\n\t\t\t\tif (k <= 0) goto end;\n\t\t\t\tif (cbuf_len <= 0)\n\t\t\t\t\t{\n\t\t\t\t\tread_tty=1;\n\t\t\t\t\twrite_ssl=0;\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tread_tty=0;\n\t\t\t\t\twrite_ssl=1;\n\t\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase SSL_ERROR_WANT_WRITE:\n\t\t\t\tBIO_printf(bio_c_out,"write W BLOCK\\n");\n\t\t\t\twrite_ssl=1;\n\t\t\t\tread_tty=0;\n\t\t\t\tbreak;\n\t\t\tcase SSL_ERROR_WANT_READ:\n\t\t\t\tBIO_printf(bio_c_out,"write R BLOCK\\n");\n\t\t\t\twrite_tty=0;\n\t\t\t\tread_ssl=1;\n\t\t\t\twrite_ssl=0;\n\t\t\t\tbreak;\n\t\t\tcase SSL_ERROR_WANT_X509_LOOKUP:\n\t\t\t\tBIO_printf(bio_c_out,"write X BLOCK\\n");\n\t\t\t\tbreak;\n\t\t\tcase SSL_ERROR_ZERO_RETURN:\n\t\t\t\tif (cbuf_len != 0)\n\t\t\t\t\t{\n\t\t\t\t\tBIO_printf(bio_c_out,"shutdown\\n");\n\t\t\t\t\tgoto shut;\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tread_tty=1;\n\t\t\t\t\twrite_ssl=0;\n\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\tcase SSL_ERROR_SYSCALL:\n\t\t\t\tif ((k != 0) || (cbuf_len != 0))\n\t\t\t\t\t{\n\t\t\t\t\tBIO_printf(bio_err,"write:errno=%d\\n",\n\t\t\t\t\t\tget_last_socket_error());\n\t\t\t\t\tgoto shut;\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tread_tty=1;\n\t\t\t\t\twrite_ssl=0;\n\t\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase SSL_ERROR_SSL:\n\t\t\t\tERR_print_errors(bio_err);\n\t\t\t\tgoto shut;\n\t\t\t\t}\n\t\t\t}\n#ifdef WINDOWS\n\t\telse if (!ssl_pending && write_tty)\n#else\n\t\telse if (!ssl_pending && FD_ISSET(fileno(stdout),&writefds))\n#endif\n\t\t\t{\n#ifdef CHARSET_EBCDIC\n\t\t\tascii2ebcdic(&(sbuf[sbuf_off]),&(sbuf[sbuf_off]),sbuf_len);\n#endif\n\t\t\ti=write(fileno(stdout),&(sbuf[sbuf_off]),sbuf_len);\n\t\t\tif (i <= 0)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_c_out,"DONE\\n");\n\t\t\t\tgoto shut;\n\t\t\t\t}\n\t\t\tsbuf_len-=i;;\n\t\t\tsbuf_off+=i;\n\t\t\tif (sbuf_len <= 0)\n\t\t\t\t{\n\t\t\t\tread_ssl=1;\n\t\t\t\twrite_tty=0;\n\t\t\t\t}\n\t\t\t}\n\t\telse if (ssl_pending || FD_ISSET(SSL_get_fd(con),&readfds))\n\t\t\t{\n#ifdef RENEG\n{ static int iiii; if (++iiii == 52) { SSL_renegotiate(con); iiii=0; } }\n#endif\n#if 1\n\t\t\tk=SSL_read(con,sbuf,1024 );\n#else\n\t\t\tk=SSL_read(con,sbuf,16);\n{ char zbuf[10240];\nprintf("read=%d pending=%d peek=%d\\n",k,SSL_pending(con),SSL_peek(con,zbuf,10240));\n}\n#endif\n\t\t\tswitch (SSL_get_error(con,k))\n\t\t\t\t{\n\t\t\tcase SSL_ERROR_NONE:\n\t\t\t\tif (k <= 0)\n\t\t\t\t\tgoto end;\n\t\t\t\tsbuf_off=0;\n\t\t\t\tsbuf_len=k;\n\t\t\t\tread_ssl=0;\n\t\t\t\twrite_tty=1;\n\t\t\t\tbreak;\n\t\t\tcase SSL_ERROR_WANT_WRITE:\n\t\t\t\tBIO_printf(bio_c_out,"read W BLOCK\\n");\n\t\t\t\twrite_ssl=1;\n\t\t\t\tread_tty=0;\n\t\t\t\tbreak;\n\t\t\tcase SSL_ERROR_WANT_READ:\n\t\t\t\tBIO_printf(bio_c_out,"read R BLOCK\\n");\n\t\t\t\twrite_tty=0;\n\t\t\t\tread_ssl=1;\n\t\t\t\tif ((read_tty == 0) && (write_ssl == 0))\n\t\t\t\t\twrite_ssl=1;\n\t\t\t\tbreak;\n\t\t\tcase SSL_ERROR_WANT_X509_LOOKUP:\n\t\t\t\tBIO_printf(bio_c_out,"read X BLOCK\\n");\n\t\t\t\tbreak;\n\t\t\tcase SSL_ERROR_SYSCALL:\n\t\t\t\tBIO_printf(bio_err,"read:errno=%d\\n",get_last_socket_error());\n\t\t\t\tgoto shut;\n\t\t\tcase SSL_ERROR_ZERO_RETURN:\n\t\t\t\tBIO_printf(bio_c_out,"closed\\n");\n\t\t\t\tgoto shut;\n\t\t\tcase SSL_ERROR_SSL:\n\t\t\t\tERR_print_errors(bio_err);\n\t\t\t\tgoto shut;\n\t\t\t\t}\n\t\t\t}\n#ifdef WINDOWS\n\t\telse if (_kbhit())\n#else\n\t\telse if (FD_ISSET(fileno(stdin),&readfds))\n#endif\n\t\t\t{\n\t\t\tif (crlf)\n\t\t\t\t{\n\t\t\t\tint j, lf_num;\n\t\t\t\ti=read(fileno(stdin),cbuf,BUFSIZZ/2);\n\t\t\t\tlf_num = 0;\n\t\t\t\tfor (j = 0; j < i; j++)\n\t\t\t\t\tif (cbuf[j] == \'\\n\')\n\t\t\t\t\t\tlf_num++;\n\t\t\t\tfor (j = i-1; j >= 0; j--)\n\t\t\t\t\t{\n\t\t\t\t\tcbuf[j+lf_num] = cbuf[j];\n\t\t\t\t\tif (cbuf[j] == \'\\n\')\n\t\t\t\t\t\t{\n\t\t\t\t\t\tlf_num--;\n\t\t\t\t\t\ti++;\n\t\t\t\t\t\tcbuf[j+lf_num] = \'\\r\';\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\tassert(lf_num == 0);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\ti=read(fileno(stdin),cbuf,BUFSIZZ);\n\t\t\tif ((!c_quiet) && ((i <= 0) || (cbuf[0] == \'Q\')))\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err,"DONE\\n");\n\t\t\t\tgoto shut;\n\t\t\t\t}\n\t\t\tif ((!c_quiet) && (cbuf[0] == \'R\'))\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err,"RENEGOTIATING\\n");\n\t\t\t\tSSL_renegotiate(con);\n\t\t\t\tcbuf_len=0;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tcbuf_len=i;\n\t\t\t\tcbuf_off=0;\n#ifdef CHARSET_EBCDIC\n\t\t\t\tebcdic2ascii(cbuf, cbuf, i);\n#endif\n\t\t\t\t}\n\t\t\twrite_ssl=1;\n\t\t\tread_tty=0;\n\t\t\t}\n\t\t}\nshut:\n\tSSL_shutdown(con);\n\tSHUTDOWN(SSL_get_fd(con));\n\tret=0;\nend:\n\tif(prexit) print_stuff(bio_c_out,con,1);\n\tif (con != NULL) SSL_free(con);\n\tif (con2 != NULL) SSL_free(con2);\n\tif (ctx != NULL) SSL_CTX_free(ctx);\n\tif (cbuf != NULL) { memset(cbuf,0,BUFSIZZ); Free(cbuf); }\n\tif (sbuf != NULL) { memset(sbuf,0,BUFSIZZ); Free(sbuf); }\n\tif (bio_c_out != NULL)\n\t\t{\n\t\tBIO_free(bio_c_out);\n\t\tbio_c_out=NULL;\n\t\t}\n\tEXIT(ret);\n\t}', 'SSL *SSL_new(SSL_CTX *ctx)\n\t{\n\tSSL *s;\n\tif (ctx == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_NEW,SSL_R_NULL_SSL_CTX);\n\t\treturn(NULL);\n\t\t}\n\tif (ctx->method == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_NEW,SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);\n\t\treturn(NULL);\n\t\t}\n\ts=(SSL *)Malloc(sizeof(SSL));\n\tif (s == NULL) goto err;\n\tmemset(s,0,sizeof(SSL));\n\tif (ctx->cert != NULL)\n\t\t{\n\t\ts->cert = ssl_cert_dup(ctx->cert);\n\t\tif (s->cert == NULL)\n\t\t\tgoto err;\n\t\t}\n\telse\n\t\ts->cert=NULL;\n\ts->sid_ctx_length=ctx->sid_ctx_length;\n\tmemcpy(&s->sid_ctx,&ctx->sid_ctx,sizeof(s->sid_ctx));\n\ts->verify_mode=ctx->verify_mode;\n\ts->verify_depth=ctx->verify_depth;\n\ts->verify_callback=ctx->default_verify_callback;\n\ts->purpose = ctx->purpose;\n\ts->trust = ctx->trust;\n\tCRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);\n\ts->ctx=ctx;\n\ts->verify_result=X509_V_OK;\n\ts->method=ctx->method;\n\tif (!s->method->ssl_new(s))\n\t\tgoto err;\n\ts->quiet_shutdown=ctx->quiet_shutdown;\n\ts->references=1;\n\ts->server=(ctx->method->ssl_accept == ssl_undefined_function)?0:1;\n\ts->options=ctx->options;\n\ts->mode=ctx->mode;\n\tSSL_clear(s);\n\tCRYPTO_new_ex_data(ssl_meth,s,&s->ex_data);\n\treturn(s);\nerr:\n\tif (s != NULL)\n\t\t{\n\t\tif (s->cert != NULL)\n\t\t\tssl_cert_free(s->cert);\n\t\tif (s->ctx != NULL)\n\t\t\tSSL_CTX_free(s->ctx);\n\t\tFree(s);\n\t\t}\n\tSSLerr(SSL_F_SSL_NEW,ERR_R_MALLOC_FAILURE);\n\treturn(NULL);\n\t}', 'void SSL_free(SSL *s)\n\t{\n\tint i;\n\tif(s == NULL)\n\t return;\n\ti=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL);\n#ifdef REF_PRINT\n\tREF_PRINT("SSL",s);\n#endif\n\tif (i > 0) return;\n#ifdef REF_CHECK\n\tif (i < 0)\n\t\t{\n\t\tfprintf(stderr,"SSL_free, bad reference count\\n");\n\t\tabort();\n\t\t}\n#endif\n\tCRYPTO_free_ex_data(ssl_meth,(char *)s,&s->ex_data);\n\tif (s->bbio != NULL)\n\t\t{\n\t\tif (s->bbio == s->wbio)\n\t\t\t{\n\t\t\ts->wbio=BIO_pop(s->wbio);\n\t\t\t}\n\t\tBIO_free(s->bbio);\n\t\ts->bbio=NULL;\n\t\t}\n\tif (s->rbio != NULL)\n\t\tBIO_free_all(s->rbio);\n\tif ((s->wbio != NULL) && (s->wbio != s->rbio))\n\t\tBIO_free_all(s->wbio);\n\tif (s->init_buf != NULL) BUF_MEM_free(s->init_buf);\n\tif (s->cipher_list != NULL) sk_SSL_CIPHER_free(s->cipher_list);\n\tif (s->cipher_list_by_id != NULL) sk_SSL_CIPHER_free(s->cipher_list_by_id);\n\tif (s->session != NULL)\n\t\t{\n\t\tssl_clear_bad_session(s);\n\t\tSSL_SESSION_free(s->session);\n\t\t}\n\tssl_clear_cipher_ctx(s);\n\tif (s->cert != NULL) ssl_cert_free(s->cert);\n\tif (s->ctx) SSL_CTX_free(s->ctx);\n\tif (s->client_CA != NULL)\n\t\tsk_X509_NAME_pop_free(s->client_CA,X509_NAME_free);\n\tif (s->method != NULL) s->method->ssl_free(s);\n\tFree((char *)s);\n\t}', 'int ssl_clear_bad_session(SSL *s)\n\t{\n\tif (\t(s->session != NULL) &&\n\t\t!(s->shutdown & SSL_SENT_SHUTDOWN) &&\n\t\t!(SSL_in_init(s) || SSL_in_before(s)))\n\t\t{\n\t\tSSL_CTX_remove_session(s->ctx,s->session);\n\t\treturn(1);\n\t\t}\n\telse\n\t\treturn(0);\n\t}', 'int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)\n{\n\treturn remove_session_lock(ctx, c, 1);\n}', 'static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)\n\t{\n\tSSL_SESSION *r;\n\tint ret=0;\n\tif ((c != NULL) && (c->session_id_length != 0))\n\t\t{\n\t\tif(lck) CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);\n\t\tr=(SSL_SESSION *)lh_delete(ctx->sessions,(char *)c);\n\t\tif (r != NULL)\n\t\t\t{\n\t\t\tret=1;\n\t\t\tSSL_SESSION_list_remove(ctx,c);\n\t\t\t}\n\t\tif(lck) CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n\t\tif (ret)\n\t\t\t{\n\t\t\tr->not_resumable=1;\n\t\t\tif (ctx->remove_session_cb != NULL)\n\t\t\t\tctx->remove_session_cb(ctx,r);\n\t\t\tSSL_SESSION_free(r);\n\t\t\t}\n\t\t}\n\telse\n\t\tret=0;\n\treturn(ret);\n\t}', 'char *lh_delete(LHASH *lh, char *data)\n\t{\n\tunsigned long hash;\n\tLHASH_NODE *nn,**rn;\n\tchar *ret;\n\tlh->error=0;\n\trn=getrn(lh,data,&hash);\n\tif (*rn == NULL)\n\t\t{\n\t\tlh->num_no_delete++;\n\t\treturn(NULL);\n\t\t}\n\telse\n\t\t{\n\t\tnn= *rn;\n\t\t*rn=nn->next;\n\t\tret=nn->data;\n\t\tFree((char *)nn);\n\t\tlh->num_delete++;\n\t\t}\n\tlh->num_items--;\n\tif ((lh->num_nodes > MIN_NODES) &&\n\t\t(lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes)))\n\t\tcontract(lh);\n\treturn(ret);\n\t}', 'static void contract(LHASH *lh)\n\t{\n\tLHASH_NODE **n,*n1,*np;\n\tnp=lh->b[lh->p+lh->pmax-1];\n\tlh->b[lh->p+lh->pmax-1]=NULL;\n\tif (lh->p == 0)\n\t\t{\n\t\tn=(LHASH_NODE **)Realloc((char *)lh->b,\n\t\t\t(unsigned int)(sizeof(LHASH_NODE *)*lh->pmax));\n\t\tif (n == NULL)\n\t\t\t{\n\t\t\tlh->error++;\n\t\t\treturn;\n\t\t\t}\n\t\tlh->num_contract_reallocs++;\n\t\tlh->num_alloc_nodes/=2;\n\t\tlh->pmax/=2;\n\t\tlh->p=lh->pmax-1;\n\t\tlh->b=n;\n\t\t}\n\telse\n\t\tlh->p--;\n\tlh->num_nodes--;\n\tlh->num_contracts++;\n\tn1=lh->b[(int)lh->p];\n\tif (n1 == NULL)\n\t\tlh->b[(int)lh->p]=np;\n\telse\n\t\t{\n\t\twhile (n1->next != NULL)\n\t\t\tn1=n1->next;\n\t\tn1->next=np;\n\t\t}\n\t}']
2,399
0
https://github.com/openssl/openssl/blob/d8028b202bfe337200a0cc89b80983ea1838cb30/crypto/lhash/lhash.c/#L123
void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data) { unsigned long hash; OPENSSL_LH_NODE *nn, **rn; void *ret; lh->error = 0; rn = getrn(lh, data, &hash); if (*rn == NULL) { lh->num_no_delete++; return (NULL); } else { nn = *rn; *rn = nn->next; ret = nn->data; OPENSSL_free(nn); lh->num_delete++; } lh->num_items--; if ((lh->num_nodes > MIN_NODES) && (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes))) contract(lh); return (ret); }
['static int dtls_listen_test(int i)\n{\n SSL_CTX *ctx = NULL;\n SSL *ssl = NULL;\n BIO *outbio = NULL;\n BIO *inbio = NULL;\n BIO_ADDR *peer = NULL;\n tests *tp = &testpackets[i];\n char *data;\n long datalen;\n int ret, success = 0;\n if (!TEST_ptr(ctx = SSL_CTX_new(DTLS_server_method()))\n || !TEST_ptr(peer = BIO_ADDR_new()))\n goto err;\n SSL_CTX_set_cookie_generate_cb(ctx, cookie_gen);\n SSL_CTX_set_cookie_verify_cb(ctx, cookie_verify);\n if (!TEST_ptr(ssl = SSL_new(ctx))\n || !TEST_ptr(outbio = BIO_new(BIO_s_mem())))\n goto err;\n SSL_set0_wbio(ssl, outbio);\n if (!TEST_ptr(inbio = BIO_new_mem_buf((char *)tp->in, tp->inlen)))\n goto err;\n BIO_set_mem_eof_return(inbio, -1);\n SSL_set0_rbio(ssl, inbio);\n if (!TEST_int_ge(ret = DTLSv1_listen(ssl, peer), 0))\n goto err;\n datalen = BIO_get_mem_data(outbio, &data);\n if (tp->outtype == VERIFY) {\n if (!TEST_int_eq(ret, 0)\n || !TEST_mem_eq(data, datalen, verify, sizeof(verify)))\n goto err;\n } else if (datalen == 0) {\n if (!TEST_true((ret == 0 && tp->outtype == DROP)\n || (ret == 1 && tp->outtype == GOOD)))\n goto err;\n } else {\n TEST_info("Test %d: unexpected data output", i);\n goto err;\n }\n (void)BIO_reset(outbio);\n inbio = NULL;\n SSL_set0_rbio(ssl, NULL);\n success = 1;\n err:\n SSL_free(ssl);\n SSL_CTX_free(ctx);\n BIO_free(inbio);\n OPENSSL_free(peer);\n return success;\n}', 'SSL *SSL_new(SSL_CTX *ctx)\n{\n SSL *s;\n if (ctx == NULL) {\n SSLerr(SSL_F_SSL_NEW, SSL_R_NULL_SSL_CTX);\n return (NULL);\n }\n if (ctx->method == NULL) {\n SSLerr(SSL_F_SSL_NEW, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);\n return (NULL);\n }\n s = OPENSSL_zalloc(sizeof(*s));\n if (s == NULL)\n goto err;\n s->lock = CRYPTO_THREAD_lock_new();\n if (s->lock == NULL) {\n SSLerr(SSL_F_SSL_NEW, ERR_R_MALLOC_FAILURE);\n OPENSSL_free(s);\n return NULL;\n }\n RECORD_LAYER_init(&s->rlayer, s);\n s->options = ctx->options;\n s->dane.flags = ctx->dane.flags;\n s->min_proto_version = ctx->min_proto_version;\n s->max_proto_version = ctx->max_proto_version;\n s->mode = ctx->mode;\n s->max_cert_list = ctx->max_cert_list;\n s->references = 1;\n s->max_early_data = ctx->max_early_data;\n s->cert = ssl_cert_dup(ctx->cert);\n if (s->cert == NULL)\n goto err;\n RECORD_LAYER_set_read_ahead(&s->rlayer, ctx->read_ahead);\n s->msg_callback = ctx->msg_callback;\n s->msg_callback_arg = ctx->msg_callback_arg;\n s->verify_mode = ctx->verify_mode;\n s->not_resumable_session_cb = ctx->not_resumable_session_cb;\n s->record_padding_cb = ctx->record_padding_cb;\n s->record_padding_arg = ctx->record_padding_arg;\n s->block_padding = ctx->block_padding;\n s->sid_ctx_length = ctx->sid_ctx_length;\n OPENSSL_assert(s->sid_ctx_length <= sizeof s->sid_ctx);\n memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));\n s->verify_callback = ctx->default_verify_callback;\n s->generate_session_id = ctx->generate_session_id;\n s->param = X509_VERIFY_PARAM_new();\n if (s->param == NULL)\n goto err;\n X509_VERIFY_PARAM_inherit(s->param, ctx->param);\n s->quiet_shutdown = ctx->quiet_shutdown;\n s->max_send_fragment = ctx->max_send_fragment;\n s->split_send_fragment = ctx->split_send_fragment;\n s->max_pipelines = ctx->max_pipelines;\n if (s->max_pipelines > 1)\n RECORD_LAYER_set_read_ahead(&s->rlayer, 1);\n if (ctx->default_read_buf_len > 0)\n SSL_set_default_read_buffer_len(s, ctx->default_read_buf_len);\n SSL_CTX_up_ref(ctx);\n s->ctx = ctx;\n s->ext.debug_cb = 0;\n s->ext.debug_arg = NULL;\n s->ext.ticket_expected = 0;\n s->ext.status_type = ctx->ext.status_type;\n s->ext.status_expected = 0;\n s->ext.ocsp.ids = NULL;\n s->ext.ocsp.exts = NULL;\n s->ext.ocsp.resp = NULL;\n s->ext.ocsp.resp_len = 0;\n SSL_CTX_up_ref(ctx);\n s->session_ctx = ctx;\n#ifndef OPENSSL_NO_EC\n if (ctx->ext.ecpointformats) {\n s->ext.ecpointformats =\n OPENSSL_memdup(ctx->ext.ecpointformats,\n ctx->ext.ecpointformats_len);\n if (!s->ext.ecpointformats)\n goto err;\n s->ext.ecpointformats_len =\n ctx->ext.ecpointformats_len;\n }\n if (ctx->ext.supportedgroups) {\n s->ext.supportedgroups =\n OPENSSL_memdup(ctx->ext.supportedgroups,\n ctx->ext.supportedgroups_len);\n if (!s->ext.supportedgroups)\n goto err;\n s->ext.supportedgroups_len = ctx->ext.supportedgroups_len;\n }\n#endif\n#ifndef OPENSSL_NO_NEXTPROTONEG\n s->ext.npn = NULL;\n#endif\n if (s->ctx->ext.alpn) {\n s->ext.alpn = OPENSSL_malloc(s->ctx->ext.alpn_len);\n if (s->ext.alpn == NULL)\n goto err;\n memcpy(s->ext.alpn, s->ctx->ext.alpn, s->ctx->ext.alpn_len);\n s->ext.alpn_len = s->ctx->ext.alpn_len;\n }\n s->verified_chain = NULL;\n s->verify_result = X509_V_OK;\n s->default_passwd_callback = ctx->default_passwd_callback;\n s->default_passwd_callback_userdata = ctx->default_passwd_callback_userdata;\n s->method = ctx->method;\n s->key_update = SSL_KEY_UPDATE_NONE;\n if (!s->method->ssl_new(s))\n goto err;\n s->server = (ctx->method->ssl_accept == ssl_undefined_function) ? 0 : 1;\n if (!SSL_clear(s))\n goto err;\n if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data))\n goto err;\n#ifndef OPENSSL_NO_PSK\n s->psk_client_callback = ctx->psk_client_callback;\n s->psk_server_callback = ctx->psk_server_callback;\n#endif\n s->job = NULL;\n#ifndef OPENSSL_NO_CT\n if (!SSL_set_ct_validation_callback(s, ctx->ct_validation_callback,\n ctx->ct_validation_callback_arg))\n goto err;\n#endif\n return s;\n err:\n SSL_free(s);\n SSLerr(SSL_F_SSL_NEW, ERR_R_MALLOC_FAILURE);\n return NULL;\n}', 'int DTLSv1_listen(SSL *s, BIO_ADDR *client)\n{\n int next, n, ret = 0, clearpkt = 0;\n unsigned char cookie[DTLS1_COOKIE_LENGTH];\n unsigned char seq[SEQ_NUM_SIZE];\n const unsigned char *data;\n unsigned char *buf;\n size_t fragoff, fraglen, msglen;\n unsigned int rectype, versmajor, msgseq, msgtype, clientvers, cookielen;\n BIO *rbio, *wbio;\n BUF_MEM *bufm;\n BIO_ADDR *tmpclient = NULL;\n PACKET pkt, msgpkt, msgpayload, session, cookiepkt;\n if (s->handshake_func == NULL) {\n SSL_set_accept_state(s);\n }\n if (!SSL_clear(s))\n return -1;\n ERR_clear_error();\n rbio = SSL_get_rbio(s);\n wbio = SSL_get_wbio(s);\n if (!rbio || !wbio) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_BIO_NOT_SET);\n return -1;\n }\n BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_PEEK_MODE, 1, NULL);\n if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00)) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_UNSUPPORTED_SSL_VERSION);\n return -1;\n }\n if (s->init_buf == NULL) {\n if ((bufm = BUF_MEM_new()) == NULL) {\n SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_MALLOC_FAILURE);\n return -1;\n }\n if (!BUF_MEM_grow(bufm, SSL3_RT_MAX_PLAIN_LENGTH)) {\n BUF_MEM_free(bufm);\n SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_MALLOC_FAILURE);\n return -1;\n }\n s->init_buf = bufm;\n }\n buf = (unsigned char *)s->init_buf->data;\n do {\n clear_sys_error();\n n = BIO_read(rbio, buf, SSL3_RT_MAX_PLAIN_LENGTH);\n if (n <= 0) {\n if (BIO_should_retry(rbio)) {\n goto end;\n }\n return -1;\n }\n clearpkt = 1;\n if (!PACKET_buf_init(&pkt, buf, n)) {\n SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_INTERNAL_ERROR);\n return -1;\n }\n if (n < DTLS1_RT_HEADER_LENGTH) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_RECORD_TOO_SMALL);\n goto end;\n }\n if (s->msg_callback)\n s->msg_callback(0, 0, SSL3_RT_HEADER, buf,\n DTLS1_RT_HEADER_LENGTH, s, s->msg_callback_arg);\n if (!PACKET_get_1(&pkt, &rectype)\n || !PACKET_get_1(&pkt, &versmajor)) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH);\n goto end;\n }\n if (rectype != SSL3_RT_HANDSHAKE) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_UNEXPECTED_MESSAGE);\n goto end;\n }\n if (versmajor != DTLS1_VERSION_MAJOR) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_BAD_PROTOCOL_VERSION_NUMBER);\n goto end;\n }\n if (!PACKET_forward(&pkt, 1)\n || !PACKET_copy_bytes(&pkt, seq, SEQ_NUM_SIZE)\n || !PACKET_get_length_prefixed_2(&pkt, &msgpkt)) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH);\n goto end;\n }\n if (seq[0] != 0 || seq[1] != 0) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_UNEXPECTED_MESSAGE);\n goto end;\n }\n data = PACKET_data(&msgpkt);\n if (!PACKET_get_1(&msgpkt, &msgtype)\n || !PACKET_get_net_3_len(&msgpkt, &msglen)\n || !PACKET_get_net_2(&msgpkt, &msgseq)\n || !PACKET_get_net_3_len(&msgpkt, &fragoff)\n || !PACKET_get_net_3_len(&msgpkt, &fraglen)\n || !PACKET_get_sub_packet(&msgpkt, &msgpayload, fraglen)\n || PACKET_remaining(&msgpkt) != 0) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH);\n goto end;\n }\n if (msgtype != SSL3_MT_CLIENT_HELLO) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_UNEXPECTED_MESSAGE);\n goto end;\n }\n if (msgseq > 2) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_INVALID_SEQUENCE_NUMBER);\n goto end;\n }\n if (fragoff != 0 || fraglen > msglen) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_FRAGMENTED_CLIENT_HELLO);\n goto end;\n }\n if (s->msg_callback)\n s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, data,\n fraglen + DTLS1_HM_HEADER_LENGTH, s,\n s->msg_callback_arg);\n if (!PACKET_get_net_2(&msgpayload, &clientvers)) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH);\n goto end;\n }\n if (DTLS_VERSION_LT(clientvers, (unsigned int)s->method->version) &&\n s->method->version != DTLS_ANY_VERSION) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_WRONG_VERSION_NUMBER);\n goto end;\n }\n if (!PACKET_forward(&msgpayload, SSL3_RANDOM_SIZE)\n || !PACKET_get_length_prefixed_1(&msgpayload, &session)\n || !PACKET_get_length_prefixed_1(&msgpayload, &cookiepkt)) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH);\n goto end;\n }\n if (PACKET_remaining(&cookiepkt) == 0) {\n next = LISTEN_SEND_VERIFY_REQUEST;\n } else {\n if (s->ctx->app_verify_cookie_cb == NULL) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_NO_VERIFY_COOKIE_CALLBACK);\n return -1;\n }\n if (s->ctx->app_verify_cookie_cb(s, PACKET_data(&cookiepkt),\n (unsigned int)PACKET_remaining(&cookiepkt)) == 0) {\n next = LISTEN_SEND_VERIFY_REQUEST;\n } else {\n next = LISTEN_SUCCESS;\n }\n }\n if (next == LISTEN_SEND_VERIFY_REQUEST) {\n WPACKET wpkt;\n unsigned int version;\n size_t wreclen;\n BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_PEEK_MODE, 0, NULL);\n BIO_read(rbio, buf, SSL3_RT_MAX_PLAIN_LENGTH);\n BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_PEEK_MODE, 1, NULL);\n if (s->ctx->app_gen_cookie_cb == NULL ||\n s->ctx->app_gen_cookie_cb(s, cookie, &cookielen) == 0 ||\n cookielen > 255) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_COOKIE_GEN_CALLBACK_FAILURE);\n return -1;\n }\n version = (s->method->version == DTLS_ANY_VERSION) ? DTLS1_VERSION\n : s->version;\n if (!WPACKET_init(&wpkt, s->init_buf)\n || !WPACKET_put_bytes_u8(&wpkt, SSL3_RT_HANDSHAKE)\n || !WPACKET_put_bytes_u16(&wpkt, version)\n || !WPACKET_memcpy(&wpkt, seq, SEQ_NUM_SIZE)\n || !WPACKET_start_sub_packet_u16(&wpkt)\n || !WPACKET_put_bytes_u8(&wpkt,\n DTLS1_MT_HELLO_VERIFY_REQUEST)\n || !WPACKET_put_bytes_u24(&wpkt, 0)\n || !WPACKET_put_bytes_u16(&wpkt, 0)\n || !WPACKET_put_bytes_u24(&wpkt, 0)\n || !WPACKET_start_sub_packet_u24(&wpkt)\n || !dtls_raw_hello_verify_request(&wpkt, cookie, cookielen)\n || !WPACKET_close(&wpkt)\n || !WPACKET_close(&wpkt)\n || !WPACKET_get_total_written(&wpkt, &wreclen)\n || !WPACKET_finish(&wpkt)) {\n SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_INTERNAL_ERROR);\n WPACKET_cleanup(&wpkt);\n return -1;\n }\n memcpy(&buf[DTLS1_RT_HEADER_LENGTH + 1],\n &buf[DTLS1_RT_HEADER_LENGTH + DTLS1_HM_HEADER_LENGTH - 3],\n 3);\n if (s->msg_callback)\n s->msg_callback(1, 0, SSL3_RT_HEADER, buf,\n DTLS1_RT_HEADER_LENGTH, s, s->msg_callback_arg);\n if ((tmpclient = BIO_ADDR_new()) == NULL) {\n SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_MALLOC_FAILURE);\n goto end;\n }\n if (BIO_dgram_get_peer(rbio, tmpclient) > 0) {\n (void)BIO_dgram_set_peer(wbio, tmpclient);\n }\n BIO_ADDR_free(tmpclient);\n tmpclient = NULL;\n if (BIO_write(wbio, buf, wreclen) < (int)wreclen) {\n if (BIO_should_retry(wbio)) {\n goto end;\n }\n return -1;\n }\n if (BIO_flush(wbio) <= 0) {\n if (BIO_should_retry(wbio)) {\n goto end;\n }\n return -1;\n }\n }\n } while (next != LISTEN_SUCCESS);\n s->d1->handshake_read_seq = 1;\n s->d1->handshake_write_seq = 1;\n s->d1->next_handshake_write_seq = 1;\n DTLS_RECORD_LAYER_set_write_sequence(&s->rlayer, seq);\n SSL_set_options(s, SSL_OP_COOKIE_EXCHANGE);\n ossl_statem_set_hello_verify_done(s);\n if (BIO_dgram_get_peer(rbio, client) <= 0)\n BIO_ADDR_clear(client);\n ret = 1;\n clearpkt = 0;\n end:\n BIO_ADDR_free(tmpclient);\n BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_PEEK_MODE, 0, NULL);\n if (clearpkt) {\n BIO_read(rbio, buf, SSL3_RT_MAX_PLAIN_LENGTH);\n }\n return ret;\n}', 'int SSL_clear(SSL *s)\n{\n if (s->method == NULL) {\n SSLerr(SSL_F_SSL_CLEAR, SSL_R_NO_METHOD_SPECIFIED);\n return (0);\n }\n if (ssl_clear_bad_session(s)) {\n SSL_SESSION_free(s->session);\n s->session = NULL;\n }\n s->error = 0;\n s->hit = 0;\n s->shutdown = 0;\n if (s->renegotiate) {\n SSLerr(SSL_F_SSL_CLEAR, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n ossl_statem_clear(s);\n s->version = s->method->version;\n s->client_version = s->version;\n s->rwstate = SSL_NOTHING;\n BUF_MEM_free(s->init_buf);\n s->init_buf = NULL;\n clear_ciphers(s);\n s->first_packet = 0;\n s->key_update = SSL_KEY_UPDATE_NONE;\n s->dane.mdpth = -1;\n s->dane.pdpth = -1;\n X509_free(s->dane.mcert);\n s->dane.mcert = NULL;\n s->dane.mtlsa = NULL;\n X509_VERIFY_PARAM_move_peername(s->param, NULL);\n if (!ossl_statem_get_in_handshake(s) && (s->session == NULL)\n && (s->method != s->ctx->method)) {\n s->method->ssl_free(s);\n s->method = s->ctx->method;\n if (!s->method->ssl_new(s))\n return (0);\n } else\n s->method->ssl_clear(s);\n RECORD_LAYER_clear(&s->rlayer);\n return (1);\n}', 'int ssl_clear_bad_session(SSL *s)\n{\n if ((s->session != NULL) &&\n !(s->shutdown & SSL_SENT_SHUTDOWN) &&\n !(SSL_in_init(s) || SSL_in_before(s))) {\n SSL_CTX_remove_session(s->session_ctx, s->session);\n return (1);\n } else\n return (0);\n}', 'int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)\n{\n return remove_session_lock(ctx, c, 1);\n}', 'static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)\n{\n SSL_SESSION *r;\n int ret = 0;\n if ((c != NULL) && (c->session_id_length != 0)) {\n if (lck)\n CRYPTO_THREAD_write_lock(ctx->lock);\n if ((r = lh_SSL_SESSION_retrieve(ctx->sessions, c)) == c) {\n ret = 1;\n r = lh_SSL_SESSION_delete(ctx->sessions, c);\n SSL_SESSION_list_remove(ctx, c);\n }\n c->not_resumable = 1;\n if (lck)\n CRYPTO_THREAD_unlock(ctx->lock);\n if (ret)\n SSL_SESSION_free(r);\n if (ctx->remove_session_cb != NULL)\n ctx->remove_session_cb(ctx, c);\n } else\n ret = 0;\n return (ret);\n}', 'DEFINE_LHASH_OF(SSL_SESSION)', 'void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)\n{\n unsigned long hash;\n OPENSSL_LH_NODE *nn, **rn;\n void *ret;\n lh->error = 0;\n rn = getrn(lh, data, &hash);\n if (*rn == NULL) {\n lh->num_no_delete++;\n return (NULL);\n } else {\n nn = *rn;\n *rn = nn->next;\n ret = nn->data;\n OPENSSL_free(nn);\n lh->num_delete++;\n }\n lh->num_items--;\n if ((lh->num_nodes > MIN_NODES) &&\n (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))\n contract(lh);\n return (ret);\n}']
2,400
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 mtu_test(SSL_CTX *ctx, const char *cs, int no_etm)\n{\n SSL *srvr_ssl = NULL, *clnt_ssl = NULL;\n BIO *sc_bio = NULL;\n int i;\n size_t s;\n size_t mtus[30];\n unsigned char buf[600];\n int rv = 0;\n memset(buf, 0x5a, sizeof(buf));\n if (!TEST_true(create_ssl_objects(ctx, ctx, &srvr_ssl, &clnt_ssl,\n NULL, NULL)))\n goto end;\n if (no_etm)\n SSL_set_options(srvr_ssl, SSL_OP_NO_ENCRYPT_THEN_MAC);\n if (!TEST_true(SSL_set_cipher_list(srvr_ssl, cs))\n || !TEST_true(SSL_set_cipher_list(clnt_ssl, cs))\n || !TEST_ptr(sc_bio = SSL_get_rbio(srvr_ssl))\n || !TEST_true(create_ssl_connection(clnt_ssl, srvr_ssl,\n SSL_ERROR_NONE)))\n goto end;\n if (debug)\n TEST_info("Channel established");\n for (i = 0; i < 30; i++) {\n SSL_set_mtu(clnt_ssl, 500 + i);\n mtus[i] = DTLS_get_data_mtu(clnt_ssl);\n if (debug)\n TEST_info("%s%s MTU for record mtu %d = %lu",\n cs, no_etm ? "-noEtM" : "",\n 500 + i, (unsigned long)mtus[i]);\n if (!TEST_size_t_ne(mtus[i], 0)) {\n TEST_info("Cipher %s MTU %d", cs, 500 + i);\n goto end;\n }\n }\n SSL_set_mtu(clnt_ssl, 1000);\n for (s = mtus[0]; s <= mtus[29]; s++) {\n size_t reclen;\n if (!TEST_int_eq(SSL_write(clnt_ssl, buf, s), (int)s))\n goto end;\n reclen = BIO_read(sc_bio, buf, sizeof(buf));\n if (debug)\n TEST_info("record %zu for payload %zu", reclen, s);\n for (i = 0; i < 30; i++) {\n if (!TEST_false(s <= mtus[i] && reclen > (size_t)(500 + i))) {\n TEST_error("%s: s=%lu, mtus[i]=%lu, reclen=%lu, i=%d",\n cs, (unsigned long)s, (unsigned long)mtus[i],\n (unsigned long)reclen, 500 + i);\n goto end;\n }\n if (!TEST_false(s > mtus[i] && reclen <= (size_t)(500 + i))) {\n TEST_error("%s: s=%lu, mtus[i]=%lu, reclen=%lu, i=%d",\n cs, (unsigned long)s, (unsigned long)mtus[i],\n (unsigned long)reclen, 500 + i);\n goto end;\n }\n }\n }\n rv = 1;\n if (SSL_READ_ETM(clnt_ssl))\n rv = 2;\n end:\n SSL_free(clnt_ssl);\n SSL_free(srvr_ssl);\n return rv;\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}']