123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- #include <stdio.h>
- #include <string.h>
- #include <math.h>
- #include <stdbool.h>
- #include <ctype.h>
- #include <libavutil/avutil.h>
- #include "utils.h"
- #include "ffs.h"
- size_t strlcpy(char *dest, const char *src, size_t maxlen) {
- memset(dest, '\0', maxlen);
- int charnum;
- for(charnum = 0; charnum < maxlen && src[charnum] != '\0'; ++charnum) {
- dest[charnum] = src[charnum];
- }
- dest[maxlen - 1] = '\0';
- return(strlen(dest));
- }
- bool is_base10(const char* number_string) {
- size_t ndigits = strlen(number_string);
- int charnum;
- for(charnum = 0; charnum < ndigits && number_string[charnum] != '\0'; ++charnum) {
- if(!isdigit(number_string[charnum])) {
- return false;
- }
- }
- return true;
- }
- void itos10(char* dest, const int src) {
- /// An itoa-like wrapper to snprintf for converting integers to strings (base-10).
- ///
- /// The caller should make sure space has been allocated in dest.
- /// This function assumes dest has size at least equal to FFS_MAX_INT_LEN.
- memset(dest, '\0', FFS_MAX_INT_LEN);
- snprintf(dest, FFS_MAX_INT_LEN, "%d", src);
- dest[FFS_MAX_INT_LEN] = '\0';
- }
- AVCodecParameters* get_stream_codec_parameters(const int stream) {
- return(av_fmt_context->streams[stream]->codecpar);
- }
- int get_path_stream_id(const char* path) {
- if(strlen(path) < 2) {
- return -1;
- }
- char *filename = (char*)malloc(strlen(path));
- if(!filename) {
- fprintf(ffs_log, "Error allocating memory in %s.\n", __func__);
- stop_logging();
- exit(1);
- }
- strlcpy(filename, path + 1, strlen(path));
- char* filename_rem = filename;
- char* stream_base10 = strsep(&filename_rem, ".");
- if(!stream_base10) {
- return -1;
- }
- if(!is_base10(stream_base10)) {
- return -1;
- }
- int s = atoi(stream_base10);
- if(filename) {
- free(filename);
- filename = NULL;
- }
- return s;
- }
- void get_stream_codec_type(char* type_name, const int stream) {
- enum AVMediaType type_id = get_stream_codec_parameters(stream)->codec_type;
- strlcpy(type_name, av_get_media_type_string(type_id), FFS_MAX_TYPE_LEN);
- }
- void get_stream_codec_name(char* codec_name, const int stream) {
- enum AVCodecID cid = get_stream_codec_parameters(stream)->codec_id;
- strlcpy(codec_name, avcodec_get_name(cid), FFS_MAX_CODEC_LEN);
- }
- void build_stream_descriptor(char* codec, const int stream) {
- char* stream_base10 = (char*)malloc(FFS_MAX_INT_LEN);
- if(!stream_base10) goto descriptor_string_malloc_err;
- itos10(stream_base10, stream);
- char* codec_type = (char*)malloc(FFS_MAX_TYPE_LEN);
- if(!codec_type) goto descriptor_string_malloc_err;
- get_stream_codec_type(codec_type, stream);
- char* codec_name = (char*)malloc(FFS_MAX_CODEC_LEN);
- if(!codec_name) goto descriptor_string_malloc_err;
- get_stream_codec_name(codec_name, stream);
- snprintf(codec, FFS_MAX_DESCRIPTOR_LEN, "%s.%s.%s", stream_base10, codec_type, codec_name);
- codec[FFS_MAX_DESCRIPTOR_LEN] = '\0';
- if(stream_base10 != NULL) {
- free(stream_base10);
- stream_base10 = NULL;
- }
- if(codec_type != NULL) {
- free(codec_type);
- codec_type = NULL;
- }
- if(codec_name != NULL) {
- free(codec_name);
- codec_name = NULL;
- }
- return;
- descriptor_string_malloc_err:
- fprintf(ffs_log, "Memory allocation error in %s(..., stream = %d).\n", __func__, stream);
- stop_logging();
- exit(1);
- }
- bool valid_stream_descriptor(const char* path) {
- bool valid_stream_number = true;
- int s = get_path_stream_id(path);
- if(s < 0 || s >= av_fmt_context->nb_streams) {
- valid_stream_number = false;
- }
- char *filename_orig = (char*)malloc(strlen(path));
- if(!filename_orig) {
- fprintf(ffs_log, "Error allocating memory in %s.\n", __func__);
- stop_logging();
- exit(1);
- }
- strlcpy(filename_orig, path + 1, strlen(path));
- char* filename_rem = filename_orig;
- bool valid_codec_type = true;
- char* stream_base10 = strsep(&filename_rem, ".");
- char* path_codec_type = strsep(&filename_rem, ".");
- if(path_codec_type) {
- char* true_codec_type = (char*)malloc(FFS_MAX_TYPE_LEN);
- if(!true_codec_type) {
- fprintf(ffs_log, "Error allocating memory in %s.\n", __func__);
- stop_logging();
- exit(1);
- }
- get_stream_codec_type(true_codec_type, s);
- if(strncmp(path_codec_type, true_codec_type, FFS_MAX_TYPE_LEN) != 0) {
- valid_codec_type = false;
- }
- if(true_codec_type) {
- free(true_codec_type);
- true_codec_type = NULL;
- }
- } else {
- valid_codec_type = false;
- }
- bool valid_codec_name = true;
- char* path_codec_name = strsep(&filename_rem, ".");
- if(path_codec_name) {
- char* true_codec_name = (char*)malloc(FFS_MAX_CODEC_LEN);
- if(!true_codec_name) {
- fprintf(ffs_log, "Error allocating memory in %s.\n", __func__);
- stop_logging();
- exit(1);
- }
- get_stream_codec_name(true_codec_name, s);
- if(strncmp(path_codec_name, true_codec_name, FFS_MAX_CODEC_LEN) != 0) {
- valid_codec_name = false;
- }
- if(true_codec_name != NULL) {
- free(true_codec_name);
- true_codec_name = NULL;
- }
- } else {
- valid_codec_name = false;
- }
- if(filename_orig) {
- free(filename_orig);
- filename_orig = NULL;
- }
- return valid_stream_number && valid_codec_type && valid_codec_name;
- }
- bool valid_path(const char* path) {
- if (strcmp(path, "/") == 0) {
- return true;
- }
- char* trash_prefix = "/.Trash";
- if(strncmp(path, trash_prefix, strlen(trash_prefix)) == 0) {
- return true;
- }
- return valid_stream_descriptor(path);
- }
|