| 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 | #include <assert.h> |
| 19 | #include "t3std.h" |
| 20 | #include "charmap.h" |
| 21 | #include "vmerr.h" |
| 22 | #include "vmerrnum.h" |
| 23 | #include "vmtype.h" |
| 24 | #include "vmbif.h" |
| 25 | #include "vmcset.h" |
| 26 | #include "vmstack.h" |
| 27 | #include "vmmeta.h" |
| 28 | #include "vmrun.h" |
| 29 | #include "vmglob.h" |
| 30 | #include "vmfile.h" |
| 31 | #include "vmobj.h" |
| 32 | #include "vmstr.h" |
| 33 | #include "vmlst.h" |
| 34 | #include "vmdate.h" |
| 35 | #include "vmfilobj.h" |
| 36 | #include "vmpredef.h" |
| 37 | #include "vmundo.h" |
| 38 | #include "vmbytarr.h" |
| 39 | #include "vmbignum.h" |
| 40 | #include "vmbiftad.h" |
| 41 | #include "vmhost.h" |
| 42 | #include "vmimage.h" |
| 43 | #include "vmnetfil.h" |
| 44 | #include "vmnet.h" |
| 45 | #include "vmfilnam.h" |
| 46 | #include "vmhash.h" |
| 47 | #include "vmpack.h" |
| 48 | #include "sha2.h" |
| 49 | #include "md5.h" |
| 50 | |
| 51 | |
| 52 | |
| 53 | /* ------------------------------------------------------------------------ */ |
| 54 | /* |
| 55 | * statics |
| 56 | */ |
| 57 | |
| 58 | /* metaclass registration object */ |
| 59 | static CVmMetaclassFile metaclass_reg_obj; |
| 60 | CVmMetaclass *CVmObjFile::metaclass_reg_ = &metaclass_reg_obj; |
| 61 | |
| 62 | /* function table */ |
| 63 | int (CVmObjFile:: |
| 64 | *CVmObjFile::func_table_[])(VMG_ vm_obj_id_t self, |
| 65 | vm_val_t *retval, uint *argc) = |
| 66 | { |
| 67 | &CVmObjFile::getp_undef, /* index 0 */ |
| 68 | &CVmObjFile::getp_open_text, /* 1 */ |
| 69 | &CVmObjFile::getp_open_data, /* 2 */ |
| 70 | &CVmObjFile::getp_open_raw, /* 3 */ |
| 71 | &CVmObjFile::getp_get_charset, /* 4 */ |
| 72 | &CVmObjFile::getp_set_charset, /* 5 */ |
| 73 | &CVmObjFile::getp_close_file, /* 6 */ |
| 74 | &CVmObjFile::getp_read_file, /* 7 */ |
| 75 | &CVmObjFile::getp_write_file, /* 8 */ |
| 76 | &CVmObjFile::getp_read_bytes, /* 9 */ |
| 77 | &CVmObjFile::getp_write_bytes, /* 10 */ |
| 78 | &CVmObjFile::getp_get_pos, /* 11 */ |
| 79 | &CVmObjFile::getp_set_pos, /* 12 */ |
| 80 | &CVmObjFile::getp_set_pos_end, /* 13 */ |
| 81 | &CVmObjFile::getp_open_res_text, /* 14 */ |
| 82 | &CVmObjFile::getp_open_res_raw, /* 15 */ |
| 83 | &CVmObjFile::getp_get_size, /* 16 */ |
| 84 | &CVmObjFile::getp_get_mode, /* 17 */ |
| 85 | &CVmObjFile::getp_getRootName, /* 18 */ |
| 86 | &CVmObjFile::getp_deleteFile, /* 19 */ |
| 87 | &CVmObjFile::getp_setMode, /* 20 */ |
| 88 | &CVmObjFile::getp_packBytes, /* 21 */ |
| 89 | &CVmObjFile::getp_unpackBytes, /* 22 */ |
| 90 | &CVmObjFile::getp_sha256, /* 23 */ |
| 91 | &CVmObjFile::getp_digestMD5 /* 24 */ |
| 92 | }; |
| 93 | |
| 94 | /* |
| 95 | * Vector indices - we only need to define these for the static functions, |
| 96 | * since we only need them to decode calls to call_stat_prop(). |
| 97 | */ |
| 98 | enum vmobjfil_meta_fnset |
| 99 | { |
| 100 | VMOBJFILE_OPEN_TEXT = 1, |
| 101 | VMOBJFILE_OPEN_DATA = 2, |
| 102 | VMOBJFILE_OPEN_RAW = 3, |
| 103 | VMOBJFILE_OPEN_RES_TEXT = 14, |
| 104 | VMOBJFILE_OPEN_RES_RAW = 15, |
| 105 | VMOBJFILE_GET_ROOT_NAME = 18, |
| 106 | VMOBJFILE_DELETE_FILE = 19, |
| 107 | }; |
| 108 | |
| 109 | /* ------------------------------------------------------------------------ */ |
| 110 | /* |
| 111 | * Create from stack |
| 112 | */ |
| 113 | vm_obj_id_t CVmObjFile::create_from_stack(VMG_ const uchar **pc_ptr, |
| 114 | uint argc) |
| 115 | { |
| 116 | /* |
| 117 | * we can't be created with 'new' - we can only be created via our |
| 118 | * static creator methods (openTextFile, openDataFile, openRawFile) |
| 119 | */ |
| 120 | err_throw(VMERR_BAD_DYNAMIC_NEW); |
| 121 | |
| 122 | /* not reached, but the compiler might not know that */ |
| 123 | AFTER_ERR_THROW(return VM_INVALID_OBJ;) |
| 124 | } |
| 125 | |
| 126 | /* ------------------------------------------------------------------------ */ |
| 127 | /* |
| 128 | * Create with no contents |
| 129 | */ |
| 130 | vm_obj_id_t CVmObjFile::create(VMG_ int in_root_set) |
| 131 | { |
| 132 | /* instantiate the object */ |
| 133 | vm_obj_id_t id = vm_new_id(vmg_ in_root_set, TRUE, FALSE); |
| 134 | new (vmg_ id) CVmObjFile(); |
| 135 | |
| 136 | /* files are always transient */ |
| 137 | G_obj_table->set_obj_transient(id); |
| 138 | |
| 139 | /* return the new ID */ |
| 140 | return id; |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * Create with the given character set object and file handle. |
| 145 | */ |
| 146 | vm_obj_id_t CVmObjFile::create(VMG_ int in_root_set, |
| 147 | CVmNetFile *netfile, |
| 148 | vm_obj_id_t charset, CVmDataSource *fp, |
| 149 | int mode, int access, int create_readbuf) |
| 150 | { |
| 151 | /* instantiate the object */ |
| 152 | vm_obj_id_t id = vm_new_id(vmg_ in_root_set, TRUE, FALSE); |
| 153 | new (vmg_ id) CVmObjFile( |
| 154 | vmg_ netfile, charset, fp, mode, access, create_readbuf); |
| 155 | |
| 156 | /* files are always transient */ |
| 157 | G_obj_table->set_obj_transient(id); |
| 158 | |
| 159 | /* return the ID */ |
| 160 | return id; |
| 161 | } |
| 162 | |
| 163 | /* ------------------------------------------------------------------------ */ |
| 164 | /* |
| 165 | * Instantiate |
| 166 | */ |
| 167 | CVmObjFile::CVmObjFile(VMG_ CVmNetFile *netfile, |
| 168 | vm_obj_id_t charset, CVmDataSource *fp, |
| 169 | int mode, int access, int create_readbuf) |
| 170 | { |
| 171 | /* allocate and initialize our extension */ |
| 172 | ext_ = 0; |
| 173 | alloc_ext(vmg_ netfile, charset, fp, 0, mode, access, create_readbuf); |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | * Allocate and initialize our extension |
| 178 | */ |
| 179 | void CVmObjFile::alloc_ext(VMG_ CVmNetFile *netfile, |
| 180 | vm_obj_id_t charset, CVmDataSource *fp, |
| 181 | unsigned long flags, int mode, int access, |
| 182 | int create_readbuf) |
| 183 | { |
| 184 | /* |
| 185 | * if we already have an extension, delete it (and release our |
| 186 | * underlying system file, if any) |
| 187 | */ |
| 188 | notify_delete(vmg_ FALSE); |
| 189 | |
| 190 | /* |
| 191 | * Figure the needed size. We need at least the standard extension; |
| 192 | * if we also need a read buffer, figure in space for that as well. |
| 193 | */ |
| 194 | size_t siz = sizeof(vmobjfile_ext_t); |
| 195 | if (create_readbuf) |
| 196 | siz += sizeof(vmobjfile_readbuf_t); |
| 197 | |
| 198 | /* allocate space for our extension structure */ |
| 199 | ext_ = (char *)G_mem->get_var_heap()->alloc_mem(siz, this); |
| 200 | |
| 201 | /* store the data we received from the caller */ |
| 202 | get_ext()->netfile = netfile; |
| 203 | get_ext()->fp = fp; |
| 204 | get_ext()->charset = charset; |
| 205 | get_ext()->flags = flags; |
| 206 | get_ext()->mode = (unsigned char)mode; |
| 207 | get_ext()->access = (unsigned char)access; |
| 208 | |
| 209 | /* |
| 210 | * point to our read buffer, for which we allocated space contiguously |
| 211 | * with and immediately following our extension, if we have one |
| 212 | */ |
| 213 | if (create_readbuf) |
| 214 | { |
| 215 | /* point to our read buffer object */ |
| 216 | vmobjfile_readbuf_t *rb = (vmobjfile_readbuf_t *)(get_ext() + 1); |
| 217 | get_ext()->readbuf = rb; |
| 218 | |
| 219 | /* initialize the read buffer with no initial data */ |
| 220 | rb->rem = 0; |
| 221 | rb->ptr = rb->buf; |
| 222 | } |
| 223 | else |
| 224 | { |
| 225 | /* there's no read buffer at all */ |
| 226 | get_ext()->readbuf = 0; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | /* ------------------------------------------------------------------------ */ |
| 231 | /* |
| 232 | * Notify of deletion |
| 233 | */ |
| 234 | void CVmObjFile::notify_delete(VMG_ int /*in_root_set*/) |
| 235 | { |
| 236 | /* if we have an extension, clean it up */ |
| 237 | if (ext_ != 0) |
| 238 | { |
| 239 | /* close our file if we have one */ |
| 240 | if (get_ext()->fp != 0) |
| 241 | delete get_ext()->fp; |
| 242 | |
| 243 | /* close the network file */ |
| 244 | err_try |
| 245 | { |
| 246 | if (get_ext()->netfile != 0) |
| 247 | get_ext()->netfile->close(vmg0_); |
| 248 | } |
| 249 | err_catch_disc |
| 250 | { |
| 251 | /* |
| 252 | * Since we're being deleted by the garbage collector, there's |
| 253 | * no way to relay this error to the bytecode program - it |
| 254 | * evidently lost track of the file, so it must not care about |
| 255 | * the ending disposition. Just discard the error. |
| 256 | */ |
| 257 | } |
| 258 | err_end; |
| 259 | |
| 260 | /* free our extension */ |
| 261 | G_mem->get_var_heap()->free_mem(ext_); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | /* ------------------------------------------------------------------------ */ |
| 266 | /* |
| 267 | * Get the data source |
| 268 | */ |
| 269 | CVmDataSource *CVmObjFile::get_datasource() const |
| 270 | { |
| 271 | return get_ext()->fp; |
| 272 | } |
| 273 | |
| 274 | /* |
| 275 | * Get the file spec from the netfile |
| 276 | */ |
| 277 | void CVmObjFile::get_filespec(vm_val_t *val) const |
| 278 | { |
| 279 | /* presume we won't find a value */ |
| 280 | val->set_nil(); |
| 281 | |
| 282 | /* if we have a net file object, get its filespec */ |
| 283 | if (get_ext()->netfile != 0) |
| 284 | val->set_obj(get_ext()->netfile->filespec); |
| 285 | } |
| 286 | |
| 287 | /* ------------------------------------------------------------------------ */ |
| 288 | /* |
| 289 | * set a property |
| 290 | */ |
| 291 | void CVmObjFile::set_prop(VMG_ class CVmUndo *, |
| 292 | vm_obj_id_t, vm_prop_id_t, |
| 293 | const vm_val_t *) |
| 294 | { |
| 295 | err_throw(VMERR_INVALID_SETPROP); |
| 296 | } |
| 297 | |
| 298 | /* ------------------------------------------------------------------------ */ |
| 299 | /* |
| 300 | * get a property |
| 301 | */ |
| 302 | int CVmObjFile::get_prop(VMG_ vm_prop_id_t prop, vm_val_t *retval, |
| 303 | vm_obj_id_t self, vm_obj_id_t *source_obj, |
| 304 | uint *argc) |
| 305 | { |
| 306 | uint func_idx; |
| 307 | |
| 308 | /* translate the property index to an index into our function table */ |
| 309 | func_idx = G_meta_table |
| 310 | ->prop_to_vector_idx(metaclass_reg_->get_reg_idx(), prop); |
| 311 | |
| 312 | /* call the appropriate function */ |
| 313 | if ((this->*func_table_[func_idx])(vmg_ self, retval, argc)) |
| 314 | { |
| 315 | *source_obj = metaclass_reg_->get_class_obj(vmg0_); |
| 316 | return TRUE; |
| 317 | } |
| 318 | |
| 319 | /* inherit default handling */ |
| 320 | return CVmObject::get_prop(vmg_ prop, retval, self, source_obj, argc); |
| 321 | } |
| 322 | |
| 323 | /* |
| 324 | * call a static property |
| 325 | */ |
| 326 | int CVmObjFile::call_stat_prop(VMG_ vm_val_t *result, |
| 327 | const uchar **pc_ptr, uint *argc, |
| 328 | vm_prop_id_t prop) |
| 329 | { |
| 330 | /* look up the function */ |
| 331 | uint midx = G_meta_table |
| 332 | ->prop_to_vector_idx(metaclass_reg_->get_reg_idx(), prop); |
| 333 | |
| 334 | /* |
| 335 | * set up a blank recursive call descriptor, to be filled in when we |
| 336 | * know which function we're calling |
| 337 | */ |
| 338 | vm_rcdesc rc(vmg_ "", |
| 339 | CVmObjFile::metaclass_reg_->get_class_obj(vmg0_), |
| 340 | (ushort)midx, G_stk->get(0), argc != 0 ? *argc : 0); |
| 341 | |
| 342 | /* translate the property into a function vector index */ |
| 343 | switch(midx) |
| 344 | { |
| 345 | case VMOBJFILE_OPEN_TEXT: |
| 346 | rc.name = "File.openTextFile"; |
| 347 | return s_getp_open_text(vmg_ result, argc, FALSE, &rc); |
| 348 | |
| 349 | case VMOBJFILE_OPEN_DATA: |
| 350 | rc.name = "File.openDataFile"; |
| 351 | return s_getp_open_data(vmg_ result, argc, &rc); |
| 352 | |
| 353 | case VMOBJFILE_OPEN_RAW: |
| 354 | rc.name = "File.openRawFile"; |
| 355 | return s_getp_open_raw(vmg_ result, argc, FALSE, &rc); |
| 356 | |
| 357 | case VMOBJFILE_OPEN_RES_TEXT: |
| 358 | rc.name = "File.openTextResource"; |
| 359 | return s_getp_open_text(vmg_ result, argc, TRUE, &rc); |
| 360 | |
| 361 | case VMOBJFILE_OPEN_RES_RAW: |
| 362 | rc.name = "File.openRawResource"; |
| 363 | return s_getp_open_raw(vmg_ result, argc, TRUE, &rc); |
| 364 | |
| 365 | case VMOBJFILE_GET_ROOT_NAME: |
| 366 | return s_getp_getRootName(vmg_ result, argc); |
| 367 | |
| 368 | case VMOBJFILE_DELETE_FILE: |
| 369 | return s_getp_deleteFile(vmg_ result, argc); |
| 370 | |
| 371 | default: |
| 372 | /* it's not one of ours - inherit from the base object metaclass */ |
| 373 | return CVmObject::call_stat_prop(vmg_ result, pc_ptr, argc, prop); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | /* ------------------------------------------------------------------------ */ |
| 378 | /* |
| 379 | * Static "open" methods |
| 380 | */ |
| 381 | |
| 382 | int CVmObjFile::getp_open_text(VMG_ vm_obj_id_t self, |
| 383 | vm_val_t *retval, uint *argc) |
| 384 | { |
| 385 | vm_rcdesc rc(vmg_ "File.openTextFile", |
| 386 | CVmObjFile::metaclass_reg_->get_class_obj(vmg0_), |
| 387 | VMOBJFILE_OPEN_TEXT, G_stk->get(0), argc); |
| 388 | return s_getp_open_text(vmg_ retval, argc, FALSE, &rc); |
| 389 | } |
| 390 | |
| 391 | int CVmObjFile::getp_open_data(VMG_ vm_obj_id_t self, |
| 392 | vm_val_t *retval, uint *argc) |
| 393 | { |
| 394 | vm_rcdesc rc(vmg_ "File.openDataFile", |
| 395 | CVmObjFile::metaclass_reg_->get_class_obj(vmg0_), |
| 396 | VMOBJFILE_OPEN_DATA, G_stk->get(0), argc); |
| 397 | return s_getp_open_data(vmg_ retval, argc, &rc); |
| 398 | } |
| 399 | |
| 400 | int CVmObjFile::getp_open_raw(VMG_ vm_obj_id_t self, |
| 401 | vm_val_t *retval, uint *argc) |
| 402 | { |
| 403 | vm_rcdesc rc(vmg_ "File.openRawFile", |
| 404 | CVmObjFile::metaclass_reg_->get_class_obj(vmg0_), |
| 405 | VMOBJFILE_OPEN_RAW, G_stk->get(0), argc); |
| 406 | return s_getp_open_raw(vmg_ retval, argc, FALSE, &rc); |
| 407 | } |
| 408 | |
| 409 | int CVmObjFile::getp_open_res_text(VMG_ vm_obj_id_t self, |
| 410 | vm_val_t *retval, uint *argc) |
| 411 | { |
| 412 | vm_rcdesc rc(vmg_ "File.openTextResource", |
| 413 | CVmObjFile::metaclass_reg_->get_class_obj(vmg0_), |
| 414 | VMOBJFILE_OPEN_RES_TEXT, G_stk->get(0), argc); |
| 415 | return s_getp_open_text(vmg_ retval, argc, TRUE, &rc); |
| 416 | } |
| 417 | |
| 418 | int CVmObjFile::getp_open_res_raw(VMG_ vm_obj_id_t self, |
| 419 | vm_val_t *retval, uint *argc) |
| 420 | { |
| 421 | vm_rcdesc rc(vmg_ "File.openRawResource", |
| 422 | CVmObjFile::metaclass_reg_->get_class_obj(vmg0_), |
| 423 | VMOBJFILE_OPEN_RES_RAW, G_stk->get(0), argc); |
| 424 | return s_getp_open_raw(vmg_ retval, argc, TRUE, &rc); |
| 425 | } |
| 426 | |
| 427 | |
| 428 | |
| 429 | /* ------------------------------------------------------------------------ */ |
| 430 | /* |
| 431 | * load from an image file |
| 432 | */ |
| 433 | void CVmObjFile::load_from_image(VMG_ vm_obj_id_t self, |
| 434 | const char *ptr, size_t siz) |
| 435 | { |
| 436 | /* load from the image data */ |
| 437 | load_image_data(vmg_ ptr, siz); |
| 438 | |
| 439 | /* |
| 440 | * save our image data pointer in the object table, so that we can |
| 441 | * access it (without storing it ourselves) during a reload |
| 442 | */ |
| 443 | G_obj_table->save_image_pointer(self, ptr, siz); |
| 444 | } |
| 445 | |
| 446 | /* |
| 447 | * reload the object from image data |
| 448 | */ |
| 449 | void CVmObjFile::reload_from_image(VMG_ vm_obj_id_t /*self*/, |
| 450 | const char *ptr, size_t siz) |
| 451 | { |
| 452 | /* load the image data */ |
| 453 | load_image_data(vmg_ ptr, siz); |
| 454 | } |
| 455 | |
| 456 | /* |
| 457 | * load or re-load image data |
| 458 | */ |
| 459 | void CVmObjFile::load_image_data(VMG_ const char *ptr, size_t siz) |
| 460 | { |
| 461 | /* read our character set */ |
| 462 | vm_obj_id_t charset = vmb_get_objid(ptr); |
| 463 | ptr += VMB_OBJECT_ID; |
| 464 | |
| 465 | /* get the mode and access values */ |
| 466 | int mode = (unsigned char)*ptr++; |
| 467 | int access = (unsigned char)*ptr++; |
| 468 | |
| 469 | /* get the flags */ |
| 470 | unsigned long flags = t3rp4u(ptr); |
| 471 | |
| 472 | /* |
| 473 | * add in the out-of-sync flag, since we've restored the state from a |
| 474 | * past state and thus we're out of sync with the external file system |
| 475 | * environment |
| 476 | */ |
| 477 | flags |= VMOBJFILE_OUT_OF_SYNC; |
| 478 | |
| 479 | /* |
| 480 | * Initialize our extension - we have no underlying network file |
| 481 | * descriptor or native file handle, since the file is out of sync by |
| 482 | * virtue of being loaded from a previously saved image state. Note |
| 483 | * that we don't need a read buffer because the file is inherently out |
| 484 | * of sync and thus cannot be read. |
| 485 | */ |
| 486 | alloc_ext(vmg_ 0, charset, 0, flags, mode, access, FALSE); |
| 487 | } |
| 488 | |
| 489 | /* ------------------------------------------------------------------------ */ |
| 490 | /* |
| 491 | * save to a file |
| 492 | */ |
| 493 | void CVmObjFile::save_to_file(VMG_ class CVmFile *fp) |
| 494 | { |
| 495 | /* files are always transient, so should never be saved */ |
| 496 | assert(FALSE); |
| 497 | } |
| 498 | |
| 499 | /* |
| 500 | * restore from a file |
| 501 | */ |
| 502 | void CVmObjFile::restore_from_file(VMG_ vm_obj_id_t self, |
| 503 | CVmFile *fp, CVmObjFixup *) |
| 504 | { |
| 505 | /* files are always transient, so should never be saved */ |
| 506 | } |
| 507 | |
| 508 | /* ------------------------------------------------------------------------ */ |
| 509 | /* |
| 510 | * Mark as referenced all of the objects to which we refer |
| 511 | */ |
| 512 | void CVmObjFile::mark_refs(VMG_ uint state) |
| 513 | { |
| 514 | /* mark our character set object, if we have one */ |
| 515 | if (get_ext()->charset != VM_INVALID_OBJ) |
| 516 | G_obj_table->mark_all_refs(get_ext()->charset, state); |
| 517 | |
| 518 | /* mark our network file descriptor's references */ |
| 519 | if (get_ext()->netfile != 0) |
| 520 | get_ext()->netfile->mark_refs(vmg_ state); |
| 521 | } |
| 522 | |
| 523 | /* ------------------------------------------------------------------------ */ |
| 524 | /* |
| 525 | * Note that we're seeking within the file. |
| 526 | */ |
| 527 | void CVmObjFile::note_file_seek(VMG_ int is_explicit) |
| 528 | { |
| 529 | /* |
| 530 | * if it's an explicit seek, invalidate our internal read buffer and |
| 531 | * note that the stdio buffers have been invalidated |
| 532 | */ |
| 533 | if (is_explicit) |
| 534 | { |
| 535 | /* the read buffer (if we have one) is invalid after a seek */ |
| 536 | if (get_ext()->readbuf != 0) |
| 537 | get_ext()->readbuf->rem = 0; |
| 538 | |
| 539 | /* |
| 540 | * mark the last operation as clearing stdio buffering - when we |
| 541 | * explicitly seek, stdio automatically invalidates its internal |
| 542 | * buffers |
| 543 | */ |
| 544 | get_ext()->flags &= ~VMOBJFILE_STDIO_BUF_DIRTY; |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | /* |
| 549 | * Update stdio buffering for a switch between read and write mode, if |
| 550 | * necessary. Any time we perform consecutive read and write operations, |
| 551 | * stdio requires us to explicitly seek when performing consecutive |
| 552 | * dissimilar operations. In other words, if we read then write, we must |
| 553 | * seek before the write, and likewise if we write then read. |
| 554 | */ |
| 555 | void CVmObjFile::switch_read_write_mode(VMG_ int writing) |
| 556 | { |
| 557 | /* if we're writing, invalidate the read buffer */ |
| 558 | if (writing && get_ext()->readbuf != 0) |
| 559 | get_ext()->readbuf->rem = 0; |
| 560 | |
| 561 | /* |
| 562 | * if we just performed a read or write operation, we must seek if |
| 563 | * we're performing the opposite type of operation now |
| 564 | */ |
| 565 | if ((get_ext()->flags & VMOBJFILE_STDIO_BUF_DIRTY) != 0) |
| 566 | { |
| 567 | int was_writing; |
| 568 | |
| 569 | /* check what type of operation we did last */ |
| 570 | was_writing = ((get_ext()->flags & VMOBJFILE_LAST_OP_WRITE) != 0); |
| 571 | |
| 572 | /* |
| 573 | * if we're switching operations, explicitly seek to the current |
| 574 | * location to flush the stdio buffers |
| 575 | */ |
| 576 | if ((writing && !was_writing) || (!writing && was_writing)) |
| 577 | get_ext()->fp->seek(get_pos(vmg0_), OSFSK_SET); |
| 578 | } |
| 579 | |
| 580 | /* |
| 581 | * remember that this operation is stdio-buffered, so that we'll know |
| 582 | * we need to seek if we perform the opposite operation after this one |
| 583 | */ |
| 584 | get_ext()->flags |= VMOBJFILE_STDIO_BUF_DIRTY; |
| 585 | |
| 586 | /* remember which type of operation we're performing */ |
| 587 | if (writing) |
| 588 | get_ext()->flags |= VMOBJFILE_LAST_OP_WRITE; |
| 589 | else |
| 590 | get_ext()->flags &= ~VMOBJFILE_LAST_OP_WRITE; |
| 591 | } |
| 592 | |
| 593 | /* ------------------------------------------------------------------------ */ |
| 594 | /* |
| 595 | * Retrieve the filename argument, and optionally the access mode argument. |
| 596 | * Sets up network storage server access to the file, if applicable. |
| 597 | * Returns a network/local file descriptor that the caller can use to open |
| 598 | * the local file system object. |
| 599 | * |
| 600 | * If '*access' is zero at entry, we'll retrieve the access argument from |
| 601 | * the stack for a non-resource file. (Resource files always use access |
| 602 | * mode READ, so there's no extra argument for those.) If '*access' is |
| 603 | * non-zero, we'll simply use the given access mode. |
| 604 | * |
| 605 | * This leaves the argument values on the stack - the caller must discard |
| 606 | * when done with them. |
| 607 | */ |
| 608 | CVmNetFile *CVmObjFile::get_filename_and_access( |
| 609 | VMG_ const vm_rcdesc *rc, int *access, int is_resource_file, |
| 610 | os_filetype_t file_type, const char *mime_type) |
| 611 | { |
| 612 | /* |
| 613 | * If it's a resource file, the access mode is always READ, and there's |
| 614 | * no access mode argument. If the caller provided a non-zero access |
| 615 | * mode, there's also no argument - we just use the caller's mode. If |
| 616 | * the caller specified zero as the mode, it means we have to read the |
| 617 | * mode from an extra integer argument. |
| 618 | */ |
| 619 | if (is_resource_file) |
| 620 | { |
| 621 | /* resource file - the only access mode possible is READ */ |
| 622 | *access = VMOBJFILE_ACCESS_READ; |
| 623 | } |
| 624 | else if (*access == 0) |
| 625 | { |
| 626 | /* get the access mode from the stack */ |
| 627 | *access = G_stk->get(1)->num_to_int(vmg0_); |
| 628 | |
| 629 | /* make sure it's within the valid range for bytecode access codes */ |
| 630 | if (*access > VMOBJFILE_ACCESS_USERMAX) |
| 631 | err_throw(VMERR_BAD_VAL_BIF); |
| 632 | } |
| 633 | |
| 634 | /* get the filename from the first argument */ |
| 635 | return get_filename_arg( |
| 636 | vmg_ G_stk->get(0), rc, *access, is_resource_file, |
| 637 | file_type, mime_type); |
| 638 | } |
| 639 | |
| 640 | /* |
| 641 | * Get the filename for a given argument value and access mode |
| 642 | */ |
| 643 | CVmNetFile *CVmObjFile::get_filename_from_obj( |
| 644 | VMG_ vm_obj_id_t obj, const vm_rcdesc *rc, int access, |
| 645 | os_filetype_t file_type, const char *mime_type) |
| 646 | { |
| 647 | vm_val_t arg; |
| 648 | arg.set_obj(obj); |
| 649 | return get_filename_arg(vmg_ &arg, rc, access, |
| 650 | FALSE, file_type, mime_type); |
| 651 | } |
| 652 | |
| 653 | /* |
| 654 | * Get the filename for a given argument value and access mode |
| 655 | */ |
| 656 | CVmNetFile *CVmObjFile::get_filename_arg( |
| 657 | VMG_ const vm_val_t *arg, const vm_rcdesc *rc, |
| 658 | int access, int is_resource_file, |
| 659 | os_filetype_t file_type, const char *mime_type) |
| 660 | { |
| 661 | /* figure the network file access mode */ |
| 662 | int nmode; |
| 663 | switch (access) |
| 664 | { |
| 665 | case VMOBJFILE_ACCESS_READ: |
| 666 | /* read access; file must exist */ |
| 667 | nmode = NETF_READ; |
| 668 | break; |
| 669 | |
| 670 | case VMOBJFILE_ACCESS_WRITE: |
| 671 | /* write access; create or replace existing file */ |
| 672 | nmode = NETF_WRITE | NETF_CREATE | NETF_TRUNC; |
| 673 | break; |
| 674 | |
| 675 | case VMOBJFILE_ACCESS_RW_KEEP: |
| 676 | /* read/write access; keep an existing file or create a new one */ |
| 677 | nmode = NETF_READ | NETF_WRITE | NETF_CREATE; |
| 678 | break; |
| 679 | |
| 680 | case VMOBJFILE_ACCESS_RW_TRUNC: |
| 681 | /* read/write access; truncate an existing file or create a new one */ |
| 682 | nmode = NETF_READ | NETF_WRITE | NETF_CREATE | NETF_TRUNC; |
| 683 | break; |
| 684 | |
| 685 | case VMOBJFILE_ACCESS_DELETE: |
| 686 | /* delete mode */ |
| 687 | nmode = NETF_DELETE; |
| 688 | break; |
| 689 | |
| 690 | case VMOBJFILE_ACCESS_GETINFO: |
| 691 | case VMOBJFILE_ACCESS_MKDIR: |
| 692 | case VMOBJFILE_ACCESS_RMDIR: |
| 693 | case VMOBJFILE_ACCESS_READDIR: |
| 694 | case VMOBJFILE_ACCESS_RENAME_TO: |
| 695 | case VMOBJFILE_ACCESS_RENAME_FROM: |
| 696 | /* |
| 697 | * getFileType/getFileInfo/rename/directory manipulation - we don't |
| 698 | * need to open the file at all for these operations |
| 699 | */ |
| 700 | nmode = 0; |
| 701 | break; |
| 702 | |
| 703 | default: |
| 704 | /* invalid mode */ |
| 705 | err_throw(VMERR_BAD_VAL_BIF); |
| 706 | } |
| 707 | |
| 708 | /* we don't have a network file descriptor yet */ |
| 709 | CVmNetFile *netfile = 0; |
| 710 | char fname[OSFNMAX]; |
| 711 | |
| 712 | /* check to see what kind of file spec we have */ |
| 713 | if (is_resource_file) |
| 714 | { |
| 715 | /* a resource file must be given as a string name - retrieve it */ |
| 716 | CVmBif::get_str_val_fname(vmg_ fname, sizeof(fname), |
| 717 | CVmBif::get_str_val(vmg_ arg)); |
| 718 | |
| 719 | /* resources are always local, so create a local file descriptor */ |
| 720 | netfile = CVmNetFile::open_local(vmg_ fname, 0, nmode, file_type); |
| 721 | } |
| 722 | else |
| 723 | { |
| 724 | /* regular file - create the network file descriptor */ |
| 725 | netfile = CVmNetFile::open( |
| 726 | vmg_ arg, rc, nmode, file_type, mime_type); |
| 727 | } |
| 728 | |
| 729 | /* |
| 730 | * If this isn't a resource file, check the file safety mode to see if |
| 731 | * the operation is allowed. Reading resources is always allowed, |
| 732 | * regardless of the safety mode, since resources are read-only and are |
| 733 | * inherently constrained in the paths they can access. |
| 734 | */ |
| 735 | if (!is_resource_file) |
| 736 | { |
| 737 | err_try |
| 738 | { |
| 739 | check_safety_for_open(vmg_ netfile, access); |
| 740 | } |
| 741 | err_catch_disc |
| 742 | { |
| 743 | /* abandon the net file descriptor and bubble up the error */ |
| 744 | netfile->abandon(vmg0_); |
| 745 | err_rethrow(); |
| 746 | } |
| 747 | err_end; |
| 748 | } |
| 749 | |
| 750 | /* return the network file descriptor */ |
| 751 | return netfile; |
| 752 | } |
| 753 | |
| 754 | /* |
| 755 | * Resolve a special file ID to a local filename path |
| 756 | */ |
| 757 | int CVmObjFile::sfid_to_path(VMG_ char *buf, size_t buflen, int32_t sfid) |
| 758 | { |
| 759 | const char *fname = 0; |
| 760 | char path[OSFNMAX]; |
| 761 | switch (sfid) |
| 762 | { |
| 763 | case SFID_LIB_DEFAULTS: |
| 764 | /* library defaults file - T3_APP_DATA/settings.txt */ |
| 765 | fname = "settings.txt"; |
| 766 | goto app_data_file; |
| 767 | |
| 768 | case SFID_WEBUI_PREFS: |
| 769 | /* Web UI preferences - T3_APP_DATA/webprefs.txt */ |
| 770 | fname = "webprefs.txt"; |
| 771 | goto app_data_file; |
| 772 | |
| 773 | app_data_file: |
| 774 | /* get the system application data path */ |
| 775 | G_host_ifc->get_special_file_path( |
| 776 | path, sizeof(path), OS_GSP_T3_APP_DATA); |
| 777 | |
| 778 | /* add the filename */ |
| 779 | os_build_full_path(buf, buflen, path, fname); |
| 780 | |
| 781 | /* success */ |
| 782 | return TRUE; |
| 783 | |
| 784 | default: |
| 785 | /* invalid ID value */ |
| 786 | buf[0] = '\0'; |
| 787 | return FALSE; |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | |
| 792 | /* ------------------------------------------------------------------------ */ |
| 793 | /* |
| 794 | * Pop a character set mapper argument. If there's no argument, we'll |
| 795 | * create and return a mapper for the default character set. |
| 796 | */ |
| 797 | vm_obj_id_t CVmObjFile::get_charset_arg(VMG_ int argn, int argc) |
| 798 | { |
| 799 | /* presume we won't find a valid argument value */ |
| 800 | vm_obj_id_t obj = VM_INVALID_OBJ; |
| 801 | |
| 802 | /* if there's an argument, pop it */ |
| 803 | if (argn < argc) |
| 804 | obj = CVmBif::get_charset_obj(vmg_ argn); |
| 805 | |
| 806 | /* if we don't have a character set yet, create the default */ |
| 807 | if (obj == VM_INVALID_OBJ) |
| 808 | { |
| 809 | /* get the default local character set for file contents */ |
| 810 | char csname[32]; |
| 811 | os_get_charmap(csname, OS_CHARMAP_FILECONTENTS); |
| 812 | |
| 813 | /* create the mapper */ |
| 814 | obj = CVmObjCharSet::create(vmg_ FALSE, csname, strlen(csname)); |
| 815 | } |
| 816 | |
| 817 | /* return the character set */ |
| 818 | return obj; |
| 819 | } |
| 820 | |
| 821 | |
| 822 | /* ------------------------------------------------------------------------ */ |
| 823 | /* |
| 824 | * Static property evaluator - open a text file |
| 825 | */ |
| 826 | int CVmObjFile::s_getp_open_text(VMG_ vm_val_t *retval, uint *in_argc, |
| 827 | int is_resource_file, const vm_rcdesc *rc) |
| 828 | { |
| 829 | /* check arguments */ |
| 830 | uint argc = (in_argc != 0 ? *in_argc : 0); |
| 831 | static CVmNativeCodeDesc desc_file(2, 1); |
| 832 | static CVmNativeCodeDesc desc_res(1, 1); |
| 833 | if (get_prop_check_argc(retval, in_argc, |
| 834 | is_resource_file ? &desc_res : &desc_file)) |
| 835 | return TRUE; |
| 836 | |
| 837 | /* |
| 838 | * retrieve the filename and access mode arguments, and set up network |
| 839 | * storage server access if applicable |
| 840 | */ |
| 841 | int access = 0; |
| 842 | CVmNetFile *netfile = get_filename_and_access( |
| 843 | vmg_ rc, &access, is_resource_file, OSFTTEXT, "text/plain"); |
| 844 | |
| 845 | err_try |
| 846 | { |
| 847 | /* presume we won't need a read buffer */ |
| 848 | int create_readbuf = FALSE; |
| 849 | |
| 850 | /* retrieve the character set object */ |
| 851 | vm_obj_id_t cset_obj = get_charset_arg(vmg_ 2, argc); |
| 852 | |
| 853 | /* push the character map object onto the stack for gc protection */ |
| 854 | G_stk->push()->set_obj(cset_obj); |
| 855 | |
| 856 | /* open the file for reading or writing, as appropriate */ |
| 857 | CVmDataSource *ds = 0; |
| 858 | osfildef *fp = 0; |
| 859 | switch(access) |
| 860 | { |
| 861 | case VMOBJFILE_ACCESS_READ: |
| 862 | /* open a resource file or file system file, as appropriate */ |
| 863 | if (is_resource_file) |
| 864 | { |
| 865 | /* it's a resource - open it */ |
| 866 | unsigned long res_len; |
| 867 | fp = G_host_ifc->find_resource( |
| 868 | netfile->lclfname, strlen(netfile->lclfname), &res_len); |
| 869 | |
| 870 | /* |
| 871 | * if we found the resource, note the start and end seek |
| 872 | * positions, so we can limit reading of the underlying |
| 873 | * file to the section that contains the resource data |
| 874 | */ |
| 875 | if (fp != 0) |
| 876 | { |
| 877 | /* |
| 878 | * find_resource() handed us back a file handle |
| 879 | * positioned at the start of the resource data stream, |
| 880 | * possibly within a larger file; note the current |
| 881 | * offset as the starting offset in case we need to |
| 882 | * seek within the resource later |
| 883 | */ |
| 884 | long res_start = osfpos(fp); |
| 885 | |
| 886 | /* |
| 887 | * note where the resource ends - it might be embedded |
| 888 | * within a larger file, so we need to limit reading to |
| 889 | * the extent from the resource map |
| 890 | */ |
| 891 | long res_end = res_start + res_len; |
| 892 | |
| 893 | /* create the data source */ |
| 894 | ds = new CVmResFileSource(fp, res_start, res_end); |
| 895 | } |
| 896 | } |
| 897 | else |
| 898 | { |
| 899 | /* |
| 900 | * It's not a resource - open an ordinary text file for |
| 901 | * reading. Even though we're going to treat the file as a |
| 902 | * text file, open it in binary mode, since we'll do our |
| 903 | * own universal newline translations; this allows us to |
| 904 | * work with files in any character set, and using almost |
| 905 | * any newline conventions, so files copied from other |
| 906 | * systems will be fully usable even if they haven't been |
| 907 | * fixed up to local conventions. |
| 908 | */ |
| 909 | fp = osfoprb(netfile->lclfname, OSFTTEXT); |
| 910 | |
| 911 | /* if that worked, create the data source */ |
| 912 | if (fp != 0) |
| 913 | ds = new CVmFileSource(fp); |
| 914 | } |
| 915 | |
| 916 | /* make sure we opened it successfully */ |
| 917 | if (fp == 0) |
| 918 | G_interpreter->throw_new_class( |
| 919 | vmg_ G_predef->file_not_found_exc, |
| 920 | 0, "file not found"); |
| 921 | |
| 922 | /* we need a read buffer */ |
| 923 | create_readbuf = TRUE; |
| 924 | break; |
| 925 | |
| 926 | case VMOBJFILE_ACCESS_WRITE: |
| 927 | /* open for writing */ |
| 928 | fp = osfopwb(netfile->lclfname, OSFTTEXT); |
| 929 | |
| 930 | /* make sure we created it successfully */ |
| 931 | if (fp == 0) |
| 932 | G_interpreter->throw_new_class( |
| 933 | vmg_ G_predef->file_creation_exc, |
| 934 | 0, "error creating file"); |
| 935 | |
| 936 | /* create the data source */ |
| 937 | ds = new CVmFileSource(fp); |
| 938 | break; |
| 939 | |
| 940 | case VMOBJFILE_ACCESS_RW_KEEP: |
| 941 | /* open for read/write, keeping existing contents */ |
| 942 | fp = osfoprwb(netfile->lclfname, OSFTTEXT); |
| 943 | |
| 944 | /* make sure we were able to find or create the file */ |
| 945 | if (fp == 0) |
| 946 | G_interpreter->throw_new_class(vmg_ G_predef->file_open_exc, |
| 947 | 0, "error opening file"); |
| 948 | |
| 949 | /* create the data source */ |
| 950 | ds = new CVmFileSource(fp); |
| 951 | |
| 952 | /* we need a read buffer */ |
| 953 | create_readbuf = TRUE; |
| 954 | break; |
| 955 | |
| 956 | case VMOBJFILE_ACCESS_RW_TRUNC: |
| 957 | /* open for read/write, truncating existing contents */ |
| 958 | fp = osfoprwtb(netfile->lclfname, OSFTTEXT); |
| 959 | |
| 960 | /* make sure we were successful */ |
| 961 | if (fp == 0) |
| 962 | G_interpreter->throw_new_class(vmg_ G_predef->file_open_exc, |
| 963 | 0, "error opening file"); |
| 964 | |
| 965 | /* create the data source */ |
| 966 | ds = new CVmFileSource(fp); |
| 967 | |
| 968 | /* we need a read buffer */ |
| 969 | create_readbuf = TRUE; |
| 970 | break; |
| 971 | |
| 972 | default: |
| 973 | /* invalid access mode */ |
| 974 | fp = 0; |
| 975 | err_throw(VMERR_BAD_VAL_BIF); |
| 976 | } |
| 977 | |
| 978 | /* create our file object */ |
| 979 | retval->set_obj(create(vmg_ FALSE, netfile, cset_obj, ds, |
| 980 | VMOBJFILE_MODE_TEXT, access, create_readbuf)); |
| 981 | |
| 982 | /* |
| 983 | * the network file descriptor is stored in the File object now, so |
| 984 | * we no longer need to delete it |
| 985 | */ |
| 986 | netfile = 0; |
| 987 | |
| 988 | /* discard arguments and gc protection */ |
| 989 | G_stk->discard(argc + 1); |
| 990 | } |
| 991 | err_finally |
| 992 | { |
| 993 | /* |
| 994 | * If we created a network file descriptor and didn't hand it off |
| 995 | * to the File object, we're abandoning the object. |
| 996 | */ |
| 997 | if (netfile != 0) |
| 998 | netfile->abandon(vmg0_); |
| 999 | } |
| 1000 | err_end; |
| 1001 | |
| 1002 | /* handled */ |
| 1003 | return TRUE; |
| 1004 | } |
| 1005 | |
| 1006 | /* |
| 1007 | * Static property evaluator - open a data file |
| 1008 | */ |
| 1009 | int CVmObjFile::s_getp_open_data(VMG_ vm_val_t *retval, uint *argc, |
| 1010 | const vm_rcdesc *rc) |
| 1011 | { |
| 1012 | /* use the generic binary file opener in 'data' mode */ |
| 1013 | return open_binary(vmg_ retval, argc, VMOBJFILE_MODE_DATA, FALSE, rc); |
| 1014 | } |
| 1015 | |
| 1016 | /* |
| 1017 | * Static property evaluator - open a raw file |
| 1018 | */ |
| 1019 | int CVmObjFile::s_getp_open_raw(VMG_ vm_val_t *retval, uint *argc, |
| 1020 | int is_resource_file, const vm_rcdesc *rc) |
| 1021 | { |
| 1022 | /* use the generic binary file opener in 'raw' mode */ |
| 1023 | return open_binary(vmg_ retval, argc, VMOBJFILE_MODE_RAW, |
| 1024 | is_resource_file, rc); |
| 1025 | } |
| 1026 | |
| 1027 | /* |
| 1028 | * Generic binary file opener - common to 'data' and 'raw' files |
| 1029 | */ |
| 1030 | int CVmObjFile::open_binary(VMG_ vm_val_t *retval, uint *in_argc, int mode, |
| 1031 | int is_resource_file, const vm_rcdesc *rc) |
| 1032 | { |
| 1033 | /* check arguments */ |
| 1034 | uint argc = (in_argc != 0 ? *in_argc : 0); |
| 1035 | static CVmNativeCodeDesc file_desc(2); |
| 1036 | static CVmNativeCodeDesc res_desc(1); |
| 1037 | if (get_prop_check_argc(retval, in_argc, |
| 1038 | is_resource_file ? &res_desc : &file_desc)) |
| 1039 | return TRUE; |
| 1040 | |
| 1041 | /* retrieve the filename and access mode */ |
| 1042 | int access = 0; |
| 1043 | CVmNetFile *netfile = get_filename_and_access( |
| 1044 | vmg_ rc, &access, is_resource_file, |
| 1045 | OSFTBIN, "application/octet-stream"); |
| 1046 | |
| 1047 | err_try |
| 1048 | { |
| 1049 | osfildef *fp; |
| 1050 | CVmDataSource *ds = 0; |
| 1051 | |
| 1052 | /* open the file in binary mode, with the desired access type */ |
| 1053 | switch(access) |
| 1054 | { |
| 1055 | case VMOBJFILE_ACCESS_READ: |
| 1056 | /* open the resource or ordinary file, as appropriate */ |
| 1057 | if (is_resource_file) |
| 1058 | { |
| 1059 | unsigned long res_len; |
| 1060 | |
| 1061 | /* it's a resource - open it */ |
| 1062 | fp = G_host_ifc->find_resource( |
| 1063 | netfile->lclfname, strlen(netfile->lclfname), &res_len); |
| 1064 | |
| 1065 | /* |
| 1066 | * if we found the resource, note the start and end seek |
| 1067 | * positions, so we can limit reading of the underlying |
| 1068 | * file to the section that contains the resource data |
| 1069 | */ |
| 1070 | if (fp != 0) |
| 1071 | { |
| 1072 | /* |
| 1073 | * find_resource() hands us a file handle positioned at |
| 1074 | * the start of the resource data - note the seek |
| 1075 | * offset in case we need to seek around within the |
| 1076 | * resource stream later on |
| 1077 | */ |
| 1078 | long res_start = osfpos(fp); |
| 1079 | |
| 1080 | /* |
| 1081 | * note the end of the resource, in case it's embedded |
| 1082 | * within a larger physical file |
| 1083 | */ |
| 1084 | long res_end = res_start + res_len; |
| 1085 | |
| 1086 | /* create the data source */ |
| 1087 | ds = new CVmResFileSource(fp, res_start, res_end); |
| 1088 | } |
| 1089 | } |
| 1090 | else |
| 1091 | { |
| 1092 | /* open the ordinary file in binary mode for reading only */ |
| 1093 | fp = osfoprb(netfile->lclfname, OSFTBIN); |
| 1094 | |
| 1095 | /* create the data source */ |
| 1096 | if (fp != 0) |
| 1097 | ds = new CVmFileSource(fp); |
| 1098 | } |
| 1099 | |
| 1100 | /* make sure we were able to find it and open it */ |
| 1101 | if (fp == 0) |
| 1102 | G_interpreter->throw_new_class( |
| 1103 | vmg_ G_predef->file_not_found_exc, 0, "file not found"); |
| 1104 | break; |
| 1105 | |
| 1106 | case VMOBJFILE_ACCESS_WRITE: |
| 1107 | /* open in binary mode for writing only */ |
| 1108 | fp = osfopwb(netfile->lclfname, OSFTBIN); |
| 1109 | |
| 1110 | /* make sure we were able to create the file successfully */ |
| 1111 | if (fp == 0) |
| 1112 | G_interpreter->throw_new_class( |
| 1113 | vmg_ G_predef->file_creation_exc, |
| 1114 | 0, "error creating file"); |
| 1115 | |
| 1116 | /* create the data source */ |
| 1117 | ds = new CVmFileSource(fp); |
| 1118 | break; |
| 1119 | |
| 1120 | case VMOBJFILE_ACCESS_RW_KEEP: |
| 1121 | /* open for read/write, keeping existing contents */ |
| 1122 | fp = osfoprwb(netfile->lclfname, OSFTBIN); |
| 1123 | |
| 1124 | /* make sure we were able to find or create the file */ |
| 1125 | if (fp == 0) |
| 1126 | G_interpreter->throw_new_class(vmg_ G_predef->file_open_exc, |
| 1127 | 0, "error opening file"); |
| 1128 | |
| 1129 | /* create the data source */ |
| 1130 | ds = new CVmFileSource(fp); |
| 1131 | break; |
| 1132 | |
| 1133 | case VMOBJFILE_ACCESS_RW_TRUNC: |
| 1134 | /* open for read/write, truncating existing contents */ |
| 1135 | fp = osfoprwtb(netfile->lclfname, OSFTBIN); |
| 1136 | |
| 1137 | /* make sure we were successful */ |
| 1138 | if (fp == 0) |
| 1139 | G_interpreter->throw_new_class(vmg_ G_predef->file_open_exc, |
| 1140 | 0, "error opening file"); |
| 1141 | |
| 1142 | /* create the data source */ |
| 1143 | ds = new CVmFileSource(fp); |
| 1144 | break; |
| 1145 | |
| 1146 | default: |
| 1147 | fp = 0; |
| 1148 | err_throw(VMERR_BAD_VAL_BIF); |
| 1149 | } |
| 1150 | |
| 1151 | /* create our file object */ |
| 1152 | retval->set_obj(create( |
| 1153 | vmg_ FALSE, netfile, VM_INVALID_OBJ, ds, mode, access, FALSE)); |
| 1154 | |
| 1155 | /* |
| 1156 | * we've handed off the network file descriptor to the File object, |
| 1157 | * so we no longer have responsibility for it |
| 1158 | */ |
| 1159 | netfile = 0; |
| 1160 | } |
| 1161 | err_finally |
| 1162 | { |
| 1163 | /* |
| 1164 | * if we created a network file descriptor and didn't hand it off |
| 1165 | * to the new File object, we're abandoning it |
| 1166 | */ |
| 1167 | if (netfile != 0) |
| 1168 | netfile->abandon(vmg0_); |
| 1169 | } |
| 1170 | err_end; |
| 1171 | |
| 1172 | /* discard arguments */ |
| 1173 | G_stk->discard(argc); |
| 1174 | |
| 1175 | /* handled */ |
| 1176 | return TRUE; |
| 1177 | } |
| 1178 | |
| 1179 | /* ------------------------------------------------------------------------ */ |
| 1180 | /* |
| 1181 | * Check the safety settings to determine if an open operation is allowed. |
| 1182 | * If the access is not allowed, we'll throw an error. |
| 1183 | */ |
| 1184 | void CVmObjFile::check_safety_for_open(VMG_ CVmNetFile *nf, int access) |
| 1185 | { |
| 1186 | /* |
| 1187 | * Special files are inherently sandboxed, since the system determines |
| 1188 | * their paths. These are always allowed for ordinary read/write. |
| 1189 | */ |
| 1190 | if (nf->sfid != 0 |
| 1191 | && (access == VMOBJFILE_ACCESS_READ |
| 1192 | || access == VMOBJFILE_ACCESS_WRITE |
| 1193 | || access == VMOBJFILE_ACCESS_RW_KEEP |
| 1194 | || access == VMOBJFILE_ACCESS_RW_TRUNC |
| 1195 | || access == VMOBJFILE_ACCESS_GETINFO |
| 1196 | || access == VMOBJFILE_ACCESS_READDIR)) |
| 1197 | return; |
| 1198 | |
| 1199 | /* |
| 1200 | * Temporary file paths can only be obtained from the VM. Ordinary |
| 1201 | * read/write operations are allowed, as is delete. |
| 1202 | */ |
| 1203 | if (nf->is_temp |
| 1204 | && (access == VMOBJFILE_ACCESS_READ |
| 1205 | || access == VMOBJFILE_ACCESS_WRITE |
| 1206 | || access == VMOBJFILE_ACCESS_RW_KEEP |
| 1207 | || access == VMOBJFILE_ACCESS_RW_TRUNC |
| 1208 | || access == VMOBJFILE_ACCESS_DELETE |
| 1209 | || access == VMOBJFILE_ACCESS_GETINFO |
| 1210 | || access == VMOBJFILE_ACCESS_READDIR)) |
| 1211 | return; |
| 1212 | |
| 1213 | /* |
| 1214 | * Network files are subject to separate permission rules enforced by |
| 1215 | * the storage server. Local file safety settings don't apply. |
| 1216 | */ |
| 1217 | if (nf->is_net_file()) |
| 1218 | return; |
| 1219 | |
| 1220 | /* |
| 1221 | * If the file was specifically selected by the user via a file dialog, |
| 1222 | * allow access of the type proposed by the dialog, even if it's |
| 1223 | * outside the sandbox. The user has implicitly granted us permission |
| 1224 | * on this individual file by manually selecting it via a UI |
| 1225 | * interaction. |
| 1226 | */ |
| 1227 | CVmObjFileName *ofn; |
| 1228 | if ((ofn = vm_objid_cast(CVmObjFileName, nf->filespec)) != 0 |
| 1229 | && ofn->get_from_ui() != 0) |
| 1230 | { |
| 1231 | /* grant permission based on the dialog mode */ |
| 1232 | switch (ofn->get_from_ui()) |
| 1233 | { |
| 1234 | case OS_AFP_OPEN: |
| 1235 | /* allow reading for a file selected with an Open dialog */ |
| 1236 | if (access == VMOBJFILE_ACCESS_READ) |
| 1237 | return; |
| 1238 | break; |
| 1239 | |
| 1240 | case OS_AFP_SAVE: |
| 1241 | /* allow writing for a file selected with a Save dialog */ |
| 1242 | if (access == VMOBJFILE_ACCESS_WRITE) |
| 1243 | return; |
| 1244 | break; |
| 1245 | } |
| 1246 | |
| 1247 | /* |
| 1248 | * For anything we didn't override above based on the dialog mode, |
| 1249 | * don't give up - the file safety permissions might still allow |
| 1250 | * it. All we've determined at this point is that the dialog |
| 1251 | * selection doesn't grant extraordinary permission for the |
| 1252 | * attempted access mode. |
| 1253 | */ |
| 1254 | } |
| 1255 | |
| 1256 | /* |
| 1257 | * The file wasn't specified by a type that confers any special |
| 1258 | * privileges, so determine if it's accessible according to the local |
| 1259 | * file name's handling under the file safety and sandbox rules. |
| 1260 | */ |
| 1261 | return check_safety_for_open(vmg_ nf->lclfname, access); |
| 1262 | } |
| 1263 | |
| 1264 | /* |
| 1265 | * Check file safety for opening the given local file path. |
| 1266 | */ |
| 1267 | void CVmObjFile::check_safety_for_open(VMG_ const char *lclfname, int access) |
| 1268 | { |
| 1269 | /* check the safety settings */ |
| 1270 | if (!query_safety_for_open(vmg_ lclfname, access)) |
| 1271 | { |
| 1272 | /* this operation is not allowed - throw an error */ |
| 1273 | G_interpreter->throw_new_class(vmg_ G_predef->file_safety_exc, |
| 1274 | 0, "prohibited file access"); |
| 1275 | } |
| 1276 | } |
| 1277 | |
| 1278 | /* |
| 1279 | * Query the safety settings. Returns true if the access is allowed, false |
| 1280 | * if it's prohibited. |
| 1281 | */ |
| 1282 | int CVmObjFile::query_safety_for_open(VMG_ const char *lclfname, int access) |
| 1283 | { |
| 1284 | /* get the current file safety level from the host application */ |
| 1285 | int read_level = G_host_ifc->get_io_safety_read(); |
| 1286 | int write_level = G_host_ifc->get_io_safety_write(); |
| 1287 | |
| 1288 | /* |
| 1289 | * First check to see if the safety level allows or disallows the |
| 1290 | * requested access without regard to location. If so, we can save a |
| 1291 | * little work by skipping the sandbox resolution. |
| 1292 | */ |
| 1293 | switch (access) |
| 1294 | { |
| 1295 | case VMOBJFILE_ACCESS_READ: |
| 1296 | case VMOBJFILE_ACCESS_GETINFO: |
| 1297 | case VMOBJFILE_ACCESS_READDIR: |
| 1298 | /* |
| 1299 | * Read a file or its metadata. If the safety level is READ_ANY or |
| 1300 | * below, we can read any file regardless of location. If it's |
| 1301 | * MAXIMUM, we can't read anything. |
| 1302 | */ |
| 1303 | if (read_level <= VM_IO_SAFETY_READ_ANY_WRITE_CUR) |
| 1304 | return TRUE; |
| 1305 | if (read_level >= VM_IO_SAFETY_MAXIMUM) |
| 1306 | return FALSE; |
| 1307 | break; |
| 1308 | |
| 1309 | case VMOBJFILE_ACCESS_RMDIR: |
| 1310 | case VMOBJFILE_ACCESS_WRITE: |
| 1311 | case VMOBJFILE_ACCESS_RW_KEEP: |
| 1312 | case VMOBJFILE_ACCESS_RW_TRUNC: |
| 1313 | case VMOBJFILE_ACCESS_DELETE: |
| 1314 | case VMOBJFILE_ACCESS_MKDIR: |
| 1315 | case VMOBJFILE_ACCESS_RENAME_FROM: |
| 1316 | case VMOBJFILE_ACCESS_RENAME_TO: |
| 1317 | /* |
| 1318 | * Writing, deleting, renaming, creating/removing a directory. If |
| 1319 | * the safety level is MINIMUM, we're allowed to write anywhere. |
| 1320 | * If it's READ_CUR or higher, writing isn't allowed anywhere. |
| 1321 | */ |
| 1322 | if (write_level <= VM_IO_SAFETY_MINIMUM) |
| 1323 | return TRUE; |
| 1324 | if (write_level >= VM_IO_SAFETY_READ_CUR) |
| 1325 | return FALSE; |
| 1326 | break; |
| 1327 | } |
| 1328 | |
| 1329 | /* |
| 1330 | * If we get this far, it means we're in sandbox mode, so the access is |
| 1331 | * conditional on the file's location relative to the sandbox folder. |
| 1332 | * Get the sandbox path, defaulting to the file base path. |
| 1333 | */ |
| 1334 | const char *sandbox = (G_sandbox_path != 0 ? G_sandbox_path : G_file_path); |
| 1335 | |
| 1336 | /* assume the file isn't in the sandbox */ |
| 1337 | int in_sandbox = FALSE; |
| 1338 | |
| 1339 | /* |
| 1340 | * Most operations in sandbox mode don't consider the sandbox directory |
| 1341 | * itself to be part of the sandbox - e.g., you can't delete the |
| 1342 | * sandbox folder. |
| 1343 | */ |
| 1344 | int match_self = FALSE; |
| 1345 | |
| 1346 | /* |
| 1347 | * The effective sandbox extends beyond the actual contents the sandbox |
| 1348 | * directory for a couple of modes: |
| 1349 | * |
| 1350 | * - For READDIR mode (reading the contents of a directory), the |
| 1351 | * sandbox directory itself is considered part of the sandbox, since |
| 1352 | * the information we're accessing in creating a directory listing is |
| 1353 | * really the directory's contents. |
| 1354 | * |
| 1355 | * - For GETINFO mode (read file metadata), extend the sandbox to |
| 1356 | * include the sandbox directory itself, its parent, its parent's |
| 1357 | * parent, etc. It's pretty obvious that we should have metadata read |
| 1358 | * access to the sandbox directory itself, given that we can list and |
| 1359 | * access its contents. The rationale for also including parents of |
| 1360 | * the sandbox folder is that they're effectively part of its metadata, |
| 1361 | * since they're visible in its full path name. |
| 1362 | */ |
| 1363 | switch (access) |
| 1364 | { |
| 1365 | case VMOBJFILE_ACCESS_GETINFO: |
| 1366 | /* |
| 1367 | * read metadata - the sandbox directory is part of the sandbox for |
| 1368 | * this operation, as is its parent, its parent's parent, etc |
| 1369 | * (which is to say, any folder that contains the sandbox) |
| 1370 | */ |
| 1371 | in_sandbox |= os_is_file_in_dir(sandbox, lclfname, TRUE, TRUE); |
| 1372 | break; |
| 1373 | |
| 1374 | case VMOBJFILE_ACCESS_READDIR: |
| 1375 | /* |
| 1376 | * read directory contents - the sandbox directory itself is part |
| 1377 | * of the sandbox for this mode |
| 1378 | */ |
| 1379 | match_self = TRUE; |
| 1380 | break; |
| 1381 | } |
| 1382 | |
| 1383 | /* |
| 1384 | * Check to see if the file is in the sandbox folder (or any subfolder |
| 1385 | * thereof). If not, we may have to disallow the operation based on |
| 1386 | * safety level settings, but for now just note the location status. |
| 1387 | */ |
| 1388 | in_sandbox |= os_is_file_in_dir(lclfname, sandbox, TRUE, match_self); |
| 1389 | |
| 1390 | /* if the file isn't in the sandbox, access isn't allowed */ |
| 1391 | if (!in_sandbox) |
| 1392 | return FALSE; |
| 1393 | |
| 1394 | /* didn't find any problems - allow the access */ |
| 1395 | return TRUE; |
| 1396 | } |
| 1397 | |
| 1398 | /* ------------------------------------------------------------------------ */ |
| 1399 | /* |
| 1400 | * Property evaluator - get the character set |
| 1401 | */ |
| 1402 | int CVmObjFile::getp_get_charset(VMG_ vm_obj_id_t self, vm_val_t *retval, |
| 1403 | uint *argc) |
| 1404 | { |
| 1405 | static CVmNativeCodeDesc desc(0); |
| 1406 | |
| 1407 | /* check arguments */ |
| 1408 | if (get_prop_check_argc(retval, argc, &desc)) |
| 1409 | return TRUE; |
| 1410 | |
| 1411 | /* if we have a character set object, return it; otherwise return nil */ |
| 1412 | if (get_ext()->charset != VM_INVALID_OBJ) |
| 1413 | retval->set_obj(get_ext()->charset); |
| 1414 | else |
| 1415 | retval->set_nil(); |
| 1416 | |
| 1417 | /* handled */ |
| 1418 | return TRUE; |
| 1419 | } |
| 1420 | |
| 1421 | /* ------------------------------------------------------------------------ */ |
| 1422 | /* |
| 1423 | * Property evaluator - set the character set |
| 1424 | */ |
| 1425 | int CVmObjFile::getp_set_charset(VMG_ vm_obj_id_t self, vm_val_t *retval, |
| 1426 | uint *argc) |
| 1427 | { |
| 1428 | /* check arguments */ |
| 1429 | static CVmNativeCodeDesc desc(1); |
| 1430 | if (get_prop_check_argc(retval, argc, &desc)) |
| 1431 | return TRUE; |
| 1432 | |
| 1433 | /* retrieve and save the character set argument */ |
| 1434 | get_ext()->charset = get_charset_arg(vmg_ 0, 1); |
| 1435 | |
| 1436 | /* discard arguments */ |
| 1437 | G_stk->discard(); |
| 1438 | |
| 1439 | /* no return value */ |
| 1440 | retval->set_nil(); |
| 1441 | |
| 1442 | /* handled */ |
| 1443 | return TRUE; |
| 1444 | } |
| 1445 | |
| 1446 | /* ------------------------------------------------------------------------ */ |
| 1447 | /* |
| 1448 | * Check that we're in a valid state to perform file operations. |
| 1449 | */ |
| 1450 | void CVmObjFile::check_valid_file(VMG0_) |
| 1451 | { |
| 1452 | /* if we're 'out of sync', we can't perform any operations on it */ |
| 1453 | if ((get_ext()->flags & VMOBJFILE_OUT_OF_SYNC) != 0) |
| 1454 | G_interpreter->throw_new_class(vmg_ G_predef->file_sync_exc, |
| 1455 | 0, "file out of sync"); |
| 1456 | |
| 1457 | /* if the file has been closed, we can't perform any operations on it */ |
| 1458 | if (get_ext()->fp == 0) |
| 1459 | G_interpreter->throw_new_class(vmg_ G_predef->file_closed_exc, 0, |
| 1460 | "operation attempted on closed file"); |
| 1461 | } |
| 1462 | |
| 1463 | /* |
| 1464 | * Check that we have read access |
| 1465 | */ |
| 1466 | void CVmObjFile::check_read_access(VMG0_) |
| 1467 | { |
| 1468 | /* we have read access unless we're in write-only mode */ |
| 1469 | if (get_ext()->access == VMOBJFILE_ACCESS_WRITE) |
| 1470 | G_interpreter->throw_new_class(vmg_ G_predef->file_mode_exc, 0, |
| 1471 | "wrong file mode"); |
| 1472 | } |
| 1473 | |
| 1474 | /* |
| 1475 | * Check that we have write access |
| 1476 | */ |
| 1477 | void CVmObjFile::check_write_access(VMG0_) |
| 1478 | { |
| 1479 | /* we have write access unless we're in read-only mode */ |
| 1480 | if (get_ext()->access == VMOBJFILE_ACCESS_READ) |
| 1481 | G_interpreter->throw_new_class(vmg_ G_predef->file_mode_exc, 0, |
| 1482 | "wrong file mode"); |
| 1483 | } |
| 1484 | |
| 1485 | /* |
| 1486 | * Check that we're in raw mode |
| 1487 | */ |
| 1488 | void CVmObjFile::check_raw_mode(VMG0_) |
| 1489 | { |
| 1490 | /* if we're not in raw mode, throw an error */ |
| 1491 | if (get_ext()->mode != VMOBJFILE_MODE_RAW) |
| 1492 | G_interpreter->throw_new_class(vmg_ G_predef->file_mode_exc, |
| 1493 | 0, "wrong file mode"); |
| 1494 | } |
| 1495 | |
| 1496 | /* |
| 1497 | * Check that we've a valid raw readable file |
| 1498 | */ |
| 1499 | void CVmObjFile::check_raw_read(VMG0_) |
| 1500 | { |
| 1501 | check_valid_file(vmg0_); |
| 1502 | check_read_access(vmg0_); |
| 1503 | check_raw_mode(vmg0_); |
| 1504 | } |
| 1505 | |
| 1506 | |
| 1507 | /* |
| 1508 | * Check that we've a valid raw writable file |
| 1509 | */ |
| 1510 | void CVmObjFile::check_raw_write(VMG0_) |
| 1511 | { |
| 1512 | check_valid_file(vmg0_); |
| 1513 | check_write_access(vmg0_); |
| 1514 | check_raw_mode(vmg0_); |
| 1515 | } |
| 1516 | |
| 1517 | |
| 1518 | /* ------------------------------------------------------------------------ */ |
| 1519 | /* |
| 1520 | * Property evaluator - close the file |
| 1521 | */ |
| 1522 | int CVmObjFile::getp_close_file(VMG_ vm_obj_id_t self, vm_val_t *retval, |
| 1523 | uint *argc) |
| 1524 | { |
| 1525 | static CVmNativeCodeDesc desc(0); |
| 1526 | |
| 1527 | /* check arguments */ |
| 1528 | if (get_prop_check_argc(retval, argc, &desc)) |
| 1529 | return TRUE; |
| 1530 | |
| 1531 | /* make sure we are allowed to perform operations on the file */ |
| 1532 | check_valid_file(vmg0_); |
| 1533 | |
| 1534 | /* |
| 1535 | * take over the network file descriptor from the File object - we're |
| 1536 | * going to either close it or abandon it, which deletes it, so we |
| 1537 | * don't need to delete it again when the File object is collected |
| 1538 | */ |
| 1539 | CVmNetFile *netfile = get_ext()->netfile; |
| 1540 | get_ext()->netfile = 0; |
| 1541 | |
| 1542 | /* |
| 1543 | * Explicitly flush the file, to detect any errors flushing our final |
| 1544 | * write buffers. The regular osifc close ignores errors, so we have |
| 1545 | * to check with an explicit flush if we want this information. |
| 1546 | */ |
| 1547 | err_try |
| 1548 | { |
| 1549 | /* flush buffers */ |
| 1550 | get_ext()->fp->flush(); |
| 1551 | } |
| 1552 | err_catch_disc |
| 1553 | { |
| 1554 | /* |
| 1555 | * The flush failed, so the close effectively failed. Close the |
| 1556 | * file anyway to release system resources. |
| 1557 | */ |
| 1558 | delete get_ext()->fp; |
| 1559 | |
| 1560 | /* |
| 1561 | * Abandon the network file object. Since we failed to close the |
| 1562 | * local copy properly, we don't want to update the server copy (if |
| 1563 | * it exists) with corrupt data. |
| 1564 | */ |
| 1565 | if (netfile != 0) |
| 1566 | netfile->abandon(vmg0_); |
| 1567 | |
| 1568 | /* rethrow the error */ |
| 1569 | err_rethrow(); |
| 1570 | } |
| 1571 | err_end; |
| 1572 | |
| 1573 | /* close and explicitly forget the data source */ |
| 1574 | delete get_ext()->fp; |
| 1575 | get_ext()->fp = 0; |
| 1576 | |
| 1577 | /* if there's a network file, close it */ |
| 1578 | if (netfile != 0) |
| 1579 | netfile->close(vmg0_); |
| 1580 | |
| 1581 | /* no return value */ |
| 1582 | retval->set_nil(); |
| 1583 | |
| 1584 | /* handled */ |
| 1585 | return TRUE; |
| 1586 | } |
| 1587 | |
| 1588 | /* ------------------------------------------------------------------------ */ |
| 1589 | /* |
| 1590 | * Property evaluator - read from the file |
| 1591 | */ |
| 1592 | int CVmObjFile::getp_read_file(VMG_ vm_obj_id_t self, vm_val_t *retval, |
| 1593 | uint *argc) |
| 1594 | { |
| 1595 | static CVmNativeCodeDesc desc(0); |
| 1596 | |
| 1597 | /* check arguments */ |
| 1598 | if (get_prop_check_argc(retval, argc, &desc)) |
| 1599 | return TRUE; |
| 1600 | |
| 1601 | /* push a self-reference for gc protection */ |
| 1602 | G_stk->push()->set_obj(self); |
| 1603 | |
| 1604 | /* make sure we are allowed to perform operations on the file */ |
| 1605 | check_valid_file(vmg0_); |
| 1606 | |
| 1607 | /* check that we have read access */ |
| 1608 | check_read_access(vmg0_); |
| 1609 | |
| 1610 | /* note the implicit seeking */ |
| 1611 | note_file_seek(vmg_ FALSE); |
| 1612 | |
| 1613 | /* flush stdio buffers as needed and note the read operation */ |
| 1614 | switch_read_write_mode(vmg_ FALSE); |
| 1615 | |
| 1616 | /* read according to our mode */ |
| 1617 | switch(get_ext()->mode) |
| 1618 | { |
| 1619 | case VMOBJFILE_MODE_TEXT: |
| 1620 | /* read a line of text */ |
| 1621 | read_text_mode(vmg_ retval); |
| 1622 | break; |
| 1623 | |
| 1624 | case VMOBJFILE_MODE_DATA: |
| 1625 | /* read in data mode */ |
| 1626 | read_data_mode(vmg_ retval); |
| 1627 | break; |
| 1628 | |
| 1629 | case VMOBJFILE_MODE_RAW: |
| 1630 | /* can't use this call on this type of file */ |
| 1631 | G_interpreter->throw_new_class(vmg_ G_predef->file_mode_exc, |
| 1632 | 0, "wrong file mode"); |
| 1633 | break; |
| 1634 | } |
| 1635 | |
| 1636 | /* discard the gc protection */ |
| 1637 | G_stk->discard(); |
| 1638 | |
| 1639 | /* handled */ |
| 1640 | return TRUE; |
| 1641 | } |
| 1642 | |
| 1643 | /* |
| 1644 | * Read a value in text mode |
| 1645 | */ |
| 1646 | void CVmObjFile::read_text_mode(VMG_ vm_val_t *retval) |
| 1647 | { |
| 1648 | /* get our file data source and read buffer from the extension */ |
| 1649 | CVmDataSource *fp = get_ext()->fp; |
| 1650 | vmobjfile_readbuf_t *readbuf = get_ext()->readbuf; |
| 1651 | |
| 1652 | /* get our character mapper */ |
| 1653 | CCharmapToUni *charmap = |
| 1654 | ((CVmObjCharSet *)vm_objp(vmg_ get_ext()->charset)) |
| 1655 | ->get_to_uni(vmg0_); |
| 1656 | |
| 1657 | /* replenish the buffer if it's empty */ |
| 1658 | if (readbuf->rem == 0 && !readbuf->refill(fp)) |
| 1659 | { |
| 1660 | /* end of file - return nil */ |
| 1661 | retval->set_nil(); |
| 1662 | return; |
| 1663 | } |
| 1664 | |
| 1665 | /* |
| 1666 | * Allocate a string object for the result. We have no idea how long |
| 1667 | * the line will be, so start with an arbitrary guess; we'll adjust the |
| 1668 | * buffer up or down as needed. |
| 1669 | */ |
| 1670 | vm_obj_id_t strid = CVmObjString::create(vmg_ FALSE, 128); |
| 1671 | CVmObjString *str = (CVmObjString *)vm_objp(vmg_ strid); |
| 1672 | char *dst = str->cons_get_buf(); |
| 1673 | |
| 1674 | /* this string will be the return value */ |
| 1675 | retval->set_obj(strid); |
| 1676 | |
| 1677 | /* push it for gc protection */ |
| 1678 | G_stk->push(retval); |
| 1679 | |
| 1680 | /* |
| 1681 | * Read a line of text from the file into our buffer. Keep going |
| 1682 | * until we read an entire line; we might have to read the line in |
| 1683 | * chunks, since the line might end up being longer than our buffer. |
| 1684 | */ |
| 1685 | for (;;) |
| 1686 | { |
| 1687 | /* read the next character; if we can't, we're at EOF */ |
| 1688 | wchar_t ch; |
| 1689 | size_t chlen; |
| 1690 | if (!readbuf->getch(ch, fp, charmap)) |
| 1691 | break; |
| 1692 | |
| 1693 | /* if this is a CR, LF, or U+2028, it's the end of the line */ |
| 1694 | if (ch == '\n' || ch == '\r' || ch == 0x2028) |
| 1695 | { |
| 1696 | /* newline - skip CR-LF and LR-CR pairs */ |
| 1697 | wchar_t nxt = (ch == '\n' ? '\r' : ch == '\r' ? '\n' : 0); |
| 1698 | size_t b; |
| 1699 | if (nxt != 0 && readbuf->peekch(ch, fp, charmap, b) && ch == nxt) |
| 1700 | readbuf->commit_peek(b); |
| 1701 | |
| 1702 | /* whichever type of newline we read, store it as LF */ |
| 1703 | ch = 10; |
| 1704 | chlen = 1; |
| 1705 | } |
| 1706 | else |
| 1707 | { |
| 1708 | /* ordinary character - figure the output character length */ |
| 1709 | chlen = utf8_ptr::s_wchar_size(ch); |
| 1710 | } |
| 1711 | |
| 1712 | /* ensure we have enough space for it in our string */ |
| 1713 | dst = str->cons_ensure_space(vmg_ dst, chlen, 128); |
| 1714 | |
| 1715 | /* add this character to the string */ |
| 1716 | dst += utf8_ptr::s_putch(dst, ch); |
| 1717 | |
| 1718 | /* if we just read a newline, we're done */ |
| 1719 | if (ch == '\n') |
| 1720 | break; |
| 1721 | } |
| 1722 | |
| 1723 | /* close the string */ |
| 1724 | str->cons_shrink_buffer(vmg_ dst); |
| 1725 | |
| 1726 | /* |
| 1727 | * we now can discard the string we've been keeping on the stack to |
| 1728 | * for garbage collection protection |
| 1729 | */ |
| 1730 | G_stk->discard(); |
| 1731 | } |
| 1732 | |
| 1733 | /* |
| 1734 | * Read a value in 'data' mode |
| 1735 | */ |
| 1736 | void CVmObjFile::read_data_mode(VMG_ vm_val_t *retval) |
| 1737 | { |
| 1738 | char buf[32]; |
| 1739 | CVmObjString *str_obj; |
| 1740 | vm_obj_id_t str_id; |
| 1741 | CVmDataSource *fp = get_ext()->fp; |
| 1742 | |
| 1743 | /* read the type flag */ |
| 1744 | if (fp->read(buf, 1)) |
| 1745 | { |
| 1746 | /* end of file - return nil */ |
| 1747 | retval->set_nil(); |
| 1748 | return; |
| 1749 | } |
| 1750 | |
| 1751 | /* see what we have */ |
| 1752 | switch(buf[0]) |
| 1753 | { |
| 1754 | case VMOBJFILE_TAG_INT: |
| 1755 | /* read the INT4 value */ |
| 1756 | if (fp->read(buf, 4)) |
| 1757 | goto io_error; |
| 1758 | |
| 1759 | /* set the integer value from the buffer */ |
| 1760 | retval->set_int(osrp4s(buf)); |
| 1761 | break; |
| 1762 | |
| 1763 | case VMOBJFILE_TAG_ENUM: |
| 1764 | /* read the UINT4 value */ |
| 1765 | if (fp->read(buf, 4)) |
| 1766 | goto io_error; |
| 1767 | |
| 1768 | /* set the 'enum' value */ |
| 1769 | retval->set_enum(t3rp4u(buf)); |
| 1770 | break; |
| 1771 | |
| 1772 | case VMOBJFILE_TAG_STRING: |
| 1773 | /* |
| 1774 | * read the string's length - note that this length is two |
| 1775 | * higher than the actual length of the string, because it |
| 1776 | * includes the length prefix bytes |
| 1777 | */ |
| 1778 | if (fp->read(buf, 2)) |
| 1779 | goto io_error; |
| 1780 | |
| 1781 | /* |
| 1782 | * allocate a new string of the required size (deducting two |
| 1783 | * bytes from the indicated size, since the string allocator |
| 1784 | * only wants to know about the bytes of the string we want to |
| 1785 | * store, not the length prefix part) |
| 1786 | */ |
| 1787 | str_id = CVmObjString::create(vmg_ FALSE, osrp2(buf) - 2); |
| 1788 | str_obj = (CVmObjString *)vm_objp(vmg_ str_id); |
| 1789 | |
| 1790 | /* read the bytes of the string into the object's buffer */ |
| 1791 | if (fp->read(str_obj->cons_get_buf(), osrp2(buf) - 2)) |
| 1792 | goto io_error; |
| 1793 | |
| 1794 | /* success - set the string return value, and we're done */ |
| 1795 | retval->set_obj(str_id); |
| 1796 | break; |
| 1797 | |
| 1798 | case VMOBJFILE_TAG_TRUE: |
| 1799 | /* it's a simple 'true' value */ |
| 1800 | retval->set_true(); |
| 1801 | break; |
| 1802 | |
| 1803 | case VMOBJFILE_TAG_BIGNUM: |
| 1804 | /* read the BigNumber value and return a new BigNumber object */ |
| 1805 | if (CVmObjBigNum::read_from_data_file(vmg_ retval, fp)) |
| 1806 | goto io_error; |
| 1807 | break; |
| 1808 | |
| 1809 | case VMOBJFILE_TAG_BYTEARRAY: |
| 1810 | /* read the ByteArray value and return a new ByteArray object */ |
| 1811 | if (CVmObjByteArray::read_from_data_file(vmg_ retval, fp)) |
| 1812 | goto io_error; |
| 1813 | break; |
| 1814 | |
| 1815 | default: |
| 1816 | /* invalid data - throw an error */ |
| 1817 | G_interpreter->throw_new_class(vmg_ G_predef->file_io_exc, |
| 1818 | 0, "file I/O error"); |
| 1819 | } |
| 1820 | |
| 1821 | /* done */ |
| 1822 | return; |
| 1823 | |
| 1824 | io_error: |
| 1825 | /* |
| 1826 | * we'll come here if we read the type tag correctly but encounter |
| 1827 | * an I/O error reading the value - this indicates a corrupted input |
| 1828 | * stream, so throw an I/O error |
| 1829 | */ |
| 1830 | G_interpreter->throw_new_class(vmg_ G_predef->file_io_exc, |
| 1831 | 0, "file I/O error"); |
| 1832 | } |
| 1833 | |
| 1834 | /* |
| 1835 | * Internal read routine |
| 1836 | */ |
| 1837 | int CVmObjFile::read_file(VMG_ char *buf, int32_t &len) |
| 1838 | { |
| 1839 | /* get the data source */ |
| 1840 | CVmDataSource *fp = get_ext()->fp; |
| 1841 | |
| 1842 | /* make sure it's valid */ |
| 1843 | if ((get_ext()->flags & VMOBJFILE_OUT_OF_SYNC) != 0 || fp == 0) |
| 1844 | return FALSE; |
| 1845 | |
| 1846 | /* deal with stdio buffering if we're changing modes */ |
| 1847 | switch_read_write_mode(vmg_ FALSE); |
| 1848 | |
| 1849 | /* read according to the mode */ |
| 1850 | switch (get_ext()->mode) |
| 1851 | { |
| 1852 | case VMOBJFILE_MODE_TEXT: |
| 1853 | { |
| 1854 | /* get the read buffer and character mapper */ |
| 1855 | vmobjfile_readbuf_t *readbuf = get_ext()->readbuf; |
| 1856 | CCharmapToUni *charmap = |
| 1857 | ((CVmObjCharSet *)vm_objp(vmg_ get_ext()->charset)) |
| 1858 | ->get_to_uni(vmg0_); |
| 1859 | |
| 1860 | /* loop until we satisfy the request */ |
| 1861 | int32_t actual; |
| 1862 | for (actual = 0 ; ; ) |
| 1863 | { |
| 1864 | /* peek at the next character */ |
| 1865 | wchar_t ch; |
| 1866 | size_t b; |
| 1867 | if (!readbuf->peekch(ch, fp, charmap, b)) |
| 1868 | break; |
| 1869 | |
| 1870 | /* make sure it'll fit */ |
| 1871 | size_t csiz = utf8_ptr::s_wchar_size(ch); |
| 1872 | if (actual + (int32_t)csiz > len) |
| 1873 | break; |
| 1874 | |
| 1875 | /* store this character */ |
| 1876 | actual += utf8_ptr::s_putch(buf + actual, ch); |
| 1877 | |
| 1878 | /* advance to the next character */ |
| 1879 | readbuf->commit_peek(b); |
| 1880 | } |
| 1881 | |
| 1882 | /* set the actual return length */ |
| 1883 | len = actual; |
| 1884 | } |
| 1885 | break; |
| 1886 | |
| 1887 | case VMOBJFILE_MODE_RAW: |
| 1888 | /* do the direct read */ |
| 1889 | len = fp->readc(buf, (size_t)len); |
| 1890 | break; |
| 1891 | |
| 1892 | default: |
| 1893 | /* can't handle others */ |
| 1894 | return FALSE; |
| 1895 | } |
| 1896 | |
| 1897 | /* success */ |
| 1898 | return TRUE; |
| 1899 | } |
| 1900 | |
| 1901 | |
| 1902 | /* ------------------------------------------------------------------------ */ |
| 1903 | /* |
| 1904 | * Property evaluator - write to the file |
| 1905 | */ |
| 1906 | int CVmObjFile::getp_write_file(VMG_ vm_obj_id_t self, vm_val_t *retval, |
| 1907 | uint *argc) |
| 1908 | { |
| 1909 | static CVmNativeCodeDesc desc(1); |
| 1910 | const vm_val_t *argval; |
| 1911 | |
| 1912 | /* check arguments */ |
| 1913 | if (get_prop_check_argc(retval, argc, &desc)) |
| 1914 | return TRUE; |
| 1915 | |
| 1916 | /* |
| 1917 | * get a pointer to the argument value, but leave it on the stack |
| 1918 | * for now to protect against losing it in garbage collection |
| 1919 | */ |
| 1920 | argval = G_stk->get(0); |
| 1921 | |
| 1922 | /* push a self-reference for gc protection */ |
| 1923 | G_stk->push()->set_obj(self); |
| 1924 | |
| 1925 | /* make sure we are allowed to perform operations on the file */ |
| 1926 | check_valid_file(vmg0_); |
| 1927 | |
| 1928 | /* check that we have write access */ |
| 1929 | check_write_access(vmg0_); |
| 1930 | |
| 1931 | /* deal with stdio buffering if we're changing modes */ |
| 1932 | switch_read_write_mode(vmg_ TRUE); |
| 1933 | |
| 1934 | /* read according to our mode */ |
| 1935 | switch(get_ext()->mode) |
| 1936 | { |
| 1937 | case VMOBJFILE_MODE_TEXT: |
| 1938 | /* read a line of text */ |
| 1939 | write_text_mode(vmg_ argval); |
| 1940 | break; |
| 1941 | |
| 1942 | case VMOBJFILE_MODE_DATA: |
| 1943 | /* read in data mode */ |
| 1944 | write_data_mode(vmg_ argval); |
| 1945 | break; |
| 1946 | |
| 1947 | case VMOBJFILE_MODE_RAW: |
| 1948 | /* can't use this call on this type of file */ |
| 1949 | G_interpreter->throw_new_class(vmg_ G_predef->file_mode_exc, |
| 1950 | 0, "wrong file mode"); |
| 1951 | break; |
| 1952 | } |
| 1953 | |
| 1954 | /* discard our gc protection and argument */ |
| 1955 | G_stk->discard(2); |
| 1956 | |
| 1957 | /* no return value - return nil by default */ |
| 1958 | retval->set_nil(); |
| 1959 | |
| 1960 | /* handled */ |
| 1961 | return TRUE; |
| 1962 | } |
| 1963 | |
| 1964 | /* |
| 1965 | * Write a value in text mode |
| 1966 | */ |
| 1967 | void CVmObjFile::write_text_mode(VMG_ const vm_val_t *val) |
| 1968 | { |
| 1969 | char conv_buf[128]; |
| 1970 | vm_val_t new_str; |
| 1971 | CCharmapToLocal *charmap; |
| 1972 | const char *constp; |
| 1973 | const char *p, *startp; |
| 1974 | size_t rem; |
| 1975 | |
| 1976 | /* get our character mapper */ |
| 1977 | charmap = ((CVmObjCharSet *)vm_objp(vmg_ get_ext()->charset)) |
| 1978 | ->get_to_local(vmg0_); |
| 1979 | |
| 1980 | /* convert the value to a string */ |
| 1981 | constp = CVmObjString::cvt_to_str( |
| 1982 | vmg_ &new_str, conv_buf, sizeof(conv_buf), val, 10, 0); |
| 1983 | |
| 1984 | /* protect the new string (if any) from gc while working */ |
| 1985 | G_stk->push(&new_str); |
| 1986 | |
| 1987 | /* scan for newlines - we need to write newline sequences specially */ |
| 1988 | for (startp = constp + VMB_LEN, rem = vmb_get_len(constp) ; |
| 1989 | rem != 0 ; ) |
| 1990 | { |
| 1991 | /* scan to the next newline */ |
| 1992 | for (p = startp ; rem != 0 && *p != '\n' ; ++p, --rem) ; |
| 1993 | |
| 1994 | /* write this chunk through the character mapper */ |
| 1995 | if (p != startp |
| 1996 | && charmap->write_file(get_ext()->fp, startp, p - startp)) |
| 1997 | { |
| 1998 | /* the write failed - throw an I/O exception */ |
| 1999 | G_interpreter->throw_new_class(vmg_ G_predef->file_io_exc, |
| 2000 | 0, "file I/O error"); |
| 2001 | } |
| 2002 | |
| 2003 | /* write the newline, if applicable */ |
| 2004 | if (rem != 0 |
| 2005 | && charmap->write_file(get_ext()->fp, OS_NEWLINE_SEQ, |
| 2006 | strlen(OS_NEWLINE_SEQ))) |
| 2007 | { |
| 2008 | /* the write failed - throw an I/O exception */ |
| 2009 | G_interpreter->throw_new_class(vmg_ G_predef->file_io_exc, |
| 2010 | 0, "file I/O error"); |
| 2011 | } |
| 2012 | |
| 2013 | /* if we're at a newline, skip it */ |
| 2014 | if (rem != 0) |
| 2015 | ++p, --rem; |
| 2016 | |
| 2017 | /* note where the new chunk starts */ |
| 2018 | startp = p; |
| 2019 | } |
| 2020 | |
| 2021 | /* done with the gc protection */ |
| 2022 | G_stk->discard(); |
| 2023 | |
| 2024 | /* invalidate any read buffer */ |
| 2025 | note_file_seek(vmg_ FALSE); |
| 2026 | } |
| 2027 | |
| 2028 | /* |
| 2029 | * Write a value in 'data' mode |
| 2030 | */ |
| 2031 | void CVmObjFile::write_data_mode(VMG_ const vm_val_t *val) |
| 2032 | { |
| 2033 | char buf[32]; |
| 2034 | CVmDataSource *fp = get_ext()->fp; |
| 2035 | vm_val_t new_str; |
| 2036 | const char *constp; |
| 2037 | |
| 2038 | /* see what type of data we want to put */ |
| 2039 | switch(val->typ) |
| 2040 | { |
| 2041 | case VM_INT: |
| 2042 | /* put the type in the buffer */ |
| 2043 | buf[0] = VMOBJFILE_TAG_INT; |
| 2044 | |
| 2045 | /* add the value in INT4 format */ |
| 2046 | oswp4s(buf + 1, val->val.intval); |
| 2047 | |
| 2048 | /* write out the type prefix plus the value */ |
| 2049 | if (fp->write(buf, 5)) |
| 2050 | goto io_error; |
| 2051 | |
| 2052 | /* done */ |
| 2053 | break; |
| 2054 | |
| 2055 | case VM_ENUM: |
| 2056 | /* put the type in the buffer */ |
| 2057 | buf[0] = VMOBJFILE_TAG_ENUM; |
| 2058 | |
| 2059 | /* add the value in INT4 format */ |
| 2060 | oswp4(buf + 1, val->val.enumval); |
| 2061 | |
| 2062 | /* write out the type prefix plus the value */ |
| 2063 | if (fp->write(buf, 5)) |
| 2064 | goto io_error; |
| 2065 | |
| 2066 | /* done */ |
| 2067 | break; |
| 2068 | |
| 2069 | case VM_SSTRING: |
| 2070 | /* get the string value pointer */ |
| 2071 | constp = val->get_as_string(vmg0_); |
| 2072 | |
| 2073 | write_binary_string: |
| 2074 | /* write the type prefix byte */ |
| 2075 | buf[0] = VMOBJFILE_TAG_STRING; |
| 2076 | if (fp->write(buf, 1)) |
| 2077 | goto io_error; |
| 2078 | |
| 2079 | /* |
| 2080 | * write the length prefix - for TADS 2 compatibility, include |
| 2081 | * the bytes of the prefix itself in the length count |
| 2082 | */ |
| 2083 | oswp2(buf, vmb_get_len(constp) + 2); |
| 2084 | if (fp->write(buf, 2)) |
| 2085 | goto io_error; |
| 2086 | |
| 2087 | /* write the string's bytes */ |
| 2088 | if (fp->write(constp + VMB_LEN, vmb_get_len(constp))) |
| 2089 | goto io_error; |
| 2090 | |
| 2091 | /* done */ |
| 2092 | break; |
| 2093 | |
| 2094 | case VM_OBJ: |
| 2095 | /* |
| 2096 | * Write BigNumber and ByteArray types in special formats. For |
| 2097 | * other types, try converting to a string. |
| 2098 | */ |
| 2099 | if (CVmObjBigNum::is_bignum_obj(vmg_ val->val.obj)) |
| 2100 | { |
| 2101 | CVmObjBigNum *bignum; |
| 2102 | |
| 2103 | /* we know it's a BigNumber - cast it properly */ |
| 2104 | bignum = (CVmObjBigNum *)vm_objp(vmg_ val->val.obj); |
| 2105 | |
| 2106 | /* write the type tag */ |
| 2107 | buf[0] = VMOBJFILE_TAG_BIGNUM; |
| 2108 | if (fp->write(buf, 1)) |
| 2109 | goto io_error; |
| 2110 | |
| 2111 | /* write it out */ |
| 2112 | if (bignum->write_to_data_file(fp)) |
| 2113 | goto io_error; |
| 2114 | } |
| 2115 | else if (CVmObjByteArray::is_byte_array(vmg_ val->val.obj)) |
| 2116 | { |
| 2117 | CVmObjByteArray *bytarr; |
| 2118 | |
| 2119 | /* we know it's a ByteArray - cast it properly */ |
| 2120 | bytarr = (CVmObjByteArray *)vm_objp(vmg_ val->val.obj); |
| 2121 | |
| 2122 | /* write the type tag */ |
| 2123 | buf[0] = VMOBJFILE_TAG_BYTEARRAY; |
| 2124 | if (fp->write(buf, 1)) |
| 2125 | goto io_error; |
| 2126 | |
| 2127 | /* write the array */ |
| 2128 | if (bytarr->write_to_data_file(fp)) |
| 2129 | goto io_error; |
| 2130 | } |
| 2131 | else |
| 2132 | { |
| 2133 | /* |
| 2134 | * Cast it to a string value and write that out. Note that |
| 2135 | * we can ignore garbage collection for any new string we've |
| 2136 | * created, since we're just calling the OS-level file |
| 2137 | * writer, which will never invoke garbage collection. |
| 2138 | */ |
| 2139 | constp = vm_objp(vmg_ val->val.obj) |
| 2140 | ->cast_to_string(vmg_ val->val.obj, &new_str); |
| 2141 | goto write_binary_string; |
| 2142 | } |
| 2143 | break; |
| 2144 | |
| 2145 | case VM_TRUE: |
| 2146 | /* |
| 2147 | * All we need for this is the type tag. Note that we can't |
| 2148 | * write nil because we'd have no way of reading it back in - a |
| 2149 | * nil return from file_read indicates that we've reached the |
| 2150 | * end of the file. So there's no point in writing nil to a |
| 2151 | * file. |
| 2152 | */ |
| 2153 | buf[0] = VMOBJFILE_TAG_TRUE; |
| 2154 | if (fp->write(buf, 1)) |
| 2155 | goto io_error; |
| 2156 | |
| 2157 | /* done */ |
| 2158 | break; |
| 2159 | |
| 2160 | default: |
| 2161 | /* other types are not acceptable */ |
| 2162 | err_throw(VMERR_BAD_TYPE_BIF); |
| 2163 | } |
| 2164 | |
| 2165 | /* done */ |
| 2166 | return; |
| 2167 | |
| 2168 | io_error: |
| 2169 | /* the write failed - throw an i/o exception */ |
| 2170 | G_interpreter->throw_new_class(vmg_ G_predef->file_io_exc, |
| 2171 | 0, "file I/O error"); |
| 2172 | } |
| 2173 | |
| 2174 | |
| 2175 | /* ------------------------------------------------------------------------ */ |
| 2176 | /* |
| 2177 | * Property evaluator - read raw bytes from the file |
| 2178 | */ |
| 2179 | int CVmObjFile::getp_read_bytes(VMG_ vm_obj_id_t self, vm_val_t *retval, |
| 2180 | uint *in_argc) |
| 2181 | { |
| 2182 | static CVmNativeCodeDesc desc(1, 2); |
| 2183 | uint argc = (in_argc != 0 ? *in_argc : 0); |
| 2184 | vm_val_t arr_val; |
| 2185 | CVmObjByteArray *arr; |
| 2186 | unsigned long idx; |
| 2187 | unsigned long len; |
| 2188 | |
| 2189 | /* check arguments */ |
| 2190 | if (get_prop_check_argc(retval, in_argc, &desc)) |
| 2191 | return TRUE; |
| 2192 | |
| 2193 | /* make sure we are allowed to perform operations on the file */ |
| 2194 | check_valid_file(vmg0_); |
| 2195 | |
| 2196 | /* check that we have read access */ |
| 2197 | check_read_access(vmg0_); |
| 2198 | |
| 2199 | /* we can only use this call on 'raw' files */ |
| 2200 | if (get_ext()->mode != VMOBJFILE_MODE_RAW) |
| 2201 | G_interpreter->throw_new_class(vmg_ G_predef->file_mode_exc, |
| 2202 | 0, "wrong file mode"); |
| 2203 | |
| 2204 | /* retrieve the ByteArray destination */ |
| 2205 | G_stk->pop(&arr_val); |
| 2206 | |
| 2207 | /* make sure it's really a ByteArray object */ |
| 2208 | if (arr_val.typ != VM_OBJ |
| 2209 | || !CVmObjByteArray::is_byte_array(vmg_ arr_val.val.obj)) |
| 2210 | err_throw(VMERR_BAD_TYPE_BIF); |
| 2211 | |
| 2212 | /* we know it's a byte array object, so cast it */ |
| 2213 | arr = (CVmObjByteArray *)vm_objp(vmg_ arr_val.val.obj); |
| 2214 | |
| 2215 | /* presume we'll try to fill the entire array */ |
| 2216 | idx = 1; |
| 2217 | len = arr->get_element_count(); |
| 2218 | |
| 2219 | /* if we have a starting index argument, retrieve it */ |
| 2220 | if (argc >= 2) |
| 2221 | idx = (unsigned long)CVmBif::pop_int_val(vmg0_); |
| 2222 | |
| 2223 | /* if we have a length argument, retrieve it */ |
| 2224 | if (argc >= 3) |
| 2225 | len = (unsigned long)CVmBif::pop_int_val(vmg0_); |
| 2226 | |
| 2227 | /* push a self-reference for gc protection */ |
| 2228 | G_stk->push()->set_obj(self); |
| 2229 | |
| 2230 | /* note the implicit seeking */ |
| 2231 | note_file_seek(vmg_ FALSE); |
| 2232 | |
| 2233 | /* deal with stdio buffering issues if necessary */ |
| 2234 | switch_read_write_mode(vmg_ FALSE); |
| 2235 | |
| 2236 | /* |
| 2237 | * read the data into the array, and return the number of bytes we |
| 2238 | * actually manage to read |
| 2239 | */ |
| 2240 | retval->set_int(arr->read_from_file( |
| 2241 | vmg_ arr_val.val.obj, get_ext()->fp, idx, len, TRUE)); |
| 2242 | |
| 2243 | /* discard our gc protection */ |
| 2244 | G_stk->discard(); |
| 2245 | |
| 2246 | /* handled */ |
| 2247 | return TRUE; |
| 2248 | } |
| 2249 | |
| 2250 | /* ------------------------------------------------------------------------ */ |
| 2251 | /* |
| 2252 | * Property evaluator - write raw bytes to the file |
| 2253 | */ |
| 2254 | int CVmObjFile::getp_write_bytes(VMG_ vm_obj_id_t self, vm_val_t *retval, |
| 2255 | uint *in_argc) |
| 2256 | { |
| 2257 | static CVmNativeCodeDesc desc(1, 2); |
| 2258 | uint argc = (in_argc != 0 ? *in_argc : 0); |
| 2259 | vm_val_t srcval; |
| 2260 | CVmObjByteArray *arr = 0; |
| 2261 | CVmObjFile *file = 0; |
| 2262 | unsigned long idx, has_idx = FALSE; |
| 2263 | unsigned long len; |
| 2264 | |
| 2265 | /* check arguments */ |
| 2266 | if (get_prop_check_argc(retval, in_argc, &desc)) |
| 2267 | return TRUE; |
| 2268 | |
| 2269 | /* make sure it's a valid file, with write access, in raw mode */ |
| 2270 | check_valid_file(vmg0_); |
| 2271 | check_write_access(vmg0_); |
| 2272 | check_raw_mode(vmg0_); |
| 2273 | |
| 2274 | /* pop the source object argument */ |
| 2275 | G_stk->pop(&srcval); |
| 2276 | if (srcval.typ != VM_OBJ) |
| 2277 | err_throw(VMERR_OBJ_VAL_REQD); |
| 2278 | |
| 2279 | /* check the object type */ |
| 2280 | if (CVmObjByteArray::is_byte_array(vmg_ srcval.val.obj)) |
| 2281 | { |
| 2282 | /* get the ByteArray object, suitably cast */ |
| 2283 | arr = (CVmObjByteArray *)vm_objp(vmg_ srcval.val.obj); |
| 2284 | |
| 2285 | /* assume we'll write the entire byte array */ |
| 2286 | idx = 1; |
| 2287 | len = arr->get_element_count(); |
| 2288 | } |
| 2289 | else if (CVmObjFile::is_file_obj(vmg_ srcval.val.obj)) |
| 2290 | { |
| 2291 | /* get the file object, suitably cast */ |
| 2292 | file = (CVmObjFile *)vm_objp(vmg_ srcval.val.obj); |
| 2293 | |
| 2294 | /* make sure it's in raw mode */ |
| 2295 | if (file->get_ext()->mode != VMOBJFILE_MODE_RAW) |
| 2296 | G_interpreter->throw_new_class(vmg_ G_predef->file_mode_exc, |
| 2297 | 0, "wrong file mode"); |
| 2298 | |
| 2299 | /* assume we'll copy the file from the current seek offset onward */ |
| 2300 | idx = file->get_pos(vmg0_); |
| 2301 | len = file->get_file_size(vmg0_); |
| 2302 | } |
| 2303 | else |
| 2304 | { |
| 2305 | /* invalid source type */ |
| 2306 | err_throw(VMERR_BAD_TYPE_BIF); |
| 2307 | } |
| 2308 | |
| 2309 | /* if we have a starting index, retrieve it */ |
| 2310 | if (argc >= 2) |
| 2311 | { |
| 2312 | /* if it's nil, just ignore it, otherwise pop the int value */ |
| 2313 | if (G_stk->get(0)->typ == VM_NIL) |
| 2314 | G_stk->discard(); |
| 2315 | else |
| 2316 | { |
| 2317 | idx = (unsigned long)CVmBif::pop_int_val(vmg0_); |
| 2318 | has_idx = TRUE; |
| 2319 | } |
| 2320 | } |
| 2321 | |
| 2322 | /* if we have a length, retrieve it */ |
| 2323 | if (argc >= 3) |
| 2324 | { |
| 2325 | /* if it's nil, just ignore it, otherwise pop the int value */ |
| 2326 | if (G_stk->get(0)->typ == VM_NIL) |
| 2327 | G_stk->discard(); |
| 2328 | else |
| 2329 | len = (unsigned long)CVmBif::pop_int_val(vmg0_); |
| 2330 | } |
| 2331 | |
| 2332 | /* push a self-reference and a source reference for gc protection */ |
| 2333 | G_stk->push()->set_obj(self); |
| 2334 | G_stk->push(&srcval); |
| 2335 | |
| 2336 | /* flush stdio buffers as needed and note the read operation */ |
| 2337 | switch_read_write_mode(vmg_ TRUE); |
| 2338 | |
| 2339 | /* do the copy according to the source type */ |
| 2340 | if (arr != 0) |
| 2341 | { |
| 2342 | /* |
| 2343 | * write the bytes to the file - on success (zero write_to_file |
| 2344 | * return), return nil, on failure (non-zero write_to_file return), |
| 2345 | * return true |
| 2346 | */ |
| 2347 | if (arr->write_to_file(get_ext()->fp, idx, len)) |
| 2348 | { |
| 2349 | /* we failed to write the bytes - throw an I/O exception */ |
| 2350 | G_interpreter->throw_new_class(vmg_ G_predef->file_io_exc, |
| 2351 | 0, "file I/O error"); |
| 2352 | } |
| 2353 | } |
| 2354 | else if (file != 0) |
| 2355 | { |
| 2356 | /* |
| 2357 | * if there's an explicit starting seek position, go there; if it |
| 2358 | * wasn't specified, the default is the current seek location, so |
| 2359 | * we just leave well enough alone in that case |
| 2360 | */ |
| 2361 | if (has_idx) |
| 2362 | file->set_pos(vmg_ idx); |
| 2363 | |
| 2364 | /* copy the requested byte range */ |
| 2365 | while (len != 0) |
| 2366 | { |
| 2367 | /* read the next chunk of bytes */ |
| 2368 | char buf[1024]; |
| 2369 | int32_t cur = (len < (int32_t)sizeof(buf) ? len : sizeof(buf)); |
| 2370 | if (!file->read_file(vmg_ buf, cur)) |
| 2371 | G_interpreter->throw_new_class(vmg_ G_predef->file_io_exc, |
| 2372 | 0, "file I/O error"); |
| 2373 | |
| 2374 | /* if we read zero bytes, we've reached EOF */ |
| 2375 | if (cur == 0) |
| 2376 | break; |
| 2377 | |
| 2378 | /* write out this chunk */ |
| 2379 | if (get_ext()->fp->write(buf, (size_t)cur)) |
| 2380 | G_interpreter->throw_new_class(vmg_ G_predef->file_io_exc, |
| 2381 | 0, "file I/O error"); |
| 2382 | |
| 2383 | /* deduct this hunk from the remaining length */ |
| 2384 | len -= cur; |
| 2385 | } |
| 2386 | } |
| 2387 | |
| 2388 | /* discard our gc protection */ |
| 2389 | G_stk->discard(2); |
| 2390 | |
| 2391 | /* no return value - return nil by default */ |
| 2392 | retval->set_nil(); |
| 2393 | |
| 2394 | /* handled */ |
| 2395 | return TRUE; |
| 2396 | } |
| 2397 | |
| 2398 | /* ------------------------------------------------------------------------ */ |
| 2399 | /* |
| 2400 | * Property evaluator - get the seek position |
| 2401 | */ |
| 2402 | int CVmObjFile::getp_get_pos(VMG_ vm_obj_id_t self, vm_val_t *retval, |
| 2403 | uint *argc) |
| 2404 | { |
| 2405 | static CVmNativeCodeDesc desc(0); |
| 2406 | |
| 2407 | /* check arguments */ |
| 2408 | if (get_prop_check_argc(retval, argc, &desc)) |
| 2409 | return TRUE; |
| 2410 | |
| 2411 | /* make sure we are allowed to perform operations on the file */ |
| 2412 | check_valid_file(vmg0_); |
| 2413 | |
| 2414 | /* get the position */ |
| 2415 | retval->set_int(get_pos(vmg0_)); |
| 2416 | |
| 2417 | /* handled */ |
| 2418 | return TRUE; |
| 2419 | } |
| 2420 | |
| 2421 | /* |
| 2422 | * Get the current file position |
| 2423 | */ |
| 2424 | long CVmObjFile::get_pos(VMG0_) |
| 2425 | { |
| 2426 | /* get the seek position in the underlying file */ |
| 2427 | long pos = get_ext()->fp->get_pos(); |
| 2428 | |
| 2429 | /* |
| 2430 | * if we have a read buffer, adjust for buffering: the underlying file |
| 2431 | * corresponds to the end of the read buffer, so subtract the number of |
| 2432 | * bytes remaining in the read buffer |
| 2433 | */ |
| 2434 | if (get_ext()->readbuf != 0) |
| 2435 | pos -= get_ext()->readbuf->rem; |
| 2436 | |
| 2437 | /* return the result */ |
| 2438 | return pos; |
| 2439 | } |
| 2440 | |
| 2441 | /* ------------------------------------------------------------------------ */ |
| 2442 | /* |
| 2443 | * Property evaluator - set the seek position |
| 2444 | */ |
| 2445 | int CVmObjFile::getp_set_pos(VMG_ vm_obj_id_t self, vm_val_t *retval, |
| 2446 | uint *argc) |
| 2447 | { |
| 2448 | static CVmNativeCodeDesc desc(1); |
| 2449 | unsigned long pos; |
| 2450 | |
| 2451 | /* check arguments */ |
| 2452 | if (get_prop_check_argc(retval, argc, &desc)) |
| 2453 | return TRUE; |
| 2454 | |
| 2455 | /* make sure we are allowed to perform operations on the file */ |
| 2456 | check_valid_file(vmg0_); |
| 2457 | |
| 2458 | /* note the seeking operation */ |
| 2459 | note_file_seek(vmg_ TRUE); |
| 2460 | |
| 2461 | /* retrieve the target seek position */ |
| 2462 | pos = CVmBif::pop_long_val(vmg0_); |
| 2463 | |
| 2464 | /* seek to the new position */ |
| 2465 | get_ext()->fp->seek(pos, OSFSK_SET); |
| 2466 | |
| 2467 | /* no return value */ |
| 2468 | retval->set_nil(); |
| 2469 | |
| 2470 | /* handled */ |
| 2471 | return TRUE; |
| 2472 | } |
| 2473 | |
| 2474 | /* |
| 2475 | * Set position - internal version |
| 2476 | */ |
| 2477 | int CVmObjFile::set_pos(VMG_ long pos) |
| 2478 | { |
| 2479 | /* make sure it's valid */ |
| 2480 | if ((get_ext()->flags & VMOBJFILE_OUT_OF_SYNC) != 0 |
| 2481 | || get_ext()->fp == 0) |
| 2482 | return FALSE; |
| 2483 | |
| 2484 | /* note the seek */ |
| 2485 | note_file_seek(vmg_ TRUE); |
| 2486 | |
| 2487 | /* set the seek position */ |
| 2488 | return !get_ext()->fp->seek(pos, OSFSK_SET); |
| 2489 | } |
| 2490 | |
| 2491 | /* ------------------------------------------------------------------------ */ |
| 2492 | /* |
| 2493 | * Property evaluator - set position to end of file |
| 2494 | */ |
| 2495 | int CVmObjFile::getp_set_pos_end(VMG_ vm_obj_id_t self, vm_val_t *retval, |
| 2496 | uint *argc) |
| 2497 | { |
| 2498 | static CVmNativeCodeDesc desc(0); |
| 2499 | |
| 2500 | /* check arguments */ |
| 2501 | if (get_prop_check_argc(retval, argc, &desc)) |
| 2502 | return TRUE; |
| 2503 | |
| 2504 | /* make sure we are allowed to perform operations on the file */ |
| 2505 | check_valid_file(vmg0_); |
| 2506 | |
| 2507 | /* note the seeking operation */ |
| 2508 | note_file_seek(vmg_ TRUE); |
| 2509 | |
| 2510 | /* seek to the end position */ |
| 2511 | get_ext()->fp->seek(0, OSFSK_END); |
| 2512 | |
| 2513 | /* no return value */ |
| 2514 | retval->set_nil(); |
| 2515 | |
| 2516 | /* handled */ |
| 2517 | return TRUE; |
| 2518 | } |
| 2519 | |
| 2520 | /* ------------------------------------------------------------------------ */ |
| 2521 | /* |
| 2522 | * Property evaluator - get size |
| 2523 | */ |
| 2524 | int CVmObjFile::getp_get_size(VMG_ vm_obj_id_t self, vm_val_t *retval, |
| 2525 | uint *argc) |
| 2526 | { |
| 2527 | static CVmNativeCodeDesc desc(0); |
| 2528 | |
| 2529 | /* check arguments */ |
| 2530 | if (get_prop_check_argc(retval, argc, &desc)) |
| 2531 | return TRUE; |
| 2532 | |
| 2533 | /* make sure we are allowed to perform operations on the file */ |
| 2534 | check_valid_file(vmg0_); |
| 2535 | |
| 2536 | /* get the size */ |
| 2537 | retval->set_int(get_file_size(vmg0_)); |
| 2538 | |
| 2539 | /* handled */ |
| 2540 | return TRUE; |
| 2541 | } |
| 2542 | |
| 2543 | /* |
| 2544 | * Get the size, internal version |
| 2545 | */ |
| 2546 | long CVmObjFile::get_file_size(VMG0_) |
| 2547 | { |
| 2548 | /* make sure it's valid */ |
| 2549 | if ((get_ext()->flags & VMOBJFILE_OUT_OF_SYNC) != 0 |
| 2550 | || get_ext()->fp == 0) |
| 2551 | return -1; |
| 2552 | |
| 2553 | /* note the seeking operation */ |
| 2554 | note_file_seek(vmg_ TRUE); |
| 2555 | |
| 2556 | /* get the size */ |
| 2557 | return get_ext()->fp->get_size(); |
| 2558 | } |
| 2559 | |
| 2560 | /* ------------------------------------------------------------------------ */ |
| 2561 | /* |
| 2562 | * Property evaluator - get the file mode |
| 2563 | */ |
| 2564 | int CVmObjFile::getp_get_mode(VMG_ vm_obj_id_t self, vm_val_t *retval, |
| 2565 | uint *argc) |
| 2566 | { |
| 2567 | static CVmNativeCodeDesc desc(0); |
| 2568 | |
| 2569 | /* check arguments */ |
| 2570 | if (get_prop_check_argc(retval, argc, &desc)) |
| 2571 | return TRUE; |
| 2572 | |
| 2573 | /* make sure we are allowed to perform operations on the file */ |
| 2574 | check_valid_file(vmg0_); |
| 2575 | |
| 2576 | /* return the mode */ |
| 2577 | retval->set_int(get_ext()->mode); |
| 2578 | |
| 2579 | /* handled */ |
| 2580 | return TRUE; |
| 2581 | } |
| 2582 | |
| 2583 | /* |
| 2584 | * Get the mode, internal version |
| 2585 | */ |
| 2586 | int CVmObjFile::get_file_mode(VMG0_) |
| 2587 | { |
| 2588 | return get_ext()->mode; |
| 2589 | } |
| 2590 | |
| 2591 | |
| 2592 | /* ------------------------------------------------------------------------ */ |
| 2593 | /* |
| 2594 | * Property evaluator - setMode - change the file mode |
| 2595 | */ |
| 2596 | int CVmObjFile::getp_setMode(VMG_ vm_obj_id_t self, vm_val_t *retval, |
| 2597 | uint *oargc) |
| 2598 | { |
| 2599 | /* check arguments */ |
| 2600 | static CVmNativeCodeDesc desc(1, 1); |
| 2601 | int argc = (oargc != 0 ? *oargc : 0); |
| 2602 | if (get_prop_check_argc(retval, oargc, &desc)) |
| 2603 | return TRUE; |
| 2604 | |
| 2605 | /* make sure we are allowed to perform operations on the file */ |
| 2606 | check_valid_file(vmg0_); |
| 2607 | |
| 2608 | /* retrieve the mode argument */ |
| 2609 | int mode = CVmBif::pop_int_val(vmg0_); |
| 2610 | --argc; |
| 2611 | |
| 2612 | /* presume no character set or read buffer */ |
| 2613 | vm_obj_id_t cset = VM_INVALID_OBJ; |
| 2614 | int create_readbuf = FALSE; |
| 2615 | |
| 2616 | /* save the retained settings from the current extension */ |
| 2617 | vmobjfile_ext_t *ext = get_ext(); |
| 2618 | CVmDataSource *fp = ext->fp; |
| 2619 | CVmNetFile *netfile = ext->netfile; |
| 2620 | unsigned long flags = ext->flags; |
| 2621 | int access = ext->access; |
| 2622 | |
| 2623 | /* check the mode */ |
| 2624 | switch (mode) |
| 2625 | { |
| 2626 | case VMOBJFILE_MODE_TEXT: |
| 2627 | /* text mode - we need a character mapper in this mode */ |
| 2628 | cset = get_charset_arg(vmg_ 0, argc); |
| 2629 | |
| 2630 | /* |
| 2631 | * if we're open with read access, we need a read buffer in order |
| 2632 | * to do the character set translation |
| 2633 | */ |
| 2634 | if (access == VMOBJFILE_ACCESS_READ) |
| 2635 | create_readbuf = TRUE; |
| 2636 | break; |
| 2637 | |
| 2638 | case VMOBJFILE_MODE_DATA: |
| 2639 | case VMOBJFILE_MODE_RAW: |
| 2640 | /* |
| 2641 | * data mode or raw mode - we don't use a character mapper for this |
| 2642 | * mode, so if the argument is present, simply discard it |
| 2643 | */ |
| 2644 | if (argc >= 2) |
| 2645 | G_stk->discard(); |
| 2646 | break; |
| 2647 | |
| 2648 | default: |
| 2649 | /* invalid mode */ |
| 2650 | err_throw(VMERR_BAD_VAL_BIF); |
| 2651 | } |
| 2652 | |
| 2653 | /* push the character set object (if any) for gc protection */ |
| 2654 | G_stk->push_obj_or_nil(vmg_ cset); |
| 2655 | |
| 2656 | /* |
| 2657 | * remove the data source and network file descriptor from the |
| 2658 | * extension, so that they're not deleted when we reallocate it |
| 2659 | */ |
| 2660 | ext->fp = 0; |
| 2661 | ext->netfile = 0; |
| 2662 | |
| 2663 | /* |
| 2664 | * Reallocate the extension. This will set up the correct structures |
| 2665 | * for the new file mode. |
| 2666 | */ |
| 2667 | alloc_ext(vmg_ netfile, cset, fp, flags, mode, access, create_readbuf); |
| 2668 | |
| 2669 | /* discard arguments and gc protection */ |
| 2670 | G_stk->discard(argc + 1); |
| 2671 | |
| 2672 | /* handled */ |
| 2673 | return TRUE; |
| 2674 | } |
| 2675 | |
| 2676 | |
| 2677 | |
| 2678 | /* ------------------------------------------------------------------------ */ |
| 2679 | /* |
| 2680 | * getRootName property evaluator - get the root filename of a given |
| 2681 | * string. This is a static (class) method. |
| 2682 | */ |
| 2683 | int CVmObjFile::s_getp_getRootName(VMG_ vm_val_t *retval, uint *argc) |
| 2684 | { |
| 2685 | static CVmNativeCodeDesc desc(1); |
| 2686 | char buf[OSFNMAX*2]; |
| 2687 | const char *ret; |
| 2688 | |
| 2689 | /* check arguments */ |
| 2690 | if (get_prop_check_argc(retval, argc, &desc)) |
| 2691 | return TRUE; |
| 2692 | |
| 2693 | /* get the string argument into our buffer */ |
| 2694 | CVmBif::pop_fname_val(vmg_ buf, sizeof(buf)); |
| 2695 | |
| 2696 | /* set up a no-access file descriptor to determine where the file is */ |
| 2697 | CVmNetFile *nf = CVmNetFile::open(vmg_ buf, 0, 0, OSFTUNK, 0); |
| 2698 | |
| 2699 | /* |
| 2700 | * If the file is on the network storage server, we always use the Unix |
| 2701 | * convention, regardless of the local naming rules. Otherwise, use |
| 2702 | * the local OS API to get the root name. |
| 2703 | */ |
| 2704 | if (nf->is_net_file()) |
| 2705 | { |
| 2706 | /* |
| 2707 | * We're in network mode, so use the Unix path conventions. Simply |
| 2708 | * scan for the last '/' character; if we find it, the root name is |
| 2709 | * the part after the '/', otherwise it's the whole string. |
| 2710 | */ |
| 2711 | const char *sl = strrchr(buf, '/'); |
| 2712 | ret = (sl != 0 ? sl + 1 : buf); |
| 2713 | } |
| 2714 | else |
| 2715 | { |
| 2716 | /* it's not a network file - use the local filename rules */ |
| 2717 | ret = os_get_root_name(buf); |
| 2718 | } |
| 2719 | |
| 2720 | /* done with the net file desriptor */ |
| 2721 | nf->abandon(vmg0_); |
| 2722 | |
| 2723 | /* return the string */ |
| 2724 | retval->set_obj(CVmObjString::create(vmg_ FALSE, ret, strlen(ret))); |
| 2725 | |
| 2726 | /* handled */ |
| 2727 | return TRUE; |
| 2728 | } |
| 2729 | |
| 2730 | |
| 2731 | /* ------------------------------------------------------------------------ */ |
| 2732 | /* |
| 2733 | * deleteFile() property evaluator - deletes a file |
| 2734 | */ |
| 2735 | int CVmObjFile::s_getp_deleteFile(VMG_ vm_val_t *retval, uint *in_argc) |
| 2736 | { |
| 2737 | /* check arguments */ |
| 2738 | uint argc = (in_argc != 0 ? *in_argc : 0); |
| 2739 | static CVmNativeCodeDesc desc(1); |
| 2740 | if (get_prop_check_argc(retval, in_argc, &desc)) |
| 2741 | return TRUE; |
| 2742 | |
| 2743 | /* get the filename; use the DELETE access mode */ |
| 2744 | vm_rcdesc rc(vmg_ "File.deleteFile", |
| 2745 | CVmObjFile::metaclass_reg_->get_class_obj(vmg0_), |
| 2746 | VMOBJFILE_DELETE_FILE, G_stk->get(0), argc); |
| 2747 | int access = VMOBJFILE_ACCESS_DELETE; |
| 2748 | CVmNetFile *netfile = get_filename_and_access( |
| 2749 | vmg_ &rc, &access, FALSE, OSFTUNK, "application/octet-stream"); |
| 2750 | |
| 2751 | /* close the network file descriptor - this will delete the file */ |
| 2752 | netfile->close(vmg0_); |
| 2753 | |
| 2754 | /* discard arguments */ |
| 2755 | G_stk->discard(1); |
| 2756 | |
| 2757 | /* no return */ |
| 2758 | retval->set_nil(); |
| 2759 | |
| 2760 | /* handled */ |
| 2761 | return TRUE; |
| 2762 | } |
| 2763 | |
| 2764 | |
| 2765 | /* ------------------------------------------------------------------------ */ |
| 2766 | /* |
| 2767 | * property evaluator - unpackBytes |
| 2768 | */ |
| 2769 | int CVmObjFile::getp_unpackBytes(VMG_ vm_obj_id_t self, |
| 2770 | vm_val_t *retval, uint *oargc) |
| 2771 | { |
| 2772 | /* check arguments */ |
| 2773 | uint argc = (oargc != 0 ? *oargc : 0); |
| 2774 | static CVmNativeCodeDesc desc(1); |
| 2775 | if (get_prop_check_argc(retval, oargc, &desc)) |
| 2776 | return TRUE; |
| 2777 | |
| 2778 | /* check that the it's a valid file open with read access in "raw" mode */ |
| 2779 | check_raw_read(vmg0_); |
| 2780 | |
| 2781 | /* get the format string, but leave it on the stack */ |
| 2782 | const char *fmt = G_stk->get(0)->get_as_string(vmg0_); |
| 2783 | if (fmt == 0) |
| 2784 | err_throw(VMERR_BAD_TYPE_BIF); |
| 2785 | |
| 2786 | /* get the format string pointer and length */ |
| 2787 | size_t fmtlen = vmb_get_len(fmt); |
| 2788 | fmt += VMB_LEN; |
| 2789 | |
| 2790 | /* save 'self' for gc protection */ |
| 2791 | G_stk->push_obj(vmg_ self); |
| 2792 | |
| 2793 | /* unpack the data from our file stream into a list, returning the list */ |
| 2794 | CVmPack::unpack(vmg_ retval, fmt, fmtlen, get_ext()->fp); |
| 2795 | |
| 2796 | /* discard our arguments and gc protection */ |
| 2797 | G_stk->discard(argc + 1); |
| 2798 | |
| 2799 | /* handled */ |
| 2800 | return TRUE; |
| 2801 | } |
| 2802 | |
| 2803 | /* |
| 2804 | * property evaluator - packBytes |
| 2805 | */ |
| 2806 | int CVmObjFile::getp_packBytes(VMG_ vm_obj_id_t self, |
| 2807 | vm_val_t *retval, uint *oargc) |
| 2808 | { |
| 2809 | /* check arguments */ |
| 2810 | uint argc = (oargc != 0 ? *oargc : 0); |
| 2811 | static CVmNativeCodeDesc desc(1, 0, TRUE); |
| 2812 | if (get_prop_check_argc(retval, oargc, &desc)) |
| 2813 | return TRUE; |
| 2814 | |
| 2815 | /* check that the it's a valid file open with write access in "raw" mode */ |
| 2816 | check_raw_write(vmg0_); |
| 2817 | |
| 2818 | /* save 'self' for gc protection */ |
| 2819 | G_stk->push_obj(vmg_ self); |
| 2820 | |
| 2821 | /* remember the starting seek position */ |
| 2822 | long start = get_pos(vmg0_); |
| 2823 | |
| 2824 | /* pack the arguments into our file data stream */ |
| 2825 | CVmPack::pack(vmg_ 1, argc, get_ext()->fp); |
| 2826 | |
| 2827 | /* discard our gc protection and our arguments */ |
| 2828 | G_stk->discard(argc + 1); |
| 2829 | |
| 2830 | /* return the number of bytes packed */ |
| 2831 | retval->set_int(get_pos(vmg0_) - start); |
| 2832 | |
| 2833 | /* handled */ |
| 2834 | return TRUE; |
| 2835 | } |
| 2836 | |
| 2837 | |
| 2838 | /* |
| 2839 | * property evaluator - sha256 |
| 2840 | */ |
| 2841 | int CVmObjFile::getp_sha256(VMG_ vm_obj_id_t self, |
| 2842 | vm_val_t *retval, uint *oargc) |
| 2843 | { |
| 2844 | /* check arguments */ |
| 2845 | uint argc = (oargc != 0 ? *oargc : 0); |
| 2846 | static CVmNativeCodeDesc desc(0, 1); |
| 2847 | if (get_prop_check_argc(retval, oargc, &desc)) |
| 2848 | return TRUE; |
| 2849 | |
| 2850 | /* check that the it's a valid file open with read access in "raw" mode */ |
| 2851 | check_raw_read(vmg0_); |
| 2852 | |
| 2853 | /* retrieve the length value, if present; if not, use the file size */ |
| 2854 | long len = (argc >= 1 && G_stk->get(0)->typ != VM_NIL |
| 2855 | ? G_stk->get(0)->num_to_int(vmg0_) |
| 2856 | : get_file_size(vmg0_)); |
| 2857 | |
| 2858 | /* save 'self' for gc protection */ |
| 2859 | G_stk->push_obj(vmg_ self); |
| 2860 | |
| 2861 | /* calculate the hash */ |
| 2862 | char hash[65]; |
| 2863 | sha256_datasrc(hash, get_ext()->fp, len); |
| 2864 | |
| 2865 | /* return the hash value */ |
| 2866 | retval->set_obj(CVmObjString::create(vmg_ FALSE, hash, 64)); |
| 2867 | |
| 2868 | /* discard our gc protection and our arguments */ |
| 2869 | G_stk->discard(argc + 1); |
| 2870 | |
| 2871 | /* handled */ |
| 2872 | return TRUE; |
| 2873 | } |
| 2874 | |
| 2875 | |
| 2876 | /* |
| 2877 | * property evaluator - digestMD5 |
| 2878 | */ |
| 2879 | int CVmObjFile::getp_digestMD5(VMG_ vm_obj_id_t self, |
| 2880 | vm_val_t *retval, uint *oargc) |
| 2881 | { |
| 2882 | /* check arguments */ |
| 2883 | uint argc = (oargc != 0 ? *oargc : 0); |
| 2884 | static CVmNativeCodeDesc desc(0, 1); |
| 2885 | if (get_prop_check_argc(retval, oargc, &desc)) |
| 2886 | return TRUE; |
| 2887 | |
| 2888 | /* check that the it's a valid file open with read access in "raw" mode */ |
| 2889 | check_raw_read(vmg0_); |
| 2890 | |
| 2891 | /* retrieve the length value, if present; if not, use the file size */ |
| 2892 | long len = (argc >= 1 && G_stk->get(0)->typ != VM_NIL |
| 2893 | ? G_stk->get(0)->num_to_int(vmg0_) |
| 2894 | : get_file_size(vmg0_)); |
| 2895 | |
| 2896 | /* save 'self' for gc protection */ |
| 2897 | G_stk->push_obj(vmg_ self); |
| 2898 | |
| 2899 | /* calculate the hash */ |
| 2900 | char hash[33]; |
| 2901 | md5_datasrc(hash, get_ext()->fp, len); |
| 2902 | |
| 2903 | /* return the hash value */ |
| 2904 | retval->set_obj(CVmObjString::create(vmg_ FALSE, hash, 32)); |
| 2905 | |
| 2906 | /* discard our gc protection and our arguments */ |
| 2907 | G_stk->discard(argc + 1); |
| 2908 | |
| 2909 | /* handled */ |
| 2910 | return TRUE; |
| 2911 | } |
| 2912 | |
| 2913 | /* ------------------------------------------------------------------------ */ |
| 2914 | /* |
| 2915 | * Refill the read buffer. Returns true if the buffer contains any data |
| 2916 | * on return, false if we're at end of file. |
| 2917 | */ |
| 2918 | int vmobjfile_readbuf_t::refill(CVmDataSource *fp) |
| 2919 | { |
| 2920 | /* if the buffer is already full, do nothing */ |
| 2921 | if (rem == sizeof(buf)) |
| 2922 | return FALSE; |
| 2923 | |
| 2924 | /* |
| 2925 | * if there's anything left in the buffer, move it to the start of the |
| 2926 | * buffer |
| 2927 | */ |
| 2928 | if (rem != 0) |
| 2929 | { |
| 2930 | memmove(buf, ptr, rem); |
| 2931 | ptr = buf + rem; |
| 2932 | } |
| 2933 | else |
| 2934 | ptr = buf; |
| 2935 | |
| 2936 | /* fill up the rest of the buffer */ |
| 2937 | size_t cur = fp->readc(ptr, sizeof(buf) - rem); |
| 2938 | rem += cur; |
| 2939 | |
| 2940 | /* return success if we managed to read any bytes on this round */ |
| 2941 | return (cur != 0); |
| 2942 | } |
| 2943 | |
| 2944 | /* |
| 2945 | * Read a character |
| 2946 | */ |
| 2947 | int vmobjfile_readbuf_t::getch(wchar_t &ch, |
| 2948 | CVmDataSource *fp, |
| 2949 | class CCharmapToUni *charmap) |
| 2950 | { |
| 2951 | /* keep going until we map a character or run out of input */ |
| 2952 | for (;;) |
| 2953 | { |
| 2954 | /* try mapping a character - if we get one, return success */ |
| 2955 | if (charmap->mapchar(ch, (const char *&)ptr, rem)) |
| 2956 | return TRUE; |
| 2957 | |
| 2958 | /* |
| 2959 | * we don't have enough bytes to map a whole character, so refill |
| 2960 | * our buffer; if that fails, we're at EOF |
| 2961 | */ |
| 2962 | if (!refill(fp)) |
| 2963 | return FALSE; |
| 2964 | } |
| 2965 | } |
| 2966 | |
| 2967 | /* |
| 2968 | * Peek at the next character |
| 2969 | */ |
| 2970 | int vmobjfile_readbuf_t::peekch(wchar_t &ch, |
| 2971 | CVmDataSource *fp, |
| 2972 | class CCharmapToUni *charmap, |
| 2973 | size_t &in_bytes) |
| 2974 | { |
| 2975 | /* keep going until we map a character or run out of input */ |
| 2976 | for (;;) |
| 2977 | { |
| 2978 | /* try mapping a character - if we get one, return success */ |
| 2979 | char *ptr2 = ptr; |
| 2980 | size_t rem2 = rem; |
| 2981 | if (charmap->mapchar(ch, (const char *&)ptr2, rem2)) |
| 2982 | { |
| 2983 | in_bytes = rem - rem2; |
| 2984 | return TRUE; |
| 2985 | } |
| 2986 | |
| 2987 | /* |
| 2988 | * we don't have enough bytes to map a whole character, so refill |
| 2989 | * our buffer; if that fails, we're at EOF |
| 2990 | */ |
| 2991 | if (!refill(fp)) |
| 2992 | return FALSE; |
| 2993 | } |
| 2994 | } |