fix for corrupted graphics when manipulating config files
[open-ps2-loader.git] / modules / network / SMSTCPIP / inet.s
blobed40259a16215684ba46a857a11727d7d2b92b05
1 # ___ _ _ ___
2 # | | | | |
3 # ___| | | ___| PS2DEV Open Source Project.
4 #----------------------------------------------------------
5 # (c) 2006 Eugene Plotnikov <e-plotnikov@operamail.com>
6 # Licenced under Academic Free License version 2.0
7 # Review ps2sdk README & LICENSE files for further details.
9 .set noreorder
10 .set nomacro
12 .globl ip_cksum
13 .globl inet_chksum
14 .globl inet_chksum_pseudo
15 .globl inet_chksum_pbuf
16 .globl inet_aton
17 .globl inet_addr
18 .globl inet_ntoa
19 .globl htons
20 .globl ntohs
21 .globl htonl
22 .globl ntohl
24 .section ".bss"
25 s_Str: .space 16
27 .text
29 htons :
30 ntohs :
31 _htons:
32 andi $v1, $a0, 0xFFFF
33 andi $a1, $v1, 0xFF
34 sll $v0, $a1, 8
35 srl $a0, $v1, 8
36 jr $ra
37 or $v0, $v0, $a0
39 htonl :
40 _htonl:
41 ntohl :
42 andi $t1, $a0, 0xFF00
43 sll $t0, $a0, 24
44 srl $a3, $a0, 8
45 sll $a1, $t1, 8
46 or $a2, $t0, $a1
47 andi $v1, $a3, 0xFF00
48 or $v0, $a2, $v1
49 srl $a0, $a0, 24
50 jr $ra
51 or $v0, $v0, $a0
53 ip_cksum :
54 _ip_cksum:
55 andi $v0, $a0, 0x3
56 move $t0, $a0
57 move $t1, $a1
58 move $a0, $zero
59 move $t3, $zero
60 move $t2, $zero
61 beqz $v0, 1f
62 move $t4, $zero
63 slti $v1, $a1, 3
64 bnez $v1, 2f
65 andi $a1, $t0, 0x1
66 bnez $a1, 3f
67 nop
68 14:
69 andi $a3, $t0, 0x2
70 beqz $a3, 4f
71 slti $t6, $t1, 72
72 lhu $t5, 0($t0)
73 addiu $t1, $t1, -2
74 addu $t2, $t2, $t5
75 addiu $t0, $t0, 2
77 slti $t6, $t1, 72
79 bnez $t6, 5f
80 nop
81 lw $v1, 0($t0)
82 lw $a3, 4($t0)
84 lw $a1, 8($t0)
85 srl $t7, $v1, 16
86 addu $t5, $t3, $t7
87 lw $t9, 12($t0)
88 addu $v1, $a0, $v1
89 srl $t6, $a3, 16
90 addu $a2, $t5, $t6
91 addu $t3, $v1, $a3
92 lw $t5, 16($t0)
93 srl $a3, $a1, 16
94 addu $v0, $a2, $a3
95 addu $t8, $t3, $a1
96 srl $a0, $t9, 16
97 lw $a1, 20($t0)
98 addu $t6, $v0, $a0
99 addu $v1, $t8, $t9
100 srl $t7, $t5, 16
101 lw $t9, 24($t0)
102 addu $a2, $t6, $t7
103 addu $t3, $v1, $t5
104 srl $a3, $a1, 16
105 lw $t5, 28($t0)
106 addu $v0, $a2, $a3
107 addu $t8, $t3, $a1
108 srl $a0, $t9, 16
109 lw $a1, 32($t0)
110 addu $t6, $v0, $a0
111 addu $v1, $t8, $t9
112 srl $t7, $t5, 16
113 lw $t9, 36($t0)
114 addu $a2, $t6, $t7
115 addu $t3, $v1, $t5
116 srl $a3, $a1, 16
117 lw $t5, 40($t0)
118 addu $v0, $a2, $a3
119 addu $t8, $t3, $a1
120 srl $a0, $t9, 16
121 lw $a1, 44($t0)
122 addu $t6, $v0, $a0
123 addu $v1, $t8, $t9
124 srl $t7, $t5, 16
125 lw $t9, 48($t0)
126 addu $a2, $t6, $t7
127 addu $t3, $v1, $t5
128 srl $a3, $a1, 16
129 lw $t5, 52($t0)
130 addu $v0, $a2, $a3
131 addu $t8, $t3, $a1
132 srl $a0, $t9, 16
133 lw $a1, 56($t0)
134 addu $v1, $t8, $t9
135 addu $t6, $v0, $a0
136 lw $t8, 60($t0)
137 srl $t7, $t5, 16
138 addu $t3, $v1, $t5
139 addu $a2, $t6, $t7
140 srl $a3, $a1, 16
141 addiu $t1, $t1, -64
142 addu $v0, $a2, $a3
143 addu $t9, $t3, $a1
144 srl $a0, $t8, 16
145 slti $t7, $t1, 72
146 addu $t3, $v0, $a0
147 lw $v1, 64($t0)
148 lw $a3, 68($t0)
149 addu $a0, $t9, $t8
150 beqz $t7, 6b
151 addiu $t0, $t0, 64
152 sll $v0, $t3, 16
153 subu $t9, $a0, $v0
154 addu $t8, $t2, $t9
155 addu $t2, $t8, $t3
157 slti $a0, $t1, 4
158 move $a1, $zero
159 bnez $a0, 7f
160 move $a2, $zero
162 lw $a3, 0($t0)
163 addiu $t1, $t1, -4
164 srl $v1, $a3, 16
165 slti $t3, $t1, 4
166 addu $a2, $a2, $v1
167 addiu $t0, $t0, 4
168 beqz $t3, 8b
169 addu $a1, $a1, $a3
171 sll $t6, $a2, 16
172 subu $t5, $a1, $t6
173 addu $a1, $t2, $t5
174 slti $v1, $t1, 2
175 b 9f
176 addu $t2, $a1, $a2
178 lhu $a2, 0($t0)
179 addiu $t1, $t1, -2
180 slti $v1, $t1, 2
181 addu $t2, $t2, $a2
182 addiu $t0, $t0, 2
184 beqz $v1, 10b
187 blez $t1, 11f
188 addiu $a0, $t1, -1
190 lbu $t7, 0($t0)
191 move $t1, $a0
192 addu $t2, $t2, $t7
193 addiu $t0, $t0, 1
194 bgtz $t1, 12b
195 addiu $a0, $a0, -1
197 srl $v1, $t2, 16
198 beqz $t4, 13f
199 andi $v0, $t2, 0xFFFF
200 srl $t3, $t2, 16
201 andi $a0, $t2, 0xFFFF
202 addu $v0, $a0, $t3
203 srl $t9, $v0, 16
204 andi $t8, $v0, 0xFFFF
205 addu $t4, $t8, $t9
206 andi $t0, $t4, 0xFF
207 srl $t2, $t4, 8
208 andi $v0, $t2, 0xFF
209 sll $v1, $t0, 8
211 addu $a1, $v0, $v1
212 srl $v1, $a1, 16
213 andi $a3, $a1, 0xFFFF
214 jr $ra
215 addu $v0, $a3, $v1
217 lbu $a2, 0($t0)
218 addiu $t1, $t1, -1
219 sll $t2, $a2, 8
220 addiu $t0, $t0, 1
221 b 14b
222 li $t4, 1
224 inet_chksum:
225 addiu $sp, $sp, -8
226 sw $ra, 0($sp)
227 bal _ip_cksum
228 andi $a1, $a1, 0xFFFF
229 b 1f
230 srl $v1, $v0, 16
232 andi $v0, $v0, 0xFFFF
233 addu $v0, $v0, $v1
234 srl $v1, $v0, 16
236 bnez $v1, 2b
238 nor $v1, $zero, $v0
239 lw $ra, 0($sp)
240 andi $v0, $v1, 0xFFFF
241 jr $ra
242 addiu $sp, $sp, 8
244 inet_chksum_pseudo:
245 addiu $sp, $sp, -56
246 sw $s7, 44($sp)
247 sw $s6, 40($sp)
248 sw $s4, 32($sp)
249 sw $s3, 28($sp)
250 sw $s2, 24($sp)
251 sw $s1, 20($sp)
252 sw $s0, 16($sp)
253 sw $ra, 48($sp)
254 sw $s5, 36($sp)
255 move $s0, $a0
256 move $s3, $a1
257 move $s4, $a2
258 andi $s6, $a3, 0xFF
259 lhu $s7, 72($sp)
260 move $s1, $zero
261 beqz $a0, 1f
262 move $s2, $zero
263 li $s5, 1
265 lw $a0, 4($s0)
266 bal _ip_cksum
267 lhu $a1, 10($s0)
268 b 2f
269 addu $s1, $s1, $v0
271 addu $s1, $v0, $a0
273 srl $a0, $s1, 16
274 bnez $a0, 3b
275 andi $v0, $s1, 0xFFFF
276 lhu $a0, 10($s0)
278 andi $v1, $a0, 1
279 beqz $v1, 4f
280 andi $t0, $s1, 0xFF00
281 andi $t1, $s1, 0xFF
282 subu $a1, $s5, $s2
283 sll $a3, $t1, 8
284 srl $a2, $t0, 8
285 or $s1, $a3, $a2
286 andi $s2, $a1, 0xFF
288 lw $s0, 0($s0)
290 bnez $s0, 5b
292 beqz $s2, 1f
293 andi $t4, $s1, 0xFF00
294 andi $t5, $s1, 0xFF
295 sll $t3, $t5, 8
296 srl $t2, $t4, 8
297 or $s1, $t3, $t2
299 lwl $v1, 3($s3)
300 lwl $ra, 3($s3)
301 lwr $v1, 0($s3)
302 lwl $s5, 3($s4)
303 lwr $ra, 0($s3)
304 lwl $s2, 3($s4)
305 andi $v0, $v1, 0xFFFF
306 lwr $s5, 0($s4)
307 addu $t8, $s1, $v0
308 srl $t9, $ra, 16
309 lwr $s2, 0($s4)
310 addu $s3, $t8, $t9
311 andi $s4, $s5, 0xFFFF
312 addu $t6, $s3, $s4
313 srl $t7, $s2, 16
314 move $a0, $s6
315 bal _htons
316 addu $s1, $t6, $t7
317 move $a0, $s7
318 bal _htons
319 addu $s0, $s1, $v0
320 b 6f
321 addu $v0, $s0, $v0
323 addu $v0, $s6, $a0
325 srl $a0, $v0, 16
326 bnez $a0, 7b
327 andi $s6, $v0, 0xFFFF
328 nor $s7, $zero, $v0
329 andi $v0, $s7, 0xFFFF
330 lw $ra, 48($sp)
331 lw $s7, 44($sp)
332 lw $s6, 40($sp)
333 lw $s5, 36($sp)
334 lw $s4, 32($sp)
335 lw $s3, 28($sp)
336 lw $s2, 24($sp)
337 lw $s1, 20($sp)
338 lw $s0, 16($sp)
339 jr $ra
340 addiu $sp, $sp, 56
342 inet_chksum_pbuf:
343 addiu $sp, $sp, -40
344 sw $s2, 24($sp)
345 sw $s1, 20($sp)
346 sw $s0, 16($sp)
347 sw $ra, 32($sp)
348 sw $s3, 28($sp)
349 move $s1, $a0
350 move $s0, $zero
351 beqz $a0, 1f
352 move $s2, $zero
353 li $s3, 1
355 lw $a0, 4($s1)
356 bal _ip_cksum
357 lhu $a1, 10($s1)
358 b 2f
359 addu $s0, $s0, $v0
361 addu $s0, $v0, $a0
363 srl $a0, $s0, 16
364 bnez $a0, 3b
365 andi $v0, $s0, 0xFFFF
366 lhu $a0, 10($s1)
368 andi $v1, $a0, 1
369 beqz $v1, 4f
370 subu $a1, $s3, $s2
371 andi $s2, $a1, 0xFF
372 andi $s0, $s0, 0xFFFF
374 lw $s1, 0($s1)
376 bnez $s1, 5b
378 beqz $s2, 6f
379 nor $t2, $zero, $s0
380 andi $t0, $s0, 0xFF00
381 andi $t1, $s0, 0xFF
382 sll $a3, $t1, 8
383 srl $a2, $t0, 8
384 or $s0, $a3, $a2
386 nor $t2, $zero, $s0
388 lw $ra, 32($sp)
389 lw $s3, 28($sp)
390 lw $s2, 24($sp)
391 lw $s1, 20($sp)
392 lw $s0, 16($sp)
393 andi $v0, $t2, 0xFFFF
394 jr $ra
395 addiu $sp, $sp, 40
397 inet_aton :
398 _inet_aton:
399 addiu $sp, $sp, -40
400 sw $s0, 32($sp)
401 sw $ra, 36($sp)
402 addiu $t7, $sp, 16
403 lbu $a2, 0($a0)
404 move $s0, $a1
405 move $t3, $t7
406 li $t4, 48
407 li $t5, 46
408 addiu $v0, $a2, -48
409 andi $a3, $v0, 0xFF
410 sltiu $v1, $a3, 10
411 beqz $v1, 1f
412 addiu $t6, $sp, 28
414 move $t0, $zero
415 beq $a2, $t4, 2f
416 li $t1, 10
418 li $t2, 16
420 addiu $t8, $a2, -97
421 andi $a1, $t8, 0xFF
422 addiu $v1, $a2, -32
423 sltiu $v0, $a3, 10
424 sltiu $v1, $v1, 96
425 beqz $v0, 3f
426 sltiu $a3, $a1, 6
428 mult $t0, $t1
429 addiu $a0, $a0, 1
430 mflo $t0
431 addu $a3, $t0, $a2
432 lbu $a2, 0($a0)
433 addiu $t0, $a3, -48
434 addiu $a1, $a2, -48
435 andi $a3, $a1, 0xFF
436 addiu $t8, $a2, -97
437 andi $a1, $t8, 0xFF
438 addiu $v1, $a2, -32
439 sltiu $v0, $a3, 10
440 sltiu $v1, $v1, 96
441 bnez $v0, 4b
442 sltiu $a3, $a1, 6
444 addiu $t9, $a2, -65
445 sltiu $a1, $a1, 26
446 bne $t1, $t2, 5f
447 sltiu $v0, $t9 ,6
448 beqz $v1, 5f
450 bnez $a3, 6f
452 beqz $v0, 5f
455 addiu $a0, $a0, 1
456 sll $v1, $t0, 4
457 bnez $a1, 7f
458 addiu $v0, $a2, -87
459 addiu $v0, $a2, -55
461 lbu $a2, 0($a0)
462 or $t0, $v1, $v0
463 addiu $ra, $a2, -48
464 b 8b
465 andi $a3, $ra, 0xFF
467 addiu $a0, $a0, 1
468 lbu $a2, 0($a0)
470 xori $t8, $a2, 0x78
471 xori $t2, $a2, 0x58
472 sltiu $a3, $t8, 1
473 sltiu $t1, $t2, 1
474 or $a1, $a3, $t1
475 beqz $a1, 9f
476 addiu $ra, $a2, -48
477 addiu $a0, $a0, 1
478 lbu $a2, 0($a0)
479 li $t1, 16
480 addiu $t9, $a2, -48
481 b 10b
482 andi $a3, $t9, 0xFF
484 bne $a2, $t5, 11f
486 sltu $a2, $t3, $t6
487 beqz $a2, 12f
488 move $v1, $zero
489 addiu $a0, $a0, 1
490 lbu $a2, 0($a0)
491 sw $t0, 0($t3)
492 addiu $v0, $a2, -48
493 andi $a3, $v0, 0xFF
494 sltiu $v1, $a3, 10
495 bnez $v1, 14b
496 addiu $t3, $t3, 4
498 move $v1, $zero
500 lw $ra, 36($sp)
501 lw $s0, 32($sp)
502 move $v0, $v1
503 jr $ra
504 addiu $sp, $sp, 40
506 andi $a3, $ra, 0xFF
507 b 10b
508 li $t1, 8
510 beqz $a2, 15f
511 subu $t6, $t3, $t7
512 addiu $t4, $a2, -32
513 sltiu $a0, $t4, 96
514 beqz $a0, 12b
515 move $v1, $zero
516 xori $v0, $a2, 0x20
517 xori $t2, $a2, 0xC
518 sltu $t6, $zero, $v0
519 sltu $t1, $zero, $t2
520 and $t5, $t6, $t1
521 beqz $t5, 15f
522 subu $t6, $t3, $t7
523 xori $t9, $a2, 0xA
524 xori $a3, $a2, 0xD
525 sltu $t8, $zero, $t9
526 sltu $a1, $zero, $a3
527 and $v1, $t8, $a1
528 bnez $v1, 16f
529 xori $t4, $a2, 0xB
530 subu $t6, $t3, $t7
532 sra $t7, $t6, 2
533 addiu $a0, $t7, 1
534 li $t3, 2
535 beq $a0, $t3, 17f
536 slti $t1, $a0, 3
537 beqz $t1, 18f
538 li $t2, 3
539 beqz $a0, 12b
540 move $v1, $zero
542 beqz $s0,19f
543 li $v1, 1
544 bal _htonl
545 move $a0, $t0
546 sw $v0, 0($s0)
547 li $v1, 1
549 lw $ra, 36($sp)
550 lw $s0, 32($sp)
551 move $v0, $v1
552 jr $ra
553 addiu $sp, $sp, 40
555 lui $a1, 0xFF
556 ori $v1, $a1, 0xFFFF
557 sltu $t8, $v1, $t0
558 bnez $t8, 12b
559 move $v1, $zero
560 lw $a3, 16($sp)
562 sll $v0, $a3, 24
563 b 10b
564 or $t0, $t0, $v0
566 beq $a0, $t2, 21f
567 li $v0, 4
568 bne $a0, $v0, 10b
569 sltiu $t7, $t0, 256
570 beqz $t7, 12b
571 move $v1, $zero
572 lw $a1, 16($sp)
573 lw $v1, 20($sp)
574 lw $t2, 24($sp)
575 sll $v0, $a1, 24
576 sll $t8, $v1, 16
577 or $t6, $v0, $t8
578 sll $t1, $t2, 8
579 or $v0, $t6, $t1
580 b 10b
581 or $t0, $t0, $v0
583 xori $t5, $a2, 0x9
584 sltu $a2, $zero, $t5
585 sltu $a0, $zero, $t4
586 and $ra, $a2, $a0
587 bnez $ra, 12b
588 move $v1, $zero
589 b 15b
590 subu $t6, $t3, $t7
592 li $ra, 0xFFFF
593 sltu $t9, $ra, $t0
594 bnez $t9, 12b
595 move $v1, $zero
596 lw $t3, 16($sp)
597 lw $t5, 20($sp)
598 sll $a0, $t3, 24
599 sll $t4, $t5, 16
600 or $a2, $a0, $t4
601 b 10b
602 or $t0, $t0, $a2
604 inet_addr:
605 addiu $sp, $sp, -32
606 sw $ra, 24($sp)
607 bal _inet_aton
608 addiu $a1, $sp, 16
609 beqz $v0, 1f
610 li $v1, -1
611 lw $v1, 16($sp)
613 lw $ra, 24($sp)
614 move $v0, $v1
615 jr $ra
616 addiu $sp, $sp, 32
618 inet_ntoa:
619 lui $v0, %hi( s_Str )
620 addiu $sp, $sp, -8
621 addiu $t5, $v0, %lo( s_Str )
622 sw $a0, 8($sp)
623 move $a3, $t5
624 addiu $t1, $sp, 8
625 move $t2, $zero
626 li $t0, 10
627 li $t4, 255
628 li $t3, 46
630 lbu $a1, 0($t1)
631 move $v1, $zero
633 divu $zero, $a1, $t0
634 addu $a0, $sp, $v1
635 addiu $v1, $v1, 1
636 andi $v1, $v1, 0xFF
637 mfhi $a1
638 addiu $t6, $a1, 48
639 mflo $a2
640 andi $a1, $a2, 0xFF
641 bnez $a1, 1b
642 sb $t6, 0($a0)
643 addiu $t7, $v1, -1
644 andi $v1, $t7, 0xFF
645 beq $v1, $t4, 2f
646 sb $a2, 0($t1)
647 li $a1, 255
649 addu $t9, $sp, $v1
650 lbu $a2, 0($t9)
651 addiu $t8, $v1, -1
652 andi $v1, $t8, 0xFF
653 sb $a2, 0($a3)
654 bne $v1, $a1, 3b
655 addiu $a3, $a3, 1
657 addiu $a0, $t2, 1
658 andi $t2, $a0, 0xFF
659 sltiu $v0, $t2, 4
660 sb $t3, 0($a3)
661 addiu $t1, $t1, 1
662 bnez $v0, 4b
663 addiu $a3, $a3, 1
664 move $v0, $t5
665 addiu $sp, $sp, 8
666 jr $ra
667 sb $zero, -1($a3)