Xlate 39.4 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 27
/* 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
28
#include <stdlib.h>
29 30 31 32
#include <ctype.h>

#include "kernel.h"
#include "stdtypes.h"
33
#include "Global/FileTypes.h"
34
#include "swis.h"
35

36 37 38 39
#include "Xlate.h"
#include "attr.h"
#include "omni.h"
#include "lmvars.h"
Stewart Brodie's avatar
Stewart Brodie committed
40 41 42 43 44 45 46 47 48 49 50
#include "SMB.h"
#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
 */
51
static const int deaddead = (int)0xDEADDEAD;
Stewart Brodie's avatar
Stewart Brodie committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

/* For the moment, untyped files have these invented load/exec addresses.
 * They ensure that if such a file is *Load'ed or *Run'ed, then a data
 * abort will occur
 */
static const int untyped_load = 0x03800000;
static const int untyped_exec = 0x03800000;

/* Macro returns non-zero if the specifiec load addressis indicative of
 * 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
 */
#define ENCODE_FILETYPE(load,type) \
	(((load)|0xFFF00000)^((GET_FILETYPE(load)^(type))<<8))
#endif


83 84 85 86

/* 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
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
/* 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)
104
{
Stewart Brodie's avatar
Stewart Brodie committed
105 106 107 108 109 110 111 112 113 114 115 116
	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;
		}
	}
117 118 119 120 121 122 123 124 125 126 127
}

/* 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
128
#ifdef OLD_SLOW_METHOD
129 130 131
  while ( --len > 0 && *s != 0 )
    *d++ = *s++;
  *d = 0;
Stewart Brodie's avatar
Stewart Brodie committed
132 133 134 135
#else
  *d = 0;
  (void) strncat(d, s, len);
#endif
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 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 187 188 189 190 191 192 193 194 195
}

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

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

static int daycount[13] =
{
  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
196 197 198 199 200 201
  /*
  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);
  */

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 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 272 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 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 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
  /* 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) +
                       0xFE400; /* Default type = 'DOS' */
    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 )
  {
    if ( (pRA->loadaddr & 0xFFF00000) != 0xFFF00000 )
    {
      /* 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 '_'

/* Current tables set


  DOS   RISCOS
  #      ?
  $      <
  %      >
  &      +
  @      =
  ^      ,

  Illegal in RISCOS names: space * " : \ | # $ % & @ ^ DLE

  Also: RISCOS { and [ map to DOS (, } and ] to ),
    ; and top-bit-set chars to _

*/

static char xlt_RO2DOS[256] =
{
  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 */

  CH_ERR, '!',    CH_ERR, CH_WILD,CH_ERR, CH_ERR, CH_ERR, '\'',
  '(',    ')',    CH_WILD,'&',    '^',    '-',    CH_PATH,CH_SEP,
  '0',    '1',    '2',    '3',    '4',    '5',    '6',    '7',
  '8',    '9',    CH_ERR, CH_DUD, '$',    '@',    '%',    '#',

  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',
  'X',    'Y',    'Z',    '(',    CH_ERR, ')',    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',
  'X',    'Y',    'Z',    '(',    CH_ERR, ')',    '~',    CH_ERR,

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

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

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

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


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

}

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

static char xlt_DOS2RO[256] =
{
  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, /* 00-07 */
  CH_DUD, CH_DUD, CH_DUD, CH_DUD, CH_DUD, CH_DUD, CH_DUD, CH_DUD, /* 00-07 */

  CH_DUD, '!',    CH_DUD, '?',    '<',    '>',    '+',    '\'',
  '(',    ')',    CH_DUD, '+',    ',',    '-',    CH_SEP, '/',
  '0',    '1',    '2',    '3',    '4',    '5',    '6',    '7',
  '8',    '9',    CH_DUD, ';',    '<',    '=',    '>',    '?',

  '=',    'A',    'B',    'C',    'D',    'E',    'F',    'G',
  'H',    'I',    'J',    'K',    'L',    'M',    'N',    'O',
  'P',    'Q',    'R',    'S',    'T',    'U',    'V',    'W',
  'X',    'Y',    'Z',    '[',    CH_DUD, ']',    ',',    '_',

  '`',    'a',    'b',    'c',    'd',    'e',    'f',    'g',
  'h',    'i',    'j',    'k',    'l',    'm',    'n',    'o',
  'p',    'q',    'r',    's',    't',    'u',    'v',    'w',
Stewart Brodie's avatar
Stewart Brodie committed
535
  'x',    'y',    'z',    '{',    CH_DUD, '}',    '~',     CH_DUD,
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 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 594 595 596 597 598 599 600 601 602 603 604

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

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

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

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


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

void Xlt_NameDOStoRO ( char *dst, char *src )
{
  int i, c;
  int lcl_name_mode = LM_Vars.namemode & 3;

  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
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 637 638 639 640 641 642 643 644 645 646
#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
647
     "\x20!\"?$%+'()*+,-\\.0123456789:;<=>#"
Stewart Brodie's avatar
Stewart Brodie committed
648 649 650
     "=ABCDEFGHIJKLMNOPQRSTUVWXYZ[.],_"
     "`abcdefghijklmnopqrstuvwxyz{|}~_"
     "€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ"
Stewart Brodie's avatar
Stewart Brodie committed
651
     "\x20¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿"
Stewart Brodie's avatar
Stewart Brodie committed
652 653 654 655 656 657 658 659
     "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞß"
     "àáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ";


/* These two structures MUST be kept in step in order to allow the
 * wildcard resolution code function correctly.
 */
static const char lanmanfs_contentious_characters[]=
Stewart Brodie's avatar
Stewart Brodie committed
660
     "+=><,\x20";
Stewart Brodie's avatar
Stewart Brodie committed
661
static const char lanmanfs_contentious_pairing[]=
Stewart Brodie's avatar
Stewart Brodie committed
662
     "&@%$^\xa0";
Stewart Brodie's avatar
Stewart Brodie committed
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 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 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 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 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 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 935 936 937 938 939 940 941 942

/* 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.
 */
static err_t Xlt_NameXlateCallbackX2 ( BYTE *entry, int format, void *_dst )
{
    Xlt_NXCX2_Data *dst = _dst;
    char *eptr = (char *) (entry + 23);
    char *dptr = 1 + strrchr(dst->matchbuf, '\\');
    err_t res = OK;

    if (dst->dstcpy[0] != '*') {
      return EOUTOFMEM;
    }

    debug3("Xlt_NameXlateCallbackX2: checking `%s', against `%s' in `%s'\n",
        (char *) entry + 23,
        dptr,
    	dst->dstcpybuf);

    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;
  char *inptr;
  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
   */
  (void) NameCache_Locate(private.matchbuf);
  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;
    }
  }

  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
   */
  for (status = OK, inptr = private.dstcpybuf; status == OK; inptr = NULL) {
    status = SMB_ReadDirEntries(inptr, 32, Xlt_NameXlateCallbackX2,
      &private, NULL);
    switch (status) {
      case OK:
        /* More entries to read, and no match found yet */
        break;
      case EOUTOFMEM:
        /* Found it */
        strcpy(dst, private.dstcpybuf);
        debug1("\n**RIGHT.  Got a match: `%s'\n", dst);
        break;
      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);
        debug1("\n**WRONG.  Not got a match. Reverting to `%s'\n", dst);
        break;
    }
  }

  return OK;
}

/* Kicks off the DOS to RISC OS name conversion process - setting up the
 * number of already-translated characters as zero.
 */
static err_t Xlt_NameROtoDOSX2 ( char *dst, char *src )
{
        return Xlt_NameROtoDOSX2_sub(dst, src, 0);
}

#endif

943 944 945 946 947 948 949 950 951 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
/* --------------------------- */


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
995 996 997 998 999 1000 1001 1002
#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
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
    return ( nameROtoDOS ( name_out+2, name_in+1 ) );
  }

  name_out[0] = 0;
  return EBADNAME;
}

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

