os/persistentdata/persistentstorage/store/HTOOLS/PFSDUMP.H
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
#ifndef __PFSDUMP_H__
sl@0
    17
#define __PFSDUMP_H__
sl@0
    18
sl@0
    19
#ifndef __MSVCDOTNET__
sl@0
    20
#pragma warning(disable: 4710)
sl@0
    21
#endif //__MSVCDOTNET__
sl@0
    22
sl@0
    23
int const MajorVersion=1;
sl@0
    24
int const MinorVersion=6;
sl@0
    25
int const Build=66;
sl@0
    26
sl@0
    27
#include <stdlib.h>
sl@0
    28
#include <memory.h>
sl@0
    29
#include <ctype.h>
sl@0
    30
sl@0
    31
#ifdef __MSVCDOTNET__
sl@0
    32
#include <strstream>
sl@0
    33
#include <fstream>
sl@0
    34
#include <iomanip>
sl@0
    35
#include <iostream>
sl@0
    36
using namespace std;
sl@0
    37
#else //!__MSVCDOTNET__
sl@0
    38
#include <strstrea.h>
sl@0
    39
#include <fstream.h>
sl@0
    40
#include <iomanip.h>
sl@0
    41
#endif //__MSVCDOTNET__
sl@0
    42
sl@0
    43
char const* const Margin="  ";
sl@0
    44
sl@0
    45
template <class T> inline T min(T a,T b) {return a<b ? a : b;}
sl@0
    46
template <class T> inline T max(T a,T b) {return a>b ? a : b;}
sl@0
    47
sl@0
    48
class Handle
sl@0
    49
	{
sl@0
    50
	enum
sl@0
    51
		{
sl@0
    52
		IndexMask=0xffffff,
sl@0
    53
		GenMask=0xf000000,
sl@0
    54
		GenShift=24,
sl@0
    55
		TocBase=0x40000000,
sl@0
    56
		AvailBit=0x80000000,
sl@0
    57
		TocDelta=AvailBit
sl@0
    58
		};
sl@0
    59
public:
sl@0
    60
	inline Handle() {}
sl@0
    61
	inline Handle(unsigned aValue) :iValue(aValue) {}
sl@0
    62
	inline bool IsNull() const {return iValue==0;}
sl@0
    63
	inline int Avail() const {return iValue&AvailBit;}
sl@0
    64
	inline int IsDelta() const {return iValue&TocDelta;}
sl@0
    65
	inline bool IsTocBase() const {return iValue==TocBase;}
sl@0
    66
	inline int Generation() const {return (iValue&GenMask)>>GenShift;}
sl@0
    67
	inline int Index() const {return iValue&IndexMask;}
sl@0
    68
	inline int operator==(Handle aHandle) const {return iValue==aHandle.iValue;}
sl@0
    69
	inline int operator!=(Handle aHandle) const {return iValue!=aHandle.iValue;}
sl@0
    70
private:
sl@0
    71
	unsigned long iValue;
sl@0
    72
//
sl@0
    73
	friend class StreamId;
sl@0
    74
	friend ostream& operator<<(ostream&,Handle);
sl@0
    75
	};
sl@0
    76
sl@0
    77
class StreamId : public Handle
sl@0
    78
	{
sl@0
    79
	enum {Mask=0xfffffff};
sl@0
    80
public:
sl@0
    81
	inline StreamId(Handle aHandle) :Handle(aHandle.iValue&Mask) {}
sl@0
    82
	};
sl@0
    83
sl@0
    84
class FramePos
sl@0
    85
	{
sl@0
    86
public:
sl@0
    87
	inline FramePos() {}
sl@0
    88
	inline FramePos(int aPos) :iValue(aPos) {}
sl@0
    89
	inline int Pos() const {return iValue;}
sl@0
    90
private:
sl@0
    91
	int iValue;
sl@0
    92
//
sl@0
    93
	friend ostream& operator<<(ostream&,FramePos);
sl@0
    94
	};
sl@0
    95
sl@0
    96
class FileOffset
sl@0
    97
	{
sl@0
    98
public:
sl@0
    99
	inline FileOffset(int anOffset) :iValue(anOffset) {}
sl@0
   100
	inline int Offset() const {return iValue;}
sl@0
   101
// offset/pos conversion
sl@0
   102
	FileOffset(FramePos aPos);
sl@0
   103
	operator FramePos() const;
sl@0
   104
private:
sl@0
   105
	int iValue;
sl@0
   106
//
sl@0
   107
	friend ostream& operator<<(ostream&,FileOffset);
sl@0
   108
	};
sl@0
   109
sl@0
   110
