rewrite bebyte conversion functions: NULL check, return buffer

Rewrite bebyte conversion functions:
* check whether the buffer passed is non-NULL
* for conversions to bebytes, return the buffer passed

Signed-off-by: Peter Marschall <peter@adpm.de>

git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@5498 c6295689-39f2-0310-b995-f0e70906c6a9
This commit is contained in:
martin 2011-05-23 17:33:45 +00:00
parent fa259c63d5
commit 15cdf5367d
2 changed files with 24 additions and 12 deletions

View File

@ -86,14 +86,16 @@ int _sc_free_atr(struct sc_context *ctx, struct sc_card_driver *driver);
* Convert an unsigned long into 4 bytes in big endian order
* @param buf the byte array for the result, should be 4 bytes long
* @param x the value to be converted
* @return the buffer passed, containing the converted value
*/
void ulong2bebytes(u8 *buf, unsigned long x);
u8 *ulong2bebytes(u8 *buf, unsigned long x);
/**
* Convert an unsigned long into 2 bytes in big endian order
* @param buf the byte array for the result, should be 2 bytes long
* @param x the value to be converted
* @return the buffer passed, containing the converted value
*/
void ushort2bebytes(u8 *buf, unsigned short x);
u8 *ushort2bebytes(u8 *buf, unsigned short x);
/**
* Convert 4 bytes in big endian order into an unsigned long
* @param buf the byte array of 4 bytes

View File

@ -111,28 +111,38 @@ int sc_bin_to_hex(const u8 *in, size_t in_len, char *out, size_t out_len,
return 0;
}
void ulong2bebytes(u8 *buf, unsigned long x)
u8 *ulong2bebytes(u8 *buf, unsigned long x)
{
buf[3] = (u8) (x & 0xff);
buf[2] = (u8) ((x >> 8) & 0xff);
buf[1] = (u8) ((x >> 16) & 0xff);
buf[0] = (u8) ((x >> 24) & 0xff);
if (buf != NULL) {
buf[3] = (u8) (x & 0xff);
buf[2] = (u8) ((x >> 8) & 0xff);
buf[1] = (u8) ((x >> 16) & 0xff);
buf[0] = (u8) ((x >> 24) & 0xff);
}
return buf;
}
void ushort2bebytes(u8 *buf, unsigned short x)
u8 *ushort2bebytes(u8 *buf, unsigned short x)
{
buf[1] = (u8) (x & 0xff);
buf[0] = (u8) ((x >> 8) & 0xff);
if (buf != NULL) {
buf[1] = (u8) (x & 0xff);
buf[0] = (u8) ((x >> 8) & 0xff);
}
return buf;
}
unsigned long bebytes2ulong(const u8 *buf)
{
return (unsigned long) (buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3]);
if (buf == NULL)
return 0UL;
return (unsigned long) (buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3]);
}
unsigned short bebytes2ushort(const u8 *buf)
{
return (unsigned short) (buf[0] << 8 | buf[1]);
if (buf == NULL)
return 0U;
return (unsigned short) (buf[0] << 8 | buf[1]);
}
int sc_format_oid(struct sc_object_id *oid, const char *in)