rs03-verify.c 44.1 KB
Newer Older
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 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
/*  dvdisaster: Additional error correction for optical media.
 *  Copyright (C) 2004-2015 Carsten Gnoerlich.
 *
 *  Email: carsten@dvdisaster.org  -or-  cgnoerlich@fsfe.org
 *  Project homepage: http://www.dvdisaster.org
 *
 *  This file is part of dvdisaster.
 *
 *  dvdisaster 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  dvdisaster 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 dvdisaster. If not, see <http://www.gnu.org/licenses/>.
 */

#include "dvdisaster.h"

#include "rs03-includes.h"

#define EXIT_CODE_SIZE_MISMATCH 1
#define EXIT_CODE_VERSION_MISMATCH 2

#define EXIT_CODE_UNEXPECTED_EOF 10
#define EXIT_CODE_MISSING_SECTOR 11
#define EXIT_CODE_CHECKSUM_ERROR 12
#define EXIT_CODE_SYNDROME_ERROR 13

/***
 *** Reset the verify output window
 ***/

void ResetRS03VerifyWindow(Method *self)
{  RS03Widgets *wl = (RS03Widgets*)self->widgetList;

   SetLabelText(GTK_LABEL(wl->cmpImageSectors), "");
   SetLabelText(GTK_LABEL(wl->cmpImageMd5Sum), "");
   SetLabelText(GTK_LABEL(wl->cmpDataSection), "");
   SetLabelText(GTK_LABEL(wl->cmpCrcSection), "");
   SetLabelText(GTK_LABEL(wl->cmpEccSection), "");
   SetLabelText(GTK_LABEL(wl->cmpImageErasure), "");
   SetLabelText(GTK_LABEL(wl->cmpImagePrognosis), "");
   SetLabelText(GTK_LABEL(wl->cmpImageErasureCnt), "");
   SetLabelText(GTK_LABEL(wl->cmpImagePrognosisMsg), "");
   SetLabelText(GTK_LABEL(wl->cmpImageResult), "");
   SwitchAndSetFootline(wl->cmpImageNotebook, 1, NULL, NULL);

   SetLabelText(GTK_LABEL(wl->cmpEccCreatedBy), "dvdisaster");
   SetLabelText(GTK_LABEL(wl->cmpEccMethod), "");
   SetLabelText(GTK_LABEL(wl->cmpEccType), "");
   SetLabelText(GTK_LABEL(wl->cmpEccRequires), "");
   SetLabelText(GTK_LABEL(wl->cmpEccDataCrc), _("Data checksum:"));
   SetLabelText(GTK_LABEL(wl->cmpEccDataCrcVal), "");
   SetLabelText(GTK_LABEL(wl->cmpEccFingerprint), _("n/a"));
   SetLabelText(GTK_LABEL(wl->cmpEccResult), "");
   SetLabelText(GTK_LABEL(wl->cmpEccSynLabel), "");
   SetLabelText(GTK_LABEL(wl->cmpEccSyndromes), "");

   wl->lastPercent = 0;

   FillSpiral(wl->cmpSpiral, Closure->background);
   DrawSpiral(wl->cmpSpiral);
}

/***
 *** Manage the image spiral
 ***/

/*
 * Update part of the spiral
 */

typedef struct _spiral_idle_info
{  Spiral *cmpSpiral;
   GdkColor *segColor;
   int from, to;
} spiral_idle_info;

static gboolean spiral_idle_func(gpointer data)
{  spiral_idle_info *sii = (spiral_idle_info*)data;
   int i;

   for(i=sii->from; i<=sii->to; i++)
     DrawSpiralSegment(sii->cmpSpiral, sii->segColor, i-1);

   g_free(sii);
   return FALSE;
}

static void add_verify_values(Method *method, int percent, 
			       gint64 newMissing, gint64 newCrcErrors)
{  RS03Widgets *wl = (RS03Widgets*)method->widgetList;
   spiral_idle_info *sii = g_malloc(sizeof(spiral_idle_info));

   if(percent < 0 || percent > VERIFY_IMAGE_SEGMENTS)
     return;

   sii->cmpSpiral = wl->cmpSpiral;

   sii->segColor = Closure->greenSector;
   if(newCrcErrors) sii->segColor = Closure->yellowSector;
   if(newMissing) sii->segColor = Closure->redSector;

   sii->from = wl->lastPercent+1;
   sii->to   = percent;

   wl->lastPercent = percent;

   g_idle_add(spiral_idle_func, sii);
}

/*
 * Redraw whole spiral
 */

static void redraw_spiral(RS03Widgets *wl)
{  int x = wl->cmpSpiral->mx - wl->cmpSpiral->diameter/2 + 10;

   DrawSpiralLabel(wl->cmpSpiral, wl->cmpLayout,
		   _("Good sectors"), Closure->greenSector, x, 1);

   DrawSpiralLabel(wl->cmpSpiral, wl->cmpLayout,
		   _("Sectors with CRC errors"), Closure->yellowSector, x, 2);

   DrawSpiralLabel(wl->cmpSpiral, wl->cmpLayout,
		   _("Missing sectors"), Closure->redSector, x, 3);

   DrawSpiral(wl->cmpSpiral);
}

/*
 * expose event handler for the spiral
 */

static gboolean expose_cb(GtkWidget *widget, GdkEventExpose *event, gpointer data)
{  RS03Widgets *wl = (RS03Widgets*)data;
   GtkAllocation *a = &widget->allocation;
   int w,h,size;

   /* Finish spiral initialization */

   if(!wl->cmpLayout)
   {  SetSpiralWidget(wl->cmpSpiral, widget);
      wl->cmpLayout = gtk_widget_create_pango_layout(widget, NULL);
   }

   SetText(wl->cmpLayout, _("Missing sectors"), &w, &h);
   size = wl->cmpSpiral->diameter + 20 + 3*(10+h);  /* approx. size of spiral + labels */

   wl->cmpSpiral->mx = a->width / 2;
   wl->cmpSpiral->my = (wl->cmpSpiral->diameter + a->height - size)/2;

   if(!event->count)      /* Exposure compression */
     redraw_spiral(wl);   /* Redraw the spiral */

   return TRUE;
}

/***
 *** Create the notebook contents for the verify output
 ***/

void CreateRS03VerifyWindow(Method *self, GtkWidget *parent)
{  RS03Widgets *wl = (RS03Widgets*)self->widgetList;
   GtkWidget *sep,*notebook,*ignore,*table,*table2,*lab,*frame,*d_area;
   int y1,y2;

   wl->cmpHeadline = gtk_label_new(NULL);
   gtk_misc_set_alignment(GTK_MISC(wl->cmpHeadline), 0.0, 0.0); 
   gtk_misc_set_padding(GTK_MISC(wl->cmpHeadline), 5, 0);
   gtk_box_pack_start(GTK_BOX(parent), wl->cmpHeadline, FALSE, FALSE, 3);

   sep = gtk_hseparator_new();
   gtk_box_pack_start(GTK_BOX(parent), sep, FALSE, FALSE, 0);

   sep = gtk_hseparator_new();
   gtk_box_pack_start(GTK_BOX(parent), sep, FALSE, FALSE, 0);

   table = gtk_table_new(2, 2, FALSE);
   gtk_container_set_border_width(GTK_CONTAINER(table), 5);
   gtk_box_pack_start(GTK_BOX(parent), table, TRUE, TRUE, 0);


   /*** Ecc data info */

   frame = gtk_frame_new(_utf("Error correction properties"));
   gtk_table_attach(GTK_TABLE(table), frame, 0, 1, 0, 1, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 5, 5);

   table2 = gtk_table_new(2, 7, FALSE);
   ignore = gtk_label_new("ecc info");
   gtk_container_set_border_width(GTK_CONTAINER(table2), 5);
   gtk_container_add(GTK_CONTAINER(frame), table2);
   y1=0; y2=1;

   lab = gtk_label_new(NULL);
   gtk_misc_set_alignment(GTK_MISC(lab), 0.0, 0.0); 
   SetLabelText(GTK_LABEL(lab), _("Type:"));
   gtk_table_attach(GTK_TABLE(table2), lab, 0, 1, y1, y2, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 5, 2 );
   lab = wl->cmpEccType = gtk_label_new(NULL);
   gtk_misc_set_alignment(GTK_MISC(lab), 0.0, 0.0); 
   gtk_table_attach(GTK_TABLE(table2), lab, 1, 2, y1, y2, GTK_EXPAND | GTK_FILL, GTK_SHRINK, 0, 0);
   y1++; y2++;

   lab = gtk_label_new(NULL);
   gtk_misc_set_alignment(GTK_MISC(lab), 0.0, 0.0); 
   SetLabelText(GTK_LABEL(lab), _("Method:"));
   gtk_table_attach(GTK_TABLE(table2), lab, 0, 1, y1, y2, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 5, 2 );
   lab = wl->cmpEccMethod = gtk_label_new(NULL);
   gtk_misc_set_alignment(GTK_MISC(lab), 0.0, 0.0); 
   gtk_table_attach(GTK_TABLE(table2), lab, 1, 2, y1, y2, GTK_EXPAND | GTK_FILL, GTK_SHRINK, 0, 0);
   y1++; y2++;

   lab = gtk_label_new(NULL);
   gtk_misc_set_alignment(GTK_MISC(lab), 0.0, 0.0); 
   SetLabelText(GTK_LABEL(lab), _("Created by:"));
   gtk_table_attach(GTK_TABLE(table2), lab, 0, 1, y1, y2, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 5, 2 );
   lab = wl->cmpEccCreatedBy = gtk_label_new(NULL);
   gtk_misc_set_alignment(GTK_MISC(lab), 0.0, 0.0); 
   gtk_table_attach(GTK_TABLE(table2), lab, 1, 2, y1, y2, GTK_EXPAND | GTK_FILL, GTK_SHRINK, 0, 0);
   y1++; y2++;

   lab = gtk_label_new(NULL);
   gtk_misc_set_alignment(GTK_MISC(lab), 0.0, 0.0); 
   SetLabelText(GTK_LABEL(lab), _("Requires:"));
   gtk_table_attach(GTK_TABLE(table2), lab, 0, 1, y1, y2, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 5, 2 );
   lab = wl->cmpEccRequires = gtk_label_new(NULL);
   gtk_misc_set_alignment(GTK_MISC(lab), 0.0, 0.0); 
   gtk_table_attach(GTK_TABLE(table2), lab, 1, 2, y1, y2, GTK_EXPAND | GTK_FILL, GTK_SHRINK, 0, 0);
   y1++; y2++;

   lab = wl->cmpEccDataCrc = gtk_label_new(NULL);
   gtk_misc_set_alignment(GTK_MISC(lab), 0.0, 0.0); 
   SetLabelText(GTK_LABEL(lab), _("Data checksum:"));
   gtk_table_attach(GTK_TABLE(table2), lab, 0, 1, y1, y2, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 5, 2 );
   lab = wl->cmpEccDataCrcVal = gtk_label_new(NULL);
   gtk_misc_set_alignment(GTK_MISC(lab), 0.0, 0.0); 
   gtk_table_attach(GTK_TABLE(table2), lab, 1, 2, y1, y2, GTK_EXPAND | GTK_FILL, GTK_SHRINK, 0, 0);
   y1++; y2++;

   lab = gtk_label_new(NULL);
   gtk_misc_set_alignment(GTK_MISC(lab), 0.0, 0.0); 
   SetLabelText(GTK_LABEL(lab), _("Fingerprint:"));
   gtk_table_attach(GTK_TABLE(table2), lab, 0, 1, y1, y2, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 5, 2 );
   lab = wl->cmpEccFingerprint = gtk_label_new(NULL);
   gtk_misc_set_alignment(GTK_MISC(lab), 0.0, 0.0); 
   gtk_table_attach(GTK_TABLE(table2), lab, 1, 2, y1, y2, GTK_EXPAND | GTK_FILL, GTK_SHRINK, 0, 0);
   y1++; y2++;

   lab = wl->cmpEccResult = gtk_label_new(NULL);
   gtk_misc_set_alignment(GTK_MISC(lab), 0.0, 0.0);
   gtk_table_attach(GTK_TABLE(table2), lab, 0, 2, y1, y2, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 5, 4);

   /*** Image spiral */

   frame = gtk_frame_new(_utf("Image state"));
   gtk_table_attach(GTK_TABLE(table), frame, 1, 2, 0, 2, GTK_SHRINK | GTK_FILL, GTK_EXPAND | GTK_FILL, 5, 5);

   wl->cmpSpiral = CreateSpiral(Closure->grid, Closure->background, 10, 5, VERIFY_IMAGE_SEGMENTS);
   d_area = wl->cmpDrawingArea = gtk_drawing_area_new();
   gtk_widget_set_size_request(d_area, wl->cmpSpiral->diameter+20, -1);
   gtk_container_add(GTK_CONTAINER(frame), d_area);
   g_signal_connect(G_OBJECT(d_area), "expose_event", G_CALLBACK(expose_cb), (gpointer)wl);

   /*** Image info */

   frame = gtk_frame_new(_utf("Data integrity"));
   gtk_table_attach(GTK_TABLE(table), frame, 0, 1, 1, 2, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 5, 5);

   notebook = wl->cmpImageNotebook = gtk_notebook_new();
   gtk_notebook_set_show_tabs(GTK_NOTEBOOK(notebook), FALSE);
   gtk_notebook_set_show_border(GTK_NOTEBOOK(notebook), FALSE);
   gtk_container_add(GTK_CONTAINER(frame), notebook);

   ignore = gtk_label_new("no image");
   lab = gtk_label_new(_utf("No image present."));
   gtk_notebook_append_page(GTK_NOTEBOOK(notebook), lab, ignore);

   table2 = gtk_table_new(2, 9, FALSE);
   ignore = gtk_label_new("image info");
   gtk_notebook_append_page(GTK_NOTEBOOK(notebook), table2, ignore);
   gtk_container_set_border_width(GTK_CONTAINER(table2), 5);
   y1=0; y2=1;

   lab = gtk_label_new(NULL);
   gtk_misc_set_alignment(GTK_MISC(lab), 0.0, 0.0); 
   SetLabelText(GTK_LABEL(lab), _("Medium sectors:"));
   gtk_table_attach(GTK_TABLE(table2), lab, 0, 1, y1, y2, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 5, 2 );
   lab = wl->cmpImageSectors = gtk_label_new("0");
   gtk_misc_set_alignment(GTK_MISC(lab), 0.0, 0.0); 
   gtk_table_attach(GTK_TABLE(table2), lab, 1, 2, y1, y2, GTK_EXPAND | GTK_FILL, GTK_SHRINK, 0, 0);
   y1++; y2++;

   lab = gtk_label_new(NULL);
   gtk_misc_set_alignment(GTK_MISC(lab), 0.0, 0.0); 
   SetLabelText(GTK_LABEL(lab), _("Data checksum:"));
   gtk_table_attach(GTK_TABLE(table2), lab, 0, 1, y1, y2, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 5, 2 );
   lab = wl->cmpImageMd5Sum = gtk_label_new("0");
   gtk_misc_set_alignment(GTK_MISC(lab), 0.0, 0.0); 
   gtk_table_attach(GTK_TABLE(table2), lab, 1, 2, y1, y2, GTK_EXPAND | GTK_FILL, GTK_SHRINK, 0, 0);
   y1++; y2++;

   lab = gtk_label_new(NULL);
   gtk_misc_set_alignment(GTK_MISC(lab), 0.0, 0.0); 
   SetLabelText(GTK_LABEL(lab), _("Data section:"));
   gtk_table_attach(GTK_TABLE(table2), lab, 0, 1, y1, y2, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 5, 2 );
   lab = wl->cmpDataSection = gtk_label_new(".");
   gtk_misc_set_alignment(GTK_MISC(lab), 0.0, 0.0); 
   gtk_table_attach(GTK_TABLE(table2), lab, 1, 2, y1, y2, GTK_EXPAND | GTK_FILL, GTK_SHRINK, 0, 0);
   y1++; y2++;

   lab = gtk_label_new(NULL);
   gtk_misc_set_alignment(GTK_MISC(lab), 0.0, 0.0); 
   SetLabelText(GTK_LABEL(lab), _("Crc section:"));
   gtk_table_attach(GTK_TABLE(table2), lab, 0, 1, y1, y2, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 5, 2 );
   lab = wl->cmpCrcSection = gtk_label_new(".");
   gtk_misc_set_alignment(GTK_MISC(lab), 0.0, 0.0); 
   gtk_table_attach(GTK_TABLE(table2), lab, 1, 2, y1, y2, GTK_EXPAND | GTK_FILL, GTK_SHRINK, 0, 0);
   y1++; y2++;

   lab = gtk_label_new(NULL);
   gtk_misc_set_alignment(GTK_MISC(lab), 0.0, 0.0); 
   SetLabelText(GTK_LABEL(lab), _("Ecc section:"));
   gtk_table_attach(GTK_TABLE(table2), lab, 0, 1, y1, y2, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 5, 2 );
   lab = wl->cmpEccSection= gtk_label_new(".");
   gtk_misc_set_alignment(GTK_MISC(lab), 0.0, 0.0); 
   gtk_table_attach(GTK_TABLE(table2), lab, 1, 2, y1, y2, GTK_EXPAND | GTK_FILL, GTK_SHRINK, 0, 0);
   y1++; y2++;

   lab = wl->cmpEccSynLabel = gtk_label_new(NULL);
   gtk_misc_set_alignment(GTK_MISC(lab), 0.0, 0.0);
   SetLabelText(GTK_LABEL(lab), _("Ecc block test:")); 
   gtk_table_attach(GTK_TABLE(table2), lab, 0, 1, y1, y2, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 5, 2 );
   lab = wl->cmpEccSyndromes = gtk_label_new(NULL);
   gtk_misc_set_alignment(GTK_MISC(lab), 0.0, 0.0); 
   gtk_table_attach(GTK_TABLE(table2), lab, 1, 2, y1, y2, GTK_EXPAND | GTK_FILL, GTK_SHRINK, 0, 0);
   y1++; y2++;

   lab = wl->cmpImageErasure = gtk_label_new(NULL);
   gtk_misc_set_alignment(GTK_MISC(lab), 0.0, 0.0); 
   gtk_table_attach(GTK_TABLE(table2), lab, 0, 1, y1, y2, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 5, 2 );
   lab = wl->cmpImageErasureCnt = gtk_label_new(NULL);
   gtk_misc_set_alignment(GTK_MISC(lab), 0.0, 0.0); 
   gtk_table_attach(GTK_TABLE(table2), lab, 1, 2, y1, y2, GTK_EXPAND | GTK_FILL, GTK_SHRINK, 0, 0);
   y1++; y2++;

   lab = wl->cmpImagePrognosis = gtk_label_new(NULL);
   gtk_misc_set_alignment(GTK_MISC(lab), 0.0, 0.0); 
   gtk_table_attach(GTK_TABLE(table2), lab, 0, 1, y1, y2, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 5, 2 );
   lab = wl->cmpImagePrognosisMsg = gtk_label_new(NULL);
   gtk_misc_set_alignment(GTK_MISC(lab), 0.0, 0.0); 
   gtk_table_attach(GTK_TABLE(table2), lab, 1, 2, y1, y2, GTK_EXPAND | GTK_FILL, GTK_SHRINK, 0, 0);
   y1++; y2++;

   lab = wl->cmpImageResult = gtk_label_new(NULL);
   gtk_misc_set_alignment(GTK_MISC(lab), 0.0, 0.0);
   gtk_table_attach(GTK_TABLE(table2), lab, 0, 2, y1, y2, GTK_SHRINK | GTK_FILL, GTK_SHRINK, 5, 4);
}

/***
 *** Check the consistency of the augmented image
 ***/

/* 
 * housekeeping
 */

typedef struct
{  Image *image;
   EccHeader *eh;
   RS03Layout *lay;
   RS03Widgets *wl;
   CrcBuf *crcBuf;
   Bitmap *map;
   unsigned char crcSum[16];
   unsigned char *eccBlock[256];
   GaloisTables *gt;
   ReedSolomonTables *rt;
} verify_closure;

static void cleanup(gpointer data)
{  verify_closure *vc = (verify_closure*)data;
   int i;

   UnregisterCleanup();

   if(Closure->guiMode)
      AllowActions(TRUE);

   if(vc->image) CloseImage(vc->image);
   if(vc->lay) 
   {  g_free(vc->lay);
   }
   if(vc->map) FreeBitmap(vc->map);
   if(vc->crcBuf) FreeCrcBuf(vc->crcBuf);

   for(i=0; i<255; i++)
      if(vc->eccBlock[i])
	 g_free(vc->eccBlock[i]);

   if(vc->gt) FreeGaloisTables(vc->gt);
   if(vc->rt) FreeReedSolomonTables(vc->rt);

   g_free(vc);

   if(Closure->guiMode)
     g_thread_exit(0);
}

/***
 *** Prognosis for correctability
 ***/

