Enable security removal in FPDF_Doc_Save
[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 "../include/fsdk_define.h"
8 #include "../include/fpdfsave.h"
9 #include "../include/fpdfedit.h"
10 #if _FX_OS_ == _FX_ANDROID_
11 #include "time.h"
12 #else
13 #include <ctime>
14 #endif
15
16 class CFX_IFileWrite:public IFX_StreamWrite
17 {
18         
19 public:
20         CFX_IFileWrite();
21         FX_BOOL                         Init( FPDF_FILEWRITE * pFileWriteStruct );
22         virtual FX_BOOL         WriteBlock(const void* pData, size_t size);
23         virtual void            Release(){};
24         
25 protected:
26         FPDF_FILEWRITE*         m_pFileWriteStruct;
27 };
28
29 CFX_IFileWrite::CFX_IFileWrite()
30 {
31         m_pFileWriteStruct = NULL;
32 }
33
34 FX_BOOL CFX_IFileWrite::Init( FPDF_FILEWRITE * pFileWriteStruct )
35 {
36         if (!pFileWriteStruct)
37                 return FALSE;
38         else
39         {
40                 m_pFileWriteStruct = pFileWriteStruct;
41         }
42         return TRUE;
43 }
44
45 FX_BOOL CFX_IFileWrite::WriteBlock(const void* pData, size_t size)
46 {
47         if (m_pFileWriteStruct)
48         {
49                 m_pFileWriteStruct->WriteBlock( m_pFileWriteStruct, pData, size );
50                 return TRUE;
51         }
52         else 
53                 return FALSE;
54 }
55
56 FPDF_BOOL _FPDF_Doc_Save(FPDF_DOCUMENT document,FPDF_FILEWRITE * pFileWrite,FPDF_DWORD flags, FPDF_BOOL bSetVersion,
57                                                  int fileVerion)
58 {
59         CPDF_Document* pDoc = (CPDF_Document*)document;
60         if (!pDoc) 
61                 return 0;
62         
63         if ( flags < FPDF_INCREMENTAL || flags > FPDF_REMOVE_SECURITY )
64         {
65                 flags = 0;
66         }
67         
68         CPDF_Creator FileMaker(pDoc);
69         if(bSetVersion)
70                 FileMaker.SetFileVersion(fileVerion);
71         if(flags == FPDF_REMOVE_SECURITY)
72         {
73                 flags =  0;
74                 FileMaker.RemoveSecurity();
75         }
76         CFX_IFileWrite* pStreamWrite = NULL;
77         FX_BOOL bRet;
78         pStreamWrite = new CFX_IFileWrite;
79         pStreamWrite->Init( pFileWrite );
80         bRet = FileMaker.Create(pStreamWrite, flags);
81         delete pStreamWrite;
82         return bRet;
83 }
84
85 DLLEXPORT FPDF_BOOL STDCALL FPDF_SaveAsCopy(    FPDF_DOCUMENT document,FPDF_FILEWRITE * pFileWrite,
86                                                                                                 FPDF_DWORD flags )
87 {
88         return _FPDF_Doc_Save(document, pFileWrite, flags, FALSE , 0);
89 }
90
91
92 DLLEXPORT FPDF_BOOL STDCALL FPDF_SaveWithVersion(       FPDF_DOCUMENT document,FPDF_FILEWRITE * pFileWrite,
93         FPDF_DWORD flags, int fileVersion)
94 {
95         return _FPDF_Doc_Save(document, pFileWrite, flags, TRUE , fileVersion);
96 }