Revert "Revert "Add type cast definitions for CPDF_Dictionary.""
[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   FX_BOOL WriteBlock(const void* pData, size_t size) override;
22   void Release() override;
23
24  protected:
25   ~CFX_IFileWrite() override {}
26
27   FPDF_FILEWRITE* m_pFileWriteStruct;
28 };
29
30 CFX_IFileWrite::CFX_IFileWrite() {
31   m_pFileWriteStruct = NULL;
32 }
33
34 FX_BOOL CFX_IFileWrite::Init(FPDF_FILEWRITE* pFileWriteStruct) {
35   if (!pFileWriteStruct)
36     return FALSE;
37
38   m_pFileWriteStruct = pFileWriteStruct;
39   return TRUE;
40 }
41
42 FX_BOOL CFX_IFileWrite::WriteBlock(const void* pData, size_t size) {
43   if (!m_pFileWriteStruct)
44     return FALSE;
45
46   m_pFileWriteStruct->WriteBlock(m_pFileWriteStruct, pData, size);
47   return TRUE;
48 }
49
50 void CFX_IFileWrite::Release() {
51   delete this;
52 }
53
54 FPDF_BOOL _FPDF_Doc_Save(FPDF_DOCUMENT document,
55                          FPDF_FILEWRITE* pFileWrite,
56                          FPDF_DWORD flags,
57                          FPDF_BOOL bSetVersion,
58                          int fileVerion) {
59   CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
60   if (!pDoc)
61     return 0;
62
63   if (flags < FPDF_INCREMENTAL || flags > FPDF_REMOVE_SECURITY) {
64     flags = 0;
65   }
66
67   CPDF_Creator FileMaker(pDoc);
68   if (bSetVersion)
69     FileMaker.SetFileVersion(fileVerion);
70   if (flags == FPDF_REMOVE_SECURITY) {
71     flags = 0;
72     FileMaker.RemoveSecurity();
73   }
74   CFX_IFileWrite* pStreamWrite = NULL;
75   FX_BOOL bRet;
76   pStreamWrite = new CFX_IFileWrite;
77   pStreamWrite->Init(pFileWrite);
78   bRet = FileMaker.Create(pStreamWrite, flags);
79   pStreamWrite->Release();
80   return bRet;
81 }
82
83 DLLEXPORT FPDF_BOOL STDCALL FPDF_SaveAsCopy(FPDF_DOCUMENT document,
84                                             FPDF_FILEWRITE* pFileWrite,
85                                             FPDF_DWORD flags) {
86   return _FPDF_Doc_Save(document, pFileWrite, flags, FALSE, 0);
87 }
88
89 DLLEXPORT FPDF_BOOL STDCALL FPDF_SaveWithVersion(FPDF_DOCUMENT document,
90                                                  FPDF_FILEWRITE* pFileWrite,
91                                                  FPDF_DWORD flags,
92                                                  int fileVersion) {
93   return _FPDF_Doc_Save(document, pFileWrite, flags, TRUE, fileVersion);
94 }