Hi,
yes, getting rid of the endian #defines is one thing.
But the edian things are only used for converting an
unsigned long into a 4-byte array in big-endian notation
and vice versa (+ idem for unsigned shorts) in the
following way:
- swap unsigned long if needed
- memcpy(byte array, &(the unsigned long), 4)
It's nicer/simpler to make a function that does both,
pls. see the attached patch.
Cheers,
Stef
Ludovic Rousseau wrote:
> Hello,
>
> On 20/06/06, Andreas Jellinghaus <aj dungeon.inka.de> wrote:
>
>> hmm, I don't know how the code looks right now or
would look after your
>> change. but I'd prefer to have the code without
#ifdef and then have
>> some define macro or function (maybe inline?) swap
bytes around (or
>> not).
>> i.e. make the code easy to read and only have the
macro/function look
>> at the endian of the architecture.
>
>
> I agree.
>
>> also I wonder if there are no functions we can use.
endian problems are
>> not new, for example the whole network code has
this host to network
>> conversions etc, so I wonder.
>
>
> Yes. The network code can use
> #include <arpa/inet.h>
> uint32_t htonl(uint32_t hostlong);
> uint16_t htons(uint16_t hostshort);
> uint32_t ntohl(uint32_t netlong);
> uint16_t ntohs(uint16_t netshort);
>
> Also according to the manpage: "On the i80x86 the
host byte order is
> Least Significant Byte first, whereas the network
byte order, as
> used on the Internet, is Most Significant Byte
first."
>
> So the byte order is swapped on a i386 host. But I
needed the reverse
> in my CCID code (do not swap on i386 but swap on
PowerPC)
>
> According to the code in Stef' mail it should be
possible to replace
> if (BIG_ENDIAN)
> x = bswap_32(x);
> by
> x = htonl(x);
>
> Bye,
>
Index: sc.c
============================================================
=======
--- sc.c (revision 2977)
+++ sc.c (working copy)
 -110,6
+110,32 
return 0;
}
+void ulong2bebytes(u8 *buf, unsigned long x)
+{
+ buf[3] = (u8) (x % 256);
+ x /= 256;
+ buf[2] = (u8) (x % 256);
+ x /= 256;
+ buf[1] = (u8) (x % 256);
+ buf[0] = (u8) (x / 256);
+}
+
+void ushort2bebytes(u8 *buf, unsigned short x)
+{
+ buf[1] = (u8) (x % 256);
+ buf[0] = (u8) (x / 256);
+}
+
+unsigned long bebytes2ulong(const u8 *buf)
+{
+ return (unsigned long) (256 * (256 * (256 * buf[0] +
buf[1]) + buf[2]) + buf[3]);
+}
+
+unsigned short bebytes2ushort(const u8 *buf)
+{
+ return (unsigned short) (256 * buf[0] + buf[1]);
+}
+
int sc_format_oid(struct sc_object_id *oid, const char *in)
{
int ii, ret = SC_ERROR_INVALID_ARGUMENTS;
Index: muscle.c
============================================================
=======
--- muscle.c (revision 2977)
+++ muscle.c (working copy)
 -41,27
+41,6 
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#endif
-#if defined(_WIN32) || defined(_WIN64)
-static unsigned long bswap_32(unsigned long x)
-{
- unsigned long res = x % 256;
- x /= 256;
- res = 256 * res + x % 256;
- x /= 256;
- res = 256 * res + x % 256;
- x /= 256;
- return 256 * res + x % 256;
-}
-static unsigned short bswap_16(unsigned short x)
-{
- return 256 * (x % 256) + (x / 256);
-}
-#define BIG_ENDIAN 1
-#else
-#include <endian.h>
-#include <byteswap.h>
-#endif
-
int msc_list_objects(sc_card_t* card, u8 next,
mscfs_file_t* file) {
sc_apdu_t apdu;
u8 fileData[14];
 -89,17
+68,11 
return SC_ERROR_UNKNOWN_DATA_RECEIVED;
}
memcpy(file->objectId, fileData, 4);
- file->size = *(int*)(fileData + 4);
- file->read = *(short*)(fileData + 8);
- file->write = *(short*)(fileData + 10);
- file->delete = *(short*)(fileData + 12);
-
- if(BIG_ENDIAN) {
- file->size = bswap_32(file->size);
- file->read = bswap_16(file->read);
- file->write = bswap_16(file->write);
- file->delete = bswap_16(file->delete);
- }
+ file->size = bebytes2ulong(fileData + 4);
+ file->read = bebytes2ushort(fileData + 8);
+ file->write = bebytes2ushort(fileData + 10);
+ file->delete = bebytes2ushort(fileData + 12);
+
return 1;
}
 -108,16
