static int opt_show_ ## section ( const char * opt , const char * arg ) {
 mark_section_show_entries ( SECTION_ID_ ## target_section_id , 1 , NULL ) ;
 return 0 ;
 }
 DEFINE_OPT_SHOW_SECTION ( chapters , CHAPTERS ) DEFINE_OPT_SHOW_SECTION ( error , ERROR ) DEFINE_OPT_SHOW_SECTION ( format , FORMAT ) DEFINE_OPT_SHOW_SECTION ( frames , FRAMES ) DEFINE_OPT_SHOW_SECTION ( library_versions , LIBRARY_VERSIONS ) DEFINE_OPT_SHOW_SECTION ( packets , PACKETS ) DEFINE_OPT_SHOW_SECTION ( pixel_formats , PIXEL_FORMATS ) DEFINE_OPT_SHOW_SECTION ( program_version , PROGRAM_VERSION )


void ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, int64_t *pts, int *duration) { int64_t out_pts = AV_NOPTS_VALUE; int removed_samples = 0; int i; if (afq->frame_count || afq->frame_alloc) { if (afq->frames->pts != AV_NOPTS_VALUE) out_pts = afq->frames->pts; } if(!afq->frame_count) av_log(afq->avctx, AV_LOG_WARNING, "Trying to remove %d samples, but que empty\n", nb_samples); if (pts) *pts = ff_samples_to_time_base(afq->avctx, out_pts); for(i=0; nb_samples && i<afq->frame_count; i++){ int n= FFMIN(afq->frames[i].duration, nb_samples); afq->frames[i].duration -= n; nb_samples -= n; removed_samples += n; if(afq->frames[i].pts != AV_NOPTS_VALUE) afq->frames[i].pts += n; } i -= i && afq->frames[i-1].duration; memmove(afq->frames, afq->frames + i, sizeof(*afq->frames) * (afq->frame_count - i)); afq->frame_count -= i; if(nb_samples){ av_assert0(!afq->frame_count); if(afq->frames[0].pts != AV_NOPTS_VALUE) afq->frames[0].pts += nb_samples; av_log(afq->avctx, AV_LOG_DEBUG, "Trying to remove %d more samples than are in the que\n", nb_samples); } if (duration) *duration = ff_samples_to_time_base(afq->avctx, removed_samples); }


static inline struct link_map * find_needed ( const char * name ) {
 struct r_scope_elem * scope = & GL ( dl_ns ) [ LM_ID_BASE ] . _ns_loaded -> l_searchlist ;
 unsigned int n = scope -> r_nlist ;
 while ( n -- > 0 ) if ( _dl_name_match_p ( name , scope -> r_list [ n ] ) ) return scope -> r_list [ n ] ;
 return NULL ;
 }


static int raw_decode ( AVCodecContext * avctx , void * data , int * got_frame , AVPacket * avpkt ) {
 const AVPixFmtDescriptor * desc = av_pix_fmt_desc_get ( avctx -> pix_fmt ) ;
 RawVideoContext * context = avctx -> priv_data ;
 const uint8_t * buf = avpkt -> data ;
 int buf_size = avpkt -> size ;
 int need_copy = ! avpkt -> buf || context -> is_2_4_bpp || context -> is_yuv2 ;
 int res ;
 AVFrame * frame = data ;
 AVPicture * picture = data ;
 frame -> pict_type = AV_PICTURE_TYPE_I ;
 frame -> key_frame = 1 ;
 frame -> reordered_opaque = avctx -> reordered_opaque ;
 frame -> pkt_pts = avctx -> pkt -> pts ;
 if ( buf_size < context -> frame_size - ( avctx -> pix_fmt == AV_PIX_FMT_PAL8 ? AVPALETTE_SIZE : 0 ) ) return - 1 ;
 if ( need_copy ) frame -> buf [ 0 ] = av_buffer_alloc ( context -> frame_size ) ;
 else frame -> buf [ 0 ] = av_buffer_ref ( avpkt -> buf ) ;
 if ( ! frame -> buf [ 0 ] ) return AVERROR ( ENOMEM ) ;
 if ( context -> is_2_4_bpp ) {
 int i ;
 uint8_t * dst = frame -> buf [ 0 ] -> data ;
 buf_size = context -> frame_size - AVPALETTE_SIZE ;
 if ( avctx -> bits_per_coded_sample == 4 ) {
 for ( i = 0 ;
 2 * i + 1 < buf_size ;
 i ++ ) {
 dst [ 2 * i + 0 ] = buf [ i ] >> 4 ;
 dst [ 2 * i + 1 ] = buf [ i ] & 15 ;
 }
 }
 else {
 for ( i = 0 ;
 4 * i + 3 < buf_size ;
 i ++ ) {
 dst [ 4 * i + 0 ] = buf [ i ] >> 6 ;
 dst [ 4 * i + 1 ] = buf [ i ] >> 4 & 3 ;
 dst [ 4 * i + 2 ] = buf [ i ] >> 2 & 3 ;
 dst [ 4 * i + 3 ] = buf [ i ] & 3 ;
 }
 }
 buf = dst ;
 }
 else if ( need_copy ) {
 memcpy ( frame -> buf [ 0 ] -> data , buf , FFMIN ( buf_size , context -> frame_size ) ) ;
 buf = frame -> buf [ 0 ] -> data ;
 }
 if ( avctx -> codec_tag == MKTAG ( 'A' , 'V' , '1' , 'x' ) || avctx -> codec_tag == MKTAG ( 'A' , 'V' , 'u' , 'p' ) ) buf += buf_size - context -> frame_size ;
 if ( ( res = avpicture_fill ( picture , buf , avctx -> pix_fmt , avctx -> width , avctx -> height ) ) < 0 ) return res ;
 if ( avctx -> pix_fmt == AV_PIX_FMT_PAL8 ) {
 const uint8_t * pal = av_packet_get_side_data ( avpkt , AV_PKT_DATA_PALETTE , NULL ) ;
 if ( pal ) {
 av_buffer_unref ( & context -> palette ) ;
 context -> palette = av_buffer_alloc ( AVPALETTE_SIZE ) ;
 if ( ! context -> palette ) return AVERROR ( ENOMEM ) ;
 memcpy ( context -> palette -> data , pal , AVPALETTE_SIZE ) ;
 frame -> palette_has_changed = 1 ;
 }
 }
 if ( ( avctx -> pix_fmt == AV_PIX_FMT_PAL8 && buf_size < context -> frame_size ) || ( desc -> flags & PIX_FMT_PSEUDOPAL ) ) {
 frame -> buf [ 1 ] = av_buffer_ref ( context -> palette ) ;
 if ( ! frame -> buf [ 1 ] ) return AVERROR ( ENOMEM ) ;
 frame -> data [ 1 ] = frame -> buf [ 1 ] -> data ;
 }
 if ( avctx -> pix_fmt == AV_PIX_FMT_BGR24 && ( ( frame -> linesize [ 0 ] + 3 ) & ~ 3 ) * avctx -> height <= buf_size ) frame -> linesize [ 0 ] = ( frame -> linesize [ 0 ] + 3 ) & ~ 3 ;
 if ( context -> flip ) flip ( avctx , picture ) ;
 if ( avctx -> codec_tag == MKTAG ( 'Y' , 'V' , '1' , '2' ) || avctx -> codec_tag == MKTAG ( 'Y' , 'V' , '1' , '6' ) || avctx -> codec_tag == MKTAG ( 'Y' , 'V' , '2' , '4' ) || avctx -> codec_tag == MKTAG ( 'Y' , 'V' , 'U' , '9' ) ) FFSWAP ( uint8_t * , picture -> data [ 1 ] , picture -> data [ 2 ] ) ;
 if ( avctx -> codec_tag == AV_RL32 ( "yuv2" ) && avctx -> pix_fmt == AV_PIX_FMT_YUYV422 ) {
 int x , y ;
 uint8_t * line = picture -> data [ 0 ] ;
 for ( y = 0 ;
 y < avctx -> height ;
 y ++ ) {
 for ( x = 0 ;
 x < avctx -> width ;
 x ++ ) line [ 2 * x + 1 ] ^= 0x80 ;
 line += picture -> linesize [ 0 ] ;
 }
 }
 * got_frame = 1 ;
 return buf_size ;
 }


static void iothread_set_poll_max_ns(Object *obj, Visitor *v, const char *name, void *opaque, Error **errp) { IOThread *iothread = IOTHREAD(obj); Error *local_err = NULL; int64_t value; visit_type_int64(v, name, &value, &local_err); if (local_err) { goto out; } if (value < 0) { error_setg(&local_err, "poll_max_ns value must be in range " "[0, %"PRId64"]", INT64_MAX); goto out; } iothread->poll_max_ns = value; if (iothread->ctx) { aio_context_set_poll_params(iothread->ctx, value, &local_err); } out: error_propagate(errp, local_err); }


int use_env()
{
    int indent;
    size_t flags = 0;
    json_t *json;
    json_error_t error;

    #ifdef _WIN32
    /* On Windows, set stdout and stderr to binary mode to avoid
       outputting DOS line terminators */
    _setmode(_fileno(stdout), _O_BINARY);
    _setmode(_fileno(stderr), _O_BINARY);
    #endif

    indent = getenv_int("JSON_INDENT");
    if(indent < 0 || indent > 255) {
        fprintf(stderr, "invalid value for JSON_INDENT: %d\n", indent);
        return 2;
    }

    if(indent > 0)
        flags |= JSON_INDENT(indent);

    if(getenv_int("JSON_COMPACT") > 0)
        flags |= JSON_COMPACT;

    if(getenv_int("JSON_ENSURE_ASCII"))
        flags |= JSON_ENSURE_ASCII;

    if(getenv_int("JSON_PRESERVE_ORDER"))
        flags |= JSON_PRESERVE_ORDER;

    if(getenv_int("JSON_SORT_KEYS"))
         flags |= JSON_SORT_KEYS;

    if(getenv_int("STRIP")) {
        /* Load to memory, strip leading and trailing whitespace */
        size_t size = 0, used = 0;
        char *buffer = NULL;

        while(1) {
            size_t count;

            size = (size == 0 ? 128 : size * 2);
            buffer = realloc(buffer, size);
            if(!buffer) {
                fprintf(stderr, "Unable to allocate %d bytes\n", (int)size);
                return 1;
            }

            count = fread(buffer + used, 1, size - used, stdin);
            if(count < size - used) {
                buffer[used + count] = '\0';
                break;
            }
            used += count;
        }

        json = json_loads(strip(buffer), 0, &error);
        free(buffer);
    }
    else
        json = json_loadf(stdin, 0, &error);

    if(!json) {
        fprintf(stderr, "%d %d %d\n%s\n",
            error.line, error.column,
            error.position, error.text);
        return 1;
    }

    json_dumpf(json, stdout, flags);
    json_decref(json);

    return 0;
}


static void dissect_rtmpt_common ( tvbuff_t * tvb , packet_info * pinfo , proto_tree * tree , rtmpt_conv_t * rconv , int cdir , guint32 seq , guint32 lastackseq ) {
 int offset = 0 ;
 int remain ;
 int want ;
 guint8 header_type ;
 int basic_hlen ;
 int message_hlen ;
 guint32 id ;
 guint32 ts = 0 ;
 guint32 tsd = 0 ;
 int body_len ;
 guint8 cmd ;
 guint32 src ;
 int chunk_size ;
 rtmpt_frag_t * tf ;
 rtmpt_id_t * ti ;
 rtmpt_packet_t * tp ;
 tvbuff_t * pktbuf ;
 remain = tvb_reported_length ( tvb ) ;
 if ( ! remain ) return ;
 RTMPT_DEBUG ( "Segment: cdir=%d seq=%d-%d\n" , cdir , seq , seq + remain - 1 ) ;
 if ( pinfo -> fd -> flags . visited ) {
 wmem_stack_t * packets ;
 packets = wmem_stack_new ( wmem_packet_scope ( ) ) ;
 wmem_stack_push ( packets , 0 ) ;
 tp = ( rtmpt_packet_t * ) wmem_tree_lookup32_le ( rconv -> packets [ cdir ] , seq + remain - 1 ) ;
 while ( tp && tp -> lastseq >= seq ) {
 wmem_stack_push ( packets , tp ) ;
 tp = ( rtmpt_packet_t * ) wmem_tree_lookup32_le ( rconv -> packets [ cdir ] , tp -> lastseq - 1 ) ;
 }
 while ( ( tp = ( rtmpt_packet_t * ) wmem_stack_pop ( packets ) ) != NULL ) {
 if ( tp -> resident ) {
 pktbuf = tvb_new_child_real_data ( tvb , tp -> data . p , tp -> have , tp -> have ) ;
 add_new_data_source ( pinfo , pktbuf , "Unchunked RTMP" ) ;
 }
 else {
 pktbuf = tvb_new_subset_length ( tvb , tp -> data . offset , tp -> have ) ;
 }
 dissect_rtmpt ( pktbuf , pinfo , tree , rconv , cdir , tp ) ;
 }
 return ;
 }
 while ( remain > 0 ) {
 tf = NULL ;
 ti = NULL ;
 tp = NULL ;
 if ( offset == 0 ) {
 tf = ( rtmpt_frag_t * ) wmem_tree_lookup32_le ( rconv -> frags [ cdir ] , seq + offset - 1 ) ;
 if ( tf ) {
 RTMPT_DEBUG ( " tf seq=%d lseq=%d h=%d l=%d\n" , tf -> seq , tf -> lastseq , tf -> have , tf -> len ) ;
 if ( tf -> have >= tf -> len || seq + offset < tf -> seq || seq + offset > tf -> lastseq + tf -> len - tf -> have ) {
 tf = NULL ;
 }
 else if ( ! tf -> ishdr ) {
 ti = ( rtmpt_id_t * ) wmem_tree_lookup32 ( rconv -> ids [ cdir ] , tf -> saved . id ) ;
 if ( ti ) {
 tp = ( rtmpt_packet_t * ) wmem_tree_lookup32_le ( ti -> packets , seq + offset - 1 ) ;
 }
 if ( tp && tp -> chunkwant ) {
 goto unchunk ;
 }
 tf = NULL ;
 ti = NULL ;
 tp = NULL ;
 }
 if ( tf ) {
 want = tf -> len - tf -> have ;
 if ( remain < want ) want = remain ;
 tvb_memcpy ( tvb , tf -> saved . d + tf -> have , offset , want ) ;
 id = tf -> saved . d [ 0 ] ;
 header_type = ( id >> 6 ) & 3 ;
 basic_hlen = rtmpt_basic_header_length ( id ) ;
 if ( ( header_type < 3 ) && ( tf -> have < ( basic_hlen + 3 ) ) && ( tf -> have + want >= ( basic_hlen + 3 ) ) ) {
 if ( pntoh24 ( tf -> saved . d + basic_hlen ) == 0xffffff ) {
 tf -> len += 4 ;
 }
 }
 tf -> have += want ;
 tf -> lastseq = seq + want - 1 ;
 remain -= want ;
 offset += want ;
 if ( tf -> have < tf -> len ) {
 return ;
 }
 }
 }
 }
 if ( ! tf ) {
 id = tvb_get_guint8 ( tvb , offset ) ;
 if ( id == RTMPT_MAGIC && seq + offset == RTMPT_HANDSHAKE_OFFSET_1 ) {
 header_type = 4 ;
 basic_hlen = 1 ;
 message_hlen = 0 ;
 id = lastackseq == 1 ? RTMPT_TYPE_HANDSHAKE_1 : RTMPT_TYPE_HANDSHAKE_2 ;
 }
 else if ( seq + offset == RTMPT_HANDSHAKE_OFFSET_2 ) {
 header_type = 4 ;
 basic_hlen = 0 ;
 message_hlen = 0 ;
 id = RTMPT_TYPE_HANDSHAKE_3 ;
 }
 else {
 header_type = ( id >> 6 ) & 3 ;
 basic_hlen = rtmpt_basic_header_length ( id ) ;
 message_hlen = rtmpt_message_header_length ( id ) ;
 if ( ( header_type < 3 ) && ( remain >= ( basic_hlen + 3 ) ) ) {
 if ( tvb_get_ntoh24 ( tvb , offset + basic_hlen ) == 0xffffff ) {
 message_hlen += 4 ;
 }
 }
 if ( remain < ( basic_hlen + message_hlen ) ) {
 tf = wmem_new ( wmem_file_scope ( ) , rtmpt_frag_t ) ;
 tf -> ishdr = 1 ;
 tf -> seq = seq + offset ;
 tf -> lastseq = tf -> seq + remain - 1 ;
 tf -> len = basic_hlen + message_hlen ;
 tvb_memcpy ( tvb , tf -> saved . d , offset , remain ) ;
 tf -> have = remain ;
 wmem_tree_insert32 ( rconv -> frags [ cdir ] , seq + offset , tf ) ;
 return ;
 }
 id = id & 0x3f ;
 if ( id == 0 ) id = tvb_get_guint8 ( tvb , offset + 1 ) + 64 ;
 else if ( id == 1 ) id = tvb_get_letohs ( tvb , offset + 1 ) + 64 ;
 }
 }
 else {
 id = tf -> saved . d [ 0 ] ;
 header_type = ( id >> 6 ) & 3 ;
 basic_hlen = rtmpt_basic_header_length ( id ) ;
 message_hlen = tf -> len - basic_hlen ;
 id = id & 0x3f ;
 if ( id == 0 ) id = tf -> saved . d [ 1 ] + 64 ;
 else if ( id == 1 ) id = pletoh16 ( tf -> saved . d + 1 ) + 64 ;
 }
 if ( id <= RTMPT_ID_MAX ) ti = ( rtmpt_id_t * ) wmem_tree_lookup32 ( rconv -> ids [ cdir ] , id ) ;
 if ( ti ) tp = ( rtmpt_packet_t * ) wmem_tree_lookup32_le ( ti -> packets , seq + offset - 1 ) ;
 if ( header_type == 0 ) src = tf ? pntoh32 ( tf -> saved . d + basic_hlen + 7 ) : tvb_get_ntohl ( tvb , offset + basic_hlen + 7 ) ;
 else if ( ti ) src = ti -> src ;
 else src = 0 ;
 if ( header_type < 2 ) cmd = tf ? tf -> saved . d [ basic_hlen + 6 ] : tvb_get_guint8 ( tvb , offset + basic_hlen + 6 ) ;
 else if ( ti ) cmd = ti -> cmd ;
 else cmd = 0 ;
 if ( id > RTMPT_ID_MAX ) {
 if ( id == RTMPT_TYPE_HANDSHAKE_1 ) chunk_size = body_len = 1536 ;
 else if ( id == RTMPT_TYPE_HANDSHAKE_2 ) chunk_size = body_len = 3072 ;
 else chunk_size = body_len = 1536 ;
 }
 else {
 chunk_size = GPOINTER_TO_INT ( wmem_tree_lookup32_le ( rconv -> chunksize [ cdir ] , seq + offset - 1 ) ) ;
 if ( ! chunk_size ) chunk_size = RTMPT_DEFAULT_CHUNK_SIZE ;
 if ( header_type < 2 ) body_len = tf ? pntoh24 ( tf -> saved . d + basic_hlen + 3 ) : tvb_get_ntoh24 ( tvb , offset + basic_hlen + 3 ) ;
 else if ( ti ) body_len = ti -> len ;
 else body_len = chunk_size ;
 if ( body_len > ( gint ) rtmpt_max_packet_size ) {
 return ;
 }
 }
 if ( ! ti || ! tp || header_type < 3 || tp -> have == tp -> want || tp -> chunkhave != tp -> chunkwant ) {
 RTMPT_DEBUG ( "New packet cdir=%d seq=%d ti=%p tp=%p header_type=%d header_len=%d id=%d tph=%d tpw=%d len=%d cs=%d\n" , cdir , seq + offset , ti , tp , header_type , basic_hlen + message_hlen , id , tp ? tp -> have : 0 , tp ? tp -> want : 0 , body_len , chunk_size ) ;
 if ( ! ti ) {
 ti = wmem_new ( wmem_file_scope ( ) , rtmpt_id_t ) ;
 ti -> packets = wmem_tree_new ( wmem_file_scope ( ) ) ;
 ti -> ts = 0 ;
 ti -> tsd = 0 ;
 wmem_tree_insert32 ( rconv -> ids [ cdir ] , id , ti ) ;
 }
 if ( header_type == 0 ) {
 ts = tf ? pntoh24 ( tf -> saved . d + basic_hlen ) : tvb_get_ntoh24 ( tvb , offset + basic_hlen ) ;
 if ( ts == 0xffffff ) {
 ts = tf ? pntoh32 ( tf -> saved . d + basic_hlen + 11 ) : tvb_get_ntohl ( tvb , offset + basic_hlen + 11 ) ;
 }
 tsd = ts - ti -> ts ;
 }
 else if ( header_type < 3 ) {
 tsd = tf ? pntoh24 ( tf -> saved . d + basic_hlen ) : tvb_get_ntoh24 ( tvb , offset + basic_hlen ) ;
 if ( tsd == 0xffffff ) {
 ts = tf ? pntoh32 ( tf -> saved . d + basic_hlen + message_hlen - 4 ) : tvb_get_ntohl ( tvb , offset + basic_hlen + message_hlen - 4 ) ;
 tsd = ti -> tsd ;
 }
 else {
 ts = ti -> ts + tsd ;
 }
 }
 else {
 ts = ti -> ts + ti -> tsd ;
 tsd = ti -> tsd ;
 }
 tp = wmem_new ( wmem_file_scope ( ) , rtmpt_packet_t ) ;
 tp -> seq = tp -> lastseq = tf ? tf -> seq : seq + offset ;
 tp -> have = 0 ;
 tp -> want = basic_hlen + message_hlen + body_len ;
 tp -> chunkwant = 0 ;
 tp -> chunkhave = 0 ;
 tp -> bhlen = basic_hlen ;
 tp -> mhlen = message_hlen ;
 tp -> fmt = header_type ;
 tp -> id = id ;
 tp -> ts = ts ;
 tp -> len = body_len ;
 if ( id > RTMPT_ID_MAX ) tp -> cmd = id ;
 else tp -> cmd = cmd & 0x7f ;
 tp -> src = src ;
 tp -> txid = 0 ;
 tp -> isresponse = FALSE ;
 tp -> otherframe = 0 ;
 ti -> ts = ts ;
 ti -> tsd = tsd ;
 ti -> len = body_len ;
 ti -> cmd = cmd ;
 ti -> src = src ;
 wmem_tree_insert32 ( ti -> packets , tp -> seq , tp ) ;
 if ( ! tf && body_len <= chunk_size && tp -> want <= remain ) {
 tp -> resident = FALSE ;
 tp -> data . offset = offset ;
 tp -> lastseq = seq + offset + tp -> want - 1 ;
 tp -> have = tp -> want ;
 wmem_tree_insert32 ( rconv -> packets [ cdir ] , tp -> lastseq , tp ) ;
 pktbuf = tvb_new_subset_length ( tvb , tp -> data . offset , tp -> have ) ;
 dissect_rtmpt ( pktbuf , pinfo , tree , rconv , cdir , tp ) ;
 offset += tp -> want ;
 remain -= tp -> want ;
 continue ;
 }
 else {
 tp -> resident = TRUE ;
 tp -> data . p = ( guint8 * ) wmem_alloc ( wmem_file_scope ( ) , tp -> bhlen + tp -> mhlen + tp -> len ) ;
 if ( tf && tf -> ishdr ) {
 memcpy ( tp -> data . p , tf -> saved . d , tf -> len ) ;
 }
 else {
 tvb_memcpy ( tvb , tp -> data . p , offset , basic_hlen + message_hlen ) ;
 offset += basic_hlen + message_hlen ;
 remain -= basic_hlen + message_hlen ;
 }
 tp -> lastseq = seq + offset - 1 ;
 tp -> have = basic_hlen + message_hlen ;
 if ( tp -> have == tp -> want ) {
 wmem_tree_insert32 ( rconv -> packets [ cdir ] , tp -> lastseq , tp ) ;
 pktbuf = tvb_new_child_real_data ( tvb , tp -> data . p , tp -> have , tp -> have ) ;
 add_new_data_source ( pinfo , pktbuf , "Unchunked RTMP" ) ;
 dissect_rtmpt ( pktbuf , pinfo , tree , rconv , cdir , tp ) ;
 continue ;
 }
 tp -> chunkwant = chunk_size ;
 if ( tp -> chunkwant > tp -> want - tp -> have ) tp -> chunkwant = tp -> want - tp -> have ;
 }
 }
 else {
 RTMPT_DEBUG ( "Old packet cdir=%d seq=%d ti=%p tp=%p header_len=%d id=%d tph=%d tpw=%d len=%d cs=%d\n" , cdir , seq + offset , ti , tp , basic_hlen + message_hlen , id , tp ? tp -> have : 0 , tp ? tp -> want : 0 , body_len , chunk_size ) ;
 tp -> chunkwant = chunk_size ;
 if ( tp -> chunkwant > tp -> want - tp -> have ) tp -> chunkwant = tp -> want - tp -> have ;
 offset += basic_hlen + message_hlen ;
 remain -= basic_hlen + message_hlen ;
 }
 tf = NULL ;
 unchunk : want = tp -> chunkwant - tp -> chunkhave ;
 if ( want > remain ) want = remain ;
 RTMPT_DEBUG ( " cw=%d ch=%d r=%d w=%d\n" , tp -> chunkwant , tp -> chunkhave , remain , want ) ;
 tvb_memcpy ( tvb , tp -> data . p + tp -> have , offset , want ) ;
 if ( tf ) {
 tf -> have += want ;
 tf -> lastseq = seq + offset + want - 1 ;
 }
 tp -> lastseq = seq + offset + want - 1 ;
 tp -> have += want ;
 tp -> chunkhave += want ;
 offset += want ;
 remain -= want ;
 if ( tp -> chunkhave == tp -> chunkwant ) {
 tp -> chunkhave = 0 ;
 tp -> chunkwant = 0 ;
 }
 if ( tp -> have == tp -> want ) {
 wmem_tree_insert32 ( rconv -> packets [ cdir ] , tp -> lastseq , tp ) ;
 pktbuf = tvb_new_child_real_data ( tvb , tp -> data . p , tp -> have , tp -> have ) ;
 add_new_data_source ( pinfo , pktbuf , "Unchunked RTMP" ) ;
 dissect_rtmpt ( pktbuf , pinfo , tree , rconv , cdir , tp ) ;
 }
 else if ( tp -> chunkhave < tp -> chunkwant ) {
 rtmpt_frag_t * tf2 = wmem_new ( wmem_file_scope ( ) , rtmpt_frag_t ) ;
 tf2 -> ishdr = 0 ;
 tf2 -> seq = seq + offset - want ;
 tf2 -> lastseq = tf2 -> seq + remain - 1 + want ;
 tf2 -> have = tp -> chunkhave ;
 tf2 -> len = tp -> chunkwant ;
 tf2 -> saved . id = tp -> id ;
 RTMPT_DEBUG ( " inserting tf @ %d\n" , seq + offset - want - 1 ) ;
 wmem_tree_insert32 ( rconv -> frags [ cdir ] , seq + offset - want - 1 , tf2 ) ;
 }
 }
 }


static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
		int closing, int tx_ring)
{
	struct pgv *pg_vec = NULL;
	struct packet_sock *po = pkt_sk(sk);
	int was_running, order = 0;
	struct packet_ring_buffer *rb;
	struct sk_buff_head *rb_queue;
	__be16 num;
	int err = -EINVAL;
	/* Added to avoid minimal code churn */
	struct tpacket_req *req = &req_u->req;

	lock_sock(sk);

	rb = tx_ring ? &po->tx_ring : &po->rx_ring;
	rb_queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;

	err = -EBUSY;
	if (!closing) {
		if (atomic_read(&po->mapped))
			goto out;
		if (packet_read_pending(rb))
			goto out;
	}

	if (req->tp_block_nr) {
		/* Sanity tests and some calculations */
		err = -EBUSY;
		if (unlikely(rb->pg_vec))
			goto out;

		switch (po->tp_version) {
		case TPACKET_V1:
			po->tp_hdrlen = TPACKET_HDRLEN;
			break;
		case TPACKET_V2:
			po->tp_hdrlen = TPACKET2_HDRLEN;
			break;
		case TPACKET_V3:
			po->tp_hdrlen = TPACKET3_HDRLEN;
			break;
		}

		err = -EINVAL;
		if (unlikely((int)req->tp_block_size <= 0))
			goto out;
		if (unlikely(!PAGE_ALIGNED(req->tp_block_size)))
			goto out;
		if (po->tp_version >= TPACKET_V3 &&
		    req->tp_block_size <=
			  BLK_PLUS_PRIV((u64)req_u->req3.tp_sizeof_priv))
			goto out;
		if (unlikely(req->tp_frame_size < po->tp_hdrlen +
					po->tp_reserve))
			goto out;
		if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1)))
			goto out;

		rb->frames_per_block = req->tp_block_size / req->tp_frame_size;
		if (unlikely(rb->frames_per_block == 0))
			goto out;
		if (unlikely((rb->frames_per_block * req->tp_block_nr) !=
					req->tp_frame_nr))
			goto out;

		err = -ENOMEM;
		order = get_order(req->tp_block_size);
		pg_vec = alloc_pg_vec(req, order);
		if (unlikely(!pg_vec))
			goto out;
		switch (po->tp_version) {
		case TPACKET_V3:
			/* Block transmit is not supported yet */
			if (!tx_ring) {
				init_prb_bdqc(po, rb, pg_vec, req_u);
			} else {
				struct tpacket_req3 *req3 = &req_u->req3;

				if (req3->tp_retire_blk_tov ||
				    req3->tp_sizeof_priv ||
				    req3->tp_feature_req_word) {
					err = -EINVAL;
					goto out;
				}
			}
			break;
		default:
			break;
		}
	}
	/* Done */
	else {
		err = -EINVAL;
		if (unlikely(req->tp_frame_nr))
			goto out;
	}

	/* Detach socket from network */
	spin_lock(&po->bind_lock);
	was_running = po->running;
	num = po->num;
	if (was_running) {
		po->num = 0;
		__unregister_prot_hook(sk, false);
	}
	spin_unlock(&po->bind_lock);

	synchronize_net();

	err = -EBUSY;
	mutex_lock(&po->pg_vec_lock);
	if (closing || atomic_read(&po->mapped) == 0) {
		err = 0;
		spin_lock_bh(&rb_queue->lock);
		swap(rb->pg_vec, pg_vec);
		rb->frame_max = (req->tp_frame_nr - 1);
		rb->head = 0;
		rb->frame_size = req->tp_frame_size;
		spin_unlock_bh(&rb_queue->lock);

		swap(rb->pg_vec_order, order);
		swap(rb->pg_vec_len, req->tp_block_nr);

		rb->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
		po->prot_hook.func = (po->rx_ring.pg_vec) ?
						tpacket_rcv : packet_rcv;
		skb_queue_purge(rb_queue);
		if (atomic_read(&po->mapped))
			pr_err("packet_mmap: vma is busy: %d\n",
			       atomic_read(&po->mapped));
	}
	mutex_unlock(&po->pg_vec_lock);

	spin_lock(&po->bind_lock);
	if (was_running) {
		po->num = num;
		register_prot_hook(sk);
	}
	spin_unlock(&po->bind_lock);
	if (closing && (po->tp_version > TPACKET_V2)) {
		/* Because we don't support block-based V3 on tx-ring */
		if (!tx_ring)
			prb_shutdown_retire_blk_timer(po, rb_queue);
	}

	if (pg_vec)
		free_pg_vec(pg_vec, order, req->tp_block_nr);
out:
	release_sock(sk);
	return err;
}


static void my_free(void *ptr)
{
    free_called += 1;
    free(ptr);
}


static void my_free(void *ptr)
{
    free_called = 1;
    free(ptr);
}


int ff_h263_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt) { const uint8_t *buf = avpkt->data; int buf_size = avpkt->size; MpegEncContext *s = avctx->priv_data; int ret; int slice_ret = 0; AVFrame *pict = data; s->flags = avctx->flags; s->flags2 = avctx->flags2; /* no supplementary picture */ if (buf_size == 0) { /* special case for last picture */ if (s->low_delay == 0 && s->next_picture_ptr) { if ((ret = av_frame_ref(pict, &s->next_picture_ptr->f)) < 0) return ret; s->next_picture_ptr = NULL; *got_frame = 1; } return 0; } if (s->flags & CODEC_FLAG_TRUNCATED) { int next; if (CONFIG_MPEG4_DECODER && s->codec_id == AV_CODEC_ID_MPEG4) { next = ff_mpeg4_find_frame_end(&s->parse_context, buf, buf_size); } else if (CONFIG_H263_DECODER && s->codec_id == AV_CODEC_ID_H263) { next = ff_h263_find_frame_end(&s->parse_context, buf, buf_size); } else if (CONFIG_H263P_DECODER && s->codec_id == AV_CODEC_ID_H263P) { next = ff_h263_find_frame_end(&s->parse_context, buf, buf_size); } else { av_log(s->avctx, AV_LOG_ERROR, "this codec does not support truncated bitstreams\n"); return AVERROR(ENOSYS); } if (ff_combine_frame(&s->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0) return buf_size; } retry: if (s->divx_packed && s->bitstream_buffer_size) { int i; for(i=0; i < buf_size-3; i++) { if (buf[i]==0 && buf[i+1]==0 && buf[i+2]==1) { if (buf[i+3]==0xB0) { av_log(s->avctx, AV_LOG_WARNING, "Discarding excessive bitstream in packed xvid\n"); s->bitstream_buffer_size = 0; } break; } } } if (s->bitstream_buffer_size && (s->divx_packed || buf_size < 20)) // divx 5.01+/xvid frame reorder ret = init_get_bits8(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size); else ret = init_get_bits8(&s->gb, buf, buf_size); s->bitstream_buffer_size = 0; if (ret < 0) return ret; if (!s->context_initialized) // we need the idct permutaton for reading a custom matrix if ((ret = ff_MPV_common_init(s)) < 0) return ret; /* We need to set current_picture_ptr before reading the header, * otherwise we cannot store anyting in there */ if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) { int i = ff_find_unused_picture(s, 0); if (i < 0) return i; s->current_picture_ptr = &s->picture[i]; } /* let's go :-) */ if (CONFIG_WMV2_DECODER && s->msmpeg4_version == 5) { ret = ff_wmv2_decode_picture_header(s); } else if (CONFIG_MSMPEG4_DECODER && s->msmpeg4_version) { ret = ff_msmpeg4_decode_picture_header(s); } else if (CONFIG_MPEG4_DECODER && avctx->codec_id == AV_CODEC_ID_MPEG4) { if (s->avctx->extradata_size && s->picture_number == 0) { GetBitContext gb; if (init_get_bits8(&gb, s->avctx->extradata, s->avctx->extradata_size) >= 0 ) ff_mpeg4_decode_picture_header(avctx->priv_data, &gb); } ret = ff_mpeg4_decode_picture_header(avctx->priv_data, &s->gb); } else if (CONFIG_H263I_DECODER && s->codec_id == AV_CODEC_ID_H263I) { ret = ff_intel_h263_decode_picture_header(s); } else if (CONFIG_FLV_DECODER && s->h263_flv) { ret = ff_flv_decode_picture_header(s); } else { ret = ff_h263_decode_picture_header(s); } if (ret < 0 || ret == FRAME_SKIPPED) { if ( s->width != avctx->coded_width || s->height != avctx->coded_height) { av_log(s->avctx, AV_LOG_WARNING, "Reverting picture dimensions change due to header decoding failure\n"); s->width = avctx->coded_width; s->height= avctx->coded_height; } } if (ret == FRAME_SKIPPED) return get_consumed_bytes(s, buf_size); /* skip if the header was thrashed */ if (ret < 0) { av_log(s->avctx, AV_LOG_ERROR, "header damaged\n"); return ret; } avctx->has_b_frames = !s->low_delay; if (ff_mpeg4_workaround_bugs(avctx) == 1) goto retry; /* After H263 & mpeg4 header decode we have the height, width, * and other parameters. So then we could init the picture. * FIXME: By the way H263 decoder is evolving it should have * an H263EncContext */ if (s->width != avctx->coded_width || s->height != avctx->coded_height || s->context_reinit) { /* H.263 could change picture size any time */ s->context_reinit = 0; ret = ff_set_dimensions(avctx, s->width, s->height); if (ret < 0) return ret; if ((ret = ff_MPV_common_frame_size_change(s))) return ret; } if (s->codec_id == AV_CODEC_ID_H263 || s->codec_id == AV_CODEC_ID_H263P || s->codec_id == AV_CODEC_ID_H263I) s->gob_index = ff_h263_get_gob_height(s); // for skipping the frame s->current_picture.f.pict_type = s->pict_type; s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I; /* skip B-frames if we don't have reference frames */ if (s->last_picture_ptr == NULL && (s->pict_type == AV_PICTURE_TYPE_B || s->droppable)) return get_consumed_bytes(s, buf_size); if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) || (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) || avctx->skip_frame >= AVDISCARD_ALL) return get_consumed_bytes(s, buf_size); if (s->next_p_frame_damaged) { if (s->pict_type == AV_PICTURE_TYPE_B) return get_consumed_bytes(s, buf_size); else s->next_p_frame_damaged = 0; } if ((!s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) { s->me.qpel_put = s->dsp.put_qpel_pixels_tab; s->me.qpel_avg = s->dsp.avg_qpel_pixels_tab; } else { s->me.qpel_put = s->dsp.put_no_rnd_qpel_pixels_tab; s->me.qpel_avg = s->dsp.avg_qpel_pixels_tab; } if ((ret = ff_MPV_frame_start(s, avctx)) < 0) return ret; if (!s->divx_packed && !avctx->hwaccel) ff_thread_finish_setup(avctx); if (CONFIG_MPEG4_VDPAU_DECODER && (s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)) { ff_vdpau_mpeg4_decode_picture(s, s->gb.buffer, s->gb.buffer_end - s->gb.buffer); goto frame_end; } if (avctx->hwaccel) { ret = avctx->hwaccel->start_frame(avctx, s->gb.buffer, s->gb.buffer_end - s->gb.buffer); if (ret < 0 ) return ret; } ff_mpeg_er_frame_start(s); /* the second part of the wmv2 header contains the MB skip bits which * are stored in current_picture->mb_type which is not available before * ff_MPV_frame_start() */ if (CONFIG_WMV2_DECODER && s->msmpeg4_version == 5) { ret = ff_wmv2_decode_secondary_picture_header(s); if (ret < 0) return ret; if (ret == 1) goto frame_end; } /* decode each macroblock */ s->mb_x = 0; s->mb_y = 0; slice_ret = decode_slice(s); while (s->mb_y < s->mb_height) { if (s->msmpeg4_version) { if (s->slice_height == 0 || s->mb_x != 0 || (s->mb_y % s->slice_height) != 0 || get_bits_left(&s->gb) < 0) break; } else { int prev_x = s->mb_x, prev_y = s->mb_y; if (ff_h263_resync(s) < 0) break; if (prev_y * s->mb_width + prev_x < s->mb_y * s->mb_width + s->mb_x) s->er.error_occurred = 1; } if (s->msmpeg4_version < 4 && s->h263_pred) ff_mpeg4_clean_buffers(s); if (decode_slice(s) < 0) slice_ret = AVERROR_INVALIDDATA; } if (s->msmpeg4_version && s->msmpeg4_version < 4 && s->pict_type == AV_PICTURE_TYPE_I) if (!CONFIG_MSMPEG4_DECODER || ff_msmpeg4_decode_ext_header(s, buf_size) < 0) s->er.error_status_table[s->mb_num - 1] = ER_MB_ERROR; av_assert1(s->bitstream_buffer_size == 0); frame_end: ff_er_frame_end(&s->er); if (avctx->hwaccel) { ret = avctx->hwaccel->end_frame(avctx); if (ret < 0) return ret; } ff_MPV_frame_end(s); /* divx 5.01+ bitstream reorder stuff */ /* Since this clobbers the input buffer and hwaccel codecs still need the * data during hwaccel->end_frame we should not do this any earlier */ if (s->codec_id == AV_CODEC_ID_MPEG4 && s->divx_packed) { int current_pos = s->gb.buffer == s->bitstream_buffer ? 0 : (get_bits_count(&s->gb) >> 3); int startcode_found = 0; if (buf_size - current_pos > 7) { int i; for (i = current_pos; i < buf_size - 4; i++) if (buf[i] == 0 && buf[i + 1] == 0 && buf[i + 2] == 1 && buf[i + 3] == 0xB6) { startcode_found = !(buf[i + 4] & 0x40); break; } } if (startcode_found) { av_fast_malloc(&s->bitstream_buffer, &s->allocated_bitstream_buffer_size, buf_size - current_pos + FF_INPUT_BUFFER_PADDING_SIZE); if (!s->bitstream_buffer) return AVERROR(ENOMEM); memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos); s->bitstream_buffer_size = buf_size - current_pos; } } if (!s->divx_packed && avctx->hwaccel) ff_thread_finish_setup(avctx); av_assert1(s->current_picture.f.pict_type == s->current_picture_ptr->f.pict_type); av_assert1(s->current_picture.f.pict_type == s->pict_type); if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) { if ((ret = av_frame_ref(pict, &s->current_picture_ptr->f)) < 0) return ret; ff_print_debug_info(s, s->current_picture_ptr, pict); ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, FF_QSCALE_TYPE_MPEG1); } else if (s->last_picture_ptr != NULL) { if ((ret = av_frame_ref(pict, &s->last_picture_ptr->f)) < 0) return ret; ff_print_debug_info(s, s->last_picture_ptr, pict); ff_mpv_export_qp_table(s, pict, s->last_picture_ptr, FF_QSCALE_TYPE_MPEG1); } if (s->last_picture_ptr || s->low_delay) { if ( pict->format == AV_PIX_FMT_YUV420P && (s->codec_tag == AV_RL32("GEOV") || s->codec_tag == AV_RL32("GEOX"))) { int x, y, p; av_frame_make_writable(pict); for (p=0; p<3; p++) { int w = FF_CEIL_RSHIFT(pict-> width, !!p); int h = FF_CEIL_RSHIFT(pict->height, !!p); int linesize = pict->linesize[p]; for (y=0; y<(h>>1); y++) for (x=0; x<w; x++) FFSWAP(int, pict->data[p][x + y*linesize], pict->data[p][x + (h-1-y)*linesize]); } } *got_frame = 1; } if (slice_ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE)) return ret; else return get_consumed_bytes(s, buf_size); }


static int v210_read_packet(AVFormatContext *s, AVPacket *pkt) { int packet_size, ret, width, height; AVStream *st = s->streams[0]; width = st->codec->width; height = st->codec->height; packet_size = GET_PACKET_SIZE(width, height); if (packet_size < 0) return -1; ret = av_get_packet(s->pb, pkt, packet_size); pkt->pts = pkt->dts = pkt->pos / packet_size; pkt->stream_index = 0; if (ret < 0) return ret; return 0; }


TEST_F ( BudgetDatabaseTest , GetBudgetNoBudgetOrSES ) {
 GetBudgetDetails ( ) ;
 ASSERT_TRUE ( success_ ) ;
 ASSERT_EQ ( 2U , prediction_ . size ( ) ) ;
 EXPECT_EQ ( 0 , prediction_ [ 0 ] -> budget_at ) ;
 }


const EVP_CIPHER * EVP_aes_ ## keylen ## _ ## mode ( void ) \ {
 return & aes_ ## keylen ## _ ## mode ;
 }
 # endif # if defined ( OPENSSL_CPUID_OBJ ) && ( defined ( __arm__ ) || defined ( __arm ) || defined ( __aarch64__ ) ) # include "arm_arch.h" # if __ARM_MAX_ARCH__ >= 7 # if defined ( BSAES_ASM ) # define BSAES_CAPABLE ( OPENSSL_armcap_P & ARMV7_NEON ) # endif # if defined ( VPAES_ASM ) # define VPAES_CAPABLE ( OPENSSL_armcap_P & ARMV7_NEON ) # endif # define HWAES_CAPABLE ( OPENSSL_armcap_P & ARMV8_AES ) # define HWAES_set_encrypt_key aes_v8_set_encrypt_key # define HWAES_set_decrypt_key aes_v8_set_decrypt_key # define HWAES_encrypt aes_v8_encrypt # define HWAES_decrypt aes_v8_decrypt # define HWAES_cbc_encrypt aes_v8_cbc_encrypt # define HWAES_ctr32_encrypt_blocks aes_v8_ctr32_encrypt_blocks # endif # endif # if defined ( HWAES_CAPABLE ) int HWAES_set_encrypt_key ( const unsigned char * userKey , const int bits , AES_KEY * key ) ;
 int HWAES_set_decrypt_key ( const unsigned char * userKey , const int bits , AES_KEY * key ) ;
 void HWAES_encrypt ( const unsigned char * in , unsigned char * out , const AES_KEY * key ) ;
 void HWAES_decrypt ( const unsigned char * in , unsigned char * out , const AES_KEY * key ) ;
 void HWAES_cbc_encrypt ( const unsigned char * in , unsigned char * out , size_t length , const AES_KEY * key , unsigned char * ivec , const int enc ) ;
 void HWAES_ctr32_encrypt_blocks ( const unsigned char * in , unsigned char * out , size_t len , const AES_KEY * key , const unsigned char ivec [ 16 ] ) ;
 void HWAES_xts_encrypt ( const unsigned char * inp , unsigned char * out , size_t len , const AES_KEY * key1 , const AES_KEY * key2 , const unsigned char iv [ 16 ] ) ;
 void HWAES_xts_decrypt ( const unsigned char * inp , unsigned char * out , size_t len , const AES_KEY * key1 , const AES_KEY * key2 , const unsigned char iv [ 16 ] ) ;
 # endif # define BLOCK_CIPHER_generic_pack ( nid , keylen , flags ) \ BLOCK_CIPHER_generic ( nid , keylen , 16 , 16 , cbc , cbc , CBC , flags | EVP_CIPH_FLAG_DEFAULT_ASN1 ) \ BLOCK_CIPHER_generic ( nid , keylen , 16 , 0 , ecb , ecb , ECB , flags | EVP_CIPH_FLAG_DEFAULT_ASN1 ) \ BLOCK_CIPHER_generic ( nid , keylen , 1 , 16 , ofb128 , ofb , OFB , flags | EVP_CIPH_FLAG_DEFAULT_ASN1 ) \ BLOCK_CIPHER_generic ( nid , keylen , 1 , 16 , cfb128 , cfb , CFB , flags | EVP_CIPH_FLAG_DEFAULT_ASN1 ) \ BLOCK_CIPHER_generic ( nid , keylen , 1 , 16 , cfb1 , cfb1 , CFB , flags ) \ BLOCK_CIPHER_generic ( nid , keylen , 1 , 16 , cfb8 , cfb8 , CFB , flags ) \ BLOCK_CIPHER_generic ( nid , keylen , 1 , 16 , ctr , ctr , CTR , flags ) static int aes_init_key ( EVP_CIPHER_CTX * ctx , const unsigned char * key , const unsigned char * iv , int enc ) {
 int ret , mode ;
 EVP_AES_KEY * dat = EVP_C_DATA ( EVP_AES_KEY , ctx ) ;
 mode = EVP_CIPHER_CTX_mode ( ctx ) ;
 if ( ( mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE ) && ! enc ) {
 # ifdef HWAES_CAPABLE if ( HWAES_CAPABLE ) {
 ret = HWAES_set_decrypt_key ( key , EVP_CIPHER_CTX_key_length ( ctx ) * 8 , & dat -> ks . ks ) ;
 dat -> block = ( block128_f ) HWAES_decrypt ;
 dat -> stream . cbc = NULL ;
 # ifdef HWAES_cbc_encrypt if ( mode == EVP_CIPH_CBC_MODE ) dat -> stream . cbc = ( cbc128_f ) HWAES_cbc_encrypt ;
 # endif }
 else # endif # ifdef BSAES_CAPABLE if ( BSAES_CAPABLE && mode == EVP_CIPH_CBC_MODE ) {
 ret = AES_set_decrypt_key ( key , EVP_CIPHER_CTX_key_length ( ctx ) * 8 , & dat -> ks . ks ) ;
 dat -> block = ( block128_f ) AES_decrypt ;
 dat -> stream . cbc = ( cbc128_f ) bsaes_cbc_encrypt ;
 }
 else # endif # ifdef VPAES_CAPABLE if ( VPAES_CAPABLE ) {
 ret = vpaes_set_decrypt_key ( key , EVP_CIPHER_CTX_key_length ( ctx ) * 8 , & dat -> ks . ks ) ;
 dat -> block = ( block128_f ) vpaes_decrypt ;
 dat -> stream . cbc = mode == EVP_CIPH_CBC_MODE ? ( cbc128_f ) vpaes_cbc_encrypt : NULL ;
 }
 else # endif {
 ret = AES_set_decrypt_key ( key , EVP_CIPHER_CTX_key_length ( ctx ) * 8 , & dat -> ks . ks ) ;
 dat -> block = ( block128_f ) AES_decrypt ;
 dat -> stream . cbc = mode == EVP_CIPH_CBC_MODE ? ( cbc128_f ) AES_cbc_encrypt : NULL ;
 }
 }
 else # ifdef HWAES_CAPABLE if ( HWAES_CAPABLE ) {
 ret = HWAES_set_encrypt_key ( key , EVP_CIPHER_CTX_key_length ( ctx ) * 8 , & dat -> ks . ks ) ;
 dat -> block = ( block128_f ) HWAES_encrypt ;
 dat -> stream . cbc = NULL ;
 # ifdef HWAES_cbc_encrypt if ( mode == EVP_CIPH_CBC_MODE ) dat -> stream . cbc = ( cbc128_f ) HWAES_cbc_encrypt ;
 else # endif # ifdef HWAES_ctr32_encrypt_blocks if ( mode == EVP_CIPH_CTR_MODE ) dat -> stream . ctr = ( ctr128_f ) HWAES_ctr32_encrypt_blocks ;
 else # endif ( void ) 0 ;
 }
 else # endif # ifdef BSAES_CAPABLE if ( BSAES_CAPABLE && mode == EVP_CIPH_CTR_MODE ) {
 ret = AES_set_encrypt_key ( key , EVP_CIPHER_CTX_key_length ( ctx ) * 8 , & dat -> ks . ks ) ;
 dat -> block = ( block128_f ) AES_encrypt ;
 dat -> stream . ctr = ( ctr128_f ) bsaes_ctr32_encrypt_blocks ;
 }
 else # endif # ifdef VPAES_CAPABLE if ( VPAES_CAPABLE ) {
 ret = vpaes_set_encrypt_key ( key , EVP_CIPHER_CTX_key_length ( ctx ) * 8 , & dat -> ks . ks ) ;
 dat -> block = ( block128_f ) vpaes_encrypt ;
 dat -> stream . cbc = mode == EVP_CIPH_CBC_MODE ? ( cbc128_f ) vpaes_cbc_encrypt : NULL ;
 }
 else # endif {
 ret = AES_set_encrypt_key ( key , EVP_CIPHER_CTX_key_length ( ctx ) * 8 , & dat -> ks . ks ) ;
 dat -> block = ( block128_f ) AES_encrypt ;
 dat -> stream . cbc = mode == EVP_CIPH_CBC_MODE ? ( cbc128_f ) AES_cbc_encrypt : NULL ;
 # ifdef AES_CTR_ASM if ( mode == EVP_CIPH_CTR_MODE ) dat -> stream . ctr = ( ctr128_f ) AES_ctr32_encrypt ;
 # endif }
 if ( ret < 0 ) {
 EVPerr ( EVP_F_AES_INIT_KEY , EVP_R_AES_KEY_SETUP_FAILED ) ;
 return 0 ;
 }
 return 1 ;
 }
 static int aes_cbc_cipher ( EVP_CIPHER_CTX * ctx , unsigned char * out , const unsigned char * in , size_t len ) {
 EVP_AES_KEY * dat = EVP_C_DATA ( EVP_AES_KEY , ctx ) ;
 if ( dat -> stream . cbc ) ( * dat -> stream . cbc ) ( in , out , len , & dat -> ks , EVP_CIPHER_CTX_iv_noconst ( ctx ) , EVP_CIPHER_CTX_encrypting ( ctx ) ) ;
 else if ( EVP_CIPHER_CTX_encrypting ( ctx ) ) CRYPTO_cbc128_encrypt ( in , out , len , & dat -> ks , EVP_CIPHER_CTX_iv_noconst ( ctx ) , dat -> block ) ;
 else CRYPTO_cbc128_decrypt ( in , out , len , & dat -> ks , EVP_CIPHER_CTX_iv_noconst ( ctx ) , dat -> block ) ;
 return 1 ;
 }
 static int aes_ecb_cipher ( EVP_CIPHER_CTX * ctx , unsigned char * out , const unsigned char * in , size_t len ) {
 size_t bl = EVP_CIPHER_CTX_block_size ( ctx ) ;
 size_t i ;
 EVP_AES_KEY * dat = EVP_C_DATA ( EVP_AES_KEY , ctx ) ;
 if ( len < bl ) return 1 ;
 for ( i = 0 , len -= bl ;
 i <= len ;
 i += bl ) ( * dat -> block ) ( in + i , out + i , & dat -> ks ) ;
 return 1 ;
 }
 static int aes_ofb_cipher ( EVP_CIPHER_CTX * ctx , unsigned char * out , const unsigned char * in , size_t len ) {
 EVP_AES_KEY * dat = EVP_C_DATA ( EVP_AES_KEY , ctx ) ;
 int num = EVP_CIPHER_CTX_num ( ctx ) ;
 CRYPTO_ofb128_encrypt ( in , out , len , & dat -> ks , EVP_CIPHER_CTX_iv_noconst ( ctx ) , & num , dat -> block ) ;
 EVP_CIPHER_CTX_set_num ( ctx , num ) ;
 return 1 ;
 }
 static int aes_cfb_cipher ( EVP_CIPHER_CTX * ctx , unsigned char * out , const unsigned char * in , size_t len ) {
 EVP_AES_KEY * dat = EVP_C_DATA ( EVP_AES_KEY , ctx ) ;
 int num = EVP_CIPHER_CTX_num ( ctx ) ;
 CRYPTO_cfb128_encrypt ( in , out , len , & dat -> ks , EVP_CIPHER_CTX_iv_noconst ( ctx ) , & num , EVP_CIPHER_CTX_encrypting ( ctx ) , dat -> block ) ;
 EVP_CIPHER_CTX_set_num ( ctx , num ) ;
 return 1 ;
 }
 static int aes_cfb8_cipher ( EVP_CIPHER_CTX * ctx , unsigned char * out , const unsigned char * in , size_t len ) {
 EVP_AES_KEY * dat = EVP_C_DATA ( EVP_AES_KEY , ctx ) ;
 int num = EVP_CIPHER_CTX_num ( ctx ) ;
 CRYPTO_cfb128_8_encrypt ( in , out , len , & dat -> ks , EVP_CIPHER_CTX_iv_noconst ( ctx ) , & num , EVP_CIPHER_CTX_encrypting ( ctx ) , dat -> block ) ;
 EVP_CIPHER_CTX_set_num ( ctx , num ) ;
 return 1 ;
 }
 static int aes_cfb1_cipher ( EVP_CIPHER_CTX * ctx , unsigned char * out , const unsigned char * in , size_t len ) {
 EVP_AES_KEY * dat = EVP_C_DATA ( EVP_AES_KEY , ctx ) ;
 if ( EVP_CIPHER_CTX_test_flags ( ctx , EVP_CIPH_FLAG_LENGTH_BITS ) ) {
 int num = EVP_CIPHER_CTX_num ( ctx ) ;
 CRYPTO_cfb128_1_encrypt ( in , out , len , & dat -> ks , EVP_CIPHER_CTX_iv_noconst ( ctx ) , & num , EVP_CIPHER_CTX_encrypting ( ctx ) , dat -> block ) ;
 EVP_CIPHER_CTX_set_num ( ctx , num ) ;
 return 1 ;
 }
 while ( len >= MAXBITCHUNK ) {
 int num = EVP_CIPHER_CTX_num ( ctx ) ;
 CRYPTO_cfb128_1_encrypt ( in , out , MAXBITCHUNK * 8 , & dat -> ks , EVP_CIPHER_CTX_iv_noconst ( ctx ) , & num , EVP_CIPHER_CTX_encrypting ( ctx ) , dat -> block ) ;
 EVP_CIPHER_CTX_set_num ( ctx , num ) ;
 len -= MAXBITCHUNK ;
 }
 if ( len ) {
 int num = EVP_CIPHER_CTX_num ( ctx ) ;
 CRYPTO_cfb128_1_encrypt ( in , out , len * 8 , & dat -> ks , EVP_CIPHER_CTX_iv_noconst ( ctx ) , & num , EVP_CIPHER_CTX_encrypting ( ctx ) , dat -> block ) ;
 EVP_CIPHER_CTX_set_num ( ctx , num ) ;
 }
 return 1 ;
 }
 static int aes_ctr_cipher ( EVP_CIPHER_CTX * ctx , unsigned char * out , const unsigned char * in , size_t len ) {
 unsigned int num = EVP_CIPHER_CTX_num ( ctx ) ;
 EVP_AES_KEY * dat = EVP_C_DATA ( EVP_AES_KEY , ctx ) ;
 if ( dat -> stream . ctr ) CRYPTO_ctr128_encrypt_ctr32 ( in , out , len , & dat -> ks , EVP_CIPHER_CTX_iv_noconst ( ctx ) , EVP_CIPHER_CTX_buf_noconst ( ctx ) , & num , dat -> stream . ctr ) ;
 else CRYPTO_ctr128_encrypt ( in , out , len , & dat -> ks , EVP_CIPHER_CTX_iv_noconst ( ctx ) , EVP_CIPHER_CTX_buf_noconst ( ctx ) , & num , dat -> block ) ;
 EVP_CIPHER_CTX_set_num ( ctx , num ) ;
 return 1 ;
 }
 BLOCK_CIPHER_generic_pack ( NID_aes , 128 , 0 ) BLOCK_CIPHER_generic_pack ( NID_aes , 192 , 0 ) BLOCK_CIPHER_generic_pack ( NID_aes , 256 , 0 ) static int aes_gcm_cleanup ( EVP_CIPHER_CTX * c ) {
 EVP_AES_GCM_CTX * gctx = EVP_C_DATA ( EVP_AES_GCM_CTX , c ) ;
 OPENSSL_cleanse ( & gctx -> gcm , sizeof ( gctx -> gcm ) ) ;
 if ( gctx -> iv != EVP_CIPHER_CTX_iv_noconst ( c ) ) OPENSSL_free ( gctx -> iv ) ;
 return 1 ;
 }
 static void ctr64_inc ( unsigned char * counter ) {
 int n = 8 ;
 unsigned char c ;
 do {
 -- n ;
 c = counter [ n ] ;
 ++ c ;
 counter [ n ] = c ;
 if ( c ) return ;
 }
 while ( n ) ;
 }
 static int aes_gcm_ctrl ( EVP_CIPHER_CTX * c , int type , int arg , void * ptr ) {
 EVP_AES_GCM_CTX * gctx = EVP_C_DATA ( EVP_AES_GCM_CTX , c ) ;
 switch ( type ) {
 case EVP_CTRL_INIT : gctx -> key_set = 0 ;
 gctx -> iv_set = 0 ;
 gctx -> ivlen = EVP_CIPHER_CTX_iv_length ( c ) ;
 gctx -> iv = EVP_CIPHER_CTX_iv_noconst ( c ) ;
 gctx -> taglen = - 1 ;
 gctx -> iv_gen = 0 ;
 gctx -> tls_aad_len = - 1 ;
 return 1 ;
 case EVP_CTRL_AEAD_SET_IVLEN : if ( arg <= 0 ) return 0 ;
 if ( ( arg > EVP_MAX_IV_LENGTH ) && ( arg > gctx -> ivlen ) ) {
 if ( gctx -> iv != EVP_CIPHER_CTX_iv_noconst ( c ) ) OPENSSL_free ( gctx -> iv ) ;
 gctx -> iv = OPENSSL_malloc ( arg ) ;
 if ( gctx -> iv == NULL ) return 0 ;
 }
 gctx -> ivlen = arg ;
 return 1 ;
 case EVP_CTRL_AEAD_SET_TAG : if ( arg <= 0 || arg > 16 || EVP_CIPHER_CTX_encrypting ( c ) ) return 0 ;
 memcpy ( EVP_CIPHER_CTX_buf_noconst ( c ) , ptr , arg ) ;
 gctx -> taglen = arg ;
 return 1 ;
 case EVP_CTRL_AEAD_GET_TAG : if ( arg <= 0 || arg > 16 || ! EVP_CIPHER_CTX_encrypting ( c ) || gctx -> taglen < 0 ) return 0 ;
 memcpy ( ptr , EVP_CIPHER_CTX_buf_noconst ( c ) , arg ) ;
 return 1 ;
 case EVP_CTRL_GCM_SET_IV_FIXED : if ( arg == - 1 ) {
 memcpy ( gctx -> iv , ptr , gctx -> ivlen ) ;
 gctx -> iv_gen = 1 ;
 return 1 ;
 }
 if ( ( arg < 4 ) || ( gctx -> ivlen - arg ) < 8 ) return 0 ;
 if ( arg ) memcpy ( gctx -> iv , ptr , arg ) ;
 if ( EVP_CIPHER_CTX_encrypting ( c ) && RAND_bytes ( gctx -> iv + arg , gctx -> ivlen - arg ) <= 0 ) return 0 ;
 gctx -> iv_gen = 1 ;
 return 1 ;
 case EVP_CTRL_GCM_IV_GEN : if ( gctx -> iv_gen == 0 || gctx -> key_set == 0 ) return 0 ;
 CRYPTO_gcm128_setiv ( & gctx -> gcm , gctx -> iv , gctx -> ivlen ) ;
 if ( arg <= 0 || arg > gctx -> ivlen ) arg = gctx -> ivlen ;
 memcpy ( ptr , gctx -> iv + gctx -> ivlen - arg , arg ) ;
 ctr64_inc ( gctx -> iv + gctx -> ivlen - 8 ) ;
 gctx -> iv_set = 1 ;
 return 1 ;
 case EVP_CTRL_GCM_SET_IV_INV : if ( gctx -> iv_gen == 0 || gctx -> key_set == 0 || EVP_CIPHER_CTX_encrypting ( c ) ) return 0 ;
 memcpy ( gctx -> iv + gctx -> ivlen - arg , ptr , arg ) ;
 CRYPTO_gcm128_setiv ( & gctx -> gcm , gctx -> iv , gctx -> ivlen ) ;
 gctx -> iv_set = 1 ;
 return 1 ;
 case EVP_CTRL_AEAD_TLS1_AAD : if ( arg != EVP_AEAD_TLS1_AAD_LEN ) return 0 ;
 memcpy ( EVP_CIPHER_CTX_buf_noconst ( c ) , ptr , arg ) ;
 gctx -> tls_aad_len = arg ;
 {
 unsigned int len = EVP_CIPHER_CTX_buf_noconst ( c ) [ arg - 2 ] << 8 | EVP_CIPHER_CTX_buf_noconst ( c ) [ arg - 1 ] ;
 len -= EVP_GCM_TLS_EXPLICIT_IV_LEN ;
 if ( ! EVP_CIPHER_CTX_encrypting ( c ) ) len -= EVP_GCM_TLS_TAG_LEN ;
 EVP_CIPHER_CTX_buf_noconst ( c ) [ arg - 2 ] = len >> 8 ;
 EVP_CIPHER_CTX_buf_noconst ( c ) [ arg - 1 ] = len & 0xff ;
 }
 return EVP_GCM_TLS_TAG_LEN ;
 case EVP_CTRL_COPY : {
 EVP_CIPHER_CTX * out = ptr ;
 EVP_AES_GCM_CTX * gctx_out = EVP_C_DATA ( EVP_AES_GCM_CTX , out ) ;
 if ( gctx -> gcm . key ) {
 if ( gctx -> gcm . key != & gctx -> ks ) return 0 ;
 gctx_out -> gcm . key = & gctx_out -> ks ;
 }
 if ( gctx -> iv == EVP_CIPHER_CTX_iv_noconst ( c ) ) gctx_out -> iv = EVP_CIPHER_CTX_iv_noconst ( out ) ;
 else {
 gctx_out -> iv = OPENSSL_malloc ( gctx -> ivlen ) ;
 if ( gctx_out -> iv == NULL ) return 0 ;
 memcpy ( gctx_out -> iv , gctx -> iv , gctx -> ivlen ) ;
 }
 return 1 ;
 }
 default : return - 1 ;
 }
 }
 static int aes_gcm_init_key ( EVP_CIPHER_CTX * ctx , const unsigned char * key , const unsigned char * iv , int enc ) {
 EVP_AES_GCM_CTX * gctx = EVP_C_DATA ( EVP_AES_GCM_CTX , ctx ) ;
 if ( ! iv && ! key ) return 1 ;
 if ( key ) {
 do {
 # ifdef HWAES_CAPABLE if ( HWAES_CAPABLE ) {
 HWAES_set_encrypt_key ( key , EVP_CIPHER_CTX_key_length ( ctx ) * 8 , & gctx -> ks . ks ) ;
 CRYPTO_gcm128_init ( & gctx -> gcm , & gctx -> ks , ( block128_f ) HWAES_encrypt ) ;
 # ifdef HWAES_ctr32_encrypt_blocks gctx -> ctr = ( ctr128_f ) HWAES_ctr32_encrypt_blocks ;
 # else gctx -> ctr = NULL ;
 # endif break ;
 }
 else # endif # ifdef BSAES_CAPABLE if ( BSAES_CAPABLE ) {
 AES_set_encrypt_key ( key , EVP_CIPHER_CTX_key_length ( ctx ) * 8 , & gctx -> ks . ks ) ;
 CRYPTO_gcm128_init ( & gctx -> gcm , & gctx -> ks , ( block128_f ) AES_encrypt ) ;
 gctx -> ctr = ( ctr128_f ) bsaes_ctr32_encrypt_blocks ;
 break ;
 }
 else # endif # ifdef VPAES_CAPABLE if ( VPAES_CAPABLE ) {
 vpaes_set_encrypt_key ( key , EVP_CIPHER_CTX_key_length ( ctx ) * 8 , & gctx -> ks . ks ) ;
 CRYPTO_gcm128_init ( & gctx -> gcm , & gctx -> ks , ( block128_f ) vpaes_encrypt ) ;
 gctx -> ctr = NULL ;
 break ;
 }
 else # endif ( void ) 0 ;
 AES_set_encrypt_key ( key , EVP_CIPHER_CTX_key_length ( ctx ) * 8 , & gctx -> ks . ks ) ;
 CRYPTO_gcm128_init ( & gctx -> gcm , & gctx -> ks , ( block128_f ) AES_encrypt ) ;
 # ifdef AES_CTR_ASM gctx -> ctr = ( ctr128_f ) AES_ctr32_encrypt ;
 # else gctx -> ctr = NULL ;
 # endif }
 while ( 0 ) ;
 if ( iv == NULL && gctx -> iv_set ) iv = gctx -> iv ;
 if ( iv ) {
 CRYPTO_gcm128_setiv ( & gctx -> gcm , iv , gctx -> ivlen ) ;
 gctx -> iv_set = 1 ;
 }
 gctx -> key_set = 1 ;
 }
 else {
 if ( gctx -> key_set ) CRYPTO_gcm128_setiv ( & gctx -> gcm , iv , gctx -> ivlen ) ;
 else memcpy ( gctx -> iv , iv , gctx -> ivlen ) ;
 gctx -> iv_set = 1 ;
 gctx -> iv_gen = 0 ;
 }
 return 1 ;
 }
 static int aes_gcm_tls_cipher ( EVP_CIPHER_CTX * ctx , unsigned char * out , const unsigned char * in , size_t len ) {
 EVP_AES_GCM_CTX * gctx = EVP_C_DATA ( EVP_AES_GCM_CTX , ctx ) ;
 int rv = - 1 ;
 if ( out != in || len < ( EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN ) ) return - 1 ;
 if ( EVP_CIPHER_CTX_ctrl ( ctx , EVP_CIPHER_CTX_encrypting ( ctx ) ? EVP_CTRL_GCM_IV_GEN : EVP_CTRL_GCM_SET_IV_INV , EVP_GCM_TLS_EXPLICIT_IV_LEN , out ) <= 0 ) goto err ;
 if ( CRYPTO_gcm128_aad ( & gctx -> gcm , EVP_CIPHER_CTX_buf_noconst ( ctx ) , gctx -> tls_aad_len ) ) goto err ;
 in += EVP_GCM_TLS_EXPLICIT_IV_LEN ;
 out += EVP_GCM_TLS_EXPLICIT_IV_LEN ;
 len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN ;
 if ( EVP_CIPHER_CTX_encrypting ( ctx ) ) {
 if ( gctx -> ctr ) {
 size_t bulk = 0 ;
 # if defined ( AES_GCM_ASM ) if ( len >= 32 && AES_GCM_ASM ( gctx ) ) {
 if ( CRYPTO_gcm128_encrypt ( & gctx -> gcm , NULL , NULL , 0 ) ) return - 1 ;
 bulk = AES_gcm_encrypt ( in , out , len , gctx -> gcm . key , gctx -> gcm . Yi . c , gctx -> gcm . Xi . u ) ;
 gctx -> gcm . len . u [ 1 ] += bulk ;
 }
 # endif if ( CRYPTO_gcm128_encrypt_ctr32 ( & gctx -> gcm , in + bulk , out + bulk , len - bulk , gctx -> ctr ) ) goto err ;
 }
 else {
 size_t bulk = 0 ;
 # if defined ( AES_GCM_ASM2 ) if ( len >= 32 && AES_GCM_ASM2 ( gctx ) ) {
 if ( CRYPTO_gcm128_encrypt ( & gctx -> gcm , NULL , NULL , 0 ) ) return - 1 ;
 bulk = AES_gcm_encrypt ( in , out , len , gctx -> gcm . key , gctx -> gcm . Yi . c , gctx -> gcm . Xi . u ) ;
 gctx -> gcm . len . u [ 1 ] += bulk ;
 }
 # endif if ( CRYPTO_gcm128_encrypt ( & gctx -> gcm , in + bulk , out + bulk , len - bulk ) ) goto err ;
 }
 out += len ;
 CRYPTO_gcm128_tag ( & gctx -> gcm , out , EVP_GCM_TLS_TAG_LEN ) ;
 rv = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN ;
 }
 else {
 if ( gctx -> ctr ) {
 size_t bulk = 0 ;
 # if defined ( AES_GCM_ASM ) if ( len >= 16 && AES_GCM_ASM ( gctx ) ) {
 if ( CRYPTO_gcm128_decrypt ( & gctx -> gcm , NULL , NULL , 0 ) ) return - 1 ;
 bulk = AES_gcm_decrypt ( in , out , len , gctx -> gcm . key , gctx -> gcm . Yi . c , gctx -> gcm . Xi . u ) ;
 gctx -> gcm . len . u [ 1 ] += bulk ;
 }
 # endif if ( CRYPTO_gcm128_decrypt_ctr32 ( & gctx -> gcm , in + bulk , out + bulk , len - bulk , gctx -> ctr ) ) goto err ;
 }
 else {
 size_t bulk = 0 ;
 # if defined ( AES_GCM_ASM2 ) if ( len >= 16 && AES_GCM_ASM2 ( gctx ) ) {
 if ( CRYPTO_gcm128_decrypt ( & gctx -> gcm , NULL , NULL , 0 ) ) return - 1 ;
 bulk = AES_gcm_decrypt ( in , out , len , gctx -> gcm . key , gctx -> gcm . Yi . c , gctx -> gcm . Xi . u ) ;
 gctx -> gcm . len . u [ 1 ] += bulk ;
 }
 # endif if ( CRYPTO_gcm128_decrypt ( & gctx -> gcm , in + bulk , out + bulk , len - bulk ) ) goto err ;
 }
 CRYPTO_gcm128_tag ( & gctx -> gcm , EVP_CIPHER_CTX_buf_noconst ( ctx ) , EVP_GCM_TLS_TAG_LEN ) ;
 if ( CRYPTO_memcmp ( EVP_CIPHER_CTX_buf_noconst ( ctx ) , in + len , EVP_GCM_TLS_TAG_LEN ) ) {
 OPENSSL_cleanse ( out , len ) ;
 goto err ;
 }
 rv = len ;
 }
 err : gctx -> iv_set = 0 ;
 gctx -> tls_aad_len = - 1 ;
 return rv ;
 }
 static int aes_gcm_cipher ( EVP_CIPHER_CTX * ctx , unsigned char * out , const unsigned char * in , size_t len ) {
 EVP_AES_GCM_CTX * gctx = EVP_C_DATA ( EVP_AES_GCM_CTX , ctx ) ;
 if ( ! gctx -> key_set ) return - 1 ;
 if ( gctx -> tls_aad_len >= 0 ) return aes_gcm_tls_cipher ( ctx , out , in , len ) ;
 if ( ! gctx -> iv_set ) return - 1 ;
 if ( in ) {
 if ( out == NULL ) {
 if ( CRYPTO_gcm128_aad ( & gctx -> gcm , in , len ) ) return - 1 ;
 }
 else if ( EVP_CIPHER_CTX_encrypting ( ctx ) ) {
 if ( gctx -> ctr ) {
 size_t bulk = 0 ;
 # if defined ( AES_GCM_ASM ) if ( len >= 32 && AES_GCM_ASM ( gctx ) ) {
 size_t res = ( 16 - gctx -> gcm . mres ) % 16 ;
 if ( CRYPTO_gcm128_encrypt ( & gctx -> gcm , in , out , res ) ) return - 1 ;
 bulk = AES_gcm_encrypt ( in + res , out + res , len - res , gctx -> gcm . key , gctx -> gcm . Yi . c , gctx -> gcm . Xi . u ) ;
 gctx -> gcm . len . u [ 1 ] += bulk ;
 bulk += res ;
 }
 # endif if ( CRYPTO_gcm128_encrypt_ctr32 ( & gctx -> gcm , in + bulk , out + bulk , len - bulk , gctx -> ctr ) ) return - 1 ;
 }
 else {
 size_t bulk = 0 ;
 # if defined ( AES_GCM_ASM2 ) if ( len >= 32 && AES_GCM_ASM2 ( gctx ) ) {
 size_t res = ( 16 - gctx -> gcm . mres ) % 16 ;
 if ( CRYPTO_gcm128_encrypt ( & gctx -> gcm , in , out , res ) ) return - 1 ;
 bulk = AES_gcm_encrypt ( in + res , out + res , len - res , gctx -> gcm . key , gctx -> gcm . Yi . c , gctx -> gcm . Xi . u ) ;
 gctx -> gcm . len . u [ 1 ] += bulk ;
 bulk += res ;
 }
 # endif if ( CRYPTO_gcm128_encrypt ( & gctx -> gcm , in + bulk , out + bulk , len - bulk ) ) return - 1 ;
 }
 }
 else {
 if ( gctx -> ctr ) {
 size_t bulk = 0 ;
 # if defined ( AES_GCM_ASM ) if ( len >= 16 && AES_GCM_ASM ( gctx ) ) {
 size_t res = ( 16 - gctx -> gcm . mres ) % 16 ;
 if ( CRYPTO_gcm128_decrypt ( & gctx -> gcm , in , out , res ) ) return - 1 ;
 bulk = AES_gcm_decrypt ( in + res , out + res , len - res , gctx -> gcm . key , gctx -> gcm . Yi . c , gctx -> gcm . Xi . u ) ;
 gctx -> gcm . len . u [ 1 ] += bulk ;
 bulk += res ;
 }
 # endif if ( CRYPTO_gcm128_decrypt_ctr32 ( & gctx -> gcm , in + bulk , out + bulk , len - bulk , gctx -> ctr ) ) return - 1 ;
 }
 else {
 size_t bulk = 0 ;
 # if defined ( AES_GCM_ASM2 ) if ( len >= 16 && AES_GCM_ASM2 ( gctx ) ) {
 size_t res = ( 16 - gctx -> gcm . mres ) % 16 ;
 if ( CRYPTO_gcm128_decrypt ( & gctx -> gcm , in , out , res ) ) return - 1 ;
 bulk = AES_gcm_decrypt ( in + res , out + res , len - res , gctx -> gcm . key , gctx -> gcm . Yi . c , gctx -> gcm . Xi . u ) ;
 gctx -> gcm . len . u [ 1 ] += bulk ;
 bulk += res ;
 }
 # endif if ( CRYPTO_gcm128_decrypt ( & gctx -> gcm , in + bulk , out + bulk , len - bulk ) ) return - 1 ;
 }
 }
 return len ;
 }
 else {
 if ( ! EVP_CIPHER_CTX_encrypting ( ctx ) ) {
 if ( gctx -> taglen < 0 ) return - 1 ;
 if ( CRYPTO_gcm128_finish ( & gctx -> gcm , EVP_CIPHER_CTX_buf_noconst ( ctx ) , gctx -> taglen ) != 0 ) return - 1 ;
 gctx -> iv_set = 0 ;
 return 0 ;
 }
 CRYPTO_gcm128_tag ( & gctx -> gcm , EVP_CIPHER_CTX_buf_noconst ( ctx ) , 16 ) ;
 gctx -> taglen = 16 ;
 gctx -> iv_set = 0 ;
 return 0 ;
 }
 }
 # define CUSTOM_FLAGS ( EVP_CIPH_FLAG_DEFAULT_ASN1 \ | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \ | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT \ | EVP_CIPH_CUSTOM_COPY ) BLOCK_CIPHER_custom ( NID_aes , 128 , 1 , 12 , gcm , GCM , EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS ) BLOCK_CIPHER_custom ( NID_aes , 192 , 1 , 12 , gcm , GCM , EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS ) BLOCK_CIPHER_custom ( NID_aes , 256 , 1 , 12 , gcm , GCM , EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS ) static int aes_xts_ctrl ( EVP_CIPHER_CTX * c , int type , int arg , void * ptr ) {
 EVP_AES_XTS_CTX * xctx = EVP_C_DATA ( EVP_AES_XTS_CTX , c ) ;
 if ( type == EVP_CTRL_COPY ) {
 EVP_CIPHER_CTX * out = ptr ;
 EVP_AES_XTS_CTX * xctx_out = EVP_C_DATA ( EVP_AES_XTS_CTX , out ) ;
 if ( xctx -> xts . key1 ) {
 if ( xctx -> xts . key1 != & xctx -> ks1 ) return 0 ;
 xctx_out -> xts . key1 = & xctx_out -> ks1 ;
 }
 if ( xctx -> xts . key2 ) {
 if ( xctx -> xts . key2 != & xctx -> ks2 ) return 0 ;
 xctx_out -> xts . key2 = & xctx_out -> ks2 ;
 }
 return 1 ;
 }
 else if ( type != EVP_CTRL_INIT ) return - 1 ;
 xctx -> xts . key1 = NULL ;
 xctx -> xts . key2 = NULL ;
 return 1 ;
 }
 static int aes_xts_init_key ( EVP_CIPHER_CTX * ctx , const unsigned char * key , const unsigned char * iv , int enc ) {
 EVP_AES_XTS_CTX * xctx = EVP_C_DATA ( EVP_AES_XTS_CTX , ctx ) ;
 if ( ! iv && ! key ) return 1 ;
 if ( key ) do {
 # ifdef AES_XTS_ASM xctx -> stream = enc ? AES_xts_encrypt : AES_xts_decrypt ;
 # else xctx -> stream = NULL ;
 # endif # ifdef HWAES_CAPABLE if ( HWAES_CAPABLE ) {
 if ( enc ) {
 HWAES_set_encrypt_key ( key , EVP_CIPHER_CTX_key_length ( ctx ) * 4 , & xctx -> ks1 . ks ) ;
 xctx -> xts . block1 = ( block128_f ) HWAES_encrypt ;
 # ifdef HWAES_xts_encrypt xctx -> stream = HWAES_xts_encrypt ;
 # endif }
 else {
 HWAES_set_decrypt_key ( key , EVP_CIPHER_CTX_key_length ( ctx ) * 4 , & xctx -> ks1 . ks ) ;
 xctx -> xts . block1 = ( block128_f ) HWAES_decrypt ;
 # ifdef HWAES_xts_decrypt xctx -> stream = HWAES_xts_decrypt ;
 # endif }
 HWAES_set_encrypt_key ( key + EVP_CIPHER_CTX_key_length ( ctx ) / 2 , EVP_CIPHER_CTX_key_length ( ctx ) * 4 , & xctx -> ks2 . ks ) ;
 xctx -> xts . block2 = ( block128_f ) HWAES_encrypt ;
 xctx -> xts . key1 = & xctx -> ks1 ;
 break ;
 }
 else # endif # ifdef BSAES_CAPABLE if ( BSAES_CAPABLE ) xctx -> stream = enc ? bsaes_xts_encrypt : bsaes_xts_decrypt ;
 else # endif # ifdef VPAES_CAPABLE if ( VPAES_CAPABLE ) {
 if ( enc ) {
 vpaes_set_encrypt_key ( key , EVP_CIPHER_CTX_key_length ( ctx ) * 4 , & xctx -> ks1 . ks ) ;
 xctx -> xts . block1 = ( block128_f ) vpaes_encrypt ;
 }
 else {
 vpaes_set_decrypt_key ( key , EVP_CIPHER_CTX_key_length ( ctx ) * 4 , & xctx -> ks1 . ks ) ;
 xctx -> xts . block1 = ( block128_f ) vpaes_decrypt ;
 }
 vpaes_set_encrypt_key ( key + EVP_CIPHER_CTX_key_length ( ctx ) / 2 , EVP_CIPHER_CTX_key_length ( ctx ) * 4 , & xctx -> ks2 . ks ) ;
 xctx -> xts . block2 = ( block128_f ) vpaes_encrypt ;
 xctx -> xts . key1 = & xctx -> ks1 ;
 break ;
 }
 else # endif ( void ) 0 ;
 if ( enc ) {
 AES_set_encrypt_key ( key , EVP_CIPHER_CTX_key_length ( ctx ) * 4 , & xctx -> ks1 . ks ) ;
 xctx -> xts . block1 = ( block128_f ) AES_encrypt ;
 }
 else {
 AES_set_decrypt_key ( key , EVP_CIPHER_CTX_key_length ( ctx ) * 4 , & xctx -> ks1 . ks ) ;
 xctx -> xts . block1 = ( block128_f ) AES_decrypt ;
 }
 AES_set_encrypt_key ( key + EVP_CIPHER_CTX_key_length ( ctx ) / 2 , EVP_CIPHER_CTX_key_length ( ctx ) * 4 , & xctx -> ks2 . ks ) ;
 xctx -> xts . block2 = ( block128_f ) AES_encrypt ;
 xctx -> xts . key1 = & xctx -> ks1 ;
 }
 while ( 0 ) ;
 if ( iv ) {
 xctx -> xts . key2 = & xctx -> ks2 ;
 memcpy ( EVP_CIPHER_CTX_iv_noconst ( ctx ) , iv , 16 ) ;
 }
 return 1 ;
 }
 static int aes_xts_cipher ( EVP_CIPHER_CTX * ctx , unsigned char * out , const unsigned char * in , size_t len ) {
 EVP_AES_XTS_CTX * xctx = EVP_C_DATA ( EVP_AES_XTS_CTX , ctx ) ;
 if ( ! xctx -> xts . key1 || ! xctx -> xts . key2 ) return 0 ;
 if ( ! out || ! in || len < AES_BLOCK_SIZE ) return 0 ;
 if ( xctx -> stream ) ( * xctx -> stream ) ( in , out , len , xctx -> xts . key1 , xctx -> xts . key2 , EVP_CIPHER_CTX_iv_noconst ( ctx ) ) ;
 else if ( CRYPTO_xts128_encrypt ( & xctx -> xts , EVP_CIPHER_CTX_iv_noconst ( ctx ) , in , out , len , EVP_CIPHER_CTX_encrypting ( ctx ) ) ) return 0 ;
 return 1 ;
 }
 # define aes_xts_cleanup NULL # define XTS_FLAGS ( EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CUSTOM_IV \ | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT \ | EVP_CIPH_CUSTOM_COPY ) BLOCK_CIPHER_custom ( NID_aes , 128 , 1 , 16 , xts , XTS , XTS_FLAGS ) BLOCK_CIPHER_custom ( NID_aes , 256 , 1 , 16 , xts , XTS , XTS_FLAGS ) static int aes_ccm_ctrl ( EVP_CIPHER_CTX * c , int type , int arg , void * ptr ) {
 EVP_AES_CCM_CTX * cctx = EVP_C_DATA ( EVP_AES_CCM_CTX , c ) ;
 switch ( type ) {
 case EVP_CTRL_INIT : cctx -> key_set = 0 ;
 cctx -> iv_set = 0 ;
 cctx -> L = 8 ;
 cctx -> M = 12 ;
 cctx -> tag_set = 0 ;
 cctx -> len_set = 0 ;
 cctx -> tls_aad_len = - 1 ;
 return 1 ;
 case EVP_CTRL_AEAD_TLS1_AAD : if ( arg != EVP_AEAD_TLS1_AAD_LEN ) return 0 ;
 memcpy ( EVP_CIPHER_CTX_buf_noconst ( c ) , ptr , arg ) ;
 cctx -> tls_aad_len = arg ;
 {
 uint16_t len = EVP_CIPHER_CTX_buf_noconst ( c ) [ arg - 2 ] << 8 | EVP_CIPHER_CTX_buf_noconst ( c ) [ arg - 1 ] ;
 len -= EVP_CCM_TLS_EXPLICIT_IV_LEN ;
 if ( ! EVP_CIPHER_CTX_encrypting ( c ) ) len -= cctx -> M ;
 EVP_CIPHER_CTX_buf_noconst ( c ) [ arg - 2 ] = len >> 8 ;
 EVP_CIPHER_CTX_buf_noconst ( c ) [ arg - 1 ] = len & 0xff ;
 }
 return cctx -> M ;
 case EVP_CTRL_CCM_SET_IV_FIXED : if ( arg != EVP_CCM_TLS_FIXED_IV_LEN ) return 0 ;
 memcpy ( EVP_CIPHER_CTX_iv_noconst ( c ) , ptr , arg ) ;
 return 1 ;
 case EVP_CTRL_AEAD_SET_IVLEN : arg = 15 - arg ;
 case EVP_CTRL_CCM_SET_L : if ( arg < 2 || arg > 8 ) return 0 ;
 cctx -> L = arg ;
 return 1 ;
 case EVP_CTRL_AEAD_SET_TAG : if ( ( arg & 1 ) || arg < 4 || arg > 16 ) return 0 ;
 if ( EVP_CIPHER_CTX_encrypting ( c ) && ptr ) return 0 ;
 if ( ptr ) {
 cctx -> tag_set = 1 ;
 memcpy ( EVP_CIPHER_CTX_buf_noconst ( c ) , ptr , arg ) ;
 }
 cctx -> M = arg ;
 return 1 ;
 case EVP_CTRL_AEAD_GET_TAG : if ( ! EVP_CIPHER_CTX_encrypting ( c ) || ! cctx -> tag_set ) return 0 ;
 if ( ! CRYPTO_ccm128_tag ( & cctx -> ccm , ptr , ( size_t ) arg ) ) return 0 ;
 cctx -> tag_set = 0 ;
 cctx -> iv_set = 0 ;
 cctx -> len_set = 0 ;
 return 1 ;
 case EVP_CTRL_COPY : {
 EVP_CIPHER_CTX * out = ptr ;
 EVP_AES_CCM_CTX * cctx_out = EVP_C_DATA ( EVP_AES_CCM_CTX , out ) ;
 if ( cctx -> ccm . key ) {
 if ( cctx -> ccm . key != & cctx -> ks ) return 0 ;
 cctx_out -> ccm . key = & cctx_out -> ks ;
 }
 return 1 ;
 }
 default : return - 1 ;
 }
 }
 static int aes_ccm_init_key ( EVP_CIPHER_CTX * ctx , const unsigned char * key , const unsigned char * iv , int enc ) {
 EVP_AES_CCM_CTX * cctx = EVP_C_DATA ( EVP_AES_CCM_CTX , ctx ) ;
 if ( ! iv && ! key ) return 1 ;
 if ( key ) do {
 # ifdef HWAES_CAPABLE if ( HWAES_CAPABLE ) {
 HWAES_set_encrypt_key ( key , EVP_CIPHER_CTX_key_length ( ctx ) * 8 , & cctx -> ks . ks ) ;
 CRYPTO_ccm128_init ( & cctx -> ccm , cctx -> M , cctx -> L , & cctx -> ks , ( block128_f ) HWAES_encrypt ) ;
 cctx -> str = NULL ;
 cctx -> key_set = 1 ;
 break ;
 }
 else # endif # ifdef VPAES_CAPABLE if ( VPAES_CAPABLE ) {
 vpaes_set_encrypt_key ( key , EVP_CIPHER_CTX_key_length ( ctx ) * 8 , & cctx -> ks . ks ) ;
 CRYPTO_ccm128_init ( & cctx -> ccm , cctx -> M , cctx -> L , & cctx -> ks , ( block128_f ) vpaes_encrypt ) ;
 cctx -> str = NULL ;
 cctx -> key_set = 1 ;
 break ;
 }
 # endif AES_set_encrypt_key ( key , EVP_CIPHER_CTX_key_length ( ctx ) * 8 , & cctx -> ks . ks ) ;
 CRYPTO_ccm128_init ( & cctx -> ccm , cctx -> M , cctx -> L , & cctx -> ks , ( block128_f ) AES_encrypt ) ;
 cctx -> str = NULL ;
 cctx -> key_set = 1 ;
 }
 while ( 0 ) ;
 if ( iv ) {
 memcpy ( EVP_CIPHER_CTX_iv_noconst ( ctx ) , iv , 15 - cctx -> L ) ;
 cctx -> iv_set = 1 ;
 }
 return 1 ;
 }
 static int aes_ccm_tls_cipher ( EVP_CIPHER_CTX * ctx , unsigned char * out , const unsigned char * in , size_t len ) {
 EVP_AES_CCM_CTX * cctx = EVP_C_DATA ( EVP_AES_CCM_CTX , ctx ) ;
 CCM128_CONTEXT * ccm = & cctx -> ccm ;
 if ( out != in || len < ( EVP_CCM_TLS_EXPLICIT_IV_LEN + ( size_t ) cctx -> M ) ) return - 1 ;
 if ( EVP_CIPHER_CTX_encrypting ( ctx ) ) memcpy ( out , EVP_CIPHER_CTX_buf_noconst ( ctx ) , EVP_CCM_TLS_EXPLICIT_IV_LEN ) ;
 memcpy ( EVP_CIPHER_CTX_iv_noconst ( ctx ) + EVP_CCM_TLS_FIXED_IV_LEN , in , EVP_CCM_TLS_EXPLICIT_IV_LEN ) ;
 len -= EVP_CCM_TLS_EXPLICIT_IV_LEN + cctx -> M ;
 if ( CRYPTO_ccm128_setiv ( ccm , EVP_CIPHER_CTX_iv_noconst ( ctx ) , 15 - cctx -> L , len ) ) return - 1 ;
 CRYPTO_ccm128_aad ( ccm , EVP_CIPHER_CTX_buf_noconst ( ctx ) , cctx -> tls_aad_len ) ;
 in += EVP_CCM_TLS_EXPLICIT_IV_LEN ;
 out += EVP_CCM_TLS_EXPLICIT_IV_LEN ;
 if ( EVP_CIPHER_CTX_encrypting ( ctx ) ) {
 if ( cctx -> str ? CRYPTO_ccm128_encrypt_ccm64 ( ccm , in , out , len , cctx -> str ) : CRYPTO_ccm128_encrypt ( ccm , in , out , len ) ) return - 1 ;
 if ( ! CRYPTO_ccm128_tag ( ccm , out + len , cctx -> M ) ) return - 1 ;
 return len + EVP_CCM_TLS_EXPLICIT_IV_LEN + cctx -> M ;
 }
 else {
 if ( cctx -> str ? ! CRYPTO_ccm128_decrypt_ccm64 ( ccm , in , out , len , cctx -> str ) : ! CRYPTO_ccm128_decrypt ( ccm , in , out , len ) ) {
 unsigned char tag [ 16 ] ;
 if ( CRYPTO_ccm128_tag ( ccm , tag , cctx -> M ) ) {
 if ( ! CRYPTO_memcmp ( tag , in + len , cctx -> M ) ) return len ;
 }
 }
 OPENSSL_cleanse ( out , len ) ;
 return - 1 ;
 }
 }
 static int aes_ccm_cipher ( EVP_CIPHER_CTX * ctx , unsigned char * out , const unsigned char * in , size_t len ) {
 EVP_AES_CCM_CTX * cctx = EVP_C_DATA ( EVP_AES_CCM_CTX , ctx ) ;
 CCM128_CONTEXT * ccm = & cctx -> ccm ;
 if ( ! cctx -> key_set ) return - 1 ;
 if ( cctx -> tls_aad_len >= 0 ) return aes_ccm_tls_cipher ( ctx , out , in , len ) ;
 if ( ! cctx -> iv_set ) return - 1 ;
 if ( ! EVP_CIPHER_CTX_encrypting ( ctx ) && ! cctx -> tag_set ) return - 1 ;
 if ( ! out ) {
 if ( ! in ) {
 if ( CRYPTO_ccm128_setiv ( ccm , EVP_CIPHER_CTX_iv_noconst ( ctx ) , 15 - cctx -> L , len ) ) return - 1 ;
 cctx -> len_set = 1 ;
 return len ;
 }
 if ( ! cctx -> len_set && len ) return - 1 ;
 CRYPTO_ccm128_aad ( ccm , in , len ) ;
 return len ;
 }
 if ( ! in ) return 0 ;
 if ( ! cctx -> len_set ) {
 if ( CRYPTO_ccm128_setiv ( ccm , EVP_CIPHER_CTX_iv_noconst ( ctx ) , 15 - cctx -> L , len ) ) return - 1 ;
 cctx -> len_set = 1 ;
 }
 if ( EVP_CIPHER_CTX_encrypting ( ctx ) ) {
 if ( cctx -> str ? CRYPTO_ccm128_encrypt_ccm64 ( ccm , in , out , len , cctx -> str ) : CRYPTO_ccm128_encrypt ( ccm , in , out , len ) ) return - 1 ;
 cctx -> tag_set = 1 ;
 return len ;
 }
 else {
 int rv = - 1 ;
 if ( cctx -> str ? ! CRYPTO_ccm128_decrypt_ccm64 ( ccm , in , out , len , cctx -> str ) : ! CRYPTO_ccm128_decrypt ( ccm , in , out , len ) ) {
 unsigned char tag [ 16 ] ;
 if ( CRYPTO_ccm128_tag ( ccm , tag , cctx -> M ) ) {
 if ( ! CRYPTO_memcmp ( tag , EVP_CIPHER_CTX_buf_noconst ( ctx ) , cctx -> M ) ) rv = len ;
 }
 }
 if ( rv == - 1 ) OPENSSL_cleanse ( out , len ) ;
 cctx -> iv_set = 0 ;
 cctx -> tag_set = 0 ;
 cctx -> len_set = 0 ;
 return rv ;
 }
 }
 # define aes_ccm_cleanup NULL BLOCK_CIPHER_custom ( NID_aes , 128 , 1 , 12 , ccm , CCM , EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS ) BLOCK_CIPHER_custom ( NID_aes , 192 , 1 , 12 , ccm , CCM , EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS ) BLOCK_CIPHER_custom ( NID_aes , 256 , 1 , 12 , ccm , CCM , EVP_CIPH_FLAG_AEAD_CIPHER | CUSTOM_FLAGS )


