byte_buffer.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Copyright 2016, gRPC Authors All rights reserved.
  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. #include <stdio.h>
  17. #include "internal.h"
  18. #include "cgrpc.h"
  19. #include <stdbool.h>
  20. #include <stdio.h>
  21. #include <assert.h>
  22. #include <string.h>
  23. void cgrpc_byte_buffer_destroy(cgrpc_byte_buffer *bb) {
  24. grpc_byte_buffer_destroy(bb);
  25. }
  26. cgrpc_byte_buffer *cgrpc_byte_buffer_create_by_copying_data(const void *source, size_t len) {
  27. grpc_slice request_payload_slice = grpc_slice_from_copied_buffer(source, len);
  28. cgrpc_byte_buffer *bb = grpc_raw_byte_buffer_create(&request_payload_slice, 1);
  29. grpc_slice_unref(request_payload_slice);
  30. return bb;
  31. }
  32. const void *cgrpc_byte_buffer_copy_data(cgrpc_byte_buffer *bb, size_t *length) {
  33. if (!bb) {
  34. return NULL;
  35. }
  36. grpc_byte_buffer_reader reader;
  37. bool success = grpc_byte_buffer_reader_init(&reader, bb);
  38. if (!success) {
  39. return NULL;
  40. }
  41. grpc_slice slice = grpc_byte_buffer_reader_readall(&reader);
  42. *length = (size_t) GRPC_SLICE_LENGTH(slice);
  43. void *result = malloc(*length);
  44. memcpy(result, GRPC_SLICE_START_PTR(slice), *length);
  45. grpc_slice_unref(slice);
  46. grpc_byte_buffer_reader_destroy(&reader);
  47. return result;
  48. }