📄 util_str.c
字号:
list[num] = SMB_STRDUP(src[num]); if (!list[num]) { DEBUG(0,("str_list_copy: Unable to allocate memory")); str_list_free(&list); return False; } num++; } *dest = list; return True; }/** * Return true if all the elements of the list match exactly. **/BOOL str_list_compare(char **list1, char **list2){ int num; if (!list1 || !list2) return (list1 == list2); for (num = 0; list1[num]; num++) { if (!list2[num]) return False; if (!strcsequal(list1[num], list2[num])) return False; } if (list2[num]) return False; /* if list2 has more elements than list1 fail */ return True;}void str_list_free(char ***list){ char **tlist; if (!list || !*list) return; tlist = *list; for(; *tlist; tlist++) SAFE_FREE(*tlist); SAFE_FREE(*list);}/****************************************************************************** *****************************************************************************/int str_list_count( const char **list ){ int i = 0; if ( ! list ) return 0; /* count the number of list members */ for ( i=0; *list; i++, list++ ); return i;}/****************************************************************************** version of standard_sub_basic() for string lists; uses alloc_sub_basic() for the work *****************************************************************************/ BOOL str_list_sub_basic( char **list, const char *smb_name ){ char *s, *tmpstr; while ( *list ) { s = *list; tmpstr = alloc_sub_basic(smb_name, s); if ( !tmpstr ) { DEBUG(0,("str_list_sub_basic: alloc_sub_basic() return NULL!\n")); return False; } SAFE_FREE(*list); *list = tmpstr; list++; } return True;}/****************************************************************************** substritute a specific pattern in a string list *****************************************************************************/ BOOL str_list_substitute(char **list, const char *pattern, const char *insert){ char *p, *s, *t; ssize_t ls, lp, li, ld, i, d; if (!list) return False; if (!pattern) return False; if (!insert) return False; lp = (ssize_t)strlen(pattern); li = (ssize_t)strlen(insert); ld = li -lp; while (*list) { s = *list; ls = (ssize_t)strlen(s); while ((p = strstr_m(s, pattern))) { t = *list; d = p -t; if (ld) { t = (char *) SMB_MALLOC(ls +ld +1); if (!t) { DEBUG(0,("str_list_substitute: Unable to allocate memory")); return False; } memcpy(t, *list, d); memcpy(t +d +li, p +lp, ls -d -lp +1); SAFE_FREE(*list); *list = t; ls += ld; s = t +d +li; } for (i = 0; i < li; i++) { switch (insert[i]) { case '`': case '"': case '\'': case ';': case '$': case '%': case '\r': case '\n': t[d +i] = '_'; break; default: t[d +i] = insert[i]; } } } list++; } return True;}#define IPSTR_LIST_SEP ","#define IPSTR_LIST_CHAR ','/** * Add ip string representation to ipstr list. Used also * as part of @function ipstr_list_make * * @param ipstr_list pointer to string containing ip list; * MUST BE already allocated and IS reallocated if necessary * @param ipstr_size pointer to current size of ipstr_list (might be changed * as a result of reallocation) * @param ip IP address which is to be added to list * @return pointer to string appended with new ip and possibly * reallocated to new length **/char* ipstr_list_add(char** ipstr_list, const struct ip_service *service){ char* new_ipstr = NULL; /* arguments checking */ if (!ipstr_list || !service) return NULL; /* attempt to convert ip to a string and append colon separator to it */ if (*ipstr_list) { asprintf(&new_ipstr, "%s%s%s:%d", *ipstr_list, IPSTR_LIST_SEP, inet_ntoa(service->ip), service->port); SAFE_FREE(*ipstr_list); } else { asprintf(&new_ipstr, "%s:%d", inet_ntoa(service->ip), service->port); } *ipstr_list = new_ipstr; return *ipstr_list;}/** * Allocate and initialise an ipstr list using ip adresses * passed as arguments. * * @param ipstr_list pointer to string meant to be allocated and set * @param ip_list array of ip addresses to place in the list * @param ip_count number of addresses stored in ip_list * @return pointer to allocated ip string **/ char* ipstr_list_make(char** ipstr_list, const struct ip_service* ip_list, int ip_count){ int i; /* arguments checking */ if (!ip_list && !ipstr_list) return 0; *ipstr_list = NULL; /* process ip addresses given as arguments */ for (i = 0; i < ip_count; i++) *ipstr_list = ipstr_list_add(ipstr_list, &ip_list[i]); return (*ipstr_list);}/** * Parse given ip string list into array of ip addresses * (as ip_service structures) * e.g. 192.168.1.100:389,192.168.1.78, ... * * @param ipstr ip string list to be parsed * @param ip_list pointer to array of ip addresses which is * allocated by this function and must be freed by caller * @return number of succesfully parsed addresses **/ int ipstr_list_parse(const char* ipstr_list, struct ip_service **ip_list){ fstring token_str; size_t count; int i; if (!ipstr_list || !ip_list) return 0; count = count_chars(ipstr_list, IPSTR_LIST_CHAR) + 1; if ( (*ip_list = SMB_MALLOC_ARRAY(struct ip_service, count)) == NULL ) { DEBUG(0,("ipstr_list_parse: malloc failed for %lu entries\n", (unsigned long)count)); return 0; } for ( i=0; next_token(&ipstr_list, token_str, IPSTR_LIST_SEP, FSTRING_LEN) && i<count; i++ ) { struct in_addr addr; unsigned port = 0; char *p = strchr(token_str, ':'); if (p) { *p = 0; port = atoi(p+1); } /* convert single token to ip address */ if ( (addr.s_addr = inet_addr(token_str)) == INADDR_NONE ) break; (*ip_list)[i].ip = addr; (*ip_list)[i].port = port; } return count;}/** * Safely free ip string list * * @param ipstr_list ip string list to be freed **/void ipstr_list_free(char* ipstr_list){ SAFE_FREE(ipstr_list);}/** Unescape a URL encoded string, in place.**/void rfc1738_unescape(char *buf){ char *p=buf; while (p && *p && (p=strchr_m(p,'%'))) { int c1 = p[1]; int c2 = p[2]; if (c1 >= '0' && c1 <= '9') c1 = c1 - '0'; else if (c1 >= 'A' && c1 <= 'F') c1 = 10 + c1 - 'A'; else if (c1 >= 'a' && c1 <= 'f') c1 = 10 + c1 - 'a'; else {p++; continue;} if (c2 >= '0' && c2 <= '9') c2 = c2 - '0'; else if (c2 >= 'A' && c2 <= 'F') c2 = 10 + c2 - 'A'; else if (c2 >= 'a' && c2 <= 'f') c2 = 10 + c2 - 'a'; else {p++; continue;} *p = (c1<<4) | c2; memmove(p+1, p+3, strlen(p+3)+1); p++; }}static const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";/** * Decode a base64 string into a DATA_BLOB - simple and slow algorithm **/DATA_BLOB base64_decode_data_blob(const char *s){ int bit_offset, byte_offset, idx, i, n; DATA_BLOB decoded = data_blob(s, strlen(s)+1); unsigned char *d = decoded.data; char *p; n=i=0; while (*s && (p=strchr_m(b64,*s))) { idx = (int)(p - b64); byte_offset = (i*6)/8; bit_offset = (i*6)%8; d[byte_offset] &= ~((1<<(8-bit_offset))-1); if (bit_offset < 3) { d[byte_offset] |= (idx << (2-bit_offset)); n = byte_offset+1; } else { d[byte_offset] |= (idx >> (bit_offset-2)); d[byte_offset+1] = 0; d[byte_offset+1] |= (idx << (8-(bit_offset-2))) & 0xFF; n = byte_offset+2; } s++; i++; } if ((n > 0) && (*s == '=')) { n -= 1; } /* fix up length */ decoded.length = n; return decoded;}/** * Decode a base64 string in-place - wrapper for the above **/void base64_decode_inplace(char *s){ DATA_BLOB decoded = base64_decode_data_blob(s); if ( decoded.length != 0 ) { memcpy(s, decoded.data, decoded.length); /* null terminate */ s[decoded.length] = '\0'; } else { *s = '\0'; } data_blob_free(&decoded);}/** * Encode a base64 string into a malloc()ed string caller to free. * *From SQUID: adopted from http://ftp.sunet.se/pub2/gnu/vm/base64-encode.c with adjustments **/char * base64_encode_data_blob(DATA_BLOB data){ int bits = 0; int char_count = 0; size_t out_cnt, len, output_len; char *result; if (!data.length || !data.data) return NULL; out_cnt = 0; len = data.length; output_len = data.length * 2; result = SMB_MALLOC(output_len); /* get us plenty of space */ while (len-- && out_cnt < (data.length * 2) - 5) { int c = (unsigned char) *(data.data++); bits += c; char_count++; if (char_count == 3) { result[out_cnt++] = b64[bits >> 18]; result[out_cnt++] = b64[(bits >> 12) & 0x3f]; result[out_cnt++] = b64[(bits >> 6) & 0x3f]; result[out_cnt++] = b64[bits & 0x3f]; bits = 0; char_count = 0; } else { bits <<= 8; } } if (char_count != 0) { bits <<= 16 - (8 * char_count); result[out_cnt++] = b64[bits >> 18]; result[out_cnt++] = b64[(bits >> 12) & 0x3f]; if (char_count == 1) { result[out_cnt++] = '='; result[out_cnt++] = '='; } else { result[out_cnt++] = b64[(bits >> 6) & 0x3f]; result[out_cnt++] = '='; } } result[out_cnt] = '\0'; /* terminate */ return result;}/* read a SMB_BIG_UINT from a string */SMB_BIG_UINT STR_TO_SMB_BIG_UINT(const char *nptr, const char **entptr){ SMB_BIG_UINT val = -1; const char *p = nptr; while (p && *p && isspace(*p)) p++;#ifdef LARGE_SMB_OFF_T sscanf(p,"%llu",&val); #else /* LARGE_SMB_OFF_T */ sscanf(p,"%lu",&val);#endif /* LARGE_SMB_OFF_T */ if (entptr) { while (p && *p && isdigit(*p)) p++; *entptr = p; } return val;}void string_append(char **left, const char *right){ int new_len = strlen(right) + 1; if (*left == NULL) { *left = SMB_MALLOC(new_len); *left[0] = '\0'; } else { new_len += strlen(*left); *left = SMB_REALLOC(*left, new_len); } if (*left == NULL) return; safe_strcat(*left, right, new_len-1);}BOOL add_string_to_array(TALLOC_CTX *mem_ctx, const char *str, const char ***strings, int *num){ char *dup_str = talloc_strdup(mem_ctx, str); *strings = TALLOC_REALLOC_ARRAY(mem_ctx, *strings, const char *, (*num)+1); if ((*strings == NULL) || (dup_str == NULL)) return False; (*strings)[*num] = dup_str; *num += 1; return True;}/* Append an sprintf'ed string. Double buffer size on demand. Usable without * error checking in between. The indiation that something weird happened is * string==NULL */void sprintf_append(TALLOC_CTX *mem_ctx, char **string, ssize_t *len, size_t *bufsize, const char *fmt, ...){ va_list ap; char *newstr; int ret; BOOL increased; /* len<0 is an internal marker that something failed */ if (*len < 0) goto error; if (*string == NULL) { if (*bufsize == 0) *bufsize = 128; if (mem_ctx != NULL) *string = TALLOC_ARRAY(mem_ctx, char, *bufsize); else *string = SMB_MALLOC_ARRAY(char, *bufsize); if (*string == NULL) goto error; } va_start(ap, fmt); ret = vasprintf(&newstr, fmt, ap); va_end(ap); if (ret < 0) goto error; increased = False; while ((*len)+ret >= *bufsize) { increased = True; *bufsize *= 2; if (*bufsize >= (1024*1024*256)) goto error; } if (increased) { if (mem_ctx != NULL) *string = TALLOC_REALLOC_ARRAY(mem_ctx, *string, char, *bufsize); else *string = SMB_REALLOC_ARRAY(*string, char, *bufsize); if (*string == NULL) goto error; } StrnCpy((*string)+(*len), newstr, ret); (*len) += ret; free(newstr); return; error: *len = -1; *string = NULL;}/* Returns the substring from src between the first occurrence of the char "front" and the first occurence of the char "back". Mallocs the return string which must be freed. Not for use with wide character strings.*/char *sstring_sub(const char *src, char front, char back){ char *temp1, *temp2, *temp3; ptrdiff_t len; temp1 = strchr(src, front); if (temp1 == NULL) return NULL; temp2 = strchr(src, back); if (temp2 == NULL) return NULL; len = temp2 - temp1; if (len <= 0) return NULL; temp3 = (char*)SMB_MALLOC(len); if (temp3 == NULL) { DEBUG(1,("Malloc failure in sstring_sub\n")); return NULL; } memcpy(temp3, temp1+1, len-1); temp3[len-1] = '\0'; return temp3;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -