File: himd.c

package info (click to toggle)
linux-minidisc 0.9.16-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,640 kB
  • sloc: ansic: 6,389; cpp: 2,731; python: 2,537; perl: 866; sh: 207; makefile: 10
file content (342 lines) | stat: -rw-r--r-- 9,885 bytes parent folder | download | duplicates (2)
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
 * himd.c
 *
 * This file is part of libhimd, a library for accessing Sony HiMD devices.
 *
 * Copyright (C) 2009-2011 Michael Karcher
 * Copyright (C) 2011 MÃ¥rten Cassel
 * Copyright (C) 2011 Thomas Arp
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>

#define G_LOG_DOMAIN "HiMD"
#include <glib.h>
#include <glib/gstdio.h>
#include "himd.h"
#include "himd_private.h"

#define _(x) (x)

void set_status_const(struct himderrinfo * status, enum himdstatus code, const char * msg)
{
    if(status)
    {
        status->status = code;
        g_strlcpy(status->statusmsg, msg, sizeof status->statusmsg);
    }
}

void set_status_printf(struct himderrinfo * status, enum himdstatus code, const char * format, ...)
{
    if(status)
    {
        va_list args;
        va_start(args, format);
        status->status = code;
        g_vsnprintf(status->statusmsg, sizeof status->statusmsg, format, args);
        va_end(args);
    }
}

static int scanforatdata(GDir * dir)
{
    const char * hmafile;
    /* I don't use g_pattern_* stuff, because they can't be case insensitive */
    int maxdatanum = -1;
    int curdatanum;
    while((hmafile = g_dir_read_name(dir)) != NULL)
    {
        /* atdataNN.hma - should be only one of them */
        if(g_ascii_strncasecmp(hmafile,"atdata0",7) == 0 &&
           strlen(hmafile) == 12 &&
           isxdigit(hmafile[7]) &&
           g_ascii_strncasecmp(hmafile+8,".hma",4) == 0 &&
           sscanf(hmafile+6,"%x",&curdatanum) == 1 &&
           curdatanum > maxdatanum)
        {
            if(maxdatanum != -1)
                g_warning("Found two atdata files: %02X and %02X\n",curdatanum,maxdatanum);
            maxdatanum = curdatanum;
        }
    }
    return maxdatanum;
}


// scan for TRKIDX files
static int scanfortif(GDir * dir, int* oldnum, int *newnum)
{
    const char * hmafile;
    gboolean found_something = FALSE;

    GRegex *trkidx_hma = g_regex_new("^[t_]rkidx([0-9a-f]{2})\\.hma$", G_REGEX_CASELESS, 0, NULL);

    while((hmafile = g_dir_read_name(dir)) != NULL)
    {
        GMatchInfo *info = NULL;
        if (g_regex_match(trkidx_hma, hmafile, 0, &info)) {
            int value = (int)g_ascii_strtoll(g_match_info_fetch(info, 1), NULL, 16);

            if (hmafile[0] == '_') {
                // old version
                if (value > *oldnum) {
                    *oldnum = value;
                    found_something = TRUE;
                }
            } else {
                // new version
                if (value > *newnum) {
                    *newnum = value;
                    found_something = TRUE;
                }
            }
        }
        g_match_info_free(info);
    }

    g_regex_unref(trkidx_hma);

    return found_something;
}

static void nong_inplace_ascii_down(gchar * string)
{
    while(*string)
    {
        *string = g_ascii_tolower(*string);
        string++;
    }
}

static void nong_inplace_ascii_up(gchar * string)
{
    while(*string)
    {
        *string = g_ascii_toupper(*string);
        string++;
    }
}

FILE * himd_open_file(struct himd * himd, const char * fileid, enum himd_rw_mode mode)
{
    char filename[13];
    FILE * file;
    char * filepath;

    sprintf(filename,"%s%02X.HMA",fileid,himd->datanum);
    if(himd->need_lowercase)
        nong_inplace_ascii_down(filename);
    else
        nong_inplace_ascii_up(filename);
    filepath = g_build_filename(himd->rootpath,himd->need_lowercase ? "hmdhifi" : "HMDHIFI",filename,NULL);
    file = fopen(filepath,mode == HIMD_READ_WRITE ? "rb+" : "rb");
    g_free(filepath);
    return file;
}


