📄 fdt_gen.c
字号:
strcpy(enc_fullpath, fullpath); strcat(enc_fullpath, GZ_SUFFIX);#ifdef WIN32 if(_stat64(enc_fullpath, &enc_file_stats) == -1) {#else if(stat64(enc_fullpath, &enc_file_stats) == -1) {#endif printf("Error: %s is not valid file name\n", enc_fullpath); fflush(stdout); free_uri(uri); continue; } fprintf(fp, "\n\t\t"); fprintf(fp, "Content-Encoding=\"%s\"", "gzip");#ifdef USE_OPENSSL md5 = file_md5(enc_fullpath); if(md5 == NULL) { free(hostname); free_uri(uri); continue; } fprintf(fp, "\n\t\t"); fprintf(fp, "Content-MD5=\"%s\"", md5);#endif fprintf(fp, "\n\t\t"); fprintf(fp, "Transfer-Length=\"%llu\"", enc_file_stats.st_size); } }#endif else {#ifdef USE_OPENSSL md5 = file_md5(fullpath); if(md5 == NULL) { free(hostname); free_uri(uri); continue; } fprintf(fp, "\n\t\t"); fprintf(fp, "Content-MD5=\"%s\"", md5);#endif } #ifdef FDT_INST_FEC_OTI_FILE if(!s->use_fec_oti_ext_hdr) { fprintf(fp, "\n\t\t"); fprintf(fp, "FEC-OTI-FEC-Encoding-ID=\"%u\"", s->def_fec_enc_id); if(s->def_fec_enc_id >= 128) { fprintf(fp, "\n\t\t"); fprintf(fp, "FEC-OTI-FEC-Instance-ID=\"%u\"", s->def_fec_inst_id); } fprintf(fp, "\n\t\t"); fprintf(fp, "FEC-OTI-Maximum-Source-Block-Length=\"%u\"", s->def_max_sblen); fprintf(fp, "\n\t\t"); fprintf(fp, "FEC-OTI-Encoding-Symbol-Length=\"%u\"", s->def_eslen); if(s->def_fec_enc_id == SB_SYS_FEC_ENC_ID) { fprintf(fp, "\n\t\t"); fprintf(fp, "FEC-OTI-Max-Number-of-Encoding-Symbols=\"%u\"", max_n); } }#endif fprintf(fp, "/>\n"); toi++; free_uri(uri);#ifdef USE_OPENSSL free(md5); md5 = NULL;#endif } } FindClose(dirptr);#else user = getenv("USER"); memset(findfile, 0, MAX_PATH); if(!(strcmp(base_dir, "") == 0)) { strcpy(findfile, base_dir); strcat(findfile, "/"); } strcat(findfile, directory); dirptr = opendir(findfile); if(dirptr == NULL) { printf("%s is not valid directory name\n", findfile); fflush(stdout); free(hostname); return -1; } entry = readdir(dirptr); while(entry != NULL) { if(checkpath(entry->d_name)) { entry = readdir(dirptr); continue; } memset(fullname, 0 , MAX_PATH); strcpy(fullname, directory); if(fullname[strlen(fullname) - 1] != '/') { strcat(fullname, "/"); } strcat(fullname, entry->d_name); memset(fullpath, 0, MAX_PATH); if(!(strcmp(base_dir, "") == 0)) { strcpy(fullpath, base_dir); strcat(fullpath, "/"); } strcat(fullpath, fullname); if(stat64(fullpath, &file_stats) == -1) { printf("Error: %s is not valid file name\n", fullpath); fflush(stdout); entry = readdir(dirptr); continue; } if(S_ISDIR(file_stats.st_mode)) { result = encode_directory(fullname, base_dir, fp, s_id); if(result < 0) { entry = readdir(dirptr); continue; } } else { if(file_stats.st_size == 0) { printf("Error: file %s size = 0\n", fullpath); fflush(stdout); entry = readdir(dirptr); continue; } uri = alloc_uri_struct(); set_uri_scheme(uri, "file"); if(user != NULL) { set_uri_user(uri, user); } #ifdef HOSTNAME_TO_FDT if(hostname != NULL) { set_uri_host(uri, hostname); }#endif set_uri_path(uri, fullname); fprintf(fp, "\t<File TOI=\"%llu\"", toi); uri_str = uri_string(uri); fprintf(fp, "\n\t\t"); fprintf(fp, "Content-Location=\"%s\"", uri_str); free(uri_str); uri_str = NULL; fprintf(fp, "\n\t\t"); fprintf(fp, "Content-Length=\"%llu\"", file_stats.st_size); if(s->encode_content == PAD_FILES) { if(compute_padding_length(file_stats.st_size, s->def_max_sblen, s->def_eslen)) { fprintf(fp, "\n\t\t"); fprintf(fp, "Content-Encoding=\"%s\"", "pad"); fprintf(fp, "\n\t\t"); fprintf(fp, "Transfer-Length=\"%llu\"", compute_padding_length(file_stats.st_size, s->def_max_sblen, s->def_eslen) + file_stats.st_size); }/*#ifdef USE_OPENSSL md5 = file_md5(fullpath); if(md5 == NULL) { free_uri(uri); continue; } fprintf(fp, "\n\t\t"); fprintf(fp, "Content-MD5=\"%s\"", md5);#endif*/ }#ifdef USE_ZLIB else if(s->encode_content == ZLIB_FDT_AND_GZIP_FILES) { retcode = file_gzip_compress(fullpath, "wb"); if(retcode == 0) { memset(enc_fullpath, 0 , MAX_PATH); strcpy(enc_fullpath, fullpath); strcat(enc_fullpath, GZ_SUFFIX); if(stat64(enc_fullpath, &enc_file_stats) == -1) { printf("Error: %s is not valid file name\n", enc_fullpath); fflush(stdout); entry = readdir(dirptr); free_uri(uri); continue; } fprintf(fp, "\n\t\t"); fprintf(fp, "Content-Encoding=\"%s\"", "gzip"); #ifdef USE_OPENSSL md5 = file_md5(enc_fullpath); if(md5 == NULL) { free_uri(uri); continue; } fprintf(fp, "\n\t\t"); fprintf(fp, "Content-MD5=\"%s\"", md5);#endif fprintf(fp, "\n\t\t"); fprintf(fp, "Transfer-Length=\"%llu\"", enc_file_stats.st_size); } }#endif else {#ifdef USE_OPENSSL md5 = file_md5(fullpath); if(md5 == NULL) { free_uri(uri); continue; } fprintf(fp, "\n\t\t"); fprintf(fp, "Content-MD5=\"%s\"", md5);#endif } #ifdef FDT_INST_FEC_OTI_FILE if(!s->use_fec_oti_ext_hdr) { fprintf(fp, "\n\t\t"); fprintf(fp, "FEC-OTI-FEC-Encoding-ID=\"%u\"", s->def_fec_enc_id); if(s->def_fec_enc_id >= 128) { fprintf(fp, "\n\t\t"); fprintf(fp, "FEC-OTI-FEC-Instance-ID=\"%u\"", s->def_fec_inst_id); } fprintf(fp, "\n\t\t"); fprintf(fp, "FEC-OTI-Maximum-Source-Block-Length=\"%u\"", s->def_max_sblen); fprintf(fp, "\n\t\t"); fprintf(fp, "FEC-OTI-Encoding-Symbol-Length=\"%u\"", s->def_eslen); if(s->def_fec_enc_id == SB_SYS_FEC_ENC_ID) { fprintf(fp, "\n\t\t"); fprintf(fp, "FEC-OTI-Max-Number-of-Encoding-Symbols=\"%u\"", max_n); } }#endif fprintf(fp, "/>\n"); toi++; free_uri(uri);#ifdef USE_OPENSSL free(md5); md5 = NULL;#endif } entry = readdir(dirptr); } closedir(dirptr);#endif if(hostname != NULL) { free(hostname); } if(toi == 1) { return -1; } return 0;}/* * This function encodes file to fdt. * * Params: char *file: Pointer to buffer containing file to be encoded, * char *base_dir: Pointer to buffer containing base directory for file * or directory to be parsed to the fdt, * FILE *fp: File pointer, * int *s_id: Pointer to session identifier. * * Return: int: 0 in success, -1 otherwise. * */int encode_file(char *file, char *base_dir, FILE *fp, int *s_id) { char fullname[MAX_PATH + MAX_LENGTH]; char fullpath[MAX_PATH + MAX_LENGTH]; /*struct stat file_stats;*/#ifdef WIN32 struct __stat64 file_stats;#else struct stat64 file_stats;#endif int i; uri_t *uri = NULL; char *hostname = NULL; alc_session_t *s = get_alc_session(*s_id); char *user = NULL; char *uri_str = NULL; #ifdef USE_ZLIB int retcode; char enc_fullpath[MAX_PATH + MAX_LENGTH]; /*struct stat enc_file_stats;*/#ifdef WIN32 struct __stat64 enc_file_stats;#else struct stat64 enc_file_stats;#endif#endif#ifdef USE_OPENSSL char *md5 = NULL;#endif#ifdef FDT_INST_FEC_OTI_FILE div_t div_max_n; int max_n; div_max_n = div((s->def_max_sblen * (100 + s->def_fec_ratio)), 100); max_n = div_max_n.quot;#endif#ifdef WIN32 user = getenv("USERNAME");#else user = getenv("USER");#endif memset(fullpath, 0, (MAX_PATH + MAX_LENGTH)); if(!(strcmp(base_dir, "") == 0)) { strcpy(fullpath, base_dir);#ifdef WIN32 strcat(fullpath, "\\");#else strcat(fullpath, "/");#endif } strcat(fullpath, file);#ifdef WIN32 if(_stat64(fullpath, &file_stats) == -1) {#else if(stat64(fullpath, &file_stats) == -1) {#endif printf("Error: %s is not valid file name\n", fullpath); fflush(stdout); return -1; } if(file_stats.st_size == 0) { printf("Error: file %s size = 0\n", fullpath); fflush(stdout); return -1; } hostname = getdnsname(); memset(fullname, 0, (MAX_PATH + MAX_LENGTH)); strcpy(fullname, file); for(i = 0; i < (int)strlen(fullname); i++) { if(fullname[i] == '\\') { fullname[i] = '/'; } } uri = alloc_uri_struct(); set_uri_scheme(uri, "file"); if(user != NULL) { set_uri_user(uri, user); } #ifdef HOSTNAME_TO_FDT if(hostname != NULL) { set_uri_host(uri, hostname); }#endif set_uri_path(uri, fullname);#ifdef WIN32 fprintf(fp, "\t<File TOI=\"%I64u\"", toi);#else fprintf(fp, "\t<File TOI=\"%llu\"", toi);#endif uri_str = uri_string(uri); fprintf(fp, "\n\t\t"); fprintf(fp, "Content-Location=\"%s\"", uri_str); free(uri_str); fprintf(fp, "\n\t\t"); fprintf(fp, "Content-Length=\"%llu\"", file_stats.st_size); if(s->encode_content == PAD_FILES) { if(compute_padding_length(file_stats.st_size, s->def_max_sblen, s->def_eslen)) { fprintf(fp, "\n\t\t"); fprintf(fp, "Content-Encoding=\"%s\"", "pad"); fprintf(fp, "\n\t\t"); fprintf(fp, "Transfer-Length=\"%llu\"", compute_padding_length(file_stats.st_size, s->def_max_sblen, s->def_eslen) + file_stats.st_size); }/*#ifdef USE_OPENSSL md5 = file_md5(fullpath); if(md5 == NULL) { free(hostname); free_uri(uri); return -1; } fprintf(fp, "\n\t\t"); fprintf(fp, "Content-MD5=\"%s\"", md5);#endif*/ }#ifdef USE_ZLIB else if(s->encode_content == ZLIB_FDT_AND_GZIP_FILES) { retcode = file_gzip_compress(fullpath, "wb"); if(retcode == 0) { fprintf(fp, "\n\t\t"); fprintf(fp, "Content-Encoding=\"%s\"", "gzip"); memset(enc_fullpath, 0 , MAX_PATH); strcpy(enc_fullpath, fullpath); strcat(enc_fullpath, GZ_SUFFIX);#ifdef WIN32 if(_stat64(enc_fullpath, &enc_file_stats) == -1) {#else if(stat64(enc_fullpath, &enc_file_stats) == -1) {#endif printf("Error: %s is not valid file name\n", enc_fullpath); fflush(stdout); free(hostname); free_uri(uri); return -1; }#ifdef USE_OPENSSL md5 = file_md5(enc_fullpath); if(md5 == NULL) { free(hostname); free_uri(uri); return -1; } fprintf(fp, "\n\t\t"); fprintf(fp, "Content-MD5=\"%s\"", md5);#endif fprintf(fp, "\n\t\t"); fprintf(fp, "Transfer-Length=\"%llu\"", enc_file_stats.st_size); } }#endif else {#ifdef USE_OPENSSL md5 = file_md5(fullpath); if(md5 == NULL) { free(hostname); free_uri(uri); return -1; } fprintf(fp, "\n\t\t"); fprintf(fp, "Content-MD5=\"%s\"", md5);#endif } #ifdef FDT_INST_FEC_OTI_FILE if(!s->use_fec_oti_ext_hdr) { fprintf(fp, "\n\t\t"); fprintf(fp, "FEC-OTI-FEC-Encoding-ID=\"%u\"", s->def_fec_enc_id); if(s->def_fec_enc_id >= 128) { fprintf(fp, "\n\t\t"); fprintf(fp, "FEC-OTI-FEC-Instance-ID=\"%u\"", s->def_fec_inst_id); } fprintf(fp, "\n\t\t"); fprintf(fp, "FEC-OTI-Maximum-Source-Block-Length=\"%u\"", s->def_max_sblen); fprintf(fp, "\n\t\t"); fprintf(fp, "FEC-OTI-Encoding-Symbol-Length=\"%u\"", s->def_eslen); if(s->def_fec_enc_id == SB_SYS_FEC_ENC_ID) { fprintf(fp, "\n\t\t"); fprintf(fp, "FEC-OTI-Max-Number-of-Encoding-Symbols=\"%u\"", max_n); } }#endif fprintf(fp, "/>\n"); free_uri(uri);#ifdef USE_OPENSSL free(md5);#endif if(hostname != NULL) { free(hostname); } return 0;}/* * This function checks if path is empty, or . or .. * * Params: const char *path: Pointer to buffer containg path. * * Return: int: 1 if path is empty or . or .., 0 otherwise. * */int checkpath(const char *path) { int ret = 0; if(strcmp(path, "") == 0) { ret = 1; } else if(strcmp(path, ".") == 0) { ret = 1; } else if(strcmp(path, "..") == 0) { ret = 1; } return ret;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -