summaryrefslogtreecommitdiff
path: root/libspectre
diff options
context:
space:
mode:
authorRandy <randy408@protonmail.com>2020-02-29 10:55:27 +0000
committerAlbert Astals Cid <tsdgeos@yahoo.es>2020-02-29 10:55:27 +0000
commit920c30cf1d4353b28266fc91f88b2ae72e0f5e4f (patch)
tree56f1d617f164aead0044b6e929e190b27b4875e4 /libspectre
parentca205d084434915127e64a9d54eafd05cdee872b (diff)
Add spectre_document_load_from_data(), update fuzz target
Diffstat (limited to 'libspectre')
-rw-r--r--libspectre/spectre-document.c53
-rw-r--r--libspectre/spectre-document.h13
2 files changed, 59 insertions, 7 deletions
diff --git a/libspectre/spectre-document.c b/libspectre/spectre-document.c
index c08c3c2..ff3e57c 100644
--- a/libspectre/spectre-document.c
+++ b/libspectre/spectre-document.c
@@ -50,13 +50,26 @@ spectre_document_new (void)
return doc;
}
-void
-spectre_document_load (SpectreDocument *document,
- const char *filename)
+static void
+document_load (SpectreDocument *document,
+ const char *filename,
+ void *buffer,
+ size_t size)
{
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;
@@ -68,7 +81,16 @@ spectre_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;
@@ -77,7 +99,7 @@ spectre_document_load (SpectreDocument *document,
document->doc = psscan (file, filename, SCANSTYLE_NORMAL);
if (!document->doc) {
document->status = SPECTRE_STATUS_LOAD_ERROR;
- fclose(file);
+ fclose (file);
return;
}
@@ -85,7 +107,7 @@ spectre_document_load (SpectreDocument *document,
document->status = SPECTRE_STATUS_LOAD_ERROR;
psdocdestroy (document->doc);
document->doc = NULL;
- fclose(file);
+ fclose (file);
return;
} else if (document->doc->numpages == 0 && !document->doc->format) {
@@ -100,7 +122,7 @@ spectre_document_load (SpectreDocument *document,
document->status = SPECTRE_STATUS_LOAD_ERROR;
psdocdestroy (document->doc);
document->doc = NULL;
- fclose(file);
+ fclose (file);
return;
}
@@ -112,8 +134,25 @@ spectre_document_load (SpectreDocument *document,
if (document->status != SPECTRE_STATUS_SUCCESS)
document->status = SPECTRE_STATUS_SUCCESS;
- fclose(file);
+ fclose (file);
+}
+
+void
+spectre_document_load (SpectreDocument *document,
+ const char *filename)
+{
+ document_load (document, filename, NULL, 0);
+}
+
+#if _POSIX_C_SOURCE >= 200809L
+void
+spectre_document_load_from_data (SpectreDocument *document,
+ void *data,
+ size_t size)
+{
+ document_load (document, NULL, data, size);
}
+#endif
void
spectre_document_free (SpectreDocument *document)
diff --git a/libspectre/spectre-document.h b/libspectre/spectre-document.h
index 40cccb4..c2ac135 100644
--- a/libspectre/spectre-document.h
+++ b/libspectre/spectre-document.h
@@ -43,6 +43,19 @@ 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
+ @see spectre_document_status
+*/
+SPECTRE_PUBLIC
+void spectre_document_load_from_data (SpectreDocument *document,
+ void *buffer,
+ size_t size);
+#endif
+
/*! Returns the document status
@param document the document whose status will be returned
*/