common.h 1.2 KB

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