<commit_before>var TRACE = false;

var net = require('net');
var util = require('util');
var events = require('events');
var pathModule = require('path');
var fsModule = require('fs');
var dateformat = require('dateformat');

var glob = require('./glob');
var starttls = require('./starttls');

/*
 TODO:
 - Implement Full RFC 959
 - Implement Full RFC 2228 [PBSZ and PROT implemented already]
 - Implement RFC 3659

 - passive command is for server to determine which port it listens on and report that to the client
 - doesn't necessarily mean it needs to be listening (i guess), but i assume it actually SHOULD be listening
 - it keeps listening for subsequent connections

 - what sort of security should i enforce? should i require the same IP for data and control connections?
 - maybe just for milesplit's use?
 */

var withCwd;
(function () {
  // Constructs regexp to remove '../' and (../ + pathModule.sep) from
  // the path. Ensures that this does not give rise to sequences such
  // as '//' in the resulting path.  (These are technically valid, but
  // if the user of the library is passing in their own fs
  // implementation, it would be a bit rude to give them such paths.)
  var pms = pathModule.sep.charCodeAt(0).toString(16); //
  while (pms.length < 4)                               //
    pms = '0' + pms;                                   // Gets pathModule.sep in escaped '\uXXXX' format.
  pms = '\\u' + pms;                                   //
  // The whole thing is wrapped in (?: ... )+ so that sequences such as '../../../' are replaced
  // all at once. This prevents multiple adjacent path separatos appearing in the result path.
  var relsRegExp = new RegExp("(?:(?:\\/|" + pms + ")?\\.\\.(?:\\/|" + pms + "))+", "g");

  withCwd = function withCwd(cwd, path) {
    // **Note on path separators** Internally we should always use
    // '/'. This is valid on Windows too and there is no need to
    // complicate the code by using pathModule.sep everywhere. However,
    // if this is running on Windows we may well get '\' (== pathModule.sep)
    // within cwd and path.  In this case we should recognize these as
    // path separators and do the right thing. This code makes no effort
    // to ensure that paths are consistent in their use of separators.
    // On Windows, paths with mixed / and \ separators should work fine.
    // (If the user of the library wants non-gross paths, they'll have to
    // make sure to pass in paths using '/' as the separator.)

    var firstChar = (path || '').charAt(0);
    cwd = cwd || '/';
    path = path || '/';

    // Remove relative path specs so that it's impossible to escape cwd.
    path = path.replace(relsRegExp, '/');

    // Could use path.join here, but I am wary of doing so because
    // we'll never be 100% sure of what kind of path specs it might
    // recognize.  Since joining paths together is pretty simple, we
    // may as well just do it manually.
    if (cwd.charAt(cwd.length-1) === '/' || cwd.charAt(cwd.length-1) === pathModule.sep)
      cwd = cwd.substr(0, cwd.length-1)
    if (path.charAt(0) === '/' || path.charAt(0) === pathModule.sep)
      path = path.substr(1);
    return cwd + '/' + path;
  }
})();
exports.withCwd = withCwd;


// Currently used for stripping options from beginning of argument to LIST and NLST.
function stripOptions(str) {
  var IN_SPACE = 0, IN_DASH = 1;
  var state = IN_SPACE;
  for (var i = 0; i < str.length; ++i) {
    var c = str.charAt(i);
    if (state == IN_SPACE) {
      if (c == ' ' || c == '\t')
        ;
      else if (c == '-')<commit_msg>Revert "Attempt to fix security issue with withCwd and '../' in paths."<commit_after>var TRACE = false;

var net = require('net');
var util = require('util');
var events = require('events');
var pathModule = require('path');
var fsModule = require('fs');
var dateformat = require('dateformat');

var glob = require('./glob');
var starttls = require('./starttls');

/*
 TODO:
 - Implement Full RFC 959
 - Implement Full RFC 2228 [PBSZ and PROT implemented already]
 - Implement RFC 3659

 - passive command is for server to determine which port it listens on and report that to the client
 - doesn't necessarily mean it needs to be listening (i guess), but i assume it actually SHOULD be listening
 - it keeps listening for subsequent connections

 - what sort of security should i enforce? should i require the same IP for data and control connections?
 - maybe just for milesplit's use?
 */

function withCwd(cwd, path) {
  var firstChar = (path || '').charAt(0);
  cwd = cwd || pathModule.sep;
  path = path || pathModule.sep;
  if (firstChar === '/' || firstChar === pathModule.sep) {
    cwd = pathModule.sep;
  }
  path = pathModule.join(cwd, path);
  return path;
}

// Currently used for stripping options from beginning of argument to LIST and NLST.
function stripOptions(str) {
  var IN_SPACE = 0, IN_DASH = 1;
  var state = IN_SPACE;
  for (var i = 0; i < str.length; ++i) {
    var c = str.charAt(i);
    if (state == IN_SPACE) {
      if (c == ' ' || c == '\t')
        ;
      else if (c == '-')
{'fraction_non_alphanumeric': 0.12850146914789423, 'fraction_numerical': 0.00881488736532811, 'mean_word_length': 2.748898678414097, 'pattern_counts': {'":': 0, '<': 6, '<?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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '285472', 'n_tokens_mistral': 1564, 'n_tokens_neox': 1511, 'n_words': 637}