2007年12月3日 星期一

haserl:Html And Shell Embedded Report Language

haserl:Html And Shell Embedded Report Language,一個小型的 CGI wrapper,可以直接在 HTML 文件中嵌入 shell script 語法,使用用 POST 或 GET 傳入的變數會自動轉換成環境變數,在標記內( <? ?> )的語法則視為 shell script 來執行。

haserl 網址:http://haserl.sourceforge.net/



範例程式
#!/usr/bin/haserl
<?
temp='Html And Shell Embedded Report Language '

echo $temp

echo $FORM_foo

?>
<html>
<body>
<form action=1.cgi method=post>
<input type=text name=foo></input>
<input type=submit>
</form>
</body>
</html>

2007年10月26日 星期五

NetBIOS over TCP/IP 名稱解析 IP 位址方法

NetBIOS over TCP/IP 名稱解析 IP 位址方法
1.在LMHOSTS檔案中尋找。
2.區域網路中廣播。
3.透過WINS伺服器。

MS Windows系統使用下列四種方法解析:
B Mode(Broadcast、ox1)
不適合大型網路,查詢順序如下
1.LMHOSTS CACHE
2.Broadcast
3.LMHOSTS

P Mode(per-to-per、0x2)
僅使用WINS伺服器查詢。

M Mode(mixed、0x4)
聯合使用B Mode及P Mode,預設使用B Mode,如果不能使用Broadcast,則使用P Mode的WINS伺服器。

H Mode(hybrid、0x8)
聯合使用B Mode及P Mode,預設使用P Mode。

2007年10月23日 星期二

計算中文 big5 在編碼中的順序

//計算中文 big5 在編碼中的順序
//高位元區段 81-8D 8E-A0 A1-FE ---->126個
//低位元區段 40-7E A1-FE ---> 157 個
//先計算高位元的距離 公式:(高位元 - 0x81) * 157
//再計算低位元距離 分為兩組 40-7E A1-FE
//0x40=64 0x7E=126 63個
//0xA1=161 0xFE=254 94個
//落在 0x40-0x7E 直接加上 (低位元的值 - 0x40 + 1)
//落在 0xA1-0xFE 63 + (低位元的值 - 0xA1 + 1)

// PHP

