amf_inl.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. #ifndef BRPC_AMF_INL_H
  18. #define BRPC_AMF_INL_H
  19. void* fast_memcpy(void *__restrict dest, const void *__restrict src, size_t n);
  20. namespace brpc {
  21. inline size_t AMFInputStream::cutn(void* out, size_t n) {
  22. const size_t saved_n = n;
  23. do {
  24. if (_size >= (int64_t)n) {
  25. memcpy(out, _data, n);
  26. _data = (const char*)_data + n;
  27. _size -= n;
  28. _popped_bytes += saved_n;
  29. return saved_n;
  30. }
  31. if (_size) {
  32. memcpy(out, _data, _size);
  33. out = (char*)out + _size;
  34. n -= _size;
  35. }
  36. } while (_zc_stream->Next(&_data, &_size));
  37. _data = NULL;
  38. _size = 0;
  39. _popped_bytes += saved_n - n;
  40. return saved_n - n;
  41. }
  42. inline bool AMFInputStream::check_emptiness() {
  43. return _size == 0 && !_zc_stream->Next(&_data, &_size);
  44. }
  45. inline size_t AMFInputStream::cut_u8(uint8_t* val) {
  46. if (_size >= 1) {
  47. *val = *(uint8_t*)_data;
  48. _data = (const char*)_data + 1;
  49. _size -= 1;
  50. _popped_bytes += 1;
  51. return 1;
  52. }
  53. return cutn(val, 1);
  54. }
  55. inline size_t AMFInputStream::cut_u16(uint16_t* val) {
  56. if (_size >= 2) {
  57. const uint16_t netval = *(uint16_t*)_data;
  58. *val = butil::NetToHost16(netval);
  59. _data = (const char*)_data + 2;
  60. _size -= 2;
  61. _popped_bytes += 2;
  62. return 2;
  63. }
  64. uint16_t netval = 0;
  65. const size_t ret = cutn(&netval, 2);
  66. *val = butil::NetToHost16(netval);
  67. return ret;
  68. }
  69. inline size_t AMFInputStream::cut_u32(uint32_t* val) {
  70. if (_size >= 4) {
  71. const uint32_t netval = *(uint32_t*)_data;
  72. *val = butil::NetToHost32(netval);
  73. _data = (const char*)_data + 4;
  74. _size -= 4;
  75. _popped_bytes += 4;
  76. return 4;
  77. }
  78. uint32_t netval = 0;
  79. const size_t ret = cutn(&netval, 4);
  80. *val = butil::NetToHost32(netval);
  81. return ret;
  82. }
  83. inline size_t AMFInputStream::cut_u64(uint64_t* val) {
  84. if (_size >= 8) {
  85. const uint64_t netval = *(uint64_t*)_data;
  86. *val = butil::NetToHost64(netval);
  87. _data = (const char*)_data + 8;
  88. _size -= 8;
  89. _popped_bytes += 8;
  90. return 8;
  91. }
  92. uint64_t netval = 0;
  93. const size_t ret = cutn(&netval, 8);
  94. *val = butil::NetToHost64(netval);
  95. return ret;
  96. }
  97. inline void AMFOutputStream::done() {
  98. if (_good && _size) {
  99. _zc_stream->BackUp(_size);
  100. _size = 0;
  101. }
  102. }
  103. inline void AMFOutputStream::putn(const void* data, int n) {
  104. const int saved_n = n;
  105. do {
  106. if (n <= _size) {
  107. fast_memcpy(_data, data, n);
  108. _data = (char*)_data + n;
  109. _size -= n;
  110. _pushed_bytes += saved_n;
  111. return;
  112. }
  113. fast_memcpy(_data, data, _size);
  114. data = (const char*)data + _size;
  115. n -= _size;
  116. } while (_zc_stream->Next(&_data, &_size));
  117. _data = NULL;
  118. _size = 0;
  119. _pushed_bytes += (saved_n - n);
  120. if (n) {
  121. set_bad();
  122. }
  123. }
  124. inline void AMFOutputStream::put_u8(uint8_t val) {
  125. do {
  126. if (_size > 0) {
  127. *(uint8_t*)_data = val;
  128. _data = (char*)_data + 1;
  129. --_size;
  130. ++_pushed_bytes;
  131. return;
  132. }
  133. } while (_zc_stream->Next(&_data, &_size));
  134. _data = NULL;
  135. _size = 0;
  136. set_bad();
  137. }
  138. inline void AMFOutputStream::put_u16(uint16_t val) {
  139. uint16_t netval = butil::HostToNet16(val);
  140. return putn(&netval, sizeof(netval));
  141. }
  142. inline void AMFOutputStream::put_u32(uint32_t val) {
  143. uint32_t netval = butil::HostToNet32(val);
  144. return putn(&netval, sizeof(netval));
  145. }
  146. inline void AMFOutputStream::put_u64(uint64_t val) {
  147. uint64_t netval = butil::HostToNet64(val);
  148. return putn(&netval, sizeof(netval));
  149. }
  150. } // namespace brpc
  151. #endif // BRPC_AMF_INL_H