  ' From: maarten@uva.uucp (Maarten Carels) * Subject: The MS Word File format - revised Date: 11 Aug 86 15:25:58 GMT= Organization: Computer Science Dept., University of Amsterdam 
 Lines: 544  H After  our previous posting, we found out a lot more about the format ofH MS  Word  document  files.  By now, all parts of the file are known, notH only  the  function  of  the part, but also the almost complete internalH structure.  A  new  version of the document (BinHexed MS Word format) isH posted  to  net.sources.mac.  For  those  of you who do not have MS WordH (what  could  they  do  with  this description -:)), a text only version follows this posting.   H New  parts  discovered are mostly related to the division structure of aH MS Word document. Almost all details related to this part are described.  F Some small errors in the first version of the document were corrected.  H We  encourage  everyone  to make use of the information provided, but weH expect  you  to  be  fair and place every program you develop based uponH this  information  in the public domain, or at least make it a sharewareH product.  (Free  for  us  of  course  !). Also we would like anybody whoH discovers  new  facts  about  the  format,  or  who  finds errors in our explanation to inform us.   , (-------------- Cut Here ------------------)   MicroSoft Word file format   second, revised edition    M.J. Carels,   A.G. Starreveld        Department of Computer Science     University of Amsterdam   : e-mail: {decvax, philabs, seismo}!mcvax!uva!{maarten,dolf}  : s-mail: Kruislaan 409, 1098 SJ  Amsterdam, The Netherlands   1. Introduction.  H This document describes the structure of the files produced by MicroSoftC WORD (versions 1.00 and 1.05). These files are of type TWDBNU. This C knowledge has been gathered by looking at such files, and trying to K interpret the bytes in the file. All information is believed to be correct, I but no responsibility for errors or omissions can be taken. Please let us G know if you find errors in this document, or if you find more about the J structure of the file. The file format used by MS Word for other computers& than Macs may, or may not be the same.  J We will describe all structures of the file format by means of C structureD declarations, as this is a convenient way to describe things. In the. structures some basic types appear. These are:   ubyte8 bits unsigned number  byte8 bits signed number ushort16 bits unsigned number  short16 bits signed number ulong32 bits unsigned number long32 bits signed number   B No alignment is to be assumed, the only bytes present are the onesE described. In all structures byte addresses (decimal) are included as 	 comments.    2. General file structure.  G A  MS Word file of type TWDBNU can be divided into seven different main J parts. We will call these parts the RheaderS, RtextS, Rcharacter formatsS,H Rparagraph formatsS, Rdivision blocksS, Rdivision listS and Rpage listS K respectively. These parts appear in the above mentioned order in the file's A data fork. The resource fork is always empty, i.e. not allocated.   K The file can be seen as being built from basic blocks, each 128 bytes long. M This is the case for all parts of the file, although it does not appear to be L significant for the text part. This implies that the size of an MS Word fileB is always a multiple of 128 bytes, though within each of the partsK mentioned, the last bytes in the last block belonging to a certain part may # (and usually will) contain garbage.   L In several sections sizes, dimensions and distances are present. In all suchK fields these dimensions are given in Tbasic unitsU. A MS Word basic unit is G 1/20 of a point, or 1/1440 of an inch (a point equals 1/72 of an inch).   
 3. Header.  D The header part consists of a single block of 128 bytes. It containsK pointers to most parts of the file. The header can be defined in terms of a  C structure as follows:    /*1  *This structure starts each MS-WORD 'WDBN' file.   */    struct_header{& /* 00 */ushorth_1 ;/* always 0xfe32 */! /* 02 */ushorth_2 ;/* always 0 */ & /* 04 */ushorth_3 ;/* always 0xab00 */& /* 06 */shorth_unk1[4] ;/* always 0 */3 /* 14 */ulongh_ET ;/* Position of byte past text */ 2 /* 18 */ushorth_par ;/* first paragraph block # */6 /* 20 */ushorth_div ;/* first division info block # */  /* 22 */ushorth_div1 ;/* same */: /* 24 */ushorth_divlist ;/* first division list block # */7 /* 26 */ushorth_pagelist ;/* first page list block # */ 8 /* 28 */ushorth_unalloc ;/* first unallocated block # */' /* 30 */shorth_unk2[17] ;/* always 0 */ , /* 64 */ulongh_tlength ;/* Length of text */# /* 68 */ulongh_tlength1 ;/* same */ ' /* 72 */shorth_unk3[28] ;/* always 0 */  };   typedef struct _header MS_Head;   I The h_ET field contains the address within the file of the byte just past L the last character in the document text, i.e. the RtextS part. The h_tlengthL field contains the length of the RtextS part in bytes. The h_par field givesD the block number (remember a MS Word block is 128 bytes long) of theG first block that contains paragraph formats. Every division has its own G block containing margins, page number and that kind of stuff. The h_div F field contains the block number for the first division block. For someK reason it is stored twice. The connection between the text and the division M blocks is made through the division list. The h_divlist field gives the block K number for the first block in the division list. The last block in the file L contains the page list. In the page list the position of the first characterJ in the first line of each page is stored. It is this list which is updatedF when you issue a RRepaginateS (COMMAND-J) command. The small T=U signsG in the margins come also from this list. The h_pagelist field gives the L first block for this list. The block number of the first RunallocatedS blockI is stored in the h_unalloc field. The file is also h_unalloc blocks long.   
 4. Text part.   C The text part contains a complete representation of the text in the F document, including running heads, footnotes and pictures. The text isC represented in the order in which it occurs in the document, in the F extended Macintosh ascii character set. Some ascii values have special meaning however:  ! 0x01page number ((page) glossary) : 0x05auto numbered footnote reference ((footnote) glossary)$ 0x0bForced new line within paragraph& 0x0cEnd of division or forced new page 0x0dEnd of paragraph 0x1fOptional hyphen   H The above implies that to extract a text only version of an MS Word fileL one only needs to extract the text part of the file, possibly replacing someI of the special characters with others, depending on what you want. If you H do nothing, you will certainly get very long lines, since you will get aH newline character only at the end of each paragraph, so perhaps you wantH to do some line folding. Pictures are stored within the text part, alongI with a header. The picture is a single paragraph by itself. The paragraph @ format run pointing to the picture has a bit set to indicate the% corresponding paragraph is a picture.    5. Format runs.   K Everything related to the layout of the text is stored in what we will call H Rformat runsS and Rformat descriptorsS. A format run consists of severalE bytes of formatting information, described below (section 6 and 7). A G format descriptor consists of 6 bytes. It is described by the following 
 structure:   /*-  *This structure defines a format descriptor.   */    struct_fdescriptor{ 6 /* 00 */ulongfd_start;/* start of text for next run */5 /* 04 */shortfd_run ;/* pointer to this format run */  };  C Each format block starts with the offset in the text part where the G formats of this block start. After the initial start a number of format F descriptors follow. The rest of the format block contains format runs.F Both the format run and the format descriptor must be contained in theA same block. A new block is allocated if either one does not fit.    D The last byte in a format block (offset 0x7f) contains the number of( format descriptors present in the block.  I The format runs are stored preceded by a byte count. This bytecount gives K the number of bytes in the format run that are actually stored in the file. I The other (not stored) bytes of the format run contain the default value. : File size is reduced by not storing seldomly used fields.   J The fd_start field is a pointer in the text part of the document. The nextL format applies from there. The fd_run field is an offset (relative to byte 4( in the format block) to the format run.    6. Character formats.   J The character format runs define how the characters in the text look. ThisH includes properties like the font, size and style of the characters. TheJ character format runs are 6 bytes long, although not all 6 need be stored.I One field needs special attention. The font number is split in (at least) I two pieces. The low order 6 bits are in the cf_font field. This fits most I standard fonts, as they have small numbers. More exotic fonts have larger L numbers. The extra bits are stored in the high order 3 bits of the cf_flags25 field. The meaning of the bytes in the format run is:    /*+  *This structure defines character formats.   */    struct_cformat{ 8 /* 00 */ubytecf_unknown ;/* seems always 0x80 or 0x00 */3 /* 01 */ubytecf_font ;/* font number, some flags */ 5 /* 02 */ubytecf_pointsize ;/* times 2, 0 = default */ ( /* 03 */ubytecf_flags1 ;/* more flags */5 /* 04 /*ubytecf_flags2 ;/* more flags, more font # */ 8 /* 05 */bytecf_position ;/* > 0 super, < 0 sub script */ };    typedef struct _cformat MS_CFmt;  * /* macro for extracting the font number */H #define CHF_FONT(x) (((x)->cf_font&0x3f) | (((x)->cf_flags2&0xe0) << 1))  " /* values for the cf_font field */  ! #defineCHF_BOLD0x80/* bold bit */ # #defineCHF_ITAL0x40/* italic bit */   - /* values for the flags1 and flags2 fields */     #defineF1_UL0x80/* underlined */  #defineF1_SC0x0c/* Small Caps */ #defineF2_OL0x10/* outline */  #defineF2_SH0x08/* shadow */  H The first field in a character formats format run seems to take only the; values 0x00 and 0x80. The meaning of this field is unknown.    7. Paragraph formats.   K The fourth part of the file contains the paragraph formats. The format runs I start with normal paragraph formatting information. Thereafter follow the J Rtab definitionsS. As many tab definitions as needed will be in the format< run. The tab definition is described by the structure below:   /*  *This structure defines tabs   */    struct_tformat {, /* 00 */ushortt_position ;/* tab position */. /* 02 */ushor tt_flags ;/* type of tab stop */ };   typedef struct _tformat MS_Tab;   " /* values for the t_flags field */  5 #defineT_ALIGNMS K0x600 0/* mask for tab alignment */  /* alignment values: */ * #defineT_LEFT0x0000/* left aligning tab */. #defineT_CENTER0x2000/* center aligning tab */, #defineT_RIGHT0x4000/* right aligning tab */' #defineT_DECIMAL0x6000/* decimal tab */     / #defineT_LEADMSK0x0c00/* mask for tab leader */    /* leader values: */& #defineT_BLANK0x0000/* blank leader */& #defineT_DOTS0x0400/* dotted leader */& #defineT_DASH0x0800/* dashed leader */$ #defineT_LINE0x0c00/* line leader */  1 The format run is defined by the structure below:    /*+  *This structure defines paragraph formats.   */    struct_pformat{ & /* 00 */ushortp_flags;/* some flags */# /* 02 */ushortp_unk1;/* always 0 */ ( /* 04 */ushortp_right;/* right indent */& /* 06 */ushortp_left;/* left indent */( /* 08 */ushortp_first;/* first indent */: /* 10 */ushortp_line_spacing;/* line spacing (0 = auto) */) /* 12 */ushortp_before;/* space before */ ' /* 14 */ushortp_after;/* space after */ < /* 16 */ushortp_rhead_pict;/* running head & picture info */# /* 18 */ushortp_unk2;/* always 0 */ # /* 20 */ushortp_unk3;/* always 0 */ 6 /* 22 */MS_Tabp_tabs [0];/* list of tab descriptors */ };   typedefstruct _pformatMS_Fmt;   " /* values for the p_flags field */  4 #definePF_FOOTMASK0x7f00/* mask for footnote info */ /* values unknown */  4 #definePF_JUSTMASK0x00c0/* mask for justification */ /* justification values: */ 1 #definePF_LEFT0x0000/* left justifiedparagraph */ . #definePF_CENTER0x0040/* centered paragraph */4 #definePF_RIGHT0x0080/* right justified paragraph */- #definePF_JUST0x00c0/* justified paragraph */ 2 #definePF_KEEP0x0010/* keep with next paragraph */. #definePF_KEEPL0x0020/* keep lines together */  ' /* values for the running_head field */   4 #defineRH_MASK0xf000/* mask for running head info */ /* running head values: */0 #defineRH_FIRST0x8000/* appears on first page */' #defineRH_EVEN0x4000/* on even pages */ % #defineRH_ODD0x2000/* on odd pages */ 5 #defineRH_BOTTOM0x1000/* appears on bottom of page */   5 #defineRH_PICT0x0800/* this paragraph is a picture */   D The p_flags field has sometimes the high order bit set (0x8000). TheC meaning of this could be the same as the setting of this bit in the J character formats, where it is also sometimes set. What this bit indicates is unknown.   K In the paragraph format run only the tab descriptors needed for explicitely F defined tabs are stored. The list of tab descriptors is ended by a tabM descriptor with the t_position field equal to 0. This final tab descriptor is 6 stored in the file, although this seems not necessary.  J A picture is identified by having the RH_PICT bit set. In the text part ofE the document the picture is present, preceded by a 6 byte header. The L actual picture data follows after the header. It is encoded in standard PICTJ format (see tech note #21). The picture header is defined by the structure given below:   /*' This structure defines a picture header    */   struct_phead {4 /* 00 */shortph_offset;/* offset from left margin */5 /* 02 */shortph_xdist;/* distortion in x direction */ 5 /* 04 */shortph_ydist;/* distortion in y direction */  };   typedef struct _phead MS_Pict;  H The ph_xdist and ph_ydist fields are used to store the distortion of theK picture. If both are zero, the picture is undistorted. The exact meaning of # the values stored here is unknown.     8. Division blocks.   I The next (fifth) part of the file contains the division blocks. One block J (128 bytes!) is allocated for every division. The block is filled with theG following structure, preceded by a byte count indicating how many bytes  are actually stored:   /*+  *This structure describes division formats   */    struct _dformat { ! /* 00 */ushortd_flags;/* flags */ 0 /* 02 */ushortd_pap_len;/* total paper length *// /* 04 */ushortd_pap_wit;/* total paper width */ * /* 06 */ushortd_p_start;/* start page # */$ /* 08 */ushortd_top;/* top margin */7 /* 10 */ushortd_bot;/* bottom margin, from top paper */ & /* 12 */ushortd_left;/* left margin */9 /* 14 */ushortd_right;/* right margin, from left paper */ < /* 16 */ushortd_flag_col;/* some flags, number of columns */< /* 18 */ushortd_r_top;/* top run.head pos, from top paper */? /* 20 */ushortd_r_bot;/* bottom run.head pos, from top paper */ * /* 22 */ushortd_colsp;/* column spacing */# /* 24 */ushortd_gutter;/* gutter */ ; /* 26 */ushortd_pag_top;/* page number position from top */ = /* 28 */ushortd_pag_left;/* page number position from left */  /* 30 */ushortd_unk1; = /* 32 */ushortd_rbot;/* seems runn.  head pos, from bottom */  /* 34 */shortd_unk2[34]; };   typedef struct _dformat MS_Div;   ) #defineDFB_MASK0x00e0/* mask for break */  /* values for break: */ % #defineDFB_CONT0x0000/* continuous */   #defineDFB_COL0x0020/* column */) #defineDFB_PAGE0x0040/* page (default) */  defineDFB_ODD0x0060/* odd */ #defineDFB_EVEN0x0080/* even */   5 #defineDFP_MASK0x001c/* mask for the page # format */  /* values for page # format: */ * #defineDFP_NUM0x0000/* numeric (1, 2...)*/6 #defineDFP_ROM0x0004/* roman, upper case (I, II...) */6 #defineDFP_rom0x0008/* roman, lower case (i, ii...) */: #defineDFP_ALF0x000c/* alphabetic, upper case (A, B...) */: #defineDFP_alf0x0010/* alphabetic, lower case (a, b...) */  0 #defineDF_DIV0x0001/* division layout present */  1 #defineDEFAULT_PAG0xffff/* default page number */   , /* mask for flags in the d_flag_col field */
 /* values: */ 1 #defineDCF_AUTO0x0200/* auto page numbering on */ 8 #defineDCF_FOOT0x0100/* 1=footnote at end of division */  4 #defineDCF_COL0x00ff/* mask for number of columns */  I Some of the values stored in the division blocks seem to have no relationmF to a division, but rather to the document as a whole. These values areN present in all division blocks, but only the value in the first division blockG is used. The values in the other blocks are just ignored. As usual, allt dimensions are in basic units.    J The DF_DIV bit used to indicate whether the division layout information isE stored (DF_DIV = 1) or only the paper dimensions (DF_DIV = 0). If theh> DF_DIV bit is 0, only 16 bytes of the division block are used.  G The division blocks do not contain any information on which part of theoL text they apply to. This information is stored in the next part of the file, the division list:   9. Division list.f  G The division list links the division blocks to the text. It consists of 9 division descriptors. These are defined by the structure:t   /*0  *This structure describes a division descriptor  */h  
 struct_pdiv {i4 /* 00 */ulongpd_text;/* where the division starts */" /* 04 */ushortpd_unk;/* unknown */2 /* 06 */ulongpd_block;/* there is the div block */ };   typedef struct _pdiv MS_DivD;   F The pd_text field gives the place in the text where the division ends,K relative to the start of the text part. Add 0x80 to get the offset from theoL start of the file. The pd_block field gives the offset in the file where theM division block starts. If the pd_block field is 0xffffffff, this division hasaK no division block allocated. This seems a division with all default values..+ The meaning of the pd_unk field is unclear.   D The structure described in section 8 is present in the correspondingB division block, preceded by a bytecount. As with the paragraph andC character formats, bytes not stored contain default values. As manytI division descriptors are in the division list as there are divisions. The J division list holds some more information, as can be seen in the structure definition:u   /*,  *This structure describes the division list  */l   struct _divlist {e6 /* 00 */ushortdl_count;/* the number of descriptors *// /* 02 */ushortdl_unk;/* some unknown counter */e2 /* 06 */MS_DivDdl_list [0];/* as many as needed */ };   typedef struct _divlist MS_Div;d  J If one block (128 bytes) is not sufficient to store the division list, the list can span block boundaries.  I The division list is also the way to se if a 0x0c character stored within F the file is a Tforced new pageU (TSHIFT-ENTERU) or a Tend of divisionUL character. If there is no entry for the 0x0c character in the division list, it must be only a new page.l   10. Page list.  J The last part of the file is the page list. This list contains informationH about the pages in the document. This information is used to process theI RGo ToIS command, and to show the small T=U signs in the margin. The pageb@ list is updated when the document is repaginated by means of theF RRepaginateS command. The items in the page list are described by this
 structure:   /*+  *This structure describes a page list itemn  */e   struct _page {* /* 00 */ushortpg_num;/* the page number */* /* 02 */ulongpg_text;/* where it starts */ };   typedef struct _page MS_Page;n  H The page numbers seem to be always in numerical order. The pg_text fieldI is the offset in the text part where the page starts. Add 0x80 to get the,M position relative to the start of the file. All page list items are containedy8 in the page list, which fills the last part of the file:   /*(  *This structure describes the page list  */s   struct _plist {r3 /* 00 */ushortpl_count;/* the number  list items *//+ /* 02 */ushortpl_unk;/* some other count */e) /* 04 */MS_Pagepl_list [0];/* the list */h };   typedef struct _plist MS_PList;k   11. End of file.  I Just after the page list the end of file is present. The file contains ansG integral number of MS Word blocks. As a MS Word block is smaller than aOE physical disk block, the end of file may be in the middle of the last0 physical disk block.        Dolf Starreveld / Maarten Carels# Department of Computer Science, UvAh   Usenet:{dolf,maarten}@uva.uucp0 {seismo,decvax,philabs}!mcvax!uva!{dolf,maarten}   Snail mail:Dolf Starreveld Department of Computing Science_ University of Amsterdamf
 Kruislaan 409* NL-1098 SJ  Amsterdams The Netherlandss  ) Telefone:In Holland:    020-592 5137/5022 / International: 31-20-592 5137 or 31-20-592 5022b   Telex:10262 HEF NL            