summaryrefslogtreecommitdiffstats
path: root/tools/ucl2src.pl
blob: 54cda5b3c0e8ba1d2fa2f3eb68ebde22e2014669 (plain)
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
#!/usr/bin/env perl
############################################################################
#             __________               __   ___.                  
#   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___  
#   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /  
#   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <   
#   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
#                     \/            \/     \/    \/            \/ 
# $Id$
#
# Copyright (C) 2005 by Jens Arnold
#
# All files in this archive are subject to the GNU General Public License.
# See the file COPYING in the source tree root for full license agreement.
#
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
# KIND, either express or implied.
#
############################################################################

if (!$ARGV[0])
{
    print <<HERE
Usage: ucl2src  [-p=<prefix>] <ucl file>

Check & strip header from an .ucl file and generate <prefix>.c and
<prefix>.h from it.
HERE
;
    exit;
}

my $prefix = $p;
if(!$prefix) {
    $prefix="uclimage";
}

my $input = $ARGV[0];
my $buffer;
my $insize;
my $readsize = 0;

open(INF, "<$input") or die "Can't open $input";
binmode INF;

# check UCL header

# magic header
read(INF, $buffer, 8);
if ($buffer ne pack("C8", 0x00, 0xe9, 0x55, 0x43, 0x4c, 0xff, 0x01, 0x1a))
{
    die "Not an UCL file.";
}
read(INF, $buffer, 4);

# method
read(INF, $buffer, 1);
if (ord($buffer) != 0x2E)
{
    die sprintf("Wrong compression method (expected 0x2E, found 0x%02X)",
                ord($buffer));
}

read(INF, $buffer, 9);

# file size
read(INF, $buffer, 4);
$insize = unpack("N", $buffer) + 8;

open(OUTF, ">$prefix.c") or die "Can't open $prefix.c";

print OUTF <<HERE
/* This file was automatically generated using ucl2src.pl */

/* Data compressed with UCL method 0x2e follows */
const unsigned char image[] = {
HERE
    ;
    
while (read(INF, $buffer, 1))
{
    $readsize++;
    printf OUTF ("0x%02x,", ord($buffer));
    if (!($readsize % 16))
    {
        print OUTF "\n";
    }
}

close(INF);

if ($readsize != $insize)
{
    die "Input file truncated, got $readsize of $insize bytes."
}

print OUTF <<HERE
};
/* end of compressed image */
HERE
    ;
close(OUTF);

open(OUTF, ">$prefix.h") or die "Can't open $prefix.h";

print OUTF "/* This file was automatically generated using ucl2src.pl */\n";
print OUTF "extern const unsigned char image[".$insize."];\n";

close(OUTF);