From: "Tim Van Holder" To: "Eli Zaretskii" Cc: Subject: Re: [patch] Second draft: a64l and l64a Date: Tue, 5 Jun 2001 19:36:03 +0200 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook IMO, Build 9.0.2416 (9.0.2910.0) In-reply-to: Importance: Normal X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 Reply-To: djgpp-workers AT delorie DOT com Errors-To: nobody AT delorie DOT com X-Mailing-List: djgpp-workers AT delorie DOT com X-Unsubscribes-To: listserv AT delorie DOT com Precedence: bulk > > +For a description of the radix-64 representation, @ref{l64a}. > > Please add "see" before @ref. What you wrote will look wrong in the > printed manual: > > For a description of the radix-64 representation, section `l64a', > page 1234. > > That is why @ref always needs something like "see", or "refer to", or > "as described in", etc., before it. > Drat - fix follows. This can then (hopefully) be considered the final draft. Note: texinfo changes are untested - for some reason, makeinfo suddenly sees fit to segfault when trying to build libc.info :-( Index: include/stdlib.h =================================================================== RCS file: /cvs/djgpp/djgpp/include/stdlib.h,v retrieving revision 1.8 diff -u -r1.8 stdlib.h --- include/stdlib.h 2001/01/20 22:04:14 1.8 +++ include/stdlib.h 2001/06/05 17:24:59 @@ -76,6 +76,9 @@ #ifndef __STRICT_ANSI__ +long a64l(const char *_s); +char * l64a(long _value); + #ifndef _POSIX_SOURCE typedef struct { --- /dev/null Tue Jun 5 19:26:02 2001 +++ src/libc/posix/stdlib/a64l.c Mon Jun 4 11:50:36 2001 @@ -0,0 +1,38 @@ +/* Copyright (C) 2001 DJ Delorie, see COPYING.DJ for details */ + +#include +#include +#include + +long +a64l(const char* s) +{ + int i = 0; + unsigned long value = 0; + + if (s == NULL || *s == '\0') + return 0L; + + for (i = 0; i < 6; ++i, ++s) + { + if (*s == '\0') + break; + /* Detect overflow; return the conversion of '/2BIG/' */ + if (value > (ULONG_MAX >> 6)) + return 1144341633L; + value <<= 6; + if (*s == '.') /* 0 */ + value += 0; + else if (*s == '/') /* 1 */ + ++value; + else if (*s >= '0' && *s <= '9') /* 2-11 */ + value += (*s - '0') + 2; + else if (*s >= 'A' && *s <= 'Z') /* 12-37 */ + value += (*s - 'A') + 12; + else if (*s >= 'a' && *s <= 'z') /* 38-63 */ + value += (*s - 'a') + 38; + else /* invalid digit */ + return 0L; + } + return (long) value; +} --- /dev/null Tue Jun 5 19:26:02 2001 +++ src/libc/posix/stdlib/a64l.txh Tue Jun 5 19:15:46 2001 @@ -0,0 +1,37 @@ +@node a64l, string +@subheading Syntax + +@example +#include + +long a64l(const char *string); +@end example + +@subheading Description + +This function takes a pointer to a radix-64 representation, with the +first digit the least significant, and returns the corresponding +@code{long} value. + +If @var{string} contains more than six characters, only the first six are +used. If the first six characters of @var{string} contain a null terminator, +only those characters before the null terminator are used. +@code{a64l} will scan the string from left to right, with the least +significant digit on the left, decoding each character as a 6-bit radix-64 +number. + +The radix-64 representation used by this function is described in the +documentation for the @code{l64a} function (@pxref{l64a}). + +@subheading Return Value + +Returns the @code{long} value resulting from the conversion of the contents +of @var{string}, or @code{0L} if @var{string} is @code{NULL}, points to an +empty string, or points to an invalid string (i.e. one not generated by a +previous call to @code{l64a}). If the result would overflow a @code{long}, +the conversion of @samp{/2BIG/} (@code{1144341633L}) is returned. + +@subheading Portability + +@portability !ansi, posix +@port-note Posix 1003.1-200x, not POSIX-1:1996 --- /dev/null Tue Jun 5 19:26:02 2001 +++ src/libc/posix/stdlib/l64a.c Mon Jun 4 11:51:16 2001 @@ -0,0 +1,36 @@ +/* Copyright (C) 2001 DJ Delorie, see COPYING.DJ for details */ + +#include +#include +#include + +char* +l64a(long _value) +{ + static char radix64[7] = { "" }; + char *s = radix64 + 5; + unsigned long value = (unsigned long) _value; + + memset (radix64, 0, sizeof radix64); + + if (value == 0) + return radix64; + + while (value && s >= radix64) + { + int digit = value & 0x3f; + value >>= 6; + + if (digit == 0) + *s-- = '.'; + else if (digit == 1) + *s-- = '/'; + else if (digit < 12) + *s-- = '0' + (digit - 2); + else if (digit < 38) + *s-- = 'A' + (digit - 12); + else + *s-- = 'a' + (digit - 38); + } + return ++s; +} --- /dev/null Tue Jun 5 19:26:02 2001 +++ src/libc/posix/stdlib/l64a.txh Mon Jun 4 22:21:46 2001 @@ -0,0 +1,42 @@ +@node l64a, string +@subheading Syntax + +@example +#include + +char *l64a(long value); +@end example + +@subheading Description + +This function takes a @code{long} argument and returns a pointer to its +radix-64 representation. Negative values are supported as well. +The resulting string can be turned back into a @code{long} value by the +@code{a64l} function (@pxref{a64l)). + +@subheading Return Value + +A pointer to a static buffer containing the radix-64 representation of +@var{value}. Subsequent calls will overwrite the contents of this buffer. +If @var{value} is @code{0L}, this function returns an empty string. + +@subheading Radix-64 +@cindex radix-64 + +The radix-64 @sc{ascii} representation is a notation whereby 32-bit integers +are represented by up to 6 @sc{ascii} characters; each character represents +a single radix-64 digit. Radix-64 refers to the fact that each digit in this +representation can take 64 different values. +If the @code{long} type is more than 32 bits in size, only the low-order +32 bits are used. +The characters used to represent digits are @samp{.} (dot) for 0, @samp{/} +for 1, @samp{0} through @samp{9} for 2 to 11, @samp{A} through @samp{Z} for +12 to 37, and @samp{a} through @samp{z} for 38 to 63. + +Note that this is @emph{not} the same encoding used by either uuencode or the +MIME base64 encoding. + +@subheading Portability + +@portability !ansi, posix +@port-note Posix 1003.1-200x, not POSIX-1:1996 --- /dev/null Tue Jun 5 19:26:02 2001 +++ src/libc/posix/stdlib/makefile Mon Jun 4 22:16:02 2001 @@ -0,0 +1,8 @@ +# Copyright (C) 2001 DJ Delorie, see COPYING.DJ for details + +TOP=../.. + +SRC += a64l.c +SRC += l64a.c + +include $(TOP)/../makefile.inc --- /dev/null Tue Jun 5 19:26:02 2001 +++ tests/libc/posix/stdlib/makefile Sun Jun 3 21:04:24 2001 @@ -0,0 +1,5 @@ +TOP=../.. + +SRC += radix64.c + +include $(TOP)/../makefile.inc --- /dev/null Tue Jun 5 19:26:02 2001 +++ tests/libc/posix/stdlib/radix64.c Mon Jun 4 19:08:00 2001 @@ -0,0 +1,39 @@ +#include +#include + +int +main(void) +{ + printf ("a64l(\"/.\") -> %ld\n", a64l("/.")); + printf ("a64l(\"DJGPP\") -> %ld\n", a64l("DJGPP")); + printf ("a64l(\"/.Rules!\") -> %ld\n", a64l("/.Rules!")); + printf ("a64l(\"Rhubarb!\") -> %ld\n", a64l("Rhubarb!")); + printf ("a64l(NULL) -> %ld\n", a64l(NULL)); + printf ("a64l(\"\") -> %ld\n", a64l("")); + printf ("a64l(\"Not Radix64\") -> %ld\n", a64l("Not Radix64")); + + printf ("a64l(l64a(1234)) -> %ld\n", a64l(l64a(1234))); + printf ("a64l(l64a(64)) -> %ld\n", a64l(l64a(64))); + printf ("a64l(l64a(7)) -> %ld\n", a64l(l64a(7))); + printf ("a64l(l64a(0)) -> %ld\n", a64l(l64a(0))); + printf ("a64l(l64a(-88)) -> %ld\n", a64l(l64a(-88))); + /* 0x54534554 is the binary representation of 'TEST' */ + printf ("a64l(l64a(0x54534554)) -> 0x%lx\n", a64l(l64a(0x54534554))); + + printf ("l64a(1234) -> '%s'\n", l64a(1234)); + printf ("l64a(64) -> '%s'\n", l64a(64)); + printf ("l64a(7) -> '%s'\n", l64a(7)); + printf ("l64a(0) -> '%s'\n", l64a(0)); + printf ("l64a(-88) -> '%s'\n", l64a(-88)); + printf ("l64a(0x54534554) -> '%s'\n", l64a(0x54534554)); + + printf ("l64a(a64l(\"/.\")) -> '%s'\n", l64a(a64l("/."))); + printf ("l64a(a64l(\"DJGPP\")) -> '%s'\n", l64a(a64l("DJGPP"))); + printf ("l64a(a64l(\"/.Rules!\")) -> '%s'\n", l64a(a64l("/.Rules!"))); + printf ("l64a(a64l(\"Rhubarb!\")) -> '%s'\n", l64a(a64l("Rhubarb!"))); + printf ("l64a(a64l(NULL)) -> '%s'\n", l64a(a64l(NULL))); + printf ("l64a(a64l(\"\")) -> '%s'\n", l64a(a64l(""))); + printf ("l64a(a64l(\"Not Radix64\")) -> '%s'\n", l64a(a64l("Not Radix64"))); + + return 0; +}