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

function GETSTATE (

  State exists or return 0,

  key = -16 firstof State,			
  16 lastof State == key hmacmd5 Process-Secret or return 0,

  Memory(op = Load-Purge, key := -8 lastof key), 
  del op, del key,

  moveall rep:strval, moveall rep:ordval,		# from rep to req
  return del rep:int
),


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

function PUTSTATE (

  # See if we have a valid State on the request list

  State exists 
  and str = -16 firstof State 
  and 16 lastof State == str hmacmd5 Process-Secret
  and (

    # Yes; if we need to store anything, Store under given key 
    # (between 8 octet nonce and 16 octet hmac), else purge given key

    strval exists or ordval exists and (
      Memory(op = Store, key = -8 lastof str),
      del op, del key, del str, 
      moveall State,				      # from req to rep
      return del rep:int
    ),

    Memory(op = Purge, key = -8 lastof str),
    del op, del key, del str, 
    return del rep:int,				      # don't add state here
  ),

  # No; if we need to store anything, generate new key and new State containing
  # signed key, else we're done.

  strval exists or ordval exists or return 0,
  Memory(op = Store-Newkey, key = random 8),	      # increments key if it
  del op, del key,				      # exists and returns it

  rep:State = random 8 . rep:key, del rep:key,
  rep:State := rep:State . rep:State hmacmd5 Process-Secret,
  del rep:int
),


# vim:softtabstop=2:sw=2

