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 char ole_sign[]={0xD0,0xCF,0x11,0xE0,0xA1,0xB1,0x1A,0xE1,0};
41 char zip_sign[]="PK\003\004";
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;
60 long int sbdMaxLen, sbdCurrent, propMaxLen, propCurrent, mblock, msat_size;
63 /* deleting old data (if it was allocated) */
66 if (fseek(f,0,SEEK_SET) == -1) {
67 if ( errno == ESPIPE ) {
68 /* We got non-seekable file, create temp file */
69 if((newfile=tmpfile()) == NULL) {
70 perror("Can't create tmp file");
74 ret=fwrite(buffer, 1, bufSize, newfile);
76 perror("Can't write to tmp file");
82 ret=fread(oleBuf,1,BBD_BLOCK_SIZE,f);
83 fwrite(oleBuf, 1, ret, newfile);
85 fseek(newfile,0,SEEK_SET);
87 perror("Can't seek in file");
93 fseek(newfile,0,SEEK_END);
94 fileLength=ftell(newfile);
95 /* fprintf(stderr, "fileLength=%ld\n", fileLength); */
96 fseek(newfile,0,SEEK_SET);
97 ret=fread(oleBuf,1,BBD_BLOCK_SIZE,newfile);
98 if ( ret != BBD_BLOCK_SIZE ) {
101 if (strncmp((char *)&oleBuf,zip_sign,4) == 0) {
102 fprintf(stderr,"Looks like ZIP archive or Office 2007 or later. Not supported\n");
104 } else if (strncmp((char *)&oleBuf,ole_sign,8) != 0) {
107 sectorSize = 1<<getshort(oleBuf,0x1e);
108 shortSectorSize=1<<getshort(oleBuf,0x20);
110 /* Read BBD into memory */
111 bbdNumBlocks = getulong(oleBuf,0x2c);
112 bbdSize = bbdNumBlocks * sectorSize;
113 if (bbdSize > fileLength) {
114 /* broken file, BBD size greater than entire file*/
118 if((BBD=malloc(bbdNumBlocks*sectorSize)) == NULL ) {
122 if((tmpBuf=malloc(MSAT_ORIG_SIZE)) == NULL ) {
125 memcpy(tmpBuf,oleBuf+0x4c,MSAT_ORIG_SIZE);
126 mblock=getlong(oleBuf,0x44);
127 msat_size=getlong(oleBuf,0x48);
128 if (msat_size * sectorSize)
129 /* fprintf(stderr, "msat_size=%ld\n", msat_size); */
132 while((mblock >= 0) && (i < msat_size)) {
133 unsigned char *newbuf;
134 /* fprintf(stderr, "i=%d mblock=%ld\n", i, mblock); */
135 if ((newbuf=realloc(tmpBuf, sectorSize*(i+1)+MSAT_ORIG_SIZE)) != NULL) {
138 perror("MSAT realloc error");
144 fseek(newfile, 512+mblock*sectorSize, SEEK_SET);
145 if(fread(tmpBuf+MSAT_ORIG_SIZE+(sectorSize-4)*i,
146 1, sectorSize, newfile) != sectorSize) {
147 fprintf(stderr, "Error read MSAT!\n");
153 mblock=getlong(tmpBuf, MSAT_ORIG_SIZE+(sectorSize-4)*i);
156 /* fprintf(stderr, "bbdNumBlocks=%ld\n", bbdNumBlocks); */
157 for(i=0; i< bbdNumBlocks; i++) {
158 long int bbdSector=getlong(tmpBuf,4*i);
160 if (bbdSector >= fileLength/sectorSize || bbdSector < 0) {
161 fprintf(stderr, "Bad BBD entry!\n");
165 fseek(newfile, 512+bbdSector*sectorSize, SEEK_SET);
166 if ( fread(BBD+i*sectorSize, 1, sectorSize, newfile) != sectorSize ) {
167 fprintf(stderr, "Can't read BBD!\n");
175 /* Read SBD into memory */
178 sbdCurrent = sbdStart = getlong(oleBuf,0x3c);
180 if((SBD=malloc(sectorSize*sbdMaxLen)) == NULL ) {
185 fseek(newfile, 512+sbdCurrent*sectorSize, SEEK_SET);
186 fread(SBD+sbdLen*sectorSize, 1, sectorSize, newfile);
188 if (sbdLen >= sbdMaxLen) {
189 unsigned char *newSBD;
192 if ((newSBD=realloc(SBD, sectorSize*sbdMaxLen)) != NULL) {
195 perror("SBD realloc error");
200 if (sbdCurrent * 4 > bbdSize) {
204 sbdCurrent = getlong(BBD, sbdCurrent*4);
206 sbdCurrent >= fileLength/sectorSize)
209 sbdNumber = (sbdLen*sectorSize)/shortSectorSize;
210 /* fprintf(stderr, "sbdLen=%ld sbdNumber=%ld\n",sbdLen, sbdNumber); */
214 /* Read property catalog into memory */
217 propCurrent = propStart = getlong(oleBuf,0x30);
218 if (propStart >= 0) {
219 if((properties=malloc(propMaxLen*sectorSize)) == NULL ) {
224 /* fprintf(stderr, "propCurrent=%ld\n",propCurrent); */
225 fseek(newfile, 512+propCurrent*sectorSize, SEEK_SET);
227 if (fread(properties+propLen*sectorSize,
228 1, sectorSize, newfile)!=sectorSize) {
230 perror("reading properties catalog");
236 if (propLen >= propMaxLen) {
237 unsigned char *newProp;
240 if ((newProp=realloc(properties, propMaxLen*sectorSize)) != NULL)
243 perror("Properties realloc error");
249 propCurrent = getlong(BBD, propCurrent*4);
250 if(propCurrent < 0 ||
251 propCurrent >= bbdSize/4 ) {
255 /* fprintf(stderr, "propLen=%ld\n",propLen); */
256 propNumber = (propLen*sectorSize)/PROP_BLOCK_SIZE;
265 /* Find Root Entry */
266 while((tEntry=(oleEntry*)ole_readdir(newfile)) != NULL) {
267 if (tEntry->type == oleRootDir ) {
271 ole_close((FILE*)tEntry);
274 fseek(newfile, 0, SEEK_SET);
276 fprintf(stderr,"Broken OLE structure. Cannot find root entry in this file!\n"); ole_finish();
289 int rightOleType(unsigned char *oleBuf) {
290 return (oleBuf[0x42] == 1 || oleBuf[0x42] == 2 ||
291 oleBuf[0x42] == 3 || oleBuf[0x42] == 5 );
301 oleType getOleType(unsigned char *oleBuf) {
302 return (oleType)((unsigned char)oleBuf[0x42]);
306 * Reads next directory entry from file
308 * @param name buffer for name converted to us-ascii should be at least 33 chars long
309 * @param size size of file
311 * @return 0 if everything is ok -1 on error
313 FILE *ole_readdir(FILE *f) {
315 unsigned char *oleBuf;
317 long int chainMaxLen, chainCurrent;
319 if ( properties == NULL || propCurNumber >= propNumber || f == NULL )
321 oleBuf=properties + propCurNumber*PROP_BLOCK_SIZE;
322 if( !rightOleType(oleBuf))
324 if ((e = (oleEntry*) calloc(sizeof(oleEntry),1)) == NULL) {
325 perror("Can\'t allocate memory");
329 e->type=getOleType(oleBuf);
331 e->startBlock=getlong(oleBuf,0x74);
334 nLen=getshort(oleBuf,0x40);
335 if (nLen > OLENAMELENGTH) {
339 for (i=0 ; i < nLen /2; i++)
340 e->name[i]=(char)oleBuf[i*2];
343 e->length=getulong(oleBuf,0x78);
344 /* Read sector chain for object */
347 chainCurrent = e->startBlock;
348 e->isBigBlock = (e->length >= 0x1000) || !strcmp(e->name, "Root Entry");
349 /* fprintf(stderr, "e->name=%s e->length=%ld\n", e->name, e->length); */
350 /* fprintf(stderr, "e->startBlock=%ld BBD=%p\n", e->startBlock, BBD); */
351 if (e->startBlock >= 0 &&
354 fileLength/(e->isBigBlock ? sectorSize : shortSectorSize))) {
355 if((e->blocks=malloc(chainMaxLen*sizeof(long int))) == NULL ) {
360 if(chainCurrent < 0 ||
363 ((bbdNumBlocks*sectorSize)/4) :
364 ((sbdNumber*shortSectorSize)/4)
374 /* fprintf(stderr, "chain End=%ld\n", chainCurrent); */
377 /* fprintf(stderr, "chainCurrent=%ld\n", chainCurrent); */
378 e->blocks[e->numOfBlocks++] = chainCurrent;
379 if (e->numOfBlocks >= chainMaxLen) {
382 if ((newChain=realloc(e->blocks,
383 chainMaxLen*sizeof(long int))) != NULL) {
386 perror("Properties realloc error");
392 if ( e->isBigBlock ) {
393 chainCurrent = getlong(BBD, chainCurrent*4);
394 } else if ( SBD != NULL ) {
395 chainCurrent = getlong(SBD, chainCurrent*4);
402 if(e->length > (e->isBigBlock ? sectorSize : shortSectorSize)*e->numOfBlocks)
403 e->length = (e->isBigBlock ? sectorSize : shortSectorSize)*e->numOfBlocks;
404 /* fprintf(stderr, "READDIR: e->name=%s e->numOfBlocks=%ld length=%ld\n", */
405 /* e->name, e->numOfBlocks, e->length); */
411 * Open stream, which correspond to directory entry last read by
415 * @return opaque pointer to pass to ole_read, casted to (FILE *)
417 int ole_open(FILE *stream) {
418 oleEntry *e=(oleEntry *)stream;
419 if ( e->type != oleStream)
423 e->file_offset= ftell(e->file);
435 long int calcFileBlockOffset(oleEntry *e, long int blk) {
437 if ( e->isBigBlock ) {
438 res=512+e->blocks[blk]*sectorSize;
440 long int sbdPerSector=sectorSize/shortSectorSize;
441 long int sbdSecNum=e->blocks[blk]/sbdPerSector;
442 long int sbdSecMod=e->blocks[blk]%sbdPerSector;
443 /* fprintf(stderr, "calcoffset: e->name=%s e->numOfBlocks=%ld length=%ld sbdSecNum=%ld rootEntry->blocks=%p\n",
444 e->name, e->numOfBlocks, e->length, sbdSecNum, rootEntry->blocks);*/
445 res=512 + rootEntry->blocks[sbdSecNum]*sectorSize + sbdSecMod*shortSectorSize;
452 * Reads block from open ole stream interface-compatible with fread
454 * @param ptr pointer to buffer for read to
455 * @param size size of block
456 * @param nmemb size in blocks
457 * @param stream pointer to FILE* structure
459 * @return number of readed blocks
461 size_t ole_read(void *ptr, size_t size, size_t nmemb, FILE *stream) {
462 oleEntry *e = (oleEntry*)stream;
463 long int llen = size*nmemb, rread=0, i;
464 long int blockNumber, modBlock, toReadBlocks, toReadBytes, bytesInBlock;
465 long int ssize; /**< Size of block */
467 unsigned char *cptr = ptr;
468 if( e->ole_offset+llen > e->length )
469 llen= e->length - e->ole_offset;
471 ssize = (e->isBigBlock ? sectorSize : shortSectorSize);
472 blockNumber=e->ole_offset/ssize;
473 /* fprintf(stderr, "blockNumber=%ld e->numOfBlocks=%ld llen=%ld\n", */
474 /* blockNumber, e->numOfBlocks, llen); */
475 if ( blockNumber >= e->numOfBlocks || llen <=0 )
478 modBlock=e->ole_offset%ssize;
479 bytesInBlock = ssize - modBlock;
480 if(bytesInBlock < llen) {
481 toReadBlocks = (llen-bytesInBlock)/ssize;
482 toReadBytes = (llen-bytesInBlock)%ssize;
484 toReadBlocks = toReadBytes = 0;
486 /* fprintf(stderr, "llen=%ld toReadBlocks=%ld toReadBytes=%ld bytesInBlock=%ld blockNumber=%ld modBlock=%ld\n", */
487 /* llen, toReadBlocks, toReadBytes, bytesInBlock, blockNumber, modBlock); */
488 newoffset = calcFileBlockOffset(e,blockNumber)+modBlock;
489 if (e->file_offset != newoffset) {
490 fseek(e->file, e->file_offset=newoffset, SEEK_SET);
492 rread=fread(ptr, 1, min(llen,bytesInBlock), e->file);
493 e->file_offset += rread;
494 for(i=0; i<toReadBlocks; i++) {
497 newoffset = calcFileBlockOffset(e,blockNumber);
498 if (newoffset != e->file_offset);
499 fseek(e->file, e->file_offset=newoffset , SEEK_SET);
500 readbytes=fread(cptr+rread, 1, min(llen-rread, ssize), e->file);
502 e->file_offset +=readbytes;
504 if(toReadBytes > 0) {
507 newoffset = calcFileBlockOffset(e,blockNumber);
508 fseek(e->file, e->file_offset=newoffset, SEEK_SET);
509 readbytes=fread(cptr+rread, 1, toReadBytes,e ->file);
511 e->file_offset +=readbytes;
513 /* fprintf(stderr, "ole_offset=%ld rread=%ld llen=%ld\n",
514 e->ole_offset, rread, llen);*/
515 e->ole_offset+=rread;
526 int ole_eof(FILE *stream) {
527 oleEntry *e=(oleEntry*)stream;
528 /* fprintf(stderr, "EOF: e->ole_offset=%ld e->length=%ld\n",
529 e->ole_offset, e->length);*/
530 return (e->ole_offset >= e->length);
537 void ole_finish(void) {
538 if ( BBD != NULL ) free(BBD);
539 if ( SBD != NULL ) free(SBD);
540 if ( properties != NULL ) free(properties);
541 if ( rootEntry != NULL ) ole_close((FILE*)rootEntry);
542 properties = SBD = BBD = NULL;
553 int ole_close(FILE *stream) {
554 oleEntry *e=(oleEntry*)stream;
557 if (e->blocks != NULL)
566 * @param stream pointer to OLE stream structure
572 int ole_seek(FILE *stream, long offset, int whence) {
573 oleEntry *e=(oleEntry*)stream;
574 long int new_ole_offset=0, new_file_offset;
575 int ssize, modBlock, blockNumber;
579 new_ole_offset=offset;
583 new_ole_offset=e->ole_offset+offset;
587 new_ole_offset=e->length+offset;
596 if(new_ole_offset >= e->length)
597 new_ole_offset=e->length;
599 ssize = (e->isBigBlock ? sectorSize : shortSectorSize);
600 blockNumber=new_ole_offset/ssize;
601 if ( blockNumber >= e->numOfBlocks )
604 modBlock=new_ole_offset%ssize;
605 new_file_offset = calcFileBlockOffset(e,blockNumber)+modBlock;
606 fseek(e->file, e->file_offset=new_file_offset, SEEK_SET);
607 e->ole_offset=new_ole_offset;
613 * Tell position inside OLE stream
615 * @param stream pointer to OLE stream
617 * @return current position inside OLE stream
619 long ole_tell(FILE *stream) {
620 oleEntry *e=(oleEntry*)stream;
621 return e->ole_offset;
629 size_t (*catdoc_read)(void *ptr, size_t size, size_t nmemb, FILE *stream);
630 int (*catdoc_eof)(FILE *stream);
631 int (*catdoc_seek)(FILE *stream, long offset, int whence);
632 long (*catdoc_tell)(FILE *stream);
634 void set_ole_func(void) {
635 catdoc_read=ole_read;
637 catdoc_seek=ole_seek;
638 catdoc_tell=ole_tell;
642 /* feof is macro in Turbo C, so we need a real function to assign to
645 int my_feof(FILE *f) {
653 void set_std_func(void) {