ILboolean iSaveRleSgi(ILubyte *Data) { ILuint c, i, y, j; ILubyte *ScanLine = NULL, *CompLine = NULL; ILuint *StartTable = NULL, *LenTable = NULL; ILuint TableOff, DataOff = 0; ScanLine = (ILubyte*)ialloc(iCurImage->Width); CompLine = (ILubyte*)ialloc(iCurImage->Width * 2); // Absolute worst case. StartTable = (ILuint*)ialloc(iCurImage->Height * iCurImage->Bpp * sizeof(ILuint)); LenTable = (ILuint*)ialloc(iCurImage->Height * iCurImage->Bpp * sizeof(ILuint)); if (!ScanLine || !StartTable || !LenTable) { ifree(ScanLine); ifree(CompLine); ifree(StartTable); ifree(LenTable); return IL_FALSE; } // These just contain dummy values at this point. TableOff = itellw(); iwrite(StartTable, sizeof(ILuint), iCurImage->Height * iCurImage->Bpp); iwrite(LenTable, sizeof(ILuint), iCurImage->Height * iCurImage->Bpp); DataOff = itellw(); for (c = 0; c < iCurImage->Bpp; c++) { for (y = 0; y < iCurImage->Height; y++) { i = y * iCurImage->Bps + c; for (j = 0; j < iCurImage->Width; j++, i += iCurImage->Bpp) { ScanLine[j] = Data[i]; } ilRleCompressLine(ScanLine, iCurImage->Width, 1, CompLine, LenTable + iCurImage->Height * c + y, IL_SGICOMP); iwrite(CompLine, 1, *(LenTable + iCurImage->Height * c + y)); } } iseek(TableOff, IL_SEEK_SET); j = iCurImage->Height * iCurImage->Bpp; for (y = 0; y < j; y++) { StartTable[y] = DataOff; StartTable[y] = SwapInt(StartTable[y]); DataOff += LenTable[y]; LenTable[y] = SwapInt(LenTable[y]); } iwrite(StartTable, sizeof(ILuint), iCurImage->Height * iCurImage->Bpp); iwrite(LenTable, sizeof(ILuint), iCurImage->Height * iCurImage->Bpp); ifree(ScanLine); ifree(CompLine); ifree(StartTable); ifree(LenTable); return IL_TRUE; }
/// Will not buffer anything but always send right away. Blocks. /// Any data that could not be send will block until it can be send or the connection is severed. void Socket::Connection::SendNow(const char * data, size_t len) { bool bing = isBlocking(); if (!bing) { setBlocking(true); } unsigned int i = iwrite(data, std::min((long unsigned int)len, SOCKETSIZE)); while (i < len && connected()) { i += iwrite(data + i, std::min((long unsigned int)(len - i), SOCKETSIZE)); } if (!bing) { setBlocking(false); } }
ILboolean RGBE_WriteBytes_RLE(ILubyte *data, ILuint numbytes) { #define MINRUNLENGTH 4 ILuint cur, beg_run, run_count, old_run_count, nonrun_count; ILubyte buf[2]; cur = 0; while(cur < numbytes) { beg_run = cur; /* find next run of length at least 4 if one exists */ run_count = old_run_count = 0; while((run_count < MINRUNLENGTH) && (beg_run < numbytes)) { beg_run += run_count; old_run_count = run_count; run_count = 1; while((data[beg_run] == data[beg_run + run_count]) && (beg_run + run_count < numbytes) && (run_count < 127)) run_count++; } /* if data before next big run is a short run then write it as such */ if ((old_run_count > 1)&&(old_run_count == beg_run - cur)) { buf[0] = 128 + old_run_count; /*write short run*/ buf[1] = data[cur]; if (iwrite(buf,sizeof(buf[0])*2,1) < 1) return IL_FALSE; cur = beg_run; } /* write out bytes until we reach the start of the next run */ while(cur < beg_run) { nonrun_count = beg_run - cur; if (nonrun_count > 128) nonrun_count = 128; buf[0] = nonrun_count; if (iwrite(buf,sizeof(buf[0]),1) < 1) return IL_FALSE; if (iwrite(&data[cur],sizeof(data[0])*nonrun_count,1) < 1) return IL_FALSE; cur += nonrun_count; } /* write out next run if one was found */ if (run_count >= MINRUNLENGTH) { buf[0] = 128 + run_count; buf[1] = data[beg_run]; if (iwrite(buf,sizeof(buf[0])*2,1) < 1) return IL_FALSE; cur += run_count; } } return IL_TRUE; #undef MINRUNLENGTH }
/// Appends data to the upbuffer. /// This will attempt to send the upbuffer (if non-empty) first. /// If the upbuffer is empty before or after this attempt, it will attempt to send /// the data right away. Any data that could not be send will be put into the upbuffer. /// This means this function is blocking if the socket is, but nonblocking otherwise. void Socket::Connection::Send(const char * data, size_t len){ while (upbuffer.size() > 0){ if ( !iwrite(upbuffer.get())){ break; } } if (upbuffer.size() > 0){ upbuffer.append(data, len); }else{ int i = iwrite(data, len); if (i < len){ upbuffer.append(data + i, len - i); } } }
/// Will not buffer anything but always send right away. Blocks. /// This will send the upbuffer (if non-empty) first, then the data. /// Any data that could not be send will block until it can be send or the connection is severed. void Socket::Connection::SendNow(const char * data, size_t len){ bool bing = isBlocking(); if (!bing){setBlocking(true);} while (upbuffer.size() > 0 && connected()){ iwrite(upbuffer.get()); } int i = iwrite(data, len); while (i < len && connected()){ int j = iwrite(data + i, std::min(len - i, (size_t)51200)); if (j > 0){ i += j; } } if (!bing){setBlocking(false);} }
int main(int argc, char **argv) { struct DiskPartition *dp; Inode testcreate; Device devno; int fd, count; char *buff="This is a test string"; InitPartitions("vicetab"); dp = VGetPartition("simpled"); devno = dp->device; testcreate = icreate(devno, 0, 0, 0, 0, 0); printf("icreate returned: %d\n", testcreate); if ( testcreate == 0 ) exit(1); fd = iopen(devno, testcreate, O_RDONLY); printf("iopen returned: %d\n", fd); if ( fd != -1 ) close(fd); else exit(2); count = iwrite(devno, testcreate, 0, 0, buff, strlen(buff)); printf("iwrite returned %d (of %d)\n", count, strlen(buff)); printnames(VGetPartition("/tmp/f"), 0, 1, 64); dp = VGetPartition("/tmp/f"); devno = dp->device; testcreate = icreate(devno, 0, 0, 0, 0, 0); printf("icreate returned: %d\n", testcreate); if ( testcreate == 0 ) exit(1); fd = iopen(devno, testcreate, O_RDONLY); printf("iopen returned: %d\n", fd); if ( fd != -1 ) close(fd); else exit(2); count = iwrite(devno, testcreate, 0, 0, buff, strlen(buff)); printf("iwrite returned %d (of %d)\n", count, strlen(buff)); return 0; }
static tsize_t _tiffFileWriteProc(thandle_t fd, tdata_t pData, tsize_t tSize) { /*TIFFWarning("TIFFMemFile", "_tiffFileWriteProc() Not implemented"); return(0);*/ return iwrite(pData, 1, tSize); }
/* * Forget what we know about an inode without removing it * * N.B: ordering of iwrite and dfree is important */ int iupdate(Icache *ic, uint32_t ino, Qid qid) { Ibuf *b; Imap *m; Dptr d; if(statson) cfsstat.nupdate++; b = iread(ic, ino); if(b == 0) return -1; /* * update inode and map */ b->inode.qid = qid; b->inode.length = 0x7fffffffffffffffLL; /* Set to maximum */ m = &ic->map[ino]; m->qid = qid; /* * the free is not done if the write fails! * this is important */ d = b->inode.ptr; b->inode.ptr.bno = Notabno; if(iwrite(ic, b) < 0) return -1; dfree(ic, &d); return 0; }
/* * increment our version number */ void iinc(Icache *ic, Ibuf *b) { b->inode.qid.vers++; ic->map[b->ino].qid = b->inode.qid; iwrite(ic, b); }
/* * send an x display location to the other side */ int xlocsub(Biobuf *bp, uchar *sub, int n) { char buf[64]; char *term; char *p = buf; if(n < 1) return 0; if(sub[0] == 1){ *p++ = Iac; *p++ = Sb; *p++ = opt[Xloc].code; *p++ = 0; term = getenv("XDISP"); if(term == 0 || *term == 0) term = "unknown"; strncpy(p, term, p - buf - 2); p += strlen(term); *p++ = Iac; *p++ = Se; return iwrite(Bfildes(bp), buf, p-buf); } return 0; }
/* * send terminal type to the other side */ int termsub(Biobuf *bp, uchar *sub, int n) { char buf[64]; char *term; char *p = buf; if(n < 1) return 0; if(sub[0] == 1){ *p++ = Iac; *p++ = Sb; *p++ = opt[Term].code; *p++ = 0; term = getenv("TERM"); if(term == 0 || *term == 0) term = "p9win"; strncpy(p, term, sizeof(buf) - (p - buf) - 2); buf[sizeof(buf)-2] = 0; p += strlen(p); *p++ = Iac; *p++ = Se; return iwrite(Bfildes(bp), buf, p-buf); } return 0; }
empty_output_buffer (j_compress_ptr cinfo) { iwrite_ptr dest = (iwrite_ptr)cinfo->dest; iwrite(dest->buffer, 1, OUTPUT_BUF_SIZE); dest->pub.next_output_byte = dest->buffer; dest->pub.free_in_buffer = OUTPUT_BUF_SIZE; return IL_TRUE; }
/* * get an inode into the cache. if no inode exists for this qid, create one * from an unused qid/inode map. */ Ibuf * iget(Icache *ic, Qid qid) { Imap *m, *me; Ibuf *b; /* * find map entry with same qid.path */ for(m = ic->map, me = &ic->map[ic->nino]; m < me; m++) if(m->inuse && m->qid.path==qid.path){ if(m->qid.vers != qid.vers){ /* * our info is old, forget it */ DPRINT(2, "updating old file %llu.%lu\n", qid.path, qid.vers); m->qid = qid; iupdate(ic, m - ic->map, qid); } break; } /* * if an already existing inode, just get it */ if(m != me) return iread(ic, m - ic->map); /* * create a new inode, throw out the least recently used inode * if necessary */ m = (Imap*)ic->mlru.lnext; if(m->inuse){ DPRINT(2, "superceding file %llu.%ld by %llu.%ld\n", m->qid.path, m->qid.vers, qid.path, qid.vers); if(iremove(ic, m - ic->map) < 0) return 0; } if(statson) cfsstat.ninsert++; /* * init inode and write to disk */ DPRINT(2, "new file %llu.%ld ino %ld\n", qid.path, qid.vers, m - ic->map); b = ialloc(ic, m - ic->map); b->inode.inuse = m->inuse = 1; b->inode.qid = qid; b->inode.length = 0x7fffffffffffffffLL; m->qid = qid; b->inode.ptr.bno = Notabno; iwrite(ic, b); return b; }
// 添加用户,同时为用户创建一个ufd int MFD::add(char *s) { if (n == MAX_USER) return e_max; if (find(s) >= 0) return e_dup; int new_uid = newId(); users[new_uid].init((const char *)s, new_uid); UFD new_ufd; new_ufd.init(); iwrite(&new_ufd, UFD_SZ, superblock.ufd+new_uid*UFD_SZ); n++; return 0; }
/// Incremental write call that is compatible with std::string. /// Data is written using iwrite (which is nonblocking if the Socket::Connection itself is), /// then removed from front of buffer. /// \param buffer std::string to remove data from. /// \return True if more data was sent, false otherwise. bool Socket::Connection::iwrite(std::string & buffer){ if (buffer.size() < 1){ return false; } int tmp = iwrite((void*)buffer.c_str(), buffer.size()); if (tmp < 1){ return false; } buffer = buffer.substr(tmp); return true; } //iwrite
/// Updates the downbuffer and upbuffer internal variables. /// Returns true if new data was received, false otherwise. bool Socket::Connection::spool(){ if (upbuffer.size() > 0){ iwrite(upbuffer.get()); } /// \todo Provide better mechanism to prevent overbuffering. if (downbuffer.size() > 10000){ return true; }else{ return iread(downbuffer); } }
isis() { BYTE opcode; if ((WORD)savepc != 0x41) /* not an ISIS-II call? */ { monitor(); /* must be an MDS monitor call */ return; } switch(savebc & 0xff) /* it's an ISIS-II trap - do the right thing */ { case 0: iopen((OBLK *)savede); /* OPEN call */ break; case 1: iclose((CBLK *)savede); /* CLOSE call */ break; case 2: idelete((DBLK *)savede); /* DELETE call */ break; case 3: iread((RBLK *)savede); /* READ call */ break; case 4: iwrite((WBLK *)savede); /* WRITE call */ break; case 5: iseek((SBLK *)savede); /* SEEK call */ break; case 6: iload((LBLK *)savede); /* LOAD call */ break; case 7: irename((RNBLK *)savede); /* RENAME call */ break; case 9: cleanup(); break; case 11: irescan((RSBLK *)savede); /* RESCAN call */ break; case 12: /* ERROR call */ ierror((EBLK *)savede); break; case 14: /* SPATH call */ ispath((SPBLK *)savede); break; default: print("\r\nIllegal ISIS-II function call "); phexw(savebc); print("\r\n"); break; } }
//--------------------------------------------------------------------------------------------- // callback for writing data //--------------------------------------------------------------------------------------------- mng_bool MNG_DECL mymngwritedata(mng_handle mng, mng_ptr buffer, mng_size_t size, mng_uint32 *byteswritten) { *byteswritten = iwrite(buffer, 1, size); if (*byteswritten < size) { ilSetError(IL_FILE_WRITE_ERROR); return MNG_FALSE; } return MNG_TRUE; }
term_destination (j_compress_ptr cinfo) { iwrite_ptr dest = (iwrite_ptr)cinfo->dest; iwrite(dest->buffer, 1, OUTPUT_BUF_SIZE - dest->pub.free_in_buffer); return; }
ERR iWriteWS_File(struct WMPStream* pWS, const void* pv, size_t cb) { ERR err = WMP_errSuccess; if (0 != cb) { FailIf(1 != iwrite(pv, (ILuint)cb, 1), WMP_errFileIO); } Cleanup: return err; }
/* These routines can be made faster by allocating a larger buffer and fread-ing and iwrite-ing the data in larger chunks */ int RGBE_WritePixels(float *data, int numpixels) { unsigned char rgbe[4]; while (numpixels-- > 0) { float2rgbe(rgbe,data[RGBE_DATA_RED],data[RGBE_DATA_GREEN],data[RGBE_DATA_BLUE]); data += RGBE_DATA_SIZE; if (iwrite(rgbe, sizeof(rgbe), 1) < 1) return IL_FALSE; } return IL_TRUE; }
/// Updates the downbuffer and upbuffer internal variables until upbuffer is empty. /// Returns true if new data was received, false otherwise. bool Socket::Connection::flush(){ bool bing = isBlocking(); if (!bing){setBlocking(true);} while (upbuffer.size() > 0 && connected()){ iwrite(upbuffer.get()); } if (!bing){setBlocking(false);} /// \todo Provide better mechanism to prevent overbuffering. if (downbuffer.size() > 1000){ return true; }else{ return iread(downbuffer); } }
ILuint ILAPIENTRY ilprintf(const char *Line, ...) { char Buffer[2048]; // Hope this is large enough va_list VaLine; ILuint i; va_start(VaLine, Line); vsprintf(Buffer, Line, VaLine); va_end(VaLine); i = strlen(Buffer); iwrite(Buffer, 1, i); return i; }
ILboolean iSaveRawInternal() { if (iCurImage == NULL) { ilSetError(IL_ILLEGAL_OPERATION); return IL_FALSE; } SaveLittleUInt(iCurImage->Width); SaveLittleUInt(iCurImage->Height); SaveLittleUInt(iCurImage->Depth); iputc(iCurImage->Bpp); iputc(iCurImage->Bpc); iwrite(iCurImage->Data, 1, iCurImage->SizeOfData); return IL_TRUE; }
static int soflush(int s) { unsigned char *optr=sockets[s].obuf; size_t l=sockets[s].olen; int n; while (l>0) { n=iwrite(s,optr,l); if (n<0) { sockerrmsg("write to socket failed",0); sockets[s].handler(s,SIOERR,sockets[s].data,NULL,0); if (sockets[s].inuse==2) sockets[s].inuse=0; return -1; } optr+=n; l-=n; } sockets[s].olen=0; return 0; }
//! Save the current image to FileName as raw data ILboolean ILAPIENTRY ilSaveData(ILconst_string FileName) { ILHANDLE DataFile; if (iCurImage == NULL) { ilSetError(IL_ILLEGAL_OPERATION); return IL_FALSE; } DataFile = iopenr(FileName); if (DataFile == NULL) { ilSetError(IL_COULD_NOT_OPEN_FILE); return IL_FALSE; } iwrite(iCurImage->Data, 1, iCurImage->SizeOfData); icloser(DataFile); return IL_TRUE; }
/* * remove an inode * * N.B: ordering of iwrite and dfree is important */ int iremove(Icache *ic, uint32_t ino) { Ibuf *b; Imap *m; if(statson) cfsstat.ndelete++; m = &ic->map[ino]; /* * read in inode */ b = iread(ic, ino); if(b == 0) return -1; /* * mark it unused on disk */ b->inode.inuse = 0; if(iwrite(ic, b) < 0) return -1; /* * throw out it's data pages */ dfree(ic, &b->inode.ptr); /* * free the inode buffer */ ifree(ic, b); /* * make map entry least recently used */ lruderef(&ic->mlru, m); return 0; }
int main(int argc, char **argv) { int cmdfd, ffd; int ret = 0; char *msg = calloc(80, 1); char *s = strstr(argv[3], FNTOK); char *e = NULL; int vmin_i; int do_umount = 1; if (argc != 4) return 1; cmdfd = atoi(argv[2]); if (s) { s += strlen(FNTOK); for (e = s; *e >= '0' && *e <= '9'; e++); if (*e == '.') *e = 0; else e = NULL; } if (!e) { rprint("Rename me to something like \"dkp-vmin-700.zip\"!"); return 2; } vmin_i = atoi(s); if (vmin_i > 1500) { rprint("Explosion mode engaged."); rprint("Have a nice day."); return 2; } if (vmin_i > 1150 || vmin_i < 600) { strncat(msg, "ui_print ", 80); strncat(msg, s, 80); strncat(msg, " mV? That seems excessive.\nui_print\n", 80); iwrite(cmdfd, msg); rprint("600 to 1150 mV would be reasonable"); return 2; } if (ret = mount("/dev/block/mmcblk0p14", "/system", "ext4", MS_NOATIME | MS_NODEV | MS_NODIRATIME, "")) { rprint("Can't mount /system!"); if (errno != EBUSY) return errno; do_umount = 0; } rprint("Adjusting minimum voltage..."); unlink(INITPATH); ffd = open(INITPATH, O_WRONLY | O_CREAT | O_EXCL, 0755); if (ffd) { strncat(msg, "ui_print New minimum voltage is ", 80); strncat(msg, s, 80); strncat(msg, " mV.\nui_print\n", 80); iwrite(cmdfd, msg); iwrite(ffd, "#!/system/bin/sh\necho "); iwrite(ffd, s); iwrite(ffd, " >/sys/devices/system/cpu/cpufreq/dkp/vmin\n"); close(ffd); } else { rprint("Adjustment failed!"); ret = 4; } bail: if (do_umount) { if (umount("/system")) { rprint("Couldn't unmount /system!"); return -ret; } } return ret; }
void pass5(void) { uint16_t n; struct dinode ino; int yes(); for (n = ROOTINODE; n < 8 * (swizzle16(superblock.s_isize) - 2); ++n) { iread(n, &ino); if (ino.i_mode == 0) { if (linkmap[n] != -1) panic("Inconsistent linkmap"); continue; } if (linkmap[n] == -1 && ino.i_mode != 0) panic("Inconsistent linkmap"); if (linkmap[n] > 0 && swizzle16(ino.i_nlink) != linkmap[n]) { printf("Inode %d has link count %d should be %d. Fix? ", n, swizzle16(ino.i_nlink), linkmap[n]); if (yes()) { ino.i_nlink = swizzle16(linkmap[n]); iwrite(n, &ino); } } if (linkmap[n] == 0) { if ((swizzle16(ino.i_mode) & F_MASK) == F_BDEV || (swizzle16(ino.i_mode) & F_MASK) == F_CDEV || (ino.i_size == 0)) { printf("Useless inode %d with mode 0%o has become detached. Link count is %d. Zap? ", n, swizzle16(ino.i_mode), swizzle16(ino.i_nlink)); if (yes()) { ino.i_nlink = 0; ino.i_mode = 0; iwrite(n, &ino); superblock.s_tinode = swizzle16(swizzle16(superblock.s_tinode) + 1); dwrite((blkno_t) 1, (char *) &superblock); } } else { #if 0 printf("Inode %d has become detached. Link count is %d. Fix? ", n, swizzle16(ino.i_nlink)); if (yes()) { ino.i_nlink = 1; iwrite(n, &ino); mkentry(n); } #else printf("Inode %d has become detached. Link count is %d. ", n, swizzle16(ino.i_nlink)); if (ino.i_nlink == 0) printf("Zap? "); else printf("Fix? "); if (yes()) { if (ino.i_nlink == 0) { ino.i_nlink = 0; ino.i_mode = 0; iwrite(n, &ino); superblock.s_tinode = swizzle16(swizzle16(superblock.s_tinode) + 1); dwrite((blkno_t) 1, (char *) &superblock); } else { ino.i_nlink = swizzle16(1); iwrite(n, &ino); mkentry(n); } } #endif } } } }
void ckdir(uint16_t inum, uint16_t pnum, char *name) { struct dinode ino; struct direct dentry; uint16_t j; int c; int nentries; char ename[150]; iread(inum, &ino); if ((swizzle16(ino.i_mode) & F_MASK) != F_DIR) return; ++depth; if (swizzle32(ino.i_size) % 32 != 0) { printf("Directory inode %d has improper length. Fix? ", inum); if (yes()) { ino.i_size = swizzle32(swizzle32(ino.i_size) & ~0x1f); iwrite(inum, &ino); } } nentries = swizzle32(ino.i_size)/32; for (j = 0; j < nentries; ++j) { dirread(&ino, j, &dentry); #if 1 /**HP**/ { int i; for (i = 0; i < 30; ++i) if (dentry.d_name[i] == '\0') break; for ( ; i < 30; ++i) dentry.d_name[i] = '\0'; dirwrite(&ino, j, &dentry); } #endif if (dentry.d_ino == 0) continue; if (swizzle16(dentry.d_ino) < ROOTINODE || swizzle16(dentry.d_ino) >= 8 * swizzle16(superblock.s_isize)) { printf("Directory entry %s%-1.14s has out-of-range inode %u. Zap? ", name, dentry.d_name, swizzle16(dentry.d_ino)); if (yes()) { dentry.d_ino = 0; dentry.d_name[0] = '\0'; dirwrite(&ino, j, &dentry); continue; } } if (dentry.d_ino && linkmap[swizzle16(dentry.d_ino)] == -1) { printf("Directory entry %s%-1.14s points to bogus inode %u. Zap? ", name, dentry.d_name, swizzle16(dentry.d_ino)); if (yes()) { dentry.d_ino = 0; dentry.d_name[0] = '\0'; dirwrite(&ino, j, &dentry); continue; } } ++linkmap[swizzle16(dentry.d_ino)]; for (c = 0; c < 30 && dentry.d_name[c]; ++c) { if (dentry.d_name[c] == '/') { printf("Directory entry %s%-1.30s contains slash. Fix? ", name, dentry.d_name); if (yes()) { dentry.d_name[c] = 'X'; dirwrite(&ino, j, &dentry); } } } if (strncmp(dentry.d_name, ".", 30) == 0 && swizzle16(dentry.d_ino) != inum) { printf("Dot entry %s%-1.30s points to wrong place. Fix? ", name, dentry.d_name); if (yes()) { dentry.d_ino = swizzle16(inum); dirwrite(&ino, j, &dentry); } } if (strncmp(dentry.d_name, "..", 30) == 0 && swizzle16(dentry.d_ino) != pnum) { printf("DotDot entry %s%-1.30s points to wrong place. Fix? ", name, dentry.d_name); if (yes()) { dentry.d_ino = swizzle16(pnum); dirwrite(&ino, j, &dentry); } } if (swizzle16(dentry.d_ino) != pnum && swizzle16(dentry.d_ino) != inum && depth < MAXDEPTH) { strcpy(ename, name); strcat(ename, dentry.d_name); strcat(ename, "/"); ckdir(swizzle16(dentry.d_ino), inum, ename); } } --depth; }