summaryrefslogtreecommitdiff
path: root/utility/hamming.c
blob: b3c3220a02bc3d3fdc0de606f18381e55b32ec92 (plain)
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
/* ----------------------------------------------------------------------------
 *         ATMEL Microcontroller Software Support 
 * ----------------------------------------------------------------------------
 * Copyright (c) 2008, Atmel Corporation
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the disclaimer below.
 *
 * Atmel's name may not be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ----------------------------------------------------------------------------
 */

//------------------------------------------------------------------------------
//         Headers
//------------------------------------------------------------------------------

#include "hamming.h"
#include <utility/trace.h>
#include <utility/assert.h>

//------------------------------------------------------------------------------
//         Internal function
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
/// Counts and return the number of bits set to '1' in the given byte.
/// \param byte  Byte to count.
//------------------------------------------------------------------------------
static unsigned char CountBitsInByte(unsigned char byte)
{
    unsigned char count = 0;
    while (byte > 0) {

        if (byte & 1) {

            count++;
        }
        byte >>= 1;
    }

    return count;
}

//------------------------------------------------------------------------------
/// Counts and return the number of bits set to '1' in the given hamming code.
/// \param code  Hamming code.
//------------------------------------------------------------------------------
static unsigned char CountBitsInCode256(unsigned char *code)
{
    return CountBitsInByte(code[0])
           + CountBitsInByte(code[1])
           + CountBitsInByte(code[2]);
}

//------------------------------------------------------------------------------
/// Calculates the 22-bit hamming code for a 256-bytes block of data.
/// \param data  Data buffer to calculate code for.
/// \param code  Pointer to a buffer where the code should be stored.
//------------------------------------------------------------------------------
static void Compute256(const unsigned char *data, unsigned char *code)
{
    unsigned int i;
    unsigned char columnSum = 0;
    unsigned char evenLineCode = 0;
    unsigned char oddLineCode = 0;
    unsigned char evenColumnCode = 0;
    unsigned char oddColumnCode = 0;

    // Xor all bytes together to get the column sum;
    // At the same time, calculate the even and odd line codes
    for (i=0; i < 256; i++) {

        columnSum ^= data[i];

        // If the xor sum of the byte is 0, then this byte has no incidence on
        // the computed code; so check if the sum is 1.
        if ((CountBitsInByte(data[i]) & 1) == 1) {

            // Parity groups are formed by forcing a particular index bit to 0
            // (even) or 1 (odd).
            // Example on one byte:
            // 
            // bits (dec)  7   6   5   4   3   2   1   0    
            //      (bin) 111 110 101 100 011 010 001 000    
            //                            '---'---'---'----------.
            //                                                   |
            // groups P4' ooooooooooooooo eeeeeeeeeeeeeee P4     |
            //        P2' ooooooo eeeeeee ooooooo eeeeeee P2     |
            //        P1' ooo eee ooo eee ooo eee ooo eee P1     |
            //                                                   |
            // We can see that:                                  |
            //  - P4  -> bit 2 of index is 0 --------------------'
            //  - P4' -> bit 2 of index is 1.
            //  - P2  -> bit 1 of index if 0.
            //  - etc...
            // We deduce that a bit position has an impact on all even Px if
            // the log2(x)nth bit of its index is 0
            //     ex: log2(4) = 2, bit2 of the index must be 0 (-> 0 1 2 3)
            // and on all odd Px' if the log2(x)nth bit of its index is 1
            //     ex: log2(2) = 1, bit1 of the index must be 1 (-> 0 1 4 5)
            // 
            // As such, we calculate all the possible Px and Px' values at the
            // same time in two variables, evenLineCode and oddLineCode, such as
            //     evenLineCode bits: P128  P64  P32  P16  P8  P4  P2  P1
            //     oddLineCode  bits: P128' P64' P32' P16' P8' P4' P2' P1'
            // 
            evenLineCode ^= (255 - i);
            oddLineCode ^= i;
        }
    }

    // At this point, we have the line parities, and the column sum. First, We
    // must caculate the parity group values on the column sum.
    for (i=0; i < 8; i++) {

        if (columnSum & 1) {

            evenColumnCode ^= (7 - i);
            oddColumnCode ^= i;
        }
        columnSum >>= 1;
    }

    // Now, we must interleave the parity values, to obtain the following layout:
    // Code[0] = Line1
    // Code[1] = Line2
    // Code[2] = Column
    // Line = Px' Px P(x-1)- P(x-1) ...
    // Column = P4' P4 P2' P2 P1' P1 PadBit PadBit 
    code[0] = 0;
    code[1] = 0;
    code[2] = 0;

    for (i=0; i < 4; i++) {

        code[0] <<= 2;
        code[1] <<= 2;
        code[2] <<= 2;

        // Line 1
        if ((oddLineCode & 0x80) != 0) {

            code[0] |= 2;
        }
        if ((evenLineCode & 0x80) != 0) {

            code[0] |= 1;
        }

        // Line 2
        if ((oddLineCode & 0x08) != 0) {

            code[1] |= 2;
        }
        if ((evenLineCode & 0x08) != 0) {

            code[1] |= 1;
        }

        // Column
        if ((oddColumnCode & 0x04) != 0) {

            code[2] |= 2;
        }
        if ((evenColumnCode & 0x04) != 0) {

            code[2] |= 1;
        }

        oddLineCode <<= 1;
        evenLineCode <<= 1;
        oddColumnCode <<= 1;
        evenColumnCode <<= 1;
    }

    // Invert codes (linux compatibility)
    code[0] = ~code[0];
    code[1] = ~code[1];
    code[2] = ~code[2];

    TRACE_DEBUG("Computed code = %02X %02X %02X\n\r",
              code[0], code[1], code[2]);
}

