<commit_before>import { sym } from "./sys.js";
import utils from "./utils.js";
'use strict';
const pendingCmds = {};
const pendingCmdSends = [];
let sendingCommand = false;
let cmdSerial = 0;
// r2->io->frida->r2pipe->r2
let _r2 = null;
let _r_core_new = null; // eslint-disable-line camelcase
let _r_core_cmd_str = null; // eslint-disable-line camelcase
let _r_core_free = null; // eslint-disable-line camelcase,no-unused-vars
let _free = null;
function getR2Arch(arch) {
    switch (arch) {
        case 'ia32':
        case 'x64':
            return 'x86';
        case 'arm64':
            return 'arm';
    }
    return arch;
}
function hostCmds(commands) {
    let i = 0;
    function sendOne() {
        if (i < commands.length) {
            return hostCmd(commands[i]).then(() => {
                i += 1;
                return sendOne();
            });
        }
        else {
            return Promise.resolve();
        }
    }
    return sendOne();
}
function hostCmd(cmd) {
    return new Promise((resolve) => {
        const serial = cmdSerial;
        cmdSerial++;
        pendingCmds[serial] = resolve;
        _sendCommand(cmd, serial);
    });
}
function hostCmdj(cmd) {
    return hostCmd(cmd)
        .then(output => {
        return JSON.parse(output);
    });
}
function onCmdResp(params) {
    const { serial, output } = params;
    sendingCommand = false;
    if (serial in pendingCmds) {
        const onFinish = pendingCmds[serial];
        delete pendingCmds[serial];
        process.nextTick(() => onFinish(output));
    }
    else {
        throw new Error('Command response out of sync');
    }
    process.nextTick(() => {
        if (!sendingCommand) {
            const nextSend = pendingCmdSends.shift();
            if (nextSend !== undefined) {
                nextSend();
            }
        }
    });
    return [{}, null];
}
function _sendCommand(cmd, serial) {
    function sendIt() {
        sendingCommand = true;
        send(utils.wrapStanza('cmd', {
            cmd: cmd,
            serial: serial
        }));
    }
    if (sendingCommand) {
        pendingCmdSends.push(sendIt);
    }
    else {
        sendIt();
    }
}
function radareSeek(args) {
    const addr = utils.getPtr('' + args);
    const cmdstr = 's ' + (addr || '' + args);
    return cmdstr;
    // XXX hangs
    // return r2.hostCmd(cmdstr);
}
function radareCommand(args) {
    const cmd = args.join(' ');
    if (cmd.length === 0) {
        return 'Usage: :r [cmd]';
    }
    if (_radareCommandInit()) {
        return _radareCommandString(cmd);
    }
    return ':dl /tmp/libr.dylib';
}
function _radareCommandInit() {
    if (_r2) {
        return true;
    }
    if (!_r_core_new) {
        _r_core_new = sym('r_core_new', 'pointer', []);
        if (!_r_core_new) {
            console.error('ERROR: Cannot find r_core_new. Do :dl /tmp/libr.dylib');
            return false;
        }
        _r_core_cmd_str = sym('r_core_cmd_str', 'pointer', ['pointer', 'pointer']);
        _r_core_free = sym('r_core_free', 'void', ['pointer']);
        _free = sym('free', 'void', ['pointer']);
        _r2 = _r_core_new();
    }
    return true;
}
function _radareCommandString(cmd) {
    if (_r2) {
        const aCmd = Memory.allocUtf8String(cmd);
        const ptr = _r_core_cmd_str(_r2, aCmd);
        const str = Memory.readCString(ptr);
        _free(ptr);
        return str;
    }
    console.error('Warning: not calling back r2');
    return '';
}
export { getR2Arch };
export { hostCmds };
export { hostCmd };
export { hostCmdj };
export { onCmdResp };
export { radareSeek };
export { radareCommand };
export default {
    getR2Arch,
    hostCmds,
    hostCmd,
    hostCmdj,
    onCmdResp,
    radareSeek,
    radareCommand
};<commit_msg>Reindent r2<commit_after>import { sym } from './sys.js';
import utils from './utils.js';
'use strict';

