endian.c 5.65 KB
Newer Older
1
/*  dvdisaster: Additional error correction for optical media.
2
 *  Copyright (C) 2004-2015 Carsten Gnoerlich.
3
 *
4 5 6 7 8 9
 *  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
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation, either version 3 of the License, or
12 13
 *  (at your option) any later version.
 *
14
 *  dvdisaster is distributed in the hope that it will be useful,
15 16 17 18 19
 *  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
20
 *  along with dvdisaster. If not, see <http://www.gnu.org/licenses/>.
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
 */

#include "dvdisaster.h"

/***
 *** Conversion between little and big endian words.
 *** Only suitable for non performance critical code.
 */

/* 
 * Swap bytes in a 32 bit word to convert between little and big endian.
 */

guint32 SwapBytes32(guint32 in)
{
  return
        ((in & 0xff000000) >> 24) 
      | ((in & 0x00ff0000) >>  8) 
      | ((in & 0x0000ff00) <<  8) 
      | ((in & 0x000000ff) << 24);
}

guint64 SwapBytes64(guint64 in)
{
  return
        ((in & 0xff00000000000000ull) >> 56)
      | ((in & 0x00ff000000000000ull) >> 40)
      | ((in & 0x0000ff0000000000ull) >> 24)
      | ((in & 0x000000ff00000000ull) >>  8)
      | ((in & 0x00000000ff000000ull) <<  8)
      | ((in & 0x0000000000ff0000ull) << 24)
      | ((in & 0x000000000000ff00ull) << 40)
      | ((in & 0x00000000000000ffull) << 56);
}

/***
 *** Convert the EccHeader structure between different endians.
 ***/

/*
 * A debugging function for printing the Ecc header.
 */

void print_hex(char *label, guint8 *values, int n)
{  PrintCLI(label);

   while(n--)
     PrintCLI("%02x ",*values++);

   PrintCLI("\n");
}

73
void PrintEccHeader(EccHeader *eh)
74 75
{  char buf[16]; 

76
  PrintCLI(_("\nContents of Ecc Header:\n\n"));
77 78 79 80 81 82 83 84 85 86

   strncpy(buf, (char*)eh->cookie, 12); buf[12] = 0;
   PrintCLI("cookie           %s\n",buf);
   strncpy(buf, (char*)eh->method, 4);  buf[4] = 0;
   PrintCLI("method           %s\n",buf);
   print_hex("methodFlags      ", (guint8*)eh->methodFlags, 4);
   print_hex("mediumFP         ", eh->mediumFP, 16);
   print_hex("mediumSum        ", eh->mediumSum, 16);
   print_hex("eccSum           ", eh->eccSum, 16);
   print_hex("sectors          ", eh->sectors, 8);
87 88 89 90 91 92 93 94 95 96 97
   PrintCLI("sectors (native)  %lld\n", uchar_to_gint64(eh->sectors));
   PrintCLI("dataBytes         %8x\n", eh->dataBytes);
   PrintCLI("eccBytes          %8x\n", eh->eccBytes);
   PrintCLI("creatorVersion    %8x\n", eh->creatorVersion);
   PrintCLI("neededVersion     %8x\n", eh->neededVersion);
   PrintCLI("fpSector          %8x\n", eh->fpSector);
   PrintCLI("selfCRC           %8x\n", eh->selfCRC);
   print_hex("crcSum            ", eh->crcSum, 16);
   PrintCLI("inLast            %8x\n", eh->inLast);
   PrintCLI("sectorsPerLayer   %lld\n", eh->sectorsPerLayer);
   PrintCLI("sectorsAddedByEcc %lld\n", eh->sectorsAddedByEcc);
98 99 100 101

   PrintCLI("\n");
}

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
void print_crc_block(CrcBlock *cb)
{  char buf[16]; 

   PrintCLI("\nContents of CrcBlock:\n\n");

   strncpy(buf, (char*)cb->cookie, 12); buf[12] = 0;
   PrintCLI("cookie           %s\n",buf);
   strncpy(buf, (char*)cb->method, 4);  buf[4] = 0;
   PrintCLI("method           %s\n",buf);
   print_hex("methodFlags      ", (guint8*)cb->methodFlags, 4);
   PrintCLI("creatorVersion   %8x\n", cb->creatorVersion);
   PrintCLI("neededVersion    %8x\n", cb->neededVersion);
   PrintCLI("fpSector         %8x\n", cb->fpSector);
   print_hex("mediumFP         ", cb->mediumFP, 16);
   print_hex("mediumSum        ", cb->mediumSum, 16);
   PrintCLI("dataSectors     %ll16x\n ",cb->dataSectors);
   PrintCLI("inLast           %8x\n", cb->inLast);
   PrintCLI("dataBytes        %8x\n", cb->dataBytes);
   PrintCLI("eccBytes         %8x\n", cb->eccBytes);
   PrintCLI("sectorsPerLayer  %lld\n", cb->sectorsPerLayer);
   PrintCLI("selfCRC          %8x\n", cb->selfCRC);

   PrintCLI("\n");
}
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
/*
 * This is the most annoying part of the endian conversions.
 */

//#define VERBOSE

void SwapEccHeaderBytes(EccHeader *eh)
{
#ifdef VERBOSE
  printf("before swap:\n");
  print_ecc_header(eh);
#endif

  eh->dataBytes = SwapBytes32(eh->dataBytes);
  eh->eccBytes = SwapBytes32(eh->eccBytes);
  eh->creatorVersion = SwapBytes32(eh->creatorVersion);
  eh->neededVersion = SwapBytes32(eh->neededVersion);
  eh->fpSector = SwapBytes32(eh->fpSector);
  eh->inLast = SwapBytes32(eh->inLast);
145 146
  eh->sectorsPerLayer = SwapBytes64(eh->sectorsPerLayer);
  eh->sectorsAddedByEcc = SwapBytes64(eh->sectorsAddedByEcc);
147 148 149 150 151 152
#ifdef VERBOSE
  printf("after swap:\n");
  print_ecc_header(eh);
#endif
}

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
void SwapCrcBlockBytes(CrcBlock *cb)
{
#ifdef VERBOSE
  printf("before swap:\n");
  print_crc_block(cb);
#endif

  cb->creatorVersion = SwapBytes32(cb->creatorVersion);
  cb->neededVersion = SwapBytes32(cb->neededVersion);
  cb->fpSector = SwapBytes32(cb->fpSector);
  cb->dataSectors = SwapBytes64(cb->dataSectors);
  cb->inLast = SwapBytes32(cb->inLast);
  cb->dataBytes = SwapBytes32(cb->dataBytes);
  cb->eccBytes = SwapBytes32(cb->eccBytes);
  cb->sectorsPerLayer = SwapBytes64(cb->sectorsPerLayer);
#ifdef VERBOSE
  printf("after swap:\n");
  print_crc_block(cb);
#endif
}

174 175 176 177 178 179 180 181
void SwapDefectiveHeaderBytes(DefectiveSectorHeader *dsh)
{  
  dsh->lba        = SwapBytes64(dsh->lba);
  dsh->sectorSize = SwapBytes32(dsh->sectorSize);
  dsh->properties = SwapBytes32(dsh->properties);
  dsh->dshFormat  = SwapBytes32(dsh->dshFormat);
  dsh->nSectors   = SwapBytes32(dsh->nSectors);
}