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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
|
/*
* gretl -- Gnu Regression, Econometrics and Time-series Library
* Copyright (C) 2001 Allin Cottrell and Riccardo "Jack" Lucchetti
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
/*
Mostly borrowed from Gnumeric's Excel importer, written by Michael Meeks
*/
#include "libgretl.h"
#include "importer.h"
#include "biff.h"
typedef struct _BiffBoundsheetData BiffBoundsheetData;
struct _BiffBoundsheetData
{
guint16 index;
guint32 streamStartPos;
MsBiffFileType type;
char *name;
};
BiffQuery *ms_biff_query_new (MsOleStream *ptr)
{
BiffQuery *bq;
if (ptr == NULL) return NULL;
bq = g_new0(BiffQuery, 1);
bq->opcode = 0;
bq->length = 0;
bq->data_malloced = 0;
bq->pos = ptr;
return bq;
}
/**
* Returns 0 if has hit end
**/
int ms_biff_query_next (BiffQuery *bq)
{
guint8 tmp[4];
int ans = 1;
if (bq == NULL || bq->pos->position >= bq->pos->size) {
return 0;
}
if (bq->data_malloced) {
g_free(bq->data);
bq->data = NULL;
bq->data_malloced = 0;
}
bq->streamPos = bq->pos->position;
if (!bq->pos->read_copy(bq->pos, tmp, 4)) {
return 0;
}
bq->opcode = MS_OLE_GET_GUINT16(tmp);
bq->length = MS_OLE_GET_GUINT16(tmp+2);
bq->ms_op = (bq->opcode >> 8);
bq->ls_op = (bq->opcode & 0xff);
if (bq->length > 0 &&
!(bq->data = bq->pos->read_ptr(bq->pos, bq->length))) {
bq->data = g_new0(guint8, bq->length);
if (!bq->pos->read_copy(bq->pos, bq->data, bq->length)) {
ans = 0;
g_free(bq->data);
bq->data = NULL;
bq->length = 0;
} else {
bq->data_malloced = 1;
}
}
if (bq->length == 0) {
bq->data = NULL;
return 1;
}
return ans;
}
void ms_biff_query_destroy (BiffQuery *bq)
{
if (bq != NULL) {
if (bq->data_malloced) {
g_free(bq->data);
bq->data = NULL;
bq->data_malloced = 0;
}
g_free(bq);
}
}
static gboolean
biff_string_get_flags (const guint8 *ptr, gboolean *word_chars,
gboolean *extended, gboolean *rich)
{
guint8 header;
header = MS_OLE_GET_GUINT8(ptr);
if (((header & 0xf2) == 0)) {
*word_chars = (header & 0x1) != 0;
*extended = (header & 0x4) != 0;
*rich = (header & 0x8) != 0;
return TRUE;
} else {
*word_chars = 0;
*extended = 0;
*rich = 0;
return FALSE;
}
}
static char *biff_get_text (guint8 const *ptr, guint32 len)
{
char *ans = NULL;
gboolean header;
gboolean high_byte;
gboolean ext_str;
gboolean rich_str;
guint32 i;
if (len == 0) {
return NULL;
}
ans = (char *) g_new (char, len + 2);
header = biff_string_get_flags(ptr,
&high_byte,
&ext_str,
&rich_str);
if (header) ptr += 1;
if (rich_str) ptr += 2;
if (ext_str) ptr += 4;
for (i=0; i<len; i++) {
guint16 c;
guint8 header;
if (((header = MS_OLE_GET_GUINT8(ptr)) & 0xf2) == 0) {
high_byte = (header & 0x1) != 0;
ext_str = (header & 0x4) != 0;
rich_str = (header & 0x8) != 0;
ptr += 1;
i--;
} else if (high_byte) {
c = MS_OLE_GET_GUINT16(ptr);
ptr += 2;
ans[i] = (char) c;
} else {
c = MS_OLE_GET_GUINT8(ptr);
ptr += 1;
ans[i] = (char) c;
}
}
if (i > 0) {
ans[i] = '\0';
} else {
g_warning("Warning unterminated string floating");
}
return ans;
}
static BiffBoundsheetData *
biff_boundsheet_data_new (BiffQuery *q, MsBiffVersion ver)
{
BiffBoundsheetData *ans = NULL;
guint32 startpos;
if (ver != MS_BIFF_V5 && ver != MS_BIFF_V7 && ver != MS_BIFF_V8) {
printf ("Unknown BIFF Boundsheet spec. Assuming same as Biff7\n");
ver = MS_BIFF_V7;
}
startpos = MS_OLE_GET_GUINT32(q->data);
if (!(startpos == MS_OLE_GET_GUINT32(q->data))) {
return NULL;
}
if (MS_OLE_GET_GUINT8(q->data + 4) == 0 &&
((MS_OLE_GET_GUINT8(q->data + 5)) & 0x3) == 0) {
/* worksheet, visible */
ans = g_malloc(sizeof *ans);
ans->streamStartPos = startpos;
ans->name = biff_get_text(q->data + 7,
MS_OLE_GET_GUINT8(q->data + 6));
}
return ans;
}
static MsBiffBofData *ms_biff_bof_data_new (BiffQuery *q)
{
MsBiffBofData *ans = g_new (MsBiffBofData, 1);
if ((q->opcode & 0xff) == BIFF_BOF && (q->length >= 4)) {
switch (q->opcode >> 8) {
case 0: ans->version = MS_BIFF_V2;
break;
case 2: ans->version = MS_BIFF_V3;
break;
case 4: ans->version = MS_BIFF_V4;
break;
case 8: {
switch (MS_OLE_GET_GUINT16 (q->data)) {
case 0x0600: ans->version = MS_BIFF_V8;
break;
case 0x500:
ans->version = MS_BIFF_V7;
break;
default:
printf("Unknown BIFF sub-number in BOF %x\n", q->opcode);
ans->version = MS_BIFF_V_UNKNOWN;
}
break;
}
default:
printf("Unknown BIFF number in BOF %x\n", q->opcode);
ans->version = MS_BIFF_V_UNKNOWN;
printf("Biff version %d\n", ans->version);
}
switch (MS_OLE_GET_GUINT16(q->data + 2)) {
case 0x0005: ans->type = MS_BIFF_TYPE_Workbook; break;
case 0x0006: ans->type = MS_BIFF_TYPE_VBModule; break;
case 0x0010: ans->type = MS_BIFF_TYPE_Worksheet; break;
case 0x0020: ans->type = MS_BIFF_TYPE_Chart; break;
case 0x0040: ans->type = MS_BIFF_TYPE_Macrosheet; break;
case 0x0100: ans->type = MS_BIFF_TYPE_Workspace; break;
default:
ans->type = MS_BIFF_TYPE_Unknown;
printf("Unknown BIFF type in BOF %x\n",
MS_OLE_GET_GUINT16 (q->data + 2));
break;
}
} else {
printf("Not a BOF!\n");
ans->version = MS_BIFF_V_UNKNOWN;
ans->type = MS_BIFF_TYPE_Unknown;
}
return ans;
}
static void
ms_biff_bof_data_destroy (MsBiffBofData *data)
{
g_free(data);
}
static void
ms_excel_read_bof (BiffQuery *q, MsBiffBofData **version)
{
/* The first BOF seems to be OK, the rest lie? */
MsBiffVersion vv = MS_BIFF_V_UNKNOWN;
MsBiffBofData *ver = *version;
if (ver) {
vv = ver->version;
ms_biff_bof_data_destroy(ver);
}
*version = ver = ms_biff_bof_data_new(q);
if (vv != MS_BIFF_V_UNKNOWN) {
ver->version = vv;
}
if (ver->type == MS_BIFF_TYPE_Workbook) {
if (ver->version >= MS_BIFF_V8) {
guint32 ver = MS_OLE_GET_GUINT32 (q->data + 4);
if (ver == 0x4107cd18) {
printf("Excel 2000 ?\n");
} else {
printf("Excel 97 +\n");
}
} else if (ver->version >= MS_BIFF_V7)
printf("Excel 95\n");
else if (ver->version >= MS_BIFF_V5)
printf("Excel 5.x\n");
else if (ver->version >= MS_BIFF_V4)
printf("Excel 4.x\n");
else if (ver->version >= MS_BIFF_V3)
printf("Excel 3.x\n");
else if (ver->version >= MS_BIFF_V2)
printf("Excel 2.x\n");
} else if (ver->type == MS_BIFF_TYPE_Worksheet) {
; /* printf ("Got worksheet\n"); */
} else if (ver->type == MS_BIFF_TYPE_Chart) {
; /* printf ("Chart.\n"); */
} else if (ver->type == MS_BIFF_TYPE_VBModule ||
ver->type == MS_BIFF_TYPE_Macrosheet) {
/* printf ("VB Module or Macrosheet.\n"); */
while (ms_biff_query_next (q) && q->opcode != BIFF_EOF)
;
if (q->opcode != BIFF_EOF)
g_warning ("XLS file format error. Missing BIFF_EOF");
} else {
printf ("Unknown BOF (%x)\n", ver->type);
}
}
static int extend_xf_list (wbook *book, int fmt)
{
int *xf_list = NULL;
int nxf = 0, ret = 0;
if (book->xf_list == NULL) {
xf_list = malloc(2 * sizeof *xf_list);
} else {
nxf = book->xf_list[0];
xf_list = realloc(book->xf_list, (nxf + 2) * sizeof *xf_list);
}
if (xf_list != NULL) {
nxf++;
xf_list[0] = nxf;
xf_list[nxf] = fmt;
book->xf_list = xf_list;
} else {
ret = 1;
}
return ret;
}
static int
ms_excel_read_workbook (MsOle *file, BiffBoundsheetData ***bounds,
wbook *book)
{
MsOleStream *stream;
MsOleErr result;
BiffQuery *q;
MsBiffBofData *ver = NULL;
char *problem_loading = NULL;
int excel_version = MS_BIFF_V_UNKNOWN;
result = ms_ole_stream_open_workbook(&stream, file);
if (result != MS_OLE_ERR_OK) {
return excel_version;
}
q = ms_biff_query_new(stream);
while (problem_loading == NULL && ms_biff_query_next(q)) {
if (0x1 == q->ms_op) {
switch (q->opcode) {
case BIFF_DSF:
case BIFF_XL9FILE:
case BIFF_REFRESHALL:
case BIFF_USESELFS:
case BIFF_TABID:
case BIFF_PROT4REV:
case BIFF_PROT4REVPASS:
case BIFF_CODENAME:
case BIFF_SUPBOOK:
break;
default:
#ifdef EDEBUG
fprintf(stderr, "Got unexpected BIFF token 0x%x\n", q->opcode);
#else
;
#endif
}
continue;
}
switch (q->ls_op) {
case BIFF_BOF:
#ifdef EDEBUG
fprintf(stderr, "Got BIFF_BOF\n");
#endif
ms_excel_read_bof(q, &ver);
break;
case BIFF_EOF:
#ifdef EDEBUG
fprintf(stderr, "Got BIFF_EOF\n");
#endif
break;
case BIFF_BOUNDSHEET: {
BiffBoundsheetData *ans;
#ifdef EDEBUG
fprintf(stderr, "Got BIFF_BOUNDSHEET\n");
#endif
ans = biff_boundsheet_data_new(q, ver->version);
if (ans != NULL) {
*bounds = g_realloc(*bounds, (book->nsheets + 1) * sizeof **bounds);
(*bounds)[book->nsheets] = ans;
book->nsheets += 1;
}
break;
}
case BIFF_PALETTE:
case BIFF_FONT:
case BIFF_XF_OLD:
break;
case BIFF_XF:
if (q->data != NULL) {
guint16 val = (guint16) *(q->data + 2);
extend_xf_list(book, (int) val);
}
break;
case BIFF_SST:
case BIFF_EXTSST:
break;
case BIFF_EXTERNSHEET: /* See: S59D82.HTM */
/* fprintf(stderr, "Got BIFF_EXTERNSHEET\n"); */
break;
case BIFF_FORMAT:
#if 0
fprintf(stderr, "got BIFF_FORMAT: index ");
if (q->data != NULL) {
guint16 val = (guint16) *q->data;
fprintf(stderr, "%d\n", (int) val);
} else {
fputs("unknown\n", stderr);
}
#endif
break;
case BIFF_EXTERNCOUNT:
case BIFF_CODEPAGE:
case BIFF_OBJPROTECT:
case BIFF_PROTECT:
case BIFF_PASSWORD:
break;
case BIFF_FILEPASS:
/* All records after this are encrypted */
problem_loading = g_strdup (_("Password protected workbooks "
"are not supported yet."));
break;
case BIFF_STYLE:
case BIFF_WINDOWPROTECT:
break;
case BIFF_EXTERNNAME:
#ifdef EDEBUG
fprintf(stderr, "Got BIFF_EXTERNNAME\n");
#endif
break;
case BIFF_NAME:
#ifdef EDEBUG
fprintf(stderr, "Got BIFF_NAME\n");
#endif
break;
case BIFF_1904:
fprintf(stderr, "Got BIFF_1904: value ");
if (q->data != NULL) {
guint16 val = (guint16) *q->data;
fprintf(stderr, "%d\n", (int) val);
if (val) {
book_set_base_1904(book);
}
} else {
fputs("unknown\n", stderr);
}
break;
case BIFF_WRITEACCESS:
case BIFF_HIDEOBJ:
case BIFF_FNGROUPCOUNT:
case BIFF_MMS:
case BIFF_OBPROJ:
case BIFF_BOOKBOOL:
case BIFF_COUNTRY:
case BIFF_INTERFACEHDR:
case BIFF_INTERFACEEND:
case BIFF_WINDOW1:
case BIFF_SELECTION: /* 0, NOT 10 */
break;
case BIFF_DIMENSIONS: /* 2, NOT 1,10 */
#ifdef EDEBUG
fprintf(stderr, "Got BIFF_DIMENSIONS\n");
#endif
/* ms_excel_biff_dimensions (q, wb); */
break;
case BIFF_OBJ: /* See: ms-obj.c and S59DAD.HTM */
case BIFF_SCL:
break;
case BIFF_MS_O_DRAWING:
case BIFF_MS_O_DRAWING_GROUP:
case BIFF_MS_O_DRAWING_SELECTION:
case BIFF_ADDMENU:
break;
default:
/* fprintf(stderr, "ms_excel_unexpected_biff\n"); */
break;
}
}
ms_biff_query_destroy(q);
if (ver) {
excel_version = ver->version;
ms_biff_bof_data_destroy(ver);
}
ms_ole_stream_close(&stream);
return excel_version;
}
/* public interface */
int excel_book_get_info (const char *fname, wbook *book)
{
MsOleErr ole_error;
MsOle *f;
BiffBoundsheetData **bounds = NULL;
int i;
ole_error = ms_ole_open(&f, fname);
if (ole_error != MS_OLE_ERR_OK) {
char const *msg = (ole_error == MS_OLE_ERR_INVALID ||
ole_error == MS_OLE_ERR_FORMAT) ?
_("This file is not an 'OLE' file -- it may be too "
"old for gretl to read\n")
: _("Unexpected error reading the file\n");
ms_ole_destroy(&f);
fputs(msg, stderr);
return 1;
}
book->version = ms_excel_read_workbook(f, &bounds, book);
ms_ole_destroy(&f);
if (book->nsheets == 0 || bounds == NULL) {
return 1;
}
book->sheetnames = g_malloc(book->nsheets * sizeof *book->sheetnames);
if (book->sheetnames == NULL) return 1;
book->byte_offsets = g_malloc(book->nsheets * sizeof *book->byte_offsets);
if (book->byte_offsets == NULL) return 1;
for (i=0; i<book->nsheets; i++) {
if (bounds[i]->name == NULL) {
book->sheetnames[i] = g_strdup("");
} else {
book->sheetnames[i] = bounds[i]->name;
tailstrip(book->sheetnames[i]);
}
book->byte_offsets[i] = bounds[i]->streamStartPos;
g_free(bounds[i]);
}
g_free(bounds);
return 0;
}
|