| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 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 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
class CVmObjFile: public CVmObject |
| 53 |
{ |
| 54 |
friend class CVmMetaclassFile; |
| 55 |
|
| 56 |
public: |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
static const int32_t SFID_LIB_DEFAULTS = 1; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
static const int32_t SFID_WEBUI_PREFS = 2; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
static class CVmMetaclass *metaclass_reg_; |
| 92 |
class CVmMetaclass *get_metaclass_reg() const { return metaclass_reg_; } |
| 93 |
|
| 94 |
|
| 95 |
virtual int is_of_metaclass(class CVmMetaclass *meta) const |
| 96 |
{ |
| 97 |
|
| 98 |
return (meta == metaclass_reg_ |
| 99 |
|| CVmObject::is_of_metaclass(meta)); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 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 |
|
| 107 |
static vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, |
| 108 |
uint argc); |
| 109 |
|
| 110 |
|
| 111 |
virtual void reserve_const_data(VMG_ class CVmConstMapper *, |
| 112 |
vm_obj_id_t ) |
| 113 |
{ |
| 114 |
|
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
virtual void convert_to_const_data(VMG_ class CVmConstMapper *, |
| 119 |
vm_obj_id_t ) |
| 120 |
{ |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
static vm_obj_id_t create(VMG_ int in_root_set); |
| 129 |
|
| 130 |
|
| 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 |
|
| 137 |
class CVmDataSource *get_datasource() const; |
| 138 |
|
| 139 |
|
| 140 |
void get_filespec(vm_val_t *val) const; |
| 141 |
|
| 142 |
|
| 143 |
void notify_delete(VMG_ int in_root_set); |
| 144 |
|
| 145 |
|
| 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 |
|
| 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 |
|
| 155 |
void notify_new_savept() { } |
| 156 |
void apply_undo(VMG_ struct CVmUndoRecord *) { } |
| 157 |
|
| 158 |
|
| 159 |
void mark_undo_ref(VMG_ struct CVmUndoRecord *) { } |
| 160 |
void mark_refs(VMG_ uint state); |
| 161 |
|
| 162 |
|
| 163 |
void remove_stale_undo_weak_ref(VMG_ struct CVmUndoRecord *) { } |
| 164 |
void remove_stale_weak_refs(VMG0_) { } |
| 165 |
|
| 166 |
|
| 167 |
void load_from_image(VMG_ vm_obj_id_t, const char *ptr, size_t); |
| 168 |
|
| 169 |
|
| 170 |
void reload_from_image(VMG_ vm_obj_id_t, const char *ptr, size_t); |
| 171 |
|
| 172 |
|
| 173 |
virtual ulong rebuild_image(VMG_ char *buf, ulong buflen); |
| 174 |
|
| 175 |
|
| 176 |
void save_to_file(VMG_ class CVmFile *fp); |
| 177 |
|
| 178 |
|
| 179 |
void restore_from_file(VMG_ vm_obj_id_t self, |
| 180 |
class CVmFile *fp, class CVmObjFixup *fixups); |
| 181 |
|
| 182 |
|
| 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 |
|
| 189 |
|
| 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 |
|
| 198 |
|
| 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 |
|
| 205 |
static int sfid_to_path(VMG_ char *buf, size_t buflen, int32_t sfid); |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
static void check_safety_for_open(VMG_ class CVmNetFile *f, int access); |
| 213 |
|
| 214 |
|
| 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 |
|
| 219 |
long get_file_size(VMG0_); |
| 220 |
|
| 221 |
|
| 222 |
int get_file_mode(VMG0_); |
| 223 |
|
| 224 |
|
| 225 |
int set_pos(VMG_ long pos); |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
int read_file(VMG_ char *buf, int32_t &len); |
| 235 |
|
| 236 |
protected: |
| 237 |
|
| 238 |
CVmObjFile() { ext_ = 0; } |
| 239 |
|
| 240 |
|
| 241 |
CVmObjFile(VMG_ class CVmNetFile *netfile, vm_obj_id_t charset, |
| 242 |
CVmDataSource *fp, int mode, int access, int create_readbuf); |
| 243 |
|
| 244 |
|
| 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 |
|
| 251 |
void load_image_data(VMG_ const char *ptr, size_t siz); |
| 252 |
|
| 253 |
|
| 254 |
int getp_undef(VMG_ vm_obj_id_t, vm_val_t *, uint *) { return FALSE; } |
| 255 |
|
| 256 |
|
| 257 |
int getp_get_charset(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc); |
| 258 |
|
| 259 |
|
| 260 |
int getp_set_charset(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc); |
| 261 |
|
| 262 |
|
| 263 |
int getp_close_file(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc); |
| 264 |
|
| 265 |
|
| 266 |
int getp_read_file(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc); |
| 267 |
|
| 268 |
|
| 269 |
int getp_write_file(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc); |
| 270 |
|
| 271 |
|
| 272 |
int getp_read_bytes(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc); |
| 273 |
|
| 274 |
|
| 275 |
int getp_write_bytes(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc); |
| 276 |
|
| 277 |
|
| 278 |
int getp_get_pos(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc); |
| 279 |
|
| 280 |
|
| 281 |
int getp_set_pos(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc); |
| 282 |
|
| 283 |
|
| 284 |
long get_pos(VMG0_); |
| 285 |
|
| 286 |
|
| 287 |
int getp_set_pos_end(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc); |
| 288 |
|
| 289 |
|
| 290 |
int getp_open_text(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc); |
| 291 |
|
| 292 |
|
| 293 |
int getp_open_data(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc); |
| 294 |
|
| 295 |
|
| 296 |
int getp_open_raw(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc); |
| 297 |
|
| 298 |
|
| 299 |
int getp_open_res_text(VMG_ vm_obj_id_t self, vm_val_t *ret, uint *argc); |
| 300 |
|
| 301 |
|
| 302 |
int getp_open_res_raw(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc); |
| 303 |
|
| 304 |
|
| 305 |
int getp_get_size(VMG_ vm_obj_id_t self, vm_val_t *ret, uint *argc); |
| 306 |
|
| 307 |
|
| 308 |
int getp_get_mode(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc); |
| 309 |
|
| 310 |
|
| 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 |
|
| 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 |
|
| 325 |
static int s_getp_getRootName(VMG_ vm_val_t *retval, uint *argc); |
| 326 |
|
| 327 |
|
| 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 |
|
| 332 |
static int s_getp_deleteFile(VMG_ vm_val_t *retval, uint *argc); |
| 333 |
|
| 334 |
|
| 335 |
int getp_setMode(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc); |
| 336 |
|
| 337 |
|
| 338 |
int getp_packBytes(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc); |
| 339 |
|
| 340 |
|
| 341 |
int getp_unpackBytes(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc); |
| 342 |
|
| 343 |
|
| 344 |
int getp_sha256(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc); |
| 345 |
|
| 346 |
|
| 347 |
int getp_digestMD5(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc); |
| 348 |
|
| 349 |
|
| 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 |
|
| 355 |
static vm_obj_id_t get_charset_arg(VMG_ int argn, int argc); |
| 356 |
|
| 357 |
|
| 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 |
|
| 362 |
void read_text_mode(VMG_ vm_val_t *retval); |
| 363 |
void read_data_mode(VMG_ vm_val_t *retval); |
| 364 |
|
| 365 |
|
| 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 |
|
| 371 |
|
| 372 |
|
| 373 |
void check_valid_file(VMG0_); |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
void check_read_access(VMG0_); |
| 380 |
void check_write_access(VMG0_); |
| 381 |
|
| 382 |
|
| 383 |
void check_raw_mode(VMG0_); |
| 384 |
|
| 385 |
|
| 386 |
void check_raw_read(VMG0_); |
| 387 |
void check_raw_write(VMG0_); |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
void note_file_seek(VMG_ int is_explicit); |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
void switch_read_write_mode(VMG_ int writing); |
| 405 |
|
| 406 |
|
| 407 |
struct vmobjfile_ext_t *get_ext() const |
| 408 |
{ return (struct vmobjfile_ext_t *)ext_; } |
| 409 |
|
| 410 |
|
| 411 |
static int (CVmObjFile::*func_table_[])( |
| 412 |
VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc); |
| 413 |
}; |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
struct vmobjfile_ext_t |
| 419 |
{ |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
class CVmNetFile *netfile; |
| 425 |
|
| 426 |
|
| 427 |
CVmDataSource *fp; |
| 428 |
|
| 429 |
|
| 430 |
vm_obj_id_t charset; |
| 431 |
|
| 432 |
|
| 433 |
unsigned char mode; |
| 434 |
|
| 435 |
|
| 436 |
unsigned char access; |
| 437 |
|
| 438 |
|
| 439 |
unsigned long flags; |
| 440 |
|
| 441 |
|
| 442 |
struct vmobjfile_readbuf_t *readbuf; |
| 443 |
}; |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
struct vmobjfile_readbuf_t |
| 450 |
{ |
| 451 |
|
| 452 |
char *ptr; |
| 453 |
|
| 454 |
|
| 455 |
size_t rem; |
| 456 |
|
| 457 |
|
| 458 |
char buf[512]; |
| 459 |
|
| 460 |
|
| 461 |
int getch(wchar_t &ch, CVmDataSource *fp, class CCharmapToUni *charmap); |
| 462 |
|
| 463 |
|
| 464 |
int peekch(wchar_t &ch, CVmDataSource *fp, class CCharmapToUni *charmap, |
| 465 |
size_t &in_bytes); |
| 466 |
|
| 467 |
|
| 468 |
void commit_peek(size_t last_in_bytes) |
| 469 |
{ |
| 470 |
ptr += last_in_bytes; |
| 471 |
rem -= last_in_bytes; |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
int refill(CVmDataSource *fp); |
| 476 |
}; |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
#define VMOBJFILE_OUT_OF_SYNC 0x0001 |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
#define VMOBJFILE_STDIO_BUF_DIRTY 0x0002 |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
#define VMOBJFILE_LAST_OP_WRITE 0x0004 |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
#define VMOBJFILE_MODE_TEXT 0x01 |
| 509 |
#define VMOBJFILE_MODE_DATA 0x02 |
| 510 |
#define VMOBJFILE_MODE_RAW 0x03 |
| 511 |
|
| 512 |
|
| 513 |
|
| 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 |
|
| 522 |
#define VMOBJFILE_ACCESS_INTERNAL 0x1000 |
| 523 |
#define VMOBJFILE_ACCESS_DELETE 0x1000 |
| 524 |
#define VMOBJFILE_ACCESS_GETINFO 0x1001 |
| 525 |
#define VMOBJFILE_ACCESS_MKDIR 0x1002 |
| 526 |
#define VMOBJFILE_ACCESS_RMDIR 0x1003 |
| 527 |
#define VMOBJFILE_ACCESS_READDIR 0x1004 |
| 528 |
#define VMOBJFILE_ACCESS_RENAME_FROM 0x1005 |
| 529 |
#define VMOBJFILE_ACCESS_RENAME_TO 0x1006 |
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
#define VMOBJFILE_TAG_INT 0x01 |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
#define VMOBJFILE_TAG_STRING 0x03 |
| 564 |
|
| 565 |
|
| 566 |
#define VMOBJFILE_TAG_TRUE 0x08 |
| 567 |
|
| 568 |
|
| 569 |
#define VMOBJFILE_TAG_ENUM 0x20 |
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
#define VMOBJFILE_TAG_BIGNUM 0x21 |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
#define VMOBJFILE_TAG_BYTEARRAY 0x22 |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
class CVmMetaclassFile: public CVmMetaclass |
| 589 |
{ |
| 590 |
public: |
| 591 |
|
| 592 |
const char *get_meta_name() const { return "file/030003"; } |
| 593 |
|
| 594 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
VM_REGISTER_METACLASS(CVmObjFile) |