This site is a testing version, but all data is shared with the live forum.


Raised This Month: $ Target: $400
 0% 

Reading nVault Files


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
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
 



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 19:06.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode