| 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 |
#include "dng_flags.h" |
| 13 |
|
| 14 |
#if qAndroid |
| 15 |
#include <unistd.h> |
| 16 |
#endif |
| 17 |
|
| 18 |
/*****************************************************************************/ |
| 19 |
|
| 20 |
dng_file_stream::dng_file_stream (const char *filename, |
| 21 |
bool output, |
| 22 |
uint32 bufferSize) |
| 23 |
|
| 24 |
: dng_stream ((dng_abort_sniffer *) NULL, |
| 25 |
bufferSize, |
| 26 |
0) |
| 27 |
|
| 28 |
, fFile (NULL) |
| 29 |
|
| 30 |
{ |
| 31 |
|
| 32 |
fFile = fopen (filename, output ? "wb" : "rb"); |
| 33 |
|
| 34 |
if (!fFile) |
| 35 |
{ |
| 36 |
|
| 37 |
#if qDNGValidate |
| 38 |
|
| 39 |
ReportError ("Unable to open file", |
| 40 |
filename); |
| 41 |
|
| 42 |
ThrowSilentError (); |
| 43 |
|
| 44 |
#else |
| 45 |
|
| 46 |
ThrowOpenFile (); |
| 47 |
|
| 48 |
#endif |
| 49 |
|
| 50 |
} |
| 51 |
|
| 52 |
} |
| 53 |
|
| 54 |
/*****************************************************************************/ |
| 55 |
|
| 56 |
dng_file_stream::dng_file_stream (FILE *file, |
| 57 |
uint32 bufferSize) |
| 58 |
|
| 59 |
: dng_stream ((dng_abort_sniffer *) NULL, |
| 60 |
bufferSize, |
| 61 |
0) |
| 62 |
|
| 63 |
, fFile (file) |
| 64 |
|
| 65 |
{ |
| 66 |
|
| 67 |
if (!fFile) |
| 68 |
{ |
| 69 |
|
| 70 |
ThrowOpenFile ("Unable to open FILE *"); |
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
/*****************************************************************************/ |
| 77 |
|
| 78 |
#if qAndroid |
| 79 |
|
| 80 |
/*****************************************************************************/ |
| 81 |
|
| 82 |
dng_file_stream::dng_file_stream (int fd, |
| 83 |
const char *mode, |
| 84 |
uint32 bufferSize) |
| 85 |
|
| 86 |
: dng_stream ((dng_abort_sniffer *) NULL, |
| 87 |
bufferSize, |
| 88 |
0) |
| 89 |
|
| 90 |
, fFile (NULL) |
| 91 |
|
| 92 |
{ |
| 93 |
|
| 94 |
// Note: Use dup here as caller is responsible for separately managing fd. |
| 95 |
|
| 96 |
fFile = fdopen (dup (fd), mode); |
| 97 |
|
| 98 |
if (!fFile) |
| 99 |
{ |
| 100 |
|
| 101 |
#if qDNGValidate |
| 102 |
|
| 103 |
ReportError ("Unable to open file", |
| 104 |
filename); |
| 105 |
|
| 106 |
ThrowSilentError (); |
| 107 |
|
| 108 |
#else |
| 109 |
|
| 110 |
ThrowOpenFile (); |
| 111 |
|
| 112 |
#endif |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
/*****************************************************************************/ |
| 119 |
|
| 120 |
dng_file_stream::dng_file_stream (int fd, |
| 121 |
bool output, |
| 122 |
uint32 bufferSize) |
| 123 |
|
| 124 |
: dng_stream ((dng_abort_sniffer *) NULL, |
| 125 |
bufferSize, |
| 126 |
0) |
| 127 |
|
| 128 |
, fFile (NULL) |
| 129 |
|
| 130 |
{ |
| 131 |
|
| 132 |
// Note: Use dup here as caller is responsible for separately managing fd. |
| 133 |
|
| 134 |
fFile = fdopen (dup (fd), output ? "wb" : "rb"); |
| 135 |
|
| 136 |
if (!fFile) |
| 137 |
{ |
| 138 |
|
| 139 |
#if qDNGValidate |
| 140 |
|
| 141 |
ReportError ("Unable to open file", |
| 142 |
filename); |
| 143 |
|
| 144 |
ThrowSilentError (); |
| 145 |
|
| 146 |
#else |
| 147 |
|
| 148 |
ThrowOpenFile (); |
| 149 |
|
| 150 |
#endif |
| 151 |
|
| 152 |
} |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
/*****************************************************************************/ |
| 157 |
|
| 158 |
#endif // qAndroid |
| 159 |
|
| 160 |
/*****************************************************************************/ |
| 161 |
|
| 162 |
#if qWinOS |
| 163 |
|
| 164 |
/*****************************************************************************/ |
| 165 |
|
| 166 |
dng_file_stream::dng_file_stream (const wchar_t *filename, |
| 167 |
bool output, |
| 168 |
uint32 bufferSize) |
| 169 |
|
| 170 |
: dng_stream ((dng_abort_sniffer *) NULL, |
| 171 |
bufferSize, |
| 172 |
0) |
| 173 |
|
| 174 |
, fFile (NULL) |
| 175 |
|
| 176 |
{ |
| 177 |
|
| 178 |
fFile = _wfopen (filename, output ? L"wb" : L"rb"); |
| 179 |
|
| 180 |
if (!fFile) |
| 181 |
{ |
| 182 |
|
| 183 |
#if qDNGValidate |
| 184 |
|
| 185 |
char filenameCString[256]; |
| 186 |
|
| 187 |
size_t returnCount; |
| 188 |
|
| 189 |
wcstombs_s (&returnCount, |
| 190 |
filenameCString, |
| 191 |
256, |
| 192 |
filename, |
| 193 |
_TRUNCATE); |
| 194 |
|
| 195 |
ReportError ("Unable to open file", |
| 196 |
filenameCString); |
| 197 |
|
| 198 |
ThrowSilentError (); |
| 199 |
|
| 200 |
#else |
| 201 |
|
| 202 |
ThrowOpenFile (); |
| 203 |
|
| 204 |
#endif // qDNGValidate |
| 205 |
|
| 206 |
} |
| 207 |
|
| 208 |
} |
| 209 |
|
| 210 |
/*****************************************************************************/ |
| 211 |
|
| 212 |
#endif // qWinOS |
| 213 |
|
| 214 |
/*****************************************************************************/ |
| 215 |
|
| 216 |
dng_file_stream::~dng_file_stream () |
| 217 |
{ |
| 218 |
|
| 219 |
if (fFile) |
| 220 |
{ |
| 221 |
fclose (fFile); |
| 222 |
fFile = NULL; |
| 223 |
} |
| 224 |
|
| 225 |
} |
| 226 |
|
| 227 |
/*****************************************************************************/ |
| 228 |
|
| 229 |
uint64 dng_file_stream::DoGetLength () |
| 230 |
{ |
| 231 |
|
| 232 |
if (fseek (fFile, 0, SEEK_END) != 0) |
| 233 |
{ |
| 234 |
|
| 235 |
ThrowReadFile (); |
| 236 |
|
| 237 |
} |
| 238 |
|
| 239 |
return (uint64) ftell (fFile); |
| 240 |
|
| 241 |
} |
| 242 |
|
| 243 |
/*****************************************************************************/ |
| 244 |
|
| 245 |
void dng_file_stream::DoRead (void *data, |
| 246 |
uint32 count, |
| 247 |
uint64 offset) |
| 248 |
{ |
| 249 |
|
| 250 |
if (fseek (fFile, (long) offset, SEEK_SET) != 0) |
| 251 |
{ |
| 252 |
|
| 253 |
ThrowReadFile (); |
| 254 |
|
| 255 |
} |
| 256 |
|
| 257 |
uint32 bytesRead = (uint32) fread (data, 1, count, fFile); |
| 258 |
|
| 259 |
if (bytesRead != count) |
| 260 |
{ |
| 261 |
|
| 262 |
ThrowReadFile (); |
| 263 |
|
| 264 |
} |
| 265 |
|
| 266 |
} |
| 267 |
|
| 268 |
/*****************************************************************************/ |
| 269 |
|
| 270 |
void dng_file_stream::DoWrite (const void *data, |
| 271 |
uint32 count, |
| 272 |
uint64 offset) |
| 273 |
{ |
| 274 |
|
| 275 |
if (fseek (fFile, (uint32) offset, SEEK_SET) != 0) |
| 276 |
{ |
| 277 |
|
| 278 |
ThrowWriteFile (); |
| 279 |
|
| 280 |
} |
| 281 |
|
| 282 |
uint32 bytesWritten = (uint32) fwrite (data, 1, count, fFile); |
| 283 |
|
| 284 |
if (bytesWritten != count) |
| 285 |
{ |
| 286 |
|
| 287 |
ThrowWriteFile (); |
| 288 |
|
| 289 |
} |
| 290 |
|
| 291 |
} |
| 292 |
|
| 293 |
/*****************************************************************************/ |