img_file.c 788 B

12345678910111213141516171819202122232425262728293031
  1. /* Whole-file image loading for ring-3 commands (see img_file.h). */
  2. #include "img_file.h"
  3. #include "ucmd_lib.h"
  4. int img_read_file(const char *path, void *buf, unsigned int cap)
  5. {
  6. int n;
  7. if (!path || !buf || cap == 0)
  8. return IMG_ERR_IO;
  9. n = u_syscall3(SYS_READ_FILE, (int)path, (int)buf, (int)cap);
  10. if (n < 0)
  11. return IMG_ERR_IO;
  12. if (n == 0)
  13. return IMG_ERR_EMPTY;
  14. if ((unsigned int)n >= cap)
  15. return IMG_ERR_TOOBIG; /* filled the buffer: file is larger */
  16. return n;
  17. }
  18. int img_decode_file(const char *path, void *filebuf, unsigned int filecap,
  19. const struct img_options *opt, struct img_mem *mem,
  20. struct img_image *out)
  21. {
  22. int n = img_read_file(path, filebuf, filecap);
  23. if (n < 0)
  24. return n;
  25. return img_decode(filebuf, (unsigned int)n, opt, mem, out);
  26. }