@echo off
if "%~dpnx1"=="" goto usage
if not exist "%~dp0dummywad.bin" goto main_error1
if not exist "%~dp0common-key.bin" goto main_error2

:scriptStart
%~d1
cd "%~dp1"
set wwpacker_tik=:
set wwpacker_tmd=:
for %%k in ("%~dp0waddatainfo.exe") do set wwpacker_cmd=%%~fsk
for %%k in ("%ComSpec%") do set wwpacker_findcmd=%%~dpk
for %%k in ("%wwpacker_findcmd%find.exe") do set wwpacker_findcmd=%%~fsk
for /F "tokens=1,2" %%k in ('%wwpacker_cmd% /n "%~dpnx1" ^| %wwpacker_findcmd% /I " info of "') do if /I '%%k'=='Ticket' set wwpacker_tik=%~dpnx1
set wwpacker_cmd=
set wwpacker_findcmd=
for %%k in ("*.tmd","tmd*.*") do set wwpacker_tmd=%%~dpnxk
if "%wwpacker_tik%"==":" goto main_error3
if "%wwpacker_tmd%"==":" goto main_error4
set wwpacker_sameDir=0
if exist "common-key.bin" if exist "dummywad.bin" if exist "bfgr_wadunpacker.exe" if exist "cygcrypto-0.9.8.dll" if exist "cygwin1.dll" set wwpacker_sameDir=1
if %wwpacker_sameDir% EQU 0 (
copy /Y /b "%~dp0common-key.bin"
copy /Y /b "%~dp0bfgr_wadunpacker.exe"
copy /Y /b "%~dp0cygcrypto-0.9.8.dll"
copy /Y /b "%~dp0cygwin1.dll"
)

for %%k in ("%~dp0htool.exe") do set wwpacker_htcmd=%%~fsk
for %%k in ("%ComSpec%") do set wwpacker_findcmd=%%~dpk
for %%k in ("%wwpacker_findcmd%find.exe") do set wwpacker_findcmd=%%~fsk
for /F "tokens=3" %%k in ('%wwpacker_htcmd% /d /b0x1de /e0x1e0 "%wwpacker_tmd%" ^| %wwpacker_findcmd% " : "') do set /A wwpacker_contentTotal=0x%%k
for /F "tokens=3" %%k in ('%wwpacker_htcmd% /d /b0x1bf /e0x1cf "%wwpacker_tik%" ^| %wwpacker_findcmd% " : "') do set wwpacker_titleKey=%%k
for /F "tokens=3" %%k in ('%wwpacker_htcmd% /d /b0x1dc /e0x1e4 "%wwpacker_tik%" ^| %wwpacker_findcmd% " : "') do set wwpacker_title=%%k
set /A wwpacker_tmdSize=0x1e4 + %wwpacker_contentTotal% * 0x24

:findContent
set /A wwpacker_infoOffB=0x1e4
:findContentBegin
if %wwpacker_contentTotal% LEQ 0 goto findContentEnd
set /A wwpacker_infoOffE=%wwpacker_infoOffB% + 0x24
for /F "tokens=3" %%k in ('%wwpacker_htcmd% /d /b%wwpacker_infoOffB% /e%wwpacker_infoOffE% "%wwpacker_tmd%" ^| %wwpacker_findcmd% " : "') do set wwpacker_content=%%k
if exist "%wwpacker_content:~0,8%" goto decryptContent
:nextContent
set /A wwpacker_contentTotal=%wwpacker_contentTotal% - 1
set /A wwpacker_infoOffB=%wwpacker_infoOffE%
goto findContentBegin
:findContentEnd

