Created
August 28, 2018 10:47
-
-
Save alphp/146e5b4d521a8cf7b75cf811b303c1ec to your computer and use it in GitHub Desktop.
DD-WRT nvram dump
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class nvram { | |
public static function dump (string $nvramfile) { | |
$nvram = file_get_contents($nvramfile); | |
$header = unpack('a6header/v1record_count', $nvram); | |
if ($header['header'] !== 'DD-WRT') { | |
die('nvram format is not compatible); | |
} | |
$record_count = $header['record_count']; | |
$nvram = substr($nvram, 8); | |
$records = []; | |
while (strlen($nvram) and count($records) < $record_count) { | |
$name_size = ord($nvram[0]); | |
$record_name = substr($nvram, 1, $name_size); | |
$record_size = current(unpack('v', $nvram, $name_size + 1)); | |
$records[$record_name] = substr($nvram, $name_size + 3, $record_size); | |
$nvram = substr($nvram, $name_size + $record_size + 3); | |
} | |
return $records; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment