-
Notifications
You must be signed in to change notification settings - Fork 0
/
hap.c
979 lines (832 loc) · 31.8 KB
/
hap.c
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
#include "hap.h"
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "snappy-c.h"
#define kHapUInt24Max 0x00FFFFFF
#define kHapCompressorNone 0xA
#define kHapCompressorSnappy 0xB
#define kHapCompressorComplex 0xC
#define kHapFormatRGBDXT1 0xB
#define kHapFormatRGBADXT5 0xE
#define kHapFormatYCoCgDXT5 0xF
#define kHapFormatARGTC1 0x1
/*
Format Compressor Byte Code
----------------------------------------
RGB_DXT1 None 0xAB
RGB_DXT1 Snappy 0xBB
RGB_DXT1 Complex 0xCB
RGBA_DXT5 None 0xAE
RGBA_DXT5 Snappy 0xBE
RGBA_DXT5 Complex 0xCE
YCoCg_DXT5 None 0xAF
YCoCg_DXT5 Snappy 0xBF
YCoCg_DXT5 Complex 0xCF
A_RGTC1 None 0xA1
A_RGTC1 Snappy 0xB1
A_RGTC1 Complex 0xC1
*/
/*
Frame Section Types
*/
#define kHapSectionMultipleImages 0x0D
#define kHapSectionDecodeInstructionsContainer 0x01
#define kHapSectionChunkSecondStageCompressorTable 0x02
#define kHapSectionChunkSizeTable 0x03
#define kHapSectionChunkOffsetTable 0x04
typedef struct HapChunkDecodeInfo {
unsigned int result;
unsigned int compressor;
const char *compressed_chunk_data;
size_t compressed_chunk_size;
char *uncompressed_chunk_data;
size_t uncompressed_chunk_size;
} HapChunkDecodeInfo;
static unsigned int hap_read_3_byte_uint(const void *buffer)
{
return (*(uint8_t *)buffer) + ((*(((uint8_t *)buffer) + 1)) << 8) + ((*(((uint8_t *)buffer) + 2)) << 16);
}
static void hap_write_3_byte_uint(void *buffer, unsigned int value)
{
*(uint8_t *)buffer = value & 0xFF;
*(((uint8_t *)buffer) + 1) = (value >> 8) & 0xFF;
*(((uint8_t *)buffer) + 2) = (value >> 16) & 0xFF;
}
static unsigned int hap_read_4_byte_uint(const void *buffer)
{
return (*(uint8_t *)buffer) + ((*(((uint8_t *)buffer) + 1)) << 8) + ((*(((uint8_t *)buffer) + 2)) << 16) + ((*(((uint8_t *)buffer) + 3)) << 24);
}
static void hap_write_4_byte_uint(const void *buffer, unsigned int value)
{
*(uint8_t *)buffer = value & 0xFF;
*(((uint8_t *)buffer) + 1) = (value >> 8) & 0xFF;
*(((uint8_t *)buffer) + 2) = (value >> 16) & 0xFF;
*(((uint8_t *)buffer) + 3) = (value >> 24) & 0xFF;
}
#define hap_top_4_bits(x) (((x) & 0xF0) >> 4)
#define hap_bottom_4_bits(x) ((x) & 0x0F)
#define hap_4_bit_packed_byte(top_bits, bottom_bits) (((top_bits) << 4) | ((bottom_bits) & 0x0F))
static int hap_read_section_header(const void *buffer, uint32_t buffer_length, uint32_t *out_header_length, uint32_t *out_section_length, unsigned int *out_section_type)
{
if (buffer_length < 4U)
{
return HapResult_Bad_Frame;
}
*out_section_length = hap_read_3_byte_uint(buffer);
if (*out_section_length == 0U)
{
if (buffer_length < 8U)
{
return HapResult_Bad_Frame;
}
*out_section_length = hap_read_4_byte_uint(((uint8_t *)buffer) + 4U);
*out_header_length = 8U;
}
else
{
*out_header_length = 4U;
}
*out_section_type = *(((uint8_t *)buffer) + 3U);
if (*out_header_length + *out_section_length > buffer_length)
{
return HapResult_Bad_Frame;
}
return HapResult_No_Error;
}
static void hap_write_section_header(void *buffer, size_t header_length, uint32_t section_length, unsigned int section_type)
{
if (header_length == 4U)
{
hap_write_3_byte_uint(buffer, (unsigned int)section_length);
}
else
{
hap_write_3_byte_uint(buffer, 0U);
hap_write_4_byte_uint(((uint8_t *)buffer) + 4U, section_length);
}
*(((uint8_t *)buffer) + 3) = section_type;
}
static unsigned int hap_texture_format_constant_for_format_identifier(unsigned int identifier)
{
switch (identifier)
{
case kHapFormatRGBDXT1:
return HapTextureFormat_RGB_DXT1;
case kHapFormatRGBADXT5:
return HapTextureFormat_RGBA_DXT5;
case kHapFormatYCoCgDXT5:
return HapTextureFormat_YCoCg_DXT5;
case kHapFormatARGTC1:
return HapTextureFormat_A_RGTC1;
default:
return 0;
}
}
static unsigned int hap_texture_format_identifier_for_format_constant(unsigned int constant)
{
switch (constant)
{
case HapTextureFormat_RGB_DXT1:
return kHapFormatRGBDXT1;
case HapTextureFormat_RGBA_DXT5:
return kHapFormatRGBADXT5;
case HapTextureFormat_YCoCg_DXT5:
return kHapFormatYCoCgDXT5;
case HapTextureFormat_A_RGTC1:
return kHapFormatARGTC1;
default:
return 0;
}
}
static size_t hap_decode_instructions_length(unsigned int chunk_count)
{
/*
= chunk_count + (4 * chunk_count) + 4 + 4
*/
size_t length = (5 * chunk_count) + 8;
return length;
}
static unsigned int hap_limited_chunk_count_for_frame(size_t input_bytes, unsigned int texture_format, unsigned int chunk_count)
{
// (0xFFFFFF == count + (4 x count) + 20)
if (chunk_count > 3355431)
{
chunk_count = 3355431;
}
// Divide frame equally on DXT block boundries (8 or 16 bytes)
unsigned long dxt_block_count;
switch (texture_format) {
case HapTextureFormat_RGB_DXT1:
case HapTextureFormat_A_RGTC1:
dxt_block_count = input_bytes / 8;
break;
default:
dxt_block_count = input_bytes / 16;
}
while (dxt_block_count % chunk_count != 0) {
chunk_count--;
}
return chunk_count;
}
static size_t hap_max_encoded_length(size_t input_bytes, unsigned int texture_format, unsigned int compressor, unsigned int chunk_count)
{
size_t decode_instructions_length, max_compressed_length;
chunk_count = hap_limited_chunk_count_for_frame(input_bytes, texture_format, chunk_count);
decode_instructions_length = hap_decode_instructions_length(chunk_count);
if (compressor == HapCompressorSnappy)
{
size_t chunk_size = input_bytes / chunk_count;
max_compressed_length = snappy_max_compressed_length(chunk_size) * chunk_count;
}
else
{
max_compressed_length = input_bytes;
}
return max_compressed_length + 8U + decode_instructions_length + 4U;
}
unsigned long HapMaxEncodedLength(unsigned int count,
unsigned long *inputBytes,
unsigned int *textureFormats,
unsigned int *chunkCounts)
{
unsigned long total_length = 8;
// Return 0 for bad arguments
if (count == 0 || count > 2
|| inputBytes == NULL
|| textureFormats == NULL
|| chunkCounts == NULL)
{
return 0;
}
for (int i = 0; i < count; i++)
{
if (chunkCounts[i] == 0)
{
return 0;
}
total_length += hap_max_encoded_length(inputBytes[i], textureFormats[i], HapCompressorSnappy, chunkCounts[i]);
}
return total_length;
}
static unsigned int hap_encode_texture(const void *inputBuffer, unsigned long inputBufferBytes, unsigned int textureFormat,
unsigned int compressor, unsigned int chunkCount, void *outputBuffer,
unsigned long outputBufferBytes, unsigned long *outputBufferBytesUsed)
{
size_t top_section_header_length;
size_t top_section_length;
unsigned int storedCompressor;
unsigned int storedFormat;
if (inputBuffer == NULL
|| inputBufferBytes == 0
|| (textureFormat != HapTextureFormat_RGB_DXT1
&& textureFormat != HapTextureFormat_RGBA_DXT5
&& textureFormat != HapTextureFormat_YCoCg_DXT5
&& textureFormat != HapTextureFormat_A_RGTC1
)
|| (compressor != HapCompressorNone
&& compressor != HapCompressorSnappy
)
|| outputBuffer == NULL
|| outputBufferBytesUsed == NULL
)
{
return HapResult_Bad_Arguments;
}
else if (outputBufferBytes < hap_max_encoded_length(inputBufferBytes, textureFormat, compressor, chunkCount))
{
return HapResult_Buffer_Too_Small;
}
if (inputBufferBytes > kHapUInt24Max)
{
top_section_header_length = 8U;
}
else
{
top_section_header_length = 4U;
}
if (compressor == HapCompressorSnappy)
{
size_t decode_instructions_length;
size_t chunk_size, compress_buffer_remaining;
uint8_t *second_stage_compressor_table;
void *chunk_size_table;
char *compressed_data;
unsigned int i;
chunkCount = hap_limited_chunk_count_for_frame(inputBufferBytes, textureFormat, chunkCount);
decode_instructions_length = hap_decode_instructions_length(chunkCount);
if ((inputBufferBytes + decode_instructions_length + 4) > kHapUInt24Max)
{
top_section_header_length = 8U;
}
second_stage_compressor_table = ((uint8_t *)outputBuffer) + top_section_header_length + 4 + 4;
chunk_size_table = ((uint8_t *)outputBuffer) + top_section_header_length + 4 + 4 + chunkCount + 4;
chunk_size = inputBufferBytes / chunkCount;
hap_write_section_header(((uint8_t *)outputBuffer) + top_section_header_length, 4U, decode_instructions_length, kHapSectionDecodeInstructionsContainer);
hap_write_section_header(((uint8_t *)outputBuffer) + top_section_header_length + 4U, 4U, chunkCount, kHapSectionChunkSecondStageCompressorTable);
hap_write_section_header(((uint8_t *)outputBuffer) + top_section_header_length + 4U + 4U + chunkCount, 4U, chunkCount * 4U, kHapSectionChunkSizeTable);
compressed_data = (char *)(((uint8_t *)outputBuffer) + top_section_header_length + 4 + decode_instructions_length);
compress_buffer_remaining = outputBufferBytes - top_section_header_length - 4 - decode_instructions_length;
top_section_length = 4 + decode_instructions_length;
for (i = 0; i < chunkCount; i++) {
size_t chunk_packed_length = compress_buffer_remaining;
const char *chunk_input_start = (const char *)(((uint8_t *)inputBuffer) + (chunk_size * i));
if (compressor == HapCompressorSnappy)
{
snappy_status result = snappy_compress(chunk_input_start, chunk_size, (char *)compressed_data, &chunk_packed_length);
if (result != SNAPPY_OK)
{
return HapResult_Internal_Error;
}
}
if (compressor == HapCompressorNone || chunk_packed_length >= chunk_size)
{
memcpy(compressed_data, chunk_input_start, chunk_size);
chunk_packed_length = chunk_size;
second_stage_compressor_table[i] = kHapCompressorNone;
}
else
{
second_stage_compressor_table[i] = kHapCompressorSnappy;
}
hap_write_4_byte_uint(((uint8_t *)chunk_size_table) + (i * 4), chunk_packed_length);
compressed_data += chunk_packed_length;
top_section_length += chunk_packed_length;
compress_buffer_remaining -= chunk_packed_length;
}
if (top_section_length < inputBufferBytes + top_section_header_length)
{
storedCompressor = kHapCompressorComplex;
}
else
{
compressor = HapCompressorNone;
}
}
if (compressor == HapCompressorNone)
{
memcpy(((uint8_t *)outputBuffer) + top_section_header_length, inputBuffer, inputBufferBytes);
top_section_length = inputBufferBytes;
storedCompressor = kHapCompressorNone;
}
storedFormat = hap_texture_format_identifier_for_format_constant(textureFormat);
hap_write_section_header(outputBuffer, top_section_header_length, top_section_length, hap_4_bit_packed_byte(storedCompressor, storedFormat));
*outputBufferBytesUsed = top_section_length + top_section_header_length;
return HapResult_No_Error;
}
unsigned int HapEncode(unsigned int count,
const void **inputBuffers, unsigned long *inputBuffersBytes,
unsigned int *textureFormats,
unsigned int *compressors,
unsigned int *chunkCounts,
void *outputBuffer, unsigned long outputBufferBytes,
unsigned long *outputBufferBytesUsed)
{
size_t top_section_header_length;
size_t top_section_length;
unsigned long section_length;
if (count == 0 || count > 2
|| inputBuffers == NULL
|| inputBuffersBytes == NULL
|| textureFormats == NULL
|| compressors == NULL
|| chunkCounts == NULL
|| outputBuffer == NULL
|| outputBufferBytes == 0
|| outputBufferBytesUsed == NULL)
{
return HapResult_Bad_Arguments;
}
for (int i = 0; i < count; i++)
{
if (chunkCounts[i] == 0)
{
return HapResult_Bad_Arguments;
}
}
if (count == 1)
{
return hap_encode_texture(inputBuffers[0],
inputBuffersBytes[0],
textureFormats[0],
compressors[0],
chunkCounts[0],
outputBuffer,
outputBufferBytes,
outputBufferBytesUsed);
}
else if ((textureFormats[0] != HapTextureFormat_YCoCg_DXT5 && textureFormats[1] != HapTextureFormat_YCoCg_DXT5)
&& (textureFormats[0] != HapTextureFormat_A_RGTC1 && textureFormats[1] != HapTextureFormat_A_RGTC1))
{
return HapResult_Bad_Arguments;
}
else
{
top_section_length = 0;
for (int i = 0; i < count; i++)
{
top_section_length += inputBuffersBytes[i] + hap_decode_instructions_length(chunkCounts[i]) + 4;
}
if (top_section_length > kHapUInt24Max)
{
top_section_header_length = 8U;
}
else
{
top_section_header_length = 4U;
}
top_section_length = 0;
for (int i = 0; i < count; i++)
{
void *section = ((uint8_t *)outputBuffer) + top_section_header_length + top_section_length;
unsigned int result = hap_encode_texture(inputBuffers[i],
inputBuffersBytes[i],
textureFormats[i],
compressors[i],
chunkCounts[i],
section,
outputBufferBytes - (top_section_header_length + top_section_length),
§ion_length);
if (result != HapResult_No_Error)
{
return result;
}
top_section_length += section_length;
}
hap_write_section_header(outputBuffer, top_section_header_length, top_section_length, kHapSectionMultipleImages);
*outputBufferBytesUsed = top_section_length + top_section_header_length;
return HapResult_No_Error;
}
}
static void hap_decode_chunk(HapChunkDecodeInfo chunks[], unsigned int index)
{
if (chunks)
{
if (chunks[index].compressor == kHapCompressorSnappy)
{
snappy_status snappy_result = snappy_uncompress(chunks[index].compressed_chunk_data,
chunks[index].compressed_chunk_size,
chunks[index].uncompressed_chunk_data,
&chunks[index].uncompressed_chunk_size);
switch (snappy_result)
{
case SNAPPY_INVALID_INPUT:
chunks[index].result = HapResult_Bad_Frame;
break;
case SNAPPY_OK:
chunks[index].result = HapResult_No_Error;
break;
default:
chunks[index].result = HapResult_Internal_Error;
break;
}
}
else if (chunks[index].compressor == kHapCompressorNone)
{
memcpy(chunks[index].uncompressed_chunk_data,
chunks[index].compressed_chunk_data,
chunks[index].compressed_chunk_size);
chunks[index].result = HapResult_No_Error;
}
else
{
chunks[index].result = HapResult_Bad_Frame;
}
}
}
static unsigned int hap_decode_header_complex_instructions(const void *texture_section, uint32_t texture_section_length, int * chunk_count,
const void **compressors, const void **chunk_sizes, const void **chunk_offsets, const char **frame_data){
int result = HapResult_No_Error;
const void *section_start;
uint32_t section_header_length;
uint32_t section_length;
unsigned int section_type;
size_t bytes_remaining = 0;
*compressors = NULL;
*chunk_sizes = NULL;
*chunk_offsets = NULL;
result = hap_read_section_header(texture_section, texture_section_length, §ion_header_length, §ion_length, §ion_type);
if (result == HapResult_No_Error && section_type != kHapSectionDecodeInstructionsContainer)
{
result = HapResult_Bad_Frame;
}
if (result != HapResult_No_Error)
{
return result;
}
*frame_data = ((const char *)texture_section) + section_header_length + section_length;
section_start = ((uint8_t *)texture_section) + section_header_length;
bytes_remaining = section_length;
while (bytes_remaining > 0) {
unsigned int section_chunk_count = 0;
result = hap_read_section_header(section_start, bytes_remaining, §ion_header_length, §ion_length, §ion_type);
if (result != HapResult_No_Error)
{
return result;
}
section_start = ((uint8_t *)section_start) + section_header_length;
switch (section_type) {
case kHapSectionChunkSecondStageCompressorTable:
*compressors = section_start;
section_chunk_count = section_length;
break;
case kHapSectionChunkSizeTable:
*chunk_sizes = section_start;
section_chunk_count = section_length / 4;
break;
case kHapSectionChunkOffsetTable:
*chunk_offsets = section_start;
section_chunk_count = section_length / 4;
break;
default:
break;
}
if (section_chunk_count != 0)
{
if ((*chunk_count) != 0 && section_chunk_count != (*chunk_count))
{
return HapResult_Bad_Frame;
}
*chunk_count = section_chunk_count;
}
section_start = ((uint8_t *)section_start) + section_length;
bytes_remaining -= section_header_length + section_length;
}
if (*compressors == NULL || *chunk_sizes == NULL)
{
return HapResult_Bad_Frame;
}
return result;
}
unsigned int hap_decode_single_texture(const void *texture_section, uint32_t texture_section_length,
unsigned int texture_section_type,
HapDecodeCallback callback, void *info,
void *outputBuffer, unsigned long outputBufferBytes,
unsigned long *outputBufferBytesUsed,
unsigned int *outputBufferTextureFormat)
{
int result = HapResult_No_Error;
unsigned int textureFormat;
unsigned int compressor;
size_t bytesUsed = 0;
compressor = hap_top_4_bits(texture_section_type);
textureFormat = hap_bottom_4_bits(texture_section_type);
*outputBufferTextureFormat = hap_texture_format_constant_for_format_identifier(textureFormat);
if (*outputBufferTextureFormat == 0)
{
return HapResult_Bad_Frame;
}
if (compressor == kHapCompressorComplex)
{
int chunk_count = 0;
const void *compressors = NULL;
const void *chunk_sizes = NULL;
const void *chunk_offsets = NULL;
const char *frame_data = NULL;
result = hap_decode_header_complex_instructions(texture_section, texture_section_length, &chunk_count, &compressors, &chunk_sizes, &chunk_offsets, &frame_data);
if (result != HapResult_No_Error)
{
return result;
}
if (chunk_count > 0)
{
HapChunkDecodeInfo *chunk_info = (HapChunkDecodeInfo *)malloc(sizeof(HapChunkDecodeInfo) * chunk_count);
size_t running_compressed_chunk_size = 0;
size_t running_uncompressed_chunk_size = 0;
int i;
if (chunk_info == NULL)
{
return HapResult_Internal_Error;
}
for (i = 0; i < chunk_count; i++) {
chunk_info[i].compressor = *(((uint8_t *)compressors) + i);
chunk_info[i].compressed_chunk_size = hap_read_4_byte_uint(((uint8_t *)chunk_sizes) + (i * 4));
if (chunk_offsets)
{
chunk_info[i].compressed_chunk_data = frame_data + hap_read_4_byte_uint(((uint8_t *)chunk_offsets) + (i * 4));
}
else
{
chunk_info[i].compressed_chunk_data = frame_data + running_compressed_chunk_size;
}
running_compressed_chunk_size += chunk_info[i].compressed_chunk_size;
if (chunk_info[i].compressor == kHapCompressorSnappy)
{
snappy_status snappy_result = snappy_uncompressed_length(chunk_info[i].compressed_chunk_data,
chunk_info[i].compressed_chunk_size,
&(chunk_info[i].uncompressed_chunk_size));
if (snappy_result != SNAPPY_OK)
{
switch (snappy_result)
{
case SNAPPY_INVALID_INPUT:
result = HapResult_Bad_Frame;
break;
default:
result = HapResult_Internal_Error;
break;
}
break;
}
}
else
{
chunk_info[i].uncompressed_chunk_size = chunk_info[i].compressed_chunk_size;
}
chunk_info[i].uncompressed_chunk_data = (char *)(((uint8_t *)outputBuffer) + running_uncompressed_chunk_size);
running_uncompressed_chunk_size += chunk_info[i].uncompressed_chunk_size;
}
if (result == HapResult_No_Error && running_uncompressed_chunk_size > outputBufferBytes)
{
result = HapResult_Buffer_Too_Small;
}
if (result == HapResult_No_Error)
{
bytesUsed = running_uncompressed_chunk_size;
if (chunk_count == 1)
{
hap_decode_chunk(chunk_info, 0);
}
else
{
callback((HapDecodeWorkFunction)hap_decode_chunk, chunk_info, chunk_count, info);
}
for (i = 0; i < chunk_count; i++)
{
if (chunk_info[i].result != HapResult_No_Error)
{
result = chunk_info[i].result;
break;
}
}
}
free(chunk_info);
if (result != HapResult_No_Error)
{
return result;
}
}
}
else if (compressor == kHapCompressorSnappy)
{
snappy_status snappy_result = snappy_uncompressed_length((const char *)texture_section, texture_section_length, &bytesUsed);
if (snappy_result != SNAPPY_OK)
{
return HapResult_Internal_Error;
}
if (bytesUsed > outputBufferBytes)
{
return HapResult_Buffer_Too_Small;
}
snappy_result = snappy_uncompress((const char *)texture_section, texture_section_length, (char *)outputBuffer, &bytesUsed);
if (snappy_result != SNAPPY_OK)
{
return HapResult_Internal_Error;
}
}
else if (compressor == kHapCompressorNone)
{
bytesUsed = texture_section_length;
if (texture_section_length > outputBufferBytes)
{
return HapResult_Buffer_Too_Small;
}
memcpy(outputBuffer, texture_section, texture_section_length);
}
else
{
return HapResult_Bad_Frame;
}
if (outputBufferBytesUsed != NULL)
{
*outputBufferBytesUsed = bytesUsed;
}
return HapResult_No_Error;
}
int hap_get_section_at_index(const void *input_buffer, uint32_t input_buffer_bytes,
unsigned int index,
const void **section, uint32_t *section_length, unsigned int *section_type)
{
int result;
uint32_t section_header_length;
result = hap_read_section_header(input_buffer, input_buffer_bytes, §ion_header_length, section_length, section_type);
if (result != HapResult_No_Error)
{
return result;
}
if (*section_type == kHapSectionMultipleImages)
{
size_t offset = 0;
size_t top_section_length = *section_length;
input_buffer = ((uint8_t *)input_buffer) + section_header_length;
section_header_length = 0;
*section_length = 0;
for (int i = 0; i <= index; i++) {
offset += section_header_length + *section_length;
if (offset >= top_section_length)
{
return HapResult_Bad_Arguments;
}
result = hap_read_section_header(((uint8_t *)input_buffer) + offset,
top_section_length - offset,
§ion_header_length,
section_length,
section_type);
if (result != HapResult_No_Error)
{
return result;
}
}
offset += section_header_length;
*section = ((uint8_t *)input_buffer) + offset;
return HapResult_No_Error;
}
else if (index == 0)
{
*section = ((uint8_t *)input_buffer) + section_header_length;
return HapResult_No_Error;
}
else
{
*section = NULL;
*section_length = 0;
*section_type = 0;
return HapResult_Bad_Arguments;
}
}
unsigned int HapDecode(const void *inputBuffer, unsigned long inputBufferBytes,
unsigned int index,
HapDecodeCallback callback, void *info,
void *outputBuffer, unsigned long outputBufferBytes,
unsigned long *outputBufferBytesUsed,
unsigned int *outputBufferTextureFormat)
{
int result = HapResult_No_Error;
const void *section;
uint32_t section_length;
unsigned int section_type;
if (inputBuffer == NULL
|| index > 1
|| callback == NULL
|| outputBuffer == NULL
|| outputBufferTextureFormat == NULL
)
{
return HapResult_Bad_Arguments;
}
result = hap_get_section_at_index(inputBuffer, inputBufferBytes, index, §ion, §ion_length, §ion_type);
if (result == HapResult_No_Error)
{
result = hap_decode_single_texture(section,
section_length,
section_type,
callback, info,
outputBuffer,
outputBufferBytes,
outputBufferBytesUsed,
outputBufferTextureFormat);
}
return result;
}
unsigned int HapGetFrameTextureCount(const void *inputBuffer, unsigned long inputBufferBytes, unsigned int *outputTextureCount)
{
int result;
uint32_t section_header_length;
uint32_t section_length;
unsigned int section_type;
result = hap_read_section_header(inputBuffer, inputBufferBytes, §ion_header_length, §ion_length, §ion_type);
if (result != HapResult_No_Error)
{
return result;
}
if (section_type == kHapSectionMultipleImages)
{
uint32_t offset = section_header_length;
uint32_t top_section_length = section_length;
*outputTextureCount = 0;
while (offset < top_section_length) {
result = hap_read_section_header(((uint8_t *)inputBuffer) + offset,
inputBufferBytes - offset,
§ion_header_length,
§ion_length,
§ion_type);
if (result != HapResult_No_Error)
{
return result;
}
offset += section_header_length + section_length;
*outputTextureCount += 1;
}
return HapResult_No_Error;
}
else
{
*outputTextureCount = 1;
return HapResult_No_Error;
}
}
unsigned int HapGetFrameTextureFormat(const void *inputBuffer, unsigned long inputBufferBytes, unsigned int index, unsigned int *outputBufferTextureFormat)
{
unsigned int result = HapResult_No_Error;
const void *section;
uint32_t section_length;
unsigned int section_type;
if (inputBuffer == NULL
|| index > 1
|| outputBufferTextureFormat == NULL
)
{
return HapResult_Bad_Arguments;
}
result = hap_get_section_at_index(inputBuffer, inputBufferBytes, index, §ion, §ion_length, §ion_type);
if (result == HapResult_No_Error)
{
*outputBufferTextureFormat = hap_texture_format_constant_for_format_identifier(hap_bottom_4_bits(section_type));
if (*outputBufferTextureFormat == 0)
{
result = HapResult_Bad_Frame;
}
}
return result;
}
unsigned int HapGetFrameTextureChunkCount(const void *inputBuffer, unsigned long inputBufferBytes, unsigned int index, int *chunk_count)
{
unsigned int result = HapResult_No_Error;
const void *section;
uint32_t section_length;
unsigned int section_type;
*chunk_count = 0;
if (inputBuffer == NULL
|| index > 1
)
{
return HapResult_Bad_Arguments;
}
result = hap_get_section_at_index(inputBuffer, inputBufferBytes, index, §ion, §ion_length, §ion_type);
if (result == HapResult_No_Error)
{
unsigned int compressor;
compressor = hap_top_4_bits(section_type);
if (compressor == kHapCompressorComplex)
{
const void *compressors = NULL;
const void *chunk_sizes = NULL;
const void *chunk_offsets = NULL;
const char *frame_data = NULL;
result = hap_decode_header_complex_instructions(section, section_length, chunk_count, &compressors, &chunk_sizes, &chunk_offsets, &frame_data);
if (result != HapResult_No_Error)
{
return result;
}
}
else if ((compressor == kHapCompressorSnappy)||(compressor == kHapCompressorNone))
{
*chunk_count = 1;
}
else
{
return HapResult_Bad_Frame;
}
}
return result;
}