byte_buffer_shim.c 1.6 KB

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