3 * @author Alex Ott, Victor B Wagner
4 * @date Wed Jun 11 12:33:01 2003
5 * Version: $Id: ole.c,v 1.2 2006-02-25 15:28:14 vitus Exp $
6 * Copyright: Victor B Wagner, 1996-2003 Alex Ott, 2003
8 * @brief Parsing structure of MS Office compound document
10 * This file is part of catdoc project
11 * and distributed under GNU Public License
25 #define min(a,b) ((a) < (b) ? (a) : (b))
27 long int sectorSize, shortSectorSize;
29 long int bbdNumBlocks;
30 unsigned char *BBD=NULL;
32 long int sbdNumber, sbdStart, sbdLen;
33 unsigned char *SBD=NULL;
34 oleEntry *rootEntry=NULL;
36 long propCurNumber, propLen, propNumber, propStart;
37 unsigned char *properties=NULL;
38 long int fileLength=0;
40 static unsigned char ole_sign[]={0xD0,0xCF,0x11,0xE0,0xA1,0xB1,0x1A,0xE1,0};
44 * Initializes ole structure
46 * @param f (FILE *) compound document file, positioned at bufSize
47 * byte. Might be pipe or socket
48 * @param buffer (void *) bytes already read from f
49 * @param bufSize number of bytes already read from f should be less
54 FILE* ole_init(FILE *f, void *buffer, size_t bufSize) {
55 unsigned char oleBuf[BBD_BLOCK_SIZE];
56 unsigned char *tmpBuf;
59 long int sbdMaxLen, sbdCurrent, propMaxLen, propCurrent, mblock, msat_size;
62 /* deleting old data (if it was allocated) */
65 if (fseek(f,0,SEEK_SET) == -1) {
66 if ( errno == ESPIPE ) {
67 /* We got non-seekable file, create temp file */
68 if((newfile=tmpfile()) == NULL) {
69 perror("Can't create tmp file");
73 ret=fwrite(buffer, 1, bufSize, newfile);
75 perror("Can't write to tmp file");
81 ret=fread(oleBuf,1,BBD_BLOCK_SIZE,f);
82 fwrite(oleBuf, 1, ret, newfile);
84 fseek(newfile,0,SEEK_SET);
86 perror("Can't seek in file");
92 fseek(newfile,0,SEEK_END);
93 fileLength=ftell(newfile);
94 /* fprintf(stderr, "fileLength=%ld\n", fileLength); */
95 fseek(newfile,0,SEEK_SET);
96 ret=fread(oleBuf,1,BBD_BLOCK_SIZE,newfile);
97 if ( ret != BBD_BLOCK_SIZE ) {
100 if (strncmp(oleBuf,ole_sign,8) != 0) {
103 sectorSize = 1<<getshort(oleBuf,0x1e);
104 shortSectorSize=1<<getshort(oleBuf,0x20);
106 /* Read BBD into memory */
107 bbdNumBlocks = getulong(oleBuf,0x2c);
108 if((BBD=malloc(bbdNumBlocks*sectorSize)) == NULL ) {
112 if((tmpBuf=malloc(MSAT_ORIG_SIZE)) == NULL ) {
115 memcpy(tmpBuf,oleBuf+0x4c,MSAT_ORIG_SIZE);
116 mblock=getlong(oleBuf,0x44);
117 msat_size=getlong(oleBuf,0x48);
119 /* fprintf(stderr, "msat_size=%ld\n", msat_size); */
122 while((mblock >= 0) && (i < msat_size)) {
123 unsigned char *newbuf;
124 /* fprintf(stderr, "i=%d mblock=%ld\n", i, mblock); */
125 if ((newbuf=realloc(tmpBuf, sectorSize*(i+1)+MSAT_ORIG_SIZE)) != NULL) {
128 perror("MSAT realloc error");
134 fseek(newfile, 512+mblock*sectorSize, SEEK_SET);
135 if(fread(tmpBuf+MSAT_ORIG_SIZE+(sectorSize-4)*i,
136 1, sectorSize, newfile) != sectorSize) {
137 fprintf(stderr, "Error read MSAT!\n");
143 mblock=getlong(tmpBuf, MSAT_ORIG_SIZE+(sectorSize-4)*i);
146 /* fprintf(stderr, "bbdNumBlocks=%ld\n", bbdNumBlocks); */
147 for(i=0; i< bbdNumBlocks; i++) {
148 long int bbdSector=getlong(tmpBuf,4*i);
150 if (bbdSector >= fileLength/sectorSize || bbdSector < 0) {
151 fprintf(stderr, "Bad BBD entry!\n");
155 fseek(newfile, 512+bbdSector*sectorSize, SEEK_SET);
156 if ( fread(BBD+i*sectorSize, 1, sectorSize, newfile) != sectorSize ) {
157 fprintf(stderr, "Can't read BBD!\n");
165 /* Read SBD into memory */
168 sbdCurrent = sbdStart = getlong(oleBuf,0x3c);
170 if((SBD=malloc(sectorSize*sbdMaxLen)) == NULL ) {
175 fseek(newfile, 512+sbdCurrent*sectorSize, SEEK_SET);
176 fread(SBD+sbdLen*sectorSize, 1, sectorSize, newfile);
178 if (sbdLen >= sbdMaxLen) {
179 unsigned char *newSBD;
182 if ((newSBD=realloc(SBD, sectorSize*sbdMaxLen)) != NULL) {
185 perror("SBD realloc error");
190 sbdCurrent = getlong(BBD, sbdCurrent*4);
192 sbdCurrent >= fileLength/sectorSize)
195 sbdNumber = (sbdLen*sectorSize)/shortSectorSize;
196 /* fprintf(stderr, "sbdLen=%ld sbdNumber=%ld\n",sbdLen, sbdNumber); */
200 /* Read property catalog into memory */
203 propCurrent = propStart = getlong(oleBuf,0x30);
204 if (propStart >= 0) {
205 if((properties=malloc(propMaxLen*sectorSize)) == NULL ) {
210 /* fprintf(stderr, "propCurrent=%ld\n",propCurrent); */
211 fseek(newfile, 512+propCurrent*sectorSize, SEEK_SET);
212 fread(properties+propLen*sectorSize,
213 1, sectorSize, newfile);
215 if (propLen >= propMaxLen) {
216 unsigned char *newProp;
219 if ((newProp=realloc(properties, propMaxLen*sectorSize)) != NULL)
222 perror("Properties realloc error");
228 propCurrent = getlong(BBD, propCurrent*4);
229 if(propCurrent < 0 ||
230 propCurrent >= fileLength/sectorSize ) {
234 /* fprintf(stderr, "propLen=%ld\n",propLen); */
235 propNumber = (propLen*sectorSize)/PROP_BLOCK_SIZE;
244 /* Find Root Entry */
245 while((tEntry=(oleEntry*)ole_readdir(newfile)) != NULL) {
246 if (tEntry->type == oleRootDir ) {
250 ole_close((FILE*)tEntry);
253 fseek(newfile, 0, SEEK_SET);
255 fprintf(stderr,"Broken OLE structure. Cannot find root entry in this file!\n"); ole_finish();
268 int rightOleType(unsigned char *oleBuf) {
269 return (oleBuf[0x42] == 1 || oleBuf[0x42] == 2 ||
270 oleBuf[0x42] == 3 || oleBuf[0x42] == 5 );
280 oleType getOleType(unsigned char *oleBuf) {
281 return (oleType)((unsigned char)oleBuf[0x42]);
285 * Reads next directory entry from file
287 * @param name buffer for name converted to us-ascii should be at least 33 chars long
288 * @param size size of file
290 * @return 0 if everything is ok -1 on error
292 FILE *ole_readdir(FILE *f) {
294 unsigned char *oleBuf;
296 long int chainMaxLen, chainCurrent;
298 if ( properties == NULL || propCurNumber >= propNumber || f == NULL )
300 oleBuf=properties + propCurNumber*PROP_BLOCK_SIZE;
301 if( !rightOleType(oleBuf))
303 if ((e = (oleEntry*)malloc(sizeof(oleEntry))) == NULL) {
304 perror("Can\'t allocate memory");
308 e->type=getOleType(oleBuf);
310 e->startBlock=getlong(oleBuf,0x74);
313 nLen=getshort(oleBuf,0x40);
314 for (i=0 ; i < nLen /2; i++)
315 e->name[i]=(char)oleBuf[i*2];
318 e->length=getulong(oleBuf,0x78);
319 /* Read sector chain for object */
322 chainCurrent = e->startBlock;
323 e->isBigBlock = (e->length >= 0x1000) || !strcmp(e->name, "Root Entry");
324 /* fprintf(stderr, "e->name=%s e->length=%ld\n", e->name, e->length); */
325 /* fprintf(stderr, "e->startBlock=%ld BBD=%p\n", e->startBlock, BBD); */
326 if (e->startBlock >= 0 &&
329 fileLength/(e->isBigBlock ? sectorSize : shortSectorSize))) {
330 if((e->blocks=malloc(chainMaxLen*sizeof(long int))) == NULL ) {
334 /* fprintf(stderr, "chainCurrent=%ld\n", chainCurrent); */
335 e->blocks[e->numOfBlocks++] = chainCurrent;
336 if (e->numOfBlocks >= chainMaxLen) {
339 if ((newChain=realloc(e->blocks,
340 chainMaxLen*sizeof(long int))) != NULL)
343 perror("Properties realloc error");
349 if ( e->isBigBlock ) {
350 chainCurrent = getlong(BBD, chainCurrent*4);
351 } else if ( SBD != NULL ) {
352 chainCurrent = getlong(SBD, chainCurrent*4);
356 if(chainCurrent <= 0 ||
357 chainCurrent >= ( e->isBigBlock ?
358 ((bbdNumBlocks*sectorSize)/4)
359 : ((sbdNumber*shortSectorSize)/4) ) ||
361 e->length/(e->isBigBlock ? sectorSize : shortSectorSize))) {
362 /* fprintf(stderr, "chain End=%ld\n", chainCurrent); */
368 if(e->length > (e->isBigBlock ? sectorSize : shortSectorSize)*e->numOfBlocks)
369 e->length = (e->isBigBlock ? sectorSize : shortSectorSize)*e->numOfBlocks;
370 /* fprintf(stderr, "READDIR: e->name=%s e->numOfBlocks=%ld length=%ld\n", */
371 /* e->name, e->numOfBlocks, e->length); */
377 * Open stream, which correspond to directory entry last read by
381 * @return opaque pointer to pass to ole_read, casted to (FILE *)
383 int ole_open(FILE *stream) {
384 oleEntry *e=(oleEntry *)stream;
385 if ( e->type != oleStream)
389 e->file_offset= ftell(e->file);
401 long int calcFileBlockOffset(oleEntry *e, long int blk) {
403 if ( e->isBigBlock ) {
404 res=512+e->blocks[blk]*sectorSize;
406 long int sbdPerSector=sectorSize/shortSectorSize;
407 long int sbdSecNum=e->blocks[blk]/sbdPerSector;
408 long int sbdSecMod=e->blocks[blk]%sbdPerSector;
409 /* fprintf(stderr, "calcoffset: e->name=%s e->numOfBlocks=%ld length=%ld sbdSecNum=%ld rootEntry->blocks=%p\n",
410 e->name, e->numOfBlocks, e->length, sbdSecNum, rootEntry->blocks);*/
411 res=512 + rootEntry->blocks[sbdSecNum]*sectorSize + sbdSecMod*shortSectorSize;
418 * Reads block from open ole stream interface-compatible with fread
420 * @param ptr pointer to buffer for read to
421 * @param size size of block
422 * @param nmemb size in blocks
423 * @param stream pointer to FILE* structure
425 * @return number of readed blocks
427 size_t ole_read(void *ptr, size_t size, size_t nmemb, FILE *stream) {
428 oleEntry *e = (oleEntry*)stream;
429 long int llen = size*nmemb, rread=0, i;
430 long int blockNumber, modBlock, toReadBlocks, toReadBytes, bytesInBlock;
431 long int ssize; /**< Size of block */
433 unsigned char *cptr = ptr;
434 if( e->ole_offset+llen > e->length )
435 llen= e->length - e->ole_offset;
437 ssize = (e->isBigBlock ? sectorSize : shortSectorSize);
438 blockNumber=e->ole_offset/ssize;
439 /* fprintf(stderr, "blockNumber=%ld e->numOfBlocks=%ld llen=%ld\n", */
440 /* blockNumber, e->numOfBlocks, llen); */
441 if ( blockNumber >= e->numOfBlocks || llen <=0 )
444 modBlock=e->ole_offset%ssize;
445 bytesInBlock = ssize - modBlock;
446 if(bytesInBlock < llen) {
447 toReadBlocks = (llen-bytesInBlock)/ssize;
448 toReadBytes = (llen-bytesInBlock)%ssize;
450 toReadBlocks = toReadBytes = 0;
452 /* fprintf(stderr, "llen=%ld toReadBlocks=%ld toReadBytes=%ld bytesInBlock=%ld blockNumber=%ld modBlock=%ld\n", */
453 /* llen, toReadBlocks, toReadBytes, bytesInBlock, blockNumber, modBlock); */
454 newoffset = calcFileBlockOffset(e,blockNumber)+modBlock;
455 if (e->file_offset != newoffset) {
456 fseek(e->file, e->file_offset=newoffset, SEEK_SET);
458 rread=fread(ptr, 1, min(llen,bytesInBlock), e->file);
459 e->file_offset += rread;
460 for(i=0; i<toReadBlocks; i++) {
463 newoffset = calcFileBlockOffset(e,blockNumber);
464 if (newoffset != e->file_offset);
465 fseek(e->file, e->file_offset=newoffset , SEEK_SET);
466 readbytes=fread(cptr+rread, 1, min(llen-rread, ssize), e->file);
468 e->file_offset +=readbytes;
470 if(toReadBytes > 0) {
473 newoffset = calcFileBlockOffset(e,blockNumber);
474 fseek(e->file, e->file_offset=newoffset, SEEK_SET);
475 readbytes=fread(cptr+rread, 1, toReadBytes,e ->file);
477 e->file_offset +=readbytes;
479 /* fprintf(stderr, "ole_offset=%ld rread=%ld llen=%ld\n",
480 e->ole_offset, rread, llen);*/
481 e->ole_offset+=rread;
492 int ole_eof(FILE *stream) {
493 oleEntry *e=(oleEntry*)stream;
494 /* fprintf(stderr, "EOF: e->ole_offset=%ld e->length=%ld\n",
495 e->ole_offset, e->length);*/
496 return (e->ole_offset >= e->length);
503 void ole_finish(void) {
504 if ( BBD != NULL ) free(BBD);
505 if ( SBD != NULL ) free(SBD);
506 if ( properties != NULL ) free(properties);
507 if ( rootEntry != NULL ) ole_close((FILE*)rootEntry);
508 properties = SBD = BBD = NULL;
519 int ole_close(FILE *stream) {
520 oleEntry *e=(oleEntry*)stream;
523 if (e->blocks != NULL)
532 * @param stream pointer to OLE stream structure
538 int ole_seek(FILE *stream, long offset, int whence) {
539 oleEntry *e=(oleEntry*)stream;
540 long int new_ole_offset=0, new_file_offset;
541 int ssize, modBlock, blockNumber;
545 new_ole_offset=offset;
549 new_ole_offset=e->ole_offset+offset;
553 new_ole_offset=e->length+offset;
562 if(new_ole_offset >= e->length)
563 new_ole_offset=e->length;
565 ssize = (e->isBigBlock ? sectorSize : shortSectorSize);
566 blockNumber=new_ole_offset/ssize;
567 if ( blockNumber >= e->numOfBlocks )
570 modBlock=new_ole_offset%ssize;
571 new_file_offset = calcFileBlockOffset(e,blockNumber)+modBlock;
572 fseek(e->file, e->file_offset=new_file_offset, SEEK_SET);
573 e->ole_offset=new_ole_offset;
579 * Tell position inside OLE stream
581 * @param stream pointer to OLE stream
583 * @return current position inside OLE stream
585 long ole_tell(FILE *stream) {
586 oleEntry *e=(oleEntry*)stream;
587 return e->ole_offset;
595 size_t (*catdoc_read)(void *ptr, size_t size, size_t nmemb, FILE *stream);
596 int (*catdoc_eof)(FILE *stream);
597 int (*catdoc_seek)(FILE *stream, long offset, int whence);
598 long (*catdoc_tell)(FILE *stream);
600 void set_ole_func(void) {
601 catdoc_read=ole_read;
603 catdoc_seek=ole_seek;
604 catdoc_tell=ole_tell;
608 /* feof is macro in Turbo C, so we need a real function to assign to
611 int my_feof(FILE *f) {
619 void set_std_func(void) {