fdt_gen.c

Go to the documentation of this file.
00001 
00033 #include <stdlib.h>
00034 #include <stdio.h>
00035 #include <sys/stat.h>
00036 #include <string.h>
00037 #include <sys/types.h>
00038 
00039 #ifdef _MSC_VER
00040 #include <windows.h>
00041 #else
00042 #include <dirent.h>
00043 #endif
00044 
00045 #include "../alclib/alc_session.h"
00046 #include "../alclib/fec.h"
00047 #include "../alclib/blocking_alg.h"
00048 
00049 #include "fdt_gen.h"
00050 #include "uri.h"
00051 #include "getdnsname.h"
00052 #include "padding_encoding.h"
00053 #include "mad_zlib.h"
00054 #include "mad_md5.h"
00055 
00056 unsigned long long toi; 
00067 int checkpath(const char *path) {
00068         int ret = 0;
00069 
00070         if(strcmp(path, "") == 0) {
00071                 ret = 1;
00072         }
00073         else if(strcmp(path, ".") == 0) {
00074                 ret = 1;
00075         }
00076         else if(strcmp(path, "..") == 0) {
00077                 ret = 1;
00078         }
00079 
00080         return ret;
00081 }
00082 
00096 int is_enough_source_block_numbers(unsigned int max_sb_len, int es_len, unsigned long long f_size,
00097                                                                    unsigned char fec_enc_id, unsigned short fec_inst_id) {
00098 
00099   blocking_struct_t *bs;
00100   int retval = 0;
00101   
00102   /* Let's compute the blocking structure */
00103   bs = compute_blocking_structure(f_size, max_sb_len, es_len);
00104   
00105   if(fec_enc_id == COM_NO_C_FEC_ENC_ID || fec_enc_id == COM_FEC_ENC_ID) {
00106     if(bs->N > 65536U) {
00107       retval = -1;
00108     }
00109   }
00110   else if(fec_enc_id == RS_FEC_ENC_ID) {
00111     if(bs->N > 16777216U) {
00112       retval = -1;
00113     }
00114   }
00115   else if((fec_enc_id == SB_SYS_FEC_ENC_ID&& fec_inst_id == REED_SOL_FEC_INST_ID) ||
00116           fec_enc_id == SIMPLE_XOR_FEC_ENC_ID || fec_enc_id == SB_LB_E_FEC_ENC_ID) {
00117     if(bs->N > 4294967295U) { /* TODO: now it is not possible to use 4294967296, because bs->N is unsigned int */
00118       retval = -1;
00119     }
00120   }
00121   
00122   free(bs);
00123   return retval;
00124 }
00125 
00138 int encode_file(char *file, char *base_dir, FILE *fp, int *s_id) {
00139 
00140         char fullname[MAX_PATH_LENGTH];
00141         char fullpath[MAX_PATH_LENGTH];
00142 
00143 #ifdef _MSC_VER
00144         struct __stat64 file_stats;
00145 #else
00146         struct stat64 file_stats;
00147 #endif
00148         int i;
00149 
00150         uri_t *uri = NULL;
00151         char *hostname = NULL;
00152         alc_session_t *s = get_alc_session(*s_id);
00153         char *user = NULL;
00154         char *uri_str = NULL;   
00155         
00156 #ifdef USE_ZLIB
00157         int retcode;
00158         char enc_fullpath[MAX_PATH_LENGTH];
00159 
00160 #ifdef _MSC_VER
00161         struct __stat64 enc_file_stats;
00162 #else
00163         struct stat64 enc_file_stats;
00164 #endif
00165 #endif
00166 
00167 #ifdef USE_OPENSSL
00168         char *md5 = NULL;
00169 #endif
00170 
00171 #ifdef FDT_INST_FEC_OTI_FILE
00172         div_t div_max_n;
00173         int max_n;
00174 
00175         div_max_n = div((s->def_max_sblen * (100 + s->def_fec_ratio)), 100);
00176         max_n = div_max_n.quot;
00177 #endif
00178 
00179         unsigned long long padding_length;
00180 
00181 #ifdef _MSC_VER
00182         user = getenv("USERNAME");
00183 #else
00184         user = getenv("USER");
00185 #endif
00186 
00187         memset(fullpath, 0, MAX_PATH_LENGTH);
00188 
00189         if(!(strcmp(base_dir, "") == 0)) {
00190                 strcpy(fullpath, base_dir);
00191 
00192 #ifdef _MSC_VER
00193                 strcat(fullpath, "\\");
00194 #else
00195                 strcat(fullpath, "/");
00196 #endif
00197         }
00198 
00199         strcat(fullpath, file);
00200 
00201 #ifdef _MSC_VER
00202         if(_stat64(fullpath, &file_stats) == -1) {
00203 #else
00204         if(stat64(fullpath, &file_stats) == -1) {
00205 #endif
00206                 printf("Error: %s is not valid file name\n", fullpath);
00207                 fflush(stdout);
00208                 return -1;
00209         }
00210 
00211         if(file_stats.st_size == 0) {
00212                 printf("Error: file %s size = 0\n", fullpath);
00213                 fflush(stdout);
00214                 return -1;
00215         }
00216 
00217         hostname = getdnsname();        
00218 
00219         memset(fullname, 0, MAX_PATH_LENGTH);
00220         strcpy(fullname, file);
00221 
00222         for(i = 0; i < (int)strlen(fullname); i++) {
00223                 if(fullname[i] == '\\') {
00224                         fullname[i] = '/';
00225                 }
00226         }
00227 
00228         uri = alloc_uri_struct();
00229 
00230         set_uri_scheme(uri, "file");
00231 
00232         if(user != NULL) {
00233                 set_uri_user(uri, user);
00234         }       
00235 
00236 #ifdef HOSTNAME_TO_FDT
00237         if(hostname != NULL) {
00238                 set_uri_host(uri, hostname);
00239         }
00240 #endif
00241 
00242         set_uri_path(uri, fullname);
00243 
00244 #ifdef _MSC_VER
00245         fprintf(fp, "\t<File TOI=\"%I64u\"", toi);
00246 #else
00247         fprintf(fp, "\t<File TOI=\"%llu\"", toi);
00248 #endif
00249 
00250         uri_str = uri_string(uri);
00251         
00252         fprintf(fp, "\n\t\t");
00253         fprintf(fp, "Content-Location=\"%s\"", uri_str);
00254         
00255         free(uri_str);
00256         
00257         fprintf(fp, "\n\t\t");
00258         fprintf(fp, "Content-Length=\"%llu\"", file_stats.st_size);
00259 
00260         if(s->encode_content == 0 || s->encode_content == ZLIB_FDT) {
00261                 if(is_enough_source_block_numbers(s->def_max_sblen, s->def_eslen, file_stats.st_size,
00262                                                                    s->def_fec_enc_id, s->def_fec_inst_id) < 0) {
00263                         printf("Maximum source block length %i too small for the file: %s\n", s->def_max_sblen, file);
00264                         fflush(stdout);
00265                         if(hostname != NULL) {
00266                                 free(hostname);
00267                         }
00268                         free_uri(uri);
00269                         return -1;
00270                 }
00271         }
00272 
00273         if(s->encode_content == PAD_FILES) {
00274                 padding_length = compute_padding_length(file_stats.st_size, s->def_max_sblen, s->def_eslen);
00275 
00276         if(padding_length) {
00277                 fprintf(fp, "\n\t\t");
00278                 fprintf(fp, "Content-Encoding=\"%s\"", "pad");
00279 
00280                 fprintf(fp, "\n\t\t");
00281             fprintf(fp, "Transfer-Length=\"%llu\"", (file_stats.st_size + padding_length));
00282         }
00283 
00284                 if(is_enough_source_block_numbers(s->def_max_sblen, s->def_eslen, (file_stats.st_size + padding_length),
00285                                                                    s->def_fec_enc_id, s->def_fec_inst_id) < 0) {
00286                         printf("Maximum source block length %i too small for the file: %s\n", s->def_max_sblen, file);
00287                         fflush(stdout);
00288                         if(hostname != NULL) {
00289                                 free(hostname);
00290                         }
00291                         free_uri(uri);
00292                         return -1;
00293                 }
00294         }
00295 
00296 #ifdef USE_ZLIB         
00297         else if(s->encode_content == ZLIB_FDT_AND_GZIP_FILES) {
00298                 
00299                 retcode = file_gzip_compress(fullpath, "wb");
00300                                                                                                                                                               
00301                 if(retcode == 0) {
00302                         fprintf(fp, "\n\t\t");
00303                         fprintf(fp, "Content-Encoding=\"%s\"", "gzip");
00304                                                                                                                                                               
00305                         memset(enc_fullpath, 0 , MAX_PATH_LENGTH);
00306                         strcpy(enc_fullpath, fullpath);
00307                         strcat(enc_fullpath, GZ_SUFFIX);
00308 #ifdef _MSC_VER                                                                                                                                                      
00309                         if(_stat64(enc_fullpath, &enc_file_stats) == -1) {
00310 #else
00311                         if(stat64(enc_fullpath, &enc_file_stats) == -1) {
00312 #endif
00313                         printf("Error: %s is not valid file name\n", enc_fullpath);
00314                         fflush(stdout);
00315                                                 if(hostname != NULL) {
00316                                                         free(hostname);
00317                                                 }
00318                                                 free_uri(uri);
00319                         return -1;
00320                 }
00321 
00322 #ifdef USE_OPENSSL
00323                                         if(s->calculate_session_size == FALSE) {
00324                                          md5 = file_md5(enc_fullpath);
00325 
00326                                          if(md5 == NULL) {
00327                                                 if(hostname != NULL) {
00328                                                         free(hostname);
00329                                                 }
00330                                                 free_uri(uri);
00331                                                 return -1;
00332                                          } 
00333                                         
00334                                          fprintf(fp, "\n\t\t");
00335                                          fprintf(fp, "Content-MD5=\"%s\"", md5);
00336                                         }
00337 #endif
00338                                                                                                                                                               
00339                                         fprintf(fp, "\n\t\t");
00340                                         fprintf(fp, "Transfer-Length=\"%llu\"", enc_file_stats.st_size);
00341 
00342                                         if(is_enough_source_block_numbers(s->def_max_sblen, s->def_eslen, enc_file_stats.st_size,
00343                                                                    s->def_fec_enc_id, s->def_fec_inst_id) < 0) {
00344                                                 printf("Maximum source block length %i too small for the file: %s\n", s->def_max_sblen, file);
00345                                                 fflush(stdout);
00346                                                 if(hostname != NULL) {
00347                                                         free(hostname);
00348                                                 }
00349                                                 free_uri(uri);
00350                                                 return -1;
00351                                         }
00352                 }
00353         }
00354 #endif
00355         else {
00356 
00357 #ifdef USE_OPENSSL
00358                         if(s->calculate_session_size == FALSE) {
00359                          md5 = file_md5(fullpath);
00360 
00361                          if(md5 == NULL) {
00362                                 if(hostname != NULL) {
00363                                         free(hostname);
00364                                 }
00365                                 free_uri(uri);
00366                                 return -1;
00367                          }
00368 
00369                          fprintf(fp, "\n\t\t");
00370                          fprintf(fp, "Content-MD5=\"%s\"", md5);
00371                         }
00372 #endif
00373         }
00374         
00375 #ifdef FDT_INST_FEC_OTI_FILE
00376         if(!s->use_fec_oti_ext_hdr) {
00377 
00378                 fprintf(fp, "\n\t\t");
00379                 fprintf(fp, "FEC-OTI-FEC-Encoding-ID=\"%u\"", s->def_fec_enc_id);
00380 
00381                 if(s->def_fec_enc_id >= 128) {
00382                         fprintf(fp, "\n\t\t");
00383                         fprintf(fp, "FEC-OTI-FEC-Instance-ID=\"%u\"", s->def_fec_inst_id);
00384                 }
00385 
00386                 if(s->def_fec_enc_id == RS_FEC_ENC_ID) {
00387                         fprintf(fp, "\n\t");
00388                         fprintf(fp, "FEC-OTI-Finite-Field-Parameter=\"%u\"", GF_BITS);
00389                         fprintf(fp, "\n\t");
00390                         fprintf(fp, "FEC-OTI-Number-of-Encoding-Symbols-per-Group=\"%u\"", 1);
00391                 }
00392 
00393                 fprintf(fp, "\n\t\t");
00394                 fprintf(fp, "FEC-OTI-Maximum-Source-Block-Length=\"%u\"", s->def_max_sblen);
00395                 fprintf(fp, "\n\t\t");
00396                 fprintf(fp, "FEC-OTI-Encoding-Symbol-Length=\"%u\"", s->def_eslen);
00397 
00398                 if(s->def_fec_enc_id == SB_SYS_FEC_ENC_ID) {
00399                         fprintf(fp, "\n\t\t");
00400                         fprintf(fp, "FEC-OTI-Max-Number-of-Encoding-Symbols=\"%u\"", max_n);    
00401                 }
00402         }
00403 #endif
00404 
00405         fprintf(fp, "/>\n");
00406         toi++;
00407         free_uri(uri);
00408 
00409 #ifdef USE_OPENSSL
00410         if(s->calculate_session_size == FALSE) {
00411                 free(md5);
00412         }
00413 #endif
00414         
00415         if(hostname != NULL) {
00416                 free(hostname);
00417         }
00418 
00419         return 0;
00420 }
00421 
00434 int encode_directory(char *directory, char *base_dir, FILE *fp, int *s_id) {
00435 
00436         int result;
00437         char fullname[MAX_PATH_LENGTH];
00438         char fullpath[MAX_PATH_LENGTH];
00439 
00440 #ifdef _MSC_VER
00441         struct __stat64 file_stats;
00442 #else
00443         struct stat64 file_stats;
00444 #endif
00445 
00446         uri_t *uri = NULL;
00447         char *hostname = NULL;
00448         alc_session_t *s = NULL;
00449         char *user = NULL;
00450 
00451 #ifdef USE_ZLIB
00452         int retcode;
00453         char enc_fullpath[MAX_PATH_LENGTH];
00454 #ifdef _MSC_VER
00455         struct __stat64 enc_file_stats;
00456 #else
00457         struct stat64 enc_file_stats;
00458 #endif
00459 #endif
00460 
00461 #ifdef USE_OPENSSL
00462         char *md5 = NULL;
00463 #endif
00464 
00465 #ifdef _MSC_VER
00466         int i;
00467         char findfile[MAX_PATH_LENGTH + 3];
00468         HANDLE dirptr = NULL;
00469         WIN32_FIND_DATA entry;
00470 #else
00471         struct dirent *entry;
00472         DIR *dirptr = NULL;
00473         char findfile[MAX_PATH_LENGTH];
00474 #endif
00475 
00476 #ifdef FDT_INST_FEC_OTI_FILE
00477         div_t div_max_n;
00478         int max_n;
00479 #endif
00480 
00481         char *uri_str = NULL;
00482 
00483         hostname = getdnsname();
00484 
00485         s = get_alc_session(*s_id);
00486 
00487 #ifdef FDT_INST_FEC_OTI_FILE
00488         div_max_n = div((s->def_max_sblen * (100 + s->def_fec_ratio)), 100);
00489         max_n = div_max_n.quot;
00490 #endif
00491 
00492 #ifdef _MSC_VER
00493         user = getenv("USERNAME");
00494 
00495         memset(findfile, 0, (MAX_PATH_LENGTH + 3));
00496 
00497         if(!(strcmp(base_dir, "") == 0)) {
00498                 strcpy(findfile, base_dir);
00499                 strcat(findfile, "\\");
00500         }
00501 
00502         strcat(findfile, directory);
00503         strcat(findfile, "\\*");
00504         
00505         dirptr = FindFirstFile(findfile, &entry);
00506 
00507         if(dirptr == INVALID_HANDLE_VALUE) {
00508                 printf("Error: %s is not valid directory name\n", directory);
00509                 fflush(stdout);
00510                 free(hostname);
00511                 return -1;
00512         }
00513 
00514         if(!checkpath(entry.cFileName)) {
00515 
00516                 memset(fullname, 0 , MAX_PATH_LENGTH);
00517                 strcpy(fullname, directory);
00518 
00519                 if(fullname[strlen(fullname) - 1] != '\\') {
00520                         strcat(fullname, "\\");
00521                 }
00522 
00523                 strcat(fullname, entry.cFileName);
00524 
00525                 for(i = 0; i < (int)strlen(fullname); i++) {
00526                         if(fullname[i] == '\\') {
00527                                 fullname[i] = '/';
00528                         }
00529                 }
00530 
00531                 if(entry.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
00532                         result = encode_directory(fullname, base_dir, fp, s_id);
00533 
00534                         if(result < 0) {
00535                                 free(hostname);
00536                                 return -1;
00537                         }
00538                 }
00539                 else {
00540 
00541                         memset(fullpath, 0, MAX_PATH_LENGTH);
00542 
00543                         if(!(strcmp(base_dir, "") == 0)) {
00544                                 strcpy(fullpath, base_dir);
00545                                 strcat(fullpath, "\\");
00546                         }
00547 
00548                         strcat(fullpath, fullname);
00549 
00550 #ifdef _MSC_VER
00551                         if(_stat64(fullpath, &file_stats) == -1) {
00552 #else
00553                         if(stat64(fullpath, &file_stats) == -1) {
00554 #endif
00555                                 printf("Error: %s is not valid file name\n", fullpath);
00556                                 fflush(stdout);
00557                                 free(hostname);
00558                                 return -1;
00559                         }               
00560 
00561                         if(file_stats.st_size == 0) {
00562                                 printf("Error: file %s size = 0\n", fullpath);
00563                                 fflush(stdout);
00564                                 free(hostname);
00565                                 return -1;
00566                         }
00567                         
00568                         uri = alloc_uri_struct();
00569 
00570                         set_uri_scheme(uri, "file");
00571 
00572                         if(user != NULL) {
00573                                 set_uri_user(uri, user);
00574                         }
00575 
00576 #ifdef HOSTNAME_TO_FDT
00577                         if(hostname != NULL) {
00578                                 set_uri_host(uri, hostname);
00579                         }
00580 #endif
00581 
00582                         set_uri_path(uri, fullname);
00583 
00584                         fprintf(fp, "\t<File TOI=\"%I64u\"", toi);
00585                         
00586                         uri_str = uri_string(uri);
00587 
00588                         fprintf(fp, "\n\t\t");
00589                         fprintf(fp, "Content-Location=\"%s\"", uri_str);
00590 
00591                         free(uri_str);
00592                         uri_str = NULL;
00593 
00594                         fprintf(fp, "\n\t\t");
00595                         fprintf(fp, "Content-Length=\"%llu\"", file_stats.st_size);
00596 
00597                         if(s->encode_content == PAD_FILES) {
00598 
00599                                 if(compute_padding_length(file_stats.st_size, s->def_max_sblen, s->def_eslen)) {
00600                                         fprintf(fp, "\n\t\t");
00601                                         fprintf(fp, "Content-Encoding=\"%s\"", "pad"); 
00602 
00603                                         fprintf(fp, "\n\t\t");
00604                                         fprintf(fp, "Transfer-Length=\"%llu\"", compute_padding_length(file_stats.st_size, s->def_max_sblen, s->def_eslen) + file_stats.st_size);
00605                                                                 }
00606 /* Not possible to use, because padded file is not in the hard disk in the sender side
00607 #ifdef USE_OPENSSL
00608                                 if(s->calculate_session_size == FALSE) {
00609                                                                  md5 = file_md5(fullpath);
00610 
00611                                  if(md5 == NULL) {
00612                                         free(hostname);        
00613                                         free_uri(uri);           
00614                                         return -1;
00615                                  }
00616                               
00617                                 fprintf(fp, "\n\t\t"); 
00618                                 fprintf(fp, "Content-MD5=\"%s\"", md5);
00619                                                            }
00620 #endif
00621 */
00622                         }
00623 #ifdef USE_ZLIB
00624                         else if(s->encode_content == ZLIB_FDT_AND_GZIP_FILES) {
00625 
00626                                 retcode = file_gzip_compress(fullpath, "wb");
00627 
00628                                 if(retcode == 0) {
00629                                         
00630                                         memset(enc_fullpath, 0 , MAX_PATH_LENGTH);
00631                                         strcpy(enc_fullpath, fullpath);
00632                                         strcat(enc_fullpath, GZ_SUFFIX);
00633 #ifdef _MSC_VER
00634                                         if(_stat64(enc_fullpath, &enc_file_stats) == -1) {
00635 #else
00636                                         if(stat64(enc_fullpath, &enc_file_stats) == -1) {
00637 #endif
00638                                                 printf("Error: %s is not valid file name\n", enc_fullpath);
00639                                                 fflush(stdout);
00640                                                 free(hostname);
00641                                                 free_uri(uri);
00642                                                 return -1;
00643                                         }
00644                                         
00645                                         fprintf(fp, "\n\t\t");
00646                                         fprintf(fp, "Content-Encoding=\"%s\"", "gzip");
00647                                 
00648 #ifdef USE_OPENSSL
00649                                         if(s->calculate_session_size == FALSE) {
00650                                          md5 = file_md5(enc_fullpath);
00651 
00652                                          if(md5 == NULL) {
00653                                                 free(hostname);
00654                                                 free_uri(uri);
00655                                                 return -1;
00656                                          }
00657 
00658                                          fprintf(fp, "\n\t\t");
00659                                          fprintf(fp, "Content-MD5=\"%s\"", md5);
00660                                     }
00661 #endif
00662 
00663                                         fprintf(fp, "\n\t\t");
00664                                         fprintf(fp, "Transfer-Length=\"%llu\"", enc_file_stats.st_size);
00665                                 }
00666                         }
00667 #endif
00668                         else {
00669 #ifdef USE_OPENSSL
00670                                 if(s->calculate_session_size == FALSE) {
00671                                  md5 = file_md5(fullpath);
00672 
00673                                  if(md5 == NULL) {
00674                                         free(hostname);
00675                                         free_uri(uri);
00676                                         return -1;
00677                                  }
00678 
00679                                  fprintf(fp, "\n\t\t");
00680                                  fprintf(fp, "Content-MD5=\"%s\"", md5);
00681                                 }
00682 #endif
00683                         }
00684 
00685 #ifdef FDT_INST_FEC_OTI_FILE
00686                         if(!s->use_fec_oti_ext_hdr) {
00687 
00688                                 fprintf(fp, "\n\t\t");
00689                                 fprintf(fp, "FEC-OTI-FEC-Encoding-ID=\"%u\"", s->def_fec_enc_id);
00690 
00691                                 if(s->def_fec_enc_id >= 128) {
00692                                         fprintf(fp, "\n\t\t");
00693                                         fprintf(fp, "FEC-OTI-FEC-Instance-ID=\"%u\"", s->def_fec_inst_id);
00694                                 }
00695 
00696                                 if(s->def_fec_enc_id == RS_FEC_ENC_ID) {
00697                                         fprintf(fp, "\n\t");
00698                                         fprintf(fp, "FEC-OTI-Finite-Field-Parameter=\"%u\"", GF_BITS);
00699                                         fprintf(fp, "\n\t");
00700                                         fprintf(fp, "FEC-OTI-Number-of-Encoding-Symbols-per-Group=\"%u\"", 1);
00701                                 }
00702 
00703                                 fprintf(fp, "\n\t\t");
00704                                 fprintf(fp, "FEC-OTI-Maximum-Source-Block-Length=\"%u\"", s->def_max_sblen);
00705                                 fprintf(fp, "\n\t\t");
00706                                 fprintf(fp, "FEC-OTI-Encoding-Symbol-Length=\"%u\"", s->def_eslen);
00707 
00708                                 if(s->def_fec_enc_id == RS_FEC_ENC_ID || s->def_fec_enc_id == SB_SYS_FEC_ENC_ID) {
00709                                         fprintf(fp, "\n\t\t");
00710                                         fprintf(fp, "FEC-OTI-Max-Number-of-Encoding-Symbols=\"%u\"", max_n);    
00711                                 }
00712                         }
00713 #endif
00714 
00715                         fprintf(fp, "/>\n");
00716                         
00717                         toi++;
00718                         free_uri(uri);
00719 
00720 #ifdef USE_OPENSSL
00721                         free(md5);
00722                         md5 = NULL;
00723 #endif
00724                 }
00725         }
00726 
00727         while(FindNextFile(dirptr, &entry)) {
00728 
00729                 if(checkpath(entry.cFileName)) {
00730                         continue;
00731                 }
00732 
00733                 memset(fullname, 0 , MAX_PATH_LENGTH);
00734                 strcpy(fullname, directory);
00735 
00736                 if(fullname[strlen(fullname) - 1] != '\\') {
00737                         strcat(fullname, "\\");
00738                 }
00739 
00740                 strcat(fullname, entry.cFileName);
00741 
00742                 for(i = 0; i < (int)strlen(fullname); i++) {
00743                         if(fullname[i] == '\\') {
00744                                 fullname[i] = '/';
00745                         }
00746                 }
00747 
00748                 if(entry.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
00749                         result = encode_directory(fullname, base_dir, fp, s_id);
00750 
00751                         if(result < 0) {
00752                                 continue;
00753                         }
00754                 }
00755                 else {
00756         
00757                         memset(fullpath, 0, MAX_PATH_LENGTH);
00758 
00759                         if(!(strcmp(base_dir, "") == 0)) {
00760                                 strcpy(fullpath, base_dir);
00761                                 strcat(fullpath, "\\");
00762                         }
00763 
00764                         strcat(fullpath, fullname);
00765 
00766 #ifdef _MSC_VER
00767                         if(_stat64(fullpath, &file_stats) == -1) {
00768 #else
00769                         if(stat64(fullpath, &file_stats) == -1) {
00770 #endif
00771                                 printf("Error: %s is not valid file name\n", fullpath);
00772                                 fflush(stdout);
00773                                 continue;
00774                         }
00775 
00776                         if(file_stats.st_size == 0) {
00777                                 printf("Error: file %s size = 0\n", fullpath);
00778                                 fflush(stdout);
00779                                 continue;
00780                         }
00781 
00782                         uri = alloc_uri_struct();
00783 
00784                         set_uri_scheme(uri, "file");
00785 
00786                         if(user != NULL) {
00787                                 set_uri_user(uri, user);
00788                         }
00789 
00790 #ifdef HOSTNAME_TO_FDT
00791                         if(hostname != NULL) {
00792                                 set_uri_host(uri, hostname);
00793                         }
00794 #endif
00795 
00796                         set_uri_path(uri, fullname);
00797 
00798             fprintf(fp, "\t<File TOI=\"%I64u\"", toi);
00799 
00800                         uri_str = uri_string(uri);
00801 
00802                         fprintf(fp, "\n\t\t");
00803                         fprintf(fp, "Content-Location=\"%s\"", uri_str);
00804 
00805                         free(uri_str);
00806                         uri_str = NULL;
00807 
00808                         fprintf(fp, "\n\t\t");
00809                         fprintf(fp, "Content-Length=\"%llu\"", file_stats.st_size);
00810 
00811                         if(s->encode_content == PAD_FILES) {
00812 
00813                                 if(compute_padding_length(file_stats.st_size, s->def_max_sblen, s->def_eslen)) {           
00814                                         fprintf(fp, "\n\t\t");
00815                                         fprintf(fp, "Content-Encoding=\"%s\"", "pad");
00816 
00817                                         fprintf(fp, "\n\t\t");
00818                                         fprintf(fp, "Transfer-Length=\"%llu\"", compute_padding_length(file_stats.st_size, s->def_max_sblen, s->def_eslen) + file_stats.st_size);
00819                                 }
00820                         }
00821 #ifdef USE_ZLIB
00822                         else if(s->encode_content == ZLIB_FDT_AND_GZIP_FILES) {
00823 
00824                                 retcode = file_gzip_compress(fullpath, "wb");
00825                         
00826                                 if(retcode == 0) {
00827 
00828                                         memset(enc_fullpath, 0 , MAX_PATH_LENGTH);
00829                                         strcpy(enc_fullpath, fullpath);
00830                                         strcat(enc_fullpath, GZ_SUFFIX);
00831 
00832 #ifdef _MSC_VER
00833                                         if(_stat64(enc_fullpath, &enc_file_stats) == -1) {
00834 #else
00835                                         if(stat64(enc_fullpath, &enc_file_stats) == -1) {
00836 #endif
00837                                                 printf("Error: %s is not valid file name\n", enc_fullpath);
00838                                                 fflush(stdout);
00839                                                 free_uri(uri);
00840                                                 continue;
00841                                         }
00842                                         
00843                                         fprintf(fp, "\n\t\t");
00844                                         fprintf(fp, "Content-Encoding=\"%s\"", "gzip");
00845 
00846 #ifdef USE_OPENSSL
00847                                         if(s->calculate_session_size == FALSE) {
00848                                          md5 = file_md5(enc_fullpath);
00849 
00850                                          if(md5 == NULL) {
00851                                                 free(hostname);
00852                                                 free_uri(uri);
00853                                                 continue;
00854                                          }
00855 
00856                                          fprintf(fp, "\n\t\t");
00857                                          fprintf(fp, "Content-MD5=\"%s\"", md5);
00858                                         }
00859 #endif
00860 
00861                                         fprintf(fp, "\n\t\t");
00862                                         fprintf(fp, "Transfer-Length=\"%llu\"", enc_file_stats.st_size);
00863                                 }
00864                         }
00865 #endif
00866                         else {
00867 #ifdef USE_OPENSSL
00868                                 if(s->calculate_session_size == FALSE) {
00869                                  md5 = file_md5(fullpath);
00870 
00871                                  if(md5 == NULL) {
00872                                         free(hostname);
00873                                         free_uri(uri);
00874                                         continue;
00875                                  }
00876 
00877                                  fprintf(fp, "\n\t\t");
00878                                  fprintf(fp, "Content-MD5=\"%s\"", md5);
00879                                 }
00880 #endif
00881                         }
00882                         
00883 #ifdef FDT_INST_FEC_OTI_FILE
00884                         if(!s->use_fec_oti_ext_hdr) {
00885 
00886                                 fprintf(fp, "\n\t\t");
00887                                 fprintf(fp, "FEC-OTI-FEC-Encoding-ID=\"%u\"", s->def_fec_enc_id);
00888 
00889                                 if(s->def_fec_enc_id >= 128) {
00890                                         fprintf(fp, "\n\t\t");
00891                                         fprintf(fp, "FEC-OTI-FEC-Instance-ID=\"%u\"", s->def_fec_inst_id);
00892                                 }
00893 
00894                                 if(s->def_fec_enc_id == RS_FEC_ENC_ID) {
00895                                         fprintf(fp, "\n\t");
00896                                         fprintf(fp, "FEC-OTI-Finite-Field-Parameter=\"%u\"", GF_BITS);  
00897                                         fprintf(fp, "\n\t");
00898                                         fprintf(fp, "FEC-OTI-Number-of-Encoding-Symbols-per-Group=\"%u\"", 1);
00899                                 }
00900 
00901                                 fprintf(fp, "\n\t\t");
00902                                 fprintf(fp, "FEC-OTI-Maximum-Source-Block-Length=\"%u\"", s->def_max_sblen);
00903                                 fprintf(fp, "\n\t\t");
00904                                 fprintf(fp, "FEC-OTI-Encoding-Symbol-Length=\"%u\"", s->def_eslen);
00905                                 
00906                                 if(s->def_fec_enc_id == RS_FEC_ENC_ID || s->def_fec_enc_id == SB_SYS_FEC_ENC_ID) {
00907                                         fprintf(fp, "\n\t\t");
00908                                         fprintf(fp, "FEC-OTI-Max-Number-of-Encoding-Symbols=\"%u\"", max_n);    
00909                                 }
00910                         }
00911 #endif
00912 
00913                         fprintf(fp, "/>\n");
00914                         
00915                         toi++;
00916                         free_uri(uri);
00917 
00918 #ifdef USE_OPENSSL
00919                         if(s->calculate_session_size == FALSE) {
00920                           free(md5);
00921                           md5 = NULL;
00922                         }
00923 #endif
00924                 }
00925         }
00926         FindClose(dirptr);
00927 
00928 #else
00929         user = getenv("USER");  
00930 
00931         memset(findfile, 0, MAX_PATH_LENGTH);
00932 
00933         if(!(strcmp(base_dir, "") == 0)) {
00934                 strcpy(findfile, base_dir);
00935                 strcat(findfile, "/");
00936         }
00937 
00938         strcat(findfile, directory);
00939 
00940         dirptr = opendir(findfile);
00941 
00942         if(dirptr == NULL) {
00943                 printf("%s is not valid directory name\n", findfile);
00944                 fflush(stdout);
00945                 free(hostname);
00946                 return -1;
00947         }
00948 
00949         entry = readdir(dirptr);
00950 
00951         while(entry != NULL) {
00952                 
00953                 if(checkpath(entry->d_name)) {
00954                         entry = readdir(dirptr);
00955                         continue;
00956                 }
00957 
00958                 memset(fullname, 0 , MAX_PATH_LENGTH);
00959                 strcpy(fullname, directory);
00960                 
00961                 if(fullname[strlen(fullname) - 1] != '/') {
00962                         strcat(fullname, "/");
00963                 }
00964 
00965                 strcat(fullname, entry->d_name);
00966 
00967                 memset(fullpath, 0, MAX_PATH_LENGTH);
00968 
00969                 if(!(strcmp(base_dir, "") == 0)) {
00970                         strcpy(fullpath, base_dir);
00971                         strcat(fullpath, "/");
00972                 }
00973 
00974                 strcat(fullpath, fullname);
00975 
00976                 if(stat64(fullpath, &file_stats) == -1) {
00977                         printf("Error: %s is not valid file name\n", fullpath);
00978                         fflush(stdout);
00979                         entry = readdir(dirptr);
00980                         continue;
00981                 }
00982 
00983                 if(S_ISDIR(file_stats.st_mode)) {
00984                         result = encode_directory(fullname, base_dir, fp, s_id);
00985 
00986                         if(result < 0) {
00987                                 entry = readdir(dirptr);
00988                                 continue;
00989                         }
00990                 }
00991                 else {  
00992                         if(file_stats.st_size == 0) {
00993                                 printf("Error: file %s size = 0\n", fullpath);
00994                                 fflush(stdout);
00995                                 entry = readdir(dirptr);
00996                                 continue;
00997                         }
00998 
00999                         uri = alloc_uri_struct();
01000 
01001                         set_uri_scheme(uri, "file");
01002 
01003                         if(user != NULL) {
01004                                 set_uri_user(uri, user);
01005                         }
01006                         
01007 #ifdef HOSTNAME_TO_FDT
01008                         if(hostname != NULL) {
01009                                 set_uri_host(uri, hostname);
01010                         }
01011 #endif
01012 
01013                         set_uri_path(uri, fullname);
01014 
01015                         fprintf(fp, "\t<File TOI=\"%llu\"", toi);
01016 
01017                         uri_str = uri_string(uri);
01018 
01019                         fprintf(fp, "\n\t\t");
01020                         fprintf(fp, "Content-Location=\"%s\"", uri_str);
01021 
01022                         free(uri_str);
01023                         uri_str = NULL;
01024 
01025                         fprintf(fp, "\n\t\t");
01026                         fprintf(fp, "Content-Length=\"%llu\"", file_stats.st_size);
01027 
01028                         if(s->encode_content == PAD_FILES) {
01029 
01030                                 if(compute_padding_length(file_stats.st_size, s->def_max_sblen, s->def_eslen)) {           
01031                                         fprintf(fp, "\n\t\t");
01032                                         fprintf(fp, "Content-Encoding=\"%s\"", "pad");
01033 
01034                                         fprintf(fp, "\n\t\t");
01035                                         fprintf(fp, "Transfer-Length=\"%llu\"", compute_padding_length(file_stats.st_size, s->def_max_sblen, s->def_eslen) + file_stats.st_size);
01036                                 }
01037                         }
01038 #ifdef USE_ZLIB
01039                         else if(s->encode_content == ZLIB_FDT_AND_GZIP_FILES) {
01040                                                                                                                                               
01041                                 retcode = file_gzip_compress(fullpath, "wb");
01042                                                                                                                                           
01043                                 if(retcode == 0) {
01044                                         
01045                                         memset(enc_fullpath, 0 , MAX_PATH_LENGTH);
01046                                         strcpy(enc_fullpath, fullpath);
01047                                         strcat(enc_fullpath, GZ_SUFFIX);
01048                                                                                                                                       
01049                                         if(stat64(enc_fullpath, &enc_file_stats) == -1) {
01050                                                 printf("Error: %s is not valid file name\n", enc_fullpath);
01051                                                 fflush(stdout);
01052                                                 entry = readdir(dirptr);
01053                                                 free_uri(uri);
01054                                                 continue;
01055                                         }
01056 
01057                                         fprintf(fp, "\n\t\t");
01058                                         fprintf(fp, "Content-Encoding=\"%s\"", "gzip");                                 
01059 #ifdef USE_OPENSSL
01060                                         if(s->calculate_session_size == FALSE) {
01061                                          md5 = file_md5(enc_fullpath);
01062 
01063                                          if(md5 == NULL) {
01064                                                 free_uri(uri);
01065                                                 continue;
01066                                          }
01067                                         
01068                                          fprintf(fp, "\n\t\t");
01069                                          fprintf(fp, "Content-MD5=\"%s\"", md5);
01070                                         }
01071 #endif
01072                 
01073                                         fprintf(fp, "\n\t\t");
01074                                         fprintf(fp, "Transfer-Length=\"%llu\"", enc_file_stats.st_size);
01075                                 }
01076                         }
01077 #endif
01078                         else {
01079 
01080 #ifdef USE_OPENSSL
01081                                 if(s->calculate_session_size == FALSE) {
01082                                  md5 = file_md5(fullpath);
01083 
01084                                  if(md5 == NULL) {
01085                                         free_uri(uri);
01086                                         continue;
01087                                  }
01088 
01089                                  fprintf(fp, "\n\t\t");
01090                                  fprintf(fp, "Content-MD5=\"%s\"", md5);
01091                                 }
01092 #endif
01093                         }
01094                         
01095 #ifdef FDT_INST_FEC_OTI_FILE
01096                         if(!s->use_fec_oti_ext_hdr) {
01097 
01098                                 fprintf(fp, "\n\t\t");
01099                                 fprintf(fp, "FEC-OTI-FEC-Encoding-ID=\"%u\"", s->def_fec_enc_id);
01100 
01101                                 if(s->def_fec_enc_id >= 128) {
01102                                         fprintf(fp, "\n\t\t");
01103                                         fprintf(fp, "FEC-OTI-FEC-Instance-ID=\"%u\"", s->def_fec_inst_id);
01104                                 }
01105 
01106                                 if(s->def_fec_enc_id == RS_FEC_ENC_ID) {
01107                                         fprintf(fp, "\n\t");
01108                                         fprintf(fp, "FEC-OTI-Finite-Field-Parameter=\"%u\"", GF_BITS);  
01109                                         fprintf(fp, "\n\t");
01110                                         fprintf(fp, "FEC-OTI-Number-of-Encoding-Symbols-per-Group=\"%u\"", 1);
01111                                 }
01112 
01113                                 fprintf(fp, "\n\t\t");
01114                                 fprintf(fp, "FEC-OTI-Maximum-Source-Block-Length=\"%u\"", s->def_max_sblen);
01115                                 fprintf(fp, "\n\t\t");
01116                                 fprintf(fp, "FEC-OTI-Encoding-Symbol-Length=\"%u\"", s->def_eslen);
01117 
01118                                 if(s->def_fec_enc_id == RS_FEC_ENC_ID || s->def_fec_enc_id == SB_SYS_FEC_ENC_ID) {
01119                                         fprintf(fp, "\n\t\t");
01120                                         fprintf(fp, "FEC-OTI-Max-Number-of-Encoding-Symbols=\"%u\"", max_n);    
01121                                 }
01122                         }
01123 #endif
01124 
01125                         fprintf(fp, "/>\n");
01126                         
01127                         toi++;
01128                         free_uri(uri);
01129 
01130 #ifdef USE_OPENSSL
01131                         if(s->calculate_session_size == FALSE) {
01132                          free(md5);
01133                          md5 = NULL;
01134                         }
01135 #endif
01136                 }
01137                 entry = readdir(dirptr);
01138         }
01139         closedir(dirptr);
01140 
01141 #endif
01142         
01143         if(hostname != NULL) {
01144                 free(hostname);
01145         }
01146 
01147         if(toi == 1) {
01148                 return -1;
01149         }
01150 
01151         return 0;
01152 }
01153 
01154 int generate_fdt(char *file_token, char *base_dir, int *s_id, char *fdt_file_name,
01155                                  int complete_fdt, int verbosity) {
01156   int result;
01157   FILE *fp;
01158   
01159 #ifdef _MSC_VER
01160   struct __stat64 file_stats;
01161 #else
01162   struct stat64 file_stats;
01163 #endif
01164   alc_session_t *s = NULL;
01165   char fullpath[MAX_PATH_LENGTH];
01166   
01167 #ifdef FDT_INST_FEC_OTI_COMMON
01168   div_t div_max_n;
01169   int max_n;
01170 #endif
01171 
01172   char *token;
01173   
01174   if((fp = fopen(fdt_file_name, "wb")) == NULL) {
01175     printf("Error: unable to create fdtfile %s\n", fdt_file_name);
01176     fflush(stdout);
01177     return -1;
01178   }
01179   
01180   s = get_alc_session(*s_id);
01181   
01182 #ifdef FDT_INST_FEC_OTI_COMMON
01183   div_max_n = div((s->def_max_sblen * (100 + s->def_fec_ratio)), 100);
01184   max_n = div_max_n.quot;
01185 #endif
01186   
01187   toi = 1;
01188   
01189   fprintf(fp, "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n");
01190   fprintf(fp, "<FDT-Instance ");
01191   
01192 #ifdef _MSC_VER
01193   if(s->stoptime == 0) {
01194     /* session is not bounded, set 64bits max value */
01195     fprintf(fp, "Expires=\"%I64u\"", (unsigned long long)0xFFFFFFFFFFFFFFFF);
01196   }
01197   else {
01198     fprintf(fp, "Expires=\"%I64u\"", s->stoptime);
01199   }
01200 #else
01201   if(s->stoptime == 0) {
01202     /* session is not bounded, set 64bits max value */
01203     fprintf(fp, "Expires=\"%llu\"", 0xFFFFFFFFFFFFFFFFULL);
01204   }
01205   else {
01206     fprintf(fp, "Expires=\"%llu\"", s->stoptime);
01207   }
01208 #endif
01209   
01210   if(complete_fdt > 0) {
01211     fprintf(fp, "\n\t");
01212     fprintf(fp, "Complete=\"true\"");
01213   }
01214   
01215 #ifdef FDT_INST_FEC_OTI_COMMON
01216   if(!s->use_fec_oti_ext_hdr) {
01217     
01218     fprintf(fp, "\n\t");
01219     fprintf(fp, "FEC-OTI-FEC-Encoding-ID=\"%u\"", s->def_fec_enc_id);
01220     
01221     if(s->def_fec_enc_id >= 128) {
01222       fprintf(fp, "\n\t");
01223       fprintf(fp, "FEC-OTI-FEC-Instance-ID=\"%u\"", s->def_fec_inst_id);
01224     }
01225     
01226     if(s->def_fec_enc_id == RS_FEC_ENC_ID) {
01227       fprintf(fp, "\n\t");
01228       fprintf(fp, "FEC-OTI-Finite-Field-Parameter=\"%u\"", GF_BITS);
01229       fprintf(fp, "\n\t");
01230       fprintf(fp, "FEC-OTI-Number-of-Encoding-Symbols-per-Group=\"%u\"", 1);
01231     }
01232     
01233     fprintf(fp, "\n\t");
01234     fprintf(fp, "FEC-OTI-Maximum-Source-Block-Length=\"%u\"", s->def_max_sblen);
01235     fprintf(fp, "\n\t");
01236     fprintf(fp, "FEC-OTI-Encoding-Symbol-Length=\"%u\"", s->def_eslen);
01237     
01238     if(s->def_fec_enc_id == RS_FEC_ENC_ID || s->def_fec_enc_id == SB_SYS_FEC_ENC_ID) {
01239       fprintf(fp, "\n\t");
01240       fprintf(fp, "FEC-OTI-Max-Number-of-Encoding-Symbols=\"%u\"", max_n);
01241     }
01242   }
01243 #endif
01244   
01245   fprintf(fp, ">\n");
01246 
01247   token = strtok(file_token, ",");
01248 
01249   while(token != NULL) {
01250 
01251     memset(fullpath, 0, (MAX_PATH_LENGTH));
01252     
01253     if(!(strcmp(base_dir, "") == 0)) {
01254       strcpy(fullpath, base_dir);
01255       
01256 #ifdef _MSC_VER
01257       strcat(fullpath, "\\");
01258 #else
01259       strcat(fullpath, "/");
01260 #endif
01261     }
01262     
01263     strcat(fullpath, token);
01264 #ifdef _MSC_VER
01265     if(_stat64(fullpath, &file_stats) == -1) {
01266 #else
01267     if(stat64(fullpath, &file_stats) == -1) {
01268 #endif
01269       printf("Error: %s is not valid file name\n", fullpath);
01270       fflush(stdout);
01271       return -1;
01272     }
01273 
01274     if(file_stats.st_mode & S_IFDIR) {
01275       result = encode_directory(token, base_dir, fp, s_id);
01276     }
01277     else {
01278       result = encode_file(token, base_dir, fp, s_id);
01279     }
01280 
01281     if(result < 0) {
01282       fclose(fp);
01283       remove(fdt_file_name);
01284       return -1;
01285     }
01286 
01287     token = strtok(NULL, ",");
01288   }
01289     
01290   fprintf(fp, "</FDT-Instance>\n");
01291     
01292   if(verbosity == 4) {
01293     printf("File: %s created\n", fdt_file_name);
01294   }
01295   
01296   fclose(fp);
01297   
01298   return 0;
01299 }
01300 
01301 

Generated on Fri Mar 9 19:59:53 2007 for MAD-FCL by  doxygen 1.5.0