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