Friday, February 20, 2009

Encrypt your private blog post

[轉載] [Javascript] Encrypt your private blog post from the article of Kaie's Blog

今日在PPT上的Blog版逛到版友weijr所提到的一篇好文章,可以用來針對Blog中某一段文字給予加密,變成讓人無法理解的「火星文」 XD.. 解密過程只輸入正確的密碼之後,即可馬上在Blog上顯示出正確的「地球文」,不用重新Reload...超棒的Hack,對於此類技術性的Hack,個人超愛的…

研究所時有碰過一些跟Cryptography(密碼學)有關的東西,但只懂一點理論上的皮毛而已,沒有動作實作過,如今玩Blogger的同時,剛好有個有人心士將他所學應用在他的需求上,真是與實務面作了結合,太棒了老兄…我要向他看齊..XD。

原作者實現此Hack的用意就是: 他想要將他一些私人的、不方便公開的東西,亦或某些只想供親朋好友看的內容,記錄在公開的Blog上頭,又不失隱密性,且方便日後查詢,於是針對這些私密訊息的簡易加解密功能就如此誕生啦! (所利用的演算法是 256-bit AES,目前來說「安全性」算相當高了)

※適用範圍: 所有支援Javascript的Blog,若不幸你的Blog不支援的話,快跳槽來「Blogger」吧,保證可以隨便你惡搞的Blog平台,不會後悔的資料來源:
Vincent Cheung -> Encrypted blog posts
快往下瞧瞧這強大的Hack吧... :D
:: Demo (展現出來的效果如下,兩種方式) ::
Key(密鑰): ie-sucks

* Demo1:
Click to Decrypt text (點擊此處解開火星文,裡頭有清涼的唷)

恭喜你解碼成功,順便送你看個辣妹,地球文還是好懂多了吧...
* Demo2:
Show encrypted text (點擊此處變身為火星人)


火星人:是不是超屌的呀!! 再不來安裝一下,就對不起自己了..



上方展現出來的效果是不是很炫呀 ^_^,超吸引人的吧,快來安裝一下吧,底下開始中文化原文..囧rz,方法如下:

首先在你的Blog中嵌入此Hack所需的Javascript,在Blogger的使用方法就是將底下的code加在Template中的上面即可(其它Blog方法就是加在header裡頭就對了)

完成上面動作之後,基本上就完成了,若要在新文章中新增「火星文時」的話,請透過底下兩個步驟:

1. 產生加密過後的火星文:
透過此網站,輸入「Key」欄位以及「Plain Text」欄位,接著按Encrypt Button(加密鈕,在Plain Text欄的下方),此時便會在「Cipher Text」欄位產生加密過後的火星文字啦(底下會用到..)!!
※千萬要記得上方你自行定義的Key,之後解密時要用的,若忘記了,恐怕沒有量子電腦(Quantum Computer)這般等級的高速電腦是解不出來的。
2. 嵌入火星文片段至新文章中:
共有底下兩種顯示方式,依個人喜好選擇擇一
.方法一(如上頭Demo1,直接在文章中顯示火星文):Click to Decrypt text (點擊此處解開火星文)

此段內容為由上面Step1.所產生出來的一整段火星文(例如:wRxSwskrWsmohc1...之類的文字)


.方法二(如上頭Demo2,隱藏火星文,只顯示連結):Show encrypted text



※注意上方的unique_name型態是div tag的一個id名子,可以自行命名,只要在template中,或文章中不出現同樣的id即可,否則會造成判斷錯誤!!也就是在一個顯示頁面中,有用到兩個以上火星文片段的話,那麼此id的命名就必須是unique_name,unique_name1,unique_name2...之類的。


最後,作者很風趣的列舉了幾個常見問題(Faq),很北爛的一個傢伙...有興趣去看看...

Wednesday, February 18, 2009

Make the DLL compiled-linked with GCC under Cygwin valid and acceptable for Microsoft Visual Studio

First of all, we need to have the DLL and DEF files generated by GCC. Here is the command to do so.
Assuming we have foo.c
gcc -mno-cygwin -shared -o foo.dll foo.c -Wl,--output-def,foo.def,--out-implib,libfoo.a
The output files are foo.dll, libfoo.a, foo.def.
Second, using Microsoft Library Manager lib.exe utility to generate the foo.lib with foo.def. Here is the command:
lib.exe /machine:i386 /def:foo.def
Then we will get a foo.lib
Now, we are ready to use it in the Visual Studio's Project.
All you need to do is to add the foo.lib to Linker->Input->Additional Dependencies field and put foo.lib and foo.dll in your project's folder.
P.S. If you cannot find lib.exe or fail to launch lib.exe, check the PATH of your System Variables. (right click on My Computer->Properties->Advanced tab->Environment Variables->System Variables)
Make sure C:\Program Files\Microsoft Visual Studio 8\VC\bin and C:\Program Files\Microsoft Visual Studio 8\Common7\IDE are there in the PATH.

Using GCC to create static and shared library

[轉貼]用GCC自製Library

引用: PTT看板: LinuxDev (作者: cole945)



Library可分成三種,staticshareddynamically loaded

1. Static libraries

Static
程式庫用於靜態連結,簡單講是把一堆object檔用ar(archiver)
包裝集合起來,檔名以 `.a' 結尾。優點是執行效能通常會比後兩者快,
而且因為是靜態連結,所以不易發生執行時找不到library或版本錯置而
無法執行的問題。缺點則是檔案較大,維護度較低;例如library如果發
bug需要更新,那麼就必須重新連結執行檔。

1.1
編譯

編譯方式很簡單,先例用 `-c' 編出 object 檔,再用 ar 包起來即可。

____ hello.c ____
#include <stdio.h>
void hello(){ printf("Hello "); }

____ world.c ____
#include <stdio.h>
void world(){ printf("world."); }

____ mylib.h ____
void hello();
void world();

$ gcc -c hello.c world.c /*
編出 hello.o world.o */
$ ar rcs libmylib.a hello.o world.o /*
包成 limylib.a */

這樣就可以建出一個檔名為 libmylib.a 的檔。輸出的檔名其實沒有硬性規定,
但如果想要配合 gcc '-l' 參數來連結,一定要以 `lib' 開頭,中間是你要
library名稱,然後緊接著 `.a' 結尾。

1.2
使用

____ main.c ____
#include "mylib.h"
int main() {
hello();
world();
}

使用上就像與一般的 object 檔連結沒有差別。

$ gcc main.c libmylib.a

也可以配合 gcc `-l' 參數使用

$ gcc main.c -L. -lmylib

`-Ldir'
參數用來指定要搜尋程式庫的目錄,`.' 表示搜尋現在所在的目錄。
通常預設會搜 /usr/lib /lib 等目錄。
`-llibrary'
參數用來指定要連結的程式庫 'mylib' 表示要與mylib進行連結
,他會搜尋library名稱前加`lib'後接`.a'的檔案來連結。

$ ./a.out
Hello world.


2. Shared libraries

Shared library
會在程式執行起始時才被自動載入。因為程式庫與執行檔
是分離的,所以維護彈性較好。有兩點要注意,shared library是在程式起始
時就要被載入,而不是執行中用到才載入,而且在連結階段需要有該程式庫
才能進行連結。

首先有一些名詞要弄懂,sonamereal namelinker name

soname
用來表示是一個特定 library 的名稱,像是 libmylib.so.1
前面以 `lib' 開頭,接著是該 library 的名稱,然後是 `.so' ,接著
是版號,用來表名他的介面;如果介面改變時,就會增加版號來維護相容度。

real name
是實際放有library程式的檔案名稱,後面會再加上 minor 版號與
release
版號,像是 libmylib.so.1.0.0

一般來說,版號的改變規則是(印象中在 APress-Difinitive Guide to GCC中有
提到,但目前手邊沒這本書),最尾碼的release版號用於程式內容的修正,
介面完全沒有改變。中間的minor用於有新增加介面,但相舊介面沒改變,所以
與舊版本相容。最前面的version版號用於原介面有移除或改變,與舊版不相容
時。

linker name
是用於連結時的名稱,是不含版號的 soname ,如: libmylib.so
通常 linker name real name是用 ln 指到對應的 real name ,用來提供
彈性與維護性。

2.1 編譯
shared library
的製作過程較複雜。

$ gcc -c -fPIC hello.c world.c

編譯時要加上 -fPIC 用來產生 position-independent code。也可以用 -fpic
參數。 (不太清楚差異,只知道 -fPIC 較通用於不同平台,但產生的code較大
,而且編譯速度較慢)

$ gcc -shared -Wl,-soname,libmylib.so.1 -o libmylib.so.1.0.0 \
hello.o world.o

-shared
表示要編譯成 shared library
-Wl
用於參遞參數給linker,因此-sonamelibmylib.so.1會被傳給linker處理。
-soname
用來指名 soname limylib.so.1
library
會被輸出成libmylib.so.1.0.0 (也就是real name)

若不指定 soname 的話,在編譯結連後的執行檔會以連時的library檔名為
soname
,並載入他。否則是載入soname指定的library檔案。

可以利用 objdump 來看 library soname

$ objdump -p libmylib.so | grep SONAME
SONAME libmylib.so.1

若不指名-soname參數的話,則library不會有這個欄位資料。

