-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
rres-raylib.h
1094 lines (929 loc) · 51.6 KB
/
rres-raylib.h
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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**********************************************************************************************
*
* rres-raylib v1.2 - rres loaders specific for raylib data structures
*
* CONFIGURATION:
*
* #define RRES_RAYLIB_IMPLEMENTATION
* Generates the implementation of the library into the included file.
* If not defined, the library is in header only mode and can be included in other headers
* or source files without problems. But only ONE file should hold the implementation.
*
* #define RRES_SUPPORT_COMPRESSION_LZ4
* Support data compression algorithm LZ4, provided by lz4.h/lz4.c library
*
* #define RRES_SUPPORT_ENCRYPTION_AES
* Support data encryption algorithm AES, provided by aes.h/aes.c library
*
* #define RRES_SUPPORT_ENCRYPTION_XCHACHA20
* Support data encryption algorithm XChaCha20-Poly1305,
* provided by monocypher.h/monocypher.c library
*
* DEPENDENCIES:
*
* - raylib.h: Data types definition and data loading from memory functions
* WARNING: raylib.h MUST be included before including rres-raylib.h
* - rres.h: Base implementation of rres specs, required to read rres files and resource chunks
* - lz4.h: LZ4 compression support (optional)
* - aes.h: AES-256 CTR encryption support (optional)
* - monocypher.h: for XChaCha20-Poly1305 encryption support (optional)
*
* VERSION HISTORY:
*
* - 1.2 (15-Apr-2023): Updated to monocypher 4.0.1
* - 1.0 (11-May-2022): Initial implementation release
*
*
* LICENSE: MIT
*
* Copyright (c) 2020-2024 Ramon Santamaria (@raysan5)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
**********************************************************************************************/
#ifndef RRES_RAYLIB_H
#define RRES_RAYLIB_H
#ifndef RRES_H
#include "rres.h"
#endif
//----------------------------------------------------------------------------------
// Defines and Macros
//----------------------------------------------------------------------------------
//...
//----------------------------------------------------------------------------------
// Types and Structures Definition
//----------------------------------------------------------------------------------
//...
//----------------------------------------------------------------------------------
// Global variables
//----------------------------------------------------------------------------------
//...
//----------------------------------------------------------------------------------
// Module Functions Declaration
//----------------------------------------------------------------------------------
#if defined(__cplusplus)
extern "C" { // Prevents name mangling of functions
#endif
// rres data loading to raylib data structures
// NOTE: Chunk data must be provided uncompressed/unencrypted
RLAPI void *LoadDataFromResource(rresResourceChunk chunk, unsigned int *size); // Load raw data from rres resource chunk
RLAPI char *LoadTextFromResource(rresResourceChunk chunk); // Load text data from rres resource chunk
RLAPI Image LoadImageFromResource(rresResourceChunk chunk); // Load Image data from rres resource chunk
RLAPI Wave LoadWaveFromResource(rresResourceChunk chunk); // Load Wave data from rres resource chunk
RLAPI Font LoadFontFromResource(rresResourceMulti multi); // Load Font data from rres resource multiple chunks
RLAPI Mesh LoadMeshFromResource(rresResourceMulti multi); // Load Mesh data from rres resource multiple chunks
// Unpack resource chunk data (decompres/decrypt data)
// NOTE: Function return 0 on success or other value on failure
RLAPI int UnpackResourceChunk(rresResourceChunk *chunk); // Unpack resource chunk data (decompress/decrypt)
// Set base directory for externally linked data
// NOTE: When resource chunk contains an external link (FourCC: LINK, Type: RRES_DATA_LINK),
// a base directory is required to be prepended to link path
// If not provided, the application path is prepended to link by default
RLAPI void SetBaseDirectory(const char *baseDir); // Set base directory for externally linked data
#if defined(__cplusplus)
}
#endif
#endif // RRES_RAYLIB_H
/***********************************************************************************
*
* RRES RAYLIB IMPLEMENTATION
*
************************************************************************************/
#if defined(RRES_RAYLIB_IMPLEMENTATION)
// Compression/Encryption algorithms supported
// NOTE: They should be the same supported by the rres packaging tool (rrespacker)
// https://github.com/phoboslab/qoi
#include "external/qoi.h" // Compression algorithm: QOI (implementation in raylib)
#if defined(RRES_SUPPORT_COMPRESSION_LZ4)
// https://github.com/lz4/lz4
#include "external/lz4.h" // Compression algorithm: LZ4
#include "external/lz4.c" // Compression algorithm implementation: LZ4
#endif
#if defined(RRES_SUPPORT_ENCRYPTION_AES)
// https://github.com/kokke/tiny-AES-c
#include "external/aes.h" // Encryption algorithm: AES
#include "external/aes.c" // Encryption algorithm implementation: AES
#endif
#if defined(RRES_SUPPORT_ENCRYPTION_XCHACHA20)
// https://github.com/LoupVaillant/Monocypher
#include "external/monocypher.h" // Encryption algorithm: XChaCha20-Poly1305
#include "external/monocypher.c" // Encryption algorithm implementation: XChaCha20-Poly1305
#endif
//----------------------------------------------------------------------------------
// Defines and Macros
//----------------------------------------------------------------------------------
//...
//----------------------------------------------------------------------------------
// Types and Structures Definition
//----------------------------------------------------------------------------------
//...
//----------------------------------------------------------------------------------
// Global Variables Definition
//----------------------------------------------------------------------------------
static const char *baseDir = NULL; // Base directory pointer, used on external linked data loading
//----------------------------------------------------------------------------------
// Module specific Functions Declaration
//----------------------------------------------------------------------------------
// Load simple data chunks that are later required by multi-chunk resources
// NOTE: Chunk data must be provided uncompressed/unencrypted
static void *LoadDataFromResourceLink(rresResourceChunk chunk, unsigned int *size); // Load chunk: RRES_DATA_LINK
static void *LoadDataFromResourceChunk(rresResourceChunk chunk, unsigned int *size); // Load chunk: RRES_DATA_RAW
static char *LoadTextFromResourceChunk(rresResourceChunk chunk, unsigned int *codeLang); // Load chunk: RRES_DATA_TEXT
static Image LoadImageFromResourceChunk(rresResourceChunk chunk); // Load chunk: RRES_DATA_IMAGE
static const char *GetExtensionFromProps(unsigned int ext01, unsigned int ext02); // Get file extension from RRES_DATA_RAW properties (unsigned int)
static unsigned int *ComputeMD5(const unsigned char *data, int size); // Compute MD5 hash code, returns 4 integers array (static)
//----------------------------------------------------------------------------------
// Module Functions Definition
//----------------------------------------------------------------------------------
// Load raw data from rres resource
void *LoadDataFromResource(rresResourceChunk chunk, unsigned int *size)
{
void *rawData = NULL;
// Data can be provided in the resource or linked to an external file
if (rresGetDataType(chunk.info.type) == RRES_DATA_RAW) // Raw data
{
rawData = LoadDataFromResourceChunk(chunk, size);
}
else if (rresGetDataType(chunk.info.type) == RRES_DATA_LINK) // Link to external file
{
// Get raw data from external linked file
unsigned int dataSize = 0;
void *data = LoadDataFromResourceLink(chunk, &dataSize);
rawData = data;
*size = dataSize;
}
return rawData;
}
// Load text data from rres resource
// NOTE: Text must be NULL terminated
char *LoadTextFromResource(rresResourceChunk chunk)
{
char *text = NULL;
unsigned int codeLang = 0;
if (rresGetDataType(chunk.info.type) == RRES_DATA_TEXT) // Text data
{
text = LoadTextFromResourceChunk(chunk, &codeLang);
// TODO: Consider text code language to load shader or code scripts
}
else if (rresGetDataType(chunk.info.type) == RRES_DATA_RAW) // Raw text file
{
unsigned int size = 0;
text = (char *)LoadDataFromResourceChunk(chunk, &size);
}
else if (rresGetDataType(chunk.info.type) == RRES_DATA_LINK) // Link to external file
{
// Get raw data from external linked file
unsigned int dataSize = 0;
void *data = LoadDataFromResourceLink(chunk, &dataSize);
text = (char *)data;
}
return text;
}
// Load Image data from rres resource
Image LoadImageFromResource(rresResourceChunk chunk)
{
Image image = { 0 };
if (rresGetDataType(chunk.info.type) == RRES_DATA_IMAGE) // Image data
{
image = LoadImageFromResourceChunk(chunk);
}
else if (rresGetDataType(chunk.info.type) == RRES_DATA_RAW) // Raw image file
{
unsigned int dataSize = 0;
unsigned char *data = (unsigned char *)LoadDataFromResourceChunk(chunk, &dataSize);
image = LoadImageFromMemory(GetExtensionFromProps(chunk.data.props[1], chunk.data.props[2]), data, dataSize);
RL_FREE(data);
}
else if (rresGetDataType(chunk.info.type) == RRES_DATA_LINK) // Link to external file
{
// Get raw data from external linked file
unsigned int dataSize = 0;
void *data = LoadDataFromResourceLink(chunk, &dataSize);
// Load image from linked file data
// NOTE: Function checks internally if the file extension is supported to
// properly load the data, if it fails it logs the result and image.data = NULL
image = LoadImageFromMemory(GetFileExtension((char *)chunk.data.raw), (unsigned char *)data, dataSize);
}
return image;
}
// Load Wave data from rres resource
Wave LoadWaveFromResource(rresResourceChunk chunk)
{
Wave wave = { 0 };
if (rresGetDataType(chunk.info.type) == RRES_DATA_WAVE) // Wave data
{
if ((chunk.info.compType == RRES_COMP_NONE) && (chunk.info.cipherType == RRES_CIPHER_NONE))
{
wave.frameCount = chunk.data.props[0];
wave.sampleRate = chunk.data.props[1];
wave.sampleSize = chunk.data.props[2];
wave.channels = chunk.data.props[3];
unsigned int size = wave.frameCount*wave.sampleSize*wave.channels/8;
wave.data = RL_CALLOC(size, 1);
memcpy(wave.data, chunk.data.raw, size);
}
RRES_LOG("RRES: %c%c%c%c: WARNING: Data must be decompressed/decrypted\n", chunk.info.type[0], chunk.info.type[1], chunk.info.type[2], chunk.info.type[3]);
}
else if (rresGetDataType(chunk.info.type) == RRES_DATA_RAW) // Raw wave file
{
unsigned int dataSize = 0;
unsigned char *data = (unsigned char *)LoadDataFromResourceChunk(chunk, &dataSize);
wave = LoadWaveFromMemory(GetExtensionFromProps(chunk.data.props[1], chunk.data.props[2]), data, dataSize);
RL_FREE(data);
}
else if (rresGetDataType(chunk.info.type) == RRES_DATA_LINK) // Link to external file
{
// Get raw data from external linked file
unsigned int dataSize = 0;
void *data = LoadDataFromResourceLink(chunk, &dataSize);
// Load wave from linked file data
// NOTE: Function checks internally if the file extension is supported to
// properly load the data, if it fails it logs the result and wave.data = NULL
wave = LoadWaveFromMemory(GetFileExtension((char *)chunk.data.raw), (unsigned char *)data, dataSize);
}
return wave;
}
// Load Font data from rres resource
Font LoadFontFromResource(rresResourceMulti multi)
{
Font font = { 0 };
// Font resource consist of (2) chunks:
// - RRES_DATA_FONT_GLYPHS: Basic font and glyphs properties/data
// - RRES_DATA_IMAGE: Image atlas for the font characters
if (multi.count >= 2)
{
if (rresGetDataType(multi.chunks[0].info.type) == RRES_DATA_FONT_GLYPHS)
{
if ((multi.chunks[0].info.compType == RRES_COMP_NONE) && (multi.chunks[0].info.cipherType == RRES_CIPHER_NONE))
{
// Load font basic properties from chunk[0]
font.baseSize = multi.chunks[0].data.props[0]; // Base size (default chars height)
font.glyphCount = multi.chunks[0].data.props[1]; // Number of characters (glyphs)
font.glyphPadding = multi.chunks[0].data.props[2]; // Padding around the chars
font.recs = (Rectangle *)RL_CALLOC(font.glyphCount, sizeof(Rectangle));
font.glyphs = (GlyphInfo *)RL_CALLOC(font.glyphCount, sizeof(GlyphInfo));
for (int i = 0; i < font.glyphCount; i++)
{
// Font glyphs info comes as a data blob
font.recs[i].x = (float)((rresFontGlyphInfo *)multi.chunks[0].data.raw)[i].x;
font.recs[i].y = (float)((rresFontGlyphInfo *)multi.chunks[0].data.raw)[i].y;
font.recs[i].width = (float)((rresFontGlyphInfo *)multi.chunks[0].data.raw)[i].width;
font.recs[i].height = (float)((rresFontGlyphInfo *)multi.chunks[0].data.raw)[i].height;
font.glyphs[i].value = ((rresFontGlyphInfo *)multi.chunks[0].data.raw)[i].value;
font.glyphs[i].offsetX = ((rresFontGlyphInfo *)multi.chunks[0].data.raw)[i].offsetX;
font.glyphs[i].offsetY = ((rresFontGlyphInfo *)multi.chunks[0].data.raw)[i].offsetY;
font.glyphs[i].advanceX = ((rresFontGlyphInfo *)multi.chunks[0].data.raw)[i].advanceX;
// NOTE: font.glyphs[i].image is not loaded
}
}
else RRES_LOG("RRES: %s: WARNING: Data must be decompressed/decrypted\n", multi.chunks[0].info.type);
}
// Load font image chunk
if (rresGetDataType(multi.chunks[1].info.type) == RRES_DATA_IMAGE)
{
if ((multi.chunks[0].info.compType == RRES_COMP_NONE) && (multi.chunks[0].info.cipherType == RRES_CIPHER_NONE))
{
Image image = LoadImageFromResourceChunk(multi.chunks[1]);
font.texture = LoadTextureFromImage(image);
UnloadImage(image);
}
else RRES_LOG("RRES: %s: WARNING: Data must be decompressed/decrypted\n", multi.chunks[1].info.type);
}
}
else // One chunk of data: RRES_DATA_RAW or RRES_DATA_LINK?
{
if (rresGetDataType(multi.chunks[0].info.type) == RRES_DATA_RAW) // Raw font file
{
unsigned int dataSize = 0;
unsigned char *rawData = (unsigned char *)LoadDataFromResourceChunk(multi.chunks[0], &dataSize);
font = LoadFontFromMemory(GetExtensionFromProps(multi.chunks[0].data.props[1], multi.chunks[0].data.props[2]), rawData, dataSize, 32, NULL, 0);
RL_FREE(rawData);
}
if (rresGetDataType(multi.chunks[0].info.type) == RRES_DATA_LINK) // Link to external font file
{
// Get raw data from external linked file
unsigned int dataSize = 0;
void *rawData = LoadDataFromResourceLink(multi.chunks[0], &dataSize);
// Load image from linked file data
// NOTE 1: Loading font at 32px base size and default charset (95 glyphs)
// NOTE 2: Function checks internally if the file extension is supported to
// properly load the data, if it fails it logs the result and font.texture.id = 0
font = LoadFontFromMemory(GetFileExtension((char *)multi.chunks[0].data.raw), (unsigned char *)rawData, dataSize, 32, NULL, 0);
RRES_FREE(rawData);
}
}
return font;
}
// Load Mesh data from rres resource
// NOTE: We try to load vertex data following raylib structure constraints,
// in case data does not fit raylib Mesh structure, it is not loaded
Mesh LoadMeshFromResource(rresResourceMulti multi)
{
Mesh mesh = { 0 };
// TODO: Support externally linked mesh resource?
// Mesh resource consist of (n) chunks:
for (unsigned int i = 0; i < multi.count; i++)
{
if ((multi.chunks[0].info.compType == RRES_COMP_NONE) && (multi.chunks[0].info.cipherType == RRES_CIPHER_NONE))
{
// NOTE: raylib only supports vertex arrays with same vertex count,
// rres.chunks[0] defined vertexCount will be the reference for the following chunks
// The only exception to vertexCount is the mesh.indices array
if (mesh.vertexCount == 0) mesh.vertexCount = multi.chunks[0].data.props[0];
// Verify chunk type and vertex count
if (rresGetDataType(multi.chunks[i].info.type) == RRES_DATA_VERTEX)
{
// In case vertex count do not match we skip that resource chunk
if ((multi.chunks[i].data.props[1] != RRES_VERTEX_ATTRIBUTE_INDEX) && (multi.chunks[i].data.props[0] != mesh.vertexCount)) continue;
// NOTE: We are only loading raylib supported rresVertexFormat and raylib expected components count
switch (multi.chunks[i].data.props[1]) // Check rresVertexAttribute value
{
case RRES_VERTEX_ATTRIBUTE_POSITION:
{
// raylib expects 3 components per vertex and float vertex format
if ((multi.chunks[i].data.props[2] == 3) && (multi.chunks[i].data.props[3] == RRES_VERTEX_FORMAT_FLOAT))
{
mesh.vertices = (float *)RL_CALLOC(mesh.vertexCount*3, sizeof(float));
memcpy(mesh.vertices, multi.chunks[i].data.raw, mesh.vertexCount*3*sizeof(float));
}
else RRES_LOG("RRES: WARNING: MESH: Vertex attribute position not valid, componentCount/vertexFormat do not fit\n");
} break;
case RRES_VERTEX_ATTRIBUTE_TEXCOORD1:
{
// raylib expects 2 components per vertex and float vertex format
if ((multi.chunks[i].data.props[2] == 2) && (multi.chunks[i].data.props[3] == RRES_VERTEX_FORMAT_FLOAT))
{
mesh.texcoords = (float *)RL_CALLOC(mesh.vertexCount*2, sizeof(float));
memcpy(mesh.texcoords, multi.chunks[i].data.raw, mesh.vertexCount*2*sizeof(float));
}
else RRES_LOG("RRES: WARNING: MESH: Vertex attribute texcoord1 not valid, componentCount/vertexFormat do not fit\n");
} break;
case RRES_VERTEX_ATTRIBUTE_TEXCOORD2:
{
// raylib expects 2 components per vertex and float vertex format
if ((multi.chunks[i].data.props[2] == 2) && (multi.chunks[i].data.props[3] == RRES_VERTEX_FORMAT_FLOAT))
{
mesh.texcoords2 = (float *)RL_CALLOC(mesh.vertexCount*2, sizeof(float));
memcpy(mesh.texcoords2, multi.chunks[i].data.raw, mesh.vertexCount*2*sizeof(float));
}
else RRES_LOG("RRES: WARNING: MESH: Vertex attribute texcoord2 not valid, componentCount/vertexFormat do not fit\n");
} break;
case RRES_VERTEX_ATTRIBUTE_TEXCOORD3:
{
RRES_LOG("RRES: WARNING: MESH: Vertex attribute texcoord3 not supported\n");
} break;
case RRES_VERTEX_ATTRIBUTE_TEXCOORD4:
{
RRES_LOG("RRES: WARNING: MESH: Vertex attribute texcoord4 not supported\n");
} break;
case RRES_VERTEX_ATTRIBUTE_NORMAL:
{
// raylib expects 3 components per vertex and float vertex format
if ((multi.chunks[i].data.props[2] == 3) && (multi.chunks[i].data.props[3] == RRES_VERTEX_FORMAT_FLOAT))
{
mesh.normals = (float *)RL_CALLOC(mesh.vertexCount*3, sizeof(float));
memcpy(mesh.normals, multi.chunks[i].data.raw, mesh.vertexCount*3*sizeof(float));
}
else RRES_LOG("RRES: WARNING: MESH: Vertex attribute normal not valid, componentCount/vertexFormat do not fit\n");
} break;
case RRES_VERTEX_ATTRIBUTE_TANGENT:
{
// raylib expects 4 components per vertex and float vertex format
if ((multi.chunks[i].data.props[2] == 4) && (multi.chunks[i].data.props[3] == RRES_VERTEX_FORMAT_FLOAT))
{
mesh.tangents = (float *)RL_CALLOC(mesh.vertexCount*4, sizeof(float));
memcpy(mesh.tangents, multi.chunks[i].data.raw, mesh.vertexCount*4*sizeof(float));
}
else RRES_LOG("RRES: WARNING: MESH: Vertex attribute tangent not valid, componentCount/vertexFormat do not fit\n");
} break;
case RRES_VERTEX_ATTRIBUTE_COLOR:
{
// raylib expects 4 components per vertex and unsigned char vertex format
if ((multi.chunks[i].data.props[2] == 4) && (multi.chunks[i].data.props[3] == RRES_VERTEX_FORMAT_UBYTE))
{
mesh.colors = (unsigned char *)RL_CALLOC(mesh.vertexCount*4, sizeof(unsigned char));
memcpy(mesh.colors, multi.chunks[i].data.raw, mesh.vertexCount*4*sizeof(unsigned char));
}
else RRES_LOG("RRES: WARNING: MESH: Vertex attribute color not valid, componentCount/vertexFormat do not fit\n");
} break;
case RRES_VERTEX_ATTRIBUTE_INDEX:
{
// raylib expects 1 components per index and unsigned short vertex format
if ((multi.chunks[i].data.props[2] == 1) && (multi.chunks[i].data.props[3] == RRES_VERTEX_FORMAT_USHORT))
{
mesh.indices = (unsigned short *)RL_CALLOC(multi.chunks[i].data.props[0], sizeof(unsigned short));
memcpy(mesh.indices, multi.chunks[i].data.raw, multi.chunks[i].data.props[0]*sizeof(unsigned short));
}
else RRES_LOG("RRES: WARNING: MESH: Vertex attribute index not valid, componentCount/vertexFormat do not fit\n");
} break;
default: break;
}
}
}
else RRES_LOG("RRES: WARNING: Vertex provided data must be decompressed/decrypted\n");
}
return mesh;
}
// Unpack compressed/encrypted data from resource chunk
// In case data could not be processed by rres.h, it is just copied in chunk.data.raw for processing here
// NOTE 1: Function return 0 on success or an error code on failure
// NOTE 2: Data corruption CRC32 check has already been performed by rresLoadResourceMulti() on rres.h
int UnpackResourceChunk(rresResourceChunk *chunk)
{
int result = 0;
bool updateProps = false;
// Result error codes:
// 0 - No error, decompression/decryption successful
// 1 - Encryption algorithm not supported
// 2 - Invalid password on decryption
// 3 - Compression algorithm not supported
// 4 - Error on data decompression
// NOTE 1: If data is compressed/encrypted the properties are not loaded by rres.h because
// it's up to the user to process the data; *chunk must be properly updated by this function
// NOTE 2: rres-raylib should support the same algorithms and libraries used by rrespacker tool
void *unpackedData = NULL;
// STEP 1. Data decryption
//-------------------------------------------------------------------------------------
unsigned char *decryptedData = NULL;
switch (chunk->info.cipherType)
{
case RRES_CIPHER_NONE: decryptedData = (unsigned char *)chunk->data.raw; break;
#if defined(RRES_SUPPORT_ENCRYPTION_AES)
case RRES_CIPHER_AES:
{
// WARNING: Implementation dependant!
// rrespacker tool appends (salt[16] + MD5[16]) to encrypted data for convenience,
// Actually, chunk->info.packedSize considers those additional elements
// Get some memory for the possible message output
decryptedData = (unsigned char *)RL_CALLOC(chunk->info.packedSize - 16 - 16, 1);
if (decryptedData != NULL) memcpy(decryptedData, chunk->data.raw, chunk->info.packedSize - 16 - 16);
// Required variables for key stretching
uint8_t key[32] = { 0 }; // Encryption key
uint8_t salt[16] = { 0 }; // Key stretching salt
// Retrieve salt from chunk packed data
// salt is stored at the end of packed data, before nonce and MAC: salt[16] + MD5[16]
memcpy(salt, ((unsigned char *)chunk->data.raw) + (chunk->info.packedSize - 16 - 16), 16);
// Key stretching configuration
crypto_argon2_config config = {
.algorithm = CRYPTO_ARGON2_I, // Algorithm: Argon2i
.nb_blocks = 16384, // Blocks: 16 MB
.nb_passes = 3, // Iterations
.nb_lanes = 1 // Single-threaded
};
crypto_argon2_inputs inputs = {
.pass = (const uint8_t *)rresGetCipherPassword(), // User password
.pass_size = strlen(rresGetCipherPassword()), // Password length
.salt = salt, // Salt for the password
.salt_size = 16
};
crypto_argon2_extras extras = { 0 }; // Extra parameters unused
void *workArea = RL_MALLOC(config.nb_blocks*1024); // Key stretching work area
// Generate strong encryption key, generated from user password using Argon2i algorithm (256 bit)
crypto_argon2(key, 32, workArea, config, inputs, extras);
// Wipe key generation secrets, they are no longer needed
crypto_wipe(salt, 16);
RL_FREE(workArea);
// Required variables for decryption and message authentication
unsigned int md5[4] = { 0 }; // Message Authentication Code generated on encryption
// Retrieve MD5 from chunk packed data
// NOTE: MD5 is stored at the end of packed data, after salt: salt[16] + MD5[16]
memcpy(md5, ((unsigned char *)chunk->data.raw) + (chunk->info.packedSize - 16), 4*sizeof(unsigned int));
// Message decryption, requires key
struct AES_ctx ctx = { 0 };
AES_init_ctx(&ctx, key);
AES_CTR_xcrypt_buffer(&ctx, (uint8_t *)decryptedData, chunk->info.packedSize - 16 - 16); // AES Counter mode, stream cipher
// Verify MD5 to check if data decryption worked
unsigned int decryptMD5[4] = { 0 };
unsigned int *md5Ptr = ComputeMD5(decryptedData, chunk->info.packedSize - 16 - 16);
for (int i = 0; i < 4; i++) decryptMD5[i] = md5Ptr[i];
// Wipe secrets if they are no longer needed
crypto_wipe(key, 32);
if (memcmp(decryptMD5, md5, 4*sizeof(unsigned int)) == 0) // Decrypted successfully!
{
chunk->info.packedSize -= (16 + 16); // We remove additional data size from packed size (salt[16] + MD5[16])
RRES_LOG("RRES: %c%c%c%c: Data decrypted successfully (AES)\n", chunk->info.type[0], chunk->info.type[1], chunk->info.type[2], chunk->info.type[3]);
}
else
{
result = 2; // Data was not decrypted as expected, wrong password or message corrupted
RRES_LOG("RRES: WARNING: %c%c%c%c: Data decryption failed, wrong password or corrupted data\n", chunk->info.type[0], chunk->info.type[1], chunk->info.type[2], chunk->info.type[3]);
}
} break;
#endif
#if defined(RRES_SUPPORT_ENCRYPTION_XCHACHA20)
case RRES_CIPHER_XCHACHA20_POLY1305:
{
// WARNING: Implementation dependant!
// rrespacker tool appends (salt[16] + nonce[24] + MAC[16]) to encrypted data for convenience,
// Actually, chunk->info.packedSize considers those additional elements
// Get some memory for the possible message output
decryptedData = (unsigned char *)RL_CALLOC(chunk->info.packedSize - 16 - 24 - 16, 1);
// Required variables for key stretching
uint8_t key[32] = { 0 }; // Encryption key
uint8_t salt[16] = { 0 }; // Key stretching salt
// Retrieve salt from chunk packed data
// salt is stored at the end of packed data, before nonce and MAC: salt[16] + nonce[24] + MAC[16]
memcpy(salt, ((unsigned char *)chunk->data.raw) + (chunk->info.packedSize - 16 - 24 - 16), 16);
// Key stretching configuration
crypto_argon2_config config = {
.algorithm = CRYPTO_ARGON2_I, // Algorithm: Argon2i
.nb_blocks = 16384, // Blocks: 16 MB
.nb_passes = 3, // Iterations
.nb_lanes = 1 // Single-threaded
};
crypto_argon2_inputs inputs = {
.pass = (const uint8_t *)rresGetCipherPassword(), // User password
.pass_size = strlen(rresGetCipherPassword()), // Password length
.salt = salt, // Salt for the password
.salt_size = 16
};
crypto_argon2_extras extras = { 0 }; // Extra parameters unused
void *workArea = RL_MALLOC(config.nb_blocks*1024); // Key stretching work area
// Generate strong encryption key, generated from user password using Argon2i algorithm (256 bit)
crypto_argon2(key, 32, workArea, config, inputs, extras);
// Wipe key generation secrets, they are no longer needed
crypto_wipe(salt, 16);
RL_FREE(workArea);
// Required variables for decryption and message authentication
uint8_t nonce[24] = { 0 }; // nonce used on encryption, unique to processed file
uint8_t mac[16] = { 0 }; // Message Authentication Code generated on encryption
// Retrieve nonce and MAC from chunk packed data
// nonce and MAC are stored at the end of packed data, after salt: salt[16] + nonce[24] + MAC[16]
memcpy(nonce, ((unsigned char *)chunk->data.raw) + (chunk->info.packedSize - 16 - 24), 24);
memcpy(mac, ((unsigned char *)chunk->data.raw) + (chunk->info.packedSize - 16), 16);
// Message decryption requires key, nonce and MAC
int decryptResult = crypto_aead_unlock(decryptedData, mac, key, nonce, NULL, 0, chunk->data.raw, (chunk->info.packedSize - 16 - 24 - 16));
// Wipe secrets if they are no longer needed
crypto_wipe(nonce, 24);
crypto_wipe(key, 32);
if (decryptResult == 0) // Decrypted successfully!
{
chunk->info.packedSize -= (16 + 24 + 16); // We remove additional data size from packed size
RRES_LOG("RRES: %c%c%c%c: Data decrypted successfully (XChaCha20)\n", chunk->info.type[0], chunk->info.type[1], chunk->info.type[2], chunk->info.type[3]);
}
else if (decryptResult == -1)
{
result = 2; // Wrong password or message corrupted
RRES_LOG("RRES: WARNING: %c%c%c%c: Data decryption failed, wrong password or corrupted data\n", chunk->info.type[0], chunk->info.type[1], chunk->info.type[2], chunk->info.type[3]);
}
} break;
#endif
default:
{
result = 1; // Decryption algorithm not supported
RRES_LOG("RRES: WARNING: %c%c%c%c: Chunk data encryption algorithm not supported\n", chunk->info.type[0], chunk->info.type[1], chunk->info.type[2], chunk->info.type[3]);
} break;
}
if ((result == 0) && (chunk->info.cipherType != RRES_CIPHER_NONE))
{
// Data is not encrypted any more, register it
chunk->info.cipherType = RRES_CIPHER_NONE;
updateProps = true;
}
// STEP 2: Data decompression (if decryption was successful)
//-------------------------------------------------------------------------------------
unsigned char *uncompData = NULL;
if (result == 0)
{
switch (chunk->info.compType)
{
case RRES_COMP_NONE: unpackedData = decryptedData; break;
case RRES_COMP_DEFLATE:
{
int uncompDataSize = 0;
// TODO: WARNING: Possible issue with allocators: RL_CALLOC() vs RRES_CALLOC()
uncompData = DecompressData(decryptedData, chunk->info.packedSize, &uncompDataSize);
if ((uncompData != NULL) && (uncompDataSize > 0)) // Decompression successful
{
unpackedData = uncompData;
chunk->info.packedSize = uncompDataSize;
RRES_LOG("RRES: %c%c%c%c: Data decompressed successfully (DEFLATE)\n", chunk->info.type[0], chunk->info.type[1], chunk->info.type[2], chunk->info.type[3]);
}
else
{
result = 4; // Decompression process failed
RRES_LOG("RRES: WARNING: %c%c%c%c: Chunk data decompression failed\n", chunk->info.type[0], chunk->info.type[1], chunk->info.type[2], chunk->info.type[3]);
}
// Security check, uncompDataSize must match the provided chunk->baseSize
if (uncompDataSize != chunk->info.baseSize) RRES_LOG("RRES: WARNING: Decompressed data could be corrupted, unexpected size\n");
} break;
#if defined(RRES_SUPPORT_COMPRESSION_LZ4)
case RRES_COMP_LZ4:
{
int uncompDataSize = 0;
uncompData = (unsigned char *)RRES_CALLOC(chunk->info.baseSize, 1);
uncompDataSize = LZ4_decompress_safe((char *)decryptedData, (char *)uncompData, chunk->info.packedSize, chunk->info.baseSize);
if ((uncompData != NULL) && (uncompDataSize > 0)) // Decompression successful
{
unpackedData = uncompData;
chunk->info.packedSize = uncompDataSize;
RRES_LOG("RRES: %c%c%c%c: Data decompressed successfully (LZ4)\n", chunk->info.type[0], chunk->info.type[1], chunk->info.type[2], chunk->info.type[3]);
}
else
{
result = 4; // Decompression process failed
RRES_LOG("RRES: WARNING: %c%c%c%c: Chunk data decompression failed\n", chunk->info.type[0], chunk->info.type[1], chunk->info.type[2], chunk->info.type[3]);
}
// WARNING: Decompression could be successful but not the original message size returned
if (uncompDataSize != chunk->info.baseSize) RRES_LOG("RRES: WARNING: Decompressed data could be corrupted, unexpected size\n");
} break;
#endif
case RRES_COMP_QOI:
{
int uncompDataSize = 0;
qoi_desc desc = { 0 };
// TODO: WARNING: Possible issue with allocators: QOI_MALLOC() vs RRES_MALLOC()
uncompData = (unsigned char *)qoi_decode(decryptedData, chunk->info.packedSize, &desc, 0);
uncompDataSize = (desc.width*desc.height*desc.channels) + 20; // Add the 20 bytes of (propCount + props[4])
if ((uncompData != NULL) && (uncompDataSize > 0)) // Decompression successful
{
unpackedData = uncompData;
chunk->info.packedSize = uncompDataSize;
RRES_LOG("RRES: %c%c%c%c: Data decompressed successfully (QOI)\n", chunk->info.type[0], chunk->info.type[1], chunk->info.type[2], chunk->info.type[3]);
}
else
{
result = 4; // Decompression process failed
RRES_LOG("RRES: WARNING: %c%c%c%c: Chunk data decompression failed\n", chunk->info.type[0], chunk->info.type[1], chunk->info.type[2], chunk->info.type[3]);
}
if (uncompDataSize != chunk->info.baseSize) RRES_LOG("RRES: WARNING: Decompressed data could be corrupted, unexpected size\n");
} break;
default:
{
result = 3;
RRES_LOG("RRES: WARNING: %c%c%c%c: Chunk data compression algorithm not supported\n", chunk->info.type[0], chunk->info.type[1], chunk->info.type[2], chunk->info.type[3]);
} break;
}
}
if ((result == 0) && (chunk->info.compType != RRES_COMP_NONE))
{
// Data is not encrypted any more, register it
chunk->info.compType = RRES_COMP_NONE;
updateProps = true;
}
// Update chunk->data.propCount and chunk->data.props if required
if (updateProps && (unpackedData != NULL))
{
// Data is decompressed/decrypted into chunk->data.raw but data.propCount and data.props[] are still empty,
// they must be filled with the just updated chunk->data.raw (that contains everything)
chunk->data.propCount = ((int *)unpackedData)[0];
if (chunk->data.propCount > 0)
{
chunk->data.props = (unsigned int *)RRES_CALLOC(chunk->data.propCount, sizeof(int));
for (unsigned int i = 0; i < chunk->data.propCount; i++) chunk->data.props[i] = ((int *)unpackedData)[1 + i];
}
// Move chunk->data.raw pointer (chunk->data.propCount*sizeof(int)) positions
void *raw = RRES_CALLOC(chunk->info.baseSize - 20, 1);
if (raw != NULL) memcpy(raw, ((unsigned char *)unpackedData) + 20, chunk->info.baseSize - 20);
RRES_FREE(chunk->data.raw);
chunk->data.raw = raw;
RL_FREE(unpackedData);
}
return result;
}
//----------------------------------------------------------------------------------
// Module specific Functions Definition
//----------------------------------------------------------------------------------
// Load data chunk: RRES_DATA_LINK
static void *LoadDataFromResourceLink(rresResourceChunk chunk, unsigned int *size)
{
char fullFilePath[2048] = { 0 };
void *data = NULL;
*size = 0;
// Get external link filepath
char *linkFilePath = (char *)RL_CALLOC(chunk.data.props[0], 1);
if (linkFilePath != NULL) memcpy(linkFilePath, chunk.data.raw, chunk.data.props[0]);
// Get base directory to append filepath if not provided by user
if (baseDir == NULL) baseDir = GetApplicationDirectory();
strcpy(fullFilePath, baseDir);
strcat(fullFilePath, linkFilePath);
RRES_LOG("RRES: %c%c%c%c: Data file linked externally: %s\n", chunk.info.type[0], chunk.info.type[1], chunk.info.type[2], chunk.info.type[3], linkFilePath);
if (FileExists(fullFilePath))
{
// Load external file as raw data
// NOTE: We check if file is a text file to allow automatic line-endings processing
if (IsFileExtension(linkFilePath, ".txt;.md;.vs;.fs;.info;.c;.h;.json;.xml;.glsl")) // Text file
{
data = LoadFileText(fullFilePath);
*size = TextLength((char *)data);
}
else data = LoadFileData(fullFilePath, (int *)size);
if ((data != NULL) && (*size > 0)) RRES_LOG("RRES: %c%c%c%c: External linked file loaded successfully\n", chunk.info.type[0], chunk.info.type[1], chunk.info.type[2], chunk.info.type[3]);
}
else RRES_LOG("RRES: WARNING: [%s] Linked external file could not be found\n", linkFilePath);
return data;
}
// Load data chunk: RRES_DATA_RAW
// NOTE: This chunk can be used raw files embedding or other binary blobs
static void *LoadDataFromResourceChunk(rresResourceChunk chunk, unsigned int *size)
{
void *rawData = NULL;
if ((chunk.info.compType == RRES_COMP_NONE) && (chunk.info.cipherType == RRES_CIPHER_NONE))
{
rawData = RL_CALLOC(chunk.data.props[0], 1);
if (rawData != NULL) memcpy(rawData, chunk.data.raw, chunk.data.props[0]);
*size = chunk.data.props[0];
}
else RRES_LOG("RRES: %c%c%c%c: WARNING: Data must be decompressed/decrypted\n", chunk.info.type[0], chunk.info.type[1], chunk.info.type[2], chunk.info.type[3]);
return rawData;
}
// Load data chunk: RRES_DATA_TEXT
// NOTE: This chunk can be used for shaders or other text data elements (materials?)
static char *LoadTextFromResourceChunk(rresResourceChunk chunk, unsigned int *codeLang)
{
char *text = NULL;
if ((chunk.info.compType == RRES_COMP_NONE) && (chunk.info.cipherType == RRES_CIPHER_NONE))
{
text = (char *)RL_CALLOC(chunk.data.props[0] + 1, 1); // We add NULL terminator, just in case
if (text != NULL) memcpy(text, chunk.data.raw, chunk.data.props[0]);
// TODO: We got some extra text properties, in case they could be useful for users:
// chunk.props[1]:rresTextEncoding, chunk.props[2]:rresCodeLang, chunk. props[3]:cultureCode
*codeLang = chunk.data.props[2];
//chunks.props[3]:cultureCode could be useful for localized text
}
else RRES_LOG("RRES: %c%c%c%c: WARNING: Data must be decompressed/decrypted\n", chunk.info.type[0], chunk.info.type[1], chunk.info.type[2], chunk.info.type[3]);
return text;
}
// Load data chunk: RRES_DATA_IMAGE
// NOTE: Many data types use images data in some way (font, material...)
static Image LoadImageFromResourceChunk(rresResourceChunk chunk)
{
Image image = { 0 };
if ((chunk.info.compType == RRES_COMP_NONE) && (chunk.info.cipherType == RRES_CIPHER_NONE))
{
image.width = chunk.data.props[0];
image.height = chunk.data.props[1];
int format = chunk.data.props[2];
// Assign equivalent pixel formats for our engine
// NOTE: In this case rresPixelFormat defined values match raylib PixelFormat values
switch (format)
{
case RRES_PIXELFORMAT_UNCOMP_GRAYSCALE: image.format = PIXELFORMAT_UNCOMPRESSED_GRAYSCALE; break;
case RRES_PIXELFORMAT_UNCOMP_GRAY_ALPHA: image.format = PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA; break;
case RRES_PIXELFORMAT_UNCOMP_R5G6B5: image.format = PIXELFORMAT_UNCOMPRESSED_R5G6B5; break;
case RRES_PIXELFORMAT_UNCOMP_R8G8B8: image.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8; break;
case RRES_PIXELFORMAT_UNCOMP_R5G5B5A1: image.format = PIXELFORMAT_UNCOMPRESSED_R5G5B5A1; break;
case RRES_PIXELFORMAT_UNCOMP_R4G4B4A4: image.format = PIXELFORMAT_UNCOMPRESSED_R4G4B4A4; break;
case RRES_PIXELFORMAT_UNCOMP_R8G8B8A8: image.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8; break;
case RRES_PIXELFORMAT_UNCOMP_R32: image.format = PIXELFORMAT_UNCOMPRESSED_R32; break;
case RRES_PIXELFORMAT_UNCOMP_R32G32B32: image.format = PIXELFORMAT_UNCOMPRESSED_R32G32B32; break;
case RRES_PIXELFORMAT_UNCOMP_R32G32B32A32: image.format = PIXELFORMAT_UNCOMPRESSED_R32G32B32A32; break;
case RRES_PIXELFORMAT_COMP_DXT1_RGB: image.format = PIXELFORMAT_COMPRESSED_DXT1_RGB; break;
case RRES_PIXELFORMAT_COMP_DXT1_RGBA: image.format = PIXELFORMAT_COMPRESSED_DXT1_RGBA; break;
case RRES_PIXELFORMAT_COMP_DXT3_RGBA: image.format = PIXELFORMAT_COMPRESSED_DXT3_RGBA; break;
case RRES_PIXELFORMAT_COMP_DXT5_RGBA: image.format = PIXELFORMAT_COMPRESSED_DXT5_RGBA; break;
case RRES_PIXELFORMAT_COMP_ETC1_RGB: image.format = PIXELFORMAT_COMPRESSED_ETC1_RGB; break;
case RRES_PIXELFORMAT_COMP_ETC2_RGB: image.format = PIXELFORMAT_COMPRESSED_ETC2_RGB; break;
case RRES_PIXELFORMAT_COMP_ETC2_EAC_RGBA: image.format = PIXELFORMAT_COMPRESSED_ETC2_EAC_RGBA; break;
case RRES_PIXELFORMAT_COMP_PVRT_RGB: image.format = PIXELFORMAT_COMPRESSED_PVRT_RGB; break;
case RRES_PIXELFORMAT_COMP_PVRT_RGBA: image.format = PIXELFORMAT_COMPRESSED_PVRT_RGBA; break;
case RRES_PIXELFORMAT_COMP_ASTC_4x4_RGBA: image.format = PIXELFORMAT_COMPRESSED_ASTC_4x4_RGBA; break;
case RRES_PIXELFORMAT_COMP_ASTC_8x8_RGBA: image.format = PIXELFORMAT_COMPRESSED_ASTC_8x8_RGBA; break;
default: break;
}
image.mipmaps = chunk.data.props[3];
// Image data size can be computed from image properties
unsigned int size = GetPixelDataSize(image.width, image.height, image.format);
// NOTE: Computed image data must match the data size of the chunk processed (minus propCount + props[4] size)
if (size == (chunk.info.baseSize - 20))
{
image.data = RL_CALLOC(size, 1);
if (image.data != NULL) memcpy(image.data, chunk.data.raw, size);
}
else RRES_LOG("RRES: WARNING: IMGE: Chunk data size do not match expected image data size\n");
}
else RRES_LOG("RRES: %c%c%c%c: WARNING: Data must be decompressed/decrypted\n", chunk.info.type[0], chunk.info.type[1], chunk.info.type[2], chunk.info.type[3]);
return image;
}
// Get file extension from RRES_DATA_RAW properties (unsigned int)
static const char *GetExtensionFromProps(unsigned int ext01, unsigned int ext02)
{
static char extension[8] = { 0 };
memset(extension, 0, 8);
// Convert file extension provided as 2 unsigned int properties, to a char[] array
// NOTE: Extension is defined as 2 unsigned int big-endian values (4 bytes each),
// starting with a dot, i.e 0x2e706e67 => ".png"
extension[0] = (unsigned char)((ext01 & 0xff000000) >> 24);
extension[1] = (unsigned char)((ext01 & 0x00ff0000) >> 16);
extension[2] = (unsigned char)((ext01 & 0x0000ff00) >> 8);
extension[3] = (unsigned char)(ext01 & 0x000000ff);
extension[4] = (unsigned char)((ext02 & 0xff000000) >> 24);
extension[5] = (unsigned char)((ext02 & 0x00ff0000) >> 16);
extension[6] = (unsigned char)((ext02 & 0x0000ff00) >> 8);
extension[7] = (unsigned char)(ext02 & 0x000000ff);
return extension;
}
// Compute MD5 hash code, returns 4 integers array (static)
static unsigned int *ComputeMD5(const unsigned char *data, int size)
{
#define LEFTROTATE(x, c) (((x) << (c)) | ((x) >> (32 - (c))))
static unsigned int hash[4] = { 0 };
// NOTE: All variables are unsigned 32 bit and wrap modulo 2^32 when calculating
// r specifies the per-round shift amounts
unsigned int r[] = {
7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21
};
// Use binary integer part of the sines of integers (in radians) as constants// Initialize variables:
unsigned int k[] = {
0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,