//------------------------------------------------------------------------------
/// Verifies and corrects a 256-bytes block of data using the given 22-bits
/// hamming code.
/// Returns 0 if there is no error, otherwise returns a HAMMING_ERROR code.
/// \param data  Data buffer to check.
/// \param originalCode  Hamming code to use for verifying the data.
//------------------------------------------------------------------------------
static unsigned char Verify256(
    unsigned char *data,
    const unsigned char *originalCode)
{
    // Calculate new code
    unsigned char computedCode[3];
    unsigned char correctionCode[3];
    Compute256(data, computedCode);

    // Xor both codes together
    correctionCode[0] = computedCode[0] ^ originalCode[0];
    correctionCode[1] = computedCode[1] ^ originalCode[1];
    correctionCode[2] = computedCode[2] ^ originalCode[2];

    TRACE_DEBUG("Correction code = %02X %02X %02X\n\r",
              correctionCode[0], correctionCode[1], correctionCode[2]);

    // If all bytes are 0, there is no error
    if ((correctionCode[0] == 0)
        && (correctionCode[1] == 0)
        && (correctionCode[2] == 0)) {

        return 0;
    }
    // If there is a single bit error, there are 11 bits set to 1
    if (CountBitsInCode256(correctionCode) == 11) {

        // Get byte and bit indexes
        unsigned char byte = correctionCode[0] & 0x80;
        byte |= (correctionCode[0] << 1) & 0x40;
        byte |= (correctionCode[0] << 2) & 0x20;
        byte |= (correctionCode[0] << 3) & 0x10;

        byte |= (correctionCode[1] >> 4) & 0x08;
        byte |= (correctionCode[1] >> 3) & 0x04;
        byte |= (correctionCode[1] >> 2) & 0x02;
        byte |= (correctionCode[1] >> 1) & 0x01;

        unsigned char bit = (correctionCode[2] >> 5) & 0x04;
        bit |= (correctionCode[2] >> 4) & 0x02;
        bit |= (correctionCode[2] >> 3) & 0x01;

        // Correct bit
        TRACE_DEBUG("Correcting byte #%d at bit %d\n\r", byte, bit);
        data[byte] ^= (1 << bit);

        return Hamming_ERROR_SINGLEBIT;
    }
    // Check if ECC has been corrupted
    if (CountBitsInCode256(correctionCode) == 1) {

        return Hamming_ERROR_ECC;
    }
    // Otherwise, this is a multi-bit error
    else {

        return Hamming_ERROR_MULTIPLEBITS;
    }
}

//------------------------------------------------------------------------------
//         Exported functions
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
/// Computes 3-bytes hamming codes for a data block whose size is multiple of
/// 256 bytes. Each 256 bytes block gets its own code.
/// \param data  Data to compute code for.
/// \param size  Data size in bytes.
/// \param code  Codes buffer.
//------------------------------------------------------------------------------
void Hamming_Compute256x(
    const unsigned char *data,
    unsigned int size,
    unsigned char *code)
{
    TRACE_DEBUG("Hamming_Compute256x()\n\r");

    while (size > 0) {

        Compute256(data, code);
        data += 256;
        code += 3;
        size -= 256;
    }
}

//------------------------------------------------------------------------------
/// Verifies 3-bytes hamming codes for a data block whose size is multiple of
/// 256 bytes. Each 256-bytes block is verified with its own code.
/// Returns 0 if the data is correct, Hamming_ERROR_SINGLEBIT if one or more
/// block(s) have had a single bit corrected, or either Hamming_ERROR_ECC
/// or Hamming_ERROR_MULTIPLEBITS.
/// \param data  Data buffer to verify.
/// \param size  Size of the data in bytes.
/// \param code  Original codes.
//------------------------------------------------------------------------------
unsigned char Hamming_Verify256x(
    unsigned char *data,
    unsigned int size,
    const unsigned char *code)
{
    unsigned char error;
    unsigned char result = 0;

    TRACE_DEBUG("Hamming_Verify256x()\n\r");

    while (size > 0) {

        error = Verify256(data, code);
        if (error == Hamming_ERROR_SINGLEBIT) {

            result = Hamming_ERROR_SINGLEBIT;
        }
        else if (error) {

            return error;
        }

        data += 256;
        code += 3;
        size -= 256;
    }

    return result;
}

personal git repositories of Harald Welte. Your mileage may vary