fix crash [#27158] Appending crash with preview.
[blender-staging.git] / source / blender / blenloader / intern / readblenentry.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  * .blend file reading entry point
29  */
30
31 /** \file blender/blenloader/intern/readblenentry.c
32  *  \ingroup blenloader
33  */
34
35
36 #include <stddef.h>
37 #include "BLI_storage.h" /* _LARGEFILE_SOURCE */
38
39 #include <stdlib.h>
40 #include <string.h>
41 #include <stdio.h>
42 #include <math.h>
43
44 #include "MEM_guardedalloc.h"
45
46 #include "BLI_utildefines.h"
47 #include "BLI_ghash.h"
48 #include "BLI_linklist.h"
49
50 #include "DNA_genfile.h"
51 #include "DNA_sdna_types.h"
52
53
54 #include "BKE_main.h"
55 #include "BKE_library.h" // for free_main
56 #include "BKE_idcode.h"
57 #include "BKE_report.h"
58 #include "BKE_utildefines.h"
59
60 #include "BLO_readfile.h"
61 #include "BLO_undofile.h"
62
63 #include "readfile.h"
64
65 #include "BLO_readblenfile.h"
66
67 #include "BLO_sys_types.h" // needed for intptr_t
68
69 #ifdef _WIN32
70 #include "BLI_winstuff.h"
71 #endif
72
73 /* local prototypes --------------------- */
74 void BLO_blendhandle_print_sizes(BlendHandle *, void *); 
75
76         /* Access routines used by filesel. */
77          
78 BlendHandle *BLO_blendhandle_from_file(char *file, ReportList *reports)
79 {
80         BlendHandle *bh;
81
82         bh= (BlendHandle*)blo_openblenderfile(file, reports);
83
84         return bh;
85 }
86
87 BlendHandle *BLO_blendhandle_from_memory(void *mem, int memsize)
88 {
89         BlendHandle *bh;
90
91         bh= (BlendHandle*)blo_openblendermemory(mem, memsize, NULL);
92
93         return bh;
94 }
95
96 void BLO_blendhandle_print_sizes(BlendHandle *bh, void *fp) 
97 {
98         FileData *fd= (FileData*) bh;
99         BHead *bhead;
100
101         fprintf(fp, "[\n");
102         for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
103                 if (bhead->code==ENDB)
104                         break;
105                 else {
106                         short *sp= fd->filesdna->structs[bhead->SDNAnr];
107                         char *name= fd->filesdna->types[ sp[0] ];
108                         char buf[4];
109                         
110                         buf[0]= (bhead->code>>24)&0xFF;
111                         buf[1]= (bhead->code>>16)&0xFF;
112                         buf[2]= (bhead->code>>8)&0xFF;
113                         buf[3]= (bhead->code>>0)&0xFF;
114                         
115                         buf[0]= buf[0]?buf[0]:' ';
116                         buf[1]= buf[1]?buf[1]:' ';
117                         buf[2]= buf[2]?buf[2]:' ';
118                         buf[3]= buf[3]?buf[3]:' ';
119                         
120                         fprintf(fp, "['%.4s', '%s', %d, %ld ], \n", buf, name, bhead->nr, (long int)bhead->len+sizeof(BHead));
121                 }
122         }
123         fprintf(fp, "]\n");
124 }
125
126 LinkNode *BLO_blendhandle_get_datablock_names(BlendHandle *bh, int ofblocktype, int *tot_names)
127 {
128         FileData *fd= (FileData*) bh;
129         LinkNode *names= NULL;
130         BHead *bhead;
131         int tot= 0;
132
133         for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
134                 if (bhead->code==ofblocktype) {
135                         char *idname= bhead_id_name(fd, bhead);
136
137                         BLI_linklist_prepend(&names, strdup(idname+2));
138                         tot++;
139                 } else if (bhead->code==ENDB)
140                         break;
141         }
142
143         *tot_names= tot;
144         return names;
145 }
146
147 LinkNode *BLO_blendhandle_get_previews(BlendHandle *bh, int ofblocktype, int *tot_prev)
148 {
149         FileData *fd= (FileData*) bh;
150         LinkNode *previews= NULL;
151         BHead *bhead;
152         int looking=0;
153         int npreviews = 0;
154         PreviewImage* prv = NULL;
155         PreviewImage* new_prv = NULL;
156         int tot= 0;
157         
158         for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
159                 if (bhead->code==ofblocktype) {
160                         ID *id= (ID*) (bhead+1);
161                         switch(GS(id->name))
162                         {
163                                 case ID_MA: /* fall through */
164                                 case ID_TE: /* fall through */
165                                 case ID_IM: /* fall through */
166                                 case ID_WO: /* fall through */
167                                 case ID_LA: /* fall through */
168                                         new_prv = MEM_callocN(sizeof(PreviewImage), "newpreview");
169                                         BLI_linklist_prepend(&previews, new_prv);
170                                         tot++;
171                                         looking = 1;
172                                         break;
173                                 default:
174                                         break;
175                         }
176                 } else if (bhead->code==DATA) {
177                         if (looking) {
178                                 if (bhead->SDNAnr == DNA_struct_find_nr(fd->filesdna, "PreviewImage") ) {
179                                         prv = (PreviewImage*) (bhead+1);
180                                         npreviews = 0;                          
181                                         memcpy(new_prv, prv, sizeof(PreviewImage));
182                                         if (prv->rect[0]) {
183                                                 unsigned int *rect = NULL;
184                                                 // int rectlen = 0;
185                                                 new_prv->rect[0] = MEM_callocN(new_prv->w[0]*new_prv->h[0]*sizeof(unsigned int), "prvrect");
186                                                 bhead= blo_nextbhead(fd, bhead);
187                                                 rect = (unsigned int*)(bhead+1);
188                                                 // rectlen = new_prv->w[0]*new_prv->h[0]*sizeof(unsigned int);
189                                                 memcpy(new_prv->rect[0], rect, bhead->len);                                     
190                                         } else {
191                                                 new_prv->rect[0] = NULL;
192                                         }
193                                         
194                                         if (prv->rect[1]) {
195                                                 unsigned int *rect = NULL;
196                                                 // int rectlen = 0;
197                                                 new_prv->rect[1] = MEM_callocN(new_prv->w[1]*new_prv->h[1]*sizeof(unsigned int), "prvrect");
198                                                 bhead= blo_nextbhead(fd, bhead);
199                                                 rect = (unsigned int*)(bhead+1);
200                                                 // rectlen = new_prv->w[1]*new_prv->h[1]*sizeof(unsigned int);
201                                                 memcpy(new_prv->rect[1], rect, bhead->len);                                                     
202                                         } else {
203                                                 new_prv->rect[1] = NULL;
204                                         }
205                                 }
206                         }
207                 } else if (bhead->code==ENDB) {
208                         break;
209                 } else if (bhead->code==DATA) {
210                         /* DATA blocks between IDBlock and Preview */
211                 } else {
212                         looking = 0;
213                         new_prv = NULL;
214                         prv = NULL;
215                 }
216                 
217         }
218
219         *tot_prev= tot;
220         return previews;
221 }
222
223 LinkNode *BLO_blendhandle_get_linkable_groups(BlendHandle *bh) 
224 {
225         FileData *fd= (FileData*) bh;
226         GHash *gathered= BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "linkable_groups gh");
227         LinkNode *names= NULL;
228         BHead *bhead;
229         
230         for (bhead= blo_firstbhead(fd); bhead; bhead= blo_nextbhead(fd, bhead)) {
231                 if (bhead->code==ENDB) {
232                         break;
233                 } else if (BKE_idcode_is_valid(bhead->code)) {
234                         if (BKE_idcode_is_linkable(bhead->code)) {
235                                 const char *str= BKE_idcode_to_name(bhead->code);
236                                 
237                                 if (!BLI_ghash_haskey(gathered, (void *)str)) {
238                                         BLI_linklist_prepend(&names, strdup(str));
239                                         BLI_ghash_insert(gathered, (void *)str, NULL);
240                                 }
241                         }
242                 }
243         }
244         
245         BLI_ghash_free(gathered, NULL, NULL);
246         
247         return names;
248 }               
249
250 void BLO_blendhandle_close(BlendHandle *bh) {
251         FileData *fd= (FileData*) bh;
252         
253         blo_freefiledata(fd);
254 }
255
256         /**********/
257
258 BlendFileData *BLO_read_from_file(const char *file, ReportList *reports)
259 {
260         BlendFileData *bfd = NULL;
261         FileData *fd;
262                 
263         fd = blo_openblenderfile(file, reports);
264         if (fd) {
265                 fd->reports= reports;
266                 bfd= blo_read_file_internal(fd, file);
267                 blo_freefiledata(fd);                   
268         }
269
270         return bfd;     
271 }
272
273 BlendFileData *BLO_read_from_memory(void *mem, int memsize, ReportList *reports)
274 {
275         BlendFileData *bfd = NULL;
276         FileData *fd;
277                 
278         fd = blo_openblendermemory(mem, memsize,  reports);
279         if (fd) {
280                 fd->reports= reports;
281                 bfd= blo_read_file_internal(fd, "");
282                 blo_freefiledata(fd);                   
283         }
284
285         return bfd;     
286 }
287
288 BlendFileData *BLO_read_from_memfile(Main *oldmain, const char *filename, MemFile *memfile, ReportList *reports)
289 {
290         BlendFileData *bfd = NULL;
291         FileData *fd;
292         ListBase mainlist;
293         
294         fd = blo_openblendermemfile(memfile, reports);
295         if (fd) {
296                 fd->reports= reports;
297                 strcpy(fd->relabase, filename);
298                 
299                 /* clear ob->proxy_from pointers in old main */
300                 blo_clear_proxy_pointers_from_lib(oldmain);
301
302                 /* separate libraries from old main */
303                 blo_split_main(&mainlist, oldmain);
304                 /* add the library pointers in oldmap lookup */
305                 blo_add_library_pointer_map(&mainlist, fd);
306                 
307                 /* makes lookup of existing images in old main */
308                 blo_make_image_pointer_map(fd, oldmain);
309                 
310                 bfd= blo_read_file_internal(fd, filename);
311                 
312                 /* ensures relinked images are not freed */
313                 blo_end_image_pointer_map(fd, oldmain);
314                 
315                 /* move libraries from old main to new main */
316                 if(bfd && mainlist.first!=mainlist.last) {
317                         
318                         /* Library structs themselves */
319                         bfd->main->library= oldmain->library;
320                         oldmain->library.first= oldmain->library.last= NULL;
321                         
322                         /* add the Library mainlist to the new main */
323                         BLI_remlink(&mainlist, oldmain);
324                         BLI_addhead(&mainlist, bfd->main);
325                 }
326                 blo_join_main(&mainlist);
327                 
328                 blo_freefiledata(fd);                   
329         }
330
331         return bfd;     
332 }
333
334 void BLO_blendfiledata_free(BlendFileData *bfd)
335 {
336         if (bfd->main) {
337                 free_main(bfd->main);
338         }
339         
340         if (bfd->user) {
341                 MEM_freeN(bfd->user);
342         }
343
344         MEM_freeN(bfd);
345 }
346