aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/ole2_extractor.c
blob: a48b726c7fe7e1f7a94cc96b2aa630e9a1568de3 (plain) (blame)
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
/*
     This file is part of libextractor.
     Copyright (C) 2004, 2005, 2006, 2007, 2009, 2012, 2018 Vidyut Samanta and Christian Grothoff

     libextractor 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, or (at your
     option) any later version.

     libextractor 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 libextractor; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.

     This code makes extensive use of libgsf
     -- the Gnome Structured File Library
     Copyright Copyright (C) 2002-2004 Jody Goldberg (jody@gnome.org)

     Part of this code was adapted from wordleaker.
*/
/**
 * @file plugins/ole2_extractor.c
 * @brief plugin to support OLE2 (DOC, XLS, etc.) files
 * @author Christian Grothoff
 */
#include "platform.h"
#include "extractor.h"
#include "convert.h"
#include <glib-object.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <gsf/gsf-utils.h>
#include <gsf/gsf-input-impl.h>
#include <gsf/gsf-input-memory.h>
#include <gsf/gsf-impl-utils.h>
#include <gsf/gsf-infile.h>
#include <gsf/gsf-infile-msole.h>
#include <gsf/gsf-msole-utils.h>


/**
 * Set to 1 to use our own GsfInput subclass which supports seeking
 * and thus can handle very large files.  Set to 0 to use the simple
 * gsf in-memory buffer (which can only access the first ~16k) for
 * debugging.
 */
#define USE_LE_INPUT 1


/**
 * Give the given UTF8 string to LE by calling 'proc'.
 *
 * @param proc callback to invoke
 * @param proc_cls closure for proc
 * @param phrase metadata string to pass; may include spaces
 *        just double-quotes or just a space in a double quote;
 *        in those cases, nothing should be done
 * @param type meta data type to use
 * @return if 'proc' returned 1, otherwise 0
 */
static int
add_metadata (EXTRACTOR_MetaDataProcessor proc,
	      void *proc_cls,
	      const char *phrase,
	      enum EXTRACTOR_MetaType type)
{
  char *tmp;
  int ret;

  if (0 == strlen (phrase))
    return 0;
  if (0 == strcmp (phrase, "\"\""))
    return 0;
  if (0 == strcmp (phrase, "\" \""))
    return 0;
  if (0 == strcmp (phrase, " "))
    return 0;
  if (NULL == (tmp = strdup (phrase)))
    return 0;

  while ( (strlen (tmp) > 0) &&
	  (isblank ((unsigned char) tmp [strlen (tmp) - 1])) )
    tmp [strlen (tmp) - 1] = '\0';
  ret = proc (proc_cls,
	      "ole2",
	      type,
	      EXTRACTOR_METAFORMAT_UTF8,
	      "text/plain",
	      tmp,
	      strlen (tmp) + 1);
  free (tmp);
  return ret;
}


/**
 * Entry in the map from OLE meta type  strings
 * to LE types.
 */
struct Matches
{
  /**
   * OLE description.
   */
  const char *text;

  /**
   * Corresponding LE type.
   */
  enum EXTRACTOR_MetaType type;
};