<?php
function big5order($big5){
$big5_h = $big5[0] . $big5[1];
$big5_l = $big5[2] . $big5[3];int big5order(unsigned short big5){
unsigned short big5order;
unsigned char hi, lo;

hi = (big5 & 0xFF00) >> 0x08;
lo = big5 &0x00FF;

big5order = (hi - 0x81) * 157;

if(lo>=0x40 && lo<=0x7E)
big5order = big5order + lo - 0x40 + 1;
else if(lo>=0xA1 && lo<=0xFE)
big5order = big5order + 63 + lo - 0xA1 + 1;

return big5order;
}

?>

// C/C++

int big5order(unsigned short big5){
unsigned short big5order;
unsigned char hi, lo;

hi = (big5 & 0xFF00) >> 0x08;
if(!(hi>=0x81 && hi<=0xFE)) return -1;

lo = big5 &0x00FF;
if(!((lo>=0x40 && lo<=0x7E) (lo>=0xA1 && lo<=0xFE))) return -2;

big5order = (hi - 0x81) * 157;

if(lo>=0x40 && lo<=0x7E)
big5order = big5order + lo - 0x40 + 1;
else if(lo>=0xA1 && lo<=0xFE)
big5order = big5order + 63 + lo - 0xA1 + 1;

return big5order;
}

2007年10月18日 星期四

將字串型態表示的16位元數字轉為整數資料型態

將字串型態表示的16位元數字轉為整數資料型態

傳入的參數可為 " ffbc","FFbC","0xFFFC", "12F4"


//字元 HEX 轉 OCT
int chex2oct(char ch[]){
unsigned int i, j[10], leng, leng_s=0, chex=0;
unsigned int hex16[] = {1, 16, 256, 4096, 65536, 1048576, 16777216,268435456};
leng = strlen(ch);

for(i=0; i<leng; i++) {
if(ch[i]>47 && ch[i]<58) j[leng_s++] = ch[i] - 48;
else {
switch (ch[i]){
case 'a': case 'A': j[leng_s++] = 10; break;
case 'b': case 'B': j[leng_s++] = 11; break;
case 'c': case 'C': j[leng_s++] = 12; break;
case 'd': case 'D': j[leng_s++] = 13; break;
case 'e': case 'E': j[leng_s++] = 14; break;
case 'f': case 'F': j[leng_s++] = 15; break;
}
}
}

for(i=0; i<leng_s; i++) chex += j[i] * hex16[leng_s - i - 1];
return chex;
}

2007年9月7日 星期五

Linux syslog 程式設計筆記

LINUX下執行的程式除了自己輸出系統日誌到特定檔案外,還可以透過 syslogd來達成這個功能,免除了自行開檔關檔的工作。

程式要使用系統日誌功能,只需要在程式啟動時使用openlog函數來連接syslogd程式,後面隨時用syslog函數傳人系統日誌就行了。

相關的函式有 openlog,syslog,closelog。

#include <syslog.h>

void openlog(const char *ident, int option, int facility)

開啟一個到系統日誌記錄程式的連接,開啟後就可使用用syslog或vsyslog函數在系統日誌中添加資訊

ident 是一個標記,ident所表示的字串將固定地加在每行日誌的前面以標識這個日誌,通常就寫成當前程式的名稱以作標記。

option 是下列值取與運算的結果:

LOG_CONS
Write directly to system console if there is an error while sending to system logger.

LOG_NDELAY
Open the connection immediately (normally, the connection is opened when the first message is logged).

LOG_NOWAIT
Don’t wait for child processes that may have been created while logging the message. (The GNU C library does not create a
child process, so this option has no effect on Linux.)

LOG_ODELAY
The converse of LOG_NDELAY; opening of the connection is delayed until syslog() is called. (This is the default, and need
not be specified.)

LOG_PERROR
(Not in SUSv3.) Print to stderr as well.

LOG_PID
Include PID with each message.


facility 指明記錄日誌的程式的類型。facility is the default facility code for this connection. A syslog on this connection that specifies default facility causes this facility to be associated with the message. See syslog for possible values. A value of zero means the default default, which is LOG_USER.

#include <syslog.h>

void syslog(int priority, const char *format, ...)

把日誌消息發給系統程式輸出至syslogd記錄

priority 日誌消息的緊急級別

LOG_USER

A miscellaneous user process
LOG_MAIL

Mail
LOG_DAEMON

A miscellaneous system daemon
LOG_AUTH

Security (authorization)
LOG_SYSLOG

Syslog
LOG_LPR

Central printer
LOG_NEWS

Network news (e.g. Usenet)
LOG_UUCP

UUCP
LOG_CRON

Cron and At
LOG_AUTHPRIV

Private security (authorization)
LOG_FTP

Ftp server
LOG_LOCAL0

Locally defined
LOG_LOCAL1

Locally defined
LOG_LOCAL2

Locally defined
LOG_LOCAL3

Locally defined
LOG_LOCAL4

Locally defined
LOG_LOCAL5

Locally defined
LOG_LOCAL6

Locally defined
LOG_LOCAL7

Locally defined

format 日誌消息的格式,之後是格式對應的參數類似printf函數。

#include <syslog.h>

void closelog(void)

關閉 開啟的 openlog 連接

#include <stdarg.h>

void vsyslog(int priority, const char *format, va_list ap);

把日誌消息發給系統程式輸出至syslogd記錄


簡單範例

#include <syslog.h>

int main(int argc, char **argv)
{
openlog("test", LOG_CONS | LOG_PID, 0);
syslog(LOG_INFO, "This is a syslog test message generated by program '%s'n", argv[0]);
closelog();

return 0;
}

結果輸出一般是 /var/log/messages 或是 /var/log/syslog

視 /etc/syslog.conf 中的設定,下面的設定是輸出至 /var/log/messages

# Log all kernel messages to the console.
# Logging much else clutters up the screen.
#kern.* /dev/console

# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
*.info;mail.none;authpriv.none;cron.none /var/log/messages

# The authpriv file has restricted access.
authpriv.* /var/log/secure

# Log all the mail messages in one place.
mail.* -/var/log/maillog


# Log cron stuff
cron.* /var/log/cron

# Everybody gets emergency messages
*.emerg *

# Save news errors of level crit and higher in a special file.
uucp,news.crit /var/log/spooler

# Save boot messages also to boot.log
local7.* /var/log/boot.log

補充

Linux有關syslog的函式沒有提供syslog寫到遠端syslogd的功能,如果在程式中想要將syslog寫到遠端的syslog 伺服器,可以使用socket udp(514)的函式,將訊息送出。

參考資料

http://insecure.org/sploits/aix.generic.syslogd.problem.html

http://zhoulifa.bokee.com/6104993.html

http://www.dusek.ch/manual/glibc/libc_18.html

http://www.balabit.com/network-security/syslog-ng/

2007年8月31日 星期五

C Function - split

ANSI C 中缺少類似 PHP 中的 split 處理函式,在字串處理中卻又常使用,以下是簡單範例

str:欲被 split 的字串

delim:用來分隔的字串

num:設定分隔出來的子字串最大的數目,如果 <1則不限制

傳回值為一儲存分割結果的字串陣列,當陣列中的值為 NULL表示結束。

char** split(char* str, const char* delim, int num){
char** tab=NULL;
int flag;
unsigned int pos_num=0, pos_num2=0;
unsigned long int i, j, k, str_leng, delim_leng, *pos;

str_leng = strlen(str);
delim_leng = strlen(delim);

pos = (unsigned long int*)malloc(sizeof(unsigned long int)*(int)(str_leng/delim_leng+1));

i = 0;

while(i<=(str_leng - delim_leng)) {
flag = 0;
for(j=0; j<delim_leng; j++) {
if(str[i+j]!=delim[j]) { flag=-1; break; }
flag = 1;
}
if(flag == 1) {
i = i + delim_leng;
pos[pos_num++] = i - delim_leng;
if(num<1) continue;
else if(num==pos_num) break;
} else i++;
}

if(pos_num==0) {
tab = (char **)malloc(sizeof(char*)*2);
tab[0] = (char *)malloc(sizeof(char)*(str_leng+1));
strcpy(tab[0], str);
tab[1] = NULL;
return tab;
}

tab = (char **)malloc(sizeof(char*)*pos_num);

if(pos[0]!=0) {
tab[pos_num2] = (char *)malloc(sizeof(char)*pos[0]);
for(i=0, j=0; i<pos[0]; i++, j++) tab[pos_num2][j] = str[i];
tab[pos_num2][j] = '\0';
pos_num2++;
}

for(i=1; i<pos_num; i++) {
if(pos[i] - (pos[i-1] + delim_leng) >0) {
tab[pos_num2] = (char *)malloc(sizeof(char)*(pos[i] - (pos[i-1] + delim_leng)) + 1 + 1);
for(j=pos[i-1] + delim_leng, k=0; j<pos[i]; j++, k++) tab[pos_num2][k] = str[j];
tab[pos_num2][k] = '\0';
pos_num2++;
}
}

if((pos[pos_num-1] + delim_leng - 1) < str_leng - 1) {
tab[pos_num2] = (char *)malloc(sizeof(char)*(pos[pos_num-1] + delim_leng - 1) + 1 + 1);
for(j=pos[pos_num - 1] + delim_leng, i=0; j<str_leng; j++, i++) tab[pos_num2][i] = str[j];
tab[pos_num2][i] = '\0';
pos_num2++;
}

tab[pos_num2] = NULL;
return tab;
}

使用範例

char **tab;
int i;

tab = split("1,2,3,4,5,6,7,8,9,0", ",", -1);
i = 0;
while(tab[i]!=NULL) printf("%d = %s =\n", i, tab[i++]);

C Function - fgetc、fgets讀取整個檔案內容

fgetc:傳回所讀到的字元,傳回值如果是 EOF,可能是有錯誤發生或是檔案終止(end-of-file),所以應該利用 feof 或是 ferror 來判定錯誤發生或是檔案終止。

fgets: 傳回所讀到的字串,傳回值如果是 NULL,可能是有錯誤發生或是檔案終止(end-of-file),所以應該利用 feof 或是 ferror 來判定錯誤發生或是檔案終止。

使用 fgetc 讀取整個檔案 

#include "stdio.h"
main()
{
FILE *fp;
ch ch;

if((fp=fopen("test", "r"))==NULL)
{
printf("file cannot be opened\n");
exit(1);
}

while((ch=fgetc(fp))!=EOF) fputc(ch,stdout);
fclose(fp);
}

使用 fgets 讀取整個檔案
#include "stdio.h"
main()
{
FILE *fp;
char str[128];

if((fp=fopen("test", "r"))==NULL)
{
printf("cannot open file\n");
exit(1);
}

while(!feof(fp))
if(fgets(str,128,fp)!=NULL) printf("%s",str);

fclose(fp);
}