class FrameDes
sl@0
   111
	{
sl@0
   112
	enum {TypeMask=0xc000,TypeShift=14,LengthMask=0x3fff};
sl@0
   113
public:
sl@0
   114
	enum {First=0x20,Size=2,Interval=0x4002,FullShift=14};
sl@0
   115
	enum {Free=0,Data,Toc,Continuation};
sl@0
   116
//
sl@0
   117
	inline FrameDes() {}
sl@0
   118
	inline FrameDes(unsigned aDes) :iDes((unsigned short)aDes) {}
sl@0
   119
	inline int Type() const {return iDes>>TypeShift;}
sl@0
   120
	inline int Length() const {return iDes&LengthMask;}
sl@0
   121
private:
sl@0
   122
	unsigned short iDes;
sl@0
   123
//
sl@0
   124
	friend ostream& operator<<(ostream&,FrameDes);
sl@0
   125
	friend istream& operator>>(istream&,FrameDes&);
sl@0
   126
	};
sl@0
   127
sl@0
   128
class Header
sl@0
   129
	{
sl@0
   130
	enum {dirty=0x1,backupshift=1};
sl@0
   131
public:
sl@0
   132
	enum {tocoffset=-12};
sl@0
   133
public:
sl@0
   134
	struct Reloc
sl@0
   135
		{
sl@0
   136
		Handle iHandle;
sl@0
   137
		FramePos iPos;
sl@0
   138
		};
sl@0
   139
	enum {Offset=0x10,Size=14};
sl@0
   140
public:
sl@0
   141
	inline int Empty() const {return Toc().Pos()<0;}
sl@0
   142
	inline int Dirty() const {return iBackupToc&dirty;}
sl@0
   143
	FramePos Toc() const;
sl@0
   144
	Reloc const* GetReloc() const;
sl@0
   145
private:
sl@0
   146
	long iBackupToc;
sl@0
   147
	union
sl@0
   148
		{
sl@0
   149
		struct
sl@0
   150
			{
sl@0
   151
			long iZero;
sl@0
   152
			long iPos;
sl@0
   153
			} iToc;
sl@0
   154
		struct
sl@0
   155
			{
sl@0
   156
			unsigned long iHandle;
sl@0
   157
			long iPos;
sl@0
   158
			} iReloc;
sl@0
   159
		};
sl@0
   160
	unsigned short iCrc;
sl@0
   161
	};
sl@0
   162
ostream& operator<<(ostream&,Header const&);
sl@0
   163
sl@0
   164
class Frames
sl@0
   165
	{
sl@0
   166
public:
sl@0
   167
	struct Element
sl@0
   168
		{
sl@0
   169
		FramePos iPos;
sl@0
   170
		FrameDes iDes;
sl@0
   171
		};
sl@0
   172
	typedef Element const* Iterator;
sl@0
   173
public:
sl@0
   174
	Frames();
sl@0
   175
	~Frames();
sl@0
   176
//
sl@0
   177
	void Add(FramePos aPos,FrameDes aDes);
sl@0
   178
	void Complete();
sl@0
   179
	Iterator Find(FramePos aPos) const;
sl@0
   180
	inline Iterator Begin() const {return iRep;}
sl@0
   181
	inline Iterator End() const {return iRep+iElements;}
sl@0
   182
private:
sl@0
   183
	static int Compare(void const* aLeft,void const* aRight);
sl@0
   184
private:
sl@0
   185
	int iSize;
sl@0
   186
	int iElements;
sl@0
   187
	Element* iRep;
sl@0
   188
	};
sl@0
   189
sl@0
   190
class Stream
sl@0
   191
	{
sl@0
   192
public:
sl@0
   193
	inline Stream(Frames const& aFrames,FramePos aPos)	:iFrame(aFrames.Find(aPos)) {}
sl@0
   194
	inline Stream(Frames::Iterator aFrame) :iFrame(aFrame) {}
sl@0
   195
	inline int IsGood() const {return iFrame!=NULL;}
sl@0
   196
	inline int Type() const {return iFrame->iDes.Type();}
sl@0
   197
	inline Frames::Iterator Frame() const {return iFrame;}
sl@0
   198
	int Length() const;
sl@0
   199
	void* Load(istream& aStream) const;
sl@0
   200
private:
sl@0
   201
	Frames::Iterator iFrame;
sl@0
   202
	};
sl@0
   203
sl@0
   204
class Toc
sl@0
   205
	{
sl@0
   206
public:
sl@0
   207
	struct Head
sl@0
   208
		{
sl@0
   209
		enum {Size=12};
sl@0
   210
		inline StreamId Root() const {return StreamId(iRoot);}
sl@0
   211
		inline int IsDelta() const {return iRoot.IsDelta();}
sl@0
   212
		Handle iRoot;
sl@0
   213
		Handle iAvail;
sl@0
   214
		long iCount;
sl@0
   215
		};
sl@0
   216
	struct DeltaHead
sl@0
   217
		{
sl@0
   218
		enum {Size = 7};
sl@0
   219
		long iBase;
sl@0
   220
		unsigned short iMagic;
sl@0
   221
		unsigned char iCount;
sl@0
   222
		};
sl@0
   223
	struct Entry
sl@0
   224
		{
sl@0
   225
		enum {BaseSize=5,BaseRedundant=3,DeltaSize=8};
sl@0
   226
		inline Handle Link() const {return iLink;}
sl@0
   227
		inline FramePos Pos() const {return iPos;}
sl@0
   228
	//
sl@0
   229
		Handle iHandle;
sl@0
   230
		union
sl@0
   231
			{
sl@0
   232
			unsigned long iLink;
sl@0
   233
			long iPos;
sl@0
   234
			};
sl@0
   235
		};
sl@0
   236
	typedef Entry const* Iterator;
sl@0
   237
public:
sl@0
   238
	Toc();
sl@0
   239
	~Toc();
sl@0
   240
//
sl@0
   241
	void Load(istream& aStream,Frames const& aFrames,Frames::Iterator aFrame,Header::Reloc const* aReloc);
sl@0
   242
	inline Head const& Header() const {return iHeader;}
sl@0
   243
	inline Iterator Begin() const {return iRep;}
sl@0
   244
	inline Iterator End() const {return iRep+iHeader.iCount;}
sl@0
   245
	inline Entry const& operator[](int anIndex) {return iRep[anIndex-1];}
sl@0
   246
private:
sl@0
   247
	void Base(const char* aPtr,int aCount);
sl@0
   248
private:
sl@0
   249
	FramePos iPos;
sl@0
   250
	Head iHeader;
sl@0
   251
	DeltaHead iDelta;
sl@0
   252
	Entry* iRep;
sl@0
   253
	int iAvail;
sl@0
   254
//
sl@0
   255
	friend ostream& operator<<(ostream&,Toc const&);
sl@0
   256
	};
sl@0
   257
sl@0
   258
class HexDump
sl@0
   259
	{
sl@0
   260
	enum {HexInterval=0x10};
sl@0
   261
public:
sl@0
   262
	HexDump();
sl@0
   263
	~HexDump();
sl@0
   264
//
sl@0
   265
	void Reset();
sl@0
   266
	void Dump(istream& aStream,int aBytes);
sl@0
   267
	void Flush();
sl@0
   268
private:
sl@0
   269
	int iPos;
sl@0
   270
	int iByte;
sl@0
   271
	char* iPtr;
sl@0
   272
	char iChar[HexInterval+4];
sl@0
   273
	};
sl@0
   274
sl@0
   275
class StoreFile
sl@0
   276
	{
sl@0
   277
	enum {PermanentFileStoreUid=268435536};
sl@0
   278
public:
sl@0
   279
	StoreFile(char const* aFile);
sl@0
   280
	~StoreFile();
sl@0
   281
//
sl@0
   282
	inline int Empty() const {return iHeader.Empty();}
sl@0
   283
	void EmitHeader();
sl@0
   284
	void EmitFrames();
sl@0
   285
	void EmitToc();
sl@0
   286
	void EmitStreams();
sl@0
   287
private:
sl@0
   288
	void LoadFrames();
sl@0
   289
	void LoadToc();
sl@0
   290
public:
sl@0
   291
	static int OutWidth;
sl@0
   292
private:
sl@0
   293
	ifstream iFile;
sl@0
   294
	int iSize;
sl@0
   295
	Header iHeader;
sl@0
   296
	Frames iFrames;
sl@0
   297
	Toc iToc;
sl@0
   298
	};
sl@0
   299
sl@0
   300
class binbuf : public streambuf
sl@0
   301
	{
sl@0
   302
public:
sl@0
   303
	binbuf();
sl@0
   304
private:
sl@0
   305
	int overflow(int);
sl@0
   306
	int underflow();
sl@0
   307
	};
sl@0
   308
sl@0
   309
class Program
sl@0
   310
	{
sl@0
   311
public:
sl@0
   312
	enum {frame=0x1,quiet=0x2,verbose=0x4,compare=0x8};
sl@0
   313
public:
sl@0
   314
	Program();
sl@0
   315
//
sl@0
   316
	int ProcessCommandLine(int argc,char ** argv);
sl@0
   317
	void Information();
sl@0
   318
	void ExplainUsage();
sl@0
   319
	void Run();
sl@0
   320
	inline int Option(int type) const {return iOptions&type;}
sl@0
   321
	inline int TocRevision() const {return iTocRevision;}
sl@0
   322
//
sl@0
   323
	static void Abort(char const* message);
sl@0
   324
	static void Corrupt(char const* message);
sl@0
   325
	static ostream& Error();
sl@0
   326
	static ostream& Warning();
sl@0
   327
private:
sl@0
   328
	char const* iFile;
sl@0
   329
	int iOptions;
sl@0
   330
	int iTocRevision;
sl@0
   331
	binbuf iBinBuf;
sl@0
   332
	ostream iBin;
sl@0
   333
	int iErrors;
sl@0
   334
	};
sl@0
   335
sl@0
   336
extern Program TheProgram;
sl@0
   337
sl@0
   338
#endif