Author Archives: Enrico

nodejs quick install

nodejs 在linux安裝如果不透過安裝包或是yum的包管理來安裝,最快的方式是直接從官方網站抓binary dist,以下是在CentOS 6.9測試 https://nodejs.org/dist/latest-v10.x/Linux: https://nodejs.org/dist/latest-v10.x/node-v10.17.0-linux-x64.tar.gz tar xf node-v10.17.0-linux-x64.tar.gz 將 bin/node copy到 /usr/local/bin cp node /usr/local/bin/ install npm 解壓縮的binary包有帶npm,用這個npm來抓取最新的npm安裝到系統 ./npm -g install npm 如果只是拿nodejs來執行的話,npm並不需要裝,只需要node執行檔。在壓縮包裏有 include,那是需要build binary addon時會用到,可直接複製到 /usr/local/include cp -r include/node/ /usr/local/include/

Posted in nodejs | Leave a comment

C語言的struct hack整理

在C語言中,有一個技巧叫struct hack,主要是要實現動態大小的struct 要紀錄的資訊有名字欄位,可以這樣設計 但是這邊假設name存的字串最長是31 + 1 bytes (null terminated string),如果要能動態長度就要改用指標處理,例如 這個的缺點就是 name的記憶體配置是另外配置,和person_info不在同一處,可能無法利用到cpu cache。 struct hack的作法是讓尾端可以動態大小,此時在struct最後面定義一個place holder malloc 會allocate 一塊空間包含struct member所需的,剩下的空間可以用name來存取 注意這裡name的用途是拿來做place holder,不用另外計算struct之後的起始位置,因為有時候有alignment padding的問題。 struct hack的作法只能用在最後一個member是不定大小。在C99 可以用flexible array member來描述 char name[]; flexible array member算是incomplete array type,所以不能直接sizeof §6.2.5 #12 An array type … Continue reading

Posted in C Language | Leave a comment

Diffie-Hellman Key Exchange整理

在雙方要做加密通信時,需要共同協商出密鑰,無論是加解密同一把,或是加密/解密分開, 都需要某種方式讓雙方得到密鑰。最簡單的做法就是透過事先設定好密鑰,或是另外透過安全的傳輸傳輸密鑰。 但是密鑰要在安全傳輸的通道裡傳輸,安全傳輸的通道怎麼來的,例如可能是實體的額外的線路,也可能是兩方私下先共同約定好特定密鑰的加密通道,不過這兩種做法似乎都要先約定好,是否有辦法直接在不安全的通道裡在雙方都沒有事先溝通的參數的情況下協商出一把只有雙方才知道的密鑰呢? 這個問題等同於如果有一個人在偷聽兩個陌生人的加密對話,從頭聽到尾(包括兩個人加密協商階段也能偷聽到的情況下),是否可以讓偷聽的人無法知道兩個陌生人原始的對話,也就是避免偷聽的人解開兩人通信使用的密鑰,答案是可以,這邊必須注意的是 – 偷聽是只在旁邊偷偷觀察,不是MTIM中間人攻擊 關於這個問題可參考 https://en.wikipedia.org/wiki/Key_exchange 比較多詳細的描述 Diffie-Hellman密鑰交換算法可以解決這個問題: 即使是在不安全的通道裡雙方還是可以協商出一同一把密鑰,而竊聽的人無法輕易得知那把密鑰。 他的做法是給定兩個數 p, g p是質數、g是 primitive root modulo p https://en.wikipedia.org/wiki/Primitive_root_modulo_n Alice、Bob透過不安全的通道交換p, g的資訊(所以這個資訊可能被第三者知道) Alice挑了一個數字a 計算出 A=g^a mod p,然後將A數字給BobBob挑了一個數字b 計算出 B=g^b mod p,然後將B數字給AliceAlice將拿到的B 計算出 K1=B^a mod pBob將拿到的A 計算出 K2=A^b mod p事實上 K1 … Continue reading

Posted in General | Leave a comment

v8 build from source

