📄 os_spec.h
字号:
order to perform range checking and allocate buffers. This gets a bit
tricky since not all systems have PATH_MAX, so we first try for PATH_MAX,
if that fails we try _POSIX_PATH_MAX (which is a generic 255 bytes and if
defined always seems to be less than whatever the real PATH_MAX should be),
if that also fails we grab stdio.h and try and get FILENAME_MAX, with an
extra check for PATH_MAX in case it's defined in stdio.h instead of
limits.h where it should be. FILENAME_MAX isn't really correct since it's
the maximum length of a filename rather than a path, but some environments
treat it as if it were PATH_MAX and in any case it's the best that we can
do in the absence of anything better */
#if defined( PATH_MAX )
#define MAX_PATH_LENGTH PATH_MAX
#elif defined( _POSIX_PATH_MAX )
#define MAX_PATH_LENGTH _POSIX_PATH_MAX
#elif defined( __FileX__ )
#define MAX_PATH_LENGTH FX_MAXIMUM_PATH
#else
#ifndef FILENAME_MAX
#include <stdio.h>
#endif /* FILENAME_MAX */
#if defined( PATH_MAX )
#define MAX_PATH_LENGTH PATH_MAX
#elif defined( MAX_PATH )
#define MAX_PATH_LENGTH MAX_PATH
#elif defined( FILENAME_MAX )
#define MAX_PATH_LENGTH FILENAME_MAX
#elif defined( __MSDOS16__ )
#define FILENAME_MAX 80
#else
#error Need to add a MAX_PATH_LENGTH define in misc/os_spec.h
#endif /* PATH_MAX, MAX_PATH, or FILENAME_MAX */
#endif /* PATH_MAX */
/* SunOS 4 doesn't have memmove(), but Solaris does, so we define memmove()
to bcopy() under 4. In addition SunOS doesn't define the fseek()
position indicators so we define these as well */
#if defined( __UNIX__ ) && defined( sun ) && ( OSVERSION == 4 )
#define memmove bcopy
#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2
#endif /* SunOS 4 */
/****************************************************************************
* *
* Charset Support *
* *
****************************************************************************/
/* Widechar handling. Most systems now support this, the only support that
we only require is the wchar_t type define.
Unfortunately in order to check for explicitly enabled widechar support
via config.h we have to include config.h at this point, because this
file, containing OS- and compiler-specific settings, both detects the
OSes and compilers that support widechars in the "OS Detection" section
above, and then sets the appropriate widechar settings here. In between
the two, config.h uses the OS/compiler-detection output to enable or
disable widechars as required, so we need to slip it in between the two
sections */
#if defined( INC_ALL )
#include "config.h"
#else
#include "misc/config.h"
#endif /* Compiler-specific includes */
#ifdef USE_WIDECHARS
#if !( ( defined( __QNX__ ) && ( OSVERSION <= 4 ) ) || \
( defined( __WIN32__ ) && defined( __BORLANDC__ ) ) || \
( defined( __WINCE__ ) && _WIN32_WCE < 400 ) || \
defined( __XMK__ ) )
#include <wchar.h>
#endif /* Systems with widechar support in stdlib.h */
#define WCSIZE ( sizeof( wchar_t ) )
#if defined( __MSDOS16__ ) && !defined( __BORLANDC__ )
typedef unsigned short int wchar_t; /* Widechar data type */
#endif /* OSes that don't support widechars */
#if defined( __BORLANDC__ ) && ( __BORLANDC__ == 0x410 )
#define wchar_t unsigned short int; /* BC++ 3.1 has an 8-bit wchar_t */
#endif /* BC++ 3.1 */
#else
/* No native widechar support, define the necesary types ourselves unless
we're running under older OS X (Darwin 6.x), which defines wchar_t in
stdlib.h even though there's no wchar support present, or PalmOS, which
defines it in wchar.h but then defines it differently in stddef.h, and
in any case has no wchar support present */
#if !( defined( __APPLE__ ) || defined( __MVS__ ) || \
defined( __OpenBSD__ ) || defined( __PALMOS__ ) )
typedef unsigned short int wchar_t;
#endif /* __APPLE__ */
#define WCSIZE ( sizeof( wchar_t ) )
#endif /* USE_WIDECHARS */
/* The EOL convention used when outputting text. Technically speaking
XMK doesn't use any particular EOL convention, but since the
typical development environment is debug output sent to a Windows
terminal emulator, we use CRLF */
#if defined( __MSDOS16__ ) || defined( __MSDOS32__ ) || \
defined( __OS2__ ) || defined( __SYMBIAN32__ ) || \
defined( __WINDOWS__ ) || defined( __XMK__ )
#define EOL "\r\n"
#define EOL_LEN 2
#elif ( defined( __APPLE__ ) && !defined( __MAC__ ) ) || \
defined( __BEOS__ ) || defined( __IBM4758__ ) || \
defined( __MVS__ ) || defined( __PALMOS__ ) || \
defined( __TANDEM_NSK__ ) || defined( __TANDEM_OSS__ ) || \
defined( __UNIX__ ) || defined( __VMCMS__ )
#define EOL "\n"
#define EOL_LEN 1
#elif defined( __MAC__ )
#define EOL "\r"
#define EOL_LEN 1
#else
#error "You need to add the OS-specific define to enable end-of-line handling"
#endif /* OS-specific EOL markers */
/* If we're compiling on IBM mainframes, enable EBCDIC <-> ASCII string
conversion. Since cryptlib uses ASCII internally for all strings, we
need to check to make sure it's been built with ASCII strings enabled
before we go any further */
#ifdef EBCDIC_CHARS
#if 'A' != 0x41
#error cryptlib must be compiled with ASCII literals
#endif /* Check for use of ASCII */
int asciiToEbcdic( char *dest, const char *src, const int length );
int ebcdicToAscii( char *dest, const char *src, const int length );
char *bufferToEbcdic( char *buffer, const char *string );
char *bufferToAscii( char *buffer, const char *string );
#endif /* IBM mainframes */
/* If we're compiling on Windows CE, enable Unicode <-> ASCII string
conversion */
#ifdef UNICODE_CHARS
int asciiToUnicode( wchar_t *dest, const int destMaxLen,
const char *src, const int length );
int unicodeToAscii( char *dest, const int destMaxLen,
const wchar_t *src, const int length );
#endif /* Windows CE */
/* Since cryptlib uses ASCII internally, we have to force the use of
ASCII-compatible versions of system library functions if the system
uses EBCDIC */
#ifdef EBCDIC_CHARS
#define ASCII_ALPHA 0x01
#define ASCII_LOWER 0x02
#define ASCII_NUMERIC 0x04
#define ASCII_SPACE 0x08
#define ASCII_UPPER 0x10
#define ASCII_HEX 0x20
extern const BYTE asciiCtypeTbl[];
#define isAlnum( ch ) \
( asciiCtypeTbl[ ch ] & ( ASCII_ALPHA | ASCII_NUMERIC ) )
#define isAlpha( ch ) \
( asciiCtypeTbl[ ch ] & ASCII_ALPHA )
#define isDigit( ch ) \
( asciiCtypeTbl[ ch ] & ASCII_NUMERIC )
#define isPrint( ch ) \
( ( ch ) >= 0x20 && ( ch ) <= 0x7E )
#define isXDigit( ch ) \
( asciiCtypeTbl[ ch ] & ASCII_HEX )
#define toLower( ch ) \
( ( asciiCtypeTbl[ ch ] & ASCII_UPPER ) ? ( ch ) + 32 : ( ch ) )
#define toUpper( ch ) \
( ( asciiCtypeTbl[ ch ] & ASCII_LOWER ) ? ( ch ) - 32 : ( ch ) )
int strCompareZ( const char *src, const char *dest );
int strCompare( const char *src, const char *dest, int length );
#define atoi aToI
#define sprintf_s sPrintf_s
#define vsprintf_s sPrintf_s
#else
#include <ctype.h>
#define isAlnum( ch ) isalnum( ch )
#define isAlpha( ch ) isalpha( ch )
#define isDigit( ch ) isdigit( ch )
#define isPrint( ch ) isprint( ch )
#define isXDigit( ch ) isxdigit( ch )
#define toLower( ch ) tolower( ch )
#define toUpper( ch ) toupper( ch )
#define strCompareZ( str1, str2 ) \
stricmp( str1, str2 )
#define strCompare( str1, str2, len ) \
strnicmp( str1, str2, len )
#endif /* EBCDIC_CHARS */
/* SunOS and older Slowaris have broken sprintf() handling. In SunOS 4.x
this was documented as returning a pointer to the output data as per the
Berkeley original. Under Slowaris the manpage was changed so that it
looks like any other sprintf(), but it still returns the pointer to the
output buffer in some versions so we use a wrapper that checks at
runtime to see what we've got and adjusts its behaviour accordingly */
#if defined( sun ) && ( OSVERSION <= 5 )
int fixedSprintf( char *buffer, const int bufSize,
const char *format, ... );
#undef sPrintf_s
#define sPrintf_s fixedSprintf
#endif /* Old SunOS */
/* Borland C++ before 5.50 doesn't have snprintf() or vsnprintf() */
#if defined( __BORLANDC__ ) && ( __BORLANDC__ < 0x550 )
#include <stdarg.h>
int bcSnprintf( char *buffer, const int bufSize,
const char *format, ... );
int bcVsnprintf( char *buffer, const int bufSize,
const char *format, va_list argPtr );
#endif /* BC++ before 5.50 */
/****************************************************************************
* *
* TR 24731 Safe stdlib Extensions *
* *
****************************************************************************/
/* ISO/IEC TR 24731 defines alternative stdlib functions designed to perform
additional parameter checking and avoid some types of common buffer
overflows. We use these if possible, if they're not available we map
them down to the traditional stdlib equivalents, via the preprocessor if
possible or using wrapper functions if not. In addition we use the
OpenBSD et al strlcpy()/strlcat() functions, whose truncation semantics
make them more useful than the TR 24731 equivalents (for example
strcpy_s() does nothing on overflow while the equivalent strlcpy() copies
with truncation). Microsoft recognise this as well, implementing them in
TR 24731 by allowing the caller to specify _TRUNCATE semantics */
#ifdef __STDC_LIB_EXT1__
#if defined( _MSC_VER ) && VC_GE_2005( _MSC_VER )
/* The VC++ implementation of TR 24731 is based on preliminary versions
of the design for the spec, and in some cases needs re-mapping onto
the final versions. Instances of this are:
TR 24731: struct tm *gmtime_s( const time_t *timer, struct tm *result );
VC++: errno_t gmtime_s( struct tm *result, const time_t timer );
Because this could potentially result in a circular definition, we
have to kludge in an intermediate layer by renaming the call to
gmTime_s(), which we then remap to the VC++ gmtime_s() */
#define gmTime_s( timer, result ) \
( ( gmtime_s( result, timer ) == 0 ) ? result : NULL )
/* Complicating things further, the Windows DDK doesn't have gmtime_s(),
although it does have all of the other TR 24731 functions. To handle
this, we use the same workaround as for the non-TR 24731 libcs */
#ifdef WIN_DDK
#undef gmTime_s
#define gmTime_s( timer, result ) gmtime( timer )
#endif /* WIN_DDK */
/* MS implements strlcpy/strlcat-equivalents via the TR 24731
functions */
#define strlcpy_s( s1, s1max, s2 ) strncpy_s( s1, s1max, s2, _TRUNCATE )
#define strlcat_s( s1, s1max, s2 ) strncat_s( s1, s1max, s2, _TRUNCATE )
#else
#define gmTime_s gmtime_s
#endif /* VC++ 2005 */
#else
/* String functions. The OpenBSD strlcpy()/strlcat() functions with their
truncation semantics are quite useful so we use these as well,
overlaying them with a macro that makes them match the TR 24731 look
and feel */
#define strcpy_s( s1, s1max, s2 ) strcpy( s1, s2 )
#if defined( __UNIX__ ) && \
( defined( __APPLE__ ) || defined( __FreeBSD__ ) || \
defined( __NetBSD__ ) || defined( __OpenBSD__ ) || \
( defined( sun ) && OSVERSION >= 7 ) )
/* Despite the glibc maintainer's pigheaded opposition to these
functions, some Unix OSes support them via custom libc patches */
#define strlcpy_s( s1, s1max, s2 ) strlcpy( s1, s2, s1max )
#define strlcat_s( s1, s1max, s2 ) strlcat( s1, s2, s1max )
#else
int strlcpy_s( char *dest, const int destLen, const char *src );
int strlcat_s( char *dest, const int destLen, const char *src );
#define NO_NATIVE_STRLCPY
#endif /* OpenBSD safe string functions */
/* Widechar functions */
int mbstowcs_s( size_t *retval, wchar_t *dst, size_t dstmax,
const char *src, size_t len );
int wcstombs_s( size_t *retval, char *dst, size_t dstmax,
const wchar_t *src, size_t len );
/* printf() */
#if defined( _MSC_VER ) && VC_LT_2005( _MSC_VER )
#define sprintf_s _snprintf
#define vsprintf_s _vsnprintf
#elif defined( __BORLANDC__ ) && ( __BORLANDC__ < 0x550 )
#define sprintf_s bcSnprintf
#define vsprintf_s bcVsnprintf
#elif defined( __QNX__ ) && ( OSVERSION <= 4 )
/* snprintf() exists under QNX 4.x but causes a SIGSEGV when called */
#define sprintf_s _bprintf
#define vsnprintf _vbprintf
#elif defined( EBCDIC_CHARS )
/* We provide our own replacements for these functions which handle
output in ASCII (rather than EBCDIC) form */
#else
#define sprintf_s snprintf
#define vsprintf_s vsnprintf
#endif /* VC++ 6 or below */
/* Misc.functions */
#define gmTime_s( timer, result ) gmtime( timer )
#endif /* TR 24731 safe stdlib extensions */
#endif /* _OSSPEC_DEFINED */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -