int hex4(uint8_t **buffer, uint16_t *value) { uint8_t v; if ((hex2(buffer, &v)) < 0) { return -1; } *(value) = (v << 8); if ((hex2(buffer, &v)) < 0) { return -1; } *(value) |= v; return 0; }
// File Operation Example void file_ex () { // Degree 163 Binary Field from fips186-2 use_NIST_B_163 (); EC_Domain_Parameters dp = NIST_B_163; ECPrivKey privKeyA (dp, OS2IP(SHA1("A's password"))); ECPubKey pubKeyA (privKeyA); DER derA1 (pubKeyA); std::ofstream key_outfile ("pubKeyA.key", std::ios::binary); if (!key_outfile) { std::cout << "Error can't open file!\n"; return; } key_outfile << derA1; key_outfile.close(); std::ifstream key_infile ("pubKeyA.key", std::ios::binary); if (!key_infile) { std::cout << "Error can't open file!\n"; return; } OCTETSTR derA2_v; char c; OCTET o; while (key_infile.get (c)) { o = (unsigned char)c; derA2_v.push_back (o); } std::cout << std::endl; key_infile.close(); DER derA2 (derA2_v); HexEncoder hex1 (derA1); HexEncoder hex2 (derA2); std::cout << "derA1: " << hex1 << std::endl; std::cout << "derA2: " << hex2 << std::endl; if (!(derA1.v == derA2.v)) { std::cout << "Error\n"; return; } else { std::cout << "OK\n"; } return; }
int send_wol_packet(unsigned char *broadcast_addr, unsigned char *mac_addr) { int sd; int optval; char unsigned buf[BUFMAX]; size_t len; struct sockaddr_in sin; unsigned char mac[MACLEN]; unsigned char *p; int i, j; bzero((char *)&sin, sizeof(sin)); sin.sin_family = AF_INET; host2addr((char *)broadcast_addr, &sin.sin_addr, &sin.sin_family); sin.sin_port = htons(9); if ((sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { fprintf(stderr, "Can't get socket.\n"); return 1; } optval = 1; if (setsockopt(sd, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval)) < 0) { fprintf(stderr, "Can't set sockopt: %d.\n", errno); return 1; } p = mac_addr; j = hex2(p); if (j < 0) { MACerror: fprintf(stderr,"Illegal MAC address: %s.\n", mac_addr); return 1; } mac[0] = j; p += 2; for (i = 1; i < MACLEN; i++) { if (*p++ != ':') goto MACerror; j = hex2(p); if (j < 0) goto MACerror; mac[i] = j; p += 2; } p = buf; for (i = 0; i < 6; i++) { *p++ = 0xFF; } for (i = 0; i < 16; i++) { for (j = 0; j < MACLEN; j++) { *p++ = mac[j]; } } len = p - buf; if (sendto(sd, buf, len, 0, (struct sockaddr*)&sin, sizeof(sin)) != len) { fprintf(stderr, "Failed to send: %d.\n", errno); } return 0; }
int process_ihexfile(const char *ihexfile, struct record **rec_p) { int fd; int rc; int i; int len; uint8_t *ptr; uint8_t c; uint8_t ih_count; uint16_t ih_address; uint8_t ih_type; uint8_t ih_data[256]; uint8_t ih_checksum; uint32_t ip = 0; struct record *rec; fd = open(ihexfile, O_RDONLY); if (fd < 0) { fprintf(stderr, "process_ihexfile: open failed (%s) :%s\n", ihexfile, strerror(errno)); return -1; } while (1) { ptr = io_buffer; len = read_fully(fd, ptr, 1); if (len < 0) goto read_error; else if (len == 0) break; c = *(ptr++); if (c == '\n') continue; else if (c == '\r') continue; if (c != ':') goto data_error; ptr = io_buffer; len = read_fully(fd, ptr, 8); if (len < 0) goto read_error; else if (len == 0) goto data_error; if (hex2(&ptr, &ih_count) < 0) goto data_error; if (hex4(&ptr, &ih_address) < 0) goto data_error; if (hex2(&ptr, &ih_type) < 0) goto data_error; if (ih_count > 0) { ptr = io_buffer; len = read_fully(fd, ptr, (ih_count * 2)); if (len < 0) goto read_error; else if (len == 0) goto data_error; for (i = 0; i < ih_count; i++) { if (hex2(&ptr, &ih_data[i]) < 0) goto data_error; } } ptr = io_buffer; len = read_fully(fd, ptr, 2); if (len < 0) goto read_error; else if (len == 0) goto data_error; if (hex2(&ptr, &ih_checksum) < 0) goto data_error; if (ih_type == 0x00) { rec = malloc(sizeof(struct record) + (sizeof(uint8_t) * ih_count)); if (rec == NULL) goto memory_error; rec->next = NULL; rec->count = ih_count; rec->address = ip + ih_address; memcpy(rec->data, ih_data, ih_count); *(rec_p) = rec; rec_p = &rec->next; } else if (ih_type == 0x02) { ip = (((ih_data[0] << 8) | (ih_data[1] << 0)) << 4); } else if (ih_type == 0x04) { ip = (((ih_data[0] << 8) | (ih_data[1] << 0)) << 16); } } rc = 0; goto close; memory_error: fprintf(stderr, "process_ihexfile: memory error\n"); rc = -1; goto close; read_error: fprintf(stderr, "process_ihexfile: read error\n"); rc = -1; goto close; data_error: fprintf(stderr, "process_ihexfile: data error\n"); rc = -1; goto close; close: if (close(fd) < 0) { fprintf(stderr, "process_ihexfile: close failed %s\n", strerror(errno)); return -1; } return rc; }