Xlate 39.8 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 44 45 46 47 48 49
#include "NameCache.h"

#ifdef LONGNAMES
#define FileChar_TypedNamePrefix        ','
#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
 */
50
static const int deaddead = (int)0xDEADDEAD;
Robert Sprowson's avatar
Robert Sprowson committed
51
#endif
Stewart Brodie's avatar
Stewart Brodie committed
52

Robert Sprowson's avatar
Robert Sprowson committed
53
/* Macro returns non-zero if the specific load address is indicative of
Stewart Brodie's avatar
Stewart Brodie committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
 * 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
71
#define ENCODE_FILETYPE(load,type) (((load)|0xFFF00000)^((GET_FILETYPE(load)^(type))<<8))
Stewart Brodie's avatar
Stewart Brodie committed
72 73


74 75 76 77

/* 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
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
/* 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)
95
{
Ben Avison's avatar
Ben Avison committed
96 97 98 99 100 101 102 103 104 105 106 107
  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;
    }
  }
108 109 110 111 112 113 114 115 116 117 118
}

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

/* 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
151
static const ushort daycount[13] =
152 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
{
  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
187 188 189 190 191 192
  /*
  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);
  */

193 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
  /* 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
239
                       (LM_Vars.default_type << 8); /* Default type */
240 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
    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
272
    if ( !IS_FILETYPED(pRA->loadaddr) )
273 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 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 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
    {
      /* 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.

   All strings passed to Jumble & Unjumble should be NAME_LIMIT bytes
   long.
*/

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

  for ( i=0; i < NAME_LIMIT; i++ )
  {
    key <<= 1;
    if ( key & 0x80000 )
      key ^= 39;
    *str -= (key ^ 0x40);
    str++;
  }
}

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

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

  for ( i=0; i < NAME_LIMIT; i++ )
  {
    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
372
/*
373 374 375 376 377
  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 _
378
*/
379
static const char xlt_RO2DOS[256] =
380 381 382 383 384 385
{
  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 */

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

  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
};

428
static const char xlt_DOS2RO[256] =
Ben Avison's avatar
Ben Avison committed
429 430 431 432 433 434
{
  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 */

435 436 437 438 439
/* 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
440
  '0',    '1',    '2',    '3',    '4',    '5',    '6',    '7',
441 442 443
/* 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
444 445 446
  '=',    'A',    'B',    'C',    'D',    'E',    'F',    'G',
  'H',    'I',    'J',    'K',    'L',    'M',    'N',    'O',
  'P',    'Q',    'R',    'S',    'T',    'U',    'V',    'W',
447 448 449
/* 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
450 451 452
  '`',    'a',    'b',    'c',    'd',    'e',    'f',    'g',
  'h',    'i',    'j',    'k',    'l',    'm',    'n',    'o',
  'p',    'q',    'r',    's',    't',    'u',    'v',    'w',
453
/* x       y       z       {       |       }       ~       DEL       78-7F */
Ben Avison's avatar
Ben Avison committed
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
  '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
};
476 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

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
553
  int lcl_name_mode = LM_Vars.namemode & (NM_LOWERCASE | NM_FIRSTCAPS | NM_PRESERVED);
554 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

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


/* These two structures MUST be kept in step in order to allow the
646
 * wildcard resolution code to function correctly.
Stewart Brodie's avatar
Stewart Brodie committed
647 648
 */
static const char lanmanfs_contentious_characters[]=
Stewart Brodie's avatar
Stewart Brodie committed
649
     "+=><,\x20";
Stewart Brodie's avatar
Stewart Brodie committed
650
static const char lanmanfs_contentious_pairing[]=
Stewart Brodie's avatar
Stewart Brodie committed
651
     "&@%$^\xa0";
Stewart Brodie's avatar
Stewart Brodie committed
652 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 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 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745

/* 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;
    }
  }
}

/* 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;
    if (i < level) {
      dst[i] = c;
    }
    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
746
static err_t Xlt_NameXlateCallbackX2 ( BYTE *entry, int format, bool *taken, void *_dst )
Stewart Brodie's avatar
Stewart Brodie committed
747 748 749 750 751 752
{
    Xlt_NXCX2_Data *dst = _dst;
    char *eptr = (char *) (entry + 23);
    char *dptr = 1 + strrchr(dst->matchbuf, '\\');
    err_t res = OK;

753
    (void) format; /* Unused */
Ben Avison's avatar
Ben Avison committed
754
    *taken = true;
755

Stewart Brodie's avatar
Stewart Brodie committed
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 786 787 788 789 790 791 792 793 794 795 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;
      }
      else if (e == ',' && d == 0) {
        /* Might have been a filetype suffix */
        int type, num;
        if ((sscanf(eptr, "%x%n", &type, &num) == 1 && num == 3)
          || strcmp(eptr, FileString_DeadFile) == 0
          || strcmp(eptr, FileString_UntypedFile) == 0) {
          /* It was */
          res = EOUTOFMEM;
          break;
        }
      }
      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
810
  static Transact_SearchContext con;
Stewart Brodie's avatar
Stewart Brodie committed
811
  char *inptr;
812
  int   i;
