murgatroid99 | 749666e | 2015-01-12 18:25:58 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2014, Google Inc. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 34 | #include <string.h> |
| 35 | #include <malloc.h> |
| 36 | |
| 37 | #include <node.h> |
| 38 | #include <nan.h> |
| 39 | #include "grpc/grpc.h" |
| 40 | #include "grpc/support/slice.h" |
| 41 | |
| 42 | namespace grpc { |
| 43 | namespace node { |
| 44 | |
| 45 | #include "byte_buffer.h" |
| 46 | |
| 47 | using ::node::Buffer; |
| 48 | using v8::Handle; |
| 49 | using v8::Value; |
| 50 | |
| 51 | grpc_byte_buffer *BufferToByteBuffer(Handle<Value> buffer) { |
| 52 | NanScope(); |
| 53 | int length = Buffer::Length(buffer); |
| 54 | char *data = Buffer::Data(buffer); |
| 55 | gpr_slice slice = gpr_slice_malloc(length); |
| 56 | memcpy(GPR_SLICE_START_PTR(slice), data, length); |
| 57 | grpc_byte_buffer *byte_buffer(grpc_byte_buffer_create(&slice, 1)); |
| 58 | gpr_slice_unref(slice); |
| 59 | return byte_buffer; |
| 60 | } |
| 61 | |
| 62 | Handle<Value> ByteBufferToBuffer(grpc_byte_buffer *buffer) { |
| 63 | NanEscapableScope(); |
| 64 | if (buffer == NULL) { |
| 65 | NanReturnNull(); |
| 66 | } |
| 67 | size_t length = grpc_byte_buffer_length(buffer); |
Craig Tiller | e8e304e | 2015-01-13 14:41:29 -0800 | [diff] [blame] | 68 | char *result = reinterpret_cast<char *>(calloc(length, sizeof(char))); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 69 | size_t offset = 0; |
| 70 | grpc_byte_buffer_reader *reader = grpc_byte_buffer_reader_create(buffer); |
| 71 | gpr_slice next; |
| 72 | while (grpc_byte_buffer_reader_next(reader, &next) != 0) { |
Craig Tiller | e8e304e | 2015-01-13 14:41:29 -0800 | [diff] [blame] | 73 | memcpy(result + offset, GPR_SLICE_START_PTR(next), GPR_SLICE_LENGTH(next)); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 74 | offset += GPR_SLICE_LENGTH(next); |
| 75 | } |
| 76 | return NanEscapeScope(NanNewBufferHandle(result, length)); |
| 77 | } |
| 78 | } // namespace node |
| 79 | } // namespace grpc |