diff --git a/src/arib_std_b25.c b/src/arib_std_b25.c index 4867fed..d06e898 100644 --- a/src/arib_std_b25.c +++ b/src/arib_std_b25.c @@ -512,7 +512,7 @@ static int reset_arib_std_b25(void *std_b25) static int flush_arib_std_b25(void *std_b25) { - int r; + int r,l; int m,n; int32_t crypt; @@ -539,6 +539,9 @@ static int flush_arib_std_b25(void *std_b25) if(r < 0){ return r; } + if(prv->unit_size < 188){ + return ARIB_STD_B25_ERROR_NON_TS_INPUT_STREAM; + } } r = proc_arib_std_b25(prv); @@ -565,6 +568,12 @@ static int flush_arib_std_b25(void *std_b25) if(p == NULL){ goto LAST; } + if((p-curr) >= (unit-188)){ + if(!append_work_buffer(&(prv->dbuf), p-(unit-188), unit-188)){ + r = ARIB_STD_B25_ERROR_NO_ENOUGH_MEMORY; + goto LAST; + } + } curr = p; } @@ -574,7 +583,12 @@ static int flush_arib_std_b25(void *std_b25) if(hdr.transport_error_indicator != 0){ /* bit error - append output buffer without parsing */ - if(!append_work_buffer(&(prv->dbuf), curr, 188)){ + if((curr+unit) <= tail){ + l = unit; + }else{ + l = 188; + } + if(!append_work_buffer(&(prv->dbuf), curr, l)){ r = ARIB_STD_B25_ERROR_NO_ENOUGH_MEMORY; goto LAST; } @@ -588,12 +602,14 @@ static int flush_arib_std_b25(void *std_b25) p = curr+4; if(hdr.adaptation_field_control & 0x02){ p += (p[0]+1); - } - n = 188 - (p-curr); - if( (n < 1) && ((n < 0) || (hdr.adaptation_field_control & 0x01)) ){ - /* broken packet */ - curr += 1; - continue; + n = 188 - (p-curr); + if( (n < 1) && ((n < 0) || (hdr.adaptation_field_control & 0x01)) ){ + /* broken packet */ + curr += 1; + continue; + } + }else{ + n = 188 - 4; } if( (crypt != 0) && @@ -612,6 +628,7 @@ static int flush_arib_std_b25(void *std_b25) m = dec->m2->decrypt(dec->m2, crypt, p, n); if(m < 0){ r = ARIB_STD_B25_ERROR_DECRYPT_FAILURE; + curr += unit; goto LAST; } curr[3] &= 0x3f; @@ -623,7 +640,12 @@ static int flush_arib_std_b25(void *std_b25) prv->map[pid].normal_packet += 1; } - if(!append_work_buffer(&(prv->dbuf), curr, 188)){ + if((curr+unit) <= tail){ + l = unit; + }else{ + l = 188; + } + if(!append_work_buffer(&(prv->dbuf), curr, l)){ r = ARIB_STD_B25_ERROR_NO_ENOUGH_MEMORY; goto LAST; } @@ -633,16 +655,19 @@ static int flush_arib_std_b25(void *std_b25) if( (dec == NULL) || (dec->ecm == NULL) ){ /* this code will never execute */ r = ARIB_STD_B25_ERROR_ECM_PARSE_FAILURE; + curr += unit; goto LAST; } m = dec->ecm->put(dec->ecm, &hdr, p, n); if(m < 0){ r = ARIB_STD_B25_ERROR_ECM_PARSE_FAILURE; + curr += unit; goto LAST; } m = dec->ecm->get_count(dec->ecm); if(m < 0){ r = ARIB_STD_B25_ERROR_ECM_PARSE_FAILURE; + curr += unit; goto LAST; } if(m == 0){ @@ -650,6 +675,7 @@ static int flush_arib_std_b25(void *std_b25) } r = proc_ecm(dec, prv->bcas, prv->multi2_round); if(r < 0){ + curr += unit; goto LAST; } }else if(prv->map[pid].type == PID_MAP_TYPE_PMT){ @@ -657,16 +683,19 @@ static int flush_arib_std_b25(void *std_b25) if( (pgrm == NULL) || (pgrm->pmt == NULL) ){ /* this code will never execute */ r = ARIB_STD_B25_ERROR_PMT_PARSE_FAILURE; + curr += unit; goto LAST; } m = pgrm->pmt->put(pgrm->pmt, &hdr, p, n); if(m < 0){ r = ARIB_STD_B25_ERROR_PMT_PARSE_FAILURE; + curr += unit; goto LAST; } m = pgrm->pmt->get_count(pgrm->pmt); if(m < 0){ r = ARIB_STD_B25_ERROR_PMT_PARSE_FAILURE; + curr += unit; goto LAST; } if(m == 0){ @@ -674,6 +703,7 @@ static int flush_arib_std_b25(void *std_b25) } r = proc_pmt(prv, pgrm); if(r < 0){ + curr += unit; goto LAST; } }else if(prv->map[pid].type == PID_MAP_TYPE_EMM){ @@ -683,18 +713,20 @@ static int flush_arib_std_b25(void *std_b25) if( prv->emm == NULL ){ prv->emm = create_ts_section_parser(); if(prv->emm == NULL){ - r = ARIB_STD_B25_ERROR_EMM_PARSE_FAILURE; + r = ARIB_STD_B25_ERROR_NO_ENOUGH_MEMORY; goto LAST; } } m = prv->emm->put(prv->emm, &hdr, p, n); if(m < 0){ r = ARIB_STD_B25_ERROR_EMM_PARSE_FAILURE; + curr += unit; goto LAST; } m = prv->emm->get_count(prv->emm); if(m < 0){ r = ARIB_STD_B25_ERROR_EMM_PARSE_FAILURE; + curr += unit; goto LAST; } if(m == 0){ @@ -702,6 +734,7 @@ static int flush_arib_std_b25(void *std_b25) } r = proc_emm(prv); if(r < 0){ + curr += unit; goto LAST; } }else if(pid == 0x0001){ @@ -715,11 +748,13 @@ static int flush_arib_std_b25(void *std_b25) m = prv->cat->put(prv->cat, &hdr, p, n); if(m < 0){ r = ARIB_STD_B25_ERROR_CAT_PARSE_FAILURE; + curr += unit; goto LAST; } m = prv->cat->get_count(prv->cat); if(m < 0){ r = ARIB_STD_B25_ERROR_CAT_PARSE_FAILURE; + curr += unit; goto LAST; } if(m == 0){ @@ -727,6 +762,7 @@ static int flush_arib_std_b25(void *std_b25) } r = proc_cat(prv); if(r < 0){ + curr += unit; goto LAST; } }else if(pid == 0x0000){ @@ -740,11 +776,13 @@ static int flush_arib_std_b25(void *std_b25) m = prv->pat->put(prv->pat, &hdr, p, n); if(m < 0){ r = ARIB_STD_B25_ERROR_PAT_PARSE_FAILURE; + curr += unit; goto LAST; } m = prv->pat->get_count(prv->pat); if(m < 0){ r = ARIB_STD_B25_ERROR_PAT_PARSE_FAILURE; + curr += unit; goto LAST; } if(m == 0){ @@ -752,6 +790,7 @@ static int flush_arib_std_b25(void *std_b25) } r = proc_pat(prv); if(r < 0){ + curr += unit; goto LAST; } } @@ -762,11 +801,13 @@ static int flush_arib_std_b25(void *std_b25) LAST: - m = curr - prv->sbuf.head; + m = curr - prv->sbuf.pool; n = tail - curr; if( (n < 1024) || (m > (prv->sbuf.max/2) ) ){ p = prv->sbuf.pool; - memcpy(p, curr, n); + if(n > 0){ + memcpy(p, curr, n); + } prv->sbuf.head = p; prv->sbuf.tail = p+n; }else{ @@ -1011,7 +1052,15 @@ static int select_unit_size(ARIB_STD_B25_PRIVATE_DATA *prv) head = prv->sbuf.head; tail = prv->sbuf.tail; - + + if((tail-head) < (188*16)){ + // need more data + return 0; + } + if((tail-head) > (188*32)){ + tail = head + (188*32); + } + buf = head; memset(count, 0, sizeof(count)); @@ -1086,10 +1135,12 @@ static int find_pat(ARIB_STD_B25_PRIVATE_DATA *prv) p = curr+4; if(hdr.adaptation_field_control & 0x02){ p += (p[0]+1); - } - size = 188 - (p-curr); - if(size < 1){ - goto NEXT; + size = 188 - (p-curr); + if(size < 1){ + goto NEXT; + } + }else{ + size = 188 - 4; } if(prv->pat == NULL){ @@ -1278,6 +1329,45 @@ static int find_pmt(ARIB_STD_B25_PRIVATE_DATA *prv) extract_ts_header(&hdr, curr); + if(hdr.pid == 0x0000){ + p = curr+4; + if(hdr.adaptation_field_control & 0x02){ + p += (p[0]+1); + size = 188 - (p-curr); + if(size < 1){ + goto NEXT; + } + }else{ + size = 188 - 4; + } + if(prv->pat == NULL){ + prv->pat = create_ts_section_parser(); + if(prv->pat == NULL){ + r = ARIB_STD_B25_ERROR_NO_ENOUGH_MEMORY; + goto LAST; + } + } + n = prv->pat->put(prv->pat, &hdr, p, size); + if(n < 0){ + r = ARIB_STD_B25_ERROR_PAT_PARSE_FAILURE; + curr += unit; + goto LAST; + } + n = prv->pat->get_count(prv->pat); + if(n < 0){ + r = ARIB_STD_B25_ERROR_PAT_PARSE_FAILURE; + curr += unit; + goto LAST; + } + if(n == 0){ + goto NEXT; + } + r = proc_pat(prv); + prv->sbuf.head = curr; + curr += unit; + goto LAST; + } + if(prv->map[hdr.pid].type != PID_MAP_TYPE_PMT){ goto NEXT; } @@ -1291,10 +1381,12 @@ static int find_pmt(ARIB_STD_B25_PRIVATE_DATA *prv) p = curr + 4; if(hdr.adaptation_field_control & 0x02){ p += (p[0]+1); - } - size = 188 - (p-curr); - if(size < 1){ - goto NEXT; + size = 188 - (p-curr); + if(size < 1){ + goto NEXT; + } + }else{ + size = 188 - 4; } if(pgrm->pmt == NULL){ @@ -1610,6 +1702,46 @@ static int find_ecm(ARIB_STD_B25_PRIVATE_DATA *prv) curr = p; } extract_ts_header(&hdr, curr); + + if(hdr.pid == 0x0000){ + p = curr+4; + if(hdr.adaptation_field_control & 0x02){ + p += (p[0]+1); + size = 188 - (p-curr); + if(size < 1){ + goto NEXT; + } + }else{ + size = 188 - 4; + } + if(prv->pat == NULL){ + prv->pat = create_ts_section_parser(); + if(prv->pat == NULL){ + r = ARIB_STD_B25_ERROR_NO_ENOUGH_MEMORY; + goto LAST; + } + } + n = prv->pat->put(prv->pat, &hdr, p, size); + if(n < 0){ + r = ARIB_STD_B25_ERROR_PAT_PARSE_FAILURE; + curr += unit; + goto LAST; + } + n = prv->pat->get_count(prv->pat); + if(n < 0){ + r = ARIB_STD_B25_ERROR_PAT_PARSE_FAILURE; + curr += unit; + goto LAST; + } + if(n == 0){ + goto NEXT; + } + r = proc_pat(prv); + prv->sbuf.head = curr; + curr += unit; + goto LAST; + } + if(prv->map[hdr.pid].type != PID_MAP_TYPE_ECM){ goto NEXT; } @@ -1623,10 +1755,12 @@ static int find_ecm(ARIB_STD_B25_PRIVATE_DATA *prv) p = curr + 4; if(hdr.adaptation_field_control & 0x02){ p += (p[0]+1); - } - size = 188 - (p-curr); - if(size < 1){ - goto NEXT; + size = 188 - (p-curr); + if(size < 1){ + goto NEXT; + } + }else{ + size = 188 - 4; } if(dec->ecm == NULL){ @@ -1866,6 +2000,12 @@ static int proc_arib_std_b25(ARIB_STD_B25_PRIVATE_DATA *prv) if(p == NULL){ goto LAST; } + if((p-curr) >= (unit-188)){ + if(!append_work_buffer(&(prv->dbuf), p-(unit-188), unit-188)){ + r = ARIB_STD_B25_ERROR_NO_ENOUGH_MEMORY; + goto LAST; + } + } curr = p; } @@ -1875,7 +2015,7 @@ static int proc_arib_std_b25(ARIB_STD_B25_PRIVATE_DATA *prv) if(hdr.transport_error_indicator != 0){ /* bit error - append output buffer without parsing */ - if(!append_work_buffer(&(prv->dbuf), curr, 188)){ + if(!append_work_buffer(&(prv->dbuf), curr, unit)){ r = ARIB_STD_B25_ERROR_NO_ENOUGH_MEMORY; goto LAST; } @@ -1890,12 +2030,14 @@ static int proc_arib_std_b25(ARIB_STD_B25_PRIVATE_DATA *prv) p = curr+4; if(hdr.adaptation_field_control & 0x02){ p += (p[0]+1); - } - n = 188 - (p-curr); - if( (n < 1) && ((n < 0) || (hdr.adaptation_field_control & 0x01)) ){ - /* broken packet */ - curr += 1; - continue; + n = 188 - (p-curr); + if( (n < 1) && ((n < 0) || (hdr.adaptation_field_control & 0x01)) ){ + /* broken packet */ + curr += 1; + continue; + } + }else{ + n = 188 - 4; } if( (crypt != 0) && @@ -1914,6 +2056,7 @@ static int proc_arib_std_b25(ARIB_STD_B25_PRIVATE_DATA *prv) m = dec->m2->decrypt(dec->m2, crypt, p, n); if(m < 0){ r = ARIB_STD_B25_ERROR_DECRYPT_FAILURE; + curr += unit; goto LAST; } curr[3] &= 0x3f; @@ -1929,7 +2072,7 @@ static int proc_arib_std_b25(ARIB_STD_B25_PRIVATE_DATA *prv) dump_pts(curr, crypt); } #endif - if(!append_work_buffer(&(prv->dbuf), curr, 188)){ + if(!append_work_buffer(&(prv->dbuf), curr, unit)){ r = ARIB_STD_B25_ERROR_NO_ENOUGH_MEMORY; goto LAST; } @@ -1939,16 +2082,19 @@ static int proc_arib_std_b25(ARIB_STD_B25_PRIVATE_DATA *prv) if( (dec == NULL) || (dec->ecm == NULL) ){ /* this code will never execute */ r = ARIB_STD_B25_ERROR_ECM_PARSE_FAILURE; + curr += unit; goto LAST; } m = dec->ecm->put(dec->ecm, &hdr, p, n); if(m < 0){ r = ARIB_STD_B25_ERROR_ECM_PARSE_FAILURE; + curr += unit; goto LAST; } m = dec->ecm->get_count(dec->ecm); if(m < 0){ r = ARIB_STD_B25_ERROR_ECM_PARSE_FAILURE; + curr += unit; goto LAST; } if(m == 0){ @@ -1956,6 +2102,7 @@ static int proc_arib_std_b25(ARIB_STD_B25_PRIVATE_DATA *prv) } r = proc_ecm(dec, prv->bcas, prv->multi2_round); if(r < 0){ + curr += unit; goto LAST; } }else if(prv->map[pid].type == PID_MAP_TYPE_PMT){ @@ -1963,16 +2110,19 @@ static int proc_arib_std_b25(ARIB_STD_B25_PRIVATE_DATA *prv) if( (pgrm == NULL) || (pgrm->pmt == NULL) ){ /* this code will never execute */ r = ARIB_STD_B25_ERROR_PMT_PARSE_FAILURE; + curr += unit; goto LAST; } m = pgrm->pmt->put(pgrm->pmt, &hdr, p, n); if(m < 0){ r = ARIB_STD_B25_ERROR_PMT_PARSE_FAILURE; + curr += unit; goto LAST; } m = pgrm->pmt->get_count(pgrm->pmt); if(m < 0){ r = ARIB_STD_B25_ERROR_PMT_PARSE_FAILURE; + curr += unit; goto LAST; } if(m == 0){ @@ -1980,6 +2130,7 @@ static int proc_arib_std_b25(ARIB_STD_B25_PRIVATE_DATA *prv) } r = proc_pmt(prv, pgrm); if(r < 0){ + curr += unit; goto LAST; } }else if(prv->map[pid].type == PID_MAP_TYPE_EMM){ @@ -1989,18 +2140,20 @@ static int proc_arib_std_b25(ARIB_STD_B25_PRIVATE_DATA *prv) if( prv->emm == NULL ){ prv->emm = create_ts_section_parser(); if(prv->emm == NULL){ - r = ARIB_STD_B25_ERROR_EMM_PARSE_FAILURE; + r = ARIB_STD_B25_ERROR_NO_ENOUGH_MEMORY; goto LAST; } } m = prv->emm->put(prv->emm, &hdr, p, n); if(m < 0){ r = ARIB_STD_B25_ERROR_EMM_PARSE_FAILURE; + curr += unit; goto LAST; } m = prv->emm->get_count(prv->emm); if(m < 0){ r = ARIB_STD_B25_ERROR_EMM_PARSE_FAILURE; + curr += unit; goto LAST; } if(m == 0){ @@ -2008,6 +2161,7 @@ static int proc_arib_std_b25(ARIB_STD_B25_PRIVATE_DATA *prv) } r = proc_emm(prv); if(r < 0){ + curr += unit; goto LAST; } }else if(pid == 0x0001){ @@ -2021,11 +2175,13 @@ static int proc_arib_std_b25(ARIB_STD_B25_PRIVATE_DATA *prv) m = prv->cat->put(prv->cat, &hdr, p, n); if(m < 0){ r = ARIB_STD_B25_ERROR_CAT_PARSE_FAILURE; + curr += unit; goto LAST; } m = prv->cat->get_count(prv->cat); if(m < 0){ r = ARIB_STD_B25_ERROR_CAT_PARSE_FAILURE; + curr += unit; goto LAST; } if(m == 0){ @@ -2033,6 +2189,7 @@ static int proc_arib_std_b25(ARIB_STD_B25_PRIVATE_DATA *prv) } r = proc_cat(prv); if(r < 0){ + curr += unit; goto LAST; } }else if(pid == 0x0000){ @@ -2046,17 +2203,20 @@ static int proc_arib_std_b25(ARIB_STD_B25_PRIVATE_DATA *prv) m = prv->pat->put(prv->pat, &hdr, p, n); if(m < 0){ r = ARIB_STD_B25_ERROR_PAT_PARSE_FAILURE; + curr += unit; goto LAST; } m = prv->pat->get_count(prv->pat); if(m < 0){ r = ARIB_STD_B25_ERROR_PAT_PARSE_FAILURE; + curr += unit; goto LAST; } if(m == 0){ goto NEXT; } r = proc_pat(prv); + curr += unit; goto LAST; } @@ -2065,11 +2225,13 @@ static int proc_arib_std_b25(ARIB_STD_B25_PRIVATE_DATA *prv) } LAST: - m = curr - prv->sbuf.head; + m = curr - prv->sbuf.pool; n = tail - curr; if( (n < 1024) || (m > (prv->sbuf.max/2) ) ){ p = prv->sbuf.pool; - memcpy(p, curr, n); + if(n > 0){ + memcpy(p, curr, n); + } prv->sbuf.head = p; prv->sbuf.tail = p+n; }else{ @@ -2159,7 +2321,7 @@ static int proc_emm(ARIB_STD_B25_PRIVATE_DATA *prv) n = prv->emm->get(prv->emm, §); if(n < 0){ - r = ARIB_STD_B25_ERROR_CAT_PARSE_FAILURE; + r = ARIB_STD_B25_ERROR_EMM_PARSE_FAILURE; goto LAST; }