4 * - Memory Pseudo Files
10 tVFS_Node *VFS_MemFile_Create(tVFS_Node *Unused, const char *Path);
11 void VFS_MemFile_Close(tVFS_Node *Node);
12 Uint64 VFS_MemFile_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
13 Uint64 VFS_MemFile_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
16 tVFS_Node gVFS_MemRoot = {
17 .Flags = VFS_FFLAG_DIRECTORY,
19 .FindDir = VFS_MemFile_Create
24 * \fn tVFS_Node *VFS_MemFile_Create(tVFS_Node *Unused, const char *Path)
25 * \note Treated as finddir by VFS_ParsePath
27 tVFS_Node *VFS_MemFile_Create(tVFS_Node *Unused, const char *Path)
30 const char *str = Path;
37 for( ; ('0' <= *str && *str <= '9') || ('A' <= *str && *str <= 'F'); str++ )
40 if('A' <= *str && *str <= 'F')
41 base += *str - 'A' + 10;
47 if(*str++ != ':') return NULL;
51 for( ; ('0' <= *str && *str <= '9') || ('A' <= *str && *str <= 'F'); str++ )
54 if('A' <= *str && *str <= 'F')
55 size += *str - 'A' + 10;
60 // Check for NULL byte
61 if(*str != '\0') return NULL;
63 // Allocate and fill node
64 ret = malloc(sizeof(tVFS_Node));
65 memset(ret, 0, sizeof(tVFS_Node));
68 ret->ImplPtr = (void*)base;
73 ret->ACLs = &gVFS_ACL_EveryoneRWX;
76 ret->Close = VFS_MemFile_Close;
77 ret->Read = VFS_MemFile_Read;
78 ret->Write = VFS_MemFile_Write;
84 * \fn void VFS_MemFile_Close(tVFS_Node *Node)
85 * \brief Dereference and clean up a memory file
87 void VFS_MemFile_Close(tVFS_Node *Node)
89 Node->ReferenceCount --;
90 if( Node->ReferenceCount == 0 ) {
97 * \fn Uint64 VFS_MemFile_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
98 * \brief Read from a memory file
100 Uint64 VFS_MemFile_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
102 // Check for use of free'd file
103 if(Node->ImplPtr == NULL) return 0;
105 // Check for out of bounds read
106 if(Offset > Node->Size) return 0;
108 // Truncate data read if needed
109 if(Offset + Length > Node->Size)
110 Length = Node->Size - Offset;
113 memcpy(Buffer, (Uint8*)Node->ImplPtr + Offset, Length);
119 * \fn Uint64 VFS_MemFile_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
120 * \brief Write to a memory file
122 Uint64 VFS_MemFile_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
124 // Check for use of free'd file
125 if(Node->ImplPtr == NULL) return 0;
127 // Check for out of bounds read
128 if(Offset > Node->Size) return 0;
130 // Truncate data read if needed
131 if(Offset + Length > Node->Size)
132 Length = Node->Size - Offset;
135 memcpy((Uint8*)Node->ImplPtr + Offset, Buffer, Length);