mariadb/bdb/common/db_byteorder.c

75 lines
1.4 KiB
C
Raw Normal View History

2001-03-05 01:42:05 +01:00
/*-
* See the file LICENSE for redistribution information.
*
2002-10-30 12:57:05 +01:00
* Copyright (c) 1996-2002
2001-03-05 01:42:05 +01:00
* Sleepycat Software. All rights reserved.
*/
#include "db_config.h"
#ifndef lint
2002-10-30 12:57:05 +01:00
static const char revid[] = "$Id: db_byteorder.c,v 11.8 2002/02/01 18:15:29 bostic Exp $";
2001-03-05 01:42:05 +01:00
#endif /* not lint */
#ifndef NO_SYSTEM_INCLUDES
#include <sys/types.h>
#endif
#include "db_int.h"
2002-10-30 12:57:05 +01:00
/*
* __db_isbigendian --
* Return 1 if big-endian (Motorola and Sparc), not little-endian
* (Intel and Vax). We do this work at run-time, rather than at
* configuration time so cross-compilation and general embedded
* system support is simpler.
*
* PUBLIC: int __db_isbigendian __P((void));
*/
int
__db_isbigendian()
{
union { /* From Harbison & Steele. */
long l;
char c[sizeof(long)];
} u;
u.l = 1;
return (u.c[sizeof(long) - 1] == 1);
}
2001-03-05 01:42:05 +01:00
/*
* __db_byteorder --
* Return if we need to do byte swapping, checking for illegal
* values.
*
* PUBLIC: int __db_byteorder __P((DB_ENV *, int));
*/
int
__db_byteorder(dbenv, lorder)
DB_ENV *dbenv;
int lorder;
{
2002-10-30 12:57:05 +01:00
int is_bigendian;
is_bigendian = __db_isbigendian();
2001-03-05 01:42:05 +01:00
switch (lorder) {
case 0:
break;
case 1234:
2002-10-30 12:57:05 +01:00
if (is_bigendian)
return (DB_SWAPBYTES);
2001-03-05 01:42:05 +01:00
break;
case 4321:
2002-10-30 12:57:05 +01:00
if (!is_bigendian)
return (DB_SWAPBYTES);
2001-03-05 01:42:05 +01:00
break;
default:
__db_err(dbenv,
"unsupported byte order, only big and little-endian supported");
return (EINVAL);
}
return (0);
}