4 * - Memory Pseudo Files
10 tVFS_Node *VFS_MemFile_Create(const char *Path);
11 void VFS_MemFile_Close(tVFS_Node *Node);
12 size_t VFS_MemFile_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer);
13 size_t VFS_MemFile_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer);
16 tVFS_NodeType gVFS_MemFileType = {
17 .Close = VFS_MemFile_Close,
18 .Read = VFS_MemFile_Read,
19 .Write = VFS_MemFile_Write
24 * \fn tVFS_Node *VFS_MemFile_Create(const char *Path)
26 tVFS_Node *VFS_MemFile_Create(const char *Path)
29 const char *str = Path;
36 for( ; ('0' <= *str && *str <= '9') || ('A' <= *str && *str <= 'F'); str++ )
39 if('A' <= *str && *str <= 'F')
40 base += *str - 'A' + 10;
46 if(*str++ != ':') return NULL;
50 for( ; ('0' <= *str && *str <= '9') || ('A' <= *str && *str <= 'F'); str++ )
53 if('A' <= *str && *str <= 'F')
54 size += *str - 'A' + 10;
59 // Check for NULL byte
60 if(*str != '\0') return NULL;
62 // Allocate and fill node
63 ret = malloc(sizeof(tVFS_Node));
64 memset(ret, 0, sizeof(tVFS_Node));
67 ret->ImplPtr = (void*)base;
72 ret->ACLs = &gVFS_ACL_EveryoneRWX;
75 ret->Type = &gVFS_MemFileType;
81 * \fn void VFS_MemFile_Close(tVFS_Node *Node)
82 * \brief Dereference and clean up a memory file
84 void VFS_MemFile_Close(tVFS_Node *Node)
86 Node->ReferenceCount --;
87 if( Node->ReferenceCount == 0 ) {
94 * \brief Read from a memory file
96 size_t VFS_MemFile_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer)
98 // Check for use of free'd file
99 if(Node->ImplPtr == NULL) return 0;
101 // Check for out of bounds read
102 if(Offset > Node->Size) return 0;
104 // Truncate data read if needed
105 if(Length > Node->Size)
107 if(Offset + Length > Node->Size)
108 Length = Node->Size - Offset;
111 memcpy(Buffer, (Uint8*)Node->ImplPtr + Offset, Length);
117 * \brief Write to a memory file
119 size_t VFS_MemFile_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer)
121 // Check for use of free'd file
122 if(Node->ImplPtr == NULL) return 0;
124 // Check for out of bounds read
125 if(Offset > Node->Size) return 0;
127 // Truncate data read if needed
128 if(Length > Node->Size)
130 if(Offset + Length > Node->Size)
131 Length = Node->Size - Offset;
134 memcpy((Uint8*)Node->ImplPtr + Offset, Buffer, Length);