491
499
/* ------------------------------------------------------------------------- */
501
/*! \brief read an array of DWORDs (4 bytes) in low endian from a file
504
file descriptor as obtained by fopen().
507
Pointer to a buffer where the DWORDs will be stored.
510
number of DWORD to read. buf is considered as an array defined
514
0 on success, else -1.
517
num is the number of DWORDs to read; it is *not* the
518
size of the buffer in bytes!
493
520
int util_dword_read(FILE *fd, DWORD *buf, size_t num)
498
tmpbuf = lib_malloc(num);
500
if (fread((char *)tmpbuf, num, 1, fd) < 1) {
525
assert( sizeof(DWORD) == 4);
527
tmpbuf = lib_malloc(4 * num);
529
if (fread(tmpbuf, num, 4, fd) < 4) {
501
530
lib_free(tmpbuf);
505
for (i = 0; i < ((int)(num) / 4); i++)
534
for (i = 0; i < num; i++) {
506
535
buf[i] = (tmpbuf[i * 4] + (tmpbuf[i * 4 + 1] << 8)
507
536
+ (tmpbuf[i * 4 + 2] << 16) + (tmpbuf[i * 4 + 3] << 24));
509
539
lib_free(tmpbuf);
543
/*! \brief write an array of DWORDs (4 bytes) in low endian to a file
546
file descriptor as obtained by fopen().
549
Pointer to the array of DWORDs to be written to the file
552
number of DWORD to read. buf is considered as an array defined
556
0 on success, else -1.
559
num is the number of DWORDs to write; it is *not* the
560
size of the buffer in bytes!
513
562
int util_dword_write(FILE *fd, DWORD *buf, size_t num)
518
tmpbuf = lib_malloc(num);
520
for (i = 0; i < ((int)(num) / 4); i++) {
567
assert( sizeof(DWORD) == 4 );
569
tmpbuf = lib_malloc(4 * num);
571
for (i = 0; i < num; i++) {
521
572
tmpbuf[i * 4] = (BYTE)(buf[i] & 0xff);
522
573
tmpbuf[i * 4 + 1] = (BYTE)((buf[i] >> 8) & 0xff);
523
574
tmpbuf[i * 4 + 2] = (BYTE)((buf[i] >> 16) & 0xff);
524
575
tmpbuf[i * 4 + 3] = (BYTE)((buf[i] >> 24) & 0xff);
527
if (fwrite((char *)tmpbuf, num, 1, fd) < 1) {
578
if (fwrite(tmpbuf, num, 4, fd) < 1) {
528
579
lib_free(tmpbuf);