Skip to content

Commit

Permalink
s3-lib Replace StrCaseCmp() with strcasecmp_m()
Browse files Browse the repository at this point in the history
strcasecmp_m() never needs to call to talloc, and via next_codepoint()
still has an ASCII fast-path bypassing iconv() calls.

Andrew Bartlett
  • Loading branch information
abartlet committed May 18, 2011
1 parent 7a11e5d commit c615ebe
Show file tree
Hide file tree
Showing 51 changed files with 179 additions and 263 deletions.
4 changes: 2 additions & 2 deletions nsswitch/wins.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ int lookup(nsd_file_t *rq)
* response needs to be a string of the following format
* ip_address[ ip_address]*\tname[ alias]*
*/
if (StrCaseCmp(map,"hosts.byaddr") == 0) {
if (strcasecmp_m(map,"hosts.byaddr") == 0) {
if ( status = lookup_byaddr_backend(key, &count)) {
size = strlen(key) + 1;
if (size > len) {
Expand Down Expand Up @@ -208,7 +208,7 @@ int lookup(nsd_file_t *rq)
response[strlen(response)-1] = '\n';
talloc_free(status);
}
} else if (StrCaseCmp(map,"hosts.byname") == 0) {
} else if (strcasecmp_m(map,"hosts.byname") == 0) {
if (ip_list = lookup_byname_backend(key, &count)) {
for (i = count; i ; i--) {
addr = inet_ntoa(ip_list[i-1]);
Expand Down
14 changes: 7 additions & 7 deletions source3/groupdb/mapping_tdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ static int convert_ldb_record(TDB_CONTEXT *ltdb, TDB_DATA key,
p += len + 1;

num_vals = pull_uint32(p, 0);
if (StrCaseCmp(name, "member") == 0) {
if (strcasecmp_m(name, "member") == 0) {
num_mem = num_vals;
members = talloc_array(tmp_ctx, struct dom_sid, num_mem);
if (members == NULL) {
Expand Down Expand Up @@ -882,30 +882,30 @@ static int convert_ldb_record(TDB_CONTEXT *ltdb, TDB_DATA key,

/* we ignore unknown or uninteresting attributes
* (objectclass, etc.) */
if (StrCaseCmp(name, "gidNumber") == 0) {
if (strcasecmp_m(name, "gidNumber") == 0) {
map.gid = strtoul(val, &q, 10);
if (*q) {
errno = EIO;
goto failed;
}
} else if (StrCaseCmp(name, "sid") == 0) {
} else if (strcasecmp_m(name, "sid") == 0) {
if (!string_to_sid(&map.sid, val)) {
errno = EIO;
goto failed;
}
} else if (StrCaseCmp(name, "sidNameUse") == 0) {
} else if (strcasecmp_m(name, "sidNameUse") == 0) {
map.sid_name_use = strtoul(val, &q, 10);
if (*q) {
errno = EIO;
goto failed;
}
} else if (StrCaseCmp(name, "ntname") == 0) {
} else if (strcasecmp_m(name, "ntname") == 0) {
strlcpy(map.nt_name, val,
sizeof(map.nt_name));
} else if (StrCaseCmp(name, "comment") == 0) {
} else if (strcasecmp_m(name, "comment") == 0) {
strlcpy(map.comment, val,
sizeof(map.comment));
} else if (StrCaseCmp(name, "member") == 0) {
} else if (strcasecmp_m(name, "member") == 0) {
if (!string_to_sid(&members[j], val)) {
errno = EIO;
goto failed;
Expand Down
1 change: 0 additions & 1 deletion source3/include/proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,6 @@ ssize_t tstream_read_packet_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
/* The following definitions come from lib/util_str.c */

bool next_token(const char **ptr, char *buff, const char *sep, size_t bufsize);
int StrCaseCmp(const char *s, const char *t);
int StrnCaseCmp(const char *s, const char *t, size_t len);
bool strnequal(const char *s1,const char *s2,size_t n);
bool strcsequal(const char *s1,const char *s2);
Expand Down
4 changes: 2 additions & 2 deletions source3/lib/adt_tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ static struct tree_node *pathtree_birth_child(struct tree_node *node,
/* the strings should never match assuming that we
have called pathtree_find_child() first */

if ( StrCaseCmp( infant->key, node->children[i-1]->key ) > 0 ) {
if ( strcasecmp_m( infant->key, node->children[i-1]->key ) > 0 ) {
DEBUG(11,("pathtree_birth_child: storing infant in i == [%d]\n",
i));
node->children[i] = infant;
Expand Down Expand Up @@ -183,7 +183,7 @@ static struct tree_node *pathtree_find_child(struct tree_node *node,
DEBUG(11,("pathtree_find_child: child key => [%s]\n",
node->children[i]->key));

result = StrCaseCmp( node->children[i]->key, key );
result = strcasecmp_m( node->children[i]->key, key );

if ( result == 0 )
next = node->children[i];
Expand Down
2 changes: 1 addition & 1 deletion source3/lib/filename_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,5 +202,5 @@ bool is_ntfs_default_stream_smb_fname(const struct smb_filename *smb_fname)
return false;
}

return StrCaseCmp(smb_fname->stream_name, "::$DATA") == 0;
return strcasecmp_m(smb_fname->stream_name, "::$DATA") == 0;
}
2 changes: 1 addition & 1 deletion source3/lib/ms_fnmatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ int ms_fnmatch(const char *pattern, const char *string, bool translate_pattern,
if (is_case_sensitive) {
return strcmp(pattern, string);
} else {
return StrCaseCmp(pattern, string);
return strcasecmp_m(pattern, string);
}
}

Expand Down
4 changes: 2 additions & 2 deletions source3/lib/smbldap.c
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ ATTRIB_MAP_ENTRY sidmap_attr_list[] = {
return NULL;
}

if (StrCaseCmp(tmp, result) < 0) {
if (strcasecmp_m(tmp, result) < 0) {
TALLOC_FREE(result);
result = tmp;
} else {
Expand Down Expand Up @@ -654,7 +654,7 @@ static void smbldap_make_mod_internal(LDAP *ldap_struct, LDAPMessage *existing,
equal = (newblob && (data_blob_cmp(&oldblob, newblob) == 0));
} else {
/* all of our string attributes are case insensitive */
equal = (newval && (StrCaseCmp(oldval, newval) == 0));
equal = (newval && (strcasecmp_m(oldval, newval) == 0));
}

if (equal) {
Expand Down
2 changes: 1 addition & 1 deletion source3/lib/tldap_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ static int compare_utf8_blobs(const DATA_BLOB *d1, const DATA_BLOB *d2)
TALLOC_FREE(s1);
return 0;
}
ret = StrCaseCmp(s1, s2);
ret = strcasecmp_m(s1, s2);
TALLOC_FREE(s2);
TALLOC_FREE(s1);
return ret;
Expand Down
4 changes: 2 additions & 2 deletions source3/lib/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -1299,7 +1299,7 @@ bool is_in_path(const char *name, name_compare_entry *namelist, bool case_sensit
}
} else {
if((case_sensitive && (strcmp(last_component, namelist->name) == 0))||
(!case_sensitive && (StrCaseCmp(last_component, namelist->name) == 0))) {
(!case_sensitive && (strcasecmp_m(last_component, namelist->name) == 0))) {
DEBUG(8,("is_in_path: match succeeded\n"));
return True;
}
Expand Down Expand Up @@ -2203,7 +2203,7 @@ bool name_to_fqdn(fstring fqdn, const char *name)
}
}
}
if (full && (StrCaseCmp(full, "localhost.localdomain") == 0)) {
if (full && (strcasecmp_m(full, "localhost.localdomain") == 0)) {
DEBUG(1, ("WARNING: your /etc/hosts file may be broken!\n"));
DEBUGADD(1, (" Specifing the machine hostname for address 127.0.0.1 may lead\n"));
DEBUGADD(1, (" to Kerberos authentication problems as localhost.localdomain\n"));
Expand Down
85 changes: 1 addition & 84 deletions source3/lib/util_str.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,89 +35,6 @@ const char toupper_ascii_fast_table[128] = {
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f
};

/**
* Case insensitive string compararison.
*
* iconv does not directly give us a way to compare strings in
* arbitrary unix character sets -- all we can is convert and then
* compare. This is expensive.
*
* As an optimization, we do a first pass that considers only the
* prefix of the strings that is entirely 7-bit. Within this, we
* check whether they have the same value.
*
* Hopefully this will often give the answer without needing to copy.
* In particular it should speed comparisons to literal ascii strings
* or comparisons of strings that are "obviously" different.
*
* If we find a non-ascii character we fall back to converting via
* iconv.
*
* This should never be slower than convering the whole thing, and
* often faster.
*
* A different optimization would be to compare for bitwise equality
* in the binary encoding. (It would be possible thought hairy to do
* both simultaneously.) But in that case if they turn out to be
* different, we'd need to restart the whole thing.
*
* Even better is to implement strcasecmp for each encoding and use a
* function pointer.
**/
int StrCaseCmp(const char *s, const char *t)
{

const char *ps, *pt;
size_t size;
smb_ucs2_t *buffer_s, *buffer_t;
int ret;

for (ps = s, pt = t; ; ps++, pt++) {
char us, ut;

if (!*ps && !*pt)
return 0; /* both ended */
else if (!*ps)
return -1; /* s is a prefix */
else if (!*pt)
return +1; /* t is a prefix */
else if ((*ps & 0x80) || (*pt & 0x80))
/* not ascii anymore, do it the hard way
* from here on in */
break;

us = toupper_ascii_fast(*ps);
ut = toupper_ascii_fast(*pt);
if (us == ut)
continue;
else if (us < ut)
return -1;
else if (us > ut)
return +1;
}

if (!push_ucs2_talloc(talloc_tos(), &buffer_s, ps, &size)) {
return strcmp(ps, pt);
/* Not quite the right answer, but finding the right one
under this failure case is expensive, and it's pretty
close */
}

if (!push_ucs2_talloc(talloc_tos(), &buffer_t, pt, &size)) {
TALLOC_FREE(buffer_s);
return strcmp(ps, pt);
/* Not quite the right answer, but finding the right one
under this failure case is expensive, and it's pretty
close */
}

ret = strcasecmp_w(buffer_s, buffer_t);
TALLOC_FREE(buffer_s);
TALLOC_FREE(buffer_t);
return ret;
}


/**
Case insensitive string compararison, length limited.
**/
Expand Down Expand Up @@ -360,7 +277,7 @@ bool in_list(const char *s, const char *list, bool casesensitive)
break;
}
} else {
if (StrCaseCmp(tok,s) == 0) {
if (strcasecmp_m(tok,s) == 0) {
ret = true;
break;
}
Expand Down
2 changes: 1 addition & 1 deletion source3/libads/ldap.c
Original file line number Diff line number Diff line change
Expand Up @@ -2228,7 +2228,7 @@ static bool ads_dump_field(ADS_STRUCT *ads, char *field, void **values, void *da
}

for (i=0; handlers[i].name; i++) {
if (StrCaseCmp(handlers[i].name, field) == 0) {
if (strcasecmp_m(handlers[i].name, field) == 0) {
if (!values) /* first time, indicate string or not */
return handlers[i].string;
handlers[i].handler(ads, field, (struct berval **) values);
Expand Down
2 changes: 1 addition & 1 deletion source3/libads/ldap_printer.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ static void map_regval_to_ads(TALLOC_CTX *ctx, ADS_MODLIST *mods,
int i;

for (i=0; map[i].valname; i++) {
if (StrCaseCmp(map[i].valname, regval_name(value)) == 0) {
if (strcasecmp_m(map[i].valname, regval_name(value)) == 0) {
if (!map[i].fn(ctx, mods, value)) {
DEBUG(5, ("Add of value %s to modlist failed\n", regval_name(value)));
} else {
Expand Down
4 changes: 2 additions & 2 deletions source3/libnet/libnet_join.c
Original file line number Diff line number Diff line change
Expand Up @@ -1771,7 +1771,7 @@ static int libnet_destroy_JoinCtx(struct libnet_JoinCtx *r)
}

krb5_cc_env = getenv(KRB5_ENV_CCNAME);
if (krb5_cc_env && StrCaseCmp(krb5_cc_env, "MEMORY:libnetjoin")) {
if (krb5_cc_env && strcasecmp_m(krb5_cc_env, "MEMORY:libnetjoin")) {
unsetenv(KRB5_ENV_CCNAME);
}

Expand All @@ -1790,7 +1790,7 @@ static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx *r)
}

krb5_cc_env = getenv(KRB5_ENV_CCNAME);
if (krb5_cc_env && StrCaseCmp(krb5_cc_env, "MEMORY:libnetjoin")) {
if (krb5_cc_env && strcasecmp_m(krb5_cc_env, "MEMORY:libnetjoin")) {
unsetenv(KRB5_ENV_CCNAME);
}

Expand Down
4 changes: 2 additions & 2 deletions source3/librpc/rpc/dcerpc_ep.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ static NTSTATUS ep_register(TALLOC_CTX *mem_ctx,
"rpc_server", "epmapper",
"none");

if (StrCaseCmp(rpcsrv_type, "embedded") == 0) {
if (strcasecmp_m(rpcsrv_type, "embedded") == 0) {
static struct client_address client_id;

strlcpy(client_id.addr, "localhost", sizeof(client_id.addr));
Expand All @@ -186,7 +186,7 @@ static NTSTATUS ep_register(TALLOC_CTX *mem_ctx,
"epmapper (%s)", nt_errstr(status)));
goto done;
}
} else if (StrCaseCmp(rpcsrv_type, "daemon") == 0) {
} else if (strcasecmp_m(rpcsrv_type, "daemon") == 0) {
/* Connect to the endpoint mapper locally */
ncalrpc_sock = talloc_asprintf(tmp_ctx,
"%s/%s",
Expand Down
Loading

0 comments on commit c615ebe

Please sign in to comment.