From c33c3635f7a20a40af570342d9a7609d4a22c39e Mon Sep 17 00:00:00 2001 From: Jeff Johnston Date: Mon, 6 May 2002 20:44:54 +0000 Subject: [PATCH] 2002-05-06 Jeff Johnston * libc/include/sys/reent.h (_l64a_buf): New reentrant area. (_REENT_L64A_BUF): New macro for accessing area. * libc/stdlib/Makefile.am: Add a64l.c and l64a.c. * libc/stdlib/Makefile.in: Regenerated. * libc/stdlib/a64l.c: New file. * libc/stdlib/l64a.c: New file. --- newlib/ChangeLog | 9 ++ newlib/libc/include/sys/reent.h | 8 +- newlib/libc/stdlib/Makefile.am | 3 + newlib/libc/stdlib/Makefile.in | 48 ++++---- newlib/libc/stdlib/a64l.c | 192 ++++++++++++++++++++++++++++++++ newlib/libc/stdlib/l64a.c | 64 +++++++++++ 6 files changed, 301 insertions(+), 23 deletions(-) create mode 100644 newlib/libc/stdlib/a64l.c create mode 100644 newlib/libc/stdlib/l64a.c diff --git a/newlib/ChangeLog b/newlib/ChangeLog index 6ba3e1852..5b97d5c13 100644 --- a/newlib/ChangeLog +++ b/newlib/ChangeLog @@ -1,3 +1,12 @@ +2002-05-06 Jeff Johnston + + * libc/include/sys/reent.h (_l64a_buf): New reentrant area. + (_REENT_L64A_BUF): New macro for accessing area. + * libc/stdlib/Makefile.am: Add a64l.c and l64a.c. + * libc/stdlib/Makefile.in: Regenerated. + * libc/stdlib/a64l.c: New file. + * libc/stdlib/l64a.c: New file. + 2002-05-06 Jeff Johnston * libc/unix/pread.c: Fix typo for _pread_r. diff --git a/newlib/libc/include/sys/reent.h b/newlib/libc/include/sys/reent.h index b2a3b0f5b..7a5305328 100644 --- a/newlib/libc/include/sys/reent.h +++ b/newlib/libc/include/sys/reent.h @@ -253,6 +253,7 @@ struct _misc_reent int _mblen_state; int _wctomb_state; int _mbtowc_state; + char _l64a_buf[8]; }; /* This version of _reent is layed our with "int"s in pairs, to help @@ -394,6 +395,7 @@ struct _reent _r->_misc->_mblen_state = 0; \ _r->_misc->_wctomb_state = 0; \ _r->_misc->_mbtowc_state = 0; \ + _r->_misc->_l64a_buf[0] = '\0'; \ } while (0) #define _REENT_CHECK_MISC(var) \ _REENT_CHECK(var, _misc, struct _misc_reent *, sizeof *((var)->_misc), _REENT_INIT_MISC(var)) @@ -414,6 +416,7 @@ struct _reent #define _REENT_MBLEN_STATE(ptr) ((ptr)->_misc->_mblen_state) #define _REENT_MBTOWC_STATE(ptr)((ptr)->_misc->_mbtowc_state) #define _REENT_WCTOMB_STATE(ptr)((ptr)->_misc->_wctomb_state) +#define _REENT_L64A_BUF(ptr) ((ptr)->_misc->_l64a_buf) #else /* !_REENT_SMALL */ @@ -460,6 +463,7 @@ struct _reent int _mblen_state; int _mbtowc_state; int _wctomb_state; + char _l64a_buf[8]; } _reent; /* Two next two fields were once used by malloc. They are no longer used. They are used to preserve the space used before so as to @@ -492,7 +496,7 @@ struct _reent { 0,0,0,0,0,0,0,0}, 0, 1, \ {{_RAND48_SEED_0, _RAND48_SEED_1, _RAND48_SEED_2}, \ {_RAND48_MULT_0, _RAND48_MULT_1, _RAND48_MULT_2}, _RAND48_ADD}, \ - 0, 0, 0} } } + 0, 0, 0, ""} } } #define _REENT_INIT_PTR(var) \ { int i; \ @@ -532,6 +536,7 @@ struct _reent var->_new._reent._mblen_state = 0; \ var->_new._reent._mbtowc_state = 0; \ var->_new._reent._wctomb_state = 0; \ + var->_new._reent._l64a_buf[0] = '\0'; \ } #define _REENT_CHECK_RAND48(ptr) /* nothing */ @@ -557,6 +562,7 @@ struct _reent #define _REENT_MBLEN_STATE(ptr) ((ptr)->_new._reent._mblen_state) #define _REENT_MBTOWC_STATE(ptr)((ptr)->_new._reent._mbtowc_state) #define _REENT_WCTOMB_STATE(ptr)((ptr)->_new._reent._wctomb_state) +#define _REENT_L64A_BUF(ptr) ((ptr)->_new._reent._l64a_buf) #endif /* !_REENT_SMALL */ diff --git a/newlib/libc/stdlib/Makefile.am b/newlib/libc/stdlib/Makefile.am index 8c7687e70..aad103266 100644 --- a/newlib/libc/stdlib/Makefile.am +++ b/newlib/libc/stdlib/Makefile.am @@ -8,6 +8,7 @@ LIB_SOURCES = \ __adjust.c \ __exp10.c \ __ten_mu.c \ + a64l.c \ abort.c \ abs.c \ assert.c \ @@ -33,6 +34,7 @@ LIB_SOURCES = \ getenv_r.c \ getopt.c \ jrand48.c \ + l64a.c \ labs.c \ lcong48.c \ ldiv.c \ @@ -141,6 +143,7 @@ malloptr.$(oext): mallocr.c $(MALLOC_COMPILE) -DDEFINE_MALLOPT -c $(srcdir)/mallocr.c -o $@ CHEWOUT_FILES= \ + a64l.def \ abort.def \ abs.def \ assert.def \ diff --git a/newlib/libc/stdlib/Makefile.in b/newlib/libc/stdlib/Makefile.in index d56a3129c..95c56e667 100644 --- a/newlib/libc/stdlib/Makefile.in +++ b/newlib/libc/stdlib/Makefile.in @@ -106,6 +106,7 @@ LIB_SOURCES = \ __adjust.c \ __exp10.c \ __ten_mu.c \ + a64l.c \ abort.c \ abs.c \ assert.c \ @@ -131,6 +132,7 @@ LIB_SOURCES = \ getenv_r.c \ getopt.c \ jrand48.c \ + l64a.c \ labs.c \ lcong48.c \ ldiv.c \ @@ -200,6 +202,7 @@ libstdlib_la_LDFLAGS = -Xcompiler -nostdlib MALLOC_COMPILE = $(LIB_COMPILE) -DINTERNAL_NEWLIB CHEWOUT_FILES = \ + a64l.def \ abort.def \ abs.def \ assert.def \ @@ -257,13 +260,13 @@ LIBS = @LIBS@ @USE_LIBTOOL_FALSE@mallstatsr.$(oext) msizer.$(oext) malloptr.$(oext) \ @USE_LIBTOOL_FALSE@mallocr.$(oext) @USE_LIBTOOL_FALSE@lib_a_OBJECTS = __adjust.o __exp10.o __ten_mu.o \ -@USE_LIBTOOL_FALSE@abort.o abs.o assert.o atexit.o atof.o atoff.o \ -@USE_LIBTOOL_FALSE@atoi.o atol.o bsearch.o calloc.o div.o drand48.o \ -@USE_LIBTOOL_FALSE@dtoa.o dtoastub.o ecvtbuf.o efgcvt.o environ.o \ -@USE_LIBTOOL_FALSE@envlock.o eprintf.o erand48.o exit.o getenv.o \ -@USE_LIBTOOL_FALSE@getenv_r.o getopt.o jrand48.o labs.o lcong48.o \ -@USE_LIBTOOL_FALSE@ldiv.o ldtoa.o lrand48.o malign.o malloc.o mblen.o \ -@USE_LIBTOOL_FALSE@mblen_r.o mbstowcs.o mbstowcs_r.o mbtowc.o \ +@USE_LIBTOOL_FALSE@a64l.o abort.o abs.o assert.o atexit.o atof.o \ +@USE_LIBTOOL_FALSE@atoff.o atoi.o atol.o bsearch.o calloc.o div.o \ +@USE_LIBTOOL_FALSE@drand48.o dtoa.o dtoastub.o ecvtbuf.o efgcvt.o \ +@USE_LIBTOOL_FALSE@environ.o envlock.o eprintf.o erand48.o exit.o \ +@USE_LIBTOOL_FALSE@getenv.o getenv_r.o getopt.o jrand48.o l64a.o labs.o \ +@USE_LIBTOOL_FALSE@lcong48.o ldiv.o ldtoa.o lrand48.o malign.o malloc.o \ +@USE_LIBTOOL_FALSE@mblen.o mblen_r.o mbstowcs.o mbstowcs_r.o mbtowc.o \ @USE_LIBTOOL_FALSE@mbtowc_r.o mlock.o mprec.o mrand48.o msize.o \ @USE_LIBTOOL_FALSE@mstats.o mtrim.o nrand48.o putenv.o putenv_r.o \ @USE_LIBTOOL_FALSE@qsort.o rand.o rand48.o rand_r.o realloc.o seed48.o \ @@ -279,21 +282,22 @@ LTLIBRARIES = $(noinst_LTLIBRARIES) @USE_LIBTOOL_TRUE@mallinfor.$(oext) mallstatsr.$(oext) msizer.$(oext) \ @USE_LIBTOOL_TRUE@malloptr.$(oext) mallocr.$(oext) @USE_LIBTOOL_TRUE@libstdlib_la_OBJECTS = __adjust.lo __exp10.lo \ -@USE_LIBTOOL_TRUE@__ten_mu.lo abort.lo abs.lo assert.lo atexit.lo \ -@USE_LIBTOOL_TRUE@atof.lo atoff.lo atoi.lo atol.lo bsearch.lo calloc.lo \ -@USE_LIBTOOL_TRUE@div.lo drand48.lo dtoa.lo dtoastub.lo ecvtbuf.lo \ -@USE_LIBTOOL_TRUE@efgcvt.lo environ.lo envlock.lo eprintf.lo erand48.lo \ -@USE_LIBTOOL_TRUE@exit.lo getenv.lo getenv_r.lo getopt.lo jrand48.lo \ -@USE_LIBTOOL_TRUE@labs.lo lcong48.lo ldiv.lo ldtoa.lo lrand48.lo \ -@USE_LIBTOOL_TRUE@malign.lo malloc.lo mblen.lo mblen_r.lo mbstowcs.lo \ -@USE_LIBTOOL_TRUE@mbstowcs_r.lo mbtowc.lo mbtowc_r.lo mlock.lo mprec.lo \ -@USE_LIBTOOL_TRUE@mrand48.lo msize.lo mstats.lo mtrim.lo nrand48.lo \ -@USE_LIBTOOL_TRUE@putenv.lo putenv_r.lo qsort.lo rand.lo rand48.lo \ -@USE_LIBTOOL_TRUE@rand_r.lo realloc.lo seed48.lo setenv.lo setenv_r.lo \ -@USE_LIBTOOL_TRUE@srand48.lo strdup.lo strdup_r.lo strtod.lo strtol.lo \ -@USE_LIBTOOL_TRUE@strtoll.lo strtoll_r.lo strtoul.lo strtoull.lo \ -@USE_LIBTOOL_TRUE@strtoull_r.lo system.lo valloc.lo wcstombs.lo \ -@USE_LIBTOOL_TRUE@wcstombs_r.lo wctomb.lo wctomb_r.lo +@USE_LIBTOOL_TRUE@__ten_mu.lo a64l.lo abort.lo abs.lo assert.lo \ +@USE_LIBTOOL_TRUE@atexit.lo atof.lo atoff.lo atoi.lo atol.lo bsearch.lo \ +@USE_LIBTOOL_TRUE@calloc.lo div.lo drand48.lo dtoa.lo dtoastub.lo \ +@USE_LIBTOOL_TRUE@ecvtbuf.lo efgcvt.lo environ.lo envlock.lo eprintf.lo \ +@USE_LIBTOOL_TRUE@erand48.lo exit.lo getenv.lo getenv_r.lo getopt.lo \ +@USE_LIBTOOL_TRUE@jrand48.lo l64a.lo labs.lo lcong48.lo ldiv.lo \ +@USE_LIBTOOL_TRUE@ldtoa.lo lrand48.lo malign.lo malloc.lo mblen.lo \ +@USE_LIBTOOL_TRUE@mblen_r.lo mbstowcs.lo mbstowcs_r.lo mbtowc.lo \ +@USE_LIBTOOL_TRUE@mbtowc_r.lo mlock.lo mprec.lo mrand48.lo msize.lo \ +@USE_LIBTOOL_TRUE@mstats.lo mtrim.lo nrand48.lo putenv.lo putenv_r.lo \ +@USE_LIBTOOL_TRUE@qsort.lo rand.lo rand48.lo rand_r.lo realloc.lo \ +@USE_LIBTOOL_TRUE@seed48.lo setenv.lo setenv_r.lo srand48.lo strdup.lo \ +@USE_LIBTOOL_TRUE@strdup_r.lo strtod.lo strtol.lo strtoll.lo \ +@USE_LIBTOOL_TRUE@strtoll_r.lo strtoul.lo strtoull.lo strtoull_r.lo \ +@USE_LIBTOOL_TRUE@system.lo valloc.lo wcstombs.lo wcstombs_r.lo \ +@USE_LIBTOOL_TRUE@wctomb.lo wctomb_r.lo CFLAGS = @CFLAGS@ COMPILE = $(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) LTCOMPILE = $(LIBTOOL) --mode=compile $(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) diff --git a/newlib/libc/stdlib/a64l.c b/newlib/libc/stdlib/a64l.c new file mode 100644 index 000000000..9a56a9141 --- /dev/null +++ b/newlib/libc/stdlib/a64l.c @@ -0,0 +1,192 @@ +/* +FUNCTION +<>,<>---convert between radix-64 ascii string and long + +INDEX + a64l +INDEX + l64a + +ANSI_SYNOPSIS + #include + long a64l(const char *<[input]>); + char *l64a(long <[input]>); + +TRAD_SYNOPSIS + #include + long a64l(<[input]>) + const char *<[input]>; + + char *l64a(<[input]>) + long <[input]>; + +DESCRIPTION +Conversion is performed between long and radix-64 characters. The <> routine +transforms up to 32-bits of input value starting from least significant bits to +the most significant bits. The input value is split up into a maximum of 5 +groups of 6-bits and possibly one group of 2 bits (bits 31 and 30). + +Each group of 6 bits forms a value from 0-63 which is translated into a character +as follows: + + 0 = '.' + 1 = '/' + 2-11 = '0' to '9' + 12-37 = 'A' to 'Z' + 38-63 = 'a' to 'z' + +When remaining bits are zero or all bits have been translated, a nul terminator +is appended to the string. An input value of 0 results in the empty string. + +The <> performs the reverse translation. Each character is used to generate +a 6-bit value for up to 30 bits and then a 2-bit value to complete a 32-bit result. +The nul terminator means that the remaining digits are 0. An empty input string or +NULL string results in 0L. An invalid string results in undefined behavior. +If the size of a long is > 32 bits, the result is sign-extended. + +RETURNS +<> returns a nul-terminated string of 0 to 6 characters. +<> returns the 32-bit translated value from the input character string. + +PORTABILITY +<> and <> are non-ANSI and are defined by the Single Unix Specification. + +Supporting OS subroutines required: None. +*/ + +#include <_ansi.h> +#include +#include + +long +_DEFUN (a64l, (input), + const char *input) +{ + char *ptr; + char ch; + int i, digit; + unsigned long result = 0; + + if (input == NULL) + return 0; + + ptr = input; + + /* it easiest to go from most significant digit to least so find end of input or up + to 6 characters worth */ + for (i = 0; i < 6; ++i) + { + if (*ptr) + ++ptr; + } + + while (ptr > input) + { + ch = *(--ptr); + +#if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__) + if (ch >= 'a') + digit = (ch - 'a') + 38; + else if (ch >= 'A') + digit = (ch - 'A') + 12; + else if (ch >= '0') + digit = (ch - '0') + 2; + else if (ch == '/') + digit = 1; + else + digit = 0; +#else /* !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__) */ + switch (ch) + { + case '/': + digit = 1; + break; + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + digit = (ch - '0') + 2; + break; + case 'A': + case 'B': + case 'C': + case 'D': + case 'E': + case 'F': + case 'G': + case 'H': + case 'I': + case 'J': + case 'K': + case 'L': + case 'M': + case 'N': + case 'O': + case 'P': + case 'Q': + case 'R': + case 'S': + case 'T': + case 'U': + case 'V': + case 'W': + case 'X': + case 'Y': + case 'Z': + digit = (ch - 'A') + 12; + break; + case 'a': + case 'b': + case 'c': + case 'd': + case 'e': + case 'f': + case 'g': + case 'h': + case 'i': + case 'j': + case 'k': + case 'l': + case 'm': + case 'n': + case 'o': + case 'p': + case 'q': + case 'r': + case 's': + case 't': + case 'u': + case 'v': + case 'w': + case 'x': + case 'y': + case 'z': + digit = (ch - 'A') + 38; + break; + default: + digit = 0; + break; + } +#endif /* !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__) */ + + result = (result << 6) + digit; + } + +#if LONG_MAX > 2147483647 + /* for implementations where long is > 32 bits, the result must be sign-extended */ + if (result & 0x80000000) + return (((long)-1 >> 32) << 32) + result; +#endif + + return result; +} + + + + diff --git a/newlib/libc/stdlib/l64a.c b/newlib/libc/stdlib/l64a.c new file mode 100644 index 000000000..d415d0478 --- /dev/null +++ b/newlib/libc/stdlib/l64a.c @@ -0,0 +1,64 @@ +/* l64a - convert long to radix-64 ascii string + * + * Conversion is performed on at most 32-bits of input value starting + * from least significant bits to the most significant bits. + * + * The routine splits the input value into groups of 6 bits for up to + * 32 bits of input. This means that the last group may be 2 bits + * (bits 30 and 31). + * + * Each group of 6 bits forms a value from 0-63 which is converted into + * a character as follows: + * 0 = '.' + * 1 = '/' + * 2-11 = '0' to '9' + * 12-37 = 'A' to 'Z' + * 38-63 = 'a' to 'z' + * + * When the remaining bits are zero or all 32 bits have been translated, + * a nul terminator is appended to the resulting string. An input value of + * 0 results in an empty string. + */ + +#include <_ansi.h> +#include +#include + +static const char R64_ARRAY[] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + +char * +_DEFUN (l64a, (value), + long value) +{ + return _l64a_r (_REENT, value); +} + +char * +_DEFUN (_l64a_r, (rptr, value), + struct _reent *rptr _AND + long value) +{ + char *ptr; + char *result; + int i, index; + unsigned long tmp = (unsigned long)value & 0xffffffff; + + _REENT_CHECK_MISC(rptr); + result = _REENT_L64A_BUF(rptr); + ptr = result; + + for (i = 0; i < 6; ++i) + { + if (tmp == 0) + { + *ptr = '\0'; + break; + } + + index = tmp & (64 - 1); + *ptr++ = R64_ARRAY[index]; + tmp >>= 6; + } + + return result; +}