Debian Codegraph @

terps/tads/tads3/vmfilobj.h

raw symbols SCIP
1 /*
2 * Copyright (c) 2001, 2002 Michael J. Roberts. All Rights Reserved.
3 *
4 * Please see the accompanying license file, LICENSE.TXT, for information
5 * on using and copying this software.
6 */
7 /*
8 Name
9 vmfilobj.h - File object metaclass
10 Function
11 Implements an intrinsic class interface to operating system file I/O.
12 Notes
13
14 Modified
15 06/28/01 MJRoberts - Creation
16 */
17
18 #ifndef VMFILOBJ_H
19 #define VMFILOBJ_H
20
21 #include "os.h"
22 #include "utf8.h"
23 #include "vmglob.h"
24 #include "vmobj.h"
25 #include "vmstr.h"
26 #include "vmdatasrc.h"
27
28
29 /* ------------------------------------------------------------------------ */
30 /*
31 * File intrinsic class. Our extension keeps track of our data source (our
32 * abstract file handle), our character set object, and our flags.
33 *
34 * CVmDataSource *fp
35 *. objid charset
36 *. byte mode (text/data/raw)
37 *. byte access (read/write/both)
38 *. uint32 flags
39 *
40 * Our image file data leaves out the file handle, since it can't be
41 * loaded:
42 *
43 * objid charset
44 *. byte mode
45 *. byte access
46 *. uint32 flags
47 *
48 * An open file is inherently transient, so its state cannot be loaded,
49 * restored, or undone.
50 */
51
52 class CVmObjFile: public CVmObject
53 {
54 friend class CVmMetaclassFile;
55
56 public:
57 /*
58 * Special filename designators. These integer values can be passed to
59 * the Open methods (from bytecode) in place of the filename string.
60 * These designate system files whose actual location is determined by
61 * the interpreter.
62 *
63 * The special file mechanism is designed to give games a well-defined
64 * portable way to access global, cross-game files outside of the game
65 * sandbox, while still protecting the local file system against
66 * malicious (or just buggy) games. The actual system path and
67 * filename of a given special file is determined by the interpreter,
68 * so games can't use this to access arbitrary files - only the
69 * designated special files are accessible.
70 *
71 * Note that the integer values defined here must not be changed in
72 * future releases, since they're a published part of the API - i.e.,
73 * bytecode programs hard-code the integer values.
74 */
75
76 /*
77 * Library defaults file. This is defined as a system file, common to
78 * all games, where we can store global cross-game preference settings.
79 * This is generally stored in the interpreter install directory.
80 */
81 static const int32_t SFID_LIB_DEFAULTS = 1;
82
83 /*
84 * Web UI preferences file. This is a system file common to all games
85 * that stores the UI settings for the Web UI client.
86 */
87 static const int32_t SFID_WEBUI_PREFS = 2;
88
89
90 /* metaclass registration object */
91 static class CVmMetaclass *metaclass_reg_;
92 class CVmMetaclass *get_metaclass_reg() const { return metaclass_reg_; }
93
94 /* am I of the given metaclass? */
95 virtual int is_of_metaclass(class CVmMetaclass *meta) const
96 {
97 /* try my own metaclass and my base class */
98 return (meta == metaclass_reg_
99 || CVmObject::is_of_metaclass(meta));
100 }
101
102 /* is 'obj' a File object? */
103 static int is_file_obj(VMG_ vm_obj_id_t obj)
104 { return vm_objp(vmg_ obj)->is_of_metaclass(metaclass_reg_); }
105
106 /* create from stack arguments */
107 static vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr,
108 uint argc);
109
110 /* reserve constant data */
111 virtual void reserve_const_data(VMG_ class CVmConstMapper *,
112 vm_obj_id_t /*self*/)
113 {
114 /* we can't be converted to constant data */
115 }
116
117 /* convert to constant data */
118 virtual void convert_to_const_data(VMG_ class CVmConstMapper *,
119 vm_obj_id_t /*self*/)
120 {
121 /*
122 * we can't be converted to constant data and reference nothing
123 * that can be converted to constant data
124 */
125 }
126
127 /* create an empty file object */
128 static vm_obj_id_t create(VMG_ int in_root_set);
129
130 /* create with the given character set and file handle */
131 static vm_obj_id_t create(VMG_ int in_root_set,
132 class CVmNetFile *netfile,
133 vm_obj_id_t charset, CVmDataSource *fp,
134 int mode, int access, int create_readbuf);
135
136 /* get the data source for the file */
137 class CVmDataSource *get_datasource() const;
138
139 /* get the filespec used to create the file */
140 void get_filespec(vm_val_t *val) const;
141
142 /* notify of deletion */
143 void notify_delete(VMG_ int in_root_set);
144
145 /* set a property */
146 void set_prop(VMG_ class CVmUndo *undo,
147 vm_obj_id_t self, vm_prop_id_t prop, const vm_val_t *val);
148
149 /* call a static property */
150 static int call_stat_prop(VMG_ vm_val_t *result,
151 const uchar **pc_ptr, uint *argc,
152 vm_prop_id_t prop);
153
154 /* undo operations */
155 void notify_new_savept() { }
156 void apply_undo(VMG_ struct CVmUndoRecord *) { }
157
158 /* mark references */
159 void mark_undo_ref(VMG_ struct CVmUndoRecord *) { }
160 void mark_refs(VMG_ uint state);
161
162 /* we keep no weak references */
163 void remove_stale_undo_weak_ref(VMG_ struct CVmUndoRecord *) { }
164 void remove_stale_weak_refs(VMG0_) { }
165
166 /* load from an image file */
167 void load_from_image(VMG_ vm_obj_id_t, const char *ptr, size_t);
168
169 /* reload from an image file (on restart) */
170 void reload_from_image(VMG_ vm_obj_id_t, const char *ptr, size_t);
171
172 /* rebuild for image file */
173 virtual ulong rebuild_image(VMG_ char *buf, ulong buflen);
174
175 /* save to a file */
176 void save_to_file(VMG_ class CVmFile *fp);
177
178 /* restore from a file */
179 void restore_from_file(VMG_ vm_obj_id_t self,
180 class CVmFile *fp, class CVmObjFixup *fixups);
181
182 /* evaluate a property */
183 virtual int get_prop(VMG_ vm_prop_id_t prop, vm_val_t *val,
184 vm_obj_id_t self, vm_obj_id_t *source_obj,
185 uint *argc);
186
187 /*
188 * Translate a filename argument to a netfile object. Checks file
189 * safety against the given access mode.
190 */
191 static CVmNetFile *get_filename_arg(
192 VMG_ const vm_val_t *arg, const vm_rcdesc *rc,
193 int access, int is_resource_file,
194 os_filetype_t file_type, const char *mime_type);
195
196 /*
197 * get the filename from an object argument; this can only be used for
198 * regular files (not resources)
199 */
200 static CVmNetFile *get_filename_from_obj(
201 VMG_ vm_obj_id_t obj, const vm_rcdesc *rc, int access,
202 os_filetype_t file_type, const char *mime_type);
203
204 /* resolve a special file ID to a local path */
205 static int sfid_to_path(VMG_ char *buf, size_t buflen, int32_t sfid);
206
207 /*
208 * Check the safety settings to determine if an open is allowed on the
209 * given file with the given access mode. If the access is not
210 * allowed, we'll throw an error.
211 */
212 static void check_safety_for_open(VMG_ class CVmNetFile *f, int access);
213
214 /* check the safety settings for opening the given local file path */
215 static void check_safety_for_open(VMG_ const char *lclfname, int access);
216 static int query_safety_for_open(VMG_ const char *lclfname, int access);
217
218 /* get the file's size; returns -1 if the file isn't open or valid */
219 long get_file_size(VMG0_);
220
221 /* get the mode - returns a VMOBJFILE_MODE_xxx value */
222 int get_file_mode(VMG0_);
223
224 /* seek; returns true on success, false on failure */
225 int set_pos(VMG_ long pos);
226
227 /*
228 * Read the file, storing the result in the caller's buffer. The
229 * request is a suggested read size; we'll attempt to fill this as
230 * closely as possible, but we might return less than requested (never
231 * more) due to reaching the end of the file or due to character
232 * conversions. Returns true on success, false on failure.
233 */
234 int read_file(VMG_ char *buf, int32_t &len);
235
236 protected:
237 /* create with no initial contents */
238 CVmObjFile() { ext_ = 0; }
239
240 /* create with the given character set and file object */
241 CVmObjFile(VMG_ class CVmNetFile *netfile, vm_obj_id_t charset,
242 CVmDataSource *fp, int mode, int access, int create_readbuf);
243
244 /* allocate our extension */
245 void alloc_ext(VMG_ class CVmNetFile *netfile,
246 vm_obj_id_t charset, CVmDataSource *fp,
247 unsigned long flags, int mode, int access,
248 int create_readbuf);
249
250 /* load or reload data from the image file */
251 void load_image_data(VMG_ const char *ptr, size_t siz);
252
253 /* property evaluator - undefined property */
254 int getp_undef(VMG_ vm_obj_id_t, vm_val_t *, uint *) { return FALSE; }
255
256 /* property evaluator - get my character set */
257 int getp_get_charset(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
258
259 /* property evaluator - set my character set */
260 int getp_set_charset(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
261
262 /* property evaluator - close the file */
263 int getp_close_file(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
264
265 /* property evaluator - read from the file */
266 int getp_read_file(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
267
268 /* property evaluator - write to the file */
269 int getp_write_file(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
270
271 /* property evaluator - read bytes from the file */
272 int getp_read_bytes(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
273
274 /* property evaluator - write bytes to the file */
275 int getp_write_bytes(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
276
277 /* property evaluator - get seek position */
278 int getp_get_pos(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
279
280 /* property evaluator - set seek position */
281 int getp_set_pos(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
282
283 /* get the seek position */
284 long get_pos(VMG0_);
285
286 /* property evaluator - set seek position to end of file */
287 int getp_set_pos_end(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
288
289 /* property evaluator - openFileText */
290 int getp_open_text(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
291
292 /* property evaluator - openFileData */
293 int getp_open_data(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
294
295 /* property evaluator - openFileRaw */
296 int getp_open_raw(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
297
298 /* property evaluator - openResourceText */
299 int getp_open_res_text(VMG_ vm_obj_id_t self, vm_val_t *ret, uint *argc);
300
301 /* property evaluator - openResourceRaw */
302 int getp_open_res_raw(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
303
304 /* property evaluator - get size */
305 int getp_get_size(VMG_ vm_obj_id_t self, vm_val_t *ret, uint *argc);
306
307 /* get the file's mode */
308 int getp_get_mode(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
309
310 /* static property evaluators for the openFileXxx creator methods */
311 static int s_getp_open_text(VMG_ vm_val_t *retval, uint *argc,
312 int is_resource_file,
313 const struct vm_rcdesc *rc);
314 static int s_getp_open_data(VMG_ vm_val_t *retval, uint *argc,
315 const struct vm_rcdesc *rc);
316 static int s_getp_open_raw(VMG_ vm_val_t *retval, uint *argc,
317 int is_resource_file,
318 const struct vm_rcdesc *rc);
319
320 /* property evaluator - getRootName */
321 int getp_getRootName(VMG_ vm_obj_id_t, vm_val_t *retval, uint *argc)
322 { return s_getp_getRootName(vmg_ retval, argc); }
323
324 /* static property evaluator for getRootName */
325 static int s_getp_getRootName(VMG_ vm_val_t *retval, uint *argc);
326
327 /* property evaluator - deleteFile */
328 int getp_deleteFile(VMG_ vm_obj_id_t, vm_val_t *retval, uint *argc)
329 { return s_getp_deleteFile(vmg_ retval, argc); }
330
331 /* static property evaluator for deleteFile */
332 static int s_getp_deleteFile(VMG_ vm_val_t *retval, uint *argc);
333
334 /* property evaluator - setMode */
335 int getp_setMode(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
336
337 /* property evaluator - packBytes */
338 int getp_packBytes(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
339
340 /* property evaluator - unpackBytes */
341 int getp_unpackBytes(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
342
343 /* property evaluator - sha256 */
344 int getp_sha256(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
345
346 /* property evaluator - digestMD5 */
347 int getp_digestMD5(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
348
349 /* retrieve the filename and access arguments for an 'open' method */
350 static class CVmNetFile *get_filename_and_access(
351 VMG_ const struct vm_rcdesc *rc, int *access, int is_resource_file,
352 os_filetype_t file_type, const char *mime_type);
353
354 /* pop a character set mapper argument */
355 static vm_obj_id_t get_charset_arg(VMG_ int argn, int argc);
356
357 /* generic binary file opener - common to 'raw' and 'data' files */
358 static int open_binary(VMG_ vm_val_t *retval, uint *argc, int mode,
359 int is_resource_file, const struct vm_rcdesc *rc);
360
361 /* read a value in text/data mode */
362 void read_text_mode(VMG_ vm_val_t *retval);
363 void read_data_mode(VMG_ vm_val_t *retval);
364
365 /* write a value in text/data mode */
366 void write_text_mode(VMG_ const vm_val_t *val);
367 void write_data_mode(VMG_ const vm_val_t *val);
368
369 /*
370 * check to make sure we can perform operations on the file - if we're
371 * in the 'out of sync' state, we'll throw an exception
372 */
373 void check_valid_file(VMG0_);
374
375 /*
376 * check that we have read or write access to the file - throws a mode
377 * exception if we don't have the requested access mode
378 */
379 void check_read_access(VMG0_);
380 void check_write_access(VMG0_);
381
382 /* check that we're open in raw mode */
383 void check_raw_mode(VMG0_);
384
385 /* check that we're valid, in raw mode, and read/write */
386 void check_raw_read(VMG0_);
387 void check_raw_write(VMG0_);
388
389 /*
390 * Note a file seek position change.
391 *
392 * 'is_explicit' indicates whether the seek is an explicit osfseek()
393 * operation or simply a change in position due to a read or write. If
394 * we're explicitly seeking to a new location with osfseek(),
395 * 'is_explicit' is true; if we're simply reading or writing, which
396 * affects the file position implicitly, 'is_explicit' is false.
397 */
398 void note_file_seek(VMG_ int is_explicit);
399
400 /*
401 * check for a switch between reading and writing, flushing our
402 * underlying stdio buffers if necessary
403 */
404 void switch_read_write_mode(VMG_ int writing);
405
406 /* get my extension, properly cast */
407 struct vmobjfile_ext_t *get_ext() const
408 { return (struct vmobjfile_ext_t *)ext_; }
409
410 /* property evaluation function table */
411 static int (CVmObjFile::*func_table_[])(
412 VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
413 };
414
415 /*
416 * File object extension
417 */
418 struct vmobjfile_ext_t
419 {
420 /*
421 * Our network/local file descriptor. This handles network storage
422 * server operations when we're running in web server mode.
423 */
424 class CVmNetFile *netfile;
425
426 /* our abstract data source (usually an OS file) */
427 CVmDataSource *fp;
428
429 /* our character set object */
430 vm_obj_id_t charset;
431
432 /* format mode */
433 unsigned char mode;
434
435 /* read/write access */
436 unsigned char access;
437
438 /* flags */
439 unsigned long flags;
440
441 /* read buffer, if we have one */
442 struct vmobjfile_readbuf_t *readbuf;
443 };
444
445 /*
446 * File object text read buffer. This is attached to the file extension
447 * if the file is opened in text mode with read access.
448 */
449 struct vmobjfile_readbuf_t
450 {
451 /* current read pointer */
452 char *ptr;
453
454 /* bytes remaining in read buffer */
455 size_t rem;
456
457 /* read buffer */
458 char buf[512];
459
460 /* read a character */
461 int getch(wchar_t &ch, CVmDataSource *fp, class CCharmapToUni *charmap);
462
463 /* peek at the next character */
464 int peekch(wchar_t &ch, CVmDataSource *fp, class CCharmapToUni *charmap,
465 size_t &in_bytes);
466
467 /* commit a 'peekch' */
468 void commit_peek(size_t last_in_bytes)
469 {
470 ptr += last_in_bytes;
471 rem -= last_in_bytes;
472 }
473
474 /* refill the buffer, if it's empty */
475 int refill(CVmDataSource *fp);
476 };
477
478 /*
479 * file extension flags
480 */
481
482 /*
483 * File out of sync. This is set when we load a File object from an image
484 * file; since the file cannot remain open across a preinit-save-load
485 * cycle, no operations can be performed on the File object after it's
486 * loaded from an image file.
487 */
488 #define VMOBJFILE_OUT_OF_SYNC 0x0001
489
490 /*
491 * last operation left the underlying stdio buffers dirty, so we must take
492 * care to seek explicitly in the file if changing read/write mode on the
493 * next operation
494 */
495 #define VMOBJFILE_STDIO_BUF_DIRTY 0x0002
496
497 /*
498 * last operation was 'write' - we must keep track of the last operation
499 * so that we will know to explicitly seek when switching between read and
500 * write operations
501 */
502 #define VMOBJFILE_LAST_OP_WRITE 0x0004
503
504
505 /*
506 * modes
507 */
508 #define VMOBJFILE_MODE_TEXT 0x01
509 #define VMOBJFILE_MODE_DATA 0x02
510 #define VMOBJFILE_MODE_RAW 0x03
511
512 /*
513 * access types
514 */
515 #define VMOBJFILE_ACCESS_READ 0x0001
516 #define VMOBJFILE_ACCESS_WRITE 0x0002
517 #define VMOBJFILE_ACCESS_RW_KEEP 0x0003
518 #define VMOBJFILE_ACCESS_RW_TRUNC 0x0004
519 #define VMOBJFILE_ACCESS_USERMAX 0x0004
520
521 /* internal access types - not allowed as user arguments */
522 #define VMOBJFILE_ACCESS_INTERNAL 0x1000
523 #define VMOBJFILE_ACCESS_DELETE 0x1000 /* delete file */
524 #define VMOBJFILE_ACCESS_GETINFO 0x1001 /* get file type/info */
525 #define VMOBJFILE_ACCESS_MKDIR 0x1002 /* mkdir */
526 #define VMOBJFILE_ACCESS_RMDIR 0x1003 /* rmdir */
527 #define VMOBJFILE_ACCESS_READDIR 0x1004 /* read a directory (list files) */
528 #define VMOBJFILE_ACCESS_RENAME_FROM 0x1005 /* rename FROM this name */
529 #define VMOBJFILE_ACCESS_RENAME_TO 0x1006 /* rename TO this name */
530
531
532 /* ------------------------------------------------------------------------ */
533 /*
534 * 'Data' mode type tags. Each item in a data-mode file is written with
535 * a byte giving the type tag, followed immediately by the data value.
536 *
537 * For compatibility with TADS 2 'binary' mode files, we use the same
538 * type tags that TADS 2 used for types in common. We are upwardly
539 * compatible with the TADS 2 format: we recognize all TADS 2 type tags,
540 * but we provide support for additional types that TADS 2 does not use.
541 * All of the tag values below 0x20 are compatible with TADS 2; those
542 * tag values 0x20 and above are not backwards compatible, so files that
543 * include these values will not be readable by TADS 2 programs.
544 */
545
546 /*
547 * integer - 32-bit int in little-endian format (4 bytes) - TADS 2
548 * compatible
549 */
550 #define VMOBJFILE_TAG_INT 0x01
551
552 /*
553 * string - 16-bit byte-length prefix in little-endian format followed
554 * by the bytes of the string, in UTF-8 format - TADS 2 compatible,
555 * although any string which includes characters outside of the ASCII
556 * set (Unicode code points 0 to 127 inclusive) will not be properly
557 * interpreted by TADS 2 programs.
558 *
559 * For compatibility with TADS 2, the length prefix includes in its byte
560 * count the two bytes of the length prefix itself, so this value is
561 * actually two higher than the number of bytes in the string proper.
562 */
563 #define VMOBJFILE_TAG_STRING 0x03
564
565 /* true - no associated value - TADS 2 compatible */
566 #define VMOBJFILE_TAG_TRUE 0x08
567
568 /* enum - 32-bit enum value in little-endian format */
569 #define VMOBJFILE_TAG_ENUM 0x20
570
571 /*
572 * BigNumber - 16-bit length prefix in little-endian format followed by
573 * the bytes of the BigNumber
574 */
575 #define VMOBJFILE_TAG_BIGNUM 0x21
576
577 /*
578 * ByteArray - 32-bit length prefix in little-endian format followed by
579 * the bytes of the byte array
580 */
581 #define VMOBJFILE_TAG_BYTEARRAY 0x22
582
583
584 /* ------------------------------------------------------------------------ */
585 /*
586 * Registration table object
587 */
588 class CVmMetaclassFile: public CVmMetaclass
589 {
590 public:
591 /* get the global name */
592 const char *get_meta_name() const { return "file/030003"; }
593
594 /* create from image file */
595 void create_for_image_load(VMG_ vm_obj_id_t id)
596 {
597 new (vmg_ id) CVmObjFile();
598 G_obj_table->set_obj_gc_characteristics(id, TRUE, FALSE);
599 }
600
601 /* create from restoring from saved state */
602 void create_for_restore(VMG_ vm_obj_id_t id)
603 {
604 new (vmg_ id) CVmObjFile();
605 G_obj_table->set_obj_gc_characteristics(id, TRUE, FALSE);
606 }
607
608 /* create dynamically using stack arguments */
609 vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, uint argc)
610 { return CVmObjFile::create_from_stack(vmg_ pc_ptr, argc); }
611
612 /* call a static property */
613 int call_stat_prop(VMG_ vm_val_t *result,
614 const uchar **pc_ptr, uint *argc,
615 vm_prop_id_t prop)
616 {
617 return CVmObjFile::call_stat_prop(vmg_ result, pc_ptr, argc, prop);
618 }
619 };
620
621
622 #endif /* VMFILOBJ_H */
623
624 /*
625 * Register the class
626 */
627 VM_REGISTER_METACLASS(CVmObjFile)