Xlate 40.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/* Copyright 1998 Acorn Computers Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
*  Lan Manager client
*
*  Xlate.C --  DOS to  RISCOS name & attrib mapping
*
*  Versions
*  07-03-94 INH Original
*
*/

#include <stdio.h>
#include <string.h>
Stewart Brodie's avatar
Stewart Brodie committed
27
#include <stdlib.h>
28
#include <ctype.h>
29 30
#include "swis.h"
#include "Interface/MimeMap.h"
31 32

#include "stdtypes.h"
Robert Sprowson's avatar
Robert Sprowson committed
33
#include "LanMan.h"
34
#include "Xlate.h"
35 36 37
#include "Attr.h"
#include "Omni.h"
#include "LMVars.h"
Stewart Brodie's avatar
Stewart Brodie committed
38
#include "SMB.h"
Ben Avison's avatar
Ben Avison committed
39
#include "Transact.h"
Stewart Brodie's avatar
Stewart Brodie committed
40 41 42 43
#include "NameCache.h"

#ifdef LONGNAMES
#define FileChar_TypedNamePrefix        ','
44
#define FileChar_UntypedNameSep         '-'
Stewart Brodie's avatar
Stewart Brodie committed
45 46 47 48 49 50
#define FileString_DeadFile             "xxx"
#define FileString_UntypedFile          "lxa"

/* Magic value used to indicate an incomplete file - used by the Filer,
 * for example, when writing a new file
 */
51
static const int deaddead = (int)0xDEADDEAD;
Robert Sprowson's avatar
Robert Sprowson committed
52
#endif
Stewart Brodie's avatar
Stewart Brodie committed
53

Robert Sprowson's avatar
Robert Sprowson committed
54
/* Macro returns non-zero if the specific load address is indicative of
Stewart Brodie's avatar
Stewart Brodie committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
 * a filetyped object.   Rather than checking for the top 12 bits being
 * set, it's much quicker to arithmetic shift it right 20 bits and test
 * for -1.  (Norcroft generates:  MVN rn, #0: TEQ rn, ra, ASR #20)
 */
#define IS_FILETYPED(load_addr) ((((signed int)(load_addr)) >> 20) == -1)

/* Extracts the filetype from a load address.  Assumes that IS_FILETYPED
 * would return non-zero.  In isolation, Norcroft generates:
 * MOV ra, ra, LSL #12: MOV ra, ra, LSR #20.
 */
#define GET_FILETYPE(load_addr) ((((unsigned int)(load_addr)) << 12) >> 20)

/* Encodes a filetype into a load address - Norcroft compiler makes a
 * nice job of this macro (result in load): ORR rn, load, #0xf0000000
 * ORR rn, rn, #0x0FF00000: MOV load,load,LSL #12:
 * EOR load, type, load LSR #20: EOR load,rn,load LSL #8
 */
Ben Avison's avatar
Ben Avison committed
72
#define ENCODE_FILETYPE(load,type) (((load)|0xFFF00000)^((GET_FILETYPE(load)^(type))<<8))
Stewart Brodie's avatar
Stewart Brodie committed
73 74


75 76 77 78

/* stricmp(): ignore-case string compare ------------------------ */
/* returns 0 if they match, > 0 if s1 > s2, < 0 if s1 < s2 */

Stewart Brodie's avatar
Stewart Brodie committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
/* Function to compare two strings case insensitively
 *
 * Originally: sbrodie
 *
 * Parameters: matches those of strcmp.
 * Result: matches the exit conditions of strcmp.
 *
 *
 * The conversions to unsigned int stop the compiler messing around with
 * shifts all over the place whilst trying to promote the chars to int
 * whilst retaining the sign.
 *
 * Problems: Choice of return value when strings do not match is based
 *           upon character number rather than any alphabetic sorting.
 *
 */
int stricmp(const char *first, const char *second)
96
{
Ben Avison's avatar
Ben Avison committed
97 98 99 100 101 102 103 104 105 106 107 108
  for (;;) {
    unsigned int a = *first++;
    unsigned int b = *second++;

    if (a == 0) return -b;
    if (a != b) {
      unsigned int c = (unsigned int) tolower(a);
      unsigned int d = (unsigned int) tolower(b);
      signed int result = c - d;
      if (result != 0) return result;
    }
  }
109 110 111 112 113 114 115 116 117 118 119
}

/* strcpyn(): copies a string with given max length. Note that
    unlike strncpy(), this will always correctly put terminating
    zeros on the end. len is the max number of characters including
    terminating zero to copy (= length of buffer where result is to
    be put).
*/

void strcpyn ( char *d, const char *s, int len )
{
Stewart Brodie's avatar
Stewart Brodie committed
120
#ifdef OLD_SLOW_METHOD
121 122 123
  while ( --len > 0 && *s != 0 )
    *d++ = *s++;
  *d = 0;
Stewart Brodie's avatar
Stewart Brodie committed
124 125 126 127
#else
  *d = 0;
  (void) strncat(d, s, len);
#endif
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
}

/* strcpyn_upper(): copies a string making all characters uppercase --- */

void strcpyn_upper ( char *d, const char *s, int len )
{
  while ( --len > 0 && *s != 0 )
   *d++ = toupper (*s++);

  *d=0;
}

/* strcpyn_lower(): doubtless you can guess --- */

void strcpyn_lower ( char *d, const char *s, int len )
{
  while ( --len > 0 && *s != 0 )
   *d++ = tolower (*s++);

  *d=0;
}

/* -------------------------- */

Ben Avison's avatar
Ben Avison committed
152
static const ushort daycount[13] =
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
{
  0,
  0,      /* Jan=31 */
  31,     /* Feb=28 */
  59,     /* Mar=31 */
  90,     /* Apr=30 */
  120,    /* May=31 */
  151,    /* Jun=30 */
  181,    /* Jul=31 */
  212,    /* Aug=31 */
  243,    /* Sep=30 */
  273,    /* Oct=31 */
  304,    /* Nov=30 */
  334     /* Dec=31 */
};

/* --------------------------- */

/* Directory entries, as returned from the 'search' command
   have the time & date returned in a packed-binary DD/MM/YY
   HH:MM:SS format. This routine converts it to 'Utime',
   which is the format used in other DOS calls.
*/

static uint DMYtoUtime ( int dtime, int ddate )
{
  uint x, dd, mm, yy, hrs, min, sec;

  dd = ddate & 31;
  mm = (ddate >> 5) & 15;
  yy = ((ddate >> 9) & 127); /* Years since 1980 */
  sec = (dtime & 31) << 1;
  min = (dtime >> 5) & 63;
  hrs = (dtime >> 11) & 31;

Stewart Brodie's avatar
Stewart Brodie committed
188 189 190 191 192 193
  /*
  debug2("%08x %08x => ", dtime, ddate);
  debug3("HH:MM:SS => %02d:%02d:%02d   ", hrs, min, sec);
  debug3("DD:MM:YY => %02d:%02d:%04d \n", dd, mm, yy + 1980);
  */

194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
  /* Calc. no. of days since 1-1-70 */
  x = 3652 + (yy*365) + ((yy+3)/4);
  x += daycount[mm] + (dd-1);
  if ( mm >= 3 && ((yy & 3)==0) ) /* March or later, in leap year */
    x++;

  return ((x*24+hrs)*60+min)*60 + sec;
}



/* ----------------------- */

/* Xlt_CnvDOStoRO translates DOS attributes into RISCOS attributes;
   it is used by a variety of read-info calls. Flags can be passed
   to specify whether the date/time or the 'flags' are to be converted.

   The file type filled in the 'load address' and 'exec address' is a
   default value; if this is important, Attr_GetInfo() should be
   called to fill this in accurately.

*/

void Xlt_CnvDOStoRO ( DOS_ATTRIBS *pDA, RISCOS_ATTRIBS *pRA, int flags )
{
  uint thi, tlo;

/* The "load address" attribute used by RISCOS is equal to
   0xFFFtttdd, where ttt is a 12-bit file type and dd is
   bits 32..39 of the time stamp. This is defined as the number
   of centiseconds since 01-Jan-1900.

   DOS deals (in the main) with time as 'Utime' - the number of seconds
   since 01-Jan-1970, which is to be RISCOS time 0x33 6E99 6A00.
   Hence the conversion is relatively simple.
     RISCOS time = 336E996A00h + 100*Utime
*/

  if ( flags & CNV_DATETIME )
  {
    thi = 0x336E99 + (pDA->utime >> 16) * 100;
    tlo = 0x6A00 + (pDA->utime & 0xFFFF) * 100;

    /* Total = (thi << 16)+tlo; */

    pRA->loadaddr = 0xFFF00000 + ( (thi+ (tlo >> 16) ) >> 16) +
Ben Avison's avatar
Ben Avison committed
240
                       (LM_Vars.default_type << 8); /* Default type */
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
    pRA->execaddr = (thi << 16) + tlo;
  }


  if ( flags & CNV_ATTRIBS )
  {
    if ( pDA->attr & ATTR_DIR )
    {
      pRA->flags = (pDA->attr & ATTR_RO) ? ROA_LOCKED :0;
    }
    else
    {
      pRA->flags = (pDA->attr & ATTR_RO) ? ROA_READ | ROA_LOCKED :
                       ROA_READ | ROA_WRITE;
    }
  }
}

/* --------------------------- */

/* Xlt_CnvROtoDOS converts RISCOS attributes to DOS attributes;
   this is usually prior to some set-attributes call.
*/

void Xlt_CnvROtoDOS ( RISCOS_ATTRIBS *pRA, DOS_ATTRIBS *pDA, int flags )
{
  uint x, res;
  /* Here, we convert RISCOS time to DOS Utime. Here,
     Utime = (RISCOStime - 0x336E996A00h) / 100 */

  if ( flags & CNV_DATETIME )
  {
Robert Sprowson's avatar
Robert Sprowson committed
273
    if ( !IS_FILETYPED(pRA->loadaddr) )
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
    {
      /* If this is not a time/date/type-stamped file... */
      pDA->utime = 0;
    }
    else
    {
      x = ((pRA->loadaddr & 0xFF) << 24) + (pRA->execaddr >> 8);
      /* Clip these values to DOS range */
      if ( x < 0x336E996A )
        x = 0;
      else
        x -= 0x336E996A;

      if ( x >= 100 * 0xFFFFFF ) x = 100*0xFFFFFF;
      res = x/100;
      x = ((x - res*100) << 8) + (pRA->execaddr & 0xFF);
      res = (res << 8) + (x / 100);
      pDA->utime = res;
    }
  }

  if ( flags & CNV_ATTRIBS )
  {
    if ( (pRA->flags & ROA_WRITE) == 0 &&
         (pRA->flags & ROA_LOCKED)  != 0
       )
      pDA->attr = ATTR_ARC | ATTR_RO;
    else
      pDA->attr = ATTR_ARC;
  }
}

/* --------------------------- */

/* Xlt_Jumble() and Xlt_Unjumble() are used to avoid keeping passwords
   lying round in plain text in memory; it's hardly invincible,
   but it'll stop people spotting passwords simply by dumping memory.

312
   All strings passed to Jumble & Unjumble should be PASSWORD_LIMIT bytes
313 314 315 316 317 318 319 320
   long.
*/

void Xlt_Jumble ( char *str )
{
  int i;
  uint key = (uint) str | 0x40000;

321
  for ( i=0; i < PASSWORD_LIMIT; i++ )
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
  {
    key <<= 1;
    if ( key & 0x80000 )
      key ^= 39;
    *str -= (key ^ 0x40);
    str++;
  }
}

/* ------------------- */

void Xlt_Unjumble ( char *str )
{
  int i;
  uint key = (uint) str | 0x40000;

338
  for ( i=0; i < PASSWORD_LIMIT; i++ )
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
  {
    key <<= 1;
    if ( key & 0x80000 )
      key ^= 39;
    *str += (key ^ 0x40);
    str++;
  }
}

/* Name conversion, etc =========================================== */

static char Xlt_DefaultDrv = 'A';

/* Wildcards, it seems, are not in fact used:
   A 'delete' operation reads directory entries
   first, and passes us individual filenames to
   delete. A 'rename' operation tries to do a
   'get file info' function on the wildcarded
   filename, then complains when it fails. ADFS
   doesn't allow wildcarded renames (although it
   does allow "move to new directory" renames), so
   we won't either. This means we can dispense with
   all wildcards in filenames.
*/

/* Character translate table ------------- */

#define CH_END  0
#define CH_ERR  1
#define CH_WILD 2
#define CH_PATH 3
#define CH_SEP  4
#define CH_DUD '_'

Ben Avison's avatar
Ben Avison committed
373
/*
374 375 376 377 378
  Illegal in DOS names:    SPACE / < > ? + , ; = [ ] : * \ " | DEL
  Illegal in RISCOS names: SPACE . $ ^ # &   % @     : * \ " | DEL
  Additionally:
    All control characters are bad
    All top bit set characters are swapped for _
379
*/
380
static const char xlt_RO2DOS[256] =
381 382 383 384 385 386
{
  CH_END, CH_ERR, CH_ERR, CH_ERR, CH_ERR, CH_ERR, CH_ERR, CH_ERR, /* 00-07 */
  CH_ERR, CH_ERR, CH_END, CH_ERR, CH_ERR, CH_END, CH_ERR, CH_ERR, /* 08-0F */
  CH_ERR, CH_ERR, CH_ERR, CH_ERR, CH_ERR, CH_ERR, CH_ERR, CH_ERR, /* 10-17 */
  CH_ERR, CH_ERR, CH_ERR, CH_ERR, CH_ERR, CH_ERR, CH_ERR, CH_ERR, /* 17-1F */

387
/* SPACE   !       "       #       $       %       &       '         20-27 */
388
  CH_ERR, '!',    CH_ERR, CH_WILD,CH_ERR, CH_ERR, CH_ERR, '\'',
389 390 391
/* (       )       *       +       ,       -       .       /         28-2F */
  '(',    ')',    CH_WILD,'&',    CH_ERR, '-',    CH_PATH,CH_SEP,
/* 0       1       2       3       4       5       6       7         30-37 */
392
  '0',    '1',    '2',    '3',    '4',    '5',    '6',    '7',
393 394 395
/* 8       9       :       ;       <       =       >       ?         38-3F */
  '8',    '9',    CH_ERR, '%',    '$',    '@',    '^',    '#',
/* @       A       B       C       D       E       F       G         40-47 */
396 397 398
  CH_ERR, 'A',    'B',    'C',    'D',    'E',    'F',    'G',
  'H',    'I',    'J',    'K',    'L',    'M',    'N',    'O',
  'P',    'Q',    'R',    'S',    'T',    'U',    'V',    'W',
399 400 401
/* X       Y       Z       [       \       ]       ^       _         58-5F */
  'X',    'Y',    'Z',    CH_ERR, CH_ERR, CH_ERR, CH_ERR, '_',
/* `       a       b       c       d       e       f       g         60-67 */
402 403 404
  '`',    'A',    'B',    'C',    'D',    'E',    'F',    'G',
  'H',    'I',    'J',    'K',    'L',    'M',    'N',    'O',
  'P',    'Q',    'R',    'S',    'T',    'U',    'V',    'W',
405
/* x       y       z       {       |       }       ~       DEL       78-7F */
Ben Avison's avatar
Ben Avison committed
406
  'X',    'Y',    'Z',    '{',    CH_ERR, '}',    '~',    CH_ERR,
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428

  CH_DUD, CH_DUD, CH_DUD, CH_DUD,  CH_DUD, CH_DUD, CH_DUD, CH_DUD,
  CH_DUD, CH_DUD, CH_DUD, CH_DUD,  CH_DUD, CH_DUD, CH_DUD, CH_DUD,
  CH_DUD, CH_DUD, CH_DUD, CH_DUD,  CH_DUD, CH_DUD, CH_DUD, CH_DUD,
  CH_DUD, CH_DUD, CH_DUD, CH_DUD,  CH_DUD, CH_DUD, CH_DUD, CH_DUD,

  CH_DUD, CH_DUD, CH_DUD, CH_DUD,  CH_DUD, CH_DUD, CH_DUD, CH_DUD,
  CH_DUD, CH_DUD, CH_DUD, CH_DUD,  CH_DUD, CH_DUD, CH_DUD, CH_DUD,
  CH_DUD, CH_DUD, CH_DUD, CH_DUD,  CH_DUD, CH_DUD, CH_DUD, CH_DUD,
  CH_DUD, CH_DUD, CH_DUD, CH_DUD,  CH_DUD, CH_DUD, CH_DUD, CH_DUD,

  CH_DUD, CH_DUD, CH_DUD, CH_DUD,  CH_DUD, CH_DUD, CH_DUD, CH_DUD,
  CH_DUD, CH_DUD, CH_DUD, CH_DUD,  CH_DUD, CH_DUD, CH_DUD, CH_DUD,
  CH_DUD, CH_DUD, CH_DUD, CH_DUD,  CH_DUD, CH_DUD, CH_DUD, CH_DUD,
  CH_DUD, CH_DUD, CH_DUD, CH_DUD,  CH_DUD, CH_DUD, CH_DUD, CH_DUD,

  CH_DUD, CH_DUD, CH_DUD, CH_DUD,  CH_DUD, CH_DUD, CH_DUD, CH_DUD,
  CH_DUD, CH_DUD, CH_DUD, CH_DUD,  CH_DUD, CH_DUD, CH_DUD, CH_DUD,
  CH_DUD, CH_DUD, CH_DUD, CH_DUD,  CH_DUD, CH_DUD, CH_DUD, CH_DUD,
  CH_DUD, CH_DUD, CH_DUD, CH_DUD,  CH_DUD, CH_DUD, CH_DUD, CH_DUD
};

429
static const char xlt_DOS2RO[256] =
Ben Avison's avatar
Ben Avison committed
430 431 432 433 434 435
{
  CH_END, CH_DUD, CH_DUD, CH_DUD, CH_DUD, CH_DUD, CH_DUD, CH_DUD, /* 00-07 */
  CH_DUD, CH_DUD, CH_END, CH_DUD, CH_DUD, CH_END, CH_DUD, CH_DUD, /* 08-1F */
  CH_DUD, CH_DUD, CH_DUD, CH_DUD, CH_DUD, CH_DUD, CH_DUD, CH_DUD, /* 10-17 */
  CH_DUD, CH_DUD, CH_DUD, CH_DUD, CH_DUD, CH_DUD, CH_DUD, CH_DUD, /* 17-1F */

436 437 438 439 440
/* SPACE   !       "       #       $       %       &       '         20-27 */
  CH_DUD, '!',    CH_DUD, '?',    '<',    ';',    '+',    '\'',
/* (       )       *       +       ,       -       .       /         28-2F */
  '(',    ')',    CH_DUD, CH_DUD, CH_DUD, '-',    CH_SEP, CH_DUD,
/* 0       1       2       3       4       5       6       7         30-37 */
Ben Avison's avatar
Ben Avison committed
441
  '0',    '1',    '2',    '3',    '4',    '5',    '6',    '7',
442 443 444
/* 8       9       :       ;       <       =       >       ?         38-3F */
  '8',    '9',    CH_DUD, CH_DUD, CH_DUD, CH_DUD, CH_DUD, CH_DUD,
/* @       A       B       C       D       E       F       G         40-47 */
Ben Avison's avatar
Ben Avison committed
445 446 447
  '=',    'A',    'B',    'C',    'D',    'E',    'F',    'G',
  'H',    'I',    'J',    'K',    'L',    'M',    'N',    'O',
  'P',    'Q',    'R',    'S',    'T',    'U',    'V',    'W',
448 449 450
/* X       Y       Z       [       \       ]       ^       _         58-5F */
  'X',    'Y',    'Z',    CH_DUD, CH_DUD, CH_DUD, '>',    '_',
/* `       a       b       c       d       e       f       g         60-67 */
Ben Avison's avatar
Ben Avison committed
451 452 453
  '`',    'a',    'b',    'c',    'd',    'e',    'f',    'g',
  'h',    'i',    'j',    'k',    'l',    'm',    'n',    'o',
  'p',    'q',    'r',    's',    't',    'u',    'v',    'w',
454
/* x       y       z       {       |       }       ~       DEL       78-7F */
Ben Avison's avatar
Ben Avison committed
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
  'x',    'y',    'z',    '{',    CH_DUD, '}',    '~',     CH_DUD,

  CH_DUD, CH_DUD, CH_DUD, CH_DUD,  CH_DUD, CH_DUD, CH_DUD, CH_DUD,
  CH_DUD, CH_DUD, CH_DUD, CH_DUD,  CH_DUD, CH_DUD, CH_DUD, CH_DUD,
  CH_DUD, CH_DUD, CH_DUD, CH_DUD,  CH_DUD, CH_DUD, CH_DUD, CH_DUD,
  CH_DUD, CH_DUD, CH_DUD, CH_DUD,  CH_DUD, CH_DUD, CH_DUD, CH_DUD,

  CH_DUD, CH_DUD, CH_DUD, CH_DUD,  CH_DUD, CH_DUD, CH_DUD, CH_DUD,
  CH_DUD, CH_DUD, CH_DUD, CH_DUD,  CH_DUD, CH_DUD, CH_DUD, CH_DUD,
  CH_DUD, CH_DUD, CH_DUD, CH_DUD,  CH_DUD, CH_DUD, CH_DUD, CH_DUD,
  CH_DUD, CH_DUD, CH_DUD, CH_DUD,  CH_DUD, CH_DUD, CH_DUD, CH_DUD,

  CH_DUD, CH_DUD, CH_DUD, CH_DUD,  CH_DUD, CH_DUD, CH_DUD, CH_DUD,
  CH_DUD, CH_DUD, CH_DUD, CH_DUD,  CH_DUD, CH_DUD, CH_DUD, CH_DUD,
  CH_DUD, CH_DUD, CH_DUD, CH_DUD,  CH_DUD, CH_DUD, CH_DUD, CH_DUD,
  CH_DUD, CH_DUD, CH_DUD, CH_DUD,  CH_DUD, CH_DUD, CH_DUD, CH_DUD,

  CH_DUD, CH_DUD, CH_DUD, CH_DUD,  CH_DUD, CH_DUD, CH_DUD, CH_DUD,
  CH_DUD, CH_DUD, CH_DUD, CH_DUD,  CH_DUD, CH_DUD, CH_DUD, CH_DUD,
  CH_DUD, CH_DUD, CH_DUD, CH_DUD,  CH_DUD, CH_DUD, CH_DUD, CH_DUD,
  CH_DUD, CH_DUD, CH_DUD, CH_DUD,  CH_DUD, CH_DUD, CH_DUD, CH_DUD
};
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553

