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