source code open
[framework/security/cert-svc.git] / tests / capi / crl_cache.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file        crl_cache.h
18  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @version     1.0
20  * @brief       Example implementation of memory cache for crl.
21  */
22 #ifndef _CRL_MEMORY_CACHE_H_
23 #define _CRL_MEMORY_CACHE_H_
24
25 #include <map>
26 #include <string>
27 #include <vector>
28
29 #include <string.h>
30 #include <time.h>
31
32 typedef std::vector<char> BinaryBuffer;
33
34 typedef struct CrlRecord_t {
35     BinaryBuffer buffer;
36     time_t nextUpdate;
37 } CrlRecord;
38
39 typedef std::map<std::string,CrlRecord> MemoryCache;
40
41 void memoryCacheWrite(
42     const char *distributionPoint,
43     const char *body,
44     int bodySize,
45     time_t nextUpdateTime,
46     void *userParam)
47 {
48     MemoryCache *cache = static_cast<MemoryCache*>(userParam);
49
50     CrlRecord record;
51     record.buffer.resize(bodySize);
52     memcpy(&record.buffer[0], body, bodySize);
53     record.nextUpdate = nextUpdateTime;
54
55     cache->insert(std::make_pair(std::string(distributionPoint),record));
56 }
57
58 int memoryCacheRead(
59     const char *distributorPoint,
60     char **body,
61     int *bodySize,
62     time_t *nextUpdateTime,
63     void *userParam)
64 {
65     MemoryCache *cache = static_cast<MemoryCache*>(userParam);
66     auto iter = cache->find(distributorPoint);
67     if (iter == cache->end()) {
68         return 0;
69     }
70     CrlRecord record = iter->second;
71     *bodySize = record.buffer.size();
72     *body = new char[*bodySize];
73     memcpy(*body, &record.buffer[0], *bodySize);
74     *nextUpdateTime = record.nextUpdate;
75     return 1;
76 }
77
78 void memoryCacheFree(
79     char *buffer,
80     void *)
81 {
82     delete[] buffer;
83 }
84
85 #endif // _CRL_MEMORY_CACHE_H_
86