static err_t nameROtoDOS ( char *dst, char *src )
{
  int ch;
  int o_name= 0;
  int o_ext = 0;

  while (1)  /* Process as many chars as we can */
  {
    ch = xlt_RO2DOS[(*src++) & 0xFF];

    /* o_name counts the number of characters in the
       'name' part which we have output, in the range 0..8;
       it is set to 9 when we are outputting the 'ext' part.
       o_ext counts the number of characters in the 'ext'
       part which we have output.
    */

    switch( ch )
    {
      case CH_END:      /* End of string */
        *dst = 0;
        return OK;

      case CH_ERR:      /* No-good chars */
        return EBADNAME;

      case CH_WILD:
        return ENOWILDCARD;

      case CH_PATH:      /* RISCOS pathname separator */
        *dst++ = '\\';
        o_name = 0;
        o_ext = 0;
        continue;

      case CH_SEP:      /* Separator for name/ext */
        if ( o_name >= 1 && o_name <= 8 )
        {
          *dst++ = '.';
          o_name = 9;   /* Stop any more */
        }
        continue;

      default:
        if ( o_name == 8 ) /* Time for a separator? */
        {
          *dst++ = '.';
          *dst++ = '~';
          *dst++ = ch;
          o_name = 9;
          o_ext  = 2;
        }
        else if ( o_name < 8 )
        {
          *dst++ = ch;
          o_name++;
        }
        else if ( o_ext < 3 )
        {
          *dst++ = ch;
          o_ext++;
        }

        continue;
    }
  }

}



/* -------------------------- */

void Xlt_NameDOStoRO ( char *dst, char *src )
{
  int i, c;
Ben Avison's avatar
Ben Avison committed
554
  int lcl_name_mode = LM_Vars.namemode & (NM_LOWERCASE | NM_FIRSTCAPS | NM_PRESERVED);
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594

  for ( i=0; i<12; i++ )      /* Up to 12 chars in 8.3 name */
  {
    c = xlt_DOS2RO[src[i] & 0xFF];

    if ( c == CH_END )
      break;

    if ( c == CH_SEP ) /* Name/ext separator */
    {
      if ( i == 8 && src[9] == '~' ) /* Skip ".~" */
        i=9;
      else
        *dst++ = '/';
    }
    else switch (lcl_name_mode)
    {
      case NM_LOWERCASE:
        *dst++ = tolower(c);
        break;

      case NM_FIRSTCAPS:
        if ( isalpha(c) )
        {
          *dst++ = toupper(c);
          lcl_name_mode = NM_LOWERCASE;
          break;
        }
        /* else drop through into */
      case NM_PRESERVED:
      default:
        *dst++ = c;
        break;

    }
  }

  *dst = 0;
}

Stewart Brodie's avatar
Stewart Brodie committed
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
#ifdef LONGNAMES

/* OK, the mappings for long filename discs are different.
 *
 * They are:
 *
 *       DOS     ->     RISC OS         ->       DOS
 *
 *        *                *                      *    (wildcard)
 *        ?                #                      ?    (wildcard)
 *        #                ?                      #    (swap match for above)
 *
 *        :                :                      :    (won't be seen)
 *        \                .                      \    (dir sep)
 *        .                /                      .
 *
 *        &                +                    + or &
 *        +                +                    & or +
 *        @                =                    @ or =
 *        =                =                    = or @
 *        %                >                    % or >
 *        >                >                    > or %
 *        $                <                    $ or <
 *        <                <                    < or $
 *        ^                ,                    ^ or ,  (don't like this map)
 *        ,                , (or extn)          , or ^
 *      space          hard space               space or hard space
 *    hard space       hard space               hard space or space
 *
 */
static const char lanmanfs_lookup_table[257]=
     "________________________________"
     "\xa0!\"?<>+'()*+,-/_0123456789:;<=>#"
     "=ABCDEFGHIJKLMNOPQRSTUVWXYZ[.],_"
     "`abcdefghijklmnopqrstuvwxyz{|}~_"
     ""
     "\xa0、ウЖ渦慨偽係杭纂従"
     "請唾津毒班碧麺力佰厶壞嶐慵无槿渤"
     "珀矣粤肄蓍裨跋鈿韵鴦";

static const char lanmanfs_inverse_lookup_table[257]=
     "________________________________"
Stewart Brodie's avatar
Stewart Brodie committed
637
     "\x20!\"?$%+'()*+,-\\.0123456789:;<=>#"
Stewart Brodie's avatar
Stewart Brodie committed
638 639 640
     "=ABCDEFGHIJKLMNOPQRSTUVWXYZ[.],_"
     "`abcdefghijklmnopqrstuvwxyz{|}~_"
     ""
Stewart Brodie's avatar
Stewart Brodie committed
641
     "\x20、ウЖ渦慨偽係杭纂従"
Stewart Brodie's avatar
Stewart Brodie committed
642 643 644 645 646
     "請唾津毒班碧麺力佰厶壞嶐慵无槿渤"
     "珀矣粤肄蓍裨跋鈿韵鴦";


/* These two structures MUST be kept in step in order to allow the
647
 * wildcard resolution code to function correctly.
Stewart Brodie's avatar
Stewart Brodie committed
648 649
 */
static const char lanmanfs_contentious_characters[]=
Stewart Brodie's avatar
Stewart Brodie committed
650
     "+=><,\x20";
Stewart Brodie's avatar
Stewart Brodie committed
651
static const char lanmanfs_contentious_pairing[]=
Stewart Brodie's avatar
Stewart Brodie committed
652
     "&@%$^\xa0";
Stewart Brodie's avatar
Stewart Brodie committed
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682

/* Translate DOS names to RISC OS names using the translation table
 *
 * This routine translates the given DOS filename into the RISC OS filename,
 * stripping the file extension if there is one and setting up the pRA
 * structure with the type details of the file.
 */
static void Xlt_NameDOStoROX2 ( char *dst, char *src, RISCOS_ATTRIBS *pRA )
{
  int i;
  char *odst = dst;

  for ( i=0; i<(DOS_NAME_LEN-1); i++ )      /* Length limit is 255 chars */
  {
    int c = src[i] & 0xFF;
    if (c == 0) break; /* Found the terminator */
    *dst++ = lanmanfs_lookup_table[c];
  }

  *dst = 0;
  dst = strrchr(odst, '.');
  if (dst == NULL) dst = odst; else ++dst;
  if (dst != NULL) {
    if (Xlt_SplitLeafnameX2 ( dst, pRA, &dst ) == OK) {
      /* Strip off the extension */
      *dst = 0;
    }
  }
}

