summaryrefslogtreecommitdiff
path: root/src/tools/util.c
blob: 3c1f5aa9dcd787de105df6a4f66cf5feab0c3909 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdint.h>

#include "util.h"

int32_t ReadInt32M(FILE *fp)
{
	unsigned char d[4];
	
	fread(d, 1, 4, fp);
	
	return (d[0] << 24) | (d[1] << 16) | (d[2] << 8) | (d[3] << 0);
}

int32_t ReadInt24M(FILE *fp)
{
	unsigned char d[3];
	
	fread(d, 1, 3, fp);
	
	return (d[0] << 16) | (d[1] << 8) | (d[2] << 0);
}

int16_t ReadInt16M(FILE *fp)
{
	unsigned char d[2];
	
	fread(d, 1, 2, fp);
	
	return (d[0] << 8) | (d[1] << 0);
}

int32_t ReadInt32L(FILE *fp)
{
	unsigned char d[4];
	
	fread(d, 1, 4, fp);
	
	return (d[3] << 24) | (d[2] << 16) | (d[1] << 8) | (d[0] << 0);
}

int32_t ReadInt24L(FILE *fp)
{
	unsigned char d[3];
	
	fread(d, 1, 3, fp);
	
	return (d[2] << 16) | (d[1] << 8) | (d[0] << 0);
}

int16_t ReadInt16L(FILE *fp)
{
	unsigned char d[2];
	
	fread(d, 1, 2, fp);
	
	return (d[1] << 8) | (d[0] << 0);
}

int8_t ReadInt8(FILE *fp)
{
	unsigned char d[1];
	
	fread(d, 1, 1, fp);
	
	return d[0];
}

int filelength(int filedes)
{
	struct stat buf;
	
	if (fstat(filedes, &buf) == -1) 
		return -1;
	
	return buf.st_size;
}

int fsize(char *file_name)
{
	struct stat buf;
	
	if (stat(file_name, &buf) == -1)
		return -1;
	
	return buf.st_size;
}