Chapter 20: Low-Level Programming

Example xor.c, Page 515

In [17]:
KEY='&'
orig=raw_input()
orig_char=list(orig)
new_char=[]

def sxor(s1,s2):    
    return ''.join(chr(ord(a) ^ ord(b)) for a,b in zip(s1,s2))
for i in orig_char:
    N=sxor(i,KEY)
    new_char.append(N)
print ''.join(new_char)
Trust not him with your secrets, who, when left alone in your room, turns over your papers. Johann Kaspar Lavater 
rTSURHIRNOKQORN_ISTUCETCRU
QNI
QNCHJC@RGJIHCOH_ISTTIIK
RSTHUIPCT_ISTVGVCTUlINGHHmGUVGTjGPGRCT

Example viewmemory.c, Page 521

In [11]:
from itertools import product
def main():
    
    addr=""
    i=0
    n=0
    print "Address of main function: %s" % hex(id(main))
    print "Address of addr variable: %s" % hex(id(addr))
    addr=raw_input( "Enter a (hex) address: ")
    n=int(raw_input( "Enter number of bytes to view: "))
    #printf("\n");
    print " Address     Bytes     Characters\n"
    print " - - - - - - - - - - - - - - - - - - - - - - "
    ptr = addr;
    for nn in range(n,0,-10):
        print "%8s "% hex(id(addr))
       # for i,i in product(range(0//10), range(0//n)) :
            #print "%.2X ", *(ptr + i));  
        #for (; i <10; i++)
           # printf(" ");                    #pointer dereferencing not in python
        #printf(" ");
        #for (i = 0; i < 10 && i < n; i++) {
           # BYTE ch = *(ptr + i);
            #if (!isprint(ch))
            #    ch = '.';
           # printf("%c", ch);
    #}
        #printf("\n");
        #ptr += 10;
    #} 
    #return 0;
    #}
if  __name__=='__main__':
    main()
Address of main function: 0x4117eb8L
Address of addr variable: 0x1c3c148L
Enter a (hex) address: 8048000
Enter number of bytes to view: 40
 Address     Bytes     Characters

 - - - - - - - - - - - - - - - - - - - - - - 
0x41884b8L 
0x41884b8L 
0x41884b8L 
0x41884b8L