683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
static bool Xlt_ValidLoadExec ( const char *suffix )
{
  bool sep = false;
  int  lhs = 0, rhs = 0;

  /* Quick exit if no separator */
  if (strchr ( suffix, FileChar_UntypedNameSep ) == NULL)
    return false;

  /* Rules: each half must be comprised only of hex digits
   *        min 1 digit, max 8 digits
   *        no whitespace
   *        must be one separator in between
   * Returns true if rules are met.
   */
  while (*suffix)
  {
    if (isxdigit ( *suffix ))
    {
      if (sep) rhs++; else lhs++;
    }
    else
    {
      if (*suffix == FileChar_UntypedNameSep)
      {
        if (sep) return false; /* Already seen a separator */
        if (lhs == 0) return false; /* Not seen any hex digits */
        sep = true;
      }
      else
      {
        return false; /* Neither hex digit nor separator */
      }
    }
    suffix++;
  }
  return (bool)(lhs > 0 && lhs <= 8 && rhs > 0 && rhs <= 8);
}

Stewart Brodie's avatar
Stewart Brodie committed
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
/* This function copies the string src to dst.  Once it gets past the
 * level'th character, it maps each character through the RISC OS->DOS
 * character conversion table.
 *
 * This means that partially converted filenames can be copied verbatim
 * and the not-yet-done portions translated as required.
 */
static void Xlt_CopyViaInverseTable( char *dst, const char *src, int level )
{
  int i;

  for ( i=0; i<(DOS_NAME_LEN-1); i++ )
  {
    int c = src[i] & 0xFF;
    if (c == 0) break;
737 738
    if (level != 0) { /* Whole path has already been through  */
      dst[i] = c;     /* inverse lookup table when level != 0 */
Stewart Brodie's avatar
Stewart Brodie committed
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
    }
    else {
      dst[i] = lanmanfs_inverse_lookup_table[c];
    }
  }
  dst[i] = 0;
}

/* Temporary state data structure for the filename mangling */
typedef struct {
        char *dstcpy;
        char dstcpybuf[DOS_NAME_LEN + 4];
        char matchbuf[DOS_NAME_LEN + 4];
} Xlt_NXCX2_Data;


/* Xlt_ContentiousCharCheck
 *
 * Checks contentious characters. Returns the actual remote char if
 * the chars are not equal but match under the contentious char table.
 * Returns '\0' if no match is found.
 */
static char Xlt_ContentiousCharCheck(char e, char d)
{
   int i;

   for (i=0; lanmanfs_contentious_characters[i]; ++i) {
     if ((e == lanmanfs_contentious_characters[i] ||
          e == lanmanfs_contentious_pairing[i]) &&
         (d == lanmanfs_contentious_characters[i] ||
          d == lanmanfs_contentious_pairing[i])) {
            return e;
          }
   }

   return '\0';
}

/* An unusual routine.  The SMB_ReadDirectoryEntriesX2 routine calls this
 * function back in order to process each directory entry as it is
 * discovered whilst we are searching for filename matches.  In order to
 * get that routine to stop when we have found a match, we return it a
 * value of EOUTOFMEM.  If we want it to continue, we return OK.  The
 * private handle (_dst) is actually a pointer to the state structure
 * and that structure is updated with the real filename once a match
 * has been found.  The format parameter should always be 1.
 */
Ben Avison's avatar
Ben Avison committed
786
static err_t Xlt_NameXlateCallbackX2 ( BYTE *entry, int format, bool *taken, void *_dst )
Stewart Brodie's avatar
Stewart Brodie committed
787 788 789 790 791 792
{
    Xlt_NXCX2_Data *dst = _dst;
    char *eptr = (char *) (entry + 23);
    char *dptr = 1 + strrchr(dst->matchbuf, '\\');
    err_t res = OK;

793
    (void) format; /* Unused */
Ben Avison's avatar
Ben Avison committed
794
    *taken = true;
795

Stewart Brodie's avatar
Stewart Brodie committed
796 797 798 799 800 801 802 803 804 805 806 807 808 809
    if (dst->dstcpy[0] != '*') {
      return EOUTOFMEM;
    }

    for (;;) {
      char e = *eptr++;
      char d = *dptr++;

      if (e == d || toupper(e) == toupper(d)) {
        if (e) continue;
        /* We have a match */
        res = EOUTOFMEM;
        break;
      }
810 811
#ifdef LONGNAMES
      else if (e == FileChar_TypedNamePrefix && d == 0) {
Stewart Brodie's avatar
Stewart Brodie committed
812 813 814
        /* Might have been a filetype suffix */
        int type, num;
        if ((sscanf(eptr, "%x%n", &type, &num) == 1 && num == 3)
815
          || Xlt_ValidLoadExec(eptr)
Stewart Brodie's avatar
Stewart Brodie committed
816 817 818 819 820 821 822
          || strcmp(eptr, FileString_DeadFile) == 0
          || strcmp(eptr, FileString_UntypedFile) == 0) {
          /* It was */
          res = EOUTOFMEM;
          break;
        }
      }
823
#endif
Stewart Brodie's avatar
Stewart Brodie committed
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
      else {
        if (!Xlt_ContentiousCharCheck(e, d)) {
          /* No match */
          return OK;
        }
      }
    }

    if (res == EOUTOFMEM) {
      /* Update stored leafname */
      if (dst->dstcpy[0] == '*') {
         dst->dstcpy[0] = '\0';
         NameCache_Add(dst->dstcpybuf, entry);
      }
      strcpy(dst->dstcpy, (char *) entry+23);
    }
    return res;
}

/* Map RISC OS names onto DOS names.  Complicated by the need to
 * resolve the duplicate mapped characters.  All lookups invoke a
 * directory search at the remote end, looking for the actual object
 * name.  If any of the path components have nasty characters in them,
 * then sub-searches are performed to resolve those too.  Only a single
 * level of recursion is required (and supported).
 */
static err_t Xlt_NameROtoDOSX2_sub ( char *dst, char *src, int level )
{
  static Xlt_NXCX2_Data private;
Ben Avison's avatar
Ben Avison committed
853
  static Transact_SearchContext con;
Stewart Brodie's avatar
Stewart Brodie committed
854
  char *inptr;
855
  int   i;
Stewart Brodie's avatar
Stewart Brodie committed
856 857 858 859 860 861 862 863 864 865 866 867 868 869
  err_t status;

  private.matchbuf[0] = dst[0];
  private.matchbuf[1] = dst[1];
  Xlt_CopyViaInverseTable(private.matchbuf + 2, src, level);
  debug0("\n\n");
  debug1("Xlt_NameROtoDOSX2: `%s'\n", src);
  debug1("Xlt_CopyViaInverseTable -> `%s'\n", private.matchbuf);

  /* Construct the search pathname buffer, by taking the parent directory
   * and ensuring that the name ends \*
   * private.dstcpy must point at the * character so that the callback
   * function can write the matched target name straight in
   */
Ben Avison's avatar
Ben Avison committed
870
#if 0 /* No side effects */
Stewart Brodie's avatar
Stewart Brodie committed
871
  (void) NameCache_Locate(private.matchbuf);
Ben Avison's avatar
Ben Avison committed
872
#endif
Stewart Brodie's avatar
Stewart Brodie committed
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
  strcpy(private.dstcpybuf, private.matchbuf);
  private.dstcpy = strrchr(private.dstcpybuf, '\\');
  if (private.dstcpy) {
    strcpy(++private.dstcpy, "*");
  }
  else {
    private.dstcpy = strchr(private.dstcpybuf, '\0');
    strcpy(private.dstcpy, "\\*");
  }

  debug1("Xlt_NameROtoDOSX2 initiates a dir search of `%s'\n",
    private.dstcpybuf);

  if (level == 0)
  for (inptr = private.dstcpybuf + 2 + level; inptr != private.dstcpy; ++inptr) {
    if (Xlt_ContentiousCharCheck(*inptr, *inptr)) {
      /* We have a problem - there are contentious characters in the
       * path leading to the actual object we are going to seek!
       * We now have to mess about doing sub-searching for the required
       * contentious directory names, remembering to patch back the
       * private.matchbuf with the matched name so that if any search
       * component fails (eg. the top-level one when the object does
       * not exist (consider *Cdir or *Create)) does remember the path
       * did exist.
       */
      static Xlt_NXCX2_Data sub_search;
      static char srccpy[DOS_NAME_LEN];
      char *ptr, preserved, *okptr;
      size_t len;

      /* Preserve existing state whilst sub-search is performed */
      sub_search = private;

      /* Duplicate source string and truncate it appropriately for
       * the sub-search target
       */
      strcpy(srccpy, private.matchbuf + 2);
      okptr = srccpy + (inptr - (private.dstcpybuf + 2));
      while (okptr != srccpy && *okptr != '\\') --okptr;
      ptr = srccpy + (inptr - (private.dstcpybuf + 2));
      while (*ptr != '\0' && *ptr != '\\') ++ptr;
      preserved = ptr[0];
      ptr[0] = '\0';
      /* Search for the name.  Don't worry about failures, dst will be
       * safe to use whatever the result
       */
      Xlt_NameROtoDOSX2_sub(dst, srccpy, okptr - srccpy + 1);
      len = strlen(dst);
      ptr[0] = preserved;
      /* Restore state, update matchbuf (answer on fail), and dstcpybuf
       * (current copy buffer).
       */
      private = sub_search;
      memcpy(private.dstcpybuf, dst, len);
      memcpy(private.matchbuf, dst, len);
      /* Skip to next component - we've already tried to resolve this
       * one and, with success or failure, we already have the best we
       * can do for this component
       */
      inptr = private.dstcpybuf + len;
    }
  }
935

Stewart Brodie's avatar
Stewart Brodie committed
936 937 938 939 940 941
  debug1("Xlt_NameROtoDOSX2 initiates a dir search of `%s' (post-mangle)\n",
    private.dstcpybuf);

  /* Repeatedly search the directory until we find a match, or there are
   * no more entries to be read back
   */
Ben Avison's avatar
Ben Avison committed
942
  con.resume_key = 0;
Stewart Brodie's avatar
Stewart Brodie committed
943 944
  for (status = OK, inptr = private.dstcpybuf; status == OK; inptr = NULL) {
    status = SMB_ReadDirEntries(inptr, 32, Xlt_NameXlateCallbackX2,
Ben Avison's avatar
Ben Avison committed
945
                                &private, &con);
Stewart Brodie's avatar
Stewart Brodie committed
946 947 948 949
    switch (status) {
      case OK:
        /* More entries to read, and no match found yet */
        break;
Ben Avison's avatar
Ben Avison committed
950

Stewart Brodie's avatar
Stewart Brodie committed
951 952 953
      case EOUTOFMEM:
        /* Found it */
        strcpy(dst, private.dstcpybuf);
Ben Avison's avatar
Ben Avison committed
954
        debug1("**RIGHT.  Got a match: `%s'\n", dst);
Stewart Brodie's avatar
Stewart Brodie committed
955
        break;
Ben Avison's avatar
Ben Avison committed
956

Stewart Brodie's avatar
Stewart Brodie committed
957 958 959 960 961
      default:
        /* Definitely didn't find it - revert to original filename
         * or at least filename with nasties resolved as far as possible
         */
        strcpy(dst, private.matchbuf);
Ben Avison's avatar
Ben Avison committed
962
        debug1("**WRONG.  Not got a match. Reverting to `%s'\n", dst);
Stewart Brodie's avatar
Stewart Brodie committed
963 964 965 966
        break;
    }
  }

967 968 969 970 971 972 973 974 975
  /* Go back and respect the case given regardless of the case on the
   * server so that "*RENAME Test TEST" does something
   */
  i = 0;
  while (src[i]) {
    if (isalpha(src[i])) dst[i + 2] = src[i];
    i++;
  }

Stewart Brodie's avatar
Stewart Brodie committed
976 977 978
  return OK;
}

979
/* Kicks off the RISC OS to DOS name conversion process - setting up the
Stewart Brodie's avatar
Stewart Brodie committed
980 981 982 983
 * number of already-translated characters as zero.
 */
static err_t Xlt_NameROtoDOSX2 ( char *dst, char *src )
{
984
  if (strchr(src, '*') != NULL) return ENOWILDCARD; /* As per nameROtoDOS() */
Ben Avison's avatar
Ben Avison committed
985
  return Xlt_NameROtoDOSX2_sub(dst, src, 0);
Stewart Brodie's avatar
Stewart Brodie committed
986 987 988 989
}

#endif

990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
/* --------------------------- */


err_t Xlt_SetDefaultDrv ( char *dospath )
{
  Xlt_DefaultDrv = dospath[0];
  return OK;
}

/* --------------------------- */

static char mount_name[20];

err_t Xlt_ConvertPath ( char *name_in, char *name_out )
{
  int i;
  char drvc = Xlt_DefaultDrv;

  if ( name_in[0] == ':' )  /* Mount name is given */
  {
    name_in++;
    for (i=0; i<19; i++)
    {
      mount_name[i] = name_in[i];
      if ( name_in[i] < ' ' )  /* Premature end of name */
        return EBADNAME;

      if ( name_in[i] == '.' )
        break;
    }

    mount_name[i] = 0;
    drvc = Omni_GetDrvLetter(mount_name);
    if ( drvc == 0 )
      return EBADDRV;

    name_in += (i+1); /* Skip '.' */
  }

  if ( name_in[0] != '$' )
    return EBADNAME;

  name_out[0] = drvc;
  name_out[1] = ':';

  if ( name_in[1] < ' ' ) /* Just '$' as pathname */
  {
    strcpy(name_out+2, "\\");
    return OK;
  }
  else if ( name_in[1] == '.' )
  {
Stewart Brodie's avatar
Stewart Brodie committed
1042 1043 1044 1045 1046 1047 1048 1049
#ifdef LONGNAMES
    /* We use a different system for long filename shares.  Call
     * the long name resolution routine if necessary
     */
    if (SMB_IsLongNameFS( name_out )) {
      return ( Xlt_NameROtoDOSX2 ( name_out, name_in+1 ) );
    }
#endif
1050 1051 1052 1053 1054 1055 1056 1057 1058
    return ( nameROtoDOS ( name_out+2, name_in+1 ) );
  }

  name_out[0] = 0;
  return EBADNAME;
}

