<commit_before>/****************************************************************************
*   PROJECT: Squeak port for Win32 (NT / Win95)
*   FILE:    sqWin32Directory.c
*   CONTENT: Directory management
*
*   AUTHOR:  <NAME> (ar)
*   ADDRESS: University of Magdeburg, Germany
*   EMAIL:   <EMAIL>
*
*   UPDATES:
*     1) Support for long path names added by using UNC prefix in that case
*        (<NAME>, Hasso Plattner Institute, Postdam, Germany)
*
*****************************************************************************/
#include <windows.h>

#include "sq.h"
#include "sqWin32File.h"
# include <sys/types.h>
# include <sys/stat.h>

/**
 * Posix permissions are not defined in Windows, except when using
 * Mingw or Cygwin. Since these constants are just standard, we define
 * them for our purpose of emulating permissions.
 */
#ifndef S_IRUSR
#define S_IRUSR 0400
#define S_IWUSR 0200
#define S_IXUSR 0100
#endif

extern struct VirtualMachine *interpreterProxy;

#define FAIL() { return interpreterProxy->primitiveFail(); }

/***
	The interface to the directory primitive is path based.
	That is, the client supplies a Squeak string describing
	the path to the directory on every call. To avoid traversing
	this path on every call, a cache is maintained of the last
	path seen.
***/

/*** Constants ***/
#define ENTRY_FOUND     0
#define NO_MORE_ENTRIES 1
#define BAD_PATH        2

#define DEFAULT_DRIVE_PERMISSIONS (S_IRUSR | (S_IRUSR>>3) | (S_IRUSR>>6) | \
                                   S_IWUSR | (S_IWUSR>>3) | (S_IWUSR>>6) | \
                                   S_IXUSR | (S_IXUSR>>3) | (S_IXUSR>>6))

static void read_permissions(sqInt *posixPermissions, WCHAR* path, sqInt pathLength, sqInt attr)
{
  *posixPermissions |= S_IRUSR | (S_IRUSR>>3) | (S_IRUSR>>6);
  if (!(attr & FILE_ATTRIBUTE_READONLY)) {
    *posixPermissions |= S_IWUSR | (S_IWUSR>>3) | (S_IWUSR>>6);
  }
  if (attr & FILE_ATTRIBUTE_DIRECTORY) {
    *posixPermissions |= S_IXUSR | (S_IXUSR>>3) | (S_IXUSR>>6);
  }
  else if (path && path[pathLength - 4] == L'.') {
    WCHAR *ext = &path[pathLength - 3];
    if (!_wcsicmp (ext, L"COM")) {
      *posixPermissions |= S_IXUSR | (S_IXUSR>>3) | (S_IXUSR>>6);
    }
    else if (!_wcsicmp (ext, L"EXE")) {
      *posixPermissions |= S_IXUSR | (S_IXUSR>>3) | (S_IXUSR>>6);
    }
    else if (!_wcsicmp (ext, L"BAT")) {
      *posixPermissions |= S_IXUSR | (S_IXUSR>>3) | (S_IXUSR>>6);
    }
    else if (!_wcsicmp (ext, L"CMD")) {
      *posixPermissions |= S_IXUSR | (S_IXUSR>>3) | (S_IXUSR>>6);
    }<commit_msg>backport pathLength protection<commit_after>/****************************************************************************
*   PROJECT: Squeak port for Win32 (NT / Win95)
*   FILE:    sqWin32Directory.c
*   CONTENT: Directory management
*
*   AUTHOR:  <NAME> (ar)
*   ADDRESS: University of Magdeburg, Germany
*   EMAIL:   <EMAIL>
*
*   UPDATES:
*     1) Support for long path names added by using UNC prefix in that case
*        (<NAME>, Hasso Plattner Institute, Postdam, Germany)
*
*****************************************************************************/
#include <windows.h>

#include "sq.h"
#include "sqWin32File.h"
# include <sys/types.h>
# include <sys/stat.h>

/**
 * Posix permissions are not defined in Windows, except when using
 * Mingw or Cygwin. Since these constants are just standard, we define
 * them for our purpose of emulating permissions.
 */
#ifndef S_IRUSR
#define S_IRUSR 0400
#define S_IWUSR 0200
#define S_IXUSR 0100
#endif

extern struct VirtualMachine *interpreterProxy;

#define FAIL() { return interpreterProxy->primitiveFail(); }

/***
	The interface to the directory primitive is path based.
	That is, the client supplies a Squeak string describing
	the path to the directory on every call. To avoid traversing
	this path on every call, a cache is maintained of the last
	path seen.
***/

/*** Constants ***/
#define ENTRY_FOUND     0
#define NO_MORE_ENTRIES 1
#define BAD_PATH        2

#define DEFAULT_DRIVE_PERMISSIONS (S_IRUSR | (S_IRUSR>>3) | (S_IRUSR>>6) | \
                                   S_IWUSR | (S_IWUSR>>3) | (S_IWUSR>>6) | \
                                   S_IXUSR | (S_IXUSR>>3) | (S_IXUSR>>6))

static void read_permissions(sqInt *posixPermissions, WCHAR* path, sqInt pathLength, sqInt attr)
{
  *posixPermissions |= S_IRUSR | (S_IRUSR>>3) | (S_IRUSR>>6);
  if (!(attr & FILE_ATTRIBUTE_READONLY)) {
    *posixPermissions |= S_IWUSR | (S_IWUSR>>3) | (S_IWUSR>>6);
  }
  if (attr & FILE_ATTRIBUTE_DIRECTORY) {
    *posixPermissions |= S_IXUSR | (S_IXUSR>>3) | (S_IXUSR>>6);
  }
  else if (path && pathLength > 3 && path[pathLength - 4] == L'.') {
    WCHAR *ext = &path[pathLength - 3];
    if (!_wcsicmp (ext, L"COM")) {
      *posixPermissions |= S_IXUSR | (S_IXUSR>>3) | (S_IXUSR>>6);
    }
    else if (!_wcsicmp (ext, L"EXE")) {
      *posixPermissions |= S_IXUSR | (S_IXUSR>>3) | (S_IXUSR>>6);
    }
    else if (!_wcsicmp (ext, L"BAT")) {
      *posixPermissions |= S_IXUSR | (S_IXUSR>>3) | (S_IXUSR>>6);
    }
    else if (!_wcsicmp (ext, L"CMD")) {
      *posixPermissions |= S_IXUSR | (S_IXUSR>>3) | (S_IXUSR>>6);
    }
{'fraction_non_alphanumeric': 0.19004084808403035, 'fraction_numerical': 0.01808986578486676, 'mean_word_length': 3.6116591928251123, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 98, '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': '4325555', 'n_tokens_mistral': 1931, 'n_tokens_neox': 1878, 'n_words': 482}