Subversion Repositories freemyipod

Rev

Rev 693 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 693 Rev 694
Line -... Line 1...
-
 
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
 
1
#include <stdio.h>
24
#include <stdio.h>
2
 
25
 
3
#include <libusb-1.0/libusb.h>
26
#include <libusb-1.0/libusb.h>
4
 
27
 
5
#include "misc.h"
28
#include "misc.h"
6
 
29
 
7
const char *libusb_error_messages[13] = {
30
const char *libusb_error_messages[13] =
-
 
31
{
8
	"No error",					/* LIBUSB_SUCCESS = 0 */
32
    "No error",                // LIBUSB_SUCCESS = 0
9
	"Input/output error",		/* LIBUSB_ERROR_IO = -1 */
33
    "Input/output error",      // LIBUSB_ERROR_IO = -1
10
	"Invalid parameter",		/* LIBUSB_ERROR_INVALID_PARAM = -2 */
34
    "Invalid parameter",       // LIBUSB_ERROR_INVALID_PARAM = -2
11
	"Access denied",			/* LIBUSB_ERROR_ACCESS = -3 */
35
    "Access denied",           // LIBUSB_ERROR_ACCESS = -3
12
	"No such device",			/* LIBUSB_ERROR_NO_DEVICE = -4 */
36
    "No such device",          // LIBUSB_ERROR_NO_DEVICE = -4
13
	"Entity not found",			/* LIBUSB_ERROR_NOT_FOUND = -5 */
37
    "Entity not found",        // LIBUSB_ERROR_NOT_FOUND = -5
14
	"Resource busy",			/* LIBUSB_ERROR_BUSY = -6 */
38
    "Resource busy",           // LIBUSB_ERROR_BUSY = -6
15
	"Operation timed out",		/* LIBUSB_ERROR_TIMEOUT = -7 */
39
    "Operation timed out",     // LIBUSB_ERROR_TIMEOUT = -7
16
	"Overflow",					/* LIBUSB_ERROR_OVERFLOW = -8 */
40
    "Overflow",                // LIBUSB_ERROR_OVERFLOW = -8
17
	"Pipe error",				/* LIBUSB_ERROR_PIPE = -9 */
41
    "Pipe error",              // LIBUSB_ERROR_PIPE = -9
18
	"System call interrupted",	/* LIBUSB_ERROR_INTERRUPTED = -10 */
42
    "System call interrupted", // LIBUSB_ERROR_INTERRUPTED = -10
19
	"Insufficient memory",		/* LIBUSB_ERROR_NO_MEM = -11 */
43
    "Insufficient memory",     // LIBUSB_ERROR_NO_MEM = -11
20
	"Operation not supported",	/* LIBUSB_ERROR_NOT_SUPPORTED = -12 */
44
    "Operation not supported", // LIBUSB_ERROR_NOT_SUPPORTED = -12
21
};
45
};
22
 
46
 
23
long int fgetsize(FILE *fp) {
47
long int fgetsize(FILE *fp)
-
 
48
{
24
	static long int pos, size;
49
    static long int pos, size;
25
	
50
    
26
	if (-1L == (pos = ftell(fp))) {
51
    if (-1L == (pos = ftell(fp)))
-
 
52
    {
27
		perror("ftell");
53
        perror("ftell");
28
		
54
        
29
		return -1L;
55
        return -1L;
30
	}
56
    }
31
	
57
    
32
	if(fseek(fp, 0L, SEEK_END)) {
58
    if(fseek(fp, 0L, SEEK_END))
-
 
59
    {
33
		perror("fseek");
60
        perror("fseek");
34
		
61
        
35
		return -1L;
62
        return -1L;
36
	}
63
    }
37
	
64
    
38
	if (-1L == (size = ftell(fp))) {
65
    if (-1L == (size = ftell(fp)))
-
 
66
    {
39
		perror("ftell");
67
        perror("ftell");
40
		
68
        
41
		return -1L;
69
        return -1L;
42
	}
70
    }
43
	
71
    
44
	if (fseek(fp, pos, SEEK_SET)) {
72
    if (fseek(fp, pos, SEEK_SET))
-
 
73
    {
45
		perror("fseek");
74
        perror("fseek");
46
		
75
        
47
		return -1L;
76
        return -1L;
48
	}
77
    }
49
	
78
    
50
	return size;
79
    return size;
51
}
80
}
52
 
81
 
53
void dump_packet(const unsigned char *data, const unsigned int length) {
82
void dump_packet(const unsigned char *data, const unsigned int length)
-
 
83
{
54
	static unsigned int i;
84
    static unsigned int i;
55
	
85
    
56
	for (i = 0; i < length; ++i) {
86
    for (i = 0; i < length; ++i)
-
 
87
    {
57
		printf("%02x ", data[i]);
88
        printf("%02x ", data[i]);
58
		
89
        
59
		if (i % 4 == 3) {
90
        if (i % 4 == 3)
-
 
91
        {
60
			printf(" ");
92
            printf(" ");
61
		}
93
        }
62
		
94
        
63
		if (i % 16 == 15 && i + 1 < length) {
95
        if (i % 16 == 15 && i + 1 < length)
-
 
96
        {
64
			printf("\n");
97
            printf("\n");
65
		}
98
        }
66
	}
99
    }
67
 
100
 
68
	printf("\n");
101
    printf("\n");
69
}
102
}
70
 
103
 
71
void print_error(const int code) {
104
void print_error(const int code)
-
 
105
{
72
	if (code > 0) {	
106
    if (code > 0)
-
 
107
    {    
73
		fprintf(stderr, "error: code %d\n", code);
108
        fprintf(stderr, "error: code %d\n", code);
74
	}
109
    }
-
 
110
    else
75
	else {
111
    {
76
		fprintf(stderr, "libusb error: %s (code %d)\n", (
112
        fprintf(stderr, "libusb error: %s (code %d)\n", (
77
			LIBUSB_ERROR_OTHER == code || code > 0 ?
113
            LIBUSB_ERROR_OTHER == code || code > 0 ?
78
				"unspecified" :
-
 
79
				libusb_error_messages[-code]
114
                "unspecified" : libusb_error_messages[-code]
80
		), code);
115
        ), code);
81
	}
116
    }
82
}
117
}