Wednesday, February 01, 2012

BCD

You can change your BCD configuration (parameters used by bootmgr, boot applications, and winload) by BCDedit in command line or editing the registry (if not changeable by BCDedit)

1 Show all entries in a BCD store
bcdedit -enum all

2 Enable debug
bcdedit -bootdebug {bootmgr} on  //enable debugging of Windows Boot Manager
bcdedit -bootdebug on            //enable debugging of boot loader
Bcdedit -debug on                //enable debugging of operating system

3 Enable bootlog (cause windows to write a log of the boot to %SystemRoot%\Ntbtlog.txt In your VM, edit registry (HKLV\System\CurrentControlSet\Control\) Change value of "SystemStartOptions" 4 Turn off driver verification
bcdedit -set loadoptions DDISABLE_INTEGRITY_CHECKS
bcdedit -set TESTSIGNING ON


configure for other BCD files

(1) check the for "Windows Boot Loader"
bcdedit /store c:\winpe_amd64\ISO\boot\BCD -enum all
(2) turn the debug option on (see output in WinDbg), is sth like {7619dcc8-fafe-11d9-b411-000476eba25f} in your "Windows Boot Loader" session
bcdedit /store c:\winpe_amd64\ISO\boot\BCD -set  debug on
(3) turn the bootdebug option on (break in WinDbg during boot), is sth like {7619dcc8-fafe-11d9-b411-000476eba25f} in your "Windows Boot Loader" session
bcdedit /store c:\winpe_amd64\ISO\boot\BCD -set  bootdebug on
Boot Options in Windows Vista and Later: http://msdn.microsoft.com/en-us/library/windows/hardware/ff542273(v=vs.85).aspx BCD Boot Options Reference: http://msdn.microsoft.com/en-us/library/windows/hardware/ff542205(v=vs.85).aspx

Sunday, January 29, 2012

WinSock Kernel

Winsock Kernel http://msdn.microsoft.com/en-us/library/windows/hardware/ff571084(v=vs.85).aspx Sample code: http://www.rohitab.com/discuss/topic/37876-winsock-kernel/ http://hi.baidu.com/kernelkit/blog/item/e8a93060e1ad6a41eaf8f856.html

Monday, January 02, 2012

[Linux]静态库,动态库

.o, .a, .so 这些的区别
Static, Shared Dynamic and Loadable Linux Libraries
Shared libraries and static libraries
Creating a shared and static library with the gnu compiler [gcc]

创建静态库(.a)
比如为了把foo1.o, foo2.o这两个obj文件打包成一个静态库叫做foo.a
$ gcc -Wall -c foo1.c foo2.c
$ ar -cvq libfoo.a foo1.o foo2.o
创建动态库(.so)
首先用-fPIC选项告诉gcc生成position independant code的.o文件
$ gcc -Wall -fPIC -c foo1.c foo2.c
接着使用-shared选项生成.so文件
$ gcc -shared -Wl,-soname,libfoo.so.1 -o libfoo.so.1.0.1  foo1.o foo2.o
使用库(.a,.so)
假设生成的libfoo.a或者libfoo.so都在/path/to/libfoo这个路径下,可以通过"-L/path/to/libfoo"这个选项告诉gcc去这个路径下去寻找需要的库.此外再通过-lfoo这个选项告诉gcc去寻找libfoo.a或者libfoo.so.
$ gcc -Wall -L/path/to/libfoo your-program.c -lfoo -o your-program
默认情况下/usr/lib, /usr/local/lib这个几个路径一般会被首先查找.如果需要gcc去其他默认路径下查找(比如/path/to/libfoo),也可以通过把/path/to/libfoo添加LD_LIBRARY_PATH环境变量中
强制使用静态库(.a)
如果在/path/to/libfoo下同时存在libfoo.a和libfoo.so, libfoo.so会被优先使用.可以用-static选项强制gcc避免使用动态库
$ gcc -Wall -static -L/path/to/libfoo your-program.c -lfoo -o your-program
libtool
configure关于静态动态库的参数
  • --enable-shared: 编译动态库(默认为yes)
  • --enable-static: 编译静态库(默认为no)
有时"--enable-static"会以尝试静态的方式编译所有dependent的库,产生undefined reference这样的错误.
$ ./configure --enable-static
warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/x86_64-linux-gnu/4.6.1/../../../x86_64-linux-gnu/libcrypto.a(c_zlib.o): In function `bio_zlib_free':
(.text+0x4f): undefined reference to `inflateEnd'
可以以--enable-static=libfoo这样的方式指定gcc只以static的方式编译libfoo.其他依赖的库还是以默认的shared方式编译(http://sourceware.org/autobook/autobook/autobook_85.html)
$ ./configure --enable-static=libfoo
libtool在link模式下有选项:
  • -shared
  • -static
  • -all-static

Thursday, December 29, 2011

[Linux/Mac]给一个文件/目录加密

如果你像我一样越来越依赖类似Dropbox这样的云端存储工具来同步几台不同的机器上的数据,同时又担心放上去了重要的文件可能会被偷窥或者担心泄露,那么你可以考虑像我一样,加密你在Dropbox上的文件以及目录。这样你既可以享受Dropbox带来的自动同步的好处,又减小对Dropbox安全性的依赖.
使用encfs加密目录

Step0: 安装encfs:
在Ubuntu上
$ sudo apt-get install encfs
在Mac上如果你使用home brew(推荐!)
$ brew install encfs
或者还在使用古老的mac port
$ sudo port install encfs

Step1: 使用encfs:
首先建立一个叫~/Dropbox/Encrypted的目录。这个目录暂时还没有任何数据,但是以后你的加密过的数据和文件就会存放在这里。由于这个目录在Dropbox下,所以它会被自动同步到你的各个机器上。
然后再建立一个叫~/ToBeEncrypted的目录。这个目录将会作为一个mount point。
将Encrypted通过encfs而mount到ToBeEncrypted:
$ encfs ~/Dropbox/Encrypted ~/ToBeEncrypted
Creating new encrypted volume.
Please choose from one of the following options:
 enter "x" for expert configuration mode,
 enter "p" for pre-configured paranoia mode,
 anything else, or an empty line will select standard mode.
?> p
这里选择p 然后输入密码。这样你就创建了一个用此密码加密的volume,并且mount在了~/ToBeEncrypted这里
Step2: 把你需要加密的数据(文件或者目录都可以)copy或者move到~/ToBeEncrypted目录下:
比如我们要把一个叫MyTopSecret.txt的文件放入加密后的目录
$ mv MyTopSecret.txt ~/ToBeEncrypted
你会看见在~/Dropbox/Encrypted这个目录下,也会出现一个文件,但是是被加密过后的。
Step3: 拷贝完毕后,unmount含有未加密数据的目录
$ fusermount -u ~/ToBeEncrypted

Step4: 再次mount加密过的目录
$ encfs ~/Dropbox/Encrypted ~/ToBeEncrypted
输入密码后,那些加密过的文件就会再次以加密前的模样出现再~/ToBeEncrypted这里
使用vi加密单个文件

在encfs的基础上,你还可以再用vim给文件设置一层密码
加密码
$ vim -x filename
更换密码:
$ vim +X filename

Ref

File Encryption in Linux

Saturday, December 03, 2011

[占坑贴] Scala笔记

尽量简单的说,啥是Scala

在某种意义上,Scala是一种与Java平行的语言.它的源代码被编译成Java的bytecode,运行于Java虚拟机之上.正因为在JVM级是兼容的,Scala里可以调用Java的库. 这是用Scala写的HelloWorld程序
object HelloWorld extends App {
   println("Hello, world!")
}


那Scala和Java比有啥不一样的地方?

Scala这个语言的设计初衷是融合面向对象(OO)的设计思想(以C++,Java,Object C为代表)和函数式编程(functional programming)的思想(以Lisp,Haskell,ML为代表). Scala的设计优美的融合了这两种截然不同的风格.以quick sort为例,大家可以看一下Scala如何分别用普通的C风格和Haskell风格来实现:
def sort(xs: Array[Int]) {
  def swap(i: Int, j: Int) {
    val t = xs(i); xs(i) = xs(j); xs(j) = t
  }
  def sort1(l: Int, r: Int) {
    val pivot = xs((l + r) / 2)
    var i = l; var j = r
    while (i <= j) {
    while (xs(i) < pivot) i += 1
    while (xs(j) > pivot) j -= 1
    if (i <= j) {
      swap(i, j)
      i += 1
      j -= 1
    }
  }
  if (l < j) sort1(l, j)
  if (j < r) sort1(i, r)
}
sort1(0, xs.length - 1)
}
或者像Haskell那样写的:
def sort(xs: Array[Int]): Array[Int] = {
  if (xs.length <= 1) xs
  else {
    val pivot = xs(xs.length / 2)
    Array.concat(
      sort(xs filter (pivot >)),
      xs filter (pivot ==),
      sort(xs filter (pivot <)))
  }
}

支持Functional Programming有啥好处呢?

Functional programming language有着简洁,强大的表达能力,容易帮你写出bug-free的代码.另外在并行性上也有着优势。 比如在传说中那间以青年导师李开复老师为校长的CS四大牛校之一的CMU,大一新生首先学习Functional programming而不是传统的面向对象
具体到Scala,它支持如下几个Functional Programming的特性:
  • higher-order function(高阶函数)
  • anonymous function(匿名函数)
  • nested function()
  • curry(柯里化)
而有了对这几个特性的支持,Scala可以宣布自己支持First-class function。也就是说,函数是first-class citizen。注意这四个特性在Java中是不被支持的。

啥是Higher-order function

其定义是说函数本身可以作为其他函数的参数,或者返回值,或者赋值给一个变量。总而言之.
如果一个函数的参数可以为其他函数,那么这个函数被成为Higher-order function(高阶函数). C和Python都支持Higher-order function,但是Java本身是不支持的.一个Python的例子:
def f(x):
    return x + 3
 
def g(function, x):
    return function(x) * function(x)
 
print g(f, 7)
分别用Java和Scala来完成Wordcount这个经典的MapReduce应用: http://www.scala-blogs.org/2008/09/scalable-language-and-scalable.html 我们可以看到Scala的代码比Java的代码简介许多.
参考

Scala的官方网站: http://www.scala-lang.org/
一个不错的scala学习网站: http://www.simplyscala.com/
Scala by examples
A Tour of Scala