convutil.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*############################################################################
  2. # Copyright 2016 Intel Corporation
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. ############################################################################*/
  16. /*!
  17. * \file
  18. *
  19. * \brief Conversion utilities implementation.
  20. *
  21. */
  22. #include <stdlib.h>
  23. #include <ctype.h>
  24. #include <string.h>
  25. #include "util/convutil.h"
  26. #include "util/envutil.h"
  27. const char* hash_alg_to_string[] = {"SHA-256", "SHA-384", "SHA-512",
  28. "SHA-512/256", "SHA3/256", "SHA3/384",
  29. "SHA3/512"};
  30. #define COUNT_OF(A) (sizeof(A) / sizeof((A)[0]))
  31. char const* HashAlgToString(HashAlg alg) {
  32. if ((int)alg < 0 || (size_t)alg >= COUNT_OF(hash_alg_to_string))
  33. return "unknown";
  34. return hash_alg_to_string[alg];
  35. }
  36. bool StringToHashAlg(char const* str, HashAlg* alg) {
  37. size_t i;
  38. if (!alg || !str) return false;
  39. for (i = 0; i < COUNT_OF(hash_alg_to_string); i++) {
  40. if (0 == strcmp(str, hash_alg_to_string[i])) {
  41. *alg = (HashAlg)i;
  42. return true;
  43. }
  44. }
  45. log_error("hash algorithm \"%s\" is unknown", str);
  46. return false;
  47. }
  48. const char* epid_file_type_to_string[kNumFileTypes] = {
  49. "IssuingCaPubKey", "GroupPubKey", "PrivRl", "SigRl", "GroupRl"};
  50. char const* EpidFileTypeToString(EpidFileType type) {
  51. if ((int)type < 0 || (size_t)type >= COUNT_OF(epid_file_type_to_string))
  52. return "unknown";
  53. return epid_file_type_to_string[type];
  54. }
  55. bool StringToEpidFileType(char const* str, EpidFileType* type) {
  56. size_t i;
  57. if (!type || !str) return false;
  58. for (i = 0; i < COUNT_OF(epid_file_type_to_string); i++) {
  59. if (0 == strcmp(str, epid_file_type_to_string[i])) {
  60. *type = (EpidFileType)i;
  61. return true;
  62. }
  63. }
  64. log_error("epid file type \"%s\" is unknown", str);
  65. return false;
  66. }