00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
#ifndef CTFILESYSTEM2_H
00030
#define CTFILESYSTEM2_H
00031
00032
#ifdef CT_USE_ENCRYPTION
00033
00034
00035
00036
00037
# define PERL5
00038
# include <openssl/des.h>
00039
# undef PERL5
00040
#endif
00041
00042
00043 #define CTFILESYSTEM_VERSION_MAJOR 1
00044 #define CTFILESYSTEM_VERSION_MINOR 0
00045
00050 #define CTFILESYSTEM_BASEBLOCKSIZE 32
00051
00058 #define CTFILESYSTEM_MAXSIZE (64*1024)
00059
00060
00061 #define CTFILESYSTEM_FAT_LENGTH 256
00062
00063
00064
#include <chipcard/ctmemorycard.h>
00065
#include <chipcard/ctdatacache.h>
00066
00067
00114
#ifndef DOXYGEN
00115
00118
class CHIPCARD_API CTBlockMedium:
public CTMemoryCard {
00119
public:
00120 CTBlockMedium(
const CTCard &c);
00121
virtual ~CTBlockMedium();
00122
00123
virtual CTError readBlocks(
int bn,
int n, string &bl);
00124
virtual CTError writeBlocks(
int bn,
int n,
const string &bl);
00125
virtual CTError mountMedium();
00126
virtual CTError unmountMedium();
00127 };
00128
#endif
00129
00130
00131
00132
#ifndef DOXYGEN
00133
00136
class CHIPCARD_API CTCachedBlockMedium:
public CTBlockMedium {
00137
private:
00138
CTDataCache<CTFILESYSTEM_MAXSIZE,32> _cache;
00139
public:
00140 CTCachedBlockMedium(
const CTCard &c);
00141
virtual ~CTCachedBlockMedium();
00142
00143
virtual CTError readBlocks(
int bn,
int n, string &bl);
00144
virtual CTError writeBlocks(
int bn,
int n,
const string &bl);
00145
virtual int flush(
int maxb=0x800000)
throw (
class CTError);
00146
virtual void purge();
00147
virtual CTError mountMedium();
00148
virtual CTError unmountMedium();
00149 };
00150
#endif
00151
00152
00153
#ifndef DOXYGEN
00154
00158
class CHIPCARD_API CTCryptedBlockMedium:
public CTCachedBlockMedium {
00159
private:
00160 des_cblock _desKey1;
00161 des_cblock _desKey2;
00162
bool _desKeyIsValid;
00163
00164
protected:
00165
CTError crypt(
bool encrypt,
const string &src, string &dest);
00166
00167
public:
00168 CTCryptedBlockMedium(
const CTCard &c);
00169
virtual ~CTCryptedBlockMedium();
00170
00171
virtual CTError readBlocks(
int bn,
int n, string &bl,
00172
bool cr=
false);
00173
virtual CTError writeBlocks(
int bn,
int n,
const string &bl,
00174
bool cr=
false);
00175
virtual CTError setPassword(
const string &pw);
00176
virtual void clearPassword();
00177 };
00178
#endif
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189 #define CTFS_SUPERBLOCK_FLAG_CRYPTED 0x0001
00190 #define CTFS_SUPERBLOCK_FLAG_READONLY 0x0002
00191
00192
00199 class CHIPCARD_API CTSuperBlock {
00200
private:
00201
bool _changed;
00202 string _mediumName;
00203
int _blockSize;
00204
int _blocks;
00205
int _mediumSize;
00206
unsigned char _versionMajor;
00207
unsigned char _versionMinor;
00208
unsigned int _flags;
00209
int _reservedBlocks;
00210
int _firstDirBlock;
00211 string _userName;
00212
00213
CTError _fromString(
const string &s);
00214
00215
public:
00220 CTSuperBlock();
00224 CTSuperBlock(
const string &block);
00225
00229 CTSuperBlock(
unsigned int mediumSize);
00230 ~CTSuperBlock();
00232
00242 const string mediumName()
const {
return _mediumName;};
00243
00249 int mediumSize() {
return _mediumSize;};
00250
00256 unsigned char versionMajor()
const {
return _versionMajor;};
00257
00264 unsigned char versionMinor()
const {
return _versionMinor;};
00265
00269 bool isCrypted()
const {
return _flags &
CTFS_SUPERBLOCK_FLAG_CRYPTED;};
00270
00276 bool isReadOnly()
const {
return _flags &
CTFS_SUPERBLOCK_FLAG_READONLY;};
00278
00288 int blockSize()
const {
return _blockSize;};
00289
00295 int blocks()
const {
return _blocks;};
00297
00308 int reservedBlocks()
const {
return _reservedBlocks;};
00309
00313 int firstDirBlock()
const {
return _firstDirBlock;};
00314
00318 const string userName()
const {
return _userName;};
00319
00323 bool changed()
const {
return _changed;};
00325
00333 void setMediumName(
const string &n) { _mediumName=n; _changed=
true;};
00334
00338 void setBlockSize(
int n) { _blockSize=n; _changed=
true;};
00339
00343 void setBlocks(
int n) { _blocks=n; _changed=
true;};
00344
00348 void setReservedBlocks(
int i) { _reservedBlocks=i;};
00349
00353 void setMediumSize(
int n) { _mediumSize=n; _changed=
true;};
00354
00358 void setVersion(
int mj,
int mn) {
00359 _versionMajor=mj;
00360 _versionMinor=mn;
00361 _changed=
true;};
00362
00366 void setIsCrypted(
bool b) {
00367
if (b)
00368 _flags|=
CTFS_SUPERBLOCK_FLAG_CRYPTED;
00369
else
00370 _flags&=~
CTFS_SUPERBLOCK_FLAG_CRYPTED;
00371 _changed=
true;};
00372
00376 void setIsReadOnly(
bool b) {
00377
if (b)
00378 _flags|=
CTFS_SUPERBLOCK_FLAG_READONLY;
00379
else
00380 _flags&=~
CTFS_SUPERBLOCK_FLAG_READONLY;
00381 _changed=
true;};
00382
00386 void setFirstDirBlock(
int i) { _firstDirBlock=i; _changed=
true;};
00387
00391 void setUserName(
const string &n) { _userName=n; _changed=
true;};
00392
00396 void setChanged(
bool b) { _changed=b;};
00398
00406 string toString();
00407
00411 string dump();
00413 };
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
#ifndef DOXYGEN
00427
00431
class CHIPCARD_API CTBlockManager {
00432
private:
00433
unsigned char _fat[
CTFILESYSTEM_FAT_LENGTH];
00434
int _blocks;
00435
bool _changed;
00436
00437
public:
00438 CTBlockManager();
00439 CTBlockManager(
int blocks,
const string &fat=
"");
00440 ~CTBlockManager();
00441
00442
bool changed()
const {
return _changed;};
00443
void setChanged(
bool b) { _changed=b;};
00444
00445
int allocateBlock(
int bl=-1);
00446
void freeBlock(
int bl);
00447
void freeChain(
int bn);
00448
int blocks(
int bl=-1);
00449
int freeBlocks();
00450
int blockAt(
int first,
int idx);
00451
00452
int nextBlock(
int bl);
00453
int previousBlock(
int bl);
00454
int lastBlock(
int bl);
00455
00456 string toString();
00457 };
00458
#endif // DOXYGEN
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
#ifndef DOXYGEN
00469
00474
class CHIPCARD_API CTDataBlockMedium:
public CTCryptedBlockMedium {
00475
friend class CTFileBase;
00476
00477
private:
00478
int _firstDataBlock;
00479 CTSuperBlock _superBlock;
00480 CTBlockManager _blockManager;
00481
bool _isMounted;
00482
00483
CTError _createMedium(
unsigned int mediumSize,
00484
const string &mediumname,
00485
const string &userName,
00486
const string &passwd);
00487
CTError _readFAT();
00488
CTError _writeFAT();
00489
CTError _readSuperBlock();
00490
CTError _writeSuperBlock();
00491
00492
protected:
00493 CTDataBlockMedium(
const CTCard &c);
00494
public:
00495
virtual ~CTDataBlockMedium();
00496
00497
virtual CTError readBlock(
int bn, string &bl);
00498
virtual CTError writeBlock(
int bn,
const string &bl);
00499
virtual CTError allocateBlock(
int &bn);
00500
virtual CTError freeBlock(
int bn);
00501
virtual CTError freeChain(
int bn);
00502
virtual CTError nextBlock(
int &bn);
00503
const CTSuperBlock &superBlock()
const {
return _superBlock;};
00504
virtual int blocks(
int bl=-1);
00505
virtual int freeBlocks();
00506
virtual int blockAt(
int first,
int idx);
00507
virtual int blockSize() const;
00508 virtual
int firstDirBlock();
00509 virtual
CTError mountMedium(const string &username="",
00510 const string &passwd="");
00511 virtual
CTError unmountMedium();
00512 virtual
CTError createMedium(
unsigned int mediumSize,
00513 const string &mediumname,
00514 const string &userName,
00515 const string &passwd);
00516
bool isReadOnly()
const {
return _superBlock.isReadOnly();};
00517
void setReadOnly(
bool b) { _superBlock.setIsReadOnly(b);};
00518
virtual int flush(
int maxb=0x800000)
throw (
class CTError);
00519
virtual void purge();
00520 };
00521
#endif // DOXYGEN
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
#ifndef DOXYGEN
00535
00539
class CHIPCARD_API CTDataFile {
00540
friend class CTDirectoryBase;
00541
friend class CTFileBase;
00542
private:
00543 string _buffer;
00544
int _block;
00545
int _pos;
00546
bool _dirty;
00547
bool _valid;
00548
int _allocatedSize;
00549
00550
int _firstBlock;
00551
CTPointer<CTDataBlockMedium> _medium;
00552
00553
CTError _readBlock();
00554
CTError _writeBlock();
00555
00556
protected:
00557 CTDataFile();
00558 CTDataFile(
CTPointer<CTDataBlockMedium> medium,
00559
int firstBlock=0);
00560
public:
00561 ~CTDataFile();
00562
00563
CTError seek(
int where);
00564
int position();
00565
unsigned char readChar();
00566 string readString(
int len);
00567
CTError writeChar(
unsigned char c);
00568
CTError writeString(
const string &s);
00569
CTError flush();
00570
CTError truncate();
00571
int firstBlock()
const {
return _firstBlock;};
00572
00573
int blocks();
00574
int allocatedSize();
00575
CTError appendBlock();
00576
00577 };
00578
#endif // DOXYGEN
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00597 class CHIPCARD_API CTDirEntry {
00598
private:
00599
bool _changed;
00600 string _name;
00601
unsigned int _attributes;
00602
unsigned int _size;
00603
int _firstBlock;
00604
int _parent;
00605
00606
int _indexInParent;
00607
00608
CTError _fromString(
const string &s);
00609
00610
public:
00614 enum Attributes {
00618 Attr_USED =0x0001,
00622 Attr_READ =0x0002,
00623
00627 Attr_WRITE =0x0004,
00628
00632 Attr_DIR =0x0008,
00633
00637 Attr_HIDDEN=0x0010
00638 };
00639
00650 CTDirEntry();
00651
00655 CTDirEntry(
int parent,
00656
const string &name,
00657
unsigned int attribs=Attr_USED,
00658
unsigned int size=0,
00659
int firstblock=-1);
00660
00664 CTDirEntry(
const string &s);
00665
00669 CTDirEntry(
int parent,
int indexInParent=-1);
00670 ~CTDirEntry();
00672
00681 const string &name()
const {
return _name;};
00682
00687 unsigned int attributes()
const {
return _attributes;};
00688
00694 int size()
const {
return _size;};
00696
00708 bool changed()
const {
return _changed;};
00713 int firstBlock()
const {
return _firstBlock;};
00714
00715
00722 int parent()
const {
return _parent;};
00723
00730 int indexInParent()
const {
return _indexInParent;};
00732
00740 void setAttributes(
unsigned int i) { _attributes=i; _changed=
true;};
00741
00745 void setSize(
unsigned int i) { _size=i; _changed=
true;};
00749 void setName(
const string &s) { _name=s; _changed=
true;};
00753 void setFirstBlock(
int i) { _firstBlock=i; _changed=
true;};
00757 void setParent(
int i) { _parent=i; _changed=
true;};
00761 void setIndexInParent(
int i) { _indexInParent=i;};
00765 void setChanged(
bool b) { _changed=b;};
00767
00775 string toString();
00779 string dump();
00781
00782 };
00783
00784
00785
00786
00787
00788
#ifndef DOXYGEN
00789
00793
class CHIPCARD_API CTDirectoryBase:
public CTDataFile {
00794
private:
00795 CTDirEntry _entry;
00796
int _currEntry;
00797
00798
CTError _readEntry(CTDirEntry &de,
int idx);
00799
int _findFreeEntry();
00800
int _findOrAddFreeEntry();
00801
00802
public:
00803 CTDirectoryBase();
00804 CTDirectoryBase(
CTPointer<CTDataBlockMedium> medium,
00805
int firstBlock=0);
00806 ~CTDirectoryBase();
00807
00808
CTError firstEntry(CTDirEntry &de);
00809
CTError nextEntry(CTDirEntry &de);
00810 CTDirEntry findEntry(
const string &name);
00811
00818
CTError writeEntry(CTDirEntry &de);
00819
00820 };
00821
#endif
00822
00823
00824
00825
00826
00827
00828
#ifndef DOXYGEN
00829
00833
class CHIPCARD_API CTFileBase {
00834
friend class CTFile;
00835
friend class CTDirectory;
00836
private:
00837
CTPointer<CTDataBlockMedium> _medium;
00838 string _path;
00839 CTDirEntry _entry;
00840
bool _isOpen;
00841 CTDataFile _data;
00842
00843 string _normalizeName(string n);
00844
CTError _createEntry(
const string &n,
00845
unsigned int attribs,
00846 CTDirEntry &fileEntry);
00847
00848
protected:
00849
const CTDirEntry &dirEntry()
const {
return _entry;};
00850 CTDirEntry path2Entry(
const string &path);
00851
CTError writeEntry(CTDirEntry &entry);
00852
00853 CTFileBase();
00854
public:
00855 CTFileBase(
CTPointer<CTDataBlockMedium> medium,
00856
const string &path);
00857
virtual ~CTFileBase();
00858
00859
bool isOpen()
const {
return _isOpen;};
00860
00861
virtual CTError openFile();
00862
virtual CTError closeFile();
00863
virtual CTError createFile(
unsigned int attribs=
00864 CTDirEntry::Attr_USED |
00865 CTDirEntry::Attr_READ |
00866 CTDirEntry::Attr_WRITE);
00867
virtual CTError removeFile();
00868
virtual CTError statFile(CTDirEntry &ent);
00869
CTError renameFile(
const string &n);
00870
00871
CTError truncate();
00872
CTError seek(
int where);
00873
int position();
00874
int size();
00875
unsigned char readChar();
00876 string readString(
int len);
00877
CTError writeChar(
unsigned char c);
00878
CTError writeString(
const string &s);
00879
CTError flush();
00880 };
00881
#endif // DOXYGEN
00882
00883
00884
00895 class CHIPCARD_API CTCardFS:
public CTDataBlockMedium {
00896
private:
00897
public:
00912 CTCardFS(
const CTCard &c);
00913
virtual ~CTCardFS();
00915
00941
virtual CTError mountMedium(
const string &username=
"",
00942
const string &passwd=
"");
00943
00950 virtual CTError unmountMedium() {
00951
return CTDataBlockMedium::unmountMedium();};
00952
00966
virtual CTError createMedium(
unsigned int mediumSize,
00967
const string &mediumName,
00968
const string &userName,
00969
const string &passwd);
00971
00985 virtual int blocks() {
return CTDataBlockMedium::blocks(-1);};
00986
00994 virtual int freeBlocks() {
return CTDataBlockMedium::freeBlocks();};
00995
01039 virtual int blockSize()
const {
return CTDataBlockMedium::blockSize();};
01040
01049 const CTSuperBlock &superBlock()
const {
01050
return CTDataBlockMedium::superBlock();};
01051
01058 bool isReadOnly()
const {
return CTDataBlockMedium::isReadOnly();};
01059
01066 void setReadOnly(
bool b) { CTDataBlockMedium::setReadOnly(b);};
01068
01095 virtual int flush(
int maxb=0x800000)
throw (
class CTError) {
01096
return CTDataBlockMedium::flush(maxb);};
01097
01104 virtual void purge() { CTDataBlockMedium::purge();};
01106 };
01107
01108
01109
01114 class CHIPCARD_API CTFile:
private CTFileBase {
01115 CTFileBase _fb;
01116
01117
public:
01122
01123 CTFile();
01124
01130 CTFile(
CTPointer<CTCardFS> medium,
01131
const string &path);
01132 ~CTFile();
01134
01146 CTError openFile() {
return CTFileBase::openFile();};
01147
01153 CTError closeFile() {
return CTFileBase::closeFile();};
01159 CTError createFile(
unsigned int attribs=
01160 CTDirEntry::Attr_USED |
01161 CTDirEntry::Attr_READ |
01162 CTDirEntry::Attr_WRITE){
01163
return CTFileBase::createFile(attribs|CTDirEntry::Attr_USED);
01164 };
01166
01179 CTError removeFile() {
return CTFileBase::removeFile();};
01180
01187 CTError renameFile(
const string &n) {
return CTFileBase::renameFile(n);};
01188
01195 CTError statFile(CTDirEntry &ent) {
return CTFileBase::statFile(ent);};
01196
01201 CTError truncateFile() {
return CTFileBase::truncate();};;
01203
01219 CTError seek(
int where) {
return CTFileBase::seek(where);};
01220
01226 int position() {
return CTFileBase::position();};
01228
01241 unsigned char readChar() {
return CTFileBase::readChar();};
01242
01249 string readString(
int len);
01250
01258 CTError writeChar(
unsigned char c) {
return CTFileBase::writeChar(c);};
01259
01264 CTError writeString(
const string &s) {
return CTFileBase::writeString(s);};
01265
01272 CTError flush() {
return CTFileBase::flush();};
01274 };
01275
01276
01277
01278
01279
01285 class CHIPCARD_API CTDirectory:
private CTFileBase {
01286
01287
public:
01288 CTDirectory();
01289 CTDirectory(
CTPointer<CTCardFS> medium,
01290
const string &path);
01291 ~CTDirectory();
01292
01293
CTError openDirectory();
01294 CTError closeDirectory() {
return CTFileBase::closeFile();};
01295 CTError createDirectory(
unsigned int attribs=
01296 CTDirEntry::Attr_USED |
01297 CTDirEntry::Attr_READ |
01298 CTDirEntry::Attr_WRITE){
01299
return CTFileBase::createFile(attribs|CTDirEntry::Attr_DIR);
01300 };
01301
01302
CTError removeDirectory();
01303 CTError renameDirectory(
const string &n) {
01304
return CTFileBase::renameFile(n);
01305 };
01306
01307
CTError firstEntry(CTDirEntry &de);
01308
CTError nextEntry(CTDirEntry &de);
01309
CTError entry(CTDirEntry &de,
int idx);
01310
01311 };
01312
01313
01314
01315
#endif
01316
01317
01318