Skip to content

Commit

Permalink
null alloc check
Browse files Browse the repository at this point in the history
Signed-off-by: junka <[email protected]>
  • Loading branch information
junka committed Jul 28, 2024
1 parent 0ee6cb8 commit a5c8a01
Show file tree
Hide file tree
Showing 29 changed files with 316 additions and 73 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,7 @@ jobs:

- name: Install Pybind11 windows
run: >
git clone https://github.com/Microsoft/vcpkg.git &&
cd vcpkg &&
.\bootstrap-vcpkg.bat &&
.\vcpkg integrate install &&
curl -LO https://aka.ms/vcpkg-init.cmd && .\vcpkg-init.cmd &&
vcpkg install pybind11
- name: cmake
Expand All @@ -99,6 +96,7 @@ jobs:
-DCMAKE_BUILD_TYPE=Release
-DCMAKE_C_COMPILER=cl
-DCMAKE_CXX_COMPILER=cl
-DCMAKE_TOOLCHAIN_FILE="C:/vcpkg/scripts/buildsystems/vcpkg.cmake"
-S ${{github.workspace}}
- name: Build
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ Makefile.in
Makefile
.vscode/
.DS_Store
.vs/
11 changes: 4 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/src SRC_LIST)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 11)

# SET(CMAKE_C_COMPILER "/usr/local/opt/llvm/bin/clang")
# SET(CMAKE_CXX_COMPILER "/usr/local/opt/llvm/bin/clang++")

IF (WIN32)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
ELSE()
Expand All @@ -31,8 +28,8 @@ ENDIF()

ADD_EXECUTABLE(tsanalyze ${SRC_LIST})

#find_package(Python 3.8 COMPONENTS Interpreter Development REQUIRED)
#find_package(pybind11 REQUIRED)
#python3 -m pybind11 --includes
find_package(Python 3.8 COMPONENTS Interpreter Development REQUIRED)
# on windows, use vcpkg install pybind11 and configure PYBIND11_DIR manually
find_package(pybind11 CONFIG REQUIRED)

#pybind11_add_module(tsana ./pybind/binding.cpp ${SRC_LIST})
pybind11_add_module(tsana ./pybind/binding.cpp ${SRC_LIST})
41 changes: 29 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,48 @@
# TS Analyze

A TS packet start with 4 bytes header, total length could be 188, 192 or 204 bytes.
Most system will use 188 bytes.
A TS packet start with 4 bytes header, total length could be 188, 192 or 204 bytes which are specified in standard DVB/ATSC/ISDB.

Construct TS packets we will get PES/PSI packets. PSI is structured for search and
other auxiliary functions. PES contains ES packets which could be audio or vedio
streams.

```
Construct TS packets we will get PES/PSI packets.
PSI is structured for search and other auxiliary functions.
PES contains ES packets which could be audio or vedio streams.
```

With this understanding, you can start decoding a TS stream or file now.


Use basic rules from ISO 13818 and ETSI EN 300 468 to decode MPEG2 TS
Use EN 300 743 to decode subtitle
Use ETS 300 706 to decode teletext
- Use basic rules from ISO 13818 and ETSI EN 300 468 to decode MPEG2 TS
- Use EN 300 743 to decode subtitle
- Use ETS 300 706 to decode teletext

Print descriptors for DVB/ATSC/ISDB standard.
Print PSI tables infomation and some other subsystem infomation


# Compile

Two methods: start with
We could install pybind11 to build package for python.

for Linux & Macos
```
git clone https://github.com/junka/tsanalyze.git
cd tsanalyze
mkdir build
cd build
cmake ..
make
```

for Windows with MSVC
```
- autogen.sh
- cmake
git clone https://github.com/junka/tsanalyze.git
cd tsanalyze
mkdir build
cd build
cmake .. -G Ninja -DCMAKE_TOOLCHAIN_FILE="C:/vcpkg/scripts/buildsystems/vcpkg.cmake"
cmake --build .
```
and then do ```make``` for both ways

