[576533]: src / remap.c  Maximize  Restore  History

Download this file

267 lines (240 with data), 6.4 kB

  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
/*
* This file is part of libdvdnav, a DVD navigation library.
*
* libdvdnav is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* libdvdnav 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with libdvdnav; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#ifndef _MSC_VER
#include <sys/param.h>
#include <sys/fcntl.h>
#else
#ifndef MAXPATHLEN
#define MAXPATHLEN 255
#endif
#endif /* _MSC_VER */
#include <inttypes.h>
#include <limits.h>
#include <sys/time.h>
#include "dvdnav/dvdnav.h"
#include <dvdread/nav_types.h>
#include <dvdread/ifo_types.h>
#include "remap.h"
#include "vm/decoder.h"
#include "vm/vm.h"
#include "dvdnav_internal.h"
struct block_s {
int domain;
int title;
int program;
unsigned long start_block;
unsigned long end_block;
};
struct remap_s {
char *title;
int maxblocks;
int nblocks;
int debug;
struct block_s *blocks;
};
static remap_t* remap_new( char *title) {
remap_t *map = malloc( sizeof(remap_t));
map->title = strdup(title);
map->maxblocks = 0;
map->nblocks = 0;
map->blocks = NULL;
map->debug = 0;
return map;
}
static int compare_block( block_t *a, block_t *b) {
/* returns -1 if a precedes b, 1 if a follows b, and 0 if a and b overlap */
if (a->domain < b->domain) {
return -1;
} else if (a->domain > b->domain) {
return 1;
}
if (a->title < b->title) {
return -1;
} else if (a->title > b->title) {
return 1;
}
if (a->program < b->program) {
return -1;
} else if (a->program > b->program) {
return 1;
}
if (a->end_block < b->start_block) {
return -1;
} else if (a->start_block > b->end_block) {
/*
* if a->start_block == b->end_block then the two regions
* aren't strictly overlapping, but they should be merged
* anyway since there are zero blocks between them
*/
return 1;
}
return 0;
}
static block_t *findblock( remap_t *map, block_t *key) {
int lb = 0;
int ub = map->nblocks - 1;
int mid;
int res;
while (lb <= ub) {
mid = lb + (ub - lb)/2;
res = compare_block( key, &map->blocks[mid]);
if (res < 0) {
ub = mid-1;
} else if (res > 0) {
lb = mid+1;
} else {
return &map->blocks[mid];
}
}
return NULL;
}
static void mergeblock( block_t *b, block_t tmp) {
if (tmp.start_block < b->start_block) b->start_block = tmp.start_block;
if (tmp.end_block > b->end_block) b->end_block = tmp.end_block;
}
static void remap_add_node( remap_t *map, block_t block) {
block_t *b;
int n;
b = findblock( map, &block);
if (b) {
/* overlaps an existing block */
mergeblock( b, block);
} else {
/* new block */
if (map->nblocks >= map->maxblocks) {
map->maxblocks += 20;
map->blocks = realloc( map->blocks, sizeof( block_t)*map->maxblocks);
}
n = map->nblocks++;
while (n > 0 && compare_block( &block, &map->blocks[ n-1]) < 0) {
map->blocks[ n] = map->blocks[ n-1];
n--;
}
map->blocks[ n] = block;
}
}
static int parseblock(char *buf, int *dom, int *tt, int *pg,
unsigned long *start, unsigned long *end) {
long tmp;
char *tok;
char *epos;
char *marker[]={"domain", "title", "program", "start", "end"};
int st = 0;
tok = strtok( buf, " ");
while (st < 5) {
if (strcmp(tok, marker[st])) return -st-1000;
tok = strtok( NULL, " ");
if (!tok) return -st-2000;
tmp = strtol( tok, &epos, 0);
if (*epos != 0 && *epos != ',') return -st-3000;
switch (st) {
case 0:
*dom = (int)tmp;
break;
case 1:
*tt = (int)tmp;
break;
case 2:
*pg = (int)tmp;
break;
case 3:
*start = tmp;
break;
case 4:
*end = tmp;
break;
}
st++;
tok = strtok( NULL, " ");
}
return st;
}
remap_t* remap_loadmap( char *title) {
char buf[160];
char fname[MAXPATHLEN];
char *home;
int res;
FILE *fp;
block_t tmp;
remap_t *map;
memset(&tmp, 0, sizeof(tmp));
/* Build the map filename */
home = getenv("HOME");
if(!home) {
fprintf(MSG_OUT, "libdvdnav: Unable to find home directory" );
return NULL;
}
snprintf(fname, sizeof(fname), "%s/.dvdnav/%s.map", home, title);
/* Open the map file */
fp = fopen( fname, "r");
if (!fp) {
fprintf(MSG_OUT, "libdvdnav: Unable to find map file '%s'\n", fname);
return NULL;
}
/* Load the map file */
map = remap_new( title);
while (fgets( buf, sizeof(buf), fp) != NULL) {
if (buf[0] == '\n' || buf[0] == '#' || buf[0] == 0) continue;
if (strncasecmp( buf, "debug", 5) == 0) {
map->debug = 1;
} else {
res = parseblock( buf,
&tmp.domain, &tmp.title, &tmp.program, &tmp.start_block, &tmp.end_block);
if (res != 5) {
fprintf(MSG_OUT, "libdvdnav: Ignoring map line (%d): %s\n", res, buf);
continue;
}
remap_add_node( map, tmp);
}
}
fclose(fp);
if (map->nblocks == 0 && map->debug == 0) {
free(map);
return NULL;
}
return map;
}
unsigned long remap_block(
remap_t *map, int domain, int title, int program,
unsigned long cblock, unsigned long offset)
{
block_t key;
block_t *b;
if (map->debug) {
fprintf(MSG_OUT, "libdvdnav: %s: domain %d, title %d, program %d, start %lx, next %lx\n",
map->title, domain, title, program, cblock, cblock+offset);
}
key.domain = domain;
key.title = title;
key.program = program;
key.start_block = key.end_block = cblock + offset;
b = findblock( map, &key);
if (b) {
if (map->debug) {
fprintf(MSG_OUT, "libdvdnav: Redirected to %lx\n", b->end_block);
}
return b->end_block - cblock;
}
return offset;
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks