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 
19 #include "util.h"
20 
21 const std::string ChecksumField::kFieldType = "ChecksumField";
22 
ChecksumField(std::string name,std::string type_name,int size,ParseLocation loc)23 ChecksumField::ChecksumField(std::string name, std::string type_name, int size, ParseLocation loc)
24     : ScalarField(name, size, loc), type_name_(type_name) {}
25 
GetFieldType() const26 const std::string& ChecksumField::GetFieldType() const { return ChecksumField::kFieldType; }
27 
GetDataType() const28 std::string ChecksumField::GetDataType() const { return type_name_; }
29 
GenExtractor(std::ostream &,int,bool) const30 void ChecksumField::GenExtractor(std::ostream&, int, bool) const {}
31 
GetGetterFunctionName() const32 std::string ChecksumField::GetGetterFunctionName() const { return ""; }
33 
GenGetter(std::ostream &,Size,Size) const34 void ChecksumField::GenGetter(std::ostream&, Size, Size) const {}
35 
GenBuilderParameter(std::ostream &) const36 bool ChecksumField::GenBuilderParameter(std::ostream&) const { return false; }
37 
HasParameterValidator() const38 bool ChecksumField::HasParameterValidator() const { return false; }
39 
GenParameterValidator(std::ostream &) const40 void ChecksumField::GenParameterValidator(std::ostream&) const {
41   // Do nothing.
42 }
43 
GenInserter(std::ostream & s) const44 void ChecksumField::GenInserter(std::ostream& s) const {
45   s << "packet::ByteObserver observer = i.UnregisterObserver();";
46   s << "insert(static_cast<" << util::GetTypeForSize(GetSize().bits())
47     << ">(observer.GetValue()), i);";
48 }
49 
GenValidator(std::ostream &) const50 void ChecksumField::GenValidator(std::ostream&) const {
51   // Done in packet_def.cc
52 }
53 
GenStringRepresentation(std::ostream & s,std::string) const54 void ChecksumField::GenStringRepresentation(std::ostream& s, std::string) const {
55   // TODO: there is currently no way to get checksum value
56   s << "\"CHECKSUM\"";
57 }
58