1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
/* AbiWord
* Copyright (C) 2001 Sean Young <sean@mess.org>
* 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., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gsf/gsf-input.h>
#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<unsigned char *>(malloc(size));
if (!blob)
{
UT_WARNINGMSG(("read_wri_struct: Out of memory!\n"));
return false;
}
if (!gsf_input_read(f, size, blob))
{
UT_WARNINGMSG(("read_wri_struct: File not big enough!\n"));
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<char *>(malloc(w[i].size));
if (!w[i].data)
{
UT_WARNINGMSG(("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! */
UT_WARNINGMSG(("wri_struct_value: 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, int spaces)
{
#ifdef DEBUG
char sp[16], format[48], x[8];
sprintf(sp, "%%-%d.%ds", spaces, spaces);
for (int i = 0; w[i].name; i++)
{
switch (w[i].type)
{
case CT_VALUE:
sprintf(x, "%%0%dX", w[i].size << 1);
sprintf(format, "%s%%-13.13s= 0x%s (%%d)\n", sp, x);
UT_DEBUGMSG((format, " ", w[i].name, w[i].value, w[i].value));
break;
case CT_BLOB:
sprintf(format, "%s%%-13.13s: tblob (%%d)\n", sp);
UT_DEBUGMSG((format, " ", w[i].name, w[i].size));
break;
case CT_IGNORE:
sprintf(format, "%s%%-13.13s ignored\n", sp);
UT_DEBUGMSG((format, " ", w[i].name));
break;
}
}
#else
UT_UNUSED(w);
UT_UNUSED(spaces);
#endif
}
|