File: utility.cpp

package info (click to toggle)
uefitool 0.28.0%2BA73-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 7,728 kB
  • sloc: ansic: 55,322; cpp: 23,375; sh: 43; xml: 23; makefile: 5
file content (681 lines) | stat: -rwxr-xr-x 24,860 bytes parent folder | download
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
/* utility.cpp
 
 Copyright (c) 2016, Nikolaj Schlej. All rights reserved.
 This program and the accompanying materials
 are licensed and made available under the terms and conditions of the BSD License
 which accompanies this distribution.  The full text of the license may be found at
 http://opensource.org/licenses/bsd-license.php
 
 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 
 */

#include <cstdio>
#include <cctype>
#include <cstring>

#include "treemodel.h"
#include "utility.h"
#include "ffs.h"
#include "Tiano/EfiTianoCompress.h"
#include "Tiano/EfiTianoDecompress.h"
#include "LZMA/LzmaCompress.h"
#include "LZMA/LzmaDecompress.h"

// Returns bytes as string when all bytes are ascii visible, hex representation otherwise
UString visibleAsciiOrHex(UINT8* bytes, UINT32 length)
{
    bool ascii = true;
    UString asciiString;
    UString hexString;
    
    for (UINT32 i = 0; i < length; i++) {
        hexString += usprintf("%02X", bytes[i]);
        
        if (ascii && i > 0 && bytes[i] == '\x00') { // Check for the rest of the buffer being zeroes, and make the whole previous string visible, if so
            for (UINT32 j = i + 1; j < length; j++) {
                if (bytes[j] != '\x00') {
                    ascii = false;
                    break;
                }
            }
            
            if (ascii) {
                // No need to continue iterating over every symbol, we did it already
                break;
            }
        }
        else if (bytes[i] < '\x20' || bytes[i] > '\x7E') {  // Explicit ascii codes to avoid locale dependency
            ascii = false;
        }
        
        if (ascii) {
            asciiString += usprintf("%c", bytes[i]);
        }
    }
    
    if (ascii) {
        return asciiString;
    }
    
    return hexString;
}

// Returns unique name string based for tree item
UString uniqueItemName(const UModelIndex & index)
{
    // Sanity check
    if (!index.isValid())
        return UString("InvalidIndex");
    
    // Get model from index
    const TreeModel* model = (const TreeModel*)index.model();
    
    // Construct the name
    UString itemName = model->name(index);
    UString itemText = model->text(index);
    
    // Default name
    UString name = itemName;
    switch (model->type(index)) {
        case Types::NvarEntry:
        case Types::VssEntry:
        case Types::SysFEntry:
        case Types::EvsaEntry:
        case Types::PhoenixFlashMapEntry:
        case Types::InsydeFlashDeviceMapEntry:
        case Types::File:
            name = itemText.isEmpty() ? itemName : itemName + '_' + itemText;
            break;
        case Types::Section: {
            // Get parent file name
            UModelIndex fileIndex = model->findParentOfType(index, Types::File);
            UString fileText = model->text(fileIndex);
            name = fileText.isEmpty() ? model->name(fileIndex) : model->name(fileIndex) + '_' + fileText;
            
            // Special case of GUIDed sections
            if (model->subtype(index) == EFI_SECTION_GUID_DEFINED || model->subtype(index) == EFI_SECTION_FREEFORM_SUBTYPE_GUID) {
                name = model->name(index) +'_' + name;
            }
        } break;
    }
    
    // Populate subtypeString
    UString subtypeString = itemSubtypeToUString(model->type(index), model->subtype(index));
    
    // Create final name
    name = itemTypeToUString(model->type(index))
    + (subtypeString.length() ? ('_' + subtypeString) : UString())
    + '_' + name;
    
    fixFileName(name, true);
    
    return name;
}

// Makes the name usable as a file name
void fixFileName(UString &name, bool replaceSpaces)
{
    // Replace some symbols with underscores for compatibility
    const char table[] = {
        '/', // Banned in *nix and Windows
        '<', '>', ':', '\"', '\\', '|', '?', '*', // Banned in Windows
    };
    int nameLength = (int)name.length(); // Note: Qt uses int for whatever reason.
    for (int i = 0; i < nameLength; i++) {
        if (
            name[i] < (char)0x20 || // ASCII control characters, banned in Windows, hard to work with in *nix
            name[i] > (char)0x7f || // high ASCII characters
            (replaceSpaces && name[i] == ' ') // Provides better readability
            ) {
                name[i] = '_';
                continue;
            }
        for (size_t j = 0; j < sizeof(table); j++) {
            if (name[i] == table[j]) {
                name[i] = '_';
                break;
            }
        }
    }
    if (!nameLength) {
        name = "_";
    }
}

