/src/gdal/frmts/pcidsk/sdk/blockdir/binarytilelayer.cpp
Line | Count | Source |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Purpose: Block directory API. |
4 | | * |
5 | | ****************************************************************************** |
6 | | * Copyright (c) 2011 |
7 | | * PCI Geomatics, 90 Allstate Parkway, Markham, Ontario, Canada. |
8 | | * |
9 | | * SPDX-License-Identifier: MIT |
10 | | ****************************************************************************/ |
11 | | |
12 | | #include "blockdir/binarytilelayer.h" |
13 | | #include "blockdir/blockfile.h" |
14 | | #include "core/pcidsk_utils.h" |
15 | | #include "pcidsk_exception.h" |
16 | | #include <algorithm> |
17 | | #include <limits> |
18 | | |
19 | | using namespace PCIDSK; |
20 | | |
21 | | /************************************************************************/ |
22 | | /* BinaryTileLayer() */ |
23 | | /************************************************************************/ |
24 | | |
25 | | /** |
26 | | * Constructor. |
27 | | * |
28 | | * @param poBlockDir The associated block directory. |
29 | | * @param nLayer The index of the block layer. |
30 | | * @param psBlockLayer The block layer info. |
31 | | * @param psTileLayer The tile layer info. |
32 | | */ |
33 | | BinaryTileLayer::BinaryTileLayer(BlockDir * poBlockDir, uint32 nLayer, |
34 | | BlockLayerInfo * psBlockLayer, |
35 | | TileLayerInfo * psTileLayer) |
36 | 78 | : BlockTileLayer(poBlockDir, nLayer, psBlockLayer, psTileLayer) |
37 | 78 | { |
38 | 78 | } |
39 | | |
40 | | /************************************************************************/ |
41 | | /* WriteTileList() */ |
42 | | /************************************************************************/ |
43 | | |
44 | | /** |
45 | | * Writes the tile list to disk. |
46 | | */ |
47 | | void BinaryTileLayer::WriteTileList(void) |
48 | 0 | { |
49 | 0 | BlockTileInfoList oTileList = moTileList; |
50 | |
|
51 | 0 | SwapBlockTile(&oTileList.front(), oTileList.size()); |
52 | |
|
53 | 0 | WriteToLayer(&oTileList.front(), 0, |
54 | 0 | oTileList.size() * sizeof(BlockTileInfo)); |
55 | 0 | } |
56 | | |
57 | | /************************************************************************/ |
58 | | /* ReadTileList() */ |
59 | | /************************************************************************/ |
60 | | |
61 | | /** |
62 | | * Reads the tile list from disk. |
63 | | */ |
64 | | void BinaryTileLayer::ReadTileList(void) |
65 | 37 | { |
66 | 37 | uint32 nTileCount = GetTileCount(); |
67 | | |
68 | 37 | uint64 nSize = static_cast<uint64>(nTileCount) * sizeof(BlockTileInfo); |
69 | | |
70 | 37 | if (nSize > GetLayerSize() || !GetFile()->IsValidFileOffset(nSize)) |
71 | 1 | return ThrowPCIDSKException("The tile layer is corrupted."); |
72 | | |
73 | | #if SIZEOF_VOIDP < 8 |
74 | | if (nSize > std::numeric_limits<size_t>::max()) |
75 | | return ThrowPCIDSKException("Unable to read extremely large tile layer on 32-bit system."); |
76 | | #endif |
77 | | |
78 | 36 | try |
79 | 36 | { |
80 | 36 | moTileList.resize(nTileCount); |
81 | 36 | } |
82 | 36 | catch (const std::exception & ex) |
83 | 36 | { |
84 | 0 | return ThrowPCIDSKException("Out of memory in BinaryTileDir::ReadTileList(): %s", ex.what()); |
85 | 0 | } |
86 | | |
87 | 36 | ReadFromLayer(&moTileList.front(), 0, |
88 | 36 | moTileList.size() * sizeof(BlockTileInfo)); |
89 | | |
90 | 36 | SwapBlockTile(&moTileList.front(), moTileList.size()); |
91 | 36 | } |
92 | | |
93 | | /************************************************************************/ |
94 | | /* SwapBlockTile() */ |
95 | | /************************************************************************/ |
96 | | |
97 | | /** |
98 | | * Swaps the specified block tile info array. |
99 | | * |
100 | | * @param psTile The block tile info array to swap. |
101 | | * @param nCount The number of block tile info. |
102 | | */ |
103 | | void BinaryTileLayer::SwapBlockTile(BlockTileInfo * psTile, size_t nCount) |
104 | 34 | { |
105 | 34 | if (!mpoBlockDir->NeedsSwap()) |
106 | 34 | return; |
107 | | |
108 | 0 | for (BlockTileInfo * psEnd = psTile + nCount; |
109 | 0 | psTile < psEnd; psTile++) |
110 | 0 | { |
111 | 0 | SwapData(&psTile->nOffset, 8, 1); |
112 | 0 | SwapData(&psTile->nSize, 4, 1); |
113 | 0 | } |
114 | 0 | } |