8 #if defined(_MSC_VER) || defined(__MINGW32__)
10 #include <sys/utime.h>
18 // global verbosity setting
21 int read_s16_be(unsigned char *buf
)
23 unsigned tmp
= read_u16_be(buf
);
26 ret
= -((int)0x10000 - (int)tmp
);
33 float read_f32_be(unsigned char *buf
)
35 union {uint32_t i
; float f
;} ret
;
36 ret
.i
= read_u32_be(buf
);
40 int is_power2(unsigned int val
)
42 while (((val
& 1) == 0) && (val
> 1)) {
48 void fprint_hex(FILE *fp
, const unsigned char *buf
, int length
)
51 for (i
= 0; i
< length
; i
++) {
52 fprint_byte(fp
, buf
[i
]);
57 void fprint_hex_source(FILE *fp
, const unsigned char *buf
, int length
)
60 for (i
= 0; i
< length
; i
++) {
61 if (i
> 0) fputs(", ", fp
);
63 fprint_byte(fp
, buf
[i
]);
67 void print_hex(const unsigned char *buf
, int length
)
69 fprint_hex(stdout
, buf
, length
);
72 void swap_bytes(unsigned char *data
, long length
)
76 for (i
= 0; i
< length
; i
+= 2) {
83 void reverse_endian(unsigned char *data
, long length
)
87 for (i
= 0; i
< length
; i
+= 4) {
92 data
[i
+1] = data
[i
+2];
97 long filesize(const char *filename
)
101 if (stat(filename
, &st
) == 0) {
108 void touch_file(const char *filename
)
111 //fd = open(filename, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, 0666);
112 fd
= open(filename
, O_WRONLY
|O_CREAT
, 0666);
114 utime(filename
, NULL
);
119 long read_file(const char *file_name
, unsigned char **data
)
122 unsigned char *in_buf
= NULL
;
125 in
= fopen(file_name
, "rb");
130 // allocate buffer to read from offset to end of file
131 fseek(in
, 0, SEEK_END
);
132 file_size
= ftell(in
);
135 if (file_size
> 256*MB
) {
139 in_buf
= malloc(file_size
);
140 fseek(in
, 0, SEEK_SET
);
143 bytes_read
= fread(in_buf
, 1, file_size
, in
);
144 if (bytes_read
!= file_size
) {
153 long write_file(const char *file_name
, unsigned char *data
, long length
)
158 out
= fopen(file_name
, "wb");
163 bytes_written
= fwrite(data
, 1, length
, out
);
165 return bytes_written
;
168 void generate_filename(const char *in_name
, char *out_name
, char *extension
)
170 char tmp_name
[FILENAME_MAX
];
173 strcpy(tmp_name
, in_name
);
174 len
= strlen(tmp_name
);
175 for (i
= len
- 1; i
> 0; i
--) {
176 if (tmp_name
[i
] == '.') {
184 sprintf(out_name
, "%s.%s", tmp_name
, extension
);
187 char *basename(const char *name
)
189 const char *base
= name
;
191 if (*name
++ == '/') {
198 void make_dir(const char *dir_name
)
200 struct stat st
= {0};
201 if (stat(dir_name
, &st
) == -1) {
202 mkdir(dir_name
, 0755);
206 long copy_file(const char *src_name
, const char *dst_name
)
212 bytes_read
= read_file(src_name
, &buf
);
214 if (bytes_read
> 0) {
215 bytes_written
= write_file(dst_name
, buf
, bytes_read
);
216 if (bytes_written
!= bytes_read
) {
225 void dir_list_ext(const char *dir
, const char *extension
, dir_list
*list
)
229 struct dirent
*entry
;
235 ERROR("Can't open '%s'\n", dir
);
239 pool
= malloc(FILENAME_MAX
* MAX_DIR_FILES
);
243 while ((entry
= readdir(dfd
)) != NULL
&& idx
< MAX_DIR_FILES
) {
244 if (!extension
|| str_ends_with(entry
->d_name
, extension
)) {
245 sprintf(pool_ptr
, "%s/%s", dir
, entry
->d_name
);
246 list
->files
[idx
] = pool_ptr
;
247 pool_ptr
+= strlen(pool_ptr
) + 1;
256 void dir_list_free(dir_list
*list
)
258 // assume first entry in array is allocated
259 if (list
->files
[0]) {
260 free(list
->files
[0]);
261 list
->files
[0] = NULL
;
265 int str_ends_with(const char *str
, const char *suffix
)
267 if (!str
|| !suffix
) {
270 size_t len_str
= strlen(str
);
271 size_t len_suffix
= strlen(suffix
);
272 if (len_suffix
> len_str
) {
275 return (0 == strncmp(str
+ len_str
- len_suffix
, suffix
, len_suffix
));