def decode_terminated(data, encoding, strict=True):
 codec_info = codecs.lookup(encoding)
 encoding = codec_info.name
 if (encoding in ('utf-8', 'iso8859-1')):
  index = data.find('\x00')
  if (index == (-1)):
   res = (data.decode(encoding), '')
   if strict:
    raise ValueError('not null terminated')
   else:
    return res
  return (data[:index].decode(encoding), data[(index + 1):])
 decoder = codec_info.incrementaldecoder()
 r = []
 for (i, b) in enumerate(iterbytes(data)):
  c = decoder.decode(b)
  if (c == u'\x00'):
   return (u''.join(r), data[(i + 1):])
  r.append(c)
 else:
  r.append(decoder.decode('', True))
  if strict:
   raise ValueError('not null terminated')
  return (u''.join(r), '')

def decode_terminated ( data , encoding , strict = True ) : codec_info = codecs . lookup ( encoding ) encoding = codec_info . name if ( encoding in ( 'utf-8' , 'iso8859-1' ) ) : index = data . find ( '\x00' ) if ( index == ( - 1 ) ) : res = ( data . decode ( encoding ) , '' ) if strict : raise ValueError ( 'not null terminated' ) else : return res return ( data [ : index ] . decode ( encoding ) , data [ ( index + 1 ) : ] ) decoder = codec_info . incrementaldecoder ( ) r = [ ] for ( i , b ) in enumerate ( iterbytes ( data ) ) : c = decoder . decode ( b ) if ( c == u'\x00' ) : return ( u'' . join ( r ) , data [ ( i + 1 ) : ] ) r . append ( c ) else : r . append ( decoder . decode ( '' , True ) ) if strict : raise ValueError ( 'not null terminated' ) return ( u'' . join ( r ) , '' )
returns the decoded data until the first null terminator and all data after it .