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.
31 /* Controls the verbosity level for msg() */
32 static int verbosity
= 0;
34 /* Adjusts the verbosity level for msg() */
36 adjust_verbosity(int adj
)
42 * Prints messages to console according to the current verbosity
43 * - see utils.h for level defines
46 msg(int level
, const char *fmt
, ...)
51 if (level
> verbosity
)
56 if (level
< MSG_QUIET
)
57 ret
= vfprintf(stderr
, fmt
, ap
);
59 ret
= vfprintf(stdout
, fmt
, ap
);
65 /* Malloc which either succeeds or exits */
69 void *result
= malloc(size
);
71 msg(MSG_CRITICAL
, "Failed to malloc %zi bytes\n", size
);
77 /* Ditto for strdup */
79 xstrdup(const char *s
)
81 char *result
= strdup(s
);
83 msg(MSG_CRITICAL
, "Failed to strdup %zi bytes\n", strlen(s
));
89 /* Human-readable printout of binary data */
91 binary_print(const char *s
, ssize_t len
)
95 for (i
= 0; i
< len
; i
++) {
97 msg(MSG_DEBUG
, "%c", s
[i
]);
99 msg(MSG_DEBUG
, "0x%02X", (int)s
[i
]);
103 /* Writes data to a file or exits on failure */
105 xfwrite(const void *ptr
, size_t size
, FILE *stream
)
107 if (fwrite(ptr
, size
, 1, stream
) != 1) {
113 /* Writes an int to a file, using len bytes, in bigendian order */
115 write_int(uint64_t value
, size_t len
, FILE *to
)
120 for (i
= 0; i
< len
; i
++)
121 buf
[i
] = ((value
>> (8 * i
)) & 0xff);
122 xfwrite(buf
, len
, to
);
125 /* Writes a binary string to a file */
127 write_binary_string(const char *string
, size_t len
, FILE *to
)
129 xfwrite(string
, len
, to
);
132 /* Writes a normal C string to a file */
134 write_string(const char *string
, FILE *to
)
136 xfwrite(string
, strlen(string
) + 1, to
);
139 /* Reads an int from a file, using len bytes, in bigendian order */
141 read_int(char **from
, size_t len
, const char *max
)
146 if (*from
+ len
> max
) {
148 "Attempt to read beyond end of file, corrupt file?\n");
152 for (i
= 0; i
< len
; i
++)
153 result
+= (((*from
)[i
] & 0xff) << (8 * i
));
158 /* Reads a binary string from a file */
160 read_binary_string(char **from
, size_t len
, const char *max
)
164 if (*from
+ len
> max
) {
166 "Attempt to read beyond end of file, corrupt file?\n");
170 result
= xmalloc(len
);
171 strncpy(result
, *from
, len
);
176 /* Reads a normal C string from a file */
178 read_string(char **from
, const char *max
)
180 return read_binary_string(from
, strlen(*from
) + 1, max
);