// Returns text representation of error code
UString errorCodeToUString(USTATUS errorCode)
{
    // TODO: improve
    switch (errorCode) {
        case U_SUCCESS:                         return UString("Success");
        case U_NOT_IMPLEMENTED:                 return UString("Not implemented");
        case U_INVALID_PARAMETER:               return UString("Function called with invalid parameter");
        case U_BUFFER_TOO_SMALL:                return UString("Buffer too small");
        case U_OUT_OF_RESOURCES:                return UString("Out of resources");
        case U_OUT_OF_MEMORY:                   return UString("Out of memory");
        case U_FILE_OPEN:                       return UString("File can't be opened");
        case U_FILE_READ:                       return UString("File can't be read");
        case U_FILE_WRITE:                      return UString("File can't be written");
        case U_ITEM_NOT_FOUND:                  return UString("Item not found");
        case U_UNKNOWN_ITEM_TYPE:               return UString("Unknown item type");
        case U_INVALID_FLASH_DESCRIPTOR:        return UString("Invalid flash descriptor");
        case U_INVALID_REGION:                  return UString("Invalid region");
        case U_EMPTY_REGION:                    return UString("Empty region");
        case U_BIOS_REGION_NOT_FOUND:           return UString("BIOS region not found");
        case U_VOLUMES_NOT_FOUND:               return UString("UEFI volumes not found");
        case U_INVALID_VOLUME:                  return UString("Invalid UEFI volume");
        case U_VOLUME_REVISION_NOT_SUPPORTED:   return UString("Volume revision not supported");
        case U_UNKNOWN_FFS:                     return UString("Unknown file system");
        case U_INVALID_FILE:                    return UString("Invalid file");
        case U_INVALID_SECTION:                 return UString("Invalid section");
        case U_UNKNOWN_SECTION:                 return UString("Unknown section");
        case U_STANDARD_COMPRESSION_FAILED:     return UString("Standard compression failed");
        case U_CUSTOMIZED_COMPRESSION_FAILED:   return UString("Customized compression failed");
        case U_STANDARD_DECOMPRESSION_FAILED:   return UString("Standard decompression failed");
        case U_CUSTOMIZED_DECOMPRESSION_FAILED: return UString("Customized decompression failed");
        case U_UNKNOWN_COMPRESSION_TYPE:        return UString("Unknown compression type");
        case U_UNKNOWN_EXTRACT_MODE:            return UString("Unknown extract mode");
        case U_UNKNOWN_REPLACE_MODE:            return UString("Unknown replace mode");
        case U_UNKNOWN_IMAGE_TYPE:              return UString("Unknown executable image type");
        case U_UNKNOWN_PE_OPTIONAL_HEADER_TYPE: return UString("Unknown PE optional header type");
        case U_UNKNOWN_RELOCATION_TYPE:         return UString("Unknown relocation type");
        case U_COMPLEX_BLOCK_MAP:               return UString("Block map structure too complex for correct analysis");
        case U_DIR_ALREADY_EXIST:               return UString("Directory already exists");
        case U_DIR_CREATE:                      return UString("Directory can't be created");
        case U_DIR_CHANGE:                      return UString("Change directory failed");
        case U_DEPEX_PARSE_FAILED:              return UString("Dependency expression parsing failed");
        case U_TRUNCATED_IMAGE:                 return UString("Image is truncated");
        case U_INVALID_CAPSULE:                 return UString("Invalid capsule");
        case U_STORES_NOT_FOUND:                return UString("Stores not found");
        case U_INVALID_STORE_SIZE:              return UString("Invalid store size");
        case U_INVALID_STORE:                   return UString("Invalid store");
        default:                                return usprintf("Unknown error %02lX", errorCode);
    }
}

// Compression routines
USTATUS decompress(const UByteArray & compressedData, const UINT8 compressionType, UINT8 & algorithm, UINT32 & dictionarySize, UByteArray & decompressedData, UByteArray & efiDecompressedData)
{
    const UINT8* data;
    UINT32 dataSize;
    UINT8* decompressed;
    UINT8* efiDecompressed;
    UINT32 decompressedSize = 0;
    UINT8* scratch;
    UINT32 scratchSize = 0;
    const EFI_TIANO_HEADER* header;
    
    // For all but LZMA dictionary size is 0
    dictionarySize = 0;
    
    switch (compressionType)
    {
        case EFI_NOT_COMPRESSED: {
            decompressedData = compressedData;
            algorithm = COMPRESSION_ALGORITHM_NONE;
            return U_SUCCESS;
        }
        case EFI_STANDARD_COMPRESSION: {
            // Set default algorithm to unknown
            algorithm = COMPRESSION_ALGORITHM_UNKNOWN;
            
            // Get buffer sizes
            data = (UINT8*)compressedData.data();
            dataSize = (UINT32)compressedData.size();
            
            // Check header to be valid
            header = (const EFI_TIANO_HEADER*)data;
            if (header->CompSize + sizeof(EFI_TIANO_HEADER) != dataSize)
                return U_STANDARD_DECOMPRESSION_FAILED;
            
            // Get info function is the same for both algorithms
            if (U_SUCCESS != EfiTianoGetInfo(data, dataSize, &decompressedSize, &scratchSize))
                return U_STANDARD_DECOMPRESSION_FAILED;
            
            // Allocate memory
            decompressed = (UINT8*)malloc(decompressedSize);
            efiDecompressed = (UINT8*)malloc(decompressedSize);
            scratch = (UINT8*)malloc(scratchSize);
            if (!decompressed || !efiDecompressed || !scratch) {
                free(decompressed);
                free(efiDecompressed);
                free(scratch);
                return U_STANDARD_DECOMPRESSION_FAILED;
            }
            
            // Decompress section data using both algorithms
            USTATUS result = U_SUCCESS;
            // Try Tiano
            USTATUS TianoResult = TianoDecompress(data, dataSize, decompressed, decompressedSize, scratch, scratchSize);
            // Try EFI 1.1
            USTATUS EfiResult = EfiDecompress(data, dataSize, efiDecompressed, decompressedSize, scratch, scratchSize);
            
            if (decompressedSize > INT32_MAX) {
                result = U_STANDARD_DECOMPRESSION_FAILED;
            }
            else if (EfiResult == U_SUCCESS && TianoResult == U_SUCCESS) { // Both decompressions are OK
                algorithm = COMPRESSION_ALGORITHM_UNDECIDED;
                decompressedData = UByteArray((const char*)decompressed, (int)decompressedSize);
                efiDecompressedData = UByteArray((const char*)efiDecompressed, (int)decompressedSize);
            }
            else if (TianoResult == U_SUCCESS) { // Only Tiano is OK
                algorithm = COMPRESSION_ALGORITHM_TIANO;
                decompressedData = UByteArray((const char*)decompressed, (int)decompressedSize);
            }
            else if (EfiResult == U_SUCCESS) { // Only EFI 1.1 is OK
                algorithm = COMPRESSION_ALGORITHM_EFI11;
                decompressedData = UByteArray((const char*)efiDecompressed, (int)decompressedSize);
            }
            else { // Both decompressions failed
                result = U_STANDARD_DECOMPRESSION_FAILED;
            }
            
            free(decompressed);
            free(efiDecompressed);
            free(scratch);
            return result;
        }
        case EFI_CUSTOMIZED_COMPRESSION: {
            // Set default algorithm to unknown
            algorithm = COMPRESSION_ALGORITHM_UNKNOWN;
            
            // Get buffer sizes
            data = (const UINT8*)compressedData.constData();
            dataSize = (UINT32)compressedData.size();
            
            // Get info as normal LZMA section
            if (U_SUCCESS != LzmaGetInfo(data, dataSize, &decompressedSize)) {
                // Get info as Intel legacy LZMA section
                data += sizeof(UINT32);
                if (U_SUCCESS != LzmaGetInfo(data, dataSize, &decompressedSize)) {
                    return U_CUSTOMIZED_DECOMPRESSION_FAILED;
                }
                else {
                    algorithm = COMPRESSION_ALGORITHM_LZMA_INTEL_LEGACY;
                }
            }
            else {
                algorithm = COMPRESSION_ALGORITHM_LZMA;
            }
            
            // Allocate memory
            decompressed = (UINT8*)malloc(decompressedSize);
            if (!decompressed) {
                return U_OUT_OF_MEMORY;
            }
            
            // Decompress section data
            if (U_SUCCESS != LzmaDecompress(data, dataSize, decompressed)) {
                free(decompressed);
                return U_CUSTOMIZED_DECOMPRESSION_FAILED;
            }
            
            if (decompressedSize > INT32_MAX) {
                free(decompressed);
                return U_CUSTOMIZED_DECOMPRESSION_FAILED;
            }
            
            dictionarySize = readUnaligned((UINT32*)(data + 1)); // LZMA dictionary size is stored in bytes 1-4 of LZMA properties header
            decompressedData = UByteArray((const char*)decompressed, (int)decompressedSize);
            free(decompressed);
            return U_SUCCESS;
        }
        case EFI_CUSTOMIZED_COMPRESSION_LZMAF86: {
            // Set default algorithm to unknown
            algorithm = COMPRESSION_ALGORITHM_UNKNOWN;
            
            // Get buffer sizes
            data = (const UINT8*)compressedData.constData();
            dataSize = (UINT32)compressedData.size();
            
            // Get info as normal LZMA section
            if (U_SUCCESS != LzmaGetInfo(data, dataSize, &decompressedSize)) {
                return U_CUSTOMIZED_DECOMPRESSION_FAILED;
            }
            algorithm = COMPRESSION_ALGORITHM_LZMAF86;
            
            // Allocate memory
            decompressed = (UINT8*)malloc(decompressedSize);
            if (!decompressed) {
                return U_OUT_OF_MEMORY;
            }
            
            // Decompress section data
            if (U_SUCCESS != LzmaDecompress(data, dataSize, decompressed)) {
                free(decompressed);
                return U_CUSTOMIZED_DECOMPRESSION_FAILED;
            }
            
            if (decompressedSize > INT32_MAX) {
                free(decompressed);
                return U_CUSTOMIZED_DECOMPRESSION_FAILED;
            }
            
            // TODO: need to correctly handle non-x86 architecture of the FW image
            // After LZMA decompression, the data need to be converted to the raw data.
            UINT32 state = 0;
            z7_BranchConvSt_X86_Dec(decompressed, decompressedSize, 0, &state);
            
            dictionarySize = readUnaligned((UINT32*)(data + 1)); // LZMA dictionary size is stored in bytes 1-4 of LZMA properties header
            decompressedData = UByteArray((const char*)decompressed, (int)decompressedSize);
            free(decompressed);
            return U_SUCCESS;
        }
        default: {
            algorithm = COMPRESSION_ALGORITHM_UNKNOWN;
            return U_UNKNOWN_COMPRESSION_TYPE;
        }
    }
}

// 8bit sum calculation routine
UINT8 calculateSum8(const UINT8* buffer, UINT32 bufferSize)
{
    if (!buffer)
        return 0;
    
    UINT8 counter = 0;
    
    while (bufferSize--)
        counter += buffer[bufferSize];
    
    return counter;
}

// 8bit checksum calculation routine
UINT8 calculateChecksum8(const UINT8* buffer, UINT32 bufferSize)
{
    if (!buffer)
        return 0;
    
    return (UINT8)(0x100U - calculateSum8(buffer, bufferSize));
}

// 16bit checksum calculation routine
UINT16 calculateChecksum16(const UINT16* buffer, UINT32 bufferSize)
{
    if (!buffer)
        return 0;
    
    UINT16 counter = 0;
    UINT32 index = 0;
    
    bufferSize /= sizeof(UINT16);
    
    for (; index < bufferSize; index++) {
        counter = (UINT16)(counter + buffer[index]);
    }
    
    return (UINT16)(0x10000 - counter);
}

// 32bit checksum calculation routine
UINT32 calculateChecksum32(const UINT32* buffer, UINT32 bufferSize)
{
    if (!buffer)
        return 0;
    
    UINT32 counter = 0;
    UINT32 index = 0;
    
    bufferSize /= sizeof(UINT32);
    
    for (; index < bufferSize; index++) {
        counter = (UINT32)(counter + buffer[index]);
    }
    
    return (UINT32)(0x100000000ULL - counter);
}

