Beispiel #1
0
static size_t escape_html(unsigned char *out, const unsigned char *in, size_t in_len) {
    size_t i = 0, offset = 0, total = 0;

    for(; i<in_len; i++) {
        switch(in[i]) {
        case '&':
            APPEND_BUFFER("&amp;",  5, 1);
            break;
        case '<':
            APPEND_BUFFER("&lt;",   4, 1);
            break;
        case '>':
            APPEND_BUFFER("&gt;",   4, 1);
            break;
        case '\'':
            APPEND_BUFFER("&#39;",  5, 1);
            break;
        case '\"':
            APPEND_BUFFER("&quot;", 6, 1);
            break;
        }
    }

    // append the rest of the buffer
    memcpy(&out[total], &in[offset], i-offset);

    return total + (i-offset);
}
Beispiel #2
0
static size_t unescape_html(unsigned char *out, const unsigned char *in, size_t in_len) {
    size_t i = 0, offset = 0, total = 0;

    for(; i<in_len; i++) {
        if(in[i] == '&') {
            if (i+3 <= in_len) {
                if (memcmp(&in[i], "&lt;", 4) == 0) {
                    APPEND_BUFFER("<", 1, 4);
                } else if (memcmp(&in[i], "&gt;", 4) == 0) {
                    APPEND_BUFFER(">", 1, 4);
                }
            }
            if (i+4 <= in_len) {
                if (memcmp(&in[i], "&amp;", 5) == 0) {
                    APPEND_BUFFER("&", 1, 5);
                } else if (memcmp(&in[i], "&#39;", 5) == 0) {
                    APPEND_BUFFER("\'", 1, 5);
                }
            }
            if (i+5 <= in_len) {
                if (memcmp(&in[i], "&quot;", 6) == 0) {
                    APPEND_BUFFER("\"", 1, 6);
                }
            }
        }
    }

    // append the rest of the buffer
    memcpy(&out[total], &in[offset], i-offset);

    return total + (i-offset);
}
Beispiel #3
0
KNIEXPORT KNI_RETURNTYPE_VOID
Java_com_sun_midp_jsr82emul_ConnectionEmul_notifyReceived() {
    int myHandle = KNI_GetParameterAsInt(1);
    int buflen = KNI_GetParameterAsInt(3);
    connection_info_t *info = &emul_data.handled_info[myHandle].conn;
    
    LOG1("ConnectionEmul_notifyReceived(%d)", myHandle);
    
    /* otherwise connection is already closed */
    if (info->flags & IN_USE) { 
        info->flags &= ~SKIP_REQUEST;
        
        LOG1("ConnectionEmul_notifyReceived() received %d", buflen);
        
        if (buflen == CONN_FAILURE) {
            info->receive_status = JAVACALL_FAIL;
            
        } else {
            info->receive_status = JAVACALL_OK;
            
            if (buflen == CONN_ENDOF_INP) {
                info->flags |= ENDOF_INP_REACHED;
            } else {
                KNI_StartHandles(1);
                KNI_DeclareHandle(buf);
                KNI_GetParameterAsObject(2, buf);
                
                APPEND_BUFFER(info->in, info->in_len, JavaByteArray(buf), buflen);
                KNI_EndHandles();
            }
                
            LOG1("ConnectionEmul_notifyReceived() info->in_len %d", info->in_len);
        }
        
        javanotify_bt_protocol_event(JAVACALL_EVENT_BT_RECEIVE_COMPLETE, 
            (javacall_handle)myHandle, info->receive_status);
    }
            
    KNI_ReturnVoid();
}
Beispiel #4
0
static size_t escape_javascript(unsigned char *out, const unsigned char *in, size_t in_len) {
    size_t i = 0, offset = 0, total = 0;

    for(; i<in_len; i++) {
        switch(in[i]) {
        case '\\':
            APPEND_BUFFER("\\\\", 2, 1);
            break;
        case '<':
            if (i+1 <= in_len && in[i+1] == '/') {
                APPEND_BUFFER("<\\/", 3, 2);
            }
            break;
        case '\r':
            if (i+1 <= in_len && in[i+1] == '\n') {
                APPEND_BUFFER("\\n", 2, 1);
            } else {
                APPEND_BUFFER("\\n", 2, 1);
            }
            break;
        case '\n':
            APPEND_BUFFER("\\n", 2, 1);
            break;
        case '\"':
            APPEND_BUFFER("\\\"", 2, 1);
            break;
        case '\'':
            APPEND_BUFFER("\\'", 2, 1);
            break;
        }
    }

    // append the rest of the buffer
    memcpy(&out[total], &in[offset], i-offset);

    return total + (i-offset);
}
const char *tag_handler_img(struct taginfo *taginfo, map_arg_t* maparg, spec_tag_t *spec_tag)
{
  struct _img_arg *img_arg = (struct _img_arg *)spec_tag->handler_arg;
  buffer_t *text_buffer = maparg->text_buffer_p;
  buffer_t *url_buffer = img_arg->url_buffer_p;
  int i, j, k, url_len, len, rpos, *w, imgextnum;
  char *file, *ext, *p, *alt;
  char url_md5[33], url[MAX_URL_LEN+1], rpath[MAX_PATH_LEN+1];
  const char *imgext[] = {".jpg", ".png", ".jpeg", ".gif", ".bmp"};

  switch (img_arg->op)
  {
    case IMG_OP_FILTER:
      goto end;
    case IMG_OP_RESERVE:
      for (i=0;i<taginfo->nattrs;i++) {
        if (strcasecmp(taginfo->attrs[i].name, "src") == 0) {
          if(!taginfo->attrs[i].value || !taginfo->attrs[i].value[0]) 
            goto end;
          p = url_mergenormalize(taginfo->attrs[i].value, img_arg->base, img_arg->referer, img_arg->pageisutf8, img_arg->cd,url, MAX_URL_LEN + 1);
          if (!p) goto end;
          APPEND_BUFFER((*text_buffer),"<img ",5);
          APPEND_BUFFER((*text_buffer),"src=\"",5);
          APPEND_BUFFER((*text_buffer),url,strlen(url));
          APPEND_BUFFER((*text_buffer),"\" ",2);
          for (j=0;j<taginfo->nattrs;j++) {
            if (strcasecmp(taginfo->attrs[j].name, "src") != 0) {
              if(!taginfo->attrs[j].value || !taginfo->attrs[j].value[0])
                continue;
              APPEND_BUFFER((*text_buffer),taginfo->attrs[j].name,strlen(taginfo->attrs[j].name));
              APPEND_BUFFER((*text_buffer),"=\"",2);
              APPEND_BUFFER((*text_buffer),taginfo->attrs[j].value,strlen(taginfo->attrs[j].value));
              APPEND_BUFFER((*text_buffer),"\" ",2);
            }
          }
          APPEND_BUFFER((*text_buffer)," />",3);
          goto end;
        }
      }
      break;
    /* tag_len = taginfo->end_position - taginfo->start_position;
      if (text_buffer->free >= tag_len) {
        memcpy(text_buffer->p + text_buffer->pos, taginfo->start_position, tag_len);
        text_buffer->pos += tag_len;
        text_buffer->free -= tag_len;
        goto end;
      } else {
        maparg->last_tag_end_position = taginfo->end_position;
        return NULL;
      } */
    case IMG_OP_REPLACE:
      for (i = 0;i < taginfo->nattrs;i++) {
        if (strcasecmp(taginfo->attrs[i].name, "src") == 0) {
          if(!taginfo->attrs[i].value || !taginfo->attrs[i].value[0])
            goto end;
          p = url_mergenormalize(taginfo->attrs[i].value, img_arg->base, img_arg->referer, img_arg->pageisutf8, img_arg->cd,url, MAX_URL_LEN + 1);
          if (!p) goto end;

          file = strchr(url + 7, '/');
          if (!file) goto end;
          url_len = strlen(url);
          md5s((unsigned char *)url, url_len, url_md5);

          rpos = 0;
          rpath[0] = 0;
          if (img_arg->image_option_dir) {
            strcpy(rpath, img_arg->image_option_dir);
            rpos += strlen(img_arg->image_option_dir);
            if (rpath[rpos-1] != PATH_SEPARATOR) {
              rpath[rpos] = PATH_SEPARATOR;
              rpath[++rpos] = '\0';
            }
          }

          if (img_arg->image_subdir_num) {
            p = url_md5;
            w = img_arg->image_subdirname_width;
            for (k = 0;k < img_arg->image_subdir_num;k++) {
              memcpy(rpath + rpos, p, w[k]);
              rpos += w[k];
              p += w[k];
              rpath[rpos++] = '/';
              rpath[rpos] = 0;
            }
          }

          memcpy(rpath + rpos, url_md5, 32);
          rpos += 32;
          rpath[rpos] = 0;

          ext = strrchr(file, '.');
          if (ext) {
            imgextnum = countof(imgext);
            for (k = 0;k < imgextnum;k++) {
              if (strcasecmp(ext, imgext[k]) == 0)
                break;
            }
            if (k != imgextnum) {
              len = strlen(ext);
              memcpy(rpath + rpos, ext, len);
              rpos += len;
              rpath[rpos] = 0;
            }
          }

          //xhtml中每个图片标签都必须有ALT说明文字。
          //<img src="ball.jpg" alt="large red ball" />
          //保留原来img中alt文本。
          alt = "";
          for (j=0;j<taginfo->nattrs;j++){
            if (strcasecmp(taginfo->attrs[j].name, "alt") == 0 && taginfo->attrs[j].value) {
              alt = taginfo->attrs[j].value;
              break;
            }
          }
          
          APPEND_BUFFER((*text_buffer),"<img ",5);
          APPEND_BUFFER((*text_buffer),"src=\"",5);
          if(img_arg->image_base_url) {
            APPEND_BUFFER((*text_buffer),img_arg->image_base_url,strlen(img_arg->image_base_url));
          }
          APPEND_BUFFER((*text_buffer),rpath,strlen(rpath));
          APPEND_BUFFER((*text_buffer),"\" ",2);
          APPEND_BUFFER((*text_buffer),"alt=\"",5);
          APPEND_BUFFER((*text_buffer),alt,strlen(alt));
          APPEND_BUFFER((*text_buffer),"\" ",2);
          for (j=0;j<taginfo->nattrs;j++) {
            if (strcasecmp(taginfo->attrs[j].name, "src") != 0 && \
                strcasecmp(taginfo->attrs[j].name, "alt") != 0) {
              if(!taginfo->attrs[j].value || !taginfo->attrs[j].value[0])
                continue;
              APPEND_BUFFER((*text_buffer),taginfo->attrs[j].name,strlen(taginfo->attrs[j].name));
              APPEND_BUFFER((*text_buffer),"=\"",2);
              APPEND_BUFFER((*text_buffer),taginfo->attrs[j].value,strlen(taginfo->attrs[j].value));
              APPEND_BUFFER((*text_buffer),"\" ",2);
            }
          }
          APPEND_BUFFER((*text_buffer)," />",3);
          
          len = strlen(img_arg->url_prefix);
          if (url_buffer->free < len + url_len + 1) { /* '\n' */
            RESIZE_BUFFER((*url_buffer), url_buffer->size, (len + url_len + 1)*2)
          }
          memcpy(url_buffer->p + url_buffer->pos, img_arg->url_prefix, len);
          url_buffer->pos += len;
          url_buffer->free -= len;
          memcpy(url_buffer->p + url_buffer->pos, url, url_len);
          url_buffer->pos += url_len;
          url_buffer->free -= url_len;
          *(url_buffer->p + url_buffer->pos) = '\n';
          url_buffer->pos ++;
          url_buffer->free --;
          url_buffer->reserve2++;
          goto end;
        }
      }
      break;
    default:
      break;
  }

end:
  maparg->last_tag_end_position = taginfo->end_position;
  return (taginfo->end_position - 1);
}
Beispiel #6
0
int
more_packet_data( ImportThread *it )
{
	int	rc;
	unsigned char	c;
	int	bns_errno;
	unsigned char bytecount[2];
	unsigned char *bytecountp;
	int	retcode = 0;

	while( ( rc = bnsget( it->bnsin, &c, BYTES, 1 ) ) == 0 ) {

		switch( it->sync_state ) {

		case STREAM_SEARCH: 

			if( c == 0x9c ) {
				
				it->nbytes = 0;

				APPEND_BUFFER( it->buf, it->bufsize, it->nbytes, c );

				it->sync_state = STREAM_CONFIRM;

			} else {

				BAD_BYTE( "search", c );
			}

			break;

		case STREAM_CONFIRM: 

			if( c == 0xa5 ) {
				
				APPEND_BUFFER( it->buf, it->bufsize, it->nbytes, c );

				it->sync_state = STREAM_ACCUMULATE;

				bnsget( it->bnsin, bytecount, BYTES, 1 );
				bnsget( it->bnsin, bytecount+1, BYTES, 1 );

				APPEND_BUFFER( it->buf, it->bufsize, it->nbytes, bytecount[0] );
				APPEND_BUFFER( it->buf, it->bufsize, it->nbytes, bytecount[1] );

				bytecountp = &bytecount[0];
				vs2hs( &bytecountp, &it->intake_packet_bytecount, 1 );

				if( VeryVerbose ) {

					elog_notify( 0, 
					  "Receiving a %d-byte packet...\n",
					  it->intake_packet_bytecount );

				}

			} else {

				it->sync_state = STREAM_SEARCH;

				it->nbytes = 0;

				BAD_BYTE( "search", c );
			}

			break;
	
		case STREAM_ACCUMULATE:

			APPEND_BUFFER( it->buf, it->bufsize, it->nbytes, c );

			if( it->nbytes == it->intake_packet_bytecount ) {

				it->sync_state = STREAM_SEARCH;

				return 1;
			}

			break;

		default:

			BAD_BYTE( "default", c );

			it->sync_state = STREAM_SEARCH;

			break;
		}
	}

	if( bnserr( it->bnsin ) != 0 ) {
			
		bns_errno = bnserrno( it->bnsin );

		if( Verbose ) {

		  if( bns_errno == ECONNRESET ) {

			elog_complain( 1, 
				  "'%s': Connection reset by peer\n",
				  it->name );

		  } else {

			elog_complain( 1, 
				  "'%s': bns error %d\n", 
				  it->name, bns_errno );
		  }
		}

		retcode = -1;

	} else if( bnseof( it->bnsin ) ) {

		if( Verbose ) {

			elog_complain( 1, 
				  "'%s': Connection closed by remote "
				  "export module\n",
				  it->name );
		}

		retcode = -1;
	}

	return retcode;
}