Advertisement
Guest User

Code stuff for FAT32

a guest
Apr 10th, 2024
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. struct FILE {
  2. uint32_t curr_cluster;
  3. uint32_t file_size;
  4. uint32_t fptr;
  5. uint32_t buffptr;
  6. uint8_t currbuf[];
  7. };
  8.  
  9. FILE *fopen(const char *pathname, const char *mode) {
  10. FAT32::DirectoryEntry entry;
  11. if (!EntryForPath(pathname, &entry)) {
  12. if (entry.name != nullptr)
  13. free(entry.name);
  14. return NULL;
  15. }
  16.  
  17. free(entry.name);
  18.  
  19. FILE *f = (FILE *)malloc(sizeof(FILE) + FAT32::MasterFS->cluster_size);
  20. f->curr_cluster = entry.first_cluster;
  21. f->file_size = entry.file_size;
  22. f->fptr = 0;
  23. f->buffptr = 0;
  24. FAT32::MasterFS->GetCluster(f->currbuf, f->curr_cluster);
  25. return f;
  26. }
  27.  
  28. size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
  29. size_t bytes_to_read = size * nmemb;
  30. size_t bytes_read = 0;
  31.  
  32. if (stream->fptr + bytes_to_read > stream->file_size) {
  33. bytes_to_read = stream->file_size - stream->fptr;
  34. }
  35.  
  36. while (bytes_to_read > 0) {
  37. if (stream->buffptr + bytes_to_read > FAT32::MasterFS->cluster_size) {
  38. size_t to_read_in_this_cluster = FAT32::MasterFS->cluster_size - stream->buffptr;
  39. memcpy((void *)((uint64_t)ptr + bytes_read), stream->currbuf + stream->buffptr, to_read_in_this_cluster);
  40. bytes_read += to_read_in_this_cluster;
  41. stream->buffptr = 0;
  42. stream->curr_cluster = FAT32::MasterFS->GetNextClusterID(stream->curr_cluster);
  43.  
  44. if (stream->curr_cluster >= EOC) {
  45.  
  46. stream->fptr += bytes_read;
  47. return bytes_read;
  48. }
  49.  
  50. FAT32::MasterFS->GetCluster(stream->currbuf, stream->curr_cluster);
  51. bytes_to_read -= to_read_in_this_cluster;
  52. } else {
  53. memcpy((void *)((uint64_t)ptr + bytes_read), stream->currbuf + stream->buffptr, bytes_to_read);
  54. bytes_read += bytes_to_read;
  55. stream->buffptr += bytes_to_read;
  56. bytes_to_read = 0;
  57. }
  58. }
  59. stream->fptr += bytes_read;
  60.  
  61. return bytes_read;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement