summaryrefslogtreecommitdiff
path: root/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter20.ipynb
blob: 10d6049f5b25011b45c90f2259106066f5366759 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
{
 "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": {}
  }
 ]
}