Add util functions to deal with ISO types.

This commit is contained in:
Vreixo Formoso 2007-12-27 00:19:09 +01:00
parent bf51fba3bc
commit aae230a321
3 changed files with 107 additions and 30 deletions

View File

@ -514,6 +514,28 @@ void iso_bb(uint8_t *buf, uint32_t num, int bytes)
iso_msb(buf+bytes, num, bytes);
}
uint32_t iso_read_lsb(const uint8_t *buf, int bytes)
{
int i;
uint32_t ret = 0;
for (i=0; i<bytes; i++) {
ret += ((uint32_t) buf[i]) << (i*8);
}
return ret;
}
uint32_t iso_read_msb(const uint8_t *buf, int bytes)
{
int i;
uint32_t ret = 0;
for (i=0; i<bytes; i++) {
ret += ((uint32_t) buf[bytes-i-1]) << (i*8);
}
return ret;
}
void iso_datetime_7(unsigned char *buf, time_t t)
{
static int tzsetup = 0;

View File

@ -93,6 +93,9 @@ void iso_lsb(uint8_t *buf, uint32_t num, int bytes);
void iso_msb(uint8_t *buf, uint32_t num, int bytes);
void iso_bb(uint8_t *buf, uint32_t num, int bytes);
uint32_t iso_read_lsb(const uint8_t *buf, int bytes);
uint32_t iso_read_msb(const uint8_t *buf, int bytes);
/** Records the date/time into a 7 byte buffer (ECMA-119, 9.1.5) */
void iso_datetime_7(uint8_t *buf, time_t t);

View File

@ -29,6 +29,56 @@ static void test_round_up()
CU_ASSERT_EQUAL( round_up(14, 7), 14 );
}
static void test_iso_lsb_msb()
{
uint8_t buf[4];
uint32_t num;
num = 0x01020304;
iso_lsb(buf, num, 4);
CU_ASSERT_EQUAL( buf[0], 0x04 );
CU_ASSERT_EQUAL( buf[1], 0x03 );
CU_ASSERT_EQUAL( buf[2], 0x02 );
CU_ASSERT_EQUAL( buf[3], 0x01 );
iso_msb(buf, num, 4);
CU_ASSERT_EQUAL( buf[0], 0x01 );
CU_ASSERT_EQUAL( buf[1], 0x02 );
CU_ASSERT_EQUAL( buf[2], 0x03 );
CU_ASSERT_EQUAL( buf[3], 0x04 );
iso_lsb(buf, num, 2);
CU_ASSERT_EQUAL( buf[0], 0x04 );
CU_ASSERT_EQUAL( buf[1], 0x03 );
iso_msb(buf, num, 2);
CU_ASSERT_EQUAL( buf[0], 0x03 );
CU_ASSERT_EQUAL( buf[1], 0x04 );
}
static void test_iso_read_lsb_msb()
{
uint8_t buf[4];
uint32_t num;
buf[0] = 0x04;
buf[1] = 0x03;
buf[2] = 0x02;
buf[3] = 0x01;
num = iso_read_lsb(buf, 4);
CU_ASSERT_EQUAL(num, 0x01020304);
num = iso_read_msb(buf, 4);
CU_ASSERT_EQUAL(num, 0x04030201);
num = iso_read_lsb(buf, 2);
CU_ASSERT_EQUAL(num, 0x0304);
num = iso_read_msb(buf, 2);
CU_ASSERT_EQUAL(num, 0x0403);
}
static void test_iso_bb()
{
uint8_t buf[8];
@ -186,6 +236,8 @@ void add_util_suite()
CU_add_test(pSuite, "div_up()", test_div_up);
CU_add_test(pSuite, "round_up()", test_round_up);
CU_add_test(pSuite, "iso_bb()", test_iso_bb);
CU_add_test(pSuite, "iso_lsb/msb()", test_iso_lsb_msb);
CU_add_test(pSuite, "iso_read_lsb/msb()", test_iso_read_lsb_msb);
CU_add_test(pSuite, "iso_1_dirid()", test_iso_1_dirid);
CU_add_test(pSuite, "iso_2_dirid()", test_iso_2_dirid);
CU_add_test(pSuite, "iso_1_fileid()", test_iso_1_fileid);