Ignore:
Timestamp:
Feb 27, 2013 10:11:59 AM (11 years ago)
Author:
nanang
Message:

Re #1556: backported to 1.x

Location:
pjproject/branches/1.x
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • pjproject/branches/1.x

  • pjproject/branches/1.x/pjlib/src/pj/hash.c

    r3553 r4385  
    8080    for (i=0; i<key->slen; ++i) { 
    8181        pj_uint8_t c = key->ptr[i]; 
     82        char lower; 
    8283        if (c & 64) 
    83             result[i] = (char)(c | 32); 
     84            lower = (char)(c | 32); 
    8485        else 
    85             result[i] = (char)c; 
    86         hval = hval * PJ_HASH_MULTIPLIER + result[i]; 
     86            lower = (char)c; 
     87        if (result) 
     88            result[i] = lower; 
     89        hval = hval * PJ_HASH_MULTIPLIER + lower; 
    8790    } 
    8891#else 
    8992    for (i=0; i<key->slen; ++i) { 
    90         result[i] = (char)pj_tolower(key->ptr[i]); 
    91         hval = hval * PJ_HASH_MULTIPLIER + result[i]; 
     93        char lower = (char)pj_tolower(key->ptr[i]); 
     94        if (result) 
     95            result[i] = lower; 
     96        hval = hval * PJ_HASH_MULTIPLIER + lower; 
    9297    } 
    9398#endif 
     
    129134                                   const void *key, unsigned keylen, 
    130135                                   void *val, pj_uint32_t *hval, 
    131                                    void *entry_buf) 
     136                                   void *entry_buf, pj_bool_t lower) 
    132137{ 
    133138    pj_uint32_t hash; 
     
    147152            const pj_uint8_t *p = (const pj_uint8_t*)key; 
    148153            for ( ; *p; ++p ) { 
    149                 hash = hash * PJ_HASH_MULTIPLIER + *p; 
     154                if (lower) 
     155                    hash = hash * PJ_HASH_MULTIPLIER + pj_tolower(*p); 
     156                else  
     157                    hash = hash * PJ_HASH_MULTIPLIER + *p; 
    150158            } 
    151159            keylen = p - (const unsigned char*)key; 
     
    154162                                  *end = p + keylen; 
    155163            for ( ; p!=end; ++p) { 
    156                 hash = hash * PJ_HASH_MULTIPLIER + *p; 
     164                if (lower) 
     165                    hash = hash * PJ_HASH_MULTIPLIER + pj_tolower(*p); 
     166                else 
     167                    hash = hash * PJ_HASH_MULTIPLIER + *p; 
    157168            } 
    158169        } 
     
    169180    { 
    170181        if (entry->hash==hash && entry->keylen==keylen && 
    171             pj_memcmp(entry->key, key, keylen)==0)  
     182            ((lower && pj_ansi_strnicmp((const char*)entry->key, 
     183                                        (const char*)key, keylen)==0) || 
     184             (!lower && pj_memcmp(entry->key, key, keylen)==0))) 
    172185        { 
    173             break;       
     186            break; 
    174187        } 
    175188    } 
     
    215228{ 
    216229    pj_hash_entry *entry; 
    217     entry = *find_entry( NULL, ht, key, keylen, NULL, hval, NULL); 
     230    entry = *find_entry( NULL, ht, key, keylen, NULL, hval, NULL, PJ_FALSE); 
    218231    return entry ? entry->value : NULL; 
    219232} 
    220233 
    221 PJ_DEF(void) pj_hash_set( pj_pool_t *pool, pj_hash_table_t *ht, 
    222                           const void *key, unsigned keylen, pj_uint32_t hval, 
    223                           void *value ) 
     234PJ_DEF(void *) pj_hash_get_lower( pj_hash_table_t *ht, 
     235                                  const void *key, unsigned keylen, 
     236                                  pj_uint32_t *hval) 
     237{ 
     238    pj_hash_entry *entry; 
     239    entry = *find_entry( NULL, ht, key, keylen, NULL, hval, NULL, PJ_TRUE); 
     240    return entry ? entry->value : NULL; 
     241} 
     242 
     243static void hash_set( pj_pool_t *pool, pj_hash_table_t *ht, 
     244                      const void *key, unsigned keylen, pj_uint32_t hval, 
     245                      void *value, void *entry_buf, pj_bool_t lower ) 
    224246{ 
    225247    pj_hash_entry **p_entry; 
    226248 
    227     p_entry = find_entry( pool, ht, key, keylen, value, &hval, NULL); 
     249    p_entry = find_entry( pool, ht, key, keylen, value, &hval, entry_buf, 
     250                          lower); 
    228251    if (*p_entry) { 
    229252        if (value == NULL) { 
     
    242265} 
    243266 
     267PJ_DEF(void) pj_hash_set( pj_pool_t *pool, pj_hash_table_t *ht, 
     268                          const void *key, unsigned keylen, pj_uint32_t hval, 
     269                          void *value ) 
     270{ 
     271    hash_set(pool, ht, key, keylen, hval, value, NULL, PJ_FALSE); 
     272} 
     273 
     274PJ_DEF(void) pj_hash_set_lower( pj_pool_t *pool, pj_hash_table_t *ht, 
     275                                const void *key, unsigned keylen, 
     276                                pj_uint32_t hval, void *value ) 
     277{ 
     278    hash_set(pool, ht, key, keylen, hval, value, NULL, PJ_TRUE); 
     279} 
     280 
    244281PJ_DEF(void) pj_hash_set_np( pj_hash_table_t *ht, 
    245282                             const void *key, unsigned keylen,  
     
    247284                             void *value) 
    248285{ 
    249     pj_hash_entry **p_entry; 
    250  
    251     p_entry = find_entry( NULL, ht, key, keylen, value, &hval,  
    252                          (void*)entry_buf ); 
    253     if (*p_entry) { 
    254         if (value == NULL) { 
    255             /* delete entry */ 
    256             PJ_LOG(6, ("hashtbl", "%p: p_entry %p deleted", ht, *p_entry)); 
    257             *p_entry = (*p_entry)->next; 
    258             --ht->count; 
    259              
    260         } else { 
    261             /* overwrite */ 
    262             (*p_entry)->value = value; 
    263             PJ_LOG(6, ("hashtbl", "%p: p_entry %p value set to %p", ht,  
    264                        *p_entry, value)); 
    265         } 
    266     } 
     286    hash_set(NULL, ht, key, keylen, hval, value, (void *)entry_buf, PJ_FALSE); 
     287} 
     288 
     289PJ_DEF(void) pj_hash_set_np_lower( pj_hash_table_t *ht, 
     290                                   const void *key, unsigned keylen, 
     291                                   pj_uint32_t hval, 
     292                                   pj_hash_entry_buf entry_buf, 
     293                                   void *value) 
     294{ 
     295    hash_set(NULL, ht, key, keylen, hval, value, (void *)entry_buf, PJ_TRUE); 
    267296} 
    268297 
Note: See TracChangeset for help on using the changeset viewer.