Kill JS_TIMER_MAPARRAY
[pdfium.git] / fpdfsdk / src / fpdfsave.cpp
1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "../../public/fpdf_edit.h"
8 #include "../../public/fpdf_save.h"
9 #include "../include/fsdk_define.h"
10
11 #if _FX_OS_ == _FX_ANDROID_
12 #include "time.h"
13 #else
14 #include <ctime>
15 #endif
16
17 class CFX_IFileWrite final : public IFX_StreamWrite {
18  public:
19   CFX_IFileWrite();
20   FX_BOOL Init(FPDF_FILEWRITE* pFileWriteStruct);
21   virtual FX_BOOL WriteBlock(const void* pData, size_t size) override;
22   virtual void Release() override {}
23
24  protected:
25   FPDF_FILEWRITE* m_pFileWriteStruct;
26 };
27
28 CFX_IFileWrite::CFX_IFileWrite() {
29   m_pFileWriteStruct = NULL;
30 }
31
32 FX_BOOL CFX_IFileWrite::Init(FPDF_FILEWRITE* pFileWriteStruct) {
33   if (!pFileWriteStruct)
34     return FALSE;
35
36   m_pFileWriteStruct = pFileWriteStruct;
37   return TRUE;
38 }
39
40 FX_BOOL CFX_IFileWrite::WriteBlock(const void* pData, size_t size) {
41   if (!m_pFileWriteStruct)
42     return FALSE;
43
44   m_pFileWriteStruct->WriteBlock(m_pFileWriteStruct, pData, size);
45   return TRUE;
46 }
47
48 FPDF_BOOL _FPDF_Doc_Save(FPDF_DOCUMENT document,
49                          FPDF_FILEWRITE* pFileWrite,
50                          FPDF_DWORD flags,
51                          FPDF_BOOL bSetVersion,
52                          int fileVerion) {
53   CPDF_Document* pDoc = (CPDF_Document*)document;
54   if (!pDoc)
55     return 0;
56
57   if (flags < FPDF_INCREMENTAL || flags > FPDF_REMOVE_SECURITY) {
58     flags = 0;
59   }
60
61   CPDF_Creator FileMaker(pDoc);
62   if (bSetVersion)
63     FileMaker.SetFileVersion(fileVerion);
64   if (flags == FPDF_REMOVE_SECURITY) {
65     flags = 0;
66     FileMaker.RemoveSecurity();
67   }
68   CFX_IFileWrite* pStreamWrite = NULL;
69   FX_BOOL bRet;
70   pStreamWrite = new CFX_IFileWrite;
71   pStreamWrite->Init(pFileWrite);
72   bRet = FileMaker.Create(pStreamWrite, flags);
73   delete pStreamWrite;
74   return bRet;
75 }
76
77 DLLEXPORT FPDF_BOOL STDCALL FPDF_SaveAsCopy(FPDF_DOCUMENT document,
78                                             FPDF_FILEWRITE* pFileWrite,
79                                             FPDF_DWORD flags) {
80   return _FPDF_Doc_Save(document, pFileWrite, flags, FALSE, 0);
81 }
82
83 DLLEXPORT FPDF_BOOL STDCALL FPDF_SaveWithVersion(FPDF_DOCUMENT document,
84                                                  FPDF_FILEWRITE* pFileWrite,
85                                                  FPDF_DWORD flags,
86                                                  int fileVersion) {
87   return _FPDF_Doc_Save(document, pFileWrite, flags, TRUE, fileVersion);
88 }