constants.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis
  2. *
  3. * LibTomCrypt is a library that provides various cryptographic
  4. * algorithms in a highly modular and flexible manner.
  5. *
  6. * The library is free for all purposes without any express
  7. * guarantee it works.
  8. */
  9. #include "tomcrypt.h"
  10. #if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L
  11. #include <libgen.h>
  12. #else
  13. #define basename(x) x
  14. #endif
  15. /**
  16. @file demo_crypt_constants.c
  17. Demo how to get various constants to dynamic languages
  18. like Python
  19. Larry Bugbee, February 2013
  20. */
  21. static void _print_line(const char* cmd, const char* desc)
  22. {
  23. printf(" %-16s - %s\n", cmd, desc);
  24. }
  25. int main(int argc, char **argv)
  26. {
  27. if (argc == 1) {
  28. /* given a specific constant name, get and print its value */
  29. char name[] = "CTR_COUNTER_BIG_ENDIAN";
  30. int value;
  31. char *names_list;
  32. unsigned int names_list_len;
  33. if (crypt_get_constant(name, &value) != 0) exit(EXIT_FAILURE);
  34. printf("\n %s is %d \n\n", name, value);
  35. /* get and print the length of the names (and values) list */
  36. if (crypt_list_all_constants(NULL, &names_list_len) != 0) exit(EXIT_FAILURE);
  37. printf(" need to allocate %u bytes \n\n", names_list_len);
  38. /* get and print the names (and values) list */
  39. if ((names_list = malloc(names_list_len)) == NULL) exit(EXIT_FAILURE);
  40. if (crypt_list_all_constants(names_list, &names_list_len) != 0) exit(EXIT_FAILURE);
  41. printf(" supported constants:\n\n%s\n\n", names_list);
  42. free(names_list);
  43. } else if (argc == 2) {
  44. if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
  45. char* base = strdup(basename(argv[0]));
  46. printf("Usage: %s [-a] [-s name]\n\n", base);
  47. _print_line("<no argument>", "The old behavior of the demo");
  48. _print_line("-a", "Only lists all constants");
  49. _print_line("-s name", "List a single constant given as argument");
  50. _print_line("-h", "The help you're looking at");
  51. free(base);
  52. } else if (strcmp(argv[1], "-a") == 0) {
  53. char *names_list;
  54. unsigned int names_list_len;
  55. /* get and print the length of the names (and values) list */
  56. if (crypt_list_all_constants(NULL, &names_list_len) != 0) exit(EXIT_FAILURE);
  57. /* get and print the names (and values) list */
  58. if ((names_list = malloc(names_list_len)) == NULL) exit(EXIT_FAILURE);
  59. if (crypt_list_all_constants(names_list, &names_list_len) != 0) exit(EXIT_FAILURE);
  60. printf("%s\n", names_list);
  61. free(names_list);
  62. }
  63. } else if (argc == 3) {
  64. if (strcmp(argv[1], "-s") == 0) {
  65. int value;
  66. if (crypt_get_constant(argv[2], &value) != 0) exit(EXIT_FAILURE);
  67. printf("%s,%u\n", argv[2], value);
  68. }
  69. }
  70. return 0;
  71. }
  72. /* ref: $Format:%D$ */
  73. /* git commit: $Format:%H$ */
  74. /* commit time: $Format:%ai$ */