The next example opens a file, and guess what version of Microsoft Excel generated the file.
#include <stdio.h> #include <string.h> #include <cole/cole.h> int main (int argc, char **argv) { pps_entry *stream_tree; U32 root_stream; U32 stream; FILE * f; unsigned char buff[6]; if (argc != 2) { fprintf (stderr, "cole example 2. cole is a free C OLE library.\n"); fprintf (stderr, "Usage: coleexample2 FILE.\n"); return 1; } if (OLEdecode (argv[1], &stream_tree, &root_stream, 0)) { printf ("File is not a Microsoft Excel one.\n"); return 1; } for (stream = stream_tree[root_stream].dir; stream != 0xffffffff; stream = stream_tree[stream].next) { if (stream_tree[stream].type != 1 && stream_tree[stream].level == 1) if (!strcmp(stream_tree[stream].name, "Workbook") || !strcmp(stream_tree[stream].name, "Book")) { /* Book stream found */ f = fopen (stream_tree[stream].filename, "rb"); if (f == NULL) { perror ("Error opening temporal file"); freeOLEtree (stream_tree); return 1; } if (fread (buff, 1, 6, f) != 6) { perror ("Error reading temporal file"); fclose (f); freeOLEtree (stream_tree); return 1; } /* the next is from the internal structure of a Excel stream */ if (buff[0] != 0x09 || buff[1] != 0x08) { printf ("File is not a Microsoft Excel one.\n"); fclose (f); freeOLEtree (stream_tree); return 1; } if (buff[4] != 0x00 || (buff[5] != 0x05 && buff[5] != 0x06)) { printf ("File is from an unknown Microsoft Excel version.\n"); fclose (f); freeOLEtree (stream_tree); return 1; } if (buff[5] == 0x05) printf ("File is from Microsoft Excel version 5 or 7.\n"); else printf ("File is from Microsoft Excel version 8.\n"); fclose (f); freeOLEtree (stream_tree); return 0; } } printf ("File is not a Microsoft Excel one.\n"); freeOLEtree (stream_tree); return 0; } |
When you run this example, the following output is produced:
$ coleexample2 examples/sprsheet.xls File is from Microsoft Excel version 5 or 7. $ coleexample2 examples/text.doc File is not a Microsoft Excel one. $ coleexample2 README File is not a Microsoft Excel one. |