ufont_file.c 823 B

12345678910111213141516171819202122232425262728293031
  1. /* VFS face loading for ring-3 commands. The host tests use
  2. * ufont_face_open_mem() and do not link this file. */
  3. #include "ufont.h"
  4. #include "ucmd_lib.h"
  5. #include <stdlib.h>
  6. #include <string.h>
  7. struct ufont_face *ufont_face_open_path(const char *path)
  8. {
  9. struct sys_stat st;
  10. unsigned char *buf;
  11. struct ufont_face *f;
  12. int n;
  13. if (!path || u_stat(path, &st) != 0 || st.is_dir || st.size == 0 ||
  14. st.size > UFONT_MAX_FILE - 512u)
  15. return 0;
  16. /* FAT reads may write a final partial sector past the requested
  17. * length; keep the slack inside the buffer. */
  18. buf = (unsigned char *)malloc(st.size + 512u);
  19. if (!buf)
  20. return 0;
  21. n = u_syscall3(SYS_READ_FILE, (int)path, (int)buf, (int)st.size);
  22. if (n != (int)st.size) {
  23. free(buf);
  24. return 0;
  25. }
  26. f = ufont_face_open_mem(buf, st.size);
  27. free(buf);
  28. return f;
  29. }