1 /*
2  * Copyright 2019 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 "fields/checksum_field.h"
18 #include "util.h"
19 
20 const std::string ChecksumField::kFieldType = "ChecksumField";
21 
ChecksumField(std::string name,std::string type_name,int size,ParseLocation loc)22 ChecksumField::ChecksumField(std::string name, std::string type_name, int size, ParseLocation loc)
23     : ScalarField(name, size, loc), type_name_(type_name) {}
24 
GetFieldType() const25 const std::string& ChecksumField::GetFieldType() const {
26   return ChecksumField::kFieldType;
27 }
28 
GetDataType() const29 std::string ChecksumField::GetDataType() const {
30   return type_name_;
31 }
32 
GenExtractor(std::ostream &,int,bool) const33 void ChecksumField::GenExtractor(std::ostream&, int, bool) const {}
34 
GetGetterFunctionName() const35 std::string ChecksumField::GetGetterFunctionName() const {
36   return "";
37 }
38 
GenGetter(std::ostream &,Size,Size) const39 void ChecksumField::GenGetter(std::ostream&, Size, Size) const {}
40 
GenBuilderParameter(std::ostream &) const41 bool ChecksumField::GenBuilderParameter(std::ostream&) const {
42   return false;
43 }
44 
HasParameterValidator() const45 bool ChecksumField::HasParameterValidator() const {
46   return false;
47 }
48 
GenParameterValidator(std::ostream &) const49 void ChecksumField::GenParameterValidator(std::ostream&) const {
50   // Do nothing.
51 }
52 
GenInserter(std::ostream & s) const53 void ChecksumField::GenInserter(std::ostream& s) const {
54   s << "packet::ByteObserver observer = i.UnregisterObserver();";
55   s << "insert(static_cast<" << util::GetTypeForSize(GetSize().bits()) << ">(observer.GetValue()), i);";
56 }
57 
GenValidator(std::ostream &) const58 void ChecksumField::GenValidator(std::ostream&) const {
59   // Done in packet_def.cc
60 }
61 
GenStringRepresentation(std::ostream & s,std::string) const62 void ChecksumField::GenStringRepresentation(std::ostream& s, std::string) const {
63   // TODO: there is currently no way to get checksum value
64   s << "\"CHECKSUM\"";
65 }
66