static int prognosis(verify_closure *vc, gint64 missing, gint64 expected)
{  int j,eccblock;
   int worst_ecc = 0;
   gint64 damaged_sectors = 0, damaged_eccsecs = 0;
   gint64 correctable = 0;
   gint64 recoverable;

   for(eccblock=0; eccblock<vc->lay->sectorsPerLayer; eccblock++)
   {  int count = 255;

      /* Note: ecc file sectors are virtually mapped to augmented image sectors */

      for(j=0; j<255; j++)
      {  gint64 sector = j*vc->lay->sectorsPerLayer+eccblock;

	 if(GetBit(vc->map, sector))
	      count--;
	 else damaged_sectors++;
      }

      if(count>0)                damaged_eccsecs++;
      if(count>worst_ecc)        worst_ecc = count; 
      if(count<=vc->lay->nroots) correctable += count;
   }

   recoverable = expected - missing + correctable;

   if(damaged_sectors > 0)
   {  int percentage  = (1000*recoverable) / expected;

      PrintLog(_("- erasure counts   :  avg =  %.1f; worst = %d per ecc block.\n"),
	      (double)damaged_sectors/(double)damaged_eccsecs,worst_ecc);

      PrintLog(_("- prognosis        : %lld of %lld sectors recoverable (%d.%d%%)\n"),
	       recoverable, expected, percentage/10, percentage%10);

      if(Closure->guiMode)
      {  SetLabelText(GTK_LABEL(vc->wl->cmpImageErasure), _("Erasure counts:"));
	 SetLabelText(GTK_LABEL(vc->wl->cmpImagePrognosis), _("Prognosis:"));

	 SetLabelText(GTK_LABEL(vc->wl->cmpImageErasureCnt),
		      _("<span %s>avg =  %.1f; worst = %d per ecc block.</span>"),
		      worst_ecc <= vc->lay->nroots ? Closure->greenMarkup : Closure->redMarkup,
		      (double)damaged_sectors/(double)damaged_eccsecs,worst_ecc);

	 SetLabelText(GTK_LABEL(vc->wl->cmpImagePrognosisMsg),
		     _("<span %s>%lld of %lld sectors recoverable (%d.%d%%)</span>"),
		      recoverable < expected ? Closure->redMarkup : Closure->greenMarkup,
		     recoverable, expected, percentage/10, percentage%10);
      }
   }	      

   if(damaged_sectors && worst_ecc <= vc->lay->nroots && recoverable >= expected)
        return TRUE;
   else return FALSE;
}

/***
 *** Error syndrome check
 ***/

static int check_syndromes(verify_closure *vc)
{  RS03Layout *lay = vc->lay;
   Image *image = vc->image;
   gint64 li,ecc_block;
   gint64 cache_idx = Closure->prefetchSectors;
   gint64 ecc_good, ecc_bad, ecc_bad_sub;
   int percent,last_percent = -1;
   int bad_counted;
   int layer,i,j;

   if(Closure->guiMode)
     SetLabelText(GTK_LABEL(vc->wl->cmpHeadline), "<big>%s</big>\n<i>%s</i>",
		  _("Checking the image and error correction files."),
		  _("- Checking ecc blocks (deep verify) -"));

   /* Allocate buffers and initialize layer sector addresses */

   for(i=0, li=0; i<GF_FIELDMAX; i++,li+=lay->sectorsPerLayer)
   {  
      vc->eccBlock[i] = g_try_malloc(2048*Closure->prefetchSectors);
      if(!vc->eccBlock[i])  /* out of memory */
      {  int j;

	 for(j=0; j<i; j++)
	    g_free(vc->eccBlock[j]);

	 if(Closure->guiMode)
	   SetLabelText(GTK_LABEL(vc->wl->cmpEccSyndromes),
			_("<span %s>Out of memory; try reducing sector prefetch!</span>"),
			Closure->redMarkup);
	 PrintLog(_("* Ecc block test   : out of memory; try reducing sector prefetch!\n"));
	 return 0;
      }
   }

   /* Init Reed-Solomon tables */

   vc->gt = CreateGaloisTables(RS_GENERATOR_POLY);
   vc->rt = CreateReedSolomonTables(vc->gt, RS_FIRST_ROOT, RS_PRIM_ELEM, lay->nroots);

   /* Check the error syndromes */

   ecc_good = ecc_bad = ecc_bad_sub = 0;

   for(ecc_block=0; ecc_block<lay->sectorsPerLayer; ecc_block++)
   {  gint64 num_sectors = 0; 
      unsigned char data[GF_FIELDMAX];

      /* Check for user interruption */

      if(Closure->stopActions)   
      {  if(Closure->stopActions == STOP_CURRENT_ACTION) /* suppress memleak warning when closing window */
	    SetLabelText(GTK_LABEL(vc->wl->cmpEccSyndromes), 
			 _("<span %s>Aborted by user request!</span>"),
			 Closure->redMarkup); 
         return 0;
      }

      /* Reload cache? */
      
      if(cache_idx == Closure->prefetchSectors)
      {  
	 cache_idx = 0;
	 num_sectors = Closure->prefetchSectors;
	 if(ecc_block+num_sectors >= lay->sectorsPerLayer)
	    num_sectors = lay->sectorsPerLayer - ecc_block;

	 for(layer=0; layer<GF_FIELDMAX; layer++)
	   if(layer < lay->ndata-1)
	     RS03ReadSectors(image, vc->lay, vc->eccBlock[layer], 
			    layer, ecc_block, num_sectors, RS03_READ_DATA);
	   else
	     RS03ReadSectors(image, vc->lay, vc->eccBlock[layer], 
			    layer, ecc_block, num_sectors, RS03_READ_CRC | RS03_READ_ECC);
      }

      /* Calculate the error syndromes.
	 Note that we are only called when the image does not contain
	 dead sector markers; therefore we can skip this test. */

      bad_counted = FALSE;

      for(i=0; i<2048; i++) 
      {  int result;

	 for(j=0; j<GF_FIELDMAX; j++)
	   data[j] = vc->eccBlock[j][2048*cache_idx+i];

	 result = TestErrorSyndromes(vc->rt, data);

	 if(result)
	 {  ecc_bad_sub++;
	    if(!bad_counted)
	    {  bad_counted++;
	       ecc_bad++;
	    }
	 }
      }
      cache_idx++;

      if(!bad_counted) ecc_good++;

      /* Advance percentage gauge */

      percent = (100*(ecc_block+1))/lay->sectorsPerLayer;
      if(percent != last_percent)
      {  last_percent = percent;

	 if(!ecc_bad)
	 {  if(Closure->guiMode)
	      SetLabelText(GTK_LABEL(vc->wl->cmpEccSyndromes),
			   _("%d%% tested"),
			   percent);
	    PrintProgress(_("- Ecc block test   : %d%% tested"), percent);

	 }
	 else
	 {  if(Closure->guiMode)
	      SetLabelText(GTK_LABEL(vc->wl->cmpEccSyndromes),
			   _("<span %s>%lld good, %lld bad; %d%% tested</span>"),
			   Closure->redMarkup, ecc_good, ecc_bad, percent);
	    PrintProgress(_("* Ecc block test   : %lld good, %lld bad; %d%% tested")
			  , ecc_good, ecc_bad, percent);
	 }
      }
   }

   /* Tell user about our findings */

   if(!ecc_bad)
   {  if(Closure->guiMode)
       SetLabelText(GTK_LABEL(vc->wl->cmpEccSyndromes),_("pass"));
      ClearProgress();
      PrintLog(_("- Ecc block test   : pass\n"));
   }
   else
   {  if(Closure->guiMode)
       SetLabelText(GTK_LABEL(vc->wl->cmpEccSyndromes),
		    _("<span %s>%lld good, %lld bad; %lld bad sub blocks</span>"),
		    Closure->redMarkup, ecc_good, ecc_bad, ecc_bad_sub);
      PrintLog(_("* Ecc block test   : %lld good, %lld bad; %lld bad sub blocks\n"),
	       ecc_good, ecc_bad, ecc_bad_sub);

      exitCode = EXIT_CODE_SYNDROME_ERROR;
   }
   return ecc_bad;
}

/***
 *** The verify action
 ***/

void RS03Verify(Image *image)
{  Method *self = FindMethod("RS03");
   verify_closure *vc = g_malloc0(sizeof(verify_closure));
   RS03Widgets *wl = self->widgetList;
   EccHeader *eh = NULL;
   RS03Layout *lay;
   RS03CksumClosure *csc;
   struct MD5Context image_md5;
   unsigned char medium_sum[16];
   char data_digest[33], hdr_digest[33];
   gint64 s, crc_idx;
   int last_percent = 0;
   unsigned char buf[2048];
   gint64 first_missing, last_missing;
   gint64 total_missing,data_missing,crc_missing,ecc_missing;
   gint64 new_missing = 0, new_crc_errors = 0;
   gint64 data_crc_errors;
   gint64 virtual_expected;
   gint64 expected_image_sectors;
   gint64 eccfile_sectors = 0,expected_eccfile_sectors = 0;
   int major,minor,pl;
   char method[5];
   char *img_advice = NULL;
   char *ecc_advice = NULL;
   char *version;
   int syn_error = 0;
   int try_it;
   int missing_sector_explained = 0;
   int matching_byte_size = TRUE;

   /*** Prepare for early termination */

   RegisterCleanup(_("Check aborted"), cleanup, vc);
   vc->image = image;
   vc->wl = wl;

   if(image->eccFileHeader && !strncmp((char*)(image->eccFileHeader->method), "RS03", 4)) 
   {      eh = image->eccFileHeader;
   }
   else if(image->eccHeader && !strncmp((char*)(image->eccHeader->method), "RS03", 4)) 
   {      eh = image->eccHeader;
   }
   else Stop("Internal error: RS03Verify() called without suitable image and ecc file");
   
   vc->eh = eh; 

   /*** Announce type of error correction and what we are going to do */

   if(image->file)
   {
      PrintLog("\n%s present.\n", Closure->imageName);
   }
   else  /* may only happen when ecc file is present */
   {  PrintLog("\n%s not present.\n", Closure->imageName);

      if(Closure->guiMode)
	SwitchAndSetFootline(wl->cmpImageNotebook, 0, NULL, NULL);
   }

   if(eh->methodFlags[0] & MFLAG_ECC_FILE)
   {
      if(Closure->guiMode)
	SetLabelText(GTK_LABEL(wl->cmpHeadline), "<big>%s</big>\n<i>%s</i>",
		     _("Checking the image and error correction files."),
		     _("- Checking image file -"));

      PrintLog(_("%s present.\n"), Closure->eccName);
   }
   else 
   {  
     if(Closure->guiMode)
       SetLabelText(GTK_LABEL(wl->cmpHeadline), "<big>%s</big>\n<i>%s</i>",
		    _("Checking the image file."),
		    _("- Checking image file -"));
   }

   /*** Calculate the layout */

   if(eh->methodFlags[0] & MFLAG_ECC_FILE)
        lay = vc->lay = CalcRS03Layout(image, ECC_FILE); 
   else lay = vc->lay = CalcRS03Layout(image, ECC_IMAGE); 

   /*** Print information on the ecc portion */

   PrintLog(_("\nError correction properties:\n"));

   /* Check size of error correction file */

   if(lay->target == ECC_FILE)
   {  eccfile_sectors = image->eccFile->size / 2048;
      expected_eccfile_sectors = 2 + (lay->nroots+1)*lay->sectorsPerLayer;

      if(expected_eccfile_sectors != eccfile_sectors)
      {  char *msg;

	 if(expected_eccfile_sectors > eccfile_sectors)
	      msg = g_strdup_printf(_("Ecc file is %lld sectors shorter than expected."),
				  expected_eccfile_sectors - eccfile_sectors);
	 else msg = g_strdup_printf(_("Ecc file is %lld sectors longer than expected."), 
				    eccfile_sectors - expected_eccfile_sectors);

	 if(Closure->guiMode)
	    ecc_advice = g_strdup_printf("<span %s>%s</span>", Closure->redMarkup, msg);

	 PrintLog(_("* Warning          : %s\n"), msg);
	 g_free(msg);
	 exitCode = EXIT_CODE_SIZE_MISMATCH;
      }
   }
   
   /* Error correction type */

   if(eh->methodFlags[0] & MFLAG_ECC_FILE)
        PrintLog(_("- type             : Error correction file\n"));
   else PrintLog(_("- type             : Augmented image\n"));

   if(Closure->guiMode)
   {  if(eh->methodFlags[0] & MFLAG_ECC_FILE)
	   SetLabelText(GTK_LABEL(wl->cmpEccType), _("Error correction file"));
      else SetLabelText(GTK_LABEL(wl->cmpEccType), _("Augmented image"));
   }

   /* Error correction method */

   memcpy(method, eh->method, 4); method[4] = 0;

   PrintLog(_("- method           : %4s, %d roots, %4.1f%% redundancy.\n"),
	    method, eh->eccBytes, 
	    ((double)eh->eccBytes*100.0)/(double)eh->dataBytes);

   if(Closure->guiMode)
     SetLabelText(GTK_LABEL(wl->cmpEccMethod), _("%4s, %d roots, %4.1f%% redundancy"),
		  method, eh->eccBytes, 
		  ((double)eh->eccBytes*100.0)/(double)eh->dataBytes);

   /* Creator version */

   major = eh->creatorVersion/10000; 
   minor = (eh->creatorVersion%10000)/100;
   pl    = eh->creatorVersion%100;

   if(eh->creatorVersion%100)        
   {  char *format, *color_format = NULL;

      if(eh->methodFlags[3] & MFLAG_DEVEL) 
      {  format = "%s-%d.%d (devel-%d)";
	 color_format = "%s-%d.%d <span %s>(devel-%d)</span>";
      }
      else if(eh->methodFlags[3] & MFLAG_RC) 
      {  format = "%s-%d.%d (rc-%d)";
	 color_format = "%s-%d.%d <span %s>(rc-%d)</span>";
      }
      else format = "%s-%d.%d (pl%d)";

      PrintLog(format, _("- created by       : dvdisaster"), major, minor, pl);
      PrintLog("\n");

      if(!color_format) color_format = format;
      if(Closure->guiMode)
      {  if(!color_format)
	      SetLabelText(GTK_LABEL(wl->cmpEccCreatedBy), color_format, 
			   "dvdisaster", major, minor, Closure->redMarkup, pl);
	 else SetLabelText(GTK_LABEL(wl->cmpEccCreatedBy), format, 
			   "dvdisaster", major, minor, pl);
      }
   }
   else
   {  PrintLog(_("- created by       : dvdisaster-%d.%d\n"), 
	       major, minor);

      if(Closure->guiMode)
	SetLabelText(GTK_LABEL(wl->cmpEccCreatedBy), "dvdisaster-%d.%d", major, minor);
   }

   /* Required dvdisaster version */

   if(eh->neededVersion%100)        
        version = g_strdup_printf("%d.%d (pl%d)",
				  eh->neededVersion/10000,
				  (eh->neededVersion%10000)/100,
				  eh->neededVersion%100);
   else version = g_strdup_printf("%d.%d",
				  eh->neededVersion/10000,
				  (eh->neededVersion%10000)/100);

   if(Closure->version >= eh->neededVersion)
   {  PrintLog(_("- requires         : dvdisaster-%s\n"), version);

      if(Closure->guiMode)
	SetLabelText(GTK_LABEL(wl->cmpEccRequires), "dvdisaster-%s", version);
   }
   else 
   {  PrintLog(_("* requires         : dvdisaster-%s (BAD)\n"
		 "* Warning          : The following output might be incorrect.\n"
		 "*                  : Please visit http://www.dvdisaster.org for an upgrade.\n"),
	       version);

     if(Closure->guiMode)
     {  SetLabelText(GTK_LABEL(wl->cmpEccRequires), 
		     "<span %s>dvdisaster-%s</span>",
		     Closure->redMarkup, version);
        if(!ecc_advice) 
	   ecc_advice = g_strdup_printf(_("<span %s>Please upgrade your version of dvdisaster!</span>"), Closure->redMarkup);
     }

     exitCode = EXIT_CODE_VERSION_MISMATCH;
   }

   g_free(version);

   /* image md5sum as stored in the ecc header */

   if(eh->methodFlags[0] & MFLAG_DATA_MD5)
        AsciiDigest(hdr_digest, eh->mediumSum);
   else strcpy(hdr_digest, _("none available"));

   PrintLog(_("- data md5sum      : %s\n"),hdr_digest);

   if(Closure->guiMode)
     SetLabelText(GTK_LABEL(wl->cmpEccDataCrcVal), "%s", hdr_digest);

   /* compare images in ecc file case */

   if(eh->methodFlags[0] & MFLAG_ECC_FILE)
   {  if(image && image->file)
      {  if(image->fpState != FP_PRESENT)
         {  PrintLog(_("* fingerprint match: NOT POSSIBLE - related sector is missing in image!\n"));

	    if(Closure->guiMode)
	      SetLabelText(GTK_LABEL(wl->cmpEccFingerprint), _("<span %s>missing sector prevents calculation</span>"), Closure->redMarkup);
	}
	else
	{ 
	   if(memcmp(image->imageFP, eh->mediumFP, 16)) 
	   {  PrintLog(_("* fingerprint match: MISMATCH - .iso and .ecc don't belong together!\n"));

	      if(Closure->guiMode)
	      {  SetLabelText(GTK_LABEL(wl->cmpEccFingerprint), 
			      _("<span %s>mismatch</span>"), Closure->redMarkup);

		if(!ecc_advice)
		  ecc_advice = g_strdup_printf(_("<span %s>Image and error correction files do not belong together!</span>"), Closure->redMarkup);
	      }
	   }
	   else 
	   {  PrintLog(_("- fingerprint match: good\n"));
	      if(Closure->guiMode)
		SetLabelText(GTK_LABEL(wl->cmpEccFingerprint), _("good"));
	   }
	}
      }
   }
   
   /* print advice collected from above tests */

   if(Closure->guiMode)
   {  if(ecc_advice) 
      {  SetLabelText(GTK_LABEL(wl->cmpEccResult), ecc_advice);
         g_free(ecc_advice);
      }
      else SetLabelText(GTK_LABEL(wl->cmpEccResult),
			_("<span %s>Good error correction data.</span>"),
			Closure->greenMarkup);
   }

   if(!image->file)  /* Ecc file but no image */
      goto terminate;

   /*** Print information on image size */

   PrintLog(_("\nData integrity:\n"));

   /* Provide enough bitmap space for all layers */

   vc->map = CreateBitmap0(GF_FIELDMAX*lay->sectorsPerLayer);

   /* Expected and real sectors */

   if(lay->target == ECC_FILE)
   {  //expected_sectors = lay->dataSectors + lay->totalSectors;  /* image + ecc file */
      virtual_expected = GF_FIELDMAX*lay->sectorsPerLayer;      /* for prognosis map */
      expected_image_sectors = lay->dataSectors;                /* just the expected image size */
      if(eh->inLast != image->inLast)
	 matching_byte_size = FALSE;
   }
   else 
   {  virtual_expected = expected_image_sectors = lay->totalSectors;
      SetBit(vc->map, lay->eccHeaderPos);
      SetBit(vc->map, lay->eccHeaderPos+1);
   }

   /* Image size and expected size in ecc file match */
   
   if(expected_image_sectors == image->sectorSize && matching_byte_size)
   {  if(lay->target == ECC_FILE)
      {  if(Closure->guiMode)
	 {  if(image->inLast == 2048)
	      SetLabelText(GTK_LABEL(wl->cmpImageSectors), _("%lld in image; %lld in ecc file"), 
			   image->sectorSize, eccfile_sectors);
	    else
	      SetLabelText(GTK_LABEL(wl->cmpImageSectors), _("%lld sectors + %d bytes in image; %lld in ecc file"), 
			   image->sectorSize-1, image->inLast, eccfile_sectors);
	 }

	 if(image->inLast == 2048)
	      PrintLog(_("- sectors          : %lld in image; "), image->sectorSize);
	 else PrintLog(_("- sectors          : %lld sectors + %d bytes in image; "), image->sectorSize-1, image->inLast);

	 PrintLog(_("%lld in ecc file\n"), eccfile_sectors);
      }
      else 
      {  if(Closure->guiMode)
	   SetLabelText(GTK_LABEL(wl->cmpImageSectors), _("%lld total / %lld data"), 
			image->sectorSize, lay->dataSectors);
	 PrintLog(_("- medium sectors   : %lld total / %lld data\n"),
		  image->sectorSize, lay->dataSectors);
      }
   }
   else  /* Mismatch between image size and expected size in ecc file */
   {  char *image_size, *expected_size;

      if(image->inLast == 2048)
	image_size = g_strdup_printf("%lld", (long long int)image->sectorSize);
      else image_size = g_strdup_printf("%lld sectors + %d bytes", (long long int)image->sectorSize-1, image->inLast);

      if(eh->inLast == 2048)
           expected_size = g_strdup_printf("%lld", (long long int)expected_image_sectors);
      else expected_size = g_strdup_printf("%lld sectors + %d bytes", (long long int)expected_image_sectors-1, eh->inLast);

      if(Closure->guiMode)
      {  SetLabelText(GTK_LABEL(wl->cmpImageSectors), _("<span %s>%s (%s expected)</span>"), 
		      Closure->redMarkup, image_size, expected_size);

	 if(image->sectorSize == expected_image_sectors)
	 {  if(image->inLast < eh->inLast)
	         img_advice = g_strdup_printf(_("<span %s>Image file is %d bytes shorter than expected.</span>"),
					      Closure->redMarkup, eh->inLast-image->inLast);
	    else img_advice = g_strdup_printf(_("<span %s>Image file is %d bytes longer than expected.</span>"),
					      Closure->redMarkup, image->inLast-eh->inLast);
	 }

	 if(expected_image_sectors > image->sectorSize)
	    img_advice = g_strdup_printf(_("<span %s>Image file is %lld sectors shorter than expected.</span>"),
					 Closure->redMarkup, expected_image_sectors - image->sectorSize);
	 if(expected_image_sectors < image->sectorSize)
	    img_advice = g_strdup_printf(_("<span %s>Image file is %lld sectors longer than expected.</span>"),
					 Closure->redMarkup, image->sectorSize - expected_image_sectors);
      }

      if(lay->target == ECC_FILE)
	PrintLog(_("* sectors          : %s (%s expected); %lld sectors in ecc file\n"),
		 image_size, expected_size, eccfile_sectors);
      else
	PrintLog(_("* medium sectors   : %s (%s expected)\n"),
		 image_size, expected_size);
      g_free(image_size);
      g_free(expected_size);
   }
   
   if(Closure->quickVerify)
   {  PrintLog(_("* quick mode        : image NOT scanned\n"));
      goto terminate;
   }

   /*** Read the CRC portion */ 

   vc->crcBuf = self->getCrcBuf(vc->image);
   csc = (RS03CksumClosure*)self->ckSumClosure;

   /*** Check the data portion of the image file for the
	"dead sector marker" and CRC errors */
   
   if(!LargeSeek(image->file, 0))
     Stop(_("Failed seeking to start of image: %s\n"), strerror(errno));

   if(lay->target == ECC_FILE)
     if(!LargeSeek(image->eccFile, 4096))  /* skip the header */
       Stop(_("Failed seeking to start of ecc file: %s\n"), strerror(errno));

   MD5Init(&image_md5);

   first_missing = last_missing = -1;
   total_missing = data_missing = crc_missing = ecc_missing = 0;
   data_crc_errors = 0;
   crc_idx = 0;

   for(s=0; s<virtual_expected; s++)
   {  int percent,current_missing;
      int defective = 0;

      /* Check for user interruption */

      if(Closure->stopActions)   
      {  if(Closure->stopActions == STOP_CURRENT_ACTION) /* suppress memleak warning when closing window */
	    SetLabelText(GTK_LABEL(wl->cmpImageResult), 
			 _("<span %s>Aborted by user request!</span>"),
			 Closure->redMarkup); 
         goto terminate;
      }

      /* Read the next sector */

      RS03ReadSectors(image, vc->lay, buf,
		      s/vc->lay->sectorsPerLayer,
		      s%vc->lay->sectorsPerLayer,
		      1,
		      RS03_READ_DATA|RS03_READ_CRC|RS03_READ_ECC);

      /* update the MD5 sum */

      if(s < lay->dataSectors)
      {  if(s < lay->dataSectors - 1)
	      MD5Update(&image_md5, buf, 2048);
	 else MD5Update(&image_md5, buf, eh->inLast);
      }

      /* Look for the dead sector marker */

      current_missing = CheckForMissingSector(buf, s, eh->mediumFP, eh->fpSector);

      /* Truncated images and ecc files may create "legal" dead sectors. */

      if(current_missing != SECTOR_PRESENT)
      {  int dead_sector_from_truncation = 0;
	 guint64 real_sector = s;
	
	 if(lay->target == ECC_FILE)
	 {   if(s>=lay->dataSectors)
	     {  real_sector = s - (lay->ndata-1)*lay->sectorsPerLayer + 2;
	        if(real_sector*2048 >= image->eccFile->size)
		  dead_sector_from_truncation = 1;
	     }
	 }

	 if(!dead_sector_from_truncation)
	 {  int source_type = SOURCE_IMAGE;

	    if(lay->target == ECC_FILE && s>=lay->dataSectors)
	      source_type = SOURCE_ECCFILE;
	 
	    ExplainMissingSector(buf, real_sector, current_missing, source_type, &missing_sector_explained);
	 }
      }

      if(current_missing)
      {  
	 if(first_missing < 0) first_missing = s;
         last_missing = s;
	 total_missing++;
	 new_missing++;

	 if(lay->target == ECC_IMAGE)
	 {  if(s < lay->firstCrcPos) data_missing++;
	    else if(s >= lay->firstCrcPos && s < lay->firstEccPos) crc_missing++;
	    else ecc_missing++;
	 }
	 else  /* ecc file case */
	 {  if(s < lay->dataSectors) data_missing++;
	    else if(s < lay->ndata*lay->sectorsPerLayer) crc_missing++;
	    else ecc_missing++;
	 }
	 defective = TRUE;
	 exitCode = EXIT_CODE_MISSING_SECTOR;
      }

      /* Report dead sectors. Combine subsequent missing sectors into one report. */

      if(!current_missing || s==virtual_expected-1)
      {  if(first_missing>=0)
	{   gint64 first, last;
	    char *ecc_msg;
	
	    if(lay->target == ECC_FILE && last_missing >= (lay->ndata-1)*lay->sectorsPerLayer)
	    {    first = first_missing - (lay->ndata-1)*lay->sectorsPerLayer + 2;
	         last = last_missing - (lay->ndata-1)*lay->sectorsPerLayer + 2;
	         ecc_msg = g_strdup(_(" (in ecc file)"));
	    }
	    else 
	    {    first = first_missing;
	         last = last_missing;
		 ecc_msg = g_strdup(" ");
	    }
	    if(first_missing == last_missing)
	         PrintCLI(_("* missing sector   : %lld%s\n"), first,ecc_msg);
	    else PrintCLI(_("* missing sectors  : %lld - %lld%s\n"), first, last, ecc_msg);
	    first_missing = -1;
	    g_free(ecc_msg);
	 }
      }

      /* If the image sector is from the data portion and it was readable, 
	 test its CRC sum */

      if(   !current_missing
	 && (   (lay->target == ECC_IMAGE && s < lay->firstCrcPos)
	     || (lay->target == ECC_FILE && s < lay->dataSectors)))
      {  guint32 crc = Crc32(buf, 2048);

	 if(GetBit(vc->crcBuf->valid,crc_idx)
	    && crc != vc->crcBuf->crcbuf[crc_idx])
	 {  PrintCLI(_("* CRC error, sector: %lld\n"), s);
	    data_crc_errors++;
	    new_crc_errors++;
	    defective = TRUE;
	    exitCode = EXIT_CODE_CHECKSUM_ERROR;
	 }
      }
      crc_idx++;

      if(!defective)
	SetBit(vc->map, s);

      if(Closure->guiMode) 
      {   /* data part / spiral animation */
	  percent = (VERIFY_IMAGE_SEGMENTS*(s+1))/virtual_expected;

	  /* percentage is reset / output differently for ecc file part */
	  if(lay->target == ECC_FILE && s >= lay->dataSectors) 
	    percent = (100*(s+1-lay->dataSectors)/(virtual_expected-lay->dataSectors));
      }  
      else  percent = (100*(s+1))/virtual_expected;

      if(last_percent != percent) /* Update sector results */
      {  PrintProgress(_("- testing sectors  : %3d%%") ,percent);
	 if(Closure->guiMode)
	 {  if(lay->target == ECC_IMAGE)
	    {  add_verify_values(self, percent, new_missing, new_crc_errors); 
	    }
	    else /* do not include ecc file sectors in the spiral! */
	    {  if(s<lay->dataSectors)
	       {  int image_percent = (VERIFY_IMAGE_SEGMENTS*(s+1))/lay->dataSectors;
	          
	          add_verify_values(self, image_percent, new_missing, new_crc_errors); 
	       }
	       else
	       {  SetLabelText(GTK_LABEL(wl->cmpEccSyndromes),"%d%% tested",percent);
	       }
	    }

	    if(data_missing || data_crc_errors)
	      SetLabelText(GTK_LABEL(wl->cmpDataSection), 
			   _("<span %s>%lld sectors missing; %lld CRC errors</span>"),
			   Closure->redMarkup, data_missing, data_crc_errors);
	    if(crc_missing || csc->signatureErrors)
	      SetLabelText(GTK_LABEL(wl->cmpCrcSection), 
			   _("<span %s>%lld sectors missing; %lld signature errors</span>"),
			   Closure->redMarkup, crc_missing, csc->signatureErrors);
	    if(ecc_missing)
	      SetLabelText(GTK_LABEL(wl->cmpEccSection), 
			   _("<span %s>%lld sectors missing</span>"),
			   Closure->redMarkup, ecc_missing);
	 }
	 last_percent = percent;
	 new_missing = new_crc_errors = 0;
      }
      
      /* If we have processed the image and are about to switch over
	 to the ecc file, do some bookkeeping. */

      if(lay->target == ECC_FILE && s == lay->dataSectors-1)
      {	 
	if(Closure->guiMode)
	{  /* flush/complete spiral */
	   add_verify_values(self, VERIFY_IMAGE_SEGMENTS, new_missing, new_crc_errors); 

	   SetLabelText(GTK_LABEL(wl->cmpHeadline), "<big>%s</big>\n<i>%s</i>",
			_("Checking the image and error correction files."),
			_("- Checking ecc file -"));

	   SetLabelText(GTK_LABEL(wl->cmpEccSynLabel), _("Error correction file:"));
	   last_percent = 0; /* restart counting for ecc file */
	}
      }
   }

   /* Complete damage summary */

   if(Closure->guiMode)
   {  if(data_missing || data_crc_errors)
        SetLabelText(GTK_LABEL(wl->cmpDataSection), 
		     _("<span %s>%lld sectors missing; %lld CRC errors</span>"),
		     Closure->redMarkup, data_missing, data_crc_errors);
      if(crc_missing || csc->signatureErrors)
        SetLabelText(GTK_LABEL(wl->cmpCrcSection), 
		     _("<span %s>%lld sectors missing; %lld signature errors</span>"),
		     Closure->redMarkup, crc_missing, csc->signatureErrors);
      if(ecc_missing)
	SetLabelText(GTK_LABEL(wl->cmpEccSection), 
		     _("<span %s>%lld sectors missing</span>"),
		     Closure->redMarkup, ecc_missing);
   }

   /* The image md5sum is only useful if all blocks have been successfully read. */

   MD5Final(medium_sum, &image_md5);
   AsciiDigest(data_digest, medium_sum);

   /* Do a resume of our findings */ 

   if(!total_missing && !data_crc_errors && !csc->signatureErrors)
      PrintLog(_("- good image/file  : all sectors present\n"
		 "- data md5sum      : %s\n"),data_digest);
   else
   {  if(!data_crc_errors && !csc->signatureErrors)
         PrintLog(_("* BAD image/file   : %lld sectors missing\n"), total_missing);
      if(!total_missing)
	 PrintLog(_("* suspicious image : all sectors present, but %lld CRC errors\n"), 
		  data_crc_errors);
      if(total_missing && data_crc_errors)
	 PrintLog(_("* BAD image        : %lld sectors missing, %lld CRC errors\n"), 
		  total_missing, data_crc_errors);

      PrintLog(_("  ... data section   : %lld sectors missing; %lld CRC errors\n"), 
	       data_missing, data_crc_errors);
      if(!total_missing && !data_crc_errors && !csc->signatureErrors)
	PrintLog(_("  ... data md5sum    : %s\n"), data_digest); 

      if(csc->signatureErrors)
	 PrintLog(_("  ... crc section    : %lld sectors missing; %lld signature errors\n"), 
		  crc_missing, csc->signatureErrors);
      else
	 PrintLog(_("  ... crc section    : %lld sectors missing\n"), crc_missing);

      PrintLog(_("  ... ecc section    : %lld sectors missing\n"), ecc_missing);
   }

   if(Closure->guiMode)
   {  if(!data_missing && !data_crc_errors) 
                        SetLabelText(GTK_LABEL(wl->cmpDataSection), _("complete"));
      if(!crc_missing && !csc->signatureErrors)  
	                SetLabelText(GTK_LABEL(wl->cmpCrcSection), _("complete"));
      if(!ecc_missing)  SetLabelText(GTK_LABEL(wl->cmpEccSection), _("complete"));
     
      SetLabelText(GTK_LABEL(wl->cmpImageMd5Sum), "%s", data_missing ? "-" : data_digest);
   }

   /*** Test error syndromes */

   if(Closure->guiMode)
   {  SetLabelText(GTK_LABEL(wl->cmpEccSynLabel), _("Ecc block test:"));
      SetLabelText(GTK_LABEL(wl->cmpEccSyndromes), "");
   }
   if(0&&total_missing + data_crc_errors != 0)
   { if(Closure->guiMode) 
        SetLabelText(GTK_LABEL(wl->cmpEccSyndromes),
		     _("<span %s>Skipped; not useful on known defective image</span>"),
		     Closure->redMarkup);

     PrintLog(_("* Ecc block test   : skipped; not useful on defective image\n"));
   }
   else syn_error = check_syndromes(vc);

   /*** Print image advice */

   if(Closure->guiMode)
   {
      if(img_advice) 
      {  SetLabelText(GTK_LABEL(wl->cmpImageResult), img_advice);
         g_free(img_advice);
      }
      else 
      {  if(!total_missing && !data_crc_errors && !syn_error)
	    SetLabelText(GTK_LABEL(wl->cmpImageErasure),  /* avoid two blank lines */
			 _("<span %s>Good image.</span>"),
			 Closure->greenMarkup);
	 else
           SetLabelText(GTK_LABEL(wl->cmpImageResult),
			_("<span %s>Damaged image.</span>"),
			Closure->redMarkup);
      }
   }

   /*** Print final results */

   try_it = prognosis(vc, total_missing+data_crc_errors, lay->totalSectors); 

   if(Closure->guiMode)
   {  if(total_missing || data_crc_errors)
      {  if(try_it) SetLabelText(GTK_LABEL(wl->cmpImageResult),
				 _("<span %s>Full data recovery is likely.</span>"),
				 Closure->greenMarkup);
         else       SetLabelText(GTK_LABEL(wl->cmpImageResult),
				 _("<span %s>Full data recovery is NOT possible.</span>"),
				 Closure->redMarkup);
      }
   }

   /*** Close and clean up */

terminate:
   cleanup((gpointer)vc);
}