chaokunyang commented on code in PR #3216:
URL: https://github.com/apache/fory/pull/3216#discussion_r2730140612


##########
cpp/fory/python/pyfory.cc:
##########
@@ -58,4 +58,133 @@ int Fory_PyFloatSequenceWriteToBuffer(PyObject *collection, 
Buffer *buffer,
   }
   return 0;
 }
+
+// Write varint64 with ZigZag encoding inline
+// Returns number of bytes written
+static inline uint32_t WriteVarint64ZigZag(uint8_t *arr, int64_t value) {
+  // ZigZag encoding: (value << 1) ^ (value >> 63)
+  uint64_t v = (static_cast<uint64_t>(value) << 1) ^
+               (static_cast<uint64_t>(value >> 63));
+
+  if (v < 0x80) {
+    arr[0] = static_cast<uint8_t>(v);
+    return 1;
+  }
+  arr[0] = static_cast<uint8_t>((v & 0x7F) | 0x80);
+  if (v < 0x4000) {
+    arr[1] = static_cast<uint8_t>(v >> 7);
+    return 2;
+  }
+  arr[1] = static_cast<uint8_t>((v >> 7) | 0x80);
+  if (v < 0x200000) {
+    arr[2] = static_cast<uint8_t>(v >> 14);
+    return 3;
+  }
+  arr[2] = static_cast<uint8_t>((v >> 14) | 0x80);
+  if (v < 0x10000000) {
+    arr[3] = static_cast<uint8_t>(v >> 21);
+    return 4;
+  }
+  arr[3] = static_cast<uint8_t>((v >> 21) | 0x80);
+  if (v < 0x800000000ULL) {
+    arr[4] = static_cast<uint8_t>(v >> 28);
+    return 5;
+  }
+  arr[4] = static_cast<uint8_t>((v >> 28) | 0x80);
+  if (v < 0x40000000000ULL) {
+    arr[5] = static_cast<uint8_t>(v >> 35);
+    return 6;
+  }
+  arr[5] = static_cast<uint8_t>((v >> 35) | 0x80);
+  if (v < 0x2000000000000ULL) {
+    arr[6] = static_cast<uint8_t>(v >> 42);
+    return 7;
+  }
+  arr[6] = static_cast<uint8_t>((v >> 42) | 0x80);
+  if (v < 0x100000000000000ULL) {
+    arr[7] = static_cast<uint8_t>(v >> 49);
+    return 8;
+  }
+  arr[7] = static_cast<uint8_t>((v >> 49) | 0x80);
+  arr[8] = static_cast<uint8_t>(v >> 56);
+  return 9;
+}
+
+Py_ssize_t Fory_PyInt64SequenceWriteToBuffer(PyObject *collection,
+                                             Buffer *buffer,
+                                             Py_ssize_t start_index) {
+  PyObject **items = PySequenceGetItems(collection);
+  if (items == nullptr) {
+    return -1;
+  }
+  Py_ssize_t size = Py_SIZE(collection);
+
+  uint8_t *data = buffer->data() + start_index;
+  Py_ssize_t total_bytes = 0;
+
+  for (Py_ssize_t i = 0; i < size; i++) {
+    PyObject *item = items[i];
+    int64_t value = PyLong_AsLongLong(item);
+    if (value == -1 && PyErr_Occurred()) {
+      return -1;
+    }
+    uint32_t bytes_written = WriteVarint64ZigZag(data + total_bytes, value);
+    total_bytes += bytes_written;
+  }
+
+  return total_bytes;
+}
+
+// Read varint64 with ZigZag decoding inline
+// Returns the number of bytes read
+static inline uint32_t ReadVarint64ZigZag(const uint8_t *arr, int64_t *result) 
{

Review Comment:
   There is not buffer bnoudn chekc, it will introduce a crash in malicouse 
input data



##########
cpp/fory/python/pyfory.cc:
##########
@@ -58,4 +58,133 @@ int Fory_PyFloatSequenceWriteToBuffer(PyObject *collection, 
Buffer *buffer,
   }
   return 0;
 }
+
+// Write varint64 with ZigZag encoding inline
+// Returns number of bytes written
+static inline uint32_t WriteVarint64ZigZag(uint8_t *arr, int64_t value) {
+  // ZigZag encoding: (value << 1) ^ (value >> 63)
+  uint64_t v = (static_cast<uint64_t>(value) << 1) ^
+               (static_cast<uint64_t>(value >> 63));
+
+  if (v < 0x80) {
+    arr[0] = static_cast<uint8_t>(v);
+    return 1;
+  }
+  arr[0] = static_cast<uint8_t>((v & 0x7F) | 0x80);
+  if (v < 0x4000) {
+    arr[1] = static_cast<uint8_t>(v >> 7);
+    return 2;
+  }
+  arr[1] = static_cast<uint8_t>((v >> 7) | 0x80);
+  if (v < 0x200000) {
+    arr[2] = static_cast<uint8_t>(v >> 14);
+    return 3;
+  }
+  arr[2] = static_cast<uint8_t>((v >> 14) | 0x80);
+  if (v < 0x10000000) {
+    arr[3] = static_cast<uint8_t>(v >> 21);
+    return 4;
+  }
+  arr[3] = static_cast<uint8_t>((v >> 21) | 0x80);
+  if (v < 0x800000000ULL) {
+    arr[4] = static_cast<uint8_t>(v >> 28);
+    return 5;
+  }
+  arr[4] = static_cast<uint8_t>((v >> 28) | 0x80);
+  if (v < 0x40000000000ULL) {
+    arr[5] = static_cast<uint8_t>(v >> 35);
+    return 6;
+  }
+  arr[5] = static_cast<uint8_t>((v >> 35) | 0x80);
+  if (v < 0x2000000000000ULL) {
+    arr[6] = static_cast<uint8_t>(v >> 42);
+    return 7;
+  }
+  arr[6] = static_cast<uint8_t>((v >> 42) | 0x80);
+  if (v < 0x100000000000000ULL) {
+    arr[7] = static_cast<uint8_t>(v >> 49);
+    return 8;
+  }
+  arr[7] = static_cast<uint8_t>((v >> 49) | 0x80);
+  arr[8] = static_cast<uint8_t>(v >> 56);
+  return 9;
+}
+
+Py_ssize_t Fory_PyInt64SequenceWriteToBuffer(PyObject *collection,
+                                             Buffer *buffer,
+                                             Py_ssize_t start_index) {
+  PyObject **items = PySequenceGetItems(collection);
+  if (items == nullptr) {
+    return -1;
+  }
+  Py_ssize_t size = Py_SIZE(collection);
+
+  uint8_t *data = buffer->data() + start_index;
+  Py_ssize_t total_bytes = 0;
+
+  for (Py_ssize_t i = 0; i < size; i++) {
+    PyObject *item = items[i];
+    int64_t value = PyLong_AsLongLong(item);
+    if (value == -1 && PyErr_Occurred()) {
+      return -1;
+    }
+    uint32_t bytes_written = WriteVarint64ZigZag(data + total_bytes, value);
+    total_bytes += bytes_written;
+  }
+
+  return total_bytes;
+}
+
+// Read varint64 with ZigZag decoding inline
+// Returns the number of bytes read
+static inline uint32_t ReadVarint64ZigZag(const uint8_t *arr, int64_t *result) 
{

Review Comment:
   There is no buffer bound chekc, it will introduce a crash in malicouse input 
data



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to