{ "metadata": { "name": "", "signature": "sha256:4a14f929c77abfd0a54b26e2baa48a2c7f478d22968a4f6231afc128a12dd480" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 20: Low-Level Programming" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example xor.c, Page 515" ] }, { "cell_type": "code", "collapsed": false, "input": [ "KEY='&'\n", "orig=raw_input()\n", "orig_char=list(orig)\n", "new_char=[]\n", "\n", "def sxor(s1,s2): \n", " return ''.join(chr(ord(a) ^ ord(b)) for a,b in zip(s1,s2))\n", "for i in orig_char:\n", " N=sxor(i,KEY)\n", " new_char.append(N)\n", "print ''.join(new_char)" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Trust not him with your secrets, who, when left alone in your room, turns over your papers. Johann Kaspar Lavater \n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "rTSUR\u0006HIR\u0006NOK\u0006QORN\u0006_IST\u0006UCETCRU\n", "\u0006QNI\n", "\u0006QNCH\u0006JC@R\u0006GJIHC\u0006OH\u0006_IST\u0006TIIK\n", "\u0006RSTHU\u0006IPCT\u0006_IST\u0006VGVCTU\b\u0006lINGHH\u0006mGUVGT\u0006jGPGRCT\u0006\n" ] } ], "prompt_number": 17 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example viewmemory.c, Page 521" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from itertools import product\n", "def main():\n", " \n", " addr=\"\"\n", " i=0\n", " n=0\n", " print \"Address of main function: %s\" % hex(id(main))\n", " print \"Address of addr variable: %s\" % hex(id(addr))\n", " addr=raw_input( \"Enter a (hex) address: \")\n", " n=int(raw_input( \"Enter number of bytes to view: \"))\n", " #printf(\"\\n\");\n", " print \" Address Bytes Characters\\n\"\n", " print \" - - - - - - - - - - - - - - - - - - - - - - \"\n", " ptr = addr;\n", " for nn in range(n,0,-10):\n", " print \"%8s \"% hex(id(addr))\n", " # for i,i in product(range(0//10), range(0//n)) :\n", " #print \"%.2X \", *(ptr + i)); \n", " #for (; i <10; i++)\n", " # printf(\" \"); #pointer dereferencing not in python\n", " #printf(\" \");\n", " #for (i = 0; i < 10 && i < n; i++) {\n", " # BYTE ch = *(ptr + i);\n", " #if (!isprint(ch))\n", " # ch = '.';\n", " # printf(\"%c\", ch);\n", " #}\n", " #printf(\"\\n\");\n", " #ptr += 10;\n", " #} \n", " #return 0;\n", " #}\n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Address of main function: 0x4117eb8L\n", "Address of addr variable: 0x1c3c148L\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a (hex) address: 8048000\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter number of bytes to view: 40\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Address Bytes Characters\n", "\n", " - - - - - - - - - - - - - - - - - - - - - - \n", "0x41884b8L \n", "0x41884b8L \n", "0x41884b8L \n", "0x41884b8L \n" ] } ], "prompt_number": 11 } ], "metadata": {} } ] }