├── Untitled Document~ ├── utils-cpp ├── MODULE_LICENSE_APACHE2 ├── Flattenable.cpp ├── ZipFileCRO.cpp ├── AssetDir.cpp ├── StopWatch.cpp ├── Static.cpp ├── StringArray.cpp ├── SharedBuffer.cpp ├── Android.mk ├── SystemClock.cpp ├── Timers.cpp ├── TextOutput.cpp └── misc.cpp ├── libs ├── libc.so ├── liblog.so └── libstdc++.so ├── preDef.h ├── bin └── Debug │ └── aapt ├── obj └── Debug │ ├── Main.o │ ├── Images.o │ ├── Command.o │ ├── Package.o │ ├── Resource.o │ ├── SourcePos.o │ ├── XMLNode.o │ ├── ZipEntry.o │ ├── ZipFile.o │ ├── printapk.o │ ├── AaptAssets.o │ ├── CrunchCache.o │ ├── FileFinder.o │ ├── StringPool.o │ ├── ResourceFilter.o │ ├── ResourceTable.o │ ├── cutils-c │ ├── array.o │ ├── uio.o │ ├── atomic.o │ ├── sockets.o │ ├── threads.o │ ├── properties.o │ ├── abort_socket.o │ ├── sched_policy.o │ └── android_reboot.o │ ├── utils-cpp │ ├── misc.o │ ├── Asset.o │ ├── Debug.o │ ├── Looper.o │ ├── Static.o │ ├── Timers.o │ ├── AssetDir.o │ ├── BlobCache.o │ ├── CallStack.o │ ├── FileMap.o │ ├── ObbFile.o │ ├── RefBase.o │ ├── StopWatch.o │ ├── String16.o │ ├── String8.o │ ├── Threads.o │ ├── Tokenizer.o │ ├── Unicode.o │ ├── ZipFileRO.o │ ├── ZipUtils.o │ ├── BackupData.o │ ├── Flattenable.o │ ├── PropertyMap.o │ ├── StringArray.o │ ├── SystemClock.o │ ├── TextOutput.o │ ├── VectorImpl.o │ ├── ZipFileCRO.o │ ├── AssetManager.o │ ├── BackupHelpers.o │ ├── ResourceTypes.o │ ├── SharedBuffer.o │ ├── LinearTransform.o │ ├── BufferedTextOutput.o │ └── StreamingZipInflater.o │ └── host │ └── pseudolocalize.o ├── constDef.h ├── aapt.layout ├── host ├── pseudolocalize.h └── pseudolocalize.cpp ├── fakeLog.h ├── SourcePos.h ├── Images.h ├── cutils-c ├── atomic.c ├── loghack.h ├── sockets.c ├── uio.c └── threads.c ├── utils ├── Atomic.h ├── SystemClock.h ├── Functor.h ├── Endian.h ├── zLog.h ├── ashmem.h ├── Compat.h ├── StopWatch.h ├── ZipFileCRO.h ├── Flattenable.h ├── LinearTransform.h ├── BufferedTextOutput.h ├── StringArray.h ├── CallStack.h ├── ZipUtils.h ├── Debug.h ├── ByteOrder.h ├── Singleton.h ├── Errors.h ├── misc.h ├── StreamingZipInflater.h ├── PropertyMap.h ├── ObbFile.h ├── Tokenizer.h ├── BitSet.h ├── FileMap.h ├── Timers.h ├── AssetDir.h ├── BackupHelpers.h └── SharedBuffer.h ├── ResourceFilter.h ├── cutils ├── sched_policy.h ├── uio.h ├── logd.h ├── atomic-inline.h ├── properties.h ├── sockets.h ├── threads.h └── atomic-x86.h ├── private └── utils │ └── Static.h ├── aapt.cbp~ ├── Android.mk ├── zipfile └── zipfile.h ├── Main.h ├── README.md ├── DirectoryWalker.h ├── FileFinder.h ├── ResourceFilter.cpp ├── FileFinder.cpp ├── printapk.cpp ├── CrunchCache.h ├── CrunchCache.cpp ├── CacheUpdater.h ├── SourcePos.cpp └── system └── graphics.h /Untitled Document~: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /utils-cpp/MODULE_LICENSE_APACHE2: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /libs/libc.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/libs/libc.so -------------------------------------------------------------------------------- /preDef.h: -------------------------------------------------------------------------------- 1 | #ifndef OS_PATH_SEPARATOR 2 | #define OS_PATH_SEPARATOR '/' 3 | #endif 4 | -------------------------------------------------------------------------------- /bin/Debug/aapt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/bin/Debug/aapt -------------------------------------------------------------------------------- /libs/liblog.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/libs/liblog.so -------------------------------------------------------------------------------- /obj/Debug/Main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/Main.o -------------------------------------------------------------------------------- /libs/libstdc++.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/libs/libstdc++.so -------------------------------------------------------------------------------- /obj/Debug/Images.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/Images.o -------------------------------------------------------------------------------- /obj/Debug/Command.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/Command.o -------------------------------------------------------------------------------- /obj/Debug/Package.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/Package.o -------------------------------------------------------------------------------- /obj/Debug/Resource.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/Resource.o -------------------------------------------------------------------------------- /obj/Debug/SourcePos.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/SourcePos.o -------------------------------------------------------------------------------- /obj/Debug/XMLNode.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/XMLNode.o -------------------------------------------------------------------------------- /obj/Debug/ZipEntry.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/ZipEntry.o -------------------------------------------------------------------------------- /obj/Debug/ZipFile.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/ZipFile.o -------------------------------------------------------------------------------- /obj/Debug/printapk.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/printapk.o -------------------------------------------------------------------------------- /obj/Debug/AaptAssets.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/AaptAssets.o -------------------------------------------------------------------------------- /obj/Debug/CrunchCache.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/CrunchCache.o -------------------------------------------------------------------------------- /obj/Debug/FileFinder.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/FileFinder.o -------------------------------------------------------------------------------- /obj/Debug/StringPool.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/StringPool.o -------------------------------------------------------------------------------- /obj/Debug/ResourceFilter.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/ResourceFilter.o -------------------------------------------------------------------------------- /obj/Debug/ResourceTable.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/ResourceTable.o -------------------------------------------------------------------------------- /obj/Debug/cutils-c/array.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/cutils-c/array.o -------------------------------------------------------------------------------- /obj/Debug/cutils-c/uio.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/cutils-c/uio.o -------------------------------------------------------------------------------- /obj/Debug/utils-cpp/misc.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/utils-cpp/misc.o -------------------------------------------------------------------------------- /obj/Debug/cutils-c/atomic.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/cutils-c/atomic.o -------------------------------------------------------------------------------- /obj/Debug/cutils-c/sockets.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/cutils-c/sockets.o -------------------------------------------------------------------------------- /obj/Debug/cutils-c/threads.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/cutils-c/threads.o -------------------------------------------------------------------------------- /obj/Debug/utils-cpp/Asset.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/utils-cpp/Asset.o -------------------------------------------------------------------------------- /obj/Debug/utils-cpp/Debug.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/utils-cpp/Debug.o -------------------------------------------------------------------------------- /obj/Debug/utils-cpp/Looper.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/utils-cpp/Looper.o -------------------------------------------------------------------------------- /obj/Debug/utils-cpp/Static.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/utils-cpp/Static.o -------------------------------------------------------------------------------- /obj/Debug/utils-cpp/Timers.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/utils-cpp/Timers.o -------------------------------------------------------------------------------- /obj/Debug/cutils-c/properties.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/cutils-c/properties.o -------------------------------------------------------------------------------- /obj/Debug/host/pseudolocalize.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/host/pseudolocalize.o -------------------------------------------------------------------------------- /obj/Debug/utils-cpp/AssetDir.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/utils-cpp/AssetDir.o -------------------------------------------------------------------------------- /obj/Debug/utils-cpp/BlobCache.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/utils-cpp/BlobCache.o -------------------------------------------------------------------------------- /obj/Debug/utils-cpp/CallStack.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/utils-cpp/CallStack.o -------------------------------------------------------------------------------- /obj/Debug/utils-cpp/FileMap.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/utils-cpp/FileMap.o -------------------------------------------------------------------------------- /obj/Debug/utils-cpp/ObbFile.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/utils-cpp/ObbFile.o -------------------------------------------------------------------------------- /obj/Debug/utils-cpp/RefBase.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/utils-cpp/RefBase.o -------------------------------------------------------------------------------- /obj/Debug/utils-cpp/StopWatch.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/utils-cpp/StopWatch.o -------------------------------------------------------------------------------- /obj/Debug/utils-cpp/String16.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/utils-cpp/String16.o -------------------------------------------------------------------------------- /obj/Debug/utils-cpp/String8.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/utils-cpp/String8.o -------------------------------------------------------------------------------- /obj/Debug/utils-cpp/Threads.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/utils-cpp/Threads.o -------------------------------------------------------------------------------- /obj/Debug/utils-cpp/Tokenizer.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/utils-cpp/Tokenizer.o -------------------------------------------------------------------------------- /obj/Debug/utils-cpp/Unicode.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/utils-cpp/Unicode.o -------------------------------------------------------------------------------- /obj/Debug/utils-cpp/ZipFileRO.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/utils-cpp/ZipFileRO.o -------------------------------------------------------------------------------- /obj/Debug/utils-cpp/ZipUtils.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/utils-cpp/ZipUtils.o -------------------------------------------------------------------------------- /obj/Debug/cutils-c/abort_socket.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/cutils-c/abort_socket.o -------------------------------------------------------------------------------- /obj/Debug/cutils-c/sched_policy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/cutils-c/sched_policy.o -------------------------------------------------------------------------------- /obj/Debug/utils-cpp/BackupData.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/utils-cpp/BackupData.o -------------------------------------------------------------------------------- /obj/Debug/utils-cpp/Flattenable.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/utils-cpp/Flattenable.o -------------------------------------------------------------------------------- /obj/Debug/utils-cpp/PropertyMap.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/utils-cpp/PropertyMap.o -------------------------------------------------------------------------------- /obj/Debug/utils-cpp/StringArray.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/utils-cpp/StringArray.o -------------------------------------------------------------------------------- /obj/Debug/utils-cpp/SystemClock.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/utils-cpp/SystemClock.o -------------------------------------------------------------------------------- /obj/Debug/utils-cpp/TextOutput.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/utils-cpp/TextOutput.o -------------------------------------------------------------------------------- /obj/Debug/utils-cpp/VectorImpl.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/utils-cpp/VectorImpl.o -------------------------------------------------------------------------------- /obj/Debug/utils-cpp/ZipFileCRO.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/utils-cpp/ZipFileCRO.o -------------------------------------------------------------------------------- /obj/Debug/cutils-c/android_reboot.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/cutils-c/android_reboot.o -------------------------------------------------------------------------------- /obj/Debug/utils-cpp/AssetManager.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/utils-cpp/AssetManager.o -------------------------------------------------------------------------------- /obj/Debug/utils-cpp/BackupHelpers.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/utils-cpp/BackupHelpers.o -------------------------------------------------------------------------------- /obj/Debug/utils-cpp/ResourceTypes.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/utils-cpp/ResourceTypes.o -------------------------------------------------------------------------------- /obj/Debug/utils-cpp/SharedBuffer.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/utils-cpp/SharedBuffer.o -------------------------------------------------------------------------------- /obj/Debug/utils-cpp/LinearTransform.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/utils-cpp/LinearTransform.o -------------------------------------------------------------------------------- /obj/Debug/utils-cpp/BufferedTextOutput.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/utils-cpp/BufferedTextOutput.o -------------------------------------------------------------------------------- /obj/Debug/utils-cpp/StreamingZipInflater.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DylanZhao/aapt-build/HEAD/obj/Debug/utils-cpp/StreamingZipInflater.o -------------------------------------------------------------------------------- /constDef.h: -------------------------------------------------------------------------------- 1 | #ifndef CONSTDEF_H_INCLUDED 2 | #define CONSTDEF_H_INCLUDED 3 | 4 | #define OS_PATH_SEPARATOR '/' 5 | 6 | #endif // CONSTDEF_H_INCLUDED 7 | -------------------------------------------------------------------------------- /aapt.layout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /host/pseudolocalize.h: -------------------------------------------------------------------------------- 1 | #ifndef HOST_PSEUDOLOCALIZE_H 2 | #define HOST_PSEUDOLOCALIZE_H 3 | 4 | #include 5 | 6 | std::string pseudolocalize_string(const std::string& source); 7 | 8 | #endif // HOST_PSEUDOLOCALIZE_H 9 | 10 | -------------------------------------------------------------------------------- /fakeLog.h: -------------------------------------------------------------------------------- 1 | #ifndef FAKELOG_H_INCLUDED 2 | #define FAKELOG_H_INCLUDED 3 | 4 | #define LOG(...) 5 | #define LOGV(...) 6 | #define LOGD(...) 7 | #define LOGI(...) 8 | #define LOGW(...) 9 | #define LOGE(...) 10 | #define LOG_FATAL_IF(...) 11 | #define LOG_ALWAYS_FATAL_IF(...) 12 | #define LOG_ASSERT(...) 13 | #define LOG_ALWAYS_FATAL(...) 14 | 15 | #define SLOGE(...) 16 | #define SLOGW(...) 17 | 18 | #endif // FAKELOG_H_INCLUDED 19 | -------------------------------------------------------------------------------- /SourcePos.h: -------------------------------------------------------------------------------- 1 | #ifndef SOURCEPOS_H 2 | #define SOURCEPOS_H 3 | 4 | #include 5 | #include 6 | 7 | using namespace android; 8 | 9 | class SourcePos 10 | { 11 | public: 12 | String8 file; 13 | int line; 14 | 15 | SourcePos(const String8& f, int l); 16 | SourcePos(const SourcePos& that); 17 | SourcePos(); 18 | ~SourcePos(); 19 | 20 | int error(const char* fmt, ...) const; 21 | int warning(const char* fmt, ...) const; 22 | 23 | static bool hasErrors(); 24 | static void printErrors(FILE* to); 25 | }; 26 | 27 | 28 | #endif // SOURCEPOS_H 29 | -------------------------------------------------------------------------------- /Images.h: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright 2006 The Android Open Source Project 3 | // 4 | // Build resource files from raw assets. 5 | // 6 | 7 | #ifndef IMAGES_H 8 | #define IMAGES_H 9 | 10 | #include "ResourceTable.h" 11 | #include "Bundle.h" 12 | 13 | #include 14 | #include 15 | 16 | using android::String8; 17 | 18 | status_t preProcessImage(Bundle* bundle, const sp& assets, 19 | const sp& file, String8* outNewLeafName); 20 | 21 | status_t preProcessImageToCache(Bundle* bundle, String8 source, String8 dest); 22 | 23 | status_t postProcessImage(const sp& assets, 24 | ResourceTable* table, const sp& file); 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /cutils-c/atomic.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 | #define inline 18 | 19 | #include 20 | -------------------------------------------------------------------------------- /utils/Atomic.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005 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_UTILS_ATOMIC_H 18 | #define ANDROID_UTILS_ATOMIC_H 19 | 20 | #include 21 | 22 | #endif // ANDROID_UTILS_ATOMIC_H 23 | -------------------------------------------------------------------------------- /utils-cpp/Flattenable.cpp: -------------------------------------------------------------------------------- 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 | namespace android { 20 | 21 | Flattenable::~Flattenable() { 22 | } 23 | 24 | }; // namespace android 25 | -------------------------------------------------------------------------------- /ResourceFilter.h: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright 2011 The Android Open Source Project 3 | // 4 | // Build resource files from raw assets. 5 | // 6 | 7 | #ifndef RESOURCE_FILTER_H 8 | #define RESOURCE_FILTER_H 9 | 10 | #include "AaptAssets.h" 11 | 12 | /** 13 | * Implements logic for parsing and handling "-c" and "--preferred-configurations" 14 | * options. 15 | */ 16 | class ResourceFilter 17 | { 18 | public: 19 | ResourceFilter() : mData(), mContainsPseudo(false) {} 20 | status_t parse(const char* arg); 21 | bool isEmpty() const; 22 | bool match(int axis, uint32_t value) const; 23 | bool match(int axis, const ResTable_config& config) const; 24 | bool match(const ResTable_config& config) const; 25 | const SortedVector* configsForAxis(int axis) const; 26 | inline bool containsPseudo() const { return mContainsPseudo; } 27 | 28 | private: 29 | KeyedVector > mData; 30 | bool mContainsPseudo; 31 | }; 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /utils/SystemClock.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 ANDROID_UTILS_SYSTEMCLOCK_H 18 | #define ANDROID_UTILS_SYSTEMCLOCK_H 19 | 20 | #include 21 | #include 22 | 23 | namespace android { 24 | 25 | int setCurrentTimeMillis(int64_t millis); 26 | int64_t uptimeMillis(); 27 | int64_t elapsedRealtime(); 28 | 29 | }; // namespace android 30 | 31 | #endif // ANDROID_UTILS_SYSTEMCLOCK_H 32 | 33 | -------------------------------------------------------------------------------- /utils/Functor.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 ANDROID_FUNCTOR_H 18 | #define ANDROID_FUNCTOR_H 19 | 20 | #include 21 | 22 | namespace android { 23 | 24 | class Functor { 25 | public: 26 | Functor() {} 27 | virtual ~Functor() {} 28 | virtual status_t operator ()(int what, void* data) { return NO_ERROR; } 29 | }; 30 | 31 | }; // namespace android 32 | 33 | #endif // ANDROID_FUNCTOR_H 34 | -------------------------------------------------------------------------------- /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 | typedef enum { 25 | SP_BACKGROUND = 0, 26 | SP_FOREGROUND = 1, 27 | } SchedPolicy; 28 | 29 | extern int set_sched_policy(int tid, SchedPolicy policy); 30 | extern int get_sched_policy(int tid, SchedPolicy *policy); 31 | 32 | #ifdef __cplusplus 33 | } 34 | #endif 35 | 36 | #endif /* __CUTILS_SCHED_POLICY_H */ 37 | -------------------------------------------------------------------------------- /utils/Endian.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005 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 | // Android endian-ness defines. 19 | // 20 | #ifndef _LIBS_UTILS_ENDIAN_H 21 | #define _LIBS_UTILS_ENDIAN_H 22 | 23 | #if defined(HAVE_ENDIAN_H) 24 | 25 | #include 26 | 27 | #else /*not HAVE_ENDIAN_H*/ 28 | 29 | #define __BIG_ENDIAN 0x1000 30 | #define __LITTLE_ENDIAN 0x0001 31 | 32 | #if defined(HAVE_LITTLE_ENDIAN) 33 | # define __BYTE_ORDER __LITTLE_ENDIAN 34 | #else 35 | # define __BYTE_ORDER __BIG_ENDIAN 36 | #endif 37 | 38 | #endif /*not HAVE_ENDIAN_H*/ 39 | 40 | #endif /*_LIBS_UTILS_ENDIAN_H*/ 41 | -------------------------------------------------------------------------------- /private/utils/Static.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 | // All static variables go here, to control initialization and 18 | // destruction order in the library. 19 | 20 | #include 21 | #include 22 | 23 | namespace android { 24 | // For TextStream.cpp 25 | extern Vector gTextBuffers; 26 | 27 | // For String8.cpp 28 | extern void initialize_string8(); 29 | extern void terminate_string8(); 30 | 31 | // For String16.cpp 32 | extern void initialize_string16(); 33 | extern void terminate_string16(); 34 | 35 | } // namespace android 36 | -------------------------------------------------------------------------------- /aapt.cbp~: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 42 | 43 | -------------------------------------------------------------------------------- /utils/zLog.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005 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 | // C/C++ logging functions. See the logging documentation for API details. 19 | // 20 | // We'd like these to be available from C code (in case we import some from 21 | // somewhere), so this has a C interface. 22 | // 23 | // The output will be correct when the log file is shared between multiple 24 | // threads and/or multiple processes so long as the operating system 25 | // supports O_APPEND. These calls have mutex-protected data structures 26 | // and so are NOT reentrant. Do not use LOG in a signal handler. 27 | // 28 | #ifndef _LIBS_UTILS_LOG_H 29 | #define _LIBS_UTILS_LOG_H 30 | 31 | #include 32 | 33 | #endif // _LIBS_UTILS_LOG_H 34 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /cutils-c/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 LOG(level, ...) \ 29 | ((void)printf("cutils:" level "/" LOG_TAG ": " __VA_ARGS__)) 30 | #define LOGV(...) LOG("V", __VA_ARGS__) 31 | #define LOGD(...) LOG("D", __VA_ARGS__) 32 | #define LOGI(...) LOG("I", __VA_ARGS__) 33 | #define LOGW(...) LOG("W", __VA_ARGS__) 34 | #define LOGE(...) LOG("E", __VA_ARGS__) 35 | #define LOG_ALWAYS_FATAL(...) do { LOGE(__VA_ARGS__); exit(1); } while (0) 36 | #endif 37 | 38 | #endif // _CUTILS_LOGHACK_H 39 | -------------------------------------------------------------------------------- /Android.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2006 The Android Open Source Project 3 | # 4 | # Android Asset Packaging Tool 5 | # 6 | 7 | # This tool is prebuilt if we're doing an app-only build. 8 | ifeq ($(TARGET_BUILD_APPS),) 9 | 10 | LOCAL_PATH:= $(call my-dir) 11 | include $(CLEAR_VARS) 12 | 13 | LOCAL_SRC_FILES := \ 14 | AaptAssets.cpp \ 15 | Command.cpp \ 16 | CrunchCache.cpp \ 17 | FileFinder.cpp \ 18 | Main.cpp \ 19 | Package.cpp \ 20 | StringPool.cpp \ 21 | XMLNode.cpp \ 22 | ResourceFilter.cpp \ 23 | ResourceTable.cpp \ 24 | Images.cpp \ 25 | Resource.cpp \ 26 | SourcePos.cpp \ 27 | ZipEntry.cpp \ 28 | ZipFile.cpp 29 | 30 | 31 | LOCAL_CFLAGS += -Wno-format-y2k 32 | 33 | LOCAL_C_INCLUDES += external/expat/lib 34 | LOCAL_C_INCLUDES += external/libpng 35 | LOCAL_C_INCLUDES += external/zlib 36 | LOCAL_C_INCLUDES += build/libs/host/include 37 | 38 | #LOCAL_WHOLE_STATIC_LIBRARIES := 39 | LOCAL_STATIC_LIBRARIES := \ 40 | libhost \ 41 | libutils \ 42 | libcutils \ 43 | libexpat \ 44 | libpng 45 | 46 | ifeq ($(HOST_OS),linux) 47 | LOCAL_LDLIBS += -lrt -lpthread 48 | endif 49 | 50 | # Statically link libz for MinGW (Win SDK under Linux), 51 | # and dynamically link for all others. 52 | ifneq ($(strip $(USE_MINGW)),) 53 | LOCAL_STATIC_LIBRARIES += libz 54 | else 55 | LOCAL_LDLIBS += -lz 56 | endif 57 | 58 | LOCAL_MODULE := aapt 59 | 60 | include $(BUILD_HOST_EXECUTABLE) 61 | 62 | endif # TARGET_BUILD_APPS 63 | -------------------------------------------------------------------------------- /utils/ashmem.h: -------------------------------------------------------------------------------- 1 | /* utils/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 _UTILS_ASHMEM_H 11 | #define _UTILS_ASHMEM_H 12 | 13 | #include 14 | #include 15 | 16 | #define ASHMEM_NAME_LEN 256 17 | 18 | #define ASHMEM_NAME_DEF "dev/ashmem" 19 | 20 | /* Return values from ASHMEM_PIN: Was the mapping purged while unpinned? */ 21 | #define ASHMEM_NOT_REAPED 0 22 | #define ASHMEM_WAS_REAPED 1 23 | 24 | /* Return values from ASHMEM_UNPIN: Is the mapping now pinned or unpinned? */ 25 | #define ASHMEM_NOW_UNPINNED 0 26 | #define ASHMEM_NOW_PINNED 1 27 | 28 | #define __ASHMEMIOC 0x77 29 | 30 | #define ASHMEM_SET_NAME _IOW(__ASHMEMIOC, 1, char[ASHMEM_NAME_LEN]) 31 | #define ASHMEM_GET_NAME _IOR(__ASHMEMIOC, 2, char[ASHMEM_NAME_LEN]) 32 | #define ASHMEM_SET_SIZE _IOW(__ASHMEMIOC, 3, size_t) 33 | #define ASHMEM_GET_SIZE _IO(__ASHMEMIOC, 4) 34 | #define ASHMEM_SET_PROT_MASK _IOW(__ASHMEMIOC, 5, unsigned long) 35 | #define ASHMEM_GET_PROT_MASK _IO(__ASHMEMIOC, 6) 36 | #define ASHMEM_PIN _IO(__ASHMEMIOC, 7) 37 | #define ASHMEM_UNPIN _IO(__ASHMEMIOC, 8) 38 | #define ASHMEM_ISPINNED _IO(__ASHMEMIOC, 9) 39 | #define ASHMEM_PURGE_ALL_CACHES _IO(__ASHMEMIOC, 10) 40 | 41 | #endif /* _UTILS_ASHMEM_H */ 42 | -------------------------------------------------------------------------------- /utils/Compat.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 __LIB_UTILS_COMPAT_H 18 | #define __LIB_UTILS_COMPAT_H 19 | 20 | #include 21 | 22 | /* Compatibility definitions for non-Linux (i.e., BSD-based) hosts. */ 23 | #ifndef HAVE_OFF64_T 24 | #if _FILE_OFFSET_BITS < 64 25 | #error "_FILE_OFFSET_BITS < 64; large files are not supported on this platform" 26 | #endif /* _FILE_OFFSET_BITS < 64 */ 27 | 28 | typedef off_t off64_t; 29 | 30 | static inline off64_t lseek64(int fd, off64_t offset, int whence) { 31 | return lseek(fd, offset, whence); 32 | } 33 | 34 | #ifdef HAVE_PREAD 35 | static inline ssize_t pread64(int fd, void* buf, size_t nbytes, off64_t offset) { 36 | return pread(fd, buf, nbytes, offset); 37 | } 38 | #endif 39 | 40 | #endif /* !HAVE_OFF64_T */ 41 | 42 | #endif /* __LIB_UTILS_COMPAT_H */ 43 | -------------------------------------------------------------------------------- /cutils-c/sockets.c: -------------------------------------------------------------------------------- 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 | #include 18 | #include 19 | 20 | #ifdef HAVE_ANDROID_OS 21 | /* For the socket trust (credentials) check */ 22 | #include 23 | #endif 24 | 25 | bool socket_peer_is_trusted(int fd) 26 | { 27 | #ifdef HAVE_ANDROID_OS 28 | struct ucred cr; 29 | socklen_t len = sizeof(cr); 30 | int n = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len); 31 | 32 | if (n != 0) { 33 | LOGE("could not get socket credentials: %s\n", strerror(errno)); 34 | return false; 35 | } 36 | 37 | if ((cr.uid != AID_ROOT) && (cr.uid != AID_SHELL)) { 38 | LOGE("untrusted userid on other end of socket: userid %d\n", cr.uid); 39 | return false; 40 | } 41 | #endif 42 | 43 | return true; 44 | } 45 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /utils/StopWatch.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005 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_STOPWATCH_H 18 | #define ANDROID_STOPWATCH_H 19 | 20 | #include 21 | #include 22 | 23 | #include 24 | 25 | // --------------------------------------------------------------------------- 26 | 27 | namespace android { 28 | 29 | class StopWatch 30 | { 31 | public: 32 | StopWatch( const char *name, 33 | int clock = SYSTEM_TIME_MONOTONIC, 34 | uint32_t flags = 0); 35 | ~StopWatch(); 36 | 37 | const char* name() const; 38 | nsecs_t lap(); 39 | nsecs_t elapsedTime() const; 40 | 41 | void reset(); 42 | 43 | private: 44 | const char* mName; 45 | int mClock; 46 | uint32_t mFlags; 47 | 48 | struct lap_t { 49 | nsecs_t soFar; 50 | nsecs_t thisLap; 51 | }; 52 | 53 | nsecs_t mStartTime; 54 | lap_t mLaps[8]; 55 | int mNumLaps; 56 | }; 57 | 58 | 59 | }; // namespace android 60 | 61 | 62 | // --------------------------------------------------------------------------- 63 | 64 | #endif // ANDROID_STOPWATCH_H 65 | -------------------------------------------------------------------------------- /utils/ZipFileCRO.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 | // C API for ead-only access to Zip archives, with minimal heap allocation. 19 | // 20 | #ifndef __LIBS_ZIPFILECRO_H 21 | #define __LIBS_ZIPFILECRO_H 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | #include 28 | 29 | #ifdef __cplusplus 30 | extern "C" { 31 | #endif 32 | 33 | /* 34 | * Trivial typedef to ensure that ZipFileCRO is not treated as a simple integer. 35 | */ 36 | typedef void* ZipFileCRO; 37 | 38 | /* 39 | * Trivial typedef to ensure that ZipEntryCRO is not treated as a simple 40 | * integer. We use NULL to indicate an invalid value. 41 | */ 42 | typedef void* ZipEntryCRO; 43 | 44 | extern ZipFileCRO ZipFileXRO_open(const char* path); 45 | 46 | extern void ZipFileCRO_destroy(ZipFileCRO zip); 47 | 48 | extern ZipEntryCRO ZipFileCRO_findEntryByName(ZipFileCRO zip, 49 | const char* fileName); 50 | 51 | extern bool ZipFileCRO_getEntryInfo(ZipFileCRO zip, ZipEntryCRO entry, 52 | int* pMethod, size_t* pUncompLen, 53 | size_t* pCompLen, off64_t* pOffset, long* pModWhen, long* pCrc32); 54 | 55 | extern bool ZipFileCRO_uncompressEntry(ZipFileCRO zip, ZipEntryCRO entry, int fd); 56 | 57 | #ifdef __cplusplus 58 | } 59 | #endif 60 | 61 | #endif /*__LIBS_ZIPFILECRO_H*/ 62 | -------------------------------------------------------------------------------- /utils-cpp/ZipFileCRO.cpp: -------------------------------------------------------------------------------- 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 | #include 19 | 20 | using namespace android; 21 | 22 | ZipFileCRO ZipFileXRO_open(const char* path) { 23 | ZipFileRO* zip = new ZipFileRO(); 24 | if (zip->open(path) == NO_ERROR) { 25 | return (ZipFileCRO)zip; 26 | } 27 | return NULL; 28 | } 29 | 30 | void ZipFileCRO_destroy(ZipFileCRO zipToken) { 31 | ZipFileRO* zip = (ZipFileRO*)zipToken; 32 | delete zip; 33 | } 34 | 35 | ZipEntryCRO ZipFileCRO_findEntryByName(ZipFileCRO zipToken, 36 | const char* fileName) { 37 | ZipFileRO* zip = (ZipFileRO*)zipToken; 38 | return (ZipEntryCRO)zip->findEntryByName(fileName); 39 | } 40 | 41 | bool ZipFileCRO_getEntryInfo(ZipFileCRO zipToken, ZipEntryRO entryToken, 42 | int* pMethod, size_t* pUncompLen, 43 | size_t* pCompLen, off64_t* pOffset, long* pModWhen, long* pCrc32) { 44 | ZipFileRO* zip = (ZipFileRO*)zipToken; 45 | ZipEntryRO entry = (ZipEntryRO)entryToken; 46 | return zip->getEntryInfo(entry, pMethod, pUncompLen, pCompLen, pOffset, 47 | pModWhen, pCrc32); 48 | } 49 | 50 | bool ZipFileCRO_uncompressEntry(ZipFileCRO zipToken, ZipEntryRO entryToken, int fd) { 51 | ZipFileRO* zip = (ZipFileRO*)zipToken; 52 | ZipEntryRO entry = (ZipEntryRO)entryToken; 53 | return zip->uncompressEntry(entry, fd); 54 | } 55 | -------------------------------------------------------------------------------- /utils-cpp/AssetDir.cpp: -------------------------------------------------------------------------------- 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 | // Provide access to a virtual directory in "asset space". Most of the 19 | // implementation is in the header file or in friend functions in 20 | // AssetManager. 21 | // 22 | #include 23 | 24 | using namespace android; 25 | 26 | 27 | /* 28 | * Find a matching entry in a vector of FileInfo. Because it's sorted, we 29 | * can use a binary search. 30 | * 31 | * Assumes the vector is sorted in ascending order. 32 | */ 33 | /*static*/ int AssetDir::FileInfo::findEntry(const SortedVector* pVector, 34 | const String8& fileName) 35 | { 36 | FileInfo tmpInfo; 37 | 38 | tmpInfo.setFileName(fileName); 39 | return pVector->indexOf(tmpInfo); 40 | 41 | #if 0 // don't need this after all (uses 1/2 compares of SortedVector though) 42 | int lo, hi, cur; 43 | 44 | lo = 0; 45 | hi = pVector->size() -1; 46 | while (lo <= hi) { 47 | int cmp; 48 | 49 | cur = (hi + lo) / 2; 50 | cmp = strcmp(pVector->itemAt(cur).getFileName(), fileName); 51 | if (cmp == 0) { 52 | /* match, bail */ 53 | return cur; 54 | } else if (cmp < 0) { 55 | /* too low */ 56 | lo = cur + 1; 57 | } else { 58 | /* too high */ 59 | hi = cur -1; 60 | } 61 | } 62 | 63 | return -1; 64 | #endif 65 | } 66 | 67 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /Main.h: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright 2006 The Android Open Source Project 3 | // 4 | // Some global defines that don't really merit their own header. 5 | // 6 | #ifndef __MAIN_H 7 | #define __MAIN_H 8 | 9 | #include "fakeLog.h" /*#include */ 10 | #include 11 | #include 12 | #include 13 | #include "Bundle.h" 14 | #include "AaptAssets.h" 15 | #include "ZipFile.h" 16 | 17 | #include "preDef.h" 18 | 19 | /* Benchmarking Flag */ 20 | //#define BENCHMARK 1 21 | 22 | #if BENCHMARK 23 | #include 24 | #endif /* BENCHMARK */ 25 | 26 | extern int doVersion(Bundle* bundle); 27 | extern int doList(Bundle* bundle); 28 | extern int doDump(Bundle* bundle); 29 | extern int doAdd(Bundle* bundle); 30 | extern int doRemove(Bundle* bundle); 31 | extern int doPackage(Bundle* bundle); 32 | extern int doCrunch(Bundle* bundle); 33 | 34 | extern int calcPercent(long uncompressedLen, long compressedLen); 35 | 36 | extern android::status_t writeAPK(Bundle* bundle, 37 | const sp& assets, 38 | const android::String8& outputFile); 39 | 40 | extern android::status_t updatePreProcessedCache(Bundle* bundle); 41 | 42 | extern android::status_t buildResources(Bundle* bundle, 43 | const sp& assets); 44 | 45 | extern android::status_t writeResourceSymbols(Bundle* bundle, 46 | const sp& assets, const String8& pkgName, bool includePrivate); 47 | 48 | extern android::status_t writeProguardFile(Bundle* bundle, const sp& assets); 49 | 50 | extern bool isValidResourceType(const String8& type); 51 | 52 | ssize_t processAssets(Bundle* bundle, ZipFile* zip, const sp& assets); 53 | 54 | extern status_t filterResources(Bundle* bundle, const sp& assets); 55 | 56 | int dumpResources(Bundle* bundle); 57 | 58 | String8 getAttribute(const ResXMLTree& tree, const char* ns, 59 | const char* attr, String8* outError); 60 | 61 | status_t writeDependencyPreReqs(Bundle* bundle, const sp& assets, 62 | FILE* fp, bool includeRaw); 63 | #endif // __MAIN_H 64 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | aapt-build 2 | ========== 3 | this work is unfinished. 4 | 5 | 看到有fork,所以觉得应该把README写清楚,以下是当初在Window下的尝试过程: 6 | 7 | 一、准备 8 | 1、获取源码。android开放源码存放在git库中: https://android.googlesource.com/ 9 | 选择项目 platform/frameworks/base 10 | 选择 TAG android-4.4.4_r2.0.1(最新的tag) 11 | 寻找aapt tools/aapt/ 12 | 2、搭建编译环境 13 | 当前开发环境为 Windows + Eclipse(in ADT), 要支持C++编译,还需要安装插件和编译器。 14 | >在Eclipse中安装CDT(C/C++ Development Toolkit)插件; 15 | >安装MinGW. 16 | 17 | 二、解决依赖问题 18 | 在Eclipse中用向导新建C++工程,导入源码。在CDT语法分析之后,报告出一些错误。出现大批语法错误和多处引用头文件找不到:#include \ (其中\*\*\*泛指头文件名)。可见aapt源码要依赖其他的工具或者库,不能独立编译。那么,去找这个utils吧。 19 | 还是去源码库,aapt/目录下没有,遍历父目录tools/,也没有。再上一层,到了根目录。这时有个子目录映入眼帘:include/ ,应该是它了,进去看看,没有!那么,搜索吧,搜索...结果还是没有,整个源码集中都找不到,那这个utils/是何方神圣?是个什么库吗?google之。 20 | 搜索发现,在早期版本的源码中是存在utils/这个目录的,是从哪个TAG开始丢掉了呢?从4.4.4往前一个个翻出来看吧。没有,没有,...,有了!版本: 4.0.1_r1 。选定这个TAG,获取到 tools/aapt/ 和 include/utils/ 。 21 | 工程清理,重新导入源码。嗯,现在utils又出现大批语法错误和多处引用头文件找不到。其中有#include \ 22 | ,去找cutils/ 。在platform/frameworks/base中找了好久,最终发现是在另外一个项目中:platform/system/core/./include/cutils/ .嗯,好了,找到,添加进去,检查,坏了,cutils也要报错。针对报错的文件,进行以下处理: 23 | >没有被引用到的,删掉; 24 | >宏定义错误的,修改宏定义; 25 | >类型、常量、方法找不到的,注释掉; 26 | 解决一大堆编译红叉之后,变得稍微有些条理。 27 | cutils(.h & .cpp)依赖的头文件缺少: 28 | sys/cdefs.h 29 | sys/socket.h 30 | sys/ioctl.h 31 | sys/mount.h 32 | sys/wait.h 33 | netinet/in.h 34 | machine/cpu-features.h 35 | 36 | utils(.h & .cpp)依赖的头文件缺少: 37 | linux/ioctl.h 38 | linux/limits.h 39 | netinet/in.h 40 | android/looper.h 41 | android/configuration.h 42 | sys/epoll.h 43 | sys/poll.h 44 | sys/uio.h 45 | zlib.h 46 | system/graphics.h 47 | private/utils/Static.h 48 | 49 | aapt源码依赖的头文件缺少: 50 | png.h 51 | zipfile/zipfile.h 52 | expat.h 53 | host/pseudolocalize.h 54 | zlib.h 55 | 56 | 接下来,就把这些头文件一个个找出来补充上。 57 | 先找 private/utils/Static.h,因为刚才在找别的文件时,好像见过它。果然,就在utils同级目录中发现private,把它添加到工程中,一个搞定。 58 | 然后 system/graphics.h,在system/core/./include/中。 59 | 然后 zipfile/zipfile.h,在system/core/./include/中。 60 | 然后 android/looper.h ,在frameworks/base/native/include/中。 61 | 62 | 还有linux/,netinet/等,这些是Linux平台的底层库,参考了 http://winuxxan.blog.51cto.com/2779763/502340 ,看来还是要在Linux平台来做交叉编译了,路还很长啊。 63 | 64 | 65 | 【To Be Continued】 66 | 67 | -------------------------------------------------------------------------------- /utils/Flattenable.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_UTILS_FLATTENABLE_H 18 | #define ANDROID_UTILS_FLATTENABLE_H 19 | 20 | 21 | #include 22 | #include 23 | #include 24 | 25 | namespace android { 26 | 27 | class Flattenable 28 | { 29 | public: 30 | // size in bytes of the flattened object 31 | virtual size_t getFlattenedSize() const = 0; 32 | 33 | // number of file descriptors to flatten 34 | virtual size_t getFdCount() const = 0; 35 | 36 | // flattens the object into buffer. 37 | // size should be at least of getFlattenedSize() 38 | // file descriptors are written in the fds[] array but ownership is 39 | // not transfered (ie: they must be dupped by the caller of 40 | // flatten() if needed). 41 | virtual status_t flatten(void* buffer, size_t size, 42 | int fds[], size_t count) const = 0; 43 | 44 | // unflattens the object from buffer. 45 | // size should be equal to the value of getFlattenedSize() when the 46 | // object was flattened. 47 | // unflattened file descriptors are found in the fds[] array and 48 | // don't need to be dupped(). ie: the caller of unflatten doesn't 49 | // keep ownership. If a fd is not retained by unflatten() it must be 50 | // explicitly closed. 51 | virtual status_t unflatten(void const* buffer, size_t size, 52 | int fds[], size_t count) = 0; 53 | 54 | protected: 55 | virtual ~Flattenable() = 0; 56 | 57 | }; 58 | 59 | }; // namespace android 60 | 61 | 62 | #endif /* ANDROID_UTILS_FLATTENABLE_H */ 63 | -------------------------------------------------------------------------------- /utils/LinearTransform.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 _LIBS_UTILS_LINEAR_TRANSFORM_H 18 | #define _LIBS_UTILS_LINEAR_TRANSFORM_H 19 | 20 | #include 21 | 22 | namespace android { 23 | 24 | // LinearTransform defines a structure which hold the definition of a 25 | // transformation from single dimensional coordinate system A into coordinate 26 | // system B (and back again). Values in A and in B are 64 bit, the linear 27 | // scale factor is expressed as a rational number using two 32 bit values. 28 | // 29 | // Specifically, let 30 | // f(a) = b 31 | // F(b) = f^-1(b) = a 32 | // then 33 | // 34 | // f(a) = (((a - a_zero) * a_to_b_numer) / a_to_b_denom) + b_zero; 35 | // 36 | // and 37 | // 38 | // F(b) = (((b - b_zero) * a_to_b_denom) / a_to_b_numer) + a_zero; 39 | // 40 | struct LinearTransform { 41 | int64_t a_zero; 42 | int64_t b_zero; 43 | int32_t a_to_b_numer; 44 | uint32_t a_to_b_denom; 45 | 46 | // Transform from A->B 47 | // Returns true on success, or false in the case of a singularity or an 48 | // overflow. 49 | bool doForwardTransform(int64_t a_in, int64_t* b_out) const; 50 | 51 | // Transform from B->A 52 | // Returns true on success, or false in the case of a singularity or an 53 | // overflow. 54 | bool doReverseTransform(int64_t b_in, int64_t* a_out) const; 55 | 56 | // Helpers which will reduce the fraction N/D using Euclid's method. 57 | template static void reduce(T* N, T* D); 58 | static void reduce(int32_t* N, uint32_t* D); 59 | }; 60 | 61 | 62 | } 63 | 64 | #endif // _LIBS_UTILS_LINEAR_TRANSFORM_H 65 | -------------------------------------------------------------------------------- /utils/BufferedTextOutput.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_BUFFEREDTEXTOUTPUT_H 18 | #define ANDROID_BUFFEREDTEXTOUTPUT_H 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | // --------------------------------------------------------------------------- 25 | namespace android { 26 | 27 | class BufferedTextOutput : public TextOutput 28 | { 29 | public: 30 | //** Flags for constructor */ 31 | enum { 32 | MULTITHREADED = 0x0001 33 | }; 34 | 35 | BufferedTextOutput(uint32_t flags = 0); 36 | virtual ~BufferedTextOutput(); 37 | 38 | virtual status_t print(const char* txt, size_t len); 39 | virtual void moveIndent(int delta); 40 | 41 | virtual void pushBundle(); 42 | virtual void popBundle(); 43 | 44 | protected: 45 | virtual status_t writeLines(const struct iovec& vec, size_t N) = 0; 46 | 47 | private: 48 | struct BufferState; 49 | struct ThreadState; 50 | 51 | static ThreadState*getThreadState(); 52 | static void threadDestructor(void *st); 53 | 54 | BufferState*getBuffer() const; 55 | 56 | uint32_t mFlags; 57 | const int32_t mSeq; 58 | const int32_t mIndex; 59 | 60 | Mutex mLock; 61 | BufferState* mGlobalState; 62 | }; 63 | 64 | // --------------------------------------------------------------------------- 65 | }; // namespace android 66 | 67 | #endif // ANDROID_BUFFEREDTEXTOUTPUT_H 68 | -------------------------------------------------------------------------------- /cutils-c/uio.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 | #ifndef HAVE_SYS_UIO_H 18 | 19 | #include 20 | #include 21 | 22 | int readv( int fd, struct iovec* vecs, int count ) 23 | { 24 | int total = 0; 25 | 26 | for ( ; count > 0; count--, vecs++ ) { 27 | const char* buf = vecs->iov_base; 28 | int len = vecs->iov_len; 29 | 30 | while (len > 0) { 31 | int ret = read( fd, buf, len ); 32 | if (ret < 0) { 33 | if (total == 0) 34 | total = -1; 35 | goto Exit; 36 | } 37 | if (ret == 0) 38 | goto Exit; 39 | 40 | total += ret; 41 | buf += ret; 42 | len -= ret; 43 | } 44 | } 45 | Exit: 46 | return total; 47 | } 48 | 49 | int writev( int fd, const struct iovec* vecs, int count ) 50 | { 51 | int total = 0; 52 | 53 | for ( ; count > 0; count--, vecs++ ) { 54 | const char* buf = (const char*)vecs->iov_base; 55 | int len = (int)vecs->iov_len; 56 | 57 | while (len > 0) { 58 | int ret = write( fd, buf, len ); 59 | if (ret < 0) { 60 | if (total == 0) 61 | total = -1; 62 | goto Exit; 63 | } 64 | if (ret == 0) 65 | goto Exit; 66 | 67 | total += ret; 68 | buf += ret; 69 | len -= ret; 70 | } 71 | } 72 | Exit: 73 | return total; 74 | } 75 | 76 | #endif /* !HAVE_SYS_UIO_H */ 77 | -------------------------------------------------------------------------------- /utils/StringArray.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 | // 18 | // Sortable array of strings. STL-ish, but STL-free. 19 | // 20 | #ifndef _LIBS_UTILS_STRING_ARRAY_H 21 | #define _LIBS_UTILS_STRING_ARRAY_H 22 | 23 | #include 24 | #include 25 | 26 | namespace android { 27 | 28 | // 29 | // An expanding array of strings. Add, get, sort, delete. 30 | // 31 | class StringArray { 32 | public: 33 | StringArray(); 34 | virtual ~StringArray(); 35 | 36 | // 37 | // Add a string. A copy of the string is made. 38 | // 39 | bool push_back(const char* str); 40 | 41 | // 42 | // Delete an entry. 43 | // 44 | void erase(int idx); 45 | 46 | // 47 | // Sort the array. 48 | // 49 | void sort(int (*compare)(const void*, const void*)); 50 | 51 | // 52 | // Pass this to the sort routine to do an ascending alphabetical sort. 53 | // 54 | static int cmpAscendingAlpha(const void* pstr1, const void* pstr2); 55 | 56 | // 57 | // Get the #of items in the array. 58 | // 59 | inline int size(void) const { return mCurrent; } 60 | 61 | // 62 | // Return entry N. 63 | // [should use operator[] here] 64 | // 65 | const char* getEntry(int idx) const { 66 | return (unsigned(idx) >= unsigned(mCurrent)) ? NULL : mArray[idx]; 67 | } 68 | 69 | // 70 | // Set entry N to specified string. 71 | // [should use operator[] here] 72 | // 73 | void setEntry(int idx, const char* str); 74 | 75 | private: 76 | int mMax; 77 | int mCurrent; 78 | char** mArray; 79 | }; 80 | 81 | }; // namespace android 82 | 83 | #endif // _LIBS_UTILS_STRING_ARRAY_H 84 | -------------------------------------------------------------------------------- /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(__sh__) 51 | /* implementation is in atomic-android-sh.c */ 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 | -------------------------------------------------------------------------------- /utils/CallStack.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_CALLSTACK_H 18 | #define ANDROID_CALLSTACK_H 19 | 20 | #include 21 | #include 22 | 23 | #include 24 | 25 | // --------------------------------------------------------------------------- 26 | 27 | namespace android { 28 | 29 | class CallStack 30 | { 31 | public: 32 | enum { 33 | MAX_DEPTH = 31 34 | }; 35 | 36 | CallStack(); 37 | CallStack(const CallStack& rhs); 38 | ~CallStack(); 39 | 40 | CallStack& operator = (const CallStack& rhs); 41 | 42 | bool operator == (const CallStack& rhs) const; 43 | bool operator != (const CallStack& rhs) const; 44 | bool operator < (const CallStack& rhs) const; 45 | bool operator >= (const CallStack& rhs) const; 46 | bool operator > (const CallStack& rhs) const; 47 | bool operator <= (const CallStack& rhs) const; 48 | 49 | const void* operator [] (int index) const; 50 | 51 | void clear(); 52 | 53 | void update(int32_t ignoreDepth=1, int32_t maxDepth=MAX_DEPTH); 54 | 55 | // Dump a stack trace to the log 56 | void dump(const char* prefix = 0) const; 57 | 58 | // Return a string (possibly very long) containing the complete stack trace 59 | String8 toString(const char* prefix = 0) const; 60 | 61 | size_t size() const { return mCount; } 62 | 63 | private: 64 | // Internal helper function 65 | String8 toStringSingleLevel(const char* prefix, int32_t level) const; 66 | 67 | size_t mCount; 68 | const void* mStack[MAX_DEPTH]; 69 | }; 70 | 71 | }; // namespace android 72 | 73 | 74 | // --------------------------------------------------------------------------- 75 | 76 | #endif // ANDROID_CALLSTACK_H 77 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /utils/ZipUtils.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 | // Miscellaneous zip/gzip utility functions. 19 | // 20 | #ifndef __LIBS_ZIPUTILS_H 21 | #define __LIBS_ZIPUTILS_H 22 | 23 | #include 24 | 25 | namespace android { 26 | 27 | /* 28 | * Container class for utility functions, primarily for namespace reasons. 29 | */ 30 | class ZipUtils { 31 | public: 32 | /* 33 | * General utility function for uncompressing "deflate" data from a file 34 | * to a buffer. 35 | */ 36 | static bool inflateToBuffer(int fd, void* buf, long uncompressedLen, 37 | long compressedLen); 38 | static bool inflateToBuffer(FILE* fp, void* buf, long uncompressedLen, 39 | long compressedLen); 40 | 41 | /* 42 | * Someday we might want to make this generic and handle bzip2 ".bz2" 43 | * files too. 44 | * 45 | * We could declare gzip to be a sub-class of zip that has exactly 46 | * one always-compressed entry, but we currently want to treat Zip 47 | * and gzip as distinct, so there's no value. 48 | * 49 | * The zlib library has some gzip utilities, but it has no interface 50 | * for extracting the uncompressed length of the file (you do *not* 51 | * want to gzseek to the end). 52 | * 53 | * Pass in a seeked file pointer for the gzip file. If this is a gzip 54 | * file, we set our return values appropriately and return "true" with 55 | * the file seeked to the start of the compressed data. 56 | */ 57 | static bool examineGzip(FILE* fp, int* pCompressionMethod, 58 | long* pUncompressedLen, long* pCompressedLen, unsigned long* pCRC32); 59 | 60 | private: 61 | ZipUtils() {} 62 | ~ZipUtils() {} 63 | }; 64 | 65 | }; // namespace android 66 | 67 | #endif /*__LIBS_ZIPUTILS_H*/ 68 | -------------------------------------------------------------------------------- /utils-cpp/StopWatch.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005 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 | #define LOG_TAG "StopWatch" 18 | 19 | #include 20 | #include 21 | #include 22 | 23 | //#include 24 | #include "fakeLog.h" 25 | 26 | #include 27 | #include 28 | 29 | /*****************************************************************************/ 30 | 31 | namespace android { 32 | 33 | 34 | StopWatch::StopWatch(const char *name, int clock, uint32_t flags) 35 | : mName(name), mClock(clock), mFlags(flags) 36 | { 37 | reset(); 38 | } 39 | 40 | StopWatch::~StopWatch() 41 | { 42 | nsecs_t elapsed = elapsedTime(); 43 | const int n = mNumLaps; 44 | LOGD("StopWatch %s (us): %lld ", mName, ns2us(elapsed)); 45 | for (int i=0 ; i= 8) { 61 | elapsed = 0; 62 | } else { 63 | const int n = mNumLaps; 64 | mLaps[n].soFar = elapsed; 65 | mLaps[n].thisLap = n ? (elapsed - mLaps[n-1].soFar) : elapsed; 66 | mNumLaps = n+1; 67 | } 68 | return elapsed; 69 | } 70 | 71 | nsecs_t StopWatch::elapsedTime() const 72 | { 73 | return systemTime(mClock) - mStartTime; 74 | } 75 | 76 | void StopWatch::reset() 77 | { 78 | mNumLaps = 0; 79 | mStartTime = systemTime(mClock); 80 | } 81 | 82 | 83 | /*****************************************************************************/ 84 | 85 | }; // namespace android 86 | 87 | -------------------------------------------------------------------------------- /utils/Debug.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005 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_DEBUG_H 18 | #define ANDROID_DEBUG_H 19 | 20 | #include 21 | #include 22 | 23 | namespace android { 24 | // --------------------------------------------------------------------------- 25 | 26 | #ifdef __cplusplus 27 | template struct CompileTimeAssert; 28 | template<> struct CompileTimeAssert {}; 29 | #define COMPILE_TIME_ASSERT(_exp) \ 30 | template class CompileTimeAssert< (_exp) >; 31 | #endif 32 | #define COMPILE_TIME_ASSERT_FUNCTION_SCOPE(_exp) \ 33 | CompileTimeAssert<( _exp )>(); 34 | 35 | // --------------------------------------------------------------------------- 36 | 37 | #ifdef __cplusplus 38 | template struct CompileTimeIfElse; 39 | template 40 | struct CompileTimeIfElse { typedef LHS TYPE; }; 41 | template 42 | struct CompileTimeIfElse { typedef RHS TYPE; }; 43 | #endif 44 | 45 | // --------------------------------------------------------------------------- 46 | 47 | #ifdef __cplusplus 48 | extern "C" { 49 | #endif 50 | 51 | const char* stringForIndent(int32_t indentLevel); 52 | 53 | typedef void (*debugPrintFunc)(void* cookie, const char* txt); 54 | 55 | void printTypeCode(uint32_t typeCode, 56 | debugPrintFunc func = 0, void* cookie = 0); 57 | 58 | void printHexData(int32_t indent, const void *buf, size_t length, 59 | size_t bytesPerLine=16, int32_t singleLineBytesCutoff=16, 60 | size_t alignment=0, bool cArrayStyle=false, 61 | debugPrintFunc func = 0, void* cookie = 0); 62 | 63 | #ifdef __cplusplus 64 | } 65 | #endif 66 | 67 | // --------------------------------------------------------------------------- 68 | }; // namespace android 69 | 70 | #endif // ANDROID_DEBUG_H 71 | -------------------------------------------------------------------------------- /utils/ByteOrder.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 | 19 | #ifndef _LIBS_UTILS_BYTE_ORDER_H 20 | #define _LIBS_UTILS_BYTE_ORDER_H 21 | 22 | #include 23 | #include 24 | #ifdef HAVE_WINSOCK 25 | #include 26 | #else 27 | #include 28 | #endif 29 | 30 | /* 31 | * These macros are like the hton/ntoh byte swapping macros, 32 | * except they allow you to swap to and from the "device" byte 33 | * order. The device byte order is the endianness of the target 34 | * device -- for the ARM CPUs we use today, this is little endian. 35 | * 36 | * Note that the byte swapping functions have not been optimized 37 | * much; performance is currently not an issue for them since the 38 | * intent is to allow us to avoid byte swapping on the device. 39 | */ 40 | 41 | static inline uint32_t android_swap_long(uint32_t v) 42 | { 43 | return (v<<24) | ((v<<8)&0x00FF0000) | ((v>>8)&0x0000FF00) | (v>>24); 44 | } 45 | 46 | static inline uint16_t android_swap_short(uint16_t v) 47 | { 48 | return (v<<8) | (v>>8); 49 | } 50 | 51 | #define DEVICE_BYTE_ORDER LITTLE_ENDIAN 52 | 53 | #if BYTE_ORDER == DEVICE_BYTE_ORDER 54 | 55 | #define dtohl(x) (x) 56 | #define dtohs(x) (x) 57 | #define htodl(x) (x) 58 | #define htods(x) (x) 59 | 60 | #else 61 | 62 | #define dtohl(x) (android_swap_long(x)) 63 | #define dtohs(x) (android_swap_short(x)) 64 | #define htodl(x) (android_swap_long(x)) 65 | #define htods(x) (android_swap_short(x)) 66 | 67 | #endif 68 | 69 | #if BYTE_ORDER == LITTLE_ENDIAN 70 | #define fromlel(x) (x) 71 | #define fromles(x) (x) 72 | #define tolel(x) (x) 73 | #define toles(x) (x) 74 | #else 75 | #define fromlel(x) (android_swap_long(x)) 76 | #define fromles(x) (android_swap_short(x)) 77 | #define tolel(x) (android_swap_long(x)) 78 | #define toles(x) (android_swap_short(x)) 79 | #endif 80 | 81 | #endif // _LIBS_UTILS_BYTE_ORDER_H 82 | -------------------------------------------------------------------------------- /utils/Singleton.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_UTILS_SINGLETON_H 18 | #define ANDROID_UTILS_SINGLETON_H 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | namespace android { 25 | // --------------------------------------------------------------------------- 26 | 27 | template 28 | class Singleton 29 | { 30 | public: 31 | static TYPE& getInstance() { 32 | Mutex::Autolock _l(sLock); 33 | TYPE* instance = sInstance; 34 | if (instance == 0) { 35 | instance = new TYPE(); 36 | sInstance = instance; 37 | } 38 | return *instance; 39 | } 40 | 41 | static bool hasInstance() { 42 | Mutex::Autolock _l(sLock); 43 | return sInstance != 0; 44 | } 45 | 46 | protected: 47 | ~Singleton() { }; 48 | Singleton() { }; 49 | 50 | private: 51 | Singleton(const Singleton&); 52 | Singleton& operator = (const Singleton&); 53 | static Mutex sLock; 54 | static TYPE* sInstance; 55 | }; 56 | 57 | /* 58 | * use ANDROID_SINGLETON_STATIC_INSTANCE(TYPE) in your implementation file 59 | * (eg: .cpp) to create the static instance of Singleton<>'s attributes, 60 | * and avoid to have a copy of them in each compilation units Singleton 61 | * is used. 62 | * NOTE: we use a version of Mutex ctor that takes a parameter, because 63 | * for some unknown reason using the default ctor doesn't emit the variable! 64 | */ 65 | 66 | #define ANDROID_SINGLETON_STATIC_INSTANCE(TYPE) \ 67 | template class Singleton< TYPE >; \ 68 | template<> Mutex Singleton< TYPE >::sLock(Mutex::PRIVATE); \ 69 | template<> TYPE* Singleton< TYPE >::sInstance(0); 70 | 71 | 72 | // --------------------------------------------------------------------------- 73 | }; // namespace android 74 | 75 | #endif // ANDROID_UTILS_SINGLETON_H 76 | 77 | -------------------------------------------------------------------------------- /utils-cpp/Static.cpp: -------------------------------------------------------------------------------- 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 | // All static variables go here, to control initialization and 18 | // destruction order in the library. 19 | 20 | #include 21 | 22 | #include 23 | //#include 24 | #include "fakeLog.h" 25 | 26 | #include 27 | 28 | namespace android { 29 | 30 | class LibUtilsFirstStatics 31 | { 32 | public: 33 | LibUtilsFirstStatics() 34 | { 35 | initialize_string8(); 36 | initialize_string16(); 37 | } 38 | 39 | ~LibUtilsFirstStatics() 40 | { 41 | terminate_string16(); 42 | terminate_string8(); 43 | } 44 | }; 45 | 46 | static LibUtilsFirstStatics gFirstStatics; 47 | int gDarwinCantLoadAllObjects = 1; 48 | 49 | // ------------ Text output streams 50 | 51 | Vector gTextBuffers; 52 | 53 | class LogTextOutput : public BufferedTextOutput 54 | { 55 | public: 56 | LogTextOutput() : BufferedTextOutput(MULTITHREADED) { } 57 | virtual ~LogTextOutput() { }; 58 | 59 | protected: 60 | virtual status_t writeLines(const struct iovec& vec, size_t N) 61 | { 62 | //android_writevLog(&vec, N); <-- this is now a no-op 63 | if (N != 1) LOGI("WARNING: writeLines N=%d\n", N); 64 | LOGI("%.*s", vec.iov_len, (const char*) vec.iov_base); 65 | return NO_ERROR; 66 | } 67 | }; 68 | 69 | class FdTextOutput : public BufferedTextOutput 70 | { 71 | public: 72 | FdTextOutput(int fd) : BufferedTextOutput(MULTITHREADED), mFD(fd) { } 73 | virtual ~FdTextOutput() { }; 74 | 75 | protected: 76 | virtual status_t writeLines(const struct iovec& vec, size_t N) 77 | { 78 | writev(mFD, &vec, N); 79 | return NO_ERROR; 80 | } 81 | 82 | private: 83 | int mFD; 84 | }; 85 | 86 | static LogTextOutput gLogTextOutput; 87 | static FdTextOutput gStdoutTextOutput(STDOUT_FILENO); 88 | static FdTextOutput gStderrTextOutput(STDERR_FILENO); 89 | 90 | TextOutput& alog(gLogTextOutput); 91 | TextOutput& aout(gStdoutTextOutput); 92 | TextOutput& aerr(gStderrTextOutput); 93 | 94 | } // namespace android 95 | -------------------------------------------------------------------------------- /cutils-c/threads.c: -------------------------------------------------------------------------------- 1 | /* libs/cutils/threads.c 2 | ** 3 | ** Copyright (C) 2007, 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 | #include 18 | #include 19 | 20 | #ifdef HAVE_PTHREADS 21 | void* thread_store_get( thread_store_t* store ) 22 | { 23 | const pthread_key_t k = store->tls; 24 | 25 | if (!store->has_tls) 26 | return NULL; 27 | 28 | return pthread_getspecific( store->tls ); 29 | } 30 | 31 | extern void thread_store_set( thread_store_t* store, 32 | void* value, 33 | thread_store_destruct_t destroy) 34 | { 35 | pthread_mutex_lock( &store->lock ); 36 | if (!store->has_tls) { 37 | if (pthread_key_create( &store->tls, destroy) != 0) { 38 | pthread_mutex_unlock(&store->lock); 39 | return; 40 | } 41 | store->has_tls = 1; 42 | } 43 | pthread_mutex_unlock( &store->lock ); 44 | 45 | pthread_setspecific( store->tls, value ); 46 | } 47 | 48 | #endif 49 | 50 | #ifdef HAVE_WIN32_THREADS 51 | void* thread_store_get( thread_store_t* store ) 52 | { 53 | if (!store->has_tls) 54 | return NULL; 55 | 56 | return (void*) TlsGetValue( store->tls ); 57 | } 58 | 59 | void thread_store_set( thread_store_t* store, 60 | void* value, 61 | thread_store_destruct_t destroy ) 62 | { 63 | /* XXX: can't use destructor on thread exit */ 64 | if (!store->lock_init) { 65 | store->lock_init = -1; 66 | InitializeCriticalSection( &store->lock ); 67 | store->lock_init = -2; 68 | } else while (store->lock_init != -2) { 69 | Sleep(10); /* 10ms */ 70 | } 71 | 72 | EnterCriticalSection( &store->lock ); 73 | if (!store->has_tls) { 74 | store->tls = TlsAlloc(); 75 | if (store->tls == TLS_OUT_OF_INDEXES) { 76 | LeaveCriticalSection( &store->lock ); 77 | return; 78 | } 79 | store->has_tls = 1; 80 | } 81 | LeaveCriticalSection( &store->lock ); 82 | 83 | TlsSetValue( store->tls, value ); 84 | } 85 | #endif 86 | -------------------------------------------------------------------------------- /utils/Errors.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_ERRORS_H 18 | #define ANDROID_ERRORS_H 19 | 20 | #include 21 | #include 22 | 23 | namespace android { 24 | 25 | // use this type to return error codes 26 | #ifdef HAVE_MS_C_RUNTIME 27 | typedef int status_t; 28 | #else 29 | typedef int32_t status_t; 30 | #endif 31 | 32 | /* the MS C runtime lacks a few error codes */ 33 | 34 | /* 35 | * Error codes. 36 | * All error codes are negative values. 37 | */ 38 | 39 | // Win32 #defines NO_ERROR as well. It has the same value, so there's no 40 | // real conflict, though it's a bit awkward. 41 | #ifdef _WIN32 42 | # undef NO_ERROR 43 | #endif 44 | 45 | enum { 46 | OK = 0, // Everything's swell. 47 | NO_ERROR = 0, // No errors. 48 | 49 | UNKNOWN_ERROR = 0x80000000, 50 | 51 | NO_MEMORY = -ENOMEM, 52 | INVALID_OPERATION = -ENOSYS, 53 | BAD_VALUE = -EINVAL, 54 | BAD_TYPE = 0x80000001, 55 | NAME_NOT_FOUND = -ENOENT, 56 | PERMISSION_DENIED = -EPERM, 57 | NO_INIT = -ENODEV, 58 | ALREADY_EXISTS = -EEXIST, 59 | DEAD_OBJECT = -EPIPE, 60 | FAILED_TRANSACTION = 0x80000002, 61 | JPARKS_BROKE_IT = -EPIPE, 62 | #if !defined(HAVE_MS_C_RUNTIME) 63 | BAD_INDEX = -EOVERFLOW, 64 | NOT_ENOUGH_DATA = -ENODATA, 65 | WOULD_BLOCK = -EWOULDBLOCK, 66 | TIMED_OUT = -ETIMEDOUT, 67 | UNKNOWN_TRANSACTION = -EBADMSG, 68 | #else 69 | BAD_INDEX = -E2BIG, 70 | NOT_ENOUGH_DATA = 0x80000003, 71 | WOULD_BLOCK = 0x80000004, 72 | TIMED_OUT = 0x80000005, 73 | UNKNOWN_TRANSACTION = 0x80000006, 74 | #endif 75 | FDS_NOT_ALLOWED = 0x80000007, 76 | }; 77 | 78 | // Restore define; enumeration is in "android" namespace, so the value defined 79 | // there won't work for Win32 code in a different namespace. 80 | #ifdef _WIN32 81 | # define NO_ERROR 0L 82 | #endif 83 | 84 | }; // namespace android 85 | 86 | // --------------------------------------------------------------------------- 87 | 88 | #endif // ANDROID_ERRORS_H 89 | -------------------------------------------------------------------------------- /DirectoryWalker.h: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright 2011 The Android Open Source Project 3 | // 4 | // Defines an abstraction for opening a directory on the filesystem and 5 | // iterating through it. 6 | 7 | #ifndef DIRECTORYWALKER_H 8 | #define DIRECTORYWALKER_H 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | #include 18 | 19 | using namespace android; 20 | 21 | // Directory Walker 22 | // This is an abstraction for walking through a directory and getting files 23 | // and descriptions. 24 | 25 | class DirectoryWalker { 26 | public: 27 | virtual ~DirectoryWalker() {}; 28 | virtual bool openDir(String8 path) = 0; 29 | virtual bool openDir(const char* path) = 0; 30 | // Advance to next directory entry 31 | virtual struct dirent* nextEntry() = 0; 32 | // Get the stats for the current entry 33 | virtual struct stat* entryStats() = 0; 34 | // Clean Up 35 | virtual void closeDir() = 0; 36 | // This class is able to replicate itself on the heap 37 | virtual DirectoryWalker* clone() = 0; 38 | 39 | // DATA MEMBERS 40 | // Current directory entry 41 | struct dirent mEntry; 42 | // Stats for that directory entry 43 | struct stat mStats; 44 | // Base path 45 | String8 mBasePath; 46 | }; 47 | 48 | // System Directory Walker 49 | // This is an implementation of the above abstraction that calls 50 | // real system calls and is fully functional. 51 | // functions are inlined since they're very short and simple 52 | 53 | class SystemDirectoryWalker : public DirectoryWalker { 54 | 55 | // Default constructor, copy constructor, and destructor are fine 56 | public: 57 | virtual bool openDir(String8 path) { 58 | mBasePath = path; 59 | dir = NULL; 60 | dir = opendir(mBasePath.string() ); 61 | 62 | if (dir == NULL) 63 | return false; 64 | 65 | return true; 66 | }; 67 | virtual bool openDir(const char* path) { 68 | String8 p(path); 69 | openDir(p); 70 | return true; 71 | }; 72 | // Advance to next directory entry 73 | virtual struct dirent* nextEntry() { 74 | struct dirent* entryPtr = readdir(dir); 75 | if (entryPtr == NULL) 76 | return NULL; 77 | 78 | mEntry = *entryPtr; 79 | // Get stats 80 | String8 fullPath = mBasePath.appendPathCopy(mEntry.d_name); 81 | stat(fullPath.string(),&mStats); 82 | return &mEntry; 83 | }; 84 | // Get the stats for the current entry 85 | virtual struct stat* entryStats() { 86 | return &mStats; 87 | }; 88 | virtual void closeDir() { 89 | closedir(dir); 90 | }; 91 | virtual DirectoryWalker* clone() { 92 | return new SystemDirectoryWalker(*this); 93 | }; 94 | private: 95 | DIR* dir; 96 | }; 97 | 98 | #endif // DIRECTORYWALKER_H 99 | -------------------------------------------------------------------------------- /utils/misc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005 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 | // Handy utility functions and portability code. 19 | // 20 | #ifndef _LIBS_UTILS_MISC_H 21 | #define _LIBS_UTILS_MISC_H 22 | 23 | #include 24 | #include 25 | 26 | namespace android { 27 | 28 | /* get #of elements in a static array */ 29 | #ifndef NELEM 30 | # define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0]))) 31 | #endif 32 | 33 | /* 34 | * Make a copy of the string, using "new[]" instead of "malloc". Free the 35 | * string with delete[]. 36 | * 37 | * Returns NULL if "str" is NULL. 38 | */ 39 | char* strdupNew(const char* str); 40 | 41 | /* 42 | * Concatenate an argument vector into a single string. If argc is >= 0 43 | * it will be used; if it's < 0 then the last element in the arg vector 44 | * must be NULL. 45 | * 46 | * This inserts a space between each argument. 47 | * 48 | * This does not automatically add double quotes around arguments with 49 | * spaces in them. This practice is necessary for Win32, because Win32's 50 | * CreateProcess call is stupid. 51 | * 52 | * The caller should delete[] the returned string. 53 | */ 54 | char* concatArgv(int argc, const char* const argv[]); 55 | 56 | /* 57 | * Count up the number of arguments in "argv". The count does not include 58 | * the final NULL entry. 59 | */ 60 | int countArgv(const char* const argv[]); 61 | 62 | /* 63 | * Some utility functions for working with files. These could be made 64 | * part of a "File" class. 65 | */ 66 | typedef enum FileType { 67 | kFileTypeUnknown = 0, 68 | kFileTypeNonexistent, // i.e. ENOENT 69 | kFileTypeRegular, 70 | kFileTypeDirectory, 71 | kFileTypeCharDev, 72 | kFileTypeBlockDev, 73 | kFileTypeFifo, 74 | kFileTypeSymlink, 75 | kFileTypeSocket, 76 | } FileType; 77 | /* get the file's type; follows symlinks */ 78 | FileType getFileType(const char* fileName); 79 | /* get the file's modification date; returns -1 w/errno set on failure */ 80 | time_t getFileModDate(const char* fileName); 81 | 82 | /* 83 | * Round up to the nearest power of 2. Handy for hash tables. 84 | */ 85 | unsigned int roundUpPower2(unsigned int val); 86 | 87 | void strreverse(char* begin, char* end); 88 | void k_itoa(int value, char* str, int base); 89 | char* itoa(int val, int base); 90 | 91 | }; // namespace android 92 | 93 | #endif // _LIBS_UTILS_MISC_H 94 | -------------------------------------------------------------------------------- /FileFinder.h: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright 2011 The Android Open Source Project 3 | // 4 | 5 | // File Finder. 6 | // This is a collection of useful functions for finding paths and modification 7 | // times of files that match an extension pattern in a directory tree. 8 | // and finding files in it. 9 | 10 | #ifndef FILEFINDER_H 11 | #define FILEFINDER_H 12 | 13 | #include 14 | #include 15 | #include 16 | 17 | #include "DirectoryWalker.h" 18 | 19 | using namespace android; 20 | 21 | // Abstraction to allow for dependency injection. See MockFileFinder.h 22 | // for the testing implementation. 23 | class FileFinder { 24 | public: 25 | virtual bool findFiles(String8 basePath, Vector& extensions, 26 | KeyedVector& fileStore, 27 | DirectoryWalker* dw) = 0; 28 | 29 | virtual ~FileFinder() {}; 30 | }; 31 | 32 | class SystemFileFinder : public FileFinder { 33 | public: 34 | 35 | /* findFiles takes a path, a Vector of extensions, and a destination KeyedVector 36 | * and places path/modification date key/values pointing to 37 | * all files with matching extensions found into the KeyedVector 38 | * PRECONDITIONS 39 | * path is a valid system path 40 | * extensions should include leading "." 41 | * This is not necessary, but the comparison directly 42 | * compares the end of the path string so if the "." 43 | * is excluded there is a small chance you could have 44 | * a false positive match. (For example: extension "png" 45 | * would match a file called "blahblahpng") 46 | * 47 | * POSTCONDITIONS 48 | * fileStore contains (in no guaranteed order) paths to all 49 | * matching files encountered in subdirectories of path 50 | * as keys in the KeyedVector. Each key has the modification time 51 | * of the file as its value. 52 | * 53 | * Calls checkAndAddFile on each file encountered in the directory tree 54 | * Recursively descends into subdirectories. 55 | */ 56 | virtual bool findFiles(String8 basePath, Vector& extensions, 57 | KeyedVector& fileStore, 58 | DirectoryWalker* dw); 59 | 60 | private: 61 | /** 62 | * checkAndAddFile looks at a single file path and stat combo 63 | * to determine whether it is a matching file (by looking at 64 | * the extension) 65 | * 66 | * PRECONDITIONS 67 | * no setup is needed 68 | * 69 | * POSTCONDITIONS 70 | * If the given file has a matching extension then a new entry 71 | * is added to the KeyedVector with the path as the key and the modification 72 | * time as the value. 73 | * 74 | */ 75 | static void checkAndAddFile(String8 path, const struct stat* stats, 76 | Vector& extensions, 77 | KeyedVector& fileStore); 78 | 79 | }; 80 | #endif // FILEFINDER_H 81 | -------------------------------------------------------------------------------- /utils-cpp/StringArray.cpp: -------------------------------------------------------------------------------- 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 | // 18 | // Sortable array of strings. STL-ish, but STL-free. 19 | // 20 | 21 | #include 22 | #include 23 | 24 | #include 25 | 26 | namespace android { 27 | 28 | // 29 | // An expanding array of strings. Add, get, sort, delete. 30 | // 31 | StringArray::StringArray() 32 | : mMax(0), mCurrent(0), mArray(NULL) 33 | { 34 | } 35 | 36 | StringArray:: ~StringArray() { 37 | for (int i = 0; i < mCurrent; i++) 38 | delete[] mArray[i]; 39 | delete[] mArray; 40 | } 41 | 42 | // 43 | // Add a string. A copy of the string is made. 44 | // 45 | bool StringArray::push_back(const char* str) { 46 | if (mCurrent >= mMax) { 47 | char** tmp; 48 | 49 | if (mMax == 0) 50 | mMax = 16; // initial storage 51 | else 52 | mMax *= 2; 53 | 54 | tmp = new char*[mMax]; 55 | if (tmp == NULL) 56 | return false; 57 | 58 | memcpy(tmp, mArray, mCurrent * sizeof(char*)); 59 | delete[] mArray; 60 | mArray = tmp; 61 | } 62 | 63 | int len = strlen(str); 64 | mArray[mCurrent] = new char[len+1]; 65 | memcpy(mArray[mCurrent], str, len+1); 66 | mCurrent++; 67 | 68 | return true; 69 | } 70 | 71 | // 72 | // Delete an entry. 73 | // 74 | void StringArray::erase(int idx) { 75 | if (idx < 0 || idx >= mCurrent) 76 | return; 77 | delete[] mArray[idx]; 78 | if (idx < mCurrent-1) { 79 | memmove(&mArray[idx], &mArray[idx+1], 80 | (mCurrent-1 - idx) * sizeof(char*)); 81 | } 82 | mCurrent--; 83 | } 84 | 85 | // 86 | // Sort the array. 87 | // 88 | void StringArray::sort(int (*compare)(const void*, const void*)) { 89 | qsort(mArray, mCurrent, sizeof(char*), compare); 90 | } 91 | 92 | // 93 | // Pass this to the sort routine to do an ascending alphabetical sort. 94 | // 95 | int StringArray::cmpAscendingAlpha(const void* pstr1, const void* pstr2) { 96 | return strcmp(*(const char**)pstr1, *(const char**)pstr2); 97 | } 98 | 99 | // 100 | // Set entry N to specified string. 101 | // [should use operator[] here] 102 | // 103 | void StringArray::setEntry(int idx, const char* str) { 104 | if (idx < 0 || idx >= mCurrent) 105 | return; 106 | delete[] mArray[idx]; 107 | int len = strlen(str); 108 | mArray[idx] = new char[len+1]; 109 | memcpy(mArray[idx], str, len+1); 110 | } 111 | 112 | 113 | }; // namespace android 114 | -------------------------------------------------------------------------------- /ResourceFilter.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright 2011 The Android Open Source Project 3 | // 4 | // Build resource files from raw assets. 5 | // 6 | 7 | #include "ResourceFilter.h" 8 | 9 | status_t 10 | ResourceFilter::parse(const char* arg) 11 | { 12 | if (arg == NULL) { 13 | return 0; 14 | } 15 | 16 | const char* p = arg; 17 | const char* q; 18 | 19 | while (true) { 20 | q = strchr(p, ','); 21 | if (q == NULL) { 22 | q = p + strlen(p); 23 | } 24 | 25 | String8 part(p, q-p); 26 | 27 | if (part == "zz_ZZ") { 28 | mContainsPseudo = true; 29 | } 30 | int axis; 31 | uint32_t value; 32 | if (AaptGroupEntry::parseNamePart(part, &axis, &value)) { 33 | fprintf(stderr, "Invalid configuration: %s\n", arg); 34 | fprintf(stderr, " "); 35 | for (int i=0; i()); 48 | } 49 | SortedVector& sv = mData.editValueFor(axis); 50 | sv.add(value); 51 | // if it's a locale with a region, also match an unmodified locale of the 52 | // same language 53 | if (axis == AXIS_LANGUAGE) { 54 | if (value & 0xffff0000) { 55 | sv.add(value & 0x0000ffff); 56 | } 57 | } 58 | p = q; 59 | if (!*p) break; 60 | p++; 61 | } 62 | 63 | return NO_ERROR; 64 | } 65 | 66 | bool 67 | ResourceFilter::isEmpty() const 68 | { 69 | return mData.size() == 0; 70 | } 71 | 72 | bool 73 | ResourceFilter::match(int axis, uint32_t value) const 74 | { 75 | if (value == 0) { 76 | // they didn't specify anything so take everything 77 | return true; 78 | } 79 | ssize_t index = mData.indexOfKey(axis); 80 | if (index < 0) { 81 | // we didn't request anything on this axis so take everything 82 | return true; 83 | } 84 | const SortedVector& sv = mData.valueAt(index); 85 | return sv.indexOf(value) >= 0; 86 | } 87 | 88 | bool 89 | ResourceFilter::match(int axis, const ResTable_config& config) const 90 | { 91 | return match(axis, AaptGroupEntry::getConfigValueForAxis(config, axis)); 92 | } 93 | 94 | bool 95 | ResourceFilter::match(const ResTable_config& config) const 96 | { 97 | for (int i=AXIS_START; i<=AXIS_END; i++) { 98 | if (!match(i, AaptGroupEntry::getConfigValueForAxis(config, i))) { 99 | return false; 100 | } 101 | } 102 | return true; 103 | } 104 | 105 | const SortedVector* ResourceFilter::configsForAxis(int axis) const 106 | { 107 | ssize_t index = mData.indexOfKey(axis); 108 | if (index < 0) { 109 | return NULL; 110 | } 111 | return &mData.valueAt(index); 112 | } 113 | -------------------------------------------------------------------------------- /utils/StreamingZipInflater.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 __LIBS_STREAMINGZIPINFLATER_H 18 | #define __LIBS_STREAMINGZIPINFLATER_H 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | #include 25 | 26 | namespace android { 27 | 28 | class StreamingZipInflater { 29 | public: 30 | static const size_t INPUT_CHUNK_SIZE = 64 * 1024; 31 | static const size_t OUTPUT_CHUNK_SIZE = 64 * 1024; 32 | 33 | // Flavor that pages in the compressed data from a fd 34 | StreamingZipInflater(int fd, off64_t compDataStart, size_t uncompSize, size_t compSize); 35 | 36 | // Flavor that gets the compressed data from an in-memory buffer 37 | StreamingZipInflater(class FileMap* dataMap, size_t uncompSize); 38 | 39 | ~StreamingZipInflater(); 40 | 41 | // read 'count' bytes of uncompressed data from the current position. outBuf may 42 | // be NULL, in which case the data is consumed and discarded. 43 | ssize_t read(void* outBuf, size_t count); 44 | 45 | // seeking backwards requires uncompressing fom the beginning, so is very 46 | // expensive. seeking forwards only requires uncompressing from the current 47 | // position to the destination. 48 | off64_t seekAbsolute(off64_t absoluteInputPosition); 49 | 50 | private: 51 | void initInflateState(); 52 | int readNextChunk(); 53 | 54 | // where to find the uncompressed data 55 | int mFd; 56 | off64_t mInFileStart; // where the compressed data lives in the file 57 | class FileMap* mDataMap; 58 | 59 | z_stream mInflateState; 60 | bool mStreamNeedsInit; 61 | 62 | // output invariants for this asset 63 | uint8_t* mOutBuf; // output buf for decompressed bytes 64 | size_t mOutBufSize; // allocated size of mOutBuf 65 | size_t mOutTotalSize; // total uncompressed size of the blob 66 | 67 | // current output state bookkeeping 68 | off64_t mOutCurPosition; // current position in total offset 69 | size_t mOutLastDecoded; // last decoded byte + 1 in mOutbuf 70 | size_t mOutDeliverable; // next undelivered byte of decoded output in mOutBuf 71 | 72 | // input invariants 73 | uint8_t* mInBuf; 74 | size_t mInBufSize; // allocated size of mInBuf; 75 | size_t mInTotalSize; // total size of compressed data for this blob 76 | 77 | // input state bookkeeping 78 | size_t mInNextChunkOffset; // offset from start of blob at which the next input chunk lies 79 | // the z_stream contains state about input block consumption 80 | }; 81 | 82 | } 83 | 84 | #endif 85 | -------------------------------------------------------------------------------- /FileFinder.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright 2011 The Android Open Source Project 3 | // 4 | 5 | // File Finder implementation. 6 | // Implementation for the functions declared and documented in FileFinder.h 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | #include 13 | #include 14 | 15 | #include "DirectoryWalker.h" 16 | #include "FileFinder.h" 17 | 18 | //#define DEBUG 19 | 20 | using android::String8; 21 | 22 | // Private function to check whether a file is a directory or not 23 | bool isDirectory(const char* filename) { 24 | struct stat fileStat; 25 | if (stat(filename, &fileStat) == -1) { 26 | return false; 27 | } 28 | return(S_ISDIR(fileStat.st_mode)); 29 | } 30 | 31 | 32 | // Private function to check whether a file is a regular file or not 33 | bool isFile(const char* filename) { 34 | struct stat fileStat; 35 | if (stat(filename, &fileStat) == -1) { 36 | return false; 37 | } 38 | return(S_ISREG(fileStat.st_mode)); 39 | } 40 | 41 | bool SystemFileFinder::findFiles(String8 basePath, Vector& extensions, 42 | KeyedVector& fileStore, 43 | DirectoryWalker* dw) 44 | { 45 | // Scan the directory pointed to by basePath 46 | // check files and recurse into subdirectories. 47 | if (!dw->openDir(basePath)) { 48 | return false; 49 | } 50 | /* 51 | * Go through all directory entries. Check each file using checkAndAddFile 52 | * and recurse into sub-directories. 53 | */ 54 | struct dirent* entry; 55 | while ((entry = dw->nextEntry()) != NULL) { 56 | String8 entryName(entry->d_name); 57 | if (entry->d_name[0] == '.') // Skip hidden files and directories 58 | continue; 59 | 60 | String8 fullPath = basePath.appendPathCopy(entryName); 61 | // If this entry is a directory we'll recurse into it 62 | if (isDirectory(fullPath.string()) ) { 63 | DirectoryWalker* copy = dw->clone(); 64 | findFiles(fullPath, extensions, fileStore,copy); 65 | delete copy; 66 | } 67 | 68 | // If this entry is a file, we'll pass it over to checkAndAddFile 69 | if (isFile(fullPath.string()) ) { 70 | checkAndAddFile(fullPath,dw->entryStats(),extensions,fileStore); 71 | } 72 | } 73 | 74 | // Clean up 75 | dw->closeDir(); 76 | 77 | return true; 78 | } 79 | 80 | void SystemFileFinder::checkAndAddFile(String8 path, const struct stat* stats, 81 | Vector& extensions, 82 | KeyedVector& fileStore) 83 | { 84 | // Loop over the extensions, checking for a match 85 | bool done = false; 86 | String8 ext(path.getPathExtension()); 87 | ext.toLower(); 88 | for (size_t i = 0; i < extensions.size() && !done; ++i) { 89 | String8 ext2 = extensions[i].getPathExtension(); 90 | ext2.toLower(); 91 | // Compare the extensions. If a match is found, add to storage. 92 | if (ext == ext2) { 93 | done = true; 94 | fileStore.add(path,stats->st_mtime); 95 | } 96 | } 97 | } 98 | 99 | -------------------------------------------------------------------------------- /utils-cpp/SharedBuffer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005 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 | 20 | #include 21 | #include 22 | 23 | // --------------------------------------------------------------------------- 24 | 25 | namespace android { 26 | 27 | SharedBuffer* SharedBuffer::alloc(size_t size) 28 | { 29 | SharedBuffer* sb = static_cast(malloc(sizeof(SharedBuffer) + size)); 30 | if (sb) { 31 | sb->mRefs = 1; 32 | sb->mSize = size; 33 | } 34 | return sb; 35 | } 36 | 37 | 38 | ssize_t SharedBuffer::dealloc(const SharedBuffer* released) 39 | { 40 | if (released->mRefs != 0) return -1; // XXX: invalid operation 41 | free(const_cast(released)); 42 | return 0; 43 | } 44 | 45 | SharedBuffer* SharedBuffer::edit() const 46 | { 47 | if (onlyOwner()) { 48 | return const_cast(this); 49 | } 50 | SharedBuffer* sb = alloc(mSize); 51 | if (sb) { 52 | memcpy(sb->data(), data(), size()); 53 | release(); 54 | } 55 | return sb; 56 | } 57 | 58 | SharedBuffer* SharedBuffer::editResize(size_t newSize) const 59 | { 60 | if (onlyOwner()) { 61 | SharedBuffer* buf = const_cast(this); 62 | if (buf->mSize == newSize) return buf; 63 | buf = (SharedBuffer*)realloc(buf, sizeof(SharedBuffer) + newSize); 64 | if (buf != NULL) { 65 | buf->mSize = newSize; 66 | return buf; 67 | } 68 | } 69 | SharedBuffer* sb = alloc(newSize); 70 | if (sb) { 71 | const size_t mySize = mSize; 72 | memcpy(sb->data(), data(), newSize < mySize ? newSize : mySize); 73 | release(); 74 | } 75 | return sb; 76 | } 77 | 78 | SharedBuffer* SharedBuffer::attemptEdit() const 79 | { 80 | if (onlyOwner()) { 81 | return const_cast(this); 82 | } 83 | return 0; 84 | } 85 | 86 | SharedBuffer* SharedBuffer::reset(size_t new_size) const 87 | { 88 | // cheap-o-reset. 89 | SharedBuffer* sb = alloc(new_size); 90 | if (sb) { 91 | release(); 92 | } 93 | return sb; 94 | } 95 | 96 | void SharedBuffer::acquire() const { 97 | android_atomic_inc(&mRefs); 98 | } 99 | 100 | int32_t SharedBuffer::release(uint32_t flags) const 101 | { 102 | int32_t prev = 1; 103 | if (onlyOwner() || ((prev = android_atomic_dec(&mRefs)) == 1)) { 104 | mRefs = 0; 105 | if ((flags & eKeepStorage) == 0) { 106 | free(const_cast(this)); 107 | } 108 | } 109 | return prev; 110 | } 111 | 112 | 113 | }; // namespace android 114 | -------------------------------------------------------------------------------- /utils-cpp/Android.mk: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2008 The Android Open Source Project 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | LOCAL_PATH:= $(call my-dir) 16 | 17 | # libutils is a little unique: It's built twice, once for the host 18 | # and once for the device. 19 | 20 | commonSources:= \ 21 | Asset.cpp \ 22 | AssetDir.cpp \ 23 | AssetManager.cpp \ 24 | BlobCache.cpp \ 25 | BufferedTextOutput.cpp \ 26 | CallStack.cpp \ 27 | Debug.cpp \ 28 | FileMap.cpp \ 29 | Flattenable.cpp \ 30 | LinearTransform.cpp \ 31 | ObbFile.cpp \ 32 | PropertyMap.cpp \ 33 | RefBase.cpp \ 34 | ResourceTypes.cpp \ 35 | SharedBuffer.cpp \ 36 | Static.cpp \ 37 | StopWatch.cpp \ 38 | StreamingZipInflater.cpp \ 39 | String8.cpp \ 40 | String16.cpp \ 41 | StringArray.cpp \ 42 | SystemClock.cpp \ 43 | TextOutput.cpp \ 44 | Threads.cpp \ 45 | Timers.cpp \ 46 | Tokenizer.cpp \ 47 | Unicode.cpp \ 48 | VectorImpl.cpp \ 49 | ZipFileCRO.cpp \ 50 | ZipFileRO.cpp \ 51 | ZipUtils.cpp \ 52 | misc.cpp 53 | 54 | 55 | # For the host 56 | # ===================================================== 57 | 58 | include $(CLEAR_VARS) 59 | 60 | LOCAL_SRC_FILES:= $(commonSources) 61 | 62 | LOCAL_MODULE:= libutils 63 | 64 | LOCAL_CFLAGS += -DLIBUTILS_NATIVE=1 $(TOOL_CFLAGS) 65 | LOCAL_C_INCLUDES += external/zlib 66 | 67 | ifeq ($(HOST_OS),windows) 68 | ifeq ($(strip $(USE_CYGWIN),),) 69 | # Under MinGW, ctype.h doesn't need multi-byte support 70 | LOCAL_CFLAGS += -DMB_CUR_MAX=1 71 | endif 72 | endif 73 | 74 | include $(BUILD_HOST_STATIC_LIBRARY) 75 | 76 | 77 | 78 | # For the device 79 | # ===================================================== 80 | include $(CLEAR_VARS) 81 | 82 | 83 | # we have the common sources, plus some device-specific stuff 84 | LOCAL_SRC_FILES:= \ 85 | $(commonSources) \ 86 | BackupData.cpp \ 87 | BackupHelpers.cpp \ 88 | Looper.cpp 89 | 90 | ifeq ($(TARGET_OS),linux) 91 | LOCAL_LDLIBS += -lrt -ldl 92 | endif 93 | 94 | LOCAL_C_INCLUDES += \ 95 | external/zlib \ 96 | external/icu4c/common 97 | 98 | LOCAL_LDLIBS += -lpthread 99 | 100 | LOCAL_SHARED_LIBRARIES := \ 101 | libz \ 102 | liblog \ 103 | libcutils \ 104 | libdl 105 | 106 | LOCAL_MODULE:= libutils 107 | include $(BUILD_SHARED_LIBRARY) 108 | 109 | ifeq ($(TARGET_OS),linux) 110 | include $(CLEAR_VARS) 111 | LOCAL_C_INCLUDES += external/zlib external/icu4c/common 112 | LOCAL_LDLIBS := -lrt -ldl -lpthread 113 | LOCAL_MODULE := libutils 114 | LOCAL_SRC_FILES := $(commonSources) BackupData.cpp BackupHelpers.cpp 115 | include $(BUILD_STATIC_LIBRARY) 116 | endif 117 | 118 | 119 | # Include subdirectory makefiles 120 | # ============================================================ 121 | 122 | # If we're building with ONE_SHOT_MAKEFILE (mm, mmm), then what the framework 123 | # team really wants is to build the stuff defined by this makefile. 124 | ifeq (,$(ONE_SHOT_MAKEFILE)) 125 | include $(call first-makefiles-under,$(LOCAL_PATH)) 126 | endif 127 | -------------------------------------------------------------------------------- /printapk.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace android; 11 | 12 | static int 13 | usage() 14 | { 15 | fprintf(stderr, 16 | "usage: apk APKFILE\n" 17 | "\n" 18 | "APKFILE an android packge file produced by aapt.\n" 19 | ); 20 | return 1; 21 | } 22 | 23 | 24 | int 25 | p_main(int argc, char** argv) 26 | { 27 | const char* filename; 28 | int fd; 29 | ssize_t amt; 30 | off_t size; 31 | void* buf; 32 | zipfile_t zip; 33 | zipentry_t entry; 34 | void* cookie; 35 | void* resfile; 36 | int bufsize; 37 | int err; 38 | 39 | if (argc != 2) { 40 | return usage(); 41 | } 42 | 43 | filename = argv[1]; 44 | fd = open(filename, O_RDONLY); 45 | if (fd == -1) { 46 | fprintf(stderr, "apk: couldn't open file for read: %s\n", filename); 47 | return 1; 48 | } 49 | 50 | size = lseek(fd, 0, SEEK_END); 51 | amt = lseek(fd, 0, SEEK_SET); 52 | 53 | if (size < 0 || amt < 0) { 54 | fprintf(stderr, "apk: error determining file size: %s\n", filename); 55 | return 1; 56 | } 57 | 58 | buf = malloc(size); 59 | if (buf == NULL) { 60 | fprintf(stderr, "apk: file too big: %s\n", filename); 61 | return 1; 62 | } 63 | 64 | amt = read(fd, buf, size); 65 | if (amt != size) { 66 | fprintf(stderr, "apk: error reading file: %s\n", filename); 67 | return 1; 68 | } 69 | 70 | close(fd); 71 | 72 | zip = init_zipfile(buf, size); 73 | if (zip == NULL) { 74 | fprintf(stderr, "apk: file doesn't seem to be a zip file: %s\n", 75 | filename); 76 | return 1; 77 | } 78 | 79 | printf("files:\n"); 80 | cookie = NULL; 81 | while ((entry = iterate_zipfile(zip, &cookie))) { 82 | char* name = get_zipentry_name(entry); 83 | printf(" %s\n", name); 84 | free(name); 85 | } 86 | 87 | entry = lookup_zipentry(zip, "resources.arsc"); 88 | if (entry != NULL) { 89 | size = get_zipentry_size(entry); 90 | bufsize = size + (size / 1000) + 1; 91 | resfile = malloc(bufsize); 92 | 93 | err = decompress_zipentry(entry, resfile, bufsize); 94 | if (err != 0) { 95 | fprintf(stderr, "apk: error decompressing resources.arsc"); 96 | return 1; 97 | } 98 | 99 | ResTable res(resfile, size, resfile); 100 | res.print(false); 101 | #if 0 102 | size_t tableCount = res.getTableCount(); 103 | printf("Tables: %d\n", (int)tableCount); 104 | for (size_t tableIndex=0; tableIndexsize(); 107 | for (size_t stringIndex=0; stringIndexstringAt(stringIndex, &len); 110 | String8 s(String16(ch, len)); 111 | printf(" [%3d] %s\n", (int)stringIndex, s.string()); 112 | } 113 | } 114 | 115 | size_t basePackageCount = res.getBasePackageCount(); 116 | printf("Base Packages: %d\n", (int)basePackageCount); 117 | for (size_t bpIndex=0; bpIndex 21 | #include 22 | #include 23 | #include 24 | 25 | namespace android { 26 | 27 | /* 28 | * Provides a mechanism for passing around string-based property key / value pairs 29 | * and loading them from property files. 30 | * 31 | * The property files have the following simple structure: 32 | * 33 | * # Comment 34 | * key = value 35 | * 36 | * Keys and values are any sequence of printable ASCII characters. 37 | * The '=' separates the key from the value. 38 | * The key and value may not contain whitespace. 39 | * 40 | * The '\' character is reserved for escape sequences and is not currently supported. 41 | * The '"" character is reserved for quoting and is not currently supported. 42 | * Files that contain the '\' or '"' character will fail to parse. 43 | * 44 | * The file must not contain duplicate keys. 45 | * 46 | * TODO Support escape sequences and quoted values when needed. 47 | */ 48 | class PropertyMap { 49 | public: 50 | /* Creates an empty property map. */ 51 | PropertyMap(); 52 | ~PropertyMap(); 53 | 54 | /* Clears the property map. */ 55 | void clear(); 56 | 57 | /* Adds a property. 58 | * Replaces the property with the same key if it is already present. 59 | */ 60 | void addProperty(const String8& key, const String8& value); 61 | 62 | /* Returns true if the property map contains the specified key. */ 63 | bool hasProperty(const String8& key) const; 64 | 65 | /* Gets the value of a property and parses it. 66 | * Returns true and sets outValue if the key was found and its value was parsed successfully. 67 | * Otherwise returns false and does not modify outValue. (Also logs a warning.) 68 | */ 69 | bool tryGetProperty(const String8& key, String8& outValue) const; 70 | bool tryGetProperty(const String8& key, bool& outValue) const; 71 | bool tryGetProperty(const String8& key, int32_t& outValue) const; 72 | bool tryGetProperty(const String8& key, float& outValue) const; 73 | 74 | /* Adds all values from the specified property map. */ 75 | void addAll(const PropertyMap* map); 76 | 77 | /* Gets the underlying property map. */ 78 | inline const KeyedVector& getProperties() const { return mProperties; } 79 | 80 | /* Loads a property map from a file. */ 81 | static status_t load(const String8& filename, PropertyMap** outMap); 82 | 83 | private: 84 | class Parser { 85 | PropertyMap* mMap; 86 | Tokenizer* mTokenizer; 87 | 88 | public: 89 | Parser(PropertyMap* map, Tokenizer* tokenizer); 90 | ~Parser(); 91 | status_t parse(); 92 | 93 | private: 94 | status_t parseType(); 95 | status_t parseKey(); 96 | status_t parseKeyProperty(); 97 | status_t parseModifier(const String8& token, int32_t* outMetaState); 98 | status_t parseCharacterLiteral(char16_t* outCharacter); 99 | }; 100 | 101 | KeyedVector mProperties; 102 | }; 103 | 104 | } // namespace android 105 | 106 | #endif // _UTILS_PROPERTY_MAP_H 107 | -------------------------------------------------------------------------------- /CrunchCache.h: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright 2011 The Android Open Source Project 3 | // 4 | // Cache manager for pre-processed PNG files. 5 | // Contains code for managing which PNG files get processed 6 | // at build time. 7 | // 8 | 9 | #ifndef CRUNCHCACHE_H 10 | #define CRUNCHCACHE_H 11 | 12 | #include 13 | #include 14 | #include "FileFinder.h" 15 | #include "CacheUpdater.h" 16 | 17 | using namespace android; 18 | 19 | /** CrunchCache 20 | * This class is a cache manager which can pre-process PNG files and store 21 | * them in a mirror-cache. It's capable of doing incremental updates to its 22 | * cache. 23 | * 24 | * Usage: 25 | * Create an instance initialized with the root of the source tree, the 26 | * root location to store the cache files, and an instance of a file finder. 27 | * Then update the cache by calling crunch. 28 | */ 29 | class CrunchCache { 30 | public: 31 | // Constructor 32 | CrunchCache(String8 sourcePath, String8 destPath, FileFinder* ff); 33 | 34 | // Nobody should be calling the default constructor 35 | // So this space is intentionally left blank 36 | 37 | // Default Copy Constructor and Destructor are fine 38 | 39 | /** crunch is the workhorse of this class. 40 | * It goes through all the files found in the sourcePath and compares 41 | * them to the cached versions in the destPath. If the optional 42 | * argument forceOverwrite is set to true, then all source files are 43 | * re-crunched even if they have not been modified recently. Otherwise, 44 | * source files are only crunched when they needUpdating. Afterwards, 45 | * we delete any leftover files in the cache that are no longer present 46 | * in source. 47 | * 48 | * PRECONDITIONS: 49 | * No setup besides construction is needed 50 | * POSTCONDITIONS: 51 | * The cache is updated to fully reflect all changes in source. 52 | * The function then returns the number of files changed in cache 53 | * (counting deletions). 54 | */ 55 | size_t crunch(CacheUpdater* cu, bool forceOverwrite=false); 56 | 57 | private: 58 | /** loadFiles is a wrapper to the FileFinder that places matching 59 | * files into mSourceFiles and mDestFiles. 60 | * 61 | * POSTCONDITIONS 62 | * mDestFiles and mSourceFiles are refreshed to reflect the current 63 | * state of the files in the source and dest directories. 64 | * Any previous contents of mSourceFiles and mDestFiles are cleared. 65 | */ 66 | void loadFiles(); 67 | 68 | /** needsUpdating takes a file path 69 | * and returns true if the file represented by this path is newer in the 70 | * sourceFiles than in the cache (mDestFiles). 71 | * 72 | * PRECONDITIONS: 73 | * mSourceFiles and mDestFiles must be initialized and filled. 74 | * POSTCONDITIONS: 75 | * returns true if and only if source file's modification time 76 | * is greater than the cached file's mod-time. Otherwise returns false. 77 | * 78 | * USAGE: 79 | * Should be used something like the following: 80 | * if (needsUpdating(filePath)) 81 | * // Recrunch sourceFile out to destFile. 82 | * 83 | */ 84 | bool needsUpdating(String8 relativePath) const; 85 | 86 | // DATA MEMBERS ==================================================== 87 | 88 | String8 mSourcePath; 89 | String8 mDestPath; 90 | 91 | Vector mExtensions; 92 | 93 | // Each vector of paths contains one entry per PNG file encountered. 94 | // Each entry consists of a path pointing to that PNG. 95 | DefaultKeyedVector mSourceFiles; 96 | DefaultKeyedVector mDestFiles; 97 | 98 | // Pointer to a FileFinder to use 99 | FileFinder* mFileFinder; 100 | }; 101 | 102 | #endif // CRUNCHCACHE_H 103 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /utils/ObbFile.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 OBBFILE_H_ 18 | #define OBBFILE_H_ 19 | 20 | #include 21 | #include 22 | 23 | #include 24 | #include 25 | 26 | namespace android { 27 | 28 | // OBB flags (bit 0) 29 | #define OBB_OVERLAY (1 << 0) 30 | #define OBB_SALTED (1 << 1) 31 | 32 | class ObbFile : public RefBase { 33 | protected: 34 | virtual ~ObbFile(); 35 | 36 | public: 37 | ObbFile(); 38 | 39 | bool readFrom(const char* filename); 40 | bool readFrom(int fd); 41 | bool writeTo(const char* filename); 42 | bool writeTo(int fd); 43 | bool removeFrom(const char* filename); 44 | bool removeFrom(int fd); 45 | 46 | const char* getFileName() const { 47 | return mFileName; 48 | } 49 | 50 | const String8 getPackageName() const { 51 | return mPackageName; 52 | } 53 | 54 | void setPackageName(String8 packageName) { 55 | mPackageName = packageName; 56 | } 57 | 58 | int32_t getVersion() const { 59 | return mVersion; 60 | } 61 | 62 | void setVersion(int32_t version) { 63 | mVersion = version; 64 | } 65 | 66 | int32_t getFlags() const { 67 | return mFlags; 68 | } 69 | 70 | void setFlags(int32_t flags) { 71 | mFlags = flags; 72 | } 73 | 74 | const unsigned char* getSalt(size_t* length) const { 75 | if ((mFlags & OBB_SALTED) == 0) { 76 | *length = 0; 77 | return NULL; 78 | } 79 | 80 | *length = sizeof(mSalt); 81 | return mSalt; 82 | } 83 | 84 | bool setSalt(const unsigned char* salt, size_t length) { 85 | if (length != sizeof(mSalt)) { 86 | return false; 87 | } 88 | 89 | memcpy(mSalt, salt, sizeof(mSalt)); 90 | mFlags |= OBB_SALTED; 91 | return true; 92 | } 93 | 94 | bool isOverlay() { 95 | return (mFlags & OBB_OVERLAY) == OBB_OVERLAY; 96 | } 97 | 98 | void setOverlay(bool overlay) { 99 | if (overlay) { 100 | mFlags |= OBB_OVERLAY; 101 | } else { 102 | mFlags &= ~OBB_OVERLAY; 103 | } 104 | } 105 | 106 | static inline uint32_t get4LE(const unsigned char* buf) { 107 | return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24); 108 | } 109 | 110 | static inline void put4LE(unsigned char* buf, uint32_t val) { 111 | buf[0] = val & 0xFF; 112 | buf[1] = (val >> 8) & 0xFF; 113 | buf[2] = (val >> 16) & 0xFF; 114 | buf[3] = (val >> 24) & 0xFF; 115 | } 116 | 117 | private: 118 | /* Package name this ObbFile is associated with */ 119 | String8 mPackageName; 120 | 121 | /* Package version this ObbFile is associated with */ 122 | int32_t mVersion; 123 | 124 | /* Flags for this OBB type. */ 125 | int32_t mFlags; 126 | 127 | /* Whether the file is salted. */ 128 | bool mSalted; 129 | 130 | /* The encryption salt. */ 131 | unsigned char mSalt[8]; 132 | 133 | const char* mFileName; 134 | 135 | size_t mFileSize; 136 | 137 | size_t mFooterStart; 138 | 139 | unsigned char* mReadBuf; 140 | 141 | bool parseObbFile(int fd); 142 | }; 143 | 144 | } 145 | #endif /* OBBFILE_H_ */ 146 | -------------------------------------------------------------------------------- /CrunchCache.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright 2011 The Android Open Source Project 3 | // 4 | // Implementation file for CrunchCache 5 | // This file defines functions laid out and documented in 6 | // CrunchCache.h 7 | 8 | #include 9 | #include 10 | 11 | #include "DirectoryWalker.h" 12 | #include "FileFinder.h" 13 | #include "CacheUpdater.h" 14 | #include "CrunchCache.h" 15 | 16 | using namespace android; 17 | 18 | CrunchCache::CrunchCache(String8 sourcePath, String8 destPath, FileFinder* ff) 19 | : mSourcePath(sourcePath), mDestPath(destPath), mSourceFiles(0), mDestFiles(0), mFileFinder(ff) 20 | { 21 | // We initialize the default value to return to 0 so if a file doesn't exist 22 | // then all files are automatically "newer" than it. 23 | 24 | // Set file extensions to look for. Right now just pngs. 25 | mExtensions.push(String8(".png")); 26 | 27 | // Load files into our data members 28 | loadFiles(); 29 | } 30 | 31 | size_t CrunchCache::crunch(CacheUpdater* cu, bool forceOverwrite) 32 | { 33 | size_t numFilesUpdated = 0; 34 | 35 | // Iterate through the source files and compare to cache. 36 | // After processing a file, remove it from the source files and 37 | // from the dest files. 38 | // We're done when we're out of files in source. 39 | String8 relativePath; 40 | while (mSourceFiles.size() > 0) { 41 | // Get the full path to the source file, then convert to a c-string 42 | // and offset our beginning pointer to the length of the sourcePath 43 | // This efficiently strips the source directory prefix from our path. 44 | // Also, String8 doesn't have a substring method so this is what we've 45 | // got to work with. 46 | const char* rPathPtr = mSourceFiles.keyAt(0).string()+mSourcePath.length(); 47 | // Strip leading slash if present 48 | int offset = 0; 49 | if (rPathPtr[0] == OS_PATH_SEPARATOR) 50 | offset = 1; 51 | relativePath = String8(rPathPtr + offset); 52 | 53 | if (forceOverwrite || needsUpdating(relativePath)) { 54 | cu->processImage(mSourcePath.appendPathCopy(relativePath), 55 | mDestPath.appendPathCopy(relativePath)); 56 | numFilesUpdated++; 57 | // crunchFile(relativePath); 58 | } 59 | // Delete this file from the source files and (if it exists) from the 60 | // dest files. 61 | mSourceFiles.removeItemsAt(0); 62 | mDestFiles.removeItem(mDestPath.appendPathCopy(relativePath)); 63 | } 64 | 65 | // Iterate through what's left of destFiles and delete leftovers 66 | while (mDestFiles.size() > 0) { 67 | cu->deleteFile(mDestFiles.keyAt(0)); 68 | mDestFiles.removeItemsAt(0); 69 | } 70 | 71 | // Update our knowledge of the files cache 72 | // both source and dest should be empty by now. 73 | loadFiles(); 74 | 75 | return numFilesUpdated; 76 | } 77 | 78 | void CrunchCache::loadFiles() 79 | { 80 | // Clear out our data structures to avoid putting in duplicates 81 | mSourceFiles.clear(); 82 | mDestFiles.clear(); 83 | 84 | // Make a directory walker that points to the system. 85 | DirectoryWalker* dw = new SystemDirectoryWalker(); 86 | 87 | // Load files in the source directory 88 | mFileFinder->findFiles(mSourcePath, mExtensions, mSourceFiles,dw); 89 | 90 | // Load files in the destination directory 91 | mFileFinder->findFiles(mDestPath,mExtensions,mDestFiles,dw); 92 | 93 | delete dw; 94 | } 95 | 96 | bool CrunchCache::needsUpdating(String8 relativePath) const 97 | { 98 | // Retrieve modification dates for this file entry under the source and 99 | // cache directory trees. The vectors will return a modification date of 0 100 | // if the file doesn't exist. 101 | time_t sourceDate = mSourceFiles.valueFor(mSourcePath.appendPathCopy(relativePath)); 102 | time_t destDate = mDestFiles.valueFor(mDestPath.appendPathCopy(relativePath)); 103 | return sourceDate > destDate; 104 | } -------------------------------------------------------------------------------- /CacheUpdater.h: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright 2011 The Android Open Source Project 3 | // 4 | // Abstraction of calls to system to make directories and delete files and 5 | // wrapper to image processing. 6 | 7 | #ifndef CACHE_UPDATER_H 8 | #define CACHE_UPDATER_H 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "Images.h" 15 | 16 | #include "preDef.h" 17 | 18 | using namespace android; 19 | 20 | /** CacheUpdater 21 | * This is a pure virtual class that declares abstractions of functions useful 22 | * for managing a cache files. This manager is set up to be used in a 23 | * mirror cache where the source tree is duplicated and filled with processed 24 | * images. This class is abstracted to allow for dependency injection during 25 | * unit testing. 26 | * Usage: 27 | * To update/add a file to the cache, call processImage 28 | * To remove a file from the cache, call deleteFile 29 | */ 30 | class CacheUpdater { 31 | public: 32 | // Make sure all the directories along this path exist 33 | virtual void ensureDirectoriesExist(String8 path) = 0; 34 | 35 | // Delete a file 36 | virtual void deleteFile(String8 path) = 0; 37 | 38 | // Process an image from source out to dest 39 | virtual void processImage(String8 source, String8 dest) = 0; 40 | private: 41 | }; 42 | 43 | /** SystemCacheUpdater 44 | * This is an implementation of the above virtual cache updater specification. 45 | * This implementations hits the filesystem to manage a cache and calls out to 46 | * the PNG crunching in images.h to process images out to its cache components. 47 | */ 48 | class SystemCacheUpdater : public CacheUpdater { 49 | public: 50 | // Constructor to set bundle to pass to preProcessImage 51 | SystemCacheUpdater (Bundle* b) 52 | : bundle(b) { }; 53 | 54 | // Make sure all the directories along this path exist 55 | virtual void ensureDirectoriesExist(String8 path) 56 | { 57 | // Check to see if we're dealing with a fully qualified path 58 | String8 existsPath; 59 | String8 toCreate; 60 | String8 remains; 61 | struct stat s; 62 | 63 | // Check optomistically to see if all directories exist. 64 | // If something in the path doesn't exist, then walk the path backwards 65 | // and find the place to start creating directories forward. 66 | if (stat(path.string(),&s) == -1) { 67 | // Walk backwards to find place to start creating directories 68 | existsPath = path; 69 | do { 70 | // As we remove the end of existsPath add it to 71 | // the string of paths to create. 72 | toCreate = existsPath.getPathLeaf().appendPath(toCreate); 73 | existsPath = existsPath.getPathDir(); 74 | } while (stat(existsPath.string(),&s) == -1); 75 | 76 | // Walk forwards and build directories as we go 77 | do { 78 | // Advance to the next segment of the path 79 | existsPath.appendPath(toCreate.walkPath(&remains)); 80 | toCreate = remains; 81 | #ifdef HAVE_MS_C_RUNTIME 82 | _mkdir(existsPath.string()); 83 | #else 84 | mkdir(existsPath.string(), S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP); 85 | #endif 86 | } while (remains.length() > 0); 87 | } //if 88 | }; 89 | 90 | // Delete a file 91 | virtual void deleteFile(String8 path) 92 | { 93 | if (remove(path.string()) != 0) 94 | fprintf(stderr,"ERROR DELETING %s\n",path.string()); 95 | }; 96 | 97 | // Process an image from source out to dest 98 | virtual void processImage(String8 source, String8 dest) 99 | { 100 | // Make sure we're trying to write to a directory that is extant 101 | ensureDirectoriesExist(dest.getPathDir()); 102 | 103 | preProcessImageToCache(bundle, source, dest); 104 | }; 105 | private: 106 | Bundle* bundle; 107 | }; 108 | 109 | #endif // CACHE_UPDATER_H 110 | -------------------------------------------------------------------------------- /utils/Tokenizer.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 _UTILS_TOKENIZER_H 18 | #define _UTILS_TOKENIZER_H 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | namespace android { 26 | 27 | /** 28 | * A simple tokenizer for loading and parsing ASCII text files line by line. 29 | */ 30 | class Tokenizer { 31 | Tokenizer(const String8& filename, FileMap* fileMap, char* buffer, size_t length); 32 | 33 | public: 34 | ~Tokenizer(); 35 | 36 | /** 37 | * Opens a file and maps it into memory. 38 | * 39 | * Returns NO_ERROR and a tokenizer for the file, if successful. 40 | * Otherwise returns an error and sets outTokenizer to NULL. 41 | */ 42 | static status_t open(const String8& filename, Tokenizer** outTokenizer); 43 | 44 | /** 45 | * Returns true if at the end of the file. 46 | */ 47 | inline bool isEof() const { return mCurrent == getEnd(); } 48 | 49 | /** 50 | * Returns true if at the end of the line or end of the file. 51 | */ 52 | inline bool isEol() const { return isEof() || *mCurrent == '\n'; } 53 | 54 | /** 55 | * Gets the name of the file. 56 | */ 57 | inline String8 getFilename() const { return mFilename; } 58 | 59 | /** 60 | * Gets a 1-based line number index for the current position. 61 | */ 62 | inline int32_t getLineNumber() const { return mLineNumber; } 63 | 64 | /** 65 | * Formats a location string consisting of the filename and current line number. 66 | * Returns a string like "MyFile.txt:33". 67 | */ 68 | String8 getLocation() const; 69 | 70 | /** 71 | * Gets the character at the current position. 72 | * Returns null at end of file. 73 | */ 74 | inline char peekChar() const { return isEof() ? '\0' : *mCurrent; } 75 | 76 | /** 77 | * Gets the remainder of the current line as a string, excluding the newline character. 78 | */ 79 | String8 peekRemainderOfLine() const; 80 | 81 | /** 82 | * Gets the character at the current position and advances past it. 83 | * Returns null at end of file. 84 | */ 85 | inline char nextChar() { return isEof() ? '\0' : *(mCurrent++); } 86 | 87 | /** 88 | * Gets the next token on this line stopping at the specified delimiters 89 | * or the end of the line whichever comes first and advances past it. 90 | * Also stops at embedded nulls. 91 | * Returns the token or an empty string if the current character is a delimiter 92 | * or is at the end of the line. 93 | */ 94 | String8 nextToken(const char* delimiters); 95 | 96 | /** 97 | * Advances to the next line. 98 | * Does nothing if already at the end of the file. 99 | */ 100 | void nextLine(); 101 | 102 | /** 103 | * Skips over the specified delimiters in the line. 104 | * Also skips embedded nulls. 105 | */ 106 | void skipDelimiters(const char* delimiters); 107 | 108 | private: 109 | Tokenizer(const Tokenizer& other); // not copyable 110 | 111 | String8 mFilename; 112 | FileMap* mFileMap; 113 | char* mBuffer; 114 | size_t mLength; 115 | 116 | const char* mCurrent; 117 | int32_t mLineNumber; 118 | 119 | inline const char* getEnd() const { return mBuffer + mLength; } 120 | 121 | }; 122 | 123 | } // namespace android 124 | 125 | #endif // _UTILS_TOKENIZER_H 126 | -------------------------------------------------------------------------------- /utils-cpp/SystemClock.cpp: -------------------------------------------------------------------------------- 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 | /* 19 | * System clock functions. 20 | */ 21 | 22 | #ifdef HAVE_ANDROID_OS 23 | #include 24 | #include 25 | #include 26 | #include 27 | #endif 28 | 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | 35 | #include 36 | #include 37 | 38 | #define LOG_TAG "SystemClock" 39 | //#include "utils/Log.h" 40 | #include "fakeLog.h" 41 | 42 | namespace android { 43 | 44 | /* 45 | * Set the current time. This only works when running as root. 46 | */ 47 | int setCurrentTimeMillis(int64_t millis) 48 | { 49 | #if WIN32 50 | // not implemented 51 | return -1; 52 | #else 53 | struct timeval tv; 54 | #ifdef HAVE_ANDROID_OS 55 | struct timespec ts; 56 | int fd; 57 | int res; 58 | #endif 59 | int ret = 0; 60 | 61 | if (millis <= 0 || millis / 1000LL >= INT_MAX) { 62 | return -1; 63 | } 64 | 65 | tv.tv_sec = (time_t) (millis / 1000LL); 66 | tv.tv_usec = (suseconds_t) ((millis % 1000LL) * 1000LL); 67 | 68 | LOGD("Setting time of day to sec=%d\n", (int) tv.tv_sec); 69 | 70 | #ifdef HAVE_ANDROID_OS 71 | fd = open("/dev/alarm", O_RDWR); 72 | if(fd < 0) { 73 | LOGW("Unable to open alarm driver: %s\n", strerror(errno)); 74 | return -1; 75 | } 76 | ts.tv_sec = tv.tv_sec; 77 | ts.tv_nsec = tv.tv_usec * 1000; 78 | res = ioctl(fd, ANDROID_ALARM_SET_RTC, &ts); 79 | if(res < 0) { 80 | LOGW("Unable to set rtc to %ld: %s\n", tv.tv_sec, strerror(errno)); 81 | ret = -1; 82 | } 83 | close(fd); 84 | #else 85 | if (settimeofday(&tv, NULL) != 0) { 86 | LOGW("Unable to set clock to %d.%d: %s\n", 87 | (int) tv.tv_sec, (int) tv.tv_usec, strerror(errno)); 88 | ret = -1; 89 | } 90 | #endif 91 | 92 | return ret; 93 | #endif // WIN32 94 | } 95 | 96 | /* 97 | * native public static long uptimeMillis(); 98 | */ 99 | int64_t uptimeMillis() 100 | { 101 | int64_t when = systemTime(SYSTEM_TIME_MONOTONIC); 102 | return (int64_t) nanoseconds_to_milliseconds(when); 103 | } 104 | 105 | /* 106 | * native public static long elapsedRealtime(); 107 | */ 108 | int64_t elapsedRealtime() 109 | { 110 | #ifdef HAVE_ANDROID_OS 111 | static int s_fd = -1; 112 | 113 | if (s_fd == -1) { 114 | int fd = open("/dev/alarm", O_RDONLY); 115 | if (android_atomic_cmpxchg(-1, fd, &s_fd)) { 116 | close(fd); 117 | } 118 | } 119 | 120 | struct timespec ts; 121 | int result = ioctl(s_fd, 122 | ANDROID_ALARM_GET_TIME(ANDROID_ALARM_ELAPSED_REALTIME), &ts); 123 | 124 | if (result == 0) { 125 | int64_t when = seconds_to_nanoseconds(ts.tv_sec) + ts.tv_nsec; 126 | return (int64_t) nanoseconds_to_milliseconds(when); 127 | } else { 128 | // XXX: there was an error, probably because the driver didn't 129 | // exist ... this should return 130 | // a real error, like an exception! 131 | int64_t when = systemTime(SYSTEM_TIME_MONOTONIC); 132 | return (int64_t) nanoseconds_to_milliseconds(when); 133 | } 134 | #else 135 | int64_t when = systemTime(SYSTEM_TIME_MONOTONIC); 136 | return (int64_t) nanoseconds_to_milliseconds(when); 137 | #endif 138 | } 139 | 140 | }; // namespace android 141 | -------------------------------------------------------------------------------- /SourcePos.cpp: -------------------------------------------------------------------------------- 1 | #include "SourcePos.h" 2 | 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | 9 | // ErrorPos 10 | // ============================================================================= 11 | struct ErrorPos 12 | { 13 | String8 file; 14 | int line; 15 | String8 error; 16 | bool fatal; 17 | 18 | ErrorPos(); 19 | ErrorPos(const ErrorPos& that); 20 | ErrorPos(const String8& file, int line, const String8& error, bool fatal); 21 | ~ErrorPos(); 22 | bool operator<(const ErrorPos& rhs) const; 23 | bool operator==(const ErrorPos& rhs) const; 24 | ErrorPos& operator=(const ErrorPos& rhs); 25 | 26 | void print(FILE* to) const; 27 | }; 28 | 29 | static vector g_errors; 30 | 31 | ErrorPos::ErrorPos() 32 | :line(-1), fatal(false) 33 | { 34 | } 35 | 36 | ErrorPos::ErrorPos(const ErrorPos& that) 37 | :file(that.file), 38 | line(that.line), 39 | error(that.error), 40 | fatal(that.fatal) 41 | { 42 | } 43 | 44 | ErrorPos::ErrorPos(const String8& f, int l, const String8& e, bool fat) 45 | :file(f), 46 | line(l), 47 | error(e), 48 | fatal(fat) 49 | { 50 | } 51 | 52 | ErrorPos::~ErrorPos() 53 | { 54 | } 55 | 56 | bool 57 | ErrorPos::operator<(const ErrorPos& rhs) const 58 | { 59 | if (this->file < rhs.file) return true; 60 | if (this->file == rhs.file) { 61 | if (this->line < rhs.line) return true; 62 | if (this->line == rhs.line) { 63 | if (this->error < rhs.error) return true; 64 | } 65 | } 66 | return false; 67 | } 68 | 69 | bool 70 | ErrorPos::operator==(const ErrorPos& rhs) const 71 | { 72 | return this->file == rhs.file 73 | && this->line == rhs.line 74 | && this->error == rhs.error; 75 | } 76 | 77 | ErrorPos& 78 | ErrorPos::operator=(const ErrorPos& rhs) 79 | { 80 | this->file = rhs.file; 81 | this->line = rhs.line; 82 | this->error = rhs.error; 83 | return *this; 84 | } 85 | 86 | void 87 | ErrorPos::print(FILE* to) const 88 | { 89 | const char* type = fatal ? "error:" : "warning:"; 90 | 91 | if (this->line >= 0) { 92 | fprintf(to, "%s:%d: %s %s\n", this->file.string(), this->line, type, this->error.string()); 93 | } else { 94 | fprintf(to, "%s: %s %s\n", this->file.string(), type, this->error.string()); 95 | } 96 | } 97 | 98 | // SourcePos 99 | // ============================================================================= 100 | SourcePos::SourcePos(const String8& f, int l) 101 | : file(f), line(l) 102 | { 103 | } 104 | 105 | SourcePos::SourcePos(const SourcePos& that) 106 | : file(that.file), line(that.line) 107 | { 108 | } 109 | 110 | SourcePos::SourcePos() 111 | : file("???", 0), line(-1) 112 | { 113 | } 114 | 115 | SourcePos::~SourcePos() 116 | { 117 | } 118 | 119 | int 120 | SourcePos::error(const char* fmt, ...) const 121 | { 122 | int retval=0; 123 | char buf[1024]; 124 | va_list ap; 125 | va_start(ap, fmt); 126 | retval = vsnprintf(buf, sizeof(buf), fmt, ap); 127 | va_end(ap); 128 | char* p = buf + retval - 1; 129 | while (p > buf && *p == '\n') { 130 | *p = '\0'; 131 | p--; 132 | } 133 | g_errors.push_back(ErrorPos(this->file, this->line, String8(buf), true)); 134 | return retval; 135 | } 136 | 137 | int 138 | SourcePos::warning(const char* fmt, ...) const 139 | { 140 | int retval=0; 141 | char buf[1024]; 142 | va_list ap; 143 | va_start(ap, fmt); 144 | retval = vsnprintf(buf, sizeof(buf), fmt, ap); 145 | va_end(ap); 146 | char* p = buf + retval - 1; 147 | while (p > buf && *p == '\n') { 148 | *p = '\0'; 149 | p--; 150 | } 151 | ErrorPos(this->file, this->line, String8(buf), false).print(stderr); 152 | return retval; 153 | } 154 | 155 | bool 156 | SourcePos::hasErrors() 157 | { 158 | return g_errors.size() > 0; 159 | } 160 | 161 | void 162 | SourcePos::printErrors(FILE* to) 163 | { 164 | vector::const_iterator it; 165 | for (it=g_errors.begin(); it!=g_errors.end(); it++) { 166 | it->print(to); 167 | } 168 | } 169 | 170 | 171 | 172 | -------------------------------------------------------------------------------- /utils/BitSet.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 UTILS_BITSET_H 18 | #define UTILS_BITSET_H 19 | 20 | #include 21 | 22 | /* 23 | * Contains some bit manipulation helpers. 24 | */ 25 | 26 | namespace android { 27 | 28 | // A simple set of 32 bits that can be individually marked or cleared. 29 | struct BitSet32 { 30 | uint32_t value; 31 | 32 | inline BitSet32() : value(0) { } 33 | explicit inline BitSet32(uint32_t value) : value(value) { } 34 | 35 | // Gets the value associated with a particular bit index. 36 | static inline uint32_t valueForBit(uint32_t n) { return 0x80000000 >> n; } 37 | 38 | // Clears the bit set. 39 | inline void clear() { value = 0; } 40 | 41 | // Returns the number of marked bits in the set. 42 | inline uint32_t count() const { return __builtin_popcount(value); } 43 | 44 | // Returns true if the bit set does not contain any marked bits. 45 | inline bool isEmpty() const { return ! value; } 46 | 47 | // Returns true if the bit set does not contain any unmarked bits. 48 | inline bool isFull() const { return value == 0xffffffff; } 49 | 50 | // Returns true if the specified bit is marked. 51 | inline bool hasBit(uint32_t n) const { return value & valueForBit(n); } 52 | 53 | // Marks the specified bit. 54 | inline void markBit(uint32_t n) { value |= valueForBit(n); } 55 | 56 | // Clears the specified bit. 57 | inline void clearBit(uint32_t n) { value &= ~ valueForBit(n); } 58 | 59 | // Finds the first marked bit in the set. 60 | // Result is undefined if all bits are unmarked. 61 | inline uint32_t firstMarkedBit() const { return __builtin_clz(value); } 62 | 63 | // Finds the first unmarked bit in the set. 64 | // Result is undefined if all bits are marked. 65 | inline uint32_t firstUnmarkedBit() const { return __builtin_clz(~ value); } 66 | 67 | // Finds the last marked bit in the set. 68 | // Result is undefined if all bits are unmarked. 69 | inline uint32_t lastMarkedBit() const { return 31 - __builtin_ctz(value); } 70 | 71 | // Finds the first marked bit in the set and clears it. Returns the bit index. 72 | // Result is undefined if all bits are unmarked. 73 | inline uint32_t clearFirstMarkedBit() { 74 | uint32_t n = firstMarkedBit(); 75 | clearBit(n); 76 | return n; 77 | } 78 | 79 | // Finds the first unmarked bit in the set and marks it. Returns the bit index. 80 | // Result is undefined if all bits are marked. 81 | inline uint32_t markFirstUnmarkedBit() { 82 | uint32_t n = firstUnmarkedBit(); 83 | markBit(n); 84 | return n; 85 | } 86 | 87 | // Finds the last marked bit in the set and clears it. Returns the bit index. 88 | // Result is undefined if all bits are unmarked. 89 | inline uint32_t clearLastMarkedBit() { 90 | uint32_t n = lastMarkedBit(); 91 | clearBit(n); 92 | return n; 93 | } 94 | 95 | // Gets the index of the specified bit in the set, which is the number of 96 | // marked bits that appear before the specified bit. 97 | inline uint32_t getIndexOfBit(uint32_t n) const { 98 | return __builtin_popcount(value & ~(0xffffffffUL >> n)); 99 | } 100 | 101 | inline bool operator== (const BitSet32& other) const { return value == other.value; } 102 | inline bool operator!= (const BitSet32& other) const { return value != other.value; } 103 | }; 104 | 105 | } // namespace android 106 | 107 | #endif // UTILS_BITSET_H 108 | -------------------------------------------------------------------------------- /host/pseudolocalize.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | static const char* 6 | pseudolocalize_char(char c) 7 | { 8 | switch (c) { 9 | case 'a': return "\xc4\x83"; 10 | case 'b': return "\xcf\x84"; 11 | case 'c': return "\xc4\x8b"; 12 | case 'd': return "\xc4\x8f"; 13 | case 'e': return "\xc4\x99"; 14 | case 'f': return "\xc6\x92"; 15 | case 'g': return "\xc4\x9d"; 16 | case 'h': return "\xd1\x9b"; 17 | case 'i': return "\xcf\x8a"; 18 | case 'j': return "\xc4\xb5"; 19 | case 'k': return "\xc4\xb8"; 20 | case 'l': return "\xc4\xba"; 21 | case 'm': return "\xe1\xb8\xbf"; 22 | case 'n': return "\xd0\xb8"; 23 | case 'o': return "\xcf\x8c"; 24 | case 'p': return "\xcf\x81"; 25 | case 'q': return "\x51"; 26 | case 'r': return "\xd2\x91"; 27 | case 's': return "\xc5\xa1"; 28 | case 't': return "\xd1\x82"; 29 | case 'u': return "\xce\xb0"; 30 | case 'v': return "\x56"; 31 | case 'w': return "\xe1\xba\x85"; 32 | case 'x': return "\xd1\x85"; 33 | case 'y': return "\xe1\xbb\xb3"; 34 | case 'z': return "\xc5\xba"; 35 | case 'A': return "\xc3\x85"; 36 | case 'B': return "\xce\xb2"; 37 | case 'C': return "\xc4\x88"; 38 | case 'D': return "\xc4\x90"; 39 | case 'E': return "\xd0\x84"; 40 | case 'F': return "\xce\x93"; 41 | case 'G': return "\xc4\x9e"; 42 | case 'H': return "\xc4\xa6"; 43 | case 'I': return "\xd0\x87"; 44 | case 'J': return "\xc4\xb5"; 45 | case 'K': return "\xc4\xb6"; 46 | case 'L': return "\xc5\x81"; 47 | case 'M': return "\xe1\xb8\xbe"; 48 | case 'N': return "\xc5\x83"; 49 | case 'O': return "\xce\x98"; 50 | case 'P': return "\xcf\x81"; 51 | case 'Q': return "\x71"; 52 | case 'R': return "\xd0\xaf"; 53 | case 'S': return "\xc8\x98"; 54 | case 'T': return "\xc5\xa6"; 55 | case 'U': return "\xc5\xa8"; 56 | case 'V': return "\xce\xbd"; 57 | case 'W': return "\xe1\xba\x84"; 58 | case 'X': return "\xc3\x97"; 59 | case 'Y': return "\xc2\xa5"; 60 | case 'Z': return "\xc5\xbd"; 61 | default: return NULL; 62 | } 63 | } 64 | 65 | /** 66 | * Converts characters so they look like they've been localized. 67 | * 68 | * Note: This leaves escape sequences untouched so they can later be 69 | * processed by ResTable::collectString in the normal way. 70 | */ 71 | string 72 | pseudolocalize_string(const string& source) 73 | { 74 | const char* s = source.c_str(); 75 | string result; 76 | const size_t I = source.length(); 77 | for (size_t i=0; i 21 | //#include 22 | #include "fakeLog.h" 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #ifdef HAVE_WIN32_THREADS 33 | #include 34 | #endif 35 | 36 | nsecs_t systemTime(int clock) 37 | { 38 | #if defined(HAVE_POSIX_CLOCKS) 39 | static const clockid_t clocks[] = { 40 | CLOCK_REALTIME, 41 | CLOCK_MONOTONIC, 42 | CLOCK_PROCESS_CPUTIME_ID, 43 | CLOCK_THREAD_CPUTIME_ID 44 | }; 45 | struct timespec t; 46 | t.tv_sec = t.tv_nsec = 0; 47 | clock_gettime(clocks[clock], &t); 48 | return nsecs_t(t.tv_sec)*1000000000LL + t.tv_nsec; 49 | #else 50 | // we don't support the clocks here. 51 | struct timeval t; 52 | t.tv_sec = t.tv_usec = 0; 53 | gettimeofday(&t, NULL); 54 | return nsecs_t(t.tv_sec)*1000000000LL + nsecs_t(t.tv_usec)*1000LL; 55 | #endif 56 | } 57 | 58 | int toMillisecondTimeoutDelay(nsecs_t referenceTime, nsecs_t timeoutTime) 59 | { 60 | int timeoutDelayMillis; 61 | if (timeoutTime > referenceTime) { 62 | uint64_t timeoutDelay = uint64_t(timeoutTime - referenceTime); 63 | if (timeoutDelay > uint64_t((INT_MAX - 1) * 1000000LL)) { 64 | timeoutDelayMillis = -1; 65 | } else { 66 | timeoutDelayMillis = (timeoutDelay + 999999LL) / 1000000LL; 67 | } 68 | } else { 69 | timeoutDelayMillis = 0; 70 | } 71 | return timeoutDelayMillis; 72 | } 73 | 74 | 75 | /* 76 | * =========================================================================== 77 | * DurationTimer 78 | * =========================================================================== 79 | */ 80 | 81 | using namespace android; 82 | 83 | // Start the timer. 84 | void DurationTimer::start(void) 85 | { 86 | gettimeofday(&mStartWhen, NULL); 87 | } 88 | 89 | // Stop the timer. 90 | void DurationTimer::stop(void) 91 | { 92 | gettimeofday(&mStopWhen, NULL); 93 | } 94 | 95 | // Get the duration in microseconds. 96 | long long DurationTimer::durationUsecs(void) const 97 | { 98 | return (long) subtractTimevals(&mStopWhen, &mStartWhen); 99 | } 100 | 101 | // Subtract two timevals. Returns the difference (ptv1-ptv2) in 102 | // microseconds. 103 | /*static*/ long long DurationTimer::subtractTimevals(const struct timeval* ptv1, 104 | const struct timeval* ptv2) 105 | { 106 | long long stop = ((long long) ptv1->tv_sec) * 1000000LL + 107 | ((long long) ptv1->tv_usec); 108 | long long start = ((long long) ptv2->tv_sec) * 1000000LL + 109 | ((long long) ptv2->tv_usec); 110 | return stop - start; 111 | } 112 | 113 | // Add the specified amount of time to the timeval. 114 | /*static*/ void DurationTimer::addToTimeval(struct timeval* ptv, long usec) 115 | { 116 | if (usec < 0) { 117 | LOG(LOG_WARN, "", "Negative values not supported in addToTimeval\n"); 118 | return; 119 | } 120 | 121 | // normalize tv_usec if necessary 122 | if (ptv->tv_usec >= 1000000) { 123 | ptv->tv_sec += ptv->tv_usec / 1000000; 124 | ptv->tv_usec %= 1000000; 125 | } 126 | 127 | ptv->tv_usec += usec % 1000000; 128 | if (ptv->tv_usec >= 1000000) { 129 | ptv->tv_usec -= 1000000; 130 | ptv->tv_sec++; 131 | } 132 | ptv->tv_sec += usec / 1000000; 133 | } 134 | 135 | -------------------------------------------------------------------------------- /utils-cpp/TextOutput.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005 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 | 21 | #include 22 | #include 23 | #include 24 | 25 | namespace android { 26 | 27 | // --------------------------------------------------------------------------- 28 | 29 | TextOutput::TextOutput() { 30 | } 31 | 32 | TextOutput::~TextOutput() { 33 | } 34 | 35 | // --------------------------------------------------------------------------- 36 | 37 | TextOutput& operator<<(TextOutput& to, bool val) 38 | { 39 | if (val) to.print("true", 4); 40 | else to.print("false", 5); 41 | return to; 42 | } 43 | 44 | TextOutput& operator<<(TextOutput& to, int val) 45 | { 46 | char buf[16]; 47 | sprintf(buf, "%d", val); 48 | to.print(buf, strlen(buf)); 49 | return to; 50 | } 51 | 52 | TextOutput& operator<<(TextOutput& to, long val) 53 | { 54 | char buf[16]; 55 | sprintf(buf, "%ld", val); 56 | to.print(buf, strlen(buf)); 57 | return to; 58 | } 59 | 60 | TextOutput& operator<<(TextOutput& to, unsigned int val) 61 | { 62 | char buf[16]; 63 | sprintf(buf, "%u", val); 64 | to.print(buf, strlen(buf)); 65 | return to; 66 | } 67 | 68 | TextOutput& operator<<(TextOutput& to, unsigned long val) 69 | { 70 | char buf[16]; 71 | sprintf(buf, "%lu", val); 72 | to.print(buf, strlen(buf)); 73 | return to; 74 | } 75 | 76 | TextOutput& operator<<(TextOutput& to, long long val) 77 | { 78 | char buf[32]; 79 | sprintf(buf, "%Ld", val); 80 | to.print(buf, strlen(buf)); 81 | return to; 82 | } 83 | 84 | TextOutput& operator<<(TextOutput& to, unsigned long long val) 85 | { 86 | char buf[32]; 87 | sprintf(buf, "%Lu", val); 88 | to.print(buf, strlen(buf)); 89 | return to; 90 | } 91 | 92 | static TextOutput& print_float(TextOutput& to, double value) 93 | { 94 | char buf[64]; 95 | sprintf(buf, "%g", value); 96 | if( !strchr(buf, '.') && !strchr(buf, 'e') && 97 | !strchr(buf, 'E') ) { 98 | strncat(buf, ".0", sizeof(buf)-1); 99 | } 100 | to.print(buf, strlen(buf)); 101 | return to; 102 | } 103 | 104 | TextOutput& operator<<(TextOutput& to, float val) 105 | { 106 | return print_float(to,val); 107 | } 108 | 109 | TextOutput& operator<<(TextOutput& to, double val) 110 | { 111 | return print_float(to,val); 112 | } 113 | 114 | TextOutput& operator<<(TextOutput& to, const void* val) 115 | { 116 | char buf[16]; 117 | sprintf(buf, "%p", val); 118 | to.print(buf, strlen(buf)); 119 | return to; 120 | } 121 | 122 | static void textOutputPrinter(void* cookie, const char* txt) 123 | { 124 | ((TextOutput*)cookie)->print(txt, strlen(txt)); 125 | } 126 | 127 | TextOutput& operator<<(TextOutput& to, const TypeCode& val) 128 | { 129 | printTypeCode(val.typeCode(), textOutputPrinter, (void*)&to); 130 | return to; 131 | } 132 | 133 | HexDump::HexDump(const void *buf, size_t size, size_t bytesPerLine) 134 | : mBuffer(buf) 135 | , mSize(size) 136 | , mBytesPerLine(bytesPerLine) 137 | , mSingleLineCutoff(16) 138 | , mAlignment(4) 139 | , mCArrayStyle(false) 140 | { 141 | if (bytesPerLine >= 16) mAlignment = 4; 142 | else if (bytesPerLine >= 8) mAlignment = 2; 143 | else mAlignment = 1; 144 | } 145 | 146 | TextOutput& operator<<(TextOutput& to, const HexDump& val) 147 | { 148 | printHexData(0, val.buffer(), val.size(), val.bytesPerLine(), 149 | val.singleLineCutoff(), val.alignment(), val.carrayStyle(), 150 | textOutputPrinter, (void*)&to); 151 | return to; 152 | } 153 | 154 | }; // namespace android 155 | -------------------------------------------------------------------------------- /system/graphics.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 SYSTEM_CORE_INCLUDE_ANDROID_GRAPHICS_H 18 | #define SYSTEM_CORE_INCLUDE_ANDROID_GRAPHICS_H 19 | 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | /* 25 | * If the HAL needs to create service threads to handle graphics related 26 | * tasks, these threads need to run at HAL_PRIORITY_URGENT_DISPLAY priority 27 | * if they can block the main rendering thread in any way. 28 | * 29 | * the priority of the current thread can be set with: 30 | * 31 | * #include 32 | * setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY); 33 | * 34 | */ 35 | 36 | #define HAL_PRIORITY_URGENT_DISPLAY (-8) 37 | 38 | /** 39 | * pixel format definitions 40 | */ 41 | 42 | enum { 43 | HAL_PIXEL_FORMAT_RGBA_8888 = 1, 44 | HAL_PIXEL_FORMAT_RGBX_8888 = 2, 45 | HAL_PIXEL_FORMAT_RGB_888 = 3, 46 | HAL_PIXEL_FORMAT_RGB_565 = 4, 47 | HAL_PIXEL_FORMAT_BGRA_8888 = 5, 48 | HAL_PIXEL_FORMAT_RGBA_5551 = 6, 49 | HAL_PIXEL_FORMAT_RGBA_4444 = 7, 50 | 51 | /* 0x8 - 0xFF range unavailable */ 52 | 53 | /* 54 | * 0x100 - 0x1FF 55 | * 56 | * This range is reserved for pixel formats that are specific to the HAL 57 | * implementation. Implementations can use any value in this range to 58 | * communicate video pixel formats between their HAL modules. These formats 59 | * must not have an alpha channel. Additionally, an EGLimage created from a 60 | * gralloc buffer of one of these formats must be supported for use with the 61 | * GL_OES_EGL_image_external OpenGL ES extension. 62 | */ 63 | 64 | /* 65 | * Android YUV format: 66 | * 67 | * This format is exposed outside of the HAL to software decoders and 68 | * applications. EGLImageKHR must support it in conjunction with the 69 | * OES_EGL_image_external extension. 70 | * 71 | * YV12 is a 4:2:0 YCrCb planar format comprised of a WxH Y plane followed 72 | * by (W/2) x (H/2) Cr and Cb planes. 73 | * 74 | * This format assumes 75 | * - an even width 76 | * - an even height 77 | * - a horizontal stride multiple of 16 pixels 78 | * - a vertical stride equal to the height 79 | * 80 | * y_size = stride * height 81 | * c_size = ALIGN(stride/2, 16) * height/2 82 | * size = y_size + c_size * 2 83 | * cr_offset = y_size 84 | * cb_offset = y_size + c_size 85 | * 86 | */ 87 | HAL_PIXEL_FORMAT_YV12 = 0x32315659, // YCrCb 4:2:0 Planar 88 | 89 | 90 | 91 | /* Legacy formats (deprecated), used by ImageFormat.java */ 92 | HAL_PIXEL_FORMAT_YCbCr_422_SP = 0x10, // NV16 93 | HAL_PIXEL_FORMAT_YCrCb_420_SP = 0x11, // NV21 94 | HAL_PIXEL_FORMAT_YCbCr_422_I = 0x14, // YUY2 95 | }; 96 | 97 | 98 | /** 99 | * Transformation definitions 100 | * 101 | * IMPORTANT NOTE: 102 | * HAL_TRANSFORM_ROT_90 is applied CLOCKWISE and AFTER HAL_TRANSFORM_FLIP_{H|V}. 103 | * 104 | */ 105 | 106 | enum { 107 | /* flip source image horizontally (around the vertical axis) */ 108 | HAL_TRANSFORM_FLIP_H = 0x01, 109 | /* flip source image vertically (around the horizontal axis)*/ 110 | HAL_TRANSFORM_FLIP_V = 0x02, 111 | /* rotate source image 90 degrees clockwise */ 112 | HAL_TRANSFORM_ROT_90 = 0x04, 113 | /* rotate source image 180 degrees */ 114 | HAL_TRANSFORM_ROT_180 = 0x03, 115 | /* rotate source image 270 degrees clockwise */ 116 | HAL_TRANSFORM_ROT_270 = 0x07, 117 | }; 118 | 119 | #ifdef __cplusplus 120 | } 121 | #endif 122 | 123 | #endif /* SYSTEM_CORE_INCLUDE_ANDROID_GRAPHICS_H */ 124 | -------------------------------------------------------------------------------- /utils/FileMap.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 | // Encapsulate a shared file mapping. 19 | // 20 | #ifndef __LIBS_FILE_MAP_H 21 | #define __LIBS_FILE_MAP_H 22 | 23 | #include 24 | 25 | #include 26 | 27 | #ifdef HAVE_WIN32_FILEMAP 28 | #include 29 | #endif 30 | 31 | namespace android { 32 | 33 | /* 34 | * This represents a memory-mapped file. It might be the entire file or 35 | * only part of it. This requires a little bookkeeping because the mapping 36 | * needs to be aligned on page boundaries, and in some cases we'd like to 37 | * have multiple references to the mapped area without creating additional 38 | * maps. 39 | * 40 | * This always uses MAP_SHARED. 41 | * 42 | * TODO: we should be able to create a new FileMap that is a subset of 43 | * an existing FileMap and shares the underlying mapped pages. Requires 44 | * completing the refcounting stuff and possibly introducing the notion 45 | * of a FileMap hierarchy. 46 | */ 47 | class FileMap { 48 | public: 49 | FileMap(void); 50 | 51 | /* 52 | * Create a new mapping on an open file. 53 | * 54 | * Closing the file descriptor does not unmap the pages, so we don't 55 | * claim ownership of the fd. 56 | * 57 | * Returns "false" on failure. 58 | */ 59 | bool create(const char* origFileName, int fd, 60 | off64_t offset, size_t length, bool readOnly); 61 | 62 | /* 63 | * Return the name of the file this map came from, if known. 64 | */ 65 | const char* getFileName(void) const { return mFileName; } 66 | 67 | /* 68 | * Get a pointer to the piece of the file we requested. 69 | */ 70 | void* getDataPtr(void) const { return mDataPtr; } 71 | 72 | /* 73 | * Get the length we requested. 74 | */ 75 | size_t getDataLength(void) const { return mDataLength; } 76 | 77 | /* 78 | * Get the data offset used to create this map. 79 | */ 80 | off64_t getDataOffset(void) const { return mDataOffset; } 81 | 82 | /* 83 | * Get a "copy" of the object. 84 | */ 85 | FileMap* acquire(void) { mRefCount++; return this; } 86 | 87 | /* 88 | * Call this when mapping is no longer needed. 89 | */ 90 | void release(void) { 91 | if (--mRefCount <= 0) 92 | delete this; 93 | } 94 | 95 | /* 96 | * This maps directly to madvise() values, but allows us to avoid 97 | * including everywhere. 98 | */ 99 | enum MapAdvice { 100 | NORMAL, RANDOM, SEQUENTIAL, WILLNEED, DONTNEED 101 | }; 102 | 103 | /* 104 | * Apply an madvise() call to the entire file. 105 | * 106 | * Returns 0 on success, -1 on failure. 107 | */ 108 | int advise(MapAdvice advice); 109 | 110 | protected: 111 | // don't delete objects; call release() 112 | ~FileMap(void); 113 | 114 | private: 115 | // these are not implemented 116 | FileMap(const FileMap& src); 117 | const FileMap& operator=(const FileMap& src); 118 | 119 | int mRefCount; // reference count 120 | char* mFileName; // original file name, if known 121 | void* mBasePtr; // base of mmap area; page aligned 122 | size_t mBaseLength; // length, measured from "mBasePtr" 123 | off64_t mDataOffset; // offset used when map was created 124 | void* mDataPtr; // start of requested data, offset from base 125 | size_t mDataLength; // length, measured from "mDataPtr" 126 | #ifdef HAVE_WIN32_FILEMAP 127 | HANDLE mFileHandle; // Win32 file handle 128 | HANDLE mFileMapping; // Win32 file mapping handle 129 | #endif 130 | 131 | static long mPageSize; 132 | }; 133 | 134 | }; // namespace android 135 | 136 | #endif // __LIBS_FILE_MAP_H 137 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /utils-cpp/misc.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005 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 | // Miscellaneous utility functions. 19 | // 20 | #include 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | using namespace android; 29 | 30 | namespace android { 31 | 32 | /* 33 | * Like strdup(), but uses C++ "new" operator instead of malloc. 34 | */ 35 | char* strdupNew(const char* str) 36 | { 37 | char* newStr; 38 | int len; 39 | 40 | if (str == NULL) 41 | return NULL; 42 | 43 | len = strlen(str); 44 | newStr = new char[len+1]; 45 | memcpy(newStr, str, len+1); 46 | 47 | return newStr; 48 | } 49 | 50 | /* 51 | * Concatenate an argument vector. 52 | */ 53 | char* concatArgv(int argc, const char* const argv[]) 54 | { 55 | char* newStr = NULL; 56 | int len, totalLen, posn, idx; 57 | 58 | /* 59 | * First, figure out the total length. 60 | */ 61 | totalLen = idx = 0; 62 | while (1) { 63 | if (idx == argc || argv[idx] == NULL) 64 | break; 65 | if (idx) 66 | totalLen++; // leave a space between args 67 | totalLen += strlen(argv[idx]); 68 | idx++; 69 | } 70 | 71 | /* 72 | * Alloc the string. 73 | */ 74 | newStr = new char[totalLen +1]; 75 | if (newStr == NULL) 76 | return NULL; 77 | 78 | /* 79 | * Finally, allocate the string and copy data over. 80 | */ 81 | idx = posn = 0; 82 | while (1) { 83 | if (idx == argc || argv[idx] == NULL) 84 | break; 85 | if (idx) 86 | newStr[posn++] = ' '; 87 | 88 | len = strlen(argv[idx]); 89 | memcpy(&newStr[posn], argv[idx], len); 90 | posn += len; 91 | 92 | idx++; 93 | } 94 | 95 | assert(posn == totalLen); 96 | newStr[posn] = '\0'; 97 | 98 | return newStr; 99 | } 100 | 101 | /* 102 | * Count the #of args in an argument vector. Don't count the final NULL. 103 | */ 104 | int countArgv(const char* const argv[]) 105 | { 106 | int count = 0; 107 | 108 | while (argv[count] != NULL) 109 | count++; 110 | 111 | return count; 112 | } 113 | 114 | 115 | #include 116 | /* 117 | * Get a file's type. 118 | */ 119 | FileType getFileType(const char* fileName) 120 | { 121 | struct stat sb; 122 | 123 | if (stat(fileName, &sb) < 0) { 124 | if (errno == ENOENT || errno == ENOTDIR) 125 | return kFileTypeNonexistent; 126 | else { 127 | fprintf(stderr, "getFileType got errno=%d on '%s'\n", 128 | errno, fileName); 129 | return kFileTypeUnknown; 130 | } 131 | } else { 132 | if (S_ISREG(sb.st_mode)) 133 | return kFileTypeRegular; 134 | else if (S_ISDIR(sb.st_mode)) 135 | return kFileTypeDirectory; 136 | else if (S_ISCHR(sb.st_mode)) 137 | return kFileTypeCharDev; 138 | else if (S_ISBLK(sb.st_mode)) 139 | return kFileTypeBlockDev; 140 | else if (S_ISFIFO(sb.st_mode)) 141 | return kFileTypeFifo; 142 | #ifdef HAVE_SYMLINKS 143 | else if (S_ISLNK(sb.st_mode)) 144 | return kFileTypeSymlink; 145 | else if (S_ISSOCK(sb.st_mode)) 146 | return kFileTypeSocket; 147 | #endif 148 | else 149 | return kFileTypeUnknown; 150 | } 151 | } 152 | 153 | /* 154 | * Get a file's modification date. 155 | */ 156 | time_t getFileModDate(const char* fileName) 157 | { 158 | struct stat sb; 159 | 160 | if (stat(fileName, &sb) < 0) 161 | return (time_t) -1; 162 | 163 | return sb.st_mtime; 164 | } 165 | 166 | /* 167 | * Round up to the next highest power of 2. 168 | * 169 | * Found on http://graphics.stanford.edu/~seander/bithacks.html. 170 | */ 171 | unsigned int roundUpPower2(unsigned int val) 172 | { 173 | val--; 174 | val |= val >> 1; 175 | val |= val >> 2; 176 | val |= val >> 4; 177 | val |= val >> 8; 178 | val |= val >> 16; 179 | val++; 180 | 181 | return val; 182 | } 183 | 184 | }; // namespace android 185 | 186 | -------------------------------------------------------------------------------- /utils/Timers.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005 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 | // Timer functions. 19 | // 20 | #ifndef _LIBS_UTILS_TIMERS_H 21 | #define _LIBS_UTILS_TIMERS_H 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | // ------------------------------------------------------------------ 28 | // C API 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | 34 | typedef int64_t nsecs_t; // nano-seconds 35 | 36 | static inline nsecs_t seconds_to_nanoseconds(nsecs_t secs) 37 | { 38 | return secs*1000000000; 39 | } 40 | 41 | static inline nsecs_t milliseconds_to_nanoseconds(nsecs_t secs) 42 | { 43 | return secs*1000000; 44 | } 45 | 46 | static inline nsecs_t microseconds_to_nanoseconds(nsecs_t secs) 47 | { 48 | return secs*1000; 49 | } 50 | 51 | static inline nsecs_t nanoseconds_to_seconds(nsecs_t secs) 52 | { 53 | return secs/1000000000; 54 | } 55 | 56 | static inline nsecs_t nanoseconds_to_milliseconds(nsecs_t secs) 57 | { 58 | return secs/1000000; 59 | } 60 | 61 | static inline nsecs_t nanoseconds_to_microseconds(nsecs_t secs) 62 | { 63 | return secs/1000; 64 | } 65 | 66 | static inline nsecs_t s2ns(nsecs_t v) {return seconds_to_nanoseconds(v);} 67 | static inline nsecs_t ms2ns(nsecs_t v) {return milliseconds_to_nanoseconds(v);} 68 | static inline nsecs_t us2ns(nsecs_t v) {return microseconds_to_nanoseconds(v);} 69 | static inline nsecs_t ns2s(nsecs_t v) {return nanoseconds_to_seconds(v);} 70 | static inline nsecs_t ns2ms(nsecs_t v) {return nanoseconds_to_milliseconds(v);} 71 | static inline nsecs_t ns2us(nsecs_t v) {return nanoseconds_to_microseconds(v);} 72 | 73 | static inline nsecs_t seconds(nsecs_t v) { return s2ns(v); } 74 | static inline nsecs_t milliseconds(nsecs_t v) { return ms2ns(v); } 75 | static inline nsecs_t microseconds(nsecs_t v) { return us2ns(v); } 76 | 77 | enum { 78 | SYSTEM_TIME_REALTIME = 0, // system-wide realtime clock 79 | SYSTEM_TIME_MONOTONIC = 1, // monotonic time since unspecified starting point 80 | SYSTEM_TIME_PROCESS = 2, // high-resolution per-process clock 81 | SYSTEM_TIME_THREAD = 3 // high-resolution per-thread clock 82 | }; 83 | 84 | // return the system-time according to the specified clock 85 | #ifdef __cplusplus 86 | nsecs_t systemTime(int clock = SYSTEM_TIME_MONOTONIC); 87 | #else 88 | nsecs_t systemTime(int clock); 89 | #endif // def __cplusplus 90 | 91 | /** 92 | * Returns the number of milliseconds to wait between the reference time and the timeout time. 93 | * If the timeout is in the past relative to the reference time, returns 0. 94 | * If the timeout is more than INT_MAX milliseconds in the future relative to the reference time, 95 | * such as when timeoutTime == LLONG_MAX, returns -1 to indicate an infinite timeout delay. 96 | * Otherwise, returns the difference between the reference time and timeout time 97 | * rounded up to the next millisecond. 98 | */ 99 | int toMillisecondTimeoutDelay(nsecs_t referenceTime, nsecs_t timeoutTime); 100 | 101 | #ifdef __cplusplus 102 | } // extern "C" 103 | #endif 104 | 105 | // ------------------------------------------------------------------ 106 | // C++ API 107 | 108 | #ifdef __cplusplus 109 | 110 | namespace android { 111 | /* 112 | * Time the duration of something. 113 | * 114 | * Includes some timeval manipulation functions. 115 | */ 116 | class DurationTimer { 117 | public: 118 | DurationTimer() {} 119 | ~DurationTimer() {} 120 | 121 | // Start the timer. 122 | void start(); 123 | // Stop the timer. 124 | void stop(); 125 | // Get the duration in microseconds. 126 | long long durationUsecs() const; 127 | 128 | // Subtract two timevals. Returns the difference (ptv1-ptv2) in 129 | // microseconds. 130 | static long long subtractTimevals(const struct timeval* ptv1, 131 | const struct timeval* ptv2); 132 | 133 | // Add the specified amount of time to the timeval. 134 | static void addToTimeval(struct timeval* ptv, long usec); 135 | 136 | private: 137 | struct timeval mStartWhen; 138 | struct timeval mStopWhen; 139 | }; 140 | 141 | }; // android 142 | #endif // def __cplusplus 143 | 144 | #endif // _LIBS_UTILS_TIMERS_H 145 | -------------------------------------------------------------------------------- /utils/AssetDir.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 | // Access a chunk of the asset hierarchy as if it were a single directory. 19 | // 20 | #ifndef __LIBS_ASSETDIR_H 21 | #define __LIBS_ASSETDIR_H 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | namespace android { 30 | 31 | /* 32 | * This provides vector-style access to a directory. We do this rather 33 | * than modeling opendir/readdir access because it's simpler and the 34 | * nature of the operation requires us to have all data on hand anyway. 35 | * 36 | * The list of files will be sorted in ascending order by ASCII value. 37 | * 38 | * The contents are populated by our friend, the AssetManager. 39 | */ 40 | class AssetDir { 41 | public: 42 | AssetDir(void) 43 | : mFileInfo(NULL) 44 | {} 45 | virtual ~AssetDir(void) { 46 | delete mFileInfo; 47 | } 48 | 49 | /* 50 | * Vector-style access. 51 | */ 52 | size_t getFileCount(void) { return mFileInfo->size(); } 53 | const String8& getFileName(int idx) { 54 | return mFileInfo->itemAt(idx).getFileName(); 55 | } 56 | const String8& getSourceName(int idx) { 57 | return mFileInfo->itemAt(idx).getSourceName(); 58 | } 59 | 60 | /* 61 | * Get the type of a file (usually regular or directory). 62 | */ 63 | FileType getFileType(int idx) { 64 | return mFileInfo->itemAt(idx).getFileType(); 65 | } 66 | 67 | private: 68 | /* these operations are not implemented */ 69 | AssetDir(const AssetDir& src); 70 | const AssetDir& operator=(const AssetDir& src); 71 | 72 | friend class AssetManager; 73 | 74 | /* 75 | * This holds information about files in the asset hierarchy. 76 | */ 77 | class FileInfo { 78 | public: 79 | FileInfo(void) {} 80 | FileInfo(const String8& path) // useful for e.g. svect.indexOf 81 | : mFileName(path), mFileType(kFileTypeUnknown) 82 | {} 83 | ~FileInfo(void) {} 84 | FileInfo(const FileInfo& src) { 85 | copyMembers(src); 86 | } 87 | const FileInfo& operator= (const FileInfo& src) { 88 | if (this != &src) 89 | copyMembers(src); 90 | return *this; 91 | } 92 | 93 | void copyMembers(const FileInfo& src) { 94 | mFileName = src.mFileName; 95 | mFileType = src.mFileType; 96 | mSourceName = src.mSourceName; 97 | } 98 | 99 | /* need this for SortedVector; must compare only on file name */ 100 | bool operator< (const FileInfo& rhs) const { 101 | return mFileName < rhs.mFileName; 102 | } 103 | 104 | /* used by AssetManager */ 105 | bool operator== (const FileInfo& rhs) const { 106 | return mFileName == rhs.mFileName; 107 | } 108 | 109 | void set(const String8& path, FileType type) { 110 | mFileName = path; 111 | mFileType = type; 112 | } 113 | 114 | const String8& getFileName(void) const { return mFileName; } 115 | void setFileName(const String8& path) { mFileName = path; } 116 | 117 | FileType getFileType(void) const { return mFileType; } 118 | void setFileType(FileType type) { mFileType = type; } 119 | 120 | const String8& getSourceName(void) const { return mSourceName; } 121 | void setSourceName(const String8& path) { mSourceName = path; } 122 | 123 | /* 124 | * Handy utility for finding an entry in a sorted vector of FileInfo. 125 | * Returns the index of the matching entry, or -1 if none found. 126 | */ 127 | static int findEntry(const SortedVector* pVector, 128 | const String8& fileName); 129 | 130 | private: 131 | String8 mFileName; // filename only 132 | FileType mFileType; // regular, directory, etc 133 | 134 | String8 mSourceName; // currently debug-only 135 | }; 136 | 137 | /* AssetManager uses this to initialize us */ 138 | void setFileList(SortedVector* list) { mFileInfo = list; } 139 | 140 | SortedVector* mFileInfo; 141 | }; 142 | 143 | }; // namespace android 144 | 145 | #endif // __LIBS_ASSETDIR_H 146 | -------------------------------------------------------------------------------- /utils/BackupHelpers.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 _UTILS_BACKUP_HELPERS_H 18 | #define _UTILS_BACKUP_HELPERS_H 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | namespace android { 25 | 26 | enum { 27 | BACKUP_HEADER_ENTITY_V1 = 0x61746144, // Data (little endian) 28 | }; 29 | 30 | typedef struct { 31 | int type; // BACKUP_HEADER_ENTITY_V1 32 | int keyLen; // length of the key name, not including the null terminator 33 | int dataSize; // size of the data, not including the padding, -1 means delete 34 | } entity_header_v1; 35 | 36 | struct SnapshotHeader { 37 | int magic0; 38 | int fileCount; 39 | int magic1; 40 | int totalSize; 41 | }; 42 | 43 | struct FileState { 44 | int modTime_sec; 45 | int modTime_nsec; 46 | int mode; 47 | int size; 48 | int crc32; 49 | int nameLen; 50 | }; 51 | 52 | struct FileRec { 53 | String8 file; 54 | bool deleted; 55 | FileState s; 56 | }; 57 | 58 | 59 | /** 60 | * Writes the data. 61 | * 62 | * If an error occurs, it poisons this object and all write calls will fail 63 | * with the error that occurred. 64 | */ 65 | class BackupDataWriter 66 | { 67 | public: 68 | BackupDataWriter(int fd); 69 | // does not close fd 70 | ~BackupDataWriter(); 71 | 72 | status_t WriteEntityHeader(const String8& key, size_t dataSize); 73 | 74 | /* Note: WriteEntityData will write arbitrary data into the file without 75 | * validation or a previously-supplied header. The full backup implementation 76 | * uses it this way to generate a controlled binary stream that is not 77 | * entity-structured. If the implementation here is changed, either this 78 | * use case must remain valid, or the full backup implementation should be 79 | * adjusted to use some other appropriate mechanism. 80 | */ 81 | status_t WriteEntityData(const void* data, size_t size); 82 | 83 | void SetKeyPrefix(const String8& keyPrefix); 84 | 85 | private: 86 | explicit BackupDataWriter(); 87 | status_t write_padding_for(int n); 88 | 89 | int m_fd; 90 | status_t m_status; 91 | ssize_t m_pos; 92 | int m_entityCount; 93 | String8 m_keyPrefix; 94 | }; 95 | 96 | /** 97 | * Reads the data. 98 | * 99 | * If an error occurs, it poisons this object and all write calls will fail 100 | * with the error that occurred. 101 | */ 102 | class BackupDataReader 103 | { 104 | public: 105 | BackupDataReader(int fd); 106 | // does not close fd 107 | ~BackupDataReader(); 108 | 109 | status_t Status(); 110 | status_t ReadNextHeader(bool* done, int* type); 111 | 112 | bool HasEntities(); 113 | status_t ReadEntityHeader(String8* key, size_t* dataSize); 114 | status_t SkipEntityData(); // must be called with the pointer at the beginning of the data. 115 | ssize_t ReadEntityData(void* data, size_t size); 116 | 117 | private: 118 | explicit BackupDataReader(); 119 | status_t skip_padding(); 120 | 121 | int m_fd; 122 | bool m_done; 123 | status_t m_status; 124 | ssize_t m_pos; 125 | ssize_t m_dataEndPos; 126 | int m_entityCount; 127 | union { 128 | int type; 129 | entity_header_v1 entity; 130 | } m_header; 131 | String8 m_key; 132 | }; 133 | 134 | int back_up_files(int oldSnapshotFD, BackupDataWriter* dataStream, int newSnapshotFD, 135 | char const* const* files, char const* const *keys, int fileCount); 136 | 137 | int write_tarfile(const String8& packageName, const String8& domain, 138 | const String8& rootPath, const String8& filePath, BackupDataWriter* outputStream); 139 | 140 | class RestoreHelperBase 141 | { 142 | public: 143 | RestoreHelperBase(); 144 | ~RestoreHelperBase(); 145 | 146 | status_t WriteFile(const String8& filename, BackupDataReader* in); 147 | status_t WriteSnapshot(int fd); 148 | 149 | private: 150 | void* m_buf; 151 | bool m_loggedUnknownMetadata; 152 | KeyedVector m_files; 153 | }; 154 | 155 | #define TEST_BACKUP_HELPERS 1 156 | 157 | #if TEST_BACKUP_HELPERS 158 | int backup_helper_test_empty(); 159 | int backup_helper_test_four(); 160 | int backup_helper_test_files(); 161 | int backup_helper_test_null_base(); 162 | int backup_helper_test_missing_file(); 163 | int backup_helper_test_data_writer(); 164 | int backup_helper_test_data_reader(); 165 | #endif 166 | 167 | } // namespace android 168 | 169 | #endif // _UTILS_BACKUP_HELPERS_H 170 | -------------------------------------------------------------------------------- /utils/SharedBuffer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005 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_SHARED_BUFFER_H 18 | #define ANDROID_SHARED_BUFFER_H 19 | 20 | #include 21 | #include 22 | 23 | // --------------------------------------------------------------------------- 24 | 25 | namespace android { 26 | 27 | class SharedBuffer 28 | { 29 | public: 30 | 31 | /* flags to use with release() */ 32 | enum { 33 | eKeepStorage = 0x00000001 34 | }; 35 | 36 | /*! allocate a buffer of size 'size' and acquire() it. 37 | * call release() to free it. 38 | */ 39 | static SharedBuffer* alloc(size_t size); 40 | 41 | /*! free the memory associated with the SharedBuffer. 42 | * Fails if there are any users associated with this SharedBuffer. 43 | * In other words, the buffer must have been release by all its 44 | * users. 45 | */ 46 | static ssize_t dealloc(const SharedBuffer* released); 47 | 48 | //! get the SharedBuffer from the data pointer 49 | static inline const SharedBuffer* sharedBuffer(const void* data); 50 | 51 | //! access the data for read 52 | inline const void* data() const; 53 | 54 | //! access the data for read/write 55 | inline void* data(); 56 | 57 | //! get size of the buffer 58 | inline size_t size() const; 59 | 60 | //! get back a SharedBuffer object from its data 61 | static inline SharedBuffer* bufferFromData(void* data); 62 | 63 | //! get back a SharedBuffer object from its data 64 | static inline const SharedBuffer* bufferFromData(const void* data); 65 | 66 | //! get the size of a SharedBuffer object from its data 67 | static inline size_t sizeFromData(const void* data); 68 | 69 | //! edit the buffer (get a writtable, or non-const, version of it) 70 | SharedBuffer* edit() const; 71 | 72 | //! edit the buffer, resizing if needed 73 | SharedBuffer* editResize(size_t size) const; 74 | 75 | //! like edit() but fails if a copy is required 76 | SharedBuffer* attemptEdit() const; 77 | 78 | //! resize and edit the buffer, loose it's content. 79 | SharedBuffer* reset(size_t size) const; 80 | 81 | //! acquire/release a reference on this buffer 82 | void acquire() const; 83 | 84 | /*! release a reference on this buffer, with the option of not 85 | * freeing the memory associated with it if it was the last reference 86 | * returns the previous reference count 87 | */ 88 | int32_t release(uint32_t flags = 0) const; 89 | 90 | //! returns wether or not we're the only owner 91 | inline bool onlyOwner() const; 92 | 93 | 94 | private: 95 | inline SharedBuffer() { } 96 | inline ~SharedBuffer() { } 97 | inline SharedBuffer(const SharedBuffer&); 98 | 99 | // 16 bytes. must be sized to preserve correct alingment. 100 | mutable int32_t mRefs; 101 | size_t mSize; 102 | uint32_t mReserved[2]; 103 | }; 104 | 105 | // --------------------------------------------------------------------------- 106 | 107 | const SharedBuffer* SharedBuffer::sharedBuffer(const void* data) { 108 | return data ? reinterpret_cast(data)-1 : 0; 109 | } 110 | 111 | const void* SharedBuffer::data() const { 112 | return this + 1; 113 | } 114 | 115 | void* SharedBuffer::data() { 116 | return this + 1; 117 | } 118 | 119 | size_t SharedBuffer::size() const { 120 | return mSize; 121 | } 122 | 123 | SharedBuffer* SharedBuffer::bufferFromData(void* data) 124 | { 125 | return ((SharedBuffer*)data)-1; 126 | } 127 | 128 | const SharedBuffer* SharedBuffer::bufferFromData(const void* data) 129 | { 130 | return ((const SharedBuffer*)data)-1; 131 | } 132 | 133 | size_t SharedBuffer::sizeFromData(const void* data) 134 | { 135 | return (((const SharedBuffer*)data)-1)->mSize; 136 | } 137 | 138 | bool SharedBuffer::onlyOwner() const { 139 | return (mRefs == 1); 140 | } 141 | 142 | }; // namespace android 143 | 144 | // --------------------------------------------------------------------------- 145 | 146 | #endif // ANDROID_VECTOR_H 147 | --------------------------------------------------------------------------------