cload.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <draw.h>
  12. #include <memdraw.h>
  13. int
  14. cloadmemimage(Memimage *i, Rectangle r, uint8_t *data, int ndata)
  15. {
  16. int y, bpl, c, cnt, offs;
  17. uint8_t mem[NMEM], *memp, *omemp, *emem, *linep, *elinep, *u, *eu;
  18. if(!rectinrect(r, i->r))
  19. return -1;
  20. bpl = bytesperline(r, i->depth);
  21. u = data;
  22. eu = data+ndata;
  23. memp = mem;
  24. emem = mem+NMEM;
  25. y = r.min.y;
  26. linep = byteaddr(i, Pt(r.min.x, y));
  27. elinep = linep+bpl;
  28. for(;;){
  29. if(linep == elinep){
  30. if(++y == r.max.y)
  31. break;
  32. linep = byteaddr(i, Pt(r.min.x, y));
  33. elinep = linep+bpl;
  34. }
  35. if(u == eu){ /* buffer too small */
  36. return -1;
  37. }
  38. c = *u++;
  39. if(c >= 128){
  40. for(cnt=c-128+1; cnt!=0 ;--cnt){
  41. if(u == eu){ /* buffer too small */
  42. return -1;
  43. }
  44. if(linep == elinep){ /* phase error */
  45. return -1;
  46. }
  47. *linep++ = *u;
  48. *memp++ = *u++;
  49. if(memp == emem)
  50. memp = mem;
  51. }
  52. }
  53. else{
  54. if(u == eu) /* short buffer */
  55. return -1;
  56. offs = *u++ + ((c&3)<<8)+1;
  57. if(memp-mem < offs)
  58. omemp = memp+(NMEM-offs);
  59. else
  60. omemp = memp-offs;
  61. for(cnt=(c>>2)+NMATCH; cnt!=0; --cnt){
  62. if(linep == elinep) /* phase error */
  63. return -1;
  64. *linep++ = *omemp;
  65. *memp++ = *omemp++;
  66. if(omemp == emem)
  67. omemp = mem;
  68. if(memp == emem)
  69. memp = mem;
  70. }
  71. }
  72. }
  73. return u-data;
  74. }