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
25 #ifdef _MSC_VER
26 #define inline __inline
27 #endif
28
29 typedef struct msgpack_packer {
30 char *buf;
31 size_t length;
32 size_t buf_size;
33 } msgpack_packer;
34
35 typedef struct Packer Packer;
36
37
msgpack_pack_write(msgpack_packer * pk,const char * data,size_t l)38 static inline int msgpack_pack_write(msgpack_packer* pk, const char *data, size_t l)
39 {
40 char* buf = pk->buf;
41 size_t bs = pk->buf_size;
42 size_t len = pk->length;
43
44 if (len + l > bs) {
45 bs = (len + l) * 2;
46 buf = (char*)PyMem_Realloc(buf, bs);
47 if (!buf) {
48 PyErr_NoMemory();
49 return -1;
50 }
51 }
52 memcpy(buf + len, data, l);
53 len += l;
54
55 pk->buf = buf;
56 pk->buf_size = bs;
57 pk->length = len;
58 return 0;
59 }
60
61 #define msgpack_pack_append_buffer(user, buf, len) \
62 return msgpack_pack_write(user, (const char*)buf, len)
63
64 #include "pack_template.h"
65
66 // return -2 when o is too long
67 static inline int
msgpack_pack_unicode(msgpack_packer * pk,PyObject * o,long long limit)68 msgpack_pack_unicode(msgpack_packer *pk, PyObject *o, long long limit)
69 {
70 #if PY_MAJOR_VERSION >= 3
71 assert(PyUnicode_Check(o));
72
73 Py_ssize_t len;
74 const char* buf = PyUnicode_AsUTF8AndSize(o, &len);
75 if (buf == NULL)
76 return -1;
77
78 if (len > limit) {
79 return -2;
80 }
81
82 int ret = msgpack_pack_raw(pk, len);
83 if (ret) return ret;
84
85 return msgpack_pack_raw_body(pk, buf, len);
86 #else
87 PyObject *bytes;
88 Py_ssize_t len;
89 int ret;
90
91 // py2
92 bytes = PyUnicode_AsUTF8String(o);
93 if (bytes == NULL)
94 return -1;
95
96 len = PyString_GET_SIZE(bytes);
97 if (len > limit) {
98 Py_DECREF(bytes);
99 return -2;
100 }
101
102 ret = msgpack_pack_raw(pk, len);
103 if (ret) {
104 Py_DECREF(bytes);
105 return -1;
106 }
107 ret = msgpack_pack_raw_body(pk, PyString_AS_STRING(bytes), len);
108 Py_DECREF(bytes);
109 return ret;
110 #endif
111 }
112
113