| 1 | /* |
| 2 | ** NSData+Extensions.h |
| 3 | ** |
| 4 | ** Copyright (c) 2001-2007 Ludovic Marcotte |
| 5 | ** Copyright (C) 2017-2018 Riccardo Mottola |
| 6 | ** |
| 7 | ** Author: Ludovic Marcotte <[email protected]> |
| 8 | ** Riccardo Mottola <[email protected]> |
| 9 | ** |
| 10 | ** This library is free software; you can redistribute it and/or |
| 11 | ** modify it under the terms of the GNU Lesser General Public |
| 12 | ** License as published by the Free Software Foundation; either |
| 13 | ** version 2.1 of the License, or (at your option) any later version. |
| 14 | ** |
| 15 | ** This library is distributed in the hope that it will be useful, |
| 16 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | ** Lesser General Public License for more details. |
| 19 | ** |
| 20 | ** You should have received a copy of the GNU General Public License |
| 21 | ** along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | */ |
| 23 | |
| 24 | #ifndef _Pantomime_H_NSData_Extensions |
| 25 | #define _Pantomime_H_NSData_Extensions |
| 26 | |
| 27 | #import <Foundation/NSObject.h> |
| 28 | #import <Foundation/NSArray.h> |
| 29 | #import <Foundation/NSData.h> |
| 30 | #import <Foundation/NSString.h> |
| 31 | |
| 32 | #import "CWConstants.h" |
| 33 | |
| 34 | /*! |
| 35 | @category NSData (PantomimeExtensions) |
| 36 | @abstract Pantomime extensions to NSData. |
| 37 | @discussion This category provides useful extensions for handling |
| 38 | NSData objects. |
| 39 | */ |
| 40 | @interface NSData (PantomimeExtensions) |
| 41 | |
| 42 | /*! |
| 43 | @method decodeBase64 |
| 44 | @abstract Decode using the base64 encoding. |
| 45 | @discussion This method is used to decode data that has been encoded |
| 46 | using the base64 method. |
| 47 | @result Returns the decoded bytes, as a NSData instance. |
| 48 | */ |
| 49 | - (NSData *) decodeBase64; |
| 50 | |
| 51 | /*! |
| 52 | @method encodeBase64WithLineLength: |
| 53 | @abstract Encoding the bytes using the base64 encoding |
| 54 | @discussion Pending. |
| 55 | @param theLength Specifies the length of the lines if wrapping |
| 56 | should be done. 0 disables any wrapping. |
| 57 | @result Returns a NSData object on success, nil on error. |
| 58 | */ |
| 59 | - (NSData *) encodeBase64WithLineLength: (NSUInteger) theLength; |
| 60 | |
| 61 | /*! |
| 62 | @method unfoldLines |
| 63 | @abstract Unfold lines contained in this object. |
| 64 | @discussion This method is used to replace all occurences |
| 65 | of "\n " or "\n\t" by "\n". |
| 66 | @result Returns a new NSData object. |
| 67 | */ |
| 68 | - (NSData *) unfoldLines; |
| 69 | |
| 70 | /*! |
| 71 | @method decodeQuotedPrintableInHeader |
| 72 | @abstract Decode using the quoted-printable encoding. |
| 73 | @discussion This method is used to decode data that has been |
| 74 | encoded using the quoted-printable method. |
| 75 | @param aBOOL Specifies if we are decoding data from |
| 76 | a message header, or not. |
| 77 | @result Returns a new NSData object. |
| 78 | */ |
| 79 | - (NSData *) decodeQuotedPrintableInHeader: (BOOL) aBOOL; |
| 80 | |
| 81 | /*! |
| 82 | @method encodeQuotedPrintableWithLineLength:inHeader: |
| 83 | @abstract Encoding the bytes using the quoted-printable encoding |
| 84 | @discussion Pending. |
| 85 | @param aBOOL Specifies if we are encoding data from |
| 86 | a message header, or not. |
| 87 | @param theLength Specifies the length of the lines if wrapping |
| 88 | should be done. 0 disables any wrapping. |
| 89 | @result Returns a NSData object. |
| 90 | */ |
| 91 | - (NSData *) encodeQuotedPrintableWithLineLength: (NSUInteger) theLength |
| 92 | inHeader: (BOOL) aBOOL; |
| 93 | /*! |
| 94 | @method rangeOfData: |
| 95 | @discussion This method searches for <i>theData</i> in the receiver |
| 96 | and returns the associated NSRange. |
| 97 | @param theData The data to search for in the receiver. |
| 98 | @result The associated range. |
| 99 | */ |
| 100 | -(NSRange) rangeOfData: (NSData *) theData; |
| 101 | |
| 102 | /*! |
| 103 | @method rangeOfCString: |
| 104 | @discussion Invokes rangeOfCString:options:range: with no option |
| 105 | and the complete range. |
| 106 | @param theCString The C string to search for. |
| 107 | @result The associated range of the C string in the receiver. |
| 108 | */ |
| 109 | -(NSRange) rangeOfCString: (const char *) theCString; |
| 110 | |
| 111 | /*! |
| 112 | @method rangeOfCString: |
| 113 | @discussion Invokes rangeOfCString:options:range: with <i>theOptions</i> |
| 114 | and the complete range. |
| 115 | @param theCString The C string to search for. |
| 116 | @param theOptions The options used during the search. |
| 117 | @result The associated range of the C string in the receiver. |
| 118 | */ |
| 119 | -(NSRange) rangeOfCString: (const char *) theCString |
| 120 | options: (NSUInteger) theOptions; |
| 121 | |
| 122 | /*! |
| 123 | @method rangeOfCString: |
| 124 | @discussion Search for <i>theCString<i> using <i>theOptions</i> |
| 125 | and <i>theRange</i> in the receiver. |
| 126 | @param theCString The C string to search for. |
| 127 | @param theOptions The options used during the search. |
| 128 | @param theRange The range to use when performing the search. |
| 129 | @result The associated range of the C string in the receiver. |
| 130 | */ |
| 131 | -(NSRange) rangeOfCString: (const char *) theCString |
| 132 | options: (NSUInteger) theOptions |
| 133 | range: (NSRange) theRange; |
| 134 | |
| 135 | /*! |
| 136 | @method subdataFromIndex: |
| 137 | @discussion This method is used to obtain the subdata from <i>theIndex</i> |
| 138 | in the receiver. The byte at <i>theIndex</i> is part of the |
| 139 | returned NSData instance. |
| 140 | @param theIndex The index used to get the subdata from. |
| 141 | @result The subdata. |
| 142 | */ |
| 143 | - (NSData *) subdataFromIndex: (NSUInteger) theIndex; |
| 144 | |
| 145 | /*! |
| 146 | @method subdataToIndex: |
| 147 | @discussion This method is used to obtain the subdata to <i>theIndex</i> |
| 148 | from the receiver. The byte at <i>theIndex</i> is not included in |
| 149 | returned NSData instance. |
| 150 | @param theIndex The index used to get the subdata to. |
| 151 | @result The subdata. |
| 152 | */ |
| 153 | - (NSData *) subdataToIndex: (NSUInteger) theIndex; |
| 154 | |
| 155 | /*! |
| 156 | @method dataByTrimmingWhiteSpaces |
| 157 | @discussion This method is used to trim the leading and trailing |
| 158 | spaces (space characters, tabs, newlines, carriage returns |
| 159 | or other characters with no visible representation) |
| 160 | from the receiver. |
| 161 | @result The trimmed data. |
| 162 | */ |
| 163 | - (NSData *) dataByTrimmingWhiteSpaces; |
| 164 | |
| 165 | /*! |
| 166 | @method dataFromQuotedData |
| 167 | @discussion This method returns an unquoted data if the |
| 168 | data has a leading AND trailing quote. |
| 169 | @result The unquoted data or the original if it does not |
| 170 | match the criteria. |
| 171 | */ |
| 172 | - (NSData *) dataFromQuotedData; |
| 173 | |
| 174 | /*! |
| 175 | @method dataFromSemicolonTerminatedData |
| 176 | @discussion This method returns the data by omitting the |
| 177 | ending semicolon, if present. |
| 178 | @result The data without the ending semicolon. |
| 179 | */ |
| 180 | - (NSData *) dataFromSemicolonTerminatedData; |
| 181 | |
| 182 | /*! |
| 183 | @method dataByRemovingLineFeedCharacters |
| 184 | @discussion This method is used to return the receiver without |
| 185 | any occurences of the line feed (LF, control-J, ASCII 10) |
| 186 | character. |
| 187 | @result The data withtout line feed characters. |
| 188 | */ |
| 189 | - (NSData *) dataByRemovingLineFeedCharacters; |
| 190 | |
| 191 | /*! |
| 192 | @method indexOfCharacter: |
| 193 | @discussion This method finds the first occurence of |
| 194 | the character in the receiver and returns its index (zero-based) |
| 195 | @param theCharacter The caracter to be searched for. |
| 196 | @result The index of the character, NSNotFound if it's not found in the receiver. |
| 197 | */ |
| 198 | - (NSUInteger) indexOfCharacter: (unichar) theCharacter; |
| 199 | |
| 200 | /*! |
| 201 | @method hasCPrefix: |
| 202 | @discussion This method is used to verify if the receiver has <i>theCString</i> |
| 203 | as a prefix. |
| 204 | @param theCString The C string to look for. |
| 205 | @result YES if <i>theCString</i> is a prefix of the receiver, NO otherwise. |
| 206 | */ |
| 207 | - (BOOL) hasCPrefix: (const char *) theCString; |
| 208 | |
| 209 | /*! |
| 210 | @method hasCSuffix: |
| 211 | @discussion This method is used to verify if the receiver has <i>theCString</i> |
| 212 | as a suffix. |
| 213 | @param theCString The C string to look for. |
| 214 | @result YES if <i>theCString</i> is a suffix of the receiver, NO otherwise. |
| 215 | */ |
| 216 | - (BOOL) hasCSuffix: (const char *) theCString; |
| 217 | |
| 218 | /*! |
| 219 | @method hasCaseInsensitiveCPrefix: |
| 220 | @discussion Same as -hasCPrefix but the comparison is done |
| 221 | in a case-insensitiveness matter. |
| 222 | @param thePrefix The prefix to be used. |
| 223 | @result YES the there is a match, NO otherwise. |
| 224 | */ |
| 225 | - (BOOL) hasCaseInsensitiveCPrefix: (const char *) theCString; |
| 226 | |
| 227 | /*! |
| 228 | @method hasCaseInsensitiveCSuffix: |
| 229 | @discussion Same as -hasCSuffix but the comparison is done |
| 230 | in a case-insensitiveness matter. |
| 231 | @param theSuffix The suffix to be used. |
| 232 | @result YES the there is a match, NO otherwise. |
| 233 | */ |
| 234 | - (BOOL) hasCaseInsensitiveCSuffix: (const char *) theCString; |
| 235 | |
| 236 | /*! |
| 237 | @method caseInsensitiveCCompare: |
| 238 | @discussion This method is used to compare <i>theCString</i> with our |
| 239 | receiver's bytes. |
| 240 | @param theCString The C string to compare the receiver with. |
| 241 | @result One of the three possible values of the NSComparisonResult enum. |
| 242 | */ |
| 243 | - (NSComparisonResult) caseInsensitiveCCompare: (const char *) theCString; |
| 244 | |
| 245 | /*! |
| 246 | @method componentsSeparatedByCString: |
| 247 | @discussion This method is used to separate the receiver into subdata, |
| 248 | using <i>theCString</i> as the separator. |
| 249 | @param theCString The separator to use, as a C string. |
| 250 | @result An instance of NSArray holding all components. The array is |
| 251 | empty if the separator was not found in the receiver. |
| 252 | */ |
| 253 | - (NSArray *) componentsSeparatedByCString: (const char *) theCString; |
| 254 | |
| 255 | /*! |
| 256 | @method asciiString |
| 257 | @discussion This method turns the receiver into a NSString object. |
| 258 | The receiver's bytes must be all-ASCII bytes. |
| 259 | @result A NSString instance, nil if the conversion failed. |
| 260 | */ |
| 261 | -(NSString *) asciiString; |
| 262 | |
| 263 | /*! |
| 264 | @method cString |
| 265 | @discussion This method returns the receiver's byte as a NULL terminated |
| 266 | C string. |
| 267 | @result The NULL terminated C string. |
| 268 | */ |
| 269 | -(const char *) cString; |
| 270 | |
| 271 | - (unichar) characterAtIndex: (NSUInteger) theIndex; |
| 272 | |
| 273 | /*! |
| 274 | @method quoteWithLevel:wrappingLimit: |
| 275 | @discussion This method is used to quote an unwrapped string. |
| 276 | @param theLevel The quoting level. |
| 277 | @param theLimit The line wrapping limit to use, in number of characters. |
| 278 | @result Returns a quoted string as a NSData instance. An empty NSData |
| 279 | instance is returned if <i>theLevel</i> is greater than <i>theLimit</i>. |
| 280 | */ |
| 281 | - (NSData *) quoteWithLevel: (NSUInteger) theLevel |
| 282 | wrappingLimit: (NSUInteger) theLimit; |
| 283 | |
| 284 | /*! |
| 285 | @method unwrapWithLimit: |
| 286 | @discussion This method is used to unwrap the string using a quote limit. |
| 287 | This method implements the behavior specified in RFC2646. |
| 288 | @param theQuoteLimit The quote limit to use for unwrapping the string. |
| 289 | @result The unwrapped string, as a NSData instance. |
| 290 | */ |
| 291 | - (NSData *) unwrapWithLimit: (NSUInteger) theQuoteLimit; |
| 292 | |
| 293 | /*! |
| 294 | @method wrapWithLimit: |
| 295 | @discussion This method wraps the string at the <i>theLimit</i> with |
| 296 | respect to RFC2646. |
| 297 | @param theLimit The limit to use for wrapping the string. The limit |
| 298 | corresponds to the maximum length of a line, specified |
| 299 | in characters. |
| 300 | @result The wrapped string. |
| 301 | */ |
| 302 | - (NSData *) wrapWithLimit: (NSUInteger) theLimit; |
| 303 | |
| 304 | @end |
| 305 | |
| 306 | /*! |
| 307 | @category NSMutableData (PantomimeExtensions) |
| 308 | @abstract Pantomime extensions to NSMutableData. |
| 309 | @discussion This category provides useful extensions for handling |
| 310 | NSMutableData objects. |
| 311 | */ |
| 312 | @interface NSMutableData (PantomimeExtensions) |
| 313 | |
| 314 | /*! |
| 315 | @method appendCFormat: |
| 316 | @discussion This method is used to append the variable arguments |
| 317 | to the receiver. The bytes of the arguments must |
| 318 | be composed only of ASCII characters. |
| 319 | @param theFormat The format of the rest of the argument, as this |
| 320 | method is a variadic method. |
| 321 | */ |
| 322 | -(void) appendCFormat: (NSString *) theFormat, ...; |
| 323 | |
| 324 | /*! |
| 325 | @method appendCString: |
| 326 | @discussion This method is used to append the C string to the receiver. |
| 327 | @param theCString The C string to append. |
| 328 | */ |
| 329 | -(void) appendCString: (const char *) theCString; |
| 330 | |
| 331 | /*! |
| 332 | @method insertCString: atIndex: |
| 333 | @discussion This method is used to insert the C string into |
| 334 | the received at the specified index. If <i>theIndex</i> is zero, |
| 335 | the C string will be added at the beginning of the |
| 336 | receiver. If <i>theIndex</i> is greater than the receiver's length, |
| 337 | the C string will be appended to the receiver. |
| 338 | @param theCString The C string to insert into the receiver. |
| 339 | @param theIndex The index where to insert the C string. |
| 340 | */ |
| 341 | - (void) insertCString: (const char *) theCString |
| 342 | atIndex: (NSUInteger) theIndex; |
| 343 | |
| 344 | /*! |
| 345 | @method replaceCRLFWithLF |
| 346 | @discussion This method destructively elides all carriage return |
| 347 | (CR, Control-M, ASCII 13) from the receiver. |
| 348 | */ |
| 349 | - (void) replaceCRLFWithLF; |
| 350 | |
| 351 | /*! |
| 352 | @method replaceLFWithCRLF |
| 353 | @discussion This method is used to replace all occurences of line feed |
| 354 | characters by sequences of a carriage return followed by |
| 355 | line feed characters in the receiver. |
| 356 | */ |
| 357 | - (NSMutableData *) replaceLFWithCRLF; |
| 358 | |
| 359 | @end |
| 360 | |
| 361 | #endif // _Pantomime_H_NSData_Extensions |