option.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /*
  2. File autogenerated by gengetopt version 2.22.6
  3. generated with the following command:
  4. gengetopt --output-dir=../gengetopt --file-name=option --unamed-opts
  5. The developers of gengetopt consider the fixed text that goes in all
  6. gengetopt output files to be in the public domain:
  7. we make no copyright claims on it.
  8. */
  9. /* If we use autoconf. */
  10. #ifdef HAVE_CONFIG_H
  11. #include "config.h"
  12. #endif
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #ifndef FIX_UNUSED
  17. #define FIX_UNUSED(X) (void) (X) /* avoid warnings for unused params */
  18. #endif
  19. #include <getopt.h>
  20. #include "option.h"
  21. const char *gengetopt_args_info_purpose = "\n\nCe programme a pour objectif de démontrer la faisabilité d'un traitement\ncohérent sur des données chiffrées, avec la cryptographie homomorphe. Il\ns'agit d'une preuve de concept. Les données traitées sont des sous-titres\nchiffrés avec le schéma homomorphe BGNFCF. Il s'agit d'une variante du\nschéma de Boneh Goh Nissim (2006) ayant subi la conversion de Freeman (2010)\nainsi que la transformation de Catalano et Fiore (2015). Le traitement opéré\nsur les sous-titres est le changement de casse des caractères. Il s'opère en\névaluant un circuit arithmétique de profondeur multiplicative 2.\n\nDescription des circuits :\n\nminmaj : passage minuscule majuscule, sans erreur, avec précalcul et\npostcalcul\ndemo : minmaj sur un caractère avec affichage des chiffrés et de l'évalué\nminmaj2 : passage minuscule majuscule, avec erreur\nminmaj3 : passage minuscule majuscule, avec erreur et postcalcul\nsize : donne la taille en octets des chiffrés de différents niveaux\ntime : donne le temps en millisecondes du précalcul, calcul et postcalcul\ntime2 : donne le temps en millisecondes du chiffrement, de l'évaluation et du\ndéchiffrement\naddition et multiplication : teste les différentes opérations possibles et\ndonne le temps pour chacune\nchiffrement : chiffre et déchiffre en mode curve et twist sans évaluation\nhomomorphique\nip : mise à jour des ips et du compteur selon la valeur du compteur \nscalar_product : calcul du produit scalaire de vecteur de bits\n\nLa version 2 effectue du calcul parallèle avec OpenMP. Le temps de calcul est\ndiminué si l'on dispose de processeurs multi-coeurs.\n\n\n";
  22. const char *gengetopt_args_info_usage = "Usage: BGNFCF [OPTIONS]... [FILES]...";
  23. const char *gengetopt_args_info_versiontext = "";
  24. const char *gengetopt_args_info_description = "";
  25. const char *gengetopt_args_info_help[] = {
  26. " -h, --help Print help and exit",
  27. " -V, --version Print version and exit",
  28. " -s, --subtitle=SUBTITLE Pour indiquer les sous-titres en ligne de commande",
  29. " -c, --circuit=ENUM Pour indiquer le circuit de calcul. minmaj=avec\n précalcul et postcalcul, minmaj2=avec erreur,\n minmaj3=avec erreur et postcalcul. (possible\n values=\"minmaj\", \"chiffrement\",\n \"additionL1\", \"additionL2\", \"additionL3\",\n \"additionL4\", \"multiplicationL1\",\n \"multiplicationL1L2\", \"multiplicationL2\",\n \"size\", \"time\", \"minmaj2\", \"minmaj3\",\n \"demo\", \"ip\", \"scalar_product\", \"time2\"\n default=`minmaj')",
  30. 0
  31. };
  32. typedef enum {ARG_NO
  33. , ARG_STRING
  34. , ARG_ENUM
  35. } cmdline_parser_arg_type;
  36. static
  37. void clear_given (struct gengetopt_args_info *args_info);
  38. static
  39. void clear_args (struct gengetopt_args_info *args_info);
  40. static int
  41. cmdline_parser_internal (int argc, char **argv, struct gengetopt_args_info *args_info,
  42. struct cmdline_parser_params *params, const char *additional_error);
  43. const char *cmdline_parser_circuit_values[] = {"minmaj", "chiffrement", "additionL1", "additionL2", "additionL3", "additionL4", "multiplicationL1", "multiplicationL1L2", "multiplicationL2", "size", "time", "minmaj2", "minmaj3", "demo", "ip", "scalar_product", "time2", 0}; /*< Possible values for circuit. */
  44. static char *
  45. gengetopt_strdup (const char *s);
  46. static
  47. void clear_given (struct gengetopt_args_info *args_info)
  48. {
  49. args_info->help_given = 0 ;
  50. args_info->version_given = 0 ;
  51. args_info->subtitle_given = 0 ;
  52. args_info->circuit_given = 0 ;
  53. }
  54. static
  55. void clear_args (struct gengetopt_args_info *args_info)
  56. {
  57. FIX_UNUSED (args_info);
  58. args_info->subtitle_arg = NULL;
  59. args_info->subtitle_orig = NULL;
  60. args_info->circuit_arg = circuit_arg_minmaj;
  61. args_info->circuit_orig = NULL;
  62. }
  63. static
  64. void init_args_info(struct gengetopt_args_info *args_info)
  65. {
  66. args_info->help_help = gengetopt_args_info_help[0] ;
  67. args_info->version_help = gengetopt_args_info_help[1] ;
  68. args_info->subtitle_help = gengetopt_args_info_help[2] ;
  69. args_info->circuit_help = gengetopt_args_info_help[3] ;
  70. }
  71. void
  72. cmdline_parser_print_version (void)
  73. {
  74. printf ("%s %s\n",
  75. (strlen(CMDLINE_PARSER_PACKAGE_NAME) ? CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE),
  76. CMDLINE_PARSER_VERSION);
  77. if (strlen(gengetopt_args_info_versiontext) > 0)
  78. printf("\n%s\n", gengetopt_args_info_versiontext);
  79. }
  80. static void print_help_common(void) {
  81. cmdline_parser_print_version ();
  82. if (strlen(gengetopt_args_info_purpose) > 0)
  83. printf("\n%s\n", gengetopt_args_info_purpose);
  84. if (strlen(gengetopt_args_info_usage) > 0)
  85. printf("\n%s\n", gengetopt_args_info_usage);
  86. printf("\n");
  87. if (strlen(gengetopt_args_info_description) > 0)
  88. printf("%s\n\n", gengetopt_args_info_description);
  89. }
  90. void
  91. cmdline_parser_print_help (void)
  92. {
  93. int i = 0;
  94. print_help_common();
  95. while (gengetopt_args_info_help[i])
  96. printf("%s\n", gengetopt_args_info_help[i++]);
  97. }
  98. void
  99. cmdline_parser_init (struct gengetopt_args_info *args_info)
  100. {
  101. clear_given (args_info);
  102. clear_args (args_info);
  103. init_args_info (args_info);
  104. args_info->inputs = 0;
  105. args_info->inputs_num = 0;
  106. }
  107. void
  108. cmdline_parser_params_init(struct cmdline_parser_params *params)
  109. {
  110. if (params)
  111. {
  112. params->override = 0;
  113. params->initialize = 1;
  114. params->check_required = 1;
  115. params->check_ambiguity = 0;
  116. params->print_errors = 1;
  117. }
  118. }
  119. struct cmdline_parser_params *
  120. cmdline_parser_params_create(void)
  121. {
  122. struct cmdline_parser_params *params =
  123. (struct cmdline_parser_params *)malloc(sizeof(struct cmdline_parser_params));
  124. cmdline_parser_params_init(params);
  125. return params;
  126. }
  127. static void
  128. free_string_field (char **s)
  129. {
  130. if (*s)
  131. {
  132. free (*s);
  133. *s = 0;
  134. }
  135. }
  136. static void
  137. cmdline_parser_release (struct gengetopt_args_info *args_info)
  138. {
  139. unsigned int i;
  140. free_string_field (&(args_info->subtitle_arg));
  141. free_string_field (&(args_info->subtitle_orig));
  142. free_string_field (&(args_info->circuit_orig));
  143. for (i = 0; i < args_info->inputs_num; ++i)
  144. free (args_info->inputs [i]);
  145. if (args_info->inputs_num)
  146. free (args_info->inputs);
  147. clear_given (args_info);
  148. }
  149. /**
  150. * @param val the value to check
  151. * @param values the possible values
  152. * @return the index of the matched value:
  153. * -1 if no value matched,
  154. * -2 if more than one value has matched
  155. */
  156. static int
  157. check_possible_values(const char *val, const char *values[])
  158. {
  159. int i, found, last;
  160. size_t len;
  161. if (!val) /* otherwise strlen() crashes below */
  162. return -1; /* -1 means no argument for the option */
  163. found = last = 0;
  164. for (i = 0, len = strlen(val); values[i]; ++i)
  165. {
  166. if (strncmp(val, values[i], len) == 0)
  167. {
  168. ++found;
  169. last = i;
  170. if (strlen(values[i]) == len)
  171. return i; /* exact macth no need to check more */
  172. }
  173. }
  174. if (found == 1) /* one match: OK */
  175. return last;
  176. return (found ? -2 : -1); /* return many values or none matched */
  177. }
  178. static void
  179. write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[])
  180. {
  181. int found = -1;
  182. if (arg) {
  183. if (values) {
  184. found = check_possible_values(arg, values);
  185. }
  186. if (found >= 0)
  187. fprintf(outfile, "%s=\"%s\" # %s\n", opt, arg, values[found]);
  188. else
  189. fprintf(outfile, "%s=\"%s\"\n", opt, arg);
  190. } else {
  191. fprintf(outfile, "%s\n", opt);
  192. }
  193. }
  194. int
  195. cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
  196. {
  197. int i = 0;
  198. if (!outfile)
  199. {
  200. fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_PACKAGE);
  201. return EXIT_FAILURE;
  202. }
  203. if (args_info->help_given)
  204. write_into_file(outfile, "help", 0, 0 );
  205. if (args_info->version_given)
  206. write_into_file(outfile, "version", 0, 0 );
  207. if (args_info->subtitle_given)
  208. write_into_file(outfile, "subtitle", args_info->subtitle_orig, 0);
  209. if (args_info->circuit_given)
  210. write_into_file(outfile, "circuit", args_info->circuit_orig, cmdline_parser_circuit_values);
  211. i = EXIT_SUCCESS;
  212. return i;
  213. }
  214. int
  215. cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
  216. {
  217. FILE *outfile;
  218. int i = 0;
  219. outfile = fopen(filename, "w");
  220. if (!outfile)
  221. {
  222. fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
  223. return EXIT_FAILURE;
  224. }
  225. i = cmdline_parser_dump(outfile, args_info);
  226. fclose (outfile);
  227. return i;
  228. }
  229. void
  230. cmdline_parser_free (struct gengetopt_args_info *args_info)
  231. {
  232. cmdline_parser_release (args_info);
  233. }
  234. /** @brief replacement of strdup, which is not standard */
  235. char *
  236. gengetopt_strdup (const char *s)
  237. {
  238. char *result = 0;
  239. if (!s)
  240. return result;
  241. result = (char*)malloc(strlen(s) + 1);
  242. if (result == (char*)0)
  243. return (char*)0;
  244. strcpy(result, s);
  245. return result;
  246. }
  247. int
  248. cmdline_parser (int argc, char **argv, struct gengetopt_args_info *args_info)
  249. {
  250. return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
  251. }
  252. int
  253. cmdline_parser_ext (int argc, char **argv, struct gengetopt_args_info *args_info,
  254. struct cmdline_parser_params *params)
  255. {
  256. int result;
  257. result = cmdline_parser_internal (argc, argv, args_info, params, 0);
  258. if (result == EXIT_FAILURE)
  259. {
  260. cmdline_parser_free (args_info);
  261. exit (EXIT_FAILURE);
  262. }
  263. return result;
  264. }
  265. int
  266. cmdline_parser2 (int argc, char **argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
  267. {
  268. int result;
  269. struct cmdline_parser_params params;
  270. params.override = override;
  271. params.initialize = initialize;
  272. params.check_required = check_required;
  273. params.check_ambiguity = 0;
  274. params.print_errors = 1;
  275. result = cmdline_parser_internal (argc, argv, args_info, &params, 0);
  276. if (result == EXIT_FAILURE)
  277. {
  278. cmdline_parser_free (args_info);
  279. exit (EXIT_FAILURE);
  280. }
  281. return result;
  282. }
  283. int
  284. cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
  285. {
  286. FIX_UNUSED (args_info);
  287. FIX_UNUSED (prog_name);
  288. return EXIT_SUCCESS;
  289. }
  290. static char *package_name = 0;
  291. /**
  292. * @brief updates an option
  293. * @param field the generic pointer to the field to update
  294. * @param orig_field the pointer to the orig field
  295. * @param field_given the pointer to the number of occurrence of this option
  296. * @param prev_given the pointer to the number of occurrence already seen
  297. * @param value the argument for this option (if null no arg was specified)
  298. * @param possible_values the possible values for this option (if specified)
  299. * @param default_value the default value (in case the option only accepts fixed values)
  300. * @param arg_type the type of this option
  301. * @param check_ambiguity @see cmdline_parser_params.check_ambiguity
  302. * @param override @see cmdline_parser_params.override
  303. * @param no_free whether to free a possible previous value
  304. * @param multiple_option whether this is a multiple option
  305. * @param long_opt the corresponding long option
  306. * @param short_opt the corresponding short option (or '-' if none)
  307. * @param additional_error possible further error specification
  308. */
  309. static
  310. int update_arg(void *field, char **orig_field,
  311. unsigned int *field_given, unsigned int *prev_given,
  312. char *value, const char *possible_values[],
  313. const char *default_value,
  314. cmdline_parser_arg_type arg_type,
  315. int check_ambiguity, int override,
  316. int no_free, int multiple_option,
  317. const char *long_opt, char short_opt,
  318. const char *additional_error)
  319. {
  320. char *stop_char = 0;
  321. const char *val = value;
  322. int found;
  323. char **string_field;
  324. FIX_UNUSED (field);
  325. stop_char = 0;
  326. found = 0;
  327. if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
  328. {
  329. if (short_opt != '-')
  330. fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n",
  331. package_name, long_opt, short_opt,
  332. (additional_error ? additional_error : ""));
  333. else
  334. fprintf (stderr, "%s: `--%s' option given more than once%s\n",
  335. package_name, long_opt,
  336. (additional_error ? additional_error : ""));
  337. return 1; /* failure */
  338. }
  339. if (possible_values && (found = check_possible_values((value ? value : default_value), possible_values)) < 0)
  340. {
  341. if (short_opt != '-')
  342. fprintf (stderr, "%s: %s argument, \"%s\", for option `--%s' (`-%c')%s\n",
  343. package_name, (found == -2) ? "ambiguous" : "invalid", value, long_opt, short_opt,
  344. (additional_error ? additional_error : ""));
  345. else
  346. fprintf (stderr, "%s: %s argument, \"%s\", for option `--%s'%s\n",
  347. package_name, (found == -2) ? "ambiguous" : "invalid", value, long_opt,
  348. (additional_error ? additional_error : ""));
  349. return 1; /* failure */
  350. }
  351. if (field_given && *field_given && ! override)
  352. return 0;
  353. if (prev_given)
  354. (*prev_given)++;
  355. if (field_given)
  356. (*field_given)++;
  357. if (possible_values)
  358. val = possible_values[found];
  359. switch(arg_type) {
  360. case ARG_ENUM:
  361. if (val) *((int *)field) = found;
  362. break;
  363. case ARG_STRING:
  364. if (val) {
  365. string_field = (char **)field;
  366. if (!no_free && *string_field)
  367. free (*string_field); /* free previous string */
  368. *string_field = gengetopt_strdup (val);
  369. }
  370. break;
  371. default:
  372. break;
  373. };
  374. /* store the original value */
  375. switch(arg_type) {
  376. case ARG_NO:
  377. break;
  378. default:
  379. if (value && orig_field) {
  380. if (no_free) {
  381. *orig_field = value;
  382. } else {
  383. if (*orig_field)
  384. free (*orig_field); /* free previous string */
  385. *orig_field = gengetopt_strdup (value);
  386. }
  387. }
  388. };
  389. return 0; /* OK */
  390. }
  391. int
  392. cmdline_parser_internal (
  393. int argc, char **argv, struct gengetopt_args_info *args_info,
  394. struct cmdline_parser_params *params, const char *additional_error)
  395. {
  396. int c; /* Character of the parsed option. */
  397. int error_occurred = 0;
  398. struct gengetopt_args_info local_args_info;
  399. int override;
  400. int initialize;
  401. int check_required;
  402. int check_ambiguity;
  403. package_name = argv[0];
  404. override = params->override;
  405. initialize = params->initialize;
  406. check_required = params->check_required;
  407. check_ambiguity = params->check_ambiguity;
  408. if (initialize)
  409. cmdline_parser_init (args_info);
  410. cmdline_parser_init (&local_args_info);
  411. optarg = 0;
  412. optind = 0;
  413. opterr = params->print_errors;
  414. optopt = '?';
  415. while (1)
  416. {
  417. int option_index = 0;
  418. static struct option long_options[] = {
  419. { "help", 0, NULL, 'h' },
  420. { "version", 0, NULL, 'V' },
  421. { "subtitle", 1, NULL, 's' },
  422. { "circuit", 1, NULL, 'c' },
  423. { 0, 0, 0, 0 }
  424. };
  425. c = getopt_long (argc, argv, "hVs:c:", long_options, &option_index);
  426. if (c == -1) break; /* Exit from `while (1)' loop. */
  427. switch (c)
  428. {
  429. case 'h': /* Print help and exit. */
  430. cmdline_parser_print_help ();
  431. cmdline_parser_free (&local_args_info);
  432. exit (EXIT_SUCCESS);
  433. case 'V': /* Print version and exit. */
  434. cmdline_parser_print_version ();
  435. cmdline_parser_free (&local_args_info);
  436. exit (EXIT_SUCCESS);
  437. case 's': /* Pour indiquer les sous-titres en ligne de commande. */
  438. if (update_arg( (void *)&(args_info->subtitle_arg),
  439. &(args_info->subtitle_orig), &(args_info->subtitle_given),
  440. &(local_args_info.subtitle_given), optarg, 0, 0, ARG_STRING,
  441. check_ambiguity, override, 0, 0,
  442. "subtitle", 's',
  443. additional_error))
  444. goto failure;
  445. break;
  446. case 'c': /* Pour indiquer le circuit de calcul. minmaj=avec précalcul et postcalcul, minmaj2=avec erreur, minmaj3=avec erreur et postcalcul.. */
  447. if (update_arg( (void *)&(args_info->circuit_arg),
  448. &(args_info->circuit_orig), &(args_info->circuit_given),
  449. &(local_args_info.circuit_given), optarg, cmdline_parser_circuit_values, "minmaj", ARG_ENUM,
  450. check_ambiguity, override, 0, 0,
  451. "circuit", 'c',
  452. additional_error))
  453. goto failure;
  454. break;
  455. case 0: /* Long option with no short option */
  456. case '?': /* Invalid option. */
  457. /* `getopt_long' already printed an error message. */
  458. goto failure;
  459. default: /* bug: option not considered. */
  460. fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
  461. abort ();
  462. } /* switch */
  463. } /* while */
  464. cmdline_parser_release (&local_args_info);
  465. if ( error_occurred )
  466. return (EXIT_FAILURE);
  467. if (optind < argc)
  468. {
  469. int i = 0 ;
  470. int found_prog_name = 0;
  471. /* whether program name, i.e., argv[0], is in the remaining args
  472. (this may happen with some implementations of getopt,
  473. but surely not with the one included by gengetopt) */
  474. i = optind;
  475. while (i < argc)
  476. if (argv[i++] == argv[0]) {
  477. found_prog_name = 1;
  478. break;
  479. }
  480. i = 0;
  481. args_info->inputs_num = argc - optind - found_prog_name;
  482. args_info->inputs =
  483. (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
  484. while (optind < argc)
  485. if (argv[optind++] != argv[0])
  486. args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind-1]) ;
  487. }
  488. return 0;
  489. failure:
  490. cmdline_parser_release (&local_args_info);
  491. return (EXIT_FAILURE);
  492. }