-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmp4tagfileop.c
261 lines (226 loc) · 4.84 KB
/
mp4tagfileop.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
* Copyright 2023-2024 Brad Lanam Pleasant Hill CA
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#if _hdr_windows
# define WIN32_LEAN_AND_MEAN 1
# include <windows.h>
#endif
#include "libmp4tag.h"
#include "mp4tagint.h"
FILE *
mp4tag_fopen (const char *fname, const char *mode)
{
FILE *fh = NULL;
#ifdef _WIN32
{
wchar_t *tfname = NULL;
wchar_t *tmode = NULL;
tfname = mp4tag_towide (fname);
tmode = mp4tag_towide (mode);
if (tfname != NULL && tmode != NULL) {
fh = _wfopen (tfname, tmode);
free (tfname);
free (tmode);
}
}
#else
{
fh = fopen (fname, mode);
}
#endif
return fh;
}
ssize_t
mp4tag_file_size (const char *fname)
{
ssize_t sz = -1;
#if _lib__wstat64
{
struct __stat64 statbuf;
wchar_t *tfname = NULL;
int rc;
tfname = mp4tag_towide (fname);
if (tfname != NULL) {
rc = _wstat64 (tfname, &statbuf);
if (rc == 0) {
sz = statbuf.st_size;
}
free (tfname);
}
}
#else
{
int rc;
struct stat statbuf;
rc = stat (fname, &statbuf);
if (rc == 0) {
sz = statbuf.st_size;
}
}
#endif
return sz;
}
char *
mp4tag_read_file (const char *fn, size_t *sz, int *mp4error)
{
char *fdata = NULL;
int rc = -1;
FILE *fh;
ssize_t fsz;
*mp4error = MP4TAG_OK;
*sz = 0;
fsz = mp4tag_file_size (fn);
if (fsz < 0) {
*mp4error = MP4TAG_ERR_FILE_NOT_FOUND;
return NULL;
}
fdata = malloc (fsz);
if (fdata == NULL) {
*mp4error = MP4TAG_ERR_OUT_OF_MEMORY;
return NULL;
}
fh = mp4tag_fopen (fn, "rb");
if (fh == NULL) {
*mp4error = MP4TAG_ERR_FILE_NOT_FOUND;
} else {
rc = fread (fdata, fsz, 1, fh);
if (rc != 1) {
*mp4error = MP4TAG_ERR_FILE_READ_ERROR;
}
}
fclose (fh);
if (rc != 1 && fdata != NULL) {
free (fdata);
fdata = NULL;
}
*sz = fsz;
return fdata;
}
int
mp4tag_file_delete (const char *fname)
{
int rc = -1;
#if _lib__wunlink
wchar_t *tname;
#endif
if (fname == NULL) {
return 0;
}
#if _lib__wunlink
tname = mp4tag_towide (fname);
if (tname != NULL) {
rc = _wunlink (tname);
free (tname);
}
#else
rc = unlink (fname);
#endif
return rc;
}
int
mp4tag_file_move (const char *fname, const char *nfn)
{
int rc = -1;
#if _lib__wrename
/*
* Windows won't rename to an existing file, but does
* not return an error.
*/
mp4tag_file_delete (nfn);
{
wchar_t *wfname;
wchar_t *wnfn;
wfname = mp4tag_towide (fname);
wnfn = mp4tag_towide (nfn);
if (wfname != NULL && wnfn != NULL) {
rc = _wrename (wfname, wnfn);
free (wfname);
free (wnfn);
}
}
#else
rc = rename (fname, nfn);
#endif
return rc;
}
void
mp4tag_copy_file_times (FILE *ifh, FILE *ofh)
{
if (ifh == NULL || ofh == NULL) {
return;
}
#if _lib_GetFileTime
{
HANDLE ih;
HANDLE oh;
FILETIME ctime;
FILETIME atime;
FILETIME wtime;
ih = (HANDLE) _get_osfhandle (_fileno (ifh));
oh = (HANDLE) _get_osfhandle (_fileno (ofh));
/* the windows api documentation states that a non-zero return value */
/* indicates success */
if (GetFileTime (ih, &ctime, &atime, &wtime) != 0) {
SetFileTime (oh, &ctime, &atime, &wtime);
}
}
#else
{
int rc;
struct stat statbuf;
struct timespec ts [2];
rc = fstat (fileno (ifh), &statbuf);
if (rc == 0) {
memset (ts, 0, sizeof (ts));
#if _mem_struct_stat_st_atim
memcpy (&ts [0], &statbuf.st_atim, sizeof (struct timespec));
memcpy (&ts [1], &statbuf.st_mtim, sizeof (struct timespec));
#endif
#if _mem_struct_stat_st_atimespec
memcpy (&ts [0], &statbuf.st_atimespec, sizeof (struct timespec));
memcpy (&ts [1], &statbuf.st_mtimespec, sizeof (struct timespec));
#endif
futimens (fileno (ofh), ts);
}
}
#endif
}
#ifdef _WIN32
wchar_t *
mp4tag_towide (const char *buff)
{
wchar_t *tbuff = NULL;
size_t len;
/* the documentation lies; len does not include room for the null byte */
len = MultiByteToWideChar (CP_UTF8, 0, buff, strlen (buff), NULL, 0);
tbuff = malloc ((len + 1) * sizeof (wchar_t));
if (tbuff != NULL) {
MultiByteToWideChar (CP_UTF8, 0, buff, strlen (buff), tbuff, len);
tbuff [len] = L'\0';
}
return tbuff;
}
char *
mp4tag_fromwide (const wchar_t *buff)
{
char *tbuff = NULL;
size_t len;
/* the documentation lies; len does not include room for the null byte */
len = WideCharToMultiByte (CP_UTF8, 0, buff, -1, NULL, 0, NULL, NULL);
tbuff = malloc (len + 1);
if (tbuff != NULL) {
WideCharToMultiByte (CP_UTF8, 0, buff, -1, tbuff, len, NULL, NULL);
tbuff [len] = '\0';
}
return tbuff;
}
#endif