• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #include "hdf_cstring.h"
10 #include "hdf_log.h"
11 #include "osal_mem.h"
12 #include "securec.h"
13 
14 #define CSTRING_MAX 1024
15 
HdfStringMakeHashKey(const char * key,uint32_t mask)16 uint32_t HdfStringMakeHashKey(const char *key, uint32_t mask)
17 {
18     uint32_t hashValue = 0;
19     const uint32_t seed = 131; // 31 131 1313 13131 131313 etc..
20     while ((key != NULL) && *key) {
21         hashValue = hashValue * seed + (*key++);
22     }
23     return (hashValue & 0x7FFFFFFF) | mask;
24 }
25 
HdfCStringObtain(const char * str)26 struct HdfCString *HdfCStringObtain(const char *str)
27 {
28     struct HdfCString *instance = NULL;
29     if (str != NULL) {
30         size_t strLen = strlen(str);
31         if (strLen > CSTRING_MAX) {
32             return NULL;
33         }
34         size_t size = sizeof(struct HdfCString) + strLen + 1;
35         instance = (struct HdfCString *)OsalMemCalloc(size);
36         if (instance == NULL) {
37             HDF_LOGE("HdfCStringObtain failed, alloc memory failed");
38             return NULL;
39         }
40         if (strncpy_s(instance->value, strLen + 1, str, strLen) != EOK) {
41             HDF_LOGE("HdfCStringObtain failed, strncpy_s failed");
42             OsalMemFree(instance);
43             return NULL;
44         }
45         instance->size = strLen;
46     }
47     return instance;
48 }
49 
HdfCStringRecycle(struct HdfCString * inst)50 void HdfCStringRecycle(struct HdfCString *inst)
51 {
52     if (inst != NULL) {
53         OsalMemFree(inst);
54     }
55 }
56