libxlsxwriter/src/packager.c in fast_excel-0.2.3 vs libxlsxwriter/src/packager.c in fast_excel-0.2.5
- old
+ new
@@ -1,11 +1,11 @@
/*****************************************************************************
* packager - A library for creating Excel XLSX packager files.
*
* Used in conjunction with the libxlsxwriter library.
*
- * Copyright 2014-2017, John McNamara, jmcnamara@cpan.org. See LICENSE.txt.
+ * Copyright 2014-2018, John McNamara, jmcnamara@cpan.org. See LICENSE.txt.
*
*/
#include "xlsxwriter/xmlwriter.h"
#include "xlsxwriter/packager.h"
@@ -35,11 +35,16 @@
/* Silence Windows warning with duplicate symbol for SLIST_ENTRY in local
* queue.h and widows.h. */
#undef SLIST_ENTRY
#include <windows.h>
+
+#ifdef USE_SYSTEM_MINIZIP
+#include "minizip/iowin32.h"
+#else
#include "../third_party/minizip/iowin32.h"
+#endif
zipFile
_open_zipfile_win32(const char *filename)
{
int n;
@@ -55,11 +60,11 @@
LXW_ERROR("MultiByteToWideChar error");
return NULL;
}
/* Use the native Win32 file handling functions with minizip. */
- fill_win32_filefunc64(&filefunc);
+ fill_win32_filefunc64W(&filefunc);
return zipOpen2_64(wide_filename, 0, NULL, &filefunc);
}
#endif
@@ -194,10 +199,11 @@
{
lxw_workbook *workbook = self->workbook;
lxw_worksheet *worksheet;
lxw_image_options *image;
lxw_error err;
+ FILE *image_stream;
char filename[LXW_FILENAME_LENGTH] = { 0 };
uint16_t index = 1;
STAILQ_FOREACH(worksheet, workbook->worksheets, list_pointers) {
@@ -208,16 +214,23 @@
STAILQ_FOREACH(image, worksheet->image_data, list_pointers) {
lxw_snprintf(filename, LXW_FILENAME_LENGTH,
"xl/media/image%d.%s", index++, image->extension);
- rewind(image->stream);
+ /* Check that the image file exists and can be opened. */
+ image_stream = fopen(image->filename, "rb");
+ if (!image_stream) {
+ LXW_WARN_FORMAT1("Error adding image to xlsx file: file "
+ "doesn't exist or can't be opened: %s.",
+ image->filename);
+ return LXW_ERROR_CREATING_TMPFILE;
+ }
- err = _add_file_to_zip(self, image->stream, filename);
- RETURN_ON_ERROR(err);
+ err = _add_file_to_zip(self, image_stream, filename);
+ fclose(image_stream);
- fclose(image->stream);
+ RETURN_ON_ERROR(err);
}
}
return LXW_NO_ERROR;
}
@@ -932,10 +945,10 @@
error = _write_drawing_rels_file(self);
RETURN_ON_ERROR(error);
error = _write_image_files(self);
- RETURN_ON_ERROR(error);;
+ RETURN_ON_ERROR(error);
error = _write_root_rels_file(self);
RETURN_ON_ERROR(error);
zip_error = zipClose(self->zipfile, NULL);