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月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);


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



2015年3月2日 星期一

Formal language and automata - ch3

Regular language and regular grammar

Regular expression:

● definition:
 1. 元素:φ, λ, a ∈ Σ 為Regular expression
 2. 運算元:若r1, r2為Regular expression
    r1 + r2  (union)
    r1 ⋅ r2  (concatenation)
    r1*       (star-closure)
   (r1)
  均為regular expression
3. 根據有限次數的1和2造出的字串均為regular expression

● language associated with regular expression:
 1. φ is a regular expression {}  (empty set)
 2. λ is a regular expression {λ}
 3. for a ∈ Σ, a is a regular expression denote  {a}

 4. L(r1 + r2) = L(r1) ∪ L(r2)
 5. L(r1 ⋅ r2) = L(r1)L(r2)
 6. L((r1)) = L(r1)
 7. L(r1*) = (L(r1))*

● operation priority:
  star closure > concatenation > union

Regular expression and regular language:

Regular expression -> Regular language

● 所有regular expression 定義的都可以畫成nfa
1. nfa accept φ

2. nfa accept {λ}

3. nfa accept {a}

4. nfa accept L(r1 + r2)

5. nfa accept L(r1 ‧ r2)

6. nfa accept L((r1)):直接去掉括號
7. nfa accept (L(r1))*

Regular language-> Regular expression

● Generalized transition graph(GTG):一個label為regular expression的transition graph

● nfa轉成complete GTG:將所有沒有連接的edge連上並標上φ
 e.g.

轉換成
● 2 state GTG轉換成regular expression:
r = r1r2 (r4 + r3r1*r2)*

● 3 state(或以上) complete GTG轉換成regular expression:將state減少,直到剩兩個state(initial state和final state)
每次減少state時,考慮將會經過的state,將它加到graph的其他路徑上
e.g.
reduce成:

○完整procedure:
  1. 將nfa轉換成單一final state的GTG,並使它complete
  2. 若GTG state為2,直接代公式
  3. 若GTG state為3,選擇一個要刪除的state(qi)(非final 也非initial的state)
  對於另外兩個state,考慮經過qi的路線
   r1 + r2r3*r4
(均為這種type)
 4. 若GTG大於4,選擇要刪除的state,對所有state均套用 r1 + r2r3*r4找關係
     降為3個state後,再用step3

Regular grammar:

● definition:
 ○left-linear grammar:每次production左側最多只出現1個variable,variable為最左側的symbol
  e.g.:
    A →Bx
    A →x
 ○right-linear grammar:每次production左側最多只出現1個variable,variable為最右側的symbol
 ○regular grammar:left-linear 或 right-linear
 ○linear grammar:每次production左側最多只出現1個variable

● right-linear grammar 轉換成 regular language(nfa):
  variable → state
  alphabet → state轉換的label

●  regular language轉換成 right-linear grammar:
  δ(qi, aj) = qk 轉換成 qi → ajqk
  (若 qk 為final state:則轉換成qk →λ)

● regular language轉換成 left-linear grammar:
  略

● regular expression <=> dfa or nfa <=> regular grammar
  為等價,可以互相轉換