在編譯後再用 ln 來建立 soname linker name 兩個檔案。
$ ln -s libmylib.so.1.0.0 libmylib.so
$ ln -s libmylib.so.1.0.0 libmylib.so.1


2.2
使用

與使用 static library 同。

$ gcc main.c libmylib.so

以上直接指定與 libmylib.so 連結。

或用

$ gcc main.c -L. -lmylib

linker
會搜尋 libmylib.so 來進行連結。

如果目錄下同時有staticshared library的話,會以shared為主。
使用 -static 參數可以避免使用shared連結。

$ gcc main.c -static -L. -lmylib

此時可以用 ldd 看編譯出的執行檔與shared程式庫的相依性
$ldd a.out
linux-gate.so.1 => (0xffffe000)
libmylib.so.1 => not found
libc.so.6 => /lib/libc.so.6 (0xb7dd6000)
/lib/ld-linux.so.2 (0xb7f07000)
輸出結果顯示出該執行檔需要 libmylib.so.1 這個shared library
會顯示 not found 因為沒指定該library所在的目錄,所找不到該library

因為編譯時有指定-soname參數為 libmylib.so.1 的關係,所以該執行檔會
載入libmylib.so.1。否則以libmylib.so連結,執行檔則會變成要求載入
libmylib.so

$ ./a.out
./a.out: error while loading shared libraries: libmylib.so.1:
cannot open shared object file: No such file or directory

因為找不到 libmylib.so.1 所以無法執行程式。
有幾個方式可以處理。

a.
libmylib.so.1 安裝到系統的library目錄,如/usr/lib
b.
設定 /etc/ld.so.conf ,加入一個新的library搜尋目錄,並執行ldconfig
更新快取
c.
設定 LD_LIBRARY_PATH 環境變數來搜尋library
這個例子是加入目前的目錄來搜尋要載作的library
$ LD_LIBRARY_PATH=. ./a.out
Hello world.

3. Dynamically loaded libraries

Dynamicaaly loaded libraries
才是像 windows 所用的 DLL ,在使用到
時才載入,編譯連結時不需要相關的library。動態載入庫常被用於像plug-ins
的應用。

3.1
使用方式
動態載入是透過一套 dl function來處理。
#include <dlfcn.h>
void *dlopen(const char *filename, int flag);
開啟載入 filename 指定的 library
void *dlsym(void *handle, const char *symbol);
取得 symbol 指定的symbol namelibrary被載入的記憶體位址。
int dlclose(void *handle);
關閉dlopen開啟的handle
char *dlerror(void);
傳回最近所發生的錯誤訊息。

____ dltest.c ____
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
int main() {
void *handle;
void (*f)();
char *error;

/*
開啟之前所撰寫的 libmylib.so 程式庫 */
handle = dlopen("./libmylib.so", RTLD_LAZY);
if( !handle ) {
fputs( dlerror(), stderr);
exit(1);
}

/*
取得 hello function address */
f = dlsym(handle, "hello");
if(( error=dlerror())!=NULL) {
fputs(error, stderr);
exit(1);
}
/*
呼叫該 function */
f();
dlclose(handle);
}

編譯時要加上 -ldl 參數來與 dl library 連結
$ gcc dltest.c -ldl
結果會印出 Hello 字串
$ ./a.out
Hello

關於dl的詳細內容請參閱 man dlopen

--
參考資料:

Creating a shared and static library with the gnu compiler [gcc]
http://www.adp-gmbh.ch/cpp/gcc/create_lib.html

Program Library HOWTO
http://tldp.org/HOWTO/Program-Library-HOWTO/index.html

APress - Definitive Guide to GCC

Monday, February 16, 2009

Compile FFMPEG with Cygwin under Windows

This atricle guides you how to compile FFMPEG with Cygwin under Windows. The orginal post is at http://mqjing.blogspot.com/2008/12/c-cygwin-ffmpeg-make-ffmpeg-under.html by 井民全.

FFmpeg 是著名的多媒體壓縮解壓縮 library, 例如: 你想處理壓縮 Xvid 或 Divx 壓縮解壓縮, 這個專案裡面的 source 你都可以參考. 可是呢...

這東西不好編譯!

尤其是你以為正在編譯的版本和網路上文章作者所說的一樣時, 你就會開始犯錯. 要正確的編譯 FFmpeg 你一定要注意版本序號以及你的編譯環境.

FFmpeg 版本一直在進展, 昨天更新的最新 source code 並不一定保證在任何的編譯環境都能順利成功. 因為 update 的作者不一定會把所有的編譯環境都測過才 update, 如此一來可以意味著 open source 領域有一個 source code dependency 問題.

如果你想要編譯 FFmpeg 最好使用作者當時寫作並測試通過的所有軟體版本, 已保證第一次就會成功.

