common.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  2. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
  3. #ifdef UA_ENABLE_AMALGAMATION
  4. # include <open62541.h>
  5. #else
  6. # include "ua_types.h"
  7. # include "ua_types_generated_handling.h"
  8. #endif
  9. /* loadFile parses the certificate file.
  10. *
  11. * @param path specifies the file name given in argv[]
  12. * @return Returns the file content after parsing */
  13. static UA_INLINE UA_ByteString
  14. loadFile(const char *const path) {
  15. UA_ByteString fileContents = UA_STRING_NULL;
  16. /* Open the file */
  17. FILE *fp = fopen(path, "rb");
  18. if(!fp) {
  19. errno = 0; /* We read errno also from the tcp layer... */
  20. return fileContents;
  21. }
  22. /* Get the file length, allocate the data and read */
  23. fseek(fp, 0, SEEK_END);
  24. fileContents.length = (size_t)ftell(fp);
  25. fileContents.data = (UA_Byte *)UA_malloc(fileContents.length * sizeof(UA_Byte));
  26. if(fileContents.data) {
  27. fseek(fp, 0, SEEK_SET);
  28. size_t read = fread(fileContents.data, sizeof(UA_Byte), fileContents.length, fp);
  29. if(read != fileContents.length)
  30. UA_ByteString_clear(&fileContents);
  31. } else {
  32. fileContents.length = 0;
  33. }
  34. fclose(fp);
  35. return fileContents;
  36. }