Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct FILE {
- uint32_t curr_cluster;
- uint32_t file_size;
- uint32_t fptr;
- uint32_t buffptr;
- uint8_t currbuf[];
- };
- FILE *fopen(const char *pathname, const char *mode) {
- FAT32::DirectoryEntry entry;
- if (!EntryForPath(pathname, &entry)) {
- if (entry.name != nullptr)
- free(entry.name);
- return NULL;
- }
- free(entry.name);
- FILE *f = (FILE *)malloc(sizeof(FILE) + FAT32::MasterFS->cluster_size);
- f->curr_cluster = entry.first_cluster;
- f->file_size = entry.file_size;
- f->fptr = 0;
- f->buffptr = 0;
- FAT32::MasterFS->GetCluster(f->currbuf, f->curr_cluster);
- return f;
- }
- size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
- size_t bytes_to_read = size * nmemb;
- size_t bytes_read = 0;
- if (stream->fptr + bytes_to_read > stream->file_size) {
- bytes_to_read = stream->file_size - stream->fptr;
- }
- while (bytes_to_read > 0) {
- if (stream->buffptr + bytes_to_read > FAT32::MasterFS->cluster_size) {
- size_t to_read_in_this_cluster = FAT32::MasterFS->cluster_size - stream->buffptr;
- memcpy((void *)((uint64_t)ptr + bytes_read), stream->currbuf + stream->buffptr, to_read_in_this_cluster);
- bytes_read += to_read_in_this_cluster;
- stream->buffptr = 0;
- stream->curr_cluster = FAT32::MasterFS->GetNextClusterID(stream->curr_cluster);
- if (stream->curr_cluster >= EOC) {
- stream->fptr += bytes_read;
- return bytes_read;
- }
- FAT32::MasterFS->GetCluster(stream->currbuf, stream->curr_cluster);
- bytes_to_read -= to_read_in_this_cluster;
- } else {
- memcpy((void *)((uint64_t)ptr + bytes_read), stream->currbuf + stream->buffptr, bytes_to_read);
- bytes_read += bytes_to_read;
- stream->buffptr += bytes_to_read;
- bytes_to_read = 0;
- }
- }
- stream->fptr += bytes_read;
- return bytes_read;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement