diff options
| -rw-r--r-- | src/vdefine.c | 309 | ||||
| -rw-r--r-- | src/vdefine.h | 35 | ||||
| -rw-r--r-- | test/test.c | 124 | ||||
| -rw-r--r-- | test/test_dir.c | 144 |
4 files changed, 612 insertions, 0 deletions
diff --git a/src/vdefine.c b/src/vdefine.c new file mode 100644 index 0000000..b673de6 --- /dev/null +++ b/src/vdefine.c @@ -0,0 +1,309 @@ +#include "vdefine.h" + +#define AV_CONTAINER_MPEGTS 1 +#define AV_CONTAINER_FLV 2 +#define AV_CONTAINER_MP4 3 +#define UNKNOWN 4 +#define FIT_TIMES 2 +#define MAX_FLV_FRAME max_flv_frame;//? +int xtoint(const char* buff, size_t size) +{ + int size_h = 0; + int i; + unsigned char *add=(unsigned char *)malloc(sizeof(unsigned char *)); + for (i = 0; i < size; i++) + { + memcpy(add,(buff+i),1); + size_h +=((*add) << (8 * (size - i - 1))); + } + free(add); + add = NULL; + return size_h; + +} +int is_mpeg_ts_each(const char *buff, size_t size) +{ + const char *p = buff; + int fit_times = 0; + p += 188; + + if ((buff+3)>=(buff+size)) + return 0; + int c_count_ori = xtoint(buff + 3, 1); + c_count_ori = c_count_ori % 16; + int c_count_aft; + while (p < (buff + size))//=? + { + if (*p == 0x47) + { + if ((p+3) < (buff+size)) + { + c_count_aft = xtoint(p + 3, 1); + c_count_aft = c_count_aft % 16; + c_count_aft = (c_count_aft == 0 ? 16 : c_count_aft); + if (c_count_aft == c_count_ori + 1) + fit_times++; + else + return 0; + + } + if (fit_times > FIT_TIMES) + return 1; + } + else + return 0; + + c_count_ori = c_count_aft; + p += 188; + } + return 0; +} + +int is_mpeg_ts(const char* buff, size_t size) +{ + const char*p = buff; + const char *p_each = buff; + size_t size_move = 0; + int ret; + while (size_move < size) + { + p_each = memchr(p_each, 0x47, size - size_move); + if (p_each == NULL) + return 0; + else + { + ret = is_mpeg_ts_each(p_each,size-(p_each-buff)); + if (ret == 1) + { + return 1; + } + p_each++; + size_move = p_each - buff; + } + } + return 0; +} +void * my_memchr_flv(const char *buff, size_t size, int count_type)//void * my_memchr_flv(const void * buff, size_t size, int count_type) +{ + int sig; + int i; + while (size) + { + sig = 0; + i = 0; + for (i = 0; i < count_type; i++) + { + if (*buff != (const char)flv_tag_type_all[i]) + sig++; + } + if (sig == count_type) + { + buff ++; + size --; + } + else + return (void *)buff; + } + return NULL; +} + +int is_flv_each(const char* buff, size_t size) +{ + const char *p = buff; + int fit_times = 1; + unsigned int left; + if ((p+3)>=(buff+size)) + return 0; + int right = 11 + xtoint(p + 1, 3); + p += (right + 4); + + while (p < (buff + size)) + { + + left = xtoint(p - 4, 4); + if (right==left&&(*(p) == 0x08 || *(p) == 0x09 || *(p) == 0x12)) + { + fit_times++; + if (fit_times > FIT_TIMES) + { + return 1; + } + } + else + return 0; + if ((p+3)>=(buff+size)) + return 0; + right = 11 + xtoint(p + 1, 3); + p += (right + 4); + } + return 0; +} + +int is_flv(const char* buff, size_t size) +{ + const char*p = buff; + const char *p_each = buff; + size_t size_move = 0; + + while (size_move < size) + { + p_each = my_memchr_flv(p_each, size - size_move, 3); + if (p_each==NULL) + return 0; + + else + { + int ret = is_flv_each(p_each, size - (p_each - buff)); + if (ret == 1) + { + return 1; + } + p_each++;/// + size_move = p_each - buff; + } + } + return 0; +} + + +void * my_memmem_mp4(const char * buf, size_t size, int count_type) +{ + int i; + const char *ret; + const char *ret_rec;//?? + for (i = 0; i < count_type; i++) + { + ret = (const char *)memmem(buf,size, mp4_box_type_all[i], strlen(mp4_box_type_all[i])); + if (ret != NULL) + break; + } + if (ret == NULL) + return NULL; + for (i = 0; i < count_type; i++) + { + ret_rec = ret; + ret = (const char *)memmem(buf,size, mp4_box_type_all[i], strlen(mp4_box_type_all[i])); + if (ret != NULL) + { + if (ret_rec > ret) + return (void *)ret; + else if (ret_rec < ret) + return (void *)ret_rec; + else + continue; + } + else + ret = ret_rec; + } + return (void *)ret;//return NULL; +} + +int is_mp4_each_part(const char *buff, int count_type) +{ + int i; + size_t size_type_h; + for (i = 0; i < count_type; i++) + { + size_type_h = strlen(mp4_box_type_all[i]); + if (memcmp(buff, mp4_box_type_all[i], size_type_h) == 0) + return 1; + } + return 0; +} + +int is_mp4_each(const char* buff, size_t size, const char *buff_s, int count_type) +{ + const char *p = buff; + int fit_times = 1; + + unsigned int size_h; + int ret; + while (p < (buff + size)) + { + p += 8; + if (p < (buff + size)) + { + if ((p+4)>=(buff+size)) + return 0; + ret = is_mp4_each_part(p, count_type); + if (ret == 1) + { + fit_times++; + if (fit_times > FIT_TIMES) + { + return 1; + } + } + else + { + if ((p-8-4)<buff_s) + return 0; + else + { + size_h = xtoint(p -8 - 4, 4); + p += (size_h-8); + if ((p+4)>=(buff+size)) + return 0; + ret = is_mp4_each_part(p, count_type); + if (ret == 1) + { + fit_times++; + if (fit_times > FIT_TIMES) + return 1; + } + else + return 0;///adding + } + } + } + else + return 0; + } + return 0; +} + +int is_mp4(const char* buff, size_t size) +{ + const char *p = buff; + const char *p_each = buff; + size_t size_move = 0; + + int count_type = sizeof(mp4_box_type_all) / sizeof(char *); + int ret; + while (size_move < size) + { + p_each = (const char *)my_memmem_mp4(p_each, size - size_move, count_type); + if (p_each==NULL) + return 0; + else + { + ret = is_mp4_each(p_each, size - (p_each - buff), buff,count_type); + if (ret == 1) + { + return 1; + } + p_each++; + size_move = p_each - buff; + } + } + return 0; +} + +int AV_container_identify(const char* buff, size_t size) +{ + if (1 == is_mpeg_ts(buff, size)) + { + return AV_CONTAINER_MPEGTS; + } + if (1 == is_flv(buff, size)) + { + return AV_CONTAINER_FLV; + } + if (1 == is_mp4(buff, size)) + { + return AV_CONTAINER_MP4; + } + return UNKNOWN; +} + + diff --git a/src/vdefine.h b/src/vdefine.h new file mode 100644 index 0000000..9783b7a --- /dev/null +++ b/src/vdefine.h @@ -0,0 +1,35 @@ +#ifndef _VDEFINE_H +#define _VDEFINE_H + +#include <arpa/inet.h> +#include <stdlib.h> +#define _GNU_SOURCE +#define __USE_GNU +#include <string.h> +#include <stdio.h> +#include <math.h> +#include <sys/types.h> + +#include <sys/io.h> + +const char *mp4_box_type_all[] = { "ftyp", "pdin", "moov", "mvhd", "trak", "tkhd", "tref", "edts", "elst", "mdia", "mdhd", +"hdlr", "minf", "vmhd", "smhd", "hmhd", "nmhd", "dinf", "dref", "stbl", "stsd", "stts", "ctts", "stsc", "stsz", +"stz2", "stco", "co64", "stss", "stsh", "padb", "stdp", "sdtp", "sbgp", "sgpd", "subs", "mvex", "mehd", "trex", +"ipmc", "moof", "mfhd", "traf", "tfhd", "trun", "sdtp", "sbgp", "subs", "mfra", "tfra", "mfro", "mdat", "free", +"skip", "udta", "cprt", "meta", "hdlr", "dinf", "dref", "ipmc", "iloc", "ipro", "sinf", "frma", "imif", "schm", +"schi", "iinf", "xml", "bxml", "pitm", "fiin", "paen", "fpar", "fecr", "segr", "gitn", "tsel", "meco", "mere" }; +const int flv_tag_type_all[] = { 0x08,0x09,0x12 }; + +int xtoint(const char* buff, size_t size); +int is_mpeg_ts_each(const char *buff, size_t size); +int is_mpeg_ts(const char* buff, size_t size); +void * my_memchr_flv(const char * buffer, size_t size, int count_set);//void * my_memchr_flv(const void * buffer, size_t size, int count_set); +int is_flv_each(const char* buff, size_t size); +int is_flv(const char* buff, size_t size); +void * my_memmem_mp4(const char * buf, size_t size, int count_type); +int is_mp4_each_part(const char *buff, int count_type); +int is_mp4_each(const char* buff, size_t size, const char *buff_s, int count_type); +int is_mp4(const char* buff, size_t size); +int AV_container_identify(const char* buff, size_t size); + +#endif diff --git a/test/test.c b/test/test.c new file mode 100644 index 0000000..2bf5476 --- /dev/null +++ b/test/test.c @@ -0,0 +1,124 @@ +#include "vdefine.h" +#define SIZE_BUFF 16*16*16*16*16*16*16*16-1//6*16*16*16*16*16 +#include <dirent.h> +#include <sys/stat.h> +#include <unistd.h> +typedef struct buff_inf_h +{ + char *buff; + size_t size; +}buff_inf; + +buff_inf *buff_get(char *vfile) { + buff_inf *buff_inf_h = (buff_inf *)malloc(sizeof(buff_inf)); + + FILE *fr = NULL; + char *buff_ori = (char *)malloc(sizeof(char *)*SIZE_BUFF);//CH + char *buff = buff_ori;//CH + memset(buff, 0, sizeof(buff)); + if ((fr = fopen(vfile, "rb")) == NULL) + { + perror("fopen error:"); + buff_inf_h->buff = NULL; + buff_inf_h->size = 0; + return buff_inf_h;//-1; + } + size_t i = 0;//unsigned int i = 0;//size_t i = 0; + //int j = 0; + while (!feof(fr)) + { + fread(buff, 1, 1, fr); + buff += 1; + i++; + } + buff_inf_h->buff = (char *)malloc(sizeof(char *) * (i-2)); + memcpy(buff_inf_h->buff, buff - i, i-2); + buff_inf_h->size = i - 2; + //buff_inf_h.buff = buff - i; + //buff_inf_h.size = i; + free(buff_ori);//CH--free(buff - i);//? + buff_ori = NULL; + fclose(fr); + return buff_inf_h; + +} +int main(int argc, char *argv[]) +{ + char *vfile = argv[1]; + buff_inf *buff_inf_h; + //CH--char *buff;// = (char *)malloc(sizeof(char *)*size);//; + int ret; + int ret_sig = atoi(argv[2]); + int sig_show_all = atoi(argv[3]); + int sig_choose_all = atoi(argv[4]); + size_t size; + + buff_inf_h = buff_get(vfile); + if (buff_inf_h->size == 0) + { + printf("read file failed.\n"); + return -1; + } + if (sig_choose_all == 0) + { + int divide_h = 10; + int divide_s = 0; + int divide_e = 1; + + size = (buff_inf_h->size / divide_h)*(divide_e - divide_s); + + //CH--buff = (char *)malloc(sizeof(char *)*size); + //CH--memcpy(buff, buff_inf_h->buff + (buff_inf_h->size / divide_h)*divide_s, size); + + //printf("buff_whole is %p.\n", buff_inf_h->buff); + //printf("buff_inf_h.size/divide_h is %x.\n", buff_inf_h->size / divide_h); + //printf("buff starts from(>=) %p.\n", buff); + //printf("buff ends at(<) %p.\n", buff + size - 1);//+size + ret = AV_container_identify(buff_inf_h->buff + (buff_inf_h->size / divide_h)*divide_s, size);;//CCH--ret = AV_container_identify(buff, size);//CH--(const char *)? + //CH--free(buff); + //CH--buff = NULL; + //printf("define : %d.\n", ret); + //printf("size of whole buff is %d.\n", buff_inf_h->size);//size - 1 + //printf("size of chosen buff is %d.\n", size); + } + else + ret = AV_container_identify(buff_inf_h->buff, buff_inf_h->size); + if (sig_show_all == 0) + { + if (ret != ret_sig) + { + printf("-----------------data of [%s] is kind of [%d].\n",vfile,ret);// dp->d_name, ret); + //printf("define : %d.\n", ret); + printf("size of whole buff is %d.\n", buff_inf_h->size); + if (sig_choose_all == 0) + printf("size of chosen buff is %d.\n", size); + + } + } + else + { + printf("-----------------data of [%s] is kind of [%d].\n", vfile, ret);// dp->d_name, ret); + printf("size of whole buff is %d.\n", buff_inf_h->size); + if (sig_choose_all == 0) + printf("size of chosen buff is %d.\n", size); + } + free(buff_inf_h->buff);//CH-- + buff_inf_h->buff = NULL;//CH-- + free(buff_inf_h);//CH-- + buff_inf_h = NULL;//CH-- + /* + free(buff); + + free(buff_inf_h->buff); + buff_inf_h = NULL; + free(buff_inf_h); + return ret; + */ + + //CH--free(buff_inf_h->buff); + //CH--buff_inf_h = NULL; + //CH--free(buff_inf_h); + + + return ret; +} diff --git a/test/test_dir.c b/test/test_dir.c new file mode 100644 index 0000000..af90842 --- /dev/null +++ b/test/test_dir.c @@ -0,0 +1,144 @@ +#include "vdefine.h" +#define SIZE_BUFF 16*16*16*16*16*16*16*16-1//6*16*16*16*16*16 +#include <dirent.h> +#include <sys/stat.h> +#include <unistd.h> +typedef struct buff_inf_h +{ + char *buff; + size_t size; +}buff_inf; + +buff_inf *buff_get(char *vfile) { + buff_inf *buff_inf_h = (buff_inf *)malloc(sizeof(buff_inf)); + + FILE *fr = NULL; + char *buff_ori = (char *)malloc(sizeof(char *)*SIZE_BUFF);//CH + char *buff = buff_ori;//CH + memset(buff, 0, sizeof(buff)); + if ((fr = fopen(vfile, "rb")) == NULL) + { + perror("fopen error:"); + buff_inf_h->buff = NULL; + buff_inf_h->size = 0; + return buff_inf_h;//-1; + } + size_t i = 0;//unsigned int i = 0;//size_t i = 0; + //int j = 0; + while (!feof(fr)) + { + fread(buff, 1, 1, fr); + buff += 1; + i++; + } + buff_inf_h->buff = (char *)malloc(sizeof(char *) * (i-2)); + memcpy(buff_inf_h->buff, buff - i, i-2); + buff_inf_h->size = i - 2; + //buff_inf_h.buff = buff - i; + //buff_inf_h.size = i; + free(buff_ori);//CH--free(buff - i);//? + buff_ori = NULL; + fclose(fr); + return buff_inf_h; + +} +int main(int argc, char *argv[]) +{ + DIR *dirp; + struct dirent *dp; + char *vdir = argv[1]; + char *vfile_ori = (char *)malloc(30); + memcpy(vfile_ori,vdir,strlen(vdir)); + strcat(vfile_ori, "/"); + char *vfile = (char *)malloc(30); + //strcat(vdir,".dir"); + dirp = opendir(vdir); + struct stat info; + + buff_inf *buff_inf_h; + //CH--char *buff;// = (char *)malloc(sizeof(char *)*size);//; + int ret; + int ret_sig = atoi(argv[2]); + int sig_show_all = atoi(argv[3]); + int sig_choose_all = atoi(argv[4]); + size_t size; + while ((dp = readdir(dirp)) != NULL) + { + memcpy(vfile,vfile_ori,30); + strncat(vfile, dp->d_name, strlen(dp->d_name));//strcat(vfile,dp->d_name);//vfile = dp->d_name;//fileinfo->name;//&((fileinfo->name)[0]);//argv[1];// "0003.dat"; + + stat(vfile, &info); + if(S_ISDIR(info.st_mode)) + continue; + buff_inf_h = buff_get(vfile); + if (buff_inf_h->size == 0) + { + printf("read file failed.\n"); + return -1; + } + if (sig_choose_all == 0) + { + int divide_h = 10; + int divide_s = 0; + int divide_e = 1; + + size = (buff_inf_h->size / divide_h)*(divide_e - divide_s); + + //CH--buff = (char *)malloc(sizeof(char *)*size); + //CH--memcpy(buff, buff_inf_h->buff + (buff_inf_h->size / divide_h)*divide_s, size); + + //printf("buff_whole is %p.\n", buff_inf_h->buff); + //printf("buff_inf_h.size/divide_h is %x.\n", buff_inf_h->size / divide_h); + //printf("buff starts from(>=) %p.\n", buff); + //printf("buff ends at(<) %p.\n", buff + size - 1);//+size + ret = AV_container_identify(buff_inf_h->buff + (buff_inf_h->size / divide_h)*divide_s, size);;//CCH--ret = AV_container_identify(buff, size);//CH--(const char *)? + //CH--free(buff); + //CH--buff = NULL; + //printf("define : %d.\n", ret); + //printf("size of whole buff is %d.\n", buff_inf_h->size);//size - 1 + //printf("size of chosen buff is %d.\n", size); + } + else + ret = AV_container_identify(buff_inf_h->buff, buff_inf_h->size); + if (sig_show_all == 0) + { + if (ret != ret_sig) + { + printf("-----------------data of [%s] is kind of [%d].\n", dp->d_name, ret); + //printf("define : %d.\n", ret); + printf("size of whole buff is %d.\n", buff_inf_h->size); + if (sig_choose_all == 0) + printf("size of chosen buff is %d.\n", size); + + } + } + else + { + printf("-----------------data of [%s] is kind of [%d].\n", dp->d_name, ret); + printf("size of whole buff is %d.\n", buff_inf_h->size); + if (sig_choose_all == 0) + printf("size of chosen buff is %d.\n", size); + } + free(buff_inf_h->buff);//CH-- + buff_inf_h->buff = NULL;//CH-- + free(buff_inf_h);//CH-- + buff_inf_h = NULL;//CH-- + } + /* + free(buff); + + free(buff_inf_h->buff); + buff_inf_h = NULL; + free(buff_inf_h); + return ret; + */ + + //CH--free(buff_inf_h->buff); + //CH--buff_inf_h = NULL; + //CH--free(buff_inf_h); + + free(vfile); + free(vfile_ori); + + return ret; +} |
