• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 
17 #include "gfxstream/guest/ChecksumCalculator.h"
18 
19 #include <string.h>
20 
21 namespace gfxstream {
22 namespace guest {
23 
24 // Checklist when implementing new protocol:
25 // 1. update CHECKSUMHELPER_MAX_VERSION
26 // 2. update ChecksumCalculator::Sizes enum
27 // 3. update checksumByteSize()
28 // 4. update addBuffer, writeChecksum, resetChecksum, validate
29 
30 // change CHECKSUMHELPER_MAX_VERSION when you want to update the protocol version
31 #define CHECKSUMHELPER_MAX_VERSION 1
32 
33 // utility macros to create checksum string at compilation time
34 #define CHECKSUMHELPER_VERSION_STR_PREFIX "ANDROID_EMU_CHECKSUM_HELPER_v"
35 #define CHECKSUMHELPER_MACRO_TO_STR(x) #x
36 #define CHECKSUMHELPER_MACRO_VAL_TO_STR(x) CHECKSUMHELPER_MACRO_TO_STR(x)
37 
38 static const uint32_t kMaxVersion = CHECKSUMHELPER_MAX_VERSION;
39 static const char* kMaxVersionStrPrefix = CHECKSUMHELPER_VERSION_STR_PREFIX;
40 static const char* kMaxVersionStr = CHECKSUMHELPER_VERSION_STR_PREFIX CHECKSUMHELPER_MACRO_VAL_TO_STR(CHECKSUMHELPER_MAX_VERSION);
41 
42 #undef CHECKSUMHELPER_MAX_VERSION
43 #undef CHECKSUMHELPER_VERSION_STR_PREFIX
44 #undef CHECKSUMHELPER_MACRO_TO_STR
45 #undef CHECKSUMHELPER_MACRO_VAL_TO_STR
46 
getMaxVersion()47 uint32_t ChecksumCalculator::getMaxVersion() {return kMaxVersion;}
getMaxVersionStr()48 const char* ChecksumCalculator::getMaxVersionStr() {return kMaxVersionStr;}
getMaxVersionStrPrefix()49 const char* ChecksumCalculator::getMaxVersionStrPrefix() {return kMaxVersionStrPrefix;}
50 
setVersion(uint32_t version)51 bool ChecksumCalculator::setVersion(uint32_t version) {
52     if (version > kMaxVersion) {  // unsupported version
53         LOG_CHECKSUMHELPER("%s: ChecksumCalculator Set Unsupported version Version %d\n",
54                 __FUNCTION__, m_version);
55         return false;
56     }
57     if (m_isEncodingChecksum) { // setVersion is called in the middle of encoding checksums
58         LOG_CHECKSUMHELPER("%s: called between addBuffer and writeChecksum\n",
59                 __FUNCTION__);
60         return false;
61     }
62     m_version = version;
63     LOG_CHECKSUMHELPER("%s: ChecksumCalculator Set Version %d\n", __FUNCTION__,
64                 m_version);
65     return true;
66 }
67 
checksumByteSize() const68 size_t ChecksumCalculator::checksumByteSize() const {
69     switch (m_version) {
70         case 0:
71             return 0;
72         case 1:
73             return sizeof(uint32_t) + sizeof(m_numWrite);
74         default:
75             return 0;
76     }
77 }
78 
ChecksumCalculator()79 ChecksumCalculator::ChecksumCalculator()
80                 : m_version(0)
81                 , m_numRead(0)
82                 , m_numWrite(0)
83                 , m_isEncodingChecksum(false)
84                 , m_v1BufferTotalLength(0)
85 {
86 }
87 
addBuffer(const void *,size_t packetLen)88 void ChecksumCalculator::addBuffer(const void*, size_t packetLen) {
89     m_isEncodingChecksum = true;
90     switch (m_version) {
91         case 1:
92             m_v1BufferTotalLength += packetLen;
93             break;
94     }
95 }
96 
writeChecksum(void * outputChecksum,size_t outputChecksumLen)97 bool ChecksumCalculator::writeChecksum(void* outputChecksum, size_t outputChecksumLen) {
98     if (outputChecksumLen < checksumByteSize()) return false;
99     char *checksumPtr = (char *)outputChecksum;
100     switch (m_version) {
101         case 1: { // protocol v1 is to reverse the packetLen and write it at the end
102             uint32_t val = computeV1Checksum();
103             memcpy(checksumPtr, &val, sizeof(val));
104             memcpy(checksumPtr+sizeof(val), &m_numWrite, sizeof(m_numWrite));
105             break;
106         }
107     }
108     resetChecksum();
109     m_numWrite++;
110     return true;
111 }
112 
resetChecksum()113 void ChecksumCalculator::resetChecksum() {
114     switch (m_version) {
115         case 1:
116             m_v1BufferTotalLength = 0;
117             break;
118     }
119     m_isEncodingChecksum = false;
120 }
121 
validate(const void * expectedChecksum,size_t expectedChecksumLen)122 bool ChecksumCalculator::validate(const void* expectedChecksum, size_t expectedChecksumLen) {
123     size_t checksumSize = checksumByteSize();
124     if (expectedChecksumLen != checksumSize) {
125         m_numRead++;
126         resetChecksum();
127         return false;
128     }
129     bool isValid;
130     switch (m_version) {
131         case 1: {
132             const uint32_t val = computeV1Checksum();
133             isValid = 0 == memcmp(&val, expectedChecksum, sizeof(val)) &&
134                       0 == memcmp(&m_numRead,
135                                   static_cast<const char*>(expectedChecksum) +
136                                           sizeof(val),
137                                   sizeof(m_numRead));
138 
139             break;
140         }
141         default:
142             isValid = true;  // No checksum is a valid checksum.
143             break;
144     }
145     m_numRead++;
146     resetChecksum();
147     return isValid;
148 }
149 
computeV1Checksum()150 uint32_t ChecksumCalculator::computeV1Checksum() {
151     uint32_t revLen = m_v1BufferTotalLength;
152     revLen = (revLen & 0xffff0000) >> 16 | (revLen & 0x0000ffff) << 16;
153     revLen = (revLen & 0xff00ff00) >> 8 | (revLen & 0x00ff00ff) << 8;
154     revLen = (revLen & 0xf0f0f0f0) >> 4 | (revLen & 0x0f0f0f0f) << 4;
155     revLen = (revLen & 0xcccccccc) >> 2 | (revLen & 0x33333333) << 2;
156     revLen = (revLen & 0xaaaaaaaa) >> 1 | (revLen & 0x55555555) << 1;
157     return revLen;
158 }
159 
160 }  // namespace guest
161 }  // namespace gfxstream