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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 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
|
/*
* $Id: ntstream.c,v 1.1 2003/01/25 15:10:03 andrew_belov Exp $
* ---------------------------------------------------------------------------
* A convenient interface to the NT backup stream functions (Backup*)
*
*/
#include "arj.h"
DEBUGHDR(__FILE__) /* Debug information block */
#define W32_SID_HEADER_SIZE 20
#if TARGET==WIN32
/* Open a file for stream operation */
struct nt_sid *open_streams(char *name, int is_write)
{
struct nt_sid *rc;
if((rc=(struct nt_sid *)malloc(sizeof(struct nt_sid)))==NULL)
return(NULL);
if((rc->hf=CreateFile(name, is_write?GENERIC_WRITE:GENERIC_READ,
FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS,
0))==INVALID_HANDLE_VALUE)
{
free(rc);
return(NULL);
}
rc->is_write=is_write;
rc->lpcontext=NULL;
rc->rem.LowPart=rc->rem.HighPart=0;
return(rc);
}
/* Finalize a backup operation before closing the stream */
static void finalize_backup(struct nt_sid *sid)
{
DWORD dummy;
if(sid->lpcontext!=NULL)
{
if(sid->is_write)
BackupWrite(sid->hf, NULL, 0, &dummy, TRUE, FALSE, &sid->lpcontext);
#if SFX_LEVEL>=ARJ
else
BackupRead(sid->hf, NULL, 0, &dummy, TRUE, FALSE, &sid->lpcontext);
#endif
sid->lpcontext=NULL;
}
}
/* Close a stream operations handle */
void close_streams(struct nt_sid *sid)
{
finalize_backup(sid);
CloseHandle(sid->hf);
}
/* Skip to the next stream */
int next_stream(WIN32_STREAM_ID *dest, struct nt_sid *sid)
{
DWORD lo, hi, b;
if(sid->is_write)
return(-1);
if(sid->rem.LowPart!=0||sid->rem.HighPart!=0)
{
if(!BackupSeek(sid->hf, sid->rem.LowPart, sid->rem.HighPart, &lo, &hi, &sid->lpcontext))
return(-1);
if(lo!=sid->rem.LowPart||hi!=sid->rem.HighPart)
return(-1);
sid->rem.LowPart=sid->rem.HighPart=0;
}
if(!BackupRead(sid->hf, (unsigned char *)dest, W32_SID_HEADER_SIZE, &b, FALSE, TRUE, &sid->lpcontext))
return(-1);
if(b!=W32_SID_HEADER_SIZE)
return(-1);
sid->rem=dest->Size;
return(0);
}
/* Looks for a specific stream ID. Returns stream size. */
unsigned long seek_stream_id(DWORD id, struct nt_sid *sid)
{
int tampered;
WIN32_STREAM_ID winsid;
/* Perform 1st iteration for "virgin" streams (that haven't yet been operated
with). Redo the lookup in a second operation if the streams were in use. */
tampered=(sid->lpcontext!=NULL)?1:0;
do
{
while(!next_stream(&winsid, sid))
{
if(winsid.dwStreamId==id)
return((winsid.Size.HighPart>0)?0xFFFFFFFF:winsid.Size.LowPart);
}
finalize_backup(sid);
} while(tampered--);
return(0);
}
#if SFX_LEVEL>=ARJ
/* Read from a stream */
unsigned long read_stream(unsigned char *dest, unsigned long bytes, struct nt_sid *sid)
{
unsigned long br, rc;
if(sid->is_write)
return(0);
br=(sid->rem.HighPart==0&&sid->rem.LowPart<bytes)?sid->rem.LowPart:bytes;
if(br==0)
return(0);
if(!BackupRead(sid->hf, dest, br, &rc, FALSE, TRUE, &sid->lpcontext))
return(0);
sid->rem=LargeIntegerSubtract(sid->rem, ConvertUlongToLargeInteger(rc));
return(rc);
}
#endif
/* Initialize a new stream before writing */
int create_stream(WIN32_STREAM_ID *src, struct nt_sid *sid)
{
DWORD lo, hi, b;
if(!sid->is_write)
return(-1);
if(sid->rem.LowPart!=0&&sid->rem.HighPart!=0)
{
if(!BackupSeek(sid->hf, sid->rem.LowPart, sid->rem.HighPart, &lo, &hi, &sid->lpcontext))
return(-1);
if(lo!=sid->rem.LowPart||hi!=sid->rem.HighPart)
return(-1);
sid->rem.LowPart=sid->rem.HighPart=0;
}
if(!BackupWrite(sid->hf, (unsigned char *)src, W32_SID_HEADER_SIZE, &b, FALSE, TRUE, &sid->lpcontext))
return(-1);
if(b!=W32_SID_HEADER_SIZE)
return(-1);
sid->rem=src->Size;
return(0);
}
/* Write to a stream */
unsigned long write_stream(unsigned char *src, unsigned long bytes, struct nt_sid *sid)
{
unsigned long bw, rc;
if(!sid->is_write)
return(0);
/* This is crucial to prevent overflow */
bw=(sid->rem.HighPart==0&&sid->rem.LowPart<bytes)?sid->rem.LowPart:bytes;
if(bw==0)
return(0);
if(!BackupWrite(sid->hf, src, bw, &rc, FALSE, TRUE, &sid->lpcontext))
return(0);
sid->rem=LargeIntegerSubtract(sid->rem, ConvertUlongToLargeInteger(rc));
return(rc);
}
#endif
|