a9f2e1757aa6b231ceac40c78eace3072b5c1b84
[tpg/acess2.git] / Usermode / Libraries / liburi.so_src / main.c
1 /*
2  * Acess2 - URI Parser and opener
3  * By John Hodge (thePowersGang)
4  */
5 #include <acess/sys.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <ctype.h>
10 #include <uri.h>
11
12 // === STRUCTURES ===
13 struct sURIFile
14 {
15          int    Handle;
16          int    Mode;
17         tURIHandler     *Handler;
18          int    CurBlockOffset;
19         char    Buffer[];
20 };
21
22 // === PROTOTYPES ===
23  int    SoMain(void);
24 tURI    *URI_Parse(const char *String);
25 tURIFile        *URI_MakeHandle(int Mode, int Handle, tURIHandler *Handler);
26 tURIFile        *URI_Open(int Mode, tURI *URI);
27 size_t  URI_Read(tURIFile *File, size_t Bytes, void *Buffer);
28 size_t  URI_Write(tURIFile *File, size_t Bytes, void *Buffer);
29 void    URI_Close(tURIFile *File);
30 // --- file:/// handler
31  int    URI_file_Open(char *Host, int Port, char *Path, int Mode);
32 size_t  URI_file_Read(int Handle, size_t Bytes, void *Buffer);
33 size_t  URI_file_Write(int Handle, size_t Bytes, void *Buffer);
34 void    URI_file_Close(int Handle);
35 off_t   URI_file_GetSize(int Handle);
36
37 // === CONSTANTS ===
38 // Builtin URI protocol handlers
39 tURIHandler     caBuiltinHandlers[] = {
40         {"file", 0, URI_file_Open, URI_file_Close, URI_file_Read, URI_file_Write, URI_file_GetSize}
41 };
42 #define NUM_BUILTIN_HANDLERS    (sizeof(caBuiltinHandlers)/sizeof(caBuiltinHandlers[0]))
43
44 // === CODE ===
45 int SoMain(void)
46 {
47         return 0;
48 }
49
50 tURI *URI_Parse(const char *String)
51 {
52         const char      *tmp = String;
53         tURI    *ret;
54          int    protolen;
55         
56         if(!String)     return NULL;
57         
58         protolen = 0;
59         while( isalpha(*tmp) || isdigit(*tmp) ) tmp++, protolen++;
60         
61         // true URI
62         if(tmp[0] == ':' && tmp[1] == '/' && tmp[2] == '/')
63         {
64                  int    hostlen, portlen, pathlen;
65                 tmp += 3;       // Eat '://'
66                 ret = malloc(sizeof(tURI) + protolen + 1 + strlen(tmp) + 1);
67                 
68                 ret->Proto = (char*)ret + sizeof(tURI);
69                 memcpy(ret->Proto, String, protolen);
70                 ret->Proto[protolen] = '\0';
71                 
72                 ret->Host = ret->Proto + protolen + 1;
73                 hostlen = 0;
74                 
75                 // IPv6
76                 if( *tmp == '[' )
77                 {
78                         tmp ++;
79                         while( *tmp && *tmp != ']' ) {
80                                 ret->Host[hostlen] = *tmp;
81                                 tmp ++;
82                                 hostlen ++;
83                         }
84                         tmp ++;
85                         ret->Host[hostlen] = '\0';
86                 }
87                 // IPv4/DNS
88                 else
89                 {
90                         while( *tmp && *tmp != '/' && *tmp != ':' )
91                         {
92                                 ret->Host[hostlen] = *tmp;
93                                 tmp ++;
94                                 hostlen ++;
95                         }
96                         ret->Host[hostlen] = '\0';
97                 }
98                 
99                 // Port
100                 if( *tmp == ':' ) {
101                         ret->PortStr = ret->Host + hostlen + 1;
102                         tmp ++;
103                         portlen = 0;
104                         while(isalpha(*tmp) || isdigit(*tmp))
105                         {
106                                 ret->PortStr[portlen] = *tmp;
107                                 portlen ++;
108                                 tmp ++;
109                         }
110                         ret->PortStr[portlen] = '\0';
111                         
112                         ret->PortNum = atoi(ret->PortStr);
113                         if(!ret->PortNum && !(ret->PortStr[0] == '0' && portlen == 1) )
114                         {
115                                 // error!
116                                 ret->Path = NULL;
117                                 return ret;
118                         }
119                 }
120                 else {
121                         ret->PortStr = NULL;
122                         ret->PortNum = -1;
123                         portlen = 0;
124                 }
125                 
126                 if(*tmp == '\0')
127                 {
128                         ret->Path = NULL;
129                         return ret;
130                 }
131                 
132                 // TODO: What to do on a parse error
133                 if(*tmp != '/') {
134                         ret->Path = NULL;
135                         return ret;
136                 }
137                 
138                 if(ret->PortStr)
139                         ret->Path = ret->PortStr + portlen + 1;
140                 else
141                         ret->Path = ret->Host + hostlen + 1;
142                 
143                 pathlen = 0;
144                 strcpy(ret->Path, tmp);
145                 
146                 return ret;
147         }
148         else
149         {
150 //              char    *cwd;
151 //               int    retlen;
152                 
153                 // Path
154                 // TODO: What to do?
155                 // Probably return file:///<path>
156                 // but should I get the CWD and use append that?
157                 ret = malloc( sizeof(tURI) + strlen(String) + 1 );
158                 ret->Path = (char*)ret + sizeof(tURI);
159                 strcpy(ret->Path, String);
160                 ret->Proto = "file";
161                 ret->Host = NULL;
162                 ret->PortNum = 0;
163                 ret->PortStr = NULL;
164                 return ret;
165         }
166 }
167
168 tURIFile *URI_MakeHandle(int Mode, int Handle, tURIHandler *Handler)
169 {
170         tURIFile        *ret;
171         
172         ret = malloc(sizeof(tURIFile)+Handler->BlockSize);
173         if(!ret)        return NULL;
174         
175         ret->Handle = Handle;
176         ret->Mode = Mode;
177         ret->Handler = Handler;
178         ret->CurBlockOffset = 0;
179         
180         return ret;
181 }
182
183 tURIFile *URI_Open(int Mode, tURI *URI)
184 {
185         tURIHandler     *handler;
186         tURIFile        *ret;
187          int    handle;
188          int    i;
189         
190         if(!URI)
191                 return NULL;
192         
193         for( i = 0; i < NUM_BUILTIN_HANDLERS; i ++ )
194         {
195                 if(strcmp(URI->Proto, caBuiltinHandlers[i].Name) == 0)
196                         break;
197         }
198         
199         if( i == NUM_BUILTIN_HANDLERS )
200         {
201                 // TODO: Dynamics
202                 printf("URI_Open: Warning - Unknown URI handler\n");
203                 return NULL;
204         }
205         else
206                 handler = &caBuiltinHandlers[i];
207         
208         printf("URI_Open: handler->Open = %p\n", handler->Open);
209         
210         handle = handler->Open(URI->Host, URI->PortNum, URI->Path, Mode);
211         printf("URI_Open: handle = %i\n", handle);
212         if(handle == -1)        return NULL;
213         
214         printf("URI_MakeHandle(Mode=%i, handle=%i, handler=%p)\n",
215                 Mode, handle, handler);
216         ret = URI_MakeHandle(Mode, handle, handler);
217         if(!ret) {
218                 handler->Close( handle );
219                 return NULL;
220         }
221         return ret;
222 }
223
224 int URI_GetSize(tURIFile *File, size_t *Size)
225 {
226         if( !File || !Size )    return -1;
227         
228         if( File->Handler->GetSize )
229         {
230                 *Size = File->Handler->GetSize(File->Handle);
231                 return 0;       // Success
232         }
233         
234         return 1;       // Size not avaliable
235 }
236
237 /**
238  * \brief Read from a URI file
239  */
240 size_t URI_Read(tURIFile *File, size_t Bytes, void *Buffer)
241 {
242         size_t  rem = Bytes;
243         void    *buf = Buffer;
244         size_t  tmp;
245         
246         printf("URI_Read(File=%p, Bytes=%u, Buffer=%p)\n",
247                 File, (unsigned int)Bytes, Buffer);
248         
249         if(!File || !Buffer)    return -1;
250         if(Bytes == 0)  return 0;
251         
252         if( !(File->Mode & URI_MODE_READ) )     return -1;
253         
254         // Read from cache if avaliable
255         if(File->Handler->BlockSize && File->CurBlockOffset)
256         {
257                  int    avail;
258                 
259                 avail = File->Handler->BlockSize - File->CurBlockOffset;
260                 
261                 if(avail >= Bytes) {
262                         memcpy(Buffer, File->Buffer, Bytes);
263                         File->CurBlockOffset += Bytes;
264                         File->CurBlockOffset %= File->Handler->BlockSize;
265                         return Bytes;
266                 }
267                 
268                 rem -= avail;
269                 memcpy(Buffer, File->Buffer, avail);
270                 File->CurBlockOffset = 0;
271                 buf += avail;
272         }
273         
274         
275         if( File->Handler->BlockSize )
276         {
277                 // Read whole blocks
278                 while( rem >= File->Handler->BlockSize )
279                 {
280                         tmp = File->Handler->Read( File->Handle, File->Handler->BlockSize, buf );
281                         if(tmp < File->Handler->BlockSize)
282                                 return Bytes - rem - tmp;
283                         buf += File->Handler->BlockSize;
284                 }
285                 
286                 // Read the trailing part
287                 if(rem)
288                 {
289                         File->Handler->Read( File->Handle, File->Handler->BlockSize, File->Buffer );
290                         memcpy( buf, File->Buffer, rem );
291                         File->CurBlockOffset += rem;
292                 }
293                 return Bytes;
294         }
295         
296         return File->Handler->Read( File->Handle, Bytes, Buffer );
297 }
298
299 /**
300  * \brief Write to a URI file
301  */
302
303
304 // ====
305 // Builtin Handlers
306 // ====
307 int URI_file_Open(char *Host, int Port, char *Path, int Mode)
308 {
309          int    smode = 0;
310         if(Mode & URI_MODE_READ)        smode |= OPENFLAG_READ;
311         if(Mode & URI_MODE_WRITE)       smode |= OPENFLAG_WRITE;
312         
313 //      printf("URI_file_Open: open('%s', 0x%x)\n", Path, smode);
314         {
315                  int    ret;
316                 ret = _SysOpen(Path, smode);
317                 return ret;
318         }
319 }
320 size_t URI_file_Read(int Handle, size_t Bytes, void *Buffer)
321 {
322 //      printf("URI_file_Read: (Handle=%i, Buffer=%p, Bytes=%i)\n",
323 //              Handle, Buffer, (int)Bytes);
324         return _SysRead(Handle, Buffer, Bytes);
325 }
326 size_t URI_file_Write(int Handle, size_t Bytes, void *Buffer)
327 {
328         return _SysWrite(Handle, Buffer, Bytes);
329 }
330 void URI_file_Close(int Handle)
331 {
332         _SysClose(Handle);
333 }
334 off_t URI_file_GetSize(int Handle)
335 {
336         uint64_t curpos = _SysTell(Handle);
337         off_t ret;
338         _SysSeek(Handle, 0, SEEK_END);
339         ret = _SysTell(Handle);
340         _SysSeek(Handle, curpos, SEEK_SET);
341         return ret;
342 }

UCC git Repository :: git.ucc.asn.au