2 * Main functions of the program.
4 * Copyright (C) 2007 David Härdeman <david@hardeman.nu>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
32 /* Controls the verbosity level for msg() */
33 static int verbosity
= 0;
35 /* Adjusts the verbosity level for msg() */
37 adjust_verbosity(int adj
)
43 * Prints messages to console according to the current verbosity
44 * - see utils.h for level defines
47 msg(int level
, const char *fmt
, ...)
52 if (level
> verbosity
)
57 if (level
< MSG_QUIET
)
58 ret
= vfprintf(stderr
, fmt
, ap
);
60 ret
= vfprintf(stdout
, fmt
, ap
);
66 /* Malloc which either succeeds or exits */
70 void *result
= malloc(size
);
72 msg(MSG_CRITICAL
, "Failed to malloc %zi bytes\n", size
);
78 /* Ditto for strdup */
80 xstrdup(const char *s
)
82 char *result
= strdup(s
);
84 msg(MSG_CRITICAL
, "Failed to strdup %zi bytes\n", strlen(s
));
90 /* Human-readable printout of binary data */
92 binary_print(const char *s
, ssize_t len
)
96 for (i
= 0; i
< len
; i
++) {
98 msg(MSG_DEBUG
, "%c", s
[i
]);
100 msg(MSG_DEBUG
, "0x%02X", (int)s
[i
]);
104 /* Writes data to a file or exits on failure */
106 xfwrite(const void *ptr
, size_t size
, FILE *stream
)
108 if (fwrite(ptr
, size
, 1, stream
) != 1) {
109 msg(MSG_CRITICAL
, "Failed to write to file: %s\n",
115 /* Writes an int to a file, using len bytes, in bigendian order */
117 write_int(uint64_t value
, size_t len
, FILE *to
)
122 for (i
= 0; i
< len
; i
++)
123 buf
[i
] = ((value
>> (8 * i
)) & 0xff);
124 xfwrite(buf
, len
, to
);
127 /* Writes a binary string to a file */
129 write_binary_string(const char *string
, size_t len
, FILE *to
)
131 xfwrite(string
, len
, to
);
134 /* Writes a normal C string to a file */
136 write_string(const char *string
, FILE *to
)
138 xfwrite(string
, strlen(string
) + 1, to
);
141 /* Reads an int from a file, using len bytes, in bigendian order */
143 read_int(char **from
, size_t len
, const char *max
)
148 if (*from
+ len
> max
) {
150 "Attempt to read beyond end of file, corrupt file?\n");
154 for (i
= 0; i
< len
; i
++)
155 result
+= (((*from
)[i
] & 0xff) << (8 * i
));
160 /* Reads a binary string from a file */
162 read_binary_string(char **from
, size_t len
, const char *max
)
166 if (*from
+ len
> max
) {
168 "Attempt to read beyond end of file, corrupt file?\n");
172 result
= xmalloc(len
);
173 strncpy(result
, *from
, len
);
178 /* Reads a normal C string from a file */
180 read_string(char **from
, const char *max
)
182 return read_binary_string(from
, strlen(*from
) + 1, max
);