1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 1993-2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 /******************************* DisplayPort*********************************\
25 *                                                                           *
26 * Module: dp_crc.cpp                                                        *
27 *    CRC Algorithms for the messaging subsystem.                            *
28 *                                                                           *
29 \***************************************************************************/
30 #include "dp_internal.h"
31 #include "dp_bitstream.h"
32 #include "dp_crc.h"
33 using namespace DisplayPort;
34 
35 //
36 //  DP CRC for transactions headers
37 //
dpCalculateHeaderCRC(BitStreamReader * reader)38 unsigned DisplayPort::dpCalculateHeaderCRC(BitStreamReader * reader)
39 {
40     unsigned remainder = 0;
41     unsigned bit, i;
42 
43     while (reader->read(&bit, 1))
44     {
45         remainder <<= 1;
46         remainder |= bit;
47         if ((remainder & 0x10) == 0x10)
48         {
49             remainder ^= 0x13;
50         }
51     }
52 
53     for (i = 4; i != 0; i--)
54     {
55         remainder <<= 1;
56         if ((remainder & 0x10) != 0)
57         {
58             remainder ^= 0x13;
59         }
60     }
61 
62     return remainder & 0xF;
63 }
64 
65 //
66 //  DP CRC for body
67 //
dpCalculateBodyCRC(BitStreamReader * reader)68 unsigned DisplayPort::dpCalculateBodyCRC(BitStreamReader * reader)
69 {
70     unsigned remainder = 0;
71     unsigned bit, i;
72 
73     while (reader->read(&bit, 1))
74     {
75         remainder <<= 1;
76         remainder |= bit;
77         if ((remainder & 0x100) == 0x100)
78         {
79             remainder ^= 0xD5;
80         }
81     }
82 
83     for (i = 8; i != 0; i--)
84     {
85         remainder <<= 1;
86         if ((remainder & 0x100) != 0)
87         {
88             remainder ^= 0xD5;
89         }
90     }
91 
92     return remainder & 0xFF;
93 }
94