cgi_functions.c
来自「这是我自己写的用于嵌入式设备的CGI程序」· C语言 代码 · 共 1,932 行 · 第 1/4 页
C
1,932 行
读取失败返回NULL,成功则返回指向字符串的指针 *--------------------------------------------------------------------------*/char* get_var_from_file(char *conf_path, char *get_name, char *get_value){ FILE *conf_fp; char name[128]; char dec_value[1024]={0}; if ( NULL == get_name ) { return NULL; } if (!(conf_fp=fopen(conf_path,"r"))) // open file error { return NULL; } fseek( conf_fp, 0, SEEK_SET ); while (feof(conf_fp) == 0) { fscanf(conf_fp, "%s %s\n", name, get_value); //Read next record if(!strcmp(name, get_name)) { fclose(conf_fp); dec_str ( get_value, dec_value, sizeof(dec_value) ); strcpy (get_value, dec_value); return get_value; } } fclose(conf_fp); //get none value return NULL;}/*--------------------------------------------------------------------------* 从文件中读取名字的值,返回指向值字符串的指针 如果文件中不存在此名字的值,则返回指向默认值字符串的指针 参数非法则返回NULL *--------------------------------------------------------------------------*/char* GetVarFromFile (char *conf_path, char *get_name, char *get_value, char *default_value){ if ( (NULL == conf_path) || (NULL == get_name) || (NULL == default_value) ) return NULL; if ( NULL == get_var_from_file ( conf_path, get_name, get_value )) { strcpy ( get_value, default_value); return default_value; } else return get_value;}/*--------------------------------------------------------------------------* 将值写入文件 打开文件失败返回-1,文件中没有此名字则返回-2,写OK则返回0 *--------------------------------------------------------------------------*/int set_var_to_file(char *conf_path, char *set_name, char *set_value){ char name[128]={0},value[1024]={0},enc_value[1024]={0}; int write_flag = 0; FILE *conf_fp, *temp_fp; char rename_cmd[128]; if ((NULL==conf_path) || (NULL==set_name) || (NULL==set_value)) return -3; enc_str ( set_value, enc_value, sizeof(enc_value) ); if (!(conf_fp=fopen(conf_path,"r"))) { return -1; // open file error } if (!(temp_fp=fopen("/tmp/vartmp","w")))// open file pointer { return -1; // open tmp file error } fseek( conf_fp, 0, SEEK_SET ); while(feof(conf_fp) == 0) { fscanf(conf_fp, "%s %s\n", name, value); // Read next record if ( strcmp(name,"") && strcmp(value,"") ) { if (!strcmp( name, set_name )) { strcpy( value, enc_value ); write_flag =1; } fprintf(temp_fp,"%s %s\n", name, value); } } fflush(temp_fp); fclose(temp_fp); fclose(conf_fp); if ( write_flag ==0) return -2; sprintf(rename_cmd,"cp /tmp/vartmp %s",conf_path); system(rename_cmd); return 0;}/*--------------------------------------------------------------------------* 向文件中写入名字的值 不管此文件是否存在/文件中是否存在此名字的值,都将值写入。文件不存在的时候则建立该文件 建立/打开文件失败返回-1,写OK则返回0 *--------------------------------------------------------------------------*/int SetVarToFile (char *conf_path, char *set_name, char *default_value){ int flag; FILE *conf_fp; flag = set_var_to_file(conf_path, set_name, default_value); if ((flag == -1)||(flag == -2)) { if (!(conf_fp=fopen(conf_path,"w"))) return -1; else { fclose(conf_fp); flag = add_var_to_file(conf_path, set_name, default_value); if ( flag !=0 ) return -1; } } return 0;}/*--------------------------------------------------------------------------* 从文件中读取名字的值,返回指向值字符串的指针 如果文件中不存在此名字的值,则返回指向默认值字符串的指针 并且将默认值写入此文件中。如果文件不存在,则建立之。RB的意思即为rebuild 参数非法/文件未能Rebuild/出错则返回NULL *--------------------------------------------------------------------------*/char* GetVarFromFileRB (char *conf_path, char *get_name, char *get_value, char *default_value){ if ( (NULL == conf_path) || (NULL == get_name) || (NULL == default_value) ) return NULL; if ( NULL == get_var_from_file ( conf_path, get_name, get_value )) { if (SetVarToFile (conf_path, get_name, default_value) != 0) return NULL; strcpy ( get_value, default_value ); return default_value; } else return get_value;}/*--------------------------------------------------------------------------* 将名字和值跟文件中的名字和值比较 如果匹配则返回0, 名字存在,值不匹配则返回-2,名字不存在则返回-3,打开文件出错返回-1 *--------------------------------------------------------------------------*/int match_var_from_file (char *conf_path, char *match_name, char *match_value){ char name[128],value[1024],enc_value[1024]={0}; FILE *conf_fp; if ((NULL==conf_path) || (NULL==match_name) || (NULL==match_value)) return -3; enc_str ( match_value, enc_value, sizeof(enc_value) ); if (!(conf_fp=fopen(conf_path,"r"))) // open file error { return -1; } fseek( conf_fp, 0, SEEK_SET ); while (feof(conf_fp) == 0) { fscanf(conf_fp, "%s %s\n", name,value); //Read next record if(!strcmp(name, match_name)) { //printf("%s = %s \n",name,value); if(!strcmp(value, enc_value)) { fclose(conf_fp); return 0; } else { fclose(conf_fp); return -2; } } } fclose(conf_fp); return -3;}/*--------------------------------------------------------------------------* 将值追加写入文件 打开文件失败返回-1,已经存在返回-2,添加OK则返回0 *--------------------------------------------------------------------------*/int add_var_to_file(char *conf_path, char *add_name, char *add_value){ char *ret; char name[128]={0},value[1024]={0},enc_value[1024]={0}; FILE *conf_fp,*temp_fp; char rename_cmd[128]; char getvalue[1024]; if ((NULL==conf_path) || (NULL==add_name) || (NULL==add_value)) return -3; enc_str ( add_value, enc_value, sizeof(enc_value) ); if (!(conf_fp=fopen(conf_path,"r"))) // open file error { return -1; } if (!(temp_fp=fopen("/tmp/vartemp","w"))) // open file error { fclose(conf_fp); return -1; } ret = get_var_from_file( conf_path, add_name, getvalue); if(ret== NULL) //add new config item { fseek( conf_fp, 0, SEEK_SET ); while( feof(conf_fp) == 0 ) { fscanf(conf_fp, "%s %s\n", name, value); // Read next record fprintf(temp_fp, "%s %s\n", name, value); } fclose(conf_fp); fprintf(temp_fp,"%s %s\n", add_name, enc_value); fflush(temp_fp); fclose(temp_fp); sprintf(rename_cmd,"cp /tmp/vartemp %s",conf_path); system(rename_cmd); return 0; } else { //already existed. return -2; }}/*--------------------------------------------------------------------------* 删除名字和对应的值 打开文件失败返回-1,不存在返回-2,删除OK则返回0 *--------------------------------------------------------------------------*/int del_var_from_file(char *conf_path, char *del_name){ varconfig var_array[CONF_NUM]={0}; int index=0; int i; char *ret; char name[128]={0}, value[128]={0}; FILE *conf_fp, *temp_fp; char rename_cmd[128]; char getvalue[128]; if (!(conf_fp = fopen(conf_path,"r"))) // open file error { return -1; } if (!(temp_fp=fopen("/tmp/vartemp","w")))// open temp file error { return -1; } ret = get_var_from_file( conf_path, del_name, getvalue); if(ret!= NULL) // { fseek( conf_fp, 0, SEEK_SET ); while(feof(conf_fp) == 0) { fscanf( conf_fp, "%s %s\n", name, value); // Read next record strcpy( var_array[index].name, name); strcpy( var_array[index].value, value); //if( !strcasecmp(var_array[index].name, del_name)) if( !strcmp(var_array[index].name, del_name)) { index--; } index++; } fclose(conf_fp); fseek( temp_fp, 0, SEEK_SET ); for(i=0; i<index; i++) { fprintf(temp_fp, "%s %s\n", var_array[i].name, var_array[i].value); } fflush(temp_fp); fclose(temp_fp); sprintf(rename_cmd,"cp /tmp/vartemp %s", conf_path); system(rename_cmd); return 0; } else{ //doesn't exist. return -2; }}/*--------------------------------------------------------------------------* 一组操作用户配置的函数。 Ported from G1 Old Version by Kevin 09.19.2007 *--------------------------------------------------------------------------*//*--------------------------------------------------------------------------* * characters used for Base64 encoding *--------------------------------------------------------------------------*/const char *BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";static unsigned char itoa64[] = /* 0 ... 63 => ascii - 64 */ "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";static void to64(register char *s, register long v, register int n){ while (--n >= 0) { *s++ = itoa64[v&0x3f]; v >>= 6; }}void _base64_encode_triple(unsigned char triple[3], char result[4]){ int tripleValue, i; tripleValue = triple[0]; tripleValue *= 256; tripleValue += triple[1]; tripleValue *= 256; tripleValue += triple[2]; for (i=0; i<4; i++) { result[3-i] = BASE64_CHARS[tripleValue%64]; tripleValue /= 64; }}void DES_encode(unsigned char *source, char *target){ char salt[3]; (void) srandom( (int) time( (time_t*) 0 ) ); to64( &salt[0], random(), 2 ); //strcpy(target, crypt( source, salt )); strcpy(target, source); return;}/** * encode an array of bytes using Base64 (RFC 3548) * * @param source the source buffer * @param sourcelen the length of the source buffer * @param target the target buffer * @param targetlen the length of the target buffer * @return 1 on success, 0 otherwise */int base64_encode(unsigned char *source, int sourcelen, char *target, int targetlen){ /* check if the result will fit in the target buffer */ if ((sourcelen+2)/3*4 > targetlen-1) return 0; /* encode all full triples */ while (sourcelen >= 3) { _base64_encode_triple(source, target); sourcelen -= 3; source += 3; target += 4; } /* encode the last one or two characters */ if (sourcelen > 0) { unsigned char temp[3]; memset(temp, 0, sizeof(temp)); memcpy(temp, source, sourcelen); _base64_encode_triple(temp, target); target[3] = '='; if (sourcelen == 1) target[2] = '='; target += 4; } /* terminate the string */ target[0] = 0; return 1;}/* * Name: base64decode * * Description: Decodes BASE-64 encoded string */int base64_decode(void *dst,char *src,int maxlen){ int bitval,bits; int val; int len,x,y; len = strlen(src); bitval=0; bits=0; y=0; for(x=0;x<len;x++) { if ((src[x]>='A')&&(src[x]<='Z')) val=src[x]-'A'; else if ((src[x]>='a')&&(src[x]<='z')) val=src[x]-'a'+26; else if ((src[x]>='0')&&(src[x]<='9')) val=src[x]-'0'+52; else if (src[x]=='+') val=62; else if (src[x]=='-') val=63; else val=-1; if (val>=0) { bitval=bitval<<6; bitval+=val; bits+=6; while (bits>=8) { if (y<maxlen) ((char *)dst)[y++]=(bitval>>(bits-8))&0xFF; bits-=8; bitval &= (1<<bits)-1; } } } if (y<maxlen) ((char *)dst)[y++]=0; return y;}/*--------------------------------------------------------------------------* 验证帐号和密码,返回0则OK,密码验证未通过返回-1 *--------------------------------------------------------------------------*/int cgi_match_usr_pwd(char * username, char * password, char * conf_path){ FILE *conf_fp; char pattern[512],*name1,*password1; if (( NULL == username ) || (NULL == password) ) return -1; if (!(conf_fp=fopen(conf_path,"r"))) // open file pointer { return -1; } fseek( conf_fp, 0, SEEK_SET ); while (feof(conf_fp) == 0) { fscanf(conf_fp,"%s\n",pattern); //Read next record name1=strtok(pattern,":"); password1 = strtok(NULL,":"); if ((name1 == NULL) || (password1 == NULL))
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?