2015年4月6日 星期一

Makefile

ref: http://tetralet.luna.com.tw/?op=ViewArticle&articleId=185

Makefile語法:

● 語法:

target: dependencies
<tab>Commands

target:  要建立的檔案
  make在編譯時,會比較檔案時間,決定是否重新建立target
  若該項目並非檔案,則為fake項目,而不會建立target檔案。(利用 .PHONY來指定fake項目)

dependencies: make根據這些項目決定是否重新編譯
  建立target前,必定先檢查的項目。可以不指定。

commands: 建立的指令
  必定以 \Tab 開頭(Tab開頭的都會被視為Shell script)
  每條command會啟動一個新的Shell,可以用; 將指令寫在同一行,或是 &&

● PHONY:

.PHONY: clean
clean:
<tab>rm *.o

  不論檔案時間必定執行,不會建立target

● 特別字元:

  @: 不顯示執行的命令
  -:   即使指令出錯,也不中斷make

.PHONY: clean
clean:
    @echo "Clean..."
    -rm *.o

● 隱性法則:

foo.o: common.h
    gcc -c foo.c

由於產生foo.o的指令就是foo.c,因此在make中可以簡化為:

foo.o: common.h

也可以用空白指令避免make利用隱性法則編譯
foo.o: common.h
<tab>

● 註解:

以#開頭即為註解

● 變數宣告:(macro)

利用=指定
target = foo
$(target): command
<tab>gcc -o $(target) foo.c

●Make參數:

可以用參數蓋過makefile裡的參數
make CFLAGS="-g -O2"

可以在make後接要重新建立的target
make clean



2015年3月31日 星期二

[OpenGL]Visual studio 環境設定與Error隨手記


1. visual studio 製作執行檔:
將debug模式調整為release模式


ref: https://sites.google.com/site/sjdsalg/materials/program/generateexe


2. 增加library和 link:


ref : https://social.msdn.microsoft.com/Forums/vstudio/en-US/a494abb8-3561-4ebe-9eb0-6f644a679862/visual-studio-2010-professional-how-to-add-include-directory-for-all-projects?forum=vcgeneral

3. Error: 'C:\Windows\SysWOW64\ntdll.dll'。找不到或無法開啟 PDB 檔案。



ref: http://vs-imaxlive.blogspot.tw/2013/06/cwindowssyswow64ntdlldll-pdb.html


4. Error: error LNK2001: unresolved external symbol __imp____glewBindBuffer

確定glew.lib在有在library中
(glew32.lib)

ref : http://stackoverflow.com/questions/16390078/build-error-when-trying-to-run-an-opengl-example-in-vc

2015年3月22日 星期日

jquery-flip


Reference link:
http://nnattawat.github.io/flip/

利用jquery可以達到flip的效果
1. 下載套件
網站最上方下載
或是在Github
https://github.com/nnattawat/flip

2. 在html 載入需要的連結和檔案
 <script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
 <script src="dist/jquery.flip.js"></script>

(第一行為jquery,第二行為flip套件)
(第二個套件要注意檔案位置)

3. 對於需要翻轉的block,需設成下列形式:
    <div id="card-2" class="card">
      <div class="front">
         Front content
      </div>
      <div class="back">
         Back content
      </div>
    </div>

即需要翻轉的block內容必須包含兩個<div>,一個class為front,一個class為back


4. 設置flip()
    <script type="text/javascript">
    $(function(){
      $("#card-2").flip({
        axis: "y",
        reverse: true,
        trigger: "click"
      });
    });
    </script>


5. 其他Option:
Attribute Possible value Default Description
axis y, x y The axis that you want to rotate around
trigger click, hover, manual click Event that activates the flip action. Using manual means that you have to activate it via javascript.
reverse true, false false Set to true if you want to reverse the direction of the flip.
speed any integer 500 Duration in milisecond for flipping dom

若將trigger設為manual,則需要在javascript呼叫flip(),如:
$("#card-2").flip(true);  //back side

$("#card-2").flip(false);  //front side

6. demo
http://m100.nthu.edu.tw/~s100062209/hw3.html

2015年3月15日 星期日

Regular Expression-javascript

可以參考
http://www.w3schools.com/jsref/jsref_obj_regexp.asp

● /pattern/modifiers
  ○ pattern
    ◎ 運算元:
      [abc]:找到有abc字元(a或b或c)
      [^abc]:不包含abc字元
      a|b    :找到a或b
      n+    :找到n至少一次
      n*    :找到n 零次或一次以上
      n?    :找到n 零次或一次
   
   
    ◎ 特殊符號:
      \w :word-character (a-z, A-Z, 0-9,_)
      \W:非word-character
      \d :digit (0-9)
      \D:非digit
      \s :空白
      \S:非空白
      \b:在開頭或結尾
      \B:不在開頭或結尾
      \n:換行符號
      \r: carriage return
      \t: tab
   

  ○ modifiers
     i: 忽略大小寫
     g: 找出所有pattern,而非只找到一個就停
     m: multiline matching,和換行有關

● javascript RegExp Object:
  ○建構:
    var res = new RegExp(pattern, modifiers);

  ○Method:
    1. exec()
    2. test():  match時回傳true,否則回傳false
    3. toString()
e.g.
  var res = new RegExp(/[2-9]/g);
  if (res.test(input_txt.value))
  {
    alert("Sorry! " + input_txt.value + " is not accepted");
    clear_in();
    return;
  }
● replace():  (String method)
1.  str.replace("str1", "str2"):str1被取代為str2
2.  str.replace(/pattern/modifiers, "str1"):match的pattern取代為str1
3.  str.replace(res, "str1"):利用ResExp object,將match到的pattern取代為str1
4. 利用$1可以取得()中match到的pattern  (一定要用小括號)
e.g. (將2進位數字轉換成number) 
  str.replace(/([0-1]+)/g, "parseInt(\"$1\",2)")
  eval(str)

● test($str):  (regexp method)
  用來測試string是否包含pattern(回傳true或false)
e.g.
 var res = new RegExp(pattern, modifier);
 var str = "...";
 res.test(str);
● match: (String method)
  用來測試string 是否包含pattern(回傳match的pattern)
e.g.
 var str = "...";
 var res = str.match(/pattern/modifier);


CodeMirror範例

Code Mirror
可以在網頁中顯示程式碼,並且可以編輯

以下是一個範例:
1. 首先將Code Mirror 載下來
 http://codemirror.net/index.html

2.在網頁<head>中載入檔案
<link href="codemirror-5.0/lib/codemirror.css" rel="stylesheet"></link>
<script src="codemirror-5.0/lib/codemirror.js"></script>
<script src="codemirror-5.0/mode/javascript/javascript.js"></script>
<script src="codemirror-5.0/addon/edit/matchbrackets.js"></script>

3. 以及設置style

<style>
  .CodeMirror { height: auto; border: 1px solid #ddd; }
  .CodeMirror-scroll { max-height: 200px; }
  .CodeMirror pre { padding-left: 7px; line-height: 1.25; }
</style>

4. 在<body>需要插入程式碼的地方加上textarea
<form>
<textarea id="code_input" >
//this block allow you to define function
//please define your function in javascript
</textarea>
</form>

5. 並且加上script,讓它可以讀值
<script>
var editor = CodeMirror.fromTextArea(document.getElementById("code_input"), {
  lineNumbers: true,
  mode: "text/javascript",
  matchBrackets: true,
});
editor.on("blur", enable_key);
editor.on("focus", disable_key);
</script>

6. 即可完成

或是連到網站觀看成果
http://m100.nthu.edu.tw/~s100062209/cal.html

note1. code_input可以改為自己定義的id
note2. enable_key 則是自己定義的funciton,表示在blur這個事件時執行
note3. disable_key和上述同理
note4. 文字區塊更改:
  取值: editor.getValue();
  設值: editor.SetValue("   ");


2015年3月12日 星期四

Cryptography and Network Security-ch8

Ch8. Encipherment using modern symmetric-key cipher

5 mode to used with modern block cipher:
1.  Electronic Codebook (ECB):每個block單獨去加密
   加密:


   解密:


2. Cipher Block Chaining (CBC):將加密後的block和下一次的plaintext做XOR再加密(第一組需要initial vector)

加密:

解密:


3. Cipher Feedback (CFB):對shift register加密,加密後再和plaintext做XOR,得到的ciphertext再傳到下一個shift register

 加密:



4. Output Feedback(OFB):和CFB類似,但是傳到下一個block的是shift register加密後的temporary register

 加密:


5. Counter (CTR):直接利用counter加密,再和plaintext做XOR
 加密:



● mode 3, 4, 5 plaintext 中一個bit計算完及可以傳送這個bit,因此類似stream cipher
● ciphertext stealing:最後兩個block可以交換順序計算,進而避免最後一個block因位數不足做padding(也就是不需要做padding)
  在ECB mode下:
    X = Ek(PN-1)              → CN = headm(X)
    Y = PN | tailn-m(X)    → CN-1 = EK(Y)

  在CBC mode下:
    U = PN-1 + CN-2      →  X = Ek(U)     →  CN = headm(X)
    V = PN | padn-m(0)   → Y = X + V      → CN-1 = Ek(Y)

ref: wiki
http://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Electronic_Codebook_.28ECB.29

2015年3月9日 星期一

Regular Expression-perl

文章轉
http://user.frdm.info/ckhung/b/re/rules.php
http://ind.ntou.edu.tw/~dada/cgi/Perlsynx.htm


/regular expression/expression modifier
● Modifiers:
  g: Match globally, i.e. find all occurrences. (搜尋全部)
  i: Makes the search case-insensitive. (區分大小寫)
  m: If the string has new-line characters embedded within it, the metacharacters ^ and $ will not work correctly. This modifier tells Perl to treat this line as a multiple line. (包含多行)
  o: Only compile pattern once.
  s: The character . matches any character except a new line. This modifier treats this line as a single line, which allows . to match a new-line character. 
  x: Allows white space in the expression.

● regular expression:
  ○特殊意義字元:
    \  跳脫字元
    ^ 以...為開頭
    . 除了換行的所有字串
    $ 以...為結尾
    | 或
    [] 搜尋誇號中字元
    () 括號

  ○ 字元數量關係:
    * 大於等於0次
    + 大於等於一次
    ? 0或1次
    {n} n次
    {n.} 至少n次
    {n, m} 最少n次,不超過m次

  ○ 特定字元:
    \r: carriage return(CR)
    \n: 換行
    \t: tab
    \w: 英文字母
    \W: 非英文字母
    \s white space
    \S non-white space
    \d: 數字
    \D: 非數字
    \b:  word boundary
    \B: non-word boundary
    \033: octal char
    \x1B: hex char


字串比對:

1. $string =~ /regular expression/expression modifier
2. if /regular expression/expression modifier

example:
(將C #define 轉換成verilog #parameter)

###read file
open($fp, "test1.c");
foreach $word (<$fp>)
{
 if ($word=~/^#define (\w+) (\d+)/)
 {
  $word =~ s/define (\w+) (\d+)\n/parameter/g;
  $word = $word . sprintf(" %s = 2'b%b\n",$1 , $2);;
  print "$word";
 }
}
close($fp);


字串取代:

s/PATTERN/REPLACEMENT/egimox