vuminhdao / pdfium

Automatically exported from code.google.com/p/pdfium
0 stars 0 forks source link

Refactor: Expose a new FPDF_LoadDocument(int file_descriptor, string password) #49

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago

Currently, Pdfium exposes FPDF_LoadDocument(path) and 
FPDF_LoadMemDocument(buffer), but those 2 should really be just convenience 
wrappers on top of a more fundamental FPDF_LoadDocument(fd).

Pdfium should accept the PDF to be given as:
- a buffer
- a file (with path or fd)
- a stream (fd works, except have to make sure it never calls seek... there may 
be an issue here with protected PDFs)

For information, currently, we have added our own FPDF_LoadFdDocument(fd) as 
this (on Android's Google PDF Viewer):

// Patch on Foxit's File reader class to accept a FD directly.
class FdAccess: public CFXCRT_FileAccess_Posix {
 public:
  using CFXCRT_FileAccess_Posix::Open;
  bool Open(int fd) {
    m_nFD = fd;
    return true;
  }
};

FPDF_DOCUMENT Document::LoadFdDocument(int fd, const char* password) {
  IFX_FileRead* fdReader = CreateFileRead(fd);

  // The rest of the method id copied from FPDF_LoadDocument
  CPDF_Parser* pParser = new CPDF_Parser;
  pParser->SetPassword(password);

  FX_DWORD err_code = pParser->StartParse(fdReader);
  if (err_code) {
    delete pParser;
    ProcessParseError(err_code);
    // TODO(tlb): This is not safe at all. Take a new fd instead.
    lseek(fd, 0, SEEK_SET);
    return NULL;
  }
  return pParser->GetDocument();
}

IFX_FileRead* Document::CreateFileRead(int fd) {
  // Adapted from FX_CreateFileStream
  FdAccess* pFA = new FdAccess;
  if (!pFA) {
      return NULL;
  }
  if (!pFA->Open(fd)) {
      pFA->Release();
      return NULL;
  }
  return FX_NEW CFX_CRTFileStream(pFA);
}

Original issue reported on code.google.com by t...@google.com on 19 Sep 2014 at 2:09

GoogleCodeExporter commented 9 years ago

Original comment by pal...@chromium.org on 25 Sep 2014 at 11:02

GoogleCodeExporter commented 9 years ago

Original comment by pal...@chromium.org on 25 Sep 2014 at 11:24

GoogleCodeExporter commented 9 years ago
Here is a first attempt at part of it. Don't know if it's any good: 
https://codereview.chromium.org/607643004

Original comment by pal...@chromium.org on 26 Sep 2014 at 12:40

GoogleCodeExporter commented 9 years ago
Hey, thanks for picking that up.

Yes that looks like a good start. One thing that isn't clear is what state the 
FileRead is left after (attempting) to read the PDF. In particular (the only 
case where it matters is the password-protected), can the same object be reused 
and resubmitted with a different password, or do we have to discard it and give 
a new one?

Could we think of one unified method that would take all 3 cases, not only 2 of 
them:
- File as path (string)
- file as fd (int)
- memory as a buffer (void* + length)

Original comment by t...@google.com on 29 Sep 2014 at 11:36