Turn audio into a shareable video. forked from nypublicradio/audiogram

node_buffer.h 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright Joyent, Inc. and other Node contributors.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a
  4. // copy of this software and associated documentation files (the
  5. // "Software"), to deal in the Software without restriction, including
  6. // without limitation the rights to use, copy, modify, merge, publish,
  7. // distribute, sublicense, and/or sell copies of the Software, and to permit
  8. // persons to whom the Software is furnished to do so, subject to the
  9. // following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included
  12. // in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  17. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #ifndef SRC_NODE_BUFFER_H_
  22. #define SRC_NODE_BUFFER_H_
  23. #include "node.h"
  24. #include "v8.h"
  25. namespace node {
  26. namespace Buffer {
  27. static const unsigned int kMaxLength = v8::TypedArray::kMaxLength;
  28. typedef void (*FreeCallback)(char* data, void* hint);
  29. NODE_EXTERN bool HasInstance(v8::Local<v8::Value> val);
  30. NODE_EXTERN bool HasInstance(v8::Local<v8::Object> val);
  31. NODE_EXTERN char* Data(v8::Local<v8::Value> val);
  32. NODE_EXTERN char* Data(v8::Local<v8::Object> val);
  33. NODE_EXTERN size_t Length(v8::Local<v8::Value> val);
  34. NODE_EXTERN size_t Length(v8::Local<v8::Object> val);
  35. // public constructor - data is copied
  36. NODE_EXTERN v8::MaybeLocal<v8::Object> Copy(v8::Isolate* isolate,
  37. const char* data,
  38. size_t len);
  39. // public constructor
  40. NODE_EXTERN v8::MaybeLocal<v8::Object> New(v8::Isolate* isolate, size_t length);
  41. // public constructor from string
  42. NODE_EXTERN v8::MaybeLocal<v8::Object> New(v8::Isolate* isolate,
  43. v8::Local<v8::String> string,
  44. enum encoding enc = UTF8);
  45. // public constructor - data is used, callback is passed data on object gc
  46. NODE_EXTERN v8::MaybeLocal<v8::Object> New(v8::Isolate* isolate,
  47. char* data,
  48. size_t length,
  49. FreeCallback callback,
  50. void* hint);
  51. // public constructor - data is used.
  52. NODE_EXTERN v8::MaybeLocal<v8::Object> New(v8::Isolate* isolate,
  53. char* data,
  54. size_t len);
  55. // This is verbose to be explicit with inline commenting
  56. static inline bool IsWithinBounds(size_t off, size_t len, size_t max) {
  57. // Asking to seek too far into the buffer
  58. // check to avoid wrapping in subsequent subtraction
  59. if (off > max)
  60. return false;
  61. // Asking for more than is left over in the buffer
  62. if (max - off < len)
  63. return false;
  64. // Otherwise we're in bounds
  65. return true;
  66. }
  67. } // namespace Buffer
  68. } // namespace node
  69. #endif // SRC_NODE_BUFFER_H_