Adds utility functions to NSData, that help deal with null termination of C strings.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

NSData+SA_NSDataExtensions.h 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //
  2. // NSData+SA_NSDataExtensions.h
  3. //
  4. // Copyright 2015-2021 Said Achmiz.
  5. // See LICENSE and README.md for more info.
  6. #import <Foundation/Foundation.h>
  7. /** \category NSData+SA_NSDataExtensions
  8. * @brief Adds several utility methods to NSData.
  9. */
  10. @interface NSData (SA_NSDataExtensions)
  11. // NOTE on stripping nulls from the ends of byte arrays.
  12. //
  13. // If you strip a null from the end of an array which is something other than
  14. // a null-terminated C string (such as, for example, the bytes representing a
  15. // UTF-16 string), and thereby cause yourself difficulties, you have only
  16. // yourself to blame. Be sure that you know what your NSData objects are
  17. // supposed to contain!
  18. /** Returns YES if the last byte of the stored data is null, NO otherwise.
  19. */
  20. @property (readonly, getter=isNullTerminated) BOOL nullTerminated;
  21. /** Returns the stored bytes as a null-terminated C string (byte array).
  22. If the stored data is already null-terminated, the returned pointer will
  23. be a pointer to the bytes managed by the receiver. If it is not already
  24. null-terminated, the returned pointer will point to bytes managed by a
  25. copy of the receiver (and the bytes of the copy will be null-terminated).
  26. */
  27. @property (readonly) const char *terminatedCString;
  28. /** Returns data containing the stored bytes as a null-terminated C string
  29. (byte array).
  30. If the stored data is already null-terminated, this method simply returns
  31. the receiver. If it is not already null-terminated, this method returns a
  32. reference to a fresh copy of the receiver (a copy that contains a
  33. null-terminated byte array, of course).
  34. */
  35. @property (readonly) NSData *dataWithTerminatedCString;
  36. /* Range manipulation.
  37. */
  38. @property (readonly) NSRange startRange;
  39. @property (readonly) NSRange fullRange;
  40. @property (readonly) NSRange endRange;
  41. -(NSRange) rangeAfterRange:(NSRange)aRange;
  42. -(NSRange) rangeFromEndOfRange:(NSRange)aRange;
  43. -(NSRange) rangeToEndFrom:(NSRange)aRange;
  44. /** Returns an NSData object containing a blank C string (i.e. a byte sequence
  45. of length 1, containing the null character '\0').
  46. */
  47. +(NSData *) dataWithBlankCString;
  48. /** Returns an NSData object containing bytes copied from the given C string
  49. (sans the null terminator).
  50. */
  51. +(NSData *) dataFromCString:(const char *)cString;
  52. /** Returns an NSData object containing the bytes of the given C string
  53. (sans the null terminator).
  54. */
  55. +(NSData *) dataWithCString:(char *)cString;
  56. @end