Example #1
0
int
main (int argc, char **argv)
{
	if (argc != 2)
	{	puts ("\nEncode a single input file into a number of different output ") ;
		puts ("encodings. These output encodings can then be moved to another ") ;
		puts ("OS for testing.\n") ;
		puts ("    Usage : generate <filename>\n") ;
		exit (1) ;
		} ;

	/* A couple of standard WAV files. Make sure Win32 plays these. */
	encode_file (argv [1], "pcmu8.wav"	, SF_FORMAT_WAV | SF_FORMAT_PCM_U8) ;
	encode_file (argv [1], "pcm16.wav"	, SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
	encode_file (argv [1], "imaadpcm.wav", SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM) ;
	encode_file (argv [1], "msadpcm.wav", SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM) ;
	encode_file (argv [1], "gsm610.wav"	, SF_FORMAT_WAV | SF_FORMAT_GSM610) ;

	/* Soundforge W64. */
	encode_file (argv [1], "pcmu8.w64"	, SF_FORMAT_W64 | SF_FORMAT_PCM_U8) ;
	encode_file (argv [1], "pcm16.w64"	, SF_FORMAT_W64 | SF_FORMAT_PCM_16) ;
	encode_file (argv [1], "imaadpcm.w64", SF_FORMAT_W64 | SF_FORMAT_MS_ADPCM) ;
	encode_file (argv [1], "msadpcm.w64", SF_FORMAT_W64 | SF_FORMAT_IMA_ADPCM) ;
	encode_file (argv [1], "gsm610.w64"	, SF_FORMAT_W64 | SF_FORMAT_GSM610) ;

	return 0 ;
} /* main */
Example #2
0
void parseOpt(int argc , char * const * argv)
{
    int result;
    std::string in_file, out_file,action;
    opterr = 0; //使getopt不行stderr输出错误信息
    while( (result = getopt(argc, argv, "edi:o:")) != -1 )
    {
        switch(result)
        {
            case 'e':
                action = (char *)"encode";
                break;
            case 'd':
                action = (char *)"decode";
                break;
            case 'i':
                in_file = optarg;
                break;
            case 'o':
                out_file = optarg;
                break;
            default:
                printf("Usage:\n    ./wbxml -d -i input -o output\n");
                break;
        }
    }
    if (action == "encode"){
        encode_file(in_file,out_file);
    }else if (action == "decode") {
        decode_file(in_file,out_file);
    }
}
Example #3
0
bool EncRbSpeex::encode(QString input,QString output)
{
    qDebug() << "encoding " << input << " to "<< output;
    char errstr[512];

    FILE *fin,*fout;
    if ((fin = fopen(input.toLocal8Bit(), "rb")) == NULL) {
        qDebug() << "Error: could not open input file\n";
        return false;
    }
    if ((fout = fopen(output.toLocal8Bit(), "wb")) == NULL) {
        qDebug() << "Error: could not open output file\n";
        fclose(fin);
        return false;
    }

    int ret = encode_file(fin, fout, quality, complexity, narrowband, volume,
                      errstr, sizeof(errstr));
    fclose(fout);
    fclose(fin);

    if (!ret) {
        /* Attempt to delete unfinished output */
        qDebug() << "Error:" << errstr;
        QFile(output).remove();
        return false;
    }
    return true;
}
Example #4
0
int main(int argc, char *argv[])
{
	char *NameFunc = argv[1];
	char *InFile = argv[2];
	char *OutFile = argv[3];
	int ptr1, ptr2;
	
	ptr1 = strncmp("encode", NameFunc, 10);
	ptr2 = strncmp("decode", NameFunc, 10);

	if ((argc != 4) || (((ptr1 == -1) || (ptr1 == 1)) & ((ptr2 == -1) || (ptr2 == 1)))) {
		printf("Usage:\n");
		printf("main encode <in-file-name> <out-file-name>\n");
		printf("main decode <in-file-name> <out-file-name>\n");
		return 0;
	}
	else if (ptr1 == 0) {
		printf("encode\n");
		encode_file(InFile, OutFile);	
	}
	else if (ptr2 == 0) {
		printf("decode\n");
		decode_file(InFile, OutFile);
	}
	return 0;
}
Example #5
0
int main(int argc, char **argv) {
	FILE *fin, *fout;

	if (argc != 3) {
		fprintf(stderr, "Usage: %s file_in file_out\n", argv[0]);
		exit(1);
	}

	if ((fin = fopen(argv[1], "r")) == NULL) {
		fprintf(stderr, "Unable to open %s\n", argv[1]);
		exit(1);
	}

	if ((fout = fopen(argv[2], "w+")) == NULL) {
		fprintf(stderr, "Unable to open %s\n", argv[2]);
		exit(1);
	}

	encode_file(fin, fout);

	fclose(fin);
	fclose(fout);

	return 0;
}
Example #6
0
int main (int argc, char **argv)
{
  if (argc != 2)
    {
      printf("Usage : ./encode human_voice.wav\n");
      return (0);
    }
  encode_file(argv [1], "human_voice.flac", SF_FORMAT_FLAC | SF_FORMAT_PCM_16);
  return 0 ;
} /* main */
Example #7
0
int encode_steg(char *file_cont, char *file_data,
				char *file_output, unsigned char *key)
{
	FILE *fpc, *fpd, *fpo;
	unsigned char digest[16];
	unsigned long max_hide=0;
	//clock_t c1, c2;

	// Open files
	if((fpc = fopen(file_cont, "rb")) == NULL) {
		fprintf(stderr, "Cannot open container file!\n");
		return(-1);
	}
	if((fpd = fopen(file_data, "rb")) == NULL) {
		fprintf(stderr, "Cannot open data file!\n");
		return(-1);
	}
	if((fpo = fopen(file_output, "wb")) == NULL) {
		fprintf(stderr, "Cannot open output file!\n");
		return(-1);
	}

	// Make sure the data will fit in the container
	max_hide = get_max_hide(get_filesize(fpc)-STARTBYTE-(sizeof(struct stash_hdr)*8));
	if(max_hide < get_filesize(fpd)) {
		fprintf(stderr, "Error! Data file cannot fit in %s; %d bytes max!\n",
			file_cont, (int)max_hide);
		return(-1);
	}

	// Get MD5 Checksum of key
	MD5String(key, digest);

	// Seed the PRNG with the md5 checksum of the key
	prng_seed(digest);

	// Encode
	//c1 = clock();
	encode_file(fpc, fpd, fpo);
	//c2 = clock();
	//printf("%f\n", get_speed(c1,c2));
	//system("PAUSE");

	// Close files
	fclose(fpc);
	fclose(fpd);
	fclose(fpo);

	return(0);
}
Example #8
0
static int set_value(const char* path, const char* var, const char* val) {
	epai_file_t* efp;
	epai_error_t err = decode_file(path, &efp);
	if (err) {
		fprintf(stderr, "Failed to decode file. Error %d.\n", err);
		return 2;
	}

	err = encode_file(path, efp);
	if (err) {
		fprintf(stderr, "Failed to encode file. Error %d.\n", err);
		return 4;
	}

	return 0;
}
Example #9
0
void file_mode(char *filename,file_mode_t type)
{
    Enigma enigma;

    init_enigma(&enigma);

    if(type == ENCODE)
    {
        printf("Encoding\n");
        encode_file(&enigma,filename);
    }
    else if(type == DECODE)
    {
        printf("Decoding\n");
        decode_file(&enigma,filename);
    }
}
Example #10
0
int main(int argc, char *argv[])
{
	int nativeBlockNum = 4;
	int parityBlockNum = 2;
	char *inFile = NULL;
	char *confFile = NULL;
	char *outFile = NULL;

	enum func
	{
		encode,
		decode
	};
	enum func op;

	nativeBlockNum = atoi(argv[1]);
	parityBlockNum = atoi(argv[2]);

	if( strcmp(argv[3], "-e") == 0 )
	{
		op = encode;
	}
	else if( strcmp(argv[3], "-d") == 0 )
	{
		op = decode;
	}
	else
	{
		printf("Invalid option!\n");
		exit(-1);
	}
/*	
	if(op == encode)
	{
		file = argv[4];
		encode_file(file, nativeBlockNum, parityBlockNum);
	}

	if(op == decode)
	{
		file = argv[4];
		decode_file(file, nativeBlockNum, parityBlockNum);
	}
*/

	switch(op)
	{
		case encode:
			inFile = argv[4];
			encode_file(inFile, nativeBlockNum, parityBlockNum);
			break;

		case decode:
			/*
			if(argc > 4)
			{
				file = argv[4];
				decode_file(file, nativeBlockNum, parityBlockNum);
			}
			else
			{
				decode_file(NULL, nativeBlockNum, parityBlockNum);
			}
			*/
			if(argc == 5)
			{
				confFile = argv[4];
			}
			else if(argc == 7 && strcmp(argv[5], "-o") == 0)
			{
				confFile = argv[4];
				outFile = argv[6];
			}
			else
			{
				printf("Invalid command!\n");
				exit(-1);
			}
			decode_file(confFile, outFile, nativeBlockNum, parityBlockNum);
			break;
	}

	return 0;

}
Example #11
0
int main (int ac, char **av)
{
    char *cmd = *av;

    char *cname;

    unsigned long delimiters[MAX_DELIMITER];

    char *localmappers[MAX_LOCALMAPPER];

    char *nameprep_version = NULL;

    int ndelimiters = 0;

    int nlocalmappers = 0;

    char *in_code = NULL;

    char *out_code = NULL;

    char *resconf_file = NULL;

    int no_resconf = 0;

    char *encoding_alias = NULL;

    int flags = DEFAULT_FLAGS;

    FILE *fp;

    idn_result_t r;

    idn_resconf_t resconf1, resconf2;

    idn_converter_t conv;

    int exit_value;

#ifdef HAVE_SETLOCALE
    (void) setlocale (LC_ALL, "");
#endif

    /*
     * If the command name begins with 'r', reverse mode is assumed.
     */
    if ((cname = strrchr (cmd, '/')) != NULL)
        cname++;
    else
        cname = cmd;
    if (cname[0] == 'r')
        flags |= FLAG_REVERSE;

    ac--;
    av++;
    while (ac > 0 && **av == '-')
    {

#define OPT_MATCH(opt) (strcmp(*av, opt) == 0)
#define MUST_HAVE_ARG if (ac < 2) print_usage(cmd)
#define APPEND_LIST(array, size, item, what) \
    if (size >= (sizeof(array) / sizeof(array[0]))) { \
        errormsg("too many " what "\n"); \
        exit(1); \
    } \
    array[size++] = item; \
    ac--; av++

        if (OPT_MATCH ("-in") || OPT_MATCH ("-i"))
        {
            MUST_HAVE_ARG;
            in_code = av[1];
            ac--;
            av++;
        }
        else if (OPT_MATCH ("-out") || OPT_MATCH ("-o"))
        {
            MUST_HAVE_ARG;
            out_code = av[1];
            ac--;
            av++;
        }
        else if (OPT_MATCH ("-conf") || OPT_MATCH ("-c"))
        {
            MUST_HAVE_ARG;
            resconf_file = av[1];
            ac--;
            av++;
        }
        else if (OPT_MATCH ("-nameprep") || OPT_MATCH ("-n"))
        {
            MUST_HAVE_ARG;
            nameprep_version = av[1];
            ac--;
            av++;
        }
        else if (OPT_MATCH ("-noconf") || OPT_MATCH ("-C"))
        {
            no_resconf = 1;
        }
        else if (OPT_MATCH ("-reverse") || OPT_MATCH ("-r"))
        {
            flags |= FLAG_REVERSE;
        }
        else if (OPT_MATCH ("-nolocalmap") || OPT_MATCH ("-L"))
        {
            flags &= ~FLAG_LOCALMAP;
        }
        else if (OPT_MATCH ("-nonameprep") || OPT_MATCH ("-N"))
        {
            flags &= ~FLAG_NAMEPREP;
        }
        else if (OPT_MATCH ("-unassigncheck") || OPT_MATCH ("-u"))
        {
            flags |= FLAG_UNASSIGNCHECK;
        }
        else if (OPT_MATCH ("-nounassigncheck") || OPT_MATCH ("-U"))
        {
            flags &= ~FLAG_UNASSIGNCHECK;
        }
        else if (OPT_MATCH ("-nobidicheck") || OPT_MATCH ("-B"))
        {
            flags &= ~FLAG_BIDICHECK;
        }
        else if (OPT_MATCH ("-noasciicheck") || OPT_MATCH ("-A"))
        {
            flags &= ~FLAG_ASCIICHECK;
        }
        else if (OPT_MATCH ("-nolengthcheck"))
        {
            flags &= ~FLAG_LENGTHCHECK;
        }
        else if (OPT_MATCH ("-noroundtripcheck"))
        {
            flags &= ~FLAG_ROUNDTRIPCHECK;
        }
        else if (OPT_MATCH ("-whole") || OPT_MATCH ("-w"))
        {
            flags &= ~FLAG_SELECTIVE;
        }
        else if (OPT_MATCH ("-localmap"))
        {
            MUST_HAVE_ARG;
            APPEND_LIST (localmappers, nlocalmappers, av[1], "local maps");
        }
        else if (OPT_MATCH ("-delimiter"))
        {
            unsigned long v;

            MUST_HAVE_ARG;
            v = get_ucs (av[1]);
            APPEND_LIST (delimiters, ndelimiters, v, "delimiter maps");
        }
        else if (OPT_MATCH ("-alias") || OPT_MATCH ("-a"))
        {
            MUST_HAVE_ARG;
            encoding_alias = av[1];
            ac--;
            av++;
        }
        else if (OPT_MATCH ("-flush"))
        {
            flush_every_line = 1;
        }
        else if (OPT_MATCH ("-version") || OPT_MATCH ("-v"))
        {
            print_version ();
        }
        else
        {
            print_usage (cmd);
        }
#undef OPT_MATCH
#undef MUST_HAVE_ARG
#undef APPEND_LIST

        ac--;
        av++;
    }

    if (ac > 1)
        print_usage (cmd);

    /* Initialize. */
    if ((r = idn_resconf_initialize ()) != idn_success)
    {
        errormsg ("error initializing library\n");
        return (1);
    }

    /*
     * Create resource contexts.
     * `resconf1' and `resconf2' are almost the same but local and
     * IDN encodings are reversed.
     */
    resconf1 = NULL;
    resconf2 = NULL;
    if (idn_resconf_create (&resconf1) != idn_success || idn_resconf_create (&resconf2) != idn_success)
    {
        errormsg ("error initializing configuration contexts\n");
        return (1);
    }

    /* Load configuration file. */
    if (no_resconf)
    {
        set_defaults (resconf1);
        set_defaults (resconf2);
    }
    else
    {
        load_conf_file (resconf1, resconf_file);
        load_conf_file (resconf2, resconf_file);
    }

    /* Set encoding alias file. */
    if (encoding_alias != NULL)
        set_encoding_alias (encoding_alias);

    /* Set input codeset. */
    if (flags & FLAG_REVERSE)
    {
        if (in_code == NULL)
        {
            conv = idn_resconf_getidnconverter (resconf1);
            if (conv == NULL)
            {
                errormsg ("cannot get the IDN encoding.\n" "please specify an appropriate one " "with `-in' option.\n");
                exit (1);
            }
            idn_resconf_setlocalconverter (resconf2, conv);
            idn_converter_destroy (conv);
        }
        else
        {
            set_idncode (resconf1, in_code);
            set_localcode (resconf2, in_code);
        }
    }
    else
    {
        if (in_code == NULL)
        {
            conv = idn_resconf_getlocalconverter (resconf1);
            if (conv == NULL)
            {
                errormsg ("cannot get the local encoding.\n"
                          "please specify an appropriate one " "with `-in' option.\n");
                exit (1);
            }
            idn_resconf_setidnconverter (resconf2, conv);
            idn_converter_destroy (conv);
        }
        else
        {
            set_localcode (resconf1, in_code);
            set_idncode (resconf2, in_code);
        }
    }

    /* Set output codeset. */
    if (flags & FLAG_REVERSE)
    {
        if (out_code == NULL)
        {
            conv = idn_resconf_getlocalconverter (resconf1);
            if (conv == NULL)
            {
                errormsg ("cannot get the local encoding.\n"
                          "please specify an appropriate one " "with `-out' option.\n");
                exit (1);
            }
            idn_resconf_setidnconverter (resconf2, conv);
            idn_converter_destroy (conv);
        }
        else
        {
            set_localcode (resconf1, out_code);
            set_idncode (resconf2, out_code);
        }
    }
    else
    {
        if (out_code == NULL)
        {
            conv = idn_resconf_getidnconverter (resconf1);
            if (conv == NULL)
            {
                errormsg ("cannot get the IDN encoding.\n"
                          "please specify an appropriate one " "with `-out' option.\n");
                exit (1);
            }
            idn_resconf_setlocalconverter (resconf2, conv);
            idn_converter_destroy (conv);
        }
        else
        {
            set_idncode (resconf1, out_code);
            set_localcode (resconf2, out_code);
        }
    }

    /* Set delimiter map(s). */
    if (ndelimiters > 0)
    {
        set_delimitermapper (resconf1, delimiters, ndelimiters);
        set_delimitermapper (resconf2, delimiters, ndelimiters);
    }

    /* Set local map(s). */
    if (nlocalmappers > 0)
    {
        set_localmapper (resconf1, localmappers, nlocalmappers);
        set_localmapper (resconf2, localmappers, nlocalmappers);
    }

    /* Set NAMEPREP version. */
    if (nameprep_version != NULL)
    {
        set_nameprep (resconf1, nameprep_version);
        set_nameprep (resconf2, nameprep_version);
    }

    idn_res_enable (1);

    /* Open input file. */
    if (ac > 0)
    {
        if ((fp = fopen (av[0], "r")) == NULL)
        {
            errormsg ("cannot open file %s: %s\n", av[0], strerror (errno));
            return (1);
        }
    }
    else
    {
        fp = stdin;
    }

    /* Do the conversion. */
    if (flags & FLAG_REVERSE)
        exit_value = decode_file (resconf1, resconf2, fp, flags);
    else
        exit_value = encode_file (resconf1, resconf2, fp, flags);

    idn_resconf_destroy (resconf1);
    idn_resconf_destroy (resconf2);

    return exit_value;
}
Example #12
0
int
main(int argc, char **argv)
{
    char *progName;
    SECStatus rv;
    FILE *inFile, *outFile;
    PLOptState *optstate;
    PLOptStatus status;
    char *suffix = NULL;

    inFile = 0;
    outFile = 0;
    progName = strrchr(argv[0], '/');
    if (!progName)
        progName = strrchr(argv[0], '\\');
    progName = progName ? progName + 1 : argv[0];

    /* Parse command line arguments */
    optstate = PL_CreateOptState(argc, argv, "i:o:w:");
    while ((status = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
        switch (optstate->option) {
            default:
                Usage(progName);
                break;

            case 'i':
                inFile = fopen(optstate->value, "rb");
                if (!inFile) {
                    fprintf(stderr, "%s: unable to open \"%s\" for reading\n",
                            progName, optstate->value);
                    return -1;
                }
                break;

            case 'o':
                outFile = fopen(optstate->value, "wb");
                if (!outFile) {
                    fprintf(stderr, "%s: unable to open \"%s\" for writing\n",
                            progName, optstate->value);
                    return -1;
                }
                break;

            case 'w':
                if (!strcmp(optstate->value, "c"))
                    suffix = strdup("CERTIFICATE");
                else
                    suffix = strdup(optstate->value);
                break;
        }
    }
    if (status == PL_OPT_BAD)
        Usage(progName);
    if (!inFile) {
#if defined(WIN32)
        /* If we're going to read binary data from stdin, we must put stdin
        ** into O_BINARY mode or else incoming \r\n's will become \n's.
        */

        int smrv = _setmode(_fileno(stdin), _O_BINARY);
        if (smrv == -1) {
            fprintf(stderr,
                    "%s: Cannot change stdin to binary mode. Use -i option instead.\n",
                    progName);
            return smrv;
        }
#endif
        inFile = stdin;
    }
    if (!outFile) {
#if defined(WIN32)
        /* We're going to write binary data to stdout. We must put stdout
        ** into O_BINARY mode or else outgoing \r\n's will become \r\r\n's.
        */

        int smrv = _setmode(_fileno(stdout), _O_BINARY);
        if (smrv == -1) {
            fprintf(stderr,
                    "%s: Cannot change stdout to binary mode. Use -o option instead.\n",
                    progName);
            return smrv;
        }
#endif
        outFile = stdout;
    }
    if (suffix) {
        fprintf(outFile, "-----BEGIN %s-----\n", suffix);
    }
    rv = encode_file(outFile, inFile);
    if (rv != SECSuccess) {
        fprintf(stderr, "%s: lossage: error=%d errno=%d\n",
                progName, PORT_GetError(), errno);
        return -1;
    }
    if (suffix) {
        fprintf(outFile, "-----END %s-----\n", suffix);
    }
    return 0;
}
Example #13
0
int main(int argc, const char * argv[])
{
  std::ios_base::sync_with_stdio(false);

  try
  {
    sp::program_options po(argc, argv);

    if( po.contains("help") )
    {
      po.print(argv[0], std::cout);
      return EXIT_SUCCESS;
    }

    if( !po.validate_or_print_error(std::cerr) )
    {
      return EXIT_FAILURE;
    }

    std::string output_file_name = po.get<std::string>("output-file");
    if( sp::file_exists(output_file_name) )
    {
      std::cerr << "Error: output-file " << output_file_name
                << " already exists; refusing to overwrite.\n";
      return EXIT_FAILURE;
    }

    std::ofstream output_file(
      output_file_name,
      std::ios::binary
    );

    if( !output_file.good() )
    {
      std::cerr << "Error: failed creating output-file " << output_file_name
                << "\n";
      return EXIT_FAILURE;
    }

    //
    // DECODE FILE
    //
    if( po.contains("decode-file") )
    {
      std::ifstream decode_file(
        po.get<std::string>("decode-file"),
        std::ios::binary
      );

      if( !decode_file.good() )
      {
        std::cerr << "Error: failed opening decode-file "
                  << po.get<std::string>("decode-file") << "\n";
        return EXIT_FAILURE;
      }

      auto dec_iter = std::istreambuf_iterator<char>(decode_file);
      auto dec_iter_end = std::istreambuf_iterator<char>();

      hm::meta md_decoded = hm::decode_meta_data(dec_iter, dec_iter_end);

      hm::decode(
        md_decoded,
        dec_iter,
        dec_iter_end,
        std::ostreambuf_iterator<char>(output_file)
      );

      decode_file.close();
    }
    //
    // ENCODE FILE
    //
    else if( po.contains("encode-file") )
    {
      std::ifstream encode_file(
        po.get<std::string>("encode-file"),
        std::ios::binary
      );

      if( !encode_file.good() )
      {
        std::cerr << "Error: failed opening encode-file "
                  << po.get<std::string>("encode-file") << "\n";
        return EXIT_FAILURE;
      }

      unsigned int entity_size = po.get_entity_size();

      auto enc_iter = std::istreambuf_iterator<char>(encode_file);
      auto enc_iter_end = std::istreambuf_iterator<char>();
      auto out_iter = std::ostreambuf_iterator<char>(output_file);

      // write dummy data
      hm::meta md;
      hm::encode_meta_data(md, out_iter);

      // Because we cannot select a type based on runtime input, we either have
      // to use a macro or duplicate code.
      switch( entity_size )
      {
        default:
        case 1:
        {
          auto tree = hm::build_huffman_tree<uint8_t>(
            enc_iter,
            enc_iter_end
          );

          encode_file.seekg(0);
          md = hm::encode(enc_iter, enc_iter_end, tree.get(), out_iter);
          break;
        }
        case 2:
        {
          auto tree = hm::build_huffman_tree<uint16_t>(
            enc_iter,
            enc_iter_end
          );

          encode_file.seekg(0);
          md = hm::encode(enc_iter, enc_iter_end, tree.get(), out_iter);
          break;
        }
        case 4:
        {
          auto tree = hm::build_huffman_tree<uint32_t>(
            enc_iter,
            enc_iter_end
          );

          encode_file.seekg(0);
          md = hm::encode(enc_iter, enc_iter_end, tree.get(), out_iter);
          break;
        }
        case 8:
        {
          auto tree = hm::build_huffman_tree<uint64_t>(
            enc_iter,
            enc_iter_end
          );

          encode_file.seekg(0);
          md = hm::encode(enc_iter, enc_iter_end, tree.get(), out_iter);
          break;
        }
      }

      // overwrite dummy with actual meta data
      output_file.seekp(0);
      hm::encode_meta_data(md, out_iter);

      encode_file.close();
    }
    else
    {
      // program option validation failed
      // "should never happen"
      assert(false);
    }

    output_file.close();
    return EXIT_SUCCESS;
  }
  catch(const boost::program_options::validation_error& e)
  {
    std::cerr << "Error " << e.what() << "\n";
    return EXIT_FAILURE;
  }

  // "should never happen"
  assert(false);
  return EXIT_FAILURE;
}
Example #14
0
int encode_file(char *file, char *base_dir, FILE *fp, int *s_id) {

	char fullname[MAX_PATH_LENGTH];
	char fullpath[MAX_PATH_LENGTH];

#ifdef _MSC_VER
	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_LENGTH];

#ifdef _MSC_VER
	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

	unsigned long long padding_length;

#ifdef _MSC_VER
	user = getenv("USERNAME");
#else
	user = getenv("USER");
#endif

	memset(fullpath, 0, MAX_PATH_LENGTH);

	if(!(strcmp(base_dir, "") == 0)) {
		strcpy(fullpath, base_dir);

#ifdef _MSC_VER
		strcat(fullpath, "\\");
#else
		strcat(fullpath, "/");
#endif
	}

	strcat(fullpath, file);

#ifdef _MSC_VER
	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_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 _MSC_VER
	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 == 0 || s->encode_content == ZLIB_FDT) {
		if(is_enough_source_block_numbers(s->def_max_sblen, s->def_eslen, file_stats.st_size,
								   s->def_fec_enc_id, s->def_fec_inst_id) < 0) {
			printf("Maximum source block length %i too small for the file: %s\n", s->def_max_sblen, file);
			fflush(stdout);
			if(hostname != NULL) {
				free(hostname);
			}
			free_uri(uri);
			return -1;
		}
	}

	if(s->encode_content == PAD_FILES) {
		padding_length = compute_padding_length(file_stats.st_size, s->def_max_sblen, s->def_eslen);

        if(padding_length) {
        	fprintf(fp, "\n\t\t");
        	fprintf(fp, "Content-Encoding=\"%s\"", "pad");

        	fprintf(fp, "\n\t\t");
            fprintf(fp, "Transfer-Length=\"%llu\"", (file_stats.st_size + padding_length));
        }

		if(is_enough_source_block_numbers(s->def_max_sblen, s->def_eslen, (file_stats.st_size + padding_length),
								   s->def_fec_enc_id, s->def_fec_inst_id) < 0) {
			printf("Maximum source block length %i too small for the file: %s\n", s->def_max_sblen, file);
			fflush(stdout);
			if(hostname != NULL) {
				free(hostname);
			}
			free_uri(uri);
			return -1;
		}
	}

#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_LENGTH);
                	strcpy(enc_fullpath, fullpath);
                	strcat(enc_fullpath, GZ_SUFFIX);
#ifdef _MSC_VER                                                                                                                                                      
                	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);
						if(hostname != NULL) {
							free(hostname);
						}
						free_uri(uri);
                        return -1;
                }