open source 社群漏掉的拼圖, 就是編譯環境與 source code 的版本控制還不是那麼的完善, 新版本的 library source code 可能造成其他的第三方應用軟體編譯失敗. 現在想想很明白這道理, 可是這卻是大家花費大量寶貴時間解決編譯問題的根源所在.

或許在編譯之前就要詳細檢查所有會用到 source code 的版本, 現在 ./configure 不完整, 要徹底解決 source code 版本 dependency 的問題, 還有待大家努力!

Step 1: Checkout the FFmpeg source code (20080508 版本)

  • svn checkout svn://svn.mplayerhq.hu/ffmpeg/trunk@12383 ffmpeg-12383

(我使用 revision 12383 版以前的原因, 在於最新版的 vfwcap.c 無法在 cygwin 下編譯成功)


Step 2: Patch--ffmpeg--cygwin (see link)

  • replace all llrint functions to the lrint in the libavcodec/mpegaudiodec.c

Step 3: Setup the configure option script

1. modify the option from --enable-pp to the --enable-postproc

  • from revision 12376, rename pp variable to postproc for consistency

2. --target-os=mingw32 (from revision 8049)

3. [Optional] modify the option from --enable-amr_nb to the --enable-libamr-nb (not free. see:http://www.3gpp.org)

4. [Optional] add the option --enable-nonfree , if you add the option --enable-amr_nb

ex:

--------------------- configure-ffmpeg.sh ---------------------

./configure --enable-memalign-hack \
--enable-postproc \
--extra-cflags=-I/usr/win32/include \
--extra-ldflags=-L/usr/win32/lib \
--prefix=/usr/win32 \
--cc="gcc -mno-cygwin" \
--enable-gpl \
--disable-vhook \
--enable-shared \
--target-os=mingw32 \
--extra-cflags=-mno-cygwin \
--extra-libs=-mno-cygwin

--------------------- configure-ffmpeg.sh ---------------------

5. Update your /usr/win32/include

These content is out of the version of the FFmpeg which you want to build. Update these files by yourself. Copy all header files in the following directories to /usr/win32/include.

ffmpeg-12383\libavcodec, libavdevice, libavfilter, libavformat, libavutil, libpostproc, libswscale

7. Create Makefile for building the FFmpeg

run the following command to setup the FFmpeg configures

   configure-ffmpeg.sh


 


6. make


加入 amr 語音libarary 的步驟


1. 新增 configure-ffmpeg.sh



--enable-libamr-nb \

--enable-nonfree \



2. 修改 source code



at ffmpeg-12383/libavcodec/libamr.c



#include 改成 #include



#include 改成 #include



否則你可能會遭遇到 下面的錯誤訊息



libamr.c:80:30: amrnb/interf_dec.h: No such file or directory

libamr.c:81:30: amrnb/interf_enc.h: No such file or directory


libamr.c:93: error: field `mode' has incomplete type



3. 增加 amr_float 內相關的 depend



否則你會遭遇到下面的錯誤



---------------------------------------------------------------------



libamr.o: In function `amr_nb_decode_init':

/home/Jing/ffmpeg-12383/libavcodec/libamr.c:369: undefined reference to `_Decode


r_Interface_init'


libamr.o: In function `amr_nb_encode_init':


/home/Jing/ffmpeg-12383/libavcodec/libamr.c:408: undefined reference to `_Encode


r_Interface_init'



---------------------------------------------------------------------



a. cd /home/Jing/ffmpeg-12383/libavcodec/amr_float



b. 修改 amr_float 裡面的 makefile.gcc, 讓他獨立於 cygwin



原來是



CFLAGS = -Wall -I. $(CFLAGS_$(MODE)) -D$(VAD)



加上 CFLAGS = -Wall -mno-cygwin -I. $(CFLAGS_$(MODE)) -D$(VAD)



否則你在編譯 ffmpeg 的 libcodec 時, 會遇到 找不到 ___getreent 的錯誤.



amr_float/interf_dec.o:interf_dec.c:(.text+0x34e): undefined reference to `___ge

treent'


amr_float/interf_enc.o:interf_enc.c:(.text+0x56e): undefined reference to `___ge


treent'



c. 產生必要的 .o 檔



make -fmakefile.gcc



d. 修改 ffmpeg/libcocdec 的 Makefile, 加入必要的 amr_float object files



OBJS-$(CONFIG_LIBAMR_NB) += libamr.o



改成


OBJS-$(CONFIG_LIBAMR_NB) += libamr.o ./amr_float/interf_dec.o ./amr_float/interf_enc.o ./amr_float/sp_dec.o ./amr_float/sp_enc.o

Check clients which connect to Mac OS X Wi-Fi Internet Sharing

arp -i bridge100 -a bridge100 may be different on your Mac OSX