int fscrypt_process_policy(struct inode *inode,
				const struct fscrypt_policy *policy)
{
	if (policy->version != 0)
		return -EINVAL;

	if (!inode_has_encryption_context(inode)) {
		if (!inode->i_sb->s_cop->empty_dir)
			return -EOPNOTSUPP;
		if (!inode->i_sb->s_cop->empty_dir(inode))
			return -ENOTEMPTY;
		return create_encryption_context_from_policy(inode, policy);
	}

	if (is_encryption_context_consistent_with_policy(inode, policy))
		return 0;

	printk(KERN_WARNING "%s: Policy inconsistent with encryption context\n",
	       __func__);
	return -EINVAL;
}


static inline int compute_score2 ( struct sock * sk , struct net * net , const struct in6_addr * saddr , __be16 sport , const struct in6_addr * daddr , unsigned short hnum , int dif ) {
 int score = - 1 ;
 if ( net_eq ( sock_net ( sk ) , net ) && udp_sk ( sk ) -> udp_port_hash == hnum && sk -> sk_family == PF_INET6 ) {
 struct ipv6_pinfo * np = inet6_sk ( sk ) ;
 struct inet_sock * inet = inet_sk ( sk ) ;
 if ( ! ipv6_addr_equal ( & np -> rcv_saddr , daddr ) ) return - 1 ;
 score = 0 ;
 if ( inet -> inet_dport ) {
 if ( inet -> inet_dport != sport ) return - 1 ;
 score ++ ;
 }
 if ( ! ipv6_addr_any ( & np -> daddr ) ) {
 if ( ! ipv6_addr_equal ( & np -> daddr , saddr ) ) return - 1 ;
 score ++ ;
 }
 if ( sk -> sk_bound_dev_if ) {
 if ( sk -> sk_bound_dev_if != dif ) return - 1 ;
 score ++ ;
 }
 }
 return score ;
 }


