Advertisement
Guest User

Untitled

a guest
Feb 10th, 2010
11,317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.80 KB | None | 0 0
  1. .globl main
  2. main:
  3. /* Change to the working directory */
  4. xorl %eax,%eax
  5. pushl $dir // path
  6. movb $12,%al
  7. pushl %eax // chdir
  8. int $0x80
  9. setc %bl
  10. addl $8,%esp
  11. testb %bl,%bl
  12. jnz out
  13.  
  14. /* Allow up to 32 links */
  15. pushl $(32*256)
  16. calll alloc
  17. addl $4,%esp
  18. movl %eax,links
  19.  
  20. /*
  21. * Read each link.
  22. * They are stored in files whose name start at 'a' and is
  23. * incremented at each new link.
  24. * XXX Allow for more than 26
  25. */
  26. xorl %eax,%eax
  27. movb $'a',%al
  28. pushl %eax
  29. movl %esp,%ecx // ecx = path
  30.  
  31. read_link:
  32. xorl %eax,%eax
  33. pushl %eax // O_RDONLY
  34. pushl %ecx // path
  35. movb $5,%al
  36. pushl %eax // open
  37. int $0x80
  38. setc %bl
  39. addl $12,%esp
  40. testb %bl,%bl
  41. jnz out_link2
  42.  
  43. movl %eax,%edx // edx = fd
  44.  
  45. leal tmp_buf,%edi
  46.  
  47. pushl $256 // len
  48. pushl %edi // buf
  49. pushl %eax // fd
  50. xorl %eax,%eax
  51. movb $3,%al
  52. pushl %eax // read
  53. int $0x80
  54. setc %bl
  55. addl $16,%esp
  56. testb %bl,%bl
  57. jnz out_link
  58.  
  59. movb $0,0(%edi,%eax)
  60.  
  61. /*
  62. * Store in the "links" array
  63. */
  64.  
  65. pushl %ecx
  66.  
  67. /* Copy url */
  68. pushl $0xa // '\n'
  69. pushl %edi // url
  70. calll _strchr
  71. addl $8,%esp
  72. subl %edi,%eax
  73. movl %eax,%ecx
  74. movl %edi,%esi
  75. movl nlinks,%ebx
  76. movl links,%edi
  77. shll $8,%ebx // ebx = nlinks * 256
  78. leal 0(%edi,%ebx),%edi
  79. cld
  80. rep movsb
  81. movb $0,(%edi)
  82.  
  83. /* Copy description */
  84. incl %esi
  85. pushl $0xa
  86. pushl %esi
  87. calll _strchr
  88. addl $8,%esp
  89. subl %esi,%eax
  90. movl %eax,%ecx
  91. movl links,%edi
  92. leal 128(%edi,%ebx),%edi
  93. cld
  94. rep movsb
  95. movb $0,(%edi)
  96.  
  97. /* Copy path */
  98. movl 4(%esp),%ecx
  99. movl links,%edi
  100. movl %ecx,248(%edi,%ebx)
  101.  
  102. /* Copy score */
  103. incl %esi
  104. pushl %esi
  105. calll _atoi
  106. addl $4,%esp
  107. movl links,%edi
  108. leal 252(%edi,%ebx),%edi
  109. movl %eax,(%edi)
  110.  
  111. popl %ecx
  112.  
  113. pushl %edx // fd
  114. movl $6,%eax
  115. pushl %eax // close
  116. int $0x80
  117. addl $8,%esp
  118.  
  119. incl (%esp) // Next file (increment the filename)
  120. incl nlinks
  121. jmp read_link
  122.  
  123. out_link:
  124. pushl %edx // fd
  125. movl $6,%eax
  126. pushl %eax // close
  127. int $0x80
  128. addl $8,%esp
  129. out_link2:
  130. addl $4,%esp
  131.  
  132. /* XXX Implement my own qsort */
  133. pushl $compar
  134. pushl $256
  135. pushl nlinks
  136. pushl links
  137. calll qsort
  138. addl $16,%esp
  139.  
  140. /* get QUERY_STRING */
  141. pushl $query_string
  142. calll getenv
  143. addl $4,%esp
  144.  
  145. testl %eax,%eax
  146. jz 1f
  147. movl (%eax),%ebx
  148. testb %bl,%bl
  149. jz 1f
  150. movl %eax,saved_qs
  151. jmp have_qs
  152. 1:
  153. /* Output header */
  154. xorl %eax,%eax
  155. pushl $(ok_end-ok) // len
  156. pushl $ok // buf
  157. movb $1,%al
  158. pushl %eax // stdout
  159. movb $4,%al
  160. pushl %eax // write
  161. int $0x80
  162. addl $16,%esp
  163. /* Print the links */
  164. movl nlinks,%ecx
  165. movl links,%edx
  166. 1:
  167. leal tmp_buf,%edi
  168. /* Upvote */
  169. xorl %ebx,%ebx
  170. pushl %ebx
  171. pushl $0x003d703f // ?p=
  172. movl %esp,%eax
  173. movl 248(%edx),%ebx
  174. movb %bl,3(%eax)
  175. pushl $uarr // desc
  176. pushl %eax // url
  177. pushl %edi // dst
  178. calll make_a_href
  179. addl $20,%esp
  180. movl %eax,%edi
  181. /* <br/> */
  182. movl $0x2f72623c,%eax
  183. stosl
  184. movb $0x3e,%al
  185. stosb
  186.  
  187. /* Score */
  188. movl 252(%edx),%eax
  189. /* XXX Use a proper itoa() */
  190. addl $'0',%eax
  191. stosb
  192. movb $' ',%al
  193. stosb
  194.  
  195. /* The actual link */
  196. leal 128(%edx),%ebx
  197. pushl %ebx // desc
  198. pushl %edx // url
  199. pushl %edi // dst
  200. calll make_a_href
  201. addl $12,%esp
  202. movl %eax,%edi
  203. /* <br/> */
  204. movl $0x2f72623c,%eax
  205. stosl
  206. movb $0x3e,%al
  207. stosb
  208.  
  209. /* Downvote */
  210. xorl %ebx,%ebx
  211. pushl %ebx
  212. pushl $0x003d643f // ?d=
  213. movl %esp,%eax
  214. movl 248(%edx),%ebx
  215. movb %bl,3(%eax)
  216. pushl $darr // desc
  217. pushl %eax // url
  218. pushl %edi // dst
  219. calll make_a_href
  220. addl $20,%esp
  221. movl %eax,%edi
  222. /* <br/> */
  223. movl $0x2f72623c,%eax
  224. stosl
  225. movb $0x3e,%al
  226. stosb
  227.  
  228. leal tmp_buf,%eax
  229. subl %eax,%edi
  230. pushl %edi // len
  231. pushl %eax // buf
  232. xorl %eax,%eax
  233. movb $1,%al
  234. pushl %eax // stdout
  235. movb $4,%al
  236. pushl %eax // write
  237. int $0x80
  238. addl $16,%esp
  239.  
  240. addl $256,%edx
  241. decl %ecx
  242. jnz 1b
  243. jmp out
  244.  
  245. have_qs:
  246. /* Output header */
  247. xorl %eax,%eax
  248. pushl $(submit_str_end-submit_str) // len
  249. pushl $submit_str // buf
  250. movb $1,%al
  251. pushl %eax // stdout
  252. movb $4,%al
  253. pushl %eax // write
  254. int $0x80
  255. addl $16,%esp
  256.  
  257. /* URL submit? */
  258. movl saved_qs,%eax
  259. cmpw $0x3d74,(%eax) // "t="
  260. jnz 9f
  261. /* Get Title */
  262. incl %eax
  263. incl %eax
  264. movl %eax,%ebx
  265. pushl $'&'
  266. pushl %eax
  267. calll _strchr
  268. addl $8,%esp
  269. movb $0,(%eax)
  270. pushl %ebx
  271.  
  272. /* Get URL */
  273. incl %eax
  274. cmpw $0x3d75,(%eax) // "u="
  275. jnz 2f
  276. incl %eax
  277. incl %eax
  278.  
  279. /* Build file */
  280. /* strlen(URL) < 120 */
  281. movl %eax,%esi
  282. pushl %eax
  283. calll _strlen
  284. addl $4,%esp
  285. cmpl $120,%eax
  286. jge 2f
  287. movl %eax,%ecx
  288. leal tmp_buf,%edi
  289. /* Decode URL */
  290. 0:
  291. movb (%esi),%al // XXX use lodsb
  292. cmpb $'%',%al
  293. jne 1f
  294. incl %esi
  295. decl %ecx
  296. movb (%esi),%al
  297. cmpb $'9',%al
  298. jg 3f
  299. subb $'0',%al
  300. jmp 4f
  301. 3:
  302. subb $'A',%al // XXX lowercase too
  303. addb $10,%al
  304. 4:
  305. shlb $4,%al
  306. movb 1(%esi),%bl
  307. cmpb $'9',%bl
  308. jg 3f
  309. subb $'0',%bl
  310. jmp 4f
  311. 3:
  312. subb $'A',%bl // XXX lowercase too
  313. addb $10,%bl
  314. 4:
  315. orb %bl,%al
  316. incl %esi
  317. decl %ecx
  318. 1:
  319. stosb
  320. incl %esi
  321. loop 0b
  322.  
  323. movb $'\n',(%edi)
  324. incl %edi
  325. /* strlen(title) < 128 */
  326. movl (%esp),%esi
  327. pushl %esi
  328. calll _strlen
  329. addl $4,%esp
  330. cmpl $128,%eax
  331. jge 2f
  332. /* Replace '+' with ' ' in title */
  333. /* XXX also do the same thing as URL decoding */
  334. movl %eax,%ecx
  335. 0:
  336. movb (%esi),%al
  337. cmpb $'+',%al
  338. jne 1f
  339. movb $' ',%al
  340. 1:
  341. stosb
  342. incl %esi
  343. loop 0b
  344.  
  345. movb $'\n',(%edi)
  346. incl %edi
  347. movw $0x0a30,%ax // "0\n"
  348. stosw
  349. leal tmp_buf,%esi
  350. subl %esi,%edi // edi = len
  351.  
  352. movl nlinks,%eax
  353. addl $'a',%eax // XXX more than 26?
  354. pushl %eax
  355. movl %esp,%ecx // ecx = path
  356. pushl $0644 // mode
  357. pushl $0x202 // O_RDWR | O_CREAT
  358. pushl %ecx // path
  359. movb $5,%al
  360. pushl %eax // open
  361. int $0x80
  362. setc %bl
  363. addl $20,%esp
  364. testb %bl,%bl
  365. jnz 2f
  366. pushl %edi // len
  367. pushl $tmp_buf // buf
  368. pushl %eax // fd
  369. movl $4,%eax
  370. pushl %eax // write
  371. int $0x80
  372. addl $16,%esp
  373.  
  374. 2:
  375. addl $4,%esp
  376. jmp out
  377.  
  378. 9:
  379. /* Upvote? */
  380. cmpw $0x3d70,(%eax) // "p="
  381. jnz 1f
  382. movl $1,%ebx // ebx = up
  383. jmp 2f
  384. /* Downvote? */
  385. 1:
  386. cmpw $0x3d64,(%eax) // "d="
  387. jnz out
  388. movl $~0,%ebx // ebx = down
  389. 2:
  390. xorl %edx,%edx
  391. movb 2(%eax),%dl
  392. /* Look for the link we voted on */
  393. movl nlinks,%ecx
  394. movl links,%edi
  395. 1:
  396. cmpb %dl,248(%edi)
  397. je 2f
  398. addl $256,%edi
  399. loop 1b
  400. 2:
  401. testl %ecx,%ecx
  402. jz out
  403. addl %ebx,252(%edi) // Update the score
  404. movl 252(%edi),%ecx
  405. cmpb $0,%cl
  406. js out
  407. cmpb $10,%cl
  408. jge out
  409. /* Get offset in the file to update */
  410. pushl %edi
  411. calll _strlen
  412. addl $4,%esp
  413. movl %eax,%ebx
  414. leal 128(%edi),%edx
  415. pushl %edx
  416. calll _strlen
  417. addl $4,%esp
  418. addl %eax,%ebx
  419. incl %ebx
  420. incl %ebx // Account for the two '\n'
  421.  
  422. xorl %eax,%eax
  423. movb $2,%al
  424. pushl %eax // O_RDWR
  425. leal 248(%edi),%esi
  426. pushl %esi // path
  427. movb $5,%al
  428. pushl %eax // open
  429. int $0x80
  430. setc %cl
  431. addl $12,%esp
  432. testb %cl,%cl
  433. jnz out
  434.  
  435. movl %eax,%ecx // ecx = fd
  436. xorl %eax,%eax
  437. pushl %eax // SEEK_SET
  438. pushl %ebx // offset
  439. pushl %ecx // fd
  440. movb $19,%al
  441. pushl %eax // lseek
  442. int $0x80
  443. setc %bl
  444. addl $16,%esp
  445. testb %bl,%bl
  446. jnz out
  447.  
  448. /* Write the new score */
  449. movl 252(%edi),%eax
  450. addl $'0',%eax // XXX proper itoa()
  451. orl $0x0a00,%eax // '\n'
  452. movw %ax,tmp_buf
  453.  
  454. write:
  455. pushl $2 // len
  456. pushl $tmp_buf // buf
  457. pushl %ecx // fd
  458. movl $4,%eax
  459. pushl %eax // write
  460. int $0x80
  461. addl $16,%esp
  462.  
  463. out:
  464. xorl %eax,%eax
  465. retl
  466.  
  467. /*
  468. * char *make_a_href(char *dst, char *url, char *desc)
  469. */
  470. make_a_href:
  471. pushl %ecx
  472. pushl %edx
  473. pushl %edi
  474. pushl %esi
  475. movl 20(%esp),%edi
  476. /* <a href=" */
  477. leal link_start_str,%esi
  478. movl $9,%ecx // strlen(&link_start_str)
  479. cld
  480. rep movsb
  481. /* http://blah.com/ */
  482. movl 24(%esp),%edx
  483. pushl %edx
  484. calll _strlen
  485. addl $4,%esp
  486. movl %eax,%ecx
  487. movl %edx,%esi
  488. rep movsb
  489. /* "> */
  490. movw $0x3e22,%ax
  491. stosw
  492. /* Description */
  493. movl 28(%esp),%edx
  494. pushl %edx
  495. calll _strlen
  496. addl $4,%esp
  497. movl %eax,%ecx
  498. movl %edx,%esi
  499. rep movsb
  500. /* </a> */
  501. movl $0x3e612f3c,%eax
  502. stosl
  503. movl %edi,%eax
  504. popl %esi
  505. popl %edi
  506. popl %edx
  507. popl %ecx
  508. retl
  509.  
  510. /*
  511. * char *strchr(char *s, int c)
  512. */
  513. _strchr:
  514. pushl %ecx
  515. pushl %edi
  516. xorl %ecx,%ecx
  517. notl %ecx
  518. movl 12(%esp),%edi
  519. movl 16(%esp),%eax
  520. cld
  521. repnz scasb
  522. leal -1(%edi),%eax
  523. popl %edi
  524. popl %ecx
  525. retl
  526.  
  527. _strlen:
  528. movl 4(%esp),%eax
  529. pushl $0
  530. pushl %eax
  531. calll _strchr
  532. addl $8,%esp
  533. subl 4(%esp),%eax
  534. retl
  535.  
  536. compar:
  537. pushl %ebx
  538. movl 8(%esp),%ebx
  539. movl 252(%ebx),%ebx
  540. movl 12(%esp),%eax
  541. movl 252(%eax),%eax
  542. subl %ebx,%eax
  543. popl %ebx
  544. retl
  545.  
  546. /*
  547. * void *alloc(int size)
  548. */
  549. alloc:
  550. pushl %ecx
  551. movl 8(%esp),%ecx
  552. addl $(0x1000 - 1),%ecx
  553. andl $~(0x1000 - 1),%ecx
  554. xorl %eax,%eax
  555. pushl %eax
  556. pushl %eax // offset
  557. pushl $~0 // fd
  558. pushl $0x1000 // flags = MAP_ANON
  559. pushl $3 // prot = PROT_READ | PROT_WRITE
  560. pushl %ecx // len
  561. pushl %eax // addr
  562. movb $197,%al // mmap
  563. pushl %eax
  564. int $0x80
  565. addl $32,%esp
  566. popl %ecx
  567. retl
  568.  
  569. /*
  570. * int atoi(char *s)
  571. */
  572. _atoi:
  573. movl 4(%esp),%eax
  574. pushl %ebx
  575. pushl %ecx
  576. pushl %edx
  577. pushl %edi
  578. movl %eax,%ecx // end
  579. movl %eax,%edx // beginning
  580. xorl %eax,%eax
  581. xorl %ebx,%ebx
  582. movl $1,%edi
  583. /* Find the end */
  584. 1:
  585. movb (%ecx),%bl
  586. subb $'0',%bl
  587. js 2f
  588. cmp $10,%bl
  589. jge 2f
  590. incl %ecx
  591. jmp 1b
  592. 2:
  593. decl %ecx
  594. cmp %edx,%ecx
  595. jl 3f
  596. movb (%ecx),%bl
  597. subb $'0',%bl
  598. /* XXX choose registers better to avoid this crap */
  599. pushl %eax
  600. pushl %edx
  601. movl %edi,%eax
  602. mull %ebx
  603. movl %eax,%ebx
  604. popl %edx
  605. popl %eax
  606. addl %ebx,%eax
  607. leal 0(%edi,%edi,4),%edi
  608. addl %edi,%edi
  609. jmp 2b
  610. 3:
  611. popl %edi
  612. popl %edx
  613. popl %ecx
  614. popl %ebx
  615. retl
  616.  
  617. .data
  618. query_string:
  619. .asciz "QUERY_STRING"
  620. ok:
  621. .ascii "HTTP/1.1 200 OK\r\n"
  622. .ascii "Server: r.cgi\r\n"
  623. .ascii "Content-Type: text/html; charset=iso-8859-1\r\n\r\n"
  624. .ascii "<html><body>"
  625. .ascii "<form action=\"r.cgi\" id=\"form\">"
  626. .ascii "Description: <input id=\"input\" name=\"t\" type=\"text\""
  627. .ascii "value=\"\" />"
  628. .ascii "URL: <input id=\"input\" name=\"u\" type=\"text\""
  629. .ascii "value=\"\" />"
  630. .ascii "<input type='submit' value=\"Submit\" />"
  631. .ascii "</form>"
  632. .ascii "</body></html>"
  633. ok_end:
  634. submit_str:
  635. .ascii "HTTP/1.1 200 OK\r\n"
  636. .ascii "Server: r.cgi\r\n"
  637. .ascii "Content-Type: text/html; charset=iso-8859-1\r\n\r\n"
  638. .ascii "<?xml version=\"1.0\"?>\n<!DOCTYPE html PUBLIC \"-//W3C//DTD "
  639. .ascii "XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml"
  640. .ascii "1-strict.dtd\"><html><head>"
  641. .ascii "<meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=r.cgi\"/>"
  642. .ascii "</head></html>"
  643. submit_str_end:
  644. saved_qs:
  645. .long 0
  646. link_start_str:
  647. .ascii "<a href=\""
  648. uarr:
  649. .asciz "&uarr;"
  650. darr:
  651. .asciz "&darr;"
  652. /*
  653. * Pointer to array of "links", where each link is in the following format:
  654. * char link[128];
  655. * char desc[120];
  656. * char path[4];
  657. * int score;
  658. */
  659. links:
  660. .long 0
  661. nlinks:
  662. .long 0
  663. dir:
  664. .asciz "www/r/t"
  665. tmp_buf:
  666. .skip 512
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement