1#!/usr/bin/perl -w
2#
3# USAGE: $0 </path/to/libgphoto2/camlibs/ptp2>
4#
5# USB PTP Dissector
6#    Extracts USB devices from libgphoto2
7#  This is then parsed by make-usb.py to make epan/dissectors/usb.c
8#
9# (c)2013 Max Baker <max@warped.org>
10#
11# SPDX-License-Identifier: GPL-2.0-or-later
12
13my $path = shift @ARGV || '.';
14
15$re_hex = '0x[0-9a-f]+';
16
17parse_file("$path/library.c",1);
18parse_file("$path/music-players.h",0);
19
20open (O,"> tools/usb-ptp-extract-models.txt") or die $!;
21
22foreach my $vendor (sort {hex($a) <=> hex($b)} keys %devices) {
23    my $p = $devices{$vendor};
24    foreach my $product (sort {hex($a) <=> hex($b)} keys %$p) {
25        my $pd = $product; $pd =~ s/^0x//i;
26        my $v = $vendor; $v =~ s/^0x//i;
27        # { 0xeb1ae355, "KWorld DVB-T 355U Digital TV Dongle" },
28        #printf "    { 0x%s%s, \"%s\" },\n",$v, $pd, $p->{$product};
29
30        printf O "%s%s %s\n", $v, $pd, $p->{$product};
31    }
32}
33
34close O or die $!;
35
36exit;
37
38sub parse_file {
39    my $file = shift;
40    my $detect = shift;
41
42    my $start = !$detect;
43
44    open (H,"<$file") or die "Could not find $file. $!";
45    while (<H>) {
46        chomp;
47
48        # Look for models[] line as start
49        if (/\bmodels\[\]/) {
50            $start = 1;
51            next;
52        }
53
54        # Look for }; as the end
55        $start = 0 if /^\s*};/;
56
57        next unless $start;
58        # Skip comment lines
59
60        # Remove comments
61        s,/\*.*\*/,,g;
62
63        s,^\s*,,;
64        s,\s*$,,;
65
66        # Skip blank lines
67        next if /^$/;
68        next if m,^\s*/?\*,;
69
70        my $line = $_;
71
72        my ($model, $vendor, $product, $manif);
73
74        # {"Nikon:DSC D90 (PTP mode)",  0x04b0, 0x0421, PTP_CAP|PTP_CAP_PREVIEW},
75        if($line =~ m/^\{
76            "([^"]+)",\s*
77            ($re_hex),\s*
78            ($re_hex),\s*
79            /xi) {
80
81            ($model, $vendor, $product) = ($1,$2,$3);
82            $model =~ s/:/ /;
83            $model =~ s/\(.*\)//;
84        }
85        # { "Creative", 0x041e, "ZEN X-Fi 3", 0x4169,
86        # { "TrekStor", 0x0402, "i.Beat Sweez FM", 0x0611,
87        if($line=~ m/^\{\s*
88            "([^"]+)",\s*
89            ($re_hex),\s*
90            "([^"]+)",\s*
91            ($re_hex),\s*
92            /xi) {
93            ($manif, $vendor, $model, $product) = ($1,$2,$3,$4);
94            $model = "$manif $model";
95        }
96
97        next unless defined $vendor;
98
99        $model =~ s/\s+/ /g;
100        $model =~ s/\s*$//;
101
102        #print "$vendor $product $model\n";
103        $devices{$vendor}->{$product}=$model;
104    }
105}
106