/* Gets a leaf name from a DOS name */

char *Xlt_GetRISCOSLeafName ( char *name_in )
{
  char *tmp;
  tmp = strrchr ( name_in, '.' );
  return ( tmp == NULL ) ? name_in : tmp;
}

/* 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
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
  /* 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.
   */
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
  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
1071 1072 1073 1074 1075 1076 1077 1078 1079 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 1106 1107 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
#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;

  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 )
    {
      Xlt_CnvDOStoRO ( &da, ra_out, CNV_DATETIME+CNV_ATTRIBS );
      ra_out->loadaddr ^= ((ra_name.loadaddr ^ ra_out->loadaddr) & (0xFFF00));
    }
  }

  return OK;
}
#endif

1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 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
/* Error translation =========================================== */

extern _kernel_oserror *Err_XltTable[MAX_ERRS+1];

static _kernel_oserror *Xlt_OS_Error;

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

err_t Xlt_SetOSError ( _kernel_oserror *err )
/* This is used when an error generated by RISCOS has to be
   returned as if it was one of ours - most notably, errors
   returned from processing a command line. We use a special
   error number, EXT_OS_ERROR, to denote this. */
{
  Xlt_OS_Error = err;

  if ( err == NULL )
    return OK;

  return EXT_OS_ERROR;
}

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

_kernel_oserror *Xlt_Error ( err_t err )
{
  if ( err==0 ) return NULL;

  if ( err==EXT_OS_ERROR && Xlt_OS_Error != NULL )
    return Xlt_OS_Error;       /* Else, drop through to mysterious error */

  if ( err < 0 || err > MAX_ERRS ) /* Use 'mysterious error' */
    err=0;

  return Err_XltTable[err];
}

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

Stewart Brodie's avatar
Stewart Brodie committed
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
#ifdef LONGNAMES
/* 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;
1190
  char *term, *oldname;
Stewart Brodie's avatar
Stewart Brodie committed
1191
  err_t res = ENOTPRESENT;
1192
  _kernel_swi_regs rset;
Stewart Brodie's avatar
Stewart Brodie committed
1193 1194

  *terminator = NULL;
1195 1196
  if (leafname == NULL)
    {
Stewart Brodie's avatar
Stewart Brodie committed
1197 1198 1199
    /* Oh dear - don't understand this - just claim it's text */
    pRA->loadaddr |= 0xFFFFFF00;
    return res;
1200 1201 1202 1203
    }

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

Stewart Brodie's avatar
Stewart Brodie committed
1204
  term = strchr(leafname, '\0');
1205 1206 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 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
  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");
           pRA->loadaddr = untyped_load;
           pRA->execaddr = untyped_exec;
           *terminator = leafname;
           res = OK;
           }
        else
           {
           if (stricmp(leafname+1, FileString_DeadFile) == 0)
              {
              pRA->loadaddr = pRA->execaddr = deaddead;
              debug0("File is DEADDEAD\n");
              *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;
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
     /* 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" */
        }

1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
     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)
     {
     /* No ",ttt" and no mimemap lookup - mark as a DOS file */
     pRA->loadaddr = ENCODE_FILETYPE(pRA->loadaddr, FileType_MSDOS);
     }

return res;
Stewart Brodie's avatar
Stewart Brodie committed
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
}
#endif

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

#ifdef LONGNAMES
/* Copies the filetype information from one name to another.
 * Note that the file saving routine actually uses this
 * routine to strip filetype information by passing both
 * parameters the same.  This must continue to function.
1300 1301 1302
 * JB 18/12/2003 .. make sure the ,xxx is only removed IF
 * both names are NOT (identical and contains .xxx)
 * (function only seen in file renaming so far JB)
Stewart Brodie's avatar
Stewart Brodie committed
1303 1304 1305 1306
 */
err_t Xlt_CnvRenameX2 ( char *src, char *dst )
{
  RISCOS_ATTRIBS RA;
1307 1308
  char  *nterm;  // *terminator
  int cnvq=strcmp(src,dst); // check if just need to strip..
Stewart Brodie's avatar
Stewart Brodie committed
1309

1310 1311 1312 1313 1314 1315
  // if src and dst are same, check if it should have the ,xxx appended
  // if so, ensure it still is...
  if(!cnvq)
  {
    if((strlen(dst)>4) && (dst[strlen(dst)-4] == ',')) cnvq++;
  }
Stewart Brodie's avatar
Stewart Brodie committed
1316 1317 1318 1319 1320 1321 1322 1323
  if (Xlt_SplitLeafnameX2 ( dst, &RA, &nterm ) != OK) {
    /* No type information - find end of string */
    nterm = strchr(dst, '\0');
  }
  else {
    /* Strip old type information in case source didn't have any either */
    *nterm = '\0';
  }
1324 1325 1326
  Xlt_SplitLeafnameX2 ( src, &RA, &nterm ) ; // recover source's filetype
  // if not identical strings.. add type if not in dos name
  if (cnvq)Xlt_AddROType (dst,RA.loadaddr);
Stewart Brodie's avatar
Stewart Brodie committed
1327 1328 1329 1330 1331 1332 1333 1334
  return OK;
}
#endif

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

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

   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);
1353
     sprintf(typebuf+1, "%03x", type);
Stewart Brodie's avatar
Stewart Brodie committed
1354 1355
   }

1356 1357 1358 1359
   // strip any acorn filetype suffix
   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
1360 1361
   }
   else {
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
     /* Strip old type information in case source didn't have any either */
     *nterm = '\0';
   }
   // check leaf for dos file type, else add acorn extra...
   if (leafname[1] == ':')
   {
     term = strrchr(leafname, '.');  /* strrchr catches names like "file.tar.gz" */
   }
   else
   {
     term = strrchr(leafname, '/');  /* strrchr catches names like "file/tar/gz" */
   }

   if(term)
   {                                                // found a DOS type
     if(!_swix(MimeMap_Translate,_INR(0,2)|_OUT(3), // so check the mimemap
                     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;     // its OK.. no need to append type
     }
Stewart Brodie's avatar
Stewart Brodie committed
1384
   }
1385 1386
   strcat(leafname, typebuf);
   return 1;
Stewart Brodie's avatar
Stewart Brodie committed
1387 1388 1389 1390
}
#endif

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