Adds utility functions to NSData, that help deal with null termination of C strings.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NSData+SA_NSDataExtensions.h 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 *SA_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 *SA_dataWithTerminatedCString;
  36. /** Returns the stored bytes as an non-null-terminated byte array.
  37. If the stored data was not null-terminated to begin with, the returned
  38. pointer will be a pointer to the bytes managed by the receiver. If the
  39. stored data was null-terminated, the returned pointer will point to bytes
  40. managed by a copy of the receiver (and the bytes of the copy will not be
  41. null-terminated; but see NOTE).
  42. NOTE: If the receiver's *last* byte is null, the bytes pointed to by the
  43. returned pointer will have that null stripped; but if there are any more
  44. null bytes prior to that last null, they will remain untouched!
  45. */
  46. @property (readonly) const char *SA_unterminatedByteString;
  47. /** Returns data containing the stored bytes as a non-null-terminated byte
  48. array.
  49. If the stored data was not null-terminated to begin with, this method simply
  50. returns the receiver. If the stored data was null-terminated, this method
  51. returns a reference to a fresh copy of the receiver (a copy that contains a
  52. non-null-terminated byte array, of course; but see NOTE).
  53. NOTE: If the receiver's *last* byte is null, the bytes managed by the
  54. returned object will have that null stripped; but if there are any more
  55. null bytes prior to that last null, they will remain untouched!
  56. */
  57. @property (readonly) NSData *SA_dataWithUnterminatedByteString;
  58. /** Returns an NSData object containing a blank C string (i.e. a byte sequence
  59. of length 1, containing the null character '\0').
  60. */
  61. +(NSData *) dataWithBlankCString;
  62. /** Returns an NSData object containing bytes copied from the given C string
  63. (sans the null terminator).
  64. */
  65. +(NSData *) dataFromCString:(const char *)cString;
  66. /** Returns an NSData object containing the bytes of the given C string
  67. (sans the null terminator).
  68. */
  69. +(NSData *) dataWithCString:(char *)cString;
  70. @end