static Node *rdsym(FILE *fd, Trait *ctx) { int line; Node *name; Node *n; line = rdint(fd); name = unpickle(fd); n = mkdecl(Zloc, name, NULL); n->loc.line = line; n->loc.file = file->file.nfiles - 1; rdtype(fd, &n->decl.type); if (rdint(fd) == Vishidden) n->decl.ishidden = 1; n->decl.trait = ctx; n->decl.isconst = rdbool(fd); n->decl.isgeneric = rdbool(fd); n->decl.isextern = rdbool(fd); n->decl.ispkglocal = rdbool(fd); n->decl.isnoret = rdbool(fd); n->decl.isimport = 1; n->decl.isexportinit = rdbool(fd); n->decl.isinit = rdbool(fd); if (n->decl.isexportinit) n->decl.init = unpickle(fd); return n; }
void LuaSerializer::Unserialize(Serializer::Reader &rd) { lua_State *l = Lua::manager->GetLuaState(); LUA_DEBUG_START(l); lua_newtable(l); lua_setfield(l, LUA_REGISTRYINDEX, "PiSerializerTableRefs"); std::string pickled = rd.String(); const char *start = pickled.c_str(); const char *end = unpickle(l, start); if (size_t(end - start) != pickled.length()) throw SavedGameCorruptException(); if (!lua_istable(l, -1)) throw SavedGameCorruptException(); int savetable = lua_gettop(l); lua_pushnil(l); lua_setfield(l, LUA_REGISTRYINDEX, "PiSerializerTableRefs"); lua_getfield(l, LUA_REGISTRYINDEX, "PiSerializerCallbacks"); if (lua_isnil(l, -1)) { lua_pop(l, 1); lua_newtable(l); lua_pushvalue(l, -1); lua_setfield(l, LUA_REGISTRYINDEX, "PiSerializerCallbacks"); } lua_pushnil(l); while (lua_next(l, -2) != 0) { lua_pushvalue(l, -2); lua_pushinteger(l, 2); lua_gettable(l, -3); lua_getfield(l, savetable, lua_tostring(l, -2)); if (lua_isnil(l, -1)) { lua_pop(l, 1); lua_newtable(l); } pi_lua_protected_call(l, 1, 0); lua_pop(l, 2); } lua_pop(l, 2); LUA_DEBUG_END(l, 0); }
void LuaSerializer::FromJson(const Json::Value &jsonObj) { if (!jsonObj.isMember("lua_modules")) throw SavedGameCorruptException(); lua_State *l = Lua::manager->GetLuaState(); LUA_DEBUG_START(l); std::string pickled = JsonToBinStr(jsonObj, "lua_modules"); const char *start = pickled.c_str(); const char *end = unpickle(l, start); if (size_t(end - start) != pickled.length()) throw SavedGameCorruptException(); if (!lua_istable(l, -1)) throw SavedGameCorruptException(); int savetable = lua_gettop(l); lua_getfield(l, LUA_REGISTRYINDEX, "PiSerializerCallbacks"); if (lua_isnil(l, -1)) { lua_pop(l, 1); lua_newtable(l); lua_pushvalue(l, -1); lua_setfield(l, LUA_REGISTRYINDEX, "PiSerializerCallbacks"); } lua_pushnil(l); while (lua_next(l, -2) != 0) { lua_pushvalue(l, -2); lua_pushinteger(l, 2); lua_gettable(l, -3); lua_getfield(l, savetable, lua_tostring(l, -2)); if (lua_isnil(l, -1)) { lua_pop(l, 1); lua_newtable(l); } pi_lua_protected_call(l, 1, 0); lua_pop(l, 2); } lua_pop(l, 2); LUA_DEBUG_END(l, 0); }
Trait *traitunpickle(FILE *fd) { Trait *tr; size_t i, n; intptr_t uid; /* create an empty trait */ tr = mktrait(Zloc, NULL, NULL, NULL, 0, NULL, 0, 0); uid = rdint(fd); tr->ishidden = rdbool(fd); tr->name = unpickle(fd); tr->param = tyunpickle(fd); n = rdint(fd); for (i = 0; i < n; i++) lappend(&tr->memb, &tr->nmemb, rdsym(fd, tr)); n = rdint(fd); for (i = 0; i < n; i++) lappend(&tr->funcs, &tr->nfuncs, rdsym(fd, tr)); htput(trmap, itop(uid), tr); return tr; }
static Ucon *rducon(FILE *fd, Type *ut) { Type *et; Node *name; Ucon *uc; size_t id; int line; int synth; et = NULL; line = rdint(fd); id = rdint(fd); synth = rdbool(fd); name = unpickle(fd); uc = mkucon(Zloc, name, ut, et); uc->loc.line = line; uc->loc.file = file->file.nfiles - 1; if (rdbool(fd)) rdtype(fd, &uc->etype); uc->id = id; uc->synth = synth; return uc; }
/* Reads a symbol table from file. The converse * of wrstab. */ static Stab *rdstab(FILE *fd, int isfunc) { Stab *st; Type *ty; Node *nm; int n; int i; /* read dcls */ st = mkstab(isfunc); st->name = rdstr(fd); n = rdint(fd); for (i = 0; i < n; i++) putdcl(st, rdsym(fd, NULL)); /* read types */ n = rdint(fd); for (i = 0; i < n; i++) { nm = unpickle(fd); rdtype(fd, &ty); puttype(st, nm, ty); } return st; }
/* Usefile format: * U<pkgname> * T<pickled-type> * R<picled-trait> * I<pickled-impl> * D<picled-decl> * G<pickled-decl><pickled-initializer> */ int loaduse(char *path, FILE *f, Stab *st, Vis vis) { intptr_t tid; size_t i; int v; char *pkg; Node *dcl, *impl, *init; Stab *s; Type *ty; Trait *tr; char *lib; int c; pushstab(file->file.globls); if (!tydedup) tydedup = mkht(tyhash, tyeq); if (fgetc(f) != 'U') return 0; v = rdint(f); if (v != Abiversion) { fprintf(stderr, "%s: abi version %d, expected %d\n", path, v, Abiversion); return 0; } pkg = rdstr(f); /* if the package names match up, or the usefile has no declared * package, then we simply add to the current stab. Otherwise, * we add a new stab under the current one */ if (st->name) { if (pkg && !strcmp(pkg, st->name)) { s = st; } else { s = findstab(st, pkg); } } else { if (pkg) { s = findstab(st, pkg); } else { s = st; } } if (!streq(st->name, pkg)) vis = Visintern; if (!s) { printf("could not find matching package for merge: %s in %s\n", st->name, path); exit(1); } tidmap = mkht(ptrhash, ptreq); trmap = mkht(ptrhash, ptreq); if (!initmap) initmap = mkht(namehash, nameeq); /* builtin traits */ for (i = 0; i < Ntraits; i++) htput(trmap, itop(i), traittab[i]); while ((c = fgetc(f)) != EOF) { switch(c) { case 'L': lib = rdstr(f); for (i = 0; i < file->file.nlibdeps; i++) if (!strcmp(file->file.libdeps[i], lib)) /* break out of both loop and switch */ goto foundlib; lappend(&file->file.libdeps, &file->file.nlibdeps, lib); foundlib: break; case 'X': lib = rdstr(f); for (i = 0; i < file->file.nextlibs; i++) if (!strcmp(file->file.extlibs[i], lib)) /* break out of both loop and switch */ goto foundextlib; lappend(&file->file.extlibs, &file->file.nextlibs, lib); foundextlib: break; case 'F': lappend(&file->file.files, &file->file.nfiles, rdstr(f)); break; case 'G': case 'D': dcl = rdsym(f, NULL); dcl->decl.vis = vis; dcl->decl.isglobl = 1; putdcl(s, dcl); break; case 'S': init = unpickle(f); if (!hthas(initmap, init)) { htput(initmap, init, init); lappend(&file->file.init, &file->file.ninit, init); } break; case 'R': tr = traitunpickle(f); tr->vis = vis; puttrait(s, tr->name, tr); for (i = 0; i < tr->nfuncs; i++) putdcl(s, tr->funcs[i]); break; case 'T': tid = rdint(f); ty = tyunpickle(f); if(!ty->ishidden) ty->vis = vis; htput(tidmap, itop(tid), ty); /* fix up types */ if (ty->type == Tyname || ty->type == Tygeneric) { if (ty->issynth) break; if (!streq(s->name, ty->name->name.ns)) ty->ishidden = 1; if (!gettype(s, ty->name) && !ty->ishidden) puttype(s, ty->name, ty); } else if (ty->type == Tyunion) { for (i = 0; i < ty->nmemb; i++) if (!getucon(s, ty->udecls[i]->name) && !ty->udecls[i]->synth) putucon(s, ty->udecls[i]); } break; case 'I': impl = unpickle(f); putimpl(s, impl); /* specialized declarations always go into the global stab */ for (i = 0; i < impl->impl.ndecls; i++) putdcl(file->file.globls, impl->impl.decls[i]); break; case EOF: break; } } fixtypemappings(s); fixtraitmappings(s); htfree(tidmap); popstab(); return 1; }
/* Unpickles a node from a file. Minimal checking * is done. Specifically, no checks are done for * sane arities, a bad file can crash the compiler */ static Node *unpickle(FILE *fd) { size_t i; Ntype type; Node *n; type = rdbyte(fd); if (type == Nnone) return NULL; n = mknode(Zloc, type); n->loc.line = rdint(fd); n->loc.file = file->file.nfiles - 1; switch (n->type) { case Nfile: lappend(&n->file.files, &n->file.nfiles, rdstr(fd)); n->file.nuses = rdint(fd); n->file.uses = zalloc(sizeof(Node*)*n->file.nuses); for (i = 0; i < n->file.nuses; i++) n->file.uses[i] = unpickle(fd); n->file.nstmts = rdint(fd); n->file.stmts = zalloc(sizeof(Node*)*n->file.nstmts); for (i = 0; i < n->file.nstmts; i++) n->file.stmts[i] = unpickle(fd); n->file.globls = rdstab(fd, 0); break; case Nexpr: n->expr.op = rdbyte(fd); rdtype(fd, &n->expr.type); n->expr.isconst = rdbool(fd); n->expr.idx = unpickle(fd); n->expr.nargs = rdint(fd); n->expr.args = zalloc(sizeof(Node *)*n->expr.nargs); for (i = 0; i < n->expr.nargs; i++) n->expr.args[i] = unpickle(fd); break; case Nname: if (rdbool(fd)) n->name.ns = rdstr(fd); n->name.name = rdstr(fd); break; case Nuse: n->use.islocal = rdbool(fd); n->use.name = rdstr(fd); break; case Nlit: n->lit.littype = rdbyte(fd); rdtype(fd, &n->lit.type); n->lit.nelt = rdint(fd); switch (n->lit.littype) { case Lchr: n->lit.chrval = rdint(fd); break; case Lint: n->lit.intval = rdint(fd); break; case Lflt: n->lit.fltval = rdflt(fd); break; case Lstr: rdlenstr(fd, &n->lit.strval); break; case Llbl: n->lit.lblval = rdstr(fd); break; case Lbool: n->lit.boolval = rdbool(fd); break; case Lfunc: n->lit.fnval = unpickle(fd); break; } break; case Nloopstmt: n->loopstmt.init = unpickle(fd); n->loopstmt.cond = unpickle(fd); n->loopstmt.step = unpickle(fd); n->loopstmt.body = unpickle(fd); break; case Niterstmt: n->iterstmt.elt = unpickle(fd); n->iterstmt.seq = unpickle(fd); n->iterstmt.body = unpickle(fd); break; case Nmatchstmt: n->matchstmt.val = unpickle(fd); n->matchstmt.nmatches = rdint(fd); n->matchstmt.matches = zalloc(sizeof(Node *)*n->matchstmt.nmatches); for (i = 0; i < n->matchstmt.nmatches; i++) n->matchstmt.matches[i] = unpickle(fd); break; case Nmatch: n->match.pat = unpickle(fd); n->match.block = unpickle(fd); break; case Nifstmt: n->ifstmt.cond = unpickle(fd); n->ifstmt.iftrue = unpickle(fd); n->ifstmt.iffalse = unpickle(fd); break; case Nblock: n->block.scope = rdstab(fd, 0); n->block.nstmts = rdint(fd); n->block.stmts = zalloc(sizeof(Node *)*n->block.nstmts); n->block.scope->super = curstab(); pushstab(n->func.scope->super); for (i = 0; i < n->block.nstmts; i++) n->block.stmts[i] = unpickle(fd); popstab(); break; case Ndecl: n->decl.did = ndecls; /* unique within file */ /* sym */ n->decl.name = unpickle(fd); rdtype(fd, &n->decl.type); /* symflags */ n->decl.isconst = rdbool(fd); n->decl.isgeneric = rdbool(fd); n->decl.isextern = rdbool(fd); n->decl.isnoret = rdbool(fd); n->decl.ispkglocal = rdbool(fd); /* init */ n->decl.init = unpickle(fd); lappend(&decls, &ndecls, n); break; case Nfunc: rdtype(fd, &n->func.type); n->func.scope = rdstab(fd, 1); n->func.nargs = rdint(fd); n->func.args = zalloc(sizeof(Node *)*n->func.nargs); n->func.scope->super = curstab(); pushstab(n->func.scope->super); for (i = 0; i < n->func.nargs; i++) n->func.args[i] = unpickle(fd); n->func.body = unpickle(fd); popstab(); break; case Nimpl: n->impl.traitname = unpickle(fd); i = rdint(fd); rdtrait(fd, &n->impl.trait, NULL); rdtype(fd, &n->impl.type); n->impl.ndecls = rdint(fd); n->impl.decls = zalloc(sizeof(Node *)*n->impl.ndecls); for (i = 0; i < n->impl.ndecls; i++) n->impl.decls[i] = rdsym(fd, n->impl.trait); break; case Nnone: die("Nnone should not be seen as node type!"); break; } return n; }
/* Writes types to a file. Errors on * internal only types like Tyvar that * will not be meaningful in another file */ static Type *tyunpickle(FILE *fd) { size_t i, n; Type *ty; Ty t; t = rdbyte(fd); ty = mktype(Zloc, t); ty->isimport = 1; if (rdbyte(fd) == Vishidden) ty->ishidden = 1; /* tid is generated; don't write */ n = rdint(fd); for (i = 0; i < n; i++) rdtrait(fd, NULL, ty); ty->nsub = rdint(fd); if (ty->nsub > 0) ty->sub = zalloc(ty->nsub * sizeof(Type*)); switch (ty->type) { case Tyunres: ty->name = unpickle(fd); break; case Typaram: ty->pname = rdstr(fd); break; case Tystruct: ty->nmemb = rdint(fd); ty->sdecls = zalloc(ty->nmemb * sizeof(Node*)); for (i = 0; i < ty->nmemb; i++) ty->sdecls[i] = unpickle(fd); break; case Tyunion: ty->nmemb = rdint(fd); ty->udecls = zalloc(ty->nmemb * sizeof(Node*)); for (i = 0; i < ty->nmemb; i++) ty->udecls[i] = rducon(fd, ty); break; case Tyarray: rdtype(fd, &ty->sub[0]); ty->asize = unpickle(fd); break; case Tyslice: rdtype(fd, &ty->sub[0]); break; case Tyname: ty->name = unpickle(fd); ty->issynth = rdbool(fd); ty->narg = rdint(fd); ty->arg = zalloc(ty->narg * sizeof(Type *)); for (i = 0; i < ty->narg; i++) rdtype(fd, &ty->arg[i]); rdtype(fd, &ty->sub[0]); break; case Tygeneric: ty->name = unpickle(fd); ty->issynth = rdbool(fd); ty->ngparam = rdint(fd); ty->gparam = zalloc(ty->ngparam * sizeof(Type *)); for (i = 0; i < ty->ngparam; i++) rdtype(fd, &ty->gparam[i]); rdtype(fd, &ty->sub[0]); break; default: for (i = 0; i < ty->nsub; i++) rdtype(fd, &ty->sub[i]); break; } return ty; }
static PyObject * marshal_Load_internal(PyObject *py_stream, PyObject *py_callback, int skipcrc) { // Return value: New Reference char *stream; Py_ssize_t size; char *s; char *end; int type = -1; // current object type int shared = -1; // indicates whether current object is shared int i; char *error = "NO ERROR SPECIFIED"; char errortext[256]; Py_ssize_t length = 0; // generic length value. int shared_mapsize; int shared_count; // shared object index counter int *shared_map; // points to shared object mapping at end of stream PyObject **shared_obj = NULL; // holds the shared objects PyObject *obj = NULL; // currently decoded object PyObject *result = NULL; // final result int ct_ix = 0; struct Container ct_stack[MAX_DEPTH]; struct Container *container = &ct_stack[0]; if(PyString_AsStringAndSize(py_stream, &stream, &size) == -1) return NULL; s = stream; container->obj = NULL; container->type = 0; container->free = -1; container->index = 0; if(size < 6 || *s++ != PROTOCOL_ID) { int offset = 0; result = unpickle(py_stream, &offset); if(!result) goto cleanup; return result; } // how many shared objects in this stream? shared_mapsize = *(int32_t *)s; s += 4; // Security Check: assert there is enough data for that many items. if((5 + shared_mapsize*4) > size) { PyErr_Format(UnmarshalError, "Not enough room in stream for map. Wanted %d, but have only %d bytes remaining...", (shared_mapsize*4), ((int)size-5)); goto cleanup; } // ok, we got the map data right here... shared_map = (int32_t *)&stream[size - shared_mapsize * 4]; // Security Check #2: assert all map entries are between 1 and shared_mapsize for(i=0; i<shared_mapsize; i++) { if( (shared_map[i] > shared_mapsize) || (shared_map[i] < 1) ) { PyErr_SetString(UnmarshalError, "Bogus map data in marshal stream"); goto cleanup; } } // the start of which is incidentally also the end of the object data. end = (char *)shared_map; // create object table shared_obj = PyMem_MALLOC(shared_mapsize * sizeof(PyObject *)); if(!shared_obj) goto cleanup; // zero out object table for(shared_count = 0; shared_count < shared_mapsize; shared_count++) shared_obj[shared_count] = NULL; shared_count = 0; // start decoding. while(s < end) { // This outer loop is responsible for reading and decoding the next // object from the stream. The object is then handed to the inner loop, // which adds it to the current container, or returns it to the caller. // get type of next object to decode and check shared flag type = *s++; shared = type & SHARED_FLAG; type &= ~SHARED_FLAG; // if token uses a normal length value, read it now. if(needlength[type]) { READ_LENGTH; } else length = 0; #if MARSHAL_DEBUG // if(shared) { char text[220]; DEBUG_INDENT; sprintf(text, "pos:%4d type:%s(0x%02x) shared:%d len:%4d map:[", s-stream, tokenname[type], type, shared?1:0, length); printf(text); for(i=0; i<shared_mapsize; i++) printf("%d(%d),", shared_obj[i], shared_obj[i] ? ((PyObject *)(shared_obj[i]))->ob_refcnt : 0); printf("]\r\n"); } #endif // MARSHAL_DEBUG switch(type) { // // break statement: // attempts to add the newly decoded object (in the obj variable) to // the currently building container object. // // continue statement: // indicates the decoded object or type marker was handled/consumed // by the case and should _not_ be added to the currently building // container object or used in any other way; immediately decode a // new object // //--------------------------------------------------------------------- // SCALAR TYPES //--------------------------------------------------------------------- case TYPE_INT8: CHECK_SIZE(1); obj = PyInt_FromLong(*(int8_t *)s); s++; break; case TYPE_INT16: CHECK_SIZE(2); obj = PyInt_FromLong(*(int16_t *)s); s += 2; break; case TYPE_INT32: CHECK_SIZE(4); obj = PyInt_FromLong(*(int32_t *)s); s += 4; break; case TYPE_INT64: CHECK_SIZE(8); obj = PyLong_FromLongLong(*(int64_t *)s); s += 8; break; case TYPE_LONG: CHECK_SIZE(length); if(!length) obj = PyLong_FromLong(0); else { obj = _PyLong_FromByteArray((unsigned char *)s, length, 1, 1); Py_INCREF(obj); } CHECK_SHARED(obj); s += length; break; case TYPE_FLOAT: CHECK_SIZE(8); obj = PyFloat_FromDouble(*(double *)s); s += 8; break; case TYPE_CHECKSUM: CHECK_SIZE(4); if(!skipcrc && (*(uint32_t *)s != (uint32_t)adler32(1, s, (unsigned long)(end-s)))) { error = "checksum error"; goto fail; } s += 4; // because this type does not yield an object, go grab another // object right away! continue; //--------------------------------------------------------------------- // STRING TYPES //--------------------------------------------------------------------- case TYPE_STRINGR: if (length < 1 || length >= PyList_GET_SIZE(string_table)) { if(PyList_GET_SIZE(string_table)) PyErr_Format(UnmarshalError, "Invalid string table index %d", (int)length); else PyErr_SetString(PyExc_RuntimeError, "_stringtable not initialized"); goto cleanup; } obj = PyList_GET_ITEM(string_table, length); Py_INCREF(obj); break; case TYPE_STRING: // appears to be deprecated since machoVersion 213 CHECK_SIZE(1); length = *(unsigned char *)s++; CHECK_SIZE(length); obj = PyString_FromStringAndSize(s, length); s += length; break; case TYPE_STRING1: CHECK_SIZE(1); obj = PyString_FromStringAndSize(s, 1); s++; break; case TYPE_STREAM: // fallthrough, can be treated as string. case TYPE_STRINGL: // fallthrough, deprecated since machoVersion 213 case TYPE_BUFFER: // Type identifier re-used by CCP. treat as string. CHECK_SIZE(length); obj = PyString_FromStringAndSize(s, length); s += length; CHECK_SHARED(obj); break; case TYPE_UNICODE1: CHECK_SIZE(2); #ifdef Py_UNICODE_WIDE obj = _PyUnicodeUCS4_FromUCS2((void *)s, 1); #else obj = PyUnicode_FromWideChar((wchar_t *)s, 1); #endif s += 2; break; case TYPE_UNICODE: CHECK_SIZE(length*2); #ifdef Py_UNICODE_WIDE obj = _PyUnicodeUCS4_FromUCS2((void *)s, (int)length); #else obj = PyUnicode_FromWideChar((wchar_t *)s, length); #endif s += length*2; break; case TYPE_UTF8: CHECK_SIZE(length); obj = PyUnicode_DecodeUTF8(s, length, NULL); s += length; break; //--------------------------------------------------------------------- // SEQUENCE/MAPPING TYPES //--------------------------------------------------------------------- case TYPE_TUPLE1: NEW_SEQUENCE(TYPE_TUPLE, 1); continue; case TYPE_TUPLE2: NEW_SEQUENCE(TYPE_TUPLE, 2); continue; case TYPE_TUPLE: NEW_SEQUENCE(TYPE_TUPLE, (int)length); continue; case TYPE_LIST0: obj = PyList_New(0); CHECK_SHARED(obj); break; case TYPE_LIST1: NEW_SEQUENCE(TYPE_LIST, 1); continue; case TYPE_LIST: NEW_SEQUENCE(TYPE_LIST, (int)length); continue; case TYPE_DICT: if(length) { CHECK_SIZE(length*2); PUSH_CONTAINER(TYPE_DICT, (int)length*2); container->obj = PyDict_New(); container->obj2 = NULL; container->index = 0; CHECK_SHARED(container->obj); continue; } else { obj = PyDict_New(); CHECK_SHARED(obj); break; } //--------------------------------------------------------------------- // OBJECT TYPES //--------------------------------------------------------------------- case TYPE_REF: // length value is index in sharedobj array! if((length < 1 || length > shared_mapsize)) { error = "Shared reference index out of range"; goto fail; } if(!(obj = shared_obj[length-1])) { error = "Shared reference points to invalid object"; goto fail; } Py_INCREF(obj); //printf("Getting object %d from %d (refs:%d)\r\n", (int)obj, length-1, obj->ob_refcnt); break; case TYPE_GLOBAL: { PyObject *name; CHECK_SIZE(length); name = PyString_FromStringAndSize(s, length); if(!name) goto cleanup; s += length; if(!(obj = find_global(name))) { // exception should be set by find_global goto cleanup; } Py_DECREF(name); CHECK_SHARED(obj); break; } case TYPE_DBROW: case TYPE_INSTANCE: case TYPE_NEWOBJ: case TYPE_REDUCE: PUSH_CONTAINER(type, -1); container->obj = NULL; RESERVE_SLOT(container->index); continue; case TYPE_MARK: // this is a marker, not a real object. list/dict iterators check // for this type, but it can't be instantiated. break; default: if((obj = constants[type])) { Py_INCREF(obj); } else { error = "Unsupported type"; goto fail; } } // object decoding and construction done! if(!obj && type != TYPE_MARK) { // if obj is somehow NULL, whatever caused it is expected to have // set an exception at this point. goto cleanup; } #if MARSHAL_DEBUG /* if(obj && obj->ob_refcnt < 0) { char b[200]; sprintf(b, "type: %d, refcount: %d", type, obj->ob_refcnt); DEBUG(b); } */ if(obj) { DEBUG_INDENT; printf("`-- "); PyObject_Print(obj, stdout, 0); printf("\r\n"); fflush(stdout); } else { DEBUG_INDENT; printf("*** MARK\r\n"); } #endif // MARSHAL_DEBUG while(1) { // This inner loop does one of two things: // // - return the finished object to the caller if we're at the root // container. // // - add the object to the current container in a container- // specific manner. note that ownership of the reference is to be // given to the container object. #if MARSHAL_DEBUG { //char text[220]; DEBUG_INDENT; printf("container ix:%d (%08lx) type:%s[0x%02x] free:%d index:%d\r\n", ct_ix, container->obj, tokenname[container->type], container->type, container->free, container->index); } #endif // MARSHAL_DEBUG /* if(!container->obj) { error = "Root container popped off stack"; goto fail; } */ switch(container->type) { case TYPE_TUPLE: // tuples steal references. PyTuple_SET_ITEM(container->obj, container->index++, obj); break; case TYPE_LIST: // lists steal references. PyList_SET_ITEM(container->obj, container->index++, obj); break; case TYPE_DBROW: if(container->obj) { // we have an initialized DBRow. current object is a // non-scalar object for the row. append it. if(!dbrow_append_internal((PyDBRowObject *)container->obj, obj)) { // append call will have set an exception here. goto cleanup; } } else { // we now have a DBRowDescriptor, and the header data // should follow. Pull it and create the DBRow. READ_LENGTH; CHECK_SIZE(length); container->obj = PyDBRow_New((PyDBRowDescriptorObject *)obj, s, (int)length); container->free = 1+((PyDBRowDescriptorObject *)obj)->rd_num_objects; if(!container->obj) goto cleanup; Py_DECREF(obj); s += length; // add as shared object, if neccessary... UPDATE_SLOT(container->index, container->obj); } break; case TYPE_INSTANCE: { PyObject *cls; if(container->free == -1) { // create class instance if(!(cls = find_global(obj))) goto cleanup; container->obj = PyInstance_NewRaw(cls, 0); Py_DECREF(cls); if(!container->obj) goto cleanup; UPDATE_SLOT(container->index, container->obj); Py_DECREF(obj); break; } if(container->free == -2) { container->free = 1; // set state. if(!set_state(container->obj, obj)) goto cleanup; Py_DECREF(obj); break; } error = "invalid container state"; goto fail; } case TYPE_NEWOBJ: { PyObject *cls, *args, *__new__, *state; // instantiate the object... if(!(args = PyTuple_GetItem(obj, 0))) goto cleanup; if(!(cls = PyTuple_GetItem(args, 0))) goto cleanup; __new__ = PyObject_GetAttr(cls, py__new__); if(!__new__) goto cleanup; container->obj = PyObject_CallObject(__new__, args); Py_DECREF(__new__); if(!container->obj) goto cleanup; // add as shared object, if neccessary... UPDATE_SLOT(container->index, container->obj); // is there state data? if(PyTuple_GET_SIZE(obj) > 1) { state = PyTuple_GET_ITEM(obj, 1); if(!set_state(container->obj, state)) goto cleanup; } Py_DECREF(obj); // switch to list iterator LIST_ITERATOR; break; } case TYPE_REDUCE: { PyObject *callable, *args, *state; if(!(args = PyTuple_GetItem(obj, 1))) goto cleanup; if(!(callable = PyTuple_GET_ITEM(obj, 0))) goto cleanup; if(!(container->obj = PyObject_CallObject(callable, args))) goto cleanup; UPDATE_SLOT(container->index, container->obj); if(PyTuple_GET_SIZE(obj) > 2) { state = PyTuple_GET_ITEM(obj, 2); if(!set_state(container->obj, state)) goto cleanup; } Py_DECREF(obj); // switch to list iterator LIST_ITERATOR; break; } case TYPE_LIST_ITERATOR: if(type == TYPE_MARK) { // clear mark so nested iterator containers do not get terminated prematurely. type = -1; // decref the append method Py_XDECREF(container->obj2); container->obj2 = NULL; container->type = TYPE_DICT_ITERATOR; break; } if(!container->obj2) { // grab the append method from the container and keep // it around for speed. if(!(container->obj2 = PyObject_GetAttr(container->obj, pyappend))) goto cleanup; } if(!PyObject_CallFunctionObjArgs(container->obj2, obj, NULL)) goto cleanup; #if MARSHAL_DEBUG DEBUG_INDENT; printf("Appended %08lx to %08lx\r\n", obj, container->obj); #endif // MARSHAL_DEBUG Py_DECREF(obj); break; case TYPE_DICT_ITERATOR: if(type == TYPE_MARK) { // clear mark so nested iterator containers do not get terminated prematurely. type = -1; // we're done with dict iter. container is finished. container->free = 1; break; } POPULATE_DICT(container->obj2, obj); break; case TYPE_DICT: POPULATE_DICT(obj, container->obj2); break; case 0: // we're at the root. return the object to caller. result = obj; // avoid decreffing this object. obj = NULL; goto cleanup; } container->free--; if(container->free) // still room in this container. // break out of container handling to get next object for it. break; // this container is done, it is the next object to put into the // container under it! obj = container->obj; // switch context to said older container POP_CONTAINER; } // we've processed the object. clear it for next one. obj = NULL; } // if we get here, we're out of data, but it's a "clean" eof; we ran out // of data while expecting a new object... error = "Not enough objects in stream"; fail: PyErr_Format(UnmarshalError, "%s - type:0x%02x ctype:0x%02x len:%d share:%d pos:%d size:%d", error, type, container->type, (int)length, shared, (int)(s-stream), (int)(size)); cleanup: // on any error the current object we were working on will be unassociated // with anything, decref it. if decode was succesful or an object failed to // be created, it will be NULL anyway. Py_XDECREF(obj); // same story for containers... while(container->type) { Py_XDECREF(container->obj); // possibly unassociated object for dict entry? if(container->type == TYPE_DICT || container->type == TYPE_DICT_ITERATOR) { Py_XDECREF(container->obj2); } POP_CONTAINER; } if(shared_obj) { /* shared object list held a safety ref to all objects, decref em */ int i; for(i=0; i<shared_mapsize; i++) Py_XDECREF(shared_obj[i]); /* and free the list */ PyMem_FREE(shared_obj); } return result; }
const char *LuaSerializer::unpickle(lua_State *l, const char *pos) { LUA_DEBUG_START(l); char type = *pos++; switch (type) { case 'f': { char *end; double f = strtod(pos, &end); if (pos == end) throw SavedGameCorruptException(); lua_pushnumber(l, f); pos = end+1; // skip newline break; } case 'b': { if (*pos != '0' && *pos != '1') throw SavedGameCorruptException(); bool b = (*pos == '0') ? false : true; lua_pushboolean(l, b); pos++; break; } case 's': { char *end; int len = strtol(pos, const_cast<char**>(&end), 0); if (pos == end) throw SavedGameCorruptException(); end++; // skip newline lua_pushlstring(l, end, len); pos = end + len; break; } case 't': { lua_newtable(l); lua_getfield(l, LUA_REGISTRYINDEX, "PiSerializerTableRefs"); pos = unpickle(l, pos); lua_pushvalue(l, -3); lua_rawset(l, -3); lua_pop(l, 1); while (*pos != 'n') { pos = unpickle(l, pos); pos = unpickle(l, pos); lua_rawset(l, -3); } pos++; break; } case 'r': { pos = unpickle(l, pos); lua_getfield(l, LUA_REGISTRYINDEX, "PiSerializerTableRefs"); lua_pushvalue(l, -2); lua_rawget(l, -2); if (lua_isnil(l, -1)) throw SavedGameCorruptException(); lua_insert(l, -3); lua_pop(l, 2); break; } case 'u': { const char *end = strchr(pos, '\n'); if (!end) throw SavedGameCorruptException(); int len = end - pos; end++; // skip newline if (len == 10 && strncmp(pos, "SystemPath", 10) == 0) { pos = end; Sint32 sectorX = strtol(pos, const_cast<char**>(&end), 0); if (pos == end) throw SavedGameCorruptException(); pos = end+1; // skip newline Sint32 sectorY = strtol(pos, const_cast<char**>(&end), 0); if (pos == end) throw SavedGameCorruptException(); pos = end+1; // skip newline Sint32 sectorZ = strtol(pos, const_cast<char**>(&end), 0); if (pos == end) throw SavedGameCorruptException(); pos = end+1; // skip newline Uint32 systemNum = strtoul(pos, const_cast<char**>(&end), 0); if (pos == end) throw SavedGameCorruptException(); pos = end+1; // skip newline Uint32 sbodyId = strtoul(pos, const_cast<char**>(&end), 0); if (pos == end) throw SavedGameCorruptException(); pos = end+1; // skip newline SystemPath *sbp = new SystemPath(sectorX, sectorY, sectorZ, systemNum, sbodyId); LuaSystemPath::PushToLuaGC(sbp); break; } if (len == 4 && strncmp(pos, "Body", 4) == 0) { pos = end; Uint32 n = strtoul(pos, const_cast<char**>(&end), 0); if (pos == end) throw SavedGameCorruptException(); pos = end+1; // skip newline Body *body = Pi::game->GetSpace()->GetBodyByIndex(n); if (pos == end) throw SavedGameCorruptException(); switch (body->GetType()) { case Object::BODY: LuaBody::PushToLua(body); break; case Object::SHIP: LuaShip::PushToLua(dynamic_cast<Ship*>(body)); break; case Object::SPACESTATION: LuaSpaceStation::PushToLua(dynamic_cast<SpaceStation*>(body)); break; case Object::PLANET: LuaPlanet::PushToLua(dynamic_cast<Planet*>(body)); break; case Object::STAR: LuaStar::PushToLua(dynamic_cast<Star*>(body)); break; case Object::PLAYER: LuaPlayer::PushToLua(dynamic_cast<Player*>(body)); break; default: throw SavedGameCorruptException(); } break; } throw SavedGameCorruptException(); } case 'o': { const char *end = strchr(pos, '\n'); if (!end) throw SavedGameCorruptException(); int len = end - pos; end++; // skip newline const char *cl = pos; // unpickle the object, and insert it beneath the method table value pos = unpickle(l, end); // get _G[typename] lua_rawgeti(l, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS); lua_pushlstring(l, cl, len); lua_gettable(l, -2); lua_remove(l, -2); if (lua_isnil(l, -1)) { lua_pop(l, 2); break; } lua_getfield(l, -1, "Unserialize"); if (lua_isnil(l, -1)) { lua_pushlstring(l, cl, len); luaL_error(l, "No Unserialize method found for class '%s'\n", lua_tostring(l, -1)); } lua_insert(l, -3); lua_pop(l, 1); pi_lua_protected_call(l, 1, 1); break; } default: throw SavedGameCorruptException(); } LUA_DEBUG_END(l, 1); return pos; }
const char *LuaSerializer::unpickle(lua_State *l, const char *pos) { LUA_DEBUG_START(l); // tables are also unpickled recursively, so we can run out of Lua stack space if we're not careful // start by ensuring we have enough (this grows the stack if necessary) // (20 is somewhat arbitrary) if (!lua_checkstack(l, 20)) luaL_error(l, "The Lua stack couldn't be extended (not enough memory?)"); char type = *pos++; switch (type) { case 'f': { char *end; double f = strtod(pos, &end); if (pos == end) throw SavedGameCorruptException(); lua_pushnumber(l, f); pos = end+1; // skip newline break; } case 'b': { if (*pos != '0' && *pos != '1') throw SavedGameCorruptException(); bool b = (*pos == '0') ? false : true; lua_pushboolean(l, b); pos++; break; } case 's': { char *end; int len = strtol(pos, const_cast<char**>(&end), 0); if (pos == end) throw SavedGameCorruptException(); end++; // skip newline lua_pushlstring(l, end, len); pos = end + len; break; } case 't': { lua_newtable(l); lua_getfield(l, LUA_REGISTRYINDEX, "PiSerializerTableRefs"); pos = unpickle(l, pos); lua_pushvalue(l, -3); lua_rawset(l, -3); lua_pop(l, 1); while (*pos != 'n') { pos = unpickle(l, pos); pos = unpickle(l, pos); lua_rawset(l, -3); } pos++; break; } case 'r': { pos = unpickle(l, pos); lua_getfield(l, LUA_REGISTRYINDEX, "PiSerializerTableRefs"); lua_pushvalue(l, -2); lua_rawget(l, -2); if (lua_isnil(l, -1)) throw SavedGameCorruptException(); lua_insert(l, -3); lua_pop(l, 2); break; } case 'u': { const char *end; if (!LuaObjectBase::Deserialize(pos, &end)) throw SavedGameCorruptException(); pos = end; break; } case 'o': { const char *end = strchr(pos, '\n'); if (!end) throw SavedGameCorruptException(); int len = end - pos; end++; // skip newline const char *cl = pos; // unpickle the object, and insert it beneath the method table value pos = unpickle(l, end); // If it is a reference, don't run the unserializer. It has either // already been run, or the data is still building (cyclic // references will do that to you.) if (*end != 'r') { // get PiSerializerClasses[typename] lua_getfield(l, LUA_REGISTRYINDEX, "PiSerializerClasses"); lua_pushlstring(l, cl, len); lua_gettable(l, -2); lua_remove(l, -2); if (lua_isnil(l, -1)) { lua_pop(l, 2); break; } lua_getfield(l, -1, "Unserialize"); if (lua_isnil(l, -1)) { lua_pushlstring(l, cl, len); luaL_error(l, "No Unserialize method found for class '%s'\n", lua_tostring(l, -1)); } lua_insert(l, -3); lua_pop(l, 1); pi_lua_protected_call(l, 1, 1); } break; } default: throw SavedGameCorruptException(); } LUA_DEBUG_END(l, 1); return pos; }
const char *LuaSerializer::unpickle(lua_State *l, const char *pos) { LUA_DEBUG_START(l); char type = *pos++; switch (type) { case 'f': { char *end; double f = strtod(pos, &end); if (pos == end) throw SavedGameCorruptException(); lua_pushnumber(l, f); pos = end+1; // skip newline break; } case 'b': { if (*pos != '0' && *pos != '1') throw SavedGameCorruptException(); bool b = (*pos == '0') ? false : true; lua_pushboolean(l, b); pos++; break; } case 's': { char *end; int len = strtod(pos, &end); if (pos == end) throw SavedGameCorruptException(); end++; // skip newline lua_pushlstring(l, end, len); pos = end + len; break; } case 't': { lua_newtable(l); while (*pos != 'n') { pos = unpickle(l, pos); pos = unpickle(l, pos); lua_rawset(l, -3); } pos++; break; } case 'u': { const char *end = strchr(pos, '\n'); if (!end) throw SavedGameCorruptException(); int len = end - pos; end++; // skip newline if (len == 10 && strncmp(pos, "SystemPath", 10) == 0) { pos = end; Sint32 sectorX = strtol(pos, const_cast<char**>(&end), 0); if (pos == end) throw SavedGameCorruptException(); pos = end+1; // skip newline Sint32 sectorY = strtol(pos, const_cast<char**>(&end), 0); if (pos == end) throw SavedGameCorruptException(); pos = end+1; // skip newline Sint32 sectorZ = strtol(pos, const_cast<char**>(&end), 0); if (pos == end) throw SavedGameCorruptException(); pos = end+1; // skip newline Sint32 systemNum = strtol(pos, const_cast<char**>(&end), 0); if (pos == end) throw SavedGameCorruptException(); pos = end+1; // skip newline Sint32 sbodyId = strtol(pos, const_cast<char**>(&end), 0); if (pos == end) throw SavedGameCorruptException(); pos = end+1; // skip newline SystemPath *sbp = new SystemPath(sectorX, sectorY, sectorZ, systemNum, sbodyId); LuaSystemPath::PushToLuaGC(sbp); break; } if (len == 4 && strncmp(pos, "Body", 4) == 0) { pos = end; int n = strtol(pos, const_cast<char**>(&end), 0); if (pos == end) throw SavedGameCorruptException(); pos = end+1; // skip newline Body *body = Serializer::LookupBody(n); if (pos == end) throw SavedGameCorruptException(); switch (body->GetType()) { case Object::BODY: LuaBody::PushToLua(body); break; case Object::SHIP: LuaShip::PushToLua(dynamic_cast<Ship*>(body)); break; case Object::SPACESTATION: LuaSpaceStation::PushToLua(dynamic_cast<SpaceStation*>(body)); break; case Object::PLANET: LuaPlanet::PushToLua(dynamic_cast<Planet*>(body)); break; case Object::STAR: LuaStar::PushToLua(dynamic_cast<Star*>(body)); break; case Object::PLAYER: LuaPlayer::PushToLua(dynamic_cast<Player*>(body)); break; default: throw SavedGameCorruptException(); } break; } throw SavedGameCorruptException(); } default: throw SavedGameCorruptException(); } LUA_DEBUG_END(l, 1); return pos; }