unsigned long deflatewriter_tell(struct _writer *writer) { struct _deflatewriter *deflatewriter = CAST_UP(struct _deflatewriter,base,writer); _xlog("deflatewriter.seek : not supported\n"); writer->error = 1; return(0); }
int deflatewriter_resize(unsigned long size, struct _writer *writer) { struct _deflatewriter *deflatewriter = CAST_UP(struct _deflatewriter,base,writer); _xlog("deflatewriter.resize : not supported\n"); writer->error = 1; return(writer->error); }
int deflatewriter_seek(struct _writer *writer, long pos, int origin) { struct _deflatewriter *deflatewriter = CAST_UP(struct _deflatewriter,base,writer); _xlog("deflatewriter.seek : not supported\n"); writer->error = 1; return(writer->error); }
int deflatewriter_write(const void *src, unsigned long size, unsigned int count, struct _writer *writer) { struct _deflatewriter *deflatewriter = CAST_UP(struct _deflatewriter,base,writer); unsigned int bytes; writer->error = 0; if (_mul_over_limit(size, count, 0xFFFFFFFF)) { _xlog("deflatewriter.write : overflow\n"); writer->error = 1; return(writer->error); } bytes = (unsigned int)size * count; _deflatewriter_output(deflatewriter, src, bytes, Z_NO_FLUSH); if (writer->error) _xlog("deflatewriter.write : error\n"); return(writer->error); }
/// zlib error void _deflatewriter_zerror(const char *funcname, int err) { switch(err) { case Z_MEM_ERROR: _xlog("deflatewriter.%s : Z_MEM_ERROR\n", funcname); break; case Z_BUF_ERROR: _xlog("deflatewriter.%s : Z_BUF_ERROR\n", funcname); break; case Z_STREAM_ERROR: _xlog("deflatewriter.%s : Z_STREAM_ERROR\n", funcname); break; case Z_DATA_ERROR: _xlog("deflatewriter.%s : Z_DATA_ERROR\n", funcname); break; default: _xlog("deflatewriter.%s : zlib error number %d\n", funcname, err); break; } }
int imf_save(const struct ROImf *imf, struct _writer *writer) { unsigned int layerId, actionId; unsigned int layercount; if (imf == NULL || writer == NULL || writer->error) { _xlog("imf.save : invalid argument (imf=%p writer=%p writer.error=%d)\n", imf, writer, writer->error); return(1); } if (imf_inspect(imf) == 0.0f) { _xlog("imf.save : invalid\n"); return(1); } if (imf->version != 1.01f) { _xlog("imf.save : unknown version v%f\n", imf->version); return(1); } writer->write(&imf->version, 4, 1, writer); writer->write(&imf->checksum, 4, 1, writer); writer->write(&imf->lastlayer, 4, 1, writer); layercount = (unsigned int)(imf->lastlayer + 1); for (layerId = 0; layerId < layercount; layerId++) { const struct ROImfLayer *layer = &imf->layers[layerId]; writer->write(&layer->actioncount, 4, 1, writer); for (actionId = 0; actionId < layer->actioncount; actionId++) { const struct ROImfAction *action = &layer->actions[actionId]; writer->write(&action->motioncount, 4, 1, writer); writer->write(action->motions, sizeof(struct ROImfMotion), action->motioncount, writer); } } if (writer->error) { _xlog("imf.save : write error\n"); return(1); } return(0); }
struct ROPal *pal_load(struct _reader *reader) { struct ROPal *ret; ret = (struct ROPal*)_xalloc(sizeof(struct ROPal)); reader->read(ret, sizeof(struct ROPal), 1, reader); if (reader->error) { // data was missing _xlog("PAL is incomplete or invalid\n"); pal_unload(ret); return(NULL); } return(ret); }
/// Output data to parent writer. /// flush=Z_FINISH : terminate the stream, writing all pending data void _deflatewriter_output(struct _deflatewriter *deflatewriter, const void *data, unsigned int length, int flush) { struct _writer *parent = deflatewriter->parent; unsigned char buf[0x8000]; // 32k unsigned int bytes; int err; deflatewriter->stream.next_in = (Bytef*)data; deflatewriter->stream.avail_in = (uInt)length; for(;;) { deflatewriter->stream.next_out = (Bytef*)buf; deflatewriter->stream.avail_out = sizeof(buf); err = deflate(&deflatewriter->stream, flush); if (err != Z_OK && err != Z_STREAM_END) { _deflatewriter_zerror("output",err); deflatewriter->base.error = 1; return; } bytes = sizeof(buf) - deflatewriter->stream.avail_out; if (bytes > 0) { parent->write(buf, 1, bytes, parent); if (parent->error) { _xlog("deflatewriter.output : parent.write error\n"); deflatewriter->base.error = 1; return; } } if (flush == Z_FINISH) { if (err == Z_STREAM_END) return; } else { if (deflatewriter->stream.avail_in == 0) return; } } }
void _xlog_log_hexdump( const char * file, const char * function, uint16_t line_no, int pri, const void * start_addr, uint16_t dump_byte_len, const void * print_address) { #define HEXDUMP_BYTES_PER_LINE (16) #define HEXDUMP_MAX_HEX_LENGTH (80) char *hex_buff_p; char * char_buff_p; char *hex_wr_p; const char *dump_p = (const char*) start_addr; unsigned long row_start_addr = (unsigned long)print_address & ~((unsigned long) (HEXDUMP_BYTES_PER_LINE - 1)); unsigned int first_row_start_column = (unsigned long)print_address % HEXDUMP_BYTES_PER_LINE; unsigned int column = 0; unsigned int bytes_left = dump_byte_len; unsigned int i; hex_buff_p = (char *)malloc(HEXDUMP_MAX_HEX_LENGTH + 1); char_buff_p = (char *)malloc(HEXDUMP_BYTES_PER_LINE + 1); hex_wr_p = hex_buff_p; if (pri <= sim.loglevel) { // Print the lead in for (i = 0; i < first_row_start_column; i++) { hex_wr_p += sprintf(hex_wr_p, ".. "); char_buff_p[column++] = ' '; } while (bytes_left) { hex_wr_p += sprintf(hex_wr_p, "%02X ", ((unsigned int)*dump_p) & 0xFF); if ((*dump_p >= ' ') && (*dump_p <= '~')) { char_buff_p[column] = *dump_p; } else { char_buff_p[column] = '.'; } dump_p++; column++; bytes_left--; if (column >= HEXDUMP_BYTES_PER_LINE) { // Print the completed line hex_buff_p[HEXDUMP_MAX_HEX_LENGTH] = '\0'; char_buff_p[HEXDUMP_BYTES_PER_LINE] = '\0'; _xlog(file, function, line_no, pri, "0x%08X: %s [%s]\n", row_start_addr, hex_buff_p, char_buff_p); row_start_addr += HEXDUMP_BYTES_PER_LINE; hex_wr_p = hex_buff_p; column = 0; } } if (column) { // Print the lead out for (i = column; i < HEXDUMP_BYTES_PER_LINE; i++) { hex_wr_p += sprintf(hex_wr_p, ".. "); char_buff_p[i] = ' '; } hex_buff_p[HEXDUMP_MAX_HEX_LENGTH] = '\0'; char_buff_p[HEXDUMP_BYTES_PER_LINE] = '\0'; _xlog(file, function, line_no, pri, "0x%08X: %s [%s]\n", row_start_addr, hex_buff_p, char_buff_p); } } free(hex_buff_p); free(char_buff_p); }
struct ROImf *imf_load(struct _reader *reader) { struct ROImf *ret; unsigned int layerId, actionId; if (reader == NULL || reader->error) { _xlog("imf.load : invalid argument (reader=%p reader.error=%d)\n", reader, reader->error); return(NULL); } ret = (struct ROImf*)_xalloc(sizeof(struct ROImf)); memset(ret, 0, sizeof(struct ROImf)); reader->read(&ret->version, 4, 1, reader); if (ret->version == 1.01f) ;// supported else { _xlog("imf.load : unknown version v%f\n", ret->version); imf_unload(ret); return(NULL); } reader->read(&ret->checksum, 4, 1, reader); reader->read(&ret->lastlayer, 4, 1, reader); if (ret->lastlayer >= 0) { unsigned int layercount = (unsigned int)ret->lastlayer + 1; if (layercount > IMF_MAX_LAYER_COUNT) { _xlog("imf.load : too many layers (%u)\n", layercount); imf_unload(ret); return(NULL); } ret->layers = (struct ROImfLayer*)_xalloc(sizeof(struct ROImfLayer) * layercount); memset(ret->layers, 0, sizeof(struct ROImfLayer) * layercount); for (layerId = 0; layerId < layercount; layerId++) { struct ROImfLayer *layer = &ret->layers[layerId]; reader->read(&layer->actioncount, 4, 1, reader); if (layer->actioncount > IMF_MAX_ACTION_COUNT) { _xlog("imf.load : [%u] too many actions (%u)\n", layerId, layer->actioncount); imf_unload(ret); return(NULL); } if (layer->actioncount > 0) { layer->actions = (struct ROImfAction*)_xalloc(sizeof(struct ROImfAction) * layer->actioncount); memset(layer->actions, 0, sizeof(struct ROImfAction) * layer->actioncount); for (actionId = 0; actionId < layer->actioncount; actionId++) { struct ROImfAction *action = &layer->actions[actionId]; reader->read(&action->motioncount, 4, 1, reader); if (action->motioncount > IMF_MAX_MOTION_COUNT) { _xlog("imf.load : [%u][%u] too many motions (%u)\n", layerId, actionId, action->motioncount); imf_unload(ret); return(NULL); } if (action->motioncount > 0) { action->motions = (struct ROImfMotion*)_xalloc(sizeof(struct ROImfMotion) * action->motioncount); reader->read(action->motions, sizeof(struct ROImfMotion), action->motioncount, reader); } } } } } if (reader->error) { _xlog("imf.load : read error\n"); imf_unload(ret); return(NULL); } return(ret); }
float imf_inspect(const struct ROImf *imf) { unsigned int layerId, actionId; if (imf == NULL) { _xlog("imf.inspect : invalid argument (imf=%p)\n", imf); return(0.0f); } if (imf->lastlayer < -1) { _xlog("imf.inspect : invalid lastlayer (%d)\n", imf->lastlayer); return(0.0f); } if (imf->lastlayer == -1 && imf->layers != NULL) { _xlog("imf.inspect : expected NULL layers\n"); return(0.0f); } if (imf->lastlayer > -1) { unsigned int layercount = (unsigned int)imf->lastlayer + 1; if (imf->layers == NULL) { _xlog("imf.inspect : expected non-NULL layers\n"); return(0.0f); } if (layercount > IMF_MAX_LAYER_COUNT) { _xlog("imf.inspect : too many layers (%u)\n", layercount); return(0.0f); } for (layerId = 0; layerId < layercount; layerId++) { const struct ROImfLayer *layer = &imf->layers[layerId]; if (layer->actioncount > 0 && layer->actions == NULL) { _xlog("imf.inspect : [%u] expected non-NULL actions\n", layerId); return(0.0f); } if (layer->actioncount == 0 && layer->actions != NULL) { _xlog("imf.inspect : [%u] expected NULL actions\n", layerId); return(0.0f); } if (layer->actioncount > IMF_MAX_ACTION_COUNT) { _xlog("imf.inspect : [%u] too many actions (%u)\n", layerId, layer->actioncount); return(0.0f); } for (actionId = 0; actionId < layer->actioncount; actionId++) { const struct ROImfAction *action = &layer->actions[actionId]; if (action->motioncount > 0 && action->motions == NULL) { _xlog("imf.inspect : [%u][%u] expected non-NULL motions\n", layerId, actionId); return(0.0f); } if (action->motioncount == 0 && action->motions != NULL) { _xlog("imf.inspect : [%u][%u] expected NULL motions\n", layerId, actionId); return(0.0f); } if (action->motioncount > IMF_MAX_MOTION_COUNT) { _xlog("imf.inspect : [%u][%u] too many actions (%u)\n", layerId, actionId, action->motioncount); return(0.0f); } } } } return(1.01f); }
struct RORsm *rsm_load(struct _reader *reader) { struct RORsm *ret; ret = (struct RORsm*)_xalloc(sizeof(struct RORsm)); reader->read(ret->magic, 4, 1, reader); reader->read(&ret->version, 2, 1, reader); if (strncmp("GRSM", ret->magic, 4) != 0) { _xlog("Invalid RSM header: '%c%c%c%c'\n", ret->magic[0], ret->magic[1], ret->magic[2], ret->magic[3]); _xfree(ret); return(NULL); } //_xlog("RSM Version: %u.%u\n", (unsigned int)ret->v.major, (unsigned int)ret->v.minor); reader->read(&ret->anim_length, sizeof(int), 1, reader); reader->read(&ret->shade_type, sizeof(int), 1, reader); if (ret->v.major > 1 || (ret->v.major == 1 && ret->v.minor >= 4)) { // Versions 1.4 and up reader->read(&ret->alpha, sizeof(unsigned char), 1, reader); } else { ret->alpha = 0xff; } reader->read(ret->reserved, 16, 1, reader); reader->read(&ret->texture_count, sizeof(int), 1, reader); // Load Textures if (ret->texture_count > 0) { int i; char texname[40]; ret->textures = (char**)_xalloc(sizeof(char*) * ret->texture_count); for (i = 0; i < ret->texture_count; i++) { reader->read(texname, 40, 1, reader); texname[39] = 0; ret->textures[i] = (char*)_xalloc(sizeof(char) * (strlen(texname) + 1)); strcpy(ret->textures[i], texname); } } else { ret->textures = NULL; } // Nodes reader->read(ret->main_node, 40, 1, reader); reader->read(&ret->node_count, sizeof(int), 1, reader); if (ret->node_count > 0) { int i; struct RORsmNode *currentNode; ret->nodes = (struct RORsmNode*)_xalloc(sizeof(struct RORsmNode) * ret->node_count); for (i = 0; i < ret->node_count; i++) { currentNode = &ret->nodes[i]; reader->read(currentNode->name, 40, 1, reader); reader->read(currentNode->parent, 40, 1, reader); reader->read(¤tNode->texture_count, sizeof(int), 1, reader); if (currentNode->texture_count > 0) { currentNode->textures = (int*)_xalloc(sizeof(int) * currentNode->texture_count); reader->read(currentNode->textures, sizeof(int), currentNode->texture_count, reader); } else { currentNode->textures = NULL; } reader->read(currentNode->offsetMT, sizeof(float), 9, reader); reader->read(¤tNode->pos_, sizeof(struct RORsmVertex), 1, reader); reader->read(¤tNode->pos, sizeof(struct RORsmVertex), 1, reader); reader->read(¤tNode->rot_angle, sizeof(float), 1, reader); reader->read(¤tNode->rot_axis, sizeof(struct RORsmVertex), 1, reader); reader->read(¤tNode->scale, sizeof(struct RORsmVertex), 1, reader); // Node vertexes reader->read(¤tNode->vertice_count, sizeof(int), 1, reader); if (currentNode->vertice_count > 0) { currentNode->vertices = (struct RORsmVertex*)_xalloc(sizeof(struct RORsmVertex) * currentNode->vertice_count); reader->read(currentNode->vertices, sizeof(struct RORsmVertex), currentNode->vertice_count, reader); } else { currentNode->vertices = NULL; } // Texture vertices reader->read(¤tNode->texv_count, sizeof(int), 1, reader); if (currentNode->texv_count > 0) { currentNode->texv = (struct RORsmTexture*)_xalloc(sizeof(struct RORsmTexture) * currentNode->texv_count); if (ret->v.major > 1 || (ret->v.major == 1 && ret->v.minor >= 2)) { // Versions 1.2 and up reader->read(currentNode->texv, sizeof(struct RORsmTexture), currentNode->texv_count, reader); } else { int j; for (j = 0; j < currentNode->texture_count; i++) { currentNode->texv[j].color = 0xffffffff; reader->read(¤tNode->texv[j].u, sizeof(float), 2, reader); } } } else { currentNode->texv = NULL; } // Faces reader->read(¤tNode->face_count, sizeof(int), 1, reader); if (currentNode->face_count > 0) { currentNode->faces = (struct RORsmFace*)_xalloc(sizeof(struct RORsmFace) * currentNode->face_count); if (ret->v.major > 1 || (ret->v.major == 1 && ret->v.minor >= 2)) { // Versions 1.2 and up reader->read(currentNode->faces, sizeof(struct RORsmFace), currentNode->face_count, reader); } else { int j; for (j = 0; j < currentNode->face_count; j++) { reader->read(¤tNode->faces[j], sizeof(struct RORsmFace) - sizeof(int), 1, reader); currentNode->faces[j].smoothGroup = 0; } } } else { currentNode->faces = NULL; } // Position keyframes if (ret->v.major > 1 || (ret->v.major == 2 && ret->v.minor >= 5)) { // Versions 1.5 and up reader->read(¤tNode->poskey_count, sizeof(int), 1, reader); } else { currentNode->poskey_count = 0; } if (currentNode->poskey_count > 0) { currentNode->poskeys = (struct RORsmPosKeyframe*)_xalloc(sizeof(struct RORsmPosKeyframe) * currentNode->poskey_count); reader->read(currentNode->poskeys, sizeof(struct RORsmPosKeyframe), currentNode->poskey_count, reader); } else { currentNode->poskeys = NULL; } // Rotation keyframes reader->read(¤tNode->rotkey_count, sizeof(int), 1, reader); if (currentNode->rotkey_count > 0) { struct RORsmRotKeyframe* x; int rotkeyframe_size = sizeof(struct RORsmRotKeyframe) * currentNode->rotkey_count; x = _xalloc(rotkeyframe_size); currentNode->rotkeys = x; reader->read(currentNode->rotkeys, sizeof(struct RORsmRotKeyframe), currentNode->rotkey_count, reader); } else { currentNode->rotkeys = NULL; } } } else { ret->nodes = NULL; } if (reader->error) { // data was missing _xlog("RSM is incomplete or invalid\n"); rsm_unload(ret); ret = NULL; } return(ret); }