/* --------------------------- */

1059
/* Gets a leaf name from a RISC OS name */
1060 1061 1062 1063 1064

char *Xlt_GetRISCOSLeafName ( char *name_in )
{
  char *tmp;
  tmp = strrchr ( name_in, '.' );
1065
  return ( tmp == NULL ) ? name_in : tmp + 1;
1066 1067 1068 1069 1070 1071 1072 1073 1074
}

/* Directory entry conversion ================================== */

err_t Xlt_ExpandSearchEntry ( BYTE *entry, char *path_base,
            char *name_out,
            DOS_ATTRIBS *da_out,
            RISCOS_ATTRIBS *ra_out )
{
Stewart Brodie's avatar
Stewart Brodie committed
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
  /* Old SMBsearch format.  For reference, entry points to the following
   * structure (treat strictly as byte array with no padding except where
   * stated:
   *  BYTE find_buf_attr;
   *  WORD find_buf_time;
   *  WORD find_buf_date;
   *  WORD find_buf_size_l;
   *  WORD find_buf_size_h;
   *  BYTE find_buf_pname[13];  ASCII - NUL terminated
   * =====
   *   22  bytes.
   */
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
  DOS_ATTRIBS da;

  if ( entry == NULL )
    return EBADPARAM;

  if ( name_out != NULL )
  {
    Xlt_NameDOStoRO ( name_out, (char *)entry+9 );
  }

  if ( da_out != NULL || ra_out != NULL )
  {
    da.attr   = entry[0];
    da.utime  = DMYtoUtime ( entry[1] + (entry[2] << 8),
                          entry[3] + (entry[4] << 8) );
    da.length = entry[5] + (entry[6]<<8) +
                       (entry[7]<<16) + (entry[8]<<24);

    if ( da_out != NULL )
      *da_out = da;

    if ( ra_out != NULL )
    {
      Xlt_CnvDOStoRO ( &da, ra_out, CNV_DATETIME+CNV_ATTRIBS );
      Attr_GetInfo ( path_base, (char *)entry+9, ra_out );
    }
  }

  return OK;
}

Stewart Brodie's avatar
Stewart Brodie committed
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
#ifdef LONGNAMES
err_t Xlt_ExpandSearchEntryX2 ( BYTE *entry, char *path_base,
            char *name_out,
            DOS_ATTRIBS *da_out,
            RISCOS_ATTRIBS *ra_out )
{
  /* New TRANSACT2/FINDFIRST format.  For reference, entry points to the
   * following structure (treat strictly as byte array with no padding
   * except where stated:   (SMB_DATE and SMB_TIME are actually WORD)
   *
   * WORD   CreationDate
   * WORD   CreationTime
   * WORD   LastAccessDate
   * WORD   LastAccessTime
   * WORD   LastWriteDate
   * WORD   LastWriteTime
   * DWORD  DataSize
   * DWORD  AllocationSize
   * WORD   Attributes
   * BYTE   FilenameLength
   * STRING FileName
   * =====
   */
  DOS_ATTRIBS da;
  RISCOS_ATTRIBS ra_name;

1144 1145
  (void) path_base; /* Unused */

Stewart Brodie's avatar
Stewart Brodie committed
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
  if ( entry == NULL )
    return EBADPARAM;

  if ( name_out != NULL )
  {
    Xlt_NameDOStoROX2 ( name_out, (char *)entry+23, &ra_name );
  }
  else
  {
    char *dst;
    if (Xlt_SplitLeafnameX2 ( (char *)entry+23, &ra_name, &dst ) == OK) {
      *dst = 0;
    }
  }

  if ( da_out != NULL || ra_out != NULL )
  {
    da.attr   = entry[20];
    da.utime  = DMYtoUtime ( entry[10] + (entry[11] << 8),
                          entry[8] + (entry[9] << 8) );
    da.length = entry[12] + (entry[13]<<8) +
                       (entry[14]<<16) + (entry[15]<<24);

    if ( da_out != NULL )
      *da_out = da;

    if ( ra_out != NULL )
    {
1174
      /* Always populate the access attributes of ra_out */
Stewart Brodie's avatar
Stewart Brodie committed
1175
      Xlt_CnvDOStoRO ( &da, ra_out, CNV_DATETIME+CNV_ATTRIBS );
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
      if (IS_FILETYPED(ra_name.loadaddr))
      {
        /* Mix the filetype in with the date and time from Xlt_CnvDOStoRO */
        ra_out->loadaddr = (ra_out->loadaddr & 0xFFF000FF) | (ra_name.loadaddr & 0x000FFF00);
      }
      else
      {
        /* Unstamped, so force back to those discovered by Xlt_SplitLeafnameX2 */
        ra_out->loadaddr = ra_name.loadaddr;
        ra_out->execaddr = ra_name.execaddr;
      }
Stewart Brodie's avatar
Stewart Brodie committed
1187 1188 1189 1190 1191
    }
  }

  return OK;
}
1192

Stewart Brodie's avatar
Stewart Brodie committed
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
/* Xlt_SplitLeafname2
 *
 * This routine examines the supplied leafname/pathname and looks to
 * see if it has a type suffix.  If it does, then the type suffix is
 * decoded and the RISCOS_ATTRIBS structure is updated to hold the
 * load/exec address data, a pointer to the type suffix separator (the
 * comma) is stored in *terminator and the function returns OK.  The
 * caller can strip the suffix by zeroing the byte pointed to by
 * *terminator.  If there was no type suffix, ENOTPRESENT is returned
 * and NULL is stored in *terminator.
 *
 */
