<commit_before>// Copyright 2013, <NAME>
// See LICENCE for more information

#include <string.h>

#include "reader.h"
#include "desfire.h"

#ifndef USE_LIBNFC
void reader_timer_callback(uv_timer_t *handle, int timer_status) {
  HandleScope scope;
  reader_data *data = static_cast<reader_data *>(handle->data);
  LONG res;
  DWORD event;
  Local<String> status;
  Local<Object> reader = Local<Object>::New(data->self);
  reader->Set(String::NewSymbol("name"), String::New(data->name.c_str()));

  res = SCardGetStatusChange(data->context->context, 1, &data->state, 1);
  if(res == SCARD_S_SUCCESS) {
    event = data->state.dwEventState;
    if(event & SCARD_STATE_CHANGED) {
      data->state.dwCurrentState = event;
      if(event & SCARD_STATE_IGNORE) {
        status = v8::String::New("ignore");
      } else if(event & SCARD_STATE_UNKNOWN) {
        status = v8::String::New("unknown");
      } else if(event & SCARD_STATE_UNAVAILABLE) {
        status = v8::String::New("unavailable");
      } else if(event & SCARD_STATE_EMPTY) {
        status = v8::String::New("empty");
      } else if(event & SCARD_STATE_PRESENT) {
        status = v8::String::New("present");
      } else if(event & SCARD_STATE_ATRMATCH) {
        status = v8::String::New("atrmatch");
      } else if(event & SCARD_STATE_EXCLUSIVE) {
        status = v8::String::New("exclusive");
      } else if(event & SCARD_STATE_INUSE) {
        status = v8::String::New("inuse");
      } else if(event & SCARD_STATE_MUTE) {
        status = v8::String::New("mute");
      }

      // Prepare readerObject event
      reader->Set(String::NewSymbol("status"), v8::Local<v8::Value>::New(status));

      // Card object, will be eventually filled lateron
      if(event & SCARD_STATE_PRESENT) {
        if(data->state.cbAtr>0) {
        }

        // Establishes a connection to a smart card contained by a specific reader.
        MifareTag *tags = freefare_get_tags_pcsc(data->context, data->state.szReader);
        // XXX: With PCSC tags is always length 2 with {tag, NULL} we assume this is allways the case here!!!!
        smart_tags *st = new smart_tags(tags);
        for(int i = 0; (!res) && tags[i]; i++) {
          if(tags[i] && freefare_get_tag_type(tags[i]) == DESFIRE) {

            card_data *cardData = new card_data(data, st);
            cardData->tag = tags[i];
            Local<Object> card = Object::New();
            card->Set(String::NewSymbol("type"), String::New("desfire"));
            card->SetHiddenValue(String::NewSymbol("data"), External::Wrap(cardData));

            card->Set(String::NewSymbol("info"), FunctionTemplate::New(CardInfo)->GetFunction());
            card->Set(String::NewSymbol("masterKeyInfo"), FunctionTemplate::New(CardMasterKeyInfo)->GetFunction());
            card->Set(String::NewSymbol("keyVersion"), FunctionTemplate::New(CardKeyVersion)->GetFunction());
            card->Set(String::NewSymbol("freeMemory"), FunctionTemplate::New(CardFreeMemory)->GetFunction());
            card->Set(String::NewSymbol("setKey"), FunctionTemplate::New(CardSetKey)->GetFunction());
            card->Set(String::NewSymbol("setAid"), FunctionTemplate::New(CardSetAid)->GetFunction());
            card->Set(String::NewSymbol("format"), FunctionTemplate::New(CardFormat)->GetFunction());
            card->Set(String::NewSymbol("createNdef"), FunctionTemplate::New(CardCreateNdef)->GetFunction());
            card->Set(String::NewSymbol("readNdef"), FunctionTemplate::New(CardReadNdef)->GetFunction());
            card->Set(String::NewSymbol("writeNdef"), FunctionTemplate::New(CardWriteNdef)->GetFunction());
            card->Set(String::NewSymbol("free"), FunctionTemplate::New(CardFree)->GetFunction());

            const unsigned argc = 3;
            Local<Value> argv[argc] = {
              Local<Value>::New(Undefined()),
              Local<Value>::New(reader),
              Local<Value>::New(card)
            };
            data->callback->Call(Context::GetCurrent()->Global(), argc, argv);

            //delete cardData;
          }
        }
        //freefare_free_tags(tags);
      } else {
        const unsigned argc = 3;
        Local<Value> argv[argc] = {
          Local<Value>::New(Undefined()),
          Local<Value>::New(reader),
          Local<Value>::New(Undefined())
        };
        data->callback->Call(Context::GetCurrent()->Global(), argc, argv);
      }
    }
  } else if(static_cast<unsigned int>(res) == SCARD_E_TIMEOUT) {
      reader->Set(String::NewSymbol("status"), v8::Local<v8::Value>::New(v8::String::New("timeout")));
      const unsigned argc = 3;
      Local<Value> argv[argc] = {
        Local<Value>::New(Undefined()),
        Local<Value>::New(reader),
        Local<Value>::New(Undefined())
      };
      data->callback->Call(Context::GetCurrent()->Global(), argc, argv);
  } else {
      reader->Set(String::NewSymbol("status"), v8::Local<v8::Value>::New(v8::String::New("unknown")));
      const unsigned argc = 3;
      Local<Value> argv[argc] = {
        Local<Value>::New(Undefined()),
        Local<Value>::New(reader),
        Local<Value>::New(Undefined())
      };
      data->callback->Call(Context::GetCurrent()->Global(), argc, argv);
  }
}
#else // USE_LIBNFC
inline Local<String> make_status(const char* const s) {
  return v8::String::New(s);
}
void reader_timer_callback(uv_timer_t *handle, int timer_status) {
  HandleScope scope;
  reader_data *data = static_cast<reader_data *>(handle->data);
  Local<String> status;
  Local<Object> reader = Local<Object>::New(data->self);
  reader->Set(String::NewSymbol("name"), String::New(data->name.c_str()));
  if (!data->device) {
    reader->Set(String::NewSymbol("status"), v8::Local<v8::Value>::New(make_status("unavailable")));
    const unsigned argc = 3;
    Local<Value> err = String::New("No NFC device associated with this reader");
    Local<Value> argv[argc] = {
      Local<Value>::New(err),
      Local<Value>::New(reader),
      Local<Value>::New(Undefined())
    };
    data->callback->Call(Context::GetCurrent()->Global(), argc, argv);
    return;
  }

  MifareTag *tags = freefare_get_tags(data->device);
  int err = nfc_device_get_last_error(data->device);
  // return on all but success cases
  // for succes, we have to distinghish between empty and present
  if (err != NFC_SUCCESS && err == data->last_err) {
    freefare_free_tags(tags);
    scope.Close(Undefined());
    return;
  }
  if (err == NFC_SUCCESS && tags != NULL && tags[0] != NULL) { // found more than 0 tags -> present
    data->last_err = err;

    MifareTag t = NULL;
    MifareTag old_tag;
    size_t len;
    char *old_tag_uid, *t_uid;
    // we definetly found new tags if previous search did not get any
    // else we have to compare in the loop below
    bool found_new_tag = data->last_uids.size() == 0;
    for(std::vector<char *>::const_iterator i = data->last_uids.begin(); !found_new_tag && i != data->last_uids.end(); ++i) {
      found_new_tag = true; // assume tag is new
      old_tag_uid = *i;
      int tag_count = 0;
      for (t = tags[0]; t != NULL && found_new_tag; t=tags[++tag_count]) {
        t_uid = freefare_get_tag_uid(t);
        if(0 == strcmp(old_tag_uid, t_uid)) {
          found_new_tag = false; // unless it matches a known uid
        }
        free(t_uid);<commit_msg>avoid waiting in mifare_desfire_connect if no tag is present<commit_after>// Copyright 2013, <NAME>
// See LICENCE for more information

#include <string.h>

#include "reader.h"
#include "desfire.h"

#ifndef USE_LIBNFC
void reader_timer_callback(uv_timer_t *handle, int timer_status) {
  HandleScope scope;
  reader_data *data = static_cast<reader_data *>(handle->data);
  LONG res;
  DWORD event;
  Local<String> status;
  Local<Object> reader = Local<Object>::New(data->self);
  reader->Set(String::NewSymbol("name"), String::New(data->name.c_str()));

  res = SCardGetStatusChange(data->context->context, 1, &data->state, 1);
  if(res == SCARD_S_SUCCESS) {
    event = data->state.dwEventState;
    if(event & SCARD_STATE_CHANGED) {
      data->state.dwCurrentState = event;
      if(event & SCARD_STATE_IGNORE) {
        status = v8::String::New("ignore");
      } else if(event & SCARD_STATE_UNKNOWN) {
        status = v8::String::New("unknown");
      } else if(event & SCARD_STATE_UNAVAILABLE) {
        status = v8::String::New("unavailable");
      } else if(event & SCARD_STATE_EMPTY) {
        status = v8::String::New("empty");
      } else if(event & SCARD_STATE_PRESENT) {
        status = v8::String::New("present");
      } else if(event & SCARD_STATE_ATRMATCH) {
        status = v8::String::New("atrmatch");
      } else if(event & SCARD_STATE_EXCLUSIVE) {
        status = v8::String::New("exclusive");
      } else if(event & SCARD_STATE_INUSE) {
        status = v8::String::New("inuse");
      } else if(event & SCARD_STATE_MUTE) {
        status = v8::String::New("mute");
      }

      // Prepare readerObject event
      reader->Set(String::NewSymbol("status"), v8::Local<v8::Value>::New(status));

      // Card object, will be eventually filled lateron
      if(event & SCARD_STATE_PRESENT) {
        if(data->state.cbAtr>0) {
        }

        // Establishes a connection to a smart card contained by a specific reader.
        MifareTag *tags = freefare_get_tags_pcsc(data->context, data->state.szReader);
        // XXX: With PCSC tags is always length 2 with {tag, NULL} we assume this is allways the case here!!!!
        smart_tags *st = new smart_tags(tags);
        for(int i = 0; (!res) && tags[i]; i++) {
          if(tags[i] && freefare_get_tag_type(tags[i]) == DESFIRE) {

            card_data *cardData = new card_data(data, st);
            cardData->tag = tags[i];
            Local<Object> card = Object::New();
            card->Set(String::NewSymbol("type"), String::New("desfire"));
            card->SetHiddenValue(String::NewSymbol("data"), External::Wrap(cardData));

            card->Set(String::NewSymbol("info"), FunctionTemplate::New(CardInfo)->GetFunction());
            card->Set(String::NewSymbol("masterKeyInfo"), FunctionTemplate::New(CardMasterKeyInfo)->GetFunction());
            card->Set(String::NewSymbol("keyVersion"), FunctionTemplate::New(CardKeyVersion)->GetFunction());
            card->Set(String::NewSymbol("freeMemory"), FunctionTemplate::New(CardFreeMemory)->GetFunction());
            card->Set(String::NewSymbol("setKey"), FunctionTemplate::New(CardSetKey)->GetFunction());
            card->Set(String::NewSymbol("setAid"), FunctionTemplate::New(CardSetAid)->GetFunction());
            card->Set(String::NewSymbol("format"), FunctionTemplate::New(CardFormat)->GetFunction());
            card->Set(String::NewSymbol("createNdef"), FunctionTemplate::New(CardCreateNdef)->GetFunction());
            card->Set(String::NewSymbol("readNdef"), FunctionTemplate::New(CardReadNdef)->GetFunction());
            card->Set(String::NewSymbol("writeNdef"), FunctionTemplate::New(CardWriteNdef)->GetFunction());
            card->Set(String::NewSymbol("free"), FunctionTemplate::New(CardFree)->GetFunction());

            const unsigned argc = 3;
            Local<Value> argv[argc] = {
              Local<Value>::New(Undefined()),
              Local<Value>::New(reader),
              Local<Value>::New(card)
            };
            data->callback->Call(Context::GetCurrent()->Global(), argc, argv);

            //delete cardData;
          }
        }
        //freefare_free_tags(tags);
      } else {
        const unsigned argc = 3;
        Local<Value> argv[argc] = {
          Local<Value>::New(Undefined()),
          Local<Value>::New(reader),
          Local<Value>::New(Undefined())
        };
        data->callback->Call(Context::GetCurrent()->Global(), argc, argv);
      }
    }
  } else if(static_cast<unsigned int>(res) == SCARD_E_TIMEOUT) {
      reader->Set(String::NewSymbol("status"), v8::Local<v8::Value>::New(v8::String::New("timeout")));
      const unsigned argc = 3;
      Local<Value> argv[argc] = {
        Local<Value>::New(Undefined()),
        Local<Value>::New(reader),
        Local<Value>::New(Undefined())
      };
      data->callback->Call(Context::GetCurrent()->Global(), argc, argv);
  } else {
      reader->Set(String::NewSymbol("status"), v8::Local<v8::Value>::New(v8::String::New("unknown")));
      const unsigned argc = 3;
      Local<Value> argv[argc] = {
        Local<Value>::New(Undefined()),
        Local<Value>::New(reader),
        Local<Value>::New(Undefined())
      };
      data->callback->Call(Context::GetCurrent()->Global(), argc, argv);
  }
}
#else // USE_LIBNFC
inline Local<String> make_status(const char* const s) {
  return v8::String::New(s);
}
void reader_timer_callback(uv_timer_t *handle, int timer_status) {
  HandleScope scope;
  reader_data *data = static_cast<reader_data *>(handle->data);
  Local<String> status;
  Local<Object> reader = Local<Object>::New(data->self);
  reader->Set(String::NewSymbol("name"), String::New(data->name.c_str()));
  if (!data->device) {
    reader->Set(String::NewSymbol("status"), v8::Local<v8::Value>::New(make_status("unavailable")));
    const unsigned argc = 3;
    Local<Value> err = String::New("No NFC device associated with this reader");
    Local<Value> argv[argc] = {
      Local<Value>::New(err),
      Local<Value>::New(reader),
      Local<Value>::New(Undefined())
    };
    data->callback->Call(Context::GetCurrent()->Global(), argc, argv);
    return;
  }

  MifareTag *tags = freefare_get_tags(data->device);
  int err = nfc_device_get_last_error(data->device);
  nfc_device_set_property_bool(data->device, NP_INFINITE_SELECT, false);
  // return on all but success cases
  // for succes, we have to distinghish between empty and present
  if (err != NFC_SUCCESS && err == data->last_err) {
    freefare_free_tags(tags);
    scope.Close(Undefined());
    return;
  }
  if (err == NFC_SUCCESS && tags != NULL && tags[0] != NULL) { // found more than 0 tags -> present
    data->last_err = err;

    MifareTag t = NULL;
    MifareTag old_tag;
    size_t len;
    char *old_tag_uid, *t_uid;
    // we definetly found new tags if previous search did not get any
    // else we have to compare in the loop below
    bool found_new_tag = data->last_uids.size() == 0;
    for(std::vector<char *>::const_iterator i = data->last_uids.begin(); !found_new_tag && i != data->last_uids.end(); ++i) {
      found_new_tag = true; // assume tag is new
      old_tag_uid = *i;
      int tag_count = 0;
      for (t = tags[0]; t != NULL && found_new_tag; t=tags[++tag_count]) {
        t_uid = freefare_get_tag_uid(t);
        if(0 == strcmp(old_tag_uid, t_uid)) {
          found_new_tag = false; // unless it matches a known uid
        }
        free(t_uid);
{'fraction_non_alphanumeric': 0.16657641803167728, 'fraction_numerical': 0.005414918099363747, 'mean_word_length': 3.4249775381850855, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 222, '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': '1622128', 'n_tokens_mistral': 4631, 'n_tokens_neox': 4580, 'n_words': 870}