opj_j2k_check_poc_val(): fix starting index for checking layer dimension
[openjpeg.git] / src / lib / openjpip / cache_manager.c
1 /*
2  * $Id$
3  *
4  * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
5  * Copyright (c) 2002-2014, Professor Benoit Macq
6  * Copyright (c) 2010-2011, Kaori Hagihara
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include "cache_manager.h"
35
36 cachelist_param_t * gene_cachelist(void)
37 {
38     cachelist_param_t *cachelist;
39
40     cachelist = (cachelist_param_t *)malloc(sizeof(cachelist_param_t));
41
42     cachelist->first = NULL;
43     cachelist->last  = NULL;
44
45     return cachelist;
46 }
47
48 void delete_cachelist(cachelist_param_t **cachelist)
49 {
50     cache_param_t *cachePtr, *cacheNext;
51
52     cachePtr = (*cachelist)->first;
53     while (cachePtr != NULL) {
54         cacheNext = cachePtr->next;
55         delete_cache(&cachePtr);
56         cachePtr = cacheNext;
57     }
58     free(*cachelist);
59 }
60
61 cache_param_t * gene_cache(const char *targetname, int csn, char *tid,
62                            char *cid)
63 {
64     cache_param_t *cache;
65
66     cache = (cache_param_t *)malloc(sizeof(cache_param_t));
67     cache->filename = strdup(targetname);
68     cache->tid = strdup(tid);
69     cache->csn = csn;
70     cache->cid = (char **)malloc(sizeof(char *));
71     *cache->cid = strdup(cid);
72     cache->numOfcid = 1;
73 #if 1
74     cache->metadatalist = NULL;
75 #else
76     cache->metadatalist = gene_metadatalist();
77 #endif
78     cache->ihdrbox = NULL;
79     cache->next = NULL;
80
81     return cache;
82 }
83
84 void delete_cache(cache_param_t **cache)
85 {
86     int i;
87
88     free((*cache)->filename);
89     free((*cache)->tid);
90
91     delete_metadatalist(&(*cache)->metadatalist);
92
93     if ((*cache)->ihdrbox) {
94         free((*cache)->ihdrbox);
95     }
96     for (i = 0; i < (*cache)->numOfcid; i++) {
97         free((*cache)->cid[i]);
98     }
99     free((*cache)->cid);
100     free(*cache);
101 }
102
103 void insert_cache_into_list(cache_param_t *cache, cachelist_param_t *cachelist)
104 {
105     if (cachelist->first) {
106         cachelist->last->next = cache;
107     } else {
108         cachelist->first = cache;
109     }
110     cachelist->last = cache;
111 }
112
113 cache_param_t * search_cache(const char targetname[],
114                              cachelist_param_t *cachelist)
115 {
116     cache_param_t *foundcache;
117
118     if (!targetname) {
119         return NULL;
120     }
121
122     foundcache = cachelist->first;
123
124     while (foundcache != NULL) {
125
126         if (strcmp(targetname, foundcache->filename) == 0) {
127             return foundcache;
128         }
129
130         foundcache = foundcache->next;
131     }
132     return NULL;
133 }
134
135 cache_param_t * search_cacheBycsn(int csn, cachelist_param_t *cachelist)
136 {
137     cache_param_t *foundcache;
138
139     foundcache = cachelist->first;
140
141     while (foundcache != NULL) {
142
143         if (csn == foundcache->csn) {
144             return foundcache;
145         }
146         foundcache = foundcache->next;
147     }
148     return NULL;
149 }
150
151 cache_param_t * search_cacheBycid(const char cid[],
152                                   cachelist_param_t *cachelist)
153 {
154     cache_param_t *foundcache;
155     int i;
156
157     if (!cid) {
158         return NULL;
159     }
160
161     foundcache = cachelist->first;
162
163     while (foundcache != NULL) {
164         for (i = 0; i < foundcache->numOfcid; i++)
165             if (strcmp(cid, foundcache->cid[i]) == 0) {
166                 return foundcache;
167             }
168         foundcache = foundcache->next;
169     }
170     return NULL;
171 }
172
173 cache_param_t * search_cacheBytid(const char tid[],
174                                   cachelist_param_t *cachelist)
175 {
176     cache_param_t *foundcache;
177
178     if (!tid) {
179         return NULL;
180     }
181
182     foundcache = cachelist->first;
183
184     while (foundcache != NULL) {
185         if (strcmp(tid, foundcache->tid) == 0) {
186             return foundcache;
187         }
188         foundcache = foundcache->next;
189     }
190     return NULL;
191 }
192
193 void add_cachecid(const char *cid, cache_param_t *cache)
194 {
195     if (!cid) {
196         return;
197     }
198
199     if ((cache->cid = realloc(cache->cid,
200                               (OPJ_SIZE_T)(cache->numOfcid + 1) * sizeof(char *))) == NULL) {
201         fprintf(stderr, "failed to add new cid to cache table in add_cachecid()\n");
202         return;
203     }
204
205     cache->cid[ cache->numOfcid] = strdup(cid);
206
207     cache->numOfcid ++;
208 }
209
210 void update_cachetid(const char *tid, cache_param_t *cache)
211 {
212     if (!tid) {
213         return;
214     }
215
216     if (tid[0] != '0' && strcmp(tid, cache->tid) != 0) {
217         fprintf(stderr, "tid is updated to %s for %s\n", tid, cache->filename);
218         free(cache->tid);
219         cache->tid = strdup(tid);
220     }
221 }
222
223 void remove_cidInCache(const char *cid, cache_param_t *cache);
224
225 void remove_cachecid(const char *cid, cachelist_param_t *cachelist)
226 {
227     cache_param_t *cache;
228
229     cache = search_cacheBycid(cid, cachelist);
230     remove_cidInCache(cid, cache);
231 }
232
233 void remove_cidInCache(const char *cid, cache_param_t *cache)
234 {
235     int idx = -1;
236     char **tmp;
237     int i, j;
238
239     for (i = 0; i < cache->numOfcid; i++)
240         if (strcmp(cid, cache->cid[i]) == 0) {
241             idx = i;
242             break;
243         }
244
245     if (idx == -1) {
246         fprintf(stderr, "cid: %s not found\n", cid);
247         return;
248     }
249
250     tmp = cache->cid;
251
252     cache->cid = (char **)malloc((OPJ_SIZE_T)(cache->numOfcid - 1) * sizeof(
253                                      char *));
254
255     for (i = 0, j = 0; i < cache->numOfcid; i++) {
256         if (i != idx) {
257             cache->cid[j] = strdup(tmp[i]);
258             j++;
259         }
260         free(tmp[i]);
261     }
262     free(tmp);
263
264     cache->numOfcid --;
265 }
266
267 void print_cache(cache_param_t *cache)
268 {
269     int i;
270
271     fprintf(stdout, "cache\n");
272     fprintf(stdout, "\t filename: %s\n", cache->filename);
273     fprintf(stdout, "\t tid: %s\n", cache->tid);
274     fprintf(stdout, "\t csn: %d\n", cache->csn);
275     fprintf(stdout, "\t cid:");
276
277     for (i = 0; i < cache->numOfcid; i++) {
278         fprintf(stdout, " %s", cache->cid[i]);
279     }
280     fprintf(stdout, "\n");
281 }
282
283 void print_allcache(cachelist_param_t *cachelist)
284 {
285     cache_param_t *ptr;
286
287     fprintf(stdout, "cache list\n");
288
289     ptr = cachelist->first;
290     while (ptr != NULL) {
291         print_cache(ptr);
292         ptr = ptr->next;
293     }
294 }