├── README.markdown
├── dll
├── AdbWinApi.dll.def
├── AdbWinApi.dll.exports
├── AdbWinApi.dll.lib
├── build_exports.bat
└── build_lib.bat
├── myadb.sln
├── myadb.vcxproj
├── myadb.vcxproj.filters
└── src
├── adb.c
├── adb.h
├── adb_api.h
├── adb_client.c
├── adb_client.h
├── commandline.c
├── console.c
├── dirent.h
├── file_sync_client.c
├── file_sync_service.h
├── get_my_path_windows.c
├── libzipfile
├── centraldir.c
├── private.h
└── zipfile.c
├── mutex_list.h
├── services.c
├── sockets.c
├── sysdeps.h
├── sysdeps_win32.c
├── transport.c
├── transport_local.c
├── transport_usb.c
├── unistd.h
├── usb100.h
├── usb200.h
├── usb_vendors.c
├── usb_vendors.h
├── usb_windows.c
├── zipfile
└── zipfile.h
└── zlib
├── adler32.c
├── compress.c
├── crc32.c
├── crc32.h
├── deflate.c
├── deflate.h
├── gzclose.c
├── gzguts.h
├── gzlib.c
├── gzread.c
├── gzwrite.c
├── infback.c
├── inffast.c
├── inffast.h
├── inffixed.h
├── inflate.c
├── inflate.h
├── inftrees.c
├── inftrees.h
├── trees.c
├── trees.h
├── uncompr.c
├── zconf.h
├── zlib.h
├── zutil.c
└── zutil.h
/README.markdown:
--------------------------------------------------------------------------------
1 | my_adb is Android Debug Bridge code for Visual C++ 2010 Express Edition
2 | =======================================================================
3 |
4 | TODO
5 | ====
6 |
7 | - More 'officially' look repository for future code updating.
8 | - Import android code from android repo (@github, maybe)
9 |
--------------------------------------------------------------------------------
/dll/AdbWinApi.dll.def:
--------------------------------------------------------------------------------
1 | LIBRARY AdbWinApi.dll
2 | EXPORTS
3 | AdbCloseHandle
4 | AdbCreateInterface
5 | AdbCreateInterfaceByName
6 | AdbEnumInterfaces
7 | AdbGetDefaultBulkReadEndpointInformation
8 | AdbGetDefaultBulkWriteEndpointInformation
9 | AdbGetEndpointInformation
10 | AdbGetEndpointInterface
11 | AdbGetInterfaceName
12 | AdbGetOvelappedIoResult
13 | AdbGetSerialNumber
14 | AdbGetUsbConfigurationDescriptor
15 | AdbGetUsbDeviceDescriptor
16 | AdbGetUsbInterfaceDescriptor
17 | AdbHasOvelappedIoComplated
18 | AdbNextInterface
19 | AdbOpenDefaultBulkReadEndpoint
20 | AdbOpenDefaultBulkWriteEndpoint
21 | AdbOpenEndpoint
22 | AdbQueryInformationEndpoint
23 | AdbReadEndpointAsync
24 | AdbReadEndpointSync
25 | AdbResetInterfaceEnum
26 | AdbWriteEndpointAsync
27 | AdbWriteEndpointSync
28 |
--------------------------------------------------------------------------------
/dll/AdbWinApi.dll.exports:
--------------------------------------------------------------------------------
1 | Microsoft (R) COFF/PE Dumper Version 10.00.40219.01
2 | Copyright (C) Microsoft Corporation. All rights reserved.
3 |
4 |
5 | Dump of file AdbWinApi.dll
6 |
7 | File Type: DLL
8 |
9 | Section contains the following exports for AdbWinApi.DLL
10 |
11 | 00000000 characteristics
12 | 4A808501 time date stamp Tue Aug 11 05:37:21 2009
13 | 0.00 version
14 | 1 ordinal base
15 | 79 number of functions
16 | 79 number of names
17 |
18 | ordinal hint RVA name
19 |
20 | 1 0 00003332 ??0AdbEndpointObject@@QAE@ABV0@@Z
21 | 2 1 00004336 ??0AdbEndpointObject@@QAE@PAVAdbInterfaceObject@@EE@Z
22 | 3 2 00003419 ??0AdbIOCompletion@@QAE@ABV0@@Z
23 | 4 3 0000593F ??0AdbIOCompletion@@QAE@PAVAdbEndpointObject@@KPAX@Z
24 | 5 4 00004288 ??0AdbInterfaceObject@@QAE@ABV0@@Z
25 | 6 5 00005188 ??0AdbInterfaceObject@@QAE@PBG@Z
26 | 7 6 000031E5 ??0AdbObjectHandle@@QAE@ABV0@@Z
27 | 8 7 00005B55 ??0AdbObjectHandle@@QAE@W4AdbObjectType@@@Z
28 | 9 8 00004382 ??1AdbEndpointObject@@MAE@XZ
29 | 10 9 00005991 ??1AdbIOCompletion@@MAE@XZ
30 | 11 A 0000513F ??1AdbInterfaceObject@@MAE@XZ
31 | 12 B 00005B7C ??1AdbObjectHandle@@MAE@XZ
32 | 13 C 00003369 ??4AdbEndpointObject@@QAEAAV0@ABV0@@Z
33 | 14 D 0000345A ??4AdbIOCompletion@@QAEAAV0@ABV0@@Z
34 | 15 E 000042E8 ??4AdbInterfaceObject@@QAEAAV0@ABV0@@Z
35 | 16 F 00003210 ??4AdbObjectHandle@@QAEAAV0@ABV0@@Z
36 | 17 10 00001294 ??_7AdbEndpointObject@@6B@
37 | 18 11 000012CC ??_7AdbIOCompletion@@6B@
38 | 19 12 00001370 ??_7AdbInterfaceObject@@6B@
39 | 20 13 00001278 ??_7AdbObjectHandle@@6B@
40 | 21 14 00005B88 ?AddRef@AdbObjectHandle@@UAEJXZ
41 | 22 15 000043F6 ?AsyncRead@AdbEndpointObject@@UAEPAXPAXKPAK0K@Z
42 | 23 16 0000441A ?AsyncWrite@AdbEndpointObject@@UAEPAXPAXKPAK0K@Z
43 | 24 17 000064BD ?CloseHandle@AdbObjectHandle@@UAE_NXZ
44 | 25 18 00006649 ?CreateHandle@AdbObjectHandle@@UAEPAXXZ
45 | 26 19 000043BF ?GetEndpointInformation@AdbEndpointObject@@UAE_NPAU_AdbEndpointInformation@@@Z
46 | 27 1A 000050B3 ?GetInterfaceName@AdbInterfaceObject@@UAE_NPAXPAK_N@Z
47 | 28 1B 0000331F ?GetParentInterfaceHandle@AdbEndpointObject@@QBEPAXXZ
48 | 29 1C 000033FD ?GetParentObjectHandle@AdbIOCompletion@@QBEPAXXZ
49 | 30 1D 00005035 ?GetUsbConfigurationDescriptor@AdbInterfaceObject@@UAE_NPAU_USB_CONFIGURATION_DESCRIPTOR@@@Z
50 | 31 1E 00004FF3 ?GetUsbDeviceDescriptor@AdbInterfaceObject@@UAE_NPAU_USB_DEVICE_DESCRIPTOR@@@Z
51 | 32 1F 00005074 ?GetUsbInterfaceDescriptor@AdbInterfaceObject@@UAE_NPAU_USB_INTERFACE_DESCRIPTOR@@@Z
52 | 33 20 000059CA ?IsCompleted@AdbIOCompletion@@UAE_NXZ
53 | 34 21 00005BC7 ?IsObjectOfType@AdbObjectHandle@@UBE_NW4AdbObjectType@@@Z
54 | 35 22 000031D7 ?IsOpened@AdbObjectHandle@@QBE_NXZ
55 | 36 23 000049D2 ?LastReferenceReleased@AdbObjectHandle@@MAEXXZ
56 | 37 24 0000657D ?Lookup@AdbObjectHandle@@SGPAV1@PAX@Z
57 | 38 25 00005B98 ?Release@AdbObjectHandle@@UAEJXZ
58 | 39 26 0000443E ?SyncRead@AdbEndpointObject@@UAE_NPAXKPAKK@Z
59 | 40 27 0000445F ?SyncWrite@AdbEndpointObject@@UAE_NPAXKPAKK@Z
60 | 41 28 000032FB ?Type@AdbEndpointObject@@SG?AW4AdbObjectType@@XZ
61 | 42 29 00003410 ?Type@AdbIOCompletion@@SG?AW4AdbObjectType@@XZ
62 | 43 2A 0000328F ?Type@AdbInterfaceObject@@SG?AW4AdbObjectType@@XZ
63 | 44 2B 000031C5 ?adb_handle@AdbObjectHandle@@QBEPAXXZ
64 | 45 2C 0000330D ?endpoint_id@AdbEndpointObject@@QBEEXZ
65 | 46 2D 00003316 ?endpoint_index@AdbEndpointObject@@QBEEXZ
66 | 47 2E 00003286 ?interface_name@AdbInterfaceObject@@QBEABV?$basic_string@GU?$char_traits@G@std@@V?$allocator@G@2@@std@@XZ
67 | 48 2F 000031CE ?object_type@AdbObjectHandle@@QBE?AW4AdbObjectType@@XZ
68 | 49 30 000033EB ?overlapped@AdbIOCompletion@@QAEPAU_OVERLAPPED@@XZ
69 | 50 31 00003304 ?parent_interface@AdbEndpointObject@@QBEPAVAdbInterfaceObject@@XZ
70 | 51 32 000033F4 ?parent_io_object@AdbIOCompletion@@QBEPAVAdbEndpointObject@@XZ
71 | 52 33 00003298 ?usb_config_descriptor@AdbInterfaceObject@@QBEPBU_USB_CONFIGURATION_DESCRIPTOR@@XZ
72 | 53 34 000033EB ?usb_device_descriptor@AdbInterfaceObject@@QBEPBU_USB_DEVICE_DESCRIPTOR@@XZ
73 | 54 35 000032A1 ?usb_interface_descriptor@AdbInterfaceObject@@QBEPBU_USB_INTERFACE_DESCRIPTOR@@XZ
74 | 55 36 00003642 AdbCloseHandle
75 | 56 37 000040D2 AdbCreateInterface
76 | 57 38 00003580 AdbCreateInterfaceByName
77 | 58 39 000034E6 AdbEnumInterfaces
78 | 59 3A 000039BA AdbGetDefaultBulkReadEndpointInformation
79 | 60 3B 000039D9 AdbGetDefaultBulkWriteEndpointInformation
80 | 61 3C 00003978 AdbGetEndpointInformation
81 | 62 3D 00003A81 AdbGetEndpointInterface
82 | 63 3E 00003831 AdbGetInterfaceName
83 | 64 3F 00003C23 AdbGetOvelappedIoResult
84 | 65 40 00003876 AdbGetSerialNumber
85 | 66 41 000038FA AdbGetUsbConfigurationDescriptor
86 | 67 42 000038BB AdbGetUsbDeviceDescriptor
87 | 68 43 00003939 AdbGetUsbInterfaceDescriptor
88 | 69 44 00003C68 AdbHasOvelappedIoComplated
89 | 70 45 000037B1 AdbNextInterface
90 | 71 46 00003A3D AdbOpenDefaultBulkReadEndpoint
91 | 72 47 00003A5F AdbOpenDefaultBulkWriteEndpoint
92 | 73 48 000039F8 AdbOpenEndpoint
93 | 74 49 00003ABE AdbQueryInformationEndpoint
94 | 75 4A 00003AFD AdbReadEndpointAsync
95 | 76 4B 00003B93 AdbReadEndpointSync
96 | 77 4C 000037FD AdbResetInterfaceEnum
97 | 78 4D 00003B48 AdbWriteEndpointAsync
98 | 79 4E 00003BDB AdbWriteEndpointSync
99 |
100 | Summary
101 |
102 | 3000 .data
103 | 2000 .reloc
104 | 1000 .rsrc
105 | 14000 .text
106 |
--------------------------------------------------------------------------------
/dll/AdbWinApi.dll.lib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/t-mat/my_adb/13d3f506189cc148c668ccb39ecc8a0d41be1bcb/dll/AdbWinApi.dll.lib
--------------------------------------------------------------------------------
/dll/build_exports.bat:
--------------------------------------------------------------------------------
1 | set VCBIN="%VS100COMNTOOLS%..\..\VC\bin"
2 | call %VCBIN%\vcvars32.bat
3 | %VCBIN%\dumpbin AdbWinApi.dll > AdbWinApi.dll.exports
4 |
--------------------------------------------------------------------------------
/dll/build_lib.bat:
--------------------------------------------------------------------------------
1 | set VCBIN="%VS100COMNTOOLS%..\..\VC\bin"
2 | call %VCBIN%\vcvars32.bat
3 | %VCBIN%\lib /def:AdbWinApi.dll.def /out:AdbWinApi.dll.lib
4 |
--------------------------------------------------------------------------------
/myadb.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 11.00
3 | # Visual C++ Express 2010
4 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "myadb", "myadb.vcxproj", "{B28B9A32-1ABE-4752-A4BB-4A2A4B48AB5E}"
5 | EndProject
6 | Global
7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
8 | Debug|Win32 = Debug|Win32
9 | Release|Win32 = Release|Win32
10 | EndGlobalSection
11 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
12 | {B28B9A32-1ABE-4752-A4BB-4A2A4B48AB5E}.Debug|Win32.ActiveCfg = Debug|Win32
13 | {B28B9A32-1ABE-4752-A4BB-4A2A4B48AB5E}.Debug|Win32.Build.0 = Debug|Win32
14 | {B28B9A32-1ABE-4752-A4BB-4A2A4B48AB5E}.Release|Win32.ActiveCfg = Release|Win32
15 | {B28B9A32-1ABE-4752-A4BB-4A2A4B48AB5E}.Release|Win32.Build.0 = Release|Win32
16 | EndGlobalSection
17 | GlobalSection(SolutionProperties) = preSolution
18 | HideSolutionNode = FALSE
19 | EndGlobalSection
20 | EndGlobal
21 |
--------------------------------------------------------------------------------
/myadb.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release
10 | Win32
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 | {B28B9A32-1ABE-4752-A4BB-4A2A4B48AB5E}
63 | Win32Proj
64 | myadb
65 |
66 |
67 |
68 | Application
69 | true
70 | MultiByte
71 |
72 |
73 | Application
74 | false
75 | true
76 | MultiByte
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 | true
90 |
91 |
92 | false
93 |
94 |
95 |
96 |
97 |
98 | Level3
99 | Disabled
100 | WIN32;_WINSOCKAPI_;_CRT_NONSTDC_NO_WARNINGS;_CRT_SECURE_NO_WARNINGS;HAVE_WIN32_PROC;HAVE_WIN32_IPC;HAVE_WINSOCK;ADB_HOST=1;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)
101 | $(ProjectDir)\src;$(ProjectDir)\src\zlib
102 |
103 |
104 | Console
105 | true
106 | $(ProjectDir)\dll;%(AdditionalLibraryDirectories)
107 |
108 |
109 |
110 |
111 | Level3
112 |
113 |
114 | MaxSpeed
115 | true
116 | true
117 | WIN32;_WINSOCKAPI_;_CRT_NONSTDC_NO_WARNINGS;_CRT_SECURE_NO_WARNINGS;HAVE_WIN32_PROC;HAVE_WIN32_IPC;HAVE_WINSOCK;ADB_HOST=1;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
118 | $(ProjectDir)\src;$(ProjectDir)\src\zlib
119 |
120 |
121 | Console
122 | true
123 | true
124 | true
125 | $(ProjectDir)\dll;%(AdditionalLibraryDirectories)
126 |
127 |
128 |
129 |
130 |
131 |
--------------------------------------------------------------------------------
/myadb.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 | libzipfile
20 |
21 |
22 | libzipfile
23 |
24 |
25 | zlib
26 |
27 |
28 | zlib
29 |
30 |
31 | zlib
32 |
33 |
34 | zlib
35 |
36 |
37 | zlib
38 |
39 |
40 | zlib
41 |
42 |
43 | zlib
44 |
45 |
46 | zlib
47 |
48 |
49 | zlib
50 |
51 |
52 | zlib
53 |
54 |
55 | zlib
56 |
57 |
58 | zlib
59 |
60 |
61 | zlib
62 |
63 |
64 | zlib
65 |
66 |
67 | zlib
68 |
69 |
70 |
71 |
72 |
73 | libzipfile
74 |
75 |
76 | zlib
77 |
78 |
79 | zlib
80 |
81 |
82 | zlib
83 |
84 |
85 | zlib
86 |
87 |
88 | zlib
89 |
90 |
91 | zlib
92 |
93 |
94 | zlib
95 |
96 |
97 | zlib
98 |
99 |
100 | zlib
101 |
102 |
103 | zlib
104 |
105 |
106 | zlib
107 |
108 |
109 |
110 |
111 | {be0aee09-2b61-4b82-a587-ba360acc4506}
112 |
113 |
114 | {79f7f4da-c8c8-4200-a239-c1131587ef42}
115 |
116 |
117 |
--------------------------------------------------------------------------------
/src/adb.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2007 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #ifndef __ADB_H
18 | #define __ADB_H
19 |
20 | #include
21 |
22 | #define MAX_PAYLOAD 4096
23 |
24 | #define A_SYNC 0x434e5953
25 | #define A_CNXN 0x4e584e43
26 | #define A_OPEN 0x4e45504f
27 | #define A_OKAY 0x59414b4f
28 | #define A_CLSE 0x45534c43
29 | #define A_WRTE 0x45545257
30 |
31 | #define A_VERSION 0x01000000 // ADB protocol version
32 |
33 | #define ADB_VERSION_MAJOR 1 // Used for help/version information
34 | #define ADB_VERSION_MINOR 0 // Used for help/version information
35 |
36 | #define ADB_SERVER_VERSION 26 // Increment this when we want to force users to start a new adb server
37 |
38 | typedef struct amessage amessage;
39 | typedef struct apacket apacket;
40 | typedef struct asocket asocket;
41 | typedef struct alistener alistener;
42 | typedef struct aservice aservice;
43 | typedef struct atransport atransport;
44 | typedef struct adisconnect adisconnect;
45 | typedef struct usb_handle usb_handle;
46 |
47 | struct amessage {
48 | unsigned command; /* command identifier constant */
49 | unsigned arg0; /* first argument */
50 | unsigned arg1; /* second argument */
51 | unsigned data_length; /* length of payload (0 is allowed) */
52 | unsigned data_check; /* checksum of data payload */
53 | unsigned magic; /* command ^ 0xffffffff */
54 | };
55 |
56 | struct apacket
57 | {
58 | apacket *next;
59 |
60 | unsigned len;
61 | unsigned char *ptr;
62 |
63 | amessage msg;
64 | unsigned char data[MAX_PAYLOAD];
65 | };
66 |
67 | /* An asocket represents one half of a connection between a local and
68 | ** remote entity. A local asocket is bound to a file descriptor. A
69 | ** remote asocket is bound to the protocol engine.
70 | */
71 | struct asocket {
72 | /* chain pointers for the local/remote list of
73 | ** asockets that this asocket lives in
74 | */
75 | asocket *next;
76 | asocket *prev;
77 |
78 | /* the unique identifier for this asocket
79 | */
80 | unsigned id;
81 |
82 | /* flag: set when the socket's peer has closed
83 | ** but packets are still queued for delivery
84 | */
85 | int closing;
86 |
87 | /* the asocket we are connected to
88 | */
89 |
90 | asocket *peer;
91 |
92 | /* For local asockets, the fde is used to bind
93 | ** us to our fd event system. For remote asockets
94 | ** these fields are not used.
95 | */
96 | fdevent fde;
97 | int fd;
98 |
99 | /* queue of apackets waiting to be written
100 | */
101 | apacket *pkt_first;
102 | apacket *pkt_last;
103 |
104 | /* enqueue is called by our peer when it has data
105 | ** for us. It should return 0 if we can accept more
106 | ** data or 1 if not. If we return 1, we must call
107 | ** peer->ready() when we once again are ready to
108 | ** receive data.
109 | */
110 | int (*enqueue)(asocket *s, apacket *pkt);
111 |
112 | /* ready is called by the peer when it is ready for
113 | ** us to send data via enqueue again
114 | */
115 | void (*ready)(asocket *s);
116 |
117 | /* close is called by the peer when it has gone away.
118 | ** we are not allowed to make any further calls on the
119 | ** peer once our close method is called.
120 | */
121 | void (*close)(asocket *s);
122 |
123 | /* socket-type-specific extradata */
124 | void *extra;
125 |
126 | /* A socket is bound to atransport */
127 | atransport *transport;
128 | };
129 |
130 |
131 | /* the adisconnect structure is used to record a callback that
132 | ** will be called whenever a transport is disconnected (e.g. by the user)
133 | ** this should be used to cleanup objects that depend on the
134 | ** transport (e.g. remote sockets, listeners, etc...)
135 | */
136 | struct adisconnect
137 | {
138 | void (*func)(void* opaque, atransport* t);
139 | void* opaque;
140 | adisconnect* next;
141 | adisconnect* prev;
142 | };
143 |
144 |
145 | /* a transport object models the connection to a remote device or emulator
146 | ** there is one transport per connected device/emulator. a "local transport"
147 | ** connects through TCP (for the emulator), while a "usb transport" through
148 | ** USB (for real devices)
149 | **
150 | ** note that kTransportHost doesn't really correspond to a real transport
151 | ** object, it's a special value used to indicate that a client wants to
152 | ** connect to a service implemented within the ADB server itself.
153 | */
154 | typedef enum transport_type {
155 | kTransportUsb,
156 | kTransportLocal,
157 | kTransportAny,
158 | kTransportHost,
159 | } transport_type;
160 |
161 | struct atransport
162 | {
163 | atransport *next;
164 | atransport *prev;
165 |
166 | int (*read_from_remote)(apacket *p, atransport *t);
167 | int (*write_to_remote)(apacket *p, atransport *t);
168 | void (*close)(atransport *t);
169 | void (*kick)(atransport *t);
170 |
171 | int fd;
172 | int transport_socket;
173 | fdevent transport_fde;
174 | int ref_count;
175 | unsigned sync_token;
176 | int connection_state;
177 | transport_type type;
178 |
179 | /* usb handle or socket fd as needed */
180 | usb_handle *usb;
181 | int sfd;
182 |
183 | /* used to identify transports for clients */
184 | char *serial;
185 | char *product;
186 | int adb_port; // Use for emulators (local transport)
187 |
188 | /* a list of adisconnect callbacks called when the transport is kicked */
189 | int kicked;
190 | adisconnect disconnects;
191 | };
192 |
193 |
194 | /* A listener is an entity which binds to a local port
195 | ** and, upon receiving a connection on that port, creates
196 | ** an asocket to connect the new local connection to a
197 | ** specific remote service.
198 | **
199 | ** TODO: some listeners read from the new connection to
200 | ** determine what exact service to connect to on the far
201 | ** side.
202 | */
203 | struct alistener
204 | {
205 | alistener *next;
206 | alistener *prev;
207 |
208 | fdevent fde;
209 | int fd;
210 |
211 | const char *local_name;
212 | const char *connect_to;
213 | atransport *transport;
214 | adisconnect disconnect;
215 | };
216 |
217 |
218 | void print_packet(const char *label, apacket *p);
219 |
220 | asocket *find_local_socket(unsigned id);
221 | void install_local_socket(asocket *s);
222 | void remove_socket(asocket *s);
223 | void close_all_sockets(atransport *t);
224 |
225 | #define LOCAL_CLIENT_PREFIX "emulator-"
226 |
227 | asocket *create_local_socket(int fd);
228 | asocket *create_local_service_socket(const char *destination);
229 |
230 | asocket *create_remote_socket(unsigned id, atransport *t);
231 | void connect_to_remote(asocket *s, const char *destination);
232 | void connect_to_smartsocket(asocket *s);
233 |
234 | void fatal(const char *fmt, ...);
235 | void fatal_errno(const char *fmt, ...);
236 |
237 | void handle_packet(apacket *p, atransport *t);
238 | void send_packet(apacket *p, atransport *t);
239 |
240 | void get_my_path(char *s, size_t maxLen);
241 | int launch_server(int server_port);
242 | int adb_main(int is_daemon, int server_port);
243 |
244 |
245 | /* transports are ref-counted
246 | ** get_device_transport does an acquire on your behalf before returning
247 | */
248 | void init_transport_registration(void);
249 | int list_transports(char *buf, size_t bufsize);
250 | void update_transports(void);
251 |
252 | asocket* create_device_tracker(void);
253 |
254 | /* Obtain a transport from the available transports.
255 | ** If state is != CS_ANY, only transports in that state are considered.
256 | ** If serial is non-NULL then only the device with that serial will be chosen.
257 | ** If no suitable transport is found, error is set.
258 | */
259 | atransport *acquire_one_transport(int state, transport_type ttype, const char* serial, char **error_out);
260 | void add_transport_disconnect( atransport* t, adisconnect* dis );
261 | void remove_transport_disconnect( atransport* t, adisconnect* dis );
262 | void run_transport_disconnects( atransport* t );
263 | void kick_transport( atransport* t );
264 |
265 | /* initialize a transport object's func pointers and state */
266 | #if ADB_HOST
267 | int get_available_local_transport_index();
268 | #endif
269 | int init_socket_transport(atransport *t, int s, int port, int local);
270 | void init_usb_transport(atransport *t, usb_handle *usb, int state);
271 |
272 | /* for MacOS X cleanup */
273 | void close_usb_devices();
274 |
275 | /* cause new transports to be init'd and added to the list */
276 | void register_socket_transport(int s, const char *serial, int port, int local);
277 |
278 | /* these should only be used for the "adb disconnect" command */
279 | void unregister_transport(atransport *t);
280 | void unregister_all_tcp_transports();
281 |
282 | void register_usb_transport(usb_handle *h, const char *serial, unsigned writeable);
283 |
284 | /* this should only be used for transports with connection_state == CS_NOPERM */
285 | void unregister_usb_transport(usb_handle *usb);
286 |
287 | atransport *find_transport(const char *serial);
288 | #if ADB_HOST
289 | atransport* find_emulator_transport_by_adb_port(int adb_port);
290 | #endif
291 |
292 | int service_to_fd(const char *name);
293 | #if ADB_HOST
294 | asocket *host_service_to_socket(const char* name, const char *serial);
295 | #endif
296 |
297 | #if !ADB_HOST
298 | int init_jdwp(void);
299 | asocket* create_jdwp_service_socket();
300 | asocket* create_jdwp_tracker_service_socket();
301 | int create_jdwp_connection_fd(int jdwp_pid);
302 | #endif
303 |
304 | #if !ADB_HOST
305 | void framebuffer_service(int fd, void *cookie);
306 | void log_service(int fd, void *cookie);
307 | void remount_service(int fd, void *cookie);
308 | char * get_log_file_path(const char * log_name);
309 | #endif
310 |
311 | /* packet allocator */
312 | apacket *get_apacket(void);
313 | void put_apacket(apacket *p);
314 |
315 | int check_header(apacket *p);
316 | int check_data(apacket *p);
317 |
318 | /* convenience wrappers around read/write that will retry on
319 | ** EINTR and/or short read/write. Returns 0 on success, -1
320 | ** on error or EOF.
321 | */
322 | int readx(int fd, void *ptr, size_t len);
323 | int writex(int fd, const void *ptr, size_t len);
324 |
325 | /* define ADB_TRACE to 1 to enable tracing support, or 0 to disable it */
326 |
327 | #define ADB_TRACE 1
328 |
329 | /* IMPORTANT: if you change the following list, don't
330 | * forget to update the corresponding 'tags' table in
331 | * the adb_trace_init() function implemented in adb.c
332 | */
333 | typedef enum {
334 | TRACE_ADB = 0,
335 | TRACE_SOCKETS,
336 | TRACE_PACKETS,
337 | TRACE_TRANSPORT,
338 | TRACE_RWX,
339 | TRACE_USB,
340 | TRACE_SYNC,
341 | TRACE_SYSDEPS,
342 | TRACE_JDWP,
343 | } AdbTrace;
344 |
345 | #if ADB_TRACE
346 |
347 | int adb_trace_mask;
348 |
349 | void adb_trace_init(void);
350 |
351 | # define ADB_TRACING ((adb_trace_mask & (1 << TRACE_TAG)) != 0)
352 |
353 | /* you must define TRACE_TAG before using this macro */
354 | #define D(...) \
355 | do { \
356 | if (ADB_TRACING) \
357 | fprintf(stderr, __VA_ARGS__ ); \
358 | } while (0)
359 | #else
360 | # define D(...) ((void)0)
361 | # define ADB_TRACING 0
362 | #endif
363 |
364 |
365 | #if !TRACE_PACKETS
366 | #define print_packet(tag,p) do {} while (0)
367 | #endif
368 |
369 | #define DEFAULT_ADB_PORT 5037
370 | #define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555
371 |
372 | #define ADB_CLASS 0xff
373 | #define ADB_SUBCLASS 0x42
374 | #define ADB_PROTOCOL 0x1
375 |
376 |
377 | void local_init(int port);
378 | int local_connect(int port);
379 | int local_connect_arbitrary_ports(int console_port, int adb_port);
380 |
381 | /* usb host/client interface */
382 | void usb_init();
383 | void usb_cleanup();
384 | int usb_write(usb_handle *h, const void *data, int len);
385 | int usb_read(usb_handle *h, void *data, int len);
386 | int usb_close(usb_handle *h);
387 | void usb_kick(usb_handle *h);
388 |
389 | /* used for USB device detection */
390 | #if ADB_HOST
391 | int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol);
392 | #endif
393 |
394 | unsigned host_to_le32(unsigned n);
395 | int adb_commandline(int argc, char **argv);
396 |
397 | int connection_state(atransport *t);
398 |
399 | #define CS_ANY -1
400 | #define CS_OFFLINE 0
401 | #define CS_BOOTLOADER 1
402 | #define CS_DEVICE 2
403 | #define CS_HOST 3
404 | #define CS_RECOVERY 4
405 | #define CS_NOPERM 5 /* Insufficient permissions to communicate with the device */
406 |
407 | extern int HOST;
408 |
409 | #define CHUNK_SIZE (64*1024)
410 |
411 | int sendfailmsg(int fd, const char *reason);
412 | int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s);
413 |
414 | #endif
415 |
--------------------------------------------------------------------------------
/src/adb_client.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 |
11 | #include "sysdeps.h"
12 |
13 | #define TRACE_TAG TRACE_ADB
14 | #include "adb_client.h"
15 |
16 | static transport_type __adb_transport = kTransportAny;
17 | static const char* __adb_serial = NULL;
18 |
19 | static int __adb_server_port = DEFAULT_ADB_PORT;
20 |
21 | void adb_set_transport(transport_type type, const char* serial)
22 | {
23 | __adb_transport = type;
24 | __adb_serial = serial;
25 | }
26 |
27 | void adb_set_tcp_specifics(int server_port)
28 | {
29 | __adb_server_port = server_port;
30 | }
31 |
32 | int adb_get_emulator_console_port(void)
33 | {
34 | const char* serial = __adb_serial;
35 | int port;
36 |
37 | if (serial == NULL) {
38 | /* if no specific device was specified, we need to look at */
39 | /* the list of connected devices, and extract an emulator */
40 | /* name from it. two emulators is an error */
41 | char* tmp = adb_query("host:devices");
42 | char* p = tmp;
43 | if(!tmp) {
44 | printf("no emulator connected\n");
45 | return -1;
46 | }
47 | while (*p) {
48 | char* q = strchr(p, '\n');
49 | if (q != NULL)
50 | *q++ = 0;
51 | else
52 | q = p + strlen(p);
53 |
54 | if (!memcmp(p, LOCAL_CLIENT_PREFIX, sizeof(LOCAL_CLIENT_PREFIX)-1)) {
55 | if (serial != NULL) { /* more than one emulator listed */
56 | free(tmp);
57 | return -2;
58 | }
59 | serial = p;
60 | }
61 |
62 | p = q;
63 | }
64 | free(tmp);
65 |
66 | if (serial == NULL)
67 | return -1; /* no emulator found */
68 | }
69 | else {
70 | if (memcmp(serial, LOCAL_CLIENT_PREFIX, sizeof(LOCAL_CLIENT_PREFIX)-1) != 0)
71 | return -1; /* not an emulator */
72 | }
73 |
74 | serial += sizeof(LOCAL_CLIENT_PREFIX)-1;
75 | port = strtol(serial, NULL, 10);
76 | return port;
77 | }
78 |
79 | static char __adb_error[256] = { 0 };
80 |
81 | const char *adb_error(void)
82 | {
83 | return __adb_error;
84 | }
85 |
86 | static int switch_socket_transport(int fd)
87 | {
88 | char service[64];
89 | char tmp[5];
90 | int len;
91 |
92 | if (__adb_serial)
93 | snprintf(service, sizeof service, "host:transport:%s", __adb_serial);
94 | else {
95 | char* transport_type = "???";
96 |
97 | switch (__adb_transport) {
98 | case kTransportUsb:
99 | transport_type = "transport-usb";
100 | break;
101 | case kTransportLocal:
102 | transport_type = "transport-local";
103 | break;
104 | case kTransportAny:
105 | transport_type = "transport-any";
106 | break;
107 | case kTransportHost:
108 | // no switch necessary
109 | return 0;
110 | break;
111 | }
112 |
113 | snprintf(service, sizeof service, "host:%s", transport_type);
114 | }
115 | len = strlen(service);
116 | snprintf(tmp, sizeof tmp, "%04x", len);
117 |
118 | if(writex(fd, tmp, 4) || writex(fd, service, len)) {
119 | strcpy(__adb_error, "write failure during connection");
120 | adb_close(fd);
121 | return -1;
122 | }
123 | D("Switch transport in progress\n");
124 |
125 | if(adb_status(fd)) {
126 | adb_close(fd);
127 | D("Switch transport failed\n");
128 | return -1;
129 | }
130 | D("Switch transport success\n");
131 | return 0;
132 | }
133 |
134 | int adb_status(int fd)
135 | {
136 | unsigned char buf[5];
137 | unsigned len;
138 |
139 | if(readx(fd, buf, 4)) {
140 | strcpy(__adb_error, "protocol fault (no status)");
141 | return -1;
142 | }
143 |
144 | if(!memcmp(buf, "OKAY", 4)) {
145 | return 0;
146 | }
147 |
148 | if(memcmp(buf, "FAIL", 4)) {
149 | sprintf(__adb_error,
150 | "protocol fault (status %02x %02x %02x %02x?!)",
151 | buf[0], buf[1], buf[2], buf[3]);
152 | return -1;
153 | }
154 |
155 | if(readx(fd, buf, 4)) {
156 | strcpy(__adb_error, "protocol fault (status len)");
157 | return -1;
158 | }
159 | buf[4] = 0;
160 | len = strtoul((char*)buf, 0, 16);
161 | if(len > 255) len = 255;
162 | if(readx(fd, __adb_error, len)) {
163 | strcpy(__adb_error, "protocol fault (status read)");
164 | return -1;
165 | }
166 | __adb_error[len] = 0;
167 | return -1;
168 | }
169 |
170 | int _adb_connect(const char *service)
171 | {
172 | char tmp[5];
173 | int len;
174 | int fd;
175 |
176 | D("_adb_connect: %s\n", service);
177 | len = strlen(service);
178 | if((len < 1) || (len > 1024)) {
179 | strcpy(__adb_error, "service name too long");
180 | return -1;
181 | }
182 | snprintf(tmp, sizeof tmp, "%04x", len);
183 |
184 | fd = socket_loopback_client(__adb_server_port, SOCK_STREAM);
185 | if(fd < 0) {
186 | strcpy(__adb_error, "cannot connect to daemon");
187 | return -2;
188 | }
189 |
190 | if (memcmp(service,"host",4) != 0 && switch_socket_transport(fd)) {
191 | return -1;
192 | }
193 |
194 | if(writex(fd, tmp, 4) || writex(fd, service, len)) {
195 | strcpy(__adb_error, "write failure during connection");
196 | adb_close(fd);
197 | return -1;
198 | }
199 |
200 | if(adb_status(fd)) {
201 | adb_close(fd);
202 | return -1;
203 | }
204 |
205 | return fd;
206 | }
207 |
208 | int adb_connect(const char *service)
209 | {
210 | // first query the adb server's version
211 | int fd = _adb_connect("host:version");
212 |
213 | if(fd == -2) {
214 | fprintf(stdout,"* daemon not running. starting it now on port %d *\n",
215 | __adb_server_port);
216 | start_server:
217 | if(launch_server(__adb_server_port)) {
218 | fprintf(stderr,"* failed to start daemon *\n");
219 | return -1;
220 | } else {
221 | fprintf(stdout,"* daemon started successfully *\n");
222 | }
223 | /* give the server some time to start properly and detect devices */
224 | adb_sleep_ms(3000);
225 | // fall through to _adb_connect
226 | } else {
227 | // if server was running, check its version to make sure it is not out of date
228 | char buf[100];
229 | int n;
230 | int version = ADB_SERVER_VERSION - 1;
231 |
232 | // if we have a file descriptor, then parse version result
233 | if(fd >= 0) {
234 | if(readx(fd, buf, 4)) goto error;
235 |
236 | buf[4] = 0;
237 | n = strtoul(buf, 0, 16);
238 | if(n > (int)sizeof(buf)) goto error;
239 | if(readx(fd, buf, n)) goto error;
240 | adb_close(fd);
241 |
242 | if (sscanf(buf, "%04x", &version) != 1) goto error;
243 | } else {
244 | // if fd is -1, then check for "unknown host service",
245 | // which would indicate a version of adb that does not support the version command
246 | if (strcmp(__adb_error, "unknown host service") != 0)
247 | return fd;
248 | }
249 |
250 | if(version != ADB_SERVER_VERSION) {
251 | printf("adb server is out of date. killing...\n");
252 | fd = _adb_connect("host:kill");
253 | adb_close(fd);
254 |
255 | /* XXX can we better detect its death? */
256 | adb_sleep_ms(2000);
257 | goto start_server;
258 | }
259 | }
260 |
261 | // if the command is start-server, we are done.
262 | if (!strcmp(service, "host:start-server"))
263 | return 0;
264 |
265 | fd = _adb_connect(service);
266 | if(fd == -2) {
267 | fprintf(stderr,"** daemon still not running");
268 | }
269 |
270 | return fd;
271 | error:
272 | adb_close(fd);
273 | return -1;
274 | }
275 |
276 |
277 | int adb_command(const char *service)
278 | {
279 | int fd = adb_connect(service);
280 | if(fd < 0) {
281 | return -1;
282 | }
283 |
284 | if(adb_status(fd)) {
285 | adb_close(fd);
286 | return -1;
287 | }
288 |
289 | return 0;
290 | }
291 |
292 | char *adb_query(const char *service)
293 | {
294 | char buf[5];
295 | unsigned n;
296 | char *tmp;
297 | int fd; //+
298 |
299 | D("adb_query: %s\n", service);
300 | //- int fd = adb_connect(service);
301 | fd = adb_connect(service);
302 | if(fd < 0) {
303 | fprintf(stderr,"error: %s\n", __adb_error);
304 | return 0;
305 | }
306 |
307 | if(readx(fd, buf, 4)) goto oops;
308 |
309 | buf[4] = 0;
310 | n = strtoul(buf, 0, 16);
311 | if(n > 1024) goto oops;
312 |
313 | tmp = malloc(n + 1);
314 | if(tmp == 0) goto oops;
315 |
316 | if(readx(fd, tmp, n) == 0) {
317 | tmp[n] = 0;
318 | adb_close(fd);
319 | return tmp;
320 | }
321 | free(tmp);
322 |
323 | oops:
324 | adb_close(fd);
325 | return 0;
326 | }
327 |
--------------------------------------------------------------------------------
/src/adb_client.h:
--------------------------------------------------------------------------------
1 | #ifndef _ADB_CLIENT_H_
2 | #define _ADB_CLIENT_H_
3 |
4 | #include "adb.h"
5 |
6 | /* connect to adb, connect to the named service, and return
7 | ** a valid fd for interacting with that service upon success
8 | ** or a negative number on failure
9 | */
10 | int adb_connect(const char *service);
11 | int _adb_connect(const char *service);
12 |
13 | /* connect to adb, connect to the named service, return 0 if
14 | ** the connection succeeded AND the service returned OKAY
15 | */
16 | int adb_command(const char *service);
17 |
18 | /* connect to adb, connect to the named service, return
19 | ** a malloc'd string of its response upon success or NULL
20 | ** on failure.
21 | */
22 | char *adb_query(const char *service);
23 |
24 | /* Set the preferred transport to connect to.
25 | */
26 | void adb_set_transport(transport_type type, const char* serial);
27 |
28 | /* Set TCP specifics of the transport to use
29 | */
30 | void adb_set_tcp_specifics(int server_port);
31 |
32 | /* Return the console port of the currently connected emulator (if any)
33 | * of -1 if there is no emulator, and -2 if there is more than one.
34 | * assumes adb_set_transport() was alled previously...
35 | */
36 | int adb_get_emulator_console_port(void);
37 |
38 | /* send commands to the current emulator instance. will fail if there
39 | * is zero, or more than one emulator connected (or if you use -s
40 | * with a that does not designate an emulator)
41 | */
42 | int adb_send_emulator_command(int argc, char** argv);
43 |
44 | /* return verbose error string from last operation */
45 | const char *adb_error(void);
46 |
47 | /* read a standard adb status response (OKAY|FAIL) and
48 | ** return 0 in the event of OKAY, -1 in the event of FAIL
49 | ** or protocol error
50 | */
51 | int adb_status(int fd);
52 |
53 | #endif
54 |
--------------------------------------------------------------------------------
/src/console.c:
--------------------------------------------------------------------------------
1 | #include "sysdeps.h"
2 | #include "adb.h"
3 | #include "adb_client.h"
4 | #include
5 |
6 | static int connect_to_console(void)
7 | {
8 | int fd, port;
9 |
10 | port = adb_get_emulator_console_port();
11 | if (port < 0) {
12 | if (port == -2)
13 | fprintf(stderr, "error: more than one emulator detected. use -s option\n");
14 | else
15 | fprintf(stderr, "error: no emulator detected\n");
16 | return -1;
17 | }
18 | fd = socket_loopback_client( port, SOCK_STREAM );
19 | if (fd < 0) {
20 | fprintf(stderr, "error: could not connect to TCP port %d\n", port);
21 | return -1;
22 | }
23 | return fd;
24 | }
25 |
26 |
27 | int adb_send_emulator_command(int argc, char** argv)
28 | {
29 | int fd, nn;
30 |
31 | fd = connect_to_console();
32 | if (fd < 0)
33 | return 1;
34 |
35 | #define QUIT "quit\n"
36 |
37 | for (nn = 1; nn < argc; nn++) {
38 | adb_write( fd, argv[nn], strlen(argv[nn]) );
39 | adb_write( fd, (nn == argc-1) ? "\n" : " ", 1 );
40 | }
41 | adb_write( fd, QUIT, sizeof(QUIT)-1 );
42 | adb_close(fd);
43 |
44 | return 0;
45 | }
46 |
--------------------------------------------------------------------------------
/src/dirent.h:
--------------------------------------------------------------------------------
1 | /*****************************************************************************
2 | * dirent.h - dirent API for Microsoft Visual Studio
3 | *
4 | * Copyright (C) 2006 Toni Ronkko
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining
7 | * a copy of this software and associated documentation files (the
8 | * ``Software''), to deal in the Software without restriction, including
9 | * without limitation the rights to use, copy, modify, merge, publish,
10 | * distribute, sublicense, and/or sell copies of the Software, and to
11 | * permit persons to whom the Software is furnished to do so, subject to
12 | * the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be included
15 | * in all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 | * IN NO EVENT SHALL TONI RONKKO BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 | * OTHER DEALINGS IN THE SOFTWARE.
24 | *
25 | * Mar 15, 2011, Toni Ronkko
26 | * Defined FILE_ATTRIBUTE_DEVICE for MSVC 6.0.
27 | *
28 | * Aug 11, 2010, Toni Ronkko
29 | * Added d_type and d_namlen fields to dirent structure. The former is
30 | * especially useful for determining whether directory entry represents a
31 | * file or a directory. For more information, see
32 | * http://www.delorie.com/gnu/docs/glibc/libc_270.html
33 | *
34 | * Aug 11, 2010, Toni Ronkko
35 | * Improved conformance to the standards. For example, errno is now set
36 | * properly on failure and assert() is never used. Thanks to Peter Brockam
37 | * for suggestions.
38 | *
39 | * Aug 11, 2010, Toni Ronkko
40 | * Fixed a bug in rewinddir(): when using relative directory names, change
41 | * of working directory no longer causes rewinddir() to fail.
42 | *
43 | * Dec 15, 2009, John Cunningham
44 | * Added rewinddir member function
45 | *
46 | * Jan 18, 2008, Toni Ronkko
47 | * Using FindFirstFileA and WIN32_FIND_DATAA to avoid converting string
48 | * between multi-byte and unicode representations. This makes the
49 | * code simpler and also allows the code to be compiled under MingW. Thanks
50 | * to Azriel Fasten for the suggestion.
51 | *
52 | * Mar 4, 2007, Toni Ronkko
53 | * Bug fix: due to the strncpy_s() function this file only compiled in
54 | * Visual Studio 2005. Using the new string functions only when the
55 | * compiler version allows.
56 | *
57 | * Nov 2, 2006, Toni Ronkko
58 | * Major update: removed support for Watcom C, MS-DOS and Turbo C to
59 | * simplify the file, updated the code to compile cleanly on Visual
60 | * Studio 2005 with both unicode and multi-byte character strings,
61 | * removed rewinddir() as it had a bug.
62 | *
63 | * Aug 20, 2006, Toni Ronkko
64 | * Removed all remarks about MSVC 1.0, which is antiqued now. Simplified
65 | * comments by removing SGML tags.
66 | *
67 | * May 14 2002, Toni Ronkko
68 | * Embedded the function definitions directly to the header so that no
69 | * source modules need to be included in the Visual Studio project. Removed
70 | * all the dependencies to other projects so that this very header can be
71 | * used independently.
72 | *
73 | * May 28 1998, Toni Ronkko
74 | * First version.
75 | *****************************************************************************/
76 | #ifndef DIRENT_H
77 | #define DIRENT_H
78 |
79 | #define WIN32_LEAN_AND_MEAN
80 | #include
81 | #include
82 | #include
83 | #include
84 | #include
85 | #include
86 |
87 | /* Entries missing from MSVC 6.0 */
88 | #if !defined(FILE_ATTRIBUTE_DEVICE)
89 | # define FILE_ATTRIBUTE_DEVICE 0x40
90 | #endif
91 |
92 | /* File type and permission flags for stat() */
93 | #if defined(_MSC_VER) && !defined(S_IREAD)
94 | # define S_IFMT _S_IFMT /* file type mask */
95 | # define S_IFDIR _S_IFDIR /* directory */
96 | # define S_IFCHR _S_IFCHR /* character device */
97 | # define S_IFFIFO _S_IFFIFO /* pipe */
98 | # define S_IFREG _S_IFREG /* regular file */
99 | # define S_IREAD _S_IREAD /* read permission */
100 | # define S_IWRITE _S_IWRITE /* write permission */
101 | # define S_IEXEC _S_IEXEC /* execute permission */
102 | #endif
103 | #define S_IFBLK 0 /* block device */
104 | #define S_IFLNK 0 /* link */
105 | #define S_IFSOCK 0 /* socket */
106 |
107 | #if defined(_MSC_VER)
108 | # define S_IRUSR S_IREAD /* read, user */
109 | # define S_IWUSR S_IWRITE /* write, user */
110 | # define S_IXUSR 0 /* execute, user */
111 | # define S_IRGRP 0 /* read, group */
112 | # define S_IWGRP 0 /* write, group */
113 | # define S_IXGRP 0 /* execute, group */
114 | # define S_IROTH 0 /* read, others */
115 | # define S_IWOTH 0 /* write, others */
116 | # define S_IXOTH 0 /* execute, others */
117 | #endif
118 |
119 | /* Indicates that d_type field is available in dirent structure */
120 | #define _DIRENT_HAVE_D_TYPE
121 |
122 | /* File type flags for d_type */
123 | #define DT_UNKNOWN 0
124 | #define DT_REG S_IFREG
125 | #define DT_DIR S_IFDIR
126 | #define DT_FIFO S_IFFIFO
127 | #define DT_SOCK S_IFSOCK
128 | #define DT_CHR S_IFCHR
129 | #define DT_BLK S_IFBLK
130 |
131 | /* Macros for converting between st_mode and d_type */
132 | #define IFTODT(mode) ((mode) & S_IFMT)
133 | #define DTTOIF(type) (type)
134 |
135 | /*
136 | * File type macros. Note that block devices, sockets and links cannot be
137 | * distinguished on Windows and the macros S_ISBLK, S_ISSOCK and S_ISLNK are
138 | * only defined for compatibility. These macros should always return false
139 | * on Windows.
140 | */
141 | #define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFFIFO)
142 | #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
143 | #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
144 | //#define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
145 | #define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
146 | #define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)
147 | #define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)
148 |
149 | #ifdef __cplusplus
150 | extern "C" {
151 | #endif
152 |
153 |
154 | typedef struct dirent
155 | {
156 | char d_name[MAX_PATH + 1]; /* File name */
157 | size_t d_namlen; /* Length of name without \0 */
158 | int d_type; /* File type */
159 | } dirent;
160 |
161 |
162 | typedef struct DIR
163 | {
164 | dirent curentry; /* Current directory entry */
165 | WIN32_FIND_DATAA find_data; /* Private file data */
166 | int cached; /* True if data is valid */
167 | HANDLE search_handle; /* Win32 search handle */
168 | char patt[MAX_PATH + 3]; /* Initial directory name */
169 | } DIR;
170 |
171 |
172 | /* Forward declarations */
173 | static DIR *opendir(const char *dirname);
174 | static struct dirent *readdir(DIR *dirp);
175 | static int closedir(DIR *dirp);
176 | static void rewinddir(DIR* dirp);
177 |
178 |
179 | /* Use the new safe string functions introduced in Visual Studio 2005 */
180 | #if defined(_MSC_VER) && _MSC_VER >= 1400
181 | # define DIRENT_STRNCPY(dest,src,size) strncpy_s((dest),(size),(src),_TRUNCATE)
182 | #else
183 | # define DIRENT_STRNCPY(dest,src,size) strncpy((dest),(src),(size))
184 | #endif
185 |
186 | /* Set errno variable */
187 | #if defined(_MSC_VER)
188 | #define DIRENT_SET_ERRNO(x) _set_errno (x)
189 | #else
190 | #define DIRENT_SET_ERRNO(x) (errno = (x))
191 | #endif
192 |
193 |
194 | /*****************************************************************************
195 | * Open directory stream DIRNAME for read and return a pointer to the
196 | * internal working area that is used to retrieve individual directory
197 | * entries.
198 | */
199 | static DIR *opendir(const char *dirname)
200 | {
201 | DIR *dirp;
202 |
203 | /* ensure that the resulting search pattern will be a valid file name */
204 | if (dirname == NULL) {
205 | DIRENT_SET_ERRNO (ENOENT);
206 | return NULL;
207 | }
208 | if (strlen (dirname) + 3 >= MAX_PATH) {
209 | DIRENT_SET_ERRNO (ENAMETOOLONG);
210 | return NULL;
211 | }
212 |
213 | /* construct new DIR structure */
214 | dirp = (DIR*) malloc (sizeof (struct DIR));
215 | if (dirp != NULL) {
216 | int error;
217 |
218 | /*
219 | * Convert relative directory name to an absolute one. This
220 | * allows rewinddir() to function correctly when the current working
221 | * directory is changed between opendir() and rewinddir().
222 | */
223 | if (GetFullPathNameA (dirname, MAX_PATH, dirp->patt, NULL)) {
224 | char *p;
225 |
226 | /* append the search pattern "\\*\0" to the directory name */
227 | p = strchr (dirp->patt, '\0');
228 | if (dirp->patt < p && *(p-1) != '\\' && *(p-1) != ':') {
229 | *p++ = '\\';
230 | }
231 | *p++ = '*';
232 | *p = '\0';
233 |
234 | /* open directory stream and retrieve the first entry */
235 | dirp->search_handle = FindFirstFileA (dirp->patt, &dirp->find_data);
236 | if (dirp->search_handle != INVALID_HANDLE_VALUE) {
237 | /* a directory entry is now waiting in memory */
238 | dirp->cached = 1;
239 | error = 0;
240 | } else {
241 | /* search pattern is not a directory name? */
242 | DIRENT_SET_ERRNO (ENOENT);
243 | error = 1;
244 | }
245 | } else {
246 | /* buffer too small */
247 | DIRENT_SET_ERRNO (ENOMEM);
248 | error = 1;
249 | }
250 |
251 | if (error) {
252 | free (dirp);
253 | dirp = NULL;
254 | }
255 | }
256 |
257 | return dirp;
258 | }
259 |
260 |
261 | /*****************************************************************************
262 | * Read a directory entry, and return a pointer to a dirent structure
263 | * containing the name of the entry in d_name field. Individual directory
264 | * entries returned by this very function include regular files,
265 | * sub-directories, pseudo-directories "." and "..", but also volume labels,
266 | * hidden files and system files may be returned.
267 | */
268 | static struct dirent *readdir(DIR *dirp)
269 | {
270 | DWORD attr;
271 | if (dirp == NULL) {
272 | /* directory stream did not open */
273 | DIRENT_SET_ERRNO (EBADF);
274 | return NULL;
275 | }
276 |
277 | /* get next directory entry */
278 | if (dirp->cached != 0) {
279 | /* a valid directory entry already in memory */
280 | dirp->cached = 0;
281 | } else {
282 | /* get the next directory entry from stream */
283 | if (dirp->search_handle == INVALID_HANDLE_VALUE) {
284 | return NULL;
285 | }
286 | if (FindNextFileA (dirp->search_handle, &dirp->find_data) == FALSE) {
287 | /* the very last entry has been processed or an error occured */
288 | FindClose (dirp->search_handle);
289 | dirp->search_handle = INVALID_HANDLE_VALUE;
290 | return NULL;
291 | }
292 | }
293 |
294 | /* copy as a multibyte character string */
295 | DIRENT_STRNCPY ( dirp->curentry.d_name,
296 | dirp->find_data.cFileName,
297 | sizeof(dirp->curentry.d_name) );
298 | dirp->curentry.d_name[MAX_PATH] = '\0';
299 |
300 | /* compute the length of name */
301 | dirp->curentry.d_namlen = strlen (dirp->curentry.d_name);
302 |
303 | /* determine file type */
304 | attr = dirp->find_data.dwFileAttributes;
305 | if ((attr & FILE_ATTRIBUTE_DEVICE) != 0) {
306 | dirp->curentry.d_type = DT_CHR;
307 | } else if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) {
308 | dirp->curentry.d_type = DT_DIR;
309 | } else {
310 | dirp->curentry.d_type = DT_REG;
311 | }
312 | return &dirp->curentry;
313 | }
314 |
315 |
316 | /*****************************************************************************
317 | * Close directory stream opened by opendir() function. Close of the
318 | * directory stream invalidates the DIR structure as well as any previously
319 | * read directory entry.
320 | */
321 | static int closedir(DIR *dirp)
322 | {
323 | if (dirp == NULL) {
324 | /* invalid directory stream */
325 | DIRENT_SET_ERRNO (EBADF);
326 | return -1;
327 | }
328 |
329 | /* release search handle */
330 | if (dirp->search_handle != INVALID_HANDLE_VALUE) {
331 | FindClose (dirp->search_handle);
332 | dirp->search_handle = INVALID_HANDLE_VALUE;
333 | }
334 |
335 | /* release directory structure */
336 | free (dirp);
337 | return 0;
338 | }
339 |
340 |
341 | /*****************************************************************************
342 | * Resets the position of the directory stream to which dirp refers to the
343 | * beginning of the directory. It also causes the directory stream to refer
344 | * to the current state of the corresponding directory, as a call to opendir()
345 | * would have done. If dirp does not refer to a directory stream, the effect
346 | * is undefined.
347 | */
348 | static void rewinddir(DIR* dirp)
349 | {
350 | if (dirp != NULL) {
351 | /* release search handle */
352 | if (dirp->search_handle != INVALID_HANDLE_VALUE) {
353 | FindClose (dirp->search_handle);
354 | }
355 |
356 | /* open new search handle and retrieve the first entry */
357 | dirp->search_handle = FindFirstFileA (dirp->patt, &dirp->find_data);
358 | if (dirp->search_handle != INVALID_HANDLE_VALUE) {
359 | /* a directory entry is now waiting in memory */
360 | dirp->cached = 1;
361 | } else {
362 | /* failed to re-open directory: no directory entry in memory */
363 | dirp->cached = 0;
364 | }
365 | }
366 | }
367 |
368 |
369 | #ifdef __cplusplus
370 | }
371 | #endif
372 | #endif /*DIRENT_H*/
373 |
--------------------------------------------------------------------------------
/src/file_sync_service.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2007 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #ifndef _FILE_SYNC_SERVICE_H_
18 | #define _FILE_SYNC_SERVICE_H_
19 |
20 | #ifdef HAVE_BIG_ENDIAN
21 | static inline unsigned __swap_uint32(unsigned x)
22 | {
23 | return (((x) & 0xFF000000) >> 24)
24 | | (((x) & 0x00FF0000) >> 8)
25 | | (((x) & 0x0000FF00) << 8)
26 | | (((x) & 0x000000FF) << 24);
27 | }
28 | #define htoll(x) __swap_uint32(x)
29 | #define ltohl(x) __swap_uint32(x)
30 | #define MKID(a,b,c,d) ((d) | ((c) << 8) | ((b) << 16) | ((a) << 24))
31 | #else
32 | #define htoll(x) (x)
33 | #define ltohl(x) (x)
34 | #define MKID(a,b,c,d) ((a) | ((b) << 8) | ((c) << 16) | ((d) << 24))
35 | #endif
36 |
37 | #define ID_STAT MKID('S','T','A','T')
38 | #define ID_LIST MKID('L','I','S','T')
39 | #define ID_ULNK MKID('U','L','N','K')
40 | #define ID_SEND MKID('S','E','N','D')
41 | #define ID_RECV MKID('R','E','C','V')
42 | #define ID_DENT MKID('D','E','N','T')
43 | #define ID_DONE MKID('D','O','N','E')
44 | #define ID_DATA MKID('D','A','T','A')
45 | #define ID_OKAY MKID('O','K','A','Y')
46 | #define ID_FAIL MKID('F','A','I','L')
47 | #define ID_QUIT MKID('Q','U','I','T')
48 |
49 | typedef union {
50 | unsigned id;
51 | struct {
52 | unsigned id;
53 | unsigned namelen;
54 | } req;
55 | struct {
56 | unsigned id;
57 | unsigned mode;
58 | unsigned size;
59 | unsigned time;
60 | } stat;
61 | struct {
62 | unsigned id;
63 | unsigned mode;
64 | unsigned size;
65 | unsigned time;
66 | unsigned namelen;
67 | } dent;
68 | struct {
69 | unsigned id;
70 | unsigned size;
71 | } data;
72 | struct {
73 | unsigned id;
74 | unsigned msglen;
75 | } status;
76 | } syncmsg;
77 |
78 |
79 | void file_sync_service(int fd, void *cookie);
80 | int do_sync_ls(const char *path);
81 | int do_sync_push(const char *lpath, const char *rpath, int verifyApk);
82 | int do_sync_sync(const char *lpath, const char *rpath, int listonly);
83 | int do_sync_pull(const char *rpath, const char *lpath);
84 |
85 | #define SYNC_DATA_MAX (64*1024)
86 |
87 | #endif
88 |
--------------------------------------------------------------------------------
/src/get_my_path_windows.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2007 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #include
18 | #include
19 | #include
20 |
21 | void get_my_path(char *exe, size_t maxLen)
22 | {
23 | char *r;
24 |
25 | /* XXX: should be GetModuleFileNameA */
26 | if (GetModuleFileName(NULL, exe, maxLen) > 0) {
27 | r = strrchr(exe, '\\');
28 | if (r != NULL)
29 | *r = '\0';
30 | } else {
31 | exe[0] = '\0';
32 | }
33 | }
34 |
35 |
--------------------------------------------------------------------------------
/src/libzipfile/centraldir.c:
--------------------------------------------------------------------------------
1 | #include "private.h"
2 | #include
3 | #include
4 | #include
5 |
6 | enum {
7 | // finding the directory
8 | CD_SIGNATURE = 0x06054b50,
9 | EOCD_LEN = 22, // EndOfCentralDir len, excl. comment
10 | MAX_COMMENT_LEN = 65535,
11 | MAX_EOCD_SEARCH = MAX_COMMENT_LEN + EOCD_LEN,
12 |
13 | // central directory entries
14 | ENTRY_SIGNATURE = 0x02014b50,
15 | ENTRY_LEN = 46, // CentralDirEnt len, excl. var fields
16 |
17 | // local file header
18 | LFH_SIZE = 30,
19 | };
20 |
21 | unsigned int
22 | read_le_int(const unsigned char* buf)
23 | {
24 | return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
25 | }
26 |
27 | unsigned int
28 | read_le_short(const unsigned char* buf)
29 | {
30 | return buf[0] | (buf[1] << 8);
31 | }
32 |
33 | static int
34 | read_central_dir_values(Zipfile* file, const unsigned char* buf, int len)
35 | {
36 | if (len < EOCD_LEN) {
37 | // looks like ZIP file got truncated
38 | fprintf(stderr, " Zip EOCD: expected >= %d bytes, found %d\n",
39 | EOCD_LEN, len);
40 | return -1;
41 | }
42 |
43 | file->disknum = read_le_short(&buf[0x04]);
44 | file->diskWithCentralDir = read_le_short(&buf[0x06]);
45 | file->entryCount = read_le_short(&buf[0x08]);
46 | file->totalEntryCount = read_le_short(&buf[0x0a]);
47 | file->centralDirSize = read_le_int(&buf[0x0c]);
48 | file->centralDirOffest = read_le_int(&buf[0x10]);
49 | file->commentLen = read_le_short(&buf[0x14]);
50 |
51 | if (file->commentLen > 0) {
52 | if (EOCD_LEN + file->commentLen > len) {
53 | fprintf(stderr, "EOCD(%d) + comment(%d) exceeds len (%d)\n",
54 | EOCD_LEN, file->commentLen, len);
55 | return -1;
56 | }
57 | file->comment = buf + EOCD_LEN;
58 | }
59 |
60 | return 0;
61 | }
62 |
63 | static int
64 | read_central_directory_entry(Zipfile* file, Zipentry* entry,
65 | const unsigned char** buf, ssize_t* len)
66 | {
67 | const unsigned char* p;
68 |
69 | unsigned short versionMadeBy;
70 | unsigned short versionToExtract;
71 | unsigned short gpBitFlag;
72 | unsigned short compressionMethod;
73 | unsigned short lastModFileTime;
74 | unsigned short lastModFileDate;
75 | unsigned long crc32;
76 | unsigned short extraFieldLength;
77 | unsigned short fileCommentLength;
78 | unsigned short diskNumberStart;
79 | unsigned short internalAttrs;
80 | unsigned long externalAttrs;
81 | unsigned long localHeaderRelOffset;
82 | const unsigned char* extraField;
83 | const unsigned char* fileComment;
84 | unsigned int dataOffset;
85 | unsigned short lfhExtraFieldSize;
86 |
87 |
88 | p = *buf;
89 |
90 | if (*len < ENTRY_LEN) {
91 | fprintf(stderr, "cde entry not large enough\n");
92 | return -1;
93 | }
94 |
95 | if (read_le_int(&p[0x00]) != ENTRY_SIGNATURE) {
96 | fprintf(stderr, "Whoops: didn't find expected signature\n");
97 | return -1;
98 | }
99 |
100 | versionMadeBy = read_le_short(&p[0x04]);
101 | versionToExtract = read_le_short(&p[0x06]);
102 | gpBitFlag = read_le_short(&p[0x08]);
103 | entry->compressionMethod = read_le_short(&p[0x0a]);
104 | lastModFileTime = read_le_short(&p[0x0c]);
105 | lastModFileDate = read_le_short(&p[0x0e]);
106 | crc32 = read_le_int(&p[0x10]);
107 | entry->compressedSize = read_le_int(&p[0x14]);
108 | entry->uncompressedSize = read_le_int(&p[0x18]);
109 | entry->fileNameLength = read_le_short(&p[0x1c]);
110 | extraFieldLength = read_le_short(&p[0x1e]);
111 | fileCommentLength = read_le_short(&p[0x20]);
112 | diskNumberStart = read_le_short(&p[0x22]);
113 | internalAttrs = read_le_short(&p[0x24]);
114 | externalAttrs = read_le_int(&p[0x26]);
115 | localHeaderRelOffset = read_le_int(&p[0x2a]);
116 |
117 | p += ENTRY_LEN;
118 |
119 | // filename
120 | if (entry->fileNameLength != 0) {
121 | entry->fileName = p;
122 | } else {
123 | entry->fileName = NULL;
124 | }
125 | p += entry->fileNameLength;
126 |
127 | // extra field
128 | if (extraFieldLength != 0) {
129 | extraField = p;
130 | } else {
131 | extraField = NULL;
132 | }
133 | p += extraFieldLength;
134 |
135 | // comment, if any
136 | if (fileCommentLength != 0) {
137 | fileComment = p;
138 | } else {
139 | fileComment = NULL;
140 | }
141 | p += fileCommentLength;
142 |
143 | *buf = p;
144 |
145 | // the size of the extraField in the central dir is how much data there is,
146 | // but the one in the local file header also contains some padding.
147 | p = file->buf + localHeaderRelOffset;
148 | extraFieldLength = read_le_short(&p[0x1c]);
149 |
150 | dataOffset = localHeaderRelOffset + LFH_SIZE
151 | + entry->fileNameLength + extraFieldLength;
152 | entry->data = file->buf + dataOffset;
153 | #if 0
154 | printf("file->buf=%p entry->data=%p dataOffset=%x localHeaderRelOffset=%d "
155 | "entry->fileNameLength=%d extraFieldLength=%d\n",
156 | file->buf, entry->data, dataOffset, localHeaderRelOffset,
157 | entry->fileNameLength, extraFieldLength);
158 | #endif
159 | return 0;
160 | }
161 |
162 | /*
163 | * Find the central directory and read the contents.
164 | *
165 | * The fun thing about ZIP archives is that they may or may not be
166 | * readable from start to end. In some cases, notably for archives
167 | * that were written to stdout, the only length information is in the
168 | * central directory at the end of the file.
169 | *
170 | * Of course, the central directory can be followed by a variable-length
171 | * comment field, so we have to scan through it backwards. The comment
172 | * is at most 64K, plus we have 18 bytes for the end-of-central-dir stuff
173 | * itself, plus apparently sometimes people throw random junk on the end
174 | * just for the fun of it.
175 | *
176 | * This is all a little wobbly. If the wrong value ends up in the EOCD
177 | * area, we're hosed. This appears to be the way that everbody handles
178 | * it though, so we're in pretty good company if this fails.
179 | */
180 | int
181 | read_central_dir(Zipfile *file)
182 | {
183 | int err;
184 |
185 | const unsigned char* buf = file->buf;
186 | ssize_t bufsize = file->bufsize;
187 | const unsigned char* eocd;
188 | const unsigned char* p;
189 | const unsigned char* start;
190 | ssize_t len;
191 | int i;
192 |
193 | // too small to be a ZIP archive?
194 | if (bufsize < EOCD_LEN) {
195 | fprintf(stderr, "Length is %d -- too small\n", bufsize);
196 | goto bail;
197 | }
198 |
199 | // find the end-of-central-dir magic
200 | if (bufsize > MAX_EOCD_SEARCH) {
201 | start = buf + bufsize - MAX_EOCD_SEARCH;
202 | } else {
203 | start = buf;
204 | }
205 | p = buf + bufsize - 4;
206 | while (p >= start) {
207 | if (*p == 0x50 && read_le_int(p) == CD_SIGNATURE) {
208 | eocd = p;
209 | break;
210 | }
211 | p--;
212 | }
213 | if (p < start) {
214 | fprintf(stderr, "EOCD not found, not Zip\n");
215 | goto bail;
216 | }
217 |
218 | // extract eocd values
219 | err = read_central_dir_values(file, eocd, (buf+bufsize)-eocd);
220 | if (err != 0) {
221 | goto bail;
222 | }
223 |
224 | if (file->disknum != 0
225 | || file->diskWithCentralDir != 0
226 | || file->entryCount != file->totalEntryCount) {
227 | fprintf(stderr, "Archive spanning not supported\n");
228 | goto bail;
229 | }
230 |
231 | // Loop through and read the central dir entries.
232 | p = buf + file->centralDirOffest;
233 | len = (buf+bufsize)-p;
234 | for (i=0; i < file->totalEntryCount; i++) {
235 | Zipentry* entry = malloc(sizeof(Zipentry));
236 | memset(entry, 0, sizeof(Zipentry));
237 |
238 | err = read_central_directory_entry(file, entry, &p, &len);
239 | if (err != 0) {
240 | fprintf(stderr, "read_central_directory_entry failed\n");
241 | free(entry);
242 | goto bail;
243 | }
244 |
245 | // add it to our list
246 | entry->next = file->entries;
247 | file->entries = entry;
248 | }
249 |
250 | return 0;
251 | bail:
252 | return -1;
253 | }
254 |
--------------------------------------------------------------------------------
/src/libzipfile/private.h:
--------------------------------------------------------------------------------
1 | #ifndef PRIVATE_H
2 | #define PRIVATE_H
3 |
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | typedef int ssize_t; //+
10 | typedef struct Zipentry {
11 | unsigned long fileNameLength;
12 | const unsigned char* fileName;
13 | unsigned short compressionMethod;
14 | unsigned int uncompressedSize;
15 | unsigned int compressedSize;
16 | const unsigned char* data;
17 |
18 | struct Zipentry* next;
19 | } Zipentry;
20 |
21 | typedef struct Zipfile
22 | {
23 | const unsigned char *buf;
24 | ssize_t bufsize;
25 |
26 | // Central directory
27 | unsigned short disknum; //mDiskNumber;
28 | unsigned short diskWithCentralDir; //mDiskWithCentralDir;
29 | unsigned short entryCount; //mNumEntries;
30 | unsigned short totalEntryCount; //mTotalNumEntries;
31 | unsigned int centralDirSize; //mCentralDirSize;
32 | unsigned int centralDirOffest; // offset from first disk //mCentralDirOffset;
33 | unsigned short commentLen; //mCommentLen;
34 | const unsigned char* comment; //mComment;
35 |
36 | Zipentry* entries;
37 | } Zipfile;
38 |
39 | int read_central_dir(Zipfile* file);
40 |
41 | unsigned int read_le_int(const unsigned char* buf);
42 | unsigned int read_le_short(const unsigned char* buf);
43 |
44 | #endif // PRIVATE_H
45 |
46 |
--------------------------------------------------------------------------------
/src/libzipfile/zipfile.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | #include "private.h"
4 | #include
5 | #include
6 | #include
7 | #define DEF_MEM_LEVEL 8 // normally in zutil.h?
8 |
9 | zipfile_t
10 | init_zipfile(const void* data, size_t size)
11 | {
12 | int err;
13 |
14 | Zipfile *file = malloc(sizeof(Zipfile));
15 | if (file == NULL) return NULL;
16 | memset(file, 0, sizeof(Zipfile));
17 | file->buf = data;
18 | file->bufsize = size;
19 |
20 | err = read_central_dir(file);
21 | if (err != 0) goto fail;
22 |
23 | return file;
24 | fail:
25 | free(file);
26 | return NULL;
27 | }
28 |
29 | void
30 | release_zipfile(zipfile_t f)
31 | {
32 | Zipfile* file = (Zipfile*)f;
33 | Zipentry* entry = file->entries;
34 | while (entry) {
35 | Zipentry* next = entry->next;
36 | free(entry);
37 | entry = next;
38 | }
39 | free(file);
40 | }
41 |
42 | zipentry_t
43 | lookup_zipentry(zipfile_t f, const char* entryName)
44 | {
45 | Zipfile* file = (Zipfile*)f;
46 | Zipentry* entry = file->entries;
47 | while (entry) {
48 | if (0 == memcmp(entryName, entry->fileName, entry->fileNameLength)) {
49 | return entry;
50 | }
51 | entry = entry->next;
52 | }
53 | return NULL;
54 | }
55 |
56 | size_t
57 | get_zipentry_size(zipentry_t entry)
58 | {
59 | return ((Zipentry*)entry)->uncompressedSize;
60 | }
61 |
62 | char*
63 | get_zipentry_name(zipentry_t entry)
64 | {
65 | Zipentry* e = (Zipentry*)entry;
66 | int l = e->fileNameLength;
67 | char* s = malloc(l+1);
68 | memcpy(s, e->fileName, l);
69 | s[l] = '\0';
70 | return s;
71 | }
72 |
73 | enum {
74 | STORED = 0,
75 | DEFLATED = 8
76 | };
77 |
78 | static int
79 | uninflate(unsigned char* out, int unlen, const unsigned char* in, int clen)
80 | {
81 | z_stream zstream;
82 | unsigned long crc;
83 | int err = 0;
84 | int zerr;
85 |
86 | memset(&zstream, 0, sizeof(zstream));
87 | zstream.zalloc = Z_NULL;
88 | zstream.zfree = Z_NULL;
89 | zstream.opaque = Z_NULL;
90 | zstream.next_in = (void*)in;
91 | zstream.avail_in = clen;
92 | zstream.next_out = (Bytef*) out;
93 | zstream.avail_out = unlen;
94 | zstream.data_type = Z_UNKNOWN;
95 |
96 | // Use the undocumented "negative window bits" feature to tell zlib
97 | // that there's no zlib header waiting for it.
98 | zerr = inflateInit2(&zstream, -MAX_WBITS);
99 | if (zerr != Z_OK) {
100 | return -1;
101 | }
102 |
103 | // uncompress the data
104 | zerr = inflate(&zstream, Z_FINISH);
105 | if (zerr != Z_STREAM_END) {
106 | fprintf(stderr, "zerr=%d Z_STREAM_END=%d total_out=%lu\n", zerr, Z_STREAM_END,
107 | zstream.total_out);
108 | err = -1;
109 | }
110 |
111 | inflateEnd(&zstream);
112 | return err;
113 | }
114 |
115 | int
116 | decompress_zipentry(zipentry_t e, void* buf, int bufsize)
117 | {
118 | Zipentry* entry = (Zipentry*)e;
119 | switch (entry->compressionMethod)
120 | {
121 | case STORED:
122 | memcpy(buf, entry->data, entry->uncompressedSize);
123 | return 0;
124 | case DEFLATED:
125 | return uninflate(buf, bufsize, entry->data, entry->compressedSize);
126 | default:
127 | return -1;
128 | }
129 | }
130 |
131 | void
132 | dump_zipfile(FILE* to, zipfile_t file)
133 | {
134 | Zipfile* zip = (Zipfile*)file;
135 | Zipentry* entry = zip->entries;
136 | int i;
137 |
138 | fprintf(to, "entryCount=%d\n", zip->entryCount);
139 | for (i=0; ientryCount; i++) {
140 | fprintf(to, " file \"");
141 | fwrite(entry->fileName, entry->fileNameLength, 1, to);
142 | fprintf(to, "\"\n");
143 | entry = entry->next;
144 | }
145 | }
146 |
147 | zipentry_t
148 | iterate_zipfile(zipfile_t file, void** cookie)
149 | {
150 | Zipentry* entry = (Zipentry*)*cookie;
151 | if (entry == NULL) {
152 | Zipfile* zip = (Zipfile*)file;
153 | *cookie = zip->entries;
154 | return *cookie;
155 | } else {
156 | entry = entry->next;
157 | *cookie = entry;
158 | return entry;
159 | }
160 | }
161 |
--------------------------------------------------------------------------------
/src/mutex_list.h:
--------------------------------------------------------------------------------
1 | /* the list of mutexes used by addb */
2 | #ifndef ADB_MUTEX
3 | #error ADB_MUTEX not defined when including this file
4 | #endif
5 |
6 | ADB_MUTEX(dns_lock)
7 | ADB_MUTEX(socket_list_lock)
8 | ADB_MUTEX(transport_lock)
9 | #if ADB_HOST
10 | ADB_MUTEX(local_transports_lock)
11 | #endif
12 | ADB_MUTEX(usb_lock)
13 |
14 | #undef ADB_MUTEX
15 |
--------------------------------------------------------------------------------
/src/sysdeps.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2007 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | /* this file contains system-dependent definitions used by ADB
18 | * they're related to threads, sockets and file descriptors
19 | */
20 | #ifndef _ADB_SYSDEPS_H
21 | #define _ADB_SYSDEPS_H
22 |
23 | #ifdef __CYGWIN__
24 | # undef _WIN32
25 | #endif
26 |
27 | #ifdef _WIN32
28 |
29 | #define __inline__ __inline //+
30 | #define PATH_MAX MAX_PATH //+
31 | #define F_OK 00 //+
32 | #pragma warning(disable : 4005) //+
33 | #define S_ISREG(x) ((x) & _S_IFREG) //+
34 | #include //+
35 | #include
36 | #include
37 | #include
38 | #include
39 | #include
40 | #include
41 | #include
42 | #include
43 | #include
44 | #include
45 |
46 | struct timezone2 //+
47 | { //+
48 | __int32 tz_minuteswest; /* minutes W of Greenwich */ //+
49 | int tz_dsttime; /* type of dst correction */ //+
50 | }; //+
51 | //+
52 | struct timeval2 { //+
53 | __int32 tv_sec; /* seconds */ //+
54 | __int32 tv_usec; /* microseconds */ //+
55 | }; //+
56 | int gettimeofday(struct timeval2 *tv/*in*/, struct timezone2 *tz/*in*/); //+
57 |
58 | __inline__ int snprintf(char* buffer, size_t count, const char* format, ...) { //+
59 | va_list args; //+
60 | va_start(args, format); //+
61 | return _vsnprintf(buffer, count, format, args); //+
62 | } //+
63 |
64 | #define OS_PATH_SEPARATOR '\\'
65 | #define OS_PATH_SEPARATOR_STR "\\"
66 |
67 | typedef CRITICAL_SECTION adb_mutex_t;
68 |
69 | #define ADB_MUTEX_DEFINE(x) adb_mutex_t x
70 |
71 | /* declare all mutexes */
72 | #define ADB_MUTEX(x) extern adb_mutex_t x;
73 | #include "mutex_list.h"
74 |
75 | extern void adb_sysdeps_init(void);
76 |
77 | static __inline__ void adb_mutex_lock( adb_mutex_t* lock )
78 | {
79 | EnterCriticalSection( lock );
80 | }
81 |
82 | static __inline__ void adb_mutex_unlock( adb_mutex_t* lock )
83 | {
84 | LeaveCriticalSection( lock );
85 | }
86 |
87 | typedef struct { unsigned tid; } adb_thread_t;
88 |
89 | typedef void* (*adb_thread_func_t)(void* arg);
90 |
91 | typedef void (*win_thread_func_t)(void* arg);
92 |
93 | static __inline__ int adb_thread_create( adb_thread_t *thread, adb_thread_func_t func, void* arg)
94 | {
95 | thread->tid = _beginthread( (win_thread_func_t)func, 0, arg );
96 | if (thread->tid == (unsigned)-1L) {
97 | return -1;
98 | }
99 | return 0;
100 | }
101 |
102 | static __inline__ void close_on_exec(int fd)
103 | {
104 | /* nothing really */
105 | }
106 |
107 | extern void disable_tcp_nagle(int fd);
108 |
109 | #define lstat stat /* no symlinks on Win32 */
110 |
111 | #define S_ISLNK(m) 0 /* no symlinks on Win32 */
112 |
113 | static __inline__ int adb_unlink(const char* path)
114 | {
115 | int rc = unlink(path);
116 |
117 | if (rc == -1 && errno == EACCES) {
118 | /* unlink returns EACCES when the file is read-only, so we first */
119 | /* try to make it writable, then unlink again... */
120 | rc = chmod(path, _S_IREAD|_S_IWRITE );
121 | if (rc == 0)
122 | rc = unlink(path);
123 | }
124 | return rc;
125 | }
126 | #undef unlink
127 | #define unlink ___xxx_unlink
128 |
129 | static __inline__ int adb_mkdir(const char* path, int mode)
130 | {
131 | return _mkdir(path);
132 | }
133 | #undef mkdir
134 | #define mkdir ___xxx_mkdir
135 |
136 | extern int adb_open(const char* path, int options);
137 | extern int adb_creat(const char* path, int mode);
138 | extern int adb_read(int fd, void* buf, int len);
139 | extern int adb_write(int fd, const void* buf, int len);
140 | extern int adb_lseek(int fd, int pos, int where);
141 | extern int adb_shutdown(int fd);
142 | extern int adb_close(int fd);
143 |
144 | static __inline__ int unix_close(int fd)
145 | {
146 | return close(fd);
147 | }
148 | #undef close
149 | #define close ____xxx_close
150 |
151 | static __inline__ int unix_read(int fd, void* buf, size_t len)
152 | {
153 | return read(fd, buf, len);
154 | }
155 | #undef read
156 | #define read ___xxx_read
157 |
158 | static __inline__ int unix_write(int fd, const void* buf, size_t len)
159 | {
160 | return write(fd, buf, len);
161 | }
162 | #undef write
163 | #define write ___xxx_write
164 |
165 | static __inline__ int adb_open_mode(const char* path, int options, int mode)
166 | {
167 | return adb_open(path, options);
168 | }
169 |
170 | static __inline__ int unix_open(const char* path, int options,...)
171 | {
172 | if ((options & O_CREAT) == 0)
173 | {
174 | return open(path, options);
175 | }
176 | else
177 | {
178 | int mode;
179 | va_list args;
180 | va_start( args, options );
181 | mode = va_arg( args, int );
182 | va_end( args );
183 | return open(path, options, mode);
184 | }
185 | }
186 | #define open ___xxx_unix_open
187 |
188 |
189 | /* normally provided by */
190 | extern void* load_file(const char* pathname, unsigned* psize);
191 |
192 | /* normally provided by */
193 | extern int socket_loopback_client(int port, int type);
194 | extern int socket_network_client(const char *host, int port, int type);
195 | extern int socket_loopback_server(int port, int type);
196 | extern int socket_inaddr_any_server(int port, int type);
197 |
198 | /* normally provided by "fdevent.h" */
199 |
200 | #define FDE_READ 0x0001
201 | #define FDE_WRITE 0x0002
202 | #define FDE_ERROR 0x0004
203 | #define FDE_DONT_CLOSE 0x0080
204 |
205 | typedef struct fdevent fdevent;
206 |
207 | typedef void (*fd_func)(int fd, unsigned events, void *userdata);
208 |
209 | fdevent *fdevent_create(int fd, fd_func func, void *arg);
210 | void fdevent_destroy(fdevent *fde);
211 | void fdevent_install(fdevent *fde, int fd, fd_func func, void *arg);
212 | void fdevent_remove(fdevent *item);
213 | void fdevent_set(fdevent *fde, unsigned events);
214 | void fdevent_add(fdevent *fde, unsigned events);
215 | void fdevent_del(fdevent *fde, unsigned events);
216 | void fdevent_loop();
217 |
218 | struct fdevent {
219 | fdevent *next;
220 | fdevent *prev;
221 |
222 | int fd;
223 | unsigned short state;
224 | unsigned short events;
225 |
226 | fd_func func;
227 | void *arg;
228 | };
229 |
230 | static __inline__ void adb_sleep_ms( int mseconds )
231 | {
232 | Sleep( mseconds );
233 | }
234 |
235 | extern int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen);
236 |
237 | #undef accept
238 | #define accept ___xxx_accept
239 |
240 | static __inline__ int adb_socket_setbufsize( int fd, int bufsize )
241 | {
242 | int opt = bufsize;
243 | return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const char*)&opt, sizeof(opt));
244 | }
245 |
246 | extern int adb_socketpair( int sv[2] );
247 |
248 | static __inline__ char* adb_dirstart( const char* path )
249 | {
250 | char* p = strchr(path, '/');
251 | char* p2 = strchr(path, '\\');
252 |
253 | if ( !p )
254 | p = p2;
255 | else if ( p2 && p2 > p )
256 | p = p2;
257 |
258 | return p;
259 | }
260 |
261 | static __inline__ char* adb_dirstop( const char* path )
262 | {
263 | char* p = strrchr(path, '/');
264 | char* p2 = strrchr(path, '\\');
265 |
266 | if ( !p )
267 | p = p2;
268 | else if ( p2 && p2 > p )
269 | p = p2;
270 |
271 | return p;
272 | }
273 |
274 | static __inline__ int adb_is_absolute_host_path( const char* path )
275 | {
276 | return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
277 | }
278 |
279 | #else /* !_WIN32 a.k.a. Unix */
280 |
281 | #include "fdevent.h"
282 | #include
283 | #include
284 | #include
285 | #include
286 | #include
287 | #include
288 | #include
289 |
290 | #include
291 | #include
292 | #include
293 | #include
294 | #include
295 | #include
296 | #include
297 |
298 | #define OS_PATH_SEPARATOR '/'
299 | #define OS_PATH_SEPARATOR_STR "/"
300 |
301 | typedef pthread_mutex_t adb_mutex_t;
302 | #define ADB_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
303 | #define adb_mutex_init pthread_mutex_init
304 | #define adb_mutex_lock pthread_mutex_lock
305 | #define adb_mutex_unlock pthread_mutex_unlock
306 | #define adb_mutex_destroy pthread_mutex_destroy
307 |
308 | #define ADB_MUTEX_DEFINE(m) static adb_mutex_t m = PTHREAD_MUTEX_INITIALIZER
309 |
310 | #define adb_cond_t pthread_cond_t
311 | #define adb_cond_init pthread_cond_init
312 | #define adb_cond_wait pthread_cond_wait
313 | #define adb_cond_broadcast pthread_cond_broadcast
314 | #define adb_cond_signal pthread_cond_signal
315 | #define adb_cond_destroy pthread_cond_destroy
316 |
317 | static __inline__ void close_on_exec(int fd)
318 | {
319 | fcntl( fd, F_SETFD, FD_CLOEXEC );
320 | }
321 |
322 | static __inline__ int unix_open(const char* path, int options,...)
323 | {
324 | if ((options & O_CREAT) == 0)
325 | {
326 | return open(path, options);
327 | }
328 | else
329 | {
330 | int mode;
331 | va_list args;
332 | va_start( args, options );
333 | mode = va_arg( args, int );
334 | va_end( args );
335 | return open(path, options, mode);
336 | }
337 | }
338 |
339 | static __inline__ int adb_open_mode( const char* pathname, int options, int mode )
340 | {
341 | return open( pathname, options, mode );
342 | }
343 |
344 |
345 | static __inline__ int adb_open( const char* pathname, int options )
346 | {
347 | int fd = open( pathname, options );
348 | if (fd < 0)
349 | return -1;
350 | close_on_exec( fd );
351 | return fd;
352 | }
353 | #undef open
354 | #define open ___xxx_open
355 |
356 | static __inline__ int adb_shutdown(int fd)
357 | {
358 | return shutdown(fd, SHUT_RDWR);
359 | }
360 | #undef shutdown
361 | #define shutdown ____xxx_shutdown
362 |
363 | static __inline__ int adb_close(int fd)
364 | {
365 | return close(fd);
366 | }
367 | #undef close
368 | #define close ____xxx_close
369 |
370 |
371 | static __inline__ int adb_read(int fd, void* buf, size_t len)
372 | {
373 | return read(fd, buf, len);
374 | }
375 |
376 | #undef read
377 | #define read ___xxx_read
378 |
379 | static __inline__ int adb_write(int fd, const void* buf, size_t len)
380 | {
381 | return write(fd, buf, len);
382 | }
383 | #undef write
384 | #define write ___xxx_write
385 |
386 | static __inline__ int adb_lseek(int fd, int pos, int where)
387 | {
388 | return lseek(fd, pos, where);
389 | }
390 | #undef lseek
391 | #define lseek ___xxx_lseek
392 |
393 | static __inline__ int adb_unlink(const char* path)
394 | {
395 | return unlink(path);
396 | }
397 | #undef unlink
398 | #define unlink ___xxx_unlink
399 |
400 | static __inline__ int adb_creat(const char* path, int mode)
401 | {
402 | int fd = creat(path, mode);
403 |
404 | if ( fd < 0 )
405 | return -1;
406 |
407 | close_on_exec(fd);
408 | return fd;
409 | }
410 | #undef creat
411 | #define creat ___xxx_creat
412 |
413 | static __inline__ int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen)
414 | {
415 | int fd;
416 |
417 | fd = accept(serverfd, addr, addrlen);
418 | if (fd >= 0)
419 | close_on_exec(fd);
420 |
421 | return fd;
422 | }
423 |
424 | #undef accept
425 | #define accept ___xxx_accept
426 |
427 | #define unix_read adb_read
428 | #define unix_write adb_write
429 | #define unix_close adb_close
430 |
431 | typedef pthread_t adb_thread_t;
432 |
433 | typedef void* (*adb_thread_func_t)( void* arg );
434 |
435 | static __inline__ int adb_thread_create( adb_thread_t *pthread, adb_thread_func_t start, void* arg )
436 | {
437 | pthread_attr_t attr;
438 |
439 | pthread_attr_init (&attr);
440 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
441 |
442 | return pthread_create( pthread, &attr, start, arg );
443 | }
444 |
445 | static __inline__ int adb_socket_setbufsize( int fd, int bufsize )
446 | {
447 | int opt = bufsize;
448 | return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt));
449 | }
450 |
451 | static __inline__ void disable_tcp_nagle(int fd)
452 | {
453 | int on = 1;
454 | setsockopt( fd, IPPROTO_TCP, TCP_NODELAY, (void*)&on, sizeof(on) );
455 | }
456 |
457 |
458 | static __inline__ int unix_socketpair( int d, int type, int protocol, int sv[2] )
459 | {
460 | return socketpair( d, type, protocol, sv );
461 | }
462 |
463 | static __inline__ int adb_socketpair( int sv[2] )
464 | {
465 | int rc;
466 |
467 | rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv );
468 | if (rc < 0)
469 | return -1;
470 |
471 | close_on_exec( sv[0] );
472 | close_on_exec( sv[1] );
473 | return 0;
474 | }
475 |
476 | #undef socketpair
477 | #define socketpair ___xxx_socketpair
478 |
479 | static __inline__ void adb_sleep_ms( int mseconds )
480 | {
481 | usleep( mseconds*1000 );
482 | }
483 |
484 | static __inline__ int adb_mkdir(const char* path, int mode)
485 | {
486 | return mkdir(path, mode);
487 | }
488 | #undef mkdir
489 | #define mkdir ___xxx_mkdir
490 |
491 | static __inline__ void adb_sysdeps_init(void)
492 | {
493 | }
494 |
495 | static __inline__ char* adb_dirstart(const char* path)
496 | {
497 | return strchr(path, '/');
498 | }
499 |
500 | static __inline__ char* adb_dirstop(const char* path)
501 | {
502 | return strrchr(path, '/');
503 | }
504 |
505 | static __inline__ int adb_is_absolute_host_path( const char* path )
506 | {
507 | return path[0] == '/';
508 | }
509 |
510 | #endif /* !_WIN32 */
511 |
512 | #endif /* _ADB_SYSDEPS_H */
513 |
--------------------------------------------------------------------------------
/src/transport_local.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2007 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #include
18 | #include
19 | #include
20 | #include
21 |
22 | #include "sysdeps.h"
23 | #include
24 |
25 | #define TRACE_TAG TRACE_TRANSPORT
26 | #include "adb.h"
27 |
28 | #ifdef HAVE_BIG_ENDIAN
29 | #define H4(x) (((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8) | (((x) & 0x0000FF00) << 8) | (((x) & 0x000000FF) << 24)
30 | static inline void fix_endians(apacket *p)
31 | {
32 | p->msg.command = H4(p->msg.command);
33 | p->msg.arg0 = H4(p->msg.arg0);
34 | p->msg.arg1 = H4(p->msg.arg1);
35 | p->msg.data_length = H4(p->msg.data_length);
36 | p->msg.data_check = H4(p->msg.data_check);
37 | p->msg.magic = H4(p->msg.magic);
38 | }
39 | #else
40 | #define fix_endians(p) do {} while (0)
41 | #endif
42 |
43 | #if ADB_HOST
44 | /* we keep a list of opened transports. The atransport struct knows to which
45 | * local transport it is connected. The list is used to detect when we're
46 | * trying to connect twice to a given local transport.
47 | */
48 | #define ADB_LOCAL_TRANSPORT_MAX 16
49 |
50 | ADB_MUTEX_DEFINE( local_transports_lock );
51 |
52 | static atransport* local_transports[ ADB_LOCAL_TRANSPORT_MAX ];
53 | #endif /* ADB_HOST */
54 |
55 | static int remote_read(apacket *p, atransport *t)
56 | {
57 | if(readx(t->sfd, &p->msg, sizeof(amessage))){
58 | D("remote local: read terminated (message)\n");
59 | return -1;
60 | }
61 |
62 | fix_endians(p);
63 |
64 | #if 0 && defined HAVE_BIG_ENDIAN
65 | D("read remote packet: %04x arg0=%0x arg1=%0x data_length=%0x data_check=%0x magic=%0x\n",
66 | p->msg.command, p->msg.arg0, p->msg.arg1, p->msg.data_length, p->msg.data_check, p->msg.magic);
67 | #endif
68 | if(check_header(p)) {
69 | D("bad header: terminated (data)\n");
70 | return -1;
71 | }
72 |
73 | if(readx(t->sfd, p->data, p->msg.data_length)){
74 | D("remote local: terminated (data)\n");
75 | return -1;
76 | }
77 |
78 | if(check_data(p)) {
79 | D("bad data: terminated (data)\n");
80 | return -1;
81 | }
82 |
83 | return 0;
84 | }
85 |
86 | static int remote_write(apacket *p, atransport *t)
87 | {
88 | int length = p->msg.data_length;
89 |
90 | fix_endians(p);
91 |
92 | #if 0 && defined HAVE_BIG_ENDIAN
93 | D("write remote packet: %04x arg0=%0x arg1=%0x data_length=%0x data_check=%0x magic=%0x\n",
94 | p->msg.command, p->msg.arg0, p->msg.arg1, p->msg.data_length, p->msg.data_check, p->msg.magic);
95 | #endif
96 | if(writex(t->sfd, &p->msg, sizeof(amessage) + length)) {
97 | D("remote local: write terminated\n");
98 | return -1;
99 | }
100 |
101 | return 0;
102 | }
103 |
104 |
105 | int local_connect(int port) {
106 | return local_connect_arbitrary_ports(port-1, port);
107 | }
108 |
109 | int local_connect_arbitrary_ports(int console_port, int adb_port)
110 | {
111 | char buf[64];
112 | int fd = -1;
113 |
114 | #if ADB_HOST
115 | const char *host = getenv("ADBHOST");
116 | if (host) {
117 | fd = socket_network_client(host, adb_port, SOCK_STREAM);
118 | }
119 | #endif
120 | if (fd < 0) {
121 | fd = socket_loopback_client(adb_port, SOCK_STREAM);
122 | }
123 |
124 | if (fd >= 0) {
125 | D("client: connected on remote on fd %d\n", fd);
126 | close_on_exec(fd);
127 | disable_tcp_nagle(fd);
128 | snprintf(buf, sizeof buf, "%s%d", LOCAL_CLIENT_PREFIX, console_port);
129 | register_socket_transport(fd, buf, adb_port, 1);
130 | return 0;
131 | }
132 | return -1;
133 | }
134 |
135 |
136 | static void *client_socket_thread(void *x)
137 | {
138 | #if ADB_HOST
139 | int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
140 | int count = ADB_LOCAL_TRANSPORT_MAX;
141 |
142 | D("transport: client_socket_thread() starting\n");
143 |
144 | /* try to connect to any number of running emulator instances */
145 | /* this is only done when ADB starts up. later, each new emulator */
146 | /* will send a message to ADB to indicate that is is starting up */
147 | for ( ; count > 0; count--, port += 2 ) {
148 | (void) local_connect(port);
149 | }
150 | #endif
151 | return 0;
152 | }
153 |
154 | static void *server_socket_thread(void * arg)
155 | {
156 | int serverfd, fd;
157 | struct sockaddr addr;
158 | socklen_t alen;
159 | int port = (int)arg;
160 |
161 | D("transport: server_socket_thread() starting\n");
162 | serverfd = -1;
163 | for(;;) {
164 | if(serverfd == -1) {
165 | serverfd = socket_inaddr_any_server(port, SOCK_STREAM);
166 | if(serverfd < 0) {
167 | D("server: cannot bind socket yet\n");
168 | adb_sleep_ms(1000);
169 | continue;
170 | }
171 | close_on_exec(serverfd);
172 | }
173 |
174 | alen = sizeof(addr);
175 | D("server: trying to get new connection from %d\n", port);
176 | fd = adb_socket_accept(serverfd, &addr, &alen);
177 | if(fd >= 0) {
178 | D("server: new connection on fd %d\n", fd);
179 | close_on_exec(fd);
180 | disable_tcp_nagle(fd);
181 | register_socket_transport(fd, "host", port, 1);
182 | }
183 | }
184 | D("transport: server_socket_thread() exiting\n");
185 | return 0;
186 | }
187 |
188 | void local_init(int port)
189 | {
190 | adb_thread_t thr;
191 | void* (*func)(void *);
192 |
193 | if(HOST) {
194 | func = client_socket_thread;
195 | } else {
196 | func = server_socket_thread;
197 | }
198 |
199 | D("transport: local %s init\n", HOST ? "client" : "server");
200 |
201 | if(adb_thread_create(&thr, func, (void *)port)) {
202 | fatal_errno("cannot create local socket %s thread",
203 | HOST ? "client" : "server");
204 | }
205 | }
206 |
207 | static void remote_kick(atransport *t)
208 | {
209 | int fd = t->sfd;
210 | t->sfd = -1;
211 | adb_shutdown(fd);
212 | adb_close(fd);
213 |
214 | #if ADB_HOST
215 | if(HOST) {
216 | int nn;
217 | adb_mutex_lock( &local_transports_lock );
218 | for (nn = 0; nn < ADB_LOCAL_TRANSPORT_MAX; nn++) {
219 | if (local_transports[nn] == t) {
220 | local_transports[nn] = NULL;
221 | break;
222 | }
223 | }
224 | adb_mutex_unlock( &local_transports_lock );
225 | }
226 | #endif
227 | }
228 |
229 | static void remote_close(atransport *t)
230 | {
231 | adb_close(t->fd);
232 | }
233 |
234 |
235 | #if ADB_HOST
236 | /* Only call this function if you already hold local_transports_lock. */
237 | atransport* find_emulator_transport_by_adb_port_locked(int adb_port)
238 | {
239 | int i;
240 | for (i = 0; i < ADB_LOCAL_TRANSPORT_MAX; i++) {
241 | if (local_transports[i] && local_transports[i]->adb_port == adb_port) {
242 | return local_transports[i];
243 | }
244 | }
245 | return NULL;
246 | }
247 |
248 | atransport* find_emulator_transport_by_adb_port(int adb_port)
249 | {
250 | atransport* result;//+
251 | adb_mutex_lock( &local_transports_lock );
252 | //- atransport* result = find_emulator_transport_by_adb_port_locked(adb_port);
253 | result = find_emulator_transport_by_adb_port_locked(adb_port);//+
254 | adb_mutex_unlock( &local_transports_lock );
255 | return result;
256 | }
257 |
258 | /* Only call this function if you already hold local_transports_lock. */
259 | int get_available_local_transport_index_locked()
260 | {
261 | int i;
262 | for (i = 0; i < ADB_LOCAL_TRANSPORT_MAX; i++) {
263 | if (local_transports[i] == NULL) {
264 | return i;
265 | }
266 | }
267 | return -1;
268 | }
269 |
270 | int get_available_local_transport_index()
271 | {
272 | int result;//+
273 | adb_mutex_lock( &local_transports_lock );
274 | //- int result = get_available_local_transport_index_locked();
275 | result = get_available_local_transport_index_locked(); //+
276 | adb_mutex_unlock( &local_transports_lock );
277 | return result;
278 | }
279 | #endif
280 |
281 | int init_socket_transport(atransport *t, int s, int adb_port, int local)
282 | {
283 | int fail = 0;
284 |
285 | t->kick = remote_kick;
286 | t->close = remote_close;
287 | t->read_from_remote = remote_read;
288 | t->write_to_remote = remote_write;
289 | t->sfd = s;
290 | t->sync_token = 1;
291 | t->connection_state = CS_OFFLINE;
292 | t->type = kTransportLocal;
293 | t->adb_port = 0;
294 |
295 | #if ADB_HOST
296 | if (HOST && local) {
297 | adb_mutex_lock( &local_transports_lock );
298 | {
299 | atransport* existing_transport; //+
300 | int index; //+
301 | t->adb_port = adb_port;
302 | //- atransport* existing_transport =
303 | existing_transport = //+
304 | find_emulator_transport_by_adb_port_locked(adb_port);
305 | //- int index = get_available_local_transport_index_locked();
306 | index = get_available_local_transport_index_locked(); //+
307 | if (existing_transport != NULL) {
308 | D("local transport for port %d already registered (%p)?\n",
309 | adb_port, existing_transport);
310 | fail = -1;
311 | } else if (index < 0) {
312 | // Too many emulators.
313 | D("cannot register more emulators. Maximum is %d\n",
314 | ADB_LOCAL_TRANSPORT_MAX);
315 | fail = -1;
316 | } else {
317 | local_transports[index] = t;
318 | }
319 | }
320 | adb_mutex_unlock( &local_transports_lock );
321 | }
322 | #endif
323 | return fail;
324 | }
325 |
--------------------------------------------------------------------------------
/src/transport_usb.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2007 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #include
18 | #include
19 | #include
20 |
21 | #include
22 |
23 | #define TRACE_TAG TRACE_TRANSPORT
24 | #include "adb.h"
25 |
26 | #if ADB_HOST
27 | #include "usb_vendors.h"
28 | #endif
29 |
30 | #ifdef HAVE_BIG_ENDIAN
31 | #define H4(x) (((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8) | (((x) & 0x0000FF00) << 8) | (((x) & 0x000000FF) << 24)
32 | static inline void fix_endians(apacket *p)
33 | {
34 | p->msg.command = H4(p->msg.command);
35 | p->msg.arg0 = H4(p->msg.arg0);
36 | p->msg.arg1 = H4(p->msg.arg1);
37 | p->msg.data_length = H4(p->msg.data_length);
38 | p->msg.data_check = H4(p->msg.data_check);
39 | p->msg.magic = H4(p->msg.magic);
40 | }
41 | unsigned host_to_le32(unsigned n)
42 | {
43 | return H4(n);
44 | }
45 | #else
46 | #define fix_endians(p) do {} while (0)
47 | unsigned host_to_le32(unsigned n)
48 | {
49 | return n;
50 | }
51 | #endif
52 |
53 | static int remote_read(apacket *p, atransport *t)
54 | {
55 | if(usb_read(t->usb, &p->msg, sizeof(amessage))){
56 | D("remote usb: read terminated (message)\n");
57 | return -1;
58 | }
59 |
60 | fix_endians(p);
61 |
62 | if(check_header(p)) {
63 | D("remote usb: check_header failed\n");
64 | return -1;
65 | }
66 |
67 | if(p->msg.data_length) {
68 | if(usb_read(t->usb, p->data, p->msg.data_length)){
69 | D("remote usb: terminated (data)\n");
70 | return -1;
71 | }
72 | }
73 |
74 | if(check_data(p)) {
75 | D("remote usb: check_data failed\n");
76 | return -1;
77 | }
78 |
79 | return 0;
80 | }
81 |
82 | static int remote_write(apacket *p, atransport *t)
83 | {
84 | unsigned size = p->msg.data_length;
85 |
86 | fix_endians(p);
87 |
88 | if(usb_write(t->usb, &p->msg, sizeof(amessage))) {
89 | D("remote usb: 1 - write terminated\n");
90 | return -1;
91 | }
92 | if(p->msg.data_length == 0) return 0;
93 | if(usb_write(t->usb, &p->data, size)) {
94 | D("remote usb: 2 - write terminated\n");
95 | return -1;
96 | }
97 |
98 | return 0;
99 | }
100 |
101 | static void remote_close(atransport *t)
102 | {
103 | usb_close(t->usb);
104 | t->usb = 0;
105 | }
106 |
107 | static void remote_kick(atransport *t)
108 | {
109 | usb_kick(t->usb);
110 | }
111 |
112 | void init_usb_transport(atransport *t, usb_handle *h, int state)
113 | {
114 | D("transport: usb\n");
115 | t->close = remote_close;
116 | t->kick = remote_kick;
117 | t->read_from_remote = remote_read;
118 | t->write_to_remote = remote_write;
119 | t->sync_token = 1;
120 | t->connection_state = state;
121 | t->type = kTransportUsb;
122 | t->usb = h;
123 |
124 | #if ADB_HOST
125 | HOST = 1;
126 | #else
127 | HOST = 0;
128 | #endif
129 | }
130 |
131 | #if ADB_HOST
132 | int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol)
133 | {
134 | unsigned i;
135 | for (i = 0; i < vendorIdCount; i++) {
136 | if (vid == vendorIds[i]) {
137 | if (usb_class == ADB_CLASS && usb_subclass == ADB_SUBCLASS &&
138 | usb_protocol == ADB_PROTOCOL) {
139 | return 1;
140 | }
141 |
142 | return 0;
143 | }
144 | }
145 |
146 | return 0;
147 | }
148 | #endif
149 |
--------------------------------------------------------------------------------
/src/unistd.h:
--------------------------------------------------------------------------------
1 | // dummy
2 |
--------------------------------------------------------------------------------
/src/usb100.h:
--------------------------------------------------------------------------------
1 | #ifndef __USB100_H__
2 | #define __USB100_H__
3 |
4 |
5 | #include
6 |
7 |
8 | //bmRequest.Dir
9 | #define BMREQUEST_HOST_TO_DEVICE 0
10 | #define BMREQUEST_DEVICE_TO_HOST 1
11 |
12 | //bmRequest.Type
13 | #define BMREQUEST_STANDARD 0
14 | #define BMREQUEST_CLASS 1
15 | #define BMREQUEST_VENDOR 2
16 |
17 | //bmRequest.Recipient
18 | #define BMREQUEST_TO_DEVICE 0
19 | #define BMREQUEST_TO_INTERFACE 1
20 | #define BMREQUEST_TO_ENDPOINT 2
21 | #define BMREQUEST_TO_OTHER 3
22 |
23 |
24 | #define MAXIMUM_USB_STRING_LENGTH 255
25 |
26 | // values for the bits returned by the USB GET_STATUS command
27 | #define USB_GETSTATUS_SELF_POWERED 0x01
28 | #define USB_GETSTATUS_REMOTE_WAKEUP_ENABLED 0x02
29 |
30 |
31 | #define USB_DEVICE_DESCRIPTOR_TYPE 0x01
32 | #define USB_CONFIGURATION_DESCRIPTOR_TYPE 0x02
33 | #define USB_STRING_DESCRIPTOR_TYPE 0x03
34 | #define USB_INTERFACE_DESCRIPTOR_TYPE 0x04
35 | #define USB_ENDPOINT_DESCRIPTOR_TYPE 0x05
36 |
37 | // descriptor types defined by DWG documents
38 | #define USB_RESERVED_DESCRIPTOR_TYPE 0x06
39 | #define USB_CONFIG_POWER_DESCRIPTOR_TYPE 0x07
40 | #define USB_INTERFACE_POWER_DESCRIPTOR_TYPE 0x08
41 |
42 | #define USB_DESCRIPTOR_MAKE_TYPE_AND_INDEX(d, i) ((USHORT)((USHORT)d<<8 | i))
43 |
44 | //
45 | // Values for bmAttributes field of an
46 | // endpoint descriptor
47 | //
48 |
49 | #define USB_ENDPOINT_TYPE_MASK 0x03
50 |
51 | #define USB_ENDPOINT_TYPE_CONTROL 0x00
52 | #define USB_ENDPOINT_TYPE_ISOCHRONOUS 0x01
53 | #define USB_ENDPOINT_TYPE_BULK 0x02
54 | #define USB_ENDPOINT_TYPE_INTERRUPT 0x03
55 |
56 |
57 | //
58 | // definitions for bits in the bmAttributes field of a
59 | // configuration descriptor.
60 | //
61 | #define USB_CONFIG_POWERED_MASK 0xc0
62 |
63 | #define USB_CONFIG_BUS_POWERED 0x80
64 | #define USB_CONFIG_SELF_POWERED 0x40
65 | #define USB_CONFIG_REMOTE_WAKEUP 0x20
66 |
67 | //
68 | // Endpoint direction bit, stored in address
69 | //
70 |
71 | #define USB_ENDPOINT_DIRECTION_MASK 0x80
72 |
73 | // test direction bit in the bEndpointAddress field of
74 | // an endpoint descriptor.
75 | #define USB_ENDPOINT_DIRECTION_OUT(addr) (!((addr) & USB_ENDPOINT_DIRECTION_MASK))
76 | #define USB_ENDPOINT_DIRECTION_IN(addr) ((addr) & USB_ENDPOINT_DIRECTION_MASK)
77 |
78 | //
79 | // USB defined request codes
80 | // see chapter 9 of the USB 1.0 specifcation for
81 | // more information.
82 | //
83 |
84 | // These are the correct values based on the USB 1.0
85 | // specification
86 |
87 | #define USB_REQUEST_GET_STATUS 0x00
88 | #define USB_REQUEST_CLEAR_FEATURE 0x01
89 |
90 | #define USB_REQUEST_SET_FEATURE 0x03
91 |
92 | #define USB_REQUEST_SET_ADDRESS 0x05
93 | #define USB_REQUEST_GET_DESCRIPTOR 0x06
94 | #define USB_REQUEST_SET_DESCRIPTOR 0x07
95 | #define USB_REQUEST_GET_CONFIGURATION 0x08
96 | #define USB_REQUEST_SET_CONFIGURATION 0x09
97 | #define USB_REQUEST_GET_INTERFACE 0x0A
98 | #define USB_REQUEST_SET_INTERFACE 0x0B
99 | #define USB_REQUEST_SYNC_FRAME 0x0C
100 |
101 |
102 | //
103 | // defined USB device classes
104 | //
105 |
106 |
107 | #define USB_DEVICE_CLASS_RESERVED 0x00
108 | #define USB_DEVICE_CLASS_AUDIO 0x01
109 | #define USB_DEVICE_CLASS_COMMUNICATIONS 0x02
110 | #define USB_DEVICE_CLASS_HUMAN_INTERFACE 0x03
111 | #define USB_DEVICE_CLASS_MONITOR 0x04
112 | #define USB_DEVICE_CLASS_PHYSICAL_INTERFACE 0x05
113 | #define USB_DEVICE_CLASS_POWER 0x06
114 | #define USB_DEVICE_CLASS_PRINTER 0x07
115 | #define USB_DEVICE_CLASS_STORAGE 0x08
116 | #define USB_DEVICE_CLASS_HUB 0x09
117 | #define USB_DEVICE_CLASS_VENDOR_SPECIFIC 0xFF
118 |
119 | //
120 | // USB Core defined Feature selectors
121 | //
122 |
123 | #define USB_FEATURE_ENDPOINT_STALL 0x0000
124 | #define USB_FEATURE_REMOTE_WAKEUP 0x0001
125 |
126 | //
127 | // USB DWG defined Feature selectors
128 | //
129 |
130 | #define USB_FEATURE_INTERFACE_POWER_D0 0x0002
131 | #define USB_FEATURE_INTERFACE_POWER_D1 0x0003
132 | #define USB_FEATURE_INTERFACE_POWER_D2 0x0004
133 | #define USB_FEATURE_INTERFACE_POWER_D3 0x0005
134 |
135 | typedef struct _USB_DEVICE_DESCRIPTOR {
136 | UCHAR bLength;
137 | UCHAR bDescriptorType;
138 | USHORT bcdUSB;
139 | UCHAR bDeviceClass;
140 | UCHAR bDeviceSubClass;
141 | UCHAR bDeviceProtocol;
142 | UCHAR bMaxPacketSize0;
143 | USHORT idVendor;
144 | USHORT idProduct;
145 | USHORT bcdDevice;
146 | UCHAR iManufacturer;
147 | UCHAR iProduct;
148 | UCHAR iSerialNumber;
149 | UCHAR bNumConfigurations;
150 | } USB_DEVICE_DESCRIPTOR, *PUSB_DEVICE_DESCRIPTOR;
151 |
152 | typedef struct _USB_ENDPOINT_DESCRIPTOR {
153 | UCHAR bLength;
154 | UCHAR bDescriptorType;
155 | UCHAR bEndpointAddress;
156 | UCHAR bmAttributes;
157 | USHORT wMaxPacketSize;
158 | UCHAR bInterval;
159 | } USB_ENDPOINT_DESCRIPTOR, *PUSB_ENDPOINT_DESCRIPTOR;
160 |
161 | typedef struct _USB_CONFIGURATION_DESCRIPTOR {
162 | UCHAR bLength;
163 | UCHAR bDescriptorType;
164 | USHORT wTotalLength;
165 | UCHAR bNumInterfaces;
166 | UCHAR bConfigurationValue;
167 | UCHAR iConfiguration;
168 | UCHAR bmAttributes;
169 | UCHAR MaxPower;
170 | } USB_CONFIGURATION_DESCRIPTOR, *PUSB_CONFIGURATION_DESCRIPTOR;
171 |
172 | typedef struct _USB_INTERFACE_DESCRIPTOR {
173 | UCHAR bLength;
174 | UCHAR bDescriptorType;
175 | UCHAR bInterfaceNumber;
176 | UCHAR bAlternateSetting;
177 | UCHAR bNumEndpoints;
178 | UCHAR bInterfaceClass;
179 | UCHAR bInterfaceSubClass;
180 | UCHAR bInterfaceProtocol;
181 | UCHAR iInterface;
182 | } USB_INTERFACE_DESCRIPTOR, *PUSB_INTERFACE_DESCRIPTOR;
183 |
184 | typedef struct _USB_STRING_DESCRIPTOR {
185 | UCHAR bLength;
186 | UCHAR bDescriptorType;
187 | WCHAR bString[1];
188 | } USB_STRING_DESCRIPTOR, *PUSB_STRING_DESCRIPTOR;
189 |
190 | typedef struct _USB_COMMON_DESCRIPTOR {
191 | UCHAR bLength;
192 | UCHAR bDescriptorType;
193 | } USB_COMMON_DESCRIPTOR, *PUSB_COMMON_DESCRIPTOR;
194 |
195 |
196 | //
197 | // Standard USB HUB definitions
198 | //
199 | // See Chapter 11 USB core specification
200 | //
201 |
202 | typedef struct _USB_HUB_DESCRIPTOR {
203 | UCHAR bDescriptorLength; // Length of this descriptor
204 | UCHAR bDescriptorType; // Hub configuration type
205 | UCHAR bNumberOfPorts; // number of ports on this hub
206 | USHORT wHubCharacteristics; // Hub Charateristics
207 | UCHAR bPowerOnToPowerGood; // port power on till power good in 2ms
208 | UCHAR bHubControlCurrent; // max current in mA
209 | //
210 | // room for 255 ports power control and removable bitmask
211 | UCHAR bRemoveAndPowerMask[64];
212 | } USB_HUB_DESCRIPTOR, *PUSB_HUB_DESCRIPTOR;
213 |
214 |
215 | //
216 | // Structures defined by various DWG feature documents
217 | //
218 |
219 |
220 | //
221 | // See DWG USB Feature Specification: Interface Power Management
222 | //
223 |
224 | #define USB_SUPPORT_D0_COMMAND 0x01
225 | #define USB_SUPPORT_D1_COMMAND 0x02
226 | #define USB_SUPPORT_D2_COMMAND 0x04
227 | #define USB_SUPPORT_D3_COMMAND 0x08
228 |
229 | #define USB_SUPPORT_D1_WAKEUP 0x10
230 | #define USB_SUPPORT_D2_WAKEUP 0x20
231 |
232 |
233 | typedef struct _USB_CONFIGURATION_POWER_DESCRIPTOR {
234 | UCHAR bLength;
235 | UCHAR bDescriptorType;
236 | UCHAR SelfPowerConsumedD0[3];
237 | UCHAR bPowerSummaryId;
238 | UCHAR bBusPowerSavingD1;
239 | UCHAR bSelfPowerSavingD1;
240 | UCHAR bBusPowerSavingD2;
241 | UCHAR bSelfPowerSavingD2;
242 | UCHAR bBusPowerSavingD3;
243 | UCHAR bSelfPowerSavingD3;
244 | USHORT TransitionTimeFromD1;
245 | USHORT TransitionTimeFromD2;
246 | USHORT TransitionTimeFromD3;
247 | } USB_CONFIGURATION_POWER_DESCRIPTOR, *PUSB_CONFIGURATION_POWER_DESCRIPTOR;
248 |
249 |
250 | typedef struct _USB_INTERFACE_POWER_DESCRIPTOR {
251 | UCHAR bLength;
252 | UCHAR bDescriptorType;
253 | UCHAR bmCapabilitiesFlags;
254 | UCHAR bBusPowerSavingD1;
255 | UCHAR bSelfPowerSavingD1;
256 | UCHAR bBusPowerSavingD2;
257 | UCHAR bSelfPowerSavingD2;
258 | UCHAR bBusPowerSavingD3;
259 | UCHAR bSelfPowerSavingD3;
260 | USHORT TransitionTimeFromD1;
261 | USHORT TransitionTimeFromD2;
262 | USHORT TransitionTimeFromD3;
263 | } USB_INTERFACE_POWER_DESCRIPTOR, *PUSB_INTERFACE_POWER_DESCRIPTOR;
264 |
265 |
266 | #include
267 |
268 |
269 | #endif /* __USB100_H__ */
270 |
271 |
--------------------------------------------------------------------------------
/src/usb200.h:
--------------------------------------------------------------------------------
1 | #ifndef __USB200_H__
2 | #define __USB200_H__
3 |
4 | #include "usb100.h"
5 |
6 |
7 | #include
8 |
9 | typedef enum _USB_DEVICE_SPEED {
10 | UsbLowSpeed = 0,
11 | UsbFullSpeed,
12 | UsbHighSpeed
13 | } USB_DEVICE_SPEED;
14 |
15 | typedef enum _USB_DEVICE_TYPE {
16 | Usb11Device = 0,
17 | Usb20Device
18 | } USB_DEVICE_TYPE;
19 |
20 | // standard definiions for the port status
21 | // word of the HUB port register
22 |
23 | #define USB_PORT_STATUS_CONNECT 0x0001
24 | #define USB_PORT_STATUS_ENABLE 0x0002
25 | #define USB_PORT_STATUS_SUSPEND 0x0004
26 | #define USB_PORT_STATUS_OVER_CURRENT 0x0008
27 | #define USB_PORT_STATUS_RESET 0x0010
28 | #define USB_PORT_STATUS_POWER 0x0100
29 | #define USB_PORT_STATUS_LOW_SPEED 0x0200
30 | #define USB_PORT_STATUS_HIGH_SPEED 0x0400
31 |
32 | typedef union _BM_REQUEST_TYPE {
33 | struct _BM {
34 | UCHAR Recipient:2;
35 | UCHAR Reserved:3;
36 | UCHAR Type:2;
37 | UCHAR Dir:1;
38 | };
39 | UCHAR B;
40 | } BM_REQUEST_TYPE, *PBM_REQUEST_TYPE;
41 |
42 | typedef struct _USB_DEFAULT_PIPE_SETUP_PACKET {
43 | BM_REQUEST_TYPE bmRequestType;
44 | UCHAR bRequest;
45 |
46 | union _wValue {
47 | struct {
48 | UCHAR LowByte;
49 | UCHAR HiByte;
50 | };
51 | USHORT W;
52 | } wValue;
53 |
54 | union _wIndex {
55 | struct {
56 | UCHAR LowByte;
57 | UCHAR HiByte;
58 | };
59 | USHORT W;
60 | } wIndex;
61 | USHORT wLength;
62 | } USB_DEFAULT_PIPE_SETUP_PACKET, *PUSB_DEFAULT_PIPE_SETUP_PACKET;
63 |
64 | // setup packet is eight bytes -- defined by spec
65 | C_ASSERT(sizeof(USB_DEFAULT_PIPE_SETUP_PACKET) == 8);
66 |
67 |
68 | #define USB_DEVICE_QUALIFIER_DESCRIPTOR_TYPE 0x06
69 |
70 | typedef struct _USB_DEVICE_QUALIFIER_DESCRIPTOR {
71 | UCHAR bLength;
72 | UCHAR bDescriptorType;
73 | USHORT bcdUSB;
74 | UCHAR bDeviceClass;
75 | UCHAR bDeviceSubClass;
76 | UCHAR bDeviceProtocol;
77 | UCHAR bMaxPacketSize0;
78 | UCHAR bNumConfigurations;
79 | UCHAR bReserved;
80 | } USB_DEVICE_QUALIFIER_DESCRIPTOR, *PUSB_DEVICE_QUALIFIER_DESCRIPTOR;
81 |
82 |
83 | typedef union _USB_HIGH_SPEED_MAXPACKET {
84 | struct _MP {
85 | USHORT MaxPacket:11; /* 0..10 */
86 | USHORT HSmux:2; /* 11..12 */
87 | USHORT Reserved:3; /* 13..15 */
88 | };
89 | USHORT us;
90 | } USB_HIGH_SPEED_MAXPACKET, *PUSB_HIGH_SPEED_MAXPACKET;
91 |
92 | #define USB_INTERFACE_ASSOCIATION_DESCRIPTOR_TYPE 0x0B
93 |
94 | typedef struct _USB_INTERFACE_ASSOCIATION_DESCRIPTOR {
95 |
96 | UCHAR bLength;
97 | UCHAR bDescriptorType;
98 | UCHAR bFirstInterface;
99 | UCHAR bInterfaceCount;
100 | UCHAR bFunctionClass;
101 | UCHAR bFunctionSubClass;
102 | UCHAR bFunctionProtocol;
103 | UCHAR iFunction;
104 |
105 | } USB_INTERFACE_ASSOCIATION_DESCRIPTOR, *PUSB_INTERFACE_ASSOCIATION_DESCRIPTOR;
106 |
107 |
108 | #include
109 |
110 | #endif __USB200_H__
111 |
112 |
--------------------------------------------------------------------------------
/src/usb_vendors.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2009 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #include "usb_vendors.h"
18 |
19 | #include
20 |
21 | #ifdef _WIN32
22 | # define WIN32_LEAN_AND_MEAN
23 | # include "windows.h"
24 | # include "shlobj.h"
25 | #else
26 | # include
27 | # include
28 | #endif
29 |
30 | #include "sysdeps.h"
31 | #include "adb.h"
32 |
33 | #define ANDROID_PATH ".android"
34 | #define ANDROID_ADB_INI "adb_usb.ini"
35 |
36 | #define TRACE_TAG TRACE_USB
37 |
38 | // Google's USB Vendor ID
39 | #define VENDOR_ID_GOOGLE 0x18d1
40 | // HTC's USB Vendor ID
41 | #define VENDOR_ID_HTC 0x0bb4
42 | // Samsung's USB Vendor ID
43 | #define VENDOR_ID_SAMSUNG 0x04e8
44 | // Motorola's USB Vendor ID
45 | #define VENDOR_ID_MOTOROLA 0x22b8
46 | // LG's USB Vendor ID
47 | #define VENDOR_ID_LGE 0x1004
48 | // Huawei's USB Vendor ID
49 | #define VENDOR_ID_HUAWEI 0x12D1
50 | // Acer's USB Vendor ID
51 | #define VENDOR_ID_ACER 0x0502
52 | // Sony Ericsson's USB Vendor ID
53 | #define VENDOR_ID_SONY_ERICSSON 0x0FCE
54 | // Foxconn's USB Vendor ID
55 | #define VENDOR_ID_FOXCONN 0x0489
56 | // Dell's USB Vendor ID
57 | #define VENDOR_ID_DELL 0x413c
58 | // Nvidia's USB Vendor ID
59 | #define VENDOR_ID_NVIDIA 0x0955
60 | // Garmin-Asus's USB Vendor ID
61 | #define VENDOR_ID_GARMIN_ASUS 0x091E
62 | // Sharp's USB Vendor ID
63 | #define VENDOR_ID_SHARP 0x04dd
64 | // ZTE's USB Vendor ID
65 | #define VENDOR_ID_ZTE 0x19D2
66 | // Kyocera's USB Vendor ID
67 | #define VENDOR_ID_KYOCERA 0x0482
68 | // Pantech's USB Vendor ID
69 | #define VENDOR_ID_PANTECH 0x10A9
70 | // Qualcomm's USB Vendor ID
71 | #define VENDOR_ID_QUALCOMM 0x05c6
72 | // On-The-Go-Video's USB Vendor ID
73 | #define VENDOR_ID_OTGV 0x2257
74 | // NEC's USB Vendor ID
75 | #define VENDOR_ID_NEC 0x0409
76 | // Panasonic Mobile Communication's USB Vendor ID
77 | #define VENDOR_ID_PMC 0x04DA
78 | // Toshiba's USB Vendor ID
79 | #define VENDOR_ID_TOSHIBA 0x0930
80 | // SK Telesys's USB Vendor ID
81 | #define VENDOR_ID_SK_TELESYS 0x1F53
82 | // KT Tech's USB Vendor ID
83 | #define VENDOR_ID_KT_TECH 0x2116
84 | // Asus's USB Vendor ID
85 | #define VENDOR_ID_ASUS 0x0b05
86 | // Philips's USB Vendor ID
87 | #define VENDOR_ID_PHILIPS 0x0471
88 | // Texas Instruments's USB Vendor ID
89 | #define VENDOR_ID_TI 0x0451
90 | // Funai's USB Vendor ID
91 | #define VENDOR_ID_FUNAI 0x0F1C
92 | // Gigabyte's USB Vendor ID
93 | #define VENDOR_ID_GIGABYTE 0x0414
94 | // IRiver's USB Vendor ID
95 | #define VENDOR_ID_IRIVER 0x2420
96 | // Compal's USB Vendor ID
97 | #define VENDOR_ID_COMPAL 0x1219
98 | // T & A Mobile Phones' USB Vendor ID
99 | #define VENDOR_ID_T_AND_A 0x1BBB
100 | // LenovoMobile's USB Vendor ID
101 | #define VENDOR_ID_LENOVOMOBILE 0x2006
102 | // Lenovo's USB Vendor ID
103 | #define VENDOR_ID_LENOVO 0x17EF
104 | // Vizio's USB Vendor ID
105 | #define VENDOR_ID_VIZIO 0xE040
106 |
107 |
108 | /** built-in vendor list */
109 | int builtInVendorIds[] = {
110 | VENDOR_ID_GOOGLE,
111 | VENDOR_ID_HTC,
112 | VENDOR_ID_SAMSUNG,
113 | VENDOR_ID_MOTOROLA,
114 | VENDOR_ID_LGE,
115 | VENDOR_ID_HUAWEI,
116 | VENDOR_ID_ACER,
117 | VENDOR_ID_SONY_ERICSSON,
118 | VENDOR_ID_FOXCONN,
119 | VENDOR_ID_DELL,
120 | VENDOR_ID_NVIDIA,
121 | VENDOR_ID_GARMIN_ASUS,
122 | VENDOR_ID_SHARP,
123 | VENDOR_ID_ZTE,
124 | VENDOR_ID_KYOCERA,
125 | VENDOR_ID_PANTECH,
126 | VENDOR_ID_QUALCOMM,
127 | VENDOR_ID_OTGV,
128 | VENDOR_ID_NEC,
129 | VENDOR_ID_PMC,
130 | VENDOR_ID_TOSHIBA,
131 | VENDOR_ID_SK_TELESYS,
132 | VENDOR_ID_KT_TECH,
133 | VENDOR_ID_ASUS,
134 | VENDOR_ID_PHILIPS,
135 | VENDOR_ID_TI,
136 | VENDOR_ID_FUNAI,
137 | VENDOR_ID_GIGABYTE,
138 | VENDOR_ID_IRIVER,
139 | VENDOR_ID_COMPAL,
140 | VENDOR_ID_T_AND_A,
141 | VENDOR_ID_LENOVOMOBILE,
142 | VENDOR_ID_LENOVO,
143 | VENDOR_ID_VIZIO,
144 | };
145 |
146 | #define BUILT_IN_VENDOR_COUNT (sizeof(builtInVendorIds)/sizeof(builtInVendorIds[0]))
147 |
148 | /* max number of supported vendor ids (built-in + 3rd party). increase as needed */
149 | #define VENDOR_COUNT_MAX 128
150 |
151 | int vendorIds[VENDOR_COUNT_MAX];
152 | unsigned vendorIdCount = 0;
153 |
154 | int get_adb_usb_ini(char* buff, size_t len);
155 |
156 | void usb_vendors_init(void)
157 | {
158 | if (VENDOR_COUNT_MAX < BUILT_IN_VENDOR_COUNT) {
159 | fprintf(stderr, "VENDOR_COUNT_MAX not big enough for built-in vendor list.\n");
160 | exit(2);
161 | }
162 |
163 | /* add the built-in vendors at the beginning of the array */
164 | memcpy(vendorIds, builtInVendorIds, sizeof(builtInVendorIds));
165 |
166 | /* default array size is the number of built-in vendors */
167 | vendorIdCount = BUILT_IN_VENDOR_COUNT;
168 |
169 | if (VENDOR_COUNT_MAX == BUILT_IN_VENDOR_COUNT)
170 | return;
171 |
172 | {//+
173 | char temp[PATH_MAX];
174 | if (get_adb_usb_ini(temp, sizeof(temp)) == 0) {
175 | FILE * f = fopen(temp, "rt");
176 |
177 | if (f != NULL) {
178 | /* The vendor id file is pretty basic. 1 vendor id per line.
179 | Lines starting with # are comments */
180 | while (fgets(temp, sizeof(temp), f) != NULL) {
181 | if (temp[0] == '#')
182 | continue;
183 |
184 | {//+
185 | long value = strtol(temp, NULL, 0);
186 | if (errno == EINVAL || errno == ERANGE || value > INT_MAX || value < 0) {
187 | fprintf(stderr, "Invalid content in %s. Quitting.\n", ANDROID_ADB_INI);
188 | exit(2);
189 | }
190 |
191 | vendorIds[vendorIdCount++] = (int)value;
192 |
193 | /* make sure we don't go beyond the array */
194 | if (vendorIdCount == VENDOR_COUNT_MAX) {
195 | break;
196 | }
197 | }//+
198 | }
199 | }
200 | }
201 | }//+
202 | }
203 |
204 | /* Utils methods */
205 |
206 | /* builds the path to the adb vendor id file. returns 0 if success */
207 | int build_path(char* buff, size_t len, const char* format, const char* home)
208 | {
209 | if (snprintf(buff, len, format, home, ANDROID_PATH, ANDROID_ADB_INI) >= (signed)len) {
210 | return 1;
211 | }
212 |
213 | return 0;
214 | }
215 |
216 | /* fills buff with the path to the adb vendor id file. returns 0 if success */
217 | int get_adb_usb_ini(char* buff, size_t len)
218 | {
219 | #ifdef _WIN32
220 | const char* home = getenv("ANDROID_SDK_HOME");
221 | if (home != NULL) {
222 | return build_path(buff, len, "%s\\%s\\%s", home);
223 | } else {
224 | char path[MAX_PATH];
225 | SHGetFolderPath( NULL, CSIDL_PROFILE, NULL, 0, path);
226 | return build_path(buff, len, "%s\\%s\\%s", path);
227 | }
228 | #else
229 | const char* home = getenv("HOME");
230 | if (home == NULL)
231 | home = "/tmp";
232 |
233 | return build_path(buff, len, "%s/%s/%s", home);
234 | #endif
235 | }
236 |
--------------------------------------------------------------------------------
/src/usb_vendors.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2009 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #ifndef __USB_VENDORS_H
18 | #define __USB_VENDORS_H
19 |
20 | extern int vendorIds[];
21 | extern unsigned vendorIdCount;
22 |
23 | void usb_vendors_init(void);
24 |
25 | #endif
26 |
--------------------------------------------------------------------------------
/src/zipfile/zipfile.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2008 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #ifndef _ZIPFILE_ZIPFILE_H
18 | #define _ZIPFILE_ZIPFILE_H
19 |
20 | #include
21 |
22 | #ifdef __cplusplus
23 | extern "C" {
24 | #endif
25 |
26 | typedef void* zipfile_t;
27 | typedef void* zipentry_t;
28 |
29 | // Provide a buffer. Returns NULL on failure.
30 | zipfile_t init_zipfile(const void* data, size_t size);
31 |
32 | // Release the zipfile resources.
33 | void release_zipfile(zipfile_t file);
34 |
35 | // Get a named entry object. Returns NULL if it doesn't exist
36 | // or if we won't be able to decompress it. The zipentry_t is
37 | // freed by release_zipfile()
38 | zipentry_t lookup_zipentry(zipfile_t file, const char* entryName);
39 |
40 | // Return the size of the entry.
41 | size_t get_zipentry_size(zipentry_t entry);
42 |
43 | // return the filename of this entry, you own the memory returned
44 | char* get_zipentry_name(zipentry_t entry);
45 |
46 | // The buffer must be 1.001 times the buffer size returned
47 | // by get_zipentry_size. Returns nonzero on failure.
48 | int decompress_zipentry(zipentry_t entry, void* buf, int bufsize);
49 |
50 | // iterate through the entries in the zip file. pass a pointer to
51 | // a void* initialized to NULL to start. Returns NULL when done
52 | zipentry_t iterate_zipfile(zipfile_t file, void** cookie);
53 |
54 | #ifdef __cplusplus
55 | } // extern "C"
56 | #endif
57 |
58 | #endif // _ZIPFILE_ZIPFILE_H
59 |
--------------------------------------------------------------------------------
/src/zlib/adler32.c:
--------------------------------------------------------------------------------
1 | /* adler32.c -- compute the Adler-32 checksum of a data stream
2 | * Copyright (C) 1995-2007 Mark Adler
3 | * For conditions of distribution and use, see copyright notice in zlib.h
4 | */
5 |
6 | /* @(#) $Id$ */
7 |
8 | #include "zutil.h"
9 |
10 | #define local static
11 |
12 | local uLong adler32_combine_(uLong adler1, uLong adler2, z_off64_t len2);
13 |
14 | #define BASE 65521UL /* largest prime smaller than 65536 */
15 | #define NMAX 5552
16 | /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
17 |
18 | #define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;}
19 | #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
20 | #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
21 | #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
22 | #define DO16(buf) DO8(buf,0); DO8(buf,8);
23 |
24 | /* use NO_DIVIDE if your processor does not do division in hardware */
25 | #ifdef NO_DIVIDE
26 | # define MOD(a) \
27 | do { \
28 | if (a >= (BASE << 16)) a -= (BASE << 16); \
29 | if (a >= (BASE << 15)) a -= (BASE << 15); \
30 | if (a >= (BASE << 14)) a -= (BASE << 14); \
31 | if (a >= (BASE << 13)) a -= (BASE << 13); \
32 | if (a >= (BASE << 12)) a -= (BASE << 12); \
33 | if (a >= (BASE << 11)) a -= (BASE << 11); \
34 | if (a >= (BASE << 10)) a -= (BASE << 10); \
35 | if (a >= (BASE << 9)) a -= (BASE << 9); \
36 | if (a >= (BASE << 8)) a -= (BASE << 8); \
37 | if (a >= (BASE << 7)) a -= (BASE << 7); \
38 | if (a >= (BASE << 6)) a -= (BASE << 6); \
39 | if (a >= (BASE << 5)) a -= (BASE << 5); \
40 | if (a >= (BASE << 4)) a -= (BASE << 4); \
41 | if (a >= (BASE << 3)) a -= (BASE << 3); \
42 | if (a >= (BASE << 2)) a -= (BASE << 2); \
43 | if (a >= (BASE << 1)) a -= (BASE << 1); \
44 | if (a >= BASE) a -= BASE; \
45 | } while (0)
46 | # define MOD4(a) \
47 | do { \
48 | if (a >= (BASE << 4)) a -= (BASE << 4); \
49 | if (a >= (BASE << 3)) a -= (BASE << 3); \
50 | if (a >= (BASE << 2)) a -= (BASE << 2); \
51 | if (a >= (BASE << 1)) a -= (BASE << 1); \
52 | if (a >= BASE) a -= BASE; \
53 | } while (0)
54 | #else
55 | # define MOD(a) a %= BASE
56 | # define MOD4(a) a %= BASE
57 | #endif
58 |
59 | /* ========================================================================= */
60 | uLong ZEXPORT adler32(adler, buf, len)
61 | uLong adler;
62 | const Bytef *buf;
63 | uInt len;
64 | {
65 | unsigned long sum2;
66 | unsigned n;
67 |
68 | /* split Adler-32 into component sums */
69 | sum2 = (adler >> 16) & 0xffff;
70 | adler &= 0xffff;
71 |
72 | /* in case user likes doing a byte at a time, keep it fast */
73 | if (len == 1) {
74 | adler += buf[0];
75 | if (adler >= BASE)
76 | adler -= BASE;
77 | sum2 += adler;
78 | if (sum2 >= BASE)
79 | sum2 -= BASE;
80 | return adler | (sum2 << 16);
81 | }
82 |
83 | /* initial Adler-32 value (deferred check for len == 1 speed) */
84 | if (buf == Z_NULL)
85 | return 1L;
86 |
87 | /* in case short lengths are provided, keep it somewhat fast */
88 | if (len < 16) {
89 | while (len--) {
90 | adler += *buf++;
91 | sum2 += adler;
92 | }
93 | if (adler >= BASE)
94 | adler -= BASE;
95 | MOD4(sum2); /* only added so many BASE's */
96 | return adler | (sum2 << 16);
97 | }
98 |
99 | /* do length NMAX blocks -- requires just one modulo operation */
100 | while (len >= NMAX) {
101 | len -= NMAX;
102 | n = NMAX / 16; /* NMAX is divisible by 16 */
103 | do {
104 | DO16(buf); /* 16 sums unrolled */
105 | buf += 16;
106 | } while (--n);
107 | MOD(adler);
108 | MOD(sum2);
109 | }
110 |
111 | /* do remaining bytes (less than NMAX, still just one modulo) */
112 | if (len) { /* avoid modulos if none remaining */
113 | while (len >= 16) {
114 | len -= 16;
115 | DO16(buf);
116 | buf += 16;
117 | }
118 | while (len--) {
119 | adler += *buf++;
120 | sum2 += adler;
121 | }
122 | MOD(adler);
123 | MOD(sum2);
124 | }
125 |
126 | /* return recombined sums */
127 | return adler | (sum2 << 16);
128 | }
129 |
130 | /* ========================================================================= */
131 | local uLong adler32_combine_(adler1, adler2, len2)
132 | uLong adler1;
133 | uLong adler2;
134 | z_off64_t len2;
135 | {
136 | unsigned long sum1;
137 | unsigned long sum2;
138 | unsigned rem;
139 |
140 | /* the derivation of this formula is left as an exercise for the reader */
141 | rem = (unsigned)(len2 % BASE);
142 | sum1 = adler1 & 0xffff;
143 | sum2 = rem * sum1;
144 | MOD(sum2);
145 | sum1 += (adler2 & 0xffff) + BASE - 1;
146 | sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
147 | if (sum1 >= BASE) sum1 -= BASE;
148 | if (sum1 >= BASE) sum1 -= BASE;
149 | if (sum2 >= (BASE << 1)) sum2 -= (BASE << 1);
150 | if (sum2 >= BASE) sum2 -= BASE;
151 | return sum1 | (sum2 << 16);
152 | }
153 |
154 | /* ========================================================================= */
155 | uLong ZEXPORT adler32_combine(adler1, adler2, len2)
156 | uLong adler1;
157 | uLong adler2;
158 | z_off_t len2;
159 | {
160 | return adler32_combine_(adler1, adler2, len2);
161 | }
162 |
163 | uLong ZEXPORT adler32_combine64(adler1, adler2, len2)
164 | uLong adler1;
165 | uLong adler2;
166 | z_off64_t len2;
167 | {
168 | return adler32_combine_(adler1, adler2, len2);
169 | }
170 |
--------------------------------------------------------------------------------
/src/zlib/compress.c:
--------------------------------------------------------------------------------
1 | /* compress.c -- compress a memory buffer
2 | * Copyright (C) 1995-2005 Jean-loup Gailly.
3 | * For conditions of distribution and use, see copyright notice in zlib.h
4 | */
5 |
6 | /* @(#) $Id$ */
7 |
8 | #define ZLIB_INTERNAL
9 | #include "zlib.h"
10 |
11 | /* ===========================================================================
12 | Compresses the source buffer into the destination buffer. The level
13 | parameter has the same meaning as in deflateInit. sourceLen is the byte
14 | length of the source buffer. Upon entry, destLen is the total size of the
15 | destination buffer, which must be at least 0.1% larger than sourceLen plus
16 | 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
17 |
18 | compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
19 | memory, Z_BUF_ERROR if there was not enough room in the output buffer,
20 | Z_STREAM_ERROR if the level parameter is invalid.
21 | */
22 | int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
23 | Bytef *dest;
24 | uLongf *destLen;
25 | const Bytef *source;
26 | uLong sourceLen;
27 | int level;
28 | {
29 | z_stream stream;
30 | int err;
31 |
32 | stream.next_in = (Bytef*)source;
33 | stream.avail_in = (uInt)sourceLen;
34 | #ifdef MAXSEG_64K
35 | /* Check for source > 64K on 16-bit machine: */
36 | if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
37 | #endif
38 | stream.next_out = dest;
39 | stream.avail_out = (uInt)*destLen;
40 | if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
41 |
42 | stream.zalloc = (alloc_func)0;
43 | stream.zfree = (free_func)0;
44 | stream.opaque = (voidpf)0;
45 |
46 | err = deflateInit(&stream, level);
47 | if (err != Z_OK) return err;
48 |
49 | err = deflate(&stream, Z_FINISH);
50 | if (err != Z_STREAM_END) {
51 | deflateEnd(&stream);
52 | return err == Z_OK ? Z_BUF_ERROR : err;
53 | }
54 | *destLen = stream.total_out;
55 |
56 | err = deflateEnd(&stream);
57 | return err;
58 | }
59 |
60 | /* ===========================================================================
61 | */
62 | int ZEXPORT compress (dest, destLen, source, sourceLen)
63 | Bytef *dest;
64 | uLongf *destLen;
65 | const Bytef *source;
66 | uLong sourceLen;
67 | {
68 | return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
69 | }
70 |
71 | /* ===========================================================================
72 | If the default memLevel or windowBits for deflateInit() is changed, then
73 | this function needs to be updated.
74 | */
75 | uLong ZEXPORT compressBound (sourceLen)
76 | uLong sourceLen;
77 | {
78 | return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
79 | (sourceLen >> 25) + 13;
80 | }
81 |
--------------------------------------------------------------------------------
/src/zlib/deflate.h:
--------------------------------------------------------------------------------
1 | /* deflate.h -- internal compression state
2 | * Copyright (C) 1995-2010 Jean-loup Gailly
3 | * For conditions of distribution and use, see copyright notice in zlib.h
4 | */
5 |
6 | /* WARNING: this file should *not* be used by applications. It is
7 | part of the implementation of the compression library and is
8 | subject to change. Applications should only use zlib.h.
9 | */
10 |
11 | /* @(#) $Id$ */
12 |
13 | #ifndef DEFLATE_H
14 | #define DEFLATE_H
15 |
16 | #include "zutil.h"
17 |
18 | /* define NO_GZIP when compiling if you want to disable gzip header and
19 | trailer creation by deflate(). NO_GZIP would be used to avoid linking in
20 | the crc code when it is not needed. For shared libraries, gzip encoding
21 | should be left enabled. */
22 | #ifndef NO_GZIP
23 | # define GZIP
24 | #endif
25 |
26 | /* ===========================================================================
27 | * Internal compression state.
28 | */
29 |
30 | #define LENGTH_CODES 29
31 | /* number of length codes, not counting the special END_BLOCK code */
32 |
33 | #define LITERALS 256
34 | /* number of literal bytes 0..255 */
35 |
36 | #define L_CODES (LITERALS+1+LENGTH_CODES)
37 | /* number of Literal or Length codes, including the END_BLOCK code */
38 |
39 | #define D_CODES 30
40 | /* number of distance codes */
41 |
42 | #define BL_CODES 19
43 | /* number of codes used to transfer the bit lengths */
44 |
45 | #define HEAP_SIZE (2*L_CODES+1)
46 | /* maximum heap size */
47 |
48 | #define MAX_BITS 15
49 | /* All codes must not exceed MAX_BITS bits */
50 |
51 | #define INIT_STATE 42
52 | #define EXTRA_STATE 69
53 | #define NAME_STATE 73
54 | #define COMMENT_STATE 91
55 | #define HCRC_STATE 103
56 | #define BUSY_STATE 113
57 | #define FINISH_STATE 666
58 | /* Stream status */
59 |
60 |
61 | /* Data structure describing a single value and its code string. */
62 | typedef struct ct_data_s {
63 | union {
64 | ush freq; /* frequency count */
65 | ush code; /* bit string */
66 | } fc;
67 | union {
68 | ush dad; /* father node in Huffman tree */
69 | ush len; /* length of bit string */
70 | } dl;
71 | } FAR ct_data;
72 |
73 | #define Freq fc.freq
74 | #define Code fc.code
75 | #define Dad dl.dad
76 | #define Len dl.len
77 |
78 | typedef struct static_tree_desc_s static_tree_desc;
79 |
80 | typedef struct tree_desc_s {
81 | ct_data *dyn_tree; /* the dynamic tree */
82 | int max_code; /* largest code with non zero frequency */
83 | static_tree_desc *stat_desc; /* the corresponding static tree */
84 | } FAR tree_desc;
85 |
86 | typedef ush Pos;
87 | typedef Pos FAR Posf;
88 | typedef unsigned IPos;
89 |
90 | /* A Pos is an index in the character window. We use short instead of int to
91 | * save space in the various tables. IPos is used only for parameter passing.
92 | */
93 |
94 | typedef struct internal_state {
95 | z_streamp strm; /* pointer back to this zlib stream */
96 | int status; /* as the name implies */
97 | Bytef *pending_buf; /* output still pending */
98 | ulg pending_buf_size; /* size of pending_buf */
99 | Bytef *pending_out; /* next pending byte to output to the stream */
100 | uInt pending; /* nb of bytes in the pending buffer */
101 | int wrap; /* bit 0 true for zlib, bit 1 true for gzip */
102 | gz_headerp gzhead; /* gzip header information to write */
103 | uInt gzindex; /* where in extra, name, or comment */
104 | Byte method; /* STORED (for zip only) or DEFLATED */
105 | int last_flush; /* value of flush param for previous deflate call */
106 |
107 | /* used by deflate.c: */
108 |
109 | uInt w_size; /* LZ77 window size (32K by default) */
110 | uInt w_bits; /* log2(w_size) (8..16) */
111 | uInt w_mask; /* w_size - 1 */
112 |
113 | Bytef *window;
114 | /* Sliding window. Input bytes are read into the second half of the window,
115 | * and move to the first half later to keep a dictionary of at least wSize
116 | * bytes. With this organization, matches are limited to a distance of
117 | * wSize-MAX_MATCH bytes, but this ensures that IO is always
118 | * performed with a length multiple of the block size. Also, it limits
119 | * the window size to 64K, which is quite useful on MSDOS.
120 | * To do: use the user input buffer as sliding window.
121 | */
122 |
123 | ulg window_size;
124 | /* Actual size of window: 2*wSize, except when the user input buffer
125 | * is directly used as sliding window.
126 | */
127 |
128 | Posf *prev;
129 | /* Link to older string with same hash index. To limit the size of this
130 | * array to 64K, this link is maintained only for the last 32K strings.
131 | * An index in this array is thus a window index modulo 32K.
132 | */
133 |
134 | Posf *head; /* Heads of the hash chains or NIL. */
135 |
136 | uInt ins_h; /* hash index of string to be inserted */
137 | uInt hash_size; /* number of elements in hash table */
138 | uInt hash_bits; /* log2(hash_size) */
139 | uInt hash_mask; /* hash_size-1 */
140 |
141 | uInt hash_shift;
142 | /* Number of bits by which ins_h must be shifted at each input
143 | * step. It must be such that after MIN_MATCH steps, the oldest
144 | * byte no longer takes part in the hash key, that is:
145 | * hash_shift * MIN_MATCH >= hash_bits
146 | */
147 |
148 | long block_start;
149 | /* Window position at the beginning of the current output block. Gets
150 | * negative when the window is moved backwards.
151 | */
152 |
153 | uInt match_length; /* length of best match */
154 | IPos prev_match; /* previous match */
155 | int match_available; /* set if previous match exists */
156 | uInt strstart; /* start of string to insert */
157 | uInt match_start; /* start of matching string */
158 | uInt lookahead; /* number of valid bytes ahead in window */
159 |
160 | uInt prev_length;
161 | /* Length of the best match at previous step. Matches not greater than this
162 | * are discarded. This is used in the lazy match evaluation.
163 | */
164 |
165 | uInt max_chain_length;
166 | /* To speed up deflation, hash chains are never searched beyond this
167 | * length. A higher limit improves compression ratio but degrades the
168 | * speed.
169 | */
170 |
171 | uInt max_lazy_match;
172 | /* Attempt to find a better match only when the current match is strictly
173 | * smaller than this value. This mechanism is used only for compression
174 | * levels >= 4.
175 | */
176 | # define max_insert_length max_lazy_match
177 | /* Insert new strings in the hash table only if the match length is not
178 | * greater than this length. This saves time but degrades compression.
179 | * max_insert_length is used only for compression levels <= 3.
180 | */
181 |
182 | int level; /* compression level (1..9) */
183 | int strategy; /* favor or force Huffman coding*/
184 |
185 | uInt good_match;
186 | /* Use a faster search when the previous match is longer than this */
187 |
188 | int nice_match; /* Stop searching when current match exceeds this */
189 |
190 | /* used by trees.c: */
191 | /* Didn't use ct_data typedef below to supress compiler warning */
192 | struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */
193 | struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
194 | struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */
195 |
196 | struct tree_desc_s l_desc; /* desc. for literal tree */
197 | struct tree_desc_s d_desc; /* desc. for distance tree */
198 | struct tree_desc_s bl_desc; /* desc. for bit length tree */
199 |
200 | ush bl_count[MAX_BITS+1];
201 | /* number of codes at each bit length for an optimal tree */
202 |
203 | int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */
204 | int heap_len; /* number of elements in the heap */
205 | int heap_max; /* element of largest frequency */
206 | /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
207 | * The same heap array is used to build all trees.
208 | */
209 |
210 | uch depth[2*L_CODES+1];
211 | /* Depth of each subtree used as tie breaker for trees of equal frequency
212 | */
213 |
214 | uchf *l_buf; /* buffer for literals or lengths */
215 |
216 | uInt lit_bufsize;
217 | /* Size of match buffer for literals/lengths. There are 4 reasons for
218 | * limiting lit_bufsize to 64K:
219 | * - frequencies can be kept in 16 bit counters
220 | * - if compression is not successful for the first block, all input
221 | * data is still in the window so we can still emit a stored block even
222 | * when input comes from standard input. (This can also be done for
223 | * all blocks if lit_bufsize is not greater than 32K.)
224 | * - if compression is not successful for a file smaller than 64K, we can
225 | * even emit a stored file instead of a stored block (saving 5 bytes).
226 | * This is applicable only for zip (not gzip or zlib).
227 | * - creating new Huffman trees less frequently may not provide fast
228 | * adaptation to changes in the input data statistics. (Take for
229 | * example a binary file with poorly compressible code followed by
230 | * a highly compressible string table.) Smaller buffer sizes give
231 | * fast adaptation but have of course the overhead of transmitting
232 | * trees more frequently.
233 | * - I can't count above 4
234 | */
235 |
236 | uInt last_lit; /* running index in l_buf */
237 |
238 | ushf *d_buf;
239 | /* Buffer for distances. To simplify the code, d_buf and l_buf have
240 | * the same number of elements. To use different lengths, an extra flag
241 | * array would be necessary.
242 | */
243 |
244 | ulg opt_len; /* bit length of current block with optimal trees */
245 | ulg static_len; /* bit length of current block with static trees */
246 | uInt matches; /* number of string matches in current block */
247 | int last_eob_len; /* bit length of EOB code for last block */
248 |
249 | #ifdef DEBUG
250 | ulg compressed_len; /* total bit length of compressed file mod 2^32 */
251 | ulg bits_sent; /* bit length of compressed data sent mod 2^32 */
252 | #endif
253 |
254 | ush bi_buf;
255 | /* Output buffer. bits are inserted starting at the bottom (least
256 | * significant bits).
257 | */
258 | int bi_valid;
259 | /* Number of valid bits in bi_buf. All bits above the last valid bit
260 | * are always zero.
261 | */
262 |
263 | ulg high_water;
264 | /* High water mark offset in window for initialized bytes -- bytes above
265 | * this are set to zero in order to avoid memory check warnings when
266 | * longest match routines access bytes past the input. This is then
267 | * updated to the new high water mark.
268 | */
269 |
270 | } FAR deflate_state;
271 |
272 | /* Output a byte on the stream.
273 | * IN assertion: there is enough room in pending_buf.
274 | */
275 | #define put_byte(s, c) {s->pending_buf[s->pending++] = (c);}
276 |
277 |
278 | #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
279 | /* Minimum amount of lookahead, except at the end of the input file.
280 | * See deflate.c for comments about the MIN_MATCH+1.
281 | */
282 |
283 | #define MAX_DIST(s) ((s)->w_size-MIN_LOOKAHEAD)
284 | /* In order to simplify the code, particularly on 16 bit machines, match
285 | * distances are limited to MAX_DIST instead of WSIZE.
286 | */
287 |
288 | #define WIN_INIT MAX_MATCH
289 | /* Number of bytes after end of data in window to initialize in order to avoid
290 | memory checker errors from longest match routines */
291 |
292 | /* in trees.c */
293 | void ZLIB_INTERNAL _tr_init OF((deflate_state *s));
294 | int ZLIB_INTERNAL _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc));
295 | void ZLIB_INTERNAL _tr_flush_block OF((deflate_state *s, charf *buf,
296 | ulg stored_len, int last));
297 | void ZLIB_INTERNAL _tr_align OF((deflate_state *s));
298 | void ZLIB_INTERNAL _tr_stored_block OF((deflate_state *s, charf *buf,
299 | ulg stored_len, int last));
300 |
301 | #define d_code(dist) \
302 | ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)])
303 | /* Mapping from a distance to a distance code. dist is the distance - 1 and
304 | * must not have side effects. _dist_code[256] and _dist_code[257] are never
305 | * used.
306 | */
307 |
308 | #ifndef DEBUG
309 | /* Inline versions of _tr_tally for speed: */
310 |
311 | #if defined(GEN_TREES_H) || !defined(STDC)
312 | extern uch ZLIB_INTERNAL _length_code[];
313 | extern uch ZLIB_INTERNAL _dist_code[];
314 | #else
315 | extern const uch ZLIB_INTERNAL _length_code[];
316 | extern const uch ZLIB_INTERNAL _dist_code[];
317 | #endif
318 |
319 | # define _tr_tally_lit(s, c, flush) \
320 | { uch cc = (c); \
321 | s->d_buf[s->last_lit] = 0; \
322 | s->l_buf[s->last_lit++] = cc; \
323 | s->dyn_ltree[cc].Freq++; \
324 | flush = (s->last_lit == s->lit_bufsize-1); \
325 | }
326 | # define _tr_tally_dist(s, distance, length, flush) \
327 | { uch len = (length); \
328 | ush dist = (distance); \
329 | s->d_buf[s->last_lit] = dist; \
330 | s->l_buf[s->last_lit++] = len; \
331 | dist--; \
332 | s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \
333 | s->dyn_dtree[d_code(dist)].Freq++; \
334 | flush = (s->last_lit == s->lit_bufsize-1); \
335 | }
336 | #else
337 | # define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c)
338 | # define _tr_tally_dist(s, distance, length, flush) \
339 | flush = _tr_tally(s, distance, length)
340 | #endif
341 |
342 | #endif /* DEFLATE_H */
343 |
--------------------------------------------------------------------------------
/src/zlib/gzclose.c:
--------------------------------------------------------------------------------
1 | /* gzclose.c -- zlib gzclose() function
2 | * Copyright (C) 2004, 2010 Mark Adler
3 | * For conditions of distribution and use, see copyright notice in zlib.h
4 | */
5 |
6 | #include "gzguts.h"
7 |
8 | /* gzclose() is in a separate file so that it is linked in only if it is used.
9 | That way the other gzclose functions can be used instead to avoid linking in
10 | unneeded compression or decompression routines. */
11 | int ZEXPORT gzclose(file)
12 | gzFile file;
13 | {
14 | #ifndef NO_GZCOMPRESS
15 | gz_statep state;
16 |
17 | if (file == NULL)
18 | return Z_STREAM_ERROR;
19 | state = (gz_statep)file;
20 |
21 | return state->mode == GZ_READ ? gzclose_r(file) : gzclose_w(file);
22 | #else
23 | return gzclose_r(file);
24 | #endif
25 | }
26 |
--------------------------------------------------------------------------------
/src/zlib/gzguts.h:
--------------------------------------------------------------------------------
1 | /* gzguts.h -- zlib internal header definitions for gz* operations
2 | * Copyright (C) 2004, 2005, 2010 Mark Adler
3 | * For conditions of distribution and use, see copyright notice in zlib.h
4 | */
5 |
6 | #ifdef _LARGEFILE64_SOURCE
7 | # ifndef _LARGEFILE_SOURCE
8 | # define _LARGEFILE_SOURCE 1
9 | # endif
10 | # ifdef _FILE_OFFSET_BITS
11 | # undef _FILE_OFFSET_BITS
12 | # endif
13 | #endif
14 |
15 | #if ((__GNUC__-0) * 10 + __GNUC_MINOR__-0 >= 33) && !defined(NO_VIZ)
16 | # define ZLIB_INTERNAL __attribute__((visibility ("hidden")))
17 | #else
18 | # define ZLIB_INTERNAL
19 | #endif
20 |
21 | #include
22 | #include "zlib.h"
23 | #ifdef STDC
24 | # include
25 | # include
26 | # include
27 | #endif
28 | #include
29 |
30 | #ifdef NO_DEFLATE /* for compatibility with old definition */
31 | # define NO_GZCOMPRESS
32 | #endif
33 |
34 | #ifdef _MSC_VER
35 | # include
36 | # define vsnprintf _vsnprintf
37 | #endif
38 |
39 | #ifndef local
40 | # define local static
41 | #endif
42 | /* compile with -Dlocal if your debugger can't find static symbols */
43 |
44 | /* gz* functions always use library allocation functions */
45 | #ifndef STDC
46 | extern voidp malloc OF((uInt size));
47 | extern void free OF((voidpf ptr));
48 | #endif
49 |
50 | /* get errno and strerror definition */
51 | #if defined UNDER_CE
52 | # include
53 | # define zstrerror() gz_strwinerror((DWORD)GetLastError())
54 | #else
55 | # ifdef STDC
56 | # include
57 | # define zstrerror() strerror(errno)
58 | # else
59 | # define zstrerror() "stdio error (consult errno)"
60 | # endif
61 | #endif
62 |
63 | /* provide prototypes for these when building zlib without LFS */
64 | #if !defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0
65 | ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *));
66 | ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int));
67 | ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile));
68 | ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile));
69 | #endif
70 |
71 | /* default i/o buffer size -- double this for output when reading */
72 | #define GZBUFSIZE 8192
73 |
74 | /* gzip modes, also provide a little integrity check on the passed structure */
75 | #define GZ_NONE 0
76 | #define GZ_READ 7247
77 | #define GZ_WRITE 31153
78 | #define GZ_APPEND 1 /* mode set to GZ_WRITE after the file is opened */
79 |
80 | /* values for gz_state how */
81 | #define LOOK 0 /* look for a gzip header */
82 | #define COPY 1 /* copy input directly */
83 | #define GZIP 2 /* decompress a gzip stream */
84 |
85 | /* internal gzip file state data structure */
86 | typedef struct {
87 | /* used for both reading and writing */
88 | int mode; /* see gzip modes above */
89 | int fd; /* file descriptor */
90 | char *path; /* path or fd for error messages */
91 | z_off64_t pos; /* current position in uncompressed data */
92 | unsigned size; /* buffer size, zero if not allocated yet */
93 | unsigned want; /* requested buffer size, default is GZBUFSIZE */
94 | unsigned char *in; /* input buffer */
95 | unsigned char *out; /* output buffer (double-sized when reading) */
96 | unsigned char *next; /* next output data to deliver or write */
97 | /* just for reading */
98 | unsigned have; /* amount of output data unused at next */
99 | int eof; /* true if end of input file reached */
100 | z_off64_t start; /* where the gzip data started, for rewinding */
101 | z_off64_t raw; /* where the raw data started, for seeking */
102 | int how; /* 0: get header, 1: copy, 2: decompress */
103 | int direct; /* true if last read direct, false if gzip */
104 | /* just for writing */
105 | int level; /* compression level */
106 | int strategy; /* compression strategy */
107 | /* seek request */
108 | z_off64_t skip; /* amount to skip (already rewound if backwards) */
109 | int seek; /* true if seek request pending */
110 | /* error information */
111 | int err; /* error code */
112 | char *msg; /* error message */
113 | /* zlib inflate or deflate stream */
114 | z_stream strm; /* stream structure in-place (not a pointer) */
115 | } gz_state;
116 | typedef gz_state FAR *gz_statep;
117 |
118 | /* shared functions */
119 | void ZLIB_INTERNAL gz_error OF((gz_statep, int, const char *));
120 | #if defined UNDER_CE
121 | char ZLIB_INTERNAL *gz_strwinerror OF((DWORD error));
122 | #endif
123 |
124 | /* GT_OFF(x), where x is an unsigned value, is true if x > maximum z_off64_t
125 | value -- needed when comparing unsigned to z_off64_t, which is signed
126 | (possible z_off64_t types off_t, off64_t, and long are all signed) */
127 | #ifdef INT_MAX
128 | # define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > INT_MAX)
129 | #else
130 | unsigned ZLIB_INTERNAL gz_intmax OF((void));
131 | # define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > gz_intmax())
132 | #endif
133 |
--------------------------------------------------------------------------------
/src/zlib/inffast.c:
--------------------------------------------------------------------------------
1 | /* inffast.c -- fast decoding
2 | * Copyright (C) 1995-2008, 2010 Mark Adler
3 | * For conditions of distribution and use, see copyright notice in zlib.h
4 | */
5 |
6 | #include "zutil.h"
7 | #include "inftrees.h"
8 | #include "inflate.h"
9 | #include "inffast.h"
10 |
11 | #ifndef ASMINF
12 |
13 | /* Allow machine dependent optimization for post-increment or pre-increment.
14 | Based on testing to date,
15 | Pre-increment preferred for:
16 | - PowerPC G3 (Adler)
17 | - MIPS R5000 (Randers-Pehrson)
18 | Post-increment preferred for:
19 | - none
20 | No measurable difference:
21 | - Pentium III (Anderson)
22 | - M68060 (Nikl)
23 | */
24 | #ifdef POSTINC
25 | # define OFF 0
26 | # define PUP(a) *(a)++
27 | #else
28 | # define OFF 1
29 | # define PUP(a) *++(a)
30 | #endif
31 |
32 | /*
33 | Decode literal, length, and distance codes and write out the resulting
34 | literal and match bytes until either not enough input or output is
35 | available, an end-of-block is encountered, or a data error is encountered.
36 | When large enough input and output buffers are supplied to inflate(), for
37 | example, a 16K input buffer and a 64K output buffer, more than 95% of the
38 | inflate execution time is spent in this routine.
39 |
40 | Entry assumptions:
41 |
42 | state->mode == LEN
43 | strm->avail_in >= 6
44 | strm->avail_out >= 258
45 | start >= strm->avail_out
46 | state->bits < 8
47 |
48 | On return, state->mode is one of:
49 |
50 | LEN -- ran out of enough output space or enough available input
51 | TYPE -- reached end of block code, inflate() to interpret next block
52 | BAD -- error in block data
53 |
54 | Notes:
55 |
56 | - The maximum input bits used by a length/distance pair is 15 bits for the
57 | length code, 5 bits for the length extra, 15 bits for the distance code,
58 | and 13 bits for the distance extra. This totals 48 bits, or six bytes.
59 | Therefore if strm->avail_in >= 6, then there is enough input to avoid
60 | checking for available input while decoding.
61 |
62 | - The maximum bytes that a single length/distance pair can output is 258
63 | bytes, which is the maximum length that can be coded. inflate_fast()
64 | requires strm->avail_out >= 258 for each loop to avoid checking for
65 | output space.
66 | */
67 | void ZLIB_INTERNAL inflate_fast(strm, start)
68 | z_streamp strm;
69 | unsigned start; /* inflate()'s starting value for strm->avail_out */
70 | {
71 | struct inflate_state FAR *state;
72 | unsigned char FAR *in; /* local strm->next_in */
73 | unsigned char FAR *last; /* while in < last, enough input available */
74 | unsigned char FAR *out; /* local strm->next_out */
75 | unsigned char FAR *beg; /* inflate()'s initial strm->next_out */
76 | unsigned char FAR *end; /* while out < end, enough space available */
77 | #ifdef INFLATE_STRICT
78 | unsigned dmax; /* maximum distance from zlib header */
79 | #endif
80 | unsigned wsize; /* window size or zero if not using window */
81 | unsigned whave; /* valid bytes in the window */
82 | unsigned wnext; /* window write index */
83 | unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */
84 | unsigned long hold; /* local strm->hold */
85 | unsigned bits; /* local strm->bits */
86 | code const FAR *lcode; /* local strm->lencode */
87 | code const FAR *dcode; /* local strm->distcode */
88 | unsigned lmask; /* mask for first level of length codes */
89 | unsigned dmask; /* mask for first level of distance codes */
90 | code here; /* retrieved table entry */
91 | unsigned op; /* code bits, operation, extra bits, or */
92 | /* window position, window bytes to copy */
93 | unsigned len; /* match length, unused bytes */
94 | unsigned dist; /* match distance */
95 | unsigned char FAR *from; /* where to copy match from */
96 |
97 | /* copy state to local variables */
98 | state = (struct inflate_state FAR *)strm->state;
99 | in = strm->next_in - OFF;
100 | last = in + (strm->avail_in - 5);
101 | out = strm->next_out - OFF;
102 | beg = out - (start - strm->avail_out);
103 | end = out + (strm->avail_out - 257);
104 | #ifdef INFLATE_STRICT
105 | dmax = state->dmax;
106 | #endif
107 | wsize = state->wsize;
108 | whave = state->whave;
109 | wnext = state->wnext;
110 | window = state->window;
111 | hold = state->hold;
112 | bits = state->bits;
113 | lcode = state->lencode;
114 | dcode = state->distcode;
115 | lmask = (1U << state->lenbits) - 1;
116 | dmask = (1U << state->distbits) - 1;
117 |
118 | /* decode literals and length/distances until end-of-block or not enough
119 | input data or output space */
120 | do {
121 | if (bits < 15) {
122 | hold += (unsigned long)(PUP(in)) << bits;
123 | bits += 8;
124 | hold += (unsigned long)(PUP(in)) << bits;
125 | bits += 8;
126 | }
127 | here = lcode[hold & lmask];
128 | dolen:
129 | op = (unsigned)(here.bits);
130 | hold >>= op;
131 | bits -= op;
132 | op = (unsigned)(here.op);
133 | if (op == 0) { /* literal */
134 | Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
135 | "inflate: literal '%c'\n" :
136 | "inflate: literal 0x%02x\n", here.val));
137 | PUP(out) = (unsigned char)(here.val);
138 | }
139 | else if (op & 16) { /* length base */
140 | len = (unsigned)(here.val);
141 | op &= 15; /* number of extra bits */
142 | if (op) {
143 | if (bits < op) {
144 | hold += (unsigned long)(PUP(in)) << bits;
145 | bits += 8;
146 | }
147 | len += (unsigned)hold & ((1U << op) - 1);
148 | hold >>= op;
149 | bits -= op;
150 | }
151 | Tracevv((stderr, "inflate: length %u\n", len));
152 | if (bits < 15) {
153 | hold += (unsigned long)(PUP(in)) << bits;
154 | bits += 8;
155 | hold += (unsigned long)(PUP(in)) << bits;
156 | bits += 8;
157 | }
158 | here = dcode[hold & dmask];
159 | dodist:
160 | op = (unsigned)(here.bits);
161 | hold >>= op;
162 | bits -= op;
163 | op = (unsigned)(here.op);
164 | if (op & 16) { /* distance base */
165 | dist = (unsigned)(here.val);
166 | op &= 15; /* number of extra bits */
167 | if (bits < op) {
168 | hold += (unsigned long)(PUP(in)) << bits;
169 | bits += 8;
170 | if (bits < op) {
171 | hold += (unsigned long)(PUP(in)) << bits;
172 | bits += 8;
173 | }
174 | }
175 | dist += (unsigned)hold & ((1U << op) - 1);
176 | #ifdef INFLATE_STRICT
177 | if (dist > dmax) {
178 | strm->msg = (char *)"invalid distance too far back";
179 | state->mode = BAD;
180 | break;
181 | }
182 | #endif
183 | hold >>= op;
184 | bits -= op;
185 | Tracevv((stderr, "inflate: distance %u\n", dist));
186 | op = (unsigned)(out - beg); /* max distance in output */
187 | if (dist > op) { /* see if copy from window */
188 | op = dist - op; /* distance back in window */
189 | if (op > whave) {
190 | if (state->sane) {
191 | strm->msg =
192 | (char *)"invalid distance too far back";
193 | state->mode = BAD;
194 | break;
195 | }
196 | #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
197 | if (len <= op - whave) {
198 | do {
199 | PUP(out) = 0;
200 | } while (--len);
201 | continue;
202 | }
203 | len -= op - whave;
204 | do {
205 | PUP(out) = 0;
206 | } while (--op > whave);
207 | if (op == 0) {
208 | from = out - dist;
209 | do {
210 | PUP(out) = PUP(from);
211 | } while (--len);
212 | continue;
213 | }
214 | #endif
215 | }
216 | from = window - OFF;
217 | if (wnext == 0) { /* very common case */
218 | from += wsize - op;
219 | if (op < len) { /* some from window */
220 | len -= op;
221 | do {
222 | PUP(out) = PUP(from);
223 | } while (--op);
224 | from = out - dist; /* rest from output */
225 | }
226 | }
227 | else if (wnext < op) { /* wrap around window */
228 | from += wsize + wnext - op;
229 | op -= wnext;
230 | if (op < len) { /* some from end of window */
231 | len -= op;
232 | do {
233 | PUP(out) = PUP(from);
234 | } while (--op);
235 | from = window - OFF;
236 | if (wnext < len) { /* some from start of window */
237 | op = wnext;
238 | len -= op;
239 | do {
240 | PUP(out) = PUP(from);
241 | } while (--op);
242 | from = out - dist; /* rest from output */
243 | }
244 | }
245 | }
246 | else { /* contiguous in window */
247 | from += wnext - op;
248 | if (op < len) { /* some from window */
249 | len -= op;
250 | do {
251 | PUP(out) = PUP(from);
252 | } while (--op);
253 | from = out - dist; /* rest from output */
254 | }
255 | }
256 | while (len > 2) {
257 | PUP(out) = PUP(from);
258 | PUP(out) = PUP(from);
259 | PUP(out) = PUP(from);
260 | len -= 3;
261 | }
262 | if (len) {
263 | PUP(out) = PUP(from);
264 | if (len > 1)
265 | PUP(out) = PUP(from);
266 | }
267 | }
268 | else {
269 | from = out - dist; /* copy direct from output */
270 | do { /* minimum length is three */
271 | PUP(out) = PUP(from);
272 | PUP(out) = PUP(from);
273 | PUP(out) = PUP(from);
274 | len -= 3;
275 | } while (len > 2);
276 | if (len) {
277 | PUP(out) = PUP(from);
278 | if (len > 1)
279 | PUP(out) = PUP(from);
280 | }
281 | }
282 | }
283 | else if ((op & 64) == 0) { /* 2nd level distance code */
284 | here = dcode[here.val + (hold & ((1U << op) - 1))];
285 | goto dodist;
286 | }
287 | else {
288 | strm->msg = (char *)"invalid distance code";
289 | state->mode = BAD;
290 | break;
291 | }
292 | }
293 | else if ((op & 64) == 0) { /* 2nd level length code */
294 | here = lcode[here.val + (hold & ((1U << op) - 1))];
295 | goto dolen;
296 | }
297 | else if (op & 32) { /* end-of-block */
298 | Tracevv((stderr, "inflate: end of block\n"));
299 | state->mode = TYPE;
300 | break;
301 | }
302 | else {
303 | strm->msg = (char *)"invalid literal/length code";
304 | state->mode = BAD;
305 | break;
306 | }
307 | } while (in < last && out < end);
308 |
309 | /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
310 | len = bits >> 3;
311 | in -= len;
312 | bits -= len << 3;
313 | hold &= (1U << bits) - 1;
314 |
315 | /* update state and return */
316 | strm->next_in = in + OFF;
317 | strm->next_out = out + OFF;
318 | strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
319 | strm->avail_out = (unsigned)(out < end ?
320 | 257 + (end - out) : 257 - (out - end));
321 | state->hold = hold;
322 | state->bits = bits;
323 | return;
324 | }
325 |
326 | /*
327 | inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
328 | - Using bit fields for code structure
329 | - Different op definition to avoid & for extra bits (do & for table bits)
330 | - Three separate decoding do-loops for direct, window, and wnext == 0
331 | - Special case for distance > 1 copies to do overlapped load and store copy
332 | - Explicit branch predictions (based on measured branch probabilities)
333 | - Deferring match copy and interspersed it with decoding subsequent codes
334 | - Swapping literal/length else
335 | - Swapping window/direct else
336 | - Larger unrolled copy loops (three is about right)
337 | - Moving len -= 3 statement into middle of loop
338 | */
339 |
340 | #endif /* !ASMINF */
341 |
--------------------------------------------------------------------------------
/src/zlib/inffast.h:
--------------------------------------------------------------------------------
1 | /* inffast.h -- header to use inffast.c
2 | * Copyright (C) 1995-2003, 2010 Mark Adler
3 | * For conditions of distribution and use, see copyright notice in zlib.h
4 | */
5 |
6 | /* WARNING: this file should *not* be used by applications. It is
7 | part of the implementation of the compression library and is
8 | subject to change. Applications should only use zlib.h.
9 | */
10 |
11 | void ZLIB_INTERNAL inflate_fast OF((z_streamp strm, unsigned start));
12 |
--------------------------------------------------------------------------------
/src/zlib/inffixed.h:
--------------------------------------------------------------------------------
1 | /* inffixed.h -- table for decoding fixed codes
2 | * Generated automatically by makefixed().
3 | */
4 |
5 | /* WARNING: this file should *not* be used by applications. It
6 | is part of the implementation of the compression library and
7 | is subject to change. Applications should only use zlib.h.
8 | */
9 |
10 | static const code lenfix[512] = {
11 | {96,7,0},{0,8,80},{0,8,16},{20,8,115},{18,7,31},{0,8,112},{0,8,48},
12 | {0,9,192},{16,7,10},{0,8,96},{0,8,32},{0,9,160},{0,8,0},{0,8,128},
13 | {0,8,64},{0,9,224},{16,7,6},{0,8,88},{0,8,24},{0,9,144},{19,7,59},
14 | {0,8,120},{0,8,56},{0,9,208},{17,7,17},{0,8,104},{0,8,40},{0,9,176},
15 | {0,8,8},{0,8,136},{0,8,72},{0,9,240},{16,7,4},{0,8,84},{0,8,20},
16 | {21,8,227},{19,7,43},{0,8,116},{0,8,52},{0,9,200},{17,7,13},{0,8,100},
17 | {0,8,36},{0,9,168},{0,8,4},{0,8,132},{0,8,68},{0,9,232},{16,7,8},
18 | {0,8,92},{0,8,28},{0,9,152},{20,7,83},{0,8,124},{0,8,60},{0,9,216},
19 | {18,7,23},{0,8,108},{0,8,44},{0,9,184},{0,8,12},{0,8,140},{0,8,76},
20 | {0,9,248},{16,7,3},{0,8,82},{0,8,18},{21,8,163},{19,7,35},{0,8,114},
21 | {0,8,50},{0,9,196},{17,7,11},{0,8,98},{0,8,34},{0,9,164},{0,8,2},
22 | {0,8,130},{0,8,66},{0,9,228},{16,7,7},{0,8,90},{0,8,26},{0,9,148},
23 | {20,7,67},{0,8,122},{0,8,58},{0,9,212},{18,7,19},{0,8,106},{0,8,42},
24 | {0,9,180},{0,8,10},{0,8,138},{0,8,74},{0,9,244},{16,7,5},{0,8,86},
25 | {0,8,22},{64,8,0},{19,7,51},{0,8,118},{0,8,54},{0,9,204},{17,7,15},
26 | {0,8,102},{0,8,38},{0,9,172},{0,8,6},{0,8,134},{0,8,70},{0,9,236},
27 | {16,7,9},{0,8,94},{0,8,30},{0,9,156},{20,7,99},{0,8,126},{0,8,62},
28 | {0,9,220},{18,7,27},{0,8,110},{0,8,46},{0,9,188},{0,8,14},{0,8,142},
29 | {0,8,78},{0,9,252},{96,7,0},{0,8,81},{0,8,17},{21,8,131},{18,7,31},
30 | {0,8,113},{0,8,49},{0,9,194},{16,7,10},{0,8,97},{0,8,33},{0,9,162},
31 | {0,8,1},{0,8,129},{0,8,65},{0,9,226},{16,7,6},{0,8,89},{0,8,25},
32 | {0,9,146},{19,7,59},{0,8,121},{0,8,57},{0,9,210},{17,7,17},{0,8,105},
33 | {0,8,41},{0,9,178},{0,8,9},{0,8,137},{0,8,73},{0,9,242},{16,7,4},
34 | {0,8,85},{0,8,21},{16,8,258},{19,7,43},{0,8,117},{0,8,53},{0,9,202},
35 | {17,7,13},{0,8,101},{0,8,37},{0,9,170},{0,8,5},{0,8,133},{0,8,69},
36 | {0,9,234},{16,7,8},{0,8,93},{0,8,29},{0,9,154},{20,7,83},{0,8,125},
37 | {0,8,61},{0,9,218},{18,7,23},{0,8,109},{0,8,45},{0,9,186},{0,8,13},
38 | {0,8,141},{0,8,77},{0,9,250},{16,7,3},{0,8,83},{0,8,19},{21,8,195},
39 | {19,7,35},{0,8,115},{0,8,51},{0,9,198},{17,7,11},{0,8,99},{0,8,35},
40 | {0,9,166},{0,8,3},{0,8,131},{0,8,67},{0,9,230},{16,7,7},{0,8,91},
41 | {0,8,27},{0,9,150},{20,7,67},{0,8,123},{0,8,59},{0,9,214},{18,7,19},
42 | {0,8,107},{0,8,43},{0,9,182},{0,8,11},{0,8,139},{0,8,75},{0,9,246},
43 | {16,7,5},{0,8,87},{0,8,23},{64,8,0},{19,7,51},{0,8,119},{0,8,55},
44 | {0,9,206},{17,7,15},{0,8,103},{0,8,39},{0,9,174},{0,8,7},{0,8,135},
45 | {0,8,71},{0,9,238},{16,7,9},{0,8,95},{0,8,31},{0,9,158},{20,7,99},
46 | {0,8,127},{0,8,63},{0,9,222},{18,7,27},{0,8,111},{0,8,47},{0,9,190},
47 | {0,8,15},{0,8,143},{0,8,79},{0,9,254},{96,7,0},{0,8,80},{0,8,16},
48 | {20,8,115},{18,7,31},{0,8,112},{0,8,48},{0,9,193},{16,7,10},{0,8,96},
49 | {0,8,32},{0,9,161},{0,8,0},{0,8,128},{0,8,64},{0,9,225},{16,7,6},
50 | {0,8,88},{0,8,24},{0,9,145},{19,7,59},{0,8,120},{0,8,56},{0,9,209},
51 | {17,7,17},{0,8,104},{0,8,40},{0,9,177},{0,8,8},{0,8,136},{0,8,72},
52 | {0,9,241},{16,7,4},{0,8,84},{0,8,20},{21,8,227},{19,7,43},{0,8,116},
53 | {0,8,52},{0,9,201},{17,7,13},{0,8,100},{0,8,36},{0,9,169},{0,8,4},
54 | {0,8,132},{0,8,68},{0,9,233},{16,7,8},{0,8,92},{0,8,28},{0,9,153},
55 | {20,7,83},{0,8,124},{0,8,60},{0,9,217},{18,7,23},{0,8,108},{0,8,44},
56 | {0,9,185},{0,8,12},{0,8,140},{0,8,76},{0,9,249},{16,7,3},{0,8,82},
57 | {0,8,18},{21,8,163},{19,7,35},{0,8,114},{0,8,50},{0,9,197},{17,7,11},
58 | {0,8,98},{0,8,34},{0,9,165},{0,8,2},{0,8,130},{0,8,66},{0,9,229},
59 | {16,7,7},{0,8,90},{0,8,26},{0,9,149},{20,7,67},{0,8,122},{0,8,58},
60 | {0,9,213},{18,7,19},{0,8,106},{0,8,42},{0,9,181},{0,8,10},{0,8,138},
61 | {0,8,74},{0,9,245},{16,7,5},{0,8,86},{0,8,22},{64,8,0},{19,7,51},
62 | {0,8,118},{0,8,54},{0,9,205},{17,7,15},{0,8,102},{0,8,38},{0,9,173},
63 | {0,8,6},{0,8,134},{0,8,70},{0,9,237},{16,7,9},{0,8,94},{0,8,30},
64 | {0,9,157},{20,7,99},{0,8,126},{0,8,62},{0,9,221},{18,7,27},{0,8,110},
65 | {0,8,46},{0,9,189},{0,8,14},{0,8,142},{0,8,78},{0,9,253},{96,7,0},
66 | {0,8,81},{0,8,17},{21,8,131},{18,7,31},{0,8,113},{0,8,49},{0,9,195},
67 | {16,7,10},{0,8,97},{0,8,33},{0,9,163},{0,8,1},{0,8,129},{0,8,65},
68 | {0,9,227},{16,7,6},{0,8,89},{0,8,25},{0,9,147},{19,7,59},{0,8,121},
69 | {0,8,57},{0,9,211},{17,7,17},{0,8,105},{0,8,41},{0,9,179},{0,8,9},
70 | {0,8,137},{0,8,73},{0,9,243},{16,7,4},{0,8,85},{0,8,21},{16,8,258},
71 | {19,7,43},{0,8,117},{0,8,53},{0,9,203},{17,7,13},{0,8,101},{0,8,37},
72 | {0,9,171},{0,8,5},{0,8,133},{0,8,69},{0,9,235},{16,7,8},{0,8,93},
73 | {0,8,29},{0,9,155},{20,7,83},{0,8,125},{0,8,61},{0,9,219},{18,7,23},
74 | {0,8,109},{0,8,45},{0,9,187},{0,8,13},{0,8,141},{0,8,77},{0,9,251},
75 | {16,7,3},{0,8,83},{0,8,19},{21,8,195},{19,7,35},{0,8,115},{0,8,51},
76 | {0,9,199},{17,7,11},{0,8,99},{0,8,35},{0,9,167},{0,8,3},{0,8,131},
77 | {0,8,67},{0,9,231},{16,7,7},{0,8,91},{0,8,27},{0,9,151},{20,7,67},
78 | {0,8,123},{0,8,59},{0,9,215},{18,7,19},{0,8,107},{0,8,43},{0,9,183},
79 | {0,8,11},{0,8,139},{0,8,75},{0,9,247},{16,7,5},{0,8,87},{0,8,23},
80 | {64,8,0},{19,7,51},{0,8,119},{0,8,55},{0,9,207},{17,7,15},{0,8,103},
81 | {0,8,39},{0,9,175},{0,8,7},{0,8,135},{0,8,71},{0,9,239},{16,7,9},
82 | {0,8,95},{0,8,31},{0,9,159},{20,7,99},{0,8,127},{0,8,63},{0,9,223},
83 | {18,7,27},{0,8,111},{0,8,47},{0,9,191},{0,8,15},{0,8,143},{0,8,79},
84 | {0,9,255}
85 | };
86 |
87 | static const code distfix[32] = {
88 | {16,5,1},{23,5,257},{19,5,17},{27,5,4097},{17,5,5},{25,5,1025},
89 | {21,5,65},{29,5,16385},{16,5,3},{24,5,513},{20,5,33},{28,5,8193},
90 | {18,5,9},{26,5,2049},{22,5,129},{64,5,0},{16,5,2},{23,5,385},
91 | {19,5,25},{27,5,6145},{17,5,7},{25,5,1537},{21,5,97},{29,5,24577},
92 | {16,5,4},{24,5,769},{20,5,49},{28,5,12289},{18,5,13},{26,5,3073},
93 | {22,5,193},{64,5,0}
94 | };
95 |
--------------------------------------------------------------------------------
/src/zlib/inflate.h:
--------------------------------------------------------------------------------
1 | /* inflate.h -- internal inflate state definition
2 | * Copyright (C) 1995-2009 Mark Adler
3 | * For conditions of distribution and use, see copyright notice in zlib.h
4 | */
5 |
6 | /* WARNING: this file should *not* be used by applications. It is
7 | part of the implementation of the compression library and is
8 | subject to change. Applications should only use zlib.h.
9 | */
10 |
11 | /* define NO_GZIP when compiling if you want to disable gzip header and
12 | trailer decoding by inflate(). NO_GZIP would be used to avoid linking in
13 | the crc code when it is not needed. For shared libraries, gzip decoding
14 | should be left enabled. */
15 | #ifndef NO_GZIP
16 | # define GUNZIP
17 | #endif
18 |
19 | /* Possible inflate modes between inflate() calls */
20 | typedef enum {
21 | HEAD, /* i: waiting for magic header */
22 | FLAGS, /* i: waiting for method and flags (gzip) */
23 | TIME, /* i: waiting for modification time (gzip) */
24 | OS, /* i: waiting for extra flags and operating system (gzip) */
25 | EXLEN, /* i: waiting for extra length (gzip) */
26 | EXTRA, /* i: waiting for extra bytes (gzip) */
27 | NAME, /* i: waiting for end of file name (gzip) */
28 | COMMENT, /* i: waiting for end of comment (gzip) */
29 | HCRC, /* i: waiting for header crc (gzip) */
30 | DICTID, /* i: waiting for dictionary check value */
31 | DICT, /* waiting for inflateSetDictionary() call */
32 | TYPE, /* i: waiting for type bits, including last-flag bit */
33 | TYPEDO, /* i: same, but skip check to exit inflate on new block */
34 | STORED, /* i: waiting for stored size (length and complement) */
35 | COPY_, /* i/o: same as COPY below, but only first time in */
36 | COPY, /* i/o: waiting for input or output to copy stored block */
37 | TABLE, /* i: waiting for dynamic block table lengths */
38 | LENLENS, /* i: waiting for code length code lengths */
39 | CODELENS, /* i: waiting for length/lit and distance code lengths */
40 | LEN_, /* i: same as LEN below, but only first time in */
41 | LEN, /* i: waiting for length/lit/eob code */
42 | LENEXT, /* i: waiting for length extra bits */
43 | DIST, /* i: waiting for distance code */
44 | DISTEXT, /* i: waiting for distance extra bits */
45 | MATCH, /* o: waiting for output space to copy string */
46 | LIT, /* o: waiting for output space to write literal */
47 | CHECK, /* i: waiting for 32-bit check value */
48 | LENGTH, /* i: waiting for 32-bit length (gzip) */
49 | DONE, /* finished check, done -- remain here until reset */
50 | BAD, /* got a data error -- remain here until reset */
51 | MEM, /* got an inflate() memory error -- remain here until reset */
52 | SYNC /* looking for synchronization bytes to restart inflate() */
53 | } inflate_mode;
54 |
55 | /*
56 | State transitions between above modes -
57 |
58 | (most modes can go to BAD or MEM on error -- not shown for clarity)
59 |
60 | Process header:
61 | HEAD -> (gzip) or (zlib) or (raw)
62 | (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME -> COMMENT ->
63 | HCRC -> TYPE
64 | (zlib) -> DICTID or TYPE
65 | DICTID -> DICT -> TYPE
66 | (raw) -> TYPEDO
67 | Read deflate blocks:
68 | TYPE -> TYPEDO -> STORED or TABLE or LEN_ or CHECK
69 | STORED -> COPY_ -> COPY -> TYPE
70 | TABLE -> LENLENS -> CODELENS -> LEN_
71 | LEN_ -> LEN
72 | Read deflate codes in fixed or dynamic block:
73 | LEN -> LENEXT or LIT or TYPE
74 | LENEXT -> DIST -> DISTEXT -> MATCH -> LEN
75 | LIT -> LEN
76 | Process trailer:
77 | CHECK -> LENGTH -> DONE
78 | */
79 |
80 | /* state maintained between inflate() calls. Approximately 10K bytes. */
81 | struct inflate_state {
82 | inflate_mode mode; /* current inflate mode */
83 | int last; /* true if processing last block */
84 | int wrap; /* bit 0 true for zlib, bit 1 true for gzip */
85 | int havedict; /* true if dictionary provided */
86 | int flags; /* gzip header method and flags (0 if zlib) */
87 | unsigned dmax; /* zlib header max distance (INFLATE_STRICT) */
88 | unsigned long check; /* protected copy of check value */
89 | unsigned long total; /* protected copy of output count */
90 | gz_headerp head; /* where to save gzip header information */
91 | /* sliding window */
92 | unsigned wbits; /* log base 2 of requested window size */
93 | unsigned wsize; /* window size or zero if not using window */
94 | unsigned whave; /* valid bytes in the window */
95 | unsigned wnext; /* window write index */
96 | unsigned char FAR *window; /* allocated sliding window, if needed */
97 | /* bit accumulator */
98 | unsigned long hold; /* input bit accumulator */
99 | unsigned bits; /* number of bits in "in" */
100 | /* for string and stored block copying */
101 | unsigned length; /* literal or length of data to copy */
102 | unsigned offset; /* distance back to copy string from */
103 | /* for table and code decoding */
104 | unsigned extra; /* extra bits needed */
105 | /* fixed and dynamic code tables */
106 | code const FAR *lencode; /* starting table for length/literal codes */
107 | code const FAR *distcode; /* starting table for distance codes */
108 | unsigned lenbits; /* index bits for lencode */
109 | unsigned distbits; /* index bits for distcode */
110 | /* dynamic table building */
111 | unsigned ncode; /* number of code length code lengths */
112 | unsigned nlen; /* number of length code lengths */
113 | unsigned ndist; /* number of distance code lengths */
114 | unsigned have; /* number of code lengths in lens[] */
115 | code FAR *next; /* next available space in codes[] */
116 | unsigned short lens[320]; /* temporary storage for code lengths */
117 | unsigned short work[288]; /* work area for code table building */
118 | code codes[ENOUGH]; /* space for code tables */
119 | int sane; /* if false, allow invalid distance too far */
120 | int back; /* bits back of last unprocessed length/lit */
121 | unsigned was; /* initial length of match */
122 | };
123 |
--------------------------------------------------------------------------------
/src/zlib/inftrees.h:
--------------------------------------------------------------------------------
1 | /* inftrees.h -- header to use inftrees.c
2 | * Copyright (C) 1995-2005, 2010 Mark Adler
3 | * For conditions of distribution and use, see copyright notice in zlib.h
4 | */
5 |
6 | /* WARNING: this file should *not* be used by applications. It is
7 | part of the implementation of the compression library and is
8 | subject to change. Applications should only use zlib.h.
9 | */
10 |
11 | /* Structure for decoding tables. Each entry provides either the
12 | information needed to do the operation requested by the code that
13 | indexed that table entry, or it provides a pointer to another
14 | table that indexes more bits of the code. op indicates whether
15 | the entry is a pointer to another table, a literal, a length or
16 | distance, an end-of-block, or an invalid code. For a table
17 | pointer, the low four bits of op is the number of index bits of
18 | that table. For a length or distance, the low four bits of op
19 | is the number of extra bits to get after the code. bits is
20 | the number of bits in this code or part of the code to drop off
21 | of the bit buffer. val is the actual byte to output in the case
22 | of a literal, the base length or distance, or the offset from
23 | the current table to the next table. Each entry is four bytes. */
24 | typedef struct {
25 | unsigned char op; /* operation, extra bits, table bits */
26 | unsigned char bits; /* bits in this part of the code */
27 | unsigned short val; /* offset in table or code value */
28 | } code;
29 |
30 | /* op values as set by inflate_table():
31 | 00000000 - literal
32 | 0000tttt - table link, tttt != 0 is the number of table index bits
33 | 0001eeee - length or distance, eeee is the number of extra bits
34 | 01100000 - end of block
35 | 01000000 - invalid code
36 | */
37 |
38 | /* Maximum size of the dynamic table. The maximum number of code structures is
39 | 1444, which is the sum of 852 for literal/length codes and 592 for distance
40 | codes. These values were found by exhaustive searches using the program
41 | examples/enough.c found in the zlib distribtution. The arguments to that
42 | program are the number of symbols, the initial root table size, and the
43 | maximum bit length of a code. "enough 286 9 15" for literal/length codes
44 | returns returns 852, and "enough 30 6 15" for distance codes returns 592.
45 | The initial root table size (9 or 6) is found in the fifth argument of the
46 | inflate_table() calls in inflate.c and infback.c. If the root table size is
47 | changed, then these maximum sizes would be need to be recalculated and
48 | updated. */
49 | #define ENOUGH_LENS 852
50 | #define ENOUGH_DISTS 592
51 | #define ENOUGH (ENOUGH_LENS+ENOUGH_DISTS)
52 |
53 | /* Type of code to build for inflate_table() */
54 | typedef enum {
55 | CODES,
56 | LENS,
57 | DISTS
58 | } codetype;
59 |
60 | int ZLIB_INTERNAL inflate_table OF((codetype type, unsigned short FAR *lens,
61 | unsigned codes, code FAR * FAR *table,
62 | unsigned FAR *bits, unsigned short FAR *work));
63 |
--------------------------------------------------------------------------------
/src/zlib/trees.h:
--------------------------------------------------------------------------------
1 | /* header created automatically with -DGEN_TREES_H */
2 |
3 | local const ct_data static_ltree[L_CODES+2] = {
4 | {{ 12},{ 8}}, {{140},{ 8}}, {{ 76},{ 8}}, {{204},{ 8}}, {{ 44},{ 8}},
5 | {{172},{ 8}}, {{108},{ 8}}, {{236},{ 8}}, {{ 28},{ 8}}, {{156},{ 8}},
6 | {{ 92},{ 8}}, {{220},{ 8}}, {{ 60},{ 8}}, {{188},{ 8}}, {{124},{ 8}},
7 | {{252},{ 8}}, {{ 2},{ 8}}, {{130},{ 8}}, {{ 66},{ 8}}, {{194},{ 8}},
8 | {{ 34},{ 8}}, {{162},{ 8}}, {{ 98},{ 8}}, {{226},{ 8}}, {{ 18},{ 8}},
9 | {{146},{ 8}}, {{ 82},{ 8}}, {{210},{ 8}}, {{ 50},{ 8}}, {{178},{ 8}},
10 | {{114},{ 8}}, {{242},{ 8}}, {{ 10},{ 8}}, {{138},{ 8}}, {{ 74},{ 8}},
11 | {{202},{ 8}}, {{ 42},{ 8}}, {{170},{ 8}}, {{106},{ 8}}, {{234},{ 8}},
12 | {{ 26},{ 8}}, {{154},{ 8}}, {{ 90},{ 8}}, {{218},{ 8}}, {{ 58},{ 8}},
13 | {{186},{ 8}}, {{122},{ 8}}, {{250},{ 8}}, {{ 6},{ 8}}, {{134},{ 8}},
14 | {{ 70},{ 8}}, {{198},{ 8}}, {{ 38},{ 8}}, {{166},{ 8}}, {{102},{ 8}},
15 | {{230},{ 8}}, {{ 22},{ 8}}, {{150},{ 8}}, {{ 86},{ 8}}, {{214},{ 8}},
16 | {{ 54},{ 8}}, {{182},{ 8}}, {{118},{ 8}}, {{246},{ 8}}, {{ 14},{ 8}},
17 | {{142},{ 8}}, {{ 78},{ 8}}, {{206},{ 8}}, {{ 46},{ 8}}, {{174},{ 8}},
18 | {{110},{ 8}}, {{238},{ 8}}, {{ 30},{ 8}}, {{158},{ 8}}, {{ 94},{ 8}},
19 | {{222},{ 8}}, {{ 62},{ 8}}, {{190},{ 8}}, {{126},{ 8}}, {{254},{ 8}},
20 | {{ 1},{ 8}}, {{129},{ 8}}, {{ 65},{ 8}}, {{193},{ 8}}, {{ 33},{ 8}},
21 | {{161},{ 8}}, {{ 97},{ 8}}, {{225},{ 8}}, {{ 17},{ 8}}, {{145},{ 8}},
22 | {{ 81},{ 8}}, {{209},{ 8}}, {{ 49},{ 8}}, {{177},{ 8}}, {{113},{ 8}},
23 | {{241},{ 8}}, {{ 9},{ 8}}, {{137},{ 8}}, {{ 73},{ 8}}, {{201},{ 8}},
24 | {{ 41},{ 8}}, {{169},{ 8}}, {{105},{ 8}}, {{233},{ 8}}, {{ 25},{ 8}},
25 | {{153},{ 8}}, {{ 89},{ 8}}, {{217},{ 8}}, {{ 57},{ 8}}, {{185},{ 8}},
26 | {{121},{ 8}}, {{249},{ 8}}, {{ 5},{ 8}}, {{133},{ 8}}, {{ 69},{ 8}},
27 | {{197},{ 8}}, {{ 37},{ 8}}, {{165},{ 8}}, {{101},{ 8}}, {{229},{ 8}},
28 | {{ 21},{ 8}}, {{149},{ 8}}, {{ 85},{ 8}}, {{213},{ 8}}, {{ 53},{ 8}},
29 | {{181},{ 8}}, {{117},{ 8}}, {{245},{ 8}}, {{ 13},{ 8}}, {{141},{ 8}},
30 | {{ 77},{ 8}}, {{205},{ 8}}, {{ 45},{ 8}}, {{173},{ 8}}, {{109},{ 8}},
31 | {{237},{ 8}}, {{ 29},{ 8}}, {{157},{ 8}}, {{ 93},{ 8}}, {{221},{ 8}},
32 | {{ 61},{ 8}}, {{189},{ 8}}, {{125},{ 8}}, {{253},{ 8}}, {{ 19},{ 9}},
33 | {{275},{ 9}}, {{147},{ 9}}, {{403},{ 9}}, {{ 83},{ 9}}, {{339},{ 9}},
34 | {{211},{ 9}}, {{467},{ 9}}, {{ 51},{ 9}}, {{307},{ 9}}, {{179},{ 9}},
35 | {{435},{ 9}}, {{115},{ 9}}, {{371},{ 9}}, {{243},{ 9}}, {{499},{ 9}},
36 | {{ 11},{ 9}}, {{267},{ 9}}, {{139},{ 9}}, {{395},{ 9}}, {{ 75},{ 9}},
37 | {{331},{ 9}}, {{203},{ 9}}, {{459},{ 9}}, {{ 43},{ 9}}, {{299},{ 9}},
38 | {{171},{ 9}}, {{427},{ 9}}, {{107},{ 9}}, {{363},{ 9}}, {{235},{ 9}},
39 | {{491},{ 9}}, {{ 27},{ 9}}, {{283},{ 9}}, {{155},{ 9}}, {{411},{ 9}},
40 | {{ 91},{ 9}}, {{347},{ 9}}, {{219},{ 9}}, {{475},{ 9}}, {{ 59},{ 9}},
41 | {{315},{ 9}}, {{187},{ 9}}, {{443},{ 9}}, {{123},{ 9}}, {{379},{ 9}},
42 | {{251},{ 9}}, {{507},{ 9}}, {{ 7},{ 9}}, {{263},{ 9}}, {{135},{ 9}},
43 | {{391},{ 9}}, {{ 71},{ 9}}, {{327},{ 9}}, {{199},{ 9}}, {{455},{ 9}},
44 | {{ 39},{ 9}}, {{295},{ 9}}, {{167},{ 9}}, {{423},{ 9}}, {{103},{ 9}},
45 | {{359},{ 9}}, {{231},{ 9}}, {{487},{ 9}}, {{ 23},{ 9}}, {{279},{ 9}},
46 | {{151},{ 9}}, {{407},{ 9}}, {{ 87},{ 9}}, {{343},{ 9}}, {{215},{ 9}},
47 | {{471},{ 9}}, {{ 55},{ 9}}, {{311},{ 9}}, {{183},{ 9}}, {{439},{ 9}},
48 | {{119},{ 9}}, {{375},{ 9}}, {{247},{ 9}}, {{503},{ 9}}, {{ 15},{ 9}},
49 | {{271},{ 9}}, {{143},{ 9}}, {{399},{ 9}}, {{ 79},{ 9}}, {{335},{ 9}},
50 | {{207},{ 9}}, {{463},{ 9}}, {{ 47},{ 9}}, {{303},{ 9}}, {{175},{ 9}},
51 | {{431},{ 9}}, {{111},{ 9}}, {{367},{ 9}}, {{239},{ 9}}, {{495},{ 9}},
52 | {{ 31},{ 9}}, {{287},{ 9}}, {{159},{ 9}}, {{415},{ 9}}, {{ 95},{ 9}},
53 | {{351},{ 9}}, {{223},{ 9}}, {{479},{ 9}}, {{ 63},{ 9}}, {{319},{ 9}},
54 | {{191},{ 9}}, {{447},{ 9}}, {{127},{ 9}}, {{383},{ 9}}, {{255},{ 9}},
55 | {{511},{ 9}}, {{ 0},{ 7}}, {{ 64},{ 7}}, {{ 32},{ 7}}, {{ 96},{ 7}},
56 | {{ 16},{ 7}}, {{ 80},{ 7}}, {{ 48},{ 7}}, {{112},{ 7}}, {{ 8},{ 7}},
57 | {{ 72},{ 7}}, {{ 40},{ 7}}, {{104},{ 7}}, {{ 24},{ 7}}, {{ 88},{ 7}},
58 | {{ 56},{ 7}}, {{120},{ 7}}, {{ 4},{ 7}}, {{ 68},{ 7}}, {{ 36},{ 7}},
59 | {{100},{ 7}}, {{ 20},{ 7}}, {{ 84},{ 7}}, {{ 52},{ 7}}, {{116},{ 7}},
60 | {{ 3},{ 8}}, {{131},{ 8}}, {{ 67},{ 8}}, {{195},{ 8}}, {{ 35},{ 8}},
61 | {{163},{ 8}}, {{ 99},{ 8}}, {{227},{ 8}}
62 | };
63 |
64 | local const ct_data static_dtree[D_CODES] = {
65 | {{ 0},{ 5}}, {{16},{ 5}}, {{ 8},{ 5}}, {{24},{ 5}}, {{ 4},{ 5}},
66 | {{20},{ 5}}, {{12},{ 5}}, {{28},{ 5}}, {{ 2},{ 5}}, {{18},{ 5}},
67 | {{10},{ 5}}, {{26},{ 5}}, {{ 6},{ 5}}, {{22},{ 5}}, {{14},{ 5}},
68 | {{30},{ 5}}, {{ 1},{ 5}}, {{17},{ 5}}, {{ 9},{ 5}}, {{25},{ 5}},
69 | {{ 5},{ 5}}, {{21},{ 5}}, {{13},{ 5}}, {{29},{ 5}}, {{ 3},{ 5}},
70 | {{19},{ 5}}, {{11},{ 5}}, {{27},{ 5}}, {{ 7},{ 5}}, {{23},{ 5}}
71 | };
72 |
73 | const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {
74 | 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8,
75 | 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10,
76 | 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
77 | 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
78 | 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13,
79 | 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
80 | 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
81 | 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
82 | 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
83 | 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15,
84 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
85 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
86 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 0, 16, 17,
87 | 18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22,
88 | 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
89 | 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
90 | 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
91 | 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27,
92 | 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
93 | 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
94 | 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
95 | 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
96 | 28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
97 | 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
98 | 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
99 | 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
100 | };
101 |
102 | const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {
103 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12,
104 | 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16,
105 | 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19,
106 | 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
107 | 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22,
108 | 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
109 | 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
110 | 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
111 | 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
112 | 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26,
113 | 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
114 | 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
115 | 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28
116 | };
117 |
118 | local const int base_length[LENGTH_CODES] = {
119 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56,
120 | 64, 80, 96, 112, 128, 160, 192, 224, 0
121 | };
122 |
123 | local const int base_dist[D_CODES] = {
124 | 0, 1, 2, 3, 4, 6, 8, 12, 16, 24,
125 | 32, 48, 64, 96, 128, 192, 256, 384, 512, 768,
126 | 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576
127 | };
128 |
129 |
--------------------------------------------------------------------------------
/src/zlib/uncompr.c:
--------------------------------------------------------------------------------
1 | /* uncompr.c -- decompress a memory buffer
2 | * Copyright (C) 1995-2003, 2010 Jean-loup Gailly.
3 | * For conditions of distribution and use, see copyright notice in zlib.h
4 | */
5 |
6 | /* @(#) $Id$ */
7 |
8 | #define ZLIB_INTERNAL
9 | #include "zlib.h"
10 |
11 | /* ===========================================================================
12 | Decompresses the source buffer into the destination buffer. sourceLen is
13 | the byte length of the source buffer. Upon entry, destLen is the total
14 | size of the destination buffer, which must be large enough to hold the
15 | entire uncompressed data. (The size of the uncompressed data must have
16 | been saved previously by the compressor and transmitted to the decompressor
17 | by some mechanism outside the scope of this compression library.)
18 | Upon exit, destLen is the actual size of the compressed buffer.
19 |
20 | uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
21 | enough memory, Z_BUF_ERROR if there was not enough room in the output
22 | buffer, or Z_DATA_ERROR if the input data was corrupted.
23 | */
24 | int ZEXPORT uncompress (dest, destLen, source, sourceLen)
25 | Bytef *dest;
26 | uLongf *destLen;
27 | const Bytef *source;
28 | uLong sourceLen;
29 | {
30 | z_stream stream;
31 | int err;
32 |
33 | stream.next_in = (Bytef*)source;
34 | stream.avail_in = (uInt)sourceLen;
35 | /* Check for source > 64K on 16-bit machine: */
36 | if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
37 |
38 | stream.next_out = dest;
39 | stream.avail_out = (uInt)*destLen;
40 | if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
41 |
42 | stream.zalloc = (alloc_func)0;
43 | stream.zfree = (free_func)0;
44 |
45 | err = inflateInit(&stream);
46 | if (err != Z_OK) return err;
47 |
48 | err = inflate(&stream, Z_FINISH);
49 | if (err != Z_STREAM_END) {
50 | inflateEnd(&stream);
51 | if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
52 | return Z_DATA_ERROR;
53 | return err;
54 | }
55 | *destLen = stream.total_out;
56 |
57 | err = inflateEnd(&stream);
58 | return err;
59 | }
60 |
--------------------------------------------------------------------------------
/src/zlib/zutil.c:
--------------------------------------------------------------------------------
1 | /* zutil.c -- target dependent utility functions for the compression library
2 | * Copyright (C) 1995-2005, 2010 Jean-loup Gailly.
3 | * For conditions of distribution and use, see copyright notice in zlib.h
4 | */
5 |
6 | /* @(#) $Id$ */
7 |
8 | #include "zutil.h"
9 |
10 | #ifndef NO_DUMMY_DECL
11 | struct internal_state {int dummy;}; /* for buggy compilers */
12 | #endif
13 |
14 | const char * const z_errmsg[10] = {
15 | "need dictionary", /* Z_NEED_DICT 2 */
16 | "stream end", /* Z_STREAM_END 1 */
17 | "", /* Z_OK 0 */
18 | "file error", /* Z_ERRNO (-1) */
19 | "stream error", /* Z_STREAM_ERROR (-2) */
20 | "data error", /* Z_DATA_ERROR (-3) */
21 | "insufficient memory", /* Z_MEM_ERROR (-4) */
22 | "buffer error", /* Z_BUF_ERROR (-5) */
23 | "incompatible version",/* Z_VERSION_ERROR (-6) */
24 | ""};
25 |
26 |
27 | const char * ZEXPORT zlibVersion()
28 | {
29 | return ZLIB_VERSION;
30 | }
31 |
32 | uLong ZEXPORT zlibCompileFlags()
33 | {
34 | uLong flags;
35 |
36 | flags = 0;
37 | switch ((int)(sizeof(uInt))) {
38 | case 2: break;
39 | case 4: flags += 1; break;
40 | case 8: flags += 2; break;
41 | default: flags += 3;
42 | }
43 | switch ((int)(sizeof(uLong))) {
44 | case 2: break;
45 | case 4: flags += 1 << 2; break;
46 | case 8: flags += 2 << 2; break;
47 | default: flags += 3 << 2;
48 | }
49 | switch ((int)(sizeof(voidpf))) {
50 | case 2: break;
51 | case 4: flags += 1 << 4; break;
52 | case 8: flags += 2 << 4; break;
53 | default: flags += 3 << 4;
54 | }
55 | switch ((int)(sizeof(z_off_t))) {
56 | case 2: break;
57 | case 4: flags += 1 << 6; break;
58 | case 8: flags += 2 << 6; break;
59 | default: flags += 3 << 6;
60 | }
61 | #ifdef DEBUG
62 | flags += 1 << 8;
63 | #endif
64 | #if defined(ASMV) || defined(ASMINF)
65 | flags += 1 << 9;
66 | #endif
67 | #ifdef ZLIB_WINAPI
68 | flags += 1 << 10;
69 | #endif
70 | #ifdef BUILDFIXED
71 | flags += 1 << 12;
72 | #endif
73 | #ifdef DYNAMIC_CRC_TABLE
74 | flags += 1 << 13;
75 | #endif
76 | #ifdef NO_GZCOMPRESS
77 | flags += 1L << 16;
78 | #endif
79 | #ifdef NO_GZIP
80 | flags += 1L << 17;
81 | #endif
82 | #ifdef PKZIP_BUG_WORKAROUND
83 | flags += 1L << 20;
84 | #endif
85 | #ifdef FASTEST
86 | flags += 1L << 21;
87 | #endif
88 | #ifdef STDC
89 | # ifdef NO_vsnprintf
90 | flags += 1L << 25;
91 | # ifdef HAS_vsprintf_void
92 | flags += 1L << 26;
93 | # endif
94 | # else
95 | # ifdef HAS_vsnprintf_void
96 | flags += 1L << 26;
97 | # endif
98 | # endif
99 | #else
100 | flags += 1L << 24;
101 | # ifdef NO_snprintf
102 | flags += 1L << 25;
103 | # ifdef HAS_sprintf_void
104 | flags += 1L << 26;
105 | # endif
106 | # else
107 | # ifdef HAS_snprintf_void
108 | flags += 1L << 26;
109 | # endif
110 | # endif
111 | #endif
112 | return flags;
113 | }
114 |
115 | #ifdef DEBUG
116 |
117 | # ifndef verbose
118 | # define verbose 0
119 | # endif
120 | int ZLIB_INTERNAL z_verbose = verbose;
121 |
122 | void ZLIB_INTERNAL z_error (m)
123 | char *m;
124 | {
125 | fprintf(stderr, "%s\n", m);
126 | exit(1);
127 | }
128 | #endif
129 |
130 | /* exported to allow conversion of error code to string for compress() and
131 | * uncompress()
132 | */
133 | const char * ZEXPORT zError(err)
134 | int err;
135 | {
136 | return ERR_MSG(err);
137 | }
138 |
139 | #if defined(_WIN32_WCE)
140 | /* The Microsoft C Run-Time Library for Windows CE doesn't have
141 | * errno. We define it as a global variable to simplify porting.
142 | * Its value is always 0 and should not be used.
143 | */
144 | int errno = 0;
145 | #endif
146 |
147 | #ifndef HAVE_MEMCPY
148 |
149 | void ZLIB_INTERNAL zmemcpy(dest, source, len)
150 | Bytef* dest;
151 | const Bytef* source;
152 | uInt len;
153 | {
154 | if (len == 0) return;
155 | do {
156 | *dest++ = *source++; /* ??? to be unrolled */
157 | } while (--len != 0);
158 | }
159 |
160 | int ZLIB_INTERNAL zmemcmp(s1, s2, len)
161 | const Bytef* s1;
162 | const Bytef* s2;
163 | uInt len;
164 | {
165 | uInt j;
166 |
167 | for (j = 0; j < len; j++) {
168 | if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
169 | }
170 | return 0;
171 | }
172 |
173 | void ZLIB_INTERNAL zmemzero(dest, len)
174 | Bytef* dest;
175 | uInt len;
176 | {
177 | if (len == 0) return;
178 | do {
179 | *dest++ = 0; /* ??? to be unrolled */
180 | } while (--len != 0);
181 | }
182 | #endif
183 |
184 |
185 | #ifdef SYS16BIT
186 |
187 | #ifdef __TURBOC__
188 | /* Turbo C in 16-bit mode */
189 |
190 | # define MY_ZCALLOC
191 |
192 | /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
193 | * and farmalloc(64K) returns a pointer with an offset of 8, so we
194 | * must fix the pointer. Warning: the pointer must be put back to its
195 | * original form in order to free it, use zcfree().
196 | */
197 |
198 | #define MAX_PTR 10
199 | /* 10*64K = 640K */
200 |
201 | local int next_ptr = 0;
202 |
203 | typedef struct ptr_table_s {
204 | voidpf org_ptr;
205 | voidpf new_ptr;
206 | } ptr_table;
207 |
208 | local ptr_table table[MAX_PTR];
209 | /* This table is used to remember the original form of pointers
210 | * to large buffers (64K). Such pointers are normalized with a zero offset.
211 | * Since MSDOS is not a preemptive multitasking OS, this table is not
212 | * protected from concurrent access. This hack doesn't work anyway on
213 | * a protected system like OS/2. Use Microsoft C instead.
214 | */
215 |
216 | voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size)
217 | {
218 | voidpf buf = opaque; /* just to make some compilers happy */
219 | ulg bsize = (ulg)items*size;
220 |
221 | /* If we allocate less than 65520 bytes, we assume that farmalloc
222 | * will return a usable pointer which doesn't have to be normalized.
223 | */
224 | if (bsize < 65520L) {
225 | buf = farmalloc(bsize);
226 | if (*(ush*)&buf != 0) return buf;
227 | } else {
228 | buf = farmalloc(bsize + 16L);
229 | }
230 | if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
231 | table[next_ptr].org_ptr = buf;
232 |
233 | /* Normalize the pointer to seg:0 */
234 | *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
235 | *(ush*)&buf = 0;
236 | table[next_ptr++].new_ptr = buf;
237 | return buf;
238 | }
239 |
240 | void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
241 | {
242 | int n;
243 | if (*(ush*)&ptr != 0) { /* object < 64K */
244 | farfree(ptr);
245 | return;
246 | }
247 | /* Find the original pointer */
248 | for (n = 0; n < next_ptr; n++) {
249 | if (ptr != table[n].new_ptr) continue;
250 |
251 | farfree(table[n].org_ptr);
252 | while (++n < next_ptr) {
253 | table[n-1] = table[n];
254 | }
255 | next_ptr--;
256 | return;
257 | }
258 | ptr = opaque; /* just to make some compilers happy */
259 | Assert(0, "zcfree: ptr not found");
260 | }
261 |
262 | #endif /* __TURBOC__ */
263 |
264 |
265 | #ifdef M_I86
266 | /* Microsoft C in 16-bit mode */
267 |
268 | # define MY_ZCALLOC
269 |
270 | #if (!defined(_MSC_VER) || (_MSC_VER <= 600))
271 | # define _halloc halloc
272 | # define _hfree hfree
273 | #endif
274 |
275 | voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, uInt items, uInt size)
276 | {
277 | if (opaque) opaque = 0; /* to make compiler happy */
278 | return _halloc((long)items, size);
279 | }
280 |
281 | void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
282 | {
283 | if (opaque) opaque = 0; /* to make compiler happy */
284 | _hfree(ptr);
285 | }
286 |
287 | #endif /* M_I86 */
288 |
289 | #endif /* SYS16BIT */
290 |
291 |
292 | #ifndef MY_ZCALLOC /* Any system without a special alloc function */
293 |
294 | #ifndef STDC
295 | extern voidp malloc OF((uInt size));
296 | extern voidp calloc OF((uInt items, uInt size));
297 | extern void free OF((voidpf ptr));
298 | #endif
299 |
300 | voidpf ZLIB_INTERNAL zcalloc (opaque, items, size)
301 | voidpf opaque;
302 | unsigned items;
303 | unsigned size;
304 | {
305 | if (opaque) items += size - size; /* make compiler happy */
306 | return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
307 | (voidpf)calloc(items, size);
308 | }
309 |
310 | void ZLIB_INTERNAL zcfree (opaque, ptr)
311 | voidpf opaque;
312 | voidpf ptr;
313 | {
314 | free(ptr);
315 | if (opaque) return; /* make compiler happy */
316 | }
317 |
318 | #endif /* MY_ZCALLOC */
319 |
--------------------------------------------------------------------------------
/src/zlib/zutil.h:
--------------------------------------------------------------------------------
1 | /* zutil.h -- internal interface and configuration of the compression library
2 | * Copyright (C) 1995-2010 Jean-loup Gailly.
3 | * For conditions of distribution and use, see copyright notice in zlib.h
4 | */
5 |
6 | /* WARNING: this file should *not* be used by applications. It is
7 | part of the implementation of the compression library and is
8 | subject to change. Applications should only use zlib.h.
9 | */
10 |
11 | /* @(#) $Id$ */
12 |
13 | #ifndef ZUTIL_H
14 | #define ZUTIL_H
15 |
16 | #if ((__GNUC__-0) * 10 + __GNUC_MINOR__-0 >= 33) && !defined(NO_VIZ)
17 | # define ZLIB_INTERNAL __attribute__((visibility ("hidden")))
18 | #else
19 | # define ZLIB_INTERNAL
20 | #endif
21 |
22 | #include "zlib.h"
23 |
24 | #ifdef STDC
25 | # if !(defined(_WIN32_WCE) && defined(_MSC_VER))
26 | # include
27 | # endif
28 | # include
29 | # include
30 | #endif
31 |
32 | #ifndef local
33 | # define local static
34 | #endif
35 | /* compile with -Dlocal if your debugger can't find static symbols */
36 |
37 | typedef unsigned char uch;
38 | typedef uch FAR uchf;
39 | typedef unsigned short ush;
40 | typedef ush FAR ushf;
41 | typedef unsigned long ulg;
42 |
43 | extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
44 | /* (size given to avoid silly warnings with Visual C++) */
45 |
46 | #define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)]
47 |
48 | #define ERR_RETURN(strm,err) \
49 | return (strm->msg = (char*)ERR_MSG(err), (err))
50 | /* To be used only when the state is known to be valid */
51 |
52 | /* common constants */
53 |
54 | #ifndef DEF_WBITS
55 | # define DEF_WBITS MAX_WBITS
56 | #endif
57 | /* default windowBits for decompression. MAX_WBITS is for compression only */
58 |
59 | #if MAX_MEM_LEVEL >= 8
60 | # define DEF_MEM_LEVEL 8
61 | #else
62 | # define DEF_MEM_LEVEL MAX_MEM_LEVEL
63 | #endif
64 | /* default memLevel */
65 |
66 | #define STORED_BLOCK 0
67 | #define STATIC_TREES 1
68 | #define DYN_TREES 2
69 | /* The three kinds of block type */
70 |
71 | #define MIN_MATCH 3
72 | #define MAX_MATCH 258
73 | /* The minimum and maximum match lengths */
74 |
75 | #define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */
76 |
77 | /* target dependencies */
78 |
79 | #if defined(MSDOS) || (defined(WINDOWS) && !defined(WIN32))
80 | # define OS_CODE 0x00
81 | # if defined(__TURBOC__) || defined(__BORLANDC__)
82 | # if (__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__))
83 | /* Allow compilation with ANSI keywords only enabled */
84 | void _Cdecl farfree( void *block );
85 | void *_Cdecl farmalloc( unsigned long nbytes );
86 | # else
87 | # include
88 | # endif
89 | # else /* MSC or DJGPP */
90 | # include
91 | # endif
92 | #endif
93 |
94 | #ifdef AMIGA
95 | # define OS_CODE 0x01
96 | #endif
97 |
98 | #if defined(VAXC) || defined(VMS)
99 | # define OS_CODE 0x02
100 | # define F_OPEN(name, mode) \
101 | fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512")
102 | #endif
103 |
104 | #if defined(ATARI) || defined(atarist)
105 | # define OS_CODE 0x05
106 | #endif
107 |
108 | #ifdef OS2
109 | # define OS_CODE 0x06
110 | # ifdef M_I86
111 | # include
112 | # endif
113 | #endif
114 |
115 | #if defined(MACOS) || defined(TARGET_OS_MAC)
116 | # define OS_CODE 0x07
117 | # if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
118 | # include /* for fdopen */
119 | # else
120 | # ifndef fdopen
121 | # define fdopen(fd,mode) NULL /* No fdopen() */
122 | # endif
123 | # endif
124 | #endif
125 |
126 | #ifdef TOPS20
127 | # define OS_CODE 0x0a
128 | #endif
129 |
130 | #ifdef WIN32
131 | # ifndef __CYGWIN__ /* Cygwin is Unix, not Win32 */
132 | # define OS_CODE 0x0b
133 | # endif
134 | #endif
135 |
136 | #ifdef __50SERIES /* Prime/PRIMOS */
137 | # define OS_CODE 0x0f
138 | #endif
139 |
140 | #if defined(_BEOS_) || defined(RISCOS)
141 | # define fdopen(fd,mode) NULL /* No fdopen() */
142 | #endif
143 |
144 | #if (defined(_MSC_VER) && (_MSC_VER > 600)) && !defined __INTERIX
145 | # if defined(_WIN32_WCE)
146 | # define fdopen(fd,mode) NULL /* No fdopen() */
147 | # ifndef _PTRDIFF_T_DEFINED
148 | typedef int ptrdiff_t;
149 | # define _PTRDIFF_T_DEFINED
150 | # endif
151 | # else
152 | # define fdopen(fd,type) _fdopen(fd,type)
153 | # endif
154 | #endif
155 |
156 | #if defined(__BORLANDC__)
157 | #pragma warn -8004
158 | #pragma warn -8008
159 | #pragma warn -8066
160 | #endif
161 |
162 | /* provide prototypes for these when building zlib without LFS */
163 | #if !defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0
164 | ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off_t));
165 | ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off_t));
166 | #endif
167 |
168 | /* common defaults */
169 |
170 | #ifndef OS_CODE
171 | # define OS_CODE 0x03 /* assume Unix */
172 | #endif
173 |
174 | #ifndef F_OPEN
175 | # define F_OPEN(name, mode) fopen((name), (mode))
176 | #endif
177 |
178 | /* functions */
179 |
180 | #if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550)
181 | # ifndef HAVE_VSNPRINTF
182 | # define HAVE_VSNPRINTF
183 | # endif
184 | #endif
185 | #if defined(__CYGWIN__)
186 | # ifndef HAVE_VSNPRINTF
187 | # define HAVE_VSNPRINTF
188 | # endif
189 | #endif
190 | #ifndef HAVE_VSNPRINTF
191 | # ifdef MSDOS
192 | /* vsnprintf may exist on some MS-DOS compilers (DJGPP?),
193 | but for now we just assume it doesn't. */
194 | # define NO_vsnprintf
195 | # endif
196 | # ifdef __TURBOC__
197 | # define NO_vsnprintf
198 | # endif
199 | # ifdef WIN32
200 | /* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */
201 | # if !defined(vsnprintf) && !defined(NO_vsnprintf)
202 | # if !defined(_MSC_VER) || ( defined(_MSC_VER) && _MSC_VER < 1500 )
203 | # define vsnprintf _vsnprintf
204 | # endif
205 | # endif
206 | # endif
207 | # ifdef __SASC
208 | # define NO_vsnprintf
209 | # endif
210 | #endif
211 | #ifdef VMS
212 | # define NO_vsnprintf
213 | #endif
214 |
215 | #if defined(pyr)
216 | # define NO_MEMCPY
217 | #endif
218 | #if defined(SMALL_MEDIUM) && !defined(_MSC_VER) && !defined(__SC__)
219 | /* Use our own functions for small and medium model with MSC <= 5.0.
220 | * You may have to use the same strategy for Borland C (untested).
221 | * The __SC__ check is for Symantec.
222 | */
223 | # define NO_MEMCPY
224 | #endif
225 | #if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY)
226 | # define HAVE_MEMCPY
227 | #endif
228 | #ifdef HAVE_MEMCPY
229 | # ifdef SMALL_MEDIUM /* MSDOS small or medium model */
230 | # define zmemcpy _fmemcpy
231 | # define zmemcmp _fmemcmp
232 | # define zmemzero(dest, len) _fmemset(dest, 0, len)
233 | # else
234 | # define zmemcpy memcpy
235 | # define zmemcmp memcmp
236 | # define zmemzero(dest, len) memset(dest, 0, len)
237 | # endif
238 | #else
239 | void ZLIB_INTERNAL zmemcpy OF((Bytef* dest, const Bytef* source, uInt len));
240 | int ZLIB_INTERNAL zmemcmp OF((const Bytef* s1, const Bytef* s2, uInt len));
241 | void ZLIB_INTERNAL zmemzero OF((Bytef* dest, uInt len));
242 | #endif
243 |
244 | /* Diagnostic functions */
245 | #ifdef DEBUG
246 | # include
247 | extern int ZLIB_INTERNAL z_verbose;
248 | extern void ZLIB_INTERNAL z_error OF((char *m));
249 | # define Assert(cond,msg) {if(!(cond)) z_error(msg);}
250 | # define Trace(x) {if (z_verbose>=0) fprintf x ;}
251 | # define Tracev(x) {if (z_verbose>0) fprintf x ;}
252 | # define Tracevv(x) {if (z_verbose>1) fprintf x ;}
253 | # define Tracec(c,x) {if (z_verbose>0 && (c)) fprintf x ;}
254 | # define Tracecv(c,x) {if (z_verbose>1 && (c)) fprintf x ;}
255 | #else
256 | # define Assert(cond,msg)
257 | # define Trace(x)
258 | # define Tracev(x)
259 | # define Tracevv(x)
260 | # define Tracec(c,x)
261 | # define Tracecv(c,x)
262 | #endif
263 |
264 |
265 | voidpf ZLIB_INTERNAL zcalloc OF((voidpf opaque, unsigned items,
266 | unsigned size));
267 | void ZLIB_INTERNAL zcfree OF((voidpf opaque, voidpf ptr));
268 |
269 | #define ZALLOC(strm, items, size) \
270 | (*((strm)->zalloc))((strm)->opaque, (items), (size))
271 | #define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
272 | #define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
273 |
274 | #endif /* ZUTIL_H */
275 |
--------------------------------------------------------------------------------