bool Ublox::process_buf() { if(!check_checksum()) //if checksum is bad { return false; //return } //otherwise, what sort of message is it if(strncmp(buf, "$GNGGA", 6) == 0) { read_gga(); } if(strncmp(buf, "$GNGSA", 6) == 0) { read_gsa(); } if(strncmp(buf, "$GPGSV", 6) == 0) { read_gsv(); } if(strncmp(buf, "$GNRMC", 6) == 0) { read_rmc(); } if(strncmp(buf, "$GNVTG", 6) == 0) { read_vtg(); } return true; }
static int parse_gre_header(struct sk_buff *skb, struct tnl_ptk_info *tpi, bool *csum_err) { unsigned int ip_hlen = ip_hdrlen(skb); const struct gre_base_hdr *greh; __be32 *options; int hdr_len; if (unlikely(!pskb_may_pull(skb, sizeof(struct gre_base_hdr)))) return -EINVAL; greh = (struct gre_base_hdr *)(skb_network_header(skb) + ip_hlen); if (unlikely(greh->flags & (GRE_VERSION | GRE_ROUTING))) return -EINVAL; tpi->flags = gre_flags_to_tnl_flags(greh->flags); hdr_len = ip_gre_calc_hlen(tpi->flags); if (!pskb_may_pull(skb, hdr_len)) return -EINVAL; greh = (struct gre_base_hdr *)(skb_network_header(skb) + ip_hlen); tpi->proto = greh->protocol; options = (__be32 *)(greh + 1); if (greh->flags & GRE_CSUM) { if (check_checksum(skb)) { *csum_err = true; return -EINVAL; } options++; } if (greh->flags & GRE_KEY) { tpi->key = *options; options++; } else tpi->key = 0; if (unlikely(greh->flags & GRE_SEQ)) { tpi->seq = *options; options++; } else tpi->seq = 0; /* WCCP version 1 and 2 protocol decoding. * - Change protocol to IP * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header */ if (greh->flags == 0 && tpi->proto == htons(ETH_P_WCCP)) { tpi->proto = htons(ETH_P_IP); if ((*(u8 *)options & 0xF0) != 0x40) { hdr_len += 4; if (!pskb_may_pull(skb, hdr_len)) return -EINVAL; } } return iptunnel_pull_header(skb, hdr_len, tpi->proto); }
/** * Validate a given file service s. Events are posted according to * its configuration. In case of a fatal event FALSE is returned. */ int check_file(Service_T s) { struct stat stat_buf; ASSERT(s); if (stat(s->path, &stat_buf) != 0) { Event_post(s, Event_Nonexist, STATE_FAILED, s->action_NONEXIST, "file doesn't exist"); return FALSE; } else { s->inf->st_mode = stat_buf.st_mode; if (s->inf->priv.file.st_ino == 0) { s->inf->priv.file.st_ino_prev = stat_buf.st_ino; s->inf->priv.file.readpos = stat_buf.st_size; } else s->inf->priv.file.st_ino_prev = s->inf->priv.file.st_ino; s->inf->priv.file.st_ino = stat_buf.st_ino; s->inf->st_uid = stat_buf.st_uid; s->inf->st_gid = stat_buf.st_gid; s->inf->priv.file.st_size = stat_buf.st_size; s->inf->timestamp = MAX(stat_buf.st_mtime, stat_buf.st_ctime); DEBUG("'%s' file exists check succeeded\n", s->name); Event_post(s, Event_Nonexist, STATE_SUCCEEDED, s->action_NONEXIST, "file exist"); } if (!S_ISREG(s->inf->st_mode)) { Event_post(s, Event_Invalid, STATE_FAILED, s->action_INVALID, "is not a regular file"); return FALSE; } else { DEBUG("'%s' is a regular file\n", s->name); Event_post(s, Event_Invalid, STATE_SUCCEEDED, s->action_INVALID, "is a regular file"); } if (s->checksum) check_checksum(s); if (s->perm) check_perm(s); if (s->uid) check_uid(s); if (s->gid) check_gid(s); if (s->sizelist) check_size(s); if (s->timestamplist) check_timestamp(s); if (s->matchlist) check_match(s); return TRUE; }
Datum isa_gtin(PG_FUNCTION_ARGS) { char *str = GET_TEXT_STR( PG_GETARG_TEXT_P(0) ); char *norm = normalize_gtin( str ); int len = strlen( str ); if (!len) PG_RETURN_BOOL( 0 ); len = strlen( norm ); if (!len || len > MAXGTINLEN - 1) PG_RETURN_BOOL( 0 ); PG_RETURN_BOOL( check_checksum( norm ) ); }
/* Called with rcu_read_lock and BH disabled. */ static int gre_rcv(struct sk_buff *skb) { struct ovs_net *ovs_net; struct vport *vport; int hdr_len; struct iphdr *iph; struct ovs_key_ipv4_tunnel tun_key; __be16 gre_flags; u32 tnl_flags; __be64 key; bool is_gre64; if (unlikely(!pskb_may_pull(skb, sizeof(struct gre_base_hdr) + ETH_HLEN))) goto error; if (unlikely(!check_checksum(skb))) goto error; hdr_len = parse_header(ip_hdr(skb), &gre_flags, &key, &is_gre64); if (unlikely(hdr_len < 0)) goto error; ovs_net = net_generic(dev_net(skb->dev), ovs_net_id); if (is_gre64) vport = rcu_dereference(ovs_net->vport_net.gre64_vport); else vport = rcu_dereference(ovs_net->vport_net.gre_vport); if (unlikely(!vport)) goto error; if (unlikely(!pskb_may_pull(skb, hdr_len + ETH_HLEN))) goto error; iph = ip_hdr(skb); tnl_flags = gre_flags_to_tunnel_flags(gre_flags, is_gre64); tnl_tun_key_init(&tun_key, iph, key, tnl_flags); OVS_CB(skb)->tun_key = &tun_key; __skb_pull(skb, hdr_len); skb_postpull_rcsum(skb, skb_transport_header(skb), hdr_len + ETH_HLEN); ovs_tnl_rcv(vport, skb); return 0; error: kfree_skb(skb); return 0; }
Datum str_to_gtin (char *str) { int len = strlen( str ); char *result = normalize_gtin( str ); if ( !len ) { // A GTIN cannot be empty. ereport( ERROR, ( errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("GTIN cannot be empty") )); } len = strlen( result ); if ( !len ) { // normalize_gtin() returns empty if it has invalid characters. ereport( ERROR, ( errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("Invalid characters in GTIN \"%s\"", str) )); } if (len > MAXGTINLEN - 1) { // A GTIN cannot be more than 18 characters. ereport( ERROR, ( errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg( "GTIN cannot be more than %d characters: \"%s\"", MAXGTINLEN - 1, str ) )); } if ( !check_checksum( result ) ) { // check_checksum() returns false if the checksum is invalid. ereport( ERROR, ( errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("Invalid checksum for GTIN \"%s\"", str) )); } // Make sure that the string is the proper length and return it. repalloc( result, MAXGTINLEN ); PG_RETURN_POINTER( result ); }
/** * Validate a given file service s. Events are posted according to * its configuration. In case of a fatal event FALSE is returned. */ int check_file(Service_T s) { Size_T sl; struct stat stat_buf; char report[STRLEN]= {0}; if(stat(s->path, &stat_buf) != 0) { if(! s->event_handled) { Event_post(s, EVENT_START, "Event: file '%s' doesn't exist\n", s->name); s->event_handled= TRUE; } return FALSE; } else { s->event_handled= TRUE; } if(!S_ISREG(stat_buf.st_mode)) { if(! s->event_handled) { Event_post(s, EVENT_UNMONITOR, "Event: '%s' is not regular file\n", s->name); s->event_handled= TRUE; } return FALSE; } else { s->event_handled= FALSE; } if(check_checksum(s, report)) { s->checksum->event_flag= TRUE; if(! eval_actions(s->checksum->action, s, report, "checksum", EVENT_CHECKSUM)) return FALSE; } if(check_perm(s, stat_buf.st_mode, report)) { s->perm->event_flag= TRUE; if(! eval_actions(s->perm->action, s, report, "permission", EVENT_PERMISSION)) return FALSE; } if(check_uid(s, stat_buf.st_uid, report)) { s->uid->event_flag= TRUE; if(! eval_actions(s->uid->action, s, report, "uid", EVENT_UID)) return FALSE; } if(check_gid(s, stat_buf.st_gid, report)) { s->gid->event_flag= TRUE; if(! eval_actions(s->gid->action, s, report, "gid", EVENT_GID)) return FALSE; } for(sl= s->sizelist; sl; sl= sl->next) { if(!check_size_item(s, sl, (unsigned long)stat_buf.st_size, report)) { if(! sl->event_handled) { /* Turn on the object's event_flag to indicate that the size event * occured on this particular object */ sl->event_flag= TRUE; sl->event_handled= TRUE; /* Turn on the object's * event_handled flag so this * test is not handled in * subsequent poll cycles until * the error condition was reset. */ if(! eval_actions(sl->action, s, report, "size", EVENT_SIZE)) { return FALSE; } } } else { sl->event_handled= FALSE; } } if(!check_timestamps(s)) return FALSE; return TRUE; }
static msg_t GPSThread(void *arg) { (void)arg; chRegSetThreadName("gps_thread"); if (gps_data != NULL) { chHeapFree(gps_data); } gps_data = chHeapAlloc(NULL, GPS_CMD_BUF); size_t gps_bytes_read; uint16_t i; if (gps_data == NULL) { while (TRUE) { palTogglePad(GPIO_LED_1_PORT, GPIO_LED_1_PIN); chThdSleepMilliseconds(50); } } sdStart(&GPS_SERIAL, &SD3_Config); palSetPadMode(GPS_USART_PORT, GPS_USART_TX_PIN, PAL_MODE_ALTERNATE(7)); palSetPadMode(GPS_USART_PORT, GPS_USART_RX_PIN, PAL_MODE_ALTERNATE(7)); gps_reset(); while (TRUE) { size_t readed_msg_len = 0; uint8_t res = gps_read_msg(&readed_msg_len); if (res == E_OK) { if (check_checksum() != E_OK) { sdWrite(&SD1, "GPS CHK ERR\r\n", 13); } else { if (gps_message_type() == GPS_MESSAGE_GPRMC) { gps_rmc_state_t state; //sdWrite(&SD1, gps_data, readed_msg_len); if (parse_gps_rmc(&state) == E_OK) { sdWrite(&SD1, "GPS PARSE OK\r\n", 14); } } else if (gps_message_type() == GPS_MESSAGE_UNKNOWN) { sdWrite(&SD1, "GPS MSG UNKNOWN\r\n", 17); } } } else { gps_data[0] = '0' + res; sdWrite(&SD1, "GPS ERROR:", 10); sdWrite(&SD1, gps_data, 1); gps_reset(); } sdWrite(&SD1, "\r\n", 2); //sdWrite(&SD1, "GPS: ", 5); //while ((gps_bytes_read = sdReadTimeout(&GPS_SERIAL, gps_data, GPS_CMD_BUF, 100)) > 0) // sdWrite(&SD1, gps_data, gps_bytes_read); //sdWrite(&SD1, "|||\r\n", 5); //chThdSleepMilliseconds(500); } chHeapFree(gps_data); }
void sr_handlepacket(struct sr_instance* sr, uint8_t * packet/* lent */, unsigned int len, char* interface/* lent */) { /* REQUIRES */ assert(sr); assert(packet); assert(interface); printf("*** -> Received packet of length %d \n",len); sr_ethernet_hdr_t* eth_header = (sr_ethernet_hdr_t*)packet; /* printf("The type is %x\n",ntohs(eth_header->ether_type));*/ if( ntohs(eth_header->ether_type) == IP_PROTOCOL) { /* Its an IP Packet */ printf("Its an IP Packet\n"); /* Check the checksum of the packet */ uint8_t* ip_packet = packet + sizeof(sr_ethernet_hdr_t); if(check_checksum(ip_packet,sizeof(sr_ip_hdr_t)) == 0) { /* Handle ICMP Error message */ return; } else { /* Checksum is fine, Now do the functions */ sr_ip_hdr_t* ip_header = (sr_ip_hdr_t*)ip_packet; /*print_hdr_ip(ip_packet);*/ if( decrement_TTL(ip_header) == 0 ) { /* Send ICMP packet for Time Limit Exceeded*/ ip_header->ip_sum = 0; ip_header->ip_ttl = 1; ip_header->ip_sum = cksum(ip_packet,sizeof(sr_ip_hdr_t)); /*make_ICMP_packet(); make_IP_packet();*/ /* Check wheather the Destination is this router */ if( check_match_all_interfaces ( sr->if_list, ip_header->ip_dst) == 1 ) { printf("I am here in TTL 0 \n\n\\n"); uint8_t protocol = ip_header->ip_p; if( protocol == TCP_PROTOCOL || protocol == UDP_PROTOCOL) { /* This is having UDP or TCP as payload */ /* So, just include this packet as payload and send the ICMP packet */ handle_ICMP(sr,ip_packet,len-sizeof(sr_ethernet_hdr_t),3,3,interface,1); } else { /* Its ICMP protocol */ /* Check if it is echo request , If it is then send echo reply */ sr_icmp_hdr_t* icmp = (sr_icmp_hdr_t*)(ip_packet+sizeof(sr_ip_hdr_t)); if( icmp->icmp_type == 8 && icmp->icmp_code == 0) { int length = len - sizeof(sr_ethernet_hdr_t); uint8_t* data = 0; uint8_t* icmp_start = make_ICMP_packet_echo(ip_packet+sizeof(sr_ip_hdr_t),length-sizeof(sr_ip_hdr_t)); uint8_t* ip_start = make_IP_packet( *((uint8_t*)ip_header), ip_header->ip_tos, htons(length), \ ip_header->ip_id,ip_header->ip_off, 64, ip_header->ip_p,\ ip_header->ip_dst, ip_header->ip_src); uint8_t* final_packet = (uint8_t*)malloc(len); memcpy(final_packet,ip_start,sizeof(sr_ip_hdr_t)); memcpy(final_packet + sizeof(sr_ip_hdr_t),icmp_start,\ length-sizeof(sr_ip_hdr_t)); free(ip_start); free(icmp_start); forward_packet(sr,final_packet,length,interface); } else handle_ICMP(sr,ip_packet,len-sizeof(sr_ethernet_hdr_t),11,0,interface,1); } } else { handle_ICMP(sr,ip_packet,len-sizeof(sr_ethernet_hdr_t),11,0,interface,0); } return; } /* Calculate new checksum after TTL updation */ ip_header->ip_sum = 0; ip_header->ip_sum = cksum(ip_packet,sizeof(sr_ip_hdr_t)); /*print_hdr_ip(ip_packet);*/ /* Check if the packet is destined to any of its interfaces */ /* Dont free this */ if( check_match_all_interfaces ( sr->if_list, ip_header->ip_dst) == 1 ) { /* Now its destined to router, so router should handle it It Should check for the Transport layer protocol and should appropriately send ICMP packets*/ router_handle_packet(sr,ip_packet,len-sizeof(sr_ethernet_hdr_t),interface); } else { /* Packet is subjected to NAT translation first and then forwarded 1) Check if there are any existing sessions for this packet, if any use them to translate the packets 2) If there aren't any, then allocate a new session and translate the packet. */ forward_packet(sr,ip_packet,len,interface); } } } else { printf("Its an ARP Packet\n"); /* Its an ARP Packet */ uint8_t* arp_packet = packet + sizeof(sr_ethernet_hdr_t); /* Construct ARP structure */ sr_arp_hdr_t* arp_header = (sr_arp_hdr_t*)arp_packet; /* Check if the packet is request to the router */ int dst_ip = ntohl(arp_header->ar_tip); struct sr_if* arp_interface = sr_get_interface(sr,interface); int interface_ip = ntohl(arp_interface->ip); if( dst_ip == interface_ip ) { /* It is destined correctly */ uint8_t op_code = ntohs(arp_header->ar_op); if( op_code == 1 ) { /* ARP Request */ uint8_t* arp_reply_packet; arp_reply_packet = (uint8_t*)malloc(sizeof(sr_ethernet_hdr_t)+1500); bzero(arp_reply_packet,sizeof(sr_ethernet_hdr_t)+1500); /* Create both ethernet and ARP structures */ sr_ethernet_hdr_t* new_eth = (sr_ethernet_hdr_t*)arp_reply_packet; /* Fill in all the details of ethernet frame */ memcpy(new_eth->ether_dhost,eth_header->ether_shost,sizeof(uint8_t)*6); memcpy(new_eth->ether_shost,arp_interface->addr,sizeof(uint8_t)*6); new_eth->ether_type = htons(ARP_PROTOCOL); /* Fill in all the details of ARP */ uint8_t* arp_reply_segment = arp_reply_packet+ sizeof(sr_ethernet_hdr_t); memcpy(arp_reply_segment,arp_packet,sizeof(sr_arp_hdr_t)); sr_arp_hdr_t* arp_reply_structure = (sr_arp_hdr_t*)arp_reply_segment; arp_reply_structure->ar_op = htons(2); memcpy(arp_reply_structure->ar_sha,arp_interface->addr,sizeof(uint8_t)*6); memcpy(arp_reply_structure->ar_tha,eth_header->ether_shost,sizeof(uint8_t)*6); arp_reply_structure->ar_sip = arp_interface->ip; arp_reply_structure->ar_tip = arp_header->ar_sip; /* Now send the packet */ /* Beaware of the size of the frame, it should not be sizeof(arp_reply_packet) But the size used below */ sr_send_packet(sr,arp_reply_packet,sizeof(sr_ethernet_hdr_t)+1500,interface); } else if( op_code == 2 ) { /* ARP Reply */ uint8_t* MAC = (uint8_t*)malloc(sizeof(uint8_t)*6); uint32_t IP; memcpy(MAC,arp_header->ar_sha,sizeof(uint8_t)*6); IP = arp_header->ar_sip; struct sr_arpreq* queue = sr_arpcache_insert(&sr->cache,MAC,IP); if( queue == NULL ) { assert(queue!=NULL); } else { struct sr_packet* packet_i = queue->packets; while( packet_i!= NULL ) { sr_ethernet_hdr_t* head = (sr_ethernet_hdr_t*)(packet_i->buf); memcpy(head->ether_dhost,MAC,sizeof(uint8_t)*6); sr_send_packet(sr,packet_i->buf,packet_i->len,packet_i->iface); packet_i = packet_i->next; } sr_arpreq_destroy(&sr->cache,queue); } } } } }/* end sr_ForwardPacket */
int main() { char unlock_code_str [50]; char input_c; char box_id_str [50]; char filename_str [50]; uint32_t unlock_code = 0; uint8_t i; uint32_t blah; FILE* unlock_code_file; //Clear screen and print header #ifdef __WIN32__ system("cls"); #else //system("clear"); #endif printf("Generate and tests the unlock codes for the stand alone battery box \n\r"); printf("____________\r\n"); //Read in the Box ID do { box_id_str[0] = '\0'; while( strlen(box_id_str) <= 1 ) //Checks for nothing entered (endline counts as 1 char) { printf("Please enter the box ID ( 0 -> 16383 ): \n\r"); fgets( box_id_str, 256, stdin); } } while ( set_box_id( atoi(box_id_str) ) ); printf("What do you want to do? Press 'g' to generate codes, 'p' for php testing, 't' to test codes\n\r"); input_c = getchar(); if (input_c == 'g') { //Generate the filename for the output file and open it for writing box_id_str[strcspn ( box_id_str, "\n" )] = '\0'; //Removes the newline strcpy(filename_str, box_id_str); //Copies the box_id into the filename strcat(filename_str, "_unlock_codes.txt"); //Adds the second part and file extension printf("Writing to file: %s\n\r", filename_str); //Prints out update unlock_code_file = fopen(filename_str, "w"); //Opens the file for writing //Generate codes for given box_id, each unlocks the box for 2 Weeks SET_UNLOCK_DAYS(unlock_code, TWO_WEEKS); if (unlock_code_file) { //Generate 51 unlock codes for (i = 0; i < 51; i++){ SET_UNLOCK_COUNT(unlock_code, i); SET_PAD( unlock_code, rand() ) add_checksum(&unlock_code); fprintf(unlock_code_file, "Code %03i: %010u \n\r", i, unlock_code); } //Generate a final full unlock code SET_UNLOCK_DAYS(unlock_code, FULL_UNLOCK); add_checksum(&unlock_code); fprintf(unlock_code_file, "Full unlock code: %010u \n\r", unlock_code); fclose(unlock_code_file); printf("%i unlock codes have been written to the output file \n\r", (i+1) ); } else { printf("The file could not be opened \n\r"); } } else if (input_c == 'p') { blah = 0x0000D77C; get_checksum(blah); //(uint8_t*)& //Fletcher16((uint8_t*)&blah, 6); } else { while (1) { //Read in the unlock code unlock_code_str[0] = '\0'; //Checks for nothing entered (endline counts as 1 char) while( strlen(unlock_code_str) <= 1 ) { printf("\r\nPlease enter the unlock code (ten digits):\r\nInput: "); fgets( unlock_code_str, 50, stdin); } //Use to strtoul to return an unsigned value //atoi returns signed int unlock_code = (uint32_t)strtoul(unlock_code_str, NULL, 10); //printf("%s \n\r", unlock_code_str); printf("\r\nUnlock code hex: 0x%x \r\n", unlock_code); printf("Unlock code: %010u \r\n", unlock_code); printf("Box ID: %u \r\n\r\n", get_box_id()); if (check_checksum(unlock_code)) { printf("Code checksum correct \r\n\r\n"); printf("The unlock count is %u \r\n", GET_UNLOCK_COUNT(unlock_code) ); switch (GET_UNLOCK_DAYS(unlock_code)) { case FULL_UNLOCK: printf("Full unlock \r\n"); break; case TWO_DAYS: printf("Two day unlock \r\n"); break; case FIVE_DAYS: printf("Five day unlock \r\n"); break; case SEVEN_DAYS: printf("Seven day unlock \r\n"); break; case TWO_WEEKS: printf("Two week unlock \r\n"); break; case THREE_WEEKS: printf("Three week unlock \r\n"); break; case ONE_MONTH: printf("One month unlock \r\n"); break; case TWO_MONTHS: printf("Two month unlock \r\n"); break; default: printf("Invalid code \r\n"); break; } } else { printf("Code checksum incorrect"); } printf("\r\n_____________________\r\n"); } } //Keeps the window open until enter is pressed getchar(); return 0; }
/* * Now our virtualfont routine. */ Boolean virtualfont P1C(register fontdesctype *, curfnt) { #ifdef Omega register integer i ; #else register shalfword i ; #endif register shalfword cmd ; register integer k ; register integer length ; #ifdef Omega register integer cc ; #else register shalfword cc ; #endif register chardesctype *cd ; integer scaledsize = curfnt->scaledsize ; #ifdef Omega integer no_of_chars=256; integer maxcc=0; chardesctype *newchardesc; #endif register quarterword *tempr ; fontmaptype *fm, *newf ; #ifdef Omega integer kindfont; kindfont=vfopen(curfnt); /* 1 for TeX, 2 for Omega */ if (!kindfont) return (0) ; #else if (!vfopen(curfnt)) return (0) ; #endif #ifdef DEBUG if (dd(D_FONTS)) (void)fprintf(stderr,"Loading virtual font %s at %.1fpt\n", name, (real)scaledsize/(alpha*0x100000)) ; #endif /* DEBUG */ /* * We clear out some pointers: */ #ifdef Omega if (kindfont==2) { no_of_chars = 65536; curfnt->maxchars=65536; free(curfnt->chardesc); curfnt->chardesc = (chardesctype *) mymalloc(65536 * (integer)sizeof(chardesctype)); } for (i=0; i<no_of_chars; i++) { #else for (i=0; i<256; i++) { #endif curfnt->chardesc[i].TFMwidth = 0 ; curfnt->chardesc[i].packptr = NULL ; curfnt->chardesc[i].pixelwidth = 0 ; curfnt->chardesc[i].flags = 0 ; curfnt->chardesc[i].flags2 = 0 ; } if (vfbyte()!=247) badvf("expected pre") ; if (vfbyte()!=202) badvf("wrong id byte") ; for(i=vfbyte(); i>0; i--) (void)vfbyte() ; k = vfquad() ; check_checksum (k, curfnt->checksum, curfnt->name); k = (integer)(alpha * (real)vfquad()) ; if (k > curfnt->designsize + 2 || k < curfnt->designsize - 2) { (void)sprintf(errbuf,"Design size mismatch in font %s", name) ; error(errbuf) ; } /* * Now we look for font definitions. */ fm = NULL ; while ((cmd=vfbyte())>=243) { if (cmd>246) badvf("unexpected command in preamble") ; newf = vfontdef(scaledsize, cmd-242) ; if (fm) fm->next = newf ; else curfnt->localfonts = newf ; fm = newf ; fm->next = NULL ; /* FIFO */ } /* * Now we get down to the serious business of reading character definitions. */ do { if (cmd==242) { length = vfquad() + 2 ; if (length<2) badvf("negative length packet") ; if (length>65535) badvf("packet too long") ; cc = vfquad() ; #ifdef Omega if (cc<0 || cc>65535) badvf("character code out of range") ; #else if (cc<0 || cc>255) badvf("character code out of range") ; #endif cd = curfnt->chardesc + cc ; cd->TFMwidth = scalewidth(vfquad(), scaledsize) ; } else { length = cmd + 2; cc = vfbyte() ; cd = curfnt->chardesc + cc ; cd->TFMwidth = scalewidth(vftrio(), scaledsize) ; } #ifdef Omega maxcc = (maxcc<cc) ? cc : maxcc; #endif if (cd->TFMwidth >= 0) cd->pixelwidth = ((integer)(conv*cd->TFMwidth+0.5)) ; else cd->pixelwidth = -((integer)(conv*-cd->TFMwidth+0.5)) ; cd->flags = EXISTS ; cd->flags2 = EXISTS ; if (bytesleft < length) { #ifdef DEBUG if (dd(D_MEM)) (void)fprintf(stderr, #ifdef SHORTINT "Allocating new raster memory (%ld req, %ld left)\n", #else "Allocating new raster memory (%d req, %ld left)\n", #endif length, bytesleft) ; #endif /* DEBUG */ if (length > MINCHUNK) { tempr = (quarterword *)mymalloc((integer)length) ; bytesleft = 0 ; } else { raster = (quarterword *)mymalloc((integer)RASTERCHUNK) ; tempr = raster ; bytesleft = RASTERCHUNK - length ; raster += length ; } } else { tempr = raster ; bytesleft -= length ; raster += length ; } cd->packptr = tempr ; length -= 2 ; *tempr++ = length / 256 ; *tempr++ = length % 256 ; for (; length>0; length--) *tempr++ = vfbyte() ; cmd = vfbyte() ; } while (cmd < 243) ; if (cmd != 248) badvf("missing postamble") ; (void)fclose(vffile) ; curfnt->loaded = 2 ; #ifdef Omega newchardesc = (chardesctype *) mymalloc((maxcc+1) * (integer)sizeof(chardesctype)); for (i=0; i<=maxcc; i++) { newchardesc[i] = curfnt->chardesc[i]; } free(curfnt->chardesc); curfnt->chardesc = newchardesc; curfnt->maxchars=maxcc+1; #endif return (1) ; }
/* ========================================================================= */ void bootloader(void) { uint32 addr; uint16 addr_low; uint8 addr_high = 0; uint8 reclen; uint8 rectype; uint8 idx; uint8 buffer[HEX_LINE_LEN_MAX]; uint8 ch; bool hexend = 0; #if getenv("FLASH_ERASE_SIZE")>2 uint16 next_addr = 0; #endif /* until end of HEX file */ while(hexend == 0) { /* get one line of the HEX file via RS232 until we receive CR or */ /* we reached the end of the buffer */ idx = 0; do { /* get one byte */ ch = getch(); /* save to buffer */ buffer[idx] = ch; /* increment buffer index */ idx++; } while(ch != 0x0A); /* get record length */ reclen = get_hexbyte(&buffer[HEX_LEN_START]); /* check for proper checksum */ if (check_checksum(&buffer[HEX_LEN_START], reclen) != 0) { /* checkum error - send negative acknowledge */ putc(NAK); } else { /* checkum ok */ /* get address */ addr_low = make16(get_hexbyte(&buffer[HEX_ADDR_START]), get_hexbyte(&buffer[HEX_ADDR_START+2])); /* make 32 bit address */ addr = make32(addr_high, addr_low); /* get record type */ rectype = get_hexbyte(&buffer[HEX_TYPE_START]); if (rectype == HEX_DATA_REC) { /* only program code memory */ if ((addr_high < 0x30) && (addr >= RESET_VECTOR)) { for (idx=0; idx < reclen; idx++) { buffer[idx] = get_hexbyte(&buffer[HEX_DATA_START+(idx*2)]); } #if getenv("FLASH_ERASE_SIZE") > getenv("FLASH_WRITE_SIZE") #if defined(__PCM__) if ((addr_low!=next_addr)&&(addr_low&(getenv("FLASH_ERASE_SIZE")-1)!=0)) #else if ((addr_low!=next_addr)&&(addr_low&(getenv("FLASH_ERASE_SIZE")/2-1)!=0)) #endif erase_program_eeprom(addr); next_addr = addr_low + 1; #endif write_program_memory(addr, buffer, reclen); } } else if (rectype == HEX_EOF_REC) { hexend = 1; } else if (rectype == HEX_EXTADDR_REC) { /* to save resources, only take lower byte - this */ /* allows 24 bit addresses => enough for PIC18F */ //addr_high = make16(get_hexbyte(&buffer[HEX_DATA_START]), // get_hexbyte(&buffer[HEX_DATA_START+2])); addr_high = get_hexbyte(&buffer[HEX_DATA_START+2]); } /* send positive acknowledge */ putc(ACK); } } }
void bbtfmload P1C(int, n) { #ifdef Omega register integer i, j ; #else register shalfword i ; #endif register integer li ; integer scaledsize ; shalfword nw, nh, nd, ns, hd ; #ifdef Omega integer bc, ec ; integer nco, ncw, npc, no_repeats ; integer scaled[65536] ; integer chardat[65536] ; integer font_level; integer pretend_no_chars; #else shalfword bc, ec ; integer scaled[300] ; integer chardat[256] ; #endif integer slant = 0 ; bbchardesctype *cc ; register fontdesctype *curfnt = baseFonts[n] ; register bbfontdesctype *bbcurfnt = (bbfontdesctype *)mymalloc(sizeof(bbfontdesctype)) ; bbaseFonts[n] = bbcurfnt ; tfmopen(curfnt) ; /* * Next, we read the font data from the tfm file, and store it in * our own arrays. */ #ifdef Omega li = tfm16() ; if (li!=0) { font_level = -1; hd = tfm16() ; bc = tfm16() ; ec = tfm16() ; nw = tfm16() ; nh = tfm16(); nd = tfm16(); ns = tfm16() ; ns += tfm16() ; ns += tfm16() ; ns += tfm16() ; li = tfm16() ; } else { /* In an .ofm file */ font_level = tfm16(); li = tfm32() ; hd = tfm32() ; bc = tfm32() ; ec = tfm32() ; nw = tfm32() ; nh = tfm32() ; nd = tfm32(); ns = tfm32() ; ns += tfm32() ; ns += tfm32() ; ns += tfm32() ; li = tfm32() ; li = tfm32() ; if (font_level==1) { nco = tfm32() ; ncw = tfm32() ; npc = tfm32() ; for (i=0; i<12; i++) li=tfm32(); } } #else li = tfm16() ; hd = tfm16() ; bc = tfm16() ; ec = tfm16() ; nw = tfm16() ; nh = tfm16() ; nd = tfm16() ; ns = tfm16() ; ns += tfm16() ; ns += tfm16() ; ns += tfm16() ; li = tfm16() ; #endif li = tfm32() ; check_checksum (li, curfnt->checksum, curfnt->name); li = tfm32() ; #ifdef Omega pretend_no_chars=ec+1; if (pretend_no_chars<256) pretend_no_chars=256; bbcurfnt->bbchardesc = (bbchardesctype *) xmalloc(pretend_no_chars*sizeof(bbchardesctype)); for (i=2; i<((font_level==1)?nco-29:hd); i++) li = tfm32() ; no_repeats = 0 ; for (i=0; i<pretend_no_chars; i++) #else for (i=2; i<hd; i++) li = tfm32() ; for (i=0; i<256; i++) #endif chardat[i] = -1 ; for (i=bc; i<=ec; i++) { #ifdef Omega if (no_repeats>0) { chardat[i] = chardat[i-1] ; no_repeats-- ; } else if (font_level>=0) { chardat[i] = tfm32() ; li = tfm32() ; if (font_level==1) { no_repeats = tfm16() ; for (j=0; j<(npc|1); j++) tfm16() ; ncw -= 3 + npc/2 ; } } else { chardat[i] = tfm16() ; li = tfm16() ; } #else chardat[i] = tfm16() ; li = tfm16() ; #endif } #ifdef Omega if (font_level==1&&ncw!=0) { sprintf(errbuf, "Table size mismatch in %s", curfnt->name) ; error(errbuf) ; } #endif scaledsize = curfnt->scaledsize ; for (i=0; i<nw + nh + nd; i++) scaled[i] = scalewidth(tfm32(), scaledsize) ; for (i=0; i<ns; i++) tfm32() ; slant = tfm32() ; (void)fclose(tfmfile) ; #ifdef Omega for (i=0; i<pretend_no_chars; i++) { #else for (i=0; i<256; i++) { #endif cc = &(bbcurfnt->bbchardesc[i]) ; if (chardat[i] != -1) { #ifdef Omega if (font_level>=0) { cc->ury = scaled[(chardat[i] >> 16) + nw] ; cc->lly = - scaled[(chardat[i] & 255) + nw + nh] ; } else { cc->ury = scaled[((chardat[i] >> 4) & 15) + nw] ; cc->lly = - scaled[(chardat[i] & 15) + nw + nh] ; } #else cc->ury = scaled[((chardat[i] >> 4) & 15) + nw] ; cc->lly = - scaled[(chardat[i] & 15) + nw + nh] ; #endif cc->llx = 0 ; cc->urx = curfnt->chardesc[i].TFMwidth ; } else {