static void write_encoded(FILE * fbin, ucell * c, int num) { assert(sizeof(cell) <= 4); /* code must be adjusted for larger cells */ assert(fbin != NULL); while (num-- > 0) { if (sc_compress) { ucell p = (ucell) * c; unsigned char t[5]; /* a 32-bit cell is encoded in max. 5 bytes (3 bytes for a 16-bit cell) */ unsigned char code; int idx; for (idx = 0; idx < 5; idx++) { t[idx] = (unsigned char)(p & 0x7f); /* store 7 bits */ p >>= 7; } /* for */ /* skip leading zeros */ while (idx > 1 && t[idx - 1] == 0 && (t[idx - 2] & 0x40) == 0) idx--; /* skip leading -1s *//* ??? for BIT16, check for idx==3 && t[idx-1]==0x03 */ if (idx == 5 && t[idx - 1] == 0x0f && (t[idx - 2] & 0x40) != 0) idx--; while (idx > 1 && t[idx - 1] == 0x7f && (t[idx - 2] & 0x40) != 0) idx--; /* write high byte first, write continuation bits */ assert(idx > 0); while (idx-- > 0) { code = (unsigned char)((idx == 0) ? t[idx] : (t[idx] | 0x80)); writeerror |= !sc_writebin(fbin, &code, 1); bytes_out++; } /* while */ bytes_in += sizeof *c; assert(AMX_EXPANDMARGIN > 2); if (bytes_out - bytes_in >= AMX_EXPANDMARGIN - 2) error(106); /* compression buffer overflow */ } else { assert((sc_lengthbin(fbin) % sizeof(cell)) == 0); writeerror |= !sc_writebin(fbin, aligncell(c), sizeof *c); } /* if */ c++; } /* while */
static void write_encoded(FILE *fbin,ucell *c,int num) { #if PAWN_CELL_SIZE == 16 #define ENC_MAX 3 /* a 16-bit cell is encoded in max. 3 bytes */ #define ENC_MASK 0x03 /* after 2x7 bits, 2 bits remain to make 16 bits */ #elif PAWN_CELL_SIZE == 32 #define ENC_MAX 5 /* a 32-bit cell is encoded in max. 5 bytes */ #define ENC_MASK 0x0f /* after 4x7 bits, 4 bits remain to make 32 bits */ #elif PAWN_CELL_SIZE == 64 #define ENC_MAX 10 /* a 32-bit cell is encoded in max. 10 bytes */ #define ENC_MASK 0x01 /* after 9x7 bits, 1 bit remains to make 64 bits */ #endif assert(fbin!=NULL); while (num-->0) { if (pc_compress) { ucell p=(ucell)*c; unsigned char t[ENC_MAX]; unsigned char code; int index; for (index=0; index<ENC_MAX; index++) { t[index]=(unsigned char)(p & 0x7f); /* store 7 bits */ p>>=7; } /* for */ /* skip leading zeros */ while (index>1 && t[index-1]==0 && (t[index-2] & 0x40)==0) index--; /* skip leading -1s */ if (index==ENC_MAX && t[index-1]==ENC_MASK && (t[index-2] & 0x40)!=0) index--; while (index>1 && t[index-1]==0x7f && (t[index-2] & 0x40)!=0) index--; /* write high byte first, write continuation bits */ assert(index>0); while (index-->0) { code=(unsigned char)((index==0) ? t[index] : (t[index]|0x80)); writeerror |= !pc_writebin(fbin,&code,1); bytes_out++; } /* while */ bytes_in+=sizeof *c; assert(AMX_COMPACTMARGIN>2); if (bytes_out-bytes_in>=AMX_COMPACTMARGIN-2) longjmp(compact_err,1); } else { assert((pc_lengthbin(fbin) % sizeof(cell)) == 0); writeerror |= !pc_writebin(fbin,aligncell(c),sizeof *c); } /* if */ c++; } /* while */
static void write_cell(FILE *fbin,ucell c) { assert(fbin!=NULL); assert((pc_lengthbin(fbin) % pc_cellsize) == 0); if (pc_cryptkey!=0) { uint32_t *ptr=(uint32_t*)&c; assert(pc_cellsize==4 || pc_cellsize==8); *ptr=KeeLoq_Encrypt(*ptr,pc_cryptkey); if (pc_cellsize==8) { ptr++; *ptr=KeeLoq_Encrypt(*ptr,pc_cryptkey); } /* if */ } /* if */ writeerror |= !pc_writebin(fbin,aligncell(&c),pc_cellsize); }