err_t Xlt_SplitLeafnameX2 ( char *leafname, RISCOS_ATTRIBS *pRA,
  char **terminator)
{
  int type, len;
1209
  char *term, *pre;
Stewart Brodie's avatar
Stewart Brodie committed
1210
  err_t res = ENOTPRESENT;
1211
  _kernel_swi_regs rset;
Stewart Brodie's avatar
Stewart Brodie committed
1212 1213

  *terminator = NULL;
1214 1215
  if (leafname == NULL)
    {
Stewart Brodie's avatar
Stewart Brodie committed
1216
    /* Oh dear - don't understand this - just claim it's text */
1217
    pRA->loadaddr = ENCODE_FILETYPE(pRA->loadaddr, 0xFFF);
Stewart Brodie's avatar
Stewart Brodie committed
1218
    return res;
1219 1220 1221 1222
    }

  debug1("Xlt_SplitLeafnameX2('%s',...)\n", leafname);

Stewart Brodie's avatar
Stewart Brodie committed
1223
  term = strchr(leafname, '\0');
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
  pre = strrchr(leafname, FileChar_TypedNamePrefix);
  if (pre && (term - leafname) > 4)
  {
    if ((term - pre) == 1+3) /* Try most common form first */
    {
      /* At least 5 letters long and of the form ",ttt" */
      if (stricmp(pre+1, FileString_UntypedFile) == 0)
      {
        debug0("File is untyped (,lxa)\n");
        pRA->loadaddr = pRA->execaddr = LM_Vars.untyped_address;
        *terminator = pre;
        return OK;
      }
      if (stricmp(pre+1, FileString_DeadFile) == 0)
      {
        debug0("File is DEADDEAD\n");
        pRA->loadaddr = pRA->execaddr = deaddead;
        *terminator = pre;
        return OK;
      }
      if (sscanf(pre+1, "%x%n", &type, &len) == 1 && len == 3)
      {
        /* note.  sscanf returns the number of conversions which were
         * successfully performed.  the %n conversion never fails and
         * does not count towards the total number of conversions, but
         * holds the number of characters consumed from the source
         * string.  Therefore, provided that len was 3, then we have
         * consumed three hex digits.
         */
        pRA->loadaddr = ENCODE_FILETYPE(pRA->loadaddr, type);
        *terminator = pre;
        debug3("Filetype is %#03x; load/exec=%#08x %#08x\n", type,
                pRA->loadaddr, pRA->execaddr);
        return OK;
      }
    }
    if ((term - pre) >= 1+3 && Xlt_ValidLoadExec(pre + 1)) /* Less likely */
    {
      uint load, exec;
      /* At least long enough for ",l-x" and not more than ",llllllll-xxxxxxxx" */
      if (sscanf(pre+1, "%x-%x", &load, &exec) == 2)
      {
        debug2("File is untyped load/exec=%#08x %#08x\n", load, exec);
        pRA->loadaddr = load; pRA->execaddr = exec;
        *terminator = pre;
        return OK;
      }
    }
  }
1273

1274
  /* No ",ttt" or ",llll-xxxx" was found, try the mimemap for ".ext" */
1275 1276
  if (res != OK)
     {
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
     /* This function can get called with "A:\dosname.txt" paths or "riscosname/txt" leafs */
     if (leafname[1] == ':')
        {
        term = strrchr(leafname, '.');  /* strrchr catches names like "file.tar.gz" */
        }
     else
        {
        term = strrchr(leafname, '/');  /* strrchr catches names like "file/tar/gz" */
        }

1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
     if (term != NULL)
        {
        /* A dot was found so try to lookup the dos style extension */
        term++;  /* Skip '.' */
        rset.r[0] = MMM_TYPE_DOT_EXTN;
        rset.r[1] = (int)term;
        rset.r[2] = MMM_TYPE_RISCOS;
        if (_kernel_swi(MimeMap_Translate, &rset, &rset) == NULL)
           {
           pRA->loadaddr = ENCODE_FILETYPE(pRA->loadaddr, rset.r[3]);
           debug2("Mimemap gave type %X for '%s'\n", rset.r[3], leafname );
           *terminator = strchr(leafname, '\0'); /* Safe to point to that null on exit */
           res = OK;
           }
        }
     }

  if (res != OK)
     {
Ben Avison's avatar
Ben Avison committed
1306 1307
     /* No ",ttt" and no mimemap lookup - mark as default type */
     pRA->loadaddr = ENCODE_FILETYPE(pRA->loadaddr, LM_Vars.default_type);
1308 1309 1310
     }

return res;
Stewart Brodie's avatar
Stewart Brodie committed
1311 1312 1313 1314 1315 1316
}
#endif

/* ------------------------------- */

#ifdef LONGNAMES
1317 1318
/* Trims the optional ",ttt" extension from 'name'
 * and returns the updated string
Stewart Brodie's avatar
Stewart Brodie committed
1319
 */
1320
err_t Xlt_RemoveROType ( char *name )
Stewart Brodie's avatar
Stewart Brodie committed
1321 1322
{
  RISCOS_ATTRIBS RA;
1323
  char          *nterm;
Ben Avison's avatar
Ben Avison committed
1324

1325 1326
  if (Xlt_SplitLeafnameX2( name, &RA, &nterm ) != OK) {
    /* No type information - nothing to do */
Stewart Brodie's avatar
Stewart Brodie committed
1327 1328
  }
  else {
1329
    /* Poke a terminator to get just the base name */
Stewart Brodie's avatar
Stewart Brodie committed
1330 1331
    *nterm = '\0';
  }
Ben Avison's avatar
Ben Avison committed
1332

Stewart Brodie's avatar
Stewart Brodie committed
1333 1334 1335 1336 1337 1338 1339 1340
  return OK;
}
#endif

/* ------------------------------- */

#ifdef LONGNAMES
/* Adds the type suffix for a RISC OS filename.  The type is extracted
Ben Avison's avatar
Ben Avison committed
1341
 * from the passed load address, unless DOS name is sufficient
Stewart Brodie's avatar
Stewart Brodie committed
1342
 */
1343
int Xlt_AddROType ( char *leafname, uint loadaddr, uint execaddr )
Stewart Brodie's avatar
Stewart Brodie committed
1344 1345
{
   RISCOS_ATTRIBS RA;
1346
   char *nterm, *term;
1347
   char typebuf[20], ftypebuf[8];
1348
   int ftype;
Stewart Brodie's avatar
Stewart Brodie committed
1349 1350 1351 1352 1353 1354

   typebuf[0] = FileChar_TypedNamePrefix;
   if (loadaddr == deaddead) {
     strcpy(typebuf+1, FileString_DeadFile);
   }
   else if (!IS_FILETYPED(loadaddr)) {
1355
     sprintf(typebuf+1, "%x-%x", loadaddr, execaddr);
Stewart Brodie's avatar
Stewart Brodie committed
1356 1357 1358
   }
   else {
     const int type = GET_FILETYPE(loadaddr);
1359
     sprintf(typebuf+1, "%03x", type);
Stewart Brodie's avatar
Stewart Brodie committed
1360 1361
   }

Ben Avison's avatar
Ben Avison committed
1362
   /* Strip any acorn filetype suffix */
1363 1364 1365
   if (Xlt_SplitLeafnameX2 ( leafname, &RA, &nterm ) != OK) {
     /* No type information - find end of string */
     nterm = strchr(leafname, '\0');
Stewart Brodie's avatar
Stewart Brodie committed
1366 1367
   }
   else {
1368 1369 1370
     /* Strip old type information in case source didn't have any either */
     *nterm = '\0';
   }
Ben Avison's avatar
Ben Avison committed
1371 1372
   /* Check leaf for DOS file type, else add acorn extra... */
   if (leafname[1] == ':') {
1373 1374
     term = strrchr(leafname, '.');  /* strrchr catches names like "file.tar.gz" */
   }
Ben Avison's avatar
Ben Avison committed
1375
   else {
1376 1377 1378
     term = strrchr(leafname, '/');  /* strrchr catches names like "file/tar/gz" */
   }

Ben Avison's avatar
Ben Avison committed
1379 1380 1381 1382 1383 1384 1385 1386
   if (term) {
     /* Found a DOS type so check the mimemap */
     if (!_swix(MimeMap_Translate, _INR(0,2)|_OUT(3),
                MMM_TYPE_DOT_EXTN, term,
                MMM_TYPE_RISCOS, &ftype)) {
        /* Got a name valid in RISCOS */
        sprintf(ftypebuf, ",%03x", ftype);
        if (!strcmp(typebuf, ftypebuf)) return 1;
1387
     }
Stewart Brodie's avatar
Stewart Brodie committed
1388
   }
Ben Avison's avatar
Ben Avison committed
1389 1390 1391
   /* Don't append ,xxx filetype if its the default */
   sprintf(ftypebuf,",%03x",LM_Vars.default_type);
   if (strcmp(typebuf,ftypebuf)) strcat(leafname, typebuf);
1392
   return 1;
Stewart Brodie's avatar
Stewart Brodie committed
1393 1394 1395 1396
}
#endif

/* ------------------------------- */