Stewart Brodie's avatar
Stewart Brodie committed
813 814 815 816 817 818 819 820 821 822 823 824 825 826
  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
827
#if 0 /* No side effects */
Stewart Brodie's avatar
Stewart Brodie committed
828
  (void) NameCache_Locate(private.matchbuf);
Ben Avison's avatar
Ben Avison committed
829
#endif
Stewart Brodie's avatar
Stewart Brodie committed
830 831 832 833 834 835 836 837 838 839 840 841 842
  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);

843 844 845 846 847 848
#if 0
// if I'm not mistaken, the previous translate gets all the contentious chars
// so if the result contains translated chars that would then be contentious
// in riscos, they get retranslated to give incorrect result.
// e.g. riscos '.1 /s' becomes 'A:\1 .s' as expected.
// Second pass takes it to 'A:\1 \s' which is incorrect.
Stewart Brodie's avatar
Stewart Brodie committed
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 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
  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;
    }
  }
898
#endif
Stewart Brodie's avatar
Stewart Brodie committed
899 900 901 902 903 904
  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
905
  con.resume_key = 0;
Stewart Brodie's avatar
Stewart Brodie committed
906 907
  for (status = OK, inptr = private.dstcpybuf; status == OK; inptr = NULL) {
    status = SMB_ReadDirEntries(inptr, 32, Xlt_NameXlateCallbackX2,
Ben Avison's avatar
Ben Avison committed
908
                                &private, &con);
Stewart Brodie's avatar
Stewart Brodie committed
909 910 911 912
    switch (status) {
      case OK:
        /* More entries to read, and no match found yet */
        break;
Ben Avison's avatar
Ben Avison committed
913

Stewart Brodie's avatar
Stewart Brodie committed
914 915 916
      case EOUTOFMEM:
        /* Found it */
        strcpy(dst, private.dstcpybuf);
Ben Avison's avatar
Ben Avison committed
917
        debug1("**RIGHT.  Got a match: `%s'\n", dst);
Stewart Brodie's avatar
Stewart Brodie committed
918
        break;
Ben Avison's avatar
Ben Avison committed
919

Stewart Brodie's avatar
Stewart Brodie committed
920 921 922 923 924
      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
925
        debug1("**WRONG.  Not got a match. Reverting to `%s'\n", dst);
Stewart Brodie's avatar
Stewart Brodie committed
926 927 928 929
        break;
    }
  }

930 931 932 933 934 935 936 937 938
  /* 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
939 940 941
  return OK;
}

942
/* Kicks off the RISC OS to DOS name conversion process - setting up the
Stewart Brodie's avatar
Stewart Brodie committed
943 944 945 946
 * number of already-translated characters as zero.
 */
static err_t Xlt_NameROtoDOSX2 ( char *dst, char *src )
{
Ben Avison's avatar
Ben Avison committed
947
  return Xlt_NameROtoDOSX2_sub(dst, src, 0);
Stewart Brodie's avatar
Stewart Brodie committed
948 949 950 951
}

#endif

952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
/* --------------------------- */


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
1004 1005 1006 1007 1008 1009 1010 1011
#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
1012 1013 1014 1015 1016 1017 1018 1019 1020
    return ( nameROtoDOS ( name_out+2, name_in+1 ) );
  }

  name_out[0] = 0;
  return EBADNAME;
}

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

1021
/* Gets a leaf name from a RISC OS name */
1022 1023 1024 1025 1026

char *Xlt_GetRISCOSLeafName ( char *name_in )
{
  char *tmp;
  tmp = strrchr ( name_in, '.' );
1027
  return ( tmp == NULL ) ? name_in : tmp + 1;
1028 1029 1030 1031 1032 1033 1034 1035 1036
}

/* 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
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
  /* 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.
   */
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
  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
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
#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;

1106 1107
  (void) path_base; /* Unused */

Stewart Brodie's avatar
Stewart Brodie committed
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
  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 )
    {
1136
      /* Always populate the access attributes of ra_out */
Stewart Brodie's avatar
Stewart Brodie committed
1137
      Xlt_CnvDOStoRO ( &da, ra_out, CNV_DATETIME+CNV_ATTRIBS );
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
      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
1149 1150 1151 1152 1153
    }
  }

  return OK;
}
1154

Stewart Brodie's avatar
Stewart Brodie committed
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
/* 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;
1171
  char *term, *oldname;
Stewart Brodie's avatar
Stewart Brodie committed
1172
  err_t res = ENOTPRESENT;
1173
  _kernel_swi_regs rset;
Stewart Brodie's avatar
Stewart Brodie committed
1174 1175

  *terminator = NULL;
1176 1177
  if (leafname == NULL)
    {
Stewart Brodie's avatar
Stewart Brodie committed
1178
    /* Oh dear - don't understand this - just claim it's text */
1179
    pRA->loadaddr = ENCODE_FILETYPE(pRA->loadaddr, 0xFFF);
Stewart Brodie's avatar
Stewart Brodie committed
1180
    return res;
1181 1182 1183 1184
    }

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

Stewart Brodie's avatar
Stewart Brodie committed
1185
  term = strchr(leafname, '\0');
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
  oldname = leafname;
  if ((term - leafname) > 4)
     {
     /* Ah good,it's at least 5 letters long */
     leafname = term - 4;
     if (leafname[0] == FileChar_TypedNamePrefix)
        {
        /* At least 5 letters long and of the form ",ttt" */
        if (stricmp(leafname+1, FileString_UntypedFile) == 0)
           {
           debug0("File is untyped (,lxa)\n");
1197
           pRA->loadaddr = pRA->execaddr = LM_Vars.untyped_address;
1198 1199 1200 1201 1202 1203 1204 1205
           *terminator = leafname;
           res = OK;
           }
        else
           {
           if (stricmp(leafname+1, FileString_DeadFile) == 0)
              {
              debug0("File is DEADDEAD\n");
1206
              pRA->loadaddr = pRA->execaddr = deaddead;
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
              *terminator = leafname;
              res = OK;
              }
           else
              {
              if (sscanf(leafname+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 = leafname;
                 debug3("Filetype is %#03x; load/exec=%#08x %#08x\n", type,
                         pRA->loadaddr, pRA->execaddr);
                 res = OK;
                 }
              }
           }
        }
     }

  if (res != OK)
     {
     /* No ",ttt" was found,try the mimemap for ".ext" */
     leafname = oldname;
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
     /* 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" */
        }

1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
     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
1265 1266
     /* No ",ttt" and no mimemap lookup - mark as default type */
     pRA->loadaddr = ENCODE_FILETYPE(pRA->loadaddr, LM_Vars.default_type);
1267 1268 1269
     }

return res;
Stewart Brodie's avatar
Stewart Brodie committed
1270 1271 1272 1273 1274 1275
}
#endif

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

#ifdef LONGNAMES
1276 1277
/* Trims the optional ",ttt" extension from 'name'
 * and returns the updated string
Stewart Brodie's avatar
Stewart Brodie committed
1278
 */
1279
err_t Xlt_RemoveROType ( char *name )
Stewart Brodie's avatar
Stewart Brodie committed
1280 1281
{
  RISCOS_ATTRIBS RA;
1282
  char          *nterm;
Ben Avison's avatar
Ben Avison committed
1283

1284 1285
  if (Xlt_SplitLeafnameX2( name, &RA, &nterm ) != OK) {
    /* No type information - nothing to do */
Stewart Brodie's avatar
Stewart Brodie committed
1286 1287
  }
  else {
1288
    /* Poke a terminator to get just the base name */
Stewart Brodie's avatar
Stewart Brodie committed
1289 1290
    *nterm = '\0';
  }
Ben Avison's avatar
Ben Avison committed
1291

Stewart Brodie's avatar
Stewart Brodie committed
1292 1293 1294 1295 1296 1297 1298 1299
  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
1300
 * from the passed load address, unless DOS name is sufficient
Stewart Brodie's avatar
Stewart Brodie committed
1301 1302 1303 1304
 */
int Xlt_AddROType ( char *leafname, uint loadaddr )
{
   RISCOS_ATTRIBS RA;
1305 1306 1307
   char *nterm, *term;
   char typebuf[8],ftypebuf[8];
   int ftype;
Stewart Brodie's avatar
Stewart Brodie committed
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317

   typebuf[0] = FileChar_TypedNamePrefix;
   if (loadaddr == deaddead) {
     strcpy(typebuf+1, FileString_DeadFile);
   }
   else if (!IS_FILETYPED(loadaddr)) {
     strcpy(typebuf+1, FileString_UntypedFile);
   }
   else {
     const int type = GET_FILETYPE(loadaddr);
1318
     sprintf(typebuf+1, "%03x", type);
Stewart Brodie's avatar
Stewart Brodie committed
1319 1320
   }

Ben Avison's avatar
Ben Avison committed
1321
   /* Strip any acorn filetype suffix */
1322 1323 1324
   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
1325 1326
   }
   else {
1327 1328 1329
     /* Strip old type information in case source didn't have any either */
     *nterm = '\0';
   }
Ben Avison's avatar
Ben Avison committed
1330 1331
   /* Check leaf for DOS file type, else add acorn extra... */
   if (leafname[1] == ':') {
1332 1333
     term = strrchr(leafname, '.');  /* strrchr catches names like "file.tar.gz" */
   }
Ben Avison's avatar
Ben Avison committed
1334
   else {
1335 1336 1337
     term = strrchr(leafname, '/');  /* strrchr catches names like "file/tar/gz" */
   }

Ben Avison's avatar
Ben Avison committed
1338 1339 1340 1341 1342 1343 1344 1345
   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;
1346
     }
Stewart Brodie's avatar
Stewart Brodie committed
1347
   }
Ben Avison's avatar
Ben Avison committed
1348 1349 1350
   /* Don't append ,xxx filetype if its the default */
   sprintf(ftypebuf,",%03x",LM_Vars.default_type);
   if (strcmp(typebuf,ftypebuf)) strcat(leafname, typebuf);
1351
   return 1;
Stewart Brodie's avatar
Stewart Brodie committed
1352 1353 1354 1355
}
#endif

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