UT_Error AbiCollabSessionManager::deserializeDocument(PD_Document** pDoc, const std::string& document, bool isEncodedBase64)
{
	UT_return_val_if_fail(pDoc, UT_ERROR);
	
	UT_Error res = UT_ERROR;
	
	// really bad abuse of std::string's below, but more efficient than copying 
	// the whole buffer (which could be massive) into a temporary buffer
	GsfInput *source;
	if (isEncodedBase64)
	{
		char* base64gzBuf = const_cast<char*>(document.c_str());
		size_t gzbufLen = gsf_base64_decode_simple((guint8*)base64gzBuf, strlen(base64gzBuf));
		char* gzBuf = base64gzBuf;
		source = gsf_input_memory_new((guint8*)gzBuf, gzbufLen, false);
	}
	else
	{
		// string contains raw zipped data
		source = gsf_input_memory_new((guint8*)document.c_str(), document.size(), false);
	}

	if (source)
	{
		GsfInput *gzabwBuf = gsf_input_gzip_new (source, NULL); // todo: don't pass null here, but check for errors
		if (gzabwBuf)
		{
			bool create = (*pDoc == NULL);
			if (create)
			{
				UT_DEBUGMSG(("Creating a new document in AbiCollabSessionManager::deserializeDocument()\n"));
				(*pDoc) = new PD_Document();
				(*pDoc)->createRawDocument();
			}
			IE_Imp* imp = (IE_Imp*)new IE_Imp_AbiWord_1(*pDoc);
			imp->importFile(gzabwBuf); // todo: check for errors
			(*pDoc)->repairDoc();
			if (create)
				(*pDoc)->finishRawCreation();
			DELETEP(imp);
			g_object_unref(G_OBJECT(gzabwBuf));
			res = UT_OK;
        }
		else
		{
			UT_ASSERT(UT_SHOULD_NOT_HAPPEN);
		}
		g_object_unref(G_OBJECT(source));
	}
	else
	{
		UT_ASSERT(UT_SHOULD_NOT_HAPPEN);
	}

	return res;
}
Example #2
0
bool IE_Imp_EPUB::pasteFromBuffer(PD_DocumentRange* pDocRange,
				  const unsigned char* pData, UT_uint32 lenData, const char* /*szEncoding*/)
{
    UT_return_val_if_fail(getDoc() == pDocRange->m_pDoc,false);
    UT_return_val_if_fail(pDocRange->m_pos1 == pDocRange->m_pos2,false);

    PD_Document * newDoc = new PD_Document();
    newDoc->createRawDocument();
    IE_Imp_EPUB * pEPUBImp = new IE_Imp_EPUB(newDoc);
    //
    // Turn pData into something that can be imported by the open documenb
    // importer.
    //
    GsfInput * pInStream = gsf_input_memory_new((const guint8 *) pData,
            (gsf_off_t) lenData, FALSE);
    pEPUBImp->loadFile(newDoc, pInStream);

    newDoc->finishRawCreation();

    IE_Imp_PasteListener * pPasteListen = new IE_Imp_PasteListener(getDoc(),
            pDocRange->m_pos1, newDoc);
    newDoc->tellListener(static_cast<PL_Listener *> (pPasteListen));
    delete pPasteListen;
    delete pEPUBImp;
    UNREFP( newDoc);
    return true;
}
Example #3
0
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;
}
bool IE_Imp_OpenDocument::pasteFromBuffer(PD_DocumentRange * pDocRange,
				    const unsigned char * pData, 
				    UT_uint32 lenData, 
					  const char * /*szEncoding*/)
{
    UT_return_val_if_fail(getDoc() == pDocRange->m_pDoc,false);
    UT_return_val_if_fail(pDocRange->m_pos1 == pDocRange->m_pos2,false);
	
    PD_Document * newDoc = new PD_Document();
    newDoc->createRawDocument();
    IE_Imp_OpenDocument * pODImp = new IE_Imp_OpenDocument(newDoc);
    //
    // Turn pData into something that can be imported by the open documenb
    // importer.
    //
    GsfInput * pInStream =  gsf_input_memory_new((const guint8 *) pData, 
						 (gsf_off_t) lenData,
						 FALSE);
    pODImp->loadFile(newDoc, pInStream);
    // pInStream deleted after load.
    newDoc->finishRawCreation();

    // Handle RDF for the newdoc
    {
        PD_DocumentRDFHandle rdf = newDoc->getDocumentRDF();
        rdf->dumpModel("about to broadcast...");
        PD_DocumentRDFMutationHandle m = getDoc()->getDocumentRDF()->createMutation();
        m->add( rdf );
        m->commit();
    }
    
    //
    // OK Broadcast from the just filled source document into our current
    // doc via the paste listener
    //
    IE_Imp_PasteListener * pPasteListen = new  IE_Imp_PasteListener(getDoc(),pDocRange->m_pos1,newDoc);
    newDoc->tellListener(static_cast<PL_Listener *>(pPasteListen));
    delete pPasteListen;
    delete pODImp;
    UNREFP( newDoc);
    return true;
}
Example #5
0
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;
}
Example #6
0
/**
 * go_gtk_builder_load:
 * @uifile: the name of the file load
 * @domain: the translation domain
 * @gcc: #GOCmdContext
 *
 * Simple utility to open ui files
 *
 * Since 0.9.6
 * Returns: (transfer full): a new #GtkBuilder or NULL
 *
 * @uifile should be one of these:
 *
 * res:NAME  -- data from resource manager
 * data:DATA -- data right here
 * filename  -- data from local file
 *
 * Data may be compressed, regardless of source.
**/
GtkBuilder *
go_gtk_builder_load (char const *uifile,
		     char const *domain, GOCmdContext *gcc)
{
	GtkBuilder *gui;
	GError *error = NULL;
	gboolean ok = FALSE;

	g_return_val_if_fail (uifile != NULL, NULL);

	gui = gtk_builder_new ();
	if (domain)
		gtk_builder_set_translation_domain (gui, domain);

	if (strncmp (uifile, "res:", 4) == 0) {
		size_t len;
		gconstpointer data = go_rsm_lookup (uifile + 4, &len);
		GsfInput *src = data
			? gsf_input_memory_new (data, len, FALSE)
			: NULL;
		ok = apply_ui_from_file (gui, src, NULL, &error);
	} else if (strncmp (uifile, "data:", 5) == 0) {
		const char *data = uifile + 5;
		GsfInput *src = gsf_input_memory_new (data, strlen (data), FALSE);
		ok = apply_ui_from_file (gui, src, NULL, &error);
	} else {
		/* we need to set the current directory so that the builder can find pixbufs */
		GsfInput *src = gsf_input_stdio_new (uifile, &error);
		ok = apply_ui_from_file (gui, src, uifile, &error);
	}

	if (!ok) {
		g_object_unref (gui);
		gui = NULL;
	}

	if (gui == NULL && gcc != NULL) {
		char *msg;
		if (error) {
			msg = g_strdup (error->message);
			g_error_free (error);
		} else
			msg = g_strdup_printf (_("Unable to open file '%s'"), uifile);
		go_cmd_context_error_system (gcc, msg);
		g_free (msg);
	} else if (error)
		g_error_free (error);

	if (gui && go_debug_flag ("leaks")) {
		GSList *l, *objs = gtk_builder_get_objects (gui);
		for (l = objs; l; l = l->next) {
			GObject *obj = l->data;
			/* Darn -- cannot access object name! */
			char *name = g_strdup_printf ("Anonymous from %s", uifile);
			go_debug_check_finalized (obj, name);
			g_free (name);
		}
		g_slist_free (objs);
	}

	return gui;
}
Example #7
0
static void make_stream (HwpHWP5File *file, GError **error)
{
  GsfInput  *input        = NULL;
  GsfInfile *ole          = GSF_INFILE (file->priv->olefile);
  gint       n_root_entry = gsf_infile_num_children (ole);

  if (n_root_entry < 1)
  {
    g_set_error_literal (error,
                         HWP_FILE_ERROR,
                         HWP_FILE_ERROR_INVALID,
                         "invalid hwp file");
    return;
  }

  /* 우선 순위에 따라 스트림을 만든다 */
  input = gsf_infile_child_by_name (ole, "FileHeader");
  if (input && gsf_infile_num_children (GSF_INFILE (input)) == -1)
  {
    file->file_header_stream = input;
    input = NULL;
    parse_file_header (file);
  }
  else
  {
    goto FAIL;
  }

  input = gsf_infile_child_by_name (ole, "DocInfo");
  if (input && gsf_infile_num_children (GSF_INFILE (input)) == -1)
  {
    if (file->is_compress)
    {
      GInputStream      *gis;
      GZlibDecompressor *zd;
      GInputStream      *cis;

      gis = (GInputStream *) gsf_input_stream_new (input);
      zd  = g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_RAW);
      cis = g_converter_input_stream_new (gis, (GConverter *)   zd);
      g_filter_input_stream_set_close_base_stream (G_FILTER_INPUT_STREAM (cis), TRUE);
      file->doc_info_stream = cis;

      g_object_unref (zd);
      g_object_unref (gis);

      input = NULL;
    }
    else
    {
      file->doc_info_stream = (GInputStream *) gsf_input_stream_new (input);
    }
  }
  else
  {
    goto FAIL;
  }

  if (!file->is_distribute)
    input = gsf_infile_child_by_name (ole, "BodyText");
  else
    input = gsf_infile_child_by_name (ole, "ViewText");

  if (input)
  {
    for (gint i = 0; i < gsf_infile_num_children (GSF_INFILE (input)); i++)
    {
      GsfInput *section =
                  gsf_infile_child_by_name (GSF_INFILE (input),
                                            g_strdup_printf("Section%d", i));

      if (gsf_infile_num_children (GSF_INFILE (section)) != -1)
      {
        if (GSF_IS_INPUT (section))
          g_object_unref (section);

        g_set_error_literal (error,
                             HWP_FILE_ERROR,
                             HWP_FILE_ERROR_INVALID,
                             "invalid hwp file");
        return;
      }

      if (file->is_distribute)
      {
        guint8 *data = g_malloc0 (256);
        gsf_input_read (section, 4, NULL);
        gsf_input_read (section, 256, data);
        guint32 seed = GSF_LE_GET_GUINT32 (data);
        msvc_srand (seed);
        gint n = 0, val = 0, offset;

        for (guint i = 0; i < 256; i++)
        {
          if (n == 0)
          {
            val = msvc_rand() & 0xff;
            n = (msvc_rand() & 0xf) + 1;
          }

          data[i] ^= val;

          n--;
        }

        offset = 4 + (seed & 0xf);
        gchar *key = g_malloc0 (16);
        memcpy (key, (const gchar *) data + offset, 16);
#ifdef HWP_ENABLE_DEBUG
        gchar *sha1 = g_convert ((const gchar *) data + offset, 80,
                                 "UTF-8", "UTF-16LE", NULL, NULL, error);
        printf ("sha1: %s\n", sha1);
        printf ("key: %s\n", key);
        g_free (sha1);
#endif
        g_free (data);

        EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new ();
        EVP_CIPHER_CTX_init (ctx);
        EVP_DecryptInit_ex (ctx, EVP_aes_128_ecb(), NULL,
                            (unsigned char *) key, NULL);
        g_free (key);
        EVP_CIPHER_CTX_set_padding(ctx, 0); /* no padding */

        gsf_off_t encrypted_data_len = gsf_input_remaining (section);
        guint8 const *encrypted_data = gsf_input_read (section, encrypted_data_len, NULL);

        guint8 *decrypted_data = g_malloc (encrypted_data_len);
        int decrypted_data_len, len;

        EVP_DecryptUpdate (ctx, decrypted_data, &len, encrypted_data, encrypted_data_len);
        decrypted_data_len = len;

        EVP_DecryptFinal_ex (ctx, decrypted_data + len, &len);
        decrypted_data_len += len;

        EVP_CIPHER_CTX_free (ctx);
        g_object_unref (section);

        section = gsf_input_memory_new (decrypted_data, decrypted_data_len, TRUE);
      }

      if (file->is_compress)
      {
        GInputStream      *gis;
        GZlibDecompressor *zd;
        GInputStream      *cis;

        gis = (GInputStream *) gsf_input_stream_new (section);
        zd  = g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_RAW);
        cis = g_converter_input_stream_new (gis, (GConverter *) zd);
        g_filter_input_stream_set_close_base_stream (G_FILTER_INPUT_STREAM (cis), TRUE);
        g_ptr_array_add (file->section_streams, cis);
        g_object_unref (zd);
        g_object_unref (gis);
      }
      else
      {
        GInputStream *stream = (GInputStream *) gsf_input_stream_new (section);
        g_ptr_array_add (file->section_streams, stream);
      }
    } /* for */
    g_object_unref (input);
    input = NULL;
  }
  else
  {
    goto FAIL;
  }

  input = gsf_infile_child_by_name (ole, "\005HwpSummaryInformation");
  if (input && gsf_infile_num_children (GSF_INFILE (input)) == -1)
  {
    file->summary_info_stream = input;
    input = NULL;
  }
  else
  {
    goto FAIL;
  }

  input = gsf_infile_child_by_name (ole, "BinData");

  if (input)
  {
    gint n_data = gsf_infile_num_children (GSF_INFILE (input));

    for (gint i = 0; i < n_data; i++)
    {
      GsfInput *bin_data_input =
                  gsf_infile_child_by_index (GSF_INFILE (input), i);

      if (gsf_infile_num_children (GSF_INFILE (bin_data_input)) != -1)
      {
        if (GSF_IS_INPUT (bin_data_input))
          g_object_unref (bin_data_input);

        g_set_error_literal (error,
                             HWP_FILE_ERROR,
                             HWP_FILE_ERROR_INVALID,
                             "invalid hwp file");
        return;
      }

      if (file->is_compress)
      {
        GInputStream      *gis;
        GZlibDecompressor *zd;
        GInputStream      *cis;

        gis = (GInputStream *) gsf_input_stream_new (bin_data_input);
        zd  = g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_RAW);
        cis = g_converter_input_stream_new (gis, (GConverter *) zd);
        g_filter_input_stream_set_close_base_stream (G_FILTER_INPUT_STREAM (cis), TRUE);
        g_ptr_array_add (file->bin_data_streams, cis);
        g_object_unref (zd);
        g_object_unref (gis);
      }
      else
      {
        GInputStream *stream = (GInputStream *) gsf_input_stream_new (bin_data_input);
        g_ptr_array_add (file->bin_data_streams, stream);
      }
    }

    g_object_unref (input);
    input = NULL;
  }

  input = gsf_infile_child_by_name (ole, "PrvText");
  if (input && gsf_infile_num_children (GSF_INFILE (input)) == -1)
  {
    file->prv_text_stream = input;
    input = NULL;
  }
  else
  {
    goto FAIL;
  }

  input = gsf_infile_child_by_name (ole, "PrvImage");
  if (input && gsf_infile_num_children (GSF_INFILE (input)) == -1)
  {
    file->prv_image_stream = input;
    input = NULL;
  }
  else
  {
    goto FAIL;
  }

  return;

  FAIL:

  if (GSF_IS_INPUT (input))
    g_object_unref (input);

  g_set_error_literal (error,
                       HWP_FILE_ERROR,
                       HWP_FILE_ERROR_INVALID,
                       "invalid hwp file");
  return;
}