From: "Tim Van Holder" To: "DJGPP-Workers" Subject: [patch] Second draft: a64l and l64a Date: Sun, 3 Jun 2001 21:06:47 +0200 Message-ID: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_0000_01C0EC71.19E87C20" X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook IMO, Build 9.0.2416 (9.0.2910.0) Importance: Normal X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 Reply-To: djgpp-workers AT delorie DOT com This is a multi-part message in MIME format. ------=_NextPart_000_0000_01C0EC71.19E87C20 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Second draft; both functions are now in their own file; a test program has been added. ------=_NextPart_000_0000_01C0EC71.19E87C20 Content-Type: application/octet-stream; name="radix64.diff" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="radix64.diff" Index: include/stdlib.h=0A= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A= RCS file: /cvs/djgpp/djgpp/include/stdlib.h,v=0A= retrieving revision 1.8=0A= diff -u -r1.8 stdlib.h=0A= --- include/stdlib.h 2001/01/20 22:04:14 1.8=0A= +++ include/stdlib.h 2001/06/03 18:53:12=0A= @@ -76,6 +76,9 @@=0A= =0A= #ifndef __STRICT_ANSI__=0A= =0A= +long a64l(const char *_s);=0A= +char * l64a(long _value);=0A= +=0A= #ifndef _POSIX_SOURCE=0A= =0A= typedef struct {=0A= Index: src/libc/posix/stdlib/makefile=0A= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A= RCS file: /cvs/djgpp/djgpp/src/libc/posix/stdlib/makefile,v=0A= retrieving revision 1.1=0A= diff -u -r1.1 makefile=0A= --- /dev/null Sun Jun 3 20:55:05 2001=0A= +++ src/libc/posix/stdlib/makefile Wed May 30 20:57:40 2001=0A= @@ -0,0 +1,7 @@=0A= +# Copyright (C) 2001 DJ Delorie, see COPYING.DJ for details=0A= +=0A= +TOP=3D../..=0A= +=0A= +SRC +=3D a64l.c l64a.c=0A= +=0A= +include $(TOP)/../makefile.inc=0A= Index: src/libc/posix/stdlib/a64l.c=0A= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A= RCS file: /cvs/djgpp/djgpp/src/libc/posix/stdlib/a64l.c,v=0A= retrieving revision 1.1=0A= diff -u -r1.1 a64l.c=0A= --- /dev/null Sun Jun 3 20:55:18 2001=0A= +++ src/libc/posix/stdlib/a64l.c Wed May 30 20:50:14 2001=0A= @@ -0,0 +1,38 @@=0A= +/* Copyright (C) 2001 DJ Delorie, see COPYING.DJ for details */=0A= +=0A= +#include =0A= +#include =0A= +#include =0A= +=0A= +long=0A= +a64l(const char* s)=0A= +{=0A= + int i =3D 0;=0A= + long value =3D 0;=0A= +=0A= + if (s =3D=3D NULL || *s =3D=3D '\0')=0A= + return 0L;=0A= +=0A= + for (i =3D 0; i < 6; ++i, ++s)=0A= + {=0A= + if (*s =3D=3D '\0')=0A= + break;=0A= + /* Detect overflow; return the conversion of '/2BIG/' */=0A= + if (value > (LONG_MAX >> 6))=0A= + return 1144341633L;=0A= + value <<=3D 6;=0A= + if (*s =3D=3D '.') /* 0 */=0A= + value +=3D 0;=0A= + else if (*s =3D=3D '/') /* 1 */=0A= + ++value;=0A= + else if (*s >=3D '0' && *s <=3D '9') /* 2-11 */=0A= + value +=3D (*s - '0') + 2;=0A= + else if (*s >=3D 'A' && *s <=3D 'Z') /* 12-37 */=0A= + value +=3D (*s - 'A') + 12;=0A= + else if (*s >=3D 'a' && *s <=3D 'z') /* 38-63 */=0A= + value +=3D (*s - 'a') + 38;=0A= + else /* invalid digit */=0A= + return 0L;=0A= + }=0A= + return value;=0A= +}=0A= Index: src/libc/posix/stdlib/a64l.txh=0A= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A= RCS file: /cvs/djgpp/djgpp/src/libc/posix/stdlib/a64l.txh,v=0A= retrieving revision 1.1=0A= diff -u -r1.1 a64l.txh=0A= --- /dev/null Sun Jun 3 20:55:21 2001=0A= +++ src/libc/posix/stdlib/a64l.txh Wed May 30 21:19:40 2001=0A= @@ -0,0 +1,36 @@=0A= +@node a64l, string=0A= +@subheading Syntax=0A= +=0A= +@example=0A= +#include =0A= +=0A= +long a64l(const char *s);=0A= +@end example=0A= +=0A= +@subheading Description=0A= +=0A= +This function takes a pointer to a radix-64 representation, with the=0A= +first digit the least significant, and returns the corresponding=0A= +@code{long} value.=0A= +=0A= +If @var{s} contains more than six characters, only the first six are=0A= +used. If the first six characters of @var{s} contain a null terminator,=0A= +only those characters before the null terminator are used.=0A= +@code{a64l()} will scan the string from left to right, with the least=0A= +significant digit on the left, decoding each character as a 6-bit = radix-64=0A= +number. If the @code{long} type contains more than 32 bits, the result = is=0A= +sign-extended.=0A= +=0A= +For a description of the radix-64 representation, @ref{l64a}.=0A= +=0A= +@subheading Return Value=0A= +=0A= +Returns the @code{long} value resulting from the conversion of the = contents=0A= +of @var{s}, or 0L if @var{s} is NULL, points to an empty string, or = points=0A= +to an invalid string (i.e. one not generated by a previous call to=0A= +@code{l64a()}). If the result would overflow a signed long, the=0A= +conversion of @samp{/2BIG/} (1144341633L) is returned.=0A= +=0A= +@subheading Portability=0A= +=0A= +@portability !ansi, posix=0A= Index: src/libc/posix/stdlib/l64a.c=0A= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A= RCS file: /cvs/djgpp/djgpp/src/libc/posix/stdlib/l64a.c,v=0A= retrieving revision 1.1=0A= diff -u -r1.1 l64a.c=0A= --- /dev/null Sun Jun 3 20:55:27 2001=0A= +++ src/libc/posix/stdlib/l64a.c Wed May 30 20:50:54 2001=0A= @@ -0,0 +1,35 @@=0A= +/* Copyright (C) 2001 DJ Delorie, see COPYING.DJ for details */=0A= +=0A= +#include =0A= +#include =0A= +#include =0A= +=0A= +char*=0A= +l64a(long value)=0A= +{=0A= + static char radix64[7] =3D { "" };=0A= + char *s =3D radix64 + 5;=0A= +=0A= + memset (radix64, 0, sizeof radix64);=0A= +=0A= + if (value <=3D 0)=0A= + return radix64;=0A= +=0A= + while (value && s >=3D radix64)=0A= + {=0A= + int digit =3D value & 0x3f;=0A= + value >>=3D 6;=0A= +=0A= + if (digit =3D=3D 0)=0A= + *s-- =3D '.';=0A= + else if (digit =3D=3D 1)=0A= + *s-- =3D '/';=0A= + else if (digit < 12)=0A= + *s-- =3D '0' + (digit - 2);=0A= + else if (digit < 38)=0A= + *s-- =3D 'A' + (digit - 12);=0A= + else=0A= + *s-- =3D 'a' + (digit - 38);=0A= + }=0A= + return ++s;=0A= +}=0A= Index: src/libc/posix/stdlib/l64a.txh=0A= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A= RCS file: /cvs/djgpp/djgpp/src/libc/posix/stdlib/l64a.txh,v=0A= retrieving revision 1.1=0A= diff -u -r1.1 l64a.txh=0A= --- /dev/null Sun Jun 3 20:55:30 2001=0A= +++ src/libc/posix/stdlib/l64a.txh Wed May 30 21:13:30 2001=0A= @@ -0,0 +1,41 @@=0A= +@node l64a, string=0A= +@subheading Syntax=0A= +=0A= +@example=0A= +#include =0A= +=0A= +char *l64a(long value);=0A= +@end example=0A= +=0A= +@subheading Description=0A= +=0A= +This function takes a @code{long} argument and returns a pointer to its=0A= +radix-64 representation. Negative numbers are not supported.=0A= +@c FIXME: Supporting negative values (or at least unsigned longs) seems=0A= +@c more logical; should we be POSIX-compliant here?=0A= +=0A= +@subheading Return Value=0A= +=0A= +A pointer to a static buffer containing the radix-64 representation of=0A= +@var{value}. Subsequent calls will overwrite the contents of this = buffer.=0A= +If @var{value} is negative or 0L, this function returns an empty string.=0A= +=0A= +@subheading Radix-64=0A= +@cindex radix-64=0A= +=0A= +The radix-64 @sc{ascii} representation is a notation whereby 32-bit = integers=0A= +are represented by up to 6 @sc{ascii} characters; each character = represents=0A= +a single radix-64 digit. Radix-64 refers to the fact that each digit = in this=0A= +representation can take 64 different values.=0A= +If the @code{long} type is more than 32 bits in size, only the low-order=0A= +32 bits are used.=0A= +The characters used to represent digits are @samp{.} (dot) for 0, = @samp{/}=0A= +for 1, @samp{0} through @samp{9} for 2 to 11, @samp{A} through @samp{Z} = for=0A= +12 to 37, and @samp{a} through @samp{z} for 38 to 63.=0A= +=0A= +Note that this is @emph{not} the same encoding used by either uuencode = or the=0A= +MIME base64 encoding.=0A= +=0A= +@subheading Portability=0A= +=0A= +@portability !ansi, posix=0A= Index: tests/libc/posix/stdlib/makefile=0A= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A= RCS file: /cvs/djgpp/djgpp/tests/libc/posix/stdlib/makefile,v=0A= retrieving revision 1.1=0A= diff -u -r1.1 makefile=0A= --- /dev/null Sun Jun 3 20:56:12 2001=0A= +++ tests/libc/posix/stdlib/makefile Wed May 30 21:02:12 2001=0A= @@ -0,0 +1,5 @@=0A= +TOP=3D../..=0A= +=0A= +SRC +=3D radix64.c=0A= +=0A= +include $(TOP)/../makefile.inc=0A= Index: tests/libc/posix/stdlib/radix64.c=0A= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A= RCS file: /cvs/djgpp/djgpp/tests/libc/posix/stdlib/radix64.c,v=0A= retrieving revision 1.1=0A= diff -u -r1.1 radix64.c=0A= --- /dev/null Sun Jun 3 20:56:21 2001=0A= +++ tests/libc/posix/stdlib/radix64.c Wed May 30 21:09:02 2001=0A= @@ -0,0 +1,39 @@=0A= +#include =0A= +#include =0A= +=0A= +int=0A= +main(void)=0A= +{=0A= + printf ("a64l(\"/.\") -> %ld\n", a64l("/."));=0A= + printf ("a64l(\"DJGPP\") -> %ld\n", a64l("DJGPP"));=0A= + printf ("a64l(\"/.Rules!\") -> %ld\n", a64l("/.Rules!"));=0A= + printf ("a64l(\"EliRules!\") -> %ld\n", a64l("EliRules!"));=0A= + printf ("a64l(NULL) -> %ld\n", a64l(NULL));=0A= + printf ("a64l(\"\") -> %ld\n", a64l(""));=0A= + printf ("a64l(\"Not Radix64\") -> %ld\n", a64l("Not Radix64"));=0A= +=0A= + printf ("a64l(l64a(1234)) -> %ld\n", a64l(l64a(1234)));=0A= + printf ("a64l(l64a(64)) -> %ld\n", a64l(l64a(64)));=0A= + printf ("a64l(l64a(7)) -> %ld\n", a64l(l64a(7)));=0A= + printf ("a64l(l64a(0)) -> %ld\n", a64l(l64a(0)));=0A= + printf ("a64l(l64a(-88)) -> %ld\n", a64l(l64a(-88)));=0A= + printf ("a64l(l64a(0x54534554)) -> 0x%lx\n", = a64l(l64a(0x54534554)));=0A= +=0A= + printf ("l64a(1234) -> '%s'\n", l64a(1234));=0A= + printf ("l64a(64) -> '%s'\n", l64a(64));=0A= + printf ("l64a(7) -> '%s'\n", l64a(7));=0A= + printf ("l64a(0) -> '%s'\n", l64a(0));=0A= + printf ("l64a(-88) -> '%s'\n", l64a(-88));=0A= + /* 0x54534554 is the binary representation of 'TEST' */=0A= + printf ("l64a(0x54534554) -> '%s'\n", l64a(0x54534554));=0A= + =0A= + printf ("l64a(a64l(\"/.\")) -> '%s'\n", l64a(a64l("/.")));=0A= + printf ("l64a(a64l(\"DJGPP\")) -> '%s'\n", l64a(a64l("DJGPP")));=0A= + printf ("l64a(a64l(\"/.Rules!\")) -> '%s'\n", = l64a(a64l("/.Rules!")));=0A= + printf ("l64a(a64l(\"EliRules!\")) -> '%s'\n", = l64a(a64l("EliRules!")));=0A= + printf ("l64a(a64l(NULL)) -> '%s'\n", l64a(a64l(NULL)));=0A= + printf ("l64a(a64l(\"\")) -> '%s'\n", l64a(a64l("")));=0A= + printf ("l64a(a64l(\"Not Radix64\")) -> '%s'\n", l64a(a64l("Not = Radix64")));=0A= +=0A= + return 0;=0A= +}=0A= ------=_NextPart_000_0000_01C0EC71.19E87C20--