
# Retrieves all instances of REQ:ordval and REQ:strval attached to a State

function GETSTATE (

	State exists or return 0,

	str = -16 firstof State,			
	16 lastof State == REQ:str hmacmd5 Process-Secret or return 0,

	Memory(op = Load, key := -8 lastof REQ:str), del str,
	moveall strval, moveall ordval,			# from REP to REQ
	return 1
),

# Stores all instances of REQ:ordval and REQ:strval and returns a new State

function NEWSTATE (

	Memory(op = Store-Newkey, key = random 8), 	# increments key if it
	del op, del key,				# exists; returns it

	str := random 8 . key,				# nonce . REP:key
	State := REQ:str .
		 REQ:str hmacmd5 Process-Secret,	# add HMAC over it
	del str
),

# Replaces all instances of REQ:ordval and REQ:strval attached to existing
# state and returns 1, or returns 0 if the state does not exist.

function UPDSTATE (

	State exists or return 0,

	str = -16 firstof State,
	16 lastof State == REQ:str hmacmd5 Process-Secret or return 0,

	Memory(op = Replace, key := -8 lastof REQ:str), del str,
	return 1
),

# Updates all instances of REQ:ordval and REQ:strval attached to State, if
# it exists, or else create a new one.

function PUTSTATE ((UPDSTATE) or (NEWSTATE)),

1

