Skip to content

Instantly share code, notes, and snippets.

@sredna
Last active June 13, 2023 13:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sredna/45e510f260a824f6b144e523f3c31fc1 to your computer and use it in GitHub Desktop.
Save sredna/45e510f260a824f6b144e523f3c31fc1 to your computer and use it in GitHub Desktop.
!ifndef FSContainerExists
!macro _FSContainerExists _a _b _t _f ; Directory, UNC share or drive root
IfFileExists `${_b}\*` `${_t}` `${_f}`
!macroend
!define FSContainerExists `"" FSContainerExists`
!endif
!ifndef FSDirectoryExists
!macro _FSDirectoryExists _a _b _t _f ; Directory or UNC share
IfFileExists `${_b}\?` `${_t}` `${_f}`
!macroend
!define FSDirectoryExists `"" FSDirectoryExists`
!endif
!ifndef FSFileExists
!macro _FSFileExists _a _b _t _f ; Valid non-directory file
!insertmacro _LOGICLIB_TEMP
System::Call `kernel32::GetFileAttributes(ts)i.s` `${_b}` ; TODO: Should really use FindFirstFile here to be compatible with IfFileExists
Pop $_LOGICLIB_TEMP
IntOp $_LOGICLIB_TEMP $_LOGICLIB_TEMP & 0x50 ; FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_DEVICE
IntCmp $_LOGICLIB_TEMP 0 `${_t}` `${_f}` `${_f}`
!macroend
!define FSFileExists `"" FSFileExists`
!endif
!ifndef IsFSRoot
!macro _IsFSRoot _a _b _t _f ; UNC share or drive root
IfFileExists `${_b}\.` `${_f}` ; Not a file, directory nor UNC server?
!insertmacro _FSContainerExists `${_a}` `${_b}` `${_t}` `${_f}` ; UNC share or drive root?
!macroend
!define IsFSRoot `"" IsFSRoot`
!endif
!macro PathGetFilename outvar path
Push "${path}"
Call PathGetFilename
Pop ${outvar}
!macroend
Function PathGetFilename
Exch $1
Push $2
Push $3
StrCpy $2 ""
loop:
IntOp $2 $2 - 1
StrCpy $0 $1 1 $2
StrCmp $0 "" done
StrCmp $0 '\' +3
StrCmp $0 '/' +2
Goto loop
IntOp $2 $2 + 1
IntCmp $2 0 "" +2 +2
StrCpy $1 "" ; Ended with slash, return empty string
StrCpy $1 $1 "" $2
done:
Pop $3
Pop $2
Exch $1
FunctionEnd
!define PathAppend "!insertmacro PathAppend "
!macro PathAppend OutVar Base Append
!if "${OutVar}" == ""
Function ${Base}PathAppend
Exch $1
Push $0
Exch 2
Exch $2
StrCpy $0 $1 1 -1
StrCmp $0 "" a
StrCmp $0 '\' a
StrCmp $0 '/' a
StrCpy $2 "\$2"
a: StrCpy $0 $1$2
Pop $2
Pop $1
Exch $0
FunctionEnd
!else
Push "${Append}"
Push "${Base}"
Call PathAppend
Pop ${OutVar}
!endif
!macroend
Function PathRemoveQuotes
Exch $0
Push $1
StrCpy $1 $0 1
StrCmp $1 '"' 0 done
StrCpy $1 $0 1 -1
StrCmp $1 '"' 0 done
StrCpy $0 $0 -1 1
done:
Pop $1
Exch $0
FunctionEnd
!define PathRemoveArgsAndQuotes "!insertmacro PathRemoveArgsAndQuotes "
!macro PathRemoveArgsAndQuotes out path
Push `${path}`
Call PathRemoveArgsAndQuotes
Pop ${out}
!macroend
Function PathRemoveArgsAndQuotes
Exch $0
Push $1 ; Pos
Push $2
Push $3 ; Quote or space
StrCpy $3 " "
StrCpy $1 ""
StrCpy $2 $0 1
StrCmp $2 '"' 0 +3
IntOp $1 $1 + 1
StrCpy $3 $2
loop:
StrCpy $2 $0 1 $1
StrCmp $2 "" stop
StrCmp $2 $3 stop
IntOp $1 $1 + 1
Goto loop
stop:
StrCmp $3 '"' 0 +3
StrCpy $2 1
IntOp $1 $1 - 1
StrCpy $0 $0 $1 $2
Pop $3
Pop $2
Pop $1
Exch $0
FunctionEnd
!macro DbgPrintMemToLog addr size
Push ${size}
Push ${addr}
Call DbgPrintMemToLog
!macroend
Function DbgPrintMemToLog
System::Store S
Pop $0 ; Addr
Pop $9 ; Size
IntPtrOp $9 $0 + $9
loop:
IntPtrCmp $0 $9 end "" end
!if ${NSIS_PTR_SIZE} > 4
IntFmt $1 "%I64.16X " $0
!else
IntFmt $1 "%.8X " $0
!endif
StrCpy $5 " "
StrCpy $8 0
loopline:
IntOp $8 $8 + 1
IntPtrCmp $0 $9 stopline "" stopline
System::Call *$0(&i1.r7)
IntPtrOp $0 $0 + 1
StrCpy $6 '.'
IntCmp $7 32 "" +3 ""
IntCmp $7 127 +2 "" +2
IntFmt $6 "%c" $7
StrCpy $5 "$5$6"
IntFmt $7 "%.2X" $7
StrCpy $1 "$1 $7"
IntCmp $8 8 printline loopline printline
completeline:
IntOp $8 $8 + 1
StrCpy $5 "$5 "
StrCpy $1 "$1 ??"
stopline:
IntCmp $8 8 completeline completeline printline
printline:
DetailPrint $1$5
Goto loop
end:
System::Store L
FunctionEnd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment