@echo off
set base_dir="%~dp0"


if "%1"=="-x" (
goto pkg_check
) else (
if "%1"=="-l" (
goto pkg_check
) else (
echo "unknown option!"
exit /b 1
)
)
)

:pkg_check
if not '%2'=='' (
if exist %2 goto args_ok
)
echo "no package supplied"
exit /b 1

:args_ok
set pkg=%2

setlocal enabledelayedexpansion
set count=1
set leadsize=96
set /a o=%leadsize%+8
for /f "tokens=2-9" %%a in ('"%base_dir%od -j %o% -N 8 -t u1 %pkg%"') do (
if !count!==2 goto endod
set var1=%%a
set var2=%%b
set var3=%%c
set var4=%%d
set var5=%%e
set var6=%%f
set var7=%%g
set var8=%%h
set /a count=!count!+1
)
:endod

set /a il=256 * ( 256 * ( 256 * %var1% + %var2% ) + %var3% ) + %var4%
set /a dl=256 * ( 256 * ( 256 * %var5% + %var6% ) + %var7% ) + %var8%

endlocal & set o=%o% & set il=%il% & set dl=%dl%

set /a sigsize=8 + 16 * %il% + %dl%
set /a o=%o% + %sigsize% + ( 8 - ( %sigsize% %% 8 ) ) %% 8 + 8
setlocal enabledelayedexpansion
set count=1
for /f "tokens=2-9" %%a in ('"%base_dir%od -j %o% -N 8 -t u1 %pkg%"') do (
if !count!==2 goto end2od
set var1=%%a
set var2=%%b
set var3=%%c
set var4=%%d
set var5=%%e
set var6=%%f
set var7=%%g
set var8=%%h
set /a count=!count!+1
)
:end2od

set /a il=256 * ( 256 * ( 256 * %var1% + %var2% ) + %var3% ) + %var4%
set /a dl=256 * ( 256 * ( 256 * %var5% + %var6% ) + %var7% ) + %var8%

endlocal & set il=%il% & set dl=%dl%

set /a hdrsize=8 + 16 * %il% + %dl%
set /a o=%o% + %hdrsize%

if "%1"=="-x" (
%base_dir%dd if=%pkg% ibs=%o% skip=1 2>nul  | %base_dir%gzip -d | %base_dir%cpio -i --make-directories --preserve-modification-time 2>nul
) else (
if "%1"=="-l" (
%base_dir%dd if=%pkg% ibs=%o% skip=1 2>nul  | %base_dir%gzip -d | %base_dir%cpio -t
)
)

