Back to index

eyed3  0.6.18
binfuncs.py
Go to the documentation of this file.
00001 ################################################################################
00002 #
00003 #  Copyright (C) 2002-2005  Travis Shirk <travis@pobox.com>
00004 #  Copyright (C) 2001  Ryan Finne <ryan@finnie.org>
00005 #
00006 #  This program is free software; you can redistribute it and/or modify
00007 #  it under the terms of the GNU General Public License as published by
00008 #  the Free Software Foundation; either version 2 of the License, or
00009 #  (at your option) any later version.
00010 #
00011 #  This program is distributed in the hope that it will be useful,
00012 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 #  GNU General Public License for more details.
00015 #
00016 #  You should have received a copy of the GNU General Public License
00017 #  along with this program; if not, write to the Free Software
00018 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 #
00020 ################################################################################
00021 
00022 # Accepts a string of bytes (chars) and returns an array of bits
00023 # representing the bytes in big endian byte (Most significant byte/bit first)
00024 # order.  Each byte can have it's higher bits ignored by passing an sz arg.
00025 def bytes2bin(bytes, sz = 8):
00026    if sz < 1 or sz > 8:
00027       raise ValueError("Invalid sz value: " + str(sz));
00028 
00029    retVal = [];
00030    for b in bytes:
00031       bits = [];
00032       b = ord(b);
00033       while b > 0:
00034          bits.append(b & 1);
00035          b >>= 1;
00036 
00037       if len(bits) < sz:
00038          bits.extend([0] * (sz - len(bits)));
00039       elif len(bits) > sz:
00040          bits = bits[:sz];
00041 
00042       # Big endian byte order.
00043       bits.reverse();
00044       retVal.extend(bits);
00045 
00046    if len(retVal) == 0:
00047       retVal = [0];
00048    return retVal;
00049 
00050 # Convert am array of bits (MSB first) into a string of characters.
00051 def bin2bytes(x):
00052    bits = [];
00053    bits.extend(x);
00054    bits.reverse();
00055 
00056    i = 0;
00057    out = '';
00058    multi = 1;
00059    ttl = 0;
00060    for b in bits:
00061       i += 1;
00062       ttl += b * multi;
00063       multi *= 2;
00064       if i == 8:
00065          i = 0;
00066          out += chr(ttl);
00067          multi = 1;
00068          ttl = 0;
00069 
00070    if multi > 1:
00071       out += chr(ttl);
00072 
00073    out = list(out);
00074    out.reverse();
00075    out = ''.join(out);
00076    return out;
00077 
00078 # Convert and array of "bits" (MSB first) to it's decimal value.
00079 def bin2dec(x):
00080    bits = [];
00081    bits.extend(x);
00082    bits.reverse();
00083 
00084    multi = 1;
00085    value = long(0);
00086    for b in bits:
00087       value += b * multi;
00088       multi *= 2;
00089    return value;
00090 
00091 def bytes2dec(bytes, sz = 8):
00092     return bin2dec(bytes2bin(bytes, sz));
00093 
00094 # Convert a decimal value to an array of bits (MSB first), optionally
00095 # padding the overall size to p bits.
00096 def dec2bin(n, p = 0):
00097    assert(n >= 0)
00098    retVal = [];
00099 
00100    while n > 0:
00101       retVal.append(n & 1);
00102       n >>= 1;
00103 
00104    if p > 0:
00105       retVal.extend([0] * (p - len(retVal)));
00106    retVal.reverse();
00107    return retVal;
00108 
00109 def dec2bytes(n, p = 0):
00110     return bin2bytes(dec2bin(n, p));
00111 
00112 # Convert a list of bits (MSB first) to a synch safe list of bits (section 6.2
00113 # of the ID3 2.4 spec).
00114 def bin2synchsafe(x):
00115    if len(x) > 32 or bin2dec(x) > 268435456:   # 2^28
00116       raise ValueError("Invalid value");
00117    elif len(x) < 8:
00118       return x;
00119 
00120    n = bin2dec(x);
00121    bites = "";
00122    bites += chr((n >> 21) & 0x7f);
00123    bites += chr((n >> 14) & 0x7f);
00124    bites += chr((n >>  7) & 0x7f);
00125    bites += chr((n >>  0) & 0x7f);
00126    bits = bytes2bin(bites);
00127    if len(bits) < 32:
00128       bits = ([0] * (32 - len(x))) + bits;
00129 
00130    return bits;
00131 
00132 def bytes2str(bytes):
00133     s = ""
00134     for b in bytes:
00135         s += ("\\x%02x" % ord(b))
00136     return s