/* Some of this based on ao/src/ao_wav.c */ int write_prelim_header(OggVorbis_File *vf, FILE *out, ogg_int64_t knownlength) { unsigned int size = 0x7fffffff; int channels = ov_info(vf,0)->channels; int samplerate = ov_info(vf,0)->rate; int bytespersec = channels*samplerate*bits/8; int align = channels*bits/8; int samplesize = bits; if(knownlength && knownlength*bits/8*channels < size) size = (unsigned int)(knownlength*bits/8*channels+44) ; memcpy(headbuf, "RIFF", 4); WRITE_U32(headbuf+4, size-8); memcpy(headbuf+8, "WAVE", 4); memcpy(headbuf+12, "fmt ", 4); WRITE_U32(headbuf+16, 16); WRITE_U16(headbuf+20, 1); /* format */ WRITE_U16(headbuf+22, channels); WRITE_U32(headbuf+24, samplerate); WRITE_U32(headbuf+28, bytespersec); WRITE_U16(headbuf+32, align); WRITE_U16(headbuf+34, samplesize); memcpy(headbuf+36, "data", 4); WRITE_U32(headbuf+40, size - 44); if(fwrite(headbuf, 1, 44, out) != 44) { fprintf(stderr, _("ERROR: Failed to write Wave header: %s\n"), strerror(errno)); return 1; } return 0; }
/* Reconstruct nabto header with crypto payload header. */ uint8_t* reconstruct_header(uint8_t* buf, const nabto_packet_header * hdr) { uint8_t* ptr; // insert copy of request header (Length will be inserted later) ptr = buf; /* Write fixed part of packet header */ WRITE_U32(ptr, hdr->nsi_cp); ptr += 4; WRITE_U32(ptr, hdr->nsi_sp); ptr += 4; WRITE_U8(ptr, hdr->type); ptr += 1; WRITE_U8(ptr, hdr->version); ptr += 1; WRITE_U8(ptr, hdr->rsvd); ptr += 1; WRITE_U8(ptr, hdr->flags | NP_PACKET_HDR_FLAG_RESPONSE); ptr += 1; WRITE_U16(ptr, hdr->seq); ptr += 2; /*WRITE_U16(ptr, len);*/ ptr += 2; /* Length to be packed later */ /* Write NSI.co to packet header (optional) */ if (hdr->flags & NP_PACKET_HDR_FLAG_NSI_CO) { memcpy(ptr, (const void*) hdr->nsi_co, 8); ptr += 8; } /* Write tag to packet header (optional) */ if (hdr->flags & NP_PACKET_HDR_FLAG_TAG) { WRITE_U16(ptr, hdr->tag); ptr += 2; } { ptrdiff_t diff = ptr - buf; if (hdr->hlen != (uint16_t)(diff)) { NABTO_LOG_ERROR(("header length mismatch: %" PRIu16 " != %" PRIptrdiff " !!!!!!!!!!!!!!!!!!!", hdr->hlen, diff)); return NULL; } } // insert crypto header (len and code will be inserted later) WRITE_U8(ptr, NP_PAYLOAD_TYPE_CRYPTO); ptr += 1; WRITE_U8(ptr, NP_PAYLOAD_HDR_FLAG_NONE); /*ptr += 1; WRITE_U16(ptr, len); ptr += 2; WRITE_U16(ptr, code); ptr += 2;*/ /* len and code is patched by unabto_encrypt() */ ptr += 5; return ptr; /* return end of packet (so far) */ }
static void send_rendezvous_socket(nabto_socket_t socket, nabto_connect* con, uint16_t seq, nabto_endpoint* dest, nabto_endpoint *myAddress) { uint8_t* ptr; uint8_t* buf = nabtoCommunicationBuffer; ptr = insert_header(buf, 0, con->spnsi, U_CONNECT, false, seq, 0, 0); ptr = insert_payload(ptr, NP_PAYLOAD_TYPE_EP, 0, 6); WRITE_U32(ptr, dest->addr); ptr += 4; WRITE_U16(ptr, dest->port); ptr += 2; if (seq > 0) { if (!myAddress) { NABTO_LOG_ERROR(("Send rendezvous called with an invalid address")); return; } else { ptr = insert_payload(ptr, NP_PAYLOAD_TYPE_EP, 0, 6); WRITE_U32(ptr, myAddress->addr); ptr += 4; WRITE_U16(ptr, myAddress->port); ptr += 2; } } { size_t len = ptr - buf; insert_length(buf, len); if (seq) { NABTO_LOG_DEBUG((PRInsi " RENDEZVOUS Send to " PRIep ": seq=%" PRIu16 " " PRIep, MAKE_NSI_PRINTABLE(0, con->spnsi, 0), MAKE_EP_PRINTABLE(*dest), seq, MAKE_EP_PRINTABLE(*myAddress))); } else { NABTO_LOG_DEBUG((PRInsi " RENDEZVOUS Send to " PRIep ": seq=0", MAKE_NSI_PRINTABLE(0, con->spnsi, 0), MAKE_EP_PRINTABLE(*dest))); } if (dest->addr != 0 && dest->port != 0) { nabto_write(socket, buf, len, dest->addr, dest->port); } else { NABTO_LOG_TRACE(("invalid rendezvous packet thrown away")); } } }
bool unabto_util_test(void) { uint8_t buf[] = {0x00,0x00,0x00,0x00,0x12, 0x34, 0x56, 0x78}; uint32_t* buf2 = (uint32_t*)buf; uint32_t testNr; uint16_t testu16; bool ret = true; READ_U32(testNr, buf+4); if (testNr != 305419896) { // 0x12345678 in host byte order NABTO_LOG_TRACE(("testNr: %i", testNr)); ret = false; } READ_U32(testNr, buf2+1); if (testNr != 0x12345678) { NABTO_LOG_TRACE(("testNr: %i", testNr)); ret = false; } READ_U16(testu16, buf+4); if (testu16 != 4660) { NABTO_LOG_TRACE(("testu16 read failed %i", testu16)); ret = false; } WRITE_U32(buf, 0x01020304); if (buf[0] != 0x01 || buf[1] != 0x02 || buf[2] != 0x03 || buf[3] != 0x04) { NABTO_LOG_TRACE(("WRITE u32 failed")); ret = false; } WRITE_U16(buf, 0x0506); if (buf[0] != 0x05 || buf[1] != 0x06) { NABTO_LOG_TRACE(("WRITE u16 failed")); ret = false; } return ret; }
static int ao_wav_open(ao_device *device, ao_sample_format *format) { ao_wav_internal *internal = (ao_wav_internal *) device->internal; unsigned char buf[WAV_HEADER_LEN]; int size = 0x7fffffff; /* Use a bogus size initially */ /* Store information */ internal->wave.common.wChannels = device->output_channels; internal->wave.common.wBitsPerSample = ((format->bits+7)>>3)<<3; internal->wave.common.wValidBitsPerSample = format->bits; internal->wave.common.dwSamplesPerSec = format->rate; memset(buf, 0, WAV_HEADER_LEN); /* Fill out our wav-header with some information. */ strncpy(internal->wave.riff.id, "RIFF",4); internal->wave.riff.len = size - 8; strncpy(internal->wave.riff.wave_id, "WAVE",4); strncpy(internal->wave.format.id, "fmt ",4); internal->wave.format.len = 40; internal->wave.common.wFormatTag = WAVE_FORMAT_EXTENSIBLE; internal->wave.common.dwAvgBytesPerSec = internal->wave.common.wChannels * internal->wave.common.dwSamplesPerSec * (internal->wave.common.wBitsPerSample >> 3); internal->wave.common.wBlockAlign = internal->wave.common.wChannels * (internal->wave.common.wBitsPerSample >> 3); internal->wave.common.cbSize = 22; internal->wave.common.subFormat = WAVE_FORMAT_PCM; internal->wave.common.dwChannelMask=device->output_mask; strncpy(internal->wave.data.id, "data",4); internal->wave.data.len = size - WAV_HEADER_LEN; strncpy(buf, internal->wave.riff.id, 4); WRITE_U32(buf+4, internal->wave.riff.len); strncpy(buf+8, internal->wave.riff.wave_id, 4); strncpy(buf+12, internal->wave.format.id,4); WRITE_U32(buf+16, internal->wave.format.len); WRITE_U16(buf+20, internal->wave.common.wFormatTag); WRITE_U16(buf+22, internal->wave.common.wChannels); WRITE_U32(buf+24, internal->wave.common.dwSamplesPerSec); WRITE_U32(buf+28, internal->wave.common.dwAvgBytesPerSec); WRITE_U16(buf+32, internal->wave.common.wBlockAlign); WRITE_U16(buf+34, internal->wave.common.wBitsPerSample); WRITE_U16(buf+36, internal->wave.common.cbSize); WRITE_U16(buf+38, internal->wave.common.wValidBitsPerSample); WRITE_U32(buf+40, internal->wave.common.dwChannelMask); WRITE_U16(buf+44, internal->wave.common.subFormat); memcpy(buf+46,"\x00\x00\x00\x00\x10\x00\x80\x00\x00\xAA\x00\x38\x9B\x71",14); strncpy(buf+60, internal->wave.data.id, 4); WRITE_U32(buf+64, internal->wave.data.len); if (fwrite(buf, sizeof(char), WAV_HEADER_LEN, device->file) != WAV_HEADER_LEN) { return 0; /* Could not write wav header */ } device->driver_byte_format = AO_FMT_LITTLE; return 1; }