UT_Error IE_Imp_XML::_loadFile(GsfInput * input) { m_szFileName = gsf_input_name (input); UT_XML default_xml; UT_XML * parser = &default_xml; if (m_pParser) parser = m_pParser; parser->setListener (this); if (m_pReader) parser->setReader (m_pReader); // hack!!! size_t num_bytes = gsf_input_size(input); char * bytes = (char *)gsf_input_read(input, num_bytes, NULL); UT_Error err = parser->parse (bytes, num_bytes); if ((err != UT_OK) && (err != UT_IE_SKIPINVALID)) m_error = UT_IE_BOGUSDOCUMENT; if (m_error != UT_OK) { UT_DEBUGMSG(("Problem reading document\n")); if(m_error != UT_IE_SKIPINVALID) m_szFileName = 0; } return m_error; }
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 void clone(GsfInput * input, GsfOutput * output) { guint8 const *data; size_t len; int i; if (gsf_input_size(input) > 0) { while ((len = gsf_input_remaining(input)) > 0) { /* copy in odd sized chunks to exercise system */ if (len > 314) len = 314; if (NULL == (data = gsf_input_read(input, len, NULL))) { g_warning("error reading ?"); return; } if (!gsf_output_write(output, len, data)) { g_warning("error writing ?"); return; } } } gsf_output_close(output); g_object_unref(G_OBJECT(output)); g_object_unref(G_OBJECT(input)); }
static gsf_off_t zip_find_trailer (GsfInfileZip *zip) { static guint8 const trailer_signature[] = { 'P', 'K', 0x05, 0x06 }; gsf_off_t offset, trailer_offset, filesize; gsf_off_t maplen; guint8 const *data; filesize = gsf_input_size (zip->source); if (filesize < ZIP_TRAILER_SIZE) return -1; trailer_offset = filesize; maplen = filesize & (ZIP_BUF_SIZE - 1); if (maplen == 0) maplen = ZIP_BUF_SIZE; offset = filesize - maplen; /* offset is now BUFSIZ aligned */ while (TRUE) { guchar *p, *s; if (gsf_input_seek (zip->source, offset, G_SEEK_SET)) return -1; if ((data = gsf_input_read (zip->source, maplen, NULL)) == NULL) return -1; p = (guchar *) data; for (s = p + maplen - 1; (s >= p); s--, trailer_offset--) { if ((*s == 'P') && (p + maplen - 1 - s > ZIP_TRAILER_SIZE - 2) && !memcmp (s, trailer_signature, sizeof (trailer_signature))) { return --trailer_offset; } } /* not found in currently mapped block, so update it if * there is some room in before. The requirements are.. * (a) mappings should overlap so that trailer can cross BUFSIZ-boundary * (b) trailer cannot be farther away than 64K from fileend */ /* outer loop cond */ if (offset <= 0) return -1; /* outer loop step */ offset -= ZIP_BUF_SIZE / 2; maplen = MIN (filesize - offset, ZIP_BUF_SIZE); trailer_offset = offset + maplen; if (filesize - offset > 64 * 1024) return -1; } /*outer loop*/ return -1; }
GOImage * go_spectre_new_from_file (char const *filename, GError **error) { GOSpectre *spectre = g_object_new (GO_TYPE_SPECTRE, NULL); guint8 *data; GsfInput *input = gsf_input_stdio_new (filename, error); #ifdef GOFFICE_WITH_EPS int width, height; #endif GOImage *image; if (!input) return NULL; image = GO_IMAGE (spectre); image->data_length = gsf_input_size (input); data = g_malloc (image->data_length); if (!data || !gsf_input_read (input, image->data_length, data)) { g_object_unref (spectre); g_free (data); return NULL; } image->data = data; #ifdef GOFFICE_WITH_EPS spectre->doc = spectre_document_new (); if (spectre->doc == NULL) { g_object_unref (spectre); return NULL; } spectre_document_load (spectre->doc, filename); if (spectre_document_status (spectre->doc) != SPECTRE_STATUS_SUCCESS) { g_object_unref (spectre); return NULL; } spectre_document_get_page_size (spectre->doc, &width, &height); image->width = width; image->height = height; #else { GsfInput *input = gsf_input_memory_new (image->data, image->data_length, FALSE); GsfInputTextline *text = GSF_INPUT_TEXTLINE (gsf_input_textline_new (input)); guint8 *line; while ((line = gsf_input_textline_ascii_gets (text))) if (!strncmp (line, "%%BoundingBox: ", 15)) { unsigned x0, x1, y0, y1; if (sscanf (line + 15, "%u %u %u %u", &x0, &y0, &x1, &y1) == 4) { image->width = x1 - x0; image->height = y1 - y0; } else { image->width = 100; image->height = 100; } break; } g_object_unref (text); g_object_unref (input); } #endif return image; }
/** * gsf_vba_inflate: * @input: stream to read from * @offset: offset into it for start byte of compressed stream * @size: size of the returned array * @add_null_terminator: whenever add or not null at the end of array * * Decompresses VBA stream. * * Return value: A pointer to guint8 array **/ guint8 * gsf_vba_inflate (GsfInput *input, gsf_off_t offset, int *size, gboolean add_null_terminator) { guint8 sig; GByteArray *res; gsf_off_t length; res = g_byte_array_new(); gsf_input_read (input, 1, &sig); if (1 != sig) /* should start with 0x01 */ return NULL; offset++; length = gsf_input_size (input); while (offset < length) { GsfInput *chunk; guint16 chunk_hdr; guint8 const *tmp; tmp = gsf_input_read (input, 2, NULL); if (!tmp) break; chunk_hdr = GSF_LE_GET_GUINT16 (tmp); offset += 2; if (0xB000 == (chunk_hdr&0xF000) && (chunk_hdr&0xFFF) > 0 && (length - offset < 4094)){ if (length < offset + (chunk_hdr&0xFFF)) break; chunk = gsf_input_proxy_new_section (input, offset, (gsf_off_t) (chunk_hdr&0xFFF) + 1); offset += (chunk_hdr&0xFFF) + 1; } else { if (length < offset + 4094){ chunk = gsf_input_proxy_new_section (input, offset, length-offset); offset = length; } else { chunk = gsf_input_proxy_new_section (input, offset, 4094); offset += 4094; } } if (chunk) { GByteArray *tmpres = gsf_msole_inflate (chunk, 0); gsf_input_seek (input, offset, G_SEEK_CUR); g_byte_array_append (res, tmpres->data, tmpres->len); g_byte_array_free (tmpres, TRUE); g_object_unref (chunk); } } if (res == NULL) return NULL; if (add_null_terminator) g_byte_array_append (res, "", 1); *size = res->len; return g_byte_array_free (res, FALSE); }
UT_Confidence_t IE_ImpSniffer::recognizeContents (GsfInput * input) { char szBuf[4097] = ""; // 4096+nul ought to be enough UT_uint32 iNumbytes = UT_MIN(4096, gsf_input_size(input)); gsf_input_read(input, iNumbytes, (guint8 *)(szBuf)); szBuf[iNumbytes] = '\0'; return recognizeContents(szBuf, iNumbytes); }
UT_Error IE_Imp_OpenDocument::_loadRDFFromFile ( GsfInput* pInput, const char * pStream, RDFArguments* args ) { UT_return_val_if_fail(pInput, UT_ERROR); #ifndef WITH_REDLAND UT_UNUSED(pStream); UT_UNUSED(args); return UT_OK; #else int sz = gsf_input_size (pInput); if (sz > 0) { // I would have liked to pass 0 to input_read() and // get a shared buffer back, but doing so seems to // return a non-null terminated buffer, so we make a // smart_ptr to an array an explicitly nul-terminate it. boost::shared_array<char> data( new char[sz+1] ); data[sz] = '\0'; gsf_input_read ( pInput, sz, (guint8*)data.get() ); if( sz && !data ) { return UT_ERROR; } // Note that although the API docs say you can use NULL for base_uri // you will likely find it an error to try to call that way. librdf_uri* base_uri = librdf_new_uri( args->world, (const unsigned char*)pStream ); if( !base_uri ) { UT_DEBUGMSG(("Failed to create a base URI to parse RDF into model. stream:%s sz:%d\n", pStream, sz )); return UT_ERROR; } UT_DEBUGMSG(("_handleRDFStreams() stream:%s RDF/XML:::%s:::\n", pStream, data.get() )); if( librdf_parser_parse_string_into_model( args->parser, (const unsigned char*)data.get(), base_uri, args->model )) { UT_DEBUGMSG(("Failed to parse RDF into model. stream:%s sz:%d\n", pStream, sz )); librdf_free_uri( base_uri ); return UT_ERROR; } librdf_free_uri( base_uri ); } return UT_OK; #endif }
static GsfInput * gsf_input_textline_dup (GsfInput *src_input, G_GNUC_UNUSED GError **err) { GsfInputTextline const *src = (GsfInputTextline *)src_input; GsfInputTextline *dst = g_object_new (GSF_INPUT_TEXTLINE_TYPE, NULL); dst->source = g_object_ref (src->source); gsf_input_set_size (GSF_INPUT (dst), gsf_input_size (src_input)); return GSF_INPUT (dst); }
UT_Error IE_Imp_EPUB::readMetadata() { GsfInput* metaInf = gsf_infile_child_by_name(m_epub, "META-INF"); if (metaInf == NULL) { UT_DEBUGMSG(("Can`t open container META-INF dir\n")); return UT_ERROR; } GsfInput* meta = gsf_infile_child_by_name(GSF_INFILE(metaInf), "container.xml"); if (meta == NULL) { UT_DEBUGMSG(("Can`t open container metadata\n")); return UT_ERROR; } size_t metaSize = gsf_input_size(meta); if (metaSize == 0) { UT_DEBUGMSG(("Container metadata file is empty\n")); return UT_ERROR; } gchar* metaXml = (gchar*) gsf_input_read(meta, metaSize, NULL); std::string rootfilePath; UT_XML metaParser; ContainerListener containerListener; metaParser.setListener(&containerListener); if (metaParser.sniff(metaXml, metaSize, "container")) { UT_DEBUGMSG(("Parsing container.xml file\n")); metaParser.parse(metaXml, metaSize); } else { UT_DEBUGMSG(("Incorrect container.xml file\n")); return UT_ERROR; } m_rootfilePath = containerListener.getRootFilePath(); g_object_unref(G_OBJECT(meta)); g_object_unref(G_OBJECT(metaInf)); return UT_OK; }
UT_Error IE_Imp_Hancom::_loadFile(GsfInput * input) { mDoc = GSF_INFILE(gsf_infile_msole_new (input, NULL)); if (!mDoc) return UT_IE_BOGUSDOCUMENT; GsfInput *textStream = gsf_infile_child_by_name(mDoc, "/PrvText"); if (!textStream) return UT_IE_BOGUSDOCUMENT; size_t len = gsf_input_size(textStream); UT_DEBUGMSG(("HANCOM: Text length = %zd bytes\n", len)); unsigned char* buf = new unsigned char[len]; if (!buf) { g_object_unref (G_OBJECT (textStream)); return UT_IE_NOMEMORY; } gsf_input_read(textStream, len, buf); g_object_unref (G_OBJECT (textStream)); UT_uint32 length; UT_UCS4Char* text = reinterpret_cast<UT_UCS4Char*>(UT_convert((const char *)buf, len, "UCS-2LE", UCS_INTERNAL, NULL, &length)); delete[] buf; if (!text) return UT_IE_NOMEMORY; UT_DEBUGMSG(("HANCOM: Text successfully converted.\n")); if (!appendStrux(PTX_Section, NULL)) { FREEP(text); return UT_IE_NOMEMORY; } if (!appendStrux(PTX_Block, NULL)) { FREEP(text); return UT_IE_NOMEMORY; } if (!appendSpan(text, length/4)) { FREEP(text); return UT_IE_NOMEMORY; } FREEP(text); return UT_OK; }
static void clone (GsfInput *input, GsfOutput *output) { guint8 const *data; size_t len; int i; if (gsf_input_size (input) > 0) { while ((len = gsf_input_remaining (input)) > 0) { /* copy in odd sized chunks to exercise system */ if (len > 314) len = 314; if (NULL == (data = gsf_input_read (input, len, NULL))) { g_warning ("error reading ?"); return; } if (!gsf_output_write (output, len, data)) { g_warning ("error writing ?"); return; } } } /* See test-cp-msole.c for explanation how to distinct directories * from regular files. */ if (GSF_IS_INFILE (input) && gsf_infile_num_children (GSF_INFILE (input)) > 0) { GsfInfile *in = GSF_INFILE (input); GsfOutfile *out = GSF_OUTFILE (output); GsfInput *src; GsfOutput *dst; gboolean is_dir; for (i = 0 ; i < gsf_infile_num_children (in) ; i++) { src = gsf_infile_child_by_index (in, i); is_dir = GSF_IS_INFILE (src) && gsf_infile_num_children (GSF_INFILE (src)) >= 0; dst = gsf_outfile_new_child (out, gsf_infile_name_by_index (in, i), is_dir); clone (src, dst); } } gsf_output_close (output); g_object_unref (G_OBJECT (output)); g_object_unref (G_OBJECT (input)); }
bool UT_ByteBuf::insertFromInput(UT_uint32 iPosition, GsfInput * fp) { UT_return_val_if_fail (fp != NULL, false); UT_uint32 iLengthOfFile = gsf_input_size(fp); // create a lot of space initialized to 0s this->ins (iPosition, iLengthOfFile); UT_Byte *pBuf = m_pBuf + iPosition; gsf_input_read(fp, iLengthOfFile, pBuf); return true; }
/** * gsf_input_textline_new: * @source: in some combination of ascii and utf8 * * <note>This adds a reference to @source.</note> * * Returns: a new file or %NULL. **/ GsfInput * gsf_input_textline_new (GsfInput *source) { GsfInputTextline *input; g_return_val_if_fail (source != NULL, NULL); input = g_object_new (GSF_INPUT_TEXTLINE_TYPE, NULL); input->source = g_object_ref (source); input->buf = NULL; input->buf_size = 0; gsf_input_set_size (GSF_INPUT (input), gsf_input_size (source)); gsf_input_set_name (GSF_INPUT (input), gsf_input_name (source)); return GSF_INPUT (input); }
static void ls_R (GsfInput *input, char const *prefix) { char const *name = gsf_input_name (input); GsfInfile *infile = GSF_IS_INFILE (input) ? GSF_INFILE (input) : NULL; gboolean is_dir = infile && gsf_infile_num_children (infile) > 0; char *full_name; char *new_prefix; if (prefix) { char *display_name = name ? g_filename_display_name (name) : g_strdup ("?"); full_name = g_strconcat (prefix, display_name, NULL); new_prefix = g_strconcat (full_name, "/", NULL); g_free (display_name); } else { full_name = g_strdup ("*root*"); new_prefix = g_strdup (""); } g_print ("%c %10" GSF_OFF_T_FORMAT " %s\n", (is_dir ? 'd' : 'f'), gsf_input_size (input), full_name); if (is_dir) { int i; for (i = 0 ; i < gsf_infile_num_children (infile) ; i++) { GsfInput *child = gsf_infile_child_by_index (infile, i); /* We can get NULL here in case of file corruption. */ if (child) { ls_R (child, new_prefix); g_object_unref (child); } } } g_free (full_name); g_free (new_prefix); }
UT_Error IE_Imp_EPUB::readPackage() { gchar **aname = g_strsplit(m_rootfilePath.c_str(), G_DIR_SEPARATOR_S, 0); GsfInput* opf = gsf_infile_child_by_aname(m_epub, (const char**) aname); UT_DEBUGMSG(("Getting parent\n")); GsfInfile* opfParent = gsf_input_container(opf); m_opsDir = std::string(gsf_input_name(GSF_INPUT(opfParent))); UT_DEBUGMSG(("OPS dir: %s\n", m_opsDir.c_str())); if (opf == NULL) { UT_DEBUGMSG(("Can`t open .opf file\n")); return UT_ERROR; } size_t opfSize = gsf_input_size(opf); gchar* opfXml = (gchar*) gsf_input_read(opf, opfSize, NULL); UT_XML opfParser; OpfListener opfListener; opfParser.setListener(&opfListener); if (opfParser.sniff(opfXml, opfSize, "package")) { UT_DEBUGMSG(("Parsing opf file\n")); opfParser.parse(opfXml, opfSize); } else { UT_DEBUGMSG(("Incorrect opf file found \n")); return UT_ERROR; } g_strfreev(aname); g_object_unref(G_OBJECT(opf)); //g_object_unref(G_OBJECT(opfParent)); m_spine = opfListener.getSpine(); m_manifestItems = opfListener.getManifestItems(); return UT_OK; }
UT_Error OXMLi_PackageManager::_parseStream( GsfInput * stream, OXMLi_StreamListener * pListener) { UT_return_val_if_fail(stream != NULL && pListener != NULL , UT_ERROR); //First, we check if this stream has already been parsed before std::string part_name = gsf_input_name(stream); //TODO: determine if part names are truly unique std::map<std::string, bool>::iterator it; it = m_parsedParts.find(part_name); if (it != m_parsedParts.end() && it->second) { //this stream has already been parsed successfully return UT_OK; } UT_Error ret = UT_OK; guint8 const *data = NULL; const char * cdata = NULL; size_t len = 0; UT_XML reader; reader.setListener(pListener); if (gsf_input_size (stream) > 0) { len = gsf_input_remaining (stream); if (len > 0) { data = gsf_input_read (stream, len, NULL); if (NULL == data) { g_object_unref (G_OBJECT (stream)); return UT_ERROR; } cdata = (const char *)data; ret = reader.parse (cdata, len); } } //There are two error codes to check here. if (ret == UT_OK && pListener->getStatus() == UT_OK) m_parsedParts[part_name] = true; //We prioritize the one from UT_XML when returning. return ret == UT_OK ? pListener->getStatus() : ret; }
/** * Recognize the contents as best we can * */ UT_Confidence_t IE_Imp_OpenDocument_Sniffer::recognizeContents (GsfInput * input) { UT_Confidence_t confidence = UT_CONFIDENCE_ZILCH; GsfInfile * zip = gsf_infile_zip_new (input, NULL); if (zip != NULL) { GsfInput* pInput = gsf_infile_child_by_name(zip, "mimetype"); if (pInput) { std::string mimetype; gsf_off_t size = gsf_input_size (pInput); if (size > 0) { const char * p = (const char *)gsf_input_read(pInput, size, NULL); if(p) { mimetype.assign(p, size); } } if ((mimetype == "application/vnd.oasis.opendocument.text") || (mimetype == "application/vnd.oasis.opendocument.text-template") || (mimetype == "application/vnd.oasis.opendocument.text-web")) { confidence = UT_CONFIDENCE_PERFECT; } g_object_unref (G_OBJECT (pInput)); } else { // we need to figure out what to do without mimetype file. pInput = gsf_infile_child_by_name(zip, "content.xml"); if(pInput) { // we need to identify further to get a better confidence. confidence = UT_CONFIDENCE_SOSO; } g_object_unref (G_OBJECT (pInput)); } g_object_unref (G_OBJECT (zip)); } return confidence; }
static psiconv_buffer psiconv_stream_to_buffer (GsfInput *input, int maxlen) { psiconv_buffer buf; gsf_off_t size; int len; if (!input) return NULL; if ((buf = psiconv_buffer_new()) == NULL) return NULL; if (gsf_input_seek (input, 0, G_SEEK_SET) == TRUE) { psiconv_buffer_free(buf); return NULL; } size = gsf_input_size (input); if (maxlen > 0 && size > maxlen) size = maxlen; for (; size > 0 ; size -= len) { guint8 const *chunk; int i; len = MIN (4096, size); chunk = gsf_input_read (input, len, NULL); if (chunk == NULL) break; for (i = 0; i<len; i++) { if (psiconv_buffer_add(buf, chunk[i]) != 0) { psiconv_buffer_free(buf); return NULL; } } } return buf; }
/* Quick and dirty html probe. */ gboolean html_file_probe (G_GNUC_UNUSED GOFileOpener const *fo, GsfInput *input, G_GNUC_UNUSED GOFileProbeLevel pl) { gsf_off_t size = 200; guint8 const* buf = gsf_input_read (input, size, NULL); gchar *ulstr = NULL; GString *ustr; gboolean res = FALSE; /* Avoid seeking in large streams - try to read, fall back if * stream is too short. (Actually, currently _size does not * involve any syscalls -- MW). */ if (!buf) { size = gsf_input_size (input); buf = gsf_input_read (input, size, NULL); if (!buf) return res; } if (go_guess_encoding (buf, size, NULL, &ustr, NULL)) { ulstr = g_utf8_strdown (ustr->str, -1); g_string_free (ustr, TRUE); } if (!ulstr) return res; res = (strstr (ulstr, "<table") != NULL || strstr (ulstr, "<html") != NULL || strstr (ulstr, "<!doctype html") != NULL); g_free (ulstr); return res; }
gint XAP_UnixDialog_FileOpenSaveAs::previewPicture (void) { UT_ASSERT (m_FC && m_preview); UT_ASSERT(XAP_App::getApp()); const XAP_StringSet * pSS = m_pApp->getStringSet(); UT_return_val_if_fail( pSS, 0 ); // attach and clear the area immediately GR_UnixCairoAllocInfo ai(m_preview); GR_CairoGraphics* pGr = (GR_CairoGraphics*) XAP_App::getApp()->newGraphics(ai); const gchar * file_name = gtk_file_chooser_get_uri (m_FC); GR_Font * fnt = pGr->findFont("Times New Roman", "normal", "", "normal", "", "12pt", pSS->getLanguageName()); pGr->setFont(fnt); UT_UTF8String str; pSS->getValueUTF8(XAP_STRING_ID_DLG_IP_No_Picture_Label, str); int answer = 0; FG_Graphic * pGraphic = 0; GR_Image *pImage = NULL; double scale_factor = 0.0; UT_sint32 scaled_width,scaled_height; UT_sint32 iImageWidth,iImageHeight; { GR_Painter painter(pGr); painter.clearArea(0, 0, pGr->tlu(m_preview->allocation.width), pGr->tlu(m_preview->allocation.height)); if (!file_name) { painter.drawChars (str.ucs4_str().ucs4_str(), 0, str.size(), pGr->tlu(12), pGr->tlu(static_cast<int>(m_preview->allocation.height / 2)) - pGr->getFontHeight(fnt)/2); goto Cleanup; } // are we dealing with a file or directory here? struct stat st; if (!stat (file_name, &st)) { if (!S_ISREG(st.st_mode)) { painter.drawChars (str.ucs4_str().ucs4_str(), 0, str.size(), pGr->tlu(12), pGr->tlu(static_cast<int>(m_preview->allocation.height / 2)) - pGr->getFontHeight(fnt)/2); goto Cleanup; } } GsfInput * input = NULL; UT_DEBUGMSG(("file_name %s \n",file_name)); input = UT_go_file_open (file_name, NULL); if (!input) goto Cleanup; char Buf[4097] = ""; // 4096+nul ought to be enough UT_uint32 iNumbytes = UT_MIN(4096, gsf_input_size(input)); gsf_input_read(input, iNumbytes, (guint8 *)(Buf)); Buf[iNumbytes] = '\0'; IEGraphicFileType ief = IE_ImpGraphic::fileTypeForContents(Buf,4096); if((ief == IEGFT_Unknown) || (ief == IEGFT_Bogus)) { painter.drawChars (str.ucs4_str().ucs4_str(), 0, str.size(), pGr->tlu(12), pGr->tlu(static_cast<int>(m_preview->allocation.height / 2)) - pGr->getFontHeight(fnt)/2); g_object_unref (G_OBJECT (input)); goto Cleanup; } g_object_unref (G_OBJECT (input)); input = UT_go_file_open (file_name, NULL); size_t num_bytes = gsf_input_size(input); UT_Byte * bytes = (UT_Byte *) gsf_input_read(input, num_bytes,NULL ); if(bytes == NULL) { painter.drawChars (str.ucs4_str().ucs4_str(), 0, str.size(), pGr->tlu(12), pGr->tlu(static_cast<int>(m_preview->allocation.height / 2)) - pGr->getFontHeight(fnt)/2); g_object_unref (G_OBJECT (input)); goto Cleanup; } UT_ByteBuf * pBB = new UT_ByteBuf(); pBB->append(bytes,num_bytes); g_object_unref (G_OBJECT (input)); // // OK load the data into a GdkPixbuf // bool bLoadFailed = false; // GdkPixbuf * pixbuf = pixbufForByteBuf ( pBB); delete pBB; if(pixbuf == NULL) { // // Try a fallback loader here. // painter.drawChars (str.ucs4_str().ucs4_str(), 0, str.size(), pGr->tlu(12), pGr->tlu(static_cast<int>(m_preview->allocation.height / 2)) - pGr->getFontHeight(fnt)/2); bLoadFailed = true; goto Cleanup; } pImage = new GR_UnixImage(NULL,pixbuf); iImageWidth = gdk_pixbuf_get_width (pixbuf); iImageHeight = gdk_pixbuf_get_height (pixbuf); if (m_preview->allocation.width >= iImageWidth && m_preview->allocation.height >= iImageHeight) scale_factor = 1.0; else scale_factor = MIN( static_cast<double>(m_preview->allocation.width)/iImageWidth, static_cast<double>(m_preview->allocation.height)/iImageHeight); scaled_width = static_cast<int>(scale_factor * iImageWidth); scaled_height = static_cast<int>(scale_factor * iImageHeight); static_cast<GR_UnixImage *>(pImage)->scale(scaled_width,scaled_height); painter.drawImage(pImage, pGr->tlu(static_cast<int>((m_preview->allocation.width - scaled_width ) / 2)), pGr->tlu(static_cast<int>((m_preview->allocation.height - scaled_height) / 2))); answer = 1; } Cleanup: FREEP(file_name); DELETEP(pImage); DELETEP(pGr); DELETEP(pGraphic); return answer; }
static void clone_ (GsfInfile *in, GsfOutfile *out) { GsfInput *input = GSF_INPUT (in); GsfOutput *output = GSF_OUTPUT (out); if (gsf_input_size (input) > 0) { size_t len; while ((len = gsf_input_remaining (input)) > 0) { guint8 const *data; /* copy in odd sized chunks to exercise system */ if (len > 314) len = 314; if (NULL == (data = gsf_input_read (input, len, NULL))) { g_warning ("error reading ?"); break; } if (!gsf_output_write (output, len, data)) { g_warning ("error writing ?"); break; } } } else { int i, n = gsf_infile_num_children (in); for (i = 0 ; i < n; i++) { int level; gboolean is_dir; char const *name = gsf_infile_name_by_index (in, i); char *display_name = name ? g_filename_display_name (name) : NULL; input = gsf_infile_child_by_index (in, i); if (NULL == input) { g_print ("Error opening '%s, index = %d\n", display_name ? display_name : "?", i); g_free (display_name); continue; } is_dir = gsf_infile_num_children (GSF_INFILE (input)) >= 0; g_object_get (G_OBJECT (input), "compression-level", &level, NULL); g_print ("%s: size=%ld, level=%d, %s\n", display_name ? display_name : "?", (long)gsf_input_size (input), level, is_dir ? "directory" : "file"); g_free (display_name); output = gsf_outfile_new_child_full (out, name, is_dir, "compression-level", level, NULL); clone_ (GSF_INFILE (input), GSF_OUTFILE (output)); } } gsf_output_close (GSF_OUTPUT (out)); g_object_unref (G_OBJECT (out)); g_object_unref (G_OBJECT (in)); }
void AbiCollabSessionManager::loadProfile() { UT_DEBUGMSG(("AbiCollabSessionManager::loadProfile()\n")); gchar *s = g_build_filename(XAP_App::getApp()->getUserPrivateDirectory(), "AbiCollab.Profile", (void*)0); UT_UTF8String profile(s); FREEP(s); GsfInput* in = NULL; char *uri = UT_go_filename_to_uri(profile.utf8_str()); UT_return_if_fail(uri); in = UT_go_file_open(uri, NULL); // TODO: shouldn't use NULL here, but check for errors FREEP(uri); if (!in) return; guint8 const* contents = gsf_input_read(in, gsf_input_size(in), NULL); if (contents) { xmlDocPtr reader = xmlReadMemory(reinterpret_cast<const char*>(contents), strlen(reinterpret_cast<const char*>(contents)), 0, "UTF-8", 0); if (reader) { xmlNode* node = xmlDocGetRootElement(reader); if (node) { if (strcmp(reinterpret_cast<const char*>(node->name), "AbiCollabProfile") == 0) { for (xmlNode* accountNode = node->children; accountNode; accountNode = accountNode->next) { // TODO: check if this node is really an AccountHandler node // find the account handler belonging to this type xmlChar* prop = xmlGetProp(accountNode, BAD_CAST "type"); UT_UTF8String handlerType = reinterpret_cast<char *>(prop); xmlFree(prop); std::map<UT_UTF8String, AccountHandlerConstructor>::iterator handler_iter = m_regAccountHandlers.find(handlerType); if (handler_iter == m_regAccountHandlers.end()) continue; // could happen for example when the sugar backend is found in the profile, which does not have a registered account handler belowing to it for now AccountHandlerConstructor constructor = handler_iter->second; AccountHandler* pHandler = constructor(); UT_continue_if_fail(pHandler); for (xmlNode* accountProp = accountNode->children; accountProp; accountProp = accountProp->next) { if (accountProp->type == XML_ELEMENT_NODE) { // some node names are pre-defined... if (strcmp(reinterpret_cast<const char*>(accountProp->name), "buddies") == 0) { for (xmlNode* buddyNode = accountProp->children; buddyNode; buddyNode = buddyNode->next) { if (buddyNode->type != XML_ELEMENT_NODE) continue; UT_continue_if_fail(strcmp(reinterpret_cast<const char*>(buddyNode->name), "buddy") == 0); UT_continue_if_fail(buddyNode->children); // read all buddy properties PropertyMap vBuddyProps; for (xmlNode* buddyPropertyNode = buddyNode->children; buddyPropertyNode; buddyPropertyNode = buddyPropertyNode->next) { UT_continue_if_fail(buddyPropertyNode->type == XML_ELEMENT_NODE); UT_UTF8String buddyPropValue = reinterpret_cast<const char*>(xmlNodeGetContent(buddyPropertyNode)); UT_continue_if_fail(buddyPropertyNode->name && *buddyPropertyNode->name && buddyPropValue.size() > 0); vBuddyProps.insert(PropertyMap::value_type( reinterpret_cast<const char*>(buddyPropertyNode->name), buddyPropValue.utf8_str()) ); } // construct the buddy BuddyPtr pBuddy = pHandler->constructBuddy(vBuddyProps); if (pBuddy) { // add the buddy to the account handler pHandler->addBuddy(pBuddy); } } } else { // ... the rest are generic properties UT_UTF8String propValue = reinterpret_cast<const char*>(xmlNodeGetContent(accountProp)); pHandler->addProperty(reinterpret_cast<const char*>(accountProp->name), propValue.utf8_str()); } } } // add the account to the account list if it is not a duplicate if (addAccount(pHandler)) { if (pHandler->autoConnect()) pHandler->connect(); } else { _deleteAccount(pHandler); } } } } xmlFreeDoc(reader); } } g_object_unref(G_OBJECT(in)); }
static gboolean check_header (GsfInputGZip *input) { if (input->raw) { input->header_size = 0; input->trailer_size = 0; } else { static guint8 const signature[2] = {0x1f, 0x8b}; guint8 const *data; unsigned flags, len; /* Check signature */ if (NULL == (data = gsf_input_read (input->source, 2 + 1 + 1 + 6, NULL)) || 0 != memcmp (data, signature, sizeof (signature))) return TRUE; /* verify flags and compression type */ flags = data[3]; if (data[2] != Z_DEFLATED || (flags & ~GZIP_HEADER_FLAGS) != 0) return TRUE; /* If we have the size, don't bother seeking to the end. */ if (input->uncompressed_size < 0) { /* Get the uncompressed size */ if (gsf_input_seek (input->source, (gsf_off_t) -4, G_SEEK_END) || NULL == (data = gsf_input_read (input->source, 4, NULL))) return TRUE; /* FIXME, but how? The size read here is modulo 2^32. */ input->uncompressed_size = GSF_LE_GET_GUINT32 (data); if (input->uncompressed_size / 1000 > gsf_input_size (input->source)) { g_warning ("Suspiciously well compressed file with better than 1000:1 ratio.\n" "It is probably truncated or corrupt"); } } if (gsf_input_seek (input->source, 2 + 1 + 1 + 6, G_SEEK_SET)) return TRUE; if (flags & GZIP_EXTRA_FIELD) { if (NULL == (data = gsf_input_read (input->source, 2, NULL))) return TRUE; len = GSF_LE_GET_GUINT16 (data); if (NULL == gsf_input_read (input->source, len, NULL)) return TRUE; } if (flags & GZIP_ORIGINAL_NAME) { /* Skip over the filename (which is in ISO 8859-1 encoding). */ do { if (NULL == (data = gsf_input_read (input->source, 1, NULL))) return TRUE; } while (*data != 0); } if (flags & GZIP_HAS_COMMENT) { /* Skip over the comment (which is in ISO 8859-1 encoding). */ do { if (NULL == (data = gsf_input_read (input->source, 1, NULL))) return TRUE; } while (*data != 0); } if (flags & GZIP_HEADER_CRC && NULL == (data = gsf_input_read (input->source, 2, NULL))) return TRUE; input->header_size = input->source->cur_offset; /* the last 8 bytes are the crc and size. */ input->trailer_size = 8; } gsf_input_set_size (GSF_INPUT (input), input->uncompressed_size); if (gsf_input_remaining (input->source) < input->trailer_size) return TRUE; /* No room for payload */ return FALSE; }
static int test (unsigned argc, char *argv[]) { static char const * const stream_names[] = { "Workbook", "WORKBOOK", "workbook", "Book", "BOOK", "book" }; GsfInput *input, *stream, *pcache_dir; GsfInfile *infile; GError *err = NULL; unsigned i, j; for (i = 1 ; i < argc ; i++) { fprintf( stderr, "%s\n",argv[i]); input = gsf_input_mmap_new (argv[i], NULL); if (input == NULL) /* Only report error if stdio fails too */ input = gsf_input_stdio_new (argv[i], &err); if (input == NULL) { g_return_val_if_fail (err != NULL, 1); g_warning ("'%s' error: %s", argv[i], err->message); g_error_free (err); err = NULL; continue; } input = gsf_input_uncompress (input); infile = gsf_infile_msole_new (input, &err); if (infile == NULL) { g_return_val_if_fail (err != NULL, 1); g_warning ("'%s' Not an OLE file: %s", argv[i], err->message); g_error_free (err); err = NULL; #ifdef DUMP_CONTENT dump_biff_stream (input); #endif g_object_unref (G_OBJECT (input)); continue; } #if 0 stream = gsf_infile_child_by_name (infile, "\01CompObj"); if (stream != NULL) { gsf_off_t len = gsf_input_size (stream); guint8 const *data = gsf_input_read (stream, len, NULL); if (data != NULL) gsf_mem_dump (data, len); g_object_unref (G_OBJECT (stream)); } return 0; #endif stream = gsf_infile_child_by_name (infile, "\05SummaryInformation"); if (stream != NULL) { GsfDocMetaData *meta_data = gsf_doc_meta_data_new (); puts ( "SummaryInfo"); err = gsf_doc_meta_data_read_from_msole (meta_data, stream); if (err != NULL) { g_warning ("'%s' error: %s", argv[i], err->message); g_error_free (err); err = NULL; } else gsf_doc_meta_dump (meta_data); g_object_unref (meta_data); g_object_unref (G_OBJECT (stream)); } stream = gsf_infile_child_by_name (infile, "\05DocumentSummaryInformation"); if (stream != NULL) { GsfDocMetaData *meta_data = gsf_doc_meta_data_new (); puts ( "DocSummaryInfo"); err = gsf_doc_meta_data_read_from_msole (meta_data, stream); if (err != NULL) { g_warning ("'%s' error: %s", argv[i], err->message); g_error_free (err); err = NULL; } else gsf_doc_meta_dump (meta_data); g_object_unref (meta_data); g_object_unref (G_OBJECT (stream)); } for (j = 0 ; j < G_N_ELEMENTS (stream_names) ; j++) { stream = gsf_infile_child_by_name (infile, stream_names[j]); if (stream != NULL) { puts (j < 3 ? "Excel97" : "Excel95"); #ifdef DUMP_CONTENT dump_biff_stream (stream); #endif g_object_unref (G_OBJECT (stream)); break; } } #ifdef DUMP_CONTENT pcache_dir = gsf_infile_child_by_name (infile, "_SX_DB_CUR"); /* Excel 97 */ if (NULL == pcache_dir) pcache_dir = gsf_infile_child_by_name (infile, "_SX_DB"); /* Excel 95 */ if (NULL != pcache_dir) { int i, n = gsf_infile_num_children (infile); for (i = 0 ; i < n ; i++) { stream = gsf_infile_child_by_index (GSF_INFILE (pcache_dir), i); if (stream != NULL) { printf ("=================================================\nPivot cache '%04hX'\n\n", i); dump_biff_stream (stream); g_object_unref (G_OBJECT (stream)); } } g_object_unref (G_OBJECT (pcache_dir)); } #endif g_object_unref (G_OBJECT (infile)); g_object_unref (G_OBJECT (input)); } return 0; }
/*! * This method copies the selection defined by pDocRange to ODT format * placed in the ByteBuf bufODT */ UT_Error IE_Exp_OpenDocument::copyToBuffer(PD_DocumentRange * pDocRange,UT_ByteBuf * bufODT) { // // First export selected range to a tempory document // PD_Document * outDoc = new PD_Document(); outDoc->createRawDocument(); IE_Exp_DocRangeListener * pRangeListener = new IE_Exp_DocRangeListener(pDocRange,outDoc); UT_DEBUGMSG(("DocumentRange low %d High %d \n",pDocRange->m_pos1,pDocRange->m_pos2)); PL_ListenerCoupleCloser* pCloser = new PL_ListenerCoupleCloser(); pDocRange->m_pDoc->tellListenerSubset(pRangeListener,pDocRange,pCloser); if( pCloser) delete pCloser; // // Grab the RDF triples while we are copying... // if( PD_DocumentRDFHandle outrdf = outDoc->getDocumentRDF() ) { std::set< std::string > xmlids; PD_DocumentRDFHandle inrdf = pDocRange->m_pDoc->getDocumentRDF(); inrdf->addRelevantIDsForRange( xmlids, pDocRange ); if( !xmlids.empty() ) { UT_DEBUGMSG(("MIQ: ODF export creating restricted RDF model xmlids.sz:%ld \n",(long)xmlids.size())); PD_RDFModelHandle subm = inrdf->createRestrictedModelForXMLIDs( xmlids ); PD_DocumentRDFMutationHandle m = outrdf->createMutation(); m->add( subm ); m->commit(); subm->dumpModel("copied rdf triples subm"); outrdf->dumpModel("copied rdf triples result"); } // PD_DocumentRDFMutationHandle m = outrdf->createMutation(); // m->add( PD_URI("http://www.example.com/foo"), // PD_URI("http://www.example.com/bar"), // PD_Literal("copyToBuffer path") ); // m->commit(); } outDoc->finishRawCreation(); // // OK now we have a complete and valid document containing our selected // content. We export this to an in memory GSF buffer // IE_Exp * pNewExp = NULL; char *szTempFileName = NULL; GError *err = NULL; g_file_open_tmp ("XXXXXX", &szTempFileName, &err); GsfOutput * outBuf = gsf_output_stdio_new (szTempFileName,&err); IEFileType ftODT = IE_Exp::fileTypeForMimetype("application/vnd.oasis.opendocument.text"); UT_Error aerr = IE_Exp::constructExporter(outDoc,outBuf, ftODT,&pNewExp); if(pNewExp == NULL) { return aerr; } aerr = pNewExp->writeFile(szTempFileName); if(aerr != UT_OK) { delete pNewExp; delete pRangeListener; UNREFP( outDoc); g_remove(szTempFileName); g_free (szTempFileName); return aerr; } // // File is closed at the end of the export. Open it again. // GsfInput * fData = gsf_input_stdio_new(szTempFileName,&err); UT_DebugOnly<UT_sint32> siz = gsf_input_size(fData); const UT_Byte * pData = gsf_input_read(fData,gsf_input_size(fData),NULL); UT_DEBUGMSG(("Writing %d bytes to clipboard \n", (UT_sint32)siz)); bufODT->append( pData, gsf_input_size(fData)); delete pNewExp; delete pRangeListener; UNREFP( outDoc); g_remove(szTempFileName); g_free (szTempFileName); return aerr; }
UT_Error ODc_Crypto::performDecrypt(GsfInput* pStream, unsigned char* salt, UT_uint32 salt_length, UT_uint32 iter_count, unsigned char* ivec, const std::string& password, UT_uint32 decrypted_size, GsfInput** pDecryptedInput) { unsigned char sha1_password[PASSWORD_HASH_LEN]; char key[PBKDF2_KEYLEN]; // get the sha1 sum of the password sha1_buffer(&password[0], password.size(), sha1_password); // create a PBKDF2 key from the sha1 sum int k = pbkdf2_sha1 ((const char*)sha1_password, PASSWORD_HASH_LEN, (const char*)salt, salt_length, iter_count, key, PBKDF2_KEYLEN); if (k != 0) return UT_ERROR; // create the decryption key BF_KEY bf_key; BF_set_key(&bf_key, PBKDF2_KEYLEN, (const unsigned char*)key); // perform the actual decryption UT_sint32 content_size = gsf_input_size(pStream); if (content_size == -1) return UT_ERROR; const unsigned char* content = gsf_input_read(pStream, content_size, NULL); if (!content) return UT_ERROR; int num = 0; unsigned char* content_decrypted = (unsigned char*)g_malloc(content_size); BF_cfb64_encrypt(content, content_decrypted, content_size, &bf_key, ivec, &num, BF_DECRYPT); // deflate the decrypted content z_stream zs; zs.zalloc = Z_NULL; zs.zfree = Z_NULL; zs.opaque = Z_NULL; zs.avail_in = 0; zs.next_in = Z_NULL; int err; err = inflateInit2(&zs, -MAX_WBITS); if (err != Z_OK) return UT_ERROR; unsigned char* decrypted = (unsigned char*)g_malloc(decrypted_size); zs.avail_in = content_size; zs.avail_out = decrypted_size; zs.next_in = content_decrypted; zs.next_out = decrypted; err = inflate(&zs, Z_FINISH); FREEP(content_decrypted); if (err != Z_STREAM_END) { inflateEnd(&zs); FREEP(decrypted); return UT_ERROR; } inflateEnd(&zs); *pDecryptedInput = gsf_input_memory_new(decrypted, decrypted_size, TRUE); return UT_OK; }
void html_file_open (G_GNUC_UNUSED GOFileOpener const *fo, GOIOContext *io_context, WorkbookView *wb_view, GsfInput *input) { guint8 const *buf; gsf_off_t size; int len, bomlen; htmlParserCtxtPtr ctxt; htmlDocPtr doc = NULL; xmlCharEncoding enc; GnmHtmlTableCtxt tc; g_return_if_fail (input != NULL); if (gsf_input_seek (input, 0, G_SEEK_SET)) return; size = gsf_input_size (input); if (size >= 4) { size -= 4; buf = gsf_input_read (input, 4, NULL); if (buf != NULL) { enc = xmlDetectCharEncoding(buf, 4); switch (enc) { /* Skip byte order mark */ case XML_CHAR_ENCODING_UCS4BE: case XML_CHAR_ENCODING_UCS4LE: case XML_CHAR_ENCODING_UCS4_2143: case XML_CHAR_ENCODING_UCS4_3412: case XML_CHAR_ENCODING_EBCDIC: bomlen = 4; break; case XML_CHAR_ENCODING_UTF16BE: case XML_CHAR_ENCODING_UTF16LE: bomlen = 2; break; case XML_CHAR_ENCODING_UTF8: if (buf[0] == 0xef) bomlen = 3; else if (buf[0] == 0x3c) bomlen = 4; else bomlen = 0; break; case XML_CHAR_ENCODING_NONE: bomlen = 0; /* Try to detect unmarked UTF16LE (Firefox Windows clipboard, drag data all platforms) */ if ((buf[0] >= 0x20 || g_ascii_isspace(buf[0])) && buf[1] == 0 && (buf[2] >= 0x20 || g_ascii_isspace(buf[2])) && buf[3] == 0) enc = XML_CHAR_ENCODING_UTF16LE; break; default: bomlen = 0; } ctxt = htmlCreatePushParserCtxt ( NULL, NULL, (char const *)(buf + bomlen), 4 - bomlen, gsf_input_name (input), enc); for (; size > 0 ; size -= len) { len = MIN (4096, size); buf = gsf_input_read (input, len, NULL); if (buf == NULL) break; htmlParseChunk ( ctxt, (char const *)buf, len, 0); } htmlParseChunk (ctxt, (char const *)buf, 0, 1); doc = ctxt->myDoc; htmlFreeParserCtxt (ctxt); } } if (doc != NULL) { xmlNodePtr ptr; tc.sheet = NULL; tc.row = -1; tc.wb_view = wb_view; for (ptr = doc->children; ptr != NULL ; ptr = ptr->next) html_search_for_tables (ptr, doc, wb_view, &tc); xmlFreeDoc (doc); } else go_io_error_info_set (io_context, go_error_info_new_str (_("Unable to parse the html."))); }
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; } }
/** * tracker_gsf_parse_xml_in_zip: * @zip_file_uri: URI of the ZIP archive * @xml_filename: Name of the XML file stored inside the ZIP archive * @context: Markup context to be used when parsing the XML * * This function reads and parses the contents of an XML file stored * inside a ZIP compressed archive. Reading and parsing is done buffered, and * maximum size of the uncompressed XML file is limited to be to 20MBytes. */ void tracker_gsf_parse_xml_in_zip (const gchar *zip_file_uri, const gchar *xml_filename, GMarkupParseContext *context, GError **err) { gchar *filename; GError *error = NULL; GsfInfile *infile = NULL; GsfInput *src = NULL; GsfInput *member = NULL; FILE *file; g_debug ("Parsing '%s' XML file contained inside zip archive...", xml_filename); /* Get filename from the given URI */ if ((filename = g_filename_from_uri (zip_file_uri, NULL, &error)) == NULL) { g_warning ("Can't get filename from uri '%s': %s", zip_file_uri, error ? error->message : "no error given"); } else { /* Create a new Input GSF object for the given file */ file = tracker_file_open (filename); if (!file) { g_warning ("Can't open file from uri '%s': %s", zip_file_uri, g_strerror (errno)); } else if ((src = gsf_input_stdio_new_FILE (filename, file, TRUE)) == NULL) { g_warning ("Failed creating a GSF Input object for '%s': %s", zip_file_uri, error ? error->message : "no error given"); } /* Input object is a Zip file */ else if ((infile = gsf_infile_zip_new (src, &error)) == NULL) { g_warning ("'%s' Not a zip file: %s", zip_file_uri, error ? error->message : "no error given"); } /* Look for requested filename inside the ZIP file */ else if ((member = find_member (infile, xml_filename)) == NULL) { g_warning ("No member '%s' in zip file '%s'", xml_filename, zip_file_uri); } /* Load whole contents of the internal file in the xml buffer */ else { guint8 buf[XML_BUFFER_SIZE]; size_t remaining_size, chunk_size, accum; /* Get whole size of the contents to read */ remaining_size = (size_t) gsf_input_size (GSF_INPUT (member)); /* Note that gsf_input_read() needs to be able to read ALL specified * number of bytes, or it will fail */ chunk_size = MIN (remaining_size, XML_BUFFER_SIZE); accum = 0; while (!error && accum <= XML_MAX_BYTES_READ && chunk_size > 0 && gsf_input_read (GSF_INPUT (member), chunk_size, buf) != NULL) { /* update accumulated count */ accum += chunk_size; /* Pass the read stream to the context parser... */ g_markup_parse_context_parse (context, buf, chunk_size, &error); /* update bytes to be read */ remaining_size -= chunk_size; chunk_size = MIN (remaining_size, XML_BUFFER_SIZE); } } if (file) { tracker_file_close (file, FALSE); } } g_free (filename); if (error) g_propagate_error (err, error); if (infile) g_object_unref (infile); if (src) g_object_unref (src); if (member) g_object_unref (member); }