| 1 |
/* |
| 2 |
* MessagePack for Python packing routine |
| 3 |
* |
| 4 |
* Copyright (C) 2009 Naoki INADA |
| 5 |
* |
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
* you may not use this file except in compliance with the License. |
| 8 |
* You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <stddef.h> |
| 20 |
#include <stdlib.h> |
| 21 |
#include "sysdep.h" |
| 22 |
#include <limits.h> |
| 23 |
#include <string.h> |
| 24 |
#include <stdbool.h> |
| 25 |
|
| 26 |
#ifdef __cplusplus |
| 27 |
extern "C" { |
| 28 |
#endif |
| 29 |
|
| 30 |
typedef struct msgpack_packer { |
| 31 |
char *buf; |
| 32 |
size_t length; |
| 33 |
size_t buf_size; |
| 34 |
bool use_bin_type; |
| 35 |
} msgpack_packer; |
| 36 |
|
| 37 |
typedef struct Packer Packer; |
| 38 |
|
| 39 |
static inline int msgpack_pack_write(msgpack_packer* pk, const char *data, size_t l) |
| 40 |
{ |
| 41 |
char* buf = pk->buf; |
| 42 |
size_t bs = pk->buf_size; |
| 43 |
size_t len = pk->length; |
| 44 |
|
| 45 |
if (len + l > bs) { |
| 46 |
bs = (len + l) * 2; |
| 47 |
buf = (char*)PyMem_Realloc(buf, bs); |
| 48 |
if (!buf) { |
| 49 |
PyErr_NoMemory(); |
| 50 |
return -1; |
| 51 |
} |
| 52 |
} |
| 53 |
memcpy(buf + len, data, l); |
| 54 |
len += l; |
| 55 |
|
| 56 |
pk->buf = buf; |
| 57 |
pk->buf_size = bs; |
| 58 |
pk->length = len; |
| 59 |
return 0; |
| 60 |
} |
| 61 |
|
| 62 |
#define msgpack_pack_append_buffer(user, buf, len) \ |
| 63 |
return msgpack_pack_write(user, (const char*)buf, len) |
| 64 |
|
| 65 |
#include "pack_template.h" |
| 66 |
|
| 67 |
#ifdef __cplusplus |
| 68 |
} |
| 69 |
#endif |