// Returns 0x00..0xFF if an array is filled by a single repeated value, and 0xFFFFFFFF if not
UINT32 uniformByte(const UByteArray& a, const UINT32 rcIfEmpty)
{
    if (a.isEmpty())
        return rcIfEmpty;
    size_t s = a.size();
    if ((s == 1) || (s > 1 && memcmp(a.constData(), a.constData() + 1, s - 1) == 0))
        return (UINT8)a.at(0);
    return UINT32_MAX;
}

// Returns true if an array is filled by a specified single repeated value or an array is empty
UINT32 isUniformByte(const UByteArray& a, const UINT8 value)
{
    return uniformByte(a, value) == value;
}

// Get padding type for a given padding
UINT8 getPaddingType(const UByteArray & padding)
{
    switch (uniformByte(padding)) {
        case 0:
            return Subtypes::ZeroPadding;
        case 0xFF:
            return Subtypes::OnePadding;
    }
    return Subtypes::DataPadding;
}

static inline int char2hex(char c)
{
    if (c >= '0' && c <= '9')
        return c - '0';
    if (c >= 'A' && c <= 'F')
        return c - 'A' + 10;
    if (c == '.')
        return -2;
    return -1;
}

INTN findPattern(const UINT8 *pattern, const UINT8 *patternMask, UINTN patternSize,
                 const UINT8 *data, UINTN dataSize, UINTN dataOff)
{
    if (patternSize == 0 || dataSize == 0 || dataOff >= dataSize || dataSize - dataOff < patternSize)
        return -1;
    
    while (dataOff + patternSize <= dataSize) {
        bool matches = true;
        for (UINTN i = 0; i < patternSize; i++) {
            if ((data[dataOff + i] & patternMask[i]) != pattern[i]) {
                matches = false;
                break;
            }
        }
        
        if (matches)
            return static_cast<INTN>(dataOff);
        
        dataOff++;
    }
    
    return -1;
}

bool makePattern(const CHAR8 *textPattern, std::vector<UINT8> &pattern, std::vector<UINT8> &patternMask)
{
    UINTN len = std::strlen(textPattern);
    
    if (len == 0 || len % 2 != 0)
        return false;
    
    len /= 2;
    
    pattern.resize(len);
    patternMask.resize(len);
    
    for (UINTN i = 0; i < len; i++) {
        int v1 = char2hex(std::toupper(textPattern[i * 2]));
        int v2 = char2hex(std::toupper(textPattern[i * 2 + 1]));
        
        if (v1 == -1 || v2 == -1)
            return false;
        
        if (v1 != -2) {
            patternMask[i] = 0xF0;
            pattern[i] = static_cast<UINT8>(v1) << 4;
        }
        
        if (v2 != -2) {
            patternMask[i] |= 0x0F;
            pattern[i] |= static_cast<UINT8>(v2);
        }
    }
    
    return true;
}

USTATUS gzipDecompress(const UByteArray & input, UByteArray & output)
{
    output.clear();
    
    if (input.size() == 0)
        return U_SUCCESS;
    
    z_stream stream = {};
    stream.next_in = (z_const Bytef *)input.data();
    stream.avail_in = (uInt)input.size();
    stream.zalloc = Z_NULL;
    stream.zfree = Z_NULL;
    stream.opaque = Z_NULL;
    
    // 15 for the maximum history buffer, 16 for gzip only input
    int ret = inflateInit2(&stream, 15U | 16U);
    if (ret != Z_OK)
        return U_GZIP_DECOMPRESSION_FAILED;
    
    while (ret == Z_OK) {
        Bytef out[0x1000] = {};
        stream.next_out = out;
        stream.avail_out = sizeof(out);
        
        ret = inflate(&stream, Z_NO_FLUSH);
        if ((ret == Z_OK || ret == Z_STREAM_END) && stream.avail_out != sizeof(out))
            output += UByteArray((char *)out, sizeof(out) - stream.avail_out);
    }
    
    inflateEnd(&stream);
    return ret == Z_STREAM_END ? U_SUCCESS : U_GZIP_DECOMPRESSION_FAILED;
}

USTATUS zlibDecompress(const UByteArray& input, UByteArray& output)
{
    output.clear();

    if (input.size() == 0)
        return U_SUCCESS;

    z_stream stream = {};
    stream.next_in = (z_const Bytef*)input.data();
    stream.avail_in = (uInt)input.size();
    stream.zalloc = Z_NULL;
    stream.zfree = Z_NULL;
    stream.opaque = Z_NULL;

    // 15 for the maximum history buffer
    int ret = inflateInit2(&stream, 15U);
    if (ret != Z_OK)
        return U_ZLIB_DECOMPRESSION_FAILED;

    while (ret == Z_OK) {
        Bytef out[0x1000] = {};
        stream.next_out = out;
        stream.avail_out = sizeof(out);

        ret = inflate(&stream, Z_NO_FLUSH);
        if ((ret == Z_OK || ret == Z_STREAM_END) && stream.avail_out != sizeof(out))
            output += UByteArray((char*)out, sizeof(out) - stream.avail_out);
    }

    inflateEnd(&stream);
    return ret == Z_STREAM_END ? U_SUCCESS : U_ZLIB_DECOMPRESSION_FAILED;
}

USTATUS brotliDecompress(const UByteArray& input, UByteArray& output)
{
    output.clear();

    if (input.size() == 0)
        return U_SUCCESS;

    const uint8_t *next_in = (const uint8_t*)input.data();
    size_t avail_in = input.size();

    BrotliDecoderStateStruct *decoder = BrotliDecoderCreateInstance(nullptr, nullptr, nullptr);
    if (decoder == nullptr)
        return U_BROTLI_DECOMPRESSION_FAILED;

    BrotliDecoderResult ret = BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT;
    BROTLI_BOOL ret_param = BrotliDecoderSetParameter(decoder, BROTLI_DECODER_PARAM_LARGE_WINDOW, 1);
    if (ret_param != BROTLI_FALSE) {
        while (ret == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) {
            uint8_t out[0x1000] = {};
            uint8_t *next_out = out;
            size_t avail_out = sizeof(out);
            
            ret = BrotliDecoderDecompressStream(decoder, &avail_in, &next_in, &avail_out, &next_out, NULL);
            if ((ret == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT || ret == BROTLI_DECODER_RESULT_SUCCESS) && avail_out != sizeof(out))
                output += UByteArray((char*)out, sizeof(out) - avail_out);
        }
    }
    
    BrotliDecoderDestroyInstance(decoder);

    return ret == BROTLI_DECODER_RESULT_SUCCESS ? U_SUCCESS : U_BROTLI_DECOMPRESSION_FAILED;
}

UString fourCC(const UINT32 value)
{
    const UINT8 byte0 = (const UINT8)(value & 0xFF);
    const UINT8 byte1 = (const UINT8)((value & 0xFF00) >> 8);
    const UINT8 byte2 = (const UINT8)((value & 0xFF0000) >> 16);
    const UINT8 byte3 = (const UINT8)((value & 0xFF000000) >> 24);
    return usprintf("%c%c%c%c", byte0, byte1, byte2, byte3);
}

/*
 * Creates the OSI Fletcher checksum. See 8473-1, Appendix C, section C.3.
 * The checksum field of the passed PDU does not need to be reset to zero.
 *
 * The "Fletcher Checksum" was proposed in a paper by John G. Fletcher of
 * Lawrence Livermore Labs.  The Fletcher Checksum was proposed as an
 * alternative to cyclical redundancy checks because it provides error-
 * detection properties similar to cyclical redundancy checks but at the
 * cost of a simple summation technique.  Its characteristics were first
 * published in IEEE Transactions on Communications in January 1982.  One
 * version has been adopted by ISO for use in the class-4 transport layer
 * of the network protocol.
 *
 */
UINT32 fletcher32(const UByteArray& img)
{
    UINT32 c0;
    UINT32 c1;
    UINT32 checksum;
    INTN index;
    const UINT16* pptr = (const UINT16*)img.constData();

    INTN length = img.size() / 2;

    c0 = 0xFFFF;
    c1 = 0xFFFF;

    while (length) {
        index = length >= 359 ? 359 : length;
        length -= index;
        do {
            c0 += *(pptr++);
            c1 += c0;
        } while (--index);
        c0 = (c0 & 0xFFFF) + (c0 >> 16);
        c1 = (c1 & 0xFFFF) + (c1 >> 16);
    }

    /* Sums[0,1] mod 64K + overflow */
    c0 = (c0 & 0xFFFF) + (c0 >> 16);
    c1 = (c1 & 0xFFFF) + (c1 >> 16);
    checksum = (c1 << 16) | c0;

    return checksum;
}