pdfviewer: don't crash when trailer is missing

Review URL: https://codereview.chromium.org/19027003

git-svn-id: http://skia.googlecode.com/svn/trunk@9987 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/experimental/PdfViewer/pdfparser/native/SkNativeParsedPDF.cpp b/experimental/PdfViewer/pdfparser/native/SkNativeParsedPDF.cpp
index 5f16176..9b668da 100644
--- a/experimental/PdfViewer/pdfparser/native/SkNativeParsedPDF.cpp
+++ b/experimental/PdfViewer/pdfparser/native/SkNativeParsedPDF.cpp
@@ -67,7 +67,10 @@
 // 1) run on a lot of file
 // 2) recoverable corupt file: remove endobj, endsteam, remove other keywords, use other white spaces, insert comments randomly, ...
 // 3) irrecoverable corrupt file
-SkNativeParsedPDF::SkNativeParsedPDF(const char* path) : fAllocator(new SkPdfAllocator()) {
+SkNativeParsedPDF::SkNativeParsedPDF(const char* path)
+        : fAllocator(new SkPdfAllocator())
+        , fRootCatalogRef(NULL)
+        , fRootCatalog(NULL) {
     gDoc = this;
     FILE* file = fopen(path, "r");
     fContentLength = getFileSize(path);
@@ -97,10 +100,16 @@
     // TODO(edisonn): warn/error expect fObjects[fRefCatalogId].fGeneration == fRefCatalogGeneration
     // TODO(edisonn): security, verify that SkPdfCatalogDictionary is indeed using mapper
     // load catalog
-    fRootCatalog = (SkPdfCatalogDictionary*)resolveReference(fRootCatalogRef);
-    SkPdfPageTreeNodeDictionary* tree = fRootCatalog->Pages(this);
 
-    fillPages(tree);
+    if (fRootCatalogRef) {
+        fRootCatalog = (SkPdfCatalogDictionary*)resolveReference(fRootCatalogRef);
+        SkPdfPageTreeNodeDictionary* tree = fRootCatalog->Pages(this);
+
+        fillPages(tree);
+    } else {
+        // TODO(edisonn): corrupted pdf, read it from beginning and rebuild (xref, trailer, or just reall all objects)
+        // 0 pages
+    }
 
     // now actually read all objects if we want, or do it lazyly
     // and resolve references?... or not ...
@@ -171,7 +180,13 @@
 
     SkPdfObject token;
     current = nextObject(current, trailerEnd, &token, fAllocator);
+    if (!token.isDictionary()) {
+        return -1;
+    }
     SkPdfFileTrailerDictionary* trailer = (SkPdfFileTrailerDictionary*)&token;
+    if (!trailer->valid()) {
+        return -1;
+    }
 
     if (storeCatalog) {
         const SkPdfObject* ref = trailer->Root(NULL);