]> CyberLeo.Net >> Repos - SourceForge/eyefi-config.git/blob - eyefi-firmware-fetch.pl
Change filesystem detection to fstypename comparison instead of naked integer
[SourceForge/eyefi-config.git] / eyefi-firmware-fetch.pl
1 #!/usr/bin/perl
2 use strict;
3 use LWP::UserAgent;
4 use Compress::Raw::Zlib ;
5
6 sub cat
7 {
8         my $filename = shift;;
9         my $contents;
10         my $line;
11         open FILE, "< $filename";
12         while ($line = <FILE>) {
13                 $contents .= $line;
14         }
15         close FILE;
16         return $contents;
17 }
18
19 my $ua = LWP::UserAgent->new;
20
21 my $xml;
22 my $ver = $ARGV[0];#'3.0144'; #'2.0400';
23 my $mac = $ARGV[1];
24 if ( ! defined $ver) {
25         die "usage: eyefi-firmware-fetch.pl <FIRMWARE VERSION> [optional MAC]";
26 }
27 while (1) {
28         my $m1 = 0x2d; #rand 0x30;
29         my $m2 = rand 256;
30         my $m3 = rand 256;
31         if (! length($mac)) {
32                 $mac = sprintf '00-18-56-%02x-%02x-%02x', $m1, $m2, $m3;
33         }
34         my $url = sprintf 'http://api.eye.fi/api/rest/eyeserver/v1/getCardFirmware?Card=%s&Version=%s', $mac, $ver;
35         print $url."\n";
36         my $res = $ua->get($url);
37         $xml = $res->content();
38         #rint $xml."\n";
39         next if $xml =~ /Card not found./;
40         next if $xml =~ /File not found./;
41         last;
42 }
43 my $filename .= "EYEFIFWU-$ver-$mac.bin";
44 printf STDERR "got %d bytes of xml\n", length($xml);
45 #strip the XML off:
46 $xml =~ s/<\?xml.*<Firmware>//s;
47 printf STDERR "got %d bytes of xml\n", length($xml);
48 $xml =~ s/<\/Firmware>.*Response>//s;
49 printf STDERR "got %d bytes of xml\n", length($xml);
50 my $base64_encoded = $xml;
51 printf STDERR "got %d bytes of base64 encoded data\n", length($base64_encoded);
52
53 use Email::MIME::Encodings;
54 my $zlib_encoded = Email::MIME::Encodings::decode(base64 => $base64_encoded);
55
56 printf STDERR "got %d bytes of zlib encoded data\n", length($zlib_encoded);
57
58 my $status;
59 my $output;
60
61 my $i;
62 ($i, $status) = new Compress::Raw::Zlib::Inflate() ;
63 $status = $i->inflate($zlib_encoded, $output);
64 $status = $i->inflateSync($zlib_encoded);
65
66 open FILE, "> $filename";
67 print FILE $output;
68 close FILE;
69 printf STDERR "done, wrote %d bytes to '$filename'\n", length($output);