use AppleScript version "2.0" -- Leopard (10.5) or later
use scripting additions
--version 1-9-2024
--non numerical vars are decoded, too
--workaround quit handler for OS like Mavericks
global hex_value
on run
	try
		set csr_nvram_string to do shell script "nvram csr-active-config"
		set current_status to decode_csr_value("")
	on error
		display dialog "nothing set" with title "current csr policy - by Macschrauber" buttons "OK" default button 1 with icon 1
		quit
	end try
	
	display dialog csr_nvram_string & " = 0x" & hex_value & return & return & current_status with title "current csr policy - by Macschrauber" buttons big_button("OK") default button 1 with icon 1
	
end run
on quit
	try
		continue quit
	on error
		delay 1
		try
			continue quit
		on error
			error number -128 -- some OS like Mavericks don't quit on contine quit
		end try
	end try
	error number -128
end quit
on decode_csr_value(value)
	set value to value as string
	
	
	try
		if value = "" then set value to do shell script "nvram csr-active-config"
	on error
		return ""
	end try
	
	if value begins with "csr-active-config" then
		set text item delimiters to ASCII character 9 -- tab
		set value to text item 2 of value -- cut csr-active-config\tab
		set text item delimiters to ""
	end if
	
	
	
	set hex_value to ""
	set i to 1
	repeat while i ≤ length of value
		set char to character i of value
		if char = "%" then
			-- Next two characters are hex
			set hex_value to hex_value & (characters (i + 1) through (i + 2) of value as string)
			set i to i + 3
		else
			-- Convert ASCII to hex
			set char_to_hex to (do shell script "printf '%02x' " & (ASCII number char))
			set hex_value to hex_value & char_to_hex
			set i to i + 1
		end if
	end repeat
	
	
	-- Convert to little-endian format
	set little_endian to ""
	repeat with i from (length of hex_value) to 1 by -2
		set little_endian to little_endian & (character (i - 1) of hex_value) & (character i of hex_value)
	end repeat
	
	
	set hex_value to little_endian
	
	
	-- strip leading zeroes of hex_value			
	try
		set char_list to characters of hex_value
		
		-- Remove leading zeros
		repeat while (char_list) starts with "0"
			set char_list to rest of char_list
		end repeat
		
		-- Convert the list back to a string
		set printable_hex_value to char_list as string
		
		-- If the result is empty (all zeros were removed), set it to "0"
		if printable_hex_value = "" then
			set printable_hex_value to "0"
		end if
		
		
		--format hex string	
		set digits to (count (characters of printable_hex_value))
		if digits = 1 then
			set printable_hex_value to "0x00" & printable_hex_value
		else if digits = 2 then
			set printable_hex_value to "0x0" & printable_hex_value
		else
			set printable_hex_value to "0x" & printable_hex_value
		end if
		
		
		
	on error errortext
		set printable_hex_value to "0x" & hex_value
	end try
	
	
	
	try
		set flags to {¬
			"0x001 CSR_ALLOW_UNTRUSTED_KEXTS", ¬
			"0x002 CSR_ALLOW_UNRESTRICTED_FS", ¬
			"0x004 CSR_ALLOW_TASK_FOR_PID", ¬
			"0x008 CSR_ALLOW_KERNEL_DEBUGGER", ¬
			"0x010 CSR_ALLOW_APPLE_INTERNAL", ¬
			"0x020 CSR_ALLOW_UNRESTRICTED_DTRACE", ¬
			"0x040 CSR_ALLOW_UNRESTRICTED_NVRAM", ¬
			"0x080 CSR_ALLOW_DEVICE_CONFIGURATION", ¬
			"0x100 CSR_ALLOW_ANY_RECOVERY_OS", ¬
			"0x200 CSR_ALLOW_UNAPPROVED_KEXTS", ¬
			"0x400 CSR_ALLOW_EXECUTABLE_POLICY_OVERRIDE", ¬
			"0x800 CSR_ALLOW_UNAUTHENTICATED_ROOT"}
		
		
		set bin_value to hexToBinary(hex_value)
		set bin_size to (count characters of bin_value)
		set active_flags to ""
		
		repeat with i from 1 to bin_size
			set current_bit to (bin_size - i + 1) -- last character is first bit
			if (character current_bit of bin_value) = "1" then set active_flags to active_flags & ((item i of flags) as string) & return
		end repeat
		
		if active_flags ≠ "" then -- delete last cr
			return (printable_hex_value) & return & ((strings 1 thru -2 of active_flags) as text)
			
		else
			return ""
		end if
		
	on error errortext
		return ""
		display dialog errortext buttons "OK" default button 1
	end try
	
end decode_csr_value
on hexToBinary(hex_value)
	if hex_value starts with "0x" then
		set hex_value to text 3 thru -1 of hex_value
	end if
	
	set binary_value to ""
	
	repeat with i from 1 to (count characters of hex_value)
		set current_char to character i of hex_value
		
		if current_char = "0" then
			set binary_value to binary_value & "0000"
		else if current_char = "1" then
			set binary_value to binary_value & "0001"
		else if current_char = "2" then
			set binary_value to binary_value & "0010"
		else if current_char = "3" then
			set binary_value to binary_value & "0011"
		else if current_char = "4" then
			set binary_value to binary_value & "0100"
		else if current_char = "5" then
			set binary_value to binary_value & "0101"
		else if current_char = "6" then
			set binary_value to binary_value & "0110"
		else if current_char = "7" then
			set binary_value to binary_value & "0111"
		else if current_char = "8" then
			set binary_value to binary_value & "1000"
		else if current_char = "9" then
			set binary_value to binary_value & "1001"
		else if current_char = "a" or current_char = "A" then
			set binary_value to binary_value & "1010"
		else if current_char = "b" or current_char = "B" then
			set binary_value to binary_value & "1011"
		else if current_char = "c" or current_char = "C" then
			set binary_value to binary_value & "1100"
		else if current_char = "d" or current_char = "D" then
			set binary_value to binary_value & "1101"
		else if current_char = "e" or current_char = "E" then
			set binary_value to binary_value & "1110"
		else if current_char = "f" or current_char = "F" then
			set binary_value to binary_value & "1111"
		end if
	end repeat
	
	return binary_value
end hexToBinary
on big_button(buttonstring)
	
	try -- init variable, if not set
		system_version
	on error
		set system_version to ""
	end try
	
	if system_version is "" then set system_version to do shell script "sw_vers -productVersion"
	
	try
		set spaces to "                                                                                    "
		considering numeric strings
			if (system_version ≥ "10.16") or (system_version begins with "10.6") or (system_version begins with "10.7") then
				return buttonstring & spaces & spaces
			else
				return spaces & buttonstring & spaces
			end if
		end considering
	on error
		return buttonstring
	end try
	
end big_button