1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
|
/*
* $Id$
*
* ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version. The Blender
* Foundation also sells licenses for use in proprietary software under
* the Blender License. See http://www.blender.org/BL/ for information
* about this.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): none yet.
*
* ***** END GPL/BL DUAL LICENSE BLOCK *****
*
*/
/**
* \file BLO_readblenfile.c
* \brief This file handles the loading if .blend files
* \ingroup mainmodule
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef WIN32
#include <io.h> // read, open
#else // ! WIN32
#include <unistd.h> // read
#endif
#include "BLO_readStreamGlue.h"
#include "BLO_readfile.h"
#include "BLO_readblenfile.h"
#include "BKE_blender.h"
#define CACHESIZE 100000
/** Magic number for the file header */
char *headerMagic = "BLENDFI";
/**
* \brief Set the version number into the array.
*
* version contains the integer number of the version
* i.e. 227
* array[1] gets set to the div of the number by 100 i.e. 2
* array[2] gets the remainder i.e. 27
*/
void BLO_setversionnumber(char array[4], int version)
{
memset(array, 0, sizeof(array));
array[1] = version / 100;
array[2] = version % 100;
}
/**
* Sets version number using BLENDER_VERSION
* Function that calls the setversionnumber(char[],int) with
* the BLENDER_VERSION constant and sets the resultant array
* with the version parts.
* see BLO_setversionnumber(char[],int).
*/
void BLO_setcurrentversionnumber(char array[4])
{
BLO_setversionnumber(array, BLENDER_VERSION);
}
#ifndef O_BINARY
#define O_BINARY 0
#endif
/**
* Defines the data struct for the .blend file
*/
struct BLO_readblenfileStruct {
struct readStreamGlueStruct *streamGlue;
int fileDes;
unsigned int cacheSize;
unsigned int inCache;
unsigned int leftToRead;
unsigned int Seek;
int (*read)(struct BLO_readblenfileStruct *readblenfileStruct, void *buffer, int size);
char *readCache;
char *fromBuffer;
int fromBufferSize;
char crInBuffer;
char removeCR;
};
// declare static functions
static int readfromfilehandle(
struct BLO_readblenfileStruct *readblenfileStruct,
void *buffer,
int size);
static int readfrommemory(
struct BLO_readblenfileStruct *readblenfileStruct,
void *buffer,
int size);
static int fillcache(
struct BLO_readblenfileStruct *readblenfileStruct);
static unsigned int readfromcache(
struct BLO_readblenfileStruct *readblenfileStruct,
void * buffer,
unsigned int size);
static BlendFileData *readblenfilegeneric(
struct BLO_readblenfileStruct *readblenfileStruct,
BlendReadError *error_r);
// implementation of static functions
/**
* \brief Reads data from the already opened file.
* Given the file structure a buffer and the size of the block
* the function will read from the file if it is open.
* If not it will return -1 indicating an unopened file.
*
* \return Returns the size of the file read, or -1.
*/
static int readfromfilehandle(
struct BLO_readblenfileStruct *readblenfileStruct,
void *buffer,
int size)
{
int readsize = -1;
if (readblenfileStruct->fileDes != -1) {
readsize = read(readblenfileStruct->fileDes, buffer, size);
}
return(readsize);
}
/**
* \brief Reads and erases from readblenfileStruct->fromBuffer
*
* Copies information from the from the fromBuffer to the buffer, then
* decrements the size of the fromBuffer, and moves the pointer along
* thereby effectively removing the data forever.
*
* \return Returns the size of the read from memory
*/
static int readfrommemory(
struct BLO_readblenfileStruct *readblenfileStruct,
void *buffer,
int size)
{
int readsize = -1;
if (readblenfileStruct->fromBuffer) {
if (size > readblenfileStruct->fromBufferSize) {
size = readblenfileStruct->fromBufferSize;
}
memcpy(buffer, readblenfileStruct->fromBuffer, size);
readblenfileStruct->fromBufferSize -= size;
readblenfileStruct->fromBuffer += size;
readsize = size;
}
return(readsize);
}
/**
* Read in data from the file into a cache.
*
* \return Returns the size of the read.
*
* \attention Note: there is some code missing to return CR if the
* structure indicates it.
*/
static int fillcache(
struct BLO_readblenfileStruct *readblenfileStruct)
{
int readsize;
int toread;
// how many bytes can we read ?
toread = readblenfileStruct->leftToRead;
if (toread > readblenfileStruct->cacheSize) {
toread = readblenfileStruct->cacheSize;
}
readsize = readblenfileStruct->read(readblenfileStruct, readblenfileStruct->readCache, toread);
if (readsize > 0) {
if (readblenfileStruct->removeCR) {
// do some stuff here
}
readblenfileStruct->inCache = readsize;
readblenfileStruct->leftToRead -= readsize;
}
return (readsize);
}
/**
* \brief Read data from the cache into a buffer.
* Marks the last read location with a seek value.
*
* \return Returns the size of the read from the cache.
*
* \attention Note: missing some handling code if the location is
* \attention outside of the cache.
*/
static unsigned int readfromcache(
struct BLO_readblenfileStruct *readblenfileStruct,
void * buffer,
unsigned int size)
{
unsigned int readsize = 0;
if (readblenfileStruct->inCache - readblenfileStruct->Seek > size) {
memcpy(buffer, readblenfileStruct->readCache + readblenfileStruct->Seek, size);
readblenfileStruct->Seek += size;
readsize = size;
} else {
// handle me
}
return(readsize);
}
/**
* \brief Converts from BRS error code to BRE error code.
*
* Error conversion method to convert from
* the BRS type errors and return a BRE
* type error code.
* Decodes based on the function, the generic,
* and the specific portions of the error.
*/
static BlendReadError brs_to_bre(int err)
{
int errFunction = BRS_GETFUNCTION(err);
int errGeneric = BRS_GETGENERR(err);
int errSpecific = BRS_GETSPECERR(err);
if (errGeneric) {
switch (errGeneric) {
case BRS_MALLOC:
return BRE_OUT_OF_MEMORY;
case BRS_NULL:
return BRE_INTERNAL_ERROR;
case BRS_MAGIC:
return BRE_NOT_A_BLEND;
case BRS_CRCHEADER:
case BRS_CRCDATA:
return BRE_CORRUPT;
case BRS_DATALEN:
return BRE_INCOMPLETE;
case BRS_STUB:
return BRE_NOT_A_BLEND;
}
} else if (errSpecific) {
switch (errFunction) {
case BRS_READSTREAMGLUE:
switch (errSpecific) {
case BRS_UNKNOWN:
return BRE_INTERNAL_ERROR;
}
break;
case BRS_READSTREAMFILE:
switch (errSpecific) {
case BRS_NOTABLEND:
return BRE_NOT_A_BLEND;
case BRS_READERROR:
return BRE_UNABLE_TO_READ;
}
break;
case BRS_INFLATE:
switch (errSpecific) {
case BRS_INFLATEERROR:
return BRE_CORRUPT;
}
break;
case BRS_DECRYPT:
switch (errSpecific) {
case BRS_RSANEWERROR:
return BRE_INTERNAL_ERROR;
case BRS_DECRYPTERROR:
return BRE_INTERNAL_ERROR;
case BRS_NOTOURPUBKEY:
return BRE_NOT_ALLOWED;
}
break;
case BRS_VERIFY:
switch (errSpecific) {
case BRS_RSANEWERROR:
return BRE_INTERNAL_ERROR;
case BRS_SIGFAILED:
return BRE_INTERNAL_ERROR;
}
break;
}
}
return BRE_INVALID;
}
static BlendFileData *readblenfilegeneric(
struct BLO_readblenfileStruct *readblenfileStruct,
BlendReadError *error_r)
{
BlendFileData *bfd= NULL;
unsigned char reserved[BLO_RESERVEDSIZE];
uint8_t minversion[4];
uint8_t myversion[4];
uint8_t version[4];
uint8_t flags[4];
void *parms[2];
int filesize;
parms[0]= &bfd;
parms[1]= error_r;
BLO_setcurrentversionnumber(myversion);
readblenfileStruct->cacheSize = CACHESIZE;
readblenfileStruct->readCache = malloc(readblenfileStruct->cacheSize);
if (fillcache(readblenfileStruct) <= 0) {
*error_r = BRE_UNABLE_TO_READ;
} else if (readfromcache(readblenfileStruct, minversion, sizeof(minversion)) != sizeof(minversion)) {
*error_r = BRE_UNABLE_TO_READ;
} else if (memcmp(minversion, myversion, sizeof(minversion)) > 0) {
*error_r = BRE_TOO_NEW;
} else if (readfromcache(readblenfileStruct, version, sizeof(version)) != sizeof(version)) {
*error_r = BRE_UNABLE_TO_READ;
} else if (readfromcache(readblenfileStruct, flags, sizeof(flags)) != sizeof(flags)) {
*error_r = BRE_UNABLE_TO_READ;
} else if (readfromcache(readblenfileStruct, &filesize, sizeof(filesize)) != sizeof(filesize)) {
*error_r = BRE_UNABLE_TO_READ;
} else if (readfromcache(readblenfileStruct, reserved, sizeof(reserved)) != sizeof(reserved)) {
*error_r = BRE_UNABLE_TO_READ;
} else {
filesize = ntohl(filesize);
// substract number of bytes we've
// been handling outside readfromcache()
filesize -= strlen(headerMagic);
filesize--;
if (filesize < readblenfileStruct->inCache) {
// we've allready read more than we're supposed to
readblenfileStruct->inCache = filesize;
readblenfileStruct->leftToRead = 0;
} else {
//
readblenfileStruct->leftToRead = filesize - readblenfileStruct->inCache;
}
do {
int err;
*error_r = BRE_NONE;
err = readStreamGlue(
parms,
&(readblenfileStruct->streamGlue),
readblenfileStruct->readCache + readblenfileStruct->Seek,
readblenfileStruct->inCache - readblenfileStruct->Seek);
readblenfileStruct->inCache = 0;
readblenfileStruct->Seek = 0;
if (err) {
bfd = NULL;
/* If *error_r != BRE_NONE then it is
* blo_readstreamfile_end signaling an error
* in the loading code. Otherwise it is some
* other part of the streamglue system signalling
* and error so we convert the BRS error into
* a BRE error.
*
* Does this have to be so convoluted? No.
*/
if (*error_r == BRE_NONE) {
*error_r = brs_to_bre(err);
}
break;
}
} while (fillcache(readblenfileStruct) > 0);
}
free(readblenfileStruct->readCache);
readblenfileStruct->readCache = 0;
return bfd;
}
// implementation of exported functions
BlendFileData *
BLO_readblenfilememory(
char *fromBuffer,
int fromBufferSize,
BlendReadError *error_r)
{
static char *functionality_check= "\0FUNCTIONALITY_CHECK += BLO_readblenfilememory\n";
int magiclen = strlen(headerMagic);
BlendFileData *bfd = NULL;
if (!fromBuffer) {
*error_r = BRE_UNABLE_TO_OPEN;
} else if (fromBufferSize < magiclen) {
*error_r = BRE_UNABLE_TO_READ;
} else if (strncmp(fromBuffer, headerMagic, magiclen) != 0) {
*error_r = BRE_NOT_A_BLEND;
} else if (fromBufferSize < magiclen+1) {
*error_r = BRE_UNABLE_TO_READ;
} else if (fromBuffer[magiclen] != '\r' && fromBuffer[magiclen] != '\n') {
*error_r = BRE_NOT_A_BLEND;
} else {
int crnl;
fromBuffer+= magiclen;
fromBufferSize-= magiclen;
crnl = (fromBuffer[0] == '\r');
fromBuffer++;
fromBufferSize--;
if (crnl && fromBufferSize<1) {
*error_r = BRE_UNABLE_TO_READ;
} else {
struct BLO_readblenfileStruct *readblenfileStruct = NULL;
/* skip carriage return if necessary */
if (crnl) {
fromBuffer++;
fromBufferSize--;
}
// Allocate all the stuff we need
readblenfileStruct = calloc(sizeof(struct BLO_readblenfileStruct), 1);
readblenfileStruct->fileDes = -1;
readblenfileStruct->fromBuffer = fromBuffer;
readblenfileStruct->fromBufferSize = fromBufferSize;
readblenfileStruct->read = readfrommemory;
readblenfileStruct->removeCR = crnl;
// fake filesize for now until we've
// actually read in the filesize from the header
// make sure we don't read more bytes than there
// are left to handle accoding to fromBufferSize
readblenfileStruct->leftToRead = readblenfileStruct->fromBufferSize;
bfd = readblenfilegeneric(readblenfileStruct, error_r);
free(readblenfileStruct);
readblenfileStruct = 0;
}
}
return bfd;
}
BlendFileData *
BLO_readblenfilehandle(
int fd,
BlendReadError *error_r)
{
static char *functionality_check= "\0FUNCTIONALITY_CHECK += BLO_readblenfilehandle\n";
int magiclen = strlen(headerMagic);
BlendFileData *bfd = NULL;
char tempbuffer[256];
if (fd==-1) {
*error_r = BRE_UNABLE_TO_OPEN;
} else if (read(fd, tempbuffer, magiclen) != magiclen) {
*error_r = BRE_UNABLE_TO_READ;
} else if (strncmp(tempbuffer, headerMagic, magiclen) != 0 ) {
*error_r = BRE_NOT_A_BLEND;
} else if (read(fd, tempbuffer, 1) != 1) {
*error_r = BRE_UNABLE_TO_READ;
} else if (tempbuffer[0] != '\r' && tempbuffer[0] != '\n') {
*error_r = BRE_NOT_A_BLEND;
} else {
int crnl = (tempbuffer[0] == '\r');
if (crnl && read(fd, tempbuffer, 1)!=1) {
*error_r = BRE_UNABLE_TO_READ;
} else {
struct BLO_readblenfileStruct *readblenfileStruct;
// Allocate all the stuff we need
readblenfileStruct = calloc(sizeof(struct BLO_readblenfileStruct), 1);
readblenfileStruct->fileDes = fd;
readblenfileStruct->read = readfromfilehandle;
readblenfileStruct->removeCR = crnl;
// fake filesize for now until we've
// actually read in the filesize from the header
readblenfileStruct->leftToRead = CACHESIZE;
bfd = readblenfilegeneric(readblenfileStruct, error_r);
free(readblenfileStruct);
readblenfileStruct = 0;
}
}
return bfd;
}
BlendFileData *
BLO_readblenfilename(
char *fileName,
BlendReadError *error_r)
{
static char *functionality_check= "\0FUNCTIONALITY_CHECK += BLO_readblenfilename\n";
BlendFileData *bfd = NULL;
int fd;
fd = open(fileName, O_RDONLY | O_BINARY);
if (fd==-1) {
*error_r= BRE_UNABLE_TO_OPEN;
} else {
bfd = BLO_readblenfilehandle(fd, error_r);
}
if (fd!=-1)
close(fd);
return bfd;
}
/* Runtime reading */
static int handle_read_msb_int(int handle) {
unsigned char buf[4];
if (read(handle, buf, 4)!=4)
return -1;
else
return (buf[0]<<24) + (buf[1]<<16) + (buf[2]<<8) + (buf[3]<<0);
}
int blo_is_a_runtime(char *path) {
int res= 0, fd= open(path, O_BINARY|O_RDONLY, 0);
int datastart;
char buf[8];
if (fd==-1)
goto cleanup;
lseek(fd, -12, SEEK_END);
datastart= handle_read_msb_int(fd);
if (datastart==-1)
goto cleanup;
else if (read(fd, buf, 8)!=8)
goto cleanup;
else if (memcmp(buf, "BRUNTIME", 8)!=0)
goto cleanup;
else
res= 1;
cleanup:
if (fd!=-1)
close(fd);
return res;
}
BlendFileData *
blo_read_runtime(
char *path,
BlendReadError *error_r)
{
static char *functionality_check= "\0FUNCTIONALITY_CHECK += BLO_read_runtime\n";
BlendFileData *bfd= NULL;
int fd, datastart;
char buf[8];
fd= open(path, O_BINARY|O_RDONLY, 0);
if (fd==-1) {
*error_r= BRE_UNABLE_TO_OPEN;
goto cleanup;
}
lseek(fd, -12, SEEK_END);
datastart= handle_read_msb_int(fd);
if (datastart==-1) {
*error_r= BRE_UNABLE_TO_READ;
goto cleanup;
} else if (read(fd, buf, 8)!=8) {
*error_r= BRE_UNABLE_TO_READ;
goto cleanup;
} else if (memcmp(buf, "BRUNTIME", 8)!=0) {
*error_r= BRE_NOT_A_BLEND;
goto cleanup;
} else {
lseek(fd, datastart, SEEK_SET);
bfd= BLO_readblenfilehandle(fd, error_r);
}
cleanup:
if (fd!=-1)
close(fd);
return bfd;
}
#if 0
static char *brs_error_to_string(int err) {
int errFunction = BRS_GETFUNCTION(err);
int errGeneric = BRS_GETGENERR(err);
int errSpecific = BRS_GETSPECERR(err);
char *errFunctionStrings[] = {
"",
"The read stream",
"The read stream loopback",
"The key store",
"The file reading",
"Decompressing the file",
"Decrypting the file",
"Verifying the signature"};
char *errGenericStrings[] = {
"",
"generated an out of memory error",
"bumped on an internal programming error",
"did not recognize this as a blend file",
"failed a blend file check",
"bumped on corrupted data",
"needed the rest of the blend file",
"is not allowed in this version"};
char *errReadStreamGlueStrings[] = {
"",
"does not know how to proceed"};
char *errReadStreamFileStrings[] = {
"",
"did not recognize this as a blend file",
"was busted on a read error"};
char *errInflateStrings[] = {
"",
"bumped on a decompress error"};
char *errDecryptStrings[] = {
"",
"could not make a new key",
"bumped on a decrypt error",
"was not allowed. This blend file is not made by you."};
char *errVerifyStrings[] = {
"",
"could not make a new key",
"failed"};
char *errFunctionString= errFunctionStrings[errFunction];
char *errExtraString= "";
char *errString;
if (errGeneric) {
errExtraString= errGenericStrings[errGeneric];
} else if (errSpecific) {
switch (errFunction) {
case BRS_READSTREAMGLUE:
errExtraString= errReadStreamGlueStrings[errSpecific];
break;
case BRS_READSTREAMFILE:
errExtraString= errReadStreamFileStrings[errSpecific];
break;
case BRS_INFLATE:
errExtraString= errInflateStrings[errSpecific];
break;
case BRS_DECRYPT:
errExtraString= errDecryptStrings[errSpecific];
break;
case BRS_VERIFY:
errExtraString= errVerifyStrings[errSpecific];
break;
default:
break;
}
}
errString= MEM_mallocN(strlen(errFunctionString) + 1 + strlen(errExtraString) + 1);
sprintf(errString, "%s %s", errFunctionString, errExtraString);
return errString;
}
#endif
|