xref: /illumos-gate/usr/src/lib/libc/port/gen/l64a.c (revision 3db86aab)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*	Copyright (c) 1988 AT&T	*/
30 /*	  All Rights Reserved  	*/
31 
32 
33 /*
34  * convert long int to base 64 ascii
35  * char set is [./0-9A-Za-z]
36  * two's complement negatives are assumed,
37  * but no assumptions are made about sign propagation on right shift
38  *
39  */
40 
41 #pragma weak l64a = _l64a
42 
43 #include "synonyms.h"
44 #include "mtlib.h"
45 #include "libc.h"
46 #include <values.h>
47 #include <synch.h>
48 #include <thread.h>
49 #include <stdlib.h>
50 #include <sys/types.h>
51 #include "tsd.h"
52 
53 #define	BITSPERCHAR	6 /* to hold entire character set */
54 #define	BITSUSED	(BITSPERBYTE * sizeof (int))
55 #define	NMAX		((BITSUSED + BITSPERCHAR - 1)/BITSPERCHAR)
56 #define	SIGN		(-(1 << (BITSUSED - BITSPERCHAR - 1)))
57 #define	CHARMASK	((1 << BITSPERCHAR) - 1)
58 #define	WORDMASK	((1 << ((NMAX - 1) * BITSPERCHAR)) - 1)
59 
60 char *
61 l64a(long value)
62 {
63 	/* XPG4: only the lower 32 bits are used */
64 	int lg = (int)value;
65 	char *buf = tsdalloc(_T_L64A, NMAX + 1, NULL);
66 	char *s = buf;
67 
68 	if (buf == NULL)
69 		return (NULL);
70 
71 	while (lg != 0) {
72 
73 		int c = (lg & CHARMASK) + ('0' - 2);
74 
75 		if (c > '9')
76 			c += 'A' - '9' - 1;
77 		if (c > 'Z')
78 			c += 'a' - 'Z' - 1;
79 		*s++ = (char)c;
80 		/* fill high-order CHAR if negative */
81 		/* but suppress sign propagation */
82 		lg = ((lg < 0) ? (lg >> BITSPERCHAR) | SIGN :
83 			lg >> BITSPERCHAR) & WORDMASK;
84 	}
85 	*s = '\0';
86 	return (buf);
87 }
88