Skip to content

Instantly share code, notes, and snippets.

@alphp
Created August 28, 2018 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alphp/146e5b4d521a8cf7b75cf811b303c1ec to your computer and use it in GitHub Desktop.
Save alphp/146e5b4d521a8cf7b75cf811b303c1ec to your computer and use it in GitHub Desktop.
DD-WRT nvram dump
<?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