Subversion Repositories freemyipod

Rev

Rev 694 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
694 user890104 1
//
2
//
3
//    Copyright 2011 user890104
4
//
5
//
6
//    This file is part of ipoddfu.
7
//
8
//    ipoddfu is free software: you can redistribute it and/or
9
//    modify it under the terms of the GNU General Public License as
10
//    published by the Free Software Foundation, either version 2 of the
11
//    License, or (at your option) any later version.
12
//
13
//    ipoddfu is distributed in the hope that it will be useful,
14
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
//    See the GNU General Public License for more details.
17
//
18
//    You should have received a copy of the GNU General Public License along
19
//    with ipoddfu.  If not, see <http://www.gnu.org/licenses/>.
20
//
21
//
22
 
23
 
24
#include <stdio.h>
25
 
972 user890104 26
#include <libusb.h>
694 user890104 27
 
28
#include "misc.h"
29
 
30
const char *libusb_error_messages[13] =
31
{
32
    "No error",                // LIBUSB_SUCCESS = 0
33
    "Input/output error",      // LIBUSB_ERROR_IO = -1
34
    "Invalid parameter",       // LIBUSB_ERROR_INVALID_PARAM = -2
35
    "Access denied",           // LIBUSB_ERROR_ACCESS = -3
36
    "No such device",          // LIBUSB_ERROR_NO_DEVICE = -4
37
    "Entity not found",        // LIBUSB_ERROR_NOT_FOUND = -5
38
    "Resource busy",           // LIBUSB_ERROR_BUSY = -6
39
    "Operation timed out",     // LIBUSB_ERROR_TIMEOUT = -7
40
    "Overflow",                // LIBUSB_ERROR_OVERFLOW = -8
41
    "Pipe error",              // LIBUSB_ERROR_PIPE = -9
42
    "System call interrupted", // LIBUSB_ERROR_INTERRUPTED = -10
43
    "Insufficient memory",     // LIBUSB_ERROR_NO_MEM = -11
44
    "Operation not supported", // LIBUSB_ERROR_NOT_SUPPORTED = -12
45
};
46
 
47
long int fgetsize(FILE *fp)
48
{
49
    static long int pos, size;
50
 
51
    if (-1L == (pos = ftell(fp)))
52
    {
53
        perror("ftell");
54
 
55
        return -1L;
56
    }
57
 
58
    if(fseek(fp, 0L, SEEK_END))
59
    {
60
        perror("fseek");
61
 
62
        return -1L;
63
    }
64
 
65
    if (-1L == (size = ftell(fp)))
66
    {
67
        perror("ftell");
68
 
69
        return -1L;
70
    }
71
 
72
    if (fseek(fp, pos, SEEK_SET))
73
    {
74
        perror("fseek");
75
 
76
        return -1L;
77
    }
78
 
79
    return size;
80
}
81
 
82
void dump_packet(const unsigned char *data, const unsigned int length)
83
{
84
    static unsigned int i;
85
 
86
    for (i = 0; i < length; ++i)
87
    {
88
        printf("%02x ", data[i]);
89
 
90
        if (i % 4 == 3)
91
        {
92
            printf(" ");
93
        }
94
 
95
        if (i % 16 == 15 && i + 1 < length)
96
        {
97
            printf("\n");
98
        }
99
    }
100
 
101
    printf("\n");
102
}
103
 
104
void print_error(const int code)
105
{
106
    if (code > 0)
107
    {    
108
        fprintf(stderr, "error: code %d\n", code);
109
    }
110
    else
111
    {
112
        fprintf(stderr, "libusb error: %s (code %d)\n", (
113
            LIBUSB_ERROR_OTHER == code || code > 0 ?
114
                "unspecified" : libusb_error_messages[-code]
115
        ), code);
116
    }
117
}