bool CVobSubImage::Decode(BYTE* lpData, int packetsize, int datasize, bool fCustomPal, int tridx, RGBQUAD* orgpal /*[16]*/, RGBQUAD* cuspal /*[4]*/, bool fTrim) { GetPacketInfo(lpData, packetsize, datasize); if(!Alloc(rect.Width(), rect.Height())) { return(false); } lpPixels = lpTemp1; nPlane = 0; fAligned = 1; this->fCustomPal = fCustomPal; this->orgpal = orgpal; this->tridx = tridx; this->cuspal = cuspal; CPoint p(rect.left, rect.top); int end0 = nOffset[1]; int end1 = datasize; while((nPlane == 0 && nOffset[0] < end0) || (nPlane == 1 && nOffset[1] < end1)) { DWORD code; if((code = GetNibble(lpData)) >= 0x4 || (code = (code << 4) | GetNibble(lpData)) >= 0x10 || (code = (code << 4) | GetNibble(lpData)) >= 0x40 || (code = (code << 4) | GetNibble(lpData)) >= 0x100) { DrawPixels(p, code >> 2, code & 3); if((p.x += code >> 2) < rect.right) { continue; } } DrawPixels(p, rect.right - p.x, code & 3); if(!fAligned) { GetNibble(lpData); // align to byte } p.x = rect.left; p.y++; nPlane = 1 - nPlane; }
/** * Puts a packet to the head of the buffer. * @params packet The data of the packet to put. * @returns whether the operation is successful. */ bool Put(const std::vector<u8>& packet) { if (info.packet_count == max_packet_count) return false; u32 write_offset; // finds free space offset in data buffer if (info.packet_count == 0) { write_offset = 0; if (packet.size() > max_data_size) return false; } else { const u32 last_index = (info.end_index + max_packet_count - 1) % max_packet_count; const PacketInfo first = GetPacketInfo(info.begin_index); const PacketInfo last = GetPacketInfo(last_index); write_offset = (last.offset + last.size) % max_data_size; const u32 free_space = (first.offset + max_data_size - write_offset) % max_data_size; if (packet.size() > free_space) return false; } // writes packet info PacketInfo packet_info{write_offset, static_cast<u32>(packet.size())}; SetPacketInfo(info.end_index, packet_info); // writes packet data for (std::size_t i = 0; i < packet.size(); ++i) { *GetDataBufferPointer((write_offset + i) % max_data_size) = packet[i]; } // updates buffer info info.end_index++; info.end_index %= max_packet_count; info.packet_count++; UpdateBufferInfo(); return true; }
/* ************************************************************************* * PortReadyForWrite ************************************************************************* * * Called when COM port is ready for another write packet. * Send the first frame in the send queue. * * Return TRUE iff send succeeded. * * NOTE: Do not call inside of interrupt context. * */ BOOLEAN PortReadyForWrite(IrDevice *thisDev, BOOLEAN isSynchronousSend) { BOOLEAN sendSucceeded = FALSE; DBGOUT(("PortReadyForWrite(dev=0x%x, %xh, %s)", (UINT)thisDev, thisDev->portInfo.ioBase, (CHAR *)(isSynchronousSend ? "sync" : "async"))); if (thisDev->firstSendPacket){ PNDIS_PACKET packetToSend; PNDIS_IRDA_PACKET_INFO packetInfo; /* * Dequeue the first send packet and step the send queue. * (We use the packet's MiniportReserved field is a 'next' pointer). */ packetToSend = thisDev->firstSendPacket; thisDev->firstSendPacket = *(PNDIS_PACKET *)thisDev->firstSendPacket->MiniportReserved; if (!thisDev->firstSendPacket){ thisDev->lastSendPacket = NULL; } *(PNDIS_PACKET *)packetToSend->MiniportReserved = NULL; /* * Enforce the minimum turnaround time that must transpire * after the last receive. */ packetInfo = GetPacketInfo(packetToSend); if (packetInfo->MinTurnAroundTime){ /* * Don't want to call NdisStallExecution with more than * 8 msec or it will cause a task switch. * Make a series of calls with 8 msec or less. */ UINT usecToWait = packetInfo->MinTurnAroundTime; do { UINT usec = (usecToWait > 8000) ? 8000 : usecToWait; NdisStallExecution(usec); usecToWait -= usec; } while (usecToWait > 0); } /* * See if this was the last packet before we need to change speed. */ if (packetToSend == thisDev->lastPacketAtOldSpeed){ thisDev->lastPacketAtOldSpeed = NULL; thisDev->setSpeedAfterCurrentSendPacket = TRUE; } /* * Send one packet to the COMM port. */ DBGPKT(("Sending packet 0x%x (0x%x).", thisDev->packetsSent++, (UINT)packetToSend)); sendSucceeded = DoSend(thisDev, packetToSend); /* * If the buffer we just sent was pending * (i.e. we returned NDIS_STATUS_PENDING for it in MiniportSend), * then hand the sent packet back to the protocol. * Otherwise, we're just delivering it synchronously from MiniportSend. */ if (!isSynchronousSend){ DBGOUT(("Calling NdisMSendComplete")); NdisMSendComplete(thisDev->ndisAdapterHandle, packetToSend, (NDIS_STATUS)(sendSucceeded ? NDIS_STATUS_SUCCESS : NDIS_STATUS_FAILURE)); } } DBGOUT(("PortReadyForWrite done.")); return sendSucceeded; }
VOID ProcessPacket(SYSTEMTIME rTime, unsigned char *szBuffer, int size, BOOL bFilter) { struct ethernet_802_3 *eth; struct iphdr *ip; struct arppkt *arp; int size_iphdr, size_framehdr, index; RuleNode *alert_node, *log_node, *counter_node; BOOL bMatch = FALSE; char src[32], dst[32], type[64]; char info[64], str[128], msg[128], numbytes[12]; unsigned short ptype; if (size < 42) // probably a corrupt packet return; size_framehdr = 14; src[0] = '\0'; dst[0] = '\0'; info[0] = '\0'; msg[0] = '\0'; wsprintf(numbytes, "%d", size); wsprintf(type, "Unknown"); eth = (struct ethernet_802_3 *) szBuffer; if (ntohs(eth->length) > 0x05DC) // its an ethernet_II frame { ptype = eth->length; /* length actually is type since its an eth_II frame */ size_framehdr = 14; } else // its an 802.3 frame { ptype = eth->type; size_framehdr = 22; } ptype = ntohs(ptype); /* if rules are NOT to be used, show all captured packets */ if (bFilter == FALSE) { GetPacketInfo(ptype, szBuffer, size_framehdr, src, dst, type, info); AddToAlert(rTime, szBuffer, size, src, dst, numbytes, type, info, ""); return; } if (ptype == 0x0800) { ip = (struct iphdr *) &szBuffer[size_framehdr]; size_iphdr = (ip->verlen & 0x0f) * 4; switch (ip->prot) { case R_ICMP: alert_node = root.alert.IcmpList; log_node = root.log.IcmpList; counter_node = root.counter.IcmpList; break; case R_UDP: alert_node = root.alert.UdpList; log_node = root.log.UdpList; counter_node = root.counter.UdpList; break; case R_TCP: alert_node = root.alert.TcpList; log_node = root.log.TcpList; counter_node = root.counter.TcpList; break; default: return; } /* check for match in the alert chain */ while (alert_node != NULL) { debug("checking alert node\r\n"); bMatch = MatchIpFields(alert_node, &szBuffer[size_framehdr]); if (bMatch) debug("ip header matches\r\n"); if (bMatch && alert_node->content_set) bMatch = MatchContent(&szBuffer[size_iphdr], size-size_iphdr, alert_node->content, lstrlen(alert_node->content)); if (bMatch) { GetPacketInfo(ptype, szBuffer, size_framehdr, src, dst, type, info); AddToAlert(rTime, szBuffer, size, src, dst, numbytes, type, info, alert_node->msg); break; } alert_node = alert_node->next; } /* check for match in the log chain */ while (log_node != NULL) { bMatch = MatchIpFields(log_node, &szBuffer[size_framehdr]); if (bMatch && log_node->content_set) bMatch = MatchContent(&szBuffer[size_iphdr], size-size_iphdr, log_node->content, lstrlen(log_node->content)); if (bMatch) { AddToLog(rTime, szBuffer, size, log_node->msg); break; } log_node = log_node->next; } /* check for all matches in the counter chain */ while (counter_node != NULL) { bMatch = MatchIpFields(counter_node, &szBuffer[size_framehdr]); if (bMatch && counter_node->content_set) bMatch = MatchContent(&szBuffer[size_iphdr], size-size_iphdr, counter_node->content, lstrlen(counter_node->content)); if (bMatch && counter_node->counter_id_set) { index = counter_node->counter_id; counter[index].count += 1; counter[index].bytes += size; wsprintf(str, "%d", counter[index].count); ListView_SetItemText(hWndCounterList, index, 2, str); FormatByteValue(counter[index].bytes, str); ListView_SetItemText(hWndCounterList, index, 3, str); } counter_node = counter_node->next; } } /* SPECIAL HANDLING FOR ARP PACKETS */ else if (ptype == 0x0806) { alert_node = root.alert.ArpList; if (alert_node != NULL) { GetPacketInfo(ptype, szBuffer, size_framehdr, src, dst, type, info); AddToAlert(rTime, szBuffer, size, src, dst, numbytes, type, info, alert_node->msg); } log_node = root.log.ArpList; if (log_node != NULL) { AddToLog(rTime, szBuffer, size, log_node->msg); } /* increment counter while here */ alert_node = root.counter.ArpList; if ((alert_node != NULL) && alert_node->counter_id_set) { index = alert_node->counter_id; counter[index].count += 1; counter[index].bytes += size; wsprintf(str, "%d", counter[index].count); ListView_SetItemText(hWndCounterList, index, 2, str); FormatByteValue(counter[index].bytes, str); ListView_SetItemText(hWndCounterList, index, 3, str); } arp = (struct arppkt *) &szBuffer[size_framehdr]; wsprintf(dst, "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X", arp->sender_ha[0], arp->sender_ha[1],arp->sender_ha[2],arp->sender_ha[3],arp->sender_ha[4],arp->sender_ha[5]); wsprintf(src, "%d.%d.%d.%d", arp->sender_ip[0], arp->sender_ip[1], arp->sender_ip[2], arp->sender_ip[3]); AddToARP(hWndARPList, src, dst); } }