SFS Algos
Posted: Sun Apr 05, 2009 2:47 pm
Hey,
I'm trying to implement the SFS system. Could some of you point me in direction of some implementations of SFS, since the algo's section is still not there in the draft.
um.... and besides the point a bit, if there are no algo's, Here are a few functions I came up with for SFS:
It definately aint the best way to do it, perhaps pretty much the worst , but it does work, as tested against a SFS created by combuster's vdisk tool.
I'm trying to implement the SFS system. Could some of you point me in direction of some implementations of SFS, since the algo's section is still not there in the draft.
um.... and besides the point a bit, if there are no algo's, Here are a few functions I came up with for SFS:
Code: Select all
#include "../includes/basedef.h"
#include "../includes/hdd.h"
#include "../includes/sfs.h"
extern void printNum(uint32 num);
extern void puts(char* s);
uint32 readInt(struct hddSector* s,int i);
uint32 fs_sizeRB,fs_sizeDA,fs_sizeIA,fs_sizeTot;
void mountDisk(){
struct hddSector mbr;
read28(0,1,&mbr);
if(mbr.data[0x1AC]==0x53 && mbr.data[0x1AD]==0x46 && mbr.data[0x1AE]==0x53){
fs_sizeDA=readInt(&mbr,0x19C);
fs_sizeIA=readInt(&mbr,0x1A4);
fs_sizeTot=readInt(&mbr,0x1B0);
fs_sizeRB=readInt(&mbr,0x1B8);
}
else{
return;
}
}
int streql(char* s1,char* s2){
int i;
for(i=0;s1[i];i++);
int s1len=i;
for(i=0;s2[i];i++);
int s2len=i;
if(s1len!=s2len) return 0;
for(i=0;i<s1len;i++){
if(s1[i]!=s2[i]) return 0;
}
return 1;
}
int dirExists(char *s){
//Check if directory exists
struct hddSector sect;
uint32 lbaIndex=fs_sizeTot-fs_sizeIA/512;
uint32 skipRecords=0;
if(fs_sizeIA%512) {
lbaIndex--;
skipRecords=8-(fs_sizeIA%512)/64;
}
uint32 curSector=0;
int foundDir=0;
while((lbaIndex+curSector)<fs_sizeTot){
read28(lbaIndex+curSector,1,§);
//Parse
char dirname[200]="";
int i,j=0,conti=0;
for(i=0;i<8;i++){
if(curSector==0&&skipRecords){ skipRecords--; continue; }
if(sect.data[i*64]==0x11){
for(j=0;j<54;j++){
dirname[j]=sect.data[j+0x0A+i*64];
if(dirname[j]==0) break;
}
conti=sect.data[1+i*64];
if(!conti){
//Check if dir name is correct
if(streql(s,dirname)){
foundDir=1;
}
}
}
else{
if(conti&&(sect.data[i*64]>=0x20)&&(sect.data[i*64]<=0xFF)){
//This is a continuation sector
int k;
for(k=0;k<64&&j<200;j++,k++){
dirname[j]=sect.data[k+i*64];
if(dirname[j]==0) break;
}
conti--;
if(!conti){
//Check if dir name is correct
if(streql(s,dirname)){
foundDir=1;
}
}
}
}
if(foundDir) break;
}
if(foundDir) break;
curSector++;
}
if(foundDir){
return 1;
}
else{
return 0;
}
}
int strFileInDirectory(char *filename,char *dirname){
int i;
for(i=0;filename[i];i++);
i--;
for(;(filename[i]!='/')&&(i>=0);i--);
if(i>0) i--;
int dirlen;
for(dirlen=0;dirname[dirlen];dirlen++);
dirlen--;
if(dirlen!=i) return 0;
for(;i>=0;i--){
if(filename[i]!=dirname[i]) return 0;
}
return 1;
}
int openDir(char* dirname,struct dirhandle* res){
if(!dirExists(dirname)) return 0;
int i;
for(i=0;dirname[i];i++) res->dirname[i]=dirname[i];
uint32 lbaIndex=fs_sizeTot-fs_sizeIA/512;
uint32 skipRecords=0;
if(fs_sizeIA%512) {
lbaIndex--;
skipRecords=8-(fs_sizeIA%512)/64;
}
res->curlba=lbaIndex;
res->record=skipRecords;
}
int getNextDirEntry(char* buf,struct dirhandle* res){
uint32 lbaIndex=res->curlba;
int i=res->record; // Pointing to the record from where to start reading
int conti=0;
struct hddSector sect;
while((lbaIndex)<fs_sizeTot){
read28(lbaIndex,1,§);
//Parse
char filename[300];
int j=0,conti=0;
for(;i<8;i++){
if(sect.data[i*64]==0x12){ //Regular file
for(j=0;j<30;j++){
filename[j]=sect.data[j+0x22+i*64];
if(filename[j]==0) break;
}
conti=sect.data[1+i*64];
if(!conti){
//Check if file is in directory
if(strFileInDirectory(filename,res->dirname)){
//Copy
for(j=0;filename[j];j++) buf[j]=filename[j];
if(i==7){ res->curlba=lbaIndex+1; res->record=0; }
else{ res->curlba=lbaIndex; res->record++; }
return 1;
}
}
}
else{
if(conti&&(sect.data[i*64]>=0x20)&&(sect.data[i*64]<=0xFF)){
//This is a continuation sector (for a file, cause conti is set only for files)
int k;
for(k=0;k<64&&j<300;j++,k++){
filename[j]=sect.data[k+i*64];
if(filename[j]==0) break;
}
conti--;
if(!conti){
if(strFileInDirectory(filename,res->dirname)){
//Copy
for(j=0;filename[j];j++) buf[j]=filename[j];
if(i==7){ res->curlba=lbaIndex+1; res->record=0; }
else{ res->curlba=lbaIndex; res->record++; }
return 1;
}
}
}
}
}
i=0;
lbaIndex++;
}
return 0; //Did not find any files
}
uint32 readInt(struct hddSector* s,int i){
return MAKEDWORD(MAKEWORD(s->data[i+3],s->data[i+2]),MAKEWORD(s->data[i+1],s->data[i]));
}
Code: Select all
//Usage
char filename[300];
struct dirhandle s;
openDir("sample",&s);
while(getNextDirEntry(filename,&s)){
puts("Got a filename: ");
puts(filename);
putch('\n');
}