const pendingCmds = {};
const pendingCmdSends = [];
let sendingCommand = false;
let cmdSerial = 0;
// r2->io->frida->r2pipe->r2
let _r2 = null;
let _r_core_new = null; // eslint-disable-line camelcase
let _r_core_cmd_str = null; // eslint-disable-line camelcase
let _r_core_free = null; // eslint-disable-line camelcase,no-unused-vars
let _free = null;

function getR2Arch (arch) {
  switch (arch) {
    case 'ia32':
    case 'x64':
      return 'x86';
    case 'arm64':
      return 'arm';
  }
  return arch;
}

function hostCmds (commands) {
  let i = 0;
  function sendOne () {
    if (i < commands.length) {
      return hostCmd(commands[i]).then(() => {
        i += 1;
        return sendOne();
      });
    } else {
      return Promise.resolve();
    }
  }
  return sendOne();
}

function hostCmd (cmd) {
  return new Promise((resolve) => {
    const serial = cmdSerial;
    cmdSerial++;
    pendingCmds[serial] = resolve;
    _sendCommand(cmd, serial);
  });
}

function hostCmdj (cmd) {
  return hostCmd(cmd)
    .then(output => {
      return JSON.parse(output);
    });
}

function onCmdResp (params) {
  const { serial, output } = params;
  sendingCommand = false;
  if (serial in pendingCmds) {
    const onFinish = pendingCmds[serial];
    delete pendingCmds[serial];
    process.nextTick(() => onFinish(output));
  } else {
    throw new Error('Command response out of sync');
  }
  process.nextTick(() => {
    if (!sendingCommand) {
      const nextSend = pendingCmdSends.shift();
      if (nextSend !== undefined) {
        nextSend();
      }
    }
  });
  return [{}, null];
}

function _sendCommand (cmd, serial) {
  function sendIt () {
    sendingCommand = true;
    send(utils.wrapStanza('cmd', {
      cmd: cmd,
      serial: serial
    }));
  }
  if (sendingCommand) {
    pendingCmdSends.push(sendIt);
  } else {
    sendIt();
  }
}

function radareSeek (args) {
  const addr = utils.getPtr('' + args);
  const cmdstr = 's ' + (addr || '' + args);
  return cmdstr;
  // XXX hangs
  // return r2.hostCmd(cmdstr);
}

function radareCommand (args) {
  const cmd = args.join(' ');
  if (cmd.length === 0) {
    return 'Usage: :r [cmd]';
  }
  if (_radareCommandInit()) {
    return _radareCommandString(cmd);
  }
  return ':dl /tmp/libr.dylib';
}

function _radareCommandInit () {
  if (_r2) {
    return true;
  }
  if (!_r_core_new) {
    _r_core_new = sym('r_core_new', 'pointer', []);
    if (!_r_core_new) {
      console.error('ERROR: Cannot find r_core_new. Do :dl /tmp/libr.dylib');
      return false;
    }
    _r_core_cmd_str = sym('r_core_cmd_str', 'pointer', ['pointer', 'pointer']);
    _r_core_free = sym('r_core_free', 'void', ['pointer']);
    _free = sym('free', 'void', ['pointer']);
    _r2 = _r_core_new();
  }
  return true;
}

function _radareCommandString (cmd) {
  if (_r2) {
    const aCmd = Memory.allocUtf8String(cmd);
    const ptr = _r_core_cmd_str(_r2, aCmd);
    const str = Memory.readCString(ptr);
    _free(ptr);
    return str;
  }
  console.error('Warning: not calling back r2');
  return '';
}

export { getR2Arch };
export { hostCmds };
export { hostCmd };
export { hostCmdj };
export { onCmdResp };
export { radareSeek };
export { radareCommand };
export default {
  getR2Arch,
  hostCmds,
  hostCmd,
  hostCmdj,
  onCmdResp,
  radareSeek,
  radareCommand
};
{'fraction_non_alphanumeric': 0.14706700027800945, 'fraction_numerical': 0.007367250486516541, 'mean_word_length': 2.5373647984267453, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, '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': '6018536', 'n_tokens_mistral': 2477, 'n_tokens_neox': 2463, 'n_words': 564}