1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  *
3  * Copyright 2004 Komarov Valery
4  * Copyright 2006 Christophe Leitienne
5  * Copyright 2008-2017 David Hoerl
6  * Copyright 2013 Bob Colbert
7  * Copyright 2013-2018 Evan Miller
8  *
9  * This file is part of libxls -- A multiplatform, C/C++ library for parsing
10  * Excel(TM) files.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  *
15  *    1. Redistributions of source code must retain the above copyright notice,
16  *    this list of conditions and the following disclaimer.
17  *
18  *    2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS
23  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
26  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  */
35 
36 #ifndef OLE_INCLUDE
37 #define OLE_INCLUDE
38 
39 #include <stdio.h>			// FILE *
40 
41 #include "libxls/xlstypes.h"
42 
43 #if defined(_AIX) || defined(__sun)
44 #pragma pack(1)
45 #else
46 #pragma pack(push, 1)
47 #endif
48 
49 typedef struct TIME_T
50 {
51     DWORD	LowDate;
52     DWORD	HighDate;
53 }
54 TIME_T;
55 
56 typedef struct OLE2Header
57 {
58     DWORD		id[2];		//D0CF11E0 A1B11AE1
59     DWORD		clid[4];
60     WORD		verminor;	//0x3e
61     WORD		verdll;		//0x03
62     WORD		byteorder;
63     WORD		lsectorB;
64     WORD		lssectorB;
65 
66     WORD		reserved1;
67     DWORD		reserved2;
68     DWORD		reserved3;
69 
70     DWORD		cfat;			// count full sectors
71     DWORD		dirstart;
72 
73     DWORD		reserved4;
74 
75     DWORD		sectorcutoff;	// min size of a standard stream ; if less than this then it uses short-streams
76     DWORD		sfatstart;		// first short-sector or EOC
77     DWORD		csfat;			// count short sectors
78     DWORD		difstart;		// first sector master sector table or EOC
79     DWORD		cdif;			// total count
80     DWORD		MSAT[109];		// First 109 MSAT
81 }
82 OLE2Header;
83 
84 #pragma pack(pop)
85 
86 //-----------------------------------------------------------------------------------
87 typedef	struct st_olefiles
88 {
89     long count;
90     struct st_olefiles_data
91     {
92         char*	name;
93         DWORD	start;
94         DWORD	size;
95    }
96     * file;
97 }
98 st_olefiles;
99 
100 typedef struct OLE2
101 {
102     FILE*		file;
103     const void *buffer;
104     size_t      buffer_len;
105     size_t      buffer_pos;
106 
107     WORD		lsector;
108     WORD		lssector;
109     DWORD		cfat;
110     DWORD		dirstart;
111 
112     DWORD		sectorcutoff;
113     DWORD		sfatstart;
114     DWORD		csfat;
115     DWORD		difstart;
116     DWORD		cdif;
117 
118     DWORD*		SecID;	// regular sector data
119     DWORD       SecIDCount;
120 
121 	DWORD*		SSecID;	// short sector data
122     DWORD       SSecIDCount;
123 
124 	BYTE*		SSAT;	// directory of short sectors
125     DWORD       SSATCount;
126 
127     st_olefiles	files;
128 }
129 OLE2;
130 
131 typedef struct OLE2Stream
132 {
133     OLE2*	ole;
134     DWORD	start;
135     size_t	pos;
136     size_t	cfat;
137     size_t	size;
138     size_t	fatpos;
139     BYTE*	buf;
140     DWORD	bufsize;
141     BYTE	eof;
142 	BYTE	sfat;	// short
143 }
144 OLE2Stream;
145 
146 #if defined(_AIX) || defined(__sun)
147 #pragma pack(1)
148 #else
149 #pragma pack(push, 1)
150 #endif
151 
152 typedef struct PSS
153 {
154     char	name[64];
155     WORD	bsize;
156     BYTE	type;		//STGTY
157 #define PS_EMPTY		00
158 #define PS_USER_STORAGE	01
159 #define PS_USER_STREAM	02
160 #define PS_USER_ROOT	05
161     BYTE	flag;		//COLOR
162 #define BLACK	1
163     DWORD	left;
164     DWORD	right;
165     DWORD	child;
166     WORD	guid[8];
167     DWORD	userflags;
168     TIME_T	time[2];
169     DWORD	sstart;
170     DWORD	size;
171     DWORD	proptype;
172 }
173 PSS;
174 
175 #pragma pack(pop)
176 
177 ssize_t ole2_read(void* buf,size_t size,size_t count,OLE2Stream* olest);
178 OLE2Stream* ole2_sopen(OLE2* ole,DWORD start, size_t size);
179 int ole2_seek(OLE2Stream* olest,DWORD ofs);
180 OLE2Stream*  ole2_fopen(OLE2* ole, const char *file);
181 void ole2_fclose(OLE2Stream* ole2st);
182 OLE2* ole2_open_file(const char *file);
183 OLE2* ole2_open_buffer(const void *buffer, size_t len);
184 void ole2_close(OLE2* ole2);
185 
186 #endif
187