<s> """<STR_LIT>""" <EOL> from struct import unpack as _unpack , pack as _pack <EOL> from netaddr . compat import _bytes_join , _is_str <EOL> AF_INET = <NUM_LIT:2> <EOL> AF_INET6 = <NUM_LIT:10> <EOL> def inet_ntoa ( packed_ip ) : <EOL> """<STR_LIT>""" <EOL> if not _is_str ( packed_ip ) : <EOL> raise TypeError ( '<STR_LIT>' % str ( type ( packed_ip ) ) ) <EOL> if len ( packed_ip ) != <NUM_LIT:4> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return '<STR_LIT>' % _unpack ( '<STR_LIT>' , packed_ip ) <EOL> def _compact_ipv6_tokens ( tokens ) : <EOL> new_tokens = [ ] <EOL> positions = [ ] <EOL> start_index = None <EOL> num_tokens = <NUM_LIT:0> <EOL> for idx , token in enumerate ( tokens ) : <EOL> if token == '<STR_LIT:0>' : <EOL> if start_index is None : <EOL> start_index = idx <EOL> num_tokens += <NUM_LIT:1> <EOL> else : <EOL> if num_tokens > <NUM_LIT:1> : <EOL> positions . append ( ( num_tokens , start_index ) ) <EOL> start_index = None <EOL> num_tokens = <NUM_LIT:0> <EOL> new_tokens . append ( token ) <EOL> if num_tokens > <NUM_LIT:1> : <EOL> positions . append ( ( num_tokens , start_index ) ) <EOL> if len ( positions ) != <NUM_LIT:0> : <EOL> positions . sort ( key = lambda x : x [ <NUM_LIT:1> ] ) <EOL> best_position = positions [ <NUM_LIT:0> ] <EOL> for position in positions : <EOL> if position [ <NUM_LIT:0> ] > best_position [ <NUM_LIT:0> ] : <EOL> best_position = position <EOL> ( length , start_idx ) = best_position <EOL> new_tokens = new_tokens [ <NUM_LIT:0> : start_idx ] + [ '<STR_LIT>' ] + new_tokens [ start_idx + length : ] <EOL> if new_tokens [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> new_tokens . insert ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> if new_tokens [ - <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> new_tokens . append ( '<STR_LIT>' ) <EOL> return new_tokens <EOL> def inet_ntop ( af , packed_ip ) : <EOL> """<STR_LIT>""" <EOL> if af == AF_INET : <EOL> return inet_ntoa ( packed_ip ) <EOL> elif af == AF_INET6 : <EOL> if len ( packed_ip ) != <NUM_LIT:16> or not _is_str ( packed_ip ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> tokens = [ '<STR_LIT>' % i for i in _unpack ( '<STR_LIT>' , packed_ip ) ] <EOL> words = list ( _unpack ( '<STR_LIT>' , packed_ip ) ) <EOL> int_val = <NUM_LIT:0> <EOL> for i , num in enumerate ( reversed ( words ) ) : <EOL> word = num <EOL> word = word << <NUM_LIT:16> * i <EOL> int_val = int_val | word <EOL> if <NUM_LIT> < int_val <= <NUM_LIT> or int_val >> <NUM_LIT:32> == <NUM_LIT> : <EOL> packed_ipv4 = _pack ( '<STR_LIT>' , * [ int ( i , <NUM_LIT:16> ) for i in tokens [ - <NUM_LIT:2> : ] ] ) <EOL> ipv4_str = inet_ntoa ( packed_ipv4 ) <EOL> tokens = tokens [ <NUM_LIT:0> : - <NUM_LIT:2> ] + [ ipv4_str ] <EOL> return '<STR_LIT::>' . join ( _compact_ipv6_tokens ( tokens ) ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % af ) <EOL> def _inet_pton_af_inet ( ip_string ) : <EOL> """<STR_LIT>""" <EOL> if _is_str ( ip_string ) : <EOL> invalid_addr = ValueError ( '<STR_LIT>' % ip_string ) <EOL> tokens = ip_string . split ( '<STR_LIT:.>' ) <EOL> if len ( tokens ) == <NUM_LIT:4> : <EOL> words = [ ] <EOL> for token in tokens : <EOL> if token . startswith ( '<STR_LIT>' ) or ( token . startswith ( '<STR_LIT:0>' ) and len ( token ) > <NUM_LIT:1> ) : <EOL> raise invalid_addr <EOL> try : <EOL> octet = int ( token ) <EOL> except ValueError : <EOL> raise invalid_addr <EOL> if ( octet >> <NUM_LIT:8> ) != <NUM_LIT:0> : <EOL> raise invalid_addr <EOL> words . append ( _pack ( '<STR_LIT:B>' , octet ) ) <EOL> return _bytes_join ( words ) <EOL> else : <EOL> raise invalid_addr <EOL> raise ValueError ( '<STR_LIT>' % type ( ip_string ) ) <EOL> def inet_pton ( af , ip_string ) : <EOL> """<STR_LIT>""" <EOL> if af == AF_INET : <EOL> return _inet_pton_af_inet ( ip_string ) <EOL> elif af == AF_INET6 : <EOL> invalid_addr = ValueError ( '<STR_LIT>' % ip_string ) <EOL> values = [ ] <EOL> if not _is_str ( ip_string ) : <EOL> raise invalid_addr <EOL> if '<STR_LIT:x>' in ip_string : <EOL> raise invalid_addr <EOL> if '<STR_LIT>' in ip_string : <EOL> if ip_string == '<STR_LIT>' : <EOL> return '<STR_LIT:\x00>' . encode ( ) * <NUM_LIT:16> <EOL> try : <EOL> prefix , suffix = ip_string . split ( '<STR_LIT>' ) <EOL> except ValueError : <EOL> raise invalid_addr <EOL> l_prefix = [ ] <EOL> l_suffix = [ ] <EOL> if prefix != '<STR_LIT>' : <EOL> l_prefix = prefix . split ( '<STR_LIT::>' ) <EOL> if suffix != '<STR_LIT>' : <EOL> l_suffix = suffix . split ( '<STR_LIT::>' ) <EOL> if len ( l_suffix ) and '<STR_LIT:.>' in l_suffix [ - <NUM_LIT:1> ] : <EOL> ipv4_str = _inet_pton_af_inet ( l_suffix . pop ( ) ) <EOL> l_suffix . append ( '<STR_LIT>' % _unpack ( '<STR_LIT>' , ipv4_str [ <NUM_LIT:0> : <NUM_LIT:2> ] ) [ <NUM_LIT:0> ] ) <EOL> l_suffix . append ( '<STR_LIT>' % _unpack ( '<STR_LIT>' , ipv4_str [ <NUM_LIT:2> : <NUM_LIT:4> ] ) [ <NUM_LIT:0> ] ) <EOL> token_count = len ( l_prefix ) + len ( l_suffix ) <EOL> if not <NUM_LIT:0> <= token_count <= <NUM_LIT:8> - <NUM_LIT:1> : <EOL> raise invalid_addr <EOL> gap_size = <NUM_LIT:8> - ( len ( l_prefix ) + len ( l_suffix ) ) <EOL> values = ( <EOL> [ _pack ( '<STR_LIT>' , int ( i , <NUM_LIT:16> ) ) for i in l_prefix ] + <EOL> [ '<STR_LIT>' . encode ( ) for i in range ( gap_size ) ] + <EOL> [ _pack ( '<STR_LIT>' , int ( i , <NUM_LIT:16> ) ) for i in l_suffix ] <EOL> ) <EOL> try : <EOL> for token in l_prefix + l_suffix : <EOL> word = int ( token , <NUM_LIT:16> ) <EOL> if not <NUM_LIT:0> <= word <= <NUM_LIT> : <EOL> raise invalid_addr <EOL> except ValueError : <EOL> raise invalid_addr <EOL> else : <EOL> if '<STR_LIT::>' in ip_string : <EOL> tokens = ip_string . split ( '<STR_LIT::>' ) <EOL> if '<STR_LIT:.>' in ip_string : <EOL> ipv6_prefix = tokens [ : - <NUM_LIT:1> ] <EOL> if ipv6_prefix [ : - <NUM_LIT:1> ] != [ '<STR_LIT:0>' , '<STR_LIT:0>' , '<STR_LIT:0>' , '<STR_LIT:0>' , '<STR_LIT:0>' ] : <EOL> raise invalid_addr <EOL> if ipv6_prefix [ - <NUM_LIT:1> ] . lower ( ) not in ( '<STR_LIT:0>' , '<STR_LIT>' ) : <EOL> raise invalid_addr <EOL> if len ( tokens ) != <NUM_LIT:7> : <EOL> raise invalid_addr <EOL> ipv4_str = _inet_pton_af_inet ( tokens . pop ( ) ) <EOL> tokens . append ( '<STR_LIT>' % _unpack ( '<STR_LIT>' , ipv4_str [ <NUM_LIT:0> : <NUM_LIT:2> ] ) [ <NUM_LIT:0> ] ) <EOL> tokens . append ( '<STR_LIT>' % _unpack ( '<STR_LIT>' , ipv4_str [ <NUM_LIT:2> : <NUM_LIT:4> ] ) [ <NUM_LIT:0> ] ) <EOL> values = [ _pack ( '<STR_LIT>' , int ( i , <NUM_LIT:16> ) ) for i in tokens ] <EOL> else : <EOL> if len ( tokens ) != <NUM_LIT:8> : <EOL> raise invalid_addr <EOL> try : <EOL> tokens = [ int ( token , <NUM_LIT:16> ) for token in tokens ] <EOL> for token in tokens : <EOL> if not <NUM_LIT:0> <= token <= <NUM_LIT> : <EOL> raise invalid_addr <EOL> except ValueError : <EOL> raise invalid_addr <EOL> values = [ _pack ( '<STR_LIT>' , i ) for i in tokens ] <EOL> else : <EOL> raise invalid_addr <EOL> return _bytes_join ( values ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % af ) </s>