int himd_write_tifdata(struct himd * himd, struct himderrinfo * status)
{
    gchar *unusedfile=NULL,*usedfile=NULL,*tempfile=NULL;
    gchar *filepath;
    GDir * dir;
    GError * error = NULL;
    (void)status;

    filepath = g_build_filename(himd->rootpath,himd->need_lowercase ? "hmdhifi" : "HMDHIFI", NULL);
    dir      = g_dir_open(filepath,0,&error);
    int oldnum=0, newnum=0;

    if(scanfortif(dir, &oldnum, &newnum))
	{
	    char *indexfilename = g_strdup_printf(himd->need_lowercase ? "_rkidx%02x.hma" : "_RKIDX%02X.HMA", oldnum);
	    unusedfile = g_build_filename(himd->rootpath,himd->need_lowercase ? "hmdhifi" : "HMDHIFI",
					  indexfilename,NULL);
	    g_free(indexfilename);

	    indexfilename = g_strdup_printf(himd->need_lowercase ? "trkidx%02x.hma" : "TRKIDX%02X.HMA", newnum);
	    usedfile = g_build_filename(himd->rootpath,himd->need_lowercase ? "hmdhifi" : "HMDHIFI",
					indexfilename,NULL);
	    g_free(indexfilename);
	}
    else
	{
	    printf("didnt found any .TIF files\n");
	    exit(1);
	}

    // Setup filepaths to TRKIDX.TMP, TRKIDX01.HMA
    tempfile         = g_build_filename(himd->rootpath,himd->need_lowercase ? "hmdhifi" : "HMDHIFI",
					"TRKIDX.TMP",NULL);

    if(!g_file_set_contents(unusedfile, (const char*)himd->tifdata, HIMD_TIFFILE_SIZE, &error))
	{
	    printf("Could not update unused TIFDATA file %s.\n", unusedfile);
	    exit(1);
	}

    // unused                 -> tmp
    // used                   -> unused
    // tempfile               -> used

    if(g_rename(unusedfile, tempfile) < 0)
	{
	    printf("Could not rename blank unused %s to %s\n", unusedfile, tempfile);
	    exit(1);
	}
    if(g_rename(usedfile, unusedfile) < 0)
	{
	    printf("Could not rename %s to %s\n", usedfile, tempfile);
	    exit(1);
	}
    if(g_rename(tempfile, usedfile) < 0)
	{
	    printf("Could not rename %s to %s\n", tempfile, usedfile);
	}

    g_free(tempfile);
    g_free(unusedfile);
    g_free(usedfile);
    g_free(filepath);
    g_dir_close(dir);

    return 0;
}

static int himd_read_discid(struct himd * himd, struct himderrinfo * status)
{
    FILE * mclistfile = himd_open_file(himd, "MCLIST", HIMD_READ_ONLY);

    if(!mclistfile)
    {
        set_status_printf(status, HIMD_ERROR_CANT_OPEN_MCLIST,
                          _("Can't open mclist file: %s\n"), g_strerror(errno));
        return -1;
    }

    fseek(mclistfile,0x40L,SEEK_SET);
    if(fread(himd->discid,16,1,mclistfile) != 1)
    {
        set_status_printf(status, HIMD_ERROR_CANT_READ_MCLIST,
                          _("Can't read mclist file: %s\n"), g_strerror(errno));
        fclose(mclistfile);
        return -1;
    }
    fclose(mclistfile);
    himd->discid_valid = 1;
    return 0;
}

int himd_open(struct himd * himd, const char * himdroot, struct himderrinfo * status)
{
    char * filepath;
    char indexfilename[13];
    gsize filelen;
    GDir * dir;
    GError * error = NULL;
    
    g_return_val_if_fail(himd != NULL, -1);
    g_return_val_if_fail(himdroot != NULL, -1);

    himd->need_lowercase = 0;
    filepath = g_build_filename(himdroot,"HMDHIFI",NULL);
    dir = g_dir_open(filepath,0,&error);
    if(g_error_matches(error,G_FILE_ERROR,G_FILE_ERROR_NOENT))
    {
        g_error_free(error);
        error = NULL;
        filepath = g_build_filename(himdroot,"hmdhifi",NULL);
        dir = g_dir_open(filepath,0,&error);
        himd->need_lowercase = 1;
    }
    g_free(filepath);
    if(dir == NULL)
    {
        set_status_const(status, HIMD_ERROR_CANT_ACCESS_HMDHIFI, error->message);
        return -1;
    }

    himd->datanum = scanforatdata(dir);
    g_dir_close(dir);
    if(himd->datanum == -1)
    {
        set_status_const(status, HIMD_ERROR_NO_TRACK_INDEX, _("No track index file found"));
        return -1;		/* ERROR: track index not found */
    }
    
    sprintf(indexfilename,
            himd->need_lowercase ? "trkidx%02x.hma" : "TRKIDX%02X.HMA",
            himd->datanum);
    filepath = g_build_filename(himdroot,himd->need_lowercase ? "hmdhifi" : 
                                "HMDHIFI",indexfilename,NULL);
    if(!g_file_get_contents(filepath, (char**)&himd->tifdata, &filelen, &error))
    {
        set_status_printf(status, HIMD_ERROR_CANT_READ_TIF,
                          _("Can't load TIF data from %s: %s"),
                          filepath, error->message);
        g_free(filepath);
        return -1;
    }
    g_free(filepath);
    
    if(filelen != 0x50000)
    {
        set_status_printf(status, HIMD_ERROR_WRONG_TIF_SIZE,
                          _("TIF file is 0x%x bytes instead of 0x50000"),
                          (int)filelen);
        g_free(himd->tifdata);
        return -1;
    }

    if(memcmp(himd->tifdata,"TIF ",4) != 0)
    {
        set_status_printf(status, HIMD_ERROR_WRONG_TIF_MAGIC,
                         _("TIF file starts with wrong magic: %02x %02x %02x %02x"),
                         himd->tifdata[0],himd->tifdata[1],himd->tifdata[2],himd->tifdata[3]);
        g_free(himd->tifdata);
        return -1;
    }

    himd->rootpath = g_strdup(himdroot);
    himd->discid_valid = 0;

    return 0;
}

const unsigned char * himd_get_discid(struct himd * himd, struct himderrinfo * status)
{
    if(!himd->discid_valid && himd_read_discid(himd, status) < 0)
        return 0;
    return himd->discid;
}

void himd_close(struct himd * himd)
{
    g_free(himd->tifdata);
    g_free(himd->rootpath);
}

void himd_free(void * data)
{
    g_free(data);
}