| 2827 | } |
| 2828 | |
| 2829 | static int build_program_map(const uint8_t *buf, hb_stream_t *stream) |
| 2830 | { |
| 2831 | // Get adaption header info |
| 2832 | int adapt_len = 0; |
| 2833 | int adaption = (buf[3] & 0x30) >> 4; |
| 2834 | if (adaption == 0) |
| 2835 | return 0; |
| 2836 | else if (adaption == 0x2) |
| 2837 | adapt_len = 184; |
| 2838 | else if (adaption == 0x3) |
| 2839 | adapt_len = buf[4] + 1; |
| 2840 | if (adapt_len > 184) |
| 2841 | return 0; |
| 2842 | |
| 2843 | // Get payload start indicator |
| 2844 | int start; |
| 2845 | start = (buf[1] & 0x40) != 0; |
| 2846 | |
| 2847 | // Get pointer length - only valid in packets with a start flag |
| 2848 | int pointer_len = 0; |
| 2849 | |
| 2850 | if (start) |
| 2851 | { |
| 2852 | pointer_len = buf[4 + adapt_len] + 1; |
| 2853 | stream->pmt_info.tablepos = 0; |
| 2854 | } |
| 2855 | // Get Continuity Counter |
| 2856 | int continuity_counter = buf[3] & 0x0f; |
| 2857 | if (!start && (stream->pmt_info.current_continuity_counter + 1 != continuity_counter)) |
| 2858 | { |
| 2859 | hb_log("build_program_map - Continuity Counter %d out of sequence - expected %d", continuity_counter, stream->pmt_info.current_continuity_counter+1); |
| 2860 | return 0; |
| 2861 | } |
| 2862 | stream->pmt_info.current_continuity_counter = continuity_counter; |
| 2863 | stream->pmt_info.reading |= start; |
| 2864 | |
| 2865 | // Add the payload for this packet to the current buffer |
| 2866 | int amount_to_copy = 184 - adapt_len - pointer_len; |
| 2867 | if (stream->pmt_info.reading && (amount_to_copy > 0)) |
| 2868 | { |
| 2869 | stream->pmt_info.tablebuf = realloc(stream->pmt_info.tablebuf, stream->pmt_info.tablepos + amount_to_copy); |
| 2870 | |
| 2871 | memcpy(stream->pmt_info.tablebuf + stream->pmt_info.tablepos, buf + 4 + adapt_len + pointer_len, amount_to_copy); |
| 2872 | stream->pmt_info.tablepos += amount_to_copy; |
| 2873 | } |
| 2874 | if (stream->pmt_info.tablepos > 3) |
| 2875 | { |
| 2876 | // We have enough to check the section length |
| 2877 | int length; |
| 2878 | length = ((stream->pmt_info.tablebuf[1] << 8) + |
| 2879 | stream->pmt_info.tablebuf[2]) & 0xFFF; |
| 2880 | if (stream->pmt_info.tablepos > length + 1) |
| 2881 | { |
| 2882 | // We just finished a bunch of packets - parse the program map details |
| 2883 | int decode_ok = 0; |
| 2884 | if (stream->pmt_info.tablebuf[0] == 0x02) |
| 2885 | decode_ok = decode_program_map(stream); |
| 2886 | free(stream->pmt_info.tablebuf); |
no test coverage detected