u_short WSAAPI ntohs ( IN u_short netshort ) /*++ Routine Description: Convert a u_short from TCP/IP network byte order to host byte order. Arguments: netshort A 16-bit number in TCP/IP network byte order. Returns: ntohs() returns the value in host byte order. --*/ { return SWAP_SHORT( netshort ); }
u_short WSAAPI htons ( IN u_short hostshort ) /*++ Routine Description: Convert a u_short from host to TCP/IP network byte order. Arguments: hostshort - A 16-bit number in host byte order. Returns: htons() returns the value in TCP/IP network byte order. --*/ { return SWAP_SHORT( hostshort ); }
int matout(char *fullname,char *varname,void *data,int nrows,int ncols,char vartype, char *mode, char endianess) { int size; long nelem; int type,namelen; int mrows,mcols,imagf; int i; FILE *fs; mrows = nrows; mcols = ncols; nelem = mrows*mcols; switch (vartype) { case 'd': /* 8-byte doubles */ type=00; size=8; double *vard=data; if (endianess=='B') { for (i=0;i<nelem;i++) SWAP_DOUBLE(vard[i]); }; break; case 'r': /* 4-byte reals */ type=10; size=4; float *varf=data; if (endianess=='B') { for (i=0;i<nelem;i++) SWAP_FLOAT(varf[i]); }; break; case 'l': /* 4-byte int, row-wise */ type=20; size=4; int *varl=data; if (endianess=='B') { for (i=0;i<nelem;i++) SWAP_INT(varl[i]); }; break; case 's': /* 2-byte signed shorts */ type=30; size=2; short *vars=data; if (endianess=='B') { for (i=0;i<nelem;i++) SWAP_SHORT(vars[i]); }; break; case 'u': /* 2-byte unsigned shorts */ type=40; size=2; short *varus=data; if (endianess=='B') { for (i=0;i<nelem;i++) SWAP_SHORT(varus[i]); }; break; // case 't': /* 2-byte unsigned shorts saved as "text" */ // type=41; // nbytes = mrows*mcols*2; case 't': /* 1-byte unsigned shorts saved as "text" */ type=51; size=1; break; case 'b': /* 1-byte unsigned chars */ case 'c': /* 1-byte signed chars */ type=50; size=1; break; default: return (-1); } if (disable_disk_writing) return (0); for (i=0 ; i<nfiles ; i++) { if (! *matfile[i]) break; if (!strcmp(fullname,matfile[i])) break; } if (i<nfiles) { fs = fd[i]; } else { fs = NULL; } if (fs == NULL) { if ((fs=fopen(fullname,mode))==NULL) { sprintf(message,"%s could not be opened because of reason %d",fullname,errno);; warn(message); return (-1); } strcpy(matfile[i],fullname); fd[i] = fs; if ((i==nfiles) && (nfiles<MAXFILES-1)) nfiles++; } imagf=0; if (endianess=='B') SWAP_INT(type); if (fwrite(&type,sizeof(int),1,fs) != 1) { writerr(); return (-1); } if (endianess=='B') SWAP_INT(mrows); if (fwrite(&mrows,sizeof(int),1,fs) != 1) { writerr(); return (-1); } if (endianess=='B') SWAP_INT(mcols); if (fwrite(&mcols,sizeof(int),1,fs) != 1) { writerr(); return (-1); } if (endianess=='B') SWAP_INT(imagf); if (fwrite(&imagf,sizeof(int),1,fs) != 1) { writerr(); return (-1); } namelen=strlen(varname)+1; if (endianess=='B') SWAP_INT(namelen); if (fwrite(&namelen,sizeof(int),1,fs) != 1) { writerr(); return (-1); } if (endianess=='B') SWAP_INT(namelen); if (fwrite(varname,(unsigned int)namelen,1,fs) != 1) { writerr(); return (-1); } if (fwrite(data,size,nelem,fs) != nelem) { writerr(); return (-1); } matclose(fullname); return (0); }
void Y_ml4read(int nArgs) { char *filename=""; char *varname=""; int leave_open = 0; if (nArgs==2) { filename=YGetString(sp-nArgs+1); varname=YGetString(sp-nArgs+2); leave_open = 0; } else if (nArgs==3) { filename=YGetString(sp-nArgs+1); varname=YGetString(sp-nArgs+2); leave_open=YGetInteger(sp-nArgs+3); } unsigned long bytes_read; int type,namelen; unsigned long nElements,nBytesToRead; int mrows,mcols,imagf; FILE *fs; int fileptr; int endian = 'L'; int size=0,i; fs = openmat(filename); if (fs == NULL) YError(p_strncat("Can't open file ",filename,0)); if (!matfind(fs,varname,50000)) YError(p_strncat("No Such variable ",varname,0)); fileptr = ftell(fs); if (DEBUG) printf("@ position %d\n",fileptr); bytes_read = fread(&type,sizeof(int),1,fs); if (bytes_read==0) { matclose(filename); YError("Premature end of file");; // end of file } fread(&mrows,sizeof(int),1,fs); fread(&mcols,sizeof(int),1,fs); fread(&imagf,sizeof(int),1,fs); fread(&namelen,sizeof(int),1,fs); if (namelen & 0xffff0000) { if (DEBUG) printf("Big endian file\n"); endian = 'B'; SWAP_INT(type); SWAP_INT(mrows); SWAP_INT(mcols); SWAP_INT(imagf); SWAP_INT(namelen); } type = type%1000; if (DEBUG) printf("rows,cols,namelen= %d %d %d\n",mrows,mcols,namelen); if (namelen>255) { fseek(fs,fileptr,SEEK_SET); // leave file ptr at begginning of this variable matclose(filename); YError("Variable name too long!"); } fread(tempvarname,(unsigned int)namelen,1,fs); // if ((*varname!='*') && strcmp(varname,tempvarname)) { // error if not same varname if (!matchvarname(tempvarname,varname)) { // error if not same varname fseek(fs,fileptr,SEEK_SET); // leave file ptr at begginning of this variable matclose(filename); YError(p_strncat("Can't find variable",varname,0)); } nElements = (unsigned)mrows*(unsigned)mcols; Dimension *tmp=tmpDims; tmpDims=0; FreeDimension(tmp); if (mrows<=1) { tmpDims= NewDimension(mcols, 1L, (Dimension *)0); } else if (mcols<=1) { tmpDims= NewDimension(mrows, 1L, (Dimension *)0); } else { tmpDims= NewDimension(mrows, 1L, (Dimension *)0); tmpDims= NewDimension(mcols, 1L, tmpDims); } if (type==0) { // 8-byte doubles size = 8; Array *a= PushDataBlock(NewArray(&doubleStruct, tmpDims)); double *data = a->value.d; bytes_read = fread((void *)data,size,nElements,fs); if (endian=='B') { for (i=0;i<nElements;i++) SWAP_DOUBLE(data[i]); } } else if (type==10) { // 4-byte reals size = 4; Array *a= PushDataBlock(NewArray(&floatStruct, tmpDims)); float *data = a->value.f; bytes_read = fread((void *)data,size,nElements,fs); if (endian=='B') { for (i=0;i<nElements;i++) SWAP_FLOAT(data[i]); } } else if ((type==120) || (type==20)) { // 4-byte int size = 4; Array *a= PushDataBlock(NewArray(&intStruct, tmpDims)); int *data = a->value.l; bytes_read = fread((void *)data,size,nElements,fs); if (endian=='B') { for (i=0;i<nElements;i++) SWAP_INT(data[i]); } } else if (type==30) { // 2-byte signed (30) shorts size = 2; Array *a= PushDataBlock(NewArray(&shortStruct, tmpDims)); short *data = a->value.s; bytes_read = fread((void *)data,size,nElements,fs); if (endian=='B') { for (i=0;i<nElements;i++) SWAP_SHORT(data[i]); } } else if (type==40) { // 2-byte unsigned (40) shorts size = 2; Array *a= PushDataBlock(NewArray(&shortStruct, tmpDims)); short *data = a->value.s; Array *b= PushDataBlock(NewArray(&longStruct, tmpDims)); long *data2 = b->value.l; bytes_read = fread((void *)data,size,nElements,fs); if (endian=='B') { for (i=0;i<nElements;i++) SWAP_SHORT(data[i]); } for (i=1;i<=nElements;i++) *(data2++) = (((long)*(data++))|0xFFFF0000)+65535; } else if (type==50) { // 1-byte signed or unsigned chars (50) size = 1; Array *a= PushDataBlock(NewArray(&charStruct, tmpDims)); char *data = a->value.c; bytes_read = fread((void *)data,size,nElements,fs); } else if (type==51) { // text (51) size = 1; Array *a= PushDataBlock(NewArray(&stringStruct, (Dimension *)0)); char *buf; a->value.q[0] = buf = p_malloc(nElements+1); if (DEBUG) printf("strlen: %d\n",(int)strlen((void *)a->value.q[0])); // bytes_read = fread(a->value.q[0],1,nElements,fs); bytes_read = fread(buf,1,nElements,fs); *((char *)buf + nElements) = 0; // append a NULL to text string } else { matclose(filename); sprintf(message,"Unknown type %d",type); YError(message); } if (bytes_read!=nElements) { fseek(fs,nElements*size,SEEK_CUR); matclose(filename); if (DEBUG) printf("read:%ld expected:%ld\n",bytes_read,nBytesToRead); YError("Premature end of file"); } if (!leave_open) matclose(filename); }
int WSAAPI WSANtohs ( IN SOCKET s, IN u_short netshort, OUT u_short FAR * lphostshort ) /*++ Routine Description: Arguments: Returns: Zero on success else SOCKET_ERROR. The error code is stored with SetErrorCode(). --*/ { PDPROCESS Process; PDTHREAD Thread; PDSOCKET Socket; INT ErrorCode; INT ReturnCode; PPROTO_CATALOG_ITEM CatalogEntry; LPWSAPROTOCOL_INFOW ProtocolInfo; ReturnCode = PROLOG( &Process, &Thread, &ErrorCode); if (ERROR_SUCCESS != ReturnCode) { SetLastError(ErrorCode); return(SOCKET_ERROR); } //if if( lphostshort == NULL ) { SetLastError( WSAEFAULT ); return(SOCKET_ERROR); } ErrorCode = DSOCKET::GetCountedDSocketFromSocket( s, // SocketHandle & Socket); // DSocket if(ERROR_SUCCESS == ErrorCode){ CatalogEntry = Socket->GetCatalogItem(); // This is kind of a special case. We are done with the DSOCKET // object reference and we don't call through to the provider at // all. Socket->DropDSocketReference(); ProtocolInfo = CatalogEntry->GetProtocolInfo(); if (LITTLEENDIAN == ProtocolInfo->iNetworkByteOrder) { *lphostshort = netshort; } //if else { *lphostshort = SWAP_SHORT( netshort ); } //else } //if if (ErrorCode != ERROR_SUCCESS) { SetLastError(ErrorCode); ReturnCode = SOCKET_ERROR; } return(ReturnCode); }
int WSAAPI WSAHtons( IN SOCKET s, IN u_short hostshort, OUT u_short FAR * lpnetshort ) /*++ Routine Description: Convert a u_short from a specified host byte order to network byte order. Arguments: s - A descriptor identifying a socket. netshort - A 16-bit number in network byte order. lphostshort - A pointer to a 16-bit number in host byte order. Returns: If no error occurs, WSANtohs() returns 0. Otherwise, a value of SOCKET_ERROR is returned. --*/ { PDPROCESS Process; PDTHREAD Thread; PDSOCKET Socket; INT ErrorCode; INT ReturnCode; PPROTO_CATALOG_ITEM CatalogEntry; LPWSAPROTOCOL_INFOW ProtocolInfo; ReturnCode = PROLOG( &Process, &Thread, &ErrorCode); if (ERROR_SUCCESS != ReturnCode) { SetLastError(ErrorCode); return(SOCKET_ERROR); } //if if( lpnetshort == NULL ) { SetLastError( WSAEFAULT ); return(SOCKET_ERROR); } ErrorCode = DSOCKET::GetCountedDSocketFromSocket( s, // SocketHandle & Socket); // DSocket if(ERROR_SUCCESS == ErrorCode){ CatalogEntry = Socket->GetCatalogItem(); // This is kind of a special case. We are done with the DSOCKET // object reference and we don't call through to the provider at // all. Socket->DropDSocketReference(); ProtocolInfo = CatalogEntry->GetProtocolInfo(); if (LITTLEENDIAN == ProtocolInfo->iNetworkByteOrder) { *lpnetshort = hostshort; } //if else { *lpnetshort = SWAP_SHORT( hostshort ); } //else } //if if (ErrorCode != ERROR_SUCCESS) { SetLastError(ErrorCode); ReturnCode = SOCKET_ERROR; } return(ReturnCode); }