Add ntohs, htons, ntohl, htons.

This commit is contained in:
Colin Leroy-Mira 2023-09-06 08:09:00 +02:00
parent e52e350498
commit dfe7562f76
6 changed files with 217 additions and 0 deletions

View File

@ -435,6 +435,16 @@ see also <tt>testcode/lib/em-test.c</tt> and <tt>samples/multidemo.c</tt>.
(incomplete)
<sect1><tt/inet.h/<label id="inet.h"><p>
<itemize>
<item><ref id="htonl" name="htonl">
<item><ref id="htons" name="htons">
<item><ref id="ntohl" name="ntohl">
<item><ref id="ntohs" name="ntohs">
</itemize>
<sect1><tt/geos.h/<label id="geos.h"><p>
<url url="geos.html" name="GEOS API">.
@ -4388,6 +4398,45 @@ to undefined behaviour.
</descrip>
</quote>
<sect1>htonl<label id="htonl"><p>
<quote>
<descrip>
<tag/Function/Swaps byte order in a 32 bit word.
<tag/Header/<tt/<ref id="stdlib.h" name="stdlib.h">/
<tag/Declaration/<tt/int htonl(val)/
<tag/Description/Converts a 32 bit word from from network byte order
(big endian) to little endian (or vice-versa).
<tag/Notes/<itemize>
<item>The function is only available as fastcall function, so it may only
be used in presence of a prototype.
</itemize>
<tag/See also/
<ref id="ntohl" name="ntohl">
<tag/Availability/cc65
</descrip>
</quote>
<sect1>htons<label id="htons"><p>
<quote>
<descrip>
<tag/Function/Swaps byte order in a 16 bit word.
<tag/Header/<tt/<ref id="stdlib.h" name="stdlib.h">/
<tag/Declaration/<tt/int htons(val)/
<tag/Description/Converts a 16 bit word from from network byte order
(big endian) to little endian (or vice-versa) by swapping both its bytes.
<tag/Notes/<itemize>
<item>The function is only available as fastcall function, so it may only
be used in presence of a prototype.
</itemize>
<tag/See also/
<ref id="ntohs" name="ntohs">
<tag/Availability/cc65
</descrip>
</quote>
<sect1>isalnum<label id="isalnum"><p>
@ -5757,6 +5806,44 @@ memory allocated for the driver.
</descrip>
</quote>
<sect1>ntohl<label id="ntohl"><p>
<quote>
<descrip>
<tag/Function/Swaps byte order in a 32 bit word.
<tag/Header/<tt/<ref id="stdlib.h" name="stdlib.h">/
<tag/Declaration/<tt/int __fastcall__ ntohl (int val);/
<tag/Description/Converts a 32 bit word from from host byte order (little endian)
to big endian (or vice-versa).
<tag/Notes/<itemize>
<item>The function is only available as fastcall function, so it may only
be used in presence of a prototype.
</itemize>
<tag/See also/
<ref id="htonl" name="htonl">
<tag/Availability/cc65
</descrip>
</quote>
<sect1>ntohs<label id="ntohs"><p>
<quote>
<descrip>
<tag/Function/Swaps byte order in a 16 bit word.
<tag/Header/<tt/<ref id="stdlib.h" name="stdlib.h">/
<tag/Declaration/<tt/int __fastcall__ ntohs (int val);/
<tag/Description/Converts a 16 bit word from from host byte order (little endian)
to big endian (or vice-versa) by swapping both its bytes.
<tag/Notes/<itemize>
<item>The function is only available as fastcall function, so it may only
be used in presence of a prototype.
</itemize>
<tag/See also/
<ref id="htons" name="htons">
<tag/Availability/cc65
</descrip>
</quote>
<sect1>offsetof<label id="offsetof"><p>
<quote>

10
include/arpa/inet.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef _ARPA_INET_H
#define _ARPA_INET_H
int __fastcall__ ntohs (int val);
int __fastcall__ htons (int val);
long __fastcall__ ntohl (long val);
long __fastcall__ htonl (long val);
#endif

34
libsrc/common/ntohl.s Normal file
View File

@ -0,0 +1,34 @@
;
; Colin Leroy-Mira <colin@colino.net>, 2023-09-06
;
; int __fastcall__ ntohl (long val);
;
.export _ntohl, _htonl
.import popa
.importzp tmp1, tmp2, sreg
_htonl := _ntohl
.code
_ntohl:
; The parts of our 32 bit word
; are in sreg+1, sreg, X, A.
; Save A and X
stx tmp1
sta tmp2
; Invert high word
lda sreg+1
ldx sreg
; Invert low word
ldy tmp1
sty sreg
ldy tmp2
sty sreg+1
rts

18
libsrc/common/ntohs.s Normal file
View File

@ -0,0 +1,18 @@
;
; Colin Leroy-Mira <colin@colino.net>, 2023-09-06
;
; int __fastcall__ ntohs (int val);
;
.export _ntohs, _htons
.importzp tmp1
_htons := _ntohs
.code
_ntohs:
sta tmp1
txa
ldx tmp1
rts

View File

@ -0,0 +1,34 @@
/*
!!DESCRIPTION!! A small test for htons.
!!ORIGIN!!
!!LICENCE!!
!!AUTHOR!! Colin Leroy-Mira
*/
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
static unsigned int Failures = 0;
static void CheckHtonl (long input, long expected)
{
long result = htonl(input);
if (result != expected) {
printf ("htonl error:\n"
" result = %ld for %ld, should be %ld\n", result, input, expected);
++Failures;
}
}
int main (void)
{
CheckHtonl(0x00000000, 0x00000000);
CheckHtonl(0x12345678, 0x78563412);
CheckHtonl(0xAABBCCDD, 0xDDCCBBAA);
CheckHtonl(0xFFFFFFFF, 0xFFFFFFFF);
printf ("Failures: %u\n", Failures);
return Failures;
}

View File

@ -0,0 +1,34 @@
/*
!!DESCRIPTION!! A small test for htons.
!!ORIGIN!!
!!LICENCE!!
!!AUTHOR!! Colin Leroy-Mira
*/
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
static unsigned int Failures = 0;
static void CheckHtons (int input, int expected)
{
int result = htons(input);
if (result != expected) {
printf ("htons error:\n"
" result = %d for %d, should be %d\n", result, input, expected);
++Failures;
}
}
int main (void)
{
CheckHtons(0x0000, 0x0000);
CheckHtons(0x1234, 0x3412);
CheckHtons(0xA0F2, 0xF2A0);
CheckHtons(0xFFFF, 0xFFFF);
printf ("Failures: %u\n", Failures);
return Failures;
}