static struct Matches tmap[] = {
  { "Title", EXTRACTOR_METATYPE_TITLE },
  { "PresentationFormat", EXTRACTOR_METATYPE_FORMAT },
  { "Category", EXTRACTOR_METATYPE_SECTION },
  { "Manager", EXTRACTOR_METATYPE_MANAGER },
  { "Company", EXTRACTOR_METATYPE_COMPANY },
  { "Subject", EXTRACTOR_METATYPE_SUBJECT },
  { "Author", EXTRACTOR_METATYPE_AUTHOR_NAME },
  { "Keywords", EXTRACTOR_METATYPE_KEYWORDS },
  { "Comments", EXTRACTOR_METATYPE_COMMENT },
  { "Template", EXTRACTOR_METATYPE_TEMPLATE },
  { "NumPages", EXTRACTOR_METATYPE_PAGE_COUNT },
  { "AppName", EXTRACTOR_METATYPE_PRODUCED_BY_SOFTWARE },
  { "RevisionNumber", EXTRACTOR_METATYPE_REVISION_NUMBER },
  { "NumBytes", EXTRACTOR_METATYPE_EMBEDDED_FILE_SIZE },
  { "CreatedTime", EXTRACTOR_METATYPE_CREATION_DATE },
  { "LastSavedTime" , EXTRACTOR_METATYPE_MODIFICATION_DATE },
  { "gsf:company", EXTRACTOR_METATYPE_COMPANY },
  { "gsf:character-count", EXTRACTOR_METATYPE_CHARACTER_COUNT },
  { "gsf:page-count", EXTRACTOR_METATYPE_PAGE_COUNT },
  { "gsf:line-count", EXTRACTOR_METATYPE_LINE_COUNT },
  { "gsf:word-count", EXTRACTOR_METATYPE_WORD_COUNT },
  { "gsf:paragraph-count", EXTRACTOR_METATYPE_PARAGRAPH_COUNT },
  { "gsf:last-saved-by", EXTRACTOR_METATYPE_LAST_SAVED_BY },
  { "gsf:manager", EXTRACTOR_METATYPE_MANAGER },
  { "dc:title", EXTRACTOR_METATYPE_TITLE },
  { "dc:creator", EXTRACTOR_METATYPE_CREATOR },
  { "dc:date", EXTRACTOR_METATYPE_UNKNOWN_DATE },
  { "dc:subject", EXTRACTOR_METATYPE_SUBJECT },
  { "dc:keywords", EXTRACTOR_METATYPE_KEYWORDS },
  { "dc:last-printed", EXTRACTOR_METATYPE_LAST_PRINTED },
  { "dc:description", EXTRACTOR_METATYPE_DESCRIPTION },
  { "meta:creation-date", EXTRACTOR_METATYPE_CREATION_DATE },
  { "meta:generator", EXTRACTOR_METATYPE_CREATED_BY_SOFTWARE },
  { "meta:template", EXTRACTOR_METATYPE_TEMPLATE },
  { "meta:editing-cycles", EXTRACTOR_METATYPE_EDITING_CYCLES },
  /* { "Dictionary", EXTRACTOR_METATYPE_LANGUAGE },  */
  /* { "gsf:security", EXTRACTOR_SECURITY }, */
  /* { "gsf:scale", EXTRACTOR_SCALE }, // always "false"? */
  /* { "meta:editing-duration", EXTRACTOR_METATYPE_TOTAL_EDITING_TIME }, // encoding? */
  /* { "msole:codepage", EXTRACTOR_CHARACTER_SET }, */
  { NULL, 0 }
};


/**
 * Closure for 'process_metadata'.
 */
struct ProcContext
{
  /**
   * Function to call for meta data that was found.
   */
  EXTRACTOR_MetaDataProcessor proc;

  /**
   * Closure for @e proc.
   */
  void *proc_cls;

  /**
   * Return value; 0 to continue to extract, 1 if we are done
   */
  int ret;
};


/**
 * Function invoked by 'gst_msole_metadata_read' with
 * metadata found in the document.
 *
 * @param key 'const char *' describing the meta data
 * @param value the UTF8 representation of the meta data
 * @param user_data our 'struct ProcContext' (closure)
 */
static void
process_metadata (gpointer key,
		  gpointer value,
		  gpointer user_data)
{
  const char *type = key;
  const GsfDocProp *prop = value;
  struct ProcContext *pc = user_data;
  const GValue *gval;
  char *contents;
  int pos;

  if ( (NULL == key) ||
       (NULL == value) )
    return;
  if (0 != pc->ret)
    return;
  gval = gsf_doc_prop_get_val (prop);

  if (G_VALUE_TYPE(gval) == G_TYPE_STRING)
    {
      const char *gvals;

      gvals = g_value_get_string (gval);
      if (NULL == gvals)
        return;
      contents = strdup (gvals);
    }
  else
    {
      /* convert other formats? */
      contents = g_strdup_value_contents (gval);
    }
  if (NULL == contents)
    return;
  if (0 == strcmp (type,
                   "meta:generator"))
    {
      const char *mimetype = "application/vnd.ms-files";
      struct {
        const char *v;
        const char *m;
      } mm[] = {
        { "Microsoft Word", "application/msword" },
        { "Microsoft Office Word", "application/msword" },
        { "Microsoft Excel", "application/vnd.ms-excel" },
        { "Microsoft Office Excel", "application/vnd.ms-excel" },
        { "Microsoft PowerPoint", "application/vnd.ms-powerpoint" },
        { "Microsoft Office PowerPoint", "application/vnd.ms-powerpoint"},
        { "Microsoft Project", "application/vnd.ms-project" },
        { "Microsoft Visio", "application/vnd.visio" },
        { "Microsoft Office", "application/vnd.ms-office" },
        { NULL, NULL }
      };
      int i;

      for (i=0;NULL != mm[i].v; i++)
        if (0 == strncmp (value,
                          mm[i].v,
                          strlen (mm[i].v) + 1))
          {
            mimetype = mm[i].m;
            break;
          }
      if (0 != add_metadata (pc->proc,
			     pc->proc_cls,
			     mimetype,
			     EXTRACTOR_METATYPE_MIMETYPE))
	{
	  free (contents);
	  pc->ret = 1;
	  return;
	}
    }
  for (pos = 0; NULL != tmap[pos].text; pos++)
    if (0 == strcmp (tmap[pos].text,
		     type))
      break;
  if ( (NULL != tmap[pos].text) &&
       (0 != add_metadata (pc->proc, pc->proc_cls,
			   contents,
			   tmap[pos].type)) )
    {
      free (contents);
      pc->ret = 1;
      return;
    }
  free(contents);
}


/**
 * Function called on (Document)SummaryInformation OLE
 * streams.
 *
 * @param in the input OLE stream
 * @param proc function to call on meta data found
 * @param proc_cls closure for proc
 * @return 0 to continue to extract, 1 if we are done
 */
static int
process (GsfInput *in,
	 EXTRACTOR_MetaDataProcessor proc,
	 void *proc_cls)
{
  struct ProcContext pc;
  GsfDocMetaData *sections;
  GError *error;

  pc.proc = proc;
  pc.proc_cls = proc_cls;
  pc.ret = 0;
  sections = gsf_doc_meta_data_new ();
#ifdef HAVE_GSF_DOC_META_DATA_READ_FROM_MSOLE
  error = gsf_doc_meta_data_read_from_msole (sections, in);
#else
  error = gsf_msole_metadata_read (in, sections);
#endif
  if (NULL == error)
    {
      gsf_doc_meta_data_foreach (sections,
				 &process_metadata,
				 &pc);
    }
  else
    {
      g_error_free (error);
    }
  g_object_unref (G_OBJECT (sections));
  return pc.ret;
}


/**
 * Function called on SfxDocumentInfo OLE
 * streams.
 *
 * @param in the input OLE stream
 * @param proc function to call on meta data found
 * @param proc_cls closure for proc
 * @return 0 to continue to extract, 1 if we are done
 */
static int
process_star_office (GsfInput *src,
		     EXTRACTOR_MetaDataProcessor proc,
		     void *proc_cls)
{
  off_t size = gsf_input_size (src);

  if ( (size < 0x374) ||
       (size > 4*1024*1024) )  /* == 0x375?? */
    return 0;
  {
    char buf[size];

    gsf_input_read (src, size, (unsigned char*) buf);
    if ( (buf[0] != 0x0F) ||
	 (buf[1] != 0x0) ||
	 (0 != strncmp (&buf[2],
			"SfxDocumentInfo",
			strlen ("SfxDocumentInfo"))) ||
	 (buf[0x11] != 0x0B) ||
	 (buf[0x13] != 0x00) || /* pw protected! */
	 (buf[0x12] != 0x00) )
      return 0;
    buf[0xd3] = '\0';
    if ( (buf[0x94] + buf[0x93] > 0) &&
	 (0 != add_metadata (proc, proc_cls,
			     &buf[0x95],
			     EXTRACTOR_METATYPE_TITLE)) )
      return 1;
    buf[0x114] = '\0';
    if ( (buf[0xd5] + buf[0xd4] > 0) &&
	 (0 != add_metadata (proc, proc_cls,
			     &buf[0xd6],
			     EXTRACTOR_METATYPE_SUBJECT)) )
      return 1;
    buf[0x215] = '\0';
    if ( (buf[0x115] + buf[0x116] > 0) &&
	 (0 != add_metadata (proc, proc_cls,
			     &buf[0x117],
			     EXTRACTOR_METATYPE_COMMENT)) )
      return 1;
    buf[0x296] = '\0';
    if ( (buf[0x216] + buf[0x217] > 0) &&
	 (0 != add_metadata(proc, proc_cls,
			    &buf[0x218],
			    EXTRACTOR_METATYPE_KEYWORDS)) )
      return 1;
    /* fixme: do timestamps,
       mime-type, user-defined info's */
  }
  return 0;
}


/**
 * We use "__" to translate using iso-639.
 *
 * @param a string to translate
 * @return translated string
 */
#define __(a) dgettext("iso-639", a)


/**
 * Get the language string for the given language ID (lid)
 * value.
 *
 * @param lid language id value
 * @return language string corresponding to the lid
 */
static const char *
lid_to_language (unsigned int lid)
{
  switch (lid)
    {
    case 0x0400:
      return _("No Proofing");
    case 0x0401:
      return __("Arabic");
    case 0x0402:
      return __("Bulgarian");
    case 0x0403:
      return __("Catalan");
    case 0x0404:
      return _("Traditional Chinese");
    case 0x0804:
      return _("Simplified Chinese");
    case 0x0405:
      return __("Chechen");
    case 0x0406:
      return __("Danish");
    case 0x0407:
      return __("German");
    case 0x0807:
      return _("Swiss German");
    case 0x0408:
      return __("Greek");
    case 0x0409:
      return _("U.S. English");
    case 0x0809:
      return _("U.K. English");
    case 0x0c09:
      return _("Australian English");
    case 0x040a:
      return _("Castilian Spanish");
    case 0x080a:
      return _("Mexican Spanish");
    case 0x040b:
      return __("Finnish");
    case 0x040c:
      return __("French");
    case 0x080c:
      return _("Belgian French");
    case 0x0c0c:
      return _("Canadian French");
    case 0x100c:
      return _("Swiss French");
    case 0x040d:
      return __("Hebrew");
    case 0x040e:
      return __("Hungarian");
    case 0x040f:
      return __("Icelandic");
    case 0x0410:
      return __("Italian");
    case 0x0810:
      return _("Swiss Italian");
    case 0x0411:
      return __("Japanese");
    case 0x0412:
      return __("Korean");
    case 0x0413:
      return __("Dutch");
    case 0x0813:
      return _("Belgian Dutch");
    case 0x0414:
      return _("Norwegian Bokmal");
    case 0x0814:
      return __("Norwegian Nynorsk");
    case 0x0415:
      return __("Polish");
    case 0x0416:
      return __("Brazilian Portuguese");
    case 0x0816:
      return __("Portuguese");
    case 0x0417:
      return _("Rhaeto-Romanic");
    case 0x0418:
      return __("Romanian");
    case 0x0419:
      return __("Russian");
    case 0x041a:
      return _("Croato-Serbian (Latin)");
    case 0x081a:
      return _("Serbo-Croatian (Cyrillic)");
    case 0x041b:
      return __("Slovak");
    case 0x041c:
      return __("Albanian");
    case 0x041d:
      return __("Swedish");
    case 0x041e:
      return __("Thai");
    case 0x041f:
      return __("Turkish");
    case 0x0420:
      return __("Urdu");
    case 0x0421:
      return __("Bahasa");
    case 0x0422:
      return __("Ukrainian");
    case 0x0423:
      return __("Byelorussian");
    case 0x0424:
      return __("Slovenian");
    case 0x0425:
      return __("Estonian");
    case 0x0426:
      return __("Latvian");
    case 0x0427:
      return __("Lithuanian");
    case 0x0429:
      return _("Farsi");
    case 0x042D:
      return __("Basque");
    case 0x042F:
      return __("Macedonian");
    case 0x0436:
      return __("Afrikaans");
    case 0x043E:
      return __("Malayalam");
    default:
      return NULL;
    }
}


/**
 * Extract editing history from XTable stream.
 *
 * @param stream OLE stream to process
 * @param lcSttbSavedBy length of the revision history in bytes
 * @param fcSttbSavedBy offset of the revision history in the stream
 * @param proc function to call on meta data found
 * @param proc_cls closure for proc
 * @return 0 to continue to extract, 1 if we are done
 */
static int
history_extract (GsfInput *stream,
		 unsigned int lcbSttbSavedBy,
		 unsigned int fcSttbSavedBy,
		 EXTRACTOR_MetaDataProcessor proc,
		 void *proc_cls)
{
  unsigned int where;
  unsigned char *lbuffer;
  unsigned int i;
  unsigned int length;
  char *author;
  char *filename;
  char *rbuf;
  unsigned int nRev;
  int ret;

  /* goto offset of revision information */
  gsf_input_seek (stream, fcSttbSavedBy, G_SEEK_SET);
  if (gsf_input_remaining (stream) < lcbSttbSavedBy)
    return 0;
  if (NULL == (lbuffer = malloc (lcbSttbSavedBy)))
    return 0;
  /* read all the revision history */
  gsf_input_read (stream, lcbSttbSavedBy, lbuffer);
  /* there are n strings, so n/2 revisions (author & file) */
  nRev = (lbuffer[2] + (lbuffer[3] << 8)) / 2;
  where = 6;
  ret = 0;
  for (i=0; i < nRev; i++)
    {
      if (where >= lcbSttbSavedBy)
	break;
      length = lbuffer[where++];
      if ( (where + 2 * length + 2 >= lcbSttbSavedBy) ||
	   (where + 2 * length + 2 <= where) )
	break;
      author = EXTRACTOR_common_convert_to_utf8 ((const char*) &lbuffer[where],
						 length * 2,
						 "UTF-16BE");
      where += length * 2 + 1;
      length = lbuffer[where++];
      if ( (where + 2 * length >= lcbSttbSavedBy) ||
	   (where + 2 * length + 1 <= where) )
	{
	  if (NULL != author)
	    free(author);
	  break;
	}
      filename = EXTRACTOR_common_convert_to_utf8 ((const char*) &lbuffer[where],
						   length * 2,
						   "UTF-16BE");
      where += length * 2 + 1;
      if ( (NULL != author) &&
	   (NULL != filename) )
	{
          size_t bsize;

          bsize = strlen (author) + strlen (filename) + 512;
          if (NULL != (rbuf = malloc (bsize)))
	    {
              int snret;

              snret = snprintf (rbuf,
                                bsize,
                                _("Revision #%u: Author `%s' worked on `%s'"),
                                i,
                                author,
                                filename);
              if ( (-1 != snret) &&
                   (bsize > (size_t) snret) )
                {
                  ret = add_metadata (proc,
                                      proc_cls,
                                      rbuf,
                                      EXTRACTOR_METATYPE_REVISION_HISTORY);
                }
	      free (rbuf);
	    }
        }
      if (NULL != author)
	free (author);
      if (NULL != filename)
	free (filename);
      if (0 != ret)
	break;
    }
  free (lbuffer);
  return ret;
}


/* *************************** custom GSF input method ***************** */

#define LE_TYPE_INPUT                  (le_input_get_type ())
#define LE_INPUT(obj)                  (G_TYPE_CHECK_INSTANCE_CAST ((obj), LE_TYPE_INPUT, LeInput))
#define LE_INPUT_CLASS(klass)          (G_TYPE_CHECK_CLASS_CAST ((klass), LE_TYPE_INPUT, LeInputClass))
#define IS_LE_INPUT(obj)               (G_TYPE_CHECK_INSTANCE_TYPE ((obj), LE_TYPE_INPUT))
#define IS_LE_INPUT_CLASS(klass)       (G_TYPE_CHECK_CLASS_TYPE ((klass), LE_TYPE_INPUT))
#define LE_INPUT_GET_CLASS(obj)        (G_TYPE_INSTANCE_GET_CLASS ((obj), LE_TYPE_INPUT, LeInputClass))

/**
 * Internal state of an "LeInput" object.
 */
typedef struct _LeInputPrivate
{
  /**
   * Our extraction context.
   */
  struct EXTRACTOR_ExtractContext *ec;
} LeInputPrivate;


/**
 * Overall state of an "LeInput" object.
 */
typedef struct _LeInput
{
  /**
   * Inherited state from parent (GsfInput).
   */
  GsfInput input;

  /*< private > */
  /**
   * Private state of the LeInput.
   */
  LeInputPrivate *priv;
} LeInput;


/**
 * LeInput's class state.
 */
typedef struct _LeInputClass
{
  /**
   * GsfInput is our parent class.
   */
  GsfInputClass parent_class;

  /* Padding for future expansion */
  void (*_gtk_reserved1) (void);
  void (*_gtk_reserved2) (void);
  void (*_gtk_reserved3) (void);
  void (*_gtk_reserved4) (void);
} LeInputClass;


/**
 * Constructor for LeInput objects.
 *
 * @param ec extraction context to use
 * @return the LeInput, NULL on error
 */
GsfInput *
le_input_new (struct EXTRACTOR_ExtractContext *ec);


/**
 * Class initializer for the "LeInput" class.
 *
 * @param class class object to initialize
 */
static void
le_input_class_init (LeInputClass *class);


/**
 * Initialize internal state of fresh input object.
 *
 * @param input object to initialize
 */
static void
le_input_init (LeInput *input);


/**
 * Macro to create LeInput type definition and register the class.
 */
GSF_CLASS (LeInput, le_input, le_input_class_init, le_input_init, GSF_INPUT_TYPE)


/**
 * Duplicate input, leaving the new one at the same offset.
 *
 * @param input the input to duplicate
 * @param err location for error reporting, can be NULL
 * @return NULL on error (always)
 */
static GsfInput *
le_input_dup (GsfInput *input,
	      GError **err)
{
  if (NULL != err)
    *err = g_error_new (gsf_input_error_id (), 0,
			"dup not supported on LeInput");
  return NULL;
}


/**
 * Read at least num_bytes. Does not change the current position if
 * there is an error. Will only read if the entire amount can be
 * read. Invalidates the buffer associated with previous calls to
 * gsf_input_read.
 *
 * @param input
 * @param num_bytes
 * @param optional_buffer
 * @return buffer where num_bytes data are available, or NULL on error
 */
