#!/usr/bin/env python

# Read the wiki for more infomation
# https://github.com/lennylxx/ipv6-hosts/wiki/sn-domains

import sys
table = '1023456789abcdefghijklmnopqrstuvwxyz'

def iata2sn(iata):
    global table
    sn = ''
    for v in iata[0:3]:
        i = ((ord(v) - ord('a')) * 7 + 5) % 36
        sn += table[i]
    return sn

def sn2iata(sn):
    global table
    iata = ''
    for v in sn:
        i = table.index(v)
        i = (5 - i % 7) * 5 + i / 7 + 10
        iata += table[i]
    return iata

def num2code(num):
    global table
    code = ''
    for v in num:
        i = ((ord(v) - ord('0') + 1) * 7) % 36
        code += table[i]
    return code
    
def code2num(code):
    global table
    num = ''
    for v in code:
        i = table.index(v)
        i = i / 7 + i % 7 - 1
        num += str(i)
    return num

def main():
    if len(sys.argv) != 3:
        print 'usage:\tconv -i iata\n\tconv -s sn'
        sys.exit(1)

    input = sys.argv[2]
    ret = ''
    if sys.argv[1] == '-i':
        ret += iata2sn(input[0:3])
        ret += num2code(input[3:5])
        ret += 'n'
        ret += num2code(input[6:8])
        print ret
    elif sys.argv[1] == '-s':
        ret += sn2iata(input[0:3])
        ret += code2num(input[3:5])
        ret += 's'
        ret += code2num(input[6:8])
        print ret
    else:
        print 'Unknown option.'
        sys.exit(1)

if __name__ == '__main__':
    main()

@@ -238,15 +238,37 @@
 iata
-%5B0:3%5D:%0A
+:%0A        if v in table:%0A    
     
@@ -318,16 +318,20 @@
         
+    
 sn += ta
@@ -329,32 +329,66 @@
  sn += table%5Bi%5D%0A
+        else:%0A            sn += v%0A
     return sn%0A%0Ad
@@ -451,16 +451,43 @@
  in sn:%0A
+        if v in table:%0A    
         
@@ -509,24 +509,28 @@
 (v)%0A        
+ 
+   
 i = (5 - i %25
@@ -554,24 +554,28 @@
  10%0A        
+    
 iata += tabl
@@ -571,32 +571,68 @@
 ata += table%5Bi%5D%0A
+        else:%0A            iata += v%0A
     return iata%0A
@@ -700,16 +700,43 @@
 in num:%0A
+        if v in table:%0A    
         
@@ -778,24 +778,28 @@
  36%0A        
+    
 code += tabl
@@ -799,24 +799,60 @@
 += table%5Bi%5D%0A
+        else:%0A            code += v%0A
     return c
@@ -929,16 +929,43 @@
 n code:%0A
+        if v in table:%0A    
         
@@ -975,32 +975,36 @@
  table.index(v)%0A
+    
         i = i / 
@@ -1021,24 +1021,28 @@
 - 1%0A        
+   
+ 
 num += str(i
@@ -1043,16 +1043,51 @@
  str(i)%0A
+        else:%0A            num += v%0A
     retu
@@ -1182,16 +1182,46 @@
 nv -s sn
+%5Cn%5Ctconv -p isp%5Cn%5Ctconv -g ggc
 '%0A      
@@ -1426,34 +1426,16 @@
 t%5B6:8%5D)%0A
-        print ret%0A
     elif
@@ -1590,21 +1590,203 @@
     
-    print ret
+elif sys.argv%5B1%5D == '-p':%0A        ret += iata2sn(input%5B:-1%5D)%0A        ret += num2code(input%5B-1%5D)%0A    elif sys.argv%5B1%5D == '-g':%0A        ret += sn2iata(input%5B:-1%5D)%0A        ret += code2num(input%5B-1%5D)
 %0A   
@@ -1845,16 +1845,47 @@
 xit(1)%0A%0A
+    print ret%0A    sys.exit(0)%0A%0A
 if __nam

support ggc sn domain names