📄 fileio.c
字号:
} } } return TRUE;}/** * load_ip_prefix: * @char: Any IP address with or without a mask. Valid input string is any * of the following: * a.b.c.d * a.b.c.d/n (0 <= n <= 32) * @addr: * @prefix_len: */int load_ip_prefix(char *data, struct in_addr *addr, int *prefix_len){ char *pos; pos = strchr(data, '/'); if (pos == NULL) { if (load_ip_address(data, addr) != TRUE) { fprintf(stderr, "load_net_address: invalid IP address\n"); return FALSE; } *prefix_len = 32; } else { *pos = '\0'; if (load_ip_address(data, addr) != TRUE) { fprintf(stderr, "load_ip_prefix: invalid IP address\n"); return FALSE; } pos++; if (!(sscanf(pos, "%d", prefix_len) == 1 && *prefix_len >= 0 && *prefix_len <= 32)) { fprintf(stderr, "load_ip_prefix: invalid network prefix\n"); return FALSE; } } return TRUE;}/* Load hex table * * Reads hex table into the array and optionally sets the length read * * INPUT VALUES: * * ) data must not be NULL * ) table must not be NULL * M23) Valid hex table * M24) Invalid odd character * M25) Invalid even character * M26) Incomplete hex byte * M27) Too long hex value * M28) Length is NULL * M29) Something else */int load_hex_table(const char *data, unsigned char *table, int maxlen, int *length){ int i, tmp, res; char c; ASSERT(data != NULL); ASSERT(table != NULL); if (length != NULL) { *length = 0; } for (i = 0; i < maxlen; i++) { table[i] = '\0'; } while (*data == ' ' || *data == '\t') data++; if (strncmp(data, "0x", 2) == 0) { data += 2; } else if (*data == '"') { res = load_char_table(data, (char *)table, maxlen); if (length != NULL && res == TRUE) { table[maxlen - 1] = '\0'; *length = strlen((char *)table); } return res; } for (i = 0; i < maxlen; i++) { do { c = *data++; } while (c == ' ' || c == '\t'); if (c == '\0') break; tmp = get_hex_digit(c); if (tmp == -1) { fprintf(stderr, "load_hex_table: invalid character '%c'\n", c); return FALSE; } res = tmp * 16; c = *data++; /* no spaces allowed in one hex byte */ if (c == '\0') { fprintf(stderr, "load_hex_table: incomplete hex byte\n"); return FALSE; } tmp = get_hex_digit(c); if (tmp == -1) { fprintf(stderr, "load_hex_table: invalid character '%c'\n", c); return FALSE; } res += tmp; table[i] = res; } if (i == maxlen) { do { c = *data++; } while (c == ' ' || c == '\t'); if (c != '\0') { fprintf(stderr, "load_hex_table: too long hex value\n"); return FALSE; } } if (length != NULL) { *length = i; } return TRUE;}/* Load character table (string) * * NOTE! The routine loads actually a table of characters. * If the string is as long as the buffer, the string will not be * null-terminated! The user of the routine has to make sure that the * string is null-terminated if required. * * INPUT VALUES: * * ) data must not be NULL * ) table must not be NULL * M30) Valid string * M31) 2 x '"' in string * M32) only one '"' in string * M33) '"' in the middle string * M34) '\n' in string * M35) '\r' in string * M36) '\\' in string * M37) '\"' in string * M38) '\oct' in string * M39) Too long string */int load_char_table(const char *data, char *table, int maxlen){ int i, j, startpos; char temp[5]; int val, write, len; char ch; int start, stop; ASSERT(data != NULL); ASSERT(table != NULL); for (i = 0 ; i < maxlen ; i++) { table[i] = '\0'; } write = 0; start = FALSE; stop = FALSE; startpos = 0; while (data[startpos] == ' ' || data[startpos] == '\t') startpos++; len = strlen(data) - startpos; for (i = startpos; i <= len; i++) { val = data[i]; if (val == '\0') { if (start == TRUE && stop == TRUE) break; fprintf(stderr, "load_char_table: Unexpected end of string\n"); return FALSE; } if (val == '"') { if (start == FALSE) { start = TRUE; continue; } else if (stop == FALSE) { stop = TRUE; continue; } else { fprintf(stderr, "load_char_table: Unexpected '\"'\n"); return FALSE; } } else { if (start == FALSE) { fprintf(stderr, "load_char_table: Unrecognized " "character '%c'\n", val); return FALSE; } } if (val == '\\') { if (i == len - 1) { fprintf(stderr, "load_char_table: String longer " "than expected\n"); return FALSE; } i++; if (data[i] == '0') { ch = '\0'; } else if (data[i] == 'n') { ch = '\n'; } else if (data[i] == 'r') { ch = '\r'; } else if (data[i] == '\\') { ch = '\\'; } else if (data[i] == '"') { ch = '"'; } else { if (i >= len - 3) { fprintf(stderr, "load_char_table: String " "longer than expected\n"); return FALSE; } for (j = 0 ; j < 3 ; j++) { ch = data[i]; i++; if (ch < '0' || ch > '9') { fprintf(stderr, "load_char_table: " "Invalid octal " "number\n"); return FALSE; } temp[j] = ch; } i--; temp[3] = '\0'; if (sscanf(temp, "%03o", &val) != 1) { fprintf(stderr, "load_char_table: Invalid " "octal number\n"); return FALSE; } ch = val; } } else { ch = val; } if (write == maxlen) { fprintf(stderr, "load_char_table: String longer than " "expected\n"); return FALSE; } table[write] = ch; write++; } if (write < maxlen) { table[write] = '\0'; } return TRUE;}/* Load syslog facility * * INPUT VALUES: * * ) data must not be NULL * M40) Known syslog facility * M41) Unknown syslog facility */int load_syslog_facility(const char *data, int *facility){ struct facilitydata { char *name; int code; }; struct facilitydata facilitycode[] = { { "LOG_KERN", LOG_KERN }, { "LOG_MAIL", LOG_MAIL }, { "LOG_DAEMON", LOG_DAEMON }, { "LOG_AUTH", LOG_AUTH }, { "LOG_SYSLOG", LOG_SYSLOG }, { "LOG_LPR", LOG_LPR }, { "LOG_NEWS", LOG_NEWS }, { "LOG_UUCP", LOG_UUCP }, { "LOG_CRON", LOG_CRON }, { "LOG_AUTHPRIV", LOG_AUTHPRIV }, { "LOG_FTP", LOG_FTP }, { "LOG_LOCAL0", LOG_LOCAL0 }, { "LOG_LOCAL1", LOG_LOCAL1 }, { "LOG_LOCAL2", LOG_LOCAL2 }, { "LOG_LOCAL3", LOG_LOCAL3 }, { "LOG_LOCAL4", LOG_LOCAL4 }, { "LOG_LOCAL5", LOG_LOCAL5 }, { "LOG_LOCAL6", LOG_LOCAL6 }, { "LOG_LOCAL7", LOG_LOCAL7 }, { NULL, -1 } }; int i; for (i = 0; facilitycode[i].name != NULL; i++) { if (strcmp(data, facilitycode[i].name) == 0) { *facility = facilitycode[i].code; return TRUE; } } return FALSE;}int load_nai(const char *data, char *nai, int *len){ char *start, *end, *t; char hwbuf[128], tmp[MAX_NAI_LEN]; if (load_char_table(data, nai, MAX_NAI_LEN) != TRUE) return FALSE; start = strchr(nai, '['); if (start != NULL) { end = strchr(start, ']'); if (end == NULL || end < start) { fprintf(stderr, "load_nai - parse error ('[' without ']')\n"); return FALSE; } *end = '\0'; if (dyn_ip_get_hwaddr((start + 1), hwbuf, sizeof(hwbuf)) < 0) { *end = ']'; fprintf(stderr, "load_nai - cannot resolve hwaddr for " "interface '%s'.\n", (start + 1)); return FALSE; } t = hwbuf; while (*t != '\0') { if (*t == ':') *t = '.'; t++; } if (strlen(nai) + strlen(hwbuf) + start - end - 1 >= MAX_NAI_LEN) { fprintf(stderr, "load_nai - too long NAI\n"); return FALSE; } dynamics_strlcpy(tmp, end + 1, sizeof(tmp)); dynamics_strlcpy(start, hwbuf, MAX_NAI_LEN - (start - nai)); dynamics_strlcpy(start + strlen(hwbuf), tmp, MAX_NAI_LEN - (start + strlen(hwbuf) - nai)); } *len = strlen(nai); return TRUE;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -