common.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  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. #include "open62541.h"
  4. /* loadFile parses the certificate file.
  5. *
  6. * @param path specifies the file name given in argv[]
  7. * @return Returns the file content after parsing */
  8. static UA_INLINE UA_ByteString
  9. loadFile(const char *const path) {
  10. UA_ByteString fileContents = UA_STRING_NULL;
  11. /* Open the file */
  12. FILE *fp = fopen(path, "rb");
  13. if(!fp) {
  14. errno = 0; /* We read errno also from the tcp layer... */
  15. return fileContents;
  16. }
  17. /* Get the file length, allocate the data and read */
  18. fseek(fp, 0, SEEK_END);
  19. fileContents.length = (size_t)ftell(fp);
  20. fileContents.data = (UA_Byte *)UA_malloc(fileContents.length * sizeof(UA_Byte));
  21. if(fileContents.data) {
  22. fseek(fp, 0, SEEK_SET);
  23. size_t read = fread(fileContents.data, sizeof(UA_Byte), fileContents.length, fp);
  24. if(read != fileContents.length)
  25. UA_ByteString_deleteMembers(&fileContents);
  26. } else {
  27. fileContents.length = 0;
  28. }
  29. fclose(fp);
  30. return fileContents;
  31. }