<commit_before>#ifndef PK11_STRUCT_MACROS_INCLUDED
#define PK11_STRUCT_MACROS_INCLUDED

/**************************************************/
/* struct/attribute implementation                */
/**************************************************/

#define HANDLE2NUM(n) ULONG2NUM(n)
#define NUM2HANDLE(n) PKNUM2ULONG(n)
#define PKNUM2ULONG(n) pkcs11_num2ulong(n)

static VALUE
pkcs11_num2ulong(VALUE val)
{
  if (TYPE(val) == T_BIGNUM || TYPE(val) == T_FIXNUM) {
    return NUM2ULONG(val);
  }
  return NUM2ULONG(rb_to_int(val));
}

static VALUE
get_string(VALUE obj, off_t offset, size_t size)
{
  char *ptr = (char*)DATA_PTR(obj);
  return rb_str_new(ptr+offset, size);
}

static VALUE
set_string(VALUE obj, VALUE value, off_t offset, size_t size)
{
  char *ptr = (char*)DATA_PTR(obj);
  int len = size;
  StringValue(value);
  if (RSTRING_LEN(value) < len) len = RSTRING_LEN(value);
  memset(ptr+offset, 0, size);
  memcpy(ptr+offset, RSTRING_PTR(value), len);
  return value;
}

static VALUE
get_ulong(VALUE obj, off_t offset)
{
  char *ptr = (char*)DATA_PTR(obj);
  return ULONG2NUM(*(CK_ULONG_PTR)(ptr+offset));
}

static VALUE
set_ulong(VALUE obj, VALUE value, off_t offset)
{
  char *ptr = (char*)DATA_PTR(obj);
  *(CK_ULONG_PTR)(ptr+offset) = NUM2ULONG(value);
  return value;
}

static VALUE
get_byte(VALUE obj, off_t offset)
{
  char *ptr = (char*)DATA_PTR(obj);
  return ULONG2NUM(*(CK_BYTE_PTR)(ptr+offset));
}

static VALUE
set_byte(VALUE obj, VALUE value, off_t offset)
{
  char *ptr = (char*)DATA_PTR(obj);
  *(CK_BYTE_PTR)(ptr+offset) = NUM2ULONG(value);
  return value;
}

static VALUE
get_ulong_ptr(VALUE obj, off_t offset)
{
  char *ptr = (char*)DATA_PTR(obj);
  CK_ULONG_PTR p = *(CK_ULONG_PTR *)(ptr+offset);
  if (!p) return Qnil;
  return ULONG2NUM(*p);
}

static VALUE
set_ulong_ptr(VALUE obj, VALUE value, const char *name, off_t offset)
{
  VALUE new_obj;
  CK_ULONG_PTR *ptr = (CK_ULONG_PTR *)((char*)DATA_PTR(obj) + offset);
  if (NIL_P(value)){
    rb_iv_set(obj, name, value);
    *ptr = NULL_PTR;
    return value;
  }
  new_obj = Data_Make_Struct(rb_cInteger, CK_ULONG, 0, -1, *ptr);
  rb_iv_set(obj, name, new_obj);
  **ptr = NUM2ULONG(value);
  return value;
}

static VALUE<commit_msg>Use Data as base class for ULONG_PTR_ACCESSOR<commit_after>#ifndef PK11_STRUCT_MACROS_INCLUDED
#define PK11_STRUCT_MACROS_INCLUDED

/**************************************************/
/* struct/attribute implementation                */
/**************************************************/

#define HANDLE2NUM(n) ULONG2NUM(n)
#define NUM2HANDLE(n) PKNUM2ULONG(n)
#define PKNUM2ULONG(n) pkcs11_num2ulong(n)

static VALUE
pkcs11_num2ulong(VALUE val)
{
  if (TYPE(val) == T_BIGNUM || TYPE(val) == T_FIXNUM) {
    return NUM2ULONG(val);
  }
  return NUM2ULONG(rb_to_int(val));
}

static VALUE
get_string(VALUE obj, off_t offset, size_t size)
{
  char *ptr = (char*)DATA_PTR(obj);
  return rb_str_new(ptr+offset, size);
}

static VALUE
set_string(VALUE obj, VALUE value, off_t offset, size_t size)
{
  char *ptr = (char*)DATA_PTR(obj);
  int len = size;
  StringValue(value);
  if (RSTRING_LEN(value) < len) len = RSTRING_LEN(value);
  memset(ptr+offset, 0, size);
  memcpy(ptr+offset, RSTRING_PTR(value), len);
  return value;
}

static VALUE
get_ulong(VALUE obj, off_t offset)
{
  char *ptr = (char*)DATA_PTR(obj);
  return ULONG2NUM(*(CK_ULONG_PTR)(ptr+offset));
}

static VALUE
set_ulong(VALUE obj, VALUE value, off_t offset)
{
  char *ptr = (char*)DATA_PTR(obj);
  *(CK_ULONG_PTR)(ptr+offset) = NUM2ULONG(value);
  return value;
}

static VALUE
get_byte(VALUE obj, off_t offset)
{
  char *ptr = (char*)DATA_PTR(obj);
  return ULONG2NUM(*(CK_BYTE_PTR)(ptr+offset));
}

static VALUE
set_byte(VALUE obj, VALUE value, off_t offset)
{
  char *ptr = (char*)DATA_PTR(obj);
  *(CK_BYTE_PTR)(ptr+offset) = NUM2ULONG(value);
  return value;
}

static VALUE
get_ulong_ptr(VALUE obj, off_t offset)
{
  char *ptr = (char*)DATA_PTR(obj);
  CK_ULONG_PTR p = *(CK_ULONG_PTR *)(ptr+offset);
  if (!p) return Qnil;
  return ULONG2NUM(*p);
}

static VALUE
set_ulong_ptr(VALUE obj, VALUE value, const char *name, off_t offset)
{
  VALUE new_obj;
  CK_ULONG_PTR *ptr = (CK_ULONG_PTR *)((char*)DATA_PTR(obj) + offset);
  if (NIL_P(value)){
    rb_iv_set(obj, name, value);
    *ptr = NULL_PTR;
    return value;
  }
  new_obj = Data_Make_Struct(rb_cData, CK_ULONG, 0, -1, *ptr);
  rb_iv_set(obj, name, new_obj);
  **ptr = NUM2ULONG(value);
  return value;
}

static VALUE
{'fraction_non_alphanumeric': 0.17982554238425408, 'fraction_numerical': 0.011630507716394543, 'mean_word_length': 5.343262411347518, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2325614', 'n_tokens_mistral': 1815, 'n_tokens_neox': 1784, 'n_words': 375}