|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# MinIO Python Library for Amazon S3 Compatible Cloud Storage, (C) |
| 3 | +# 2019 MinIO, Inc. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +""" |
| 18 | +minio.select.helpers |
| 19 | +~~~~~~~~~~~~~~~ |
| 20 | +
|
| 21 | +This module implements the helper functions for SelectObject responses. |
| 22 | +
|
| 23 | +:copyright: (c) 2019 by MinIO, Inc. |
| 24 | +:license: Apache 2.0, see LICENSE for more details. |
| 25 | +
|
| 26 | +""" |
| 27 | + |
| 28 | +import codecs |
| 29 | +from binascii import crc32 |
| 30 | + |
| 31 | +SQL = 'SQL' # Value for ExpressionType |
| 32 | +EVENT_RECORDS = 'Records' # Event Type is Records |
| 33 | +EVENT_PROGRESS = 'Progress' # Event Type Progress |
| 34 | +EVENT_STATS = 'Stats' # Event Type Stats |
| 35 | +EVENT_CONT = 'Cont' # Event Type continue |
| 36 | +EVENT_END = 'End' # Event Type is End |
| 37 | +EVENT_CONTENT_TYPE = "text/xml" # Event content xml type |
| 38 | +EVENT = 'event' # Message Type is event |
| 39 | +ERROR = 'error' # Message Type is error |
| 40 | + |
| 41 | +def calculate_crc(value): |
| 42 | + ''' |
| 43 | + Returns the CRC using crc32 |
| 44 | + ''' |
| 45 | + return crc32(value) & 0xffffffff |
| 46 | + |
| 47 | +def validate_crc(current_value, expected_value): |
| 48 | + ''' |
| 49 | + Validate through CRC check |
| 50 | + ''' |
| 51 | + crc_current = calculate_crc(current_value) |
| 52 | + crc_expected = byte_int(expected_value) |
| 53 | + if crc_current == crc_expected: |
| 54 | + return True |
| 55 | + return False |
| 56 | + |
| 57 | +def byte_int(data_bytes): |
| 58 | + ''' |
| 59 | + Convert bytes to big-endian integer |
| 60 | + ''' |
| 61 | + return int(codecs.encode(data_bytes, 'hex'), 16) |
0 commit comments