+81,13 
u8 buffer[9];
sc_apdu_t apdu;
int r;
- unsigned int le_offset = offset;
sc_format_apdu(card, &apdu, SC_APDU_CASE_4_SHORT,
0x56, 0x00, 0x00);
- if(BIG_ENDIAN)
- le_offset = bswap_32(le_offset);
if (card->ctx->debug >= 2)
- sc_debug(card->ctx, "READ: Offset: %x\tLength:
%i\n", le_offset, dataLength);
- memcpy(buffer, &le_objectId, 4);
- memcpy(buffer + 4, &le_offset, 4);
+ sc_debug(card->ctx, "READ: Offset: %x\tLength:
%i\n", offset, dataLength);
+ ulong2bebytes(buffer, le_objectId);
+ ulong2bebytes(buffer + 4, offset);
buffer[8] = (u8)dataLength;
apdu.data = buffer;
apdu.datalen = 9;
 -182,21
+152,15 
apdu.data = buffer,
apdu.datalen = 14;
- if(BIG_ENDIAN) {
- objectSize = bswap_32(objectSize);
- readAcl = bswap_16(readAcl);
- writeAcl = bswap_16(writeAcl);
- deleteAcl = bswap_16(deleteAcl);
- }
- memcpy(buffer, &objectId, 4);
- memcpy(buffer + 4, &objectSize, 4);
- memcpy(buffer + 8, &readAcl, 2);
- memcpy(buffer + 10, &writeAcl, 2);
- memcpy(buffer + 12, &deleteAcl, 2);
+ ulong2bebytes(buffer, objectId);
+ ulong2bebytes(buffer + 4, objectSize);
+ ushort2bebytes(buffer + 8, readAcl);
+ ushort2bebytes(buffer + 10, writeAcl);
+ ushort2bebytes(buffer + 12, deleteAcl);
r = sc_transmit_apdu(card, &apdu);
SC_TEST_RET(card->ctx, r, "APDU transmit
failed");
if(apdu.sw1 == 0x90 && apdu.sw2 == 0x00)
- return BIG_ENDIAN ? bswap_32(objectSize) : objectSize;
+ return objectSize;
if(apdu.sw1 == 0x9C) {
if(apdu.sw2 == 0x01) {
SC_FUNC_RETURN(card->ctx, 2,
SC_ERROR_MEMORY_FAILURE);
 -210,8
+174,8 
sc_debug(card->ctx, "got strange SWs: 0x%02X
0x%02X\n",
apdu.sw1, apdu.sw2);
}
- msc_zero_object(card, objectId, BIG_ENDIAN ?
bswap_32(objectSize) : objectSize);
- return BIG_ENDIAN ? bswap_32(objectSize) : objectSize;
+ msc_zero_object(card, objectId, objectSize);
+ return objectSize;
}
/* Update up to 246 bytes */
 -219,18
+183,14 
{
u8 buffer[256];
sc_apdu_t apdu;
- unsigned int le_offset;
int r;
sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT,
0x54, 0x00, 0x00);
apdu.lc = dataLength + 9;
- le_offset = offset;
- if(BIG_ENDIAN)
- le_offset = bswap_32(le_offset);
if (card->ctx->debug >= 2)
- sc_debug(card->ctx, "WRITE: Offset: %x\tLength:
%i\n", le_offset, dataLength);
- memcpy(buffer, &le_objectId, 4);
- memcpy(buffer + 4, &le_offset, 4);
+ sc_debug(card->ctx, "WRITE: Offset: %x\tLength:
%i\n", offset, dataLength);
+ ulong2bebytes(buffer, le_objectId);
+ ulong2bebytes(buffer + 4, offset);
buffer[8] = (u8)dataLength;
memcpy(buffer + 9, data, dataLength);
apdu.data = buffer;
 -270,11
+230,13 
int msc_delete_object(sc_card_t *card, unsigned int
objectId, int zero)
{
sc_apdu_t apdu;
+ u8 buf[4];
int r;
sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT,
0x52, 0x00, zero ? 0x01 : 0x00);
apdu.lc = 4;
- apdu.data = (u8*)&objectId;
+ ulong2bebytes(buf, objectId);
+ apdu.data = buf;
apdu.datalen = 4;
r = sc_transmit_apdu(card, &apdu);
SC_TEST_RET(card->ctx, r, "APDU transmit
failed");
 -434,8
+396,6 
{
sc_apdu_t apdu;
int r, location, cse, len;
- short dataLength_le = dataLength;
- short seedLength_le = seedLength;
u8 *buffer, *ptr;
location = (dataLength < 255) ? 1 : 2; /* 1 == APDU, 2
== (seed in 0xFFFFFFFE, out in 0xFFFFFFFF) */
 -444,17
+404,13 
assert(seedLength < 251);
assert(dataLength < 255); /* Output buffer doesn't
seem to operate as desired.... nobody can read/delete */
- if(BIG_ENDIAN) {
- dataLength_le = bswap_16(dataLength_le);
- seedLength_le = bswap_16(seedLength_le);
- }
buffer = malloc(len);
if(!buffer) SC_FUNC_RETURN(card->ctx, 0,
SC_ERROR_OUT_OF_MEMORY);
ptr = buffer;
- memcpy(ptr, &dataLength_le, 2);
+ ushort2bebytes(ptr, dataLength);
ptr+=2;
- memcpy(ptr, &seedLength_le, 2);
+ ushort2bebytes(ptr, seedLength);
ptr+=2;
if(seedLength > 0) {
memcpy(ptr, seedData, seedLength);
 -524,32
+480,24 
assert(privateKey <= 0x0F && publicKey <=
0x0F);
sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT,
0x30, privateKey, publicKey);
- if(BIG_ENDIAN) {
- keySize = bswap_16(keySize);
- prRead = bswap_16(prRead);
- prWrite = bswap_16(prWrite);
- prCompute = bswap_16(prCompute);
- puRead = bswap_16(puRead);
- puWrite = bswap_16(puWrite);
- puCompute = bswap_16(puCompute);
- }
+
*ptr = algorithm; ptr++;
- memcpy(ptr, &keySize, 2);
+ ushort2bebytes(ptr, keySize);
ptr+=2;
- memcpy(ptr, &prRead, 2);
+ ushort2bebytes(ptr, prRead);
ptr+=2;
- memcpy(ptr, &prWrite, 2);
+ ushort2bebytes(ptr, prWrite);
ptr+=2;
- memcpy(ptr, &prCompute, 2);
+ ushort2bebytes(ptr, prCompute);
ptr+=2;
- memcpy(ptr, &puRead, 2);
+ ushort2bebytes(ptr, puRead);
ptr+=2;
- memcpy(ptr, &puWrite, 2);
+ ushort2bebytes(ptr, puWrite);
ptr+=2;
- memcpy(ptr, &puCompute, 2);
+ ushort2bebytes(ptr, puCompute);
ptr+=2;
*ptr = 0; /* options; -- no options for now, they need
extra data */
 -870,8
+818,7 
/* USED IN KEY ITEM WRITING */
#define CPYVAL(valName) \
- length = BIG_ENDIAN ? bswap_16(data->valName ## Length)
: data->valName ## Length; \
- memcpy(p, &length, 2); p+= 2; \
+ ushort2bebytes(p, data->valName ## Length); p+= 2; \
memcpy(p, data->valName ## Value, data->valName ##
Length); p+= data->valName ## Length
int msc_import_key(sc_card_t *card,
 -905,13
+852,6 
SC_FUNC_RETURN(card->ctx, 2,
SC_ERROR_INVALID_ARGUMENTS)
}
- if(BIG_ENDIAN) {
- read = bswap_16(read);
- write = bswap_16(write);
- use = bswap_16(use);
- keySize = bswap_16(keySize);
- }
-
if(data->keyType == 0x02) {
bufferSize = 4 + 4 + data->pLength +
data->modLength;
} else if(data->keyType == 0x03) {
 -924,14
+864,12 
p = buffer;
*p = 0x00; p++; /* Encoding plain */
*p = data->keyType; p++; /* RSA_PRIVATE */
- memcpy(p, &keySize, 2); p+=2; /* key size */
+ ushort2bebytes(p, keySize); p+=2; /* key size */
if(data->keyType == 0x02) {
- unsigned int length;
CPYVAL(mod);
CPYVAL(p);
} else if(data->keyType == 0x03) {
- unsigned int length;
CPYVAL(p);
CPYVAL(q);
CPYVAL(pq);
 -939,9
+877,7 
CPYVAL(dq1);
}
objectId = 0xFFFFFFFEul;
- if(BIG_ENDIAN) {
- objectId = bswap_32(objectId);
- }
+
r = msc_create_object(card, objectId, bufferSize, 0x02,
0x02, 0x02);
if(r < 0) {
if(r == SC_ERROR_FILE_ALREADY_EXISTS) {
 -967,9
+903,9 
apdu.data = apduBuffer;
apdu.datalen = 6;
p = apduBuffer;
- memcpy(p, &read, 2); p+=2;
- memcpy(p, &write, 2); p+=2;
- memcpy(p, &use, 2); p+=2;
+ ushort2bebytes(p, read); p+=2;
+ ushort2bebytes(p, write); p+=2;
+ ushort2bebytes(p, use); p+=2;
r = sc_transmit_apdu(card, &apdu);
SC_TEST_RET(card->ctx, r, "APDU transmit
failed");
if(apdu.sw1 == 0x90 && apdu.sw2 == 0x00) {
Index: card-muscle.c
============================================================
=======
--- card-muscle.c (revision 2977)
+++ card-muscle.c (working copy)
 -137,7
+137,7 
objectSize = file->size;
muscle_parse_acls(file, &read, &write,
&delete);
- r = msc_create_object(card, *(int*)objectId, objectSize,
read, write, delete);
+ r = msc_create_object(card, bebytes2ulong(objectId),
objectSize, read, write, delete);
mscfs_clear_cache(fs);
if(r >= 0) return 0;
return r;
 -183,7
+183,7 
objectId[1] = objectId[3];
objectId[2] = objectId[3] = 0;
}
- r = msc_read_object(card, *(int*)objectId, index, buf,
count);
+ r = msc_read_object(card, bebytes2ulong(objectId), index,
buf, count);
SC_FUNC_RETURN(card->ctx, 0, r);
}
 -209,22
+209,22 
u8* buffer = malloc(newFileSize);
if(buffer == NULL) SC_FUNC_RETURN(card->ctx, 0,
SC_ERROR_OUT_OF_MEMORY);
- r = msc_read_object(card, *(int*)objectId, 0, buffer,
file->size);
+ r = msc_read_object(card, bebytes2ulong(objectId), 0,
buffer, file->size);
/* TODO: RETREIVE ACLS */
if(r < 0) goto update_bin_free_buffer;
- r = msc_delete_object(card, *(int*)objectId, 0);
+ r = msc_delete_object(card, bebytes2ulong(objectId), 0);
if(r < 0) goto update_bin_free_buffer;
- r = msc_create_object(card, *(int*)objectId, newFileSize,
0,0,0);
+ r = msc_create_object(card, bebytes2ulong(objectId),
newFileSize, 0,0,0);
if(r < 0) goto update_bin_free_buffer;
memcpy(buffer + index, buf, count);
- r = msc_update_object(card, *(int*)objectId, 0, buffer,
newFileSize);
+ r = msc_update_object(card, bebytes2ulong(objectId), 0,
buffer, newFileSize);
if(r < 0) goto update_bin_free_buffer;
file->size = newFileSize;
update_bin_free_buffer:
free(buffer);
SC_FUNC_RETURN(card->ctx, 0, r);
} else {
- r = msc_update_object(card, *(int*)objectId, index, buf,
count);
+ r = msc_update_object(card, bebytes2ulong(objectId),
index, buf, count);
}
//mscfs_clear_cache(fs);
return r;
 -234,7
+234,7 
{
mscfs_t *fs = MUSCLE_FS(card);
u8 *id = file_data->objectId;
- int objectId = *(int*)id;
+ int objectId = bebytes2ulong(id);
int r;
if(!file_data->ef) {
Index: muscle-filesystem.h
============================================================
=======
--- muscle-filesystem.h (revision 2977)
+++ muscle-filesystem.h (working copy)
 -63,5
+63,4 
int mscfs_check_selection(mscfs_t *fs, int requiredItem);
int mscfs_loadFileInfo(mscfs_t* fs, const u8 *path, int
pathlen, mscfs_file_t **file_data, int* index);
-
#endif
Index: opensc.h
============================================================
=======
--- opensc.h (revision 2977)
+++ opensc.h (working copy)
 -1081,6
+1081,16 
int sc_hex_to_bin(const char *in, u8 *out, size_t *outlen);
int sc_bin_to_hex(const u8 *, size_t, char *, size_t, int
separator);
+
+/* unsigned long -> 4 bytes in big endian order */
+void ulong2bebytes(u8 *buf, unsigned long x);
+/* unsigned short -> 2 bytes in big endian order */
+void ushort2bebytes(u8 *buf, unsigned short x);
+/* 4 bytes in big endian order -> unsigned long */
+unsigned long bebytes2ulong(const u8 *buf);
+/* 2 bytes in big endian order -> unsigned long */
+unsigned short bebytes2ushort(const u8 *buf);
+
scconf_block *sc_get_conf_block(sc_context_t *ctx, const
char *name1, const char *name2, int priority);
/**
* Converts a given OID in ascii form to a internal
sc_object_id object
_______________________________________________
opensc-devel mailing list
opensc-devel lists.opensc-project.org
http://www.opensc-project.org/mailman/listinfo/opensc
-devel |