~ubuntu-branches/ubuntu/raring/vice/raring

« back to all changes in this revision

Viewing changes to src/util.c

  • Committer: Bazaar Package Importer
  • Author(s): Laszlo Boszormenyi (GCS)
  • Date: 2009-03-31 00:37:15 UTC
  • mfrom: (1.1.7 upstream) (9.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20090331003715-i5yisvcfv7mgz3eh
Tags: 2.1.dfsg-1
* New major upstream release (closes: #495937).
* Add desktop files (closes: #501181).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
 
1
/*! \file util.c \n
 
2
 *  \author Ettore Perazzoli, Andreas Boose\n
 
3
 *  \brief  Miscellaneous utility functions.
 
4
 *
2
5
 * util.c - Miscellaneous utility functions.
3
6
 *
4
7
 * Written by
27
30
 
28
31
#include "vice.h"
29
32
 
 
33
#include <assert.h>
30
34
#include <ctype.h>
31
35
#include <stdarg.h>
32
36
#include <stdio.h>
33
37
#include <stdlib.h>
34
38
#include <string.h>
35
39
 
 
40
#ifdef HAVE_STRINGS_H
 
41
#include <strings.h>
 
42
#endif
 
43
 
36
44
#include "archdep.h"
37
45
#include "ioutil.h"
38
46
#include "lib.h"
490
498
 
491
499
/* ------------------------------------------------------------------------- */
492
500
 
 
501
/*! \brief read an array of DWORDs (4 bytes) in low endian from a file
 
502
 
 
503
 \param fd
 
504
   file descriptor as obtained by fopen().
 
505
 
 
506
 \param buf
 
507
   Pointer to a buffer where the DWORDs will be stored.
 
508
 
 
509
 \param num
 
510
   number of DWORD to read. buf is considered as an array defined
 
511
   as DWORD buf[num].
 
512
 
 
513
 \return
 
514
   0 on success, else -1.
 
515
 
 
516
 \remark
 
517
   num is the number of DWORDs to read; it is *not* the
 
518
   size of the buffer in bytes!
 
519
*/
493
520
int util_dword_read(FILE *fd, DWORD *buf, size_t num)
494
521
{
495
 
    int i;
 
522
    unsigned int i;
496
523
    BYTE *tmpbuf;
497
524
 
498
 
    tmpbuf = lib_malloc(num);
499
 
 
500
 
    if (fread((char *)tmpbuf, num, 1, fd) < 1) {
 
525
    assert( sizeof(DWORD) == 4);
 
526
 
 
527
    tmpbuf = lib_malloc(4 * num);
 
528
 
 
529
    if (fread(tmpbuf, num, 4, fd) < 4) {
501
530
        lib_free(tmpbuf);
502
531
        return -1;
503
532
    }
504
533
 
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));
 
537
    }
508
538
 
509
539
    lib_free(tmpbuf);
510
540
    return 0;
511
541
}
512
542
 
 
543
/*! \brief write an array of DWORDs (4 bytes) in low endian to a file
 
544
 
 
545
 \param fd
 
546
   file descriptor as obtained by fopen().
 
547
 
 
548
 \param buf
 
549
   Pointer to the array of DWORDs to be written to the file
 
550
 
 
551
 \param num
 
552
   number of DWORD to read. buf is considered as an array defined
 
553
   as DWORD buf[num].
 
554
 
 
555
 \return
 
556
   0 on success, else -1.
 
557
 
 
558
 \remark
 
559
   num is the number of DWORDs to write; it is *not* the
 
560
   size of the buffer in bytes!
 
561
*/
513
562
int util_dword_write(FILE *fd, DWORD *buf, size_t num)
514
563
{
515
 
    int i;
 
564
    unsigned int i;
516
565
    BYTE *tmpbuf;
517
566
 
518
 
    tmpbuf = lib_malloc(num);
519
 
 
520
 
    for (i = 0; i < ((int)(num) / 4); i++) {
 
567
    assert( sizeof(DWORD) == 4 );
 
568
 
 
569
    tmpbuf = lib_malloc(4 * num);
 
570
 
 
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);
525
576
    }
526
577
 
527
 
    if (fwrite((char *)tmpbuf, num, 1, fd) < 1) {
 
578
    if (fwrite(tmpbuf, num, 4, fd) < 1) {
528
579
        lib_free(tmpbuf);
529
580
        return -1;
530
581
    }