Fixes for re-enabling more MSVC level 4 warnings: pdfium/ edition
[pdfium.git] / samples / pdfium_test.cc
index 67c15e0..f8e5673 100644 (file)
-// Copyright (c) 2010 The Chromium Authors. All rights reserved.\r
-// Use of this source code is governed by a BSD-style license that can be\r
-// found in the LICENSE file.\r
-\r
-#include <limits.h>\r
-#include <stdio.h>\r
-#include <stdlib.h>\r
-#include <string.h>\r
-\r
-#include <list>\r
-#include <string>\r
-#include <utility>\r
-\r
-#include "fpdfsdk/include/fpdf_dataavail.h"\r
-#include "fpdfsdk/include/fpdf_ext.h"\r
-#include "fpdfsdk/include/fpdfformfill.h"\r
-#include "fpdfsdk/include/fpdftext.h"\r
-#include "fpdfsdk/include/fpdfview.h"\r
-#include "v8/include/v8.h"\r
-\r
-#ifdef _WIN32\r
-  #define snprintf _snprintf\r
-  /* in Windows, rb for open and read binary file */\r
-  #define FOPEN_READ "rb"\r
-#else\r
-  #define FOPEN_READ "r"\r
-#endif\r
-\r
-static void WritePpm(const char* pdf_name, int num,\r
-                     const char* buffer, int stride, int width, int height) {\r
-  if (stride < 0 || width < 0 || height < 0)\r
-    return;\r
-  if (height > 0 && width > INT_MAX / height)\r
-    return;\r
-  int out_len = width * height;\r
-  if (out_len > INT_MAX / 3)\r
-    return;\r
-  out_len *= 3;\r
-\r
-  char filename[256];\r
-  snprintf(filename, sizeof(filename), "%s.%d.ppm", pdf_name, num);\r
-  FILE* fp = fopen(filename, "w");\r
-  if (!fp)\r
-    return;\r
-  fprintf(fp, "P6\n# PDF test render\n%d %d\n255\n", width, height);\r
-  // Source data is B, G, R, unused.\r
-  // Dest data is R, G, B.\r
-  char* result = new char[out_len];\r
-  if (result) {\r
-    for (int h = 0; h < height; ++h) {\r
-      const char* src_line = buffer + (stride * h);\r
-      char* dest_line = result + (width * h * 3);\r
-      for (int w = 0; w < width; ++w) {\r
-        // R\r
-        dest_line[w * 3] = src_line[(w * 4) + 2];\r
-        // G\r
-        dest_line[(w * 3) + 1] = src_line[(w * 4) + 1];\r
-        // B\r
-        dest_line[(w * 3) + 2] = src_line[w * 4];\r
-      }\r
-    }\r
-    fwrite(result, out_len, 1, fp);\r
-    delete [] result;\r
-  }\r
-  fclose(fp);\r
-}\r
-\r
-int Form_Alert(IPDF_JSPLATFORM*, FPDF_WIDESTRING, FPDF_WIDESTRING, int, int) {\r
-  printf("Form_Alert called.\n");\r
-  return 0;\r
-}\r
-\r
-void Unsupported_Handler(UNSUPPORT_INFO*, int type) {\r
-  std::string feature = "Unknown";\r
-  switch (type) {\r
-    case FPDF_UNSP_DOC_XFAFORM:\r
-      feature = "XFA";\r
-      break;\r
-    case FPDF_UNSP_DOC_PORTABLECOLLECTION:\r
-      feature = "Portfolios_Packages";\r
-      break;\r
-    case FPDF_UNSP_DOC_ATTACHMENT:\r
-    case FPDF_UNSP_ANNOT_ATTACHMENT:\r
-      feature = "Attachment";\r
-      break;\r
-    case FPDF_UNSP_DOC_SECURITY:\r
-      feature = "Rights_Management";\r
-      break;\r
-    case FPDF_UNSP_DOC_SHAREDREVIEW:\r
-      feature = "Shared_Review";\r
-      break;\r
-    case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT:\r
-    case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM:\r
-    case FPDF_UNSP_DOC_SHAREDFORM_EMAIL:\r
-      feature = "Shared_Form";\r
-      break;\r
-    case FPDF_UNSP_ANNOT_3DANNOT:\r
-      feature = "3D";\r
-      break;\r
-    case FPDF_UNSP_ANNOT_MOVIE:\r
-      feature = "Movie";\r
-      break;\r
-    case FPDF_UNSP_ANNOT_SOUND:\r
-      feature = "Sound";\r
-      break;\r
-    case FPDF_UNSP_ANNOT_SCREEN_MEDIA:\r
-    case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA:\r
-      feature = "Screen";\r
-      break;\r
-    case FPDF_UNSP_ANNOT_SIG:\r
-      feature = "Digital_Signature";\r
-      break;\r
-  }\r
-  printf("Unsupported feature: %s.\n", feature.c_str());\r
-}\r
-\r
-bool ParseCommandLine(int argc, const char* argv[], bool* write_images,\r
-                      std::list<const char*>* files) {\r
-  *write_images = false;\r
-  files->clear();\r
-\r
-  int cur_arg = 1;\r
-  if (cur_arg < argc &&\r
-      strcmp(argv[cur_arg], "--write_images") == 0) {\r
-    *write_images = true;\r
-    cur_arg++;\r
-  }\r
-\r
-  if (cur_arg >= argc)\r
-    return false;\r
-\r
-  for (int i = cur_arg; i < argc; i++)\r
-    files->push_back(argv[i]);\r
-\r
-  return true;\r
-}\r
-\r
-class TestLoader {\r
- public:\r
-  TestLoader(const char* pBuf, size_t len);\r
-\r
-  const char* m_pBuf;\r
-  size_t m_Len;\r
-};\r
-\r
-TestLoader::TestLoader(const char* pBuf, size_t len)\r
-    : m_pBuf(pBuf), m_Len(len) {\r
-}\r
-\r
-int Get_Block(void* param, unsigned long pos, unsigned char* pBuf,\r
-              unsigned long size) {\r
-  TestLoader* pLoader = (TestLoader*) param;\r
-  if (pos + size < pos || pos + size > pLoader->m_Len) return 0;\r
-  memcpy(pBuf, pLoader->m_pBuf + pos, size);\r
-  return 1;\r
-}\r
-\r
-bool Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {\r
-  return true;\r
-}\r
-\r
-void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {\r
-}\r
-\r
-void RenderPdf(const char* name, const char* pBuf, size_t len,\r
-               bool write_images) {\r
-  printf("Rendering PDF file %s.\n", name);\r
-\r
-  IPDF_JSPLATFORM platform_callbacks;\r
-  memset(&platform_callbacks, '\0', sizeof(platform_callbacks));\r
-  platform_callbacks.version = 1;\r
-  platform_callbacks.app_alert = Form_Alert;\r
-\r
-  FPDF_FORMFILLINFO form_callbacks;\r
-  memset(&form_callbacks, '\0', sizeof(form_callbacks));\r
-  form_callbacks.version = 1;\r
-  form_callbacks.m_pJsPlatform = &platform_callbacks;\r
-\r
-  TestLoader loader(pBuf, len);\r
-\r
-  FPDF_FILEACCESS file_access;\r
-  memset(&file_access, '\0', sizeof(file_access));\r
-  file_access.m_FileLen = len;\r
-  file_access.m_GetBlock = Get_Block;\r
-  file_access.m_Param = &loader;\r
-\r
-  FX_FILEAVAIL file_avail;\r
-  memset(&file_avail, '\0', sizeof(file_avail));\r
-  file_avail.version = 1;\r
-  file_avail.IsDataAvail = Is_Data_Avail;\r
-\r
-  FX_DOWNLOADHINTS hints;\r
-  memset(&hints, '\0', sizeof(hints));\r
-  hints.version = 1;\r
-  hints.AddSegment = Add_Segment;\r
-\r
-  FPDF_DOCUMENT doc;\r
-  FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access);\r
-\r
-  (void) FPDFAvail_IsDocAvail(pdf_avail, &hints);\r
-\r
-  if (!FPDFAvail_IsLinearized(pdf_avail)) {\r
-    printf("Non-linearized path...\n");\r
-    doc = FPDF_LoadCustomDocument(&file_access, NULL);\r
-  } else {\r
-    printf("Linearized path...\n");\r
-    doc = FPDFAvail_GetDocument(pdf_avail, NULL);\r
-  }\r
-\r
-  (void) FPDF_GetDocPermissions(doc);\r
-  (void) FPDFAvail_IsFormAvail(pdf_avail, &hints);\r
-\r
-  FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnviroument(doc, &form_callbacks);\r
-  FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD);\r
-  FPDF_SetFormFieldHighlightAlpha(form, 100);\r
-\r
-  int first_page = FPDFAvail_GetFirstPageNum(doc);\r
-  (void) FPDFAvail_IsPageAvail(pdf_avail, first_page, &hints);\r
-\r
-  int page_count = FPDF_GetPageCount(doc);\r
-  for (int i = 0; i < page_count; ++i) {\r
-    (void) FPDFAvail_IsPageAvail(pdf_avail, i, &hints);\r
-  }\r
-\r
-  FORM_DoDocumentJSAction(form);\r
-  FORM_DoDocumentOpenAction(form);\r
-\r
-  for (int i = 0; i < page_count; ++i) {\r
-    FPDF_PAGE page = FPDF_LoadPage(doc, i);\r
-    FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page);\r
-    FORM_OnAfterLoadPage(page, form);\r
-    FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN);\r
-\r
-    int width = static_cast<int>(FPDF_GetPageWidth(page));\r
-    int height = static_cast<int>(FPDF_GetPageHeight(page));\r
-    FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);\r
-    FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 255, 255, 255, 255);\r
-\r
-    FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);\r
-    FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0);\r
-    if (write_images) {\r
-      const char* buffer = reinterpret_cast<const char*>(\r
-          FPDFBitmap_GetBuffer(bitmap));\r
-      int stride = FPDFBitmap_GetStride(bitmap);\r
-      WritePpm(name, i, buffer, stride, width, height);\r
-    }\r
-\r
-    FPDFBitmap_Destroy(bitmap);\r
-\r
-    FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);\r
-    FORM_OnBeforeClosePage(page, form);\r
-    FPDFText_ClosePage(text_page);\r
-    FPDF_ClosePage(page);\r
-  }\r
-\r
-  FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC);\r
-  FPDFDOC_ExitFormFillEnviroument(form);\r
-  FPDF_CloseDocument(doc);\r
-  FPDFAvail_Destroy(pdf_avail);\r
-\r
-  printf("Loaded, parsed and rendered %d pages.\n", page_count);\r
-}\r
-\r
-int main(int argc, const char* argv[]) {\r
-  v8::V8::InitializeICU();\r
-  bool write_images = false;\r
-  std::list<const char*> files;\r
-  if (!ParseCommandLine(argc, argv, &write_images, &files)) {\r
-    printf("Usage is: test [--write_images] /path/to/pdf\n");\r
-    printf("--write_images - to write page images <pdf-name>.<page-number>.ppm\n");\r
-    return 1;\r
-  }\r
-\r
-  FPDF_InitLibrary(NULL);\r
-\r
-  UNSUPPORT_INFO unsuppored_info;\r
-  memset(&unsuppored_info, '\0', sizeof(unsuppored_info));\r
-  unsuppored_info.version = 1;\r
-  unsuppored_info.FSDK_UnSupport_Handler = Unsupported_Handler;\r
-\r
-  FSDK_SetUnSpObjProcessHandler(&unsuppored_info);\r
-\r
-  while (!files.empty()) {\r
-    const char* filename = files.front();\r
-    files.pop_front();\r
-    FILE* file = fopen(filename, FOPEN_READ);\r
-    if (!file) {\r
-      fprintf(stderr, "Failed to open: %s\n", filename);\r
-      continue;\r
-    }\r
-    (void) fseek(file, 0, SEEK_END);\r
-    size_t len = ftell(file);\r
-    (void) fseek(file, 0, SEEK_SET);\r
-    char* pBuf = (char*) malloc(len);\r
-    size_t ret = fread(pBuf, 1, len, file);\r
-    (void) fclose(file);\r
-    if (ret != len) {\r
-      fprintf(stderr, "Failed to read: %s\n", filename);\r
-    } else {\r
-      RenderPdf(filename, pBuf, len, write_images);\r
-    }\r
-    free(pBuf);\r
-  }\r
-\r
-  FPDF_DestroyLibrary();\r
-\r
-  return 0;\r
-}\r
-\r
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <list>
+#include <string>
+#include <utility>
+
+#include "../fpdfsdk/include/fpdf_dataavail.h"
+#include "../fpdfsdk/include/fpdf_ext.h"
+#include "../fpdfsdk/include/fpdfformfill.h"
+#include "../fpdfsdk/include/fpdftext.h"
+#include "../fpdfsdk/include/fpdfview.h"
+#include "v8/include/v8.h"
+
+#ifdef _WIN32
+  #define snprintf _snprintf
+#endif
+
+static void WritePpm(const char* pdf_name, int num,
+                     const char* buffer, int stride, int width, int height) {
+  if (stride < 0 || width < 0 || height < 0)
+    return;
+  if (height > 0 && width > INT_MAX / height)
+    return;
+  int out_len = width * height;
+  if (out_len > INT_MAX / 3)
+    return;
+  out_len *= 3;
+
+  char filename[256];
+  snprintf(filename, sizeof(filename), "%s.%d.ppm", pdf_name, num);
+  FILE* fp = fopen(filename, "wb");
+  if (!fp)
+    return;
+  fprintf(fp, "P6\n# PDF test render\n%d %d\n255\n", width, height);
+  // Source data is B, G, R, unused.
+  // Dest data is R, G, B.
+  char* result = new char[out_len];
+  if (result) {
+    for (int h = 0; h < height; ++h) {
+      const char* src_line = buffer + (stride * h);
+      char* dest_line = result + (width * h * 3);
+      for (int w = 0; w < width; ++w) {
+        // R
+        dest_line[w * 3] = src_line[(w * 4) + 2];
+        // G
+        dest_line[(w * 3) + 1] = src_line[(w * 4) + 1];
+        // B
+        dest_line[(w * 3) + 2] = src_line[w * 4];
+      }
+    }
+    fwrite(result, out_len, 1, fp);
+    delete [] result;
+  }
+  fclose(fp);
+}
+
+int Form_Alert(IPDF_JSPLATFORM*, FPDF_WIDESTRING, FPDF_WIDESTRING, int, int) {
+  printf("Form_Alert called.\n");
+  return 0;
+}
+
+void Unsupported_Handler(UNSUPPORT_INFO*, int type) {
+  std::string feature = "Unknown";
+  switch (type) {
+    case FPDF_UNSP_DOC_XFAFORM:
+      feature = "XFA";
+      break;
+    case FPDF_UNSP_DOC_PORTABLECOLLECTION:
+      feature = "Portfolios_Packages";
+      break;
+    case FPDF_UNSP_DOC_ATTACHMENT:
+    case FPDF_UNSP_ANNOT_ATTACHMENT:
+      feature = "Attachment";
+      break;
+    case FPDF_UNSP_DOC_SECURITY:
+      feature = "Rights_Management";
+      break;
+    case FPDF_UNSP_DOC_SHAREDREVIEW:
+      feature = "Shared_Review";
+      break;
+    case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT:
+    case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM:
+    case FPDF_UNSP_DOC_SHAREDFORM_EMAIL:
+      feature = "Shared_Form";
+      break;
+    case FPDF_UNSP_ANNOT_3DANNOT:
+      feature = "3D";
+      break;
+    case FPDF_UNSP_ANNOT_MOVIE:
+      feature = "Movie";
+      break;
+    case FPDF_UNSP_ANNOT_SOUND:
+      feature = "Sound";
+      break;
+    case FPDF_UNSP_ANNOT_SCREEN_MEDIA:
+    case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA:
+      feature = "Screen";
+      break;
+    case FPDF_UNSP_ANNOT_SIG:
+      feature = "Digital_Signature";
+      break;
+  }
+  printf("Unsupported feature: %s.\n", feature.c_str());
+}
+
+bool ParseCommandLine(int argc, const char* argv[], bool* write_images,
+                      std::list<const char*>* files) {
+  *write_images = false;
+  files->clear();
+
+  int cur_arg = 1;
+  if (cur_arg < argc &&
+      strcmp(argv[cur_arg], "--write_images") == 0) {
+    *write_images = true;
+    cur_arg++;
+  }
+
+  if (cur_arg >= argc)
+    return false;
+
+  for (int i = cur_arg; i < argc; i++)
+    files->push_back(argv[i]);
+
+  return true;
+}
+
+class TestLoader {
+ public:
+  TestLoader(const char* pBuf, size_t len);
+
+  const char* m_pBuf;
+  size_t m_Len;
+};
+
+TestLoader::TestLoader(const char* pBuf, size_t len)
+    : m_pBuf(pBuf), m_Len(len) {
+}
+
+int Get_Block(void* param, unsigned long pos, unsigned char* pBuf,
+              unsigned long size) {
+  TestLoader* pLoader = (TestLoader*) param;
+  if (pos + size < pos || pos + size > pLoader->m_Len) return 0;
+  memcpy(pBuf, pLoader->m_pBuf + pos, size);
+  return 1;
+}
+
+bool Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
+  return true;
+}
+
+void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
+}
+
+void RenderPdf(const char* name, const char* pBuf, size_t len,
+               bool write_images) {
+  printf("Rendering PDF file %s.\n", name);
+
+  IPDF_JSPLATFORM platform_callbacks;
+  memset(&platform_callbacks, '\0', sizeof(platform_callbacks));
+  platform_callbacks.version = 1;
+  platform_callbacks.app_alert = Form_Alert;
+
+  FPDF_FORMFILLINFO form_callbacks;
+  memset(&form_callbacks, '\0', sizeof(form_callbacks));
+  form_callbacks.version = 1;
+  form_callbacks.m_pJsPlatform = &platform_callbacks;
+
+  TestLoader loader(pBuf, len);
+
+  FPDF_FILEACCESS file_access;
+  memset(&file_access, '\0', sizeof(file_access));
+  file_access.m_FileLen = static_cast<unsigned long>(len);
+  file_access.m_GetBlock = Get_Block;
+  file_access.m_Param = &loader;
+
+  FX_FILEAVAIL file_avail;
+  memset(&file_avail, '\0', sizeof(file_avail));
+  file_avail.version = 1;
+  file_avail.IsDataAvail = Is_Data_Avail;
+
+  FX_DOWNLOADHINTS hints;
+  memset(&hints, '\0', sizeof(hints));
+  hints.version = 1;
+  hints.AddSegment = Add_Segment;
+
+  FPDF_DOCUMENT doc;
+  FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access);
+
+  (void) FPDFAvail_IsDocAvail(pdf_avail, &hints);
+
+  if (!FPDFAvail_IsLinearized(pdf_avail)) {
+    printf("Non-linearized path...\n");
+    doc = FPDF_LoadCustomDocument(&file_access, NULL);
+  } else {
+    printf("Linearized path...\n");
+    doc = FPDFAvail_GetDocument(pdf_avail, NULL);
+  }
+
+  (void) FPDF_GetDocPermissions(doc);
+  (void) FPDFAvail_IsFormAvail(pdf_avail, &hints);
+
+  FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnviroument(doc, &form_callbacks);
+  FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD);
+  FPDF_SetFormFieldHighlightAlpha(form, 100);
+
+  int first_page = FPDFAvail_GetFirstPageNum(doc);
+  (void) FPDFAvail_IsPageAvail(pdf_avail, first_page, &hints);
+
+  int page_count = FPDF_GetPageCount(doc);
+  for (int i = 0; i < page_count; ++i) {
+    (void) FPDFAvail_IsPageAvail(pdf_avail, i, &hints);
+  }
+
+  FORM_DoDocumentJSAction(form);
+  FORM_DoDocumentOpenAction(form);
+
+  for (int i = 0; i < page_count; ++i) {
+    FPDF_PAGE page = FPDF_LoadPage(doc, i);
+    FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page);
+    FORM_OnAfterLoadPage(page, form);
+    FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN);
+
+    int width = static_cast<int>(FPDF_GetPageWidth(page));
+    int height = static_cast<int>(FPDF_GetPageHeight(page));
+    FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
+    FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
+
+    FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
+    FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0);
+    if (write_images) {
+      const char* buffer = reinterpret_cast<const char*>(
+          FPDFBitmap_GetBuffer(bitmap));
+      int stride = FPDFBitmap_GetStride(bitmap);
+      WritePpm(name, i, buffer, stride, width, height);
+    }
+
+    FPDFBitmap_Destroy(bitmap);
+
+    FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
+    FORM_OnBeforeClosePage(page, form);
+    FPDFText_ClosePage(text_page);
+    FPDF_ClosePage(page);
+  }
+
+  FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC);
+  FPDFDOC_ExitFormFillEnviroument(form);
+  FPDF_CloseDocument(doc);
+  FPDFAvail_Destroy(pdf_avail);
+
+  printf("Loaded, parsed and rendered %d pages.\n", page_count);
+}
+
+int main(int argc, const char* argv[]) {
+  v8::V8::InitializeICU();
+  bool write_images = false;
+  std::list<const char*> files;
+  if (!ParseCommandLine(argc, argv, &write_images, &files)) {
+    printf("Usage is: test [--write_images] /path/to/pdf\n");
+    printf("--write_images - to write page images <pdf-name>.<page-number>.ppm\n");
+    return 1;
+  }
+
+  FPDF_InitLibrary(NULL);
+
+  UNSUPPORT_INFO unsuppored_info;
+  memset(&unsuppored_info, '\0', sizeof(unsuppored_info));
+  unsuppored_info.version = 1;
+  unsuppored_info.FSDK_UnSupport_Handler = Unsupported_Handler;
+
+  FSDK_SetUnSpObjProcessHandler(&unsuppored_info);
+
+  while (!files.empty()) {
+    const char* filename = files.front();
+    files.pop_front();
+    FILE* file = fopen(filename, "rb");
+    if (!file) {
+      fprintf(stderr, "Failed to open: %s\n", filename);
+      continue;
+    }
+    (void) fseek(file, 0, SEEK_END);
+    size_t len = ftell(file);
+    (void) fseek(file, 0, SEEK_SET);
+    char* pBuf = (char*) malloc(len);
+    size_t ret = fread(pBuf, 1, len, file);
+    (void) fclose(file);
+    if (ret != len) {
+      fprintf(stderr, "Failed to read: %s\n", filename);
+    } else {
+      RenderPdf(filename, pBuf, len, write_images);
+    }
+    free(pBuf);
+  }
+
+  FPDF_DestroyLibrary();
+
+  return 0;
+}
+