283b622013-03-26Martin Nilsson #pike __REAL_VERSION__
71e1342013-02-17Bill Welliver  constant BSONBinary = 1;
8c007e2017-07-19Martin Nilsson  string data; int subtype = 0x00;
3524712015-05-26Martin Nilsson 
6ae4fb2016-10-20Henrik Grubbström (Grubba)  // NB: Code duplication from module.pmod to avoid circular dependencies. private constant BINARY_OLD = 0x02;
71e1342013-02-17Bill Welliver  //!
8c007e2017-07-19Martin Nilsson  protected void create(string data, int|void subtype)
71e1342013-02-17Bill Welliver  {
8c007e2017-07-19Martin Nilsson  this::subtype = subtype; this::data = data; if(subtype == BINARY_OLD) {
8cd4732017-07-27Martin Nilsson  if( !sscanf(data, "%-4H", this::data) )
8c007e2017-07-19Martin Nilsson  throw(Error.Generic("old binary data length does not match actual data length.\n")); }
71e1342013-02-17Bill Welliver  }
3524712015-05-26Martin Nilsson 
71e1342013-02-17Bill Welliver  int get_subtype() { return subtype; } void set_subtype(int _subtype) { subtype = _subtype; }
dd08cc2013-05-19Martin Nilsson 
ff17962014-08-15Martin Nilsson  protected int _sizeof()
3524712015-05-26Martin Nilsson  {
6ae4fb2016-10-20Henrik Grubbström (Grubba)  if(subtype == BINARY_OLD)
71e1342013-02-17Bill Welliver  return sizeof(data) + 4; else return sizeof(data); }
3524712015-05-26Martin Nilsson 
ff17962014-08-15Martin Nilsson  protected mixed cast(string type)
71e1342013-02-17Bill Welliver  { if(type == "string")
d996552017-07-16Martin Nilsson  return data;
ec1a0f2014-08-16Martin Nilsson  return UNDEFINED;
71e1342013-02-17Bill Welliver  }
8c007e2017-07-19Martin Nilsson  protected string _sprintf(int t) { return t=='O' && sprintf("%O(0x%02x, %O)", this_program, subtype, data); }