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 "ChecksumCalculator.h"
18 
19 #include <string>
20 #include <vector>
21 
22 #include <assert.h>
23 #include <string.h>
24 
25 // Checklist when implementing new protocol:
26 // 1. update CHECKSUMHELPER_MAX_VERSION
27 // 2. update checksumByteSize()
28 // 3. 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     m_checksumSize = checksumByteSize(version);
64     LOG_CHECKSUMHELPER("%s: ChecksumCalculator Set Version %d\n", __FUNCTION__,
65                 m_version);
66     return true;
67 }
68 
addBuffer(const void * buf,size_t packetLen)69 void ChecksumCalculator::addBuffer(const void* buf, size_t packetLen) {
70     m_isEncodingChecksum = true;
71     switch (m_version) {
72         case 1:
73             m_v1BufferTotalLength += packetLen;
74             break;
75     }
76 }
77 
writeChecksum(void * outputChecksum,size_t outputChecksumLen)78 bool ChecksumCalculator::writeChecksum(void* outputChecksum, size_t outputChecksumLen) {
79     if (outputChecksumLen < checksumByteSize()) return false;
80     char *checksumPtr = (char *)outputChecksum;
81     switch (m_version) {
82         case 1: { // protocol v1 is to reverse the packetLen and write it at the end
83             uint32_t val = computeV1Checksum();
84             memcpy(checksumPtr, &val, sizeof(val));
85             memcpy(checksumPtr+sizeof(val), &m_numWrite, sizeof(m_numWrite));
86             break;
87         }
88     }
89     resetChecksum();
90     m_numWrite++;
91     return true;
92 }
93 
resetChecksum()94 void ChecksumCalculator::resetChecksum() {
95     switch (m_version) {
96         case 1:
97             m_v1BufferTotalLength = 0;
98             break;
99     }
100     m_isEncodingChecksum = false;
101 }
102 
validate(const void * expectedChecksum,size_t expectedChecksumLen)103 bool ChecksumCalculator::validate(const void* expectedChecksum,
104                                   size_t expectedChecksumLen) {
105     const size_t checksumSize = checksumByteSize();
106     if (expectedChecksumLen != checksumSize) {
107         m_numRead++;
108         resetChecksum();
109         return false;
110     }
111     bool isValid;
112     switch (m_version) {
113         case 1: {
114             const uint32_t val = computeV1Checksum();
115             assert(checksumSize == sizeof(val) + sizeof(m_numRead));
116             isValid = 0 == memcmp(&val, expectedChecksum, sizeof(val)) &&
117                       0 == memcmp(&m_numRead,
118                                   static_cast<const char*>(expectedChecksum) +
119                                           sizeof(val),
120                                   sizeof(m_numRead));
121             break;
122         }
123         default:
124             isValid = true;  // No checksum is a valid checksum.
125             break;
126     }
127     m_numRead++;
128     resetChecksum();
129     return isValid;
130 }
131 
computeV1Checksum() const132 uint32_t ChecksumCalculator::computeV1Checksum() const {
133     uint32_t revLen = m_v1BufferTotalLength;
134     revLen = (revLen & 0xffff0000) >> 16 | (revLen & 0x0000ffff) << 16;
135     revLen = (revLen & 0xff00ff00) >> 8 | (revLen & 0x00ff00ff) << 8;
136     revLen = (revLen & 0xf0f0f0f0) >> 4 | (revLen & 0x0f0f0f0f) << 4;
137     revLen = (revLen & 0xcccccccc) >> 2 | (revLen & 0x33333333) << 2;
138     revLen = (revLen & 0xaaaaaaaa) >> 1 | (revLen & 0x55555555) << 1;
139     return revLen;
140 }
141