static struct pike_string *save_xbm( struct image *i, struct pike_string *name ) { dynamic_buffer buf; char size[32]; int x, y, first=-1; #define ccat( X ) low_my_binary_strcat( X, (sizeof(X)-sizeof("")), &buf ); #define cname() do{ \ if(name) \ low_my_binary_strcat( name->str, name->len, &buf ); \ else \ ccat( "image" ); \ } while(0) \ #define OUTPUT_BYTE(X) do{ \ if(!++first) \ sprintf( size, " 0x%02x", (X) ); \ else \ sprintf( size, ",%s0x%02x", (first%12?" ":"\n "), (X) ); \ (X)=0; \ low_my_binary_strcat( size, strlen(size), &buf ); \ } while(0) initialize_buf(&buf); ccat( "#define "); cname(); ccat( "_width " ); sprintf( size, "%"PRINTPIKEINT"d\n", i->xsize ); low_my_binary_strcat( size, strlen(size), &buf ); ccat( "#define "); cname(); ccat( "_height " ); sprintf( size, "%"PRINTPIKEINT"d\n", i->ysize ); low_my_binary_strcat( size, strlen(size), &buf ); ccat( "static char " ); cname(); ccat( "_bits[] = {\n" ); for(y=0; y<i->ysize; y++) { rgb_group *p = i->img+y*i->xsize; int next_byte = 0; for(x=0; x<i->xsize; x++) { if((p->r || p->g || p->b )) next_byte |= (1<<(x%8)); if((x % 8) == 7) OUTPUT_BYTE( next_byte ); p++; } if(i->xsize%8) OUTPUT_BYTE( next_byte ); } ccat( "};\n" ); return low_free_buf(&buf); }
int Encoding::writeCode(unsigned char *buffer, bool write) const { // Eliminated instruction if(!emit) return 0; unsigned char *start = buffer; #define OUTPUT_BYTE(x) if(write) {*buffer++ = (x);} else {buffer++;} if(P1 == 0xF1) // Special 'instructions', indicated by INT01 prefix byte { if(O1 == 0x00 && literal) // String constant { for(int i = 0; literal[i]; i++) { OUTPUT_BYTE(literal[i]); } OUTPUT_BYTE('\0'); } else if(O1 == 0x90 && immediate) // ALIGN { if(immediate > 256) { throw Error("ALIGN value too big: %d bytes", immediate); } buffer += align(buffer, immediate, write); } else if((O1 == 0x01 || O1 == 0x02 || O1 == 0x04) && displacement) // Array { for(int i = 0; i < O1 * displacement; i++) { OUTPUT_BYTE(0xCC); // INT3 } } else // Constant { if(format.I1) OUTPUT_BYTE(I1); if(format.I2) OUTPUT_BYTE(I2); if(format.I3) OUTPUT_BYTE(I3); if(format.I4) OUTPUT_BYTE(I4); } } else // Normal instructions { if(format.P1) OUTPUT_BYTE(P1); if(format.P2) OUTPUT_BYTE(P2); if(format.P3) OUTPUT_BYTE(P3); if(format.P4) OUTPUT_BYTE(P4); if(format.O2) OUTPUT_BYTE(O2); if(format.O1) OUTPUT_BYTE(O1); if(format.modRM) OUTPUT_BYTE(modRM.b); if(format.SIB) OUTPUT_BYTE(SIB.b); if(format.D1) OUTPUT_BYTE(D1); if(format.D2) OUTPUT_BYTE(D2); if(format.D3) OUTPUT_BYTE(D3); if(format.D4) OUTPUT_BYTE(D4); if(format.I1) OUTPUT_BYTE(I1); if(format.I2) OUTPUT_BYTE(I2); if(format.I3) OUTPUT_BYTE(I3); if(format.I4) OUTPUT_BYTE(I4); } #undef OUTPUT_BYTE return buffer - start; }