#ifdef USE_OPENSSL
					if(s->calculate_session_size == FALSE) {
					 md5 = file_md5(enc_fullpath);

					 if(md5 == NULL) {
						if(hostname != 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);

					if(is_enough_source_block_numbers(s->def_max_sblen, s->def_eslen, enc_file_stats.st_size,
								   s->def_fec_enc_id, s->def_fec_inst_id) < 0) {
						printf("Maximum source block length %i too small for the file: %s\n", s->def_max_sblen, file);
						fflush(stdout);
						if(hostname != NULL) {
							free(hostname);
						}
						free_uri(uri);
						return -1;
					}
		}
      	}
#endif
	else {

#ifdef USE_OPENSSL
			if(s->calculate_session_size == FALSE) {
			 md5 = file_md5(fullpath);

			 if(md5 == NULL) {
				if(hostname != 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);
		}

		if(s->def_fec_enc_id == RS_FEC_ENC_ID) {
			fprintf(fp, "\n\t");
			fprintf(fp, "FEC-OTI-Finite-Field-Parameter=\"%u\"", GF_BITS);
			fprintf(fp, "\n\t");
			fprintf(fp, "FEC-OTI-Number-of-Encoding-Symbols-per-Group=\"%u\"", 1);
		}

		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
	if(s->calculate_session_size == FALSE) {
		free(md5);
	}
#endif
	
	if(hostname != NULL) {
		free(hostname);
	}

	return 0;
}

/**
 * This is a private function which parses directory to the FDT.
 *
 * @param directory directory to be parsed
 * @param base_dir base directory for file to be parsed to the FDT
 * @param fp file pointer to the generated FDT
 * @param s_id session identifier
 *
 * @return 0 in success, -1 otherwise
 *
 */

int encode_directory(char *directory, char *base_dir, FILE *fp, int *s_id) {

	int result;
	char fullname[MAX_PATH_LENGTH];
	char fullpath[MAX_PATH_LENGTH];

#ifdef _MSC_VER
	struct __stat64 file_stats;
#else
	struct stat64 file_stats;
#endif

	uri_t *uri = NULL;
	char *hostname = NULL;
	alc_session_t *s = NULL;
	char *user = NULL;

#ifdef USE_ZLIB
	int retcode;
	char enc_fullpath[MAX_PATH_LENGTH];
#ifdef _MSC_VER
	struct __stat64 enc_file_stats;
#else
	struct stat64 enc_file_stats;
#endif
#endif

#ifdef USE_OPENSSL
	char *md5 = NULL;
#endif

#ifdef _MSC_VER
	int i;
	char findfile[MAX_PATH_LENGTH + 3];
	HANDLE dirptr = NULL;
	WIN32_FIND_DATA entry;
#else
	struct dirent *entry;
	DIR *dirptr = NULL;
	char findfile[MAX_PATH_LENGTH];
#endif

#ifdef FDT_INST_FEC_OTI_FILE
	div_t div_max_n;
	int max_n;
#endif

	char *uri_str = NULL;

	hostname = getdnsname();

	s = get_alc_session(*s_id);

#ifdef FDT_INST_FEC_OTI_FILE
	div_max_n = div((s->def_max_sblen * (100 + s->def_fec_ratio)), 100);
	max_n = div_max_n.quot;
#endif

#ifdef _MSC_VER
	user = getenv("USERNAME");

	memset(findfile, 0, (MAX_PATH_LENGTH + 3));

	if(!(strcmp(base_dir, "") == 0)) {
		strcpy(findfile, base_dir);
		strcat(findfile, "\\");
	}

	strcat(findfile, directory);
	strcat(findfile, "\\*");
	
	dirptr = FindFirstFile(findfile, &entry);

	if(dirptr == INVALID_HANDLE_VALUE) {
		printf("Error: %s is not valid directory name\n", directory);
		fflush(stdout);
		free(hostname);
		return -1;
	}

	if(!checkpath(entry.cFileName)) {

		memset(fullname, 0 , MAX_PATH_LENGTH);
		strcpy(fullname, directory);

		if(fullname[strlen(fullname) - 1] != '\\') {
			strcat(fullname, "\\");
		}

		strcat(fullname, entry.cFileName);

		for(i = 0; i < (int)strlen(fullname); i++) {
			if(fullname[i] == '\\') {
				fullname[i] = '/';
			}
		}

		if(entry.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
			result = encode_directory(fullname, base_dir, fp, s_id);

			if(result < 0) {
				free(hostname);
				return -1;
			}
		}
		else {

			memset(fullpath, 0, MAX_PATH_LENGTH);

			if(!(strcmp(base_dir, "") == 0)) {
				strcpy(fullpath, base_dir);
				strcat(fullpath, "\\");
			}

			strcat(fullpath, fullname);

#ifdef _MSC_VER
			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);
				free(hostname);
				return -1;
			}		

			if(file_stats.st_size == 0) {
				printf("Error: file %s size = 0\n", fullpath);
				fflush(stdout);
				free(hostname);
				return -1;
			}
			
			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=\"%I64u\"", 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);
								}
/* Not possible to use, because padded file is not in the hard disk in the sender side
#ifdef USE_OPENSSL
                                if(s->calculate_session_size == FALSE) {
								 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) {
					
					memset(enc_fullpath, 0 , MAX_PATH_LENGTH);
					strcpy(enc_fullpath, fullpath);
					strcat(enc_fullpath, GZ_SUFFIX);
#ifdef _MSC_VER
					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;
					}
					
					fprintf(fp, "\n\t\t");
					fprintf(fp, "Content-Encoding=\"%s\"", "gzip");
				
#ifdef USE_OPENSSL
					if(s->calculate_session_size == FALSE) {
					 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
				if(s->calculate_session_size == FALSE) {
				 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);
				}

				if(s->def_fec_enc_id == RS_FEC_ENC_ID) {
					fprintf(fp, "\n\t");
					fprintf(fp, "FEC-OTI-Finite-Field-Parameter=\"%u\"", GF_BITS);
					fprintf(fp, "\n\t");
					fprintf(fp, "FEC-OTI-Number-of-Encoding-Symbols-per-Group=\"%u\"", 1);
				}

				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 == RS_FEC_ENC_ID || 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
		}
	}

	while(FindNextFile(dirptr, &entry)) {

		if(checkpath(entry.cFileName)) {
			continue;
		}

		memset(fullname, 0 , MAX_PATH_LENGTH);
		strcpy(fullname, directory);

		if(fullname[strlen(fullname) - 1] != '\\') {
			strcat(fullname, "\\");
		}

		strcat(fullname, entry.cFileName);

		for(i = 0; i < (int)strlen(fullname); i++) {
			if(fullname[i] == '\\') {
				fullname[i] = '/';
			}
		}

		if(entry.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
			result = encode_directory(fullname, base_dir, fp, s_id);

			if(result < 0) {
				continue;
			}
		}
		else {
	
			memset(fullpath, 0, MAX_PATH_LENGTH);

			if(!(strcmp(base_dir, "") == 0)) {
				strcpy(fullpath, base_dir);
				strcat(fullpath, "\\");
			}

			strcat(fullpath, fullname);

#ifdef _MSC_VER
			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);
				continue;
			}

			if(file_stats.st_size == 0) {
				printf("Error: file %s size = 0\n", fullpath);
				fflush(stdout);
				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=\"%I64u\"", 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_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_LENGTH);
					strcpy(enc_fullpath, fullpath);
					strcat(enc_fullpath, GZ_SUFFIX);

#ifdef _MSC_VER
					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
					if(s->calculate_session_size == FALSE) {
					 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
				if(s->calculate_session_size == FALSE) {
				 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);
				}

				if(s->def_fec_enc_id == RS_FEC_ENC_ID) {
					fprintf(fp, "\n\t");
					fprintf(fp, "FEC-OTI-Finite-Field-Parameter=\"%u\"", GF_BITS);	
					fprintf(fp, "\n\t");
					fprintf(fp, "FEC-OTI-Number-of-Encoding-Symbols-per-Group=\"%u\"", 1);
				}

				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 == RS_FEC_ENC_ID || 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
			if(s->calculate_session_size == FALSE) {
			  free(md5);
			  md5 = NULL;
			}
#endif
		}
	}
	FindClose(dirptr);

#else
	user = getenv("USER");	

	memset(findfile, 0, MAX_PATH_LENGTH);

	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_LENGTH);
		strcpy(fullname, directory);
		
		if(fullname[strlen(fullname) - 1] != '/') {
			strcat(fullname, "/");
		}

		strcat(fullname, entry->d_name);

		memset(fullpath, 0, MAX_PATH_LENGTH);

		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_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_LENGTH);
					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
					if(s->calculate_session_size == FALSE) {
					 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
				if(s->calculate_session_size == FALSE) {
				 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);
				}

				if(s->def_fec_enc_id == RS_FEC_ENC_ID) {
					fprintf(fp, "\n\t");
					fprintf(fp, "FEC-OTI-Finite-Field-Parameter=\"%u\"", GF_BITS);	
					fprintf(fp, "\n\t");
					fprintf(fp, "FEC-OTI-Number-of-Encoding-Symbols-per-Group=\"%u\"", 1);
				}

				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 == RS_FEC_ENC_ID || 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
			if(s->calculate_session_size == FALSE) {
			 free(md5);
			 md5 = NULL;
			}
#endif
		}
		entry = readdir(dirptr);
	}
	closedir(dirptr);

#endif
	
	if(hostname != NULL) {
		free(hostname);
	}

	if(toi == 1) {
		return -1;
	}

	return 0;
}

int generate_fdt(char *file_token, char *base_dir, int *s_id, char *fdt_file_name,
				 int complete_fdt, int verbosity) {
  int result;
  FILE *fp;
  
#ifdef _MSC_VER
  struct __stat64 file_stats;
#else
  struct stat64 file_stats;
#endif
  alc_session_t *s = NULL;
  char fullpath[MAX_PATH_LENGTH];
  
#ifdef FDT_INST_FEC_OTI_COMMON
  div_t div_max_n;
  int max_n;
#endif

  char *token;
  
  if((fp = fopen(fdt_file_name, "wb")) == NULL) {
    printf("Error: unable to create fdtfile %s\n", fdt_file_name);
    fflush(stdout);
    return -1;
  }
  
  s = get_alc_session(*s_id);
  
#ifdef FDT_INST_FEC_OTI_COMMON
  div_max_n = div((s->def_max_sblen * (100 + s->def_fec_ratio)), 100);
  max_n = div_max_n.quot;
#endif
  
  toi = 1;
  
  fprintf(fp, "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n");
  fprintf(fp, "<FDT-Instance ");
  
#ifdef _MSC_VER
  if(s->stoptime == 0) {
    /* session is not bounded, set 64bits max value */
    fprintf(fp, "Expires=\"%I64u\"", (unsigned long long)0xFFFFFFFFFFFFFFFF);
  }
  else {
    fprintf(fp, "Expires=\"%I64u\"", s->stoptime);
  }
#else
  if(s->stoptime == 0) {
    /* session is not bounded, set 64bits max value */
    fprintf(fp, "Expires=\"%llu\"", 0xFFFFFFFFFFFFFFFFULL);
  }
  else {
    fprintf(fp, "Expires=\"%llu\"", s->stoptime);
  }
#endif
  
  if(complete_fdt > 0) {
    fprintf(fp, "\n\t");
    fprintf(fp, "Complete=\"true\"");
  }
  
#ifdef FDT_INST_FEC_OTI_COMMON
  if(!s->use_fec_oti_ext_hdr) {
    
    fprintf(fp, "\n\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");
      fprintf(fp, "FEC-OTI-FEC-Instance-ID=\"%u\"", s->def_fec_inst_id);
    }
    
    if(s->def_fec_enc_id == RS_FEC_ENC_ID) {
      fprintf(fp, "\n\t");
      fprintf(fp, "FEC-OTI-Finite-Field-Parameter=\"%u\"", GF_BITS);
      fprintf(fp, "\n\t");
      fprintf(fp, "FEC-OTI-Number-of-Encoding-Symbols-per-Group=\"%u\"", 1);
    }
    
    fprintf(fp, "\n\t");
    fprintf(fp, "FEC-OTI-Maximum-Source-Block-Length=\"%u\"", s->def_max_sblen);
    fprintf(fp, "\n\t");
    fprintf(fp, "FEC-OTI-Encoding-Symbol-Length=\"%u\"", s->def_eslen);
    
    if(s->def_fec_enc_id == RS_FEC_ENC_ID || s->def_fec_enc_id == SB_SYS_FEC_ENC_ID) {
      fprintf(fp, "\n\t");
      fprintf(fp, "FEC-OTI-Max-Number-of-Encoding-Symbols=\"%u\"", max_n);
    }
  }