static const guint8 *
le_input_read (GsfInput *input,
	       size_t num_bytes,
	       guint8 *optional_buffer)
{
  LeInput *li = LE_INPUT (input);
  struct EXTRACTOR_ExtractContext *ec;
  void *buf;
  uint64_t old_off;
  ssize_t ret;

  ec = li->priv->ec;
  old_off = ec->seek (ec->cls, 0, SEEK_CUR);
  if (num_bytes
      != (ret = ec->read (ec->cls,
			  &buf,
			  num_bytes)))
    {
      /* we don't support partial reads;
	 most other GsfInput implementations in this case
	 allocate some huge temporary buffer just to avoid
	 the partial read; we might need to do that as well!? */
      ec->seek (ec->cls, SEEK_SET, old_off);
      return NULL;
    }
  if (NULL != optional_buffer)
    {
      memcpy (optional_buffer, buf, num_bytes);
      return optional_buffer;
    }
  return buf;
}


/**
 * Move the current location in an input stream
 *
 * @param input stream to seek
 * @param offset target offset
 * @param whence determines to what the offset is relative to
 * @return TRUE on error
 */
static gboolean
le_input_seek (GsfInput *input,
	       gsf_off_t offset,
	       GSeekType whence)
{
  LeInput *li = LE_INPUT (input);
  struct EXTRACTOR_ExtractContext *ec;
  int w;
  int64_t ret;

  ec = li->priv->ec;
  switch (whence)
    {
    case G_SEEK_SET:
      w = SEEK_SET;
      break;
    case G_SEEK_CUR:
      w = SEEK_CUR;
      break;
    case G_SEEK_END:
      w = SEEK_END;
      break;
    default:
      return TRUE;
    }
  if (-1 ==
      (ret = ec->seek (ec->cls,
		       offset,
		       w)))
    return TRUE;
  return FALSE;
}


/**
 * Class initializer for the "LeInput" class.
 *
 * @param class class object to initialize
 */
static void
le_input_class_init (LeInputClass *class)
{
  GsfInputClass *input_class;

  input_class = (GsfInputClass *) class;
  input_class->Dup = le_input_dup;
  input_class->Read = le_input_read;
  input_class->Seek = le_input_seek;
  g_type_class_add_private (class, sizeof (LeInputPrivate));
}


/**
 * Initialize internal state of fresh input object.
 *
 * @param input object to initialize
 */
static void
le_input_init (LeInput *input)
{
  LeInputPrivate *priv;

  input->priv =
    G_TYPE_INSTANCE_GET_PRIVATE (input, LE_TYPE_INPUT,
				 LeInputPrivate);
  priv = input->priv;
  priv->ec = NULL;
}


/**
 * Creates a new LeInput object.
 *
 * @param ec extractor context to wrap
 * @return NULL on error
 */
GsfInput *
le_input_new (struct EXTRACTOR_ExtractContext *ec)
{
  LeInput *input;

  input = g_object_new (LE_TYPE_INPUT, NULL);
  gsf_input_set_size (GSF_INPUT (input),
		      ec->get_size (ec->cls));
  gsf_input_seek_emulate (GSF_INPUT (input),
			  0);
  input->input.name = NULL;
  input->input.container = NULL;
  input->priv->ec = ec;

  return GSF_INPUT (input);
}




/* *********************** end of custom GSF input method ************* */


/**
 * Main entry method for the OLE2 extraction plugin.
 *
 * @param ec extraction context provided to the plugin
 */
