glib: Don't create PopplerInputStream with length 0

Since commit a59f6164, PopplerInputStream requires a nonzero length.

Loosely based on an earlier patch by Kouhei Sutou. This version adds
support for length == -1, which is documented to work.

Resolves: https://gitlab.freedesktop.org/poppler/poppler/issues/414
Bug-Debian: https://bugs.debian.org/896596
diff --git a/glib/poppler-document.cc b/glib/poppler-document.cc
index cd7e8eb..fdaa8b0 100644
--- a/glib/poppler-document.cc
+++ b/glib/poppler-document.cc
@@ -308,7 +308,14 @@
   }
 
   if (stream_is_memory_buffer_or_local_file(stream)) {
-    str = new PopplerInputStream(stream, cancellable, 0, false, 0, Object(objNull));
+    if (length == (goffset)-1) {
+      if (!g_seekable_seek(G_SEEKABLE(stream), 0, G_SEEK_END, cancellable, error)) {
+        g_prefix_error(error, "Unable to determine length of stream: ");
+        return nullptr;
+      }
+      length = g_seekable_tell(G_SEEKABLE(stream));
+    }
+    str = new PopplerInputStream(stream, cancellable, 0, false, length, Object(objNull));
   } else {
     CachedFile *cachedFile = new CachedFile(new PopplerCachedFileLoader(stream, cancellable, length), new GooString());
     str = new CachedFileStream(cachedFile, 0, false, cachedFile->getLength(), Object(objNull));