gui
[lbook_fbreader.git] / fbreader / src / formats / pdb / MobipocketStream.cpp
blob59b580f6905da3e47f6f57a34f6a8b1dc9947143
1 /*
2 * Copyright (C) 2004-2008 Geometer Plus <contact@geometerplus.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
20 #include <ZLFile.h>
21 #include <ZLResource.h>
23 #include "MobipocketStream.h"
25 MobipocketStream::MobipocketStream(ZLFile &file) : PalmDocStream(file), myErrorCode(ERROR_NONE) {
28 bool MobipocketStream::open() {
29 myErrorCode = ERROR_NONE;
31 if (!PdbStream::open()) {
32 myErrorCode = ERROR_UNKNOWN;
33 return false;
36 myBaseSize = myBase->sizeOfOpened();
38 unsigned short version;
39 PdbUtil::readUnsignedShort(*myBase, version);
40 if ((version != 1) && (version != 2)) {
41 myErrorCode = ERROR_COMPRESSION;
42 return false;
44 myIsCompressed = (version == 2);
45 myBase->seek(6, false);
46 unsigned short records;
47 PdbUtil::readUnsignedShort(*myBase, records);
48 myMaxRecordIndex = std::min(records, (unsigned short)(myHeader.Offsets.size() - 1));
49 PdbUtil::readUnsignedShort(*myBase, myMaxRecordSize);
50 if (myMaxRecordSize == 0) {
51 myErrorCode = ERROR_UNKNOWN;
52 return false;
54 unsigned short reserved2;
55 PdbUtil::readUnsignedShort(*myBase, reserved2);
56 myBuffer = new char[myMaxRecordSize];
58 myRecordIndex = 0;
60 return true;
63 bool MobipocketStream::hasExtraSections() const {
64 return myMaxRecordIndex < myHeader.Offsets.size() - 1;
67 std::pair<int,int> MobipocketStream::imageLocation(int index) {
68 index += myMaxRecordIndex + 1;
69 int recordNumber = myHeader.Offsets.size();
70 if (index > recordNumber - 1) {
71 return std::pair<int,int>(-1, -1);
72 } else {
73 int start = myHeader.Offsets[index];
74 int end = (index < recordNumber - 1) ?
75 myHeader.Offsets[index + 1] : myBaseSize;
76 return std::pair<int,int>(start, end - start);
80 const std::string &MobipocketStream::error() const {
81 static const ZLResource &resource = ZLResource::resource("mobipocketPlugin");
82 switch (myErrorCode) {
83 default:
85 static const std::string EMPTY;
86 return EMPTY;
88 case ERROR_UNKNOWN:
89 return resource["unknown"].value();
90 case ERROR_COMPRESSION:
91 return resource["unsupportedCompressionMethod"].value();
92 case ERROR_ENCRIPTION:
93 return resource["encriptedFile"].value();