开发者

String searching for u_char*

开发者 https://www.devze.com 2023-02-10 17:56 出处:网络
How do i look for a substr in string pointed by u_char* as all C functions take a char* as argum开发者_运维问答ent?Assuming that u_char is a typedef for unsigned char, then the normal trick is to use

How do i look for a substr in string pointed by u_char* as all C functions take a char* as argum开发者_运维问答ent?


Assuming that u_char is a typedef for unsigned char, then the normal trick is to use the standard functions and apply casts.

That is very ungainly, though, so you might instead use macros that apply the casts automatically.

#define STRSTR(haystack, needle) \
                 ((u_char *)strstr((char *)haystack, (char *)needle))

Or you might write inline functions if your compiler supports C99.

static inline u_char *ustrstr(u_char *haystack, u_char *needle)
{
    return (u_char *)strstr((char *)haystack, (char *)needle);
}

Or you might simply write cover functions:

u_char *ustrstr(u_char *haystack, u_char *needle)
{
    return (u_char *)strstr((char *)haystack, (char *)needle);
}


Since u_char is defined as a typedef of unsigned char in BSD type sockets implementations (sys/types.h in *nix and winsock.h in Windows) and if that's your context, you'll be safe to apply a cast to (char*) just before passing them to the standard C functions. If that's not your context, then you have to find the declaration of u_char and confirm that it is of type unsigned char or equivalent and only then it's safe to apply the cast.

0

精彩评论

暂无评论...
验证码 换一张
取 消