Stef Hoeben wrote:
Hi,

+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);
+}


I would prefer

#define ULONG2BEBYTES(p, x)    do {\
    (p)[3] = (x) & 0xff;\
    (p)[2] = ((x) >> 8)  & 0xff;\
    (p)[1] = ((x) >> 16) & 0xff;\
    (p)[0] = ((x) >> 24) & 0xff;\
} while(0)

instead of defining function for this.

Macros might be somewhat more dangerous in this case,
e.g. someone calling ULONG2BEBYTES(buf++, 12345) ,
but it's fine for me to change it into a macro.

forget it, a function is fine here. Btw: I would still prefer
a version using shifts and masks (as done in the macros) as I
personally find it easier to understand what the function is
actually doing (but that's just my personal taste).

Cheers,
Nils

PS: doxygen comments would be nice (even for internal functions) ;-)

_______________________________________________
opensc-devel mailing list
opensc-devel@lists.opensc-project.org
http://www.opensc-project.org/mailman/listinfo/opensc-devel

Reply via email to