static int pnm_decode_frame ( AVCodecContext * avctx , void * data , int * got_frame , AVPacket * avpkt ) {
 const uint8_t * buf = avpkt -> data ;
 int buf_size = avpkt -> size ;
 PNMContext * const s = avctx -> priv_data ;
 AVFrame * picture = data ;
 AVFrame * const p = & s -> picture ;
 int i , j , n , linesize , h , upgrade = 0 ;
 unsigned char * ptr ;
 int components , sample_len , ret ;
 s -> bytestream_start = s -> bytestream = buf ;
 s -> bytestream_end = buf + buf_size ;
 if ( ( ret = ff_pnm_decode_header ( avctx , s ) ) < 0 ) return ret ;
 if ( p -> data [ 0 ] ) avctx -> release_buffer ( avctx , p ) ;
 p -> reference = 0 ;
 if ( ( ret = ff_get_buffer ( avctx , p ) ) < 0 ) {
 av_log ( avctx , AV_LOG_ERROR , "get_buffer() failed\n" ) ;
 return ret ;
 }
 p -> pict_type = AV_PICTURE_TYPE_I ;
 p -> key_frame = 1 ;
 switch ( avctx -> pix_fmt ) {
 default : return AVERROR ( EINVAL ) ;
 case AV_PIX_FMT_RGB48BE : n = avctx -> width * 6 ;
 components = 3 ;
 sample_len = 16 ;
 goto do_read ;
 case AV_PIX_FMT_RGB24 : n = avctx -> width * 3 ;
 components = 3 ;
 sample_len = 8 ;
 goto do_read ;
 case AV_PIX_FMT_GRAY8 : n = avctx -> width ;
 components = 1 ;
 sample_len = 8 ;
 if ( s -> maxval < 255 ) upgrade = 1 ;
 goto do_read ;
 case AV_PIX_FMT_GRAY16BE : case AV_PIX_FMT_GRAY16LE : n = avctx -> width * 2 ;
 components = 1 ;
 sample_len = 16 ;
 if ( s -> maxval < 65535 ) upgrade = 2 ;
 goto do_read ;
 case AV_PIX_FMT_MONOWHITE : case AV_PIX_FMT_MONOBLACK : n = ( avctx -> width + 7 ) >> 3 ;
 components = 1 ;
 sample_len = 1 ;
 do_read : ptr = p -> data [ 0 ] ;
 linesize = p -> linesize [ 0 ] ;
 if ( s -> bytestream + n * avctx -> height > s -> bytestream_end ) return AVERROR_INVALIDDATA ;
 if ( s -> type < 4 ) {
 for ( i = 0 ;
 i < avctx -> height ;
 i ++ ) {
 PutBitContext pb ;
 init_put_bits ( & pb , ptr , linesize ) ;
 for ( j = 0 ;
 j < avctx -> width * components ;
 j ++ ) {
 unsigned int c = 0 ;
 int v = 0 ;
 while ( s -> bytestream < s -> bytestream_end && ( * s -> bytestream < '0' || * s -> bytestream > '9' ) ) s -> bytestream ++ ;
 if ( s -> bytestream >= s -> bytestream_end ) return AVERROR_INVALIDDATA ;
 do {
 v = 10 * v + c ;
 c = ( * s -> bytestream ++ ) - '0' ;
 }
 while ( c <= 9 ) ;
 put_bits ( & pb , sample_len , ( ( ( 1 << sample_len ) - 1 ) * v + ( s -> maxval >> 1 ) ) / s -> maxval ) ;
 }
 flush_put_bits ( & pb ) ;
 ptr += linesize ;
 }
 }
 else {
 for ( i = 0 ;
 i < avctx -> height ;
 i ++ ) {
 if ( ! upgrade ) memcpy ( ptr , s -> bytestream , n ) ;
 else if ( upgrade == 1 ) {
 unsigned int j , f = ( 255 * 128 + s -> maxval / 2 ) / s -> maxval ;
 for ( j = 0 ;
 j < n ;
 j ++ ) ptr [ j ] = ( s -> bytestream [ j ] * f + 64 ) >> 7 ;
 }
 else if ( upgrade == 2 ) {
 unsigned int j , v , f = ( 65535 * 32768 + s -> maxval / 2 ) / s -> maxval ;
 for ( j = 0 ;
 j < n / 2 ;
 j ++ ) {
 v = av_be2ne16 ( ( ( uint16_t * ) s -> bytestream ) [ j ] ) ;
 ( ( uint16_t * ) ptr ) [ j ] = ( v * f + 16384 ) >> 15 ;
 }
 }
 s -> bytestream += n ;
 ptr += linesize ;
 }
 }
 break ;
 case AV_PIX_FMT_YUV420P : case AV_PIX_FMT_YUV420P9BE : case AV_PIX_FMT_YUV420P10BE : {
 unsigned char * ptr1 , * ptr2 ;
 n = avctx -> width ;
 ptr = p -> data [ 0 ] ;
 linesize = p -> linesize [ 0 ] ;
 if ( s -> maxval >= 256 ) n *= 2 ;
 if ( s -> bytestream + n * avctx -> height * 3 / 2 > s -> bytestream_end ) return AVERROR_INVALIDDATA ;
 for ( i = 0 ;
 i < avctx -> height ;
 i ++ ) {
 memcpy ( ptr , s -> bytestream , n ) ;
 s -> bytestream += n ;
 ptr += linesize ;
 }
 ptr1 = p -> data [ 1 ] ;
 ptr2 = p -> data [ 2 ] ;
 n >>= 1 ;
 h = avctx -> height >> 1 ;
 for ( i = 0 ;
 i < h ;
 i ++ ) {
 memcpy ( ptr1 , s -> bytestream , n ) ;
 s -> bytestream += n ;
 memcpy ( ptr2 , s -> bytestream , n ) ;
 s -> bytestream += n ;
 ptr1 += p -> linesize [ 1 ] ;
 ptr2 += p -> linesize [ 2 ] ;
 }
 }
 break ;
 case AV_PIX_FMT_YUV420P16 : {
 uint16_t * ptr1 , * ptr2 ;
 const int f = ( 65535 * 32768 + s -> maxval / 2 ) / s -> maxval ;
 unsigned int j , v ;
 n = avctx -> width * 2 ;
 ptr = p -> data [ 0 ] ;
 linesize = p -> linesize [ 0 ] ;
 if ( s -> bytestream + n * avctx -> height * 3 / 2 > s -> bytestream_end ) return AVERROR_INVALIDDATA ;
 for ( i = 0 ;
 i < avctx -> height ;
 i ++ ) {
 for ( j = 0 ;
 j < n / 2 ;
 j ++ ) {
 v = av_be2ne16 ( ( ( uint16_t * ) s -> bytestream ) [ j ] ) ;
 ( ( uint16_t * ) ptr ) [ j ] = ( v * f + 16384 ) >> 15 ;
 }
 s -> bytestream += n ;
 ptr += linesize ;
 }
 ptr1 = ( uint16_t * ) p -> data [ 1 ] ;
 ptr2 = ( uint16_t * ) p -> data [ 2 ] ;
 n >>= 1 ;
 h = avctx -> height >> 1 ;
 for ( i = 0 ;
 i < h ;
 i ++ ) {
 for ( j = 0 ;
 j < n / 2 ;
 j ++ ) {
 v = av_be2ne16 ( ( ( uint16_t * ) s -> bytestream ) [ j ] ) ;
 ptr1 [ j ] = ( v * f + 16384 ) >> 15 ;
 }
 s -> bytestream += n ;
 for ( j = 0 ;
 j < n / 2 ;
 j ++ ) {
 v = av_be2ne16 ( ( ( uint16_t * ) s -> bytestream ) [ j ] ) ;
 ptr2 [ j ] = ( v * f + 16384 ) >> 15 ;
 }
 s -> bytestream += n ;
 ptr1 += p -> linesize [ 1 ] / 2 ;
 ptr2 += p -> linesize [ 2 ] / 2 ;
 }
 }
 break ;
 case AV_PIX_FMT_RGB32 : ptr = p -> data [ 0 ] ;
 linesize = p -> linesize [ 0 ] ;
 if ( s -> bytestream + avctx -> width * avctx -> height * 4 > s -> bytestream_end ) return AVERROR_INVALIDDATA ;
 for ( i = 0 ;
 i < avctx -> height ;
 i ++ ) {
 int j , r , g , b , a ;
 for ( j = 0 ;
 j < avctx -> width ;
 j ++ ) {
 r = * s -> bytestream ++ ;
 g = * s -> bytestream ++ ;
 b = * s -> bytestream ++ ;
 a = * s -> bytestream ++ ;
 ( ( uint32_t * ) ptr ) [ j ] = ( a << 24 ) | ( r << 16 ) | ( g << 8 ) | b ;
 }
 ptr += linesize ;
 }
 break ;
 }
 * picture = s -> picture ;
 * got_frame = 1 ;
 return s -> bytestream - s -> bytestream_start ;
 }


static int ide_write_dma_cb(IDEState *s, target_phys_addr_t phys_addr, int transfer_size1) { int len, transfer_size, n; int64_t sector_num; transfer_size = transfer_size1; for(;;) { len = s->io_buffer_size - s->io_buffer_index; if (len == 0) { n = s->io_buffer_size >> 9; sector_num = ide_get_sector(s); bdrv_write(s->bs, sector_num, s->io_buffer, s->io_buffer_size >> 9); sector_num += n; ide_set_sector(s, sector_num); s->nsector -= n; n = s->nsector; if (n == 0) { /* end of transfer */ s->status = READY_STAT | SEEK_STAT; ide_set_irq(s); return 0; } if (n > MAX_MULT_SECTORS) n = MAX_MULT_SECTORS; s->io_buffer_index = 0; s->io_buffer_size = n * 512; len = s->io_buffer_size; } if (transfer_size <= 0) break; if (len > transfer_size) len = transfer_size; cpu_physical_memory_read(phys_addr, s->io_buffer + s->io_buffer_index, len); s->io_buffer_index += len; transfer_size -= len; phys_addr += len; } return transfer_size1 - transfer_size; }


int qemu_lock_fd_test(int fd, int64_t start, int64_t len, bool exclusive) { int ret; struct flock fl = { .l_whence = SEEK_SET, .l_start = start, .l_len = len, .l_type = exclusive ? F_WRLCK : F_RDLCK, }; ret = fcntl(fd, QEMU_GETLK, &fl); if (ret == -1) { return -errno; } else { return fl.l_type == F_UNLCK ? 0 : -EAGAIN; } }


hb_shape_plan_t * hb_shape_plan_get_empty ( void ) {
 static const hb_shape_plan_t _hb_shape_plan_nil = {
 HB_OBJECT_HEADER_STATIC , true , NULL , HB_SEGMENT_PROPERTIES_DEFAULT , NULL , NULL , NULL , 0 , {
 # define HB_SHAPER_IMPLEMENT ( shaper ) HB_SHAPER_DATA_INVALID , # include "hb-shaper-list.hh" # undef HB_SHAPER_IMPLEMENT }
 }
 ;
 return const_cast < hb_shape_plan_t * > ( & _hb_shape_plan_nil ) ;
 }


static int swri_resample(ResampleContext *c, uint8_t *dst, const uint8_t *src, int *consumed, int src_size, int dst_size, int update_ctx) { int fn_idx = c->format - AV_SAMPLE_FMT_S16P; if (c->filter_length == 1 && c->phase_shift == 0) { int index= c->index; int frac= c->frac; int64_t index2= (1LL<<32)*c->frac/c->src_incr + (1LL<<32)*index; int64_t incr= (1LL<<32) * c->dst_incr / c->src_incr; int new_size = (src_size * (int64_t)c->src_incr - frac + c->dst_incr - 1) / c->dst_incr; dst_size= FFMIN(dst_size, new_size); c->dsp.resample_one[fn_idx](dst, src, dst_size, index2, incr); index += dst_size * c->dst_incr_div; index += (frac + dst_size * (int64_t)c->dst_incr_mod) / c->src_incr; av_assert2(index >= 0); *consumed= index; if (update_ctx) { c->frac = (frac + dst_size * (int64_t)c->dst_incr_mod) % c->src_incr; c->index = 0; } } else { int64_t end_index = (1LL + src_size - c->filter_length) << c->phase_shift; int64_t delta_frac = (end_index - c->index) * c->src_incr - c->frac; int delta_n = (delta_frac + c->dst_incr - 1) / c->dst_incr; dst_size = FFMIN(dst_size, delta_n); if (dst_size > 0) { if (!c->linear) { *consumed = c->dsp.resample_common[fn_idx](c, dst, src, dst_size, update_ctx); } else { *consumed = c->dsp.resample_linear[fn_idx](c, dst, src, dst_size, update_ctx); } } else { *consumed = 0; } } return dst_size; }


static int lxc_mount_auto_mounts(struct lxc_conf *conf, int flags, struct lxc_handler *handler)
{
	int r;
	size_t i;
	static struct {
		int match_mask;
		int match_flag;
		const char *source;
		const char *destination;
		const char *fstype;
		unsigned long flags;
		const char *options;
	} default_mounts[] = {
		/* Read-only bind-mounting... In older kernels, doing that required
		 * to do one MS_BIND mount and then MS_REMOUNT|MS_RDONLY the same
		 * one. According to mount(2) manpage, MS_BIND honors MS_RDONLY from
		 * kernel 2.6.26 onwards. However, this apparently does not work on
		 * kernel 3.8. Unfortunately, on that very same kernel, doing the
		 * same trick as above doesn't seem to work either, there one needs
		 * to ALSO specify MS_BIND for the remount, otherwise the entire
		 * fs is remounted read-only or the mount fails because it's busy...
		 * MS_REMOUNT|MS_BIND|MS_RDONLY seems to work for kernels as low as
		 * 2.6.32...
		 */
		{ LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "proc",                                              "%r/proc",                      "proc",     MS_NODEV|MS_NOEXEC|MS_NOSUID,   NULL },
		{ LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "%r/proc/sys/net",                                   "%r/proc/net",                  NULL,       MS_BIND,                        NULL },
		{ LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "%r/proc/sys",                                       "%r/proc/sys",                  NULL,       MS_BIND,                        NULL },
		{ LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, NULL,                                                "%r/proc/sys",                  NULL,       MS_REMOUNT|MS_BIND|MS_RDONLY,   NULL },
		{ LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "%r/proc/net",                                       "%r/proc/sys/net",              NULL,       MS_MOVE,                        NULL },
		{ LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, "%r/proc/sysrq-trigger",                             "%r/proc/sysrq-trigger",        NULL,       MS_BIND,                        NULL },
		{ LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED, NULL,                                                "%r/proc/sysrq-trigger",        NULL,       MS_REMOUNT|MS_BIND|MS_RDONLY,   NULL },
		{ LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_RW,    "proc",                                              "%r/proc",                      "proc",     MS_NODEV|MS_NOEXEC|MS_NOSUID,   NULL },
		{ LXC_AUTO_SYS_MASK,  LXC_AUTO_SYS_RW,     "sysfs",                                             "%r/sys",                       "sysfs",    0,                              NULL },
		{ LXC_AUTO_SYS_MASK,  LXC_AUTO_SYS_RO,     "sysfs",                                             "%r/sys",                       "sysfs",    MS_RDONLY,                      NULL },
		{ LXC_AUTO_SYS_MASK,  LXC_AUTO_SYS_MIXED,  "sysfs",                                             "%r/sys",                       "sysfs",    MS_NODEV|MS_NOEXEC|MS_NOSUID,   NULL },
		{ LXC_AUTO_SYS_MASK,  LXC_AUTO_SYS_MIXED,  "%r/sys",                                            "%r/sys",                       NULL,       MS_BIND,                        NULL },
		{ LXC_AUTO_SYS_MASK,  LXC_AUTO_SYS_MIXED,  NULL,                                                "%r/sys",                       NULL,       MS_REMOUNT|MS_BIND|MS_RDONLY,   NULL },
		{ LXC_AUTO_SYS_MASK,  LXC_AUTO_SYS_MIXED,  "sysfs",                                             "%r/sys/devices/virtual/net",   "sysfs",    0,                              NULL },
		{ LXC_AUTO_SYS_MASK,  LXC_AUTO_SYS_MIXED,  "%r/sys/devices/virtual/net/devices/virtual/net",    "%r/sys/devices/virtual/net",   NULL,       MS_BIND,                        NULL },
		{ LXC_AUTO_SYS_MASK,  LXC_AUTO_SYS_MIXED,  NULL,                                                "%r/sys/devices/virtual/net",   NULL,       MS_REMOUNT|MS_BIND|MS_NOSUID|MS_NODEV|MS_NOEXEC,   NULL },
		{ 0,                  0,                   NULL,                                                NULL,                           NULL,       0,                              NULL }
	};

	for (i = 0; default_mounts[i].match_mask; i++) {
		if ((flags & default_mounts[i].match_mask) == default_mounts[i].match_flag) {
			char *source = NULL;
			char *destination = NULL;
			int saved_errno;
			unsigned long mflags;

			if (default_mounts[i].source) {
				/* will act like strdup if %r is not present */
				source = lxc_string_replace("%r", conf->rootfs.path ? conf->rootfs.mount : "", default_mounts[i].source);
				if (!source) {
					SYSERROR("memory allocation error");
					return -1;
				}
			}
			if (default_mounts[i].destination) {
				/* will act like strdup if %r is not present */
				destination = lxc_string_replace("%r", conf->rootfs.path ? conf->rootfs.mount : "", default_mounts[i].destination);
				if (!destination) {
					saved_errno = errno;
					SYSERROR("memory allocation error");
					free(source);
					errno = saved_errno;
					return -1;
				}
			}
			mflags = add_required_remount_flags(source, destination,
					default_mounts[i].flags);
			r = mount(source, destination, default_mounts[i].fstype, mflags, default_mounts[i].options);
			saved_errno = errno;
			if (r < 0 && errno == ENOENT) {
				INFO("Mount source or target for %s on %s doesn't exist. Skipping.", source, destination);
				r = 0;
			}
			else if (r < 0)
				SYSERROR("error mounting %s on %s flags %lu", source, destination, mflags);

			free(source);
			free(destination);
			if (r < 0) {
				errno = saved_errno;
				return -1;
			}
		}
	}

	if (flags & LXC_AUTO_CGROUP_MASK) {
		int cg_flags;

		cg_flags = flags & LXC_AUTO_CGROUP_MASK;
		/* If the type of cgroup mount was not specified, it depends on the
		 * container's capabilities as to what makes sense: if we have
		 * CAP_SYS_ADMIN, the read-only part can be remounted read-write
		 * anyway, so we may as well default to read-write; then the admin
		 * will not be given a false sense of security. (And if they really
		 * want mixed r/o r/w, then they can explicitly specify :mixed.)
		 * OTOH, if the container lacks CAP_SYS_ADMIN, do only default to
		 * :mixed, because then the container can't remount it read-write. */
		if (cg_flags == LXC_AUTO_CGROUP_NOSPEC || cg_flags == LXC_AUTO_CGROUP_FULL_NOSPEC) {
			int has_sys_admin = 0;
			if (!lxc_list_empty(&conf->keepcaps)) {
				has_sys_admin = in_caplist(CAP_SYS_ADMIN, &conf->keepcaps);
			} else {
				has_sys_admin = !in_caplist(CAP_SYS_ADMIN, &conf->caps);
			}
			if (cg_flags == LXC_AUTO_CGROUP_NOSPEC) {
				cg_flags = has_sys_admin ? LXC_AUTO_CGROUP_RW : LXC_AUTO_CGROUP_MIXED;
			} else {
				cg_flags = has_sys_admin ? LXC_AUTO_CGROUP_FULL_RW : LXC_AUTO_CGROUP_FULL_MIXED;
			}
		}

		if (!cgroup_mount(conf->rootfs.path ? conf->rootfs.mount : "", handler, cg_flags)) {
			SYSERROR("error mounting /sys/fs/cgroup");
			return -1;
		}
	}

	return 0;
}


static void test_qga_fstrim(gconstpointer fix) { const TestFixture *fixture = fix; QDict *ret; QList *list; const QListEntry *entry; ret = qmp_fd(fixture->fd, "{'execute': 'guest-fstrim'," " arguments: { minimum: 4194304 } }"); g_assert_nonnull(ret); qmp_assert_no_error(ret); list = qdict_get_qlist(ret, "return"); entry = qlist_first(list); g_assert(qdict_haskey(qobject_to_qdict(entry->value), "paths")); QDECREF(ret); }


static int dissect_h245_G729Extensions ( tvbuff_t * tvb _U_ , int offset _U_ , asn1_ctx_t * actx _U_ , proto_tree * tree _U_ , int hf_index _U_ ) {
 offset = dissect_per_sequence ( tvb , offset , actx , tree , hf_index , ett_h245_G729Extensions , G729Extensions_sequence ) ;
 return offset ;
 }


header_put_marker (SF_PRIVATE *psf, int x)
{	if (psf->headindex < SIGNED_SIZEOF (psf->header) - 4)
	{	psf->header [psf->headindex++] = x ;
		psf->header [psf->headindex++] = (x >> 8) ;
		psf->header [psf->headindex++] = (x >> 16) ;
		psf->header [psf->headindex++] = (x >> 24) ;
		} ;
} /* header_put_marker */


static void write_strip_header(CinepakEncContext *s, int y, int h, int keyframe, unsigned char *buf, int strip_size) { buf[0] = keyframe ? 0x11: 0x10; AV_WB24(&buf[1], strip_size + STRIP_HEADER_SIZE); AV_WB16(&buf[4], y); AV_WB16(&buf[6], 0); AV_WB16(&buf[8], h); AV_WB16(&buf[10], s->w); }


static cmsTagTypeSignature DecideTextType ( cmsFloat64Number ICCVersion , const void * Data ) {
 if ( ICCVersion >= 4.0 ) return cmsSigMultiLocalizedUnicodeType ;
 return cmsSigTextType ;
 cmsUNUSED_PARAMETER ( Data ) ;
 }


int mount_proc_if_needed(const char *rootfs)
{
	char path[MAXPATHLEN];
	char link[20];
	int linklen, ret;
	int mypid;

	ret = snprintf(path, MAXPATHLEN, "%s/proc/self", rootfs);
	if (ret < 0 || ret >= MAXPATHLEN) {
		SYSERROR("proc path name too long");
		return -1;
	}
	memset(link, 0, 20);
	linklen = readlink(path, link, 20);
	mypid = (int)getpid();
	INFO("I am %d, /proc/self points to '%s'", mypid, link);
	ret = snprintf(path, MAXPATHLEN, "%s/proc", rootfs);
	if (ret < 0 || ret >= MAXPATHLEN) {
		SYSERROR("proc path name too long");
		return -1;
	}
	if (linklen < 0) /* /proc not mounted */
		goto domount;
	if (atoi(link) != mypid) {
		/* wrong /procs mounted */
		umount2(path, MNT_DETACH); /* ignore failure */
		goto domount;
	}
	/* the right proc is already mounted */
	return 0;

domount:
	if (mount("proc", path, "proc", 0, NULL))
		return -1;
	INFO("Mounted /proc in container for security transition");
	return 1;
}


static proto_item * proto_tree_add_text_node ( proto_tree * tree , tvbuff_t * tvb , gint start , gint length ) {
 proto_item * pi ;
 if ( tree == NULL ) return NULL ;
 pi = proto_tree_add_pi ( tree , & hfi_text_only , tvb , start , & length ) ;
 return pi ;
 }


static void _UTF7ToUnicodeWithOffsets ( UConverterToUnicodeArgs * pArgs , UErrorCode * pErrorCode ) {
 UConverter * cnv ;
 const uint8_t * source , * sourceLimit ;
 UChar * target ;
 const UChar * targetLimit ;
 int32_t * offsets ;
 uint8_t * bytes ;
 uint8_t byteIndex ;
 int32_t length , targetCapacity ;
 uint16_t bits ;
 int8_t base64Counter ;
 UBool inDirectMode ;
 int8_t base64Value ;
 int32_t sourceIndex , nextSourceIndex ;
 uint8_t b ;
 cnv = pArgs -> converter ;
 source = ( const uint8_t * ) pArgs -> source ;
 sourceLimit = ( const uint8_t * ) pArgs -> sourceLimit ;
 target = pArgs -> target ;
 targetLimit = pArgs -> targetLimit ;
 offsets = pArgs -> offsets ;
 {
 uint32_t status = cnv -> toUnicodeStatus ;
 inDirectMode = ( UBool ) ( ( status >> 24 ) & 1 ) ;
 base64Counter = ( int8_t ) ( status >> 16 ) ;
 bits = ( uint16_t ) status ;
 }
 bytes = cnv -> toUBytes ;
 byteIndex = cnv -> toULength ;
 sourceIndex = byteIndex == 0 ? 0 : - 1 ;
 nextSourceIndex = 0 ;
 if ( inDirectMode ) {
 directMode : byteIndex = 0 ;
 length = ( int32_t ) ( sourceLimit - source ) ;
 targetCapacity = ( int32_t ) ( targetLimit - target ) ;
 if ( length > targetCapacity ) {
 length = targetCapacity ;
 }
 while ( length > 0 ) {
 b = * source ++ ;
 if ( ! isLegalUTF7 ( b ) ) {
 bytes [ 0 ] = b ;
 byteIndex = 1 ;
 * pErrorCode = U_ILLEGAL_CHAR_FOUND ;
 break ;
 }
 else if ( b != PLUS ) {
 * target ++ = b ;
 if ( offsets != NULL ) {
 * offsets ++ = sourceIndex ++ ;
 }
 }
 else {
 nextSourceIndex = ++ sourceIndex ;
 inDirectMode = FALSE ;
 byteIndex = 0 ;
 bits = 0 ;
 base64Counter = - 1 ;
 goto unicodeMode ;
 }
 -- length ;
 }
 if ( source < sourceLimit && target >= targetLimit ) {
 * pErrorCode = U_BUFFER_OVERFLOW_ERROR ;
 }
 }
 else {
 unicodeMode : while ( source < sourceLimit ) {
 if ( target < targetLimit ) {
 bytes [ byteIndex ++ ] = b = * source ++ ;
 ++ nextSourceIndex ;
 base64Value = - 3 ;
 if ( b >= 126 || ( base64Value = fromBase64 [ b ] ) == - 3 || base64Value == - 1 ) {
 inDirectMode = TRUE ;
 if ( base64Counter == - 1 ) {
 -- source ;
 bytes [ 0 ] = PLUS ;
 byteIndex = 1 ;
 * pErrorCode = U_ILLEGAL_CHAR_FOUND ;
 break ;
 }
 else if ( bits != 0 ) {
 -- source ;
 -- byteIndex ;
 * pErrorCode = U_ILLEGAL_CHAR_FOUND ;
 break ;
 }
 else {
 if ( base64Value == - 3 ) {
 * pErrorCode = U_ILLEGAL_CHAR_FOUND ;
 break ;
 }
 else {
 -- source ;
 sourceIndex = nextSourceIndex - 1 ;
 goto directMode ;
 }
 }
 }
 else if ( base64Value >= 0 ) {
 switch ( base64Counter ) {
 case - 1 : case 0 : bits = base64Value ;
 base64Counter = 1 ;
 break ;
 case 1 : case 3 : case 4 : case 6 : bits = ( uint16_t ) ( ( bits << 6 ) | base64Value ) ;
 ++ base64Counter ;
 break ;
 case 2 : * target ++ = ( UChar ) ( ( bits << 4 ) | ( base64Value >> 2 ) ) ;
 if ( offsets != NULL ) {
 * offsets ++ = sourceIndex ;
 sourceIndex = nextSourceIndex - 1 ;
 }
 bytes [ 0 ] = b ;
 byteIndex = 1 ;
 bits = ( uint16_t ) ( base64Value & 3 ) ;
 base64Counter = 3 ;
 break ;
 case 5 : * target ++ = ( UChar ) ( ( bits << 2 ) | ( base64Value >> 4 ) ) ;
 if ( offsets != NULL ) {
 * offsets ++ = sourceIndex ;
 sourceIndex = nextSourceIndex - 1 ;
 }
 bytes [ 0 ] = b ;
 byteIndex = 1 ;
 bits = ( uint16_t ) ( base64Value & 15 ) ;
 base64Counter = 6 ;
 break ;
 case 7 : * target ++ = ( UChar ) ( ( bits << 6 ) | base64Value ) ;
 if ( offsets != NULL ) {
 * offsets ++ = sourceIndex ;
 sourceIndex = nextSourceIndex ;
 }
 byteIndex = 0 ;
 bits = 0 ;
 base64Counter = 0 ;
 break ;
 default : break ;
 }
 }
 else {
 inDirectMode = TRUE ;
 if ( base64Counter == - 1 ) {
 * target ++ = PLUS ;
 if ( offsets != NULL ) {
 * offsets ++ = sourceIndex - 1 ;
 }
 }
 else {
 if ( bits != 0 ) {
 * pErrorCode = U_ILLEGAL_CHAR_FOUND ;
 break ;
 }
 }
 sourceIndex = nextSourceIndex ;
 goto directMode ;
 }
 }
 else {
 * pErrorCode = U_BUFFER_OVERFLOW_ERROR ;
 break ;
 }
 }
 }
 if ( U_SUCCESS ( * pErrorCode ) && pArgs -> flush && source == sourceLimit && bits == 0 ) {
 byteIndex = 0 ;
 }
 cnv -> toUnicodeStatus = ( ( uint32_t ) inDirectMode << 24 ) | ( ( uint32_t ) ( ( uint8_t ) base64Counter ) << 16 ) | ( uint32_t ) bits ;
 cnv -> toULength = byteIndex ;
 pArgs -> source = ( const char * ) source ;
 pArgs -> target = target ;
 pArgs -> offsets = offsets ;
 return ;
 }


static struct mskwajd_header * kwajd_open ( struct mskwaj_decompressor * base , const char * filename ) {
 struct mskwaj_decompressor_p * self = ( struct mskwaj_decompressor_p * ) base ;
 struct mskwajd_header * hdr ;
 struct mspack_system * sys ;
 struct mspack_file * fh ;
 if ( ! self ) return NULL ;
 sys = self -> system ;
 fh = sys -> open ( sys , filename , MSPACK_SYS_OPEN_READ ) ;
 hdr = ( struct mskwajd_header * ) sys -> alloc ( sys , sizeof ( struct mskwajd_header_p ) ) ;
 if ( fh && hdr ) {
 ( ( struct mskwajd_header_p * ) hdr ) -> fh = fh ;
 self -> error = kwajd_read_headers ( sys , fh , hdr ) ;
 }
 else {
 if ( ! fh ) self -> error = MSPACK_ERR_OPEN ;
 if ( ! hdr ) self -> error = MSPACK_ERR_NOMEMORY ;
 }
 if ( self -> error ) {
 if ( fh ) sys -> close ( fh ) ;
 if ( hdr ) sys -> free ( hdr ) ;
 hdr = NULL ;
 }
 return hdr ;
 }


x11grab_read_packet(AVFormatContext *s1, AVPacket *pkt) { struct x11_grab *s = s1->priv_data; Display *dpy = s->dpy; XImage *image = s->image; int x_off = s->x_off; int y_off = s->y_off; int64_t curtime, delay; struct timespec ts; /* Calculate the time of the next frame */ s->time_frame += INT64_C(1000000); /* wait based on the frame rate */ for(;;) { curtime = av_gettime(); delay = s->time_frame * av_q2d(s->time_base) - curtime; if (delay <= 0) { if (delay < INT64_C(-1000000) * av_q2d(s->time_base)) { s->time_frame += INT64_C(1000000); } break; } ts.tv_sec = delay / 1000000; ts.tv_nsec = (delay % 1000000) * 1000; nanosleep(&ts, NULL); } av_init_packet(pkt); pkt->data = image->data; pkt->size = s->frame_size; pkt->pts = curtime; if(s->use_shm) { if (!XShmGetImage(dpy, RootWindow(dpy, DefaultScreen(dpy)), image, x_off, y_off, AllPlanes)) { av_log (s1, AV_LOG_INFO, "XShmGetImage() failed\n"); } } else { if (!xget_zpixmap(dpy, RootWindow(dpy, DefaultScreen(dpy)), image, x_off, y_off)) { av_log (s1, AV_LOG_INFO, "XGetZPixmap() failed\n"); } } if(!s->nomouse){ paint_mouse_pointer(image, s); } return s->frame_size; }


POWERPC_FAMILY(POWER7P)(ObjectClass *oc, void *data) { DeviceClass *dc = DEVICE_CLASS(oc); PowerPCCPUClass *pcc = POWERPC_CPU_CLASS(oc); dc->fw_name = "PowerPC,POWER7+"; dc->desc = "POWER7+"; pcc->pvr = CPU_POWERPC_POWER7P_BASE; pcc->pvr_mask = CPU_POWERPC_POWER7P_MASK; pcc->init_proc = init_proc_POWER7; pcc->check_pow = check_pow_nocheck; pcc->insns_flags = PPC_INSNS_BASE | PPC_ISEL | PPC_STRING | PPC_MFTB | PPC_FLOAT | PPC_FLOAT_FSEL | PPC_FLOAT_FRES | PPC_FLOAT_FSQRT | PPC_FLOAT_FRSQRTE | PPC_FLOAT_FRSQRTES | PPC_FLOAT_STFIWX | PPC_FLOAT_EXT | PPC_CACHE | PPC_CACHE_ICBI | PPC_CACHE_DCBZ | PPC_MEM_SYNC | PPC_MEM_EIEIO | PPC_MEM_TLBIE | PPC_MEM_TLBSYNC | PPC_64B | PPC_ALTIVEC | PPC_SEGMENT_64B | PPC_SLBI | PPC_POPCNTB | PPC_POPCNTWD; pcc->insns_flags2 = PPC2_VSX | PPC2_DFP | PPC2_DBRX | PPC2_ISA205 | PPC2_PERM_ISA206 | PPC2_DIVE_ISA206 | PPC2_ATOMIC_ISA206 | PPC2_FP_CVT_ISA206 | PPC2_FP_TST_ISA206; pcc->msr_mask = (1ull << MSR_SF) | (1ull << MSR_VR) | (1ull << MSR_VSX) | (1ull << MSR_EE) | (1ull << MSR_PR) | (1ull << MSR_FP) | (1ull << MSR_ME) | (1ull << MSR_FE0) | (1ull << MSR_SE) | (1ull << MSR_DE) | (1ull << MSR_FE1) | (1ull << MSR_IR) | (1ull << MSR_DR) | (1ull << MSR_PMM) | (1ull << MSR_RI) | (1ull << MSR_LE); pcc->mmu_model = POWERPC_MMU_2_06; #if defined(CONFIG_SOFTMMU) pcc->handle_mmu_fault = ppc_hash64_handle_mmu_fault; #endif pcc->excp_model = POWERPC_EXCP_POWER7; pcc->bus_model = PPC_FLAGS_INPUT_POWER7; pcc->bfd_mach = bfd_mach_ppc64; pcc->flags = POWERPC_FLAG_VRE | POWERPC_FLAG_SE | POWERPC_FLAG_BE | POWERPC_FLAG_PMM | POWERPC_FLAG_BUS_CLK | POWERPC_FLAG_CFAR | POWERPC_FLAG_VSX; pcc->l1_dcache_size = 0x8000; pcc->l1_icache_size = 0x8000; pcc->interrupts_big_endian = ppc_cpu_interrupts_big_endian_lpcr; }


bool contain_volatile_functions ( Node * clause ) {
 return contain_volatile_functions_walker ( clause , NULL ) ;
 }


static inline uint16_t mipsdsp_lshift16(uint16_t a, uint8_t s, CPUMIPSState *env) { uint8_t sign; uint16_t discard; if (s == 0) { return a; } else { sign = (a >> 15) & 0x01; if (sign != 0) { discard = (((0x01 << (16 - s)) - 1) << s) | ((a >> (14 - (s - 1))) & ((0x01 << s) - 1)); } else { discard = a >> (14 - (s - 1)); } if ((discard != 0x0000) && (discard != 0xFFFF)) { set_DSPControl_overflow_flag(1, 22, env); } return a << s; } }


static gboolean prplcb_ev_remove ( guint id ) {
 b_event_remove ( ( gint ) id ) ;
 return TRUE ;
 }


static gboolean gst_asf_demux_handle_seek_event ( GstASFDemux * demux , GstEvent * event ) {
 GstClockTime idx_time ;
 GstSegment segment ;
 GstSeekFlags flags ;
 GstSeekType cur_type , stop_type ;
 GstFormat format ;
 gboolean only_need_update ;
 gboolean after , before , next ;
 gboolean flush ;
 gdouble rate ;
 gint64 cur , stop ;
 gint64 seek_time ;
 guint packet , speed_count = 1 ;
 gboolean eos ;
 guint32 seqnum ;
 GstEvent * fevent ;
 gint i ;
 gst_event_parse_seek ( event , & rate , & format , & flags , & cur_type , & cur , & stop_type , & stop ) ;
 if ( G_UNLIKELY ( format != GST_FORMAT_TIME ) ) {
 GST_LOG_OBJECT ( demux , "seeking is only supported in TIME format" ) ;
 return FALSE ;
 }
 if ( gst_pad_push_event ( demux -> sinkpad , gst_event_ref ( event ) ) ) return TRUE ;
 if ( G_UNLIKELY ( demux -> seekable == FALSE || demux -> packet_size == 0 || demux -> num_packets == 0 || demux -> play_time == 0 ) ) {
 GST_LOG_OBJECT ( demux , "stream is not seekable" ) ;
 return FALSE ;
 }
 if ( G_UNLIKELY ( ! demux -> activated_streams ) ) {
 GST_LOG_OBJECT ( demux , "streams not yet activated, ignoring seek" ) ;
 return FALSE ;
 }
 if ( G_UNLIKELY ( rate <= 0.0 ) ) {
 GST_LOG_OBJECT ( demux , "backward playback" ) ;
 demux -> seek_to_cur_pos = TRUE ;
 for ( i = 0 ;
 i < demux -> num_streams ;
 i ++ ) {
 demux -> stream [ i ] . reverse_kf_ready = FALSE ;
 }
 }
 seqnum = gst_event_get_seqnum ( event ) ;
 flush = ( ( flags & GST_SEEK_FLAG_FLUSH ) == GST_SEEK_FLAG_FLUSH ) ;
 demux -> accurate = ( ( flags & GST_SEEK_FLAG_ACCURATE ) == GST_SEEK_FLAG_ACCURATE ) ;
 demux -> keyunit_sync = ( ( flags & GST_SEEK_FLAG_KEY_UNIT ) == GST_SEEK_FLAG_KEY_UNIT ) ;
 after = ( ( flags & GST_SEEK_FLAG_SNAP_AFTER ) == GST_SEEK_FLAG_SNAP_AFTER ) ;
 before = ( ( flags & GST_SEEK_FLAG_SNAP_BEFORE ) == GST_SEEK_FLAG_SNAP_BEFORE ) ;
 next = after && ! before ;
 if ( G_UNLIKELY ( demux -> streaming ) ) {
 if ( ! flush ) {
 GST_LOG_OBJECT ( demux , "streaming;
 non-flushing seek not supported" ) ;
 return FALSE ;
 }
 if ( stop_type != GST_SEEK_TYPE_NONE && ( stop_type != GST_SEEK_TYPE_SET || GST_CLOCK_TIME_IS_VALID ( stop ) ) ) {
 GST_LOG_OBJECT ( demux , "streaming;
 end position must be NONE" ) ;
 return FALSE ;
 }
 return gst_asf_demux_handle_seek_push ( demux , event ) ;
 }
 if ( G_LIKELY ( flush ) ) {
 fevent = gst_event_new_flush_start ( ) ;
 gst_event_set_seqnum ( fevent , seqnum ) ;
 gst_pad_push_event ( demux -> sinkpad , gst_event_ref ( fevent ) ) ;
 gst_asf_demux_send_event_unlocked ( demux , fevent ) ;
 }
 else {
 gst_pad_pause_task ( demux -> sinkpad ) ;
 }
 GST_PAD_STREAM_LOCK ( demux -> sinkpad ) ;
 fevent = gst_event_new_flush_stop ( TRUE ) ;
 gst_event_set_seqnum ( fevent , seqnum ) ;
 gst_pad_push_event ( demux -> sinkpad , gst_event_ref ( fevent ) ) ;
 if ( G_LIKELY ( flush ) ) gst_asf_demux_send_event_unlocked ( demux , fevent ) ;
 else gst_event_unref ( fevent ) ;
 segment = demux -> segment ;
 if ( G_UNLIKELY ( demux -> segment_running && ! flush ) ) {
 GstSegment newsegment ;
 GstEvent * newseg ;
 gst_segment_copy_into ( & segment , & newsegment ) ;
 newseg = gst_event_new_segment ( & newsegment ) ;
 gst_event_set_seqnum ( newseg , seqnum ) ;
 gst_asf_demux_send_event_unlocked ( demux , newseg ) ;
 }
 gst_segment_do_seek ( & segment , rate , format , flags , cur_type , cur , stop_type , stop , & only_need_update ) ;
 GST_DEBUG_OBJECT ( demux , "seeking to time %" GST_TIME_FORMAT ", segment: " "%" GST_SEGMENT_FORMAT , GST_TIME_ARGS ( segment . start ) , & segment ) ;
 if ( cur_type != GST_SEEK_TYPE_SET ) seek_time = segment . start ;
 else seek_time = cur ;
 if ( G_UNLIKELY ( ! gst_asf_demux_seek_index_lookup ( demux , & packet , seek_time , & idx_time , & speed_count , next , & eos ) ) ) {
 gint64 offset ;
 if ( eos ) {
 demux -> packet = demux -> num_packets ;
 goto skip ;
 }
 if ( gst_pad_peer_query_convert ( demux -> sinkpad , GST_FORMAT_TIME , seek_time , GST_FORMAT_BYTES , & offset ) ) {
 packet = ( offset - demux -> data_offset ) / demux -> packet_size ;
 GST_LOG_OBJECT ( demux , "convert %" GST_TIME_FORMAT " to bytes query result: %" G_GINT64_FORMAT ", data_ofset: %" G_GINT64_FORMAT ", packet_size: %u," " resulting packet: %u\n" , GST_TIME_ARGS ( seek_time ) , offset , demux -> data_offset , demux -> packet_size , packet ) ;
 }
 else {
 if ( flush && ( demux -> accurate || ( demux -> keyunit_sync && ! next ) ) && demux -> num_video_streams > 0 ) {
 seek_time -= 5 * GST_SECOND ;
 if ( seek_time < 0 ) seek_time = 0 ;
 }
 packet = ( guint ) gst_util_uint64_scale ( demux -> num_packets , seek_time , demux -> play_time ) ;
 if ( packet > demux -> num_packets ) packet = demux -> num_packets ;
 }
 }
 else {
 if ( G_LIKELY ( demux -> keyunit_sync && ! demux -> accurate ) ) {
 GST_DEBUG_OBJECT ( demux , "key unit seek, adjust seek_time = %" GST_TIME_FORMAT " to index_time = %" GST_TIME_FORMAT , GST_TIME_ARGS ( seek_time ) , GST_TIME_ARGS ( idx_time ) ) ;
 segment . start = idx_time ;
 segment . position = idx_time ;
 segment . time = idx_time ;
 }
 }
 GST_DEBUG_OBJECT ( demux , "seeking to packet %u (%d)" , packet , speed_count ) ;
 GST_OBJECT_LOCK ( demux ) ;
 demux -> segment = segment ;
 if ( GST_ASF_DEMUX_IS_REVERSE_PLAYBACK ( demux -> segment ) ) {
 demux -> packet = ( gint64 ) gst_util_uint64_scale ( demux -> num_packets , stop , demux -> play_time ) ;
 }
 else {
 demux -> packet = packet ;
 }
 demux -> need_newsegment = TRUE ;
 demux -> segment_seqnum = seqnum ;
 demux -> speed_packets = GST_ASF_DEMUX_IS_REVERSE_PLAYBACK ( demux -> segment ) ? 1 : speed_count ;
 gst_asf_demux_reset_stream_state_after_discont ( demux ) ;
 GST_OBJECT_UNLOCK ( demux ) ;
 skip : gst_pad_start_task ( demux -> sinkpad , ( GstTaskFunction ) gst_asf_demux_loop , demux , NULL ) ;
 GST_PAD_STREAM_UNLOCK ( demux -> sinkpad ) ;
 return TRUE ;
 }