rem: packet->dp_Res1 = -1;
add: packet->dp_Res2 = ERROR_READ_PROTECTED;
context: void _main (){  struct DosPacket *packet;  struct Process *proc = (struct Process *) FindTask (NULL);  PktPort = &proc->pr_MsgPort;  NewList (&HanList);  Initialize ();  Running = TRUE;  AllocCnt = 0;#ifdef DEBUG  kprintf("Init\n");#endif  /*   *        Main Loop   */  while(Running) {    struct Message *msg;    while ((msg = GetMsg (PktPort)) == NULL)      Wait (1 << PktPort->mp_SigBit);    packet = (struct DosPacket *) msg->mn_Node.ln_Name;    /*     *  default return value     */    packet->dp_Res1 = DOS_TRUE;    packet->dp_Res2 = 0;    /*     *  switch on packet     */    switch (packet->dp_Type) {      case ACTION_DIE:        /*  ??? */      {        break;      }      /***********************************************************************/      case ACTION_FINDUPDATE: /*  FileHandle,Lock,Name        Bool        */      case ACTION_FINDINPUT:  /*  FileHandle,Lock,Name        Bool        */      case ACTION_FINDOUTPUT: /*  FileHandle,Lock,Name        Bool        */      {        struct FileHandle *fh = BTOC (packet->dp_Arg1);        unsigned char *base = BTOC (packet->dp_Arg3);        int len = *base;        char buf[128];        struct HandlerData *data;        // Skip volume name and ':'        while(*++base != ':')          --len;        ++base;        {          // Convert /'s to blanks          char *p = base;          while(*++p)            if(*p == '/')              *p = ' ';        }        if (len >= sizeof (buf))          len = sizeof (buf) - 1;        strncpy (buf, base, len - 1);        buf[len - 1] = '\n';        buf[len] = 0;#ifdef DEBUG        kprintf("ACTION_FIND#?: %s\n", (char *) buf);#endif        data = AllocVec(sizeof(struct HandlerData), MEMF_PUBLIC | MEMF_CLEAR);        if(! data) {          packet->dp_Res2 = ERROR_NO_FREE_STORE;          break;        }        if(packet->dp_Res2 = ParseArgs(data, (char *) buf)) {          FreeHData(data);          break;        }        if(packet->dp_Res2 = AllocAudio()) {          FreeAudio();          FreeHData(data);          break;        }        fh->fh_Arg1 = (ULONG) data;        fh->fh_Port = (struct MsgPort *) DOS_TRUE;        break;      }      /***********************************************************************/      case ACTION_READ:        /*  FHArg1,CPTRBuffer,Length    ActLength   */      {        /*         *   Reading is straightforward except for handling EOF... We         *  must guarentee a return value of 0 (no bytes left) before         *  beginning to return EOFs (-1's).  If we return a negative         *  number right off programs like COPY will assume a failure         *  (if AUDIO: is the source) and delete the destination file.         *         *  The basic idea is to feed the packets from one buffer while         *  recording asyncroniously to the other. When we have read         *  the buffer, we wait until the other is filled, and switch         *  buffer pointers.         */        struct HandlerData *data = (struct HandlerData *) packet->dp_Arg1;        UBYTE *dest   = (void *) packet->dp_Arg2;        LONG   length, filled;#ifdef DEBUG        kprintf("ACTION_READ: 0x%08lx, %ld\n", packet->dp_Arg2, packet->dp_Arg3);#endif        if(! data->initialized) {          packet->dp_Res2 = InitHData(data);          if(packet->dp_Res2) {            break;          }        }        length = filled = min(data->totallength, packet->dp_Arg3);        if(length <= 0) {          packet->dp_Res1 = length;          data->totallength = -1;          break;        }        if(data->buffer1 == NULL) {          data->buffer1 = AllocVec(data->buffersize, MEMF_PUBLIC);          data->buffer2 = AllocVec(data->buffersize, MEMF_PUBLIC);          data->readreq    = AllocVec(sizeof (struct AHIRequest), MEMF_PUBLIC);          if((data->buffer1 == NULL)          || (data->buffer2 == NULL)          || (data->readreq    == NULL)) {            packet->dp_Res2 = ERROR_NO_FREE_STORE;            break;          }          CopyMem(AHIio, data->readreq, sizeof (struct AHIRequest));          // Fill buffer 2          // Note that io_Offset is always 0 the first time          data->readreq->ahir_Std.io_Command  = CMD_READ;          data->readreq->ahir_Std.io_Data     = data->buffer2;          data->readreq->ahir_Std.io_Length   = data->buffersize;          data->readreq->ahir_Std.io_Offset   = 0;          data->readreq->ahir_Type            = data->type;          data->readreq->ahir_Frequency       = data->freq;          data->readreq->ahir_Volume          = data->vol;          data->readreq->ahir_Position        = data->pos;          SendIO((struct IORequest *) data->readreq);            // Force buffer switch filling of the other buffer          data->length = data->offset = 0;          // Check if we should write a header first          if(data->format == AIFF) {            if(length < sizeof(struct AIFFHeader)) {              packet->dp_Res1 = -1;              break;            }            FillAIFFheader(data);            CopyMem(&AIFFHeader, dest, sizeof(struct AIFFHeader));             dest              += sizeof(struct AIFFHeader);            length            -= sizeof(struct AIFFHeader);          }          else if(data->format == AIFC) {            if(length < sizeof(struct AIFCHeader)) {              packet->dp_Res1 = -1;              break;            }            FillAIFCheader(data);            CopyMem(&AIFCHeader, dest, sizeof(struct AIFCHeader));             dest              += sizeof(struct AIFCHeader);            length            -= sizeof(struct AIFCHeader);          }        }        while(length > 0) {          LONG thislength;                  if(data->offset >= data->length) {            void *temp;            temp          = data->buffer1;            data->buffer1 = data->buffer2;            data->buffer2 = temp;            WaitIO((struct IORequest *) data->readreq);            data->length = data->readreq->ahir_Std.io_Actual;            data->offset = 0;            if(data->readreq->ahir_Std.io_Error) {              packet->dp_Res1 = -1;              length = 0;              break;            }            data->readreq->ahir_Std.io_Command = CMD_READ;            data->readreq->ahir_Std.io_Data    = data->buffer2;            data->readreq->ahir_Std.io_Length  = data->buffersize;            data->readreq->ahir_Type           = data->type;            data->readreq->ahir_Frequency      = data->freq;            data->readreq->ahir_Volume         = data->vol;            data->readreq->ahir_Position       = data->pos;            SendIO((struct IORequest *) data->readreq);          } /* if */          thislength = min(data->length - data->offset, length);          CopyMem(data->buffer1 + data->offset, dest, thislength);           dest              += thislength;          length            -= thislength;          data->offset      += thislength;          data->totallength -= thislength;        } /* while */        packet->dp_Res1 = filled;        break;      } /* ACTION_READ */      /***********************************************************************/      case ACTION_WRITE:        /*  FHArg1,CPTRBuffer,Length    ActLength   */      {        struct HandlerData *data  = (struct HandlerData *) packet->dp_Arg1;        UBYTE *src    = (void *) packet->dp_Arg2;        LONG   length = packet->dp_Arg3, filled;#ifdef DEBUG        kprintf("ACTION_WRITE: 0x%08lx, %ld\n", packet->dp_Arg2, packet->dp_Arg3);#endif        if(data->buffer1 == NULL) {          // Check headers?          switch(data->args.format) {            case AIFF:              if((((ULONG *) src)[0] != ID_FORM)              || (((ULONG *) src)[2] != ID_AIFF)) {                packet->dp_Res2 = ERROR_OBJECT_WRONG_TYPE;              }              break;            case AIFC:              if((((ULONG *) src)[0] != ID_FORM)              || (((ULONG *) src)[2] != ID_AIFC)) {                packet->dp_Res2 = ERROR_OBJECT_WRONG_TYPE;              }              break;            case 0:              if(((ULONG *) src)[0] == ID_FORM) {                if(((ULONG *) src)[2] == ID_AIFF) {                  data->args.format = AIFF;                }                else if(((ULONG *) src)[2] == ID_AIFC) {                  data->args.format = AIFC;                }              }              break;                        default:              break;          }          if(packet->dp_Res2)            break;          if((data->args.format == AIFF) || (data->args.format == AIFC)) {            LONG skiplen = 0;            skiplen = ReadCOMMchunk(data, src, length);            src    += skiplen;            length -= skiplen;          }          if(packet->dp_Res2 = InitHData(data))            break;          data->writing = TRUE;          data->buffer1 = AllocVec(data->buffersize, MEMF_PUBLIC);          data->buffer2 = AllocVec(data->buffersize, MEMF_PUBLIC);          if((data->buffer1 == NULL) || (data->buffer2 == NULL)) {            packet->dp_Res2 = ERROR_NO_FREE_STORE;            break;          }          data->offset = 0;          data->length = (data->buffersize / AHI_SampleFrameSize(data->type))                          * AHI_SampleFrameSize(data->type);        }        length = min(data->totallength, length);        filled = min(data->totallength, packet->dp_Arg3);        while(length > 0) {          LONG thislength;                  if(data->offset >= data->length) {            if(! PlayAndSwap(data, data->length)) {              packet->dp_Res1 = -1;              length = 0;              break;            }          }          thislength = min(data->length - data->offset, length);          CopyMem(src, data->buffer1 + data->offset, thislength);           src               += thislength;          length            -= thislength;          data->offset      += thislength;          data->totallength -= thislength;        } /* while */        packet->dp_Res1 = filled;        break;      }      /***********************************************************************/      case ACTION_END:        /*  FHArg1                      Bool:TRUE   */      {        struct HandlerData *data = (struct HandlerData *) packet->dp_Arg1;#ifdef DEBUG        kprintf("ACTION_END\n");#endif        // Abort any reading requests        if(data->readreq) {          AbortIO((struct IORequest *) data->readreq);          WaitIO((struct IORequest *) data->readreq);        }        // Finish any playing requests        if(data->writing) {          PlayAndSwap(data, data->offset);          if(data->writereq1) {            WaitIO((struct IORequest *) data->writereq1);          }          if(data->writereq2) {            WaitIO((struct IORequest *) data->writereq2);          }        }        FreeHData(data);        FreeAudio();        break;      }      /***********************************************************************/      case ACTION_IS_FILESYSTEM:        packet->dp_Res1 = DOS_FALSE;        break;      /***********************************************************************/      default:        packet->dp_Res2 = ERROR_ACTION_NOT_KNOWN;        break;    } /* switch */    if(AllocCnt == 0)      Running = FALSE;    if (packet) {      if (packet->dp_Res2)        packet->dp_Res1 = DOS_FALSE;      returnpacket (packet);#ifdef DEBUG      kprintf("Retured packet\n");#endif    }  } /* for */#ifdef DEBUG  kprintf("Dying..!\n");#endif  UnInitialize();  _exit (0);}