int disasm_invariant_modrm( unsigned char *in, unsigned char *out ) { struct modRM_byte modrm; struct SIB_byte sib; unsigned char *c, *cin; unsigned short *s; unsigned int *i; int size = 0; /* modrm byte is already counted */ DecodeByte(*in, &modrm); /* get bitfields */ out[0] = in[0]; /* save modrm byte */ cin = &in[1]; c = &out[1]; s = (unsigned short *)&out[1]; i = (unsigned int *)&out[1]; if ( ! mode_16 && modrm.rm == MODRM_RM_SIB && modrm.mod != MODRM_MOD_NOEA ) { size ++; DecodeByte(*cin, (struct modRM_byte *)&sib); out[1] = in[1]; /* save sib byte */ cin = &in[2]; c = &out[2]; s = (unsigned short *)&out[2]; i = (unsigned int *)&out[2]; if ( sib.base == SIB_BASE_EBP && ! modrm.mod ) { *i = 0xF4F4F4F4; /* pad as variant */ size += 4; } } if (! modrm.mod && modrm.rm == 101) { if ( mode_16 ) { /* straight RVA in disp */ *s = 0xF4F4; /* pad as variant */ size += 2; } else { *i = 0xF4F4F4F4; /* pad as variant */ size += 4; } } else if (modrm.mod && modrm.mod < 3) { if (modrm.mod == MODRM_MOD_DISP8) { /* offset in disp */ *c = *cin; size += 1; } else if ( mode_16 ) { *s = (* ((unsigned short *) cin)); size += 2; } else { *i = (*((unsigned int *) cin)); size += 4; } } return (size); }
uint8_t UnpackData(struct Resp *Data, uint8_t InByte) { uint32_t ERR_CODE = 0; InByte = DecodeByte(InByte); if (InByte == STX) { PROTO_STATE = READ_SQ; ResetPacket(Data); LenSeq = 0; LenC = 0; LenChS = 0; LenD1 = 0; LenD2 = 0; LenD3 = 0; } else { switch(PROTO_STATE){ case READ_SQ: { if(CheckSymb(InByte)) { Data->Seq |= InByte; LenSeq++; if (LenSeq == LEN_SEQ) { PROTO_STATE = READ_FR1; } else { Data->Seq = Data->Seq << HALF_BYTE; } } else{ PROTO_STATE = WAIT_STX; ERR_CODE = BAD_SEQ; } break; } case READ_FR1: { if (InByte == FR1) { PROTO_STATE = READ_COM; } else { ResetPacket(Data); ERR_CODE = NO_FR1; PROTO_STATE = WAIT_STX; } break; } case READ_COM: { //printf("InByte Command: %02X\n", InByte); if(CheckSymb(InByte)) { Data->Command |= InByte; LenC ++; //printf("LenC Command: %d\n", LenC); if (LenC == LEN_COM) { PROTO_STATE = READ_FR2; } else { Data->Command = Data->Command << HALF_BYTE; } } else { ResetPacket(Data); PROTO_STATE = WAIT_STX; ERR_CODE = INV_COMM; } break; } case READ_FR2: { if (InByte == FR2) { PROTO_STATE = READ_DA1; } else { ResetPacket(Data); ERR_CODE = NO_FR2; PROTO_STATE = WAIT_STX; } break; } case READ_DA1: { if(CheckSymb(InByte)) { Data->Data1 |= InByte; LenD1++; if (LenD1 == LEN_DA1) { PROTO_STATE = READ_DA2; } else { Data->Data1 = Data->Data1 << HALF_BYTE; } } else{ ResetPacket(Data); ERR_CODE = INV_DA1; PROTO_STATE = WAIT_STX; } break; } case READ_DA2: { if(CheckSymb(InByte)) { Data->Data2 |= InByte; LenD2++; if (LenD2 == LEN_DA2) { PROTO_STATE = READ_DA3; } else { Data->Data2 = Data->Data2 << HALF_BYTE; } } else{ ResetPacket(Data); ERR_CODE = INV_DA2; PROTO_STATE = WAIT_STX; } break; } case READ_DA3: { if(CheckSymb(InByte)) { Data->Data3 |= InByte; LenD3++; if (LenD3 == LEN_DA3) { PROTO_STATE = READ_FR3; } else { Data->Data3 = Data->Data3 << HALF_BYTE; } } else{ ResetPacket(Data); ERR_CODE = INV_DA3; PROTO_STATE = WAIT_STX; } break; } case READ_FR3: { if (InByte == FR3) { PROTO_STATE = READ_CHS; } else { ResetPacket(Data); ERR_CODE = NO_FR3; PROTO_STATE = WAIT_STX; } break; } case READ_CHS: { if(CheckSymb(InByte)) { Data->CheckSum |= InByte; LenChS++; if (LenChS == LEN_CHS) { PROTO_STATE = READ_ETX; } else { Data->CheckSum = Data->CheckSum << HALF_BYTE; } } else { ResetPacket(Data); ERR_CODE = INV_CKECKSUM; PROTO_STATE = WAIT_STX; } break; } case READ_ETX: { if (InByte == ETX) { uint32_t _tmpcrc; char _tmpdata[40]; sprintf(_tmpdata, "%02X:%08X%08X%04X%04X", Data->Command, (uint32_t)((Data->Data1 & 0xFFFFFFFF00000000) >> 32), (uint32_t)(Data->Data1 & 0x00000000FFFFFFFF), Data->Data2, Data->Data3); _tmpcrc = crc32_reset(); _tmpcrc = crc32_calc_block((uint8_t *)"A", 1, _tmpcrc); _tmpcrc = crc32_calc_block((uint8_t *)_tmpdata, strlen(_tmpdata), _tmpcrc); _tmpcrc = crc32_calc_block((uint8_t *)"F", 1, _tmpcrc); _tmpcrc = crc32_final(_tmpcrc); if (_tmpcrc == Data->CheckSum) { Data->EndPacket = 1; ERR_CODE = PARSED_OK; } else { Data->EndPacket = 0; ERR_CODE = 10; } } else{ ERR_CODE = NO_ETX; ResetPacket(Data); } PROTO_STATE = WAIT_STX; break; } default: { ResetPacket(Data); } }
int readVip(EmbPattern* pattern, const char* fileName) { int fileLength, magicCode, numberOfStitches, numberOfColors; int i, attributeOffset, xOffset, yOffset, unknown, colorLength; unsigned char* stringVal; unsigned char prevByte = 0; int postitiveXHoopSize,postitiveYHoopSize,negativeXHoopSize,negativeYHoopSize; unsigned char *attributeData, *decodedColors, *attributeDataDecompressed; unsigned char *xData, *xDecompressed, *yData, *yDecompressed; FILE* file = fopen(fileName, "rb"); if(file == 0) { /* TODO: set messages here "Error opening VIP file for read:" */ return 0; } fseek(file, 0x0, SEEK_END); fileLength = ftell(file); fseek(file, 0x00, SEEK_SET); magicCode = binaryReadInt32(file); numberOfStitches = binaryReadInt32(file); numberOfColors = binaryReadInt32(file); postitiveXHoopSize = binaryReadInt16(file); postitiveYHoopSize = binaryReadInt16(file); negativeXHoopSize = binaryReadInt16(file); negativeYHoopSize = binaryReadInt16(file); attributeOffset = binaryReadInt32(file); xOffset = binaryReadInt32(file); yOffset = binaryReadInt32(file); stringVal = (unsigned char*)malloc(sizeof(unsigned char)*8); binaryReadBytes(file, stringVal, 8); unknown = binaryReadInt16(file); colorLength = binaryReadInt32(file); decodedColors = (unsigned char *)malloc(numberOfColors*4); for(i = 0; i < numberOfColors*4; ++i) { unsigned char inputByte = binaryReadByte(file); unsigned char tmpByte = (unsigned char) (inputByte ^ vipDecodingTable[i]); decodedColors[i] = (unsigned char) (tmpByte ^ prevByte); prevByte = inputByte; } for(i = 0; i < numberOfColors; i++) { EmbThread thread; int startIndex = i << 2; thread.color.r = decodedColors[startIndex]; thread.color.g = decodedColors[startIndex + 1]; thread.color.b = decodedColors[startIndex + 2]; /* printf("%d\n", decodedColors[startIndex + 3]); */ embPattern_addThread(pattern, thread); } fseek(file, attributeOffset, SEEK_SET); attributeData = (unsigned char *)malloc(xOffset - attributeOffset); binaryReadBytes(file, attributeData, xOffset - attributeOffset); attributeDataDecompressed = DecompressData(attributeData, xOffset - attributeOffset, numberOfStitches); fseek(file, xOffset, SEEK_SET); xData = (unsigned char *)malloc(yOffset - xOffset); binaryReadBytes(file, xData, yOffset - xOffset); xDecompressed = DecompressData(xData, yOffset - xOffset, numberOfStitches); fseek(file, yOffset, SEEK_SET); yData = (unsigned char *)malloc(fileLength - yOffset); binaryReadBytes(file, yData, fileLength - yOffset); yDecompressed = DecompressData(yData, fileLength - yOffset, numberOfStitches); for(i = 0; i < numberOfStitches; i++) { embPattern_addStitchRel(pattern, DecodeByte(xDecompressed[i]) / 10.0, DecodeByte(yDecompressed[i]) / 10.0, DecodeStitchType(attributeDataDecompressed[i]), 1); } embPattern_addStitchRel(pattern, 0, 0, END, 1); fclose(file); return 1; /*TODO: finish readVip */ }