for %%k in ("%wwpacker_tik%") do set /A wwpacker_tikFileSize=%%~zk
set /A wwpacker_tikType=%wwpacker_tikFileSize% %% 0x2a4
set HToolFile=%wwpacker_htcmd%
set FindCommand=%wwpacker_findcmd%
set dumpSource=%wwpacker_tik%
if %wwpacker_tikType% EQU 0 (
set /A dumpSourceStart=%wwpacker_tikFileSize% - 0x2a4
set /A dumpSourceEnd=%wwpacker_tikFileSize%
) else (
set /A dumpSourceStart=0
set /A dumpSourceEnd=0x2a4
)
set dumpTarget=decrypted_%wwpacker_title%\title.tik
set dumpTargetStart=0
set returnLabel=copyTikEndDP
if exist "%dumpTarget%" del "%dumpTarget%"
goto dumpPart
:copyTikEndDP
set dumpSource=%wwpacker_tmd%
set dumpSourceStart=0
set dumpSourceEnd=%wwpacker_tmdSize%
set dumpTarget=decrypted_%wwpacker_title%\title.tmd
set dumpTargetStart=0
set returnLabel=copyTmdEndDP
if exist "%dumpTarget%" del "%dumpTarget%"
goto dumpPart
:copyTmdEndDP
set HToolFile=
set FindCommand=
set dumpSource=
set dumpSourceStart=
set dumpSourceEnd=
set dumpTarget=
set dumpTargetStart=
set returnLabel=
if exist "%~dp0cert.sys" copy /Y /b "%~dp0cert.sys" "decrypted_%wwpacker_title%\title.cert"
if exist "decrypt.log" type "decrypt.log"
set wwpacker_htcmd=
set wwpacker_findcmd=
set wwpacker_tik=
set wwpacker_tmd=
set wwpacker_contentTotal=
set wwpacker_titleKey=
set wwpacker_title=
set wwpacker_tmdSize=
set wwpacker_content=
set wwpacker_contentSize=
set wwpacker_infoOffB=
set wwpacker_infoOffE=
set wwpacker_paddingLen=
set wwpacker_padding=
set wwpacker_tikFileSize=
set wwpacker_tikType=
if %wwpacker_sameDir% EQU 0 (
del "common-key.bin"
del "bfgr_wadunpacker.exe"
del "cygcrypto-0.9.8.dll"
del "cygwin1.dll"
)
if exist "content.bin" del "content.bin"
if exist "decrypt.log" del "decrypt.log"
set wwpacker_sameDir=
goto end


:decryptContent
for %%k in ("%wwpacker_content:~0,8%") do set /A wwpacker_contentSize=(%%~zk + 63) / 64 * 64
set hexValue=
set decValue=%wwpacker_contentSize%
set hexLength=4
set returnLabel=decryptContentD2H
goto decToHex
:decryptContentD2H
set wwpacker_contentSize=%hexValue%
set hexValue=
set decValue=
set hexLength=
set returnLabel=
copy /Y /b "%~dp0dummywad.bin" "content.bin"
%wwpacker_htcmd% /p /b0x18 "content.bin" %wwpacker_contentSize% > NUL
%wwpacker_htcmd% /p /b0xbff "content.bin" %wwpacker_titleKey% > NUL
%wwpacker_htcmd% /p /b0xc1c "content.bin" %wwpacker_title% > NUL
%wwpacker_htcmd% /p /b0xe8c "content.bin" %wwpacker_title% > NUL
%wwpacker_htcmd% /p /b0xee4 "content.bin" %wwpacker_content% > NUL
set paddingByte=00
set alignSize=64
set HToolFile=%wwpacker_htcmd%
set paddingFile=content.bin
set paddingStartOffset=0xf08
set returnLabel=tmdPaddingEndAP
goto addPadding
:tmdPaddingEndAP
copy /Y /b "content.bin" + "%wwpacker_content:~0,8%"
for %%k in ("content.bin") do set /A paddingStartOffset=%%~zk
set returnLabel=contentPaddingEndAP
goto addPadding
:contentPaddingEndAP
set paddingByte=
set alignSize=
set HToolFile=
set paddingFile=
set paddingStartOffset=
set returnLabel=
if exist "%wwpacker_title%" rd /s /q "%wwpacker_title%"
bfgr_wadunpacker.exe "content.bin"
if not exist "%wwpacker_title%\0000%wwpacker_content:~8,4%.app" (
	echo Error! Cannot decrypt "%wwpacker_content:~0,8%".
	echo "%wwpacker_content:~0,8%" =^> "0000%wwpacker_content:~8,4%.app" [FAIL]>> decrypt.log
	goto nextContent
)
if not exist "decrypted_%wwpacker_title%" md "decrypted_%wwpacker_title%"
move /y "%wwpacker_title%\0000%wwpacker_content:~8,4%.app" "decrypted_%wwpacker_title%\0000%wwpacker_content:~8,4%.app"
rd /s /q "%wwpacker_title%"
echo "%wwpacker_content:~0,8%" =^> "0000%wwpacker_content:~8,4%.app" >> decrypt.log
goto nextContent


