-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathatheros_lib.c
122 lines (103 loc) · 2.49 KB
/
atheros_lib.c
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* This file is subject to the terms and conditions of the GNU General Public
* License. See the file "COPYING" in the main directory of this archive
* for more details.
*
* Copyright (C) 2013 Álvaro Fernández Rojas <[email protected]>
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "atheros_lib.h"
#define pr_fmt(fmt) "athlib: " fmt
/*
* Read bytes from file.
* Fails if the size supplied isn't fully read.
*/
int athlib_bytes_read(int fdin, uint8_t* bytes, int bytes_off, int bytes_len) {
int file_len;
struct stat st;
// Check file descriptor
if(!fdin) {
pr_err("file descriptor error.\n");
return 0;
}
// Check if file exists
if(fstat(fdin, &st) == -1) {
pr_err("file doesn't exist.\n");
return 0;
}
// Check if file isn't empty
file_len = (int) st.st_size;
if(file_len == 0) {
pr_err("file is empty.\n");
return 0;
}
// Check offset
if(bytes_off >= file_len) {
pr_err("wrong offset.\n");
return 0;
}
// Check size
if(bytes_len >= file_len || bytes_off + bytes_len > file_len) {
pr_err("wrong size.\n");
return 0;
}
// Seek file
if(bytes_off > 0 && lseek(fdin, bytes_off, SEEK_SET) < 0) {
pr_err("error while seeking.\n");
return 0;
}
// Read the file
ssize_t bytes_read = read(fdin, bytes, bytes_len);
if(bytes_read != bytes_len) {
pr_err("file wasn't read properly.\n");
return 0;
}
return bytes_read;
}
/*
* Writes bytes to file.
* Fails if the size supplied isn't fully written.
*/
int athlib_bytes_write(int fdout, uint8_t* bytes, int bytes_off, int bytes_len, bool new) {
// Check file descriptor
if(!fdout) {
pr_err("file descriptor error.\n");
return 0;
}
// Check if file has to be created.
if(!new && bytes_off > 0) {
struct stat st;
// Check if file exists
if(fstat(fdout, &st) == -1) {
pr_err("file doesn't exist.\n");
return 0;
}
// Seek file
if(lseek(fdout, bytes_off, SEEK_SET) < 0) {
pr_err("error while seeking.\n");
return 0;
}
}
// Write the file
ssize_t bytes_written = write(fdout, bytes, bytes_len);
if(bytes_written != bytes_len) {
pr_err("file wasn't written properly.\n");
return 0;
}
return bytes_written;
}
/*
* Prints an array of bytes.
*/
int athlib_bytes_print(uint8_t* bytes, int size) {
int i;
// Print char by char
for(i = 0; i < size; i++) {
printf("%c", bytes[i]);
}
printf("\n");
return i;
}