Adds utility functions to NSData, that help deal with null termination of C strings.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

NSData+SA_NSDataExtensions.h 4.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // NSData+SA_NSDataExtensions.h
  3. //
  4. // Copyright (c) 2015 Said Achmiz.
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in all
  14. // copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. // SOFTWARE.
  23. #import <Foundation/Foundation.h>
  24. /** \category NSData+SA_NSDataExtensions
  25. * @brief Adds several utility methods to NSData.
  26. */
  27. @interface NSData (SA_NSDataExtensions)
  28. // NOTE on stripping nulls from the ends of byte arrays.
  29. //
  30. // If you strip a null from the end of an array which is something other than
  31. // a null-terminated C string (such as, for example, the bytes representing a
  32. // UTF-16 string), and thereby cause yourself difficulties, you have only
  33. // yourself to blame. Be sure that you know what your NSData objects are
  34. // supposed to contain!
  35. /** Returns YES if the last byte of the stored data is null, NO otherwise.
  36. */
  37. @property (readonly, getter=isNullTerminated) BOOL nullTerminated;
  38. /** Returns the stored bytes as a null-terminated C string (byte array).
  39. If the stored data is already null-terminated, the returned pointer will
  40. be a pointer to the bytes managed by the receiver. If it is not already
  41. null-terminated, the returned pointer will point to bytes managed by a
  42. copy of the receiver (and the bytes of the copy will be null-terminated).
  43. */
  44. @property (readonly) const char *SA_terminatedCString;
  45. /** Returns data containing the stored bytes as a null-terminated C string
  46. (byte array).
  47. If the stored data is already null-terminated, this method simply returns
  48. the receiver. If it is not already null-terminated, this method returns a
  49. reference to a fresh copy of the receiver (a copy that contains a
  50. null-terminated byte array, of course).
  51. */
  52. @property (readonly) NSData *SA_dataWithTerminatedCString;
  53. /** Returns the stored bytes as an non-null-terminated byte array.
  54. If the stored data was not null-terminated to begin with, the returned
  55. pointer will be a pointer to the bytes managed by the receiver. If the
  56. stored data was null-terminated, the returned pointer will point to bytes
  57. managed by a copy of the receiver (and the bytes of the copy will not be
  58. null-terminated; but see NOTE).
  59. NOTE: If the receiver's *last* byte is null, the bytes pointed to by the
  60. returned pointer will have that null stripped; but if there are any more
  61. null bytes prior to that last null, they will remain untouched!
  62. */
  63. @property (readonly) const char *SA_unterminatedByteString;
  64. /** Returns data containing the stored bytes as a non-null-terminated byte
  65. array.
  66. If the stored data was not null-terminated to begin with, this method simply
  67. returns the receiver. If the stored data was null-terminated, this method
  68. returns a reference to a fresh copy of the receiver (a copy that contains a
  69. non-null-terminated byte array, of course; but see NOTE).
  70. NOTE: If the receiver's *last* byte is null, the bytes managed by the
  71. returned object will have that null stripped; but if there are any more
  72. null bytes prior to that last null, they will remain untouched!
  73. */
  74. @property (readonly) NSData *SA_dataWithUnterminatedByteString;
  75. /** Returns an NSData object containing a blank C string (i.e. a byte sequence
  76. of length 1, containing the null character '\0').
  77. */
  78. +(NSData *)dataWithBlankCString;
  79. @end