static size_t php_ole_ops_read(php_stream *stream, char *buf, size_t count TSRMLS_DC) { OLE_DATA_SELF(); if(self->input){ int cur = gsf_input_tell(self->input); int len = gsf_input_size(self->input); int read_len = (int)count; if(read_len > len - cur){ read_len = len - cur; } gboolean eof = gsf_input_eof(self->input); stream->eof = eof; if(eof){ return 0; } gsf_input_read(self->input,read_len,buf); int size = gsf_input_tell(self->input); return read_len; } else{ return 0; } }
static gboolean init_zip (GsfInputGZip *gzip, GError **err) { gsf_off_t cur_pos; if (Z_OK != inflateInit2 (&(gzip->stream), -MAX_WBITS)) { if (err != NULL) *err = g_error_new (gsf_input_error_id (), 0, "Unable to initialize zlib"); return TRUE; } cur_pos = gsf_input_tell (gzip->source); if (gsf_input_seek (gzip->source, 0, G_SEEK_SET)) { if (err) *err = g_error_new (gsf_input_error_id (), 0, "Failed to rewind source"); return TRUE; } if (check_header (gzip) != FALSE) { if (err != NULL) *err = g_error_new (gsf_input_error_id (), 0, "Invalid gzip header"); if (gsf_input_seek (gzip->source, cur_pos, G_SEEK_SET)) { g_warning ("attempt to restore position failed ??"); } return TRUE; } return FALSE; }
static GlpkFileVersion gnm_glpk_detect_version (GnmGlpk *lp, GsfInputTextline *tl) { GnmSubSolver *subsol = lp->parent; gsf_off_t cur = gsf_input_tell (GSF_INPUT (tl)); GlpkFileVersion ver = GLPK_UNKNOWN; const char *line; unsigned cols, rows; if ((line = gsf_input_textline_utf8_gets (tl)) == NULL) goto out; if (sscanf (line, "%u %u", &rows, &cols) == 2 && cols == g_hash_table_size (subsol->cell_from_name)) { ver = GLPK_457; if (gnm_solver_debug ()) g_printerr ("Detected version 4.57 file format\n"); goto out; } if ((line[0] == 'c' || line[0] == 's') && line[1] == ' ') { ver = GLPK_458; if (gnm_solver_debug ()) g_printerr ("Detected version 4.58 file format\n"); goto out; } out: // Extra seek due to gsf bug gsf_input_seek (GSF_INPUT (tl), cur + 1, G_SEEK_SET); gsf_input_seek (GSF_INPUT (tl), cur, G_SEEK_SET); return ver; }
static void dump_biff_stream (GsfInput *stream) { guint8 const *data; guint16 len, opcode; unsigned pos = gsf_input_tell (stream); while (NULL != (data = gsf_input_read (stream, 4, NULL))) { gboolean enable_dump = TRUE; opcode = GSF_LE_GET_GUINT16 (data); len = GSF_LE_GET_GUINT16 (data+2); if (len > 15000) { enable_dump = TRUE; g_warning ("Suspicious import of biff record > 15,000 (0x%x) for opcode 0x%hx", len, opcode); } else if ((opcode & 0xff00) > 0x1000) { enable_dump = TRUE; g_warning ("Suspicious import of biff record with opcode 0x%hx", opcode); } if (enable_dump) printf ("Opcode 0x%3hx : %15s, length 0x%hx (=%hd)\n", opcode, get_biff_opcode_name (opcode), len, len); if (len > 0) { data = gsf_input_read (stream, len, NULL); if (data == NULL) break; if (enable_dump) gsf_mem_dump (data, len); } pos = gsf_input_tell (stream); } }
void IE_Imp_StarOffice::readRecSize(GsfInput* aStream, UT_uint32& aSize, gsf_off_t* aEOR) throw(UT_Error) { // Yes, that's correct, only 3 bytes. guint8 buf [3]; aSize = 0; streamRead(aStream, buf, 3); // buf content is little endian. aSize = buf [0] | (buf[1] << 8) | (buf [2] << 16); aSize -= 4; // Substract 4 for the rec type + size if (aSize == 0xFFFFFF && mDocHdr.nVersion >= SWG_LONGRECS) { // XXX need recsizes from header, see above UT_ASSERT_HARMLESS(UT_NOT_IMPLEMENTED); } if (aEOR) *aEOR = gsf_input_tell(aStream) + aSize; }
U32 wvStream_tell (wvStream * in) { if (in->kind == GSF_STREAM) { return (U32)gsf_input_tell(GSF_INPUT (in->stream.gsf_stream)); } else if(in->kind == FILE_STREAM) { return ((U32) ftell (in->stream.file_stream)); } else { return (in->stream.memory_stream->current); } }
U32 wvStream_offset (wvStream * in, long offset) { if (in->kind == GSF_STREAM) { gsf_input_seek (GSF_INPUT (in->stream.gsf_stream), offset, G_SEEK_CUR); return (U32)gsf_input_tell(GSF_INPUT (in->stream.gsf_stream)); } else if (in->kind == FILE_STREAM) { return ((U32) fseek (in->stream.file_stream, offset, SEEK_CUR)); } else { in->stream.memory_stream->current += offset; return in->stream.memory_stream->current; } }
U32 wvStream_goto (wvStream * in, long position) { if (in->kind == GSF_STREAM) { gsf_input_seek (GSF_INPUT (in->stream.gsf_stream), position, G_SEEK_SET); return (U32)gsf_input_tell(GSF_INPUT (in->stream.gsf_stream)); } else if (in->kind == FILE_STREAM) { return ((U32) fseek (in->stream.file_stream, position, SEEK_SET)); } else { in->stream.memory_stream->current = position; return in->stream.memory_stream->current; } }
/** * tar_init_info : * @tar : #GsfInfileTar * * Read tar headers and do some sanity checking * along the way. **/ static void tar_init_info (GsfInfileTar *tar) { TarHeader end; const TarHeader *header; gsf_off_t pos0 = gsf_input_tell (tar->source); char *pending_longname = NULL; memset (&end, 0, sizeof (end)); while (tar->err == NULL && (header = (const TarHeader *)gsf_input_read (tar->source, HEADER_SIZE, NULL))) { char *name; gsf_off_t length; gsf_off_t offset; if (memcmp (header->filler, end.filler, sizeof (end.filler))) { tar->err = g_error_new (gsf_input_error_id (), 0, "Invalid tar header"); break; } if (memcmp (header, &end, HEADER_SIZE) == 0) break; if (pending_longname) { name = pending_longname; pending_longname = NULL; } else name = g_strndup (header->name, sizeof (header->name)); length = unpack_octal (tar, header->size, sizeof (header->size)); offset = gsf_input_tell (tar->source); #if 0 g_printerr ("[%s]: %d\n", name, (int)length); #endif switch (header->typeflag) { case '0': case 0: { /* Regular file. */ GsfInfileTar *dir; const char *n = name, *s; TarChild c; /* This is deliberately slash-only. */ while ((s = strchr (n, '/'))) n = s + 1; c.name = g_strdup (n); c.offset = offset; c.length = length; c.dir = NULL; dir = tar_directory_for_file (tar, name, FALSE); g_array_append_val (dir->children, c); break; } case '5': { /* Directory */ (void)tar_directory_for_file (tar, name, TRUE); break; } case 'L': { const char *n; if (pending_longname || strcmp (name, MAGIC_LONGNAME) != 0) { tar->err = g_error_new (gsf_input_error_id (), 0, "Invalid longname header"); break; } n = gsf_input_read (tar->source, length, NULL); if (!n) { tar->err = g_error_new (gsf_input_error_id (), 0, "Failed to read longname"); break; } pending_longname = g_strndup (n, length); break; } default: /* Other -- ignore */ break; } g_free (name); /* Round up to block size */ length = (length + (BLOCK_SIZE - 1)) / BLOCK_SIZE * BLOCK_SIZE; if (!tar->err && gsf_input_seek (tar->source, offset + length, G_SEEK_SET)) { tar->err = g_error_new (gsf_input_error_id (), 0, "Seek failed"); break; } } if (pending_longname) { if (!tar->err) tar->err = g_error_new (gsf_input_error_id (), 0, "Truncated archive"); g_free (pending_longname); } if (tar->err) gsf_input_seek (tar->source, pos0, G_SEEK_SET); }
UT_Error IE_Imp_PalmDoc::_parseFile(GsfInput * pdfp) { UT_GrowBuf gbBlock(1024); bool bEatLF = false; bool bEmptyFile = true; UT_UCSChar c; UT_UCS4Char wc; pdb_header header; doc_record0 rec0; bool bCompressed = false; int num_records, rec_num; DWord file_size, offset; gsf_input_read( pdfp, PDB_HEADER_SIZE, (guint8*)&header); if (strncmp( header.type, DOC_TYPE, sizeof(header.type) ) || strncmp( header.creator, DOC_CREATOR, sizeof(header.creator) )) { UT_DEBUGMSG(("This is not a DOC file!\n")); // Create an empty paragraph. X_ReturnNoMemIfError(appendStrux(PTX_Block, NULL)); return UT_OK; } num_records = _swap_Word( header.numRecords ) - 1; gsf_input_seek( pdfp, PDB_HEADER_SIZE, G_SEEK_SET ); GET_DWord( pdfp, offset ); gsf_input_seek( pdfp, offset, G_SEEK_SET ); gsf_input_read( pdfp, sizeof(rec0), (guint8*)&rec0); if ( _swap_Word( rec0.version ) == 2 ) bCompressed = true; gsf_input_seek( pdfp, 0, G_SEEK_END ); file_size = gsf_input_tell( pdfp ); for (rec_num = 1; rec_num <= num_records; ++rec_num ) { DWord next_offset; gsf_input_seek( pdfp, PDB_HEADER_SIZE + PDB_RECORD_HEADER_SIZE * rec_num, G_SEEK_SET); GET_DWord( pdfp, offset ); if( rec_num < num_records ) { gsf_input_seek( pdfp, PDB_HEADER_SIZE + PDB_RECORD_HEADER_SIZE * (rec_num + 1), G_SEEK_SET); GET_DWord( pdfp, next_offset ); } else next_offset = file_size; gsf_input_seek( pdfp, offset, G_SEEK_SET ); // be overly cautious here _zero_fill (m_buf->buf, BUFFER_SIZE); gsf_input_read(pdfp, next_offset - offset, m_buf->buf); m_buf->position = next_offset - offset; if ( bCompressed ) _uncompress( m_buf ); m_buf->position = 0; while ( (m_buf->position) < (m_buf->len) ) { // don't copy over null chars if (m_buf->buf[m_buf->position] == '\0') { ++m_buf->position; continue; } if( !m_Mbtowc.mbtowc( wc, m_buf->buf[m_buf->position] ) ) continue; c = static_cast<UT_UCSChar>(wc); switch (c) { case static_cast<UT_UCSChar>('\r'): case static_cast<UT_UCSChar>('\n'): if ((c == static_cast<UT_UCSChar>('\n')) && bEatLF) { bEatLF = false; break; } if (c == static_cast<UT_UCSChar>('\r')) { bEatLF = true; } // we interprete either CRLF, CR, or LF as a paragraph break. // start a paragraph and emit any text that we // have accumulated. X_ReturnNoMemIfError(appendStrux(PTX_Block, NULL)); bEmptyFile = false; if (gbBlock.getLength() > 0) { X_ReturnNoMemIfError(appendSpan(reinterpret_cast<const UT_UCSChar*>(gbBlock.getPointer(0)), gbBlock.getLength())); gbBlock.truncate(0); } break; default: bEatLF = false; X_ReturnNoMemIfError(gbBlock.ins(gbBlock.getLength(),reinterpret_cast<const UT_GrowBufElement *>(&c),1)); break; } ++m_buf->position; } } if (gbBlock.getLength() > 0 || bEmptyFile) { // if we have text left over (without final CR/LF), // or if we read an empty file, // create a paragraph and emit the text now. X_ReturnNoMemIfError(appendStrux(PTX_Block, NULL)); if (gbBlock.getLength() > 0) X_ReturnNoMemIfError(appendSpan(reinterpret_cast<const UT_UCSChar *>(gbBlock.getPointer(0)), gbBlock.getLength())); } return UT_OK; }
long AbiWordperfectInputStream::tell() { return gsf_input_tell(m_input); }
UT_Error IE_Imp_StarOffice::_loadFile(GsfInput * input) { try { UT_DEBUGMSG(("SDW: Starting import\n")); mOle = GSF_INFILE (gsf_infile_msole_new(input, NULL)); if (!mOle) return UT_IE_BOGUSDOCUMENT; // firstly, load metadata SDWDocInfo::load(mOle, getDoc()); mDocStream = gsf_infile_child_by_name(mOle, "StarWriterDocument"); if (!mDocStream) return UT_IE_BOGUSDOCUMENT; gsf_off_t size = gsf_input_size(mDocStream); if (!appendStrux(PTX_Section, PP_NOPROPS)) return UT_IE_NOMEMORY; UT_DEBUGMSG(("SDW: Attempting to load DocHdr...\n")); mDocHdr.load(mDocStream); UT_DEBUGMSG(("SDW: ...success\n")); // Ask for and verify the password if (mDocHdr.cryptor) { if (!mDocHdr.cryptor->SetPassword(GetPassword().c_str())) { UT_DEBUGMSG(("SDW: Wrong password\n")); return UT_IE_PROTECTED; } } // do the actual reading char type; bool done = false; UT_uint32 recSize; while (!done) { if (gsf_input_tell(mDocStream) == size) break; readChar(mDocStream, type); gsf_off_t eor; readRecSize(mDocStream, recSize, &eor); switch (type) { case SWG_CONTENTS: { gsf_off_t flagsEnd = 0; UT_uint32 nNodes; // sw/source/core/sw3io/sw3sectn.cxx#L129 if (mDocHdr.nVersion >= SWG_LAYFRAMES) { UT_uint8 flags; readFlagRec(mDocStream, flags, &flagsEnd); } if (mDocHdr.nVersion >= SWG_LONGIDX) streamRead(mDocStream, nNodes); else { if (mDocHdr.nVersion >= SWG_LAYFRAMES) { UT_uint16 sectidDummy; streamRead(mDocStream, sectidDummy); } UT_uint16 nodes16; streamRead(mDocStream, nodes16); nNodes = (UT_uint32)nodes16; } if (flagsEnd) { UT_ASSERT(flagsEnd >= gsf_input_tell(mDocStream)); if (gsf_input_tell(mDocStream) != flagsEnd) { UT_DEBUGMSG(("SDW: have not read all flags\n")); if (gsf_input_seek(mDocStream, flagsEnd, G_SEEK_SET)) return UT_IE_BOGUSDOCUMENT; } } bool done2 = false; UT_uint32 size2; while (!done2) { readChar(mDocStream, type); gsf_off_t eor2; readRecSize(mDocStream, size2, &eor2); switch (type) { case SWG_TEXTNODE: { // sw/source/core/sw3io/sw3nodes.cxx#L788 UT_DEBUGMSG(("SDW: Found Textnode! (start at 0x%08llX end at 0x%08llX)\n", (long long)gsf_input_tell(mDocStream), (long long)eor2)); UT_uint8 flags; gsf_off_t newPos; readFlagRec(mDocStream, flags, &newPos); // XXX check flags if (gsf_input_seek(mDocStream, newPos, G_SEEK_SET)) return UT_IE_BOGUSDOCUMENT; // Read the actual text UT_UCS4Char* str; readByteString(mDocStream, str); UT_UCS4String textNode(str); free(str); UT_DEBUGMSG(("SDW: ...length=%zu contents are: |%s|\n", textNode.length(), textNode.utf8_str())); // now get the attributes UT_String attrs; UT_String pAttrs; UT_Vector charAttributes; while (gsf_input_tell(mDocStream) < eor2) { char attVal; streamRead(mDocStream, attVal); UT_uint32 attSize; gsf_off_t eoa; // end of attribute readRecSize(mDocStream, attSize, &eoa); if (attVal == SWG_ATTRIBUTE) { TextAttr* a = new TextAttr; streamRead(mDocStream, *a, eoa); UT_DEBUGMSG(("SDW: ...found text-sub-node, which=0x%x, ver=0x%x, start=%u, end=%u - data:%s len:%llu data is:", a->which, a->ver, a->start, a->end, a->data?"Yes":"No", (long long unsigned)a->dataLen)); #ifdef DEBUG hexdump(a->data, a->dataLen); putc('\n', stderr); #endif charAttributes.addItem(a); } else if (attVal == SWG_ATTRSET) { // bah, yet another loop UT_DEBUGMSG(("SDW: ...paragraph attributes found\n")); while (gsf_input_tell(mDocStream) < eoa) { // reusing attVal and attSize streamRead(mDocStream, attVal); gsf_off_t eoa2; // end of attribute readRecSize(mDocStream, attSize, &eoa2); if (attVal == SWG_ATTRIBUTE) { TextAttr a; streamRead(mDocStream, a, eoa2); if (!a.attrVal.empty()) { if (a.isPara) UT_String_setProperty(pAttrs, a.attrName, a.attrVal); else UT_String_setProperty(attrs, a.attrName, a.attrVal); } UT_DEBUGMSG(("SDW: ......found paragraph attr, which=0x%x, ver=0x%x, start=%u, end=%u (string now %s) Data:%s Len=%lld Data:", a.which, a.ver, (a.startSet?a.start:0), (a.endSet?a.end:0), attrs.c_str(), (a.data ? "Yes" : "No"), (long long)a.dataLen)); #ifdef DEBUG hexdump(a.data, a.dataLen); putc('\n', stderr); #endif } if (gsf_input_seek(mDocStream, eoa2, G_SEEK_SET)) return UT_IE_BOGUSDOCUMENT; } } else { UT_DEBUGMSG(("SDW: ...unknown attribute '%c' found (start=%" GSF_OFF_T_FORMAT " end=%" GSF_OFF_T_FORMAT ")\n", attVal, gsf_input_tell(mDocStream), eoa)); } if (gsf_input_seek(mDocStream, eoa, G_SEEK_SET)) return UT_IE_BOGUSDOCUMENT; } PP_PropertyVector attributes = { "props", pAttrs.c_str() }; // first, insert the paragraph if (!appendStrux(PTX_Block, attributes)) return UT_IE_NOMEMORY; UT_String pca(attrs); // character attributes for the whole paragraph // now insert the spans of text UT_uint32 len = textNode.length(); UT_uint32 lastInsPos = 0; for (UT_uint32 i = 1; i < len; i++) { bool doInsert = false; // whether there was an attribute change for (UT_sint32 j = 0; j < charAttributes.getItemCount(); j++) { const TextAttr* a = reinterpret_cast<const TextAttr*>(charAttributes[j]); // clear the last attribute, if set if (a->endSet && a->end == (i - 1)) { if (a->isOff) { UT_String propval = UT_String_getPropVal(pca, a->attrName); UT_String_setProperty(attrs, a->attrName, propval); } else UT_String_removeProperty(attrs, a->attrName); } // now set new attribute, if needed if (a->startSet && a->start == (i - 1)) { if (a->isPara) UT_String_setProperty(pAttrs, a->attrName, a->attrVal); else if (a->isOff) UT_String_removeProperty(attrs, a->attrName); else UT_String_setProperty(attrs, a->attrName, a->attrVal); } // insert if this is the last character, or if there was a format change if ((a->endSet && a->end == i) || (a->startSet && a->start == i)) doInsert = true; } if (doInsert || i == (len - 1)) { attributes[1] = attrs.c_str(); UT_DEBUGMSG(("SDW: Going to appendFmt with %s\n", attributes[1].c_str())); if (!appendFmt(attributes)) return UT_IE_NOMEMORY; /* leave cast alone! */ UT_DEBUGMSG(("SDW: About to insert %u-%u\n", lastInsPos, i)); size_t spanLen = i - lastInsPos; if (i == (len - 1)) spanLen++; UT_UCS4String span = textNode.substr(lastInsPos, spanLen); appendSpan(span.ucs4_str(), spanLen); lastInsPos = i; } } UT_VECTOR_PURGEALL(TextAttr*, charAttributes); break; } case SWG_JOBSETUP: { // flags are apparently unused here. no idea why they are there. gsf_off_t newpos; UT_uint8 flags; readFlagRec(mDocStream, flags, &newpos); if (gsf_input_seek(mDocStream, newpos, G_SEEK_SET)) return UT_IE_BOGUSDOCUMENT; UT_uint16 len, system; streamRead(mDocStream, len); streamRead(mDocStream, system); char printerName[64]; streamRead(mDocStream, printerName, 64); char deviceName[32], portName[32], driverName[32]; streamRead(mDocStream, deviceName, 32); streamRead(mDocStream, portName, 32); streamRead(mDocStream, driverName, 32); UT_DEBUGMSG(("SDW: Jobsetup: len %u sys 0x%x printer |%.64s| device |%.32s| port |%.32s| driver |%.32s|\n", len, system, printerName, deviceName, portName, driverName)); if (system == JOBSET_FILE364_SYSTEM || system == JOBSET_FILE605_SYSTEM) { UT_uint16 len2, system2; streamRead(mDocStream, len2); streamRead(mDocStream, system2); UT_uint32 ddl; // driver data length streamRead(mDocStream, ddl); // now the interesting data UT_uint16 orient; // 0=portrait 1=landscape streamRead(mDocStream, orient); UT_uint16 paperBin; streamRead(mDocStream, paperBin); UT_uint16 paperFormat; streamRead(mDocStream, paperFormat); UT_uint32 width, height; streamRead(mDocStream, width); streamRead(mDocStream, height); UT_DEBUGMSG(("SDW: orient %u bin %u format %u width %u height %u\n", orient, paperBin, paperFormat, width, height)); // rest of the data is ignored, seems to be printer specific anyway. // Use A4, Portrait by default PP_PropertyVector attributes = { "pagetype", "a4", // A4/Letter/... "orientation", "portrait", "width", "210", "height", "297", "units", "mm" }; const char* sdwPaperToAbi[] = { "A3", "A4", "A5", "B4", "B5", "Letter", "Legal", "Tabloid/Ledger", "Custom" }; if (paperFormat < sizeof(sdwPaperToAbi)/sizeof(*sdwPaperToAbi)) { attributes[1] = sdwPaperToAbi[paperFormat]; } const char* sdwOrientToAbi[] = { "portrait", "landscape" }; if (orient < sizeof(sdwOrientToAbi)/sizeof(*sdwOrientToAbi)) { attributes[3] = sdwOrientToAbi[orient]; } attributes[5] = UT_std_string_sprintf("%f", static_cast<double>(width)/100); attributes[7] = UT_std_string_sprintf("%f", static_cast<double>(height)/100); getDoc()->setPageSizeFromFile(attributes); } break; } case SWG_EOF: done2 = true; break; default: UT_DEBUGMSG(("SDW: SWG_CONTENT: Skipping %u bytes for record type '%c' (starting at 0x%08llX)\n", size2, type, (long long)gsf_input_tell(mDocStream))); } if (gsf_input_seek(mDocStream, eor2, G_SEEK_SET)) return UT_IE_BOGUSDOCUMENT; } break; } case SWG_STRINGPOOL: { if (mDocHdr.nVersion <= SWG_POOLIDS) { UT_ASSERT_HARMLESS(UT_NOT_IMPLEMENTED); break; } UT_uint8 encoding; streamRead(mDocStream, encoding); UT_iconv_t cd = findConverter(encoding); if (!UT_iconv_isValid(cd)) throw UT_IE_IMPORTERROR; UT_uint16 count; streamRead(mDocStream, count); while (count--) { UT_uint16 id; streamRead(mDocStream, id); char* str; UT_uint16 len; ::readByteString(mDocStream, str, &len); if (id == IDX_NOCONV_FF) { UT_ASSERT_HARMLESS(UT_NOT_IMPLEMENTED); } // FIXME: find a way to not have to copy and free // the result of UT_convert_cd.... --hub UT_DEBUGMSG(("SDW: StringPool: found 0x%04x <-> %.*s\n", id, len, str)); UT_UCS4Char* convertedString = reinterpret_cast<UT_UCS4Char*>(UT_convert_cd(str, len + 1, cd, NULL, NULL)); mStringPool.insert(stringpool_map::value_type(id, convertedString)); FREEP(convertedString); delete [] str; } UT_iconv_close(cd); break; } case SWG_COMMENT: // skip over comments break; case SWG_EOF: done = true; break; default: UT_DEBUGMSG(("SDW: Skipping %u bytes for record type '%c' (starting at 0x%08llX)\n", recSize, type, (long long)gsf_input_tell(mDocStream))); } // Seek to the end of the record, in case it wasn't read completely if (gsf_input_seek(mDocStream, eor, G_SEEK_SET)) return UT_IE_BOGUSDOCUMENT; } UT_DEBUGMSG(("SDW: Done\n")); return UT_OK; } catch(UT_Error e) { UT_DEBUGMSG(("SDW: error %d\n", e)); return e; } catch(...) { UT_DEBUGMSG(("SDW: Unknown error\n")); return UT_IE_BOGUSDOCUMENT; } }
void readFlagRec(GsfInput* stream, UT_uint8& flags, gsf_off_t* newPos) throw(UT_Error) { streamRead(stream, flags); if (newPos) *newPos = gsf_input_tell(stream) + (flags & 0xF); }
void streamRead(GsfInput* aStream, TextAttr& aAttr, gsf_off_t aEoa) throw(UT_Error) { UT_uint8 flags; gsf_off_t newPos; readFlagRec(aStream, flags, &newPos); streamRead(aStream, aAttr.which); streamRead(aStream, aAttr.ver); if (flags & 0x10) { aAttr.startSet = true; streamRead(aStream, aAttr.start); } else aAttr.startSet = false; if (flags & 0x20) { aAttr.endSet = true; streamRead(aStream, aAttr.end); } else aAttr.endSet = false; if (gsf_input_seek(aStream, newPos, G_SEEK_SET)) throw UT_IE_BOGUSDOCUMENT; gsf_off_t curPos = gsf_input_tell(aStream); if (curPos != aEoa) { // there is data aAttr.dataLen = aEoa - curPos; aAttr.data = new UT_uint8[aAttr.dataLen]; streamRead(aStream, aAttr.data, aAttr.dataLen); } // LIST OF THE VALUES: http://ooo.ximian.com/lxr/source/sw/sw/inc/hintids.hxx#086 // together with http://ooo.ximian.com/lxr/source/sw/sw/source/core/sw3io/sw3fmts.cxx#172 switch (aAttr.which) { case 0x1004: // strikethrough aAttr.attrName = "text-decoration"; if (!aAttr.data || aAttr.data[0]) aAttr.attrVal = "line-through"; else aAttr.isOff = true; break; case 0x1005: { // sub-/superscript if (aAttr.dataLen < 3) break; // first byte is size of text % of normal size UT_sint16 height = GSF_LE_GET_GINT16(aAttr.data + 1); aAttr.attrName = "text-position"; if (height > 0) aAttr.attrVal = "superscript"; else if (height < 0) aAttr.attrVal = "subscript"; else aAttr.isOff = true; break; } case 0x1006: { // font family if (!aAttr.data || aAttr.dataLen < 7) // 7 = 3 byte family etc., 2 byte name length, 2 byte style length break; aAttr.attrName = "font-family"; // XXX TODO This code here assumes that the font names are in latin1 UT_uint16 fontLen = GSF_LE_GET_GUINT16(aAttr.data + 3); UT_String_sprintf(aAttr.attrVal, "%.*s", fontLen, (aAttr.data + 5)); break; } case 0x1007: // font height // structure: | height (2 byte, twips) | prop (?) (2 byte) (if version >= 2, if ver=1 1 byte) | unit (if version>=2) | // XXX we ignore "prop" and unit for now, they seem not used much aAttr.attrName = "font-size"; if (aAttr.data) aAttr.attrVal = twipsToSizeString(GSF_LE_GET_GUINT16(aAttr.data)); break; case 0x100a: // Italic aAttr.attrName = "font-style"; if (!aAttr.data || aAttr.data[0]) // if there is data, first byte must be != 0 // abiword doesn't support oblique, so always set italic aAttr.attrVal = "italic"; else aAttr.isOff = true; break; case 0x100d: // Underline aAttr.attrName = "text-decoration"; if (!aAttr.data || aAttr.data[0]) aAttr.attrVal = "underline"; else aAttr.isOff = true; break; case 0x100e: // Bold aAttr.attrName = "font-weight"; if (!aAttr.data || aAttr.data[0] >= 8) // 8=Bold. aAttr.attrVal = "bold"; else aAttr.isOff = true; break; case 0x4000: // line spacing aAttr.attrName = "line-height"; aAttr.isPara = true; // prop space (s8) | inter space (s16) | height (u16) | rule (s8) | interrule (s8) if (aAttr.data && aAttr.dataLen >= 7) { // Abiword wants it as float value, StarOffice saves as percentage (e.g. // 150 for 1.5) float proportionalLineSpace = float(aAttr.data[0])/100; // But maybe we need to use the height - stored as twips, need points // (used for "exact" and "minimum" line spacing) // XXX inter-line spacing not supported by abiword (would be rule=0x00 // interrule=0x02, value to use=inter space, unit twips) UT_String lineHeight = twipsToSizeString(GSF_LE_GET_GINT16(aAttr.data + 3)); // We'll turn the bytes at 5 and 6 into a single integer, for easier // evaluation switch (GSF_LE_GET_GUINT16(aAttr.data + 5)) { case 0x0100: // proportional aAttr.attrVal = std_size_string(proportionalLineSpace); break; case 0x0001: case 0x0002: aAttr.attrVal = lineHeight; if (aAttr.data[5] == 2) // "minimum" case aAttr.attrVal += '+'; break; default: UT_DEBUGMSG(("Unsupported linespacing: %02x %02x\n", aAttr.data[5], aAttr.data[6])); } } break; case 0x4001: // Alignment aAttr.attrName = "text-align"; aAttr.isPara = true; if (aAttr.data) { switch (aAttr.data[0]) { case 0: aAttr.attrVal = "left"; break; case 1: aAttr.attrVal = "right"; break; case 2: case 4: // BLOCKLINE!? what's BLOCKLINE? I'm guessing justify. aAttr.attrVal = "justify"; break; case 3: aAttr.attrVal = "center"; break; } } break; case 0x4005: {// Tabstops aAttr.attrName = "tabstops"; aAttr.isPara = true; // Data structure: // Count(8) | Position (in twips) (32) | Adjustment (8) | Decimal Separator (?) (8) | Fill character (8) // (total size per tab = 7) // UT_sint8 count = aAttr.data[0]; for (UT_uint32 i = 1; (i + 6) < aAttr.dataLen; i += 7) { // Abiword wants: 12.3cm/L0, where 0 indicates what to fill with UT_uint16 posInTwips = GSF_LE_GET_GUINT32(aAttr.data + i); UT_String pos = twipsToSizeString(posInTwips); aAttr.attrVal += pos; aAttr.attrVal += '/'; if (aAttr.data[i + 4] < sizeof(sTabAlignment)/sizeof(*sTabAlignment)) aAttr.attrVal += sTabAlignment[aAttr.data[i + 4]]; else aAttr.attrVal += 'L'; // fallback char fillIndex = '0'; // Fill character switch (aAttr.data[i + 6]) { case '.': fillIndex = '1'; break; case '-': fillIndex = '2'; break; case '_': fillIndex = '3'; break; case ' ': fillIndex = '0'; break; default: UT_DEBUGMSG(("Filling with '%c' is not supported\n", aAttr.data[i + 6])); } aAttr.attrVal += fillIndex; aAttr.attrVal += ','; } } break; default: UT_DEBUGMSG(("SDW: unknown attribute 0x%x, compressed %d\n", aAttr.which, lcl_sw3io__CompressWhich(aAttr.which))); } }