home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 2 / crawlyvol2.bin / program / c / cookie / cookie.s < prev   
Text File  |  1991-01-05  |  8KB  |  370 lines

  1.  
  2. ;--------------------------------------------------------------------
  3. ;    Module:        COOKIE.S
  4. ;
  5. ;    Abstract:    Everything you need for accessing the 
  6. ;                cookie jar on Atari ST/TT computers.
  7. ;
  8. ;    Author:        Arnd Beissner, SciLab GmbH - Germany
  9. ;                Parts of the code are derived from Atari's 
  10. ;                TOS 1.06 Release Notes.
  11. ;
  12. ;    Compatibility:
  13. ;                Any ST/TT-compatible machine, any TOS version.
  14. ;
  15. ;    Language:    MAS68K by Borland
  16. ;
  17. ;    Version:    1.01
  18. ;
  19. ;    Date:        05.01.1991
  20. ;    
  21. ;    History:
  22. ;
  23. ;    05.01.1991    AB    - fixed a bug in CK_ReadJar
  24. ;
  25. ;--------------------------------------------------------------------
  26. ;    Comments:
  27. ;
  28. ;    Most functions in this module must be called in supervisor mode.
  29. ;    No registers except D0 (for the return value) are modified.
  30. ;--------------------------------------------------------------------
  31.  
  32. RESMAGIC        equ     $31415926
  33.  
  34. _p_cookies        equ        $5A0
  35. _resvalid       equ     $426
  36. _resvector      equ     $42a
  37.  
  38. ;--------------------------------------------------------------------
  39.  
  40.         .globl    CK_JarInstalled
  41.         .globl    CK_UsedEntries
  42.         .globl    CK_JarSize
  43.         .globl    CK_ResizeJar
  44.         .globl    CK_ReadJar
  45.         .globl    CK_WriteJar
  46.         .globl    CK_SetOptions
  47.         
  48.         .text
  49.  
  50. ;--------------------------------------------------------------------
  51. ; CK_JarInstalled
  52. ;
  53. ; See if the cookie jar is installed.
  54. ;
  55. ; Return value:
  56. ;        D0.L    = pointer to the cookie jar (0 = not installed)
  57. ;--------------------------------------------------------------------
  58.  
  59. CK_JarInstalled:
  60.         move.l    _p_cookies,d0
  61.         rts    
  62.  
  63.  
  64. ;--------------------------------------------------------------------
  65. ; CK_UsedEntries
  66. ;
  67. ; Inquire the number of used cookie jar entries. The number includes
  68. ; the null cookie, so a return value of 0 means that there is no
  69. ; cookie jar at all.
  70. ;
  71. ; Return value:
  72. ;        D0.W = number of used cookie jar entries
  73. ;--------------------------------------------------------------------
  74.  
  75. CK_UsedEntries:
  76.         move.l    a0,-(sp)
  77.         move.l    _p_cookies,d0
  78.         beq        _ue_end
  79.         
  80.         move.l    d0,a0
  81.         clr.w    d0
  82. _ue_loop:
  83.         addq.w    #1,d0
  84.         tst.l    (a0)
  85.         beq        _ue_end
  86.         addq.l    #8,a0
  87.         bra        _ue_loop
  88. _ue_end:
  89.         move.l    (sp)+,a0
  90.         rts
  91.  
  92.         
  93. ;--------------------------------------------------------------------
  94. ; CK_JarSize
  95. ;
  96. ; Inquire the total number of cookie jar entries.
  97. ;
  98. ; Return value:
  99. ;        D0.W = total number of cookie jar entries
  100. ;--------------------------------------------------------------------
  101.  
  102. CK_JarSize:
  103.         move.l    a0,-(sp)
  104.         move.l    _p_cookies,d0
  105.         beq        _js_end
  106.         
  107.         move.l    d0,a0
  108. _js_loop:
  109.         tst.l    (a0)
  110.         beq        _js_found
  111.         addq.l    #8,a0
  112.         bra        _js_loop
  113.         
  114. _js_found:
  115.         move.l    4(a0),d0
  116. _js_end:
  117.         move.l    (sp)+,a0
  118.         rts
  119.         
  120.  
  121. ;--------------------------------------------------------------------
  122. ; CK_ResizeJar
  123. ;
  124. ; Resize the cookie jar to the desired size.
  125. ;
  126. ; Input arguments:
  127. ;        D0.W    = desired cookie jar size, number of entries
  128. ;
  129. ; Return value:
  130. ;        D0.W    = state (0=FALSE, 1=TRUE)
  131. ;--------------------------------------------------------------------
  132.  
  133. CK_ResizeJar:
  134.         movem.l    d1-a6,-(sp)
  135.         
  136.         ; allocate a buffer for the new cookie jar
  137.         andi.l    #$FFFF,d0
  138.         move.l    d0,d7
  139.         lsl.w    #3,d0                ; 8 bytes per entry
  140.         move.l    d0,-(sp)
  141.         move.w    #$48,-(sp)            ; Malloc
  142.         trap    #1
  143.         addq.l    #6,sp
  144.         tst.l    d0                    ; allocation successful?
  145.         beq        _rj_end
  146.         bpl        _rj_allocok
  147.         clr.w    d0
  148.         bra        _rj_end
  149.         
  150. _rj_allocok:
  151.         ; copy the contents of the old cookie jar to the new buffer
  152.         move.l    d0,a2
  153.         move.l    d0,a0
  154.         move.l    _p_cookies,a1
  155.         bsr        CK_UsedEntries
  156.         subq.w    #1,d0
  157.         bra        _rj_cpend
  158. _rj_copy:
  159.         move.l    (a1)+,(a0)+
  160.         move.l    (a1)+,(a0)+
  161. _rj_cpend:
  162.         dbf        d0,_rj_copy                
  163.         
  164.         ; insert the null cookie
  165.         clr.l    (a0)+                ; null cookie
  166.         move.l    d7,(a0)                ; cookie jar size
  167.         
  168.         ; install the pointer to the new cookie jar
  169.         move.l    a2,_p_cookies
  170.         
  171.         move.w    #1,d0                ; success
  172.         
  173. _rj_end:
  174.         movem.l    (sp)+,d1-a6
  175.         rts
  176.  
  177.  
  178. ;--------------------------------------------------------------------
  179. ; _instReset            **** internal ****
  180. ;
  181. ; Install a reset handler which clears the cookie jar on reboot.
  182. ; This is necessary for TOS versions before 1.06
  183. ;--------------------------------------------------------------------
  184.  
  185. _instReset:
  186.         move.l    cookieJarXBRA,xbraId
  187.          move.l    _resvalid,oldResValid
  188.          move.l    #RESMAGIC,_resvalid
  189.         move.l    _resvector,oldReset
  190.          move.l    #newReset,_resvector
  191.          rts
  192.  
  193.          dc.b    "XBRA"
  194. xbraId:    dc.b    "ck01"               ; XBRA-structure
  195. oldReset:
  196.          dc.l    0
  197. newReset:
  198.          clr.l    _p_cookies            ; clear the cookie jar
  199.          move.l    oldReset,_resvector
  200.          move.l    oldResValid,_resvalid
  201.          jmp        (a6)
  202.  
  203.  
  204. ;--------------------------------------------------------------------
  205. ; _searchJar            **** internal ****
  206. ;
  207. ; search the position of the specified cookie
  208. ;
  209. ; Input arguments:
  210. ;        D0.L    = cookie name
  211. ;
  212. ; Return value:
  213. ;        D0.L    = pointer to cookie
  214. ;--------------------------------------------------------------------
  215.  
  216. _searchJar:
  217.         movem.l    d2/a0,-(sp)
  218.         move.l    d0,d2
  219.         move.l    _p_cookies,d0
  220.         beq        _search_end
  221.         
  222.         move.l    d0,a0
  223.         clr.l    d0
  224. _search_next:
  225.         tst.l    (a0)            ; null cookie?
  226.         beq        _search_end
  227.         cmp.l    (a0),d2            ; cookie found?
  228.         beq        _search_found
  229.         addq.l    #8,a0
  230.         bra        _search_next
  231.         
  232. _search_found:
  233.         move.l    a0,d0        
  234.  
  235. _search_end:
  236.         movem.l    (sp)+,d2/a0
  237.         rts
  238.  
  239.  
  240. ;--------------------------------------------------------------------
  241. ; CK_ReadJar
  242. ;
  243. ; Read the value of the specified cookie.
  244. ;
  245. ; Input arguments:
  246. ;        D0.L    = cookie name
  247. ;        A0        = pointer to cookie value
  248. ;
  249. ; Return value:
  250. ;        D0.W    = state (0=FALSE, 1=TRUE)
  251. ;--------------------------------------------------------------------
  252.  
  253. CK_ReadJar:
  254.         move.l    a1,-(sp)
  255.         bsr        _searchJar            ; get a pointer to the jar entry
  256.         tst.l    d0
  257.         beq        _ck_rdend            ; entry not found
  258.         move.l    d0,a1
  259.         move.l    4(a1),(a0)            ; read the cookie value
  260.         move.w    #1,d0                ; success!    
  261.  
  262. _ck_rdend:
  263.         move.l    (sp)+,a1
  264.         rts
  265.         
  266.  
  267. ;--------------------------------------------------------------------
  268. ; CK_WriteJar
  269. ;
  270. ; Insert a new entry into the cookie jar. If no cookie jar exists
  271. ; or the current cookie jar is full, a new, bigger cookie jar is
  272. ; installed. The increment in size can be set using CK_SetOptions.
  273. ;
  274. ; Input arguments:
  275. ;        D0.L    = cookie name
  276. ;        D1.L    = cookie value
  277. ;
  278. ; Return value:
  279. ;        D0.W    = state (0=FALSE, 1=TRUE)
  280. ;--------------------------------------------------------------------
  281.  
  282. CK_WriteJar:
  283.         movem.l    d2-d3/a0,-(sp)
  284.         move.l    d0,d2
  285.         bsr        _searchJar
  286.         tst.l    d0
  287.         bne        _ck_we_found
  288.         
  289.         ; cookie not found --> allocate one
  290.         bsr        CK_JarSize
  291.         move.w    d0,d3
  292.         bsr        CK_UsedEntries
  293.         sub.w    d0,d3
  294.         bgt        _ck_we_write
  295.         
  296.         ; no space for more cookies --> allocate a new cookie jar
  297.         ; before that, install a reset function which resets the
  298.         ; cookie jar on reboot. This is only necessary if no cookie
  299.         ; jar exists.
  300.         tst.l    _p_cookies
  301.         bne        _ck_we_inc
  302.         bsr        _instReset
  303.         
  304. _ck_we_inc:        
  305.         add.w    cookieJarIncrement,d0
  306.         bsr        CK_ResizeJar
  307.         tst.w    d0
  308.         beq        _ck_we_end                        ; allocation failed
  309.         bsr        CK_UsedEntries
  310.  
  311.         ; write the cookie to location d0-1
  312. _ck_we_write:
  313.         subq.w    #1,d0
  314.         lsl.w    #3,d0
  315.         move.l    _p_cookies,a0
  316.         add.w    d0,a0
  317.         clr.l    8(a0)
  318.         move.l    4(a0),12(a0)
  319.         move.l    d2,(a0)
  320.         move.l    d1,4(a0)
  321.         bra        _ck_we_ok
  322.         
  323. _ck_we_found:
  324.         ; cookie found --> overwrite with new value
  325.         move.l    d0,a0
  326.         move.l    d1,4(a0)
  327.  
  328. _ck_we_ok:
  329.         move.w    #1,d0        
  330.     
  331. _ck_we_end:
  332.         movem.l    (sp)+,d2-d3/a0
  333.         rts
  334.  
  335.  
  336. ;--------------------------------------------------------------------
  337. ; CK_SetOptions
  338. ;
  339. ; Set cookie jar options.
  340. ;
  341. ; Input arguments:
  342. ;        D0.W    = cookie jar increment when allocating a new buffer
  343. ;        D1.L    = xbra id for reset handler
  344. ;--------------------------------------------------------------------
  345.  
  346. CK_SetOptions:
  347.         move.w    d0,cookieJarIncrement
  348.         move.l    d1,cookieJarXBRA
  349.         rts
  350.  
  351. ;--------------------------------------------------------------------
  352.  
  353.         .data
  354.         
  355. cookieJarIncrement:
  356.         dc.w    20
  357.  
  358. cookieJarXBRA:
  359.         dc.b    'ck01'
  360.  
  361. ;--------------------------------------------------------------------
  362.  
  363.         .bss
  364.  
  365. oldResValid:
  366.         ds.l    1
  367.                 
  368.         .end
  369.  
  370.