├── .gitignore ├── Makefile ├── README ├── build └── Makefile ├── include ├── adb │ ├── adb.h │ ├── adb_auth.h │ ├── adb_client.h │ ├── fdevent.h │ ├── file_sync_service.h │ ├── mutex_list.h │ ├── sysdeps.h │ ├── transport.h │ ├── usb_vendors.h │ └── utils.h ├── cutils │ ├── abort_socket.h │ ├── android_reboot.h │ ├── array.h │ ├── ashmem.h │ ├── atomic-arm.h │ ├── atomic-inline.h │ ├── atomic-mips.h │ ├── atomic-x86.h │ ├── atomic.h │ ├── bitops.h │ ├── compiler.h │ ├── config_utils.h │ ├── cpu_info.h │ ├── debugger.h │ ├── dir_hash.h │ ├── event_tag_map.h │ ├── fs.h │ ├── hashmap.h │ ├── iosched_policy.h │ ├── jstring.h │ ├── klog.h │ ├── list.h │ ├── log.h │ ├── logd.h │ ├── logger.h │ ├── logprint.h │ ├── memory.h │ ├── misc.h │ ├── mq.h │ ├── multiuser.h │ ├── native_handle.h │ ├── open_memstream.h │ ├── partition_utils.h │ ├── process_name.h │ ├── properties.h │ ├── qsort_r_compat.h │ ├── qtaguid.h │ ├── record_stream.h │ ├── sched_policy.h │ ├── selector.h │ ├── sockets.h │ ├── str_parms.h │ ├── threads.h │ ├── tztime.h │ ├── uevent.h │ ├── uio.h │ └── zygote.h ├── libadb │ └── libadb.h ├── mincrypt │ ├── rsa.h │ └── sha.h ├── zipfile │ └── zipfile.h └── zlib │ ├── zconf.h │ ├── zlib.h │ └── zutil.h └── src ├── Makefile ├── adb ├── Makefile ├── adb.c ├── adb.h ├── adb_auth.h ├── adb_auth_host.c ├── adb_client.c ├── adb_client.h ├── adbexec.c ├── commandline.c ├── console.c ├── fdevent.c ├── fdevent.h ├── file_sync_client.c ├── file_sync_service.h ├── get_my_path_linux.c ├── mutex_list.h ├── services.c ├── sockets.c ├── sysdeps.h ├── transport.c ├── transport.h ├── transport_local.c ├── transport_usb.c ├── usb_linux.c ├── usb_vendors.c ├── usb_vendors.h ├── utils.c └── utils.h ├── libadb ├── Makefile └── libadb.c ├── libcutils ├── Makefile ├── abort_socket.c ├── buffer.h ├── list.c ├── load_file.c ├── loghack.h ├── private.h ├── socket_inaddr_any_server.c ├── socket_local.h ├── socket_local_client.c ├── socket_local_server.c ├── socket_loopback_client.c ├── socket_loopback_server.c ├── socket_network_client.c └── tzfile.h ├── libzipfile ├── Makefile ├── centraldir.c ├── private.h └── zipfile.c └── zlib ├── Makefile ├── adler32.c ├── compress.c ├── crc32.c ├── crc32.h ├── deflate.c ├── deflate.h ├── gzguts.h ├── 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 /.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.so 3 | *.dylib 4 | adb 5 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | make -C src 3 | mv src/adb/*.o build/ 4 | mv src/libadb/*.o build/ 5 | mv src/libcutils/*.o build/ 6 | mv src/libzipfile/*.o build/ 7 | mv src/zlib/*.o build/ 8 | make -C build 9 | 10 | clean: 11 | make -C src clean 12 | make -C build clean 13 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | Android's adb source code and required components, 2 | stripped into a modular build system. 3 | 4 | The end goal will be an adb library like libirecovery. 5 | -------------------------------------------------------------------------------- /build/Makefile: -------------------------------------------------------------------------------- 1 | LD=gcc 2 | LDFLAGS=-static 3 | LIBS=-lcrypto -ldl -lrt -lpthread 4 | 5 | all: adb libadb 6 | 7 | libadb: 8 | mv adbexec.o adbexec.obj 9 | gcc -o libadb.so *.o -shared -Wl,-soname,libadb.so $(LIBS) 10 | mv adbexec.obj adbexec.o 11 | 12 | adb: 13 | mv libadb.o libadb.obj 14 | $(LD) -o $@ $(LDFLAGS) *.o $(LIBS) 15 | mv libadb.obj libadb.o 16 | 17 | clean: 18 | rm adb *.o *.so 19 | -------------------------------------------------------------------------------- /include/adb/adb_auth.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 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_AUTH_H 18 | #define __ADB_AUTH_H 19 | 20 | void adb_auth_init(void); 21 | void adb_auth_verified(atransport *t); 22 | 23 | /* AUTH packets first argument */ 24 | /* Request */ 25 | #define ADB_AUTH_TOKEN 1 26 | /* Response */ 27 | #define ADB_AUTH_SIGNATURE 2 28 | #define ADB_AUTH_RSAPUBLICKEY 3 29 | 30 | #if ADB_HOST 31 | 32 | int adb_auth_sign(void *key, void *token, size_t token_size, void *sig); 33 | void *adb_auth_nextkey(void *current); 34 | int adb_auth_get_userkey(unsigned char *data, size_t len); 35 | 36 | static inline int adb_auth_generate_token(void *token, size_t token_size) { return 0; } 37 | static inline int adb_auth_verify(void *token, void *sig, int siglen) { return 0; } 38 | static inline void adb_auth_confirm_key(unsigned char *data, size_t len, atransport *t) { } 39 | static inline void adb_auth_reload_keys(void) { } 40 | 41 | #else // !ADB_HOST 42 | 43 | static inline int adb_auth_sign(void* key, void *token, size_t token_size, void *sig) { return 0; } 44 | static inline void *adb_auth_nextkey(void *current) { return NULL; } 45 | static inline int adb_auth_get_userkey(unsigned char *data, size_t len) { return 0; } 46 | 47 | int adb_auth_generate_token(void *token, size_t token_size); 48 | int adb_auth_verify(void *token, void *sig, int siglen); 49 | void adb_auth_confirm_key(unsigned char *data, size_t len, atransport *t); 50 | void adb_auth_reload_keys(void); 51 | 52 | #endif // ADB_HOST 53 | 54 | #endif // __ADB_AUTH_H 55 | -------------------------------------------------------------------------------- /include/adb/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 | -------------------------------------------------------------------------------- /include/adb/fdevent.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2006 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 __FDEVENT_H 18 | #define __FDEVENT_H 19 | 20 | #include /* for int64_t */ 21 | 22 | /* events that may be observed */ 23 | #define FDE_READ 0x0001 24 | #define FDE_WRITE 0x0002 25 | #define FDE_ERROR 0x0004 26 | #define FDE_TIMEOUT 0x0008 27 | 28 | /* features that may be set (via the events set/add/del interface) */ 29 | #define FDE_DONT_CLOSE 0x0080 30 | 31 | typedef struct fdevent fdevent; 32 | 33 | typedef void (*fd_func)(int fd, unsigned events, void *userdata); 34 | 35 | /* Allocate and initialize a new fdevent object 36 | * Note: use FD_TIMER as 'fd' to create a fd-less object 37 | * (used to implement timers). 38 | */ 39 | fdevent *fdevent_create(int fd, fd_func func, void *arg); 40 | 41 | /* Uninitialize and deallocate an fdevent object that was 42 | ** created by fdevent_create() 43 | */ 44 | void fdevent_destroy(fdevent *fde); 45 | 46 | /* Initialize an fdevent object that was externally allocated 47 | */ 48 | void fdevent_install(fdevent *fde, int fd, fd_func func, void *arg); 49 | 50 | /* Uninitialize an fdevent object that was initialized by 51 | ** fdevent_install() 52 | */ 53 | void fdevent_remove(fdevent *item); 54 | 55 | /* Change which events should cause notifications 56 | */ 57 | void fdevent_set(fdevent *fde, unsigned events); 58 | void fdevent_add(fdevent *fde, unsigned events); 59 | void fdevent_del(fdevent *fde, unsigned events); 60 | 61 | void fdevent_set_timeout(fdevent *fde, int64_t timeout_ms); 62 | 63 | /* loop forever, handling events. 64 | */ 65 | void fdevent_loop(); 66 | 67 | struct fdevent 68 | { 69 | fdevent *next; 70 | fdevent *prev; 71 | 72 | int fd; 73 | int force_eof; 74 | 75 | unsigned short state; 76 | unsigned short events; 77 | 78 | fd_func func; 79 | void *arg; 80 | }; 81 | 82 | 83 | #endif 84 | -------------------------------------------------------------------------------- /include/adb/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 | -------------------------------------------------------------------------------- /include/adb/mutex_list.h: -------------------------------------------------------------------------------- 1 | /* the list of mutexes used by adb */ 2 | /* #ifndef __MUTEX_LIST_H 3 | * Do not use an include-guard. This file is included once to declare the locks 4 | * and once in win32 to actually do the runtime initialization. 5 | */ 6 | #ifndef ADB_MUTEX 7 | #error ADB_MUTEX not defined when including this file 8 | #endif 9 | ADB_MUTEX(dns_lock) 10 | ADB_MUTEX(socket_list_lock) 11 | ADB_MUTEX(transport_lock) 12 | #if ADB_HOST 13 | ADB_MUTEX(local_transports_lock) 14 | #endif 15 | ADB_MUTEX(usb_lock) 16 | 17 | // Sadly logging to /data/adb/adb-... is not thread safe. 18 | // After modifying adb.h::D() to count invocations: 19 | // DEBUG(jpa):0:Handling main() 20 | // DEBUG(jpa):1:[ usb_init - starting thread ] 21 | // (Oopsies, no :2:, and matching message is also gone.) 22 | // DEBUG(jpa):3:[ usb_thread - opening device ] 23 | // DEBUG(jpa):4:jdwp control socket started (10) 24 | ADB_MUTEX(D_lock) 25 | 26 | #undef ADB_MUTEX 27 | -------------------------------------------------------------------------------- /include/adb/transport.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 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 __TRANSPORT_H 18 | #define __TRANSPORT_H 19 | 20 | /* convenience wrappers around read/write that will retry on 21 | ** EINTR and/or short read/write. Returns 0 on success, -1 22 | ** on error or EOF. 23 | */ 24 | int readx(int fd, void *ptr, size_t len); 25 | int writex(int fd, const void *ptr, size_t len); 26 | #endif /* __TRANSPORT_H */ 27 | -------------------------------------------------------------------------------- /include/adb/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 | -------------------------------------------------------------------------------- /include/adb/utils.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 | #ifndef _ADB_UTILS_H 17 | #define _ADB_UTILS_H 18 | 19 | /* bounded buffer functions */ 20 | 21 | /* all these functions are used to append data to a bounded buffer. 22 | * 23 | * after each operation, the buffer is guaranteed to be zero-terminated, 24 | * even in the case of an overflow. they all return the new buffer position 25 | * which allows one to use them in succession, only checking for overflows 26 | * at the end. For example: 27 | * 28 | * BUFF_DECL(temp,p,end,1024); 29 | * char* p; 30 | * 31 | * p = buff_addc(temp, end, '"'); 32 | * p = buff_adds(temp, end, string); 33 | * p = buff_addc(temp, end, '"'); 34 | * 35 | * if (p >= end) { 36 | * overflow detected. note that 'temp' is 37 | * zero-terminated for safety. 38 | * } 39 | * return strdup(temp); 40 | */ 41 | 42 | /* tries to add a character to the buffer, in case of overflow 43 | * this will only write a terminating zero and return buffEnd. 44 | */ 45 | char* buff_addc (char* buff, char* buffEnd, int c); 46 | 47 | /* tries to add a string to the buffer */ 48 | char* buff_adds (char* buff, char* buffEnd, const char* s); 49 | 50 | /* tries to add a bytes to the buffer. the input can contain zero bytes, 51 | * but a terminating zero will always be appended at the end anyway 52 | */ 53 | char* buff_addb (char* buff, char* buffEnd, const void* data, int len); 54 | 55 | /* tries to add a formatted string to a bounded buffer */ 56 | char* buff_add (char* buff, char* buffEnd, const char* format, ... ); 57 | 58 | /* convenience macro used to define a bounded buffer, as well as 59 | * a 'cursor' and 'end' variables all in one go. 60 | * 61 | * note: this doesn't place an initial terminating zero in the buffer, 62 | * you need to use one of the buff_ functions for this. or simply 63 | * do _cursor[0] = 0 manually. 64 | */ 65 | #define BUFF_DECL(_buff,_cursor,_end,_size) \ 66 | char _buff[_size], *_cursor=_buff, *_end = _cursor + (_size) 67 | 68 | #endif /* _ADB_UTILS_H */ 69 | -------------------------------------------------------------------------------- /include/cutils/abort_socket.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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 | /* Helper to perform abortable blocking operations on a socket: 18 | * asocket_connect() 19 | * asocket_accept() 20 | * asocket_read() 21 | * asocket_write() 22 | * These calls are similar to the regular syscalls, but can be aborted with: 23 | * asocket_abort() 24 | * 25 | * Calling close() on a regular POSIX socket does not abort blocked syscalls on 26 | * that socket in other threads. 27 | * 28 | * After calling asocket_abort() the socket cannot be reused. 29 | * 30 | * Call asocket_destory() *after* all threads have finished with the socket to 31 | * finish closing the socket and free the asocket structure. 32 | * 33 | * The helper is implemented by setting the socket non-blocking to initiate 34 | * syscalls connect(), accept(), read(), write(), then using a blocking poll() 35 | * on both the primary socket and a local pipe. This makes the poll() abortable 36 | * by writing a byte to the local pipe in asocket_abort(). 37 | * 38 | * asocket_create() sets the fd to non-blocking mode. It must not be changed to 39 | * blocking mode. 40 | * 41 | * Using asocket will triple the number of file descriptors required per 42 | * socket, due to the local pipe. It may be possible to use a global pipe per 43 | * process rather than per socket, but we have not been able to come up with a 44 | * race-free implementation yet. 45 | * 46 | * All functions except asocket_init() and asocket_destroy() are thread safe. 47 | */ 48 | 49 | #include 50 | #include 51 | 52 | #ifndef __CUTILS_ABORT_SOCKET_H__ 53 | #define __CUTILS_ABORT_SOCKET_H__ 54 | #ifdef __cplusplus 55 | extern "C" { 56 | #endif 57 | 58 | struct asocket { 59 | int fd; /* primary socket fd */ 60 | int abort_fd[2]; /* pipe used to abort */ 61 | }; 62 | 63 | /* Create an asocket from fd. 64 | * Sets the socket to non-blocking mode. 65 | * Returns NULL on error with errno set. 66 | */ 67 | struct asocket *asocket_init(int fd); 68 | 69 | /* Blocking socket I/O with timeout. 70 | * Calling asocket_abort() from another thread will cause each of these 71 | * functions to immediately return with value -1 and errno ECANCELED. 72 | * timeout is in ms, use -1 to indicate no timeout. On timeout -1 is returned 73 | * with errno ETIMEDOUT. 74 | * EINTR is handled in-call. 75 | * Other semantics are identical to the regular syscalls. 76 | */ 77 | int asocket_connect(struct asocket *s, const struct sockaddr *addr, 78 | socklen_t addrlen, int timeout); 79 | 80 | int asocket_accept(struct asocket *s, struct sockaddr *addr, 81 | socklen_t *addrlen, int timeout); 82 | 83 | int asocket_read(struct asocket *s, void *buf, size_t count, int timeout); 84 | 85 | int asocket_write(struct asocket *s, const void *buf, size_t count, 86 | int timeout); 87 | 88 | /* Abort above calls and shutdown socket. 89 | * Further I/O operations on this socket will immediately fail after this call. 90 | * asocket_destroy() should be used to release resources once all threads 91 | * have returned from blocking calls on the socket. 92 | */ 93 | void asocket_abort(struct asocket *s); 94 | 95 | /* Close socket and free asocket structure. 96 | * Must not be called until all calls on this structure have completed. 97 | */ 98 | void asocket_destroy(struct asocket *s); 99 | 100 | #ifdef __cplusplus 101 | } 102 | #endif 103 | #endif //__CUTILS_ABORT_SOCKET__H__ 104 | -------------------------------------------------------------------------------- /include/cutils/android_reboot.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011, 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 __CUTILS_ANDROID_REBOOT_H__ 18 | #define __CUTILS_ANDROID_REBOOT_H__ 19 | 20 | __BEGIN_DECLS 21 | 22 | /* Commands */ 23 | #define ANDROID_RB_RESTART 0xDEAD0001 24 | #define ANDROID_RB_POWEROFF 0xDEAD0002 25 | #define ANDROID_RB_RESTART2 0xDEAD0003 26 | 27 | /* Flags */ 28 | #define ANDROID_RB_FLAG_NO_SYNC 0x1 29 | #define ANDROID_RB_FLAG_NO_REMOUNT_RO 0x2 30 | 31 | int android_reboot(int cmd, int flags, char *arg); 32 | 33 | __END_DECLS 34 | 35 | #endif /* __CUTILS_ANDROID_REBOOT_H__ */ 36 | -------------------------------------------------------------------------------- /include/cutils/array.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 | /** 18 | * A pointer array which intelligently expands its capacity ad needed. 19 | */ 20 | 21 | #ifndef __ARRAY_H 22 | #define __ARRAY_H 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | #include 29 | 30 | /** An array. */ 31 | typedef struct Array Array; 32 | 33 | /** Constructs a new array. Returns NULL if we ran out of memory. */ 34 | Array* arrayCreate(); 35 | 36 | /** Frees an array. Does not free elements themselves. */ 37 | void arrayFree(Array* array); 38 | 39 | /** Adds a pointer. Returns 0 is successful, < 0 otherwise. */ 40 | int arrayAdd(Array* array, void* pointer); 41 | 42 | /** Gets the pointer at the specified index. */ 43 | void* arrayGet(Array* array, int index); 44 | 45 | /** Removes the pointer at the given index and returns it. */ 46 | void* arrayRemove(Array* array, int index); 47 | 48 | /** Sets pointer at the given index. Returns old pointer. */ 49 | void* arraySet(Array* array, int index, void* pointer); 50 | 51 | /** Sets the array size. Sets new pointers to NULL. Returns 0 if successful, < 0 otherwise . */ 52 | int arraySetSize(Array* array, int size); 53 | 54 | /** Returns the size of the given array. */ 55 | int arraySize(Array* array); 56 | 57 | /** 58 | * Returns a pointer to a C-style array which will be valid until this array 59 | * changes. 60 | */ 61 | const void** arrayUnwrap(Array* array); 62 | 63 | #ifdef __cplusplus 64 | } 65 | #endif 66 | 67 | #endif /* __ARRAY_H */ 68 | -------------------------------------------------------------------------------- /include/cutils/ashmem.h: -------------------------------------------------------------------------------- 1 | /* cutils/ashmem.h 2 | ** 3 | ** Copyright 2008 The Android Open Source Project 4 | ** 5 | ** This file is dual licensed. It may be redistributed and/or modified 6 | ** under the terms of the Apache 2.0 License OR version 2 of the GNU 7 | ** General Public License. 8 | */ 9 | 10 | #ifndef _CUTILS_ASHMEM_H 11 | #define _CUTILS_ASHMEM_H 12 | 13 | #include 14 | 15 | #ifdef __cplusplus 16 | extern "C" { 17 | #endif 18 | 19 | int ashmem_create_region(const char *name, size_t size); 20 | int ashmem_set_prot_region(int fd, int prot); 21 | int ashmem_pin_region(int fd, size_t offset, size_t len); 22 | int ashmem_unpin_region(int fd, size_t offset, size_t len); 23 | int ashmem_get_size_region(int fd); 24 | 25 | #ifdef __cplusplus 26 | } 27 | #endif 28 | 29 | #ifndef __ASHMEMIOC /* in case someone included too */ 30 | 31 | #define ASHMEM_NAME_LEN 256 32 | 33 | #define ASHMEM_NAME_DEF "dev/ashmem" 34 | 35 | /* Return values from ASHMEM_PIN: Was the mapping purged while unpinned? */ 36 | #define ASHMEM_NOT_PURGED 0 37 | #define ASHMEM_WAS_PURGED 1 38 | 39 | /* Return values from ASHMEM_UNPIN: Is the mapping now pinned or unpinned? */ 40 | #define ASHMEM_IS_UNPINNED 0 41 | #define ASHMEM_IS_PINNED 1 42 | 43 | #endif /* ! __ASHMEMIOC */ 44 | 45 | #endif /* _CUTILS_ASHMEM_H */ 46 | -------------------------------------------------------------------------------- /include/cutils/atomic-inline.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010 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 ANDROID_CUTILS_ATOMIC_INLINE_H 18 | #define ANDROID_CUTILS_ATOMIC_INLINE_H 19 | 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | /* 25 | * Inline declarations and macros for some special-purpose atomic 26 | * operations. These are intended for rare circumstances where a 27 | * memory barrier needs to be issued inline rather than as a function 28 | * call. 29 | * 30 | * Most code should not use these. 31 | * 32 | * Anything that does include this file must set ANDROID_SMP to either 33 | * 0 or 1, indicating compilation for UP or SMP, respectively. 34 | * 35 | * Macros defined in this header: 36 | * 37 | * void ANDROID_MEMBAR_FULL(void) 38 | * Full memory barrier. Provides a compiler reordering barrier, and 39 | * on SMP systems emits an appropriate instruction. 40 | */ 41 | 42 | #if !defined(ANDROID_SMP) 43 | # error "Must define ANDROID_SMP before including atomic-inline.h" 44 | #endif 45 | 46 | #if defined(__arm__) 47 | #include 48 | #elif defined(__i386__) || defined(__x86_64__) 49 | #include 50 | #elif defined(__mips__) 51 | #include 52 | #else 53 | #error atomic operations are unsupported 54 | #endif 55 | 56 | #if ANDROID_SMP == 0 57 | #define ANDROID_MEMBAR_FULL android_compiler_barrier 58 | #else 59 | #define ANDROID_MEMBAR_FULL android_memory_barrier 60 | #endif 61 | 62 | #if ANDROID_SMP == 0 63 | #define ANDROID_MEMBAR_STORE android_compiler_barrier 64 | #else 65 | #define ANDROID_MEMBAR_STORE android_memory_store_barrier 66 | #endif 67 | 68 | #ifdef __cplusplus 69 | } 70 | #endif 71 | 72 | #endif /* ANDROID_CUTILS_ATOMIC_INLINE_H */ 73 | -------------------------------------------------------------------------------- /include/cutils/atomic-mips.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010 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 ANDROID_CUTILS_ATOMIC_MIPS_H 18 | #define ANDROID_CUTILS_ATOMIC_MIPS_H 19 | 20 | #include 21 | 22 | extern inline void android_compiler_barrier(void) 23 | { 24 | __asm__ __volatile__ ("" : : : "memory"); 25 | } 26 | 27 | #if ANDROID_SMP == 0 28 | extern inline void android_memory_barrier(void) 29 | { 30 | android_compiler_barrier(); 31 | } 32 | extern inline void android_memory_store_barrier(void) 33 | { 34 | android_compiler_barrier(); 35 | } 36 | #else 37 | extern inline void android_memory_barrier(void) 38 | { 39 | __asm__ __volatile__ ("sync" : : : "memory"); 40 | } 41 | extern inline void android_memory_store_barrier(void) 42 | { 43 | __asm__ __volatile__ ("sync" : : : "memory"); 44 | } 45 | #endif 46 | 47 | extern inline int32_t android_atomic_acquire_load(volatile const int32_t *ptr) 48 | { 49 | int32_t value = *ptr; 50 | android_memory_barrier(); 51 | return value; 52 | } 53 | 54 | extern inline int32_t android_atomic_release_load(volatile const int32_t *ptr) 55 | { 56 | android_memory_barrier(); 57 | return *ptr; 58 | } 59 | 60 | extern inline void android_atomic_acquire_store(int32_t value, 61 | volatile int32_t *ptr) 62 | { 63 | *ptr = value; 64 | android_memory_barrier(); 65 | } 66 | 67 | extern inline void android_atomic_release_store(int32_t value, 68 | volatile int32_t *ptr) 69 | { 70 | android_memory_barrier(); 71 | *ptr = value; 72 | } 73 | 74 | extern inline int android_atomic_cas(int32_t old_value, int32_t new_value, 75 | volatile int32_t *ptr) 76 | { 77 | int32_t prev, status; 78 | do { 79 | __asm__ __volatile__ ( 80 | " ll %[prev], (%[ptr])\n" 81 | " li %[status], 1\n" 82 | " bne %[prev], %[old], 9f\n" 83 | " move %[status], %[new_value]\n" 84 | " sc %[status], (%[ptr])\n" 85 | "9:\n" 86 | : [prev] "=&r" (prev), [status] "=&r" (status) 87 | : [ptr] "r" (ptr), [old] "r" (old_value), [new_value] "r" (new_value) 88 | ); 89 | } while (__builtin_expect(status == 0, 0)); 90 | return prev != old_value; 91 | } 92 | 93 | extern inline int android_atomic_acquire_cas(int32_t old_value, 94 | int32_t new_value, 95 | volatile int32_t *ptr) 96 | { 97 | int status = android_atomic_cas(old_value, new_value, ptr); 98 | android_memory_barrier(); 99 | return status; 100 | } 101 | 102 | extern inline int android_atomic_release_cas(int32_t old_value, 103 | int32_t new_value, 104 | volatile int32_t *ptr) 105 | { 106 | android_memory_barrier(); 107 | return android_atomic_cas(old_value, new_value, ptr); 108 | } 109 | 110 | 111 | extern inline int32_t android_atomic_swap(int32_t new_value, 112 | volatile int32_t *ptr) 113 | { 114 | int32_t prev, status; 115 | do { 116 | __asm__ __volatile__ ( 117 | " move %[status], %[new_value]\n" 118 | " ll %[prev], (%[ptr])\n" 119 | " sc %[status], (%[ptr])\n" 120 | : [prev] "=&r" (prev), [status] "=&r" (status) 121 | : [ptr] "r" (ptr), [new_value] "r" (new_value) 122 | ); 123 | } while (__builtin_expect(status == 0, 0)); 124 | android_memory_barrier(); 125 | return prev; 126 | } 127 | 128 | extern inline int32_t android_atomic_add(int32_t increment, 129 | volatile int32_t *ptr) 130 | { 131 | int32_t prev, status; 132 | android_memory_barrier(); 133 | do { 134 | __asm__ __volatile__ ( 135 | " ll %[prev], (%[ptr])\n" 136 | " addu %[status], %[prev], %[inc]\n" 137 | " sc %[status], (%[ptr])\n" 138 | : [status] "=&r" (status), [prev] "=&r" (prev) 139 | : [ptr] "r" (ptr), [inc] "Ir" (increment) 140 | ); 141 | } while (__builtin_expect(status == 0, 0)); 142 | return prev; 143 | } 144 | 145 | extern inline int32_t android_atomic_inc(volatile int32_t *addr) 146 | { 147 | return android_atomic_add(1, addr); 148 | } 149 | 150 | extern inline int32_t android_atomic_dec(volatile int32_t *addr) 151 | { 152 | return android_atomic_add(-1, addr); 153 | } 154 | 155 | extern inline int32_t android_atomic_and(int32_t value, volatile int32_t *ptr) 156 | { 157 | int32_t prev, status; 158 | android_memory_barrier(); 159 | do { 160 | __asm__ __volatile__ ( 161 | " ll %[prev], (%[ptr])\n" 162 | " and %[status], %[prev], %[value]\n" 163 | " sc %[status], (%[ptr])\n" 164 | : [prev] "=&r" (prev), [status] "=&r" (status) 165 | : [ptr] "r" (ptr), [value] "Ir" (value) 166 | ); 167 | } while (__builtin_expect(status == 0, 0)); 168 | return prev; 169 | } 170 | 171 | extern inline int32_t android_atomic_or(int32_t value, volatile int32_t *ptr) 172 | { 173 | int32_t prev, status; 174 | android_memory_barrier(); 175 | do { 176 | __asm__ __volatile__ ( 177 | " ll %[prev], (%[ptr])\n" 178 | " or %[status], %[prev], %[value]\n" 179 | " sc %[status], (%[ptr])\n" 180 | : [prev] "=&r" (prev), [status] "=&r" (status) 181 | : [ptr] "r" (ptr), [value] "Ir" (value) 182 | ); 183 | } while (__builtin_expect(status == 0, 0)); 184 | return prev; 185 | } 186 | 187 | #endif /* ANDROID_CUTILS_ATOMIC_MIPS_H */ 188 | -------------------------------------------------------------------------------- /include/cutils/atomic-x86.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010 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 ANDROID_CUTILS_ATOMIC_X86_H 18 | #define ANDROID_CUTILS_ATOMIC_X86_H 19 | 20 | #include 21 | 22 | extern inline void android_compiler_barrier(void) 23 | { 24 | __asm__ __volatile__ ("" : : : "memory"); 25 | } 26 | 27 | #if ANDROID_SMP == 0 28 | extern inline void android_memory_barrier(void) 29 | { 30 | android_compiler_barrier(); 31 | } 32 | extern inline void android_memory_store_barrier(void) 33 | { 34 | android_compiler_barrier(); 35 | } 36 | #else 37 | extern inline void android_memory_barrier(void) 38 | { 39 | __asm__ __volatile__ ("mfence" : : : "memory"); 40 | } 41 | extern inline void android_memory_store_barrier(void) 42 | { 43 | android_compiler_barrier(); 44 | } 45 | #endif 46 | 47 | extern inline int32_t android_atomic_acquire_load(volatile const int32_t *ptr) 48 | { 49 | int32_t value = *ptr; 50 | android_compiler_barrier(); 51 | return value; 52 | } 53 | 54 | extern inline int32_t android_atomic_release_load(volatile const int32_t *ptr) 55 | { 56 | android_memory_barrier(); 57 | return *ptr; 58 | } 59 | 60 | extern inline void android_atomic_acquire_store(int32_t value, 61 | volatile int32_t *ptr) 62 | { 63 | *ptr = value; 64 | android_memory_barrier(); 65 | } 66 | 67 | extern inline void android_atomic_release_store(int32_t value, 68 | volatile int32_t *ptr) 69 | { 70 | android_compiler_barrier(); 71 | *ptr = value; 72 | } 73 | 74 | extern inline int android_atomic_cas(int32_t old_value, int32_t new_value, 75 | volatile int32_t *ptr) 76 | { 77 | int32_t prev; 78 | __asm__ __volatile__ ("lock; cmpxchgl %1, %2" 79 | : "=a" (prev) 80 | : "q" (new_value), "m" (*ptr), "0" (old_value) 81 | : "memory"); 82 | return prev != old_value; 83 | } 84 | 85 | extern inline int android_atomic_acquire_cas(int32_t old_value, 86 | int32_t new_value, 87 | volatile int32_t *ptr) 88 | { 89 | /* Loads are not reordered with other loads. */ 90 | return android_atomic_cas(old_value, new_value, ptr); 91 | } 92 | 93 | extern inline int android_atomic_release_cas(int32_t old_value, 94 | int32_t new_value, 95 | volatile int32_t *ptr) 96 | { 97 | /* Stores are not reordered with other stores. */ 98 | return android_atomic_cas(old_value, new_value, ptr); 99 | } 100 | 101 | extern inline int32_t android_atomic_add(int32_t increment, 102 | volatile int32_t *ptr) 103 | { 104 | __asm__ __volatile__ ("lock; xaddl %0, %1" 105 | : "+r" (increment), "+m" (*ptr) 106 | : : "memory"); 107 | /* increment now holds the old value of *ptr */ 108 | return increment; 109 | } 110 | 111 | extern inline int32_t android_atomic_inc(volatile int32_t *addr) 112 | { 113 | return android_atomic_add(1, addr); 114 | } 115 | 116 | extern inline int32_t android_atomic_dec(volatile int32_t *addr) 117 | { 118 | return android_atomic_add(-1, addr); 119 | } 120 | 121 | extern inline int32_t android_atomic_and(int32_t value, 122 | volatile int32_t *ptr) 123 | { 124 | int32_t prev, status; 125 | do { 126 | prev = *ptr; 127 | status = android_atomic_cas(prev, prev & value, ptr); 128 | } while (__builtin_expect(status != 0, 0)); 129 | return prev; 130 | } 131 | 132 | extern inline int32_t android_atomic_or(int32_t value, volatile int32_t *ptr) 133 | { 134 | int32_t prev, status; 135 | do { 136 | prev = *ptr; 137 | status = android_atomic_cas(prev, prev | value, ptr); 138 | } while (__builtin_expect(status != 0, 0)); 139 | return prev; 140 | } 141 | 142 | #endif /* ANDROID_CUTILS_ATOMIC_X86_H */ 143 | -------------------------------------------------------------------------------- /include/cutils/atomic.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 ANDROID_CUTILS_ATOMIC_H 18 | #define ANDROID_CUTILS_ATOMIC_H 19 | 20 | #include 21 | #include 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | /* 28 | * A handful of basic atomic operations. The appropriate pthread 29 | * functions should be used instead of these whenever possible. 30 | * 31 | * The "acquire" and "release" terms can be defined intuitively in terms 32 | * of the placement of memory barriers in a simple lock implementation: 33 | * - wait until compare-and-swap(lock-is-free --> lock-is-held) succeeds 34 | * - barrier 35 | * - [do work] 36 | * - barrier 37 | * - store(lock-is-free) 38 | * In very crude terms, the initial (acquire) barrier prevents any of the 39 | * "work" from happening before the lock is held, and the later (release) 40 | * barrier ensures that all of the work happens before the lock is released. 41 | * (Think of cached writes, cache read-ahead, and instruction reordering 42 | * around the CAS and store instructions.) 43 | * 44 | * The barriers must apply to both the compiler and the CPU. Note it is 45 | * legal for instructions that occur before an "acquire" barrier to be 46 | * moved down below it, and for instructions that occur after a "release" 47 | * barrier to be moved up above it. 48 | * 49 | * The ARM-driven implementation we use here is short on subtlety, 50 | * and actually requests a full barrier from the compiler and the CPU. 51 | * The only difference between acquire and release is in whether they 52 | * are issued before or after the atomic operation with which they 53 | * are associated. To ease the transition to C/C++ atomic intrinsics, 54 | * you should not rely on this, and instead assume that only the minimal 55 | * acquire/release protection is provided. 56 | * 57 | * NOTE: all int32_t* values are expected to be aligned on 32-bit boundaries. 58 | * If they are not, atomicity is not guaranteed. 59 | */ 60 | 61 | /* 62 | * Basic arithmetic and bitwise operations. These all provide a 63 | * barrier with "release" ordering, and return the previous value. 64 | * 65 | * These have the same characteristics (e.g. what happens on overflow) 66 | * as the equivalent non-atomic C operations. 67 | */ 68 | int32_t android_atomic_inc(volatile int32_t* addr); 69 | int32_t android_atomic_dec(volatile int32_t* addr); 70 | int32_t android_atomic_add(int32_t value, volatile int32_t* addr); 71 | int32_t android_atomic_and(int32_t value, volatile int32_t* addr); 72 | int32_t android_atomic_or(int32_t value, volatile int32_t* addr); 73 | 74 | /* 75 | * Perform an atomic load with "acquire" or "release" ordering. 76 | * 77 | * This is only necessary if you need the memory barrier. A 32-bit read 78 | * from a 32-bit aligned address is atomic on all supported platforms. 79 | */ 80 | int32_t android_atomic_acquire_load(volatile const int32_t* addr); 81 | int32_t android_atomic_release_load(volatile const int32_t* addr); 82 | 83 | /* 84 | * Perform an atomic store with "acquire" or "release" ordering. 85 | * 86 | * This is only necessary if you need the memory barrier. A 32-bit write 87 | * to a 32-bit aligned address is atomic on all supported platforms. 88 | */ 89 | void android_atomic_acquire_store(int32_t value, volatile int32_t* addr); 90 | void android_atomic_release_store(int32_t value, volatile int32_t* addr); 91 | 92 | /* 93 | * Compare-and-set operation with "acquire" or "release" ordering. 94 | * 95 | * This returns zero if the new value was successfully stored, which will 96 | * only happen when *addr == oldvalue. 97 | * 98 | * (The return value is inverted from implementations on other platforms, 99 | * but matches the ARM ldrex/strex result.) 100 | * 101 | * Implementations that use the release CAS in a loop may be less efficient 102 | * than possible, because we re-issue the memory barrier on each iteration. 103 | */ 104 | int android_atomic_acquire_cas(int32_t oldvalue, int32_t newvalue, 105 | volatile int32_t* addr); 106 | int android_atomic_release_cas(int32_t oldvalue, int32_t newvalue, 107 | volatile int32_t* addr); 108 | 109 | /* 110 | * Aliases for code using an older version of this header. These are now 111 | * deprecated and should not be used. The definitions will be removed 112 | * in a future release. 113 | */ 114 | #define android_atomic_write android_atomic_release_store 115 | #define android_atomic_cmpxchg android_atomic_release_cas 116 | 117 | #ifdef __cplusplus 118 | } // extern "C" 119 | #endif 120 | 121 | #endif // ANDROID_CUTILS_ATOMIC_H 122 | -------------------------------------------------------------------------------- /include/cutils/bitops.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 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 __CUTILS_BITOPS_H 18 | #define __CUTILS_BITOPS_H 19 | 20 | #include 21 | 22 | __BEGIN_DECLS 23 | 24 | static inline int popcount(unsigned int x) 25 | { 26 | return __builtin_popcount(x); 27 | } 28 | 29 | static inline int popcountl(unsigned long x) 30 | { 31 | return __builtin_popcountl(x); 32 | } 33 | 34 | static inline int popcountll(unsigned long long x) 35 | { 36 | return __builtin_popcountll(x); 37 | } 38 | 39 | __END_DECLS 40 | 41 | #endif /* __CUTILS_BITOPS_H */ 42 | -------------------------------------------------------------------------------- /include/cutils/compiler.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 ANDROID_CUTILS_COMPILER_H 18 | #define ANDROID_CUTILS_COMPILER_H 19 | 20 | /* 21 | * helps the compiler's optimizer predicting branches 22 | */ 23 | 24 | #ifdef __cplusplus 25 | # define CC_LIKELY( exp ) (__builtin_expect( !!(exp), true )) 26 | # define CC_UNLIKELY( exp ) (__builtin_expect( !!(exp), false )) 27 | #else 28 | # define CC_LIKELY( exp ) (__builtin_expect( !!(exp), 1 )) 29 | # define CC_UNLIKELY( exp ) (__builtin_expect( !!(exp), 0 )) 30 | #endif 31 | 32 | /** 33 | * exports marked symbols 34 | * 35 | * if used on a C++ class declaration, this macro must be inserted 36 | * after the "class" keyword. For instance: 37 | * 38 | * template 39 | * class ANDROID_API Singleton { } 40 | */ 41 | 42 | #define ANDROID_API __attribute__((visibility("default"))) 43 | 44 | #endif // ANDROID_CUTILS_COMPILER_H 45 | -------------------------------------------------------------------------------- /include/cutils/config_utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2006 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 __CUTILS_CONFIG_UTILS_H 18 | #define __CUTILS_CONFIG_UTILS_H 19 | 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | typedef struct cnode cnode; 25 | 26 | 27 | struct cnode 28 | { 29 | cnode *next; 30 | cnode *first_child; 31 | cnode *last_child; 32 | const char *name; 33 | const char *value; 34 | }; 35 | 36 | /* parse a text string into a config node tree */ 37 | void config_load(cnode *root, char *data); 38 | 39 | /* parse a file into a config node tree */ 40 | void config_load_file(cnode *root, const char *fn); 41 | 42 | /* create a single config node */ 43 | cnode* config_node(const char *name, const char *value); 44 | 45 | /* locate a named child of a config node */ 46 | cnode* config_find(cnode *root, const char *name); 47 | 48 | /* look up a child by name and return the boolean value */ 49 | int config_bool(cnode *root, const char *name, int _default); 50 | 51 | /* look up a child by name and return the string value */ 52 | const char* config_str(cnode *root, const char *name, const char *_default); 53 | 54 | /* add a named child to a config node (or modify it if it already exists) */ 55 | void config_set(cnode *root, const char *name, const char *value); 56 | 57 | /* free a config node tree */ 58 | void config_free(cnode *root); 59 | 60 | #ifdef __cplusplus 61 | } 62 | #endif 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /include/cutils/cpu_info.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 __CUTILS_CPU_INFO_H 18 | #define __CUTILS_CPU_INFO_H 19 | 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | /* returns a string contiaining an ASCII representation of the CPU serial number, 25 | ** or NULL if cpu info not available. 26 | ** The string is a static variable, so don't call free() on it. 27 | */ 28 | extern const char* get_cpu_serial_number(void); 29 | 30 | #ifdef __cplusplus 31 | } 32 | #endif 33 | 34 | #endif /* __CUTILS_CPU_INFO_H */ 35 | -------------------------------------------------------------------------------- /include/cutils/debugger.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 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 __CUTILS_DEBUGGER_H 18 | #define __CUTILS_DEBUGGER_H 19 | 20 | #include 21 | 22 | #ifdef __cplusplus 23 | extern "C" { 24 | #endif 25 | 26 | #define DEBUGGER_SOCKET_NAME "android:debuggerd" 27 | 28 | typedef enum { 29 | // dump a crash 30 | DEBUGGER_ACTION_CRASH, 31 | // dump a tombstone file 32 | DEBUGGER_ACTION_DUMP_TOMBSTONE, 33 | // dump a backtrace only back to the socket 34 | DEBUGGER_ACTION_DUMP_BACKTRACE, 35 | } debugger_action_t; 36 | 37 | /* message sent over the socket */ 38 | typedef struct { 39 | debugger_action_t action; 40 | pid_t tid; 41 | } debugger_msg_t; 42 | 43 | /* Dumps a process backtrace, registers, and stack to a tombstone file (requires root). 44 | * Stores the tombstone path in the provided buffer. 45 | * Returns 0 on success, -1 on error. 46 | */ 47 | int dump_tombstone(pid_t tid, char* pathbuf, size_t pathlen); 48 | 49 | /* Dumps a process backtrace only to the specified file (requires root). 50 | * Returns 0 on success, -1 on error. 51 | */ 52 | int dump_backtrace_to_file(pid_t tid, int fd); 53 | 54 | #ifdef __cplusplus 55 | } 56 | #endif 57 | 58 | #endif /* __CUTILS_DEBUGGER_H */ 59 | -------------------------------------------------------------------------------- /include/cutils/dir_hash.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 | typedef enum { 18 | SHA_1, 19 | } HashAlgorithm; 20 | 21 | int get_file_hash(HashAlgorithm algorithm, const char *path, 22 | char *output_string, size_t max_output_string); 23 | 24 | int get_recursive_hash_manifest(HashAlgorithm algorithm, 25 | const char *directory_path, 26 | char **output_string); 27 | -------------------------------------------------------------------------------- /include/cutils/event_tag_map.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 _LIBS_CUTILS_EVENTTAGMAP_H 18 | #define _LIBS_CUTILS_EVENTTAGMAP_H 19 | 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | #define EVENT_TAG_MAP_FILE "/system/etc/event-log-tags" 25 | 26 | struct EventTagMap; 27 | typedef struct EventTagMap EventTagMap; 28 | 29 | /* 30 | * Open the specified file as an event log tag map. 31 | * 32 | * Returns NULL on failure. 33 | */ 34 | EventTagMap* android_openEventTagMap(const char* fileName); 35 | 36 | /* 37 | * Close the map. 38 | */ 39 | void android_closeEventTagMap(EventTagMap* map); 40 | 41 | /* 42 | * Look up a tag by index. Returns the tag string, or NULL if not found. 43 | */ 44 | const char* android_lookupEventTag(const EventTagMap* map, int tag); 45 | 46 | #ifdef __cplusplus 47 | } 48 | #endif 49 | 50 | #endif /*_LIBS_CUTILS_EVENTTAGMAP_H*/ 51 | -------------------------------------------------------------------------------- /include/cutils/fs.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 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 __CUTILS_FS_H 18 | #define __CUTILS_FS_H 19 | 20 | #include 21 | 22 | /* 23 | * TEMP_FAILURE_RETRY is defined by some, but not all, versions of 24 | * . (Alas, it is not as standard as we'd hoped!) So, if it's 25 | * not already defined, then define it here. 26 | */ 27 | #ifndef TEMP_FAILURE_RETRY 28 | /* Used to retry syscalls that can return EINTR. */ 29 | #define TEMP_FAILURE_RETRY(exp) ({ \ 30 | typeof (exp) _rc; \ 31 | do { \ 32 | _rc = (exp); \ 33 | } while (_rc == -1 && errno == EINTR); \ 34 | _rc; }) 35 | #endif 36 | 37 | #ifdef __cplusplus 38 | extern "C" { 39 | #endif 40 | 41 | /* 42 | * Ensure that directory exists with given mode and owners. 43 | */ 44 | extern int fs_prepare_dir(const char* path, mode_t mode, uid_t uid, gid_t gid); 45 | 46 | /* 47 | * Read single plaintext integer from given file, correctly handling files 48 | * partially written with fs_write_atomic_int(). 49 | */ 50 | extern int fs_read_atomic_int(const char* path, int* value); 51 | 52 | /* 53 | * Write single plaintext integer to given file, creating backup while 54 | * in progress. 55 | */ 56 | extern int fs_write_atomic_int(const char* path, int value); 57 | 58 | #ifdef __cplusplus 59 | } 60 | #endif 61 | 62 | #endif /* __CUTILS_FS_H */ 63 | -------------------------------------------------------------------------------- /include/cutils/hashmap.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 | /** 18 | * Hash map. 19 | */ 20 | 21 | #ifndef __HASHMAP_H 22 | #define __HASHMAP_H 23 | 24 | #include 25 | #include 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | /** A hash map. */ 32 | typedef struct Hashmap Hashmap; 33 | 34 | /** 35 | * Creates a new hash map. Returns NULL if memory allocation fails. 36 | * 37 | * @param initialCapacity number of expected entries 38 | * @param hash function which hashes keys 39 | * @param equals function which compares keys for equality 40 | */ 41 | Hashmap* hashmapCreate(size_t initialCapacity, 42 | int (*hash)(void* key), bool (*equals)(void* keyA, void* keyB)); 43 | 44 | /** 45 | * Frees the hash map. Does not free the keys or values themselves. 46 | */ 47 | void hashmapFree(Hashmap* map); 48 | 49 | /** 50 | * Hashes the memory pointed to by key with the given size. Useful for 51 | * implementing hash functions. 52 | */ 53 | int hashmapHash(void* key, size_t keySize); 54 | 55 | /** 56 | * Puts value for the given key in the map. Returns pre-existing value if 57 | * any. 58 | * 59 | * If memory allocation fails, this function returns NULL, the map's size 60 | * does not increase, and errno is set to ENOMEM. 61 | */ 62 | void* hashmapPut(Hashmap* map, void* key, void* value); 63 | 64 | /** 65 | * Gets a value from the map. Returns NULL if no entry for the given key is 66 | * found or if the value itself is NULL. 67 | */ 68 | void* hashmapGet(Hashmap* map, void* key); 69 | 70 | /** 71 | * Returns true if the map contains an entry for the given key. 72 | */ 73 | bool hashmapContainsKey(Hashmap* map, void* key); 74 | 75 | /** 76 | * Gets the value for a key. If a value is not found, this function gets a 77 | * value and creates an entry using the given callback. 78 | * 79 | * If memory allocation fails, the callback is not called, this function 80 | * returns NULL, and errno is set to ENOMEM. 81 | */ 82 | void* hashmapMemoize(Hashmap* map, void* key, 83 | void* (*initialValue)(void* key, void* context), void* context); 84 | 85 | /** 86 | * Removes an entry from the map. Returns the removed value or NULL if no 87 | * entry was present. 88 | */ 89 | void* hashmapRemove(Hashmap* map, void* key); 90 | 91 | /** 92 | * Gets the number of entries in this map. 93 | */ 94 | size_t hashmapSize(Hashmap* map); 95 | 96 | /** 97 | * Invokes the given callback on each entry in the map. Stops iterating if 98 | * the callback returns false. 99 | */ 100 | void hashmapForEach(Hashmap* map, 101 | bool (*callback)(void* key, void* value, void* context), 102 | void* context); 103 | 104 | /** 105 | * Concurrency support. 106 | */ 107 | 108 | /** 109 | * Locks the hash map so only the current thread can access it. 110 | */ 111 | void hashmapLock(Hashmap* map); 112 | 113 | /** 114 | * Unlocks the hash map so other threads can access it. 115 | */ 116 | void hashmapUnlock(Hashmap* map); 117 | 118 | /** 119 | * Key utilities. 120 | */ 121 | 122 | /** 123 | * Hashes int keys. 'key' is a pointer to int. 124 | */ 125 | int hashmapIntHash(void* key); 126 | 127 | /** 128 | * Compares two int keys for equality. 129 | */ 130 | bool hashmapIntEquals(void* keyA, void* keyB); 131 | 132 | /** 133 | * For debugging. 134 | */ 135 | 136 | /** 137 | * Gets current capacity. 138 | */ 139 | size_t hashmapCurrentCapacity(Hashmap* map); 140 | 141 | /** 142 | * Counts the number of entry collisions. 143 | */ 144 | size_t hashmapCountCollisions(Hashmap* map); 145 | 146 | #ifdef __cplusplus 147 | } 148 | #endif 149 | 150 | #endif /* __HASHMAP_H */ 151 | -------------------------------------------------------------------------------- /include/cutils/iosched_policy.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 __CUTILS_IOSCHED_POLICY_H 18 | #define __CUTILS_IOSCHED_POLICY_H 19 | 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | typedef enum { 25 | IoSchedClass_NONE, 26 | IoSchedClass_RT, 27 | IoSchedClass_BE, 28 | IoSchedClass_IDLE, 29 | } IoSchedClass; 30 | 31 | extern int android_set_ioprio(int pid, IoSchedClass clazz, int ioprio); 32 | extern int android_get_ioprio(int pid, IoSchedClass *clazz, int *ioprio); 33 | 34 | #ifdef __cplusplus 35 | } 36 | #endif 37 | 38 | #endif /* __CUTILS_IOSCHED_POLICY_H */ 39 | -------------------------------------------------------------------------------- /include/cutils/jstring.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2006 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 __CUTILS_STRING16_H 18 | #define __CUTILS_STRING16_H 19 | 20 | #include 21 | #include 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | typedef uint16_t char16_t; 28 | 29 | extern char * strndup16to8 (const char16_t* s, size_t n); 30 | extern size_t strnlen16to8 (const char16_t* s, size_t n); 31 | extern char * strncpy16to8 (char *dest, const char16_t*s, size_t n); 32 | 33 | extern char16_t * strdup8to16 (const char* s, size_t *out_len); 34 | extern size_t strlen8to16 (const char* utf8Str); 35 | extern char16_t * strcpy8to16 (char16_t *dest, const char*s, size_t *out_len); 36 | extern char16_t * strcpylen8to16 (char16_t *dest, const char*s, int length, 37 | size_t *out_len); 38 | 39 | #ifdef __cplusplus 40 | } 41 | #endif 42 | 43 | #endif /* __CUTILS_STRING16_H */ 44 | -------------------------------------------------------------------------------- /include/cutils/klog.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010 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 _CUTILS_KLOG_H_ 18 | #define _CUTILS_KLOG_H_ 19 | 20 | void klog_init(void); 21 | void klog_set_level(int level); 22 | void klog_close(void); 23 | void klog_write(int level, const char *fmt, ...) 24 | __attribute__ ((format(printf, 2, 3))); 25 | 26 | #define KLOG_ERROR(tag,x...) klog_write(3, "<3>" tag ": " x) 27 | #define KLOG_WARNING(tag,x...) klog_write(4, "<4>" tag ": " x) 28 | #define KLOG_NOTICE(tag,x...) klog_write(5, "<5>" tag ": " x) 29 | #define KLOG_INFO(tag,x...) klog_write(6, "<6>" tag ": " x) 30 | #define KLOG_DEBUG(tag,x...) klog_write(7, "<7>" tag ": " x) 31 | 32 | #define KLOG_DEFAULT_LEVEL 3 /* messages <= this level are logged */ 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /include/cutils/list.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010 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 _CUTILS_LIST_H_ 18 | #define _CUTILS_LIST_H_ 19 | 20 | #include 21 | 22 | #ifdef __cplusplus 23 | extern "C" { 24 | #endif /* __cplusplus */ 25 | 26 | struct listnode 27 | { 28 | struct listnode *next; 29 | struct listnode *prev; 30 | }; 31 | 32 | #define node_to_item(node, container, member) \ 33 | (container *) (((char*) (node)) - offsetof(container, member)) 34 | 35 | #define list_declare(name) \ 36 | struct listnode name = { \ 37 | .next = &name, \ 38 | .prev = &name, \ 39 | } 40 | 41 | #define list_for_each(node, list) \ 42 | for (node = (list)->next; node != (list); node = node->next) 43 | 44 | #define list_for_each_reverse(node, list) \ 45 | for (node = (list)->prev; node != (list); node = node->prev) 46 | 47 | void list_init(struct listnode *list); 48 | void list_add_tail(struct listnode *list, struct listnode *item); 49 | void list_remove(struct listnode *item); 50 | 51 | #define list_empty(list) ((list) == (list)->next) 52 | #define list_head(list) ((list)->next) 53 | #define list_tail(list) ((list)->prev) 54 | 55 | #ifdef __cplusplus 56 | }; 57 | #endif /* __cplusplus */ 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /include/cutils/logd.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 _ANDROID_CUTILS_LOGD_H 18 | #define _ANDROID_CUTILS_LOGD_H 19 | 20 | /* the stable/frozen log-related definitions have been 21 | * moved to this header, which is exposed by the NDK 22 | */ 23 | #include 24 | 25 | /* the rest is only used internally by the system */ 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #ifdef HAVE_PTHREADS 32 | #include 33 | #endif 34 | #include 35 | #include 36 | 37 | #ifdef __cplusplus 38 | extern "C" { 39 | #endif 40 | 41 | int __android_log_bwrite(int32_t tag, const void *payload, size_t len); 42 | int __android_log_btwrite(int32_t tag, char type, const void *payload, 43 | size_t len); 44 | 45 | #ifdef __cplusplus 46 | } 47 | #endif 48 | 49 | #endif /* _LOGD_H */ 50 | -------------------------------------------------------------------------------- /include/cutils/logger.h: -------------------------------------------------------------------------------- 1 | /* utils/logger.h 2 | ** 3 | ** Copyright 2007, The Android Open Source Project 4 | ** 5 | ** This file is dual licensed. It may be redistributed and/or modified 6 | ** under the terms of the Apache 2.0 License OR version 2 of the GNU 7 | ** General Public License. 8 | */ 9 | 10 | #ifndef _UTILS_LOGGER_H 11 | #define _UTILS_LOGGER_H 12 | 13 | #include 14 | 15 | /* 16 | * The userspace structure for version 1 of the logger_entry ABI. 17 | * This structure is returned to userspace by the kernel logger 18 | * driver unless an upgrade to a newer ABI version is requested. 19 | */ 20 | struct logger_entry { 21 | uint16_t len; /* length of the payload */ 22 | uint16_t __pad; /* no matter what, we get 2 bytes of padding */ 23 | int32_t pid; /* generating process's pid */ 24 | int32_t tid; /* generating process's tid */ 25 | int32_t sec; /* seconds since Epoch */ 26 | int32_t nsec; /* nanoseconds */ 27 | char msg[0]; /* the entry's payload */ 28 | }; 29 | 30 | /* 31 | * The userspace structure for version 2 of the logger_entry ABI. 32 | * This structure is returned to userspace if ioctl(LOGGER_SET_VERSION) 33 | * is called with version==2 34 | */ 35 | struct logger_entry_v2 { 36 | uint16_t len; /* length of the payload */ 37 | uint16_t hdr_size; /* sizeof(struct logger_entry_v2) */ 38 | int32_t pid; /* generating process's pid */ 39 | int32_t tid; /* generating process's tid */ 40 | int32_t sec; /* seconds since Epoch */ 41 | int32_t nsec; /* nanoseconds */ 42 | uint32_t euid; /* effective UID of logger */ 43 | char msg[0]; /* the entry's payload */ 44 | }; 45 | 46 | #define LOGGER_LOG_MAIN "log/main" 47 | #define LOGGER_LOG_RADIO "log/radio" 48 | #define LOGGER_LOG_EVENTS "log/events" 49 | #define LOGGER_LOG_SYSTEM "log/system" 50 | 51 | /* 52 | * The maximum size of the log entry payload that can be 53 | * written to the kernel logger driver. An attempt to write 54 | * more than this amount to /dev/log/* will result in a 55 | * truncated log entry. 56 | */ 57 | #define LOGGER_ENTRY_MAX_PAYLOAD 4076 58 | 59 | /* 60 | * The maximum size of a log entry which can be read from the 61 | * kernel logger driver. An attempt to read less than this amount 62 | * may result in read() returning EINVAL. 63 | */ 64 | #define LOGGER_ENTRY_MAX_LEN (5*1024) 65 | 66 | #ifdef HAVE_IOCTL 67 | 68 | #include 69 | 70 | #define __LOGGERIO 0xAE 71 | 72 | #define LOGGER_GET_LOG_BUF_SIZE _IO(__LOGGERIO, 1) /* size of log */ 73 | #define LOGGER_GET_LOG_LEN _IO(__LOGGERIO, 2) /* used log len */ 74 | #define LOGGER_GET_NEXT_ENTRY_LEN _IO(__LOGGERIO, 3) /* next entry len */ 75 | #define LOGGER_FLUSH_LOG _IO(__LOGGERIO, 4) /* flush log */ 76 | #define LOGGER_GET_VERSION _IO(__LOGGERIO, 5) /* abi version */ 77 | #define LOGGER_SET_VERSION _IO(__LOGGERIO, 6) /* abi version */ 78 | 79 | #endif // HAVE_IOCTL 80 | 81 | #endif /* _UTILS_LOGGER_H */ 82 | -------------------------------------------------------------------------------- /include/cutils/logprint.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2006 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 _LOGPRINT_H 18 | #define _LOGPRINT_H 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | 29 | typedef enum { 30 | FORMAT_OFF = 0, 31 | FORMAT_BRIEF, 32 | FORMAT_PROCESS, 33 | FORMAT_TAG, 34 | FORMAT_THREAD, 35 | FORMAT_RAW, 36 | FORMAT_TIME, 37 | FORMAT_THREADTIME, 38 | FORMAT_LONG, 39 | } AndroidLogPrintFormat; 40 | 41 | typedef struct AndroidLogFormat_t AndroidLogFormat; 42 | 43 | typedef struct AndroidLogEntry_t { 44 | time_t tv_sec; 45 | long tv_nsec; 46 | android_LogPriority priority; 47 | int32_t pid; 48 | int32_t tid; 49 | const char * tag; 50 | size_t messageLen; 51 | const char * message; 52 | } AndroidLogEntry; 53 | 54 | AndroidLogFormat *android_log_format_new(); 55 | 56 | void android_log_format_free(AndroidLogFormat *p_format); 57 | 58 | void android_log_setPrintFormat(AndroidLogFormat *p_format, 59 | AndroidLogPrintFormat format); 60 | 61 | /** 62 | * Returns FORMAT_OFF on invalid string 63 | */ 64 | AndroidLogPrintFormat android_log_formatFromString(const char *s); 65 | 66 | /** 67 | * filterExpression: a single filter expression 68 | * eg "AT:d" 69 | * 70 | * returns 0 on success and -1 on invalid expression 71 | * 72 | * Assumes single threaded execution 73 | * 74 | */ 75 | 76 | int android_log_addFilterRule(AndroidLogFormat *p_format, 77 | const char *filterExpression); 78 | 79 | 80 | /** 81 | * filterString: a whitespace-separated set of filter expressions 82 | * eg "AT:d *:i" 83 | * 84 | * returns 0 on success and -1 on invalid expression 85 | * 86 | * Assumes single threaded execution 87 | * 88 | */ 89 | 90 | int android_log_addFilterString(AndroidLogFormat *p_format, 91 | const char *filterString); 92 | 93 | 94 | /** 95 | * returns 1 if this log line should be printed based on its priority 96 | * and tag, and 0 if it should not 97 | */ 98 | int android_log_shouldPrintLine ( 99 | AndroidLogFormat *p_format, const char *tag, android_LogPriority pri); 100 | 101 | 102 | /** 103 | * Splits a wire-format buffer into an AndroidLogEntry 104 | * entry allocated by caller. Pointers will point directly into buf 105 | * 106 | * Returns 0 on success and -1 on invalid wire format (entry will be 107 | * in unspecified state) 108 | */ 109 | int android_log_processLogBuffer(struct logger_entry *buf, 110 | AndroidLogEntry *entry); 111 | 112 | /** 113 | * Like android_log_processLogBuffer, but for binary logs. 114 | * 115 | * If "map" is non-NULL, it will be used to convert the log tag number 116 | * into a string. 117 | */ 118 | int android_log_processBinaryLogBuffer(struct logger_entry *buf, 119 | AndroidLogEntry *entry, const EventTagMap* map, char* messageBuf, 120 | int messageBufLen); 121 | 122 | 123 | /** 124 | * Formats a log message into a buffer 125 | * 126 | * Uses defaultBuffer if it can, otherwise malloc()'s a new buffer 127 | * If return value != defaultBuffer, caller must call free() 128 | * Returns NULL on malloc error 129 | */ 130 | 131 | char *android_log_formatLogLine ( 132 | AndroidLogFormat *p_format, 133 | char *defaultBuffer, 134 | size_t defaultBufferSize, 135 | const AndroidLogEntry *p_line, 136 | size_t *p_outLength); 137 | 138 | 139 | /** 140 | * Either print or do not print log line, based on filter 141 | * 142 | * Assumes single threaded execution 143 | * 144 | */ 145 | int android_log_printLogLine( 146 | AndroidLogFormat *p_format, 147 | int fd, 148 | const AndroidLogEntry *entry); 149 | 150 | 151 | #ifdef __cplusplus 152 | } 153 | #endif 154 | 155 | 156 | #endif /*_LOGPRINT_H*/ 157 | -------------------------------------------------------------------------------- /include/cutils/memory.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2006 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 ANDROID_CUTILS_MEMORY_H 18 | #define ANDROID_CUTILS_MEMORY_H 19 | 20 | #include 21 | #include 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | /* size is given in bytes and must be multiple of 2 */ 28 | void android_memset16(uint16_t* dst, uint16_t value, size_t size); 29 | 30 | /* size is given in bytes and must be multiple of 4 */ 31 | void android_memset32(uint32_t* dst, uint32_t value, size_t size); 32 | 33 | #if !HAVE_STRLCPY 34 | /* Declaration of strlcpy() for platforms that don't already have it. */ 35 | size_t strlcpy(char *dst, const char *src, size_t size); 36 | #endif 37 | 38 | #ifdef __cplusplus 39 | } // extern "C" 40 | #endif 41 | 42 | #endif // ANDROID_CUTILS_MEMORY_H 43 | -------------------------------------------------------------------------------- /include/cutils/misc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2006 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 __CUTILS_MISC_H 18 | #define __CUTILS_MISC_H 19 | 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | /* Load an entire file into a malloc'd chunk of memory 25 | * that is length_of_file + 1 (null terminator). If 26 | * sz is non-zero, return the size of the file via sz. 27 | * Returns 0 on failure. 28 | */ 29 | extern void *load_file(const char *fn, unsigned *sz); 30 | 31 | /* Connects your process to the system debugger daemon 32 | * so that on a crash it may be logged or interactively 33 | * debugged (depending on system settings). 34 | */ 35 | extern void debuggerd_connect(void); 36 | 37 | 38 | /* This is the range of UIDs (and GIDs) that are reserved 39 | * for assigning to applications. 40 | */ 41 | #define FIRST_APPLICATION_UID 10000 42 | #define LAST_APPLICATION_UID 99999 43 | 44 | #ifdef __cplusplus 45 | } 46 | #endif 47 | 48 | #endif /* __CUTILS_MISC_H */ 49 | -------------------------------------------------------------------------------- /include/cutils/mq.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 | /** 18 | * IPC messaging library. 19 | */ 20 | 21 | #ifndef __MQ_H 22 | #define __MQ_H 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | /** A message. */ 29 | typedef struct MqMessage MqMessage; 30 | 31 | /** A destination to which messages can be sent. */ 32 | typedef struct MqDestination MqDestination; 33 | 34 | /* Array of bytes. */ 35 | typedef struct MqBytes MqBytes; 36 | 37 | /** 38 | * Hears messages. 39 | * 40 | * @param destination to which the message was sent 41 | * @param message the message to hear 42 | */ 43 | typedef void MqMessageListener(MqDestination* destination, MqMessage* message); 44 | 45 | /** 46 | * Hears a destination close. 47 | * 48 | * @param destination that closed 49 | */ 50 | typedef void MqCloseListener(MqDestination* destination); 51 | 52 | /** Message functions. */ 53 | 54 | /** 55 | * Creates a new Message. 56 | * 57 | * @param header as defined by user 58 | * @param body as defined by user 59 | * @param replyTo destination to which replies should be sent, NULL if none 60 | */ 61 | MqMessage* mqCreateMessage(MqBytes header, MqBytes body, 62 | MqDestination* replyTo); 63 | 64 | /** Sends a message to a destination. */ 65 | void mqSendMessage(MqMessage* message, MqDestination* destination); 66 | 67 | /** Destination functions. */ 68 | 69 | /** 70 | * Creates a new destination. Acquires a reference implicitly. 71 | * 72 | * @param messageListener function to call when a message is recieved 73 | * @param closeListener function to call when the destination closes 74 | * @param userData user-specific data to associate with the destination. 75 | * Retrieve using mqGetDestinationUserData(). 76 | */ 77 | MqDestination* mqCreateDestination(MqMessageListener* messageListener, 78 | MqCloseListener* closeListener, void* userData); 79 | 80 | /** 81 | * Gets user data which was associated with the given destination at 82 | * construction time. 83 | * 84 | * It is only valid to call this function in the same process that the 85 | * given destination was created in. 86 | * This function returns a null pointer if you call it on a destination 87 | * created in a remote process. 88 | */ 89 | void* mqGetUserData(MqDestination* destination); 90 | 91 | /** 92 | * Returns 1 if the destination was created in this process, or 0 if 93 | * the destination was created in a different process, in which case you have 94 | * a remote stub. 95 | */ 96 | int mqIsDestinationLocal(MqDestination* destination); 97 | 98 | /** 99 | * Increments the destination's reference count. 100 | */ 101 | void mqKeepDestination(MqDesintation* destination); 102 | 103 | /** 104 | * Decrements the destination's reference count. 105 | */ 106 | void mqFreeDestination(MqDestination* desintation); 107 | 108 | /** Registry API. */ 109 | 110 | /** 111 | * Gets the destination bound to a name. 112 | */ 113 | MqDestination* mqGetDestination(char* name); 114 | 115 | /** 116 | * Binds a destination to a name. 117 | */ 118 | void mqPutDestination(char* name, MqDestination* desintation); 119 | 120 | #ifdef __cplusplus 121 | } 122 | #endif 123 | 124 | #endif /* __MQ_H */ 125 | -------------------------------------------------------------------------------- /include/cutils/multiuser.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 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 __CUTILS_MULTIUSER_H 18 | #define __CUTILS_MULTIUSER_H 19 | 20 | #include 21 | 22 | #ifdef __cplusplus 23 | extern "C" { 24 | #endif 25 | 26 | // NOTE: keep in sync with android.os.UserId 27 | 28 | #define MULTIUSER_APP_PER_USER_RANGE 100000 29 | 30 | typedef uid_t userid_t; 31 | typedef uid_t appid_t; 32 | 33 | extern userid_t multiuser_get_user_id(uid_t uid); 34 | extern appid_t multiuser_get_app_id(uid_t uid); 35 | extern uid_t multiuser_get_uid(userid_t userId, appid_t appId); 36 | 37 | #ifdef __cplusplus 38 | } 39 | #endif 40 | 41 | #endif /* __CUTILS_MULTIUSER_H */ 42 | -------------------------------------------------------------------------------- /include/cutils/native_handle.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 NATIVE_HANDLE_H_ 18 | #define NATIVE_HANDLE_H_ 19 | 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | typedef struct native_handle 25 | { 26 | int version; /* sizeof(native_handle_t) */ 27 | int numFds; /* number of file-descriptors at &data[0] */ 28 | int numInts; /* number of ints at &data[numFds] */ 29 | int data[0]; /* numFds + numInts ints */ 30 | } native_handle_t; 31 | 32 | /* 33 | * native_handle_close 34 | * 35 | * closes the file descriptors contained in this native_handle_t 36 | * 37 | * return 0 on success, or a negative error code on failure 38 | * 39 | */ 40 | int native_handle_close(const native_handle_t* h); 41 | 42 | 43 | /* 44 | * native_handle_create 45 | * 46 | * creates a native_handle_t and initializes it. must be destroyed with 47 | * native_handle_delete(). 48 | * 49 | */ 50 | native_handle_t* native_handle_create(int numFds, int numInts); 51 | 52 | /* 53 | * native_handle_delete 54 | * 55 | * frees a native_handle_t allocated with native_handle_create(). 56 | * This ONLY frees the memory allocated for the native_handle_t, but doesn't 57 | * close the file descriptors; which can be achieved with native_handle_close(). 58 | * 59 | * return 0 on success, or a negative error code on failure 60 | * 61 | */ 62 | int native_handle_delete(native_handle_t* h); 63 | 64 | 65 | #ifdef __cplusplus 66 | } 67 | #endif 68 | 69 | #endif /* NATIVE_HANDLE_H_ */ 70 | -------------------------------------------------------------------------------- /include/cutils/open_memstream.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010 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 __CUTILS_OPEN_MEMSTREAM_H__ 18 | #define __CUTILS_OPEN_MEMSTREAM_H__ 19 | 20 | #include 21 | 22 | #ifndef HAVE_OPEN_MEMSTREAM 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | FILE* open_memstream(char** bufp, size_t* sizep); 29 | 30 | #ifdef __cplusplus 31 | } 32 | #endif 33 | 34 | #endif /*!HAVE_OPEN_MEMSTREAM*/ 35 | 36 | #endif /*__CUTILS_OPEN_MEMSTREAM_H__*/ 37 | -------------------------------------------------------------------------------- /include/cutils/partition_utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011, 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 __CUTILS_PARTITION_WIPED_H__ 18 | #define __CUTILS_PARTITION_WIPED_H__ 19 | 20 | __BEGIN_DECLS 21 | 22 | int partition_wiped(char *source); 23 | void erase_footer(const char *dev_path, long long size); 24 | 25 | __END_DECLS 26 | 27 | #endif /* __CUTILS_PARTITION_WIPED_H__ */ 28 | -------------------------------------------------------------------------------- /include/cutils/process_name.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 | /** 18 | * Gives the current process a name. 19 | */ 20 | 21 | #ifndef __PROCESS_NAME_H 22 | #define __PROCESS_NAME_H 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | /** 29 | * Sets the current process name. 30 | * 31 | * Warning: This leaks a string every time you call it. Use judiciously! 32 | */ 33 | void set_process_name(const char* process_name); 34 | 35 | /** Gets the current process name. */ 36 | const char* get_process_name(void); 37 | 38 | #ifdef __cplusplus 39 | } 40 | #endif 41 | 42 | #endif /* __PROCESS_NAME_H */ 43 | -------------------------------------------------------------------------------- /include/cutils/properties.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2006 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 __CUTILS_PROPERTIES_H 18 | #define __CUTILS_PROPERTIES_H 19 | 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | /* System properties are *small* name value pairs managed by the 25 | ** property service. If your data doesn't fit in the provided 26 | ** space it is not appropriate for a system property. 27 | ** 28 | ** WARNING: system/bionic/include/sys/system_properties.h also defines 29 | ** these, but with different names. (TODO: fix that) 30 | */ 31 | #define PROPERTY_KEY_MAX 32 32 | #define PROPERTY_VALUE_MAX 92 33 | 34 | /* property_get: returns the length of the value which will never be 35 | ** greater than PROPERTY_VALUE_MAX - 1 and will always be zero terminated. 36 | ** (the length does not include the terminating zero). 37 | ** 38 | ** If the property read fails or returns an empty value, the default 39 | ** value is used (if nonnull). 40 | */ 41 | int property_get(const char *key, char *value, const char *default_value); 42 | 43 | /* property_set: returns 0 on success, < 0 on failure 44 | */ 45 | int property_set(const char *key, const char *value); 46 | 47 | int property_list(void (*propfn)(const char *key, const char *value, void *cookie), void *cookie); 48 | 49 | 50 | #ifdef HAVE_SYSTEM_PROPERTY_SERVER 51 | /* 52 | * We have an external property server instead of built-in libc support. 53 | * Used by the simulator. 54 | */ 55 | #define SYSTEM_PROPERTY_PIPE_NAME "/tmp/android-sysprop" 56 | 57 | enum { 58 | kSystemPropertyUnknown = 0, 59 | kSystemPropertyGet, 60 | kSystemPropertySet, 61 | kSystemPropertyList 62 | }; 63 | #endif /*HAVE_SYSTEM_PROPERTY_SERVER*/ 64 | 65 | 66 | #ifdef __cplusplus 67 | } 68 | #endif 69 | 70 | #endif 71 | -------------------------------------------------------------------------------- /include/cutils/qsort_r_compat.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 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 | /* 18 | * Provides a portable version of qsort_r, called qsort_r_compat, which is a 19 | * reentrant variant of qsort that passes a user data pointer to its comparator. 20 | * This implementation follows the BSD parameter convention. 21 | */ 22 | 23 | #ifndef _LIBS_CUTILS_QSORT_R_COMPAT_H 24 | #define _LIBS_CUTILS_QSORT_R_COMPAT_H 25 | 26 | #include 27 | 28 | #ifdef __cplusplus 29 | extern "C" { 30 | #endif 31 | 32 | void qsort_r_compat(void* base, size_t nel, size_t width, void* thunk, 33 | int (*compar)(void*, const void* , const void* )); 34 | 35 | #ifdef __cplusplus 36 | } 37 | #endif 38 | 39 | #endif // _LIBS_CUTILS_QSORT_R_COMPAT_H 40 | -------------------------------------------------------------------------------- /include/cutils/qtaguid.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 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 __CUTILS_QTAGUID_H 18 | #define __CUTILS_QTAGUID_H 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | /* 29 | * Set tags (and owning UIDs) for network sockets. 30 | */ 31 | extern int qtaguid_tagSocket(int sockfd, int tag, uid_t uid); 32 | 33 | /* 34 | * Untag a network socket before closing. 35 | */ 36 | extern int qtaguid_untagSocket(int sockfd); 37 | 38 | /* 39 | * For the given uid, switch counter sets. 40 | * The kernel only keeps a limited number of sets. 41 | * 2 for now. 42 | */ 43 | extern int qtaguid_setCounterSet(int counterSetNum, uid_t uid); 44 | 45 | /* 46 | * Delete all tag info that relates to the given tag an uid. 47 | * If the tag is 0, then ALL info about the uid is freeded. 48 | * The delete data also affects active tagged socketd, which are 49 | * then untagged. 50 | * The calling process can only operate on its own tags. 51 | * Unless it is part of the happy AID_NET_BW_ACCT group. 52 | * In which case it can clobber everything. 53 | */ 54 | extern int qtaguid_deleteTagData(int tag, uid_t uid); 55 | 56 | /* 57 | * Enable/disable qtaguid functionnality at a lower level. 58 | * When pacified, the kernel will accept commands but do nothing. 59 | */ 60 | extern int qtaguid_setPacifier(int on); 61 | 62 | #ifdef __cplusplus 63 | } 64 | #endif 65 | 66 | #endif /* __CUTILS_QTAG_UID_H */ 67 | -------------------------------------------------------------------------------- /include/cutils/record_stream.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2006 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 | /* 18 | * A simple utility for reading fixed records out of a stream fd 19 | */ 20 | 21 | #ifndef _CUTILS_RECORD_STREAM_H 22 | #define _CUTILS_RECORD_STREAM_H 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | 29 | typedef struct RecordStream RecordStream; 30 | 31 | extern RecordStream *record_stream_new(int fd, size_t maxRecordLen); 32 | extern void record_stream_free(RecordStream *p_rs); 33 | 34 | extern int record_stream_get_next (RecordStream *p_rs, void ** p_outRecord, 35 | size_t *p_outRecordLen); 36 | 37 | #ifdef __cplusplus 38 | } 39 | #endif 40 | 41 | 42 | #endif /*_CUTILS_RECORD_STREAM_H*/ 43 | 44 | -------------------------------------------------------------------------------- /include/cutils/sched_policy.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 __CUTILS_SCHED_POLICY_H 18 | #define __CUTILS_SCHED_POLICY_H 19 | 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | /* Keep in sync with THREAD_GROUP_* in frameworks/base/core/java/android/os/Process.java */ 25 | typedef enum { 26 | SP_DEFAULT = -1, 27 | SP_BACKGROUND = 0, 28 | SP_FOREGROUND = 1, 29 | SP_SYSTEM = 2, // can't be used with set_sched_policy() 30 | SP_AUDIO_APP = 3, 31 | SP_AUDIO_SYS = 4, 32 | SP_CNT, 33 | SP_MAX = SP_CNT - 1, 34 | SP_SYSTEM_DEFAULT = SP_FOREGROUND, 35 | } SchedPolicy; 36 | 37 | /* Assign thread tid to the cgroup associated with the specified policy. 38 | * If the thread is a thread group leader, that is it's gettid() == getpid(), 39 | * then the other threads in the same thread group are _not_ affected. 40 | * On platforms which support gettid(), zero tid means current thread. 41 | * Return value: 0 for success, or -errno for error. 42 | */ 43 | extern int set_sched_policy(int tid, SchedPolicy policy); 44 | 45 | /* Return the policy associated with the cgroup of thread tid via policy pointer. 46 | * On platforms which support gettid(), zero tid means current thread. 47 | * Return value: 0 for success, or -1 for error and set errno. 48 | */ 49 | extern int get_sched_policy(int tid, SchedPolicy *policy); 50 | 51 | /* Return a displayable string corresponding to policy. 52 | * Return value: non-NULL NUL-terminated name of unspecified length; 53 | * the caller is responsible for displaying the useful part of the string. 54 | */ 55 | extern const char *get_sched_policy_name(SchedPolicy policy); 56 | 57 | #ifdef __cplusplus 58 | } 59 | #endif 60 | 61 | #endif /* __CUTILS_SCHED_POLICY_H */ 62 | -------------------------------------------------------------------------------- /include/cutils/selector.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 | /** 18 | * Framework for multiplexing I/O. A selector manages a set of file 19 | * descriptors and calls out to user-provided callback functions to read and 20 | * write data and handle errors. 21 | */ 22 | 23 | #ifndef __SELECTOR_H 24 | #define __SELECTOR_H 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | #include 31 | 32 | /** 33 | * Manages SelectableFds and invokes their callbacks at appropriate times. 34 | */ 35 | typedef struct Selector Selector; 36 | 37 | /** 38 | * A selectable descriptor. Contains callbacks which the selector can invoke 39 | * before calling select(), when the descriptor is readable or writable, and 40 | * when the descriptor contains out-of-band data. Simply set a callback to 41 | * NULL if you're not interested in that particular event. 42 | * 43 | * A selectable descriptor can indicate that it needs to be removed from the 44 | * selector by setting the 'remove' flag. The selector will remove the 45 | * descriptor at a later time and invoke the onRemove() callback. 46 | * 47 | * SelectableFd fields should only be modified from the selector loop. 48 | */ 49 | typedef struct SelectableFd SelectableFd; 50 | struct SelectableFd { 51 | 52 | /** The file descriptor itself. */ 53 | int fd; 54 | 55 | /** Pointer to user-specific data. Can be NULL. */ 56 | void* data; 57 | 58 | /** 59 | * Set this flag when you no longer wish to be selected. The selector 60 | * will invoke onRemove() when the descriptor is actually removed. 61 | */ 62 | bool remove; 63 | 64 | /** 65 | * Invoked by the selector before calling select. You can set up other 66 | * callbacks from here as necessary. 67 | */ 68 | void (*beforeSelect)(SelectableFd* self); 69 | 70 | /** 71 | * Invoked by the selector when the descriptor has data available. Set to 72 | * NULL to indicate that you're not interested in reading. 73 | */ 74 | void (*onReadable)(SelectableFd* self); 75 | 76 | /** 77 | * Invoked by the selector when the descriptor can accept data. Set to 78 | * NULL to indicate that you're not interested in writing. 79 | */ 80 | void (*onWritable)(SelectableFd* self); 81 | 82 | /** 83 | * Invoked by the selector when out-of-band (OOB) data is available. Set to 84 | * NULL to indicate that you're not interested in OOB data. 85 | */ 86 | void (*onExcept)(SelectableFd* self); 87 | 88 | /** 89 | * Invoked by the selector after the descriptor is removed from the 90 | * selector but before the selector frees the SelectableFd memory. 91 | */ 92 | void (*onRemove)(SelectableFd* self); 93 | 94 | /** 95 | * The selector which selected this fd. Set by the selector itself. 96 | */ 97 | Selector* selector; 98 | }; 99 | 100 | /** 101 | * Creates a new selector. 102 | */ 103 | Selector* selectorCreate(void); 104 | 105 | /** 106 | * Creates a new selectable fd, adds it to the given selector and returns a 107 | * pointer. Outside of 'selector' and 'fd', all fields are set to 0 or NULL 108 | * by default. 109 | * 110 | * The selectable fd should only be modified from the selector loop thread. 111 | */ 112 | SelectableFd* selectorAdd(Selector* selector, int fd); 113 | 114 | /** 115 | * Wakes up the selector even though no I/O events occurred. Use this 116 | * to indicate that you're ready to write to a descriptor. 117 | */ 118 | void selectorWakeUp(Selector* selector); 119 | 120 | /** 121 | * Loops continuously selecting file descriptors and firing events. 122 | * Does not return. 123 | */ 124 | void selectorLoop(Selector* selector); 125 | 126 | #ifdef __cplusplus 127 | } 128 | #endif 129 | 130 | #endif /* __SELECTOR_H */ 131 | -------------------------------------------------------------------------------- /include/cutils/sockets.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2006 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 __CUTILS_SOCKETS_H 18 | #define __CUTILS_SOCKETS_H 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #ifdef HAVE_WINSOCK 26 | #include 27 | typedef int socklen_t; 28 | #elif HAVE_SYS_SOCKET_H 29 | #include 30 | #endif 31 | 32 | #define ANDROID_SOCKET_ENV_PREFIX "ANDROID_SOCKET_" 33 | #define ANDROID_SOCKET_DIR "/dev/socket" 34 | 35 | #ifdef __cplusplus 36 | extern "C" { 37 | #endif 38 | 39 | /* 40 | * android_get_control_socket - simple helper function to get the file 41 | * descriptor of our init-managed Unix domain socket. `name' is the name of the 42 | * socket, as given in init.rc. Returns -1 on error. 43 | * 44 | * This is inline and not in libcutils proper because we want to use this in 45 | * third-party daemons with minimal modification. 46 | */ 47 | static inline int android_get_control_socket(const char *name) 48 | { 49 | char key[64] = ANDROID_SOCKET_ENV_PREFIX; 50 | const char *val; 51 | int fd; 52 | 53 | /* build our environment variable, counting cycles like a wolf ... */ 54 | #if HAVE_STRLCPY 55 | strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1, 56 | name, 57 | sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX)); 58 | #else /* for the host, which may lack the almightly strncpy ... */ 59 | strncpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1, 60 | name, 61 | sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX)); 62 | key[sizeof(key)-1] = '\0'; 63 | #endif 64 | 65 | val = getenv(key); 66 | if (!val) 67 | return -1; 68 | 69 | errno = 0; 70 | fd = strtol(val, NULL, 10); 71 | if (errno) 72 | return -1; 73 | 74 | return fd; 75 | } 76 | 77 | /* 78 | * See also android.os.LocalSocketAddress.Namespace 79 | */ 80 | // Linux "abstract" (non-filesystem) namespace 81 | #define ANDROID_SOCKET_NAMESPACE_ABSTRACT 0 82 | // Android "reserved" (/dev/socket) namespace 83 | #define ANDROID_SOCKET_NAMESPACE_RESERVED 1 84 | // Normal filesystem namespace 85 | #define ANDROID_SOCKET_NAMESPACE_FILESYSTEM 2 86 | 87 | extern int socket_loopback_client(int port, int type); 88 | extern int socket_network_client(const char *host, int port, int type); 89 | extern int socket_loopback_server(int port, int type); 90 | extern int socket_local_server(const char *name, int namespaceId, int type); 91 | extern int socket_local_server_bind(int s, const char *name, int namespaceId); 92 | extern int socket_local_client_connect(int fd, 93 | const char *name, int namespaceId, int type); 94 | extern int socket_local_client(const char *name, int namespaceId, int type); 95 | extern int socket_inaddr_any_server(int port, int type); 96 | 97 | /* 98 | * socket_peer_is_trusted - Takes a socket which is presumed to be a 99 | * connected local socket (e.g. AF_LOCAL) and returns whether the peer 100 | * (the userid that owns the process on the other end of that socket) 101 | * is one of the two trusted userids, root or shell. 102 | * 103 | * Note: This only works as advertised on the Android OS and always 104 | * just returns true when called on other operating systems. 105 | */ 106 | extern bool socket_peer_is_trusted(int fd); 107 | 108 | #ifdef __cplusplus 109 | } 110 | #endif 111 | 112 | #endif /* __CUTILS_SOCKETS_H */ 113 | -------------------------------------------------------------------------------- /include/cutils/str_parms.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 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 __CUTILS_STR_PARMS_H 18 | #define __CUTILS_STR_PARMS_H 19 | 20 | #include 21 | 22 | struct str_parms; 23 | 24 | struct str_parms *str_parms_create(void); 25 | struct str_parms *str_parms_create_str(const char *_string); 26 | void str_parms_destroy(struct str_parms *str_parms); 27 | 28 | void str_parms_del(struct str_parms *str_parms, const char *key); 29 | 30 | int str_parms_add_str(struct str_parms *str_parms, const char *key, 31 | const char *value); 32 | int str_parms_add_int(struct str_parms *str_parms, const char *key, int value); 33 | 34 | int str_parms_add_float(struct str_parms *str_parms, const char *key, 35 | float value); 36 | 37 | int str_parms_get_str(struct str_parms *str_parms, const char *key, 38 | char *out_val, int len); 39 | int str_parms_get_int(struct str_parms *str_parms, const char *key, 40 | int *out_val); 41 | int str_parms_get_float(struct str_parms *str_parms, const char *key, 42 | float *out_val); 43 | 44 | char *str_parms_to_str(struct str_parms *str_parms); 45 | 46 | /* debug */ 47 | void str_parms_dump(struct str_parms *str_parms); 48 | 49 | #endif /* __CUTILS_STR_PARMS_H */ 50 | -------------------------------------------------------------------------------- /include/cutils/threads.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 _LIBS_CUTILS_THREADS_H 18 | #define _LIBS_CUTILS_THREADS_H 19 | 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | /***********************************************************************/ 25 | /***********************************************************************/ 26 | /***** *****/ 27 | /***** local thread storage *****/ 28 | /***** *****/ 29 | /***********************************************************************/ 30 | /***********************************************************************/ 31 | 32 | #ifdef HAVE_PTHREADS 33 | 34 | #include 35 | 36 | typedef struct { 37 | pthread_mutex_t lock; 38 | int has_tls; 39 | pthread_key_t tls; 40 | 41 | } thread_store_t; 42 | 43 | #define THREAD_STORE_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0 } 44 | 45 | #elif defined HAVE_WIN32_THREADS 46 | 47 | #include 48 | 49 | typedef struct { 50 | int lock_init; 51 | int has_tls; 52 | DWORD tls; 53 | CRITICAL_SECTION lock; 54 | 55 | } thread_store_t; 56 | 57 | #define THREAD_STORE_INITIALIZER { 0, 0, 0, {0, 0, 0, 0, 0, 0} } 58 | 59 | #else 60 | # error "no thread_store_t implementation for your platform !!" 61 | #endif 62 | 63 | typedef void (*thread_store_destruct_t)(void* value); 64 | 65 | extern void* thread_store_get(thread_store_t* store); 66 | 67 | extern void thread_store_set(thread_store_t* store, 68 | void* value, 69 | thread_store_destruct_t destroy); 70 | 71 | /***********************************************************************/ 72 | /***********************************************************************/ 73 | /***** *****/ 74 | /***** mutexes *****/ 75 | /***** *****/ 76 | /***********************************************************************/ 77 | /***********************************************************************/ 78 | 79 | #ifdef HAVE_PTHREADS 80 | 81 | typedef pthread_mutex_t mutex_t; 82 | 83 | #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER 84 | 85 | static __inline__ void mutex_lock(mutex_t* lock) 86 | { 87 | pthread_mutex_lock(lock); 88 | } 89 | static __inline__ void mutex_unlock(mutex_t* lock) 90 | { 91 | pthread_mutex_unlock(lock); 92 | } 93 | static __inline__ int mutex_init(mutex_t* lock) 94 | { 95 | return pthread_mutex_init(lock, NULL); 96 | } 97 | static __inline__ void mutex_destroy(mutex_t* lock) 98 | { 99 | pthread_mutex_destroy(lock); 100 | } 101 | #endif 102 | 103 | #ifdef HAVE_WIN32_THREADS 104 | typedef struct { 105 | int init; 106 | CRITICAL_SECTION lock[1]; 107 | } mutex_t; 108 | 109 | #define MUTEX_INITIALIZER { 0, {{ NULL, 0, 0, NULL, NULL, 0 }} } 110 | 111 | static __inline__ void mutex_lock(mutex_t* lock) 112 | { 113 | if (!lock->init) { 114 | lock->init = 1; 115 | InitializeCriticalSection( lock->lock ); 116 | lock->init = 2; 117 | } else while (lock->init != 2) 118 | Sleep(10); 119 | 120 | EnterCriticalSection(lock->lock); 121 | } 122 | 123 | static __inline__ void mutex_unlock(mutex_t* lock) 124 | { 125 | LeaveCriticalSection(lock->lock); 126 | } 127 | static __inline__ int mutex_init(mutex_t* lock) 128 | { 129 | InitializeCriticalSection(lock->lock); 130 | lock->init = 2; 131 | return 0; 132 | } 133 | static __inline__ void mutex_destroy(mutex_t* lock) 134 | { 135 | if (lock->init) { 136 | lock->init = 0; 137 | DeleteCriticalSection(lock->lock); 138 | } 139 | } 140 | #endif 141 | 142 | #ifdef __cplusplus 143 | } 144 | #endif 145 | 146 | #endif /* _LIBS_CUTILS_THREADS_H */ 147 | -------------------------------------------------------------------------------- /include/cutils/tztime.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2006 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 _CUTILS_TZTIME_H 18 | #define _CUTILS_TZTIME_H 19 | 20 | #include 21 | 22 | #ifdef __cplusplus 23 | extern "C" { 24 | #endif 25 | 26 | time_t mktime_tz(struct tm * const tmp, char const * tz); 27 | void localtime_tz(const time_t * const timep, struct tm * tmp, const char* tz); 28 | 29 | #ifdef HAVE_ANDROID_OS 30 | 31 | /* the following is defined in the Bionic C library on Android, but the 32 | * declarations are only available through a platform-private header 33 | */ 34 | #include 35 | 36 | #else /* !HAVE_ANDROID_OS */ 37 | 38 | struct strftime_locale { 39 | const char *mon[12]; /* short names */ 40 | const char *month[12]; /* long names */ 41 | const char *standalone_month[12]; /* long standalone names */ 42 | const char *wday[7]; /* short names */ 43 | const char *weekday[7]; /* long names */ 44 | const char *X_fmt; 45 | const char *x_fmt; 46 | const char *c_fmt; 47 | const char *am; 48 | const char *pm; 49 | const char *date_fmt; 50 | }; 51 | 52 | size_t strftime_tz(char *s, size_t max, const char *format, const struct tm *tm, const struct strftime_locale *locale); 53 | 54 | #endif /* !HAVE_ANDROID_OS */ 55 | 56 | #ifdef __cplusplus 57 | } 58 | #endif 59 | 60 | #endif /* __CUTILS_TZTIME_H */ 61 | 62 | -------------------------------------------------------------------------------- /include/cutils/uevent.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 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 __CUTILS_UEVENT_H 18 | #define __CUTILS_UEVENT_H 19 | 20 | #include 21 | #include 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | int uevent_open_socket(int buf_sz, bool passcred); 28 | ssize_t uevent_kernel_multicast_recv(int socket, void *buffer, size_t length); 29 | ssize_t uevent_kernel_multicast_uid_recv(int socket, void *buffer, size_t length, uid_t *uid); 30 | 31 | #ifdef __cplusplus 32 | } 33 | #endif 34 | 35 | #endif /* __CUTILS_UEVENT_H */ 36 | -------------------------------------------------------------------------------- /include/cutils/uio.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 | // 18 | // implementation of sys/uio.h for platforms that don't have it (Win32) 19 | // 20 | #ifndef _LIBS_CUTILS_UIO_H 21 | #define _LIBS_CUTILS_UIO_H 22 | 23 | #ifdef HAVE_SYS_UIO_H 24 | #include 25 | #else 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | #include 32 | 33 | struct iovec { 34 | const void* iov_base; 35 | size_t iov_len; 36 | }; 37 | 38 | extern int readv( int fd, struct iovec* vecs, int count ); 39 | extern int writev( int fd, const struct iovec* vecs, int count ); 40 | 41 | #ifdef __cplusplus 42 | } 43 | #endif 44 | 45 | #endif /* !HAVE_SYS_UIO_H */ 46 | 47 | #endif /* _LIBS_UTILS_UIO_H */ 48 | 49 | -------------------------------------------------------------------------------- /include/cutils/zygote.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 __CUTILS_ZYGOTE_H 18 | #define __CUTILS_ZYGOTE_H 19 | 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | int zygote_run_oneshot(int sendStdio, int argc, const char **argv); 25 | int zygote_run(int argc, const char **argv); 26 | int zygote_run_wait(int argc, const char **argv, void (*post_run_func)(int)); 27 | 28 | #ifdef __cplusplus 29 | } 30 | #endif 31 | 32 | #endif /* __CUTILS_ZYGOTE_H */ 33 | -------------------------------------------------------------------------------- /include/libadb/libadb.h: -------------------------------------------------------------------------------- 1 | #ifndef _LIBADB_H_ 2 | #define _LIBADB_H_ 3 | 4 | int libadb_push_file(const char *lpath, const char *rpath, int verify); 5 | int libadb_pull_file(const char *rpath, const char *lpath); 6 | 7 | #endif 8 | 9 | -------------------------------------------------------------------------------- /include/mincrypt/rsa.h: -------------------------------------------------------------------------------- 1 | /* rsa.h 2 | ** 3 | ** Copyright 2008, The Android Open Source Project 4 | ** 5 | ** Redistribution and use in source and binary forms, with or without 6 | ** modification, are permitted provided that the following conditions are met: 7 | ** * Redistributions of source code must retain the above copyright 8 | ** notice, this list of conditions and the following disclaimer. 9 | ** * Redistributions in binary form must reproduce the above copyright 10 | ** notice, this list of conditions and the following disclaimer in the 11 | ** documentation and/or other materials provided with the distribution. 12 | ** * Neither the name of Google Inc. nor the names of its contributors may 13 | ** be used to endorse or promote products derived from this software 14 | ** without specific prior written permission. 15 | ** 16 | ** THIS SOFTWARE IS PROVIDED BY Google Inc. ``AS IS'' AND ANY EXPRESS OR 17 | ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 | ** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19 | ** EVENT SHALL Google Inc. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 | ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 | ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 | ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 | ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #ifndef _EMBEDDED_RSA_H_ 29 | #define _EMBEDDED_RSA_H_ 30 | 31 | #include 32 | 33 | #ifdef __cplusplus 34 | extern "C" { 35 | #endif 36 | 37 | #define RSANUMBYTES 256 /* 2048 bit key length */ 38 | #define RSANUMWORDS (RSANUMBYTES / sizeof(uint32_t)) 39 | 40 | typedef struct RSAPublicKey { 41 | int len; /* Length of n[] in number of uint32_t */ 42 | uint32_t n0inv; /* -1 / n[0] mod 2^32 */ 43 | uint32_t n[RSANUMWORDS]; /* modulus as little endian array */ 44 | uint32_t rr[RSANUMWORDS]; /* R^2 as little endian array */ 45 | int exponent; /* 3 or 65537 */ 46 | } RSAPublicKey; 47 | 48 | int RSA_verify(const RSAPublicKey *key, 49 | const uint8_t* signature, 50 | const int len, 51 | const uint8_t* sha); 52 | 53 | #ifdef __cplusplus 54 | } 55 | #endif 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /include/mincrypt/sha.h: -------------------------------------------------------------------------------- 1 | /* sha.h 2 | ** 3 | ** Copyright 2008, The Android Open Source Project 4 | ** 5 | ** Redistribution and use in source and binary forms, with or without 6 | ** modification, are permitted provided that the following conditions are met: 7 | ** * Redistributions of source code must retain the above copyright 8 | ** notice, this list of conditions and the following disclaimer. 9 | ** * Redistributions in binary form must reproduce the above copyright 10 | ** notice, this list of conditions and the following disclaimer in the 11 | ** documentation and/or other materials provided with the distribution. 12 | ** * Neither the name of Google Inc. nor the names of its contributors may 13 | ** be used to endorse or promote products derived from this software 14 | ** without specific prior written permission. 15 | ** 16 | ** THIS SOFTWARE IS PROVIDED BY Google Inc. ``AS IS'' AND ANY EXPRESS OR 17 | ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 | ** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19 | ** EVENT SHALL Google Inc. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 | ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 | ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 | ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 | ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #ifndef _EMBEDDED_SHA_H_ 29 | #define _EMBEDDED_SHA_H_ 30 | 31 | #include 32 | 33 | #ifdef __cplusplus 34 | extern "C" { 35 | #endif 36 | 37 | typedef struct SHA_CTX { 38 | uint64_t count; 39 | uint32_t state[5]; 40 | #if defined(HAVE_ENDIAN_H) && defined(HAVE_LITTLE_ENDIAN) 41 | union { 42 | uint8_t b[64]; 43 | uint32_t w[16]; 44 | } buf; 45 | #else 46 | uint8_t buf[64]; 47 | #endif 48 | } SHA_CTX; 49 | 50 | void SHA_init(SHA_CTX* ctx); 51 | void SHA_update(SHA_CTX* ctx, const void* data, int len); 52 | const uint8_t* SHA_final(SHA_CTX* ctx); 53 | 54 | /* Convenience method. Returns digest parameter value. */ 55 | const uint8_t* SHA(const void* data, int len, uint8_t* digest); 56 | 57 | #define SHA_DIGEST_SIZE 20 58 | 59 | #ifdef __cplusplus 60 | } 61 | #endif 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /include/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/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | make -C adb 3 | make -C libadb 4 | make -C libcutils 5 | make -C libzipfile 6 | make -C zlib 7 | 8 | clean: 9 | make -C adb clean 10 | make -C libadb clean 11 | make -C libcutils clean 12 | make -C libzipfile clean 13 | make -C zlib clean 14 | -------------------------------------------------------------------------------- /src/adb/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS = -g -w -fPIC -I. -I../../include/ -DADB_HOST=1 -DHAVE_FORKEXEC=1 -DHAVE_SYMLINKS -DHAVE_TERMIO_H 3 | 4 | SRCS= adb.c 5 | SRCS+= adb_auth_host.c 6 | SRCS+= adb_client.c 7 | SRCS+= adbexec.c 8 | SRCS+= commandline.c 9 | SRCS+= console.c 10 | SRCS+= file_sync_client.c 11 | SRCS+= fdevent.c 12 | SRCS+= get_my_path_linux.c 13 | SRCS+= services.c 14 | SRCS+= sockets.c 15 | SRCS+= transport.c 16 | SRCS+= transport_local.c 17 | SRCS+= transport_usb.c 18 | SRCS+= usb_linux.c 19 | SRCS+= usb_vendors.c 20 | SRCS+= utils.c 21 | 22 | OBJS = $(SRCS:.c=.o) 23 | 24 | all: $(OBJS) 25 | 26 | clean: 27 | rm -rf $(OBJS) 28 | 29 | -------------------------------------------------------------------------------- /src/adb/adb_auth.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 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_AUTH_H 18 | #define __ADB_AUTH_H 19 | 20 | void adb_auth_init(void); 21 | void adb_auth_verified(atransport *t); 22 | 23 | /* AUTH packets first argument */ 24 | /* Request */ 25 | #define ADB_AUTH_TOKEN 1 26 | /* Response */ 27 | #define ADB_AUTH_SIGNATURE 2 28 | #define ADB_AUTH_RSAPUBLICKEY 3 29 | 30 | #if ADB_HOST 31 | 32 | int adb_auth_sign(void *key, void *token, size_t token_size, void *sig); 33 | void *adb_auth_nextkey(void *current); 34 | int adb_auth_get_userkey(unsigned char *data, size_t len); 35 | 36 | static inline int adb_auth_generate_token(void *token, size_t token_size) { return 0; } 37 | static inline int adb_auth_verify(void *token, void *sig, int siglen) { return 0; } 38 | static inline void adb_auth_confirm_key(unsigned char *data, size_t len, atransport *t) { } 39 | static inline void adb_auth_reload_keys(void) { } 40 | 41 | #else // !ADB_HOST 42 | 43 | static inline int adb_auth_sign(void* key, void *token, size_t token_size, void *sig) { return 0; } 44 | static inline void *adb_auth_nextkey(void *current) { return NULL; } 45 | static inline int adb_auth_get_userkey(unsigned char *data, size_t len) { return 0; } 46 | 47 | int adb_auth_generate_token(void *token, size_t token_size); 48 | int adb_auth_verify(void *token, void *sig, int siglen); 49 | void adb_auth_confirm_key(unsigned char *data, size_t len, atransport *t); 50 | void adb_auth_reload_keys(void); 51 | 52 | #endif // ADB_HOST 53 | 54 | #endif // __ADB_AUTH_H 55 | -------------------------------------------------------------------------------- /src/adb/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/adb/adbexec.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "sysdeps.h" 3 | #include "adb.h" 4 | 5 | #define TRACE_TAG TRACE_ADB 6 | 7 | 8 | int main(int argc, char **argv) { 9 | #if ADB_HOST 10 | adb_sysdeps_init(); 11 | adb_trace_init(); 12 | D("Handling commandline()\n"); 13 | return adb_commandline(argc - 1, argv + 1); 14 | #else 15 | /* If adbd runs inside the emulator this will enable adb tracing via 16 | * adb-debug qemud service in the emulator. */ 17 | adb_qemu_trace_init(); 18 | if((argc > 1) && (!strcmp(argv[1],"recovery"))) { 19 | adb_device_banner = "recovery"; 20 | recovery_mode = 1; 21 | } 22 | 23 | start_device_log(); 24 | D("Handling main()\n"); 25 | return adb_main(0, DEFAULT_ADB_PORT); 26 | #endif 27 | } 28 | -------------------------------------------------------------------------------- /src/adb/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/adb/fdevent.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2006 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 __FDEVENT_H 18 | #define __FDEVENT_H 19 | 20 | #include /* for int64_t */ 21 | 22 | /* events that may be observed */ 23 | #define FDE_READ 0x0001 24 | #define FDE_WRITE 0x0002 25 | #define FDE_ERROR 0x0004 26 | #define FDE_TIMEOUT 0x0008 27 | 28 | /* features that may be set (via the events set/add/del interface) */ 29 | #define FDE_DONT_CLOSE 0x0080 30 | 31 | typedef struct fdevent fdevent; 32 | 33 | typedef void (*fd_func)(int fd, unsigned events, void *userdata); 34 | 35 | /* Allocate and initialize a new fdevent object 36 | * Note: use FD_TIMER as 'fd' to create a fd-less object 37 | * (used to implement timers). 38 | */ 39 | fdevent *fdevent_create(int fd, fd_func func, void *arg); 40 | 41 | /* Uninitialize and deallocate an fdevent object that was 42 | ** created by fdevent_create() 43 | */ 44 | void fdevent_destroy(fdevent *fde); 45 | 46 | /* Initialize an fdevent object that was externally allocated 47 | */ 48 | void fdevent_install(fdevent *fde, int fd, fd_func func, void *arg); 49 | 50 | /* Uninitialize an fdevent object that was initialized by 51 | ** fdevent_install() 52 | */ 53 | void fdevent_remove(fdevent *item); 54 | 55 | /* Change which events should cause notifications 56 | */ 57 | void fdevent_set(fdevent *fde, unsigned events); 58 | void fdevent_add(fdevent *fde, unsigned events); 59 | void fdevent_del(fdevent *fde, unsigned events); 60 | 61 | void fdevent_set_timeout(fdevent *fde, int64_t timeout_ms); 62 | 63 | /* loop forever, handling events. 64 | */ 65 | void fdevent_loop(); 66 | 67 | struct fdevent 68 | { 69 | fdevent *next; 70 | fdevent *prev; 71 | 72 | int fd; 73 | int force_eof; 74 | 75 | unsigned short state; 76 | unsigned short events; 77 | 78 | fd_func func; 79 | void *arg; 80 | }; 81 | 82 | 83 | #endif 84 | -------------------------------------------------------------------------------- /src/adb/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/adb/get_my_path_linux.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 | void get_my_path(char *exe, size_t maxLen) 23 | { 24 | char proc[64]; 25 | snprintf(proc, sizeof proc, "/proc/%d/exe", getpid()); 26 | int err = readlink(proc, exe, maxLen - 1); 27 | if(err > 0) { 28 | exe[err] = '\0'; 29 | } else { 30 | exe[0] = '\0'; 31 | } 32 | } 33 | 34 | -------------------------------------------------------------------------------- /src/adb/mutex_list.h: -------------------------------------------------------------------------------- 1 | /* the list of mutexes used by adb */ 2 | /* #ifndef __MUTEX_LIST_H 3 | * Do not use an include-guard. This file is included once to declare the locks 4 | * and once in win32 to actually do the runtime initialization. 5 | */ 6 | #ifndef ADB_MUTEX 7 | #error ADB_MUTEX not defined when including this file 8 | #endif 9 | ADB_MUTEX(dns_lock) 10 | ADB_MUTEX(socket_list_lock) 11 | ADB_MUTEX(transport_lock) 12 | #if ADB_HOST 13 | ADB_MUTEX(local_transports_lock) 14 | #endif 15 | ADB_MUTEX(usb_lock) 16 | 17 | // Sadly logging to /data/adb/adb-... is not thread safe. 18 | // After modifying adb.h::D() to count invocations: 19 | // DEBUG(jpa):0:Handling main() 20 | // DEBUG(jpa):1:[ usb_init - starting thread ] 21 | // (Oopsies, no :2:, and matching message is also gone.) 22 | // DEBUG(jpa):3:[ usb_thread - opening device ] 23 | // DEBUG(jpa):4:jdwp control socket started (10) 24 | ADB_MUTEX(D_lock) 25 | 26 | #undef ADB_MUTEX 27 | -------------------------------------------------------------------------------- /src/adb/transport.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 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 __TRANSPORT_H 18 | #define __TRANSPORT_H 19 | 20 | /* convenience wrappers around read/write that will retry on 21 | ** EINTR and/or short read/write. Returns 0 on success, -1 22 | ** on error or EOF. 23 | */ 24 | int readx(int fd, void *ptr, size_t len); 25 | int writex(int fd, const void *ptr, size_t len); 26 | #endif /* __TRANSPORT_H */ 27 | -------------------------------------------------------------------------------- /src/adb/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/adb/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/adb/utils.c: -------------------------------------------------------------------------------- 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 | #include "utils.h" 17 | #include 18 | #include 19 | #include 20 | 21 | char* 22 | buff_addc (char* buff, char* buffEnd, int c) 23 | { 24 | int avail = buffEnd - buff; 25 | 26 | if (avail <= 0) /* already in overflow mode */ 27 | return buff; 28 | 29 | if (avail == 1) { /* overflowing, the last byte is reserved for zero */ 30 | buff[0] = 0; 31 | return buff + 1; 32 | } 33 | 34 | buff[0] = (char) c; /* add char and terminating zero */ 35 | buff[1] = 0; 36 | return buff + 1; 37 | } 38 | 39 | char* 40 | buff_adds (char* buff, char* buffEnd, const char* s) 41 | { 42 | int slen = strlen(s); 43 | 44 | return buff_addb(buff, buffEnd, s, slen); 45 | } 46 | 47 | char* 48 | buff_addb (char* buff, char* buffEnd, const void* data, int len) 49 | { 50 | int avail = (buffEnd - buff); 51 | 52 | if (avail <= 0 || len <= 0) /* already overflowing */ 53 | return buff; 54 | 55 | if (len > avail) 56 | len = avail; 57 | 58 | memcpy(buff, data, len); 59 | 60 | buff += len; 61 | 62 | /* ensure there is a terminating zero */ 63 | if (buff >= buffEnd) { /* overflow */ 64 | buff[-1] = 0; 65 | } else 66 | buff[0] = 0; 67 | 68 | return buff; 69 | } 70 | 71 | char* 72 | buff_add (char* buff, char* buffEnd, const char* format, ... ) 73 | { 74 | int avail; 75 | 76 | avail = (buffEnd - buff); 77 | 78 | if (avail > 0) { 79 | va_list args; 80 | int nn; 81 | 82 | va_start(args, format); 83 | nn = vsnprintf( buff, avail, format, args); 84 | va_end(args); 85 | 86 | if (nn < 0) { 87 | /* some C libraries return -1 in case of overflow, 88 | * but they will also do that if the format spec is 89 | * invalid. We assume ADB is not buggy enough to 90 | * trigger that last case. */ 91 | nn = avail; 92 | } 93 | else if (nn > avail) { 94 | nn = avail; 95 | } 96 | 97 | buff += nn; 98 | 99 | /* ensure that there is a terminating zero */ 100 | if (buff >= buffEnd) 101 | buff[-1] = 0; 102 | else 103 | buff[0] = 0; 104 | } 105 | return buff; 106 | } 107 | -------------------------------------------------------------------------------- /src/adb/utils.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 | #ifndef _ADB_UTILS_H 17 | #define _ADB_UTILS_H 18 | 19 | /* bounded buffer functions */ 20 | 21 | /* all these functions are used to append data to a bounded buffer. 22 | * 23 | * after each operation, the buffer is guaranteed to be zero-terminated, 24 | * even in the case of an overflow. they all return the new buffer position 25 | * which allows one to use them in succession, only checking for overflows 26 | * at the end. For example: 27 | * 28 | * BUFF_DECL(temp,p,end,1024); 29 | * char* p; 30 | * 31 | * p = buff_addc(temp, end, '"'); 32 | * p = buff_adds(temp, end, string); 33 | * p = buff_addc(temp, end, '"'); 34 | * 35 | * if (p >= end) { 36 | * overflow detected. note that 'temp' is 37 | * zero-terminated for safety. 38 | * } 39 | * return strdup(temp); 40 | */ 41 | 42 | /* tries to add a character to the buffer, in case of overflow 43 | * this will only write a terminating zero and return buffEnd. 44 | */ 45 | char* buff_addc (char* buff, char* buffEnd, int c); 46 | 47 | /* tries to add a string to the buffer */ 48 | char* buff_adds (char* buff, char* buffEnd, const char* s); 49 | 50 | /* tries to add a bytes to the buffer. the input can contain zero bytes, 51 | * but a terminating zero will always be appended at the end anyway 52 | */ 53 | char* buff_addb (char* buff, char* buffEnd, const void* data, int len); 54 | 55 | /* tries to add a formatted string to a bounded buffer */ 56 | char* buff_add (char* buff, char* buffEnd, const char* format, ... ); 57 | 58 | /* convenience macro used to define a bounded buffer, as well as 59 | * a 'cursor' and 'end' variables all in one go. 60 | * 61 | * note: this doesn't place an initial terminating zero in the buffer, 62 | * you need to use one of the buff_ functions for this. or simply 63 | * do _cursor[0] = 0 manually. 64 | */ 65 | #define BUFF_DECL(_buff,_cursor,_end,_size) \ 66 | char _buff[_size], *_cursor=_buff, *_end = _cursor + (_size) 67 | 68 | #endif /* _ADB_UTILS_H */ 69 | -------------------------------------------------------------------------------- /src/libadb/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-c -g -w -fPIC -I../adb/ -I ../../include/ 3 | 4 | all: 5 | $(CC) $(CFLAGS) -o libadb.o libadb.c 6 | 7 | clean: 8 | rm -rf *.o 9 | -------------------------------------------------------------------------------- /src/libadb/libadb.c: -------------------------------------------------------------------------------- 1 | #include "file_sync_service.h" 2 | #include 3 | 4 | int libadb_push_file(const char *lpath, const char *rpath, int verify) { 5 | return do_sync_push(lpath, rpath, verify); 6 | } 7 | 8 | int libadb_pull_file(const char *rpath, const char *lpath) { 9 | return do_sync_pull(rpath, lpath); 10 | } 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/libcutils/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS = -g -w -fPIC -I. -I../../include/ 3 | 4 | SRCS= abort_socket.c 5 | SRCS+= list.c 6 | SRCS+= load_file.c 7 | SRCS+= socket_inaddr_any_server.c 8 | SRCS+= socket_local_client.c 9 | SRCS+= socket_local_server.c 10 | SRCS+= socket_loopback_client.c 11 | SRCS+= socket_loopback_server.c 12 | SRCS+= socket_network_client.c 13 | 14 | OBJS = $(SRCS:.c=.o) 15 | 16 | all: $(OBJS) 17 | 18 | clean: 19 | rm -rf $(OBJS) 20 | 21 | -------------------------------------------------------------------------------- /src/libcutils/buffer.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 | /** 18 | * Byte buffer utilities. 19 | */ 20 | 21 | #ifndef __BUFFER_H 22 | #define __BUFFER_H 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | #include 29 | 30 | /** 31 | * Byte buffer of known size. Keeps track of how much data has been read 32 | * into or written out of the buffer. 33 | */ 34 | typedef struct { 35 | /** Buffered data. */ 36 | char* data; 37 | 38 | union { 39 | /** For reading. # of bytes we expect. */ 40 | size_t expected; 41 | 42 | /** For writing. # of bytes to write. */ 43 | size_t remaining; 44 | }; 45 | 46 | /** Actual # of bytes in the buffer. */ 47 | size_t size; 48 | 49 | /** Amount of memory allocated for this buffer. */ 50 | size_t capacity; 51 | } Buffer; 52 | 53 | /** 54 | * Returns true if all data has been read into the buffer. 55 | */ 56 | #define bufferReadComplete(buffer) (buffer->expected == buffer->size) 57 | 58 | /** 59 | * Returns true if the buffer has been completely written. 60 | */ 61 | #define bufferWriteComplete(buffer) (buffer->remaining == 0) 62 | 63 | /** 64 | * Creates a new buffer with the given initial capacity. 65 | */ 66 | Buffer* bufferCreate(size_t initialCapacity); 67 | 68 | /** 69 | * Wraps an existing byte array. 70 | */ 71 | Buffer* bufferWrap(char* data, size_t capacity, size_t size); 72 | 73 | /** 74 | * Frees and its data. 75 | */ 76 | void bufferFree(Buffer* buffer); 77 | 78 | /** 79 | * Prepares buffer to read 'expected' number of bytes. Expands capacity if 80 | * necessary. Returns 0 if successful or -1 if an error occurs allocating 81 | * memory. 82 | */ 83 | int bufferPrepareForRead(Buffer* buffer, size_t expected); 84 | 85 | /** 86 | * Reads some data into a buffer. Returns -1 in case of an error and sets 87 | * errno (see read()). Returns 0 for EOF. Updates buffer->size and returns 88 | * the new size after a succesful read. 89 | * 90 | * Precondition: buffer->size < buffer->expected 91 | */ 92 | ssize_t bufferRead(Buffer* buffer, int fd); 93 | 94 | /** 95 | * Prepares a buffer to be written out. 96 | */ 97 | void bufferPrepareForWrite(Buffer* buffer); 98 | 99 | /** 100 | * Writes data from buffer to the given fd. Returns -1 and sets errno in case 101 | * of an error. Updates buffer->remaining and returns the number of remaining 102 | * bytes to be written after a successful write. 103 | * 104 | * Precondition: buffer->remaining > 0 105 | */ 106 | ssize_t bufferWrite(Buffer* buffer, int fd); 107 | 108 | #ifdef __cplusplus 109 | } 110 | #endif 111 | 112 | #endif /* __BUFFER_H */ 113 | -------------------------------------------------------------------------------- /src/libcutils/list.c: -------------------------------------------------------------------------------- 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 | #include 18 | 19 | void list_init(struct listnode *node) 20 | { 21 | node->next = node; 22 | node->prev = node; 23 | } 24 | 25 | void list_add_tail(struct listnode *head, struct listnode *item) 26 | { 27 | item->next = head; 28 | item->prev = head->prev; 29 | head->prev->next = item; 30 | head->prev = item; 31 | } 32 | 33 | void list_remove(struct listnode *item) 34 | { 35 | item->next->prev = item->prev; 36 | item->prev->next = item->next; 37 | } 38 | -------------------------------------------------------------------------------- /src/libcutils/load_file.c: -------------------------------------------------------------------------------- 1 | /* libs/cutils/load_file.c 2 | ** 3 | ** Copyright 2006, The Android Open Source Project 4 | ** 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); 6 | ** you may not use this file except in compliance with the License. 7 | ** You may obtain a copy of the License at 8 | ** 9 | ** http://www.apache.org/licenses/LICENSE-2.0 10 | ** 11 | ** Unless required by applicable law or agreed to in writing, software 12 | ** distributed under the License is distributed on an "AS IS" BASIS, 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | ** See the License for the specific language governing permissions and 15 | ** limitations under the License. 16 | */ 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | void *load_file(const char *fn, unsigned *_sz) 23 | { 24 | char *data; 25 | int sz; 26 | int fd; 27 | 28 | data = 0; 29 | fd = open(fn, O_RDONLY); 30 | if(fd < 0) return 0; 31 | 32 | sz = lseek(fd, 0, SEEK_END); 33 | if(sz < 0) goto oops; 34 | 35 | if(lseek(fd, 0, SEEK_SET) != 0) goto oops; 36 | 37 | data = (char*) malloc(sz + 1); 38 | if(data == 0) goto oops; 39 | 40 | if(read(fd, data, sz) != sz) goto oops; 41 | close(fd); 42 | data[sz] = 0; 43 | 44 | if(_sz) *_sz = sz; 45 | return data; 46 | 47 | oops: 48 | close(fd); 49 | if(data != 0) free(data); 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /src/libcutils/loghack.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 | /** 18 | * This is a temporary hack to enable logging from cutils. 19 | */ 20 | 21 | #ifndef _CUTILS_LOGHACK_H 22 | #define _CUTILS_LOGHACK_H 23 | 24 | #ifdef HAVE_ANDROID_OS 25 | #include 26 | #else 27 | #include 28 | #define ALOG(level, ...) \ 29 | ((void)printf("cutils:" level "/" LOG_TAG ": " __VA_ARGS__)) 30 | #define ALOGV(...) ALOG("V", __VA_ARGS__) 31 | #define ALOGD(...) ALOG("D", __VA_ARGS__) 32 | #define ALOGI(...) ALOG("I", __VA_ARGS__) 33 | #define ALOGW(...) ALOG("W", __VA_ARGS__) 34 | #define ALOGE(...) ALOG("E", __VA_ARGS__) 35 | #define LOG_ALWAYS_FATAL(...) do { ALOGE(__VA_ARGS__); exit(1); } while (0) 36 | #endif 37 | 38 | #endif // _CUTILS_LOGHACK_H 39 | -------------------------------------------------------------------------------- /src/libcutils/socket_inaddr_any_server.c: -------------------------------------------------------------------------------- 1 | /* libs/cutils/socket_inaddr_any_server.c 2 | ** 3 | ** Copyright 2006, The Android Open Source Project 4 | ** 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); 6 | ** you may not use this file except in compliance with the License. 7 | ** You may obtain a copy of the License at 8 | ** 9 | ** http://www.apache.org/licenses/LICENSE-2.0 10 | ** 11 | ** Unless required by applicable law or agreed to in writing, software 12 | ** distributed under the License is distributed on an "AS IS" BASIS, 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | ** See the License for the specific language governing permissions and 15 | ** limitations under the License. 16 | */ 17 | 18 | #include 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #ifndef HAVE_WINSOCK 27 | #include 28 | #include 29 | #include 30 | #include 31 | #endif 32 | 33 | #define LISTEN_BACKLOG 4 34 | 35 | /* open listen() port on any interface */ 36 | int socket_inaddr_any_server(int port, int type) 37 | { 38 | struct sockaddr_in addr; 39 | size_t alen; 40 | int s, n; 41 | 42 | memset(&addr, 0, sizeof(addr)); 43 | addr.sin_family = AF_INET; 44 | addr.sin_port = htons(port); 45 | addr.sin_addr.s_addr = htonl(INADDR_ANY); 46 | 47 | s = socket(AF_INET, type, 0); 48 | if(s < 0) return -1; 49 | 50 | n = 1; 51 | setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n)); 52 | 53 | if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { 54 | close(s); 55 | return -1; 56 | } 57 | 58 | if (type == SOCK_STREAM) { 59 | int ret; 60 | 61 | ret = listen(s, LISTEN_BACKLOG); 62 | 63 | if (ret < 0) { 64 | close(s); 65 | return -1; 66 | } 67 | } 68 | 69 | return s; 70 | } 71 | -------------------------------------------------------------------------------- /src/libcutils/socket_local.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2006 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 __SOCKET_LOCAL_H 18 | #define __SOCKET_LOCAL_H 19 | 20 | #define FILESYSTEM_SOCKET_PREFIX "/tmp/" 21 | #define ANDROID_RESERVED_SOCKET_PREFIX "/dev/socket/" 22 | 23 | /* 24 | * Set up a given sockaddr_un, to have it refer to the given 25 | * name in the given namespace. The namespace must be one 26 | * of ANDROID_SOCKET_NAMESPACE_ABSTRACT, 27 | * ANDROID_SOCKET_NAMESPACE_RESERVED, or 28 | * ANDROID_SOCKET_NAMESPACE_FILESYSTEM. Upon success, 29 | * the pointed at sockaddr_un is filled in and the pointed at 30 | * socklen_t is set to indicate the final length. This function 31 | * will fail if the namespace is invalid (not one of the indicated 32 | * constants) or if the name is too long. 33 | * 34 | * @return 0 on success or -1 on failure 35 | */ 36 | int socket_make_sockaddr_un(const char *name, int namespaceId, 37 | struct sockaddr_un *p_addr, socklen_t *alen); 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /src/libcutils/socket_local_client.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2006 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 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #ifdef HAVE_WINSOCK 26 | 27 | int socket_local_client(const char *name, int namespaceId, int type) 28 | { 29 | errno = ENOSYS; 30 | return -1; 31 | } 32 | 33 | #else /* !HAVE_WINSOCK */ 34 | 35 | #include 36 | #include 37 | #include 38 | #include 39 | 40 | #include "socket_local.h" 41 | 42 | #define LISTEN_BACKLOG 4 43 | 44 | /* Documented in header file. */ 45 | int socket_make_sockaddr_un(const char *name, int namespaceId, 46 | struct sockaddr_un *p_addr, socklen_t *alen) 47 | { 48 | memset (p_addr, 0, sizeof (*p_addr)); 49 | size_t namelen; 50 | 51 | switch (namespaceId) { 52 | case ANDROID_SOCKET_NAMESPACE_ABSTRACT: 53 | #ifdef HAVE_LINUX_LOCAL_SOCKET_NAMESPACE 54 | namelen = strlen(name); 55 | 56 | // Test with length +1 for the *initial* '\0'. 57 | if ((namelen + 1) > sizeof(p_addr->sun_path)) { 58 | goto error; 59 | } 60 | 61 | /* 62 | * Note: The path in this case is *not* supposed to be 63 | * '\0'-terminated. ("man 7 unix" for the gory details.) 64 | */ 65 | 66 | p_addr->sun_path[0] = 0; 67 | memcpy(p_addr->sun_path + 1, name, namelen); 68 | #else /*HAVE_LINUX_LOCAL_SOCKET_NAMESPACE*/ 69 | /* this OS doesn't have the Linux abstract namespace */ 70 | 71 | namelen = strlen(name) + strlen(FILESYSTEM_SOCKET_PREFIX); 72 | /* unix_path_max appears to be missing on linux */ 73 | if (namelen > sizeof(*p_addr) 74 | - offsetof(struct sockaddr_un, sun_path) - 1) { 75 | goto error; 76 | } 77 | 78 | strcpy(p_addr->sun_path, FILESYSTEM_SOCKET_PREFIX); 79 | strcat(p_addr->sun_path, name); 80 | #endif /*HAVE_LINUX_LOCAL_SOCKET_NAMESPACE*/ 81 | break; 82 | 83 | case ANDROID_SOCKET_NAMESPACE_RESERVED: 84 | namelen = strlen(name) + strlen(ANDROID_RESERVED_SOCKET_PREFIX); 85 | /* unix_path_max appears to be missing on linux */ 86 | if (namelen > sizeof(*p_addr) 87 | - offsetof(struct sockaddr_un, sun_path) - 1) { 88 | goto error; 89 | } 90 | 91 | strcpy(p_addr->sun_path, ANDROID_RESERVED_SOCKET_PREFIX); 92 | strcat(p_addr->sun_path, name); 93 | break; 94 | 95 | case ANDROID_SOCKET_NAMESPACE_FILESYSTEM: 96 | namelen = strlen(name); 97 | /* unix_path_max appears to be missing on linux */ 98 | if (namelen > sizeof(*p_addr) 99 | - offsetof(struct sockaddr_un, sun_path) - 1) { 100 | goto error; 101 | } 102 | 103 | strcpy(p_addr->sun_path, name); 104 | break; 105 | default: 106 | // invalid namespace id 107 | return -1; 108 | } 109 | 110 | p_addr->sun_family = AF_LOCAL; 111 | *alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1; 112 | return 0; 113 | error: 114 | return -1; 115 | } 116 | 117 | /** 118 | * connect to peer named "name" on fd 119 | * returns same fd or -1 on error. 120 | * fd is not closed on error. that's your job. 121 | * 122 | * Used by AndroidSocketImpl 123 | */ 124 | int socket_local_client_connect(int fd, const char *name, int namespaceId, 125 | int type) 126 | { 127 | struct sockaddr_un addr; 128 | socklen_t alen; 129 | size_t namelen; 130 | int err; 131 | 132 | err = socket_make_sockaddr_un(name, namespaceId, &addr, &alen); 133 | 134 | if (err < 0) { 135 | goto error; 136 | } 137 | 138 | if(connect(fd, (struct sockaddr *) &addr, alen) < 0) { 139 | goto error; 140 | } 141 | 142 | return fd; 143 | 144 | error: 145 | return -1; 146 | } 147 | 148 | /** 149 | * connect to peer named "name" 150 | * returns fd or -1 on error 151 | */ 152 | int socket_local_client(const char *name, int namespaceId, int type) 153 | { 154 | int s; 155 | 156 | s = socket(AF_LOCAL, type, 0); 157 | if(s < 0) return -1; 158 | 159 | if ( 0 > socket_local_client_connect(s, name, namespaceId, type)) { 160 | close(s); 161 | return -1; 162 | } 163 | 164 | return s; 165 | } 166 | 167 | #endif /* !HAVE_WINSOCK */ 168 | -------------------------------------------------------------------------------- /src/libcutils/socket_local_server.c: -------------------------------------------------------------------------------- 1 | /* libs/cutils/socket_local_server.c 2 | ** 3 | ** Copyright 2006, The Android Open Source Project 4 | ** 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); 6 | ** you may not use this file except in compliance with the License. 7 | ** You may obtain a copy of the License at 8 | ** 9 | ** http://www.apache.org/licenses/LICENSE-2.0 10 | ** 11 | ** Unless required by applicable law or agreed to in writing, software 12 | ** distributed under the License is distributed on an "AS IS" BASIS, 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | ** See the License for the specific language governing permissions and 15 | ** limitations under the License. 16 | */ 17 | 18 | #include 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #ifdef HAVE_WINSOCK 27 | 28 | int socket_local_server(const char *name, int namespaceId, int type) 29 | { 30 | errno = ENOSYS; 31 | return -1; 32 | } 33 | 34 | #else /* !HAVE_WINSOCK */ 35 | 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include 41 | 42 | #include "socket_local.h" 43 | 44 | #define LISTEN_BACKLOG 4 45 | 46 | 47 | /** 48 | * Binds a pre-created socket(AF_LOCAL) 's' to 'name' 49 | * returns 's' on success, -1 on fail 50 | * 51 | * Does not call listen() 52 | */ 53 | int socket_local_server_bind(int s, const char *name, int namespaceId) 54 | { 55 | struct sockaddr_un addr; 56 | socklen_t alen; 57 | int n; 58 | int err; 59 | 60 | err = socket_make_sockaddr_un(name, namespaceId, &addr, &alen); 61 | 62 | if (err < 0) { 63 | return -1; 64 | } 65 | 66 | /* basically: if this is a filesystem path, unlink first */ 67 | #ifndef HAVE_LINUX_LOCAL_SOCKET_NAMESPACE 68 | if (1) { 69 | #else 70 | if (namespaceId == ANDROID_SOCKET_NAMESPACE_RESERVED 71 | || namespaceId == ANDROID_SOCKET_NAMESPACE_FILESYSTEM) { 72 | #endif 73 | /*ignore ENOENT*/ 74 | unlink(addr.sun_path); 75 | } 76 | 77 | n = 1; 78 | setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n)); 79 | 80 | if(bind(s, (struct sockaddr *) &addr, alen) < 0) { 81 | return -1; 82 | } 83 | 84 | return s; 85 | 86 | } 87 | 88 | 89 | /** Open a server-side UNIX domain datagram socket in the Linux non-filesystem 90 | * namespace 91 | * 92 | * Returns fd on success, -1 on fail 93 | */ 94 | 95 | int socket_local_server(const char *name, int namespace, int type) 96 | { 97 | int err; 98 | int s; 99 | 100 | s = socket(AF_LOCAL, type, 0); 101 | if (s < 0) return -1; 102 | 103 | err = socket_local_server_bind(s, name, namespace); 104 | 105 | if (err < 0) { 106 | close(s); 107 | return -1; 108 | } 109 | 110 | if (type == SOCK_STREAM) { 111 | int ret; 112 | 113 | ret = listen(s, LISTEN_BACKLOG); 114 | 115 | if (ret < 0) { 116 | close(s); 117 | return -1; 118 | } 119 | } 120 | 121 | return s; 122 | } 123 | 124 | #endif /* !HAVE_WINSOCK */ 125 | -------------------------------------------------------------------------------- /src/libcutils/socket_loopback_client.c: -------------------------------------------------------------------------------- 1 | /* libs/cutils/socket_loopback_client.c 2 | ** 3 | ** Copyright 2006, The Android Open Source Project 4 | ** 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); 6 | ** you may not use this file except in compliance with the License. 7 | ** You may obtain a copy of the License at 8 | ** 9 | ** http://www.apache.org/licenses/LICENSE-2.0 10 | ** 11 | ** Unless required by applicable law or agreed to in writing, software 12 | ** distributed under the License is distributed on an "AS IS" BASIS, 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | ** See the License for the specific language governing permissions and 15 | ** limitations under the License. 16 | */ 17 | 18 | #include 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #ifndef HAVE_WINSOCK 27 | #include 28 | #include 29 | #include 30 | #include 31 | #endif 32 | 33 | /* Connect to port on the loopback IP interface. type is 34 | * SOCK_STREAM or SOCK_DGRAM. 35 | * return is a file descriptor or -1 on error 36 | */ 37 | int socket_loopback_client(int port, int type) 38 | { 39 | struct sockaddr_in addr; 40 | socklen_t alen; 41 | int s; 42 | 43 | memset(&addr, 0, sizeof(addr)); 44 | addr.sin_family = AF_INET; 45 | addr.sin_port = htons(port); 46 | addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 47 | 48 | s = socket(AF_INET, type, 0); 49 | if(s < 0) return -1; 50 | 51 | if(connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { 52 | close(s); 53 | return -1; 54 | } 55 | 56 | return s; 57 | 58 | } 59 | 60 | -------------------------------------------------------------------------------- /src/libcutils/socket_loopback_server.c: -------------------------------------------------------------------------------- 1 | /* libs/cutils/socket_loopback_server.c 2 | ** 3 | ** Copyright 2006, The Android Open Source Project 4 | ** 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); 6 | ** you may not use this file except in compliance with the License. 7 | ** You may obtain a copy of the License at 8 | ** 9 | ** http://www.apache.org/licenses/LICENSE-2.0 10 | ** 11 | ** Unless required by applicable law or agreed to in writing, software 12 | ** distributed under the License is distributed on an "AS IS" BASIS, 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | ** See the License for the specific language governing permissions and 15 | ** limitations under the License. 16 | */ 17 | 18 | #include 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #define LISTEN_BACKLOG 4 27 | 28 | #ifndef HAVE_WINSOCK 29 | #include 30 | #include 31 | #include 32 | #include 33 | #endif 34 | 35 | /* open listen() port on loopback interface */ 36 | int socket_loopback_server(int port, int type) 37 | { 38 | struct sockaddr_in addr; 39 | size_t alen; 40 | int s, n; 41 | 42 | memset(&addr, 0, sizeof(addr)); 43 | addr.sin_family = AF_INET; 44 | addr.sin_port = htons(port); 45 | addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 46 | 47 | s = socket(AF_INET, type, 0); 48 | if(s < 0) return -1; 49 | 50 | n = 1; 51 | setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n)); 52 | 53 | if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { 54 | close(s); 55 | return -1; 56 | } 57 | 58 | if (type == SOCK_STREAM) { 59 | int ret; 60 | 61 | ret = listen(s, LISTEN_BACKLOG); 62 | 63 | if (ret < 0) { 64 | close(s); 65 | return -1; 66 | } 67 | } 68 | 69 | return s; 70 | } 71 | 72 | -------------------------------------------------------------------------------- /src/libcutils/socket_network_client.c: -------------------------------------------------------------------------------- 1 | /* libs/cutils/socket_network_client.c 2 | ** 3 | ** Copyright 2006, The Android Open Source Project 4 | ** 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); 6 | ** you may not use this file except in compliance with the License. 7 | ** You may obtain a copy of the License at 8 | ** 9 | ** http://www.apache.org/licenses/LICENSE-2.0 10 | ** 11 | ** Unless required by applicable law or agreed to in writing, software 12 | ** distributed under the License is distributed on an "AS IS" BASIS, 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | ** See the License for the specific language governing permissions and 15 | ** limitations under the License. 16 | */ 17 | 18 | #include 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #ifndef HAVE_WINSOCK 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #endif 33 | 34 | 35 | /* Connect to port on the IP interface. type is 36 | * SOCK_STREAM or SOCK_DGRAM. 37 | * return is a file descriptor or -1 on error 38 | */ 39 | int socket_network_client(const char *host, int port, int type) 40 | { 41 | struct hostent *hp; 42 | struct sockaddr_in addr; 43 | socklen_t alen; 44 | int s; 45 | 46 | hp = gethostbyname(host); 47 | if(hp == 0) return -1; 48 | 49 | memset(&addr, 0, sizeof(addr)); 50 | addr.sin_family = hp->h_addrtype; 51 | addr.sin_port = htons(port); 52 | memcpy(&addr.sin_addr, hp->h_addr, hp->h_length); 53 | 54 | s = socket(hp->h_addrtype, type, 0); 55 | if(s < 0) return -1; 56 | 57 | if(connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { 58 | close(s); 59 | return -1; 60 | } 61 | 62 | return s; 63 | 64 | } 65 | 66 | -------------------------------------------------------------------------------- /src/libcutils/tzfile.h: -------------------------------------------------------------------------------- 1 | #ifndef TZFILE_H 2 | 3 | #define TZFILE_H 4 | 5 | /* 6 | ** This file is in the public domain, so clarified as of 7 | ** 1996-06-05 by Arthur David Olson. 8 | */ 9 | 10 | /* 11 | ** This header is for use ONLY with the time conversion code. 12 | ** There is no guarantee that it will remain unchanged, 13 | ** or that it will remain at all. 14 | ** Do NOT copy it to any system include directory. 15 | ** Thank you! 16 | */ 17 | 18 | /* 19 | ** ID 20 | */ 21 | 22 | #ifndef lint 23 | #ifndef NOID 24 | static char tzfilehid[] = "@(#)tzfile.h 8.1"; 25 | #endif /* !defined NOID */ 26 | #endif /* !defined lint */ 27 | 28 | /* 29 | ** Information about time zone files. 30 | */ 31 | 32 | #ifndef TZDIR 33 | #define TZDIR "/usr/share/zoneinfo" /* "/android/usr/share/zoneinfo" */ /* Time zone object file directory */ 34 | #endif /* !defined TZDIR */ 35 | 36 | #ifndef TZDEFAULT 37 | #define TZDEFAULT "localtime" 38 | #endif /* !defined TZDEFAULT */ 39 | 40 | #ifndef TZDEFRULES 41 | #define TZDEFRULES "posixrules" 42 | #endif /* !defined TZDEFRULES */ 43 | 44 | /* 45 | ** Each file begins with. . . 46 | */ 47 | 48 | #define TZ_MAGIC "TZif" 49 | 50 | struct tzhead { 51 | char tzh_magic[4]; /* TZ_MAGIC */ 52 | char tzh_version[1]; /* '\0' or '2' as of 2005 */ 53 | char tzh_reserved[15]; /* reserved--must be zero */ 54 | char tzh_ttisgmtcnt[4]; /* coded number of trans. time flags */ 55 | char tzh_ttisstdcnt[4]; /* coded number of trans. time flags */ 56 | char tzh_leapcnt[4]; /* coded number of leap seconds */ 57 | char tzh_timecnt[4]; /* coded number of transition times */ 58 | char tzh_typecnt[4]; /* coded number of local time types */ 59 | char tzh_charcnt[4]; /* coded number of abbr. chars */ 60 | }; 61 | 62 | /* 63 | ** . . .followed by. . . 64 | ** 65 | ** tzh_timecnt (char [4])s coded transition times a la time(2) 66 | ** tzh_timecnt (unsigned char)s types of local time starting at above 67 | ** tzh_typecnt repetitions of 68 | ** one (char [4]) coded UTC offset in seconds 69 | ** one (unsigned char) used to set tm_isdst 70 | ** one (unsigned char) that's an abbreviation list index 71 | ** tzh_charcnt (char)s '\0'-terminated zone abbreviations 72 | ** tzh_leapcnt repetitions of 73 | ** one (char [4]) coded leap second transition times 74 | ** one (char [4]) total correction after above 75 | ** tzh_ttisstdcnt (char)s indexed by type; if TRUE, transition 76 | ** time is standard time, if FALSE, 77 | ** transition time is wall clock time 78 | ** if absent, transition times are 79 | ** assumed to be wall clock time 80 | ** tzh_ttisgmtcnt (char)s indexed by type; if TRUE, transition 81 | ** time is UTC, if FALSE, 82 | ** transition time is local time 83 | ** if absent, transition times are 84 | ** assumed to be local time 85 | */ 86 | 87 | /* 88 | ** If tzh_version is '2' or greater, the above is followed by a second instance 89 | ** of tzhead and a second instance of the data in which each coded transition 90 | ** time uses 8 rather than 4 chars, 91 | ** then a POSIX-TZ-environment-variable-style string for use in handling 92 | ** instants after the last transition time stored in the file 93 | ** (with nothing between the newlines if there is no POSIX representation for 94 | ** such instants). 95 | */ 96 | 97 | /* 98 | ** In the current implementation, "tzset()" refuses to deal with files that 99 | ** exceed any of the limits below. 100 | */ 101 | 102 | #ifndef TZ_MAX_TIMES 103 | #define TZ_MAX_TIMES 1200 104 | #endif /* !defined TZ_MAX_TIMES */ 105 | 106 | #ifndef TZ_MAX_TYPES 107 | #ifndef NOSOLAR 108 | #define TZ_MAX_TYPES 256 /* Limited by what (unsigned char)'s can hold */ 109 | #endif /* !defined NOSOLAR */ 110 | #ifdef NOSOLAR 111 | /* 112 | ** Must be at least 14 for Europe/Riga as of Jan 12 1995, 113 | ** as noted by Earl Chew. 114 | */ 115 | #define TZ_MAX_TYPES 20 /* Maximum number of local time types */ 116 | #endif /* !defined NOSOLAR */ 117 | #endif /* !defined TZ_MAX_TYPES */ 118 | 119 | #ifndef TZ_MAX_CHARS 120 | #define TZ_MAX_CHARS 50 /* Maximum number of abbreviation characters */ 121 | /* (limited by what unsigned chars can hold) */ 122 | #endif /* !defined TZ_MAX_CHARS */ 123 | 124 | #ifndef TZ_MAX_LEAPS 125 | #define TZ_MAX_LEAPS 50 /* Maximum number of leap second corrections */ 126 | #endif /* !defined TZ_MAX_LEAPS */ 127 | 128 | #define SECSPERMIN 60 129 | #define MINSPERHOUR 60 130 | #define HOURSPERDAY 24 131 | #define DAYSPERWEEK 7 132 | #define DAYSPERNYEAR 365 133 | #define DAYSPERLYEAR 366 134 | #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR) 135 | #define SECSPERDAY ((long) SECSPERHOUR * HOURSPERDAY) 136 | #define MONSPERYEAR 12 137 | 138 | #define TM_SUNDAY 0 139 | #define TM_MONDAY 1 140 | #define TM_TUESDAY 2 141 | #define TM_WEDNESDAY 3 142 | #define TM_THURSDAY 4 143 | #define TM_FRIDAY 5 144 | #define TM_SATURDAY 6 145 | 146 | #define TM_JANUARY 0 147 | #define TM_FEBRUARY 1 148 | #define TM_MARCH 2 149 | #define TM_APRIL 3 150 | #define TM_MAY 4 151 | #define TM_JUNE 5 152 | #define TM_JULY 6 153 | #define TM_AUGUST 7 154 | #define TM_SEPTEMBER 8 155 | #define TM_OCTOBER 9 156 | #define TM_NOVEMBER 10 157 | #define TM_DECEMBER 11 158 | 159 | #define TM_YEAR_BASE 1900 160 | 161 | #define EPOCH_YEAR 1970 162 | #define EPOCH_WDAY TM_THURSDAY 163 | 164 | #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0)) 165 | 166 | /* 167 | ** Since everything in isleap is modulo 400 (or a factor of 400), we know that 168 | ** isleap(y) == isleap(y % 400) 169 | ** and so 170 | ** isleap(a + b) == isleap((a + b) % 400) 171 | ** or 172 | ** isleap(a + b) == isleap(a % 400 + b % 400) 173 | ** This is true even if % means modulo rather than Fortran remainder 174 | ** (which is allowed by C89 but not C99). 175 | ** We use this to avoid addition overflow problems. 176 | */ 177 | 178 | #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400) 179 | 180 | #endif /* !defined TZFILE_H */ 181 | -------------------------------------------------------------------------------- /src/libzipfile/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS = -g -w -fPIC -I. -I../../include/ 3 | 4 | SRCS= centraldir.c 5 | SRCS+= zipfile.c 6 | 7 | OBJS = $(SRCS:.c=.o) 8 | 9 | all: $(OBJS) 10 | 11 | clean: 12 | rm -rf $(OBJS) 13 | 14 | -------------------------------------------------------------------------------- /src/libzipfile/private.h: -------------------------------------------------------------------------------- 1 | #ifndef PRIVATE_H 2 | #define PRIVATE_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 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/zlib/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS = -g -w -fPIC -I. -I../../include/zlib 3 | 4 | SRCS= adler32.c 5 | SRCS+= compress.c 6 | SRCS+= crc32.c 7 | SRCS+= deflate.c 8 | SRCS+= infback.c 9 | SRCS+= inffast.c 10 | SRCS+= inflate.c 11 | SRCS+= inftrees.c 12 | SRCS+= trees.c 13 | SRCS+= uncompr.c 14 | SRCS+= zutil.c 15 | 16 | OBJS = $(SRCS:.c=.o) 17 | 18 | all: $(OBJS) 19 | 20 | clean: 21 | rm -rf $(OBJS) 22 | 23 | -------------------------------------------------------------------------------- /src/zlib/adler32.c: -------------------------------------------------------------------------------- 1 | /* adler32.c -- compute the Adler-32 checksum of a data stream 2 | * Copyright (C) 1995-2011 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_ OF((uLong adler1, uLong adler2, z_off64_t len2)); 13 | 14 | #define BASE 65521 /* 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 | try it both ways to see which is faster */ 26 | #ifdef NO_DIVIDE 27 | /* note that this assumes BASE is 65521, where 65536 % 65521 == 15 28 | (thank you to John Reiser for pointing this out) */ 29 | # define CHOP(a) \ 30 | do { \ 31 | unsigned long tmp = a >> 16; \ 32 | a &= 0xffffUL; \ 33 | a += (tmp << 4) - tmp; \ 34 | } while (0) 35 | # define MOD28(a) \ 36 | do { \ 37 | CHOP(a); \ 38 | if (a >= BASE) a -= BASE; \ 39 | } while (0) 40 | # define MOD(a) \ 41 | do { \ 42 | CHOP(a); \ 43 | MOD28(a); \ 44 | } while (0) 45 | # define MOD63(a) \ 46 | do { /* this assumes a is not negative */ \ 47 | z_off64_t tmp = a >> 32; \ 48 | a &= 0xffffffffL; \ 49 | a += (tmp << 8) - (tmp << 5) + tmp; \ 50 | tmp = a >> 16; \ 51 | a &= 0xffffL; \ 52 | a += (tmp << 4) - tmp; \ 53 | tmp = a >> 16; \ 54 | a &= 0xffffL; \ 55 | a += (tmp << 4) - tmp; \ 56 | if (a >= BASE) a -= BASE; \ 57 | } while (0) 58 | #else 59 | # define MOD(a) a %= BASE 60 | # define MOD28(a) a %= BASE 61 | # define MOD63(a) a %= BASE 62 | #endif 63 | 64 | /* ========================================================================= */ 65 | uLong ZEXPORT adler32(adler, buf, len) 66 | uLong adler; 67 | const Bytef *buf; 68 | uInt len; 69 | { 70 | unsigned long sum2; 71 | unsigned n; 72 | 73 | /* split Adler-32 into component sums */ 74 | sum2 = (adler >> 16) & 0xffff; 75 | adler &= 0xffff; 76 | 77 | /* in case user likes doing a byte at a time, keep it fast */ 78 | if (len == 1) { 79 | adler += buf[0]; 80 | if (adler >= BASE) 81 | adler -= BASE; 82 | sum2 += adler; 83 | if (sum2 >= BASE) 84 | sum2 -= BASE; 85 | return adler | (sum2 << 16); 86 | } 87 | 88 | /* initial Adler-32 value (deferred check for len == 1 speed) */ 89 | if (buf == Z_NULL) 90 | return 1L; 91 | 92 | /* in case short lengths are provided, keep it somewhat fast */ 93 | if (len < 16) { 94 | while (len--) { 95 | adler += *buf++; 96 | sum2 += adler; 97 | } 98 | if (adler >= BASE) 99 | adler -= BASE; 100 | MOD28(sum2); /* only added so many BASE's */ 101 | return adler | (sum2 << 16); 102 | } 103 | 104 | /* do length NMAX blocks -- requires just one modulo operation */ 105 | while (len >= NMAX) { 106 | len -= NMAX; 107 | n = NMAX / 16; /* NMAX is divisible by 16 */ 108 | do { 109 | DO16(buf); /* 16 sums unrolled */ 110 | buf += 16; 111 | } while (--n); 112 | MOD(adler); 113 | MOD(sum2); 114 | } 115 | 116 | /* do remaining bytes (less than NMAX, still just one modulo) */ 117 | if (len) { /* avoid modulos if none remaining */ 118 | while (len >= 16) { 119 | len -= 16; 120 | DO16(buf); 121 | buf += 16; 122 | } 123 | while (len--) { 124 | adler += *buf++; 125 | sum2 += adler; 126 | } 127 | MOD(adler); 128 | MOD(sum2); 129 | } 130 | 131 | /* return recombined sums */ 132 | return adler | (sum2 << 16); 133 | } 134 | 135 | /* ========================================================================= */ 136 | local uLong adler32_combine_(adler1, adler2, len2) 137 | uLong adler1; 138 | uLong adler2; 139 | z_off64_t len2; 140 | { 141 | unsigned long sum1; 142 | unsigned long sum2; 143 | unsigned rem; 144 | 145 | /* for negative len, return invalid adler32 as a clue for debugging */ 146 | if (len2 < 0) 147 | return 0xffffffffUL; 148 | 149 | /* the derivation of this formula is left as an exercise for the reader */ 150 | MOD63(len2); /* assumes len2 >= 0 */ 151 | rem = (unsigned)len2; 152 | sum1 = adler1 & 0xffff; 153 | sum2 = rem * sum1; 154 | MOD(sum2); 155 | sum1 += (adler2 & 0xffff) + BASE - 1; 156 | sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem; 157 | if (sum1 >= BASE) sum1 -= BASE; 158 | if (sum1 >= BASE) sum1 -= BASE; 159 | if (sum2 >= (BASE << 1)) sum2 -= (BASE << 1); 160 | if (sum2 >= BASE) sum2 -= BASE; 161 | return sum1 | (sum2 << 16); 162 | } 163 | 164 | /* ========================================================================= */ 165 | uLong ZEXPORT adler32_combine(adler1, adler2, len2) 166 | uLong adler1; 167 | uLong adler2; 168 | z_off_t len2; 169 | { 170 | return adler32_combine_(adler1, adler2, len2); 171 | } 172 | 173 | uLong ZEXPORT adler32_combine64(adler1, adler2, len2) 174 | uLong adler1; 175 | uLong adler2; 176 | z_off64_t len2; 177 | { 178 | return adler32_combine_(adler1, adler2, len2); 179 | } 180 | -------------------------------------------------------------------------------- /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/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. 6 | It is part of the implementation of this library and is 7 | 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/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 | --------------------------------------------------------------------------------