Dieter Baron | bbb6369 | 1999-07-26 22:04:20 +0000 | [diff] [blame] | 1 | /* |
Thomas Klausner | 2d05111 | 2006-04-23 14:51:46 +0000 | [diff] [blame] | 2 | $NiH: zip_file_get_offset.c,v 1.3 2004/11/17 21:55:11 wiz Exp $ |
Dieter Baron | 1c5ffe2 | 2002-06-06 09:27:17 +0000 | [diff] [blame] | 3 | |
Dieter Baron | b2ed74d | 2004-04-14 14:01:31 +0000 | [diff] [blame] | 4 | zip_file_get_offset.c -- get offset of file data in archive. |
| 5 | Copyright (C) 1999, 2003, 2004 Dieter Baron and Thomas Klausner |
Dieter Baron | bbb6369 | 1999-07-26 22:04:20 +0000 | [diff] [blame] | 6 | |
Dieter Baron | dd9afca | 2003-10-02 14:13:37 +0000 | [diff] [blame] | 7 | This file is part of libzip, a library to manipulate ZIP archives. |
Dieter Baron | bbb6369 | 1999-07-26 22:04:20 +0000 | [diff] [blame] | 8 | The authors can be contacted at <nih@giga.or.at> |
| 9 | |
Dieter Baron | dd9afca | 2003-10-02 14:13:37 +0000 | [diff] [blame] | 10 | Redistribution and use in source and binary forms, with or without |
| 11 | modification, are permitted provided that the following conditions |
| 12 | are met: |
| 13 | 1. Redistributions of source code must retain the above copyright |
| 14 | notice, this list of conditions and the following disclaimer. |
| 15 | 2. Redistributions in binary form must reproduce the above copyright |
| 16 | notice, this list of conditions and the following disclaimer in |
| 17 | the documentation and/or other materials provided with the |
| 18 | distribution. |
| 19 | 3. The names of the authors may not be used to endorse or promote |
| 20 | products derived from this software without specific prior |
| 21 | written permission. |
| 22 | |
| 23 | THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS |
| 24 | OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 25 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 26 | ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY |
| 27 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 28 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 29 | GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 30 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 31 | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 32 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 33 | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Dieter Baron | bbb6369 | 1999-07-26 22:04:20 +0000 | [diff] [blame] | 34 | */ |
| 35 | |
| 36 | |
| 37 | |
Dieter Baron | b2ed74d | 2004-04-14 14:01:31 +0000 | [diff] [blame] | 38 | #include <stdio.h> |
Thomas Klausner | c1364c5 | 1999-07-26 02:44:54 +0000 | [diff] [blame] | 39 | #include <stdlib.h> |
Dieter Baron | b2ed74d | 2004-04-14 14:01:31 +0000 | [diff] [blame] | 40 | #include <string.h> |
| 41 | #include <errno.h> |
Dieter Baron | b2ed74d | 2004-04-14 14:01:31 +0000 | [diff] [blame] | 42 | #include <sys/types.h> |
| 43 | #include <sys/stat.h> |
| 44 | |
Thomas Klausner | af93f08 | 1999-07-26 01:39:56 +0000 | [diff] [blame] | 45 | #include "zip.h" |
Dieter Baron | b2ed74d | 2004-04-14 14:01:31 +0000 | [diff] [blame] | 46 | #include "zipint.h" |
Thomas Klausner | af93f08 | 1999-07-26 01:39:56 +0000 | [diff] [blame] | 47 | |
Dieter Baron | b2ed74d | 2004-04-14 14:01:31 +0000 | [diff] [blame] | 48 | |
| 49 | |
| 50 | /* _zip_file_get_offset(za, ze): |
| 51 | Returns the offset of the file data for entry ze. |
| 52 | |
| 53 | On error, fills in za->error and returns 0. |
| 54 | */ |
| 55 | |
| 56 | unsigned int |
| 57 | _zip_file_get_offset(struct zip *za, int idx) |
Thomas Klausner | af93f08 | 1999-07-26 01:39:56 +0000 | [diff] [blame] | 58 | { |
Dieter Baron | b2ed74d | 2004-04-14 14:01:31 +0000 | [diff] [blame] | 59 | struct zip_dirent de; |
| 60 | unsigned int offset; |
Thomas Klausner | af93f08 | 1999-07-26 01:39:56 +0000 | [diff] [blame] | 61 | |
Dieter Baron | b2ed74d | 2004-04-14 14:01:31 +0000 | [diff] [blame] | 62 | offset = za->cdir->entry[idx].offset; |
| 63 | |
| 64 | if (fseek(za->zp, offset, SEEK_SET) != 0) { |
Thomas Klausner | d0147e9 | 2004-11-17 21:55:17 +0000 | [diff] [blame] | 65 | _zip_error_set(&za->error, ZIP_ER_SEEK, errno); |
Dieter Baron | b2ed74d | 2004-04-14 14:01:31 +0000 | [diff] [blame] | 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | if (_zip_dirent_read(&de, za->zp, NULL, 0, 1, &za->error) != 0) |
| 70 | return 0; |
| 71 | |
| 72 | offset += LENTRYSIZE + de.filename_len + de.extrafield_len; |
| 73 | |
| 74 | _zip_dirent_finalize(&de); |
| 75 | |
| 76 | return offset; |
Thomas Klausner | af93f08 | 1999-07-26 01:39:56 +0000 | [diff] [blame] | 77 | } |