diff --git a/doc/funcref.sgml b/doc/funcref.sgml index 2a6d77adc..5fa1720e4 100644 --- a/doc/funcref.sgml +++ b/doc/funcref.sgml @@ -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="inet.h" name="arpa/inet.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="inet.h" name="arpa/inet.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="inet.h" name="arpa/inet.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="inet.h" name="arpa/inet.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> diff --git a/include/arpa/inet.h b/include/arpa/inet.h new file mode 100644 index 000000000..cd353a2bb --- /dev/null +++ b/include/arpa/inet.h @@ -0,0 +1,67 @@ +/*****************************************************************************/ +/* */ +/* arpa/inet.h */ +/* */ +/* Endianness utilities for cc65 */ +/* */ +/* */ +/* */ +/* (C) 2023 Colin Leroy-Mira, <colin@colino.net> */ +/* */ +/* */ +/* This software is provided 'as-is', without any expressed or implied */ +/* warranty. In no event will the authors be held liable for any damages */ +/* arising from the use of this software. */ +/* */ +/* Permission is granted to anyone to use this software for any purpose, */ +/* including commercial applications, and to alter it and redistribute it */ +/* freely, subject to the following restrictions: */ +/* */ +/* 1. The origin of this software must not be misrepresented; you must not */ +/* claim that you wrote the original software. If you use this software */ +/* in a product, an acknowledgment in the product documentation would be */ +/* appreciated but is not required. */ +/* 2. Altered source versions must be plainly marked as such, and must not */ +/* be misrepresented as being the original software. */ +/* 3. This notice may not be removed or altered from any source */ +/* distribution. */ +/* */ +/*****************************************************************************/ + + + +#ifndef _ARPA_INET_H +#define _ARPA_INET_H + + + +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ + + + +#if (__OPT_i__ < 200) +int __fastcall__ ntohs (int val); +int __fastcall__ htons (int val); +#else + +#define ntohs(x) \ + ( \ + __AX__=(x), \ + asm("sta tmp1"), \ + asm("txa"), \ + asm("ldx tmp1"), \ + __AX__ \ + ) +#define htons(x) ntohs(x) + +#endif + +long __fastcall__ ntohl (long val); +long __fastcall__ htonl (long val); + + + +/* End of arpa/inet.h */ +#endif diff --git a/libsrc/common/ntohl.s b/libsrc/common/ntohl.s new file mode 100644 index 000000000..77adba253 --- /dev/null +++ b/libsrc/common/ntohl.s @@ -0,0 +1,32 @@ +; +; 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 + +_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 diff --git a/libsrc/common/ntohs.s b/libsrc/common/ntohs.s new file mode 100644 index 000000000..042ddb005 --- /dev/null +++ b/libsrc/common/ntohs.s @@ -0,0 +1,16 @@ +; +; Colin Leroy-Mira <colin@colino.net>, 2023-09-06 +; +; int __fastcall__ ntohs (int val); +; + +.export _ntohs, _htons +.importzp tmp1 + +_htons := _ntohs + +_ntohs: + sta tmp1 + txa + ldx tmp1 + rts diff --git a/test/val/lib_common_htonl.c b/test/val/lib_common_htonl.c new file mode 100644 index 000000000..53a210a84 --- /dev/null +++ b/test/val/lib_common_htonl.c @@ -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; +} diff --git a/test/val/lib_common_htons.c b/test/val/lib_common_htons.c new file mode 100644 index 000000000..42bbb3d6b --- /dev/null +++ b/test/val/lib_common_htons.c @@ -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; +}