1 /***************************************************************************
2  *   Copyright (C) 2007 by Dominik Seichter                                *
3  *   domseichter@web.de                                                    *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU Library General Public License as       *
7  *   published by the Free Software Foundation; either version 2 of the    *
8  *   License, or (at your option) any later version.                       *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU Library General Public     *
16  *   License along with this program; if not, write to the                 *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  *                                                                         *
20  *   In addition, as a special exception, the copyright holders give       *
21  *   permission to link the code of portions of this program with the      *
22  *   OpenSSL library under certain conditions as described in each         *
23  *   individual source file, and distribute linked combinations            *
24  *   including the two.                                                    *
25  *   You must obey the GNU General Public License in all respects          *
26  *   for all of the code used other than OpenSSL.  If you modify           *
27  *   file(s) with this exception, you may extend this exception to your    *
28  *   version of the file(s), but you are not obligated to do so.  If you   *
29  *   do not wish to do so, delete this exception statement from your       *
30  *   version.  If you delete this exception statement from all source      *
31  *   files in the program, then also delete it here.                       *
32  ***************************************************************************/
33 
34 #include "PdfInputStream.h"
35 
36 #include "PdfInputDevice.h"
37 #include "PdfDefinesPrivate.h"
38 
39 #include <stdio.h>
40 #include <string.h>
41 #include <wchar.h>
42 
43 namespace PoDoFo {
44 
PdfFileInputStream(const char * pszFilename)45 PdfFileInputStream::PdfFileInputStream( const char* pszFilename )
46 {
47     m_hFile = fopen( pszFilename, "rb" );
48     if( !m_hFile )
49     {
50         PODOFO_RAISE_ERROR_INFO( ePdfError_FileNotFound, pszFilename );
51     }
52 }
53 
54 #ifdef _WIN32
PdfFileInputStream(const wchar_t * pszFilename)55 PdfFileInputStream::PdfFileInputStream( const wchar_t* pszFilename )
56 {
57     m_hFile = _wfopen( pszFilename, L"rb" );
58     if( !m_hFile )
59     {
60         PdfError e( ePdfError_FileNotFound, __FILE__, __LINE__ );
61         e.SetErrorInformation( pszFilename );
62         throw e;
63     }
64 }
65 #endif // _WIN32
66 
~PdfFileInputStream()67 PdfFileInputStream::~PdfFileInputStream()
68 {
69     if( m_hFile )
70         fclose( m_hFile );
71 }
72 
Read(char * pBuffer,pdf_long lLen,pdf_long *)73 pdf_long PdfFileInputStream::Read( char* pBuffer, pdf_long lLen, pdf_long* )
74 {
75     if( !pBuffer )
76     {
77         PODOFO_RAISE_ERROR( ePdfError_InvalidHandle );
78     }
79 
80     // return zero if EOF is reached
81     if( feof( m_hFile ) )
82         return 0;
83 
84     // return the number of bytes read and read the data
85     // into pBuffer
86     return fread( pBuffer, sizeof(char), lLen, m_hFile );
87 }
88 
GetFileLength()89 pdf_long PdfFileInputStream::GetFileLength()
90 {
91     pdf_long lOffset = ftello( m_hFile );
92     pdf_long lLen;
93 
94     if( lOffset == -1 )
95         PODOFO_RAISE_ERROR_INFO( ePdfError_InvalidDeviceOperation, "Failed to read current position in the file" );
96 
97     if( fseeko( m_hFile, 0L, SEEK_END ) == -1 )
98         PODOFO_RAISE_ERROR_INFO( ePdfError_InvalidDeviceOperation, "Failed to seek at the end of the file" );
99 
100     lLen = ftello( m_hFile );
101     if( lLen == -1 )
102         PODOFO_RAISE_ERROR_INFO( ePdfError_InvalidDeviceOperation, "Failed to read file length" );
103 
104     if( fseeko( m_hFile, lOffset, SEEK_SET ) == -1 )
105         PODOFO_RAISE_ERROR_INFO( ePdfError_InvalidDeviceOperation, "Failed to seek back to the previous position of the file" );
106 
107     return lLen;
108 }
109 
110 FILE*
GetHandle()111 PdfFileInputStream::GetHandle()
112 {
113     return m_hFile;
114 }
115 
116 
PdfMemoryInputStream(const char * pBuffer,pdf_long lBufferLen)117 PdfMemoryInputStream::PdfMemoryInputStream( const char* pBuffer, pdf_long lBufferLen )
118     : m_pBuffer( pBuffer ), m_pCur( pBuffer ), m_lBufferLen( lBufferLen )
119 {
120 
121 }
122 
~PdfMemoryInputStream()123 PdfMemoryInputStream::~PdfMemoryInputStream()
124 {
125 }
126 
Read(char * pBuffer,pdf_long lLen,pdf_long *)127 pdf_long PdfMemoryInputStream::Read( char* pBuffer, pdf_long lLen, pdf_long* )
128 {
129     if( !pBuffer )
130     {
131         PODOFO_RAISE_ERROR( ePdfError_InvalidHandle );
132     }
133 
134     pdf_long lRead = m_pCur - m_pBuffer;
135 
136     // return zero if EOF is reached
137     if( lRead == m_lBufferLen )
138         return 0;
139 
140     lLen = ( lRead + lLen <= m_lBufferLen ? lLen : m_lBufferLen - lRead );
141     memcpy( pBuffer, m_pCur, lLen );
142     m_pCur += lLen;
143 
144     return lLen;
145 }
146 
PdfDeviceInputStream(PdfInputDevice * pDevice)147 PdfDeviceInputStream::PdfDeviceInputStream( PdfInputDevice* pDevice )
148     : m_pDevice( pDevice )
149 {
150 }
151 
~PdfDeviceInputStream()152 PdfDeviceInputStream::~PdfDeviceInputStream()
153 {
154 }
155 
Read(char * pBuffer,pdf_long lLen,pdf_long *)156 pdf_long PdfDeviceInputStream::Read( char* pBuffer, pdf_long lLen, pdf_long* )
157 {
158     return m_pDevice->Read( pBuffer, lLen );
159 }
160 
161 };
162