| 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_stream.h" |
| 10 |
|
| 11 |
#include "dng_abort_sniffer.h" |
| 12 |
#include "dng_auto_ptr.h" |
| 13 |
#include "dng_bottlenecks.h" |
| 14 |
#include "dng_exceptions.h" |
| 15 |
#include "dng_globals.h" |
| 16 |
#include "dng_flags.h" |
| 17 |
#include "dng_memory.h" |
| 18 |
#include "dng_tag_types.h" |
| 19 |
#include "dng_assertions.h" |
| 20 |
|
| 21 |
/*****************************************************************************/ |
| 22 |
|
| 23 |
dng_stream::dng_stream (dng_abort_sniffer *sniffer, |
| 24 |
uint32 bufferSize, |
| 25 |
uint64 offsetInOriginalFile) |
| 26 |
|
| 27 |
: fSwapBytes (false) |
| 28 |
, fHaveLength (false) |
| 29 |
, fLength (0) |
| 30 |
, fOffsetInOriginalFile (offsetInOriginalFile) |
| 31 |
, fPosition (0) |
| 32 |
, fMemBlock () |
| 33 |
, fBuffer (NULL) |
| 34 |
, fBufferSize (Max_uint32 (bufferSize, gDNGStreamBlockSize * 2)) |
| 35 |
, fBufferStart (0) |
| 36 |
, fBufferEnd (0) |
| 37 |
, fBufferLimit (bufferSize) |
| 38 |
, fBufferDirty (false) |
| 39 |
, fSniffer (sniffer) |
| 40 |
|
| 41 |
{ |
| 42 |
|
| 43 |
fMemBlock.Reset (gDefaultDNGMemoryAllocator.Allocate (fBufferSize)); |
| 44 |
|
| 45 |
fBuffer = fMemBlock->Buffer_uint8 (); |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
/*****************************************************************************/ |
| 50 |
|
| 51 |
dng_stream::dng_stream (const void *data, |
| 52 |
uint32 count, |
| 53 |
uint64 offsetInOriginalFile) |
| 54 |
|
| 55 |
: fSwapBytes (false) |
| 56 |
, fHaveLength (true) |
| 57 |
, fLength (count) |
| 58 |
, fOffsetInOriginalFile (offsetInOriginalFile) |
| 59 |
, fPosition (0) |
| 60 |
, fMemBlock () |
| 61 |
, fBuffer ((uint8 *) data) |
| 62 |
, fBufferSize (count) |
| 63 |
, fBufferStart (0) |
| 64 |
, fBufferEnd (count) |
| 65 |
, fBufferLimit (count) |
| 66 |
, fBufferDirty (false) |
| 67 |
, fSniffer (NULL) |
| 68 |
|
| 69 |
{ |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
/*****************************************************************************/ |
| 74 |
|
| 75 |
dng_stream::~dng_stream () |
| 76 |
{ |
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
/*****************************************************************************/ |
| 81 |
|
| 82 |
uint64 dng_stream::DoGetLength () |
| 83 |
{ |
| 84 |
|
| 85 |
ThrowProgramError (); |
| 86 |
|
| 87 |
return 0; |
| 88 |
|
| 89 |
} |
| 90 |
|
| 91 |
/*****************************************************************************/ |
| 92 |
|
| 93 |
void dng_stream::DoRead (void * /* data */, |
| 94 |
uint32 /* count */, |
| 95 |
uint64 /* offset */) |
| 96 |
{ |
| 97 |
|
| 98 |
ThrowProgramError (); |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
/*****************************************************************************/ |
| 103 |
|
| 104 |
void dng_stream::DoSetLength (uint64 /* length */) |
| 105 |
{ |
| 106 |
|
| 107 |
ThrowProgramError (); |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
/*****************************************************************************/ |
| 112 |
|
| 113 |
void dng_stream::DoWrite (const void * /* data */, |
| 114 |
uint32 /* count */, |
| 115 |
uint64 /* offset */) |
| 116 |
{ |
| 117 |
|
| 118 |
ThrowProgramError (); |
| 119 |
|
| 120 |
} |
| 121 |
|
| 122 |
/*****************************************************************************/ |
| 123 |
|
| 124 |
bool dng_stream::BigEndian () const |
| 125 |
{ |
| 126 |
|
| 127 |
return fSwapBytes != (!!qDNGBigEndian); |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
/*****************************************************************************/ |
| 132 |
|
| 133 |
void dng_stream::SetBigEndian (bool bigEndian) |
| 134 |
{ |
| 135 |
|
| 136 |
fSwapBytes = (bigEndian != (!!qDNGBigEndian)); |
| 137 |
|
| 138 |
} |
| 139 |
|
| 140 |
/*****************************************************************************/ |
| 141 |
|
| 142 |
void dng_stream::SetBufferSize (dng_memory_allocator &allocator, |
| 143 |
uint32 newBufferSize) |
| 144 |
{ |
| 145 |
|
| 146 |
if (newBufferSize != fBufferSize && |
| 147 |
newBufferSize >= gDNGStreamBlockSize * 2 && |
| 148 |
!Data () && |
| 149 |
!fBufferDirty) |
| 150 |
{ |
| 151 |
|
| 152 |
try |
| 153 |
{ |
| 154 |
|
| 155 |
fMemBlock.Reset (allocator.Allocate (newBufferSize)); |
| 156 |
|
| 157 |
fBuffer = fMemBlock->Buffer_uint8 (); |
| 158 |
|
| 159 |
fBufferSize = newBufferSize; |
| 160 |
|
| 161 |
fBufferStart = 0; |
| 162 |
fBufferEnd = 0; |
| 163 |
fBufferLimit = newBufferSize; |
| 164 |
|
| 165 |
} |
| 166 |
|
| 167 |
catch (...) |
| 168 |
{ |
| 169 |
|
| 170 |
} |
| 171 |
|
| 172 |
} |
| 173 |
|
| 174 |
} |
| 175 |
|
| 176 |
/*****************************************************************************/ |
| 177 |
|
| 178 |
const void * dng_stream::Data () const |
| 179 |
{ |
| 180 |
|
| 181 |
if (fBufferStart == 0 && fHaveLength && fBufferEnd == fLength) |
| 182 |
{ |
| 183 |
|
| 184 |
return fBuffer; |
| 185 |
|
| 186 |
} |
| 187 |
|
| 188 |
return NULL; |
| 189 |
|
| 190 |
} |
| 191 |
|
| 192 |
/*****************************************************************************/ |
| 193 |
|
| 194 |
dng_memory_block * dng_stream::AsMemoryBlock (dng_memory_allocator &allocator, |
| 195 |
uint32 numLeadingZeroBytes) |
| 196 |
{ |
| 197 |
|
| 198 |
Flush (); |
| 199 |
|
| 200 |
uint64 len64 = Length (); |
| 201 |
|
| 202 |
if (len64 + uint64 (numLeadingZeroBytes) > 0xFFFFFFFF) |
| 203 |
{ |
| 204 |
ThrowProgramError (); |
| 205 |
} |
| 206 |
|
| 207 |
uint32 len = (uint32) len64; |
| 208 |
|
| 209 |
AutoPtr<dng_memory_block> block |
| 210 |
(allocator.Allocate (len + numLeadingZeroBytes)); |
| 211 |
|
| 212 |
if (len) |
| 213 |
{ |
| 214 |
|
| 215 |
SetReadPosition (0); |
| 216 |
|
| 217 |
Get (block->Buffer_uint8 () + numLeadingZeroBytes, |
| 218 |
len); |
| 219 |
|
| 220 |
if (numLeadingZeroBytes > 0) |
| 221 |
memset (block->Buffer (), |
| 222 |
0, |
| 223 |
size_t (numLeadingZeroBytes)); |
| 224 |
|
| 225 |
} |
| 226 |
|
| 227 |
return block.Release (); |
| 228 |
|
| 229 |
} |
| 230 |
|
| 231 |
/*****************************************************************************/ |
| 232 |
|
| 233 |
void dng_stream::SetReadPosition (uint64 offset) |
| 234 |
{ |
| 235 |
|
| 236 |
fPosition = offset; |
| 237 |
|
| 238 |
if (fPosition > Length ()) |
| 239 |
{ |
| 240 |
|
| 241 |
ThrowEndOfFile (); |
| 242 |
|
| 243 |
} |
| 244 |
|
| 245 |
} |
| 246 |
|
| 247 |
/*****************************************************************************/ |
| 248 |
|
| 249 |
uint64 dng_stream::OffsetInOriginalFile () const |
| 250 |
{ |
| 251 |
|
| 252 |
return fOffsetInOriginalFile; |
| 253 |
|
| 254 |
} |
| 255 |
|
| 256 |
/*****************************************************************************/ |
| 257 |
|
| 258 |
uint64 dng_stream::PositionInOriginalFile () const |
| 259 |
{ |
| 260 |
|
| 261 |
if (fOffsetInOriginalFile == kDNGStreamInvalidOffset) |
| 262 |
return kDNGStreamInvalidOffset; |
| 263 |
|
| 264 |
return fOffsetInOriginalFile + Position (); |
| 265 |
|
| 266 |
} |
| 267 |
|
| 268 |
/*****************************************************************************/ |
| 269 |
|
| 270 |
void dng_stream::Get (void *data, uint32 count, uint32 maxOverRead) |
| 271 |
{ |
| 272 |
|
| 273 |
while (count) |
| 274 |
{ |
| 275 |
|
| 276 |
// See if the request is totally inside buffer. |
| 277 |
|
| 278 |
if (fPosition >= fBufferStart && fPosition + count <= fBufferEnd) |
| 279 |
{ |
| 280 |
|
| 281 |
memcpy (data, |
| 282 |
fBuffer + (uint32) (fPosition - fBufferStart), |
| 283 |
count); |
| 284 |
|
| 285 |
fPosition += count; |
| 286 |
|
| 287 |
return; |
| 288 |
|
| 289 |
} |
| 290 |
|
| 291 |
// See if first part of request is inside buffer. |
| 292 |
|
| 293 |
if (fPosition >= fBufferStart && fPosition < fBufferEnd) |
| 294 |
{ |
| 295 |
|
| 296 |
uint32 block = (uint32) (fBufferEnd - fPosition); |
| 297 |
|
| 298 |
memcpy (data, |
| 299 |
fBuffer + (fPosition - fBufferStart), |
| 300 |
block); |
| 301 |
|
| 302 |
count -= block; |
| 303 |
|
| 304 |
data = (void *) (((char *) data) + block); |
| 305 |
|
| 306 |
fPosition += block; |
| 307 |
|
| 308 |
} |
| 309 |
|
| 310 |
// Flush buffer if dirty. |
| 311 |
|
| 312 |
Flush (); |
| 313 |
|
| 314 |
// Do large reads unbuffered. |
| 315 |
|
| 316 |
if (count > fBufferSize) |
| 317 |
{ |
| 318 |
DNG_ASSERT(maxOverRead == 0, "Over-read of large size unexpected"); |
| 319 |
if (fPosition + count > Length ()) |
| 320 |
{ |
| 321 |
|
| 322 |
ThrowEndOfFile (); |
| 323 |
|
| 324 |
} |
| 325 |
|
| 326 |
DoRead (data, |
| 327 |
count, |
| 328 |
fPosition); |
| 329 |
|
| 330 |
fPosition += count; |
| 331 |
|
| 332 |
return; |
| 333 |
|
| 334 |
} |
| 335 |
|
| 336 |
// Figure out new buffer range. |
| 337 |
|
| 338 |
fBufferStart = fPosition; |
| 339 |
|
| 340 |
if (fBufferSize >= gDNGStreamBlockSize) |
| 341 |
{ |
| 342 |
|
| 343 |
// Align to a file block. |
| 344 |
|
| 345 |
fBufferStart &= (uint64) ~((int64) (gDNGStreamBlockSize - 1)); |
| 346 |
|
| 347 |
} |
| 348 |
|
| 349 |
fBufferEnd = Min_uint64 (fBufferStart + fBufferSize, Length ()); |
| 350 |
|
| 351 |
if ((fBufferEnd - fPosition) < maxOverRead) |
| 352 |
return; // ep, allow over-read requests |
| 353 |
else |
| 354 |
if (fBufferEnd <= fPosition) |
| 355 |
{ |
| 356 |
|
| 357 |
ThrowEndOfFile (); |
| 358 |
|
| 359 |
} |
| 360 |
|
| 361 |
// Read data into buffer. |
| 362 |
|
| 363 |
dng_abort_sniffer::SniffForAbort (fSniffer); |
| 364 |
|
| 365 |
DoRead (fBuffer, |
| 366 |
(uint32) (fBufferEnd - fBufferStart), |
| 367 |
fBufferStart); |
| 368 |
|
| 369 |
} |
| 370 |
|
| 371 |
} |
| 372 |
|
| 373 |
/*****************************************************************************/ |
| 374 |
|
| 375 |
void dng_stream::SetWritePosition (uint64 offset) |
| 376 |
{ |
| 377 |
|
| 378 |
fPosition = offset; |
| 379 |
|
| 380 |
} |
| 381 |
|
| 382 |
/*****************************************************************************/ |
| 383 |
|
| 384 |
void dng_stream::Flush () |
| 385 |
{ |
| 386 |
|
| 387 |
if (fBufferDirty) |
| 388 |
{ |
| 389 |
|
| 390 |
dng_abort_sniffer::SniffForAbort (fSniffer); |
| 391 |
|
| 392 |
DoWrite (fBuffer, |
| 393 |
(uint32) (fBufferEnd - fBufferStart), |
| 394 |
fBufferStart); |
| 395 |
|
| 396 |
fBufferStart = 0; |
| 397 |
fBufferEnd = 0; |
| 398 |
fBufferLimit = fBufferSize; |
| 399 |
|
| 400 |
fBufferDirty = false; |
| 401 |
|
| 402 |
} |
| 403 |
|
| 404 |
} |
| 405 |
|
| 406 |
/*****************************************************************************/ |
| 407 |
|
| 408 |
void dng_stream::SetLength (uint64 length) |
| 409 |
{ |
| 410 |
|
| 411 |
Flush (); |
| 412 |
|
| 413 |
if (Length () != length) |
| 414 |
{ |
| 415 |
|
| 416 |
DoSetLength (length); |
| 417 |
|
| 418 |
fLength = length; |
| 419 |
|
| 420 |
} |
| 421 |
|
| 422 |
} |
| 423 |
|
| 424 |
/*****************************************************************************/ |
| 425 |
|
| 426 |
void dng_stream::Put (const void *data, |
| 427 |
uint32 count) |
| 428 |
{ |
| 429 |
|
| 430 |
// See if we can replace or append to the existing buffer. |
| 431 |
|
| 432 |
uint64 endPosition = fPosition + count; |
| 433 |
|
| 434 |
if (fBufferDirty && |
| 435 |
fPosition >= fBufferStart && |
| 436 |
fPosition <= fBufferEnd && |
| 437 |
endPosition <= fBufferLimit) |
| 438 |
{ |
| 439 |
|
| 440 |
memcpy (fBuffer + (uint32) (fPosition - fBufferStart), |
| 441 |
data, |
| 442 |
count); |
| 443 |
|
| 444 |
if (fBufferEnd < endPosition) |
| 445 |
fBufferEnd = endPosition; |
| 446 |
|
| 447 |
} |
| 448 |
|
| 449 |
// Else we need to write to the file. |
| 450 |
|
| 451 |
else |
| 452 |
{ |
| 453 |
|
| 454 |
// Write initial part of the data to buffer, if possible. |
| 455 |
|
| 456 |
if (fBufferDirty && |
| 457 |
fPosition >= fBufferStart && |
| 458 |
fPosition <= fBufferEnd && |
| 459 |
fPosition < fBufferLimit) |
| 460 |
{ |
| 461 |
|
| 462 |
uint32 subCount = (uint32) (fBufferLimit - fPosition); |
| 463 |
|
| 464 |
memcpy (fBuffer + (uint32) (fPosition - fBufferStart), |
| 465 |
data, |
| 466 |
subCount); |
| 467 |
|
| 468 |
count -= subCount; |
| 469 |
data = (const void *) (((const uint8 *) data) + subCount); |
| 470 |
|
| 471 |
fPosition = fBufferLimit; |
| 472 |
fBufferEnd = fBufferLimit; |
| 473 |
|
| 474 |
} |
| 475 |
|
| 476 |
// Write existing buffer. |
| 477 |
|
| 478 |
Flush (); |
| 479 |
|
| 480 |
// Figure out how much space we have in buffer from |
| 481 |
// current position to end of file block. |
| 482 |
|
| 483 |
uint64 blockRound = gDNGStreamBlockSize - 1; |
| 484 |
|
| 485 |
uint64 blockMask = ~((int64) blockRound); |
| 486 |
|
| 487 |
uint32 alignedSize = (uint32) |
| 488 |
(((fPosition + fBufferSize) & blockMask) - fPosition); |
| 489 |
|
| 490 |
// If write request will not fit in buffer, then write everything except |
| 491 |
// for the final unaligned part of the data. |
| 492 |
|
| 493 |
if (count > alignedSize) |
| 494 |
{ |
| 495 |
|
| 496 |
uint32 alignedCount = (uint32) |
| 497 |
(((fPosition + count) & blockMask) - fPosition); |
| 498 |
|
| 499 |
dng_abort_sniffer::SniffForAbort (fSniffer); |
| 500 |
|
| 501 |
DoWrite (data, alignedCount, fPosition); |
| 502 |
|
| 503 |
count -= alignedCount; |
| 504 |
data = (const void *) (((const uint8 *) data) + alignedCount); |
| 505 |
|
| 506 |
fPosition += alignedCount; |
| 507 |
|
| 508 |
} |
| 509 |
|
| 510 |
// Start a new buffer with small blocks. |
| 511 |
|
| 512 |
if (count > 0) |
| 513 |
{ |
| 514 |
|
| 515 |
fBufferDirty = true; |
| 516 |
|
| 517 |
fBufferStart = fPosition; |
| 518 |
fBufferEnd = endPosition; |
| 519 |
fBufferLimit = (fBufferStart + fBufferSize) & blockMask; |
| 520 |
|
| 521 |
memcpy (fBuffer, |
| 522 |
data, |
| 523 |
count); |
| 524 |
|
| 525 |
} |
| 526 |
|
| 527 |
} |
| 528 |
|
| 529 |
fPosition = endPosition; |
| 530 |
|
| 531 |
fLength = Max_uint64 (Length (), fPosition); |
| 532 |
|
| 533 |
} |
| 534 |
|
| 535 |
/*****************************************************************************/ |
| 536 |
|
| 537 |
uint16 dng_stream::Get_uint16 () |
| 538 |
{ |
| 539 |
|
| 540 |
uint16 x; |
| 541 |
|
| 542 |
Get (&x, 2); |
| 543 |
|
| 544 |
if (fSwapBytes) |
| 545 |
{ |
| 546 |
|
| 547 |
x = SwapBytes16 (x); |
| 548 |
|
| 549 |
} |
| 550 |
|
| 551 |
return x; |
| 552 |
|
| 553 |
} |
| 554 |
|
| 555 |
/*****************************************************************************/ |
| 556 |
|
| 557 |
void dng_stream::Put_uint16 (uint16 x) |
| 558 |
{ |
| 559 |
|
| 560 |
if (fSwapBytes) |
| 561 |
{ |
| 562 |
|
| 563 |
x = SwapBytes16 (x); |
| 564 |
|
| 565 |
} |
| 566 |
|
| 567 |
Put (&x, 2); |
| 568 |
|
| 569 |
} |
| 570 |
|
| 571 |
/*****************************************************************************/ |
| 572 |
|
| 573 |
uint32 dng_stream::Get_uint32 () |
| 574 |
{ |
| 575 |
|
| 576 |
uint32 x; |
| 577 |
|
| 578 |
Get (&x, 4); |
| 579 |
|
| 580 |
if (fSwapBytes) |
| 581 |
{ |
| 582 |
|
| 583 |
x = SwapBytes32 (x); |
| 584 |
|
| 585 |
} |
| 586 |
|
| 587 |
return x; |
| 588 |
|
| 589 |
} |
| 590 |
|
| 591 |
/*****************************************************************************/ |
| 592 |
|
| 593 |
void dng_stream::Put_uint32 (uint32 x) |
| 594 |
{ |
| 595 |
|
| 596 |
if (fSwapBytes) |
| 597 |
{ |
| 598 |
|
| 599 |
x = SwapBytes32 (x); |
| 600 |
|
| 601 |
} |
| 602 |
|
| 603 |
Put (&x, 4); |
| 604 |
|
| 605 |
} |
| 606 |
|
| 607 |
/*****************************************************************************/ |
| 608 |
|
| 609 |
uint64 dng_stream::Get_uint64 () |
| 610 |
{ |
| 611 |
|
| 612 |
if (fSwapBytes) |
| 613 |
{ |
| 614 |
|
| 615 |
union |
| 616 |
{ |
| 617 |
uint32 u32 [2]; |
| 618 |
uint64 u64; |
| 619 |
} u; |
| 620 |
|
| 621 |
u.u32 [1] = Get_uint32 (); |
| 622 |
u.u32 [0] = Get_uint32 (); |
| 623 |
|
| 624 |
return u.u64; |
| 625 |
|
| 626 |
} |
| 627 |
|
| 628 |
uint64 x; |
| 629 |
|
| 630 |
Get (&x, 8); |
| 631 |
|
| 632 |
return x; |
| 633 |
|
| 634 |
} |
| 635 |
|
| 636 |
/*****************************************************************************/ |
| 637 |
|
| 638 |
void dng_stream::Put_uint64 (uint64 x) |
| 639 |
{ |
| 640 |
|
| 641 |
if (fSwapBytes) |
| 642 |
{ |
| 643 |
|
| 644 |
union |
| 645 |
{ |
| 646 |
uint32 u32 [2]; |
| 647 |
uint64 u64; |
| 648 |
} u; |
| 649 |
|
| 650 |
u.u64 = x; |
| 651 |
|
| 652 |
Put_uint32 (u.u32 [1]); |
| 653 |
Put_uint32 (u.u32 [0]); |
| 654 |
|
| 655 |
} |
| 656 |
|
| 657 |
else |
| 658 |
{ |
| 659 |
|
| 660 |
Put (&x, 8); |
| 661 |
|
| 662 |
} |
| 663 |
|
| 664 |
} |
| 665 |
|
| 666 |
/*****************************************************************************/ |
| 667 |
|
| 668 |
real32 dng_stream::Get_real32 () |
| 669 |
{ |
| 670 |
|
| 671 |
union |
| 672 |
{ |
| 673 |
uint32 i; |
| 674 |
real32 r; |
| 675 |
} u; |
| 676 |
|
| 677 |
u.i = Get_uint32 (); |
| 678 |
|
| 679 |
return u.r; |
| 680 |
|
| 681 |
} |
| 682 |
|
| 683 |
/*****************************************************************************/ |
| 684 |
|
| 685 |
void dng_stream::Put_real32 (real32 x) |
| 686 |
{ |
| 687 |
|
| 688 |
if (fSwapBytes) |
| 689 |
{ |
| 690 |
|
| 691 |
union |
| 692 |
{ |
| 693 |
uint32 i; |
| 694 |
real32 r; |
| 695 |
} u; |
| 696 |
|
| 697 |
u.r = x; |
| 698 |
|
| 699 |
Put_uint32 (u.i); |
| 700 |
|
| 701 |
} |
| 702 |
|
| 703 |
else |
| 704 |
{ |
| 705 |
|
| 706 |
Put (&x, 4); |
| 707 |
|
| 708 |
} |
| 709 |
|
| 710 |
} |
| 711 |
|
| 712 |
/*****************************************************************************/ |
| 713 |
|
| 714 |
real64 dng_stream::Get_real64 () |
| 715 |
{ |
| 716 |
|
| 717 |
if (fSwapBytes) |
| 718 |
{ |
| 719 |
|
| 720 |
union |
| 721 |
{ |
| 722 |
uint32 i [2]; |
| 723 |
real64 r; |
| 724 |
} u; |
| 725 |
|
| 726 |
u.i [1] = Get_uint32 (); |
| 727 |
u.i [0] = Get_uint32 (); |
| 728 |
|
| 729 |
return u.r; |
| 730 |
|
| 731 |
} |
| 732 |
|
| 733 |
real64 x; |
| 734 |
|
| 735 |
Get (&x, 8); |
| 736 |
|
| 737 |
return x; |
| 738 |
|
| 739 |
} |
| 740 |
|
| 741 |
/*****************************************************************************/ |
| 742 |
|
| 743 |
void dng_stream::Put_real64 (real64 x) |
| 744 |
{ |
| 745 |
|
| 746 |
if (fSwapBytes) |
| 747 |
{ |
| 748 |
|
| 749 |
union |
| 750 |
{ |
| 751 |
uint32 i [2]; |
| 752 |
real64 r; |
| 753 |
} u; |
| 754 |
|
| 755 |
u.r = x; |
| 756 |
|
| 757 |
Put_uint32 (u.i [1]); |
| 758 |
Put_uint32 (u.i [0]); |
| 759 |
|
| 760 |
} |
| 761 |
|
| 762 |
else |
| 763 |
{ |
| 764 |
|
| 765 |
Put (&x, 8); |
| 766 |
|
| 767 |
} |
| 768 |
|
| 769 |
} |
| 770 |
|
| 771 |
/*****************************************************************************/ |
| 772 |
|
| 773 |
void dng_stream::Get_CString (char *data, uint32 maxLength) |
| 774 |
{ |
| 775 |
|
| 776 |
memset (data, 0, maxLength); |
| 777 |
|
| 778 |
uint32 index = 0; |
| 779 |
|
| 780 |
while (true) |
| 781 |
{ |
| 782 |
|
| 783 |
char c = (char) Get_uint8 (); |
| 784 |
|
| 785 |
if (index + 1 < maxLength) |
| 786 |
data [index++] = c; |
| 787 |
|
| 788 |
if (c == 0) |
| 789 |
break; |
| 790 |
|
| 791 |
} |
| 792 |
|
| 793 |
} |
| 794 |
|
| 795 |
/*****************************************************************************/ |
| 796 |
|
| 797 |
void dng_stream::Put_CString (const char *data) |
| 798 |
{ |
| 799 |
|
| 800 |
Put (data, (uint32) strlen (data) + 1); |
| 801 |
|
| 802 |
} |
| 803 |
|
| 804 |
/*****************************************************************************/ |
| 805 |
|
| 806 |
void dng_stream::Get_UString (char *data, uint32 maxLength) |
| 807 |
{ |
| 808 |
|
| 809 |
memset (data, 0, maxLength); |
| 810 |
|
| 811 |
uint32 index = 0; |
| 812 |
|
| 813 |
while (true) |
| 814 |
{ |
| 815 |
|
| 816 |
char c = (char) Get_uint16 (); |
| 817 |
|
| 818 |
if (index + 1 < maxLength) |
| 819 |
data [index++] = (char) c; |
| 820 |
|
| 821 |
if (c == 0) |
| 822 |
break; |
| 823 |
|
| 824 |
} |
| 825 |
|
| 826 |
} |
| 827 |
|
| 828 |
/*****************************************************************************/ |
| 829 |
|
| 830 |
void dng_stream::PutZeros (uint64 count) |
| 831 |
{ |
| 832 |
|
| 833 |
const uint32 kZeroBufferSize = 4096; |
| 834 |
|
| 835 |
if (count >= kZeroBufferSize) |
| 836 |
{ |
| 837 |
|
| 838 |
dng_memory_data zeroBuffer (kZeroBufferSize); |
| 839 |
|
| 840 |
DoZeroBytes (zeroBuffer.Buffer (), |
| 841 |
kZeroBufferSize); |
| 842 |
|
| 843 |
while (count) |
| 844 |
{ |
| 845 |
|
| 846 |
uint64 blockSize = Min_uint64 (count, kZeroBufferSize); |
| 847 |
|
| 848 |
Put (zeroBuffer.Buffer (), (uint32) blockSize); |
| 849 |
|
| 850 |
count -= blockSize; |
| 851 |
|
| 852 |
} |
| 853 |
|
| 854 |
} |
| 855 |
|
| 856 |
else |
| 857 |
{ |
| 858 |
|
| 859 |
uint32 count32 = (uint32) count; |
| 860 |
|
| 861 |
for (uint32 j = 0; j < count32; j++) |
| 862 |
{ |
| 863 |
|
| 864 |
Put_uint8 (0); |
| 865 |
|
| 866 |
} |
| 867 |
|
| 868 |
} |
| 869 |
|
| 870 |
} |
| 871 |
|
| 872 |
/*****************************************************************************/ |
| 873 |
|
| 874 |
void dng_stream::PadAlign2 () |
| 875 |
{ |
| 876 |
|
| 877 |
PutZeros (Position () & 1); |
| 878 |
|
| 879 |
} |
| 880 |
|
| 881 |
/*****************************************************************************/ |
| 882 |
|
| 883 |
void dng_stream::PadAlign4 () |
| 884 |
{ |
| 885 |
|
| 886 |
PutZeros ((4 - (Position () & 3)) & 3); |
| 887 |
|
| 888 |
} |
| 889 |
|
| 890 |
/*****************************************************************************/ |
| 891 |
|
| 892 |
uint32 dng_stream::TagValue_uint32 (uint32 tagType) |
| 893 |
{ |
| 894 |
|
| 895 |
switch (tagType) |
| 896 |
{ |
| 897 |
|
| 898 |
case ttByte: |
| 899 |
return (uint32) Get_uint8 (); |
| 900 |
|
| 901 |
case ttShort: |
| 902 |
return (uint32) Get_uint16 (); |
| 903 |
|
| 904 |
case ttLong: |
| 905 |
case ttIFD: |
| 906 |
return Get_uint32 (); |
| 907 |
|
| 908 |
} |
| 909 |
|
| 910 |
real64 x = TagValue_real64 (tagType); |
| 911 |
|
| 912 |
if (x < 0.0) |
| 913 |
x = 0.0; |
| 914 |
|
| 915 |
if (x > (real64) 0xFFFFFFFF) |
| 916 |
x = (real64) 0xFFFFFFFF; |
| 917 |
|
| 918 |
return ConvertDoubleToUint32 (x + 0.5); |
| 919 |
|
| 920 |
} |
| 921 |
|
| 922 |
/*****************************************************************************/ |
| 923 |
|
| 924 |
uint64 dng_stream::TagValue_uint64 (uint32 tagType) |
| 925 |
{ |
| 926 |
|
| 927 |
switch (tagType) |
| 928 |
{ |
| 929 |
|
| 930 |
case ttLong8: |
| 931 |
case ttIFD8: |
| 932 |
return Get_uint64 (); |
| 933 |
|
| 934 |
case ttSLong8: |
| 935 |
return (uint64) Max_int64 (0, Get_int64 ()); |
| 936 |
|
| 937 |
} |
| 938 |
|
| 939 |
return (uint64) TagValue_uint32 (tagType); |
| 940 |
|
| 941 |
} |
| 942 |
|
| 943 |
/*****************************************************************************/ |
| 944 |
|
| 945 |
int32 dng_stream::TagValue_int32 (uint32 tagType) |
| 946 |
{ |
| 947 |
|
| 948 |
switch (tagType) |
| 949 |
{ |
| 950 |
|
| 951 |
case ttSByte: |
| 952 |
return (int32) Get_int8 (); |
| 953 |
|
| 954 |
case ttSShort: |
| 955 |
return (int32) Get_int16 (); |
| 956 |
|
| 957 |
case ttSLong: |
| 958 |
return Get_int32 (); |
| 959 |
|
| 960 |
} |
| 961 |
|
| 962 |
real64 x = TagValue_real64 (tagType); |
| 963 |
|
| 964 |
if (x < 0.0) |
| 965 |
{ |
| 966 |
|
| 967 |
if (x < -2147483648.0) |
| 968 |
x = -2147483648.0; |
| 969 |
|
| 970 |
return ConvertDoubleToInt32 (x - 0.5); |
| 971 |
|
| 972 |
} |
| 973 |
|
| 974 |
else |
| 975 |
{ |
| 976 |
|
| 977 |
if (x > 2147483647.0) |
| 978 |
x = 2147483647.0; |
| 979 |
|
| 980 |
return ConvertDoubleToInt32 (x + 0.5); |
| 981 |
|
| 982 |
} |
| 983 |
|
| 984 |
} |
| 985 |
|
| 986 |
/*****************************************************************************/ |
| 987 |
|
| 988 |
int64 dng_stream::TagValue_int64 (uint32 tagType) |
| 989 |
{ |
| 990 |
|
| 991 |
switch (tagType) |
| 992 |
{ |
| 993 |
|
| 994 |
case ttSLong8: |
| 995 |
return Get_int64 (); |
| 996 |
|
| 997 |
case ttLong8: |
| 998 |
case ttIFD8: |
| 999 |
{ |
| 1000 |
|
| 1001 |
int64 x = Get_int64 (); |
| 1002 |
|
| 1003 |
if (x < 0) |
| 1004 |
x = 0x7FFFFFFFFFFFFFFF; |
| 1005 |
|
| 1006 |
return x; |
| 1007 |
|
| 1008 |
} |
| 1009 |
|
| 1010 |
} |
| 1011 |
|
| 1012 |
return (int64) TagValue_int32 (tagType); |
| 1013 |
|
| 1014 |
} |
| 1015 |
|
| 1016 |
/*****************************************************************************/ |
| 1017 |
|
| 1018 |
dng_urational dng_stream::TagValue_urational (uint32 tagType) |
| 1019 |
{ |
| 1020 |
|
| 1021 |
dng_urational result; |
| 1022 |
|
| 1023 |
result.n = 0; |
| 1024 |
result.d = 1; |
| 1025 |
|
| 1026 |
switch (tagType) |
| 1027 |
{ |
| 1028 |
|
| 1029 |
case ttRational: |
| 1030 |
{ |
| 1031 |
|
| 1032 |
result.n = Get_uint32 (); |
| 1033 |
result.d = Get_uint32 (); |
| 1034 |
|
| 1035 |
break; |
| 1036 |
|
| 1037 |
} |
| 1038 |
|
| 1039 |
case ttSRational: |
| 1040 |
{ |
| 1041 |
|
| 1042 |
int32 n = Get_int32 (); |
| 1043 |
int32 d = Get_int32 (); |
| 1044 |
|
| 1045 |
if ((n < 0) == (d < 0)) |
| 1046 |
{ |
| 1047 |
|
| 1048 |
if (d < 0) |
| 1049 |
{ |
| 1050 |
n = -n; |
| 1051 |
d = -d; |
| 1052 |
} |
| 1053 |
|
| 1054 |
result.n = (uint32) n; |
| 1055 |
result.d = (uint32) d; |
| 1056 |
|
| 1057 |
} |
| 1058 |
|
| 1059 |
break; |
| 1060 |
|
| 1061 |
} |
| 1062 |
|
| 1063 |
case ttByte: |
| 1064 |
case ttShort: |
| 1065 |
case ttLong: |
| 1066 |
case ttIFD: |
| 1067 |
{ |
| 1068 |
|
| 1069 |
result.n = TagValue_uint32 (tagType); |
| 1070 |
|
| 1071 |
break; |
| 1072 |
|
| 1073 |
} |
| 1074 |
|
| 1075 |
case ttSByte: |
| 1076 |
case ttSShort: |
| 1077 |
case ttSLong: |
| 1078 |
{ |
| 1079 |
|
| 1080 |
int32 n = TagValue_int32 (tagType); |
| 1081 |
|
| 1082 |
if (n > 0) |
| 1083 |
{ |
| 1084 |
result.n = (uint32) n; |
| 1085 |
} |
| 1086 |
|
| 1087 |
break; |
| 1088 |
|
| 1089 |
} |
| 1090 |
|
| 1091 |
default: |
| 1092 |
{ |
| 1093 |
|
| 1094 |
real64 x = TagValue_real64 (tagType); |
| 1095 |
|
| 1096 |
if (x > 0.0) |
| 1097 |
{ |
| 1098 |
|
| 1099 |
while (result.d < 10000 && x < 1000000) |
| 1100 |
{ |
| 1101 |
|
| 1102 |
result.d *= 10; |
| 1103 |
|
| 1104 |
x *= 10.0; |
| 1105 |
|
| 1106 |
} |
| 1107 |
|
| 1108 |
result.n = ConvertDoubleToUint32 (x + 0.5); |
| 1109 |
|
| 1110 |
} |
| 1111 |
|
| 1112 |
} |
| 1113 |
|
| 1114 |
} |
| 1115 |
|
| 1116 |
return result; |
| 1117 |
|
| 1118 |
} |
| 1119 |
|
| 1120 |
/*****************************************************************************/ |
| 1121 |
|
| 1122 |
dng_srational dng_stream::TagValue_srational (uint32 tagType) |
| 1123 |
{ |
| 1124 |
|
| 1125 |
dng_srational result; |
| 1126 |
|
| 1127 |
result.n = 0; |
| 1128 |
result.d = 1; |
| 1129 |
|
| 1130 |
switch (tagType) |
| 1131 |
{ |
| 1132 |
|
| 1133 |
case ttSRational: |
| 1134 |
{ |
| 1135 |
|
| 1136 |
result.n = Get_int32 (); |
| 1137 |
result.d = Get_int32 (); |
| 1138 |
|
| 1139 |
break; |
| 1140 |
|
| 1141 |
} |
| 1142 |
|
| 1143 |
default: |
| 1144 |
{ |
| 1145 |
|
| 1146 |
real64 x = TagValue_real64 (tagType); |
| 1147 |
|
| 1148 |
if (x > 0.0) |
| 1149 |
{ |
| 1150 |
|
| 1151 |
while (result.d < 10000 && x < 1000000.0) |
| 1152 |
{ |
| 1153 |
|
| 1154 |
result.d *= 10; |
| 1155 |
|
| 1156 |
x *= 10.0; |
| 1157 |
|
| 1158 |
} |
| 1159 |
|
| 1160 |
result.n = ConvertDoubleToInt32 (x + 0.5); |
| 1161 |
|
| 1162 |
} |
| 1163 |
|
| 1164 |
else |
| 1165 |
{ |
| 1166 |
|
| 1167 |
while (result.d < 10000 && x > -1000000.0) |
| 1168 |
{ |
| 1169 |
|
| 1170 |
result.d *= 10; |
| 1171 |
|
| 1172 |
x *= 10.0; |
| 1173 |
|
| 1174 |
} |
| 1175 |
|
| 1176 |
result.n = ConvertDoubleToInt32 (x - 0.5); |
| 1177 |
|
| 1178 |
} |
| 1179 |
|
| 1180 |
} |
| 1181 |
|
| 1182 |
} |
| 1183 |
|
| 1184 |
return result; |
| 1185 |
|
| 1186 |
} |
| 1187 |
|
| 1188 |
/*****************************************************************************/ |
| 1189 |
|
| 1190 |
real64 dng_stream::TagValue_real64 (uint32 tagType) |
| 1191 |
{ |
| 1192 |
|
| 1193 |
switch (tagType) |
| 1194 |
{ |
| 1195 |
|
| 1196 |
case ttByte: |
| 1197 |
case ttShort: |
| 1198 |
case ttLong: |
| 1199 |
case ttIFD: |
| 1200 |
return (real64) TagValue_uint32 (tagType); |
| 1201 |
|
| 1202 |
case ttLong8: |
| 1203 |
case ttIFD8: |
| 1204 |
return (real64) TagValue_uint64 (tagType); |
| 1205 |
|
| 1206 |
case ttSByte: |
| 1207 |
case ttSShort: |
| 1208 |
case ttSLong: |
| 1209 |
return (real64) TagValue_int32 (tagType); |
| 1210 |
|
| 1211 |
case ttSLong8: |
| 1212 |
return (real64) TagValue_int64 (tagType); |
| 1213 |
|
| 1214 |
case ttRational: |
| 1215 |
{ |
| 1216 |
|
| 1217 |
uint32 n = Get_uint32 (); |
| 1218 |
uint32 d = Get_uint32 (); |
| 1219 |
|
| 1220 |
if (d == 0) |
| 1221 |
return 0.0; |
| 1222 |
else |
| 1223 |
return (real64) n / (real64) d; |
| 1224 |
|
| 1225 |
} |
| 1226 |
|
| 1227 |
case ttSRational: |
| 1228 |
{ |
| 1229 |
|
| 1230 |
int32 n = Get_int32 (); |
| 1231 |
int32 d = Get_int32 (); |
| 1232 |
|
| 1233 |
if (d == 0) |
| 1234 |
return 0.0; |
| 1235 |
else |
| 1236 |
return (real64) n / (real64) d; |
| 1237 |
|
| 1238 |
} |
| 1239 |
|
| 1240 |
case ttFloat: |
| 1241 |
return (real64) Get_real32 (); |
| 1242 |
|
| 1243 |
case ttDouble: |
| 1244 |
return Get_real64 (); |
| 1245 |
|
| 1246 |
} |
| 1247 |
|
| 1248 |
return 0.0; |
| 1249 |
|
| 1250 |
} |
| 1251 |
|
| 1252 |
/*****************************************************************************/ |
| 1253 |
|
| 1254 |
void dng_stream::CopyToStream (dng_stream &dstStream, |
| 1255 |
uint64 count) |
| 1256 |
{ |
| 1257 |
|
| 1258 |
uint8 smallBuffer [1024]; |
| 1259 |
|
| 1260 |
if (count <= sizeof (smallBuffer)) |
| 1261 |
{ |
| 1262 |
|
| 1263 |
Get (smallBuffer, (uint32) count); |
| 1264 |
|
| 1265 |
dstStream.Put (smallBuffer, (uint32) count); |
| 1266 |
|
| 1267 |
} |
| 1268 |
|
| 1269 |
else |
| 1270 |
{ |
| 1271 |
|
| 1272 |
const uint32 bigBufferSize = (uint32) Min_uint64 (kBigBufferSize, |
| 1273 |
count); |
| 1274 |
|
| 1275 |
dng_memory_data bigBuffer (bigBufferSize); |
| 1276 |
|
| 1277 |
while (count) |
| 1278 |
{ |
| 1279 |
|
| 1280 |
uint32 blockCount = (uint32) Min_uint64 (bigBufferSize, |
| 1281 |
count); |
| 1282 |
|
| 1283 |
Get (bigBuffer.Buffer (), |
| 1284 |
blockCount); |
| 1285 |
|
| 1286 |
dstStream.Put (bigBuffer.Buffer (), |
| 1287 |
blockCount); |
| 1288 |
|
| 1289 |
count -= blockCount; |
| 1290 |
|
| 1291 |
} |
| 1292 |
|
| 1293 |
} |
| 1294 |
|
| 1295 |
} |
| 1296 |
|
| 1297 |
/*****************************************************************************/ |
| 1298 |
|
| 1299 |
void dng_stream::DuplicateStream (dng_stream &dstStream) |
| 1300 |
{ |
| 1301 |
|
| 1302 |
// Turn off sniffers for this operation. |
| 1303 |
|
| 1304 |
TempStreamSniffer noSniffer1 (*this , NULL); |
| 1305 |
TempStreamSniffer noSniffer2 (dstStream, NULL); |
| 1306 |
|
| 1307 |
// First grow the destination stream if required, in an attempt to |
| 1308 |
// reserve any needed space before overwriting the existing data. |
| 1309 |
|
| 1310 |
if (dstStream.Length () < Length ()) |
| 1311 |
{ |
| 1312 |
dstStream.SetLength (Length ()); |
| 1313 |
} |
| 1314 |
|
| 1315 |
SetReadPosition (0); |
| 1316 |
|
| 1317 |
dstStream.SetWritePosition (0); |
| 1318 |
|
| 1319 |
CopyToStream (dstStream, Length ()); |
| 1320 |
|
| 1321 |
dstStream.Flush (); |
| 1322 |
|
| 1323 |
dstStream.SetLength (Length ()); |
| 1324 |
|
| 1325 |
} |
| 1326 |
|
| 1327 |
/*****************************************************************************/ |
| 1328 |
|
| 1329 |
dng_stream_contiguous_read_hint::dng_stream_contiguous_read_hint |
| 1330 |
(dng_stream &stream, |
| 1331 |
dng_memory_allocator &allocator, |
| 1332 |
uint64 offset, |
| 1333 |
uint64 count) |
| 1334 |
|
| 1335 |
: fStream (stream) |
| 1336 |
, fAllocator (allocator) |
| 1337 |
, fOldBufferSize (stream.BufferSize ()) |
| 1338 |
|
| 1339 |
{ |
| 1340 |
|
| 1341 |
fStream.Flush (); // Cannot change buffer size with dirty buffer |
| 1342 |
|
| 1343 |
// Don't bother changing buffer size if only a small change. |
| 1344 |
|
| 1345 |
if (count > fOldBufferSize * 4) |
| 1346 |
{ |
| 1347 |
|
| 1348 |
// Round contiguous size up and down to stream blocks. |
| 1349 |
|
| 1350 |
uint64 blockRound = gDNGStreamBlockSize - 1; |
| 1351 |
|
| 1352 |
uint64 blockMask = ~((int64) blockRound); |
| 1353 |
|
| 1354 |
count = (count + (offset & blockRound) + blockRound) & blockMask; |
| 1355 |
|
| 1356 |
// Limit to maximum buffer size. |
| 1357 |
|
| 1358 |
uint64 newBufferSize = Min_uint64 (gDNGMaxStreamBufferSize, count); |
| 1359 |
|
| 1360 |
// To avoid reading too many bytes with the final read, adjust buffer |
| 1361 |
// size the to make an exact number of buffers fit. |
| 1362 |
|
| 1363 |
uint64 numBuffers = (count + newBufferSize - 1) / newBufferSize; |
| 1364 |
|
| 1365 |
newBufferSize = (count + numBuffers - 1) / numBuffers; |
| 1366 |
|
| 1367 |
// Finally round up to a block size. |
| 1368 |
|
| 1369 |
newBufferSize = (newBufferSize + blockRound) & blockMask; |
| 1370 |
|
| 1371 |
// Change the buffer size. |
| 1372 |
|
| 1373 |
fStream.SetBufferSize (fAllocator, (uint32) newBufferSize); |
| 1374 |
|
| 1375 |
} |
| 1376 |
|
| 1377 |
} |
| 1378 |
|
| 1379 |
/*****************************************************************************/ |
| 1380 |
|
| 1381 |
dng_stream_contiguous_read_hint::~dng_stream_contiguous_read_hint () |
| 1382 |
{ |
| 1383 |
|
| 1384 |
fStream.SetBufferSize (fAllocator, fOldBufferSize); |
| 1385 |
|
| 1386 |
} |
| 1387 |
|
| 1388 |
/*****************************************************************************/ |
| 1389 |
|
| 1390 |
TempBigEndian::TempBigEndian (dng_stream &stream, |
| 1391 |
bool bigEndian) |
| 1392 |
|
| 1393 |
: fStream (stream) |
| 1394 |
, fOldSwap (stream.SwapBytes ()) |
| 1395 |
|
| 1396 |
{ |
| 1397 |
|
| 1398 |
fStream.SetBigEndian (bigEndian); |
| 1399 |
|
| 1400 |
} |
| 1401 |
|
| 1402 |
/*****************************************************************************/ |
| 1403 |
|
| 1404 |
TempBigEndian::~TempBigEndian () |
| 1405 |
{ |
| 1406 |
|
| 1407 |
fStream.SetSwapBytes (fOldSwap); |
| 1408 |
|
| 1409 |
} |
| 1410 |
|
| 1411 |
/*****************************************************************************/ |
| 1412 |
|
| 1413 |
TempStreamSniffer::TempStreamSniffer (dng_stream &stream, |
| 1414 |
dng_abort_sniffer *sniffer) |
| 1415 |
|
| 1416 |
: fStream (stream) |
| 1417 |
, fOldSniffer (stream.Sniffer ()) |
| 1418 |
|
| 1419 |
{ |
| 1420 |
|
| 1421 |
fStream.SetSniffer (sniffer); |
| 1422 |
|
| 1423 |
} |
| 1424 |
|
| 1425 |
/*****************************************************************************/ |
| 1426 |
|
| 1427 |
TempStreamSniffer::~TempStreamSniffer () |
| 1428 |
{ |
| 1429 |
|
| 1430 |
fStream.SetSniffer (fOldSniffer); |
| 1431 |
|
| 1432 |
} |
| 1433 |
|
| 1434 |
/*****************************************************************************/ |