REM dumpPart
REM [IN] %HToolFile% - the short-named full path string of HTool application
REM [IN] %FindCommand% - the short-named full path string of find command
REM [IN] %dumpSource% - the source file to be dumped (no quotes)
REM [IN] %dumpSourceStart% - the start offset of source file
REM [IN] %dumpSourceEnd% - the end offset of source file (exclusive)
REM [IN] %dumpTarget% - the target file having the dumped data written in (no quotes)
REM [IN] %dumpTargetStart% - the start offset of target file
REM [IN] %returnLabel% - the label to return when this function ends
:dumpPart
set /A dumpSourceSize=%dumpSourceEnd% - %dumpSourceStart%
:copyingDumpedPartStart
if %dumpSourceSize% LEQ 0 goto copyingDumpedPartEnd
if %dumpSourceSize% GTR 506 ( set /A copyingDumpSize=506
) else set /A copyingDumpSize=%dumpSourceSize%
set /A dumpSourceEnd=%dumpSourceStart% + %copyingDumpSize%
for /F "tokens=3" %%k in ('%HToolFile% /d /b%dumpSourceStart% /e%dumpSourceEnd% "%dumpSource%" ^| %FindCommand% " : "') do set dumpedDataString=%%k
if not exist "%dumpTarget%" echo.> "%dumpTarget%"
%HToolFile% /p /b%dumpTargetStart% "%dumpTarget%" %dumpedDataString% > NUL
set /A dumpTargetStart+=%copyingDumpSize%
set /A dumpSourceStart=%dumpSourceEnd%
set /A dumpSourceSize-=%copyingDumpSize%
goto copyingDumpedPartStart
:copyingDumpedPartEnd
set dumpSourceSize=
set copyingDumpSize=
set dumpedDataString=
goto %returnLabel%


REM addPadding
REM [IN] %paddingByte% - padding byte e.g. FF
REM [IN] %alignSize% - the size to be aligned
REM [IN] %HToolFile% - the short-named full path string of HTool application
REM [IN] %paddingFile% - the file to add padding (no quotes)
REM [IN] %paddingStartOffset% - the start offset of padding
REM [IN] %returnLabel% - the label to return when this function ends
:addPadding
set /A paddingEndOffset=(%paddingStartOffset% + %alignSize% - 1) / %alignSize% * %alignSize%
if %paddingEndOffset% GTR %paddingStartOffset% %HToolFile% /p /b%paddingStartOffset% /e%paddingEndOffset% "%paddingFile%" %paddingByte% > NUL
set paddingEndOffset=
goto %returnLabel%


REM decToHex
REM [IN] %decValue% - decimal value
REM [IN] %hexLength% - the length of output %hexValue% in byte
REM [IN] %returnLabel% - the label to return when this function ends
REM [OUT] %hexValue% - hexadecimal value string without '0x' at the beginning
:decToHex
if %hexLength% LEQ 0 goto %returnLabel%
set /A hexBitLength=8
:decToHexByte
set /A dec4Bits="%decValue% & 0xF"
if %dec4Bits% EQU 10 ( set hexValue=A%hexValue%
) else if %dec4Bits% EQU 11 ( set hexValue=B%hexValue%
) else if %dec4Bits% EQU 12 ( set hexValue=C%hexValue%
) else if %dec4Bits% EQU 13 ( set hexValue=D%hexValue%
) else if %dec4Bits% EQU 14 ( set hexValue=E%hexValue%
) else if %dec4Bits% EQU 15 ( set hexValue=F%hexValue%
) else set hexValue=%dec4Bits%%hexValue%
set /A hexBitLength=%hexBitLength% - 4
set /A decValue="%decValue% >> 4"
set dec4Bits=
if %hexBitLength% GTR 0 goto decToHexByte
set hexBitLength=
set /A hexLength=%hexLength% - 1
goto decToHex


:main_error1
echo Error! Missing "%~dp0dummywad.bin".
goto end


:main_error2
echo Error! Missing "%~dp0common-key.bin".
goto end


:main_error3
echo Error! Missing ticket file.
goto end


:main_error4
echo Error! Missing tmd file.
goto end


:usage
echo Usage:
echo Drag and drop the tik/cetk file onto the icon of this batch script.
echo.


:end
pause
echo.
