View Single Post
Author Message
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-01-2010 , 09:39   Reading nVault Files
Reply With Quote #1

I wrote this code for those who may have the need to iterate through all entries in a nVault file. Please let me know if you notice any errors or have any requests/questions.

The below code will display all entries in the server console in the below format.
Code:
nVault | Magic=1851149396 Version=512 Entries=2

Entry=1 KeyLen=22 ValLen=29 TimeStamp=1272719603
Key="This is an example key"
Val="This is the data for this key"
 
Entry=2 KeyLen=27 ValLen=9 TimeStamp=1272719618
Key="This is another example key"
Val="More data"
PHP Code:
#include <amxmodx>
#include <amxmisc>

// - From nvault.h -
#define VAULT_MAGIC   0x6E564C54     //nVLT
#define VAULT_VERSION 0x0200         //second version

/*  
    nVault File Format
 
    VAULT_MAGIC       (uint32)
    VAULT_VERSION     (uint16)
    ENTRIES           (int32)
    [
        TimeStamp     (int32)
        KeyLen        (uint8)
        ValLen        (uint16)
        KeyData       ([])
        ValData       ([])
    ]                                 
*/

new const Version[] = "0.1";

const 
MaxKeyLen  255;     //Max 255
const MaxValLen  512;     //Max 65535
const DataBuffer 128;     //Make this the greater of (MaxKeyLen / 4) or (MaxValLen / 4)

public plugin_init() 
{
    
register_plugin"nVault File Reader" Version "bugsy" );
    
    
ReadVault"TheVaultFile" );
}

public 
ReadVault( const szVault[] )
{
    new 
szFile64 ] , iFile;
    new 
iVaultMagic iVaultVersion iVaultEntries;
    new 
iKeyLen iValLen iTimeStamp;
    new 
szKeyMaxKeyLen ] , szValMaxValLen ] , RawDataDataBuffer ];
    
    
formatexszFileget_datadirszFile charsmaxszFile ) ) ] , charsmaxszFile ) , "/vault/%s.vault" szVault );
    
    
iFile fopenszFile "rb" );

    if ( !
iFile )
        return 
0;
    
    
// Vault Magic
    
fread_rawiFile RawData BLOCK_INT );
    
iVaultMagic RawData];
    
    if ( 
iVaultMagic != VAULT_MAGIC )
        
set_fail_state"Error reading nVault: Vault Magic" );
    
    
// Vault Version
    
fread_rawiFile RawData BLOCK_SHORT );
    
iVaultVersion RawData] & 0xFFFF;
    
    if ( 
iVaultVersion != VAULT_VERSION )
        
set_fail_state"Error reading nVault: Vault Version" );
        
    
// Vault Entries
    
fread_rawiFile RawData BLOCK_INT );
    
iVaultEntries RawData];

    
server_print"nVault | Magic=%d Version=%d Entries=%d" iVaultMagic iVaultVersion iVaultEntries );
    
server_print" " );

    for ( new 
iEntry iEntry iVaultEntries iEntry++ )
    {
        
// TimeStamp
        
fread_rawiFile RawData BLOCK_INT );
        
iTimeStamp RawData];
        
        
// Key Length
        
fread_rawiFile RawData BLOCK_BYTE );
        
iKeyLen RawData] & 0xFF;
        
        
// Val Length
        
fread_rawiFile RawData BLOCK_SHORT );
        
iValLen RawData] & 0xFFFF;
        
        
// Key Data
        
fread_rawiFile RawData iKeyLen BLOCK_CHAR );
        
ReadStringszKey iKeyLen charsmaxszKey ) , RawData );
    
        
// Val Data
        
fread_rawiFile RawData iValLen BLOCK_CHAR );
        
ReadStringszVal iValLen charsmaxszVal ) , RawData );

        
server_print"Entry=%d KeyLen=%d ValLen=%d TimeStamp=%d" iEntry iKeyLen iValLen iTimeStamp );
        
server_print"Key=^"%s^"" szKey );
        
server_print"Val=^"%s^"" szVal );
        
server_print" " );
    }
    
    
fcloseiFile );
    
    return 
iVaultEntries;
}

ReadStringszDestString[] , iLen iMaxLen SourceData[] )
{
    new 
iStrPos = -1;
    new 
iRawPos 0;
    
    while ( ( ++
iStrPos iLen ) && ( iStrPos iMaxLen ) && ( iRawPos DataBuffer ) )
    {
        
szDestStringiStrPos ] = ( SourceDataiRawPos ] >> ( ( iStrPos ) * ) ) & 0xFF;
        
        if ( 
iStrPos && ( ( iStrPos ) == ) )
            
iRawPos++
    }
    
    
szDestStringiStrPos ] = EOS;

__________________

Last edited by Bugsy; 09-06-2010 at 23:07.
Bugsy is offline