NSDataBigString.mm 1019 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import "NSDataBigString.h"
  8. namespace facebook {
  9. namespace react {
  10. static NSData *ensureNullTerminated(NSData *source)
  11. {
  12. if (!source || source.length == 0) {
  13. return nil;
  14. }
  15. NSUInteger sourceLength = source.length;
  16. unsigned char lastByte;
  17. [source getBytes:&lastByte range:NSMakeRange(sourceLength - 1, 1)];
  18. // TODO: bundles from the packager should always include a NULL byte
  19. // or we should we relax this requirement and only read as much from the
  20. // buffer as length indicates
  21. if (lastByte == '\0') {
  22. return source;
  23. } else {
  24. NSMutableData *data = [source mutableCopy];
  25. unsigned char nullByte = '\0';
  26. [data appendBytes:&nullByte length:1];
  27. return data;
  28. }
  29. }
  30. NSDataBigString::NSDataBigString(NSData *data)
  31. {
  32. m_length = [data length];
  33. m_data = ensureNullTerminated(data);
  34. }
  35. }
  36. }