Differences From Artifact [010e8050b6]:
- File
src/OFArchiveURIHandler.m
— part of check-in
[3e455c4839]
at
2022-10-11 19:27:09
on branch trunk
— OFURI: Make scheme and path nonnull
This is as per the RFC, which says the scheme and path can never be
missing, while the path can be empty. (user: js, size: 5253) [annotate] [blame] [check-ins using] [more...]
To Artifact [e770c9f1e9]:
- File src/OFArchiveIRIHandler.m — part of check-in [8939cbdb52] at 2023-01-06 09:04:02 on branch trunk — Update copyright (user: js, size: 5253) [annotate] [blame] [check-ins using] [more...]
1 | /* | | | > < | | | | | | | | | | | | | | | | | | | | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | /* * Copyright (c) 2008-2023 Jonathan Schleifer <js@nil.im> * * All rights reserved. * * This file is part of ObjFW. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE.QPL included in * the packaging of this file. * * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #include "config.h" #include <errno.h> #import "OFArchiveIRIHandler.h" #import "OFCharacterSet.h" #import "OFGZIPStream.h" #import "OFIRI.h" #import "OFLHAArchive.h" #import "OFStream.h" #import "OFTarArchive.h" #import "OFZIPArchive.h" #import "OFInvalidArgumentException.h" #import "OFOpenItemFailedException.h" @interface OFArchiveIRIHandlerPathAllowedCharacterSet: OFCharacterSet { OFCharacterSet *_characterSet; bool (*_characterIsMember)(id, SEL, OFUnichar); } @end static OFCharacterSet *pathAllowedCharacters; static void initPathAllowedCharacters(void) { pathAllowedCharacters = [[OFArchiveIRIHandlerPathAllowedCharacterSet alloc] init]; } @implementation OFArchiveIRIHandler - (OFStream *)openItemAtIRI: (OFIRI *)IRI mode: (OFString *)mode { void *pool = objc_autoreleasePoolPush(); OFString *scheme = IRI.scheme; OFString *percentEncodedPath, *path; size_t pos; OFIRI *archiveIRI; OFStream *stream; if (IRI.host != nil || IRI.port != nil || IRI.user != nil || IRI.password != nil || IRI.query != nil || IRI.fragment != nil) @throw [OFInvalidArgumentException exception]; if (![mode isEqual: @"r"]) /* * Writing has some implications that are not decided yet: Will * it always append to an archive? What happens if the file * already exists? */ @throw [OFInvalidArgumentException exception]; /* * GZIP only compresses one file and thus has no path inside an * archive. */ if ([scheme isEqual: @"gzip"]) { stream = [OFIRIHandler openItemAtIRI: [OFIRI IRIWithString: IRI.path] mode: @"r"]; stream = [OFGZIPStream streamWithStream: stream mode: @"r"]; goto end; } percentEncodedPath = IRI.percentEncodedPath; pos = [percentEncodedPath rangeOfString: @"!"].location; if (pos == OFNotFound) @throw [OFInvalidArgumentException exception]; archiveIRI = [OFIRI IRIWithString: [percentEncodedPath substringWithRange: OFMakeRange(0, pos)] .stringByRemovingPercentEncoding]; path = [percentEncodedPath substringWithRange: OFMakeRange(pos + 1, percentEncodedPath.length - pos - 1)] .stringByRemovingPercentEncoding; if ([scheme isEqual: @"lha"]) { OFLHAArchive *archive = [OFLHAArchive archiveWithIRI: archiveIRI mode: @"r"]; OFLHAArchiveEntry *entry; while ((entry = [archive nextEntry]) != nil) { if ([entry.fileName isEqual: path]) { stream = [archive streamForReadingCurrentEntry]; goto end; } } @throw [OFOpenItemFailedException exceptionWithIRI: IRI mode: mode errNo: ENOENT]; } else if ([scheme isEqual: @"tar"]) { OFTarArchive *archive = [OFTarArchive archiveWithIRI: archiveIRI mode: @"r"]; OFTarArchiveEntry *entry; while ((entry = [archive nextEntry]) != nil) { if ([entry.fileName isEqual: path]) { stream = [archive streamForReadingCurrentEntry]; goto end; } } @throw [OFOpenItemFailedException exceptionWithIRI: IRI mode: mode errNo: ENOENT]; } else if ([scheme isEqual: @"zip"]) { OFZIPArchive *archive = [OFZIPArchive archiveWithIRI: archiveIRI mode: @"r"]; stream = [archive streamForReadingFile: path]; } else @throw [OFInvalidArgumentException exception]; end: stream = [stream retain]; objc_autoreleasePoolPop(pool); return [stream autorelease]; } @end @implementation OFArchiveIRIHandlerPathAllowedCharacterSet - (instancetype)init { self = [super init]; @try { _characterSet = [[OFCharacterSet IRIPathAllowedCharacterSet] retain]; _characterIsMember = (bool (*)(id, SEL, OFUnichar)) [_characterSet methodForSelector: @selector(characterIsMember:)]; } @catch (id e) { [self release]; @throw e; } |
︙ | ︙ | |||
168 169 170 171 172 173 174 | - (bool)characterIsMember: (OFUnichar)character { return (character != '!' && _characterIsMember(_characterSet, @selector(characterIsMember:), character)); } @end | | | | | | | | | 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | - (bool)characterIsMember: (OFUnichar)character { return (character != '!' && _characterIsMember(_characterSet, @selector(characterIsMember:), character)); } @end OFIRI * OFArchiveIRIHandlerIRIForFileInArchive(OFString *scheme, OFString *pathInArchive, OFIRI *archiveIRI) { static OFOnceControl onceControl = OFOnceControlInitValue; OFMutableIRI *ret = [OFMutableIRI IRIWithScheme: scheme]; void *pool = objc_autoreleasePoolPush(); OFString *archiveIRIString; OFOnce(&onceControl, initPathAllowedCharacters); pathInArchive = [pathInArchive stringByAddingPercentEncodingWithAllowedCharacters: pathAllowedCharacters]; archiveIRIString = [archiveIRI.string stringByAddingPercentEncodingWithAllowedCharacters: pathAllowedCharacters]; ret.percentEncodedPath = [OFString stringWithFormat: @"%@!%@", archiveIRIString, pathInArchive]; [ret makeImmutable]; objc_autoreleasePoolPop(pool); return ret; } |