http://www.cnblogs.com/wangkangluo1/archive/2011/06/29/2093727.html
pack in c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include<stdint.h> #include<stdlib.h> uint32_t pack_uint32(char* data) { uint16_t num = 0x0102; char c2[2] = {0x01, 0x02}; uint16_t num2 = *(uint16_t*)c2; if (num == num2) { //这个判断的结果可以保存到一个static变量里面,方便下次使用 return *(uint32_t*)data; } char c4[4] = {data[3], data[2], data[1], data[0]}; return *(uint32_t*)c4; } |
lua学习
module机制
1 |
module "mymodule" |
等同于
1 2 3 4 5 |
local modname = “mymodule” – 定义模块名 local M = {} -- 定义用于返回的模块表 _G[modname] = M -- 将模块表加入到全局变量中 package.loaded[modname] = M -- 将模块表加入到package.loaded中,防止多次加载 setfenv(1,M) -- 将模块表设置为函数的环境表,这使得模块中的所有操作是以在模块表中的,这样定义函数就直接定义在模块表中 |
关于nginx+luajit的折腾
缘起
需要写一个lua版的SDK在nginx中使用
基本功能
- 通过http请求下载关于SDK的配置文件并cache起来
- http的响应信息是json格式的
- 需要用到rsa/des/rc4解密
系统环境
ubuntu14.04
相关资料
- http://www.kyne.com.au/~mark/software/lua-cjson-manual.html
- http://www.kyne.com.au/~mark/software/lua-cjson.php
- zlib下载地址: http://www.zlib.net/
- 安装luajit: http://luajit.org/install.html
学到了什么
- lua基础知识
- luarocks 包管理工具: https://rocks.moonscript.org/
- luaunit 单元测试: https://github.com/bluebird75/luaunit
- lua-cjson: (json4lua 无法在luajit下使用)
1luarocks install lua-cjson
- nginx的个性(非阻塞)
- Install the latest nginx from source for Ubuntu 14.04
- 对于openssl/zlib的依赖,我是通过指定源码路径的方式安装的
1./configure --with-ld-opt='-Wl,-rpath,/home/phpor/program/luajit2.0/lib' --add-module=../ngx_devel_kit-0.2.19 --add-module=../lua-nginx-module-0.9.15 --prefix=/home/phpor/program/nginx-1.7.10 --with-http_ssl_module --with-openssl=../../temp/openssl-0.9.8zc --with-zlib=../zlib-1.2.8 - nginx编译依赖的luajit是luajit编译后的路径,所以需要先编译luajit
- 对于openssl/zlib的依赖,我是通过指定源码路径的方式安装的
- nginx-lua-module : http://wiki.nginx.org/HttpLuaModule
- resolver 指令 :http://serverfault.com/questions/484444/using-a-server-name-variable-in-an-nginx-proxy-pass-config
- Install the latest nginx from source for Ubuntu 14.04
luarocks 代理设置
luarocks是lua实现的一个lua包管理工具。对于包管理工具,下载操作是少不了的,对于墙内的那些人来讲,proxy自然就是少不了的。
/etc/luarocks/config.lua 中添加变量 proxy 如下:
1 |
proxy = [[http://10.xx.xx.xx:8888]] |
文件权限之粘滞位
缘起
如果将一个root属主的可执行文件,添加一个属主的粘滞位,那么,由该程序创建的其它进程的euid还是root吗?
测试
test.php
1 2 3 4 |
#!/usr/bin/env php <?php echo "euid: ".posix_geteuid(),"\n"; echo "uid: ". posix_getuid(), "\n"; |
看来是不好使的,即: 粘滞位是不能在程序间继承的
awk之exit
题目
有100个日志文件,每个文件大约1G,每条日志都以 “H:i:s” 的时间格式开头,如:
1 |
05:02:04 xxx yyy zzz |
因为是日志文件,所以肯定以时间为顺序的,现在可以确定的是,在某个文件中存在一条 01:02:03 这个时间点的关于xxx的日志,要找出来 ,如何做?
办法1:
1 |
# for f in *; do awk '/^01:02:03/{}' $f |grep xxx; done |
虽然我们的正则是只匹配行首,算是很快的了,但是,我们还是扫描了整个的100个文件;考虑到每个文件肯定都是有时间顺序的,我们做了大量的无用的工作,其实完全可以提前退出的,加入每10分钟都会有日志出现,则可以优化如下:
办法2:
1 |
# for f in *; do awk '/^01:02:03/{} /^01:1/{exit}' $f |grep xxx; done |
这样的话,没个文件只扫描很少的一部分就可以了
办法3:
该题的sed解法:
1 |
# for f in *; do sed -n '/^01:02:03/p; /^01:1/q}' $f |grep xxx; done |
办法4:
当然,将 xxx 并入正在表达式也是可以的(只是没有上面看起来更一目了然):
1 |
# for f in *; do sed -n '/^01:02:03.xxx/p; /^01:1/q}' $f ; done |
PHP 代码规范
sed 示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
phpor@phpor-Latitude-E6440:~$ echo -e "a\nb\n"|sed -n '1p' # -n 默认不输出 a phpor@phpor-Latitude-E6440:~$ echo -e "a\nb\n"|sed '$d' a b phpor@phpor-Latitude-E6440:~$ echo -e "a\nb\n"|sed '1d' b phpor@phpor-Latitude-E6440:~$ echo -e "a\nb\n"|sed '2,$d' a phpor@phpor-Latitude-E6440:~$ echo -e "a\nb\n"|sed '/a/d' b phpor@phpor-Latitude-E6440:~$ echo -e "a\nb\n"|sed '/^$/d' a b phpor@phpor-Latitude-E6440:~$ echo -e "a\nb\n"|sed '2,$d' a phpor@phpor-Latitude-E6440:~$ echo -e "a\nb\n"|sed '/a/d' b phpor@phpor-Latitude-E6440:~$ echo -e "a\nb\n"|sed '/^$/d' a b phpor@phpor-Latitude-E6440:~$ echo -e "a\nb\n"|sed 's/ //' a b phpor@phpor-Latitude-E6440:~$ echo -e "a b\nb\n"|sed 's/ //' ab b phpor@phpor-Latitude-E6440:~$ echo -e "a b c\nb\n"|sed 's/ //' ab c b phpor@phpor-Latitude-E6440:~$ echo -e "a b c\nb\n"|sed 's/ //g' abc b phpor@phpor-Latitude-E6440:~$ echo -e "a b c\nb\n"|sed 's/^a/&mm/' amm b c b phpor@phpor-Latitude-E6440:~$ echo -e "a b c a c\nb\n"|sed 's/^a/&mm/' amm b c a c b phpor@phpor-Latitude-E6440:~$ echo -e "a b c a c\nb\n"|sed 's/a/&mm/g' amm b c amm c b phpor@phpor-Latitude-E6440:~$ echo -e "a\nb\n"|sed '/a/a\ >m' a m b phpor@phpor-Latitude-E6440:~$ echo -e "a\nb\n"|sed '/a/a\ #在匹配到的行后插入新行,添加多行时,每行(除最后一行)后面都要用 \ 续行 > m\ > n\ > p' a m n p b phpor@phpor-Latitude-E6440:~$ echo -e "a\nb\n"|sed '/a/i\ #在匹配到的行前插入新行 >m\ >n\ >p' m n p a b phpor@phpor-Latitude-E6440:~$ echo -e "a\nb\n"|sed '/a/c\ #用新行替换掉匹配到的行 >m\ >n\ >p' m n p b phpor@phpor-Latitude-E6440:~$ echo -e "a1b2c3\n" |sed -e 's/a//' -e 's/b//' -e's/c//' #使用 -e 选项对统一行进行多次处理 123 phpor@phpor-Latitude-E6440:~$ echo -e "123\n456\n789\nabc" | sed -n '/456/{n;p}' #打印匹配到的那行的下一行 789 phpor@phpor-Latitude-E6440:~$ echo -e "123\n456\n789\nabc" | sed -n '/456/{n;n;p}' #打印匹配到的那行的下一行的下一行 abc phpor@phpor-Latitude-E6440:~$ echo -e "badc\n"|sed 'y/abcd/ABCD/' #该命令与UNIX/Linux中的tr命令类似,字符按照一对一的方式从左到右进行转换 BADC sed -i '/sudo route add/r /tmp/updatephplib.txt' /tmp/Vagrantfile #匹配到某一行后,把制定文件的内容append进去;注意: 1. r命令不能用在花括号中 2. 文件名后面不能有空格 |
在匹配到的行尾添加:
删除匹配后的N行(delete n lines following a pattern)or (delete n lines after match/matches)
1 2 |
echo -e "1\n2\n3\n3\n4\n"|sed '/2/,+2d' 1 |
如果匹配到的行不需要删除
正则替换:
将 “craete time: 1988-02-02 11:22:12″ 替换为”craete time: 1988-02-02T11:22:12”
1 |
sed -r 's/(-[0-9]{2})\ ([0-9]{2})/\1T\2/' |
- 使用 -r
- 反向引用
参考资料: http://blog.csdn.net/holandstone/article/details/8488434
http://stackoverflow.com/questions/4396974/sed-or-awk-delete-n-lines-following-a-pattern
http://stackoverflow.com/questions/8323287/how-can-i-use-sed-to-delete-2-lines-after-match-matches
云安全之PHP encoder
- 把PHP代码放在云上,确实有些不大安全,咋办?
testing-encoders-for-php (虽然文章有点儿老,还是可以看看的)