void
EXTRACTOR_ole2_extract_method (struct EXTRACTOR_ExtractContext *ec)
{
  GsfInput *input;
  GsfInfile *infile;
  GsfInput *src;
  const char *name;
  unsigned int i;
  unsigned int lcb;
  unsigned int fcb;
  const unsigned char *data512;
  unsigned int lid;
  const char *lang;
  int ret;
  void *data;
  uint64_t fsize;
  ssize_t data_size;

  fsize = ec->get_size (ec->cls);
  if (fsize < 512 + 898)
    {
      /* File too small for OLE2 */
      return; /* can hardly be OLE2 */
    }
  if (512 + 898 > (data_size = ec->read (ec->cls, &data, fsize)))
    {
      /* Failed to read minimum file size to buffer */
      return;
    }
  data512 = (const unsigned char*) data + 512;
  lid = data512[6] + (data512[7] << 8);
  if ( (NULL != (lang = lid_to_language (lid))) &&
       (0 != (ret = add_metadata (ec->proc, ec->cls,
				  lang,
				  EXTRACTOR_METATYPE_LANGUAGE))) )
    return;
  lcb = data512[726] + (data512[727] << 8) + (data512[728] << 16) + (data512[729] << 24);
  fcb = data512[722] + (data512[723] << 8) + (data512[724] << 16) + (data512[725] << 24);
  if (0 != ec->seek (ec->cls, 0, SEEK_SET))
    {
      /* seek failed!? */
      return;
    }
#if USE_LE_INPUT
  if (NULL == (input = le_input_new (ec)))
    {
      fprintf (stderr, "le_input_new failed\n");
      return;
    }
#else
  input = gsf_input_memory_new ((const guint8 *) data,
				data_size,
				FALSE);
#endif
  if (NULL == (infile = gsf_infile_msole_new (input, NULL)))
    {
      g_object_unref (G_OBJECT (input));
      return;
    }
  ret = 0;
  for (i=0;i<gsf_infile_num_children (infile);i++)
    {
      if (0 != ret)
	break;
      if (NULL == (name = gsf_infile_name_by_index (infile, i)))
	continue;
      src = NULL;
      if ( ( (0 == strcmp (name, "\005SummaryInformation")) ||
	     (0 == strcmp (name, "\005DocumentSummaryInformation")) ) &&
	   (NULL != (src = gsf_infile_child_by_index (infile, i))) )
	ret = process (src,
		       ec->proc,
		       ec->cls);
      if ( (0 == strcmp (name, "SfxDocumentInfo")) &&
	   (NULL != (src = gsf_infile_child_by_index (infile, i))) )
	ret = process_star_office (src,
				   ec->proc,
				   ec->cls);
      if (NULL != src)
	g_object_unref (G_OBJECT (src));
    }
  if (0 != ret)
    goto CLEANUP;

  if (lcb < 6)
    goto CLEANUP;
  for (i=0;i<gsf_infile_num_children (infile);i++)
    {
      if (ret != 0)
	break;
      if (NULL == (name = gsf_infile_name_by_index (infile, i)))
	continue;
      if ( ( (0 == strcmp (name, "1Table")) ||
	     (0 == strcmp (name, "0Table")) ) &&
	   (NULL != (src = gsf_infile_child_by_index (infile, i))) )
	{
	  ret = history_extract (src,
				 lcb,
				 fcb,
				 ec->proc, ec->cls);
	  g_object_unref (G_OBJECT (src));
	}
    }
 CLEANUP:
  g_object_unref (G_OBJECT (infile));
  g_object_unref (G_OBJECT (input));
}


/**
 * Custom log function we give to GSF to disable logging.
 *
 * @param log_domain unused
 * @param log_level unused
 * @param message unused
 * @param user_data unused
 */
static void
nolog (const gchar *log_domain,
       GLogLevelFlags log_level,
       const gchar *message,
       gpointer user_data)
{
  /* do nothing */
}


/**
 * OLE2 plugin constructor. Initializes glib and gsf, in particular
 * gsf logging is disabled.
 */
void __attribute__ ((constructor))
ole2_ltdl_init()
{
#if !GLIB_CHECK_VERSION(2, 35, 0)
  g_type_init ();
#endif
#ifdef HAVE_GSF_INIT
  gsf_init();
#endif
  /* disable logging -- thanks, Jody! */
  g_log_set_handler ("libgsf:msole",
		     G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING,
		     &nolog, NULL);
}


/**
 * OLE2 plugin destructor.  Shutdown of gsf.
 */
void __attribute__ ((destructor))
ole2_ltdl_fini()
{
#ifdef HAVE_GSF_INIT
  gsf_shutdown();
#endif
}


/* end of ole2_extractor.c */