注意python要2.7 否則在gm.py那一步會失敗 Linux 安裝build環境https://commondatastorage.googleapis.com/chrome-infra-docs/flat/depot_tools/docs/html/depot_tools_tutorial.html git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git export PATH=$PATH:~/depot_tools mkdir v8cd v8fetch v8 注意 在~/v8 下會有.gclient,所以工作目錄不要在home solutions = [ { “url”: “https://chromium.googlesource.com/v8/v8.git”, “managed”: False, “name”: “v8”, “deps_file”: “DEPS”, “custom_deps”: {}, }, ] gclient sync cd v8./build/install-build-deps.sh 下面這步是helper script,產生build files, compile, … Continue reading

Posted in nodejs | Leave a comment

detect elevated privilege execution in windows

要偵測是否是 UAC Privilege escalation,直接檢查是否Administrator並不可行,而是透過執行特定command來確認目前的執行是否是elevated privilege 這種作法在nodejs中不必在整合C API 參考: https://stackoverflow.com/questions/4051883/batch-script-how-to-check-for-admin-rights

Posted in nodejs | Leave a comment

Keywords (annotated)

The above tokens (case sensitive) are reserved (in translation phases 7 and 8) for use as keywords, and shall not be used otherwise. keywords出現在phase7開始,在之前是屬於preprocessor token的identifier 上面的紅框是C99新增的keyword,_Bool定義成_Bool而不是bool,主要的原因是在C89的標準中,對於reserved identifier是用 底線+大寫字母開頭 或是 雙底線。提供了後續標準新增的關鍵字避免使用上與使用者衝突。另外在標準中提到 Implementation-defined keywords shall have the form of an identifier reserved … Continue reading

Posted in C Language | Leave a comment

Identifier (annotated)

參考: https://en.cppreference.com/w/c/language/identifier 有關C99 identifier的定義 6.4.2.1 An identifier is a sequence of nondigit characters (including the underscore _, the lowercase and uppercase Latin letters, and other characters) and digits. C99也支援universal-character-name \u or \U開頭的UCN來作為identifier的字元(可以在identifier開頭) \u接4個hex \U接8個hex 當然也考慮了上面提到的不能digit開頭的限制 Each universal character name in … Continue reading

Posted in C Language | Leave a comment

C99 Terms, definitions, and symbols (annotated)

整理一下C99在Terms, definitions, and symbols提到的一些名詞定義的重點整理,總共有19個。 3.1 access 在標準中提到的access 即是對object存取,read or modify。這裡modify包含對object存入相同的值 ‘Modify’ includes the case where the new value being stored is the same as the previous value 另外是如果Expressions沒有被evaluate就沒有access(例如short circuit evaluation) Expressions that are not evaluated do not access objects. 3.2 … Continue reading

Posted in C Language | Leave a comment

Phases of translation (annotated)

在C99的translation phases共分8步 。translation phases描述了從C source code到program image的處理流程。 參考整理 https://en.cppreference.com/w/c/language/translation_phases &ISO C99標準 The C source file is processed by the compiler as if the following phases take place, in this exact order. Actual implementation may combine these actions or process them … Continue reading

Posted in C Language | Leave a comment

ASCII (annotated)

參考 https://en.cppreference.com/w/c/language/ascii 作一些補註 ASCII定義0x00-0x7F,超過的部分(第8bit)算是extended 8bit,看不同的標準有不同的定義,早期有些拿來當成parity bit 總共有128個character,其中95個是printable(0x20-0x7E),前32個和最後一個是control characters (0x00-0x1F/0-31, 0x7F/127)。 數字的安排對應BCD的bit pattern(加上了011->hex 3),簡單來說0 -> 0x30、9-> 0x39,只要 &0x0F 就可以得到數字。 可以用每32個字元為單位分成四快來看,第一塊是控制字元、第二塊(數字)第三塊(大寫字母)他的順序安排有些歷史因素,可對比以下的DEC SIXBIT、第四塊是小寫字母。 上面順便列出EBCDIC(發音: eb-SEE-dick) 供比較,主要使用在IBM mainframe上,注意大部分可印字元集中在後半區,並且A-Z, a-z並不是連續的

Posted in C Language | Leave a comment