summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRandy <randy408@protonmail.com>2020-03-10 21:34:26 +0000
committerAlbert Astals Cid <tsdgeos@yahoo.es>2020-03-10 21:34:26 +0000
commita6329a26cbc8a920cf56531a762bfbf62b3ddc9d (patch)
treebe56164498c82df74987b6b5334f67f233739282
parent567f72a04dcc5e883e708bd2a3a149e818c1c12f (diff)
Refactor spectre_document_load_from_data() -> _stream()
-rw-r--r--libspectre/spectre-document.c47
-rw-r--r--libspectre/spectre-document.h16
-rw-r--r--test/spectre_read_fuzzer.c17
3 files changed, 33 insertions, 47 deletions
diff --git a/libspectre/spectre-document.c b/libspectre/spectre-document.c
index ff3e57c..3cd9d7d 100644
--- a/libspectre/spectre-document.c
+++ b/libspectre/spectre-document.c
@@ -53,24 +53,10 @@ spectre_document_new (void)
static void
document_load (SpectreDocument *document,
const char *filename,
- void *buffer,
- size_t size)
+ FILE *file)
{
- FILE *file;
-
_spectre_return_if_fail (document != NULL);
-#if _POSIX_C_SOURCE >= 200809L
- if(buffer == NULL) {
- _spectre_return_if_fail (filename != NULL);
- } else {
- _spectre_return_if_fail (buffer != NULL);
- }
-#else
- _spectre_return_if_fail (filename != NULL);
-#endif
-
-
if (document->doc && strcmp (filename, document->doc->filename) == 0) {
document->status = SPECTRE_STATUS_SUCCESS;
return;
@@ -81,16 +67,6 @@ document_load (SpectreDocument *document,
document->doc = NULL;
}
-#if _POSIX_C_SOURCE >= 200809L
- if(buffer == NULL) {
- file = fopen (filename, "rb");
- } else {
- file = fmemopen (buffer, size, "rb");
- }
-#else
- file = fopen (filename, "rb");
-#endif
-
if (!file) {
document->status = SPECTRE_STATUS_LOAD_ERROR;
return;
@@ -99,7 +75,6 @@ document_load (SpectreDocument *document,
document->doc = psscan (file, filename, SCANSTYLE_NORMAL);
if (!document->doc) {
document->status = SPECTRE_STATUS_LOAD_ERROR;
- fclose (file);
return;
}
@@ -107,7 +82,6 @@ document_load (SpectreDocument *document,
document->status = SPECTRE_STATUS_LOAD_ERROR;
psdocdestroy (document->doc);
document->doc = NULL;
- fclose (file);
return;
} else if (document->doc->numpages == 0 && !document->doc->format) {
@@ -122,7 +96,6 @@ document_load (SpectreDocument *document,
document->status = SPECTRE_STATUS_LOAD_ERROR;
psdocdestroy (document->doc);
document->doc = NULL;
- fclose (file);
return;
}
@@ -134,25 +107,27 @@ document_load (SpectreDocument *document,
if (document->status != SPECTRE_STATUS_SUCCESS)
document->status = SPECTRE_STATUS_SUCCESS;
- fclose (file);
}
void
spectre_document_load (SpectreDocument *document,
const char *filename)
{
- document_load (document, filename, NULL, 0);
+ FILE *file;
+
+ _spectre_return_if_fail (filename != NULL);
+
+ file = fopen (filename, "rb");
+ document_load (document, filename, file);
+ fclose (file);
}
-#if _POSIX_C_SOURCE >= 200809L
void
-spectre_document_load_from_data (SpectreDocument *document,
- void *data,
- size_t size)
+spectre_document_load_from_stream (SpectreDocument *document,
+ FILE *file)
{
- document_load (document, NULL, data, size);
+ document_load (document, "stream", file);
}
-#endif
void
spectre_document_free (SpectreDocument *document)
diff --git a/libspectre/spectre-document.h b/libspectre/spectre-document.h
index c2ac135..dc10ac8 100644
--- a/libspectre/spectre-document.h
+++ b/libspectre/spectre-document.h
@@ -25,6 +25,8 @@
#include <libspectre/spectre-status.h>
#include <libspectre/spectre-page.h>
+#include <stdio.h>
+
SPECTRE_BEGIN_DECLS
/*! This is the object that represents a PostScript document. */
@@ -43,18 +45,14 @@ SPECTRE_PUBLIC
void spectre_document_load (SpectreDocument *document,
const char *filename);
-#if _POSIX_C_SOURCE >= 200809L
-/*! Loads the given buffer into the document. This function can fail
- @param document the document where the buffer will be loaded
- @param buffer the buffer to load
- @param size the size of the buffer
+/*! Loads the given open file into the document. This function can fail
+ @param document the document where the file will be loaded
+ @param file the file to load
@see spectre_document_status
*/
SPECTRE_PUBLIC
-void spectre_document_load_from_data (SpectreDocument *document,
- void *buffer,
- size_t size);
-#endif
+void spectre_document_load_from_stream (SpectreDocument *document,
+ FILE *file);
/*! Returns the document status
@param document the document whose status will be returned
diff --git a/test/spectre_read_fuzzer.c b/test/spectre_read_fuzzer.c
index a0ca3e6..4a0601a 100644
--- a/test/spectre_read_fuzzer.c
+++ b/test/spectre_read_fuzzer.c
@@ -18,20 +18,33 @@ int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size);
int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
+ FILE *f;
SpectreDocument *document;
+/* This is part of the build, it should at least compile on Windows */
+#if _POSIX_C_SOURCE >= 200809L
+ f = fmemopen((void*)data, size, "rb");
+ if(f == NULL) return 0;
+#endif
+
document = spectre_document_new();
- if(document == NULL) return 0;
+ if(document == NULL)
+ {
+ fclose(f);
+ return 0;
+ }
- spectre_document_load_from_data(document, (void*)data, size);
+ spectre_document_load_from_stream(document, f);
if(spectre_document_status(document))
{
spectre_document_free(document);
+ fclose(f);
return 0;
}
spectre_document_free(document);
+ fclose(f);
return 0;
}