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 //#define LOG_NDEBUG 0
18 #define LOG_TAG "FoundationUtils"
19 #include <utils/Log.h>
20 #include <ctype.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 
26 #include <cutils/properties.h>
27 #include <media/stagefright/foundation/AString.h>
28 
29 namespace android {
30 
uriDebugString(const AString & uri,bool incognito)31 AString uriDebugString(const AString &uri, bool incognito) {
32     if (incognito) {
33         return AString("<URI suppressed>");
34     }
35 
36     if (property_get_bool("media.stagefright.log-uri", false)) {
37         return uri;
38     }
39 
40     // find scheme
41     AString scheme;
42     const char *chars = uri.c_str();
43     for (size_t i = 0; i < uri.size(); i++) {
44         const char c = chars[i];
45         if (!isascii(c)) {
46             break;
47         } else if (isalpha(c)) {
48             continue;
49         } else if (i == 0) {
50             // first character must be a letter
51             break;
52         } else if (isdigit(c) || c == '+' || c == '.' || c =='-') {
53             continue;
54         } else if (c != ':') {
55             break;
56         }
57         scheme = AString(uri, 0, i);
58         scheme.append("://<suppressed>");
59         return scheme;
60     }
61     return AString("<no-scheme URI suppressed>");
62 }
63 
MakeUserAgent()64 AString MakeUserAgent() {
65     AString ua;
66     ua.append("stagefright/1.2 (Linux;Android ");
67 
68 #if (PROPERTY_VALUE_MAX < 8)
69 #error "PROPERTY_VALUE_MAX must be at least 8"
70 #endif
71 
72     char value[PROPERTY_VALUE_MAX];
73     property_get("ro.build.version.release", value, "Unknown");
74     ua.append(value);
75     ua.append(")");
76 
77     return ua;
78 }
79 
nameForFd(int fd)80 AString nameForFd(int fd) {
81     const size_t SIZE = 256;
82     char buffer[SIZE];
83     AString result;
84     snprintf(buffer, SIZE, "/proc/%d/fd/%d", getpid(), fd);
85     struct stat s;
86     if (lstat(buffer, &s) == 0) {
87         if ((s.st_mode & S_IFMT) == S_IFLNK) {
88             char linkto[256];
89             int len = readlink(buffer, linkto, sizeof(linkto));
90             if(len > 0) {
91                 if(len > 255) {
92                     linkto[252] = '.';
93                     linkto[253] = '.';
94                     linkto[254] = '.';
95                     linkto[255] = 0;
96                 } else {
97                     linkto[len] = 0;
98                 }
99                 result.append(linkto);
100             }
101         } else {
102             result.append("unexpected type for ");
103             result.append(buffer);
104         }
105     } else {
106         result.append("couldn't open ");
107         result.append(buffer);
108     }
109     return result;
110 }
111 
112 }  // namespace android
113