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_TAG "backup_data"
18 
19 #include <androidfw/BackupHelpers.h>
20 #include <utils/ByteOrder.h>
21 
22 #include <stdio.h>
23 #include <string.h>
24 #include <unistd.h>
25 
26 #include <cutils/log.h>
27 
28 namespace android {
29 
30 static const bool kIsDebug = false;
31 
32 /*
33  * File Format (v1):
34  *
35  * All ints are stored little-endian.
36  *
37  *  - An app_header_v1 struct.
38  *  - The name of the package, utf-8, null terminated, padded to 4-byte boundary.
39  *  - A sequence of zero or more key/value paires (entities), each with
40  *      - A entity_header_v1 struct
41  *      - The key, utf-8, null terminated, padded to 4-byte boundary.
42  *      - The value, padded to 4 byte boundary
43  */
44 
45 const static int ROUND_UP[4] = { 0, 3, 2, 1 };
46 
47 static inline size_t
padding_extra(size_t n)48 padding_extra(size_t n)
49 {
50     return ROUND_UP[n % 4];
51 }
52 
BackupDataWriter(int fd)53 BackupDataWriter::BackupDataWriter(int fd)
54     :m_fd(fd),
55      m_status(NO_ERROR),
56      m_entityCount(0)
57 {
58     m_pos = (ssize_t) lseek(fd, 0, SEEK_CUR);
59     if (kIsDebug) ALOGI("BackupDataWriter(%d) @ %ld", fd, (long)m_pos);
60 }
61 
~BackupDataWriter()62 BackupDataWriter::~BackupDataWriter()
63 {
64 }
65 
66 // Pad out anything they've previously written to the next 4 byte boundary.
67 status_t
write_padding_for(int n)68 BackupDataWriter::write_padding_for(int n)
69 {
70     ssize_t amt;
71     ssize_t paddingSize;
72 
73     paddingSize = padding_extra(n);
74     if (paddingSize > 0) {
75         uint32_t padding = 0xbcbcbcbc;
76         if (kIsDebug) ALOGI("writing %zd padding bytes for %d", paddingSize, n);
77         amt = write(m_fd, &padding, paddingSize);
78         if (amt != paddingSize) {
79             m_status = errno;
80             return m_status;
81         }
82         m_pos += amt;
83     }
84     return NO_ERROR;
85 }
86 
87 status_t
WriteEntityHeader(const String8 & key,size_t dataSize)88 BackupDataWriter::WriteEntityHeader(const String8& key, size_t dataSize)
89 {
90     if (m_status != NO_ERROR) {
91         return m_status;
92     }
93 
94     ssize_t amt;
95 
96     amt = write_padding_for(m_pos);
97     if (amt != 0) {
98         return amt;
99     }
100 
101     String8 k;
102     if (m_keyPrefix.length() > 0) {
103         k = m_keyPrefix;
104         k += ":";
105         k += key;
106     } else {
107         k = key;
108     }
109     if (kIsDebug) {
110         ALOGD("Writing header: prefix='%s' key='%s' dataSize=%zu", m_keyPrefix.string(),
111                 key.string(), dataSize);
112     }
113 
114     entity_header_v1 header;
115     ssize_t keyLen;
116 
117     keyLen = k.length();
118 
119     header.type = tolel(BACKUP_HEADER_ENTITY_V1);
120     header.keyLen = tolel(keyLen);
121     header.dataSize = tolel(dataSize);
122 
123     if (kIsDebug) ALOGI("writing entity header, %zu bytes", sizeof(entity_header_v1));
124     amt = write(m_fd, &header, sizeof(entity_header_v1));
125     if (amt != sizeof(entity_header_v1)) {
126         m_status = errno;
127         return m_status;
128     }
129     m_pos += amt;
130 
131     if (kIsDebug) ALOGI("writing entity header key, %zd bytes", keyLen+1);
132     amt = write(m_fd, k.string(), keyLen+1);
133     if (amt != keyLen+1) {
134         m_status = errno;
135         return m_status;
136     }
137     m_pos += amt;
138 
139     amt = write_padding_for(keyLen+1);
140 
141     m_entityCount++;
142 
143     return amt;
144 }
145 
146 status_t
WriteEntityData(const void * data,size_t size)147 BackupDataWriter::WriteEntityData(const void* data, size_t size)
148 {
149     if (kIsDebug) ALOGD("Writing data: size=%lu", (unsigned long) size);
150 
151     if (m_status != NO_ERROR) {
152         if (kIsDebug) {
153             ALOGD("Not writing data - stream in error state %d (%s)", m_status, strerror(m_status));
154         }
155         return m_status;
156     }
157 
158     // We don't write padding here, because they're allowed to call this several
159     // times with smaller buffers.  We write it at the end of WriteEntityHeader
160     // instead.
161     ssize_t amt = write(m_fd, data, size);
162     if (amt != (ssize_t)size) {
163         m_status = errno;
164         if (kIsDebug) ALOGD("write returned error %d (%s)", m_status, strerror(m_status));
165         return m_status;
166     }
167     m_pos += amt;
168     return NO_ERROR;
169 }
170 
171 void
SetKeyPrefix(const String8 & keyPrefix)172 BackupDataWriter::SetKeyPrefix(const String8& keyPrefix)
173 {
174     m_keyPrefix = keyPrefix;
175 }
176 
177 
BackupDataReader(int fd)178 BackupDataReader::BackupDataReader(int fd)
179     :m_fd(fd),
180      m_done(false),
181      m_status(NO_ERROR),
182      m_entityCount(0)
183 {
184     memset(&m_header, 0, sizeof(m_header));
185     m_pos = (ssize_t) lseek(fd, 0, SEEK_CUR);
186     if (kIsDebug) ALOGI("BackupDataReader(%d) @ %ld", fd, (long)m_pos);
187 }
188 
~BackupDataReader()189 BackupDataReader::~BackupDataReader()
190 {
191 }
192 
193 status_t
Status()194 BackupDataReader::Status()
195 {
196     return m_status;
197 }
198 
199 #define CHECK_SIZE(actual, expected) \
200     do { \
201         if ((actual) != (expected)) { \
202             if ((actual) == 0) { \
203                 m_status = EIO; \
204                 m_done = true; \
205             } else { \
206                 m_status = errno; \
207                 ALOGD("CHECK_SIZE(a=%ld e=%ld) failed at line %d m_status='%s'", \
208                     long(actual), long(expected), __LINE__, strerror(m_status)); \
209             } \
210             return m_status; \
211         } \
212     } while(0)
213 #define SKIP_PADDING() \
214     do { \
215         status_t err = skip_padding(); \
216         if (err != NO_ERROR) { \
217             ALOGD("SKIP_PADDING FAILED at line %d", __LINE__); \
218             m_status = err; \
219             return err; \
220         } \
221     } while(0)
222 
223 status_t
ReadNextHeader(bool * done,int * type)224 BackupDataReader::ReadNextHeader(bool* done, int* type)
225 {
226     *done = m_done;
227     if (m_status != NO_ERROR) {
228         return m_status;
229     }
230 
231     int amt;
232 
233     amt = skip_padding();
234     if (amt == EIO) {
235         *done = m_done = true;
236         return NO_ERROR;
237     }
238     else if (amt != NO_ERROR) {
239         return amt;
240     }
241     amt = read(m_fd, &m_header, sizeof(m_header));
242     *done = m_done = (amt == 0);
243     if (*done) {
244         return NO_ERROR;
245     }
246     CHECK_SIZE(amt, sizeof(m_header));
247     m_pos += sizeof(m_header);
248     if (type) {
249         *type = m_header.type;
250     }
251 
252     // validate and fix up the fields.
253     m_header.type = fromlel(m_header.type);
254     switch (m_header.type)
255     {
256         case BACKUP_HEADER_ENTITY_V1:
257         {
258             m_header.entity.keyLen = fromlel(m_header.entity.keyLen);
259             if (m_header.entity.keyLen <= 0) {
260                 ALOGD("Entity header at %d has keyLen<=0: 0x%08x\n", (int)m_pos,
261                         (int)m_header.entity.keyLen);
262                 m_status = EINVAL;
263             }
264             m_header.entity.dataSize = fromlel(m_header.entity.dataSize);
265             m_entityCount++;
266 
267             // read the rest of the header (filename)
268             size_t size = m_header.entity.keyLen;
269             char* buf = m_key.lockBuffer(size);
270             if (buf == NULL) {
271                 m_status = ENOMEM;
272                 return m_status;
273             }
274             int amt = read(m_fd, buf, size+1);
275             CHECK_SIZE(amt, (int)size+1);
276             m_key.unlockBuffer(size);
277             m_pos += size+1;
278             SKIP_PADDING();
279             m_dataEndPos = m_pos + m_header.entity.dataSize;
280 
281             break;
282         }
283         default:
284             ALOGD("Chunk header at %d has invalid type: 0x%08x",
285                     (int)(m_pos - sizeof(m_header)), (int)m_header.type);
286             m_status = EINVAL;
287     }
288 
289     return m_status;
290 }
291 
292 bool
HasEntities()293 BackupDataReader::HasEntities()
294 {
295     return m_status == NO_ERROR && m_header.type == BACKUP_HEADER_ENTITY_V1;
296 }
297 
298 status_t
ReadEntityHeader(String8 * key,size_t * dataSize)299 BackupDataReader::ReadEntityHeader(String8* key, size_t* dataSize)
300 {
301     if (m_status != NO_ERROR) {
302         return m_status;
303     }
304     if (m_header.type != BACKUP_HEADER_ENTITY_V1) {
305         return EINVAL;
306     }
307     *key = m_key;
308     *dataSize = m_header.entity.dataSize;
309     return NO_ERROR;
310 }
311 
312 status_t
SkipEntityData()313 BackupDataReader::SkipEntityData()
314 {
315     if (m_status != NO_ERROR) {
316         return m_status;
317     }
318     if (m_header.type != BACKUP_HEADER_ENTITY_V1) {
319         return EINVAL;
320     }
321     if (m_header.entity.dataSize > 0) {
322         int pos = lseek(m_fd, m_dataEndPos, SEEK_SET);
323         if (pos == -1) {
324             return errno;
325         }
326         m_pos = pos;
327     }
328     SKIP_PADDING();
329     return NO_ERROR;
330 }
331 
332 ssize_t
ReadEntityData(void * data,size_t size)333 BackupDataReader::ReadEntityData(void* data, size_t size)
334 {
335     if (m_status != NO_ERROR) {
336         return -1;
337     }
338     int remaining = m_dataEndPos - m_pos;
339     if (kIsDebug) {
340         ALOGD("ReadEntityData size=%zu m_pos=0x%zx m_dataEndPos=0x%zx remaining=%d\n",
341                 size, m_pos, m_dataEndPos, remaining);
342     }
343     if (remaining <= 0) {
344         return 0;
345     }
346     if (((int)size) > remaining) {
347         size = remaining;
348     }
349     if (kIsDebug) {
350         ALOGD("   reading %zu bytes", size);
351     }
352     int amt = read(m_fd, data, size);
353     if (amt < 0) {
354         m_status = errno;
355         return -1;
356     }
357     if (amt == 0) {
358         m_status = EIO;
359         m_done = true;
360     }
361     m_pos += amt;
362     return amt;
363 }
364 
365 status_t
skip_padding()366 BackupDataReader::skip_padding()
367 {
368     ssize_t amt;
369     ssize_t paddingSize;
370 
371     paddingSize = padding_extra(m_pos);
372     if (paddingSize > 0) {
373         uint32_t padding;
374         amt = read(m_fd, &padding, paddingSize);
375         CHECK_SIZE(amt, paddingSize);
376         m_pos += amt;
377     }
378     return NO_ERROR;
379 }
380 
381 
382 } // namespace android
383