From owner-freebsd-questions@FreeBSD.ORG Wed Apr 9 19:21:55 2003 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 65E0A37B401 for ; Wed, 9 Apr 2003 19:21:55 -0700 (PDT) Received: from alpsgiken.alpsgiken.gr.jp (www.alpsgiken.gr.jp [210.166.150.61]) by mx1.FreeBSD.org (Postfix) with ESMTP id 421EF43F75 for ; Wed, 9 Apr 2003 19:21:54 -0700 (PDT) (envelope-from joel@alpsgiken.alpsgiken.gr.jp) Received: from zz_radiant2 (www1.alpsgiken.gr.jp [61.114.244.165]) by alpsgiken.alpsgiken.gr.jp (8.9.1a/3.7W) with ESMTP id LAA11754 for ; Thu, 10 Apr 2003 11:21:51 +0900 Date: Thu, 10 Apr 2003 11:26:44 +0900 From: Joel Rees To: freebsd-questions@freebsd.org In-Reply-To: <20030409145032.32369.qmail@web10907.mail.yahoo.com> References: <20030409145032.32369.qmail@web10907.mail.yahoo.com> Message-Id: <20030410105638.9A74.JOEL@alpsgiken.gr.jp> MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Mailer: Becky! ver. 2.00.11 Subject: Re: "unsigned char" portability issues X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 10 Apr 2003 02:21:55 -0000 (Line hard-wrapping by me.) > I am writing some free software which uses the "unsigned char" C type. > > Some people have been raising the issue that the "unsigned char" type > might be bigger than 8-bits and that there is therefore portability > issues with it. I think is still a standard header. It has constants that will help with portability issues. For instance, somewhere around the parts of your code that will fail if an unsigned char is not 8 bits, you can say #if CHAR_BIT != 8 || UCHAR_MAX != 255 # error "Can't compile on this architecture!" #endif and at least you can help people trying to move your code to a Cray or a Univac 1100 to spot what they will have to tweak. Using both of those tests might be overkill. (Now, I wonder why there isn't a UCHAR_MIN? ;) You might also be interested in doing something like #if CHAR_BIT == 8 && UCHAR_MAX == 255 #if CHAR_MAX == UCHAR_MAX typedef char my_char_t; #else typedef unsigned char my_char_t; #endif #else # error "Can't compile on this architecture!" #endif And, of course, that might also be overkill. > Yet this C type is used in OpenSSL and in the SHA and MD5 routines > shipped with FreeBSD, and the way in which it is used in these programs > implicitly assumes it is exactly one byte in size. > > Can someone please give me some good advice on this issue ? > > Does the "unsigned char" type vary in size for different FreeBSD architectures ? See Erik's comments, if you haven't already. If porting to Linux or the other BSDs is expected, you probably will want to use something like the above. If the sort of warning given above is insufficient, you might actually need to encapsulate more than just a typedef in these kinds of pre-processor tests. If you need further help on C, there are some newgroups out there, for instance, http://groups.google.com/groups?group=comp.lang.c HTH -- Joel Rees