# Usage
- Most simple way
Expand Down
6 changes: 3 additions & 3 deletions include/comm.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extern "C" {
#define container_of(item, type, member) ((type *)((char *)item - (char *)(&((type *)0)->member)))
#endif

//For msvc cl.exe see https://learn.microsoft.com/zh-cn/cpp/c-language/typeof-c?view=msvc-170
//For MSVC cl.exe see https://learn.microsoft.com/zh-cn/cpp/c-language/typeof-c?view=msvc-170
// see https://learn.microsoft.com/zh-cn/cpp/overview/compiler-versions?view=msvc-170

#if _MSC_VER >= 1939
Expand All @@ -48,12 +48,12 @@ extern "C" {

#define BUILD_ASSERT_OR_ZERO(cond) (sizeof(char[1 - 2 * !(cond)]) - 1)

#if HAVE_TYPEOF
#ifdef HAVE_TYPEOF
#define check_type(expr, type) ((typeof(expr) *)0 != (type *)0)

#define check_types_match(expr1, expr2) ((typeof(expr1) *)0 != (typeof(expr2) *)0)
#else
/* Without typeof, we can only test the sizes. */
/* Without 'typeof', we can only test the sizes. */
#define check_type(expr, type) BUILD_ASSERT_OR_ZERO(sizeof(expr) == sizeof(type))

#define check_types_match(expr1, expr2) BUILD_ASSERT_OR_ZERO(sizeof(expr1) == sizeof(expr2))
Expand Down
24 changes: 16 additions & 8 deletions include/descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ struct MuxCodeSlot
uint8_t numberOfBytes;
} PACK;

struct MuxCodeSubstrue
struct MuxCodeSubstruct
{
uint8_t slotCount : 5;
uint8_t repetitionCount : 3;
Expand All @@ -312,7 +312,7 @@ struct MuxCodeTableEntry
uint8_t MuxCode : 4;
uint8_t version : 4;
uint8_t substructureCount;
struct MuxCodeSubstrue substructure;
struct MuxCodeSubstruct substructure;
} PACK;

#define foreach_muxcode_member \
Expand Down Expand Up @@ -477,7 +477,9 @@ foreach_enum_descriptor
#define __mplast(type, name) \
dr->name = NULL; \
while(dr->descriptor.length + 2 - bytes_off > 0) { \
dr->name = (type *)realloc(dr->name, (dr->name##_cnt + 1) * sizeof(type)); \
type *name = (type *)realloc(dr->name, (dr->name##_cnt + 1) * sizeof(type)); \
if (!name) { return ENOMEM; } \
dr->name = name; \
memcpy(dr->name + dr->name##_cnt, buf + bytes_off, sizeof(type)); \
dr->name##_cnt ++; \
bytes_off += sizeof(type) ; \
Expand All @@ -489,7 +491,9 @@ foreach_enum_descriptor
#define __mplast_custom_sub(type, name, parse_cb, dump_cb, free_cb) \
dr->name = NULL; \
while (dr->descriptor.length + 2 - bytes_off > 0) { \
dr->name = (type *) realloc(dr->name, ((dr->name##_cnt + 1) * sizeof(type))); \
type * name = (type *) realloc(dr->name, ((dr->name##_cnt + 1) * sizeof(type))); \
if (!name) { return ENOMEM; } \
dr->name = name; \
bytes_off += parse_cb(buf + bytes_off, dr->descriptor.length- bytes_off, &dr->name[dr->name##_cnt]); \
dr->name##_cnt ++; \
}
Expand Down Expand Up @@ -517,6 +521,7 @@ foreach_enum_descriptor
} else { \
dr->name = (type *)calloc(dr->length, sizeof(type)); \
} \
if (!dr->name) { return ENOMEM; } \
for (uint32_t i_ = 0; i_ < dr->length; i_++) { \
*(dr->name + i_) = *(type *)(buf + bytes_off); \
bytes_off += sizeof(type); \
Expand All @@ -530,7 +535,9 @@ foreach_enum_descriptor
dr->name = NULL; \
while(len - bytes_off) { \
dr->name##_num ++; \
dr->name = (type *)realloc(dr->name, sizeof(type) * dr->name##_num); \
type * name = (type *)realloc(dr->name, sizeof(type) * dr->name##_num); \
if (!name) { return ENOMEM; } \
dr->name = name; \
memcpy(dr->name + dr->name##_num - 1, buf + bytes_off, offsetof(type, length)); \
bytes_off += offsetof(type, length); \
dr->name[dr->name##_num - 1].length = TS_READ8(buf + bytes_off); \
Expand All @@ -539,6 +546,7 @@ foreach_enum_descriptor
v += offsetof(type, length) + sizeof(dr->name[dr->name##_num - 1].length); \
uintptr_t **vv = (uintptr_t **)v; \
*vv = (uintptr_t *) calloc(1, dr->name[dr->name##_num - 1].length); \
if (!*vv) { return ENOMEM; } \
memcpy(*vv, buf + bytes_off, dr->name[dr->name##_num - 1].length); \
bytes_off += dr->name[dr->name##_num - 1].length; \
}
Expand All @@ -548,7 +556,9 @@ foreach_enum_descriptor
dr->name = NULL; \
while(len > bytes_off) { \
dr->name##_num ++; \
dr->name = (type *)realloc(dr->name, sizeof(type) * dr->name##_num); \
type * name = (type *)realloc(dr->name, sizeof(type) * dr->name##_num); \
if (!name) { return ENOMEM; } \
dr->name = name; \
bytes_off += parse_cb(buf + bytes_off, len - bytes_off, dr->name + dr->name##_num - 1); \
}

Expand Down Expand Up @@ -698,8 +708,6 @@ foreach_enum_descriptor
#undef __m1
#undef __m

int parse_tlv(uint8_t *buf);

void init_descriptor_parsers(void);

void free_descriptors(struct list_head *list);
Expand Down
19 changes: 18 additions & 1 deletion include/dvb/descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,9 @@ __parse_extend_event_linkage_info(uint8_t *buf, int len, struct extend_event_lin
ptr += 1;
int l = 0, i = 0;
while (l < e->loop_length) {
e->subinfo = (struct extend_event_linkage_subinfo *)realloc(e->subinfo , (i+1) * sizeof(struct extend_event_linkage_subinfo));
struct extend_event_linkage_subinfo *einfo = (struct extend_event_linkage_subinfo *)realloc(e->subinfo, (i + 1) * sizeof(struct extend_event_linkage_subinfo));
if (!einfo) {return ENOMEM; }
e->subinfo = einfo;
e->subinfo[i].target_event_id = TS_READ16(ptr);
ptr += 2;
l += 2;
Expand Down Expand Up @@ -517,6 +519,9 @@ __parse_multilingual_node(uint8_t *buf, int len, struct multilingual_node *node)
node->ISO_639_language_code = TS_READ32_BITS(buf, 24, 0);
node->name_length = TS_READ32_BITS(buf, 8, 24);
node->text_char = (uint8_t *)calloc(1, node->name_length + 1);
if (!node->text_char) {
return ENOMEM;
}
memcpy(node->text_char, buf + 4, node->name_length);
return node->name_length + 4;
}
Expand Down Expand Up @@ -556,9 +561,15 @@ __parse_multilingual_service_node(uint8_t *buf, int len, struct multilingual_ser
node->ISO_639_language_code = TS_READ32_BITS(buf, 24, 0);
node->name_length = TS_READ32_BITS(buf, 8, 24);
node->text_char = (uint8_t *)calloc(1, node->name_length + 1);
if (!node->text_char) {
return ENOMEM;
}
memcpy(node->text_char, buf + 4, node->name_length);
node->service_name_length = TS_READ8(buf + 4 + node->name_length);
node->service_char = (uint8_t *)calloc(1, node->service_name_length + 1);
if (!node->service_char) {
return ENOMEM;
}
memcpy(node->service_char, buf + 5 + node->name_length, node->service_name_length);
return node->name_length + 5 + node->service_name_length;
}
Expand Down Expand Up @@ -729,6 +740,9 @@ parse_cell_list_descriptor__(uint8_t *buf, int len, struct cell_list_node *n)
ptr += 4;
int num = n->subcell_info_loop_length / sizeof(struct subcell_list_info);
n->subcell_list = (struct subcell_list_info *)calloc(num, sizeof(struct subcell_list_info));
if (!n->subcell_list) {
return ENOMEM;
}
for (int i = 0; i < num; i ++) {
n->subcell_list[i].cell_id_extension = TS_READ64_BITS(ptr, 8, 0);
n->subcell_list[i].subcell_latitude = TS_READ64_BITS(ptr, 16, 8);
Expand Down Expand Up @@ -790,6 +804,9 @@ parse_cell_frequency_link_descriptor__(uint8_t *buf, int len, struct cell_freque
ptr += 1;
int num = n->subcell_info_loop_length / sizeof(struct subcell_info);
n->subcell_info_list = (struct subcell_info *)calloc(num, sizeof(struct subcell_info));
if (!n->subcell_info_list) {
return ENOMEM;
}
for (int i = 0; i < num; i ++) {
n->subcell_info_list[i].cell_id_extension = TS_READ8(ptr);
ptr += 1;
Expand Down
2 changes: 1 addition & 1 deletion include/filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ typedef struct filter_param
uint8_t depth;
uint8_t coff[MAX_FILTER_DEPTH];
uint8_t mask[MAX_FILTER_DEPTH];
uint8_t negete[MAX_FILTER_DEPTH];
uint8_t negate[MAX_FILTER_DEPTH];
} filter_param_t;

typedef int (*filter_cb)(uint16_t pid, uint8_t *data, uint16_t len);
Expand Down
30 changes: 27 additions & 3 deletions include/isdb/descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ __parse_compatibility_descriptor(uint8_t *buf, int len, struct compatibility_des
c->count = TS_READ16(ptr);
ptr += 2;
c->sub = (struct compatobilitylist_descriptor *)calloc(c->count, sizeof(struct compatobilitylist_descriptor));
if (!c->sub) {
return -ENOMEM;
}
for (int i = 0; i < c->count; i++) {
c->sub[i].type = TS_READ8(ptr);
ptr += 1;
Expand All @@ -215,12 +218,18 @@ __parse_compatibility_descriptor(uint8_t *buf, int len, struct compatibility_des
c->sub[i].count = TS_READ8(ptr);
ptr += 1;
c->sub[i].sub = (struct sub_descriptor *)calloc(c->sub[i].count, sizeof(struct sub_descriptor));
if (!c->sub[i].sub) {
return -ENOMEM;
}
for (int j = 0; j < c->sub[i].count; j ++) {
c->sub[i].sub[j].sub_descriptor_tag = TS_READ8(ptr);
ptr += 1;
c->sub[i].sub[j].sub_descriptor_length = TS_READ8(ptr);
ptr += 1;
c->sub[i].sub[j].sub_descriptor_byte = (uint8_t *)calloc(1, c->sub[i].sub[j].sub_descriptor_length);
if (!c->sub[i].sub[j].sub_descriptor_byte) {
return -ENOMEM;
}
for (int m = 0; m < c->sub[i].sub[j].sub_descriptor_length; m ++) {
c->sub[i].sub[j].sub_descriptor_byte[m] = TS_READ8(ptr);
ptr ++;
Expand Down Expand Up @@ -256,11 +265,17 @@ __free_compatibility_descriptor(struct compatibility_descriptor *c)
{
for (int i = 0; i < c->count; i ++) {
for (int j = 0; j < c->sub[i].count; j ++) {
free(c->sub[i].sub[j].sub_descriptor_byte);
if (c->sub[i].sub[j].sub_descriptor_byte) {
free(c->sub[i].sub[j].sub_descriptor_byte);
}
}
free(c->sub[i].sub);
if (c->sub[i].sub) {
free(c->sub[i].sub);
}
}
free(c->sub);
if (c->sub) {
free(c->sub);
}
}

struct module_info{
Expand All @@ -281,6 +296,9 @@ __parse_module_info_list(uint8_t *buf, int len, struct module_info_list *m)
m->num_of_modules = TS_READ16(ptr);
ptr += 2;
m->info = (struct module_info *)calloc(m->num_of_modules, sizeof(struct module_info));
if (!m->info) {
return -ENOMEM;
}
for (int i = 0; i < m->num_of_modules; i ++) {
m->info[i].module_id = TS_READ16(ptr);
ptr += 2;
Expand All @@ -289,6 +307,9 @@ __parse_module_info_list(uint8_t *buf, int len, struct module_info_list *m)
m->info[i].module_info_length = TS_READ8(ptr);
ptr += 1;
m->info[i].module_info_byte = (uint8_t *)calloc(1, m->info[i].module_info_length);
if (!m->info[i].module_info_byte) {
return -ENOMEM;
}
for (int j = 0; j < m->info[i].module_info_length; j ++) {
m->info[i].module_info_byte[j] = TS_READ8(ptr);
ptr += 1;
Expand Down Expand Up @@ -333,6 +354,9 @@ __parse_text_info(uint8_t *buf, int len, struct text_info *t)
ptr += 4;

t->text_char = (uint8_t *)calloc(1, t->text_length);
if (!t->text_char) {
return ENOMEM;
}
for (int i = 0; i < t->text_length; i++) {
t->text_char[i] = TS_READ8(ptr);
ptr += 1;
Expand Down
2 changes: 1 addition & 1 deletion include/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ static inline struct list_node *list_node_from_off_(void *ptr, size_t off)

#define list_off_var_(var, member) (container_off_var(var, member) + check_type(var->member, struct list_node))

#if HAVE_TYPEOF
#ifdef HAVE_TYPEOF
#define list_typeof(var) typeof(var)
#else
#define list_typeof(var) void *
Expand Down
2 changes: 0 additions & 2 deletions include/pes.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ extern "C" {
#include "types.h"
#include "table.h"


#if 0
enum PES_scrambling_control{
PES_non_scrambled = 0x00,
Expand Down Expand Up @@ -59,7 +58,6 @@ typedef struct

typedef struct
{
EXT_STD_C11
union
{
struct
Expand Down
Loading

0 comments on commit a5c8a01

Please sign in to comment.