| 1 | #!/usr/bin/env python |
|---|
| 2 | # -*- coding: utf-8 -*- |
|---|
| 3 | |
|---|
| 4 | # Copyright (C) 2008 Adriano Monteiro Marques. |
|---|
| 5 | # |
|---|
| 6 | # Author: Bartosz SKOWRON <getxsick at gmail dot com> |
|---|
| 7 | # |
|---|
| 8 | # This library is free software; you can redistribute it and/or modify |
|---|
| 9 | # it under the terms of the GNU Lesser General Public License as published |
|---|
| 10 | # by the Free Software Foundation; either version 2.1 of the License, or |
|---|
| 11 | # (at your option) any later version. |
|---|
| 12 | # |
|---|
| 13 | # This library is distributed in the hope that it will be useful, but |
|---|
| 14 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
|---|
| 15 | # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
|---|
| 16 | # License for more details. |
|---|
| 17 | # |
|---|
| 18 | # You should have received a copy of the GNU Lesser General Public License |
|---|
| 19 | # along with this library; if not, write to the Free Software Foundation, |
|---|
| 20 | # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 21 | |
|---|
| 22 | """ |
|---|
| 23 | Functions related with parsing bits of numbers. |
|---|
| 24 | """ |
|---|
| 25 | |
|---|
| 26 | BYTE = 8 |
|---|
| 27 | |
|---|
| 28 | def split_number_into_chunks(number, chunk_size=BYTE): |
|---|
| 29 | """ |
|---|
| 30 | Split the big number into small chunks. |
|---|
| 31 | |
|---|
| 32 | @type number: C{int} |
|---|
| 33 | @param number: the number for splitting. |
|---|
| 34 | |
|---|
| 35 | @type piece_size: C{int} |
|---|
| 36 | @param piece_size: size of the each chunk (default: 8 bits) |
|---|
| 37 | |
|---|
| 38 | @rtype: C{list} |
|---|
| 39 | @return: list of chunks. |
|---|
| 40 | """ |
|---|
| 41 | |
|---|
| 42 | chunks = [] |
|---|
| 43 | mask = 2**chunk_size - 1 |
|---|
| 44 | while number: |
|---|
| 45 | chunks.append(number & mask) |
|---|
| 46 | number >>= chunk_size |
|---|
| 47 | chunks.reverse() |
|---|
| 48 | return chunks |
|---|
| 49 | |
|---|
| 50 | def get_bits(number, bits, offset=0, rev_offset=False): |
|---|
| 51 | """ |
|---|
| 52 | Return n bits from the number ragarding to offset. |
|---|
| 53 | |
|---|
| 54 | By default offset is from the left side. |
|---|
| 55 | It is recommended to use right side offset (works much faster). |
|---|
| 56 | |
|---|
| 57 | @type number: C{int} |
|---|
| 58 | @param number: the number which will be parsed. |
|---|
| 59 | |
|---|
| 60 | @type bits: C{int} |
|---|
| 61 | @param bits: number of bits for the return |
|---|
| 62 | |
|---|
| 63 | @type offset: C{int} |
|---|
| 64 | @param offset: offset of the bits (default: 0) |
|---|
| 65 | |
|---|
| 66 | @type rev_offset: C{bool} |
|---|
| 67 | @param rev_offset: a direction of the offset (default: from left to right). |
|---|
| 68 | |
|---|
| 69 | @rtype: C{int} |
|---|
| 70 | @return: grabbed n bits from the number |
|---|
| 71 | """ |
|---|
| 72 | |
|---|
| 73 | if not rev_offset: |
|---|
| 74 | length = 0 |
|---|
| 75 | while number >= 2**length: |
|---|
| 76 | length += 1 |
|---|
| 77 | offset = length - offset - bits |
|---|
| 78 | return (number & (2**bits-1 << offset)) >> offset |
|---|