NSData+SRB64Additions.m 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. //
  2. // Copyright 2012 Square Inc.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #import "FirebaseDatabase/Sources/third_party/SocketRocket/NSData+SRB64Additions.h"
  17. #import "FirebaseDatabase/Sources/third_party/SocketRocket/fbase64.h"
  18. @implementation FSRUtilities
  19. + (NSString *)base64EncodedStringFromData:(NSData *)data {
  20. size_t buffer_size = ((data.length * 3 + 2) / 2);
  21. char *buffer = (char *)malloc(buffer_size);
  22. int len = f_b64_ntop(data.bytes, data.length, buffer, buffer_size);
  23. if (len == -1) {
  24. free(buffer);
  25. return nil;
  26. } else{
  27. return [[NSString alloc] initWithBytesNoCopy:buffer length:len encoding:NSUTF8StringEncoding freeWhenDone:YES];
  28. }
  29. }
  30. @end