udftools

- Main Page
- Classes
- Files

- File List
- File Members

All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
utils.c
Go to the documentation of this file.
1 /*
2 * utils.c
3 *
4 * Copyright (c) 2016 Vojtech Vladyka <vojtch.vladyka@gmail.com>
5 * All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 */
22 #include "config.h"
23
24 #include "utils.h"
25
26 #include <stdio.h>
27 #include <stdarg.h>
28
29 #define ANSI_COLOR_RED "\x1b[31m"
30 #define ANSI_COLOR_GREEN "\x1b[32m"
31 #define ANSI_COLOR_YELLOW "\x1b[33m"
32 #define ANSI_COLOR_BLUE "\x1b[34m"
33 #define ANSI_COLOR_MAGENTA "\x1b[35m"
34 #define ANSI_COLOR_CYAN "\x1b[36m"
35 #define ANSI_COLOR_RESET "\x1b[0m"
36
37 #define EOL ""
38
39 typedef enum {
40 show = 0,
41 message,
42 important,
43 warning,
44 error,
45 faterr,
46 debug
47 } message_type;
48
49 verbosity_e verbosity;
50
56 void read_tag(tag id) {
57 note("\tIdentification Tag\n"
58 "\t==================\n");
59 note("\tID: %d (", id.tagIdent);
60 switch(id.tagIdent) {
61 case TAG_IDENT_PVD:
62 note("PVD");
63 break;
64 case TAG_IDENT_AVDP:
65 note("AVDP");
66 break;
67 case TAG_IDENT_VDP:
68 note("VDP");
69 break;
70 case TAG_IDENT_IUVD:
71 note("IUVD");
72 break;
73 case TAG_IDENT_PD:
74 note("PD");
75 break;
76 case TAG_IDENT_LVD:
77 note("LVD");
78 break;
79 case TAG_IDENT_USD:
80 note("USD");
81 break;
82 case TAG_IDENT_TD:
83 note("TD");
84 break;
85 case TAG_IDENT_LVID:
86 note("LVID");
87 break;
88 }
89 note(")\n");
90 note("\tVersion: %d\n", id.descVersion);
91 note("\tChecksum: 0x%x\n", id.tagChecksum);
92 note("\tSerial Number: 0x%x\n", id.tagSerialNum);
93 note("\tDescriptor CRC: 0x%x, Length: %d\n", id.descCRC, id.descCRCLength);
94 note("\tTag Location: 0x%x\n", id.tagLocation);
95 }
96
102 int print_disc(struct udf_disc *disc) {
103 note("\nUDF Metadata Overview\n"
104 "---------------------\n");
105
106 note("AVDP\n"
107 "----\n");
108 for(int i=0; i<3; i++) {
109 note("[%d]\n", i);
110 if(disc->udf_anchor[i] != 0) {
111 read_tag(disc->udf_anchor[i]->descTag);
112 }
113 }
114
115 note("PVD\n"
116 "---\n");
117 for(int i=0; i<2; i++) {
118 note("[%d]\n", i);
119 if(disc->udf_pvd[i] != 0) {
120 read_tag(disc->udf_pvd[i]->descTag);
121 }
122 }
123
124 note("LVD\n"
125 "---\n");
126 for(int i=0; i<2; i++) {
127 note("[%d]\n", i);
128 if(disc->udf_lvd[i] != 0) {
129 read_tag(disc->udf_lvd[i]->descTag);
130 note("\tPartition Maps: %d\n",disc->udf_lvd[i]->partitionMaps[0]);
131 }
132 }
133
134 note("PD\n"
135 "--\n");
136 for(int i=0; i<2; i++) {
137 note("[%d]\n", i);
138 if(disc->udf_pd[i] != 0) {
139 read_tag(disc->udf_pd[i]->descTag);
140 }
141 }
142
143 note("USD\n"
144 "---\n");
145 for(int i=0; i<2; i++) {
146 note("[%d]\n", i);
147 if(disc->udf_usd[i] != 0) {
148 read_tag(disc->udf_usd[i]->descTag);
149 note("\tNumOfAllocDescs: %d\n", disc->udf_usd[i]->numAllocDescs);
150 }
151 }
152
153 note("IUVD\n"
154 "----\n");
155 for(int i=0; i<2; i++) {
156 note("[%d]\n", i);
157 if(disc->udf_iuvd[i] != 0) {
158 read_tag(disc->udf_iuvd[i]->descTag);
159 }
160 }
161
162 note("TD\n"
163 "--\n");
164 for(int i=0; i<2; i++) {
165 note("[%d]\n", i);
166 if(disc->udf_td[i] != 0) {
167 read_tag(disc->udf_td[i]->descTag);
168 }
169 }
170
171 return 0;
172 }
173
184 int prompt(const char *format, ...) {
185 va_list args;
186 char b = 0,c = 0;
187 char again = 0;
188
189 do {
190 again = 0;
191 va_start(args, format);
192
193 vprintf(format, args);
194
195 va_end(args);
196
197 c = getchar();
198 while ((b=getchar()) != EOF && b != '\n');
199
200 if(c == 'y' || c == 'Y') {
201 return 1;
202 } else if(c == 'n' || c == 'N') {
203 return 0;
204 } else if(c == '\n') {
205 return -1;
206 } else {
207 again = 1;
208 }
209 } while(again);
210
211 return -128;
212 }
213
222 void logger(message_type type, char *color, const char *format, va_list arg) {
223 //va_list arg;
224 //char *msg;
225 char *prefix;
226 //char *color;
227 FILE *stream;
228 verbosity_e verblvl;
229
230 switch(type) {
231 case debug:
232 prefix = "DBG";
233 // color = "";
234 stream = stdout;
235 verblvl = DBG;
236 break;
237 case message:
238 prefix = 0;
239 // color = "";
240 stream = stdout;
241 verblvl = MSG;
242 break;
243 case important:
244 prefix = 0;
245 // color = ANSI_COLOR_GREEN;
246 stream = stdout;
247 verblvl = WARN;
248 break;
249 case warning:
250 prefix = "WARN";
251 // color = ANSI_COLOR_YELLOW;
252 stream = stdout;
253 verblvl = WARN;
254 break;
255 case error:
256 prefix = "ERROR";
257 // color = ANSI_COLOR_RED;
258 stream = stderr;
259 verblvl = NONE;
260 break;
261 case faterr:
262 prefix = "FATAL";
263 // color = ANSI_COLOR_RED;
264 stream = stderr;
265 verblvl = NONE;
266 break;
267 default:
268 prefix = 0;
269 // color = "";
270 stream = stdout;
271 verblvl = DBG;
272 break;
273 }
274
275 if(verbosity >= verblvl) {
276 if(color == NULL)
277 color = "";
278 if(prefix > 0)
279 fprintf(stream, "%s[%s] ", color, prefix);
280 else
281 fprintf(stream, "%s", color);
282 vfprintf (stream, format, arg);
283 fprintf(stream, ANSI_COLOR_RESET EOL);
284 }
285 }
286
296 void dbg(const char *format, ...) {
297 va_list arg;
298 va_start (arg, format);
299 logger(debug, "", format, arg);
300 va_end (arg);
301 }
302
312 void dwarn(const char *format, ...) {
313 va_list arg;
314 va_start (arg, format);
315 logger(debug, ANSI_COLOR_YELLOW, format, arg);
316 va_end (arg);
317 }
318
328 void note(const char *format, ...) {
329 va_list arg;
330 va_start (arg, format);
331 logger(show, "", format, arg);
332 va_end (arg);
333 }
334
344 void msg(const char *format, ...) {
345 va_list arg;
346 va_start (arg, format);
347 logger(message, "", format, arg);
348 va_end (arg);
349 }
350
360 void imp(const char *format, ...) {
361 va_list arg;
362 va_start (arg, format);
363 logger(important, ANSI_COLOR_GREEN, format, arg);
364 va_end (arg);
365 }
366
376 void warn(const char *format, ...) {
377 va_list arg;
378 va_start (arg, format);
379 logger(warning, ANSI_COLOR_YELLOW, format, arg);
380 va_end (arg);
381 }
382
392 void err(const char *format, ...) {
393 va_list arg;
394 va_start (arg, format);
395 logger(error, ANSI_COLOR_RED, format, arg);
396 va_end (arg);
397 }
398
408 void fatal(const char *format, ...) {
409 va_list arg;
410 va_start (arg, format);
411 logger(faterr, ANSI_COLOR_RED, format, arg);
412 va_end (arg);
413 }
414
420 char * verbosity_level_str(verbosity_e lvl) {
421 switch(lvl) {
422 case NONE:
423 return "NONE";
424 case WARN:
425 return "WARNING";
426 case MSG:
427 return "MESSAGE";
428 case DBG:
429 return "DEBUG";
430 default:
431 return "UNKNOWN";
432 }
433 }
434
440 void print_metadata_sequence(vds_sequence_t *seq) {
441 note("Main Reserve\n");
442 note("ident | Errors | ident | Errors \n");
443 for(int i=0; i<VDS_STRUCT_AMOUNT; ++i) {
444 note("%5d | 0x%02x | %5d | 0x%02x \n", seq->main[i].tagIdent, seq->main[i].error, seq->reserve[i].tagIdent, seq->reserve[i].error);
445 }
446 }
ANSI_COLOR_RED
#define ANSI_COLOR_RED
Definition: utils.c:29
verbosity_e
verbosity_e
Definition: utils.h:38
TAG_IDENT_PVD
#define TAG_IDENT_PVD
Definition: ecma_167.h:182
MSG
Definition: utils.h:41
message
Definition: utils.c:41
vds_sequence_t
Definition: udffsck.h:56
NONE
Definition: utils.h:39
verbosity
verbosity_e verbosity
Definition: utils.c:49
anchorVolDescPtr::descTag
tag descTag
Definition: ecma_167.h:235
udf_disc::udf_pd
struct partitionDesc * udf_pd[2]
Definition: libudffs.h:114
unallocSpaceDesc::descTag
tag descTag
Definition: ecma_167.h:346
unallocSpaceDesc::numAllocDescs
uint32_t numAllocDescs
Definition: ecma_167.h:348
warning
Definition: utils.c:43
TAG_IDENT_TD
#define TAG_IDENT_TD
Definition: ecma_167.h:189
prompt
int prompt(const char *format,...)
Simple prompt printing out message and accepting y/Y/n/N. Anything else restarts prompt.
Definition: utils.c:184
udf_disc::udf_pvd
struct primaryVolDesc * udf_pvd[2]
Definition: libudffs.h:112
udf_disc::udf_anchor
struct anchorVolDescPtr * udf_anchor[3]
Definition: libudffs.h:111
ANSI_COLOR_RESET
#define ANSI_COLOR_RESET
Definition: utils.c:35
note
void note(const char *format,...)
Note output.
Definition: utils.c:328
logicalVolDesc::descTag
tag descTag
Definition: ecma_167.h:298
WARN
Definition: utils.h:40
partitionDesc::descTag
tag descTag
Definition: ecma_167.h:262
utils.h
dbg
void dbg(const char *format,...)
Debug output.
Definition: utils.c:296
logger
void logger(message_type type, char *color, const char *format, va_list arg)
Internall logger function producing printing to stdout.
Definition: utils.c:222
primaryVolDesc::descTag
tag descTag
Definition: ecma_167.h:205
show
Definition: utils.c:40
TAG_IDENT_IUVD
#define TAG_IDENT_IUVD
Definition: ecma_167.h:185
TAG_IDENT_VDP
#define TAG_IDENT_VDP
Definition: ecma_167.h:184
TAG_IDENT_PD
#define TAG_IDENT_PD
Definition: ecma_167.h:186
VDS_STRUCT_AMOUNT
#define VDS_STRUCT_AMOUNT
Maximum amount of VDS descriptors.
Definition: udffsck.h:36
udf_disc::udf_usd
struct unallocSpaceDesc * udf_usd[2]
Definition: libudffs.h:115
print_metadata_sequence
void print_metadata_sequence(vds_sequence_t *seq)
Prints metadata error sequence.
Definition: utils.c:440
important
Definition: utils.c:42
TAG_IDENT_LVD
#define TAG_IDENT_LVD
Definition: ecma_167.h:187
format
int format(int fd, int session_grow, int size)
Definition: ide-pc.c:102
TAG_IDENT_LVID
#define TAG_IDENT_LVID
Definition: ecma_167.h:190
error
Definition: utils.c:44
metadata_t::error
uint8_t error
errors found on descriptor
Definition: udffsck.h:53
msg
void msg(const char *format,...)
Message output.
Definition: utils.c:344
EOL
#define EOL
Definition: utils.c:37
vds_sequence_t::reserve
metadata_t reserve[VDS_STRUCT_AMOUNT]
Definition: udffsck.h:59
udf_disc::udf_td
struct terminatingDesc * udf_td[2]
Definition: libudffs.h:117
DBG
Definition: utils.h:42
ANSI_COLOR_GREEN
#define ANSI_COLOR_GREEN
Definition: utils.c:30
verbosity_level_str
char * verbosity_level_str(verbosity_e lvl)
Verbosity level to string.
Definition: utils.c:420
print_disc
int print_disc(struct udf_disc *disc)
Support function printing VDS, AVDP and LVID.
Definition: utils.c:102
udf_disc
Definition: libudffs.h:95
message_type
message_type
Definition: utils.c:39
faterr
Definition: utils.c:45
impUseVolDesc::descTag
tag descTag
Definition: ecma_167.h:253
vds_sequence_t::main
metadata_t main[VDS_STRUCT_AMOUNT]
Definition: udffsck.h:58
read_tag
void read_tag(tag id)
Support function for printing basic tag information.
Definition: utils.c:56
logicalVolDesc::partitionMaps
uint8_t partitionMaps[0]
Definition: ecma_167.h:310
err
void err(const char *format,...)
Error output.
Definition: utils.c:392
dwarn
void dwarn(const char *format,...)
Debug warning output.
Definition: utils.c:312
imp
void imp(const char *format,...)
Important message output.
Definition: utils.c:360
warn
void warn(const char *format,...)
Warning output.
Definition: utils.c:376
debug
Definition: utils.c:46
metadata_t::tagIdent
uint16_t tagIdent
descriptor identifier
Definition: udffsck.h:51
udf_disc::udf_lvd
struct logicalVolDesc * udf_lvd[2]
Definition: libudffs.h:113
fatal
void fatal(const char *format,...)
Fatal Error output.
Definition: utils.c:408
TAG_IDENT_USD
#define TAG_IDENT_USD
Definition: ecma_167.h:188
udf_disc::udf_iuvd
struct impUseVolDesc * udf_iuvd[2]
Definition: libudffs.h:116
ANSI_COLOR_YELLOW
#define ANSI_COLOR_YELLOW
Definition: utils.c:31
TAG_IDENT_AVDP
#define TAG_IDENT_AVDP
Definition: ecma_167.h:183
terminatingDesc::descTag
tag descTag
Definition: ecma_167.h:355