NSNumber+XMPP.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #import "NSNumber+XMPP.h"
  2. #if ! __has_feature(objc_arc)
  3. #warning This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
  4. #endif
  5. @implementation NSNumber (XMPP)
  6. + (NSNumber *)xmpp_numberWithPtr:(const void *)ptr
  7. {
  8. return [[NSNumber alloc] xmpp_initWithPtr:ptr];
  9. }
  10. - (instancetype)xmpp_initWithPtr:(const void *)ptr
  11. {
  12. return [self initWithLong:(long)ptr];
  13. }
  14. + (BOOL)xmpp_parseString:(NSString *)str intoInt32:(int32_t *)pNum
  15. {
  16. if (str == nil)
  17. {
  18. *pNum = (int32_t)0;
  19. return NO;
  20. }
  21. errno = 0;
  22. long result = strtol([str UTF8String], NULL, 10);
  23. if (LONG_BIT != 32)
  24. {
  25. if (result > INT32_MAX)
  26. {
  27. *pNum = INT32_MAX;
  28. return NO;
  29. }
  30. if (result < INT32_MIN)
  31. {
  32. *pNum = INT32_MIN;
  33. return NO;
  34. }
  35. }
  36. // From the manpage:
  37. //
  38. // If no conversion could be performed, 0 is returned and the global variable errno is set to EINVAL.
  39. // If an overflow or underflow occurs, errno is set to ERANGE and the function return value is clamped.
  40. //
  41. // Clamped means it will be TYPE_MAX or TYPE_MIN.
  42. // If overflow/underflow occurs, returning a clamped value is more accurate then returning zero.
  43. *pNum = (int32_t)result;
  44. if (errno != 0)
  45. return NO;
  46. else
  47. return YES;
  48. }
  49. + (BOOL)xmpp_parseString:(NSString *)str intoUInt32:(uint32_t *)pNum
  50. {
  51. if (str == nil)
  52. {
  53. *pNum = (uint32_t)0;
  54. return NO;
  55. }
  56. errno = 0;
  57. unsigned long result = strtoul([str UTF8String], NULL, 10);
  58. if (LONG_BIT != 32)
  59. {
  60. if (result > UINT32_MAX)
  61. {
  62. *pNum = UINT32_MAX;
  63. return NO;
  64. }
  65. }
  66. // From the manpage:
  67. //
  68. // If no conversion could be performed, 0 is returned and the global variable errno is set to EINVAL.
  69. // If an overflow or underflow occurs, errno is set to ERANGE and the function return value is clamped.
  70. //
  71. // Clamped means it will be TYPE_MAX or TYPE_MIN.
  72. // If overflow/underflow occurs, returning a clamped value is more accurate then returning zero.
  73. *pNum = (uint32_t)result;
  74. if (errno != 0)
  75. return NO;
  76. else
  77. return YES;
  78. }
  79. + (BOOL)xmpp_parseString:(NSString *)str intoInt64:(int64_t *)pNum
  80. {
  81. if (str == nil)
  82. {
  83. *pNum = (int64_t)0;
  84. return NO;
  85. }
  86. errno = 0;
  87. // On both 32-bit and 64-bit machines, long long = 64 bit
  88. *pNum = strtoll([str UTF8String], NULL, 10);
  89. // From the manpage:
  90. //
  91. // If no conversion could be performed, 0 is returned and the global variable errno is set to EINVAL.
  92. // If an overflow or underflow occurs, errno is set to ERANGE and the function return value is clamped.
  93. //
  94. // Clamped means it will be TYPE_MAX or TYPE_MIN.
  95. // If overflow/underflow occurs, returning a clamped value is more accurate then returning zero.
  96. if (errno != 0)
  97. return NO;
  98. else
  99. return YES;
  100. }
  101. + (BOOL)xmpp_parseString:(NSString *)str intoUInt64:(uint64_t *)pNum
  102. {
  103. if (str == nil)
  104. {
  105. *pNum = (uint64_t)0;
  106. return NO;
  107. }
  108. errno = 0;
  109. // On both 32-bit and 64-bit machines, unsigned long long = 64 bit
  110. *pNum = strtoull([str UTF8String], NULL, 10);
  111. // From the manpage:
  112. //
  113. // If no conversion could be performed, 0 is returned and the global variable errno is set to EINVAL.
  114. // If an overflow or underflow occurs, errno is set to ERANGE and the function return value is clamped.
  115. //
  116. // Clamped means it will be TYPE_MAX or TYPE_MIN.
  117. // If overflow/underflow occurs, returning a clamped value is more accurate then returning zero.
  118. if (errno != 0)
  119. return NO;
  120. else
  121. return YES;
  122. }
  123. + (BOOL)xmpp_parseString:(NSString *)str intoNSInteger:(NSInteger *)pNum
  124. {
  125. if (NSIntegerMax == INT32_MAX)
  126. return [self xmpp_parseString:str intoInt32:(int32_t *)pNum];
  127. else
  128. return [self xmpp_parseString:str intoInt64:(int64_t *)pNum];
  129. }
  130. + (BOOL)xmpp_parseString:(NSString *)str intoNSUInteger:(NSUInteger *)pNum
  131. {
  132. if (NSUIntegerMax == UINT32_MAX)
  133. return [self xmpp_parseString:str intoUInt32:(uint32_t *)pNum];
  134. else
  135. return [self xmpp_parseString:str intoUInt64:(uint64_t *)pNum];
  136. }
  137. + (UInt8)xmpp_extractUInt8FromData:(NSData *)data atOffset:(unsigned int)offset
  138. {
  139. // 8 bits = 1 byte
  140. if([data length] < offset + 1) return 0;
  141. UInt8 *pResult = (UInt8 *)([data bytes] + offset);
  142. UInt8 result = *pResult;
  143. return result;
  144. }
  145. + (UInt16)xmpp_extractUInt16FromData:(NSData *)data atOffset:(unsigned int)offset andConvertFromNetworkOrder:(BOOL)flag
  146. {
  147. // 16 bits = 2 bytes
  148. if([data length] < offset + 2) return 0;
  149. UInt16 *pResult = (UInt16 *)([data bytes] + offset);
  150. UInt16 result = *pResult;
  151. if(flag)
  152. return ntohs(result);
  153. else
  154. return result;
  155. }
  156. + (UInt32)xmpp_extractUInt32FromData:(NSData *)data atOffset:(unsigned int)offset andConvertFromNetworkOrder:(BOOL)flag
  157. {
  158. // 32 bits = 4 bytes
  159. if([data length] < offset + 4) return 0;
  160. UInt32 *pResult = (UInt32 *)([data bytes] + offset);
  161. UInt32 result = *pResult;
  162. if(flag)
  163. return ntohl(result);
  164. else
  165. return result;
  166. }
  167. @end
  168. #ifndef XMPP_EXCLUDE_DEPRECATED
  169. @implementation NSNumber (XMPPDeprecated)
  170. + (NSNumber *)numberWithPtr:(const void *)ptr {
  171. return [self xmpp_numberWithPtr:ptr];
  172. }
  173. - (id)initWithPtr:(const void *)ptr {
  174. return [self xmpp_initWithPtr:ptr];
  175. }
  176. + (BOOL)parseString:(NSString *)str intoInt32:(int32_t *)pNum {
  177. return [self xmpp_parseString:str intoInt32:pNum];
  178. }
  179. + (BOOL)parseString:(NSString *)str intoUInt32:(uint32_t *)pNum {
  180. return [self xmpp_parseString:str intoUInt32:pNum];
  181. }
  182. + (BOOL)parseString:(NSString *)str intoInt64:(int64_t *)pNum {
  183. return [self xmpp_parseString:str intoInt64:pNum];
  184. }
  185. + (BOOL)parseString:(NSString *)str intoUInt64:(uint64_t *)pNum {
  186. return [self xmpp_parseString:str intoUInt64:pNum];
  187. }
  188. + (BOOL)parseString:(NSString *)str intoNSInteger:(NSInteger *)pNum {
  189. return [self xmpp_parseString:str intoNSInteger:pNum];
  190. }
  191. + (BOOL)parseString:(NSString *)str intoNSUInteger:(NSUInteger *)pNum {
  192. return [self xmpp_parseString:str intoNSUInteger:pNum];
  193. }
  194. + (UInt8)extractUInt8FromData:(NSData *)data atOffset:(unsigned int)offset {
  195. return [self xmpp_extractUInt8FromData:data atOffset:offset];
  196. }
  197. + (UInt16)extractUInt16FromData:(NSData *)data atOffset:(unsigned int)offset andConvertFromNetworkOrder:(BOOL)flag {
  198. return [self xmpp_extractUInt16FromData:data atOffset:offset andConvertFromNetworkOrder:flag];
  199. }
  200. + (UInt32)extractUInt32FromData:(NSData *)data atOffset:(unsigned int)offset andConvertFromNetworkOrder:(BOOL)flag {
  201. return [self xmpp_extractUInt32FromData:data atOffset:offset andConvertFromNetworkOrder:flag];
  202. }
  203. @end
  204. #endif