#endif
  
  fprintf(fp, ">\n");

  token = strtok(file_token, ",");

  while(token != NULL) {

    memset(fullpath, 0, (MAX_PATH_LENGTH));
    
    if(!(strcmp(base_dir, "") == 0)) {
      strcpy(fullpath, base_dir);
      
#ifdef _MSC_VER
      strcat(fullpath, "\\");
#else
      strcat(fullpath, "/");
#endif
    }
    
    strcat(fullpath, token);
#ifdef _MSC_VER
    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_mode & S_IFDIR) {
      result = encode_directory(token, base_dir, fp, s_id);
    }
    else {
      result = encode_file(token, base_dir, fp, s_id);
    }

    if(result < 0) {
      fclose(fp);
      remove(fdt_file_name);
      return -1;
    }

    token = strtok(NULL, ",");
  }
    
  fprintf(fp, "</FDT-Instance>\n");
    
  if(verbosity == 4) {
    printf("File: %s created\n", fdt_file_name);
  }
  
  fclose(fp);
  
  return 0;
}
Example #15
0
int main(int argc, char *argv[])
{
    int nativeBlockNum = 0;
    int parityBlockNum = 0;
    int totalBlockNum = 0;
    int gridDimXSize = 0;
    int streamNum = 1;
    char *inFile = NULL;
    char *confFile = NULL;
    char *outFile = NULL;

    enum func
    {
        encode,
        decode
    };
    enum func op;
    int func_flag = 0;

    int option;
    while((option = getopt(argc, argv, "Ss:Pp:Kk:Nn:Ee:Ii:Cc:Oo:Ddh")) != -1) {
        switch (option) {
            case 'S':
            case 's':
                streamNum = (int) atoi(optarg);
                break;

            case 'P':
            case 'p':
                gridDimXSize = (int) atoi(optarg);
                break;

            case 'K':
            case 'k':
                nativeBlockNum = (int) atoi(optarg);
                break;

            case 'N':
            case 'n':
                totalBlockNum = (int) atoi(optarg);
                break;

            case 'E':
            case 'e':
                inFile = optarg;
                op = encode;
                func_flag = 1;
                break;

            case 'D':
            case 'd':
                op = decode;
                func_flag = 1;
                break;

            case 'I':
            case 'i':
                if (func_flag == 1 && op == decode)
                {
                    inFile = optarg;
                }
                else
                {
                    show_help_info();
                }
                break;

            case 'C':
            case 'c':
                if (func_flag == 1 && op == decode)
                {
                    confFile = optarg;
                }
                else
                {
                    show_help_info();
                }
                break;

            case 'O':
            case 'o':
                if (func_flag == 1 && op == decode)
                {
                    outFile = optarg;
                }
                else
                {
                    show_help_info();
                }
                break;

            case 'h':
                show_help_info();
                break;

            default:
                show_help_info();
                break;
        }	/* -----  end switch  ----- */
    }

    switch ( op ) {
        case encode:
            assert(nativeBlockNum != 0);
            assert(totalBlockNum != 0);
            parityBlockNum = totalBlockNum - nativeBlockNum;
            encode_file(inFile, nativeBlockNum, parityBlockNum, gridDimXSize, streamNum);
            break;

        case decode:
            assert(inFile != NULL);
            assert(confFile != NULL);
            decode_file(inFile, confFile, outFile, gridDimXSize, streamNum);
            break;

        default:
            break;
    }		/* -----  end switch  ----- */

    return 0;
}