Adds utility functions to NSData, that help deal with null termination of C strings.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

NSData+SA_NSDataExtensions.h 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. /** Returns an NSData object containing a blank C string (i.e. a byte sequence
  37. of length 1, containing the null character '\0').
  38. */
  39. +(NSData *) dataWithBlankCString;
  40. /** Returns an NSData object containing bytes copied from the given C string
  41. (sans the null terminator).
  42. */
  43. +(NSData *) dataFromCString:(const char *)cString;
  44. /** Returns an NSData object containing the bytes of the given C string
  45. (sans the null terminator).
  46. */
  47. +(NSData *) dataWithCString:(char *)cString;
  48. @end