283b622013-03-26Martin Nilsson #pike __REAL_VERSION__
71e1342013-02-17Bill Welliver  constant BSONBinary = 1;
ff17962014-08-15Martin Nilsson  protected string data; protected 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  //!
ff17962014-08-15Martin Nilsson  protected void create(string _data, int|void _subtype)
71e1342013-02-17Bill Welliver  { subtype = _subtype;
6ae4fb2016-10-20Henrik Grubbström (Grubba)  if(subtype == BINARY_OLD)
71e1342013-02-17Bill Welliver  {
dd08cc2013-05-19Martin Nilsson  if( !sscanf(data, "%-4H", data) ) throw(Error.Generic("old binary data length does not match actual data length.\n"));
71e1342013-02-17Bill Welliver  } else data = _data; }
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") {
dd08cc2013-05-19Martin Nilsson  // the docs are a little sketchy about this, do we need to NULL // terminate?
6ae4fb2016-10-20Henrik Grubbström (Grubba)  if(subtype == BINARY_OLD)
dd08cc2013-05-19Martin Nilsson  return sprintf("%-4H", data);
71e1342013-02-17Bill Welliver  else return data; }
ec1a0f2014-08-16Martin Nilsson  return UNDEFINED;
71e1342013-02-17Bill Welliver  }