codekingpro/portable-devtools
115k
1# Copyright 2017 Google Inc. All rights reserved.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14 15from . import encode16from . import number_types17from . import packer18 19 20def GetSizePrefix(buf, offset):21 """Extract the size prefix from a buffer."""22 return encode.Get(packer.int32, buf, offset)23 24 25def GetBufferIdentifier(buf, offset, size_prefixed=False):26 """Extract the file_identifier from a buffer"""27 if size_prefixed:28 # increase offset by size of UOffsetTFlags29 offset += number_types.UOffsetTFlags.bytewidth30 # increase offset by size of root table pointer31 offset += number_types.UOffsetTFlags.bytewidth32 # end of FILE_IDENTIFIER33 end = offset + encode.FILE_IDENTIFIER_LENGTH34 return buf[offset:end]35 36 37def BufferHasIdentifier(buf, offset, file_identifier, size_prefixed=False):38 got = GetBufferIdentifier(buf, offset, size_prefixed=size_prefixed)39 return got == file_identifier40 41 42def RemoveSizePrefix(buf, offset):43 """Create a slice of a size-prefixed buffer that has44 45 its position advanced just past the size prefix.46 """47 return buf, offset + number_types.Int32Flags.bytewidth48 