1 /*****************************************************************************/
2 // Copyright 2006-2019 Adobe Systems Incorporated
3 // All Rights Reserved.
4 //
5 // NOTICE:  Adobe permits you to use, modify, and distribute this file in
6 // accordance with the terms of the Adobe license agreement accompanying it.
7 /*****************************************************************************/
8 
9 #include "dng_file_stream.h"
10 
11 #include "dng_exceptions.h"
12 
13 /*****************************************************************************/
14 
dng_file_stream(const char * filename,bool output,uint32 bufferSize)15 dng_file_stream::dng_file_stream (const char *filename,
16 								  bool output,
17 								  uint32 bufferSize)
18 
19 	:	dng_stream ((dng_abort_sniffer *) NULL,
20 					bufferSize,
21 					0)
22 
23 	,	fFile (NULL)
24 
25 	{
26 
27 	fFile = fopen (filename, output ? "wb" : "rb");
28 
29 	if (!fFile)
30 		{
31 
32 		#if qDNGValidate
33 
34 		ReportError ("Unable to open file",
35 					 filename);
36 
37 		ThrowSilentError ();
38 
39 		#else
40 
41 		ThrowOpenFile ();
42 
43 		#endif
44 
45 		}
46 
47 	}
48 
49 /*****************************************************************************/
50 
51 #if qWinOS
52 
53 /*****************************************************************************/
54 
dng_file_stream(const wchar_t * filename,bool output,uint32 bufferSize)55 dng_file_stream::dng_file_stream (const wchar_t *filename,
56 								  bool output,
57 								  uint32 bufferSize)
58 
59 	:	dng_stream ((dng_abort_sniffer *) NULL,
60 					bufferSize,
61 					0)
62 
63 	,	fFile (NULL)
64 
65 	{
66 
67 	fFile = _wfopen (filename, output ? L"wb" : L"rb");
68 
69 	if (!fFile)
70 		{
71 
72 		#if qDNGValidate
73 
74 		char filenameCString[256];
75 
76 		size_t returnCount;
77 
78 		wcstombs_s (&returnCount,
79 					filenameCString,
80 					256,
81 					filename,
82 					_TRUNCATE);
83 
84 		ReportError ("Unable to open file",
85 					 filenameCString);
86 
87 		ThrowSilentError ();
88 
89 		#else
90 
91 		ThrowOpenFile ();
92 
93 		#endif	// qDNGValidate
94 
95 		}
96 
97 	}
98 
99 /*****************************************************************************/
100 
101 #endif	// qWinOS
102 
103 /*****************************************************************************/
104 
~dng_file_stream()105 dng_file_stream::~dng_file_stream ()
106 	{
107 
108 	if (fFile)
109 		{
110 		fclose (fFile);
111 		fFile = NULL;
112 		}
113 
114 	}
115 
116 /*****************************************************************************/
117 
DoGetLength()118 uint64 dng_file_stream::DoGetLength ()
119 	{
120 
121 	if (fseek (fFile, 0, SEEK_END) != 0)
122 		{
123 
124 		ThrowReadFile ();
125 
126 		}
127 
128 	return (uint64) ftell (fFile);
129 
130 	}
131 
132 /*****************************************************************************/
133 
DoRead(void * data,uint32 count,uint64 offset)134 void dng_file_stream::DoRead (void *data,
135 							  uint32 count,
136 							  uint64 offset)
137 	{
138 
139 	if (fseek (fFile, (long) offset, SEEK_SET) != 0)
140 		{
141 
142 		ThrowReadFile ();
143 
144 		}
145 
146 	uint32 bytesRead = (uint32) fread (data, 1, count, fFile);
147 
148 	if (bytesRead != count)
149 		{
150 
151 		ThrowReadFile ();
152 
153 		}
154 
155 	}
156 
157 /*****************************************************************************/
158 
DoWrite(const void * data,uint32 count,uint64 offset)159 void dng_file_stream::DoWrite (const void *data,
160 							   uint32 count,
161 							   uint64 offset)
162 	{
163 
164 	if (fseek (fFile, (uint32) offset, SEEK_SET) != 0)
165 		{
166 
167 		ThrowWriteFile ();
168 
169 		}
170 
171 	uint32 bytesWritten = (uint32) fwrite (data, 1, count, fFile);
172 
173 	if (bytesWritten != count)
174 		{
175 
176 		ThrowWriteFile ();
177 
178 		}
179 
180 	}
181 
182 /*****************************************************************************/
183