博文

目前显示的是 七月, 2010的博文

Master theorem 主定理及其应用算法

图片
Generic form The master theorem concerns recurrence relations of the form: In the application to the analysis of a recursive algorithm, the constants and function take on the following significance: n  is the size of the problem. a  is the number of subproblems in the recursion. n / b  is the size of each subproblem. (Here it is assumed that all subproblems are essentially the same size.) f  ( n ) is the cost of the work done outside the recursive calls, which includes the cost of dividing the problem and the cost of merging the solutions to the subproblems. Application to common algorithms Algorithm Recurrence Relationship Run time Comment Binary search O (log( n )) Apply Master theorem where  f ( n ) =  n c [3] Binary tree traversal O ( n ) Apply Master theorem where  f ( n ) =  n c [3] Optimal Sorted Matrix Search O ( n ) Apply  Akra-Bazzi theorem  for  p  = 1  and  g ( u ) = log( u ) ...

Linux 缓冲区溢出

1.什么是缓冲区溢出? ~~~~~~~~~~~~~~~~~~~ buffer overflow,buffer overrun,smash the stack,trash the stack, scribble the stack, mangle the stack,spam,alias bug,fandango on core, memory leak,precedence lossage,overrun screw...指的是一种系统攻击的手 段,通过往程序的缓冲区写超出其长度的内容,造成缓冲区的溢出,从而破坏程 序的堆栈,使程序转而执行其它指令,以达到攻击的目的。据统计,通过缓冲区 溢出进行的攻击占所有系统攻击总数的80%以上。 造成缓冲区溢出的原因是程序中没有仔细检查用户输入的参数。例如下面程 序: example1.c ---------------------------------------------------------------------- void function(char *str) { char buffer[16]; strcpy(buffer,str); } ---------------------------------------------------------------------- 上面的strcpy()将直接吧str中的内容copy到buffer中。这样只要str的长度 大于16,就会造成buffer的溢出,使程序运行出错。存在象strcpy这样的问题的 标准函数还有strcat(),sprintf(),vsprintf(),gets(),scanf(),以及在循环内的 getc(),fgetc(),getchar()等。 当然,随便往缓冲区中填东西造成它溢出一般只会出现Segmentation fault 错误,而不能达到攻击的目的。最常见的手段是通过制造缓冲区溢出使程序运行 一个用户shell,再通过shell执行其它命令。如果该程序属于root且有suid权限 的话,攻击者就获得了一个有root权限的shell,可以对系统进行任意操作了。 请注意,如果没有特别说明,下面的内容都假设用户使用的平台为基于Intel x86 CPU的Linux系统。对其...

Python 正则表达式反斜杠的麻烦

在早期规定中,正则表达式用反斜杠字符 ("\") 来表示特殊格式或允许使用特殊字符而不调用它的特殊用法。这就与 Python 在字符串中的那些起相同作用的相同字符产生了冲突。 让我们举例说明,你想写一个 RE 以匹配字符串 "\section",可能是在一个 LATEX 文件查找。为了要在程序代码中判断,首先要写出想要匹配的字符串。接下来你需要在所有反斜杠和其它元字符前加反斜杠来取消其特殊意义,结果要匹配的字符串 就成了"\\section"。 当把这个字符串传递给re.compile()时必须还是"\\section"。然而,作为Python的字符串实值(string literals)来表示的话,"\\section"中两个反斜杠还要再次取消特殊意义,最后结果就变成了"\\\\section"。 字符 阶段 \section 要匹配的字符串 \\section 为 re.compile 取消反斜杠的特殊意义 "\\\\section" 为"\\section"的字符串实值(string literals)取消反斜杠的特殊意义 简单地说,为了匹配一个反斜杠,不得不在 RE 字符串中写 '\\\\',因为正则表达式中必须是 "\\",而每个反斜杠在常规的 Python 字符串实值中必须表示成 "\\"。在 REs 中反斜杠的这个重复特性会导致大量重复的反斜杠,而且所生成的字符串也很难懂。 解决的办法就是为正则表达式使用 Python 的 raw 字符串表示;在字符串前加个 "r" 反斜杠就不会被任何特殊方式处理,所以 r"\n" 就是包含"\" 和 "n" 的两个字符,而 "\n" 则是一个字符,表示一个换行。正则表达式通常在 Python 代码中都是用这种 raw 字符串表示。 常规字符串 Raw 字符串 "ab*" r"ab*" "\\\\section...

Accessing Jython from Java Without Using jythonc

You may or may not know that it is possible to access Jython code from Java without compiling it using the jythonc utility. This technique is possible using a mixture of Java interfaces and usage of the  PythonInterpreter . As a matter of fact, I believe that using this technique correctly is more effective than using jythonc. To put it simply, to use this technique you must create a "factory" method which uses the  PythonInterpreter  class to interpret a .py module for use within Java code. Any Java code that uses the Jython code should be coded against an interface which is implemented by the Jython code. In order to provide a fully functional example, I've created a simple application with hard-coded data. This application shows the potential for using this technique within your Java applications to have the ability for use of dynamic Jython objects. The application is simply called "jyinterface" and it contains four pieces of code: -Main.java - Jyt...