/* AbiWord * Copyright (C) 2001 Sean Young * Copyright (C) 2010-2011 Ingo Brueckl * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA * 02111-1307, USA. */ #include #include #include #include #include "ie_impexp_MSWrite.h" #include "ut_debugmsg.h" #include "ut_types.h" bool read_wri_struct (wri_struct *w, GsfInput *f) { int i, size; unsigned char *blob; bool result; // first we need to calculate the size i = size = 0; while (w[i].name) size += w[i++].size; // got the size, read the blob blob = static_cast(malloc(size)); if (!blob) { fprintf(stderr, "read_wri_struct: Out of memory!\n"); return false; } if (!gsf_input_read(f, size, blob)) { perror("read_wri_struct: File not big enough!"); return false; } result = read_wri_struct_mem(w, blob); free(blob); return result; } bool read_wri_struct_mem (wri_struct *w, unsigned char *blob) { int n; for (int i = 0; w[i].name; i++) { switch (w[i].type) { case CT_VALUE: n = w[i].size; w[i].value = 0; while (n--) w[i].value = (w[i].value * 256) + blob[n]; break; case CT_BLOB: w[i].data = static_cast(malloc(w[i].size)); if (!w[i].data) { fprintf(stderr, "read_wri_struct_mem: Out of memory!\n"); return false; } memcpy(w[i].data, blob, w[i].size); break; case CT_IGNORE: break; } blob += w[i].size; } return true; } int wri_struct_value (const wri_struct *w, const char *name) { for (int i = 0; w[i].name; i++) if (strcmp(w[i].name, name) == 0) return w[i].value; /* This should never happen! */ fprintf(stderr, "Internal error: '%s' not found!\n", name); exit(1); return 0; } void free_wri_struct (wri_struct *w) { for (int i = 0; w[i].name; i++) { w[i].value = 0; if (w[i].data) { free(w[i].data); w[i].data = NULL; } } } void debug_wri_struct (wri_struct *w) { #ifdef DEBUG for (int i = 0; w[i].name; i++) { switch (w[i].type) { case CT_VALUE: UT_DEBUGMSG(("%s:\t0x%04x\n", w[i].name, w[i].value)); break; case CT_BLOB: UT_DEBUGMSG(("%s:\tblob (%d)\n", w[i].name, w[i].size)); break; case CT_IGNORE: UT_DEBUGMSG(("%s:\tignored\n", w[i].name)); break; } } UT_DEBUGMSG(("\n")); #else UT_UNUSED(w); #endif }