1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
/*****************************************************************/
/* Reading routines for MS-Word, MS-Write and text files */
/* */
/* This file is part of catdoc project */
/* (c) Victor Wagner 1996-2003, (c) Alex Ott 2003 */
/*****************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include <stdio.h>
#include "catdoc.h"
unsigned short int buffer[PARAGRAPH_BUFFER];
static unsigned char read_buf[256];
static int buf_is_unicode;
/**************************************************************************/
/* Just prints out content of input file. Called when file is not OLE */
/* stream */
/* Parameters - f - file to copy out. header - first few bytes of file, */
/* which have been already read by format recognition code, but should */
/* be output anyway */
/**************************************************************************/
void copy_out (FILE *f,char *header) {
char *buf=(char *)buffer;
int count,i;
long offset;
if (get_unicode_char == get_word8_char) {
/* non-word file and -u specified. Trying to guess which kind of
* unicode is used
*/
if ((unsigned char)header[0]==0xFE && (unsigned char)header[1]==0xFF) {
get_unicode_char = get_utf16msb;
fputs(convert_char((header[2]<<8)|header[3]),stdout);
fputs(convert_char((header[4]<<8)|header[5]),stdout);
fputs(convert_char((header[6]<<8)|header[7]),stdout);
} else if ((unsigned char)header[0]!=0xFF ||
(unsigned char)header[1]!=0xFE) {
int c,j,d;
/* if it is not utf16, assume it is UTF8. We are told -u,
* aren't we */
get_unicode_char = get_utf8;
i=0;
while (i<8) {
c=(unsigned char)header[i++];
if (c >=0x80) {
if ( c<0xE0) {
c=(c & 0x1F);
count =1;
} else {
c=(c & 0xF);
count = 2;
}
for (j=0;j<count;j++) {
if (i<7) {
d=(unsigned char) header[i++];
} else {
d=fgetc(f);
}
c=(c<<6) | (d & 0x3F);
}
}
fputs (convert_char(c),stdout);
}
} else {
get_unicode_char = get_utf16lsb;
fputs(convert_char((header[3]<<8)|header[2]),stdout);
fputs(convert_char((header[5]<<8)|header[4]),stdout);
fputs(convert_char((header[7]<<8)|header[6]),stdout);
}
while (!catdoc_eof(f)) {
i=get_unicode_char(f,&offset,0x7FFFFFFF);
if (i!=EOF) fputs(convert_char(i),stdout);
}
} else {
for (i=0;i<8;i++) {
fputs(convert_char(to_unicode(source_charset,(unsigned char)header[i])),stdout);
}
/* Assuming 8-bit input text */
while ((count = catdoc_read(buf,1,PARAGRAPH_BUFFER,f))) {
for (i=0;i<count;i++) {
fputs(convert_char(to_unicode(source_charset,
(unsigned char)buf[i])),stdout);
}
}
}
}
/**************************************************************************/
/* process_file - main process engine. Reads word file using function, */
/* pointed by get_unicode_char, searches for things which looks like */
/* paragraphs and print them out */
/**************************************************************************/
int process_file(FILE *f,long stop) {
int bufptr;
int tabmode=0;
long offset=0;
int hyperlink_mode = 0;
unsigned short c;
/*
* Now we are starting to read with get_unicode_char. We guard against the
* caller giving us a signed "stop" value which would be invalid.
*/
while (!catdoc_eof(f) && stop >= 0 && offset<stop) {
bufptr = -1;
do {
c=get_unicode_char(f,&offset,stop);
/* Following symbols below 32 are allowed inside paragraph:
0x0002 - footnote mark
0x0007 - table separator (converted to tabmode)
0x0009 - Horizontal tab ( printed as is)
0x000B - hard return
0x000C - page break
0x000D - return - marks an end of paragraph
0x001E - IS2 for some reason means short defis in Word.
0x001F - soft hyphen in Word
0x0013 - start embedded hyperlink
0x0014 - separate hyperlink URL from text
0x0015 - end embedded hyperlink
*/
if (tabmode) {
tabmode=0;
if (c==0x007) {
buffer[++bufptr]=0x1E;
continue;
} else {
buffer[++bufptr]=0x1C;
}
}
if (c<32) {
switch (c) {
case 0x007:
tabmode = 1;
break;
case 0x000D:
case 0x000B:
buffer[++bufptr]=0x000A;
break;
case 0x000C:
buffer[++bufptr]=c;
break;
case 0x001E:
buffer[++bufptr]='-';
break;
case 0x0002: break;
case 0x001F:
buffer[++bufptr]=0xAD;/* translate to Unicode
soft hyphen */
break;
case 0x0009:
buffer[++bufptr]=c;
break;
case 0x0013:
hyperlink_mode=1;
buffer[++bufptr]=' ';
break;
case 0x0014:
hyperlink_mode = 0;
/*fall through */
case 0x0015:
/* just treat hyperlink separators as
* space */
buffer[++bufptr]=' ';
break;
case 0x0001: if (hyperlink_mode)
break;
/* else fall through */
default:
bufptr=-1; /* Any other control char - discard para*/
}
} else if (c != 0xfeff) {
/* skip ZERO-WIDTH-UNBREAKABLE-SPACE. Output anything
* else*/
buffer[++bufptr]=c;
}
} while (bufptr<=PARAGRAPH_BUFFER-2 &&
!catdoc_eof(f) &&
buffer[bufptr]!=0x000a);
if ((bufptr>0) && (bufptr < sizeof(buffer) / sizeof(buffer[0]) - 1)) {
buffer[++bufptr]=0;
output_paragraph(buffer);
}
}
return 0;
}
/**********************************************************************/
/* Reads file from MS-Word 97 and above file. Takes in account strange*
* situation that unicode and non-unicode 256-byte blocks could be *
* intermixed in word file *
* *
* Parameters: *
* *
* f - file to read *
* offset - position of the character inside file (to determine *
* possible block boundaries *
**********************************************************************/
int get_word8_char(FILE *f,long *offset,long fileend) {
int count,i,u;
char c;
if ((i=(*offset)%256) ==0) {
count=catdoc_read(read_buf,1,256,f);
memset(read_buf+count,0,256-count);
buf_is_unicode=0;
if (*offset+(long)count>fileend) {
count=fileend-*offset;
}
while (i<count) {
c=read_buf[i++];
if ((c==0x20|| c==0x0D||ispunct(c))&&i<count&&read_buf[i]==0) {
buf_is_unicode=1;
break;
}
i++;
}
i=0;
}
if (buf_is_unicode) {
u=read_buf[i] | read_buf[i+1]<<8;
(*offset)+=2;
} else {
u=to_unicode(source_charset,read_buf[i]);
(*offset)++;
}
return u;
}
|