common.h 1.2 KB

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